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