Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use backtrace to trace function call stack and locate segment errors under Linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use backtrace to trace function call stacks and locate segment errors under Linux. The content of the article is of high quality, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of related knowledge after reading this article.

The usual way to view the runtime stack of a function 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 when the program fails in order to analyze bugs in the program (mainly for long-running programs).

Three functions are declared in the glibc header file "execinfo.h" to get the function call stack for the current thread.

int backtrace(void **buffer,int size)

This function is used to get the call stack of the current thread, and the obtained information will be stored in buffer, which is a pointer list. The size parameter specifies how many void* elements can be stored in the buffer. The return value of the function is the actual number of pointers obtained, and the maximum value does not exceed the size.

The pointer in buffer is actually the return address taken from the stack, one for each stack frame.

Note: Some compiler optimizations interfere with getting the correct call stack, inline functions have no stack frame, and removing the frame pointer causes the stack contents to not parse correctly

char ** backtrace_symbols (void *const *buffer, int size)

backtrace_symbols converts the information obtained from the backtrace function into an array of strings. Parameter buffer should be an array of pointers obtained from the backtrace function,size is the number of elements in the array (backtrace return value)

The return value is a pointer to an array of strings, the same size as buffer. Each string contains printable information relative to the corresponding element in the buffer. It includes the function name, the offset address of the function and the actual return address

Now, only programs using ELF binary format can get function names and offset addresses. In other systems, only hexadecimal return addresses can be retrieved. In addition, you may need to pass symbols to the linker to support the function name functionality (e.g., on systems using GNU ld linker, you need to pass (-rdynamic), -rdynamic can be used to tell the linker to add all symbols to the dynamic symbol table, if your linker supports-rdynamic, it is recommended to add it!)

The return value of this function is the space requested by the malloc function, so the caller must use the free function to free the pointer.

Note: If you cannot get enough space for the 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 functionality as backtrace_symbols, except that instead of returning an array of strings to the caller, it writes the result to a file with the file descriptor fd, one line per function. It does not require a call to the malloc function, so it is suitable for situations where it is possible to call the function and fail

Here's an example from glibc (slightly modified):

#include

#include

#include

/* 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 segment errors.

How to use backtrace to trace the function call stack and locate segment errors under Linux is shared here. I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report