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