#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>
void f()
{
void *array[16];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 16);
strings = backtrace_symbols (array, size);
for (i=0; i<size; i++)
printf ("%s\n", strings[i]);
printf ("\n");
free (strings);
}
void f1()
{
f();
}
void f2()
{
f1();
}
int main()
{
f();
f1();
f2();
return 0;
}
g++ -o myTrce.o -c myTrace.cpp
-A、-o或--print-file-name:在找到的各个符号的名字前加上文件名,而不是在此文件的所有符号前只出现文件名一次.-a或--debug-syms:显示调试符号.-B:等同于--format=bsd,用来兼容MIPS的nm.-C或--demangle:将低级符号名解码(demangle)成用户级名字.这样可以使得C++函数名具有可读性.
nm myTrce.o
0000000000000000 T _Z1fv
000000000000007a T _Z2f1v
0000000000000086 T _Z2f2v
U __gxx_personality_v0
U backtrace
U backtrace_symbols
U free
0000000000000092 T main
U putchar
U puts
nm -Aa myTrce.o
myTrce.o:0000000000000000 b .bss
myTrce.o:0000000000000000 n .comment
myTrce.o:0000000000000000 d .data
myTrce.o:0000000000000000 r .eh_frame
myTrce.o:0000000000000000 n .note.GNU-stack
myTrce.o:0000000000000000 t .text
myTrce.o:0000000000000000 T _Z1fv
myTrce.o:000000000000007a T _Z2f1v
myTrce.o:0000000000000086 T _Z2f2v
myTrce.o: U __gxx_personality_v0
myTrce.o: U backtrace
myTrce.o: U backtrace_symbols
myTrce.o: U free
myTrce.o:0000000000000092 T main
myTrce.o:0000000000000000 a myTrace.cpp
myTrce.o: U putchar
myTrce.o: U puts
> nm -AC myTrce.o
myTrce.o:0000000000000000 T f()
myTrce.o:000000000000007a T f1()
myTrce.o:0000000000000086 T f2()
myTrce.o: U __gxx_personality_v0
myTrce.o: U backtrace
myTrce.o: U backtrace_symbols
myTrce.o: U free
myTrce.o:0000000000000092 T main
myTrce.o: U putchar
myTrce.o: U puts
> nm -AC myTrce.o > myT1
> nm -A myTrce.o > myT21
> diff myT1 myT21 -y -w
myTrce.o:0000000000000000 T f() | myTrce.o:0000000000000000 T _Z1fv
myTrce.o:000000000000007a T f1() | myTrce.o:000000000000007a T _Z2f1v
myTrce.o:0000000000000086 T f2() | myTrce.o:0000000000000086 T _Z2f2v
myTrce.o: U __gxx_personality_v0 myTrce.o: U __gxx_personality_v0
myTrce.o: U backtrace myTrce.o: U backtrace
myTrce.o: U backtrace_symbols myTrce.o: U backtrace_symbols
myTrce.o: U free myTrce.o: U free
myTrce.o:0000000000000092 T main myTrce.o:0000000000000092 T main
myTrce.o: U putchar myTrce.o: U putchar
myTrce.o: U puts myTrce.o: U puts
g++ -g -o myTrace -rdynamic myTrace.cpp
./myTrace
./myTrace(_Z1fv+0x1c) [0x400964]
./myTrace(main+0x9) [0x4009e3]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x2ad8c3e75304]
./myTrace(__gxx_personality_v0+0x39) [0x4008b9]
./myTrace(_Z1fv+0x1c) [0x400964]
./myTrace(_Z2f1v+0x9) [0x4009cb]
./myTrace(main+0xe) [0x4009e8]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x2ad8c3e75304]
./myTrace(__gxx_personality_v0+0x39) [0x4008b9]
./myTrace(_Z1fv+0x1c) [0x400964]
./myTrace(_Z2f1v+0x9) [0x4009cb]
./myTrace(_Z2f2v+0x9) [0x4009d7]
./myTrace(main+0x13) [0x4009ed]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x2ad8c3e75304]
./myTrace(__gxx_personality_v0+0x39) [0x4008b9]
- #include <stdio.h>
- #include <stdlib.h>
- #include <execinfo.h>
- /* Obtain a backtrace and print it to stdout. */
- void
- print_trace (void)
- {
- void *array[10];
- size_t size;
- char **strings;
- size_t i;
- size = backtrace (array, 10);
- strings = backtrace_symbols (array, size);
- printf ("Obtained %zd stack frames.\n", size);
- for (i = 0; i < size; i++)
- printf ("%s\n", strings);
- free (strings);
- }
- int main()
- {
- print_trace ();
- }
编译:g++ stack.c -g -o stack -rdynamic
另外一个方法:
- #include <unistd.h>
- #include <stdio.h>
- #include <execinfo.h>
- #include <stdlib.h>
- #include <string.h>
- void backtrace()
- {
- const int maxLevel = 200;
- void* buffer[maxLevel];
- int level = backtrace(buffer, maxLevel);
- const int SIZE = 1024;
- char cmd[SIZE] = "addr2line -C -f -e ";
- // let prog point to the end of "cmd"
- char* prog = cmd + strlen(cmd);
- int r = readlink("/proc/self/exe", prog, sizeof(cmd) - (prog-cmd)-1);
- FILE* fp = popen(cmd, "w");
- if (!fp)
- {
- perror("popen");
- return;
- }
- for (int i = 0; i < level; ++i)
- {
- fprintf(fp, "%p\n", buffer[i]);
- }
- fclose(fp);
- }
- void foo(int, char*)
- {
- backtrace();
- }
- void bar(double)
- {
- foo(0, NULL);
- }
- int main()
- {
- bar(0.0);
- //A a;
- return 0;
- }