xcode 如何在命令行应用程序中使用 Instruments 和显示控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1735000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:57:16  来源:igfitidea点击:

How use Instruments and display the console in Command Lines applications

cxcodedebuggingmacosinstruments

提问by Hugo Sereno Ferreira

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.

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

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):

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

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:

这是我在启动 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.

Any thoughts?

有什么想法吗?

回答by CommanderHK

It's easy. See the screenshot.

这很简单。看截图。

screenshot

截屏

回答by trojanfoe

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( manpage)。这允许通过将其添加到命令行的开头来从命令行收集数据:

iprofiler -leaks -d $HOME/tmp

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

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

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

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

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

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

If you are compiling using clangthen simply add both -O3and -g(clangdoesn't support the -pgcommand line option).

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

回答by jtbandes

See this questionfor your answer.

请参阅此问题以获得答案。

From Brad Larson therein:

来自布拉德·拉森(Brad Larson):

Run your application from within Xcode to have the console output piped there. While your application is running, start Instruments and choose an appropriate instrument. Under Default Target in the menu bar, select iPhone or Computer (whichever is appropriate for what you're testing), and under Attach to Process find the name of your executable.

When you click the record button, your application should start being profiled under Instruments while having its console output directed to Xcode. Unfortunately, this attachment process will need to come after the application has started, so you may have to profile the startup of your application separately.

从 Xcode 中运行您的应用程序,以便在那里通过管道传输控制台输出。在您的应用程序运行时,启动 Instruments 并选择合适的仪器。在菜单栏中的默认目标下,选择 iPhone 或计算机(以适合您正在测试的内容为准),然后在附加到进程下找到您的可执行文件的名称。

当您单击记录按钮时,您的应用程序应该开始在 Instruments 下进行分析,同时将其控制台输出定向到 Xcode。不幸的是,此附件过程需要在应用程序启动后进行,因此您可能必须单独配置应用程序的启动情况。

Edit:If that didn't work, you may just need to restart your computer. Have you done that yet?

编辑:如果这不起作用,您可能只需要重新启动计算机。你已经这样做了吗?

回答by ileitch

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

IO 选项