23 KiB
Step debugger — a guided walkthrough
LightGUI has a built-in stepping debugger: a GDB/MI front end that runs your
program under gdb, stops at breakpoints, and lets you step through it line
by line while watching variables change. This guide walks through a real
debug session, click by click, with screenshots taken from the actual GUI
running under Xvfb. Every screenshot below shows the real debugger in the
state described — no mockups.
Related docs: docs/ide.md §5 (debugger reference),
DESIGN.md (debugger design notes). The debugger itself lives in
src/debugger.cpp, src/debugger_ui.cpp, and the paused-line / breakpoint
wiring in the code window is in src/codeview.cpp.
What you need before you start
- A program built with debug info: compile with
-g(and-O0while you are learning — optimisation reorders lines and hides variables). Without-g, gdb has no line numbers and breakpoints will not resolve. gdbinstalled (/usr/bin/gdbis the default; see Settings to change it).
The sample programs used here
Two targets are used in this guide, so you can follow along exactly.
1. tests/debugger/console.cpp — a console program with a loop
A small program in the repo, already written for this. Compile it with:
g++ -g -O0 -o /tmp/console tests/debugger/console.cpp
(The paths in the screenshots below say /tmp/dbg-guide/work/console
because that is where the guide author's copy lived; the program is the
same.)
| Line | Code | What happens |
|---|---|---|
| 8 | int r = v * 2 + 1; in helper() |
helper(0) → 1, helper(1) → 3, helper(2) → 5 |
| 13 | int s = a + b; in compute() |
compute(10, 20) → s = 30 |
| 18 | int main(int argc, char** argv) { |
program entry |
| 23 | int x = helper(i); |
loop body, runs for i = 0, 1, 2 |
| 24 | sum += x; |
sum goes 0 → 1 → 4 → 9 |
| 25 | std::printf("i=%d x=%d sum=%d\n", i, x, sum); |
prints each iteration |
| 28 | int total = compute(10, 20); |
total = 61 |
| 29 | std::printf("TOTAL total=%d sum=%d\n", total, sum); |
prints TOTAL total=61 sum=9 |
| 30 | std::printf("DONE sum=%d\n", sum); |
prints DONE sum=9 |
Expected run, with no breakpoints: i=0 x=1 sum=1, i=1 x=3 sum=4,
i=2 x=5 sum=9, TOTAL total=61 sum=9, DONE sum=9.
2. A generated project — three widgets, built and debugged
Later in this guide (§15) the same
debugger is used on a real generated LightGUI project: a window with
Button_001, Label_001 and Entry_001, generated to a folder, built with
make CXXFLAGS="-g -O0 -Wall -std=c++17" and debugged as app.
1. Open the debugger
- In the designer, click Debug in the menu bar.
- Click Open Debugger….
The debugger window opens. It starts idle:
The window has, top to bottom: a toolbar (Run, Continue, Step Over, Step Into, Step Out, Stop, Restart, Settings), a Target: row (executable path, Browse…, and an Args: field for command-line arguments), a status line, a Breakpoints pane on the left, and Locals, a watch row, Stack and Target output (stdout/stderr) on the right.
First-run quirk: the breakpoint pane opens squeezed narrow — the location entry is only ~50 px wide and the list shows just the
Ignore/Messagecolumns. Drag the divider between the panes to the right and everything appears. See Troubleshooting.
2. (Optional) Turn on "Break on main"
- In the debugger, click Settings (or Debug ▸ Debugger Settings…).
- Tick Break on main (stop at main()).
- Click Save (or press
Alt+S).
With this on, every Run stops at the first line of main() before any
of your code executes — a handy place to set further breakpoints. The other
options: Stop on entry (stop before main, at program entry),
Pretty-print values (nicer STL containers and structs), Refresh
locals on every stop. Settings are saved to
~/.config/lightgui/debugger.conf and apply to the next debug session
(pretty-print and the break-on-main / stop-on-entry options take effect on
Run).
3. Enter the target and press Run
- Click in the Target: field and type the path to your executable,
e.g.
/tmp/console. - Leave Args: empty (or type arguments here — they are passed to the program).
- Click Run.
Because "Break on main" is on, the program stops at the first line of
main():
The status line reads Stopped (breakpoint-hit) — main at …/console.cpp:18, the Locals pane shows argc = 1 and argv, and the
breakpoint list shows breakpoint #3 main with Hits = 1. (Ignore the
Running… status if you catch it in the first instant after pressing Run —
the status lags the stop event by one poll; see
Troubleshooting.)
4. Set a breakpoint by typing a location
- Click in the breakpoint entry (placeholder
file.cpp:123 or function). - Type
console.cpp:23— the loop lineint x = helper(i);. - Click Add BP.
The list now shows the breakpoint, resolved to its full path:
A relative location like console.cpp:23 is resolved against the
designer's working directory (here /opt/lightgui). You can also type a
function name, e.g. main or helper, instead of file:line. The
Enabled checkbox toggles a breakpoint without deleting it, and Hits
counts how many times it has fired.
5. Set a breakpoint by clicking in the gutter
You don't have to type locations — the code window has a gutter:
- Raise the LightGUI — Code window (the debugger loads the stopped file there automatically when the target stops).
- Click in the gutter, just right of the line number, on the line you
want — here line 24 (
sum += x;). A red dot appears in the gutter.
The shot below was taken paused at the line-23 breakpoint. Line 23 is highlighted solid red with a white ▶ in the gutter (the paused line), and line 24 carries the red dot you just clicked:
Below the code is the Variables in scope — automatic inspection pane:
the scope line reads main() — /opt/lightgui/tests/debugger/console.cpp:23 (breakpoint-hit) and the locals of the current frame are listed — x = 0,
i = 0, argc = 1, argv, sum = 0. It refreshes on every stop. While
the target is running it says Running — variables will appear when the target stops, and with no target it says No target running — start the debugger to inspect variables.
Gutter clicks also work while the debugger is running: a breakpoint added that way is inserted into the live gdb session immediately. (Clicking at the very left edge of the gutter, x < 18, toggles code folding instead — click a little further right, on the line number's right side.)
6. Step Into — follow a call into helper()
The target is stopped at line 23, int x = helper(i);.
- In the debugger, click Step Into (or press
F11).
The status is now Stopped (end-stepping-range) — helper at …/console.cpp:8. The Locals pane switched to helper()'s frame:
v = 0 (the argument) and r = 32767 — r is uninitialised garbage,
because line 8 hasn't executed yet. The Stack pane shows frame 0
helper over frame 1 main, so you can see how you got here.
Note the watch row at the bottom of this shot: the watch sum + 1 added
earlier now reads <error: No symbol "sum" in current context.> — watches
are evaluated in the current frame, and sum is a local of main(),
not of helper(). That error is expected here, not a bug.
7. Step Out — return to the caller
Still stopped inside helper() at line 8:
- Click Step Out.
Status: Stopped (function-finished) — main at …/console.cpp:23. You are
back in main(), still on line 23 — x is still 0 because the return
value hasn't been stored yet. The watch reads sum + 1 = 1 again, now
that sum is in scope.
8. Step Over — run a line without following its calls
From line 23, press Step Over (F10) twice:
- First
F10: executeshelper(i)as one step, stops at line 24 (sum += x;) withx = 1. - Second
F10: executessum += x;, stops at line 25.
The breakpoint list shows both breakpoints with Hits = 1, the locals
show x = 1, i = 0, and the watch sum + 1 now reads 2 — sum is 1
after the first loop iteration. Stepping over is the everyday key: F10
to walk a loop, F11 only when you want to look inside a call.
9. Continue — run until the next breakpoint
- Click Continue (or press
F5).
The target runs freely until it hits a breakpoint (or exits). Each time it
stops, the status line, Locals, Stack and the code window's paused line all
update together. Press F5 again to carry on. When the program finishes,
the output pane shows its exit, e.g. [Inferior 1 (process …) exited normally], and the status returns to idle-ish Exited.
10. Watches — evaluate an expression at every stop
- Click in the watch entry (placeholder
watch expression, e.g. sum + 1). - Type an expression, e.g.
sum + 1. - Click Add Watch.
The watch is re-evaluated at every stop and shown under the entry with its
current value. Any gdb expression works — vals.size(), x * 2, a
variable name on its own. As §6 showed, a watch that names a variable
outside the current frame shows an error instead of a value; that is gdb
telling you the name isn't visible there, not a broken watch. Click a
watch row and Remove to delete it.
Focus quirk: clicks in the watch row can land on the watch list instead of the entry, and typing then opens GTK's interactive-search popup instead of filling the expression. If your text doesn't appear in the entry, click directly inside the entry field (its centre is a little higher than the row looks) and try again. See Troubleshooting.
11. Conditional breakpoints — stop only when it matters
Loop breakpoints fire on every iteration; a condition stops only on the one you care about.
- Click Add… (next to Add BP).
- In the dialog, type the Location:
console.cpp:24. - Click the Type: combo and pick Conditional.
- In Condition: type
i == 2. - Leave Ignore first N hits: at 0.
- Click Save (or press
Alt+S).
The dialog's hint text spells out the rules: a conditional stops only
when the condition is true; a logpoint prints and never stops. Now press
Continue (F5): the program sails past i = 0 and i = 1 and stops
only at the third iteration:
The breakpoint list shows #4 console.cpp:24, type conditional,
condition i == 2, Hits = 1, and the locals confirm i = 2, x = 5.
Ignore first N hits is the same idea counted instead of tested: set
it to 2 and the breakpoint skips its first two hits, then stops normally.
12. Logpoints — print without stopping
A logpoint prints a message every time its line is reached and never
stops the program — printf debugging without editing your code.
- Click Add….
- Location:
console.cpp:28(thecompute(10, 20)call). - Type: Logpoint.
- Log message:
LOG calling compute, sum={sum}. - Click Save.
{expr} prints as an integer, {s:name} as a string, {f:x} as a
float, {x:v} as hex. Press Continue: the program runs to the end
without stopping at line 28, and the message appears in the Target
output pane as [gdb] text, interleaved with the program's own output:
i=2 x=5 sum=9
TOTAL total=61 sum=9
DONE sum=9
[gdb] LOG calling compute, sum=9
[Inferior 1 (process 696825) exited normally]
13. Stop and Restart
- Stop interrupts a running target (breaks into the debugger wherever it happens to be). Press it again to kill the target outright.
- Restart kills the target and runs it again from the start — with
"Break on main" on, it stops at
main()again. This is the reliable way to re-run after a change, and the way out when Stop can't interrupt a target stuck in a GUI event loop (see Troubleshooting).
14. Editing breakpoints
- Enabled checkbox in the list: untick to disable a breakpoint without deleting it; tick to re-enable. (The click needs a firm press — a quick click sometimes doesn't register; see Troubleshooting.)
- Edit…: opens the same dialog as Add…, with the breakpoint's type, condition, ignore-count and log message editable. The Location field is not editable when editing.
- Remove: deletes the selected breakpoint.
- Group: an optional label in the Add/Edit dialog, for grouping related breakpoints.
Known quirk: the breakpoint list is rebuilt every 150 ms by the debugger's poll, which clears the list selection — so a row click often doesn't "stick" and Edit… / Remove then do nothing. Add… (create) always works; for a conditional or logpoint, create it with Add… rather than editing an existing row. See Troubleshooting.
15. Debugging a generated project
The same debugger works on anything you generate — this is the real use case. Build a tiny project and debug it end to end:
-
In the designer, place a few widgets from the Widgets palette onto the canvas — here
Button_001,Label_001andEntry_001. The status bar confirms each one:Entry_001 placed — 3 widget(s) on canvas. -
File ▸ Generate C++/GTK…, pick a folder in the chooser (press
Ctrl+Lto type a path), and click Generate. -
LightGUI lists the generated files — 11 in this case (
MyWindow.h/.cpp, one.h/.cpppair per widget,main.cpp,Makefile,CMakeLists.txt). -
Build it with debug info. This is the step people miss: the generated
MakefileusesCXXFLAGS ?= -O2 -Wall -std=c++17— no-g— and the generatedCMakeLists.txtdoesn't add-geither, so a plainmakeproduces a binary gdb can't map to source lines. Build with:cd <your-folder> make CXXFLAGS="-g -O0 -Wall -std=c++17"which produces
appwith debug info. -
Back in the debugger, put the path to
appin Target: and click Run. With "Break on main" on it stops atmain.cpp:6(int main(int argc, char** argv) {). -
Click Continue (
F5). The generated window appears — a real GTK window, running under the debugger:The window is 800×600, titled
Main Window, with your three widgets at the top left. It is a live program: click its button, type in the entry, and if you have a breakpoint in a widget handler the debugger will stop there. -
Stop interrupts it; Restart kills it and re-runs to
main()again (§13).
Toolbar reference
| Button | Key | What it does |
|---|---|---|
| Run | — | Start the target under gdb. From Idle it reads the Target: entry; from Stopped/Exited it re-runs the currently loaded executable (see the target-switching note below). |
| Continue | F5 |
Continue execution until the next breakpoint, or exit. |
| Step Over | F10 |
Step over the current line — run a function call as one step. |
| Step Into | F11 |
Step into the called function. |
| Step Out | — | Run until the current function returns. |
| Stop | — | Interrupt the running target; press again to kill it. |
| Restart | — | Kill and run the target again. |
| Settings | — | Debugger settings (gdb path, break on main, pretty-print…) — also under Debug ▸ Debugger. |
The Debug menu also has a Debugger submenu with toggles for Break on main, Stop on entry and Pretty-print values, and Debugger Settings… which opens the same dialog as the toolbar's Settings button.
Where settings and breakpoints are saved
Everything persists between LightGUI sessions, in
~/.config/lightgui/:
debugger.conf— settings: gdb path, default target args, break on main / stop on entry / pretty-print / refresh-locals flags, timeouts. Example after §2:gdb_path=/usr/bin/gdb, break_on_main=1, stop_on_entry=0, pretty_print=1, print_all_locals=1, default_args=, stop_timeout_ms=10000, cmd_timeout_ms=5000.debugger_bps.conf— one breakpoint per line:location|enabled|kind|condition|ignore|log_message|group. Example lines from this guide's session:console.cpp:24|1|conditional|i == 2|0||andconsole.cpp:28|1|logpoint||0|LOG calling compute, sum={sum}|. (kindisplain,conditionalorlogpoint;enabledis 1/0.)
Breakpoints are re-applied when the next session starts. Closing the debugger window does not destroy the session — the backend stays alive and keeps your breakpoints, which is why the same breakpoints are still there when you reopen it.
Troubleshooting
The breakpoint pane is too narrow.
At the default divider position the location entry is squeezed to ~50 px
and the list shows only the Ignore/Message columns, with no visible
horizontal scrollbar:
Drag the divider between the breakpoint pane and the Locals pane to the
right. The full entry and all eight columns (#, Location, Enabled,
Hits, Type, Condition, Ignore, Message) appear.
The status says "Running…" but the program already stopped.
The status is polled every 150 ms, so it can show Running… for a beat
after the stop event. Wait a moment; the status line, locals and code
window catch up together.
The debugger stops responding to clicks.
Look for a modal error dialog centred on the debugger window — for
example "finish" not meaningful in the outermost frame. if you press
Step Out while stopped in main() (there is no caller to return
to). Modal dialogs block all input to the debugger until you dismiss
them with OK, and nothing else on screen hints they are there. If
the debugger seems dead, list the windows — an untitled dialog is the
usual culprit.
Stop doesn't interrupt a GUI app.
Pressing Stop while a generated app sits in gtk_main can fail with
"timed out waiting for gdb reply" (5 s timeout) and the app keeps
running. Use Restart instead — it SIGKILLs the target and re-runs it.
Run re-runs the old program after I changed the Target field. Run from Stopped or Exited re-runs the executable that is already loaded; only a fresh (Idle) session reads the Target: entry. Closing the debugger window doesn't reset this — the session stays alive. To debug a different target, quit LightGUI and start it again for a fresh session.
Edit… / Remove do nothing. The list is rebuilt every 150 ms, which wipes the row selection almost as fast as you click it, so usually no row is selected when Edit… or Remove runs and they silently return. Create conditional/logpoint breakpoints with Add… instead, and toggle breakpoints with the Enabled checkbox (press-hold-release works more reliably than a quick click for the checkbox).
Two identical "main" breakpoints.
The break-on-main temporary breakpoint is written into
debugger_bps.conf and re-applied on the next start, so a fresh session
can show two main rows (one with hits, one without). Harmless — disable
or remove the spare one.
Breakpoints from an old target fail on the new one.
Stale breakpoints are dropped best-effort when a session starts (gdb
reports No source file named console.cpp. for locations the new target
doesn't have). They are not kept pending — re-add the ones you need.
My watch text goes into a search popup, not the watch entry. Clicks in the watch row can focus the watch list instead of the entry; typing then opens GTK's interactive-search popup. Click directly inside the entry field and retype.
Breakpoints don't resolve / locals are empty.
The target wasn't built with -g. Rebuild with -g -O0 (for a generated
project, make CXXFLAGS="-g -O0 -Wall -std=c++17" — the generated
Makefile/CMakeLists.txt don't add -g themselves) and run again.
Known bugs found while writing this guide
Found during the screenshot sessions; reported here, not fixed (this guide is documentation only):
- Breakpoint pane too narrow by default (see above).
- Breakpoint list selection doesn't stick — Edit… / Remove usually do nothing (150 ms list rebuild clears the selection).
- Step Out in
main()shows"finish" not meaningful in the outermost frame.as a modal error. - Stop times out (
"timed out waiting for gdb reply") on a GUI app sitting ingtk_main; Restart works. - Run from Stopped/Exited ignores the Target: entry and re-runs the loaded executable; no UI path to switch targets without restarting LightGUI.
- Break-on-main's temporary breakpoint is persisted, so a fresh session
can show two identical
mainbreakpoints. - Watch-entry clicks can land on the watch list and open GTK's interactive-search popup instead of focusing the entry.
- Enabled-checkbox toggles sometimes ignore a quick click.













![Target output — program output plus the [gdb] logpoint line, then exit](/thinkmelt/lightgui/media/commit/b66bc2fbf8a07e01c548cb4ad233168cf25f56af/screenshots/debugger/18_logpoint_output.png)





