xref: /freebsd/sys/arm/arm/db_trace.c (revision 55bce0c1203e70d8b62a3dedc9235ab39660c6f4)
1 /*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001 Ben Harris
5  * Copyright (c) 1996 Scott K. Stevens
6  *
7  * Mach Operating System
8  * Copyright (c) 1991,1990 Carnegie Mellon University
9  * All Rights Reserved.
10  *
11  * Permission to use, copy, modify and distribute this software and its
12  * documentation is hereby granted, provided that both the copyright
13  * notice and this permission notice appear in all copies of the
14  * software, derivative works or modified versions, and any portions
15  * thereof, and that both notices appear in supporting documentation.
16  *
17  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20  *
21  * Carnegie Mellon requests users of this software to return to
22  *
23  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
24  *  School of Computer Science
25  *  Carnegie Mellon University
26  *  Pittsburgh PA 15213-3890
27  *
28  * any improvements or extensions that they make and grant Carnegie the
29  * rights to redistribute these changes.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 
37 
38 #include <sys/proc.h>
39 #include <sys/kdb.h>
40 #include <sys/stack.h>
41 #include <machine/armreg.h>
42 #include <machine/asm.h>
43 #include <machine/cpufunc.h>
44 #include <machine/db_machdep.h>
45 #include <machine/pcb.h>
46 #include <machine/stack.h>
47 #include <machine/vmparam.h>
48 #include <ddb/ddb.h>
49 #include <ddb/db_access.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_output.h>
52 
53 #ifdef __ARM_EABI__
54 /*
55  * Definitions for the instruction interpreter.
56  *
57  * The ARM EABI specifies how to perform the frame unwinding in the
58  * Exception Handling ABI for the ARM Architecture document. To perform
59  * the unwind we need to know the initial frame pointer, stack pointer,
60  * link register and program counter. We then find the entry within the
61  * index table that points to the function the program counter is within.
62  * This gives us either a list of three instructions to process, a 31-bit
63  * relative offset to a table of instructions, or a value telling us
64  * we can't unwind any further.
65  *
66  * When we have the instructions to process we need to decode them
67  * following table 4 in section 9.3. This describes a collection of bit
68  * patterns to encode that steps to take to update the stack pointer and
69  * link register to the correct values at the start of the function.
70  */
71 
72 /* A special case when we are unable to unwind past this function */
73 #define	EXIDX_CANTUNWIND	1
74 
75 /* The register names */
76 #define	FP	11
77 #define	SP	13
78 #define	LR	14
79 #define	PC	15
80 
81 /*
82  * These are set in the linker script. Their addresses will be
83  * either the start or end of the exception table or index.
84  */
85 extern int extab_start, extab_end, exidx_start, exidx_end;
86 
87 /*
88  * Entry types.
89  * These are the only entry types that have been seen in the kernel.
90  */
91 #define	ENTRY_MASK	0xff000000
92 #define	ENTRY_ARM_SU16	0x80000000
93 #define	ENTRY_ARM_LU16	0x81000000
94 
95 /* Instruction masks. */
96 #define	INSN_VSP_MASK		0xc0
97 #define	INSN_VSP_SIZE_MASK	0x3f
98 #define	INSN_STD_MASK		0xf0
99 #define	INSN_STD_DATA_MASK	0x0f
100 #define	INSN_POP_TYPE_MASK	0x08
101 #define	INSN_POP_COUNT_MASK	0x07
102 #define	INSN_VSP_LARGE_INC_MASK	0xff
103 
104 /* Instruction definitions */
105 #define	INSN_VSP_INC		0x00
106 #define	INSN_VSP_DEC		0x40
107 #define	INSN_POP_MASKED		0x80
108 #define	INSN_VSP_REG		0x90
109 #define	INSN_POP_COUNT		0xa0
110 #define	INSN_FINISH		0xb0
111 #define	INSN_VSP_LARGE_INC	0xb2
112 
113 /* An item in the exception index table */
114 struct unwind_idx {
115 	uint32_t offset;
116 	uint32_t insn;
117 };
118 
119 /* The state of the unwind process */
120 struct unwind_state {
121 	uint32_t registers[16];
122 	uint32_t start_pc;
123 	uint32_t *insn;
124 	u_int entries;
125 	u_int byte;
126 	uint16_t update_mask;
127 };
128 
129 /* Expand a 31-bit signed value to a 32-bit signed value */
130 static __inline int32_t
131 db_expand_prel31(uint32_t prel31)
132 {
133 
134 	return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
135 }
136 
137 /*
138  * Perform a binary search of the index table to find the function
139  * with the largest address that doesn't exceed addr.
140  */
141 static struct unwind_idx *
142 db_find_index(uint32_t addr)
143 {
144 	unsigned int min, mid, max;
145 	struct unwind_idx *start;
146 	struct unwind_idx *item;
147 	int32_t prel31_addr;
148 	uint32_t func_addr;
149 
150 	start = (struct unwind_idx *)&exidx_start;
151 
152 	min = 0;
153 	max = (&exidx_end - &exidx_start) / 2;
154 
155 	while (min != max) {
156 		mid = min + (max - min + 1) / 2;
157 
158 		item = &start[mid];
159 
160 	 	prel31_addr = db_expand_prel31(item->offset);
161 		func_addr = (uint32_t)&item->offset + prel31_addr;
162 
163 		if (func_addr <= addr) {
164 			min = mid;
165 		} else {
166 			max = mid - 1;
167 		}
168 	}
169 
170 	return &start[min];
171 }
172 
173 /* Reads the next byte from the instruction list */
174 static uint8_t
175 db_unwind_exec_read_byte(struct unwind_state *state)
176 {
177 	uint8_t insn;
178 
179 	/* Read the unwind instruction */
180 	insn = (*state->insn) >> (state->byte * 8);
181 
182 	/* Update the location of the next instruction */
183 	if (state->byte == 0) {
184 		state->byte = 3;
185 		state->insn++;
186 		state->entries--;
187 	} else
188 		state->byte--;
189 
190 	return insn;
191 }
192 
193 /* Executes the next instruction on the list */
194 static int
195 db_unwind_exec_insn(struct unwind_state *state)
196 {
197 	unsigned int insn;
198 	uint32_t *vsp = (uint32_t *)state->registers[SP];
199 	int update_vsp = 0;
200 
201 	/* This should never happen */
202 	if (state->entries == 0)
203 		return 1;
204 
205 	/* Read the next instruction */
206 	insn = db_unwind_exec_read_byte(state);
207 
208 	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
209 		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
210 
211 	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
212 		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
213 
214 	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
215 		unsigned int mask, reg;
216 
217 		/* Load the mask */
218 		mask = db_unwind_exec_read_byte(state);
219 		mask |= (insn & INSN_STD_DATA_MASK) << 8;
220 
221 		/* We have a refuse to unwind instruction */
222 		if (mask == 0)
223 			return 1;
224 
225 		/* Update SP */
226 		update_vsp = 1;
227 
228 		/* Load the registers */
229 		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
230 			if (mask & 1) {
231 				state->registers[reg] = *vsp++;
232 				state->update_mask |= 1 << reg;
233 
234 				/* If we have updated SP kep its value */
235 				if (reg == SP)
236 					update_vsp = 0;
237 			}
238 		}
239 
240 	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
241 	    ((insn & INSN_STD_DATA_MASK) != 13) &&
242 	    ((insn & INSN_STD_DATA_MASK) != 15)) {
243 		/* sp = register */
244 		state->registers[SP] =
245 		    state->registers[insn & INSN_STD_DATA_MASK];
246 
247 	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
248 		unsigned int count, reg;
249 
250 		/* Read how many registers to load */
251 		count = insn & INSN_POP_COUNT_MASK;
252 
253 		/* Update sp */
254 		update_vsp = 1;
255 
256 		/* Pop the registers */
257 		for (reg = 4; reg <= 4 + count; reg++) {
258 			state->registers[reg] = *vsp++;
259 			state->update_mask |= 1 << reg;
260 		}
261 
262 		/* Check if we are in the pop r14 version */
263 		if ((insn & INSN_POP_TYPE_MASK) != 0) {
264 			state->registers[14] = *vsp++;
265 		}
266 
267 	} else if (insn == INSN_FINISH) {
268 		/* Stop processing */
269 		state->entries = 0;
270 
271 	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
272 		unsigned int uleb128;
273 
274 		/* Read the increment value */
275 		uleb128 = db_unwind_exec_read_byte(state);
276 
277 		state->registers[SP] += 0x204 + (uleb128 << 2);
278 
279 	} else {
280 		/* We hit a new instruction that needs to be implemented */
281 		db_printf("Unhandled instruction %.2x\n", insn);
282 		return 1;
283 	}
284 
285 	if (update_vsp) {
286 		state->registers[SP] = (uint32_t)vsp;
287 	}
288 
289 #if 0
290 	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
291 	    state->registers[FP], state->registers[SP], state->registers[LR],
292 	    state->registers[PC]);
293 #endif
294 
295 	return 0;
296 }
297 
298 /* Performs the unwind of a function */
299 static int
300 db_unwind_tab(struct unwind_state *state)
301 {
302 	uint32_t entry;
303 
304 	/* Set PC to a known value */
305 	state->registers[PC] = 0;
306 
307 	/* Read the personality */
308 	entry = *state->insn & ENTRY_MASK;
309 
310 	if (entry == ENTRY_ARM_SU16) {
311 		state->byte = 2;
312 		state->entries = 1;
313 	} else if (entry == ENTRY_ARM_LU16) {
314 		state->byte = 1;
315 		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
316 	} else {
317 		db_printf("Unknown entry: %x\n", entry);
318 		return 1;
319 	}
320 
321 	while (state->entries > 0) {
322 		if (db_unwind_exec_insn(state) != 0)
323 			return 1;
324 	}
325 
326 	/*
327 	 * The program counter was not updated, load it from the link register.
328 	 */
329 	if (state->registers[PC] == 0)
330 		state->registers[PC] = state->registers[LR];
331 
332 	return 0;
333 }
334 
335 static void
336 db_stack_trace_cmd(struct unwind_state *state)
337 {
338 	struct unwind_idx *index;
339 	const char *name;
340 	db_expr_t value;
341 	db_expr_t offset;
342 	c_db_sym_t sym;
343 	u_int reg, i;
344 	char *sep;
345 	uint16_t upd_mask;
346 	bool finished;
347 
348 	finished = false;
349 	while (!finished) {
350 		/* Reset the mask of updated registers */
351 		state->update_mask = 0;
352 
353 		/* The pc value is correct and will be overwritten, save it */
354 		state->start_pc = state->registers[PC];
355 
356 		/* Find the item to run */
357 		index = db_find_index(state->start_pc);
358 
359 		if (index->insn != EXIDX_CANTUNWIND) {
360 			if (index->insn & (1 << 31)) {
361 				/* The data is within the instruction */
362 				state->insn = &index->insn;
363 			} else {
364 				/* A prel31 offset to the unwind table */
365 				state->insn = (uint32_t *)
366 				    ((uintptr_t)&index->insn +
367 				     db_expand_prel31(index->insn));
368 			}
369 			/* Run the unwind function */
370 			finished = db_unwind_tab(state);
371 		}
372 
373 		/* Print the frame details */
374 		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
375 		if (sym == C_DB_SYM_NULL) {
376 			value = 0;
377 			name = "(null)";
378 		} else
379 			db_symbol_values(sym, &name, &value);
380 		db_printf("%s() at ", name);
381 		db_printsym(state->start_pc, DB_STGY_PROC);
382 		db_printf("\n");
383 		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
384 		    state->registers[LR]);
385 		db_printsym(state->registers[LR], DB_STGY_PROC);
386 		db_printf(")\n");
387 		db_printf("\t sp = 0x%08x  fp = 0x%08x",
388 		    state->registers[SP], state->registers[FP]);
389 
390 		/* Don't print the registers we have already printed */
391 		upd_mask = state->update_mask &
392 		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
393 		sep = "\n\t";
394 		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
395 			if ((upd_mask & 1) != 0) {
396 				db_printf("%s%sr%d = 0x%08x", sep,
397 				    (reg < 10) ? " " : "", reg,
398 				    state->registers[reg]);
399 				i++;
400 				if (i == 2) {
401 					sep = "\n\t";
402 					i = 0;
403 				} else
404 					sep = " ";
405 
406 			}
407 		}
408 		db_printf("\n");
409 
410 		/*
411 		 * Stop if directed to do so, or if we've unwound back to the
412 		 * kernel entry point, or if the unwind function didn't change
413 		 * anything (to avoid getting stuck in this loop forever).
414 		 * If the latter happens, it's an indication that the unwind
415 		 * information is incorrect somehow for the function named in
416 		 * the last frame printed before you see the unwind failure
417 		 * message (maybe it needs a STOP_UNWINDING).
418 		 */
419 		if (index->insn == EXIDX_CANTUNWIND) {
420 			db_printf("Unable to unwind further\n");
421 			finished = true;
422 		} else if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
423 			db_printf("Unable to unwind into user mode\n");
424 			finished = true;
425 		} else if (state->update_mask == 0) {
426 			db_printf("Unwind failure (no registers changed)\n");
427 			finished = true;
428 		}
429 	}
430 }
431 #endif
432 
433 /*
434  * APCS stack frames are awkward beasts, so I don't think even trying to use
435  * a structure to represent them is a good idea.
436  *
437  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
438  *
439  *          save code pointer       [fp]        <- fp points to here
440  *          return link value       [fp, #-4]
441  *          return sp value         [fp, #-8]
442  *          return fp value         [fp, #-12]
443  *          [saved v7 value]
444  *          [saved v6 value]
445  *          [saved v5 value]
446  *          [saved v4 value]
447  *          [saved v3 value]
448  *          [saved v2 value]
449  *          [saved v1 value]
450  *          [saved a4 value]
451  *          [saved a3 value]
452  *          [saved a2 value]
453  *          [saved a1 value]
454  *
455  * The save code pointer points twelve bytes beyond the start of the
456  * code sequence (usually a single STM) that created the stack frame.
457  * We have to disassemble it if we want to know which of the optional
458  * fields are actually present.
459  */
460 
461 #ifndef __ARM_EABI__	/* The frame format is differend in AAPCS */
462 static void
463 db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
464 {
465 	u_int32_t	*frame, *lastframe;
466 	c_db_sym_t sym;
467 	const char *name;
468 	db_expr_t value;
469 	db_expr_t offset;
470 	int	scp_offset;
471 
472 	frame = (u_int32_t *)addr;
473 	lastframe = NULL;
474 	scp_offset = -(get_pc_str_offset() >> 2);
475 
476 	while (count-- && frame != NULL && !db_pager_quit) {
477 		db_addr_t	scp;
478 		u_int32_t	savecode;
479 		int		r;
480 		u_int32_t	*rp;
481 		const char	*sep;
482 
483 		/*
484 		 * In theory, the SCP isn't guaranteed to be in the function
485 		 * that generated the stack frame.  We hope for the best.
486 		 */
487 		scp = frame[FR_SCP];
488 
489 		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
490 		if (sym == C_DB_SYM_NULL) {
491 			value = 0;
492 			name = "(null)";
493 		} else
494 			db_symbol_values(sym, &name, &value);
495 		db_printf("%s() at ", name);
496 		db_printsym(scp, DB_STGY_PROC);
497 		db_printf("\n");
498 #ifdef __PROG26
499 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
500 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
501 		db_printf(")\n");
502 #else
503 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
504 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
505 		db_printf(")\n");
506 #endif
507 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
508 
509 		savecode = ((u_int32_t *)scp)[scp_offset];
510 		if ((savecode & 0x0e100000) == 0x08000000) {
511 			/* Looks like an STM */
512 			rp = frame - 4;
513 			sep = "\n\t";
514 			for (r = 10; r >= 0; r--) {
515 				if (savecode & (1 << r)) {
516 					db_printf("%sr%d=0x%08x",
517 					    sep, r, *rp--);
518 					sep = (frame - rp) % 4 == 2 ?
519 					    "\n\t" : " ";
520 				}
521 			}
522 		}
523 
524 		db_printf("\n");
525 
526 		/*
527 		 * Switch to next frame up
528 		 */
529 		if (frame[FR_RFP] == 0)
530 			break; /* Top of stack */
531 
532 		lastframe = frame;
533 		frame = (u_int32_t *)(frame[FR_RFP]);
534 
535 		if (INKERNEL((int)frame)) {
536 			/* staying in kernel */
537 			if (frame <= lastframe) {
538 				db_printf("Bad frame pointer: %p\n", frame);
539 				break;
540 			}
541 		} else if (INKERNEL((int)lastframe)) {
542 			/* switch from user to kernel */
543 			if (kernel_only)
544 				break;	/* kernel stack only */
545 		} else {
546 			/* in user */
547 			if (frame <= lastframe) {
548 				db_printf("Bad user frame pointer: %p\n",
549 					  frame);
550 				break;
551 			}
552 		}
553 	}
554 }
555 #endif
556 
557 /* XXX stubs */
558 void
559 db_md_list_watchpoints()
560 {
561 }
562 
563 int
564 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
565 {
566 	return (0);
567 }
568 
569 int
570 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
571 {
572 	return (0);
573 }
574 
575 int
576 db_trace_thread(struct thread *thr, int count)
577 {
578 #ifdef __ARM_EABI__
579 	struct unwind_state state;
580 #endif
581 	struct pcb *ctx;
582 
583 	if (thr != curthread) {
584 		ctx = kdb_thr_ctx(thr);
585 
586 #ifdef __ARM_EABI__
587 		state.registers[FP] = ctx->un_32.pcb32_r11;
588 		state.registers[SP] = ctx->un_32.pcb32_sp;
589 		state.registers[LR] = ctx->un_32.pcb32_lr;
590 		state.registers[PC] = ctx->un_32.pcb32_pc;
591 
592 		db_stack_trace_cmd(&state);
593 #else
594 		db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
595 #endif
596 	} else
597 		db_trace_self();
598 	return (0);
599 }
600 
601 void
602 db_trace_self(void)
603 {
604 #ifdef __ARM_EABI__
605 	struct unwind_state state;
606 	uint32_t sp;
607 
608 	/* Read the stack pointer */
609 	__asm __volatile("mov %0, sp" : "=&r" (sp));
610 
611 	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
612 	state.registers[SP] = sp;
613 	state.registers[LR] = (uint32_t)__builtin_return_address(0);
614 	state.registers[PC] = (uint32_t)db_trace_self;
615 
616 	db_stack_trace_cmd(&state);
617 #else
618 	db_addr_t addr;
619 
620 	addr = (db_addr_t)__builtin_frame_address(0);
621 	db_stack_trace_cmd(addr, -1, FALSE);
622 #endif
623 }
624