aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/linux-gnu/mipsel/trace.c
blob: 9ebd6382ea77f626c44f67296f918279ea5be3af (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "config.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <asm/ptrace.h>
#include <assert.h>

#include "backend.h"
#include "common.h"
#include "debug.h"
#include "mipsel.h"
#include "proc.h"
#include "type.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


/**
   \addtogroup mipsel Mipsel specific functions.

   These are the functions that it looks like I need to implement in
   order to get ltrace to work on our target.

   @{
 */

/**
   \param proc The process that had an event.

   Called by \c next_event() right after the return from wait.

   Most targets just return here. A couple use proc->arch_ptr for a
   private data area.
 */
void
get_arch_dep(Process *proc) {
}

/**
   \param proc Process that had event.
   \param status From \c wait()
   \param sysnum 0-based syscall number.
   \return 1 if syscall, 2 if sysret, 0 otherwise.

   Called by \c next_event() after the call to get_arch_dep()

   It seems that the ptrace call trips twice on a system call, once
   just before the system call and once when it returns. Both times,
   the pc points at the instruction just after the mipsel "syscall"
   instruction.

   There are several possiblities for system call sets, each is offset
   by a base from the others. On our system, it looks like the base
   for the system calls is 4000.
 */
int
syscall_p(Process *proc, int status, int *sysnum) {
	if (WIFSTOPPED(status)
			&& WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
		/* get the user's pc (plus 8) */
		long pc = (long)get_instruction_pointer(proc);
		/* fetch the SWI instruction */
		int insn = ptrace(PTRACE_PEEKTEXT, proc->pid, pc - 4, 0);
		int num = ptrace(PTRACE_PEEKTEXT, proc->pid, pc - 8, 0);

		/*
		   On a mipsel,  syscall looks like:
		   24040fa1    li v0, 0x0fa1   # 4001 --> _exit syscall
		   0000000c    syscall
		 */
		if(insn!=0x0000000c){
			return 0;
		}

		*sysnum = (num & 0xFFFF) - 4000;
		/* if it is a syscall, return 1 or 2 */
		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;
		}

		if (*sysnum >= 0) {
			return 1;
		}
	}
	return 0;
}

/* Based on GDB code.  */
#define mips32_op(x) (x >> 26)
#define itype_op(x) (x >> 26)
#define itype_rs(x) ((x >> 21) & 0x1f)
#define itype_rt(x) ((x >> 16) & 0x1f)
#define itype_immediate(x) (x & 0xffff)

#define jtype_op(x) (x >> 26)
#define jtype_target(x) (x & 0x03ffffff)

#define rtype_op(x) (x >> 26)
#define rtype_rs(x) ((x >> 21) & 0x1f)
#define rtype_rt(x) ((x >> 16) & 0x1f)
#define rtype_rd(x) ((x >> 11) & 0x1f)
#define rtype_shamt(x) ((x >> 6) & 0x1f)
#define rtype_funct(x) (x & 0x3f)

static int32_t
mips32_relative_offset (uint32_t inst)
{
  return ((itype_immediate(inst) ^ 0x8000) - 0x8000) << 2;
}

int mips_next_pcs(struct Process *proc, uint32_t pc, uint32_t *newpc)
{
	uint32_t inst, rx;
	int op;
	int rn;
	int nr = 0;

	inst = ptrace(PTRACE_PEEKTEXT, proc->pid, pc, 0);

	if ((inst & 0xe0000000) != 0) {
		/* Check for branches.  */
		if (itype_op(inst) >> 2 == 5) {
			/* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
			op = (itype_op(inst) & 0x03);
			switch (op)
			{
			case 0:	/* BEQL */
			case 1: /* BNEL */
			case 2:	/* BLEZL */
			case 3:	/* BGTZL */
				newpc[nr++] = pc + 8;
				newpc[nr++] = pc + 4 +
					mips32_relative_offset(inst);
				break;
			default:
				newpc[nr++] = pc + 4;
				break;
			}
		} else if (itype_op(inst) == 17 && itype_rs(inst) == 8) {
			/* Step over the branch.  */
			newpc[nr++] = pc + 8;
			newpc[nr++] = pc + mips32_relative_offset(inst) + 4;
		} else {
			newpc[nr++] = pc + 4;
		}
	} else {
		/* Further subdivide into SPECIAL, REGIMM and other.  */
		switch (op = itype_op(inst) & 0x07)
		{
		case 0:
			op = rtype_funct(inst);
			switch (op)
			{
			case 8:	/* JR  */
			case 9:	/* JALR  */
				rn = rtype_rs(inst);

				rx = ptrace(PTRACE_PEEKUSER,proc->pid, rn, 0);
				newpc[nr++] = rx;
				break;
			default:
			case 12:	/* SYSCALL  */
				newpc[nr++] = pc + 4;
				break;
			}
			break;
		case 1:
			op = itype_rt(inst);
			switch (op)
			{
				case 0:
				case 1:
				case 2:
				case 3:
				case 16:
				case 17:
				case 18:
				case 19:
					newpc[nr++] = pc + 8;
					newpc[nr++] = pc + 4 +
						mips32_relative_offset(inst);
					break;
				default:
					newpc[nr++] = pc + 4;
					break;
			}
			break;
		case 2:	/* J  */
		case 3:	/* JAL  */
			rx = jtype_target(inst) << 2;
			/* Upper four bits get never changed...  */
			newpc[nr++] = rx + ((pc + 4) & ~0x0fffffff);
			break;
		case 4: /* BEQ  */
			if (itype_rs(inst) == itype_rt(inst)) {
				/* Compare the same reg for equality, always
				 * follow the branch.  */
				newpc[nr++] = pc + 4 +
					mips32_relative_offset(inst);
				break;
			}
			/* Fall through.  */
		default:
		case 5:
		case 6:
		case 7:
			/* Step over the branch.  */
			newpc[nr++] = pc + 8;
			newpc[nr++] = pc + mips32_relative_offset(inst) + 4;
			break;
		}
	}
	if (nr <= 0 || nr > 2)
		goto fail;
	if (nr == 2) {
		if (newpc[1] == 0)
			goto fail;
	}
	if (newpc[0] == 0)
		goto fail;

	assert(nr == 1 || nr == 2);
	return nr;

