prof: samples userspace PCs
[akaros.git] / kern / src / kdebug.c
index 0086279..5ab058a 100644 (file)
@@ -8,6 +8,7 @@
 #include <kmalloc.h>
 #include <string.h>
 #include <assert.h>
+#include <smp.h>
 
 struct symtab_entry gbl_symtab[1] __attribute__((weak)) = {{0, 0}};
 
@@ -49,3 +50,88 @@ uintptr_t get_symbol_addr(char *sym)
        }
        return 0;
 }
+
+static const char *blacklist[] = {
+       "addnode",
+       "addqueue",
+       "allocroute",
+       "balancetree",
+       "calcd",
+       "freeroute",
+       "genrandom",    /* not noisy, just never returns */
+       "limborexmit",
+       "rangecompare",
+       "walkadd",
+};
+
+static bool is_blacklisted(const char *s)
+{
+       for (int i = 0; i < ARRAY_SIZE(blacklist); i++) {
+               if (!strcmp(blacklist[i], s))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
+static int tab_depth = 0;
+static bool print = TRUE;
+
+/* Call these via kfunc */
+void reset_print_func_depth(void)
+{
+       tab_depth = 0;
+}
+
+void toggle_print_func(void)
+{
+       print = !print;
+       printk("Func entry/exit printing is now %sabled\n", print ? "en" : "dis");
+}
+
+static spinlock_t lock = SPINLOCK_INITIALIZER_IRQSAVE;
+
+static void __print_hdr(void)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       printd("Core %2d ", core_id()); /* may help with multicore output */
+       if (in_irq_ctx(pcpui)) {
+               printk("IRQ       :");
+       } else {
+               assert(pcpui->cur_kthread);
+               if (pcpui->cur_kthread->is_ktask) {
+                       printk("%10s:", pcpui->cur_kthread->name);
+               } else {
+                       printk("PID %3d   :", pcpui->cur_proc ? pcpui->cur_proc->pid : 0);
+               }
+       }
+}
+
+void __print_func_entry(const char *func, const char *file)
+{
+       if (!print)
+               return;
+       if (is_blacklisted(func))
+               return;
+       spin_lock_irqsave(&lock);
+       __print_hdr();
+       for (int i = 0; i < tab_depth; i++)
+               printk("\t");
+       printk("%s() in %s\n", func, file);
+       spin_unlock_irqsave(&lock);
+       tab_depth++;
+}
+
+void __print_func_exit(const char *func, const char *file)
+{
+       if (!print)
+               return;
+       if (is_blacklisted(func))
+               return;
+       tab_depth--;
+       spin_lock_irqsave(&lock);
+       __print_hdr();
+       for (int i = 0; i < tab_depth; i++)
+               printk("\t");
+       printk("---- %s()\n", func);
+       spin_unlock_irqsave(&lock);
+}