trace.c revision e99af270a60891e68d465c4cd97dbe29cd1a05e4
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(Process *proc) {
44}
45
46/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
47 */
48int
49syscall_p(Process *proc, int status, int *sysnum) {
50	if (WIFSTOPPED(status)
51	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
52		char *ip = get_instruction_pointer(proc) - 4;
53		long x = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
54		debug(2, "instr: %016lx", x);
55		if ((x & 0xffffffff) != 0x00000083)
56			return 0;
57		*sysnum =
58		    ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
59		if (proc->callstack_depth > 0 &&
60		    proc->callstack[proc->callstack_depth - 1].is_syscall &&
61			proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
62			return 2;
63		}
64		if (*sysnum >= 0 && *sysnum < 500) {
65			return 1;
66		}
67	}
68	return 0;
69}
70
71long
72gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
73{
74	if (arg_num == -1) {	/* return value */
75		return ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
76	}
77
78	if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
79		if (arg_num <= 5)
80			return ptrace(PTRACE_PEEKUSER, proc->pid,
81				      arg_num + 16 /* REG_A0 */ , 0);
82		else
83			return ptrace(PTRACE_PEEKTEXT, proc->pid,
84				      proc->stack_pointer + 8 * (arg_num - 6),
85				      0);
86	} else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) {
87		return ptrace(PTRACE_PEEKUSER, proc->pid,
88			      arg_num + 16 /* REG_A0 */ , 0);
89	} else {
90		fprintf(stderr, "gimme_arg called with wrong arguments\n");
91		exit(1);
92	}
93	return 0;
94}
95