--- name: debug-session description: Guide hypothesis-driven debugging of a TeachOS problem: a failing host test or sanitizer report, a QEMU boot hang, triple fault or reset loop, or unexpected kernel behavior. Coaches the investigation and explains root causes. Does not edit code or hand over the fix. --- # Debug session Mentor mode applies (AGENTS.md ยง1). You can't edit production code, including temporary debug prints. The human adds instrumentation and applies the fix. Your job is to make the investigation efficient and to explain what happened. ## 1. Pin down the symptom Ask for what is missing, one thing at a time: host test or QEMU kernel, build type (Debug and Release behave differently), exact output, whether it is reproducible, and the last known good commit. Don't speculate before you have the evidence. ## 2. Collect evidence first Host tests: - failing test name and output (`ctest --preset bht-dbg --output-on-failure -R `); - sanitizer reports (ASan, LSan, UBSan, and TSan for stress tests): read the top frames, and note whether they are in kernel code or in test code. - Host and target differ. Host tests run against the `test_support` fakes of the architecture layer, so a host pass doesn't prove the kernel works. Kernel under QEMU: - `qemu-debugcon-.log` (kernel output via the debug console) and `qemu-stderr-.log` (QEMU's own trace). The debug run uses `-d int,cpu_reset -no-reboot`, so the stderr log lists exceptions, faulting addresses and resets. Read the first exception, not the last. - A GDB session: the `(gdb) QEMU` launch config connects to `localhost:1234` and loads `kernel.sym`. The pretty printers in `kapi/gdb` and `scripts/gdb` decode addresses, frames, pages and devices. `.gdbinit` only loads if the repository is on the autoload safe path. - Ask the human to run QEMU and share the logs. Don't start it yourself unless asked. ## 3. Form hypotheses List two to four, ranked. For each: what it predicts, and the cheapest observation that would separate it from the others. Ask which one the human suspects and why, then test that one first. One experiment at a time. ## 4. Instrument, don't fix Suggest where the human could add output (`kstd::println` reaches the debug console), an assertion, a GDB breakpoint or a watchpoint, and say exactly what result confirms or rules out the hypothesis. Narrow with the help of: - `git bisect` by commit (the human runs it, it changes the working tree); - Debug versus Release, sanitizers on or off; - a minimal reproduction as a host test. If asked, write it under the write-tests rules. A failing regression test is a good end state. ## 5. Typical suspects (check, don't assume) - Reset loop or triple fault: stack overflow, bad descriptor tables, a fault while handling a fault, an unmapped address. - Page fault: read the faulting address and error code, then ask whether it is a missing mapping, a stale TLB entry, or a bad pointer. - Hang: a lock taken in interrupt context that the interrupted code already holds; interrupts left disabled. - Corruption: double free in an allocator, use after free, out-of-bounds in a fixed buffer, alignment. - Works on host, fails on target: a `test_support` fake that behaves differently from the hardware. ## 6. When the cause is found Explain it end to end: the mechanism, why it produced this symptom, and which invariant was broken, with `path:line`. Then give the direction of the fix and the test that should pin it. The human writes both. ## 7. If stuck After a few rounds without progress, summarize what has been ruled out, what is still open, and the cheapest next observation. Rules: separate observation from inference. Say how sure you are. Stop and ask when a needed artifact is missing. Never run `git checkout`, `reset`, `stash` or `bisect` yourself.