Stack trace
From Free net encyclopedia
A stack trace (also called backtrace) is a report of the active stack frames instantiated by the execution of a program.
Stack traces are often generated when a program terminates abnormally. The last few stack frames often indicate where the bug that caused the abnormal termination occurs.
All debuggers can produce stack traces. gdb prints a stack trace with the bt (or where) command.
For example, this intentionally ill-written C program will segfault (i.e., crash) in the function function_2:
#include <stdio.h>
int main(void) { int x; printf("This program will demonstrate gdb\n"); x=function_1(); printf("%d", x); return 0; }
int function_1(void) { int x = function_2(24); return x; }
int function_2(int x) { int *y = (int *)x; return *y; }
To get an informative stack trace from a debugger, one has to compile the program with debugging information. With gcc, that is done by compiling the program with the -g option. If one then attempts to run the program in gdb, and obtain a backtrace, one would get
#0 0x080483cb in function_2 () #1 0x080483b4 in function_1 () #2 0x08048385 in main () #3 0x4003ddc6 in __libc_start_main () from /lib/libc.so.6
This shows that the function __libc_start_main called main, which in turn called function_1 and then function_2, whose stack frame is at the top of the stack, and it is indeed this function which is in error, the statement:
int *y = (int *)x;
attempts to create a pointer pointing to a nonsensical memory location at the decimal address 24, which is usually inaccessible by programs running normally.