From bea638adc3da8a10f77358542a9a6889d833fb33 Mon Sep 17 00:00:00 2001
From: Alexis211 <alexis211@gmail.com>
Date: Fri, 13 Aug 2010 20:34:04 +0200
Subject: Some changes to stack tracing.

---
 Grapes.fl.img          | Bin 1474560 -> 1474560 bytes
 src/kernel/core/sys.c  |  27 +++++++++++++++++++++------
 src/kernel/core/sys.h  |   2 ++
 src/kernel/task/idt.c  |   4 ++--
 src/kernel/task/task.c |   9 ++-------
 5 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/Grapes.fl.img b/Grapes.fl.img
index 954d817..9416b8b 100644
Binary files a/Grapes.fl.img and b/Grapes.fl.img differ
diff --git a/src/kernel/core/sys.c b/src/kernel/core/sys.c
index 96d4908..56de038 100644
--- a/src/kernel/core/sys.c
+++ b/src/kernel/core/sys.c
@@ -24,20 +24,35 @@ uint16_t inw(uint16_t port) {
 	return ret;
 }
 
-/*	These two functions stop the system, reporting an error message, because something bad happenned. */
+//////
+void stack_trace(size_t bp) {
+	uint32_t *stack = (uint32_t*)bp, i;
+	for (i = 0; i < 5 && stack > 0xE0000000 && stack < (bp + 0x8000); i++) {
+		monitor_write(" "); monitor_writeHex(stack);
+		monitor_write("\tnext:"); monitor_writeHex(stack[0]); monitor_write("\t\tret:"); monitor_writeHex(stack[1]);
+		monitor_write("\n");
+		stack = (uint32_t*)stack[0];
+	}
+}
 
-void panic(char* message, char* file, int line) {
-	monitor_write("\n\nPANIC:\t\t"); monitor_write(message);
+/*	For internal use only. Used by panic and panic_assert. */
+static void panic_do(char* file, int line) {
 	monitor_write("\n File:\t\t"); monitor_write(file); monitor_put(':'); monitor_writeDec(line);
+	monitor_write("\nTrace:\n");
+	size_t bp; asm volatile("mov %%ebp,%0" : "=r"(bp)); stack_trace(bp);
 	monitor_write("\n\t\tSystem halted -_-'\n");
 	asm volatile("cli; hlt");
 }
 
+/*	These functions stop the system, reporting an error message, because something bad happenned. */
+void panic(char* message, char* file, int line) {
+	monitor_write("\n\nPANIC:\t\t"); monitor_write(message);
+	panic_do(file, line);
+}
+
 void panic_assert(char* assertion, char* file, int line) {
 	monitor_write("\n\nASSERT FAILED:\t"); monitor_write(assertion);
-	monitor_write("\n File:\t\t"); monitor_write(file); monitor_put(':'); monitor_writeDec(line);
-	monitor_write("\n\t\tSystem halted -_-'\n");
-	asm volatile("cli; hlt");
+	panic_do(file, line);
 }
 
 /* Global system mutex. See comments in sys.h. */
diff --git a/src/kernel/core/sys.h b/src/kernel/core/sys.h
index f147922..f429b44 100644
--- a/src/kernel/core/sys.h
+++ b/src/kernel/core/sys.h
@@ -8,6 +8,8 @@ void outw(uint16_t port, uint16_t value);
 uint8_t inb(uint16_t port);
 uint16_t inw(uint16_t port);
 
+void stack_trace(size_t bp);
+
 #define PANIC(s) panic(s, __FILE__, __LINE__);
 #define ASSERT(s) { if (!(s)) panic_assert(#s, __FILE__, __LINE__); }
 void panic(char* message, char* file, int line);
diff --git a/src/kernel/task/idt.c b/src/kernel/task/idt.c
index caf9fb0..6aa16e0 100644
--- a/src/kernel/task/idt.c
+++ b/src/kernel/task/idt.c
@@ -75,12 +75,12 @@ static struct irq_waiter {
 void idt_isrHandler(struct registers regs) {
 	if ((regs.int_no == 14 && paging_fault(&regs) != 0) || regs.int_no != 14) {
 		if (tasking_handleException(&regs) == 0) {
-			monitor_write("  >>  >> SOMETHING BAD HAPPENNED << <<\n");
+			monitor_write("\n  >>  >> SOMETHING BAD HAPPENNED << <<\n");
 			monitor_write("Unhandled exception ");
 			monitor_writeHex(regs.int_no);
 			monitor_write(" @"); monitor_writeHex(regs.eip);
 			monitor_put('\n');
-			PANIC("unhandled exception");
+			PANIC("Unhandled Exception");
 		}
 	}
 } 
diff --git a/src/kernel/task/task.c b/src/kernel/task/task.c
index 8dcf713..7153d85 100644
--- a/src/kernel/task/task.c
+++ b/src/kernel/task/task.c
@@ -107,13 +107,8 @@ uint32_t tasking_handleException(struct registers *regs) {
 	monitor_write(exception_messages[regs->int_no]);
 	monitor_write(" eip:"); monitor_writeHex(regs->eip);
 	if (regs->eip >= 0xE0000000) {
-		monitor_write("\n  Stack trace :");
-		uint32_t *stack = (uint32_t*)regs->ebp, i;
-		for (i = 0; i < 5 && stack > 0xE0000000 && stack < (regs->useresp + 0x8000); i++) {
-			monitor_write("\nframe@"); monitor_writeHex(stack);
-			monitor_write(" next:"); monitor_writeHex(stack[0]); monitor_write(" ret:"); monitor_writeHex(stack[1]);
-			stack = (uint32_t*)stack[0];
-		}
+		monitor_write("\n  Exception stack trace :");
+		stack_trace(regs->ebp);
 		PANIC("Kernel error'd.");
 	}
 	if (regs->int_no == 14) {
-- 
cgit v1.2.3