blob: 078d4064461b1c68bae7ef836b617499d2be2874 [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
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 Cespedes5c3fe062004-06-14 18:08:37 +020022#include "config.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +020023
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 Machata366c2f42012-02-09 19:34:36 +010030#include "proc.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020031#include "common.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +020032
Juan Cespedesf1350522008-12-16 18:19:58 +010033void
Petr Machata929bd572012-12-17 03:20:34 +010034get_arch_dep(struct process *proc)
35{
Juan Cespedes5c3fe062004-06-14 18:08:37 +020036 proc_archdep *a;
37 if (!proc->arch_ptr)
38 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 a = (proc_archdep *) (proc->arch_ptr);
40 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020041}
42
43/* Returns syscall number if `pid' stopped because of a syscall.
44 * Returns -1 otherwise
45 */
Juan Cespedesf1350522008-12-16 18:19:58 +010046int
Petr Machata929bd572012-12-17 03:20:34 +010047syscall_p(struct process *proc, int status, int *sysnum)
48{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 if (WIFSTOPPED(status)
50 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020051 void *ip = get_instruction_pointer(proc);
52 unsigned int insn;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010053 if (ip == (void *)-1)
54 return 0;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020055 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
56 if ((insn & 0xc1f8007f) == 0x81d00010) {
Juan Cespedes34584562009-07-25 16:10:39 +020057 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0];
Juan Cespedes3e94cbf2009-05-22 19:12:07 +020058 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 Cespedes5c3fe062004-06-14 18:08:37 +020061 return 2;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062 } else if (*sysnum >= 0) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020063 return 1;
64 }
65 }
66 }
67 return 0;
68}
69
Juan Cespedesf1350522008-12-16 18:19:58 +010070long
Petr Machata929bd572012-12-17 03:20:34 +010071gimme_arg(enum tof type, struct process *proc, int arg_num,
72 struct arg_type_info *info)
Petr Machata000e3112012-01-03 17:03:39 +010073{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020075 if (!a->valid) {
76 fprintf(stderr, "Could not get child registers\n");
77 exit(1);
78 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 if (arg_num == -1) /* return value */
Juan Cespedes34584562009-07-25 16:10:39 +020080 return a->regs.u_regs[UREG_G7];
Juan Cespedes5c3fe062004-06-14 18:08:37 +020081
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020083 if (arg_num < 6)
Juan Cespedes34584562009-07-25 16:10:39 +020084 return ((int *)&a->regs.u_regs[UREG_G7])[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 return ptrace(PTRACE_PEEKTEXT, proc->pid,
86 proc->stack_pointer + 64 * (arg_num + 1));
87 } else if (type == LT_TOF_FUNCTIONR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020088 return a->func_arg[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 else if (type == LT_TOF_SYSCALLR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020090 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}