Alias "e" to monitor's "exit"
[akaros.git] / kern / src / kdebug.c
index 0086279..982fca5 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,183 @@ 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",
+       "bnx2x_alloc_rx_data",
+       "bnx2x_frag_alloc",
+       "__dma_map_single",
+       "__dma_mapping_error",
+       "__dma_zalloc_coherent",
+       "__dma_alloc_coherent",
+       "bnx2x_ilt_line_mem_op",
+       "bnx2x_ilt_line_init_op",
+       "bnx2x_ilt_line_wr",
+       "bnx2x_wr_64",
+       "pci_write_config_dword",
+       "bnx2x_init_str_wr",
+       "bnx2x_init_fill",
+       "bnx2x_init_block",
+       "bnx2x_write_big_buf",
+       "bnx2x_init_wr_wb",
+       "bnx2x_write_big_buf_wb",
+       "bnx2x_cl45_read",
+       "bnx2x_cl45_write",
+       "bnx2x_set_mdio_clk",
+};
+
+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;
+
+/* Call this via kfunc */
+void reset_print_func_depth(void)
+{
+       tab_depth = 0;
+}
+
+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)
+{
+       char tentabs[] = "\t\t\t\t\t\t\t\t\t\t"; // ten tabs and a \0
+       char *ourtabs = &tentabs[10 - MIN(tab_depth, 10)];
+       if (!printx_on)
+               return;
+       if (is_blacklisted(func))
+               return;
+       spin_lock_irqsave(&lock);
+       __print_hdr();
+       printk("%s%s() in %s\n", ourtabs, func, file);
+       spin_unlock_irqsave(&lock);
+       tab_depth++;
+}
+
+void __print_func_exit(const char *func, const char *file)
+{
+       char tentabs[] = "\t\t\t\t\t\t\t\t\t\t"; // ten tabs and a \0
+       char *ourtabs;
+       if (!printx_on)
+               return;
+       if (is_blacklisted(func))
+               return;
+       tab_depth--;
+       ourtabs = &tentabs[10 - MIN(tab_depth, 10)];
+       spin_lock_irqsave(&lock);
+       __print_hdr();
+       printk("%s---- %s()\n", ourtabs, func);
+       spin_unlock_irqsave(&lock);
+}
+
+bool printx_on = FALSE;
+
+void set_printx(int mode)
+{
+       switch (mode) {
+               case 0:
+                       printx_on = FALSE;
+                       break;
+               case 1:
+                       printx_on = TRUE;
+                       break;
+               case 2:
+                       printx_on = !printx_on;
+                       break;
+       }
+}
+
+void debug_addr_proc(struct proc *p, unsigned long addr)
+{
+       struct vm_region *vmr;
+       spin_lock(&p->vmr_lock);
+       TAILQ_FOREACH(vmr, &p->vm_regions, vm_link) {
+               if ((vmr->vm_base <= addr) && (addr < vmr->vm_end))
+                       break;
+       }
+       if (!vmr) {
+               spin_unlock(&p->vmr_lock);
+               printk("Addr %p has no VMR\n", addr);
+               return;
+       }
+       if (!vmr->vm_file) {
+               spin_unlock(&p->vmr_lock);
+               printk("Addr %p's VMR has no file\n", addr);
+               return;
+       }
+       printk("Addr %p is in %s at offset %p\n", addr, file_name(vmr->vm_file),
+              addr - vmr->vm_base + vmr->vm_foff);
+       spin_unlock(&p->vmr_lock);
+}
+
+void debug_addr_pid(int pid, unsigned long addr)
+{
+       struct proc *p;
+       p = pid2proc(pid);
+       if (!p) {
+               printk("No such proc for pid %d\n", pid);
+               return;
+       }
+       debug_addr_proc(p, addr);
+       proc_decref(p);
+}
+
+void backtrace_kframe(struct hw_trapframe *hw_tf)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       pcpui->__lock_checking_enabled--;
+       printk("\nBacktrace of kernel context on Core %d:\n", core_id());
+       backtrace_frame(get_hwtf_pc(hw_tf), get_hwtf_fp(hw_tf));
+       pcpui->__lock_checking_enabled++;
+}
+
+void backtrace_user_ctx(struct proc *p, struct user_context *ctx)
+{
+       #define MAX_BT_DEPTH 20
+       uintptr_t pcs[MAX_BT_DEPTH];
+       size_t nr_pcs;
+
+       if (!ctx) {
+               printk("Null user context!\n");
+               return;
+       }
+       nr_pcs = backtrace_list(get_user_ctx_pc(ctx), get_user_ctx_fp(ctx), pcs,
+                               MAX_BT_DEPTH);
+       printk("User context backtrace:\n");
+       printk("\tOffsets only matter for shared libraries\n");
+       for (int i = 0; i < nr_pcs; i++) {
+               printk("#%02d ", i + 1);
+               debug_addr_proc(p, pcs[i]);
+       }
+}