trace.c revision 9a2ad351a1c3215dc596ff3e2e3fd4bc24445a6b
1#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <signal.h>
8#include <sys/ptrace.h>
9#include <asm/ptrace.h>
10
11#include "ltrace.h"
12
13#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
14# define PTRACE_PEEKUSER PTRACE_PEEKUSR
15#endif
16
17#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
18# define PTRACE_POKEUSER PTRACE_POKEUSR
19#endif
20
21/* syscall tracing protocol: ArmLinux
22 on the way in, ip is 0
23 on the way out, ip is non-zero
24*/
25#define off_r0 0
26#define off_ip 48
27#define off_pc 60
28
29void
30get_arch_dep(struct process * proc) {
31}
32
33/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
34 */
35int
36syscall_p(struct process * proc, int status, int * sysnum) {
37	if (WIFSTOPPED(status) && WSTOPSIG(status)==(SIGTRAP | proc->tracesysgood)) {
38		/* get the user's pc (plus 8) */
39		int pc = ptrace(PTRACE_PEEKUSER, proc->pid, off_pc, 0);
40		/* fetch the SWI instruction */
41		int insn = ptrace(PTRACE_PEEKTEXT, proc->pid, pc-4, 0) ;
42
43		*sysnum = insn & 0xFFFF;
44		/* if it is a syscall, return 1 or 2 */
45		if ((insn & 0xFFFF0000) == 0xef900000) {
46			return ptrace(PTRACE_PEEKUSER, proc->pid, off_ip, 0) ? 2 : 1;
47		}
48	}
49	return 0;
50}
51
52long
53gimme_arg(enum tof type, struct process * proc, int arg_num) {
54	if (arg_num==-1) {		/* return value */
55		return ptrace(PTRACE_PEEKUSER, proc->pid, off_r0, 0);
56	}
57
58	/* deal with the ARM calling conventions */
59	if (type==LT_TOF_FUNCTION || type==LT_TOF_FUNCTIONR) {
60		if (arg_num<4) {
61			return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
62		} else {
63			return ptrace(PTRACE_PEEKDATA, proc->pid, proc->stack_pointer+4*(arg_num-4), 0);
64		}
65	} else if (type==LT_TOF_SYSCALL || type==LT_TOF_SYSCALLR) {
66		if (arg_num<5) {
67			return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
68		} else {
69			return ptrace(PTRACE_PEEKDATA, proc->pid, proc->stack_pointer+4*(arg_num-5), 0);
70		}
71	} else {
72		fprintf(stderr, "gimme_arg called with wrong arguments\n");
73		exit(1);
74	}
75
76	return 0;
77}
78
79void
80save_register_args(enum tof type, struct process * proc) {
81}
82