debugger: the kernel can be asked a question
docs/plan/debugger.md, milestones D0, D1 and D2. What they replace is the
ninety-second loop -- add a cprintf, rebuild, boot, read the serial log --
which answers exactly the one question you thought of before the rebuild.
The rule the shape follows: **it observes, it does not control.** No
stepping, no software breakpoints, no stopping the world. Half the bugs
this kernel produces are timing -- an interrupt storm, a level-triggered
line nobody acknowledges, a driver that faults only on its second start --
and every one of them disappears when you halt the machine to look. And
the layering rule, which is the circularity rule the display already has:
a debugger that needs lwIP cannot debug lwIP.
D0 -- symbols. tools/ksyms.pl turns the first link pass into a table the
second pass carries. Two passes is enough because .ksym is placed after
.text and .rodata, so carrying the table cannot move anything recorded in
it; the build lists both passes' text symbols and refuses to link if they
ever disagree, because a symbol table that is subtly wrong gives confident
false answers. ksym.c reads it through weak symbols, so pass one -- which
has no table -- still links. A crash chain now reads panic+0x5f <-
proc_sysrq_cmd+0x99 <- ... <- syscall+0xf6.
D1 -- a log that outlives the kernel. Every character through consputc()
lands in a byte ring with a timestamp per line, readable as /proc/kmsg.
It sits in a region carved before the page allocator gets anything, with a
magic header, so a kernel finds the previous kernel's log still there --
including the whole PSOGM report. panic.c's own comment said "there is no
core dump and no log to go back to"; that is no longer true.
The obvious place for it is the top of RAM beside the DMA arena. That
was the first attempt and the magic came back as ASCII on the reboot,
because the top of usable memory is exactly where the firmware makes its
own allocations. It lives above the kernel image instead, which is a
stable address across boots of the same build.
D2 -- the introspection registry. A subsystem publishes its state with one
line at the bottom of the file that owns the data:
dbg_register("dma", dma_dump, "the DMA arena's free extents");
Seven views -- dma, mem, proc, vma, claims, pci, sock -- reachable three
ways: sysrq 'g' (always, no process and no filesystem), /dev/dbg (echo a
line, cat the answer), and schdbg. The protocol is lines of text because
the same bytes have to work where nothing can allocate or frame a record,
and because a protocol you cannot drive by hand is one you cannot debug.
struct sbuf moved out of kernel/dev/procfs.c into include/sbuf.h rather
than being written a second time.
Several views exist because of a specific bug. `vma` is the table that
decides whether exit() frees a page, and the DMA ownership bug that made
this worth building was a block that was never in it.
Also fixed, found by building with clang for the first time in a while:
struct iovec was defined in both posix.h and socket.h, and every file that
included both was a redefinition. GCC 15 defaults to C23, which permits an
identical struct redeclaration, so the gcc path never noticed -- while
clang defaults to gnu17 and rejected it, breaking the one toolchain
shell.nix exists to provide and the UEFI loader requires. Both are now
guarded.
D3 (`whose <addr>`, the cross-subsystem ownership query) and D4 (hardware
watchpoints on DR0-DR3) are not built and are described honestly as
missing in the architecture doc's "What it does not do".
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
parent
d1f50c4da3
commit
1dd4ba0186
35 changed files with 2296 additions and 83 deletions
128
kernel/core/ksym.c
Normal file
128
kernel/core/ksym.c
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// Naming an address.
|
||||
//
|
||||
// docs/plan/debugger.md milestone D0. It is the least clever thing on that
|
||||
// plan and the one everything else is worth more with: a call chain of six
|
||||
// hex numbers has to be resolved by hand against a disassembly that may not
|
||||
// be the build that was running, and the resolving is done at exactly the
|
||||
// moment when you least want a second task.
|
||||
//
|
||||
// The table is built by tools/ksyms.pl from the *first* of two link passes and
|
||||
// linked into the second. kernel/kernel.ld puts .ksym after .text and .rodata,
|
||||
// so carrying the table cannot move the addresses recorded in it; the Makefile
|
||||
// checks that rather than trusting it, by listing the text symbols of both
|
||||
// passes and refusing to build if they differ.
|
||||
|
||||
#include "types.h"
|
||||
#include "defs.h"
|
||||
|
||||
// Weak, and the reason is worth stating: the first link pass has no table --
|
||||
// it is the input to the program that writes one. An undefined weak symbol
|
||||
// links as zero, so that pass resolves these to nothing and still links. The
|
||||
// same property means a kernel built with the generator removed boots fine and
|
||||
// simply cannot name anything, which is a much better failure than a build
|
||||
// that stops.
|
||||
extern const uint32 ksym_count __attribute__((weak));
|
||||
extern const uint64 ksym_base __attribute__((weak));
|
||||
extern const uint64 ksym_textend __attribute__((weak));
|
||||
extern const uint32 ksym_addr[] __attribute__((weak));
|
||||
extern const uint32 ksym_nameoff[] __attribute__((weak));
|
||||
extern const char ksym_names[] __attribute__((weak));
|
||||
|
||||
int
|
||||
ksym_ready(void)
|
||||
{
|
||||
return &ksym_count != 0 && ksym_count > 0;
|
||||
}
|
||||
|
||||
// The greatest symbol at or below addr.
|
||||
//
|
||||
// Returns 0 for anything outside the kernel's text, which includes every
|
||||
// userspace address -- naming a user rip with a kernel symbol would be worse
|
||||
// than printing the number, because it would look like an answer.
|
||||
const char *
|
||||
ksym_lookup(uint64 addr, uint64 *off)
|
||||
{
|
||||
if (!ksym_ready())
|
||||
return 0;
|
||||
if (addr < ksym_base || addr >= ksym_textend)
|
||||
return 0;
|
||||
|
||||
uint32 want = (uint32)(addr - ksym_base);
|
||||
|
||||
// Binary search for the last entry <= want, in the half-open form so the
|
||||
// indices stay unsigned and nothing has to go below zero. ksym_addr[0] is 0
|
||||
// by construction -- the base *is* the first symbol -- so the invariant
|
||||
// "ksym_addr[lo] <= want" holds on entry and there is no empty case.
|
||||
uint32 lo = 0, hi = ksym_count;
|
||||
while (hi - lo > 1) {
|
||||
uint32 mid = lo + (hi - lo) / 2;
|
||||
if (ksym_addr[mid] <= want)
|
||||
lo = mid;
|
||||
else
|
||||
hi = mid;
|
||||
}
|
||||
|
||||
if (off)
|
||||
*off = want - ksym_addr[lo];
|
||||
return &ksym_names[ksym_nameoff[lo]];
|
||||
}
|
||||
|
||||
static int
|
||||
put(char *buf, int cap, int n, const char *s)
|
||||
{
|
||||
while (*s && n < cap - 1)
|
||||
buf[n++] = *s++;
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
puthex(char *buf, int cap, int n, uint64 v)
|
||||
{
|
||||
static const char h[] = "0123456789abcdef";
|
||||
char tmp[17];
|
||||
int i = 0;
|
||||
|
||||
if (v == 0)
|
||||
return put(buf, cap, n, "0");
|
||||
while (v && i < 16) { tmp[i++] = h[v & 0xF]; v >>= 4; }
|
||||
while (i-- > 0 && n < cap - 1)
|
||||
buf[n++] = tmp[i];
|
||||
return n;
|
||||
}
|
||||
|
||||
// "sched+0x2a", or the bare address when there is nothing to say.
|
||||
//
|
||||
// Writes into a caller's buffer rather than printing, because both callers
|
||||
// that matter are in panic context: one is building a line for the crash
|
||||
// screen and the other is building the QR payload. Neither can call cprintf.
|
||||
int
|
||||
ksym_fmt(char *buf, int cap, uint64 addr)
|
||||
{
|
||||
uint64 off = 0;
|
||||
const char *name;
|
||||
int n = 0;
|
||||
|
||||
if (cap <= 1) {
|
||||
if (cap == 1)
|
||||
buf[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
name = ksym_lookup(addr, &off);
|
||||
if (!name) {
|
||||
n = put(buf, cap, n, "0x");
|
||||
n = puthex(buf, cap, n, addr);
|
||||
buf[n] = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
n = put(buf, cap, n, name);
|
||||
if (off) {
|
||||
n = put(buf, cap, n, "+0x");
|
||||
n = puthex(buf, cap, n, off);
|
||||
}
|
||||
buf[n] = 0;
|
||||
return n;
|
||||
}
|
||||
Loading…
Reference in a new issue