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"
44 #include <sys/types.h>
47 #include "event-loop.h"
48 #include "event-top.h"
54 #include "gdbcore.h" /* for exec_bfd */
56 /* Prototypes for local functions */
57 static void cleanup_sigint_signal_handler (void *dummy);
58 static void initialize_sigint_signal_handler (void);
59 static int getpkt_sane (char *buf, long sizeof_buf, int forever);
61 static void handle_remote_sigint (int);
62 static void handle_remote_sigint_twice (int);
63 static void async_remote_interrupt (gdb_client_data);
64 void async_remote_interrupt_twice (gdb_client_data);
66 static void build_remote_gdbarch_data (void);
68 static int remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len);
70 static int remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len);
72 static void remote_files_info (struct target_ops *ignore);
74 static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
75 int len, int should_write,
76 struct mem_attrib *attrib,
77 struct target_ops *target);
79 static void remote_prepare_to_store (void);
81 static void remote_fetch_registers (int regno);
83 static void remote_resume (ptid_t ptid, int step,
84 enum target_signal siggnal);
85 static void remote_async_resume (ptid_t ptid, int step,
86 enum target_signal siggnal);
87 static int remote_start_remote (PTR);
89 static void remote_open (char *name, int from_tty);
90 static void remote_async_open (char *name, int from_tty);
92 static void extended_remote_open (char *name, int from_tty);
93 static void extended_remote_async_open (char *name, int from_tty);
95 static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
96 static void remote_async_open_1 (char *, int, struct target_ops *,
99 static void remote_close (int quitting);
101 static void remote_store_registers (int regno);
103 static void remote_mourn (void);
104 static void remote_async_mourn (void);
106 static void extended_remote_restart (void);
108 static void extended_remote_mourn (void);
110 static void extended_remote_create_inferior (char *, char *, char **);
111 static void extended_remote_async_create_inferior (char *, char *, char **);
113 static void remote_mourn_1 (struct target_ops *);
115 static void remote_send (char *buf, long sizeof_buf);
117 static int readchar (int timeout);
119 static ptid_t remote_wait (ptid_t ptid,
120 struct target_waitstatus *status);
121 static ptid_t remote_async_wait (ptid_t ptid,
122 struct target_waitstatus *status);
124 static void remote_kill (void);
125 static void remote_async_kill (void);
127 static int tohex (int nib);
129 static void remote_detach (char *args, int from_tty);
130 static void remote_async_detach (char *args, int from_tty);
132 static void remote_interrupt (int signo);
134 static void remote_interrupt_twice (int signo);
136 static void interrupt_query (void);
138 static void set_thread (int, int);
140 static int remote_thread_alive (ptid_t);
142 static void get_offsets (void);
144 static long read_frame (char *buf, long sizeof_buf);
146 static int remote_insert_breakpoint (CORE_ADDR, char *);
148 static int remote_remove_breakpoint (CORE_ADDR, char *);
150 static int hexnumlen (ULONGEST num);
152 static void init_remote_ops (void);
154 static void init_extended_remote_ops (void);
156 static void init_remote_cisco_ops (void);
158 static struct target_ops remote_cisco_ops;
160 static void remote_stop (void);
162 static int ishex (int ch, int *val);
164 static int stubhex (int ch);
166 static int remote_query (int /*char */ , char *, char *, int *);
168 static int hexnumstr (char *, ULONGEST);
170 static int hexnumnstr (char *, ULONGEST, int);
172 static CORE_ADDR remote_address_masked (CORE_ADDR);
174 static void print_packet (char *);
176 static unsigned long crc32 (unsigned char *, int, unsigned int);
178 static void compare_sections_command (char *, int);
180 static void packet_command (char *, int);
182 static int stub_unpack_int (char *buff, int fieldlength);
184 static ptid_t remote_current_thread (ptid_t oldptid);
186 static void remote_find_new_threads (void);
188 static void record_currthread (int currthread);
190 static int fromhex (int a);
192 static int hex2bin (const char *hex, char *bin, int count);
194 static int bin2hex (const char *bin, char *hex, int count);
196 static int putpkt_binary (char *buf, int cnt);
198 static void check_binary_download (CORE_ADDR addr);
200 struct packet_config;
202 static void show_packet_config_cmd (struct packet_config *config);
204 static void update_packet_config (struct packet_config *config);
206 /* Define the target subroutine names */
208 void open_remote_target (char *, int, struct target_ops *, int);
210 void _initialize_remote (void);
214 static struct target_ops remote_ops;
216 static struct target_ops extended_remote_ops;
218 /* Temporary target ops. Just like the remote_ops and
219 extended_remote_ops, but with asynchronous support. */
220 static struct target_ops remote_async_ops;
222 static struct target_ops extended_async_remote_ops;
224 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
225 ``forever'' still use the normal timeout mechanism. This is
226 currently used by the ASYNC code to guarentee that target reads
227 during the initial connect always time-out. Once getpkt has been
228 modified to return a timeout indication and, in turn
229 remote_wait()/wait_for_inferior() have gained a timeout parameter
231 static int wait_forever_enabled_p = 1;
234 /* This variable chooses whether to send a ^C or a break when the user
235 requests program interruption. Although ^C is usually what remote
236 systems expect, and that is the default here, sometimes a break is
237 preferable instead. */
239 static int remote_break;
241 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
242 remote_open knows that we don't have a file open when the program
244 static struct serial *remote_desc = NULL;
246 /* This is set by the target (thru the 'S' message)
247 to denote that the target is in kernel mode. */
248 static int cisco_kernel_mode = 0;
250 /* This variable sets the number of bits in an address that are to be
251 sent in a memory ("M" or "m") packet. Normally, after stripping
252 leading zeros, the entire address would be sent. This variable
253 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
254 initial implementation of remote.c restricted the address sent in
255 memory packets to ``host::sizeof long'' bytes - (typically 32
256 bits). Consequently, for 64 bit targets, the upper 32 bits of an
257 address was never sent. Since fixing this bug may cause a break in
258 some remote targets this variable is principly provided to
259 facilitate backward compatibility. */
261 static int remote_address_size;
263 /* Tempoary to track who currently owns the terminal. See
264 target_async_terminal_* for more details. */
266 static int remote_async_terminal_ours_p;
269 /* This is the size (in chars) of the first response to the ``g''
270 packet. It is used as a heuristic when determining the maximum
271 size of memory-read and memory-write packets. A target will
272 typically only reserve a buffer large enough to hold the ``g''
273 packet. The size does not include packet overhead (headers and
276 static long actual_register_packet_size;
278 /* This is the maximum size (in chars) of a non read/write packet. It
279 is also used as a cap on the size of read/write packets. */
281 static long remote_packet_size;
283 #define PBUFSIZ (remote_packet_size)
285 /* User configurable variables for the number of characters in a
286 memory read/write packet. MIN (PBUFSIZ, g-packet-size) is the
287 default. Some targets need smaller values (fifo overruns, et.al.)
288 and some users need larger values (speed up transfers). The
289 variables ``preferred_*'' (the user request), ``current_*'' (what
290 was actually set) and ``forced_*'' (Positive - a soft limit,
291 negative - a hard limit). */
293 struct memory_packet_config
300 /* Compute the current size of a read/write packet. Since this makes
301 use of ``actual_register_packet_size'' the computation is dynamic. */
304 get_memory_packet_size (struct memory_packet_config *config)
306 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
307 law?) that some hosts don't cope very well with large alloca()
308 calls. Eventually the alloca() code will be replaced by calls to
309 xmalloc() and make_cleanups() allowing this restriction to either
310 be lifted or removed. */
311 #ifndef MAX_REMOTE_PACKET_SIZE
312 #define MAX_REMOTE_PACKET_SIZE 16384
314 /* NOTE: 16 is just chosen at random. */
315 #ifndef MIN_REMOTE_PACKET_SIZE
316 #define MIN_REMOTE_PACKET_SIZE 16
321 if (config->size <= 0)
322 what_they_get = MAX_REMOTE_PACKET_SIZE;
324 what_they_get = config->size;
328 what_they_get = remote_packet_size;
329 /* Limit the packet to the size specified by the user. */
331 && what_they_get > config->size)
332 what_they_get = config->size;
333 /* Limit it to the size of the targets ``g'' response. */
334 if (actual_register_packet_size > 0
335 && what_they_get > actual_register_packet_size)
336 what_they_get = actual_register_packet_size;
338 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
339 what_they_get = MAX_REMOTE_PACKET_SIZE;
340 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
341 what_they_get = MIN_REMOTE_PACKET_SIZE;
342 return what_they_get;
345 /* Update the size of a read/write packet. If they user wants
346 something really big then do a sanity check. */
349 set_memory_packet_size (char *args, struct memory_packet_config *config)
351 int fixed_p = config->fixed_p;
352 long size = config->size;
354 error ("Argument required (integer, `fixed' or `limited').");
355 else if (strcmp (args, "hard") == 0
356 || strcmp (args, "fixed") == 0)
358 else if (strcmp (args, "soft") == 0
359 || strcmp (args, "limit") == 0)
364 size = strtoul (args, &end, 0);
366 error ("Invalid %s (bad syntax).", config->name);
368 /* Instead of explicitly capping the size of a packet to
369 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
370 instead allowed to set the size to something arbitrarily
372 if (size > MAX_REMOTE_PACKET_SIZE)
373 error ("Invalid %s (too large).", config->name);
377 if (fixed_p && !config->fixed_p)
379 if (! query ("The target may not be able to correctly handle a %s\n"
380 "of %ld bytes. Change the packet size? ",
382 error ("Packet size not changed.");
384 /* Update the config. */
385 config->fixed_p = fixed_p;
390 show_memory_packet_size (struct memory_packet_config *config)
392 printf_filtered ("The %s is %ld. ", config->name, config->size);
394 printf_filtered ("Packets are fixed at %ld bytes.\n",
395 get_memory_packet_size (config));
397 printf_filtered ("Packets are limited to %ld bytes.\n",
398 get_memory_packet_size (config));
401 static struct memory_packet_config memory_write_packet_config =
403 "memory-write-packet-size",
407 set_memory_write_packet_size (char *args, int from_tty)
409 set_memory_packet_size (args, &memory_write_packet_config);
413 show_memory_write_packet_size (char *args, int from_tty)
415 show_memory_packet_size (&memory_write_packet_config);
419 get_memory_write_packet_size (void)
421 return get_memory_packet_size (&memory_write_packet_config);
424 static struct memory_packet_config memory_read_packet_config =
426 "memory-read-packet-size",
430 set_memory_read_packet_size (char *args, int from_tty)
432 set_memory_packet_size (args, &memory_read_packet_config);
436 show_memory_read_packet_size (char *args, int from_tty)
438 show_memory_packet_size (&memory_read_packet_config);
442 get_memory_read_packet_size (void)
444 long size = get_memory_packet_size (&memory_read_packet_config);
445 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
446 extra buffer size argument before the memory read size can be
447 increased beyond PBUFSIZ. */
453 /* Register packet size initialization. Since the bounds change when
454 the architecture changes (namely REGISTER_BYTES) this all needs to
458 register_remote_packet_sizes (void)
460 REGISTER_GDBARCH_SWAP (remote_packet_size);
461 REGISTER_GDBARCH_SWAP (actual_register_packet_size);
465 build_remote_packet_sizes (void)
467 /* Default maximum number of characters in a packet body. Many
468 remote stubs have a hardwired buffer size of 400 bytes
469 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
470 as the maximum packet-size to ensure that the packet and an extra
471 NUL character can always fit in the buffer. This stops GDB
472 trashing stubs that try to squeeze an extra NUL into what is
473 already a full buffer (As of 1999-12-04 that was most stubs. */
474 remote_packet_size = 400 - 1;
475 /* Should REGISTER_BYTES needs more space than the default, adjust
476 the size accordingly. Remember that each byte is encoded as two
477 characters. 32 is the overhead for the packet header /
478 footer. NOTE: cagney/1999-10-26: I suspect that 8
479 (``$NN:G...#NN'') is a better guess, the below has been padded a
481 if (REGISTER_BYTES > ((remote_packet_size - 32) / 2))
482 remote_packet_size = (REGISTER_BYTES * 2 + 32);
484 /* This one is filled in when a ``g'' packet is received. */
485 actual_register_packet_size = 0;
488 /* Generic configuration support for packets the stub optionally
489 supports. Allows the user to specify the use of the packet as well
490 as allowing GDB to auto-detect support in the remote stub. */
494 PACKET_SUPPORT_UNKNOWN = 0,
503 enum cmd_auto_boolean detect;
504 enum packet_support support;
507 /* Analyze a packet's return value and update the packet config
518 update_packet_config (struct packet_config *config)
520 switch (config->detect)
522 case CMD_AUTO_BOOLEAN_TRUE:
523 config->support = PACKET_ENABLE;
525 case CMD_AUTO_BOOLEAN_FALSE:
526 config->support = PACKET_DISABLE;
528 case CMD_AUTO_BOOLEAN_AUTO:
529 config->support = PACKET_SUPPORT_UNKNOWN;
535 show_packet_config_cmd (struct packet_config *config)
537 char *support = "internal-error";
538 switch (config->support)
544 support = "disabled";
546 case PACKET_SUPPORT_UNKNOWN:
550 switch (config->detect)
552 case CMD_AUTO_BOOLEAN_AUTO:
553 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
554 config->name, config->title, support);
556 case CMD_AUTO_BOOLEAN_TRUE:
557 case CMD_AUTO_BOOLEAN_FALSE:
558 printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
559 config->name, config->title, support);
565 add_packet_config_cmd (struct packet_config *config,
568 void (*set_func) (char *args, int from_tty,
569 struct cmd_list_element *
571 void (*show_func) (char *name,
573 struct cmd_list_element **set_remote_list,
574 struct cmd_list_element **show_remote_list,
577 struct cmd_list_element *set_cmd;
578 struct cmd_list_element *show_cmd;
583 config->title = title;
584 config->detect = CMD_AUTO_BOOLEAN_AUTO;
585 config->support = PACKET_SUPPORT_UNKNOWN;
586 xasprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
588 xasprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
590 /* set/show TITLE-packet {auto,on,off} */
591 xasprintf (&cmd_name, "%s-packet", title);
592 set_cmd = add_set_auto_boolean_cmd (cmd_name, class_obscure,
593 &config->detect, set_doc,
595 set_cmd->function.sfunc = set_func;
596 show_cmd = add_cmd (cmd_name, class_obscure, show_func, show_doc,
598 /* set/show remote NAME-packet {auto,on,off} -- legacy */
602 xasprintf (&legacy_name, "%s-packet", name);
603 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
605 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
610 static enum packet_result
611 packet_ok (const char *buf, struct packet_config *config)
615 /* The stub recognized the packet request. Check that the
616 operation succeeded. */
617 switch (config->support)
619 case PACKET_SUPPORT_UNKNOWN:
621 fprintf_unfiltered (gdb_stdlog,
622 "Packet %s (%s) is supported\n",
623 config->name, config->title);
624 config->support = PACKET_ENABLE;
627 internal_error (__FILE__, __LINE__,
628 "packet_ok: attempt to use a disabled packet");
633 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
634 /* "OK" - definitly OK. */
637 && isxdigit (buf[1]) && isxdigit (buf[2])
639 /* "Enn" - definitly an error. */
641 /* The packet may or may not be OK. Just assume it is */
646 /* The stub does not support the packet. */
647 switch (config->support)
650 if (config->detect == CMD_AUTO_BOOLEAN_AUTO)
651 /* If the stub previously indicated that the packet was
652 supported then there is a protocol error.. */
653 error ("Protocol error: %s (%s) conflicting enabled responses.",
654 config->name, config->title);
656 /* The user set it wrong. */
657 error ("Enabled packet %s (%s) not recognized by stub",
658 config->name, config->title);
660 case PACKET_SUPPORT_UNKNOWN:
662 fprintf_unfiltered (gdb_stdlog,
663 "Packet %s (%s) is NOT supported\n",
664 config->name, config->title);
665 config->support = PACKET_DISABLE;
670 return PACKET_UNKNOWN;
674 /* Should we try the 'qSymbol' (target symbol lookup service) request? */
675 static struct packet_config remote_protocol_qSymbol;
678 set_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty,
679 struct cmd_list_element *c)
681 update_packet_config (&remote_protocol_qSymbol);
685 show_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty)
687 show_packet_config_cmd (&remote_protocol_qSymbol);
690 /* Should we try the 'e' (step over range) 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 'E' (step over range / w signal #) request? */
708 static struct packet_config remote_protocol_E;
711 set_remote_protocol_E_packet_cmd (char *args, int from_tty,
712 struct cmd_list_element *c)
714 update_packet_config (&remote_protocol_E);
718 show_remote_protocol_E_packet_cmd (char *args, int from_tty)
720 show_packet_config_cmd (&remote_protocol_E);
724 /* Should we try the 'P' (set register) request? */
726 static struct packet_config remote_protocol_P;
729 set_remote_protocol_P_packet_cmd (char *args, int from_tty,
730 struct cmd_list_element *c)
732 update_packet_config (&remote_protocol_P);
736 show_remote_protocol_P_packet_cmd (char *args, int from_tty)
738 show_packet_config_cmd (&remote_protocol_P);
741 /* Should we try one of the 'Z' requests? */
745 Z_PACKET_SOFTWARE_BP,
746 Z_PACKET_HARDWARE_BP,
753 static struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES];
755 /* FIXME: Instead of having all these boiler plate functions, the
756 command callback should include a context argument. */
759 set_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
760 struct cmd_list_element *c)
762 update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
766 show_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty)
768 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
772 set_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
773 struct cmd_list_element *c)
775 update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
779 show_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty)
781 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
785 set_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
786 struct cmd_list_element *c)
788 update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
792 show_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty)
794 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
798 set_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
799 struct cmd_list_element *c)
801 update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]);
805 show_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty)
807 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
811 set_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
812 struct cmd_list_element *c)
814 update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
818 show_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty)
820 show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
823 /* For compatibility with older distributions. Provide a ``set remote
824 Z-packet ...'' command that updates all the Z packet types. */
826 static enum cmd_auto_boolean remote_Z_packet_detect;
829 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
830 struct cmd_list_element *c)
833 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
835 remote_protocol_Z[i].detect = remote_Z_packet_detect;
836 update_packet_config (&remote_protocol_Z[i]);
841 show_remote_protocol_Z_packet_cmd (char *args, int from_tty)
844 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
846 show_packet_config_cmd (&remote_protocol_Z[i]);
850 /* Should we try the 'X' (remote binary download) packet?
852 This variable (available to the user via "set remote X-packet")
853 dictates whether downloads are sent in binary (via the 'X' packet).
854 We assume that the stub can, and attempt to do it. This will be
855 cleared if the stub does not understand it. This switch is still
856 needed, though in cases when the packet is supported in the stub,
857 but the connection does not allow it (i.e., 7-bit serial connection
860 static struct packet_config remote_protocol_binary_download;
862 /* Should we try the 'ThreadInfo' query packet?
864 This variable (NOT available to the user: auto-detect only!)
865 determines whether GDB will use the new, simpler "ThreadInfo"
866 query or the older, more complex syntax for thread queries.
867 This is an auto-detect variable (set to true at each connect,
868 and set to false when the target fails to recognize it). */
870 static int use_threadinfo_query;
871 static int use_threadextra_query;
874 set_remote_protocol_binary_download_cmd (char *args,
876 struct cmd_list_element *c)
878 update_packet_config (&remote_protocol_binary_download);
882 show_remote_protocol_binary_download_cmd (char *args,
885 show_packet_config_cmd (&remote_protocol_binary_download);
889 /* Tokens for use by the asynchronous signal handlers for SIGINT */
890 PTR sigint_remote_twice_token;
891 PTR sigint_remote_token;
893 /* These are pointers to hook functions that may be set in order to
894 modify resume/wait behavior for a particular architecture. */
896 void (*target_resume_hook) (void);
897 void (*target_wait_loop_hook) (void);
901 /* These are the threads which we last sent to the remote system.
902 -1 for all or -2 for not sent yet. */
903 static int general_thread;
904 static int continue_thread;
906 /* Call this function as a result of
907 1) A halt indication (T packet) containing a thread id
908 2) A direct query of currthread
909 3) Successful execution of set thread
913 record_currthread (int currthread)
915 general_thread = currthread;
917 /* If this is a new thread, add it to GDB's thread list.
918 If we leave it up to WFI to do this, bad things will happen. */
919 if (!in_thread_list (pid_to_ptid (currthread)))
921 add_thread (pid_to_ptid (currthread));
923 ui_out_text (uiout, "[New ");
924 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
925 ui_out_text (uiout, "]\n");
927 printf_filtered ("[New %s]\n",
928 target_pid_to_str (pid_to_ptid (currthread)));
933 #define MAGIC_NULL_PID 42000
936 set_thread (int th, int gen)
938 char *buf = alloca (PBUFSIZ);
939 int state = gen ? general_thread : continue_thread;
945 buf[1] = gen ? 'g' : 'c';
946 if (th == MAGIC_NULL_PID)
952 sprintf (&buf[2], "-%x", -th);
954 sprintf (&buf[2], "%x", th);
956 getpkt (buf, PBUFSIZ, 0);
960 continue_thread = th;
963 /* Return nonzero if the thread TH is still alive on the remote system. */
966 remote_thread_alive (ptid_t ptid)
968 int tid = PIDGET (ptid);
972 sprintf (buf, "T-%08x", -tid);
974 sprintf (buf, "T%08x", tid);
976 getpkt (buf, sizeof (buf), 0);
977 return (buf[0] == 'O' && buf[1] == 'K');
980 /* About these extended threadlist and threadinfo packets. They are
981 variable length packets but, the fields within them are often fixed
982 length. They are redundent enough to send over UDP as is the
983 remote protocol in general. There is a matching unit test module
986 #define OPAQUETHREADBYTES 8
988 /* a 64 bit opaque identifier */
989 typedef unsigned char threadref[OPAQUETHREADBYTES];
991 /* WARNING: This threadref data structure comes from the remote O.S., libstub
992 protocol encoding, and remote.c. it is not particularly changable */
994 /* Right now, the internal structure is int. We want it to be bigger.
998 typedef int gdb_threadref; /* internal GDB thread reference */
1000 /* gdb_ext_thread_info is an internal GDB data structure which is
1001 equivalint to the reply of the remote threadinfo packet */
1003 struct gdb_ext_thread_info
1005 threadref threadid; /* External form of thread reference */
1006 int active; /* Has state interesting to GDB? , regs, stack */
1007 char display[256]; /* Brief state display, name, blocked/syspended */
1008 char shortname[32]; /* To be used to name threads */
1009 char more_display[256]; /* Long info, statistics, queue depth, whatever */
1012 /* The volume of remote transfers can be limited by submitting
1013 a mask containing bits specifying the desired information.
1014 Use a union of these values as the 'selection' parameter to
1015 get_thread_info. FIXME: Make these TAG names more thread specific.
1018 #define TAG_THREADID 1
1019 #define TAG_EXISTS 2
1020 #define TAG_DISPLAY 4
1021 #define TAG_THREADNAME 8
1022 #define TAG_MOREDISPLAY 16
1024 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
1026 char *unpack_varlen_hex (char *buff, int *result);
1028 static char *unpack_nibble (char *buf, int *val);
1030 static char *pack_nibble (char *buf, int nibble);
1032 static char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
1034 static char *unpack_byte (char *buf, int *value);
1036 static char *pack_int (char *buf, int value);
1038 static char *unpack_int (char *buf, int *value);
1040 static char *unpack_string (char *src, char *dest, int length);
1042 static char *pack_threadid (char *pkt, threadref * id);
1044 static char *unpack_threadid (char *inbuf, threadref * id);
1046 void int_to_threadref (threadref * id, int value);
1048 static int threadref_to_int (threadref * ref);
1050 static void copy_threadref (threadref * dest, threadref * src);
1052 static int threadmatch (threadref * dest, threadref * src);
1054 static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
1056 static int remote_unpack_thread_info_response (char *pkt,
1057 threadref * expectedref,
1058 struct gdb_ext_thread_info
1062 static int remote_get_threadinfo (threadref * threadid, int fieldset, /*TAG mask */
1063 struct gdb_ext_thread_info *info);
1065 static int adapt_remote_get_threadinfo (gdb_threadref * ref,
1067 struct gdb_ext_thread_info *info);
1069 static char *pack_threadlist_request (char *pkt, int startflag,
1071 threadref * nextthread);
1073 static int parse_threadlist_response (char *pkt,
1075 threadref * original_echo,
1076 threadref * resultlist, int *doneflag);
1078 static int remote_get_threadlist (int startflag,
1079 threadref * nextthread,
1082 int *result_count, threadref * threadlist);
1084 typedef int (*rmt_thread_action) (threadref * ref, void *context);
1086 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1087 void *context, int looplimit);
1089 static int remote_newthread_step (threadref * ref, void *context);
1091 /* encode 64 bits in 16 chars of hex */
1093 static const char hexchars[] = "0123456789abcdef";
1096 ishex (int ch, int *val)
1098 if ((ch >= 'a') && (ch <= 'f'))
1100 *val = ch - 'a' + 10;
1103 if ((ch >= 'A') && (ch <= 'F'))
1105 *val = ch - 'A' + 10;
1108 if ((ch >= '0') && (ch <= '9'))
1119 if (ch >= 'a' && ch <= 'f')
1120 return ch - 'a' + 10;
1121 if (ch >= '0' && ch <= '9')
1123 if (ch >= 'A' && ch <= 'F')
1124 return ch - 'A' + 10;
1129 stub_unpack_int (char *buff, int fieldlength)
1136 nibble = stubhex (*buff++);
1140 retval = retval << 4;
1146 unpack_varlen_hex (char *buff, /* packet to parse */
1152 while (ishex (*buff, &nibble))
1155 retval = retval << 4;
1156 retval |= nibble & 0x0f;
1163 unpack_nibble (char *buf, int *val)
1165 ishex (*buf++, val);
1170 pack_nibble (char *buf, int nibble)
1172 *buf++ = hexchars[(nibble & 0x0f)];
1177 pack_hex_byte (char *pkt, int byte)
1179 *pkt++ = hexchars[(byte >> 4) & 0xf];
1180 *pkt++ = hexchars[(byte & 0xf)];
1185 unpack_byte (char *buf, int *value)
1187 *value = stub_unpack_int (buf, 2);
1192 pack_int (char *buf, int value)
1194 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1195 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1196 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1197 buf = pack_hex_byte (buf, (value & 0xff));
1202 unpack_int (char *buf, int *value)
1204 *value = stub_unpack_int (buf, 8);
1208 #if 0 /* currently unused, uncomment when needed */
1209 static char *pack_string (char *pkt, char *string);
1212 pack_string (char *pkt, char *string)
1217 len = strlen (string);
1219 len = 200; /* Bigger than most GDB packets, junk??? */
1220 pkt = pack_hex_byte (pkt, len);
1224 if ((ch == '\0') || (ch == '#'))
1225 ch = '*'; /* Protect encapsulation */
1230 #endif /* 0 (unused) */
1233 unpack_string (char *src, char *dest, int length)
1242 pack_threadid (char *pkt, threadref *id)
1245 unsigned char *altid;
1247 altid = (unsigned char *) id;
1248 limit = pkt + BUF_THREAD_ID_SIZE;
1250 pkt = pack_hex_byte (pkt, *altid++);
1256 unpack_threadid (char *inbuf, threadref *id)
1259 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1262 altref = (char *) id;
1264 while (inbuf < limit)
1266 x = stubhex (*inbuf++);
1267 y = stubhex (*inbuf++);
1268 *altref++ = (x << 4) | y;
1273 /* Externally, threadrefs are 64 bits but internally, they are still
1274 ints. This is due to a mismatch of specifications. We would like
1275 to use 64bit thread references internally. This is an adapter
1279 int_to_threadref (threadref *id, int value)
1281 unsigned char *scan;
1283 scan = (unsigned char *) id;
1289 *scan++ = (value >> 24) & 0xff;
1290 *scan++ = (value >> 16) & 0xff;
1291 *scan++ = (value >> 8) & 0xff;
1292 *scan++ = (value & 0xff);
1296 threadref_to_int (threadref *ref)
1299 unsigned char *scan;
1301 scan = (char *) ref;
1305 value = (value << 8) | ((*scan++) & 0xff);
1310 copy_threadref (threadref *dest, threadref *src)
1313 unsigned char *csrc, *cdest;
1315 csrc = (unsigned char *) src;
1316 cdest = (unsigned char *) dest;
1323 threadmatch (threadref *dest, threadref *src)
1325 /* things are broken right now, so just assume we got a match */
1327 unsigned char *srcp, *destp;
1329 srcp = (char *) src;
1330 destp = (char *) dest;
1334 result &= (*srcp++ == *destp++) ? 1 : 0;
1341 threadid:1, # always request threadid
1348 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1351 pack_threadinfo_request (char *pkt, int mode, threadref *id)
1353 *pkt++ = 'q'; /* Info Query */
1354 *pkt++ = 'P'; /* process or thread info */
1355 pkt = pack_int (pkt, mode); /* mode */
1356 pkt = pack_threadid (pkt, id); /* threadid */
1357 *pkt = '\0'; /* terminate */
1361 /* These values tag the fields in a thread info response packet */
1362 /* Tagging the fields allows us to request specific fields and to
1363 add more fields as time goes by */
1365 #define TAG_THREADID 1 /* Echo the thread identifier */
1366 #define TAG_EXISTS 2 /* Is this process defined enough to
1367 fetch registers and its stack */
1368 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1369 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1370 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1374 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1375 struct gdb_ext_thread_info *info)
1380 char *limit = pkt + PBUFSIZ; /* plausable parsing limit */
1383 /* info->threadid = 0; FIXME: implement zero_threadref */
1385 info->display[0] = '\0';
1386 info->shortname[0] = '\0';
1387 info->more_display[0] = '\0';
1389 /* Assume the characters indicating the packet type have been stripped */
1390 pkt = unpack_int (pkt, &mask); /* arg mask */
1391 pkt = unpack_threadid (pkt, &ref);
1394 warning ("Incomplete response to threadinfo request\n");
1395 if (!threadmatch (&ref, expectedref))
1396 { /* This is an answer to a different request */
1397 warning ("ERROR RMT Thread info mismatch\n");
1400 copy_threadref (&info->threadid, &ref);
1402 /* Loop on tagged fields , try to bail if somthing goes wrong */
1404 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
1406 pkt = unpack_int (pkt, &tag); /* tag */
1407 pkt = unpack_byte (pkt, &length); /* length */
1408 if (!(tag & mask)) /* tags out of synch with mask */
1410 warning ("ERROR RMT: threadinfo tag mismatch\n");
1414 if (tag == TAG_THREADID)
1418 warning ("ERROR RMT: length of threadid is not 16\n");
1422 pkt = unpack_threadid (pkt, &ref);
1423 mask = mask & ~TAG_THREADID;
1426 if (tag == TAG_EXISTS)
1428 info->active = stub_unpack_int (pkt, length);
1430 mask = mask & ~(TAG_EXISTS);
1433 warning ("ERROR RMT: 'exists' length too long\n");
1439 if (tag == TAG_THREADNAME)
1441 pkt = unpack_string (pkt, &info->shortname[0], length);
1442 mask = mask & ~TAG_THREADNAME;
1445 if (tag == TAG_DISPLAY)
1447 pkt = unpack_string (pkt, &info->display[0], length);
1448 mask = mask & ~TAG_DISPLAY;
1451 if (tag == TAG_MOREDISPLAY)
1453 pkt = unpack_string (pkt, &info->more_display[0], length);
1454 mask = mask & ~TAG_MOREDISPLAY;
1457 warning ("ERROR RMT: unknown thread info tag\n");
1458 break; /* Not a tag we know about */
1464 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1465 struct gdb_ext_thread_info *info)
1468 char *threadinfo_pkt = alloca (PBUFSIZ);
1470 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1471 putpkt (threadinfo_pkt);
1472 getpkt (threadinfo_pkt, PBUFSIZ, 0);
1473 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1478 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1479 representation of a threadid. */
1482 adapt_remote_get_threadinfo (gdb_threadref *ref, int selection,
1483 struct gdb_ext_thread_info *info)
1487 int_to_threadref (&lclref, *ref);
1488 return remote_get_threadinfo (&lclref, selection, info);
1491 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1494 pack_threadlist_request (char *pkt, int startflag, int threadcount,
1495 threadref *nextthread)
1497 *pkt++ = 'q'; /* info query packet */
1498 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1499 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1500 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1501 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1506 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1509 parse_threadlist_response (char *pkt, int result_limit,
1510 threadref *original_echo, threadref *resultlist,
1514 int count, resultcount, done;
1517 /* Assume the 'q' and 'M chars have been stripped. */
1518 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */
1519 pkt = unpack_byte (pkt, &count); /* count field */
1520 pkt = unpack_nibble (pkt, &done);
1521 /* The first threadid is the argument threadid. */
1522 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1523 while ((count-- > 0) && (pkt < limit))
1525 pkt = unpack_threadid (pkt, resultlist++);
1526 if (resultcount++ >= result_limit)
1535 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1536 int *done, int *result_count, threadref *threadlist)
1538 static threadref echo_nextthread;
1539 char *threadlist_packet = alloca (PBUFSIZ);
1540 char *t_response = alloca (PBUFSIZ);
1543 /* Trancate result limit to be smaller than the packet size */
1544 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
1545 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
1547 pack_threadlist_request (threadlist_packet,
1548 startflag, result_limit, nextthread);
1549 putpkt (threadlist_packet);
1550 getpkt (t_response, PBUFSIZ, 0);
1553 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1556 if (!threadmatch (&echo_nextthread, nextthread))
1558 /* FIXME: This is a good reason to drop the packet */
1559 /* Possably, there is a duplicate response */
1561 retransmit immediatly - race conditions
1562 retransmit after timeout - yes
1564 wait for packet, then exit
1566 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1567 return 0; /* I choose simply exiting */
1569 if (*result_count <= 0)
1573 warning ("RMT ERROR : failed to get remote thread list\n");
1576 return result; /* break; */
1578 if (*result_count > result_limit)
1581 warning ("RMT ERROR: threadlist response longer than requested\n");
1587 /* This is the interface between remote and threads, remotes upper interface */
1589 /* remote_find_new_threads retrieves the thread list and for each
1590 thread in the list, looks up the thread in GDB's internal list,
1591 ading the thread if it does not already exist. This involves
1592 getting partial thread lists from the remote target so, polling the
1593 quit_flag is required. */
1596 /* About this many threadisds fit in a packet. */
1598 #define MAXTHREADLISTRESULTS 32
1601 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1604 int done, i, result_count;
1608 static threadref nextthread;
1609 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1614 if (loopcount++ > looplimit)
1617 warning ("Remote fetch threadlist -infinite loop-\n");
1620 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1621 &done, &result_count, resultthreadlist))
1626 /* clear for later iterations */
1628 /* Setup to resume next batch of thread references, set nextthread. */
1629 if (result_count >= 1)
1630 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1632 while (result_count--)
1633 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1640 remote_newthread_step (threadref *ref, void *context)
1644 ptid = pid_to_ptid (threadref_to_int (ref));
1646 if (!in_thread_list (ptid))
1648 return 1; /* continue iterator */
1651 #define CRAZY_MAX_THREADS 1000
1654 remote_current_thread (ptid_t oldpid)
1656 char *buf = alloca (PBUFSIZ);
1659 getpkt (buf, PBUFSIZ, 0);
1660 if (buf[0] == 'Q' && buf[1] == 'C')
1661 return pid_to_ptid (strtol (&buf[2], NULL, 16));
1666 /* Find new threads for info threads command.
1667 * Original version, using John Metzler's thread protocol.
1671 remote_find_new_threads (void)
1673 remote_threadlist_iterator (remote_newthread_step, 0,
1675 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1676 inferior_ptid = remote_current_thread (inferior_ptid);
1680 * Find all threads for info threads command.
1681 * Uses new thread protocol contributed by Cisco.
1682 * Falls back and attempts to use the older method (above)
1683 * if the target doesn't respond to the new method.
1687 remote_threads_info (void)
1689 char *buf = alloca (PBUFSIZ);
1693 if (remote_desc == 0) /* paranoia */
1694 error ("Command can only be used when connected to the remote target.");
1696 if (use_threadinfo_query)
1698 putpkt ("qfThreadInfo");
1700 getpkt (bufp, PBUFSIZ, 0);
1701 if (bufp[0] != '\0') /* q packet recognized */
1703 while (*bufp++ == 'm') /* reply contains one or more TID */
1707 tid = strtol (bufp, &bufp, 16);
1708 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1709 add_thread (pid_to_ptid (tid));
1711 while (*bufp++ == ','); /* comma-separated list */
1712 putpkt ("qsThreadInfo");
1714 getpkt (bufp, PBUFSIZ, 0);
1720 /* Else fall back to old method based on jmetzler protocol. */
1721 use_threadinfo_query = 0;
1722 remote_find_new_threads ();
1727 * Collect a descriptive string about the given thread.
1728 * The target may say anything it wants to about the thread
1729 * (typically info about its blocked / runnable state, name, etc.).
1730 * This string will appear in the info threads display.
1732 * Optional: targets are not required to implement this function.
1736 remote_threads_extra_info (struct thread_info *tp)
1741 struct gdb_ext_thread_info threadinfo;
1742 static char display_buf[100]; /* arbitrary... */
1743 char *bufp = alloca (PBUFSIZ);
1744 int n = 0; /* position in display_buf */
1746 if (remote_desc == 0) /* paranoia */
1747 internal_error (__FILE__, __LINE__,
1748 "remote_threads_extra_info");
1750 if (use_threadextra_query)
1752 sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid));
1754 getpkt (bufp, PBUFSIZ, 0);
1757 n = min (strlen (bufp) / 2, sizeof (display_buf));
1758 result = hex2bin (bufp, display_buf, n);
1759 display_buf [result] = '\0';
1764 /* If the above query fails, fall back to the old method. */
1765 use_threadextra_query = 0;
1766 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1767 | TAG_MOREDISPLAY | TAG_DISPLAY;
1768 int_to_threadref (&id, PIDGET (tp->ptid));
1769 if (remote_get_threadinfo (&id, set, &threadinfo))
1770 if (threadinfo.active)
1772 if (*threadinfo.shortname)
1773 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1774 if (*threadinfo.display)
1775 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1776 if (*threadinfo.more_display)
1777 n += sprintf(&display_buf[n], " Priority: %s",
1778 threadinfo.more_display);
1782 /* for purely cosmetic reasons, clear up trailing commas */
1783 if (',' == display_buf[n-1])
1784 display_buf[n-1] = ' ';
1793 /* Restart the remote side; this is an extended protocol operation. */
1796 extended_remote_restart (void)
1798 char *buf = alloca (PBUFSIZ);
1800 /* Send the restart command; for reasons I don't understand the
1801 remote side really expects a number after the "R". */
1803 sprintf (&buf[1], "%x", 0);
1806 /* Now query for status so this looks just like we restarted
1807 gdbserver from scratch. */
1809 getpkt (buf, PBUFSIZ, 0);
1812 /* Clean up connection to a remote debugger. */
1816 remote_close (int quitting)
1819 serial_close (remote_desc);
1823 /* Query the remote side for the text, data and bss offsets. */
1828 char *buf = alloca (PBUFSIZ);
1831 CORE_ADDR text_addr, data_addr, bss_addr;
1832 struct section_offsets *offs;
1834 putpkt ("qOffsets");
1836 getpkt (buf, PBUFSIZ, 0);
1838 if (buf[0] == '\000')
1839 return; /* Return silently. Stub doesn't support
1843 warning ("Remote failure reply: %s", buf);
1847 /* Pick up each field in turn. This used to be done with scanf, but
1848 scanf will make trouble if CORE_ADDR size doesn't match
1849 conversion directives correctly. The following code will work
1850 with any size of CORE_ADDR. */
1851 text_addr = data_addr = bss_addr = 0;
1855 if (strncmp (ptr, "Text=", 5) == 0)
1858 /* Don't use strtol, could lose on big values. */
1859 while (*ptr && *ptr != ';')
1860 text_addr = (text_addr << 4) + fromhex (*ptr++);
1865 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1868 while (*ptr && *ptr != ';')
1869 data_addr = (data_addr << 4) + fromhex (*ptr++);
1874 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1877 while (*ptr && *ptr != ';')
1878 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1884 error ("Malformed response to offset query, %s", buf);
1886 if (symfile_objfile == NULL)
1889 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1890 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1892 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
1894 /* This is a temporary kludge to force data and bss to use the same offsets
1895 because that's what nlmconv does now. The real solution requires changes
1896 to the stub and remote.c that I don't have time to do right now. */
1898 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1899 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
1901 objfile_relocate (symfile_objfile, offs);
1905 * Cisco version of section offsets:
1907 * Instead of having GDB query the target for the section offsets,
1908 * Cisco lets the target volunteer the information! It's also in
1909 * a different format, so here are the functions that will decode
1910 * a section offset packet from a Cisco target.
1914 * Function: remote_cisco_section_offsets
1916 * Returns: zero for success, non-zero for failure
1920 remote_cisco_section_offsets (bfd_vma text_addr,
1923 bfd_signed_vma *text_offs,
1924 bfd_signed_vma *data_offs,
1925 bfd_signed_vma *bss_offs)
1927 bfd_vma text_base, data_base, bss_base;
1928 struct minimal_symbol *start;
1933 if (symfile_objfile == NULL)
1934 return -1; /* no can do nothin' */
1936 start = lookup_minimal_symbol ("_start", NULL, NULL);
1938 return -1; /* Can't find "_start" symbol */
1940 data_base = bss_base = 0;
1941 text_base = SYMBOL_VALUE_ADDRESS (start);
1943 abfd = symfile_objfile->obfd;
1944 for (sect = abfd->sections;
1948 const char *p = bfd_get_section_name (abfd, sect);
1950 if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */
1951 if (data_base == 0 ||
1952 data_base > bfd_get_section_vma (abfd, sect))
1953 data_base = bfd_get_section_vma (abfd, sect);
1954 if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */
1955 if (bss_base == 0 ||
1956 bss_base > bfd_get_section_vma (abfd, sect))
1957 bss_base = bfd_get_section_vma (abfd, sect);
1959 *text_offs = text_addr - text_base;
1960 *data_offs = data_addr - data_base;
1961 *bss_offs = bss_addr - bss_base;
1966 sprintf (tmp, "VMA: text = 0x");
1967 sprintf_vma (tmp + strlen (tmp), text_addr);
1968 sprintf (tmp + strlen (tmp), " data = 0x");
1969 sprintf_vma (tmp + strlen (tmp), data_addr);
1970 sprintf (tmp + strlen (tmp), " bss = 0x");
1971 sprintf_vma (tmp + strlen (tmp), bss_addr);
1972 fprintf_filtered (gdb_stdlog, tmp);
1973 fprintf_filtered (gdb_stdlog,
1974 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1975 paddr_nz (*text_offs),
1976 paddr_nz (*data_offs),
1977 paddr_nz (*bss_offs));
1984 * Function: remote_cisco_objfile_relocate
1986 * Relocate the symbol file for a remote target.
1990 remote_cisco_objfile_relocate (bfd_signed_vma text_off, bfd_signed_vma data_off,
1991 bfd_signed_vma bss_off)
1993 struct section_offsets *offs;
1995 if (text_off != 0 || data_off != 0 || bss_off != 0)
1997 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1998 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1999 simple canonical representation for this stuff. */
2001 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
2002 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
2004 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_off;
2005 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_off;
2006 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = bss_off;
2008 /* First call the standard objfile_relocate. */
2009 objfile_relocate (symfile_objfile, offs);
2011 /* Now we need to fix up the section entries already attached to
2012 the exec target. These entries will control memory transfers
2013 from the exec file. */
2015 exec_set_section_offsets (text_off, data_off, bss_off);
2019 /* Stub for catch_errors. */
2022 remote_start_remote_dummy (void *dummy)
2024 start_remote (); /* Initialize gdb process mechanisms */
2029 remote_start_remote (PTR dummy)
2031 immediate_quit++; /* Allow user to interrupt it */
2033 /* Ack any packet which the remote side has already sent. */
2034 serial_write (remote_desc, "+", 1);
2036 /* Let the stub know that we want it to return the thread. */
2039 inferior_ptid = remote_current_thread (inferior_ptid);
2041 get_offsets (); /* Get text, data & bss offsets */
2043 putpkt ("?"); /* initiate a query from remote machine */
2046 return remote_start_remote_dummy (dummy);
2049 /* Open a connection to a remote debugger.
2050 NAME is the filename used for communication. */
2053 remote_open (char *name, int from_tty)
2055 remote_open_1 (name, from_tty, &remote_ops, 0);
2058 /* Just like remote_open, but with asynchronous support. */
2060 remote_async_open (char *name, int from_tty)
2062 remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
2065 /* Open a connection to a remote debugger using the extended
2066 remote gdb protocol. NAME is the filename used for communication. */
2069 extended_remote_open (char *name, int from_tty)
2071 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ );
2074 /* Just like extended_remote_open, but with asynchronous support. */
2076 extended_remote_async_open (char *name, int from_tty)
2078 remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ );
2081 /* Generic code for opening a connection to a remote target. */
2084 init_all_packet_configs (void)
2087 update_packet_config (&remote_protocol_e);
2088 update_packet_config (&remote_protocol_E);
2089 update_packet_config (&remote_protocol_P);
2090 update_packet_config (&remote_protocol_qSymbol);
2091 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
2092 update_packet_config (&remote_protocol_Z[i]);
2093 /* Force remote_write_bytes to check whether target supports binary
2095 update_packet_config (&remote_protocol_binary_download);
2098 /* Symbol look-up. */
2101 remote_check_symbols (struct objfile *objfile)
2103 char *msg, *reply, *tmp;
2104 struct minimal_symbol *sym;
2107 if (remote_protocol_qSymbol.support == PACKET_DISABLE)
2110 msg = alloca (PBUFSIZ);
2111 reply = alloca (PBUFSIZ);
2113 /* Invite target to request symbol lookups. */
2115 putpkt ("qSymbol::");
2116 getpkt (reply, PBUFSIZ, 0);
2117 packet_ok (reply, &remote_protocol_qSymbol);
2119 while (strncmp (reply, "qSymbol:", 8) == 0)
2122 end = hex2bin (tmp, msg, strlen (tmp) / 2);
2124 sym = lookup_minimal_symbol (msg, NULL, NULL);
2126 sprintf (msg, "qSymbol::%s", &reply[8]);
2128 sprintf (msg, "qSymbol:%s:%s",
2129 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2132 getpkt (reply, PBUFSIZ, 0);
2137 remote_open_1 (char *name, int from_tty, struct target_ops *target,
2141 error ("To open a remote debug connection, you need to specify what\n\
2142 serial device is attached to the remote system\n\
2143 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2145 /* See FIXME above */
2146 wait_forever_enabled_p = 1;
2148 target_preopen (from_tty);
2150 unpush_target (target);
2152 remote_desc = serial_open (name);
2154 perror_with_name (name);
2156 if (baud_rate != -1)
2158 if (serial_setbaudrate (remote_desc, baud_rate))
2160 serial_close (remote_desc);
2161 perror_with_name (name);
2165 serial_raw (remote_desc);
2167 /* If there is something sitting in the buffer we might take it as a
2168 response to a command, which would be bad. */
2169 serial_flush_input (remote_desc);
2173 puts_filtered ("Remote debugging using ");
2174 puts_filtered (name);
2175 puts_filtered ("\n");
2177 push_target (target); /* Switch to using remote target now */
2179 init_all_packet_configs ();
2181 general_thread = -2;
2182 continue_thread = -2;
2184 /* Probe for ability to use "ThreadInfo" query, as required. */
2185 use_threadinfo_query = 1;
2186 use_threadextra_query = 1;
2188 /* Without this, some commands which require an active target (such
2189 as kill) won't work. This variable serves (at least) double duty
2190 as both the pid of the target process (if it has such), and as a
2191 flag indicating that a target is active. These functions should
2192 be split out into seperate variables, especially since GDB will
2193 someday have a notion of debugging several processes. */
2195 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2196 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2197 /* First delete any symbols previously loaded from shared libraries. */
2198 no_shared_libraries (NULL, 0);
2201 /* Start the remote connection; if error (0), discard this target.
2202 In particular, if the user quits, be sure to discard it
2203 (we'd be in an inconsistent state otherwise). */
2204 if (!catch_errors (remote_start_remote, NULL,
2205 "Couldn't establish connection to remote target\n",
2214 /* Tell the remote that we are using the extended protocol. */
2215 char *buf = alloca (PBUFSIZ);
2217 getpkt (buf, PBUFSIZ, 0);
2219 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2220 /* FIXME: need a master target_open vector from which all
2221 remote_opens can be called, so that stuff like this can
2222 go there. Failing that, the following code must be copied
2223 to the open function for any remote target that wants to
2224 support svr4 shared libraries. */
2226 /* Set up to detect and load shared libraries. */
2227 if (exec_bfd) /* No use without an exec file. */
2229 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2230 remote_check_symbols (symfile_objfile);
2235 /* Just like remote_open but with asynchronous support. */
2237 remote_async_open_1 (char *name, int from_tty, struct target_ops *target,
2241 error ("To open a remote debug connection, you need to specify what\n\
2242 serial device is attached to the remote system\n\
2243 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2245 target_preopen (from_tty);
2247 unpush_target (target);
2249 remote_desc = serial_open (name);
2251 perror_with_name (name);
2253 if (baud_rate != -1)
2255 if (serial_setbaudrate (remote_desc, baud_rate))
2257 serial_close (remote_desc);
2258 perror_with_name (name);
2262 serial_raw (remote_desc);
2264 /* If there is something sitting in the buffer we might take it as a
2265 response to a command, which would be bad. */
2266 serial_flush_input (remote_desc);
2270 puts_filtered ("Remote debugging using ");
2271 puts_filtered (name);
2272 puts_filtered ("\n");
2275 push_target (target); /* Switch to using remote target now */
2277 init_all_packet_configs ();
2279 general_thread = -2;
2280 continue_thread = -2;
2282 /* Probe for ability to use "ThreadInfo" query, as required. */
2283 use_threadinfo_query = 1;
2284 use_threadextra_query = 1;
2286 /* Without this, some commands which require an active target (such
2287 as kill) won't work. This variable serves (at least) double duty
2288 as both the pid of the target process (if it has such), and as a
2289 flag indicating that a target is active. These functions should
2290 be split out into seperate variables, especially since GDB will
2291 someday have a notion of debugging several processes. */
2292 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2294 /* With this target we start out by owning the terminal. */
2295 remote_async_terminal_ours_p = 1;
2297 /* FIXME: cagney/1999-09-23: During the initial connection it is
2298 assumed that the target is already ready and able to respond to
2299 requests. Unfortunately remote_start_remote() eventually calls
2300 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2301 around this. Eventually a mechanism that allows
2302 wait_for_inferior() to expect/get timeouts will be
2304 wait_forever_enabled_p = 0;
2306 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2307 /* First delete any symbols previously loaded from shared libraries. */
2308 no_shared_libraries (NULL, 0);
2311 /* Start the remote connection; if error (0), discard this target.
2312 In particular, if the user quits, be sure to discard it
2313 (we'd be in an inconsistent state otherwise). */
2314 if (!catch_errors (remote_start_remote, NULL,
2315 "Couldn't establish connection to remote target\n",
2319 wait_forever_enabled_p = 1;
2323 wait_forever_enabled_p = 1;
2327 /* Tell the remote that we are using the extended protocol. */
2328 char *buf = alloca (PBUFSIZ);
2330 getpkt (buf, PBUFSIZ, 0);
2332 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2333 /* FIXME: need a master target_open vector from which all
2334 remote_opens can be called, so that stuff like this can
2335 go there. Failing that, the following code must be copied
2336 to the open function for any remote target that wants to
2337 support svr4 shared libraries. */
2339 /* Set up to detect and load shared libraries. */
2340 if (exec_bfd) /* No use without an exec file. */
2342 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2343 remote_check_symbols (symfile_objfile);
2348 /* This takes a program previously attached to and detaches it. After
2349 this is done, GDB can be used to debug some other program. We
2350 better not have left any breakpoints in the target program or it'll
2351 die when it hits one. */
2354 remote_detach (char *args, int from_tty)
2356 char *buf = alloca (PBUFSIZ);
2359 error ("Argument given to \"detach\" when remotely debugging.");
2361 /* Tell the remote target to detach. */
2363 remote_send (buf, PBUFSIZ);
2365 target_mourn_inferior ();
2367 puts_filtered ("Ending remote debugging.\n");
2371 /* Same as remote_detach, but with async support. */
2373 remote_async_detach (char *args, int from_tty)
2375 char *buf = alloca (PBUFSIZ);
2378 error ("Argument given to \"detach\" when remotely debugging.");
2380 /* Tell the remote target to detach. */
2382 remote_send (buf, PBUFSIZ);
2384 /* Unregister the file descriptor from the event loop. */
2385 if (target_is_async_p ())
2386 serial_async (remote_desc, NULL, 0);
2388 target_mourn_inferior ();
2390 puts_filtered ("Ending remote debugging.\n");
2393 /* Convert hex digit A to a number. */
2398 if (a >= '0' && a <= '9')
2400 else if (a >= 'a' && a <= 'f')
2401 return a - 'a' + 10;
2402 else if (a >= 'A' && a <= 'F')
2403 return a - 'A' + 10;
2405 error ("Reply contains invalid hex digit %d", a);
2409 hex2bin (const char *hex, char *bin, int count)
2413 for (i = 0; i < count; i++)
2415 if (hex[0] == 0 || hex[1] == 0)
2417 /* Hex string is short, or of uneven length.
2418 Return the count that has been converted so far. */
2421 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2427 /* Convert number NIB to a hex digit. */
2435 return 'a' + nib - 10;
2439 bin2hex (const char *bin, char *hex, int count)
2442 /* May use a length, or a nul-terminated string as input. */
2444 count = strlen (bin);
2446 for (i = 0; i < count; i++)
2448 *hex++ = tohex ((*bin >> 4) & 0xf);
2449 *hex++ = tohex (*bin++ & 0xf);
2455 /* Tell the remote machine to resume. */
2457 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2459 static int last_sent_step;
2462 remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
2464 char *buf = alloca (PBUFSIZ);
2465 int pid = PIDGET (ptid);
2469 set_thread (0, 0); /* run any thread */
2471 set_thread (pid, 0); /* run this thread */
2473 last_sent_signal = siggnal;
2474 last_sent_step = step;
2476 /* A hook for when we need to do something at the last moment before
2478 if (target_resume_hook)
2479 (*target_resume_hook) ();
2482 /* The s/S/c/C packets do not return status. So if the target does
2483 not support the S or C packets, the debug agent returns an empty
2484 string which is detected in remote_wait(). This protocol defect
2485 is fixed in the e/E packets. */
2487 if (step && step_range_end)
2489 /* If the target does not support the 'E' packet, we try the 'S'
2490 packet. Ideally we would fall back to the 'e' packet if that
2491 too is not supported. But that would require another copy of
2492 the code to issue the 'e' packet (and fall back to 's' if not
2493 supported) in remote_wait(). */
2495 if (siggnal != TARGET_SIGNAL_0)
2497 if (remote_protocol_E.support != PACKET_DISABLE)
2501 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2502 *p++ = tohex (((int) siggnal) & 0xf);
2504 p += hexnumstr (p, (ULONGEST) step_range_start);
2506 p += hexnumstr (p, (ULONGEST) step_range_end);
2510 getpkt (buf, PBUFSIZ, 0);
2512 if (packet_ok (buf, &remote_protocol_E) == PACKET_OK)
2518 if (remote_protocol_e.support != PACKET_DISABLE)
2522 p += hexnumstr (p, (ULONGEST) step_range_start);
2524 p += hexnumstr (p, (ULONGEST) step_range_end);
2528 getpkt (buf, PBUFSIZ, 0);
2530 if (packet_ok (buf, &remote_protocol_e) == PACKET_OK)
2536 if (siggnal != TARGET_SIGNAL_0)
2538 buf[0] = step ? 'S' : 'C';
2539 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2540 buf[2] = tohex (((int) siggnal) & 0xf);
2544 strcpy (buf, step ? "s" : "c");
2549 /* Same as remote_resume, but with async support. */
2551 remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2553 char *buf = alloca (PBUFSIZ);
2554 int pid = PIDGET (ptid);
2558 set_thread (0, 0); /* run any thread */
2560 set_thread (pid, 0); /* run this thread */
2562 last_sent_signal = siggnal;
2563 last_sent_step = step;
2565 /* A hook for when we need to do something at the last moment before
2567 if (target_resume_hook)
2568 (*target_resume_hook) ();
2570 /* The s/S/c/C packets do not return status. So if the target does
2571 not support the S or C packets, the debug agent returns an empty
2572 string which is detected in remote_wait(). This protocol defect
2573 is fixed in the e/E packets. */
2575 if (step && step_range_end)
2577 /* If the target does not support the 'E' packet, we try the 'S'
2578 packet. Ideally we would fall back to the 'e' packet if that
2579 too is not supported. But that would require another copy of
2580 the code to issue the 'e' packet (and fall back to 's' if not
2581 supported) in remote_wait(). */
2583 if (siggnal != TARGET_SIGNAL_0)
2585 if (remote_protocol_E.support != PACKET_DISABLE)
2589 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2590 *p++ = tohex (((int) siggnal) & 0xf);
2592 p += hexnumstr (p, (ULONGEST) step_range_start);
2594 p += hexnumstr (p, (ULONGEST) step_range_end);
2598 getpkt (buf, PBUFSIZ, 0);
2600 if (packet_ok (buf, &remote_protocol_E) == PACKET_OK)
2601 goto register_event_loop;
2606 if (remote_protocol_e.support != PACKET_DISABLE)
2610 p += hexnumstr (p, (ULONGEST) step_range_start);
2612 p += hexnumstr (p, (ULONGEST) step_range_end);
2616 getpkt (buf, PBUFSIZ, 0);
2618 if (packet_ok (buf, &remote_protocol_e) == PACKET_OK)
2619 goto register_event_loop;
2624 if (siggnal != TARGET_SIGNAL_0)
2626 buf[0] = step ? 'S' : 'C';
2627 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2628 buf[2] = tohex ((int) siggnal & 0xf);
2632 strcpy (buf, step ? "s" : "c");
2636 register_event_loop:
2637 /* We are about to start executing the inferior, let's register it
2638 with the event loop. NOTE: this is the one place where all the
2639 execution commands end up. We could alternatively do this in each
2640 of the execution commands in infcmd.c.*/
2641 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2642 into infcmd.c in order to allow inferior function calls to work
2643 NOT asynchronously. */
2644 if (event_loop_p && target_can_async_p ())
2645 target_async (inferior_event_handler, 0);
2646 /* Tell the world that the target is now executing. */
2647 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2648 this? Instead, should the client of target just assume (for
2649 async targets) that the target is going to start executing? Is
2650 this information already found in the continuation block? */
2651 if (target_is_async_p ())
2652 target_executing = 1;
2656 /* Set up the signal handler for SIGINT, while the target is
2657 executing, ovewriting the 'regular' SIGINT signal handler. */
2659 initialize_sigint_signal_handler (void)
2661 sigint_remote_token =
2662 create_async_signal_handler (async_remote_interrupt, NULL);
2663 signal (SIGINT, handle_remote_sigint);
2666 /* Signal handler for SIGINT, while the target is executing. */
2668 handle_remote_sigint (int sig)
2670 signal (sig, handle_remote_sigint_twice);
2671 sigint_remote_twice_token =
2672 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2673 mark_async_signal_handler_wrapper (sigint_remote_token);
2676 /* Signal handler for SIGINT, installed after SIGINT has already been
2677 sent once. It will take effect the second time that the user sends
2680 handle_remote_sigint_twice (int sig)
2682 signal (sig, handle_sigint);
2683 sigint_remote_twice_token =
2684 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2685 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2688 /* Perform the real interruption of the target execution, in response
2691 async_remote_interrupt (gdb_client_data arg)
2694 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2699 /* Perform interrupt, if the first attempt did not succeed. Just give
2700 up on the target alltogether. */
2702 async_remote_interrupt_twice (gdb_client_data arg)
2705 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2706 /* Do something only if the target was not killed by the previous
2708 if (target_executing)
2711 signal (SIGINT, handle_remote_sigint);
2715 /* Reinstall the usual SIGINT handlers, after the target has
2718 cleanup_sigint_signal_handler (void *dummy)
2720 signal (SIGINT, handle_sigint);
2721 if (sigint_remote_twice_token)
2722 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
2723 if (sigint_remote_token)
2724 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
2727 /* Send ^C to target to halt it. Target will respond, and send us a
2729 static void (*ofunc) (int);
2731 /* The command line interface's stop routine. This function is installed
2732 as a signal handler for SIGINT. The first time a user requests a
2733 stop, we call remote_stop to send a break or ^C. If there is no
2734 response from the target (it didn't stop when the user requested it),
2735 we ask the user if he'd like to detach from the target. */
2737 remote_interrupt (int signo)
2739 /* If this doesn't work, try more severe steps. */
2740 signal (signo, remote_interrupt_twice);
2743 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2748 /* The user typed ^C twice. */
2751 remote_interrupt_twice (int signo)
2753 signal (signo, ofunc);
2755 signal (signo, remote_interrupt);
2758 /* This is the generic stop called via the target vector. When a target
2759 interrupt is requested, either by the command line or the GUI, we
2760 will eventually end up here. */
2764 /* Send a break or a ^C, depending on user preference. */
2766 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2769 serial_send_break (remote_desc);
2771 serial_write (remote_desc, "\003", 1);
2774 /* Ask the user what to do when an interrupt is received. */
2777 interrupt_query (void)
2779 target_terminal_ours ();
2781 if (query ("Interrupted while waiting for the program.\n\
2782 Give up (and stop debugging it)? "))
2784 target_mourn_inferior ();
2785 return_to_top_level (RETURN_QUIT);
2788 target_terminal_inferior ();
2791 /* Enable/disable target terminal ownership. Most targets can use
2792 terminal groups to control terminal ownership. Remote targets are
2793 different in that explicit transfer of ownership to/from GDB/target
2797 remote_async_terminal_inferior (void)
2799 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2800 sync_execution here. This function should only be called when
2801 GDB is resuming the inferior in the forground. A background
2802 resume (``run&'') should leave GDB in control of the terminal and
2803 consequently should not call this code. */
2804 if (!sync_execution)
2806 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2807 calls target_terminal_*() idenpotent. The event-loop GDB talking
2808 to an asynchronous target with a synchronous command calls this
2809 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2810 stops trying to transfer the terminal to the target when it
2811 shouldn't this guard can go away. */
2812 if (!remote_async_terminal_ours_p)
2814 delete_file_handler (input_fd);
2815 remote_async_terminal_ours_p = 0;
2816 initialize_sigint_signal_handler ();
2817 /* NOTE: At this point we could also register our selves as the
2818 recipient of all input. Any characters typed could then be
2819 passed on down to the target. */
2823 remote_async_terminal_ours (void)
2825 /* See FIXME in remote_async_terminal_inferior. */
2826 if (!sync_execution)
2828 /* See FIXME in remote_async_terminal_inferior. */
2829 if (remote_async_terminal_ours_p)
2831 cleanup_sigint_signal_handler (NULL);
2832 add_file_handler (input_fd, stdin_event_handler, 0);
2833 remote_async_terminal_ours_p = 1;
2836 /* If nonzero, ignore the next kill. */
2841 remote_console_output (char *msg)
2845 for (p = msg; p[0] && p[1]; p += 2)
2848 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2851 fputs_unfiltered (tb, gdb_stdtarg);
2853 gdb_flush (gdb_stdtarg);
2856 /* Wait until the remote machine stops, then return,
2857 storing status in STATUS just as `wait' would.
2858 Returns "pid", which in the case of a multi-threaded
2859 remote OS, is the thread-id. */
2862 remote_wait (ptid_t ptid, struct target_waitstatus *status)
2864 unsigned char *buf = alloca (PBUFSIZ);
2865 int thread_num = -1;
2867 status->kind = TARGET_WAITKIND_EXITED;
2868 status->value.integer = 0;
2874 ofunc = signal (SIGINT, remote_interrupt);
2875 getpkt (buf, PBUFSIZ, 1);
2876 signal (SIGINT, ofunc);
2878 /* This is a hook for when we need to do something (perhaps the
2879 collection of trace data) every time the target stops. */
2880 if (target_wait_loop_hook)
2881 (*target_wait_loop_hook) ();
2885 case 'E': /* Error of some sort */
2886 warning ("Remote failure reply: %s", buf);
2888 case 'T': /* Status with PC, SP, FP, ... */
2892 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
2894 /* Expedited reply, containing Signal, {regno, reg} repeat */
2895 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2897 n... = register number
2898 r... = register contents
2900 p = &buf[3]; /* after Txx */
2908 /* Read the register number */
2909 regno = strtol ((const char *) p, &p_temp, 16);
2910 p1 = (unsigned char *) p_temp;
2912 if (p1 == p) /* No register number present here */
2914 p1 = (unsigned char *) strchr ((const char *) p, ':');
2916 warning ("Malformed packet(a) (missing colon): %s\n\
2919 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2921 p_temp = unpack_varlen_hex (++p1, &thread_num);
2922 record_currthread (thread_num);
2923 p = (unsigned char *) p_temp;
2931 warning ("Malformed packet(b) (missing colon): %s\n\
2935 if (regno >= NUM_REGS)
2936 warning ("Remote sent bad register number %ld: %s\n\
2940 fieldsize = hex2bin (p, regs, REGISTER_RAW_SIZE (regno));
2942 if (fieldsize < REGISTER_RAW_SIZE (regno))
2943 warning ("Remote reply is too short: %s", buf);
2944 supply_register (regno, regs);
2949 warning ("Remote register badly formatted: %s", buf);
2950 warning (" here: %s", p);
2955 case 'S': /* Old style status, just signal only */
2956 status->kind = TARGET_WAITKIND_STOPPED;
2957 status->value.sig = (enum target_signal)
2958 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2962 /* Export Cisco kernel mode as a convenience variable
2963 (so that it can be used in the GDB prompt if desired). */
2965 if (cisco_kernel_mode == 1)
2966 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2967 value_from_string ("PDEBUG-"));
2968 cisco_kernel_mode = 0;
2969 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2970 record_currthread (thread_num);
2972 else if (buf[3] == 'k')
2974 /* Export Cisco kernel mode as a convenience variable
2975 (so that it can be used in the GDB prompt if desired). */
2977 if (cisco_kernel_mode == 1)
2978 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2979 value_from_string ("KDEBUG-"));
2980 cisco_kernel_mode = 1;
2983 case 'N': /* Cisco special: status and offsets */
2985 bfd_vma text_addr, data_addr, bss_addr;
2986 bfd_signed_vma text_off, data_off, bss_off;
2989 status->kind = TARGET_WAITKIND_STOPPED;
2990 status->value.sig = (enum target_signal)
2991 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2993 if (symfile_objfile == NULL)
2995 warning ("Relocation packet received with no symbol file. \
3000 /* Relocate object file. Buffer format is NAATT;DD;BB
3001 * where AA is the signal number, TT is the new text
3002 * address, DD * is the new data address, and BB is the
3003 * new bss address. */
3006 text_addr = strtoul (p, (char **) &p1, 16);
3007 if (p1 == p || *p1 != ';')
3008 warning ("Malformed relocation packet: Packet '%s'", buf);
3010 data_addr = strtoul (p, (char **) &p1, 16);
3011 if (p1 == p || *p1 != ';')
3012 warning ("Malformed relocation packet: Packet '%s'", buf);
3014 bss_addr = strtoul (p, (char **) &p1, 16);
3016 warning ("Malformed relocation packet: Packet '%s'", buf);
3018 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
3019 &text_off, &data_off, &bss_off)
3021 if (text_off != 0 || data_off != 0 || bss_off != 0)
3022 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
3026 case 'W': /* Target exited */
3028 /* The remote process exited. */
3029 status->kind = TARGET_WAITKIND_EXITED;
3030 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3034 status->kind = TARGET_WAITKIND_SIGNALLED;
3035 status->value.sig = (enum target_signal)
3036 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3040 case 'O': /* Console output */
3041 remote_console_output (buf + 1);
3044 if (last_sent_signal != TARGET_SIGNAL_0)
3046 /* Zero length reply means that we tried 'S' or 'C' and
3047 the remote system doesn't support it. */
3048 target_terminal_ours_for_output ();
3050 ("Can't send signals to this remote system. %s not sent.\n",
3051 target_signal_to_name (last_sent_signal));
3052 last_sent_signal = TARGET_SIGNAL_0;
3053 target_terminal_inferior ();
3055 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3056 putpkt ((char *) buf);
3059 /* else fallthrough */
3061 warning ("Invalid remote reply: %s", buf);
3066 if (thread_num != -1)
3068 return pid_to_ptid (thread_num);
3070 return inferior_ptid;
3073 /* Async version of remote_wait. */
3075 remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
3077 unsigned char *buf = alloca (PBUFSIZ);
3078 int thread_num = -1;
3080 status->kind = TARGET_WAITKIND_EXITED;
3081 status->value.integer = 0;
3087 if (!target_is_async_p ())
3088 ofunc = signal (SIGINT, remote_interrupt);
3089 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3090 _never_ wait for ever -> test on target_is_async_p().
3091 However, before we do that we need to ensure that the caller
3092 knows how to take the target into/out of async mode. */
3093 getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
3094 if (!target_is_async_p ())
3095 signal (SIGINT, ofunc);
3097 /* This is a hook for when we need to do something (perhaps the
3098 collection of trace data) every time the target stops. */
3099 if (target_wait_loop_hook)
3100 (*target_wait_loop_hook) ();
3104 case 'E': /* Error of some sort */
3105 warning ("Remote failure reply: %s", buf);
3107 case 'T': /* Status with PC, SP, FP, ... */
3111 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
3113 /* Expedited reply, containing Signal, {regno, reg} repeat */
3114 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3116 n... = register number
3117 r... = register contents
3119 p = &buf[3]; /* after Txx */
3127 /* Read the register number */
3128 regno = strtol ((const char *) p, &p_temp, 16);
3129 p1 = (unsigned char *) p_temp;
3131 if (p1 == p) /* No register number present here */
3133 p1 = (unsigned char *) strchr ((const char *) p, ':');
3135 warning ("Malformed packet(a) (missing colon): %s\n\
3138 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
3140 p_temp = unpack_varlen_hex (++p1, &thread_num);
3141 record_currthread (thread_num);
3142 p = (unsigned char *) p_temp;
3150 warning ("Malformed packet(b) (missing colon): %s\n\
3154 if (regno >= NUM_REGS)
3155 warning ("Remote sent bad register number %ld: %s\n\
3159 fieldsize = hex2bin (p, regs, REGISTER_RAW_SIZE (regno));
3161 if (fieldsize < REGISTER_RAW_SIZE (regno))
3162 warning ("Remote reply is too short: %s", buf);
3163 supply_register (regno, regs);
3168 warning ("Remote register badly formatted: %s", buf);
3169 warning (" here: %s", p);
3174 case 'S': /* Old style status, just signal only */
3175 status->kind = TARGET_WAITKIND_STOPPED;
3176 status->value.sig = (enum target_signal)
3177 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3181 /* Export Cisco kernel mode as a convenience variable
3182 (so that it can be used in the GDB prompt if desired). */
3184 if (cisco_kernel_mode == 1)
3185 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
3186 value_from_string ("PDEBUG-"));
3187 cisco_kernel_mode = 0;
3188 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3189 record_currthread (thread_num);
3191 else if (buf[3] == 'k')
3193 /* Export Cisco kernel mode as a convenience variable
3194 (so that it can be used in the GDB prompt if desired). */
3196 if (cisco_kernel_mode == 1)
3197 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
3198 value_from_string ("KDEBUG-"));
3199 cisco_kernel_mode = 1;
3202 case 'N': /* Cisco special: status and offsets */
3204 bfd_vma text_addr, data_addr, bss_addr;
3205 bfd_signed_vma text_off, data_off, bss_off;
3208 status->kind = TARGET_WAITKIND_STOPPED;
3209 status->value.sig = (enum target_signal)
3210 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3212 if (symfile_objfile == NULL)
3214 warning ("Relocation packet recieved with no symbol file. \
3219 /* Relocate object file. Buffer format is NAATT;DD;BB
3220 * where AA is the signal number, TT is the new text
3221 * address, DD * is the new data address, and BB is the
3222 * new bss address. */
3225 text_addr = strtoul (p, (char **) &p1, 16);
3226 if (p1 == p || *p1 != ';')
3227 warning ("Malformed relocation packet: Packet '%s'", buf);
3229 data_addr = strtoul (p, (char **) &p1, 16);
3230 if (p1 == p || *p1 != ';')
3231 warning ("Malformed relocation packet: Packet '%s'", buf);
3233 bss_addr = strtoul (p, (char **) &p1, 16);
3235 warning ("Malformed relocation packet: Packet '%s'", buf);
3237 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
3238 &text_off, &data_off, &bss_off)
3240 if (text_off != 0 || data_off != 0 || bss_off != 0)
3241 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
3245 case 'W': /* Target exited */
3247 /* The remote process exited. */
3248 status->kind = TARGET_WAITKIND_EXITED;
3249 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3253 status->kind = TARGET_WAITKIND_SIGNALLED;
3254 status->value.sig = (enum target_signal)
3255 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3259 case 'O': /* Console output */
3260 remote_console_output (buf + 1);
3261 /* Return immediately to the event loop. The event loop will
3262 still be waiting on the inferior afterwards. */
3263 status->kind = TARGET_WAITKIND_IGNORE;
3266 if (last_sent_signal != TARGET_SIGNAL_0)
3268 /* Zero length reply means that we tried 'S' or 'C' and
3269 the remote system doesn't support it. */
3270 target_terminal_ours_for_output ();
3272 ("Can't send signals to this remote system. %s not sent.\n",
3273 target_signal_to_name (last_sent_signal));
3274 last_sent_signal = TARGET_SIGNAL_0;
3275 target_terminal_inferior ();
3277 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3278 putpkt ((char *) buf);
3281 /* else fallthrough */
3283 warning ("Invalid remote reply: %s", buf);
3288 if (thread_num != -1)
3290 return pid_to_ptid (thread_num);
3292 return inferior_ptid;
3295 /* Number of bytes of registers this stub implements. */
3297 static int register_bytes_found;
3299 /* Read the remote registers into the block REGS. */
3300 /* Currently we just read all the registers, so we don't use regno. */
3304 remote_fetch_registers (int regno)
3306 char *buf = alloca (PBUFSIZ);
3309 char *regs = alloca (REGISTER_BYTES);
3311 set_thread (PIDGET (inferior_ptid), 1);
3314 remote_send (buf, PBUFSIZ);
3316 /* Save the size of the packet sent to us by the target. Its used
3317 as a heuristic when determining the max size of packets that the
3318 target can safely receive. */
3319 if (actual_register_packet_size == 0)
3320 actual_register_packet_size = strlen (buf);
3322 /* Unimplemented registers read as all bits zero. */
3323 memset (regs, 0, REGISTER_BYTES);
3325 /* We can get out of synch in various cases. If the first character
3326 in the buffer is not a hex character, assume that has happened
3327 and try to fetch another packet to read. */
3328 while ((buf[0] < '0' || buf[0] > '9')
3329 && (buf[0] < 'a' || buf[0] > 'f')
3330 && buf[0] != 'x') /* New: unavailable register value */
3333 fprintf_unfiltered (gdb_stdlog,
3334 "Bad register packet; fetching a new packet\n");
3335 getpkt (buf, PBUFSIZ, 0);
3338 /* Reply describes registers byte by byte, each byte encoded as two
3339 hex characters. Suck them all up, then supply them to the
3340 register cacheing/storage mechanism. */
3343 for (i = 0; i < REGISTER_BYTES; i++)
3349 warning ("Remote reply is of odd length: %s", buf);
3350 /* Don't change register_bytes_found in this case, and don't
3351 print a second warning. */
3354 if (p[0] == 'x' && p[1] == 'x')
3355 regs[i] = 0; /* 'x' */
3357 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3361 if (i != register_bytes_found)
3363 register_bytes_found = i;
3364 if (REGISTER_BYTES_OK_P ()
3365 && !REGISTER_BYTES_OK (i))
3366 warning ("Remote reply is too short: %s", buf);
3370 for (i = 0; i < NUM_REGS; i++)
3372 supply_register (i, ®s[REGISTER_BYTE (i)]);
3373 if (buf[REGISTER_BYTE (i) * 2] == 'x')
3374 set_register_cached (i, -1);
3378 /* Prepare to store registers. Since we may send them all (using a
3379 'G' request), we have to read out the ones we don't want to change
3383 remote_prepare_to_store (void)
3385 /* Make sure the entire registers array is valid. */
3386 switch (remote_protocol_P.support)
3388 case PACKET_DISABLE:
3389 case PACKET_SUPPORT_UNKNOWN:
3390 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
3397 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
3398 packet was not recognized. */
3401 store_register_using_P (int regno)
3403 /* Try storing a single register. */
3404 char *buf = alloca (PBUFSIZ);
3409 sprintf (buf, "P%x=", regno);
3410 p = buf + strlen (buf);
3411 regp = register_buffer (regno);
3412 bin2hex (regp, p, REGISTER_RAW_SIZE (regno));
3413 remote_send (buf, PBUFSIZ);
3415 return buf[0] != '\0';
3419 /* Store register REGNO, or all registers if REGNO == -1, from the contents
3420 of the register cache buffer. FIXME: ignores errors. */
3423 remote_store_registers (int regno)
3425 char *buf = alloca (PBUFSIZ);
3430 set_thread (PIDGET (inferior_ptid), 1);
3434 switch (remote_protocol_P.support)
3436 case PACKET_DISABLE:
3439 if (store_register_using_P (regno))
3442 error ("Protocol error: P packet not recognized by stub");
3443 case PACKET_SUPPORT_UNKNOWN:
3444 if (store_register_using_P (regno))
3446 /* The stub recognized the 'P' packet. Remember this. */
3447 remote_protocol_P.support = PACKET_ENABLE;
3452 /* The stub does not support the 'P' packet. Use 'G'
3453 instead, and don't try using 'P' in the future (it
3454 will just waste our time). */
3455 remote_protocol_P.support = PACKET_DISABLE;
3463 /* Command describes registers byte by byte,
3464 each byte encoded as two hex characters. */
3466 regs = register_buffer (-1);
3468 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3469 bin2hex (regs, p, register_bytes_found);
3470 remote_send (buf, PBUFSIZ);
3474 /* Return the number of hex digits in num. */
3477 hexnumlen (ULONGEST num)
3481 for (i = 0; num != 0; i++)
3487 /* Set BUF to the minimum number of hex digits representing NUM. */
3490 hexnumstr (char *buf, ULONGEST num)
3492 int len = hexnumlen (num);
3493 return hexnumnstr (buf, num, len);
3497 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3500 hexnumnstr (char *buf, ULONGEST num, int width)
3506 for (i = width - 1; i >= 0; i--)
3508 buf[i] = "0123456789abcdef"[(num & 0xf)];
3515 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3518 remote_address_masked (CORE_ADDR addr)
3520 if (remote_address_size > 0
3521 && remote_address_size < (sizeof (ULONGEST) * 8))
3523 /* Only create a mask when that mask can safely be constructed
3524 in a ULONGEST variable. */
3526 mask = (mask << remote_address_size) - 1;
3532 /* Determine whether the remote target supports binary downloading.
3533 This is accomplished by sending a no-op memory write of zero length
3534 to the target at the specified address. It does not suffice to send
3535 the whole packet, since many stubs strip the eighth bit and subsequently
3536 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3538 NOTE: This can still lose if the serial line is not eight-bit
3539 clean. In cases like this, the user should clear "remote
3543 check_binary_download (CORE_ADDR addr)
3545 switch (remote_protocol_binary_download.support)
3547 case PACKET_DISABLE:
3551 case PACKET_SUPPORT_UNKNOWN:
3553 char *buf = alloca (PBUFSIZ);
3558 p += hexnumstr (p, (ULONGEST) addr);
3560 p += hexnumstr (p, (ULONGEST) 0);
3564 putpkt_binary (buf, (int) (p - buf));
3565 getpkt (buf, PBUFSIZ, 0);
3570 fprintf_unfiltered (gdb_stdlog,
3571 "binary downloading NOT suppported by target\n");
3572 remote_protocol_binary_download.support = PACKET_DISABLE;
3577 fprintf_unfiltered (gdb_stdlog,
3578 "binary downloading suppported by target\n");
3579 remote_protocol_binary_download.support = PACKET_ENABLE;
3586 /* Write memory data directly to the remote machine.
3587 This does not inform the data cache; the data cache uses this.
3588 MEMADDR is the address in the remote memory space.
3589 MYADDR is the address of the buffer in our space.
3590 LEN is the number of bytes.
3592 Returns number of bytes transferred, or 0 (setting errno) for
3593 error. Only transfer a single packet. */
3596 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3599 int max_buf_size; /* Max size of packet output buffer */
3601 unsigned char *plen;
3607 /* Verify that the target can support a binary download */
3608 check_binary_download (memaddr);
3610 /* Determine the max packet size. */
3611 max_buf_size = get_memory_write_packet_size ();
3612 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3613 buf = alloca (sizeof_buf);
3615 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3616 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3618 /* construct "M"<memaddr>","<len>":" */
3619 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3622 /* Append [XM]. Compute a best guess of the number of bytes
3623 actually transfered. */
3624 switch (remote_protocol_binary_download.support)
3628 /* Best guess at number of bytes that will fit. */
3629 todo = min (len, max_buf_size);
3631 case PACKET_DISABLE:
3633 /* num bytes that will fit */
3634 todo = min (len, max_buf_size / 2);
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 /* Append <memaddr> */
3644 memaddr = remote_address_masked (memaddr);
3645 p += hexnumstr (p, (ULONGEST) memaddr);
3648 /* Append <len>. Retain the location/size of <len>. It may
3649 need to be adjusted once the packet body has been created. */
3651 plenlen = hexnumstr (p, (ULONGEST) todo);
3656 /* Append the packet body. */
3657 switch (remote_protocol_binary_download.support)
3660 /* Binary mode. Send target system values byte by byte, in
3661 increasing byte addresses. Only escape certain critical
3664 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3667 switch (myaddr[nr_bytes] & 0xff)
3672 /* These must be escaped */
3674 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3677 *p++ = myaddr[nr_bytes] & 0xff;
3681 if (nr_bytes < todo)
3683 /* Escape chars have filled up the buffer prematurely,
3684 and we have actually sent fewer bytes than planned.
3685 Fix-up the length field of the packet. Use the same
3686 number of characters as before. */
3688 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3689 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3692 case PACKET_DISABLE:
3693 /* Normal mode: Send target system values byte by byte, in
3694 increasing byte addresses. Each byte is encoded as a two hex
3696 nr_bytes = bin2hex (myaddr, p, todo);
3699 case PACKET_SUPPORT_UNKNOWN:
3700 internal_error (__FILE__, __LINE__,
3701 "remote_write_bytes: bad internal state");
3703 internal_error (__FILE__, __LINE__, "bad switch");
3706 putpkt_binary (buf, (int) (p - buf));
3707 getpkt (buf, sizeof_buf, 0);
3711 /* There is no correspondance between what the remote protocol
3712 uses for errors and errno codes. We would like a cleaner way
3713 of representing errors (big enough to include errno codes,
3714 bfd_error codes, and others). But for now just return EIO. */
3719 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3720 bytes than we'd planned. */
3724 /* Read memory data directly from the remote machine.
3725 This does not use the data cache; the data cache uses this.
3726 MEMADDR is the address in the remote memory space.
3727 MYADDR is the address of the buffer in our space.
3728 LEN is the number of bytes.
3730 Returns number of bytes transferred, or 0 for error. */
3732 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3733 remote targets) shouldn't attempt to read the entire buffer.
3734 Instead it should read a single packet worth of data and then
3735 return the byte size of that packet to the caller. The caller (its
3736 caller and its callers caller ;-) already contains code for
3737 handling partial reads. */
3740 remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3743 int max_buf_size; /* Max size of packet output buffer */
3747 /* Create a buffer big enough for this packet. */
3748 max_buf_size = get_memory_read_packet_size ();
3749 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3750 buf = alloca (sizeof_buf);
3759 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3761 /* construct "m"<memaddr>","<len>" */
3762 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3763 memaddr = remote_address_masked (memaddr);
3766 p += hexnumstr (p, (ULONGEST) memaddr);
3768 p += hexnumstr (p, (ULONGEST) todo);
3772 getpkt (buf, sizeof_buf, 0);
3776 /* There is no correspondance between what the remote protocol uses
3777 for errors and errno codes. We would like a cleaner way of
3778 representing errors (big enough to include errno codes, bfd_error
3779 codes, and others). But for now just return EIO. */
3784 /* Reply describes memory byte by byte,
3785 each byte encoded as two hex characters. */
3788 if ((i = hex2bin (p, myaddr, todo)) < todo)
3790 /* Reply is short. This means that we were able to read
3791 only part of what we wanted to. */
3792 return i + (origlen - len);
3801 /* Read or write LEN bytes from inferior memory at MEMADDR,
3802 transferring to or from debugger address BUFFER. Write to inferior if
3803 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3804 for error. TARGET is unused. */
3808 remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
3810 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
3811 struct target_ops *target)
3813 CORE_ADDR targ_addr;
3817 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3822 res = remote_write_bytes (targ_addr, buffer, targ_len);
3824 res = remote_read_bytes (targ_addr, buffer, targ_len);
3831 /* Enable after 4.12. */
3834 remote_search (int len, char *data, char *mask, CORE_ADDR startaddr,
3835 int increment, CORE_ADDR lorange, CORE_ADDR hirange,
3836 CORE_ADDR *addr_found, char *data_found)
3838 if (increment == -4 && len == 4)
3840 long mask_long, data_long;
3841 long data_found_long;
3842 CORE_ADDR addr_we_found;
3843 char *buf = alloca (PBUFSIZ);
3844 long returned_long[2];
3847 mask_long = extract_unsigned_integer (mask, len);
3848 data_long = extract_unsigned_integer (data, len);
3849 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3851 getpkt (buf, PBUFSIZ, 0);
3854 /* The stub doesn't support the 't' request. We might want to
3855 remember this fact, but on the other hand the stub could be
3856 switched on us. Maybe we should remember it only until
3857 the next "target remote". */
3858 generic_search (len, data, mask, startaddr, increment, lorange,
3859 hirange, addr_found, data_found);
3864 /* There is no correspondance between what the remote protocol uses
3865 for errors and errno codes. We would like a cleaner way of
3866 representing errors (big enough to include errno codes, bfd_error
3867 codes, and others). But for now just use EIO. */
3868 memory_error (EIO, startaddr);
3871 while (*p != '\0' && *p != ',')
3872 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3874 error ("Protocol error: short return for search");
3876 data_found_long = 0;
3877 while (*p != '\0' && *p != ',')
3878 data_found_long = (data_found_long << 4) + fromhex (*p++);
3879 /* Ignore anything after this comma, for future extensions. */
3881 if (addr_we_found < lorange || addr_we_found >= hirange)
3887 *addr_found = addr_we_found;
3888 *data_found = store_unsigned_integer (data_we_found, len);
3891 generic_search (len, data, mask, startaddr, increment, lorange,
3892 hirange, addr_found, data_found);
3897 remote_files_info (struct target_ops *ignore)
3899 puts_filtered ("Debugging a target over a serial line.\n");
3902 /* Stuff for dealing with the packets which are part of this protocol.
3903 See comment at top of file for details. */
3905 /* Read a single character from the remote end, masking it down to 7 bits. */
3908 readchar (int timeout)
3912 ch = serial_readchar (remote_desc, timeout);
3917 switch ((enum serial_rc) ch)
3920 target_mourn_inferior ();
3921 error ("Remote connection closed");
3924 perror_with_name ("Remote communication error");
3926 case SERIAL_TIMEOUT:
3932 /* Send the command in BUF to the remote machine, and read the reply
3933 into BUF. Report an error if we get an error reply. */
3936 remote_send (char *buf,
3940 getpkt (buf, sizeof_buf, 0);
3943 error ("Remote failure reply: %s", buf);
3946 /* Display a null-terminated packet on stdout, for debugging, using C
3950 print_packet (char *buf)
3952 puts_filtered ("\"");
3953 fputstr_filtered (buf, '"', gdb_stdout);
3954 puts_filtered ("\"");
3960 return putpkt_binary (buf, strlen (buf));
3963 /* Send a packet to the remote machine, with error checking. The data
3964 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3965 to account for the $, # and checksum, and for a possible /0 if we are
3966 debugging (remote_debug) and want to print the sent packet as a string */
3969 putpkt_binary (char *buf, int cnt)
3972 unsigned char csum = 0;
3973 char *buf2 = alloca (cnt + 6);
3974 long sizeof_junkbuf = PBUFSIZ;
3975 char *junkbuf = alloca (sizeof_junkbuf);
3981 /* Copy the packet into buffer BUF2, encapsulating it
3982 and giving it a checksum. */
3987 for (i = 0; i < cnt; i++)
3993 *p++ = tohex ((csum >> 4) & 0xf);
3994 *p++ = tohex (csum & 0xf);
3996 /* Send it over and over until we get a positive ack. */
4000 int started_error_output = 0;
4005 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
4006 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
4007 fprintf_unfiltered (gdb_stdlog, "...");
4008 gdb_flush (gdb_stdlog);
4010 if (serial_write (remote_desc, buf2, p - buf2))
4011 perror_with_name ("putpkt: write failed");
4013 /* read until either a timeout occurs (-2) or '+' is read */
4016 ch = readchar (remote_timeout);
4024 case SERIAL_TIMEOUT:
4026 if (started_error_output)
4028 putchar_unfiltered ('\n');
4029 started_error_output = 0;
4038 fprintf_unfiltered (gdb_stdlog, "Ack\n");
4042 fprintf_unfiltered (gdb_stdlog, "Nak\n");
4043 case SERIAL_TIMEOUT:
4047 break; /* Retransmit buffer */
4051 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
4052 /* It's probably an old response, and we're out of sync.
4053 Just gobble up the packet and ignore it. */
4054 read_frame (junkbuf, sizeof_junkbuf);
4055 continue; /* Now, go look for + */
4060 if (!started_error_output)
4062 started_error_output = 1;
4063 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
4065 fputc_unfiltered (ch & 0177, gdb_stdlog);
4069 break; /* Here to retransmit */
4073 /* This is wrong. If doing a long backtrace, the user should be
4074 able to get out next time we call QUIT, without anything as
4075 violent as interrupt_query. If we want to provide a way out of
4076 here without getting to the next QUIT, it should be based on
4077 hitting ^C twice as in remote_wait. */
4087 static int remote_cisco_mode;
4089 /* Come here after finding the start of the frame. Collect the rest
4090 into BUF, verifying the checksum, length, and handling run-length
4091 compression. No more than sizeof_buf-1 characters are read so that
4092 the buffer can be NUL terminated.
4094 Returns -1 on error, number of characters in buffer (ignoring the
4095 trailing NULL) on success. (could be extended to return one of the
4096 SERIAL status indications). */
4099 read_frame (char *buf,
4111 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
4112 c = readchar (remote_timeout);
4115 case SERIAL_TIMEOUT:
4117 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
4121 fputs_filtered ("Saw new packet start in middle of old one\n",
4123 return -1; /* Start a new packet, count retries */
4126 unsigned char pktcsum;
4132 check_0 = readchar (remote_timeout);
4134 check_1 = readchar (remote_timeout);
4136 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4139 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4142 else if (check_0 < 0 || check_1 < 0)
4145 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4149 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
4150 if (csum == pktcsum)
4155 fprintf_filtered (gdb_stdlog,
4156 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4158 fputs_filtered (buf, gdb_stdlog);
4159 fputs_filtered ("\n", gdb_stdlog);
4161 /* Number of characters in buffer ignoring trailing
4165 case '*': /* Run length encoding */
4170 if (remote_cisco_mode == 0)
4172 c = readchar (remote_timeout);
4174 repeat = c - ' ' + 3; /* Compute repeat count */
4178 /* Cisco's run-length encoding variant uses two
4179 hex chars to represent the repeat count. */
4181 c = readchar (remote_timeout);
4183 repeat = fromhex (c) << 4;
4184 c = readchar (remote_timeout);
4186 repeat += fromhex (c);
4189 /* The character before ``*'' is repeated. */
4191 if (repeat > 0 && repeat <= 255
4193 && bc + repeat - 1 < sizeof_buf - 1)
4195 memset (&buf[bc], buf[bc - 1], repeat);
4201 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4202 puts_filtered (buf);
4203 puts_filtered ("\n");
4207 if (bc < sizeof_buf - 1)
4215 puts_filtered ("Remote packet too long: ");
4216 puts_filtered (buf);
4217 puts_filtered ("\n");
4224 /* Read a packet from the remote machine, with error checking, and
4225 store it in BUF. If FOREVER, wait forever rather than timing out;
4226 this is used (in synchronous mode) to wait for a target that is is
4227 executing user code to stop. */
4228 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4229 don't have to change all the calls to getpkt to deal with the
4230 return value, because at the moment I don't know what the right
4231 thing to do it for those. */
4239 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4243 /* Read a packet from the remote machine, with error checking, and
4244 store it in BUF. If FOREVER, wait forever rather than timing out;
4245 this is used (in synchronous mode) to wait for a target that is is
4246 executing user code to stop. If FOREVER == 0, this function is
4247 allowed to time out gracefully and return an indication of this to
4250 getpkt_sane (char *buf,
4259 strcpy (buf, "timeout");
4263 timeout = watchdog > 0 ? watchdog : -1;
4267 timeout = remote_timeout;
4271 for (tries = 1; tries <= MAX_TRIES; tries++)
4273 /* This can loop forever if the remote side sends us characters
4274 continuously, but if it pauses, we'll get a zero from readchar
4275 because of timeout. Then we'll count that as a retry. */
4277 /* Note that we will only wait forever prior to the start of a packet.
4278 After that, we expect characters to arrive at a brisk pace. They
4279 should show up within remote_timeout intervals. */
4283 c = readchar (timeout);
4285 if (c == SERIAL_TIMEOUT)
4287 if (forever) /* Watchdog went off? Kill the target. */
4290 target_mourn_inferior ();
4291 error ("Watchdog has expired. Target detached.\n");
4294 fputs_filtered ("Timed out.\n", gdb_stdlog);
4300 /* We've found the start of a packet, now collect the data. */
4302 val = read_frame (buf, sizeof_buf);
4308 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4309 fputstr_unfiltered (buf, 0, gdb_stdlog);
4310 fprintf_unfiltered (gdb_stdlog, "\n");
4312 serial_write (remote_desc, "+", 1);
4316 /* Try the whole thing again. */
4318 serial_write (remote_desc, "-", 1);
4321 /* We have tried hard enough, and just can't receive the packet. Give up. */
4323 printf_unfiltered ("Ignoring packet error, continuing...\n");
4324 serial_write (remote_desc, "+", 1);
4331 /* For some mysterious reason, wait_for_inferior calls kill instead of
4332 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4336 target_mourn_inferior ();
4340 /* Use catch_errors so the user can quit from gdb even when we aren't on
4341 speaking terms with the remote system. */
4342 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4344 /* Don't wait for it to die. I'm not really sure it matters whether
4345 we do or not. For the existing stubs, kill is a noop. */
4346 target_mourn_inferior ();
4349 /* Async version of remote_kill. */
4351 remote_async_kill (void)
4353 /* Unregister the file descriptor from the event loop. */
4354 if (target_is_async_p ())
4355 serial_async (remote_desc, NULL, 0);
4357 /* For some mysterious reason, wait_for_inferior calls kill instead of
4358 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4362 target_mourn_inferior ();
4366 /* Use catch_errors so the user can quit from gdb even when we aren't on
4367 speaking terms with the remote system. */
4368 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4370 /* Don't wait for it to die. I'm not really sure it matters whether
4371 we do or not. For the existing stubs, kill is a noop. */
4372 target_mourn_inferior ();
4378 remote_mourn_1 (&remote_ops);
4382 remote_async_mourn (void)
4384 remote_mourn_1 (&remote_async_ops);
4388 extended_remote_mourn (void)
4390 /* We do _not_ want to mourn the target like this; this will
4391 remove the extended remote target from the target stack,
4392 and the next time the user says "run" it'll fail.
4394 FIXME: What is the right thing to do here? */
4396 remote_mourn_1 (&extended_remote_ops);
4400 /* Worker function for remote_mourn. */
4402 remote_mourn_1 (struct target_ops *target)
4404 unpush_target (target);
4405 generic_mourn_inferior ();
4408 /* In the extended protocol we want to be able to do things like
4409 "run" and have them basically work as expected. So we need
4410 a special create_inferior function.
4412 FIXME: One day add support for changing the exec file
4413 we're debugging, arguments and an environment. */
4416 extended_remote_create_inferior (char *exec_file, char *args, char **env)
4418 /* Rip out the breakpoints; we'll reinsert them after restarting
4419 the remote server. */
4420 remove_breakpoints ();
4422 /* Now restart the remote server. */
4423 extended_remote_restart ();
4425 /* Now put the breakpoints back in. This way we're safe if the
4426 restart function works via a unix fork on the remote side. */
4427 insert_breakpoints ();
4429 /* Clean up from the last time we were running. */
4430 clear_proceed_status ();
4432 /* Let the remote process run. */
4433 proceed (-1, TARGET_SIGNAL_0, 0);
4436 /* Async version of extended_remote_create_inferior. */
4438 extended_remote_async_create_inferior (char *exec_file, char *args, char **env)
4440 /* Rip out the breakpoints; we'll reinsert them after restarting
4441 the remote server. */
4442 remove_breakpoints ();
4444 /* If running asynchronously, register the target file descriptor
4445 with the event loop. */
4446 if (event_loop_p && target_can_async_p ())
4447 target_async (inferior_event_handler, 0);
4449 /* Now restart the remote server. */
4450 extended_remote_restart ();
4452 /* Now put the breakpoints back in. This way we're safe if the
4453 restart function works via a unix fork on the remote side. */
4454 insert_breakpoints ();
4456 /* Clean up from the last time we were running. */
4457 clear_proceed_status ();
4459 /* Let the remote process run. */
4460 proceed (-1, TARGET_SIGNAL_0, 0);
4464 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
4465 than other targets; in those use REMOTE_BREAKPOINT instead of just
4466 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4467 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4468 the standard routines that are in mem-break.c. */
4470 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
4471 the choice of breakpoint instruction affects target program design and
4472 vice versa, and by making it user-tweakable, the special code here
4473 goes away and we need fewer special GDB configurations. */
4475 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4476 #define REMOTE_BREAKPOINT
4479 #ifdef REMOTE_BREAKPOINT
4481 /* If the target isn't bi-endian, just pretend it is. */
4482 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4483 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4484 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4487 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4488 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4490 #endif /* REMOTE_BREAKPOINT */
4492 /* Insert a breakpoint on targets that don't have any better breakpoint
4493 support. We read the contents of the target location and stash it,
4494 then overwrite it with a breakpoint instruction. ADDR is the target
4495 location in the target machine. CONTENTS_CACHE is a pointer to
4496 memory allocated for saving the target contents. It is guaranteed
4497 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4498 is accomplished via BREAKPOINT_MAX). */
4501 remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
4503 #ifdef REMOTE_BREAKPOINT
4508 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4509 If it succeeds, then set the support to PACKET_ENABLE. If it
4510 fails, and the user has explicitly requested the Z support then
4511 report an error, otherwise, mark it disabled and go on. */
4513 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4515 char *buf = alloca (PBUFSIZ);
4518 addr = remote_address_masked (addr);
4522 p += hexnumstr (p, (ULONGEST) addr);
4523 BREAKPOINT_FROM_PC (&addr, &bp_size);
4524 sprintf (p, ",%d", bp_size);
4527 getpkt (buf, PBUFSIZ, 0);
4529 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
4535 case PACKET_UNKNOWN:
4540 #ifdef REMOTE_BREAKPOINT
4541 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4545 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4546 val = target_write_memory (addr, (char *) big_break_insn,
4547 sizeof big_break_insn);
4549 val = target_write_memory (addr, (char *) little_break_insn,
4550 sizeof little_break_insn);
4555 return memory_insert_breakpoint (addr, contents_cache);
4556 #endif /* REMOTE_BREAKPOINT */
4560 remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
4564 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4566 char *buf = alloca (PBUFSIZ);
4573 addr = remote_address_masked (addr);
4574 p += hexnumstr (p, (ULONGEST) addr);
4575 BREAKPOINT_FROM_PC (&addr, &bp_size);
4576 sprintf (p, ",%d", bp_size);
4579 getpkt (buf, PBUFSIZ, 0);
4581 return (buf[0] == 'E');
4584 #ifdef REMOTE_BREAKPOINT
4585 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4587 return memory_remove_breakpoint (addr, contents_cache);
4588 #endif /* REMOTE_BREAKPOINT */
4592 watchpoint_to_Z_packet (int type)
4606 internal_error (__FILE__, __LINE__,
4607 "hw_bp_to_z: bad watchpoint type %d", type);
4611 /* FIXME: This function should be static and a member of the remote
4615 remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
4617 char *buf = alloca (PBUFSIZ);
4619 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4621 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4622 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4623 remote_protocol_Z[packet].name,
4624 remote_protocol_Z[packet].title);
4626 sprintf (buf, "Z%x,", packet);
4627 p = strchr (buf, '\0');
4628 addr = remote_address_masked (addr);
4629 p += hexnumstr (p, (ULONGEST) addr);
4630 sprintf (p, ",%x", len);
4633 getpkt (buf, PBUFSIZ, 0);
4635 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4638 case PACKET_UNKNOWN:
4643 internal_error (__FILE__, __LINE__,
4644 "remote_insert_watchpoint: reached end of function");
4647 /* FIXME: This function should be static and a member of the remote
4651 remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
4653 char *buf = alloca (PBUFSIZ);
4655 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4657 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4658 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4659 remote_protocol_Z[packet].name,
4660 remote_protocol_Z[packet].title);
4662 sprintf (buf, "z%x,", packet);
4663 p = strchr (buf, '\0');
4664 addr = remote_address_masked (addr);
4665 p += hexnumstr (p, (ULONGEST) addr);
4666 sprintf (p, ",%x", len);
4668 getpkt (buf, PBUFSIZ, 0);
4670 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4673 case PACKET_UNKNOWN:
4678 internal_error (__FILE__, __LINE__,
4679 "remote_remove_watchpoint: reached end of function");
4682 /* FIXME: This function should be static and a member of the remote
4686 remote_insert_hw_breakpoint (CORE_ADDR addr, int len)
4688 char *buf = alloca (PBUFSIZ);
4691 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4692 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4693 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4694 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4700 addr = remote_address_masked (addr);
4701 p += hexnumstr (p, (ULONGEST) addr);
4702 sprintf (p, ",%x", len);
4705 getpkt (buf, PBUFSIZ, 0);
4707 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4710 case PACKET_UNKNOWN:
4715 internal_error (__FILE__, __LINE__,
4716 "remote_remove_watchpoint: reached end of function");
4719 /* FIXME: This function should be static and a member of the remote
4723 remote_remove_hw_breakpoint (CORE_ADDR addr, int len)
4725 char *buf = alloca (PBUFSIZ);
4728 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4729 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4730 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4731 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4737 addr = remote_address_masked (addr);
4738 p += hexnumstr (p, (ULONGEST) addr);
4739 sprintf (p, ",%x", len);
4742 getpkt (buf, PBUFSIZ, 0);
4744 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4747 case PACKET_UNKNOWN:
4752 internal_error (__FILE__, __LINE__,
4753 "remote_remove_watchpoint: reached end of function");
4756 /* Some targets are only capable of doing downloads, and afterwards
4757 they switch to the remote serial protocol. This function provides
4758 a clean way to get from the download target to the remote target.
4759 It's basically just a wrapper so that we don't have to expose any
4760 of the internal workings of remote.c.
4762 Prior to calling this routine, you should shutdown the current
4763 target code, else you will get the "A program is being debugged
4764 already..." message. Usually a call to pop_target() suffices. */
4767 push_remote_target (char *name, int from_tty)
4769 printf_filtered ("Switching to remote protocol\n");
4770 remote_open (name, from_tty);
4773 /* Other targets want to use the entire remote serial module but with
4774 certain remote_ops overridden. */
4777 open_remote_target (char *name, int from_tty, struct target_ops *target,
4780 printf_filtered ("Selecting the %sremote protocol\n",
4781 (extended_p ? "extended-" : ""));
4782 remote_open_1 (name, from_tty, target, extended_p);
4785 /* Table used by the crc32 function to calcuate the checksum. */
4787 static unsigned long crc32_table[256] =
4790 static unsigned long
4791 crc32 (unsigned char *buf, int len, unsigned int crc)
4793 if (!crc32_table[1])
4795 /* Initialize the CRC table and the decoding table. */
4799 for (i = 0; i < 256; i++)
4801 for (c = i << 24, j = 8; j > 0; --j)
4802 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4809 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4815 /* compare-sections command
4817 With no arguments, compares each loadable section in the exec bfd
4818 with the same memory range on the target, and reports mismatches.
4819 Useful for verifying the image on the target against the exec file.
4820 Depends on the target understanding the new "qCRC:" request. */
4822 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4823 target method (target verify memory) and generic version of the
4824 actual command. This will allow other high-level code (especially
4825 generic_load()) to make use of this target functionality. */
4828 compare_sections_command (char *args, int from_tty)
4831 unsigned long host_crc, target_crc;
4832 extern bfd *exec_bfd;
4833 struct cleanup *old_chain;
4836 const char *sectname;
4837 char *buf = alloca (PBUFSIZ);
4844 error ("command cannot be used without an exec file");
4845 if (!current_target.to_shortname ||
4846 strcmp (current_target.to_shortname, "remote") != 0)
4847 error ("command can only be used with remote target");
4849 for (s = exec_bfd->sections; s; s = s->next)
4851 if (!(s->flags & SEC_LOAD))
4852 continue; /* skip non-loadable section */
4854 size = bfd_get_section_size_before_reloc (s);
4856 continue; /* skip zero-length section */
4858 sectname = bfd_get_section_name (exec_bfd, s);
4859 if (args && strcmp (args, sectname) != 0)
4860 continue; /* not the section selected by user */
4862 matched = 1; /* do this section */
4864 /* FIXME: assumes lma can fit into long */
4865 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4868 /* be clever; compute the host_crc before waiting for target reply */
4869 sectdata = xmalloc (size);
4870 old_chain = make_cleanup (xfree, sectdata);
4871 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4872 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4874 getpkt (buf, PBUFSIZ, 0);
4876 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4877 sectname, lma, lma + size);
4879 error ("remote target does not support this operation");
4881 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4882 target_crc = target_crc * 16 + fromhex (*tmp);
4884 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4885 sectname, paddr (lma), paddr (lma + size));
4886 if (host_crc == target_crc)
4887 printf_filtered ("matched.\n");
4890 printf_filtered ("MIS-MATCHED!\n");
4894 do_cleanups (old_chain);
4897 warning ("One or more sections of the remote executable does not match\n\
4898 the loaded file\n");
4899 if (args && !matched)
4900 printf_filtered ("No loaded section named '%s'.\n", args);
4904 remote_query (int query_type, char *buf, char *outbuf, int *bufsiz)
4907 char *buf2 = alloca (PBUFSIZ);
4908 char *p2 = &buf2[0];
4911 error ("null pointer to remote bufer size specified");
4913 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4914 the caller know and return what the minimum size is */
4915 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4916 if (*bufsiz < PBUFSIZ)
4922 /* except for querying the minimum buffer size, target must be open */
4924 error ("remote query is only available after target open");
4926 /* we only take uppercase letters as query types, at least for now */
4927 if ((query_type < 'A') || (query_type > 'Z'))
4928 error ("invalid remote query type");
4931 error ("null remote query specified");
4934 error ("remote query requires a buffer to receive data");
4941 /* we used one buffer char for the remote protocol q command and another
4942 for the query type. As the remote protocol encapsulation uses 4 chars
4943 plus one extra in case we are debugging (remote_debug),
4944 we have PBUFZIZ - 7 left to pack the query string */
4946 while (buf[i] && (i < (PBUFSIZ - 8)))
4948 /* bad caller may have sent forbidden characters */
4949 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
4950 error ("illegal characters in query string");
4958 error ("query larger than available buffer");
4964 getpkt (outbuf, *bufsiz, 0);
4970 remote_rcmd (char *command,
4971 struct ui_file *outbuf)
4974 char *buf = alloca (PBUFSIZ);
4978 error ("remote rcmd is only available after target open");
4980 /* Send a NULL command across as an empty command */
4981 if (command == NULL)
4984 /* The query prefix */
4985 strcpy (buf, "qRcmd,");
4986 p = strchr (buf, '\0');
4988 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ)
4989 error ("\"monitor\" command ``%s'' is too long\n", command);
4991 /* Encode the actual command */
4992 bin2hex (command, p, 0);
4994 if (putpkt (buf) < 0)
4995 error ("Communication problem with target\n");
4997 /* get/display the response */
5000 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
5002 getpkt (buf, PBUFSIZ, 0);
5004 error ("Target does not support this command\n");
5005 if (buf[0] == 'O' && buf[1] != 'K')
5007 remote_console_output (buf + 1); /* 'O' message from stub */
5010 if (strcmp (buf, "OK") == 0)
5012 if (strlen (buf) == 3 && buf[0] == 'E'
5013 && isdigit (buf[1]) && isdigit (buf[2]))
5015 error ("Protocol error with Rcmd");
5017 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5019 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5020 fputc_unfiltered (c, outbuf);
5027 packet_command (char *args, int from_tty)
5029 char *buf = alloca (PBUFSIZ);
5032 error ("command can only be used with remote target");
5035 error ("remote-packet command requires packet text as argument");
5037 puts_filtered ("sending: ");
5038 print_packet (args);
5039 puts_filtered ("\n");
5042 getpkt (buf, PBUFSIZ, 0);
5043 puts_filtered ("received: ");
5045 puts_filtered ("\n");
5049 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
5051 static void display_thread_info (struct gdb_ext_thread_info *info);
5053 static void threadset_test_cmd (char *cmd, int tty);
5055 static void threadalive_test (char *cmd, int tty);
5057 static void threadlist_test_cmd (char *cmd, int tty);
5059 int get_and_display_threadinfo (threadref * ref);
5061 static void threadinfo_test_cmd (char *cmd, int tty);
5063 static int thread_display_step (threadref * ref, void *context);
5065 static void threadlist_update_test_cmd (char *cmd, int tty);
5067 static void init_remote_threadtests (void);
5069 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
5072 threadset_test_cmd (char *cmd, int tty)
5074 int sample_thread = SAMPLE_THREAD;
5076 printf_filtered ("Remote threadset test\n");
5077 set_thread (sample_thread, 1);
5082 threadalive_test (char *cmd, int tty)
5084 int sample_thread = SAMPLE_THREAD;
5086 if (remote_thread_alive (pid_to_ptid (sample_thread)))
5087 printf_filtered ("PASS: Thread alive test\n");
5089 printf_filtered ("FAIL: Thread alive test\n");
5092 void output_threadid (char *title, threadref * ref);
5095 output_threadid (char *title, threadref *ref)
5099 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5101 printf_filtered ("%s %s\n", title, (&hexid[0]));
5105 threadlist_test_cmd (char *cmd, int tty)
5108 threadref nextthread;
5109 int done, result_count;
5110 threadref threadlist[3];
5112 printf_filtered ("Remote Threadlist test\n");
5113 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5114 &result_count, &threadlist[0]))
5115 printf_filtered ("FAIL: threadlist test\n");
5118 threadref *scan = threadlist;
5119 threadref *limit = scan + result_count;
5121 while (scan < limit)
5122 output_threadid (" thread ", scan++);
5127 display_thread_info (struct gdb_ext_thread_info *info)
5129 output_threadid ("Threadid: ", &info->threadid);
5130 printf_filtered ("Name: %s\n ", info->shortname);
5131 printf_filtered ("State: %s\n", info->display);
5132 printf_filtered ("other: %s\n\n", info->more_display);
5136 get_and_display_threadinfo (threadref *ref)
5140 struct gdb_ext_thread_info threadinfo;
5142 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5143 | TAG_MOREDISPLAY | TAG_DISPLAY;
5144 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5145 display_thread_info (&threadinfo);
5150 threadinfo_test_cmd (char *cmd, int tty)
5152 int athread = SAMPLE_THREAD;
5156 int_to_threadref (&thread, athread);
5157 printf_filtered ("Remote Threadinfo test\n");
5158 if (!get_and_display_threadinfo (&thread))
5159 printf_filtered ("FAIL cannot get thread info\n");
5163 thread_display_step (threadref *ref, void *context)
5165 /* output_threadid(" threadstep ",ref); *//* simple test */
5166 return get_and_display_threadinfo (ref);
5170 threadlist_update_test_cmd (char *cmd, int tty)
5172 printf_filtered ("Remote Threadlist update test\n");
5173 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5177 init_remote_threadtests (void)
5179 add_com ("tlist", class_obscure, threadlist_test_cmd,
5180 "Fetch and print the remote list of thread identifiers, one pkt only");
5181 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5182 "Fetch and display info about one thread");
5183 add_com ("tset", class_obscure, threadset_test_cmd,
5184 "Test setting to a different thread");
5185 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5186 "Iterate through updating all remote thread info");
5187 add_com ("talive", class_obscure, threadalive_test,
5188 " Remote thread alive test ");
5193 /* Convert a thread ID to a string. Returns the string in a static
5197 remote_pid_to_str (ptid_t ptid)
5199 static char buf[30];
5201 sprintf (buf, "Thread %d", PIDGET (ptid));
5206 init_remote_ops (void)
5208 remote_ops.to_shortname = "remote";
5209 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
5211 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5212 Specify the serial device it is connected to\n\
5213 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5214 remote_ops.to_open = remote_open;
5215 remote_ops.to_close = remote_close;
5216 remote_ops.to_detach = remote_detach;
5217 remote_ops.to_resume = remote_resume;
5218 remote_ops.to_wait = remote_wait;
5219 remote_ops.to_fetch_registers = remote_fetch_registers;
5220 remote_ops.to_store_registers = remote_store_registers;
5221 remote_ops.to_prepare_to_store = remote_prepare_to_store;
5222 remote_ops.to_xfer_memory = remote_xfer_memory;
5223 remote_ops.to_files_info = remote_files_info;
5224 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5225 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5226 remote_ops.to_kill = remote_kill;
5227 remote_ops.to_load = generic_load;
5228 remote_ops.to_mourn_inferior = remote_mourn;
5229 remote_ops.to_thread_alive = remote_thread_alive;
5230 remote_ops.to_find_new_threads = remote_threads_info;
5231 remote_ops.to_pid_to_str = remote_pid_to_str;
5232 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5233 remote_ops.to_stop = remote_stop;
5234 remote_ops.to_query = remote_query;
5235 remote_ops.to_rcmd = remote_rcmd;
5236 remote_ops.to_stratum = process_stratum;
5237 remote_ops.to_has_all_memory = 1;
5238 remote_ops.to_has_memory = 1;
5239 remote_ops.to_has_stack = 1;
5240 remote_ops.to_has_registers = 1;
5241 remote_ops.to_has_execution = 1;
5242 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5243 remote_ops.to_magic = OPS_MAGIC;
5246 /* Set up the extended remote vector by making a copy of the standard
5247 remote vector and adding to it. */
5250 init_extended_remote_ops (void)
5252 extended_remote_ops = remote_ops;
5254 extended_remote_ops.to_shortname = "extended-remote";
5255 extended_remote_ops.to_longname =
5256 "Extended remote serial target in gdb-specific protocol";
5257 extended_remote_ops.to_doc =
5258 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5259 Specify the serial device it is connected to (e.g. /dev/ttya).",
5260 extended_remote_ops.to_open = extended_remote_open;
5261 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5262 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5266 * Command: info remote-process
5268 * This implements Cisco's version of the "info proc" command.
5270 * This query allows the target stub to return an arbitrary string
5271 * (or strings) giving arbitrary information about the target process.
5272 * This is optional; the target stub isn't required to implement it.
5274 * Syntax: qfProcessInfo request first string
5275 * qsProcessInfo request subsequent string
5276 * reply: 'O'<hex-encoded-string>
5277 * 'l' last reply (empty)
5281 remote_info_process (char *args, int from_tty)
5283 char *buf = alloca (PBUFSIZ);
5285 if (remote_desc == 0)
5286 error ("Command can only be used when connected to the remote target.");
5288 putpkt ("qfProcessInfo");
5289 getpkt (buf, PBUFSIZ, 0);
5291 return; /* Silently: target does not support this feature. */
5294 error ("info proc: target error.");
5296 while (buf[0] == 'O') /* Capitol-O packet */
5298 remote_console_output (&buf[1]);
5299 putpkt ("qsProcessInfo");
5300 getpkt (buf, PBUFSIZ, 0);
5309 remote_cisco_open (char *name, int from_tty)
5313 "To open a remote debug connection, you need to specify what \n\
5314 device is attached to the remote system (e.g. host:port).");
5316 /* See FIXME above */
5317 wait_forever_enabled_p = 1;
5319 target_preopen (from_tty);
5321 unpush_target (&remote_cisco_ops);
5323 remote_desc = serial_open (name);
5325 perror_with_name (name);
5328 * If a baud rate was specified on the gdb command line it will
5329 * be greater than the initial value of -1. If it is, use it otherwise
5333 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
5334 if (serial_setbaudrate (remote_desc, baud_rate))
5336 serial_close (remote_desc);
5337 perror_with_name (name);
5340 serial_raw (remote_desc);
5342 /* If there is something sitting in the buffer we might take it as a
5343 response to a command, which would be bad. */
5344 serial_flush_input (remote_desc);
5348 puts_filtered ("Remote debugging using ");
5349 puts_filtered (name);
5350 puts_filtered ("\n");
5353 remote_cisco_mode = 1;
5355 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5357 init_all_packet_configs ();
5359 general_thread = -2;
5360 continue_thread = -2;
5362 /* Probe for ability to use "ThreadInfo" query, as required. */
5363 use_threadinfo_query = 1;
5364 use_threadextra_query = 1;
5366 /* Without this, some commands which require an active target (such
5367 as kill) won't work. This variable serves (at least) double duty
5368 as both the pid of the target process (if it has such), and as a
5369 flag indicating that a target is active. These functions should
5370 be split out into seperate variables, especially since GDB will
5371 someday have a notion of debugging several processes. */
5372 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
5374 /* Start the remote connection; if error (0), discard this target. */
5376 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5377 "Couldn't establish connection to remote target\n",
5386 remote_cisco_close (int quitting)
5388 remote_cisco_mode = 0;
5389 remote_close (quitting);
5393 remote_cisco_mourn (void)
5395 remote_mourn_1 (&remote_cisco_ops);
5407 /* shared between readsocket() and readtty() */
5408 static char *tty_input;
5410 static int escape_count;
5411 static int echo_check;
5412 extern int quit_flag;
5419 /* Loop until the socket doesn't have any more data */
5421 while ((data = readchar (0)) >= 0)
5423 /* Check for the escape sequence */
5426 /* If this is the fourth escape, get out */
5427 if (++escape_count == 4)
5432 { /* This is a '|', but not the fourth in a row.
5433 Continue without echoing it. If it isn't actually
5434 one of four in a row, it'll be echoed later. */
5441 /* Ensure any pending '|'s are flushed. */
5443 for (; escape_count > 0; escape_count--)
5447 if (data == '\r') /* If this is a return character, */
5448 continue; /* - just supress it. */
5450 if (echo_check != -1) /* Check for echo of user input. */
5452 if (tty_input[echo_check] == data)
5454 echo_check++; /* Character matched user input: */
5455 continue; /* Continue without echoing it. */
5457 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5458 { /* End of the line (and of echo checking). */
5459 echo_check = -1; /* No more echo supression */
5460 continue; /* Continue without echoing. */
5463 { /* Failed check for echo of user input.
5464 We now have some suppressed output to flush! */
5467 for (j = 0; j < echo_check; j++)
5468 putchar (tty_input[j]);
5472 putchar (data); /* Default case: output the char. */
5475 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5476 return READ_MORE; /* Try to read some more */
5478 return FATAL_ERROR; /* Trouble, bail out */
5486 /* First, read a buffer full from the terminal */
5487 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
5488 if (tty_bytecount == -1)
5490 perror ("readtty: read failed");
5494 /* Remove a quoted newline. */
5495 if (tty_input[tty_bytecount - 1] == '\n' &&
5496 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5498 tty_input[--tty_bytecount] = 0; /* remove newline */
5499 tty_input[--tty_bytecount] = 0; /* remove backslash */
5502 /* Turn trailing newlines into returns */
5503 if (tty_input[tty_bytecount - 1] == '\n')
5504 tty_input[tty_bytecount - 1] = '\r';
5506 /* If the line consists of a ~, enter debugging mode. */
5507 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5510 /* Make this a zero terminated string and write it out */
5511 tty_input[tty_bytecount] = 0;
5512 if (serial_write (remote_desc, tty_input, tty_bytecount))
5514 perror_with_name ("readtty: write failed");
5524 fd_set input; /* file descriptors for select */
5525 int tablesize; /* max number of FDs for select */
5529 extern int escape_count; /* global shared by readsocket */
5530 extern int echo_check; /* ditto */
5535 tablesize = 8 * sizeof (input);
5539 /* Check for anything from our socket - doesn't block. Note that
5540 this must be done *before* the select as there may be
5541 buffered I/O waiting to be processed. */
5543 if ((status = readsocket ()) == FATAL_ERROR)
5545 error ("Debugging terminated by communications error");
5547 else if (status != READ_MORE)
5552 fflush (stdout); /* Flush output before blocking */
5554 /* Now block on more socket input or TTY input */
5557 FD_SET (fileno (stdin), &input);
5558 FD_SET (deprecated_serial_fd (remote_desc), &input);
5560 status = select (tablesize, &input, 0, 0, 0);
5561 if ((status == -1) && (errno != EINTR))
5563 error ("Communications error on select %d", errno);
5566 /* Handle Control-C typed */
5570 if ((++quit_count) == 2)
5572 if (query ("Interrupt GDB? "))
5574 printf_filtered ("Interrupted by user.\n");
5575 return_to_top_level (RETURN_QUIT);
5582 serial_send_break (remote_desc);
5584 serial_write (remote_desc, "\003", 1);
5589 /* Handle console input */
5591 if (FD_ISSET (fileno (stdin), &input))
5595 status = readtty ();
5596 if (status == READ_MORE)
5599 return status; /* telnet session ended */
5605 remote_cisco_wait (ptid_t ptid, struct target_waitstatus *status)
5607 if (minitelnet () != ENTER_DEBUG)
5609 error ("Debugging session terminated by protocol error");
5612 return remote_wait (ptid, status);
5616 init_remote_cisco_ops (void)
5618 remote_cisco_ops.to_shortname = "cisco";
5619 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5620 remote_cisco_ops.to_doc =
5621 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5622 Specify the serial device it is connected to (e.g. host:2020).";
5623 remote_cisco_ops.to_open = remote_cisco_open;
5624 remote_cisco_ops.to_close = remote_cisco_close;
5625 remote_cisco_ops.to_detach = remote_detach;
5626 remote_cisco_ops.to_resume = remote_resume;
5627 remote_cisco_ops.to_wait = remote_cisco_wait;
5628 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5629 remote_cisco_ops.to_store_registers = remote_store_registers;
5630 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5631 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5632 remote_cisco_ops.to_files_info = remote_files_info;
5633 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5634 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
5635 remote_cisco_ops.to_kill = remote_kill;
5636 remote_cisco_ops.to_load = generic_load;
5637 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5638 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5639 remote_cisco_ops.to_find_new_threads = remote_threads_info;
5640 remote_cisco_ops.to_pid_to_str = remote_pid_to_str;
5641 remote_cisco_ops.to_extra_thread_info = remote_threads_extra_info;
5642 remote_cisco_ops.to_stratum = process_stratum;
5643 remote_cisco_ops.to_has_all_memory = 1;
5644 remote_cisco_ops.to_has_memory = 1;
5645 remote_cisco_ops.to_has_stack = 1;
5646 remote_cisco_ops.to_has_registers = 1;
5647 remote_cisco_ops.to_has_execution = 1;
5648 remote_cisco_ops.to_magic = OPS_MAGIC;
5652 remote_can_async_p (void)
5654 /* We're async whenever the serial device is. */
5655 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
5659 remote_is_async_p (void)
5661 /* We're async whenever the serial device is. */
5662 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
5665 /* Pass the SERIAL event on and up to the client. One day this code
5666 will be able to delay notifying the client of an event until the
5667 point where an entire packet has been received. */
5669 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5670 static void *async_client_context;
5671 static serial_event_ftype remote_async_serial_handler;
5674 remote_async_serial_handler (struct serial *scb, void *context)
5676 /* Don't propogate error information up to the client. Instead let
5677 the client find out about the error by querying the target. */
5678 async_client_callback (INF_REG_EVENT, async_client_context);
5682 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5684 if (current_target.to_async_mask_value == 0)
5685 internal_error (__FILE__, __LINE__,
5686 "Calling remote_async when async is masked");
5688 if (callback != NULL)
5690 serial_async (remote_desc, remote_async_serial_handler, NULL);
5691 async_client_callback = callback;
5692 async_client_context = context;
5695 serial_async (remote_desc, NULL, NULL);
5698 /* Target async and target extended-async.
5700 This are temporary targets, until it is all tested. Eventually
5701 async support will be incorporated int the usual 'remote'
5705 init_remote_async_ops (void)
5707 remote_async_ops.to_shortname = "async";
5708 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5709 remote_async_ops.to_doc =
5710 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5711 Specify the serial device it is connected to (e.g. /dev/ttya).";
5712 remote_async_ops.to_open = remote_async_open;
5713 remote_async_ops.to_close = remote_close;
5714 remote_async_ops.to_detach = remote_async_detach;
5715 remote_async_ops.to_resume = remote_async_resume;
5716 remote_async_ops.to_wait = remote_async_wait;
5717 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5718 remote_async_ops.to_store_registers = remote_store_registers;
5719 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5720 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5721 remote_async_ops.to_files_info = remote_files_info;
5722 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5723 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5724 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5725 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5726 remote_async_ops.to_kill = remote_async_kill;
5727 remote_async_ops.to_load = generic_load;
5728 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5729 remote_async_ops.to_thread_alive = remote_thread_alive;
5730 remote_async_ops.to_find_new_threads = remote_threads_info;
5731 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5732 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
5733 remote_async_ops.to_stop = remote_stop;
5734 remote_async_ops.to_query = remote_query;
5735 remote_async_ops.to_rcmd = remote_rcmd;
5736 remote_async_ops.to_stratum = process_stratum;
5737 remote_async_ops.to_has_all_memory = 1;
5738 remote_async_ops.to_has_memory = 1;
5739 remote_async_ops.to_has_stack = 1;
5740 remote_async_ops.to_has_registers = 1;
5741 remote_async_ops.to_has_execution = 1;
5742 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5743 remote_async_ops.to_can_async_p = remote_can_async_p;
5744 remote_async_ops.to_is_async_p = remote_is_async_p;
5745 remote_async_ops.to_async = remote_async;
5746 remote_async_ops.to_async_mask_value = 1;
5747 remote_async_ops.to_magic = OPS_MAGIC;
5750 /* Set up the async extended remote vector by making a copy of the standard
5751 remote vector and adding to it. */
5754 init_extended_async_remote_ops (void)
5756 extended_async_remote_ops = remote_async_ops;
5758 extended_async_remote_ops.to_shortname = "extended-async";
5759 extended_async_remote_ops.to_longname =
5760 "Extended remote serial target in async gdb-specific protocol";
5761 extended_async_remote_ops.to_doc =
5762 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5763 Specify the serial device it is connected to (e.g. /dev/ttya).",
5764 extended_async_remote_ops.to_open = extended_remote_async_open;
5765 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5766 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5770 set_remote_cmd (char *args, int from_tty)
5775 show_remote_cmd (char *args, int from_tty)
5778 show_remote_protocol_Z_packet_cmd (args, from_tty);
5779 show_remote_protocol_e_packet_cmd (args, from_tty);
5780 show_remote_protocol_E_packet_cmd (args, from_tty);
5781 show_remote_protocol_P_packet_cmd (args, from_tty);
5782 show_remote_protocol_qSymbol_packet_cmd (args, from_tty);
5783 show_remote_protocol_binary_download_cmd (args, from_tty);
5787 build_remote_gdbarch_data (void)
5789 build_remote_packet_sizes ();
5792 tty_input = xmalloc (PBUFSIZ);
5793 remote_address_size = TARGET_ADDR_BIT;
5796 /* Saved pointer to previous owner of the new_objfile event. */
5797 static void (*remote_new_objfile_chain) (struct objfile *);
5799 /* Function to be called whenever a new objfile (shlib) is detected. */
5801 remote_new_objfile (struct objfile *objfile)
5803 if (remote_desc != 0) /* Have a remote connection */
5805 remote_check_symbols (objfile);
5807 /* Call predecessor on chain, if any. */
5808 if (remote_new_objfile_chain != 0 &&
5810 remote_new_objfile_chain (objfile);
5814 _initialize_remote (void)
5816 static struct cmd_list_element *remote_set_cmdlist;
5817 static struct cmd_list_element *remote_show_cmdlist;
5818 struct cmd_list_element *tmpcmd;
5820 /* architecture specific data */
5821 build_remote_gdbarch_data ();
5822 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
5823 register_remote_packet_sizes ();
5824 register_gdbarch_swap (&remote_address_size,
5825 sizeof (&remote_address_size), NULL);
5826 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5829 add_target (&remote_ops);
5831 init_extended_remote_ops ();
5832 add_target (&extended_remote_ops);
5834 init_remote_async_ops ();
5835 add_target (&remote_async_ops);
5837 init_extended_async_remote_ops ();
5838 add_target (&extended_async_remote_ops);
5840 init_remote_cisco_ops ();
5841 add_target (&remote_cisco_ops);
5843 /* Hook into new objfile notification. */
5844 remote_new_objfile_chain = target_new_objfile_hook;
5845 target_new_objfile_hook = remote_new_objfile;
5848 init_remote_threadtests ();
5851 /* set/show remote ... */
5853 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5854 Remote protocol specific variables\n\
5855 Configure various remote-protocol specific variables such as\n\
5856 the packets being used",
5857 &remote_set_cmdlist, "set remote ",
5858 0/*allow-unknown*/, &setlist);
5859 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5860 Remote protocol specific variables\n\
5861 Configure various remote-protocol specific variables such as\n\
5862 the packets being used",
5863 &remote_show_cmdlist, "show remote ",
5864 0/*allow-unknown*/, &showlist);
5866 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5867 "Compare section data on target to the exec file.\n\
5868 Argument is a single section name (default: all loaded sections).",
5871 add_cmd ("packet", class_maintenance, packet_command,
5872 "Send an arbitrary packet to a remote target.\n\
5873 maintenance packet TEXT\n\
5874 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5875 this command sends the string TEXT to the inferior, and displays the\n\
5876 response packet. GDB supplies the initial `$' character, and the\n\
5877 terminating `#' character and checksum.",
5881 (add_set_boolean_cmd ("remotebreak", no_class, &remote_break,
5882 "Set whether to send break if interrupted.\n",
5886 /* Install commands for configuring memory read/write packets. */
5888 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5889 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5891 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
5892 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5894 add_cmd ("memory-write-packet-size", no_class,
5895 set_memory_write_packet_size,
5896 "Set the maximum number of bytes per memory-write packet.\n"
5897 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5898 "default packet size. The actual limit is further reduced\n"
5899 "dependent on the target. Specify ``fixed'' to disable the\n"
5900 "further restriction and ``limit'' to enable that restriction\n",
5901 &remote_set_cmdlist);
5902 add_cmd ("memory-read-packet-size", no_class,
5903 set_memory_read_packet_size,
5904 "Set the maximum number of bytes per memory-read packet.\n"
5905 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5906 "default packet size. The actual limit is further reduced\n"
5907 "dependent on the target. Specify ``fixed'' to disable the\n"
5908 "further restriction and ``limit'' to enable that restriction\n",
5909 &remote_set_cmdlist);
5910 add_cmd ("memory-write-packet-size", no_class,
5911 show_memory_write_packet_size,
5912 "Show the maximum number of bytes per memory-write packet.\n",
5913 &remote_show_cmdlist);
5914 add_cmd ("memory-read-packet-size", no_class,
5915 show_memory_read_packet_size,
5916 "Show the maximum number of bytes per memory-read packet.\n",
5917 &remote_show_cmdlist);
5920 (add_set_cmd ("remoteaddresssize", class_obscure,
5921 var_integer, (char *) &remote_address_size,
5922 "Set the maximum size of the address (in bits) \
5923 in a memory packet.\n",
5927 add_packet_config_cmd (&remote_protocol_binary_download,
5928 "X", "binary-download",
5929 set_remote_protocol_binary_download_cmd,
5930 show_remote_protocol_binary_download_cmd,
5931 &remote_set_cmdlist, &remote_show_cmdlist,
5934 /* XXXX - should ``set remotebinarydownload'' be retained for
5937 (add_set_cmd ("remotebinarydownload", no_class,
5938 var_boolean, (char *) &remote_binary_download,
5939 "Set binary downloads.\n", &setlist),
5943 add_info ("remote-process", remote_info_process,
5944 "Query the remote system for process info.");
5946 add_packet_config_cmd (&remote_protocol_qSymbol,
5947 "qSymbol", "symbol-lookup",
5948 set_remote_protocol_qSymbol_packet_cmd,
5949 show_remote_protocol_qSymbol_packet_cmd,
5950 &remote_set_cmdlist, &remote_show_cmdlist,
5953 add_packet_config_cmd (&remote_protocol_e,
5954 "e", "step-over-range",
5955 set_remote_protocol_e_packet_cmd,
5956 show_remote_protocol_e_packet_cmd,
5957 &remote_set_cmdlist, &remote_show_cmdlist,
5960 add_packet_config_cmd (&remote_protocol_E,
5961 "E", "step-over-range-w-signal",
5962 set_remote_protocol_E_packet_cmd,
5963 show_remote_protocol_E_packet_cmd,
5964 &remote_set_cmdlist, &remote_show_cmdlist,
5967 add_packet_config_cmd (&remote_protocol_P,
5968 "P", "set-register",
5969 set_remote_protocol_P_packet_cmd,
5970 show_remote_protocol_P_packet_cmd,
5971 &remote_set_cmdlist, &remote_show_cmdlist,
5974 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
5975 "Z0", "software-breakpoint",
5976 set_remote_protocol_Z_software_bp_packet_cmd,
5977 show_remote_protocol_Z_software_bp_packet_cmd,
5978 &remote_set_cmdlist, &remote_show_cmdlist,
5981 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
5982 "Z1", "hardware-breakpoint",
5983 set_remote_protocol_Z_hardware_bp_packet_cmd,
5984 show_remote_protocol_Z_hardware_bp_packet_cmd,
5985 &remote_set_cmdlist, &remote_show_cmdlist,
5988 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
5989 "Z2", "write-watchpoint",
5990 set_remote_protocol_Z_write_wp_packet_cmd,
5991 show_remote_protocol_Z_write_wp_packet_cmd,
5992 &remote_set_cmdlist, &remote_show_cmdlist,
5995 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
5996 "Z3", "read-watchpoint",
5997 set_remote_protocol_Z_read_wp_packet_cmd,
5998 show_remote_protocol_Z_read_wp_packet_cmd,
5999 &remote_set_cmdlist, &remote_show_cmdlist,
6002 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
6003 "Z4", "access-watchpoint",
6004 set_remote_protocol_Z_access_wp_packet_cmd,
6005 show_remote_protocol_Z_access_wp_packet_cmd,
6006 &remote_set_cmdlist, &remote_show_cmdlist,
6009 /* Keep the old ``set remote Z-packet ...'' working. */
6010 tmpcmd = add_set_auto_boolean_cmd ("Z-packet", class_obscure,
6011 &remote_Z_packet_detect,
6013 Set use of remote protocol `Z' packets", &remote_set_cmdlist);
6014 tmpcmd->function.sfunc = set_remote_protocol_Z_packet_cmd;
6015 add_cmd ("Z-packet", class_obscure, show_remote_protocol_Z_packet_cmd,
6016 "Show use of remote protocol `Z' packets ",
6017 &remote_show_cmdlist);