From abedf3db3774b54ee4ed227e3ae69e55fb0ff76c Mon Sep 17 00:00:00 2001 From: hunt Date: Thu, 26 May 2005 07:43:25 +0000 Subject: 2005-05-26 Martin Hunt * current.c (_stp_sprint_regs): Implement for i386. * sym.c (_stp_symbol_sprint): Check name before trying to print it. (_stp_symbol_print): Change to macro that calls _stp_symbol_sprint(). --- runtime/current.c | 94 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 32 deletions(-) (limited to 'runtime/current.c') diff --git a/runtime/current.c b/runtime/current.c index 618c9fc9..4485ef2c 100644 --- a/runtime/current.c +++ b/runtime/current.c @@ -1,7 +1,8 @@ -#ifndef _CURRENT_C_ +#ifndef _CURRENT_C_ /* -*- linux-c -*- */ #define _CURRENT_C_ -/* -*- linux-c -*- */ +#include "regs.h" + /** @file current.c * @brief Functions to get the current state. */ @@ -14,48 +15,37 @@ /** Get the current return address. * Call from kprobes (not jprobes). * @param regs The pt_regs saved by the kprobe. - * @return The return address saved in esp or rsp. + * @return The return address saved in the stack pointer. * @note i386 and x86_64 only so far. */ unsigned long _stp_ret_addr (struct pt_regs *regs) { -#ifdef __x86_64__ - unsigned long *ra = (unsigned long *)regs->rsp; -#else - unsigned long *ra = (unsigned long *)regs->esp; -#endif - if (ra) - return *ra; - else - return 0; + unsigned long *ra = (unsigned long *)REG_SP(regs); + + if (ra) + return *ra; + else + return 0; } #ifdef __x86_64__ -#include - -void _stp_print_regs(struct pt_regs * regs) +void _stp_sprint_regs(String str, struct pt_regs * regs) { unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs; unsigned int fsindex,gsindex; unsigned int ds,cs,es; - _stp_printf("\n"); - // print_modules(); - _stp_printf("Pid: %d, comm: %.20s %s\n", - current->pid, current->comm, system_utsname.release); - _stp_printf("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->rip); - _stp_symbol_print (regs->rip); - _stp_printf("\nRSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->rsp, regs->eflags); - _stp_printf("RAX: %016lx RBX: %016lx RCX: %016lx\n", + _stp_sprintf(str,"RIP: %016lx\nRSP: %016lx EFLAGS: %08lx\n", regs->rip, regs->rsp, regs->eflags); + _stp_sprintf(str,"RAX: %016lx RBX: %016lx RCX: %016lx\n", regs->rax, regs->rbx, regs->rcx); - _stp_printf("RDX: %016lx RSI: %016lx RDI: %016lx\n", + _stp_sprintf(str,"RDX: %016lx RSI: %016lx RDI: %016lx\n", regs->rdx, regs->rsi, regs->rdi); - _stp_printf("RBP: %016lx R08: %016lx R09: %016lx\n", + _stp_sprintf(str,"RBP: %016lx R08: %016lx R09: %016lx\n", regs->rbp, regs->r8, regs->r9); - _stp_printf("R10: %016lx R11: %016lx R12: %016lx\n", + _stp_sprintf(str,"R10: %016lx R11: %016lx R12: %016lx\n", regs->r10, regs->r11, regs->r12); - _stp_printf("R13: %016lx R14: %016lx R15: %016lx\n", + _stp_sprintf(str,"R13: %016lx R14: %016lx R15: %016lx\n", regs->r13, regs->r14, regs->r15); asm("movl %%ds,%0" : "=r" (ds)); @@ -73,15 +63,55 @@ void _stp_print_regs(struct pt_regs * regs) asm("movq %%cr3, %0": "=r" (cr3)); asm("movq %%cr4, %0": "=r" (cr4)); - _stp_printf("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n", + _stp_sprintf(str,"FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n", fs,fsindex,gs,gsindex,shadowgs); - _stp_printf("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0); - _stp_printf("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4); - _stp_print_flush(); + _stp_sprintf(str,"CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0); + _stp_sprintf(str,"CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4); } -#endif /* __x86_64__ */ +#elif defined (__i386__) +/** Write the registers to a string. + * @param regs The pt_regs saved by the kprobe. + * @note i386 and x86_64 only so far. + */ +void _stp_sprint_regs(String str, struct pt_regs * regs) +{ + unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L; + + _stp_sprintf (str, "EIP: %08lx\n",regs->eip); + _stp_sprintf (str, "ESP: %08lx\n",regs->esp); + _stp_sprintf (str, "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n", + regs->eax,regs->ebx,regs->ecx,regs->edx); + _stp_sprintf (str, "ESI: %08lx EDI: %08lx EBP: %08lx", + regs->esi, regs->edi, regs->ebp); + _stp_sprintf (str, " DS: %04x ES: %04x\n", + 0xffff & regs->xds,0xffff & regs->xes); + + __asm__("movl %%cr0, %0": "=r" (cr0)); + __asm__("movl %%cr2, %0": "=r" (cr2)); + __asm__("movl %%cr3, %0": "=r" (cr3)); + /* This could fault if %cr4 does not exist */ + __asm__("1: movl %%cr4, %0 \n" + "2: \n" + ".section __ex_table,\"a\" \n" + ".long 1b,2b \n" + ".previous \n" + : "=r" (cr4): "0" (0)); + _stp_sprintf (str, "CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4); +} + +#endif + +/** Print the registers. + * @param regs The pt_regs saved by the kprobe. + * @note i386 and x86_64 only so far. + */ +#define _stp_print_regs(regs) \ + { \ + _stp_sprint_regs(_stp_stdout,regs); \ + _stp_print_flush(); \ + } /** @} */ #endif /* _CURRENT_C_ */ -- cgit