Tracing function call stack in linux

Source: Internet
Author: User
Tracking function call stacks in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Generally, you can view the function runtime stack by using an external debugger such as GDB. However, in some cases, to analyze program bugs (mainly for analysis of long-running programs ), it is very useful to print the function call stack when a program error occurs.

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

Function: 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 list. The size parameter is used to specify how many void * elements can be stored in the buffer. The Return Value of the function is the number of actually obtained pointers. The maximum value is not greater than the size.

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.

Function: 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, and the 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, which is the same size as the 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 return value of this function is the space applied through the malloc function. Therefore, you must use the free function to release the pointer to call this function.

Note: if you cannot obtain sufficient space for a string, the return value of the function will be NULL.

Function: 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.

The following example shows the usage of the three functions.
QUOTE:
Related Article

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.