xref: /freebsd/sys/arm/arm/db_trace.c (revision 81ad6265)
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 #include "opt_ddb.h"
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 
38 #include <sys/proc.h>
39 #include <sys/kdb.h>
40 #include <sys/stack.h>
41 
42 #include <machine/armreg.h>
43 #include <machine/asm.h>
44 #include <machine/cpufunc.h>
45 #include <machine/db_machdep.h>
46 #include <machine/debug_monitor.h>
47 #include <machine/pcb.h>
48 #include <machine/stack.h>
49 #include <machine/vmparam.h>
50 
51 #include <ddb/ddb.h>
52 #include <ddb/db_access.h>
53 #include <ddb/db_sym.h>
54 #include <ddb/db_output.h>
55 
56 static void
57 db_stack_trace_cmd(struct unwind_state *state)
58 {
59 	const char *name;
60 	db_expr_t value;
61 	db_expr_t offset;
62 	c_db_sym_t sym;
63 	u_int reg, i;
64 	char *sep;
65 	uint16_t upd_mask;
66 	bool finished;
67 
68 	finished = false;
69 	while (!finished) {
70 		finished = unwind_stack_one(state, 1);
71 
72 		/* Print the frame details */
73 		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
74 		if (sym == C_DB_SYM_NULL) {
75 			value = 0;
76 			name = "(null)";
77 		} else
78 			db_symbol_values(sym, &name, &value);
79 		db_printf("%s() at ", name);
80 		db_printsym(state->start_pc, DB_STGY_PROC);
81 		db_printf("\n");
82 		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
83 		    state->registers[LR]);
84 		db_printsym(state->registers[LR], DB_STGY_PROC);
85 		db_printf(")\n");
86 		db_printf("\t sp = 0x%08x  fp = 0x%08x",
87 		    state->registers[SP], state->registers[FP]);
88 
89 		/* Don't print the registers we have already printed */
90 		upd_mask = state->update_mask &
91 		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
92 		sep = "\n\t";
93 		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
94 			if ((upd_mask & 1) != 0) {
95 				db_printf("%s%sr%d = 0x%08x", sep,
96 				    (reg < 10) ? " " : "", reg,
97 				    state->registers[reg]);
98 				i++;
99 				if (i == 2) {
100 					sep = "\n\t";
101 					i = 0;
102 				} else
103 					sep = " ";
104 			}
105 		}
106 		db_printf("\n");
107 
108 		if (finished)
109 			break;
110 
111 		/*
112 		 * Stop if directed to do so, or if we've unwound back to the
113 		 * kernel entry point, or if the unwind function didn't change
114 		 * anything (to avoid getting stuck in this loop forever).
115 		 * If the latter happens, it's an indication that the unwind
116 		 * information is incorrect somehow for the function named in
117 		 * the last frame printed before you see the unwind failure
118 		 * message (maybe it needs a STOP_UNWINDING).
119 		 */
120 		if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
121 			db_printf("Unable to unwind into user mode\n");
122 			finished = true;
123 		} else if (state->update_mask == 0) {
124 			db_printf("Unwind failure (no registers changed)\n");
125 			finished = true;
126 		}
127 	}
128 }
129 
130 void
131 db_md_list_watchpoints(void)
132 {
133 
134 	dbg_show_watchpoint();
135 }
136 
137 int
138 db_trace_thread(struct thread *thr, int count)
139 {
140 	struct unwind_state state;
141 	struct pcb *ctx;
142 
143 	if (thr != curthread) {
144 		ctx = kdb_thr_ctx(thr);
145 
146 		state.registers[FP] = ctx->pcb_regs.sf_r11;
147 		state.registers[SP] = ctx->pcb_regs.sf_sp;
148 		state.registers[LR] = ctx->pcb_regs.sf_lr;
149 		state.registers[PC] = ctx->pcb_regs.sf_pc;
150 
151 		db_stack_trace_cmd(&state);
152 	} else
153 		db_trace_self();
154 	return (0);
155 }
156 
157 void
158 db_trace_self(void)
159 {
160 	struct unwind_state state;
161 	uint32_t sp;
162 
163 	/* Read the stack pointer */
164 	__asm __volatile("mov %0, sp" : "=&r" (sp));
165 
166 	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
167 	state.registers[SP] = sp;
168 	state.registers[LR] = (uint32_t)__builtin_return_address(0);
169 	state.registers[PC] = (uint32_t)db_trace_self;
170 
171 	db_stack_trace_cmd(&state);
172 }
173