How do you inspect a core dump with gdb after a segfault?
C++Level: IntermediateDebugging & Tools
How do you inspect a core dump with gdb after a segfault?
Hints
- Identify types, object lifetimes and compile-time decisions.
- Separate language guarantees from a particular compiler's behavior.
Answer
Debugging Crashes
When a program crashes with a segmentation fault, GDB helps identify the cause:
$ gdb ./myprogram
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555149 in func () at main.c:10
10 *ptr = 42;
(gdb) bt
#0 0x0000555555555149 in func () at main.c:10
#1 0x0000555555555180 in main () at main.c:20
(gdb) print ptr
$1 = (int *) 0x0
Debug a core dump after a crash:
$ ulimit -c unlimited # enable core dumps
$ ./myprogram # run and crash
$ gdb ./myprogram core # analyze core dump
(gdb) bt # see where it crashed
Source and licence: C/C++ Interview Cheatsheet — Chang Ning Tsai · MIT