remote.c revision 46289
1/* Remote target communications for serial-line targets in custom GDB protocol 2 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 1998 3 Free Software Foundation, Inc. 4 5This file is part of GDB. 6 7This program is free software; you can redistribute it and/or modify 8it under the terms of the GNU General Public License as published by 9the Free Software Foundation; either version 2 of the License, or 10(at your option) any later version. 11 12This program is distributed in the hope that it will be useful, 13but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15GNU General Public License for more details. 16 17You should have received a copy of the GNU General Public License 18along with this program; if not, write to the Free Software 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 20 21/* Remote communication protocol. 22 23 A debug packet whose contents are <data> 24 is encapsulated for transmission in the form: 25 26 $ <data> # CSUM1 CSUM2 27 28 <data> must be ASCII alphanumeric and cannot include characters 29 '$' or '#'. If <data> starts with two characters followed by 30 ':', then the existing stubs interpret this as a sequence number. 31 32 CSUM1 and CSUM2 are ascii hex representation of an 8-bit 33 checksum of <data>, the most significant nibble is sent first. 34 the hex digits 0-9,a-f are used. 35 36 Receiver responds with: 37 38 + - if CSUM is correct and ready for next packet 39 - - if CSUM is incorrect 40 41 <data> is as follows: 42 Most values are encoded in ascii hex digits. Signal numbers are according 43 to the numbering in target.h. 44 45 Request Packet 46 47 set thread Hct... Set thread for subsequent operations. 48 c = 'c' for thread used in step and 49 continue; t... can be -1 for all 50 threads. 51 c = 'g' for thread used in other 52 operations. If zero, pick a thread, 53 any thread. 54 reply OK for success 55 ENN for an error. 56 57 read registers g 58 reply XX....X Each byte of register data 59 is described by two hex digits. 60 Registers are in the internal order 61 for GDB, and the bytes in a register 62 are in the same order the machine uses. 63 or ENN for an error. 64 65 write regs GXX..XX Each byte of register data 66 is described by two hex digits. 67 reply OK for success 68 ENN for an error 69 70 write reg Pn...=r... Write register n... with value r..., 71 which contains two hex digits for each 72 byte in the register (target byte 73 order). 74 reply OK for success 75 ENN for an error 76 (not supported by all stubs). 77 78 read mem mAA..AA,LLLL AA..AA is address, LLLL is length. 79 reply XX..XX XX..XX is mem contents 80 Can be fewer bytes than requested 81 if able to read only part of the data. 82 or ENN NN is errno 83 84 write mem MAA..AA,LLLL:XX..XX 85 AA..AA is address, 86 LLLL is number of bytes, 87 XX..XX is data 88 reply OK for success 89 ENN for an error (this includes the case 90 where only part of the data was 91 written). 92 93 write mem XAA..AA,LLLL:XX..XX 94 (binary) AA..AA is address, 95 LLLL is number of bytes, 96 XX..XX is binary data 97 reply OK for success 98 ENN for an error 99 100 continue cAA..AA AA..AA is address to resume 101 If AA..AA is omitted, 102 resume at same address. 103 104 step sAA..AA AA..AA is address to resume 105 If AA..AA is omitted, 106 resume at same address. 107 108 continue with Csig;AA..AA Continue with signal sig (hex signal 109 signal number). If ;AA..AA is omitted, 110 resume at same address. 111 112 step with Ssig;AA..AA Like 'C' but step not continue. 113 signal 114 115 last signal ? Reply the current reason for stopping. 116 This is the same reply as is generated 117 for step or cont : SAA where AA is the 118 signal number. 119 120 detach D Reply OK. 121 122 There is no immediate reply to step or cont. 123 The reply comes when the machine stops. 124 It is SAA AA is the signal number. 125 126 or... TAAn...:r...;n...:r...;n...:r...; 127 AA = signal number 128 n... = register number (hex) 129 r... = register contents 130 n... = `thread' 131 r... = thread process ID. This is 132 a hex integer. 133 n... = other string not starting 134 with valid hex digit. 135 gdb should ignore this n,r pair 136 and go on to the next. This way 137 we can extend the protocol. 138 or... WAA The process exited, and AA is 139 the exit status. This is only 140 applicable for certains sorts of 141 targets. 142 or... XAA The process terminated with signal 143 AA. 144 or... OXX..XX XX..XX is hex encoding of ASCII data. This 145 can happen at any time while the 146 program is running and the debugger 147 should continue to wait for 148 'W', 'T', etc. 149 150 thread alive TXX Find out if the thread XX is alive. 151 reply OK thread is still alive 152 ENN thread is dead 153 154 remote restart RXX Restart the remote server 155 156 extended ops ! Use the extended remote protocol. 157 Sticky -- only needs to be set once. 158 159 kill request k 160 161 toggle debug d toggle debug flag (see 386 & 68k stubs) 162 reset r reset -- see sparc stub. 163 reserved <other> On other requests, the stub should 164 ignore the request and send an empty 165 response ($#<checksum>). This way 166 we can extend the protocol and GDB 167 can tell whether the stub it is 168 talking to uses the old or the new. 169 search tAA:PP,MM Search backwards starting at address 170 AA for a match with pattern PP and 171 mask MM. PP and MM are 4 bytes. 172 Not supported by all stubs. 173 174 general query qXXXX Request info about XXXX. 175 general set QXXXX=yyyy Set value of XXXX to yyyy. 176 query sect offs qOffsets Get section offsets. Reply is 177 Text=xxx;Data=yyy;Bss=zzz 178 179 Responses can be run-length encoded to save space. A '*' means that 180 the next character is an ASCII encoding giving a repeat count which 181 stands for that many repititions of the character preceding the '*'. 182 The encoding is n+29, yielding a printable character where n >=3 183 (which is where rle starts to win). Don't use an n > 126. 184 185 So 186 "0* " means the same as "0000". */ 187 188#include "defs.h" 189#include "gdb_string.h" 190#include <ctype.h> 191#include <fcntl.h> 192#include "frame.h" 193#include "inferior.h" 194#include "bfd.h" 195#include "symfile.h" 196#include "target.h" 197#include "wait.h" 198/*#include "terminal.h"*/ 199#include "gdbcmd.h" 200#include "objfiles.h" 201#include "gdb-stabs.h" 202#include "gdbthread.h" 203 204#include "dcache.h" 205 206#ifdef USG 207#include <sys/types.h> 208#endif 209 210#include <signal.h> 211#include "serial.h" 212 213/* Prototypes for local functions */ 214 215static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, 216 char *myaddr, int len)); 217 218static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, 219 char *myaddr, int len)); 220 221static void remote_files_info PARAMS ((struct target_ops *ignore)); 222 223static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char * myaddr, 224 int len, int should_write, 225 struct target_ops * target)); 226 227static void remote_prepare_to_store PARAMS ((void)); 228 229static void remote_fetch_registers PARAMS ((int regno)); 230 231static void remote_resume PARAMS ((int pid, int step, 232 enum target_signal siggnal)); 233 234static int remote_start_remote PARAMS ((PTR)); 235 236static void remote_open PARAMS ((char *name, int from_tty)); 237 238static void extended_remote_open PARAMS ((char *name, int from_tty)); 239 240static void remote_open_1 PARAMS ((char *, int, struct target_ops *, 241 int extended_p)); 242 243static void remote_close PARAMS ((int quitting)); 244 245static void remote_store_registers PARAMS ((int regno)); 246 247static void remote_mourn PARAMS ((void)); 248 249static void extended_remote_restart PARAMS ((void)); 250 251static void extended_remote_mourn PARAMS ((void)); 252 253static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); 254 255static void remote_mourn_1 PARAMS ((struct target_ops *)); 256 257static void remote_send PARAMS ((char *buf)); 258 259static int readchar PARAMS ((int timeout)); 260 261static int remote_wait PARAMS ((int pid, struct target_waitstatus * status)); 262 263static void remote_kill PARAMS ((void)); 264 265static int tohex PARAMS ((int nib)); 266 267static void remote_detach PARAMS ((char *args, int from_tty)); 268 269static void remote_interrupt PARAMS ((int signo)); 270 271static void interrupt_query PARAMS ((void)); 272 273static void set_thread PARAMS ((int, int)); 274 275static int remote_thread_alive PARAMS ((int)); 276 277static void get_offsets PARAMS ((void)); 278 279static int read_frame PARAMS ((char *)); 280 281static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); 282 283static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); 284 285static int hexnumlen PARAMS ((ULONGEST num)); 286 287static void init_remote_ops PARAMS ((void)); 288 289static void init_extended_remote_ops PARAMS ((void)); 290 291static void remote_stop PARAMS ((void)); 292 293static int ishex PARAMS ((int ch, int *val)); 294 295static int stubhex PARAMS ((int ch)); 296 297static int remote_query PARAMS ((int/*char*/, char *, char *, int *)); 298 299static int hexnumstr PARAMS ((char *, ULONGEST)); 300 301static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR)); 302 303static void print_packet PARAMS ((char *)); 304 305static unsigned long crc32 PARAMS ((unsigned char *, int, unsigned int)); 306 307static void compare_sections_command PARAMS ((char *, int)); 308 309static void packet_command PARAMS ((char *, int)); 310 311static int stub_unpack_int PARAMS ((char *buff, int fieldlength)); 312 313char *unpack_varlen_hex PARAMS ((char *buff, int *result)); 314 315static char *unpack_nibble PARAMS ((char *buf, int *val)); 316 317static char *pack_nibble PARAMS ((char *buf, int nibble)); 318 319static char *pack_hex_byte PARAMS ((char *pkt, int/*unsigned char*/ byte)); 320 321static char *unpack_byte PARAMS ((char *buf, int *value)); 322 323static char *pack_int PARAMS ((char *buf, int value)); 324 325static char *unpack_int PARAMS ((char *buf, int *value)); 326 327static char *unpack_string PARAMS ((char *src, char *dest, int length)); 328 329static char *pack_threadid PARAMS ((char *pkt, threadref *id)); 330 331static char *unpack_threadid PARAMS ((char *inbuf, threadref *id)); 332 333void int_to_threadref PARAMS ((threadref *id, int value)); 334 335static int threadref_to_int PARAMS ((threadref *ref)); 336 337static void copy_threadref PARAMS ((threadref *dest, threadref *src)); 338 339static int threadmatch PARAMS ((threadref *dest, threadref *src)); 340 341static char *pack_threadinfo_request PARAMS ((char *pkt, int mode, 342 threadref *id)); 343 344static int remote_unpack_thread_info_response PARAMS ((char *pkt, 345 threadref *expectedref, 346 struct gdb_ext_thread_info *info)); 347 348 349static int remote_get_threadinfo PARAMS ((threadref *threadid, 350 int fieldset, /*TAG mask */ 351 struct gdb_ext_thread_info *info)); 352 353static int adapt_remote_get_threadinfo PARAMS ((gdb_threadref *ref, 354 int selection, 355 struct gdb_ext_thread_info *info)); 356 357static char *pack_threadlist_request PARAMS ((char *pkt, int startflag, 358 int threadcount, 359 threadref *nextthread)); 360 361static int parse_threadlist_response PARAMS ((char *pkt, 362 int result_limit, 363 threadref *original_echo, 364 threadref *resultlist, 365 int *doneflag)); 366 367static int remote_get_threadlist PARAMS ((int startflag, 368 threadref *nextthread, 369 int result_limit, 370 int *done, 371 int *result_count, 372 threadref *threadlist)); 373 374typedef int (*rmt_thread_action) (threadref *ref, void *context); 375 376static int remote_threadlist_iterator PARAMS ((rmt_thread_action stepfunction, 377 void *context, int looplimit)); 378 379static int remote_newthread_step PARAMS ((threadref *ref, void *context)); 380 381static int remote_current_thread PARAMS ((int oldpid)); 382 383int remote_find_new_threads PARAMS ((void)); 384 385static void record_currthread PARAMS ((int currthread)); 386 387static void init_remote_threads PARAMS ((void)); 388 389/* exported functions */ 390 391extern int fromhex PARAMS ((int a)); 392 393extern void getpkt PARAMS ((char *buf, int forever)); 394 395extern int putpkt PARAMS ((char *buf)); 396 397static int putpkt_binary PARAMS ((char *buf, int cnt)); 398 399void remote_console_output PARAMS ((char *)); 400 401static void check_binary_download PARAMS ((CORE_ADDR addr)); 402 403/* Define the target subroutine names */ 404 405void open_remote_target PARAMS ((char *, int, struct target_ops *, int)); 406 407void _initialize_remote PARAMS ((void)); 408 409/* */ 410 411static struct target_ops remote_ops; 412 413static struct target_ops extended_remote_ops; 414 415static struct target_thread_vector remote_thread_vec; 416 417/* This was 5 seconds, which is a long time to sit and wait. 418 Unless this is going though some terminal server or multiplexer or 419 other form of hairy serial connection, I would think 2 seconds would 420 be plenty. */ 421 422/* Changed to allow option to set timeout value. 423 was static int remote_timeout = 2; */ 424extern int remote_timeout; 425 426/* This variable chooses whether to send a ^C or a break when the user 427 requests program interruption. Although ^C is usually what remote 428 systems expect, and that is the default here, sometimes a break is 429 preferable instead. */ 430 431static int remote_break; 432 433/* Has the user attempted to interrupt the target? If so, then offer 434 the user the opportunity to bail out completely if he interrupts 435 again. */ 436static int interrupted_already = 0; 437 438/* Descriptor for I/O to remote machine. Initialize it to NULL so that 439 remote_open knows that we don't have a file open when the program 440 starts. */ 441static serial_t remote_desc = NULL; 442 443/* This variable (available to the user via "set remotebinarydownload") 444 dictates whether downloads are sent in binary (via the 'X' packet). 445 We assume that the stub can, and attempt to do it. This will be cleared if 446 the stub does not understand it. This switch is still needed, though 447 in cases when the packet is supported in the stub, but the connection 448 does not allow it (i.e., 7-bit serial connection only). */ 449static int remote_binary_download = 1; 450 451/* Have we already checked whether binary downloads work? */ 452static int remote_binary_checked; 453 454/* Having this larger than 400 causes us to be incompatible with m68k-stub.c 455 and i386-stub.c. Normally, no one would notice because it only matters 456 for writing large chunks of memory (e.g. in downloads). Also, this needs 457 to be more than 400 if required to hold the registers (see below, where 458 we round it up based on REGISTER_BYTES). */ 459#define PBUFSIZ 400 460 461/* Maximum number of bytes to read/write at once. The value here 462 is chosen to fill up a packet (the headers account for the 32). */ 463#define MAXBUFBYTES ((PBUFSIZ-32)/2) 464 465/* Round up PBUFSIZ to hold all the registers, at least. */ 466/* The blank line after the #if seems to be required to work around a 467 bug in HP's PA compiler. */ 468#if REGISTER_BYTES > MAXBUFBYTES 469 470#undef PBUFSIZ 471#define PBUFSIZ (REGISTER_BYTES * 2 + 32) 472#endif 473 474/* This variable sets the number of bytes to be written to the target 475 in a single packet. Normally PBUFSIZ is satisfactory, but some 476 targets need smaller values (perhaps because the receiving end 477 is slow). */ 478 479static int remote_write_size = PBUFSIZ; 480 481/* This variable sets the number of bits in an address that are to be 482 sent in a memory ("M" or "m") packet. Normally, after stripping 483 leading zeros, the entire address would be sent. This variable 484 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The 485 initial implementation of remote.c restricted the address sent in 486 memory packets to ``host::sizeof long'' bytes - (typically 32 487 bits). Consequently, for 64 bit targets, the upper 32 bits of an 488 address was never sent. Since fixing this bug may cause a break in 489 some remote targets this variable is principly provided to 490 facilitate backward compatibility. */ 491 492static int remote_address_size; 493 494/* This is the size (in chars) of the first response to the `g' command. This 495 is used to limit the size of the memory read and write commands to prevent 496 stub buffers from overflowing. The size does not include headers and 497 trailers, it is only the payload size. */ 498 499static int remote_register_buf_size = 0; 500 501/* Should we try the 'P' request? If this is set to one when the stub 502 doesn't support 'P', the only consequence is some unnecessary traffic. */ 503static int stub_supports_P = 1; 504 505/* These are pointers to hook functions that may be set in order to 506 modify resume/wait behavior for a particular architecture. */ 507 508void (*target_resume_hook) PARAMS ((void)); 509void (*target_wait_loop_hook) PARAMS ((void)); 510 511 512/* 513 * Support for quasi-interactive control of device through GDB port. 514 * While we're waiting for an event to occur, chat with the running device. 515 */ 516#define REMOTE_CHAT 517#ifdef REMOTE_CHAT 518 519extern int quit_flag; 520 521static char tty_input[256]; 522static int escape_count; 523static int echo_check; 524static int remote_chat = 0; 525 526enum read_stat { 527 READ_MORE, 528 FATAL_ERROR, 529 ENTER_DEBUG 530}; 531 532static enum read_stat 533readtarget() 534{ 535 int j; 536 int data; 537 538 /* Loop until the socket doesn't have any more data */ 539 while ((data = readchar(0)) >= 0) { 540 541 /* Check for the escape sequence */ 542 if (data == '|') { 543 /* If this is the fourth escape, get out */ 544 if (++escape_count == 4) 545 return (ENTER_DEBUG); 546 continue; /* Not the fourth, continue */ 547 548 } else { 549 /* 550 * Not an escape any more, Ensure any pending ones are flushed 551 */ 552 for (j = 1; j <= escape_count; j++) 553 putchar('|'); 554 escape_count = 0; 555 } 556 557 if (data == '\r') /* If this is a return character */ 558 continue; /* just supress it */ 559 560 if (echo_check != -1) { /* If we are checking for an echo */ 561 /* If this might be an echo */ 562 if (tty_input[echo_check] == data) { 563 echo_check++; /* Note one more character match */ 564 continue; /* Go and loop */ 565 } else { 566 567 if ((data == '\n') && (tty_input[echo_check] == '\r')) { 568 /* If this is the end of the line */ 569 echo_check = -1; /* No more echo supression */ 570 continue; /* Go and loop */ 571 } 572 573 /* Not an echo, print out the data */ 574 for (j = 0; j < echo_check; j++) 575 putchar(tty_input[j]); 576 577 echo_check = -1;/* No more echo checking */ 578 } 579 } 580 putchar(data); /* Output the character */ 581 } 582 return (READ_MORE); /* Indicate to read some more */ 583} 584 585static enum read_stat 586readtty() 587{ 588 enum read_stat status; 589 int tty_bc; 590 591 /* First, read a buffer full from the terminal */ 592 if ((tty_bc = read(fileno(stdin), tty_input, sizeof(tty_input) - 1)) < 0) { 593 perror_with_name("readtty: read failed"); 594 return (FATAL_ERROR); 595 } 596 597 /* Turn trailing newlines into returns */ 598 if (tty_input[tty_bc - 1] == '\n') 599 tty_input[tty_bc - 1] = '\r'; 600 601 if ((tty_input[0] == '~') && (tty_bc == 3)) 602 switch (tty_input[1]) { 603 case 'b': /* ~b\n = send break & gdb */ 604 SERIAL_SEND_BREAK (remote_desc); 605 /* fall through */ 606 607 case 'c': /* ~c\n = return to gdb */ 608 return (ENTER_DEBUG); 609 } 610 611 /* Make this a zero terminated string and write it out */ 612 tty_input[tty_bc] = '\0'; 613 614 if (SERIAL_WRITE(remote_desc, tty_input, tty_bc)) { 615 perror_with_name("readtty: write failed"); 616 return (FATAL_ERROR); 617 } 618 619 return (READ_MORE); 620} 621 622static int 623remote_talk() 624{ 625 fd_set input; 626 int tablesize; 627 enum read_stat status; 628 int panic_flag = 0; 629 char buf[4]; 630 631 escape_count = 0; 632 echo_check = -1; 633 634 tablesize = getdtablesize(); 635 636 for (;;) { 637 638 /* 639 * Check for anything from our socket - doesn't block. Note that this 640 * must be done *before* the select as there may be buffered I/O 641 * waiting to be processed. 642 */ 643 if ((status = readtarget()) != READ_MORE) 644 return (status); 645 646 fflush(stdout); /* Flush output before blocking */ 647 648 /* Now block on more socket input or TTY input */ 649 FD_ZERO(&input); 650 FD_SET(fileno(stdin), &input); 651 FD_SET(remote_desc->fd, &input); 652 653 status = select(tablesize, &input, 0, 0, 0); 654 if ((status < 0) && (errno != EINTR)) { 655 perror_with_name("remote_talk: select"); 656 return (FATAL_ERROR); 657 } 658 659 /* Handle Control-C typed */ 660 if (quit_flag) { 661 if ((++panic_flag) == 3) { 662 printf("\nAre you repeating Control-C to terminate " 663 "the session? (y/n) [n] "); 664 fgets(buf, 3, stdin); 665 if (buf[0] == 'y') { 666 pop_target(); /* Clean up */ 667 error("Debugging terminated by user interrupt"); 668 } 669 panic_flag = 0; 670 } 671 quit_flag = 0; 672 SERIAL_WRITE(remote_desc, "\003", 1); 673 continue; 674 } 675 676 /* Handle terminal input */ 677 if (FD_ISSET(fileno(stdin), &input)) { 678 panic_flag = 0; 679 status = readtty(); 680 if (status != READ_MORE) 681 return (status); 682 echo_check = 0; 683 } 684 } 685} 686 687#endif /* REMOTE_CHAT */ 688 689/* These are the threads which we last sent to the remote system. 690 -1 for all or -2 for not sent yet. */ 691static int general_thread; 692static int cont_thread; 693 694/* Call this function as a result of 695 1) A halt indication (T packet) containing a thread id 696 2) A direct query of currthread 697 3) Successful execution of set thread 698 */ 699 700static void 701record_currthread (currthread) 702 int currthread; 703{ 704#if 0 /* target_wait must not modify inferior_pid! */ 705 inferior_pid = currthread; 706#endif 707 general_thread = currthread; 708#if 0 /* setting cont_thread has a different meaning 709 from having the target report its thread id. */ 710 cont_thread = currthread; 711#endif 712 /* If this is a new thread, add it to GDB's thread list. 713 If we leave it up to WFI to do this, bad things will happen. */ 714 if (!in_thread_list (currthread)) 715 add_thread (currthread); 716} 717 718#define MAGIC_NULL_PID 42000 719 720static void 721set_thread (th, gen) 722 int th; 723 int gen; 724{ 725 char buf[PBUFSIZ]; 726 int state = gen ? general_thread : cont_thread; 727 728 if (state == th) 729 return; 730 731 buf[0] = 'H'; 732 buf[1] = gen ? 'g' : 'c'; 733 if (th == MAGIC_NULL_PID) 734 { 735 buf[2] = '0'; 736 buf[3] = '\0'; 737 } 738 else if (th < 0) 739 sprintf (&buf[2], "-%x", -th); 740 else 741 sprintf (&buf[2], "%x", th); 742 putpkt (buf); 743 getpkt (buf, 0); 744 if (gen) 745 general_thread = th; 746 else 747 cont_thread = th; 748} 749 750/* Return nonzero if the thread TH is still alive on the remote system. */ 751 752static int 753remote_thread_alive (th) 754 int th; 755{ 756 char buf[PBUFSIZ]; 757 758 buf[0] = 'T'; 759 if (th < 0) 760 sprintf (&buf[1], "-%08x", -th); 761 else 762 sprintf (&buf[1], "%08x", th); 763 putpkt (buf); 764 getpkt (buf, 0); 765 return (buf[0] == 'O' && buf[1] == 'K'); 766} 767 768/* About these extended threadlist and threadinfo packets. They are 769 variable length packets but, the fields within them are often fixed 770 length. They are redundent enough to send over UDP as is the 771 remote protocol in general. There is a matching unit test module 772 in libstub. */ 773 774#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2) 775 776/* encode 64 bits in 16 chars of hex */ 777 778static const char hexchars[] = "0123456789abcdef"; 779 780static int 781ishex (ch, val) 782 int ch; 783 int *val; 784{ 785 if ((ch >= 'a') && (ch <= 'f')) 786 { 787 *val = ch - 'a' + 10; 788 return 1; 789 } 790 if ((ch >= 'A') && (ch <= 'F')) 791 { 792 *val = ch - 'A' + 10; 793 return 1; 794 } 795 if ((ch >= '0') && (ch <= '9')) 796 { 797 *val = ch - '0'; 798 return 1; 799 } 800 return 0; 801} 802 803static int 804stubhex (ch) 805 int ch; 806{ 807 if (ch >= 'a' && ch <= 'f') 808 return ch - 'a' + 10; 809 if (ch >= '0' && ch <= '9') 810 return ch - '0'; 811 if (ch >= 'A' && ch <= 'F') 812 return ch - 'A' + 10; 813 return -1; 814} 815 816static int 817stub_unpack_int (buff, fieldlength) 818 char *buff; 819 int fieldlength; 820{ 821 int nibble; 822 int retval = 0; 823 824 while (fieldlength) 825 { 826 nibble = stubhex (*buff++); 827 retval |= nibble; 828 fieldlength--; 829 if (fieldlength) 830 retval = retval << 4; 831 } 832 return retval; 833} 834 835char * 836unpack_varlen_hex (buff, result) 837 char *buff; /* packet to parse */ 838 int *result; 839{ 840 int nibble; 841 int retval = 0; 842 843 while (ishex (*buff, &nibble)) 844 { 845 buff++; 846 retval = retval << 4; 847 retval |= nibble & 0x0f; 848 } 849 *result = retval; 850 return buff; 851} 852 853static char * 854unpack_nibble (buf, val) 855 char *buf; 856 int *val; 857{ 858 ishex (*buf++, val); 859 return buf; 860} 861 862static char * 863pack_nibble (buf, nibble) 864 char *buf; 865 int nibble; 866{ 867 *buf++ = hexchars[(nibble & 0x0f)]; 868 return buf; 869} 870 871static char * 872pack_hex_byte (pkt, byte) 873 char *pkt; 874 int byte; 875{ 876 *pkt++ = hexchars[(byte >> 4) & 0xf]; 877 *pkt++ = hexchars[(byte & 0xf)]; 878 return pkt; 879} 880 881static char * 882unpack_byte (buf, value) 883 char *buf; 884 int *value; 885{ 886 *value = stub_unpack_int (buf, 2); 887 return buf + 2; 888} 889 890static char * 891pack_int (buf, value) 892 char *buf; 893 int value; 894{ 895 buf = pack_hex_byte (buf, (value >> 24) & 0xff); 896 buf = pack_hex_byte (buf, (value >> 16) & 0xff); 897 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); 898 buf = pack_hex_byte (buf, (value & 0xff)); 899 return buf; 900} 901 902static char * 903unpack_int (buf, value) 904 char *buf; 905 int *value; 906{ 907 *value = stub_unpack_int (buf, 8); 908 return buf + 8; 909} 910 911#if 0 /* currently unused, uncomment when needed */ 912static char *pack_string PARAMS ((char *pkt, char *string)); 913 914static char * 915pack_string (pkt, string) 916 char *pkt; 917 char *string; 918{ 919 char ch; 920 int len; 921 922 len = strlen (string); 923 if (len > 200) 924 len = 200; /* Bigger than most GDB packets, junk??? */ 925 pkt = pack_hex_byte (pkt, len); 926 while (len-- > 0) 927 { 928 ch = *string++; 929 if ((ch == '\0') || (ch == '#')) 930 ch = '*'; /* Protect encapsulation */ 931 *pkt++ = ch; 932 } 933 return pkt; 934} 935#endif /* 0 (unused) */ 936 937static char * 938unpack_string (src, dest, length) 939 char *src; 940 char *dest; 941 int length; 942{ 943 while (length--) 944 *dest++ = *src++; 945 *dest = '\0'; 946 return src; 947} 948 949static char * 950pack_threadid (pkt, id) 951 char *pkt; 952 threadref *id; 953{ 954 char *limit; 955 unsigned char *altid; 956 957 altid = (unsigned char *) id; 958 limit = pkt + BUF_THREAD_ID_SIZE; 959 while (pkt < limit) 960 pkt = pack_hex_byte (pkt, *altid++); 961 return pkt; 962} 963 964 965static char * 966unpack_threadid (inbuf, id) 967 char *inbuf; 968 threadref *id; 969{ 970 char *altref; 971 char *limit = inbuf + BUF_THREAD_ID_SIZE; 972 int x, y; 973 974 altref = (char *) id; 975 976 while (inbuf < limit) 977 { 978 x = stubhex (*inbuf++); 979 y = stubhex (*inbuf++); 980 *altref++ = (x << 4) | y; 981 } 982 return inbuf; 983} 984 985/* Externally, threadrefs are 64 bits but internally, they are still 986 ints. This is due to a mismatch of specifications. We would like 987 to use 64bit thread references internally. This is an adapter 988 function. */ 989 990void 991int_to_threadref (id, value) 992 threadref *id; 993 int value; 994{ 995 unsigned char *scan; 996 997 scan = (unsigned char *) id; 998 { 999 int i = 4; 1000 while (i--) 1001 *scan++ = 0; 1002 } 1003 *scan++ = (value >> 24) & 0xff; 1004 *scan++ = (value >> 16) & 0xff; 1005 *scan++ = (value >> 8) & 0xff; 1006 *scan++ = (value & 0xff); 1007} 1008 1009static int 1010threadref_to_int (ref) 1011 threadref *ref; 1012{ 1013 int i, value = 0; 1014 unsigned char *scan; 1015 1016 scan = (char *) ref; 1017 scan += 4; 1018 i = 4; 1019 while (i-- > 0) 1020 value = (value << 8) | ((*scan++) & 0xff); 1021 return value; 1022} 1023 1024static void 1025copy_threadref (dest, src) 1026 threadref *dest; 1027 threadref *src; 1028{ 1029 int i; 1030 unsigned char *csrc, *cdest; 1031 1032 csrc = (unsigned char *) src; 1033 cdest = (unsigned char *) dest; 1034 i = 8; 1035 while (i--) 1036 *cdest++ = *csrc++; 1037} 1038 1039static int 1040threadmatch (dest, src) 1041 threadref *dest; 1042 threadref *src; 1043{ 1044 /* things are broken right now, so just assume we got a match */ 1045#if 0 1046 unsigned char *srcp, *destp; 1047 int i, result; 1048 srcp = (char *) src; 1049 destp = (char *) dest; 1050 1051 result = 1; 1052 while (i-- > 0) 1053 result &= (*srcp++ == *destp++) ? 1 : 0; 1054 return result; 1055#endif 1056 return 1; 1057} 1058 1059/* 1060 threadid:1, # always request threadid 1061 context_exists:2, 1062 display:4, 1063 unique_name:8, 1064 more_display:16 1065*/ 1066 1067/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ 1068 1069static char * 1070pack_threadinfo_request (pkt, mode, id) 1071 char *pkt; 1072 int mode; 1073 threadref *id; 1074{ 1075 *pkt++ = 'q'; /* Info Query */ 1076 *pkt++ = 'P'; /* process or thread info */ 1077 pkt = pack_int (pkt, mode); /* mode */ 1078 pkt = pack_threadid (pkt, id); /* threadid */ 1079 *pkt = '\0'; /* terminate */ 1080 return pkt; 1081} 1082 1083/* These values tag the fields in a thread info response packet */ 1084/* Tagging the fields allows us to request specific fields and to 1085 add more fields as time goes by */ 1086 1087#define TAG_THREADID 1 /* Echo the thread identifier */ 1088#define TAG_EXISTS 2 /* Is this process defined enough to 1089 fetch registers and its stack */ 1090#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ 1091#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */ 1092#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about 1093 the process*/ 1094 1095static int 1096remote_unpack_thread_info_response (pkt, expectedref, info) 1097 char *pkt; 1098 threadref *expectedref; 1099 struct gdb_ext_thread_info *info; 1100{ 1101 int mask, length; 1102 unsigned int tag; 1103 threadref ref; 1104 char *limit = pkt + PBUFSIZ; /* plausable parsing limit */ 1105 int retval = 1; 1106 1107 /* info->threadid = 0; FIXME: implement zero_threadref */ 1108 info->active = 0; 1109 info->display[0] = '\0'; 1110 info->shortname[0] = '\0'; 1111 info->more_display[0] = '\0'; 1112 1113 /* Assume the characters indicating the packet type have been stripped */ 1114 pkt = unpack_int (pkt, &mask); /* arg mask */ 1115 pkt = unpack_threadid (pkt, &ref); 1116 1117 if (mask == 0) 1118 warning ("Incomplete response to threadinfo request\n"); 1119 if (!threadmatch (&ref, expectedref)) 1120 { /* This is an answer to a different request */ 1121 warning ("ERROR RMT Thread info mismatch\n"); 1122 return 0; 1123 } 1124 copy_threadref (&info->threadid, &ref); 1125 1126 /* Loop on tagged fields , try to bail if somthing goes wrong */ 1127 1128 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */ 1129 { 1130 pkt = unpack_int (pkt, &tag); /* tag */ 1131 pkt = unpack_byte (pkt, &length); /* length */ 1132 if (!(tag & mask)) /* tags out of synch with mask */ 1133 { 1134 warning ("ERROR RMT: threadinfo tag mismatch\n"); 1135 retval = 0; 1136 break; 1137 } 1138 if (tag == TAG_THREADID) 1139 { 1140 if (length != 16) 1141 { 1142 warning ("ERROR RMT: length of threadid is not 16\n"); 1143 retval = 0; 1144 break; 1145 } 1146 pkt = unpack_threadid (pkt, &ref); 1147 mask = mask & ~TAG_THREADID; 1148 continue; 1149 } 1150 if (tag == TAG_EXISTS) 1151 { 1152 info->active = stub_unpack_int (pkt, length); 1153 pkt += length; 1154 mask = mask & ~(TAG_EXISTS); 1155 if (length > 8) 1156 { 1157 warning ("ERROR RMT: 'exists' length too long\n"); 1158 retval = 0; 1159 break; 1160 } 1161 continue; 1162 } 1163 if (tag == TAG_THREADNAME) 1164 { 1165 pkt = unpack_string (pkt, &info->shortname[0], length); 1166 mask = mask & ~TAG_THREADNAME; 1167 continue; 1168 } 1169 if (tag == TAG_DISPLAY) 1170 { 1171 pkt = unpack_string (pkt, &info->display[0], length); 1172 mask = mask & ~TAG_DISPLAY; 1173 continue; 1174 } 1175 if (tag == TAG_MOREDISPLAY) 1176 { 1177 pkt = unpack_string (pkt, &info->more_display[0], length); 1178 mask = mask & ~TAG_MOREDISPLAY; 1179 continue; 1180 } 1181 warning ("ERROR RMT: unknown thread info tag\n"); 1182 break; /* Not a tag we know about */ 1183 } 1184 return retval; 1185} 1186 1187static int 1188remote_get_threadinfo (threadid, fieldset, info) 1189 threadref *threadid; 1190 int fieldset; /* TAG mask */ 1191 struct gdb_ext_thread_info *info; 1192{ 1193 int result; 1194 char threadinfo_pkt[PBUFSIZ]; 1195 1196 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid); 1197 putpkt (threadinfo_pkt); 1198 getpkt (threadinfo_pkt, 0); 1199 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid, 1200 info); 1201 return result; 1202} 1203 1204/* Unfortunately, 61 bit thread-ids are bigger than the internal 1205 representation of a threadid. */ 1206 1207static int 1208adapt_remote_get_threadinfo (ref, selection, info) 1209 gdb_threadref *ref; 1210 int selection; 1211 struct gdb_ext_thread_info *info; 1212{ 1213 threadref lclref; 1214 1215 int_to_threadref (&lclref, *ref); 1216 return remote_get_threadinfo (&lclref, selection, info); 1217} 1218 1219/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ 1220 1221static char * 1222pack_threadlist_request (pkt, startflag, threadcount, nextthread) 1223 char *pkt; 1224 int startflag; 1225 int threadcount; 1226 threadref *nextthread; 1227{ 1228 *pkt++ = 'q'; /* info query packet */ 1229 *pkt++ = 'L'; /* Process LIST or threadLIST request */ 1230 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ 1231 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ 1232 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ 1233 *pkt = '\0'; 1234 return pkt; 1235} 1236 1237/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ 1238 1239static int 1240parse_threadlist_response (pkt, result_limit, original_echo, resultlist, 1241 doneflag) 1242 char *pkt; 1243 int result_limit; 1244 threadref *original_echo; 1245 threadref *resultlist; 1246 int *doneflag; 1247{ 1248 char *limit; 1249 int count, resultcount, done; 1250 1251 resultcount = 0; 1252 /* Assume the 'q' and 'M chars have been stripped. */ 1253 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */ 1254 pkt = unpack_byte (pkt, &count); /* count field */ 1255 pkt = unpack_nibble (pkt, &done); 1256 /* The first threadid is the argument threadid. */ 1257 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ 1258 while ((count-- > 0) && (pkt < limit)) 1259 { 1260 pkt = unpack_threadid (pkt, resultlist++); 1261 if (resultcount++ >= result_limit) 1262 break; 1263 } 1264 if (doneflag) 1265 *doneflag = done; 1266 return resultcount; 1267} 1268 1269static int 1270remote_get_threadlist (startflag, nextthread, result_limit, 1271 done, result_count, threadlist) 1272 int startflag; 1273 threadref *nextthread; 1274 int result_limit; 1275 int *done; 1276 int *result_count; 1277 threadref *threadlist; 1278 1279{ 1280 static threadref echo_nextthread; 1281 char threadlist_packet[PBUFSIZ]; 1282 char t_response[PBUFSIZ]; 1283 int result = 1; 1284 1285 /* Trancate result limit to be smaller than the packet size */ 1286 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ) 1287 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2; 1288 1289 pack_threadlist_request (threadlist_packet, 1290 startflag, result_limit, nextthread); 1291 putpkt (threadlist_packet); 1292 getpkt (t_response, 0); 1293 1294 *result_count = 1295 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread, 1296 threadlist, done); 1297 1298 if (!threadmatch (&echo_nextthread, nextthread)) 1299 { 1300 /* FIXME: This is a good reason to drop the packet */ 1301 /* Possably, there is a duplicate response */ 1302 /* Possabilities : 1303 retransmit immediatly - race conditions 1304 retransmit after timeout - yes 1305 exit 1306 wait for packet, then exit 1307 */ 1308 warning ("HMM: threadlist did not echo arg thread, dropping it\n"); 1309 return 0; /* I choose simply exiting */ 1310 } 1311 if (*result_count <= 0) 1312 { 1313 if (*done != 1) 1314 { 1315 warning ("RMT ERROR : failed to get remote thread list\n"); 1316 result = 0; 1317 } 1318 return result; /* break; */ 1319 } 1320 if (*result_count > result_limit) 1321 { 1322 *result_count = 0; 1323 warning ("RMT ERROR: threadlist response longer than requested\n"); 1324 return 0; 1325 } 1326 return result; 1327} 1328 1329/* This is the interface between remote and threads, remotes upper interface */ 1330 1331/* remote_find_new_threads retrieves the thread list and for each 1332 thread in the list, looks up the thread in GDB's internal list, 1333 ading the thread if it does not already exist. This involves 1334 getting partial thread lists from the remote target so, polling the 1335 quit_flag is required. */ 1336 1337 1338/* About this many threadisds fit in a packet. */ 1339 1340#define MAXTHREADLISTRESULTS 32 1341 1342static int 1343remote_threadlist_iterator (stepfunction, context, looplimit) 1344 rmt_thread_action stepfunction; 1345 void *context; 1346 int looplimit; 1347{ 1348 int done, i, result_count; 1349 int startflag = 1; 1350 int result = 1; 1351 int loopcount = 0; 1352 static threadref nextthread; 1353 static threadref resultthreadlist[MAXTHREADLISTRESULTS]; 1354 1355 done = 0; 1356 while (!done) 1357 { 1358 if (loopcount++ > looplimit) 1359 { 1360 result = 0; 1361 warning ("Remote fetch threadlist -infinite loop-\n"); 1362 break; 1363 } 1364 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, 1365 &done, &result_count, resultthreadlist)) 1366 { 1367 result = 0; 1368 break; 1369 } 1370 /* clear for later iterations */ 1371 startflag = 0; 1372 /* Setup to resume next batch of thread references, set nextthread. */ 1373 if (result_count >= 1) 1374 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); 1375 i = 0; 1376 while (result_count--) 1377 if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) 1378 break; 1379 } 1380 return result; 1381} 1382 1383static int 1384remote_newthread_step (ref, context) 1385 threadref *ref; 1386 void *context; 1387{ 1388 int pid; 1389 1390 pid = threadref_to_int (ref); 1391 if (!in_thread_list (pid)) 1392 add_thread (pid); 1393 return 1; /* continue iterator */ 1394} 1395 1396#define CRAZY_MAX_THREADS 1000 1397 1398static int 1399remote_current_thread (oldpid) 1400 int oldpid; 1401{ 1402 char buf[PBUFSIZ]; 1403 1404 putpkt ("qC"); 1405 getpkt (buf, 0); 1406 if (buf[0] == 'Q' && buf[1] == 'C') 1407 return strtol (&buf[2], NULL, 16); 1408 else 1409 return oldpid; 1410} 1411 1412int 1413remote_find_new_threads () 1414{ 1415 int ret; 1416 1417 ret = remote_threadlist_iterator (remote_newthread_step, 0, 1418 CRAZY_MAX_THREADS); 1419 if (inferior_pid == MAGIC_NULL_PID) /* ack ack ack */ 1420 inferior_pid = remote_current_thread (inferior_pid); 1421 return ret; 1422} 1423 1424/* Initialize the thread vector which is used by threads.c */ 1425/* The thread stub is a package, it has an initializer */ 1426 1427static void 1428init_remote_threads () 1429{ 1430 remote_thread_vec.find_new_threads = remote_find_new_threads; 1431 remote_thread_vec.get_thread_info = adapt_remote_get_threadinfo; 1432} 1433 1434 1435/* Restart the remote side; this is an extended protocol operation. */ 1436 1437static void 1438extended_remote_restart () 1439{ 1440 char buf[PBUFSIZ]; 1441 1442 /* Send the restart command; for reasons I don't understand the 1443 remote side really expects a number after the "R". */ 1444 buf[0] = 'R'; 1445 sprintf (&buf[1], "%x", 0); 1446 putpkt (buf); 1447 1448 /* Now query for status so this looks just like we restarted 1449 gdbserver from scratch. */ 1450 putpkt ("?"); 1451 getpkt (buf, 0); 1452} 1453 1454/* Clean up connection to a remote debugger. */ 1455 1456/* ARGSUSED */ 1457static void 1458remote_close (quitting) 1459 int quitting; 1460{ 1461 if (remote_desc) 1462 SERIAL_CLOSE (remote_desc); 1463 remote_desc = NULL; 1464} 1465 1466/* Query the remote side for the text, data and bss offsets. */ 1467 1468static void 1469get_offsets () 1470{ 1471 char buf[PBUFSIZ], *ptr; 1472 int lose; 1473 CORE_ADDR text_addr, data_addr, bss_addr; 1474 struct section_offsets *offs; 1475 1476#ifdef REMOTE_CHAT 1477 if (remote_chat) 1478 (void) remote_talk(); 1479#endif /* REMOTE_CHAT */ 1480 1481 putpkt ("qOffsets"); 1482 1483 getpkt (buf, 0); 1484 1485 if (buf[0] == '\000') 1486 return; /* Return silently. Stub doesn't support 1487 this command. */ 1488 if (buf[0] == 'E') 1489 { 1490 warning ("Remote failure reply: %s", buf); 1491 return; 1492 } 1493 1494 /* Pick up each field in turn. This used to be done with scanf, but 1495 scanf will make trouble if CORE_ADDR size doesn't match 1496 conversion directives correctly. The following code will work 1497 with any size of CORE_ADDR. */ 1498 text_addr = data_addr = bss_addr = 0; 1499 ptr = buf; 1500 lose = 0; 1501 1502 if (strncmp (ptr, "Text=", 5) == 0) 1503 { 1504 ptr += 5; 1505 /* Don't use strtol, could lose on big values. */ 1506 while (*ptr && *ptr != ';') 1507 text_addr = (text_addr << 4) + fromhex (*ptr++); 1508 } 1509 else 1510 lose = 1; 1511 1512 if (!lose && strncmp (ptr, ";Data=", 6) == 0) 1513 { 1514 ptr += 6; 1515 while (*ptr && *ptr != ';') 1516 data_addr = (data_addr << 4) + fromhex (*ptr++); 1517 } 1518 else 1519 lose = 1; 1520 1521 if (!lose && strncmp (ptr, ";Bss=", 5) == 0) 1522 { 1523 ptr += 5; 1524 while (*ptr && *ptr != ';') 1525 bss_addr = (bss_addr << 4) + fromhex (*ptr++); 1526 } 1527 else 1528 lose = 1; 1529 1530 if (lose) 1531 error ("Malformed response to offset query, %s", buf); 1532 1533 if (symfile_objfile == NULL) 1534 return; 1535 1536 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) 1537 + symfile_objfile->num_sections 1538 * sizeof (offs->offsets)); 1539 memcpy (offs, symfile_objfile->section_offsets, 1540 sizeof (struct section_offsets) 1541 + symfile_objfile->num_sections 1542 * sizeof (offs->offsets)); 1543 1544 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; 1545 1546 /* This is a temporary kludge to force data and bss to use the same offsets 1547 because that's what nlmconv does now. The real solution requires changes 1548 to the stub and remote.c that I don't have time to do right now. */ 1549 1550 ANOFFSET (offs, SECT_OFF_DATA) = data_addr; 1551 ANOFFSET (offs, SECT_OFF_BSS) = data_addr; 1552 1553 objfile_relocate (symfile_objfile, offs); 1554} 1555 1556/* Stub for catch_errors. */ 1557 1558static int 1559remote_start_remote (dummy) 1560 PTR dummy; 1561{ 1562 immediate_quit = 1; /* Allow user to interrupt it */ 1563 1564 /* Ack any packet which the remote side has already sent. */ 1565 SERIAL_WRITE (remote_desc, "+", 1); 1566 1567 /* Let the stub know that we want it to return the thread. */ 1568 set_thread (-1, 0); 1569 1570 inferior_pid = remote_current_thread (inferior_pid); 1571 1572 get_offsets (); /* Get text, data & bss offsets */ 1573 1574 putpkt ("?"); /* initiate a query from remote machine */ 1575 immediate_quit = 0; 1576 1577 start_remote (); /* Initialize gdb process mechanisms */ 1578 return 1; 1579} 1580 1581/* Open a connection to a remote debugger. 1582 NAME is the filename used for communication. */ 1583 1584static void 1585remote_open (name, from_tty) 1586 char *name; 1587 int from_tty; 1588{ 1589 remote_open_1 (name, from_tty, &remote_ops, 0); 1590} 1591 1592/* Open a connection to a remote debugger using the extended 1593 remote gdb protocol. NAME is the filename used for communication. */ 1594 1595static void 1596extended_remote_open (name, from_tty) 1597 char *name; 1598 int from_tty; 1599{ 1600 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/); 1601} 1602 1603/* Generic code for opening a connection to a remote target. */ 1604 1605static DCACHE *remote_dcache; 1606 1607static void 1608remote_open_1 (name, from_tty, target, extended_p) 1609 char *name; 1610 int from_tty; 1611 struct target_ops *target; 1612 int extended_p; 1613{ 1614 if (name == 0) 1615 error ("To open a remote debug connection, you need to specify what\n\ 1616serial device is attached to the remote system (e.g. /dev/ttya)."); 1617 1618 target_preopen (from_tty); 1619 1620 unpush_target (target); 1621 1622 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); 1623 1624 remote_desc = SERIAL_OPEN (name); 1625 if (!remote_desc) 1626 perror_with_name (name); 1627 1628 if (baud_rate != -1) 1629 { 1630 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) 1631 { 1632 SERIAL_CLOSE (remote_desc); 1633 perror_with_name (name); 1634 } 1635 } 1636 1637 1638 SERIAL_RAW (remote_desc); 1639 1640 /* If there is something sitting in the buffer we might take it as a 1641 response to a command, which would be bad. */ 1642 SERIAL_FLUSH_INPUT (remote_desc); 1643 1644 if (from_tty) 1645 { 1646 puts_filtered ("Remote debugging using "); 1647 puts_filtered (name); 1648 puts_filtered ("\n"); 1649 } 1650 push_target (target); /* Switch to using remote target now */ 1651 1652 /* The target vector does not have the thread functions in it yet, 1653 so we use this function to call back into the thread module and 1654 register the thread vector and its contained functions. */ 1655 bind_target_thread_vector (&remote_thread_vec); 1656 1657 /* Start out by trying the 'P' request to set registers. We set 1658 this each time that we open a new target so that if the user 1659 switches from one stub to another, we can (if the target is 1660 closed and reopened) cope. */ 1661 stub_supports_P = 1; 1662 1663 general_thread = -2; 1664 cont_thread = -2; 1665 1666 /* Force remote_write_bytes to check whether target supports 1667 binary downloading. */ 1668 remote_binary_checked = 0; 1669 1670 /* Without this, some commands which require an active target (such 1671 as kill) won't work. This variable serves (at least) double duty 1672 as both the pid of the target process (if it has such), and as a 1673 flag indicating that a target is active. These functions should 1674 be split out into seperate variables, especially since GDB will 1675 someday have a notion of debugging several processes. */ 1676 1677 inferior_pid = MAGIC_NULL_PID; 1678 /* Start the remote connection; if error (0), discard this target. 1679 In particular, if the user quits, be sure to discard it 1680 (we'd be in an inconsistent state otherwise). */ 1681 if (!catch_errors (remote_start_remote, NULL, 1682 "Couldn't establish connection to remote target\n", 1683 RETURN_MASK_ALL)) 1684 { 1685 pop_target (); 1686 return; 1687 } 1688 1689 if (extended_p) 1690 { 1691 /* tell the remote that we're using the extended protocol. */ 1692 char buf[PBUFSIZ]; 1693 putpkt ("!"); 1694 getpkt (buf, 0); 1695 } 1696} 1697 1698/* This takes a program previously attached to and detaches it. After 1699 this is done, GDB can be used to debug some other program. We 1700 better not have left any breakpoints in the target program or it'll 1701 die when it hits one. */ 1702 1703static void 1704remote_detach (args, from_tty) 1705 char *args; 1706 int from_tty; 1707{ 1708 char buf[PBUFSIZ]; 1709 1710 if (args) 1711 error ("Argument given to \"detach\" when remotely debugging."); 1712 1713 /* Tell the remote target to detach. */ 1714 strcpy (buf, "D"); 1715 remote_send (buf); 1716 1717 pop_target (); 1718 if (from_tty) 1719 puts_filtered ("Ending remote debugging.\n"); 1720} 1721 1722/* Convert hex digit A to a number. */ 1723 1724int 1725fromhex (a) 1726 int a; 1727{ 1728 if (a >= '0' && a <= '9') 1729 return a - '0'; 1730 else if (a >= 'a' && a <= 'f') 1731 return a - 'a' + 10; 1732 else if (a >= 'A' && a <= 'F') 1733 return a - 'A' + 10; 1734 else 1735 error ("Reply contains invalid hex digit %d", a); 1736} 1737 1738/* Convert number NIB to a hex digit. */ 1739 1740static int 1741tohex (nib) 1742 int nib; 1743{ 1744 if (nib < 10) 1745 return '0'+nib; 1746 else 1747 return 'a'+nib-10; 1748} 1749 1750/* Tell the remote machine to resume. */ 1751 1752static enum target_signal last_sent_signal = TARGET_SIGNAL_0; 1753 1754static int last_sent_step; 1755 1756static void 1757remote_resume (pid, step, siggnal) 1758 int pid, step; 1759 enum target_signal siggnal; 1760{ 1761 char buf[PBUFSIZ]; 1762 1763 if (pid == -1) 1764 set_thread (0, 0); /* run any thread */ 1765 else 1766 set_thread (pid, 0); /* run this thread */ 1767 1768 dcache_flush (remote_dcache); 1769 1770 last_sent_signal = siggnal; 1771 last_sent_step = step; 1772 1773 /* A hook for when we need to do something at the last moment before 1774 resumption. */ 1775 if (target_resume_hook) 1776 (*target_resume_hook) (); 1777 1778 if (siggnal != TARGET_SIGNAL_0) 1779 { 1780 buf[0] = step ? 'S' : 'C'; 1781 buf[1] = tohex (((int)siggnal >> 4) & 0xf); 1782 buf[2] = tohex ((int)siggnal & 0xf); 1783 buf[3] = '\0'; 1784 } 1785 else 1786 strcpy (buf, step ? "s": "c"); 1787 1788 putpkt (buf); 1789} 1790 1791/* Send ^C to target to halt it. Target will respond, and send us a 1792 packet. */ 1793 1794static void (*ofunc) PARAMS ((int)); 1795 1796static void 1797remote_interrupt (signo) 1798 int signo; 1799{ 1800 remote_stop (); 1801 signal (signo, remote_interrupt); 1802} 1803 1804static void 1805remote_stop () 1806{ 1807 if (!interrupted_already) 1808 { 1809 /* Send a break or a ^C, depending on user preference. */ 1810 interrupted_already = 1; 1811 1812 if (remote_debug) 1813 printf_unfiltered ("remote_stop called\n"); 1814 1815 if (remote_break) 1816 SERIAL_SEND_BREAK (remote_desc); 1817 else 1818 SERIAL_WRITE (remote_desc, "\003", 1); 1819 } 1820 else 1821 { 1822 signal (SIGINT, ofunc); 1823 interrupt_query (); 1824 signal (SIGINT, remote_interrupt); 1825 interrupted_already = 0; 1826 } 1827} 1828 1829/* Ask the user what to do when an interrupt is received. */ 1830 1831static void 1832interrupt_query () 1833{ 1834 target_terminal_ours (); 1835 1836 if (query ("Interrupted while waiting for the program.\n\ 1837Give up (and stop debugging it)? ")) 1838 { 1839 target_mourn_inferior (); 1840 return_to_top_level (RETURN_QUIT); 1841 } 1842 1843 target_terminal_inferior (); 1844} 1845 1846/* If nonzero, ignore the next kill. */ 1847 1848int kill_kludge; 1849 1850void 1851remote_console_output (msg) 1852 char *msg; 1853{ 1854 char *p; 1855 1856 for (p = msg; *p; p +=2) 1857 { 1858 char tb[2]; 1859 char c = fromhex (p[0]) * 16 + fromhex (p[1]); 1860 tb[0] = c; 1861 tb[1] = 0; 1862 if (target_output_hook) 1863 target_output_hook (tb); 1864 else 1865 fputs_filtered (tb, gdb_stdout); 1866 } 1867} 1868 1869/* Wait until the remote machine stops, then return, storing status in 1870 STATUS just as `wait' would. Returns "pid" (though it's not clear 1871 what, if anything, that means in the case of this target). */ 1872 1873static int 1874remote_wait (pid, status) 1875 int pid; 1876 struct target_waitstatus *status; 1877{ 1878 unsigned char buf[PBUFSIZ]; 1879 int thread_num = -1; 1880 1881 status->kind = TARGET_WAITKIND_EXITED; 1882 status->value.integer = 0; 1883 1884 while (1) 1885 { 1886 unsigned char *p; 1887 1888#ifdef REMOTE_CHAT 1889 if (remote_chat) 1890 (void) remote_talk(); 1891#endif /* REMOTE_CHAT */ 1892 1893 interrupted_already = 0; 1894 ofunc = signal (SIGINT, remote_interrupt); 1895 getpkt ((char *) buf, 1); 1896 signal (SIGINT, ofunc); 1897 1898 /* This is a hook for when we need to do something (perhaps the 1899 collection of trace data) every time the target stops. */ 1900 if (target_wait_loop_hook) 1901 (*target_wait_loop_hook) (); 1902 1903 switch (buf[0]) 1904 { 1905 case 'E': /* Error of some sort */ 1906 warning ("Remote failure reply: %s", buf); 1907 continue; 1908 case 'T': /* Status with PC, SP, FP, ... */ 1909 { 1910 int i; 1911 long regno; 1912 char regs[MAX_REGISTER_RAW_SIZE]; 1913 1914 /* Expedited reply, containing Signal, {regno, reg} repeat */ 1915 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where 1916 ss = signal number 1917 n... = register number 1918 r... = register contents 1919 */ 1920 p = &buf[3]; /* after Txx */ 1921 1922 while (*p) 1923 { 1924 unsigned char *p1; 1925 char *p_temp; 1926 1927 /* Read the register number */ 1928 regno = strtol ((const char *) p, &p_temp, 16); 1929 p1 = (unsigned char *)p_temp; 1930 1931 if (p1 == p) /* No register number present here */ 1932 { 1933 p1 = (unsigned char *) strchr ((const char *) p, ':'); 1934 if (p1 == NULL) 1935 warning ("Malformed packet(a) (missing colon): %s\n\ 1936Packet: '%s'\n", 1937 p, buf); 1938 if (strncmp ((const char *) p, "thread", p1 - p) == 0) 1939 { 1940 p_temp = unpack_varlen_hex (++p1, &thread_num); 1941 record_currthread (thread_num); 1942 p = (unsigned char *) p_temp; 1943 } 1944 } 1945 else 1946 { 1947 p = p1; 1948 1949 if (*p++ != ':') 1950 warning ("Malformed packet(b) (missing colon): %s\n\ 1951Packet: '%s'\n", 1952 p, buf); 1953 1954 if (regno >= NUM_REGS) 1955 warning ("Remote sent bad register number %ld: %s\n\ 1956Packet: '%s'\n", 1957 regno, p, buf); 1958 1959 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) 1960 { 1961 if (p[0] == 0 || p[1] == 0) 1962 warning ("Remote reply is too short: %s", buf); 1963 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); 1964 p += 2; 1965 } 1966 supply_register (regno, regs); 1967 } 1968 1969 if (*p++ != ';') 1970 { 1971 warning ("Remote register badly formatted: %s", buf); 1972 warning (" here: %s",p); 1973 } 1974 } 1975 } 1976 /* fall through */ 1977 case 'S': /* Old style status, just signal only */ 1978 status->kind = TARGET_WAITKIND_STOPPED; 1979 status->value.sig = (enum target_signal) 1980 (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); 1981 1982 goto got_status; 1983 case 'W': /* Target exited */ 1984 { 1985 /* The remote process exited. */ 1986 status->kind = TARGET_WAITKIND_EXITED; 1987 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); 1988 goto got_status; 1989 } 1990 case 'X': 1991 status->kind = TARGET_WAITKIND_SIGNALLED; 1992 status->value.sig = (enum target_signal) 1993 (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); 1994 kill_kludge = 1; 1995 1996 goto got_status; 1997 case 'O': /* Console output */ 1998 remote_console_output (buf + 1); 1999 continue; 2000 case '\0': 2001 if (last_sent_signal != TARGET_SIGNAL_0) 2002 { 2003 /* Zero length reply means that we tried 'S' or 'C' and 2004 the remote system doesn't support it. */ 2005 target_terminal_ours_for_output (); 2006 printf_filtered 2007 ("Can't send signals to this remote system. %s not sent.\n", 2008 target_signal_to_name (last_sent_signal)); 2009 last_sent_signal = TARGET_SIGNAL_0; 2010 target_terminal_inferior (); 2011 2012 strcpy ((char *) buf, last_sent_step ? "s" : "c"); 2013 putpkt ((char *) buf); 2014 continue; 2015 } 2016 /* else fallthrough */ 2017 default: 2018 warning ("Invalid remote reply: %s", buf); 2019 continue; 2020 } 2021 } 2022 got_status: 2023 if (thread_num != -1) 2024 { 2025 /* Initial thread value can only be acquired via wait, so deal with 2026 this marker which is used before the first thread value is 2027 acquired. */ 2028 if (inferior_pid == MAGIC_NULL_PID) 2029 { 2030 inferior_pid = thread_num; 2031 if (!in_thread_list (inferior_pid)) 2032 add_thread (inferior_pid); 2033 } 2034 return thread_num; 2035 } 2036 return inferior_pid; 2037} 2038 2039/* Number of bytes of registers this stub implements. */ 2040 2041static int register_bytes_found; 2042 2043/* Read the remote registers into the block REGS. */ 2044/* Currently we just read all the registers, so we don't use regno. */ 2045 2046/* ARGSUSED */ 2047static void 2048remote_fetch_registers (regno) 2049 int regno; 2050{ 2051 char buf[PBUFSIZ]; 2052 int i; 2053 char *p; 2054 char regs[REGISTER_BYTES]; 2055 2056 set_thread (inferior_pid, 1); 2057 2058 sprintf (buf, "g"); 2059 remote_send (buf); 2060 2061 if (remote_register_buf_size == 0) 2062 remote_register_buf_size = strlen (buf); 2063 2064 /* Unimplemented registers read as all bits zero. */ 2065 memset (regs, 0, REGISTER_BYTES); 2066 2067 /* We can get out of synch in various cases. If the first character 2068 in the buffer is not a hex character, assume that has happened 2069 and try to fetch another packet to read. */ 2070 while ((buf[0] < '0' || buf[0] > '9') 2071 && (buf[0] < 'a' || buf[0] > 'f') 2072 && buf[0] != 'x') /* New: unavailable register value */ 2073 { 2074 if (remote_debug) 2075 printf_unfiltered ("Bad register packet; fetching a new packet\n"); 2076 getpkt (buf, 0); 2077 } 2078 2079 /* Reply describes registers byte by byte, each byte encoded as two 2080 hex characters. Suck them all up, then supply them to the 2081 register cacheing/storage mechanism. */ 2082 2083 p = buf; 2084 for (i = 0; i < REGISTER_BYTES; i++) 2085 { 2086 if (p[0] == 0) 2087 break; 2088 if (p[1] == 0) 2089 { 2090 warning ("Remote reply is of odd length: %s", buf); 2091 /* Don't change register_bytes_found in this case, and don't 2092 print a second warning. */ 2093 goto supply_them; 2094 } 2095 if (p[0] == 'x' && p[1] == 'x') 2096 regs[i] = 0; /* 'x' */ 2097 else 2098 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); 2099 p += 2; 2100 } 2101 2102 if (i != register_bytes_found) 2103 { 2104 register_bytes_found = i; 2105#ifdef REGISTER_BYTES_OK 2106 if (!REGISTER_BYTES_OK (i)) 2107 warning ("Remote reply is too short: %s", buf); 2108#endif 2109 } 2110 2111 supply_them: 2112 for (i = 0; i < NUM_REGS; i++) 2113 { 2114 supply_register (i, ®s[REGISTER_BYTE(i)]); 2115 if (buf[REGISTER_BYTE(i) * 2] == 'x') 2116 register_valid[i] = -1; /* register value not available */ 2117 } 2118} 2119 2120/* Prepare to store registers. Since we may send them all (using a 2121 'G' request), we have to read out the ones we don't want to change 2122 first. */ 2123 2124static void 2125remote_prepare_to_store () 2126{ 2127 /* Make sure the entire registers array is valid. */ 2128 read_register_bytes (0, (char *)NULL, REGISTER_BYTES); 2129} 2130 2131/* Store register REGNO, or all registers if REGNO == -1, from the contents 2132 of REGISTERS. FIXME: ignores errors. */ 2133 2134static void 2135remote_store_registers (regno) 2136 int regno; 2137{ 2138 char buf[PBUFSIZ]; 2139 int i; 2140 char *p; 2141 2142 set_thread (inferior_pid, 1); 2143 2144 if (regno >= 0 && stub_supports_P) 2145 { 2146 /* Try storing a single register. */ 2147 char *regp; 2148 2149 sprintf (buf, "P%x=", regno); 2150 p = buf + strlen (buf); 2151 regp = ®isters[REGISTER_BYTE (regno)]; 2152 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) 2153 { 2154 *p++ = tohex ((regp[i] >> 4) & 0xf); 2155 *p++ = tohex (regp[i] & 0xf); 2156 } 2157 *p = '\0'; 2158 remote_send (buf); 2159 if (buf[0] != '\0') 2160 { 2161 /* The stub understands the 'P' request. We are done. */ 2162 return; 2163 } 2164 2165 /* The stub does not support the 'P' request. Use 'G' instead, 2166 and don't try using 'P' in the future (it will just waste our 2167 time). */ 2168 stub_supports_P = 0; 2169 } 2170 2171 buf[0] = 'G'; 2172 2173 /* Command describes registers byte by byte, 2174 each byte encoded as two hex characters. */ 2175 2176 p = buf + 1; 2177 /* remote_prepare_to_store insures that register_bytes_found gets set. */ 2178 for (i = 0; i < register_bytes_found; i++) 2179 { 2180 *p++ = tohex ((registers[i] >> 4) & 0xf); 2181 *p++ = tohex (registers[i] & 0xf); 2182 } 2183 *p = '\0'; 2184 2185 remote_send (buf); 2186} 2187 2188/* Use of the data cache *used* to be disabled because it loses for looking 2189 at and changing hardware I/O ports and the like. Accepting `volatile' 2190 would perhaps be one way to fix it. Another idea would be to use the 2191 executable file for the text segment (for all SEC_CODE sections? 2192 For all SEC_READONLY sections?). This has problems if you want to 2193 actually see what the memory contains (e.g. self-modifying code, 2194 clobbered memory, user downloaded the wrong thing). 2195 2196 Because it speeds so much up, it's now enabled, if you're playing 2197 with registers you turn it of (set remotecache 0). */ 2198 2199/* Read a word from remote address ADDR and return it. 2200 This goes through the data cache. */ 2201 2202#if 0 /* unused? */ 2203static int 2204remote_fetch_word (addr) 2205 CORE_ADDR addr; 2206{ 2207 return dcache_fetch (remote_dcache, addr); 2208} 2209 2210/* Write a word WORD into remote address ADDR. 2211 This goes through the data cache. */ 2212 2213static void 2214remote_store_word (addr, word) 2215 CORE_ADDR addr; 2216 int word; 2217{ 2218 dcache_poke (remote_dcache, addr, word); 2219} 2220#endif /* 0 (unused?) */ 2221 2222 2223 2224/* Return the number of hex digits in num. */ 2225 2226static int 2227hexnumlen (num) 2228 ULONGEST num; 2229{ 2230 int i; 2231 2232 for (i = 0; num != 0; i++) 2233 num >>= 4; 2234 2235 return max (i, 1); 2236} 2237 2238/* Set BUF to the hex digits representing NUM. */ 2239 2240static int 2241hexnumstr (buf, num) 2242 char *buf; 2243 ULONGEST num; 2244{ 2245 int i; 2246 int len = hexnumlen (num); 2247 2248 buf[len] = '\0'; 2249 2250 for (i = len - 1; i >= 0; i--) 2251 { 2252 buf[i] = "0123456789abcdef" [(num & 0xf)]; 2253 num >>= 4; 2254 } 2255 2256 return len; 2257} 2258 2259/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ 2260 2261static CORE_ADDR 2262remote_address_masked (addr) 2263 CORE_ADDR addr; 2264{ 2265 if (remote_address_size > 0 2266 && remote_address_size < (sizeof (ULONGEST) * 8)) 2267 { 2268 /* Only create a mask when that mask can safely be constructed 2269 in a ULONGEST variable. */ 2270 ULONGEST mask = 1; 2271 mask = (mask << remote_address_size) - 1; 2272 addr &= mask; 2273 } 2274 return addr; 2275} 2276 2277/* Determine whether the remote target supports binary downloading. 2278 This is accomplished by sending a no-op memory write of zero length 2279 to the target at the specified address. It does not suffice to send 2280 the whole packet, since many stubs strip the eighth bit and subsequently 2281 compute a wrong checksum, which causes real havoc with remote_write_bytes. */ 2282static void 2283check_binary_download (addr) 2284 CORE_ADDR addr; 2285{ 2286 if (remote_binary_download && !remote_binary_checked) 2287 { 2288 char buf[PBUFSIZ], *p; 2289 remote_binary_checked = 1; 2290 2291 p = buf; 2292 *p++ = 'X'; 2293 p += hexnumstr (p, (ULONGEST) addr); 2294 *p++ = ','; 2295 p += hexnumstr (p, (ULONGEST) 0); 2296 *p++ = ':'; 2297 *p = '\0'; 2298 2299 putpkt_binary (buf, (int) (p - buf)); 2300 getpkt (buf, 0); 2301 2302 if (buf[0] == '\0') 2303 remote_binary_download = 0; 2304 } 2305 2306 if (remote_debug) 2307 { 2308 if (remote_binary_download) 2309 printf_unfiltered ("binary downloading suppported by target\n"); 2310 else 2311 printf_unfiltered ("binary downloading NOT suppported by target\n"); 2312 } 2313} 2314 2315/* Write memory data directly to the remote machine. 2316 This does not inform the data cache; the data cache uses this. 2317 MEMADDR is the address in the remote memory space. 2318 MYADDR is the address of the buffer in our space. 2319 LEN is the number of bytes. 2320 2321 Returns number of bytes transferred, or 0 for error. */ 2322 2323static int 2324remote_write_bytes (memaddr, myaddr, len) 2325 CORE_ADDR memaddr; 2326 char *myaddr; 2327 int len; 2328{ 2329 int max_buf_size; /* Max size of packet output buffer */ 2330 int origlen; 2331 2332 /* Verify that the target can support a binary download */ 2333 check_binary_download (memaddr); 2334 2335 /* Chop the transfer down if necessary */ 2336 2337 max_buf_size = min (remote_write_size, PBUFSIZ); 2338 if (remote_register_buf_size != 0) 2339 max_buf_size = min (max_buf_size, remote_register_buf_size); 2340 2341 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */ 2342 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4; 2343 2344 origlen = len; 2345 while (len > 0) 2346 { 2347 unsigned char buf[PBUFSIZ]; 2348 unsigned char *p, *plen; 2349 int todo; 2350 int i; 2351 2352 /* construct "M"<memaddr>","<len>":" */ 2353 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */ 2354 memaddr = remote_address_masked (memaddr); 2355 p = buf; 2356 if (remote_binary_download) 2357 { 2358 *p++ = 'X'; 2359 todo = min (len, max_buf_size); 2360 } 2361 else 2362 { 2363 *p++ = 'M'; 2364 todo = min (len, max_buf_size / 2); /* num bytes that will fit */ 2365 } 2366 2367 p += hexnumstr (p, (ULONGEST) memaddr); 2368 *p++ = ','; 2369 2370 plen = p; /* remember where len field goes */ 2371 p += hexnumstr (p, (ULONGEST) todo); 2372 *p++ = ':'; 2373 *p = '\0'; 2374 2375 /* We send target system values byte by byte, in increasing byte 2376 addresses, each byte encoded as two hex characters (or one 2377 binary character). */ 2378 if (remote_binary_download) 2379 { 2380 int escaped = 0; 2381 for (i = 0; 2382 (i < todo) && (i + escaped) < (max_buf_size - 2); 2383 i++) 2384 { 2385 switch (myaddr[i] & 0xff) 2386 { 2387 case '$': 2388 case '#': 2389 case 0x7d: 2390 /* These must be escaped */ 2391 escaped++; 2392 *p++ = 0x7d; 2393 *p++ = (myaddr[i] & 0xff) ^ 0x20; 2394 break; 2395 default: 2396 *p++ = myaddr[i] & 0xff; 2397 break; 2398 } 2399 } 2400 2401 if (i < todo) 2402 { 2403 /* Escape chars have filled up the buffer prematurely, 2404 and we have actually sent fewer bytes than planned. 2405 Fix-up the length field of the packet. */ 2406 2407 /* FIXME: will fail if new len is a shorter string than 2408 old len. */ 2409 2410 plen += hexnumstr (plen, (ULONGEST) i); 2411 *plen++ = ':'; 2412 } 2413 } 2414 else 2415 { 2416 for (i = 0; i < todo; i++) 2417 { 2418 *p++ = tohex ((myaddr[i] >> 4) & 0xf); 2419 *p++ = tohex (myaddr[i] & 0xf); 2420 } 2421 *p = '\0'; 2422 } 2423 2424 putpkt_binary (buf, (int) (p - buf)); 2425 getpkt (buf, 0); 2426 2427 if (buf[0] == 'E') 2428 { 2429 /* There is no correspondance between what the remote protocol uses 2430 for errors and errno codes. We would like a cleaner way of 2431 representing errors (big enough to include errno codes, bfd_error 2432 codes, and others). But for now just return EIO. */ 2433 errno = EIO; 2434 return 0; 2435 } 2436 2437 /* Increment by i, not by todo, in case escape chars 2438 caused us to send fewer bytes than we'd planned. */ 2439 myaddr += i; 2440 memaddr += i; 2441 len -= i; 2442 } 2443 return origlen; 2444} 2445 2446/* Read memory data directly from the remote machine. 2447 This does not use the data cache; the data cache uses this. 2448 MEMADDR is the address in the remote memory space. 2449 MYADDR is the address of the buffer in our space. 2450 LEN is the number of bytes. 2451 2452 Returns number of bytes transferred, or 0 for error. */ 2453 2454static int 2455remote_read_bytes (memaddr, myaddr, len) 2456 CORE_ADDR memaddr; 2457 char *myaddr; 2458 int len; 2459{ 2460 int max_buf_size; /* Max size of packet output buffer */ 2461 int origlen; 2462 2463 /* Chop the transfer down if necessary */ 2464 2465 max_buf_size = min (remote_write_size, PBUFSIZ); 2466 if (remote_register_buf_size != 0) 2467 max_buf_size = min (max_buf_size, remote_register_buf_size); 2468 2469 origlen = len; 2470 while (len > 0) 2471 { 2472 char buf[PBUFSIZ]; 2473 char *p; 2474 int todo; 2475 int i; 2476 2477 todo = min (len, max_buf_size / 2); /* num bytes that will fit */ 2478 2479 /* construct "m"<memaddr>","<len>" */ 2480 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */ 2481 memaddr = remote_address_masked (memaddr); 2482 p = buf; 2483 *p++ = 'm'; 2484 p += hexnumstr (p, (ULONGEST) memaddr); 2485 *p++ = ','; 2486 p += hexnumstr (p, (ULONGEST) todo); 2487 *p = '\0'; 2488 2489 putpkt (buf); 2490 getpkt (buf, 0); 2491 2492 if (buf[0] == 'E') 2493 { 2494 /* There is no correspondance between what the remote protocol uses 2495 for errors and errno codes. We would like a cleaner way of 2496 representing errors (big enough to include errno codes, bfd_error 2497 codes, and others). But for now just return EIO. */ 2498 errno = EIO; 2499 return 0; 2500 } 2501 2502 /* Reply describes memory byte by byte, 2503 each byte encoded as two hex characters. */ 2504 2505 p = buf; 2506 for (i = 0; i < todo; i++) 2507 { 2508 if (p[0] == 0 || p[1] == 0) 2509 /* Reply is short. This means that we were able to read 2510 only part of what we wanted to. */ 2511 return i + (origlen - len); 2512 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); 2513 p += 2; 2514 } 2515 myaddr += todo; 2516 memaddr += todo; 2517 len -= todo; 2518 } 2519 return origlen; 2520} 2521 2522/* Read or write LEN bytes from inferior memory at MEMADDR, 2523 transferring to or from debugger address MYADDR. Write to inferior 2524 if SHOULD_WRITE is nonzero. Returns length of data written or 2525 read; 0 for error. */ 2526 2527/* ARGSUSED */ 2528static int 2529remote_xfer_memory (memaddr, myaddr, len, should_write, target) 2530 CORE_ADDR memaddr; 2531 char *myaddr; 2532 int len; 2533 int should_write; 2534 struct target_ops *target; /* ignored */ 2535{ 2536#ifdef REMOTE_TRANSLATE_XFER_ADDRESS 2537 CORE_ADDR targaddr; 2538 int targlen; 2539 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen); 2540 if (targlen == 0) 2541 return 0; 2542 memaddr = targaddr; 2543 len = targlen; 2544#endif 2545 2546 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, 2547 len, should_write); 2548} 2549 2550 2551#if 0 2552/* Enable after 4.12. */ 2553 2554void 2555remote_search (len, data, mask, startaddr, increment, lorange, hirange 2556 addr_found, data_found) 2557 int len; 2558 char *data; 2559 char *mask; 2560 CORE_ADDR startaddr; 2561 int increment; 2562 CORE_ADDR lorange; 2563 CORE_ADDR hirange; 2564 CORE_ADDR *addr_found; 2565 char *data_found; 2566{ 2567 if (increment == -4 && len == 4) 2568 { 2569 long mask_long, data_long; 2570 long data_found_long; 2571 CORE_ADDR addr_we_found; 2572 char buf[PBUFSIZ]; 2573 long returned_long[2]; 2574 char *p; 2575 2576 mask_long = extract_unsigned_integer (mask, len); 2577 data_long = extract_unsigned_integer (data, len); 2578 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); 2579 putpkt (buf); 2580 getpkt (buf, 0); 2581 if (buf[0] == '\0') 2582 { 2583 /* The stub doesn't support the 't' request. We might want to 2584 remember this fact, but on the other hand the stub could be 2585 switched on us. Maybe we should remember it only until 2586 the next "target remote". */ 2587 generic_search (len, data, mask, startaddr, increment, lorange, 2588 hirange, addr_found, data_found); 2589 return; 2590 } 2591 2592 if (buf[0] == 'E') 2593 /* There is no correspondance between what the remote protocol uses 2594 for errors and errno codes. We would like a cleaner way of 2595 representing errors (big enough to include errno codes, bfd_error 2596 codes, and others). But for now just use EIO. */ 2597 memory_error (EIO, startaddr); 2598 p = buf; 2599 addr_we_found = 0; 2600 while (*p != '\0' && *p != ',') 2601 addr_we_found = (addr_we_found << 4) + fromhex (*p++); 2602 if (*p == '\0') 2603 error ("Protocol error: short return for search"); 2604 2605 data_found_long = 0; 2606 while (*p != '\0' && *p != ',') 2607 data_found_long = (data_found_long << 4) + fromhex (*p++); 2608 /* Ignore anything after this comma, for future extensions. */ 2609 2610 if (addr_we_found < lorange || addr_we_found >= hirange) 2611 { 2612 *addr_found = 0; 2613 return; 2614 } 2615 2616 *addr_found = addr_we_found; 2617 *data_found = store_unsigned_integer (data_we_found, len); 2618 return; 2619 } 2620 generic_search (len, data, mask, startaddr, increment, lorange, 2621 hirange, addr_found, data_found); 2622} 2623#endif /* 0 */ 2624 2625static void 2626remote_files_info (ignore) 2627 struct target_ops *ignore; 2628{ 2629 puts_filtered ("Debugging a target over a serial line.\n"); 2630} 2631 2632/* Stuff for dealing with the packets which are part of this protocol. 2633 See comment at top of file for details. */ 2634 2635/* Read a single character from the remote end, masking it down to 7 bits. */ 2636 2637static int 2638readchar (timeout) 2639 int timeout; 2640{ 2641 int ch; 2642 2643 ch = SERIAL_READCHAR (remote_desc, timeout); 2644 2645 switch (ch) 2646 { 2647 case SERIAL_EOF: 2648 error ("Remote connection closed"); 2649 case SERIAL_ERROR: 2650 perror_with_name ("Remote communication error"); 2651 case SERIAL_TIMEOUT: 2652 return ch; 2653 default: 2654 return ch & 0x7f; 2655 } 2656} 2657 2658/* Send the command in BUF to the remote machine, and read the reply 2659 into BUF. Report an error if we get an error reply. */ 2660 2661static void 2662remote_send (buf) 2663 char *buf; 2664{ 2665 putpkt (buf); 2666 getpkt (buf, 0); 2667 2668 if (buf[0] == 'E') 2669 error ("Remote failure reply: %s", buf); 2670} 2671 2672/* Display a null-terminated packet on stdout, for debugging, using C 2673 string notation. */ 2674 2675static void 2676print_packet (buf) 2677 char *buf; 2678{ 2679 puts_filtered ("\""); 2680 while (*buf) 2681 gdb_printchar (*buf++, gdb_stdout, '"'); 2682 puts_filtered ("\""); 2683} 2684 2685int 2686putpkt (buf) 2687 char *buf; 2688{ 2689 return putpkt_binary (buf, strlen (buf)); 2690} 2691 2692/* Send a packet to the remote machine, with error checking. The data 2693 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5 2694 to account for the $, # and checksum, and for a possible /0 if we are 2695 debugging (remote_debug) and want to print the sent packet as a string */ 2696 2697static int 2698putpkt_binary (buf, cnt) 2699 char *buf; 2700 int cnt; 2701{ 2702 int i; 2703 unsigned char csum = 0; 2704 char buf2[PBUFSIZ]; 2705 int ch; 2706 int tcount = 0; 2707 char *p; 2708 2709 /* Copy the packet into buffer BUF2, encapsulating it 2710 and giving it a checksum. */ 2711 2712 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ 2713 abort (); 2714 2715 p = buf2; 2716 *p++ = '$'; 2717 2718 for (i = 0; i < cnt; i++) 2719 { 2720 csum += buf[i]; 2721 *p++ = buf[i]; 2722 } 2723 *p++ = '#'; 2724 *p++ = tohex ((csum >> 4) & 0xf); 2725 *p++ = tohex (csum & 0xf); 2726 2727 /* Send it over and over until we get a positive ack. */ 2728 2729 while (1) 2730 { 2731 int started_error_output = 0; 2732 2733 if (remote_debug) 2734 { 2735 *p = '\0'; 2736 printf_unfiltered ("Sending packet: %s...", buf2); 2737 gdb_flush (gdb_stdout); 2738 } 2739 if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) 2740 perror_with_name ("putpkt: write failed"); 2741 2742 /* read until either a timeout occurs (-2) or '+' is read */ 2743 while (1) 2744 { 2745 ch = readchar (remote_timeout); 2746 2747 if (remote_debug) 2748 { 2749 switch (ch) 2750 { 2751 case '+': 2752 case SERIAL_TIMEOUT: 2753 case '$': 2754 if (started_error_output) 2755 { 2756 putchar_unfiltered ('\n'); 2757 started_error_output = 0; 2758 } 2759 } 2760 } 2761 2762 switch (ch) 2763 { 2764 case '+': 2765 if (remote_debug) 2766 printf_unfiltered ("Ack\n"); 2767 return 1; 2768 case SERIAL_TIMEOUT: 2769 tcount ++; 2770 if (tcount > 3) 2771 return 0; 2772 break; /* Retransmit buffer */ 2773 case '$': 2774 { 2775 char junkbuf[PBUFSIZ]; 2776 2777 /* It's probably an old response, and we're out of sync. 2778 Just gobble up the packet and ignore it. */ 2779 getpkt (junkbuf, 0); 2780 continue; /* Now, go look for + */ 2781 } 2782 2783#ifdef REMOTE_CHAT 2784 case '|': 2785 { 2786 if (!started_error_output) 2787 continue; 2788 /* else fall through */ 2789 } 2790#endif /* REMOTE_CHAT */ 2791 2792 default: 2793 if (remote_debug) 2794 { 2795 if (!started_error_output) 2796 { 2797 started_error_output = 1; 2798 printf_unfiltered ("putpkt: Junk: "); 2799 } 2800 putchar_unfiltered (ch & 0177); 2801 } 2802 continue; 2803 } 2804 break; /* Here to retransmit */ 2805 } 2806 2807#if 0 2808 /* This is wrong. If doing a long backtrace, the user should be 2809 able to get out next time we call QUIT, without anything as 2810 violent as interrupt_query. If we want to provide a way out of 2811 here without getting to the next QUIT, it should be based on 2812 hitting ^C twice as in remote_wait. */ 2813 if (quit_flag) 2814 { 2815 quit_flag = 0; 2816 interrupt_query (); 2817 } 2818#endif 2819 } 2820} 2821 2822/* Come here after finding the start of the frame. Collect the rest 2823 into BUF, verifying the checksum, length, and handling run-length 2824 compression. Returns 0 on any error, 1 on success. */ 2825 2826static int 2827read_frame (buf) 2828 char *buf; 2829{ 2830 unsigned char csum; 2831 char *bp; 2832 int c; 2833 2834 csum = 0; 2835 bp = buf; 2836 2837 while (1) 2838 { 2839 c = readchar (remote_timeout); 2840 2841 switch (c) 2842 { 2843 case SERIAL_TIMEOUT: 2844 if (remote_debug) 2845 puts_filtered ("Timeout in mid-packet, retrying\n"); 2846 return 0; 2847 case '$': 2848 if (remote_debug) 2849 puts_filtered ("Saw new packet start in middle of old one\n"); 2850 return 0; /* Start a new packet, count retries */ 2851 case '#': 2852 { 2853 unsigned char pktcsum; 2854 2855 *bp = '\000'; 2856 2857 pktcsum = fromhex (readchar (remote_timeout)) << 4; 2858 pktcsum |= fromhex (readchar (remote_timeout)); 2859 2860 if (csum == pktcsum) 2861 return 1; 2862 2863 if (remote_debug) 2864 { 2865 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", 2866 pktcsum, csum); 2867 puts_filtered (buf); 2868 puts_filtered ("\n"); 2869 } 2870 return 0; 2871 } 2872 case '*': /* Run length encoding */ 2873 csum += c; 2874 c = readchar (remote_timeout); 2875 csum += c; 2876 c = c - ' ' + 3; /* Compute repeat count */ 2877 2878 2879 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) 2880 { 2881 memset (bp, *(bp - 1), c); 2882 bp += c; 2883 continue; 2884 } 2885 2886 *bp = '\0'; 2887 printf_filtered ("Repeat count %d too large for buffer: ", c); 2888 puts_filtered (buf); 2889 puts_filtered ("\n"); 2890 return 0; 2891 2892 default: 2893 if (bp < buf + PBUFSIZ - 1) 2894 { 2895 *bp++ = c; 2896 csum += c; 2897 continue; 2898 } 2899 2900 *bp = '\0'; 2901 puts_filtered ("Remote packet too long: "); 2902 puts_filtered (buf); 2903 puts_filtered ("\n"); 2904 2905 return 0; 2906 } 2907 } 2908} 2909 2910/* Read a packet from the remote machine, with error checking, and 2911 store it in BUF. BUF is expected to be of size PBUFSIZ. If 2912 FOREVER, wait forever rather than timing out; this is used while 2913 the target is executing user code. */ 2914 2915void 2916getpkt (buf, forever) 2917 char *buf; 2918 int forever; 2919{ 2920 int c; 2921 int tries; 2922 int timeout; 2923 int val; 2924 2925 strcpy (buf,"timeout"); 2926 2927 if (forever) 2928 { 2929#ifdef MAINTENANCE_CMDS 2930 timeout = watchdog > 0 ? watchdog : -1; 2931#else 2932 timeout = -1; 2933#endif 2934 } 2935 2936 else 2937 timeout = remote_timeout; 2938 2939#define MAX_TRIES 3 2940 2941 for (tries = 1; tries <= MAX_TRIES; tries++) 2942 { 2943 /* This can loop forever if the remote side sends us characters 2944 continuously, but if it pauses, we'll get a zero from readchar 2945 because of timeout. Then we'll count that as a retry. */ 2946 2947 /* Note that we will only wait forever prior to the start of a packet. 2948 After that, we expect characters to arrive at a brisk pace. They 2949 should show up within remote_timeout intervals. */ 2950 2951 do 2952 { 2953 c = readchar (timeout); 2954 2955 if (c == SERIAL_TIMEOUT) 2956 { 2957#ifdef MAINTENANCE_CMDS 2958 if (forever) /* Watchdog went off. Kill the target. */ 2959 { 2960 target_mourn_inferior (); 2961 error ("Watchdog has expired. Target detached.\n"); 2962 } 2963#endif 2964 if (remote_debug) 2965 puts_filtered ("Timed out.\n"); 2966 goto retry; 2967 } 2968 } 2969 while (c != '$'); 2970 2971 /* We've found the start of a packet, now collect the data. */ 2972 2973 val = read_frame (buf); 2974 2975 if (val == 1) 2976 { 2977 if (remote_debug) 2978 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf); 2979 SERIAL_WRITE (remote_desc, "+", 1); 2980 return; 2981 } 2982 2983 /* Try the whole thing again. */ 2984 retry: 2985 SERIAL_WRITE (remote_desc, "-", 1); 2986 } 2987 2988 /* We have tried hard enough, and just can't receive the packet. Give up. */ 2989 2990 printf_unfiltered ("Ignoring packet error, continuing...\n"); 2991 SERIAL_WRITE (remote_desc, "+", 1); 2992} 2993 2994static void 2995remote_kill () 2996{ 2997 /* For some mysterious reason, wait_for_inferior calls kill instead of 2998 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ 2999 if (kill_kludge) 3000 { 3001 kill_kludge = 0; 3002 target_mourn_inferior (); 3003 return; 3004 } 3005 3006 /* Use catch_errors so the user can quit from gdb even when we aren't on 3007 speaking terms with the remote system. */ 3008 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); 3009 3010 /* Don't wait for it to die. I'm not really sure it matters whether 3011 we do or not. For the existing stubs, kill is a noop. */ 3012 target_mourn_inferior (); 3013} 3014 3015static void 3016remote_mourn () 3017{ 3018 remote_mourn_1 (&remote_ops); 3019} 3020 3021static void 3022extended_remote_mourn () 3023{ 3024 /* We do _not_ want to mourn the target like this; this will 3025 remove the extended remote target from the target stack, 3026 and the next time the user says "run" it'll fail. 3027 3028 FIXME: What is the right thing to do here? */ 3029#if 0 3030 remote_mourn_1 (&extended_remote_ops); 3031#endif 3032} 3033 3034/* Worker function for remote_mourn. */ 3035static void 3036remote_mourn_1 (target) 3037 struct target_ops *target; 3038{ 3039 unpush_target (target); 3040 generic_mourn_inferior (); 3041} 3042 3043/* In the extended protocol we want to be able to do things like 3044 "run" and have them basically work as expected. So we need 3045 a special create_inferior function. 3046 3047 FIXME: One day add support for changing the exec file 3048 we're debugging, arguments and an environment. */ 3049 3050static void 3051extended_remote_create_inferior (exec_file, args, env) 3052 char *exec_file; 3053 char *args; 3054 char **env; 3055{ 3056 /* Rip out the breakpoints; we'll reinsert them after restarting 3057 the remote server. */ 3058 remove_breakpoints (); 3059 3060 /* Now restart the remote server. */ 3061 extended_remote_restart (); 3062 3063 /* Now put the breakpoints back in. This way we're safe if the 3064 restart function works via a unix fork on the remote side. */ 3065 insert_breakpoints (); 3066 3067 /* Clean up from the last time we were running. */ 3068 clear_proceed_status (); 3069 3070 /* Let the remote process run. */ 3071 proceed (-1, TARGET_SIGNAL_0, 0); 3072} 3073 3074 3075/* On some machines, e.g. 68k, we may use a different breakpoint instruction 3076 than other targets; in those use REMOTE_BREAKPOINT instead of just 3077 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT 3078 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call 3079 the standard routines that are in mem-break.c. */ 3080 3081/* FIXME, these ought to be done in a more dynamic fashion. For instance, 3082 the choice of breakpoint instruction affects target program design and 3083 vice versa, and by making it user-tweakable, the special code here 3084 goes away and we need fewer special GDB configurations. */ 3085 3086#if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) 3087#define REMOTE_BREAKPOINT 3088#endif 3089 3090#ifdef REMOTE_BREAKPOINT 3091 3092/* If the target isn't bi-endian, just pretend it is. */ 3093#if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) 3094#define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT 3095#define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT 3096#endif 3097 3098static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; 3099static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; 3100 3101#endif /* REMOTE_BREAKPOINT */ 3102 3103/* Insert a breakpoint on targets that don't have any better breakpoint 3104 support. We read the contents of the target location and stash it, 3105 then overwrite it with a breakpoint instruction. ADDR is the target 3106 location in the target machine. CONTENTS_CACHE is a pointer to 3107 memory allocated for saving the target contents. It is guaranteed 3108 by the caller to be long enough to save sizeof BREAKPOINT bytes (this 3109 is accomplished via BREAKPOINT_MAX). */ 3110 3111static int 3112remote_insert_breakpoint (addr, contents_cache) 3113 CORE_ADDR addr; 3114 char *contents_cache; 3115{ 3116#ifdef REMOTE_BREAKPOINT 3117 int val; 3118 3119 val = target_read_memory (addr, contents_cache, sizeof big_break_insn); 3120 3121 if (val == 0) 3122 { 3123 if (TARGET_BYTE_ORDER == BIG_ENDIAN) 3124 val = target_write_memory (addr, (char *) big_break_insn, 3125 sizeof big_break_insn); 3126 else 3127 val = target_write_memory (addr, (char *) little_break_insn, 3128 sizeof little_break_insn); 3129 } 3130 3131 return val; 3132#else 3133 return memory_insert_breakpoint (addr, contents_cache); 3134#endif /* REMOTE_BREAKPOINT */ 3135} 3136 3137static int 3138remote_remove_breakpoint (addr, contents_cache) 3139 CORE_ADDR addr; 3140 char *contents_cache; 3141{ 3142#ifdef REMOTE_BREAKPOINT 3143 return target_write_memory (addr, contents_cache, sizeof big_break_insn); 3144#else 3145 return memory_remove_breakpoint (addr, contents_cache); 3146#endif /* REMOTE_BREAKPOINT */ 3147} 3148 3149/* Some targets are only capable of doing downloads, and afterwards 3150 they switch to the remote serial protocol. This function provides 3151 a clean way to get from the download target to the remote target. 3152 It's basically just a wrapper so that we don't have to expose any 3153 of the internal workings of remote.c. 3154 3155 Prior to calling this routine, you should shutdown the current 3156 target code, else you will get the "A program is being debugged 3157 already..." message. Usually a call to pop_target() suffices. */ 3158 3159void 3160push_remote_target (name, from_tty) 3161 char *name; 3162 int from_tty; 3163{ 3164 printf_filtered ("Switching to remote protocol\n"); 3165 remote_open (name, from_tty); 3166} 3167 3168/* Other targets want to use the entire remote serial module but with 3169 certain remote_ops overridden. */ 3170 3171void 3172open_remote_target (name, from_tty, target, extended_p) 3173 char *name; 3174 int from_tty; 3175 struct target_ops *target; 3176 int extended_p; 3177{ 3178 printf_filtered ("Selecting the %sremote protocol\n", 3179 (extended_p ? "extended-" : "")); 3180 remote_open_1 (name, from_tty, target, extended_p); 3181} 3182 3183/* Table used by the crc32 function to calcuate the checksum. */ 3184 3185static unsigned long crc32_table[256] = {0, 0}; 3186 3187static unsigned long 3188crc32 (buf, len, crc) 3189 unsigned char *buf; 3190 int len; 3191 unsigned int crc; 3192{ 3193 if (! crc32_table[1]) 3194 { 3195 /* Initialize the CRC table and the decoding table. */ 3196 int i, j; 3197 unsigned int c; 3198 3199 for (i = 0; i < 256; i++) 3200 { 3201 for (c = i << 24, j = 8; j > 0; --j) 3202 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); 3203 crc32_table[i] = c; 3204 } 3205 } 3206 3207 while (len--) 3208 { 3209 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; 3210 buf++; 3211 } 3212 return crc; 3213} 3214 3215/* compare-sections command 3216 3217 With no arguments, compares each loadable section in the exec bfd 3218 with the same memory range on the target, and reports mismatches. 3219 Useful for verifying the image on the target against the exec file. 3220 Depends on the target understanding the new "qCRC:" request. */ 3221 3222static void 3223compare_sections_command (args, from_tty) 3224 char *args; 3225 int from_tty; 3226{ 3227 asection *s; 3228 unsigned long host_crc, target_crc; 3229 extern bfd *exec_bfd; 3230 struct cleanup *old_chain; 3231 char *tmp, *sectdata, *sectname, buf[PBUFSIZ]; 3232 bfd_size_type size; 3233 bfd_vma lma; 3234 int matched = 0; 3235 int mismatched = 0; 3236 3237 if (!exec_bfd) 3238 error ("command cannot be used without an exec file"); 3239 if (!current_target.to_shortname || 3240 strcmp (current_target.to_shortname, "remote") != 0) 3241 error ("command can only be used with remote target"); 3242 3243 for (s = exec_bfd->sections; s; s = s->next) 3244 { 3245 if (!(s->flags & SEC_LOAD)) 3246 continue; /* skip non-loadable section */ 3247 3248 size = bfd_get_section_size_before_reloc (s); 3249 if (size == 0) 3250 continue; /* skip zero-length section */ 3251 3252 sectname = (char *) bfd_get_section_name (exec_bfd, s); 3253 if (args && strcmp (args, sectname) != 0) 3254 continue; /* not the section selected by user */ 3255 3256 matched = 1; /* do this section */ 3257 lma = s->lma; 3258 /* FIXME: assumes lma can fit into long */ 3259 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size); 3260 putpkt (buf); 3261 3262 /* be clever; compute the host_crc before waiting for target reply */ 3263 sectdata = xmalloc (size); 3264 old_chain = make_cleanup (free, sectdata); 3265 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); 3266 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); 3267 3268 getpkt (buf, 0); 3269 if (buf[0] == 'E') 3270 error ("target memory fault, section %s, range 0x%08x -- 0x%08x", 3271 sectname, lma, lma + size); 3272 if (buf[0] != 'C') 3273 error ("remote target does not support this operation"); 3274 3275 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++) 3276 target_crc = target_crc * 16 + fromhex (*tmp); 3277 3278 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ", 3279 sectname, lma, lma + size); 3280 if (host_crc == target_crc) 3281 printf_filtered ("matched.\n"); 3282 else 3283 { 3284 printf_filtered ("MIS-MATCHED!\n"); 3285 mismatched++; 3286 } 3287 3288 do_cleanups (old_chain); 3289 } 3290 if (mismatched > 0) 3291 warning ("One or more sections of the remote executable does not match\n\ 3292the loaded file\n"); 3293 if (args && !matched) 3294 printf_filtered ("No loaded section named '%s'.\n", args); 3295} 3296 3297static int 3298remote_query (query_type, buf, outbuf, bufsiz) 3299 int query_type; 3300 char *buf; 3301 char *outbuf; 3302 int *bufsiz; 3303{ 3304 int i; 3305 char buf2[PBUFSIZ]; 3306 char *p2 = &buf2[0]; 3307 char *p = buf; 3308 3309 if (! bufsiz) 3310 error ("null pointer to remote bufer size specified"); 3311 3312 /* minimum outbuf size is PBUFSIZE - if bufsiz is not large enough let 3313 the caller know and return what the minimum size is */ 3314 /* Note: a zero bufsiz can be used to query the minimum buffer size */ 3315 if ( *bufsiz < PBUFSIZ ) 3316 { 3317 *bufsiz = PBUFSIZ; 3318 return -1; 3319 } 3320 3321 /* except for querying the minimum buffer size, target must be open */ 3322 if (! remote_desc) 3323 error ("remote query is only available after target open"); 3324 3325 /* we only take uppercase letters as query types, at least for now */ 3326 if ( (query_type < 'A') || (query_type > 'Z') ) 3327 error ("invalid remote query type"); 3328 3329 if (! buf) 3330 error ("null remote query specified"); 3331 3332 if (! outbuf) 3333 error ("remote query requires a buffer to receive data"); 3334 3335 outbuf[0] = '\0'; 3336 3337 *p2++ = 'q'; 3338 *p2++ = query_type; 3339 3340 /* we used one buffer char for the remote protocol q command and another 3341 for the query type. As the remote protocol encapsulation uses 4 chars 3342 plus one extra in case we are debugging (remote_debug), 3343 we have PBUFZIZ - 7 left to pack the query string */ 3344 i = 0; 3345 while ( buf[i] && (i < (PBUFSIZ - 8)) ) 3346 { 3347 /* bad caller may have sent forbidden characters */ 3348 if ( (!isprint(buf[i])) || (buf[i] == '$') || (buf[i] == '#') ) 3349 error ("illegal characters in query string"); 3350 3351 *p2++ = buf[i]; 3352 i++; 3353 } 3354 *p2 = buf[i]; 3355 3356 if ( buf[i] ) 3357 error ("query larger than available buffer"); 3358 3359 i = putpkt (buf2); 3360 if ( i < 0 ) return i; 3361 3362 getpkt (outbuf, 0); 3363 3364 return 0; 3365} 3366 3367static void 3368packet_command (args, from_tty) 3369 char *args; 3370 int from_tty; 3371{ 3372 char buf[PBUFSIZ]; 3373 3374 if (! remote_desc) 3375 error ("command can only be used with remote target"); 3376 3377 if (! args) 3378 error ("remote-packet command requires packet text as argument"); 3379 3380 puts_filtered ("sending: "); 3381 print_packet (args); 3382 puts_filtered ("\n"); 3383 putpkt (args); 3384 3385 getpkt (buf, 0); 3386 puts_filtered ("received: "); 3387 print_packet (buf); 3388 puts_filtered ("\n"); 3389} 3390 3391#if 0 3392/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */ 3393 3394static void display_thread_info PARAMS ((struct gdb_ext_thread_info *info)); 3395 3396static void threadset_test_cmd PARAMS ((char *cmd, int tty)); 3397 3398static void threadalive_test PARAMS ((char *cmd, int tty)); 3399 3400static void threadlist_test_cmd PARAMS ((char *cmd, int tty)); 3401 3402int get_and_display_threadinfo PARAMS ((threadref *ref)); 3403 3404static void threadinfo_test_cmd PARAMS ((char *cmd, int tty)); 3405 3406static int thread_display_step PARAMS ((threadref *ref, void *context)); 3407 3408static void threadlist_update_test_cmd PARAMS ((char *cmd, int tty)); 3409 3410static void init_remote_threadtests PARAMS ((void)); 3411 3412#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */ 3413 3414static void 3415threadset_test_cmd (cmd, tty) 3416 char *cmd; 3417 int tty; 3418{ 3419 int sample_thread = SAMPLE_THREAD; 3420 3421 printf_filtered ("Remote threadset test\n"); 3422 set_thread (sample_thread, 1); 3423} 3424 3425 3426static void 3427threadalive_test (cmd, tty) 3428 char *cmd; 3429 int tty; 3430{ 3431 int sample_thread = SAMPLE_THREAD; 3432 3433 if (remote_thread_alive (sample_thread)) 3434 printf_filtered ("PASS: Thread alive test\n"); 3435 else 3436 printf_filtered ("FAIL: Thread alive test\n"); 3437} 3438 3439void output_threadid PARAMS ((char *title, threadref * ref)); 3440 3441void 3442output_threadid (title, ref) 3443 char *title; 3444 threadref *ref; 3445{ 3446 char hexid[20]; 3447 3448 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */ 3449 hexid[16] = 0; 3450 printf_filtered ("%s %s\n", title, (&hexid[0])); 3451} 3452 3453static void 3454threadlist_test_cmd (cmd, tty) 3455 char *cmd; 3456 int tty; 3457{ 3458 int startflag = 1; 3459 threadref nextthread; 3460 int done, result_count; 3461 threadref threadlist[3]; 3462 3463 printf_filtered ("Remote Threadlist test\n"); 3464 if (!remote_get_threadlist (startflag, &nextthread, 3, &done, 3465 &result_count, &threadlist[0])) 3466 printf_filtered ("FAIL: threadlist test\n"); 3467 else 3468 { 3469 threadref *scan = threadlist; 3470 threadref *limit = scan + result_count; 3471 3472 while (scan < limit) 3473 output_threadid (" thread ", scan++); 3474 } 3475} 3476 3477void 3478display_thread_info (info) 3479 struct gdb_ext_thread_info *info; 3480{ 3481 output_threadid ("Threadid: ", &info->threadid); 3482 printf_filtered ("Name: %s\n ", info->shortname); 3483 printf_filtered ("State: %s\n", info->display); 3484 printf_filtered ("other: %s\n\n", info->more_display); 3485} 3486 3487int 3488get_and_display_threadinfo (ref) 3489 threadref *ref; 3490{ 3491 int result; 3492 int set; 3493 struct gdb_ext_thread_info threadinfo; 3494 3495 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME 3496 | TAG_MOREDISPLAY | TAG_DISPLAY; 3497 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) 3498 display_thread_info (&threadinfo); 3499 return result; 3500} 3501 3502static void 3503threadinfo_test_cmd (cmd, tty) 3504 char *cmd; 3505 int tty; 3506{ 3507 int athread = SAMPLE_THREAD; 3508 threadref thread; 3509 int set; 3510 3511 int_to_threadref (&thread, athread); 3512 printf_filtered ("Remote Threadinfo test\n"); 3513 if (!get_and_display_threadinfo (&thread)) 3514 printf_filtered ("FAIL cannot get thread info\n"); 3515} 3516 3517static int 3518thread_display_step (ref, context) 3519 threadref *ref; 3520 void *context; 3521{ 3522 /* output_threadid(" threadstep ",ref); *//* simple test */ 3523 return get_and_display_threadinfo (ref); 3524} 3525 3526static void 3527threadlist_update_test_cmd (cmd, tty) 3528 char *cmd; 3529 int tty; 3530{ 3531 printf_filtered ("Remote Threadlist update test\n"); 3532 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); 3533} 3534 3535static void 3536init_remote_threadtests (void) 3537{ 3538 add_com ("tlist", class_obscure, threadlist_test_cmd, 3539 "Fetch and print the remote list of thread identifiers, one pkt only"); 3540 add_com ("tinfo", class_obscure, threadinfo_test_cmd, 3541 "Fetch and display info about one thread"); 3542 add_com ("tset", class_obscure, threadset_test_cmd, 3543 "Test setting to a different thread"); 3544 add_com ("tupd", class_obscure, threadlist_update_test_cmd, 3545 "Iterate through updating all remote thread info"); 3546 add_com ("talive", class_obscure, threadalive_test, 3547 " Remote thread alive test "); 3548} 3549 3550#endif /* 0 */ 3551 3552static void 3553init_remote_ops () 3554{ 3555 remote_ops.to_shortname = "remote"; 3556 remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; 3557 remote_ops.to_doc = 3558 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ 3559Specify the serial device it is connected to (e.g. /dev/ttya)."; 3560 remote_ops.to_open = remote_open; 3561 remote_ops.to_close = remote_close; 3562 remote_ops.to_detach = remote_detach; 3563 remote_ops.to_resume = remote_resume; 3564 remote_ops.to_wait = remote_wait; 3565 remote_ops.to_fetch_registers = remote_fetch_registers; 3566 remote_ops.to_store_registers = remote_store_registers; 3567 remote_ops.to_prepare_to_store = remote_prepare_to_store; 3568 remote_ops.to_xfer_memory = remote_xfer_memory; 3569 remote_ops.to_files_info = remote_files_info; 3570 remote_ops.to_insert_breakpoint = remote_insert_breakpoint; 3571 remote_ops.to_remove_breakpoint = remote_remove_breakpoint; 3572 remote_ops.to_kill = remote_kill; 3573 remote_ops.to_load = generic_load; 3574 remote_ops.to_mourn_inferior = remote_mourn; 3575 remote_ops.to_thread_alive = remote_thread_alive; 3576 remote_ops.to_stop = remote_stop; 3577 remote_ops.to_query = remote_query; 3578 remote_ops.to_stratum = process_stratum; 3579 remote_ops.to_has_all_memory = 1; 3580 remote_ops.to_has_memory = 1; 3581 remote_ops.to_has_stack = 1; 3582 remote_ops.to_has_registers = 1; 3583 remote_ops.to_has_execution = 1; 3584 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ 3585 remote_ops.to_magic = OPS_MAGIC; 3586} 3587 3588/* Set up the extended remote vector by making a copy of the standard 3589 remote vector and adding to it. */ 3590 3591static void 3592init_extended_remote_ops () 3593{ 3594 extended_remote_ops = remote_ops; 3595 3596 extended_remote_ops.to_shortname = "extended-remote"; 3597 extended_remote_ops.to_longname = 3598 "Extended remote serial target in gdb-specific protocol"; 3599 extended_remote_ops.to_doc = 3600 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ 3601Specify the serial device it is connected to (e.g. /dev/ttya).", 3602 extended_remote_ops.to_open = extended_remote_open; 3603 extended_remote_ops.to_create_inferior = extended_remote_create_inferior; 3604 extended_remote_ops.to_mourn_inferior = extended_remote_mourn; 3605} 3606 3607void 3608_initialize_remote () 3609{ 3610 init_remote_ops (); 3611 add_target (&remote_ops); 3612 3613 init_extended_remote_ops (); 3614 add_target (&extended_remote_ops); 3615 init_remote_threads (); 3616#if 0 3617 init_remote_threadtests (); 3618#endif 3619 3620 add_cmd ("compare-sections", class_obscure, compare_sections_command, 3621 "Compare section data on target to the exec file.\n\ 3622Argument is a single section name (default: all loaded sections).", 3623 &cmdlist); 3624 3625 add_cmd ("packet", class_maintenance, packet_command, 3626 "Send an arbitrary packet to a remote target.\n\ 3627 maintenance packet TEXT\n\ 3628If GDB is talking to an inferior via the GDB serial protocol, then\n\ 3629this command sends the string TEXT to the inferior, and displays the\n\ 3630response packet. GDB supplies the initial `$' character, and the\n\ 3631terminating `#' character and checksum.", 3632 &maintenancelist); 3633 3634#ifdef REMOTE_CHAT 3635 add_show_from_set 3636 (add_set_cmd ("remotechat", no_class, 3637 var_zinteger, (char *)&remote_chat, 3638 "Set remote port interacts with target.\n", 3639 &setlist), 3640 &showlist); 3641#endif /* REMOTE_CHAT */ 3642 3643 add_show_from_set 3644 (add_set_cmd ("remotetimeout", no_class, 3645 var_integer, (char *)&remote_timeout, 3646 "Set timeout value for remote read.\n", 3647 &setlist), 3648 &showlist); 3649 3650 add_show_from_set 3651 (add_set_cmd ("remotebreak", no_class, 3652 var_integer, (char *)&remote_break, 3653 "Set whether to send break if interrupted.\n", 3654 &setlist), 3655 &showlist); 3656 3657 add_show_from_set 3658 (add_set_cmd ("remotewritesize", no_class, 3659 var_integer, (char *)&remote_write_size, 3660 "Set the maximum number of bytes per memory write packet.\n", 3661 &setlist), 3662 &showlist); 3663 3664 remote_address_size = TARGET_PTR_BIT; 3665 add_show_from_set 3666 (add_set_cmd ("remoteaddresssize", class_obscure, 3667 var_integer, (char *)&remote_address_size, 3668 "Set the maximum size of the address (in bits) \ 3669in a memory packet.\n", 3670 &setlist), 3671 &showlist); 3672 3673 add_show_from_set (add_set_cmd ("remotebinarydownload", no_class, 3674 var_boolean, (char *) &remote_binary_download, 3675 "Set binary downloads.\n", &setlist), 3676 &showlist); 3677} 3678 3679