1 /*- 2 * Copyright (c) 2004 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 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. 14 * 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. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kdb.h> 33 #include <sys/kernel.h> 34 #include <sys/pcpu.h> 35 #include <sys/proc.h> 36 #include <sys/reboot.h> 37 38 #include <machine/gdb_machdep.h> 39 #include <machine/kdb.h> 40 41 #include <gdb/gdb.h> 42 #include <gdb/gdb_int.h> 43 44 static dbbe_init_f gdb_init; 45 static dbbe_trap_f gdb_trap; 46 47 KDB_BACKEND(gdb, gdb_init, NULL, gdb_trap); 48 49 GDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL); 50 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 51 52 struct gdb_dbgport *gdb_cur = NULL; 53 54 static int 55 gdb_init(void) 56 { 57 struct gdb_dbgport *dp, **iter; 58 int cur_pri, pri; 59 60 gdb_cur = NULL; 61 cur_pri = -1; 62 SET_FOREACH(iter, gdb_dbgport_set) { 63 dp = *iter; 64 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 65 dp->gdb_active = (pri >= 0) ? 0 : -1; 66 if (pri > cur_pri) { 67 cur_pri = pri; 68 gdb_cur = dp; 69 } 70 } 71 if (gdb_cur != NULL) { 72 printf("GDB: debug ports:"); 73 SET_FOREACH(iter, gdb_dbgport_set) { 74 dp = *iter; 75 if (dp->gdb_active == 0) 76 printf(" %s", dp->gdb_name); 77 } 78 printf("\n"); 79 } else 80 printf("GDB: no debug ports present\n"); 81 if (gdb_cur != NULL) { 82 gdb_cur->gdb_init(); 83 printf("GDB: current port: %s\n", gdb_cur->gdb_name); 84 } 85 if (gdb_cur != NULL) 86 cur_pri = (boothowto & RB_GDB) ? 2 : 0; 87 else 88 cur_pri = -1; 89 return (cur_pri); 90 } 91 92 static int 93 gdb_trap(int type, int code) 94 { 95 struct thread *thr_iter; 96 97 /* 98 * Send a T packet. We currently do not support watchpoints (the 99 * awatch, rwatch or watch elements). 100 */ 101 gdb_tx_begin('T'); 102 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 103 gdb_tx_varhex(GDB_REG_PC); 104 gdb_tx_char(':'); 105 gdb_tx_reg(GDB_REG_PC); 106 gdb_tx_char(';'); 107 gdb_tx_str("thread:"); 108 gdb_tx_varhex((long)kdb_thread->td_tid); 109 gdb_tx_char(';'); 110 gdb_tx_end(); /* XXX check error condition. */ 111 112 thr_iter = NULL; 113 while (gdb_rx_begin() == 0) { 114 /* printf("GDB: got '%s'\n", gdb_rxp); */ 115 switch (gdb_rx_char()) { 116 case '?': /* Last signal. */ 117 gdb_tx_begin('S'); 118 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 119 gdb_tx_end(); 120 break; 121 case 'c': { /* Continue. */ 122 uintmax_t addr; 123 register_t pc; 124 if (!gdb_rx_varhex(&addr)) { 125 pc = addr; 126 gdb_cpu_setreg(GDB_REG_PC, &pc); 127 } 128 kdb_cpu_clear_singlestep(); 129 return (1); 130 } 131 case 'C': { /* Continue with signal. */ 132 uintmax_t addr, sig; 133 register_t pc; 134 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 135 !gdb_rx_varhex(&addr)) { 136 pc = addr; 137 gdb_cpu_setreg(GDB_REG_PC, &pc); 138 } 139 kdb_cpu_clear_singlestep(); 140 return (1); 141 } 142 case 'g': { /* Read registers. */ 143 size_t r; 144 gdb_tx_begin(0); 145 for (r = 0; r < GDB_NREGS; r++) 146 gdb_tx_reg(r); 147 gdb_tx_end(); 148 break; 149 } 150 case 'G': /* Write registers. */ 151 gdb_tx_err(0); 152 break; 153 case 'H': { /* Set thread. */ 154 intmax_t tid; 155 struct thread *thr; 156 gdb_rx_char(); 157 gdb_rx_varhex(&tid); 158 if (tid > 0) { 159 thr = kdb_thr_lookup(tid); 160 if (thr == NULL) { 161 gdb_tx_err(ENOENT); 162 break; 163 } 164 kdb_thr_select(thr); 165 } 166 gdb_tx_ok(); 167 break; 168 } 169 case 'k': /* Kill request. */ 170 kdb_cpu_clear_singlestep(); 171 return (1); 172 case 'm': { /* Read memory. */ 173 uintmax_t addr, size; 174 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 175 gdb_rx_varhex(&size)) { 176 gdb_tx_err(EINVAL); 177 break; 178 } 179 gdb_tx_begin(0); 180 if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 181 gdb_tx_end(); 182 else 183 gdb_tx_err(EIO); 184 break; 185 } 186 case 'M': { /* Write memory. */ 187 uintmax_t addr, size; 188 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 189 gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 190 gdb_tx_err(EINVAL); 191 break; 192 } 193 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 194 gdb_tx_err(EIO); 195 else 196 gdb_tx_ok(); 197 break; 198 } 199 case 'P': { /* Write register. */ 200 char *val; 201 uintmax_t reg; 202 val = gdb_rxp; 203 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 204 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 205 gdb_tx_err(EINVAL); 206 break; 207 } 208 gdb_cpu_setreg(reg, val); 209 gdb_tx_ok(); 210 break; 211 } 212 case 'q': /* General query. */ 213 if (gdb_rx_equal("fThreadInfo")) { 214 thr_iter = kdb_thr_first(); 215 gdb_tx_begin('m'); 216 gdb_tx_hex((long)thr_iter->td_tid, 8); 217 gdb_tx_end(); 218 } else if (gdb_rx_equal("sThreadInfo")) { 219 if (thr_iter == NULL) { 220 gdb_tx_err(ENXIO); 221 break; 222 } 223 thr_iter = kdb_thr_next(thr_iter); 224 if (thr_iter != NULL) { 225 gdb_tx_begin('m'); 226 gdb_tx_hex((long)thr_iter->td_tid, 8); 227 gdb_tx_end(); 228 } else { 229 gdb_tx_begin('l'); 230 gdb_tx_end(); 231 } 232 } else if (!gdb_cpu_query()) 233 gdb_tx_empty(); 234 break; 235 case 's': { /* Step. */ 236 uintmax_t addr; 237 register_t pc; 238 if (!gdb_rx_varhex(&addr)) { 239 pc = addr; 240 gdb_cpu_setreg(GDB_REG_PC, &pc); 241 } 242 kdb_cpu_set_singlestep(); 243 return (1); 244 } 245 case 'S': { /* Step with signal. */ 246 uintmax_t addr, sig; 247 register_t pc; 248 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 249 !gdb_rx_varhex(&addr)) { 250 pc = addr; 251 gdb_cpu_setreg(GDB_REG_PC, &pc); 252 } 253 kdb_cpu_set_singlestep(); 254 return (1); 255 } 256 case 'T': { /* Thread alive. */ 257 intmax_t tid; 258 gdb_rx_varhex(&tid); 259 if (kdb_thr_lookup(tid) != NULL) 260 gdb_tx_ok(); 261 else 262 gdb_tx_err(ENOENT); 263 break; 264 } 265 case -1: 266 /* Empty command. Treat as unknown command. */ 267 /* FALLTHROUGH */ 268 default: 269 /* Unknown command. Send empty response. */ 270 gdb_tx_empty(); 271 break; 272 } 273 } 274 return (0); 275 } 276