1 #include <lwk/kernel.h>
3 #include <lwk/ptrace.h>
4 #include <lwk/version.h>
5 #include <lwk/kallsyms.h>
9 * Prints a nicely formatted address to the console.
10 * This attempts to look up the symbolic name of the address.
13 printk_address(unsigned long address)
15 unsigned long offset = 0, symsize;
19 symname = kallsyms_lookup(address, &symsize, &offset, namebuf);
21 printk(" [<%016lx>]\n", address);
24 printk(" [<%016lx>] %s+0x%lx/0x%lx\n",
25 address, symname, offset, symsize);
30 * Print a stack trace of the context.
37 #ifndef CONFIG_FRAME_POINTER
38 printk( "Unable to generate stack trace "
39 "(recompile with CONFIG_FRAME_POINTER)\n" );
43 uint64_t * rbp = rbp_v;
45 asm( "mov %%rbp, %0" : "=r"(rbp) );
48 printk( "Stack trace from RBP %p\n", rbp );
50 while( rbp && max_depth-- )
52 printk_address( rbp[1] );
53 rbp = (uint64_t*) *rbp;
59 * Prints x86_64 general purpose registers and friends to the console.
60 * NOTE: This prints the CPU register values contained in the passed in
61 * 'struct pt_regs *'. It DOES NOT print the current values in
62 * the CPU's registers.
65 show_registers(struct pt_regs * regs)
67 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
68 unsigned int fsindex, gsindex;
69 unsigned int ds, cs, es;
70 bool user_fault = (regs->rip < PAGE_OFFSET);
74 printk("Task ID: %d Task Name: %s UTS_RELEASE: %s\n",
75 current ? current->id : -1,
76 current ? current->name : "BAD CURRENT",
80 printk("RIP: %04lx:%016lx (%s)\n", regs->cs & 0xffff, regs->rip,
81 (user_fault) ? "user-context"
82 : kallsyms_lookup(regs->rip, NULL, NULL, namebuf));
83 printk("RSP: %04lx:%016lx EFLAGS: %08lx ERR: %08lx\n",
84 regs->ss, regs->rsp, regs->eflags, regs->orig_rax);
85 printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
86 regs->rax, regs->rbx, regs->rcx);
87 printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
88 regs->rdx, regs->rsi, regs->rdi);
89 printk("RBP: %016lx R08: %016lx R09: %016lx\n",
90 regs->rbp, regs->r8, regs->r9);
91 printk("R10: %016lx R11: %016lx R12: %016lx\n",
92 regs->r10, regs->r11, regs->r12);
93 printk("R13: %016lx R14: %016lx R15: %016lx\n",
94 regs->r13, regs->r14, regs->r15);
96 asm("movl %%ds,%0" : "=r" (ds));
97 asm("movl %%cs,%0" : "=r" (cs));
98 asm("movl %%es,%0" : "=r" (es));
99 asm("movl %%fs,%0" : "=r" (fsindex));
100 asm("movl %%gs,%0" : "=r" (gsindex));
102 rdmsrl(MSR_FS_BASE, fs);
103 rdmsrl(MSR_GS_BASE, gs);
104 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
106 asm("movq %%cr0, %0": "=r" (cr0));
107 asm("movq %%cr2, %0": "=r" (cr2));
108 asm("movq %%cr3, %0": "=r" (cr3));
109 asm("movq %%cr4, %0": "=r" (cr4));
111 printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
112 fs, fsindex, gs, gsindex, shadowgs);
113 printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
114 printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
117 kstack_trace( (void *) regs->rbp );