xref: /freebsd/sys/arm64/arm64/db_trace.c (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
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 static void
61 db_stack_trace_cmd(struct thread *td, struct unwind_state *frame)
62 {
63 	c_db_sym_t sym;
64 	const char *name;
65 	db_expr_t value;
66 	db_expr_t offset;
67 	int frame_type;
68 
69 	while (1) {
70 		sym = db_search_symbol(frame->pc, DB_STGY_ANY, &offset);
71 		if (sym == C_DB_SYM_NULL) {
72 			value = 0;
73 			name = "(null)";
74 		} else
75 			db_symbol_values(sym, &name, &value);
76 
77 		db_printf("%s() at ", name);
78 		db_printsym(frame->pc, DB_STGY_PROC);
79 		db_printf("\n");
80 
81 		if (strcmp(name, "handle_el0_sync") == 0 ||
82 		    strcmp(name, "handle_el1h_sync") == 0)
83 			frame_type = FRAME_SYNC;
84 		else if (strcmp(name, "handle_el0_irq") == 0 ||
85 		     strcmp(name, "handle_el1h_irq") == 0)
86 			frame_type = FRAME_IRQ;
87 		else if (strcmp(name, "handle_serror") == 0)
88 			frame_type = FRAME_SERROR;
89 		else if (strcmp(name, "handle_empty_exception") == 0)
90 			frame_type = FRAME_UNHANDLED;
91 		else
92 			frame_type = FRAME_NORMAL;
93 
94 		if (frame_type != FRAME_NORMAL) {
95 			struct trapframe *tf;
96 
97 			tf = (struct trapframe *)(uintptr_t)frame->fp - 1;
98 			if (!kstack_contains(td, (vm_offset_t)tf,
99 			    sizeof(*tf))) {
100 				db_printf("--- invalid trapframe %p\n", tf);
101 				break;
102 			}
103 
104 			switch (frame_type) {
105 			case FRAME_SYNC:
106 				db_printf("--- exception, esr %#x\n",
107 				    tf->tf_esr);
108 				break;
109 			case FRAME_IRQ:
110 				db_printf("--- interrupt\n");
111 				break;
112 			case FRAME_SERROR:
113 				db_printf("--- system error, esr %#x\n",
114 				    tf->tf_esr);
115 				break;
116 			case FRAME_UNHANDLED:
117 				db_printf("--- unhandled exception, esr %#x\n",
118 				    tf->tf_esr);
119 				break;
120 			default:
121 				__assert_unreachable();
122 				break;
123 			}
124 
125 			frame->fp = tf->tf_x[29];
126 			frame->pc = tf->tf_elr;
127 			if (!INKERNEL(frame->fp))
128 				break;
129 		} else {
130 			if (strcmp(name, "fork_trampoline") == 0)
131 				break;
132 
133 			if (!unwind_frame(td, frame))
134 				break;
135 		}
136 	}
137 }
138 
139 int
140 db_trace_thread(struct thread *thr, int count)
141 {
142 	struct unwind_state frame;
143 	struct pcb *ctx;
144 
145 	if (thr != curthread) {
146 		ctx = kdb_thr_ctx(thr);
147 
148 		frame.fp = (uintptr_t)ctx->pcb_x[29];
149 		frame.pc = (uintptr_t)ctx->pcb_lr;
150 		db_stack_trace_cmd(thr, &frame);
151 	} else
152 		db_trace_self();
153 	return (0);
154 }
155 
156 void
157 db_trace_self(void)
158 {
159 	struct unwind_state frame;
160 
161 	frame.fp = (uintptr_t)__builtin_frame_address(0);
162 	frame.pc = (uintptr_t)db_trace_self;
163 	db_stack_trace_cmd(curthread, &frame);
164 }
165