From 88285ca7d9ac11c99b01a44d3525acb82d35e1de Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 5 Jul 2025 04:15:20 +0000 Subject: kernel/amd64: Add support for coredumps To make debugging userland program crashes easier, this commit introduces coredumps that store the last known process state into a temporary /tmp/core.X file (where X is the PID of the faulting process). Signed-off-by: Ian Moffett --- sys/kern/kern_proc.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'sys/kern/kern_proc.c') diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 3cf2af8..16cd4b2 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -29,7 +29,13 @@ #include #include +#include +#include #include +#include +#include +#include +#include pid_t getpid(void) @@ -61,6 +67,39 @@ getppid(void) return td->parent->pid; } +void +proc_coredump(struct proc *td, uintptr_t fault_addr) +{ + struct coredump core; + struct sio_txn sio; + struct vnode *vp; + char pathname[128]; + int fd; + + snprintf(pathname, sizeof(pathname), "/tmp/core.%d", td->pid); + fd = fd_open(pathname, O_RDWR | O_CREAT); + + /* ... Hopefully not */ + if (__unlikely(fd < 0)) { + return; + } + + core.pid = td->pid; + core.fault_addr = fault_addr; + memcpy(&core.tf, &td->tf, sizeof(td->tf)); + + core.checksum = crc32(&core, sizeof(core) - sizeof(core.checksum)); + vp = fd_get(fd)->vp; + + sio.buf = &core; + sio.len = sizeof(core); + sio.offset = 0; + + /* Write the core file */ + vfs_vop_write(vp, &sio); + fd_close(fd); +} + scret_t sys_getpid(struct syscall_args *scargs) { -- cgit v1.2.3