1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* See the GDB User Guide for details of the GDB remote protocol. */
27 #include "gdb_string.h"
33 #include "exceptions.h"
35 /*#include "terminal.h" */
38 #include "gdb-stabs.h"
39 #include "gdbthread.h"
43 #include "gdb_assert.h"
48 #include "event-loop.h"
49 #include "event-top.h"
55 #include "gdbcore.h" /* for exec_bfd */
57 #include "remote-fileio.h"
59 /* Prototypes for local functions */
60 static void cleanup_sigint_signal_handler (void *dummy);
61 static void initialize_sigint_signal_handler (void);
62 static int getpkt_sane (char *buf, long sizeof_buf, int forever);
64 static void handle_remote_sigint (int);
65 static void handle_remote_sigint_twice (int);
66 static void async_remote_interrupt (gdb_client_data);
67 void async_remote_interrupt_twice (gdb_client_data);
69 static void build_remote_gdbarch_data (void);
71 static void remote_files_info (struct target_ops *ignore);
73 static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
74 int len, int should_write,
75 struct mem_attrib *attrib,
76 struct target_ops *target);
78 static void remote_prepare_to_store (void);
80 static void remote_fetch_registers (int regno);
82 static void remote_resume (ptid_t ptid, int step,
83 enum target_signal siggnal);
84 static void remote_async_resume (ptid_t ptid, int step,
85 enum target_signal siggnal);
86 static int remote_start_remote (struct ui_out *uiout, void *dummy);
88 static void remote_open (char *name, int from_tty);
89 static void remote_async_open (char *name, int from_tty);
91 static void extended_remote_open (char *name, int from_tty);
92 static void extended_remote_async_open (char *name, int from_tty);
94 static void remote_open_1 (char *, int, struct target_ops *, int extended_p,
97 static void remote_close (int quitting);
99 static void remote_store_registers (int regno);
101 static void remote_mourn (void);
102 static void remote_async_mourn (void);
104 static void extended_remote_restart (void);
106 static void extended_remote_mourn (void);
108 static void remote_mourn_1 (struct target_ops *);
110 static void remote_send (char *buf, long sizeof_buf);
112 static int readchar (int timeout);
114 static ptid_t remote_wait (ptid_t ptid,
115 struct target_waitstatus *status);
116 static ptid_t remote_async_wait (ptid_t ptid,
117 struct target_waitstatus *status);
119 static void remote_kill (void);
120 static void remote_async_kill (void);
122 static int tohex (int nib);
124 static void remote_detach (char *args, int from_tty);
126 static void remote_interrupt (int signo);
128 static void remote_interrupt_twice (int signo);
130 static void interrupt_query (void);
132 static void set_thread (int, int);
134 static int remote_thread_alive (ptid_t);
136 static void get_offsets (void);
138 static long read_frame (char *buf, long sizeof_buf);
140 static int remote_insert_breakpoint (CORE_ADDR, char *);
142 static int remote_remove_breakpoint (CORE_ADDR, char *);
144 static int hexnumlen (ULONGEST num);
146 static void init_remote_ops (void);
148 static void init_extended_remote_ops (void);
150 static void remote_stop (void);
152 static int ishex (int ch, int *val);
154 static int stubhex (int ch);
156 static int hexnumstr (char *, ULONGEST);
158 static int hexnumnstr (char *, ULONGEST, int);
160 static CORE_ADDR remote_address_masked (CORE_ADDR);
162 static void print_packet (char *);
164 static unsigned long crc32 (unsigned char *, int, unsigned int);
166 static void compare_sections_command (char *, int);
168 static void packet_command (char *, int);
170 static int stub_unpack_int (char *buff, int fieldlength);
172 static ptid_t remote_current_thread (ptid_t oldptid);
174 static void remote_find_new_threads (void);
176 static void record_currthread (int currthread);
178 static int fromhex (int a);
180 static int hex2bin (const char *hex, char *bin, int count);
182 static int bin2hex (const char *bin, char *hex, int count);
184 static int putpkt_binary (char *buf, int cnt);
186 static void check_binary_download (CORE_ADDR addr);
188 struct packet_config;
190 static void show_packet_config_cmd (struct packet_config *config);
192 static void update_packet_config (struct packet_config *config);
194 void _initialize_remote (void);
196 /* Description of the remote protocol. Strictly speaking, when the
197 target is open()ed, remote.c should create a per-target description
198 of the remote protocol using that target's architecture.
199 Unfortunately, the target stack doesn't include local state. For
200 the moment keep the information in the target's architecture
205 long offset; /* Offset into G packet. */
206 long regnum; /* GDB's internal register number. */
207 LONGEST pnum; /* Remote protocol register number. */
208 int in_g_packet; /* Always part of G packet. */
209 /* long size in bytes; == register_size (current_gdbarch, regnum); at present. */
210 /* char *name; == REGISTER_NAME (regnum); at present. */
215 /* Description of the remote protocol registers. */
216 long sizeof_g_packet;
218 /* Description of the remote protocol registers indexed by REGNUM
219 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
220 struct packet_reg *regs;
222 /* This is the size (in chars) of the first response to the ``g''
223 packet. It is used as a heuristic when determining the maximum
224 size of memory-read and memory-write packets. A target will
225 typically only reserve a buffer large enough to hold the ``g''
226 packet. The size does not include packet overhead (headers and
228 long actual_register_packet_size;
230 /* This is the maximum size (in chars) of a non read/write packet.
231 It is also used as a cap on the size of read/write packets. */
232 long remote_packet_size;
236 /* Handle for retreving the remote protocol data from gdbarch. */
237 static struct gdbarch_data *remote_gdbarch_data_handle;
239 static struct remote_state *
240 get_remote_state (void)
242 return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle);
246 init_remote_state (struct gdbarch *gdbarch)
249 struct remote_state *rs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_state);
251 if (deprecated_register_bytes () != 0)
252 rs->sizeof_g_packet = deprecated_register_bytes ();
254 rs->sizeof_g_packet = 0;
256 /* Assume a 1:1 regnum<->pnum table. */
257 rs->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS,
259 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
261 struct packet_reg *r = &rs->regs[regnum];
264 r->offset = DEPRECATED_REGISTER_BYTE (regnum);
265 r->in_g_packet = (regnum < NUM_REGS);
266 /* ...name = REGISTER_NAME (regnum); */
268 /* Compute packet size by accumulating the size of all registers. */
269 if (deprecated_register_bytes () == 0)
270 rs->sizeof_g_packet += register_size (current_gdbarch, regnum);
273 /* Default maximum number of characters in a packet body. Many
274 remote stubs have a hardwired buffer size of 400 bytes
275 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
276 as the maximum packet-size to ensure that the packet and an extra
277 NUL character can always fit in the buffer. This stops GDB
278 trashing stubs that try to squeeze an extra NUL into what is
279 already a full buffer (As of 1999-12-04 that was most stubs. */
280 rs->remote_packet_size = 400 - 1;
282 /* Should rs->sizeof_g_packet needs more space than the
283 default, adjust the size accordingly. Remember that each byte is
284 encoded as two characters. 32 is the overhead for the packet
285 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
286 (``$NN:G...#NN'') is a better guess, the below has been padded a
288 if (rs->sizeof_g_packet > ((rs->remote_packet_size - 32) / 2))
289 rs->remote_packet_size = (rs->sizeof_g_packet * 2 + 32);
291 /* This one is filled in when a ``g'' packet is received. */
292 rs->actual_register_packet_size = 0;
297 static struct packet_reg *
298 packet_reg_from_regnum (struct remote_state *rs, long regnum)
300 if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS)
304 struct packet_reg *r = &rs->regs[regnum];
305 gdb_assert (r->regnum == regnum);
310 static struct packet_reg *
311 packet_reg_from_pnum (struct remote_state *rs, LONGEST pnum)
314 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
316 struct packet_reg *r = &rs->regs[i];
323 /* FIXME: graces/2002-08-08: These variables should eventually be
324 bound to an instance of the target object (as in gdbarch-tdep()),
325 when such a thing exists. */
327 /* This is set to the data address of the access causing the target
328 to stop for a watchpoint. */
329 static CORE_ADDR remote_watch_data_address;
331 /* This is non-zero if taregt stopped for a watchpoint. */
332 static int remote_stopped_by_watchpoint_p;
335 static struct target_ops remote_ops;
337 static struct target_ops extended_remote_ops;
339 /* Temporary target ops. Just like the remote_ops and
340 extended_remote_ops, but with asynchronous support. */
341 static struct target_ops remote_async_ops;
343 static struct target_ops extended_async_remote_ops;
345 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
346 ``forever'' still use the normal timeout mechanism. This is
347 currently used by the ASYNC code to guarentee that target reads
348 during the initial connect always time-out. Once getpkt has been
349 modified to return a timeout indication and, in turn
350 remote_wait()/wait_for_inferior() have gained a timeout parameter
352 static int wait_forever_enabled_p = 1;
355 /* This variable chooses whether to send a ^C or a break when the user
356 requests program interruption. Although ^C is usually what remote
357 systems expect, and that is the default here, sometimes a break is
358 preferable instead. */
360 static int remote_break;
362 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
363 remote_open knows that we don't have a file open when the program
365 static struct serial *remote_desc = NULL;
367 /* This variable sets the number of bits in an address that are to be
368 sent in a memory ("M" or "m") packet. Normally, after stripping
369 leading zeros, the entire address would be sent. This variable
370 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
371 initial implementation of remote.c restricted the address sent in
372 memory packets to ``host::sizeof long'' bytes - (typically 32
373 bits). Consequently, for 64 bit targets, the upper 32 bits of an
374 address was never sent. Since fixing this bug may cause a break in
375 some remote targets this variable is principly provided to
376 facilitate backward compatibility. */
378 static int remote_address_size;
380 /* Tempoary to track who currently owns the terminal. See
381 target_async_terminal_* for more details. */
383 static int remote_async_terminal_ours_p;
386 /* User configurable variables for the number of characters in a
387 memory read/write packet. MIN ((rs->remote_packet_size),
388 rs->sizeof_g_packet) is the default. Some targets need smaller
389 values (fifo overruns, et.al.) and some users need larger values
390 (speed up transfers). The variables ``preferred_*'' (the user
391 request), ``current_*'' (what was actually set) and ``forced_*''
392 (Positive - a soft limit, negative - a hard limit). */
394 struct memory_packet_config
401 /* Compute the current size of a read/write packet. Since this makes
402 use of ``actual_register_packet_size'' the computation is dynamic. */
405 get_memory_packet_size (struct memory_packet_config *config)
407 struct remote_state *rs = get_remote_state ();
408 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
409 law?) that some hosts don't cope very well with large alloca()
410 calls. Eventually the alloca() code will be replaced by calls to
411 xmalloc() and make_cleanups() allowing this restriction to either
412 be lifted or removed. */
413 #ifndef MAX_REMOTE_PACKET_SIZE
414 #define MAX_REMOTE_PACKET_SIZE 16384
416 /* NOTE: 16 is just chosen at random. */
417 #ifndef MIN_REMOTE_PACKET_SIZE
418 #define MIN_REMOTE_PACKET_SIZE 16
423 if (config->size <= 0)
424 what_they_get = MAX_REMOTE_PACKET_SIZE;
426 what_they_get = config->size;
430 what_they_get = (rs->remote_packet_size);
431 /* Limit the packet to the size specified by the user. */
433 && what_they_get > config->size)
434 what_they_get = config->size;
435 /* Limit it to the size of the targets ``g'' response. */
436 if ((rs->actual_register_packet_size) > 0
437 && what_they_get > (rs->actual_register_packet_size))
438 what_they_get = (rs->actual_register_packet_size);
440 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
441 what_they_get = MAX_REMOTE_PACKET_SIZE;
442 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
443 what_they_get = MIN_REMOTE_PACKET_SIZE;
444 return what_they_get;
447 /* Update the size of a read/write packet. If they user wants
448 something really big then do a sanity check. */
451 set_memory_packet_size (char *args, struct memory_packet_config *config)
453 int fixed_p = config->fixed_p;
454 long size = config->size;
456 error ("Argument required (integer, `fixed' or `limited').");
457 else if (strcmp (args, "hard") == 0
458 || strcmp (args, "fixed") == 0)
460 else if (strcmp (args, "soft") == 0
461 || strcmp (args, "limit") == 0)
466 size = strtoul (args, &end, 0);
468 error ("Invalid %s (bad syntax).", config->name);
470 /* Instead of explicitly capping the size of a packet to
471 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
472 instead allowed to set the size to something arbitrarily
474 if (size > MAX_REMOTE_PACKET_SIZE)
475 error ("Invalid %s (too large).", config->name);
479 if (fixed_p && !config->fixed_p)
481 if (! query ("The target may not be able to correctly handle a %s\n"
482 "of %ld bytes. Change the packet size? ",
484 error ("Packet size not changed.");
486 /* Update the config. */
487 config->fixed_p = fixed_p;
492 show_memory_packet_size (struct memory_packet_config *config)
494 printf_filtered ("The %s is %ld. ", config->name, config->size);
496 printf_filtered ("Packets are fixed at %ld bytes.\n",
497 get_memory_packet_size (config));
499 printf_filtered ("Packets are limited to %ld bytes.\n",
500 get_memory_packet_size (config));
503 static struct memory_packet_config memory_write_packet_config =
505 "memory-write-packet-size",
509 set_memory_write_packet_size (char *args, int from_tty)
511 set_memory_packet_size (args, &memory_write_packet_config);
515 show_memory_write_packet_size (char *args, int from_tty)
517 show_memory_packet_size (&memory_write_packet_config);
521 get_memory_write_packet_size (void)
523 return get_memory_packet_size (&memory_write_packet_config);
526 static struct memory_packet_config memory_read_packet_config =
528 "memory-read-packet-size",
532 set_memory_read_packet_size (char *args, int from_tty)
534 set_memory_packet_size (args, &memory_read_packet_config);
538 show_memory_read_packet_size (char *args, int from_tty)
540 show_memory_packet_size (&memory_read_packet_config);
544 get_memory_read_packet_size (void)
546 struct remote_state *rs = get_remote_state ();
547 long size = get_memory_packet_size (&memory_read_packet_config);
548 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
549 extra buffer size argument before the memory read size can be
550 increased beyond (rs->remote_packet_size). */
551 if (size > (rs->remote_packet_size))
552 size = (rs->remote_packet_size);
557 /* Generic configuration support for packets the stub optionally
558 supports. Allows the user to specify the use of the packet as well
559 as allowing GDB to auto-detect support in the remote stub. */
563 PACKET_SUPPORT_UNKNOWN = 0,
572 enum auto_boolean detect;
573 enum packet_support support;
576 /* Analyze a packet's return value and update the packet config
587 update_packet_config (struct packet_config *config)
589 switch (config->detect)
591 case AUTO_BOOLEAN_TRUE:
592 config->support = PACKET_ENABLE;
594 case AUTO_BOOLEAN_FALSE:
595 config->support = PACKET_DISABLE;
597 case AUTO_BOOLEAN_AUTO:
598 config->support = PACKET_SUPPORT_UNKNOWN;
604 show_packet_config_cmd (struct packet_config *config)
606 char *support = "internal-error";
607 switch (config->support)
613 support = "disabled";
615 case PACKET_SUPPORT_UNKNOWN:
619 switch (config->detect)
621 case AUTO_BOOLEAN_AUTO:
622 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
623 config->name, config->title, support);
625 case AUTO_BOOLEAN_TRUE:
626 case AUTO_BOOLEAN_FALSE:
627 printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
628 config->name, config->title, support);
634 add_packet_config_cmd (struct packet_config *config,
637 cmd_sfunc_ftype *set_func,
638 cmd_sfunc_ftype *show_func,
639 struct cmd_list_element **set_remote_list,
640 struct cmd_list_element **show_remote_list,
643 struct cmd_list_element *set_cmd;
644 struct cmd_list_element *show_cmd;
651 config->title = title;
652 config->detect = AUTO_BOOLEAN_AUTO;
653 config->support = PACKET_SUPPORT_UNKNOWN;
654 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
656 show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
658 print = xstrprintf ("Current use of remote protocol `%s' (%s) is %%s",
660 /* set/show TITLE-packet {auto,on,off} */
661 cmd_name = xstrprintf ("%s-packet", title);
662 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
663 &config->detect, set_doc, show_doc,
666 set_remote_list, show_remote_list);
667 /* set/show remote NAME-packet {auto,on,off} -- legacy */
671 legacy_name = xstrprintf ("%s-packet", name);
672 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
674 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
679 static enum packet_result
680 packet_ok (const char *buf, struct packet_config *config)
684 /* The stub recognized the packet request. Check that the
685 operation succeeded. */
686 switch (config->support)
688 case PACKET_SUPPORT_UNKNOWN:
690 fprintf_unfiltered (gdb_stdlog,
691 "Packet %s (%s) is supported\n",
692 config->name, config->title);
693 config->support = PACKET_ENABLE;
696 internal_error (__FILE__, __LINE__,
697 "packet_ok: attempt to use a disabled packet");
702 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
703 /* "OK" - definitly OK. */
706 && isxdigit (buf[1]) && isxdigit (buf[2])
708 /* "Enn" - definitly an error. */
710 /* The packet may or may not be OK. Just assume it is */
715 /* The stub does not support the packet. */
716 switch (config->support)
719 if (config->detect == AUTO_BOOLEAN_AUTO)
720 /* If the stub previously indicated that the packet was
721 supported then there is a protocol error.. */
722 error ("Protocol error: %s (%s) conflicting enabled responses.",
723 config->name, config->title);
725 /* The user set it wrong. */
726 error ("Enabled packet %s (%s) not recognized by stub",
727 config->name, config->title);
729 case PACKET_SUPPORT_UNKNOWN:
731 fprintf_unfiltered (gdb_stdlog,
732 "Packet %s (%s) is NOT supported\n",
733 config->name, config->title);
734 config->support = PACKET_DISABLE;
739 return PACKET_UNKNOWN;
743 /* Should we try the 'vCont' (descriptive resume) request? */
744 static struct packet_config remote_protocol_vcont;
747 set_remote_protocol_vcont_packet_cmd (char *args, int from_tty,
748 struct cmd_list_element *c)
750 update_packet_config (&remote_protocol_vcont);
754 show_remote_protocol_vcont_packet_cmd (char *args, int from_tty,
755 struct cmd_list_element *c)
757 show_packet_config_cmd (&remote_protocol_vcont);
760 /* Should we try the 'qSymbol' (target symbol lookup service) request? */
761 static struct packet_config remote_protocol_qSymbol;
764 set_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty,
765 struct cmd_list_element *c)
767 update_packet_config (&remote_protocol_qSymbol);
771 show_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty,
772 struct cmd_list_element *c)
774 show_packet_config_cmd (&remote_protocol_qSymbol);
777 /* Should we try the 'P' (set register) request? */
779 static struct packet_config remote_protocol_P;
782 set_remote_protocol_P_packet_cmd (char *args, int from_tty,
783 struct cmd_list_element *c)
785 update_packet_config (&remote_protocol_P);
789 show_remote_protocol_P_packet_cmd (char *args, int from_tty,
790 struct cmd_list_element *c)
792 show_packet_config_cmd (&remote_protocol_P);
795 /* Should we try one of the 'Z' requests? */
799 Z_PACKET_SOFTWARE_BP,
800 Z_PACKET_HARDWARE_BP,
807 static struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES];
809 /* FIXME: Instead of having all these boiler plate functions, the
810 command callback should include a context argument. */
813 set_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
814 struct cmd_list_element *c)
816 update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
820 show_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
821 struct cmd_list_element *c)
823 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
827 set_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
828 struct cmd_list_element *c)
830 update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
834 show_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
835 struct cmd_list_element *c)
837 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
841 set_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
842 struct cmd_list_element *c)
844 update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
848 show_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
849 struct cmd_list_element *c)
851 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
855 set_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
856 struct cmd_list_element *c)
858 update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]);
862 show_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
863 struct cmd_list_element *c)
865 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
869 set_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
870 struct cmd_list_element *c)
872 update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
876 show_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
877 struct cmd_list_element *c)
879 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
882 /* For compatibility with older distributions. Provide a ``set remote
883 Z-packet ...'' command that updates all the Z packet types. */
885 static enum auto_boolean remote_Z_packet_detect;
888 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
889 struct cmd_list_element *c)
892 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
894 remote_protocol_Z[i].detect = remote_Z_packet_detect;
895 update_packet_config (&remote_protocol_Z[i]);
900 show_remote_protocol_Z_packet_cmd (char *args, int from_tty,
901 struct cmd_list_element *c)
904 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
906 show_packet_config_cmd (&remote_protocol_Z[i]);
910 /* Should we try the 'X' (remote binary download) packet?
912 This variable (available to the user via "set remote X-packet")
913 dictates whether downloads are sent in binary (via the 'X' packet).
914 We assume that the stub can, and attempt to do it. This will be
915 cleared if the stub does not understand it. This switch is still
916 needed, though in cases when the packet is supported in the stub,
917 but the connection does not allow it (i.e., 7-bit serial connection
920 static struct packet_config remote_protocol_binary_download;
922 /* Should we try the 'ThreadInfo' query packet?
924 This variable (NOT available to the user: auto-detect only!)
925 determines whether GDB will use the new, simpler "ThreadInfo"
926 query or the older, more complex syntax for thread queries.
927 This is an auto-detect variable (set to true at each connect,
928 and set to false when the target fails to recognize it). */
930 static int use_threadinfo_query;
931 static int use_threadextra_query;
934 set_remote_protocol_binary_download_cmd (char *args,
936 struct cmd_list_element *c)
938 update_packet_config (&remote_protocol_binary_download);
942 show_remote_protocol_binary_download_cmd (char *args, int from_tty,
943 struct cmd_list_element *c)
945 show_packet_config_cmd (&remote_protocol_binary_download);
948 /* Should we try the 'qPart:auxv' (target auxiliary vector read) request? */
949 static struct packet_config remote_protocol_qPart_auxv;
952 set_remote_protocol_qPart_auxv_packet_cmd (char *args, int from_tty,
953 struct cmd_list_element *c)
955 update_packet_config (&remote_protocol_qPart_auxv);
959 show_remote_protocol_qPart_auxv_packet_cmd (char *args, int from_tty,
960 struct cmd_list_element *c)
962 show_packet_config_cmd (&remote_protocol_qPart_auxv);
965 static struct packet_config remote_protocol_p;
968 set_remote_protocol_p_packet_cmd (char *args, int from_tty,
969 struct cmd_list_element *c)
971 update_packet_config (&remote_protocol_p);
975 show_remote_protocol_p_packet_cmd (char *args, int from_tty,
976 struct cmd_list_element *c)
978 show_packet_config_cmd (&remote_protocol_p);
983 /* Tokens for use by the asynchronous signal handlers for SIGINT */
984 static void *sigint_remote_twice_token;
985 static void *sigint_remote_token;
987 /* These are pointers to hook functions that may be set in order to
988 modify resume/wait behavior for a particular architecture. */
990 void (*deprecated_target_resume_hook) (void);
991 void (*deprecated_target_wait_loop_hook) (void);
995 /* These are the threads which we last sent to the remote system.
996 -1 for all or -2 for not sent yet. */
997 static int general_thread;
998 static int continue_thread;
1000 /* Call this function as a result of
1001 1) A halt indication (T packet) containing a thread id
1002 2) A direct query of currthread
1003 3) Successful execution of set thread
1007 record_currthread (int currthread)
1009 general_thread = currthread;
1011 /* If this is a new thread, add it to GDB's thread list.
1012 If we leave it up to WFI to do this, bad things will happen. */
1013 if (!in_thread_list (pid_to_ptid (currthread)))
1015 add_thread (pid_to_ptid (currthread));
1016 ui_out_text (uiout, "[New ");
1017 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
1018 ui_out_text (uiout, "]\n");
1022 #define MAGIC_NULL_PID 42000
1025 set_thread (int th, int gen)
1027 struct remote_state *rs = get_remote_state ();
1028 char *buf = alloca (rs->remote_packet_size);
1029 int state = gen ? general_thread : continue_thread;
1035 buf[1] = gen ? 'g' : 'c';
1036 if (th == MAGIC_NULL_PID)
1042 sprintf (&buf[2], "-%x", -th);
1044 sprintf (&buf[2], "%x", th);
1046 getpkt (buf, (rs->remote_packet_size), 0);
1048 general_thread = th;
1050 continue_thread = th;
1053 /* Return nonzero if the thread TH is still alive on the remote system. */
1056 remote_thread_alive (ptid_t ptid)
1058 int tid = PIDGET (ptid);
1062 sprintf (buf, "T-%08x", -tid);
1064 sprintf (buf, "T%08x", tid);
1066 getpkt (buf, sizeof (buf), 0);
1067 return (buf[0] == 'O' && buf[1] == 'K');
1070 /* About these extended threadlist and threadinfo packets. They are
1071 variable length packets but, the fields within them are often fixed
1072 length. They are redundent enough to send over UDP as is the
1073 remote protocol in general. There is a matching unit test module
1076 #define OPAQUETHREADBYTES 8
1078 /* a 64 bit opaque identifier */
1079 typedef unsigned char threadref[OPAQUETHREADBYTES];
1081 /* WARNING: This threadref data structure comes from the remote O.S., libstub
1082 protocol encoding, and remote.c. it is not particularly changable */
1084 /* Right now, the internal structure is int. We want it to be bigger.
1088 typedef int gdb_threadref; /* internal GDB thread reference */
1090 /* gdb_ext_thread_info is an internal GDB data structure which is
1091 equivalint to the reply of the remote threadinfo packet */
1093 struct gdb_ext_thread_info
1095 threadref threadid; /* External form of thread reference */
1096 int active; /* Has state interesting to GDB? , regs, stack */
1097 char display[256]; /* Brief state display, name, blocked/syspended */
1098 char shortname[32]; /* To be used to name threads */
1099 char more_display[256]; /* Long info, statistics, queue depth, whatever */
1102 /* The volume of remote transfers can be limited by submitting
1103 a mask containing bits specifying the desired information.
1104 Use a union of these values as the 'selection' parameter to
1105 get_thread_info. FIXME: Make these TAG names more thread specific.
1108 #define TAG_THREADID 1
1109 #define TAG_EXISTS 2
1110 #define TAG_DISPLAY 4
1111 #define TAG_THREADNAME 8
1112 #define TAG_MOREDISPLAY 16
1114 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
1116 char *unpack_varlen_hex (char *buff, ULONGEST *result);
1118 static char *unpack_nibble (char *buf, int *val);
1120 static char *pack_nibble (char *buf, int nibble);
1122 static char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
1124 static char *unpack_byte (char *buf, int *value);
1126 static char *pack_int (char *buf, int value);
1128 static char *unpack_int (char *buf, int *value);
1130 static char *unpack_string (char *src, char *dest, int length);
1132 static char *pack_threadid (char *pkt, threadref * id);
1134 static char *unpack_threadid (char *inbuf, threadref * id);
1136 void int_to_threadref (threadref * id, int value);
1138 static int threadref_to_int (threadref * ref);
1140 static void copy_threadref (threadref * dest, threadref * src);
1142 static int threadmatch (threadref * dest, threadref * src);
1144 static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
1146 static int remote_unpack_thread_info_response (char *pkt,
1147 threadref * expectedref,
1148 struct gdb_ext_thread_info
1152 static int remote_get_threadinfo (threadref * threadid, int fieldset, /*TAG mask */
1153 struct gdb_ext_thread_info *info);
1155 static char *pack_threadlist_request (char *pkt, int startflag,
1157 threadref * nextthread);
1159 static int parse_threadlist_response (char *pkt,
1161 threadref * original_echo,
1162 threadref * resultlist, int *doneflag);
1164 static int remote_get_threadlist (int startflag,
1165 threadref * nextthread,
1168 int *result_count, threadref * threadlist);
1170 typedef int (*rmt_thread_action) (threadref * ref, void *context);
1172 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1173 void *context, int looplimit);
1175 static int remote_newthread_step (threadref * ref, void *context);
1177 /* encode 64 bits in 16 chars of hex */
1179 static const char hexchars[] = "0123456789abcdef";
1182 ishex (int ch, int *val)
1184 if ((ch >= 'a') && (ch <= 'f'))
1186 *val = ch - 'a' + 10;
1189 if ((ch >= 'A') && (ch <= 'F'))
1191 *val = ch - 'A' + 10;
1194 if ((ch >= '0') && (ch <= '9'))
1205 if (ch >= 'a' && ch <= 'f')
1206 return ch - 'a' + 10;
1207 if (ch >= '0' && ch <= '9')
1209 if (ch >= 'A' && ch <= 'F')
1210 return ch - 'A' + 10;
1215 stub_unpack_int (char *buff, int fieldlength)
1222 nibble = stubhex (*buff++);
1226 retval = retval << 4;
1232 unpack_varlen_hex (char *buff, /* packet to parse */
1238 while (ishex (*buff, &nibble))
1241 retval = retval << 4;
1242 retval |= nibble & 0x0f;
1249 unpack_nibble (char *buf, int *val)
1251 ishex (*buf++, val);
1256 pack_nibble (char *buf, int nibble)
1258 *buf++ = hexchars[(nibble & 0x0f)];
1263 pack_hex_byte (char *pkt, int byte)
1265 *pkt++ = hexchars[(byte >> 4) & 0xf];
1266 *pkt++ = hexchars[(byte & 0xf)];
1271 unpack_byte (char *buf, int *value)
1273 *value = stub_unpack_int (buf, 2);
1278 pack_int (char *buf, int value)
1280 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1281 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1282 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1283 buf = pack_hex_byte (buf, (value & 0xff));
1288 unpack_int (char *buf, int *value)
1290 *value = stub_unpack_int (buf, 8);
1294 #if 0 /* currently unused, uncomment when needed */
1295 static char *pack_string (char *pkt, char *string);
1298 pack_string (char *pkt, char *string)
1303 len = strlen (string);
1305 len = 200; /* Bigger than most GDB packets, junk??? */
1306 pkt = pack_hex_byte (pkt, len);
1310 if ((ch == '\0') || (ch == '#'))
1311 ch = '*'; /* Protect encapsulation */
1316 #endif /* 0 (unused) */
1319 unpack_string (char *src, char *dest, int length)
1328 pack_threadid (char *pkt, threadref *id)
1331 unsigned char *altid;
1333 altid = (unsigned char *) id;
1334 limit = pkt + BUF_THREAD_ID_SIZE;
1336 pkt = pack_hex_byte (pkt, *altid++);
1342 unpack_threadid (char *inbuf, threadref *id)
1345 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1348 altref = (char *) id;
1350 while (inbuf < limit)
1352 x = stubhex (*inbuf++);
1353 y = stubhex (*inbuf++);
1354 *altref++ = (x << 4) | y;
1359 /* Externally, threadrefs are 64 bits but internally, they are still
1360 ints. This is due to a mismatch of specifications. We would like
1361 to use 64bit thread references internally. This is an adapter
1365 int_to_threadref (threadref *id, int value)
1367 unsigned char *scan;
1369 scan = (unsigned char *) id;
1375 *scan++ = (value >> 24) & 0xff;
1376 *scan++ = (value >> 16) & 0xff;
1377 *scan++ = (value >> 8) & 0xff;
1378 *scan++ = (value & 0xff);
1382 threadref_to_int (threadref *ref)
1385 unsigned char *scan;
1387 scan = (char *) ref;
1391 value = (value << 8) | ((*scan++) & 0xff);
1396 copy_threadref (threadref *dest, threadref *src)
1399 unsigned char *csrc, *cdest;
1401 csrc = (unsigned char *) src;
1402 cdest = (unsigned char *) dest;
1409 threadmatch (threadref *dest, threadref *src)
1411 /* things are broken right now, so just assume we got a match */
1413 unsigned char *srcp, *destp;
1415 srcp = (char *) src;
1416 destp = (char *) dest;
1420 result &= (*srcp++ == *destp++) ? 1 : 0;
1427 threadid:1, # always request threadid
1434 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1437 pack_threadinfo_request (char *pkt, int mode, threadref *id)
1439 *pkt++ = 'q'; /* Info Query */
1440 *pkt++ = 'P'; /* process or thread info */
1441 pkt = pack_int (pkt, mode); /* mode */
1442 pkt = pack_threadid (pkt, id); /* threadid */
1443 *pkt = '\0'; /* terminate */
1447 /* These values tag the fields in a thread info response packet */
1448 /* Tagging the fields allows us to request specific fields and to
1449 add more fields as time goes by */
1451 #define TAG_THREADID 1 /* Echo the thread identifier */
1452 #define TAG_EXISTS 2 /* Is this process defined enough to
1453 fetch registers and its stack */
1454 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1455 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1456 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1460 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1461 struct gdb_ext_thread_info *info)
1463 struct remote_state *rs = get_remote_state ();
1467 char *limit = pkt + (rs->remote_packet_size); /* plausable parsing limit */
1470 /* info->threadid = 0; FIXME: implement zero_threadref */
1472 info->display[0] = '\0';
1473 info->shortname[0] = '\0';
1474 info->more_display[0] = '\0';
1476 /* Assume the characters indicating the packet type have been stripped */
1477 pkt = unpack_int (pkt, &mask); /* arg mask */
1478 pkt = unpack_threadid (pkt, &ref);
1481 warning ("Incomplete response to threadinfo request\n");
1482 if (!threadmatch (&ref, expectedref))
1483 { /* This is an answer to a different request */
1484 warning ("ERROR RMT Thread info mismatch\n");
1487 copy_threadref (&info->threadid, &ref);
1489 /* Loop on tagged fields , try to bail if somthing goes wrong */
1491 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
1493 pkt = unpack_int (pkt, &tag); /* tag */
1494 pkt = unpack_byte (pkt, &length); /* length */
1495 if (!(tag & mask)) /* tags out of synch with mask */
1497 warning ("ERROR RMT: threadinfo tag mismatch\n");
1501 if (tag == TAG_THREADID)
1505 warning ("ERROR RMT: length of threadid is not 16\n");
1509 pkt = unpack_threadid (pkt, &ref);
1510 mask = mask & ~TAG_THREADID;
1513 if (tag == TAG_EXISTS)
1515 info->active = stub_unpack_int (pkt, length);
1517 mask = mask & ~(TAG_EXISTS);
1520 warning ("ERROR RMT: 'exists' length too long\n");
1526 if (tag == TAG_THREADNAME)
1528 pkt = unpack_string (pkt, &info->shortname[0], length);
1529 mask = mask & ~TAG_THREADNAME;
1532 if (tag == TAG_DISPLAY)
1534 pkt = unpack_string (pkt, &info->display[0], length);
1535 mask = mask & ~TAG_DISPLAY;
1538 if (tag == TAG_MOREDISPLAY)
1540 pkt = unpack_string (pkt, &info->more_display[0], length);
1541 mask = mask & ~TAG_MOREDISPLAY;
1544 warning ("ERROR RMT: unknown thread info tag\n");
1545 break; /* Not a tag we know about */
1551 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1552 struct gdb_ext_thread_info *info)
1554 struct remote_state *rs = get_remote_state ();
1556 char *threadinfo_pkt = alloca (rs->remote_packet_size);
1558 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1559 putpkt (threadinfo_pkt);
1560 getpkt (threadinfo_pkt, (rs->remote_packet_size), 0);
1561 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1566 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1569 pack_threadlist_request (char *pkt, int startflag, int threadcount,
1570 threadref *nextthread)
1572 *pkt++ = 'q'; /* info query packet */
1573 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1574 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1575 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1576 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1581 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1584 parse_threadlist_response (char *pkt, int result_limit,
1585 threadref *original_echo, threadref *resultlist,
1588 struct remote_state *rs = get_remote_state ();
1590 int count, resultcount, done;
1593 /* Assume the 'q' and 'M chars have been stripped. */
1594 limit = pkt + ((rs->remote_packet_size) - BUF_THREAD_ID_SIZE); /* done parse past here */
1595 pkt = unpack_byte (pkt, &count); /* count field */
1596 pkt = unpack_nibble (pkt, &done);
1597 /* The first threadid is the argument threadid. */
1598 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1599 while ((count-- > 0) && (pkt < limit))
1601 pkt = unpack_threadid (pkt, resultlist++);
1602 if (resultcount++ >= result_limit)
1611 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1612 int *done, int *result_count, threadref *threadlist)
1614 struct remote_state *rs = get_remote_state ();
1615 static threadref echo_nextthread;
1616 char *threadlist_packet = alloca (rs->remote_packet_size);
1617 char *t_response = alloca (rs->remote_packet_size);
1620 /* Trancate result limit to be smaller than the packet size */
1621 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= (rs->remote_packet_size))
1622 result_limit = ((rs->remote_packet_size) / BUF_THREAD_ID_SIZE) - 2;
1624 pack_threadlist_request (threadlist_packet,
1625 startflag, result_limit, nextthread);
1626 putpkt (threadlist_packet);
1627 getpkt (t_response, (rs->remote_packet_size), 0);
1630 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1633 if (!threadmatch (&echo_nextthread, nextthread))
1635 /* FIXME: This is a good reason to drop the packet */
1636 /* Possably, there is a duplicate response */
1638 retransmit immediatly - race conditions
1639 retransmit after timeout - yes
1641 wait for packet, then exit
1643 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1644 return 0; /* I choose simply exiting */
1646 if (*result_count <= 0)
1650 warning ("RMT ERROR : failed to get remote thread list\n");
1653 return result; /* break; */
1655 if (*result_count > result_limit)
1658 warning ("RMT ERROR: threadlist response longer than requested\n");
1664 /* This is the interface between remote and threads, remotes upper interface */
1666 /* remote_find_new_threads retrieves the thread list and for each
1667 thread in the list, looks up the thread in GDB's internal list,
1668 ading the thread if it does not already exist. This involves
1669 getting partial thread lists from the remote target so, polling the
1670 quit_flag is required. */
1673 /* About this many threadisds fit in a packet. */
1675 #define MAXTHREADLISTRESULTS 32
1678 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1681 int done, i, result_count;
1685 static threadref nextthread;
1686 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1691 if (loopcount++ > looplimit)
1694 warning ("Remote fetch threadlist -infinite loop-\n");
1697 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1698 &done, &result_count, resultthreadlist))
1703 /* clear for later iterations */
1705 /* Setup to resume next batch of thread references, set nextthread. */
1706 if (result_count >= 1)
1707 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1709 while (result_count--)
1710 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1717 remote_newthread_step (threadref *ref, void *context)
1721 ptid = pid_to_ptid (threadref_to_int (ref));
1723 if (!in_thread_list (ptid))
1725 return 1; /* continue iterator */
1728 #define CRAZY_MAX_THREADS 1000
1731 remote_current_thread (ptid_t oldpid)
1733 struct remote_state *rs = get_remote_state ();
1734 char *buf = alloca (rs->remote_packet_size);
1737 getpkt (buf, (rs->remote_packet_size), 0);
1738 if (buf[0] == 'Q' && buf[1] == 'C')
1739 /* Use strtoul here, so we'll correctly parse values whose highest
1740 bit is set. The protocol carries them as a simple series of
1741 hex digits; in the absence of a sign, strtol will see such
1742 values as positive numbers out of range for signed 'long', and
1743 return LONG_MAX to indicate an overflow. */
1744 return pid_to_ptid (strtoul (&buf[2], NULL, 16));
1749 /* Find new threads for info threads command.
1750 * Original version, using John Metzler's thread protocol.
1754 remote_find_new_threads (void)
1756 remote_threadlist_iterator (remote_newthread_step, 0,
1758 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1759 inferior_ptid = remote_current_thread (inferior_ptid);
1763 * Find all threads for info threads command.
1764 * Uses new thread protocol contributed by Cisco.
1765 * Falls back and attempts to use the older method (above)
1766 * if the target doesn't respond to the new method.
1770 remote_threads_info (void)
1772 struct remote_state *rs = get_remote_state ();
1773 char *buf = alloca (rs->remote_packet_size);
1777 if (remote_desc == 0) /* paranoia */
1778 error ("Command can only be used when connected to the remote target.");
1780 if (use_threadinfo_query)
1782 putpkt ("qfThreadInfo");
1784 getpkt (bufp, (rs->remote_packet_size), 0);
1785 if (bufp[0] != '\0') /* q packet recognized */
1787 while (*bufp++ == 'm') /* reply contains one or more TID */
1791 /* Use strtoul here, so we'll correctly parse values
1792 whose highest bit is set. The protocol carries
1793 them as a simple series of hex digits; in the
1794 absence of a sign, strtol will see such values as
1795 positive numbers out of range for signed 'long',
1796 and return LONG_MAX to indicate an overflow. */
1797 tid = strtoul (bufp, &bufp, 16);
1798 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1799 add_thread (pid_to_ptid (tid));
1801 while (*bufp++ == ','); /* comma-separated list */
1802 putpkt ("qsThreadInfo");
1804 getpkt (bufp, (rs->remote_packet_size), 0);
1810 /* Else fall back to old method based on jmetzler protocol. */
1811 use_threadinfo_query = 0;
1812 remote_find_new_threads ();
1817 * Collect a descriptive string about the given thread.
1818 * The target may say anything it wants to about the thread
1819 * (typically info about its blocked / runnable state, name, etc.).
1820 * This string will appear in the info threads display.
1822 * Optional: targets are not required to implement this function.
1826 remote_threads_extra_info (struct thread_info *tp)
1828 struct remote_state *rs = get_remote_state ();
1832 struct gdb_ext_thread_info threadinfo;
1833 static char display_buf[100]; /* arbitrary... */
1834 char *bufp = alloca (rs->remote_packet_size);
1835 int n = 0; /* position in display_buf */
1837 if (remote_desc == 0) /* paranoia */
1838 internal_error (__FILE__, __LINE__,
1839 "remote_threads_extra_info");
1841 if (use_threadextra_query)
1843 sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid));
1845 getpkt (bufp, (rs->remote_packet_size), 0);
1848 n = min (strlen (bufp) / 2, sizeof (display_buf));
1849 result = hex2bin (bufp, display_buf, n);
1850 display_buf [result] = '\0';
1855 /* If the above query fails, fall back to the old method. */
1856 use_threadextra_query = 0;
1857 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1858 | TAG_MOREDISPLAY | TAG_DISPLAY;
1859 int_to_threadref (&id, PIDGET (tp->ptid));
1860 if (remote_get_threadinfo (&id, set, &threadinfo))
1861 if (threadinfo.active)
1863 if (*threadinfo.shortname)
1864 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1865 if (*threadinfo.display)
1866 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1867 if (*threadinfo.more_display)
1868 n += sprintf(&display_buf[n], " Priority: %s",
1869 threadinfo.more_display);
1873 /* for purely cosmetic reasons, clear up trailing commas */
1874 if (',' == display_buf[n-1])
1875 display_buf[n-1] = ' ';
1884 /* Restart the remote side; this is an extended protocol operation. */
1887 extended_remote_restart (void)
1889 struct remote_state *rs = get_remote_state ();
1890 char *buf = alloca (rs->remote_packet_size);
1892 /* Send the restart command; for reasons I don't understand the
1893 remote side really expects a number after the "R". */
1895 sprintf (&buf[1], "%x", 0);
1898 /* Now query for status so this looks just like we restarted
1899 gdbserver from scratch. */
1901 getpkt (buf, (rs->remote_packet_size), 0);
1904 /* Clean up connection to a remote debugger. */
1907 remote_close (int quitting)
1910 serial_close (remote_desc);
1914 /* Query the remote side for the text, data and bss offsets. */
1919 struct remote_state *rs = get_remote_state ();
1920 char *buf = alloca (rs->remote_packet_size);
1923 CORE_ADDR text_addr, data_addr, bss_addr;
1924 struct section_offsets *offs;
1926 putpkt ("qOffsets");
1928 getpkt (buf, (rs->remote_packet_size), 0);
1930 if (buf[0] == '\000')
1931 return; /* Return silently. Stub doesn't support
1935 warning ("Remote failure reply: %s", buf);
1939 /* Pick up each field in turn. This used to be done with scanf, but
1940 scanf will make trouble if CORE_ADDR size doesn't match
1941 conversion directives correctly. The following code will work
1942 with any size of CORE_ADDR. */
1943 text_addr = data_addr = bss_addr = 0;
1947 if (strncmp (ptr, "Text=", 5) == 0)
1950 /* Don't use strtol, could lose on big values. */
1951 while (*ptr && *ptr != ';')
1952 text_addr = (text_addr << 4) + fromhex (*ptr++);
1957 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1960 while (*ptr && *ptr != ';')
1961 data_addr = (data_addr << 4) + fromhex (*ptr++);
1966 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1969 while (*ptr && *ptr != ';')
1970 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1976 error ("Malformed response to offset query, %s", buf);
1978 if (symfile_objfile == NULL)
1981 offs = ((struct section_offsets *)
1982 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
1983 memcpy (offs, symfile_objfile->section_offsets,
1984 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
1986 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
1988 /* This is a temporary kludge to force data and bss to use the same offsets
1989 because that's what nlmconv does now. The real solution requires changes
1990 to the stub and remote.c that I don't have time to do right now. */
1992 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1993 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
1995 objfile_relocate (symfile_objfile, offs);
1998 /* Stub for catch_errors. */
2001 remote_start_remote_dummy (struct ui_out *uiout, void *dummy)
2003 start_remote (); /* Initialize gdb process mechanisms */
2004 /* NOTE: Return something >=0. A -ve value is reserved for
2005 catch_exceptions. */
2010 remote_start_remote (struct ui_out *uiout, void *dummy)
2012 immediate_quit++; /* Allow user to interrupt it */
2014 /* Ack any packet which the remote side has already sent. */
2015 serial_write (remote_desc, "+", 1);
2017 /* Let the stub know that we want it to return the thread. */
2020 inferior_ptid = remote_current_thread (inferior_ptid);
2022 get_offsets (); /* Get text, data & bss offsets */
2024 putpkt ("?"); /* initiate a query from remote machine */
2027 /* NOTE: See comment above in remote_start_remote_dummy(). This
2028 function returns something >=0. */
2029 return remote_start_remote_dummy (uiout, dummy);
2032 /* Open a connection to a remote debugger.
2033 NAME is the filename used for communication. */
2036 remote_open (char *name, int from_tty)
2038 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
2041 /* Just like remote_open, but with asynchronous support. */
2043 remote_async_open (char *name, int from_tty)
2045 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
2048 /* Open a connection to a remote debugger using the extended
2049 remote gdb protocol. NAME is the filename used for communication. */
2052 extended_remote_open (char *name, int from_tty)
2054 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
2058 /* Just like extended_remote_open, but with asynchronous support. */
2060 extended_remote_async_open (char *name, int from_tty)
2062 remote_open_1 (name, from_tty, &extended_async_remote_ops,
2063 1 /*extended_p */, 1 /* async_p */);
2066 /* Generic code for opening a connection to a remote target. */
2069 init_all_packet_configs (void)
2072 update_packet_config (&remote_protocol_P);
2073 update_packet_config (&remote_protocol_p);
2074 update_packet_config (&remote_protocol_qSymbol);
2075 update_packet_config (&remote_protocol_vcont);
2076 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
2077 update_packet_config (&remote_protocol_Z[i]);
2078 /* Force remote_write_bytes to check whether target supports binary
2080 update_packet_config (&remote_protocol_binary_download);
2081 update_packet_config (&remote_protocol_qPart_auxv);
2084 /* Symbol look-up. */
2087 remote_check_symbols (struct objfile *objfile)
2089 struct remote_state *rs = get_remote_state ();
2090 char *msg, *reply, *tmp;
2091 struct minimal_symbol *sym;
2094 if (remote_protocol_qSymbol.support == PACKET_DISABLE)
2097 msg = alloca (rs->remote_packet_size);
2098 reply = alloca (rs->remote_packet_size);
2100 /* Invite target to request symbol lookups. */
2102 putpkt ("qSymbol::");
2103 getpkt (reply, (rs->remote_packet_size), 0);
2104 packet_ok (reply, &remote_protocol_qSymbol);
2106 while (strncmp (reply, "qSymbol:", 8) == 0)
2109 end = hex2bin (tmp, msg, strlen (tmp) / 2);
2111 sym = lookup_minimal_symbol (msg, NULL, NULL);
2113 sprintf (msg, "qSymbol::%s", &reply[8]);
2115 sprintf (msg, "qSymbol:%s:%s",
2116 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2119 getpkt (reply, (rs->remote_packet_size), 0);
2123 static struct serial *
2124 remote_serial_open (char *name)
2126 static int udp_warning = 0;
2128 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2129 of in ser-tcp.c, because it is the remote protocol assuming that the
2130 serial connection is reliable and not the serial connection promising
2132 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2134 warning ("The remote protocol may be unreliable over UDP.");
2135 warning ("Some events may be lost, rendering further debugging "
2140 return serial_open (name);
2144 remote_open_1 (char *name, int from_tty, struct target_ops *target,
2145 int extended_p, int async_p)
2148 struct remote_state *rs = get_remote_state ();
2150 error ("To open a remote debug connection, you need to specify what\n"
2151 "serial device is attached to the remote system\n"
2152 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2154 /* See FIXME above */
2156 wait_forever_enabled_p = 1;
2158 reopen_exec_file ();
2161 target_preopen (from_tty);
2163 unpush_target (target);
2165 remote_desc = remote_serial_open (name);
2167 perror_with_name (name);
2169 if (baud_rate != -1)
2171 if (serial_setbaudrate (remote_desc, baud_rate))
2173 /* The requested speed could not be set. Error out to
2174 top level after closing remote_desc. Take care to
2175 set remote_desc to NULL to avoid closing remote_desc
2177 serial_close (remote_desc);
2179 perror_with_name (name);
2183 serial_raw (remote_desc);
2185 /* If there is something sitting in the buffer we might take it as a
2186 response to a command, which would be bad. */
2187 serial_flush_input (remote_desc);
2191 puts_filtered ("Remote debugging using ");
2192 puts_filtered (name);
2193 puts_filtered ("\n");
2195 push_target (target); /* Switch to using remote target now */
2197 init_all_packet_configs ();
2199 general_thread = -2;
2200 continue_thread = -2;
2202 /* Probe for ability to use "ThreadInfo" query, as required. */
2203 use_threadinfo_query = 1;
2204 use_threadextra_query = 1;
2206 /* Without this, some commands which require an active target (such
2207 as kill) won't work. This variable serves (at least) double duty
2208 as both the pid of the target process (if it has such), and as a
2209 flag indicating that a target is active. These functions should
2210 be split out into seperate variables, especially since GDB will
2211 someday have a notion of debugging several processes. */
2213 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2217 /* With this target we start out by owning the terminal. */
2218 remote_async_terminal_ours_p = 1;
2220 /* FIXME: cagney/1999-09-23: During the initial connection it is
2221 assumed that the target is already ready and able to respond to
2222 requests. Unfortunately remote_start_remote() eventually calls
2223 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2224 around this. Eventually a mechanism that allows
2225 wait_for_inferior() to expect/get timeouts will be
2227 wait_forever_enabled_p = 0;
2230 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2231 /* First delete any symbols previously loaded from shared libraries. */
2232 no_shared_libraries (NULL, 0);
2235 /* Start the remote connection. If error() or QUIT, discard this
2236 target (we'd otherwise be in an inconsistent state) and then
2237 propogate the error on up the exception chain. This ensures that
2238 the caller doesn't stumble along blindly assuming that the
2239 function succeeded. The CLI doesn't have this problem but other
2240 UI's, such as MI do.
2242 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2243 this function should return an error indication letting the
2244 caller restore the previous state. Unfortunately the command
2245 ``target remote'' is directly wired to this function making that
2246 impossible. On a positive note, the CLI side of this problem has
2247 been fixed - the function set_cmd_context() makes it possible for
2248 all the ``target ....'' commands to share a common callback
2249 function. See cli-dump.c. */
2250 ex = catch_exceptions (uiout,
2251 remote_start_remote, NULL,
2252 "Couldn't establish connection to remote"
2259 wait_forever_enabled_p = 1;
2264 wait_forever_enabled_p = 1;
2268 /* Tell the remote that we are using the extended protocol. */
2269 char *buf = alloca (rs->remote_packet_size);
2271 getpkt (buf, (rs->remote_packet_size), 0);
2273 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2274 /* FIXME: need a master target_open vector from which all
2275 remote_opens can be called, so that stuff like this can
2276 go there. Failing that, the following code must be copied
2277 to the open function for any remote target that wants to
2278 support svr4 shared libraries. */
2280 /* Set up to detect and load shared libraries. */
2281 if (exec_bfd) /* No use without an exec file. */
2283 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2284 remote_check_symbols (symfile_objfile);
2289 /* This takes a program previously attached to and detaches it. After
2290 this is done, GDB can be used to debug some other program. We
2291 better not have left any breakpoints in the target program or it'll
2292 die when it hits one. */
2295 remote_detach (char *args, int from_tty)
2297 struct remote_state *rs = get_remote_state ();
2298 char *buf = alloca (rs->remote_packet_size);
2301 error ("Argument given to \"detach\" when remotely debugging.");
2303 /* Tell the remote target to detach. */
2305 remote_send (buf, (rs->remote_packet_size));
2307 /* Unregister the file descriptor from the event loop. */
2308 if (target_is_async_p ())
2309 serial_async (remote_desc, NULL, 0);
2311 target_mourn_inferior ();
2313 puts_filtered ("Ending remote debugging.\n");
2316 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2319 remote_disconnect (char *args, int from_tty)
2321 struct remote_state *rs = get_remote_state ();
2322 char *buf = alloca (rs->remote_packet_size);
2325 error ("Argument given to \"detach\" when remotely debugging.");
2327 /* Unregister the file descriptor from the event loop. */
2328 if (target_is_async_p ())
2329 serial_async (remote_desc, NULL, 0);
2331 target_mourn_inferior ();
2333 puts_filtered ("Ending remote debugging.\n");
2336 /* Convert hex digit A to a number. */
2341 if (a >= '0' && a <= '9')
2343 else if (a >= 'a' && a <= 'f')
2344 return a - 'a' + 10;
2345 else if (a >= 'A' && a <= 'F')
2346 return a - 'A' + 10;
2348 error ("Reply contains invalid hex digit %d", a);
2352 hex2bin (const char *hex, char *bin, int count)
2356 for (i = 0; i < count; i++)
2358 if (hex[0] == 0 || hex[1] == 0)
2360 /* Hex string is short, or of uneven length.
2361 Return the count that has been converted so far. */
2364 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2370 /* Convert number NIB to a hex digit. */
2378 return 'a' + nib - 10;
2382 bin2hex (const char *bin, char *hex, int count)
2385 /* May use a length, or a nul-terminated string as input. */
2387 count = strlen (bin);
2389 for (i = 0; i < count; i++)
2391 *hex++ = tohex ((*bin >> 4) & 0xf);
2392 *hex++ = tohex (*bin++ & 0xf);
2398 /* Check for the availability of vCont. This function should also check
2402 remote_vcont_probe (struct remote_state *rs, char *buf)
2404 strcpy (buf, "vCont?");
2406 getpkt (buf, rs->remote_packet_size, 0);
2408 /* Make sure that the features we assume are supported. */
2409 if (strncmp (buf, "vCont", 5) == 0)
2412 int support_s, support_S, support_c, support_C;
2418 while (p && *p == ';')
2421 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2423 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2425 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2427 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2430 p = strchr (p, ';');
2433 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2434 BUF will make packet_ok disable the packet. */
2435 if (!support_s || !support_S || !support_c || !support_C)
2439 packet_ok (buf, &remote_protocol_vcont);
2442 /* Resume the remote inferior by using a "vCont" packet. The thread
2443 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2444 resumed thread should be single-stepped and/or signalled. If PTID's
2445 PID is -1, then all threads are resumed; the thread to be stepped and/or
2446 signalled is given in the global INFERIOR_PTID. This function returns
2447 non-zero iff it resumes the inferior.
2449 This function issues a strict subset of all possible vCont commands at the
2453 remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2455 struct remote_state *rs = get_remote_state ();
2456 int pid = PIDGET (ptid);
2457 char *buf = NULL, *outbuf;
2458 struct cleanup *old_cleanup;
2460 buf = xmalloc (rs->remote_packet_size);
2461 old_cleanup = make_cleanup (xfree, buf);
2463 if (remote_protocol_vcont.support == PACKET_SUPPORT_UNKNOWN)
2464 remote_vcont_probe (rs, buf);
2466 if (remote_protocol_vcont.support == PACKET_DISABLE)
2468 do_cleanups (old_cleanup);
2472 /* If we could generate a wider range of packets, we'd have to worry
2473 about overflowing BUF. Should there be a generic
2474 "multi-part-packet" packet? */
2476 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
2478 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2479 don't have any PID numbers the inferior will understand. Make sure
2480 to only send forms that do not specify a PID. */
2481 if (step && siggnal != TARGET_SIGNAL_0)
2482 outbuf = xstrprintf ("vCont;S%02x", siggnal);
2484 outbuf = xstrprintf ("vCont;s");
2485 else if (siggnal != TARGET_SIGNAL_0)
2486 outbuf = xstrprintf ("vCont;C%02x", siggnal);
2488 outbuf = xstrprintf ("vCont;c");
2492 /* Resume all threads, with preference for INFERIOR_PTID. */
2493 if (step && siggnal != TARGET_SIGNAL_0)
2494 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2495 PIDGET (inferior_ptid));
2497 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
2498 else if (siggnal != TARGET_SIGNAL_0)
2499 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2500 PIDGET (inferior_ptid));
2502 outbuf = xstrprintf ("vCont;c");
2506 /* Scheduler locking; resume only PTID. */
2507 if (step && siggnal != TARGET_SIGNAL_0)
2508 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
2510 outbuf = xstrprintf ("vCont;s:%x", pid);
2511 else if (siggnal != TARGET_SIGNAL_0)
2512 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
2514 outbuf = xstrprintf ("vCont;c:%x", pid);
2517 gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size);
2518 make_cleanup (xfree, outbuf);
2522 do_cleanups (old_cleanup);
2527 /* Tell the remote machine to resume. */
2529 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2531 static int last_sent_step;
2534 remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
2536 struct remote_state *rs = get_remote_state ();
2537 char *buf = alloca (rs->remote_packet_size);
2538 int pid = PIDGET (ptid);
2541 last_sent_signal = siggnal;
2542 last_sent_step = step;
2544 /* A hook for when we need to do something at the last moment before
2546 if (deprecated_target_resume_hook)
2547 (*deprecated_target_resume_hook) ();
2549 /* The vCont packet doesn't need to specify threads via Hc. */
2550 if (remote_vcont_resume (ptid, step, siggnal))
2553 /* All other supported resume packets do use Hc, so call set_thread. */
2555 set_thread (0, 0); /* run any thread */
2557 set_thread (pid, 0); /* run this thread */
2559 if (siggnal != TARGET_SIGNAL_0)
2561 buf[0] = step ? 'S' : 'C';
2562 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2563 buf[2] = tohex (((int) siggnal) & 0xf);
2567 strcpy (buf, step ? "s" : "c");
2572 /* Same as remote_resume, but with async support. */
2574 remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2576 remote_resume (ptid, step, siggnal);
2578 /* We are about to start executing the inferior, let's register it
2579 with the event loop. NOTE: this is the one place where all the
2580 execution commands end up. We could alternatively do this in each
2581 of the execution commands in infcmd.c.*/
2582 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2583 into infcmd.c in order to allow inferior function calls to work
2584 NOT asynchronously. */
2585 if (target_can_async_p ())
2586 target_async (inferior_event_handler, 0);
2587 /* Tell the world that the target is now executing. */
2588 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2589 this? Instead, should the client of target just assume (for
2590 async targets) that the target is going to start executing? Is
2591 this information already found in the continuation block? */
2592 if (target_is_async_p ())
2593 target_executing = 1;
2597 /* Set up the signal handler for SIGINT, while the target is
2598 executing, ovewriting the 'regular' SIGINT signal handler. */
2600 initialize_sigint_signal_handler (void)
2602 sigint_remote_token =
2603 create_async_signal_handler (async_remote_interrupt, NULL);
2604 signal (SIGINT, handle_remote_sigint);
2607 /* Signal handler for SIGINT, while the target is executing. */
2609 handle_remote_sigint (int sig)
2611 signal (sig, handle_remote_sigint_twice);
2612 sigint_remote_twice_token =
2613 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2614 mark_async_signal_handler_wrapper (sigint_remote_token);
2617 /* Signal handler for SIGINT, installed after SIGINT has already been
2618 sent once. It will take effect the second time that the user sends
2621 handle_remote_sigint_twice (int sig)
2623 signal (sig, handle_sigint);
2624 sigint_remote_twice_token =
2625 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2626 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2629 /* Perform the real interruption of the target execution, in response
2632 async_remote_interrupt (gdb_client_data arg)
2635 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2640 /* Perform interrupt, if the first attempt did not succeed. Just give
2641 up on the target alltogether. */
2643 async_remote_interrupt_twice (gdb_client_data arg)
2646 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2647 /* Do something only if the target was not killed by the previous
2649 if (target_executing)
2652 signal (SIGINT, handle_remote_sigint);
2656 /* Reinstall the usual SIGINT handlers, after the target has
2659 cleanup_sigint_signal_handler (void *dummy)
2661 signal (SIGINT, handle_sigint);
2662 if (sigint_remote_twice_token)
2663 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
2664 if (sigint_remote_token)
2665 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
2668 /* Send ^C to target to halt it. Target will respond, and send us a
2670 static void (*ofunc) (int);
2672 /* The command line interface's stop routine. This function is installed
2673 as a signal handler for SIGINT. The first time a user requests a
2674 stop, we call remote_stop to send a break or ^C. If there is no
2675 response from the target (it didn't stop when the user requested it),
2676 we ask the user if he'd like to detach from the target. */
2678 remote_interrupt (int signo)
2680 /* If this doesn't work, try more severe steps. */
2681 signal (signo, remote_interrupt_twice);
2684 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2689 /* The user typed ^C twice. */
2692 remote_interrupt_twice (int signo)
2694 signal (signo, ofunc);
2696 signal (signo, remote_interrupt);
2699 /* This is the generic stop called via the target vector. When a target
2700 interrupt is requested, either by the command line or the GUI, we
2701 will eventually end up here. */
2705 /* Send a break or a ^C, depending on user preference. */
2707 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2710 serial_send_break (remote_desc);
2712 serial_write (remote_desc, "\003", 1);
2715 /* Ask the user what to do when an interrupt is received. */
2718 interrupt_query (void)
2720 target_terminal_ours ();
2722 if (query ("Interrupted while waiting for the program.\n\
2723 Give up (and stop debugging it)? "))
2725 target_mourn_inferior ();
2726 throw_reason (RETURN_QUIT);
2729 target_terminal_inferior ();
2732 /* Enable/disable target terminal ownership. Most targets can use
2733 terminal groups to control terminal ownership. Remote targets are
2734 different in that explicit transfer of ownership to/from GDB/target
2738 remote_async_terminal_inferior (void)
2740 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2741 sync_execution here. This function should only be called when
2742 GDB is resuming the inferior in the forground. A background
2743 resume (``run&'') should leave GDB in control of the terminal and
2744 consequently should not call this code. */
2745 if (!sync_execution)
2747 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2748 calls target_terminal_*() idenpotent. The event-loop GDB talking
2749 to an asynchronous target with a synchronous command calls this
2750 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2751 stops trying to transfer the terminal to the target when it
2752 shouldn't this guard can go away. */
2753 if (!remote_async_terminal_ours_p)
2755 delete_file_handler (input_fd);
2756 remote_async_terminal_ours_p = 0;
2757 initialize_sigint_signal_handler ();
2758 /* NOTE: At this point we could also register our selves as the
2759 recipient of all input. Any characters typed could then be
2760 passed on down to the target. */
2764 remote_async_terminal_ours (void)
2766 /* See FIXME in remote_async_terminal_inferior. */
2767 if (!sync_execution)
2769 /* See FIXME in remote_async_terminal_inferior. */
2770 if (remote_async_terminal_ours_p)
2772 cleanup_sigint_signal_handler (NULL);
2773 add_file_handler (input_fd, stdin_event_handler, 0);
2774 remote_async_terminal_ours_p = 1;
2777 /* If nonzero, ignore the next kill. */
2782 remote_console_output (char *msg)
2786 for (p = msg; p[0] && p[1]; p += 2)
2789 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2792 fputs_unfiltered (tb, gdb_stdtarg);
2794 gdb_flush (gdb_stdtarg);
2797 /* Wait until the remote machine stops, then return,
2798 storing status in STATUS just as `wait' would.
2799 Returns "pid", which in the case of a multi-threaded
2800 remote OS, is the thread-id. */
2803 remote_wait (ptid_t ptid, struct target_waitstatus *status)
2805 struct remote_state *rs = get_remote_state ();
2806 unsigned char *buf = alloca (rs->remote_packet_size);
2807 ULONGEST thread_num = -1;
2810 status->kind = TARGET_WAITKIND_EXITED;
2811 status->value.integer = 0;
2817 ofunc = signal (SIGINT, remote_interrupt);
2818 getpkt (buf, (rs->remote_packet_size), 1);
2819 signal (SIGINT, ofunc);
2821 /* This is a hook for when we need to do something (perhaps the
2822 collection of trace data) every time the target stops. */
2823 if (deprecated_target_wait_loop_hook)
2824 (*deprecated_target_wait_loop_hook) ();
2826 remote_stopped_by_watchpoint_p = 0;
2830 case 'E': /* Error of some sort */
2831 warning ("Remote failure reply: %s", buf);
2833 case 'F': /* File-I/O request */
2834 remote_fileio_request (buf);
2836 case 'T': /* Status with PC, SP, FP, ... */
2839 char regs[MAX_REGISTER_SIZE];
2841 /* Expedited reply, containing Signal, {regno, reg} repeat */
2842 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2844 n... = register number
2845 r... = register contents
2847 p = &buf[3]; /* after Txx */
2856 /* If the packet contains a register number save it in pnum
2857 and set p1 to point to the character following it.
2858 Otherwise p1 points to p. */
2860 /* If this packet is an awatch packet, don't parse the 'a'
2861 as a register number. */
2863 if (strncmp (p, "awatch", strlen("awatch")) != 0)
2865 /* Read the ``P'' register number. */
2866 pnum = strtol (p, &p_temp, 16);
2867 p1 = (unsigned char *) p_temp;
2872 if (p1 == p) /* No register number present here */
2874 p1 = (unsigned char *) strchr (p, ':');
2876 warning ("Malformed packet(a) (missing colon): %s\n\
2879 if (strncmp (p, "thread", p1 - p) == 0)
2881 p_temp = unpack_varlen_hex (++p1, &thread_num);
2882 record_currthread (thread_num);
2883 p = (unsigned char *) p_temp;
2885 else if ((strncmp (p, "watch", p1 - p) == 0)
2886 || (strncmp (p, "rwatch", p1 - p) == 0)
2887 || (strncmp (p, "awatch", p1 - p) == 0))
2889 remote_stopped_by_watchpoint_p = 1;
2890 p = unpack_varlen_hex (++p1, &addr);
2891 remote_watch_data_address = (CORE_ADDR)addr;
2895 /* Silently skip unknown optional info. */
2896 p_temp = strchr (p1 + 1, ';');
2898 p = (unsigned char *) p_temp;
2903 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
2907 error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
2911 error ("Remote sent bad register number %s: %s\nPacket: '%s'\n",
2912 phex_nz (pnum, 0), p, buf);
2914 fieldsize = hex2bin (p, regs, register_size (current_gdbarch, reg->regnum));
2916 if (fieldsize < register_size (current_gdbarch, reg->regnum))
2917 warning ("Remote reply is too short: %s", buf);
2918 regcache_raw_supply (current_regcache, reg->regnum, regs);
2922 error ("Remote register badly formatted: %s\nhere: %s", buf, p);
2926 case 'S': /* Old style status, just signal only */
2927 status->kind = TARGET_WAITKIND_STOPPED;
2928 status->value.sig = (enum target_signal)
2929 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2933 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2934 record_currthread (thread_num);
2937 case 'W': /* Target exited */
2939 /* The remote process exited. */
2940 status->kind = TARGET_WAITKIND_EXITED;
2941 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2945 status->kind = TARGET_WAITKIND_SIGNALLED;
2946 status->value.sig = (enum target_signal)
2947 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2951 case 'O': /* Console output */
2952 remote_console_output (buf + 1);
2955 if (last_sent_signal != TARGET_SIGNAL_0)
2957 /* Zero length reply means that we tried 'S' or 'C' and
2958 the remote system doesn't support it. */
2959 target_terminal_ours_for_output ();
2961 ("Can't send signals to this remote system. %s not sent.\n",
2962 target_signal_to_name (last_sent_signal));
2963 last_sent_signal = TARGET_SIGNAL_0;
2964 target_terminal_inferior ();
2966 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2967 putpkt ((char *) buf);
2970 /* else fallthrough */
2972 warning ("Invalid remote reply: %s", buf);
2977 if (thread_num != -1)
2979 return pid_to_ptid (thread_num);
2981 return inferior_ptid;
2984 /* Async version of remote_wait. */
2986 remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
2988 struct remote_state *rs = get_remote_state ();
2989 unsigned char *buf = alloca (rs->remote_packet_size);
2990 ULONGEST thread_num = -1;
2993 status->kind = TARGET_WAITKIND_EXITED;
2994 status->value.integer = 0;
2996 remote_stopped_by_watchpoint_p = 0;
3002 if (!target_is_async_p ())
3003 ofunc = signal (SIGINT, remote_interrupt);
3004 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3005 _never_ wait for ever -> test on target_is_async_p().
3006 However, before we do that we need to ensure that the caller
3007 knows how to take the target into/out of async mode. */
3008 getpkt (buf, (rs->remote_packet_size), wait_forever_enabled_p);
3009 if (!target_is_async_p ())
3010 signal (SIGINT, ofunc);
3012 /* This is a hook for when we need to do something (perhaps the
3013 collection of trace data) every time the target stops. */
3014 if (deprecated_target_wait_loop_hook)
3015 (*deprecated_target_wait_loop_hook) ();
3019 case 'E': /* Error of some sort */
3020 warning ("Remote failure reply: %s", buf);
3022 case 'F': /* File-I/O request */
3023 remote_fileio_request (buf);
3025 case 'T': /* Status with PC, SP, FP, ... */
3028 char regs[MAX_REGISTER_SIZE];
3030 /* Expedited reply, containing Signal, {regno, reg} repeat */
3031 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3033 n... = register number
3034 r... = register contents
3036 p = &buf[3]; /* after Txx */
3045 /* If the packet contains a register number, save it in pnum
3046 and set p1 to point to the character following it.
3047 Otherwise p1 points to p. */
3049 /* If this packet is an awatch packet, don't parse the 'a'
3050 as a register number. */
3052 if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
3054 /* Read the register number. */
3055 pnum = strtol (p, &p_temp, 16);
3056 p1 = (unsigned char *) p_temp;
3061 if (p1 == p) /* No register number present here */
3063 p1 = (unsigned char *) strchr (p, ':');
3065 error ("Malformed packet(a) (missing colon): %s\nPacket: '%s'\n",
3067 if (strncmp (p, "thread", p1 - p) == 0)
3069 p_temp = unpack_varlen_hex (++p1, &thread_num);
3070 record_currthread (thread_num);
3071 p = (unsigned char *) p_temp;
3073 else if ((strncmp (p, "watch", p1 - p) == 0)
3074 || (strncmp (p, "rwatch", p1 - p) == 0)
3075 || (strncmp (p, "awatch", p1 - p) == 0))
3077 remote_stopped_by_watchpoint_p = 1;
3078 p = unpack_varlen_hex (++p1, &addr);
3079 remote_watch_data_address = (CORE_ADDR)addr;
3083 /* Silently skip unknown optional info. */
3084 p_temp = (unsigned char *) strchr (p1 + 1, ';');
3092 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
3095 error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
3099 error ("Remote sent bad register number %ld: %s\nPacket: '%s'\n",
3102 fieldsize = hex2bin (p, regs, register_size (current_gdbarch, reg->regnum));
3104 if (fieldsize < register_size (current_gdbarch, reg->regnum))
3105 warning ("Remote reply is too short: %s", buf);
3106 regcache_raw_supply (current_regcache, reg->regnum, regs);
3110 error ("Remote register badly formatted: %s\nhere: %s",
3115 case 'S': /* Old style status, just signal only */
3116 status->kind = TARGET_WAITKIND_STOPPED;
3117 status->value.sig = (enum target_signal)
3118 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3122 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3123 record_currthread (thread_num);
3126 case 'W': /* Target exited */
3128 /* The remote process exited. */
3129 status->kind = TARGET_WAITKIND_EXITED;
3130 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3134 status->kind = TARGET_WAITKIND_SIGNALLED;
3135 status->value.sig = (enum target_signal)
3136 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3140 case 'O': /* Console output */
3141 remote_console_output (buf + 1);
3142 /* Return immediately to the event loop. The event loop will
3143 still be waiting on the inferior afterwards. */
3144 status->kind = TARGET_WAITKIND_IGNORE;
3147 if (last_sent_signal != TARGET_SIGNAL_0)
3149 /* Zero length reply means that we tried 'S' or 'C' and
3150 the remote system doesn't support it. */
3151 target_terminal_ours_for_output ();
3153 ("Can't send signals to this remote system. %s not sent.\n",
3154 target_signal_to_name (last_sent_signal));
3155 last_sent_signal = TARGET_SIGNAL_0;
3156 target_terminal_inferior ();
3158 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3159 putpkt ((char *) buf);
3162 /* else fallthrough */
3164 warning ("Invalid remote reply: %s", buf);
3169 if (thread_num != -1)
3171 return pid_to_ptid (thread_num);
3173 return inferior_ptid;
3176 /* Number of bytes of registers this stub implements. */
3178 static int register_bytes_found;
3180 /* Read the remote registers into the block REGS. */
3181 /* Currently we just read all the registers, so we don't use regnum. */
3184 fetch_register_using_p (int regnum)
3186 struct remote_state *rs = get_remote_state ();
3187 char *buf = alloca (rs->remote_packet_size), *p;
3188 char regp[MAX_REGISTER_SIZE];
3193 p += hexnumstr (p, regnum);
3195 remote_send (buf, rs->remote_packet_size);
3197 /* If the stub didn't recognize the packet, or if we got an error,
3199 if (buf[0] == '\0' || buf[0] == 'E')
3202 /* If this register is unfetchable, tell the regcache. */
3205 regcache_raw_supply (current_regcache, regnum, NULL);
3206 set_register_cached (regnum, -1);
3210 /* Otherwise, parse and supply the value. */
3217 error("fetch_register_using_p: early buf termination");
3221 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3224 regcache_raw_supply (current_regcache, regnum, regp);
3229 remote_fetch_registers (int regnum)
3231 struct remote_state *rs = get_remote_state ();
3232 char *buf = alloca (rs->remote_packet_size);
3235 char *regs = alloca (rs->sizeof_g_packet);
3237 set_thread (PIDGET (inferior_ptid), 1);
3241 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3242 gdb_assert (reg != NULL);
3243 if (!reg->in_g_packet)
3244 internal_error (__FILE__, __LINE__,
3245 "Attempt to fetch a non G-packet register when this "
3246 "remote.c does not support the p-packet.");
3248 switch (remote_protocol_p.support)
3250 case PACKET_DISABLE:
3253 if (fetch_register_using_p (regnum))
3256 error ("Protocol error: p packet not recognized by stub");
3257 case PACKET_SUPPORT_UNKNOWN:
3258 if (fetch_register_using_p (regnum))
3260 /* The stub recognized the 'p' packet. Remember this. */
3261 remote_protocol_p.support = PACKET_ENABLE;
3266 /* The stub does not support the 'P' packet. Use 'G'
3267 instead, and don't try using 'P' in the future (it
3268 will just waste our time). */
3269 remote_protocol_p.support = PACKET_DISABLE;
3275 remote_send (buf, (rs->remote_packet_size));
3277 /* Save the size of the packet sent to us by the target. Its used
3278 as a heuristic when determining the max size of packets that the
3279 target can safely receive. */
3280 if ((rs->actual_register_packet_size) == 0)
3281 (rs->actual_register_packet_size) = strlen (buf);
3283 /* Unimplemented registers read as all bits zero. */
3284 memset (regs, 0, rs->sizeof_g_packet);
3286 /* We can get out of synch in various cases. If the first character
3287 in the buffer is not a hex character, assume that has happened
3288 and try to fetch another packet to read. */
3289 while ((buf[0] < '0' || buf[0] > '9')
3290 && (buf[0] < 'a' || buf[0] > 'f')
3291 && buf[0] != 'x') /* New: unavailable register value */
3294 fprintf_unfiltered (gdb_stdlog,
3295 "Bad register packet; fetching a new packet\n");
3296 getpkt (buf, (rs->remote_packet_size), 0);
3299 /* Reply describes registers byte by byte, each byte encoded as two
3300 hex characters. Suck them all up, then supply them to the
3301 register cacheing/storage mechanism. */
3304 for (i = 0; i < rs->sizeof_g_packet; i++)
3310 warning ("Remote reply is of odd length: %s", buf);
3311 /* Don't change register_bytes_found in this case, and don't
3312 print a second warning. */
3315 if (p[0] == 'x' && p[1] == 'x')
3316 regs[i] = 0; /* 'x' */
3318 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3322 if (i != register_bytes_found)
3324 register_bytes_found = i;
3325 if (REGISTER_BYTES_OK_P ()
3326 && !REGISTER_BYTES_OK (i))
3327 warning ("Remote reply is too short: %s", buf);
3333 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
3335 struct packet_reg *r = &rs->regs[i];
3338 if (r->offset * 2 >= strlen (buf))
3339 /* A short packet that didn't include the register's
3340 value, this implies that the register is zero (and
3341 not that the register is unavailable). Supply that
3343 regcache_raw_supply (current_regcache, r->regnum, NULL);
3344 else if (buf[r->offset * 2] == 'x')
3346 gdb_assert (r->offset * 2 < strlen (buf));
3347 /* The register isn't available, mark it as such (at
3348 the same time setting the value to zero). */
3349 regcache_raw_supply (current_regcache, r->regnum, NULL);
3350 set_register_cached (i, -1);
3353 regcache_raw_supply (current_regcache, r->regnum,
3360 /* Prepare to store registers. Since we may send them all (using a
3361 'G' request), we have to read out the ones we don't want to change
3365 remote_prepare_to_store (void)
3367 struct remote_state *rs = get_remote_state ();
3369 char buf[MAX_REGISTER_SIZE];
3371 /* Make sure the entire registers array is valid. */
3372 switch (remote_protocol_P.support)
3374 case PACKET_DISABLE:
3375 case PACKET_SUPPORT_UNKNOWN:
3376 /* Make sure all the necessary registers are cached. */
3377 for (i = 0; i < NUM_REGS; i++)
3378 if (rs->regs[i].in_g_packet)
3379 regcache_raw_read (current_regcache, rs->regs[i].regnum, buf);
3386 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3387 packet was not recognized. */
3390 store_register_using_P (int regnum)
3392 struct remote_state *rs = get_remote_state ();
3393 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3394 /* Try storing a single register. */
3395 char *buf = alloca (rs->remote_packet_size);
3396 char regp[MAX_REGISTER_SIZE];
3400 sprintf (buf, "P%s=", phex_nz (reg->pnum, 0));
3401 p = buf + strlen (buf);
3402 regcache_raw_collect (current_regcache, reg->regnum, regp);
3403 bin2hex (regp, p, register_size (current_gdbarch, reg->regnum));
3404 remote_send (buf, rs->remote_packet_size);
3406 return buf[0] != '\0';
3410 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
3411 of the register cache buffer. FIXME: ignores errors. */
3414 remote_store_registers (int regnum)
3416 struct remote_state *rs = get_remote_state ();
3422 set_thread (PIDGET (inferior_ptid), 1);
3426 switch (remote_protocol_P.support)
3428 case PACKET_DISABLE:
3431 if (store_register_using_P (regnum))
3434 error ("Protocol error: P packet not recognized by stub");
3435 case PACKET_SUPPORT_UNKNOWN:
3436 if (store_register_using_P (regnum))
3438 /* The stub recognized the 'P' packet. Remember this. */
3439 remote_protocol_P.support = PACKET_ENABLE;
3444 /* The stub does not support the 'P' packet. Use 'G'
3445 instead, and don't try using 'P' in the future (it
3446 will just waste our time). */
3447 remote_protocol_P.support = PACKET_DISABLE;
3453 /* Extract all the registers in the regcache copying them into a
3457 regs = alloca (rs->sizeof_g_packet);
3458 memset (regs, rs->sizeof_g_packet, 0);
3459 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
3461 struct packet_reg *r = &rs->regs[i];
3463 regcache_raw_collect (current_regcache, r->regnum, regs + r->offset);
3467 /* Command describes registers byte by byte,
3468 each byte encoded as two hex characters. */
3469 buf = alloca (rs->remote_packet_size);
3472 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3473 bin2hex (regs, p, register_bytes_found);
3474 remote_send (buf, (rs->remote_packet_size));
3478 /* Return the number of hex digits in num. */
3481 hexnumlen (ULONGEST num)
3485 for (i = 0; num != 0; i++)
3491 /* Set BUF to the minimum number of hex digits representing NUM. */
3494 hexnumstr (char *buf, ULONGEST num)
3496 int len = hexnumlen (num);
3497 return hexnumnstr (buf, num, len);
3501 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3504 hexnumnstr (char *buf, ULONGEST num, int width)
3510 for (i = width - 1; i >= 0; i--)
3512 buf[i] = "0123456789abcdef"[(num & 0xf)];
3519 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3522 remote_address_masked (CORE_ADDR addr)
3524 if (remote_address_size > 0
3525 && remote_address_size < (sizeof (ULONGEST) * 8))
3527 /* Only create a mask when that mask can safely be constructed
3528 in a ULONGEST variable. */
3530 mask = (mask << remote_address_size) - 1;
3536 /* Determine whether the remote target supports binary downloading.
3537 This is accomplished by sending a no-op memory write of zero length
3538 to the target at the specified address. It does not suffice to send
3539 the whole packet, since many stubs strip the eighth bit and subsequently
3540 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3542 NOTE: This can still lose if the serial line is not eight-bit
3543 clean. In cases like this, the user should clear "remote
3547 check_binary_download (CORE_ADDR addr)
3549 struct remote_state *rs = get_remote_state ();
3550 switch (remote_protocol_binary_download.support)
3552 case PACKET_DISABLE:
3556 case PACKET_SUPPORT_UNKNOWN:
3558 char *buf = alloca (rs->remote_packet_size);
3563 p += hexnumstr (p, (ULONGEST) addr);
3565 p += hexnumstr (p, (ULONGEST) 0);
3569 putpkt_binary (buf, (int) (p - buf));
3570 getpkt (buf, (rs->remote_packet_size), 0);
3575 fprintf_unfiltered (gdb_stdlog,
3576 "binary downloading NOT suppported by target\n");
3577 remote_protocol_binary_download.support = PACKET_DISABLE;
3582 fprintf_unfiltered (gdb_stdlog,
3583 "binary downloading suppported by target\n");
3584 remote_protocol_binary_download.support = PACKET_ENABLE;
3591 /* Write memory data directly to the remote machine.
3592 This does not inform the data cache; the data cache uses this.
3593 MEMADDR is the address in the remote memory space.
3594 MYADDR is the address of the buffer in our space.
3595 LEN is the number of bytes.
3597 Returns number of bytes transferred, or 0 (setting errno) for
3598 error. Only transfer a single packet. */
3601 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3605 unsigned char *plen;
3611 unsigned char *payload_start;
3613 /* Verify that the target can support a binary download. */
3614 check_binary_download (memaddr);
3616 /* Compute the size, and then allocate space for the largest
3617 possible packet. Include space for an extra trailing NUL. */
3618 sizeof_buf = get_memory_write_packet_size () + 1;
3619 buf = alloca (sizeof_buf);
3621 /* Compute the size of the actual payload by subtracting out the
3622 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
3623 payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
3624 + hexnumlen (memaddr)
3625 + hexnumlen (len)));
3627 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3629 /* Append "[XM]". Compute a best guess of the number of bytes
3630 actually transfered. */
3632 switch (remote_protocol_binary_download.support)
3636 /* Best guess at number of bytes that will fit. */
3637 todo = min (len, payload_size);
3639 case PACKET_DISABLE:
3641 /* num bytes that will fit */
3642 todo = min (len, payload_size / 2);
3644 case PACKET_SUPPORT_UNKNOWN:
3645 internal_error (__FILE__, __LINE__,
3646 "remote_write_bytes: bad internal state");
3648 internal_error (__FILE__, __LINE__, "bad switch");
3651 /* Append "<memaddr>". */
3652 memaddr = remote_address_masked (memaddr);
3653 p += hexnumstr (p, (ULONGEST) memaddr);
3658 /* Append <len>. Retain the location/size of <len>. It may need to
3659 be adjusted once the packet body has been created. */
3661 plenlen = hexnumstr (p, (ULONGEST) todo);
3668 /* Append the packet body. */
3670 switch (remote_protocol_binary_download.support)
3673 /* Binary mode. Send target system values byte by byte, in
3674 increasing byte addresses. Only escape certain critical
3677 (nr_bytes < todo) && (p - payload_start) < payload_size;
3680 switch (myaddr[nr_bytes] & 0xff)
3685 /* These must be escaped */
3687 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3690 *p++ = myaddr[nr_bytes] & 0xff;
3694 if (nr_bytes < todo)
3696 /* Escape chars have filled up the buffer prematurely,
3697 and we have actually sent fewer bytes than planned.
3698 Fix-up the length field of the packet. Use the same
3699 number of characters as before. */
3700 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3701 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3704 case PACKET_DISABLE:
3705 /* Normal mode: Send target system values byte by byte, in
3706 increasing byte addresses. Each byte is encoded as a two hex
3708 nr_bytes = bin2hex (myaddr, p, todo);
3711 case PACKET_SUPPORT_UNKNOWN:
3712 internal_error (__FILE__, __LINE__,
3713 "remote_write_bytes: bad internal state");
3715 internal_error (__FILE__, __LINE__, "bad switch");
3718 putpkt_binary (buf, (int) (p - buf));
3719 getpkt (buf, sizeof_buf, 0);
3723 /* There is no correspondance between what the remote protocol
3724 uses for errors and errno codes. We would like a cleaner way
3725 of representing errors (big enough to include errno codes,
3726 bfd_error codes, and others). But for now just return EIO. */
3731 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3732 bytes than we'd planned. */
3736 /* Read memory data directly from the remote machine.
3737 This does not use the data cache; the data cache uses this.
3738 MEMADDR is the address in the remote memory space.
3739 MYADDR is the address of the buffer in our space.
3740 LEN is the number of bytes.
3742 Returns number of bytes transferred, or 0 for error. */
3744 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3745 remote targets) shouldn't attempt to read the entire buffer.
3746 Instead it should read a single packet worth of data and then
3747 return the byte size of that packet to the caller. The caller (its
3748 caller and its callers caller ;-) already contains code for
3749 handling partial reads. */
3752 remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3755 int max_buf_size; /* Max size of packet output buffer */
3759 /* Create a buffer big enough for this packet. */
3760 max_buf_size = get_memory_read_packet_size ();
3761 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3762 buf = alloca (sizeof_buf);
3771 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3773 /* construct "m"<memaddr>","<len>" */
3774 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3775 memaddr = remote_address_masked (memaddr);
3778 p += hexnumstr (p, (ULONGEST) memaddr);
3780 p += hexnumstr (p, (ULONGEST) todo);
3784 getpkt (buf, sizeof_buf, 0);
3787 && isxdigit (buf[1]) && isxdigit (buf[2])
3790 /* There is no correspondance between what the remote protocol uses
3791 for errors and errno codes. We would like a cleaner way of
3792 representing errors (big enough to include errno codes, bfd_error
3793 codes, and others). But for now just return EIO. */
3798 /* Reply describes memory byte by byte,
3799 each byte encoded as two hex characters. */
3802 if ((i = hex2bin (p, myaddr, todo)) < todo)
3804 /* Reply is short. This means that we were able to read
3805 only part of what we wanted to. */
3806 return i + (origlen - len);
3815 /* Read or write LEN bytes from inferior memory at MEMADDR,
3816 transferring to or from debugger address BUFFER. Write to inferior if
3817 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3818 for error. TARGET is unused. */
3821 remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
3822 int should_write, struct mem_attrib *attrib,
3823 struct target_ops *target)
3825 CORE_ADDR targ_addr;
3829 /* Should this be the selected frame? */
3830 gdbarch_remote_translate_xfer_address (current_gdbarch, current_regcache,
3832 &targ_addr, &targ_len);
3837 res = remote_write_bytes (targ_addr, buffer, targ_len);
3839 res = remote_read_bytes (targ_addr, buffer, targ_len);
3845 remote_files_info (struct target_ops *ignore)
3847 puts_filtered ("Debugging a target over a serial line.\n");
3850 /* Stuff for dealing with the packets which are part of this protocol.
3851 See comment at top of file for details. */
3853 /* Read a single character from the remote end, masking it down to 7 bits. */
3856 readchar (int timeout)
3860 ch = serial_readchar (remote_desc, timeout);
3865 switch ((enum serial_rc) ch)
3868 target_mourn_inferior ();
3869 error ("Remote connection closed");
3872 perror_with_name ("Remote communication error");
3874 case SERIAL_TIMEOUT:
3880 /* Send the command in BUF to the remote machine, and read the reply
3881 into BUF. Report an error if we get an error reply. */
3884 remote_send (char *buf,
3888 getpkt (buf, sizeof_buf, 0);
3891 error ("Remote failure reply: %s", buf);
3894 /* Display a null-terminated packet on stdout, for debugging, using C
3898 print_packet (char *buf)
3900 puts_filtered ("\"");
3901 fputstr_filtered (buf, '"', gdb_stdout);
3902 puts_filtered ("\"");
3908 return putpkt_binary (buf, strlen (buf));
3911 /* Send a packet to the remote machine, with error checking. The data
3912 of the packet is in BUF. The string in BUF can be at most (rs->remote_packet_size) - 5
3913 to account for the $, # and checksum, and for a possible /0 if we are
3914 debugging (remote_debug) and want to print the sent packet as a string */
3917 putpkt_binary (char *buf, int cnt)
3919 struct remote_state *rs = get_remote_state ();
3921 unsigned char csum = 0;
3922 char *buf2 = alloca (cnt + 6);
3923 long sizeof_junkbuf = (rs->remote_packet_size);
3924 char *junkbuf = alloca (sizeof_junkbuf);
3930 /* Copy the packet into buffer BUF2, encapsulating it
3931 and giving it a checksum. */
3936 for (i = 0; i < cnt; i++)
3942 *p++ = tohex ((csum >> 4) & 0xf);
3943 *p++ = tohex (csum & 0xf);
3945 /* Send it over and over until we get a positive ack. */
3949 int started_error_output = 0;
3954 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3955 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
3956 fprintf_unfiltered (gdb_stdlog, "...");
3957 gdb_flush (gdb_stdlog);
3959 if (serial_write (remote_desc, buf2, p - buf2))
3960 perror_with_name ("putpkt: write failed");
3962 /* read until either a timeout occurs (-2) or '+' is read */
3965 ch = readchar (remote_timeout);
3973 case SERIAL_TIMEOUT:
3975 if (started_error_output)
3977 putchar_unfiltered ('\n');
3978 started_error_output = 0;
3987 fprintf_unfiltered (gdb_stdlog, "Ack\n");
3991 fprintf_unfiltered (gdb_stdlog, "Nak\n");
3992 case SERIAL_TIMEOUT:
3996 break; /* Retransmit buffer */
4000 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
4001 /* It's probably an old response sent because an ACK
4002 was lost. Gobble up the packet and ack it so it
4003 doesn't get retransmitted when we resend this
4005 read_frame (junkbuf, sizeof_junkbuf);
4006 serial_write (remote_desc, "+", 1);
4007 continue; /* Now, go look for + */
4012 if (!started_error_output)
4014 started_error_output = 1;
4015 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
4017 fputc_unfiltered (ch & 0177, gdb_stdlog);
4021 break; /* Here to retransmit */
4025 /* This is wrong. If doing a long backtrace, the user should be
4026 able to get out next time we call QUIT, without anything as
4027 violent as interrupt_query. If we want to provide a way out of
4028 here without getting to the next QUIT, it should be based on
4029 hitting ^C twice as in remote_wait. */
4039 /* Come here after finding the start of the frame. Collect the rest
4040 into BUF, verifying the checksum, length, and handling run-length
4041 compression. No more than sizeof_buf-1 characters are read so that
4042 the buffer can be NUL terminated.
4044 Returns -1 on error, number of characters in buffer (ignoring the
4045 trailing NULL) on success. (could be extended to return one of the
4046 SERIAL status indications). */
4049 read_frame (char *buf,
4061 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
4062 c = readchar (remote_timeout);
4065 case SERIAL_TIMEOUT:
4067 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
4071 fputs_filtered ("Saw new packet start in middle of old one\n",
4073 return -1; /* Start a new packet, count retries */
4076 unsigned char pktcsum;
4082 check_0 = readchar (remote_timeout);
4084 check_1 = readchar (remote_timeout);
4086 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4089 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4092 else if (check_0 < 0 || check_1 < 0)
4095 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4099 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
4100 if (csum == pktcsum)
4105 fprintf_filtered (gdb_stdlog,
4106 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4108 fputs_filtered (buf, gdb_stdlog);
4109 fputs_filtered ("\n", gdb_stdlog);
4111 /* Number of characters in buffer ignoring trailing
4115 case '*': /* Run length encoding */
4120 c = readchar (remote_timeout);
4122 repeat = c - ' ' + 3; /* Compute repeat count */
4124 /* The character before ``*'' is repeated. */
4126 if (repeat > 0 && repeat <= 255
4128 && bc + repeat - 1 < sizeof_buf - 1)
4130 memset (&buf[bc], buf[bc - 1], repeat);
4136 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4137 puts_filtered (buf);
4138 puts_filtered ("\n");
4142 if (bc < sizeof_buf - 1)
4150 puts_filtered ("Remote packet too long: ");
4151 puts_filtered (buf);
4152 puts_filtered ("\n");
4159 /* Read a packet from the remote machine, with error checking, and
4160 store it in BUF. If FOREVER, wait forever rather than timing out;
4161 this is used (in synchronous mode) to wait for a target that is is
4162 executing user code to stop. */
4163 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4164 don't have to change all the calls to getpkt to deal with the
4165 return value, because at the moment I don't know what the right
4166 thing to do it for those. */
4174 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4178 /* Read a packet from the remote machine, with error checking, and
4179 store it in BUF. If FOREVER, wait forever rather than timing out;
4180 this is used (in synchronous mode) to wait for a target that is is
4181 executing user code to stop. If FOREVER == 0, this function is
4182 allowed to time out gracefully and return an indication of this to
4185 getpkt_sane (char *buf,
4194 strcpy (buf, "timeout");
4198 timeout = watchdog > 0 ? watchdog : -1;
4202 timeout = remote_timeout;
4206 for (tries = 1; tries <= MAX_TRIES; tries++)
4208 /* This can loop forever if the remote side sends us characters
4209 continuously, but if it pauses, we'll get a zero from readchar
4210 because of timeout. Then we'll count that as a retry. */
4212 /* Note that we will only wait forever prior to the start of a packet.
4213 After that, we expect characters to arrive at a brisk pace. They
4214 should show up within remote_timeout intervals. */
4218 c = readchar (timeout);
4220 if (c == SERIAL_TIMEOUT)
4222 if (forever) /* Watchdog went off? Kill the target. */
4225 target_mourn_inferior ();
4226 error ("Watchdog has expired. Target detached.\n");
4229 fputs_filtered ("Timed out.\n", gdb_stdlog);
4235 /* We've found the start of a packet, now collect the data. */
4237 val = read_frame (buf, sizeof_buf);
4243 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4244 fputstr_unfiltered (buf, 0, gdb_stdlog);
4245 fprintf_unfiltered (gdb_stdlog, "\n");
4247 serial_write (remote_desc, "+", 1);
4251 /* Try the whole thing again. */
4253 serial_write (remote_desc, "-", 1);
4256 /* We have tried hard enough, and just can't receive the packet. Give up. */
4258 printf_unfiltered ("Ignoring packet error, continuing...\n");
4259 serial_write (remote_desc, "+", 1);
4266 /* For some mysterious reason, wait_for_inferior calls kill instead of
4267 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4271 target_mourn_inferior ();
4275 /* Use catch_errors so the user can quit from gdb even when we aren't on
4276 speaking terms with the remote system. */
4277 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4279 /* Don't wait for it to die. I'm not really sure it matters whether
4280 we do or not. For the existing stubs, kill is a noop. */
4281 target_mourn_inferior ();
4284 /* Async version of remote_kill. */
4286 remote_async_kill (void)
4288 /* Unregister the file descriptor from the event loop. */
4289 if (target_is_async_p ())
4290 serial_async (remote_desc, NULL, 0);
4292 /* For some mysterious reason, wait_for_inferior calls kill instead of
4293 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4297 target_mourn_inferior ();
4301 /* Use catch_errors so the user can quit from gdb even when we aren't on
4302 speaking terms with the remote system. */
4303 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4305 /* Don't wait for it to die. I'm not really sure it matters whether
4306 we do or not. For the existing stubs, kill is a noop. */
4307 target_mourn_inferior ();
4313 remote_mourn_1 (&remote_ops);
4317 remote_async_mourn (void)
4319 remote_mourn_1 (&remote_async_ops);
4323 extended_remote_mourn (void)
4325 /* We do _not_ want to mourn the target like this; this will
4326 remove the extended remote target from the target stack,
4327 and the next time the user says "run" it'll fail.
4329 FIXME: What is the right thing to do here? */
4331 remote_mourn_1 (&extended_remote_ops);
4335 /* Worker function for remote_mourn. */
4337 remote_mourn_1 (struct target_ops *target)
4339 unpush_target (target);
4340 generic_mourn_inferior ();
4343 /* In the extended protocol we want to be able to do things like
4344 "run" and have them basically work as expected. So we need
4345 a special create_inferior function.
4347 FIXME: One day add support for changing the exec file
4348 we're debugging, arguments and an environment. */
4351 extended_remote_create_inferior (char *exec_file, char *args, char **env,
4354 /* Rip out the breakpoints; we'll reinsert them after restarting
4355 the remote server. */
4356 remove_breakpoints ();
4358 /* Now restart the remote server. */
4359 extended_remote_restart ();
4361 /* Now put the breakpoints back in. This way we're safe if the
4362 restart function works via a unix fork on the remote side. */
4363 insert_breakpoints ();
4365 /* Clean up from the last time we were running. */
4366 clear_proceed_status ();
4368 /* Let the remote process run. */
4369 proceed (-1, TARGET_SIGNAL_0, 0);
4372 /* Async version of extended_remote_create_inferior. */
4374 extended_remote_async_create_inferior (char *exec_file, char *args, char **env,
4377 /* Rip out the breakpoints; we'll reinsert them after restarting
4378 the remote server. */
4379 remove_breakpoints ();
4381 /* If running asynchronously, register the target file descriptor
4382 with the event loop. */
4383 if (target_can_async_p ())
4384 target_async (inferior_event_handler, 0);
4386 /* Now restart the remote server. */
4387 extended_remote_restart ();
4389 /* Now put the breakpoints back in. This way we're safe if the
4390 restart function works via a unix fork on the remote side. */
4391 insert_breakpoints ();
4393 /* Clean up from the last time we were running. */
4394 clear_proceed_status ();
4396 /* Let the remote process run. */
4397 proceed (-1, TARGET_SIGNAL_0, 0);
4401 /* On some machines, e.g. 68k, we may use a different breakpoint
4402 instruction than other targets; in those use
4403 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4404 Also, bi-endian targets may define
4405 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4406 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4407 just call the standard routines that are in mem-break.c. */
4409 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4410 target should use an identical BREAKPOINT_FROM_PC. As for native,
4411 the ARCH-OS-tdep.c code can override the default. */
4413 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4414 #define DEPRECATED_REMOTE_BREAKPOINT
4417 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4419 /* If the target isn't bi-endian, just pretend it is. */
4420 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4421 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4422 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4425 static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4426 static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
4428 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4430 /* Insert a breakpoint on targets that don't have any better
4431 breakpoint support. We read the contents of the target location
4432 and stash it, then overwrite it with a breakpoint instruction.
4433 ADDR is the target location in the target machine. CONTENTS_CACHE
4434 is a pointer to memory allocated for saving the target contents.
4435 It is guaranteed by the caller to be long enough to save the number
4436 of bytes returned by BREAKPOINT_FROM_PC. */
4439 remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
4441 struct remote_state *rs = get_remote_state ();
4442 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4447 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4448 If it succeeds, then set the support to PACKET_ENABLE. If it
4449 fails, and the user has explicitly requested the Z support then
4450 report an error, otherwise, mark it disabled and go on. */
4452 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4454 char *buf = alloca (rs->remote_packet_size);
4457 addr = remote_address_masked (addr);
4461 p += hexnumstr (p, (ULONGEST) addr);
4462 BREAKPOINT_FROM_PC (&addr, &bp_size);
4463 sprintf (p, ",%d", bp_size);
4466 getpkt (buf, (rs->remote_packet_size), 0);
4468 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
4474 case PACKET_UNKNOWN:
4479 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4480 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4484 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4485 val = target_write_memory (addr, (char *) big_break_insn,
4486 sizeof big_break_insn);
4488 val = target_write_memory (addr, (char *) little_break_insn,
4489 sizeof little_break_insn);
4494 return memory_insert_breakpoint (addr, contents_cache);
4495 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4499 remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
4501 struct remote_state *rs = get_remote_state ();
4504 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4506 char *buf = alloca (rs->remote_packet_size);
4513 addr = remote_address_masked (addr);
4514 p += hexnumstr (p, (ULONGEST) addr);
4515 BREAKPOINT_FROM_PC (&addr, &bp_size);
4516 sprintf (p, ",%d", bp_size);
4519 getpkt (buf, (rs->remote_packet_size), 0);
4521 return (buf[0] == 'E');
4524 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4525 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4527 return memory_remove_breakpoint (addr, contents_cache);
4528 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4532 watchpoint_to_Z_packet (int type)
4546 internal_error (__FILE__, __LINE__,
4547 "hw_bp_to_z: bad watchpoint type %d", type);
4552 remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
4554 struct remote_state *rs = get_remote_state ();
4555 char *buf = alloca (rs->remote_packet_size);
4557 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4559 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4560 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4561 remote_protocol_Z[packet].name,
4562 remote_protocol_Z[packet].title);
4564 sprintf (buf, "Z%x,", packet);
4565 p = strchr (buf, '\0');
4566 addr = remote_address_masked (addr);
4567 p += hexnumstr (p, (ULONGEST) addr);
4568 sprintf (p, ",%x", len);
4571 getpkt (buf, (rs->remote_packet_size), 0);
4573 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4576 case PACKET_UNKNOWN:
4581 internal_error (__FILE__, __LINE__,
4582 "remote_insert_watchpoint: reached end of function");
4587 remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
4589 struct remote_state *rs = get_remote_state ();
4590 char *buf = alloca (rs->remote_packet_size);
4592 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4594 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4595 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4596 remote_protocol_Z[packet].name,
4597 remote_protocol_Z[packet].title);
4599 sprintf (buf, "z%x,", packet);
4600 p = strchr (buf, '\0');
4601 addr = remote_address_masked (addr);
4602 p += hexnumstr (p, (ULONGEST) addr);
4603 sprintf (p, ",%x", len);
4605 getpkt (buf, (rs->remote_packet_size), 0);
4607 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4610 case PACKET_UNKNOWN:
4615 internal_error (__FILE__, __LINE__,
4616 "remote_remove_watchpoint: reached end of function");
4620 int remote_hw_watchpoint_limit = -1;
4621 int remote_hw_breakpoint_limit = -1;
4624 remote_check_watch_resources (int type, int cnt, int ot)
4626 if (type == bp_hardware_breakpoint)
4628 if (remote_hw_breakpoint_limit == 0)
4630 else if (remote_hw_breakpoint_limit < 0)
4632 else if (cnt <= remote_hw_breakpoint_limit)
4637 if (remote_hw_watchpoint_limit == 0)
4639 else if (remote_hw_watchpoint_limit < 0)
4643 else if (cnt <= remote_hw_watchpoint_limit)
4650 remote_stopped_by_watchpoint (void)
4652 return remote_stopped_by_watchpoint_p;
4655 extern int stepped_after_stopped_by_watchpoint;
4658 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
4661 if (remote_stopped_by_watchpoint ()
4662 || stepped_after_stopped_by_watchpoint)
4664 *addr_p = remote_watch_data_address;
4673 remote_insert_hw_breakpoint (CORE_ADDR addr, char *shadow)
4676 struct remote_state *rs = get_remote_state ();
4677 char *buf = alloca (rs->remote_packet_size);
4680 /* The length field should be set to the size of a breakpoint
4683 BREAKPOINT_FROM_PC (&addr, &len);
4685 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4686 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4687 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4688 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4694 addr = remote_address_masked (addr);
4695 p += hexnumstr (p, (ULONGEST) addr);
4696 sprintf (p, ",%x", len);
4699 getpkt (buf, (rs->remote_packet_size), 0);
4701 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4704 case PACKET_UNKNOWN:
4709 internal_error (__FILE__, __LINE__,
4710 "remote_insert_hw_breakpoint: reached end of function");
4715 remote_remove_hw_breakpoint (CORE_ADDR addr, char *shadow)
4718 struct remote_state *rs = get_remote_state ();
4719 char *buf = alloca (rs->remote_packet_size);
4722 /* The length field should be set to the size of a breakpoint
4725 BREAKPOINT_FROM_PC (&addr, &len);
4727 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4728 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4729 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4730 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4736 addr = remote_address_masked (addr);
4737 p += hexnumstr (p, (ULONGEST) addr);
4738 sprintf (p, ",%x", len);
4741 getpkt (buf, (rs->remote_packet_size), 0);
4743 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4746 case PACKET_UNKNOWN:
4751 internal_error (__FILE__, __LINE__,
4752 "remote_remove_hw_breakpoint: reached end of function");
4755 /* Some targets are only capable of doing downloads, and afterwards
4756 they switch to the remote serial protocol. This function provides
4757 a clean way to get from the download target to the remote target.
4758 It's basically just a wrapper so that we don't have to expose any
4759 of the internal workings of remote.c.
4761 Prior to calling this routine, you should shutdown the current
4762 target code, else you will get the "A program is being debugged
4763 already..." message. Usually a call to pop_target() suffices. */
4766 push_remote_target (char *name, int from_tty)
4768 printf_filtered ("Switching to remote protocol\n");
4769 remote_open (name, from_tty);
4772 /* Table used by the crc32 function to calcuate the checksum. */
4774 static unsigned long crc32_table[256] =
4777 static unsigned long
4778 crc32 (unsigned char *buf, int len, unsigned int crc)
4780 if (!crc32_table[1])
4782 /* Initialize the CRC table and the decoding table. */
4786 for (i = 0; i < 256; i++)
4788 for (c = i << 24, j = 8; j > 0; --j)
4789 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4796 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4802 /* compare-sections command
4804 With no arguments, compares each loadable section in the exec bfd
4805 with the same memory range on the target, and reports mismatches.
4806 Useful for verifying the image on the target against the exec file.
4807 Depends on the target understanding the new "qCRC:" request. */
4809 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4810 target method (target verify memory) and generic version of the
4811 actual command. This will allow other high-level code (especially
4812 generic_load()) to make use of this target functionality. */
4815 compare_sections_command (char *args, int from_tty)
4817 struct remote_state *rs = get_remote_state ();
4819 unsigned long host_crc, target_crc;
4820 extern bfd *exec_bfd;
4821 struct cleanup *old_chain;
4824 const char *sectname;
4825 char *buf = alloca (rs->remote_packet_size);
4832 error ("command cannot be used without an exec file");
4833 if (!current_target.to_shortname ||
4834 strcmp (current_target.to_shortname, "remote") != 0)
4835 error ("command can only be used with remote target");
4837 for (s = exec_bfd->sections; s; s = s->next)
4839 if (!(s->flags & SEC_LOAD))
4840 continue; /* skip non-loadable section */
4842 size = bfd_get_section_size (s);
4844 continue; /* skip zero-length section */
4846 sectname = bfd_get_section_name (exec_bfd, s);
4847 if (args && strcmp (args, sectname) != 0)
4848 continue; /* not the section selected by user */
4850 matched = 1; /* do this section */
4852 /* FIXME: assumes lma can fit into long */
4853 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4856 /* be clever; compute the host_crc before waiting for target reply */
4857 sectdata = xmalloc (size);
4858 old_chain = make_cleanup (xfree, sectdata);
4859 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4860 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4862 getpkt (buf, (rs->remote_packet_size), 0);
4864 error ("target memory fault, section %s, range 0x%s -- 0x%s",
4865 sectname, paddr (lma), paddr (lma + size));
4867 error ("remote target does not support this operation");
4869 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4870 target_crc = target_crc * 16 + fromhex (*tmp);
4872 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4873 sectname, paddr (lma), paddr (lma + size));
4874 if (host_crc == target_crc)
4875 printf_filtered ("matched.\n");
4878 printf_filtered ("MIS-MATCHED!\n");
4882 do_cleanups (old_chain);
4885 warning ("One or more sections of the remote executable does not match\n\
4886 the loaded file\n");
4887 if (args && !matched)
4888 printf_filtered ("No loaded section named '%s'.\n", args);
4892 remote_xfer_partial (struct target_ops *ops, enum target_object object,
4893 const char *annex, void *readbuf, const void *writebuf,
4894 ULONGEST offset, LONGEST len)
4896 struct remote_state *rs = get_remote_state ();
4898 char *buf2 = alloca (rs->remote_packet_size);
4899 char *p2 = &buf2[0];
4902 /* Handle memory using remote_xfer_memory. */
4903 if (object == TARGET_OBJECT_MEMORY)
4908 if (writebuf != NULL)
4910 void *buffer = xmalloc (len);
4911 struct cleanup *cleanup = make_cleanup (xfree, buffer);
4912 memcpy (buffer, writebuf, len);
4913 xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops);
4914 do_cleanups (cleanup);
4917 xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops);
4921 else if (xfered == 0 && errno == 0)
4927 /* Only handle reads. */
4928 if (writebuf != NULL || readbuf == NULL)
4931 /* Map pre-existing objects onto letters. DO NOT do this for new
4932 objects!!! Instead specify new query packets. */
4935 case TARGET_OBJECT_KOD:
4938 case TARGET_OBJECT_AVR:
4942 case TARGET_OBJECT_AUXV:
4943 if (remote_protocol_qPart_auxv.support != PACKET_DISABLE)
4945 unsigned int total = 0;
4948 LONGEST n = min ((rs->remote_packet_size - 2) / 2, len);
4949 snprintf (buf2, rs->remote_packet_size,
4950 "qPart:auxv:read::%s,%s",
4951 phex_nz (offset, sizeof offset),
4952 phex_nz (n, sizeof n));
4955 return total > 0 ? total : i;
4957 getpkt (buf2, rs->remote_packet_size, 0);
4958 if (packet_ok (buf2, &remote_protocol_qPart_auxv) != PACKET_OK)
4959 return total > 0 ? total : -1;
4960 if (buf2[0] == 'O' && buf2[1] == 'K' && buf2[2] == '\0')
4961 break; /* Got EOF indicator. */
4962 /* Got some data. */
4963 i = hex2bin (buf2, readbuf, len);
4966 readbuf = (void *) ((char *) readbuf + i);
4980 /* Note: a zero OFFSET and LEN can be used to query the minimum
4982 if (offset == 0 && len == 0)
4983 return (rs->remote_packet_size);
4984 /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is
4985 not large enough let the caller. */
4986 if (len < (rs->remote_packet_size))
4988 len = rs->remote_packet_size;
4990 /* except for querying the minimum buffer size, target must be open */
4992 error ("remote query is only available after target open");
4994 gdb_assert (annex != NULL);
4995 gdb_assert (readbuf != NULL);
5000 /* we used one buffer char for the remote protocol q command and another
5001 for the query type. As the remote protocol encapsulation uses 4 chars
5002 plus one extra in case we are debugging (remote_debug),
5003 we have PBUFZIZ - 7 left to pack the query string */
5005 while (annex[i] && (i < ((rs->remote_packet_size) - 8)))
5007 /* Bad caller may have sent forbidden characters. */
5008 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
5013 gdb_assert (annex[i] == '\0');
5019 getpkt (readbuf, len, 0);
5021 return strlen (readbuf);
5025 remote_rcmd (char *command,
5026 struct ui_file *outbuf)
5028 struct remote_state *rs = get_remote_state ();
5030 char *buf = alloca (rs->remote_packet_size);
5034 error ("remote rcmd is only available after target open");
5036 /* Send a NULL command across as an empty command */
5037 if (command == NULL)
5040 /* The query prefix */
5041 strcpy (buf, "qRcmd,");
5042 p = strchr (buf, '\0');
5044 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > (rs->remote_packet_size))
5045 error ("\"monitor\" command ``%s'' is too long\n", command);
5047 /* Encode the actual command */
5048 bin2hex (command, p, 0);
5050 if (putpkt (buf) < 0)
5051 error ("Communication problem with target\n");
5053 /* get/display the response */
5056 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
5058 getpkt (buf, (rs->remote_packet_size), 0);
5060 error ("Target does not support this command\n");
5061 if (buf[0] == 'O' && buf[1] != 'K')
5063 remote_console_output (buf + 1); /* 'O' message from stub */
5066 if (strcmp (buf, "OK") == 0)
5068 if (strlen (buf) == 3 && buf[0] == 'E'
5069 && isdigit (buf[1]) && isdigit (buf[2]))
5071 error ("Protocol error with Rcmd");
5073 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5075 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5076 fputc_unfiltered (c, outbuf);
5083 packet_command (char *args, int from_tty)
5085 struct remote_state *rs = get_remote_state ();
5086 char *buf = alloca (rs->remote_packet_size);
5089 error ("command can only be used with remote target");
5092 error ("remote-packet command requires packet text as argument");
5094 puts_filtered ("sending: ");
5095 print_packet (args);
5096 puts_filtered ("\n");
5099 getpkt (buf, (rs->remote_packet_size), 0);
5100 puts_filtered ("received: ");
5102 puts_filtered ("\n");
5106 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
5108 static void display_thread_info (struct gdb_ext_thread_info *info);
5110 static void threadset_test_cmd (char *cmd, int tty);
5112 static void threadalive_test (char *cmd, int tty);
5114 static void threadlist_test_cmd (char *cmd, int tty);
5116 int get_and_display_threadinfo (threadref * ref);
5118 static void threadinfo_test_cmd (char *cmd, int tty);
5120 static int thread_display_step (threadref * ref, void *context);
5122 static void threadlist_update_test_cmd (char *cmd, int tty);
5124 static void init_remote_threadtests (void);
5126 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
5129 threadset_test_cmd (char *cmd, int tty)
5131 int sample_thread = SAMPLE_THREAD;
5133 printf_filtered ("Remote threadset test\n");
5134 set_thread (sample_thread, 1);
5139 threadalive_test (char *cmd, int tty)
5141 int sample_thread = SAMPLE_THREAD;
5143 if (remote_thread_alive (pid_to_ptid (sample_thread)))
5144 printf_filtered ("PASS: Thread alive test\n");
5146 printf_filtered ("FAIL: Thread alive test\n");
5149 void output_threadid (char *title, threadref * ref);
5152 output_threadid (char *title, threadref *ref)
5156 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5158 printf_filtered ("%s %s\n", title, (&hexid[0]));
5162 threadlist_test_cmd (char *cmd, int tty)
5165 threadref nextthread;
5166 int done, result_count;
5167 threadref threadlist[3];
5169 printf_filtered ("Remote Threadlist test\n");
5170 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5171 &result_count, &threadlist[0]))
5172 printf_filtered ("FAIL: threadlist test\n");
5175 threadref *scan = threadlist;
5176 threadref *limit = scan + result_count;
5178 while (scan < limit)
5179 output_threadid (" thread ", scan++);
5184 display_thread_info (struct gdb_ext_thread_info *info)
5186 output_threadid ("Threadid: ", &info->threadid);
5187 printf_filtered ("Name: %s\n ", info->shortname);
5188 printf_filtered ("State: %s\n", info->display);
5189 printf_filtered ("other: %s\n\n", info->more_display);
5193 get_and_display_threadinfo (threadref *ref)
5197 struct gdb_ext_thread_info threadinfo;
5199 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5200 | TAG_MOREDISPLAY | TAG_DISPLAY;
5201 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5202 display_thread_info (&threadinfo);
5207 threadinfo_test_cmd (char *cmd, int tty)
5209 int athread = SAMPLE_THREAD;
5213 int_to_threadref (&thread, athread);
5214 printf_filtered ("Remote Threadinfo test\n");
5215 if (!get_and_display_threadinfo (&thread))
5216 printf_filtered ("FAIL cannot get thread info\n");
5220 thread_display_step (threadref *ref, void *context)
5222 /* output_threadid(" threadstep ",ref); *//* simple test */
5223 return get_and_display_threadinfo (ref);
5227 threadlist_update_test_cmd (char *cmd, int tty)
5229 printf_filtered ("Remote Threadlist update test\n");
5230 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5234 init_remote_threadtests (void)
5236 add_com ("tlist", class_obscure, threadlist_test_cmd,
5237 "Fetch and print the remote list of thread identifiers, one pkt only");
5238 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5239 "Fetch and display info about one thread");
5240 add_com ("tset", class_obscure, threadset_test_cmd,
5241 "Test setting to a different thread");
5242 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5243 "Iterate through updating all remote thread info");
5244 add_com ("talive", class_obscure, threadalive_test,
5245 " Remote thread alive test ");
5250 /* Convert a thread ID to a string. Returns the string in a static
5254 remote_pid_to_str (ptid_t ptid)
5256 static char buf[30];
5258 sprintf (buf, "Thread %d", PIDGET (ptid));
5263 init_remote_ops (void)
5265 remote_ops.to_shortname = "remote";
5266 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
5268 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5269 Specify the serial device it is connected to\n\
5270 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5271 remote_ops.to_open = remote_open;
5272 remote_ops.to_close = remote_close;
5273 remote_ops.to_detach = remote_detach;
5274 remote_ops.to_disconnect = remote_disconnect;
5275 remote_ops.to_resume = remote_resume;
5276 remote_ops.to_wait = remote_wait;
5277 remote_ops.to_fetch_registers = remote_fetch_registers;
5278 remote_ops.to_store_registers = remote_store_registers;
5279 remote_ops.to_prepare_to_store = remote_prepare_to_store;
5280 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
5281 remote_ops.to_files_info = remote_files_info;
5282 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5283 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5284 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5285 remote_ops.to_stopped_data_address = remote_stopped_data_address;
5286 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5287 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5288 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5289 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5290 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
5291 remote_ops.to_kill = remote_kill;
5292 remote_ops.to_load = generic_load;
5293 remote_ops.to_mourn_inferior = remote_mourn;
5294 remote_ops.to_thread_alive = remote_thread_alive;
5295 remote_ops.to_find_new_threads = remote_threads_info;
5296 remote_ops.to_pid_to_str = remote_pid_to_str;
5297 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5298 remote_ops.to_stop = remote_stop;
5299 remote_ops.to_xfer_partial = remote_xfer_partial;
5300 remote_ops.to_rcmd = remote_rcmd;
5301 remote_ops.to_stratum = process_stratum;
5302 remote_ops.to_has_all_memory = 1;
5303 remote_ops.to_has_memory = 1;
5304 remote_ops.to_has_stack = 1;
5305 remote_ops.to_has_registers = 1;
5306 remote_ops.to_has_execution = 1;
5307 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5308 remote_ops.to_magic = OPS_MAGIC;
5311 /* Set up the extended remote vector by making a copy of the standard
5312 remote vector and adding to it. */
5315 init_extended_remote_ops (void)
5317 extended_remote_ops = remote_ops;
5319 extended_remote_ops.to_shortname = "extended-remote";
5320 extended_remote_ops.to_longname =
5321 "Extended remote serial target in gdb-specific protocol";
5322 extended_remote_ops.to_doc =
5323 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5324 Specify the serial device it is connected to (e.g. /dev/ttya).",
5325 extended_remote_ops.to_open = extended_remote_open;
5326 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5327 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5331 remote_can_async_p (void)
5333 /* We're async whenever the serial device is. */
5334 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
5338 remote_is_async_p (void)
5340 /* We're async whenever the serial device is. */
5341 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
5344 /* Pass the SERIAL event on and up to the client. One day this code
5345 will be able to delay notifying the client of an event until the
5346 point where an entire packet has been received. */
5348 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5349 static void *async_client_context;
5350 static serial_event_ftype remote_async_serial_handler;
5353 remote_async_serial_handler (struct serial *scb, void *context)
5355 /* Don't propogate error information up to the client. Instead let
5356 the client find out about the error by querying the target. */
5357 async_client_callback (INF_REG_EVENT, async_client_context);
5361 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5363 if (current_target.to_async_mask_value == 0)
5364 internal_error (__FILE__, __LINE__,
5365 "Calling remote_async when async is masked");
5367 if (callback != NULL)
5369 serial_async (remote_desc, remote_async_serial_handler, NULL);
5370 async_client_callback = callback;
5371 async_client_context = context;
5374 serial_async (remote_desc, NULL, NULL);
5377 /* Target async and target extended-async.
5379 This are temporary targets, until it is all tested. Eventually
5380 async support will be incorporated int the usual 'remote'
5384 init_remote_async_ops (void)
5386 remote_async_ops.to_shortname = "async";
5387 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5388 remote_async_ops.to_doc =
5389 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5390 Specify the serial device it is connected to (e.g. /dev/ttya).";
5391 remote_async_ops.to_open = remote_async_open;
5392 remote_async_ops.to_close = remote_close;
5393 remote_async_ops.to_detach = remote_detach;
5394 remote_async_ops.to_disconnect = remote_disconnect;
5395 remote_async_ops.to_resume = remote_async_resume;
5396 remote_async_ops.to_wait = remote_async_wait;
5397 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5398 remote_async_ops.to_store_registers = remote_store_registers;
5399 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5400 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
5401 remote_async_ops.to_files_info = remote_files_info;
5402 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5403 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5404 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5405 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5406 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5407 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5408 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5409 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5410 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
5411 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5412 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5413 remote_async_ops.to_kill = remote_async_kill;
5414 remote_async_ops.to_load = generic_load;
5415 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5416 remote_async_ops.to_thread_alive = remote_thread_alive;
5417 remote_async_ops.to_find_new_threads = remote_threads_info;
5418 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5419 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
5420 remote_async_ops.to_stop = remote_stop;
5421 remote_async_ops.to_xfer_partial = remote_xfer_partial;
5422 remote_async_ops.to_rcmd = remote_rcmd;
5423 remote_async_ops.to_stratum = process_stratum;
5424 remote_async_ops.to_has_all_memory = 1;
5425 remote_async_ops.to_has_memory = 1;
5426 remote_async_ops.to_has_stack = 1;
5427 remote_async_ops.to_has_registers = 1;
5428 remote_async_ops.to_has_execution = 1;
5429 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5430 remote_async_ops.to_can_async_p = remote_can_async_p;
5431 remote_async_ops.to_is_async_p = remote_is_async_p;
5432 remote_async_ops.to_async = remote_async;
5433 remote_async_ops.to_async_mask_value = 1;
5434 remote_async_ops.to_magic = OPS_MAGIC;
5437 /* Set up the async extended remote vector by making a copy of the standard
5438 remote vector and adding to it. */
5441 init_extended_async_remote_ops (void)
5443 extended_async_remote_ops = remote_async_ops;
5445 extended_async_remote_ops.to_shortname = "extended-async";
5446 extended_async_remote_ops.to_longname =
5447 "Extended remote serial target in async gdb-specific protocol";
5448 extended_async_remote_ops.to_doc =
5449 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5450 Specify the serial device it is connected to (e.g. /dev/ttya).",
5451 extended_async_remote_ops.to_open = extended_remote_async_open;
5452 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5453 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5457 set_remote_cmd (char *args, int from_tty)
5462 show_remote_cmd (char *args, int from_tty)
5464 /* FIXME: cagney/2002-06-15: This function should iterate over
5465 remote_show_cmdlist for a list of sub commands to show. */
5466 show_remote_protocol_Z_packet_cmd (args, from_tty, NULL);
5467 show_remote_protocol_P_packet_cmd (args, from_tty, NULL);
5468 show_remote_protocol_p_packet_cmd (args, from_tty, NULL);
5469 show_remote_protocol_qSymbol_packet_cmd (args, from_tty, NULL);
5470 show_remote_protocol_vcont_packet_cmd (args, from_tty, NULL);
5471 show_remote_protocol_binary_download_cmd (args, from_tty, NULL);
5472 show_remote_protocol_qPart_auxv_packet_cmd (args, from_tty, NULL);
5476 build_remote_gdbarch_data (void)
5478 remote_address_size = TARGET_ADDR_BIT;
5481 /* Saved pointer to previous owner of the new_objfile event. */
5482 static void (*remote_new_objfile_chain) (struct objfile *);
5484 /* Function to be called whenever a new objfile (shlib) is detected. */
5486 remote_new_objfile (struct objfile *objfile)
5488 if (remote_desc != 0) /* Have a remote connection */
5490 remote_check_symbols (objfile);
5492 /* Call predecessor on chain, if any. */
5493 if (remote_new_objfile_chain != 0 &&
5495 remote_new_objfile_chain (objfile);
5499 _initialize_remote (void)
5501 static struct cmd_list_element *remote_set_cmdlist;
5502 static struct cmd_list_element *remote_show_cmdlist;
5503 struct cmd_list_element *tmpcmd;
5505 /* architecture specific data */
5506 remote_gdbarch_data_handle = gdbarch_data_register_post_init (init_remote_state);
5508 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5509 that the remote protocol has been initialized. */
5510 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5511 deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5514 add_target (&remote_ops);
5516 init_extended_remote_ops ();
5517 add_target (&extended_remote_ops);
5519 init_remote_async_ops ();
5520 add_target (&remote_async_ops);
5522 init_extended_async_remote_ops ();
5523 add_target (&extended_async_remote_ops);
5525 /* Hook into new objfile notification. */
5526 remote_new_objfile_chain = deprecated_target_new_objfile_hook;
5527 deprecated_target_new_objfile_hook = remote_new_objfile;
5530 init_remote_threadtests ();
5533 /* set/show remote ... */
5535 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5536 Remote protocol specific variables\n\
5537 Configure various remote-protocol specific variables such as\n\
5538 the packets being used",
5539 &remote_set_cmdlist, "set remote ",
5540 0/*allow-unknown*/, &setlist);
5541 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5542 Remote protocol specific variables\n\
5543 Configure various remote-protocol specific variables such as\n\
5544 the packets being used",
5545 &remote_show_cmdlist, "show remote ",
5546 0/*allow-unknown*/, &showlist);
5548 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5549 "Compare section data on target to the exec file.\n\
5550 Argument is a single section name (default: all loaded sections).",
5553 add_cmd ("packet", class_maintenance, packet_command,
5554 "Send an arbitrary packet to a remote target.\n\
5555 maintenance packet TEXT\n\
5556 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5557 this command sends the string TEXT to the inferior, and displays the\n\
5558 response packet. GDB supplies the initial `$' character, and the\n\
5559 terminating `#' character and checksum.",
5562 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, "\
5563 Set whether to send break if interrupted.", "\
5564 Show whether to send break if interrupted.", "\
5565 If set, a break, instead of a cntrl-c, is sent to the remote target.", "\
5566 Whether to send break if interrupted is %s.",
5568 &setlist, &showlist);
5570 /* Install commands for configuring memory read/write packets. */
5572 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5573 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5575 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
5576 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5578 add_cmd ("memory-write-packet-size", no_class,
5579 set_memory_write_packet_size,
5580 "Set the maximum number of bytes per memory-write packet.\n"
5581 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5582 "default packet size. The actual limit is further reduced\n"
5583 "dependent on the target. Specify ``fixed'' to disable the\n"
5584 "further restriction and ``limit'' to enable that restriction\n",
5585 &remote_set_cmdlist);
5586 add_cmd ("memory-read-packet-size", no_class,
5587 set_memory_read_packet_size,
5588 "Set the maximum number of bytes per memory-read packet.\n"
5589 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5590 "default packet size. The actual limit is further reduced\n"
5591 "dependent on the target. Specify ``fixed'' to disable the\n"
5592 "further restriction and ``limit'' to enable that restriction\n",
5593 &remote_set_cmdlist);
5594 add_cmd ("memory-write-packet-size", no_class,
5595 show_memory_write_packet_size,
5596 "Show the maximum number of bytes per memory-write packet.\n",
5597 &remote_show_cmdlist);
5598 add_cmd ("memory-read-packet-size", no_class,
5599 show_memory_read_packet_size,
5600 "Show the maximum number of bytes per memory-read packet.\n",
5601 &remote_show_cmdlist);
5603 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
5604 &remote_hw_watchpoint_limit, "\
5605 Set the maximum number of target hardware watchpoints.", "\
5606 Show the maximum number of target hardware watchpoints.", "\
5607 Specify a negative limit for unlimited.", "\
5608 The maximum number of target hardware watchpoints is %s.",
5610 &remote_set_cmdlist, &remote_show_cmdlist);
5611 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
5612 &remote_hw_breakpoint_limit, "\
5613 Set the maximum number of target hardware breakpoints.", "\
5614 Show the maximum number of target hardware breakpoints.", "\
5615 Specify a negative limit for unlimited.", "\
5616 The maximum number of target hardware breakpoints is %s.",
5618 &remote_set_cmdlist, &remote_show_cmdlist);
5620 deprecated_add_show_from_set
5621 (add_set_cmd ("remoteaddresssize", class_obscure,
5622 var_integer, (char *) &remote_address_size,
5623 "Set the maximum size of the address (in bits) \
5624 in a memory packet.\n",
5628 add_packet_config_cmd (&remote_protocol_binary_download,
5629 "X", "binary-download",
5630 set_remote_protocol_binary_download_cmd,
5631 show_remote_protocol_binary_download_cmd,
5632 &remote_set_cmdlist, &remote_show_cmdlist,
5635 /* XXXX - should ``set remotebinarydownload'' be retained for
5637 deprecated_add_show_from_set
5638 (add_set_cmd ("remotebinarydownload", no_class,
5639 var_boolean, (char *) &remote_binary_download,
5640 "Set binary downloads.\n", &setlist),
5644 add_packet_config_cmd (&remote_protocol_vcont,
5645 "vCont", "verbose-resume",
5646 set_remote_protocol_vcont_packet_cmd,
5647 show_remote_protocol_vcont_packet_cmd,
5648 &remote_set_cmdlist, &remote_show_cmdlist,
5651 add_packet_config_cmd (&remote_protocol_qSymbol,
5652 "qSymbol", "symbol-lookup",
5653 set_remote_protocol_qSymbol_packet_cmd,
5654 show_remote_protocol_qSymbol_packet_cmd,
5655 &remote_set_cmdlist, &remote_show_cmdlist,
5658 add_packet_config_cmd (&remote_protocol_P,
5659 "P", "set-register",
5660 set_remote_protocol_P_packet_cmd,
5661 show_remote_protocol_P_packet_cmd,
5662 &remote_set_cmdlist, &remote_show_cmdlist,
5665 add_packet_config_cmd (&remote_protocol_p,
5666 "p", "fetch-register",
5667 set_remote_protocol_p_packet_cmd,
5668 show_remote_protocol_p_packet_cmd,
5669 &remote_set_cmdlist, &remote_show_cmdlist,
5672 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
5673 "Z0", "software-breakpoint",
5674 set_remote_protocol_Z_software_bp_packet_cmd,
5675 show_remote_protocol_Z_software_bp_packet_cmd,
5676 &remote_set_cmdlist, &remote_show_cmdlist,
5679 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
5680 "Z1", "hardware-breakpoint",
5681 set_remote_protocol_Z_hardware_bp_packet_cmd,
5682 show_remote_protocol_Z_hardware_bp_packet_cmd,
5683 &remote_set_cmdlist, &remote_show_cmdlist,
5686 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
5687 "Z2", "write-watchpoint",
5688 set_remote_protocol_Z_write_wp_packet_cmd,
5689 show_remote_protocol_Z_write_wp_packet_cmd,
5690 &remote_set_cmdlist, &remote_show_cmdlist,
5693 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
5694 "Z3", "read-watchpoint",
5695 set_remote_protocol_Z_read_wp_packet_cmd,
5696 show_remote_protocol_Z_read_wp_packet_cmd,
5697 &remote_set_cmdlist, &remote_show_cmdlist,
5700 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
5701 "Z4", "access-watchpoint",
5702 set_remote_protocol_Z_access_wp_packet_cmd,
5703 show_remote_protocol_Z_access_wp_packet_cmd,
5704 &remote_set_cmdlist, &remote_show_cmdlist,
5707 add_packet_config_cmd (&remote_protocol_qPart_auxv,
5708 "qPart_auxv", "read-aux-vector",
5709 set_remote_protocol_qPart_auxv_packet_cmd,
5710 show_remote_protocol_qPart_auxv_packet_cmd,
5711 &remote_set_cmdlist, &remote_show_cmdlist,
5714 /* Keep the old ``set remote Z-packet ...'' working. */
5715 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
5716 &remote_Z_packet_detect, "\
5717 Set use of remote protocol `Z' packets", "\
5718 Show use of remote protocol `Z' packets ", "\
5719 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5721 Use of remote protocol `Z' packets is %s",
5722 set_remote_protocol_Z_packet_cmd,
5723 show_remote_protocol_Z_packet_cmd,
5724 &remote_set_cmdlist, &remote_show_cmdlist);
5726 /* Eventually initialize fileio. See fileio.c */
5727 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);