1 #include <lunaix/mm/page.h>
2 #include <lunaix/process.h>
3 #include <lunaix/spike.h>
4 #include <lunaix/syslog.h>
5 #include <lunaix/trace.h>
6 #include <lunaix/sections.h>
9 #include <asm/mm_defs.h>
10 #include <sys/trace.h>
12 #include <klibc/string.h>
14 #define NB_TRACEBACK 16
18 extern_autogen(ksymtable);
20 static struct trace_context trace_ctx;
23 trace_log(const char* fmt, ...)
28 kprintf_m("TRACE", fmt, args);
34 trace_modksyms_init(struct boot_handoff* bhctx)
36 trace_ctx.ksym_table = autogen(struct ksyms, ksymtable);
38 INFO("symbols loaded: %d @0x%lx",
39 trace_ctx.ksym_table->ksym_count, trace_ctx.ksym_table->syms);
43 trace_sym_lookup(ptr_t addr)
45 unsigned long c = trace_ctx.ksym_table->ksym_count;
46 struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
48 int i = c - 1, j = 0, m = 0;
50 if (addr > ksent[i].pc || addr < ksent[j].pc || !kernel_addr(addr)) {
56 if (ksent[m].pc > addr) {
58 } else if (ksent[m].pc < addr) {
65 struct ksym_entry* result = &ksent[MIN(i, j)];
66 if (result->pc > addr) {
74 ksym_getstr(struct ksym_entry* sym)
83 static inline bool valid_fp(ptr_t ptr) {
84 ptr_t start = ROUNDUP(current_thread->kstack - KSTACK_SIZE, PAGE_SIZE);
86 return (start < ptr && ptr < current_thread->kstack)
87 || arch_valid_fp(ptr);
91 trace_walkback(struct trace_record* tb_buffer,
96 ptr_t* frame = (ptr_t*)fp;
97 struct ksym_entry* current = NULL;
100 while (valid_fp((ptr_t)frame) && i < limit) {
101 ptr_t pc = abi_get_retaddrat((ptr_t)frame);
103 current = trace_sym_lookup(pc);
105 (struct trace_record){ .pc = pc,
106 .sym_pc = current ? current->pc : 0,
107 .symbol = ksym_getstr(current) };
109 frame = (ptr_t*)*frame;
113 if (!valid_fp((ptr_t)frame)) {
118 *last_fp = (ptr_t)frame;
125 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
128 trace_log("%s+%p", sym, inst_pc - sym_pc);
130 trace_log("??? [%p]", inst_pc);
135 trace_printstack_of(ptr_t fp)
137 struct trace_record tbs[NB_TRACEBACK];
139 // Let's get our Stackwalker does his job ;)
140 int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
143 trace_log("...<truncated>");
146 for (int i = 0; i < n; i++) {
147 struct trace_record* tb = &tbs[i];
148 trace_print_code_entry(tb->sym_pc, tb->pc, tb->symbol);
155 if (current_thread) {
156 trace_printstack_isr(current_thread->hstate);
159 trace_printstack_of(abi_get_callframe());
164 trace_printswctx(const struct hart_state* hstate, bool from_usr, bool to_usr)
167 struct ksym_entry* sym = trace_sym_lookup(hart_pc(hstate));
169 trace_log("^^^^^ --- %s", to_usr ? "user" : "kernel");
170 trace_print_transistion_short(hstate);
171 trace_log("vvvvv --- %s", from_usr ? "user" : "kernel");
173 ptr_t sym_pc = sym ? sym->pc : hart_pc(hstate);
174 trace_print_code_entry(sym_pc, hart_pc(hstate), ksym_getstr(sym));
178 trace_printstack_isr(const struct hart_state* hstate)
180 struct hart_state* p = hstate;
181 ptr_t fp = abi_get_callframe();
184 trace_log("stack trace (pid=%d)\n", __current->pid);
186 trace_printstack_of(fp);
190 if (!kernel_context(p)) {
191 trace_printswctx(p, true, false);
193 trace_printswctx(p, false, false);
196 trace_printswctx(p, false, true);
199 fp = hart_stack_frame(p);
201 trace_log("??? invalid frame: %p", fp);
205 trace_printstack_of(fp);
207 prev_usrctx = !kernel_context(p);
209 p = hart_parent_state(p);
212 trace_log("----- [trace end] -----\n");