xref: /freebsd/sys/arm/arm/db_trace.c (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
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_POP_REGS		0xb1
112 #define	INSN_VSP_LARGE_INC	0xb2
113 
114 /* An item in the exception index table */
115 struct unwind_idx {
116 	uint32_t offset;
117 	uint32_t insn;
118 };
119 
120 /* The state of the unwind process */
121 struct unwind_state {
122 	uint32_t registers[16];
123 	uint32_t start_pc;
124 	uint32_t *insn;
125 	u_int entries;
126 	u_int byte;
127 	uint16_t update_mask;
128 };
129 
130 /* Expand a 31-bit signed value to a 32-bit signed value */
131 static __inline int32_t
132 db_expand_prel31(uint32_t prel31)
133 {
134 
135 	return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
136 }
137 
138 /*
139  * Perform a binary search of the index table to find the function
140  * with the largest address that doesn't exceed addr.
141  */
142 static struct unwind_idx *
143 db_find_index(uint32_t addr)
144 {
145 	unsigned int min, mid, max;
146 	struct unwind_idx *start;
147 	struct unwind_idx *item;
148 	int32_t prel31_addr;
149 	uint32_t func_addr;
150 
151 	start = (struct unwind_idx *)&exidx_start;
152 
153 	min = 0;
154 	max = (&exidx_end - &exidx_start) / 2;
155 
156 	while (min != max) {
157 		mid = min + (max - min + 1) / 2;
158 
159 		item = &start[mid];
160 
161 	 	prel31_addr = db_expand_prel31(item->offset);
162 		func_addr = (uint32_t)&item->offset + prel31_addr;
163 
164 		if (func_addr <= addr) {
165 			min = mid;
166 		} else {
167 			max = mid - 1;
168 		}
169 	}
170 
171 	return &start[min];
172 }
173 
174 /* Reads the next byte from the instruction list */
175 static uint8_t
176 db_unwind_exec_read_byte(struct unwind_state *state)
177 {
178 	uint8_t insn;
179 
180 	/* Read the unwind instruction */
181 	insn = (*state->insn) >> (state->byte * 8);
182 
183 	/* Update the location of the next instruction */
184 	if (state->byte == 0) {
185 		state->byte = 3;
186 		state->insn++;
187 		state->entries--;
188 	} else
189 		state->byte--;
190 
191 	return insn;
192 }
193 
194 /* Executes the next instruction on the list */
195 static int
196 db_unwind_exec_insn(struct unwind_state *state)
197 {
198 	unsigned int insn;
199 	uint32_t *vsp = (uint32_t *)state->registers[SP];
200 	int update_vsp = 0;
201 
202 	/* This should never happen */
203 	if (state->entries == 0)
204 		return 1;
205 
206 	/* Read the next instruction */
207 	insn = db_unwind_exec_read_byte(state);
208 
209 	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
210 		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
211 
212 	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
213 		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
214 
215 	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
216 		unsigned int mask, reg;
217 
218 		/* Load the mask */
219 		mask = db_unwind_exec_read_byte(state);
220 		mask |= (insn & INSN_STD_DATA_MASK) << 8;
221 
222 		/* We have a refuse to unwind instruction */
223 		if (mask == 0)
224 			return 1;
225 
226 		/* Update SP */
227 		update_vsp = 1;
228 
229 		/* Load the registers */
230 		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
231 			if (mask & 1) {
232 				state->registers[reg] = *vsp++;
233 				state->update_mask |= 1 << reg;
234 
235 				/* If we have updated SP kep its value */
236 				if (reg == SP)
237 					update_vsp = 0;
238 			}
239 		}
240 
241 	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
242 	    ((insn & INSN_STD_DATA_MASK) != 13) &&
243 	    ((insn & INSN_STD_DATA_MASK) != 15)) {
244 		/* sp = register */
245 		state->registers[SP] =
246 		    state->registers[insn & INSN_STD_DATA_MASK];
247 
248 	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
249 		unsigned int count, reg;
250 
251 		/* Read how many registers to load */
252 		count = insn & INSN_POP_COUNT_MASK;
253 
254 		/* Update sp */
255 		update_vsp = 1;
256 
257 		/* Pop the registers */
258 		for (reg = 4; reg <= 4 + count; reg++) {
259 			state->registers[reg] = *vsp++;
260 			state->update_mask |= 1 << reg;
261 		}
262 
263 		/* Check if we are in the pop r14 version */
264 		if ((insn & INSN_POP_TYPE_MASK) != 0) {
265 			state->registers[14] = *vsp++;
266 		}
267 
268 	} else if (insn == INSN_FINISH) {
269 		/* Stop processing */
270 		state->entries = 0;
271 
272 	} else if (insn == INSN_POP_REGS) {
273 		unsigned int mask, reg;
274 
275 		mask = db_unwind_exec_read_byte(state);
276 		if (mask == 0 || (mask & 0xf0) != 0)
277 			return 1;
278 
279 		/* Update SP */
280 		update_vsp = 1;
281 
282 		/* Load the registers */
283 		for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
284 			if (mask & 1) {
285 				state->registers[reg] = *vsp++;
286 				state->update_mask |= 1 << reg;
287 			}
288 		}
289 
290 	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
291 		unsigned int uleb128;
292 
293 		/* Read the increment value */
294 		uleb128 = db_unwind_exec_read_byte(state);
295 
296 		state->registers[SP] += 0x204 + (uleb128 << 2);
297 
298 	} else {
299 		/* We hit a new instruction that needs to be implemented */
300 		db_printf("Unhandled instruction %.2x\n", insn);
301 		return 1;
302 	}
303 
304 	if (update_vsp) {
305 		state->registers[SP] = (uint32_t)vsp;
306 	}
307 
308 #if 0
309 	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
310 	    state->registers[FP], state->registers[SP], state->registers[LR],
311 	    state->registers[PC]);
312 #endif
313 
314 	return 0;
315 }
316 
317 /* Performs the unwind of a function */
318 static int
319 db_unwind_tab(struct unwind_state *state)
320 {
321 	uint32_t entry;
322 
323 	/* Set PC to a known value */
324 	state->registers[PC] = 0;
325 
326 	/* Read the personality */
327 	entry = *state->insn & ENTRY_MASK;
328 
329 	if (entry == ENTRY_ARM_SU16) {
330 		state->byte = 2;
331 		state->entries = 1;
332 	} else if (entry == ENTRY_ARM_LU16) {
333 		state->byte = 1;
334 		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
335 	} else {
336 		db_printf("Unknown entry: %x\n", entry);
337 		return 1;
338 	}
339 
340 	while (state->entries > 0) {
341 		if (db_unwind_exec_insn(state) != 0)
342 			return 1;
343 	}
344 
345 	/*
346 	 * The program counter was not updated, load it from the link register.
347 	 */
348 	if (state->registers[PC] == 0) {
349 		state->registers[PC] = state->registers[LR];
350 
351 		/*
352 		 * If the program counter changed, flag it in the update mask.
353 		 */
354 		if (state->start_pc != state->registers[PC])
355 			state->update_mask |= 1 << PC;
356 	}
357 
358 	return 0;
359 }
360 
361 static void
362 db_stack_trace_cmd(struct unwind_state *state)
363 {
364 	struct unwind_idx *index;
365 	const char *name;
366 	db_expr_t value;
367 	db_expr_t offset;
368 	c_db_sym_t sym;
369 	u_int reg, i;
370 	char *sep;
371 	uint16_t upd_mask;
372 	bool finished;
373 
374 	finished = false;
375 	while (!finished) {
376 		/* Reset the mask of updated registers */
377 		state->update_mask = 0;
378 
379 		/* The pc value is correct and will be overwritten, save it */
380 		state->start_pc = state->registers[PC];
381 
382 		/* Find the item to run */
383 		index = db_find_index(state->start_pc);
384 
385 		if (index->insn != EXIDX_CANTUNWIND) {
386 			if (index->insn & (1U << 31)) {
387 				/* The data is within the instruction */
388 				state->insn = &index->insn;
389 			} else {
390 				/* A prel31 offset to the unwind table */
391 				state->insn = (uint32_t *)
392 				    ((uintptr_t)&index->insn +
393 				     db_expand_prel31(index->insn));
394 			}
395 			/* Run the unwind function */
396 			finished = db_unwind_tab(state);
397 		}
398 
399 		/* Print the frame details */
400 		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
401 		if (sym == C_DB_SYM_NULL) {
402 			value = 0;
403 			name = "(null)";
404 		} else
405 			db_symbol_values(sym, &name, &value);
406 		db_printf("%s() at ", name);
407 		db_printsym(state->start_pc, DB_STGY_PROC);
408 		db_printf("\n");
409 		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
410 		    state->registers[LR]);
411 		db_printsym(state->registers[LR], DB_STGY_PROC);
412 		db_printf(")\n");
413 		db_printf("\t sp = 0x%08x  fp = 0x%08x",
414 		    state->registers[SP], state->registers[FP]);
415 
416 		/* Don't print the registers we have already printed */
417 		upd_mask = state->update_mask &
418 		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
419 		sep = "\n\t";
420 		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
421 			if ((upd_mask & 1) != 0) {
422 				db_printf("%s%sr%d = 0x%08x", sep,
423 				    (reg < 10) ? " " : "", reg,
424 				    state->registers[reg]);
425 				i++;
426 				if (i == 2) {
427 					sep = "\n\t";
428 					i = 0;
429 				} else
430 					sep = " ";
431 
432 			}
433 		}
434 		db_printf("\n");
435 
436 		/*
437 		 * Stop if directed to do so, or if we've unwound back to the
438 		 * kernel entry point, or if the unwind function didn't change
439 		 * anything (to avoid getting stuck in this loop forever).
440 		 * If the latter happens, it's an indication that the unwind
441 		 * information is incorrect somehow for the function named in
442 		 * the last frame printed before you see the unwind failure
443 		 * message (maybe it needs a STOP_UNWINDING).
444 		 */
445 		if (index->insn == EXIDX_CANTUNWIND) {
446 			finished = true;
447 		} else if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
448 			db_printf("Unable to unwind into user mode\n");
449 			finished = true;
450 		} else if (state->update_mask == 0) {
451 			db_printf("Unwind failure (no registers changed)\n");
452 			finished = true;
453 		}
454 	}
455 }
456 #endif
457 
458 /*
459  * APCS stack frames are awkward beasts, so I don't think even trying to use
460  * a structure to represent them is a good idea.
461  *
462  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
463  *
464  *          save code pointer       [fp]        <- fp points to here
465  *          return link value       [fp, #-4]
466  *          return sp value         [fp, #-8]
467  *          return fp value         [fp, #-12]
468  *          [saved v7 value]
469  *          [saved v6 value]
470  *          [saved v5 value]
471  *          [saved v4 value]
472  *          [saved v3 value]
473  *          [saved v2 value]
474  *          [saved v1 value]
475  *          [saved a4 value]
476  *          [saved a3 value]
477  *          [saved a2 value]
478  *          [saved a1 value]
479  *
480  * The save code pointer points twelve bytes beyond the start of the
481  * code sequence (usually a single STM) that created the stack frame.
482  * We have to disassemble it if we want to know which of the optional
483  * fields are actually present.
484  */
485 
486 #ifndef __ARM_EABI__	/* The frame format is differend in AAPCS */
487 static void
488 db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
489 {
490 	u_int32_t	*frame, *lastframe;
491 	c_db_sym_t sym;
492 	const char *name;
493 	db_expr_t value;
494 	db_expr_t offset;
495 	int	scp_offset;
496 
497 	frame = (u_int32_t *)addr;
498 	lastframe = NULL;
499 	scp_offset = -(get_pc_str_offset() >> 2);
500 
501 	while (count-- && frame != NULL && !db_pager_quit) {
502 		db_addr_t	scp;
503 		u_int32_t	savecode;
504 		int		r;
505 		u_int32_t	*rp;
506 		const char	*sep;
507 
508 		/*
509 		 * In theory, the SCP isn't guaranteed to be in the function
510 		 * that generated the stack frame.  We hope for the best.
511 		 */
512 		scp = frame[FR_SCP];
513 
514 		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
515 		if (sym == C_DB_SYM_NULL) {
516 			value = 0;
517 			name = "(null)";
518 		} else
519 			db_symbol_values(sym, &name, &value);
520 		db_printf("%s() at ", name);
521 		db_printsym(scp, DB_STGY_PROC);
522 		db_printf("\n");
523 #ifdef __PROG26
524 		db_printf("\tscp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
525 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
526 		db_printf(")\n");
527 #else
528 		db_printf("\tscp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
529 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
530 		db_printf(")\n");
531 #endif
532 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
533 
534 		savecode = ((u_int32_t *)scp)[scp_offset];
535 		if ((savecode & 0x0e100000) == 0x08000000) {
536 			/* Looks like an STM */
537 			rp = frame - 4;
538 			sep = "\n\t";
539 			for (r = 10; r >= 0; r--) {
540 				if (savecode & (1 << r)) {
541 					db_printf("%sr%d=0x%08x",
542 					    sep, r, *rp--);
543 					sep = (frame - rp) % 4 == 2 ?
544 					    "\n\t" : " ";
545 				}
546 			}
547 		}
548 
549 		db_printf("\n");
550 
551 		/*
552 		 * Switch to next frame up
553 		 */
554 		if (frame[FR_RFP] == 0)
555 			break; /* Top of stack */
556 
557 		lastframe = frame;
558 		frame = (u_int32_t *)(frame[FR_RFP]);
559 
560 		if (INKERNEL((int)frame)) {
561 			/* staying in kernel */
562 			if (frame <= lastframe) {
563 				db_printf("Bad frame pointer: %p\n", frame);
564 				break;
565 			}
566 		} else if (INKERNEL((int)lastframe)) {
567 			/* switch from user to kernel */
568 			if (kernel_only)
569 				break;	/* kernel stack only */
570 		} else {
571 			/* in user */
572 			if (frame <= lastframe) {
573 				db_printf("Bad user frame pointer: %p\n",
574 					  frame);
575 				break;
576 			}
577 		}
578 	}
579 }
580 #endif
581 
582 /* XXX stubs */
583 void
584 db_md_list_watchpoints()
585 {
586 }
587 
588 int
589 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
590 {
591 	return (0);
592 }
593 
594 int
595 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
596 {
597 	return (0);
598 }
599 
600 int
601 db_trace_thread(struct thread *thr, int count)
602 {
603 #ifdef __ARM_EABI__
604 	struct unwind_state state;
605 #endif
606 	struct pcb *ctx;
607 
608 	if (thr != curthread) {
609 		ctx = kdb_thr_ctx(thr);
610 
611 #ifdef __ARM_EABI__
612 		state.registers[FP] = ctx->pcb_regs.sf_r11;
613 		state.registers[SP] = ctx->pcb_regs.sf_sp;
614 		state.registers[LR] = ctx->pcb_regs.sf_lr;
615 		state.registers[PC] = ctx->pcb_regs.sf_pc;
616 
617 		db_stack_trace_cmd(&state);
618 #else
619 		db_stack_trace_cmd(ctx->pcb_regs.sf_r11, -1, TRUE);
620 #endif
621 	} else
622 		db_trace_self();
623 	return (0);
624 }
625 
626 void
627 db_trace_self(void)
628 {
629 #ifdef __ARM_EABI__
630 	struct unwind_state state;
631 	uint32_t sp;
632 
633 	/* Read the stack pointer */
634 	__asm __volatile("mov %0, sp" : "=&r" (sp));
635 
636 	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
637 	state.registers[SP] = sp;
638 	state.registers[LR] = (uint32_t)__builtin_return_address(0);
639 	state.registers[PC] = (uint32_t)db_trace_self;
640 
641 	db_stack_trace_cmd(&state);
642 #else
643 	db_addr_t addr;
644 
645 	addr = (db_addr_t)__builtin_frame_address(0);
646 	db_stack_trace_cmd(addr, -1, FALSE);
647 #endif
648 }
649