在运行时打印调用堆栈(XCode)

发布于 2024-12-27 12:34:36 字数 143 浏览 4 评论 0原文

是否可以?

我找到了 Visual Studio 的解决方案 打印 n 层调用堆栈?

Is it possible?

I have found solution for Visual Studio Print n levels of callstack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2025-01-03 12:34:36

要以编程方式在运行时打印回溯,可以使用以下函数:

#import <execinfo.h>

void PrintBacktrace ( void )
{
    void *callstack[128];
    int frameCount = backtrace(callstack, 128);
    char **frameStrings = backtrace_symbols(callstack, frameCount);

    if ( frameStrings != NULL ) {
        // Start with frame 1 because frame 0 is PrintBacktrace()
        for ( int i = 1; i < frameCount; i++ ) {
            printf("%s\n", frameStrings[i]);
        }
        free(frameStrings);
    }
}

To print a backtrace at runtime programmatically, you can use this function:

#import <execinfo.h>

void PrintBacktrace ( void )
{
    void *callstack[128];
    int frameCount = backtrace(callstack, 128);
    char **frameStrings = backtrace_symbols(callstack, frameCount);

    if ( frameStrings != NULL ) {
        // Start with frame 1 because frame 0 is PrintBacktrace()
        for ( int i = 1; i < frameCount; i++ ) {
            printf("%s\n", frameStrings[i]);
        }
        free(frameStrings);
    }
}
浅忆 2025-01-03 12:34:36

使用 bt (或 gdb 控制台中的 backtrace 命令)。以下是有关命令使用的更多信息

要打印调用堆栈的多个顶层,您可以使用 bt n

Use bt (or backtrace command in gdb console). Here's more info on command usage.

To print a number of top levels of call stacks you can use bt n

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文