How to debug using gdb?

Learn how to debug using gdb? with practical examples, diagrams, and best practices. Covers c, gdb development techniques with visual explanations.

Mastering GDB: A Comprehensive Guide to Debugging C Programs

Hero image for How to debug using gdb?

Unlock the power of GDB, the GNU Debugger, to efficiently find and fix bugs in your C programs. This guide covers essential commands, advanced techniques, and best practices for effective debugging.

Debugging is an indispensable skill for any C programmer. When your program doesn't behave as expected, a debugger like GDB (GNU Debugger) becomes your best friend. GDB allows you to see what's happening 'inside' your program while it executes or even after it crashes. This article will walk you through the fundamentals of using GDB, from basic setup to advanced features, helping you diagnose and resolve issues with confidence.

Getting Started with GDB: Compilation and Basic Commands

Before you can debug a C program with GDB, you need to compile it with debugging information. This is typically done by adding the -g flag to your GCC compilation command. Without this flag, GDB will have limited information about your source code, variables, and functions, making debugging much harder.

gcc -g -o myprogram myprogram.c

Compiling a C program with debugging information using GCC.

Once compiled, you can start GDB and load your executable. The following commands are fundamental for navigating your program's execution:

gdb ./myprogram
(gdb) run
(gdb) break main
(gdb) next
(gdb) step
(gdb) print variable_name
(gdb) list
(gdb) continue
(gdb) quit

Basic GDB commands for starting, running, and inspecting a program.

Understanding Program Flow with Breakpoints and Stepping

Breakpoints are crucial for pausing program execution at specific points of interest. Once execution is paused, you can examine the program's state, step through code line by line, or continue execution. GDB offers various ways to set and manage breakpoints.

flowchart TD
    A[Start GDB] --> B{Set Breakpoint at main()};
    B --> C[Run Program];
    C --> D{Breakpoint Hit?};
    D -- Yes --> E[Inspect Variables];
    E --> F{Step (next/step) or Continue?};
    F -- Step --> E;
    F -- Continue --> D;
    D -- No --> G[Program Ends];

Typical GDB debugging workflow using breakpoints and stepping.

(gdb) break my_function
(gdb) break my_file.c:100
(gdb) break 15 if (i == 5)
(gdb) info breakpoints
(gdb) delete 1
(gdb) disable 2

Setting conditional breakpoints and managing them in GDB.

Stepping commands allow you to control the execution flow precisely:

  • next (or n): Executes the next line of code, stepping over function calls.
  • step (or s): Executes the next line of code, stepping into function calls.
  • finish: Continues execution until the current function returns.
  • until (or u): Continues execution until a specified line number or the current loop finishes.

Inspecting Data and Memory

A key aspect of debugging is examining the values of variables and memory contents. GDB provides powerful commands for this, allowing you to understand the state of your program at any given moment.

(gdb) print my_variable
(gdb) print *my_pointer
(gdb) print array[index]
(gdb) display another_variable
(gdb) x/10i $pc
(gdb) x/16xb 0x7fffffffe000

Commands for printing variables, displaying expressions, and examining memory.

The print command evaluates and displays the value of an expression. display is similar but automatically prints the expression's value every time the program stops. The x command (examine) is used to inspect raw memory, with various format specifiers (e.g., x/10i for 10 instructions, x/16xb for 16 bytes in hex).

1. Compile with Debugging Symbols

Ensure your C code is compiled with the -g flag: gcc -g -o my_app my_app.c.

2. Start GDB

Launch GDB with your executable: gdb ./my_app.

3. Set Breakpoints

Use break function_name or break file.c:line_number to pause execution at critical points.

4. Run the Program

Type run to start your program. It will execute until it hits a breakpoint or finishes.

5. Inspect and Step

When paused, use print variable, next, step, and list to examine state and control execution.

6. Continue or Quit

Use continue to resume execution or quit to exit GDB.