The source program is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <stdint. h>
Typedef uint32_t uint32;
Void fun3 (void)
{
Void * array [10] = {0 };
Uint32 size = 0;
Char ** strframe = NULL;
Uint32 I = 0, j = 0;
Size = backtrace (array, 10 );
Strframe = (char **) backtrace_symbols (array, size );
Printf ("Print call frame now:/N ");
For (I = 0; I <size; I ++ ){
Printf ("frame % d -- % s/n", I, strframe [I]);
}
If (strframe)
{
Free (strframe );
Strframe = NULL;
}
}
Void fun2 (void)
{
Fun3 ();
}
Void fun1 (void)
{
Fun2 ();
}
Int main (void)
{
Fun1 ();
Return 0;
}
Linux-XMS:/data/test # GCC test. c
Linux-XMS:/data/test #./A. Out
Print call frame now:
Frame 0 --./A. out [0x80484fe]
Frame 1 --./A. out [0x8048582]
Frame 2 --./A. out [0x804858f]
Frame 3 --./A. out [0x80485a7]
Frame 4 --/lib/libc. so.6 (_ libc_start_main + 0xdc) [0xb7e188ac]
Frame 5 --./A. out [0x8048431]
Only addresses can be seen
Modify compilation Parameters
Linux-XMS:/data/test # gcc-rdynamic test. c
Linux-XMS:/data/test #./A. Out
Print call frame now:
Frame 0 --./A. Out (fun3 + 0x4a) [0x80486de]
Frame 1 --./A. Out (fun2 + 0xb) [0x8048762]
Frame 2 --./A. Out (fun1 + 0xb) [0x802136f]
Frame 3 --./A. Out (main + 0x16) [0x8048787]
Frame 4 --/lib/libc. so.6 (_ libc_start_main + 0xdc) [0xb7e588ac]
Frame 5 --./A. out [0x8048611]
Now we can see the function name, but there is no row number, but it doesn't matter if addr2line provides this function.
Then we try to use addr2line to view the function and row number corresponding to the address.
Linux-XMS:/data/test # addr2line 0x80486de-E./A. Out-F
Fun3
?? : 0
Failed. Don't worry. Let's modify the compilation parameters again.
Linux-XMS:/data/test # gcc-g-rdynamic test. c
Linux-XMS:/data/test #./A. Out
Print call frame now:
Frame 0 --./A. Out (fun3 + 0x4a) [0x80486de]
Frame 1 --./A. Out (fun2 + 0xb) [0x8048762]
Frame 2 --./A. Out (fun1 + 0xb) [0x802136f]
Frame 3 --./A. Out (main + 0x16) [0x8048787]
Frame 4 --/lib/libc. so.6 (_ libc_start_main + 0xdc) [0xb7dcb8ac]
Frame 5 --./A. out [0x8048611]
Linux-XMS:/data/test # addr2line 0x80486de-E./A. Out-F
Fun3
/Data/test. C: 14
This time we succeeded. we added the-G parameter.
For details about backtrace and backtrace_symbols, refer
Http://www.kernel.org/doc/man-pages/online/pages/man3/backtrace.3.html