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
139 lines
4 KiB
C
139 lines
4 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
// schdbg -- ask the kernel a question.
|
|
//
|
|
// docs/plan/debugger.md, milestone D2. The kernel half is /dev/dbg: write a
|
|
// line, read the answer. This is the half that makes that pleasant, and it is
|
|
// deliberately almost nothing -- because the protocol is lines of text, and a
|
|
// protocol whose client has to be clever is a protocol you cannot use when the
|
|
// client will not start.
|
|
//
|
|
// schdbg the views this kernel publishes
|
|
// schdbg dma one of them
|
|
// schdbg all all of them
|
|
// schdbg -i a prompt, for when you are asking several
|
|
//
|
|
// Everything here works equally well as:
|
|
//
|
|
// echo dma > /dev/dbg && cat /dev/dbg
|
|
//
|
|
// which is the point, and is what you fall back to on a system where this
|
|
// program is one of the things that is broken.
|
|
|
|
#include "types.h"
|
|
#include "user.h"
|
|
#include "fcntl.h"
|
|
|
|
static void
|
|
puts_(const char *s)
|
|
{
|
|
int n = 0;
|
|
while (s[n]) n++;
|
|
write(1, (void *)s, n);
|
|
}
|
|
|
|
// One question, one answer.
|
|
//
|
|
// The descriptor is opened fresh for each, because the answer is read from
|
|
// offset zero and the kernel replaces it on every write. Reusing one open
|
|
// descriptor across two questions would read the second answer from wherever
|
|
// the first one left off.
|
|
static int
|
|
ask(const char *cmd)
|
|
{
|
|
char buf[512];
|
|
int fd, n;
|
|
|
|
fd = open("/dev/dbg", O_RDWR);
|
|
if (fd < 0) {
|
|
puts_("schdbg: cannot open /dev/dbg\n");
|
|
return -1;
|
|
}
|
|
|
|
if (cmd) {
|
|
int len = 0;
|
|
while (cmd[len]) len++;
|
|
if (write(fd, (void *)cmd, len) != len) {
|
|
puts_("schdbg: the kernel would not take that command\n");
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
// Back to the start. Writing moved this descriptor's offset forward by
|
|
// the length of the command, and the answer begins at zero -- so
|
|
// without this the reply arrives with its first few characters missing,
|
|
// which is subtle enough to look like a formatting bug in the view.
|
|
lseek(fd, 0, 0); /* SEEK_SET */
|
|
}
|
|
|
|
while ((n = read(fd, buf, sizeof(buf))) > 0)
|
|
write(1, buf, n);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
// A line from the terminal, without the newline. Returns 0 at end of input.
|
|
static int
|
|
readline(char *buf, int cap)
|
|
{
|
|
int n = 0;
|
|
char c;
|
|
|
|
for (;;) {
|
|
int r = read(0, &c, 1);
|
|
if (r <= 0)
|
|
return n > 0;
|
|
if (c == '\n') {
|
|
buf[n] = 0;
|
|
return 1;
|
|
}
|
|
if (n < cap - 1)
|
|
buf[n++] = c;
|
|
}
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'i') {
|
|
char line[128];
|
|
|
|
puts_("schdbg: ask the kernel something. 'help' lists the views, "
|
|
"'quit' leaves.\n");
|
|
for (;;) {
|
|
puts_("dbg> ");
|
|
if (!readline(line, sizeof(line)))
|
|
break;
|
|
if (line[0] == 0)
|
|
continue;
|
|
if (line[0] == 'q' && line[1] == 'u' && line[2] == 'i' &&
|
|
line[3] == 't' && line[4] == 0)
|
|
break;
|
|
ask(line);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// No argument means "what can I ask", which is the question someone has
|
|
// the first time and the one an empty response would not answer.
|
|
if (argc < 2)
|
|
return ask("help") < 0 ? 1 : 0;
|
|
|
|
// Everything after the program name, rejoined with single spaces. The
|
|
// shell split it and the kernel wants a line; `schdbg vma 4` has to arrive
|
|
// as "vma 4" or the argument is silently dropped and the view quietly
|
|
// ignores the narrowing it was asked for.
|
|
{
|
|
char line[128];
|
|
int n = 0;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
if (i > 1 && n < (int)sizeof(line) - 1)
|
|
line[n++] = ' ';
|
|
for (int j = 0; argv[i][j] && n < (int)sizeof(line) - 1; j++)
|
|
line[n++] = argv[i][j];
|
|
}
|
|
line[n] = 0;
|
|
return ask(line) < 0 ? 1 : 0;
|
|
}
|
|
}
|