debugging 如何在命令行应用程序中使用仪器和显示控制台

omvjsjqw  于 2022-11-14  发布在  其他
关注(0)|答案(3)|浏览(165)

我正在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.

有什么想法吗?

v1uwarro

v1uwarro1#

很简单,看截图。

t9eec4r0

t9eec4r02#

现在开始讨论这个老主题有点晚了,但是我发现分析命令行实用程序的最好方法是使用iprofilermanpage)。这样,只需在命令行的开头添加以下内容,就可以从命令行收集数据:

iprofiler -leaks -d $HOME/tmp

(我在$HOME/tmp中有一个私有的临时目录,因此您可能需要使用/tmp或完全关闭-d命令行选项)。
如果定义了$FINDLEAKS,我的测试脚本会自动将其添加到命令行中(如果在Linux下运行,则会在前面添加valgrind)。
然后生成一个.dtps文件(实际上是一个目录),可以使用 Instruments 加载和分析。
如果使用clang进行编译,则只需添加-O3-gclang 不支持-pg命令行选项)。

juzqafwq

juzqafwq3#

您可以在选择目的时,变更“选项”下拉式清单中的输出。输出会显示在系统主控台(应用程序/公用程式/主控台)中。

相关问题