xref: /freebsd/sys/gdb/gdb_main.c (revision 98dc8da58c698aed1843a912c7c5f5b9dfef3656)
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 static int
166 gdb_trap(int type, int code)
167 {
168 	jmp_buf jb;
169 	struct thread *thr_iter;
170 	void *prev_jb;
171 
172 	prev_jb = kdb_jmpbuf(jb);
173 	if (setjmp(jb) != 0) {
174 		printf("%s bailing, hopefully back to ddb!\n", __func__);
175 		gdb_listening = 0;
176 		(void)kdb_jmpbuf(prev_jb);
177 		return (1);
178 	}
179 
180 	gdb_listening = 0;
181 	/*
182 	 * Send a T packet. We currently do not support watchpoints (the
183 	 * awatch, rwatch or watch elements).
184 	 */
185 	gdb_tx_begin('T');
186 	gdb_tx_hex(gdb_cpu_signal(type, code), 2);
187 	gdb_tx_varhex(GDB_REG_PC);
188 	gdb_tx_char(':');
189 	gdb_tx_reg(GDB_REG_PC);
190 	gdb_tx_char(';');
191 	gdb_tx_str("thread:");
192 	gdb_tx_varhex((long)kdb_thread->td_tid);
193 	gdb_tx_char(';');
194 	gdb_tx_end();			/* XXX check error condition. */
195 
196 	thr_iter = NULL;
197 	while (gdb_rx_begin() == 0) {
198 		/* printf("GDB: got '%s'\n", gdb_rxp); */
199 		switch (gdb_rx_char()) {
200 		case '?':	/* Last signal. */
201 			gdb_tx_begin('S');
202 			gdb_tx_hex(gdb_cpu_signal(type, code), 2);
203 			gdb_tx_end();
204 			break;
205 		case 'c': {	/* Continue. */
206 			uintmax_t addr;
207 			register_t pc;
208 			if (!gdb_rx_varhex(&addr)) {
209 				pc = addr;
210 				gdb_cpu_setreg(GDB_REG_PC, &pc);
211 			}
212 			kdb_cpu_clear_singlestep();
213 			gdb_listening = 1;
214 			return (1);
215 		}
216 		case 'C': {	/* Continue with signal. */
217 			uintmax_t addr, sig;
218 			register_t pc;
219 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
220 			    !gdb_rx_varhex(&addr)) {
221 				pc = addr;
222 				gdb_cpu_setreg(GDB_REG_PC, &pc);
223 			}
224 			kdb_cpu_clear_singlestep();
225 			gdb_listening = 1;
226 			return (1);
227 		}
228 		case 'D': {     /* Detach */
229 			gdb_tx_ok();
230 			kdb_cpu_clear_singlestep();
231 			return (1);
232 		}
233 		case 'g': {	/* Read registers. */
234 			size_t r;
235 			gdb_tx_begin(0);
236 			for (r = 0; r < GDB_NREGS; r++)
237 				gdb_tx_reg(r);
238 			gdb_tx_end();
239 			break;
240 		}
241 		case 'G':	/* Write registers. */
242 			gdb_tx_err(0);
243 			break;
244 		case 'H': {	/* Set thread. */
245 			intmax_t tid;
246 			struct thread *thr;
247 			gdb_rx_char();
248 			if (gdb_rx_varhex(&tid)) {
249 				gdb_tx_err(EINVAL);
250 				break;
251 			}
252 			if (tid > 0) {
253 				thr = kdb_thr_lookup(tid);
254 				if (thr == NULL) {
255 					gdb_tx_err(ENOENT);
256 					break;
257 				}
258 				kdb_thr_select(thr);
259 			}
260 			gdb_tx_ok();
261 			break;
262 		}
263 		case 'k':	/* Kill request. */
264 			kdb_cpu_clear_singlestep();
265 			gdb_listening = 1;
266 			return (1);
267 		case 'm': {	/* Read memory. */
268 			uintmax_t addr, size;
269 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
270 			    gdb_rx_varhex(&size)) {
271 				gdb_tx_err(EINVAL);
272 				break;
273 			}
274 			gdb_tx_begin(0);
275 			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
276 				gdb_tx_end();
277 			else
278 				gdb_tx_err(EIO);
279 			break;
280 		}
281 		case 'M': {	/* Write memory. */
282 			uintmax_t addr, size;
283 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
284 			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
285 				gdb_tx_err(EINVAL);
286 				break;
287 			}
288 			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
289 				gdb_tx_err(EIO);
290 			else
291 				gdb_tx_ok();
292 			break;
293 		}
294 		case 'P': {	/* Write register. */
295 			char *val;
296 			uintmax_t reg;
297 			val = gdb_rxp;
298 			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
299 			    !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
300 				gdb_tx_err(EINVAL);
301 				break;
302 			}
303 			gdb_cpu_setreg(reg, val);
304 			gdb_tx_ok();
305 			break;
306 		}
307 		case 'q':	/* General query. */
308 			if (gdb_rx_equal("C")) {
309 				gdb_tx_begin('Q');
310 				gdb_tx_char('C');
311 				gdb_tx_varhex((long)kdb_thread->td_tid);
312 				gdb_tx_end();
313 			} else if (gdb_rx_equal("fThreadInfo")) {
314 				thr_iter = kdb_thr_first();
315 				gdb_do_threadinfo(&thr_iter);
316 			} else if (gdb_rx_equal("sThreadInfo")) {
317 				gdb_do_threadinfo(&thr_iter);
318 			} else if (gdb_rx_equal("Search:memory:")) {
319 				gdb_do_mem_search();
320 			} else if (!gdb_cpu_query())
321 				gdb_tx_empty();
322 			break;
323 		case 's': {	/* Step. */
324 			uintmax_t addr;
325 			register_t pc;
326 			if (!gdb_rx_varhex(&addr)) {
327 				pc = addr;
328 				gdb_cpu_setreg(GDB_REG_PC, &pc);
329 			}
330 			kdb_cpu_set_singlestep();
331 			gdb_listening = 1;
332 			return (1);
333 		}
334 		case 'S': {	/* Step with signal. */
335 			uintmax_t addr, sig;
336 			register_t pc;
337 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
338 			    !gdb_rx_varhex(&addr)) {
339 				pc = addr;
340 				gdb_cpu_setreg(GDB_REG_PC, &pc);
341 			}
342 			kdb_cpu_set_singlestep();
343 			gdb_listening = 1;
344 			return (1);
345 		}
346 		case 'T': {	/* Thread alive. */
347 			intmax_t tid;
348 			if (gdb_rx_varhex(&tid)) {
349 				gdb_tx_err(EINVAL);
350 				break;
351 			}
352 			if (kdb_thr_lookup(tid) != NULL)
353 				gdb_tx_ok();
354 			else
355 				gdb_tx_err(ENOENT);
356 			break;
357 		}
358 		case -1:
359 			/* Empty command. Treat as unknown command. */
360 			/* FALLTHROUGH */
361 		default:
362 			/* Unknown command. Send empty response. */
363 			gdb_tx_empty();
364 			break;
365 		}
366 	}
367 	(void)kdb_jmpbuf(prev_jb);
368 	return (0);
369 }
370