22

I'm using Xcode on OSX to develop command line C applications. I would also like to use Instruments to profile and find memory leaks.

However, I couldn't find a way to display the console when launching the application from within Instruments. I'm also unable to attach to a running command line process (it exits with an error):

Here's an example code:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <setjmp.h>

static sigjmp_buf jmpbuf;

void handler(int sig) {
    char c[BUFSIZ];

    printf ("Got signal %d\n", sig);
    printf ("Deseja sair? (s/n) ");

    fgets(c, sizeof(c), stdin);

    if(c[0] == 's') {
        exit(0);
    } else {
        siglongjmp(jmpbuf, 1);
    }
}

int main(void) {
    char buf[BUFSIZ];

    signal(SIGINT, handler);

    sigsetjmp(jmpbuf, 1);

    while(1) {
        printf(">>>");
        fgets(buf, sizeof(buf), stdin);
        printf ("Introduziu: %s\n", buf);
    }

    return(0);
}

Here's the error I got after launching Instruments, and trying to attach to the running process in xcode:

[Switching to process 1475]
[Switching to process 1475]
Error while running hook_stop:
sharedlibrary apply-load-rules all
Error while running hook_stop:
Invalid type combination in ordering comparison.
Error while running hook_stop:
Invalid type combination in ordering comparison.
Error while running hook_stop:
Error while running hook_stop:
Error while running hook_stop:
Error while running hook_stop:
Error while running hook_stop:
Error while running hook_stop:
Error while running hook_stop:

Unable to disassemble __CFInitialize.

Any thoughts?

Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92

3 Answers3

27

It's easy. See the screenshot.

screenshot

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
CommanderHK
  • 1,128
  • 11
  • 7
15

It's a little late to contribute to this old thread, however I have found the best way of profiling a command line utility is to use iprofiler (manpage). This allows data to be collected from the command line simply by adding this to the start of the command line:

iprofiler -leaks -d $HOME/tmp

(I have a private temporary directory at $HOME/tmp, so you might need to use /tmp or leave the -d command line option off altogether).

My test scripts automatically add that to the command line if $FINDLEAKS is defined (and will prepend valgrind if running under Linux).

This then generates a .dtps file (actually a directory) which can be loaded and anaylysed using Instruments.

If you are compiling using clang then simply add both -O3 and -g (clang doesn't support the -pg command line option).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
3

You can change the output in the Options dropdown when choosing your target. The output will appear in the system Console (Applications/Utilities/Console).

IO options

ileitch
  • 532
  • 4
  • 12
  • That doesn't appear to make any difference. – trojanfoe May 11 '13 at 13:15
  • 1
    It does work if you launch instruments from a terminal (run `/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/MacOS/Instruments` on my computer). If you do this and select "System Console" you get the output. – cobbal Aug 06 '13 at 17:26
  • When I launch it from a terminal, I get nothing — no output at all, and no GUI. – Translunar Jun 09 '14 at 16:34