kth: Remove irq_state from sem_.*irqsave's interface
[akaros.git] / kern / src / monitor.c
index 6b98e48..3f26125 100644 (file)
@@ -2,9 +2,7 @@
 // controlling the kernel and exploring the system interactively.
 
 #include <arch/arch.h>
-#include <stab.h>
 #include <smp.h>
-#include <console.h>
 #include <arch/console.h>
 
 #include <stdio.h>
@@ -24,6 +22,8 @@
 #include <event.h>
 #include <trap.h>
 #include <time.h>
+#include <percpu.h>
+#include <kprof.h>
 
 #include <ros/memlayout.h>
 #include <ros/event.h>
@@ -48,7 +48,6 @@ static command_t commands[] = {
        { "cpuinfo", "Prints CPU diagnostics", mon_cpuinfo},
        { "ps", "Prints process list", mon_ps},
        { "nanwan", "Meet Nanwan!!", mon_nanwan},
-       { "bin_ls", "List files in /bin", mon_bin_ls},
        { "bin_run", "Create and run a program from /bin", mon_bin_run},
        { "manager", "Run the manager", mon_manager},
        { "procinfo", "Show information about processes", mon_procinfo},
@@ -61,16 +60,21 @@ static command_t commands[] = {
        { "measure", "Run a specific measurement", mon_measure},
        { "trace", "Run some tracing functions", mon_trace},
        { "monitor", "Run the monitor on another core", mon_monitor},
-       { "fs", "Filesystem Diagnostics", mon_fs},
-       { "bb", "Try to run busybox (ash)", mon_bb},
+       { "sh", "Try to run a shell (bash)", mon_shell},
+       { "bash", "Try to run a shell (bash)", mon_shell},
+       { "bb", "Try to run a shell (bash)", mon_shell},
        { "alarm", "Alarm Diagnostics", mon_alarm},
        { "msr", "read/write msr: msr msr [value]", mon_msr},
        { "db", "Misc debugging", mon_db},
        { "px", "Toggle printx", mon_px},
        { "kpfret", "Attempt to idle after a kernel fault", mon_kpfret},
        { "ks", "Kernel scheduler hacks", mon_ks},
-       { "gfp", "Get free pages", mon_gfp },
        { "coreinfo", "Print diagnostics for a core", mon_coreinfo},
+       { "hexdump", "Hexdump PID's memory (0 for kernel)", mon_hexdump},
+       { "hd", "Hexdump PID's memory (0 for kernel)", mon_hexdump},
+       { "pahexdump", "Hexdump physical memory", mon_pahexdump},
+       { "phd", "Hexdump physical memory", mon_pahexdump},
+       { "dmesg", "Dump the dmesg buffer", mon_dmesg},
 };
 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
 
@@ -93,12 +97,11 @@ int mon_ps(int argc, char** argv, struct hw_trapframe *hw_tf)
 
 int mon_kerninfo(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       extern char _start[], etext[], edata[], end[];
+       extern char _start[], etext[], end[];
 
        cprintf("Special kernel symbols:\n");
        cprintf("  _start %016x (virt)  %016x (phys)\n", _start, (uintptr_t)(_start - KERNBASE));
        cprintf("  etext  %016x (virt)  %016x (phys)\n", etext, (uintptr_t)(etext - KERNBASE));
-       cprintf("  edata  %016x (virt)  %016x (phys)\n", edata, (uintptr_t)(edata - KERNBASE));
        cprintf("  end    %016x (virt)  %016x (phys)\n", end, (uintptr_t)(end - KERNBASE));
        cprintf("Kernel executable memory footprint: %dKB\n",
                (uint32_t)(end-_start+1023)/1024);
@@ -118,8 +121,10 @@ static int __backtrace(int argc, char **argv, struct hw_trapframe *hw_tf)
        }
        pc = strtol(argv[1], 0, 16);
        fp = strtol(argv[2], 0, 16);
+       print_lock();
        printk("Backtrace from instruction %p, with frame pointer %p\n", pc, fp);
        backtrace_frame(pc, fp);
+       print_unlock();
        return 0;
 }
 
