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)vmm_vmap(mod->start, (end - mod->start), PG_PREM_R);
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);
94 tb_buffer[i] = (struct trace_record){ .pc = current ? current->pc : pc,
95 .symbol = ksym_getstr(current) };
97 frame = (ptr_t*)*frame;
102 *last_fp = (ptr_t)frame;
109 trace_printstack_of(ptr_t fp)
111 struct trace_record tbs[NB_TRACEBACK];
113 int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
116 kprintf(KDEBUG "...<truncated>\n");
119 for (int i = 0; i < n; i++) {
120 kprintf(KDEBUG "%p: %s\n", tbs[i].pc, tbs[i].symbol);
127 trace_printstack_of(cpu_get_fp());
131 trace_printswctx(const isr_param* p, char* direction)
134 struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
136 kprintf(KDEBUG ">> (sw:%s) iv:%d, errno:%p <<\n",
140 kprintf(KDEBUG "%p:%s\n", p->execp->eip, ksym_getstr(sym));
144 trace_printstack_isr(const isr_param* isrm)
147 ptr_t fp = cpu_get_fp();
148 int prev_fromusr = uspace_context(p);
150 kprintf(KDEBUG "\n");
151 kprintf(KDEBUG "stack trace (pid=%d)\n", __current->pid);
153 trace_printstack_of(fp);
157 if (uspace_context(p)) {
158 trace_printswctx(p, "s/u");
160 trace_printswctx(p, "s/s");
163 trace_printswctx(p, "u/s");
167 trace_printstack_of(fp);
169 prev_fromusr = uspace_context(p);
171 p = p->execp->saved_prev_ctx;
173 kprintf(KDEBUG "\n");