diff options
author | Ian Moffett <ian@osmora.org> | 2025-07-05 04:15:20 +0000 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-07-05 04:15:20 +0000 |
commit | 88285ca7d9ac11c99b01a44d3525acb82d35e1de (patch) | |
tree | cd21b0bf1e0e8e226925cf12b66da13032668ab9 /sys/kern/kern_proc.c | |
parent | 68e404c22776f547158aa8f1a88f29c757167591 (diff) |
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 <ian@osmora.org>
Diffstat (limited to 'sys/kern/kern_proc.c')
-rw-r--r-- | sys/kern/kern_proc.c | 39 |
1 files changed, 39 insertions, 0 deletions
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 <sys/types.h> #include <sys/proc.h> +#include <sys/cdefs.h> +#include <sys/vnode.h> #include <sys/syscall.h> +#include <sys/filedesc.h> +#include <sys/fcntl.h> +#include <string.h> +#include <crc32.h> 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) { |