Obtain the backtrace (function call stack) through programming (subsequent core dump debugging explanation)

Source: Internet
Author: User
Tags gdb debugger
When you use the gdb debugger, you can view the so-called backtrace, which contains a series of function call Information. You can use the command backtrace or BT to view the information of the function call stack in GDB. In some cases, you can use some related functions in the glibc library function to obtain backtrace information (in the header file execinfo. h ):
// Obtain and save the backstrace information to the buffer.
// The parameter size specifies the maximum value of the buffer. The returned value is the actual size of backstrace.
Int backtrace (void ** buffer, int size)

// Return the symbolic information based on the address specified by the buffer. Parameter size specifies the size of the returned Symbol Information
Char ** backtrace_symbols (void * const * buffer, int size)

// Similar to the backtrace_symbols () function, but the malloc space is not required to store the symbol information,
// Write the result to the file represented by the file descriptor FD.
Void backtrace_symbols_fd (void * const * buffer, int size, int FD)

When using the backtrace_symbols () or backtrace_symbols_fd () function, you must use-rdynamic compilation to get the correct symbol name. Otherwise, you can only get the offset address.
The following exampleCodeBacktrace () and backtrace_symbols () functions are used to print backtrace information:

1 # include <execinfo. h>
2 # include <stdio. h>
3 # include <stdlib. h>
4
5/* obtain a backtrace and print it to stdout .*/
6 void print_trace (void)
7 {
8 void * array [10];
9 size_t size;
10 char ** strings;
11 size_t I;
12
13 size = backtrace (array, 10 );
14 strings = backtrace_symbols (array, size );
15
16 printf ("obtained % ZD stack frames. \ n", size );
17
18 For (I = 0; I <size; I ++)
19 printf ("% s \ n", strings [I]);
20
21 free (strings );
22}
23
24/* a dummy function to make the backtrace more interesting .*/
25 void dummy_function (void)
26 {
27 print_trace ();
28}
29
30 int main (void)
31 {
32 dummy_function ();
33 return 0;
34}

The compilation and running results are as follows:

# GCC Bt. C-rdynamic-O BT
#./BT
Obtained 5 stack frames.
./Bt (print_trace + 0x14) [0x80486e4]
./Bt (dummy_function + 0xb) [0x8048765]
./Bt (main + 0x15) [0x804877c]
/Lib/tls/libc. so.6 (_ libc_start_main + 0xe4) [0x42015574]
./Bt (backtrace_symbols + 0x31) [0x8048641]

You can apply these functions inProgramPrint backtrace when an exception exits or save it to a file for later analysis.

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.