Ehedrick

Mastering GDB: How Source-Tracking Breakpoints Simplify Debugging

GDB's source-tracking breakpoints automatically adjust breakpoint lines after code edits, saving manual updates. Learn how to enable, set, verify, and understand limitations.

Ehedrick · 2026-05-02 18:26:32 · Programming

Debugging often involves a cycle of setting breakpoints, editing code, recompiling, and then manually updating breakpoints due to shifted line numbers. GDB's experimental source-tracking breakpoints automate this adjustment, saving time and reducing frustration. Below, we answer key questions about this feature.

What Problem Does Source-Tracking Solve?

When debugging with GDB, you commonly place breakpoints on specific source lines. After editing your code and recompiling, those lines often shift—say from line 42 to line 45—because you added or removed lines above. Without source-tracking, you must disable old breakpoints and set new ones manually, which is tedious during iterative edit-compile-debug cycles. Source-tracking breakpoints capture a small window of surrounding source code at the moment you set the breakpoint. After recompilation and reloading the executable, GDB automatically adjusts any breakpoints whose lines shifted due to source changes. This lets you continue debugging without manual resetting, streamlining your workflow.

Mastering GDB: How Source-Tracking Breakpoints Simplify Debugging
Source: fedoramagazine.org

How Do You Enable Source-Tracking Breakpoints?

Before using this feature, you must activate it in GDB. Run the command:

(gdb) set breakpoint source-tracking enabled on

This enables tracking for subsequent breakpoints set with file:line notation. Note that this is an experimental feature, so you may want to test it in a non‑critical session. Once enabled, GDB will automatically start capturing source context for each qualifying breakpoint you set.

How Do You Set a Source-Tracking Breakpoint?

After enabling the feature, use the standard break command with file and line number:

(gdb) break myfile.c:42

GDB responds with something like:

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

Behind the scenes, GDB captures a few lines of source code surrounding line 42 (by default, three lines). This snapshot is stored so that later, after source changes, GDB can match the original context to the new file and update the breakpoint location accordingly.

What Happens After Recompiling and Reloading?

Suppose you add three lines above the breakpoint, shifting line 42 to line 45. After you recompile the program and type run in the same GDB session, GDB reloads the executable and performs a matching algorithm. When it locates the same source snippet, it adjusts the breakpoint and prints a message like:

Breakpoint 1 adjusted from line 42 to line 45.

Mastering GDB: How Source-Tracking Breakpoints Simplify Debugging
Source: fedoramagazine.org

The breakpoint is now active at the new line. If the match fails—for example, if code shifted beyond the search window—GDB keeps the original location and issues a warning. This adjustment happens automatically, saving you from having to manually update each breakpoint.

How Can You Verify Breakpoint Tracking?

Use the info breakpoints command to see whether a breakpoint is tracked and its current status. Example output:

(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 line “source-tracking enabled (tracking 3 lines around line 45)” confirms that GDB is still tracking that breakpoint. If the breakpoint was not found during reload, you’d see a warning here instead. This command is your go‑to for confirming the current breakpoint locations.

What Are the Limitations of This Feature?

The source‑tracking algorithm relies on an exact string match of the captured source lines. Therefore:

  • Whitespace or trivial reformatting of the tracked lines will cause the match to fail.
  • GDB searches only within a 12‑line window around the original location. If the code shifted by more than 12 lines (e.g., a large block inserted above), the breakpoint won’t be found.
  • When the match fails, GDB keeps the original address and prints a warning like: “warning: Breakpoint 1 source code not found after reload, keeping original location.”

These constraints mean the feature works best for minor edits—small additions or deletions that keep the context within the search window.

Can Source-Tracking Work With Pending Breakpoints?

No. If you set a breakpoint while the symbol table is unavailable—for example, when you enable pending breakpoints via set breakpoint pending on—GDB cannot capture the source context because the file and line aren’t yet resolved. Source context capture requires a valid symbol table, so pending breakpoints are excluded from tracking. To use this feature, ensure that your breakpoints are set after the executable’s symbols are loaded.

Recommended