GDB: How Mastering Debugging using GDB Cheat Sheet

Debugging is a crucial aspect of software development, and having the right tools can make a significant difference in your workflow. The GNU Debugger (GDB) is one of the most powerful and widely used tools for debugging C programs. It allows developers to see what is happening inside their programs while they run or to understand what went wrong when they crash. This article serves as a comprehensive cheat sheet for efficiently using GDB, covering essential commands, basic and advanced examples to help you navigate and troubleshoot your code effectively.

Why Use GDB?

The GNU Debugger (GDB) is a powerful debugging tool used by developers to analyze and debug their C programs. It allows you to see what is happening inside a program while it runs or what it was doing at the moment it crashed. Efficient use of GDB can significantly speed up the debugging process, improve code quality, and help you understand complex codebases.

GDB Commands

Here are some of the most commonly used GDB commands that can help you navigate through your debugging sessions:

gdb program: Start GDB with the specified program.
break function/line: Set a breakpoint at a function or line.
run [args]: Start the program with optional arguments.
bt: Display the call stack (backtrace).
frame n: Select and print stack frame number n.
next (or n): Step over to the next line of code.
step (or s): Step into a function.
continue (or c): Continue running the program until the next breakpoint.
print expr (or p): Print the value of an expression.
info breakpoints: List all breakpoints.
delete [breakpoints]: Delete specified breakpoints.
watch expr: Set a watchpoint for an expression.
info locals: List local variables and their values.
quit (or q): Exit GDB.

Basic Example

Let's walk through a basic example of using GDB to debug a simple C program.

#include <stdio.h>

void greet() {
    printf("Hello, World!\n");
}

int main() {
    greet();
    return 0;
}

Steps to Debug:

  • Compile with Debug Information:
gcc -g -o hello hello.c

The -g flag includes debugging information in the executable.

  • Start GDB:
gdb hello
  • Set a Breakpoint:
(gdb) break main
Breakpoint 1 at 0x400526: file hello.c, line 8.
  • Run the Program:
(gdb) run
Starting program: /path/to/hello 
Breakpoint 1, main () at hello.c:8
8	    greet();
  • Step Over:
(gdb) next
9	    return 0;
  • Continue Execution:
(gdb) continue
Continuing.
Hello, World!
[Inferior 1 (process 12345) exited normally]
  • Print Variable:
(gdb) print greet
$1 = {void (void)} 0x400516 <greet>

Advanced Example

Let's take a more complex example that includes function calls and data structures.

Sample Program

#include <stdio.h>

typedef struct {
    int x, y;
} Point;

void print_point(Point p) {
    printf("Point(%d, %d)\n", p.x, p.y);
}

Point create_point(int x, int y) {
    Point p = {x, y};
    return p;
}

int main() {
    Point p = create_point(3, 4);
    print_point(p);
    return 0;
}

Steps to Debug

  • Compile with Debug Information:
gcc -g -o point point.c
  • Start GDB:
gdb point
  • Set Breakpoints:
(gdb) break create_point
Breakpoint 1 at 0x400537: file point.c, line 12.
(gdb) break print_point
Breakpoint 2 at 0x400526: file point.c, line 7.
  • Run the Program:
(gdb) run
Starting program: /path/to/point 
Breakpoint 1, create_point (x=3, y=4) at point.c:12
12	    Point p = {x, y};
  • Step Through the Function:
(gdb) next
13	    return p;
(gdb) next
14	}
(gdb) continue
Continuing.
Breakpoint 2, print_point (p=...) at point.c:7
7	    printf("Point(%d, %d)\n", p.x, p.y);
  • Inspect Variables:
(gdb) print p
$1 = {x = 3, y = 4}
  • Continue Execution:
(gdb) continue
Continuing.
Point(3, 4)
[Inferior 1 (process 12345) exited normally]

Conclusion

GDB is an invaluable tool for debugging C programs. By mastering its commands and learning how to use them efficiently, you can greatly improve your debugging skills and code quality. Use this cheat sheet as a reference during your debugging sessions to help navigate through common tasks and make the most out of GDB's powerful features.

@freecoder
@freecoder

With 15+ years in low-level development, I'm passionate about crafting clean, maintainable code.
I believe in readable coding, rigorous testing, and concise solutions.

Articles: 29