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