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