#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/sections.h>
-#include <sys/cpu.h>
-#include <sys/mm/mempart.h>
+#include <asm/abi.h>
+#include <asm/mm_defs.h>
#include <klibc/string.h>
LOG_MODULE("TRACE")
+extern_autogen(ksymtable);
+
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)
{
- 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 = autogen(struct ksyms, ksymtable);
}
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;
- 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;
}
ksym_getstr(struct ksym_entry* sym)
{
if (!sym) {
- return "???";
+ return NULL;
}
- 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, PAGE_SIZE);
+
+ return (start < ptr && ptr < current_thread->kstack)
+ || arch_valid_fp(ptr);
}
int
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] = (struct trace_record){ .pc = current ? current->pc : pc,
- .symbol = ksym_getstr(current) };
+ tb_buffer[i] =
+ (struct trace_record){ .pc = pc,
+ .sym_pc = current ? current->pc : 0,
+ .symbol = ksym_getstr(current) };
frame = (ptr_t*)*frame;
i++;
}
+ if (!valid_fp((ptr_t)frame)) {
+ frame = NULL;
+ }
+
if (last_fp) {
*last_fp = (ptr_t)frame;
}
return i;
}
+static inline void
+trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
+{
+ if (sym) {
+ trace_log("%s+%p", sym, inst_pc - sym_pc);
+ } else {
+ trace_log("??? [%p]", inst_pc);
+ }
+}
+
void
trace_printstack_of(ptr_t fp)
{
struct trace_record tbs[NB_TRACEBACK];
+ // Let's get our Stackwalker does his job ;)
int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
if (fp) {
- kprintf(KDEBUG "...<truncated>\n");
+ trace_log("...<truncated>");
}
for (int i = 0; i < n; i++) {
- kprintf(KDEBUG "%p: %s\n", tbs[i].pc, tbs[i].symbol);
+ struct trace_record* tb = &tbs[i];
+ trace_print_code_entry(tb->sym_pc, tb->pc, tb->symbol);
}
}
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
-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(hart_pc(hstate));
+
+ trace_log("^^^^^ --- %s", to_usr ? "user" : "kernel");
+ trace_print_transistion_short(hstate);
+ trace_log("vvvvv --- %s", from_usr ? "user" : "kernel");
- kprintf(KDEBUG ">> (sw:%s) iv:%d, errno:%p <<\n",
- direction,
- p->execp->vector,
- p->execp->err_code);
- kprintf(KDEBUG "%p:%s\n", 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
-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 = uspace_context(p);
+ struct hart_state* p = hstate;
+ ptr_t fp = abi_get_callframe();
+ int prev_usrctx = 0;
- kprintf(KDEBUG "\n");
- kprintf(KDEBUG "stack trace (pid=%d)\n", __current->pid);
+ trace_log("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 = hart_stack_frame(p);
+ if (!valid_fp(fp)) {
+ trace_log("??? invalid frame: %p", fp);
+ break;
}
- fp = saved_fp(p);
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);
}
- kprintf(KDEBUG "\n");
+
+ trace_log("----- [trace end] -----\n");
}
\ No newline at end of file