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 AUTHOR ``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 AUTHOR 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/ctype.h> 33 #include <sys/kdb.h> 34 #include <sys/ttydefaults.h> 35 36 #include <machine/gdb_machdep.h> 37 #include <machine/kdb.h> 38 39 #include <gdb/gdb.h> 40 #include <gdb/gdb_int.h> 41 42 static char gdb_rxbuf[GDB_BUFSZ]; 43 char *gdb_rxp = NULL; 44 size_t gdb_rxsz = 0; 45 static char gdb_txbuf[GDB_BUFSZ]; 46 char *gdb_txp = NULL; /* Used in inline functions. */ 47 48 #define C2N(c) (((c) < 'A') ? (c) - '0' : \ 49 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a')) 50 #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10) 51 52 /* 53 * Get a single character 54 */ 55 56 static int 57 gdb_getc(void) 58 { 59 int c; 60 61 do 62 c = gdb_cur->gdb_getc(); 63 while (c == -1); 64 65 if (c == CTRL('C')) { 66 printf("Received ^C; trying to switch back to ddb.\n"); 67 68 if (kdb_dbbe_select("ddb") != 0) 69 printf("The ddb backend could not be selected.\n"); 70 else { 71 printf("using longjmp, hope it works!\n"); 72 kdb_reenter(); 73 } 74 } 75 return (c); 76 } 77 78 /* 79 * Functions to receive and extract from a packet. 80 */ 81 82 int 83 gdb_rx_begin(void) 84 { 85 int c, cksum; 86 87 gdb_rxp = NULL; 88 do { 89 /* 90 * Wait for the start character, ignore all others. 91 * XXX needs a timeout. 92 */ 93 while ((c = gdb_getc()) != '$') 94 ; 95 96 /* Read until a # or end of buffer is found. */ 97 cksum = 0; 98 gdb_rxsz = 0; 99 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) { 100 c = gdb_getc(); 101 if (c == '#') 102 break; 103 gdb_rxbuf[gdb_rxsz++] = c; 104 cksum += c; 105 } 106 gdb_rxbuf[gdb_rxsz] = 0; 107 cksum &= 0xff; 108 109 /* Bail out on a buffer overflow. */ 110 if (c != '#') { 111 gdb_cur->gdb_putc('-'); 112 return (ENOSPC); 113 } 114 115 c = gdb_getc(); 116 cksum -= (C2N(c) << 4) & 0xf0; 117 c = gdb_getc(); 118 cksum -= C2N(c) & 0x0f; 119 gdb_cur->gdb_putc((cksum == 0) ? '+' : '-'); 120 if (cksum != 0) 121 printf("GDB: packet `%s' has invalid checksum\n", 122 gdb_rxbuf); 123 } while (cksum != 0); 124 125 gdb_rxp = gdb_rxbuf; 126 return (0); 127 } 128 129 int 130 gdb_rx_equal(const char *str) 131 { 132 int len; 133 134 len = strlen(str); 135 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0) 136 return (0); 137 gdb_rxp += len; 138 gdb_rxsz -= len; 139 return (1); 140 } 141 142 int 143 gdb_rx_mem(unsigned char *addr, size_t size) 144 { 145 unsigned char *p; 146 void *prev; 147 jmp_buf jb; 148 size_t cnt; 149 int ret; 150 unsigned char c; 151 152 if (size * 2 != gdb_rxsz) 153 return (-1); 154 155 prev = kdb_jmpbuf(jb); 156 ret = setjmp(jb); 157 if (ret == 0) { 158 p = addr; 159 cnt = size; 160 while (cnt-- > 0) { 161 c = (C2N(gdb_rxp[0]) << 4) & 0xf0; 162 c |= C2N(gdb_rxp[1]) & 0x0f; 163 *p++ = c; 164 gdb_rxsz -= 2; 165 gdb_rxp += 2; 166 } 167 kdb_cpu_sync_icache(addr, size); 168 } 169 (void)kdb_jmpbuf(prev); 170 return ((ret == 0) ? 1 : 0); 171 } 172 173 int 174 gdb_rx_varhex(uintmax_t *vp) 175 { 176 uintmax_t v; 177 int c, neg; 178 179 c = gdb_rx_char(); 180 neg = (c == '-') ? 1 : 0; 181 if (neg == 1) 182 c = gdb_rx_char(); 183 if (!isxdigit(c)) { 184 gdb_rxp -= ((c == -1) ? 0 : 1) + neg; 185 gdb_rxsz += ((c == -1) ? 0 : 1) + neg; 186 return (-1); 187 } 188 v = 0; 189 do { 190 v <<= 4; 191 v += C2N(c); 192 c = gdb_rx_char(); 193 } while (isxdigit(c)); 194 if (c != -1) { 195 gdb_rxp--; 196 gdb_rxsz++; 197 } 198 *vp = (neg) ? -v : v; 199 return (0); 200 } 201 202 /* 203 * Function to build and send a package. 204 */ 205 206 void 207 gdb_tx_begin(char tp) 208 { 209 210 gdb_txp = gdb_txbuf; 211 if (tp != '\0') 212 gdb_tx_char(tp); 213 } 214 215 int 216 gdb_tx_end(void) 217 { 218 const char *p; 219 int runlen; 220 unsigned char c, cksum; 221 222 do { 223 gdb_cur->gdb_putc('$'); 224 225 cksum = 0; 226 p = gdb_txbuf; 227 while (p < gdb_txp) { 228 /* Send a character and start run-length encoding. */ 229 c = *p++; 230 gdb_cur->gdb_putc(c); 231 cksum += c; 232 runlen = 0; 233 /* Determine run-length and update checksum. */ 234 while (p < gdb_txp && *p == c) { 235 runlen++; 236 p++; 237 } 238 /* Emit the run-length encoded string. */ 239 while (runlen >= 97) { 240 gdb_cur->gdb_putc('*'); 241 cksum += '*'; 242 gdb_cur->gdb_putc(97+29); 243 cksum += 97+29; 244 runlen -= 97; 245 if (runlen > 0) { 246 gdb_cur->gdb_putc(c); 247 cksum += c; 248 runlen--; 249 } 250 } 251 if (runlen == 1) { 252 gdb_cur->gdb_putc(c); 253 cksum += c; 254 runlen--; 255 } 256 if (runlen == 0) 257 continue; 258 /* Don't emit '$', '#', '+' or '-'. */ 259 if (runlen == 7) { 260 gdb_cur->gdb_putc(c); 261 cksum += c; 262 runlen--; 263 } 264 if (runlen == 6 || runlen == 14 || runlen == 16) { 265 gdb_cur->gdb_putc(c); 266 cksum += c; 267 runlen--; 268 } 269 gdb_cur->gdb_putc('*'); 270 cksum += '*'; 271 gdb_cur->gdb_putc(runlen+29); 272 cksum += runlen+29; 273 } 274 275 gdb_cur->gdb_putc('#'); 276 c = cksum >> 4; 277 gdb_cur->gdb_putc(N2C(c)); 278 c = cksum & 0x0f; 279 gdb_cur->gdb_putc(N2C(c)); 280 281 c = gdb_getc(); 282 } while (c != '+'); 283 284 return (0); 285 } 286 287 int 288 gdb_tx_mem(const unsigned char *addr, size_t size) 289 { 290 void *prev; 291 jmp_buf jb; 292 int ret; 293 294 prev = kdb_jmpbuf(jb); 295 ret = setjmp(jb); 296 if (ret == 0) { 297 while (size-- > 0) { 298 *gdb_txp++ = N2C(*addr >> 4); 299 *gdb_txp++ = N2C(*addr & 0x0f); 300 addr++; 301 } 302 } 303 (void)kdb_jmpbuf(prev); 304 return ((ret == 0) ? 1 : 0); 305 } 306 307 void 308 gdb_tx_reg(int regnum) 309 { 310 unsigned char *regp; 311 size_t regsz; 312 313 regp = gdb_cpu_getreg(regnum, ®sz); 314 if (regp == NULL) { 315 /* Register unavailable. */ 316 while (regsz--) { 317 gdb_tx_char('x'); 318 gdb_tx_char('x'); 319 } 320 } else 321 gdb_tx_mem(regp, regsz); 322 } 323