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