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 
22 #include "config.h"
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"
30 #include "proc.h"
31 #include "common.h"
32 
33 void
get_arch_dep(struct process * proc)34 get_arch_dep(struct process *proc)
35 {
36 	proc_archdep *a;
37 	if (!proc->arch_ptr)
38 		proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
39 	a = (proc_archdep *) (proc->arch_ptr);
40 	a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
41 }
42 
43 /* Returns syscall number if `pid' stopped because of a syscall.
44  * Returns -1 otherwise
45  */
46 int
syscall_p(struct process * proc,int status,int * sysnum)47 syscall_p(struct process *proc, int status, int *sysnum)
48 {
49 	if (WIFSTOPPED(status)
50 	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
51 		void *ip = get_instruction_pointer(proc);
52 		unsigned int insn;
53 		if (ip == (void *)-1)
54 			return 0;
55 		insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
56 		if ((insn & 0xc1f8007f) == 0x81d00010) {
57 			*sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0];
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) {
61 				return 2;
62 			} else if (*sysnum >= 0) {
63 				return 1;
64 			}
65 		}
66 	}
67 	return 0;
68 }
69 
70 long
gimme_arg(enum tof type,struct process * proc,int arg_num,struct arg_type_info * info)71 gimme_arg(enum tof type, struct process *proc, int arg_num,
72 	  struct arg_type_info *info)
73 {
74 	proc_archdep *a = (proc_archdep *) proc->arch_ptr;
75 	if (!a->valid) {
76 		fprintf(stderr, "Could not get child registers\n");
77 		exit(1);
78 	}
79 	if (arg_num == -1)	/* return value */
80 		return a->regs.u_regs[UREG_G7];
81 
82 	if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
83 		if (arg_num < 6)
84 			return ((int *)&a->regs.u_regs[UREG_G7])[arg_num];
85 		return ptrace(PTRACE_PEEKTEXT, proc->pid,
86 			      proc->stack_pointer + 64 * (arg_num + 1));
87 	} else if (type == LT_TOF_FUNCTIONR)
88 		return a->func_arg[arg_num];
89 	else if (type == LT_TOF_SYSCALLR)
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 }
97