xref: /freebsd/sys/gdb/gdb_main.c (revision c4fbbfaa472bb4f102aaa41bfc0f3760ccbc9ef8)
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 void
127 gdb_do_threadinfo(struct thread **thr_iter)
128 {
129 	static struct thread * const done_sentinel = (void *)(uintptr_t)1;
130 	static const size_t tidsz_hex = sizeof(lwpid_t) * 2;
131 	size_t tds_sent;
132 
133 	if (*thr_iter == NULL) {
134 		gdb_tx_err(ENXIO);
135 		return;
136 	}
137 
138 	if (*thr_iter == done_sentinel) {
139 		gdb_tx_begin('l');
140 		*thr_iter = NULL;
141 		goto sendit;
142 	}
143 
144 	gdb_tx_begin('m');
145 
146 	for (tds_sent = 0;
147 	    *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1);
148 	    *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) {
149 		if (tds_sent > 0)
150 			gdb_tx_char(',');
151 		gdb_tx_varhex((*thr_iter)->td_tid);
152 	}
153 
154 	/*
155 	 * Can't send EOF and "some" in same packet, so set a sentinel to send
156 	 * EOF when GDB asks us next.
157 	 */
158 	if (*thr_iter == NULL && tds_sent > 0)
159 		*thr_iter = done_sentinel;
160 
161 sendit:
162 	gdb_tx_end();
163 }
164 
165 #define	BIT(n)	(1ull << (n))
166 enum {
167 	GDB_MULTIPROCESS,
168 	GDB_SWBREAK,
169 	GDB_HWBREAK,
170 	GDB_QRELOCINSN,
171 	GDB_FORK_EVENTS,
172 	GDB_VFORK_EVENTS,
173 	GDB_EXEC_EVENTS,
174 	GDB_VCONT_SUPPORTED,
175 	GDB_QTHREADEVENTS,
176 	GDB_NO_RESUMED,
177 };
178 static const char * const gdb_feature_names[] = {
179 	[GDB_MULTIPROCESS] = "multiprocess",
180 	[GDB_SWBREAK] = "swbreak",
181 	[GDB_HWBREAK] = "hwbreak",
182 	[GDB_QRELOCINSN] = "qRelocInsn",
183 	[GDB_FORK_EVENTS] = "fork-events",
184 	[GDB_VFORK_EVENTS] = "vfork-events",
185 	[GDB_EXEC_EVENTS] = "exec-events",
186 	[GDB_VCONT_SUPPORTED] = "vContSupported",
187 	[GDB_QTHREADEVENTS] = "QThreadEvents",
188 	[GDB_NO_RESUMED] = "no-resumed",
189 };
190 static void
191 gdb_do_qsupported(uint32_t *feat)
192 {
193 	char *tok, *delim, ok;
194 	size_t i, toklen;
195 
196 	/* Parse supported host features */
197 	*feat = 0;
198 	if (gdb_rx_char() != ':')
199 		goto error;
200 
201 	while (gdb_rxsz > 0) {
202 		tok = gdb_rxp;
203 		delim = strchrnul(gdb_rxp, ';');
204 		toklen = (delim - tok);
205 
206 		gdb_rxp += toklen;
207 		gdb_rxsz -= toklen;
208 		if (*delim != '\0') {
209 			*delim = '\0';
210 			gdb_rxp += 1;
211 			gdb_rxsz -= 1;
212 		}
213 
214 		if (toklen < 2)
215 			goto error;
216 
217 		ok = tok[toklen - 1];
218 		if (ok != '-' && ok != '+') {
219 			/*
220 			 * GDB only has one KV-pair feature, and we don't
221 			 * support it, so ignore and move on.
222 			 */
223 			if (strchr(tok, '=') != NULL)
224 				continue;
225 			/* Not a KV-pair, and not a +/- flag?  Malformed. */
226 			goto error;
227 		}
228 		if (ok != '+')
229 			continue;
230 		tok[toklen - 1] = '\0';
231 
232 		for (i = 0; i < nitems(gdb_feature_names); i++)
233 			if (strcmp(gdb_feature_names[i], tok) == 0)
234 				break;
235 
236 		if (i == nitems(gdb_feature_names)) {
237 			/* Unknown GDB feature. */
238 			continue;
239 		}
240 
241 		*feat |= BIT(i);
242 	}
243 
244 	/* Send a supported feature list back */
245 	gdb_tx_begin(0);
246 
247 	gdb_tx_str("PacketSize");
248 	gdb_tx_char('=');
249 	/*
250 	 * We don't buffer framing bytes, but we do need to retain a byte for a
251 	 * trailing nul.
252 	 */
253 	gdb_tx_varhex(GDB_BUFSZ + strlen("$#nn") - 1);
254 
255 	/*
256 	 * Future consideration:
257 	 *   - vCont
258 	 *   - multiprocess
259 	 *   - qXfer:threads:read
260 	 */
261 	gdb_tx_end();
262 	return;
263 
264 error:
265 	*feat = 0;
266 	gdb_tx_err(EINVAL);
267 }
268 
269 static int
270 gdb_trap(int type, int code)
271 {
272 	jmp_buf jb;
273 	struct thread *thr_iter;
274 	void *prev_jb;
275 	uint32_t host_features;
276 
277 	prev_jb = kdb_jmpbuf(jb);
278 	if (setjmp(jb) != 0) {
279 		printf("%s bailing, hopefully back to ddb!\n", __func__);
280 		gdb_listening = 0;
281 		(void)kdb_jmpbuf(prev_jb);
282 		return (1);
283 	}
284 
285 	gdb_listening = 0;
286 	/*
287 	 * Send a T packet. We currently do not support watchpoints (the
288 	 * awatch, rwatch or watch elements).
289 	 */
290 	gdb_tx_begin('T');
291 	gdb_tx_hex(gdb_cpu_signal(type, code), 2);
292 	gdb_tx_varhex(GDB_REG_PC);
293 	gdb_tx_char(':');
294 	gdb_tx_reg(GDB_REG_PC);
295 	gdb_tx_char(';');
296 	gdb_tx_str("thread:");
297 	gdb_tx_varhex((long)kdb_thread->td_tid);
298 	gdb_tx_char(';');
299 	gdb_tx_end();			/* XXX check error condition. */
300 
301 	thr_iter = NULL;
302 	while (gdb_rx_begin() == 0) {
303 		/* printf("GDB: got '%s'\n", gdb_rxp); */
304 		switch (gdb_rx_char()) {
305 		case '?':	/* Last signal. */
306 			gdb_tx_begin('T');
307 			gdb_tx_hex(gdb_cpu_signal(type, code), 2);
308 			gdb_tx_str("thread:");
309 			gdb_tx_varhex((long)kdb_thread->td_tid);
310 			gdb_tx_char(';');
311 			gdb_tx_end();
312 			break;
313 		case 'c': {	/* Continue. */
314 			uintmax_t addr;
315 			register_t pc;
316 			if (!gdb_rx_varhex(&addr)) {
317 				pc = addr;
318 				gdb_cpu_setreg(GDB_REG_PC, &pc);
319 			}
320 			kdb_cpu_clear_singlestep();
321 			gdb_listening = 1;
322 			return (1);
323 		}
324 		case 'C': {	/* Continue with signal. */
325 			uintmax_t addr, sig;
326 			register_t pc;
327 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
328 			    !gdb_rx_varhex(&addr)) {
329 				pc = addr;
330 				gdb_cpu_setreg(GDB_REG_PC, &pc);
331 			}
332 			kdb_cpu_clear_singlestep();
333 			gdb_listening = 1;
334 			return (1);
335 		}
336 		case 'D': {     /* Detach */
337 			gdb_tx_ok();
338 			kdb_cpu_clear_singlestep();
339 			return (1);
340 		}
341 		case 'g': {	/* Read registers. */
342 			size_t r;
343 			gdb_tx_begin(0);
344 			for (r = 0; r < GDB_NREGS; r++)
345 				gdb_tx_reg(r);
346 			gdb_tx_end();
347 			break;
348 		}
349 		case 'G':	/* Write registers. */
350 			gdb_tx_err(0);
351 			break;
352 		case 'H': {	/* Set thread. */
353 			intmax_t tid;
354 			struct thread *thr;
355 			gdb_rx_char();
356 			if (gdb_rx_varhex(&tid)) {
357 				gdb_tx_err(EINVAL);
358 				break;
359 			}
360 			if (tid > 0) {
361 				thr = kdb_thr_lookup(tid);
362 				if (thr == NULL) {
363 					gdb_tx_err(ENOENT);
364 					break;
365 				}
366 				kdb_thr_select(thr);
367 			}
368 			gdb_tx_ok();
369 			break;
370 		}
371 		case 'k':	/* Kill request. */
372 			kdb_cpu_clear_singlestep();
373 			gdb_listening = 1;
374 			return (1);
375 		case 'm': {	/* Read memory. */
376 			uintmax_t addr, size;
377 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
378 			    gdb_rx_varhex(&size)) {
379 				gdb_tx_err(EINVAL);
380 				break;
381 			}
382 			gdb_tx_begin(0);
383 			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
384 				gdb_tx_end();
385 			else
386 				gdb_tx_err(EIO);
387 			break;
388 		}
389 		case 'M': {	/* Write memory. */
390 			uintmax_t addr, size;
391 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
392 			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
393 				gdb_tx_err(EINVAL);
394 				break;
395 			}
396 			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
397 				gdb_tx_err(EIO);
398 			else
399 				gdb_tx_ok();
400 			break;
401 		}
402 		case 'P': {	/* Write register. */
403 			char *val;
404 			uintmax_t reg;
405 			val = gdb_rxp;
406 			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
407 			    !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
408 				gdb_tx_err(EINVAL);
409 				break;
410 			}
411 			gdb_cpu_setreg(reg, val);
412 			gdb_tx_ok();
413 			break;
414 		}
415 		case 'q':	/* General query. */
416 			if (gdb_rx_equal("C")) {
417 				gdb_tx_begin('Q');
418 				gdb_tx_char('C');
419 				gdb_tx_varhex((long)kdb_thread->td_tid);
420 				gdb_tx_end();
421 			} else if (gdb_rx_equal("Supported")) {
422 				gdb_do_qsupported(&host_features);
423 			} else if (gdb_rx_equal("fThreadInfo")) {
424 				thr_iter = kdb_thr_first();
425 				gdb_do_threadinfo(&thr_iter);
426 			} else if (gdb_rx_equal("sThreadInfo")) {
427 				gdb_do_threadinfo(&thr_iter);
428 			} else if (gdb_rx_equal("Search:memory:")) {
429 				gdb_do_mem_search();
430 			} else if (!gdb_cpu_query())
431 				gdb_tx_empty();
432 			break;
433 		case 's': {	/* Step. */
434 			uintmax_t addr;
435 			register_t pc;
436 			if (!gdb_rx_varhex(&addr)) {
437 				pc = addr;
438 				gdb_cpu_setreg(GDB_REG_PC, &pc);
439 			}
440 			kdb_cpu_set_singlestep();
441 			gdb_listening = 1;
442 			return (1);
443 		}
444 		case 'S': {	/* Step with signal. */
445 			uintmax_t addr, sig;
446 			register_t pc;
447 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
448 			    !gdb_rx_varhex(&addr)) {
449 				pc = addr;
450 				gdb_cpu_setreg(GDB_REG_PC, &pc);
451 			}
452 			kdb_cpu_set_singlestep();
453 			gdb_listening = 1;
454 			return (1);
455 		}
456 		case 'T': {	/* Thread alive. */
457 			intmax_t tid;
458 			if (gdb_rx_varhex(&tid)) {
459 				gdb_tx_err(EINVAL);
460 				break;
461 			}
462 			if (kdb_thr_lookup(tid) != NULL)
463 				gdb_tx_ok();
464 			else
465 				gdb_tx_err(ENOENT);
466 			break;
467 		}
468 		case -1:
469 			/* Empty command. Treat as unknown command. */
470 			/* FALLTHROUGH */
471 		default:
472 			/* Unknown command. Send empty response. */
473 			gdb_tx_empty();
474 			break;
475 		}
476 	}
477 	(void)kdb_jmpbuf(prev_jb);
478 	return (0);
479 }
480