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_do_threadinfo(struct thread **thr_iter)
129 static struct thread * const done_sentinel = (void *)(uintptr_t)1;
130 static const size_t tidsz_hex = sizeof(lwpid_t) * 2;
133 if (*thr_iter == NULL) {
138 if (*thr_iter == done_sentinel) {
147 *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1);
148 *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) {
151 gdb_tx_varhex((*thr_iter)->td_tid);
155 * Can't send EOF and "some" in same packet, so set a sentinel to send
156 * EOF when GDB asks us next.
158 if (*thr_iter == NULL && tds_sent > 0)
159 *thr_iter = done_sentinel;
166 gdb_trap(int type, int code)
169 struct thread *thr_iter;
172 prev_jb = kdb_jmpbuf(jb);
173 if (setjmp(jb) != 0) {
174 printf("%s bailing, hopefully back to ddb!\n", __func__);
176 (void)kdb_jmpbuf(prev_jb);
182 * Send a T packet. We currently do not support watchpoints (the
183 * awatch, rwatch or watch elements).
186 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
187 gdb_tx_varhex(GDB_REG_PC);
189 gdb_tx_reg(GDB_REG_PC);
191 gdb_tx_str("thread:");
192 gdb_tx_varhex((long)kdb_thread->td_tid);
194 gdb_tx_end(); /* XXX check error condition. */
197 while (gdb_rx_begin() == 0) {
198 /* printf("GDB: got '%s'\n", gdb_rxp); */
199 switch (gdb_rx_char()) {
200 case '?': /* Last signal. */
202 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
205 case 'c': { /* Continue. */
208 if (!gdb_rx_varhex(&addr)) {
210 gdb_cpu_setreg(GDB_REG_PC, &pc);
212 kdb_cpu_clear_singlestep();
216 case 'C': { /* Continue with signal. */
219 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
220 !gdb_rx_varhex(&addr)) {
222 gdb_cpu_setreg(GDB_REG_PC, &pc);
224 kdb_cpu_clear_singlestep();
228 case 'D': { /* Detach */
230 kdb_cpu_clear_singlestep();
233 case 'g': { /* Read registers. */
236 for (r = 0; r < GDB_NREGS; r++)
241 case 'G': /* Write registers. */
244 case 'H': { /* Set thread. */
248 if (gdb_rx_varhex(&tid)) {
253 thr = kdb_thr_lookup(tid);
263 case 'k': /* Kill request. */
264 kdb_cpu_clear_singlestep();
267 case 'm': { /* Read memory. */
268 uintmax_t addr, size;
269 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
270 gdb_rx_varhex(&size)) {
275 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
281 case 'M': { /* Write memory. */
282 uintmax_t addr, size;
283 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
284 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
288 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
294 case 'P': { /* Write register. */
298 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
299 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
303 gdb_cpu_setreg(reg, val);
307 case 'q': /* General query. */
308 if (gdb_rx_equal("C")) {
311 gdb_tx_varhex((long)kdb_thread->td_tid);
313 } else if (gdb_rx_equal("fThreadInfo")) {
314 thr_iter = kdb_thr_first();
315 gdb_do_threadinfo(&thr_iter);
316 } else if (gdb_rx_equal("sThreadInfo")) {
317 gdb_do_threadinfo(&thr_iter);
318 } else if (gdb_rx_equal("Search:memory:")) {
320 } else if (!gdb_cpu_query())
323 case 's': { /* Step. */
326 if (!gdb_rx_varhex(&addr)) {
328 gdb_cpu_setreg(GDB_REG_PC, &pc);
330 kdb_cpu_set_singlestep();
334 case 'S': { /* Step with signal. */
337 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
338 !gdb_rx_varhex(&addr)) {
340 gdb_cpu_setreg(GDB_REG_PC, &pc);
342 kdb_cpu_set_singlestep();
346 case 'T': { /* Thread alive. */
348 if (gdb_rx_varhex(&tid)) {
352 if (kdb_thr_lookup(tid) != NULL)
359 /* Empty command. Treat as unknown command. */
362 /* Unknown command. Send empty response. */
367 (void)kdb_jmpbuf(prev_jb);