Finding the crash location in source code

The RIP (Instruction Pointer; EIP on IA-32, PC on the ARM) is useful: using its value and some utilities, we can pretty much pinpoint the location in code when the process crashed. How? There are several ways; some of them are as follows:

  • Use the toolchain utility objdump (with the -d -S switches)
  • An easier way is to use gdb(1) (see the following)
  • With the addr2line(1) utility

With GDB:

Load up gdb(1) with the debug version (compiled with the -g switch) of the program, and then use the list command as shown here:

$ gdb -q ./handle_segv_dbgReading symbols from ./handle_segv_dbg...done.(gdb) list *0x0000000000400ffc<< 0x0000000000400ffc is the RIP value >>0x400ffc is in main (handle_segv.c:212).

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.