-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
122 lines (100 loc) · 2.84 KB
/
monitor.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Simple command-line kernel monitor useful for
// controlling the kernel and exploring the system interactively.
#include "stdio.h"
#include "memlayout.h"
#include "x86.h"
#include "types.h"
#include "string.h"
#include "kdebug.h"
#include "monitor.h"
#define CMDBUF_SIZE 80 // enough for one VGA text line
struct Command {
const char *name;
const char *desc;
// return -1 to force monitor to exit
int (*func)(int argc, char **argv, struct Trapframe * tf);
};
static struct Command commands[] = {
{ "help", "Display this list of commands", mon_help },
{ "info-kern", "Display information about the kernel", mon_infokern },
};
#define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
/***** Implementations of basic kernel monitor commands *****/
int
mon_help(int argc, char **argv, struct Trapframe *tf)
{
int i;
for (i = 0; i < NCOMMANDS; i++)
cprintf("%s - %s\n", commands[i].name, commands[i].desc);
return 0;
}
int
mon_infokern(int argc, char **argv, struct Trapframe *tf)
{
extern char _start[], entry[], etext[], edata[], end[];
cprintf("Special kernel symbols:\n");
cprintf(" _start %08x (phys)\n", _start);
cprintf(" entry %08x (virt) %08x (phys)\n", entry, entry - KERNBASE);
cprintf(" etext %08x (virt) %08x (phys)\n", etext, etext - KERNBASE);
cprintf(" edata %08x (virt) %08x (phys)\n", edata, edata - KERNBASE);
cprintf(" end %08x (virt) %08x (phys)\n", end, end - KERNBASE);
cprintf("Kernel executable memory footprint: %dKB\n",
ROUNDUP(end - entry, 1024) / 1024);
return 0;
}
int
mon_backtrace(int argc, char **argv, struct Trapframe *tf)
{
// Your code here.
return 0;
}
/***** Kernel monitor command interpreter *****/
#define WHITESPACE "\t\r\n "
#define MAXARGS 16
static int
runcmd(char *buf, struct Trapframe *tf)
{
int argc;
char *argv[MAXARGS];
int i;
// Parse the command buffer into whitespace-separated arguments
argc = 0;
argv[argc] = 0;
while (1) {
// gobble whitespace
while (*buf && strchr(WHITESPACE, *buf))
*buf++ = 0;
if (*buf == 0)
break;
// save and scan past next arg
if (argc == MAXARGS-1) {
cprintf("Too many arguments (max %d)\n", MAXARGS);
return 0;
}
argv[argc++] = buf;
while (*buf && !strchr(WHITESPACE, *buf))
buf++;
}
argv[argc] = 0;
// Lookup and invoke the command
if (argc == 0)
return 0;
for (i = 0; i < NCOMMANDS; i++)
if (strcmp(argv[0], commands[i].name) == 0)
return commands[i].func(argc, argv, tf);
cprintf("Unknown command '%s'\n", argv[0]);
return 0;
}
void
monitor(struct Trapframe *tf)
{
char *buf;
cprintf("Welcome to the xv6 kernel monitor!\n");
cprintf("Type 'help' for a list of commands.\n");
while (1) {
buf = readline("K> ");
if (buf != NULL)
if (runcmd(buf, tf) < 0)
break;
}
}