From 1ed4b5788204ebbbde9b8b089b2991de7563a353 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 1 Oct 2025 02:48:32 +0200 Subject: [PATCH] hexdump.[ch]: utility function for dumping memory regions as hex --- Makefile | 3 ++- kern/libkern/hexdump.c | 34 ++++++++++++++++++++++++++++++++++ kern/libkern/hexdump.h | 8 ++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 kern/libkern/hexdump.c create mode 100644 kern/libkern/hexdump.h diff --git a/Makefile b/Makefile index 29aa115..5622f00 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,8 @@ KERNEL_OBJ := \ kern/libkern/mini-printf.o \ kern/libkern/stdio.o \ kern/libkern/buddy.o \ - kern/libkern/badrand.o + kern/libkern/badrand.o \ + kern/libkern/hexdump.o kern/kernel.elf: $(KERNEL_OBJ) @echo LD $@ diff --git a/kern/libkern/hexdump.c b/kern/libkern/hexdump.c new file mode 100644 index 0000000..772095c --- /dev/null +++ b/kern/libkern/hexdump.c @@ -0,0 +1,34 @@ +#include +#include +#include + +void hexdump(const void *data, size_t size) { + const unsigned char *p = (const unsigned char *)data; + size_t i, j; + + for (i = 0; i < size; i += 16) { + // Print offset + kprintf("%08zx ", i); + + // Print hex bytes + for (j = 0; j < 16; j++) { + if (i + j < size) { + kprintf("%02X ", p[i + j]); + } else { + kprintf(" "); // padding for incomplete lines + } + if (j == 7) + kprintf(" "); // extra space in middle + } + + kprintf(" |"); + + // Print ASCII characters + for (j = 0; j < 16 && i + j < size; j++) { + unsigned char c = p[i + j]; + kprintf("%c", isprint(c) ? c : '.'); + } + + kprintf("|\n"); + } +} diff --git a/kern/libkern/hexdump.h b/kern/libkern/hexdump.h new file mode 100644 index 0000000..d01a907 --- /dev/null +++ b/kern/libkern/hexdump.h @@ -0,0 +1,8 @@ +#ifndef HEXDUMP_H +#define HEXDUMP_H + +#include + +void hexdump(const void *data, size_t size); + +#endif // HEXDUMP_H