trace.c revision f670eea50e959eeb9da53d70cad8d43c19494ef0
1#include "config.h"
2
3#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/wait.h>
6#include <signal.h>
7#include <string.h>
8#include "ptrace.h"
9#include "common.h"
10
11void
12get_arch_dep(Process *proc) {
13	proc_archdep *a;
14	if (!proc->arch_ptr)
15		proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
16	a = (proc_archdep *) (proc->arch_ptr);
17	a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
18}
19
20/* Returns syscall number if `pid' stopped because of a syscall.
21 * Returns -1 otherwise
22 */
23int
24syscall_p(Process *proc, int status, int *sysnum) {
25	if (WIFSTOPPED(status)
26	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
27		void *ip = get_instruction_pointer(proc);
28		unsigned int insn;
29		if (ip == (void *)-1)
30			return 0;
31		insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
32		if ((insn & 0xc1f8007f) == 0x81d00010) {
33			*sysnum = ((proc_archdep *) proc->arch_ptr)->regs.r_g1;
34			if (proc->callstack_depth > 0 &&
35					proc->callstack[proc->callstack_depth - 1].is_syscall &&
36					proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
37				return 2;
38			} else if (*sysnum >= 0) {
39				return 1;
40			}
41		}
42	}
43	return 0;
44}
45
46long
47gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info) {
48	proc_archdep *a = (proc_archdep *) proc->arch_ptr;
49	if (!a->valid) {
50		fprintf(stderr, "Could not get child registers\n");
51		exit(1);
52	}
53	if (arg_num == -1)	/* return value */
54		return a->regs.r_o0;
55
56	if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
57		if (arg_num < 6)
58			return ((int *)&a->regs.r_o0)[arg_num];
59		return ptrace(PTRACE_PEEKTEXT, proc->pid,
60			      proc->stack_pointer + 64 * (arg_num + 1));
61	} else if (type == LT_TOF_FUNCTIONR)
62		return a->func_arg[arg_num];
63	else if (type == LT_TOF_SYSCALLR)
64		return a->sysc_arg[arg_num];
65	else {
66		fprintf(stderr, "gimme_arg called with wrong arguments\n");
67		exit(1);
68	}
69	return 0;
70}
71
72void
73save_register_args(enum tof type, Process *proc) {
74	proc_archdep *a = (proc_archdep *) proc->arch_ptr;
75	if (a->valid) {
76		if (type == LT_TOF_FUNCTION)
77			memcpy(a->func_arg, &a->regs.r_o0, sizeof(a->func_arg));
78		else
79			memcpy(a->sysc_arg, &a->regs.r_o0, sizeof(a->sysc_arg));
80	}
81}
82