Programming

Mastering GDB Source-Tracking Breakpoints: A Step-by-Step Guide

2026-05-04 12:11:26

Introduction

Have you ever been deep in a debugging session with GDB, setting breakpoints on several source lines, inspecting values, and forming hypotheses? You edit your code, recompile, and type run to test the new build—only to find your carefully placed breakpoints have shifted because line numbers changed. Without source-tracking, you must manually disable old breakpoints and set new ones. GDB's experimental source-tracking breakpoints eliminate this hassle. When enabled, GDB captures a snippet of surrounding source code at the breakpoint location. After recompilation and reloading, GDB automatically adjusts breakpoints whose lines moved due to code changes. This guide walks you through enabling and using this feature effectively.

Mastering GDB Source-Tracking Breakpoints: A Step-by-Step Guide
Source: fedoramagazine.org

What You Need

Step-by-Step Instructions

Step 1: Enable Source-Tracking

Before setting breakpoints, you must activate the source-tracking feature. Inside GDB, run:

(gdb) set breakpoint source-tracking enabled on

This command tells GDB to capture and track source context for all future breakpoints set with file:line notation.

Step 2: Set a Breakpoint Using File:Line

Place a breakpoint with the familiar file:line syntax. For example, if your source is myfile.c and you want to stop at line 42:

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

GDB now stores a small window of lines around line 42. The default window size is 3 lines above and below, but you can adjust it (see Tips).

Step 3: Verify Tracking Is Active

Always confirm that source-tracking is enabled for your breakpoint. Use 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)

The “source-tracking enabled” message confirms GDB will adjust this breakpoint after source changes.

Step 4: Edit and Recompile Your Source

Leave GDB running (do not exit). Open your source file in a text editor and make changes above the breakpoint line. For instance, add a few lines of code before line 42. Save the file and recompile your program with debug symbols, e.g.:

gcc -g -o myprogram myfile.c

Keep the same executable name so GDB can reload it.

Step 5: Reload the New Executable

Back in GDB, type run (or r) to restart the program with the new binary. GDB automatically reloads the executable and scans the source code for each tracked breakpoint. If the code has shifted, GDB prints a message like:

Breakpoint 1 adjusted from line 42 to line 45.

Now, when the program hits breakpoint 1, it will stop at the correct new line 45, preserving your debugging session.

Step 6: Confirm the New Location

Run info breakpoints again to see the updated breakpoint details:

(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 number and address have changed, but the breakpoint remains active and tracked.

Mastering GDB Source-Tracking Breakpoints: A Step-by-Step Guide
Source: fedoramagazine.org

Handling Limitations

Source-tracking is powerful but has constraints you should understand:

warning: Breakpoint 1 source code not found after reload, keeping original location.

Tips for Success

By following these steps and tips, you can streamline your debugging workflow and avoid the frustration of resetting breakpoints after every code change. Source-tracking breakpoints make iterative editing and debugging much smoother.

Explore

Snowden Leaks: Former NSA Chief Chris Inglis on Mistakes, Insider Threats, and Media Disclosures SUSE Unveils AI-Native Infrastructure Layer for Enterprise Clouds at KubeCon Europe 2026 8 Fascinating Facts About the Pleiades 'Seven Sisters' and Their Ghostly Blue Veil Breaking: Massive Discounts on Samsung Galaxy Tab S11 Ultra, Galaxy S26 Ultra, Galaxy Book6, and Amazon Echo Devices Python 3.13.6: A Maintenance Release Packed with Improvements