@@ -140,11 +145,12 @@ int mon_reboot(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 static int __showmapping(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       struct proc *p;
+       struct proc *p = NULL;
        uintptr_t start;
        size_t size;
        pgdir_t pgdir;
        pid_t pid;
+
        if (argc < 3) {
                printk("Shows virtual -> physical mappings for a virt addr range.\n");
                printk("Usage: showmapping PID START_ADDR [END_ADDR]\n");
@@ -169,6 +175,8 @@ static int __showmapping(int argc, char **argv, struct hw_trapframe *hw_tf)
                return 1;
        }
        show_mapping(pgdir, start, size);
+       if (p)
+               proc_decref(p);
        return 0;
 }
 
@@ -182,13 +190,11 @@ int mon_sm(int argc, char **argv, struct hw_trapframe *hw_tf)
        return __showmapping(argc, argv, hw_tf);
 }
 
-static spinlock_t print_info_lock = SPINLOCK_INITIALIZER_IRQSAVE;
-
 static void print_info_handler(struct hw_trapframe *hw_tf, void *data)
 {
        uint64_t tsc = read_tsc();
 
-       spin_lock_irqsave(&print_info_lock);
+       print_lock();
        cprintf("----------------------------\n");
        cprintf("This is Core %d\n", core_id());
        cprintf("Timestamp = %lld\n", tsc);
@@ -213,7 +219,7 @@ static void print_info_handler(struct hw_trapframe *hw_tf, void *data)
                read_msr(0x20e), read_msr(0x20f));
 #endif // CONFIG_X86
        cprintf("----------------------------\n");
-       spin_unlock_irqsave(&print_info_lock);
+       print_unlock();
 }
 
 static bool print_all_info(void)
@@ -249,6 +255,7 @@ int mon_nanwan(int argc, char **argv, struct hw_trapframe *hw_tf)
        /* Borrowed with love from http://www.geocities.com/SoHo/7373/zoo.htm
         * (http://www.ascii-art.com/).  Slightly modified to make it 25 lines tall.
         */
+       print_lock();
        printk("\n");
        printk("             .-.  .-.\n");
        printk("             |  \\/  |\n");
@@ -273,26 +280,7 @@ int mon_nanwan(int argc, char **argv, struct hw_trapframe *hw_tf)
        printk("                                \\'   .\\#\n");
        printk("                             jgs \\   ::\\#\n");
        printk("                                  \\      \n");
-       return 0;
-}
-
-int mon_bin_ls(int argc, char **argv, struct hw_trapframe *hw_tf)
-{
-       struct dirent dir = {0};
-       struct file *bin_dir;
-       int retval = 0;
-
-       bin_dir = do_file_open("/bin", O_READ, 0);
-       if (!bin_dir) {
-               printk("No /bin directory!\n");
-               return 1;
-       }
-       printk("Files in /bin:\n-------------------------------\n");
-       do {
-               retval = bin_dir->f_op->readdir(bin_dir, &dir); 
-               printk("%s\n", dir.d_name);
-       } while (retval == 1);
-       kref_put(&bin_dir->f_kref);
+       print_unlock();
        return 0;
 }
 
@@ -302,7 +290,7 @@ int mon_bin_run(int argc, char **argv, struct hw_trapframe *hw_tf)
                printk("Usage: bin_run FILENAME\n");
                return 1;
        }
-       struct file *program;
+       struct file_or_chan *program;
        int retval = 0;
        char buf[5 + MAX_FILENAME_SZ + 1] = "/bin/";    /* /bin/ + max + \0 */
 
@@ -311,7 +299,7 @@ int mon_bin_run(int argc, char **argv, struct hw_trapframe *hw_tf)
                printk("Filename '%s' too long!\n", argv[1]);
                return 1;
        }
