Programming

How to Use GDB's Source-Tracking Breakpoints to Avoid Manual Resets

2026-05-02 22:02:18

Introduction

Imagine you're deep in a debugging session. You've set breakpoints on a handful of lines, inspected variables, and formed a theory about what's wrong. You edit the source code, recompile, and reload the executable—but now your breakpoints point to the wrong lines because the code shifted. Without source-tracking, you'd have to delete each old breakpoint and set new ones manually. That's tedious and interrupts your flow.

How to Use GDB's Source-Tracking Breakpoints to Avoid Manual Resets
Source: fedoramagazine.org

GDB's experimental source-tracking breakpoints solve this problem. When you enable this feature and set a breakpoint using file:line notation, GDB captures a small window of surrounding source code. After you edit, recompile, and reload, GDB automatically adjusts any breakpoints whose line numbers changed. This keeps your debugging session moving without the hassle of resetting breakpoints after every edit-compile cycle.

What You Need

Step-by-Step Instructions

Step 1: Start GDB and Load Your Program

Launch GDB and load the executable you want to debug. Use the file command or pass the executable as an argument when starting GDB. For example:

gdb ./myprogram

Now you're inside the GDB prompt ((gdb)).

Step 2: Enable Source-Tracking

Before setting breakpoints, turn on the source-tracking feature with the following command:

(gdb) set breakpoint source-tracking enabled on

This tells GDB to capture source context for any breakpoint set from now on. If you want to disable it later, use the same command with off.

Step 3: Set a Breakpoint Using File:Line Notation

Choose a source line where you want to stop execution. Use the break (or b) command with the file name and line number. For example, to set a breakpoint on line 42 of myfile.c:

(gdb) break myfile.c:42

GDB will respond with something like:

Breakpoint 1 at 0x401234: file myfile.c, line 42.

Because source-tracking is enabled, GDB automatically captures a small window (by default, 3 lines around the breakpoint). You can verify this with the info breakpoints command:

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

Note the source-tracking enabled line—it confirms that GDB is tracking this breakpoint.

Step 4: Edit Your Source Code

Now, with GDB still running, edit myfile.c in another window or terminal. Make changes that will shift the line numbers—for example, add a few lines above line 42. Save the file.

Common edits that cause line shifts:

Step 5: Recompile the Program

Exit the editor and recompile your source file. For example:

gcc -g -o myprogram myfile.c

Make sure the new executable overwrites the old one. You can do this while GDB is still running; you don't need to restart GDB.

Step 6: Reload the New Executable in GDB

Back in the GDB prompt, use the run command (or start) to reload the executable:

(gdb) run

GDB will notice that the executable has been changed and will reload it automatically. As part of that reload, it applies the source-tracking algorithm: it attempts to match the captured source lines (the 3-line window) to the new source file. If a match is found, GDB adjusts the breakpoint to the new line number. You'll see a message like:

How to Use GDB's Source-Tracking Breakpoints to Avoid Manual Resets
Source: fedoramagazine.org
Breakpoint 1 adjusted from line 42 to line 45.

That means the breakpoint now lives at line 45 because you inserted three lines above it.

Step 7: Verify the New Breakpoint Location

Run info breakpoints again to confirm the update:

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

The address and line number have changed to reflect the new location. You can now continue debugging as usual—the breakpoint will trigger at the intended source line.

Step 8: Continue Debugging (Optional Repetition)

You can repeat steps 4–7 as many times as needed. Each edit-compile-reload cycle will automatically adjust all source-tracked breakpoints. If you add more breakpoints later, they will also be tracked as long as source-tracking remains enabled.

Tips and Limitations

Source-tracking breakpoints can save you significant time during iterative debugging. By following these steps, you can stay focused on finding bugs rather than managing breakpoints.

Explore

Japan's Big Four Motorcycle Makers Charge into an Electric Future 10 Key Insights About Ubuntu Pro Integration in Security Center Reducing Methane Emissions from Rice Cultivation: A Practical Guide for Farmers and Researchers Fedora KDE Plasma Desktop 44 Launches with Plasma 6.6 and Major Usability Upgrades CachyOS Linux Surges Ahead in Performance Benchmarks Against Ubuntu 26.04 and Fedora 44