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