How do you print out a stack trace to the console/log in Cocoa?

Objective CCocoaCallstackIntrospection

Objective C Problem Overview


I'd like to log the call trace during certain points, like failed assertions, or uncaught exceptions.

Objective C Solutions


Solution 1 - Objective C

This code works on any thread:

NSLog(@"%@", NSThread.callStackSymbols);

> Returns an array containing the call stack symbols. Each element is an NSString object with a value in a format determined by the backtrace_symbols() function.

Solution 2 - Objective C

n13's answer didn't quite work - I modified it slightly to come up with this

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retval;
        @try{
            retval = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
        @catch (NSException *exception)
        {
            NSLog(@"Gosh!!! %@", [exception callStackSymbols]);
            @throw;
        }
        return retval;
    }
}

Solution 3 - Objective C

Cocoa already logs the stack trace on uncaught exceptions to the console although they're just raw memory addresses. If you want symbolic information in the console there's some sample code from Apple.

If you want to generate a stack trace at an arbitrary point in your code (and you're on Leopard), see the backtrace man page. Before Leopard, you actually had to dig through the call stack itself.

Solution 4 - Objective C

This pretty much tells you what to do.

Essentially you need to set up the applications exception handling to log, something like:

#import <ExceptionHandling/NSExceptionHandler.h>

[[NSExceptionHandler defaultExceptionHandler] 
                  setExceptionHandlingMask: NSLogUncaughtExceptionMask | 
                                            NSLogUncaughtSystemExceptionMask | 
                                            NSLogUncaughtRuntimeErrorMask]

Solution 5 - Objective C

For exceptions, you can use the NSStackTraceKey member of the exception's userInfo dictionary to do this. See http://developer.apple.com/documentation/Cocoa/Conceptual/Exceptions/Tasks/ControllingAppResponse.html#//apple_ref/doc/uid/20000473-DontLinkElementID_3">Controlling a Program's Response to Exceptions on Apple's website.

Solution 6 - Objective C

In swift print this way:

print("stack trace:\(Thread.callStackSymbols)")

Solution 7 - Objective C

If you want to get it as NSString.

[NSThread  callStackSymbols].description

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrobottoborView Question on Stackoverflow
Solution 1 - Objective CsmokrisView Answer on Stackoverflow
Solution 2 - Objective CZayin KrigeView Answer on Stackoverflow
Solution 3 - Objective Cvt.View Answer on Stackoverflow
Solution 4 - Objective CMax StewartView Answer on Stackoverflow
Solution 5 - Objective CBen GottliebView Answer on Stackoverflow
Solution 6 - Objective CDipakView Answer on Stackoverflow
Solution 7 - Objective CmiragesseeView Answer on Stackoverflow