gdb_main.c revision 133446
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: head/sys/gdb/gdb_main.c 133446 2004-08-10 19:32:33Z marcel $"); 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 44static dbbe_init_f gdb_init; 45static dbbe_trap_f gdb_trap; 46 47KDB_BACKEND(gdb, gdb_init, NULL, gdb_trap); 48 49GDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL); 50SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 51 52struct gdb_dbgport *gdb_cur = NULL; 53 54static int 55gdb_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 92static int 93gdb_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 if (!gdb_rx_varhex(&addr)) 124 gdb_cpu_setreg(GDB_REG_PC, addr); 125 kdb_cpu_clear_singlestep(); 126 return (1); 127 } 128 case 'C': { /* Continue with signal. */ 129 uintmax_t addr, sig; 130 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 131 !gdb_rx_varhex(&addr)) 132 gdb_cpu_setreg(GDB_REG_PC, addr); 133 kdb_cpu_clear_singlestep(); 134 return (1); 135 } 136 case 'g': { /* Read registers. */ 137 size_t r; 138 gdb_tx_begin(0); 139 for (r = 0; r < GDB_NREGS; r++) 140 gdb_tx_reg(r); 141 gdb_tx_end(); 142 break; 143 } 144 case 'G': /* Write registers. */ 145 gdb_tx_err(0); 146 break; 147 case 'H': { /* Set thread. */ 148 intmax_t tid; 149 struct thread *thr; 150 gdb_rx_char(); 151 gdb_rx_varhex(&tid); 152 if (tid > 0) { 153 thr = kdb_thr_lookup(tid); 154 if (thr == NULL) { 155 gdb_tx_err(ENOENT); 156 break; 157 } 158 kdb_thr_select(thr); 159 } 160 gdb_tx_ok(); 161 break; 162 } 163 case 'k': /* Kill request. */ 164 kdb_cpu_clear_singlestep(); 165 return (1); 166 case 'm': { /* Read memory. */ 167 uintmax_t addr, size; 168 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 169 gdb_rx_varhex(&size)) { 170 gdb_tx_err(EINVAL); 171 break; 172 } 173 gdb_tx_begin(0); 174 if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 175 gdb_tx_end(); 176 else 177 gdb_tx_err(EIO); 178 break; 179 } 180 case 'M': { /* Write memory. */ 181 uintmax_t addr, size; 182 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 183 gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 184 gdb_tx_err(EINVAL); 185 break; 186 } 187 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 188 gdb_tx_err(EIO); 189 else 190 gdb_tx_ok(); 191 break; 192 } 193 case 'P': { /* Write register. */ 194 uintmax_t reg, val; 195 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 196 gdb_rx_varhex(&val)) { 197 gdb_tx_err(EINVAL); 198 break; 199 } 200 gdb_cpu_setreg(reg, val); 201 gdb_tx_ok(); 202 break; 203 } 204 case 'q': /* General query. */ 205 if (gdb_rx_equal("fThreadInfo")) { 206 thr_iter = kdb_thr_first(); 207 gdb_tx_begin('m'); 208 gdb_tx_hex((long)thr_iter->td_tid, 8); 209 gdb_tx_end(); 210 } else if (gdb_rx_equal("sThreadInfo")) { 211 if (thr_iter == NULL) { 212 gdb_tx_err(ENXIO); 213 break; 214 } 215 thr_iter = kdb_thr_next(thr_iter); 216 if (thr_iter != NULL) { 217 gdb_tx_begin('m'); 218 gdb_tx_hex((long)thr_iter->td_tid, 8); 219 gdb_tx_end(); 220 } else { 221 gdb_tx_begin('l'); 222 gdb_tx_end(); 223 } 224 } else if (!gdb_cpu_query()) 225 gdb_tx_empty(); 226 break; 227 case 's': { /* Step. */ 228 uintmax_t addr; 229 if (!gdb_rx_varhex(&addr)) 230 gdb_cpu_setreg(GDB_REG_PC, addr); 231 kdb_cpu_set_singlestep(); 232 return (1); 233 } 234 case 'S': { /* Step with signal. */ 235 uintmax_t addr, sig; 236 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 237 !gdb_rx_varhex(&addr)) 238 gdb_cpu_setreg(GDB_REG_PC, addr); 239 kdb_cpu_set_singlestep(); 240 return (1); 241 } 242 case 'T': { /* Thread alive. */ 243 intmax_t tid; 244 gdb_rx_varhex(&tid); 245 if (kdb_thr_lookup(tid) != NULL) 246 gdb_tx_ok(); 247 else 248 gdb_tx_err(ENOENT); 249 break; 250 } 251 case -1: 252 /* Empty command. Treat as unknown command. */ 253 /* FALLTHROUGH */ 254 default: 255 /* Unknown command. Send empty response. */ 256 gdb_tx_empty(); 257 break; 258 } 259 } 260 return (0); 261} 262