Build system to map MMIO into our virtual address space.
[akaros.git] / kern / src / monitor.c
index f116501..4ede378 100644 (file)
@@ -25,6 +25,7 @@
 #include <syscall.h>
 #include <kmalloc.h>
 #include <elf.h>
+#include <event.h>
 
 #include <ros/timer.h>
 #include <ros/memlayout.h>
@@ -59,6 +60,7 @@ static command_t (RO commands)[] = {
        { "trace", "Run a specific measurement", mon_trace},
        { "monitor", "Run the monitor on another core", mon_monitor},
        { "fs", "Filesystem Diagnostics", mon_fs},
+       { "bb", "Try to run busybox (ash)", mon_bb},
 };
 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
 
@@ -266,7 +268,6 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        do {
                retval = bin_dir->f_op->readdir(bin_dir, &dir); 
                printk("%s\n", dir.d_name);
-               dir.d_off++;
        } while (retval == 1);
        kref_put(&bin_dir->f_kref);
        return 0;
@@ -274,7 +275,7 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 
 int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
-       if (argc != 2) {
+       if (argc < 2) {
                printk("Usage: bin_run FILENAME\n");
                return 1;
        }
@@ -287,10 +288,13 @@ int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("No such program!\n");
                return 1;
        }
-       char *p_argv[] = {0, 0, 0};
+       char **p_argv = kmalloc(sizeof(char*) * argc, 0);       /* bin_run's argc */
+       for (int i = 0; i < argc - 1; i++)
+               p_argv[i] = argv[i + 1];
+       p_argv[argc - 1] = 0;
        char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
-       p_argv[0] = file_name(program);
        struct proc *p = proc_create(program, p_argv, p_envp);
+       kfree(p_argv);
 
        spin_lock(&p->proc_lock);
        __proc_set_state(p, PROC_RUNNABLE_S);
@@ -432,11 +436,12 @@ int mon_kfunc(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        return 0;
 }
 
+/* Sending a vcoreid forces an event and an IPI/notification */
 int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        struct proc *p;
-       unsigned int num;
        uint32_t vcoreid;
+       struct event_msg msg = {0};
 
        if (argc < 3) {
                printk("Usage: notify PID NUM [VCOREID]\n");
@@ -447,12 +452,14 @@ int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("No such proc\n");
                return 1;
        }
-       num = strtol(argv[2], 0, 0);
+       msg.ev_type = strtol(argv[2], 0, 0);
        if (argc == 4) {
                vcoreid = strtol(argv[3], 0, 0);
-               do_notify(p, vcoreid, num, 0);
+               post_vcore_event(p, &msg, vcoreid);
+               proc_notify(p, vcoreid);
        } else {
-               proc_notify(p, num, 0);
+               /* o/w, try and do what they want */
+               send_kernel_event(p, &msg, 0);
        }
        kref_put(&p->kref);
        return 0;
@@ -732,11 +739,11 @@ static int runcmd(char *NTS real_buf, trapframe_t *tf) {
        return 0;
 }
 
-void monitor(trapframe_t *tf) {
-       static spinlock_t monitor_lock = SPINLOCK_INITIALIZER;
-       spin_lock_irqsave(&monitor_lock);
-
-       char *buf;
+void monitor(struct trapframe *tf)
+{
+       #define MON_CMD_LENGTH 256
+       char buf[MON_CMD_LENGTH];
+       int cnt;
 
        /* they are always disabled, since we have this irqsave lock */
        if (irq_is_enabled())
@@ -748,12 +755,10 @@ void monitor(trapframe_t *tf) {
        if (tf != NULL)
                print_trapframe(tf);
 
-       spin_unlock_irqsave(&monitor_lock);
        while (1) {
-               spin_lock_irqsave(&monitor_lock);
-               buf = readline("ROS(Core %d)> ", core_id());
-               if (buf != NULL) {
-                       spin_unlock_irqsave(&monitor_lock);
+               cnt = readline(buf, MON_CMD_LENGTH, "ROS(Core %d)> ", core_id());
+               if (cnt > 0) {
+                       buf[cnt] = 0;
                        if (runcmd(buf, tf) < 0)
                                break;
                }
@@ -763,22 +768,73 @@ void monitor(trapframe_t *tf) {
 int mon_fs(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        /* this assumes one mounted FS at the NS root */
-       struct super_block *sb = default_ns.root->mnt_root->d_inode->i_sb;
-       struct file *i;
+       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");
                return 1;
        }
        if (!strcmp(argv[1], "open")) {
                printk("Open Files:\n----------------------------\n");
-               TAILQ_FOREACH(i, &sb->s_files, f_list)
-                       printk("File: %08p, %s, Refs: %d, Drefs: %d, Irefs: %d\n", i,
-                              file_name(i), kref_refcnt(&i->f_kref),
-                                  kref_refcnt(&i->f_dentry->d_kref),
-                                  kref_refcnt(&i->f_dentry->d_inode->i_kref));
+               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: %08p, %s, Refs: %d, Drefs: %d, Irefs: %d\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));
+               }
+       } 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: %08p, 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: %08p, 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);
+                       if (hashtable_count(sb->s_dcache)) {
+                               hashtable_itr_t *dcache_i = hashtable_iterator(sb->s_dcache);
+                               printk("DENTRY     FLAGS      REFCNT NAME\n");
+                               printk("--------------------------------\n");
+                               do {
+                                       struct dentry *d_i = hashtable_iterator_value(dcache_i);
+                                       printk("%08p %08p %02d     %s\n", d_i, d_i->d_flags,
+                                              kref_refcnt(&d_i->d_kref), d_i->d_name.name);
+                               } while (hashtable_iterator_advance(dcache_i));
+                       }
+               }
+               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: %08p, 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");
@@ -802,3 +858,9 @@ int mon_fs(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        }
        return 0;
 }
+
+int mon_bb(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       char *l_argv[3] = {"", "busybox", "ash"};
+       return mon_bin_run(3, l_argv, tf);
+}