Programming

How to Use GDB's Source-Tracking Breakpoints for Effortless Debugging After Code Edits

2026-05-02 19:50:06

Introduction

Imagine you're deep in a debugging session, with several breakpoints strategically placed across your source files. You spot a bug, make a quick edit in your editor, recompile, and reload the binary in GDB—only to find that your carefully set breakpoints now point to the wrong lines, thanks to added or removed code above them. The typical fix is to disable old breakpoints and set new ones manually, breaking your concentration. GDB's experimental source-tracking breakpoints eliminate this friction. When enabled, GDB captures a snapshot of source lines around each breakpoint you set using file:line notation. After you recompile and reload, GDB matches those lines to the new code and automatically adjusts the breakpoint location. This guide walks you through enabling, using, and understanding the limits of this feature, so you can keep your debugging flow intact.

How to Use GDB's Source-Tracking Breakpoints for Effortless Debugging After Code Edits
Source: fedoramagazine.org

What You Need

Step-by-Step Instructions

Step 1: Enable Source-Tracking Mode

Start GDB with your program (gdb ./myprogram). Before setting any breakpoints, turn on the experimental source-tracking feature:

(gdb) set breakpoint source-tracking enabled on

This instructs GDB to capture source context for every subsequent breakpoint created with the file:line syntax. The setting persists until you disable it with off or exit GDB.

Step 2: Set a Breakpoint Using file:line

Choose a source file and line number you want to stop at. For example, if your file is calc.c and you want to break at line 42:

(gdb) break calc.c:42
Breakpoint 1 at 0x401234: file calc.c, line 42.

GDB now stores a small window of source lines surrounding line 42 (by default, 3 lines).

Step 3: Verify the Breakpoint Is Tracked

Use the info breakpoints command to confirm that source-tracking is active for this breakpoint:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401234  in calculate at calc.c:42
        source-tracking enabled (tracking 3 lines around line 42)

The extra line indicates that GDB will attempt to adjust this breakpoint after a recompile.

Step 4: Edit Your Source Code

Open calc.c in your editor and insert a few lines above line 42. For instance, add a new variable declaration or a comment. This shifts the original breakpoint line downward—say from line 42 to line 45. Save the file.

Step 5: Recompile and Reload the Program

Recompile your project (e.g., make or gcc -g -o myprogram calc.c). Back in GDB, issue the run command to reload the new executable:

(gdb) run

GDB automatically scans the tracked source lines and compares them to the current code. If the captured lines match a new location, the breakpoint is moved. You'll see output like:

How to Use GDB's Source-Tracking Breakpoints for Effortless Debugging After Code Edits
Source: fedoramagazine.org
Breakpoint 1 adjusted from line 42 to line 45.

Step 6: Confirm the New Location

Check info breakpoints again to see the updated line:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401256  in calculate at calc.c:45
        source-tracking enabled (tracking 3 lines around line 45)

The breakpoint now points to the correct line. Your debugging session continues without having to manually re‑set anything.

Tips and Best Practices

Explore

When Software Relies on Undocumented Behavior: The Tale of Restartable Sequences and TCMalloc 10 Strategies to Build Financial Products That Truly Stick Python 3.14.3 and 3.13.12 Arrive: Major Bug Fixes and New Features Unveiled Kubernetes v1.36 Enhances Pod Resource Management with Beta In-Place Vertical Scaling 8 Key Insights into Python 3.15.0 Alpha 6: What Developers Need to Know