Decoupling Architectural-specific Code (#35)
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
index 1cf9ae34d37295a7251058d80813c978c7e84e12..66e5505e2bc97a92d6c022beecbbe57391be9112 100644 (file)
@@ -1,12 +1,12 @@
 #include <lunaix/mm/page.h>
 #include <lunaix/mm/page.h>
-#include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/trace.h>
 
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/trace.h>
 
-#include <sys/cpu.h>
-#include <sys/mm/mempart.h>
+#include <sys/abi.h>
+#include <sys/mm/mm_defs.h>
+#include <sys/trace.h>
 
 #include <klibc/string.h>
 
 
 #include <klibc/string.h>
 
 
 LOG_MODULE("TRACE")
 
 
 LOG_MODULE("TRACE")
 
+weak struct ksyms __lunaix_ksymtable[] = { };
+extern struct ksyms __lunaix_ksymtable[];
+
 static struct trace_context trace_ctx;
 
 static struct trace_context trace_ctx;
 
+void
+trace_log(const char* fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+
+    kprintf_m("TRACE", fmt, args);
+
+    va_end(args);
+}
+
 void
 trace_modksyms_init(struct boot_handoff* bhctx)
 {
 void
 trace_modksyms_init(struct boot_handoff* bhctx)
 {
-    struct boot_modent* modents = bhctx->mods.entries;
-    for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
-        struct boot_modent* mod = &bhctx->mods.entries[i];
-        if (streq(mod->str, "modksyms")) {
-            assert(PG_ALIGNED(mod->start));
-
-            ptr_t end = ROUNDUP(mod->end, PG_SIZE);
-            ptr_t ksym_va =
-              (ptr_t)vmap(mod->start, (end - mod->start), PG_PREM_R, 0);
-
-            assert(ksym_va);
-            trace_ctx.ksym_table = (struct ksyms*)ksym_va;
-        }
-    }
+    trace_ctx.ksym_table = __lunaix_ksymtable;
 }
 
 struct ksym_entry*
 trace_sym_lookup(ptr_t addr)
 {
 }
 
 struct ksym_entry*
 trace_sym_lookup(ptr_t addr)
 {
-    int c = trace_ctx.ksym_table->ksym_count;
+    unsigned long c = trace_ctx.ksym_table->ksym_count;
     struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
 
     int i = c - 1, j = 0, m = 0;
 
     struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
 
     int i = c - 1, j = 0, m = 0;
 
-    if (addr > ksent[i].pc || addr < ksent[j].pc || addr < KERNEL_EXEC) {
+    if (addr > ksent[i].pc || addr < ksent[j].pc || !kernel_addr(addr)) {
         return NULL;
     }
 
         return NULL;
     }
 
@@ -73,8 +74,14 @@ ksym_getstr(struct ksym_entry* sym)
         return "???";
     }
 
         return "???";
     }
 
-    return (char*)((ptr_t)trace_ctx.ksym_table +
-                   trace_ctx.ksym_table->ksym_label_off + sym->label_off);
+    return sym->label;
+}
+
+static inline bool valid_fp(ptr_t ptr) {
+    ptr_t start = ROUNDUP(current_thread->kstack - KSTACK_SIZE, MEM_PAGE);
+
+    return (start < ptr && ptr < current_thread->kstack) 
+           || arch_valid_fp(ptr);
 }
 
 int
 }
 
 int
