feat: kernel stack tracing
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
1 #include <lunaix/mm/mmio.h>
2 #include <lunaix/mm/page.h>
3 #include <lunaix/process.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/syslog.h>
6 #include <lunaix/trace.h>
7
8 #include <sys/cpu.h>
9 #include <sys/mm/mempart.h>
10
11 #include <klibc/string.h>
12
13 #define NB_TRACEBACK 16
14
15 LOG_MODULE("TRACE")
16
17 static struct trace_context trace_ctx;
18
19 void
20 trace_modksyms_init(struct boot_handoff* bhctx)
21 {
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             // In case boot loader does not place our ksyms on page boundary
27             ptr_t start = PG_ALIGN(mod->start);
28             ptr_t end = ROUNDUP(mod->end, PG_SIZE);
29             ptr_t ksym_va = (ptr_t)ioremap(start, (end - start));
30
31             trace_ctx.ksym_table =
32               (struct ksyms*)(ksym_va + (mod->start - start));
33         }
34     }
35 }
36
37 struct ksym_entry*
38 trace_sym_lookup(ptr_t addr)
39 {
40     int c = trace_ctx.ksym_table->ksym_count;
41     struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
42
43     int i = c - 1, j = 0, m = 0;
44
45     if (addr > ksent[i].pc || addr < ksent[j].pc || addr < KERNEL_EXEC) {
46         return NULL;
47     }
48
49     while (i - j != 1) {
50         m = (i + j) / 2;
51         if (ksent[m].pc > addr) {
52             i = m;
53         } else if (ksent[m].pc < addr) {
54             j = m;
55         } else {
56             break;
57         }
58     }
59
60     struct ksym_entry* result = &ksent[MIN(i, j)];
61     if (result->pc > addr) {
62         return NULL;
63     }
64
65     return result;
66 }
67
68 static char*
69 ksym_getstr(struct ksym_entry* sym)
70 {
71     if (!sym) {
72         return "???";
73     }
74
75     return (char*)((ptr_t)trace_ctx.ksym_table +
76                    trace_ctx.ksym_table->ksym_label_off + sym->label_off);
77 }
78
79 int
80 trace_walkback(struct trace_record* tb_buffer,
81                ptr_t fp,
82                int limit,
83                ptr_t* last_fp)
84 {
85     ptr_t* frame = (ptr_t*)fp;
86     struct ksym_entry* current = NULL;
87     int i = 0;
88
89     while (frame && i < limit) {
90         ptr_t pc = *(frame + 1);
91
92         current = trace_sym_lookup(pc);
93         tb_buffer[i] = (struct trace_record){ .pc = current ? current->pc : pc,
94                                               .symbol = ksym_getstr(current) };
95
96         frame = (ptr_t*)*frame;
97         i++;
98     }
99
100     if (last_fp) {
101         *last_fp = (ptr_t)frame;
102     }
103
104     return i;
105 }
106
107 void
108 trace_printstack_of(ptr_t fp)
109 {
110     struct trace_record tbs[NB_TRACEBACK];
111
112     int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
113
114     if (fp) {
115         kprintf(KDEBUG "...<truncated>\n");
116     }
117
118     for (int i = 0; i < n; i++) {
119         kprintf(KDEBUG "%p: %s\n", tbs[i].pc, tbs[i].symbol);
120     }
121 }
122
123 void
124 trace_printstack()
125 {
126     trace_printstack_of(cpu_get_fp());
127 }
128
129 void
130 trace_printswctx(const isr_param* p, char* direction)
131 {
132
133     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
134
135     kprintf(
136       KDEBUG ">> (sw:%s) iv:%d, errno:%p <<\n", direction, p->execp->vector);
137     kprintf(KDEBUG "%p:%s\n", p->execp->eip, ksym_getstr(sym));
138 }
139
140 void
141 trace_printstack_isr(const isr_param* isrm)
142 {
143     isr_param* p = isrm;
144     ptr_t fp = cpu_get_fp();
145     int prev_fromusr = uspace_context(p);
146
147     kprintf(KDEBUG "\n");
148     kprintf(KDEBUG "stack trace (pid=%d)\n", __current->pid);
149
150     trace_printstack_of(fp);
151
152     while (p) {
153         if (!prev_fromusr) {
154             if (uspace_context(p)) {
155                 trace_printswctx(p, "s/u");
156             } else {
157                 trace_printswctx(p, "s/s");
158             }
159         } else {
160             trace_printswctx(p, "u/s");
161         }
162
163         fp = saved_fp(p);
164         trace_printstack_of(fp);
165
166         prev_fromusr = uspace_context(p);
167
168         p = p->execp->saved_prev_ctx;
169     }
170     kprintf(KDEBUG "\n");
171 }