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