
Debug Your First Segfault With gdb and a Core Dump
Turn Segmentation fault (core dumped) into a line number: compile with -g, enable core files, open the core in gdb, and read the backtrace and variables.
Tag
12 posts

Turn Segmentation fault (core dumped) into a line number: compile with -g, enable core files, open the core in gdb, and read the backtrace and variables.

A GDB-to-LLDB command map by task: running, breakpoints, stepping, variables, memory, registers, threads and settings, with the cases where the two differ.
Why GDB shows optimized out and jumps lines at -O2, how GCC -Og and variable tracking keep more values, and what to do when you must debug the real build.

Point GDB at a debuginfod server with DEBUGINFOD_URLS and it downloads matching debug symbols and source for any shared library on demand, keyed by build ID.

Attach GDB to a process that is already running with gdb -p or attach, inspect where it is stuck, then detach so it keeps running. Covers permissions.

Stop only when it matters. Set a condition with break ... if, change it with condition, skip hits with ignore, and learn what conditions cost on hot paths.

A GDB command reference grouped by task, with the short form of each command, an example and the case where that command is the wrong choice.

Use GDB's print, display, ptype and info locals to read scalars, arrays, structs and pointers, and change output with format letters, @ slices and set print.

Move through a C program one line at a time in GDB with next, step, finish, until and advance, and learn which one to use when a line calls a function.

Use GDB's break command with a function name, a line number, file:line, an address or an offset, then list, disable, delete and make breakpoints temporary.

Build a C program with gcc -g, start it under GDB, stop at main, run it to the end and quit cleanly. The smallest complete session, with every output shown.

A full guide to GDB for C and C++: building for debugging, crashes, wrong answers, hangs, corruption, threads, core files, remote targets and habits.