fail:
	printf("nr=%d pc=%x\n", nr, pc);
	printf("pc=%x %x\n", newpc[0], newpc[1]);
	return 0;
}

int
arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
		       int (*add_cb)(void *addr, void *data),
		       void *add_cb_data)
{
	uint32_t pc = (uint32_t) get_instruction_pointer(proc);
	uint32_t newpcs[2];
	int nr;

	nr = mips_next_pcs(proc, pc, newpcs);

	while (nr-- > 0) {
		arch_addr_t baddr = (arch_addr_t) newpcs[nr];
		/* Not sure what to do here. We've already got a bp?  */
		if (dict_find_entry(proc->leader->breakpoints, baddr) != NULL) {
			fprintf(stderr, "skip %p %p\n", baddr, add_cb_data);
			continue;
		}

		if (add_cb(baddr, add_cb_data) < 0)
			return -1;
	}

	ptrace(PTRACE_SYSCALL, proc->pid, 0, 0);
	return 0;
}

/**
   \param type Function/syscall call or return.
   \param proc The process that had an event.
   \param arg_num -1 for return value,
   \return The argument to fetch.

   A couple of assumptions.

-  Type is LT_TOF_FUNCTIONR or LT_TOF_SYSCALLR if arg_num==-1. These
   types are only used in calls for output_right(), which only uses -1
   for arg_num.
-  Type is LT_TOF_FUNCTION or LT_TOF_SYSCALL for args 0...4.
-   I'm only displaying the first 4 args (Registers a0..a3). Good
   enough for now.

  Mipsel conventions seem to be:
- syscall parameters: r4...r9
- syscall return: if(!a3){ return v0;} else{ errno=v0;return -1;}
- function call: r4..r7. Not sure how to get arg number 5.
- function return: v0

The argument registers are wiped by a call, so it is a mistake to ask
for arguments on a return. If ltrace does this, we will need to cache
arguments somewhere on the call.

I'm not doing any floating point support here.

*/
long
gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
{
	long ret;
	long addr;
	debug(2,"type %d arg %d",type,arg_num);
	if (arg_num == -1) {
		if(type == LT_TOF_FUNCTIONR) {
			return  ptrace(PTRACE_PEEKUSER,proc->pid,off_v0,0);
		}
		if (type == LT_TOF_SYSCALLR) {
			unsigned a3=ptrace(PTRACE_PEEKUSER, proc->pid,off_a3,0);
			unsigned v0=ptrace(PTRACE_PEEKUSER, proc->pid,off_v0,0);
			if(!a3){
				return v0;
			}
			return -1;
		}
	}
	if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL) {
		/* o32: float args are in f12 and f14 */
		if ((info->type == ARGTYPE_FLOAT) && (arg_num < 2)) {
			ret=ptrace(PTRACE_PEEKUSER,proc->pid,off_fpr0+12+arg_num*2,0);
			debug(2,"ret = %#lx",ret);
			return ret;
		}
		if(arg_num <4){
			ret=ptrace(PTRACE_PEEKUSER,proc->pid,off_a0+arg_num,0);
			debug(2,"ret = %#lx",ret);
			return ret;
		} else {
			/* not sure it's going to work for something else than syscall */
			addr=ptrace(PTRACE_PEEKUSER,proc->pid,off_sp,0);
			if (addr == -1) {
				debug(2,"ret = %#lx",addr);
				return addr;
			}
			ret = addr + 4*arg_num;
			ret=ptrace(PTRACE_PEEKTEXT,proc->pid,addr,0);
			debug(2,"ret = %#lx",ret);
			return ret;
		}
	}
	if (type == LT_TOF_FUNCTIONR || type == LT_TOF_SYSCALLR){
		addr=ptrace(PTRACE_PEEKUSER,proc->pid,off_sp,0);
		if (addr == -1) {
			debug(2,"ret = %#lx",addr);
			return addr;
		}
		ret = addr + 4*arg_num;
		ret=ptrace(PTRACE_PEEKTEXT,proc->pid,addr,0);
		debug(2,"ret = %#lx",ret);
		return ret;
	}
	fprintf(stderr, "gimme_arg called with wrong arguments\n");
	return 0;
}

/**@}*/