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
12#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
13# define PTRACE_PEEKUSER PTRACE_PEEKUSR
14#endif
15
16#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
17# define PTRACE_POKEUSER PTRACE_POKEUSR
18#endif
19
20void
21get_arch_dep(Process *proc) {
22}
23
24/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
25 */
26int
27syscall_p(Process *proc, int status, int *sysnum) {
28	int depth;
29
30	if (WIFSTOPPED(status)
31	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
32		*sysnum = ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_ORIG_D0, 0);
33		if (*sysnum == -1)
34			return 0;
35		if (*sysnum >= 0) {
36			depth = proc->callstack_depth;
37			if (depth > 0 &&
38					proc->callstack[depth - 1].is_syscall &&
39					proc->callstack[depth - 1].c_un.syscall == *sysnum) {
40				return 2;
41			} else {
42				return 1;
43			}
44		}
45	}
46	return 0;
47}
48
49long
50gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
51{
52	if (arg_num == -1) {	/* return value */
53		return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D0, 0);
54	}
55
56	if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
57		return ptrace(PTRACE_PEEKTEXT, proc->pid,
58			      proc->stack_pointer + 4 * (arg_num + 1), 0);
59	} else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) {
60#if 0
61		switch (arg_num) {
62		case 0:
63			return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D1, 0);
64		case 1:
65			return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D2, 0);
66		case 2:
67			return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D3, 0);
68		case 3:
69			return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D4, 0);
70		case 4:
71			return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * PT_D5, 0);
72		default:
73			fprintf(stderr,
74				"gimme_arg called with wrong arguments\n");
75			exit(2);
76		}
77#else
78		/* That hack works on m68k, too */
79		return ptrace(PTRACE_PEEKUSER, proc->pid, 4 * arg_num, 0);
80#endif
81	} else {
82		fprintf(stderr, "gimme_arg called with wrong arguments\n");
83		exit(1);
84	}
85
86	return 0;
87}
88