How to handle SIGSEGV and track trace when receiving SIGSEGV in c / C ++ program?

I am creating a C ++ program in which I need to process SIGSEGV, and the Signal handler should be able to print Back Trace. Can anyone help with this.

respectfully

+4
source share
3 answers

The best way to get SIGSEV backtrace is to generate the main file more than print backtrace. Be careful if you process SIGSEV, the system will not call the default kernel generator.

If you want to handle SIGSEV anyway (as noted before this applies to the system, see the backcrace libc function [ http://www.gnu.org/s/libc/manual/html_node/Backtraces.html ] It may be helpful.

+5
source

Adding to what John answered, you basically need a function like this to print backtrace. This function will be called on SIGSEGV. But I'm the second, I think that letting the system generate a corefile would be a much better debugging mechanism for you.

void print_trace(int nSig) { printf("print_trace: got signal %d\n", nSig); void *array[32]; /* Array to store backtrace symbols */ size_t size; /* To store the exact no of values stored */ char **strings; /* To store functions from the backtrace list in ARRAY */ size_t nCnt; size = backtrace(array, 32); strings = backtrace_symbols(array, size); /* prints each string of function names of trace*/ for (nCnt = 0; nCnt < size; nCnt++) fprintf(stderr, "%s\n", strings[nCnt]); exit(-1); } 
+3
source

Take a look at the sample code:

C ++ Code Snippet is a software package for printing a stack programmatically using function names .

You may need to expand characters, which makes sample code.

And try two more options:

  • -fno-omit-frame-pointer
  • -rdynamic

The first option saves the frame pointer in the generated code, so all inverse frames are available for the code. The second stores character information in a linked binary. This works on my build of arm9 and no need for -g.

+1
source

Source: https://habr.com/ru/post/1309747/


All Articles