1 /* Remote target communications for serial-line targets in custom GDB protocol 2 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20 /* Remote communication protocol. 21 22 A debug packet whose contents are <data> 23 is encapsulated for transmission in the form: 24 25 $ <data> # CSUM1 CSUM2 26 27 <data> must be ASCII alphanumeric and cannot include characters 28 '$' or '#'. If <data> starts with two characters followed by 29 ':', then the existing stubs interpret this as a sequence number. 30 31 CSUM1 and CSUM2 are ascii hex representation of an 8-bit 32 checksum of <data>, the most significant nibble is sent first. 33 the hex digits 0-9,a-f are used. 34 35 Receiver responds with: 36 37 + - if CSUM is correct and ready for next packet 38 - - if CSUM is incorrect 39 40 <data> is as follows: 41 Most values are encoded in ascii hex digits. Signal numbers are according 42 to the numbering in target.h. 43 44 Request Packet 45 46 set thread Hct... Set thread for subsequent operations. 47 c = 'c' for thread used in step and 48 continue; t... can be -1 for all 49 threads. 50 c = 'g' for thread used in other 51 operations. If zero, pick a thread, 52 any thread. 53 reply OK for success 54 ENN for an error. 55 56 read registers g 57 reply XX....X Each byte of register data 58 is described by two hex digits. 59 Registers are in the internal order 60 for GDB, and the bytes in a register 61 are in the same order the machine uses. 62 or ENN for an error. 63 64 write regs GXX..XX Each byte of register data 65 is described by two hex digits. 66 reply OK for success 67 ENN for an error 68 69 write reg Pn...=r... Write register n... with value r..., 70 which contains two hex digits for each 71 byte in the register (target byte 72 order). 73 reply OK for success 74 ENN for an error 75 (not supported by all stubs). 76 77 read mem mAA..AA,LLLL AA..AA is address, LLLL is length. 78 reply XX..XX XX..XX is mem contents 79 Can be fewer bytes than requested 80 if able to read only part of the data. 81 or ENN NN is errno 82 83 write mem MAA..AA,LLLL:XX..XX 84 AA..AA is address, 85 LLLL is number of bytes, 86 XX..XX is data 87 reply OK for success 88 ENN for an error (this includes the case 89 where only part of the data was 90 written). 91 92 continue cAA..AA AA..AA is address to resume 93 If AA..AA is omitted, 94 resume at same address. 95 96 step sAA..AA AA..AA is address to resume 97 If AA..AA is omitted, 98 resume at same address. 99 100 continue with Csig;AA Continue with signal sig (hex signal 101 signal number). 102 103 step with Ssig;AA Like 'C' but step not continue. 104 signal 105 106 last signal ? Reply the current reason for stopping. 107 This is the same reply as is generated 108 for step or cont : SAA where AA is the 109 signal number. 110 111 detach D Reply OK. 112 113 There is no immediate reply to step or cont. 114 The reply comes when the machine stops. 115 It is SAA AA is the signal number. 116 117 or... TAAn...:r...;n...:r...;n...:r...; 118 AA = signal number 119 n... = register number (hex) 120 r... = register contents 121 n... = `thread' 122 r... = thread process ID. This is 123 a hex integer. 124 n... = other string not starting 125 with valid hex digit. 126 gdb should ignore this n,r pair 127 and go on to the next. This way 128 we can extend the protocol. 129 or... WAA The process exited, and AA is 130 the exit status. This is only 131 applicable for certains sorts of 132 targets. 133 or... XAA The process terminated with signal 134 AA. 135 or... OXX..XX XX..XX is hex encoding of ASCII data. This 136 can happen at any time while the program is 137 running and the debugger should 138 continue to wait for 'W', 'T', etc. 139 140 thread alive TXX Find out if the thread XX is alive. 141 reply OK thread is still alive 142 ENN thread is dead 143 144 remote restart RXX Restart the remote server 145 146 extended ops ! Use the extended remote protocol. 147 Sticky -- only needs to be set once. 148 149 kill request k 150 151 toggle debug d toggle debug flag (see 386 & 68k stubs) 152 reset r reset -- see sparc stub. 153 reserved <other> On other requests, the stub should 154 ignore the request and send an empty 155 response ($#<checksum>). This way 156 we can extend the protocol and GDB 157 can tell whether the stub it is 158 talking to uses the old or the new. 159 search tAA:PP,MM Search backwards starting at address 160 AA for a match with pattern PP and 161 mask MM. PP and MM are 4 bytes. 162 Not supported by all stubs. 163 164 general query qXXXX Request info about XXXX. 165 general set QXXXX=yyyy Set value of XXXX to yyyy. 166 query sect offs qOffsets Get section offsets. Reply is 167 Text=xxx;Data=yyy;Bss=zzz 168 169 Responses can be run-length encoded to save space. A '*' means that 170 the next character is an ASCII encoding giving a repeat count which 171 stands for that many repititions of the character preceding the '*'. 172 The encoding is n+29, yielding a printable character where n >=3 173 (which is where rle starts to win). Don't use an n > 126. 174 175 So 176 "0* " means the same as "0000". */ 177 178 #include "defs.h" 179 #include "gdb_string.h" 180 #include <fcntl.h> 181 #include "frame.h" 182 #include "inferior.h" 183 #include "bfd.h" 184 #include "symfile.h" 185 #include "target.h" 186 #include "wait.h" 187 /*#include "terminal.h"*/ 188 #include "gdbcmd.h" 189 #include "objfiles.h" 190 #include "gdb-stabs.h" 191 #include "gdbthread.h" 192 193 #include "dcache.h" 194 195 #ifdef USG 196 #include <sys/types.h> 197 #endif 198 199 #include <signal.h> 200 #include "serial.h" 201 202 /* Prototypes for local functions */ 203 204 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, 205 char *myaddr, int len)); 206 207 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, 208 char *myaddr, int len)); 209 210 static void remote_files_info PARAMS ((struct target_ops *ignore)); 211 212 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, 213 int len, int should_write, 214 struct target_ops *target)); 215 216 static void remote_prepare_to_store PARAMS ((void)); 217 218 static void remote_fetch_registers PARAMS ((int regno)); 219 220 static void remote_resume PARAMS ((int pid, int step, 221 enum target_signal siggnal)); 222 223 static int remote_start_remote PARAMS ((char *dummy)); 224 225 static void remote_open PARAMS ((char *name, int from_tty)); 226 227 static void extended_remote_open PARAMS ((char *name, int from_tty)); 228 229 static void remote_open_1 PARAMS ((char *, int, struct target_ops *)); 230 231 static void remote_close PARAMS ((int quitting)); 232 233 static void remote_store_registers PARAMS ((int regno)); 234 235 static void remote_mourn PARAMS ((void)); 236 237 static void extended_remote_restart PARAMS ((void)); 238 239 static void extended_remote_mourn PARAMS ((void)); 240 241 static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); 242 243 static void remote_mourn_1 PARAMS ((struct target_ops *)); 244 245 static void getpkt PARAMS ((char *buf, int forever)); 246 247 static int putpkt PARAMS ((char *buf)); 248 249 static void remote_send PARAMS ((char *buf)); 250 251 static int readchar PARAMS ((int timeout)); 252 253 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); 254 255 static void remote_kill PARAMS ((void)); 256 257 static int tohex PARAMS ((int nib)); 258 259 static int fromhex PARAMS ((int a)); 260 261 static void remote_detach PARAMS ((char *args, int from_tty)); 262 263 static void remote_interrupt PARAMS ((int signo)); 264 265 static void remote_interrupt_twice PARAMS ((int signo)); 266 267 static void interrupt_query PARAMS ((void)); 268 269 static void set_thread PARAMS ((int, int)); 270 271 static int remote_thread_alive PARAMS ((int)); 272 273 static void get_offsets PARAMS ((void)); 274 275 static int read_frame PARAMS ((char *)); 276 277 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); 278 279 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); 280 281 extern struct target_ops remote_ops; /* Forward decl */ 282 extern struct target_ops extended_remote_ops; /* Forward decl */ 283 284 /* This was 5 seconds, which is a long time to sit and wait. 285 Unless this is going though some terminal server or multiplexer or 286 other form of hairy serial connection, I would think 2 seconds would 287 be plenty. */ 288 289 /* Changed to allow option to set timeout value. 290 was static int remote_timeout = 2; */ 291 extern int remote_timeout; 292 293 /* This variable chooses whether to send a ^C or a break when the user 294 requests program interruption. Although ^C is usually what remote 295 systems expect, and that is the default here, sometimes a break is 296 preferable instead. */ 297 298 static int remote_break; 299 300 /* Descriptor for I/O to remote machine. Initialize it to NULL so that 301 remote_open knows that we don't have a file open when the program 302 starts. */ 303 serial_t remote_desc = NULL; 304 305 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c 306 and i386-stub.c. Normally, no one would notice because it only matters 307 for writing large chunks of memory (e.g. in downloads). Also, this needs 308 to be more than 400 if required to hold the registers (see below, where 309 we round it up based on REGISTER_BYTES). */ 310 #define PBUFSIZ 400 311 312 /* Maximum number of bytes to read/write at once. The value here 313 is chosen to fill up a packet (the headers account for the 32). */ 314 #define MAXBUFBYTES ((PBUFSIZ-32)/2) 315 316 /* Round up PBUFSIZ to hold all the registers, at least. */ 317 /* The blank line after the #if seems to be required to work around a 318 bug in HP's PA compiler. */ 319 #if REGISTER_BYTES > MAXBUFBYTES 320 321 #undef PBUFSIZ 322 #define PBUFSIZ (REGISTER_BYTES * 2 + 32) 323 #endif 324 325 /* This variable sets the number of bytes to be written to the target 326 in a single packet. Normally PBUFSIZ is satisfactory, but some 327 targets need smaller values (perhaps because the receiving end 328 is slow). */ 329 330 int remote_write_size = PBUFSIZ; 331 332 /* Should we try the 'P' request? If this is set to one when the stub 333 doesn't support 'P', the only consequence is some unnecessary traffic. */ 334 static int stub_supports_P = 1; 335 336 337 /* These are the threads which we last sent to the remote system. -1 for all 338 or -2 for not sent yet. */ 339 int general_thread; 340 int cont_thread; 341 342 static void 343 set_thread (th, gen) 344 int th; 345 int gen; 346 { 347 char buf[PBUFSIZ]; 348 int state = gen ? general_thread : cont_thread; 349 if (state == th) 350 return; 351 buf[0] = 'H'; 352 buf[1] = gen ? 'g' : 'c'; 353 if (th == 42000) 354 { 355 buf[2] = '0'; 356 buf[3] = '\0'; 357 } 358 else if (th < 0) 359 sprintf (&buf[2], "-%x", -th); 360 else 361 sprintf (&buf[2], "%x", th); 362 putpkt (buf); 363 getpkt (buf, 0); 364 if (gen) 365 general_thread = th; 366 else 367 cont_thread = th; 368 } 369 370 /* Return nonzero if the thread TH is still alive on the remote system. */ 371 372 static int 373 remote_thread_alive (th) 374 int th; 375 { 376 char buf[PBUFSIZ]; 377 378 buf[0] = 'T'; 379 if (th < 0) 380 sprintf (&buf[1], "-%x", -th); 381 else 382 sprintf (&buf[1], "%x", th); 383 putpkt (buf); 384 getpkt (buf, 0); 385 return (buf[0] == 'O' && buf[1] == 'K'); 386 } 387 388 /* Restart the remote side; this is an extended protocol operation. */ 389 390 static void 391 extended_remote_restart () 392 { 393 char buf[PBUFSIZ]; 394 395 /* Send the restart command; for reasons I don't understand the 396 remote side really expects a number after the "R". */ 397 buf[0] = 'R'; 398 sprintf (&buf[1], "%x", 0); 399 putpkt (buf); 400 401 /* Now query for status so this looks just like we restarted 402 gdbserver from scratch. */ 403 putpkt ("?"); 404 getpkt (buf, 0); 405 } 406 407 /* Clean up connection to a remote debugger. */ 408 409 /* ARGSUSED */ 410 static void 411 remote_close (quitting) 412 int quitting; 413 { 414 if (remote_desc) 415 SERIAL_CLOSE (remote_desc); 416 remote_desc = NULL; 417 } 418 419 /* Query the remote side for the text, data and bss offsets. */ 420 421 static void 422 get_offsets () 423 { 424 char buf[PBUFSIZ]; 425 int nvals; 426 CORE_ADDR text_addr, data_addr, bss_addr; 427 struct section_offsets *offs; 428 429 putpkt ("qOffsets"); 430 431 getpkt (buf, 0); 432 433 if (buf[0] == '\000') 434 return; /* Return silently. Stub doesn't support this 435 command. */ 436 if (buf[0] == 'E') 437 { 438 warning ("Remote failure reply: %s", buf); 439 return; 440 } 441 442 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr, 443 &bss_addr); 444 if (nvals != 3) 445 error ("Malformed response to offset query, %s", buf); 446 447 if (symfile_objfile == NULL) 448 return; 449 450 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) 451 + symfile_objfile->num_sections 452 * sizeof (offs->offsets)); 453 memcpy (offs, symfile_objfile->section_offsets, 454 sizeof (struct section_offsets) 455 + symfile_objfile->num_sections 456 * sizeof (offs->offsets)); 457 458 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; 459 460 /* This is a temporary kludge to force data and bss to use the same offsets 461 because that's what nlmconv does now. The real solution requires changes 462 to the stub and remote.c that I don't have time to do right now. */ 463 464 ANOFFSET (offs, SECT_OFF_DATA) = data_addr; 465 ANOFFSET (offs, SECT_OFF_BSS) = data_addr; 466 467 objfile_relocate (symfile_objfile, offs); 468 } 469 470 /* Stub for catch_errors. */ 471 472 static int 473 remote_start_remote (dummy) 474 char *dummy; 475 { 476 immediate_quit = 1; /* Allow user to interrupt it */ 477 478 /* Ack any packet which the remote side has already sent. */ 479 SERIAL_WRITE (remote_desc, "+", 1); 480 481 /* Let the stub know that we want it to return the thread. */ 482 set_thread (-1, 0); 483 484 get_offsets (); /* Get text, data & bss offsets */ 485 486 putpkt ("?"); /* initiate a query from remote machine */ 487 immediate_quit = 0; 488 489 start_remote (); /* Initialize gdb process mechanisms */ 490 return 1; 491 } 492 493 /* Open a connection to a remote debugger. 494 NAME is the filename used for communication. */ 495 496 static void 497 remote_open (name, from_tty) 498 char *name; 499 int from_tty; 500 { 501 remote_open_1 (name, from_tty, &remote_ops); 502 } 503 504 /* Open a connection to a remote debugger using the extended 505 remote gdb protocol. NAME is the filename used for communication. */ 506 507 static void 508 extended_remote_open (name, from_tty) 509 char *name; 510 int from_tty; 511 { 512 char buf[PBUFSIZ]; 513 514 /* Do the basic remote open stuff. */ 515 remote_open_1 (name, from_tty, &extended_remote_ops); 516 517 /* Now tell the remote that we're using the extended protocol. */ 518 putpkt ("!"); 519 getpkt (buf, 0); 520 521 } 522 523 /* Generic code for opening a connection to a remote target. */ 524 static DCACHE *remote_dcache; 525 526 static void 527 remote_open_1 (name, from_tty, target) 528 char *name; 529 int from_tty; 530 struct target_ops *target; 531 { 532 if (name == 0) 533 error ("To open a remote debug connection, you need to specify what serial\n\ 534 device is attached to the remote system (e.g. /dev/ttya)."); 535 536 target_preopen (from_tty); 537 538 unpush_target (target); 539 540 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); 541 542 remote_desc = SERIAL_OPEN (name); 543 if (!remote_desc) 544 perror_with_name (name); 545 546 if (baud_rate != -1) 547 { 548 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) 549 { 550 SERIAL_CLOSE (remote_desc); 551 perror_with_name (name); 552 } 553 } 554 555 556 SERIAL_RAW (remote_desc); 557 558 /* If there is something sitting in the buffer we might take it as a 559 response to a command, which would be bad. */ 560 SERIAL_FLUSH_INPUT (remote_desc); 561 562 if (from_tty) 563 { 564 puts_filtered ("Remote debugging using "); 565 puts_filtered (name); 566 puts_filtered ("\n"); 567 } 568 push_target (target); /* Switch to using remote target now */ 569 570 /* Start out by trying the 'P' request to set registers. We set this each 571 time that we open a new target so that if the user switches from one 572 stub to another, we can (if the target is closed and reopened) cope. */ 573 stub_supports_P = 1; 574 575 general_thread = -2; 576 cont_thread = -2; 577 578 /* Without this, some commands which require an active target (such as kill) 579 won't work. This variable serves (at least) double duty as both the pid 580 of the target process (if it has such), and as a flag indicating that a 581 target is active. These functions should be split out into seperate 582 variables, especially since GDB will someday have a notion of debugging 583 several processes. */ 584 585 inferior_pid = 42000; 586 /* Start the remote connection; if error (0), discard this target. 587 In particular, if the user quits, be sure to discard it 588 (we'd be in an inconsistent state otherwise). */ 589 if (!catch_errors (remote_start_remote, (char *)0, 590 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) 591 pop_target(); 592 } 593 594 /* This takes a program previously attached to and detaches it. After 595 this is done, GDB can be used to debug some other program. We 596 better not have left any breakpoints in the target program or it'll 597 die when it hits one. */ 598 599 static void 600 remote_detach (args, from_tty) 601 char *args; 602 int from_tty; 603 { 604 char buf[PBUFSIZ]; 605 606 if (args) 607 error ("Argument given to \"detach\" when remotely debugging."); 608 609 /* Tell the remote target to detach. */ 610 strcpy (buf, "D"); 611 remote_send (buf); 612 613 pop_target (); 614 if (from_tty) 615 puts_filtered ("Ending remote debugging.\n"); 616 } 617 618 /* Convert hex digit A to a number. */ 619 620 static int 621 fromhex (a) 622 int a; 623 { 624 if (a >= '0' && a <= '9') 625 return a - '0'; 626 else if (a >= 'a' && a <= 'f') 627 return a - 'a' + 10; 628 else 629 error ("Reply contains invalid hex digit %d", a); 630 } 631 632 /* Convert number NIB to a hex digit. */ 633 634 static int 635 tohex (nib) 636 int nib; 637 { 638 if (nib < 10) 639 return '0'+nib; 640 else 641 return 'a'+nib-10; 642 } 643 644 /* Tell the remote machine to resume. */ 645 646 static enum target_signal last_sent_signal = TARGET_SIGNAL_0; 647 int last_sent_step; 648 649 static void 650 remote_resume (pid, step, siggnal) 651 int pid, step; 652 enum target_signal siggnal; 653 { 654 char buf[PBUFSIZ]; 655 656 if (pid == -1) 657 set_thread (inferior_pid, 0); 658 else 659 set_thread (pid, 0); 660 661 dcache_flush (remote_dcache); 662 663 last_sent_signal = siggnal; 664 last_sent_step = step; 665 666 if (siggnal != TARGET_SIGNAL_0) 667 { 668 buf[0] = step ? 'S' : 'C'; 669 buf[1] = tohex (((int)siggnal >> 4) & 0xf); 670 buf[2] = tohex ((int)siggnal & 0xf); 671 buf[3] = '\0'; 672 } 673 else 674 strcpy (buf, step ? "s": "c"); 675 676 putpkt (buf); 677 } 678 679 /* Send ^C to target to halt it. Target will respond, and send us a 680 packet. */ 681 682 static void 683 remote_interrupt (signo) 684 int signo; 685 { 686 /* If this doesn't work, try more severe steps. */ 687 signal (signo, remote_interrupt_twice); 688 689 if (remote_debug) 690 printf_unfiltered ("remote_interrupt called\n"); 691 692 /* Send a break or a ^C, depending on user preference. */ 693 if (remote_break) 694 SERIAL_SEND_BREAK (remote_desc); 695 else 696 SERIAL_WRITE (remote_desc, "\003", 1); 697 } 698 699 static void (*ofunc)(); 700 701 /* The user typed ^C twice. */ 702 static void 703 remote_interrupt_twice (signo) 704 int signo; 705 { 706 signal (signo, ofunc); 707 708 interrupt_query (); 709 710 signal (signo, remote_interrupt); 711 } 712 713 /* Ask the user what to do when an interrupt is received. */ 714 715 static void 716 interrupt_query () 717 { 718 target_terminal_ours (); 719 720 if (query ("Interrupted while waiting for the program.\n\ 721 Give up (and stop debugging it)? ")) 722 { 723 target_mourn_inferior (); 724 return_to_top_level (RETURN_QUIT); 725 } 726 727 target_terminal_inferior (); 728 } 729 730 /* If nonzero, ignore the next kill. */ 731 int kill_kludge; 732 733 /* Wait until the remote machine stops, then return, 734 storing status in STATUS just as `wait' would. 735 Returns "pid" (though it's not clear what, if anything, that 736 means in the case of this target). */ 737 738 static int 739 remote_wait (pid, status) 740 int pid; 741 struct target_waitstatus *status; 742 { 743 unsigned char buf[PBUFSIZ]; 744 int thread_num = -1; 745 746 status->kind = TARGET_WAITKIND_EXITED; 747 status->value.integer = 0; 748 749 while (1) 750 { 751 unsigned char *p; 752 753 ofunc = (void (*)()) signal (SIGINT, remote_interrupt); 754 getpkt ((char *) buf, 1); 755 signal (SIGINT, ofunc); 756 757 switch (buf[0]) 758 { 759 case 'E': /* Error of some sort */ 760 warning ("Remote failure reply: %s", buf); 761 continue; 762 case 'T': /* Status with PC, SP, FP, ... */ 763 { 764 int i; 765 long regno; 766 char regs[MAX_REGISTER_RAW_SIZE]; 767 768 /* Expedited reply, containing Signal, {regno, reg} repeat */ 769 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where 770 ss = signal number 771 n... = register number 772 r... = register contents 773 */ 774 775 p = &buf[3]; /* after Txx */ 776 777 while (*p) 778 { 779 unsigned char *p1; 780 char *p_temp; 781 782 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */ 783 p1 = (unsigned char *)p_temp; 784 785 if (p1 == p) 786 { 787 p1 = (unsigned char *) strchr ((const char *) p, ':'); 788 if (p1 == NULL) 789 warning ("Malformed packet (missing colon): %s\n\ 790 Packet: '%s'\n", 791 p, buf); 792 if (strncmp ((const char *) p, "thread", p1 - p) == 0) 793 { 794 thread_num = strtol ((const char *) ++p1, &p_temp, 16); 795 p = (unsigned char *)p_temp; 796 } 797 } 798 else 799 { 800 p = p1; 801 802 if (*p++ != ':') 803 warning ("Malformed packet (missing colon): %s\n\ 804 Packet: '%s'\n", 805 p, buf); 806 807 if (regno >= NUM_REGS) 808 warning ("Remote sent bad register number %ld: %s\n\ 809 Packet: '%s'\n", 810 regno, p, buf); 811 812 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) 813 { 814 if (p[0] == 0 || p[1] == 0) 815 warning ("Remote reply is too short: %s", buf); 816 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); 817 p += 2; 818 } 819 supply_register (regno, regs); 820 } 821 822 if (*p++ != ';') 823 warning ("Remote register badly formatted: %s", buf); 824 } 825 } 826 /* fall through */ 827 case 'S': /* Old style status, just signal only */ 828 status->kind = TARGET_WAITKIND_STOPPED; 829 status->value.sig = (enum target_signal) 830 (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); 831 832 goto got_status; 833 case 'W': /* Target exited */ 834 { 835 /* The remote process exited. */ 836 status->kind = TARGET_WAITKIND_EXITED; 837 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); 838 goto got_status; 839 } 840 case 'X': 841 status->kind = TARGET_WAITKIND_SIGNALLED; 842 status->value.sig = (enum target_signal) 843 (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); 844 kill_kludge = 1; 845 846 goto got_status; 847 case 'O': /* Console output */ 848 for (p = buf + 1; *p; p +=2) 849 { 850 char tb[2]; 851 char c = fromhex (p[0]) * 16 + fromhex (p[1]); 852 tb[0] = c; 853 tb[1] = 0; 854 if (target_output_hook) 855 target_output_hook (tb); 856 else 857 fputs_filtered (tb, gdb_stdout); 858 } 859 continue; 860 case '\0': 861 if (last_sent_signal != TARGET_SIGNAL_0) 862 { 863 /* Zero length reply means that we tried 'S' or 'C' and 864 the remote system doesn't support it. */ 865 target_terminal_ours_for_output (); 866 printf_filtered 867 ("Can't send signals to this remote system. %s not sent.\n", 868 target_signal_to_name (last_sent_signal)); 869 last_sent_signal = TARGET_SIGNAL_0; 870 target_terminal_inferior (); 871 872 strcpy ((char *) buf, last_sent_step ? "s" : "c"); 873 putpkt ((char *) buf); 874 continue; 875 } 876 /* else fallthrough */ 877 default: 878 warning ("Invalid remote reply: %s", buf); 879 continue; 880 } 881 } 882 got_status: 883 if (thread_num != -1) 884 { 885 /* Initial thread value can only be acquired via wait, so deal with 886 this marker which is used before the first thread value is 887 acquired. */ 888 if (inferior_pid == 42000) 889 { 890 inferior_pid = thread_num; 891 add_thread (inferior_pid); 892 } 893 return thread_num; 894 } 895 return inferior_pid; 896 } 897 898 /* Number of bytes of registers this stub implements. */ 899 static int register_bytes_found; 900 901 /* Read the remote registers into the block REGS. */ 902 /* Currently we just read all the registers, so we don't use regno. */ 903 /* ARGSUSED */ 904 static void 905 remote_fetch_registers (regno) 906 int regno; 907 { 908 char buf[PBUFSIZ]; 909 int i; 910 char *p; 911 char regs[REGISTER_BYTES]; 912 913 set_thread (inferior_pid, 1); 914 915 sprintf (buf, "g"); 916 remote_send (buf); 917 918 /* Unimplemented registers read as all bits zero. */ 919 memset (regs, 0, REGISTER_BYTES); 920 921 /* We can get out of synch in various cases. If the first character 922 in the buffer is not a hex character, assume that has happened 923 and try to fetch another packet to read. */ 924 while ((buf[0] < '0' || buf[0] > '9') 925 && (buf[0] < 'a' || buf[0] > 'f')) 926 { 927 if (remote_debug) 928 printf_unfiltered ("Bad register packet; fetching a new packet\n"); 929 getpkt (buf, 0); 930 } 931 932 /* Reply describes registers byte by byte, each byte encoded as two 933 hex characters. Suck them all up, then supply them to the 934 register cacheing/storage mechanism. */ 935 936 p = buf; 937 for (i = 0; i < REGISTER_BYTES; i++) 938 { 939 if (p[0] == 0) 940 break; 941 if (p[1] == 0) 942 { 943 warning ("Remote reply is of odd length: %s", buf); 944 /* Don't change register_bytes_found in this case, and don't 945 print a second warning. */ 946 goto supply_them; 947 } 948 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); 949 p += 2; 950 } 951 952 if (i != register_bytes_found) 953 { 954 register_bytes_found = i; 955 #ifdef REGISTER_BYTES_OK 956 if (!REGISTER_BYTES_OK (i)) 957 warning ("Remote reply is too short: %s", buf); 958 #endif 959 } 960 961 supply_them: 962 for (i = 0; i < NUM_REGS; i++) 963 supply_register (i, ®s[REGISTER_BYTE(i)]); 964 } 965 966 /* Prepare to store registers. Since we may send them all (using a 967 'G' request), we have to read out the ones we don't want to change 968 first. */ 969 970 static void 971 remote_prepare_to_store () 972 { 973 /* Make sure the entire registers array is valid. */ 974 read_register_bytes (0, (char *)NULL, REGISTER_BYTES); 975 } 976 977 /* Store register REGNO, or all registers if REGNO == -1, from the contents 978 of REGISTERS. FIXME: ignores errors. */ 979 980 static void 981 remote_store_registers (regno) 982 int regno; 983 { 984 char buf[PBUFSIZ]; 985 int i; 986 char *p; 987 988 set_thread (inferior_pid, 1); 989 990 if (regno >= 0 && stub_supports_P) 991 { 992 /* Try storing a single register. */ 993 char *regp; 994 995 sprintf (buf, "P%x=", regno); 996 p = buf + strlen (buf); 997 regp = ®isters[REGISTER_BYTE (regno)]; 998 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) 999 { 1000 *p++ = tohex ((regp[i] >> 4) & 0xf); 1001 *p++ = tohex (regp[i] & 0xf); 1002 } 1003 *p = '\0'; 1004 remote_send (buf); 1005 if (buf[0] != '\0') 1006 { 1007 /* The stub understands the 'P' request. We are done. */ 1008 return; 1009 } 1010 1011 /* The stub does not support the 'P' request. Use 'G' instead, 1012 and don't try using 'P' in the future (it will just waste our 1013 time). */ 1014 stub_supports_P = 0; 1015 } 1016 1017 buf[0] = 'G'; 1018 1019 /* Command describes registers byte by byte, 1020 each byte encoded as two hex characters. */ 1021 1022 p = buf + 1; 1023 /* remote_prepare_to_store insures that register_bytes_found gets set. */ 1024 for (i = 0; i < register_bytes_found; i++) 1025 { 1026 *p++ = tohex ((registers[i] >> 4) & 0xf); 1027 *p++ = tohex (registers[i] & 0xf); 1028 } 1029 *p = '\0'; 1030 1031 remote_send (buf); 1032 } 1033 1034 /* 1035 Use of the data cache *used* to be disabled because it loses for looking at 1036 and changing hardware I/O ports and the like. Accepting `volatile' 1037 would perhaps be one way to fix it. Another idea would be to use the 1038 executable file for the text segment (for all SEC_CODE sections? 1039 For all SEC_READONLY sections?). This has problems if you want to 1040 actually see what the memory contains (e.g. self-modifying code, 1041 clobbered memory, user downloaded the wrong thing). 1042 1043 Because it speeds so much up, it's now enabled, if you're playing 1044 with registers you turn it of (set remotecache 0) 1045 */ 1046 1047 /* Read a word from remote address ADDR and return it. 1048 This goes through the data cache. */ 1049 1050 #if 0 /* unused? */ 1051 static int 1052 remote_fetch_word (addr) 1053 CORE_ADDR addr; 1054 { 1055 return dcache_fetch (remote_dcache, addr); 1056 } 1057 1058 /* Write a word WORD into remote address ADDR. 1059 This goes through the data cache. */ 1060 1061 static void 1062 remote_store_word (addr, word) 1063 CORE_ADDR addr; 1064 int word; 1065 { 1066 dcache_poke (remote_dcache, addr, word); 1067 } 1068 #endif /* 0 (unused?) */ 1069 1070 1071 /* Write memory data directly to the remote machine. 1072 This does not inform the data cache; the data cache uses this. 1073 MEMADDR is the address in the remote memory space. 1074 MYADDR is the address of the buffer in our space. 1075 LEN is the number of bytes. 1076 1077 Returns number of bytes transferred, or 0 for error. */ 1078 1079 static int 1080 remote_write_bytes (memaddr, myaddr, len) 1081 CORE_ADDR memaddr; 1082 char *myaddr; 1083 int len; 1084 { 1085 char buf[PBUFSIZ]; 1086 int i; 1087 char *p; 1088 int done; 1089 /* Chop the transfer down if necessary */ 1090 1091 done = 0; 1092 while (done < len) 1093 { 1094 int todo = len - done; 1095 int cando = min(remote_write_size, PBUFSIZ) / 2 - 32; /* num bytes that will fit */ 1096 1097 if (todo > cando) 1098 todo = cando; 1099 1100 /* FIXME-32x64: Need a version of print_address_numeric which puts the 1101 result in a buffer like sprintf. */ 1102 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr + done, todo); 1103 1104 /* We send target system values byte by byte, in increasing byte addresses, 1105 each byte encoded as two hex characters. */ 1106 1107 p = buf + strlen (buf); 1108 for (i = 0; i < todo; i++) 1109 { 1110 *p++ = tohex ((myaddr[i + done] >> 4) & 0xf); 1111 *p++ = tohex (myaddr[i + done] & 0xf); 1112 } 1113 *p = '\0'; 1114 1115 putpkt (buf); 1116 getpkt (buf, 0); 1117 1118 if (buf[0] == 'E') 1119 { 1120 /* There is no correspondance between what the remote protocol uses 1121 for errors and errno codes. We would like a cleaner way of 1122 representing errors (big enough to include errno codes, bfd_error 1123 codes, and others). But for now just return EIO. */ 1124 errno = EIO; 1125 return 0; 1126 } 1127 done += todo; 1128 } 1129 return len; 1130 } 1131 1132 /* Read memory data directly from the remote machine. 1133 This does not use the data cache; the data cache uses this. 1134 MEMADDR is the address in the remote memory space. 1135 MYADDR is the address of the buffer in our space. 1136 LEN is the number of bytes. 1137 1138 Returns number of bytes transferred, or 0 for error. */ 1139 1140 static int 1141 remote_read_bytes (memaddr, myaddr, len) 1142 CORE_ADDR memaddr; 1143 char *myaddr; 1144 int len; 1145 { 1146 char buf[PBUFSIZ]; 1147 int i; 1148 char *p; 1149 int done; 1150 /* Chop transfer down if neccessary */ 1151 1152 #if 0 1153 /* FIXME: This is wrong for larger packets */ 1154 if (len > PBUFSIZ / 2 - 1) 1155 abort (); 1156 #endif 1157 done = 0; 1158 while (done < len) 1159 { 1160 int todo = len - done; 1161 int cando = PBUFSIZ / 2 - 32; /* number of bytes that will fit. */ 1162 if (todo > cando) 1163 todo = cando; 1164 1165 /* FIXME-32x64: Need a version of print_address_numeric which puts the 1166 result in a buffer like sprintf. */ 1167 sprintf (buf, "m%lx,%x", (unsigned long) memaddr + done, todo); 1168 putpkt (buf); 1169 getpkt (buf, 0); 1170 1171 if (buf[0] == 'E') 1172 { 1173 /* There is no correspondance between what the remote protocol uses 1174 for errors and errno codes. We would like a cleaner way of 1175 representing errors (big enough to include errno codes, bfd_error 1176 codes, and others). But for now just return EIO. */ 1177 errno = EIO; 1178 return 0; 1179 } 1180 1181 /* Reply describes memory byte by byte, 1182 each byte encoded as two hex characters. */ 1183 1184 p = buf; 1185 for (i = 0; i < todo; i++) 1186 { 1187 if (p[0] == 0 || p[1] == 0) 1188 /* Reply is short. This means that we were able to read only part 1189 of what we wanted to. */ 1190 return i + done; 1191 myaddr[i + done] = fromhex (p[0]) * 16 + fromhex (p[1]); 1192 p += 2; 1193 } 1194 done += todo; 1195 } 1196 return len; 1197 } 1198 1199 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring 1200 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is 1201 nonzero. Returns length of data written or read; 0 for error. */ 1202 1203 /* ARGSUSED */ 1204 static int 1205 remote_xfer_memory(memaddr, myaddr, len, should_write, target) 1206 CORE_ADDR memaddr; 1207 char *myaddr; 1208 int len; 1209 int should_write; 1210 struct target_ops *target; /* ignored */ 1211 { 1212 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write); 1213 } 1214 1215 1216 #if 0 1217 /* Enable after 4.12. */ 1218 1219 void 1220 remote_search (len, data, mask, startaddr, increment, lorange, hirange 1221 addr_found, data_found) 1222 int len; 1223 char *data; 1224 char *mask; 1225 CORE_ADDR startaddr; 1226 int increment; 1227 CORE_ADDR lorange; 1228 CORE_ADDR hirange; 1229 CORE_ADDR *addr_found; 1230 char *data_found; 1231 { 1232 if (increment == -4 && len == 4) 1233 { 1234 long mask_long, data_long; 1235 long data_found_long; 1236 CORE_ADDR addr_we_found; 1237 char buf[PBUFSIZ]; 1238 long returned_long[2]; 1239 char *p; 1240 1241 mask_long = extract_unsigned_integer (mask, len); 1242 data_long = extract_unsigned_integer (data, len); 1243 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); 1244 putpkt (buf); 1245 getpkt (buf, 0); 1246 if (buf[0] == '\0') 1247 { 1248 /* The stub doesn't support the 't' request. We might want to 1249 remember this fact, but on the other hand the stub could be 1250 switched on us. Maybe we should remember it only until 1251 the next "target remote". */ 1252 generic_search (len, data, mask, startaddr, increment, lorange, 1253 hirange, addr_found, data_found); 1254 return; 1255 } 1256 1257 if (buf[0] == 'E') 1258 /* There is no correspondance between what the remote protocol uses 1259 for errors and errno codes. We would like a cleaner way of 1260 representing errors (big enough to include errno codes, bfd_error 1261 codes, and others). But for now just use EIO. */ 1262 memory_error (EIO, startaddr); 1263 p = buf; 1264 addr_we_found = 0; 1265 while (*p != '\0' && *p != ',') 1266 addr_we_found = (addr_we_found << 4) + fromhex (*p++); 1267 if (*p == '\0') 1268 error ("Protocol error: short return for search"); 1269 1270 data_found_long = 0; 1271 while (*p != '\0' && *p != ',') 1272 data_found_long = (data_found_long << 4) + fromhex (*p++); 1273 /* Ignore anything after this comma, for future extensions. */ 1274 1275 if (addr_we_found < lorange || addr_we_found >= hirange) 1276 { 1277 *addr_found = 0; 1278 return; 1279 } 1280 1281 *addr_found = addr_we_found; 1282 *data_found = store_unsigned_integer (data_we_found, len); 1283 return; 1284 } 1285 generic_search (len, data, mask, startaddr, increment, lorange, 1286 hirange, addr_found, data_found); 1287 } 1288 #endif /* 0 */ 1289 1290 static void 1291 remote_files_info (ignore) 1292 struct target_ops *ignore; 1293 { 1294 puts_filtered ("Debugging a target over a serial line.\n"); 1295 } 1296 1297 /* Stuff for dealing with the packets which are part of this protocol. 1298 See comment at top of file for details. */ 1299 1300 /* Read a single character from the remote end, masking it down to 7 bits. */ 1301 1302 static int 1303 readchar (timeout) 1304 int timeout; 1305 { 1306 int ch; 1307 1308 ch = SERIAL_READCHAR (remote_desc, timeout); 1309 1310 switch (ch) 1311 { 1312 case SERIAL_EOF: 1313 error ("Remote connection closed"); 1314 case SERIAL_ERROR: 1315 perror_with_name ("Remote communication error"); 1316 case SERIAL_TIMEOUT: 1317 return ch; 1318 default: 1319 return ch & 0x7f; 1320 } 1321 } 1322 1323 /* Send the command in BUF to the remote machine, 1324 and read the reply into BUF. 1325 Report an error if we get an error reply. */ 1326 1327 static void 1328 remote_send (buf) 1329 char *buf; 1330 { 1331 putpkt (buf); 1332 getpkt (buf, 0); 1333 1334 if (buf[0] == 'E') 1335 error ("Remote failure reply: %s", buf); 1336 } 1337 1338 /* Send a packet to the remote machine, with error checking. 1339 The data of the packet is in BUF. */ 1340 1341 static int 1342 putpkt (buf) 1343 char *buf; 1344 { 1345 int i; 1346 unsigned char csum = 0; 1347 char buf2[PBUFSIZ]; 1348 int cnt = strlen (buf); 1349 int ch; 1350 int tcount = 0; 1351 char *p; 1352 1353 /* Copy the packet into buffer BUF2, encapsulating it 1354 and giving it a checksum. */ 1355 1356 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ 1357 abort(); 1358 1359 p = buf2; 1360 *p++ = '$'; 1361 1362 for (i = 0; i < cnt; i++) 1363 { 1364 csum += buf[i]; 1365 *p++ = buf[i]; 1366 } 1367 *p++ = '#'; 1368 *p++ = tohex ((csum >> 4) & 0xf); 1369 *p++ = tohex (csum & 0xf); 1370 1371 /* Send it over and over until we get a positive ack. */ 1372 1373 while (1) 1374 { 1375 int started_error_output = 0; 1376 1377 if (remote_debug) 1378 { 1379 *p = '\0'; 1380 printf_unfiltered ("Sending packet: %s...", buf2); 1381 gdb_flush(gdb_stdout); 1382 } 1383 if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) 1384 perror_with_name ("putpkt: write failed"); 1385 1386 /* read until either a timeout occurs (-2) or '+' is read */ 1387 while (1) 1388 { 1389 ch = readchar (remote_timeout); 1390 1391 if (remote_debug) 1392 { 1393 switch (ch) 1394 { 1395 case '+': 1396 case SERIAL_TIMEOUT: 1397 case '$': 1398 if (started_error_output) 1399 { 1400 putchar_unfiltered ('\n'); 1401 started_error_output = 0; 1402 } 1403 } 1404 } 1405 1406 switch (ch) 1407 { 1408 case '+': 1409 if (remote_debug) 1410 printf_unfiltered("Ack\n"); 1411 return 1; 1412 case SERIAL_TIMEOUT: 1413 tcount ++; 1414 if (tcount > 3) 1415 return 0; 1416 break; /* Retransmit buffer */ 1417 case '$': 1418 { 1419 char junkbuf[PBUFSIZ]; 1420 1421 /* It's probably an old response, and we're out of sync. Just 1422 gobble up the packet and ignore it. */ 1423 getpkt (junkbuf, 0); 1424 continue; /* Now, go look for + */ 1425 } 1426 default: 1427 if (remote_debug) 1428 { 1429 if (!started_error_output) 1430 { 1431 started_error_output = 1; 1432 printf_unfiltered ("putpkt: Junk: "); 1433 } 1434 putchar_unfiltered (ch & 0177); 1435 } 1436 continue; 1437 } 1438 break; /* Here to retransmit */ 1439 } 1440 1441 #if 0 1442 /* This is wrong. If doing a long backtrace, the user should be 1443 able to get out next time we call QUIT, without anything as violent 1444 as interrupt_query. If we want to provide a way out of here 1445 without getting to the next QUIT, it should be based on hitting 1446 ^C twice as in remote_wait. */ 1447 if (quit_flag) 1448 { 1449 quit_flag = 0; 1450 interrupt_query (); 1451 } 1452 #endif 1453 } 1454 } 1455 1456 /* Come here after finding the start of the frame. Collect the rest into BUF, 1457 verifying the checksum, length, and handling run-length compression. 1458 Returns 0 on any error, 1 on success. */ 1459 1460 static int 1461 read_frame (buf) 1462 char *buf; 1463 { 1464 unsigned char csum; 1465 char *bp; 1466 int c; 1467 1468 csum = 0; 1469 bp = buf; 1470 1471 while (1) 1472 { 1473 c = readchar (remote_timeout); 1474 1475 switch (c) 1476 { 1477 case SERIAL_TIMEOUT: 1478 if (remote_debug) 1479 puts_filtered ("Timeout in mid-packet, retrying\n"); 1480 return 0; 1481 case '$': 1482 if (remote_debug) 1483 puts_filtered ("Saw new packet start in middle of old one\n"); 1484 return 0; /* Start a new packet, count retries */ 1485 case '#': 1486 { 1487 unsigned char pktcsum; 1488 1489 *bp = '\000'; 1490 1491 pktcsum = fromhex (readchar (remote_timeout)) << 4; 1492 pktcsum |= fromhex (readchar (remote_timeout)); 1493 1494 if (csum == pktcsum) 1495 return 1; 1496 1497 if (remote_debug) 1498 { 1499 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", 1500 pktcsum, csum); 1501 puts_filtered (buf); 1502 puts_filtered ("\n"); 1503 } 1504 return 0; 1505 } 1506 case '*': /* Run length encoding */ 1507 csum += c; 1508 c = readchar (remote_timeout); 1509 csum += c; 1510 c = c - ' ' + 3; /* Compute repeat count */ 1511 1512 1513 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) 1514 { 1515 memset (bp, *(bp - 1), c); 1516 bp += c; 1517 continue; 1518 } 1519 1520 *bp = '\0'; 1521 printf_filtered ("Repeat count %d too large for buffer: ", c); 1522 puts_filtered (buf); 1523 puts_filtered ("\n"); 1524 return 0; 1525 1526 default: 1527 if (bp < buf + PBUFSIZ - 1) 1528 { 1529 *bp++ = c; 1530 csum += c; 1531 continue; 1532 } 1533 1534 *bp = '\0'; 1535 puts_filtered ("Remote packet too long: "); 1536 puts_filtered (buf); 1537 puts_filtered ("\n"); 1538 1539 return 0; 1540 } 1541 } 1542 } 1543 1544 /* Read a packet from the remote machine, with error checking, 1545 and store it in BUF. BUF is expected to be of size PBUFSIZ. 1546 If FOREVER, wait forever rather than timing out; this is used 1547 while the target is executing user code. */ 1548 1549 static void 1550 getpkt (buf, forever) 1551 char *buf; 1552 int forever; 1553 { 1554 int c; 1555 int tries; 1556 int timeout; 1557 int val; 1558 1559 strcpy (buf,"timeout"); 1560 1561 if (forever) 1562 { 1563 #ifdef MAINTENANCE_CMDS 1564 timeout = watchdog > 0 ? watchdog : -1; 1565 #else 1566 timeout = -1; 1567 #endif 1568 } 1569 1570 else 1571 timeout = remote_timeout; 1572 1573 #define MAX_TRIES 3 1574 1575 for (tries = 1; tries <= MAX_TRIES; tries++) 1576 { 1577 /* This can loop forever if the remote side sends us characters 1578 continuously, but if it pauses, we'll get a zero from readchar 1579 because of timeout. Then we'll count that as a retry. */ 1580 1581 /* Note that we will only wait forever prior to the start of a packet. 1582 After that, we expect characters to arrive at a brisk pace. They 1583 should show up within remote_timeout intervals. */ 1584 1585 do 1586 { 1587 c = readchar (timeout); 1588 1589 if (c == SERIAL_TIMEOUT) 1590 { 1591 #ifdef MAINTENANCE_CMDS 1592 if (forever) /* Watchdog went off. Kill the target. */ 1593 { 1594 target_mourn_inferior (); 1595 error ("Watchdog has expired. Target detached.\n"); 1596 } 1597 #endif 1598 if (remote_debug) 1599 puts_filtered ("Timed out.\n"); 1600 goto retry; 1601 } 1602 } 1603 while (c != '$'); 1604 1605 /* We've found the start of a packet, now collect the data. */ 1606 1607 val = read_frame (buf); 1608 1609 if (val == 1) 1610 { 1611 if (remote_debug) 1612 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf); 1613 SERIAL_WRITE (remote_desc, "+", 1); 1614 return; 1615 } 1616 1617 /* Try the whole thing again. */ 1618 retry: 1619 SERIAL_WRITE (remote_desc, "-", 1); 1620 } 1621 1622 /* We have tried hard enough, and just can't receive the packet. Give up. */ 1623 1624 printf_unfiltered ("Ignoring packet error, continuing...\n"); 1625 SERIAL_WRITE (remote_desc, "+", 1); 1626 } 1627 1628 static void 1629 remote_kill () 1630 { 1631 /* For some mysterious reason, wait_for_inferior calls kill instead of 1632 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ 1633 if (kill_kludge) 1634 { 1635 kill_kludge = 0; 1636 target_mourn_inferior (); 1637 return; 1638 } 1639 1640 /* Use catch_errors so the user can quit from gdb even when we aren't on 1641 speaking terms with the remote system. */ 1642 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR); 1643 1644 /* Don't wait for it to die. I'm not really sure it matters whether 1645 we do or not. For the existing stubs, kill is a noop. */ 1646 target_mourn_inferior (); 1647 } 1648 1649 static void 1650 remote_mourn () 1651 { 1652 remote_mourn_1 (&remote_ops); 1653 } 1654 1655 static void 1656 extended_remote_mourn () 1657 { 1658 /* We do _not_ want to mourn the target like this; this will 1659 remove the extended remote target from the target stack, 1660 and the next time the user says "run" it'll fail. 1661 1662 FIXME: What is the right thing to do here? */ 1663 #if 0 1664 remote_mourn_1 (&extended_remote_ops); 1665 #endif 1666 } 1667 1668 /* Worker function for remote_mourn. */ 1669 static void 1670 remote_mourn_1 (target) 1671 struct target_ops *target; 1672 { 1673 unpush_target (target); 1674 generic_mourn_inferior (); 1675 } 1676 1677 /* In the extended protocol we want to be able to do things like 1678 "run" and have them basically work as expected. So we need 1679 a special create_inferior function. 1680 1681 FIXME: One day add support for changing the exec file 1682 we're debugging, arguments and an environment. */ 1683 1684 static void 1685 extended_remote_create_inferior (exec_file, args, env) 1686 char *exec_file; 1687 char *args; 1688 char **env; 1689 { 1690 /* Rip out the breakpoints; we'll reinsert them after restarting 1691 the remote server. */ 1692 remove_breakpoints (); 1693 1694 /* Now restart the remote server. */ 1695 extended_remote_restart (); 1696 1697 /* Now put the breakpoints back in. This way we're safe if the 1698 restart function works via a unix fork on the remote side. */ 1699 insert_breakpoints (); 1700 1701 /* Clean up from the last time we were running. */ 1702 clear_proceed_status (); 1703 1704 /* Let the remote process run. */ 1705 proceed (-1, TARGET_SIGNAL_0, 0); 1706 } 1707 1708 1709 /* On some machines, e.g. 68k, we may use a different breakpoint instruction 1710 than other targets; in those use REMOTE_BREAKPOINT instead of just 1711 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT 1712 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call 1713 the standard routines that are in mem-break.c. */ 1714 1715 /* FIXME, these ought to be done in a more dynamic fashion. For instance, 1716 the choice of breakpoint instruction affects target program design and 1717 vice versa, and by making it user-tweakable, the special code here 1718 goes away and we need fewer special GDB configurations. */ 1719 1720 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) 1721 #define REMOTE_BREAKPOINT 1722 #endif 1723 1724 #ifdef REMOTE_BREAKPOINT 1725 1726 /* If the target isn't bi-endian, just pretend it is. */ 1727 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) 1728 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT 1729 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT 1730 #endif 1731 1732 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; 1733 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; 1734 1735 #endif /* REMOTE_BREAKPOINT */ 1736 1737 /* Insert a breakpoint on targets that don't have any better breakpoint 1738 support. We read the contents of the target location and stash it, 1739 then overwrite it with a breakpoint instruction. ADDR is the target 1740 location in the target machine. CONTENTS_CACHE is a pointer to 1741 memory allocated for saving the target contents. It is guaranteed 1742 by the caller to be long enough to save sizeof BREAKPOINT bytes (this 1743 is accomplished via BREAKPOINT_MAX). */ 1744 1745 static int 1746 remote_insert_breakpoint (addr, contents_cache) 1747 CORE_ADDR addr; 1748 char *contents_cache; 1749 { 1750 #ifdef REMOTE_BREAKPOINT 1751 int val; 1752 1753 val = target_read_memory (addr, contents_cache, sizeof big_break_insn); 1754 1755 if (val == 0) 1756 { 1757 if (TARGET_BYTE_ORDER == BIG_ENDIAN) 1758 val = target_write_memory (addr, (char *) big_break_insn, 1759 sizeof big_break_insn); 1760 else 1761 val = target_write_memory (addr, (char *) little_break_insn, 1762 sizeof little_break_insn); 1763 } 1764 1765 return val; 1766 #else 1767 return memory_insert_breakpoint (addr, contents_cache); 1768 #endif /* REMOTE_BREAKPOINT */ 1769 } 1770 1771 static int 1772 remote_remove_breakpoint (addr, contents_cache) 1773 CORE_ADDR addr; 1774 char *contents_cache; 1775 { 1776 #ifdef REMOTE_BREAKPOINT 1777 return target_write_memory (addr, contents_cache, sizeof big_break_insn); 1778 #else 1779 return memory_remove_breakpoint (addr, contents_cache); 1780 #endif /* REMOTE_BREAKPOINT */ 1781 } 1782 1783 /* Define the target subroutine names */ 1784 1785 struct target_ops remote_ops = { 1786 "remote", /* to_shortname */ 1787 "Remote serial target in gdb-specific protocol", /* to_longname */ 1788 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ 1789 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ 1790 remote_open, /* to_open */ 1791 remote_close, /* to_close */ 1792 NULL, /* to_attach */ 1793 remote_detach, /* to_detach */ 1794 remote_resume, /* to_resume */ 1795 remote_wait, /* to_wait */ 1796 remote_fetch_registers, /* to_fetch_registers */ 1797 remote_store_registers, /* to_store_registers */ 1798 remote_prepare_to_store, /* to_prepare_to_store */ 1799 remote_xfer_memory, /* to_xfer_memory */ 1800 remote_files_info, /* to_files_info */ 1801 remote_insert_breakpoint, /* to_insert_breakpoint */ 1802 remote_remove_breakpoint, /* to_remove_breakpoint */ 1803 NULL, /* to_terminal_init */ 1804 NULL, /* to_terminal_inferior */ 1805 NULL, /* to_terminal_ours_for_output */ 1806 NULL, /* to_terminal_ours */ 1807 NULL, /* to_terminal_info */ 1808 remote_kill, /* to_kill */ 1809 generic_load, /* to_load */ 1810 NULL, /* to_lookup_symbol */ 1811 NULL, /* to_create_inferior */ 1812 remote_mourn, /* to_mourn_inferior */ 1813 0, /* to_can_run */ 1814 0, /* to_notice_signals */ 1815 remote_thread_alive, /* to_thread_alive */ 1816 0, /* to_stop */ 1817 process_stratum, /* to_stratum */ 1818 NULL, /* to_next */ 1819 1, /* to_has_all_memory */ 1820 1, /* to_has_memory */ 1821 1, /* to_has_stack */ 1822 1, /* to_has_registers */ 1823 1, /* to_has_execution */ 1824 NULL, /* sections */ 1825 NULL, /* sections_end */ 1826 OPS_MAGIC /* to_magic */ 1827 }; 1828 1829 struct target_ops extended_remote_ops = { 1830 "extended-remote", /* to_shortname */ 1831 "Extended remote serial target in gdb-specific protocol",/* to_longname */ 1832 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ 1833 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ 1834 extended_remote_open, /* to_open */ 1835 remote_close, /* to_close */ 1836 NULL, /* to_attach */ 1837 remote_detach, /* to_detach */ 1838 remote_resume, /* to_resume */ 1839 remote_wait, /* to_wait */ 1840 remote_fetch_registers, /* to_fetch_registers */ 1841 remote_store_registers, /* to_store_registers */ 1842 remote_prepare_to_store, /* to_prepare_to_store */ 1843 remote_xfer_memory, /* to_xfer_memory */ 1844 remote_files_info, /* to_files_info */ 1845 1846 remote_insert_breakpoint, /* to_insert_breakpoint */ 1847 remote_remove_breakpoint, /* to_remove_breakpoint */ 1848 1849 NULL, /* to_terminal_init */ 1850 NULL, /* to_terminal_inferior */ 1851 NULL, /* to_terminal_ours_for_output */ 1852 NULL, /* to_terminal_ours */ 1853 NULL, /* to_terminal_info */ 1854 remote_kill, /* to_kill */ 1855 generic_load, /* to_load */ 1856 NULL, /* to_lookup_symbol */ 1857 extended_remote_create_inferior,/* to_create_inferior */ 1858 extended_remote_mourn, /* to_mourn_inferior */ 1859 0, /* to_can_run */ 1860 0, /* to_notice_signals */ 1861 remote_thread_alive, /* to_thread_alive */ 1862 0, /* to_stop */ 1863 process_stratum, /* to_stratum */ 1864 NULL, /* to_next */ 1865 1, /* to_has_all_memory */ 1866 1, /* to_has_memory */ 1867 1, /* to_has_stack */ 1868 1, /* to_has_registers */ 1869 1, /* to_has_execution */ 1870 NULL, /* sections */ 1871 NULL, /* sections_end */ 1872 OPS_MAGIC /* to_magic */ 1873 }; 1874 1875 void 1876 _initialize_remote () 1877 { 1878 add_target (&remote_ops); 1879 add_target (&extended_remote_ops); 1880 1881 add_show_from_set (add_set_cmd ("remotetimeout", no_class, 1882 var_integer, (char *)&remote_timeout, 1883 "Set timeout value for remote read.\n", &setlist), 1884 &showlist); 1885 1886 add_show_from_set (add_set_cmd ("remotebreak", no_class, 1887 var_integer, (char *)&remote_break, 1888 "Set whether to send break if interrupted.\n", &setlist), 1889 &showlist); 1890 1891 add_show_from_set (add_set_cmd ("remotewritesize", no_class, 1892 var_integer, (char *)&remote_write_size, 1893 "Set the maximum number of bytes in each memory write packet.\n", &setlist), 1894 &showlist); 1895 } 1896