SchOS/tools/ksyms.pl
Nexoniarz bc39b73969 debugger: D5, the network transport -- and the plan closes
schdbgd bridges TCP to the debugger, which is the one way in that does not
need you to be sitting at the machine. It is a bridge and nothing else: a line
in becomes a line written to /dev/dbgro and the answer comes back, so `nc` is a
complete client and there is no clever client to be broken on the day you need
this.

    # schdbgd &
    schdbgd: debugger on port 4444, loopback only
    $ nc 127.0.0.1 4444
    dma
    arena  0xffff80003da41000-0xffff80003ea41000  16384 KB total, ...

A kernel debugger plus a TCP socket is a remote root hole, so three things were
decided rather than discovered:

  - It does not start by itself. schdbgd.job ships Disabled. SchOS has no
    kernel command line, so the boot flag the plan asked for is the job
    manifest, which is the same thing in this system's own idiom.
  - It binds loopback; -a names an address and says so in a paragraph.
  - It cannot reach a view that changes state, enforced in the kernel.

That last one is /dev/dbgro: /dev/dbg on minor 1. The minor is the one piece of
per-open state a character device already sees, so which node you opened is the
permission -- nothing to latch, nothing to remember between the write and the
read, no way across. Which views are refused comes from dbg_register_rw() on
the registration itself, because a central list of dangerous views goes stale
in the direction where a new view is quietly treated as safe. Today that is
`watch <addr>`; `watch` with no argument is still the listing and still
answered.

Also: socket() now accepts IPPROTO_TCP with SOCK_STREAM and IPPROTO_UDP with
SOCK_DGRAM. They name the default rather than departing from it, and toybox's
nc writes them out -- so TCP was "Protocol not supported" on a system whose
whole network is TCP.

docs/plan/debugger.md retires into docs/architecture/debugging.md; 16 files
repointed. Every plan in docs/plan/ that was open is now closed, so the next
thing is Gate 0 itself: the survey, with the instrument built.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-14 15:33:53 +02:00

145 lines
4.8 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/architecture/debugging.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/architecture/debugging.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";