]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
Feat: Debugging improvements (Detailed Panic + Heap Protection)
authortadryanom_bot <[email protected]>
Tue, 3 Feb 2026 15:17:25 +0000 (15:17 +0000)
committertadryanom_bot <[email protected]>
Tue, 3 Feb 2026 15:17:25 +0000 (15:17 +0000)
include/utils.h
src/arch/x86/idt.c
src/kernel/utils.c
src/mm/heap.c

index c48a904be57273d6183670d79239f768dbf357f5..638a62de501966244bdc20e3ef57f5a751bb1f47 100644 (file)
@@ -16,5 +16,7 @@ void reverse(char* str, int length);
 void itoa(int num, char* str, int base);
 // ASCII to Integer
 int atoi(const char* str);
+// Hex dumper
+void itoa_hex(uint32_t num, char* str);
 
 #endif
index 7217ae281f6a05d3dde5d2a9490eb1bed005c74c..8bef4bcae60d77a2d703abfac27095892692235f 100644 (file)
@@ -142,6 +142,19 @@ void register_interrupt_handler(uint8_t n, isr_handler_t handler) {
     interrupt_handlers[n] = handler;
 }
 
+#include "utils.h"
+
+// ... imports ...
+
+void print_reg(const char* name, uint32_t val) {
+    char buf[16];
+    uart_print(name);
+    uart_print(": ");
+    itoa_hex(val, buf);
+    uart_print(buf);
+    uart_print("  ");
+}
+
 // The Main Handler called by assembly
 void isr_handler(struct registers* regs) {
     // Check if we have a custom handler
@@ -151,18 +164,35 @@ void isr_handler(struct registers* regs) {
     } else {
         // If Exception (0-31), Panic
         if (regs->int_no < 32) {
-            uart_print("\n[PANIC] Unhandled Exception: ");
-            // TODO: Print int_no hex
-            uart_print(" (Halting)\n");
+            __asm__ volatile("cli"); // Stop everything
+            
+            uart_print("\n\n!!! KERNEL PANIC !!!\n");
+            uart_print("Exception Number: ");
+            char num_buf[16];
+            itoa(regs->int_no, num_buf, 10);
+            uart_print(num_buf);
+            uart_print("\n");
             
+            uart_print("registers:\n");
+            print_reg("EAX", regs->eax); print_reg("EBX", regs->ebx); print_reg("ECX", regs->ecx); print_reg("EDX", regs->edx);
+            uart_print("\n");
+            print_reg("ESI", regs->esi); print_reg("EDI", regs->edi); print_reg("EBP", regs->ebp); print_reg("ESP", regs->esp);
+            uart_print("\n");
+            print_reg("EIP", regs->eip); print_reg("CS ", regs->cs);  print_reg("EFLAGS", regs->eflags);
+            uart_print("\n");
+
             // Print Page Fault specifics
             if (regs->int_no == 14) {
-                uart_print("Page Fault! Address: ");
                 uint32_t cr2;
                 __asm__ volatile("mov %%cr2, %0" : "=r"(cr2));
-                // Print CR2
+                uart_print("\nPAGE FAULT at address: ");
+                char cr2_buf[16];
+                itoa_hex(cr2, cr2_buf);
+                uart_print(cr2_buf);
+                uart_print("\n");
             }
             
+            uart_print("\nSystem Halted.\n");
             for(;;) __asm__("hlt");
         }
     }
index 3f8c63fa281f3dcb6c9ad37bd805a5d148c19ebb..cbf3370115ae93c688f5fe6e7e7675c92e67727f 100644 (file)
@@ -97,3 +97,15 @@ int atoi(const char* str) {
   
     return sign * res;
 }
+
+void itoa_hex(uint32_t num, char* str) {
+    const char hex_chars[] = "0123456789ABCDEF";
+    str[0] = '0';
+    str[1] = 'x';
+    
+    for (int i = 0; i < 8; i++) {
+        str[9 - i] = hex_chars[num & 0xF];
+        num >>= 4;
+    }
+    str[10] = '\0';
+}
index ed7bf9b277300e8a61f3e2c000fe1edbe6368199..0ea0ee32727fbfe8d606a3adf0d512d2327b2894 100644 (file)
@@ -8,16 +8,45 @@
 #define KHEAP_INITIAL_SIZE (10 * 1024 * 1024) // 10MB
 #define PAGE_SIZE 4096
 
+#define HEAP_MAGIC 0xCAFEBABE
+
 // Advanced Header: Doubly Linked List
 typedef struct heap_header {
+    uint32_t magic;           // Magic number for corruption detection
     size_t size;              // Size of data
     uint8_t is_free;          // 1 = Free, 0 = Used
     struct heap_header* next; // Next block
-    struct heap_header* prev; // Previous block (New!)
+    struct heap_header* prev; // Previous block
 } heap_header_t;
 
 static heap_header_t* head = NULL;
-static heap_header_t* tail = NULL; // Keep track of tail for easier expansion later
+static heap_header_t* tail = NULL;
+
+// Helper to check corruption
+void check_integrity(heap_header_t* header) {
+    if (header->magic != HEAP_MAGIC) {
+        uart_print("\n[HEAP] CRITICAL: Heap Corruption Detected!\n");
+        uart_print("Block at: ");
+        // TODO: print address
+        uart_print(" has invalid magic number.\n");
+        for(;;) __asm__("hlt");
+    }
+}
+
+void kheap_init(void) {
+    uart_print("[HEAP] Initializing Advanced Heap (Doubly Linked)...\n");
+    
+    // ... (rest of mapping code) ...
+    // Note: I will need to replace the mapping code block manually or be careful
+    // Since I'm using 'edit', let's stick to what changes.
+    // I need to find the exact block to replace.
+    
+    // Actually, I can replace the whole struct definition and init logic.
+    // But 'edit' tool replaces EXACT text matches.
+    // The previous text block I provided in 'newText' won't match because I added functions.
+    
+    // Let's abort this 'edit' approach for the whole file and do precise replacements.
+
 
 void kheap_init(void) {
     uart_print("[HEAP] Initializing Advanced Heap (Doubly Linked)...\n");
@@ -44,6 +73,7 @@ void kheap_init(void) {
     
     // 2. Initial Block
     head = (heap_header_t*)KHEAP_START;
+    head->magic = HEAP_MAGIC; // Set Magic
     head->size = KHEAP_INITIAL_SIZE - sizeof(heap_header_t);
     head->is_free = 1;
     head->next = NULL;
@@ -63,12 +93,19 @@ void* kmalloc(size_t size) {
     heap_header_t* current = head;
     
     while (current) {
+        // Sanity Check
+        if (current->magic != HEAP_MAGIC) {
+            uart_print("[HEAP] Corruption Detected in kmalloc scan!\n");
+            for(;;) __asm__("hlt");
+        }
+
         if (current->is_free && current->size >= aligned_size) {
             // Found candidate. Split?
             if (current->size > aligned_size + sizeof(heap_header_t) + 16) {
                 // Create new header in the remaining space
                 heap_header_t* new_block = (heap_header_t*)((uint8_t*)current + sizeof(heap_header_t) + aligned_size);
                 
+                new_block->magic = HEAP_MAGIC; // Set Magic
                 new_block->size = current->size - aligned_size - sizeof(heap_header_t);
                 new_block->is_free = 1;
                 new_block->next = current->next;
@@ -98,6 +135,12 @@ void kfree(void* ptr) {
     if (!ptr) return;
     
     heap_header_t* header = (heap_header_t*)((uint8_t*)ptr - sizeof(heap_header_t));
+    
+    if (header->magic != HEAP_MAGIC) {
+        uart_print("[HEAP] Corruption Detected in kfree!\n");
+        for(;;) __asm__("hlt");
+    }
+
     header->is_free = 1;
     
     // 1. Coalesce Right (Forward)