22

我在 OSX 上使用 Xcode 来开发命令行 C 应用程序。我还想使用 Instruments 来分析和查找内存泄漏。

但是,从 Instruments 中启动应用程序时,我找不到显示控制台的方法。我也无法附加到正在运行的命令行进程(它退出并出现错误):

这是一个示例代码:

#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);
}

这是我在启动 Instruments 并尝试附加到 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.

有什么想法吗?

4

3 回答 3

27

这简单。请参阅屏幕截图。

截屏

于 2014-10-05T02:35:23.593 回答
15

为这个旧线程做出贡献有点晚了,但是我发现分析命令行实用程序的最佳方法是使用iprofiler手册页)。这允许从命令行收集数据,只需将其添加到命令行的开头:

iprofiler -leaks -d $HOME/tmp

(我在 有一个私人临时目录$HOME/tmp,因此您可能需要使用/tmp-d完全关闭命令行选项)。

$FINDLEAKS如果已定义,我的测试脚本会自动将其添加到命令行中(valgrind如果在 Linux 下运行,则会在前面添加)。

然后这会生成一个.dtps文件(实际上是一个目录),可以使用Instruments加载和分析该文件。

如果您正在使用编译,clang那么只需添加两者-O3-gclang不支持-pg命令行选项)。

于 2013-05-11T14:56:59.083 回答
3

选择目标时,您可以在选项下拉列表中更改输出。输出将出现在系统控制台(应用程序/实用程序/控制台)中。

IO 选项

于 2012-05-12T20:03:32.007 回答