aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/linux-gnu/ppc/trace.c
blob: 05993dee921c861501703990b1a35a58a0fc2df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "config.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <asm/ptrace.h>
#include <elf.h>
#include <errno.h>
#include <string.h>

#include "common.h"
#include "ptrace.h"

#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
# define PTRACE_PEEKUSER PTRACE_PEEKUSR
#endif

#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
# define PTRACE_POKEUSER PTRACE_POKEUSR
#endif

void
get_arch_dep(Process *proc) {
	if (proc->arch_ptr == NULL) {
		proc->arch_ptr = malloc(sizeof(proc_archdep));
#ifdef __powerpc64__
		proc->mask_32bit = (proc->e_machine == EM_PPC);
#endif
	}

	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
	a->valid = (ptrace(PTRACE_GETREGS, proc->pid, 0, &a->regs) >= 0)
		&& (ptrace(PTRACE_GETFPREGS, proc->pid, 0, &a->fpregs) >= 0);
}

#define SYSCALL_INSN   0x44000002

unsigned int greg = 3;
unsigned int freg = 1;

/* Returns 1 if syscall, 2 if sysret, 0 otherwise. */
int
syscall_p(Process *proc, int status, int *sysnum) {
	if (WIFSTOPPED(status)
	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
		long pc = (long)get_instruction_pointer(proc);
		int insn =
		    (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
				0);

		if (insn == SYSCALL_INSN) {
			*sysnum =
			    (int)ptrace(PTRACE_PEEKUSER, proc->pid,
					sizeof(long) * PT_R0, 0);
			if (proc->callstack_depth > 0 &&
					proc->callstack[proc->callstack_depth - 1].is_syscall &&
					proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
				return 2;
			}
			return 1;
		}
	}
	return 0;
}

static long
gimme_arg_regset(enum tof type, Process *proc, int arg_num, arg_type_info *info,
		 gregset_t *regs, fpregset_t *fpregs)
{
	union { long val; float fval; double dval; } cvt;

	if (info->type == ARGTYPE_FLOAT || info->type == ARGTYPE_DOUBLE) {
		if (freg <= 13 || (proc->mask_32bit && freg <= 8)) {
			double val = GET_FPREG(*fpregs, freg);

			if (info->type == ARGTYPE_FLOAT)
				cvt.fval = val;
			else
				cvt.dval = val;

			freg++;
			greg++;

			return cvt.val;
		}
	}
	else if (greg <= 10)
		return (*regs)[greg++];
	else {
#ifdef __powerpc64__
		if (proc->mask_32bit)
			return ptrace (PTRACE_PEEKDATA, proc->pid,
				       proc->stack_pointer + 8 +
				       sizeof (int) * (arg_num - 8), 0) >> 32;
		else
			return ptrace (PTRACE_PEEKDATA, proc->pid,
				       proc->stack_pointer + 112 +
				       sizeof (long) * (arg_num - 8), 0);
#else
		return ptrace (PTRACE_PEEKDATA, proc->pid,
			       proc->stack_pointer + 8 +
			       sizeof (long) * (arg_num - 8), 0);
#endif
	}

	return 0;
}

static long
gimme_retval(Process *proc, int arg_num, arg_type_info *info,
	     gregset_t *regs, fpregset_t *fpregs)
{
	union { long val; float fval; double dval; } cvt;
	if (info->type == ARGTYPE_FLOAT || info->type == ARGTYPE_DOUBLE) {
		double val = GET_FPREG(*fpregs, 1);

		if (info->type == ARGTYPE_FLOAT)
			cvt.fval = val;
		else
			cvt.dval = val;

		return cvt.val;
	}
	else 
		return (*regs)[3];
}

/* Grab functions arguments based on the PPC64 ABI.  */
long
gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info)
{
	proc_archdep *arch = (proc_archdep *)proc->arch_ptr;
	if (arch == NULL || !arch->valid)
		return -1;

	/* Check if we're entering a new function call to list parameters.  If
	   so, initialize the register control variables to keep track of where
	   the parameters were stored.  */
	if ((type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR)
	    && arg_num == 0) {
		/* Initialize the set of registrers for parameter passing.  */
		greg = 3;
		freg = 1;
	}


	if (type == LT_TOF_FUNCTIONR) {
		if (arg_num == -1)
			return gimme_retval(proc, arg_num, info,
					    &arch->regs, &arch->fpregs);
		else
			return gimme_arg_regset(type, proc, arg_num, info,
						&arch->regs_copy,
						&arch->fpregs_copy);
	}
	else
		return gimme_arg_regset(type, proc, arg_num, info,
					&arch->regs, &arch->fpregs);
}

