转载请注明出处:http://blog.csdn.net/horkychen
有时在查找问题时,
在Xcode下可以编辑断点设置中的Action设为Debug
记得勾选"Automatically continue after evaluating", 这样程序就不会停在这个断点,而是继续执行下去。
运行结果:
#0 a (i=5) at /xxxx/TestBacktrace/main.c:20
#1 0x0000000100000e72 in main (argc=1, argv=0x7fff5fbffa88) at /xxxx/TestBacktrace/main.c:25
如果使用LLDB作为调试器,则输入bt, 其运行结果如下:
* thread #1: tid = 0x2503, 0x0000000102238e37 TestBacktrace`a + 7 at main.c:20, stop reason = breakpoint 1.1
frame #0: 0x0000000102238e37 TestBacktrace`a + 7 at main.c:20
frame #1: 0x0000000102238e72 TestBacktrace`main + 34 at main.c:25
frame #2: 0x0000000102238d44 TestBacktrace`start + 52
Debugger的选择在这里:
另外也可以在代码中调用backtrace函数实现。
#include <execinfo.h>
void printCallStack(void)
{
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i)
{
printf("%s\n", strs[i]);
}
free(strs);
}
int a(int i)
{
printCallStack();
return i+1;
}
...
输出结果:
0 TestBacktrace 0x00000001013ddd6a printCallStack + 42
1 TestBacktrace 0x00000001013dde30 a + 16
2 TestBacktrace 0x00000001013dde72 main + 34
3 TestBacktrace 0x00000001013ddd34 start + 52
Windows下在Visual Studio,有类似的做法, 贴两张图就应该懂了: