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;
59 struct gdb_dbgport *dp, **iter;
64 SET_FOREACH(iter, gdb_dbgport_set) {
66 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
67 dp->gdb_active = (pri >= 0) ? 0 : -1;
73 if (gdb_cur != NULL) {
74 printf("GDB: debug ports:");
75 SET_FOREACH(iter, gdb_dbgport_set) {
77 if (dp->gdb_active == 0)
78 printf(" %s", dp->gdb_name);
82 printf("GDB: no debug ports present\n");
83 if (gdb_cur != NULL) {
85 printf("GDB: current port: %s\n", gdb_cur->gdb_name);
87 if (gdb_cur != NULL) {
88 cur_pri = (boothowto & RB_GDB) ? 2 : 0;
96 gdb_trap(int type, int code)
99 struct thread *thr_iter;
102 prev_jb = kdb_jmpbuf(jb);
103 if (setjmp(jb) != 0) {
104 printf("%s bailing, hopefully back to ddb!\n", __func__);
106 (void)kdb_jmpbuf(prev_jb);
112 * Send a T packet. We currently do not support watchpoints (the
113 * awatch, rwatch or watch elements).
116 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
117 gdb_tx_varhex(GDB_REG_PC);
119 gdb_tx_reg(GDB_REG_PC);
121 gdb_tx_str("thread:");
122 gdb_tx_varhex((long)kdb_thread->td_tid);
124 gdb_tx_end(); /* XXX check error condition. */
127 while (gdb_rx_begin() == 0) {
128 /* printf("GDB: got '%s'\n", gdb_rxp); */
129 switch (gdb_rx_char()) {
130 case '?': /* Last signal. */
132 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
135 case 'c': { /* Continue. */
138 if (!gdb_rx_varhex(&addr)) {
140 gdb_cpu_setreg(GDB_REG_PC, &pc);
142 kdb_cpu_clear_singlestep();
146 case 'C': { /* Continue with signal. */
149 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
150 !gdb_rx_varhex(&addr)) {
152 gdb_cpu_setreg(GDB_REG_PC, &pc);
154 kdb_cpu_clear_singlestep();
158 case 'D': { /* Detach */
160 kdb_cpu_clear_singlestep();
163 case 'g': { /* Read registers. */
166 for (r = 0; r < GDB_NREGS; r++)
171 case 'G': /* Write registers. */
174 case 'H': { /* Set thread. */
178 if (gdb_rx_varhex(&tid)) {
183 thr = kdb_thr_lookup(tid);
193 case 'k': /* Kill request. */
194 kdb_cpu_clear_singlestep();
197 case 'm': { /* Read memory. */
198 uintmax_t addr, size;
199 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
200 gdb_rx_varhex(&size)) {
205 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
211 case 'M': { /* Write memory. */
212 uintmax_t addr, size;
213 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
214 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
218 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
224 case 'P': { /* Write register. */
228 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
229 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
233 gdb_cpu_setreg(reg, val);
237 case 'q': /* General query. */
238 if (gdb_rx_equal("fThreadInfo")) {
239 thr_iter = kdb_thr_first();
241 gdb_tx_hex((long)thr_iter->td_tid, 8);
243 } else if (gdb_rx_equal("sThreadInfo")) {
244 if (thr_iter == NULL) {
248 thr_iter = kdb_thr_next(thr_iter);
249 if (thr_iter != NULL) {
251 gdb_tx_hex((long)thr_iter->td_tid, 8);
257 } else if (!gdb_cpu_query())
260 case 's': { /* Step. */
263 if (!gdb_rx_varhex(&addr)) {
265 gdb_cpu_setreg(GDB_REG_PC, &pc);
267 kdb_cpu_set_singlestep();
271 case 'S': { /* Step with signal. */
274 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
275 !gdb_rx_varhex(&addr)) {
277 gdb_cpu_setreg(GDB_REG_PC, &pc);
279 kdb_cpu_set_singlestep();
283 case 'T': { /* Thread alive. */
285 if (gdb_rx_varhex(&tid)) {
289 if (kdb_thr_lookup(tid) != NULL)
296 /* Empty command. Treat as unknown command. */
299 /* Unknown command. Send empty response. */
304 (void)kdb_jmpbuf(prev_jb);