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
145 lines
4.7 KiB
Perl
145 lines
4.7 KiB
Perl
#!/usr/bin/perl
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
# Turn `nm -n kernel` into a symbol table the kernel can carry with it.
|
|
#
|
|
# docs/plan/debugger.md milestone D0. Everything the debugger prints is an
|
|
# address until this exists, and an address has to be resolved by hand against
|
|
# a disassembly that may not be the one that was running.
|
|
#
|
|
# Perl and not Python, deliberately: this runs on every kernel link, so it is
|
|
# on the critical path of a clean build, and tools/vectors.pl already makes
|
|
# perl a requirement there. The Makefile comment above include/logo.h says why
|
|
# that line is worth holding.
|
|
#
|
|
# Two modes:
|
|
# (default) read nm output on stdin, write a C file on stdout
|
|
# --list read nm output on stdin, write "addr name" lines -- used by the
|
|
# Makefile to prove the two link passes agree
|
|
#
|
|
# The layout trick this depends on: .ksym is placed after .text and .rodata in
|
|
# kernel/kernel.ld, so adding the table to the image cannot move any of the
|
|
# text symbols that are *in* the table. Two passes, and the second is stable.
|
|
# The Makefile verifies that rather than trusting it.
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
# hex() on a 64-bit kernel address is fine on any perl with 64-bit integers,
|
|
# which is every perl this will meet; the warning is about 32-bit ones.
|
|
no warnings q(portable);
|
|
|
|
my $list = (@ARGV && $ARGV[0] eq '--list');
|
|
|
|
my @sym; # [addr, name]
|
|
my $textend = 0;
|
|
|
|
while (my $line = <STDIN>) {
|
|
chomp $line;
|
|
# "ffff800000100000 T begin". Symbols with no address print as spaces and
|
|
# fall out here on the field count.
|
|
my ($addr, $type, $name) = $line =~ /^([0-9a-fA-F]+)\s+(\S)\s+(\S+)$/
|
|
or next;
|
|
|
|
# etext is the end of the text section, and it is the only non-function we
|
|
# want: without it a lookup of an address past the last function walks off
|
|
# the end of the table and names whatever was linked last.
|
|
if ($name eq 'etext') {
|
|
$textend = hex($addr);
|
|
next;
|
|
}
|
|
|
|
# t/T are text, w/W are weak text. Everything else is data, bss or absolute
|
|
# and naming it would make a code address resolve to a variable.
|
|
next unless $type =~ /^[tTwW]$/;
|
|
|
|
# Compiler-local labels. They have no meaning outside one translation unit
|
|
# and they would shadow the function they sit inside.
|
|
next if $name =~ /^\.L/;
|
|
next if $name =~ /\$/;
|
|
|
|
push @sym, [hex($addr), $name, $type];
|
|
}
|
|
|
|
die "ksyms: no text symbols on stdin\n" unless @sym;
|
|
|
|
# nm -n sorts numerically, but ties come out in whatever order the symbol table
|
|
# had them. Prefer the global at a shared address: `begin` and `mboot_header`
|
|
# are the same byte, and naming that byte `begin` tells you nothing.
|
|
my @uniq;
|
|
for my $s (@sym) {
|
|
if (@uniq && $uniq[-1][0] == $s->[0]) {
|
|
$uniq[-1] = $s if $s->[2] =~ /^[TW]$/ && $uniq[-1][2] =~ /^[tw]$/;
|
|
next;
|
|
}
|
|
push @uniq, $s;
|
|
}
|
|
@sym = @uniq;
|
|
|
|
$textend = $sym[-1][0] + 0x1000 unless $textend;
|
|
|
|
if ($list) {
|
|
printf "%016x %s\n", $_->[0], $_->[1] for @sym;
|
|
exit 0;
|
|
}
|
|
|
|
my $base = $sym[0][0];
|
|
for my $s (@sym) {
|
|
die sprintf("ksyms: symbol %s at %x is more than 4 GB above the base\n",
|
|
$s->[1], $s->[0])
|
|
if $s->[0] - $base > 0xffffffff;
|
|
}
|
|
|
|
# Offsets into the name blob, computed here so the kernel does not have to
|
|
# scan for NULs at boot.
|
|
my @off;
|
|
my $cur = 0;
|
|
for my $s (@sym) {
|
|
push @off, $cur;
|
|
$cur += length($s->[1]) + 1;
|
|
}
|
|
|
|
my $n = scalar @sym;
|
|
|
|
print <<"HDR";
|
|
// Generated by tools/ksyms.pl from the first link pass. Do not edit.
|
|
//
|
|
// See docs/plan/debugger.md, milestone D0, and kernel/core/ksym.c for the
|
|
// lookup that reads this. Everything lands in .ksym, which kernel/kernel.ld
|
|
// places after .text and .rodata so that carrying the table cannot move the
|
|
// addresses recorded in it.
|
|
|
|
#include "types.h"
|
|
|
|
#define KSYM_SECTION __attribute__((section(".ksym"), used))
|
|
|
|
KSYM_SECTION const uint32 ksym_count = ${n}u;
|
|
KSYM_SECTION const uint64 ksym_base = 0x${\ sprintf("%x", $base) }ULL;
|
|
KSYM_SECTION const uint64 ksym_textend = 0x${\ sprintf("%x", $textend) }ULL;
|
|
|
|
// Offsets from ksym_base rather than absolute addresses: the kernel spans
|
|
// about a megabyte, so 32 bits is plenty and it halves the table.
|
|
KSYM_SECTION const uint32 ksym_addr[${n}] = {
|
|
HDR
|
|
|
|
for (my $i = 0; $i < $n; $i += 8) {
|
|
my $end = $i + 7; $end = $n - 1 if $end > $n - 1;
|
|
print " ", join(",", map { sprintf("0x%08x", $sym[$_][0] - $base) } ($i .. $end)), ",\n";
|
|
}
|
|
|
|
print "};\n\nKSYM_SECTION const uint32 ksym_nameoff[${n}] = {\n";
|
|
|
|
for (my $i = 0; $i < $n; $i += 8) {
|
|
my $end = $i + 7; $end = $n - 1 if $end > $n - 1;
|
|
print " ", join(",", map { sprintf("%6d", $off[$_]) } ($i .. $end)), ",\n";
|
|
}
|
|
|
|
print "};\n\nKSYM_SECTION const char ksym_names[] =\n";
|
|
|
|
for my $s (@sym) {
|
|
my $name = $s->[1];
|
|
$name =~ s/([\\"])/\\$1/g;
|
|
print " \"$name\\0\"\n";
|
|
}
|
|
|
|
print ";\n";
|