trace.c revision 5c3fe0697b202cc7d95e90459de0fb312b297b27
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 "ltrace.h"
12
13extern FILE *output;
14extern int opt_d;
15
16void get_arch_dep(struct process *proc)
17{
18	proc_archdep *a;
19	if (!proc->arch_ptr)
20		proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
21	a = (proc_archdep *)(proc->arch_ptr);
22	a->valid = (ptrace (PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
23}
24
25/* Returns syscall number if `pid' stopped because of a syscall.
26 * Returns -1 otherwise
27 */
28int syscall_p(struct process *proc, int status, int *sysnum)
29{
30	if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) {
31		void *ip = get_instruction_pointer(proc);
32		unsigned int insn;
33		if (ip == (void *)-1) return 0;
34		insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
35		if ((insn & 0xc1f8007f) == 0x81d00010) {
36			*sysnum = ((proc_archdep *)proc->arch_ptr)->regs.r_g1;
37			if ((proc->callstack_depth > 0) && proc->callstack[proc->callstack_depth-1].is_syscall) {
38				return 2;
39			} else if(*sysnum>=0) {
40				return 1;
41			}
42		}
43	}
44	return 0;
45}
46
47long gimme_arg(enum tof type, struct process * proc, int arg_num)
48{
49	proc_archdep * a = (proc_archdep *)proc->arch_ptr;
50	if (!a->valid) {
51		fprintf(stderr, "Could not get child registers\n");
52		exit(1);
53	}
54	if (arg_num==-1)		/* return value */
55		return a->regs.r_o0;
56
57	if (type==LT_TOF_FUNCTION || type==LT_TOF_SYSCALL || arg_num >= 6) {
58		if (arg_num < 6)
59			return ((int *)&a->regs.r_o0)[arg_num];
60		return ptrace(PTRACE_PEEKTEXT, proc->pid, 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 save_register_args(enum tof type, struct process * proc)
73{
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