Implementing Tracing on i386

Currently, I am transferring our code from alpha (Tru64) to the i386 processor (Linux) to C. Everything went pretty smoothly until I looked into porting our exception handling routine. We currently have a parent process that spawns many subprocesses, and when one of these subprocesses is fatal (unfielded) I have subroutines to catch the process.

Currently, I am struggling to find the best way to implement a trace procedure that can list the addresses of functions in the error log; currently, my program simply prints the signal that caused the exception and the exception classifier code.

Any help would be received at best, ideally I would write error handling for all processors, however at this stage I really like i386 and x86_64.

thanks

Mark

+4
source share
6 answers

The functions glibc backtrace() and backtrace_symbols() , from execinfo.h , can be useful.

+2
source

You can look at http://tlug.up.ac.za/wiki/index.php/Obtaining_a_stack_trace_in_C_upon_SIGSEGV . It covers the necessary functionality. However, you must establish a connection with libgdb and libdl, compile with -rdynamic (including more characters in the executable), and stop using some optimizations.

+2
source

There are two GNU functions (not POSIX) that can help you - backtrace() and backtrace_symbols() - first returns an array of function addresses and the second resolves addresses to names. Unfortunately, the names of static functions cannot be resolved.

To make it work, you need to compile your binary with the -rdynamic flag.

+1
source

Unfortunately, there is no โ€œbetterโ€ method, since the stack layout may vary depending on the processor, OS, and compiler used to compile your code. But this article may help .

Note that you must implement this in the child process; the parent process simply receives a signal that something is wrong; you are not getting a copy of the child stack.

0
source

If a comment, you declare that you are using gcc. This http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Return-Address.html#Return-Address may be useful.

0
source

If you're fine when you do the correct backtrace when you run valgrind, this might be your option:

VALGRIND_PRINTF_BACKTRACE (format, ...):

It will provide you with backtracking for all functions, including static ones.

0
source

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


All Articles