1 // Simple command-line kernel monitor useful for
2 // controlling the kernel and exploring the system interactively.
5 #include <inc/string.h>
6 #include <inc/memlayout.h>
7 #include <inc/assert.h>
10 #include <kern/console.h>
11 #include <kern/monitor.h>
13 #define CMDBUF_SIZE 80 // enough for one VGA text line
19 // return -1 to force monitor to exit
20 int (*func
)(int argc
, char** argv
, struct Trapframe
* tf
);
23 static struct Command commands
[] = {
24 { "help", "Display this list of commands", mon_help
},
25 { "kerninfo", "Display information about the kernel", mon_kerninfo
},
27 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
31 /***** Implementations of basic kernel monitor commands *****/
34 mon_help(int argc
, char **argv
, struct Trapframe
*tf
)
38 for (i
= 0; i
< NCOMMANDS
; i
++)
39 cprintf("%s - %s\n", commands
[i
].name
, commands
[i
].desc
);
44 mon_kerninfo(int argc
, char **argv
, struct Trapframe
*tf
)
46 extern char _start
[], etext
[], edata
[], end
[];
48 cprintf("Special kernel symbols:\n");
49 cprintf(" _start %08x (virt) %08x (phys)\n", _start
, _start
- KERNBASE
);
50 cprintf(" etext %08x (virt) %08x (phys)\n", etext
, etext
- KERNBASE
);
51 cprintf(" edata %08x (virt) %08x (phys)\n", edata
, edata
- KERNBASE
);
52 cprintf(" end %08x (virt) %08x (phys)\n", end
, end
- KERNBASE
);
53 cprintf("Kernel executable memory footprint: %dKB\n",
54 (end
-_start
+1023)/1024);
59 mon_backtrace(int argc
, char **argv
, struct Trapframe
*tf
)
67 /***** Kernel monitor command interpreter *****/
69 #define WHITESPACE "\t\r\n "
73 runcmd(char *buf
, struct Trapframe
*tf
)
79 // Parse the command buffer into whitespace-separated arguments
84 while (*buf
&& strchr(WHITESPACE
, *buf
))
89 // save and scan past next arg
90 if (argc
== MAXARGS
-1) {
91 cprintf("Too many arguments (max %d)\n", MAXARGS
);
95 while (*buf
&& !strchr(WHITESPACE
, *buf
))
100 // Lookup and invoke the command
103 for (i
= 0; i
< NCOMMANDS
; i
++) {
104 if (strcmp(argv
[0], commands
[i
].name
) == 0)
105 return commands
[i
].func(argc
, argv
, tf
);
107 cprintf("Unknown command '%s'\n", argv
[0]);
112 monitor(struct Trapframe
*tf
)
116 cprintf("Welcome to the JOS kernel monitor!\n");
117 cprintf("Type 'help' for a list of commands.\n");
121 buf
= readline("K> ");
123 if (runcmd(buf
, tf
) < 0)
128 // return EIP of caller.
129 // does not work if inlined.
130 // putting at the end of the file seems to prevent inlining.
135 __asm
__volatile("movl 4(%%ebp), %0" : "=r" (callerpc
));