2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/reboot.h>
40 #include <machine/gdb_machdep.h>
41 #include <machine/kdb.h>
44 #include <gdb/gdb_int.h>
46 static dbbe_init_f gdb_init;
47 static dbbe_trap_f gdb_trap;
49 KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap);
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);
55 struct gdb_dbgport *gdb_cur = NULL;
56 int gdb_listening = 0;
58 static unsigned char gdb_bindata[64];
63 struct gdb_dbgport *dp, **iter;
68 SET_FOREACH(iter, gdb_dbgport_set) {
70 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
71 dp->gdb_active = (pri >= 0) ? 0 : -1;
77 if (gdb_cur != NULL) {
78 printf("GDB: debug ports:");
79 SET_FOREACH(iter, gdb_dbgport_set) {
81 if (dp->gdb_active == 0)
82 printf(" %s", dp->gdb_name);
86 printf("GDB: no debug ports present\n");
87 if (gdb_cur != NULL) {
89 printf("GDB: current port: %s\n", gdb_cur->gdb_name);
91 if (gdb_cur != NULL) {
92 cur_pri = (boothowto & RB_GDB) ? 2 : 0;
100 gdb_do_mem_search(void)
104 const unsigned char *found;
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)) {
112 if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata,
119 gdb_tx_hex((intmax_t)(uintptr_t)found, 8);
127 gdb_trap(int type, int code)
130 struct thread *thr_iter;
133 prev_jb = kdb_jmpbuf(jb);
134 if (setjmp(jb) != 0) {
135 printf("%s bailing, hopefully back to ddb!\n", __func__);
137 (void)kdb_jmpbuf(prev_jb);
143 * Send a T packet. We currently do not support watchpoints (the
144 * awatch, rwatch or watch elements).
147 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
148 gdb_tx_varhex(GDB_REG_PC);
150 gdb_tx_reg(GDB_REG_PC);
152 gdb_tx_str("thread:");
153 gdb_tx_varhex((long)kdb_thread->td_tid);
155 gdb_tx_end(); /* XXX check error condition. */
158 while (gdb_rx_begin() == 0) {
159 /* printf("GDB: got '%s'\n", gdb_rxp); */
160 switch (gdb_rx_char()) {
161 case '?': /* Last signal. */
163 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
166 case 'c': { /* Continue. */
169 if (!gdb_rx_varhex(&addr)) {
171 gdb_cpu_setreg(GDB_REG_PC, &pc);
173 kdb_cpu_clear_singlestep();
177 case 'C': { /* Continue with signal. */
180 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
181 !gdb_rx_varhex(&addr)) {
183 gdb_cpu_setreg(GDB_REG_PC, &pc);
185 kdb_cpu_clear_singlestep();
189 case 'D': { /* Detach */
191 kdb_cpu_clear_singlestep();
194 case 'g': { /* Read registers. */
197 for (r = 0; r < GDB_NREGS; r++)
202 case 'G': /* Write registers. */
205 case 'H': { /* Set thread. */
209 if (gdb_rx_varhex(&tid)) {
214 thr = kdb_thr_lookup(tid);
224 case 'k': /* Kill request. */
225 kdb_cpu_clear_singlestep();
228 case 'm': { /* Read memory. */
229 uintmax_t addr, size;
230 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
231 gdb_rx_varhex(&size)) {
236 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
242 case 'M': { /* Write memory. */
243 uintmax_t addr, size;
244 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
245 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
249 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
255 case 'P': { /* Write register. */
259 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
260 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
264 gdb_cpu_setreg(reg, val);
268 case 'q': /* General query. */
269 if (gdb_rx_equal("fThreadInfo")) {
270 thr_iter = kdb_thr_first();
272 gdb_tx_hex((long)thr_iter->td_tid, 8);
274 } else if (gdb_rx_equal("sThreadInfo")) {
275 if (thr_iter == NULL) {
279 thr_iter = kdb_thr_next(thr_iter);
280 if (thr_iter != NULL) {
282 gdb_tx_hex((long)thr_iter->td_tid, 8);
288 } else if (gdb_rx_equal("Search:memory:")) {
290 } else if (!gdb_cpu_query())
293 case 's': { /* Step. */
296 if (!gdb_rx_varhex(&addr)) {
298 gdb_cpu_setreg(GDB_REG_PC, &pc);
300 kdb_cpu_set_singlestep();
304 case 'S': { /* Step with signal. */
307 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
308 !gdb_rx_varhex(&addr)) {
310 gdb_cpu_setreg(GDB_REG_PC, &pc);
312 kdb_cpu_set_singlestep();
316 case 'T': { /* Thread alive. */
318 if (gdb_rx_varhex(&tid)) {
322 if (kdb_thr_lookup(tid) != NULL)
329 /* Empty command. Treat as unknown command. */
332 /* Unknown command. Send empty response. */
337 (void)kdb_jmpbuf(prev_jb);