Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
index 8c5c1800fd6676ab9ec65d959eea2dc17cbf7706..1e5c44ade8af440b76383681fb6dd7b670d0b893 100644 (file)
@@ -5,8 +5,8 @@
 #include <lunaix/syslog.h>
 #include <lunaix/trace.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 <klibc/string.h>
 
 
 #include <klibc/string.h>
 
@@ -78,7 +78,8 @@ ksym_getstr(struct ksym_entry* sym)
 }
 
 static inline bool valid_fp(ptr_t ptr) {
 }
 
 static inline bool valid_fp(ptr_t ptr) {
-    return KERNEL_STACK < ptr && ptr < KERNEL_EXEC_END;
+    ptr_t start = ROUNDUP(current_thread->kstack - KSTACK_SIZE, MEM_PAGE);
+    return start < ptr && ptr < current_thread->kstack;
 }
 
 int
 }
 
 int
@@ -92,7 +93,7 @@ trace_walkback(struct trace_record* tb_buffer,
     int i = 0;
 
     while (valid_fp((ptr_t)frame) && i < limit) {
     int i = 0;
 
     while (valid_fp((ptr_t)frame) && i < limit) {
-        ptr_t pc = *(frame + 1);
+        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] =
@@ -104,6 +105,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;
     }
@@ -114,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)
 {
 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
 }
 
 void
@@ -138,19 +147,20 @@ trace_printstack_of(ptr_t fp)
 void
 trace_printstack()
 {
 void
 trace_printstack()
 {
-    trace_printstack_of(cpu_get_fp());
+    trace_printstack_of(abi_get_callframe());
 }
 
 static void
 }
 
 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);
 
 {
 
     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);
           p->execp->vector,
           p->execp->err_code);
+    DEBUG("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 : p->execp->eip;
     trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
@@ -160,28 +170,33 @@ void
 trace_printstack_isr(const isr_param* isrm)
 {
     isr_param* p = isrm;
 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) {
 
     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 {
             } else {
-                trace_printswctx(p, "s/s");
+                trace_printswctx(p, false, false);
             }
         } else {
             }
         } else {
-            trace_printswctx(p, "u/s");
+            trace_printswctx(p, false, true);
         }
 
         fp = saved_fp(p);
         }
 
         fp = saved_fp(p);
+        if (!valid_fp(fp)) {
+            DEBUG("??? invalid frame: %p", fp);
+            break;
+        }
+
         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 = p->execp->saved_prev_ctx;
     }