@@ -87,8 +94,8 @@ trace_walkback(struct trace_record* tb_buffer,
     struct ksym_entry* current = NULL;
     int i = 0;
 
     struct ksym_entry* current = NULL;
     int i = 0;
 
-    while (frame && i < limit) {
-        ptr_t pc = *(frame + 1);
+    while (valid_fp((ptr_t)frame) && i < limit) {
+        ptr_t pc = abi_get_retaddrat((ptr_t)frame);
 
         current = trace_sym_lookup(pc);
         tb_buffer[i] =
 
         current = trace_sym_lookup(pc);
         tb_buffer[i] =
@@ -100,6 +107,10 @@ trace_walkback(struct trace_record* tb_buffer,
         i++;
     }
 
         i++;
     }
 
+    if (!valid_fp((ptr_t)frame)) {
+        frame = NULL;
+    }
+
     if (last_fp) {
         *last_fp = (ptr_t)frame;
     }
     if (last_fp) {
         *last_fp = (ptr_t)frame;
     }
@@ -110,7 +121,11 @@ trace_walkback(struct trace_record* tb_buffer,
 static inline void
 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
 {
 static inline void
 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
 {
-    DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
+    if (sym_pc) {
+        trace_log("%s+%p", sym, inst_pc - sym_pc);
+    } else {
+        trace_log("%s [%p]", sym, sym_pc);
+    }
 }
 
 void
 }
 
 void
@@ -122,7 +137,7 @@ trace_printstack_of(ptr_t fp)
     int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
 
     if (fp) {
     int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
 
     if (fp) {
-        DEBUG("...<truncated>");
+        trace_log("...<truncated>");
     }
 
     for (int i = 0; i < n; i++) {
     }
 
     for (int i = 0; i < n; i++) {
@@ -134,51 +149,62 @@ trace_printstack_of(ptr_t fp)
 void
 trace_printstack()
 {
 void
 trace_printstack()
 {
-    trace_printstack_of(cpu_get_fp());
+    if (current_thread) {
+        trace_printstack_isr(current_thread->hstate);
+    }
+    else {
+        trace_printstack_of(abi_get_callframe());
+    }
 }
 
 static void
 }
 
 static void
-trace_printswctx(const isr_param* p, char* direction)
+trace_printswctx(const struct hart_state* hstate, bool from_usr, bool to_usr)
 {
 
 {
 
-    struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
+    struct ksym_entry* sym = trace_sym_lookup(hstate->execp->eip);
 
 
-    DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
-          direction,
-          p->execp->vector,
-          p->execp->err_code);
+    trace_log("^^^^^ --- %s", to_usr ? "user" : "kernel");
+    trace_print_transistion_short(hstate);
+    trace_log("vvvvv --- %s", from_usr ? "user" : "kernel");
 
 
-    ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
-    trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
+    ptr_t sym_pc = sym ? sym->pc : hart_pc(hstate);
+    trace_print_code_entry(sym_pc, hart_pc(hstate), ksym_getstr(sym));
 }
 
 void
 }
 
 void
-trace_printstack_isr(const isr_param* isrm)
+trace_printstack_isr(const struct hart_state* hstate)
 {
 {
-    isr_param* p = isrm;
-    ptr_t fp = cpu_get_fp();
-    int prev_fromusr = 0;
+    struct hart_state* p = hstate;
+    ptr_t fp = abi_get_callframe();
+    int prev_usrctx = 0;
 
 
-    DEBUG("stack trace (pid=%d)\n", __current->pid);
+    trace_log("stack trace (pid=%d)\n", __current->pid);
 
     trace_printstack_of(fp);
 
     while (p) {
 
     trace_printstack_of(fp);
 
     while (p) {
-        if (!prev_fromusr) {
-            if (uspace_context(p)) {
-                trace_printswctx(p, "s/u");
+        if (!prev_usrctx) {
+            if (!kernel_context(p)) {
+                trace_printswctx(p, true, false);
             } else {
             } else {
-                trace_printswctx(p, "s/s");
+                trace_printswctx(p, false, false);
             }
         } else {
             }
         } else {
-            trace_printswctx(p, "u/s");
+            trace_printswctx(p, false, true);
+        }
+
+        fp = hart_stack_frame(p);
+        if (!valid_fp(fp)) {
+            trace_log("??? invalid frame: %p", fp);
+            break;
         }
 
         }
 
-        fp = saved_fp(p);
         trace_printstack_of(fp);
 
         trace_printstack_of(fp);
 
-        prev_fromusr = uspace_context(p);
+        prev_usrctx = !kernel_context(p);
 
 
-        p = p->execp->saved_prev_ctx;
+        p = hart_parent_state(p);
     }
     }
+
+    trace_log("----- [trace end] -----\n");
 }
\ No newline at end of file
 }
\ No newline at end of file