AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
16
struct
stack_frame
{
17
struct
stack_frame
*
bp
;
18
uintptr_t
ip
;
19
};
20
21
extern
struct
boot
*
__kboot
;
22
23
int
__printing_trace
= 0;
24
27
static
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. */
43
void
arch_stack_trace
(
void
)
44
{
45
__printing_trace
= 1;
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
}
__kboot
struct boot * __kboot
Definition
init.c:19
__printing_trace
int __printing_trace
Definition
trace.c:11
arch_stack_trace
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
ELF32_ST_TYPE
#define ELF32_ST_TYPE(i)
Definition
elf.h:133
elf_sym_t
struct elf32_sym elf_sym_t
Definition
elf.h:224
STT_FUNC
#define STT_FUNC
Definition
elf.h:144
symbol_of
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
boot.h
system.h
printk
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition
printk.c:249
printk.h
NULL
#define NULL
Definition
stddef.h:27
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
boot
Definition
boot.h:35
boot::symtab
elf_shdr_t * symtab
Definition
boot.h:49
boot::symnum
size_t symnum
Definition
boot.h:50
boot::strtab
elf_shdr_t * strtab
Definition
boot.h:52
elf32_shdr::sh_addr
elf32_addr sh_addr
Definition
elf.h:173
elf32_sym::st_name
elf32_word st_name
Definition
elf.h:184
elf32_sym::st_info
uint8_t st_info
Definition
elf.h:187
elf32_sym::st_value
elf32_word st_value
Definition
elf.h:185
elf32_sym::st_size
elf32_word st_size
Definition
elf.h:186
stack_frame
A stack frame, as the compiler lays it out with frame pointers.
Definition
trace.c:16
stack_frame::ip
uintptr_t ip
Definition
trace.c:18
stack_frame::bp
struct stack_frame * bp
Definition
trace.c:17
kernel
arch
i386
cpu
trace.c
Generated by
1.18.0