2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/reboot.h>
38 #include <machine/gdb_machdep.h>
39 #include <machine/kdb.h>
42 #include <gdb/gdb_int.h>
44 static dbbe_init_f gdb_init;
45 static dbbe_trap_f gdb_trap;
47 KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap);
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);
53 struct gdb_dbgport *gdb_cur = NULL;
54 int gdb_listening = 0;
56 static unsigned char gdb_bindata[64];
61 struct gdb_dbgport *dp, **iter;
66 SET_FOREACH(iter, gdb_dbgport_set) {
68 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
69 dp->gdb_active = (pri >= 0) ? 0 : -1;
75 if (gdb_cur != NULL) {
76 printf("GDB: debug ports:");
77 SET_FOREACH(iter, gdb_dbgport_set) {
79 if (dp->gdb_active == 0)
80 printf(" %s", dp->gdb_name);
84 printf("GDB: no debug ports present\n");
85 if (gdb_cur != NULL) {
87 printf("GDB: current port: %s\n", gdb_cur->gdb_name);
89 if (gdb_cur != NULL) {
90 cur_pri = (boothowto & RB_GDB) ? 2 : 0;
98 gdb_trap(int type, int code)
101 struct thread *thr_iter;
104 prev_jb = kdb_jmpbuf(jb);
105 if (setjmp(jb) != 0) {
106 printf("%s bailing, hopefully back to ddb!\n", __func__);
108 (void)kdb_jmpbuf(prev_jb);
114 * Send a T packet. We currently do not support watchpoints (the
115 * awatch, rwatch or watch elements).
118 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
119 gdb_tx_varhex(GDB_REG_PC);
121 gdb_tx_reg(GDB_REG_PC);
123 gdb_tx_str("thread:");
124 gdb_tx_varhex((long)kdb_thread->td_tid);
126 gdb_tx_end(); /* XXX check error condition. */
129 while (gdb_rx_begin() == 0) {
130 /* printf("GDB: got '%s'\n", gdb_rxp); */
131 switch (gdb_rx_char()) {
132 case '?': /* Last signal. */
134 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
137 case 'c': { /* Continue. */
140 if (!gdb_rx_varhex(&addr)) {
142 gdb_cpu_setreg(GDB_REG_PC, &pc);
144 kdb_cpu_clear_singlestep();
148 case 'C': { /* Continue with signal. */
151 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
152 !gdb_rx_varhex(&addr)) {
154 gdb_cpu_setreg(GDB_REG_PC, &pc);
156 kdb_cpu_clear_singlestep();
160 case 'D': { /* Detach */
162 kdb_cpu_clear_singlestep();
165 case 'g': { /* Read registers. */
168 for (r = 0; r < GDB_NREGS; r++)
173 case 'G': /* Write registers. */
176 case 'H': { /* Set thread. */
180 if (gdb_rx_varhex(&tid)) {
185 thr = kdb_thr_lookup(tid);
195 case 'k': /* Kill request. */
196 kdb_cpu_clear_singlestep();
199 case 'm': { /* Read memory. */
200 uintmax_t addr, size;
201 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
202 gdb_rx_varhex(&size)) {
207 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
213 case 'M': { /* Write memory. */
214 uintmax_t addr, size;
215 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
216 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
220 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
226 case 'P': { /* Write register. */
230 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
231 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
235 gdb_cpu_setreg(reg, val);
239 case 'q': /* General query. */
240 if (gdb_rx_equal("fThreadInfo")) {
241 thr_iter = kdb_thr_first();
243 gdb_tx_hex((long)thr_iter->td_tid, 8);
245 } else if (gdb_rx_equal("sThreadInfo")) {
246 if (thr_iter == NULL) {
250 thr_iter = kdb_thr_next(thr_iter);
251 if (thr_iter != NULL) {
253 gdb_tx_hex((long)thr_iter->td_tid, 8);
259 } else if (gdb_rx_equal("Search:memory:")) {
262 const unsigned char *found;
263 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' ||
264 gdb_rx_varhex(&size) || gdb_rx_char() != ';' ||
265 gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) {
269 if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, patlen, &found)) {
275 gdb_tx_hex((intmax_t)(uintptr_t)found, 8);
281 } else if (!gdb_cpu_query())
284 case 's': { /* Step. */
287 if (!gdb_rx_varhex(&addr)) {
289 gdb_cpu_setreg(GDB_REG_PC, &pc);
291 kdb_cpu_set_singlestep();
295 case 'S': { /* Step with signal. */
298 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
299 !gdb_rx_varhex(&addr)) {
301 gdb_cpu_setreg(GDB_REG_PC, &pc);
303 kdb_cpu_set_singlestep();
307 case 'T': { /* Thread alive. */
309 if (gdb_rx_varhex(&tid)) {
313 if (kdb_thr_lookup(tid) != NULL)
320 /* Empty command. Treat as unknown command. */
323 /* Unknown command. Send empty response. */
328 (void)kdb_jmpbuf(prev_jb);