Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print callstack at runtime (XCode)

Is it possible?

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

like image 735
brigadir Avatar asked Feb 03 '26 04:02

brigadir


2 Answers

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);
    }
}
like image 108
Costique Avatar answered Feb 05 '26 23:02

Costique


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

like image 40
Vladimir Avatar answered Feb 06 '26 00:02

Vladimir