| 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
| 2 | |
| 3 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
| 4 | 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GDB. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | Boston, MA 02111-1307, USA. */ |
| 23 | |
| 24 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
| 25 | |
| 26 | #include "defs.h" |
| 27 | #include "gdb_string.h" |
| 28 | #include <ctype.h> |
| 29 | #include <fcntl.h> |
| 30 | #include "inferior.h" |
| 31 | #include "bfd.h" |
| 32 | #include "symfile.h" |
| 33 | #include "exceptions.h" |
| 34 | #include "target.h" |
| 35 | /*#include "terminal.h" */ |
| 36 | #include "gdbcmd.h" |
| 37 | #include "objfiles.h" |
| 38 | #include "gdb-stabs.h" |
| 39 | #include "gdbthread.h" |
| 40 | #include "remote.h" |
| 41 | #include "regcache.h" |
| 42 | #include "value.h" |
| 43 | #include "gdb_assert.h" |
| 44 | #include "observer.h" |
| 45 | |
| 46 | #include <ctype.h> |
| 47 | #include <sys/time.h> |
| 48 | |
| 49 | #include "event-loop.h" |
| 50 | #include "event-top.h" |
| 51 | #include "inf-loop.h" |
| 52 | |
| 53 | #include <signal.h> |
| 54 | #include "serial.h" |
| 55 | |
| 56 | #include "gdbcore.h" /* for exec_bfd */ |
| 57 | |
| 58 | #include "remote-fileio.h" |
| 59 | |
| 60 | /* Prototypes for local functions. */ |
| 61 | static void cleanup_sigint_signal_handler (void *dummy); |
| 62 | static void initialize_sigint_signal_handler (void); |
| 63 | static int getpkt_sane (char *buf, long sizeof_buf, int forever); |
| 64 | |
| 65 | static void handle_remote_sigint (int); |
| 66 | static void handle_remote_sigint_twice (int); |
| 67 | static void async_remote_interrupt (gdb_client_data); |
| 68 | void async_remote_interrupt_twice (gdb_client_data); |
| 69 | |
| 70 | static void build_remote_gdbarch_data (void); |
| 71 | |
| 72 | static void remote_files_info (struct target_ops *ignore); |
| 73 | |
| 74 | static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr, |
| 75 | int len, int should_write, |
| 76 | struct mem_attrib *attrib, |
| 77 | struct target_ops *target); |
| 78 | |
| 79 | static void remote_prepare_to_store (void); |
| 80 | |
| 81 | static void remote_fetch_registers (int regno); |
| 82 | |
| 83 | static void remote_resume (ptid_t ptid, int step, |
| 84 | enum target_signal siggnal); |
| 85 | static void remote_async_resume (ptid_t ptid, int step, |
| 86 | enum target_signal siggnal); |
| 87 | static void remote_open (char *name, int from_tty); |
| 88 | static void remote_async_open (char *name, int from_tty); |
| 89 | |
| 90 | static void extended_remote_open (char *name, int from_tty); |
| 91 | static void extended_remote_async_open (char *name, int from_tty); |
| 92 | |
| 93 | static void remote_open_1 (char *, int, struct target_ops *, int extended_p, |
| 94 | int async_p); |
| 95 | |
| 96 | static void remote_close (int quitting); |
| 97 | |
| 98 | static void remote_store_registers (int regno); |
| 99 | |
| 100 | static void remote_mourn (void); |
| 101 | static void remote_async_mourn (void); |
| 102 | |
| 103 | static void extended_remote_restart (void); |
| 104 | |
| 105 | static void extended_remote_mourn (void); |
| 106 | |
| 107 | static void remote_mourn_1 (struct target_ops *); |
| 108 | |
| 109 | static void remote_send (char *buf, long sizeof_buf); |
| 110 | |
| 111 | static int readchar (int timeout); |
| 112 | |
| 113 | static ptid_t remote_wait (ptid_t ptid, |
| 114 | struct target_waitstatus *status); |
| 115 | static ptid_t remote_async_wait (ptid_t ptid, |
| 116 | struct target_waitstatus *status); |
| 117 | |
| 118 | static void remote_kill (void); |
| 119 | static void remote_async_kill (void); |
| 120 | |
| 121 | static int tohex (int nib); |
| 122 | |
| 123 | static void remote_detach (char *args, int from_tty); |
| 124 | |
| 125 | static void remote_interrupt (int signo); |
| 126 | |
| 127 | static void remote_interrupt_twice (int signo); |
| 128 | |
| 129 | static void interrupt_query (void); |
| 130 | |
| 131 | static void set_thread (int, int); |
| 132 | |
| 133 | static int remote_thread_alive (ptid_t); |
| 134 | |
| 135 | static void get_offsets (void); |
| 136 | |
| 137 | static long read_frame (char *buf, long sizeof_buf); |
| 138 | |
| 139 | static int remote_insert_breakpoint (CORE_ADDR, bfd_byte *); |
| 140 | |
| 141 | static int remote_remove_breakpoint (CORE_ADDR, bfd_byte *); |
| 142 | |
| 143 | static int hexnumlen (ULONGEST num); |
| 144 | |
| 145 | static void init_remote_ops (void); |
| 146 | |
| 147 | static void init_extended_remote_ops (void); |
| 148 | |
| 149 | static void remote_stop (void); |
| 150 | |
| 151 | static int ishex (int ch, int *val); |
| 152 | |
| 153 | static int stubhex (int ch); |
| 154 | |
| 155 | static int hexnumstr (char *, ULONGEST); |
| 156 | |
| 157 | static int hexnumnstr (char *, ULONGEST, int); |
| 158 | |
| 159 | static CORE_ADDR remote_address_masked (CORE_ADDR); |
| 160 | |
| 161 | static void print_packet (char *); |
| 162 | |
| 163 | static unsigned long crc32 (unsigned char *, int, unsigned int); |
| 164 | |
| 165 | static void compare_sections_command (char *, int); |
| 166 | |
| 167 | static void packet_command (char *, int); |
| 168 | |
| 169 | static int stub_unpack_int (char *buff, int fieldlength); |
| 170 | |
| 171 | static ptid_t remote_current_thread (ptid_t oldptid); |
| 172 | |
| 173 | static void remote_find_new_threads (void); |
| 174 | |
| 175 | static void record_currthread (int currthread); |
| 176 | |
| 177 | static int fromhex (int a); |
| 178 | |
| 179 | static int hex2bin (const char *hex, char *bin, int count); |
| 180 | |
| 181 | static int bin2hex (const char *bin, char *hex, int count); |
| 182 | |
| 183 | static int putpkt_binary (char *buf, int cnt); |
| 184 | |
| 185 | static void check_binary_download (CORE_ADDR addr); |
| 186 | |
| 187 | struct packet_config; |
| 188 | |
| 189 | static void show_packet_config_cmd (struct packet_config *config); |
| 190 | |
| 191 | static void update_packet_config (struct packet_config *config); |
| 192 | |
| 193 | void _initialize_remote (void); |
| 194 | |
| 195 | /* Description of the remote protocol. Strictly speaking, when the |
| 196 | target is open()ed, remote.c should create a per-target description |
| 197 | of the remote protocol using that target's architecture. |
| 198 | Unfortunately, the target stack doesn't include local state. For |
| 199 | the moment keep the information in the target's architecture |
| 200 | object. Sigh.. */ |
| 201 | |
| 202 | struct packet_reg |
| 203 | { |
| 204 | long offset; /* Offset into G packet. */ |
| 205 | long regnum; /* GDB's internal register number. */ |
| 206 | LONGEST pnum; /* Remote protocol register number. */ |
| 207 | int in_g_packet; /* Always part of G packet. */ |
| 208 | /* long size in bytes; == register_size (current_gdbarch, regnum); |
| 209 | at present. */ |
| 210 | /* char *name; == REGISTER_NAME (regnum); at present. */ |
| 211 | }; |
| 212 | |
| 213 | struct remote_state |
| 214 | { |
| 215 | /* Description of the remote protocol registers. */ |
| 216 | long sizeof_g_packet; |
| 217 | |
| 218 | /* Description of the remote protocol registers indexed by REGNUM |
| 219 | (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */ |
| 220 | struct packet_reg *regs; |
| 221 | |
| 222 | /* This is the size (in chars) of the first response to the ``g'' |
| 223 | packet. It is used as a heuristic when determining the maximum |
| 224 | size of memory-read and memory-write packets. A target will |
| 225 | typically only reserve a buffer large enough to hold the ``g'' |
| 226 | packet. The size does not include packet overhead (headers and |
| 227 | trailers). */ |
| 228 | long actual_register_packet_size; |
| 229 | |
| 230 | /* This is the maximum size (in chars) of a non read/write packet. |
| 231 | It is also used as a cap on the size of read/write packets. */ |
| 232 | long remote_packet_size; |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | /* Handle for retreving the remote protocol data from gdbarch. */ |
| 237 | static struct gdbarch_data *remote_gdbarch_data_handle; |
| 238 | |
| 239 | static struct remote_state * |
| 240 | get_remote_state (void) |
| 241 | { |
| 242 | return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle); |
| 243 | } |
| 244 | |
| 245 | static void * |
| 246 | init_remote_state (struct gdbarch *gdbarch) |
| 247 | { |
| 248 | int regnum; |
| 249 | struct remote_state *rs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_state); |
| 250 | |
| 251 | rs->sizeof_g_packet = 0; |
| 252 | |
| 253 | /* Assume a 1:1 regnum<->pnum table. */ |
| 254 | rs->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS, |
| 255 | struct packet_reg); |
| 256 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
| 257 | { |
| 258 | struct packet_reg *r = &rs->regs[regnum]; |
| 259 | r->pnum = regnum; |
| 260 | r->regnum = regnum; |
| 261 | r->offset = DEPRECATED_REGISTER_BYTE (regnum); |
| 262 | r->in_g_packet = (regnum < NUM_REGS); |
| 263 | /* ...name = REGISTER_NAME (regnum); */ |
| 264 | |
| 265 | /* Compute packet size by accumulating the size of all registers. */ |
| 266 | if (regnum < NUM_REGS) |
| 267 | rs->sizeof_g_packet += register_size (current_gdbarch, regnum); |
| 268 | } |
| 269 | |
| 270 | /* Default maximum number of characters in a packet body. Many |
| 271 | remote stubs have a hardwired buffer size of 400 bytes |
| 272 | (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used |
| 273 | as the maximum packet-size to ensure that the packet and an extra |
| 274 | NUL character can always fit in the buffer. This stops GDB |
| 275 | trashing stubs that try to squeeze an extra NUL into what is |
| 276 | already a full buffer (As of 1999-12-04 that was most stubs. */ |
| 277 | rs->remote_packet_size = 400 - 1; |
| 278 | |
| 279 | /* Should rs->sizeof_g_packet needs more space than the |
| 280 | default, adjust the size accordingly. Remember that each byte is |
| 281 | encoded as two characters. 32 is the overhead for the packet |
| 282 | header / footer. NOTE: cagney/1999-10-26: I suspect that 8 |
| 283 | (``$NN:G...#NN'') is a better guess, the below has been padded a |
| 284 | little. */ |
| 285 | if (rs->sizeof_g_packet > ((rs->remote_packet_size - 32) / 2)) |
| 286 | rs->remote_packet_size = (rs->sizeof_g_packet * 2 + 32); |
| 287 | |
| 288 | /* This one is filled in when a ``g'' packet is received. */ |
| 289 | rs->actual_register_packet_size = 0; |
| 290 | |
| 291 | return rs; |
| 292 | } |
| 293 | |
| 294 | static struct packet_reg * |
| 295 | packet_reg_from_regnum (struct remote_state *rs, long regnum) |
| 296 | { |
| 297 | if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS) |
| 298 | return NULL; |
| 299 | else |
| 300 | { |
| 301 | struct packet_reg *r = &rs->regs[regnum]; |
| 302 | gdb_assert (r->regnum == regnum); |
| 303 | return r; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static struct packet_reg * |
| 308 | packet_reg_from_pnum (struct remote_state *rs, LONGEST pnum) |
| 309 | { |
| 310 | int i; |
| 311 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
| 312 | { |
| 313 | struct packet_reg *r = &rs->regs[i]; |
| 314 | if (r->pnum == pnum) |
| 315 | return r; |
| 316 | } |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | /* FIXME: graces/2002-08-08: These variables should eventually be |
| 321 | bound to an instance of the target object (as in gdbarch-tdep()), |
| 322 | when such a thing exists. */ |
| 323 | |
| 324 | /* This is set to the data address of the access causing the target |
| 325 | to stop for a watchpoint. */ |
| 326 | static CORE_ADDR remote_watch_data_address; |
| 327 | |
| 328 | /* This is non-zero if taregt stopped for a watchpoint. */ |
| 329 | static int remote_stopped_by_watchpoint_p; |
| 330 | |
| 331 | |
| 332 | static struct target_ops remote_ops; |
| 333 | |
| 334 | static struct target_ops extended_remote_ops; |
| 335 | |
| 336 | /* Temporary target ops. Just like the remote_ops and |
| 337 | extended_remote_ops, but with asynchronous support. */ |
| 338 | static struct target_ops remote_async_ops; |
| 339 | |
| 340 | static struct target_ops extended_async_remote_ops; |
| 341 | |
| 342 | /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
| 343 | ``forever'' still use the normal timeout mechanism. This is |
| 344 | currently used by the ASYNC code to guarentee that target reads |
| 345 | during the initial connect always time-out. Once getpkt has been |
| 346 | modified to return a timeout indication and, in turn |
| 347 | remote_wait()/wait_for_inferior() have gained a timeout parameter |
| 348 | this can go away. */ |
| 349 | static int wait_forever_enabled_p = 1; |
| 350 | |
| 351 | |
| 352 | /* This variable chooses whether to send a ^C or a break when the user |
| 353 | requests program interruption. Although ^C is usually what remote |
| 354 | systems expect, and that is the default here, sometimes a break is |
| 355 | preferable instead. */ |
| 356 | |
| 357 | static int remote_break; |
| 358 | |
| 359 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
| 360 | remote_open knows that we don't have a file open when the program |
| 361 | starts. */ |
| 362 | static struct serial *remote_desc = NULL; |
| 363 | |
| 364 | /* This variable sets the number of bits in an address that are to be |
| 365 | sent in a memory ("M" or "m") packet. Normally, after stripping |
| 366 | leading zeros, the entire address would be sent. This variable |
| 367 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The |
| 368 | initial implementation of remote.c restricted the address sent in |
| 369 | memory packets to ``host::sizeof long'' bytes - (typically 32 |
| 370 | bits). Consequently, for 64 bit targets, the upper 32 bits of an |
| 371 | address was never sent. Since fixing this bug may cause a break in |
| 372 | some remote targets this variable is principly provided to |
| 373 | facilitate backward compatibility. */ |
| 374 | |
| 375 | static int remote_address_size; |
| 376 | |
| 377 | /* Tempoary to track who currently owns the terminal. See |
| 378 | target_async_terminal_* for more details. */ |
| 379 | |
| 380 | static int remote_async_terminal_ours_p; |
| 381 | |
| 382 | \f |
| 383 | /* User configurable variables for the number of characters in a |
| 384 | memory read/write packet. MIN ((rs->remote_packet_size), |
| 385 | rs->sizeof_g_packet) is the default. Some targets need smaller |
| 386 | values (fifo overruns, et.al.) and some users need larger values |
| 387 | (speed up transfers). The variables ``preferred_*'' (the user |
| 388 | request), ``current_*'' (what was actually set) and ``forced_*'' |
| 389 | (Positive - a soft limit, negative - a hard limit). */ |
| 390 | |
| 391 | struct memory_packet_config |
| 392 | { |
| 393 | char *name; |
| 394 | long size; |
| 395 | int fixed_p; |
| 396 | }; |
| 397 | |
| 398 | /* Compute the current size of a read/write packet. Since this makes |
| 399 | use of ``actual_register_packet_size'' the computation is dynamic. */ |
| 400 | |
| 401 | static long |
| 402 | get_memory_packet_size (struct memory_packet_config *config) |
| 403 | { |
| 404 | struct remote_state *rs = get_remote_state (); |
| 405 | /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk |
| 406 | law?) that some hosts don't cope very well with large alloca() |
| 407 | calls. Eventually the alloca() code will be replaced by calls to |
| 408 | xmalloc() and make_cleanups() allowing this restriction to either |
| 409 | be lifted or removed. */ |
| 410 | #ifndef MAX_REMOTE_PACKET_SIZE |
| 411 | #define MAX_REMOTE_PACKET_SIZE 16384 |
| 412 | #endif |
| 413 | /* NOTE: 20 ensures we can write at least one byte. */ |
| 414 | #ifndef MIN_REMOTE_PACKET_SIZE |
| 415 | #define MIN_REMOTE_PACKET_SIZE 20 |
| 416 | #endif |
| 417 | long what_they_get; |
| 418 | if (config->fixed_p) |
| 419 | { |
| 420 | if (config->size <= 0) |
| 421 | what_they_get = MAX_REMOTE_PACKET_SIZE; |
| 422 | else |
| 423 | what_they_get = config->size; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | what_they_get = (rs->remote_packet_size); |
| 428 | /* Limit the packet to the size specified by the user. */ |
| 429 | if (config->size > 0 |
| 430 | && what_they_get > config->size) |
| 431 | what_they_get = config->size; |
| 432 | /* Limit it to the size of the targets ``g'' response. */ |
| 433 | if ((rs->actual_register_packet_size) > 0 |
| 434 | && what_they_get > (rs->actual_register_packet_size)) |
| 435 | what_they_get = (rs->actual_register_packet_size); |
| 436 | } |
| 437 | if (what_they_get > MAX_REMOTE_PACKET_SIZE) |
| 438 | what_they_get = MAX_REMOTE_PACKET_SIZE; |
| 439 | if (what_they_get < MIN_REMOTE_PACKET_SIZE) |
| 440 | what_they_get = MIN_REMOTE_PACKET_SIZE; |
| 441 | return what_they_get; |
| 442 | } |
| 443 | |
| 444 | /* Update the size of a read/write packet. If they user wants |
| 445 | something really big then do a sanity check. */ |
| 446 | |
| 447 | static void |
| 448 | set_memory_packet_size (char *args, struct memory_packet_config *config) |
| 449 | { |
| 450 | int fixed_p = config->fixed_p; |
| 451 | long size = config->size; |
| 452 | if (args == NULL) |
| 453 | error (_("Argument required (integer, `fixed' or `limited').")); |
| 454 | else if (strcmp (args, "hard") == 0 |
| 455 | || strcmp (args, "fixed") == 0) |
| 456 | fixed_p = 1; |
| 457 | else if (strcmp (args, "soft") == 0 |
| 458 | || strcmp (args, "limit") == 0) |
| 459 | fixed_p = 0; |
| 460 | else |
| 461 | { |
| 462 | char *end; |
| 463 | size = strtoul (args, &end, 0); |
| 464 | if (args == end) |
| 465 | error (_("Invalid %s (bad syntax)."), config->name); |
| 466 | #if 0 |
| 467 | /* Instead of explicitly capping the size of a packet to |
| 468 | MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is |
| 469 | instead allowed to set the size to something arbitrarily |
| 470 | large. */ |
| 471 | if (size > MAX_REMOTE_PACKET_SIZE) |
| 472 | error (_("Invalid %s (too large)."), config->name); |
| 473 | #endif |
| 474 | } |
| 475 | /* Extra checks? */ |
| 476 | if (fixed_p && !config->fixed_p) |
| 477 | { |
| 478 | if (! query (_("The target may not be able to correctly handle a %s\n" |
| 479 | "of %ld bytes. Change the packet size? "), |
| 480 | config->name, size)) |
| 481 | error (_("Packet size not changed.")); |
| 482 | } |
| 483 | /* Update the config. */ |
| 484 | config->fixed_p = fixed_p; |
| 485 | config->size = size; |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | show_memory_packet_size (struct memory_packet_config *config) |
| 490 | { |
| 491 | printf_filtered (_("The %s is %ld. "), config->name, config->size); |
| 492 | if (config->fixed_p) |
| 493 | printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
| 494 | get_memory_packet_size (config)); |
| 495 | else |
| 496 | printf_filtered (_("Packets are limited to %ld bytes.\n"), |
| 497 | get_memory_packet_size (config)); |
| 498 | } |
| 499 | |
| 500 | static struct memory_packet_config memory_write_packet_config = |
| 501 | { |
| 502 | "memory-write-packet-size", |
| 503 | }; |
| 504 | |
| 505 | static void |
| 506 | set_memory_write_packet_size (char *args, int from_tty) |
| 507 | { |
| 508 | set_memory_packet_size (args, &memory_write_packet_config); |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | show_memory_write_packet_size (char *args, int from_tty) |
| 513 | { |
| 514 | show_memory_packet_size (&memory_write_packet_config); |
| 515 | } |
| 516 | |
| 517 | static long |
| 518 | get_memory_write_packet_size (void) |
| 519 | { |
| 520 | return get_memory_packet_size (&memory_write_packet_config); |
| 521 | } |
| 522 | |
| 523 | static struct memory_packet_config memory_read_packet_config = |
| 524 | { |
| 525 | "memory-read-packet-size", |
| 526 | }; |
| 527 | |
| 528 | static void |
| 529 | set_memory_read_packet_size (char *args, int from_tty) |
| 530 | { |
| 531 | set_memory_packet_size (args, &memory_read_packet_config); |
| 532 | } |
| 533 | |
| 534 | static void |
| 535 | show_memory_read_packet_size (char *args, int from_tty) |
| 536 | { |
| 537 | show_memory_packet_size (&memory_read_packet_config); |
| 538 | } |
| 539 | |
| 540 | static long |
| 541 | get_memory_read_packet_size (void) |
| 542 | { |
| 543 | struct remote_state *rs = get_remote_state (); |
| 544 | long size = get_memory_packet_size (&memory_read_packet_config); |
| 545 | /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an |
| 546 | extra buffer size argument before the memory read size can be |
| 547 | increased beyond (rs->remote_packet_size). */ |
| 548 | if (size > (rs->remote_packet_size)) |
| 549 | size = (rs->remote_packet_size); |
| 550 | return size; |
| 551 | } |
| 552 | |
| 553 | \f |
| 554 | /* Generic configuration support for packets the stub optionally |
| 555 | supports. Allows the user to specify the use of the packet as well |
| 556 | as allowing GDB to auto-detect support in the remote stub. */ |
| 557 | |
| 558 | enum packet_support |
| 559 | { |
| 560 | PACKET_SUPPORT_UNKNOWN = 0, |
| 561 | PACKET_ENABLE, |
| 562 | PACKET_DISABLE |
| 563 | }; |
| 564 | |
| 565 | struct packet_config |
| 566 | { |
| 567 | char *name; |
| 568 | char *title; |
| 569 | enum auto_boolean detect; |
| 570 | enum packet_support support; |
| 571 | }; |
| 572 | |
| 573 | /* Analyze a packet's return value and update the packet config |
| 574 | accordingly. */ |
| 575 | |
| 576 | enum packet_result |
| 577 | { |
| 578 | PACKET_ERROR, |
| 579 | PACKET_OK, |
| 580 | PACKET_UNKNOWN |
| 581 | }; |
| 582 | |
| 583 | static void |
| 584 | update_packet_config (struct packet_config *config) |
| 585 | { |
| 586 | switch (config->detect) |
| 587 | { |
| 588 | case AUTO_BOOLEAN_TRUE: |
| 589 | config->support = PACKET_ENABLE; |
| 590 | break; |
| 591 | case AUTO_BOOLEAN_FALSE: |
| 592 | config->support = PACKET_DISABLE; |
| 593 | break; |
| 594 | case AUTO_BOOLEAN_AUTO: |
| 595 | config->support = PACKET_SUPPORT_UNKNOWN; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | static void |
| 601 | show_packet_config_cmd (struct packet_config *config) |
| 602 | { |
| 603 | char *support = "internal-error"; |
| 604 | switch (config->support) |
| 605 | { |
| 606 | case PACKET_ENABLE: |
| 607 | support = "enabled"; |
| 608 | break; |
| 609 | case PACKET_DISABLE: |
| 610 | support = "disabled"; |
| 611 | break; |
| 612 | case PACKET_SUPPORT_UNKNOWN: |
| 613 | support = "unknown"; |
| 614 | break; |
| 615 | } |
| 616 | switch (config->detect) |
| 617 | { |
| 618 | case AUTO_BOOLEAN_AUTO: |
| 619 | printf_filtered (_("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n"), |
| 620 | config->name, config->title, support); |
| 621 | break; |
| 622 | case AUTO_BOOLEAN_TRUE: |
| 623 | case AUTO_BOOLEAN_FALSE: |
| 624 | printf_filtered (_("Support for remote protocol `%s' (%s) packet is currently %s.\n"), |
| 625 | config->name, config->title, support); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | static void |
| 631 | add_packet_config_cmd (struct packet_config *config, |
| 632 | char *name, |
| 633 | char *title, |
| 634 | cmd_sfunc_ftype *set_func, |
| 635 | show_value_ftype *show_func, |
| 636 | struct cmd_list_element **set_remote_list, |
| 637 | struct cmd_list_element **show_remote_list, |
| 638 | int legacy) |
| 639 | { |
| 640 | char *set_doc; |
| 641 | char *show_doc; |
| 642 | char *cmd_name; |
| 643 | |
| 644 | config->name = name; |
| 645 | config->title = title; |
| 646 | config->detect = AUTO_BOOLEAN_AUTO; |
| 647 | config->support = PACKET_SUPPORT_UNKNOWN; |
| 648 | set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
| 649 | name, title); |
| 650 | show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet", |
| 651 | name, title); |
| 652 | /* set/show TITLE-packet {auto,on,off} */ |
| 653 | cmd_name = xstrprintf ("%s-packet", title); |
| 654 | add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
| 655 | &config->detect, set_doc, show_doc, NULL, /* help_doc */ |
| 656 | set_func, show_func, |
| 657 | set_remote_list, show_remote_list); |
| 658 | /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
| 659 | if (legacy) |
| 660 | { |
| 661 | char *legacy_name; |
| 662 | legacy_name = xstrprintf ("%s-packet", name); |
| 663 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
| 664 | set_remote_list); |
| 665 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
| 666 | show_remote_list); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | static enum packet_result |
| 671 | packet_ok (const char *buf, struct packet_config *config) |
| 672 | { |
| 673 | if (buf[0] != '\0') |
| 674 | { |
| 675 | /* The stub recognized the packet request. Check that the |
| 676 | operation succeeded. */ |
| 677 | switch (config->support) |
| 678 | { |
| 679 | case PACKET_SUPPORT_UNKNOWN: |
| 680 | if (remote_debug) |
| 681 | fprintf_unfiltered (gdb_stdlog, |
| 682 | "Packet %s (%s) is supported\n", |
| 683 | config->name, config->title); |
| 684 | config->support = PACKET_ENABLE; |
| 685 | break; |
| 686 | case PACKET_DISABLE: |
| 687 | internal_error (__FILE__, __LINE__, |
| 688 | _("packet_ok: attempt to use a disabled packet")); |
| 689 | break; |
| 690 | case PACKET_ENABLE: |
| 691 | break; |
| 692 | } |
| 693 | if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0') |
| 694 | /* "OK" - definitly OK. */ |
| 695 | return PACKET_OK; |
| 696 | if (buf[0] == 'E' |
| 697 | && isxdigit (buf[1]) && isxdigit (buf[2]) |
| 698 | && buf[3] == '\0') |
| 699 | /* "Enn" - definitly an error. */ |
| 700 | return PACKET_ERROR; |
| 701 | /* The packet may or may not be OK. Just assume it is. */ |
| 702 | return PACKET_OK; |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | /* The stub does not support the packet. */ |
| 707 | switch (config->support) |
| 708 | { |
| 709 | case PACKET_ENABLE: |
| 710 | if (config->detect == AUTO_BOOLEAN_AUTO) |
| 711 | /* If the stub previously indicated that the packet was |
| 712 | supported then there is a protocol error.. */ |
| 713 | error (_("Protocol error: %s (%s) conflicting enabled responses."), |
| 714 | config->name, config->title); |
| 715 | else |
| 716 | /* The user set it wrong. */ |
| 717 | error (_("Enabled packet %s (%s) not recognized by stub"), |
| 718 | config->name, config->title); |
| 719 | break; |
| 720 | case PACKET_SUPPORT_UNKNOWN: |
| 721 | if (remote_debug) |
| 722 | fprintf_unfiltered (gdb_stdlog, |
| 723 | "Packet %s (%s) is NOT supported\n", |
| 724 | config->name, config->title); |
| 725 | config->support = PACKET_DISABLE; |
| 726 | break; |
| 727 | case PACKET_DISABLE: |
| 728 | break; |
| 729 | } |
| 730 | return PACKET_UNKNOWN; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /* Should we try the 'vCont' (descriptive resume) request? */ |
| 735 | static struct packet_config remote_protocol_vcont; |
| 736 | |
| 737 | static void |
| 738 | set_remote_protocol_vcont_packet_cmd (char *args, int from_tty, |
| 739 | struct cmd_list_element *c) |
| 740 | { |
| 741 | update_packet_config (&remote_protocol_vcont); |
| 742 | } |
| 743 | |
| 744 | static void |
| 745 | show_remote_protocol_vcont_packet_cmd (struct ui_file *file, int from_tty, |
| 746 | struct cmd_list_element *c, |
| 747 | const char *value) |
| 748 | { |
| 749 | show_packet_config_cmd (&remote_protocol_vcont); |
| 750 | } |
| 751 | |
| 752 | /* Should we try the 'qSymbol' (target symbol lookup service) request? */ |
| 753 | static struct packet_config remote_protocol_qSymbol; |
| 754 | |
| 755 | static void |
| 756 | set_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty, |
| 757 | struct cmd_list_element *c) |
| 758 | { |
| 759 | update_packet_config (&remote_protocol_qSymbol); |
| 760 | } |
| 761 | |
| 762 | static void |
| 763 | show_remote_protocol_qSymbol_packet_cmd (struct ui_file *file, int from_tty, |
| 764 | struct cmd_list_element *c, |
| 765 | const char *value) |
| 766 | { |
| 767 | show_packet_config_cmd (&remote_protocol_qSymbol); |
| 768 | } |
| 769 | |
| 770 | /* Should we try the 'P' (set register) request? */ |
| 771 | |
| 772 | static struct packet_config remote_protocol_P; |
| 773 | |
| 774 | static void |
| 775 | set_remote_protocol_P_packet_cmd (char *args, int from_tty, |
| 776 | struct cmd_list_element *c) |
| 777 | { |
| 778 | update_packet_config (&remote_protocol_P); |
| 779 | } |
| 780 | |
| 781 | static void |
| 782 | show_remote_protocol_P_packet_cmd (struct ui_file *file, int from_tty, |
| 783 | struct cmd_list_element *c, |
| 784 | const char *value) |
| 785 | { |
| 786 | show_packet_config_cmd (&remote_protocol_P); |
| 787 | } |
| 788 | |
| 789 | /* Should we try one of the 'Z' requests? */ |
| 790 | |
| 791 | enum Z_packet_type |
| 792 | { |
| 793 | Z_PACKET_SOFTWARE_BP, |
| 794 | Z_PACKET_HARDWARE_BP, |
| 795 | Z_PACKET_WRITE_WP, |
| 796 | Z_PACKET_READ_WP, |
| 797 | Z_PACKET_ACCESS_WP, |
| 798 | NR_Z_PACKET_TYPES |
| 799 | }; |
| 800 | |
| 801 | static struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES]; |
| 802 | |
| 803 | /* FIXME: Instead of having all these boiler plate functions, the |
| 804 | command callback should include a context argument. */ |
| 805 | |
| 806 | static void |
| 807 | set_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty, |
| 808 | struct cmd_list_element *c) |
| 809 | { |
| 810 | update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]); |
| 811 | } |
| 812 | |
| 813 | static void |
| 814 | show_remote_protocol_Z_software_bp_packet_cmd (struct ui_file *file, int from_tty, |
| 815 | struct cmd_list_element *c, |
| 816 | const char *value) |
| 817 | { |
| 818 | show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]); |
| 819 | } |
| 820 | |
| 821 | static void |
| 822 | set_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty, |
| 823 | struct cmd_list_element *c) |
| 824 | { |
| 825 | update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]); |
| 826 | } |
| 827 | |
| 828 | static void |
| 829 | show_remote_protocol_Z_hardware_bp_packet_cmd (struct ui_file *file, int from_tty, |
| 830 | struct cmd_list_element *c, |
| 831 | const char *value) |
| 832 | { |
| 833 | show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]); |
| 834 | } |
| 835 | |
| 836 | static void |
| 837 | set_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty, |
| 838 | struct cmd_list_element *c) |
| 839 | { |
| 840 | update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]); |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | show_remote_protocol_Z_write_wp_packet_cmd (struct ui_file *file, int from_tty, |
| 845 | struct cmd_list_element *c, |
| 846 | const char *value) |
| 847 | { |
| 848 | show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]); |
| 849 | } |
| 850 | |
| 851 | static void |
| 852 | set_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty, |
| 853 | struct cmd_list_element *c) |
| 854 | { |
| 855 | update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]); |
| 856 | } |
| 857 | |
| 858 | static void |
| 859 | show_remote_protocol_Z_read_wp_packet_cmd (struct ui_file *file, int from_tty, |
| 860 | struct cmd_list_element *c, |
| 861 | const char *value) |
| 862 | { |
| 863 | show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]); |
| 864 | } |
| 865 | |
| 866 | static void |
| 867 | set_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty, |
| 868 | struct cmd_list_element *c) |
| 869 | { |
| 870 | update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]); |
| 871 | } |
| 872 | |
| 873 | static void |
| 874 | show_remote_protocol_Z_access_wp_packet_cmd (struct ui_file *file, int from_tty, |
| 875 | struct cmd_list_element *c, |
| 876 | const char *value) |
| 877 | { |
| 878 | show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]); |
| 879 | } |
| 880 | |
| 881 | /* For compatibility with older distributions. Provide a ``set remote |
| 882 | Z-packet ...'' command that updates all the Z packet types. */ |
| 883 | |
| 884 | static enum auto_boolean remote_Z_packet_detect; |
| 885 | |
| 886 | static void |
| 887 | set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
| 888 | struct cmd_list_element *c) |
| 889 | { |
| 890 | int i; |
| 891 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
| 892 | { |
| 893 | remote_protocol_Z[i].detect = remote_Z_packet_detect; |
| 894 | update_packet_config (&remote_protocol_Z[i]); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | static void |
| 899 | show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
| 900 | struct cmd_list_element *c, |
| 901 | const char *value) |
| 902 | { |
| 903 | int i; |
| 904 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
| 905 | { |
| 906 | show_packet_config_cmd (&remote_protocol_Z[i]); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* Should we try the 'X' (remote binary download) packet? |
| 911 | |
| 912 | This variable (available to the user via "set remote X-packet") |
| 913 | dictates whether downloads are sent in binary (via the 'X' packet). |
| 914 | We assume that the stub can, and attempt to do it. This will be |
| 915 | cleared if the stub does not understand it. This switch is still |
| 916 | needed, though in cases when the packet is supported in the stub, |
| 917 | but the connection does not allow it (i.e., 7-bit serial connection |
| 918 | only). */ |
| 919 | |
| 920 | static struct packet_config remote_protocol_binary_download; |
| 921 | |
| 922 | /* Should we try the 'ThreadInfo' query packet? |
| 923 | |
| 924 | This variable (NOT available to the user: auto-detect only!) |
| 925 | determines whether GDB will use the new, simpler "ThreadInfo" |
| 926 | query or the older, more complex syntax for thread queries. |
| 927 | This is an auto-detect variable (set to true at each connect, |
| 928 | and set to false when the target fails to recognize it). */ |
| 929 | |
| 930 | static int use_threadinfo_query; |
| 931 | static int use_threadextra_query; |
| 932 | |
| 933 | static void |
| 934 | set_remote_protocol_binary_download_cmd (char *args, |
| 935 | int from_tty, |
| 936 | struct cmd_list_element *c) |
| 937 | { |
| 938 | update_packet_config (&remote_protocol_binary_download); |
| 939 | } |
| 940 | |
| 941 | static void |
| 942 | show_remote_protocol_binary_download_cmd (struct ui_file *file, int from_tty, |
| 943 | struct cmd_list_element *c, |
| 944 | const char *value) |
| 945 | { |
| 946 | show_packet_config_cmd (&remote_protocol_binary_download); |
| 947 | } |
| 948 | |
| 949 | /* Should we try the 'qPart:auxv' (target auxiliary vector read) request? */ |
| 950 | static struct packet_config remote_protocol_qPart_auxv; |
| 951 | |
| 952 | static void |
| 953 | set_remote_protocol_qPart_auxv_packet_cmd (char *args, int from_tty, |
| 954 | struct cmd_list_element *c) |
| 955 | { |
| 956 | update_packet_config (&remote_protocol_qPart_auxv); |
| 957 | } |
| 958 | |
| 959 | static void |
| 960 | show_remote_protocol_qPart_auxv_packet_cmd (struct ui_file *file, int from_tty, |
| 961 | struct cmd_list_element *c, |
| 962 | const char *value) |
| 963 | { |
| 964 | show_packet_config_cmd (&remote_protocol_qPart_auxv); |
| 965 | } |
| 966 | |
| 967 | /* Should we try the 'qGetTLSAddr' (Get Thread Local Storage Address) request? */ |
| 968 | static struct packet_config remote_protocol_qGetTLSAddr; |
| 969 | |
| 970 | static void |
| 971 | set_remote_protocol_qGetTLSAddr_packet_cmd (char *args, int from_tty, |
| 972 | struct cmd_list_element *c) |
| 973 | { |
| 974 | update_packet_config (&remote_protocol_qGetTLSAddr); |
| 975 | } |
| 976 | |
| 977 | static void |
| 978 | show_remote_protocol_qGetTLSAddr_packet_cmd (struct ui_file *file, int from_tty, |
| 979 | struct cmd_list_element *c, |
| 980 | const char *value) |
| 981 | { |
| 982 | show_packet_config_cmd (&remote_protocol_qGetTLSAddr); |
| 983 | } |
| 984 | |
| 985 | static struct packet_config remote_protocol_p; |
| 986 | |
| 987 | static void |
| 988 | set_remote_protocol_p_packet_cmd (char *args, int from_tty, |
| 989 | struct cmd_list_element *c) |
| 990 | { |
| 991 | update_packet_config (&remote_protocol_p); |
| 992 | } |
| 993 | |
| 994 | static void |
| 995 | show_remote_protocol_p_packet_cmd (struct ui_file *file, int from_tty, |
| 996 | struct cmd_list_element *c, |
| 997 | const char *value) |
| 998 | { |
| 999 | show_packet_config_cmd (&remote_protocol_p); |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | /* Tokens for use by the asynchronous signal handlers for SIGINT. */ |
| 1005 | static void *sigint_remote_twice_token; |
| 1006 | static void *sigint_remote_token; |
| 1007 | |
| 1008 | /* These are pointers to hook functions that may be set in order to |
| 1009 | modify resume/wait behavior for a particular architecture. */ |
| 1010 | |
| 1011 | void (*deprecated_target_resume_hook) (void); |
| 1012 | void (*deprecated_target_wait_loop_hook) (void); |
| 1013 | \f |
| 1014 | |
| 1015 | |
| 1016 | /* These are the threads which we last sent to the remote system. |
| 1017 | -1 for all or -2 for not sent yet. */ |
| 1018 | static int general_thread; |
| 1019 | static int continue_thread; |
| 1020 | |
| 1021 | /* Call this function as a result of |
| 1022 | 1) A halt indication (T packet) containing a thread id |
| 1023 | 2) A direct query of currthread |
| 1024 | 3) Successful execution of set thread |
| 1025 | */ |
| 1026 | |
| 1027 | static void |
| 1028 | record_currthread (int currthread) |
| 1029 | { |
| 1030 | general_thread = currthread; |
| 1031 | |
| 1032 | /* If this is a new thread, add it to GDB's thread list. |
| 1033 | If we leave it up to WFI to do this, bad things will happen. */ |
| 1034 | if (!in_thread_list (pid_to_ptid (currthread))) |
| 1035 | { |
| 1036 | add_thread (pid_to_ptid (currthread)); |
| 1037 | ui_out_text (uiout, "[New "); |
| 1038 | ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread))); |
| 1039 | ui_out_text (uiout, "]\n"); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | #define MAGIC_NULL_PID 42000 |
| 1044 | |
| 1045 | static void |
| 1046 | set_thread (int th, int gen) |
| 1047 | { |
| 1048 | struct remote_state *rs = get_remote_state (); |
| 1049 | char *buf = alloca (rs->remote_packet_size); |
| 1050 | int state = gen ? general_thread : continue_thread; |
| 1051 | |
| 1052 | if (state == th) |
| 1053 | return; |
| 1054 | |
| 1055 | buf[0] = 'H'; |
| 1056 | buf[1] = gen ? 'g' : 'c'; |
| 1057 | if (th == MAGIC_NULL_PID) |
| 1058 | { |
| 1059 | buf[2] = '0'; |
| 1060 | buf[3] = '\0'; |
| 1061 | } |
| 1062 | else if (th < 0) |
| 1063 | sprintf (&buf[2], "-%x", -th); |
| 1064 | else |
| 1065 | sprintf (&buf[2], "%x", th); |
| 1066 | putpkt (buf); |
| 1067 | getpkt (buf, (rs->remote_packet_size), 0); |
| 1068 | if (gen) |
| 1069 | general_thread = th; |
| 1070 | else |
| 1071 | continue_thread = th; |
| 1072 | } |
| 1073 | \f |
| 1074 | /* Return nonzero if the thread TH is still alive on the remote system. */ |
| 1075 | |
| 1076 | static int |
| 1077 | remote_thread_alive (ptid_t ptid) |
| 1078 | { |
| 1079 | int tid = PIDGET (ptid); |
| 1080 | char buf[16]; |
| 1081 | |
| 1082 | if (tid < 0) |
| 1083 | sprintf (buf, "T-%08x", -tid); |
| 1084 | else |
| 1085 | sprintf (buf, "T%08x", tid); |
| 1086 | putpkt (buf); |
| 1087 | getpkt (buf, sizeof (buf), 0); |
| 1088 | return (buf[0] == 'O' && buf[1] == 'K'); |
| 1089 | } |
| 1090 | |
| 1091 | /* About these extended threadlist and threadinfo packets. They are |
| 1092 | variable length packets but, the fields within them are often fixed |
| 1093 | length. They are redundent enough to send over UDP as is the |
| 1094 | remote protocol in general. There is a matching unit test module |
| 1095 | in libstub. */ |
| 1096 | |
| 1097 | #define OPAQUETHREADBYTES 8 |
| 1098 | |
| 1099 | /* a 64 bit opaque identifier */ |
| 1100 | typedef unsigned char threadref[OPAQUETHREADBYTES]; |
| 1101 | |
| 1102 | /* WARNING: This threadref data structure comes from the remote O.S., |
| 1103 | libstub protocol encoding, and remote.c. it is not particularly |
| 1104 | changable. */ |
| 1105 | |
| 1106 | /* Right now, the internal structure is int. We want it to be bigger. |
| 1107 | Plan to fix this. |
| 1108 | */ |
| 1109 | |
| 1110 | typedef int gdb_threadref; /* Internal GDB thread reference. */ |
| 1111 | |
| 1112 | /* gdb_ext_thread_info is an internal GDB data structure which is |
| 1113 | equivalint to the reply of the remote threadinfo packet. */ |
| 1114 | |
| 1115 | struct gdb_ext_thread_info |
| 1116 | { |
| 1117 | threadref threadid; /* External form of thread reference. */ |
| 1118 | int active; /* Has state interesting to GDB? |
| 1119 | regs, stack. */ |
| 1120 | char display[256]; /* Brief state display, name, |
| 1121 | blocked/syspended. */ |
| 1122 | char shortname[32]; /* To be used to name threads. */ |
| 1123 | char more_display[256]; /* Long info, statistics, queue depth, |
| 1124 | whatever. */ |
| 1125 | }; |
| 1126 | |
| 1127 | /* The volume of remote transfers can be limited by submitting |
| 1128 | a mask containing bits specifying the desired information. |
| 1129 | Use a union of these values as the 'selection' parameter to |
| 1130 | get_thread_info. FIXME: Make these TAG names more thread specific. |
| 1131 | */ |
| 1132 | |
| 1133 | #define TAG_THREADID 1 |
| 1134 | #define TAG_EXISTS 2 |
| 1135 | #define TAG_DISPLAY 4 |
| 1136 | #define TAG_THREADNAME 8 |
| 1137 | #define TAG_MOREDISPLAY 16 |
| 1138 | |
| 1139 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
| 1140 | |
| 1141 | char *unpack_varlen_hex (char *buff, ULONGEST *result); |
| 1142 | |
| 1143 | static char *unpack_nibble (char *buf, int *val); |
| 1144 | |
| 1145 | static char *pack_nibble (char *buf, int nibble); |
| 1146 | |
| 1147 | static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte); |
| 1148 | |
| 1149 | static char *unpack_byte (char *buf, int *value); |
| 1150 | |
| 1151 | static char *pack_int (char *buf, int value); |
| 1152 | |
| 1153 | static char *unpack_int (char *buf, int *value); |
| 1154 | |
| 1155 | static char *unpack_string (char *src, char *dest, int length); |
| 1156 | |
| 1157 | static char *pack_threadid (char *pkt, threadref *id); |
| 1158 | |
| 1159 | static char *unpack_threadid (char *inbuf, threadref *id); |
| 1160 | |
| 1161 | void int_to_threadref (threadref *id, int value); |
| 1162 | |
| 1163 | static int threadref_to_int (threadref *ref); |
| 1164 | |
| 1165 | static void copy_threadref (threadref *dest, threadref *src); |
| 1166 | |
| 1167 | static int threadmatch (threadref *dest, threadref *src); |
| 1168 | |
| 1169 | static char *pack_threadinfo_request (char *pkt, int mode, |
| 1170 | threadref *id); |
| 1171 | |
| 1172 | static int remote_unpack_thread_info_response (char *pkt, |
| 1173 | threadref *expectedref, |
| 1174 | struct gdb_ext_thread_info |
| 1175 | *info); |
| 1176 | |
| 1177 | |
| 1178 | static int remote_get_threadinfo (threadref *threadid, |
| 1179 | int fieldset, /*TAG mask */ |
| 1180 | struct gdb_ext_thread_info *info); |
| 1181 | |
| 1182 | static char *pack_threadlist_request (char *pkt, int startflag, |
| 1183 | int threadcount, |
| 1184 | threadref *nextthread); |
| 1185 | |
| 1186 | static int parse_threadlist_response (char *pkt, |
| 1187 | int result_limit, |
| 1188 | threadref *original_echo, |
| 1189 | threadref *resultlist, |
| 1190 | int *doneflag); |
| 1191 | |
| 1192 | static int remote_get_threadlist (int startflag, |
| 1193 | threadref *nextthread, |
| 1194 | int result_limit, |
| 1195 | int *done, |
| 1196 | int *result_count, |
| 1197 | threadref *threadlist); |
| 1198 | |
| 1199 | typedef int (*rmt_thread_action) (threadref *ref, void *context); |
| 1200 | |
| 1201 | static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
| 1202 | void *context, int looplimit); |
| 1203 | |
| 1204 | static int remote_newthread_step (threadref *ref, void *context); |
| 1205 | |
| 1206 | /* Encode 64 bits in 16 chars of hex. */ |
| 1207 | |
| 1208 | static const char hexchars[] = "0123456789abcdef"; |
| 1209 | |
| 1210 | static int |
| 1211 | ishex (int ch, int *val) |
| 1212 | { |
| 1213 | if ((ch >= 'a') && (ch <= 'f')) |
| 1214 | { |
| 1215 | *val = ch - 'a' + 10; |
| 1216 | return 1; |
| 1217 | } |
| 1218 | if ((ch >= 'A') && (ch <= 'F')) |
| 1219 | { |
| 1220 | *val = ch - 'A' + 10; |
| 1221 | return 1; |
| 1222 | } |
| 1223 | if ((ch >= '0') && (ch <= '9')) |
| 1224 | { |
| 1225 | *val = ch - '0'; |
| 1226 | return 1; |
| 1227 | } |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static int |
| 1232 | stubhex (int ch) |
| 1233 | { |
| 1234 | if (ch >= 'a' && ch <= 'f') |
| 1235 | return ch - 'a' + 10; |
| 1236 | if (ch >= '0' && ch <= '9') |
| 1237 | return ch - '0'; |
| 1238 | if (ch >= 'A' && ch <= 'F') |
| 1239 | return ch - 'A' + 10; |
| 1240 | return -1; |
| 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | stub_unpack_int (char *buff, int fieldlength) |
| 1245 | { |
| 1246 | int nibble; |
| 1247 | int retval = 0; |
| 1248 | |
| 1249 | while (fieldlength) |
| 1250 | { |
| 1251 | nibble = stubhex (*buff++); |
| 1252 | retval |= nibble; |
| 1253 | fieldlength--; |
| 1254 | if (fieldlength) |
| 1255 | retval = retval << 4; |
| 1256 | } |
| 1257 | return retval; |
| 1258 | } |
| 1259 | |
| 1260 | char * |
| 1261 | unpack_varlen_hex (char *buff, /* packet to parse */ |
| 1262 | ULONGEST *result) |
| 1263 | { |
| 1264 | int nibble; |
| 1265 | int retval = 0; |
| 1266 | |
| 1267 | while (ishex (*buff, &nibble)) |
| 1268 | { |
| 1269 | buff++; |
| 1270 | retval = retval << 4; |
| 1271 | retval |= nibble & 0x0f; |
| 1272 | } |
| 1273 | *result = retval; |
| 1274 | return buff; |
| 1275 | } |
| 1276 | |
| 1277 | static char * |
| 1278 | unpack_nibble (char *buf, int *val) |
| 1279 | { |
| 1280 | ishex (*buf++, val); |
| 1281 | return buf; |
| 1282 | } |
| 1283 | |
| 1284 | static char * |
| 1285 | pack_nibble (char *buf, int nibble) |
| 1286 | { |
| 1287 | *buf++ = hexchars[(nibble & 0x0f)]; |
| 1288 | return buf; |
| 1289 | } |
| 1290 | |
| 1291 | static char * |
| 1292 | pack_hex_byte (char *pkt, int byte) |
| 1293 | { |
| 1294 | *pkt++ = hexchars[(byte >> 4) & 0xf]; |
| 1295 | *pkt++ = hexchars[(byte & 0xf)]; |
| 1296 | return pkt; |
| 1297 | } |
| 1298 | |
| 1299 | static char * |
| 1300 | unpack_byte (char *buf, int *value) |
| 1301 | { |
| 1302 | *value = stub_unpack_int (buf, 2); |
| 1303 | return buf + 2; |
| 1304 | } |
| 1305 | |
| 1306 | static char * |
| 1307 | pack_int (char *buf, int value) |
| 1308 | { |
| 1309 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); |
| 1310 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); |
| 1311 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); |
| 1312 | buf = pack_hex_byte (buf, (value & 0xff)); |
| 1313 | return buf; |
| 1314 | } |
| 1315 | |
| 1316 | static char * |
| 1317 | unpack_int (char *buf, int *value) |
| 1318 | { |
| 1319 | *value = stub_unpack_int (buf, 8); |
| 1320 | return buf + 8; |
| 1321 | } |
| 1322 | |
| 1323 | #if 0 /* Currently unused, uncomment when needed. */ |
| 1324 | static char *pack_string (char *pkt, char *string); |
| 1325 | |
| 1326 | static char * |
| 1327 | pack_string (char *pkt, char *string) |
| 1328 | { |
| 1329 | char ch; |
| 1330 | int len; |
| 1331 | |
| 1332 | len = strlen (string); |
| 1333 | if (len > 200) |
| 1334 | len = 200; /* Bigger than most GDB packets, junk??? */ |
| 1335 | pkt = pack_hex_byte (pkt, len); |
| 1336 | while (len-- > 0) |
| 1337 | { |
| 1338 | ch = *string++; |
| 1339 | if ((ch == '\0') || (ch == '#')) |
| 1340 | ch = '*'; /* Protect encapsulation. */ |
| 1341 | *pkt++ = ch; |
| 1342 | } |
| 1343 | return pkt; |
| 1344 | } |
| 1345 | #endif /* 0 (unused) */ |
| 1346 | |
| 1347 | static char * |
| 1348 | unpack_string (char *src, char *dest, int length) |
| 1349 | { |
| 1350 | while (length--) |
| 1351 | *dest++ = *src++; |
| 1352 | *dest = '\0'; |
| 1353 | return src; |
| 1354 | } |
| 1355 | |
| 1356 | static char * |
| 1357 | pack_threadid (char *pkt, threadref *id) |
| 1358 | { |
| 1359 | char *limit; |
| 1360 | unsigned char *altid; |
| 1361 | |
| 1362 | altid = (unsigned char *) id; |
| 1363 | limit = pkt + BUF_THREAD_ID_SIZE; |
| 1364 | while (pkt < limit) |
| 1365 | pkt = pack_hex_byte (pkt, *altid++); |
| 1366 | return pkt; |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | static char * |
| 1371 | unpack_threadid (char *inbuf, threadref *id) |
| 1372 | { |
| 1373 | char *altref; |
| 1374 | char *limit = inbuf + BUF_THREAD_ID_SIZE; |
| 1375 | int x, y; |
| 1376 | |
| 1377 | altref = (char *) id; |
| 1378 | |
| 1379 | while (inbuf < limit) |
| 1380 | { |
| 1381 | x = stubhex (*inbuf++); |
| 1382 | y = stubhex (*inbuf++); |
| 1383 | *altref++ = (x << 4) | y; |
| 1384 | } |
| 1385 | return inbuf; |
| 1386 | } |
| 1387 | |
| 1388 | /* Externally, threadrefs are 64 bits but internally, they are still |
| 1389 | ints. This is due to a mismatch of specifications. We would like |
| 1390 | to use 64bit thread references internally. This is an adapter |
| 1391 | function. */ |
| 1392 | |
| 1393 | void |
| 1394 | int_to_threadref (threadref *id, int value) |
| 1395 | { |
| 1396 | unsigned char *scan; |
| 1397 | |
| 1398 | scan = (unsigned char *) id; |
| 1399 | { |
| 1400 | int i = 4; |
| 1401 | while (i--) |
| 1402 | *scan++ = 0; |
| 1403 | } |
| 1404 | *scan++ = (value >> 24) & 0xff; |
| 1405 | *scan++ = (value >> 16) & 0xff; |
| 1406 | *scan++ = (value >> 8) & 0xff; |
| 1407 | *scan++ = (value & 0xff); |
| 1408 | } |
| 1409 | |
| 1410 | static int |
| 1411 | threadref_to_int (threadref *ref) |
| 1412 | { |
| 1413 | int i, value = 0; |
| 1414 | unsigned char *scan; |
| 1415 | |
| 1416 | scan = (char *) ref; |
| 1417 | scan += 4; |
| 1418 | i = 4; |
| 1419 | while (i-- > 0) |
| 1420 | value = (value << 8) | ((*scan++) & 0xff); |
| 1421 | return value; |
| 1422 | } |
| 1423 | |
| 1424 | static void |
| 1425 | copy_threadref (threadref *dest, threadref *src) |
| 1426 | { |
| 1427 | int i; |
| 1428 | unsigned char *csrc, *cdest; |
| 1429 | |
| 1430 | csrc = (unsigned char *) src; |
| 1431 | cdest = (unsigned char *) dest; |
| 1432 | i = 8; |
| 1433 | while (i--) |
| 1434 | *cdest++ = *csrc++; |
| 1435 | } |
| 1436 | |
| 1437 | static int |
| 1438 | threadmatch (threadref *dest, threadref *src) |
| 1439 | { |
| 1440 | /* Things are broken right now, so just assume we got a match. */ |
| 1441 | #if 0 |
| 1442 | unsigned char *srcp, *destp; |
| 1443 | int i, result; |
| 1444 | srcp = (char *) src; |
| 1445 | destp = (char *) dest; |
| 1446 | |
| 1447 | result = 1; |
| 1448 | while (i-- > 0) |
| 1449 | result &= (*srcp++ == *destp++) ? 1 : 0; |
| 1450 | return result; |
| 1451 | #endif |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | threadid:1, # always request threadid |
| 1457 | context_exists:2, |
| 1458 | display:4, |
| 1459 | unique_name:8, |
| 1460 | more_display:16 |
| 1461 | */ |
| 1462 | |
| 1463 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ |
| 1464 | |
| 1465 | static char * |
| 1466 | pack_threadinfo_request (char *pkt, int mode, threadref *id) |
| 1467 | { |
| 1468 | *pkt++ = 'q'; /* Info Query */ |
| 1469 | *pkt++ = 'P'; /* process or thread info */ |
| 1470 | pkt = pack_int (pkt, mode); /* mode */ |
| 1471 | pkt = pack_threadid (pkt, id); /* threadid */ |
| 1472 | *pkt = '\0'; /* terminate */ |
| 1473 | return pkt; |
| 1474 | } |
| 1475 | |
| 1476 | /* These values tag the fields in a thread info response packet. */ |
| 1477 | /* Tagging the fields allows us to request specific fields and to |
| 1478 | add more fields as time goes by. */ |
| 1479 | |
| 1480 | #define TAG_THREADID 1 /* Echo the thread identifier. */ |
| 1481 | #define TAG_EXISTS 2 /* Is this process defined enough to |
| 1482 | fetch registers and its stack? */ |
| 1483 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
| 1484 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
| 1485 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
| 1486 | the process. */ |
| 1487 | |
| 1488 | static int |
| 1489 | remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
| 1490 | struct gdb_ext_thread_info *info) |
| 1491 | { |
| 1492 | struct remote_state *rs = get_remote_state (); |
| 1493 | int mask, length; |
| 1494 | unsigned int tag; |
| 1495 | threadref ref; |
| 1496 | char *limit = pkt + (rs->remote_packet_size); /* plausable parsing limit */ |
| 1497 | int retval = 1; |
| 1498 | |
| 1499 | /* info->threadid = 0; FIXME: implement zero_threadref. */ |
| 1500 | info->active = 0; |
| 1501 | info->display[0] = '\0'; |
| 1502 | info->shortname[0] = '\0'; |
| 1503 | info->more_display[0] = '\0'; |
| 1504 | |
| 1505 | /* Assume the characters indicating the packet type have been |
| 1506 | stripped. */ |
| 1507 | pkt = unpack_int (pkt, &mask); /* arg mask */ |
| 1508 | pkt = unpack_threadid (pkt, &ref); |
| 1509 | |
| 1510 | if (mask == 0) |
| 1511 | warning (_("Incomplete response to threadinfo request.")); |
| 1512 | if (!threadmatch (&ref, expectedref)) |
| 1513 | { /* This is an answer to a different request. */ |
| 1514 | warning (_("ERROR RMT Thread info mismatch.")); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | copy_threadref (&info->threadid, &ref); |
| 1518 | |
| 1519 | /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
| 1520 | |
| 1521 | /* Packets are terminated with nulls. */ |
| 1522 | while ((pkt < limit) && mask && *pkt) |
| 1523 | { |
| 1524 | pkt = unpack_int (pkt, &tag); /* tag */ |
| 1525 | pkt = unpack_byte (pkt, &length); /* length */ |
| 1526 | if (!(tag & mask)) /* Tags out of synch with mask. */ |
| 1527 | { |
| 1528 | warning (_("ERROR RMT: threadinfo tag mismatch.")); |
| 1529 | retval = 0; |
| 1530 | break; |
| 1531 | } |
| 1532 | if (tag == TAG_THREADID) |
| 1533 | { |
| 1534 | if (length != 16) |
| 1535 | { |
| 1536 | warning (_("ERROR RMT: length of threadid is not 16.")); |
| 1537 | retval = 0; |
| 1538 | break; |
| 1539 | } |
| 1540 | pkt = unpack_threadid (pkt, &ref); |
| 1541 | mask = mask & ~TAG_THREADID; |
| 1542 | continue; |
| 1543 | } |
| 1544 | if (tag == TAG_EXISTS) |
| 1545 | { |
| 1546 | info->active = stub_unpack_int (pkt, length); |
| 1547 | pkt += length; |
| 1548 | mask = mask & ~(TAG_EXISTS); |
| 1549 | if (length > 8) |
| 1550 | { |
| 1551 | warning (_("ERROR RMT: 'exists' length too long.")); |
| 1552 | retval = 0; |
| 1553 | break; |
| 1554 | } |
| 1555 | continue; |
| 1556 | } |
| 1557 | if (tag == TAG_THREADNAME) |
| 1558 | { |
| 1559 | pkt = unpack_string (pkt, &info->shortname[0], length); |
| 1560 | mask = mask & ~TAG_THREADNAME; |
| 1561 | continue; |
| 1562 | } |
| 1563 | if (tag == TAG_DISPLAY) |
| 1564 | { |
| 1565 | pkt = unpack_string (pkt, &info->display[0], length); |
| 1566 | mask = mask & ~TAG_DISPLAY; |
| 1567 | continue; |
| 1568 | } |
| 1569 | if (tag == TAG_MOREDISPLAY) |
| 1570 | { |
| 1571 | pkt = unpack_string (pkt, &info->more_display[0], length); |
| 1572 | mask = mask & ~TAG_MOREDISPLAY; |
| 1573 | continue; |
| 1574 | } |
| 1575 | warning (_("ERROR RMT: unknown thread info tag.")); |
| 1576 | break; /* Not a tag we know about. */ |
| 1577 | } |
| 1578 | return retval; |
| 1579 | } |
| 1580 | |
| 1581 | static int |
| 1582 | remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
| 1583 | struct gdb_ext_thread_info *info) |
| 1584 | { |
| 1585 | struct remote_state *rs = get_remote_state (); |
| 1586 | int result; |
| 1587 | char *threadinfo_pkt = alloca (rs->remote_packet_size); |
| 1588 | |
| 1589 | pack_threadinfo_request (threadinfo_pkt, fieldset, threadid); |
| 1590 | putpkt (threadinfo_pkt); |
| 1591 | getpkt (threadinfo_pkt, (rs->remote_packet_size), 0); |
| 1592 | result = remote_unpack_thread_info_response (threadinfo_pkt + 2, |
| 1593 | threadid, info); |
| 1594 | return result; |
| 1595 | } |
| 1596 | |
| 1597 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
| 1598 | |
| 1599 | static char * |
| 1600 | pack_threadlist_request (char *pkt, int startflag, int threadcount, |
| 1601 | threadref *nextthread) |
| 1602 | { |
| 1603 | *pkt++ = 'q'; /* info query packet */ |
| 1604 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ |
| 1605 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
| 1606 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
| 1607 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ |
| 1608 | *pkt = '\0'; |
| 1609 | return pkt; |
| 1610 | } |
| 1611 | |
| 1612 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ |
| 1613 | |
| 1614 | static int |
| 1615 | parse_threadlist_response (char *pkt, int result_limit, |
| 1616 | threadref *original_echo, threadref *resultlist, |
| 1617 | int *doneflag) |
| 1618 | { |
| 1619 | struct remote_state *rs = get_remote_state (); |
| 1620 | char *limit; |
| 1621 | int count, resultcount, done; |
| 1622 | |
| 1623 | resultcount = 0; |
| 1624 | /* Assume the 'q' and 'M chars have been stripped. */ |
| 1625 | limit = pkt + ((rs->remote_packet_size) - BUF_THREAD_ID_SIZE); |
| 1626 | /* done parse past here */ |
| 1627 | pkt = unpack_byte (pkt, &count); /* count field */ |
| 1628 | pkt = unpack_nibble (pkt, &done); |
| 1629 | /* The first threadid is the argument threadid. */ |
| 1630 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ |
| 1631 | while ((count-- > 0) && (pkt < limit)) |
| 1632 | { |
| 1633 | pkt = unpack_threadid (pkt, resultlist++); |
| 1634 | if (resultcount++ >= result_limit) |
| 1635 | break; |
| 1636 | } |
| 1637 | if (doneflag) |
| 1638 | *doneflag = done; |
| 1639 | return resultcount; |
| 1640 | } |
| 1641 | |
| 1642 | static int |
| 1643 | remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
| 1644 | int *done, int *result_count, threadref *threadlist) |
| 1645 | { |
| 1646 | struct remote_state *rs = get_remote_state (); |
| 1647 | static threadref echo_nextthread; |
| 1648 | char *threadlist_packet = alloca (rs->remote_packet_size); |
| 1649 | char *t_response = alloca (rs->remote_packet_size); |
| 1650 | int result = 1; |
| 1651 | |
| 1652 | /* Trancate result limit to be smaller than the packet size. */ |
| 1653 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= (rs->remote_packet_size)) |
| 1654 | result_limit = ((rs->remote_packet_size) / BUF_THREAD_ID_SIZE) - 2; |
| 1655 | |
| 1656 | pack_threadlist_request (threadlist_packet, |
| 1657 | startflag, result_limit, nextthread); |
| 1658 | putpkt (threadlist_packet); |
| 1659 | getpkt (t_response, (rs->remote_packet_size), 0); |
| 1660 | |
| 1661 | *result_count = |
| 1662 | parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread, |
| 1663 | threadlist, done); |
| 1664 | |
| 1665 | if (!threadmatch (&echo_nextthread, nextthread)) |
| 1666 | { |
| 1667 | /* FIXME: This is a good reason to drop the packet. */ |
| 1668 | /* Possably, there is a duplicate response. */ |
| 1669 | /* Possabilities : |
| 1670 | retransmit immediatly - race conditions |
| 1671 | retransmit after timeout - yes |
| 1672 | exit |
| 1673 | wait for packet, then exit |
| 1674 | */ |
| 1675 | warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
| 1676 | return 0; /* I choose simply exiting. */ |
| 1677 | } |
| 1678 | if (*result_count <= 0) |
| 1679 | { |
| 1680 | if (*done != 1) |
| 1681 | { |
| 1682 | warning (_("RMT ERROR : failed to get remote thread list.")); |
| 1683 | result = 0; |
| 1684 | } |
| 1685 | return result; /* break; */ |
| 1686 | } |
| 1687 | if (*result_count > result_limit) |
| 1688 | { |
| 1689 | *result_count = 0; |
| 1690 | warning (_("RMT ERROR: threadlist response longer than requested.")); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | return result; |
| 1694 | } |
| 1695 | |
| 1696 | /* This is the interface between remote and threads, remotes upper |
| 1697 | interface. */ |
| 1698 | |
| 1699 | /* remote_find_new_threads retrieves the thread list and for each |
| 1700 | thread in the list, looks up the thread in GDB's internal list, |
| 1701 | ading the thread if it does not already exist. This involves |
| 1702 | getting partial thread lists from the remote target so, polling the |
| 1703 | quit_flag is required. */ |
| 1704 | |
| 1705 | |
| 1706 | /* About this many threadisds fit in a packet. */ |
| 1707 | |
| 1708 | #define MAXTHREADLISTRESULTS 32 |
| 1709 | |
| 1710 | static int |
| 1711 | remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
| 1712 | int looplimit) |
| 1713 | { |
| 1714 | int done, i, result_count; |
| 1715 | int startflag = 1; |
| 1716 | int result = 1; |
| 1717 | int loopcount = 0; |
| 1718 | static threadref nextthread; |
| 1719 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; |
| 1720 | |
| 1721 | done = 0; |
| 1722 | while (!done) |
| 1723 | { |
| 1724 | if (loopcount++ > looplimit) |
| 1725 | { |
| 1726 | result = 0; |
| 1727 | warning (_("Remote fetch threadlist -infinite loop-.")); |
| 1728 | break; |
| 1729 | } |
| 1730 | if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, |
| 1731 | &done, &result_count, resultthreadlist)) |
| 1732 | { |
| 1733 | result = 0; |
| 1734 | break; |
| 1735 | } |
| 1736 | /* Clear for later iterations. */ |
| 1737 | startflag = 0; |
| 1738 | /* Setup to resume next batch of thread references, set nextthread. */ |
| 1739 | if (result_count >= 1) |
| 1740 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); |
| 1741 | i = 0; |
| 1742 | while (result_count--) |
| 1743 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) |
| 1744 | break; |
| 1745 | } |
| 1746 | return result; |
| 1747 | } |
| 1748 | |
| 1749 | static int |
| 1750 | remote_newthread_step (threadref *ref, void *context) |
| 1751 | { |
| 1752 | ptid_t ptid; |
| 1753 | |
| 1754 | ptid = pid_to_ptid (threadref_to_int (ref)); |
| 1755 | |
| 1756 | if (!in_thread_list (ptid)) |
| 1757 | add_thread (ptid); |
| 1758 | return 1; /* continue iterator */ |
| 1759 | } |
| 1760 | |
| 1761 | #define CRAZY_MAX_THREADS 1000 |
| 1762 | |
| 1763 | static ptid_t |
| 1764 | remote_current_thread (ptid_t oldpid) |
| 1765 | { |
| 1766 | struct remote_state *rs = get_remote_state (); |
| 1767 | char *buf = alloca (rs->remote_packet_size); |
| 1768 | |
| 1769 | putpkt ("qC"); |
| 1770 | getpkt (buf, (rs->remote_packet_size), 0); |
| 1771 | if (buf[0] == 'Q' && buf[1] == 'C') |
| 1772 | /* Use strtoul here, so we'll correctly parse values whose highest |
| 1773 | bit is set. The protocol carries them as a simple series of |
| 1774 | hex digits; in the absence of a sign, strtol will see such |
| 1775 | values as positive numbers out of range for signed 'long', and |
| 1776 | return LONG_MAX to indicate an overflow. */ |
| 1777 | return pid_to_ptid (strtoul (&buf[2], NULL, 16)); |
| 1778 | else |
| 1779 | return oldpid; |
| 1780 | } |
| 1781 | |
| 1782 | /* Find new threads for info threads command. |
| 1783 | * Original version, using John Metzler's thread protocol. |
| 1784 | */ |
| 1785 | |
| 1786 | static void |
| 1787 | remote_find_new_threads (void) |
| 1788 | { |
| 1789 | remote_threadlist_iterator (remote_newthread_step, 0, |
| 1790 | CRAZY_MAX_THREADS); |
| 1791 | if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */ |
| 1792 | inferior_ptid = remote_current_thread (inferior_ptid); |
| 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | * Find all threads for info threads command. |
| 1797 | * Uses new thread protocol contributed by Cisco. |
| 1798 | * Falls back and attempts to use the older method (above) |
| 1799 | * if the target doesn't respond to the new method. |
| 1800 | */ |
| 1801 | |
| 1802 | static void |
| 1803 | remote_threads_info (void) |
| 1804 | { |
| 1805 | struct remote_state *rs = get_remote_state (); |
| 1806 | char *buf = alloca (rs->remote_packet_size); |
| 1807 | char *bufp; |
| 1808 | int tid; |
| 1809 | |
| 1810 | if (remote_desc == 0) /* paranoia */ |
| 1811 | error (_("Command can only be used when connected to the remote target.")); |
| 1812 | |
| 1813 | if (use_threadinfo_query) |
| 1814 | { |
| 1815 | putpkt ("qfThreadInfo"); |
| 1816 | bufp = buf; |
| 1817 | getpkt (bufp, (rs->remote_packet_size), 0); |
| 1818 | if (bufp[0] != '\0') /* q packet recognized */ |
| 1819 | { |
| 1820 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
| 1821 | { |
| 1822 | do |
| 1823 | { |
| 1824 | /* Use strtoul here, so we'll correctly parse values |
| 1825 | whose highest bit is set. The protocol carries |
| 1826 | them as a simple series of hex digits; in the |
| 1827 | absence of a sign, strtol will see such values as |
| 1828 | positive numbers out of range for signed 'long', |
| 1829 | and return LONG_MAX to indicate an overflow. */ |
| 1830 | tid = strtoul (bufp, &bufp, 16); |
| 1831 | if (tid != 0 && !in_thread_list (pid_to_ptid (tid))) |
| 1832 | add_thread (pid_to_ptid (tid)); |
| 1833 | } |
| 1834 | while (*bufp++ == ','); /* comma-separated list */ |
| 1835 | putpkt ("qsThreadInfo"); |
| 1836 | bufp = buf; |
| 1837 | getpkt (bufp, (rs->remote_packet_size), 0); |
| 1838 | } |
| 1839 | return; /* done */ |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | /* Else fall back to old method based on jmetzler protocol. */ |
| 1844 | use_threadinfo_query = 0; |
| 1845 | remote_find_new_threads (); |
| 1846 | return; |
| 1847 | } |
| 1848 | |
| 1849 | /* |
| 1850 | * Collect a descriptive string about the given thread. |
| 1851 | * The target may say anything it wants to about the thread |
| 1852 | * (typically info about its blocked / runnable state, name, etc.). |
| 1853 | * This string will appear in the info threads display. |
| 1854 | * |
| 1855 | * Optional: targets are not required to implement this function. |
| 1856 | */ |
| 1857 | |
| 1858 | static char * |
| 1859 | remote_threads_extra_info (struct thread_info *tp) |
| 1860 | { |
| 1861 | struct remote_state *rs = get_remote_state (); |
| 1862 | int result; |
| 1863 | int set; |
| 1864 | threadref id; |
| 1865 | struct gdb_ext_thread_info threadinfo; |
| 1866 | static char display_buf[100]; /* arbitrary... */ |
| 1867 | char *bufp = alloca (rs->remote_packet_size); |
| 1868 | int n = 0; /* position in display_buf */ |
| 1869 | |
| 1870 | if (remote_desc == 0) /* paranoia */ |
| 1871 | internal_error (__FILE__, __LINE__, |
| 1872 | _("remote_threads_extra_info")); |
| 1873 | |
| 1874 | if (use_threadextra_query) |
| 1875 | { |
| 1876 | sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid)); |
| 1877 | putpkt (bufp); |
| 1878 | getpkt (bufp, (rs->remote_packet_size), 0); |
| 1879 | if (bufp[0] != 0) |
| 1880 | { |
| 1881 | n = min (strlen (bufp) / 2, sizeof (display_buf)); |
| 1882 | result = hex2bin (bufp, display_buf, n); |
| 1883 | display_buf [result] = '\0'; |
| 1884 | return display_buf; |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | /* If the above query fails, fall back to the old method. */ |
| 1889 | use_threadextra_query = 0; |
| 1890 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME |
| 1891 | | TAG_MOREDISPLAY | TAG_DISPLAY; |
| 1892 | int_to_threadref (&id, PIDGET (tp->ptid)); |
| 1893 | if (remote_get_threadinfo (&id, set, &threadinfo)) |
| 1894 | if (threadinfo.active) |
| 1895 | { |
| 1896 | if (*threadinfo.shortname) |
| 1897 | n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname); |
| 1898 | if (*threadinfo.display) |
| 1899 | n += sprintf(&display_buf[n], " State: %s,", threadinfo.display); |
| 1900 | if (*threadinfo.more_display) |
| 1901 | n += sprintf(&display_buf[n], " Priority: %s", |
| 1902 | threadinfo.more_display); |
| 1903 | |
| 1904 | if (n > 0) |
| 1905 | { |
| 1906 | /* For purely cosmetic reasons, clear up trailing commas. */ |
| 1907 | if (',' == display_buf[n-1]) |
| 1908 | display_buf[n-1] = ' '; |
| 1909 | return display_buf; |
| 1910 | } |
| 1911 | } |
| 1912 | return NULL; |
| 1913 | } |
| 1914 | |
| 1915 | \f |
| 1916 | |
| 1917 | /* Restart the remote side; this is an extended protocol operation. */ |
| 1918 | |
| 1919 | static void |
| 1920 | extended_remote_restart (void) |
| 1921 | { |
| 1922 | struct remote_state *rs = get_remote_state (); |
| 1923 | char *buf = alloca (rs->remote_packet_size); |
| 1924 | |
| 1925 | /* Send the restart command; for reasons I don't understand the |
| 1926 | remote side really expects a number after the "R". */ |
| 1927 | buf[0] = 'R'; |
| 1928 | sprintf (&buf[1], "%x", 0); |
| 1929 | putpkt (buf); |
| 1930 | |
| 1931 | /* Now query for status so this looks just like we restarted |
| 1932 | gdbserver from scratch. */ |
| 1933 | putpkt ("?"); |
| 1934 | getpkt (buf, (rs->remote_packet_size), 0); |
| 1935 | } |
| 1936 | \f |
| 1937 | /* Clean up connection to a remote debugger. */ |
| 1938 | |
| 1939 | static void |
| 1940 | remote_close (int quitting) |
| 1941 | { |
| 1942 | if (remote_desc) |
| 1943 | serial_close (remote_desc); |
| 1944 | remote_desc = NULL; |
| 1945 | } |
| 1946 | |
| 1947 | /* Query the remote side for the text, data and bss offsets. */ |
| 1948 | |
| 1949 | static void |
| 1950 | get_offsets (void) |
| 1951 | { |
| 1952 | struct remote_state *rs = get_remote_state (); |
| 1953 | char *buf = alloca (rs->remote_packet_size); |
| 1954 | char *ptr; |
| 1955 | int lose; |
| 1956 | CORE_ADDR text_addr, data_addr, bss_addr; |
| 1957 | struct section_offsets *offs; |
| 1958 | |
| 1959 | putpkt ("qOffsets"); |
| 1960 | |
| 1961 | getpkt (buf, (rs->remote_packet_size), 0); |
| 1962 | |
| 1963 | if (buf[0] == '\000') |
| 1964 | return; /* Return silently. Stub doesn't support |
| 1965 | this command. */ |
| 1966 | if (buf[0] == 'E') |
| 1967 | { |
| 1968 | warning (_("Remote failure reply: %s"), buf); |
| 1969 | return; |
| 1970 | } |
| 1971 | |
| 1972 | /* Pick up each field in turn. This used to be done with scanf, but |
| 1973 | scanf will make trouble if CORE_ADDR size doesn't match |
| 1974 | conversion directives correctly. The following code will work |
| 1975 | with any size of CORE_ADDR. */ |
| 1976 | text_addr = data_addr = bss_addr = 0; |
| 1977 | ptr = buf; |
| 1978 | lose = 0; |
| 1979 | |
| 1980 | if (strncmp (ptr, "Text=", 5) == 0) |
| 1981 | { |
| 1982 | ptr += 5; |
| 1983 | /* Don't use strtol, could lose on big values. */ |
| 1984 | while (*ptr && *ptr != ';') |
| 1985 | text_addr = (text_addr << 4) + fromhex (*ptr++); |
| 1986 | } |
| 1987 | else |
| 1988 | lose = 1; |
| 1989 | |
| 1990 | if (!lose && strncmp (ptr, ";Data=", 6) == 0) |
| 1991 | { |
| 1992 | ptr += 6; |
| 1993 | while (*ptr && *ptr != ';') |
| 1994 | data_addr = (data_addr << 4) + fromhex (*ptr++); |
| 1995 | } |
| 1996 | else |
| 1997 | lose = 1; |
| 1998 | |
| 1999 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) |
| 2000 | { |
| 2001 | ptr += 5; |
| 2002 | while (*ptr && *ptr != ';') |
| 2003 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); |
| 2004 | } |
| 2005 | else |
| 2006 | lose = 1; |
| 2007 | |
| 2008 | if (lose) |
| 2009 | error (_("Malformed response to offset query, %s"), buf); |
| 2010 | |
| 2011 | if (symfile_objfile == NULL) |
| 2012 | return; |
| 2013 | |
| 2014 | offs = ((struct section_offsets *) |
| 2015 | alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections))); |
| 2016 | memcpy (offs, symfile_objfile->section_offsets, |
| 2017 | SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)); |
| 2018 | |
| 2019 | offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr; |
| 2020 | |
| 2021 | /* This is a temporary kludge to force data and bss to use the same offsets |
| 2022 | because that's what nlmconv does now. The real solution requires changes |
| 2023 | to the stub and remote.c that I don't have time to do right now. */ |
| 2024 | |
| 2025 | offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr; |
| 2026 | offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr; |
| 2027 | |
| 2028 | objfile_relocate (symfile_objfile, offs); |
| 2029 | } |
| 2030 | |
| 2031 | /* Stub for catch_errors. */ |
| 2032 | |
| 2033 | static int |
| 2034 | remote_start_remote_dummy (struct ui_out *uiout, void *dummy) |
| 2035 | { |
| 2036 | start_remote (); /* Initialize gdb process mechanisms. */ |
| 2037 | /* NOTE: Return something >=0. A -ve value is reserved for |
| 2038 | catch_exceptions. */ |
| 2039 | return 1; |
| 2040 | } |
| 2041 | |
| 2042 | static void |
| 2043 | remote_start_remote (struct ui_out *uiout, void *dummy) |
| 2044 | { |
| 2045 | immediate_quit++; /* Allow user to interrupt it. */ |
| 2046 | |
| 2047 | /* Ack any packet which the remote side has already sent. */ |
| 2048 | serial_write (remote_desc, "+", 1); |
| 2049 | |
| 2050 | /* Let the stub know that we want it to return the thread. */ |
| 2051 | set_thread (-1, 0); |
| 2052 | |
| 2053 | inferior_ptid = remote_current_thread (inferior_ptid); |
| 2054 | |
| 2055 | get_offsets (); /* Get text, data & bss offsets. */ |
| 2056 | |
| 2057 | putpkt ("?"); /* Initiate a query from remote machine. */ |
| 2058 | immediate_quit--; |
| 2059 | |
| 2060 | remote_start_remote_dummy (uiout, dummy); |
| 2061 | } |
| 2062 | |
| 2063 | /* Open a connection to a remote debugger. |
| 2064 | NAME is the filename used for communication. */ |
| 2065 | |
| 2066 | static void |
| 2067 | remote_open (char *name, int from_tty) |
| 2068 | { |
| 2069 | remote_open_1 (name, from_tty, &remote_ops, 0, 0); |
| 2070 | } |
| 2071 | |
| 2072 | /* Just like remote_open, but with asynchronous support. */ |
| 2073 | static void |
| 2074 | remote_async_open (char *name, int from_tty) |
| 2075 | { |
| 2076 | remote_open_1 (name, from_tty, &remote_async_ops, 0, 1); |
| 2077 | } |
| 2078 | |
| 2079 | /* Open a connection to a remote debugger using the extended |
| 2080 | remote gdb protocol. NAME is the filename used for communication. */ |
| 2081 | |
| 2082 | static void |
| 2083 | extended_remote_open (char *name, int from_tty) |
| 2084 | { |
| 2085 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */, |
| 2086 | 0 /* async_p */); |
| 2087 | } |
| 2088 | |
| 2089 | /* Just like extended_remote_open, but with asynchronous support. */ |
| 2090 | static void |
| 2091 | extended_remote_async_open (char *name, int from_tty) |
| 2092 | { |
| 2093 | remote_open_1 (name, from_tty, &extended_async_remote_ops, |
| 2094 | 1 /*extended_p */, 1 /* async_p */); |
| 2095 | } |
| 2096 | |
| 2097 | /* Generic code for opening a connection to a remote target. */ |
| 2098 | |
| 2099 | static void |
| 2100 | init_all_packet_configs (void) |
| 2101 | { |
| 2102 | int i; |
| 2103 | update_packet_config (&remote_protocol_P); |
| 2104 | update_packet_config (&remote_protocol_p); |
| 2105 | update_packet_config (&remote_protocol_qSymbol); |
| 2106 | update_packet_config (&remote_protocol_vcont); |
| 2107 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
| 2108 | update_packet_config (&remote_protocol_Z[i]); |
| 2109 | /* Force remote_write_bytes to check whether target supports binary |
| 2110 | downloading. */ |
| 2111 | update_packet_config (&remote_protocol_binary_download); |
| 2112 | update_packet_config (&remote_protocol_qPart_auxv); |
| 2113 | update_packet_config (&remote_protocol_qGetTLSAddr); |
| 2114 | } |
| 2115 | |
| 2116 | /* Symbol look-up. */ |
| 2117 | |
| 2118 | static void |
| 2119 | remote_check_symbols (struct objfile *objfile) |
| 2120 | { |
| 2121 | struct remote_state *rs = get_remote_state (); |
| 2122 | char *msg, *reply, *tmp; |
| 2123 | struct minimal_symbol *sym; |
| 2124 | int end; |
| 2125 | |
| 2126 | if (remote_protocol_qSymbol.support == PACKET_DISABLE) |
| 2127 | return; |
| 2128 | |
| 2129 | msg = alloca (rs->remote_packet_size); |
| 2130 | reply = alloca (rs->remote_packet_size); |
| 2131 | |
| 2132 | /* Invite target to request symbol lookups. */ |
| 2133 | |
| 2134 | putpkt ("qSymbol::"); |
| 2135 | getpkt (reply, (rs->remote_packet_size), 0); |
| 2136 | packet_ok (reply, &remote_protocol_qSymbol); |
| 2137 | |
| 2138 | while (strncmp (reply, "qSymbol:", 8) == 0) |
| 2139 | { |
| 2140 | tmp = &reply[8]; |
| 2141 | end = hex2bin (tmp, msg, strlen (tmp) / 2); |
| 2142 | msg[end] = '\0'; |
| 2143 | sym = lookup_minimal_symbol (msg, NULL, NULL); |
| 2144 | if (sym == NULL) |
| 2145 | sprintf (msg, "qSymbol::%s", &reply[8]); |
| 2146 | else |
| 2147 | sprintf (msg, "qSymbol:%s:%s", |
| 2148 | paddr_nz (SYMBOL_VALUE_ADDRESS (sym)), |
| 2149 | &reply[8]); |
| 2150 | putpkt (msg); |
| 2151 | getpkt (reply, (rs->remote_packet_size), 0); |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | static struct serial * |
| 2156 | remote_serial_open (char *name) |
| 2157 | { |
| 2158 | static int udp_warning = 0; |
| 2159 | |
| 2160 | /* FIXME: Parsing NAME here is a hack. But we want to warn here instead |
| 2161 | of in ser-tcp.c, because it is the remote protocol assuming that the |
| 2162 | serial connection is reliable and not the serial connection promising |
| 2163 | to be. */ |
| 2164 | if (!udp_warning && strncmp (name, "udp:", 4) == 0) |
| 2165 | { |
| 2166 | warning (_("\ |
| 2167 | The remote protocol may be unreliable over UDP.\n\ |
| 2168 | Some events may be lost, rendering further debugging impossible.")); |
| 2169 | udp_warning = 1; |
| 2170 | } |
| 2171 | |
| 2172 | return serial_open (name); |
| 2173 | } |
| 2174 | |
| 2175 | static void |
| 2176 | remote_open_1 (char *name, int from_tty, struct target_ops *target, |
| 2177 | int extended_p, int async_p) |
| 2178 | { |
| 2179 | struct remote_state *rs = get_remote_state (); |
| 2180 | if (name == 0) |
| 2181 | error (_("To open a remote debug connection, you need to specify what\n" |
| 2182 | "serial device is attached to the remote system\n" |
| 2183 | "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).")); |
| 2184 | |
| 2185 | /* See FIXME above. */ |
| 2186 | if (!async_p) |
| 2187 | wait_forever_enabled_p = 1; |
| 2188 | |
| 2189 | reopen_exec_file (); |
| 2190 | reread_symbols (); |
| 2191 | |
| 2192 | target_preopen (from_tty); |
| 2193 | |
| 2194 | unpush_target (target); |
| 2195 | |
| 2196 | remote_desc = remote_serial_open (name); |
| 2197 | if (!remote_desc) |
| 2198 | perror_with_name (name); |
| 2199 | |
| 2200 | if (baud_rate != -1) |
| 2201 | { |
| 2202 | if (serial_setbaudrate (remote_desc, baud_rate)) |
| 2203 | { |
| 2204 | /* The requested speed could not be set. Error out to |
| 2205 | top level after closing remote_desc. Take care to |
| 2206 | set remote_desc to NULL to avoid closing remote_desc |
| 2207 | more than once. */ |
| 2208 | serial_close (remote_desc); |
| 2209 | remote_desc = NULL; |
| 2210 | perror_with_name (name); |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | serial_raw (remote_desc); |
| 2215 | |
| 2216 | /* If there is something sitting in the buffer we might take it as a |
| 2217 | response to a command, which would be bad. */ |
| 2218 | serial_flush_input (remote_desc); |
| 2219 | |
| 2220 | if (from_tty) |
| 2221 | { |
| 2222 | puts_filtered ("Remote debugging using "); |
| 2223 | puts_filtered (name); |
| 2224 | puts_filtered ("\n"); |
| 2225 | } |
| 2226 | push_target (target); /* Switch to using remote target now. */ |
| 2227 | |
| 2228 | init_all_packet_configs (); |
| 2229 | |
| 2230 | general_thread = -2; |
| 2231 | continue_thread = -2; |
| 2232 | |
| 2233 | /* Probe for ability to use "ThreadInfo" query, as required. */ |
| 2234 | use_threadinfo_query = 1; |
| 2235 | use_threadextra_query = 1; |
| 2236 | |
| 2237 | /* Without this, some commands which require an active target (such |
| 2238 | as kill) won't work. This variable serves (at least) double duty |
| 2239 | as both the pid of the target process (if it has such), and as a |
| 2240 | flag indicating that a target is active. These functions should |
| 2241 | be split out into seperate variables, especially since GDB will |
| 2242 | someday have a notion of debugging several processes. */ |
| 2243 | |
| 2244 | inferior_ptid = pid_to_ptid (MAGIC_NULL_PID); |
| 2245 | |
| 2246 | if (async_p) |
| 2247 | { |
| 2248 | /* With this target we start out by owning the terminal. */ |
| 2249 | remote_async_terminal_ours_p = 1; |
| 2250 | |
| 2251 | /* FIXME: cagney/1999-09-23: During the initial connection it is |
| 2252 | assumed that the target is already ready and able to respond to |
| 2253 | requests. Unfortunately remote_start_remote() eventually calls |
| 2254 | wait_for_inferior() with no timeout. wait_forever_enabled_p gets |
| 2255 | around this. Eventually a mechanism that allows |
| 2256 | wait_for_inferior() to expect/get timeouts will be |
| 2257 | implemented. */ |
| 2258 | wait_forever_enabled_p = 0; |
| 2259 | } |
| 2260 | |
| 2261 | #ifdef SOLIB_CREATE_INFERIOR_HOOK |
| 2262 | /* First delete any symbols previously loaded from shared libraries. */ |
| 2263 | no_shared_libraries (NULL, 0); |
| 2264 | #endif |
| 2265 | |
| 2266 | /* Start the remote connection. If error() or QUIT, discard this |
| 2267 | target (we'd otherwise be in an inconsistent state) and then |
| 2268 | propogate the error on up the exception chain. This ensures that |
| 2269 | the caller doesn't stumble along blindly assuming that the |
| 2270 | function succeeded. The CLI doesn't have this problem but other |
| 2271 | UI's, such as MI do. |
| 2272 | |
| 2273 | FIXME: cagney/2002-05-19: Instead of re-throwing the exception, |
| 2274 | this function should return an error indication letting the |
| 2275 | caller restore the previous state. Unfortunately the command |
| 2276 | ``target remote'' is directly wired to this function making that |
| 2277 | impossible. On a positive note, the CLI side of this problem has |
| 2278 | been fixed - the function set_cmd_context() makes it possible for |
| 2279 | all the ``target ....'' commands to share a common callback |
| 2280 | function. See cli-dump.c. */ |
| 2281 | { |
| 2282 | struct gdb_exception ex |
| 2283 | = catch_exception (uiout, remote_start_remote, NULL, RETURN_MASK_ALL); |
| 2284 | if (ex.reason < 0) |
| 2285 | { |
| 2286 | pop_target (); |
| 2287 | if (async_p) |
| 2288 | wait_forever_enabled_p = 1; |
| 2289 | throw_exception (ex); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | if (async_p) |
| 2294 | wait_forever_enabled_p = 1; |
| 2295 | |
| 2296 | if (extended_p) |
| 2297 | { |
| 2298 | /* Tell the remote that we are using the extended protocol. */ |
| 2299 | char *buf = alloca (rs->remote_packet_size); |
| 2300 | putpkt ("!"); |
| 2301 | getpkt (buf, (rs->remote_packet_size), 0); |
| 2302 | } |
| 2303 | #ifdef SOLIB_CREATE_INFERIOR_HOOK |
| 2304 | /* FIXME: need a master target_open vector from which all |
| 2305 | remote_opens can be called, so that stuff like this can |
| 2306 | go there. Failing that, the following code must be copied |
| 2307 | to the open function for any remote target that wants to |
| 2308 | support svr4 shared libraries. */ |
| 2309 | |
| 2310 | /* Set up to detect and load shared libraries. */ |
| 2311 | if (exec_bfd) /* No use without an exec file. */ |
| 2312 | { |
| 2313 | SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid)); |
| 2314 | remote_check_symbols (symfile_objfile); |
| 2315 | } |
| 2316 | #endif |
| 2317 | |
| 2318 | observer_notify_inferior_created (¤t_target, from_tty); |
| 2319 | } |
| 2320 | |
| 2321 | /* This takes a program previously attached to and detaches it. After |
| 2322 | this is done, GDB can be used to debug some other program. We |
| 2323 | better not have left any breakpoints in the target program or it'll |
| 2324 | die when it hits one. */ |
| 2325 | |
| 2326 | static void |
| 2327 | remote_detach (char *args, int from_tty) |
| 2328 | { |
| 2329 | struct remote_state *rs = get_remote_state (); |
| 2330 | char *buf = alloca (rs->remote_packet_size); |
| 2331 | |
| 2332 | if (args) |
| 2333 | error (_("Argument given to \"detach\" when remotely debugging.")); |
| 2334 | |
| 2335 | /* Tell the remote target to detach. */ |
| 2336 | strcpy (buf, "D"); |
| 2337 | remote_send (buf, (rs->remote_packet_size)); |
| 2338 | |
| 2339 | /* Unregister the file descriptor from the event loop. */ |
| 2340 | if (target_is_async_p ()) |
| 2341 | serial_async (remote_desc, NULL, 0); |
| 2342 | |
| 2343 | target_mourn_inferior (); |
| 2344 | if (from_tty) |
| 2345 | puts_filtered ("Ending remote debugging.\n"); |
| 2346 | } |
| 2347 | |
| 2348 | /* Same as remote_detach, but don't send the "D" packet; just disconnect. */ |
| 2349 | |
| 2350 | static void |
| 2351 | remote_disconnect (char *args, int from_tty) |
| 2352 | { |
| 2353 | if (args) |
| 2354 | error (_("Argument given to \"detach\" when remotely debugging.")); |
| 2355 | |
| 2356 | /* Unregister the file descriptor from the event loop. */ |
| 2357 | if (target_is_async_p ()) |
| 2358 | serial_async (remote_desc, NULL, 0); |
| 2359 | |
| 2360 | target_mourn_inferior (); |
| 2361 | if (from_tty) |
| 2362 | puts_filtered ("Ending remote debugging.\n"); |
| 2363 | } |
| 2364 | |
| 2365 | /* Convert hex digit A to a number. */ |
| 2366 | |
| 2367 | static int |
| 2368 | fromhex (int a) |
| 2369 | { |
| 2370 | if (a >= '0' && a <= '9') |
| 2371 | return a - '0'; |
| 2372 | else if (a >= 'a' && a <= 'f') |
| 2373 | return a - 'a' + 10; |
| 2374 | else if (a >= 'A' && a <= 'F') |
| 2375 | return a - 'A' + 10; |
| 2376 | else |
| 2377 | error (_("Reply contains invalid hex digit %d"), a); |
| 2378 | } |
| 2379 | |
| 2380 | static int |
| 2381 | hex2bin (const char *hex, char *bin, int count) |
| 2382 | { |
| 2383 | int i; |
| 2384 | |
| 2385 | for (i = 0; i < count; i++) |
| 2386 | { |
| 2387 | if (hex[0] == 0 || hex[1] == 0) |
| 2388 | { |
| 2389 | /* Hex string is short, or of uneven length. |
| 2390 | Return the count that has been converted so far. */ |
| 2391 | return i; |
| 2392 | } |
| 2393 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); |
| 2394 | hex += 2; |
| 2395 | } |
| 2396 | return i; |
| 2397 | } |
| 2398 | |
| 2399 | /* Convert number NIB to a hex digit. */ |
| 2400 | |
| 2401 | static int |
| 2402 | tohex (int nib) |
| 2403 | { |
| 2404 | if (nib < 10) |
| 2405 | return '0' + nib; |
| 2406 | else |
| 2407 | return 'a' + nib - 10; |
| 2408 | } |
| 2409 | |
| 2410 | static int |
| 2411 | bin2hex (const char *bin, char *hex, int count) |
| 2412 | { |
| 2413 | int i; |
| 2414 | /* May use a length, or a nul-terminated string as input. */ |
| 2415 | if (count == 0) |
| 2416 | count = strlen (bin); |
| 2417 | |
| 2418 | for (i = 0; i < count; i++) |
| 2419 | { |
| 2420 | *hex++ = tohex ((*bin >> 4) & 0xf); |
| 2421 | *hex++ = tohex (*bin++ & 0xf); |
| 2422 | } |
| 2423 | *hex = 0; |
| 2424 | return i; |
| 2425 | } |
| 2426 | \f |
| 2427 | /* Check for the availability of vCont. This function should also check |
| 2428 | the response. */ |
| 2429 | |
| 2430 | static void |
| 2431 | remote_vcont_probe (struct remote_state *rs, char *buf) |
| 2432 | { |
| 2433 | strcpy (buf, "vCont?"); |
| 2434 | putpkt (buf); |
| 2435 | getpkt (buf, rs->remote_packet_size, 0); |
| 2436 | |
| 2437 | /* Make sure that the features we assume are supported. */ |
| 2438 | if (strncmp (buf, "vCont", 5) == 0) |
| 2439 | { |
| 2440 | char *p = &buf[5]; |
| 2441 | int support_s, support_S, support_c, support_C; |
| 2442 | |
| 2443 | support_s = 0; |
| 2444 | support_S = 0; |
| 2445 | support_c = 0; |
| 2446 | support_C = 0; |
| 2447 | while (p && *p == ';') |
| 2448 | { |
| 2449 | p++; |
| 2450 | if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0)) |
| 2451 | support_s = 1; |
| 2452 | else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0)) |
| 2453 | support_S = 1; |
| 2454 | else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0)) |
| 2455 | support_c = 1; |
| 2456 | else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) |
| 2457 | support_C = 1; |
| 2458 | |
| 2459 | p = strchr (p, ';'); |
| 2460 | } |
| 2461 | |
| 2462 | /* If s, S, c, and C are not all supported, we can't use vCont. Clearing |
| 2463 | BUF will make packet_ok disable the packet. */ |
| 2464 | if (!support_s || !support_S || !support_c || !support_C) |
| 2465 | buf[0] = 0; |
| 2466 | } |
| 2467 | |
| 2468 | packet_ok (buf, &remote_protocol_vcont); |
| 2469 | } |
| 2470 | |
| 2471 | /* Resume the remote inferior by using a "vCont" packet. The thread |
| 2472 | to be resumed is PTID; STEP and SIGGNAL indicate whether the |
| 2473 | resumed thread should be single-stepped and/or signalled. If PTID's |
| 2474 | PID is -1, then all threads are resumed; the thread to be stepped and/or |
| 2475 | signalled is given in the global INFERIOR_PTID. This function returns |
| 2476 | non-zero iff it resumes the inferior. |
| 2477 | |
| 2478 | This function issues a strict subset of all possible vCont commands at the |
| 2479 | moment. */ |
| 2480 | |
| 2481 | static int |
| 2482 | remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal) |
| 2483 | { |
| 2484 | struct remote_state *rs = get_remote_state (); |
| 2485 | int pid = PIDGET (ptid); |
| 2486 | char *buf = NULL, *outbuf; |
| 2487 | struct cleanup *old_cleanup; |
| 2488 | |
| 2489 | buf = xmalloc (rs->remote_packet_size); |
| 2490 | old_cleanup = make_cleanup (xfree, buf); |
| 2491 | |
| 2492 | if (remote_protocol_vcont.support == PACKET_SUPPORT_UNKNOWN) |
| 2493 | remote_vcont_probe (rs, buf); |
| 2494 | |
| 2495 | if (remote_protocol_vcont.support == PACKET_DISABLE) |
| 2496 | { |
| 2497 | do_cleanups (old_cleanup); |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
| 2501 | /* If we could generate a wider range of packets, we'd have to worry |
| 2502 | about overflowing BUF. Should there be a generic |
| 2503 | "multi-part-packet" packet? */ |
| 2504 | |
| 2505 | if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) |
| 2506 | { |
| 2507 | /* MAGIC_NULL_PTID means that we don't have any active threads, so we |
| 2508 | don't have any PID numbers the inferior will understand. Make sure |
| 2509 | to only send forms that do not specify a PID. */ |
| 2510 | if (step && siggnal != TARGET_SIGNAL_0) |
| 2511 | outbuf = xstrprintf ("vCont;S%02x", siggnal); |
| 2512 | else if (step) |
| 2513 | outbuf = xstrprintf ("vCont;s"); |
| 2514 | else if (siggnal != TARGET_SIGNAL_0) |
| 2515 | outbuf = xstrprintf ("vCont;C%02x", siggnal); |
| 2516 | else |
| 2517 | outbuf = xstrprintf ("vCont;c"); |
| 2518 | } |
| 2519 | else if (pid == -1) |
| 2520 | { |
| 2521 | /* Resume all threads, with preference for INFERIOR_PTID. */ |
| 2522 | if (step && siggnal != TARGET_SIGNAL_0) |
| 2523 | outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal, |
| 2524 | PIDGET (inferior_ptid)); |
| 2525 | else if (step) |
| 2526 | outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid)); |
| 2527 | else if (siggnal != TARGET_SIGNAL_0) |
| 2528 | outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal, |
| 2529 | PIDGET (inferior_ptid)); |
| 2530 | else |
| 2531 | outbuf = xstrprintf ("vCont;c"); |
| 2532 | } |
| 2533 | else |
| 2534 | { |
| 2535 | /* Scheduler locking; resume only PTID. */ |
| 2536 | if (step && siggnal != TARGET_SIGNAL_0) |
| 2537 | outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid); |
| 2538 | else if (step) |
| 2539 | outbuf = xstrprintf ("vCont;s:%x", pid); |
| 2540 | else if (siggnal != TARGET_SIGNAL_0) |
| 2541 | outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid); |
| 2542 | else |
| 2543 | outbuf = xstrprintf ("vCont;c:%x", pid); |
| 2544 | } |
| 2545 | |
| 2546 | gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size); |
| 2547 | make_cleanup (xfree, outbuf); |
| 2548 | |
| 2549 | putpkt (outbuf); |
| 2550 | |
| 2551 | do_cleanups (old_cleanup); |
| 2552 | |
| 2553 | return 1; |
| 2554 | } |
| 2555 | |
| 2556 | /* Tell the remote machine to resume. */ |
| 2557 | |
| 2558 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; |
| 2559 | |
| 2560 | static int last_sent_step; |
| 2561 | |
| 2562 | static void |
| 2563 | remote_resume (ptid_t ptid, int step, enum target_signal siggnal) |
| 2564 | { |
| 2565 | struct remote_state *rs = get_remote_state (); |
| 2566 | char *buf = alloca (rs->remote_packet_size); |
| 2567 | int pid = PIDGET (ptid); |
| 2568 | |
| 2569 | last_sent_signal = siggnal; |
| 2570 | last_sent_step = step; |
| 2571 | |
| 2572 | /* A hook for when we need to do something at the last moment before |
| 2573 | resumption. */ |
| 2574 | if (deprecated_target_resume_hook) |
| 2575 | (*deprecated_target_resume_hook) (); |
| 2576 | |
| 2577 | /* The vCont packet doesn't need to specify threads via Hc. */ |
| 2578 | if (remote_vcont_resume (ptid, step, siggnal)) |
| 2579 | return; |
| 2580 | |
| 2581 | /* All other supported resume packets do use Hc, so call set_thread. */ |
| 2582 | if (pid == -1) |
| 2583 | set_thread (0, 0); /* Run any thread. */ |
| 2584 | else |
| 2585 | set_thread (pid, 0); /* Run this thread. */ |
| 2586 | |
| 2587 | if (siggnal != TARGET_SIGNAL_0) |
| 2588 | { |
| 2589 | buf[0] = step ? 'S' : 'C'; |
| 2590 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
| 2591 | buf[2] = tohex (((int) siggnal) & 0xf); |
| 2592 | buf[3] = '\0'; |
| 2593 | } |
| 2594 | else |
| 2595 | strcpy (buf, step ? "s" : "c"); |
| 2596 | |
| 2597 | putpkt (buf); |
| 2598 | } |
| 2599 | |
| 2600 | /* Same as remote_resume, but with async support. */ |
| 2601 | static void |
| 2602 | remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal) |
| 2603 | { |
| 2604 | remote_resume (ptid, step, siggnal); |
| 2605 | |
| 2606 | /* We are about to start executing the inferior, let's register it |
| 2607 | with the event loop. NOTE: this is the one place where all the |
| 2608 | execution commands end up. We could alternatively do this in each |
| 2609 | of the execution commands in infcmd.c. */ |
| 2610 | /* FIXME: ezannoni 1999-09-28: We may need to move this out of here |
| 2611 | into infcmd.c in order to allow inferior function calls to work |
| 2612 | NOT asynchronously. */ |
| 2613 | if (target_can_async_p ()) |
| 2614 | target_async (inferior_event_handler, 0); |
| 2615 | /* Tell the world that the target is now executing. */ |
| 2616 | /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set |
| 2617 | this? Instead, should the client of target just assume (for |
| 2618 | async targets) that the target is going to start executing? Is |
| 2619 | this information already found in the continuation block? */ |
| 2620 | if (target_is_async_p ()) |
| 2621 | target_executing = 1; |
| 2622 | } |
| 2623 | \f |
| 2624 | |
| 2625 | /* Set up the signal handler for SIGINT, while the target is |
| 2626 | executing, ovewriting the 'regular' SIGINT signal handler. */ |
| 2627 | static void |
| 2628 | initialize_sigint_signal_handler (void) |
| 2629 | { |
| 2630 | sigint_remote_token = |
| 2631 | create_async_signal_handler (async_remote_interrupt, NULL); |
| 2632 | signal (SIGINT, handle_remote_sigint); |
| 2633 | } |
| 2634 | |
| 2635 | /* Signal handler for SIGINT, while the target is executing. */ |
| 2636 | static void |
| 2637 | handle_remote_sigint (int sig) |
| 2638 | { |
| 2639 | signal (sig, handle_remote_sigint_twice); |
| 2640 | sigint_remote_twice_token = |
| 2641 | create_async_signal_handler (async_remote_interrupt_twice, NULL); |
| 2642 | mark_async_signal_handler_wrapper (sigint_remote_token); |
| 2643 | } |
| 2644 | |
| 2645 | /* Signal handler for SIGINT, installed after SIGINT has already been |
| 2646 | sent once. It will take effect the second time that the user sends |
| 2647 | a ^C. */ |
| 2648 | static void |
| 2649 | handle_remote_sigint_twice (int sig) |
| 2650 | { |
| 2651 | signal (sig, handle_sigint); |
| 2652 | sigint_remote_twice_token = |
| 2653 | create_async_signal_handler (inferior_event_handler_wrapper, NULL); |
| 2654 | mark_async_signal_handler_wrapper (sigint_remote_twice_token); |
| 2655 | } |
| 2656 | |
| 2657 | /* Perform the real interruption of the target execution, in response |
| 2658 | to a ^C. */ |
| 2659 | static void |
| 2660 | async_remote_interrupt (gdb_client_data arg) |
| 2661 | { |
| 2662 | if (remote_debug) |
| 2663 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); |
| 2664 | |
| 2665 | target_stop (); |
| 2666 | } |
| 2667 | |
| 2668 | /* Perform interrupt, if the first attempt did not succeed. Just give |
| 2669 | up on the target alltogether. */ |
| 2670 | void |
| 2671 | async_remote_interrupt_twice (gdb_client_data arg) |
| 2672 | { |
| 2673 | if (remote_debug) |
| 2674 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n"); |
| 2675 | /* Do something only if the target was not killed by the previous |
| 2676 | cntl-C. */ |
| 2677 | if (target_executing) |
| 2678 | { |
| 2679 | interrupt_query (); |
| 2680 | signal (SIGINT, handle_remote_sigint); |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* Reinstall the usual SIGINT handlers, after the target has |
| 2685 | stopped. */ |
| 2686 | static void |
| 2687 | cleanup_sigint_signal_handler (void *dummy) |
| 2688 | { |
| 2689 | signal (SIGINT, handle_sigint); |
| 2690 | if (sigint_remote_twice_token) |
| 2691 | delete_async_signal_handler ((struct async_signal_handler **) |
| 2692 | &sigint_remote_twice_token); |
| 2693 | if (sigint_remote_token) |
| 2694 | delete_async_signal_handler ((struct async_signal_handler **) |
| 2695 | &sigint_remote_token); |
| 2696 | } |
| 2697 | |
| 2698 | /* Send ^C to target to halt it. Target will respond, and send us a |
| 2699 | packet. */ |
| 2700 | static void (*ofunc) (int); |
| 2701 | |
| 2702 | /* The command line interface's stop routine. This function is installed |
| 2703 | as a signal handler for SIGINT. The first time a user requests a |
| 2704 | stop, we call remote_stop to send a break or ^C. If there is no |
| 2705 | response from the target (it didn't stop when the user requested it), |
| 2706 | we ask the user if he'd like to detach from the target. */ |
| 2707 | static void |
| 2708 | remote_interrupt (int signo) |
| 2709 | { |
| 2710 | /* If this doesn't work, try more severe steps. */ |
| 2711 | signal (signo, remote_interrupt_twice); |
| 2712 | |
| 2713 | if (remote_debug) |
| 2714 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); |
| 2715 | |
| 2716 | target_stop (); |
| 2717 | } |
| 2718 | |
| 2719 | /* The user typed ^C twice. */ |
| 2720 | |
| 2721 | static void |
| 2722 | remote_interrupt_twice (int signo) |
| 2723 | { |
| 2724 | signal (signo, ofunc); |
| 2725 | interrupt_query (); |
| 2726 | signal (signo, remote_interrupt); |
| 2727 | } |
| 2728 | |
| 2729 | /* This is the generic stop called via the target vector. When a target |
| 2730 | interrupt is requested, either by the command line or the GUI, we |
| 2731 | will eventually end up here. */ |
| 2732 | static void |
| 2733 | remote_stop (void) |
| 2734 | { |
| 2735 | /* Send a break or a ^C, depending on user preference. */ |
| 2736 | if (remote_debug) |
| 2737 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
| 2738 | |
| 2739 | if (remote_break) |
| 2740 | serial_send_break (remote_desc); |
| 2741 | else |
| 2742 | serial_write (remote_desc, "\003", 1); |
| 2743 | } |
| 2744 | |
| 2745 | /* Ask the user what to do when an interrupt is received. */ |
| 2746 | |
| 2747 | static void |
| 2748 | interrupt_query (void) |
| 2749 | { |
| 2750 | target_terminal_ours (); |
| 2751 | |
| 2752 | if (query ("Interrupted while waiting for the program.\n\ |
| 2753 | Give up (and stop debugging it)? ")) |
| 2754 | { |
| 2755 | target_mourn_inferior (); |
| 2756 | deprecated_throw_reason (RETURN_QUIT); |
| 2757 | } |
| 2758 | |
| 2759 | target_terminal_inferior (); |
| 2760 | } |
| 2761 | |
| 2762 | /* Enable/disable target terminal ownership. Most targets can use |
| 2763 | terminal groups to control terminal ownership. Remote targets are |
| 2764 | different in that explicit transfer of ownership to/from GDB/target |
| 2765 | is required. */ |
| 2766 | |
| 2767 | static void |
| 2768 | remote_async_terminal_inferior (void) |
| 2769 | { |
| 2770 | /* FIXME: cagney/1999-09-27: Shouldn't need to test for |
| 2771 | sync_execution here. This function should only be called when |
| 2772 | GDB is resuming the inferior in the forground. A background |
| 2773 | resume (``run&'') should leave GDB in control of the terminal and |
| 2774 | consequently should not call this code. */ |
| 2775 | if (!sync_execution) |
| 2776 | return; |
| 2777 | /* FIXME: cagney/1999-09-27: Closely related to the above. Make |
| 2778 | calls target_terminal_*() idenpotent. The event-loop GDB talking |
| 2779 | to an asynchronous target with a synchronous command calls this |
| 2780 | function from both event-top.c and infrun.c/infcmd.c. Once GDB |
| 2781 | stops trying to transfer the terminal to the target when it |
| 2782 | shouldn't this guard can go away. */ |
| 2783 | if (!remote_async_terminal_ours_p) |
| 2784 | return; |
| 2785 | delete_file_handler (input_fd); |
| 2786 | remote_async_terminal_ours_p = 0; |
| 2787 | initialize_sigint_signal_handler (); |
| 2788 | /* NOTE: At this point we could also register our selves as the |
| 2789 | recipient of all input. Any characters typed could then be |
| 2790 | passed on down to the target. */ |
| 2791 | } |
| 2792 | |
| 2793 | static void |
| 2794 | remote_async_terminal_ours (void) |
| 2795 | { |
| 2796 | /* See FIXME in remote_async_terminal_inferior. */ |
| 2797 | if (!sync_execution) |
| 2798 | return; |
| 2799 | /* See FIXME in remote_async_terminal_inferior. */ |
| 2800 | if (remote_async_terminal_ours_p) |
| 2801 | return; |
| 2802 | cleanup_sigint_signal_handler (NULL); |
| 2803 | add_file_handler (input_fd, stdin_event_handler, 0); |
| 2804 | remote_async_terminal_ours_p = 1; |
| 2805 | } |
| 2806 | |
| 2807 | /* If nonzero, ignore the next kill. */ |
| 2808 | |
| 2809 | int kill_kludge; |
| 2810 | |
| 2811 | void |
| 2812 | remote_console_output (char *msg) |
| 2813 | { |
| 2814 | char *p; |
| 2815 | |
| 2816 | for (p = msg; p[0] && p[1]; p += 2) |
| 2817 | { |
| 2818 | char tb[2]; |
| 2819 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 2820 | tb[0] = c; |
| 2821 | tb[1] = 0; |
| 2822 | fputs_unfiltered (tb, gdb_stdtarg); |
| 2823 | } |
| 2824 | gdb_flush (gdb_stdtarg); |
| 2825 | } |
| 2826 | |
| 2827 | /* Wait until the remote machine stops, then return, |
| 2828 | storing status in STATUS just as `wait' would. |
| 2829 | Returns "pid", which in the case of a multi-threaded |
| 2830 | remote OS, is the thread-id. */ |
| 2831 | |
| 2832 | static ptid_t |
| 2833 | remote_wait (ptid_t ptid, struct target_waitstatus *status) |
| 2834 | { |
| 2835 | struct remote_state *rs = get_remote_state (); |
| 2836 | unsigned char *buf = alloca (rs->remote_packet_size); |
| 2837 | ULONGEST thread_num = -1; |
| 2838 | ULONGEST addr; |
| 2839 | |
| 2840 | status->kind = TARGET_WAITKIND_EXITED; |
| 2841 | status->value.integer = 0; |
| 2842 | |
| 2843 | while (1) |
| 2844 | { |
| 2845 | unsigned char *p; |
| 2846 | |
| 2847 | ofunc = signal (SIGINT, remote_interrupt); |
| 2848 | getpkt (buf, (rs->remote_packet_size), 1); |
| 2849 | signal (SIGINT, ofunc); |
| 2850 | |
| 2851 | /* This is a hook for when we need to do something (perhaps the |
| 2852 | collection of trace data) every time the target stops. */ |
| 2853 | if (deprecated_target_wait_loop_hook) |
| 2854 | (*deprecated_target_wait_loop_hook) (); |
| 2855 | |
| 2856 | remote_stopped_by_watchpoint_p = 0; |
| 2857 | |
| 2858 | switch (buf[0]) |
| 2859 | { |
| 2860 | case 'E': /* Error of some sort. */ |
| 2861 | warning (_("Remote failure reply: %s"), buf); |
| 2862 | continue; |
| 2863 | case 'F': /* File-I/O request. */ |
| 2864 | remote_fileio_request (buf); |
| 2865 | continue; |
| 2866 | case 'T': /* Status with PC, SP, FP, ... */ |
| 2867 | { |
| 2868 | char regs[MAX_REGISTER_SIZE]; |
| 2869 | |
| 2870 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
| 2871 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
| 2872 | ss = signal number |
| 2873 | n... = register number |
| 2874 | r... = register contents |
| 2875 | */ |
| 2876 | p = &buf[3]; /* after Txx */ |
| 2877 | |
| 2878 | while (*p) |
| 2879 | { |
| 2880 | unsigned char *p1; |
| 2881 | char *p_temp; |
| 2882 | int fieldsize; |
| 2883 | LONGEST pnum = 0; |
| 2884 | |
| 2885 | /* If the packet contains a register number save it in |
| 2886 | pnum and set p1 to point to the character following |
| 2887 | it. Otherwise p1 points to p. */ |
| 2888 | |
| 2889 | /* If this packet is an awatch packet, don't parse the |
| 2890 | 'a' as a register number. */ |
| 2891 | |
| 2892 | if (strncmp (p, "awatch", strlen("awatch")) != 0) |
| 2893 | { |
| 2894 | /* Read the ``P'' register number. */ |
| 2895 | pnum = strtol (p, &p_temp, 16); |
| 2896 | p1 = (unsigned char *) p_temp; |
| 2897 | } |
| 2898 | else |
| 2899 | p1 = p; |
| 2900 | |
| 2901 | if (p1 == p) /* No register number present here. */ |
| 2902 | { |
| 2903 | p1 = (unsigned char *) strchr (p, ':'); |
| 2904 | if (p1 == NULL) |
| 2905 | warning (_("Malformed packet(a) (missing colon): %s\n\ |
| 2906 | Packet: '%s'\n"), |
| 2907 | p, buf); |
| 2908 | if (strncmp (p, "thread", p1 - p) == 0) |
| 2909 | { |
| 2910 | p_temp = unpack_varlen_hex (++p1, &thread_num); |
| 2911 | record_currthread (thread_num); |
| 2912 | p = (unsigned char *) p_temp; |
| 2913 | } |
| 2914 | else if ((strncmp (p, "watch", p1 - p) == 0) |
| 2915 | || (strncmp (p, "rwatch", p1 - p) == 0) |
| 2916 | || (strncmp (p, "awatch", p1 - p) == 0)) |
| 2917 | { |
| 2918 | remote_stopped_by_watchpoint_p = 1; |
| 2919 | p = unpack_varlen_hex (++p1, &addr); |
| 2920 | remote_watch_data_address = (CORE_ADDR)addr; |
| 2921 | } |
| 2922 | else |
| 2923 | { |
| 2924 | /* Silently skip unknown optional info. */ |
| 2925 | p_temp = strchr (p1 + 1, ';'); |
| 2926 | if (p_temp) |
| 2927 | p = (unsigned char *) p_temp; |
| 2928 | } |
| 2929 | } |
| 2930 | else |
| 2931 | { |
| 2932 | struct packet_reg *reg = packet_reg_from_pnum (rs, pnum); |
| 2933 | p = p1; |
| 2934 | |
| 2935 | if (*p++ != ':') |
| 2936 | error (_("Malformed packet(b) (missing colon): %s\n\ |
| 2937 | Packet: '%s'\n"), |
| 2938 | p, buf); |
| 2939 | |
| 2940 | if (reg == NULL) |
| 2941 | error (_("Remote sent bad register number %s: %s\n\ |
| 2942 | Packet: '%s'\n"), |
| 2943 | phex_nz (pnum, 0), p, buf); |
| 2944 | |
| 2945 | fieldsize = hex2bin (p, regs, |
| 2946 | register_size (current_gdbarch, |
| 2947 | reg->regnum)); |
| 2948 | p += 2 * fieldsize; |
| 2949 | if (fieldsize < register_size (current_gdbarch, |
| 2950 | reg->regnum)) |
| 2951 | warning (_("Remote reply is too short: %s"), buf); |
| 2952 | regcache_raw_supply (current_regcache, |
| 2953 | reg->regnum, regs); |
| 2954 | } |
| 2955 | |
| 2956 | if (*p++ != ';') |
| 2957 | error (_("Remote register badly formatted: %s\nhere: %s"), |
| 2958 | buf, p); |
| 2959 | } |
| 2960 | } |
| 2961 | /* fall through */ |
| 2962 | case 'S': /* Old style status, just signal only. */ |
| 2963 | status->kind = TARGET_WAITKIND_STOPPED; |
| 2964 | status->value.sig = (enum target_signal) |
| 2965 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
| 2966 | |
| 2967 | if (buf[3] == 'p') |
| 2968 | { |
| 2969 | thread_num = strtol ((const char *) &buf[4], NULL, 16); |
| 2970 | record_currthread (thread_num); |
| 2971 | } |
| 2972 | goto got_status; |
| 2973 | case 'W': /* Target exited. */ |
| 2974 | { |
| 2975 | /* The remote process exited. */ |
| 2976 | status->kind = TARGET_WAITKIND_EXITED; |
| 2977 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); |
| 2978 | goto got_status; |
| 2979 | } |
| 2980 | case 'X': |
| 2981 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 2982 | status->value.sig = (enum target_signal) |
| 2983 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
| 2984 | kill_kludge = 1; |
| 2985 | |
| 2986 | goto got_status; |
| 2987 | case 'O': /* Console output. */ |
| 2988 | remote_console_output (buf + 1); |
| 2989 | continue; |
| 2990 | case '\0': |
| 2991 | if (last_sent_signal != TARGET_SIGNAL_0) |
| 2992 | { |
| 2993 | /* Zero length reply means that we tried 'S' or 'C' and |
| 2994 | the remote system doesn't support it. */ |
| 2995 | target_terminal_ours_for_output (); |
| 2996 | printf_filtered |
| 2997 | ("Can't send signals to this remote system. %s not sent.\n", |
| 2998 | target_signal_to_name (last_sent_signal)); |
| 2999 | last_sent_signal = TARGET_SIGNAL_0; |
| 3000 | target_terminal_inferior (); |
| 3001 | |
| 3002 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); |
| 3003 | putpkt ((char *) buf); |
| 3004 | continue; |
| 3005 | } |
| 3006 | /* else fallthrough */ |
| 3007 | default: |
| 3008 | warning (_("Invalid remote reply: %s"), buf); |
| 3009 | continue; |
| 3010 | } |
| 3011 | } |
| 3012 | got_status: |
| 3013 | if (thread_num != -1) |
| 3014 | { |
| 3015 | return pid_to_ptid (thread_num); |
| 3016 | } |
| 3017 | return inferior_ptid; |
| 3018 | } |
| 3019 | |
| 3020 | /* Async version of remote_wait. */ |
| 3021 | static ptid_t |
| 3022 | remote_async_wait (ptid_t ptid, struct target_waitstatus *status) |
| 3023 | { |
| 3024 | struct remote_state *rs = get_remote_state (); |
| 3025 | unsigned char *buf = alloca (rs->remote_packet_size); |
| 3026 | ULONGEST thread_num = -1; |
| 3027 | ULONGEST addr; |
| 3028 | |
| 3029 | status->kind = TARGET_WAITKIND_EXITED; |
| 3030 | status->value.integer = 0; |
| 3031 | |
| 3032 | remote_stopped_by_watchpoint_p = 0; |
| 3033 | |
| 3034 | while (1) |
| 3035 | { |
| 3036 | unsigned char *p; |
| 3037 | |
| 3038 | if (!target_is_async_p ()) |
| 3039 | ofunc = signal (SIGINT, remote_interrupt); |
| 3040 | /* FIXME: cagney/1999-09-27: If we're in async mode we should |
| 3041 | _never_ wait for ever -> test on target_is_async_p(). |
| 3042 | However, before we do that we need to ensure that the caller |
| 3043 | knows how to take the target into/out of async mode. */ |
| 3044 | getpkt (buf, (rs->remote_packet_size), wait_forever_enabled_p); |
| 3045 | if (!target_is_async_p ()) |
| 3046 | signal (SIGINT, ofunc); |
| 3047 | |
| 3048 | /* This is a hook for when we need to do something (perhaps the |
| 3049 | collection of trace data) every time the target stops. */ |
| 3050 | if (deprecated_target_wait_loop_hook) |
| 3051 | (*deprecated_target_wait_loop_hook) (); |
| 3052 | |
| 3053 | switch (buf[0]) |
| 3054 | { |
| 3055 | case 'E': /* Error of some sort. */ |
| 3056 | warning (_("Remote failure reply: %s"), buf); |
| 3057 | continue; |
| 3058 | case 'F': /* File-I/O request. */ |
| 3059 | remote_fileio_request (buf); |
| 3060 | continue; |
| 3061 | case 'T': /* Status with PC, SP, FP, ... */ |
| 3062 | { |
| 3063 | char regs[MAX_REGISTER_SIZE]; |
| 3064 | |
| 3065 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
| 3066 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
| 3067 | ss = signal number |
| 3068 | n... = register number |
| 3069 | r... = register contents |
| 3070 | */ |
| 3071 | p = &buf[3]; /* after Txx */ |
| 3072 | |
| 3073 | while (*p) |
| 3074 | { |
| 3075 | unsigned char *p1; |
| 3076 | char *p_temp; |
| 3077 | int fieldsize; |
| 3078 | long pnum = 0; |
| 3079 | |
| 3080 | /* If the packet contains a register number, save it |
| 3081 | in pnum and set p1 to point to the character |
| 3082 | following it. Otherwise p1 points to p. */ |
| 3083 | |
| 3084 | /* If this packet is an awatch packet, don't parse the 'a' |
| 3085 | as a register number. */ |
| 3086 | |
| 3087 | if (!strncmp (p, "awatch", strlen ("awatch")) != 0) |
| 3088 | { |
| 3089 | /* Read the register number. */ |
| 3090 | pnum = strtol (p, &p_temp, 16); |
| 3091 | p1 = (unsigned char *) p_temp; |
| 3092 | } |
| 3093 | else |
| 3094 | p1 = p; |
| 3095 | |
| 3096 | if (p1 == p) /* No register number present here. */ |
| 3097 | { |
| 3098 | p1 = (unsigned char *) strchr (p, ':'); |
| 3099 | if (p1 == NULL) |
| 3100 | error (_("Malformed packet(a) (missing colon): %s\n\ |
| 3101 | Packet: '%s'\n"), |
| 3102 | p, buf); |
| 3103 | if (strncmp (p, "thread", p1 - p) == 0) |
| 3104 | { |
| 3105 | p_temp = unpack_varlen_hex (++p1, &thread_num); |
| 3106 | record_currthread (thread_num); |
| 3107 | p = (unsigned char *) p_temp; |
| 3108 | } |
| 3109 | else if ((strncmp (p, "watch", p1 - p) == 0) |
| 3110 | || (strncmp (p, "rwatch", p1 - p) == 0) |
| 3111 | || (strncmp (p, "awatch", p1 - p) == 0)) |
| 3112 | { |
| 3113 | remote_stopped_by_watchpoint_p = 1; |
| 3114 | p = unpack_varlen_hex (++p1, &addr); |
| 3115 | remote_watch_data_address = (CORE_ADDR)addr; |
| 3116 | } |
| 3117 | else |
| 3118 | { |
| 3119 | /* Silently skip unknown optional info. */ |
| 3120 | p_temp = (unsigned char *) strchr (p1 + 1, ';'); |
| 3121 | if (p_temp) |
| 3122 | p = p_temp; |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | else |
| 3127 | { |
| 3128 | struct packet_reg *reg = packet_reg_from_pnum (rs, pnum); |
| 3129 | p = p1; |
| 3130 | if (*p++ != ':') |
| 3131 | error (_("Malformed packet(b) (missing colon): %s\n\ |
| 3132 | Packet: '%s'\n"), |
| 3133 | p, buf); |
| 3134 | |
| 3135 | if (reg == NULL) |
| 3136 | error (_("Remote sent bad register number %ld: %s\n\ |
| 3137 | Packet: '%s'\n"), |
| 3138 | pnum, p, buf); |
| 3139 | |
| 3140 | fieldsize = hex2bin (p, regs, |
| 3141 | register_size (current_gdbarch, |
| 3142 | reg->regnum)); |
| 3143 | p += 2 * fieldsize; |
| 3144 | if (fieldsize < register_size (current_gdbarch, |
| 3145 | reg->regnum)) |
| 3146 | warning (_("Remote reply is too short: %s"), buf); |
| 3147 | regcache_raw_supply (current_regcache, reg->regnum, regs); |
| 3148 | } |
| 3149 | |
| 3150 | if (*p++ != ';') |
| 3151 | error (_("Remote register badly formatted: %s\nhere: %s"), |
| 3152 | buf, p); |
| 3153 | } |
| 3154 | } |
| 3155 | /* fall through */ |
| 3156 | case 'S': /* Old style status, just signal only. */ |
| 3157 | status->kind = TARGET_WAITKIND_STOPPED; |
| 3158 | status->value.sig = (enum target_signal) |
| 3159 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
| 3160 | |
| 3161 | if (buf[3] == 'p') |
| 3162 | { |
| 3163 | thread_num = strtol ((const char *) &buf[4], NULL, 16); |
| 3164 | record_currthread (thread_num); |
| 3165 | } |
| 3166 | goto got_status; |
| 3167 | case 'W': /* Target exited. */ |
| 3168 | { |
| 3169 | /* The remote process exited. */ |
| 3170 | status->kind = TARGET_WAITKIND_EXITED; |
| 3171 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); |
| 3172 | goto got_status; |
| 3173 | } |
| 3174 | case 'X': |
| 3175 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 3176 | status->value.sig = (enum target_signal) |
| 3177 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
| 3178 | kill_kludge = 1; |
| 3179 | |
| 3180 | goto got_status; |
| 3181 | case 'O': /* Console output. */ |
| 3182 | remote_console_output (buf + 1); |
| 3183 | /* Return immediately to the event loop. The event loop will |
| 3184 | still be waiting on the inferior afterwards. */ |
| 3185 | status->kind = TARGET_WAITKIND_IGNORE; |
| 3186 | goto got_status; |
| 3187 | case '\0': |
| 3188 | if (last_sent_signal != TARGET_SIGNAL_0) |
| 3189 | { |
| 3190 | /* Zero length reply means that we tried 'S' or 'C' and |
| 3191 | the remote system doesn't support it. */ |
| 3192 | target_terminal_ours_for_output (); |
| 3193 | printf_filtered |
| 3194 | ("Can't send signals to this remote system. %s not sent.\n", |
| 3195 | target_signal_to_name (last_sent_signal)); |
| 3196 | last_sent_signal = TARGET_SIGNAL_0; |
| 3197 | target_terminal_inferior (); |
| 3198 | |
| 3199 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); |
| 3200 | putpkt ((char *) buf); |
| 3201 | continue; |
| 3202 | } |
| 3203 | /* else fallthrough */ |
| 3204 | default: |
| 3205 | warning (_("Invalid remote reply: %s"), buf); |
| 3206 | continue; |
| 3207 | } |
| 3208 | } |
| 3209 | got_status: |
| 3210 | if (thread_num != -1) |
| 3211 | { |
| 3212 | return pid_to_ptid (thread_num); |
| 3213 | } |
| 3214 | return inferior_ptid; |
| 3215 | } |
| 3216 | |
| 3217 | /* Number of bytes of registers this stub implements. */ |
| 3218 | |
| 3219 | static int register_bytes_found; |
| 3220 | |
| 3221 | /* Read the remote registers into the block REGS. */ |
| 3222 | /* Currently we just read all the registers, so we don't use regnum. */ |
| 3223 | |
| 3224 | static int |
| 3225 | fetch_register_using_p (int regnum) |
| 3226 | { |
| 3227 | struct remote_state *rs = get_remote_state (); |
| 3228 | char *buf = alloca (rs->remote_packet_size), *p; |
| 3229 | char regp[MAX_REGISTER_SIZE]; |
| 3230 | int i; |
| 3231 | |
| 3232 | p = buf; |
| 3233 | *p++ = 'p'; |
| 3234 | p += hexnumstr (p, regnum); |
| 3235 | *p++ = '\0'; |
| 3236 | remote_send (buf, rs->remote_packet_size); |
| 3237 | |
| 3238 | /* If the stub didn't recognize the packet, or if we got an error, |
| 3239 | tell our caller. */ |
| 3240 | if (buf[0] == '\0' || buf[0] == 'E') |
| 3241 | return 0; |
| 3242 | |
| 3243 | /* If this register is unfetchable, tell the regcache. */ |
| 3244 | if (buf[0] == 'x') |
| 3245 | { |
| 3246 | regcache_raw_supply (current_regcache, regnum, NULL); |
| 3247 | set_register_cached (regnum, -1); |
| 3248 | return 1; |
| 3249 | } |
| 3250 | |
| 3251 | /* Otherwise, parse and supply the value. */ |
| 3252 | p = buf; |
| 3253 | i = 0; |
| 3254 | while (p[0] != 0) |
| 3255 | { |
| 3256 | if (p[1] == 0) |
| 3257 | { |
| 3258 | error (_("fetch_register_using_p: early buf termination")); |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
| 3262 | regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 3263 | p += 2; |
| 3264 | } |
| 3265 | regcache_raw_supply (current_regcache, regnum, regp); |
| 3266 | return 1; |
| 3267 | } |
| 3268 | |
| 3269 | static void |
| 3270 | remote_fetch_registers (int regnum) |
| 3271 | { |
| 3272 | struct remote_state *rs = get_remote_state (); |
| 3273 | char *buf = alloca (rs->remote_packet_size); |
| 3274 | int i; |
| 3275 | char *p; |
| 3276 | char *regs = alloca (rs->sizeof_g_packet); |
| 3277 | |
| 3278 | set_thread (PIDGET (inferior_ptid), 1); |
| 3279 | |
| 3280 | if (regnum >= 0) |
| 3281 | { |
| 3282 | struct packet_reg *reg = packet_reg_from_regnum (rs, regnum); |
| 3283 | gdb_assert (reg != NULL); |
| 3284 | if (!reg->in_g_packet) |
| 3285 | internal_error (__FILE__, __LINE__, |
| 3286 | _("Attempt to fetch a non G-packet register when this " |
| 3287 | "remote.c does not support the p-packet.")); |
| 3288 | } |
| 3289 | switch (remote_protocol_p.support) |
| 3290 | { |
| 3291 | case PACKET_DISABLE: |
| 3292 | break; |
| 3293 | case PACKET_ENABLE: |
| 3294 | if (fetch_register_using_p (regnum)) |
| 3295 | return; |
| 3296 | else |
| 3297 | error (_("Protocol error: p packet not recognized by stub")); |
| 3298 | case PACKET_SUPPORT_UNKNOWN: |
| 3299 | if (fetch_register_using_p (regnum)) |
| 3300 | { |
| 3301 | /* The stub recognized the 'p' packet. Remember this. */ |
| 3302 | remote_protocol_p.support = PACKET_ENABLE; |
| 3303 | return; |
| 3304 | } |
| 3305 | else |
| 3306 | { |
| 3307 | /* The stub does not support the 'P' packet. Use 'G' |
| 3308 | instead, and don't try using 'P' in the future (it |
| 3309 | will just waste our time). */ |
| 3310 | remote_protocol_p.support = PACKET_DISABLE; |
| 3311 | break; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | sprintf (buf, "g"); |
| 3316 | remote_send (buf, (rs->remote_packet_size)); |
| 3317 | |
| 3318 | /* Save the size of the packet sent to us by the target. Its used |
| 3319 | as a heuristic when determining the max size of packets that the |
| 3320 | target can safely receive. */ |
| 3321 | if ((rs->actual_register_packet_size) == 0) |
| 3322 | (rs->actual_register_packet_size) = strlen (buf); |
| 3323 | |
| 3324 | /* Unimplemented registers read as all bits zero. */ |
| 3325 | memset (regs, 0, rs->sizeof_g_packet); |
| 3326 | |
| 3327 | /* We can get out of synch in various cases. If the first character |
| 3328 | in the buffer is not a hex character, assume that has happened |
| 3329 | and try to fetch another packet to read. */ |
| 3330 | while ((buf[0] < '0' || buf[0] > '9') |
| 3331 | && (buf[0] < 'A' || buf[0] > 'F') |
| 3332 | && (buf[0] < 'a' || buf[0] > 'f') |
| 3333 | && buf[0] != 'x') /* New: unavailable register value. */ |
| 3334 | { |
| 3335 | if (remote_debug) |
| 3336 | fprintf_unfiltered (gdb_stdlog, |
| 3337 | "Bad register packet; fetching a new packet\n"); |
| 3338 | getpkt (buf, (rs->remote_packet_size), 0); |
| 3339 | } |
| 3340 | |
| 3341 | /* Reply describes registers byte by byte, each byte encoded as two |
| 3342 | hex characters. Suck them all up, then supply them to the |
| 3343 | register cacheing/storage mechanism. */ |
| 3344 | |
| 3345 | p = buf; |
| 3346 | for (i = 0; i < rs->sizeof_g_packet; i++) |
| 3347 | { |
| 3348 | if (p[0] == 0) |
| 3349 | break; |
| 3350 | if (p[1] == 0) |
| 3351 | { |
| 3352 | warning (_("Remote reply is of odd length: %s"), buf); |
| 3353 | /* Don't change register_bytes_found in this case, and don't |
| 3354 | print a second warning. */ |
| 3355 | goto supply_them; |
| 3356 | } |
| 3357 | if (p[0] == 'x' && p[1] == 'x') |
| 3358 | regs[i] = 0; /* 'x' */ |
| 3359 | else |
| 3360 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
| 3361 | p += 2; |
| 3362 | } |
| 3363 | |
| 3364 | if (i != register_bytes_found) |
| 3365 | { |
| 3366 | register_bytes_found = i; |
| 3367 | if (REGISTER_BYTES_OK_P () |
| 3368 | && !REGISTER_BYTES_OK (i)) |
| 3369 | warning (_("Remote reply is too short: %s"), buf); |
| 3370 | } |
| 3371 | |
| 3372 | supply_them: |
| 3373 | { |
| 3374 | int i; |
| 3375 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
| 3376 | { |
| 3377 | struct packet_reg *r = &rs->regs[i]; |
| 3378 | if (r->in_g_packet) |
| 3379 | { |
| 3380 | if (r->offset * 2 >= strlen (buf)) |
| 3381 | /* A short packet that didn't include the register's |
| 3382 | value, this implies that the register is zero (and |
| 3383 | not that the register is unavailable). Supply that |
| 3384 | zero value. */ |
| 3385 | regcache_raw_supply (current_regcache, r->regnum, NULL); |
| 3386 | else if (buf[r->offset * 2] == 'x') |
| 3387 | { |
| 3388 | gdb_assert (r->offset * 2 < strlen (buf)); |
| 3389 | /* The register isn't available, mark it as such (at |
| 3390 | the same time setting the value to zero). */ |
| 3391 | regcache_raw_supply (current_regcache, r->regnum, NULL); |
| 3392 | set_register_cached (i, -1); |
| 3393 | } |
| 3394 | else |
| 3395 | regcache_raw_supply (current_regcache, r->regnum, |
| 3396 | regs + r->offset); |
| 3397 | } |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | /* Prepare to store registers. Since we may send them all (using a |
| 3403 | 'G' request), we have to read out the ones we don't want to change |
| 3404 | first. */ |
| 3405 | |
| 3406 | static void |
| 3407 | remote_prepare_to_store (void) |
| 3408 | { |
| 3409 | struct remote_state *rs = get_remote_state (); |
| 3410 | int i; |
| 3411 | char buf[MAX_REGISTER_SIZE]; |
| 3412 | |
| 3413 | /* Make sure the entire registers array is valid. */ |
| 3414 | switch (remote_protocol_P.support) |
| 3415 | { |
| 3416 | case PACKET_DISABLE: |
| 3417 | case PACKET_SUPPORT_UNKNOWN: |
| 3418 | /* Make sure all the necessary registers are cached. */ |
| 3419 | for (i = 0; i < NUM_REGS; i++) |
| 3420 | if (rs->regs[i].in_g_packet) |
| 3421 | regcache_raw_read (current_regcache, rs->regs[i].regnum, buf); |
| 3422 | break; |
| 3423 | case PACKET_ENABLE: |
| 3424 | break; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF |
| 3429 | packet was not recognized. */ |
| 3430 | |
| 3431 | static int |
| 3432 | store_register_using_P (int regnum) |
| 3433 | { |
| 3434 | struct remote_state *rs = get_remote_state (); |
| 3435 | struct packet_reg *reg = packet_reg_from_regnum (rs, regnum); |
| 3436 | /* Try storing a single register. */ |
| 3437 | char *buf = alloca (rs->remote_packet_size); |
| 3438 | char regp[MAX_REGISTER_SIZE]; |
| 3439 | char *p; |
| 3440 | |
| 3441 | sprintf (buf, "P%s=", phex_nz (reg->pnum, 0)); |
| 3442 | p = buf + strlen (buf); |
| 3443 | regcache_raw_collect (current_regcache, reg->regnum, regp); |
| 3444 | bin2hex (regp, p, register_size (current_gdbarch, reg->regnum)); |
| 3445 | remote_send (buf, rs->remote_packet_size); |
| 3446 | |
| 3447 | return buf[0] != '\0'; |
| 3448 | } |
| 3449 | |
| 3450 | |
| 3451 | /* Store register REGNUM, or all registers if REGNUM == -1, from the |
| 3452 | contents of the register cache buffer. FIXME: ignores errors. */ |
| 3453 | |
| 3454 | static void |
| 3455 | remote_store_registers (int regnum) |
| 3456 | { |
| 3457 | struct remote_state *rs = get_remote_state (); |
| 3458 | char *buf; |
| 3459 | char *regs; |
| 3460 | char *p; |
| 3461 | |
| 3462 | set_thread (PIDGET (inferior_ptid), 1); |
| 3463 | |
| 3464 | if (regnum >= 0) |
| 3465 | { |
| 3466 | switch (remote_protocol_P.support) |
| 3467 | { |
| 3468 | case PACKET_DISABLE: |
| 3469 | break; |
| 3470 | case PACKET_ENABLE: |
| 3471 | if (store_register_using_P (regnum)) |
| 3472 | return; |
| 3473 | else |
| 3474 | error (_("Protocol error: P packet not recognized by stub")); |
| 3475 | case PACKET_SUPPORT_UNKNOWN: |
| 3476 | if (store_register_using_P (regnum)) |
| 3477 | { |
| 3478 | /* The stub recognized the 'P' packet. Remember this. */ |
| 3479 | remote_protocol_P.support = PACKET_ENABLE; |
| 3480 | return; |
| 3481 | } |
| 3482 | else |
| 3483 | { |
| 3484 | /* The stub does not support the 'P' packet. Use 'G' |
| 3485 | instead, and don't try using 'P' in the future (it |
| 3486 | will just waste our time). */ |
| 3487 | remote_protocol_P.support = PACKET_DISABLE; |
| 3488 | break; |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | /* Extract all the registers in the regcache copying them into a |
| 3494 | local buffer. */ |
| 3495 | { |
| 3496 | int i; |
| 3497 | regs = alloca (rs->sizeof_g_packet); |
| 3498 | memset (regs, 0, rs->sizeof_g_packet); |
| 3499 | for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++) |
| 3500 | { |
| 3501 | struct packet_reg *r = &rs->regs[i]; |
| 3502 | if (r->in_g_packet) |
| 3503 | regcache_raw_collect (current_regcache, r->regnum, regs + r->offset); |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | /* Command describes registers byte by byte, |
| 3508 | each byte encoded as two hex characters. */ |
| 3509 | buf = alloca (rs->remote_packet_size); |
| 3510 | p = buf; |
| 3511 | *p++ = 'G'; |
| 3512 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
| 3513 | bin2hex (regs, p, register_bytes_found); |
| 3514 | remote_send (buf, (rs->remote_packet_size)); |
| 3515 | } |
| 3516 | \f |
| 3517 | |
| 3518 | /* Return the number of hex digits in num. */ |
| 3519 | |
| 3520 | static int |
| 3521 | hexnumlen (ULONGEST num) |
| 3522 | { |
| 3523 | int i; |
| 3524 | |
| 3525 | for (i = 0; num != 0; i++) |
| 3526 | num >>= 4; |
| 3527 | |
| 3528 | return max (i, 1); |
| 3529 | } |
| 3530 | |
| 3531 | /* Set BUF to the minimum number of hex digits representing NUM. */ |
| 3532 | |
| 3533 | static int |
| 3534 | hexnumstr (char *buf, ULONGEST num) |
| 3535 | { |
| 3536 | int len = hexnumlen (num); |
| 3537 | return hexnumnstr (buf, num, len); |
| 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */ |
| 3542 | |
| 3543 | static int |
| 3544 | hexnumnstr (char *buf, ULONGEST num, int width) |
| 3545 | { |
| 3546 | int i; |
| 3547 | |
| 3548 | buf[width] = '\0'; |
| 3549 | |
| 3550 | for (i = width - 1; i >= 0; i--) |
| 3551 | { |
| 3552 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
| 3553 | num >>= 4; |
| 3554 | } |
| 3555 | |
| 3556 | return width; |
| 3557 | } |
| 3558 | |
| 3559 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ |
| 3560 | |
| 3561 | static CORE_ADDR |
| 3562 | remote_address_masked (CORE_ADDR addr) |
| 3563 | { |
| 3564 | if (remote_address_size > 0 |
| 3565 | && remote_address_size < (sizeof (ULONGEST) * 8)) |
| 3566 | { |
| 3567 | /* Only create a mask when that mask can safely be constructed |
| 3568 | in a ULONGEST variable. */ |
| 3569 | ULONGEST mask = 1; |
| 3570 | mask = (mask << remote_address_size) - 1; |
| 3571 | addr &= mask; |
| 3572 | } |
| 3573 | return addr; |
| 3574 | } |
| 3575 | |
| 3576 | /* Determine whether the remote target supports binary downloading. |
| 3577 | This is accomplished by sending a no-op memory write of zero length |
| 3578 | to the target at the specified address. It does not suffice to send |
| 3579 | the whole packet, since many stubs strip the eighth bit and |
| 3580 | subsequently compute a wrong checksum, which causes real havoc with |
| 3581 | remote_write_bytes. |
| 3582 | |
| 3583 | NOTE: This can still lose if the serial line is not eight-bit |
| 3584 | clean. In cases like this, the user should clear "remote |
| 3585 | X-packet". */ |
| 3586 | |
| 3587 | static void |
| 3588 | check_binary_download (CORE_ADDR addr) |
| 3589 | { |
| 3590 | struct remote_state *rs = get_remote_state (); |
| 3591 | switch (remote_protocol_binary_download.support) |
| 3592 | { |
| 3593 | case PACKET_DISABLE: |
| 3594 | break; |
| 3595 | case PACKET_ENABLE: |
| 3596 | break; |
| 3597 | case PACKET_SUPPORT_UNKNOWN: |
| 3598 | { |
| 3599 | char *buf = alloca (rs->remote_packet_size); |
| 3600 | char *p; |
| 3601 | |
| 3602 | p = buf; |
| 3603 | *p++ = 'X'; |
| 3604 | p += hexnumstr (p, (ULONGEST) addr); |
| 3605 | *p++ = ','; |
| 3606 | p += hexnumstr (p, (ULONGEST) 0); |
| 3607 | *p++ = ':'; |
| 3608 | *p = '\0'; |
| 3609 | |
| 3610 | putpkt_binary (buf, (int) (p - buf)); |
| 3611 | getpkt (buf, (rs->remote_packet_size), 0); |
| 3612 | |
| 3613 | if (buf[0] == '\0') |
| 3614 | { |
| 3615 | if (remote_debug) |
| 3616 | fprintf_unfiltered (gdb_stdlog, |
| 3617 | "binary downloading NOT suppported by target\n"); |
| 3618 | remote_protocol_binary_download.support = PACKET_DISABLE; |
| 3619 | } |
| 3620 | else |
| 3621 | { |
| 3622 | if (remote_debug) |
| 3623 | fprintf_unfiltered (gdb_stdlog, |
| 3624 | "binary downloading suppported by target\n"); |
| 3625 | remote_protocol_binary_download.support = PACKET_ENABLE; |
| 3626 | } |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | /* Write memory data directly to the remote machine. |
| 3633 | This does not inform the data cache; the data cache uses this. |
| 3634 | MEMADDR is the address in the remote memory space. |
| 3635 | MYADDR is the address of the buffer in our space. |
| 3636 | LEN is the number of bytes. |
| 3637 | |
| 3638 | Returns number of bytes transferred, or 0 (setting errno) for |
| 3639 | error. Only transfer a single packet. */ |
| 3640 | |
| 3641 | int |
| 3642 | remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
| 3643 | { |
| 3644 | unsigned char *buf; |
| 3645 | unsigned char *p; |
| 3646 | unsigned char *plen; |
| 3647 | long sizeof_buf; |
| 3648 | int plenlen; |
| 3649 | int todo; |
| 3650 | int nr_bytes; |
| 3651 | int payload_size; |
| 3652 | unsigned char *payload_start; |
| 3653 | |
| 3654 | /* Verify that the target can support a binary download. */ |
| 3655 | check_binary_download (memaddr); |
| 3656 | |
| 3657 | payload_size = get_memory_write_packet_size (); |
| 3658 | |
| 3659 | /* Compute the size, and then allocate space for the largest |
| 3660 | possible packet. Include space for an extra trailing NUL. */ |
| 3661 | sizeof_buf = payload_size + 1; |
| 3662 | buf = alloca (sizeof_buf); |
| 3663 | |
| 3664 | /* Compute the size of the actual payload by subtracting out the |
| 3665 | packet header and footer overhead: "$M<memaddr>,<len>:...#nn". |
| 3666 | */ |
| 3667 | payload_size -= strlen ("$M,:#NN"); |
| 3668 | payload_size -= hexnumlen (memaddr); |
| 3669 | |
| 3670 | /* Construct the packet header: "[MX]<memaddr>,<len>:". */ |
| 3671 | |
| 3672 | /* Append "[XM]". Compute a best guess of the number of bytes |
| 3673 | actually transfered. */ |
| 3674 | p = buf; |
| 3675 | switch (remote_protocol_binary_download.support) |
| 3676 | { |
| 3677 | case PACKET_ENABLE: |
| 3678 | *p++ = 'X'; |
| 3679 | /* Best guess at number of bytes that will fit. */ |
| 3680 | todo = min (len, payload_size); |
| 3681 | payload_size -= hexnumlen (todo); |
| 3682 | todo = min (todo, payload_size); |
| 3683 | break; |
| 3684 | case PACKET_DISABLE: |
| 3685 | *p++ = 'M'; |
| 3686 | /* Num bytes that will fit. */ |
| 3687 | todo = min (len, payload_size / 2); |
| 3688 | payload_size -= hexnumlen (todo); |
| 3689 | todo = min (todo, payload_size / 2); |
| 3690 | break; |
| 3691 | case PACKET_SUPPORT_UNKNOWN: |
| 3692 | internal_error (__FILE__, __LINE__, |
| 3693 | _("remote_write_bytes: bad internal state")); |
| 3694 | default: |
| 3695 | internal_error (__FILE__, __LINE__, _("bad switch")); |
| 3696 | } |
| 3697 | if (todo <= 0) |
| 3698 | internal_error (__FILE__, __LINE__, |
| 3699 | _("minumum packet size too small to write data")); |
| 3700 | |
| 3701 | /* Append "<memaddr>". */ |
| 3702 | memaddr = remote_address_masked (memaddr); |
| 3703 | p += hexnumstr (p, (ULONGEST) memaddr); |
| 3704 | |
| 3705 | /* Append ",". */ |
| 3706 | *p++ = ','; |
| 3707 | |
| 3708 | /* Append <len>. Retain the location/size of <len>. It may need to |
| 3709 | be adjusted once the packet body has been created. */ |
| 3710 | plen = p; |
| 3711 | plenlen = hexnumstr (p, (ULONGEST) todo); |
| 3712 | p += plenlen; |
| 3713 | |
| 3714 | /* Append ":". */ |
| 3715 | *p++ = ':'; |
| 3716 | *p = '\0'; |
| 3717 | |
| 3718 | /* Append the packet body. */ |
| 3719 | payload_start = p; |
| 3720 | switch (remote_protocol_binary_download.support) |
| 3721 | { |
| 3722 | case PACKET_ENABLE: |
| 3723 | /* Binary mode. Send target system values byte by byte, in |
| 3724 | increasing byte addresses. Only escape certain critical |
| 3725 | characters. */ |
| 3726 | for (nr_bytes = 0; |
| 3727 | (nr_bytes < todo) && (p - payload_start) < payload_size; |
| 3728 | nr_bytes++) |
| 3729 | { |
| 3730 | switch (myaddr[nr_bytes] & 0xff) |
| 3731 | { |
| 3732 | case '$': |
| 3733 | case '#': |
| 3734 | case 0x7d: |
| 3735 | /* These must be escaped. */ |
| 3736 | *p++ = 0x7d; |
| 3737 | *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20; |
| 3738 | break; |
| 3739 | default: |
| 3740 | *p++ = myaddr[nr_bytes] & 0xff; |
| 3741 | break; |
| 3742 | } |
| 3743 | } |
| 3744 | if (nr_bytes < todo) |
| 3745 | { |
| 3746 | /* Escape chars have filled up the buffer prematurely, |
| 3747 | and we have actually sent fewer bytes than planned. |
| 3748 | Fix-up the length field of the packet. Use the same |
| 3749 | number of characters as before. */ |
| 3750 | plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen); |
| 3751 | *plen = ':'; /* overwrite \0 from hexnumnstr() */ |
| 3752 | } |
| 3753 | break; |
| 3754 | case PACKET_DISABLE: |
| 3755 | /* Normal mode: Send target system values byte by byte, in |
| 3756 | increasing byte addresses. Each byte is encoded as a two hex |
| 3757 | value. */ |
| 3758 | nr_bytes = bin2hex (myaddr, p, todo); |
| 3759 | p += 2 * nr_bytes; |
| 3760 | break; |
| 3761 | case PACKET_SUPPORT_UNKNOWN: |
| 3762 | internal_error (__FILE__, __LINE__, |
| 3763 | _("remote_write_bytes: bad internal state")); |
| 3764 | default: |
| 3765 | internal_error (__FILE__, __LINE__, _("bad switch")); |
| 3766 | } |
| 3767 | |
| 3768 | putpkt_binary (buf, (int) (p - buf)); |
| 3769 | getpkt (buf, sizeof_buf, 0); |
| 3770 | |
| 3771 | if (buf[0] == 'E') |
| 3772 | { |
| 3773 | /* There is no correspondance between what the remote protocol |
| 3774 | uses for errors and errno codes. We would like a cleaner way |
| 3775 | of representing errors (big enough to include errno codes, |
| 3776 | bfd_error codes, and others). But for now just return EIO. */ |
| 3777 | errno = EIO; |
| 3778 | return 0; |
| 3779 | } |
| 3780 | |
| 3781 | /* Return NR_BYTES, not TODO, in case escape chars caused us to send |
| 3782 | fewer bytes than we'd planned. */ |
| 3783 | return nr_bytes; |
| 3784 | } |
| 3785 | |
| 3786 | /* Read memory data directly from the remote machine. |
| 3787 | This does not use the data cache; the data cache uses this. |
| 3788 | MEMADDR is the address in the remote memory space. |
| 3789 | MYADDR is the address of the buffer in our space. |
| 3790 | LEN is the number of bytes. |
| 3791 | |
| 3792 | Returns number of bytes transferred, or 0 for error. */ |
| 3793 | |
| 3794 | /* NOTE: cagney/1999-10-18: This function (and its siblings in other |
| 3795 | remote targets) shouldn't attempt to read the entire buffer. |
| 3796 | Instead it should read a single packet worth of data and then |
| 3797 | return the byte size of that packet to the caller. The caller (its |
| 3798 | caller and its callers caller ;-) already contains code for |
| 3799 | handling partial reads. */ |
| 3800 | |
| 3801 | int |
| 3802 | remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
| 3803 | { |
| 3804 | char *buf; |
| 3805 | int max_buf_size; /* Max size of packet output buffer. */ |
| 3806 | long sizeof_buf; |
| 3807 | int origlen; |
| 3808 | |
| 3809 | /* Create a buffer big enough for this packet. */ |
| 3810 | max_buf_size = get_memory_read_packet_size (); |
| 3811 | sizeof_buf = max_buf_size + 1; /* Space for trailing NULL. */ |
| 3812 | buf = alloca (sizeof_buf); |
| 3813 | |
| 3814 | origlen = len; |
| 3815 | while (len > 0) |
| 3816 | { |
| 3817 | char *p; |
| 3818 | int todo; |
| 3819 | int i; |
| 3820 | |
| 3821 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
| 3822 | |
| 3823 | /* construct "m"<memaddr>","<len>" */ |
| 3824 | /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */ |
| 3825 | memaddr = remote_address_masked (memaddr); |
| 3826 | p = buf; |
| 3827 | *p++ = 'm'; |
| 3828 | p += hexnumstr (p, (ULONGEST) memaddr); |
| 3829 | *p++ = ','; |
| 3830 | p += hexnumstr (p, (ULONGEST) todo); |
| 3831 | *p = '\0'; |
| 3832 | |
| 3833 | putpkt (buf); |
| 3834 | getpkt (buf, sizeof_buf, 0); |
| 3835 | |
| 3836 | if (buf[0] == 'E' |
| 3837 | && isxdigit (buf[1]) && isxdigit (buf[2]) |
| 3838 | && buf[3] == '\0') |
| 3839 | { |
| 3840 | /* There is no correspondance between what the remote |
| 3841 | protocol uses for errors and errno codes. We would like |
| 3842 | a cleaner way of representing errors (big enough to |
| 3843 | include errno codes, bfd_error codes, and others). But |
| 3844 | for now just return EIO. */ |
| 3845 | errno = EIO; |
| 3846 | return 0; |
| 3847 | } |
| 3848 | |
| 3849 | /* Reply describes memory byte by byte, |
| 3850 | each byte encoded as two hex characters. */ |
| 3851 | |
| 3852 | p = buf; |
| 3853 | if ((i = hex2bin (p, myaddr, todo)) < todo) |
| 3854 | { |
| 3855 | /* Reply is short. This means that we were able to read |
| 3856 | only part of what we wanted to. */ |
| 3857 | return i + (origlen - len); |
| 3858 | } |
| 3859 | myaddr += todo; |
| 3860 | memaddr += todo; |
| 3861 | len -= todo; |
| 3862 | } |
| 3863 | return origlen; |
| 3864 | } |
| 3865 | \f |
| 3866 | /* Read or write LEN bytes from inferior memory at MEMADDR, |
| 3867 | transferring to or from debugger address BUFFER. Write to inferior |
| 3868 | if SHOULD_WRITE is nonzero. Returns length of data written or |
| 3869 | read; 0 for error. TARGET is unused. */ |
| 3870 | |
| 3871 | static int |
| 3872 | remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len, |
| 3873 | int should_write, struct mem_attrib *attrib, |
| 3874 | struct target_ops *target) |
| 3875 | { |
| 3876 | CORE_ADDR targ_addr; |
| 3877 | int targ_len; |
| 3878 | int res; |
| 3879 | |
| 3880 | /* Should this be the selected frame? */ |
| 3881 | gdbarch_remote_translate_xfer_address (current_gdbarch, |
| 3882 | current_regcache, |
| 3883 | mem_addr, mem_len, |
| 3884 | &targ_addr, &targ_len); |
| 3885 | if (targ_len <= 0) |
| 3886 | return 0; |
| 3887 | |
| 3888 | if (should_write) |
| 3889 | res = remote_write_bytes (targ_addr, buffer, targ_len); |
| 3890 | else |
| 3891 | res = remote_read_bytes (targ_addr, buffer, targ_len); |
| 3892 | |
| 3893 | return res; |
| 3894 | } |
| 3895 | |
| 3896 | static void |
| 3897 | remote_files_info (struct target_ops *ignore) |
| 3898 | { |
| 3899 | puts_filtered ("Debugging a target over a serial line.\n"); |
| 3900 | } |
| 3901 | \f |
| 3902 | /* Stuff for dealing with the packets which are part of this protocol. |
| 3903 | See comment at top of file for details. */ |
| 3904 | |
| 3905 | /* Read a single character from the remote end, masking it down to 7 |
| 3906 | bits. */ |
| 3907 | |
| 3908 | static int |
| 3909 | readchar (int timeout) |
| 3910 | { |
| 3911 | int ch; |
| 3912 | |
| 3913 | ch = serial_readchar (remote_desc, timeout); |
| 3914 | |
| 3915 | if (ch >= 0) |
| 3916 | return (ch & 0x7f); |
| 3917 | |
| 3918 | switch ((enum serial_rc) ch) |
| 3919 | { |
| 3920 | case SERIAL_EOF: |
| 3921 | target_mourn_inferior (); |
| 3922 | error (_("Remote connection closed")); |
| 3923 | /* no return */ |
| 3924 | case SERIAL_ERROR: |
| 3925 | perror_with_name (_("Remote communication error")); |
| 3926 | /* no return */ |
| 3927 | case SERIAL_TIMEOUT: |
| 3928 | break; |
| 3929 | } |
| 3930 | return ch; |
| 3931 | } |
| 3932 | |
| 3933 | /* Send the command in BUF to the remote machine, and read the reply |
| 3934 | into BUF. Report an error if we get an error reply. */ |
| 3935 | |
| 3936 | static void |
| 3937 | remote_send (char *buf, |
| 3938 | long sizeof_buf) |
| 3939 | { |
| 3940 | putpkt (buf); |
| 3941 | getpkt (buf, sizeof_buf, 0); |
| 3942 | |
| 3943 | if (buf[0] == 'E') |
| 3944 | error (_("Remote failure reply: %s"), buf); |
| 3945 | } |
| 3946 | |
| 3947 | /* Display a null-terminated packet on stdout, for debugging, using C |
| 3948 | string notation. */ |
| 3949 | |
| 3950 | static void |
| 3951 | print_packet (char *buf) |
| 3952 | { |
| 3953 | puts_filtered ("\""); |
| 3954 | fputstr_filtered (buf, '"', gdb_stdout); |
| 3955 | puts_filtered ("\""); |
| 3956 | } |
| 3957 | |
| 3958 | int |
| 3959 | putpkt (char *buf) |
| 3960 | { |
| 3961 | return putpkt_binary (buf, strlen (buf)); |
| 3962 | } |
| 3963 | |
| 3964 | /* Send a packet to the remote machine, with error checking. The data |
| 3965 | of the packet is in BUF. The string in BUF can be at most |
| 3966 | (rs->remote_packet_size) - 5 to account for the $, # and checksum, |
| 3967 | and for a possible /0 if we are debugging (remote_debug) and want |
| 3968 | to print the sent packet as a string. */ |
| 3969 | |
| 3970 | static int |
| 3971 | putpkt_binary (char *buf, int cnt) |
| 3972 | { |
| 3973 | struct remote_state *rs = get_remote_state (); |
| 3974 | int i; |
| 3975 | unsigned char csum = 0; |
| 3976 | char *buf2 = alloca (cnt + 6); |
| 3977 | long sizeof_junkbuf = (rs->remote_packet_size); |
| 3978 | char *junkbuf = alloca (sizeof_junkbuf); |
| 3979 | |
| 3980 | int ch; |
| 3981 | int tcount = 0; |
| 3982 | char *p; |
| 3983 | |
| 3984 | /* Copy the packet into buffer BUF2, encapsulating it |
| 3985 | and giving it a checksum. */ |
| 3986 | |
| 3987 | p = buf2; |
| 3988 | *p++ = '$'; |
| 3989 | |
| 3990 | for (i = 0; i < cnt; i++) |
| 3991 | { |
| 3992 | csum += buf[i]; |
| 3993 | *p++ = buf[i]; |
| 3994 | } |
| 3995 | *p++ = '#'; |
| 3996 | *p++ = tohex ((csum >> 4) & 0xf); |
| 3997 | *p++ = tohex (csum & 0xf); |
| 3998 | |
| 3999 | /* Send it over and over until we get a positive ack. */ |
| 4000 | |
| 4001 | while (1) |
| 4002 | { |
| 4003 | int started_error_output = 0; |
| 4004 | |
| 4005 | if (remote_debug) |
| 4006 | { |
| 4007 | *p = '\0'; |
| 4008 | fprintf_unfiltered (gdb_stdlog, "Sending packet: "); |
| 4009 | fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog); |
| 4010 | fprintf_unfiltered (gdb_stdlog, "..."); |
| 4011 | gdb_flush (gdb_stdlog); |
| 4012 | } |
| 4013 | if (serial_write (remote_desc, buf2, p - buf2)) |
| 4014 | perror_with_name (_("putpkt: write failed")); |
| 4015 | |
| 4016 | /* Read until either a timeout occurs (-2) or '+' is read. */ |
| 4017 | while (1) |
| 4018 | { |
| 4019 | ch = readchar (remote_timeout); |
| 4020 | |
| 4021 | if (remote_debug) |
| 4022 | { |
| 4023 | switch (ch) |
| 4024 | { |
| 4025 | case '+': |
| 4026 | case '-': |
| 4027 | case SERIAL_TIMEOUT: |
| 4028 | case '$': |
| 4029 | if (started_error_output) |
| 4030 | { |
| 4031 | putchar_unfiltered ('\n'); |
| 4032 | started_error_output = 0; |
| 4033 | } |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | switch (ch) |
| 4038 | { |
| 4039 | case '+': |
| 4040 | if (remote_debug) |
| 4041 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
| 4042 | return 1; |
| 4043 | case '-': |
| 4044 | if (remote_debug) |
| 4045 | fprintf_unfiltered (gdb_stdlog, "Nak\n"); |
| 4046 | case SERIAL_TIMEOUT: |
| 4047 | tcount++; |
| 4048 | if (tcount > 3) |
| 4049 | return 0; |
| 4050 | break; /* Retransmit buffer. */ |
| 4051 | case '$': |
| 4052 | { |
| 4053 | if (remote_debug) |
| 4054 | fprintf_unfiltered (gdb_stdlog, |
| 4055 | "Packet instead of Ack, ignoring it\n"); |
| 4056 | /* It's probably an old response sent because an ACK |
| 4057 | was lost. Gobble up the packet and ack it so it |
| 4058 | doesn't get retransmitted when we resend this |
| 4059 | packet. */ |
| 4060 | read_frame (junkbuf, sizeof_junkbuf); |
| 4061 | serial_write (remote_desc, "+", 1); |
| 4062 | continue; /* Now, go look for +. */ |
| 4063 | } |
| 4064 | default: |
| 4065 | if (remote_debug) |
| 4066 | { |
| 4067 | if (!started_error_output) |
| 4068 | { |
| 4069 | started_error_output = 1; |
| 4070 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
| 4071 | } |
| 4072 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
| 4073 | } |
| 4074 | continue; |
| 4075 | } |
| 4076 | break; /* Here to retransmit. */ |
| 4077 | } |
| 4078 | |
| 4079 | #if 0 |
| 4080 | /* This is wrong. If doing a long backtrace, the user should be |
| 4081 | able to get out next time we call QUIT, without anything as |
| 4082 | violent as interrupt_query. If we want to provide a way out of |
| 4083 | here without getting to the next QUIT, it should be based on |
| 4084 | hitting ^C twice as in remote_wait. */ |
| 4085 | if (quit_flag) |
| 4086 | { |
| 4087 | quit_flag = 0; |
| 4088 | interrupt_query (); |
| 4089 | } |
| 4090 | #endif |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | /* Come here after finding the start of the frame. Collect the rest |
| 4095 | into BUF, verifying the checksum, length, and handling run-length |
| 4096 | compression. No more than sizeof_buf-1 characters are read so that |
| 4097 | the buffer can be NUL terminated. |
| 4098 | |
| 4099 | Returns -1 on error, number of characters in buffer (ignoring the |
| 4100 | trailing NULL) on success. (could be extended to return one of the |
| 4101 | SERIAL status indications). */ |
| 4102 | |
| 4103 | static long |
| 4104 | read_frame (char *buf, |
| 4105 | long sizeof_buf) |
| 4106 | { |
| 4107 | unsigned char csum; |
| 4108 | long bc; |
| 4109 | int c; |
| 4110 | |
| 4111 | csum = 0; |
| 4112 | bc = 0; |
| 4113 | |
| 4114 | while (1) |
| 4115 | { |
| 4116 | /* ASSERT (bc < sizeof_buf - 1) - space for trailing NULL. */ |
| 4117 | c = readchar (remote_timeout); |
| 4118 | switch (c) |
| 4119 | { |
| 4120 | case SERIAL_TIMEOUT: |
| 4121 | if (remote_debug) |
| 4122 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
| 4123 | return -1; |
| 4124 | case '$': |
| 4125 | if (remote_debug) |
| 4126 | fputs_filtered ("Saw new packet start in middle of old one\n", |
| 4127 | gdb_stdlog); |
| 4128 | return -1; /* Start a new packet, count retries. */ |
| 4129 | case '#': |
| 4130 | { |
| 4131 | unsigned char pktcsum; |
| 4132 | int check_0 = 0; |
| 4133 | int check_1 = 0; |
| 4134 | |
| 4135 | buf[bc] = '\0'; |
| 4136 | |
| 4137 | check_0 = readchar (remote_timeout); |
| 4138 | if (check_0 >= 0) |
| 4139 | check_1 = readchar (remote_timeout); |
| 4140 | |
| 4141 | if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT) |
| 4142 | { |
| 4143 | if (remote_debug) |
| 4144 | fputs_filtered ("Timeout in checksum, retrying\n", |
| 4145 | gdb_stdlog); |
| 4146 | return -1; |
| 4147 | } |
| 4148 | else if (check_0 < 0 || check_1 < 0) |
| 4149 | { |
| 4150 | if (remote_debug) |
| 4151 | fputs_filtered ("Communication error in checksum\n", |
| 4152 | gdb_stdlog); |
| 4153 | return -1; |
| 4154 | } |
| 4155 | |
| 4156 | pktcsum = (fromhex (check_0) << 4) | fromhex (check_1); |
| 4157 | if (csum == pktcsum) |
| 4158 | return bc; |
| 4159 | |
| 4160 | if (remote_debug) |
| 4161 | { |
| 4162 | fprintf_filtered (gdb_stdlog, |
| 4163 | "Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
| 4164 | pktcsum, csum); |
| 4165 | fputs_filtered (buf, gdb_stdlog); |
| 4166 | fputs_filtered ("\n", gdb_stdlog); |
| 4167 | } |
| 4168 | /* Number of characters in buffer ignoring trailing |
| 4169 | NULL. */ |
| 4170 | return -1; |
| 4171 | } |
| 4172 | case '*': /* Run length encoding. */ |
| 4173 | { |
| 4174 | int repeat; |
| 4175 | csum += c; |
| 4176 | |
| 4177 | c = readchar (remote_timeout); |
| 4178 | csum += c; |
| 4179 | repeat = c - ' ' + 3; /* Compute repeat count. */ |
| 4180 | |
| 4181 | /* The character before ``*'' is repeated. */ |
| 4182 | |
| 4183 | if (repeat > 0 && repeat <= 255 |
| 4184 | && bc > 0 |
| 4185 | && bc + repeat - 1 < sizeof_buf - 1) |
| 4186 | { |
| 4187 | memset (&buf[bc], buf[bc - 1], repeat); |
| 4188 | bc += repeat; |
| 4189 | continue; |
| 4190 | } |
| 4191 | |
| 4192 | buf[bc] = '\0'; |
| 4193 | printf_filtered (_("Repeat count %d too large for buffer: "), |
| 4194 | repeat); |
| 4195 | puts_filtered (buf); |
| 4196 | puts_filtered ("\n"); |
| 4197 | return -1; |
| 4198 | } |
| 4199 | default: |
| 4200 | if (bc < sizeof_buf - 1) |
| 4201 | { |
| 4202 | buf[bc++] = c; |
| 4203 | csum += c; |
| 4204 | continue; |
| 4205 | } |
| 4206 | |
| 4207 | buf[bc] = '\0'; |
| 4208 | puts_filtered ("Remote packet too long: "); |
| 4209 | puts_filtered (buf); |
| 4210 | puts_filtered ("\n"); |
| 4211 | |
| 4212 | return -1; |
| 4213 | } |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | /* Read a packet from the remote machine, with error checking, and |
| 4218 | store it in BUF. If FOREVER, wait forever rather than timing out; |
| 4219 | this is used (in synchronous mode) to wait for a target that is is |
| 4220 | executing user code to stop. */ |
| 4221 | /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we |
| 4222 | don't have to change all the calls to getpkt to deal with the |
| 4223 | return value, because at the moment I don't know what the right |
| 4224 | thing to do it for those. */ |
| 4225 | void |
| 4226 | getpkt (char *buf, |
| 4227 | long sizeof_buf, |
| 4228 | int forever) |
| 4229 | { |
| 4230 | int timed_out; |
| 4231 | |
| 4232 | timed_out = getpkt_sane (buf, sizeof_buf, forever); |
| 4233 | } |
| 4234 | |
| 4235 | |
| 4236 | /* Read a packet from the remote machine, with error checking, and |
| 4237 | store it in BUF. If FOREVER, wait forever rather than timing out; |
| 4238 | this is used (in synchronous mode) to wait for a target that is is |
| 4239 | executing user code to stop. If FOREVER == 0, this function is |
| 4240 | allowed to time out gracefully and return an indication of this to |
| 4241 | the caller. */ |
| 4242 | static int |
| 4243 | getpkt_sane (char *buf, |
| 4244 | long sizeof_buf, |
| 4245 | int forever) |
| 4246 | { |
| 4247 | int c; |
| 4248 | int tries; |
| 4249 | int timeout; |
| 4250 | int val; |
| 4251 | |
| 4252 | strcpy (buf, "timeout"); |
| 4253 | |
| 4254 | if (forever) |
| 4255 | { |
| 4256 | timeout = watchdog > 0 ? watchdog : -1; |
| 4257 | } |
| 4258 | |
| 4259 | else |
| 4260 | timeout = remote_timeout; |
| 4261 | |
| 4262 | #define MAX_TRIES 3 |
| 4263 | |
| 4264 | for (tries = 1; tries <= MAX_TRIES; tries++) |
| 4265 | { |
| 4266 | /* This can loop forever if the remote side sends us characters |
| 4267 | continuously, but if it pauses, we'll get a zero from |
| 4268 | readchar because of timeout. Then we'll count that as a |
| 4269 | retry. */ |
| 4270 | |
| 4271 | /* Note that we will only wait forever prior to the start of a |
| 4272 | packet. After that, we expect characters to arrive at a |
| 4273 | brisk pace. They should show up within remote_timeout |
| 4274 | intervals. */ |
| 4275 | |
| 4276 | do |
| 4277 | { |
| 4278 | c = readchar (timeout); |
| 4279 | |
| 4280 | if (c == SERIAL_TIMEOUT) |
| 4281 | { |
| 4282 | if (forever) /* Watchdog went off? Kill the target. */ |
| 4283 | { |
| 4284 | QUIT; |
| 4285 | target_mourn_inferior (); |
| 4286 | error (_("Watchdog has expired. Target detached.")); |
| 4287 | } |
| 4288 | if (remote_debug) |
| 4289 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
| 4290 | goto retry; |
| 4291 | } |
| 4292 | } |
| 4293 | while (c != '$'); |
| 4294 | |
| 4295 | /* We've found the start of a packet, now collect the data. */ |
| 4296 | |
| 4297 | val = read_frame (buf, sizeof_buf); |
| 4298 | |
| 4299 | if (val >= 0) |
| 4300 | { |
| 4301 | if (remote_debug) |
| 4302 | { |
| 4303 | fprintf_unfiltered (gdb_stdlog, "Packet received: "); |
| 4304 | fputstr_unfiltered (buf, 0, gdb_stdlog); |
| 4305 | fprintf_unfiltered (gdb_stdlog, "\n"); |
| 4306 | } |
| 4307 | serial_write (remote_desc, "+", 1); |
| 4308 | return 0; |
| 4309 | } |
| 4310 | |
| 4311 | /* Try the whole thing again. */ |
| 4312 | retry: |
| 4313 | serial_write (remote_desc, "-", 1); |
| 4314 | } |
| 4315 | |
| 4316 | /* We have tried hard enough, and just can't receive the packet. |
| 4317 | Give up. */ |
| 4318 | |
| 4319 | printf_unfiltered (_("Ignoring packet error, continuing...\n")); |
| 4320 | serial_write (remote_desc, "+", 1); |
| 4321 | return 1; |
| 4322 | } |
| 4323 | \f |
| 4324 | static void |
| 4325 | remote_kill (void) |
| 4326 | { |
| 4327 | /* For some mysterious reason, wait_for_inferior calls kill instead of |
| 4328 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ |
| 4329 | if (kill_kludge) |
| 4330 | { |
| 4331 | kill_kludge = 0; |
| 4332 | target_mourn_inferior (); |
| 4333 | return; |
| 4334 | } |
| 4335 | |
| 4336 | /* Use catch_errors so the user can quit from gdb even when we aren't on |
| 4337 | speaking terms with the remote system. */ |
| 4338 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
| 4339 | |
| 4340 | /* Don't wait for it to die. I'm not really sure it matters whether |
| 4341 | we do or not. For the existing stubs, kill is a noop. */ |
| 4342 | target_mourn_inferior (); |
| 4343 | } |
| 4344 | |
| 4345 | /* Async version of remote_kill. */ |
| 4346 | static void |
| 4347 | remote_async_kill (void) |
| 4348 | { |
| 4349 | /* Unregister the file descriptor from the event loop. */ |
| 4350 | if (target_is_async_p ()) |
| 4351 | serial_async (remote_desc, NULL, 0); |
| 4352 | |
| 4353 | /* For some mysterious reason, wait_for_inferior calls kill instead of |
| 4354 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ |
| 4355 | if (kill_kludge) |
| 4356 | { |
| 4357 | kill_kludge = 0; |
| 4358 | target_mourn_inferior (); |
| 4359 | return; |
| 4360 | } |
| 4361 | |
| 4362 | /* Use catch_errors so the user can quit from gdb even when we |
| 4363 | aren't on speaking terms with the remote system. */ |
| 4364 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
| 4365 | |
| 4366 | /* Don't wait for it to die. I'm not really sure it matters whether |
| 4367 | we do or not. For the existing stubs, kill is a noop. */ |
| 4368 | target_mourn_inferior (); |
| 4369 | } |
| 4370 | |
| 4371 | static void |
| 4372 | remote_mourn (void) |
| 4373 | { |
| 4374 | remote_mourn_1 (&remote_ops); |
| 4375 | } |
| 4376 | |
| 4377 | static void |
| 4378 | remote_async_mourn (void) |
| 4379 | { |
| 4380 | remote_mourn_1 (&remote_async_ops); |
| 4381 | } |
| 4382 | |
| 4383 | static void |
| 4384 | extended_remote_mourn (void) |
| 4385 | { |
| 4386 | /* We do _not_ want to mourn the target like this; this will |
| 4387 | remove the extended remote target from the target stack, |
| 4388 | and the next time the user says "run" it'll fail. |
| 4389 | |
| 4390 | FIXME: What is the right thing to do here? */ |
| 4391 | #if 0 |
| 4392 | remote_mourn_1 (&extended_remote_ops); |
| 4393 | #endif |
| 4394 | } |
| 4395 | |
| 4396 | /* Worker function for remote_mourn. */ |
| 4397 | static void |
| 4398 | remote_mourn_1 (struct target_ops *target) |
| 4399 | { |
| 4400 | unpush_target (target); |
| 4401 | generic_mourn_inferior (); |
| 4402 | } |
| 4403 | |
| 4404 | /* In the extended protocol we want to be able to do things like |
| 4405 | "run" and have them basically work as expected. So we need |
| 4406 | a special create_inferior function. |
| 4407 | |
| 4408 | FIXME: One day add support for changing the exec file |
| 4409 | we're debugging, arguments and an environment. */ |
| 4410 | |
| 4411 | static void |
| 4412 | extended_remote_create_inferior (char *exec_file, char *args, |
| 4413 | char **env, int from_tty) |
| 4414 | { |
| 4415 | /* Rip out the breakpoints; we'll reinsert them after restarting |
| 4416 | the remote server. */ |
| 4417 | remove_breakpoints (); |
| 4418 | |
| 4419 | /* Now restart the remote server. */ |
| 4420 | extended_remote_restart (); |
| 4421 | |
| 4422 | /* Now put the breakpoints back in. This way we're safe if the |
| 4423 | restart function works via a unix fork on the remote side. */ |
| 4424 | insert_breakpoints (); |
| 4425 | |
| 4426 | /* Clean up from the last time we were running. */ |
| 4427 | clear_proceed_status (); |
| 4428 | |
| 4429 | /* Let the remote process run. */ |
| 4430 | proceed (-1, TARGET_SIGNAL_0, 0); |
| 4431 | } |
| 4432 | |
| 4433 | /* Async version of extended_remote_create_inferior. */ |
| 4434 | static void |
| 4435 | extended_remote_async_create_inferior (char *exec_file, char *args, |
| 4436 | char **env, int from_tty) |
| 4437 | { |
| 4438 | /* Rip out the breakpoints; we'll reinsert them after restarting |
| 4439 | the remote server. */ |
| 4440 | remove_breakpoints (); |
| 4441 | |
| 4442 | /* If running asynchronously, register the target file descriptor |
| 4443 | with the event loop. */ |
| 4444 | if (target_can_async_p ()) |
| 4445 | target_async (inferior_event_handler, 0); |
| 4446 | |
| 4447 | /* Now restart the remote server. */ |
| 4448 | extended_remote_restart (); |
| 4449 | |
| 4450 | /* Now put the breakpoints back in. This way we're safe if the |
| 4451 | restart function works via a unix fork on the remote side. */ |
| 4452 | insert_breakpoints (); |
| 4453 | |
| 4454 | /* Clean up from the last time we were running. */ |
| 4455 | clear_proceed_status (); |
| 4456 | |
| 4457 | /* Let the remote process run. */ |
| 4458 | proceed (-1, TARGET_SIGNAL_0, 0); |
| 4459 | } |
| 4460 | \f |
| 4461 | |
| 4462 | /* On some machines, e.g. 68k, we may use a different breakpoint |
| 4463 | instruction than other targets; in those use |
| 4464 | DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC. |
| 4465 | Also, bi-endian targets may define |
| 4466 | DEPRECATED_LITTLE_REMOTE_BREAKPOINT and |
| 4467 | DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we |
| 4468 | just call the standard routines that are in mem-break.c. */ |
| 4469 | |
| 4470 | /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator |
| 4471 | target should use an identical BREAKPOINT_FROM_PC. As for native, |
| 4472 | the ARCH-OS-tdep.c code can override the default. */ |
| 4473 | |
| 4474 | #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT) |
| 4475 | #define DEPRECATED_REMOTE_BREAKPOINT |
| 4476 | #endif |
| 4477 | |
| 4478 | #ifdef DEPRECATED_REMOTE_BREAKPOINT |
| 4479 | |
| 4480 | /* If the target isn't bi-endian, just pretend it is. */ |
| 4481 | #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) |
| 4482 | #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT |
| 4483 | #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT |
| 4484 | #endif |
| 4485 | |
| 4486 | static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT; |
| 4487 | static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT; |
| 4488 | |
| 4489 | #endif /* DEPRECATED_REMOTE_BREAKPOINT */ |
| 4490 | |
| 4491 | /* Insert a breakpoint on targets that don't have any better |
| 4492 | breakpoint support. We read the contents of the target location |
| 4493 | and stash it, then overwrite it with a breakpoint instruction. |
| 4494 | ADDR is the target location in the target machine. CONTENTS_CACHE |
| 4495 | is a pointer to memory allocated for saving the target contents. |
| 4496 | It is guaranteed by the caller to be long enough to save the number |
| 4497 | of bytes returned by BREAKPOINT_FROM_PC. */ |
| 4498 | |
| 4499 | static int |
| 4500 | remote_insert_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache) |
| 4501 | { |
| 4502 | struct remote_state *rs = get_remote_state (); |
| 4503 | #ifdef DEPRECATED_REMOTE_BREAKPOINT |
| 4504 | int val; |
| 4505 | #endif |
| 4506 | int bp_size; |
| 4507 | |
| 4508 | /* Try the "Z" s/w breakpoint packet if it is not already disabled. |
| 4509 | If it succeeds, then set the support to PACKET_ENABLE. If it |
| 4510 | fails, and the user has explicitly requested the Z support then |
| 4511 | report an error, otherwise, mark it disabled and go on. */ |
| 4512 | |
| 4513 | if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE) |
| 4514 | { |
| 4515 | char *buf = alloca (rs->remote_packet_size); |
| 4516 | char *p = buf; |
| 4517 | |
| 4518 | addr = remote_address_masked (addr); |
| 4519 | *(p++) = 'Z'; |
| 4520 | *(p++) = '0'; |
| 4521 | *(p++) = ','; |
| 4522 | p += hexnumstr (p, (ULONGEST) addr); |
| 4523 | BREAKPOINT_FROM_PC (&addr, &bp_size); |
| 4524 | sprintf (p, ",%d", bp_size); |
| 4525 | |
| 4526 | putpkt (buf); |
| 4527 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4528 | |
| 4529 | switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP])) |
| 4530 | { |
| 4531 | case PACKET_ERROR: |
| 4532 | return -1; |
| 4533 | case PACKET_OK: |
| 4534 | return 0; |
| 4535 | case PACKET_UNKNOWN: |
| 4536 | break; |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | #ifdef DEPRECATED_REMOTE_BREAKPOINT |
| 4541 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); |
| 4542 | |
| 4543 | if (val == 0) |
| 4544 | { |
| 4545 | if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) |
| 4546 | val = target_write_memory (addr, (char *) big_break_insn, |
| 4547 | sizeof big_break_insn); |
| 4548 | else |
| 4549 | val = target_write_memory (addr, (char *) little_break_insn, |
| 4550 | sizeof little_break_insn); |
| 4551 | } |
| 4552 | |
| 4553 | return val; |
| 4554 | #else |
| 4555 | return memory_insert_breakpoint (addr, contents_cache); |
| 4556 | #endif /* DEPRECATED_REMOTE_BREAKPOINT */ |
| 4557 | } |
| 4558 | |
| 4559 | static int |
| 4560 | remote_remove_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache) |
| 4561 | { |
| 4562 | struct remote_state *rs = get_remote_state (); |
| 4563 | int bp_size; |
| 4564 | |
| 4565 | if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE) |
| 4566 | { |
| 4567 | char *buf = alloca (rs->remote_packet_size); |
| 4568 | char *p = buf; |
| 4569 | |
| 4570 | *(p++) = 'z'; |
| 4571 | *(p++) = '0'; |
| 4572 | *(p++) = ','; |
| 4573 | |
| 4574 | addr = remote_address_masked (addr); |
| 4575 | p += hexnumstr (p, (ULONGEST) addr); |
| 4576 | BREAKPOINT_FROM_PC (&addr, &bp_size); |
| 4577 | sprintf (p, ",%d", bp_size); |
| 4578 | |
| 4579 | putpkt (buf); |
| 4580 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4581 | |
| 4582 | return (buf[0] == 'E'); |
| 4583 | } |
| 4584 | |
| 4585 | #ifdef DEPRECATED_REMOTE_BREAKPOINT |
| 4586 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); |
| 4587 | #else |
| 4588 | return memory_remove_breakpoint (addr, contents_cache); |
| 4589 | #endif /* DEPRECATED_REMOTE_BREAKPOINT */ |
| 4590 | } |
| 4591 | |
| 4592 | static int |
| 4593 | watchpoint_to_Z_packet (int type) |
| 4594 | { |
| 4595 | switch (type) |
| 4596 | { |
| 4597 | case hw_write: |
| 4598 | return 2; |
| 4599 | break; |
| 4600 | case hw_read: |
| 4601 | return 3; |
| 4602 | break; |
| 4603 | case hw_access: |
| 4604 | return 4; |
| 4605 | break; |
| 4606 | default: |
| 4607 | internal_error (__FILE__, __LINE__, |
| 4608 | _("hw_bp_to_z: bad watchpoint type %d"), type); |
| 4609 | } |
| 4610 | } |
| 4611 | |
| 4612 | static int |
| 4613 | remote_insert_watchpoint (CORE_ADDR addr, int len, int type) |
| 4614 | { |
| 4615 | struct remote_state *rs = get_remote_state (); |
| 4616 | char *buf = alloca (rs->remote_packet_size); |
| 4617 | char *p; |
| 4618 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
| 4619 | |
| 4620 | if (remote_protocol_Z[packet].support == PACKET_DISABLE) |
| 4621 | error (_("Can't set hardware watchpoints without the '%s' (%s) packet."), |
| 4622 | remote_protocol_Z[packet].name, |
| 4623 | remote_protocol_Z[packet].title); |
| 4624 | |
| 4625 | sprintf (buf, "Z%x,", packet); |
| 4626 | p = strchr (buf, '\0'); |
| 4627 | addr = remote_address_masked (addr); |
| 4628 | p += hexnumstr (p, (ULONGEST) addr); |
| 4629 | sprintf (p, ",%x", len); |
| 4630 | |
| 4631 | putpkt (buf); |
| 4632 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4633 | |
| 4634 | switch (packet_ok (buf, &remote_protocol_Z[packet])) |
| 4635 | { |
| 4636 | case PACKET_ERROR: |
| 4637 | case PACKET_UNKNOWN: |
| 4638 | return -1; |
| 4639 | case PACKET_OK: |
| 4640 | return 0; |
| 4641 | } |
| 4642 | internal_error (__FILE__, __LINE__, |
| 4643 | _("remote_insert_watchpoint: reached end of function")); |
| 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | static int |
| 4648 | remote_remove_watchpoint (CORE_ADDR addr, int len, int type) |
| 4649 | { |
| 4650 | struct remote_state *rs = get_remote_state (); |
| 4651 | char *buf = alloca (rs->remote_packet_size); |
| 4652 | char *p; |
| 4653 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
| 4654 | |
| 4655 | if (remote_protocol_Z[packet].support == PACKET_DISABLE) |
| 4656 | error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."), |
| 4657 | remote_protocol_Z[packet].name, |
| 4658 | remote_protocol_Z[packet].title); |
| 4659 | |
| 4660 | sprintf (buf, "z%x,", packet); |
| 4661 | p = strchr (buf, '\0'); |
| 4662 | addr = remote_address_masked (addr); |
| 4663 | p += hexnumstr (p, (ULONGEST) addr); |
| 4664 | sprintf (p, ",%x", len); |
| 4665 | putpkt (buf); |
| 4666 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4667 | |
| 4668 | switch (packet_ok (buf, &remote_protocol_Z[packet])) |
| 4669 | { |
| 4670 | case PACKET_ERROR: |
| 4671 | case PACKET_UNKNOWN: |
| 4672 | return -1; |
| 4673 | case PACKET_OK: |
| 4674 | return 0; |
| 4675 | } |
| 4676 | internal_error (__FILE__, __LINE__, |
| 4677 | _("remote_remove_watchpoint: reached end of function")); |
| 4678 | } |
| 4679 | |
| 4680 | |
| 4681 | int remote_hw_watchpoint_limit = -1; |
| 4682 | int remote_hw_breakpoint_limit = -1; |
| 4683 | |
| 4684 | static int |
| 4685 | remote_check_watch_resources (int type, int cnt, int ot) |
| 4686 | { |
| 4687 | if (type == bp_hardware_breakpoint) |
| 4688 | { |
| 4689 | if (remote_hw_breakpoint_limit == 0) |
| 4690 | return 0; |
| 4691 | else if (remote_hw_breakpoint_limit < 0) |
| 4692 | return 1; |
| 4693 | else if (cnt <= remote_hw_breakpoint_limit) |
| 4694 | return 1; |
| 4695 | } |
| 4696 | else |
| 4697 | { |
| 4698 | if (remote_hw_watchpoint_limit == 0) |
| 4699 | return 0; |
| 4700 | else if (remote_hw_watchpoint_limit < 0) |
| 4701 | return 1; |
| 4702 | else if (ot) |
| 4703 | return -1; |
| 4704 | else if (cnt <= remote_hw_watchpoint_limit) |
| 4705 | return 1; |
| 4706 | } |
| 4707 | return -1; |
| 4708 | } |
| 4709 | |
| 4710 | static int |
| 4711 | remote_stopped_by_watchpoint (void) |
| 4712 | { |
| 4713 | return remote_stopped_by_watchpoint_p; |
| 4714 | } |
| 4715 | |
| 4716 | extern int stepped_after_stopped_by_watchpoint; |
| 4717 | |
| 4718 | static int |
| 4719 | remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p) |
| 4720 | { |
| 4721 | int rc = 0; |
| 4722 | if (remote_stopped_by_watchpoint () |
| 4723 | || stepped_after_stopped_by_watchpoint) |
| 4724 | { |
| 4725 | *addr_p = remote_watch_data_address; |
| 4726 | rc = 1; |
| 4727 | } |
| 4728 | |
| 4729 | return rc; |
| 4730 | } |
| 4731 | |
| 4732 | |
| 4733 | static int |
| 4734 | remote_insert_hw_breakpoint (CORE_ADDR addr, char *shadow) |
| 4735 | { |
| 4736 | int len = 0; |
| 4737 | struct remote_state *rs = get_remote_state (); |
| 4738 | char *buf = alloca (rs->remote_packet_size); |
| 4739 | char *p = buf; |
| 4740 | |
| 4741 | /* The length field should be set to the size of a breakpoint |
| 4742 | instruction. */ |
| 4743 | |
| 4744 | BREAKPOINT_FROM_PC (&addr, &len); |
| 4745 | |
| 4746 | if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE) |
| 4747 | error (_("Can't set hardware breakpoint without the '%s' (%s) packet."), |
| 4748 | remote_protocol_Z[Z_PACKET_HARDWARE_BP].name, |
| 4749 | remote_protocol_Z[Z_PACKET_HARDWARE_BP].title); |
| 4750 | |
| 4751 | *(p++) = 'Z'; |
| 4752 | *(p++) = '1'; |
| 4753 | *(p++) = ','; |
| 4754 | |
| 4755 | addr = remote_address_masked (addr); |
| 4756 | p += hexnumstr (p, (ULONGEST) addr); |
| 4757 | sprintf (p, ",%x", len); |
| 4758 | |
| 4759 | putpkt (buf); |
| 4760 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4761 | |
| 4762 | switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP])) |
| 4763 | { |
| 4764 | case PACKET_ERROR: |
| 4765 | case PACKET_UNKNOWN: |
| 4766 | return -1; |
| 4767 | case PACKET_OK: |
| 4768 | return 0; |
| 4769 | } |
| 4770 | internal_error (__FILE__, __LINE__, |
| 4771 | _("remote_insert_hw_breakpoint: reached end of function")); |
| 4772 | } |
| 4773 | |
| 4774 | |
| 4775 | static int |
| 4776 | remote_remove_hw_breakpoint (CORE_ADDR addr, char *shadow) |
| 4777 | { |
| 4778 | int len; |
| 4779 | struct remote_state *rs = get_remote_state (); |
| 4780 | char *buf = alloca (rs->remote_packet_size); |
| 4781 | char *p = buf; |
| 4782 | |
| 4783 | /* The length field should be set to the size of a breakpoint |
| 4784 | instruction. */ |
| 4785 | |
| 4786 | BREAKPOINT_FROM_PC (&addr, &len); |
| 4787 | |
| 4788 | if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE) |
| 4789 | error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."), |
| 4790 | remote_protocol_Z[Z_PACKET_HARDWARE_BP].name, |
| 4791 | remote_protocol_Z[Z_PACKET_HARDWARE_BP].title); |
| 4792 | |
| 4793 | *(p++) = 'z'; |
| 4794 | *(p++) = '1'; |
| 4795 | *(p++) = ','; |
| 4796 | |
| 4797 | addr = remote_address_masked (addr); |
| 4798 | p += hexnumstr (p, (ULONGEST) addr); |
| 4799 | sprintf (p, ",%x", len); |
| 4800 | |
| 4801 | putpkt(buf); |
| 4802 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4803 | |
| 4804 | switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP])) |
| 4805 | { |
| 4806 | case PACKET_ERROR: |
| 4807 | case PACKET_UNKNOWN: |
| 4808 | return -1; |
| 4809 | case PACKET_OK: |
| 4810 | return 0; |
| 4811 | } |
| 4812 | internal_error (__FILE__, __LINE__, |
| 4813 | _("remote_remove_hw_breakpoint: reached end of function")); |
| 4814 | } |
| 4815 | |
| 4816 | /* Some targets are only capable of doing downloads, and afterwards |
| 4817 | they switch to the remote serial protocol. This function provides |
| 4818 | a clean way to get from the download target to the remote target. |
| 4819 | It's basically just a wrapper so that we don't have to expose any |
| 4820 | of the internal workings of remote.c. |
| 4821 | |
| 4822 | Prior to calling this routine, you should shutdown the current |
| 4823 | target code, else you will get the "A program is being debugged |
| 4824 | already..." message. Usually a call to pop_target() suffices. */ |
| 4825 | |
| 4826 | void |
| 4827 | push_remote_target (char *name, int from_tty) |
| 4828 | { |
| 4829 | printf_filtered (_("Switching to remote protocol\n")); |
| 4830 | remote_open (name, from_tty); |
| 4831 | } |
| 4832 | |
| 4833 | /* Table used by the crc32 function to calcuate the checksum. */ |
| 4834 | |
| 4835 | static unsigned long crc32_table[256] = |
| 4836 | {0, 0}; |
| 4837 | |
| 4838 | static unsigned long |
| 4839 | crc32 (unsigned char *buf, int len, unsigned int crc) |
| 4840 | { |
| 4841 | if (!crc32_table[1]) |
| 4842 | { |
| 4843 | /* Initialize the CRC table and the decoding table. */ |
| 4844 | int i, j; |
| 4845 | unsigned int c; |
| 4846 | |
| 4847 | for (i = 0; i < 256; i++) |
| 4848 | { |
| 4849 | for (c = i << 24, j = 8; j > 0; --j) |
| 4850 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); |
| 4851 | crc32_table[i] = c; |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | while (len--) |
| 4856 | { |
| 4857 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; |
| 4858 | buf++; |
| 4859 | } |
| 4860 | return crc; |
| 4861 | } |
| 4862 | |
| 4863 | /* compare-sections command |
| 4864 | |
| 4865 | With no arguments, compares each loadable section in the exec bfd |
| 4866 | with the same memory range on the target, and reports mismatches. |
| 4867 | Useful for verifying the image on the target against the exec file. |
| 4868 | Depends on the target understanding the new "qCRC:" request. */ |
| 4869 | |
| 4870 | /* FIXME: cagney/1999-10-26: This command should be broken down into a |
| 4871 | target method (target verify memory) and generic version of the |
| 4872 | actual command. This will allow other high-level code (especially |
| 4873 | generic_load()) to make use of this target functionality. */ |
| 4874 | |
| 4875 | static void |
| 4876 | compare_sections_command (char *args, int from_tty) |
| 4877 | { |
| 4878 | struct remote_state *rs = get_remote_state (); |
| 4879 | asection *s; |
| 4880 | unsigned long host_crc, target_crc; |
| 4881 | extern bfd *exec_bfd; |
| 4882 | struct cleanup *old_chain; |
| 4883 | char *tmp; |
| 4884 | char *sectdata; |
| 4885 | const char *sectname; |
| 4886 | char *buf = alloca (rs->remote_packet_size); |
| 4887 | bfd_size_type size; |
| 4888 | bfd_vma lma; |
| 4889 | int matched = 0; |
| 4890 | int mismatched = 0; |
| 4891 | |
| 4892 | if (!exec_bfd) |
| 4893 | error (_("command cannot be used without an exec file")); |
| 4894 | if (!current_target.to_shortname || |
| 4895 | strcmp (current_target.to_shortname, "remote") != 0) |
| 4896 | error (_("command can only be used with remote target")); |
| 4897 | |
| 4898 | for (s = exec_bfd->sections; s; s = s->next) |
| 4899 | { |
| 4900 | if (!(s->flags & SEC_LOAD)) |
| 4901 | continue; /* skip non-loadable section */ |
| 4902 | |
| 4903 | size = bfd_get_section_size (s); |
| 4904 | if (size == 0) |
| 4905 | continue; /* skip zero-length section */ |
| 4906 | |
| 4907 | sectname = bfd_get_section_name (exec_bfd, s); |
| 4908 | if (args && strcmp (args, sectname) != 0) |
| 4909 | continue; /* not the section selected by user */ |
| 4910 | |
| 4911 | matched = 1; /* do this section */ |
| 4912 | lma = s->lma; |
| 4913 | /* FIXME: assumes lma can fit into long. */ |
| 4914 | sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size); |
| 4915 | putpkt (buf); |
| 4916 | |
| 4917 | /* Be clever; compute the host_crc before waiting for target |
| 4918 | reply. */ |
| 4919 | sectdata = xmalloc (size); |
| 4920 | old_chain = make_cleanup (xfree, sectdata); |
| 4921 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); |
| 4922 | host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); |
| 4923 | |
| 4924 | getpkt (buf, (rs->remote_packet_size), 0); |
| 4925 | if (buf[0] == 'E') |
| 4926 | error (_("target memory fault, section %s, range 0x%s -- 0x%s"), |
| 4927 | sectname, paddr (lma), paddr (lma + size)); |
| 4928 | if (buf[0] != 'C') |
| 4929 | error (_("remote target does not support this operation")); |
| 4930 | |
| 4931 | for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++) |
| 4932 | target_crc = target_crc * 16 + fromhex (*tmp); |
| 4933 | |
| 4934 | printf_filtered ("Section %s, range 0x%s -- 0x%s: ", |
| 4935 | sectname, paddr (lma), paddr (lma + size)); |
| 4936 | if (host_crc == target_crc) |
| 4937 | printf_filtered ("matched.\n"); |
| 4938 | else |
| 4939 | { |
| 4940 | printf_filtered ("MIS-MATCHED!\n"); |
| 4941 | mismatched++; |
| 4942 | } |
| 4943 | |
| 4944 | do_cleanups (old_chain); |
| 4945 | } |
| 4946 | if (mismatched > 0) |
| 4947 | warning (_("One or more sections of the remote executable does not match\n\ |
| 4948 | the loaded file\n")); |
| 4949 | if (args && !matched) |
| 4950 | printf_filtered (_("No loaded section named '%s'.\n"), args); |
| 4951 | } |
| 4952 | |
| 4953 | static LONGEST |
| 4954 | remote_xfer_partial (struct target_ops *ops, enum target_object object, |
| 4955 | const char *annex, void *readbuf, const void *writebuf, |
| 4956 | ULONGEST offset, LONGEST len) |
| 4957 | { |
| 4958 | struct remote_state *rs = get_remote_state (); |
| 4959 | int i; |
| 4960 | char *buf2 = alloca (rs->remote_packet_size); |
| 4961 | char *p2 = &buf2[0]; |
| 4962 | char query_type; |
| 4963 | |
| 4964 | /* Handle memory using remote_xfer_memory. */ |
| 4965 | if (object == TARGET_OBJECT_MEMORY) |
| 4966 | { |
| 4967 | int xfered; |
| 4968 | errno = 0; |
| 4969 | |
| 4970 | if (writebuf != NULL) |
| 4971 | { |
| 4972 | void *buffer = xmalloc (len); |
| 4973 | struct cleanup *cleanup = make_cleanup (xfree, buffer); |
| 4974 | memcpy (buffer, writebuf, len); |
| 4975 | xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops); |
| 4976 | do_cleanups (cleanup); |
| 4977 | } |
| 4978 | else |
| 4979 | xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops); |
| 4980 | |
| 4981 | if (xfered > 0) |
| 4982 | return xfered; |
| 4983 | else if (xfered == 0 && errno == 0) |
| 4984 | return 0; |
| 4985 | else |
| 4986 | return -1; |
| 4987 | } |
| 4988 | |
| 4989 | /* Only handle reads. */ |
| 4990 | if (writebuf != NULL || readbuf == NULL) |
| 4991 | return -1; |
| 4992 | |
| 4993 | /* Map pre-existing objects onto letters. DO NOT do this for new |
| 4994 | objects!!! Instead specify new query packets. */ |
| 4995 | switch (object) |
| 4996 | { |
| 4997 | case TARGET_OBJECT_KOD: |
| 4998 | query_type = 'K'; |
| 4999 | break; |
| 5000 | case TARGET_OBJECT_AVR: |
| 5001 | query_type = 'R'; |
| 5002 | break; |
| 5003 | |
| 5004 | case TARGET_OBJECT_AUXV: |
| 5005 | if (remote_protocol_qPart_auxv.support != PACKET_DISABLE) |
| 5006 | { |
| 5007 | unsigned int total = 0; |
| 5008 | while (len > 0) |
| 5009 | { |
| 5010 | LONGEST n = min ((rs->remote_packet_size - 2) / 2, len); |
| 5011 | snprintf (buf2, rs->remote_packet_size, |
| 5012 | "qPart:auxv:read::%s,%s", |
| 5013 | phex_nz (offset, sizeof offset), |
| 5014 | phex_nz (n, sizeof n)); |
| 5015 | i = putpkt (buf2); |
| 5016 | if (i < 0) |
| 5017 | return total > 0 ? total : i; |
| 5018 | buf2[0] = '\0'; |
| 5019 | getpkt (buf2, rs->remote_packet_size, 0); |
| 5020 | if (packet_ok (buf2, &remote_protocol_qPart_auxv) != PACKET_OK) |
| 5021 | return total > 0 ? total : -1; |
| 5022 | if (buf2[0] == 'O' && buf2[1] == 'K' && buf2[2] == '\0') |
| 5023 | break; /* Got EOF indicator. */ |
| 5024 | /* Got some data. */ |
| 5025 | i = hex2bin (buf2, readbuf, len); |
| 5026 | if (i > 0) |
| 5027 | { |
| 5028 | readbuf = (void *) ((char *) readbuf + i); |
| 5029 | offset += i; |
| 5030 | len -= i; |
| 5031 | total += i; |
| 5032 | } |
| 5033 | } |
| 5034 | return total; |
| 5035 | } |
| 5036 | return -1; |
| 5037 | |
| 5038 | default: |
| 5039 | return -1; |
| 5040 | } |
| 5041 | |
| 5042 | /* Note: a zero OFFSET and LEN can be used to query the minimum |
| 5043 | buffer size. */ |
| 5044 | if (offset == 0 && len == 0) |
| 5045 | return (rs->remote_packet_size); |
| 5046 | /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is |
| 5047 | not large enough let the caller. */ |
| 5048 | if (len < (rs->remote_packet_size)) |
| 5049 | return -1; |
| 5050 | len = rs->remote_packet_size; |
| 5051 | |
| 5052 | /* Except for querying the minimum buffer size, target must be open. */ |
| 5053 | if (!remote_desc) |
| 5054 | error (_("remote query is only available after target open")); |
| 5055 | |
| 5056 | gdb_assert (annex != NULL); |
| 5057 | gdb_assert (readbuf != NULL); |
| 5058 | |
| 5059 | *p2++ = 'q'; |
| 5060 | *p2++ = query_type; |
| 5061 | |
| 5062 | /* We used one buffer char for the remote protocol q command and |
| 5063 | another for the query type. As the remote protocol encapsulation |
| 5064 | uses 4 chars plus one extra in case we are debugging |
| 5065 | (remote_debug), we have PBUFZIZ - 7 left to pack the query |
| 5066 | string. */ |
| 5067 | i = 0; |
| 5068 | while (annex[i] && (i < ((rs->remote_packet_size) - 8))) |
| 5069 | { |
| 5070 | /* Bad caller may have sent forbidden characters. */ |
| 5071 | gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#'); |
| 5072 | *p2++ = annex[i]; |
| 5073 | i++; |
| 5074 | } |
| 5075 | *p2 = '\0'; |
| 5076 | gdb_assert (annex[i] == '\0'); |
| 5077 | |
| 5078 | i = putpkt (buf2); |
| 5079 | if (i < 0) |
| 5080 | return i; |
| 5081 | |
| 5082 | getpkt (readbuf, len, 0); |
| 5083 | |
| 5084 | return strlen (readbuf); |
| 5085 | } |
| 5086 | |
| 5087 | static void |
| 5088 | remote_rcmd (char *command, |
| 5089 | struct ui_file *outbuf) |
| 5090 | { |
| 5091 | struct remote_state *rs = get_remote_state (); |
| 5092 | char *buf = alloca (rs->remote_packet_size); |
| 5093 | char *p = buf; |
| 5094 | |
| 5095 | if (!remote_desc) |
| 5096 | error (_("remote rcmd is only available after target open")); |
| 5097 | |
| 5098 | /* Send a NULL command across as an empty command. */ |
| 5099 | if (command == NULL) |
| 5100 | command = ""; |
| 5101 | |
| 5102 | /* The query prefix. */ |
| 5103 | strcpy (buf, "qRcmd,"); |
| 5104 | p = strchr (buf, '\0'); |
| 5105 | |
| 5106 | if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > (rs->remote_packet_size)) |
| 5107 | error (_("\"monitor\" command ``%s'' is too long."), command); |
| 5108 | |
| 5109 | /* Encode the actual command. */ |
| 5110 | bin2hex (command, p, 0); |
| 5111 | |
| 5112 | if (putpkt (buf) < 0) |
| 5113 | error (_("Communication problem with target.")); |
| 5114 | |
| 5115 | /* get/display the response */ |
| 5116 | while (1) |
| 5117 | { |
| 5118 | /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */ |
| 5119 | buf[0] = '\0'; |
| 5120 | getpkt (buf, (rs->remote_packet_size), 0); |
| 5121 | if (buf[0] == '\0') |
| 5122 | error (_("Target does not support this command.")); |
| 5123 | if (buf[0] == 'O' && buf[1] != 'K') |
| 5124 | { |
| 5125 | remote_console_output (buf + 1); /* 'O' message from stub. */ |
| 5126 | continue; |
| 5127 | } |
| 5128 | if (strcmp (buf, "OK") == 0) |
| 5129 | break; |
| 5130 | if (strlen (buf) == 3 && buf[0] == 'E' |
| 5131 | && isdigit (buf[1]) && isdigit (buf[2])) |
| 5132 | { |
| 5133 | error (_("Protocol error with Rcmd")); |
| 5134 | } |
| 5135 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) |
| 5136 | { |
| 5137 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); |
| 5138 | fputc_unfiltered (c, outbuf); |
| 5139 | } |
| 5140 | break; |
| 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | static void |
| 5145 | packet_command (char *args, int from_tty) |
| 5146 | { |
| 5147 | struct remote_state *rs = get_remote_state (); |
| 5148 | char *buf = alloca (rs->remote_packet_size); |
| 5149 | |
| 5150 | if (!remote_desc) |
| 5151 | error (_("command can only be used with remote target")); |
| 5152 | |
| 5153 | if (!args) |
| 5154 | error (_("remote-packet command requires packet text as argument")); |
| 5155 | |
| 5156 | puts_filtered ("sending: "); |
| 5157 | print_packet (args); |
| 5158 | puts_filtered ("\n"); |
| 5159 | putpkt (args); |
| 5160 | |
| 5161 | getpkt (buf, (rs->remote_packet_size), 0); |
| 5162 | puts_filtered ("received: "); |
| 5163 | print_packet (buf); |
| 5164 | puts_filtered ("\n"); |
| 5165 | } |
| 5166 | |
| 5167 | #if 0 |
| 5168 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */ |
| 5169 | |
| 5170 | static void display_thread_info (struct gdb_ext_thread_info *info); |
| 5171 | |
| 5172 | static void threadset_test_cmd (char *cmd, int tty); |
| 5173 | |
| 5174 | static void threadalive_test (char *cmd, int tty); |
| 5175 | |
| 5176 | static void threadlist_test_cmd (char *cmd, int tty); |
| 5177 | |
| 5178 | int get_and_display_threadinfo (threadref *ref); |
| 5179 | |
| 5180 | static void threadinfo_test_cmd (char *cmd, int tty); |
| 5181 | |
| 5182 | static int thread_display_step (threadref *ref, void *context); |
| 5183 | |
| 5184 | static void threadlist_update_test_cmd (char *cmd, int tty); |
| 5185 | |
| 5186 | static void init_remote_threadtests (void); |
| 5187 | |
| 5188 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */ |
| 5189 | |
| 5190 | static void |
| 5191 | threadset_test_cmd (char *cmd, int tty) |
| 5192 | { |
| 5193 | int sample_thread = SAMPLE_THREAD; |
| 5194 | |
| 5195 | printf_filtered (_("Remote threadset test\n")); |
| 5196 | set_thread (sample_thread, 1); |
| 5197 | } |
| 5198 | |
| 5199 | |
| 5200 | static void |
| 5201 | threadalive_test (char *cmd, int tty) |
| 5202 | { |
| 5203 | int sample_thread = SAMPLE_THREAD; |
| 5204 | |
| 5205 | if (remote_thread_alive (pid_to_ptid (sample_thread))) |
| 5206 | printf_filtered ("PASS: Thread alive test\n"); |
| 5207 | else |
| 5208 | printf_filtered ("FAIL: Thread alive test\n"); |
| 5209 | } |
| 5210 | |
| 5211 | void output_threadid (char *title, threadref *ref); |
| 5212 | |
| 5213 | void |
| 5214 | output_threadid (char *title, threadref *ref) |
| 5215 | { |
| 5216 | char hexid[20]; |
| 5217 | |
| 5218 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */ |
| 5219 | hexid[16] = 0; |
| 5220 | printf_filtered ("%s %s\n", title, (&hexid[0])); |
| 5221 | } |
| 5222 | |
| 5223 | static void |
| 5224 | threadlist_test_cmd (char *cmd, int tty) |
| 5225 | { |
| 5226 | int startflag = 1; |
| 5227 | threadref nextthread; |
| 5228 | int done, result_count; |
| 5229 | threadref threadlist[3]; |
| 5230 | |
| 5231 | printf_filtered ("Remote Threadlist test\n"); |
| 5232 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, |
| 5233 | &result_count, &threadlist[0])) |
| 5234 | printf_filtered ("FAIL: threadlist test\n"); |
| 5235 | else |
| 5236 | { |
| 5237 | threadref *scan = threadlist; |
| 5238 | threadref *limit = scan + result_count; |
| 5239 | |
| 5240 | while (scan < limit) |
| 5241 | output_threadid (" thread ", scan++); |
| 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | void |
| 5246 | display_thread_info (struct gdb_ext_thread_info *info) |
| 5247 | { |
| 5248 | output_threadid ("Threadid: ", &info->threadid); |
| 5249 | printf_filtered ("Name: %s\n ", info->shortname); |
| 5250 | printf_filtered ("State: %s\n", info->display); |
| 5251 | printf_filtered ("other: %s\n\n", info->more_display); |
| 5252 | } |
| 5253 | |
| 5254 | int |
| 5255 | get_and_display_threadinfo (threadref *ref) |
| 5256 | { |
| 5257 | int result; |
| 5258 | int set; |
| 5259 | struct gdb_ext_thread_info threadinfo; |
| 5260 | |
| 5261 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME |
| 5262 | | TAG_MOREDISPLAY | TAG_DISPLAY; |
| 5263 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) |
| 5264 | display_thread_info (&threadinfo); |
| 5265 | return result; |
| 5266 | } |
| 5267 | |
| 5268 | static void |
| 5269 | threadinfo_test_cmd (char *cmd, int tty) |
| 5270 | { |
| 5271 | int athread = SAMPLE_THREAD; |
| 5272 | threadref thread; |
| 5273 | int set; |
| 5274 | |
| 5275 | int_to_threadref (&thread, athread); |
| 5276 | printf_filtered ("Remote Threadinfo test\n"); |
| 5277 | if (!get_and_display_threadinfo (&thread)) |
| 5278 | printf_filtered ("FAIL cannot get thread info\n"); |
| 5279 | } |
| 5280 | |
| 5281 | static int |
| 5282 | thread_display_step (threadref *ref, void *context) |
| 5283 | { |
| 5284 | /* output_threadid(" threadstep ",ref); *//* simple test */ |
| 5285 | return get_and_display_threadinfo (ref); |
| 5286 | } |
| 5287 | |
| 5288 | static void |
| 5289 | threadlist_update_test_cmd (char *cmd, int tty) |
| 5290 | { |
| 5291 | printf_filtered ("Remote Threadlist update test\n"); |
| 5292 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); |
| 5293 | } |
| 5294 | |
| 5295 | static void |
| 5296 | init_remote_threadtests (void) |
| 5297 | { |
| 5298 | add_com ("tlist", class_obscure, threadlist_test_cmd, _("\ |
| 5299 | Fetch and print the remote list of thread identifiers, one pkt only")); |
| 5300 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, |
| 5301 | _("Fetch and display info about one thread")); |
| 5302 | add_com ("tset", class_obscure, threadset_test_cmd, |
| 5303 | _("Test setting to a different thread")); |
| 5304 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, |
| 5305 | _("Iterate through updating all remote thread info")); |
| 5306 | add_com ("talive", class_obscure, threadalive_test, |
| 5307 | _(" Remote thread alive test ")); |
| 5308 | } |
| 5309 | |
| 5310 | #endif /* 0 */ |
| 5311 | |
| 5312 | /* Convert a thread ID to a string. Returns the string in a static |
| 5313 | buffer. */ |
| 5314 | |
| 5315 | static char * |
| 5316 | remote_pid_to_str (ptid_t ptid) |
| 5317 | { |
| 5318 | static char buf[32]; |
| 5319 | int size; |
| 5320 | |
| 5321 | size = snprintf (buf, sizeof buf, "thread %d", ptid_get_pid (ptid)); |
| 5322 | gdb_assert (size < sizeof buf); |
| 5323 | return buf; |
| 5324 | } |
| 5325 | |
| 5326 | /* Get the address of the thread local variable in OBJFILE which is |
| 5327 | stored at OFFSET within the thread local storage for thread PTID. */ |
| 5328 | |
| 5329 | static CORE_ADDR |
| 5330 | remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset) |
| 5331 | { |
| 5332 | if (remote_protocol_qGetTLSAddr.support != PACKET_DISABLE) |
| 5333 | { |
| 5334 | struct remote_state *rs = get_remote_state (); |
| 5335 | char *buf = alloca (rs->remote_packet_size); |
| 5336 | char *p = buf; |
| 5337 | enum packet_result result; |
| 5338 | |
| 5339 | strcpy (p, "qGetTLSAddr:"); |
| 5340 | p += strlen (p); |
| 5341 | p += hexnumstr (p, PIDGET (ptid)); |
| 5342 | *p++ = ','; |
| 5343 | p += hexnumstr (p, offset); |
| 5344 | *p++ = ','; |
| 5345 | p += hexnumstr (p, lm); |
| 5346 | *p++ = '\0'; |
| 5347 | |
| 5348 | putpkt (buf); |
| 5349 | getpkt (buf, rs->remote_packet_size, 0); |
| 5350 | result = packet_ok (buf, &remote_protocol_qGetTLSAddr); |
| 5351 | if (result == PACKET_OK) |
| 5352 | { |
| 5353 | ULONGEST result; |
| 5354 | |
| 5355 | unpack_varlen_hex (buf, &result); |
| 5356 | return result; |
| 5357 | } |
| 5358 | else if (result == PACKET_UNKNOWN) |
| 5359 | throw_error (TLS_GENERIC_ERROR, |
| 5360 | _("Remote target doesn't support qGetTLSAddr packet")); |
| 5361 | else |
| 5362 | throw_error (TLS_GENERIC_ERROR, |
| 5363 | _("Remote target failed to process qGetTLSAddr request")); |
| 5364 | } |
| 5365 | else |
| 5366 | throw_error (TLS_GENERIC_ERROR, |
| 5367 | _("TLS not supported or disabled on this target")); |
| 5368 | /* Not reached. */ |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
| 5372 | static void |
| 5373 | init_remote_ops (void) |
| 5374 | { |
| 5375 | remote_ops.to_shortname = "remote"; |
| 5376 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
| 5377 | remote_ops.to_doc = |
| 5378 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
| 5379 | Specify the serial device it is connected to\n\ |
| 5380 | (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."; |
| 5381 | remote_ops.to_open = remote_open; |
| 5382 | remote_ops.to_close = remote_close; |
| 5383 | remote_ops.to_detach = remote_detach; |
| 5384 | remote_ops.to_disconnect = remote_disconnect; |
| 5385 | remote_ops.to_resume = remote_resume; |
| 5386 | remote_ops.to_wait = remote_wait; |
| 5387 | remote_ops.to_fetch_registers = remote_fetch_registers; |
| 5388 | remote_ops.to_store_registers = remote_store_registers; |
| 5389 | remote_ops.to_prepare_to_store = remote_prepare_to_store; |
| 5390 | remote_ops.deprecated_xfer_memory = remote_xfer_memory; |
| 5391 | remote_ops.to_files_info = remote_files_info; |
| 5392 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
| 5393 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; |
| 5394 | remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
| 5395 | remote_ops.to_stopped_data_address = remote_stopped_data_address; |
| 5396 | remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; |
| 5397 | remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; |
| 5398 | remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; |
| 5399 | remote_ops.to_insert_watchpoint = remote_insert_watchpoint; |
| 5400 | remote_ops.to_remove_watchpoint = remote_remove_watchpoint; |
| 5401 | remote_ops.to_kill = remote_kill; |
| 5402 | remote_ops.to_load = generic_load; |
| 5403 | remote_ops.to_mourn_inferior = remote_mourn; |
| 5404 | remote_ops.to_thread_alive = remote_thread_alive; |
| 5405 | remote_ops.to_find_new_threads = remote_threads_info; |
| 5406 | remote_ops.to_pid_to_str = remote_pid_to_str; |
| 5407 | remote_ops.to_extra_thread_info = remote_threads_extra_info; |
| 5408 | remote_ops.to_stop = remote_stop; |
| 5409 | remote_ops.to_xfer_partial = remote_xfer_partial; |
| 5410 | remote_ops.to_rcmd = remote_rcmd; |
| 5411 | remote_ops.to_get_thread_local_address = remote_get_thread_local_address; |
| 5412 | remote_ops.to_stratum = process_stratum; |
| 5413 | remote_ops.to_has_all_memory = 1; |
| 5414 | remote_ops.to_has_memory = 1; |
| 5415 | remote_ops.to_has_stack = 1; |
| 5416 | remote_ops.to_has_registers = 1; |
| 5417 | remote_ops.to_has_execution = 1; |
| 5418 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ |
| 5419 | remote_ops.to_magic = OPS_MAGIC; |
| 5420 | } |
| 5421 | |
| 5422 | /* Set up the extended remote vector by making a copy of the standard |
| 5423 | remote vector and adding to it. */ |
| 5424 | |
| 5425 | static void |
| 5426 | init_extended_remote_ops (void) |
| 5427 | { |
| 5428 | extended_remote_ops = remote_ops; |
| 5429 | |
| 5430 | extended_remote_ops.to_shortname = "extended-remote"; |
| 5431 | extended_remote_ops.to_longname = |
| 5432 | "Extended remote serial target in gdb-specific protocol"; |
| 5433 | extended_remote_ops.to_doc = |
| 5434 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
| 5435 | Specify the serial device it is connected to (e.g. /dev/ttya).", |
| 5436 | extended_remote_ops.to_open = extended_remote_open; |
| 5437 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
| 5438 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; |
| 5439 | } |
| 5440 | |
| 5441 | static int |
| 5442 | remote_can_async_p (void) |
| 5443 | { |
| 5444 | /* We're async whenever the serial device is. */ |
| 5445 | return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc); |
| 5446 | } |
| 5447 | |
| 5448 | static int |
| 5449 | remote_is_async_p (void) |
| 5450 | { |
| 5451 | /* We're async whenever the serial device is. */ |
| 5452 | return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc); |
| 5453 | } |
| 5454 | |
| 5455 | /* Pass the SERIAL event on and up to the client. One day this code |
| 5456 | will be able to delay notifying the client of an event until the |
| 5457 | point where an entire packet has been received. */ |
| 5458 | |
| 5459 | static void (*async_client_callback) (enum inferior_event_type event_type, |
| 5460 | void *context); |
| 5461 | static void *async_client_context; |
| 5462 | static serial_event_ftype remote_async_serial_handler; |
| 5463 | |
| 5464 | static void |
| 5465 | remote_async_serial_handler (struct serial *scb, void *context) |
| 5466 | { |
| 5467 | /* Don't propogate error information up to the client. Instead let |
| 5468 | the client find out about the error by querying the target. */ |
| 5469 | async_client_callback (INF_REG_EVENT, async_client_context); |
| 5470 | } |
| 5471 | |
| 5472 | static void |
| 5473 | remote_async (void (*callback) (enum inferior_event_type event_type, |
| 5474 | void *context), void *context) |
| 5475 | { |
| 5476 | if (current_target.to_async_mask_value == 0) |
| 5477 | internal_error (__FILE__, __LINE__, |
| 5478 | _("Calling remote_async when async is masked")); |
| 5479 | |
| 5480 | if (callback != NULL) |
| 5481 | { |
| 5482 | serial_async (remote_desc, remote_async_serial_handler, NULL); |
| 5483 | async_client_callback = callback; |
| 5484 | async_client_context = context; |
| 5485 | } |
| 5486 | else |
| 5487 | serial_async (remote_desc, NULL, NULL); |
| 5488 | } |
| 5489 | |
| 5490 | /* Target async and target extended-async. |
| 5491 | |
| 5492 | This are temporary targets, until it is all tested. Eventually |
| 5493 | async support will be incorporated int the usual 'remote' |
| 5494 | target. */ |
| 5495 | |
| 5496 | static void |
| 5497 | init_remote_async_ops (void) |
| 5498 | { |
| 5499 | remote_async_ops.to_shortname = "async"; |
| 5500 | remote_async_ops.to_longname = |
| 5501 | "Remote serial target in async version of the gdb-specific protocol"; |
| 5502 | remote_async_ops.to_doc = |
| 5503 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
| 5504 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
| 5505 | remote_async_ops.to_open = remote_async_open; |
| 5506 | remote_async_ops.to_close = remote_close; |
| 5507 | remote_async_ops.to_detach = remote_detach; |
| 5508 | remote_async_ops.to_disconnect = remote_disconnect; |
| 5509 | remote_async_ops.to_resume = remote_async_resume; |
| 5510 | remote_async_ops.to_wait = remote_async_wait; |
| 5511 | remote_async_ops.to_fetch_registers = remote_fetch_registers; |
| 5512 | remote_async_ops.to_store_registers = remote_store_registers; |
| 5513 | remote_async_ops.to_prepare_to_store = remote_prepare_to_store; |
| 5514 | remote_async_ops.deprecated_xfer_memory = remote_xfer_memory; |
| 5515 | remote_async_ops.to_files_info = remote_files_info; |
| 5516 | remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint; |
| 5517 | remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint; |
| 5518 | remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; |
| 5519 | remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; |
| 5520 | remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; |
| 5521 | remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint; |
| 5522 | remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint; |
| 5523 | remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
| 5524 | remote_async_ops.to_stopped_data_address = remote_stopped_data_address; |
| 5525 | remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior; |
| 5526 | remote_async_ops.to_terminal_ours = remote_async_terminal_ours; |
| 5527 | remote_async_ops.to_kill = remote_async_kill; |
| 5528 | remote_async_ops.to_load = generic_load; |
| 5529 | remote_async_ops.to_mourn_inferior = remote_async_mourn; |
| 5530 | remote_async_ops.to_thread_alive = remote_thread_alive; |
| 5531 | remote_async_ops.to_find_new_threads = remote_threads_info; |
| 5532 | remote_async_ops.to_pid_to_str = remote_pid_to_str; |
| 5533 | remote_async_ops.to_extra_thread_info = remote_threads_extra_info; |
| 5534 | remote_async_ops.to_stop = remote_stop; |
| 5535 | remote_async_ops.to_xfer_partial = remote_xfer_partial; |
| 5536 | remote_async_ops.to_rcmd = remote_rcmd; |
| 5537 | remote_async_ops.to_stratum = process_stratum; |
| 5538 | remote_async_ops.to_has_all_memory = 1; |
| 5539 | remote_async_ops.to_has_memory = 1; |
| 5540 | remote_async_ops.to_has_stack = 1; |
| 5541 | remote_async_ops.to_has_registers = 1; |
| 5542 | remote_async_ops.to_has_execution = 1; |
| 5543 | remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ |
| 5544 | remote_async_ops.to_can_async_p = remote_can_async_p; |
| 5545 | remote_async_ops.to_is_async_p = remote_is_async_p; |
| 5546 | remote_async_ops.to_async = remote_async; |
| 5547 | remote_async_ops.to_async_mask_value = 1; |
| 5548 | remote_async_ops.to_magic = OPS_MAGIC; |
| 5549 | } |
| 5550 | |
| 5551 | /* Set up the async extended remote vector by making a copy of the standard |
| 5552 | remote vector and adding to it. */ |
| 5553 | |
| 5554 | static void |
| 5555 | init_extended_async_remote_ops (void) |
| 5556 | { |
| 5557 | extended_async_remote_ops = remote_async_ops; |
| 5558 | |
| 5559 | extended_async_remote_ops.to_shortname = "extended-async"; |
| 5560 | extended_async_remote_ops.to_longname = |
| 5561 | "Extended remote serial target in async gdb-specific protocol"; |
| 5562 | extended_async_remote_ops.to_doc = |
| 5563 | "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\ |
| 5564 | Specify the serial device it is connected to (e.g. /dev/ttya).", |
| 5565 | extended_async_remote_ops.to_open = extended_remote_async_open; |
| 5566 | extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior; |
| 5567 | extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn; |
| 5568 | } |
| 5569 | |
| 5570 | static void |
| 5571 | set_remote_cmd (char *args, int from_tty) |
| 5572 | { |
| 5573 | } |
| 5574 | |
| 5575 | static void |
| 5576 | show_remote_cmd (char *args, int from_tty) |
| 5577 | { |
| 5578 | /* FIXME: cagney/2002-06-15: This function should iterate over |
| 5579 | remote_show_cmdlist for a list of sub commands to show. */ |
| 5580 | show_remote_protocol_Z_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5581 | show_remote_protocol_P_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5582 | show_remote_protocol_p_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5583 | show_remote_protocol_qSymbol_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5584 | show_remote_protocol_vcont_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5585 | show_remote_protocol_binary_download_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5586 | show_remote_protocol_qPart_auxv_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5587 | show_remote_protocol_qGetTLSAddr_packet_cmd (gdb_stdout, from_tty, NULL, NULL); |
| 5588 | } |
| 5589 | |
| 5590 | static void |
| 5591 | build_remote_gdbarch_data (void) |
| 5592 | { |
| 5593 | remote_address_size = TARGET_ADDR_BIT; |
| 5594 | } |
| 5595 | |
| 5596 | /* Saved pointer to previous owner of the new_objfile event. */ |
| 5597 | static void (*remote_new_objfile_chain) (struct objfile *); |
| 5598 | |
| 5599 | /* Function to be called whenever a new objfile (shlib) is detected. */ |
| 5600 | static void |
| 5601 | remote_new_objfile (struct objfile *objfile) |
| 5602 | { |
| 5603 | if (remote_desc != 0) /* Have a remote connection. */ |
| 5604 | { |
| 5605 | remote_check_symbols (objfile); |
| 5606 | } |
| 5607 | /* Call predecessor on chain, if any. */ |
| 5608 | if (remote_new_objfile_chain != 0 && |
| 5609 | remote_desc == 0) |
| 5610 | remote_new_objfile_chain (objfile); |
| 5611 | } |
| 5612 | |
| 5613 | void |
| 5614 | _initialize_remote (void) |
| 5615 | { |
| 5616 | static struct cmd_list_element *remote_set_cmdlist; |
| 5617 | static struct cmd_list_element *remote_show_cmdlist; |
| 5618 | |
| 5619 | /* architecture specific data */ |
| 5620 | remote_gdbarch_data_handle = |
| 5621 | gdbarch_data_register_post_init (init_remote_state); |
| 5622 | |
| 5623 | /* Old tacky stuff. NOTE: This comes after the remote protocol so |
| 5624 | that the remote protocol has been initialized. */ |
| 5625 | DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size); |
| 5626 | deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data); |
| 5627 | |
| 5628 | init_remote_ops (); |
| 5629 | add_target (&remote_ops); |
| 5630 | |
| 5631 | init_extended_remote_ops (); |
| 5632 | add_target (&extended_remote_ops); |
| 5633 | |
| 5634 | init_remote_async_ops (); |
| 5635 | add_target (&remote_async_ops); |
| 5636 | |
| 5637 | init_extended_async_remote_ops (); |
| 5638 | add_target (&extended_async_remote_ops); |
| 5639 | |
| 5640 | /* Hook into new objfile notification. */ |
| 5641 | remote_new_objfile_chain = deprecated_target_new_objfile_hook; |
| 5642 | deprecated_target_new_objfile_hook = remote_new_objfile; |
| 5643 | |
| 5644 | #if 0 |
| 5645 | init_remote_threadtests (); |
| 5646 | #endif |
| 5647 | |
| 5648 | /* set/show remote ... */ |
| 5649 | |
| 5650 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\ |
| 5651 | Remote protocol specific variables\n\ |
| 5652 | Configure various remote-protocol specific variables such as\n\ |
| 5653 | the packets being used"), |
| 5654 | &remote_set_cmdlist, "set remote ", |
| 5655 | 0 /* allow-unknown */, &setlist); |
| 5656 | add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\ |
| 5657 | Remote protocol specific variables\n\ |
| 5658 | Configure various remote-protocol specific variables such as\n\ |
| 5659 | the packets being used"), |
| 5660 | &remote_show_cmdlist, "show remote ", |
| 5661 | 0 /* allow-unknown */, &showlist); |
| 5662 | |
| 5663 | add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\ |
| 5664 | Compare section data on target to the exec file.\n\ |
| 5665 | Argument is a single section name (default: all loaded sections)."), |
| 5666 | &cmdlist); |
| 5667 | |
| 5668 | add_cmd ("packet", class_maintenance, packet_command, _("\ |
| 5669 | Send an arbitrary packet to a remote target.\n\ |
| 5670 | maintenance packet TEXT\n\ |
| 5671 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ |
| 5672 | this command sends the string TEXT to the inferior, and displays the\n\ |
| 5673 | response packet. GDB supplies the initial `$' character, and the\n\ |
| 5674 | terminating `#' character and checksum."), |
| 5675 | &maintenancelist); |
| 5676 | |
| 5677 | add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\ |
| 5678 | Set whether to send break if interrupted."), _("\ |
| 5679 | Show whether to send break if interrupted."), _("\ |
| 5680 | If set, a break, instead of a cntrl-c, is sent to the remote target."), |
| 5681 | NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */ |
| 5682 | &setlist, &showlist); |
| 5683 | |
| 5684 | /* Install commands for configuring memory read/write packets. */ |
| 5685 | |
| 5686 | add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\ |
| 5687 | Set the maximum number of bytes per memory write packet (deprecated)."), |
| 5688 | &setlist); |
| 5689 | add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\ |
| 5690 | Show the maximum number of bytes per memory write packet (deprecated)."), |
| 5691 | &showlist); |
| 5692 | add_cmd ("memory-write-packet-size", no_class, |
| 5693 | set_memory_write_packet_size, _("\ |
| 5694 | Set the maximum number of bytes per memory-write packet.\n\ |
| 5695 | Specify the number of bytes in a packet or 0 (zero) for the\n\ |
| 5696 | default packet size. The actual limit is further reduced\n\ |
| 5697 | dependent on the target. Specify ``fixed'' to disable the\n\ |
| 5698 | further restriction and ``limit'' to enable that restriction."), |
| 5699 | &remote_set_cmdlist); |
| 5700 | add_cmd ("memory-read-packet-size", no_class, |
| 5701 | set_memory_read_packet_size, _("\ |
| 5702 | Set the maximum number of bytes per memory-read packet.\n\ |
| 5703 | Specify the number of bytes in a packet or 0 (zero) for the\n\ |
| 5704 | default packet size. The actual limit is further reduced\n\ |
| 5705 | dependent on the target. Specify ``fixed'' to disable the\n\ |
| 5706 | further restriction and ``limit'' to enable that restriction."), |
| 5707 | &remote_set_cmdlist); |
| 5708 | add_cmd ("memory-write-packet-size", no_class, |
| 5709 | show_memory_write_packet_size, |
| 5710 | _("Show the maximum number of bytes per memory-write packet."), |
| 5711 | &remote_show_cmdlist); |
| 5712 | add_cmd ("memory-read-packet-size", no_class, |
| 5713 | show_memory_read_packet_size, |
| 5714 | _("Show the maximum number of bytes per memory-read packet."), |
| 5715 | &remote_show_cmdlist); |
| 5716 | |
| 5717 | add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class, |
| 5718 | &remote_hw_watchpoint_limit, _("\ |
| 5719 | Set the maximum number of target hardware watchpoints."), _("\ |
| 5720 | Show the maximum number of target hardware watchpoints."), _("\ |
| 5721 | Specify a negative limit for unlimited."), |
| 5722 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */ |
| 5723 | &remote_set_cmdlist, &remote_show_cmdlist); |
| 5724 | add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class, |
| 5725 | &remote_hw_breakpoint_limit, _("\ |
| 5726 | Set the maximum number of target hardware breakpoints."), _("\ |
| 5727 | Show the maximum number of target hardware breakpoints."), _("\ |
| 5728 | Specify a negative limit for unlimited."), |
| 5729 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */ |
| 5730 | &remote_set_cmdlist, &remote_show_cmdlist); |
| 5731 | |
| 5732 | add_setshow_integer_cmd ("remoteaddresssize", class_obscure, |
| 5733 | &remote_address_size, _("\ |
| 5734 | Set the maximum size of the address (in bits) in a memory packet."), _("\ |
| 5735 | Show the maximum size of the address (in bits) in a memory packet."), NULL, |
| 5736 | NULL, |
| 5737 | NULL, /* FIXME: i18n: */ |
| 5738 | &setlist, &showlist); |
| 5739 | |
| 5740 | add_packet_config_cmd (&remote_protocol_binary_download, |
| 5741 | "X", "binary-download", |
| 5742 | set_remote_protocol_binary_download_cmd, |
| 5743 | show_remote_protocol_binary_download_cmd, |
| 5744 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5745 | 1); |
| 5746 | |
| 5747 | add_packet_config_cmd (&remote_protocol_vcont, |
| 5748 | "vCont", "verbose-resume", |
| 5749 | set_remote_protocol_vcont_packet_cmd, |
| 5750 | show_remote_protocol_vcont_packet_cmd, |
| 5751 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5752 | 0); |
| 5753 | |
| 5754 | add_packet_config_cmd (&remote_protocol_qSymbol, |
| 5755 | "qSymbol", "symbol-lookup", |
| 5756 | set_remote_protocol_qSymbol_packet_cmd, |
| 5757 | show_remote_protocol_qSymbol_packet_cmd, |
| 5758 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5759 | 0); |
| 5760 | |
| 5761 | add_packet_config_cmd (&remote_protocol_P, |
| 5762 | "P", "set-register", |
| 5763 | set_remote_protocol_P_packet_cmd, |
| 5764 | show_remote_protocol_P_packet_cmd, |
| 5765 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5766 | 1); |
| 5767 | |
| 5768 | add_packet_config_cmd (&remote_protocol_p, |
| 5769 | "p", "fetch-register", |
| 5770 | set_remote_protocol_p_packet_cmd, |
| 5771 | show_remote_protocol_p_packet_cmd, |
| 5772 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5773 | 1); |
| 5774 | |
| 5775 | add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP], |
| 5776 | "Z0", "software-breakpoint", |
| 5777 | set_remote_protocol_Z_software_bp_packet_cmd, |
| 5778 | show_remote_protocol_Z_software_bp_packet_cmd, |
| 5779 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5780 | 0); |
| 5781 | |
| 5782 | add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP], |
| 5783 | "Z1", "hardware-breakpoint", |
| 5784 | set_remote_protocol_Z_hardware_bp_packet_cmd, |
| 5785 | show_remote_protocol_Z_hardware_bp_packet_cmd, |
| 5786 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5787 | 0); |
| 5788 | |
| 5789 | add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP], |
| 5790 | "Z2", "write-watchpoint", |
| 5791 | set_remote_protocol_Z_write_wp_packet_cmd, |
| 5792 | show_remote_protocol_Z_write_wp_packet_cmd, |
| 5793 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5794 | 0); |
| 5795 | |
| 5796 | add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP], |
| 5797 | "Z3", "read-watchpoint", |
| 5798 | set_remote_protocol_Z_read_wp_packet_cmd, |
| 5799 | show_remote_protocol_Z_read_wp_packet_cmd, |
| 5800 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5801 | 0); |
| 5802 | |
| 5803 | add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP], |
| 5804 | "Z4", "access-watchpoint", |
| 5805 | set_remote_protocol_Z_access_wp_packet_cmd, |
| 5806 | show_remote_protocol_Z_access_wp_packet_cmd, |
| 5807 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5808 | 0); |
| 5809 | |
| 5810 | add_packet_config_cmd (&remote_protocol_qPart_auxv, |
| 5811 | "qPart_auxv", "read-aux-vector", |
| 5812 | set_remote_protocol_qPart_auxv_packet_cmd, |
| 5813 | show_remote_protocol_qPart_auxv_packet_cmd, |
| 5814 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5815 | 0); |
| 5816 | |
| 5817 | add_packet_config_cmd (&remote_protocol_qGetTLSAddr, |
| 5818 | "qGetTLSAddr", "get-thread-local-storage-address", |
| 5819 | set_remote_protocol_qGetTLSAddr_packet_cmd, |
| 5820 | show_remote_protocol_qGetTLSAddr_packet_cmd, |
| 5821 | &remote_set_cmdlist, &remote_show_cmdlist, |
| 5822 | 0); |
| 5823 | |
| 5824 | /* Keep the old ``set remote Z-packet ...'' working. */ |
| 5825 | add_setshow_auto_boolean_cmd ("Z-packet", class_obscure, |
| 5826 | &remote_Z_packet_detect, _("\ |
| 5827 | Set use of remote protocol `Z' packets"), _("\ |
| 5828 | Show use of remote protocol `Z' packets "), _("\ |
| 5829 | When set, GDB will attempt to use the remote breakpoint and watchpoint\n\ |
| 5830 | packets."), |
| 5831 | set_remote_protocol_Z_packet_cmd, |
| 5832 | show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */ |
| 5833 | &remote_set_cmdlist, &remote_show_cmdlist); |
| 5834 | |
| 5835 | /* Eventually initialize fileio. See fileio.c */ |
| 5836 | initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist); |
| 5837 | } |