1 #include <lunaix/mm/page.h>
2 #include <lunaix/mm/vmm.h>
3 #include <lunaix/process.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/syslog.h>
6 #include <lunaix/trace.h>
9 #include <sys/mm/mempart.h>
11 #include <klibc/string.h>
13 #define NB_TRACEBACK 16
17 static struct trace_context trace_ctx;
20 trace_modksyms_init(struct boot_handoff* bhctx)
22 struct boot_modent* modents = bhctx->mods.entries;
23 for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
24 struct boot_modent* mod = &bhctx->mods.entries[i];
25 if (streq(mod->str, "modksyms")) {
26 assert(PG_ALIGNED(mod->start));
28 ptr_t end = ROUNDUP(mod->end, PG_SIZE);
30 (ptr_t)vmap(mod->start, (end - mod->start), PG_PREM_R, 0);
33 trace_ctx.ksym_table = (struct ksyms*)ksym_va;
39 trace_sym_lookup(ptr_t addr)
41 int c = trace_ctx.ksym_table->ksym_count;
42 struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
44 int i = c - 1, j = 0, m = 0;
46 if (addr > ksent[i].pc || addr < ksent[j].pc || addr < KERNEL_EXEC) {
52 if (ksent[m].pc > addr) {
54 } else if (ksent[m].pc < addr) {
61 struct ksym_entry* result = &ksent[MIN(i, j)];
62 if (result->pc > addr) {
70 ksym_getstr(struct ksym_entry* sym)
76 return (char*)((ptr_t)trace_ctx.ksym_table +
77 trace_ctx.ksym_table->ksym_label_off + sym->label_off);
81 trace_walkback(struct trace_record* tb_buffer,
86 ptr_t* frame = (ptr_t*)fp;
87 struct ksym_entry* current = NULL;
90 while (frame && i < limit) {
91 ptr_t pc = *(frame + 1);
93 current = trace_sym_lookup(pc);
95 (struct trace_record){ .pc = pc,
96 .sym_pc = current ? current->pc : 0,
97 .symbol = ksym_getstr(current) };
99 frame = (ptr_t*)*frame;
104 *last_fp = (ptr_t)frame;
111 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
113 DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
117 trace_printstack_of(ptr_t fp)
119 struct trace_record tbs[NB_TRACEBACK];
121 // Let's get our Stackwalker does his job ;)
122 int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
125 DEBUG("...<truncated>");
128 for (int i = 0; i < n; i++) {
129 struct trace_record* tb = &tbs[i];
130 trace_print_code_entry(tb->sym_pc, tb->pc, tb->symbol);
137 trace_printstack_of(cpu_get_fp());
141 trace_printswctx(const isr_param* p, char* direction)
144 struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
146 DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
151 ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
152 trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
156 trace_printstack_isr(const isr_param* isrm)
159 ptr_t fp = cpu_get_fp();
160 int prev_fromusr = 0;
162 DEBUG("stack trace (pid=%d)\n", __current->pid);
164 trace_printstack_of(fp);
168 if (uspace_context(p)) {
169 trace_printswctx(p, "s/u");
171 trace_printswctx(p, "s/s");
174 trace_printswctx(p, "u/s");
178 trace_printstack_of(fp);
180 prev_fromusr = uspace_context(p);
182 p = p->execp->saved_prev_ctx;