| 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
| 2 | Copyright 1988, 1991, 1992 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | /* Remote communication protocol. |
| 21 | All values are encoded in ascii hex digits. |
| 22 | |
| 23 | Request Packet |
| 24 | |
| 25 | read registers g |
| 26 | reply XX....X Each byte of register data |
| 27 | is described by two hex digits. |
| 28 | Registers are in the internal order |
| 29 | for GDB, and the bytes in a register |
| 30 | are in the same order the machine uses. |
| 31 | or ENN for an error. |
| 32 | |
| 33 | write regs GXX..XX Each byte of register data |
| 34 | is described by two hex digits. |
| 35 | reply OK for success |
| 36 | ENN for an error |
| 37 | |
| 38 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
| 39 | reply XX..XX XX..XX is mem contents |
| 40 | or ENN NN is errno |
| 41 | |
| 42 | write mem MAA..AA,LLLL:XX..XX |
| 43 | AA..AA is address, |
| 44 | LLLL is number of bytes, |
| 45 | XX..XX is data |
| 46 | reply OK for success |
| 47 | ENN for an error |
| 48 | |
| 49 | cont cAA..AA AA..AA is address to resume |
| 50 | If AA..AA is omitted, |
| 51 | resume at same address. |
| 52 | |
| 53 | step sAA..AA AA..AA is address to resume |
| 54 | If AA..AA is omitted, |
| 55 | resume at same address. |
| 56 | |
| 57 | last signal ? Reply the current reason for stopping. |
| 58 | This is the same reply as is generated |
| 59 | for step or cont : SAA where AA is the |
| 60 | signal number. |
| 61 | |
| 62 | There is no immediate reply to step or cont. |
| 63 | The reply comes when the machine stops. |
| 64 | It is SAA AA is the "signal number" |
| 65 | |
| 66 | or... TAAPPPPPPPPFFFFFFFF |
| 67 | where AA is the signal number, |
| 68 | PPPPPPPP is the PC (PC_REGNUM), and |
| 69 | FFFFFFFF is the frame ptr (FP_REGNUM). |
| 70 | |
| 71 | kill req k |
| 72 | */ |
| 73 | |
| 74 | #include "defs.h" |
| 75 | #include <string.h> |
| 76 | #include <fcntl.h> |
| 77 | #include "frame.h" |
| 78 | #include "inferior.h" |
| 79 | #include "target.h" |
| 80 | #include "wait.h" |
| 81 | #include "terminal.h" |
| 82 | #include "gdbcmd.h" |
| 83 | |
| 84 | #if !defined(DONT_USE_REMOTE) |
| 85 | #ifdef USG |
| 86 | #include <sys/types.h> |
| 87 | #endif |
| 88 | |
| 89 | #include <signal.h> |
| 90 | |
| 91 | /* Prototypes for local functions */ |
| 92 | |
| 93 | static void |
| 94 | remote_write_bytes PARAMS ((CORE_ADDR, char *, int)); |
| 95 | |
| 96 | static void |
| 97 | remote_read_bytes PARAMS ((CORE_ADDR, char *, int)); |
| 98 | |
| 99 | static void |
| 100 | remote_files_info PARAMS ((struct target_ops *)); |
| 101 | |
| 102 | static int |
| 103 | remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *)); |
| 104 | |
| 105 | static void |
| 106 | remote_prepare_to_store PARAMS ((void)); |
| 107 | |
| 108 | static void |
| 109 | remote_fetch_registers PARAMS ((int)); |
| 110 | |
| 111 | static void |
| 112 | remote_resume PARAMS ((int, int)); |
| 113 | |
| 114 | static void |
| 115 | remote_open PARAMS ((char *, int)); |
| 116 | |
| 117 | static void |
| 118 | remote_close PARAMS ((int)); |
| 119 | |
| 120 | static void |
| 121 | remote_store_registers PARAMS ((int)); |
| 122 | |
| 123 | static void |
| 124 | getpkt PARAMS ((char *)); |
| 125 | |
| 126 | static void |
| 127 | putpkt PARAMS ((char *)); |
| 128 | |
| 129 | static void |
| 130 | remote_send PARAMS ((char *)); |
| 131 | |
| 132 | static int |
| 133 | readchar PARAMS ((void)); |
| 134 | |
| 135 | static int |
| 136 | remote_wait PARAMS ((WAITTYPE *)); |
| 137 | |
| 138 | static int |
| 139 | tohex PARAMS ((int)); |
| 140 | |
| 141 | static int |
| 142 | fromhex PARAMS ((int)); |
| 143 | |
| 144 | static void |
| 145 | remote_detach PARAMS ((char *, int)); |
| 146 | |
| 147 | |
| 148 | extern struct target_ops remote_ops; /* Forward decl */ |
| 149 | |
| 150 | static int kiodebug = 0; |
| 151 | static int timeout = 5; |
| 152 | |
| 153 | #if 0 |
| 154 | int icache; |
| 155 | #endif |
| 156 | |
| 157 | /* Descriptor for I/O to remote machine. Initialize it to -1 so that |
| 158 | remote_open knows that we don't have a file open when the program |
| 159 | starts. */ |
| 160 | int remote_desc = -1; |
| 161 | |
| 162 | #define PBUFSIZ 1024 |
| 163 | |
| 164 | /* Maximum number of bytes to read/write at once. The value here |
| 165 | is chosen to fill up a packet (the headers account for the 32). */ |
| 166 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) |
| 167 | |
| 168 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
| 169 | #if REGISTER_BYTES > MAXBUFBYTES |
| 170 | #undef PBUFSIZ |
| 171 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
| 172 | #endif |
| 173 | \f |
| 174 | /* Called when SIGALRM signal sent due to alarm() timeout. */ |
| 175 | #ifndef HAVE_TERMIO |
| 176 | void |
| 177 | remote_timer (signo) |
| 178 | int signo; |
| 179 | { |
| 180 | if (kiodebug) |
| 181 | printf ("remote_timer called\n"); |
| 182 | |
| 183 | alarm (timeout); |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | /* Clean up connection to a remote debugger. */ |
| 188 | |
| 189 | /* ARGSUSED */ |
| 190 | static void |
| 191 | remote_close (quitting) |
| 192 | int quitting; |
| 193 | { |
| 194 | if (remote_desc >= 0) |
| 195 | close (remote_desc); |
| 196 | remote_desc = -1; |
| 197 | } |
| 198 | |
| 199 | /* Translate baud rates from integers to damn B_codes. Unix should |
| 200 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ |
| 201 | |
| 202 | #ifndef B19200 |
| 203 | #define B19200 EXTA |
| 204 | #endif |
| 205 | #ifndef B38400 |
| 206 | #define B38400 EXTB |
| 207 | #endif |
| 208 | |
| 209 | |
| 210 | |
| 211 | static struct {int rate, damn_b;} baudtab[] = { |
| 212 | {0, B0}, |
| 213 | {50, B50}, |
| 214 | {75, B75}, |
| 215 | {110, B110}, |
| 216 | {134, B134}, |
| 217 | {150, B150}, |
| 218 | {200, B200}, |
| 219 | {300, B300}, |
| 220 | {600, B600}, |
| 221 | {1200, B1200}, |
| 222 | {1800, B1800}, |
| 223 | {2400, B2400}, |
| 224 | {4800, B4800}, |
| 225 | {9600, B9600}, |
| 226 | {19200, B19200}, |
| 227 | {38400, B38400}, |
| 228 | {-1, -1}, |
| 229 | }; |
| 230 | |
| 231 | static int |
| 232 | damn_b (rate) |
| 233 | int rate; |
| 234 | { |
| 235 | int i; |
| 236 | |
| 237 | for (i = 0; baudtab[i].rate != -1; i++) |
| 238 | if (rate == baudtab[i].rate) return baudtab[i].damn_b; |
| 239 | return B38400; /* Random */ |
| 240 | } |
| 241 | |
| 242 | /* Open a connection to a remote debugger. |
| 243 | NAME is the filename used for communication. */ |
| 244 | |
| 245 | static void |
| 246 | remote_open (name, from_tty) |
| 247 | char *name; |
| 248 | int from_tty; |
| 249 | { |
| 250 | TERMINAL sg; |
| 251 | int a_rate, b_rate = 0; |
| 252 | int baudrate_set = 0; |
| 253 | |
| 254 | if (name == 0) |
| 255 | error ( |
| 256 | "To open a remote debug connection, you need to specify what serial\n\ |
| 257 | device is attached to the remote system (e.g. /dev/ttya)."); |
| 258 | |
| 259 | target_preopen (from_tty); |
| 260 | |
| 261 | remote_close (0); |
| 262 | |
| 263 | #if 0 |
| 264 | dcache_init (); |
| 265 | #endif |
| 266 | |
| 267 | remote_desc = open (name, O_RDWR); |
| 268 | if (remote_desc < 0) |
| 269 | perror_with_name (name); |
| 270 | |
| 271 | if (baud_rate) |
| 272 | { |
| 273 | if (sscanf (baud_rate, "%d", &a_rate) == 1) |
| 274 | { |
| 275 | b_rate = damn_b (a_rate); |
| 276 | baudrate_set = 1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | ioctl (remote_desc, TIOCGETP, &sg); |
| 281 | #ifdef HAVE_TERMIO |
| 282 | sg.c_cc[VMIN] = 0; /* read with timeout. */ |
| 283 | sg.c_cc[VTIME] = timeout * 10; |
| 284 | sg.c_lflag &= ~(ICANON | ECHO); |
| 285 | sg.c_cflag &= ~PARENB; /* No parity */ |
| 286 | sg.c_cflag |= CS8; /* 8-bit path */ |
| 287 | if (baudrate_set) |
| 288 | sg.c_cflag = (sg.c_cflag & ~CBAUD) | b_rate; |
| 289 | #else |
| 290 | sg.sg_flags |= RAW | ANYP; |
| 291 | sg.sg_flags &= ~ECHO; |
| 292 | if (baudrate_set) |
| 293 | { |
| 294 | sg.sg_ispeed = b_rate; |
| 295 | sg.sg_ospeed = b_rate; |
| 296 | } |
| 297 | #endif |
| 298 | ioctl (remote_desc, TIOCSETP, &sg); |
| 299 | |
| 300 | if (from_tty) |
| 301 | printf ("Remote debugging using %s\n", name); |
| 302 | push_target (&remote_ops); /* Switch to using remote target now */ |
| 303 | |
| 304 | #ifndef HAVE_TERMIO |
| 305 | #ifndef NO_SIGINTERRUPT |
| 306 | /* Cause SIGALRM's to make reads fail. */ |
| 307 | if (siginterrupt (SIGALRM, 1) != 0) |
| 308 | perror ("remote_open: error in siginterrupt"); |
| 309 | #endif |
| 310 | |
| 311 | /* Set up read timeout timer. */ |
| 312 | if ((void (*)()) signal (SIGALRM, remote_timer) == (void (*)()) -1) |
| 313 | perror ("remote_open: error in signal"); |
| 314 | #endif |
| 315 | |
| 316 | /* Ack any packet which the remote side has already sent. */ |
| 317 | write (remote_desc, "+\r", 2); |
| 318 | putpkt ("?"); /* initiate a query from remote machine */ |
| 319 | |
| 320 | start_remote (); /* Initialize gdb process mechanisms */ |
| 321 | } |
| 322 | |
| 323 | /* remote_detach() |
| 324 | takes a program previously attached to and detaches it. |
| 325 | We better not have left any breakpoints |
| 326 | in the program or it'll die when it hits one. |
| 327 | Close the open connection to the remote debugger. |
| 328 | Use this when you want to detach and do something else |
| 329 | with your gdb. */ |
| 330 | |
| 331 | static void |
| 332 | remote_detach (args, from_tty) |
| 333 | char *args; |
| 334 | int from_tty; |
| 335 | { |
| 336 | if (args) |
| 337 | error ("Argument given to \"detach\" when remotely debugging."); |
| 338 | |
| 339 | pop_target (); |
| 340 | if (from_tty) |
| 341 | printf ("Ending remote debugging.\n"); |
| 342 | } |
| 343 | |
| 344 | /* Convert hex digit A to a number. */ |
| 345 | |
| 346 | static int |
| 347 | fromhex (a) |
| 348 | int a; |
| 349 | { |
| 350 | if (a >= '0' && a <= '9') |
| 351 | return a - '0'; |
| 352 | else if (a >= 'a' && a <= 'f') |
| 353 | return a - 'a' + 10; |
| 354 | else |
| 355 | error ("Reply contains invalid hex digit"); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | /* Convert number NIB to a hex digit. */ |
| 360 | |
| 361 | static int |
| 362 | tohex (nib) |
| 363 | int nib; |
| 364 | { |
| 365 | if (nib < 10) |
| 366 | return '0'+nib; |
| 367 | else |
| 368 | return 'a'+nib-10; |
| 369 | } |
| 370 | \f |
| 371 | /* Tell the remote machine to resume. */ |
| 372 | |
| 373 | static void |
| 374 | remote_resume (step, siggnal) |
| 375 | int step, siggnal; |
| 376 | { |
| 377 | char buf[PBUFSIZ]; |
| 378 | |
| 379 | if (siggnal) |
| 380 | error ("Can't send signals to a remote system. Try `handle %d ignore'.", |
| 381 | siggnal); |
| 382 | |
| 383 | #if 0 |
| 384 | dcache_flush (); |
| 385 | #endif |
| 386 | |
| 387 | strcpy (buf, step ? "s": "c"); |
| 388 | |
| 389 | putpkt (buf); |
| 390 | } |
| 391 | |
| 392 | /* Send ^C to target to halt it. Target will respond, and send us a |
| 393 | packet. */ |
| 394 | |
| 395 | void remote_interrupt(signo) |
| 396 | int signo; |
| 397 | { |
| 398 | |
| 399 | if (kiodebug) |
| 400 | printf ("remote_interrupt called\n"); |
| 401 | |
| 402 | write (remote_desc, "\003", 1); /* Send a ^C */ |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /* Wait until the remote machine stops, then return, |
| 407 | storing status in STATUS just as `wait' would. |
| 408 | Returns "pid" (though it's not clear what, if anything, that |
| 409 | means in the case of this target). */ |
| 410 | |
| 411 | static int |
| 412 | remote_wait (status) |
| 413 | WAITTYPE *status; |
| 414 | { |
| 415 | unsigned char buf[PBUFSIZ]; |
| 416 | void (*ofunc)(); |
| 417 | unsigned char *p; |
| 418 | int i; |
| 419 | long regno; |
| 420 | char regs[MAX_REGISTER_RAW_SIZE]; |
| 421 | |
| 422 | WSETEXIT ((*status), 0); |
| 423 | |
| 424 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
| 425 | getpkt ((char *) buf); |
| 426 | signal (SIGINT, ofunc); |
| 427 | |
| 428 | if (buf[0] == 'E') |
| 429 | error ("Remote failure reply: %s", buf); |
| 430 | if (buf[0] == 'T') |
| 431 | { |
| 432 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
| 433 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
| 434 | ss = signal number |
| 435 | n... = register number |
| 436 | r... = register contents |
| 437 | */ |
| 438 | |
| 439 | p = &buf[3]; /* after Txx */ |
| 440 | |
| 441 | while (*p) |
| 442 | { |
| 443 | regno = strtol (p, &p, 16); /* Read the register number */ |
| 444 | |
| 445 | if (*p++ != ':' |
| 446 | || regno >= NUM_REGS) |
| 447 | error ("Remote sent bad register number %s", buf); |
| 448 | |
| 449 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) |
| 450 | { |
| 451 | if (p[0] == 0 || p[1] == 0) |
| 452 | error ("Remote reply is too short: %s", buf); |
| 453 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 454 | p += 2; |
| 455 | } |
| 456 | |
| 457 | if (*p++ != ';') |
| 458 | error("Remote register badly formatted: %s", buf); |
| 459 | |
| 460 | supply_register (regno, regs); |
| 461 | } |
| 462 | } |
| 463 | else if (buf[0] != 'S') |
| 464 | error ("Invalid remote reply: %s", buf); |
| 465 | |
| 466 | WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2])))); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /* Read the remote registers into the block REGS. */ |
| 472 | /* Currently we just read all the registers, so we don't use regno. */ |
| 473 | /* ARGSUSED */ |
| 474 | static void |
| 475 | remote_fetch_registers (regno) |
| 476 | int regno; |
| 477 | { |
| 478 | char buf[PBUFSIZ]; |
| 479 | int i; |
| 480 | char *p; |
| 481 | char regs[REGISTER_BYTES]; |
| 482 | |
| 483 | sprintf (buf, "g"); |
| 484 | remote_send (buf); |
| 485 | |
| 486 | /* Reply describes registers byte by byte, each byte encoded as two |
| 487 | hex characters. Suck them all up, then supply them to the |
| 488 | register cacheing/storage mechanism. */ |
| 489 | |
| 490 | p = buf; |
| 491 | for (i = 0; i < REGISTER_BYTES; i++) |
| 492 | { |
| 493 | if (p[0] == 0 || p[1] == 0) |
| 494 | error ("Remote reply is too short: %s", buf); |
| 495 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 496 | p += 2; |
| 497 | } |
| 498 | for (i = 0; i < NUM_REGS; i++) |
| 499 | supply_register (i, ®s[REGISTER_BYTE(i)]); |
| 500 | } |
| 501 | |
| 502 | /* Prepare to store registers. Since we send them all, we have to |
| 503 | read out the ones we don't want to change first. */ |
| 504 | |
| 505 | static void |
| 506 | remote_prepare_to_store () |
| 507 | { |
| 508 | /* Make sure the entire registers array is valid. */ |
| 509 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); |
| 510 | } |
| 511 | |
| 512 | /* Store the remote registers from the contents of the block REGISTERS. |
| 513 | FIXME, eventually just store one register if that's all that is needed. */ |
| 514 | |
| 515 | /* ARGSUSED */ |
| 516 | static void |
| 517 | remote_store_registers (regno) |
| 518 | int regno; |
| 519 | { |
| 520 | char buf[PBUFSIZ]; |
| 521 | int i; |
| 522 | char *p; |
| 523 | |
| 524 | buf[0] = 'G'; |
| 525 | |
| 526 | /* Command describes registers byte by byte, |
| 527 | each byte encoded as two hex characters. */ |
| 528 | |
| 529 | p = buf + 1; |
| 530 | for (i = 0; i < REGISTER_BYTES; i++) |
| 531 | { |
| 532 | *p++ = tohex ((registers[i] >> 4) & 0xf); |
| 533 | *p++ = tohex (registers[i] & 0xf); |
| 534 | } |
| 535 | *p = '\0'; |
| 536 | |
| 537 | remote_send (buf); |
| 538 | } |
| 539 | |
| 540 | #if 0 |
| 541 | /* Read a word from remote address ADDR and return it. |
| 542 | This goes through the data cache. */ |
| 543 | |
| 544 | int |
| 545 | remote_fetch_word (addr) |
| 546 | CORE_ADDR addr; |
| 547 | { |
| 548 | if (icache) |
| 549 | { |
| 550 | extern CORE_ADDR text_start, text_end; |
| 551 | |
| 552 | if (addr >= text_start && addr < text_end) |
| 553 | { |
| 554 | int buffer; |
| 555 | xfer_core_file (addr, &buffer, sizeof (int)); |
| 556 | return buffer; |
| 557 | } |
| 558 | } |
| 559 | return dcache_fetch (addr); |
| 560 | } |
| 561 | |
| 562 | /* Write a word WORD into remote address ADDR. |
| 563 | This goes through the data cache. */ |
| 564 | |
| 565 | void |
| 566 | remote_store_word (addr, word) |
| 567 | CORE_ADDR addr; |
| 568 | int word; |
| 569 | { |
| 570 | dcache_poke (addr, word); |
| 571 | } |
| 572 | #endif /* 0 */ |
| 573 | \f |
| 574 | /* Write memory data directly to the remote machine. |
| 575 | This does not inform the data cache; the data cache uses this. |
| 576 | MEMADDR is the address in the remote memory space. |
| 577 | MYADDR is the address of the buffer in our space. |
| 578 | LEN is the number of bytes. */ |
| 579 | |
| 580 | static void |
| 581 | remote_write_bytes (memaddr, myaddr, len) |
| 582 | CORE_ADDR memaddr; |
| 583 | char *myaddr; |
| 584 | int len; |
| 585 | { |
| 586 | char buf[PBUFSIZ]; |
| 587 | int i; |
| 588 | char *p; |
| 589 | |
| 590 | if (len > PBUFSIZ / 2 - 20) |
| 591 | abort (); |
| 592 | |
| 593 | sprintf (buf, "M%x,%x:", memaddr, len); |
| 594 | |
| 595 | /* We send target system values byte by byte, in increasing byte addresses, |
| 596 | each byte encoded as two hex characters. */ |
| 597 | |
| 598 | p = buf + strlen (buf); |
| 599 | for (i = 0; i < len; i++) |
| 600 | { |
| 601 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); |
| 602 | *p++ = tohex (myaddr[i] & 0xf); |
| 603 | } |
| 604 | *p = '\0'; |
| 605 | |
| 606 | remote_send (buf); |
| 607 | } |
| 608 | |
| 609 | /* Read memory data directly from the remote machine. |
| 610 | This does not use the data cache; the data cache uses this. |
| 611 | MEMADDR is the address in the remote memory space. |
| 612 | MYADDR is the address of the buffer in our space. |
| 613 | LEN is the number of bytes. */ |
| 614 | |
| 615 | static void |
| 616 | remote_read_bytes (memaddr, myaddr, len) |
| 617 | CORE_ADDR memaddr; |
| 618 | char *myaddr; |
| 619 | int len; |
| 620 | { |
| 621 | char buf[PBUFSIZ]; |
| 622 | int i; |
| 623 | char *p; |
| 624 | |
| 625 | if (len > PBUFSIZ / 2 - 1) |
| 626 | abort (); |
| 627 | |
| 628 | sprintf (buf, "m%x,%x", memaddr, len); |
| 629 | remote_send (buf); |
| 630 | |
| 631 | /* Reply describes memory byte by byte, |
| 632 | each byte encoded as two hex characters. */ |
| 633 | |
| 634 | p = buf; |
| 635 | for (i = 0; i < len; i++) |
| 636 | { |
| 637 | if (p[0] == 0 || p[1] == 0) |
| 638 | error ("Remote reply is too short: %s", buf); |
| 639 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 640 | p += 2; |
| 641 | } |
| 642 | } |
| 643 | \f |
| 644 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring |
| 645 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
| 646 | nonzero. Returns length of data written or read; 0 for error. */ |
| 647 | |
| 648 | /* ARGSUSED */ |
| 649 | static int |
| 650 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) |
| 651 | CORE_ADDR memaddr; |
| 652 | char *myaddr; |
| 653 | int len; |
| 654 | int should_write; |
| 655 | struct target_ops *target; /* ignored */ |
| 656 | { |
| 657 | int origlen = len; |
| 658 | int xfersize; |
| 659 | while (len > 0) |
| 660 | { |
| 661 | if (len > MAXBUFBYTES) |
| 662 | xfersize = MAXBUFBYTES; |
| 663 | else |
| 664 | xfersize = len; |
| 665 | |
| 666 | if (should_write) |
| 667 | remote_write_bytes(memaddr, myaddr, xfersize); |
| 668 | else |
| 669 | remote_read_bytes (memaddr, myaddr, xfersize); |
| 670 | memaddr += xfersize; |
| 671 | myaddr += xfersize; |
| 672 | len -= xfersize; |
| 673 | } |
| 674 | return origlen; /* no error possible */ |
| 675 | } |
| 676 | |
| 677 | static void |
| 678 | remote_files_info (ignore) |
| 679 | struct target_ops *ignore; |
| 680 | { |
| 681 | printf ("Debugging a target over a serial line.\n"); |
| 682 | } |
| 683 | \f |
| 684 | /* |
| 685 | |
| 686 | A debug packet whose contents are <data> |
| 687 | is encapsulated for transmission in the form: |
| 688 | |
| 689 | $ <data> # CSUM1 CSUM2 |
| 690 | |
| 691 | <data> must be ASCII alphanumeric and cannot include characters |
| 692 | '$' or '#' |
| 693 | |
| 694 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit |
| 695 | checksum of <data>, the most significant nibble is sent first. |
| 696 | the hex digits 0-9,a-f are used. |
| 697 | |
| 698 | Receiver responds with: |
| 699 | |
| 700 | + - if CSUM is correct and ready for next packet |
| 701 | - - if CSUM is incorrect |
| 702 | |
| 703 | */ |
| 704 | |
| 705 | /* Read a single character from the remote end. |
| 706 | (If supported, we actually read many characters and buffer them up.) */ |
| 707 | |
| 708 | static int |
| 709 | readchar () |
| 710 | { |
| 711 | static int inbuf_index, inbuf_count; |
| 712 | #define INBUFSIZE PBUFSIZ |
| 713 | static char inbuf[INBUFSIZE]; |
| 714 | |
| 715 | if (inbuf_index >= inbuf_count) |
| 716 | { |
| 717 | /* Time to do another read... */ |
| 718 | inbuf_index = 0; |
| 719 | inbuf_count = 0; |
| 720 | inbuf[0] = 0; /* Just in case */ |
| 721 | #ifdef HAVE_TERMIO |
| 722 | /* termio does the timeout for us. */ |
| 723 | inbuf_count = read (remote_desc, inbuf, INBUFSIZE); |
| 724 | #else |
| 725 | alarm (timeout); |
| 726 | inbuf_count = read (remote_desc, inbuf, INBUFSIZE); |
| 727 | alarm (0); |
| 728 | #endif |
| 729 | } |
| 730 | |
| 731 | /* Just return the next character from the buffer. */ |
| 732 | return inbuf[inbuf_index++] & 0x7f; |
| 733 | } |
| 734 | |
| 735 | /* Send the command in BUF to the remote machine, |
| 736 | and read the reply into BUF. |
| 737 | Report an error if we get an error reply. */ |
| 738 | |
| 739 | static void |
| 740 | remote_send (buf) |
| 741 | char *buf; |
| 742 | { |
| 743 | |
| 744 | putpkt (buf); |
| 745 | getpkt (buf); |
| 746 | |
| 747 | if (buf[0] == 'E') |
| 748 | error ("Remote failure reply: %s", buf); |
| 749 | } |
| 750 | |
| 751 | /* Send a packet to the remote machine, with error checking. |
| 752 | The data of the packet is in BUF. */ |
| 753 | |
| 754 | static void |
| 755 | putpkt (buf) |
| 756 | char *buf; |
| 757 | { |
| 758 | int i; |
| 759 | unsigned char csum = 0; |
| 760 | char buf2[PBUFSIZ]; |
| 761 | int cnt = strlen (buf); |
| 762 | char ch; |
| 763 | char *p; |
| 764 | |
| 765 | /* Copy the packet into buffer BUF2, encapsulating it |
| 766 | and giving it a checksum. */ |
| 767 | |
| 768 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
| 769 | abort(); |
| 770 | |
| 771 | p = buf2; |
| 772 | *p++ = '$'; |
| 773 | |
| 774 | for (i = 0; i < cnt; i++) |
| 775 | { |
| 776 | csum += buf[i]; |
| 777 | *p++ = buf[i]; |
| 778 | } |
| 779 | *p++ = '#'; |
| 780 | *p++ = tohex ((csum >> 4) & 0xf); |
| 781 | *p++ = tohex (csum & 0xf); |
| 782 | |
| 783 | /* Send it over and over until we get a positive ack. */ |
| 784 | |
| 785 | do { |
| 786 | if (kiodebug) |
| 787 | { |
| 788 | *p = '\0'; |
| 789 | printf ("Sending packet: %s...", buf2); fflush(stdout); |
| 790 | } |
| 791 | write (remote_desc, buf2, p - buf2); |
| 792 | |
| 793 | /* read until either a timeout occurs (\0) or '+' is read */ |
| 794 | do { |
| 795 | ch = readchar (); |
| 796 | if (kiodebug) { |
| 797 | if (ch == '+') |
| 798 | printf("Ack\n"); |
| 799 | else |
| 800 | printf ("%02X%c ", ch&0xFF, ch); |
| 801 | } |
| 802 | } while ((ch != '+') && (ch != '\0')); |
| 803 | } while (ch != '+'); |
| 804 | } |
| 805 | |
| 806 | /* Read a packet from the remote machine, with error checking, |
| 807 | and store it in BUF. BUF is expected to be of size PBUFSIZ. */ |
| 808 | |
| 809 | static void |
| 810 | getpkt (buf) |
| 811 | char *buf; |
| 812 | { |
| 813 | char *bp; |
| 814 | unsigned char csum; |
| 815 | int c; |
| 816 | unsigned char c1, c2; |
| 817 | int retries = 0; |
| 818 | #define MAX_RETRIES 10 |
| 819 | |
| 820 | #if 0 |
| 821 | /* Sorry, this will cause all hell to break loose, i.e. we'll end |
| 822 | up in the command loop with an inferior, but (at least if this |
| 823 | happens in remote_wait or some such place) without a current_frame, |
| 824 | having set up prev_* in wait_for_inferior, etc. |
| 825 | |
| 826 | If it is necessary to have such an "emergency exit", seems like |
| 827 | the only plausible thing to do is to say the inferior died, and |
| 828 | make the user reattach if they want to. Perhaps with a prompt |
| 829 | asking for confirmation. */ |
| 830 | |
| 831 | /* allow immediate quit while reading from device, it could be hung */ |
| 832 | immediate_quit++; |
| 833 | #endif /* 0 */ |
| 834 | |
| 835 | while (1) |
| 836 | { |
| 837 | /* Force csum to be zero here because of possible error retry. */ |
| 838 | csum = 0; |
| 839 | |
| 840 | while ((c = readchar()) != '$'); |
| 841 | |
| 842 | bp = buf; |
| 843 | while (1) |
| 844 | { |
| 845 | c = readchar (); |
| 846 | if (c == '#') |
| 847 | break; |
| 848 | if (bp >= buf+PBUFSIZ-1) |
| 849 | { |
| 850 | *bp = '\0'; |
| 851 | printf_filtered ("Remote packet too long: %s\n", buf); |
| 852 | goto whole; |
| 853 | } |
| 854 | *bp++ = c; |
| 855 | csum += c; |
| 856 | } |
| 857 | *bp = 0; |
| 858 | |
| 859 | c1 = fromhex (readchar ()); |
| 860 | c2 = fromhex (readchar ()); |
| 861 | if ((csum & 0xff) == (c1 << 4) + c2) |
| 862 | break; |
| 863 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
| 864 | (c1 << 4) + c2, csum & 0xff, buf); |
| 865 | |
| 866 | /* Try the whole thing again. */ |
| 867 | whole: |
| 868 | if (++retries < MAX_RETRIES) |
| 869 | { |
| 870 | write (remote_desc, "-", 1); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | printf ("Ignoring packet error, continuing...\n"); |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | #if 0 |
| 880 | immediate_quit--; |
| 881 | #endif |
| 882 | |
| 883 | write (remote_desc, "+", 1); |
| 884 | |
| 885 | if (kiodebug) |
| 886 | fprintf (stderr,"Packet received: %s\n", buf); |
| 887 | } |
| 888 | \f |
| 889 | /* The data cache leads to incorrect results because it doesn't know about |
| 890 | volatile variables, thus making it impossible to debug functions which |
| 891 | use hardware registers. Therefore it is #if 0'd out. Effect on |
| 892 | performance is some, for backtraces of functions with a few |
| 893 | arguments each. For functions with many arguments, the stack |
| 894 | frames don't fit in the cache blocks, which makes the cache less |
| 895 | helpful. Disabling the cache is a big performance win for fetching |
| 896 | large structures, because the cache code fetched data in 16-byte |
| 897 | chunks. */ |
| 898 | #if 0 |
| 899 | /* The data cache records all the data read from the remote machine |
| 900 | since the last time it stopped. |
| 901 | |
| 902 | Each cache block holds 16 bytes of data |
| 903 | starting at a multiple-of-16 address. */ |
| 904 | |
| 905 | #define DCACHE_SIZE 64 /* Number of cache blocks */ |
| 906 | |
| 907 | struct dcache_block { |
| 908 | struct dcache_block *next, *last; |
| 909 | unsigned int addr; /* Address for which data is recorded. */ |
| 910 | int data[4]; |
| 911 | }; |
| 912 | |
| 913 | struct dcache_block dcache_free, dcache_valid; |
| 914 | |
| 915 | /* Free all the data cache blocks, thus discarding all cached data. */ |
| 916 | |
| 917 | static void |
| 918 | dcache_flush () |
| 919 | { |
| 920 | register struct dcache_block *db; |
| 921 | |
| 922 | while ((db = dcache_valid.next) != &dcache_valid) |
| 923 | { |
| 924 | remque (db); |
| 925 | insque (db, &dcache_free); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * If addr is present in the dcache, return the address of the block |
| 931 | * containing it. |
| 932 | */ |
| 933 | |
| 934 | struct dcache_block * |
| 935 | dcache_hit (addr) |
| 936 | { |
| 937 | register struct dcache_block *db; |
| 938 | |
| 939 | if (addr & 3) |
| 940 | abort (); |
| 941 | |
| 942 | /* Search all cache blocks for one that is at this address. */ |
| 943 | db = dcache_valid.next; |
| 944 | while (db != &dcache_valid) |
| 945 | { |
| 946 | if ((addr & 0xfffffff0) == db->addr) |
| 947 | return db; |
| 948 | db = db->next; |
| 949 | } |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | /* Return the int data at address ADDR in dcache block DC. */ |
| 954 | |
| 955 | int |
| 956 | dcache_value (db, addr) |
| 957 | struct dcache_block *db; |
| 958 | unsigned int addr; |
| 959 | { |
| 960 | if (addr & 3) |
| 961 | abort (); |
| 962 | return (db->data[(addr>>2)&3]); |
| 963 | } |
| 964 | |
| 965 | /* Get a free cache block, put it on the valid list, |
| 966 | and return its address. The caller should store into the block |
| 967 | the address and data that it describes. */ |
| 968 | |
| 969 | struct dcache_block * |
| 970 | dcache_alloc () |
| 971 | { |
| 972 | register struct dcache_block *db; |
| 973 | |
| 974 | if ((db = dcache_free.next) == &dcache_free) |
| 975 | /* If we can't get one from the free list, take last valid */ |
| 976 | db = dcache_valid.last; |
| 977 | |
| 978 | remque (db); |
| 979 | insque (db, &dcache_valid); |
| 980 | return (db); |
| 981 | } |
| 982 | |
| 983 | /* Return the contents of the word at address ADDR in the remote machine, |
| 984 | using the data cache. */ |
| 985 | |
| 986 | int |
| 987 | dcache_fetch (addr) |
| 988 | CORE_ADDR addr; |
| 989 | { |
| 990 | register struct dcache_block *db; |
| 991 | |
| 992 | db = dcache_hit (addr); |
| 993 | if (db == 0) |
| 994 | { |
| 995 | db = dcache_alloc (); |
| 996 | remote_read_bytes (addr & ~0xf, db->data, 16); |
| 997 | db->addr = addr & ~0xf; |
| 998 | } |
| 999 | return (dcache_value (db, addr)); |
| 1000 | } |
| 1001 | |
| 1002 | /* Write the word at ADDR both in the data cache and in the remote machine. */ |
| 1003 | |
| 1004 | dcache_poke (addr, data) |
| 1005 | CORE_ADDR addr; |
| 1006 | int data; |
| 1007 | { |
| 1008 | register struct dcache_block *db; |
| 1009 | |
| 1010 | /* First make sure the word is IN the cache. DB is its cache block. */ |
| 1011 | db = dcache_hit (addr); |
| 1012 | if (db == 0) |
| 1013 | { |
| 1014 | db = dcache_alloc (); |
| 1015 | remote_read_bytes (addr & ~0xf, db->data, 16); |
| 1016 | db->addr = addr & ~0xf; |
| 1017 | } |
| 1018 | |
| 1019 | /* Modify the word in the cache. */ |
| 1020 | db->data[(addr>>2)&3] = data; |
| 1021 | |
| 1022 | /* Send the changed word. */ |
| 1023 | remote_write_bytes (addr, &data, 4); |
| 1024 | } |
| 1025 | |
| 1026 | /* Initialize the data cache. */ |
| 1027 | |
| 1028 | dcache_init () |
| 1029 | { |
| 1030 | register i; |
| 1031 | register struct dcache_block *db; |
| 1032 | |
| 1033 | db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * |
| 1034 | DCACHE_SIZE); |
| 1035 | dcache_free.next = dcache_free.last = &dcache_free; |
| 1036 | dcache_valid.next = dcache_valid.last = &dcache_valid; |
| 1037 | for (i=0;i<DCACHE_SIZE;i++,db++) |
| 1038 | insque (db, &dcache_free); |
| 1039 | } |
| 1040 | #endif /* 0 */ |
| 1041 | |
| 1042 | /* Define the target subroutine names */ |
| 1043 | |
| 1044 | struct target_ops remote_ops = { |
| 1045 | "remote", /* to_shortname */ |
| 1046 | "Remote serial target in gdb-specific protocol", /* to_longname */ |
| 1047 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
| 1048 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ |
| 1049 | remote_open, /* to_open */ |
| 1050 | remote_close, /* to_close */ |
| 1051 | NULL, /* to_attach */ |
| 1052 | remote_detach, /* to_detach */ |
| 1053 | remote_resume, /* to_resume */ |
| 1054 | remote_wait, /* to_wait */ |
| 1055 | remote_fetch_registers, /* to_fetch_registers */ |
| 1056 | remote_store_registers, /* to_store_registers */ |
| 1057 | remote_prepare_to_store, /* to_prepare_to_store */ |
| 1058 | remote_xfer_memory, /* to_xfer_memory */ |
| 1059 | remote_files_info, /* to_files_info */ |
| 1060 | NULL, /* to_insert_breakpoint */ |
| 1061 | NULL, /* to_remove_breakpoint */ |
| 1062 | NULL, /* to_terminal_init */ |
| 1063 | NULL, /* to_terminal_inferior */ |
| 1064 | NULL, /* to_terminal_ours_for_output */ |
| 1065 | NULL, /* to_terminal_ours */ |
| 1066 | NULL, /* to_terminal_info */ |
| 1067 | NULL, /* to_kill */ |
| 1068 | NULL, /* to_load */ |
| 1069 | NULL, /* to_lookup_symbol */ |
| 1070 | NULL, /* to_create_inferior */ |
| 1071 | NULL, /* to_mourn_inferior */ |
| 1072 | 0, /* to_can_run */ |
| 1073 | process_stratum, /* to_stratum */ |
| 1074 | NULL, /* to_next */ |
| 1075 | 1, /* to_has_all_memory */ |
| 1076 | 1, /* to_has_memory */ |
| 1077 | 1, /* to_has_stack */ |
| 1078 | 1, /* to_has_registers */ |
| 1079 | 1, /* to_has_execution */ |
| 1080 | NULL, /* sections */ |
| 1081 | NULL, /* sections_end */ |
| 1082 | OPS_MAGIC /* to_magic */ |
| 1083 | }; |
| 1084 | |
| 1085 | void |
| 1086 | _initialize_remote () |
| 1087 | { |
| 1088 | add_target (&remote_ops); |
| 1089 | |
| 1090 | add_show_from_set ( |
| 1091 | add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug, |
| 1092 | "Set debugging of remote serial I/O.\n\ |
| 1093 | When enabled, each packet sent or received with the remote target\n\ |
| 1094 | is displayed.", &setlist), |
| 1095 | &showlist); |
| 1096 | } |
| 1097 | |
| 1098 | #endif |