Http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c
There ' s backtrace (), and Backtrace_symbols ():
From the Mans page:
#include <execinfo.h>#include<stdio.h> ... void* callstack[ -]; intI, frames = BackTrace (callstack, -); Char* * STRs =Backtrace_symbols (callstack, frames); for(i =0; I < frames; ++i) {printf ("%s\n", Strs[i]); } Free(STRs); ...
One-the-one-use-this-in-a more convenient/oop-a-to-save-the-result of Backtrace_symbols () in an exception class Constr Uctor. Thus, whenever you throw this type of exception you have the stack trace. Then, the just provide a function for the printing it out. For example:
classMyException: Publicstd::exception {Char**STRs; MyException (ConstSTD::string&message) { intI, frames = BackTrace (callstack, -); STRs=Backtrace_symbols (callstack, frames); } voidPrintstacktrace () { for(i =0, I ...Try { ThrowMyException ("oops!");} Catch(MyException e) {e.printstacktrace ();}
Tip, it is best to remove the compilation optimization, or the printing may not be accurate.
Http://www.gnu.org/software/libc/manual/html_node/Backtraces.html
A backtrace is a list of the function calls this is currently active in a thread. The usual-inspect a backtrace of a program was to use an external debugger such as GDB. However, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of LO Gging or diagnostics.
The header file execinfo.h declares three functions that obtain and manipulate backtraces of the current thread.
-
-
Function:
int
backtrace
(void * * buffer , int size )
-
Preliminary: | Mt-safe | As-unsafe init heap Dlopen Plugin Lock | Ac-unsafe init mem Lock FD | See POSIX Safety Concepts.
backtrace
the function obtains a backtrace for the current thread, as a list of pointers, and places the information into . The argument should is the number of elements that would size void *
fit into buffer . The return value buffer is the actual number of entries of which is obtained size .
The pointers placed buffer in is actually return addresses obtained by inspecting the stack, one return address per stack Frame.
Note that certain compiler optimizations is interfere with obtaining a valid backtrace. function inlining causes the inlined function to not has a stack frame; Tail call optimization replaces one stack frame with another; Frame pointer elimination would stop from backtrace
interpreting the stack contents correctly.
-
-
Function:
char * *
backtrace_symbols
(void *const * buffer , int size )
-
Preliminary: | Mt-safe | As-unsafe Heap | Ac-unsafe Mem Lock | See POSIX Safety Concepts.
The backtrace_symbols
function translates the information obtained from the backtrace
function to an array of strings. The argument buffer should be a pointer to an array of addresses obtained via backtrace
the function, and is the number size O F entries in that array (the return value of backtrace
).
The return value is a pointer to an array of strings, which have size entries just like the array buffer . Each string contains a printable representation of the corresponding element of buffer . It includes the function name (if this can is determined), an offset to the function, and the actual return address (in hexadecimal).
Currently, the function name and offset only being obtained on systems that use the ELF binary format for programs and Librar ies. On other systems, only the hexadecimal return address would be present. Also, need to pass additional flags to the linker to make the function names available to the program. (for example, on systems using the GNU LD, you must Pass ( -rdynamic
.)
The return value backtrace_symbols
of is a pointer obtained via malloc
the function, and it's the responsibility of the caller to that pointer. Note that the return value need is freed, not the individual strings.
The return value is NULL
if sufficient memory for the strings cannot be obtained.
-
Function:
void
backtrace_symbols_fd
(void *const * buffer , I NT size , int fd )
-
Preliminary: | Mt-safe | As-safe | Ac-unsafe Lock | See POSIX Safety Concepts.
The backtrace_symbols_fd
function performs the same translation as the function backtrace_symbols< /code> function. Instead of returning the strings to the caller, it writes the strings to the file descriptor FD , one per line. It does not use the malloc
function, and can therefore is used in situations where the function might fail.< /p>
The following program illustrates the use of these functions. Note that the array to contain the return addresses returned was backtrace
allocated on the stack. Therefore code like this can is used in situations where the memory handling via malloc
does don't work anymore (in which CAs E The have to is replaced by a call as well backtrace_symbols
backtrace_symbols_fd
). The number of return addresses is normally not very large. Even complicated programs rather seldom has a nesting level of more than, say, and with a possible entries probably All programs should is covered.
#include <execinfo.h>#include<stdio.h>#include<stdlib.h>/*obtain a backtrace and print it to stdout.*/voidPrint_trace (void){ void*array[Ten]; size_t size; Char**strings; size_t i; Size= BackTrace (Array,Ten); Strings=backtrace_symbols (array, size); printf ("obtained%ZD stack frames.\n", size); for(i =0; i < size; i++) printf ("%s\n", Strings[i]); Free(strings);}/*A dummy function to make the backtrace more interesting.*/voidDummy_function (void) {print_trace ();}intMain (void) {dummy_function (); return 0;}
C Voice in the print parameter call level is called stack, calling Trace