trace.c revision 97f13e3fca546aa292ba5d8aa6011338e64f2f7a
1#include "config.h"
2
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <signal.h>
6#include <sys/ptrace.h>
7#include <asm/ptrace.h>
8
9#include "proc.h"
10#include "common.h"
11#include "debug.h"
12
13#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
14# define PTRACE_PEEKUSER PTRACE_PEEKUSR
15#endif
16
17#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
18# define PTRACE_POKEUSER PTRACE_POKEUSR
19#endif
20
21void
22get_arch_dep(Process *proc) {
23}
24
25/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
26 */
27int
28syscall_p(Process *proc, int status, int *sysnum) {
29	if (WIFSTOPPED(status)
30	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
31		char *ip = get_instruction_pointer(proc) - 4;
32		long x = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
33		debug(2, "instr: %016lx", x);
34		if ((x & 0xffffffff) != 0x00000083)
35			return 0;
36		*sysnum =
37		    ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
38		if (proc->callstack_depth > 0 &&
39		    proc->callstack[proc->callstack_depth - 1].is_syscall &&
40			proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
41			return 2;
42		}
43		if (*sysnum >= 0 && *sysnum < 500) {
44			return 1;
45		}
46	}
47	return 0;
48}
49
50long
51gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
52{
53	if (arg_num == -1) {	/* return value */
54		return ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
55	}
56
57	if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
58		if (arg_num <= 5)
59			return ptrace(PTRACE_PEEKUSER, proc->pid,
60				      arg_num + 16 /* REG_A0 */ , 0);
61		else
62			return ptrace(PTRACE_PEEKTEXT, proc->pid,
63				      proc->stack_pointer + 8 * (arg_num - 6),
64				      0);
65	} else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) {
66		return ptrace(PTRACE_PEEKUSER, proc->pid,
67			      arg_num + 16 /* REG_A0 */ , 0);
68	} else {
69		fprintf(stderr, "gimme_arg called with wrong arguments\n");
70		exit(1);
71	}
72	return 0;
73}
74