Use the backtrace and backtrace_symbols functions to print the call stack information

Source: Internet
Author: User

In the header file "execinfo. h", the three functions are declared to obtain the function call stack of the current thread.

# Include <execinfo. h>

 Int backtrace (void **Buffer, IntSize);

 Char ** backtrace_symbols (void * const *Buffer, IntSize);

 Void backtrace_symbols_fd (void * const *Buffer, IntSize, IntFd);

Man help:

DESCRIPTION
       backtrace() returns a backtrace for the calling program, in the array       pointed to by buffer.  A backtrace is the series of currently active       function calls for the program.  Each item in the array pointed to by       buffer is of type void *, and is the return address from the       corresponding stack frame.  The size argument specifies the maximum       number of addresses that can be stored in buffer.  If the backtrace       is larger than size, then the addresses corresponding to the size       most recent function calls are returned; to obtain the complete       backtrace, make sure that buffer and size are large enough.       Given the set of addresses returned by backtrace() in buffer,       backtrace_symbols() translates the addresses into an array of strings       that describe the addresses symbolically.  The size argument       specifies the number of addresses in buffer.  The symbolic       representation of each address consists of the function name (if this       can be determined), a hexadecimal offset into the function, and the       actual return address (in hexadecimal).  The address of the array of       string pointers is returned as the function result of       backtrace_symbols().  This array is malloc(3)ed by       backtrace_symbols(), and must be freed by the caller.  (The strings       pointed to by the array of pointers need not and should not be       freed.)       backtrace_symbols_fd() takes the same buffer and size arguments as       backtrace_symbols(), but instead of returning an array of strings to       the caller, it writes the strings, one per line, to the file       descriptor fd.  backtrace_symbols_fd() does not call malloc(3), and       so can be employed in situations where the latter function might       fail.

Int backtrace (void ** buffer, int size)
This function is used to obtain the call stack of the current thread. The obtained information is stored in the buffer. It is a pointer array. The size parameter is used to specify how many void * elements can be stored in the buffer. The return value of a function is the number of actually obtained pointers, and the maximum size of the pointer in the buffer is actually the return address obtained from the stack. Each stack framework has a return address.
Note that some Compiler Optimization Options interfere with obtaining the correct call stack. In addition, inline functions do not have a stack framework. Deleting a framework pointer will make it impossible to parse the stack content correctly.
Char ** backtrace_symbols (void * const * buffer, int size)
Backtrace_symbols converts the information obtained from the backtrace function into a string array. the buffer parameter should be an array pointer obtained from the backtrace function. size is the number of elements in the array (the return value of backtrace). The return value of the function is a pointer to a string array, its size is the same as that of buffer. each string contains a printable information relative to the corresponding elements in the buffer. it includes the function name, function offset address, and actual return address.
Currently, the function name and offset address can be obtained only by using the ELF binary format program and the difficulty. in other systems, only the return address in hexadecimal format can be obtained. in addition, you may need to pass the corresponding flag to the linker to support the function name function (for example, in a GNU ld system, you need to pass (-rdynamic ))
The strings generated by backtrace_symbols are all malloc, but do not free them one by one. Because backtrace_symbols stores the result strings in one memory based on the call stack layers given by backtrace, so, like the code above, you only need to get the free backtrace_symbols return pointer at the end. This is also mentioned in manual of backtrace.
Note: if you cannot obtain sufficient space for a string, the return value of the function will be NULL.
Void backtrace_symbols_fd (void * const * buffer, int size, int fd)
Backtrace_symbols_fd has the same function as the backtrace_symbols function. The difference is that it does not return a string array to the caller, but writes the result to the file with the file descriptor fd. Each function corresponds to a row. it does not need to call the malloc function, so it is applicable to situations where the function may fail to be called.
Example in man's Manual:
#include <execinfo.h><stdio.h><stdlib.h><unistd.h> SIZE 100    *buffer[ **== (strings == (j = ; j < nptrs; j++     (ncalls > -  argc,  * (argc != , argv[
Result:

Conclusion: The following functions can be used to print stack information.
Int backtrace (void ** buffer, int size)
Char ** backtrace_symbols (void * const * buffer, int size)

Char * abi ::__ cxa_demangle
(
Const char *Mangled_name,

Char *Output_buffer,

Size_t *Length,

Int *Status

)

1. backtrace can be called anywhere the program is running. It returns the return address of each called function and can limit the maximum number of returned layers of the call stack.

2. After backtrace obtains the return address of the function, backtrace_symbols can convert it into compilation symbols, which are determined during compilation.

3. According to the compilation symbol returned by backtrace_symbols, abi: :__ cxa_demangle can find the specific function method.

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.