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