【问题标题】:How use Instruments and display the console in Command Lines applications如何在命令行应用程序中使用 Instruments 并显示控制台
【发布时间】:2010-12-16 15:21:48
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: c xcode debugging macos instruments


    【解决方案1】:

    这很容易。看截图。

    【讨论】:

      【解决方案2】:

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

      iprofiler -leaks -d $HOME/tmp
      

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

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

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

      如果您使用clang 进行编译,则只需添加-O3-gclang 不支持-pg 命令行选项)。

      【讨论】:

        【解决方案3】:

        您可以在选择目标时更改“选项”下拉菜单中的输出。输出将出现在系统控制台(Applications/Utilities/Console)中。

        【讨论】:

        • 这似乎没有任何区别。
        • 如果您从终端启动仪器(在我的计算机上运行 /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/MacOS/Instruments),它确实有效。如果你这样做并选择“系统控制台”,你会得到输出。
        • 当我从终端启动它时,我什么也得不到——根本没有输出,也没有 GUI。
        猜你喜欢
        • 2018-08-16
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多