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);
80 static inline bool valid_fp(ptr_t ptr) {
81 return KERNEL_STACK < ptr && ptr < KERNEL_EXEC_END;
85 trace_walkback(struct trace_record* tb_buffer,
90 ptr_t* frame = (ptr_t*)fp;
91 struct ksym_entry* current = NULL;
94 while (valid_fp((ptr_t)frame) && i < limit) {
95 ptr_t pc = *(frame + 1);
97 current = trace_sym_lookup(pc);
99 (struct trace_record){ .pc = pc,
100 .sym_pc = current ? current->pc : 0,
101 .symbol = ksym_getstr(current) };
103 frame = (ptr_t*)*frame;
107 if (!valid_fp((ptr_t)frame)) {
112 *last_fp = (ptr_t)frame;
119 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
121 DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
125 trace_printstack_of(ptr_t fp)
127 struct trace_record tbs[NB_TRACEBACK];
129 // Let's get our Stackwalker does his job ;)
130 int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
133 DEBUG("...<truncated>");
136 for (int i = 0; i < n; i++) {
137 struct trace_record* tb = &tbs[i];
138 trace_print_code_entry(tb->sym_pc, tb->pc, tb->symbol);
145 trace_printstack_of(cpu_get_fp());
149 trace_printswctx(const isr_param* p, char* direction)
152 struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
154 DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
159 ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
160 trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
164 trace_printstack_isr(const isr_param* isrm)
167 ptr_t fp = cpu_get_fp();
168 int prev_fromusr = 0;
170 DEBUG("stack trace (pid=%d)\n", __current->pid);
172 trace_printstack_of(fp);
176 if (uspace_context(p)) {
177 trace_printswctx(p, "s/u");
179 trace_printswctx(p, "s/s");
182 trace_printswctx(p, "u/s");
186 trace_printstack_of(fp);
188 prev_fromusr = uspace_context(p);
190 p = p->execp->saved_prev_ctx;