mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 12:55:57 +00:00
Report host registers, code bytes and memory at a crash
The crash report named the lifted function and printed the guest registers, but the lifted code keeps guest registers in host locals and writes them back only at calls, so the dump showed stale values and the faulting instruction stayed unknown. Logs from #1005, #967 and #998 could not be traced further than the function. The report now adds the x86 registers of the faulting thread (Windows and Linux), 64 bytes of machine code around rip with the offset into the lifted function, so the instruction can be found in a local build of the same version, 64 bytes of memory around every register value that points at readable memory, and the first 128 bytes of the guest stack. --probe-kernel checks the new lines with a child that faults with known values in rbx and x19. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e440be7ca
commit
06d70d0dfa
5 files changed
+149
-5
No files matched your search
+83
-5
@@ -21,7 +21,8 @@
|
||||
//
|
||||
// Also here: the runtime services the generated code calls besides rt_dispatch and rt_svc (rt_brk, rt_udf,
|
||||
// rt_ticks, the function profiler hook), logging, SHA-256, and the crash reporter (a vectored exception handler that
|
||||
// names the lifted function, the guest registers and the guest frame-pointer chain).
|
||||
// names the lifted function, the host and guest registers, the machine code around the fault, the guest frame-pointer
|
||||
// chain and the memory the registers point at).
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -188,8 +189,8 @@ static int by_host(const void *a, const void *b) {
|
||||
}
|
||||
|
||||
// The lifted function that contains a host code address, by the nearest function start below it; 0 outside the lifted
|
||||
// code.
|
||||
static uint64_t lifted_function(uintptr_t rip) {
|
||||
// code. *host, when given, gets the function's host start.
|
||||
static uint64_t lifted_function_at(uintptr_t rip, uintptr_t *host) {
|
||||
if (!fn_rev) {
|
||||
static RtModule mods[MAX_MODULES];
|
||||
int n = rt_linked_modules(mods, MAX_MODULES);
|
||||
@@ -211,11 +212,78 @@ static uint64_t lifted_function(uintptr_t rip) {
|
||||
if (fn_rev[mid].host <= rip) lo = mid + 1;
|
||||
else hi = mid;
|
||||
}
|
||||
if (lo && host) *host = fn_rev[lo - 1].host;
|
||||
return lo ? fn_rev[lo - 1].guest : 0;
|
||||
}
|
||||
|
||||
static uint64_t lifted_function(uintptr_t rip) { return lifted_function_at(rip, NULL); }
|
||||
|
||||
static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); }
|
||||
|
||||
// The machine code around the faulting instruction: 32 bytes before rip, then 32 from rip. The user's totk.exe is built
|
||||
// on their machine, so its offsets need not match ours; these bytes and the offset into the lifted function find the
|
||||
// same instruction in a local build of that version, and with it the guest instruction and the operands' registers.
|
||||
static void crash_code_bytes(uintptr_t rip) {
|
||||
uint8_t b[64];
|
||||
int before = 32;
|
||||
if (!plat_mem_read(rip - 32, b, sizeof b)) {
|
||||
before = 0; // rip near the start of a mapping: the bytes from rip only
|
||||
if (!plat_mem_read(rip, b + 32, 32)) return;
|
||||
}
|
||||
rt_logf(" host code at rip-%d:", before);
|
||||
for (int i = 32 - before; i < 64; i++) rt_logf("%s%02x", i == 32 ? " | " : " ", b[i]);
|
||||
rt_log_raw("\n", 1);
|
||||
}
|
||||
|
||||
// 64 bytes around each register value that points at readable memory, as 8-byte words from 16 bytes below the value,
|
||||
// the word holding the value marked with *. Host and guest registers that land on the same window share a line. A
|
||||
// guest pointer is a host address here (the guest address space is mapped one to one), so both kinds are read alike.
|
||||
// The guest registers come from the Cpu, which the lifted code writes back only at calls and returns, so the host
|
||||
// registers carry the faulting function's live values.
|
||||
typedef struct { uint64_t start; char names[56]; uint64_t w[8]; } CrashWin;
|
||||
|
||||
static void crash_win_add(CrashWin *wins, int *n, int cap, const char *name, uint64_t v) {
|
||||
if (v < 0x10000 || v > 0x00007fffffffffffull) return; // small integers and non-canonical values
|
||||
uint64_t start = (v & ~7ull) - 16;
|
||||
for (int i = 0; i < *n; i++)
|
||||
if (wins[i].start == start) {
|
||||
size_t len = strlen(wins[i].names);
|
||||
if (len + strlen(name) + 2 < sizeof wins[i].names)
|
||||
snprintf(wins[i].names + len, sizeof wins[i].names - len, " %s", name);
|
||||
return;
|
||||
}
|
||||
if (*n == cap || !plat_mem_read(start, wins[*n].w, sizeof wins[*n].w)) return;
|
||||
wins[*n].start = start;
|
||||
snprintf(wins[*n].names, sizeof wins[*n].names, "%s", name);
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
static void crash_memory(const PlatCrash *x, const Cpu *c) {
|
||||
static CrashWin wins[64]; // the crash handler runs once (busy flag), so static storage needs no allocation
|
||||
static const char *const gpr_names[16] = PLAT_GPR_NAMES;
|
||||
int n = 0;
|
||||
char name[8];
|
||||
if (x->has_regs)
|
||||
for (int i = 0; i < 16; i++) crash_win_add(wins, &n, 64, gpr_names[i], x->gpr[i]);
|
||||
if (c) {
|
||||
for (int i = 0; i < 31; i++) snprintf(name, sizeof name, "x%d", i), crash_win_add(wins, &n, 64, name, c->x[i]);
|
||||
crash_win_add(wins, &n, 64, "sp", c->sp);
|
||||
}
|
||||
if (!n) return;
|
||||
rt_logf(" memory at the register values (8-byte words from value-16, * the value's word):\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
rt_logf(" %016llx %-12s", (unsigned long long)wins[i].start, wins[i].names);
|
||||
for (int k = 0; k < 8; k++) rt_logf(" %s%016llx", k == 2 ? "*" : "", (unsigned long long)wins[i].w[k]);
|
||||
rt_log_raw("\n", 1);
|
||||
}
|
||||
// The guest stack above sp: the crashing function's spills and locals, and the callers' saved registers
|
||||
uint64_t s[16];
|
||||
if (c && plat_mem_read(c->sp, s, sizeof s))
|
||||
for (int i = 0; i < 16; i += 4)
|
||||
rt_logf(" guest stack sp+0x%02x: %016llx %016llx %016llx %016llx\n", i * 8, (unsigned long long)s[i],
|
||||
(unsigned long long)s[i + 1], (unsigned long long)s[i + 2], (unsigned long long)s[i + 3]);
|
||||
}
|
||||
|
||||
static void crash_handler(const PlatCrash *x) {
|
||||
static volatile int32_t busy;
|
||||
if (plat_xchg32(&busy, 1)) plat_exit(71);
|
||||
@@ -230,8 +298,17 @@ static void crash_handler(const PlatCrash *x) {
|
||||
rt_log_raw("\n", 1);
|
||||
if (x->cxx_type)
|
||||
rt_logf(" C++ type %s%s%s\n", x->cxx_type, x->cxx_what ? ", what(): " : "", x->cxx_what ? x->cxx_what : "");
|
||||
uint64_t f = x->module ? 0 : lifted_function(rip);
|
||||
if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d));
|
||||
uintptr_t fstart = 0;
|
||||
uint64_t f = x->module ? 0 : lifted_function_at(rip, &fstart);
|
||||
if (f)
|
||||
rt_logf(" in lifted function f_%llx = %s, host +%#llx into it\n", (unsigned long long)f,
|
||||
ldr_describe(f, d, sizeof d), (unsigned long long)(rip - fstart));
|
||||
if (x->has_regs) {
|
||||
static const char *const gpr_names[16] = PLAT_GPR_NAMES;
|
||||
for (int i = 0; i < 16; i++)
|
||||
rt_logf(" %-3s %016llx%s", gpr_names[i], (unsigned long long)x->gpr[i], i % 4 == 3 ? "\n" : "");
|
||||
crash_code_bytes(rip);
|
||||
}
|
||||
// abort and invalid parameter: where the host code called it; a fault in another module: the driver code and the
|
||||
// host calls that led into it
|
||||
uintptr_t exe = plat_exe_base();
|
||||
@@ -266,6 +343,7 @@ static void crash_handler(const PlatCrash *x) {
|
||||
} else {
|
||||
rt_logf(" not on a guest thread\n");
|
||||
}
|
||||
crash_memory(x, c);
|
||||
kern_free_cores_note();
|
||||
fflush(stderr);
|
||||
plat_exit(71);
|
||||
|
||||
Reference in new issue
Block a user