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
|
|
@ -118,4 +118,7 @@ struct devsw devsw[] = {
|
|||
// that do not live in the kernel. Registers arrive through mmap and
|
||||
// everything else through ioctl; see include/pciuser.h.
|
||||
[9] = { pciuser_read, pciuser_write },
|
||||
// /dev/dbg (major 10) -- ask the kernel a question. Write a line, read the
|
||||
// answer; see include/dbg.h and docs/plan/debugger.md.
|
||||
[10] = { dbg_read, dbg_write },
|
||||
};
|
||||
|
|
|
|||
243
kernel/dev/dbg.c
Normal file
243
kernel/dev/dbg.c
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// /dev/dbg -- asking the kernel a question. See include/dbg.h.
|
||||
//
|
||||
// The device is a conversation: write a line, read the answer.
|
||||
//
|
||||
// # echo dma > /dev/dbg && cat /dev/dbg
|
||||
//
|
||||
// That shape is chosen so the debugger is usable with the tools already on the
|
||||
// machine, before `schdbg` is built and on a system where it will not start.
|
||||
// docs/plan/debugger.md argues the case at length; the short version is that a
|
||||
// protocol you cannot drive by hand is a protocol you cannot debug.
|
||||
//
|
||||
// One conversation at a time, deliberately. The answer buffer is global and
|
||||
// the lock is held only while it is being filled, so two programs asking at
|
||||
// once get one answer each, in some order, and neither gets half of the
|
||||
// other's. Making it per-open would mean a struct file hook for something that
|
||||
// has exactly one user.
|
||||
|
||||
#include "types.h"
|
||||
#include "defs.h"
|
||||
#include "param.h"
|
||||
#include "spinlock.h"
|
||||
#include "sleeplock.h"
|
||||
#include "fs.h"
|
||||
#include "file.h"
|
||||
#include "errno.h"
|
||||
#include "sbuf.h"
|
||||
#include "dbg.h"
|
||||
|
||||
#define NVIEW 24
|
||||
#define DBGBUF (16 * 1024)
|
||||
|
||||
struct view {
|
||||
const char *name;
|
||||
dbg_view_fn fn;
|
||||
const char *what;
|
||||
};
|
||||
|
||||
static struct view views[NVIEW];
|
||||
static int nview;
|
||||
|
||||
// Static, and 16 KB of it. A view of every process on a 64-slot table is the
|
||||
// largest thing here and comes to a few kilobytes; the alternative -- kalloc
|
||||
// on every read -- would put an allocation on the path of the tool you reach
|
||||
// for when the allocator is what you are investigating.
|
||||
static char dbgbuf[DBGBUF];
|
||||
static int dbglen;
|
||||
static struct spinlock dbglock;
|
||||
static int dbginit;
|
||||
|
||||
void
|
||||
dbg_register(const char *name, dbg_view_fn fn, const char *what)
|
||||
{
|
||||
if (nview >= NVIEW || !name || !fn)
|
||||
return;
|
||||
views[nview].name = name;
|
||||
views[nview].fn = fn;
|
||||
views[nview].what = what ? what : "";
|
||||
nview++;
|
||||
}
|
||||
|
||||
static int
|
||||
streq(const char *a, const char *b)
|
||||
{
|
||||
while (*a && *b && *a == *b) { a++; b++; }
|
||||
return *a == 0 && *b == 0;
|
||||
}
|
||||
|
||||
static void
|
||||
do_help(struct sbuf *out)
|
||||
{
|
||||
sb_puts(out, "views this kernel publishes:\n");
|
||||
for (int i = 0; i < nview; i++) {
|
||||
sb_puts(out, " ");
|
||||
sb_pad(out, views[i].name, 10);
|
||||
sb_puts(out, views[i].what);
|
||||
sb_putc(out, '\n');
|
||||
}
|
||||
sb_puts(out, "\n all every view above\n");
|
||||
sb_puts(out, " help this\n");
|
||||
}
|
||||
|
||||
static void
|
||||
do_all(struct sbuf *out)
|
||||
{
|
||||
for (int i = 0; i < nview; i++) {
|
||||
sb_puts(out, "=== ");
|
||||
sb_puts(out, views[i].name);
|
||||
sb_puts(out, " ===\n");
|
||||
views[i].fn(out, "");
|
||||
sb_putc(out, '\n');
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
dbg_command(const char *line, struct sbuf *out)
|
||||
{
|
||||
char word[32], arg[64];
|
||||
int n = 0;
|
||||
|
||||
while (*line == ' ' || *line == '\t')
|
||||
line++;
|
||||
while (*line && *line != '\n' && *line != ' ' && n < (int)sizeof(word) - 1)
|
||||
word[n++] = *line++;
|
||||
word[n] = 0;
|
||||
|
||||
while (*line == ' ' || *line == '\t')
|
||||
line++;
|
||||
n = 0;
|
||||
while (*line && *line != '\n' && n < (int)sizeof(arg) - 1)
|
||||
arg[n++] = *line++;
|
||||
while (n > 0 && (arg[n-1] == ' ' || arg[n-1] == '\r'))
|
||||
n--;
|
||||
arg[n] = 0;
|
||||
|
||||
if (word[0] == 0 || streq(word, "help")) {
|
||||
do_help(out);
|
||||
return out->len;
|
||||
}
|
||||
if (streq(word, "all")) {
|
||||
do_all(out);
|
||||
return out->len;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nview; i++) {
|
||||
if (streq(word, views[i].name)) {
|
||||
views[i].fn(out, arg);
|
||||
return out->len;
|
||||
}
|
||||
}
|
||||
|
||||
sb_puts(out, "no view called '");
|
||||
sb_puts(out, word);
|
||||
sb_puts(out, "'. try: help\n");
|
||||
return out->len;
|
||||
}
|
||||
|
||||
// Fill dbgbuf, and say so if the answer did not fit.
|
||||
//
|
||||
// sb_* silently drops what will not fit, which is the right behaviour for a
|
||||
// builder that must not fail -- but a table that stops in the middle of a row
|
||||
// with no explanation reads as a kernel that is broken rather than as a buffer
|
||||
// that is full. The last 64 bytes are held back to have somewhere to say it.
|
||||
static int
|
||||
fill(const char *cmd)
|
||||
{
|
||||
struct sbuf sb;
|
||||
|
||||
sb.p = dbgbuf; sb.len = 0; sb.cap = DBGBUF - 64;
|
||||
if (cmd)
|
||||
dbg_command(cmd, &sb);
|
||||
else
|
||||
do_all(&sb);
|
||||
|
||||
if (sb.len >= sb.cap - 1) {
|
||||
sb.cap = DBGBUF;
|
||||
sb_puts(&sb, "\n-- truncated: the answer is larger than the buffer. "
|
||||
"ask for one view at a time.\n");
|
||||
}
|
||||
dbgbuf[sb.len] = 0;
|
||||
return sb.len;
|
||||
}
|
||||
|
||||
void
|
||||
dbg_dump_all(void)
|
||||
{
|
||||
if (!dbginit) { initlock(&dbglock, "dbg"); dbginit = 1; }
|
||||
|
||||
acquire(&dbglock);
|
||||
dbglen = fill(0);
|
||||
// Printed inside the lock so a second sysrq cannot interleave with this one.
|
||||
// cprintf takes cons.lock, which is never held by anything that wants
|
||||
// dbglock, so the order is fixed and there is no cycle.
|
||||
cprintf("%s", dbgbuf);
|
||||
release(&dbglock);
|
||||
}
|
||||
|
||||
// --- the device -------------------------------------------------------------
|
||||
|
||||
// A command. The answer replaces whatever was there.
|
||||
//
|
||||
// **The answer is read from offset zero.** That matters because writing to a
|
||||
// descriptor advances its offset, so a program that writes and then reads on
|
||||
// the same descriptor starts reading as many bytes into the answer as its
|
||||
// command was long -- which is exactly the bug this comment exists to stop
|
||||
// being written again. `cat` is unaffected because it opens the file fresh;
|
||||
// cmd/schdbg seeks back to zero. See lseek() there.
|
||||
int
|
||||
dbg_write(struct inode *ip, uint64 off, char *src, int n)
|
||||
{
|
||||
char line[128];
|
||||
int i;
|
||||
|
||||
(void)ip; (void)off;
|
||||
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
if (n > (int)sizeof(line) - 1)
|
||||
n = (int)sizeof(line) - 1;
|
||||
for (i = 0; i < n; i++)
|
||||
line[i] = src[i];
|
||||
line[n] = 0;
|
||||
|
||||
if (!dbginit) { initlock(&dbglock, "dbg"); dbginit = 1; }
|
||||
|
||||
acquire(&dbglock);
|
||||
dbglen = fill(line);
|
||||
release(&dbglock);
|
||||
|
||||
return n; // the whole line was consumed either way
|
||||
}
|
||||
|
||||
int
|
||||
dbg_read(struct inode *ip, uint64 off, char *dst, int n)
|
||||
{
|
||||
int i, avail;
|
||||
|
||||
(void)ip;
|
||||
|
||||
if (!dbginit) { initlock(&dbglock, "dbg"); dbginit = 1; }
|
||||
|
||||
acquire(&dbglock);
|
||||
if (dbglen == 0) {
|
||||
// Nothing asked yet. Answering with the help text rather than with EOF,
|
||||
// because `cat /dev/dbg` on its own is what someone does first and an
|
||||
// empty file teaches them nothing.
|
||||
struct sbuf sb = { dbgbuf, 0, DBGBUF };
|
||||
do_help(&sb);
|
||||
dbglen = sb.len;
|
||||
}
|
||||
if (off >= (uint64)dbglen) {
|
||||
release(&dbglock);
|
||||
return 0; // EOF
|
||||
}
|
||||
avail = dbglen - (int)off;
|
||||
if (n > avail)
|
||||
n = avail;
|
||||
for (i = 0; i < n; i++)
|
||||
dst[i] = dbgbuf[off + i];
|
||||
release(&dbglock);
|
||||
return n;
|
||||
}
|
||||
|
|
@ -21,6 +21,8 @@
|
|||
#include "fs.h"
|
||||
#include "file.h"
|
||||
#include "pci.h"
|
||||
#include "sbuf.h"
|
||||
#include "dbg.h"
|
||||
#include "device.h"
|
||||
|
||||
static struct pci_dev devs[NPCI];
|
||||
|
|
@ -497,10 +499,54 @@ pci_info(char *buf, int cap)
|
|||
return len;
|
||||
}
|
||||
|
||||
// The same lines /proc/pci carries, plus the command register, which is the
|
||||
// bit that says whether a device can still reach memory. pciuser.c clears bus
|
||||
// mastering when a userspace driver dies -- see "Taking it back" in
|
||||
// docs/architecture/devices.md -- and "did that actually happen" is a question
|
||||
// worth being able to ask without a userspace program.
|
||||
static void
|
||||
pci_dump(struct sbuf *out, const char *arg)
|
||||
{
|
||||
(void)arg;
|
||||
sb_puts(out, " bdf vendor:device class "
|
||||
"cmd bm io mem driver\n");
|
||||
|
||||
for (int i = 0; i < ndevs; i++) {
|
||||
struct pci_dev *d = &devs[i];
|
||||
uint32 cmd = pci_read32(d->bus, d->slot, d->func, PCI_COMMAND) & 0xFFFF;
|
||||
|
||||
sb_puts(out, " ");
|
||||
sb_puthex(out, d->bus, 2); sb_putc(out, ':');
|
||||
sb_puthex(out, d->slot, 2); sb_putc(out, '.');
|
||||
sb_puthex(out, d->func, 1);
|
||||
sb_puts(out, " ");
|
||||
sb_puthex4(out, d->vendor); sb_putc(out, ':');
|
||||
sb_puthex4(out, d->device);
|
||||
sb_puts(out, " ");
|
||||
sb_pad(out, class_name(d->class, d->subclass), 25);
|
||||
sb_puthex4(out, cmd);
|
||||
sb_puts(out, (cmd & PCI_CMD_BUS_MASTER) ? " on " : " -- ");
|
||||
sb_puts(out, (cmd & PCI_CMD_IO) ? " on " : " -- ");
|
||||
sb_puts(out, (cmd & PCI_CMD_MEMORY) ? " on " : " -- ");
|
||||
sb_puts(out, " ");
|
||||
{
|
||||
int pid = pciuser_owner_pid(i);
|
||||
if (pid) {
|
||||
sb_puts(out, "userspace pid ");
|
||||
sb_putu(out, pid);
|
||||
} else {
|
||||
sb_puts(out, d->driver ? d->driver : "-");
|
||||
}
|
||||
}
|
||||
sb_putc(out, '\n');
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
pci_init(void)
|
||||
{
|
||||
ndevs = 0;
|
||||
dbg_register("pci", pci_dump, "the bus, with each device's command register");
|
||||
|
||||
// If there is no host bridge at 0:0.0 there is no PCI, and every config
|
||||
// read will come back 0xFFFFFFFF. Saying so once beats 8192 silent probes.
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@
|
|||
#include "uaccess.h"
|
||||
#include "irq.h"
|
||||
#include "pciuser.h"
|
||||
#include "sbuf.h"
|
||||
#include "dbg.h"
|
||||
int mappages(pml4e_t *pgdir, void *va, uint64 size, uint64 pa, int perm);
|
||||
|
||||
// Who holds each device, or 0. Indexed the same way pci_get() is, because the
|
||||
|
|
@ -89,10 +91,122 @@ pciuser_irq_handler(void *arg)
|
|||
wakeup(&irqdev);
|
||||
}
|
||||
|
||||
// The pid holding a device, or 0. For kernel/dev/pci.c's `pci` view, which
|
||||
// otherwise prints "-" in the driver column for a device that is very much
|
||||
// being driven -- just not from in here.
|
||||
int
|
||||
pciuser_owner_pid(int idx)
|
||||
{
|
||||
int pid = 0;
|
||||
if (idx < 0 || idx >= NPCI)
|
||||
return 0;
|
||||
acquire(&pciuser_lock);
|
||||
if (owner[idx])
|
||||
pid = owner[idx]->pid;
|
||||
release(&pciuser_lock);
|
||||
return pid;
|
||||
}
|
||||
|
||||
// Who is holding what, and what memory they were given for it.
|
||||
//
|
||||
// The three things that go wrong with a userspace driver are all here: a
|
||||
// device claimed by a process that no longer exists, DMA handed out and never
|
||||
// returned, and an interrupt line left masked with nobody reading it. Each was
|
||||
// found the hard way, and each used to need a rebuild to see.
|
||||
static void
|
||||
pciuser_dump(struct sbuf *out, const char *arg)
|
||||
{
|
||||
(void)arg;
|
||||
int held = 0, blocks = 0;
|
||||
uint64 bytes = 0;
|
||||
|
||||
acquire(&pciuser_lock);
|
||||
|
||||
sb_puts(out, "claims\n");
|
||||
for (int i = 0; i < NPCI && i < pci_count(); i++) {
|
||||
const struct pci_dev *d;
|
||||
if (!owner[i])
|
||||
continue;
|
||||
held++;
|
||||
d = pci_get(i);
|
||||
sb_puts(out, " index ");
|
||||
sb_putu_pad(out, i, 2);
|
||||
sb_puts(out, " ");
|
||||
if (d) {
|
||||
sb_puthex(out, d->bus, 2); sb_putc(out, ':');
|
||||
sb_puthex(out, d->slot, 2); sb_putc(out, '.');
|
||||
sb_puthex(out, d->func, 1);
|
||||
sb_puts(out, " ");
|
||||
sb_puthex4(out, d->vendor); sb_putc(out, ':');
|
||||
sb_puthex4(out, d->device);
|
||||
}
|
||||
sb_puts(out, " held by pid ");
|
||||
sb_putu(out, owner[i]->pid);
|
||||
sb_puts(out, " (");
|
||||
sb_puts(out, owner[i]->name);
|
||||
sb_puts(out, ")\n");
|
||||
}
|
||||
if (!held)
|
||||
sb_puts(out, " (nothing is claimed)\n");
|
||||
|
||||
sb_puts(out, "\ndma handed out\n");
|
||||
for (int i = 0; i < PCIUSER_MAXDMA; i++) {
|
||||
if (!dmablk[i].proc)
|
||||
continue;
|
||||
blocks++;
|
||||
bytes += dmablk[i].size;
|
||||
sb_puts(out, " slot ");
|
||||
sb_putu_pad(out, i, 2);
|
||||
sb_puts(out, " 0x");
|
||||
sb_puthex(out, (addr_t)dmablk[i].kva, 16);
|
||||
sb_puts(out, " phys 0x");
|
||||
sb_puthex(out, dma_phys(dmablk[i].kva), 16);
|
||||
sb_putu_pad(out, dmablk[i].size, 10);
|
||||
sb_puts(out, " pid ");
|
||||
sb_putu(out, dmablk[i].proc->pid);
|
||||
sb_puts(out, " (");
|
||||
sb_puts(out, dmablk[i].proc->name);
|
||||
sb_puts(out, ")\n");
|
||||
}
|
||||
if (!blocks)
|
||||
sb_puts(out, " (none)\n");
|
||||
else {
|
||||
sb_puts(out, " ");
|
||||
sb_putu(out, blocks);
|
||||
sb_puts(out, " block(s), ");
|
||||
sb_putu(out, bytes / 1024);
|
||||
sb_puts(out, " KB of ");
|
||||
sb_putu(out, PCIUSER_MAXDMA);
|
||||
sb_puts(out, " slots\n");
|
||||
}
|
||||
|
||||
sb_puts(out, "\ninterrupts\n");
|
||||
if (!irqdev.proc) {
|
||||
sb_puts(out, " (no line is delivered to userspace)\n");
|
||||
} else {
|
||||
sb_puts(out, " irq ");
|
||||
sb_putu(out, irqdev.irq);
|
||||
sb_puts(out, " pid ");
|
||||
sb_putu(out, irqdev.proc->pid);
|
||||
sb_puts(out, " taken ");
|
||||
sb_putu(out, irqdev.count);
|
||||
sb_puts(out, ", seen ");
|
||||
sb_putu(out, irqdev.seen);
|
||||
sb_puts(out, irqdev.masked ? ", MASKED" : ", unmasked");
|
||||
if (irqdev.count != irqdev.seen)
|
||||
sb_puts(out, " -- the driver is behind");
|
||||
sb_putc(out, '\n');
|
||||
}
|
||||
|
||||
release(&pciuser_lock);
|
||||
}
|
||||
|
||||
void
|
||||
pciuser_init(void)
|
||||
{
|
||||
initlock(&pciuser_lock, "pciuser");
|
||||
dbg_register("claims", pciuser_dump,
|
||||
"devices, DMA and interrupts held by userspace drivers");
|
||||
}
|
||||
|
||||
// Does `phys` fall inside a memory BAR of a device this process has claimed?
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "types.h"
|
||||
#include "defs.h"
|
||||
#include "klog.h"
|
||||
#include "irq.h"
|
||||
#include "x86.h"
|
||||
#include "device.h"
|
||||
|
|
@ -34,77 +35,14 @@
|
|||
#include "errno.h"
|
||||
#include "fsstat.h"
|
||||
#include "schfs.h"
|
||||
#include "sbuf.h"
|
||||
#include "dbg.h"
|
||||
|
||||
#define PROC_BUFSZ 2048
|
||||
|
||||
// --- tiny formatting helpers (no snprintf in the kernel) -------------------
|
||||
|
||||
struct sbuf {
|
||||
char *p;
|
||||
int len;
|
||||
int cap;
|
||||
};
|
||||
|
||||
static void
|
||||
sb_putc(struct sbuf *sb, char c)
|
||||
{
|
||||
if (sb->len < sb->cap - 1)
|
||||
sb->p[sb->len++] = c;
|
||||
}
|
||||
|
||||
static void
|
||||
sb_puts(struct sbuf *sb, const char *s)
|
||||
{
|
||||
while (*s)
|
||||
sb_putc(sb, *s++);
|
||||
}
|
||||
|
||||
// A string in a fixed-width column, space-padded. cprintf has no width
|
||||
// specifier and neither does this buffer, so tables build their own columns.
|
||||
// Over-long values are allowed to run past their column rather than be
|
||||
// truncated: a misaligned row is easier to read than a mangled name.
|
||||
static void
|
||||
sb_pad(struct sbuf *sb, const char *s, int width)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n]) {
|
||||
sb_putc(sb, s[n]);
|
||||
n++;
|
||||
}
|
||||
while (n++ < width)
|
||||
sb_putc(sb, ' ');
|
||||
}
|
||||
|
||||
// Four lowercase hex digits, zero-padded. PCI and USB identifiers are always
|
||||
// written this way, and a lookup table keyed on "8086:100e" does not match
|
||||
// "8086:100E" or "8086:10e".
|
||||
static void
|
||||
sb_puthex4(struct sbuf *sb, uint32 v)
|
||||
{
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
for (int i = 3; i >= 0; i--)
|
||||
sb_putc(sb, hex[(v >> (i * 4)) & 0xF]);
|
||||
}
|
||||
|
||||
static void
|
||||
sb_putu(struct sbuf *sb, uint64 v)
|
||||
{
|
||||
char tmp[24];
|
||||
int i = 0;
|
||||
if (v == 0) { sb_putc(sb, '0'); return; }
|
||||
while (v && i < (int)sizeof(tmp)) { tmp[i++] = '0' + (v % 10); v /= 10; }
|
||||
while (i--) sb_putc(sb, tmp[i]);
|
||||
}
|
||||
|
||||
// "<label><value> <unit>\n" — the shape almost every /proc line takes.
|
||||
static void
|
||||
sb_kv(struct sbuf *sb, const char *label, uint64 v, const char *unit)
|
||||
{
|
||||
sb_puts(sb, label);
|
||||
sb_putu(sb, v);
|
||||
if (unit && *unit) { sb_putc(sb, ' '); sb_puts(sb, unit); }
|
||||
sb_putc(sb, '\n');
|
||||
}
|
||||
// The formatting helpers these files are built out of now live in
|
||||
// include/sbuf.h, because the debugger's introspection registry needs exactly
|
||||
// the same thing and two copies would have drifted. See docs/plan/debugger.md.
|
||||
|
||||
// --- the files -------------------------------------------------------------
|
||||
|
||||
|
|
@ -403,6 +341,7 @@ gen_filesystems(struct sbuf *sb)
|
|||
#define PROC_DEVICES 13
|
||||
#define PROC_FSSTAT 14
|
||||
#define PROC_NETIF 15
|
||||
#define PROC_KMSG 17
|
||||
|
||||
// Render global file `id` (a PROC_* constant) into `buf`. Returns its length.
|
||||
//
|
||||
|
|
@ -446,6 +385,13 @@ proc_gen_global(int id, char *buf, int cap)
|
|||
int
|
||||
procread(struct inode *ip, uint64 off, char *dst, int n)
|
||||
{
|
||||
// The log is served straight out of its ring rather than regenerated into a
|
||||
// scratch buffer like everything else here: it is 128 KB, and this function
|
||||
// renders the whole file on every read into an array on a 4 KB kernel stack.
|
||||
// See klog_read().
|
||||
if (ip->minor == PROC_KMSG)
|
||||
return klog_read(off, dst, n);
|
||||
|
||||
char buf[PROC_BUFSZ];
|
||||
int len = proc_gen_global(ip->minor, buf, PROC_BUFSZ);
|
||||
|
||||
|
|
@ -502,6 +448,16 @@ proc_sysrq_cmd(char cmd)
|
|||
cprintf("sysrq: pid 1 asked to exit -- it should come back\n");
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
// Every registered view, straight to the console.
|
||||
//
|
||||
// This is docs/plan/debugger.md's level 0: no process, no filesystem, no
|
||||
// client program. If /dev/dbg cannot be opened -- because the filesystem
|
||||
// is what is broken -- this still works, and on a machine with only a
|
||||
// serial line it is the whole debugger.
|
||||
dbg_dump_all();
|
||||
break;
|
||||
|
||||
case 'w': {
|
||||
// Wedge the supervisor: make schinitd hang, on purpose, so PID 1's
|
||||
// heartbeat timeout is exercised instead of reasoned about. There is no
|
||||
|
|
@ -518,6 +474,7 @@ proc_sysrq_cmd(char cmd)
|
|||
cprintf("sysrq: unknown command '%c' "
|
||||
"(known: c = crash, u = dump USB, s = read the USB disk, "
|
||||
"d = display self-test, t = text self-test, n = net self-test, "
|
||||
"g = dump every debug view, "
|
||||
"w = wedge the init supervisor, i = kill pid 1)\n",
|
||||
cmd);
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in a new issue