| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 1 | #include "config.h" |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 2 | |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/wait.h> |
| 6 | #include <signal.h> |
| 7 | #include <string.h> |
| 8 | #include "ptrace.h" |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 9 | #include "proc.h" |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 10 | #include "common.h" |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 11 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 12 | void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 13 | get_arch_dep(Process *proc) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 14 | proc_archdep *a; |
| 15 | if (!proc->arch_ptr) |
| 16 | proc->arch_ptr = (void *)malloc(sizeof(proc_archdep)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 17 | a = (proc_archdep *) (proc->arch_ptr); |
| 18 | a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | /* Returns syscall number if `pid' stopped because of a syscall. |
| 22 | * Returns -1 otherwise |
| 23 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 24 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 25 | syscall_p(Process *proc, int status, int *sysnum) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | if (WIFSTOPPED(status) |
| 27 | && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 28 | void *ip = get_instruction_pointer(proc); |
| 29 | unsigned int insn; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | if (ip == (void *)-1) |
| 31 | return 0; |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 32 | insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0); |
| 33 | if ((insn & 0xc1f8007f) == 0x81d00010) { |
| Juan Cespedes | 3458456 | 2009-07-25 16:10:39 +0200 | [diff] [blame] | 34 | *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0]; |
| Juan Cespedes | 3e94cbf | 2009-05-22 19:12:07 +0200 | [diff] [blame] | 35 | if (proc->callstack_depth > 0 && |
| 36 | proc->callstack[proc->callstack_depth - 1].is_syscall && |
| 37 | proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 38 | return 2; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 39 | } else if (*sysnum >= 0) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 40 | return 1; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 47 | long |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 48 | gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 49 | proc_archdep *a = (proc_archdep *) proc->arch_ptr; |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 50 | if (!a->valid) { |
| 51 | fprintf(stderr, "Could not get child registers\n"); |
| 52 | exit(1); |
| 53 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 54 | if (arg_num == -1) /* return value */ |
| Juan Cespedes | 3458456 | 2009-07-25 16:10:39 +0200 | [diff] [blame] | 55 | return a->regs.u_regs[UREG_G7]; |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 56 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 57 | if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 58 | if (arg_num < 6) |
| Juan Cespedes | 3458456 | 2009-07-25 16:10:39 +0200 | [diff] [blame] | 59 | return ((int *)&a->regs.u_regs[UREG_G7])[arg_num]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 60 | return ptrace(PTRACE_PEEKTEXT, proc->pid, |
| 61 | proc->stack_pointer + 64 * (arg_num + 1)); |
| 62 | } else if (type == LT_TOF_FUNCTIONR) |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 63 | return a->func_arg[arg_num]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 64 | else if (type == LT_TOF_SYSCALLR) |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 65 | return a->sysc_arg[arg_num]; |
| 66 | else { |
| 67 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 68 | exit(1); |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 73 | void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 74 | save_register_args(enum tof type, Process *proc) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 75 | proc_archdep *a = (proc_archdep *) proc->arch_ptr; |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 76 | if (a->valid) { |
| 77 | if (type == LT_TOF_FUNCTION) |
| Juan Cespedes | 3458456 | 2009-07-25 16:10:39 +0200 | [diff] [blame] | 78 | memcpy(a->func_arg, &a->regs.u_regs[UREG_G7], sizeof(a->func_arg)); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 79 | else |
| Juan Cespedes | 3458456 | 2009-07-25 16:10:39 +0200 | [diff] [blame] | 80 | memcpy(a->sysc_arg, &a->regs.u_regs[UREG_G7], sizeof(a->sysc_arg)); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 81 | } |
| 82 | } |