trace.c revision e99af270a60891e68d465c4cd97dbe29cd1a05e4
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2004,2008,2009 Juan Cespedes 4 * Copyright (C) 2006 Ian Wienand 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 */ 21 22#include "config.h" 23 24#include <stdlib.h> 25#include <sys/types.h> 26#include <sys/wait.h> 27#include <signal.h> 28#include <string.h> 29#include "ptrace.h" 30#include "proc.h" 31#include "common.h" 32 33void 34get_arch_dep(Process *proc) { 35 proc_archdep *a; 36 if (!proc->arch_ptr) 37 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep)); 38 a = (proc_archdep *) (proc->arch_ptr); 39 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0); 40} 41 42/* Returns syscall number if `pid' stopped because of a syscall. 43 * Returns -1 otherwise 44 */ 45int 46syscall_p(Process *proc, int status, int *sysnum) { 47 if (WIFSTOPPED(status) 48 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { 49 void *ip = get_instruction_pointer(proc); 50 unsigned int insn; 51 if (ip == (void *)-1) 52 return 0; 53 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0); 54 if ((insn & 0xc1f8007f) == 0x81d00010) { 55 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0]; 56 if (proc->callstack_depth > 0 && 57 proc->callstack[proc->callstack_depth - 1].is_syscall && 58 proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) { 59 return 2; 60 } else if (*sysnum >= 0) { 61 return 1; 62 } 63 } 64 } 65 return 0; 66} 67 68long 69gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info) 70{ 71 proc_archdep *a = (proc_archdep *) proc->arch_ptr; 72 if (!a->valid) { 73 fprintf(stderr, "Could not get child registers\n"); 74 exit(1); 75 } 76 if (arg_num == -1) /* return value */ 77 return a->regs.u_regs[UREG_G7]; 78 79 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) { 80 if (arg_num < 6) 81 return ((int *)&a->regs.u_regs[UREG_G7])[arg_num]; 82 return ptrace(PTRACE_PEEKTEXT, proc->pid, 83 proc->stack_pointer + 64 * (arg_num + 1)); 84 } else if (type == LT_TOF_FUNCTIONR) 85 return a->func_arg[arg_num]; 86 else if (type == LT_TOF_SYSCALLR) 87 return a->sysc_arg[arg_num]; 88 else { 89 fprintf(stderr, "gimme_arg called with wrong arguments\n"); 90 exit(1); 91 } 92 return 0; 93} 94