void
save_register_args(enum tof type, Process *proc) {
	proc_archdep *arch = (proc_archdep *)proc->arch_ptr;
	if (arch == NULL || !arch->valid)
		return;

	memcpy(&arch->regs_copy, &arch->regs, sizeof(arch->regs));
	memcpy(&arch->fpregs_copy, &arch->fpregs, sizeof(arch->fpregs));
}

/* Read a single long from the process's memory address 'addr'.  */
int
arch_umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
	long pointed_to;

	errno = 0;

	pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);

	if (pointed_to == -1 && errno)
		return -errno;

#if SIZEOF_LONG == 8
	/* Since int's are 4-bytes (long is 8-bytes) in length for ppc64, we
	   need to shift the long values returned by ptrace to end up with
	   the correct value.  */

	if (info) {
		if (info->type == ARGTYPE_INT || (proc->mask_32bit && (info->type == ARGTYPE_POINTER
		    || info->type == ARGTYPE_STRING))) {
			pointed_to = (long) (((unsigned long) pointed_to) >> 32);
		}
	}
#endif

	*result = pointed_to;
	return 0;
}

/* The atomic skip code is mostly taken from GDB.  */

/* Instruction masks used during single-stepping of atomic
 * sequences.  This was lifted from GDB.  */
#define LWARX_MASK 0xfc0007fe
#define LWARX_INSTRUCTION 0x7c000028
#define LDARX_INSTRUCTION 0x7c0000A8
#define STWCX_MASK 0xfc0007ff
#define STWCX_INSTRUCTION 0x7c00012d
#define STDCX_INSTRUCTION 0x7c0001ad
#define BC_MASK 0xfc000000
#define BC_INSTRUCTION 0x40000000

int
arch_atomic_singlestep(struct Process *proc, Breakpoint *sbp,
		       int (*add_cb)(void *addr, void *data),
		       void *add_cb_data)
{
	void *addr = sbp->addr;
	debug(1, "pid=%d addr=%p", proc->pid, addr);

	/* If the original instruction was lwarx/ldarx, we can't
	 * single-step over it, instead we have to execute the whole
	 * atomic block at once.  */
	union {
		uint32_t insn;
		char buf[4];
	} u;
	memcpy(u.buf, sbp->orig_value, BREAKPOINT_LENGTH);

	if ((u.insn & LWARX_MASK) != LWARX_INSTRUCTION
	    && (u.insn & LWARX_MASK) != LDARX_INSTRUCTION)
		return 1;

	int insn_count;
	for (insn_count = 0; ; ++insn_count) {
		addr += 4;
		unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
		if (l == (unsigned long)-1 && errno)
			return -1;
		uint32_t insn;
#ifdef __powerpc64__
		insn = l >> 32;
#else
		insn = l;
#endif

		/* If we hit a branch instruction, give up.  The
		 * computation could escape that way and we'd have to
		 * treat that case specially.  */
		if ((insn & BC_MASK) == BC_INSTRUCTION) {
			debug(1, "pid=%d, found branch at %p, giving up",
			      proc->pid, addr);
			return -1;
		}

		if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
		    || (insn & STWCX_MASK) == STDCX_INSTRUCTION) {
			debug(1, "pid=%d, found end of atomic block at %p",
			      proc->pid, addr);
			break;
		}

		/* Arbitrary cut-off.  If we didn't find the
		 * terminating instruction by now, just give up.  */
		if (insn_count > 16) {
			debug(1, "pid=%d, couldn't find end of atomic block",
			      proc->pid);
			return -1;
		}
	}

	/* Put the breakpoint to the next instruction.  */
	addr += 4;
	if (add_cb(addr, add_cb_data) < 0)
		return -1;

	debug(1, "PTRACE_CONT");
	ptrace(PTRACE_CONT, proc->pid, 0, 0);
	return 0;
}