gdb_main.c revision 158949
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: head/sys/gdb/gdb_main.c 158949 2006-05-26 11:52:59Z phk $");
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
44static dbbe_init_f gdb_init;
45static dbbe_trap_f gdb_trap;
46
47KDB_BACKEND(gdb, gdb_init, NULL, gdb_trap);
48
49static struct gdb_dbgport null_gdb_dbgport;
50DATA_SET(gdb_dbgport_set, null_gdb_dbgport);
51SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
52
53struct gdb_dbgport *gdb_cur = NULL;
54int gdb_listening = 0;
55
56static int
57gdb_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
95static int
96gdb_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 'g': {	/* Read registers. */
149			size_t r;
150			gdb_tx_begin(0);
151			for (r = 0; r < GDB_NREGS; r++)
152				gdb_tx_reg(r);
153			gdb_tx_end();
154			break;
155		}
156		case 'G':	/* Write registers. */
157			gdb_tx_err(0);
158			break;
159		case 'H': {	/* Set thread. */
160			intmax_t tid;
161			struct thread *thr;
162			gdb_rx_char();
163			if (gdb_rx_varhex(&tid)) {
164				gdb_tx_err(EINVAL);
165				break;
166			}
167			if (tid > 0) {
168				thr = kdb_thr_lookup(tid);
169				if (thr == NULL) {
170					gdb_tx_err(ENOENT);
171					break;
172				}
173				kdb_thr_select(thr);
174			}
175			gdb_tx_ok();
176			break;
177		}
178		case 'k':	/* Kill request. */
179			kdb_cpu_clear_singlestep();
180			gdb_listening = 1;
181			return (1);
182		case 'm': {	/* Read memory. */
183			uintmax_t addr, size;
184			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
185			    gdb_rx_varhex(&size)) {
186				gdb_tx_err(EINVAL);
187				break;
188			}
189			gdb_tx_begin(0);
190			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
191				gdb_tx_end();
192			else
193				gdb_tx_err(EIO);
194			break;
195		}
196		case 'M': {	/* Write memory. */
197			uintmax_t addr, size;
198			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
199			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
200				gdb_tx_err(EINVAL);
201				break;
202			}
203			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
204				gdb_tx_err(EIO);
205			else
206				gdb_tx_ok();
207			break;
208		}
209		case 'P': {	/* Write register. */
210			char *val;
211			uintmax_t reg;
212			val = gdb_rxp;
213			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
214			    !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
215				gdb_tx_err(EINVAL);
216				break;
217			}
218			gdb_cpu_setreg(reg, val);
219			gdb_tx_ok();
220			break;
221		}
222		case 'q':	/* General query. */
223			if (gdb_rx_equal("fThreadInfo")) {
224				thr_iter = kdb_thr_first();
225				gdb_tx_begin('m');
226				gdb_tx_hex((long)thr_iter->td_tid, 8);
227				gdb_tx_end();
228			} else if (gdb_rx_equal("sThreadInfo")) {
229				if (thr_iter == NULL) {
230					gdb_tx_err(ENXIO);
231					break;
232				}
233				thr_iter = kdb_thr_next(thr_iter);
234				if (thr_iter != NULL) {
235					gdb_tx_begin('m');
236					gdb_tx_hex((long)thr_iter->td_tid, 8);
237					gdb_tx_end();
238				} else {
239					gdb_tx_begin('l');
240					gdb_tx_end();
241				}
242			} else if (!gdb_cpu_query())
243				gdb_tx_empty();
244			break;
245		case 's': {	/* Step. */
246			uintmax_t addr;
247			register_t pc;
248			if (!gdb_rx_varhex(&addr)) {
249				pc = addr;
250				gdb_cpu_setreg(GDB_REG_PC, &pc);
251			}
252			kdb_cpu_set_singlestep();
253			gdb_listening = 1;
254			return (1);
255		}
256		case 'S': {	/* Step with signal. */
257			uintmax_t addr, sig;
258			register_t pc;
259			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
260			    !gdb_rx_varhex(&addr)) {
261				pc = addr;
262				gdb_cpu_setreg(GDB_REG_PC, &pc);
263			}
264			kdb_cpu_set_singlestep();
265			gdb_listening = 1;
266			return (1);
267		}
268		case 'T': {	/* Thread alive. */
269			intmax_t tid;
270			if (gdb_rx_varhex(&tid)) {
271				gdb_tx_err(EINVAL);
272				break;
273			}
274			if (kdb_thr_lookup(tid) != NULL)
275				gdb_tx_ok();
276			else
277				gdb_tx_err(ENOENT);
278			break;
279		}
280		case -1:
281			/* Empty command. Treat as unknown command. */
282			/* FALLTHROUGH */
283		default:
284			/* Unknown command. Send empty response. */
285			gdb_tx_empty();
286			break;
287		}
288	}
289	return (0);
290}
291