Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
index 1235884c68704417f821f36501fa8249133fd2de..1e5c44ade8af440b76383681fb6dd7b670d0b893 100644 (file)
@@ -5,8 +5,8 @@
 #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 <klibc/string.h>
 
@@ -77,6 +77,11 @@ ksym_getstr(struct ksym_entry* sym)
                    trace_ctx.ksym_table->ksym_label_off + sym->label_off);
 }
 
+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;
+}
+
 int
 trace_walkback(struct trace_record* tb_buffer,
                ptr_t fp,
@@ -87,8 +92,8 @@ trace_walkback(struct trace_record* tb_buffer,
     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] =
@@ -100,6 +105,10 @@ trace_walkback(struct trace_record* tb_buffer,
         i++;
     }
 
+    if (!valid_fp((ptr_t)frame)) {
+        frame = NULL;
+    }
+
     if (last_fp) {
         *last_fp = (ptr_t)frame;
     }
@@ -110,7 +119,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)
 {
-    DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
+    if (sym_pc) {
+        DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
+    } else {
+        DEBUG("%p+%p: %s", inst_pc, sym_pc, sym);
+    }
 }
 
 void
@@ -134,49 +147,56 @@ trace_printstack_of(ptr_t fp)
 void
 trace_printstack()
 {
-    trace_printstack_of(cpu_get_fp());
+    trace_printstack_of(abi_get_callframe());
 }
 
 static void
-trace_printswctx(const isr_param* p, char* direction)
+trace_printswctx(const isr_param* p, bool from_usr, bool to_usr)
 {
 
     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
 
-    DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
-          direction,
+    DEBUG("^^^^^ --- %s", to_usr ? "user" : "kernel");
+    DEBUG("  interrupted on #%d, ecode=%p",
           p->execp->vector,
           p->execp->err_code);
+    DEBUG("vvvvv --- %s", from_usr ? "user" : "kernel");
 
-    trace_print_code_entry(sym->pc, p->execp->eip, ksym_getstr(sym));
+    ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
+    trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
 }
 
 void
 trace_printstack_isr(const isr_param* isrm)
 {
     isr_param* p = isrm;
-    ptr_t fp = cpu_get_fp();
-    int prev_fromusr = 0;
+    ptr_t fp = abi_get_callframe();
+    int prev_usrctx = 0;
 
     DEBUG("stack trace (pid=%d)\n", __current->pid);
 
     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 {
-                trace_printswctx(p, "s/s");
+                trace_printswctx(p, false, false);
             }
         } else {
-            trace_printswctx(p, "u/s");
+            trace_printswctx(p, false, true);
         }
 
         fp = saved_fp(p);
+        if (!valid_fp(fp)) {
+            DEBUG("??? invalid frame: %p", fp);
+            break;
+        }
+
         trace_printstack_of(fp);
 
-        prev_fromusr = uspace_context(p);
+        prev_usrctx = !kernel_context(p);
 
         p = p->execp->saved_prev_ctx;
     }