feat: owloysius - dynamic init function invocator
[lunaix-os.git] / lunaix-os / kernel / debug / trace.c
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>
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             assert(PG_ALIGNED(mod->start));
27
28             ptr_t end = ROUNDUP(mod->end, PG_SIZE);
29             ptr_t ksym_va =
30               (ptr_t)vmap(mod->start, (end - mod->start), PG_PREM_R, 0);
31
32             assert(ksym_va);
33             trace_ctx.ksym_table = (struct ksyms*)ksym_va;
34         }
35     }
36 }
37
38 struct ksym_entry*
39 trace_sym_lookup(ptr_t addr)
40 {
41     int c = trace_ctx.ksym_table->ksym_count;
42     struct ksym_entry* ksent = trace_ctx.ksym_table->syms;
43
44     int i = c - 1, j = 0, m = 0;
45
46     if (addr > ksent[i].pc || addr < ksent[j].pc || addr < KERNEL_EXEC) {
47         return NULL;
48     }
49
50     while (i - j != 1) {
51         m = (i + j) / 2;
52         if (ksent[m].pc > addr) {
53             i = m;
54         } else if (ksent[m].pc < addr) {
55             j = m;
56         } else {
57             break;
58         }
59     }
60
61     struct ksym_entry* result = &ksent[MIN(i, j)];
62     if (result->pc > addr) {
63         return NULL;
64     }
65
66     return result;
67 }
68
69 static char*
70 ksym_getstr(struct ksym_entry* sym)
71 {
72     if (!sym) {
73         return "???";
74     }
75
76     return (char*)((ptr_t)trace_ctx.ksym_table +
77                    trace_ctx.ksym_table->ksym_label_off + sym->label_off);
78 }
79
80 static inline bool valid_fp(ptr_t ptr) {
81     return KERNEL_STACK < ptr && ptr < KERNEL_EXEC_END;
82 }
83
84 int
85 trace_walkback(struct trace_record* tb_buffer,
86                ptr_t fp,
87                int limit,
88                ptr_t* last_fp)
89 {
90     ptr_t* frame = (ptr_t*)fp;
91     struct ksym_entry* current = NULL;
92     int i = 0;
93
94     while (valid_fp((ptr_t)frame) && i < limit) {
95         ptr_t pc = *(frame + 1);
96
97         current = trace_sym_lookup(pc);
98         tb_buffer[i] =
99           (struct trace_record){ .pc = pc,
100                                  .sym_pc = current ? current->pc : 0,
101                                  .symbol = ksym_getstr(current) };
102
103         frame = (ptr_t*)*frame;
104         i++;
105     }
106
107     if (!valid_fp((ptr_t)frame)) {
108         frame = NULL;
109     }
110
111     if (last_fp) {
112         *last_fp = (ptr_t)frame;
113     }
114
115     return i;
116 }
117
118 static inline void
119 trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym)
120 {
121     DEBUG("%p+%p: %s", sym_pc, inst_pc - sym_pc, sym);
122 }
123
124 void
125 trace_printstack_of(ptr_t fp)
126 {
127     struct trace_record tbs[NB_TRACEBACK];
128
129     // Let's get our Stackwalker does his job ;)
130     int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
131
132     if (fp) {
133         DEBUG("...<truncated>");
134     }
135
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);
139     }
140 }
141
142 void
143 trace_printstack()
144 {
145     trace_printstack_of(cpu_get_fp());
146 }
147
148 static void
149 trace_printswctx(const isr_param* p, char* direction)
150 {
151
152     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
153
154     DEBUG(">> (sw:%s) iv:%d, errno:%p <<",
155           direction,
156           p->execp->vector,
157           p->execp->err_code);
158
159     ptr_t sym_pc = sym ? sym->pc : p->execp->eip;
160     trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym));
161 }
162
163 void
164 trace_printstack_isr(const isr_param* isrm)
165 {
166     isr_param* p = isrm;
167     ptr_t fp = cpu_get_fp();
168     int prev_fromusr = 0;
169
170     DEBUG("stack trace (pid=%d)\n", __current->pid);
171
172     trace_printstack_of(fp);
173
174     while (p) {
175         if (!prev_fromusr) {
176             if (uspace_context(p)) {
177                 trace_printswctx(p, "s/u");
178             } else {
179                 trace_printswctx(p, "s/s");
180             }
181         } else {
182             trace_printswctx(p, "u/s");
183         }
184
185         fp = saved_fp(p);
186         trace_printstack_of(fp);
187
188         prev_fromusr = uspace_context(p);
189
190         p = p->execp->saved_prev_ctx;
191     }
192 }