Quick Facts
- Category: Programming
- Published: 2026-05-02 04:51:17
- GCC 17 Compiler Gains Support for Hygon C86-4G Series CPUs: A Detailed Q&A
- 9 Essential Insights Into Fedora Atomic Desktop Sealed Bootable Container Images
- Chainguard Forks Abandoned Open Source Projects to Plug Security Gaps
- Trump's 25% Auto Tariff Threat on EU: A New Trade War Looming
- GitHub Copilot Individual Plans: 5 Key Changes and What They Mean for You
Debugging is an iterative process: you set breakpoints, inspect variables, edit code, recompile, and then often need to manually reset those breakpoints because line numbers have shifted. GDB's experimental source-tracking breakpoints change this workflow by automatically adjusting breakpoints after source code modifications. This article explores five crucial aspects of this feature, from how it works to its limitations, so you can integrate it into your development cycle effectively.
1. What Are Source-Tracking Breakpoints?
GDB source-tracking breakpoints are an experimental feature that records the source context around a breakpoint set via file:line notation. When you enable this mode, GDB captures a small window of the surrounding source code at the moment you create the breakpoint. Later, after you edit your source files and recompile, GDB can compare the captured source context with the new version of the file. If the breakpoint's line number has shifted—because lines were added or removed above it—GDB automatically relocates the breakpoint to the correct new line. This eliminates the need to disable old breakpoints and set new ones after each edit-compile cycle, making ad-hoc debugging sessions much smoother. The feature is especially powerful when you are rapidly iterating on code and want to maintain your debugging state without interruptions.

2. Enabling and Setting a Source-Tracking Breakpoint
To start using source-tracking breakpoints, you must first enable the feature with the command set breakpoint source-tracking enabled on in GDB. Once activated, any breakpoint set using the standard file:line syntax will automatically be source-tracked. For example, typing break myfile.c:42 creates a breakpoint that GDB then annotates with the surrounding source context. You can verify the tracking status via the info breakpoints command; it will display a note like source-tracking enabled (tracking 3 lines around line 42). The number of lines GDB captures defaults to 3, but this can be adjusted. It is important to note that breakpoints set with other methods (e.g., by function name or address) are not currently eligible for source tracking. Only file:line breakpoints get the automatic relocation benefit.
3. Automatic Adjustment After Recompilation
The true power of source-tracking breakpoints becomes evident after you modify your source code and recompile. Suppose you added two new lines above the breakpoint you set at line 42, shifting it to line 44. When you type run to reload the newly compiled executable, GDB automatically compares the captured source lines with the new source file. If a match is found, GDB adjusts the breakpoint and displays a message like Breakpoint 1 adjusted from line 42 to line 44. Running info breakpoints confirms the new location, now showing line 44 with the source-tracking flag still active. This adjustment happens transparently, so you can continue debugging without manual intervention. The matching algorithm relies on an exact string match of the captured lines, so whitespace changes or trivial reformatting can cause the adjustment to fail—but in typical use cases where you insert or delete whole lines, the feature works reliably.

4. Key Limitations You Must Know
While source-tracking breakpoints are a fantastic time-saver, they come with important limitations. First, the matching algorithm requires an exact string match of the captured source lines. Any whitespace-only changes, such as adding spaces or reformatting, can prevent GDB from finding the breakpoint. Second, GDB only searches within a 12-line window around the original location. If your code shifted by more than 12 lines—for example, because you inserted a large block of code above the breakpoint—GDB will not find a match. In that case, it keeps the original location and prints a warning: warning: Breakpoint 1 source code not found after reload, keeping original location. Third, source context cannot be captured when a breakpoint is created in a pending state (e.g., with set breakpoint pending on) because no symbol table is available yet. Be aware of these constraints so you can plan your debugging workflow accordingly.
5. Practical Use Cases and Benefits
Source-tracking breakpoints are most beneficial in fast-paced development environments where you frequently edit, compile, and re-debug. For example, when experimenting with algorithm changes or fixing bugs that span multiple functions, you can set breakpoints on critical lines and then make small edits without losing your debugging positions. This is especially helpful in large codebases where manually finding the new line numbers after each change can be tedious. Additionally, the feature aids in reproducing hard-to-catch bugs by preserving the breakpoint logic across iterations. However, for stable code that rarely changes, the standard breakpoint mechanism is sufficient. GDB's source-tracking is an experimental feature, so you should test it in your environment and report any issues to the GDB developers to help improve it. Overall, it's a valuable addition to any developer's toolbox for agile debugging sessions.
GDB's source-tracking breakpoints represent a significant step toward smarter debugging. By automating the tedious task of resetting breakpoints after code changes, they let you focus on the real work: understanding and fixing your code. While the feature has some limitations—exact string matching and a limited search window—it works well for most common scenarios. Give it a try in your next debug session and see how much time you can save.