Print callstack at runtime (XCode)

强颜欢笑 提交于 2020-01-02 08:04:08

问题


Is it possible?

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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/8891382/print-callstack-at-runtime-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!