xref: /freebsd/sys/arm64/arm64/db_trace.c (revision f61e92ca5a23450bc28169bbdd71d7674df98c19)
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under
6  * the sponsorship of the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/proc.h>
36 #include <sys/kdb.h>
37 
38 #include <machine/pcb.h>
39 #include <ddb/ddb.h>
40 #include <ddb/db_sym.h>
41 
42 #include <machine/armreg.h>
43 #include <machine/debug_monitor.h>
44 #include <machine/stack.h>
45 #include <machine/vmparam.h>
46 
47 #define	FRAME_NORMAL	0
48 #define	FRAME_SYNC	1
49 #define	FRAME_IRQ	2
50 #define	FRAME_SERROR	3
51 #define	FRAME_UNHANDLED	4
52 
53 void
54 db_md_list_watchpoints()
55 {
56 
57 	dbg_show_watchpoint();
58 }
59 
60 int
61 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
62 {
63 
64 	return (dbg_remove_watchpoint(NULL, addr, size));
65 }
66 
67 int
68 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
69 {
70 
71 	return (dbg_setup_watchpoint(NULL, addr, size, HW_BREAKPOINT_RW));
72 }
73 
74 static void
75 db_stack_trace_cmd(struct thread *td, struct unwind_state *frame)
76 {
77 	c_db_sym_t sym;
78 	const char *name;
79 	db_expr_t value;
80 	db_expr_t offset;
81 	int frame_type;
82 
83 	while (1) {
84 		sym = db_search_symbol(frame->pc, DB_STGY_ANY, &offset);
85 		if (sym == C_DB_SYM_NULL) {
86 			value = 0;
87 			name = "(null)";
88 		} else
89 			db_symbol_values(sym, &name, &value);
90 
91 		db_printf("%s() at ", name);
92 		db_printsym(frame->pc, DB_STGY_PROC);
93 		db_printf("\n");
94 
95 		if (strcmp(name, "handle_el0_sync") == 0 ||
96 		    strcmp(name, "handle_el1h_sync") == 0)
97 			frame_type = FRAME_SYNC;
98 		else if (strcmp(name, "handle_el0_irq") == 0 ||
99 		     strcmp(name, "handle_el1h_irq") == 0)
100 			frame_type = FRAME_IRQ;
101 		else if (strcmp(name, "handle_serror") == 0)
102 			frame_type = FRAME_SERROR;
103 		else if (strcmp(name, "handle_empty_exception") == 0)
104 			frame_type = FRAME_UNHANDLED;
105 		else
106 			frame_type = FRAME_NORMAL;
107 
108 		if (frame_type != FRAME_NORMAL) {
109 			struct trapframe *tf;
110 
111 			tf = (struct trapframe *)(uintptr_t)frame->fp - 1;
112 			if (!kstack_contains(td, (vm_offset_t)tf,
113 			    sizeof(*tf))) {
114 				db_printf("--- invalid trapframe %p\n", tf);
115 				break;
116 			}
117 
118 			switch (frame_type) {
119 			case FRAME_SYNC:
120 				db_printf("--- exception, esr %#x\n",
121 				    tf->tf_esr);
122 				break;
123 			case FRAME_IRQ:
124 				db_printf("--- interrupt\n");
125 				break;
126 			case FRAME_SERROR:
127 				db_printf("--- system error, esr %#x\n",
128 				    tf->tf_esr);
129 				break;
130 			case FRAME_UNHANDLED:
131 				db_printf("--- unhandled exception, esr %#x\n",
132 				    tf->tf_esr);
133 				break;
134 			default:
135 				__assert_unreachable();
136 				break;
137 			}
138 
139 			frame->fp = tf->tf_x[29];
140 			frame->pc = tf->tf_elr;
141 			if (!INKERNEL(frame->fp))
142 				break;
143 		} else {
144 			if (strcmp(name, "fork_trampoline") == 0)
145 				break;
146 
147 			if (!unwind_frame(td, frame))
148 				break;
149 		}
150 	}
151 }
152 
153 int
154 db_trace_thread(struct thread *thr, int count)
155 {
156 	struct unwind_state frame;
157 	struct pcb *ctx;
158 
159 	if (thr != curthread) {
160 		ctx = kdb_thr_ctx(thr);
161 
162 		frame.fp = (uintptr_t)ctx->pcb_x[29];
163 		frame.pc = (uintptr_t)ctx->pcb_lr;
164 		db_stack_trace_cmd(thr, &frame);
165 	} else
166 		db_trace_self();
167 	return (0);
168 }
169 
170 void
171 db_trace_self(void)
172 {
173 	struct unwind_state frame;
174 
175 	frame.fp = (uintptr_t)__builtin_frame_address(0);
176 	frame.pc = (uintptr_t)db_trace_self;
177 	db_stack_trace_cmd(curthread, &frame);
178 }
179