AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
trace.c
Go to the documentation of this file.
1
10
11#include <core/system.h>
12#include <core/printk.h>
13#include <boot/boot.h>
14
20
21extern struct boot *__kboot;
22
24
27static const elf_sym_t *symbol_of(uintptr_t ip)
28{
29 if (!__kboot || !__kboot->symtab || !__kboot->strtab)
30 return NULL;
31
32 const elf_sym_t *sym = (const elf_sym_t *) (uintptr_t) __kboot->symtab->sh_addr;
33
34 for (size_t i = 0; i < __kboot->symnum; ++i, ++sym)
35 if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
36 ip >= sym->st_value && ip < sym->st_value + sym->st_size)
37 return sym;
38
39 return NULL;
40}
41
42/* Prints the stack trace of the running code, up to 20 frames. */
44{
46
47 struct stack_frame *stk = NULL;
48
49#if ARCH_BITS==32
50 asm volatile ("movl %%ebp, %0":"=r"(stk));
51#else
52 asm volatile ("movq %%rbp, %0":"=r"(stk));
53#endif
54
55 printk("stack trace:\n");
56
57 for (int frame = 0; stk && frame < 20; ++frame) {
58 const elf_sym_t *sym = symbol_of(stk->ip);
59
60 if (sym) {
61 const char *names = (const char *) (uintptr_t) __kboot->strtab->sh_addr;
62 printk(" [%p] %s+0x%lx\n", stk->ip, names + sym->st_name, (unsigned long) (stk->ip - sym->st_value));
63 } else {
64 printk(" [%p]\n", stk->ip);
65 }
66
67 stk = stk->bp;
68 }
69}
struct boot * __kboot
Definition init.c:19
int __printing_trace
Definition trace.c:11
void arch_stack_trace(void)
Prints the stack trace of the running code, for a panic: the return address of each frame.
Definition trace.c:14
#define ELF32_ST_TYPE(i)
Definition elf.h:133
struct elf32_sym elf_sym_t
Definition elf.h:224
#define STT_FUNC
Definition elf.h:144
static const elf_sym_t * symbol_of(uintptr_t ip)
Returns the symbol of the function that ip is in, or NULL if there are no symbols or no function hold...
Definition trace.c:27
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
Definition boot.h:35
elf_shdr_t * symtab
Definition boot.h:49
size_t symnum
Definition boot.h:50
elf_shdr_t * strtab
Definition boot.h:52
elf32_addr sh_addr
Definition elf.h:173
elf32_word st_name
Definition elf.h:184
uint8_t st_info
Definition elf.h:187
elf32_word st_value
Definition elf.h:185
elf32_word st_size
Definition elf.h:186
A stack frame, as the compiler lays it out with frame pointers.
Definition trace.c:16
uintptr_t ip
Definition trace.c:18
struct stack_frame * bp
Definition trace.c:17