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, 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)
98 struct thread *thr_iter;
102 * Send a T packet. We currently do not support watchpoints (the
103 * awatch, rwatch or watch elements).
106 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
107 gdb_tx_varhex(GDB_REG_PC);
109 gdb_tx_reg(GDB_REG_PC);
111 gdb_tx_str("thread:");
112 gdb_tx_varhex((long)kdb_thread->td_tid);
114 gdb_tx_end(); /* XXX check error condition. */
117 while (gdb_rx_begin() == 0) {
118 /* printf("GDB: got '%s'\n", gdb_rxp); */
119 switch (gdb_rx_char()) {
120 case '?': /* Last signal. */
122 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
125 case 'c': { /* Continue. */
128 if (!gdb_rx_varhex(&addr)) {
130 gdb_cpu_setreg(GDB_REG_PC, &pc);
132 kdb_cpu_clear_singlestep();
136 case 'C': { /* Continue with signal. */
139 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
140 !gdb_rx_varhex(&addr)) {
142 gdb_cpu_setreg(GDB_REG_PC, &pc);
144 kdb_cpu_clear_singlestep();
148 case 'g': { /* Read registers. */
151 for (r = 0; r < GDB_NREGS; r++)
156 case 'G': /* Write registers. */
159 case 'H': { /* Set thread. */
163 if (gdb_rx_varhex(&tid)) {
168 thr = kdb_thr_lookup(tid);
178 case 'k': /* Kill request. */
179 kdb_cpu_clear_singlestep();
182 case 'm': { /* Read memory. */
183 uintmax_t addr, size;
184 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
185 gdb_rx_varhex(&size)) {
190 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
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() != ':') {
203 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
209 case 'P': { /* Write register. */
213 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
214 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
218 gdb_cpu_setreg(reg, val);
222 case 'q': /* General query. */
223 if (gdb_rx_equal("fThreadInfo")) {
224 thr_iter = kdb_thr_first();
226 gdb_tx_hex((long)thr_iter->td_tid, 8);
228 } else if (gdb_rx_equal("sThreadInfo")) {
229 if (thr_iter == NULL) {
233 thr_iter = kdb_thr_next(thr_iter);
234 if (thr_iter != NULL) {
236 gdb_tx_hex((long)thr_iter->td_tid, 8);
242 } else if (!gdb_cpu_query())
245 case 's': { /* Step. */
248 if (!gdb_rx_varhex(&addr)) {
250 gdb_cpu_setreg(GDB_REG_PC, &pc);
252 kdb_cpu_set_singlestep();
256 case 'S': { /* Step with signal. */
259 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
260 !gdb_rx_varhex(&addr)) {
262 gdb_cpu_setreg(GDB_REG_PC, &pc);
264 kdb_cpu_set_singlestep();
268 case 'T': { /* Thread alive. */
270 if (gdb_rx_varhex(&tid)) {
274 if (kdb_thr_lookup(tid) != NULL)
281 /* Empty command. Treat as unknown command. */
284 /* Unknown command. Send empty response. */