* same with xv6
[mascara-docs.git] / i386 / standford / 2004 / src / lab4 / kern / monitor.c
blob2d211d1e6b27cabb2c1d595418045f472ec01be3
1 // Simple command-line kernel monitor useful for
2 // controlling the kernel and exploring the system interactively.
4 #include <inc/stdio.h>
5 #include <inc/string.h>
6 #include <inc/pmap.h>
7 #include <inc/assert.h>
8 #include <inc/x86.h>
10 #include <kern/console.h>
11 #include <kern/monitor.h>
12 #include <kern/trap.h>
14 #define CMDBUF_SIZE 80 // enough for one VGA text line
16 struct Command {
17 const char *name;
18 const char *desc;
19 void (*func)(int argc, char **argv);
22 static struct Command commands[] = {
23 {"help", "Display this list of commands", mon_help},
24 {"kerninfo", "Display information about the kernel", mon_kerninfo},
26 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
30 /***** Implementations of basic kernel monitor commands *****/
32 void
33 mon_help(int argc, char **argv)
35 int i;
37 for (i = 0; i < NCOMMANDS; i++)
38 printf("%s - %s\n", commands[i].name, commands[i].desc);
41 void
42 mon_kerninfo(int argc, char **argv)
44 extern char _start[], etext[], edata[], end[];
46 printf("Special kernel symbols:\n");
47 printf(" _start %08x (virt) %08x (phys)\n", _start, _start-KERNBASE);
48 printf(" etext %08x (virt) %08x (phys)\n", etext, etext-KERNBASE);
49 printf(" edata %08x (virt) %08x (phys)\n", edata, edata-KERNBASE);
50 printf(" end %08x (virt) %08x (phys)\n", end, end-KERNBASE);
51 printf("Kernel executable memory footprint: %dKB\n",
52 (end-_start+1023)/1024);
55 void
56 mon_backtrace(int argc, char **argv)
58 // Your code here.
62 /***** Kernel monitor command interpreter *****/
64 #define WHITESPACE "\t\r\n "
65 #define MAXARGS 16
67 static void
68 runcmd(char *buf)
70 int argc;
71 char *argv[MAXARGS];
72 int i;
74 // Parse the command buffer into whitespace-separated arguments
75 argc = 0;
76 argv[argc] = 0;
77 while (1) {
78 // gobble whitespace
79 while (*buf && strchr(WHITESPACE, *buf))
80 *buf++ = 0;
81 if (*buf == 0)
82 break;
84 // save and scan past next arg
85 if (argc == MAXARGS-1) {
86 printf("Too many arguments (max %d)\n", MAXARGS);
87 return;
89 argv[argc++] = buf;
90 while (*buf && !strchr(WHITESPACE, *buf))
91 buf++;
93 argv[argc] = 0;
95 // Lookup and invoke the command
96 if (argc == 0)
97 return;
98 for (i = 0; i < NCOMMANDS; i++) {
99 if (strcmp(argv[0], commands[i].name) == 0) {
100 commands[i].func(argc, argv);
101 return;
104 printf("Unknown command '%s'\n", argv[0]);
107 void
108 monitor(struct Trapframe *tf)
110 char *buf;
112 printf("Welcome to the JOS kernel monitor!\n");
113 printf("Type 'help' for a list of commands.\n");
115 if (tf != NULL)
116 print_trapframe(tf);
118 while (1) {
119 buf = readline("K> ");
120 if (buf != NULL)
121 runcmd(buf);