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