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