-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c~
72 lines (60 loc) · 1.61 KB
/
debug.c~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* =====================================================================================
*
* Filename: debug.c
*
* Description: 调试相关的函数
*
* Version: 1.0
* Created: 2013年11月06日 15时16分18秒
* Revision: none
* Compiler: gcc
*
* Author: Hurley (LiuHuan), [email protected]
* Company: Class 1107 of Computer Science and Technology
*
* =====================================================================================
*/
#include "debug.h"
static void print_stack_trace();
static elf_t kernel_elf;
void init_debug()
{
// 从 GRUB 提供的信息中获取到内核符号表和代码地址信息
kernel_elf = elf_from_multiboot(glb_mboot_ptr);
}
void print_cur_status()
{
static int round = 0;
uint16_t reg1, reg2, reg3, reg4;
asm volatile ( "mov %%cs, %0;"
"mov %%ds, %1;"
"mov %%es, %2;"
"mov %%ss, %3;"
: "=m"(reg1), "=m"(reg2), "=m"(reg3), "=m"(reg4));
// 打印当前的运行级别
printk("%d: @ring %d\n", round, reg1 & 0x3);
printk("%d: cs = %x\n", round, reg1);
printk("%d: ds = %x\n", round, reg2);
printk("%d: es = %x\n", round, reg3);
printk("%d: ss = %x\n", round, reg4);
++round;
}
void panic(const char *msg)
{
printk("*** System panic: %s\n", msg);
print_stack_trace();
printk("***\n");
// 致命错误发生后打印栈信息后停止在这里
while(1);
}
void print_stack_trace()
{
uint32_t *ebp, *eip;
asm volatile ("mov %%ebp, %0" : "=r" (ebp));
while (ebp) {
eip = ebp + 1;
printk(" [0x%x] %s\n", *eip, elf_lookup_symbol(*eip, &kernel_elf));
ebp = (uint32_t*)*ebp;
}
}