xref: /freebsd/sys/gdb/gdb_main.c (revision f5147e312f43a9050468de539aeafa072caa1a60)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Marcel Moolenaar
5  * All rights reserved.
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  *
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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kdb.h>
35 #include <sys/kernel.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/reboot.h>
39 
40 #include <machine/gdb_machdep.h>
41 #include <machine/kdb.h>
42 
43 #include <gdb/gdb.h>
44 #include <gdb/gdb_int.h>
45 
46 static dbbe_init_f gdb_init;
47 static dbbe_trap_f gdb_trap;
48 
49 KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap);
50 
51 static struct gdb_dbgport null_gdb_dbgport;
52 DATA_SET(gdb_dbgport_set, null_gdb_dbgport);
53 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
54 
55 struct gdb_dbgport *gdb_cur = NULL;
56 int gdb_listening = 0;
57 
58 static unsigned char gdb_bindata[64];
59 
60 static int
61 gdb_init(void)
62 {
63 	struct gdb_dbgport *dp, **iter;
64 	int cur_pri, pri;
65 
66 	gdb_cur = NULL;
67 	cur_pri = -1;
68 	SET_FOREACH(iter, gdb_dbgport_set) {
69 		dp = *iter;
70 		pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
71 		dp->gdb_active = (pri >= 0) ? 0 : -1;
72 		if (pri > cur_pri) {
73 			cur_pri = pri;
74 			gdb_cur = dp;
75 		}
76 	}
77 	if (gdb_cur != NULL) {
78 		printf("GDB: debug ports:");
79 		SET_FOREACH(iter, gdb_dbgport_set) {
80 			dp = *iter;
81 			if (dp->gdb_active == 0)
82 				printf(" %s", dp->gdb_name);
83 		}
84 		printf("\n");
85 	} else
86 		printf("GDB: no debug ports present\n");
87 	if (gdb_cur != NULL) {
88 		gdb_cur->gdb_init();
89 		printf("GDB: current port: %s\n", gdb_cur->gdb_name);
90 	}
91 	if (gdb_cur != NULL) {
92 		cur_pri = (boothowto & RB_GDB) ? 2 : 0;
93 		gdb_consinit();
94 	} else
95 		cur_pri = -1;
96 	return (cur_pri);
97 }
98 
99 static void
100 gdb_do_mem_search(void)
101 {
102 	size_t patlen;
103 	intmax_t addr, size;
104 	const unsigned char *found;
105 
106 	if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' ||
107 	    gdb_rx_varhex(&size) || gdb_rx_char() != ';' ||
108 	    gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) {
109 		gdb_tx_err(EINVAL);
110 		return;
111 	}
112 	if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata,
113 	    patlen, &found)) {
114 		if (found == 0ULL)
115 			gdb_tx_begin('0');
116 		else {
117 			gdb_tx_begin('1');
118 			gdb_tx_char(',');
119 			gdb_tx_hex((intmax_t)(uintptr_t)found, 8);
120 		}
121 		gdb_tx_end();
122 	} else
123 		gdb_tx_err(EIO);
124 }
125 
126 static int
127 gdb_trap(int type, int code)
128 {
129 	jmp_buf jb;
130 	struct thread *thr_iter;
131 	void *prev_jb;
132 
133 	prev_jb = kdb_jmpbuf(jb);
134 	if (setjmp(jb) != 0) {
135 		printf("%s bailing, hopefully back to ddb!\n", __func__);
136 		gdb_listening = 0;
137 		(void)kdb_jmpbuf(prev_jb);
138 		return (1);
139 	}
140 
141 	gdb_listening = 0;
142 	/*
143 	 * Send a T packet. We currently do not support watchpoints (the
144 	 * awatch, rwatch or watch elements).
145 	 */
146 	gdb_tx_begin('T');
147 	gdb_tx_hex(gdb_cpu_signal(type, code), 2);
148 	gdb_tx_varhex(GDB_REG_PC);
149 	gdb_tx_char(':');
150 	gdb_tx_reg(GDB_REG_PC);
151 	gdb_tx_char(';');
152 	gdb_tx_str("thread:");
153 	gdb_tx_varhex((long)kdb_thread->td_tid);
154 	gdb_tx_char(';');
155 	gdb_tx_end();			/* XXX check error condition. */
156 
157 	thr_iter = NULL;
158 	while (gdb_rx_begin() == 0) {
159 		/* printf("GDB: got '%s'\n", gdb_rxp); */
160 		switch (gdb_rx_char()) {
161 		case '?':	/* Last signal. */
162 			gdb_tx_begin('S');
163 			gdb_tx_hex(gdb_cpu_signal(type, code), 2);
164 			gdb_tx_end();
165 			break;
166 		case 'c': {	/* Continue. */
167 			uintmax_t addr;
168 			register_t pc;
169 			if (!gdb_rx_varhex(&addr)) {
170 				pc = addr;
171 				gdb_cpu_setreg(GDB_REG_PC, &pc);
172 			}
173 			kdb_cpu_clear_singlestep();
174 			gdb_listening = 1;
175 			return (1);
176 		}
177 		case 'C': {	/* Continue with signal. */
178 			uintmax_t addr, sig;
179 			register_t pc;
180 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
181 			    !gdb_rx_varhex(&addr)) {
182 				pc = addr;
183 				gdb_cpu_setreg(GDB_REG_PC, &pc);
184 			}
185 			kdb_cpu_clear_singlestep();
186 			gdb_listening = 1;
187 			return (1);
188 		}
189 		case 'D': {     /* Detach */
190 			gdb_tx_ok();
191 			kdb_cpu_clear_singlestep();
192 			return (1);
193 		}
194 		case 'g': {	/* Read registers. */
195 			size_t r;
196 			gdb_tx_begin(0);
197 			for (r = 0; r < GDB_NREGS; r++)
198 				gdb_tx_reg(r);
199 			gdb_tx_end();
200 			break;
201 		}
202 		case 'G':	/* Write registers. */
203 			gdb_tx_err(0);
204 			break;
205 		case 'H': {	/* Set thread. */
206 			intmax_t tid;
207 			struct thread *thr;
208 			gdb_rx_char();
209 			if (gdb_rx_varhex(&tid)) {
210 				gdb_tx_err(EINVAL);
211 				break;
212 			}
213 			if (tid > 0) {
214 				thr = kdb_thr_lookup(tid);
215 				if (thr == NULL) {
216 					gdb_tx_err(ENOENT);
217 					break;
218 				}
219 				kdb_thr_select(thr);
220 			}
221 			gdb_tx_ok();
222 			break;
223 		}
224 		case 'k':	/* Kill request. */
225 			kdb_cpu_clear_singlestep();
226 			gdb_listening = 1;
227 			return (1);
228 		case 'm': {	/* Read memory. */
229 			uintmax_t addr, size;
230 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
231 			    gdb_rx_varhex(&size)) {
232 				gdb_tx_err(EINVAL);
233 				break;
234 			}
235 			gdb_tx_begin(0);
236 			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
237 				gdb_tx_end();
238 			else
239 				gdb_tx_err(EIO);
240 			break;
241 		}
242 		case 'M': {	/* Write memory. */
243 			uintmax_t addr, size;
244 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
245 			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
246 				gdb_tx_err(EINVAL);
247 				break;
248 			}
249 			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
250 				gdb_tx_err(EIO);
251 			else
252 				gdb_tx_ok();
253 			break;
254 		}
255 		case 'P': {	/* Write register. */
256 			char *val;
257 			uintmax_t reg;
258 			val = gdb_rxp;
259 			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
260 			    !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
261 				gdb_tx_err(EINVAL);
262 				break;
263 			}
264 			gdb_cpu_setreg(reg, val);
265 			gdb_tx_ok();
266 			break;
267 		}
268 		case 'q':	/* General query. */
269 			if (gdb_rx_equal("fThreadInfo")) {
270 				thr_iter = kdb_thr_first();
271 				gdb_tx_begin('m');
272 				gdb_tx_hex((long)thr_iter->td_tid, 8);
273 				gdb_tx_end();
274 			} else if (gdb_rx_equal("sThreadInfo")) {
275 				if (thr_iter == NULL) {
276 					gdb_tx_err(ENXIO);
277 					break;
278 				}
279 				thr_iter = kdb_thr_next(thr_iter);
280 				if (thr_iter != NULL) {
281 					gdb_tx_begin('m');
282 					gdb_tx_hex((long)thr_iter->td_tid, 8);
283 					gdb_tx_end();
284 				} else {
285 					gdb_tx_begin('l');
286 					gdb_tx_end();
287 				}
288 			} else if (gdb_rx_equal("Search:memory:")) {
289 				gdb_do_mem_search();
290 			} else if (!gdb_cpu_query())
291 				gdb_tx_empty();
292 			break;
293 		case 's': {	/* Step. */
294 			uintmax_t addr;
295 			register_t pc;
296 			if (!gdb_rx_varhex(&addr)) {
297 				pc = addr;
298 				gdb_cpu_setreg(GDB_REG_PC, &pc);
299 			}
300 			kdb_cpu_set_singlestep();
301 			gdb_listening = 1;
302 			return (1);
303 		}
304 		case 'S': {	/* Step with signal. */
305 			uintmax_t addr, sig;
306 			register_t pc;
307 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
308 			    !gdb_rx_varhex(&addr)) {
309 				pc = addr;
310 				gdb_cpu_setreg(GDB_REG_PC, &pc);
311 			}
312 			kdb_cpu_set_singlestep();
313 			gdb_listening = 1;
314 			return (1);
315 		}
316 		case 'T': {	/* Thread alive. */
317 			intmax_t tid;
318 			if (gdb_rx_varhex(&tid)) {
319 				gdb_tx_err(EINVAL);
320 				break;
321 			}
322 			if (kdb_thr_lookup(tid) != NULL)
323 				gdb_tx_ok();
324 			else
325 				gdb_tx_err(ENOENT);
326 			break;
327 		}
328 		case -1:
329 			/* Empty command. Treat as unknown command. */
330 			/* FALLTHROUGH */
331 		default:
332 			/* Unknown command. Send empty response. */
333 			gdb_tx_empty();
334 			break;
335 		}
336 	}
337 	(void)kdb_jmpbuf(prev_jb);
338 	return (0);
339 }
340