Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode - Call stack trace on assert?

Right now when one of my asserts is triggered in Xcode, I get the assert message, and a dump of the stack, which is full of numbers that are not very meaningful to me.

In order to get a trace of the call stack, it requires me to debug the application, and run it up to the point where the assert occurred, and hope that it asserts again. For bugs that are 100% reproducible, this isn't too huge of a problem, but is still a waste of time.

It would be much better if I got a call stack trace every time an assert is hit.

How do you define an assert macro that will dump a call stack trace in Xcode?

like image 268
longshot Avatar asked Feb 17 '10 20:02

longshot


2 Answers

NSThread has a class method called callStackSymbols (and NSException has an instance method of the same name). Sorry, I don't regularly use exceptions and don't regularly use asserts either (not proud of either fact), so I'm not sure what the assertion macro should do.

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        NSLog (@"%s failed assertion\n%@", #x, [NSThread callStackSymbols]); \
        abort(); \
    } \
} while(0) 

Or, as KennyTM kindly pointed out, you can use backtrace_symbols. There is even a method that outputs the symbols directly to a file descriptor, backtrace_symbols_fd.

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        void *stack[128]; \
        int count; \
        fputs (#x " failed assertion.\n", stderr); \
        count = backtrace (stack, sizeof stack / sizeof (void *)); \
        backtrace_symbols_fd (stack, count, STDERR_FILENO); \
    } \
while (0)
like image 125
dreamlax Avatar answered Sep 27 '22 21:09

dreamlax


On iOS 4.x you can use [NSThread callStackSymbols] so.

like image 27
kalibannez Avatar answered Sep 27 '22 21:09

kalibannez