Source: Linux Community Astrotycoon
The usual way to see the function run-time stack is to use an external debugger such as GDB (BT command), but sometimes it is useful to print out the call stack of a function in case of a program error in order to parse the program's bug (mainly for long-running program analysis).
In the glibc header file "Execinfo.h", three functions were declared to get the function call stack of the current thread.
int BackTrace (void **buffer,int size)
The function is used to get the call stack of the current thread, and the information obtained will be stored in buffer, which is a list of pointers. The parameter size is used to specify how many void* elements can be saved in buffer. function return value is the actual number of pointers obtained, maximum size
The pointer in buffer is actually the return address obtained from the stack, and each stack frame has a return address
Note: Some compiler tuning options interfere with getting the correct call stack, and inline functions do not have a stack frame; Deleting a frame pointer also causes the stack contents to be parsed incorrectly
char * * BACKTRACE_SYMBOLS (void *const *buffer, int size)
Backtrace_symbols converts the information obtained from the BackTrace function into an array of strings. The parameter buffer should be an array of pointers obtained from the BackTrace function, where size is the number of elements in the array (the return value of BackTrace)
The function return value is a pointer to an array of strings that is the same size as buffer. Each string contains a printable information relative to the corresponding element in buffer. It includes the function name, the offset address of the function, and the actual return address
Only programs that use the ELF binary format can now get the function name and offset address. In other systems, only 16 of the binary return address can be obtained. In addition, you may need to pass the corresponding symbol to the linker in order to support function name functions (for example, in a system that uses the GNU LD Linker, you need to pass (- rdynamic),-rdynamic can be used to inform the linker to add all the symbols to the dynamic symbol table, and if your linker supports-rdynamic, it is recommended that it be added! )
The return value of the function is the space requested by the malloc function, so the caller must use the free function to release the pointer.
Note: If you cannot get enough 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, but instead of returning a string array to the caller, it writes the result to a file with the file descriptor fd, one row for each function. It does not need to call the malloc function , so it is suitable for situations where the function may fail to be called
The following is an example in glibc (slightly modified):
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
/* Obtain a backtrace and print it to @code {stdout}. */
void Print_trace (void)
{
void *array[10];
size_t size;
Char **strings;
size_t i;
Size = BackTrace (array, 10);
strings = Backtrace_symbols (array, size);
if (NULL = = strings)
{
Perror ("Backtrace_synbols");
Exit (exit_failure);
}
printf ("obtained%ZD stack frames.\n", size);
for (i = 0; i < size; i++)
printf ("%s\n", Strings[i]);
Free (strings);
strings = NULL;
}
/* A dummy function to make the backtrace more interesting. */
void Dummy_function (void)
{
Print_trace ();
}
int main (int argc, char *argv[])
{
Dummy_function ();
return 0;
}
The output is as follows:
Obtained 4 stack frames.
./execinfo () [0X80484DD]
./execinfo () [0x8048549]
./execinfo () [0x8048556]
/lib/i386-linux-gnu/libc.so.6 (__LIBC_START_MAIN+0XF3) [0x70a113]
We can also use this backtrace to locate the wrong position of the segment.
Using BackTrace to track function call stacks and locating segment errors under Linux [go]