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
165 lines
4.9 KiB
C
165 lines
4.9 KiB
C
#include "types.h"
|
|
#include "defs.h"
|
|
#include "irq.h"
|
|
#include "param.h"
|
|
#include "memlayout.h"
|
|
#include "mmu.h"
|
|
#include "proc.h"
|
|
#include "x86.h"
|
|
#include "traps.h"
|
|
#include "spinlock.h"
|
|
#include "posix.h"
|
|
|
|
uint *idt;
|
|
extern addr_t vectors[];
|
|
struct spinlock tickslock;
|
|
uint ticks;
|
|
|
|
static void
|
|
mkgate(uint *idt, uint n, addr_t kva, uint pl)
|
|
{
|
|
uint64 addr = (uint64) kva;
|
|
|
|
n *= 4;
|
|
idt[n+0] = (addr & 0xFFFF) | (KERNEL_CS << 16);
|
|
idt[n+1] = (addr & 0xFFFF0000) | 0x8E00 | ((pl & 3) << 13);
|
|
idt[n+2] = addr >> 32;
|
|
idt[n+3] = 0;
|
|
}
|
|
|
|
void idtinit(void)
|
|
{
|
|
struct {
|
|
ushort limit;
|
|
uint64 base;
|
|
} __attribute__((packed)) idtdesc;
|
|
|
|
idtdesc.limit = PGSIZE - 1;
|
|
idtdesc.base = (uint64)idt;
|
|
|
|
asm volatile("lidt %0" : : "m" (idtdesc));
|
|
}
|
|
|
|
void tvinit(void)
|
|
{
|
|
int n;
|
|
idt = (uint*) kalloc();
|
|
memset(idt, 0, PGSIZE);
|
|
for (n = 0; n < 256; n++)
|
|
mkgate(idt, n, vectors[n], 0);
|
|
mkgate(idt, T_SYSCALL, vectors[T_SYSCALL], 3);
|
|
}
|
|
|
|
uint
|
|
uptime(void)
|
|
{
|
|
uint xticks;
|
|
|
|
acquire(&tickslock);
|
|
xticks = ticks;
|
|
release(&tickslock);
|
|
return xticks;
|
|
}
|
|
|
|
// Which signal a CPU fault corresponds to, so a process killed by a fault is
|
|
// reported to wait() as died-by-signal with a sensible cause rather than a
|
|
// blanket SIGKILL.
|
|
static int
|
|
trapsig(uint64 trapno)
|
|
{
|
|
switch(trapno){
|
|
case T_DIVIDE: return SIGFPE;
|
|
case T_ILLOP: return SIGILL;
|
|
case T_GPFLT: return SIGSEGV;
|
|
case T_PGFLT: return SIGSEGV;
|
|
case T_BRKPT: return SIGTRAP;
|
|
case T_DEBUG: return SIGTRAP;
|
|
default: return SIGSEGV;
|
|
}
|
|
}
|
|
|
|
void
|
|
trap(struct trapframe *tf)
|
|
{
|
|
kstack_check_canary("trap entry");
|
|
switch(tf->trapno){
|
|
case T_IRQ0 + IRQ_TIMER:
|
|
if(cpunum() == 0){
|
|
acquire(&tickslock);
|
|
ticks++;
|
|
wakeup(&ticks);
|
|
release(&tickslock);
|
|
}
|
|
lapiceoi();
|
|
break;
|
|
case T_IRQ0 + IRQ_SPURIOUS:
|
|
cprintf("cpu%d: spurious interrupt at %p:%p\n",
|
|
cpunum(), tf->cs, tf->rip);
|
|
lapiceoi();
|
|
break;
|
|
|
|
default:
|
|
// Every other IOAPIC pin goes through the registration table. The device
|
|
// interrupts used to be listed here one case at a time, which cannot work
|
|
// for anything found on the PCI bus: a driver does not know its line
|
|
// until it reads the device's config space at probe time, so there is no
|
|
// constant to write a case label for. See include/irq.h.
|
|
if(tf->trapno >= T_IRQ0 && tf->trapno < T_IRQ0 + NIRQ){
|
|
irq_dispatch(tf->trapno - T_IRQ0);
|
|
lapiceoi();
|
|
break;
|
|
}
|
|
|
|
if(myproc() == 0 || (tf->cs&3) == 0){
|
|
// The rip is named where it can be. This is a kernel address, so
|
|
// ksym_lookup() has something to say about it -- and the whole reason
|
|
// docs/plan/debugger.md puts D0 first is that "rip ffff8000001294c2"
|
|
// costs an objdump session and "rip in dma_alloc+0x8c" does not.
|
|
char where[72];
|
|
ksym_fmt(where, sizeof(where), tf->rip);
|
|
cprintf("unexpected trap %d from cpu %d rip %p <%s> (cr2=0x%p) err=0x%p\n",
|
|
tf->trapno, cpunum(), tf->rip, where, rcr2(), tf->err);
|
|
if (myproc())
|
|
cprintf("proc id: %d\n", myproc()->pid);
|
|
if (tf->trapno == T_GPFLT) {
|
|
uint64 *iretframe = (uint64*)tf->rsp;
|
|
cprintf("iretq frame @ %p: rip=%p cs=%p rflags=%p rsp=%p ss=%p\n",
|
|
iretframe, iretframe[0], iretframe[1], iretframe[2],
|
|
iretframe[3], iretframe[4]);
|
|
if (myproc())
|
|
cprintf("this tf=%p myproc()->tf=%p myproc()->kstack=%p (tf-kstack=%p)\n",
|
|
tf, myproc()->tf, myproc()->kstack, (uint64)tf - (uint64)myproc()->kstack);
|
|
}
|
|
panic("trap");
|
|
}
|
|
cprintf("pid %d %s: trap %d err %d on cpu %d "
|
|
"rip 0x%p addr 0x%p--kill proc\n",
|
|
myproc()->pid, myproc()->name, tf->trapno, tf->err, cpunum(), tf->rip,
|
|
rcr2());
|
|
// General-purpose register dump. This used to be followed by ~25 lines
|
|
// that walked musl's mallocng group/meta structures out of rax/rdi and
|
|
// dumped the user stack -- scaffolding for the boot-crash investigation
|
|
// recorded in docs/history/kernel.md section 1. That
|
|
// investigation is closed and the underlying kernel bugs are fixed, so
|
|
// the allocator-specific part is gone; the registers stay, since they are
|
|
// useful for any fault.
|
|
if (tf->trapno == T_GPFLT) {
|
|
cprintf(" rax=%p rbx=%p rcx=%p rdx=%p rsi=%p rdi=%p rbp=%p rsp=%p\n",
|
|
tf->rax, tf->rbx, tf->rcx, tf->rdx, tf->rsi, tf->rdi, tf->rbp, tf->rsp);
|
|
}
|
|
myproc()->killed = 1;
|
|
if(myproc()->killsig == 0)
|
|
myproc()->killsig = trapsig(tf->trapno);
|
|
}
|
|
|
|
if(myproc() && myproc()->killed && (tf->cs&3) == DPL_USER)
|
|
exit(W_EXITCODE(0, myproc()->killsig ? myproc()->killsig : SIGKILL));
|
|
|
|
if(myproc() && myproc()->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER)
|
|
yield();
|
|
|
|
if(myproc() && myproc()->killed && (tf->cs&3) == DPL_USER)
|
|
exit(W_EXITCODE(0, myproc()->killsig ? myproc()->killsig : SIGKILL));
|
|
|
|
kstack_check_canary("trap exit");
|
|
}
|