不多说,见如下文件。
- backtrace.c:
- #include <execinfo.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BT_MAX_TRACE 100
- void print_trace (void)
- {
- void *array[BT_MAX_TRACE];
- size_t size;
- char **strings;
- size_t i;
- size = backtrace (array, BT_MAX_TRACE);
- strings = backtrace_symbols (array, size);
- printf ("Obtained %zd stack frames.\n", size);
- for (i = 0; i < size; i++)
- printf ("%s\n", strings[i]);
- free (strings);
- }
- void dummy_function (void)
- {
- print_trace ();
- }
- void test1()
- {
- dummy_function();
- }
- void test2()
- {
- test1();
- }
- int main (void)
- {
- test2();
- return 0;
- }
编译成backtrace,执行即可看出效果:gcc -g -rdynamic -o backtrace backtrace.c
# ./bt
Obtained 7 stack frames.
./bt(print_trace+0x1f) [0x80487b3]
./bt(dummy_function+0xb) [0x8048822]
./bt(test1+0xb) [0x804882f]
./bt(test2+0xb) [0x804883c]
./bt(main+0xb) [0x8048849]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0x17fbd6]
./bt() [0x8048701]
阅读(218) | 评论(0) | 转发(0) |