How to NSLog a Call stack when a program is running?


Of course there is. If you can use the Cocoa framework:

NSLog(@"%@", [NSThread callStackSymbols]);

(Documentation.)

If you can't use it:

#include <execinfo.h>

int size = 256;
void *stack[size];
size = backtrace(stack, size);

char **syms = backtrace_symbols(stack, size);
for (int i = 0; i < size; i++) {
    printf("Frame #%d: %s\n", i, syms[i]);
}
free(syms);

(Documentation.)

參考http://stackoverflow.com/questions/13319551/how-to-nslog-a-call-stack-when-a-program-is-running


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章