How to Debug a Program with GDB

Debugging is an essential skill for any programmer, and the GNU Debugger (GDB) is a powerful tool for diagnosing and fixing issues in your code. In this comprehensive guide, we’ll explore how to use GDB effectively to debug your programs and provide tips for making the process as efficient as possible.

Table of Contents

  1. Prerequisites
  2. Compiling Your Program for Debugging
  3. Starting GDB
  4. Basic GDB Commands
  5. Setting Breakpoints
  6. Running and Controlling Execution
  7. Inspecting Variables and Memory
  8. Advanced Tips for Efficient Debugging
  9. Conclusion

Prerequisites

Before diving into GDB, ensure you have the following:

  • GDB Installed: Most Linux distributions include GDB by default. If not, install it using your package manager.
  • Source Code: A compiled program with debugging symbols.
  • Basic C/C++ Knowledge: Understanding of programming concepts and syntax.

Compiling Your Program for Debugging

To make the most of GDB, compile your program with debugging information using the -g flag.

gcc -g -o myprogram myprogram.c

This includes debugging symbols in the executable, allowing GDB to map machine code back to your source code.

Starting GDB

Launch GDB by specifying the executable:

gdb myprogram

You can also start GDB and load the program later:

gdb
(gdb) file myprogram

Basic GDB Commands

Familiarize yourself with these essential commands:

  • run (or r): Starts program execution.
  • break (or b): Sets a breakpoint at a specified line or function.
  • continue (or c): Continues execution after a breakpoint.
  • next (or n): Executes the next line of code.
  • step (or s): Steps into functions.
  • print (or p): Displays variable values.
  • quit (or q): Exits GDB.

Setting Breakpoints

Breakpoints pause program execution at specified points.

Set a Breakpoint at a Function

(gdb) break main

Set a Breakpoint at a Specific Line

(gdb) break myprogram.c:42

List Breakpoints

(gdb) info breakpoints

Running and Controlling Execution

Start Program Execution

(gdb) run

Pass command-line arguments:

(gdb) run arg1 arg2

Continue Execution After a Breakpoint

(gdb) continue

Step Through Code

To execute the next line:

(gdb) next

To step into function calls:

(gdb) step

Inspecting Variables and Memory

Print Variable Values

(gdb) print variable_name

Examine Memory Addresses

(gdb) x/address

Example:

(gdb) x/4wd &array

This command displays four words in decimal format starting from the address of array.

Watchpoints

Set a watchpoint to monitor when a variable changes:

(gdb) watch variable_name

Advanced Tips for Efficient Debugging

  • Use GDB Scripts: Automate common tasks by writing GDB command scripts.
  • Leverage TUI Mode: Enable the Text User Interface for a split-screen view. (gdb) layout src
  • Set Conditional Breakpoints: Break only when certain conditions are met. (gdb) break myfunction if (x > 10)
  • Debug Core Dumps: Analyze program crashes by loading core dump files. gdb myprogram core.dump
  • Use Reverse Debugging: Step backward in execution (if supported). (gdb) target record

Conclusion

GDB is a powerful ally in diagnosing and fixing issues within your code. You can debug programs more efficiently and effectively by mastering its commands and features. Practice regularly and incorporate these tips to enhance your debugging skills.

Remember: Efficient debugging saves time and leads to better code quality.

Stay in the Loop

Get the daily email from ScoHostings that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

You might also like...