trace.c revision 000e31195ad4ad30a0c80c93ab57a424e7d8d918
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 "proc.h"
10#include "common.h"
11
12void
13get_arch_dep(Process *proc) {
14	proc_archdep *a;
15	if (!proc->arch_ptr)
16		proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
17	a = (proc_archdep *) (proc->arch_ptr);
18	a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
19}
20
21/* Returns syscall number if `pid' stopped because of a syscall.
22 * Returns -1 otherwise
23 */
24int
25syscall_p(Process *proc, int status, int *sysnum) {
26	if (WIFSTOPPED(status)
27	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
28		void *ip = get_instruction_pointer(proc);
29		unsigned int insn;
30		if (ip == (void *)-1)
31			return 0;
32		insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
33		if ((insn & 0xc1f8007f) == 0x81d00010) {
34			*sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0];
35			if (proc->callstack_depth > 0 &&
36					proc->callstack[proc->callstack_depth - 1].is_syscall &&
37					proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
38				return 2;
39			} else if (*sysnum >= 0) {
40				return 1;
41			}
42		}
43	}
44	return 0;
45}
46
47long
48gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
49{
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.u_regs[UREG_G7];
57
58	if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
59		if (arg_num < 6)
60			return ((int *)&a->regs.u_regs[UREG_G7])[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.u_regs[UREG_G7], sizeof(a->func_arg));
80		else
81			memcpy(a->sysc_arg, &a->regs.u_regs[UREG_G7], sizeof(a->sysc_arg));
82	}
83}
84