kth: Remove irq_state from sem_.*irqsave's interface
[akaros.git] / kern / src / monitor.c
index ffa2373..3f26125 100644 (file)
@@ -2,7 +2,6 @@
 // controlling the kernel and exploring the system interactively.
 
 #include <arch/arch.h>
-#include <stab.h>
 #include <smp.h>
 #include <arch/console.h>
 
@@ -24,6 +23,7 @@
 #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,7 +60,6 @@ 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},
        { "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},
@@ -76,6 +74,7 @@ static command_t commands[] = {
        { "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]))
 
@@ -122,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;
 }
 
@@ -189,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);
@@ -220,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)
@@ -256,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");
@@ -280,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;
 }
 
@@ -309,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 */
 
@@ -318,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;
@@ -336,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();
@@ -702,22 +683,21 @@ static DEFINE_PERCPU(bool, mon_nmi_trace);
 
 static void emit_hwtf_backtrace(struct hw_trapframe *hw_tf)
 {
-       char *fn_name;
-
        if (mon_verbose_trace) {
+               printk("\n");
                print_trapframe(hw_tf);
                backtrace_hwtf(hw_tf);
        }
-       fn_name = get_fn_name(get_hwtf_pc(hw_tf));
        printk("Core %d is at %p (%s)\n", core_id(), get_hwtf_pc(hw_tf),
-              fn_name);
-       kfree(fn_name);
+              get_fn_name(get_hwtf_pc(hw_tf)));
 }
 
 static void emit_vmtf_backtrace(struct vm_trapframe *vm_tf)
 {
-       if (mon_verbose_trace)
+       if (mon_verbose_trace) {
+               printk("\n");
                print_vmtrapframe(vm_tf);
+       }
        printk("Core %d is at %p\n", core_id(), get_vmtf_pc(vm_tf));
 }
 
@@ -726,22 +706,18 @@ static void emit_vmtf_backtrace(struct vm_trapframe *vm_tf)
  * doesn't deal in contexts (yet) */
 void emit_monitor_backtrace(int type, void *tf)
 {
-       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
-
        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;
-       /* Temporarily disable deadlock detection when we print.  We could
-        * deadlock if we were printing when we NMIed. */
-       pcpui->__lock_checking_enabled--;
+       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());
-       pcpui->__lock_checking_enabled++;
+       print_unlock();
 }
 
 
@@ -945,125 +921,6 @@ 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. */
-       printk("GIANT WARNING: the pm_flusher is running and will never stop!\n");
-       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_shell(int argc, char **argv, struct hw_trapframe *hw_tf)
 {
        char *l_argv[2] = {"/bin/bash", "bash"};
@@ -1148,14 +1005,14 @@ int mon_db(int argc, char **argv, struct hw_trapframe *hw_tf)
 
        if (argc < 2) {
                printk("Usage: db OPTION\n");
-               printk("\tsem [PID]: print all semaphore 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")) {
+       if (!strcmp(argv[1], "blk") || !strcmp(argv[1], "sem")) {
                if (argc > 2)
                        pid = strtol(argv[2], 0, 0);
-               print_all_sem_info(pid);
+               print_db_blk_info(pid);
        } else if (!strcmp(argv[1], "addr")) {
                if (argc < 4) {
                        printk("Usage: db addr PID 0xADDR\n");
@@ -1355,3 +1212,9 @@ int mon_pahexdump(int argc, char **argv, struct hw_trapframe *hw_tf)
        pahexdump(start, len);
        return 0;
 }
+
+int mon_dmesg(int argc, char **argv, struct hw_trapframe *hw_tf)
+{
+       kprof_dump_data();
+       return 0;
+}