Use backtrace and backtrace_symbols to print the call relationship of the Function

Source: Internet
Author: User
Tags xms

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.