-       program = do_file_open(buf, O_READ, 0);
+       program = foc_open(buf, O_EXEC | O_READ, 0);
        if (!program) {
                printk("No such program!\n");
                return 1;
@@ -329,7 +317,7 @@ int mon_bin_run(int argc, char **argv, struct hw_trapframe *hw_tf)
        kfree(p_argv);
        proc_wakeup(p);
        proc_decref(p); /* let go of the reference created in proc_create() */
-       kref_put(&program->f_kref);
+       foc_decref(program);
        /* Make a scheduling decision.  You might not get the process you created,
         * in the event there are others floating around that are runnable */
        run_scheduler();
@@ -342,7 +330,8 @@ int mon_bin_run(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 int mon_procinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       int8_t irq_state = 0;
+       int verbosity = 0;
+
        if (argc < 2) {
                printk("Usage: procinfo OPTION\n");
                printk("\tall: show all active pids\n");
@@ -354,11 +343,13 @@ int mon_procinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
        if (!strcmp(argv[1], "all")) {
                print_allpids();
        } else if (!strcmp(argv[1], "pid")) {
-               if (argc != 3) {
+               if (argc < 3) {
                        printk("Give me a pid number.\n");
                        return 1;
                }
-               print_proc_info(strtol(argv[2], 0, 0));
+               if (argc >= 4)
+                       verbosity = strtol(argv[3], 0, 0);
+               print_proc_info(strtol(argv[2], 0, 0), verbosity);
        } else if (!strcmp(argv[1], "unlock")) {
                if (argc != 3) {
                        printk("Gimme lock address!  Me want lock address!.\n");
@@ -380,9 +371,7 @@ int mon_procinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
                        printk("No such proc\n");
                        return 1;
                }
-               enable_irqsave(&irq_state);
                proc_destroy(p);
-               disable_irqsave(&irq_state);
                proc_decref(p);
        } else {
                printk("Bad option\n");
@@ -393,18 +382,22 @@ int mon_procinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 int mon_pip(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       if (argc != 2) {
+       int verbosity = 0;
+
+       if (argc < 2) {
                printk("Give me a pid number.\n");
                return 1;
        }
-       print_proc_info(strtol(argv[1], 0, 0));
+       if (argc >= 3)
+               verbosity = strtol(argv[2], 0, 0);
+       print_proc_info(strtol(argv[1], 0, 0), verbosity);
        return 0;
 }
 
 int mon_kill(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
        struct proc *p;
-       int8_t irq_state = 0;
+
        if (argc < 2) {
                printk("Usage: kill PID\n");
                return 1;
@@ -414,9 +407,8 @@ int mon_kill(int argc, char **argv, struct hw_trapframe *hw_tf)
                printk("No such proc\n");
                return 1;
        }
-       enable_irqsave(&irq_state);
+       p->exitcode = 1;        /* typical EXIT_FAILURE */
        proc_destroy(p);
-       disable_irqsave(&irq_state);
        proc_decref(p);
        return 0;
 }
@@ -428,11 +420,12 @@ int mon_exit(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 int mon_kfunc(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       void (*func)(void *arg, ...);
+       long ret;
+       long (*func)(void *arg, ...);
 
        if (argc < 2) {
                printk("Usage: kfunc FUNCTION [arg1] [arg2] [etc]\n");
-               printk("Arguments must be in hex.  Can take 6 args.\n");
+               printk("Use 0x with hex arguments.  Can take 6 args.\n");
                return 1;
        }
        func = (void*)get_symbol_addr(argv[1]);
@@ -443,46 +436,47 @@ int mon_kfunc(int argc, char **argv, struct hw_trapframe *hw_tf)
        /* Not elegant, but whatever.  maybe there's a better syntax, or we can do
         * it with asm magic. */
        switch (argc) {
-               case 2: /* have to fake one arg */
-                       func((void*)0);
-                       break;
-               case 3: /* the real first arg */
-                       func((void*)strtol(argv[2], 0, 16));
-                       break;
-               case 4:
-                       func((void*)strtol(argv[2], 0, 16),
-                                   strtol(argv[3], 0, 16));
-                       break;
-               case 5:
-                       func((void*)strtol(argv[2], 0, 16),
-                                   strtol(argv[3], 0, 16),
-                                   strtol(argv[4], 0, 16));
-                       break;
-               case 6:
-                       func((void*)strtol(argv[2], 0, 16),
-                                   strtol(argv[3], 0, 16),
-                                   strtol(argv[4], 0, 16),
-                                   strtol(argv[5], 0, 16));
-                       break;
-               case 7:
-                       func((void*)strtol(argv[2], 0, 16),
-                                   strtol(argv[3], 0, 16),
-                                   strtol(argv[4], 0, 16),
-                                   strtol(argv[5], 0, 16),
-                                   strtol(argv[6], 0, 16));
-                       break;
-               case 8:
-                       func((void*)strtol(argv[2], 0, 16),
-                                   strtol(argv[3], 0, 16),
-                                   strtol(argv[4], 0, 16),
-                                   strtol(argv[5], 0, 16),
-                                   strtol(argv[6], 0, 16),
-                                   strtol(argv[7], 0, 16));
-                       break;
-               default:
-                       printk("Bad number of arguments.\n");
-                       return -1;
+       case 2: /* have to fake one arg */
+               ret = func((void*)0);
+               break;
+       case 3: /* the real first arg */
+               ret = func((void*)strtol(argv[2], 0, 0));
+               break;
+       case 4:
+               ret = func((void*)strtol(argv[2], 0, 0),
+                                 strtol(argv[3], 0, 0));
+               break;
+       case 5:
+               ret = func((void*)strtol(argv[2], 0, 0),
+                                 strtol(argv[3], 0, 0),
+                                 strtol(argv[4], 0, 0));
+               break;
+       case 6:
+               ret = func((void*)strtol(argv[2], 0, 0),
+                                 strtol(argv[3], 0, 0),
+                                 strtol(argv[4], 0, 0),
+                                 strtol(argv[5], 0, 0));
+               break;
+       case 7:
+               ret = func((void*)strtol(argv[2], 0, 0),
+                                 strtol(argv[3], 0, 0),
+                                 strtol(argv[4], 0, 0),
+                                 strtol(argv[5], 0, 0),
+                                 strtol(argv[6], 0, 0));
+               break;
+       case 8:
+               ret = func((void*)strtol(argv[2], 0, 0),
+                                 strtol(argv[3], 0, 0),
+                                 strtol(argv[4], 0, 0),
+                                 strtol(argv[5], 0, 0),
+                                 strtol(argv[6], 0, 0),
+                                 strtol(argv[7], 0, 0));
+               break;
+       default:
+               printk("Bad number of arguments.\n");
+               return -1;
        }
+       printk("%s (might have) returned %p\n", argv[1], ret);
        return 0;
 }
 
@@ -522,7 +516,6 @@ int mon_measure(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
        uint64_t begin = 0, diff = 0;
        uint32_t end_refcnt = 0;
-       int8_t irq_state = 0;
 
        if (argc < 2) {
                printk("Usage: measure OPTION\n");
@@ -550,14 +543,12 @@ int mon_measure(int argc, char **argv, struct hw_trapframe *hw_tf)
                printk("Warning: this will be inaccurate due to the appserver.\n");
                end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores - 1;
 #endif /* CONFIG_APPSERVER */
-               enable_irqsave(&irq_state);
                proc_destroy(p);
-               disable_irqsave(&irq_state);
                proc_decref(p);
 #ifdef CONFIG_APPSERVER
                /* Won't be that accurate, since it's not actually going through the
                 * __proc_free() path. */
-               spin_on(kref_refcnt(&p->p_kref) != end_refcnt); 
+               spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
 #else
                /* this is a little ghetto. it's not fully free yet, but we are also
                 * slowing it down by messing with it, esp with the busy waiting on a
@@ -687,8 +678,48 @@ int mon_measure(int argc, char **argv, struct hw_trapframe *hw_tf)
        return 0;
 }
 
-/* Used in various debug locations.  Not a kernel API or anything. */
-bool mon_verbose_trace = FALSE;
+static bool mon_verbose_trace = FALSE;
+static DEFINE_PERCPU(bool, mon_nmi_trace);
+
+static void emit_hwtf_backtrace(struct hw_trapframe *hw_tf)
+{
+       if (mon_verbose_trace) {
+               printk("\n");
+               print_trapframe(hw_tf);
+               backtrace_hwtf(hw_tf);
+       }
+       printk("Core %d is at %p (%s)\n", core_id(), get_hwtf_pc(hw_tf),
+              get_fn_name(get_hwtf_pc(hw_tf)));
+}
+
+static void emit_vmtf_backtrace(struct vm_trapframe *vm_tf)
+{
+       if (mon_verbose_trace) {
+               printk("\n");
+               print_vmtrapframe(vm_tf);
+       }
+       printk("Core %d is at %p\n", core_id(), get_vmtf_pc(vm_tf));
+}
+
+/* This is dangerous and could cause a deadlock, since it runs in NMI context.
+ * It's only for monitor debugging, so YMMV.  We pass the type since the kernel
+ * doesn't deal in contexts (yet) */
+void emit_monitor_backtrace(int type, void *tf)
+{
+       if (!PERCPU_VAR(mon_nmi_trace))
+               return;
+       /* To prevent a spew of output during a lot of perf NMIs, we'll turn off the
+        * monitor output as soon as any NMI hits our core. */
+       PERCPU_VAR(mon_nmi_trace) = FALSE;
+       print_lock();
+       if (type == ROS_HW_CTX)
+               emit_hwtf_backtrace((struct hw_trapframe*)tf);
+       else
+               emit_vmtf_backtrace((struct vm_trapframe*)tf);
+       print_kmsgs(core_id());
+       print_unlock();
+}
+
 
 int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
@@ -709,27 +740,12 @@ int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
                        return 1;
                }
                if (!strcmp(argv[2], "start")) {
-                       bool all = TRUE;
-                       bool silent = FALSE;
-                       struct proc *p = NULL;
-                       if (argc >= 4) {
-                               silent = (bool)strtol(argv[3], 0, 0);
-                       }
-                       if (argc >= 5) {
-                               all = FALSE;
-                               p = pid2proc(strtol(argv[4], 0, 0));
-                               if (!p) {
-                                       printk("No such process\n");
-                                       return 1;
-                               }
-                       }
-                       systrace_start(silent);
-                       if (systrace_reg(all, p))
-                               printk("No room to trace more processes\n");
+                       systrace_loud = TRUE;
                } else if (!strcmp(argv[2], "stop")) {
-                       /* Stop. To see the output, kfunc systrace_print and systrace_clear */
-                       /* or cat #K/kptrace or /prof/kptrace */
-                       systrace_stop();
+                       systrace_loud = FALSE;
+               } else {
+                       printk("Need a start or stop.\n");
+                       return 1;
                }
        } else if (!strcmp(argv[1], "coretf")) {
                if (argc != 3) {
@@ -739,14 +755,18 @@ int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
                core = strtol(argv[2], 0, 0);
                if (core < 0) {
                        printk("Sending NMIs to all cores:\n");
-                       for (int i = 0; i < num_cores; i++)
+                       for (int i = 0; i < num_cores; i++) {
+                               _PERCPU_VAR(mon_nmi_trace, i) = TRUE;
                                send_nmi(i);
+                               udelay(1000000);
+                       }
                } else {
                        printk("Sending NMI core %d:\n", core);
                        if (core >= num_cores) {
                                printk("No such core!  Maybe it's in another cell...\n");
                                return 1;
                        }
+                       _PERCPU_VAR(mon_nmi_trace, core) = TRUE;
                        send_nmi(core);
                }
                udelay(1000000);
@@ -758,7 +778,7 @@ int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
                        pcpui_type = 0;
                printk("\nRunning PCPUI Trace Ring handlers for type %d\n", pcpui_type);
                if (argc >= 4) {
-                       pcpui_coreid = strtol(argv[3], 0, 0); 
+                       pcpui_coreid = strtol(argv[3], 0, 0);
                        pcpui_tr_foreach(pcpui_coreid, pcpui_type);
                } else {
                        pcpui_tr_foreach_all(pcpui_type);
@@ -784,7 +804,7 @@ int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
                        printk("ERRRRRRRRRR.\n");
                        return 1;
                }
-               print_proc_info(strtol(argv[2], 0, 0));
+               print_proc_info(strtol(argv[2], 0, 0), 0);
        } else {
                printk("Bad option\n");
                return 1;
@@ -824,6 +844,11 @@ int onecmd(int argc, char *argv[], struct hw_trapframe *hw_tf) {
        return -1;
 }
 
+void __run_mon(uint32_t srcid, long a0, long a1, long a2)
+{
+       monitor(0);
+}
+
 static int runcmd(char *real_buf, struct hw_trapframe *hw_tf) {
        char * buf = real_buf;
        int argc;
@@ -833,6 +858,9 @@ static int runcmd(char *real_buf, struct hw_trapframe *hw_tf) {
        // Parse the command buffer into whitespace-separated arguments
        argc = 0;
        argv[argc] = 0;
+       /* Discard initial 'm ', which is a common mistake when using 'm' a lot */
+       if ((buf[0] == 'm') && (buf[1] == ' '))
+               buf += 2;
        while (1) {
                // gobble whitespace
                while (*buf && strchr(WHITESPACE, *buf))
@@ -893,128 +921,10 @@ void monitor(struct hw_trapframe *hw_tf)
        }
 }
 
-static void pm_flusher(void *unused)
-{
-       struct super_block *sb;
-       struct inode *inode;
-       unsigned long nr_pages;
-
-       /* could also put the delay between calls, or even within remove, during the
-        * WB phase. */
-       while (1) {
-               kthread_usleep(5000);
-               TAILQ_FOREACH(sb, &super_blocks, s_list) {
-                       TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
-                               nr_pages = ROUNDUP(inode->i_size, PGSIZE) >> PGSHIFT;
-                               if (nr_pages)
-                                       pm_remove_contig(inode->i_mapping, 0, nr_pages);
-                       }
-               }
-       }
-}
-
-int mon_fs(int argc, char **argv, struct hw_trapframe *hw_tf)
-{
-       /* this assumes one mounted FS at the NS root */
-       struct super_block *sb;
-       struct file *file;
-       struct inode *inode;
-       struct dentry *dentry;
-       if (argc < 2) {
-               printk("Usage: fs OPTION\n");
-               printk("\topen: show all open files\n");
-               printk("\tinodes: show all inodes\n");
-               printk("\tdentries [lru|prune]: show all dentries, opt LRU/prune\n");
-               printk("\tls DIR: print the dir tree starting with DIR\n");
-               printk("\tpid: proc PID's fs crap placeholder\n");
-               printk("\tpmflusher: start a ktask to keep flushing all PMs\n");
-               return 1;
-       }
-       if (!strcmp(argv[1], "open")) {
-               printk("Open Files:\n----------------------------\n");
-               TAILQ_FOREACH(sb, &super_blocks, s_list) {
-                       printk("Superblock for %s\n", sb->s_name);
-                       TAILQ_FOREACH(file, &sb->s_files, f_list)
-                               printk("File: %p, %s, Refs: %d, Drefs: %d, Irefs: %d PM: %p\n",
-                                      file, file_name(file), kref_refcnt(&file->f_kref),
-                                      kref_refcnt(&file->f_dentry->d_kref),
-                                      kref_refcnt(&file->f_dentry->d_inode->i_kref),
-                                          file->f_mapping);
-               }
-       } else if (!strcmp(argv[1], "inodes")) {
-               printk("Mounted FS Inodes:\n----------------------------\n");
-               TAILQ_FOREACH(sb, &super_blocks, s_list) {
-                       printk("Superblock for %s\n", sb->s_name);
-                       TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
-                               printk("Inode: %p, Refs: %d, Nlinks: %d, Size(B): %d\n",
-                                      inode, kref_refcnt(&inode->i_kref), inode->i_nlink,
-                                      inode->i_size);
-                               TAILQ_FOREACH(dentry, &inode->i_dentry, d_alias)
-                                       printk("\t%s: Dentry: %p, Refs: %d\n",
-                                              dentry->d_name.name, dentry,
-                                              kref_refcnt(&dentry->d_kref));
-                       }
-               }
-       } else if (!strcmp(argv[1], "dentries")) {
-               printk("Dentry Cache:\n----------------------------\n");
-               TAILQ_FOREACH(sb, &super_blocks, s_list) {
-                       printk("Superblock for %s\n", sb->s_name);
-                       printk("DENTRY     FLAGS      REFCNT NAME\n");
-                       printk("--------------------------------\n");
-                       /* Hash helper */
-                       void print_dcache_entry(void *item, void *opaque)
-                       {
-                               struct dentry *d_i = (struct dentry*)item;
-                               printk("%p %p %02d     %s\n", d_i, d_i->d_flags,
-                                      kref_refcnt(&d_i->d_kref), d_i->d_name.name);
-                       }
-                       hash_for_each(sb->s_dcache, print_dcache_entry, NULL);
-               }
-               if (argc < 3)
-                       return 0;
-               if (!strcmp(argv[2], "lru")) {
-                       printk("LRU lists:\n");
-                       TAILQ_FOREACH(sb, &super_blocks, s_list) {
-                               printk("Superblock for %s\n", sb->s_name);
-                               TAILQ_FOREACH(dentry, &sb->s_lru_d, d_lru)
-                                       printk("Dentry: %p, Name: %s\n", dentry,
-                                              dentry->d_name.name);
-                       }
-               } else if (!strcmp(argv[2], "prune")) {
-                       printk("Pruning unused dentries\n");
-                       TAILQ_FOREACH(sb, &super_blocks, s_list)
-                               dcache_prune(sb, FALSE);
-               }
-       } else if (!strcmp(argv[1], "ls")) {
-               if (argc != 3) {
-                       printk("Give me a dir.\n");
-                       return 1;
-               }
-               if (argv[2][0] != '/') {
-                       printk("Dear fellow giraffe lover, Use absolute paths.\n");
-                       return 1;
-               }
-               ls_dash_r(argv[2]);
-               /* whatever.  placeholder. */
-       } else if (!strcmp(argv[1], "pid")) {
-               if (argc != 3) {
-                       printk("Give me a pid number.\n");
-                       return 1;
-               }
-               /* whatever.  placeholder. */
-       } else if (!strcmp(argv[1], "pmflusher")) {
-               ktask("pm_flusher", pm_flusher, 0);
-       } else {
-               printk("Bad option\n");
-               return 1;
-       }
-       return 0;
-}
-
-int mon_bb(int argc, char **argv, struct hw_trapframe *hw_tf)
+int mon_shell(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       char *l_argv[3] = {"", "busybox", "ash"};
-       return mon_bin_run(3, l_argv, hw_tf);
+       char *l_argv[2] = {"/bin/bash", "bash"};
+       return mon_bin_run(2, l_argv, hw_tf);
 }
 
 int mon_alarm(int argc, char **argv, struct hw_trapframe *hw_tf)
@@ -1091,14 +1001,18 @@ int mon_msr(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 int mon_db(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
+       pid_t pid = -1;
+
        if (argc < 2) {
                printk("Usage: db OPTION\n");
-               printk("\tsem: print all semaphore info\n");
-               printk("\taddr: for PID lookup ADDR's file/vmr info\n");
+               printk("\tblk [PID]: print all blocked kthreads\n");
+               printk("\taddr PID 0xADDR: for PID lookup ADDR's file/vmr info\n");
                return 1;
        }
-       if (!strcmp(argv[1], "sem")) {
-               print_all_sem_info();
+       if (!strcmp(argv[1], "blk") || !strcmp(argv[1], "sem")) {
+               if (argc > 2)
+                       pid = strtol(argv[2], 0, 0);
+               print_db_blk_info(pid);
        } else if (!strcmp(argv[1], "addr")) {
                if (argc < 4) {
                        printk("Usage: db addr PID 0xADDR\n");
@@ -1114,8 +1028,23 @@ int mon_db(int argc, char **argv, struct hw_trapframe *hw_tf)
 
 int mon_px(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
-       set_printx(2);
-       printk("Printxing is now %sabled\n", printx_on ? "en" : "dis");
+       pid_t pid = 0;
+       struct proc *p;
+
+       if (argc == 2)
+               pid = strtol(argv[1], 0, 0);
+       if (!pid) {
+               set_printx(2);
+               printk("Printxing is now %sabled\n", printx_on ? "en" : "dis");
+               return 0;
+       }
+       p = pid2proc(pid);
+       if (!p) {
+               printk("No proc with pid %d\n", pid);
+               return 1;
+       }
+       p->procdata->printx_on = !p->procdata->printx_on;
+       proc_decref(p);
        return 0;
 }
 
@@ -1187,7 +1116,7 @@ usage:
                return 1;
        }
        if (!strcmp(argv[1], "idles")) {
-               print_idlecoremap();
+               print_idle_core_map();
        } else if (!strcmp(argv[1], "diag")) {
                sched_diag();
        } else if (!strcmp(argv[1], "resources")) {
@@ -1207,26 +1136,6 @@ usage:
        return 0;
 }
 
-int mon_gfp(int argc, char **argv, struct hw_trapframe *hw_tf)
-{
-       size_t naddrpages = max_paddr / PGSIZE;
-       spin_lock_irqsave(&colored_page_free_list_lock);
-       printk("%9s %9s %9s\n", "start", "end", "size");
-       for (int i = 0; i < naddrpages; i++) {
-               int j;
-               for (j = i; j < naddrpages; j++) {
-                       if (!page_is_free(j))
-                               break;
-               }
-               if (j > i) {
-                       printk("%9d %9d %9d\n", i, j, j - i);
-                       i = j;
-               }
-       }
-       spin_unlock_irqsave(&colored_page_free_list_lock);
-       return 0;
-}
-
 /* Prints info about a core.  Optional first arg == coreid. */
 int mon_coreinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
@@ -1254,3 +1163,58 @@ int mon_coreinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
        }
        return 0;
 }
+
+int mon_hexdump(int argc, char **argv, struct hw_trapframe *hw_tf)
+{
+       struct proc *p = NULL;
+       uintptr_t switch_state;
+       pid_t pid;
+       uintptr_t start;
+       size_t len;
+
+       assert(argc >= 1);
+       if (argc < 4) {
+               printk("Usage: %s PID ADDR LEN\n", argv[0]);
+               printk("    PID == 0 for kernel / don't care\n");
+               return 1;
+       }
+       pid = strtol(argv[1], 0, 0);
+       start = strtoul(argv[2], 0, 0);
+       len = strtoul(argv[3], 0, 0);
+       if (pid) {
+               p = pid2proc(pid);
+               if (!p) {
+                       printk("No proc with pid %d\n", pid);
+                       return 1;
+               }
+               switch_state = switch_to(p);
+       }
+       hexdump((void*)start, len);
+       if (p) {
+               switch_back(p, switch_state);
+               proc_decref(p);
+       }
+       return 0;
+}
+
+int mon_pahexdump(int argc, char **argv, struct hw_trapframe *hw_tf)
+{
+       uintptr_t start;
+       size_t len;
+
+       assert(argc >= 1);
+       if (argc < 3) {
+               printk("Usage: %s PHYS_ADDR LEN\n", argv[0]);
+               return 1;
+       }
+       start = strtoul(argv[1], 0, 0);
+       len = strtoul(argv[2], 0, 0);
+       pahexdump(start, len);
+       return 0;
+}
+
+int mon_dmesg(int argc, char **argv, struct hw_trapframe *hw_tf)
+{
+       kprof_dump_data();
+       return 0;
+}