1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
32 /*#include "terminal.h" */
35 #include "gdb-stabs.h"
36 #include "gdbthread.h"
43 #include <sys/types.h>
46 #include "event-loop.h"
47 #include "event-top.h"
53 #include "gdbcore.h" /* for exec_bfd */
55 /* Prototypes for local functions */
56 static void cleanup_sigint_signal_handler (void *dummy);
57 static void initialize_sigint_signal_handler (void);
58 static int getpkt_sane (char *buf, long sizeof_buf, int forever);
60 static void handle_remote_sigint (int);
61 static void handle_remote_sigint_twice (int);
62 static void async_remote_interrupt (gdb_client_data);
63 void async_remote_interrupt_twice (gdb_client_data);
65 static void build_remote_gdbarch_data (void);
67 static int remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len);
69 static int remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len);
71 static void remote_files_info (struct target_ops *ignore);
73 static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
74 int len, int should_write,
75 struct mem_attrib *attrib,
76 struct target_ops *target);
78 static void remote_prepare_to_store (void);
80 static void remote_fetch_registers (int regno);
82 static void remote_resume (ptid_t ptid, int step,
83 enum target_signal siggnal);
84 static void remote_async_resume (ptid_t ptid, int step,
85 enum target_signal siggnal);
86 static int remote_start_remote (PTR);
88 static void remote_open (char *name, int from_tty);
89 static void remote_async_open (char *name, int from_tty);
91 static void extended_remote_open (char *name, int from_tty);
92 static void extended_remote_async_open (char *name, int from_tty);
94 static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
95 static void remote_async_open_1 (char *, int, struct target_ops *,
98 static void remote_close (int quitting);
100 static void remote_store_registers (int regno);
102 static void remote_mourn (void);
103 static void remote_async_mourn (void);
105 static void extended_remote_restart (void);
107 static void extended_remote_mourn (void);
109 static void extended_remote_create_inferior (char *, char *, char **);
110 static void extended_remote_async_create_inferior (char *, char *, char **);
112 static void remote_mourn_1 (struct target_ops *);
114 static void remote_send (char *buf, long sizeof_buf);
116 static int readchar (int timeout);
118 static ptid_t remote_wait (ptid_t ptid,
119 struct target_waitstatus *status);
120 static ptid_t remote_async_wait (ptid_t ptid,
121 struct target_waitstatus *status);
123 static void remote_kill (void);
124 static void remote_async_kill (void);
126 static int tohex (int nib);
128 static void remote_detach (char *args, int from_tty);
129 static void remote_async_detach (char *args, int from_tty);
131 static void remote_interrupt (int signo);
133 static void remote_interrupt_twice (int signo);
135 static void interrupt_query (void);
137 static void set_thread (int, int);
139 static int remote_thread_alive (ptid_t);
141 static void get_offsets (void);
143 static long read_frame (char *buf, long sizeof_buf);
145 static int remote_insert_breakpoint (CORE_ADDR, char *);
147 static int remote_remove_breakpoint (CORE_ADDR, char *);
149 static int hexnumlen (ULONGEST num);
151 static void init_remote_ops (void);
153 static void init_extended_remote_ops (void);
155 static void init_remote_cisco_ops (void);
157 static struct target_ops remote_cisco_ops;
159 static void remote_stop (void);
161 static int ishex (int ch, int *val);
163 static int stubhex (int ch);
165 static int remote_query (int /*char */ , char *, char *, int *);
167 static int hexnumstr (char *, ULONGEST);
169 static int hexnumnstr (char *, ULONGEST, int);
171 static CORE_ADDR remote_address_masked (CORE_ADDR);
173 static void print_packet (char *);
175 static unsigned long crc32 (unsigned char *, int, unsigned int);
177 static void compare_sections_command (char *, int);
179 static void packet_command (char *, int);
181 static int stub_unpack_int (char *buff, int fieldlength);
183 static ptid_t remote_current_thread (ptid_t oldptid);
185 static void remote_find_new_threads (void);
187 static void record_currthread (int currthread);
189 static int fromhex (int a);
191 static int hex2bin (const char *hex, char *bin, int);
193 static int bin2hex (const char *bin, char *hex, int);
195 static int putpkt_binary (char *buf, int cnt);
197 static void check_binary_download (CORE_ADDR addr);
199 struct packet_config;
201 static void show_packet_config_cmd (struct packet_config *config);
203 static void update_packet_config (struct packet_config *config);
205 /* Define the target subroutine names */
207 void open_remote_target (char *, int, struct target_ops *, int);
209 void _initialize_remote (void);
213 static struct target_ops remote_ops;
215 static struct target_ops extended_remote_ops;
217 /* Temporary target ops. Just like the remote_ops and
218 extended_remote_ops, but with asynchronous support. */
219 static struct target_ops remote_async_ops;
221 static struct target_ops extended_async_remote_ops;
223 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
224 ``forever'' still use the normal timeout mechanism. This is
225 currently used by the ASYNC code to guarentee that target reads
226 during the initial connect always time-out. Once getpkt has been
227 modified to return a timeout indication and, in turn
228 remote_wait()/wait_for_inferior() have gained a timeout parameter
230 static int wait_forever_enabled_p = 1;
233 /* This variable chooses whether to send a ^C or a break when the user
234 requests program interruption. Although ^C is usually what remote
235 systems expect, and that is the default here, sometimes a break is
236 preferable instead. */
238 static int remote_break;
240 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
241 remote_open knows that we don't have a file open when the program
243 static serial_t remote_desc = NULL;
245 /* This is set by the target (thru the 'S' message)
246 to denote that the target is in kernel mode. */
247 static int cisco_kernel_mode = 0;
249 /* This variable sets the number of bits in an address that are to be
250 sent in a memory ("M" or "m") packet. Normally, after stripping
251 leading zeros, the entire address would be sent. This variable
252 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
253 initial implementation of remote.c restricted the address sent in
254 memory packets to ``host::sizeof long'' bytes - (typically 32
255 bits). Consequently, for 64 bit targets, the upper 32 bits of an
256 address was never sent. Since fixing this bug may cause a break in
257 some remote targets this variable is principly provided to
258 facilitate backward compatibility. */
260 static int remote_address_size;
262 /* Tempoary to track who currently owns the terminal. See
263 target_async_terminal_* for more details. */
265 static int remote_async_terminal_ours_p;
268 /* This is the size (in chars) of the first response to the ``g''
269 packet. It is used as a heuristic when determining the maximum
270 size of memory-read and memory-write packets. A target will
271 typically only reserve a buffer large enough to hold the ``g''
272 packet. The size does not include packet overhead (headers and
275 static long actual_register_packet_size;
277 /* This is the maximum size (in chars) of a non read/write packet. It
278 is also used as a cap on the size of read/write packets. */
280 static long remote_packet_size;
282 #define PBUFSIZ (remote_packet_size)
284 /* User configurable variables for the number of characters in a
285 memory read/write packet. MIN (PBUFSIZ, g-packet-size) is the
286 default. Some targets need smaller values (fifo overruns, et.al.)
287 and some users need larger values (speed up transfers). The
288 variables ``preferred_*'' (the user request), ``current_*'' (what
289 was actually set) and ``forced_*'' (Positive - a soft limit,
290 negative - a hard limit). */
292 struct memory_packet_config
299 /* Compute the current size of a read/write packet. Since this makes
300 use of ``actual_register_packet_size'' the computation is dynamic. */
303 get_memory_packet_size (struct memory_packet_config *config)
305 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
306 law?) that some hosts don't cope very well with large alloca()
307 calls. Eventually the alloca() code will be replaced by calls to
308 xmalloc() and make_cleanups() allowing this restriction to either
309 be lifted or removed. */
310 #ifndef MAX_REMOTE_PACKET_SIZE
311 #define MAX_REMOTE_PACKET_SIZE 16384
313 /* NOTE: 16 is just chosen at random. */
314 #ifndef MIN_REMOTE_PACKET_SIZE
315 #define MIN_REMOTE_PACKET_SIZE 16
320 if (config->size <= 0)
321 what_they_get = MAX_REMOTE_PACKET_SIZE;
323 what_they_get = config->size;
327 what_they_get = remote_packet_size;
328 /* Limit the packet to the size specified by the user. */
330 && what_they_get > config->size)
331 what_they_get = config->size;
332 /* Limit it to the size of the targets ``g'' response. */
333 if (actual_register_packet_size > 0
334 && what_they_get > actual_register_packet_size)
335 what_they_get = actual_register_packet_size;
337 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
338 what_they_get = MAX_REMOTE_PACKET_SIZE;
339 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
340 what_they_get = MIN_REMOTE_PACKET_SIZE;
341 return what_they_get;
344 /* Update the size of a read/write packet. If they user wants
345 something really big then do a sanity check. */
348 set_memory_packet_size (char *args, struct memory_packet_config *config)
350 int fixed_p = config->fixed_p;
351 long size = config->size;
353 error ("Argument required (integer, `fixed' or `limited').");
354 else if (strcmp (args, "hard") == 0
355 || strcmp (args, "fixed") == 0)
357 else if (strcmp (args, "soft") == 0
358 || strcmp (args, "limit") == 0)
363 size = strtoul (args, &end, 0);
365 error ("Invalid %s (bad syntax).", config->name);
367 /* Instead of explicitly capping the size of a packet to
368 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
369 instead allowed to set the size to something arbitrarily
371 if (size > MAX_REMOTE_PACKET_SIZE)
372 error ("Invalid %s (too large).", config->name);
376 if (fixed_p && !config->fixed_p)
378 if (! query ("The target may not be able to correctly handle a %s\n"
379 "of %ld bytes. Change the packet size? ",
381 error ("Packet size not changed.");
383 /* Update the config. */
384 config->fixed_p = fixed_p;
389 show_memory_packet_size (struct memory_packet_config *config)
391 printf_filtered ("The %s is %ld. ", config->name, config->size);
393 printf_filtered ("Packets are fixed at %ld bytes.\n",
394 get_memory_packet_size (config));
396 printf_filtered ("Packets are limited to %ld bytes.\n",
397 get_memory_packet_size (config));
400 static struct memory_packet_config memory_write_packet_config =
402 "memory-write-packet-size",
406 set_memory_write_packet_size (char *args, int from_tty)
408 set_memory_packet_size (args, &memory_write_packet_config);
412 show_memory_write_packet_size (char *args, int from_tty)
414 show_memory_packet_size (&memory_write_packet_config);
418 get_memory_write_packet_size (void)
420 return get_memory_packet_size (&memory_write_packet_config);
423 static struct memory_packet_config memory_read_packet_config =
425 "memory-read-packet-size",
429 set_memory_read_packet_size (char *args, int from_tty)
431 set_memory_packet_size (args, &memory_read_packet_config);
435 show_memory_read_packet_size (char *args, int from_tty)
437 show_memory_packet_size (&memory_read_packet_config);
441 get_memory_read_packet_size (void)
443 long size = get_memory_packet_size (&memory_read_packet_config);
444 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
445 extra buffer size argument before the memory read size can be
446 increased beyond PBUFSIZ. */
452 /* Register packet size initialization. Since the bounds change when
453 the architecture changes (namely REGISTER_BYTES) this all needs to
457 register_remote_packet_sizes (void)
459 REGISTER_GDBARCH_SWAP (remote_packet_size);
460 REGISTER_GDBARCH_SWAP (actual_register_packet_size);
464 build_remote_packet_sizes (void)
466 /* Default maximum number of characters in a packet body. Many
467 remote stubs have a hardwired buffer size of 400 bytes
468 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
469 as the maximum packet-size to ensure that the packet and an extra
470 NUL character can always fit in the buffer. This stops GDB
471 trashing stubs that try to squeeze an extra NUL into what is
472 already a full buffer (As of 1999-12-04 that was most stubs. */
473 remote_packet_size = 400 - 1;
474 /* Should REGISTER_BYTES needs more space than the default, adjust
475 the size accordingly. Remember that each byte is encoded as two
476 characters. 32 is the overhead for the packet header /
477 footer. NOTE: cagney/1999-10-26: I suspect that 8
478 (``$NN:G...#NN'') is a better guess, the below has been padded a
480 if (REGISTER_BYTES > ((remote_packet_size - 32) / 2))
481 remote_packet_size = (REGISTER_BYTES * 2 + 32);
483 /* This one is filled in when a ``g'' packet is received. */
484 actual_register_packet_size = 0;
487 /* Generic configuration support for packets the stub optionally
488 supports. Allows the user to specify the use of the packet as well
489 as allowing GDB to auto-detect support in the remote stub. */
493 PACKET_SUPPORT_UNKNOWN = 0,
502 enum cmd_auto_boolean detect;
503 enum packet_support support;
506 /* Analyze a packet's return value and update the packet config
517 update_packet_config (struct packet_config *config)
519 switch (config->detect)
521 case CMD_AUTO_BOOLEAN_TRUE:
522 config->support = PACKET_ENABLE;
524 case CMD_AUTO_BOOLEAN_FALSE:
525 config->support = PACKET_DISABLE;
527 case CMD_AUTO_BOOLEAN_AUTO:
528 config->support = PACKET_SUPPORT_UNKNOWN;
534 show_packet_config_cmd (struct packet_config *config)
536 char *support = "internal-error";
537 switch (config->support)
543 support = "disabled";
545 case PACKET_SUPPORT_UNKNOWN:
549 switch (config->detect)
551 case CMD_AUTO_BOOLEAN_AUTO:
552 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
553 config->name, config->title, support);
555 case CMD_AUTO_BOOLEAN_TRUE:
556 case CMD_AUTO_BOOLEAN_FALSE:
557 printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
558 config->name, config->title, support);
564 add_packet_config_cmd (struct packet_config *config,
567 void (*set_func) (char *args, int from_tty,
568 struct cmd_list_element *
570 void (*show_func) (char *name,
572 struct cmd_list_element **set_remote_list,
573 struct cmd_list_element **show_remote_list,
576 struct cmd_list_element *set_cmd;
577 struct cmd_list_element *show_cmd;
582 config->title = title;
583 config->detect = CMD_AUTO_BOOLEAN_AUTO;
584 config->support = PACKET_SUPPORT_UNKNOWN;
585 xasprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
587 xasprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
589 /* set/show TITLE-packet {auto,on,off} */
590 xasprintf (&cmd_name, "%s-packet", title);
591 set_cmd = add_set_auto_boolean_cmd (cmd_name, class_obscure,
592 &config->detect, set_doc,
594 set_cmd->function.sfunc = set_func;
595 show_cmd = add_cmd (cmd_name, class_obscure, show_func, show_doc,
597 /* set/show remote NAME-packet {auto,on,off} -- legacy */
601 xasprintf (&legacy_name, "%s-packet", name);
602 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
604 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
609 static enum packet_result
610 packet_ok (const char *buf, struct packet_config *config)
614 /* The stub recognized the packet request. Check that the
615 operation succeeded. */
616 switch (config->support)
618 case PACKET_SUPPORT_UNKNOWN:
620 fprintf_unfiltered (gdb_stdlog,
621 "Packet %s (%s) is supported\n",
622 config->name, config->title);
623 config->support = PACKET_ENABLE;
626 internal_error (__FILE__, __LINE__,
627 "packet_ok: attempt to use a disabled packet");
632 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
633 /* "OK" - definitly OK. */
636 && isxdigit (buf[1]) && isxdigit (buf[2])
638 /* "Enn" - definitly an error. */
640 /* The packet may or may not be OK. Just assume it is */
645 /* The stub does not support the packet. */
646 switch (config->support)
649 if (config->detect == CMD_AUTO_BOOLEAN_AUTO)
650 /* If the stub previously indicated that the packet was
651 supported then there is a protocol error.. */
652 error ("Protocol error: %s (%s) conflicting enabled responses.",
653 config->name, config->title);
655 /* The user set it wrong. */
656 error ("Enabled packet %s (%s) not recognized by stub",
657 config->name, config->title);
659 case PACKET_SUPPORT_UNKNOWN:
661 fprintf_unfiltered (gdb_stdlog,
662 "Packet %s (%s) is NOT supported\n",
663 config->name, config->title);
664 config->support = PACKET_DISABLE;
669 return PACKET_UNKNOWN;
673 /* Should we try the 'e' (step over range) request? */
674 static struct packet_config remote_protocol_e;
677 set_remote_protocol_e_packet_cmd (char *args, int from_tty,
678 struct cmd_list_element *c)
680 update_packet_config (&remote_protocol_e);
684 show_remote_protocol_e_packet_cmd (char *args, int from_tty)
686 show_packet_config_cmd (&remote_protocol_e);
690 /* Should we try the 'E' (step over range / w signal #) request? */
691 static struct packet_config remote_protocol_E;
694 set_remote_protocol_E_packet_cmd (char *args, int from_tty,
695 struct cmd_list_element *c)
697 update_packet_config (&remote_protocol_E);
701 show_remote_protocol_E_packet_cmd (char *args, int from_tty)
703 show_packet_config_cmd (&remote_protocol_E);
707 /* Should we try the 'P' (set register) request? */
709 static struct packet_config remote_protocol_P;
712 set_remote_protocol_P_packet_cmd (char *args, int from_tty,
713 struct cmd_list_element *c)
715 update_packet_config (&remote_protocol_P);
719 show_remote_protocol_P_packet_cmd (char *args, int from_tty)
721 show_packet_config_cmd (&remote_protocol_P);
724 /* Should we try one of the 'Z' requests? */
728 Z_PACKET_SOFTWARE_BP,
729 Z_PACKET_HARDWARE_BP,
736 static struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES];
738 /* FIXME: Instead of having all these boiler plate functions, the
739 command callback should include a context argument. */
742 set_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
743 struct cmd_list_element *c)
745 update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
749 show_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty)
751 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
755 set_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
756 struct cmd_list_element *c)
758 update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
762 show_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty)
764 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
768 set_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
769 struct cmd_list_element *c)
771 update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
775 show_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty)
777 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
781 set_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
782 struct cmd_list_element *c)
784 update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]);
788 show_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty)
790 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
794 set_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
795 struct cmd_list_element *c)
797 update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
801 show_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty)
803 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
806 /* For compatibility with older distributions. Provide a ``set remote
807 Z-packet ...'' command that updates all the Z packet types. */
809 static enum cmd_auto_boolean remote_Z_packet_detect;
812 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
813 struct cmd_list_element *c)
816 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
818 remote_protocol_Z[i].detect = remote_Z_packet_detect;
819 update_packet_config (&remote_protocol_Z[i]);
824 show_remote_protocol_Z_packet_cmd (char *args, int from_tty)
827 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
829 show_packet_config_cmd (&remote_protocol_Z[i]);
833 /* Should we try the 'X' (remote binary download) packet?
835 This variable (available to the user via "set remote X-packet")
836 dictates whether downloads are sent in binary (via the 'X' packet).
837 We assume that the stub can, and attempt to do it. This will be
838 cleared if the stub does not understand it. This switch is still
839 needed, though in cases when the packet is supported in the stub,
840 but the connection does not allow it (i.e., 7-bit serial connection
843 static struct packet_config remote_protocol_binary_download;
845 /* Should we try the 'ThreadInfo' query packet?
847 This variable (NOT available to the user: auto-detect only!)
848 determines whether GDB will use the new, simpler "ThreadInfo"
849 query or the older, more complex syntax for thread queries.
850 This is an auto-detect variable (set to true at each connect,
851 and set to false when the target fails to recognize it). */
853 static int use_threadinfo_query;
854 static int use_threadextra_query;
857 set_remote_protocol_binary_download_cmd (char *args,
859 struct cmd_list_element *c)
861 update_packet_config (&remote_protocol_binary_download);
865 show_remote_protocol_binary_download_cmd (char *args,
868 show_packet_config_cmd (&remote_protocol_binary_download);
872 /* Tokens for use by the asynchronous signal handlers for SIGINT */
873 PTR sigint_remote_twice_token;
874 PTR sigint_remote_token;
876 /* These are pointers to hook functions that may be set in order to
877 modify resume/wait behavior for a particular architecture. */
879 void (*target_resume_hook) (void);
880 void (*target_wait_loop_hook) (void);
884 /* These are the threads which we last sent to the remote system.
885 -1 for all or -2 for not sent yet. */
886 static int general_thread;
887 static int continue_thread;
889 /* Call this function as a result of
890 1) A halt indication (T packet) containing a thread id
891 2) A direct query of currthread
892 3) Successful execution of set thread
896 record_currthread (int currthread)
898 general_thread = currthread;
900 /* If this is a new thread, add it to GDB's thread list.
901 If we leave it up to WFI to do this, bad things will happen. */
902 if (!in_thread_list (pid_to_ptid (currthread)))
904 add_thread (pid_to_ptid (currthread));
906 ui_out_text (uiout, "[New ");
907 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
908 ui_out_text (uiout, "]\n");
910 printf_filtered ("[New %s]\n",
911 target_pid_to_str (pid_to_ptid (currthread)));
916 #define MAGIC_NULL_PID 42000
919 set_thread (int th, int gen)
921 char *buf = alloca (PBUFSIZ);
922 int state = gen ? general_thread : continue_thread;
928 buf[1] = gen ? 'g' : 'c';
929 if (th == MAGIC_NULL_PID)
935 sprintf (&buf[2], "-%x", -th);
937 sprintf (&buf[2], "%x", th);
939 getpkt (buf, PBUFSIZ, 0);
943 continue_thread = th;
946 /* Return nonzero if the thread TH is still alive on the remote system. */
949 remote_thread_alive (ptid_t ptid)
951 int tid = PIDGET (ptid);
955 sprintf (buf, "T-%08x", -tid);
957 sprintf (buf, "T%08x", tid);
959 getpkt (buf, sizeof (buf), 0);
960 return (buf[0] == 'O' && buf[1] == 'K');
963 /* About these extended threadlist and threadinfo packets. They are
964 variable length packets but, the fields within them are often fixed
965 length. They are redundent enough to send over UDP as is the
966 remote protocol in general. There is a matching unit test module
969 #define OPAQUETHREADBYTES 8
971 /* a 64 bit opaque identifier */
972 typedef unsigned char threadref[OPAQUETHREADBYTES];
974 /* WARNING: This threadref data structure comes from the remote O.S., libstub
975 protocol encoding, and remote.c. it is not particularly changable */
977 /* Right now, the internal structure is int. We want it to be bigger.
981 typedef int gdb_threadref; /* internal GDB thread reference */
983 /* gdb_ext_thread_info is an internal GDB data structure which is
984 equivalint to the reply of the remote threadinfo packet */
986 struct gdb_ext_thread_info
988 threadref threadid; /* External form of thread reference */
989 int active; /* Has state interesting to GDB? , regs, stack */
990 char display[256]; /* Brief state display, name, blocked/syspended */
991 char shortname[32]; /* To be used to name threads */
992 char more_display[256]; /* Long info, statistics, queue depth, whatever */
995 /* The volume of remote transfers can be limited by submitting
996 a mask containing bits specifying the desired information.
997 Use a union of these values as the 'selection' parameter to
998 get_thread_info. FIXME: Make these TAG names more thread specific.
1001 #define TAG_THREADID 1
1002 #define TAG_EXISTS 2
1003 #define TAG_DISPLAY 4
1004 #define TAG_THREADNAME 8
1005 #define TAG_MOREDISPLAY 16
1007 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
1009 char *unpack_varlen_hex (char *buff, int *result);
1011 static char *unpack_nibble (char *buf, int *val);
1013 static char *pack_nibble (char *buf, int nibble);
1015 static char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
1017 static char *unpack_byte (char *buf, int *value);
1019 static char *pack_int (char *buf, int value);
1021 static char *unpack_int (char *buf, int *value);
1023 static char *unpack_string (char *src, char *dest, int length);
1025 static char *pack_threadid (char *pkt, threadref * id);
1027 static char *unpack_threadid (char *inbuf, threadref * id);
1029 void int_to_threadref (threadref * id, int value);
1031 static int threadref_to_int (threadref * ref);
1033 static void copy_threadref (threadref * dest, threadref * src);
1035 static int threadmatch (threadref * dest, threadref * src);
1037 static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
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, int fieldset, /*TAG mask */
1046 struct gdb_ext_thread_info *info);
1048 static int adapt_remote_get_threadinfo (gdb_threadref * ref,
1050 struct gdb_ext_thread_info *info);
1052 static char *pack_threadlist_request (char *pkt, int startflag,
1054 threadref * nextthread);
1056 static int parse_threadlist_response (char *pkt,
1058 threadref * original_echo,
1059 threadref * resultlist, int *doneflag);
1061 static int remote_get_threadlist (int startflag,
1062 threadref * nextthread,
1065 int *result_count, threadref * threadlist);
1067 typedef int (*rmt_thread_action) (threadref * ref, void *context);
1069 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1070 void *context, int looplimit);
1072 static int remote_newthread_step (threadref * ref, void *context);
1074 /* encode 64 bits in 16 chars of hex */
1076 static const char hexchars[] = "0123456789abcdef";
1079 ishex (int ch, int *val)
1081 if ((ch >= 'a') && (ch <= 'f'))
1083 *val = ch - 'a' + 10;
1086 if ((ch >= 'A') && (ch <= 'F'))
1088 *val = ch - 'A' + 10;
1091 if ((ch >= '0') && (ch <= '9'))
1102 if (ch >= 'a' && ch <= 'f')
1103 return ch - 'a' + 10;
1104 if (ch >= '0' && ch <= '9')
1106 if (ch >= 'A' && ch <= 'F')
1107 return ch - 'A' + 10;
1112 stub_unpack_int (char *buff, int fieldlength)
1119 nibble = stubhex (*buff++);
1123 retval = retval << 4;
1129 unpack_varlen_hex (char *buff, /* packet to parse */
1135 while (ishex (*buff, &nibble))
1138 retval = retval << 4;
1139 retval |= nibble & 0x0f;
1146 unpack_nibble (char *buf, int *val)
1148 ishex (*buf++, val);
1153 pack_nibble (char *buf, int nibble)
1155 *buf++ = hexchars[(nibble & 0x0f)];
1160 pack_hex_byte (char *pkt, int byte)
1162 *pkt++ = hexchars[(byte >> 4) & 0xf];
1163 *pkt++ = hexchars[(byte & 0xf)];
1168 unpack_byte (char *buf, int *value)
1170 *value = stub_unpack_int (buf, 2);
1175 pack_int (char *buf, int value)
1177 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1178 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1179 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1180 buf = pack_hex_byte (buf, (value & 0xff));
1185 unpack_int (char *buf, int *value)
1187 *value = stub_unpack_int (buf, 8);
1191 #if 0 /* currently unused, uncomment when needed */
1192 static char *pack_string (char *pkt, char *string);
1195 pack_string (char *pkt, char *string)
1200 len = strlen (string);
1202 len = 200; /* Bigger than most GDB packets, junk??? */
1203 pkt = pack_hex_byte (pkt, len);
1207 if ((ch == '\0') || (ch == '#'))
1208 ch = '*'; /* Protect encapsulation */
1213 #endif /* 0 (unused) */
1216 unpack_string (char *src, char *dest, int length)
1225 pack_threadid (char *pkt, threadref *id)
1228 unsigned char *altid;
1230 altid = (unsigned char *) id;
1231 limit = pkt + BUF_THREAD_ID_SIZE;
1233 pkt = pack_hex_byte (pkt, *altid++);
1239 unpack_threadid (char *inbuf, threadref *id)
1242 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1245 altref = (char *) id;
1247 while (inbuf < limit)
1249 x = stubhex (*inbuf++);
1250 y = stubhex (*inbuf++);
1251 *altref++ = (x << 4) | y;
1256 /* Externally, threadrefs are 64 bits but internally, they are still
1257 ints. This is due to a mismatch of specifications. We would like
1258 to use 64bit thread references internally. This is an adapter
1262 int_to_threadref (threadref *id, int value)
1264 unsigned char *scan;
1266 scan = (unsigned char *) id;
1272 *scan++ = (value >> 24) & 0xff;
1273 *scan++ = (value >> 16) & 0xff;
1274 *scan++ = (value >> 8) & 0xff;
1275 *scan++ = (value & 0xff);
1279 threadref_to_int (threadref *ref)
1282 unsigned char *scan;
1284 scan = (char *) ref;
1288 value = (value << 8) | ((*scan++) & 0xff);
1293 copy_threadref (threadref *dest, threadref *src)
1296 unsigned char *csrc, *cdest;
1298 csrc = (unsigned char *) src;
1299 cdest = (unsigned char *) dest;
1306 threadmatch (threadref *dest, threadref *src)
1308 /* things are broken right now, so just assume we got a match */
1310 unsigned char *srcp, *destp;
1312 srcp = (char *) src;
1313 destp = (char *) dest;
1317 result &= (*srcp++ == *destp++) ? 1 : 0;
1324 threadid:1, # always request threadid
1331 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1334 pack_threadinfo_request (char *pkt, int mode, threadref *id)
1336 *pkt++ = 'q'; /* Info Query */
1337 *pkt++ = 'P'; /* process or thread info */
1338 pkt = pack_int (pkt, mode); /* mode */
1339 pkt = pack_threadid (pkt, id); /* threadid */
1340 *pkt = '\0'; /* terminate */
1344 /* These values tag the fields in a thread info response packet */
1345 /* Tagging the fields allows us to request specific fields and to
1346 add more fields as time goes by */
1348 #define TAG_THREADID 1 /* Echo the thread identifier */
1349 #define TAG_EXISTS 2 /* Is this process defined enough to
1350 fetch registers and its stack */
1351 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1352 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1353 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1357 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1358 struct gdb_ext_thread_info *info)
1363 char *limit = pkt + PBUFSIZ; /* plausable 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 stripped */
1373 pkt = unpack_int (pkt, &mask); /* arg mask */
1374 pkt = unpack_threadid (pkt, &ref);
1377 warning ("Incomplete response to threadinfo request\n");
1378 if (!threadmatch (&ref, expectedref))
1379 { /* This is an answer to a different request */
1380 warning ("ERROR RMT Thread info mismatch\n");
1383 copy_threadref (&info->threadid, &ref);
1385 /* Loop on tagged fields , try to bail if somthing goes wrong */
1387 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
1389 pkt = unpack_int (pkt, &tag); /* tag */
1390 pkt = unpack_byte (pkt, &length); /* length */
1391 if (!(tag & mask)) /* tags out of synch with mask */
1393 warning ("ERROR RMT: threadinfo tag mismatch\n");
1397 if (tag == TAG_THREADID)
1401 warning ("ERROR RMT: length of threadid is not 16\n");
1405 pkt = unpack_threadid (pkt, &ref);
1406 mask = mask & ~TAG_THREADID;
1409 if (tag == TAG_EXISTS)
1411 info->active = stub_unpack_int (pkt, length);
1413 mask = mask & ~(TAG_EXISTS);
1416 warning ("ERROR RMT: 'exists' length too long\n");
1422 if (tag == TAG_THREADNAME)
1424 pkt = unpack_string (pkt, &info->shortname[0], length);
1425 mask = mask & ~TAG_THREADNAME;
1428 if (tag == TAG_DISPLAY)
1430 pkt = unpack_string (pkt, &info->display[0], length);
1431 mask = mask & ~TAG_DISPLAY;
1434 if (tag == TAG_MOREDISPLAY)
1436 pkt = unpack_string (pkt, &info->more_display[0], length);
1437 mask = mask & ~TAG_MOREDISPLAY;
1440 warning ("ERROR RMT: unknown thread info tag\n");
1441 break; /* Not a tag we know about */
1447 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1448 struct gdb_ext_thread_info *info)
1451 char *threadinfo_pkt = alloca (PBUFSIZ);
1453 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1454 putpkt (threadinfo_pkt);
1455 getpkt (threadinfo_pkt, PBUFSIZ, 0);
1456 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1461 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1462 representation of a threadid. */
1465 adapt_remote_get_threadinfo (gdb_threadref *ref, int selection,
1466 struct gdb_ext_thread_info *info)
1470 int_to_threadref (&lclref, *ref);
1471 return remote_get_threadinfo (&lclref, selection, info);
1474 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1477 pack_threadlist_request (char *pkt, int startflag, int threadcount,
1478 threadref *nextthread)
1480 *pkt++ = 'q'; /* info query packet */
1481 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1482 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1483 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1484 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1489 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1492 parse_threadlist_response (char *pkt, int result_limit,
1493 threadref *original_echo, threadref *resultlist,
1497 int count, resultcount, done;
1500 /* Assume the 'q' and 'M chars have been stripped. */
1501 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */
1502 pkt = unpack_byte (pkt, &count); /* count field */
1503 pkt = unpack_nibble (pkt, &done);
1504 /* The first threadid is the argument threadid. */
1505 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1506 while ((count-- > 0) && (pkt < limit))
1508 pkt = unpack_threadid (pkt, resultlist++);
1509 if (resultcount++ >= result_limit)
1518 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1519 int *done, int *result_count, threadref *threadlist)
1521 static threadref echo_nextthread;
1522 char *threadlist_packet = alloca (PBUFSIZ);
1523 char *t_response = alloca (PBUFSIZ);
1526 /* Trancate result limit to be smaller than the packet size */
1527 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
1528 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
1530 pack_threadlist_request (threadlist_packet,
1531 startflag, result_limit, nextthread);
1532 putpkt (threadlist_packet);
1533 getpkt (t_response, PBUFSIZ, 0);
1536 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1539 if (!threadmatch (&echo_nextthread, nextthread))
1541 /* FIXME: This is a good reason to drop the packet */
1542 /* Possably, there is a duplicate response */
1544 retransmit immediatly - race conditions
1545 retransmit after timeout - yes
1547 wait for packet, then exit
1549 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1550 return 0; /* I choose simply exiting */
1552 if (*result_count <= 0)
1556 warning ("RMT ERROR : failed to get remote thread list\n");
1559 return result; /* break; */
1561 if (*result_count > result_limit)
1564 warning ("RMT ERROR: threadlist response longer than requested\n");
1570 /* This is the interface between remote and threads, remotes upper interface */
1572 /* remote_find_new_threads retrieves the thread list and for each
1573 thread in the list, looks up the thread in GDB's internal list,
1574 ading the thread if it does not already exist. This involves
1575 getting partial thread lists from the remote target so, polling the
1576 quit_flag is required. */
1579 /* About this many threadisds fit in a packet. */
1581 #define MAXTHREADLISTRESULTS 32
1584 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1587 int done, i, result_count;
1591 static threadref nextthread;
1592 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1597 if (loopcount++ > looplimit)
1600 warning ("Remote fetch threadlist -infinite loop-\n");
1603 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1604 &done, &result_count, resultthreadlist))
1609 /* clear for later iterations */
1611 /* Setup to resume next batch of thread references, set nextthread. */
1612 if (result_count >= 1)
1613 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1615 while (result_count--)
1616 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1623 remote_newthread_step (threadref *ref, void *context)
1627 ptid = pid_to_ptid (threadref_to_int (ref));
1629 if (!in_thread_list (ptid))
1631 return 1; /* continue iterator */
1634 #define CRAZY_MAX_THREADS 1000
1637 remote_current_thread (ptid_t oldpid)
1639 char *buf = alloca (PBUFSIZ);
1642 getpkt (buf, PBUFSIZ, 0);
1643 if (buf[0] == 'Q' && buf[1] == 'C')
1644 return pid_to_ptid (strtol (&buf[2], NULL, 16));
1649 /* Find new threads for info threads command.
1650 * Original version, using John Metzler's thread protocol.
1654 remote_find_new_threads (void)
1656 remote_threadlist_iterator (remote_newthread_step, 0,
1658 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1659 inferior_ptid = remote_current_thread (inferior_ptid);
1663 * Find all threads for info threads command.
1664 * Uses new thread protocol contributed by Cisco.
1665 * Falls back and attempts to use the older method (above)
1666 * if the target doesn't respond to the new method.
1670 remote_threads_info (void)
1672 char *buf = alloca (PBUFSIZ);
1676 if (remote_desc == 0) /* paranoia */
1677 error ("Command can only be used when connected to the remote target.");
1679 if (use_threadinfo_query)
1681 putpkt ("qfThreadInfo");
1683 getpkt (bufp, PBUFSIZ, 0);
1684 if (bufp[0] != '\0') /* q packet recognized */
1686 while (*bufp++ == 'm') /* reply contains one or more TID */
1690 tid = strtol (bufp, &bufp, 16);
1691 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1692 add_thread (pid_to_ptid (tid));
1694 while (*bufp++ == ','); /* comma-separated list */
1695 putpkt ("qsThreadInfo");
1697 getpkt (bufp, PBUFSIZ, 0);
1703 /* Else fall back to old method based on jmetzler protocol. */
1704 use_threadinfo_query = 0;
1705 remote_find_new_threads ();
1710 * Collect a descriptive string about the given thread.
1711 * The target may say anything it wants to about the thread
1712 * (typically info about its blocked / runnable state, name, etc.).
1713 * This string will appear in the info threads display.
1715 * Optional: targets are not required to implement this function.
1719 remote_threads_extra_info (struct thread_info *tp)
1724 struct gdb_ext_thread_info threadinfo;
1725 static char display_buf[100]; /* arbitrary... */
1726 char *bufp = alloca (PBUFSIZ);
1727 int n = 0; /* position in display_buf */
1729 if (remote_desc == 0) /* paranoia */
1730 internal_error (__FILE__, __LINE__,
1731 "remote_threads_extra_info");
1733 if (use_threadextra_query)
1735 sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid));
1737 getpkt (bufp, PBUFSIZ, 0);
1740 n = min (strlen (bufp) / 2, sizeof (display_buf));
1741 result = hex2bin (bufp, display_buf, n);
1742 display_buf [result] = '\0';
1747 /* If the above query fails, fall back to the old method. */
1748 use_threadextra_query = 0;
1749 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1750 | TAG_MOREDISPLAY | TAG_DISPLAY;
1751 int_to_threadref (&id, PIDGET (tp->ptid));
1752 if (remote_get_threadinfo (&id, set, &threadinfo))
1753 if (threadinfo.active)
1755 if (*threadinfo.shortname)
1756 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1757 if (*threadinfo.display)
1758 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1759 if (*threadinfo.more_display)
1760 n += sprintf(&display_buf[n], " Priority: %s",
1761 threadinfo.more_display);
1765 /* for purely cosmetic reasons, clear up trailing commas */
1766 if (',' == display_buf[n-1])
1767 display_buf[n-1] = ' ';
1776 /* Restart the remote side; this is an extended protocol operation. */
1779 extended_remote_restart (void)
1781 char *buf = alloca (PBUFSIZ);
1783 /* Send the restart command; for reasons I don't understand the
1784 remote side really expects a number after the "R". */
1786 sprintf (&buf[1], "%x", 0);
1789 /* Now query for status so this looks just like we restarted
1790 gdbserver from scratch. */
1792 getpkt (buf, PBUFSIZ, 0);
1795 /* Clean up connection to a remote debugger. */
1799 remote_close (int quitting)
1802 SERIAL_CLOSE (remote_desc);
1806 /* Query the remote side for the text, data and bss offsets. */
1811 char *buf = alloca (PBUFSIZ);
1814 CORE_ADDR text_addr, data_addr, bss_addr;
1815 struct section_offsets *offs;
1817 putpkt ("qOffsets");
1819 getpkt (buf, PBUFSIZ, 0);
1821 if (buf[0] == '\000')
1822 return; /* Return silently. Stub doesn't support
1826 warning ("Remote failure reply: %s", buf);
1830 /* Pick up each field in turn. This used to be done with scanf, but
1831 scanf will make trouble if CORE_ADDR size doesn't match
1832 conversion directives correctly. The following code will work
1833 with any size of CORE_ADDR. */
1834 text_addr = data_addr = bss_addr = 0;
1838 if (strncmp (ptr, "Text=", 5) == 0)
1841 /* Don't use strtol, could lose on big values. */
1842 while (*ptr && *ptr != ';')
1843 text_addr = (text_addr << 4) + fromhex (*ptr++);
1848 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1851 while (*ptr && *ptr != ';')
1852 data_addr = (data_addr << 4) + fromhex (*ptr++);
1857 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1860 while (*ptr && *ptr != ';')
1861 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1867 error ("Malformed response to offset query, %s", buf);
1869 if (symfile_objfile == NULL)
1872 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1873 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1875 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
1877 /* This is a temporary kludge to force data and bss to use the same offsets
1878 because that's what nlmconv does now. The real solution requires changes
1879 to the stub and remote.c that I don't have time to do right now. */
1881 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1882 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
1884 objfile_relocate (symfile_objfile, offs);
1888 * Cisco version of section offsets:
1890 * Instead of having GDB query the target for the section offsets,
1891 * Cisco lets the target volunteer the information! It's also in
1892 * a different format, so here are the functions that will decode
1893 * a section offset packet from a Cisco target.
1897 * Function: remote_cisco_section_offsets
1899 * Returns: zero for success, non-zero for failure
1903 remote_cisco_section_offsets (bfd_vma text_addr,
1906 bfd_signed_vma *text_offs,
1907 bfd_signed_vma *data_offs,
1908 bfd_signed_vma *bss_offs)
1910 bfd_vma text_base, data_base, bss_base;
1911 struct minimal_symbol *start;
1916 if (symfile_objfile == NULL)
1917 return -1; /* no can do nothin' */
1919 start = lookup_minimal_symbol ("_start", NULL, NULL);
1921 return -1; /* Can't find "_start" symbol */
1923 data_base = bss_base = 0;
1924 text_base = SYMBOL_VALUE_ADDRESS (start);
1926 abfd = symfile_objfile->obfd;
1927 for (sect = abfd->sections;
1931 const char *p = bfd_get_section_name (abfd, sect);
1933 if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */
1934 if (data_base == 0 ||
1935 data_base > bfd_get_section_vma (abfd, sect))
1936 data_base = bfd_get_section_vma (abfd, sect);
1937 if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */
1938 if (bss_base == 0 ||
1939 bss_base > bfd_get_section_vma (abfd, sect))
1940 bss_base = bfd_get_section_vma (abfd, sect);
1942 *text_offs = text_addr - text_base;
1943 *data_offs = data_addr - data_base;
1944 *bss_offs = bss_addr - bss_base;
1949 sprintf (tmp, "VMA: text = 0x");
1950 sprintf_vma (tmp + strlen (tmp), text_addr);
1951 sprintf (tmp + strlen (tmp), " data = 0x");
1952 sprintf_vma (tmp + strlen (tmp), data_addr);
1953 sprintf (tmp + strlen (tmp), " bss = 0x");
1954 sprintf_vma (tmp + strlen (tmp), bss_addr);
1955 fprintf_filtered (gdb_stdlog, tmp);
1956 fprintf_filtered (gdb_stdlog,
1957 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1958 paddr_nz (*text_offs),
1959 paddr_nz (*data_offs),
1960 paddr_nz (*bss_offs));
1967 * Function: remote_cisco_objfile_relocate
1969 * Relocate the symbol file for a remote target.
1973 remote_cisco_objfile_relocate (bfd_signed_vma text_off, bfd_signed_vma data_off,
1974 bfd_signed_vma bss_off)
1976 struct section_offsets *offs;
1978 if (text_off != 0 || data_off != 0 || bss_off != 0)
1980 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1981 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1982 simple canonical representation for this stuff. */
1984 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1985 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1987 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_off;
1988 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_off;
1989 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = bss_off;
1991 /* First call the standard objfile_relocate. */
1992 objfile_relocate (symfile_objfile, offs);
1994 /* Now we need to fix up the section entries already attached to
1995 the exec target. These entries will control memory transfers
1996 from the exec file. */
1998 exec_set_section_offsets (text_off, data_off, bss_off);
2002 /* Stub for catch_errors. */
2005 remote_start_remote_dummy (void *dummy)
2007 start_remote (); /* Initialize gdb process mechanisms */
2012 remote_start_remote (PTR dummy)
2014 immediate_quit++; /* Allow user to interrupt it */
2016 /* Ack any packet which the remote side has already sent. */
2017 SERIAL_WRITE (remote_desc, "+", 1);
2019 /* Let the stub know that we want it to return the thread. */
2022 inferior_ptid = remote_current_thread (inferior_ptid);
2024 get_offsets (); /* Get text, data & bss offsets */
2026 putpkt ("?"); /* initiate a query from remote machine */
2029 return remote_start_remote_dummy (dummy);
2032 /* Open a connection to a remote debugger.
2033 NAME is the filename used for communication. */
2036 remote_open (char *name, int from_tty)
2038 remote_open_1 (name, from_tty, &remote_ops, 0);
2041 /* Just like remote_open, but with asynchronous support. */
2043 remote_async_open (char *name, int from_tty)
2045 remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
2048 /* Open a connection to a remote debugger using the extended
2049 remote gdb protocol. NAME is the filename used for communication. */
2052 extended_remote_open (char *name, int from_tty)
2054 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ );
2057 /* Just like extended_remote_open, but with asynchronous support. */
2059 extended_remote_async_open (char *name, int from_tty)
2061 remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ );
2064 /* Generic code for opening a connection to a remote target. */
2067 init_all_packet_configs (void)
2070 update_packet_config (&remote_protocol_e);
2071 update_packet_config (&remote_protocol_E);
2072 update_packet_config (&remote_protocol_P);
2073 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
2074 update_packet_config (&remote_protocol_Z[i]);
2075 /* Force remote_write_bytes to check whether target supports binary
2077 update_packet_config (&remote_protocol_binary_download);
2081 remote_open_1 (char *name, int from_tty, struct target_ops *target,
2085 error ("To open a remote debug connection, you need to specify what\n\
2086 serial device is attached to the remote system\n\
2087 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2089 /* See FIXME above */
2090 wait_forever_enabled_p = 1;
2092 target_preopen (from_tty);
2094 unpush_target (target);
2096 remote_desc = SERIAL_OPEN (name);
2098 perror_with_name (name);
2100 if (baud_rate != -1)
2102 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2104 SERIAL_CLOSE (remote_desc);
2105 perror_with_name (name);
2109 SERIAL_RAW (remote_desc);
2111 /* If there is something sitting in the buffer we might take it as a
2112 response to a command, which would be bad. */
2113 SERIAL_FLUSH_INPUT (remote_desc);
2117 puts_filtered ("Remote debugging using ");
2118 puts_filtered (name);
2119 puts_filtered ("\n");
2121 push_target (target); /* Switch to using remote target now */
2123 init_all_packet_configs ();
2125 general_thread = -2;
2126 continue_thread = -2;
2128 /* Probe for ability to use "ThreadInfo" query, as required. */
2129 use_threadinfo_query = 1;
2130 use_threadextra_query = 1;
2132 /* Without this, some commands which require an active target (such
2133 as kill) won't work. This variable serves (at least) double duty
2134 as both the pid of the target process (if it has such), and as a
2135 flag indicating that a target is active. These functions should
2136 be split out into seperate variables, especially since GDB will
2137 someday have a notion of debugging several processes. */
2139 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2140 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2141 /* First delete any symbols previously loaded from shared libraries. */
2142 no_shared_libraries (NULL, 0);
2145 /* Start the remote connection; if error (0), discard this target.
2146 In particular, if the user quits, be sure to discard it
2147 (we'd be in an inconsistent state otherwise). */
2148 if (!catch_errors (remote_start_remote, NULL,
2149 "Couldn't establish connection to remote target\n",
2158 /* Tell the remote that we are using the extended protocol. */
2159 char *buf = alloca (PBUFSIZ);
2161 getpkt (buf, PBUFSIZ, 0);
2163 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2164 /* FIXME: need a master target_open vector from which all
2165 remote_opens can be called, so that stuff like this can
2166 go there. Failing that, the following code must be copied
2167 to the open function for any remote target that wants to
2168 support svr4 shared libraries. */
2170 /* Set up to detect and load shared libraries. */
2171 if (exec_bfd) /* No use without an exec file. */
2172 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2176 /* Just like remote_open but with asynchronous support. */
2178 remote_async_open_1 (char *name, int from_tty, struct target_ops *target,
2182 error ("To open a remote debug connection, you need to specify what\n\
2183 serial device is attached to the remote system\n\
2184 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2186 target_preopen (from_tty);
2188 unpush_target (target);
2190 remote_desc = SERIAL_OPEN (name);
2192 perror_with_name (name);
2194 if (baud_rate != -1)
2196 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2198 SERIAL_CLOSE (remote_desc);
2199 perror_with_name (name);
2203 SERIAL_RAW (remote_desc);
2205 /* If there is something sitting in the buffer we might take it as a
2206 response to a command, which would be bad. */
2207 SERIAL_FLUSH_INPUT (remote_desc);
2211 puts_filtered ("Remote debugging using ");
2212 puts_filtered (name);
2213 puts_filtered ("\n");
2216 push_target (target); /* Switch to using remote target now */
2218 init_all_packet_configs ();
2220 general_thread = -2;
2221 continue_thread = -2;
2223 /* Probe for ability to use "ThreadInfo" query, as required. */
2224 use_threadinfo_query = 1;
2225 use_threadextra_query = 1;
2227 /* Without this, some commands which require an active target (such
2228 as kill) won't work. This variable serves (at least) double duty
2229 as both the pid of the target process (if it has such), and as a
2230 flag indicating that a target is active. These functions should
2231 be split out into seperate variables, especially since GDB will
2232 someday have a notion of debugging several processes. */
2233 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2235 /* With this target we start out by owning the terminal. */
2236 remote_async_terminal_ours_p = 1;
2238 /* FIXME: cagney/1999-09-23: During the initial connection it is
2239 assumed that the target is already ready and able to respond to
2240 requests. Unfortunately remote_start_remote() eventually calls
2241 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2242 around this. Eventually a mechanism that allows
2243 wait_for_inferior() to expect/get timeouts will be
2245 wait_forever_enabled_p = 0;
2247 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2248 /* First delete any symbols previously loaded from shared libraries. */
2249 no_shared_libraries (NULL, 0);
2252 /* Start the remote connection; if error (0), discard this target.
2253 In particular, if the user quits, be sure to discard it
2254 (we'd be in an inconsistent state otherwise). */
2255 if (!catch_errors (remote_start_remote, NULL,
2256 "Couldn't establish connection to remote target\n",
2260 wait_forever_enabled_p = 1;
2264 wait_forever_enabled_p = 1;
2268 /* Tell the remote that we are using the extended protocol. */
2269 char *buf = alloca (PBUFSIZ);
2271 getpkt (buf, PBUFSIZ, 0);
2273 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2274 /* FIXME: need a master target_open vector from which all
2275 remote_opens can be called, so that stuff like this can
2276 go there. Failing that, the following code must be copied
2277 to the open function for any remote target that wants to
2278 support svr4 shared libraries. */
2280 /* Set up to detect and load shared libraries. */
2281 if (exec_bfd) /* No use without an exec file. */
2282 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2286 /* This takes a program previously attached to and detaches it. After
2287 this is done, GDB can be used to debug some other program. We
2288 better not have left any breakpoints in the target program or it'll
2289 die when it hits one. */
2292 remote_detach (char *args, int from_tty)
2294 char *buf = alloca (PBUFSIZ);
2297 error ("Argument given to \"detach\" when remotely debugging.");
2299 /* Tell the remote target to detach. */
2301 remote_send (buf, PBUFSIZ);
2303 target_mourn_inferior ();
2305 puts_filtered ("Ending remote debugging.\n");
2309 /* Same as remote_detach, but with async support. */
2311 remote_async_detach (char *args, int from_tty)
2313 char *buf = alloca (PBUFSIZ);
2316 error ("Argument given to \"detach\" when remotely debugging.");
2318 /* Tell the remote target to detach. */
2320 remote_send (buf, PBUFSIZ);
2322 /* Unregister the file descriptor from the event loop. */
2323 if (target_is_async_p ())
2324 SERIAL_ASYNC (remote_desc, NULL, 0);
2326 target_mourn_inferior ();
2328 puts_filtered ("Ending remote debugging.\n");
2331 /* Convert hex digit A to a number. */
2336 if (a >= '0' && a <= '9')
2338 else if (a >= 'a' && a <= 'f')
2339 return a - 'a' + 10;
2340 else if (a >= 'A' && a <= 'F')
2341 return a - 'A' + 10;
2343 error ("Reply contains invalid hex digit %d", a);
2347 hex2bin (const char *hex, char *bin, int count)
2351 for (i = 0; i < count; i++)
2353 if (hex[0] == 0 || hex[1] == 0)
2355 /* Hex string is short, or of uneven length.
2356 Return the count that has been converted so far. */
2359 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2365 /* Convert number NIB to a hex digit. */
2373 return 'a' + nib - 10;
2377 bin2hex (const char *bin, char *hex, int count)
2380 /* May use a length, or a nul-terminated string as input. */
2382 count = strlen (bin);
2384 for (i = 0; i < count; i++)
2386 *hex++ = tohex ((*bin >> 4) & 0xf);
2387 *hex++ = tohex (*bin++ & 0xf);
2393 /* Tell the remote machine to resume. */
2395 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2397 static int last_sent_step;
2400 remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
2402 char *buf = alloca (PBUFSIZ);
2403 int pid = PIDGET (ptid);
2407 set_thread (0, 0); /* run any thread */
2409 set_thread (pid, 0); /* run this thread */
2411 last_sent_signal = siggnal;
2412 last_sent_step = step;
2414 /* A hook for when we need to do something at the last moment before
2416 if (target_resume_hook)
2417 (*target_resume_hook) ();
2420 /* The s/S/c/C packets do not return status. So if the target does
2421 not support the S or C packets, the debug agent returns an empty
2422 string which is detected in remote_wait(). This protocol defect
2423 is fixed in the e/E packets. */
2425 if (step && step_range_end)
2427 /* If the target does not support the 'E' packet, we try the 'S'
2428 packet. Ideally we would fall back to the 'e' packet if that
2429 too is not supported. But that would require another copy of
2430 the code to issue the 'e' packet (and fall back to 's' if not
2431 supported) in remote_wait(). */
2433 if (siggnal != TARGET_SIGNAL_0)
2435 if (remote_protocol_E.support != PACKET_DISABLE)
2439 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2440 *p++ = tohex (((int) siggnal) & 0xf);
2442 p += hexnumstr (p, (ULONGEST) step_range_start);
2444 p += hexnumstr (p, (ULONGEST) step_range_end);
2448 getpkt (buf, PBUFSIZ, 0);
2450 if (packet_ok (buf, &remote_protocol_E) == PACKET_OK)
2456 if (remote_protocol_e.support != PACKET_DISABLE)
2460 p += hexnumstr (p, (ULONGEST) step_range_start);
2462 p += hexnumstr (p, (ULONGEST) step_range_end);
2466 getpkt (buf, PBUFSIZ, 0);
2468 if (packet_ok (buf, &remote_protocol_e) == PACKET_OK)
2474 if (siggnal != TARGET_SIGNAL_0)
2476 buf[0] = step ? 'S' : 'C';
2477 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2478 buf[2] = tohex (((int) siggnal) & 0xf);
2482 strcpy (buf, step ? "s" : "c");
2487 /* Same as remote_resume, but with async support. */
2489 remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2491 char *buf = alloca (PBUFSIZ);
2492 int pid = PIDGET (ptid);
2496 set_thread (0, 0); /* run any thread */
2498 set_thread (pid, 0); /* run this thread */
2500 last_sent_signal = siggnal;
2501 last_sent_step = step;
2503 /* A hook for when we need to do something at the last moment before
2505 if (target_resume_hook)
2506 (*target_resume_hook) ();
2508 /* The s/S/c/C packets do not return status. So if the target does
2509 not support the S or C packets, the debug agent returns an empty
2510 string which is detected in remote_wait(). This protocol defect
2511 is fixed in the e/E packets. */
2513 if (step && step_range_end)
2515 /* If the target does not support the 'E' packet, we try the 'S'
2516 packet. Ideally we would fall back to the 'e' packet if that
2517 too is not supported. But that would require another copy of
2518 the code to issue the 'e' packet (and fall back to 's' if not
2519 supported) in remote_wait(). */
2521 if (siggnal != TARGET_SIGNAL_0)
2523 if (remote_protocol_E.support != PACKET_DISABLE)
2527 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2528 *p++ = tohex (((int) siggnal) & 0xf);
2530 p += hexnumstr (p, (ULONGEST) step_range_start);
2532 p += hexnumstr (p, (ULONGEST) step_range_end);
2536 getpkt (buf, PBUFSIZ, 0);
2538 if (packet_ok (buf, &remote_protocol_E) == PACKET_OK)
2539 goto register_event_loop;
2544 if (remote_protocol_e.support != PACKET_DISABLE)
2548 p += hexnumstr (p, (ULONGEST) step_range_start);
2550 p += hexnumstr (p, (ULONGEST) step_range_end);
2554 getpkt (buf, PBUFSIZ, 0);
2556 if (packet_ok (buf, &remote_protocol_e) == PACKET_OK)
2557 goto register_event_loop;
2562 if (siggnal != TARGET_SIGNAL_0)
2564 buf[0] = step ? 'S' : 'C';
2565 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2566 buf[2] = tohex ((int) siggnal & 0xf);
2570 strcpy (buf, step ? "s" : "c");
2574 register_event_loop:
2575 /* We are about to start executing the inferior, let's register it
2576 with the event loop. NOTE: this is the one place where all the
2577 execution commands end up. We could alternatively do this in each
2578 of the execution commands in infcmd.c.*/
2579 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2580 into infcmd.c in order to allow inferior function calls to work
2581 NOT asynchronously. */
2582 if (event_loop_p && target_can_async_p ())
2583 target_async (inferior_event_handler, 0);
2584 /* Tell the world that the target is now executing. */
2585 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2586 this? Instead, should the client of target just assume (for
2587 async targets) that the target is going to start executing? Is
2588 this information already found in the continuation block? */
2589 if (target_is_async_p ())
2590 target_executing = 1;
2594 /* Set up the signal handler for SIGINT, while the target is
2595 executing, ovewriting the 'regular' SIGINT signal handler. */
2597 initialize_sigint_signal_handler (void)
2599 sigint_remote_token =
2600 create_async_signal_handler (async_remote_interrupt, NULL);
2601 signal (SIGINT, handle_remote_sigint);
2604 /* Signal handler for SIGINT, while the target is executing. */
2606 handle_remote_sigint (int sig)
2608 signal (sig, handle_remote_sigint_twice);
2609 sigint_remote_twice_token =
2610 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2611 mark_async_signal_handler_wrapper (sigint_remote_token);
2614 /* Signal handler for SIGINT, installed after SIGINT has already been
2615 sent once. It will take effect the second time that the user sends
2618 handle_remote_sigint_twice (int sig)
2620 signal (sig, handle_sigint);
2621 sigint_remote_twice_token =
2622 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2623 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2626 /* Perform the real interruption of the target execution, in response
2629 async_remote_interrupt (gdb_client_data arg)
2632 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2637 /* Perform interrupt, if the first attempt did not succeed. Just give
2638 up on the target alltogether. */
2640 async_remote_interrupt_twice (gdb_client_data arg)
2643 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2644 /* Do something only if the target was not killed by the previous
2646 if (target_executing)
2649 signal (SIGINT, handle_remote_sigint);
2653 /* Reinstall the usual SIGINT handlers, after the target has
2656 cleanup_sigint_signal_handler (void *dummy)
2658 signal (SIGINT, handle_sigint);
2659 if (sigint_remote_twice_token)
2660 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
2661 if (sigint_remote_token)
2662 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
2665 /* Send ^C to target to halt it. Target will respond, and send us a
2667 static void (*ofunc) (int);
2669 /* The command line interface's stop routine. This function is installed
2670 as a signal handler for SIGINT. The first time a user requests a
2671 stop, we call remote_stop to send a break or ^C. If there is no
2672 response from the target (it didn't stop when the user requested it),
2673 we ask the user if he'd like to detach from the target. */
2675 remote_interrupt (int signo)
2677 /* If this doesn't work, try more severe steps. */
2678 signal (signo, remote_interrupt_twice);
2681 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2686 /* The user typed ^C twice. */
2689 remote_interrupt_twice (int signo)
2691 signal (signo, ofunc);
2693 signal (signo, remote_interrupt);
2696 /* This is the generic stop called via the target vector. When a target
2697 interrupt is requested, either by the command line or the GUI, we
2698 will eventually end up here. */
2702 /* Send a break or a ^C, depending on user preference. */
2704 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2707 SERIAL_SEND_BREAK (remote_desc);
2709 SERIAL_WRITE (remote_desc, "\003", 1);
2712 /* Ask the user what to do when an interrupt is received. */
2715 interrupt_query (void)
2717 target_terminal_ours ();
2719 if (query ("Interrupted while waiting for the program.\n\
2720 Give up (and stop debugging it)? "))
2722 target_mourn_inferior ();
2723 return_to_top_level (RETURN_QUIT);
2726 target_terminal_inferior ();
2729 /* Enable/disable target terminal ownership. Most targets can use
2730 terminal groups to control terminal ownership. Remote targets are
2731 different in that explicit transfer of ownership to/from GDB/target
2735 remote_async_terminal_inferior (void)
2737 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2738 sync_execution here. This function should only be called when
2739 GDB is resuming the inferior in the forground. A background
2740 resume (``run&'') should leave GDB in control of the terminal and
2741 consequently should not call this code. */
2742 if (!sync_execution)
2744 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2745 calls target_terminal_*() idenpotent. The event-loop GDB talking
2746 to an asynchronous target with a synchronous command calls this
2747 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2748 stops trying to transfer the terminal to the target when it
2749 shouldn't this guard can go away. */
2750 if (!remote_async_terminal_ours_p)
2752 delete_file_handler (input_fd);
2753 remote_async_terminal_ours_p = 0;
2754 initialize_sigint_signal_handler ();
2755 /* NOTE: At this point we could also register our selves as the
2756 recipient of all input. Any characters typed could then be
2757 passed on down to the target. */
2761 remote_async_terminal_ours (void)
2763 /* See FIXME in remote_async_terminal_inferior. */
2764 if (!sync_execution)
2766 /* See FIXME in remote_async_terminal_inferior. */
2767 if (remote_async_terminal_ours_p)
2769 cleanup_sigint_signal_handler (NULL);
2770 add_file_handler (input_fd, stdin_event_handler, 0);
2771 remote_async_terminal_ours_p = 1;
2774 /* If nonzero, ignore the next kill. */
2779 remote_console_output (char *msg)
2783 for (p = msg; p[0] && p[1]; p += 2)
2786 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2789 fputs_unfiltered (tb, gdb_stdtarg);
2791 gdb_flush (gdb_stdtarg);
2794 /* Wait until the remote machine stops, then return,
2795 storing status in STATUS just as `wait' would.
2796 Returns "pid", which in the case of a multi-threaded
2797 remote OS, is the thread-id. */
2800 remote_wait (ptid_t ptid, struct target_waitstatus *status)
2802 unsigned char *buf = alloca (PBUFSIZ);
2803 int thread_num = -1;
2805 status->kind = TARGET_WAITKIND_EXITED;
2806 status->value.integer = 0;
2812 ofunc = signal (SIGINT, remote_interrupt);
2813 getpkt (buf, PBUFSIZ, 1);
2814 signal (SIGINT, ofunc);
2816 /* This is a hook for when we need to do something (perhaps the
2817 collection of trace data) every time the target stops. */
2818 if (target_wait_loop_hook)
2819 (*target_wait_loop_hook) ();
2823 case 'E': /* Error of some sort */
2824 warning ("Remote failure reply: %s", buf);
2826 case 'T': /* Status with PC, SP, FP, ... */
2830 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
2832 /* Expedited reply, containing Signal, {regno, reg} repeat */
2833 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2835 n... = register number
2836 r... = register contents
2838 p = &buf[3]; /* after Txx */
2846 /* Read the register number */
2847 regno = strtol ((const char *) p, &p_temp, 16);
2848 p1 = (unsigned char *) p_temp;
2850 if (p1 == p) /* No register number present here */
2852 p1 = (unsigned char *) strchr ((const char *) p, ':');
2854 warning ("Malformed packet(a) (missing colon): %s\n\
2857 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2859 p_temp = unpack_varlen_hex (++p1, &thread_num);
2860 record_currthread (thread_num);
2861 p = (unsigned char *) p_temp;
2869 warning ("Malformed packet(b) (missing colon): %s\n\
2873 if (regno >= NUM_REGS)
2874 warning ("Remote sent bad register number %ld: %s\n\
2878 fieldsize = hex2bin (p, regs, REGISTER_RAW_SIZE (regno));
2880 if (fieldsize < REGISTER_RAW_SIZE (regno))
2881 warning ("Remote reply is too short: %s", buf);
2882 supply_register (regno, regs);
2887 warning ("Remote register badly formatted: %s", buf);
2888 warning (" here: %s", p);
2893 case 'S': /* Old style status, just signal only */
2894 status->kind = TARGET_WAITKIND_STOPPED;
2895 status->value.sig = (enum target_signal)
2896 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2900 /* Export Cisco kernel mode as a convenience variable
2901 (so that it can be used in the GDB prompt if desired). */
2903 if (cisco_kernel_mode == 1)
2904 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2905 value_from_string ("PDEBUG-"));
2906 cisco_kernel_mode = 0;
2907 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2908 record_currthread (thread_num);
2910 else if (buf[3] == 'k')
2912 /* Export Cisco kernel mode as a convenience variable
2913 (so that it can be used in the GDB prompt if desired). */
2915 if (cisco_kernel_mode == 1)
2916 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2917 value_from_string ("KDEBUG-"));
2918 cisco_kernel_mode = 1;
2921 case 'N': /* Cisco special: status and offsets */
2923 bfd_vma text_addr, data_addr, bss_addr;
2924 bfd_signed_vma text_off, data_off, bss_off;
2927 status->kind = TARGET_WAITKIND_STOPPED;
2928 status->value.sig = (enum target_signal)
2929 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2931 if (symfile_objfile == NULL)
2933 warning ("Relocation packet received with no symbol file. \
2938 /* Relocate object file. Buffer format is NAATT;DD;BB
2939 * where AA is the signal number, TT is the new text
2940 * address, DD * is the new data address, and BB is the
2941 * new bss address. */
2944 text_addr = strtoul (p, (char **) &p1, 16);
2945 if (p1 == p || *p1 != ';')
2946 warning ("Malformed relocation packet: Packet '%s'", buf);
2948 data_addr = strtoul (p, (char **) &p1, 16);
2949 if (p1 == p || *p1 != ';')
2950 warning ("Malformed relocation packet: Packet '%s'", buf);
2952 bss_addr = strtoul (p, (char **) &p1, 16);
2954 warning ("Malformed relocation packet: Packet '%s'", buf);
2956 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2957 &text_off, &data_off, &bss_off)
2959 if (text_off != 0 || data_off != 0 || bss_off != 0)
2960 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2964 case 'W': /* Target exited */
2966 /* The remote process exited. */
2967 status->kind = TARGET_WAITKIND_EXITED;
2968 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2972 status->kind = TARGET_WAITKIND_SIGNALLED;
2973 status->value.sig = (enum target_signal)
2974 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2978 case 'O': /* Console output */
2979 remote_console_output (buf + 1);
2982 if (last_sent_signal != TARGET_SIGNAL_0)
2984 /* Zero length reply means that we tried 'S' or 'C' and
2985 the remote system doesn't support it. */
2986 target_terminal_ours_for_output ();
2988 ("Can't send signals to this remote system. %s not sent.\n",
2989 target_signal_to_name (last_sent_signal));
2990 last_sent_signal = TARGET_SIGNAL_0;
2991 target_terminal_inferior ();
2993 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2994 putpkt ((char *) buf);
2997 /* else fallthrough */
2999 warning ("Invalid remote reply: %s", buf);
3004 if (thread_num != -1)
3006 return pid_to_ptid (thread_num);
3008 return inferior_ptid;
3011 /* Async version of remote_wait. */
3013 remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
3015 unsigned char *buf = alloca (PBUFSIZ);
3016 int thread_num = -1;
3018 status->kind = TARGET_WAITKIND_EXITED;
3019 status->value.integer = 0;
3025 if (!target_is_async_p ())
3026 ofunc = signal (SIGINT, remote_interrupt);
3027 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3028 _never_ wait for ever -> test on target_is_async_p().
3029 However, before we do that we need to ensure that the caller
3030 knows how to take the target into/out of async mode. */
3031 getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
3032 if (!target_is_async_p ())
3033 signal (SIGINT, ofunc);
3035 /* This is a hook for when we need to do something (perhaps the
3036 collection of trace data) every time the target stops. */
3037 if (target_wait_loop_hook)
3038 (*target_wait_loop_hook) ();
3042 case 'E': /* Error of some sort */
3043 warning ("Remote failure reply: %s", buf);
3045 case 'T': /* Status with PC, SP, FP, ... */
3049 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
3051 /* Expedited reply, containing Signal, {regno, reg} repeat */
3052 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3054 n... = register number
3055 r... = register contents
3057 p = &buf[3]; /* after Txx */
3065 /* Read the register number */
3066 regno = strtol ((const char *) p, &p_temp, 16);
3067 p1 = (unsigned char *) p_temp;
3069 if (p1 == p) /* No register number present here */
3071 p1 = (unsigned char *) strchr ((const char *) p, ':');
3073 warning ("Malformed packet(a) (missing colon): %s\n\
3076 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
3078 p_temp = unpack_varlen_hex (++p1, &thread_num);
3079 record_currthread (thread_num);
3080 p = (unsigned char *) p_temp;
3088 warning ("Malformed packet(b) (missing colon): %s\n\
3092 if (regno >= NUM_REGS)
3093 warning ("Remote sent bad register number %ld: %s\n\
3097 fieldsize = hex2bin (p, regs, REGISTER_RAW_SIZE (regno));
3099 if (fieldsize < REGISTER_RAW_SIZE (regno))
3100 warning ("Remote reply is too short: %s", buf);
3101 supply_register (regno, regs);
3106 warning ("Remote register badly formatted: %s", buf);
3107 warning (" here: %s", p);
3112 case 'S': /* Old style status, just signal only */
3113 status->kind = TARGET_WAITKIND_STOPPED;
3114 status->value.sig = (enum target_signal)
3115 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3119 /* Export Cisco kernel mode as a convenience variable
3120 (so that it can be used in the GDB prompt if desired). */
3122 if (cisco_kernel_mode == 1)
3123 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
3124 value_from_string ("PDEBUG-"));
3125 cisco_kernel_mode = 0;
3126 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3127 record_currthread (thread_num);
3129 else if (buf[3] == 'k')
3131 /* Export Cisco kernel mode as a convenience variable
3132 (so that it can be used in the GDB prompt if desired). */
3134 if (cisco_kernel_mode == 1)
3135 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
3136 value_from_string ("KDEBUG-"));
3137 cisco_kernel_mode = 1;
3140 case 'N': /* Cisco special: status and offsets */
3142 bfd_vma text_addr, data_addr, bss_addr;
3143 bfd_signed_vma text_off, data_off, bss_off;
3146 status->kind = TARGET_WAITKIND_STOPPED;
3147 status->value.sig = (enum target_signal)
3148 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3150 if (symfile_objfile == NULL)
3152 warning ("Relocation packet recieved with no symbol file. \
3157 /* Relocate object file. Buffer format is NAATT;DD;BB
3158 * where AA is the signal number, TT is the new text
3159 * address, DD * is the new data address, and BB is the
3160 * new bss address. */
3163 text_addr = strtoul (p, (char **) &p1, 16);
3164 if (p1 == p || *p1 != ';')
3165 warning ("Malformed relocation packet: Packet '%s'", buf);
3167 data_addr = strtoul (p, (char **) &p1, 16);
3168 if (p1 == p || *p1 != ';')
3169 warning ("Malformed relocation packet: Packet '%s'", buf);
3171 bss_addr = strtoul (p, (char **) &p1, 16);
3173 warning ("Malformed relocation packet: Packet '%s'", buf);
3175 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
3176 &text_off, &data_off, &bss_off)
3178 if (text_off != 0 || data_off != 0 || bss_off != 0)
3179 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
3183 case 'W': /* Target exited */
3185 /* The remote process exited. */
3186 status->kind = TARGET_WAITKIND_EXITED;
3187 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3191 status->kind = TARGET_WAITKIND_SIGNALLED;
3192 status->value.sig = (enum target_signal)
3193 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3197 case 'O': /* Console output */
3198 remote_console_output (buf + 1);
3199 /* Return immediately to the event loop. The event loop will
3200 still be waiting on the inferior afterwards. */
3201 status->kind = TARGET_WAITKIND_IGNORE;
3204 if (last_sent_signal != TARGET_SIGNAL_0)
3206 /* Zero length reply means that we tried 'S' or 'C' and
3207 the remote system doesn't support it. */
3208 target_terminal_ours_for_output ();
3210 ("Can't send signals to this remote system. %s not sent.\n",
3211 target_signal_to_name (last_sent_signal));
3212 last_sent_signal = TARGET_SIGNAL_0;
3213 target_terminal_inferior ();
3215 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3216 putpkt ((char *) buf);
3219 /* else fallthrough */
3221 warning ("Invalid remote reply: %s", buf);
3226 if (thread_num != -1)
3228 return pid_to_ptid (thread_num);
3230 return inferior_ptid;
3233 /* Number of bytes of registers this stub implements. */
3235 static int register_bytes_found;
3237 /* Read the remote registers into the block REGS. */
3238 /* Currently we just read all the registers, so we don't use regno. */
3242 remote_fetch_registers (int regno)
3244 char *buf = alloca (PBUFSIZ);
3247 char *regs = alloca (REGISTER_BYTES);
3249 set_thread (PIDGET (inferior_ptid), 1);
3252 remote_send (buf, PBUFSIZ);
3254 /* Save the size of the packet sent to us by the target. Its used
3255 as a heuristic when determining the max size of packets that the
3256 target can safely receive. */
3257 if (actual_register_packet_size == 0)
3258 actual_register_packet_size = strlen (buf);
3260 /* Unimplemented registers read as all bits zero. */
3261 memset (regs, 0, REGISTER_BYTES);
3263 /* We can get out of synch in various cases. If the first character
3264 in the buffer is not a hex character, assume that has happened
3265 and try to fetch another packet to read. */
3266 while ((buf[0] < '0' || buf[0] > '9')
3267 && (buf[0] < 'a' || buf[0] > 'f')
3268 && buf[0] != 'x') /* New: unavailable register value */
3271 fprintf_unfiltered (gdb_stdlog,
3272 "Bad register packet; fetching a new packet\n");
3273 getpkt (buf, PBUFSIZ, 0);
3276 /* Reply describes registers byte by byte, each byte encoded as two
3277 hex characters. Suck them all up, then supply them to the
3278 register cacheing/storage mechanism. */
3281 for (i = 0; i < REGISTER_BYTES; i++)
3287 warning ("Remote reply is of odd length: %s", buf);
3288 /* Don't change register_bytes_found in this case, and don't
3289 print a second warning. */
3292 if (p[0] == 'x' && p[1] == 'x')
3293 regs[i] = 0; /* 'x' */
3295 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3299 if (i != register_bytes_found)
3301 register_bytes_found = i;
3302 if (REGISTER_BYTES_OK_P ()
3303 && !REGISTER_BYTES_OK (i))
3304 warning ("Remote reply is too short: %s", buf);
3308 for (i = 0; i < NUM_REGS; i++)
3310 supply_register (i, ®s[REGISTER_BYTE (i)]);
3311 if (buf[REGISTER_BYTE (i) * 2] == 'x')
3312 set_register_cached (i, -1);
3316 /* Prepare to store registers. Since we may send them all (using a
3317 'G' request), we have to read out the ones we don't want to change
3321 remote_prepare_to_store (void)
3323 /* Make sure the entire registers array is valid. */
3324 switch (remote_protocol_P.support)
3326 case PACKET_DISABLE:
3327 case PACKET_SUPPORT_UNKNOWN:
3328 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
3335 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
3336 packet was not recognized. */
3339 store_register_using_P (int regno)
3341 /* Try storing a single register. */
3342 char *buf = alloca (PBUFSIZ);
3347 sprintf (buf, "P%x=", regno);
3348 p = buf + strlen (buf);
3349 regp = register_buffer (regno);
3350 bin2hex (regp, p, REGISTER_RAW_SIZE (regno));
3351 remote_send (buf, PBUFSIZ);
3353 return buf[0] != '\0';
3357 /* Store register REGNO, or all registers if REGNO == -1, from the contents
3358 of the register cache buffer. FIXME: ignores errors. */
3361 remote_store_registers (int regno)
3363 char *buf = alloca (PBUFSIZ);
3368 set_thread (PIDGET (inferior_ptid), 1);
3372 switch (remote_protocol_P.support)
3374 case PACKET_DISABLE:
3377 if (store_register_using_P (regno))
3380 error ("Protocol error: P packet not recognized by stub");
3381 case PACKET_SUPPORT_UNKNOWN:
3382 if (store_register_using_P (regno))
3384 /* The stub recognized the 'P' packet. Remember this. */
3385 remote_protocol_P.support = PACKET_ENABLE;
3390 /* The stub does not support the 'P' packet. Use 'G'
3391 instead, and don't try using 'P' in the future (it
3392 will just waste our time). */
3393 remote_protocol_P.support = PACKET_DISABLE;
3401 /* Command describes registers byte by byte,
3402 each byte encoded as two hex characters. */
3404 regs = register_buffer (-1);
3406 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3407 bin2hex (regs, p, register_bytes_found);
3408 remote_send (buf, PBUFSIZ);
3412 /* Return the number of hex digits in num. */
3415 hexnumlen (ULONGEST num)
3419 for (i = 0; num != 0; i++)
3425 /* Set BUF to the minimum number of hex digits representing NUM. */
3428 hexnumstr (char *buf, ULONGEST num)
3430 int len = hexnumlen (num);
3431 return hexnumnstr (buf, num, len);
3435 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3438 hexnumnstr (char *buf, ULONGEST num, int width)
3444 for (i = width - 1; i >= 0; i--)
3446 buf[i] = "0123456789abcdef"[(num & 0xf)];
3453 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3456 remote_address_masked (CORE_ADDR addr)
3458 if (remote_address_size > 0
3459 && remote_address_size < (sizeof (ULONGEST) * 8))
3461 /* Only create a mask when that mask can safely be constructed
3462 in a ULONGEST variable. */
3464 mask = (mask << remote_address_size) - 1;
3470 /* Determine whether the remote target supports binary downloading.
3471 This is accomplished by sending a no-op memory write of zero length
3472 to the target at the specified address. It does not suffice to send
3473 the whole packet, since many stubs strip the eighth bit and subsequently
3474 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3476 NOTE: This can still lose if the serial line is not eight-bit
3477 clean. In cases like this, the user should clear "remote
3481 check_binary_download (CORE_ADDR addr)
3483 switch (remote_protocol_binary_download.support)
3485 case PACKET_DISABLE:
3489 case PACKET_SUPPORT_UNKNOWN:
3491 char *buf = alloca (PBUFSIZ);
3496 p += hexnumstr (p, (ULONGEST) addr);
3498 p += hexnumstr (p, (ULONGEST) 0);
3502 putpkt_binary (buf, (int) (p - buf));
3503 getpkt (buf, PBUFSIZ, 0);
3508 fprintf_unfiltered (gdb_stdlog,
3509 "binary downloading NOT suppported by target\n");
3510 remote_protocol_binary_download.support = PACKET_DISABLE;
3515 fprintf_unfiltered (gdb_stdlog,
3516 "binary downloading suppported by target\n");
3517 remote_protocol_binary_download.support = PACKET_ENABLE;
3524 /* Write memory data directly to the remote machine.
3525 This does not inform the data cache; the data cache uses this.
3526 MEMADDR is the address in the remote memory space.
3527 MYADDR is the address of the buffer in our space.
3528 LEN is the number of bytes.
3530 Returns number of bytes transferred, or 0 (setting errno) for
3531 error. Only transfer a single packet. */
3534 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3537 int max_buf_size; /* Max size of packet output buffer */
3539 unsigned char *plen;
3545 /* Verify that the target can support a binary download */
3546 check_binary_download (memaddr);
3548 /* Determine the max packet size. */
3549 max_buf_size = get_memory_write_packet_size ();
3550 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3551 buf = alloca (sizeof_buf);
3553 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3554 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3556 /* construct "M"<memaddr>","<len>":" */
3557 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3560 /* Append [XM]. Compute a best guess of the number of bytes
3561 actually transfered. */
3562 switch (remote_protocol_binary_download.support)
3566 /* Best guess at number of bytes that will fit. */
3567 todo = min (len, max_buf_size);
3569 case PACKET_DISABLE:
3571 /* num bytes that will fit */
3572 todo = min (len, max_buf_size / 2);
3574 case PACKET_SUPPORT_UNKNOWN:
3575 internal_error (__FILE__, __LINE__,
3576 "remote_write_bytes: bad internal state");
3578 internal_error (__FILE__, __LINE__, "bad switch");
3581 /* Append <memaddr> */
3582 memaddr = remote_address_masked (memaddr);
3583 p += hexnumstr (p, (ULONGEST) memaddr);
3586 /* Append <len>. Retain the location/size of <len>. It may
3587 need to be adjusted once the packet body has been created. */
3589 plenlen = hexnumstr (p, (ULONGEST) todo);
3594 /* Append the packet body. */
3595 switch (remote_protocol_binary_download.support)
3598 /* Binary mode. Send target system values byte by byte, in
3599 increasing byte addresses. Only escape certain critical
3602 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3605 switch (myaddr[nr_bytes] & 0xff)
3610 /* These must be escaped */
3612 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3615 *p++ = myaddr[nr_bytes] & 0xff;
3619 if (nr_bytes < todo)
3621 /* Escape chars have filled up the buffer prematurely,
3622 and we have actually sent fewer bytes than planned.
3623 Fix-up the length field of the packet. Use the same
3624 number of characters as before. */
3626 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3627 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3630 case PACKET_DISABLE:
3631 /* Normal mode: Send target system values byte by byte, in
3632 increasing byte addresses. Each byte is encoded as a two hex
3634 nr_bytes = bin2hex (myaddr, p, todo);
3636 case PACKET_SUPPORT_UNKNOWN:
3637 internal_error (__FILE__, __LINE__,
3638 "remote_write_bytes: bad internal state");
3640 internal_error (__FILE__, __LINE__, "bad switch");
3643 putpkt_binary (buf, (int) (p - buf));
3644 getpkt (buf, sizeof_buf, 0);
3648 /* There is no correspondance between what the remote protocol
3649 uses for errors and errno codes. We would like a cleaner way
3650 of representing errors (big enough to include errno codes,
3651 bfd_error codes, and others). But for now just return EIO. */
3656 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3657 bytes than we'd planned. */
3661 /* Read memory data directly from the remote machine.
3662 This does not use the data cache; the data cache uses this.
3663 MEMADDR is the address in the remote memory space.
3664 MYADDR is the address of the buffer in our space.
3665 LEN is the number of bytes.
3667 Returns number of bytes transferred, or 0 for error. */
3669 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3670 remote targets) shouldn't attempt to read the entire buffer.
3671 Instead it should read a single packet worth of data and then
3672 return the byte size of that packet to the caller. The caller (its
3673 caller and its callers caller ;-) already contains code for
3674 handling partial reads. */
3677 remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3680 int max_buf_size; /* Max size of packet output buffer */
3684 /* Create a buffer big enough for this packet. */
3685 max_buf_size = get_memory_read_packet_size ();
3686 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3687 buf = alloca (sizeof_buf);
3696 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3698 /* construct "m"<memaddr>","<len>" */
3699 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3700 memaddr = remote_address_masked (memaddr);
3703 p += hexnumstr (p, (ULONGEST) memaddr);
3705 p += hexnumstr (p, (ULONGEST) todo);
3709 getpkt (buf, sizeof_buf, 0);
3713 /* There is no correspondance between what the remote protocol uses
3714 for errors and errno codes. We would like a cleaner way of
3715 representing errors (big enough to include errno codes, bfd_error
3716 codes, and others). But for now just return EIO. */
3721 /* Reply describes memory byte by byte,
3722 each byte encoded as two hex characters. */
3725 if ((i = hex2bin (p, myaddr, todo)) < todo)
3727 /* Reply is short. This means that we were able to read
3728 only part of what we wanted to. */
3729 return i + (origlen - len);
3738 /* Read or write LEN bytes from inferior memory at MEMADDR,
3739 transferring to or from debugger address BUFFER. Write to inferior if
3740 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3741 for error. TARGET is unused. */
3745 remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
3747 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
3748 struct target_ops *target)
3750 CORE_ADDR targ_addr;
3754 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3759 res = remote_write_bytes (targ_addr, buffer, targ_len);
3761 res = remote_read_bytes (targ_addr, buffer, targ_len);
3768 /* Enable after 4.12. */
3771 remote_search (int len, char *data, char *mask, CORE_ADDR startaddr,
3772 int increment, CORE_ADDR lorange, CORE_ADDR hirange,
3773 CORE_ADDR *addr_found, char *data_found)
3775 if (increment == -4 && len == 4)
3777 long mask_long, data_long;
3778 long data_found_long;
3779 CORE_ADDR addr_we_found;
3780 char *buf = alloca (PBUFSIZ);
3781 long returned_long[2];
3784 mask_long = extract_unsigned_integer (mask, len);
3785 data_long = extract_unsigned_integer (data, len);
3786 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3788 getpkt (buf, PBUFSIZ, 0);
3791 /* The stub doesn't support the 't' request. We might want to
3792 remember this fact, but on the other hand the stub could be
3793 switched on us. Maybe we should remember it only until
3794 the next "target remote". */
3795 generic_search (len, data, mask, startaddr, increment, lorange,
3796 hirange, addr_found, data_found);
3801 /* There is no correspondance between what the remote protocol uses
3802 for errors and errno codes. We would like a cleaner way of
3803 representing errors (big enough to include errno codes, bfd_error
3804 codes, and others). But for now just use EIO. */
3805 memory_error (EIO, startaddr);
3808 while (*p != '\0' && *p != ',')
3809 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3811 error ("Protocol error: short return for search");
3813 data_found_long = 0;
3814 while (*p != '\0' && *p != ',')
3815 data_found_long = (data_found_long << 4) + fromhex (*p++);
3816 /* Ignore anything after this comma, for future extensions. */
3818 if (addr_we_found < lorange || addr_we_found >= hirange)
3824 *addr_found = addr_we_found;
3825 *data_found = store_unsigned_integer (data_we_found, len);
3828 generic_search (len, data, mask, startaddr, increment, lorange,
3829 hirange, addr_found, data_found);
3834 remote_files_info (struct target_ops *ignore)
3836 puts_filtered ("Debugging a target over a serial line.\n");
3839 /* Stuff for dealing with the packets which are part of this protocol.
3840 See comment at top of file for details. */
3842 /* Read a single character from the remote end, masking it down to 7 bits. */
3845 readchar (int timeout)
3849 ch = SERIAL_READCHAR (remote_desc, timeout);
3854 switch ((enum serial_rc) ch)
3857 target_mourn_inferior ();
3858 error ("Remote connection closed");
3861 perror_with_name ("Remote communication error");
3863 case SERIAL_TIMEOUT:
3869 /* Send the command in BUF to the remote machine, and read the reply
3870 into BUF. Report an error if we get an error reply. */
3873 remote_send (char *buf,
3877 getpkt (buf, sizeof_buf, 0);
3880 error ("Remote failure reply: %s", buf);
3883 /* Display a null-terminated packet on stdout, for debugging, using C
3887 print_packet (char *buf)
3889 puts_filtered ("\"");
3890 fputstr_filtered (buf, '"', gdb_stdout);
3891 puts_filtered ("\"");
3897 return putpkt_binary (buf, strlen (buf));
3900 /* Send a packet to the remote machine, with error checking. The data
3901 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3902 to account for the $, # and checksum, and for a possible /0 if we are
3903 debugging (remote_debug) and want to print the sent packet as a string */
3906 putpkt_binary (char *buf, int cnt)
3909 unsigned char csum = 0;
3910 char *buf2 = alloca (cnt + 6);
3911 long sizeof_junkbuf = PBUFSIZ;
3912 char *junkbuf = alloca (sizeof_junkbuf);
3918 /* Copy the packet into buffer BUF2, encapsulating it
3919 and giving it a checksum. */
3924 for (i = 0; i < cnt; i++)
3930 *p++ = tohex ((csum >> 4) & 0xf);
3931 *p++ = tohex (csum & 0xf);
3933 /* Send it over and over until we get a positive ack. */
3937 int started_error_output = 0;
3942 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3943 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
3944 fprintf_unfiltered (gdb_stdlog, "...");
3945 gdb_flush (gdb_stdlog);
3947 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
3948 perror_with_name ("putpkt: write failed");
3950 /* read until either a timeout occurs (-2) or '+' is read */
3953 ch = readchar (remote_timeout);
3961 case SERIAL_TIMEOUT:
3963 if (started_error_output)
3965 putchar_unfiltered ('\n');
3966 started_error_output = 0;
3975 fprintf_unfiltered (gdb_stdlog, "Ack\n");
3979 fprintf_unfiltered (gdb_stdlog, "Nak\n");
3980 case SERIAL_TIMEOUT:
3984 break; /* Retransmit buffer */
3988 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
3989 /* It's probably an old response, and we're out of sync.
3990 Just gobble up the packet and ignore it. */
3991 read_frame (junkbuf, sizeof_junkbuf);
3992 continue; /* Now, go look for + */
3997 if (!started_error_output)
3999 started_error_output = 1;
4000 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
4002 fputc_unfiltered (ch & 0177, gdb_stdlog);
4006 break; /* Here to retransmit */
4010 /* This is wrong. If doing a long backtrace, the user should be
4011 able to get out next time we call QUIT, without anything as
4012 violent as interrupt_query. If we want to provide a way out of
4013 here without getting to the next QUIT, it should be based on
4014 hitting ^C twice as in remote_wait. */
4024 static int remote_cisco_mode;
4026 /* Come here after finding the start of the frame. Collect the rest
4027 into BUF, verifying the checksum, length, and handling run-length
4028 compression. No more than sizeof_buf-1 characters are read so that
4029 the buffer can be NUL terminated.
4031 Returns -1 on error, number of characters in buffer (ignoring the
4032 trailing NULL) on success. (could be extended to return one of the
4033 SERIAL status indications). */
4036 read_frame (char *buf,
4048 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
4049 c = readchar (remote_timeout);
4052 case SERIAL_TIMEOUT:
4054 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
4058 fputs_filtered ("Saw new packet start in middle of old one\n",
4060 return -1; /* Start a new packet, count retries */
4063 unsigned char pktcsum;
4069 check_0 = readchar (remote_timeout);
4071 check_1 = readchar (remote_timeout);
4073 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4076 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4079 else if (check_0 < 0 || check_1 < 0)
4082 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4086 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
4087 if (csum == pktcsum)
4092 fprintf_filtered (gdb_stdlog,
4093 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4095 fputs_filtered (buf, gdb_stdlog);
4096 fputs_filtered ("\n", gdb_stdlog);
4098 /* Number of characters in buffer ignoring trailing
4102 case '*': /* Run length encoding */
4107 if (remote_cisco_mode == 0)
4109 c = readchar (remote_timeout);
4111 repeat = c - ' ' + 3; /* Compute repeat count */
4115 /* Cisco's run-length encoding variant uses two
4116 hex chars to represent the repeat count. */
4118 c = readchar (remote_timeout);
4120 repeat = fromhex (c) << 4;
4121 c = readchar (remote_timeout);
4123 repeat += fromhex (c);
4126 /* The character before ``*'' is repeated. */
4128 if (repeat > 0 && repeat <= 255
4130 && bc + repeat < sizeof_buf - 1)
4132 memset (&buf[bc], buf[bc - 1], repeat);
4138 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4139 puts_filtered (buf);
4140 puts_filtered ("\n");
4144 if (bc < sizeof_buf - 1)
4152 puts_filtered ("Remote packet too long: ");
4153 puts_filtered (buf);
4154 puts_filtered ("\n");
4161 /* Read a packet from the remote machine, with error checking, and
4162 store it in BUF. If FOREVER, wait forever rather than timing out;
4163 this is used (in synchronous mode) to wait for a target that is is
4164 executing user code to stop. */
4165 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4166 don't have to change all the calls to getpkt to deal with the
4167 return value, because at the moment I don't know what the right
4168 thing to do it for those. */
4176 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4180 /* Read a packet from the remote machine, with error checking, and
4181 store it in BUF. If FOREVER, wait forever rather than timing out;
4182 this is used (in synchronous mode) to wait for a target that is is
4183 executing user code to stop. If FOREVER == 0, this function is
4184 allowed to time out gracefully and return an indication of this to
4187 getpkt_sane (char *buf,
4196 strcpy (buf, "timeout");
4200 timeout = watchdog > 0 ? watchdog : -1;
4204 timeout = remote_timeout;
4208 for (tries = 1; tries <= MAX_TRIES; tries++)
4210 /* This can loop forever if the remote side sends us characters
4211 continuously, but if it pauses, we'll get a zero from readchar
4212 because of timeout. Then we'll count that as a retry. */
4214 /* Note that we will only wait forever prior to the start of a packet.
4215 After that, we expect characters to arrive at a brisk pace. They
4216 should show up within remote_timeout intervals. */
4220 c = readchar (timeout);
4222 if (c == SERIAL_TIMEOUT)
4224 if (forever) /* Watchdog went off? Kill the target. */
4227 target_mourn_inferior ();
4228 error ("Watchdog has expired. Target detached.\n");
4231 fputs_filtered ("Timed out.\n", gdb_stdlog);
4237 /* We've found the start of a packet, now collect the data. */
4239 val = read_frame (buf, sizeof_buf);
4245 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4246 fputstr_unfiltered (buf, 0, gdb_stdlog);
4247 fprintf_unfiltered (gdb_stdlog, "\n");
4249 SERIAL_WRITE (remote_desc, "+", 1);
4253 /* Try the whole thing again. */
4255 SERIAL_WRITE (remote_desc, "-", 1);
4258 /* We have tried hard enough, and just can't receive the packet. Give up. */
4260 printf_unfiltered ("Ignoring packet error, continuing...\n");
4261 SERIAL_WRITE (remote_desc, "+", 1);
4268 /* For some mysterious reason, wait_for_inferior calls kill instead of
4269 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4273 target_mourn_inferior ();
4277 /* Use catch_errors so the user can quit from gdb even when we aren't on
4278 speaking terms with the remote system. */
4279 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4281 /* Don't wait for it to die. I'm not really sure it matters whether
4282 we do or not. For the existing stubs, kill is a noop. */
4283 target_mourn_inferior ();
4286 /* Async version of remote_kill. */
4288 remote_async_kill (void)
4290 /* Unregister the file descriptor from the event loop. */
4291 if (target_is_async_p ())
4292 SERIAL_ASYNC (remote_desc, NULL, 0);
4294 /* For some mysterious reason, wait_for_inferior calls kill instead of
4295 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4299 target_mourn_inferior ();
4303 /* Use catch_errors so the user can quit from gdb even when we aren't on
4304 speaking terms with the remote system. */
4305 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4307 /* Don't wait for it to die. I'm not really sure it matters whether
4308 we do or not. For the existing stubs, kill is a noop. */
4309 target_mourn_inferior ();
4315 remote_mourn_1 (&remote_ops);
4319 remote_async_mourn (void)
4321 remote_mourn_1 (&remote_async_ops);
4325 extended_remote_mourn (void)
4327 /* We do _not_ want to mourn the target like this; this will
4328 remove the extended remote target from the target stack,
4329 and the next time the user says "run" it'll fail.
4331 FIXME: What is the right thing to do here? */
4333 remote_mourn_1 (&extended_remote_ops);
4337 /* Worker function for remote_mourn. */
4339 remote_mourn_1 (struct target_ops *target)
4341 unpush_target (target);
4342 generic_mourn_inferior ();
4345 /* In the extended protocol we want to be able to do things like
4346 "run" and have them basically work as expected. So we need
4347 a special create_inferior function.
4349 FIXME: One day add support for changing the exec file
4350 we're debugging, arguments and an environment. */
4353 extended_remote_create_inferior (char *exec_file, char *args, char **env)
4355 /* Rip out the breakpoints; we'll reinsert them after restarting
4356 the remote server. */
4357 remove_breakpoints ();
4359 /* Now restart the remote server. */
4360 extended_remote_restart ();
4362 /* Now put the breakpoints back in. This way we're safe if the
4363 restart function works via a unix fork on the remote side. */
4364 insert_breakpoints ();
4366 /* Clean up from the last time we were running. */
4367 clear_proceed_status ();
4369 /* Let the remote process run. */
4370 proceed (-1, TARGET_SIGNAL_0, 0);
4373 /* Async version of extended_remote_create_inferior. */
4375 extended_remote_async_create_inferior (char *exec_file, char *args, char **env)
4377 /* Rip out the breakpoints; we'll reinsert them after restarting
4378 the remote server. */
4379 remove_breakpoints ();
4381 /* If running asynchronously, register the target file descriptor
4382 with the event loop. */
4383 if (event_loop_p && target_can_async_p ())
4384 target_async (inferior_event_handler, 0);
4386 /* Now restart the remote server. */
4387 extended_remote_restart ();
4389 /* Now put the breakpoints back in. This way we're safe if the
4390 restart function works via a unix fork on the remote side. */
4391 insert_breakpoints ();
4393 /* Clean up from the last time we were running. */
4394 clear_proceed_status ();
4396 /* Let the remote process run. */
4397 proceed (-1, TARGET_SIGNAL_0, 0);
4401 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
4402 than other targets; in those use REMOTE_BREAKPOINT instead of just
4403 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4404 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4405 the standard routines that are in mem-break.c. */
4407 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
4408 the choice of breakpoint instruction affects target program design and
4409 vice versa, and by making it user-tweakable, the special code here
4410 goes away and we need fewer special GDB configurations. */
4412 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4413 #define REMOTE_BREAKPOINT
4416 #ifdef REMOTE_BREAKPOINT
4418 /* If the target isn't bi-endian, just pretend it is. */
4419 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4420 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4421 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4424 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4425 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4427 #endif /* REMOTE_BREAKPOINT */
4429 /* Insert a breakpoint on targets that don't have any better breakpoint
4430 support. We read the contents of the target location and stash it,
4431 then overwrite it with a breakpoint instruction. ADDR is the target
4432 location in the target machine. CONTENTS_CACHE is a pointer to
4433 memory allocated for saving the target contents. It is guaranteed
4434 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4435 is accomplished via BREAKPOINT_MAX). */
4438 remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
4440 #ifdef REMOTE_BREAKPOINT
4445 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4446 If it succeeds, then set the support to PACKET_ENABLE. If it
4447 fails, and the user has explicitly requested the Z support then
4448 report an error, otherwise, mark it disabled and go on. */
4450 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4452 char *buf = alloca (PBUFSIZ);
4455 addr = remote_address_masked (addr);
4459 p += hexnumstr (p, (ULONGEST) addr);
4460 BREAKPOINT_FROM_PC (&addr, &bp_size);
4461 sprintf (p, ",%d", bp_size);
4464 getpkt (buf, PBUFSIZ, 0);
4466 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
4472 case PACKET_UNKNOWN:
4477 #ifdef REMOTE_BREAKPOINT
4478 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4482 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4483 val = target_write_memory (addr, (char *) big_break_insn,
4484 sizeof big_break_insn);
4486 val = target_write_memory (addr, (char *) little_break_insn,
4487 sizeof little_break_insn);
4492 return memory_insert_breakpoint (addr, contents_cache);
4493 #endif /* REMOTE_BREAKPOINT */
4497 remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
4501 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4503 char *buf = alloca (PBUFSIZ);
4510 addr = remote_address_masked (addr);
4511 p += hexnumstr (p, (ULONGEST) addr);
4512 BREAKPOINT_FROM_PC (&addr, &bp_size);
4513 sprintf (p, ",%d", bp_size);
4516 getpkt (buf, PBUFSIZ, 0);
4518 return (buf[0] == 'E');
4521 #ifdef REMOTE_BREAKPOINT
4522 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4524 return memory_remove_breakpoint (addr, contents_cache);
4525 #endif /* REMOTE_BREAKPOINT */
4529 watchpoint_to_Z_packet (int type)
4543 internal_error (__FILE__, __LINE__,
4544 "hw_bp_to_z: bad watchpoint type %d", type);
4548 /* FIXME: This function should be static and a member of the remote
4552 remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
4554 char *buf = alloca (PBUFSIZ);
4556 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4558 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4559 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4560 remote_protocol_Z[packet].name,
4561 remote_protocol_Z[packet].title);
4563 sprintf (buf, "Z%x,", packet);
4564 p = strchr (buf, '\0');
4565 addr = remote_address_masked (addr);
4566 p += hexnumstr (p, (ULONGEST) addr);
4567 sprintf (p, ",%x", len);
4570 getpkt (buf, PBUFSIZ, 0);
4572 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4575 case PACKET_UNKNOWN:
4580 internal_error (__FILE__, __LINE__,
4581 "remote_insert_watchpoint: reached end of function");
4584 /* FIXME: This function should be static and a member of the remote
4588 remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
4590 char *buf = alloca (PBUFSIZ);
4592 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4594 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4595 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4596 remote_protocol_Z[packet].name,
4597 remote_protocol_Z[packet].title);
4599 sprintf (buf, "z%x,", packet);
4600 p = strchr (buf, '\0');
4601 addr = remote_address_masked (addr);
4602 p += hexnumstr (p, (ULONGEST) addr);
4603 sprintf (p, ",%x", len);
4605 getpkt (buf, PBUFSIZ, 0);
4607 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4610 case PACKET_UNKNOWN:
4615 internal_error (__FILE__, __LINE__,
4616 "remote_remove_watchpoint: reached end of function");
4619 /* FIXME: This function should be static and a member of the remote
4623 remote_insert_hw_breakpoint (CORE_ADDR addr, int len)
4625 char *buf = alloca (PBUFSIZ);
4628 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4629 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4630 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4631 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4637 addr = remote_address_masked (addr);
4638 p += hexnumstr (p, (ULONGEST) addr);
4639 sprintf (p, ",%x", len);
4642 getpkt (buf, PBUFSIZ, 0);
4644 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4647 case PACKET_UNKNOWN:
4652 internal_error (__FILE__, __LINE__,
4653 "remote_remove_watchpoint: reached end of function");
4656 /* FIXME: This function should be static and a member of the remote
4660 remote_remove_hw_breakpoint (CORE_ADDR addr, int len)
4662 char *buf = alloca (PBUFSIZ);
4665 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4666 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4667 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4668 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4674 addr = remote_address_masked (addr);
4675 p += hexnumstr (p, (ULONGEST) addr);
4676 sprintf (p, ",%x", len);
4679 getpkt (buf, PBUFSIZ, 0);
4681 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4684 case PACKET_UNKNOWN:
4689 internal_error (__FILE__, __LINE__,
4690 "remote_remove_watchpoint: reached end of function");
4693 /* Some targets are only capable of doing downloads, and afterwards
4694 they switch to the remote serial protocol. This function provides
4695 a clean way to get from the download target to the remote target.
4696 It's basically just a wrapper so that we don't have to expose any
4697 of the internal workings of remote.c.
4699 Prior to calling this routine, you should shutdown the current
4700 target code, else you will get the "A program is being debugged
4701 already..." message. Usually a call to pop_target() suffices. */
4704 push_remote_target (char *name, int from_tty)
4706 printf_filtered ("Switching to remote protocol\n");
4707 remote_open (name, from_tty);
4710 /* Other targets want to use the entire remote serial module but with
4711 certain remote_ops overridden. */
4714 open_remote_target (char *name, int from_tty, struct target_ops *target,
4717 printf_filtered ("Selecting the %sremote protocol\n",
4718 (extended_p ? "extended-" : ""));
4719 remote_open_1 (name, from_tty, target, extended_p);
4722 /* Table used by the crc32 function to calcuate the checksum. */
4724 static unsigned long crc32_table[256] =
4727 static unsigned long
4728 crc32 (unsigned char *buf, int len, unsigned int crc)
4730 if (!crc32_table[1])
4732 /* Initialize the CRC table and the decoding table. */
4736 for (i = 0; i < 256; i++)
4738 for (c = i << 24, j = 8; j > 0; --j)
4739 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4746 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4752 /* compare-sections command
4754 With no arguments, compares each loadable section in the exec bfd
4755 with the same memory range on the target, and reports mismatches.
4756 Useful for verifying the image on the target against the exec file.
4757 Depends on the target understanding the new "qCRC:" request. */
4759 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4760 target method (target verify memory) and generic version of the
4761 actual command. This will allow other high-level code (especially
4762 generic_load()) to make use of this target functionality. */
4765 compare_sections_command (char *args, int from_tty)
4768 unsigned long host_crc, target_crc;
4769 extern bfd *exec_bfd;
4770 struct cleanup *old_chain;
4773 const char *sectname;
4774 char *buf = alloca (PBUFSIZ);
4781 error ("command cannot be used without an exec file");
4782 if (!current_target.to_shortname ||
4783 strcmp (current_target.to_shortname, "remote") != 0)
4784 error ("command can only be used with remote target");
4786 for (s = exec_bfd->sections; s; s = s->next)
4788 if (!(s->flags & SEC_LOAD))
4789 continue; /* skip non-loadable section */
4791 size = bfd_get_section_size_before_reloc (s);
4793 continue; /* skip zero-length section */
4795 sectname = bfd_get_section_name (exec_bfd, s);
4796 if (args && strcmp (args, sectname) != 0)
4797 continue; /* not the section selected by user */
4799 matched = 1; /* do this section */
4801 /* FIXME: assumes lma can fit into long */
4802 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4805 /* be clever; compute the host_crc before waiting for target reply */
4806 sectdata = xmalloc (size);
4807 old_chain = make_cleanup (xfree, sectdata);
4808 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4809 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4811 getpkt (buf, PBUFSIZ, 0);
4813 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4814 sectname, lma, lma + size);
4816 error ("remote target does not support this operation");
4818 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4819 target_crc = target_crc * 16 + fromhex (*tmp);
4821 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4822 sectname, paddr (lma), paddr (lma + size));
4823 if (host_crc == target_crc)
4824 printf_filtered ("matched.\n");
4827 printf_filtered ("MIS-MATCHED!\n");
4831 do_cleanups (old_chain);
4834 warning ("One or more sections of the remote executable does not match\n\
4835 the loaded file\n");
4836 if (args && !matched)
4837 printf_filtered ("No loaded section named '%s'.\n", args);
4841 remote_query (int query_type, char *buf, char *outbuf, int *bufsiz)
4844 char *buf2 = alloca (PBUFSIZ);
4845 char *p2 = &buf2[0];
4848 error ("null pointer to remote bufer size specified");
4850 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4851 the caller know and return what the minimum size is */
4852 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4853 if (*bufsiz < PBUFSIZ)
4859 /* except for querying the minimum buffer size, target must be open */
4861 error ("remote query is only available after target open");
4863 /* we only take uppercase letters as query types, at least for now */
4864 if ((query_type < 'A') || (query_type > 'Z'))
4865 error ("invalid remote query type");
4868 error ("null remote query specified");
4871 error ("remote query requires a buffer to receive data");
4878 /* we used one buffer char for the remote protocol q command and another
4879 for the query type. As the remote protocol encapsulation uses 4 chars
4880 plus one extra in case we are debugging (remote_debug),
4881 we have PBUFZIZ - 7 left to pack the query string */
4883 while (buf[i] && (i < (PBUFSIZ - 8)))
4885 /* bad caller may have sent forbidden characters */
4886 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
4887 error ("illegal characters in query string");
4895 error ("query larger than available buffer");
4901 getpkt (outbuf, *bufsiz, 0);
4907 remote_rcmd (char *command,
4908 struct ui_file *outbuf)
4911 char *buf = alloca (PBUFSIZ);
4915 error ("remote rcmd is only available after target open");
4917 /* Send a NULL command across as an empty command */
4918 if (command == NULL)
4921 /* The query prefix */
4922 strcpy (buf, "qRcmd,");
4923 p = strchr (buf, '\0');
4925 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ)
4926 error ("\"monitor\" command ``%s'' is too long\n", command);
4928 /* Encode the actual command */
4929 bin2hex (command, p, 0);
4931 if (putpkt (buf) < 0)
4932 error ("Communication problem with target\n");
4934 /* get/display the response */
4937 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4939 getpkt (buf, PBUFSIZ, 0);
4941 error ("Target does not support this command\n");
4942 if (buf[0] == 'O' && buf[1] != 'K')
4944 remote_console_output (buf + 1); /* 'O' message from stub */
4947 if (strcmp (buf, "OK") == 0)
4949 if (strlen (buf) == 3 && buf[0] == 'E'
4950 && isdigit (buf[1]) && isdigit (buf[2]))
4952 error ("Protocol error with Rcmd");
4954 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
4956 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
4957 fputc_unfiltered (c, outbuf);
4964 packet_command (char *args, int from_tty)
4966 char *buf = alloca (PBUFSIZ);
4969 error ("command can only be used with remote target");
4972 error ("remote-packet command requires packet text as argument");
4974 puts_filtered ("sending: ");
4975 print_packet (args);
4976 puts_filtered ("\n");
4979 getpkt (buf, PBUFSIZ, 0);
4980 puts_filtered ("received: ");
4982 puts_filtered ("\n");
4986 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4988 static void display_thread_info (struct gdb_ext_thread_info *info);
4990 static void threadset_test_cmd (char *cmd, int tty);
4992 static void threadalive_test (char *cmd, int tty);
4994 static void threadlist_test_cmd (char *cmd, int tty);
4996 int get_and_display_threadinfo (threadref * ref);
4998 static void threadinfo_test_cmd (char *cmd, int tty);
5000 static int thread_display_step (threadref * ref, void *context);
5002 static void threadlist_update_test_cmd (char *cmd, int tty);
5004 static void init_remote_threadtests (void);
5006 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
5009 threadset_test_cmd (char *cmd, int tty)
5011 int sample_thread = SAMPLE_THREAD;
5013 printf_filtered ("Remote threadset test\n");
5014 set_thread (sample_thread, 1);
5019 threadalive_test (char *cmd, int tty)
5021 int sample_thread = SAMPLE_THREAD;
5023 if (remote_thread_alive (pid_to_ptid (sample_thread)))
5024 printf_filtered ("PASS: Thread alive test\n");
5026 printf_filtered ("FAIL: Thread alive test\n");
5029 void output_threadid (char *title, threadref * ref);
5032 output_threadid (char *title, threadref *ref)
5036 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5038 printf_filtered ("%s %s\n", title, (&hexid[0]));
5042 threadlist_test_cmd (char *cmd, int tty)
5045 threadref nextthread;
5046 int done, result_count;
5047 threadref threadlist[3];
5049 printf_filtered ("Remote Threadlist test\n");
5050 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5051 &result_count, &threadlist[0]))
5052 printf_filtered ("FAIL: threadlist test\n");
5055 threadref *scan = threadlist;
5056 threadref *limit = scan + result_count;
5058 while (scan < limit)
5059 output_threadid (" thread ", scan++);
5064 display_thread_info (struct gdb_ext_thread_info *info)
5066 output_threadid ("Threadid: ", &info->threadid);
5067 printf_filtered ("Name: %s\n ", info->shortname);
5068 printf_filtered ("State: %s\n", info->display);
5069 printf_filtered ("other: %s\n\n", info->more_display);
5073 get_and_display_threadinfo (threadref *ref)
5077 struct gdb_ext_thread_info threadinfo;
5079 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5080 | TAG_MOREDISPLAY | TAG_DISPLAY;
5081 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5082 display_thread_info (&threadinfo);
5087 threadinfo_test_cmd (char *cmd, int tty)
5089 int athread = SAMPLE_THREAD;
5093 int_to_threadref (&thread, athread);
5094 printf_filtered ("Remote Threadinfo test\n");
5095 if (!get_and_display_threadinfo (&thread))
5096 printf_filtered ("FAIL cannot get thread info\n");
5100 thread_display_step (threadref *ref, void *context)
5102 /* output_threadid(" threadstep ",ref); *//* simple test */
5103 return get_and_display_threadinfo (ref);
5107 threadlist_update_test_cmd (char *cmd, int tty)
5109 printf_filtered ("Remote Threadlist update test\n");
5110 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5114 init_remote_threadtests (void)
5116 add_com ("tlist", class_obscure, threadlist_test_cmd,
5117 "Fetch and print the remote list of thread identifiers, one pkt only");
5118 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5119 "Fetch and display info about one thread");
5120 add_com ("tset", class_obscure, threadset_test_cmd,
5121 "Test setting to a different thread");
5122 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5123 "Iterate through updating all remote thread info");
5124 add_com ("talive", class_obscure, threadalive_test,
5125 " Remote thread alive test ");
5130 /* Convert a thread ID to a string. Returns the string in a static
5134 remote_pid_to_str (ptid_t ptid)
5136 static char buf[30];
5138 sprintf (buf, "Thread %d", PIDGET (ptid));
5143 init_remote_ops (void)
5145 remote_ops.to_shortname = "remote";
5146 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
5148 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5149 Specify the serial device it is connected to\n\
5150 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5151 remote_ops.to_open = remote_open;
5152 remote_ops.to_close = remote_close;
5153 remote_ops.to_detach = remote_detach;
5154 remote_ops.to_resume = remote_resume;
5155 remote_ops.to_wait = remote_wait;
5156 remote_ops.to_fetch_registers = remote_fetch_registers;
5157 remote_ops.to_store_registers = remote_store_registers;
5158 remote_ops.to_prepare_to_store = remote_prepare_to_store;
5159 remote_ops.to_xfer_memory = remote_xfer_memory;
5160 remote_ops.to_files_info = remote_files_info;
5161 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5162 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5163 remote_ops.to_kill = remote_kill;
5164 remote_ops.to_load = generic_load;
5165 remote_ops.to_mourn_inferior = remote_mourn;
5166 remote_ops.to_thread_alive = remote_thread_alive;
5167 remote_ops.to_find_new_threads = remote_threads_info;
5168 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5169 remote_ops.to_pid_to_str = remote_pid_to_str;
5170 remote_ops.to_stop = remote_stop;
5171 remote_ops.to_query = remote_query;
5172 remote_ops.to_rcmd = remote_rcmd;
5173 remote_ops.to_stratum = process_stratum;
5174 remote_ops.to_has_all_memory = 1;
5175 remote_ops.to_has_memory = 1;
5176 remote_ops.to_has_stack = 1;
5177 remote_ops.to_has_registers = 1;
5178 remote_ops.to_has_execution = 1;
5179 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5180 remote_ops.to_magic = OPS_MAGIC;
5183 /* Set up the extended remote vector by making a copy of the standard
5184 remote vector and adding to it. */
5187 init_extended_remote_ops (void)
5189 extended_remote_ops = remote_ops;
5191 extended_remote_ops.to_shortname = "extended-remote";
5192 extended_remote_ops.to_longname =
5193 "Extended remote serial target in gdb-specific protocol";
5194 extended_remote_ops.to_doc =
5195 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5196 Specify the serial device it is connected to (e.g. /dev/ttya).",
5197 extended_remote_ops.to_open = extended_remote_open;
5198 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5199 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5203 * Command: info remote-process
5205 * This implements Cisco's version of the "info proc" command.
5207 * This query allows the target stub to return an arbitrary string
5208 * (or strings) giving arbitrary information about the target process.
5209 * This is optional; the target stub isn't required to implement it.
5211 * Syntax: qfProcessInfo request first string
5212 * qsProcessInfo request subsequent string
5213 * reply: 'O'<hex-encoded-string>
5214 * 'l' last reply (empty)
5218 remote_info_process (char *args, int from_tty)
5220 char *buf = alloca (PBUFSIZ);
5222 if (remote_desc == 0)
5223 error ("Command can only be used when connected to the remote target.");
5225 putpkt ("qfProcessInfo");
5226 getpkt (buf, PBUFSIZ, 0);
5228 return; /* Silently: target does not support this feature. */
5231 error ("info proc: target error.");
5233 while (buf[0] == 'O') /* Capitol-O packet */
5235 remote_console_output (&buf[1]);
5236 putpkt ("qsProcessInfo");
5237 getpkt (buf, PBUFSIZ, 0);
5246 remote_cisco_open (char *name, int from_tty)
5250 "To open a remote debug connection, you need to specify what \n\
5251 device is attached to the remote system (e.g. host:port).");
5253 /* See FIXME above */
5254 wait_forever_enabled_p = 1;
5256 target_preopen (from_tty);
5258 unpush_target (&remote_cisco_ops);
5260 remote_desc = SERIAL_OPEN (name);
5262 perror_with_name (name);
5265 * If a baud rate was specified on the gdb command line it will
5266 * be greater than the initial value of -1. If it is, use it otherwise
5270 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
5271 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
5273 SERIAL_CLOSE (remote_desc);
5274 perror_with_name (name);
5277 SERIAL_RAW (remote_desc);
5279 /* If there is something sitting in the buffer we might take it as a
5280 response to a command, which would be bad. */
5281 SERIAL_FLUSH_INPUT (remote_desc);
5285 puts_filtered ("Remote debugging using ");
5286 puts_filtered (name);
5287 puts_filtered ("\n");
5290 remote_cisco_mode = 1;
5292 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5294 init_all_packet_configs ();
5296 general_thread = -2;
5297 continue_thread = -2;
5299 /* Probe for ability to use "ThreadInfo" query, as required. */
5300 use_threadinfo_query = 1;
5301 use_threadextra_query = 1;
5303 /* Without this, some commands which require an active target (such
5304 as kill) won't work. This variable serves (at least) double duty
5305 as both the pid of the target process (if it has such), and as a
5306 flag indicating that a target is active. These functions should
5307 be split out into seperate variables, especially since GDB will
5308 someday have a notion of debugging several processes. */
5309 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
5311 /* Start the remote connection; if error (0), discard this target. */
5313 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5314 "Couldn't establish connection to remote target\n",
5323 remote_cisco_close (int quitting)
5325 remote_cisco_mode = 0;
5326 remote_close (quitting);
5330 remote_cisco_mourn (void)
5332 remote_mourn_1 (&remote_cisco_ops);
5344 /* shared between readsocket() and readtty() */
5345 static char *tty_input;
5347 static int escape_count;
5348 static int echo_check;
5349 extern int quit_flag;
5356 /* Loop until the socket doesn't have any more data */
5358 while ((data = readchar (0)) >= 0)
5360 /* Check for the escape sequence */
5363 /* If this is the fourth escape, get out */
5364 if (++escape_count == 4)
5369 { /* This is a '|', but not the fourth in a row.
5370 Continue without echoing it. If it isn't actually
5371 one of four in a row, it'll be echoed later. */
5378 /* Ensure any pending '|'s are flushed. */
5380 for (; escape_count > 0; escape_count--)
5384 if (data == '\r') /* If this is a return character, */
5385 continue; /* - just supress it. */
5387 if (echo_check != -1) /* Check for echo of user input. */
5389 if (tty_input[echo_check] == data)
5391 echo_check++; /* Character matched user input: */
5392 continue; /* Continue without echoing it. */
5394 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5395 { /* End of the line (and of echo checking). */
5396 echo_check = -1; /* No more echo supression */
5397 continue; /* Continue without echoing. */
5400 { /* Failed check for echo of user input.
5401 We now have some suppressed output to flush! */
5404 for (j = 0; j < echo_check; j++)
5405 putchar (tty_input[j]);
5409 putchar (data); /* Default case: output the char. */
5412 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5413 return READ_MORE; /* Try to read some more */
5415 return FATAL_ERROR; /* Trouble, bail out */
5423 /* First, read a buffer full from the terminal */
5424 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
5425 if (tty_bytecount == -1)
5427 perror ("readtty: read failed");
5431 /* Remove a quoted newline. */
5432 if (tty_input[tty_bytecount - 1] == '\n' &&
5433 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5435 tty_input[--tty_bytecount] = 0; /* remove newline */
5436 tty_input[--tty_bytecount] = 0; /* remove backslash */
5439 /* Turn trailing newlines into returns */
5440 if (tty_input[tty_bytecount - 1] == '\n')
5441 tty_input[tty_bytecount - 1] = '\r';
5443 /* If the line consists of a ~, enter debugging mode. */
5444 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5447 /* Make this a zero terminated string and write it out */
5448 tty_input[tty_bytecount] = 0;
5449 if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount))
5451 perror_with_name ("readtty: write failed");
5461 fd_set input; /* file descriptors for select */
5462 int tablesize; /* max number of FDs for select */
5466 extern int escape_count; /* global shared by readsocket */
5467 extern int echo_check; /* ditto */
5472 tablesize = 8 * sizeof (input);
5476 /* Check for anything from our socket - doesn't block. Note that
5477 this must be done *before* the select as there may be
5478 buffered I/O waiting to be processed. */
5480 if ((status = readsocket ()) == FATAL_ERROR)
5482 error ("Debugging terminated by communications error");
5484 else if (status != READ_MORE)
5489 fflush (stdout); /* Flush output before blocking */
5491 /* Now block on more socket input or TTY input */
5494 FD_SET (fileno (stdin), &input);
5495 FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
5497 status = select (tablesize, &input, 0, 0, 0);
5498 if ((status == -1) && (errno != EINTR))
5500 error ("Communications error on select %d", errno);
5503 /* Handle Control-C typed */
5507 if ((++quit_count) == 2)
5509 if (query ("Interrupt GDB? "))
5511 printf_filtered ("Interrupted by user.\n");
5512 return_to_top_level (RETURN_QUIT);
5519 SERIAL_SEND_BREAK (remote_desc);
5521 SERIAL_WRITE (remote_desc, "\003", 1);
5526 /* Handle console input */
5528 if (FD_ISSET (fileno (stdin), &input))
5532 status = readtty ();
5533 if (status == READ_MORE)
5536 return status; /* telnet session ended */
5542 remote_cisco_wait (ptid_t ptid, struct target_waitstatus *status)
5544 if (minitelnet () != ENTER_DEBUG)
5546 error ("Debugging session terminated by protocol error");
5549 return remote_wait (ptid, status);
5553 init_remote_cisco_ops (void)
5555 remote_cisco_ops.to_shortname = "cisco";
5556 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5557 remote_cisco_ops.to_doc =
5558 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5559 Specify the serial device it is connected to (e.g. host:2020).";
5560 remote_cisco_ops.to_open = remote_cisco_open;
5561 remote_cisco_ops.to_close = remote_cisco_close;
5562 remote_cisco_ops.to_detach = remote_detach;
5563 remote_cisco_ops.to_resume = remote_resume;
5564 remote_cisco_ops.to_wait = remote_cisco_wait;
5565 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5566 remote_cisco_ops.to_store_registers = remote_store_registers;
5567 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5568 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5569 remote_cisco_ops.to_files_info = remote_files_info;
5570 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5571 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
5572 remote_cisco_ops.to_kill = remote_kill;
5573 remote_cisco_ops.to_load = generic_load;
5574 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5575 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5576 remote_cisco_ops.to_find_new_threads = remote_threads_info;
5577 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5578 remote_cisco_ops.to_stratum = process_stratum;
5579 remote_cisco_ops.to_has_all_memory = 1;
5580 remote_cisco_ops.to_has_memory = 1;
5581 remote_cisco_ops.to_has_stack = 1;
5582 remote_cisco_ops.to_has_registers = 1;
5583 remote_cisco_ops.to_has_execution = 1;
5584 remote_cisco_ops.to_magic = OPS_MAGIC;
5588 remote_can_async_p (void)
5590 /* We're async whenever the serial device is. */
5591 return (current_target.to_async_mask_value) && SERIAL_CAN_ASYNC_P (remote_desc);
5595 remote_is_async_p (void)
5597 /* We're async whenever the serial device is. */
5598 return (current_target.to_async_mask_value) && SERIAL_IS_ASYNC_P (remote_desc);
5601 /* Pass the SERIAL event on and up to the client. One day this code
5602 will be able to delay notifying the client of an event until the
5603 point where an entire packet has been received. */
5605 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5606 static void *async_client_context;
5607 static serial_event_ftype remote_async_serial_handler;
5610 remote_async_serial_handler (serial_t scb, void *context)
5612 /* Don't propogate error information up to the client. Instead let
5613 the client find out about the error by querying the target. */
5614 async_client_callback (INF_REG_EVENT, async_client_context);
5618 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5620 if (current_target.to_async_mask_value == 0)
5621 internal_error (__FILE__, __LINE__,
5622 "Calling remote_async when async is masked");
5624 if (callback != NULL)
5626 SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
5627 async_client_callback = callback;
5628 async_client_context = context;
5631 SERIAL_ASYNC (remote_desc, NULL, NULL);
5634 /* Target async and target extended-async.
5636 This are temporary targets, until it is all tested. Eventually
5637 async support will be incorporated int the usual 'remote'
5641 init_remote_async_ops (void)
5643 remote_async_ops.to_shortname = "async";
5644 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5645 remote_async_ops.to_doc =
5646 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5647 Specify the serial device it is connected to (e.g. /dev/ttya).";
5648 remote_async_ops.to_open = remote_async_open;
5649 remote_async_ops.to_close = remote_close;
5650 remote_async_ops.to_detach = remote_async_detach;
5651 remote_async_ops.to_resume = remote_async_resume;
5652 remote_async_ops.to_wait = remote_async_wait;
5653 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5654 remote_async_ops.to_store_registers = remote_store_registers;
5655 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5656 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5657 remote_async_ops.to_files_info = remote_files_info;
5658 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5659 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5660 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5661 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5662 remote_async_ops.to_kill = remote_async_kill;
5663 remote_async_ops.to_load = generic_load;
5664 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5665 remote_async_ops.to_thread_alive = remote_thread_alive;
5666 remote_async_ops.to_find_new_threads = remote_threads_info;
5667 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5668 remote_async_ops.to_stop = remote_stop;
5669 remote_async_ops.to_query = remote_query;
5670 remote_async_ops.to_rcmd = remote_rcmd;
5671 remote_async_ops.to_stratum = process_stratum;
5672 remote_async_ops.to_has_all_memory = 1;
5673 remote_async_ops.to_has_memory = 1;
5674 remote_async_ops.to_has_stack = 1;
5675 remote_async_ops.to_has_registers = 1;
5676 remote_async_ops.to_has_execution = 1;
5677 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5678 remote_async_ops.to_can_async_p = remote_can_async_p;
5679 remote_async_ops.to_is_async_p = remote_is_async_p;
5680 remote_async_ops.to_async = remote_async;
5681 remote_async_ops.to_async_mask_value = 1;
5682 remote_async_ops.to_magic = OPS_MAGIC;
5685 /* Set up the async extended remote vector by making a copy of the standard
5686 remote vector and adding to it. */
5689 init_extended_async_remote_ops (void)
5691 extended_async_remote_ops = remote_async_ops;
5693 extended_async_remote_ops.to_shortname = "extended-async";
5694 extended_async_remote_ops.to_longname =
5695 "Extended remote serial target in async gdb-specific protocol";
5696 extended_async_remote_ops.to_doc =
5697 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5698 Specify the serial device it is connected to (e.g. /dev/ttya).",
5699 extended_async_remote_ops.to_open = extended_remote_async_open;
5700 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5701 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5705 set_remote_cmd (char *args, int from_tty)
5711 show_remote_cmd (char *args, int from_tty)
5714 show_remote_protocol_Z_packet_cmd (args, from_tty);
5715 show_remote_protocol_e_packet_cmd (args, from_tty);
5716 show_remote_protocol_E_packet_cmd (args, from_tty);
5717 show_remote_protocol_P_packet_cmd (args, from_tty);
5718 show_remote_protocol_binary_download_cmd (args, from_tty);
5722 build_remote_gdbarch_data (void)
5724 build_remote_packet_sizes ();
5727 tty_input = xmalloc (PBUFSIZ);
5728 remote_address_size = TARGET_ADDR_BIT;
5732 _initialize_remote (void)
5734 static struct cmd_list_element *remote_set_cmdlist;
5735 static struct cmd_list_element *remote_show_cmdlist;
5736 struct cmd_list_element *tmpcmd;
5738 /* architecture specific data */
5739 build_remote_gdbarch_data ();
5740 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
5741 register_remote_packet_sizes ();
5742 register_gdbarch_swap (&remote_address_size,
5743 sizeof (&remote_address_size), NULL);
5744 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5747 add_target (&remote_ops);
5749 init_extended_remote_ops ();
5750 add_target (&extended_remote_ops);
5752 init_remote_async_ops ();
5753 add_target (&remote_async_ops);
5755 init_extended_async_remote_ops ();
5756 add_target (&extended_async_remote_ops);
5758 init_remote_cisco_ops ();
5759 add_target (&remote_cisco_ops);
5762 init_remote_threadtests ();
5765 /* set/show remote ... */
5767 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5768 Remote protocol specific variables\n\
5769 Configure various remote-protocol specific variables such as\n\
5770 the packets being used",
5771 &remote_set_cmdlist, "set remote ",
5772 0/*allow-unknown*/, &setlist);
5773 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5774 Remote protocol specific variables\n\
5775 Configure various remote-protocol specific variables such as\n\
5776 the packets being used",
5777 &remote_show_cmdlist, "show remote ",
5778 0/*allow-unknown*/, &showlist);
5780 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5781 "Compare section data on target to the exec file.\n\
5782 Argument is a single section name (default: all loaded sections).",
5785 add_cmd ("packet", class_maintenance, packet_command,
5786 "Send an arbitrary packet to a remote target.\n\
5787 maintenance packet TEXT\n\
5788 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5789 this command sends the string TEXT to the inferior, and displays the\n\
5790 response packet. GDB supplies the initial `$' character, and the\n\
5791 terminating `#' character and checksum.",
5795 (add_set_cmd ("remotebreak", no_class,
5796 var_boolean, (char *) &remote_break,
5797 "Set whether to send break if interrupted.\n",
5801 /* Install commands for configuring memory read/write packets. */
5803 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5804 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5806 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
5807 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5809 add_cmd ("memory-write-packet-size", no_class,
5810 set_memory_write_packet_size,
5811 "Set the maximum number of bytes per memory-write packet.\n"
5812 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5813 "default packet size. The actual limit is further reduced\n"
5814 "dependent on the target. Specify ``fixed'' to disable the\n"
5815 "further restriction and ``limit'' to enable that restriction\n",
5816 &remote_set_cmdlist);
5817 add_cmd ("memory-read-packet-size", no_class,
5818 set_memory_read_packet_size,
5819 "Set the maximum number of bytes per memory-read packet.\n"
5820 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5821 "default packet size. The actual limit is further reduced\n"
5822 "dependent on the target. Specify ``fixed'' to disable the\n"
5823 "further restriction and ``limit'' to enable that restriction\n",
5824 &remote_set_cmdlist);
5825 add_cmd ("memory-write-packet-size", no_class,
5826 show_memory_write_packet_size,
5827 "Show the maximum number of bytes per memory-write packet.\n",
5828 &remote_show_cmdlist);
5829 add_cmd ("memory-read-packet-size", no_class,
5830 show_memory_read_packet_size,
5831 "Show the maximum number of bytes per memory-read packet.\n",
5832 &remote_show_cmdlist);
5835 (add_set_cmd ("remoteaddresssize", class_obscure,
5836 var_integer, (char *) &remote_address_size,
5837 "Set the maximum size of the address (in bits) \
5838 in a memory packet.\n",
5842 add_packet_config_cmd (&remote_protocol_binary_download,
5843 "X", "binary-download",
5844 set_remote_protocol_binary_download_cmd,
5845 show_remote_protocol_binary_download_cmd,
5846 &remote_set_cmdlist, &remote_show_cmdlist,
5849 /* XXXX - should ``set remotebinarydownload'' be retained for
5852 (add_set_cmd ("remotebinarydownload", no_class,
5853 var_boolean, (char *) &remote_binary_download,
5854 "Set binary downloads.\n", &setlist),
5858 add_info ("remote-process", remote_info_process,
5859 "Query the remote system for process info.");
5861 add_packet_config_cmd (&remote_protocol_e,
5862 "e", "step-over-range",
5863 set_remote_protocol_e_packet_cmd,
5864 show_remote_protocol_e_packet_cmd,
5865 &remote_set_cmdlist, &remote_show_cmdlist,
5868 add_packet_config_cmd (&remote_protocol_E,
5869 "E", "step-over-range-w-signal",
5870 set_remote_protocol_E_packet_cmd,
5871 show_remote_protocol_E_packet_cmd,
5872 &remote_set_cmdlist, &remote_show_cmdlist,
5875 add_packet_config_cmd (&remote_protocol_P,
5876 "P", "set-register",
5877 set_remote_protocol_P_packet_cmd,
5878 show_remote_protocol_P_packet_cmd,
5879 &remote_set_cmdlist, &remote_show_cmdlist,
5882 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
5883 "Z0", "software-breakpoint",
5884 set_remote_protocol_Z_software_bp_packet_cmd,
5885 show_remote_protocol_Z_software_bp_packet_cmd,
5886 &remote_set_cmdlist, &remote_show_cmdlist,
5889 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
5890 "Z1", "hardware-breakpoint",
5891 set_remote_protocol_Z_hardware_bp_packet_cmd,
5892 show_remote_protocol_Z_hardware_bp_packet_cmd,
5893 &remote_set_cmdlist, &remote_show_cmdlist,
5896 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
5897 "Z2", "write-watchpoint",
5898 set_remote_protocol_Z_write_wp_packet_cmd,
5899 show_remote_protocol_Z_write_wp_packet_cmd,
5900 &remote_set_cmdlist, &remote_show_cmdlist,
5903 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
5904 "Z3", "read-watchpoint",
5905 set_remote_protocol_Z_read_wp_packet_cmd,
5906 show_remote_protocol_Z_read_wp_packet_cmd,
5907 &remote_set_cmdlist, &remote_show_cmdlist,
5910 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
5911 "Z4", "access-watchpoint",
5912 set_remote_protocol_Z_access_wp_packet_cmd,
5913 show_remote_protocol_Z_access_wp_packet_cmd,
5914 &remote_set_cmdlist, &remote_show_cmdlist,
5917 /* Keep the old ``set remote Z-packet ...'' working. */
5918 tmpcmd = add_set_auto_boolean_cmd ("Z-packet", class_obscure,
5919 &remote_Z_packet_detect,
5921 Set use of remote protocol `Z' packets", &remote_set_cmdlist);
5922 tmpcmd->function.sfunc = set_remote_protocol_Z_packet_cmd;
5923 add_cmd ("Z-packet", class_obscure, show_remote_protocol_Z_packet_cmd,
5924 "Show use of remote protocol `Z' packets ",
5925 &remote_show_cmdlist);