trace.c revision 65b53df7fa2577c4138aef86c115873eab684a0a
1#if HAVE_CONFIG_H 2#include "config.h" 3#endif 4 5#include <stdlib.h> 6#include <sys/types.h> 7#include <sys/wait.h> 8#include <signal.h> 9#include <string.h> 10#include "ptrace.h" 11#include "ltrace.h" 12 13extern FILE *output; 14extern int opt_d; 15 16void get_arch_dep(struct process *proc) 17{ 18 proc_archdep *a; 19 if (!proc->arch_ptr) 20 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep)); 21 a = (proc_archdep *) (proc->arch_ptr); 22 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0); 23} 24 25/* Returns syscall number if `pid' stopped because of a syscall. 26 * Returns -1 otherwise 27 */ 28int syscall_p(struct process *proc, int status, int *sysnum) 29{ 30 if (WIFSTOPPED(status) 31 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { 32 void *ip = get_instruction_pointer(proc); 33 unsigned int insn; 34 if (ip == (void *)-1) 35 return 0; 36 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0); 37 if ((insn & 0xc1f8007f) == 0x81d00010) { 38 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.r_g1; 39 if ((proc->callstack_depth > 0) 40 && proc->callstack[proc->callstack_depth - 41 1].is_syscall) { 42 return 2; 43 } else if (*sysnum >= 0) { 44 return 1; 45 } 46 } 47 } 48 return 0; 49} 50 51long gimme_arg(enum tof type, struct process *proc, arg_type_info *info) 52{ 53 int arg_num = info->arg_num; 54 55 proc_archdep *a = (proc_archdep *) proc->arch_ptr; 56 if (!a->valid) { 57 fprintf(stderr, "Could not get child registers\n"); 58 exit(1); 59 } 60 if (arg_num == -1) /* return value */ 61 return a->regs.r_o0; 62 63 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) { 64 if (arg_num < 6) 65 return ((int *)&a->regs.r_o0)[arg_num]; 66 return ptrace(PTRACE_PEEKTEXT, proc->pid, 67 proc->stack_pointer + 64 * (arg_num + 1)); 68 } else if (type == LT_TOF_FUNCTIONR) 69 return a->func_arg[arg_num]; 70 else if (type == LT_TOF_SYSCALLR) 71 return a->sysc_arg[arg_num]; 72 else { 73 fprintf(stderr, "gimme_arg called with wrong arguments\n"); 74 exit(1); 75 } 76 return 0; 77} 78 79void save_register_args(enum tof type, struct process *proc) 80{ 81 proc_archdep *a = (proc_archdep *) proc->arch_ptr; 82 if (a->valid) { 83 if (type == LT_TOF_FUNCTION) 84 memcpy(a->func_arg, &a->regs.r_o0, sizeof(a->func_arg)); 85 else 86 memcpy(a->sysc_arg, &a->regs.r_o0, sizeof(a->sysc_arg)); 87 } 88} 89