]> Devi Nivas Git - cs3210-lab1.git/commitdiff
Update code
authorAdvaith Menon <noreply-git@bp4k.net>
Sun, 1 Feb 2026 06:09:55 +0000 (11:39 +0530)
committerAdvaith Menon <noreply-git@bp4k.net>
Sun, 1 Feb 2026 06:09:55 +0000 (11:39 +0530)
* Add test backtrace to syscall in syscall.c
* Finish up backtrace functions

kernel/src/backtrace.c
kernel/src/proc.c
kernel/src/stab.c
kernel/src/syscall.c

index f3920179328da6d38475c2a9409f128d460e4220..b93a2be2ccd7e28da9b95973b5d8290b0cad03c1 100644 (file)
@@ -1,19 +1,45 @@
 #include "backtrace.h"
 #include "stdio.h"
+#include "stab.h"
 #include "asm/x86.h"
 
 // get next stack pointer - just does some math
-#define get_ra(cur_sp) (*((int *) (cur_sp) - 1))
+#define get_ra(cur_sp) (*((int *) (cur_sp) + 1))
 
 #define MAGIC_RETURN_ADDR 0xF00
 
 
 void backtrace() {
-    int ebp;
+    int ebp, i;
+    struct stab_info si = {
+        NULL, 0, NULL, 0, 0, 0};
     read_ebp(ebp);
-    cprintf("Backtrace: %d\n", sizeof(int));
+    // asm ("movl %%ebp, %0" : "=r" (ebp) );
+    cprintf("Backtrace:\n");
+    char eip_fn_name[32];
+    char *unk = "<unknown>";
+    char *realstr;
+    /* should be really offset
+     * which is how many bytes away from fn start */
+    int lineno = 0;
     while (ebp != MAGIC_RETURN_ADDR) {
-        cprintf("   <0x%x> func1+offs1\n", get_ra(ebp));
+        if (stab_info(get_ra(ebp), &si) == 0) {
+            /* do a strcpy like thing since not null
+             * terminated */
+            for (i = 0; i < si.eip_fn_namelen; ++i) {
+                eip_fn_name[i] = si.eip_fn_name[i];
+            }
+            eip_fn_name[si.eip_fn_namelen] = '\0';
+            realstr = eip_fn_name;
+            lineno = get_ra(ebp)-si.eip_fn_addr;
+        } else {
+            /* set lineno to 0 and fn to unknown */
+            lineno = 0;
+            realstr = unk;
+        }
+
+        cprintf("   <0x%x> %s+%d\n", get_ra(ebp),
+                realstr, lineno);
         ebp = *((int *) ebp);
     }
 }
index 33690ff41954955f59d7ea40cfdf3ed1abf09db9..6e72543a4a3caf53fc101f379ff6ff14ac3c7869 100644 (file)
@@ -9,9 +9,6 @@
 #include "string.h"
 #include "stdio.h"
 
-// FIXME amenon301
-#include "backtrace.h"
-
 struct {
   struct spinlock lock;
   struct proc proc[NPROC];
@@ -81,6 +78,7 @@ allocproc(void)
   struct proc *p;
   char *sp;
 
+
   acquire(&ptable.lock);
 
   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
@@ -129,8 +127,6 @@ userinit(void)
 {
   struct proc *p;
   extern char _binary_initcode_start[], _binary_initcode_size[];
-  // FIXME amenon301
-  backtrace();
 
   p = allocproc();
   
index 5c1e505a34d20f6ab9db7b1ba9e3f53e4e6081d6..0fa8936af7d93881eed219a33d0bdcd5bb571119 100644 (file)
@@ -126,7 +126,7 @@ stab_info(uint addr, struct stab_info *info)
     stabstr_end = __STABSTR_END__;
   } else {
     // Can't search for user-level addresses yet!
-    panic("User address");
+     panic("User address");
   }
 
   // String table validity checks
index 8be3959de277497062f1c16b1fabb261e99d4c8b..f11e41f7c62de212e5f52544dab27465b68e4adc 100644 (file)
@@ -8,6 +8,8 @@
 #include "syscall.h"
 #include "stdio.h"
 
+// FIXME
+#include "backtrace.h"
 // User code makes a system call with INT T_SYSCALL.
 // System call number in %eax.
 // Arguments on the stack, from the user call to the C
@@ -135,6 +137,9 @@ syscall(void)
   int num;
   struct proc *curproc = myproc();
 
+  //FIXME
+  backtrace();
+
   num = curproc->tf->eax;
   if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
     curproc->tf->eax = syscalls[num]();