Add step-by-step debugger guide with real screenshots

This commit is contained in:
cnmcdee committed 2026-11-29 13:30:07 +00:00
1 parent 0206b69211
commit 21a48f8d01
21 files changed
+512

No files matched your search

+512
View File
@@ -0,0 +1,512 @@
# 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 `-O0` while
you are learning — optimisation reorders lines and hides variables).
Without `-g`, gdb has no line numbers and breakpoints will not resolve.
* `gdb` installed (`/usr/bin/gdb` is the default; see
[Settings](#2-optional-turn-on-break-on-main) 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:
```sh
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](#15-debugging-a-generated-project)) 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
1. In the designer, click **Debug** in the menu bar.
2. Click **Open Debugger…**.
![Debug menu — Open Debugger…](../screenshots/debugger/01_debug_menu.png)
The debugger window opens. It starts idle:
![Debugger window, idle](../screenshots/debugger/02_debugger_idle.png)
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`/`Message` columns. Drag the divider between the panes to the
> right and everything appears. See
> [Troubleshooting](#troubleshooting).
## 2. (Optional) Turn on "Break on main"
1. In the debugger, click **Settings** (or **Debug ▸ Debugger Settings…**).
2. Tick **Break on main (stop at main())**.
3. Click **Save** (or press `Alt+S`).
![Debugger Settings — Break on main ticked](../screenshots/debugger/03_settings.png)
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
1. Click in the **Target:** field and type the path to your executable,
e.g. `/tmp/console`.
2. Leave **Args:** empty (or type arguments here — they are passed to the
program).
3. Click **Run**.
Because "Break on main" is on, the program stops at the first line of
`main()`:
![Stopped at main — Target filled in, locals show argc and argv](../screenshots/debugger/08_stopped_at_main.png)
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](#troubleshooting).)
## 4. Set a breakpoint by typing a location
1. Click in the breakpoint entry (placeholder `file.cpp:123 or function`).
2. Type `console.cpp:23` — the loop line `int x = helper(i);`.
3. Click **Add BP**.
The list now shows the breakpoint, resolved to its full path:
![Breakpoint list — console.cpp:23 added, hits counted](../screenshots/debugger/09_breakpoints.png)
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:
1. Raise the **LightGUI — Code** window (the debugger loads the stopped
file there automatically when the target stops).
2. 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:
![Code window paused at line 23 (red, with ▶) and a red-dot breakpoint on line 24](../screenshots/debugger/10_gutter_breakpoint.png)
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);`.
1. In the debugger, click **Step Into** (or press `F11`).
![Stepped into helper() — Locals show v and r, Stack shows helper over main](../screenshots/debugger/11_step_into.png)
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:
1. Click **Step Out**.
![Stepped out of helper() — back in main() at line 23](../screenshots/debugger/13_step_out.png)
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:
1. First `F10`: executes `helper(i)` as one step, stops at line 24
(`sum += x;`) with `x = 1`.
2. Second `F10`: executes `sum += x;`, stops at line 25.
![After two Step Overs — stopped at line 25, x = 1, watch sum + 1 = 2](../screenshots/debugger/12_step_over.png)
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
1. 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
1. Click in the watch entry (placeholder `watch expression, e.g. sum + 1`).
2. Type an expression, e.g. `sum + 1`.
3. Click **Add Watch**.
![Watch sum + 1 = 1, with locals x = 0, i = 0](../screenshots/debugger/14_watch.png)
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](#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.
1. Click **Add…** (next to **Add BP**).
2. In the dialog, type the **Location:** `console.cpp:24`.
3. Click the **Type:** combo and pick **Conditional**.
4. In **Condition:** type `i == 2`.
5. Leave **Ignore first N hits:** at 0.
6. Click **Save** (or press `Alt+S`).
![Add breakpoint dialog — Conditional, i == 2, at console.cpp:24](../screenshots/debugger/15_conditional_dialog.png)
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:
![Conditional stop — line 24, i = 2, x = 5, breakpoint #4 hits = 1](../screenshots/debugger/16_conditional_stop.png)
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.
1. Click **Add…**.
2. **Location:** `console.cpp:28` (the `compute(10, 20)` call).
3. **Type:** **Logpoint**.
4. **Log message:** `LOG calling compute, sum={sum}`.
5. Click **Save**.
![Add breakpoint dialog — Logpoint at console.cpp:28](../screenshots/debugger/17_logpoint_dialog.png)
`{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:
![Target output — program output plus the [gdb] logpoint line, then exit](../screenshots/debugger/18_logpoint_output.png)
```
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](#troubleshooting)).
![Restart — target re-run, stopped at main again](../screenshots/debugger/19_demo_restart.png)
## 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](#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](#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:
1. In the designer, place a few widgets from the **Widgets** palette onto
the canvas — here `Button_001`, `Label_001` and `Entry_001`. The
status bar confirms each one: `Entry_001 placed — 3 widget(s) on
canvas`.
![Designer with Button_001, Label_001 and Entry_001 placed](../screenshots/debugger/04_demo_placed.png)
2. **File ▸ Generate C++/GTK…**, pick a folder in the chooser (press
`Ctrl+L` to type a path), and click **Generate**.
![Folder chooser with the output path typed in](../screenshots/debugger/05_generate_dialog.png)
3. LightGUI lists the generated files — 11 in this case
(`MyWindow.h/.cpp`, one `.h/.cpp` pair per widget, `main.cpp`,
`Makefile`, `CMakeLists.txt`).
![Generated C++/GTK project — 11 files](../screenshots/debugger/06_generated_files.png)
4. Build it **with debug info**. This is the step people miss: the
generated `Makefile` uses `CXXFLAGS ?= -O2 -Wall -std=c++17` — no
`-g` — and the generated `CMakeLists.txt` doesn't add `-g` either,
so a plain `make` produces a binary gdb can't map to source lines.
Build with:
```sh
cd <your-folder>
make CXXFLAGS="-g -O0 -Wall -std=c++17"
```
which produces `app` with debug info.
5. Back in the debugger, put the path to `app` in **Target:** and click
**Run**. With "Break on main" on it stops at `main.cpp:6`
(`int main(int argc, char** argv) {`).
6. Click **Continue** (`F5`). The generated window appears — a real GTK
window, running under the debugger:
![The generated app running — Main Window with Button_001, Label_001, Entry_001](../screenshots/debugger/07_demo_running.png)
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.
7. **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||` and
`console.cpp:28|1|logpoint||0|LOG calling compute, sum={sum}|`.
(`kind` is `plain`, `conditional` or `logpoint`; `enabled` is 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:
![Squeezed breakpoint pane at the default divider](../screenshots/debugger/20_narrow_pane.png)
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):
1. Breakpoint pane too narrow by default (see above).
2. Breakpoint list selection doesn't stick — **Edit…** / **Remove**
usually do nothing (150 ms list rebuild clears the selection).
3. **Step Out** in `main()` shows `"finish" not meaningful in the
outermost frame.` as a modal error.
4. **Stop** times out (`"timed out waiting for gdb reply"`) on a GUI app
sitting in `gtk_main`; **Restart** works.
5. **Run** from Stopped/Exited ignores the **Target:** entry and re-runs
the loaded executable; no UI path to switch targets without
restarting LightGUI.
6. Break-on-main's temporary breakpoint is persisted, so a fresh session
can show two identical `main` breakpoints.
7. Watch-entry clicks can land on the watch list and open GTK's
interactive-search popup instead of focusing the entry.
8. Enabled-checkbox toggles sometimes ignore a quick click.
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB