在运行时打印调用堆栈(XCode) [英] Print callstack at runtime (XCode)

查看:164
本文介绍了在运行时打印调用堆栈(XCode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能吗?

我已经找到了Visual Studio的解决方案打印n个级别的调用堆栈?

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

推荐答案

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

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

这篇关于在运行时打印调用堆栈(XCode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