GDB Cheat Sheet: How Mastering Debugging with Ease

Welcome to the ultimate guide on GDB, the GNU Debugger. Whether you're a seasoned developer or just starting out, mastering GDB can significantly enhance your debugging skills. Debugging is an essential part of software development, and GDB is one of the most powerful tools available for this purpose. This article will walk you through everything you need to know about GDB, from basic commands to advanced debugging techniques.

Introduction

  • Installing GDB
  • Basic GDB commands

Setting and Managing Breakpoints

  • Setting a Breakpoint
    • Basic syntax
    • Conditional breakpoints
  • Listing and Removing Breakpoints
    • List breakpoints in GDB
    • Remove breakpoints in GDB
  • Inspecting Variables
  • GDB Inspect Variable
    • Printing variables
    • Showing variables
  • Advanced Variable Inspection
    • Print local variables
    • Print memory in hex

Navigating Through Code

  • Listing Source Code
    • GDB list command
    • Show source code
  • Stepping Through Code
    • Step into a function
    • Step over and step out
  • Managing Program Execution
  • Running and Passing Arguments
    • GDB run with arguments
    • Managing program execution
  • Using Watchpoints
    • Setting watchpoints
    • Using watch gdb for variable changes

Inspecting Memory and Registers

  • Memory Inspection
    • GDB examine memory
    • Print memory and dump memory
  • Register Inspection
    • Display registers
    • Print and dump registers

Stack and Thread Management

  • Viewing Stack
    • Show stack and call stack
    • Print stack
  • Thread Management
    • List threads
    • Thread-specific commands

Advanced GDB Features

  • Conditional Breakpoints and Ignore Signals
    • Setting conditional breakpoints
    • Managing signals
  • Using GDB Scripts and Automation
    • Writing GDB scripts
    • Automating debugging tasks

FAQs

  • How to set a breakpoint in GDB?
  • How to remove a breakpoint in GDB?
  • How to inspect variables in GDB?
  • How to pass arguments to a program in GDB?
  • How to view the stack in GDB?

Installing GDB

Before you can start debugging, you need to have GDB installed on your system. If you're on a Unix-based system like Linux or macOS, GDB is often available through your package manager. For instance, you can use sudo apt-get install gdb on Debian-based systems or brew install gdb on macOS. On Windows, you might need to install it via MinGW or Cygwin.

Basic GDB Commands

Here are some essential GDB commands to get you started:

  • gdb <program>: Start GDB with the specified program.
  • run: Run the program inside GDB.
  • break <location>: Set a breakpoint at the specified location.
  • next or n: Step over to the next line of code.
  • continue or c: Continue execution until the next breakpoint.

Setting and Managing Breakpoints

Setting a Breakpoint

Breakpoints are crucial in debugging as they allow you to pause program execution at specific points.

Basic syntax:

break <location>

For example, break main sets a breakpoint at the beginning of the main function.

Conditional Breakpoints

Sometimes, you need to pause execution only when certain conditions are met. This is where conditional breakpoints come in handy.

break <location> if <condition>

For example, break foo.c:42 if x == 10 will break at line 42 of foo.c if x equals 10.

Listing and Removing Breakpoints

List Breakpoints in GDB

To list all breakpoints:

info breakpoints
Remove Breakpoints in GDB

To remove a breakpoint, you need its number, which you can get from the info breakpoints command.

delete <breakpoint-number>

For instance, delete 1 removes breakpoint number 1.

Inspecting Variables

GDB Inspect Variable

Inspecting variables helps you understand the state of your program at various points.

Printing Variables
print <variable>

For example, print x will print the value of x.

Showing Variables
show <variable>

This command displays information about a variable or a setting.

Advanced Variable Inspection

Print Local Variables

To print all local variables:

info locals
Print Memory in Hex

You can examine memory in different formats, including hexadecimal.

x/x <address>

For example, x/x &x prints the memory address of x in hex.

Navigating Through Code

Listing Source Code

GDB List Command

The list command shows you the source code around the current line.

list

You can also specify a line number, function, or filename.

Show Source Code

To show the entire source file:

list <filename>

Stepping Through Code

Step Into a Function
step

This command steps into a function call.

Step Over and Step Out
next

Steps over the function calls.

finish

Steps out of the current function.

Managing Program Execution

Running and Passing Arguments

You can run the program with arguments by specifying them after the run command.

run arg1 arg2
Managing Program Execution

To restart the program without exiting GDB:

run

Using Watchpoints

Watchpoints are like breakpoints but for variables. They pause execution when a variable changes.

Setting Watchpoints
watch <variable>

For example, watch x will pause execution when x changes.

Using Watch GDB for Variable Changes

You can use the watch command to monitor changes in specific variables effectively.

Inspecting Memory and Registers

Memory Inspection

GDB Examine Memory

The x command allows you to examine memory.

x/<format> <address>

Formats include x for hex, d for decimal, s for string, etc.

Print Memory and Dump Memory

You can print memory in various formats and even dump it to a file.

dump memory <filename> <start> <end>

For example, dump memory mem.bin 0x600000 0x601000 dumps memory between the specified addresses to mem.bin.

Register Inspection

Display Registers

To display the contents of all registers:

info registers
Print and Dump Registers

To print a specific register:

print $<register>

For example, print $eax prints the eax register.

To dump all registers to a file:

dump registers <filename>

Stack and Thread Management

Viewing Stack

Show Stack and Call Stack

To display the call stack:

backtrace
Print Stack

To print the stack frame information:

info frame

Thread Management

List Threads

To list all threads:

info threads
Thread-Specific Commands

To switch to a specific thread:

thread <thread-number>

Advanced GDB Features

Conditional Breakpoints and Ignore Signals

Setting Conditional Breakpoints

You can set breakpoints that only trigger when a specific condition is true.

break <location> if <condition>
Managing Signals

To ignore a specific signal:

handle <signal> ignore

For example, handle SIGINT ignore will ignore the SIGINT signal.

Using GDB Scripts and Automation

Writing GDB Scripts

You can automate tasks by writing GDB scripts.

source <script-file>
Automating Debugging Tasks

You can put commonly used commands in a script file and source it in GDB.

echo "set logging on\nbreak main\nrun\n" > myscript.gdb
gdb -x myscript.gdb <program>

FAQs

How to set a breakpoint in GDB?
To set a breakpoint, use the break <location> command. For example, break main sets a breakpoint at the start of the main function.

How to remove a breakpoint in GDB?
To remove a breakpoint, first list them with info breakpoints and then use delete <breakpoint-number> to remove a specific breakpoint.

How to inspect variables in GDB?
Use the print <variable> command to inspect variables. For example, print x prints the value of x.

How to pass arguments to a program in GDB?
You can pass arguments by specifying them after the run command, like run arg1 arg2.

How to view the stack in GDB?
Use the backtrace command to view the call stack.

Conclusion

Mastering GDB can significantly enhance your debugging capabilities, making it easier to find and fix bugs in your code. From setting breakpoints to inspecting memory and registers, GDB offers a comprehensive set of tools for debugging. By using the commands and techniques outlined in this guide, you'll be well on your way to becoming a GDB power user.

@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