1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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"
46 #include "cli/cli-decode.h"
47 #include "cli/cli-setshow.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
63 /* Prototypes for local functions. */
64 static void cleanup_sigint_signal_handler (void *dummy);
65 static void initialize_sigint_signal_handler (void);
66 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
68 static void handle_remote_sigint (int);
69 static void handle_remote_sigint_twice (int);
70 static void async_remote_interrupt (gdb_client_data);
71 void async_remote_interrupt_twice (gdb_client_data);
73 static void build_remote_gdbarch_data (void);
75 static void remote_files_info (struct target_ops *ignore);
77 static void remote_prepare_to_store (void);
79 static void remote_fetch_registers (int regno);
81 static void remote_resume (ptid_t ptid, int step,
82 enum target_signal siggnal);
83 static void remote_async_resume (ptid_t ptid, int step,
84 enum target_signal siggnal);
85 static void remote_open (char *name, int from_tty);
86 static void remote_async_open (char *name, int from_tty);
88 static void extended_remote_open (char *name, int from_tty);
89 static void extended_remote_async_open (char *name, int from_tty);
91 static void remote_open_1 (char *, int, struct target_ops *, int extended_p,
94 static void remote_close (int quitting);
96 static void remote_store_registers (int regno);
98 static void remote_mourn (void);
99 static void remote_async_mourn (void);
101 static void extended_remote_restart (void);
103 static void extended_remote_mourn (void);
105 static void remote_mourn_1 (struct target_ops *);
107 static void remote_send (char **buf, long *sizeof_buf_p);
109 static int readchar (int timeout);
111 static ptid_t remote_wait (ptid_t ptid,
112 struct target_waitstatus *status);
113 static ptid_t remote_async_wait (ptid_t ptid,
114 struct target_waitstatus *status);
116 static void remote_kill (void);
117 static void remote_async_kill (void);
119 static int tohex (int nib);
121 static void remote_detach (char *args, int from_tty);
123 static void remote_interrupt (int signo);
125 static void remote_interrupt_twice (int signo);
127 static void interrupt_query (void);
129 static void set_thread (int, int);
131 static int remote_thread_alive (ptid_t);
133 static void get_offsets (void);
135 static void skip_frame (void);
137 static long read_frame (char **buf_p, long *sizeof_buf);
139 static int remote_insert_breakpoint (CORE_ADDR, bfd_byte *);
141 static int remote_remove_breakpoint (CORE_ADDR, bfd_byte *);
143 static int hexnumlen (ULONGEST num);
145 static void init_remote_ops (void);
147 static void init_extended_remote_ops (void);
149 static void remote_stop (void);
151 static int ishex (int ch, int *val);
153 static int stubhex (int ch);
155 static int hexnumstr (char *, ULONGEST);
157 static int hexnumnstr (char *, ULONGEST, int);
159 static CORE_ADDR remote_address_masked (CORE_ADDR);
161 static void print_packet (char *);
163 static unsigned long crc32 (unsigned char *, int, unsigned int);
165 static void compare_sections_command (char *, int);
167 static void packet_command (char *, int);
169 static int stub_unpack_int (char *buff, int fieldlength);
171 static ptid_t remote_current_thread (ptid_t oldptid);
173 static void remote_find_new_threads (void);
175 static void record_currthread (int currthread);
177 static int fromhex (int a);
179 static int hex2bin (const char *hex, gdb_byte *bin, int count);
181 static int bin2hex (const gdb_byte *bin, char *hex, int count);
183 static int putpkt_binary (char *buf, int cnt);
185 static void check_binary_download (CORE_ADDR addr);
187 struct packet_config;
189 static void show_packet_config_cmd (struct packet_config *config);
191 static void update_packet_config (struct packet_config *config);
193 void _initialize_remote (void);
195 /* Description of the remote protocol. Strictly speaking, when the
196 target is open()ed, remote.c should create a per-target description
197 of the remote protocol using that target's architecture.
198 Unfortunately, the target stack doesn't include local state. For
199 the moment keep the information in the target's architecture
204 long offset; /* Offset into G packet. */
205 long regnum; /* GDB's internal register number. */
206 LONGEST pnum; /* Remote protocol register number. */
207 int in_g_packet; /* Always part of G packet. */
208 /* long size in bytes; == register_size (current_gdbarch, regnum);
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;
234 /* A buffer to use for incoming packets, and its current size. The
235 buffer is grown dynamically for larger incoming packets.
236 Outgoing packets may also be constructed in this buffer.
237 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
238 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
245 /* Handle for retreving the remote protocol data from gdbarch. */
246 static struct gdbarch_data *remote_gdbarch_data_handle;
248 static struct remote_state *
249 get_remote_state (void)
251 return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle);
255 init_remote_state (struct gdbarch *gdbarch)
258 struct remote_state *rs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_state);
260 rs->sizeof_g_packet = 0;
262 /* Assume a 1:1 regnum<->pnum table. */
263 rs->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS,
265 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
267 struct packet_reg *r = &rs->regs[regnum];
270 r->offset = DEPRECATED_REGISTER_BYTE (regnum);
271 r->in_g_packet = (regnum < NUM_REGS);
272 /* ...name = REGISTER_NAME (regnum); */
274 /* Compute packet size by accumulating the size of all registers. */
275 if (regnum < NUM_REGS)
276 rs->sizeof_g_packet += register_size (current_gdbarch, regnum);
279 /* Default maximum number of characters in a packet body. Many
280 remote stubs have a hardwired buffer size of 400 bytes
281 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
282 as the maximum packet-size to ensure that the packet and an extra
283 NUL character can always fit in the buffer. This stops GDB
284 trashing stubs that try to squeeze an extra NUL into what is
285 already a full buffer (As of 1999-12-04 that was most stubs. */
286 rs->remote_packet_size = 400 - 1;
288 /* Should rs->sizeof_g_packet needs more space than the
289 default, adjust the size accordingly. Remember that each byte is
290 encoded as two characters. 32 is the overhead for the packet
291 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
292 (``$NN:G...#NN'') is a better guess, the below has been padded a
294 if (rs->sizeof_g_packet > ((rs->remote_packet_size - 32) / 2))
295 rs->remote_packet_size = (rs->sizeof_g_packet * 2 + 32);
297 /* This one is filled in when a ``g'' packet is received. */
298 rs->actual_register_packet_size = 0;
300 /* Create the buffer at a default size. Note that this would
301 leak memory if the gdbarch were ever destroyed; there's no
302 way to register a destructor for it, and we can't realloc
303 using the gdbarch obstack. But gdbarches are never
305 rs->buf_size = rs->remote_packet_size;
306 rs->buf = xmalloc (rs->buf_size);
311 static struct packet_reg *
312 packet_reg_from_regnum (struct remote_state *rs, long regnum)
314 if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS)
318 struct packet_reg *r = &rs->regs[regnum];
319 gdb_assert (r->regnum == regnum);
324 static struct packet_reg *
325 packet_reg_from_pnum (struct remote_state *rs, LONGEST pnum)
328 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
330 struct packet_reg *r = &rs->regs[i];
337 /* FIXME: graces/2002-08-08: These variables should eventually be
338 bound to an instance of the target object (as in gdbarch-tdep()),
339 when such a thing exists. */
341 /* This is set to the data address of the access causing the target
342 to stop for a watchpoint. */
343 static CORE_ADDR remote_watch_data_address;
345 /* This is non-zero if target stopped for a watchpoint. */
346 static int remote_stopped_by_watchpoint_p;
348 static struct target_ops remote_ops;
350 static struct target_ops extended_remote_ops;
352 /* Temporary target ops. Just like the remote_ops and
353 extended_remote_ops, but with asynchronous support. */
354 static struct target_ops remote_async_ops;
356 static struct target_ops extended_async_remote_ops;
358 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
359 ``forever'' still use the normal timeout mechanism. This is
360 currently used by the ASYNC code to guarentee that target reads
361 during the initial connect always time-out. Once getpkt has been
362 modified to return a timeout indication and, in turn
363 remote_wait()/wait_for_inferior() have gained a timeout parameter
365 static int wait_forever_enabled_p = 1;
368 /* This variable chooses whether to send a ^C or a break when the user
369 requests program interruption. Although ^C is usually what remote
370 systems expect, and that is the default here, sometimes a break is
371 preferable instead. */
373 static int remote_break;
375 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
376 remote_open knows that we don't have a file open when the program
378 static struct serial *remote_desc = NULL;
380 /* This variable sets the number of bits in an address that are to be
381 sent in a memory ("M" or "m") packet. Normally, after stripping
382 leading zeros, the entire address would be sent. This variable
383 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
384 initial implementation of remote.c restricted the address sent in
385 memory packets to ``host::sizeof long'' bytes - (typically 32
386 bits). Consequently, for 64 bit targets, the upper 32 bits of an
387 address was never sent. Since fixing this bug may cause a break in
388 some remote targets this variable is principly provided to
389 facilitate backward compatibility. */
391 static int remote_address_size;
393 /* Tempoary to track who currently owns the terminal. See
394 target_async_terminal_* for more details. */
396 static int remote_async_terminal_ours_p;
399 /* User configurable variables for the number of characters in a
400 memory read/write packet. MIN (rs->remote_packet_size,
401 rs->sizeof_g_packet) is the default. Some targets need smaller
402 values (fifo overruns, et.al.) and some users need larger values
403 (speed up transfers). The variables ``preferred_*'' (the user
404 request), ``current_*'' (what was actually set) and ``forced_*''
405 (Positive - a soft limit, negative - a hard limit). */
407 struct memory_packet_config
414 /* Compute the current size of a read/write packet. Since this makes
415 use of ``actual_register_packet_size'' the computation is dynamic. */
418 get_memory_packet_size (struct memory_packet_config *config)
420 struct remote_state *rs = get_remote_state ();
421 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
422 law?) that some hosts don't cope very well with large alloca()
423 calls. Eventually the alloca() code will be replaced by calls to
424 xmalloc() and make_cleanups() allowing this restriction to either
425 be lifted or removed. */
426 #ifndef MAX_REMOTE_PACKET_SIZE
427 #define MAX_REMOTE_PACKET_SIZE 16384
429 /* NOTE: 20 ensures we can write at least one byte. */
430 #ifndef MIN_REMOTE_PACKET_SIZE
431 #define MIN_REMOTE_PACKET_SIZE 20
436 if (config->size <= 0)
437 what_they_get = MAX_REMOTE_PACKET_SIZE;
439 what_they_get = config->size;
443 what_they_get = rs->remote_packet_size;
444 /* Limit the packet to the size specified by the user. */
446 && what_they_get > config->size)
447 what_they_get = config->size;
448 /* Limit it to the size of the targets ``g'' response. */
449 if ((rs->actual_register_packet_size) > 0
450 && what_they_get > (rs->actual_register_packet_size))
451 what_they_get = (rs->actual_register_packet_size);
453 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
454 what_they_get = MAX_REMOTE_PACKET_SIZE;
455 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
456 what_they_get = MIN_REMOTE_PACKET_SIZE;
458 /* Make sure there is room in the global buffer for this packet
459 (including its trailing NUL byte). */
460 if (rs->buf_size < what_they_get + 1)
462 rs->buf_size = 2 * what_they_get;
463 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
466 return what_they_get;
469 /* Update the size of a read/write packet. If they user wants
470 something really big then do a sanity check. */
473 set_memory_packet_size (char *args, struct memory_packet_config *config)
475 int fixed_p = config->fixed_p;
476 long size = config->size;
478 error (_("Argument required (integer, `fixed' or `limited')."));
479 else if (strcmp (args, "hard") == 0
480 || strcmp (args, "fixed") == 0)
482 else if (strcmp (args, "soft") == 0
483 || strcmp (args, "limit") == 0)
488 size = strtoul (args, &end, 0);
490 error (_("Invalid %s (bad syntax)."), config->name);
492 /* Instead of explicitly capping the size of a packet to
493 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
494 instead allowed to set the size to something arbitrarily
496 if (size > MAX_REMOTE_PACKET_SIZE)
497 error (_("Invalid %s (too large)."), config->name);
501 if (fixed_p && !config->fixed_p)
503 if (! query (_("The target may not be able to correctly handle a %s\n"
504 "of %ld bytes. Change the packet size? "),
506 error (_("Packet size not changed."));
508 /* Update the config. */
509 config->fixed_p = fixed_p;
514 show_memory_packet_size (struct memory_packet_config *config)
516 printf_filtered (_("The %s is %ld. "), config->name, config->size);
518 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
519 get_memory_packet_size (config));
521 printf_filtered (_("Packets are limited to %ld bytes.\n"),
522 get_memory_packet_size (config));
525 static struct memory_packet_config memory_write_packet_config =
527 "memory-write-packet-size",
531 set_memory_write_packet_size (char *args, int from_tty)
533 set_memory_packet_size (args, &memory_write_packet_config);
537 show_memory_write_packet_size (char *args, int from_tty)
539 show_memory_packet_size (&memory_write_packet_config);
543 get_memory_write_packet_size (void)
545 return get_memory_packet_size (&memory_write_packet_config);
548 static struct memory_packet_config memory_read_packet_config =
550 "memory-read-packet-size",
554 set_memory_read_packet_size (char *args, int from_tty)
556 set_memory_packet_size (args, &memory_read_packet_config);
560 show_memory_read_packet_size (char *args, int from_tty)
562 show_memory_packet_size (&memory_read_packet_config);
566 get_memory_read_packet_size (void)
568 struct remote_state *rs = get_remote_state ();
569 long size = get_memory_packet_size (&memory_read_packet_config);
570 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
571 extra buffer size argument before the memory read size can be
572 increased beyond RS->remote_packet_size. */
573 if (size > rs->remote_packet_size)
574 size = rs->remote_packet_size;
579 /* Generic configuration support for packets the stub optionally
580 supports. Allows the user to specify the use of the packet as well
581 as allowing GDB to auto-detect support in the remote stub. */
585 PACKET_SUPPORT_UNKNOWN = 0,
594 enum auto_boolean detect;
595 enum packet_support support;
598 /* Analyze a packet's return value and update the packet config
609 update_packet_config (struct packet_config *config)
611 switch (config->detect)
613 case AUTO_BOOLEAN_TRUE:
614 config->support = PACKET_ENABLE;
616 case AUTO_BOOLEAN_FALSE:
617 config->support = PACKET_DISABLE;
619 case AUTO_BOOLEAN_AUTO:
620 config->support = PACKET_SUPPORT_UNKNOWN;
626 show_packet_config_cmd (struct packet_config *config)
628 char *support = "internal-error";
629 switch (config->support)
635 support = "disabled";
637 case PACKET_SUPPORT_UNKNOWN:
641 switch (config->detect)
643 case AUTO_BOOLEAN_AUTO:
644 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
645 config->name, support);
647 case AUTO_BOOLEAN_TRUE:
648 case AUTO_BOOLEAN_FALSE:
649 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
650 config->name, support);
656 add_packet_config_cmd (struct packet_config *config,
659 cmd_sfunc_ftype *set_func,
660 show_value_ftype *show_func,
661 struct cmd_list_element **set_remote_list,
662 struct cmd_list_element **show_remote_list,
670 config->title = title;
671 config->detect = AUTO_BOOLEAN_AUTO;
672 config->support = PACKET_SUPPORT_UNKNOWN;
673 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
675 show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
677 /* set/show TITLE-packet {auto,on,off} */
678 cmd_name = xstrprintf ("%s-packet", title);
679 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
680 &config->detect, set_doc, show_doc, NULL, /* help_doc */
682 set_remote_list, show_remote_list);
683 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
687 legacy_name = xstrprintf ("%s-packet", name);
688 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
690 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
695 static enum packet_result
696 packet_ok (const char *buf, struct packet_config *config)
700 /* The stub recognized the packet request. Check that the
701 operation succeeded. */
702 switch (config->support)
704 case PACKET_SUPPORT_UNKNOWN:
706 fprintf_unfiltered (gdb_stdlog,
707 "Packet %s (%s) is supported\n",
708 config->name, config->title);
709 config->support = PACKET_ENABLE;
712 internal_error (__FILE__, __LINE__,
713 _("packet_ok: attempt to use a disabled packet"));
718 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
719 /* "OK" - definitly OK. */
722 && isxdigit (buf[1]) && isxdigit (buf[2])
724 /* "Enn" - definitly an error. */
726 /* The packet may or may not be OK. Just assume it is. */
731 /* The stub does not support the packet. */
732 switch (config->support)
735 if (config->detect == AUTO_BOOLEAN_AUTO)
736 /* If the stub previously indicated that the packet was
737 supported then there is a protocol error.. */
738 error (_("Protocol error: %s (%s) conflicting enabled responses."),
739 config->name, config->title);
741 /* The user set it wrong. */
742 error (_("Enabled packet %s (%s) not recognized by stub"),
743 config->name, config->title);
745 case PACKET_SUPPORT_UNKNOWN:
747 fprintf_unfiltered (gdb_stdlog,
748 "Packet %s (%s) is NOT supported\n",
749 config->name, config->title);
750 config->support = PACKET_DISABLE;
755 return PACKET_UNKNOWN;
775 static struct packet_config remote_protocol_packets[PACKET_MAX];
778 set_remote_protocol_packet_cmd (char *args, int from_tty,
779 struct cmd_list_element *c)
781 struct packet_config *packet;
783 for (packet = remote_protocol_packets;
784 packet < &remote_protocol_packets[PACKET_MAX];
787 if (&packet->detect == c->var)
789 update_packet_config (packet);
793 internal_error (__FILE__, __LINE__, "Could not find config for %s",
798 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
799 struct cmd_list_element *c,
802 struct packet_config *packet;
804 for (packet = remote_protocol_packets;
805 packet < &remote_protocol_packets[PACKET_MAX];
808 if (&packet->detect == c->var)
810 show_packet_config_cmd (packet);
814 internal_error (__FILE__, __LINE__, "Could not find config for %s",
818 /* Should we try one of the 'Z' requests? */
822 Z_PACKET_SOFTWARE_BP,
823 Z_PACKET_HARDWARE_BP,
830 /* For compatibility with older distributions. Provide a ``set remote
831 Z-packet ...'' command that updates all the Z packet types. */
833 static enum auto_boolean remote_Z_packet_detect;
836 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
837 struct cmd_list_element *c)
840 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
842 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
843 update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
848 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
849 struct cmd_list_element *c,
853 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
855 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
859 /* Should we try the 'ThreadInfo' query packet?
861 This variable (NOT available to the user: auto-detect only!)
862 determines whether GDB will use the new, simpler "ThreadInfo"
863 query or the older, more complex syntax for thread queries.
864 This is an auto-detect variable (set to true at each connect,
865 and set to false when the target fails to recognize it). */
867 static int use_threadinfo_query;
868 static int use_threadextra_query;
870 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
871 static void *sigint_remote_twice_token;
872 static void *sigint_remote_token;
874 /* These are pointers to hook functions that may be set in order to
875 modify resume/wait behavior for a particular architecture. */
877 void (*deprecated_target_resume_hook) (void);
878 void (*deprecated_target_wait_loop_hook) (void);
882 /* These are the threads which we last sent to the remote system.
883 -1 for all or -2 for not sent yet. */
884 static int general_thread;
885 static int continue_thread;
887 /* Call this function as a result of
888 1) A halt indication (T packet) containing a thread id
889 2) A direct query of currthread
890 3) Successful execution of set thread
894 record_currthread (int currthread)
896 general_thread = currthread;
898 /* If this is a new thread, add it to GDB's thread list.
899 If we leave it up to WFI to do this, bad things will happen. */
900 if (!in_thread_list (pid_to_ptid (currthread)))
902 add_thread (pid_to_ptid (currthread));
903 ui_out_text (uiout, "[New ");
904 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
905 ui_out_text (uiout, "]\n");
909 #define MAGIC_NULL_PID 42000
912 set_thread (int th, int gen)
914 struct remote_state *rs = get_remote_state ();
916 int state = gen ? general_thread : continue_thread;
922 buf[1] = gen ? 'g' : 'c';
923 if (th == MAGIC_NULL_PID)
929 xsnprintf (&buf[2], rs->remote_packet_size - 2, "-%x", -th);
931 xsnprintf (&buf[2], rs->remote_packet_size - 2, "%x", th);
933 getpkt (&rs->buf, &rs->buf_size, 0);
937 continue_thread = th;
940 /* Return nonzero if the thread TH is still alive on the remote system. */
943 remote_thread_alive (ptid_t ptid)
945 struct remote_state *rs = get_remote_state ();
946 int tid = PIDGET (ptid);
950 xsnprintf (buf, rs->remote_packet_size, "T-%08x", -tid);
952 xsnprintf (buf, rs->remote_packet_size, "T%08x", tid);
954 getpkt (&rs->buf, &rs->buf_size, 0);
955 return (buf[0] == 'O' && buf[1] == 'K');
958 /* About these extended threadlist and threadinfo packets. They are
959 variable length packets but, the fields within them are often fixed
960 length. They are redundent enough to send over UDP as is the
961 remote protocol in general. There is a matching unit test module
964 #define OPAQUETHREADBYTES 8
966 /* a 64 bit opaque identifier */
967 typedef unsigned char threadref[OPAQUETHREADBYTES];
969 /* WARNING: This threadref data structure comes from the remote O.S.,
970 libstub protocol encoding, and remote.c. it is not particularly
973 /* Right now, the internal structure is int. We want it to be bigger.
977 typedef int gdb_threadref; /* Internal GDB thread reference. */
979 /* gdb_ext_thread_info is an internal GDB data structure which is
980 equivalint to the reply of the remote threadinfo packet. */
982 struct gdb_ext_thread_info
984 threadref threadid; /* External form of thread reference. */
985 int active; /* Has state interesting to GDB?
987 char display[256]; /* Brief state display, name,
988 blocked/suspended. */
989 char shortname[32]; /* To be used to name threads. */
990 char more_display[256]; /* Long info, statistics, queue depth,
994 /* The volume of remote transfers can be limited by submitting
995 a mask containing bits specifying the desired information.
996 Use a union of these values as the 'selection' parameter to
997 get_thread_info. FIXME: Make these TAG names more thread specific.
1000 #define TAG_THREADID 1
1001 #define TAG_EXISTS 2
1002 #define TAG_DISPLAY 4
1003 #define TAG_THREADNAME 8
1004 #define TAG_MOREDISPLAY 16
1006 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1008 char *unpack_varlen_hex (char *buff, ULONGEST *result);
1010 static char *unpack_nibble (char *buf, int *val);
1012 static char *pack_nibble (char *buf, int nibble);
1014 static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
1016 static char *unpack_byte (char *buf, int *value);
1018 static char *pack_int (char *buf, int value);
1020 static char *unpack_int (char *buf, int *value);
1022 static char *unpack_string (char *src, char *dest, int length);
1024 static char *pack_threadid (char *pkt, threadref *id);
1026 static char *unpack_threadid (char *inbuf, threadref *id);
1028 void int_to_threadref (threadref *id, int value);
1030 static int threadref_to_int (threadref *ref);
1032 static void copy_threadref (threadref *dest, threadref *src);
1034 static int threadmatch (threadref *dest, threadref *src);
1036 static char *pack_threadinfo_request (char *pkt, int mode,
1039 static int remote_unpack_thread_info_response (char *pkt,
1040 threadref *expectedref,
1041 struct gdb_ext_thread_info
1045 static int remote_get_threadinfo (threadref *threadid,
1046 int fieldset, /*TAG mask */
1047 struct gdb_ext_thread_info *info);
1049 static char *pack_threadlist_request (char *pkt, int startflag,
1051 threadref *nextthread);
1053 static int parse_threadlist_response (char *pkt,
1055 threadref *original_echo,
1056 threadref *resultlist,
1059 static int remote_get_threadlist (int startflag,
1060 threadref *nextthread,
1064 threadref *threadlist);
1066 typedef int (*rmt_thread_action) (threadref *ref, void *context);
1068 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1069 void *context, int looplimit);
1071 static int remote_newthread_step (threadref *ref, void *context);
1073 /* Encode 64 bits in 16 chars of hex. */
1075 static const char hexchars[] = "0123456789abcdef";
1078 ishex (int ch, int *val)
1080 if ((ch >= 'a') && (ch <= 'f'))
1082 *val = ch - 'a' + 10;
1085 if ((ch >= 'A') && (ch <= 'F'))
1087 *val = ch - 'A' + 10;
1090 if ((ch >= '0') && (ch <= '9'))
1101 if (ch >= 'a' && ch <= 'f')
1102 return ch - 'a' + 10;
1103 if (ch >= '0' && ch <= '9')
1105 if (ch >= 'A' && ch <= 'F')
1106 return ch - 'A' + 10;
1111 stub_unpack_int (char *buff, int fieldlength)
1118 nibble = stubhex (*buff++);
1122 retval = retval << 4;
1128 unpack_varlen_hex (char *buff, /* packet to parse */
1134 while (ishex (*buff, &nibble))
1137 retval = retval << 4;
1138 retval |= nibble & 0x0f;
1145 unpack_nibble (char *buf, int *val)
1147 ishex (*buf++, val);
1152 pack_nibble (char *buf, int nibble)
1154 *buf++ = hexchars[(nibble & 0x0f)];
1159 pack_hex_byte (char *pkt, int byte)
1161 *pkt++ = hexchars[(byte >> 4) & 0xf];
1162 *pkt++ = hexchars[(byte & 0xf)];
1167 unpack_byte (char *buf, int *value)
1169 *value = stub_unpack_int (buf, 2);
1174 pack_int (char *buf, int value)
1176 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1177 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1178 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1179 buf = pack_hex_byte (buf, (value & 0xff));
1184 unpack_int (char *buf, int *value)
1186 *value = stub_unpack_int (buf, 8);
1190 #if 0 /* Currently unused, uncomment when needed. */
1191 static char *pack_string (char *pkt, char *string);
1194 pack_string (char *pkt, char *string)
1199 len = strlen (string);
1201 len = 200; /* Bigger than most GDB packets, junk??? */
1202 pkt = pack_hex_byte (pkt, len);
1206 if ((ch == '\0') || (ch == '#'))
1207 ch = '*'; /* Protect encapsulation. */
1212 #endif /* 0 (unused) */
1215 unpack_string (char *src, char *dest, int length)
1224 pack_threadid (char *pkt, threadref *id)
1227 unsigned char *altid;
1229 altid = (unsigned char *) id;
1230 limit = pkt + BUF_THREAD_ID_SIZE;
1232 pkt = pack_hex_byte (pkt, *altid++);
1238 unpack_threadid (char *inbuf, threadref *id)
1241 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1244 altref = (char *) id;
1246 while (inbuf < limit)
1248 x = stubhex (*inbuf++);
1249 y = stubhex (*inbuf++);
1250 *altref++ = (x << 4) | y;
1255 /* Externally, threadrefs are 64 bits but internally, they are still
1256 ints. This is due to a mismatch of specifications. We would like
1257 to use 64bit thread references internally. This is an adapter
1261 int_to_threadref (threadref *id, int value)
1263 unsigned char *scan;
1265 scan = (unsigned char *) id;
1271 *scan++ = (value >> 24) & 0xff;
1272 *scan++ = (value >> 16) & 0xff;
1273 *scan++ = (value >> 8) & 0xff;
1274 *scan++ = (value & 0xff);
1278 threadref_to_int (threadref *ref)
1281 unsigned char *scan;
1287 value = (value << 8) | ((*scan++) & 0xff);
1292 copy_threadref (threadref *dest, threadref *src)
1295 unsigned char *csrc, *cdest;
1297 csrc = (unsigned char *) src;
1298 cdest = (unsigned char *) dest;
1305 threadmatch (threadref *dest, threadref *src)
1307 /* Things are broken right now, so just assume we got a match. */
1309 unsigned char *srcp, *destp;
1311 srcp = (char *) src;
1312 destp = (char *) dest;
1316 result &= (*srcp++ == *destp++) ? 1 : 0;
1323 threadid:1, # always request threadid
1330 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1333 pack_threadinfo_request (char *pkt, int mode, threadref *id)
1335 *pkt++ = 'q'; /* Info Query */
1336 *pkt++ = 'P'; /* process or thread info */
1337 pkt = pack_int (pkt, mode); /* mode */
1338 pkt = pack_threadid (pkt, id); /* threadid */
1339 *pkt = '\0'; /* terminate */
1343 /* These values tag the fields in a thread info response packet. */
1344 /* Tagging the fields allows us to request specific fields and to
1345 add more fields as time goes by. */
1347 #define TAG_THREADID 1 /* Echo the thread identifier. */
1348 #define TAG_EXISTS 2 /* Is this process defined enough to
1349 fetch registers and its stack? */
1350 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1351 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1352 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1356 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1357 struct gdb_ext_thread_info *info)
1359 struct remote_state *rs = get_remote_state ();
1363 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
1366 /* info->threadid = 0; FIXME: implement zero_threadref. */
1368 info->display[0] = '\0';
1369 info->shortname[0] = '\0';
1370 info->more_display[0] = '\0';
1372 /* Assume the characters indicating the packet type have been
1374 pkt = unpack_int (pkt, &mask); /* arg mask */
1375 pkt = unpack_threadid (pkt, &ref);
1378 warning (_("Incomplete response to threadinfo request."));
1379 if (!threadmatch (&ref, expectedref))
1380 { /* This is an answer to a different request. */
1381 warning (_("ERROR RMT Thread info mismatch."));
1384 copy_threadref (&info->threadid, &ref);
1386 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1388 /* Packets are terminated with nulls. */
1389 while ((pkt < limit) && mask && *pkt)
1391 pkt = unpack_int (pkt, &tag); /* tag */
1392 pkt = unpack_byte (pkt, &length); /* length */
1393 if (!(tag & mask)) /* Tags out of synch with mask. */
1395 warning (_("ERROR RMT: threadinfo tag mismatch."));
1399 if (tag == TAG_THREADID)
1403 warning (_("ERROR RMT: length of threadid is not 16."));
1407 pkt = unpack_threadid (pkt, &ref);
1408 mask = mask & ~TAG_THREADID;
1411 if (tag == TAG_EXISTS)
1413 info->active = stub_unpack_int (pkt, length);
1415 mask = mask & ~(TAG_EXISTS);
1418 warning (_("ERROR RMT: 'exists' length too long."));
1424 if (tag == TAG_THREADNAME)
1426 pkt = unpack_string (pkt, &info->shortname[0], length);
1427 mask = mask & ~TAG_THREADNAME;
1430 if (tag == TAG_DISPLAY)
1432 pkt = unpack_string (pkt, &info->display[0], length);
1433 mask = mask & ~TAG_DISPLAY;
1436 if (tag == TAG_MOREDISPLAY)
1438 pkt = unpack_string (pkt, &info->more_display[0], length);
1439 mask = mask & ~TAG_MOREDISPLAY;
1442 warning (_("ERROR RMT: unknown thread info tag."));
1443 break; /* Not a tag we know about. */
1449 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1450 struct gdb_ext_thread_info *info)
1452 struct remote_state *rs = get_remote_state ();
1454 char *threadinfo_pkt = rs->buf;
1456 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1457 putpkt (threadinfo_pkt);
1458 getpkt (&rs->buf, &rs->buf_size, 0);
1459 result = remote_unpack_thread_info_response (threadinfo_pkt + 2,
1464 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1467 pack_threadlist_request (char *pkt, int startflag, int threadcount,
1468 threadref *nextthread)
1470 *pkt++ = 'q'; /* info query packet */
1471 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1472 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1473 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1474 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1479 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1482 parse_threadlist_response (char *pkt, int result_limit,
1483 threadref *original_echo, threadref *resultlist,
1486 struct remote_state *rs = get_remote_state ();
1488 int count, resultcount, done;
1491 /* Assume the 'q' and 'M chars have been stripped. */
1492 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
1493 /* done parse past here */
1494 pkt = unpack_byte (pkt, &count); /* count field */
1495 pkt = unpack_nibble (pkt, &done);
1496 /* The first threadid is the argument threadid. */
1497 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1498 while ((count-- > 0) && (pkt < limit))
1500 pkt = unpack_threadid (pkt, resultlist++);
1501 if (resultcount++ >= result_limit)
1510 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1511 int *done, int *result_count, threadref *threadlist)
1513 struct remote_state *rs = get_remote_state ();
1514 static threadref echo_nextthread;
1515 char *threadlist_packet = rs->buf;
1518 /* Trancate result limit to be smaller than the packet size. */
1519 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= rs->remote_packet_size)
1520 result_limit = (rs->remote_packet_size / BUF_THREAD_ID_SIZE) - 2;
1522 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
1524 getpkt (&rs->buf, &rs->buf_size, 0);
1527 parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
1530 if (!threadmatch (&echo_nextthread, nextthread))
1532 /* FIXME: This is a good reason to drop the packet. */
1533 /* Possably, there is a duplicate response. */
1535 retransmit immediatly - race conditions
1536 retransmit after timeout - yes
1538 wait for packet, then exit
1540 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1541 return 0; /* I choose simply exiting. */
1543 if (*result_count <= 0)
1547 warning (_("RMT ERROR : failed to get remote thread list."));
1550 return result; /* break; */
1552 if (*result_count > result_limit)
1555 warning (_("RMT ERROR: threadlist response longer than requested."));
1561 /* This is the interface between remote and threads, remotes upper
1564 /* remote_find_new_threads retrieves the thread list and for each
1565 thread in the list, looks up the thread in GDB's internal list,
1566 ading the thread if it does not already exist. This involves
1567 getting partial thread lists from the remote target so, polling the
1568 quit_flag is required. */
1571 /* About this many threadisds fit in a packet. */
1573 #define MAXTHREADLISTRESULTS 32
1576 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1579 int done, i, result_count;
1583 static threadref nextthread;
1584 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1589 if (loopcount++ > looplimit)
1592 warning (_("Remote fetch threadlist -infinite loop-."));
1595 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1596 &done, &result_count, resultthreadlist))
1601 /* Clear for later iterations. */
1603 /* Setup to resume next batch of thread references, set nextthread. */
1604 if (result_count >= 1)
1605 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1607 while (result_count--)
1608 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1615 remote_newthread_step (threadref *ref, void *context)
1619 ptid = pid_to_ptid (threadref_to_int (ref));
1621 if (!in_thread_list (ptid))
1623 return 1; /* continue iterator */
1626 #define CRAZY_MAX_THREADS 1000
1629 remote_current_thread (ptid_t oldpid)
1631 struct remote_state *rs = get_remote_state ();
1632 char *buf = rs->buf;
1635 getpkt (&rs->buf, &rs->buf_size, 0);
1636 if (buf[0] == 'Q' && buf[1] == 'C')
1637 /* Use strtoul here, so we'll correctly parse values whose highest
1638 bit is set. The protocol carries them as a simple series of
1639 hex digits; in the absence of a sign, strtol will see such
1640 values as positive numbers out of range for signed 'long', and
1641 return LONG_MAX to indicate an overflow. */
1642 return pid_to_ptid (strtoul (&buf[2], NULL, 16));
1647 /* Find new threads for info threads command.
1648 * Original version, using John Metzler's thread protocol.
1652 remote_find_new_threads (void)
1654 remote_threadlist_iterator (remote_newthread_step, 0,
1656 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1657 inferior_ptid = remote_current_thread (inferior_ptid);
1661 * Find all threads for info threads command.
1662 * Uses new thread protocol contributed by Cisco.
1663 * Falls back and attempts to use the older method (above)
1664 * if the target doesn't respond to the new method.
1668 remote_threads_info (void)
1670 struct remote_state *rs = get_remote_state ();
1674 if (remote_desc == 0) /* paranoia */
1675 error (_("Command can only be used when connected to the remote target."));
1677 if (use_threadinfo_query)
1679 putpkt ("qfThreadInfo");
1681 getpkt (&rs->buf, &rs->buf_size, 0);
1682 if (bufp[0] != '\0') /* q packet recognized */
1684 while (*bufp++ == 'm') /* reply contains one or more TID */
1688 /* Use strtoul here, so we'll correctly parse values
1689 whose highest bit is set. The protocol carries
1690 them as a simple series of hex digits; in the
1691 absence of a sign, strtol will see such values as
1692 positive numbers out of range for signed 'long',
1693 and return LONG_MAX to indicate an overflow. */
1694 tid = strtoul (bufp, &bufp, 16);
1695 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1696 add_thread (pid_to_ptid (tid));
1698 while (*bufp++ == ','); /* comma-separated list */
1699 putpkt ("qsThreadInfo");
1701 getpkt (&rs->buf, &rs->buf_size, 0);
1707 /* Else fall back to old method based on jmetzler protocol. */
1708 use_threadinfo_query = 0;
1709 remote_find_new_threads ();
1714 * Collect a descriptive string about the given thread.
1715 * The target may say anything it wants to about the thread
1716 * (typically info about its blocked / runnable state, name, etc.).
1717 * This string will appear in the info threads display.
1719 * Optional: targets are not required to implement this function.
1723 remote_threads_extra_info (struct thread_info *tp)
1725 struct remote_state *rs = get_remote_state ();
1729 struct gdb_ext_thread_info threadinfo;
1730 static char display_buf[100]; /* arbitrary... */
1731 int n = 0; /* position in display_buf */
1733 if (remote_desc == 0) /* paranoia */
1734 internal_error (__FILE__, __LINE__,
1735 _("remote_threads_extra_info"));
1737 if (use_threadextra_query)
1739 char *bufp = rs->buf;
1741 xsnprintf (bufp, rs->remote_packet_size, "qThreadExtraInfo,%x",
1744 getpkt (&rs->buf, &rs->buf_size, 0);
1747 n = min (strlen (bufp) / 2, sizeof (display_buf));
1748 result = hex2bin (bufp, (gdb_byte *) display_buf, n);
1749 display_buf [result] = '\0';
1754 /* If the above query fails, fall back to the old method. */
1755 use_threadextra_query = 0;
1756 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1757 | TAG_MOREDISPLAY | TAG_DISPLAY;
1758 int_to_threadref (&id, PIDGET (tp->ptid));
1759 if (remote_get_threadinfo (&id, set, &threadinfo))
1760 if (threadinfo.active)
1762 if (*threadinfo.shortname)
1763 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
1764 " Name: %s,", threadinfo.shortname);
1765 if (*threadinfo.display)
1766 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1767 " State: %s,", threadinfo.display);
1768 if (*threadinfo.more_display)
1769 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1770 " Priority: %s", threadinfo.more_display);
1774 /* For purely cosmetic reasons, clear up trailing commas. */
1775 if (',' == display_buf[n-1])
1776 display_buf[n-1] = ' ';
1784 /* Restart the remote side; this is an extended protocol operation. */
1787 extended_remote_restart (void)
1789 struct remote_state *rs = get_remote_state ();
1791 /* Send the restart command; for reasons I don't understand the
1792 remote side really expects a number after the "R". */
1793 xsnprintf (rs->buf, rs->remote_packet_size, "R%x", 0);
1796 /* Now query for status so this looks just like we restarted
1797 gdbserver from scratch. */
1799 getpkt (&rs->buf, &rs->remote_packet_size, 0);
1802 /* Clean up connection to a remote debugger. */
1805 remote_close (int quitting)
1808 serial_close (remote_desc);
1812 /* Query the remote side for the text, data and bss offsets. */
1817 struct remote_state *rs = get_remote_state ();
1818 char *buf = rs->buf;
1821 CORE_ADDR text_addr, data_addr, bss_addr;
1822 struct section_offsets *offs;
1824 putpkt ("qOffsets");
1825 getpkt (&rs->buf, &rs->buf_size, 0);
1827 if (buf[0] == '\000')
1828 return; /* Return silently. Stub doesn't support
1832 warning (_("Remote failure reply: %s"), buf);
1836 /* Pick up each field in turn. This used to be done with scanf, but
1837 scanf will make trouble if CORE_ADDR size doesn't match
1838 conversion directives correctly. The following code will work
1839 with any size of CORE_ADDR. */
1840 text_addr = data_addr = bss_addr = 0;
1844 if (strncmp (ptr, "Text=", 5) == 0)
1847 /* Don't use strtol, could lose on big values. */
1848 while (*ptr && *ptr != ';')
1849 text_addr = (text_addr << 4) + fromhex (*ptr++);
1854 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1857 while (*ptr && *ptr != ';')
1858 data_addr = (data_addr << 4) + fromhex (*ptr++);
1863 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1866 while (*ptr && *ptr != ';')
1867 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1873 error (_("Malformed response to offset query, %s"), buf);
1875 if (symfile_objfile == NULL)
1878 offs = ((struct section_offsets *)
1879 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
1880 memcpy (offs, symfile_objfile->section_offsets,
1881 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
1883 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
1885 /* This is a temporary kludge to force data and bss to use the same offsets
1886 because that's what nlmconv does now. The real solution requires changes
1887 to the stub and remote.c that I don't have time to do right now. */
1889 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1890 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
1892 objfile_relocate (symfile_objfile, offs);
1895 /* Stub for catch_errors. */
1898 remote_start_remote_dummy (struct ui_out *uiout, void *dummy)
1900 start_remote (); /* Initialize gdb process mechanisms. */
1901 /* NOTE: Return something >=0. A -ve value is reserved for
1902 catch_exceptions. */
1907 remote_start_remote (struct ui_out *uiout, void *dummy)
1909 immediate_quit++; /* Allow user to interrupt it. */
1911 /* Ack any packet which the remote side has already sent. */
1912 serial_write (remote_desc, "+", 1);
1914 /* Let the stub know that we want it to return the thread. */
1917 inferior_ptid = remote_current_thread (inferior_ptid);
1919 get_offsets (); /* Get text, data & bss offsets. */
1921 putpkt ("?"); /* Initiate a query from remote machine. */
1924 remote_start_remote_dummy (uiout, dummy);
1927 /* Open a connection to a remote debugger.
1928 NAME is the filename used for communication. */
1931 remote_open (char *name, int from_tty)
1933 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
1936 /* Just like remote_open, but with asynchronous support. */
1938 remote_async_open (char *name, int from_tty)
1940 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
1943 /* Open a connection to a remote debugger using the extended
1944 remote gdb protocol. NAME is the filename used for communication. */
1947 extended_remote_open (char *name, int from_tty)
1949 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
1953 /* Just like extended_remote_open, but with asynchronous support. */
1955 extended_remote_async_open (char *name, int from_tty)
1957 remote_open_1 (name, from_tty, &extended_async_remote_ops,
1958 1 /*extended_p */, 1 /* async_p */);
1961 /* Generic code for opening a connection to a remote target. */
1964 init_all_packet_configs (void)
1967 for (i = 0; i < PACKET_MAX; i++)
1968 update_packet_config (&remote_protocol_packets[i]);
1971 /* Symbol look-up. */
1974 remote_check_symbols (struct objfile *objfile)
1976 struct remote_state *rs = get_remote_state ();
1977 char *msg, *reply, *tmp;
1978 struct minimal_symbol *sym;
1981 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
1984 /* Allocate a message buffer. We can't reuse the input buffer in RS,
1985 because we need both at the same time. */
1986 msg = alloca (rs->remote_packet_size);
1990 /* Invite target to request symbol lookups. */
1992 putpkt ("qSymbol::");
1993 getpkt (&rs->buf, &rs->buf_size, 0);
1994 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
1996 while (strncmp (reply, "qSymbol:", 8) == 0)
1999 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
2001 sym = lookup_minimal_symbol (msg, NULL, NULL);
2003 xsnprintf (msg, rs->remote_packet_size, "qSymbol::%s", &reply[8]);
2005 xsnprintf (msg, rs->remote_packet_size, "qSymbol:%s:%s",
2006 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2009 getpkt (&rs->buf, &rs->buf_size, 0);
2013 static struct serial *
2014 remote_serial_open (char *name)
2016 static int udp_warning = 0;
2018 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2019 of in ser-tcp.c, because it is the remote protocol assuming that the
2020 serial connection is reliable and not the serial connection promising
2022 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2025 The remote protocol may be unreliable over UDP.\n\
2026 Some events may be lost, rendering further debugging impossible."));
2030 return serial_open (name);
2034 remote_open_1 (char *name, int from_tty, struct target_ops *target,
2035 int extended_p, int async_p)
2037 struct remote_state *rs = get_remote_state ();
2039 error (_("To open a remote debug connection, you need to specify what\n"
2040 "serial device is attached to the remote system\n"
2041 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2043 /* See FIXME above. */
2045 wait_forever_enabled_p = 1;
2047 reopen_exec_file ();
2050 target_preopen (from_tty);
2052 unpush_target (target);
2054 remote_desc = remote_serial_open (name);
2056 perror_with_name (name);
2058 if (baud_rate != -1)
2060 if (serial_setbaudrate (remote_desc, baud_rate))
2062 /* The requested speed could not be set. Error out to
2063 top level after closing remote_desc. Take care to
2064 set remote_desc to NULL to avoid closing remote_desc
2066 serial_close (remote_desc);
2068 perror_with_name (name);
2072 serial_raw (remote_desc);
2074 /* If there is something sitting in the buffer we might take it as a
2075 response to a command, which would be bad. */
2076 serial_flush_input (remote_desc);
2080 puts_filtered ("Remote debugging using ");
2081 puts_filtered (name);
2082 puts_filtered ("\n");
2084 push_target (target); /* Switch to using remote target now. */
2086 init_all_packet_configs ();
2088 general_thread = -2;
2089 continue_thread = -2;
2091 /* Probe for ability to use "ThreadInfo" query, as required. */
2092 use_threadinfo_query = 1;
2093 use_threadextra_query = 1;
2095 /* Without this, some commands which require an active target (such
2096 as kill) won't work. This variable serves (at least) double duty
2097 as both the pid of the target process (if it has such), and as a
2098 flag indicating that a target is active. These functions should
2099 be split out into seperate variables, especially since GDB will
2100 someday have a notion of debugging several processes. */
2102 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2106 /* With this target we start out by owning the terminal. */
2107 remote_async_terminal_ours_p = 1;
2109 /* FIXME: cagney/1999-09-23: During the initial connection it is
2110 assumed that the target is already ready and able to respond to
2111 requests. Unfortunately remote_start_remote() eventually calls
2112 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2113 around this. Eventually a mechanism that allows
2114 wait_for_inferior() to expect/get timeouts will be
2116 wait_forever_enabled_p = 0;
2119 /* First delete any symbols previously loaded from shared libraries. */
2120 no_shared_libraries (NULL, 0);
2122 /* Start the remote connection. If error() or QUIT, discard this
2123 target (we'd otherwise be in an inconsistent state) and then
2124 propogate the error on up the exception chain. This ensures that
2125 the caller doesn't stumble along blindly assuming that the
2126 function succeeded. The CLI doesn't have this problem but other
2127 UI's, such as MI do.
2129 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2130 this function should return an error indication letting the
2131 caller restore the previous state. Unfortunately the command
2132 ``target remote'' is directly wired to this function making that
2133 impossible. On a positive note, the CLI side of this problem has
2134 been fixed - the function set_cmd_context() makes it possible for
2135 all the ``target ....'' commands to share a common callback
2136 function. See cli-dump.c. */
2138 struct gdb_exception ex
2139 = catch_exception (uiout, remote_start_remote, NULL, RETURN_MASK_ALL);
2144 wait_forever_enabled_p = 1;
2145 throw_exception (ex);
2150 wait_forever_enabled_p = 1;
2154 /* Tell the remote that we are using the extended protocol. */
2156 getpkt (&rs->buf, &rs->buf_size, 0);
2159 post_create_inferior (¤t_target, from_tty);
2161 if (exec_bfd) /* No use without an exec file. */
2162 remote_check_symbols (symfile_objfile);
2165 /* This takes a program previously attached to and detaches it. After
2166 this is done, GDB can be used to debug some other program. We
2167 better not have left any breakpoints in the target program or it'll
2168 die when it hits one. */
2171 remote_detach (char *args, int from_tty)
2173 struct remote_state *rs = get_remote_state ();
2176 error (_("Argument given to \"detach\" when remotely debugging."));
2178 /* Tell the remote target to detach. */
2179 strcpy (rs->buf, "D");
2180 remote_send (&rs->buf, &rs->buf_size);
2182 /* Unregister the file descriptor from the event loop. */
2183 if (target_is_async_p ())
2184 serial_async (remote_desc, NULL, 0);
2186 target_mourn_inferior ();
2188 puts_filtered ("Ending remote debugging.\n");
2191 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2194 remote_disconnect (char *args, int from_tty)
2197 error (_("Argument given to \"detach\" when remotely debugging."));
2199 /* Unregister the file descriptor from the event loop. */
2200 if (target_is_async_p ())
2201 serial_async (remote_desc, NULL, 0);
2203 target_mourn_inferior ();
2205 puts_filtered ("Ending remote debugging.\n");
2208 /* Convert hex digit A to a number. */
2213 if (a >= '0' && a <= '9')
2215 else if (a >= 'a' && a <= 'f')
2216 return a - 'a' + 10;
2217 else if (a >= 'A' && a <= 'F')
2218 return a - 'A' + 10;
2220 error (_("Reply contains invalid hex digit %d"), a);
2224 hex2bin (const char *hex, gdb_byte *bin, int count)
2228 for (i = 0; i < count; i++)
2230 if (hex[0] == 0 || hex[1] == 0)
2232 /* Hex string is short, or of uneven length.
2233 Return the count that has been converted so far. */
2236 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2242 /* Convert number NIB to a hex digit. */
2250 return 'a' + nib - 10;
2254 bin2hex (const gdb_byte *bin, char *hex, int count)
2257 /* May use a length, or a nul-terminated string as input. */
2259 count = strlen ((char *) bin);
2261 for (i = 0; i < count; i++)
2263 *hex++ = tohex ((*bin >> 4) & 0xf);
2264 *hex++ = tohex (*bin++ & 0xf);
2270 /* Check for the availability of vCont. This function should also check
2274 remote_vcont_probe (struct remote_state *rs)
2276 char *buf = rs->buf;
2278 strcpy (buf, "vCont?");
2280 getpkt (&rs->buf, &rs->buf_size, 0);
2282 /* Make sure that the features we assume are supported. */
2283 if (strncmp (buf, "vCont", 5) == 0)
2286 int support_s, support_S, support_c, support_C;
2292 while (p && *p == ';')
2295 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2297 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2299 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2301 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2304 p = strchr (p, ';');
2307 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2308 BUF will make packet_ok disable the packet. */
2309 if (!support_s || !support_S || !support_c || !support_C)
2313 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
2316 /* Resume the remote inferior by using a "vCont" packet. The thread
2317 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2318 resumed thread should be single-stepped and/or signalled. If PTID's
2319 PID is -1, then all threads are resumed; the thread to be stepped and/or
2320 signalled is given in the global INFERIOR_PTID. This function returns
2321 non-zero iff it resumes the inferior.
2323 This function issues a strict subset of all possible vCont commands at the
2327 remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2329 struct remote_state *rs = get_remote_state ();
2330 int pid = PIDGET (ptid);
2331 char *buf = NULL, *outbuf;
2332 struct cleanup *old_cleanup;
2334 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
2335 remote_vcont_probe (rs);
2337 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
2340 /* If we could generate a wider range of packets, we'd have to worry
2341 about overflowing BUF. Should there be a generic
2342 "multi-part-packet" packet? */
2344 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
2346 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2347 don't have any PID numbers the inferior will understand. Make sure
2348 to only send forms that do not specify a PID. */
2349 if (step && siggnal != TARGET_SIGNAL_0)
2350 outbuf = xstrprintf ("vCont;S%02x", siggnal);
2352 outbuf = xstrprintf ("vCont;s");
2353 else if (siggnal != TARGET_SIGNAL_0)
2354 outbuf = xstrprintf ("vCont;C%02x", siggnal);
2356 outbuf = xstrprintf ("vCont;c");
2360 /* Resume all threads, with preference for INFERIOR_PTID. */
2361 if (step && siggnal != TARGET_SIGNAL_0)
2362 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2363 PIDGET (inferior_ptid));
2365 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
2366 else if (siggnal != TARGET_SIGNAL_0)
2367 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2368 PIDGET (inferior_ptid));
2370 outbuf = xstrprintf ("vCont;c");
2374 /* Scheduler locking; resume only PTID. */
2375 if (step && siggnal != TARGET_SIGNAL_0)
2376 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
2378 outbuf = xstrprintf ("vCont;s:%x", pid);
2379 else if (siggnal != TARGET_SIGNAL_0)
2380 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
2382 outbuf = xstrprintf ("vCont;c:%x", pid);
2385 gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size);
2386 old_cleanup = make_cleanup (xfree, outbuf);
2390 do_cleanups (old_cleanup);
2395 /* Tell the remote machine to resume. */
2397 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2399 static int last_sent_step;
2402 remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
2404 struct remote_state *rs = get_remote_state ();
2405 char *buf = rs->buf;
2406 int pid = PIDGET (ptid);
2408 last_sent_signal = siggnal;
2409 last_sent_step = step;
2411 /* A hook for when we need to do something at the last moment before
2413 if (deprecated_target_resume_hook)
2414 (*deprecated_target_resume_hook) ();
2416 /* The vCont packet doesn't need to specify threads via Hc. */
2417 if (remote_vcont_resume (ptid, step, siggnal))
2420 /* All other supported resume packets do use Hc, so call set_thread. */
2422 set_thread (0, 0); /* Run any thread. */
2424 set_thread (pid, 0); /* Run this thread. */
2426 if (siggnal != TARGET_SIGNAL_0)
2428 buf[0] = step ? 'S' : 'C';
2429 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2430 buf[2] = tohex (((int) siggnal) & 0xf);
2434 strcpy (buf, step ? "s" : "c");
2439 /* Same as remote_resume, but with async support. */
2441 remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2443 remote_resume (ptid, step, siggnal);
2445 /* We are about to start executing the inferior, let's register it
2446 with the event loop. NOTE: this is the one place where all the
2447 execution commands end up. We could alternatively do this in each
2448 of the execution commands in infcmd.c. */
2449 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2450 into infcmd.c in order to allow inferior function calls to work
2451 NOT asynchronously. */
2452 if (target_can_async_p ())
2453 target_async (inferior_event_handler, 0);
2454 /* Tell the world that the target is now executing. */
2455 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2456 this? Instead, should the client of target just assume (for
2457 async targets) that the target is going to start executing? Is
2458 this information already found in the continuation block? */
2459 if (target_is_async_p ())
2460 target_executing = 1;
2464 /* Set up the signal handler for SIGINT, while the target is
2465 executing, ovewriting the 'regular' SIGINT signal handler. */
2467 initialize_sigint_signal_handler (void)
2469 sigint_remote_token =
2470 create_async_signal_handler (async_remote_interrupt, NULL);
2471 signal (SIGINT, handle_remote_sigint);
2474 /* Signal handler for SIGINT, while the target is executing. */
2476 handle_remote_sigint (int sig)
2478 signal (sig, handle_remote_sigint_twice);
2479 sigint_remote_twice_token =
2480 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2481 mark_async_signal_handler_wrapper (sigint_remote_token);
2484 /* Signal handler for SIGINT, installed after SIGINT has already been
2485 sent once. It will take effect the second time that the user sends
2488 handle_remote_sigint_twice (int sig)
2490 signal (sig, handle_sigint);
2491 sigint_remote_twice_token =
2492 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2493 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2496 /* Perform the real interruption of the target execution, in response
2499 async_remote_interrupt (gdb_client_data arg)
2502 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2507 /* Perform interrupt, if the first attempt did not succeed. Just give
2508 up on the target alltogether. */
2510 async_remote_interrupt_twice (gdb_client_data arg)
2513 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2514 /* Do something only if the target was not killed by the previous
2516 if (target_executing)
2519 signal (SIGINT, handle_remote_sigint);
2523 /* Reinstall the usual SIGINT handlers, after the target has
2526 cleanup_sigint_signal_handler (void *dummy)
2528 signal (SIGINT, handle_sigint);
2529 if (sigint_remote_twice_token)
2530 delete_async_signal_handler ((struct async_signal_handler **)
2531 &sigint_remote_twice_token);
2532 if (sigint_remote_token)
2533 delete_async_signal_handler ((struct async_signal_handler **)
2534 &sigint_remote_token);
2537 /* Send ^C to target to halt it. Target will respond, and send us a
2539 static void (*ofunc) (int);
2541 /* The command line interface's stop routine. This function is installed
2542 as a signal handler for SIGINT. The first time a user requests a
2543 stop, we call remote_stop to send a break or ^C. If there is no
2544 response from the target (it didn't stop when the user requested it),
2545 we ask the user if he'd like to detach from the target. */
2547 remote_interrupt (int signo)
2549 /* If this doesn't work, try more severe steps. */
2550 signal (signo, remote_interrupt_twice);
2553 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2558 /* The user typed ^C twice. */
2561 remote_interrupt_twice (int signo)
2563 signal (signo, ofunc);
2565 signal (signo, remote_interrupt);
2568 /* This is the generic stop called via the target vector. When a target
2569 interrupt is requested, either by the command line or the GUI, we
2570 will eventually end up here. */
2574 /* Send a break or a ^C, depending on user preference. */
2576 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2579 serial_send_break (remote_desc);
2581 serial_write (remote_desc, "\003", 1);
2584 /* Ask the user what to do when an interrupt is received. */
2587 interrupt_query (void)
2589 target_terminal_ours ();
2591 if (query ("Interrupted while waiting for the program.\n\
2592 Give up (and stop debugging it)? "))
2594 target_mourn_inferior ();
2595 deprecated_throw_reason (RETURN_QUIT);
2598 target_terminal_inferior ();
2601 /* Enable/disable target terminal ownership. Most targets can use
2602 terminal groups to control terminal ownership. Remote targets are
2603 different in that explicit transfer of ownership to/from GDB/target
2607 remote_async_terminal_inferior (void)
2609 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2610 sync_execution here. This function should only be called when
2611 GDB is resuming the inferior in the forground. A background
2612 resume (``run&'') should leave GDB in control of the terminal and
2613 consequently should not call this code. */
2614 if (!sync_execution)
2616 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2617 calls target_terminal_*() idenpotent. The event-loop GDB talking
2618 to an asynchronous target with a synchronous command calls this
2619 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2620 stops trying to transfer the terminal to the target when it
2621 shouldn't this guard can go away. */
2622 if (!remote_async_terminal_ours_p)
2624 delete_file_handler (input_fd);
2625 remote_async_terminal_ours_p = 0;
2626 initialize_sigint_signal_handler ();
2627 /* NOTE: At this point we could also register our selves as the
2628 recipient of all input. Any characters typed could then be
2629 passed on down to the target. */
2633 remote_async_terminal_ours (void)
2635 /* See FIXME in remote_async_terminal_inferior. */
2636 if (!sync_execution)
2638 /* See FIXME in remote_async_terminal_inferior. */
2639 if (remote_async_terminal_ours_p)
2641 cleanup_sigint_signal_handler (NULL);
2642 add_file_handler (input_fd, stdin_event_handler, 0);
2643 remote_async_terminal_ours_p = 1;
2646 /* If nonzero, ignore the next kill. */
2651 remote_console_output (char *msg)
2655 for (p = msg; p[0] && p[1]; p += 2)
2658 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2661 fputs_unfiltered (tb, gdb_stdtarg);
2663 gdb_flush (gdb_stdtarg);
2666 /* Wait until the remote machine stops, then return,
2667 storing status in STATUS just as `wait' would.
2668 Returns "pid", which in the case of a multi-threaded
2669 remote OS, is the thread-id. */
2672 remote_wait (ptid_t ptid, struct target_waitstatus *status)
2674 struct remote_state *rs = get_remote_state ();
2675 char *buf = rs->buf;
2676 ULONGEST thread_num = -1;
2679 status->kind = TARGET_WAITKIND_EXITED;
2680 status->value.integer = 0;
2686 ofunc = signal (SIGINT, remote_interrupt);
2687 getpkt (&rs->buf, &rs->buf_size, 1);
2688 signal (SIGINT, ofunc);
2690 /* This is a hook for when we need to do something (perhaps the
2691 collection of trace data) every time the target stops. */
2692 if (deprecated_target_wait_loop_hook)
2693 (*deprecated_target_wait_loop_hook) ();
2695 remote_stopped_by_watchpoint_p = 0;
2699 case 'E': /* Error of some sort. */
2700 warning (_("Remote failure reply: %s"), buf);
2702 case 'F': /* File-I/O request. */
2703 remote_fileio_request (buf);
2705 case 'T': /* Status with PC, SP, FP, ... */
2707 gdb_byte regs[MAX_REGISTER_SIZE];
2709 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2710 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2712 n... = register number
2713 r... = register contents
2715 p = &buf[3]; /* after Txx */
2724 /* If the packet contains a register number save it in
2725 pnum and set p1 to point to the character following
2726 it. Otherwise p1 points to p. */
2728 /* If this packet is an awatch packet, don't parse the
2729 'a' as a register number. */
2731 if (strncmp (p, "awatch", strlen("awatch")) != 0)
2733 /* Read the ``P'' register number. */
2734 pnum = strtol (p, &p_temp, 16);
2740 if (p1 == p) /* No register number present here. */
2742 p1 = strchr (p, ':');
2744 warning (_("Malformed packet(a) (missing colon): %s\n\
2747 if (strncmp (p, "thread", p1 - p) == 0)
2749 p_temp = unpack_varlen_hex (++p1, &thread_num);
2750 record_currthread (thread_num);
2753 else if ((strncmp (p, "watch", p1 - p) == 0)
2754 || (strncmp (p, "rwatch", p1 - p) == 0)
2755 || (strncmp (p, "awatch", p1 - p) == 0))
2757 remote_stopped_by_watchpoint_p = 1;
2758 p = unpack_varlen_hex (++p1, &addr);
2759 remote_watch_data_address = (CORE_ADDR)addr;
2763 /* Silently skip unknown optional info. */
2764 p_temp = strchr (p1 + 1, ';');
2771 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
2775 error (_("Malformed packet(b) (missing colon): %s\n\
2780 error (_("Remote sent bad register number %s: %s\n\
2782 phex_nz (pnum, 0), p, buf);
2784 fieldsize = hex2bin (p, regs,
2785 register_size (current_gdbarch,
2788 if (fieldsize < register_size (current_gdbarch,
2790 warning (_("Remote reply is too short: %s"), buf);
2791 regcache_raw_supply (current_regcache,
2796 error (_("Remote register badly formatted: %s\nhere: %s"),
2801 case 'S': /* Old style status, just signal only. */
2802 status->kind = TARGET_WAITKIND_STOPPED;
2803 status->value.sig = (enum target_signal)
2804 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2808 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2809 record_currthread (thread_num);
2812 case 'W': /* Target exited. */
2814 /* The remote process exited. */
2815 status->kind = TARGET_WAITKIND_EXITED;
2816 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2820 status->kind = TARGET_WAITKIND_SIGNALLED;
2821 status->value.sig = (enum target_signal)
2822 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2826 case 'O': /* Console output. */
2827 remote_console_output (buf + 1);
2830 if (last_sent_signal != TARGET_SIGNAL_0)
2832 /* Zero length reply means that we tried 'S' or 'C' and
2833 the remote system doesn't support it. */
2834 target_terminal_ours_for_output ();
2836 ("Can't send signals to this remote system. %s not sent.\n",
2837 target_signal_to_name (last_sent_signal));
2838 last_sent_signal = TARGET_SIGNAL_0;
2839 target_terminal_inferior ();
2841 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2842 putpkt ((char *) buf);
2845 /* else fallthrough */
2847 warning (_("Invalid remote reply: %s"), buf);
2852 if (thread_num != -1)
2854 return pid_to_ptid (thread_num);
2856 return inferior_ptid;
2859 /* Async version of remote_wait. */
2861 remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
2863 struct remote_state *rs = get_remote_state ();
2864 char *buf = rs->buf;
2865 ULONGEST thread_num = -1;
2868 status->kind = TARGET_WAITKIND_EXITED;
2869 status->value.integer = 0;
2871 remote_stopped_by_watchpoint_p = 0;
2877 if (!target_is_async_p ())
2878 ofunc = signal (SIGINT, remote_interrupt);
2879 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2880 _never_ wait for ever -> test on target_is_async_p().
2881 However, before we do that we need to ensure that the caller
2882 knows how to take the target into/out of async mode. */
2883 getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
2884 if (!target_is_async_p ())
2885 signal (SIGINT, ofunc);
2887 /* This is a hook for when we need to do something (perhaps the
2888 collection of trace data) every time the target stops. */
2889 if (deprecated_target_wait_loop_hook)
2890 (*deprecated_target_wait_loop_hook) ();
2894 case 'E': /* Error of some sort. */
2895 warning (_("Remote failure reply: %s"), buf);
2897 case 'F': /* File-I/O request. */
2898 remote_fileio_request (buf);
2900 case 'T': /* Status with PC, SP, FP, ... */
2902 gdb_byte regs[MAX_REGISTER_SIZE];
2904 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2905 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2907 n... = register number
2908 r... = register contents
2910 p = &buf[3]; /* after Txx */
2919 /* If the packet contains a register number, save it
2920 in pnum and set p1 to point to the character
2921 following it. Otherwise p1 points to p. */
2923 /* If this packet is an awatch packet, don't parse the 'a'
2924 as a register number. */
2926 if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
2928 /* Read the register number. */
2929 pnum = strtol (p, &p_temp, 16);
2935 if (p1 == p) /* No register number present here. */
2937 p1 = strchr (p, ':');
2939 error (_("Malformed packet(a) (missing colon): %s\n\
2942 if (strncmp (p, "thread", p1 - p) == 0)
2944 p_temp = unpack_varlen_hex (++p1, &thread_num);
2945 record_currthread (thread_num);
2948 else if ((strncmp (p, "watch", p1 - p) == 0)
2949 || (strncmp (p, "rwatch", p1 - p) == 0)
2950 || (strncmp (p, "awatch", p1 - p) == 0))
2952 remote_stopped_by_watchpoint_p = 1;
2953 p = unpack_varlen_hex (++p1, &addr);
2954 remote_watch_data_address = (CORE_ADDR)addr;
2958 /* Silently skip unknown optional info. */
2959 p_temp = strchr (p1 + 1, ';');
2967 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
2970 error (_("Malformed packet(b) (missing colon): %s\n\
2975 error (_("Remote sent bad register number %ld: %s\n\
2979 fieldsize = hex2bin (p, regs,
2980 register_size (current_gdbarch,
2983 if (fieldsize < register_size (current_gdbarch,
2985 warning (_("Remote reply is too short: %s"), buf);
2986 regcache_raw_supply (current_regcache, reg->regnum, regs);
2990 error (_("Remote register badly formatted: %s\nhere: %s"),
2995 case 'S': /* Old style status, just signal only. */
2996 status->kind = TARGET_WAITKIND_STOPPED;
2997 status->value.sig = (enum target_signal)
2998 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3002 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3003 record_currthread (thread_num);
3006 case 'W': /* Target exited. */
3008 /* The remote process exited. */
3009 status->kind = TARGET_WAITKIND_EXITED;
3010 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3014 status->kind = TARGET_WAITKIND_SIGNALLED;
3015 status->value.sig = (enum target_signal)
3016 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3020 case 'O': /* Console output. */
3021 remote_console_output (buf + 1);
3022 /* Return immediately to the event loop. The event loop will
3023 still be waiting on the inferior afterwards. */
3024 status->kind = TARGET_WAITKIND_IGNORE;
3027 if (last_sent_signal != TARGET_SIGNAL_0)
3029 /* Zero length reply means that we tried 'S' or 'C' and
3030 the remote system doesn't support it. */
3031 target_terminal_ours_for_output ();
3033 ("Can't send signals to this remote system. %s not sent.\n",
3034 target_signal_to_name (last_sent_signal));
3035 last_sent_signal = TARGET_SIGNAL_0;
3036 target_terminal_inferior ();
3038 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3039 putpkt ((char *) buf);
3042 /* else fallthrough */
3044 warning (_("Invalid remote reply: %s"), buf);
3049 if (thread_num != -1)
3051 return pid_to_ptid (thread_num);
3053 return inferior_ptid;
3056 /* Number of bytes of registers this stub implements. */
3058 static int register_bytes_found;
3060 /* Read the remote registers into the block REGS. */
3061 /* Currently we just read all the registers, so we don't use regnum. */
3064 fetch_register_using_p (int regnum)
3066 struct remote_state *rs = get_remote_state ();
3067 char *buf = rs->buf, *p;
3068 char regp[MAX_REGISTER_SIZE];
3073 p += hexnumstr (p, regnum);
3075 remote_send (&rs->buf, &rs->buf_size);
3077 /* If the stub didn't recognize the packet, or if we got an error,
3079 if (buf[0] == '\0' || buf[0] == 'E')
3082 /* If this register is unfetchable, tell the regcache. */
3085 regcache_raw_supply (current_regcache, regnum, NULL);
3086 set_register_cached (regnum, -1);
3090 /* Otherwise, parse and supply the value. */
3097 error (_("fetch_register_using_p: early buf termination"));
3101 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3104 regcache_raw_supply (current_regcache, regnum, regp);
3109 remote_fetch_registers (int regnum)
3111 struct remote_state *rs = get_remote_state ();
3112 char *buf = rs->buf;
3115 char *regs = alloca (rs->sizeof_g_packet);
3117 set_thread (PIDGET (inferior_ptid), 1);
3121 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3122 gdb_assert (reg != NULL);
3123 if (!reg->in_g_packet)
3124 internal_error (__FILE__, __LINE__,
3125 _("Attempt to fetch a non G-packet register when this "
3126 "remote.c does not support the p-packet."));
3128 switch (remote_protocol_packets[PACKET_p].support)
3130 case PACKET_DISABLE:
3133 if (fetch_register_using_p (regnum))
3136 error (_("Protocol error: p packet not recognized by stub"));
3137 case PACKET_SUPPORT_UNKNOWN:
3138 if (fetch_register_using_p (regnum))
3140 /* The stub recognized the 'p' packet. Remember this. */
3141 remote_protocol_packets[PACKET_p].support = PACKET_ENABLE;
3146 /* The stub does not support the 'P' packet. Use 'G'
3147 instead, and don't try using 'P' in the future (it
3148 will just waste our time). */
3149 remote_protocol_packets[PACKET_p].support = PACKET_DISABLE;
3155 remote_send (&rs->buf, &rs->buf_size);
3157 /* Save the size of the packet sent to us by the target. Its used
3158 as a heuristic when determining the max size of packets that the
3159 target can safely receive. */
3160 if ((rs->actual_register_packet_size) == 0)
3161 (rs->actual_register_packet_size) = strlen (buf);
3163 /* Unimplemented registers read as all bits zero. */
3164 memset (regs, 0, rs->sizeof_g_packet);
3166 /* We can get out of synch in various cases. If the first character
3167 in the buffer is not a hex character, assume that has happened
3168 and try to fetch another packet to read. */
3169 while ((buf[0] < '0' || buf[0] > '9')
3170 && (buf[0] < 'A' || buf[0] > 'F')
3171 && (buf[0] < 'a' || buf[0] > 'f')
3172 && buf[0] != 'x') /* New: unavailable register value. */
3175 fprintf_unfiltered (gdb_stdlog,
3176 "Bad register packet; fetching a new packet\n");
3177 getpkt (&rs->buf, &rs->buf_size, 0);
3180 /* Reply describes registers byte by byte, each byte encoded as two
3181 hex characters. Suck them all up, then supply them to the
3182 register cacheing/storage mechanism. */
3185 for (i = 0; i < rs->sizeof_g_packet; i++)
3191 warning (_("Remote reply is of odd length: %s"), buf);
3192 /* Don't change register_bytes_found in this case, and don't
3193 print a second warning. */
3196 if (p[0] == 'x' && p[1] == 'x')
3197 regs[i] = 0; /* 'x' */
3199 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3203 if (i != register_bytes_found)
3205 register_bytes_found = i;
3206 if (REGISTER_BYTES_OK_P ()
3207 && !REGISTER_BYTES_OK (i))
3208 warning (_("Remote reply is too short: %s"), buf);
3214 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
3216 struct packet_reg *r = &rs->regs[i];
3219 if (r->offset * 2 >= strlen (buf))
3220 /* A short packet that didn't include the register's
3221 value, this implies that the register is zero (and
3222 not that the register is unavailable). Supply that
3224 regcache_raw_supply (current_regcache, r->regnum, NULL);
3225 else if (buf[r->offset * 2] == 'x')
3227 gdb_assert (r->offset * 2 < strlen (buf));
3228 /* The register isn't available, mark it as such (at
3229 the same time setting the value to zero). */
3230 regcache_raw_supply (current_regcache, r->regnum, NULL);
3231 set_register_cached (i, -1);
3234 regcache_raw_supply (current_regcache, r->regnum,
3241 /* Prepare to store registers. Since we may send them all (using a
3242 'G' request), we have to read out the ones we don't want to change
3246 remote_prepare_to_store (void)
3248 struct remote_state *rs = get_remote_state ();
3250 gdb_byte buf[MAX_REGISTER_SIZE];
3252 /* Make sure the entire registers array is valid. */
3253 switch (remote_protocol_packets[PACKET_P].support)
3255 case PACKET_DISABLE:
3256 case PACKET_SUPPORT_UNKNOWN:
3257 /* Make sure all the necessary registers are cached. */
3258 for (i = 0; i < NUM_REGS; i++)
3259 if (rs->regs[i].in_g_packet)
3260 regcache_raw_read (current_regcache, rs->regs[i].regnum, buf);
3267 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3268 packet was not recognized. */
3271 store_register_using_P (int regnum)
3273 struct remote_state *rs = get_remote_state ();
3274 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3275 /* Try storing a single register. */
3276 char *buf = rs->buf;
3277 gdb_byte regp[MAX_REGISTER_SIZE];
3280 xsnprintf (buf, rs->remote_packet_size, "P%s=", phex_nz (reg->pnum, 0));
3281 p = buf + strlen (buf);
3282 regcache_raw_collect (current_regcache, reg->regnum, regp);
3283 bin2hex (regp, p, register_size (current_gdbarch, reg->regnum));
3284 remote_send (&rs->buf, &rs->buf_size);
3286 return buf[0] != '\0';
3290 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3291 contents of the register cache buffer. FIXME: ignores errors. */
3294 remote_store_registers (int regnum)
3296 struct remote_state *rs = get_remote_state ();
3300 set_thread (PIDGET (inferior_ptid), 1);
3304 switch (remote_protocol_packets[PACKET_P].support)
3306 case PACKET_DISABLE:
3309 if (store_register_using_P (regnum))
3312 error (_("Protocol error: P packet not recognized by stub"));
3313 case PACKET_SUPPORT_UNKNOWN:
3314 if (store_register_using_P (regnum))
3316 /* The stub recognized the 'P' packet. Remember this. */
3317 remote_protocol_packets[PACKET_P].support = PACKET_ENABLE;
3322 /* The stub does not support the 'P' packet. Use 'G'
3323 instead, and don't try using 'P' in the future (it
3324 will just waste our time). */
3325 remote_protocol_packets[PACKET_P].support = PACKET_DISABLE;
3331 /* Extract all the registers in the regcache copying them into a
3335 regs = alloca (rs->sizeof_g_packet);
3336 memset (regs, 0, rs->sizeof_g_packet);
3337 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
3339 struct packet_reg *r = &rs->regs[i];
3341 regcache_raw_collect (current_regcache, r->regnum, regs + r->offset);
3345 /* Command describes registers byte by byte,
3346 each byte encoded as two hex characters. */
3349 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3350 bin2hex (regs, p, register_bytes_found);
3351 remote_send (&rs->buf, &rs->buf_size);
3355 /* Return the number of hex digits in num. */
3358 hexnumlen (ULONGEST num)
3362 for (i = 0; num != 0; i++)
3368 /* Set BUF to the minimum number of hex digits representing NUM. */
3371 hexnumstr (char *buf, ULONGEST num)
3373 int len = hexnumlen (num);
3374 return hexnumnstr (buf, num, len);
3378 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3381 hexnumnstr (char *buf, ULONGEST num, int width)
3387 for (i = width - 1; i >= 0; i--)
3389 buf[i] = "0123456789abcdef"[(num & 0xf)];
3396 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3399 remote_address_masked (CORE_ADDR addr)
3401 if (remote_address_size > 0
3402 && remote_address_size < (sizeof (ULONGEST) * 8))
3404 /* Only create a mask when that mask can safely be constructed
3405 in a ULONGEST variable. */
3407 mask = (mask << remote_address_size) - 1;
3413 /* Determine whether the remote target supports binary downloading.
3414 This is accomplished by sending a no-op memory write of zero length
3415 to the target at the specified address. It does not suffice to send
3416 the whole packet, since many stubs strip the eighth bit and
3417 subsequently compute a wrong checksum, which causes real havoc with
3420 NOTE: This can still lose if the serial line is not eight-bit
3421 clean. In cases like this, the user should clear "remote
3425 check_binary_download (CORE_ADDR addr)
3427 struct remote_state *rs = get_remote_state ();
3429 switch (remote_protocol_packets[PACKET_X].support)
3431 case PACKET_DISABLE:
3435 case PACKET_SUPPORT_UNKNOWN:
3437 char *buf = rs->buf;
3442 p += hexnumstr (p, (ULONGEST) addr);
3444 p += hexnumstr (p, (ULONGEST) 0);
3448 putpkt_binary (buf, (int) (p - buf));
3449 getpkt (&rs->buf, &rs->buf_size, 0);
3454 fprintf_unfiltered (gdb_stdlog,
3455 "binary downloading NOT suppported by target\n");
3456 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
3461 fprintf_unfiltered (gdb_stdlog,
3462 "binary downloading suppported by target\n");
3463 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
3470 /* Write memory data directly to the remote machine.
3471 This does not inform the data cache; the data cache uses this.
3472 MEMADDR is the address in the remote memory space.
3473 MYADDR is the address of the buffer in our space.
3474 LEN is the number of bytes.
3476 Returns number of bytes transferred, or 0 (setting errno) for
3477 error. Only transfer a single packet. */
3480 remote_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
3482 struct remote_state *rs = get_remote_state ();
3490 char *payload_start;
3492 /* Verify that the target can support a binary download. */
3493 check_binary_download (memaddr);
3495 payload_size = get_memory_write_packet_size ();
3497 /* The packet buffer will be large enough for the payload;
3498 get_memory_packet_size ensures this. */
3501 /* Compute the size of the actual payload by subtracting out the
3502 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3504 payload_size -= strlen ("$M,:#NN");
3505 payload_size -= hexnumlen (memaddr);
3507 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3509 /* Append "[XM]". Compute a best guess of the number of bytes
3510 actually transfered. */
3512 switch (remote_protocol_packets[PACKET_X].support)
3516 /* Best guess at number of bytes that will fit. */
3517 todo = min (len, payload_size);
3518 payload_size -= hexnumlen (todo);
3519 todo = min (todo, payload_size);
3521 case PACKET_DISABLE:
3523 /* Num bytes that will fit. */
3524 todo = min (len, payload_size / 2);
3525 payload_size -= hexnumlen (todo);
3526 todo = min (todo, payload_size / 2);
3528 case PACKET_SUPPORT_UNKNOWN:
3529 internal_error (__FILE__, __LINE__,
3530 _("remote_write_bytes: bad internal state"));
3532 internal_error (__FILE__, __LINE__, _("bad switch"));
3535 internal_error (__FILE__, __LINE__,
3536 _("minumum packet size too small to write data"));
3538 /* Append "<memaddr>". */
3539 memaddr = remote_address_masked (memaddr);
3540 p += hexnumstr (p, (ULONGEST) memaddr);
3545 /* Append <len>. Retain the location/size of <len>. It may need to
3546 be adjusted once the packet body has been created. */
3548 plenlen = hexnumstr (p, (ULONGEST) todo);
3555 /* Append the packet body. */
3557 switch (remote_protocol_packets[PACKET_X].support)
3560 /* Binary mode. Send target system values byte by byte, in
3561 increasing byte addresses. Only escape certain critical
3564 (nr_bytes < todo) && (p - payload_start) < payload_size;
3567 switch (myaddr[nr_bytes] & 0xff)
3572 /* These must be escaped. */
3574 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3577 *p++ = myaddr[nr_bytes] & 0xff;
3581 if (nr_bytes < todo)
3583 /* Escape chars have filled up the buffer prematurely,
3584 and we have actually sent fewer bytes than planned.
3585 Fix-up the length field of the packet. Use the same
3586 number of characters as before. */
3587 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3588 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3591 case PACKET_DISABLE:
3592 /* Normal mode: Send target system values byte by byte, in
3593 increasing byte addresses. Each byte is encoded as a two hex
3595 nr_bytes = bin2hex (myaddr, p, todo);
3598 case PACKET_SUPPORT_UNKNOWN:
3599 internal_error (__FILE__, __LINE__,
3600 _("remote_write_bytes: bad internal state"));
3602 internal_error (__FILE__, __LINE__, _("bad switch"));
3605 putpkt_binary (buf, (int) (p - buf));
3606 getpkt (&rs->buf, &rs->buf_size, 0);
3610 /* There is no correspondance between what the remote protocol
3611 uses for errors and errno codes. We would like a cleaner way
3612 of representing errors (big enough to include errno codes,
3613 bfd_error codes, and others). But for now just return EIO. */
3618 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3619 fewer bytes than we'd planned. */
3623 /* Read memory data directly from the remote machine.
3624 This does not use the data cache; the data cache uses this.
3625 MEMADDR is the address in the remote memory space.
3626 MYADDR is the address of the buffer in our space.
3627 LEN is the number of bytes.
3629 Returns number of bytes transferred, or 0 for error. */
3631 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3632 remote targets) shouldn't attempt to read the entire buffer.
3633 Instead it should read a single packet worth of data and then
3634 return the byte size of that packet to the caller. The caller (its
3635 caller and its callers caller ;-) already contains code for
3636 handling partial reads. */
3639 remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
3641 struct remote_state *rs = get_remote_state ();
3643 int max_buf_size; /* Max size of packet output buffer. */
3646 max_buf_size = get_memory_read_packet_size ();
3647 /* The packet buffer will be large enough for the payload;
3648 get_memory_packet_size ensures this. */
3658 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3660 /* construct "m"<memaddr>","<len>" */
3661 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3662 memaddr = remote_address_masked (memaddr);
3665 p += hexnumstr (p, (ULONGEST) memaddr);
3667 p += hexnumstr (p, (ULONGEST) todo);
3671 getpkt (&rs->buf, &rs->buf_size, 0);
3674 && isxdigit (buf[1]) && isxdigit (buf[2])
3677 /* There is no correspondance between what the remote
3678 protocol uses for errors and errno codes. We would like
3679 a cleaner way of representing errors (big enough to
3680 include errno codes, bfd_error codes, and others). But
3681 for now just return EIO. */
3686 /* Reply describes memory byte by byte,
3687 each byte encoded as two hex characters. */
3690 if ((i = hex2bin (p, myaddr, todo)) < todo)
3692 /* Reply is short. This means that we were able to read
3693 only part of what we wanted to. */
3694 return i + (origlen - len);
3703 /* Read or write LEN bytes from inferior memory at MEMADDR,
3704 transferring to or from debugger address BUFFER. Write to inferior
3705 if SHOULD_WRITE is nonzero. Returns length of data written or
3706 read; 0 for error. TARGET is unused. */
3709 remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
3710 int should_write, struct mem_attrib *attrib,
3711 struct target_ops *target)
3713 CORE_ADDR targ_addr;
3717 /* Should this be the selected frame? */
3718 gdbarch_remote_translate_xfer_address (current_gdbarch,
3721 &targ_addr, &targ_len);
3726 res = remote_write_bytes (targ_addr, buffer, targ_len);
3728 res = remote_read_bytes (targ_addr, buffer, targ_len);
3734 remote_files_info (struct target_ops *ignore)
3736 puts_filtered ("Debugging a target over a serial line.\n");
3739 /* Stuff for dealing with the packets which are part of this protocol.
3740 See comment at top of file for details. */
3742 /* Read a single character from the remote end, masking it down to 7
3746 readchar (int timeout)
3750 ch = serial_readchar (remote_desc, timeout);
3755 switch ((enum serial_rc) ch)
3758 target_mourn_inferior ();
3759 error (_("Remote connection closed"));
3762 perror_with_name (_("Remote communication error"));
3764 case SERIAL_TIMEOUT:
3770 /* Send the command in *BUF to the remote machine, and read the reply
3771 into *BUF. Report an error if we get an error reply. Resize
3772 *BUF using xrealloc if necessary to hold the result, and update
3776 remote_send (char **buf,
3780 getpkt (buf, sizeof_buf, 0);
3782 if ((*buf)[0] == 'E')
3783 error (_("Remote failure reply: %s"), *buf);
3786 /* Display a null-terminated packet on stdout, for debugging, using C
3790 print_packet (char *buf)
3792 puts_filtered ("\"");
3793 fputstr_filtered (buf, '"', gdb_stdout);
3794 puts_filtered ("\"");
3800 return putpkt_binary (buf, strlen (buf));
3803 /* Send a packet to the remote machine, with error checking. The data
3804 of the packet is in BUF. The string in BUF can be at most
3805 RS->remote_packet_size - 5 to account for the $, # and checksum,
3806 and for a possible /0 if we are debugging (remote_debug) and want
3807 to print the sent packet as a string. */
3810 putpkt_binary (char *buf, int cnt)
3812 struct remote_state *rs = get_remote_state ();
3814 unsigned char csum = 0;
3815 char *buf2 = alloca (cnt + 6);
3821 /* Copy the packet into buffer BUF2, encapsulating it
3822 and giving it a checksum. */
3827 for (i = 0; i < cnt; i++)
3833 *p++ = tohex ((csum >> 4) & 0xf);
3834 *p++ = tohex (csum & 0xf);
3836 /* Send it over and over until we get a positive ack. */
3840 int started_error_output = 0;
3845 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3846 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
3847 fprintf_unfiltered (gdb_stdlog, "...");
3848 gdb_flush (gdb_stdlog);
3850 if (serial_write (remote_desc, buf2, p - buf2))
3851 perror_with_name (_("putpkt: write failed"));
3853 /* Read until either a timeout occurs (-2) or '+' is read. */
3856 ch = readchar (remote_timeout);
3864 case SERIAL_TIMEOUT:
3866 if (started_error_output)
3868 putchar_unfiltered ('\n');
3869 started_error_output = 0;
3878 fprintf_unfiltered (gdb_stdlog, "Ack\n");
3882 fprintf_unfiltered (gdb_stdlog, "Nak\n");
3883 case SERIAL_TIMEOUT:
3887 break; /* Retransmit buffer. */
3891 fprintf_unfiltered (gdb_stdlog,
3892 "Packet instead of Ack, ignoring it\n");
3893 /* It's probably an old response sent because an ACK
3894 was lost. Gobble up the packet and ack it so it
3895 doesn't get retransmitted when we resend this
3898 serial_write (remote_desc, "+", 1);
3899 continue; /* Now, go look for +. */
3904 if (!started_error_output)
3906 started_error_output = 1;
3907 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
3909 fputc_unfiltered (ch & 0177, gdb_stdlog);
3913 break; /* Here to retransmit. */
3917 /* This is wrong. If doing a long backtrace, the user should be
3918 able to get out next time we call QUIT, without anything as
3919 violent as interrupt_query. If we want to provide a way out of
3920 here without getting to the next QUIT, it should be based on
3921 hitting ^C twice as in remote_wait. */
3931 /* Come here after finding the start of a frame when we expected an
3932 ack. Do our best to discard the rest of this packet. */
3941 c = readchar (remote_timeout);
3944 case SERIAL_TIMEOUT:
3945 /* Nothing we can do. */
3948 /* Discard the two bytes of checksum and stop. */
3949 c = readchar (remote_timeout);
3951 c = readchar (remote_timeout);
3954 case '*': /* Run length encoding. */
3955 /* Discard the repeat count. */
3956 c = readchar (remote_timeout);
3961 /* A regular character. */
3967 /* Come here after finding the start of the frame. Collect the rest
3968 into *BUF, verifying the checksum, length, and handling run-length
3969 compression. NUL terminate the buffer. If there is not enough room,
3970 expand *BUF using xrealloc.
3972 Returns -1 on error, number of characters in buffer (ignoring the
3973 trailing NULL) on success. (could be extended to return one of the
3974 SERIAL status indications). */
3977 read_frame (char **buf_p,
3990 c = readchar (remote_timeout);
3993 case SERIAL_TIMEOUT:
3995 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
3999 fputs_filtered ("Saw new packet start in middle of old one\n",
4001 return -1; /* Start a new packet, count retries. */
4004 unsigned char pktcsum;
4010 check_0 = readchar (remote_timeout);
4012 check_1 = readchar (remote_timeout);
4014 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4017 fputs_filtered ("Timeout in checksum, retrying\n",
4021 else if (check_0 < 0 || check_1 < 0)
4024 fputs_filtered ("Communication error in checksum\n",
4029 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
4030 if (csum == pktcsum)
4035 fprintf_filtered (gdb_stdlog,
4036 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4038 fputs_filtered (buf, gdb_stdlog);
4039 fputs_filtered ("\n", gdb_stdlog);
4041 /* Number of characters in buffer ignoring trailing
4045 case '*': /* Run length encoding. */
4050 c = readchar (remote_timeout);
4052 repeat = c - ' ' + 3; /* Compute repeat count. */
4054 /* The character before ``*'' is repeated. */
4056 if (repeat > 0 && repeat <= 255 && bc > 0)
4058 if (bc + repeat - 1 >= *sizeof_buf - 1)
4060 /* Make some more room in the buffer. */
4061 *sizeof_buf += repeat;
4062 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4066 memset (&buf[bc], buf[bc - 1], repeat);
4072 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
4076 if (bc >= *sizeof_buf - 1)
4078 /* Make some more room in the buffer. */
4080 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4091 /* Read a packet from the remote machine, with error checking, and
4092 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4093 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4094 rather than timing out; this is used (in synchronous mode) to wait
4095 for a target that is is executing user code to stop. */
4096 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4097 don't have to change all the calls to getpkt to deal with the
4098 return value, because at the moment I don't know what the right
4099 thing to do it for those. */
4107 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4111 /* Read a packet from the remote machine, with error checking, and
4112 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4113 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4114 rather than timing out; this is used (in synchronous mode) to wait
4115 for a target that is is executing user code to stop. If FOREVER ==
4116 0, this function is allowed to time out gracefully and return an
4117 indication of this to the caller. */
4119 getpkt_sane (char **buf, long *sizeof_buf, int forever)
4126 strcpy (*buf, "timeout");
4130 timeout = watchdog > 0 ? watchdog : -1;
4134 timeout = remote_timeout;
4138 for (tries = 1; tries <= MAX_TRIES; tries++)
4140 /* This can loop forever if the remote side sends us characters
4141 continuously, but if it pauses, we'll get a zero from
4142 readchar because of timeout. Then we'll count that as a
4145 /* Note that we will only wait forever prior to the start of a
4146 packet. After that, we expect characters to arrive at a
4147 brisk pace. They should show up within remote_timeout
4152 c = readchar (timeout);
4154 if (c == SERIAL_TIMEOUT)
4156 if (forever) /* Watchdog went off? Kill the target. */
4159 target_mourn_inferior ();
4160 error (_("Watchdog has expired. Target detached."));
4163 fputs_filtered ("Timed out.\n", gdb_stdlog);
4169 /* We've found the start of a packet, now collect the data. */
4171 val = read_frame (buf, sizeof_buf);
4177 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4178 fputstr_unfiltered (*buf, 0, gdb_stdlog);
4179 fprintf_unfiltered (gdb_stdlog, "\n");
4181 serial_write (remote_desc, "+", 1);
4185 /* Try the whole thing again. */
4187 serial_write (remote_desc, "-", 1);
4190 /* We have tried hard enough, and just can't receive the packet.
4193 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4194 serial_write (remote_desc, "+", 1);
4201 /* For some mysterious reason, wait_for_inferior calls kill instead of
4202 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4206 target_mourn_inferior ();
4210 /* Use catch_errors so the user can quit from gdb even when we aren't on
4211 speaking terms with the remote system. */
4212 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4214 /* Don't wait for it to die. I'm not really sure it matters whether
4215 we do or not. For the existing stubs, kill is a noop. */
4216 target_mourn_inferior ();
4219 /* Async version of remote_kill. */
4221 remote_async_kill (void)
4223 /* Unregister the file descriptor from the event loop. */
4224 if (target_is_async_p ())
4225 serial_async (remote_desc, NULL, 0);
4227 /* For some mysterious reason, wait_for_inferior calls kill instead of
4228 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4232 target_mourn_inferior ();
4236 /* Use catch_errors so the user can quit from gdb even when we
4237 aren't on speaking terms with the remote system. */
4238 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4240 /* Don't wait for it to die. I'm not really sure it matters whether
4241 we do or not. For the existing stubs, kill is a noop. */
4242 target_mourn_inferior ();
4248 remote_mourn_1 (&remote_ops);
4252 remote_async_mourn (void)
4254 remote_mourn_1 (&remote_async_ops);
4258 extended_remote_mourn (void)
4260 /* We do _not_ want to mourn the target like this; this will
4261 remove the extended remote target from the target stack,
4262 and the next time the user says "run" it'll fail.
4264 FIXME: What is the right thing to do here? */
4266 remote_mourn_1 (&extended_remote_ops);
4270 /* Worker function for remote_mourn. */
4272 remote_mourn_1 (struct target_ops *target)
4274 unpush_target (target);
4275 generic_mourn_inferior ();
4278 /* In the extended protocol we want to be able to do things like
4279 "run" and have them basically work as expected. So we need
4280 a special create_inferior function.
4282 FIXME: One day add support for changing the exec file
4283 we're debugging, arguments and an environment. */
4286 extended_remote_create_inferior (char *exec_file, char *args,
4287 char **env, int from_tty)
4289 /* Rip out the breakpoints; we'll reinsert them after restarting
4290 the remote server. */
4291 remove_breakpoints ();
4293 /* Now restart the remote server. */
4294 extended_remote_restart ();
4296 /* Now put the breakpoints back in. This way we're safe if the
4297 restart function works via a unix fork on the remote side. */
4298 insert_breakpoints ();
4300 /* Clean up from the last time we were running. */
4301 clear_proceed_status ();
4304 /* Async version of extended_remote_create_inferior. */
4306 extended_remote_async_create_inferior (char *exec_file, char *args,
4307 char **env, int from_tty)
4309 /* Rip out the breakpoints; we'll reinsert them after restarting
4310 the remote server. */
4311 remove_breakpoints ();
4313 /* If running asynchronously, register the target file descriptor
4314 with the event loop. */
4315 if (target_can_async_p ())
4316 target_async (inferior_event_handler, 0);
4318 /* Now restart the remote server. */
4319 extended_remote_restart ();
4321 /* Now put the breakpoints back in. This way we're safe if the
4322 restart function works via a unix fork on the remote side. */
4323 insert_breakpoints ();
4325 /* Clean up from the last time we were running. */
4326 clear_proceed_status ();
4330 /* On some machines, e.g. 68k, we may use a different breakpoint
4331 instruction than other targets; in those use
4332 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4333 Also, bi-endian targets may define
4334 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4335 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4336 just call the standard routines that are in mem-break.c. */
4338 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4339 target should use an identical BREAKPOINT_FROM_PC. As for native,
4340 the ARCH-OS-tdep.c code can override the default. */
4342 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4343 #define DEPRECATED_REMOTE_BREAKPOINT
4346 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4348 /* If the target isn't bi-endian, just pretend it is. */
4349 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4350 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4351 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4354 static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4355 static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
4357 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4359 /* Insert a breakpoint on targets that don't have any better
4360 breakpoint support. We read the contents of the target location
4361 and stash it, then overwrite it with a breakpoint instruction.
4362 ADDR is the target location in the target machine. CONTENTS_CACHE
4363 is a pointer to memory allocated for saving the target contents.
4364 It is guaranteed by the caller to be long enough to save the number
4365 of bytes returned by BREAKPOINT_FROM_PC. */
4368 remote_insert_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache)
4370 struct remote_state *rs = get_remote_state ();
4371 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4376 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4377 If it succeeds, then set the support to PACKET_ENABLE. If it
4378 fails, and the user has explicitly requested the Z support then
4379 report an error, otherwise, mark it disabled and go on. */
4381 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
4385 addr = remote_address_masked (addr);
4389 p += hexnumstr (p, (ULONGEST) addr);
4390 BREAKPOINT_FROM_PC (&addr, &bp_size);
4391 sprintf (p, ",%d", bp_size);
4394 getpkt (&rs->buf, &rs->buf_size, 0);
4396 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
4402 case PACKET_UNKNOWN:
4407 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4408 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4412 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4413 val = target_write_memory (addr, (char *) big_break_insn,
4414 sizeof big_break_insn);
4416 val = target_write_memory (addr, (char *) little_break_insn,
4417 sizeof little_break_insn);
4422 return memory_insert_breakpoint (addr, contents_cache);
4423 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4427 remote_remove_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache)
4429 struct remote_state *rs = get_remote_state ();
4432 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
4440 addr = remote_address_masked (addr);
4441 p += hexnumstr (p, (ULONGEST) addr);
4442 BREAKPOINT_FROM_PC (&addr, &bp_size);
4443 sprintf (p, ",%d", bp_size);
4446 getpkt (&rs->buf, &rs->buf_size, 0);
4448 return (rs->buf[0] == 'E');
4451 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4452 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4454 return memory_remove_breakpoint (addr, contents_cache);
4455 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4459 watchpoint_to_Z_packet (int type)
4473 internal_error (__FILE__, __LINE__,
4474 _("hw_bp_to_z: bad watchpoint type %d"), type);
4479 remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
4481 struct remote_state *rs = get_remote_state ();
4483 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4485 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
4486 error (_("Can't set hardware watchpoints without the '%s' (%s) packet."),
4487 remote_protocol_packets[PACKET_Z0 + packet].name,
4488 remote_protocol_packets[PACKET_Z0 + packet].title);
4490 sprintf (rs->buf, "Z%x,", packet);
4491 p = strchr (rs->buf, '\0');
4492 addr = remote_address_masked (addr);
4493 p += hexnumstr (p, (ULONGEST) addr);
4494 sprintf (p, ",%x", len);
4497 getpkt (&rs->buf, &rs->buf_size, 0);
4499 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
4502 case PACKET_UNKNOWN:
4507 internal_error (__FILE__, __LINE__,
4508 _("remote_insert_watchpoint: reached end of function"));
4513 remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
4515 struct remote_state *rs = get_remote_state ();
4517 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4519 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
4520 error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."),
4521 remote_protocol_packets[PACKET_Z0 + packet].name,
4522 remote_protocol_packets[PACKET_Z0 + packet].title);
4524 sprintf (rs->buf, "z%x,", packet);
4525 p = strchr (rs->buf, '\0');
4526 addr = remote_address_masked (addr);
4527 p += hexnumstr (p, (ULONGEST) addr);
4528 sprintf (p, ",%x", len);
4530 getpkt (&rs->buf, &rs->buf_size, 0);
4532 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
4535 case PACKET_UNKNOWN:
4540 internal_error (__FILE__, __LINE__,
4541 _("remote_remove_watchpoint: reached end of function"));
4545 int remote_hw_watchpoint_limit = -1;
4546 int remote_hw_breakpoint_limit = -1;
4549 remote_check_watch_resources (int type, int cnt, int ot)
4551 if (type == bp_hardware_breakpoint)
4553 if (remote_hw_breakpoint_limit == 0)
4555 else if (remote_hw_breakpoint_limit < 0)
4557 else if (cnt <= remote_hw_breakpoint_limit)
4562 if (remote_hw_watchpoint_limit == 0)
4564 else if (remote_hw_watchpoint_limit < 0)
4568 else if (cnt <= remote_hw_watchpoint_limit)
4575 remote_stopped_by_watchpoint (void)
4577 return remote_stopped_by_watchpoint_p;
4580 extern int stepped_after_stopped_by_watchpoint;
4583 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
4586 if (remote_stopped_by_watchpoint ()
4587 || stepped_after_stopped_by_watchpoint)
4589 *addr_p = remote_watch_data_address;
4598 remote_insert_hw_breakpoint (CORE_ADDR addr, gdb_byte *shadow)
4601 struct remote_state *rs = get_remote_state ();
4604 /* The length field should be set to the size of a breakpoint
4607 BREAKPOINT_FROM_PC (&addr, &len);
4609 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
4610 error (_("Can't set hardware breakpoint without the '%s' (%s) packet."),
4611 remote_protocol_packets[PACKET_Z1].name,
4612 remote_protocol_packets[PACKET_Z1].title);
4618 addr = remote_address_masked (addr);
4619 p += hexnumstr (p, (ULONGEST) addr);
4620 sprintf (p, ",%x", len);
4623 getpkt (&rs->buf, &rs->buf_size, 0);
4625 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
4628 case PACKET_UNKNOWN:
4633 internal_error (__FILE__, __LINE__,
4634 _("remote_insert_hw_breakpoint: reached end of function"));
4639 remote_remove_hw_breakpoint (CORE_ADDR addr, gdb_byte *shadow)
4642 struct remote_state *rs = get_remote_state ();
4645 /* The length field should be set to the size of a breakpoint
4648 BREAKPOINT_FROM_PC (&addr, &len);
4650 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
4651 error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."),
4652 remote_protocol_packets[PACKET_Z1].name,
4653 remote_protocol_packets[PACKET_Z1].title);
4659 addr = remote_address_masked (addr);
4660 p += hexnumstr (p, (ULONGEST) addr);
4661 sprintf (p, ",%x", len);
4664 getpkt (&rs->buf, &rs->buf_size, 0);
4666 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
4669 case PACKET_UNKNOWN:
4674 internal_error (__FILE__, __LINE__,
4675 _("remote_remove_hw_breakpoint: reached end of function"));
4678 /* Some targets are only capable of doing downloads, and afterwards
4679 they switch to the remote serial protocol. This function provides
4680 a clean way to get from the download target to the remote target.
4681 It's basically just a wrapper so that we don't have to expose any
4682 of the internal workings of remote.c.
4684 Prior to calling this routine, you should shutdown the current
4685 target code, else you will get the "A program is being debugged
4686 already..." message. Usually a call to pop_target() suffices. */
4689 push_remote_target (char *name, int from_tty)
4691 printf_filtered (_("Switching to remote protocol\n"));
4692 remote_open (name, from_tty);
4695 /* Table used by the crc32 function to calcuate the checksum. */
4697 static unsigned long crc32_table[256] =
4700 static unsigned long
4701 crc32 (unsigned char *buf, int len, unsigned int crc)
4703 if (!crc32_table[1])
4705 /* Initialize the CRC table and the decoding table. */
4709 for (i = 0; i < 256; i++)
4711 for (c = i << 24, j = 8; j > 0; --j)
4712 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4719 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4725 /* compare-sections command
4727 With no arguments, compares each loadable section in the exec bfd
4728 with the same memory range on the target, and reports mismatches.
4729 Useful for verifying the image on the target against the exec file.
4730 Depends on the target understanding the new "qCRC:" request. */
4732 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4733 target method (target verify memory) and generic version of the
4734 actual command. This will allow other high-level code (especially
4735 generic_load()) to make use of this target functionality. */
4738 compare_sections_command (char *args, int from_tty)
4740 struct remote_state *rs = get_remote_state ();
4742 unsigned long host_crc, target_crc;
4743 extern bfd *exec_bfd;
4744 struct cleanup *old_chain;
4747 const char *sectname;
4754 error (_("command cannot be used without an exec file"));
4755 if (!current_target.to_shortname ||
4756 strcmp (current_target.to_shortname, "remote") != 0)
4757 error (_("command can only be used with remote target"));
4759 for (s = exec_bfd->sections; s; s = s->next)
4761 if (!(s->flags & SEC_LOAD))
4762 continue; /* skip non-loadable section */
4764 size = bfd_get_section_size (s);
4766 continue; /* skip zero-length section */
4768 sectname = bfd_get_section_name (exec_bfd, s);
4769 if (args && strcmp (args, sectname) != 0)
4770 continue; /* not the section selected by user */
4772 matched = 1; /* do this section */
4774 /* FIXME: assumes lma can fit into long. */
4775 xsnprintf (rs->buf, rs->remote_packet_size, "qCRC:%lx,%lx",
4776 (long) lma, (long) size);
4779 /* Be clever; compute the host_crc before waiting for target
4781 sectdata = xmalloc (size);
4782 old_chain = make_cleanup (xfree, sectdata);
4783 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4784 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4786 getpkt (&rs->buf, &rs->buf_size, 0);
4787 if (rs->buf[0] == 'E')
4788 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
4789 sectname, paddr (lma), paddr (lma + size));
4790 if (rs->buf[0] != 'C')
4791 error (_("remote target does not support this operation"));
4793 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
4794 target_crc = target_crc * 16 + fromhex (*tmp);
4796 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4797 sectname, paddr (lma), paddr (lma + size));
4798 if (host_crc == target_crc)
4799 printf_filtered ("matched.\n");
4802 printf_filtered ("MIS-MATCHED!\n");
4806 do_cleanups (old_chain);
4809 warning (_("One or more sections of the remote executable does not match\n\
4810 the loaded file\n"));
4811 if (args && !matched)
4812 printf_filtered (_("No loaded section named '%s'.\n"), args);
4816 remote_xfer_partial (struct target_ops *ops, enum target_object object,
4817 const char *annex, gdb_byte *readbuf,
4818 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4820 struct remote_state *rs = get_remote_state ();
4825 /* Handle memory using remote_xfer_memory. */
4826 if (object == TARGET_OBJECT_MEMORY)
4831 if (writebuf != NULL)
4833 void *buffer = xmalloc (len);
4834 struct cleanup *cleanup = make_cleanup (xfree, buffer);
4835 memcpy (buffer, writebuf, len);
4836 xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops);
4837 do_cleanups (cleanup);
4840 xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops);
4844 else if (xfered == 0 && errno == 0)
4850 /* Only handle reads. */
4851 if (writebuf != NULL || readbuf == NULL)
4854 /* Map pre-existing objects onto letters. DO NOT do this for new
4855 objects!!! Instead specify new query packets. */
4858 case TARGET_OBJECT_KOD:
4861 case TARGET_OBJECT_AVR:
4865 case TARGET_OBJECT_AUXV:
4866 if (remote_protocol_packets[PACKET_qPart_auxv].support != PACKET_DISABLE)
4868 unsigned int total = 0;
4871 LONGEST n = min ((rs->remote_packet_size - 2) / 2, len);
4872 snprintf (rs->buf, rs->remote_packet_size,
4873 "qPart:auxv:read::%s,%s",
4874 phex_nz (offset, sizeof offset),
4875 phex_nz (n, sizeof n));
4876 i = putpkt (rs->buf);
4878 return total > 0 ? total : i;
4880 getpkt (&rs->buf, &rs->buf_size, 0);
4881 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qPart_auxv])
4883 return total > 0 ? total : -1;
4884 if (strcmp (rs->buf, "OK") == 0)
4885 break; /* Got EOF indicator. */
4886 /* Got some data. */
4887 i = hex2bin (rs->buf, readbuf, len);
4890 readbuf = (void *) ((char *) readbuf + i);
4904 /* Note: a zero OFFSET and LEN can be used to query the minimum
4906 if (offset == 0 && len == 0)
4907 return (rs->remote_packet_size);
4908 /* Minimum outbuf size is RS->remote_packet_size. If LEN is not
4909 large enough let the caller deal with it. */
4910 if (len < rs->remote_packet_size)
4912 len = rs->remote_packet_size;
4914 /* Except for querying the minimum buffer size, target must be open. */
4916 error (_("remote query is only available after target open"));
4918 gdb_assert (annex != NULL);
4919 gdb_assert (readbuf != NULL);
4925 /* We used one buffer char for the remote protocol q command and
4926 another for the query type. As the remote protocol encapsulation
4927 uses 4 chars plus one extra in case we are debugging
4928 (remote_debug), we have PBUFZIZ - 7 left to pack the query
4931 while (annex[i] && (i < (rs->remote_packet_size - 8)))
4933 /* Bad caller may have sent forbidden characters. */
4934 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
4939 gdb_assert (annex[i] == '\0');
4941 i = putpkt (rs->buf);
4945 getpkt (&rs->buf, &rs->buf_size, 0);
4946 strcpy ((char *) readbuf, rs->buf);
4948 return strlen ((char *) readbuf);
4952 remote_rcmd (char *command,
4953 struct ui_file *outbuf)
4955 struct remote_state *rs = get_remote_state ();
4956 char *buf = rs->buf;
4960 error (_("remote rcmd is only available after target open"));
4962 /* Send a NULL command across as an empty command. */
4963 if (command == NULL)
4966 /* The query prefix. */
4967 strcpy (buf, "qRcmd,");
4968 p = strchr (buf, '\0');
4970 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > rs->remote_packet_size)
4971 error (_("\"monitor\" command ``%s'' is too long."), command);
4973 /* Encode the actual command. */
4974 bin2hex ((gdb_byte *) command, p, 0);
4976 if (putpkt (rs->buf) < 0)
4977 error (_("Communication problem with target."));
4979 /* get/display the response */
4982 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
4984 getpkt (&rs->buf, &rs->buf_size, 0);
4986 error (_("Target does not support this command."));
4987 if (buf[0] == 'O' && buf[1] != 'K')
4989 remote_console_output (buf + 1); /* 'O' message from stub. */
4992 if (strcmp (buf, "OK") == 0)
4994 if (strlen (buf) == 3 && buf[0] == 'E'
4995 && isdigit (buf[1]) && isdigit (buf[2]))
4997 error (_("Protocol error with Rcmd"));
4999 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5001 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5002 fputc_unfiltered (c, outbuf);
5009 packet_command (char *args, int from_tty)
5011 struct remote_state *rs = get_remote_state ();
5014 error (_("command can only be used with remote target"));
5017 error (_("remote-packet command requires packet text as argument"));
5019 puts_filtered ("sending: ");
5020 print_packet (args);
5021 puts_filtered ("\n");
5024 getpkt (&rs->buf, &rs->buf_size, 0);
5025 puts_filtered ("received: ");
5026 print_packet (rs->buf);
5027 puts_filtered ("\n");
5031 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5033 static void display_thread_info (struct gdb_ext_thread_info *info);
5035 static void threadset_test_cmd (char *cmd, int tty);
5037 static void threadalive_test (char *cmd, int tty);
5039 static void threadlist_test_cmd (char *cmd, int tty);
5041 int get_and_display_threadinfo (threadref *ref);
5043 static void threadinfo_test_cmd (char *cmd, int tty);
5045 static int thread_display_step (threadref *ref, void *context);
5047 static void threadlist_update_test_cmd (char *cmd, int tty);
5049 static void init_remote_threadtests (void);
5051 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5054 threadset_test_cmd (char *cmd, int tty)
5056 int sample_thread = SAMPLE_THREAD;
5058 printf_filtered (_("Remote threadset test\n"));
5059 set_thread (sample_thread, 1);
5064 threadalive_test (char *cmd, int tty)
5066 int sample_thread = SAMPLE_THREAD;
5068 if (remote_thread_alive (pid_to_ptid (sample_thread)))
5069 printf_filtered ("PASS: Thread alive test\n");
5071 printf_filtered ("FAIL: Thread alive test\n");
5074 void output_threadid (char *title, threadref *ref);
5077 output_threadid (char *title, threadref *ref)
5081 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
5083 printf_filtered ("%s %s\n", title, (&hexid[0]));
5087 threadlist_test_cmd (char *cmd, int tty)
5090 threadref nextthread;
5091 int done, result_count;
5092 threadref threadlist[3];
5094 printf_filtered ("Remote Threadlist test\n");
5095 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5096 &result_count, &threadlist[0]))
5097 printf_filtered ("FAIL: threadlist test\n");
5100 threadref *scan = threadlist;
5101 threadref *limit = scan + result_count;
5103 while (scan < limit)
5104 output_threadid (" thread ", scan++);
5109 display_thread_info (struct gdb_ext_thread_info *info)
5111 output_threadid ("Threadid: ", &info->threadid);
5112 printf_filtered ("Name: %s\n ", info->shortname);
5113 printf_filtered ("State: %s\n", info->display);
5114 printf_filtered ("other: %s\n\n", info->more_display);
5118 get_and_display_threadinfo (threadref *ref)
5122 struct gdb_ext_thread_info threadinfo;
5124 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5125 | TAG_MOREDISPLAY | TAG_DISPLAY;
5126 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5127 display_thread_info (&threadinfo);
5132 threadinfo_test_cmd (char *cmd, int tty)
5134 int athread = SAMPLE_THREAD;
5138 int_to_threadref (&thread, athread);
5139 printf_filtered ("Remote Threadinfo test\n");
5140 if (!get_and_display_threadinfo (&thread))
5141 printf_filtered ("FAIL cannot get thread info\n");
5145 thread_display_step (threadref *ref, void *context)
5147 /* output_threadid(" threadstep ",ref); *//* simple test */
5148 return get_and_display_threadinfo (ref);
5152 threadlist_update_test_cmd (char *cmd, int tty)
5154 printf_filtered ("Remote Threadlist update test\n");
5155 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5159 init_remote_threadtests (void)
5161 add_com ("tlist", class_obscure, threadlist_test_cmd, _("\
5162 Fetch and print the remote list of thread identifiers, one pkt only"));
5163 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5164 _("Fetch and display info about one thread"));
5165 add_com ("tset", class_obscure, threadset_test_cmd,
5166 _("Test setting to a different thread"));
5167 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5168 _("Iterate through updating all remote thread info"));
5169 add_com ("talive", class_obscure, threadalive_test,
5170 _(" Remote thread alive test "));
5175 /* Convert a thread ID to a string. Returns the string in a static
5179 remote_pid_to_str (ptid_t ptid)
5181 static char buf[32];
5183 xsnprintf (buf, sizeof buf, "thread %d", ptid_get_pid (ptid));
5187 /* Get the address of the thread local variable in OBJFILE which is
5188 stored at OFFSET within the thread local storage for thread PTID. */
5191 remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
5193 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
5195 struct remote_state *rs = get_remote_state ();
5197 enum packet_result result;
5199 strcpy (p, "qGetTLSAddr:");
5201 p += hexnumstr (p, PIDGET (ptid));
5203 p += hexnumstr (p, offset);
5205 p += hexnumstr (p, lm);
5209 getpkt (&rs->buf, &rs->buf_size, 0);
5210 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]);
5211 if (result == PACKET_OK)
5215 unpack_varlen_hex (rs->buf, &result);
5218 else if (result == PACKET_UNKNOWN)
5219 throw_error (TLS_GENERIC_ERROR,
5220 _("Remote target doesn't support qGetTLSAddr packet"));
5222 throw_error (TLS_GENERIC_ERROR,
5223 _("Remote target failed to process qGetTLSAddr request"));
5226 throw_error (TLS_GENERIC_ERROR,
5227 _("TLS not supported or disabled on this target"));
5233 init_remote_ops (void)
5235 remote_ops.to_shortname = "remote";
5236 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
5238 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5239 Specify the serial device it is connected to\n\
5240 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5241 remote_ops.to_open = remote_open;
5242 remote_ops.to_close = remote_close;
5243 remote_ops.to_detach = remote_detach;
5244 remote_ops.to_disconnect = remote_disconnect;
5245 remote_ops.to_resume = remote_resume;
5246 remote_ops.to_wait = remote_wait;
5247 remote_ops.to_fetch_registers = remote_fetch_registers;
5248 remote_ops.to_store_registers = remote_store_registers;
5249 remote_ops.to_prepare_to_store = remote_prepare_to_store;
5250 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
5251 remote_ops.to_files_info = remote_files_info;
5252 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5253 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5254 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5255 remote_ops.to_stopped_data_address = remote_stopped_data_address;
5256 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5257 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5258 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5259 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5260 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
5261 remote_ops.to_kill = remote_kill;
5262 remote_ops.to_load = generic_load;
5263 remote_ops.to_mourn_inferior = remote_mourn;
5264 remote_ops.to_thread_alive = remote_thread_alive;
5265 remote_ops.to_find_new_threads = remote_threads_info;
5266 remote_ops.to_pid_to_str = remote_pid_to_str;
5267 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5268 remote_ops.to_stop = remote_stop;
5269 remote_ops.to_xfer_partial = remote_xfer_partial;
5270 remote_ops.to_rcmd = remote_rcmd;
5271 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
5272 remote_ops.to_stratum = process_stratum;
5273 remote_ops.to_has_all_memory = 1;
5274 remote_ops.to_has_memory = 1;
5275 remote_ops.to_has_stack = 1;
5276 remote_ops.to_has_registers = 1;
5277 remote_ops.to_has_execution = 1;
5278 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5279 remote_ops.to_magic = OPS_MAGIC;
5282 /* Set up the extended remote vector by making a copy of the standard
5283 remote vector and adding to it. */
5286 init_extended_remote_ops (void)
5288 extended_remote_ops = remote_ops;
5290 extended_remote_ops.to_shortname = "extended-remote";
5291 extended_remote_ops.to_longname =
5292 "Extended remote serial target in gdb-specific protocol";
5293 extended_remote_ops.to_doc =
5294 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5295 Specify the serial device it is connected to (e.g. /dev/ttya).",
5296 extended_remote_ops.to_open = extended_remote_open;
5297 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5298 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5302 remote_can_async_p (void)
5304 /* We're async whenever the serial device is. */
5305 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
5309 remote_is_async_p (void)
5311 /* We're async whenever the serial device is. */
5312 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
5315 /* Pass the SERIAL event on and up to the client. One day this code
5316 will be able to delay notifying the client of an event until the
5317 point where an entire packet has been received. */
5319 static void (*async_client_callback) (enum inferior_event_type event_type,
5321 static void *async_client_context;
5322 static serial_event_ftype remote_async_serial_handler;
5325 remote_async_serial_handler (struct serial *scb, void *context)
5327 /* Don't propogate error information up to the client. Instead let
5328 the client find out about the error by querying the target. */
5329 async_client_callback (INF_REG_EVENT, async_client_context);
5333 remote_async (void (*callback) (enum inferior_event_type event_type,
5334 void *context), void *context)
5336 if (current_target.to_async_mask_value == 0)
5337 internal_error (__FILE__, __LINE__,
5338 _("Calling remote_async when async is masked"));
5340 if (callback != NULL)
5342 serial_async (remote_desc, remote_async_serial_handler, NULL);
5343 async_client_callback = callback;
5344 async_client_context = context;
5347 serial_async (remote_desc, NULL, NULL);
5350 /* Target async and target extended-async.
5352 This are temporary targets, until it is all tested. Eventually
5353 async support will be incorporated int the usual 'remote'
5357 init_remote_async_ops (void)
5359 remote_async_ops.to_shortname = "async";
5360 remote_async_ops.to_longname =
5361 "Remote serial target in async version of the gdb-specific protocol";
5362 remote_async_ops.to_doc =
5363 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5364 Specify the serial device it is connected to (e.g. /dev/ttya).";
5365 remote_async_ops.to_open = remote_async_open;
5366 remote_async_ops.to_close = remote_close;
5367 remote_async_ops.to_detach = remote_detach;
5368 remote_async_ops.to_disconnect = remote_disconnect;
5369 remote_async_ops.to_resume = remote_async_resume;
5370 remote_async_ops.to_wait = remote_async_wait;
5371 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5372 remote_async_ops.to_store_registers = remote_store_registers;
5373 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5374 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
5375 remote_async_ops.to_files_info = remote_files_info;
5376 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5377 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5378 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5379 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5380 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5381 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5382 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5383 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5384 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
5385 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5386 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5387 remote_async_ops.to_kill = remote_async_kill;
5388 remote_async_ops.to_load = generic_load;
5389 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5390 remote_async_ops.to_thread_alive = remote_thread_alive;
5391 remote_async_ops.to_find_new_threads = remote_threads_info;
5392 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5393 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
5394 remote_async_ops.to_stop = remote_stop;
5395 remote_async_ops.to_xfer_partial = remote_xfer_partial;
5396 remote_async_ops.to_rcmd = remote_rcmd;
5397 remote_async_ops.to_stratum = process_stratum;
5398 remote_async_ops.to_has_all_memory = 1;
5399 remote_async_ops.to_has_memory = 1;
5400 remote_async_ops.to_has_stack = 1;
5401 remote_async_ops.to_has_registers = 1;
5402 remote_async_ops.to_has_execution = 1;
5403 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5404 remote_async_ops.to_can_async_p = remote_can_async_p;
5405 remote_async_ops.to_is_async_p = remote_is_async_p;
5406 remote_async_ops.to_async = remote_async;
5407 remote_async_ops.to_async_mask_value = 1;
5408 remote_async_ops.to_magic = OPS_MAGIC;
5411 /* Set up the async extended remote vector by making a copy of the standard
5412 remote vector and adding to it. */
5415 init_extended_async_remote_ops (void)
5417 extended_async_remote_ops = remote_async_ops;
5419 extended_async_remote_ops.to_shortname = "extended-async";
5420 extended_async_remote_ops.to_longname =
5421 "Extended remote serial target in async gdb-specific protocol";
5422 extended_async_remote_ops.to_doc =
5423 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5424 Specify the serial device it is connected to (e.g. /dev/ttya).",
5425 extended_async_remote_ops.to_open = extended_remote_async_open;
5426 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5427 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5430 static struct cmd_list_element *remote_set_cmdlist;
5431 static struct cmd_list_element *remote_show_cmdlist;
5434 set_remote_cmd (char *args, int from_tty)
5439 show_remote_cmd (char *args, int from_tty)
5441 /* We can't just use cmd_show_list here, because we want to skip
5442 the redundant "show remote Z-packet". */
5443 struct cleanup *showlist_chain;
5444 struct cmd_list_element *list = remote_show_cmdlist;
5446 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
5447 for (; list != NULL; list = list->next)
5448 if (strcmp (list->name, "Z-packet") == 0)
5450 else if (list->type == show_cmd)
5452 struct cleanup *option_chain
5453 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
5454 ui_out_field_string (uiout, "name", list->name);
5455 ui_out_text (uiout, ": ");
5456 do_setshow_command ((char *) NULL, from_tty, list);
5457 /* Close the tuple. */
5458 do_cleanups (option_chain);
5463 build_remote_gdbarch_data (void)
5465 remote_address_size = TARGET_ADDR_BIT;
5468 /* Saved pointer to previous owner of the new_objfile event. */
5469 static void (*remote_new_objfile_chain) (struct objfile *);
5471 /* Function to be called whenever a new objfile (shlib) is detected. */
5473 remote_new_objfile (struct objfile *objfile)
5475 if (remote_desc != 0) /* Have a remote connection. */
5477 remote_check_symbols (objfile);
5479 /* Call predecessor on chain, if any. */
5480 if (remote_new_objfile_chain != 0 &&
5482 remote_new_objfile_chain (objfile);
5486 _initialize_remote (void)
5488 /* architecture specific data */
5489 remote_gdbarch_data_handle =
5490 gdbarch_data_register_post_init (init_remote_state);
5492 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5493 that the remote protocol has been initialized. */
5494 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5495 deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5498 add_target (&remote_ops);
5500 init_extended_remote_ops ();
5501 add_target (&extended_remote_ops);
5503 init_remote_async_ops ();
5504 add_target (&remote_async_ops);
5506 init_extended_async_remote_ops ();
5507 add_target (&extended_async_remote_ops);
5509 /* Hook into new objfile notification. */
5510 remote_new_objfile_chain = deprecated_target_new_objfile_hook;
5511 deprecated_target_new_objfile_hook = remote_new_objfile;
5514 init_remote_threadtests ();
5517 /* set/show remote ... */
5519 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5520 Remote protocol specific variables\n\
5521 Configure various remote-protocol specific variables such as\n\
5522 the packets being used"),
5523 &remote_set_cmdlist, "set remote ",
5524 0 /* allow-unknown */, &setlist);
5525 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5526 Remote protocol specific variables\n\
5527 Configure various remote-protocol specific variables such as\n\
5528 the packets being used"),
5529 &remote_show_cmdlist, "show remote ",
5530 0 /* allow-unknown */, &showlist);
5532 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
5533 Compare section data on target to the exec file.\n\
5534 Argument is a single section name (default: all loaded sections)."),
5537 add_cmd ("packet", class_maintenance, packet_command, _("\
5538 Send an arbitrary packet to a remote target.\n\
5539 maintenance packet TEXT\n\
5540 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5541 this command sends the string TEXT to the inferior, and displays the\n\
5542 response packet. GDB supplies the initial `$' character, and the\n\
5543 terminating `#' character and checksum."),
5546 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
5547 Set whether to send break if interrupted."), _("\
5548 Show whether to send break if interrupted."), _("\
5549 If set, a break, instead of a cntrl-c, is sent to the remote target."),
5550 NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */
5551 &setlist, &showlist);
5553 /* Install commands for configuring memory read/write packets. */
5555 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
5556 Set the maximum number of bytes per memory write packet (deprecated)."),
5558 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
5559 Show the maximum number of bytes per memory write packet (deprecated)."),
5561 add_cmd ("memory-write-packet-size", no_class,
5562 set_memory_write_packet_size, _("\
5563 Set the maximum number of bytes per memory-write packet.\n\
5564 Specify the number of bytes in a packet or 0 (zero) for the\n\
5565 default packet size. The actual limit is further reduced\n\
5566 dependent on the target. Specify ``fixed'' to disable the\n\
5567 further restriction and ``limit'' to enable that restriction."),
5568 &remote_set_cmdlist);
5569 add_cmd ("memory-read-packet-size", no_class,
5570 set_memory_read_packet_size, _("\
5571 Set the maximum number of bytes per memory-read packet.\n\
5572 Specify the number of bytes in a packet or 0 (zero) for the\n\
5573 default packet size. The actual limit is further reduced\n\
5574 dependent on the target. Specify ``fixed'' to disable the\n\
5575 further restriction and ``limit'' to enable that restriction."),
5576 &remote_set_cmdlist);
5577 add_cmd ("memory-write-packet-size", no_class,
5578 show_memory_write_packet_size,
5579 _("Show the maximum number of bytes per memory-write packet."),
5580 &remote_show_cmdlist);
5581 add_cmd ("memory-read-packet-size", no_class,
5582 show_memory_read_packet_size,
5583 _("Show the maximum number of bytes per memory-read packet."),
5584 &remote_show_cmdlist);
5586 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
5587 &remote_hw_watchpoint_limit, _("\
5588 Set the maximum number of target hardware watchpoints."), _("\
5589 Show the maximum number of target hardware watchpoints."), _("\
5590 Specify a negative limit for unlimited."),
5591 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
5592 &remote_set_cmdlist, &remote_show_cmdlist);
5593 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
5594 &remote_hw_breakpoint_limit, _("\
5595 Set the maximum number of target hardware breakpoints."), _("\
5596 Show the maximum number of target hardware breakpoints."), _("\
5597 Specify a negative limit for unlimited."),
5598 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
5599 &remote_set_cmdlist, &remote_show_cmdlist);
5601 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
5602 &remote_address_size, _("\
5603 Set the maximum size of the address (in bits) in a memory packet."), _("\
5604 Show the maximum size of the address (in bits) in a memory packet."), NULL,
5606 NULL, /* FIXME: i18n: */
5607 &setlist, &showlist);
5609 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
5610 "X", "binary-download",
5611 set_remote_protocol_packet_cmd,
5612 show_remote_protocol_packet_cmd,
5613 &remote_set_cmdlist, &remote_show_cmdlist,
5616 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
5617 "vCont", "verbose-resume",
5618 set_remote_protocol_packet_cmd,
5619 show_remote_protocol_packet_cmd,
5620 &remote_set_cmdlist, &remote_show_cmdlist,
5623 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
5624 "qSymbol", "symbol-lookup",
5625 set_remote_protocol_packet_cmd,
5626 show_remote_protocol_packet_cmd,
5627 &remote_set_cmdlist, &remote_show_cmdlist,
5630 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
5631 "P", "set-register",
5632 set_remote_protocol_packet_cmd,
5633 show_remote_protocol_packet_cmd,
5634 &remote_set_cmdlist, &remote_show_cmdlist,
5637 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
5638 "p", "fetch-register",
5639 set_remote_protocol_packet_cmd,
5640 show_remote_protocol_packet_cmd,
5641 &remote_set_cmdlist, &remote_show_cmdlist,
5644 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
5645 "Z0", "software-breakpoint",
5646 set_remote_protocol_packet_cmd,
5647 show_remote_protocol_packet_cmd,
5648 &remote_set_cmdlist, &remote_show_cmdlist,
5651 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
5652 "Z1", "hardware-breakpoint",
5653 set_remote_protocol_packet_cmd,
5654 show_remote_protocol_packet_cmd,
5655 &remote_set_cmdlist, &remote_show_cmdlist,
5658 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
5659 "Z2", "write-watchpoint",
5660 set_remote_protocol_packet_cmd,
5661 show_remote_protocol_packet_cmd,
5662 &remote_set_cmdlist, &remote_show_cmdlist,
5665 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
5666 "Z3", "read-watchpoint",
5667 set_remote_protocol_packet_cmd,
5668 show_remote_protocol_packet_cmd,
5669 &remote_set_cmdlist, &remote_show_cmdlist,
5672 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
5673 "Z4", "access-watchpoint",
5674 set_remote_protocol_packet_cmd,
5675 show_remote_protocol_packet_cmd,
5676 &remote_set_cmdlist, &remote_show_cmdlist,
5679 add_packet_config_cmd (&remote_protocol_packets[PACKET_qPart_auxv],
5680 "qPart_auxv", "read-aux-vector",
5681 set_remote_protocol_packet_cmd,
5682 show_remote_protocol_packet_cmd,
5683 &remote_set_cmdlist, &remote_show_cmdlist,
5686 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
5687 "qGetTLSAddr", "get-thread-local-storage-address",
5688 set_remote_protocol_packet_cmd,
5689 show_remote_protocol_packet_cmd,
5690 &remote_set_cmdlist, &remote_show_cmdlist,
5693 /* Keep the old ``set remote Z-packet ...'' working. Each individual
5694 Z sub-packet has its own set and show commands, but users may
5695 have sets to this variable in their .gdbinit files (or in their
5697 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
5698 &remote_Z_packet_detect, _("\
5699 Set use of remote protocol `Z' packets"), _("\
5700 Show use of remote protocol `Z' packets "), _("\
5701 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5703 set_remote_protocol_Z_packet_cmd,
5704 show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
5705 &remote_set_cmdlist, &remote_show_cmdlist);
5707 /* Eventually initialize fileio. See fileio.c */
5708 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);