ianlancetaylor / libbacktrace

A C library that may be linked into a C/C++ program to produce symbolic backtraces
Other
971 stars 228 forks source link

mismatch with glibc backtrace #117

Closed nagarajsherigar closed 9 months ago

nagarajsherigar commented 9 months ago

hi below is the sample code

#include <execinfo.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include<cstring>
#include<backtrace.h>

void bt_error_callback(void *, const char *msg, int err) {
  fprintf(stderr,"Error: %s:%d\n",msg,err);
}

int bt_callback(void *, uintptr_t pc, const char *filename, int lineno, const char *function) {

  fprintf(stderr,"File:%s|Line:%d|pc:%p|function:%s\n", filename, lineno, pc,function);

  return 0;
}

int main(int argc,char * argv[],char **)
{
  struct backtrace_state * bt_trace=backtrace_create_state(argv[0], 0, bt_error_callback, nullptr);

  fprintf(stderr,"\nLIBBACKTRACE\n");

  backtrace_full(bt_trace, 0, bt_callback, bt_error_callback, nullptr);

  fprintf(stderr,"\nBACKTRACE\n");

  void *array[256];
  int size;

  size = backtrace(array, 256);
  backtrace_symbols_fd(array, size, STDERR_FILENO);
}

compile command

gcc -g -o testlbt testlbt.cpp -lbacktrace -rdynamic

Output

LIBBACKTRACE
File:/earth/somwhere/someplace/somepath/testlbt.cpp|Line:28|pc:0x4023de|function:main
File:(null)|Line:0|pc:0x7f5f61429d8f|function:(null)
File:(null)|Line:0|pc:0x7f5f61429e3f|function:(null)
File:(null)|Line:0|pc:0x402214|function:(null)
File:(null)|Line:0|pc:0xffffffffffffffff|function:(null)

BACKTRACE
./testlbt(main+0xb5)[0x402411]
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f5f61429d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f5f61429e40]
./testlbt(_start+0x25)[0x402215]

GCC version 13.1.0 Ubuntu 22.04

glib backtrace shows the so library name as well as offset,how can i get the same in libbacktrace?

ianlancetaylor commented 9 months ago

When using backtrace_full from libbacktrace you get the debugging info. In your case there is no debugging info for the startup code. You can get information similar to glibc backtrace by calling the libbacktrace function backtrace_syminfo.

nagarajsherigar commented 9 months ago

When using backtrace_full from libbacktrace you get the debugging info. In your case there is no debugging info for the startup code. You can get information similar to glibc backtrace by calling the libbacktrace function backtrace_syminfo.

yes.i can use that function.still it will not show from which module it is picking,like ex /lib/x86_64-linux-gnu/libc.so.6

also couple more queries 1) is it safe to use backtrace_full in SIGSEGV signal handler ? 2) In backtrace_syminfo_callback what does symval and symsize mean?

ianlancetaylor commented 9 months ago

It is safe to use backtrace_full in a signal handler.

In the backtrace_syminfo_callback function symval is the address of the symbol in memory. symsize is the size of the symbol as recorded in the executable's symbol table. This is normally the size of the function or the variable.

nagarajsherigar commented 9 months ago

It is safe to use backtrace_full in a signal handler.

In the backtrace_syminfo_callback function symval is the address of the symbol in memory. symsize is the size of the symbol as recorded in the executable's symbol table. This is normally the size of the function or the variable.

backtrace_full uses mmap.mmap is not in async safe list of functions,so just wanted to understand,how it is safe?

ianlancetaylor commented 9 months ago

mmap is async-signal-safe in practice.