trace.c revision 929bd57ca202fd2f2e8485ebf65d683e664f67b5
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 <sys/types.h>
25#include <sys/wait.h>
26#include <signal.h>
27#include <sys/ptrace.h>
28#include <asm/ptrace.h>
29
30#include "proc.h"
31#include "common.h"
32#include "debug.h"
33
34#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
35# define PTRACE_PEEKUSER PTRACE_PEEKUSR
36#endif
37
38#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
39# define PTRACE_POKEUSER PTRACE_POKEUSR
40#endif
41
42void
43get_arch_dep(struct process *proc)
44{
45}
46
47/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
48 */
49int
50syscall_p(struct process *proc, int status, int *sysnum)
51{
52	if (WIFSTOPPED(status)
53	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
54		char *ip = get_instruction_pointer(proc) - 4;
55		long x = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
56		debug(2, "instr: %016lx", x);
57		if ((x & 0xffffffff) != 0x00000083)
58			return 0;
59		*sysnum =
60		    ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
61		if (proc->callstack_depth > 0 &&
62		    proc->callstack[proc->callstack_depth - 1].is_syscall &&
63			proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
64			return 2;
65		}
66		if (*sysnum >= 0 && *sysnum < 500) {
67			return 1;
68		}
69	}
70	return 0;
71}
72
73long
74gimme_arg(enum tof type, struct process *proc, int arg_num,
75	  struct arg_type_info *info)
76{
77	if (arg_num == -1) {	/* return value */
78		return ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
79	}
80
81	if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
82		if (arg_num <= 5)
83			return ptrace(PTRACE_PEEKUSER, proc->pid,
84				      arg_num + 16 /* REG_A0 */ , 0);
85		else
86			return ptrace(PTRACE_PEEKTEXT, proc->pid,
87				      proc->stack_pointer + 8 * (arg_num - 6),
88				      0);
89	} else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) {
90		return ptrace(PTRACE_PEEKUSER, proc->pid,
91			      arg_num + 16 /* REG_A0 */ , 0);
92	} else {
93		fprintf(stderr, "gimme_arg called with wrong arguments\n");
94		exit(1);
95	}
96	return 0;
97}
98