xref: /freebsd/sys/gdb/gdb_main.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 GDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL);
50 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
51 
52 struct gdb_dbgport *gdb_cur = NULL;
53 
54 static int
55 gdb_init(void)
56 {
57 	struct gdb_dbgport *dp, **iter;
58 	int cur_pri, pri;
59 
60 	gdb_cur = NULL;
61 	cur_pri = -1;
62 	SET_FOREACH(iter, gdb_dbgport_set) {
63 		dp = *iter;
64 		pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
65 		dp->gdb_active = (pri >= 0) ? 0 : -1;
66 		if (pri > cur_pri) {
67 			cur_pri = pri;
68 			gdb_cur = dp;
69 		}
70 	}
71 	if (gdb_cur != NULL) {
72 		printf("GDB: debug ports:");
73 		SET_FOREACH(iter, gdb_dbgport_set) {
74 			dp = *iter;
75 			if (dp->gdb_active == 0)
76 				printf(" %s", dp->gdb_name);
77 		}
78 		printf("\n");
79 	} else
80 		printf("GDB: no debug ports present\n");
81 	if (gdb_cur != NULL) {
82 		gdb_cur->gdb_init();
83 		printf("GDB: current port: %s\n", gdb_cur->gdb_name);
84 	}
85 	if (gdb_cur != NULL)
86 		cur_pri = (boothowto & RB_GDB) ? 2 : 0;
87 	else
88 		cur_pri = -1;
89 	return (cur_pri);
90 }
91 
92 static int
93 gdb_trap(int type, int code)
94 {
95 	struct thread *thr_iter;
96 
97 	/*
98 	 * Send a T packet. We currently do not support watchpoints (the
99 	 * awatch, rwatch or watch elements).
100 	 */
101 	gdb_tx_begin('T');
102 	gdb_tx_hex(gdb_cpu_signal(type, code), 2);
103 	gdb_tx_varhex(GDB_REG_PC);
104 	gdb_tx_char(':');
105 	gdb_tx_reg(GDB_REG_PC);
106 	gdb_tx_char(';');
107 	gdb_tx_str("thread:");
108 	gdb_tx_varhex((long)kdb_thread->td_tid);
109 	gdb_tx_char(';');
110 	gdb_tx_end();			/* XXX check error condition. */
111 
112 	thr_iter = NULL;
113 	while (gdb_rx_begin() == 0) {
114 		/* printf("GDB: got '%s'\n", gdb_rxp); */
115 		switch (gdb_rx_char()) {
116 		case '?':	/* Last signal. */
117 			gdb_tx_begin('S');
118 			gdb_tx_hex(gdb_cpu_signal(type, code), 2);
119 			gdb_tx_end();
120 			break;
121 		case 'c': {	/* Continue. */
122 			uintmax_t addr;
123 			register_t pc;
124 			if (!gdb_rx_varhex(&addr)) {
125 				pc = addr;
126 				gdb_cpu_setreg(GDB_REG_PC, &pc);
127 			}
128 			kdb_cpu_clear_singlestep();
129 			return (1);
130 		}
131 		case 'C': {	/* Continue with signal. */
132 			uintmax_t addr, sig;
133 			register_t pc;
134 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
135 			    !gdb_rx_varhex(&addr)) {
136 				pc = addr;
137 				gdb_cpu_setreg(GDB_REG_PC, &pc);
138 			}
139 			kdb_cpu_clear_singlestep();
140 			return (1);
141 		}
142 		case 'g': {	/* Read registers. */
143 			size_t r;
144 			gdb_tx_begin(0);
145 			for (r = 0; r < GDB_NREGS; r++)
146 				gdb_tx_reg(r);
147 			gdb_tx_end();
148 			break;
149 		}
150 		case 'G':	/* Write registers. */
151 			gdb_tx_err(0);
152 			break;
153 		case 'H': {	/* Set thread. */
154 			intmax_t tid;
155 			struct thread *thr;
156 			gdb_rx_char();
157 			if (gdb_rx_varhex(&tid)) {
158 				gdb_tx_err(EINVAL);
159 				break;
160 			}
161 			if (tid > 0) {
162 				thr = kdb_thr_lookup(tid);
163 				if (thr == NULL) {
164 					gdb_tx_err(ENOENT);
165 					break;
166 				}
167 				kdb_thr_select(thr);
168 			}
169 			gdb_tx_ok();
170 			break;
171 		}
172 		case 'k':	/* Kill request. */
173 			kdb_cpu_clear_singlestep();
174 			return (1);
175 		case 'm': {	/* Read memory. */
176 			uintmax_t addr, size;
177 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
178 			    gdb_rx_varhex(&size)) {
179 				gdb_tx_err(EINVAL);
180 				break;
181 			}
182 			gdb_tx_begin(0);
183 			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
184 				gdb_tx_end();
185 			else
186 				gdb_tx_err(EIO);
187 			break;
188 		}
189 		case 'M': {	/* Write memory. */
190 			uintmax_t addr, size;
191 			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
192 			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
193 				gdb_tx_err(EINVAL);
194 				break;
195 			}
196 			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
197 				gdb_tx_err(EIO);
198 			else
199 				gdb_tx_ok();
200 			break;
201 		}
202 		case 'P': {	/* Write register. */
203 			char *val;
204 			uintmax_t reg;
205 			val = gdb_rxp;
206 			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
207 			    !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
208 				gdb_tx_err(EINVAL);
209 				break;
210 			}
211 			gdb_cpu_setreg(reg, val);
212 			gdb_tx_ok();
213 			break;
214 		}
215 		case 'q':	/* General query. */
216 			if (gdb_rx_equal("fThreadInfo")) {
217 				thr_iter = kdb_thr_first();
218 				gdb_tx_begin('m');
219 				gdb_tx_hex((long)thr_iter->td_tid, 8);
220 				gdb_tx_end();
221 			} else if (gdb_rx_equal("sThreadInfo")) {
222 				if (thr_iter == NULL) {
223 					gdb_tx_err(ENXIO);
224 					break;
225 				}
226 				thr_iter = kdb_thr_next(thr_iter);
227 				if (thr_iter != NULL) {
228 					gdb_tx_begin('m');
229 					gdb_tx_hex((long)thr_iter->td_tid, 8);
230 					gdb_tx_end();
231 				} else {
232 					gdb_tx_begin('l');
233 					gdb_tx_end();
234 				}
235 			} else if (!gdb_cpu_query())
236 				gdb_tx_empty();
237 			break;
238 		case 's': {	/* Step. */
239 			uintmax_t addr;
240 			register_t pc;
241 			if (!gdb_rx_varhex(&addr)) {
242 				pc = addr;
243 				gdb_cpu_setreg(GDB_REG_PC, &pc);
244 			}
245 			kdb_cpu_set_singlestep();
246 			return (1);
247 		}
248 		case 'S': {	/* Step with signal. */
249 			uintmax_t addr, sig;
250 			register_t pc;
251 			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
252 			    !gdb_rx_varhex(&addr)) {
253 				pc = addr;
254 				gdb_cpu_setreg(GDB_REG_PC, &pc);
255 			}
256 			kdb_cpu_set_singlestep();
257 			return (1);
258 		}
259 		case 'T': {	/* Thread alive. */
260 			intmax_t tid;
261 			if (gdb_rx_varhex(&tid)) {
262 				gdb_tx_err(EINVAL);
263 				break;
264 			}
265 			if (kdb_thr_lookup(tid) != NULL)
266 				gdb_tx_ok();
267 			else
268 				gdb_tx_err(ENOENT);
269 			break;
270 		}
271 		case -1:
272 			/* Empty command. Treat as unknown command. */
273 			/* FALLTHROUGH */
274 		default:
275 			/* Unknown command. Send empty response. */
276 			gdb_tx_empty();
277 			break;
278 		}
279 	}
280 	return (0);
281 }
282