1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* See the GDB User Guide for details of the GDB remote protocol. */
24 #include "gdb_string.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
45 #include <sys/types.h>
48 #include "event-loop.h"
49 #include "event-top.h"
55 /* Prototypes for local functions */
56 static void cleanup_sigint_signal_handler (void *dummy);
57 static void initialize_sigint_signal_handler (void);
58 static int getpkt_sane (char *buf, long sizeof_buf, int forever);
60 static void handle_remote_sigint (int);
61 static void handle_remote_sigint_twice (int);
62 static void async_remote_interrupt (gdb_client_data);
63 void async_remote_interrupt_twice (gdb_client_data);
65 static void build_remote_gdbarch_data (void);
67 static int remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len);
69 static int remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len);
71 static void remote_files_info (struct target_ops *ignore);
73 static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
74 int len, int should_write,
75 struct target_ops *target);
77 static void remote_prepare_to_store (void);
79 static void remote_fetch_registers (int regno);
81 static void remote_resume (int pid, int step, enum target_signal siggnal);
82 static void remote_async_resume (int pid, int step,
83 enum target_signal siggnal);
84 static int remote_start_remote (PTR);
86 static void remote_open (char *name, int from_tty);
87 static void remote_async_open (char *name, int from_tty);
89 static void extended_remote_open (char *name, int from_tty);
90 static void extended_remote_async_open (char *name, int from_tty);
92 static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
93 static void remote_async_open_1 (char *, int, struct target_ops *,
96 static void remote_close (int quitting);
98 static void remote_store_registers (int regno);
100 static void remote_mourn (void);
101 static void remote_async_mourn (void);
103 static void extended_remote_restart (void);
105 static void extended_remote_mourn (void);
107 static void extended_remote_create_inferior (char *, char *, char **);
108 static void extended_remote_async_create_inferior (char *, char *, char **);
110 static void remote_mourn_1 (struct target_ops *);
112 static void remote_send (char *buf, long sizeof_buf);
114 static int readchar (int timeout);
116 static int remote_wait (int pid, struct target_waitstatus *status);
117 static int remote_async_wait (int pid, struct target_waitstatus *status);
119 static void remote_kill (void);
120 static void remote_async_kill (void);
122 static int tohex (int nib);
124 static void remote_detach (char *args, int from_tty);
125 static void remote_async_detach (char *args, int from_tty);
127 static void remote_interrupt (int signo);
129 static void remote_interrupt_twice (int signo);
131 static void interrupt_query (void);
133 static void set_thread (int, int);
135 static int remote_thread_alive (int);
137 static void get_offsets (void);
139 static long read_frame (char *buf, long sizeof_buf);
141 static int remote_insert_breakpoint (CORE_ADDR, char *);
143 static int remote_remove_breakpoint (CORE_ADDR, char *);
145 static int hexnumlen (ULONGEST num);
147 static void init_remote_ops (void);
149 static void init_extended_remote_ops (void);
151 static void init_remote_cisco_ops (void);
153 static struct target_ops remote_cisco_ops;
155 static void remote_stop (void);
157 static int ishex (int ch, int *val);
159 static int stubhex (int ch);
161 static int remote_query (int /*char */ , char *, char *, int *);
163 static int hexnumstr (char *, ULONGEST);
165 static int hexnumnstr (char *, ULONGEST, int);
167 static CORE_ADDR remote_address_masked (CORE_ADDR);
169 static void print_packet (char *);
171 static unsigned long crc32 (unsigned char *, int, unsigned int);
173 static void compare_sections_command (char *, int);
175 static void packet_command (char *, int);
177 static int stub_unpack_int (char *buff, int fieldlength);
179 static int remote_current_thread (int oldpid);
181 static void remote_find_new_threads (void);
183 static void record_currthread (int currthread);
185 /* exported functions */
187 extern int fromhex (int a);
189 static int putpkt_binary (char *buf, int cnt);
191 static void check_binary_download (CORE_ADDR addr);
193 struct packet_config;
195 static void show_packet_config_cmd (struct packet_config *config);
197 static void set_packet_config_cmd (struct packet_config *config,
198 struct cmd_list_element *c);
200 static void add_packet_config_cmd (struct packet_config *config,
203 void (*set_func) (char *args, int from_tty,
204 struct cmd_list_element *
206 void (*show_func) (char *name,
208 struct cmd_list_element **setlist,
209 struct cmd_list_element **showlist);
211 static void init_packet_config (struct packet_config *config);
213 static void set_remote_protocol_P_packet_cmd (char *args,
215 struct cmd_list_element *c);
217 static void show_remote_protocol_P_packet_cmd (char *args, int from_tty);
219 static void set_remote_protocol_Z_packet_cmd (char *args,
221 struct cmd_list_element *c);
223 static void show_remote_protocol_Z_packet_cmd (char *args, int from_tty);
228 /* Define the target subroutine names */
230 void open_remote_target (char *, int, struct target_ops *, int);
232 void _initialize_remote (void);
236 static struct target_ops remote_ops;
238 static struct target_ops extended_remote_ops;
240 /* Temporary target ops. Just like the remote_ops and
241 extended_remote_ops, but with asynchronous support. */
242 static struct target_ops remote_async_ops;
244 static struct target_ops extended_async_remote_ops;
246 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
247 ``forever'' still use the normal timeout mechanism. This is
248 currently used by the ASYNC code to guarentee that target reads
249 during the initial connect always time-out. Once getpkt has been
250 modified to return a timeout indication and, in turn
251 remote_wait()/wait_for_inferior() have gained a timeout parameter
253 static int wait_forever_enabled_p = 1;
256 /* This variable chooses whether to send a ^C or a break when the user
257 requests program interruption. Although ^C is usually what remote
258 systems expect, and that is the default here, sometimes a break is
259 preferable instead. */
261 static int remote_break;
263 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
264 remote_open knows that we don't have a file open when the program
266 static serial_t remote_desc = NULL;
268 /* This is set by the target (thru the 'S' message)
269 to denote that the target is in kernel mode. */
270 static int cisco_kernel_mode = 0;
272 /* This variable sets the number of bits in an address that are to be
273 sent in a memory ("M" or "m") packet. Normally, after stripping
274 leading zeros, the entire address would be sent. This variable
275 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
276 initial implementation of remote.c restricted the address sent in
277 memory packets to ``host::sizeof long'' bytes - (typically 32
278 bits). Consequently, for 64 bit targets, the upper 32 bits of an
279 address was never sent. Since fixing this bug may cause a break in
280 some remote targets this variable is principly provided to
281 facilitate backward compatibility. */
283 static int remote_address_size;
285 /* Tempoary to track who currently owns the terminal. See
286 target_async_terminal_* for more details. */
288 static int remote_async_terminal_ours_p;
291 /* This is the size (in chars) of the first response to the ``g''
292 packet. It is used as a heuristic when determining the maximum
293 size of memory-read and memory-write packets. A target will
294 typically only reserve a buffer large enough to hold the ``g''
295 packet. The size does not include packet overhead (headers and
298 static long actual_register_packet_size;
300 /* This is the maximum size (in chars) of a non read/write packet. It
301 is also used as a cap on the size of read/write packets. */
303 static long remote_packet_size;
305 #define PBUFSIZ (remote_packet_size)
307 /* User configurable variables for the number of characters in a
308 memory read/write packet. MIN (PBUFSIZ, g-packet-size) is the
309 default. Some targets need smaller values (fifo overruns, et.al.)
310 and some users need larger values (speed up transfers). The
311 variables ``preferred_*'' (the user request), ``current_*'' (what
312 was actually set) and ``forced_*'' (Positive - a soft limit,
313 negative - a hard limit). */
315 struct memory_packet_config
322 /* Compute the current size of a read/write packet. Since this makes
323 use of ``actual_register_packet_size'' the computation is dynamic. */
326 get_memory_packet_size (struct memory_packet_config *config)
328 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
329 law?) that some hosts don't cope very well with large alloca()
330 calls. Eventually the alloca() code will be replaced by calls to
331 xmalloc() and make_cleanups() allowing this restriction to either
332 be lifted or removed. */
333 #ifndef MAX_REMOTE_PACKET_SIZE
334 #define MAX_REMOTE_PACKET_SIZE 16384
336 /* NOTE: 16 is just chosen at random. */
337 #ifndef MIN_REMOTE_PACKET_SIZE
338 #define MIN_REMOTE_PACKET_SIZE 16
343 if (config->size <= 0)
344 what_they_get = MAX_REMOTE_PACKET_SIZE;
346 what_they_get = config->size;
350 what_they_get = remote_packet_size;
351 /* Limit the packet to the size specified by the user. */
353 && what_they_get > config->size)
354 what_they_get = config->size;
355 /* Limit it to the size of the targets ``g'' response. */
356 if (actual_register_packet_size > 0
357 && what_they_get > actual_register_packet_size)
358 what_they_get = actual_register_packet_size;
360 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
361 what_they_get = MAX_REMOTE_PACKET_SIZE;
362 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
363 what_they_get = MIN_REMOTE_PACKET_SIZE;
364 return what_they_get;
367 /* Update the size of a read/write packet. If they user wants
368 something really big then do a sanity check. */
371 set_memory_packet_size (char *args, struct memory_packet_config *config)
373 int fixed_p = config->fixed_p;
374 long size = config->size;
376 error ("Argument required (integer, `fixed' or `limited').");
377 else if (strcmp (args, "hard") == 0
378 || strcmp (args, "fixed") == 0)
380 else if (strcmp (args, "soft") == 0
381 || strcmp (args, "limit") == 0)
386 size = strtoul (args, &end, 0);
388 error ("Invalid %s (bad syntax).", config->name);
390 /* Instead of explicitly capping the size of a packet to
391 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
392 instead allowed to set the size to something arbitrarily
394 if (size > MAX_REMOTE_PACKET_SIZE)
395 error ("Invalid %s (too large).", config->name);
399 if (fixed_p && !config->fixed_p)
401 if (! query ("The target may not be able to correctly handle a %s\n"
402 "of %ld bytes. Change the packet size? ",
404 error ("Packet size not changed.");
406 /* Update the config. */
407 config->fixed_p = fixed_p;
412 show_memory_packet_size (struct memory_packet_config *config)
414 printf_filtered ("The %s is %ld. ", config->name, config->size);
416 printf_filtered ("Packets are fixed at %ld bytes.\n",
417 get_memory_packet_size (config));
419 printf_filtered ("Packets are limited to %ld bytes.\n",
420 get_memory_packet_size (config));
423 static struct memory_packet_config memory_write_packet_config =
425 "memory-write-packet-size",
429 set_memory_write_packet_size (char *args, int from_tty)
431 set_memory_packet_size (args, &memory_write_packet_config);
435 show_memory_write_packet_size (char *args, int from_tty)
437 show_memory_packet_size (&memory_write_packet_config);
441 get_memory_write_packet_size (void)
443 return get_memory_packet_size (&memory_write_packet_config);
446 static struct memory_packet_config memory_read_packet_config =
448 "memory-read-packet-size",
452 set_memory_read_packet_size (char *args, int from_tty)
454 set_memory_packet_size (args, &memory_read_packet_config);
458 show_memory_read_packet_size (char *args, int from_tty)
460 show_memory_packet_size (&memory_read_packet_config);
464 get_memory_read_packet_size (void)
466 long size = get_memory_packet_size (&memory_read_packet_config);
467 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
468 extra buffer size argument before the memory read size can be
469 increased beyond PBUFSIZ. */
475 /* Register packet size initialization. Since the bounds change when
476 the architecture changes (namely REGISTER_BYTES) this all needs to
480 register_remote_packet_sizes (void)
482 REGISTER_GDBARCH_SWAP (remote_packet_size);
483 REGISTER_GDBARCH_SWAP (actual_register_packet_size);
487 build_remote_packet_sizes (void)
489 /* Default maximum number of characters in a packet body. Many
490 remote stubs have a hardwired buffer size of 400 bytes
491 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
492 as the maximum packet-size to ensure that the packet and an extra
493 NUL character can always fit in the buffer. This stops GDB
494 trashing stubs that try to squeeze an extra NUL into what is
495 already a full buffer (As of 1999-12-04 that was most stubs. */
496 remote_packet_size = 400 - 1;
497 /* Should REGISTER_BYTES needs more space than the default, adjust
498 the size accordingly. Remember that each byte is encoded as two
499 characters. 32 is the overhead for the packet header /
500 footer. NOTE: cagney/1999-10-26: I suspect that 8
501 (``$NN:G...#NN'') is a better guess, the below has been padded a
503 if (REGISTER_BYTES > ((remote_packet_size - 32) / 2))
504 remote_packet_size = (REGISTER_BYTES * 2 + 32);
506 /* This one is filled in when a ``g'' packet is received. */
507 actual_register_packet_size = 0;
510 /* Generic configuration support for packets the stub optionally
511 supports. Allows the user to specify the use of the packet as well
512 as allowing GDB to auto-detect support in the remote stub. */
516 PACKET_SUPPORT_UNKNOWN = 0,
523 PACKET_AUTO_DETECT = 0,
532 enum packet_detect detect;
533 enum packet_support support;
536 static const char packet_support_auto[] = "auto";
537 static const char packet_enable[] = "enable";
538 static const char packet_disable[] = "disable";
539 static const char *packet_support_enums[] =
548 set_packet_config_cmd (config, c)
549 struct packet_config *config;
550 struct cmd_list_element *c;
552 if (config->state == packet_enable)
554 config->detect = PACKET_MANUAL_DETECT;
555 config->support = PACKET_ENABLE;
557 else if (config->state == packet_disable)
559 config->detect = PACKET_MANUAL_DETECT;
560 config->support = PACKET_DISABLE;
562 else if (config->state == packet_support_auto)
564 config->detect = PACKET_AUTO_DETECT;
565 config->support = PACKET_SUPPORT_UNKNOWN;
568 internal_error ("Bad enum value");
572 show_packet_config_cmd (config)
573 struct packet_config *config;
575 char *support = "internal-error";
576 switch (config->support)
582 support = "disabled";
584 case PACKET_SUPPORT_UNKNOWN:
588 switch (config->detect)
590 case PACKET_AUTO_DETECT:
591 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
592 config->name, config->title, support);
594 case PACKET_MANUAL_DETECT:
595 printf_filtered ("Support for remote protocol `%s' (%s) is currently %s.\n",
596 config->name, config->title, support);
601 add_packet_config_cmd (config, name, title, set_func, show_func,
603 struct packet_config *config;
606 void (*set_func) (char *args, int from_tty, struct cmd_list_element * c);
607 void (*show_func) (char *name, int from_tty);
608 struct cmd_list_element **setlist;
609 struct cmd_list_element **showlist;
611 struct cmd_list_element *c;
616 config->title = title;
617 asprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
619 asprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
621 asprintf (&full_name, "%s-packet", name);
622 c = add_set_enum_cmd (full_name,
623 class_obscure, packet_support_enums,
626 c->function.sfunc = set_func;
627 add_cmd (full_name, class_obscure, show_func, show_doc, showlist);
631 init_packet_config (config)
632 struct packet_config *config;
634 switch (config->detect)
636 case PACKET_AUTO_DETECT:
637 config->support = PACKET_SUPPORT_UNKNOWN;
639 case PACKET_MANUAL_DETECT:
640 /* let the user beware */
645 /* Should we try the 'P' (set register) request? */
647 static struct packet_config remote_protocol_P;
650 set_remote_protocol_P_packet_cmd (args, from_tty, c)
653 struct cmd_list_element *c;
655 set_packet_config_cmd (&remote_protocol_P, c);
659 show_remote_protocol_P_packet_cmd (args, from_tty)
663 show_packet_config_cmd (&remote_protocol_P);
666 /* Should we try the 'Z' (set breakpoint) request? */
668 static struct packet_config remote_protocol_Z;
671 set_remote_protocol_Z_packet_cmd (args, from_tty, c)
674 struct cmd_list_element *c;
676 set_packet_config_cmd (&remote_protocol_Z, c);
680 show_remote_protocol_Z_packet_cmd (args, from_tty)
684 show_packet_config_cmd (&remote_protocol_Z);
687 /* Should we try the 'X' (remote binary download) packet?
689 This variable (available to the user via "set remote X-packet")
690 dictates whether downloads are sent in binary (via the 'X' packet).
691 We assume that the stub can, and attempt to do it. This will be
692 cleared if the stub does not understand it. This switch is still
693 needed, though in cases when the packet is supported in the stub,
694 but the connection does not allow it (i.e., 7-bit serial connection
697 static struct packet_config remote_protocol_binary_download;
699 /* Should we try the 'ThreadInfo' query packet?
701 This variable (NOT available to the user: auto-detect only!)
702 determines whether GDB will use the new, simpler "ThreadInfo"
703 query or the older, more complex syntax for thread queries.
704 This is an auto-detect variable (set to true at each connect,
705 and set to false when the target fails to recognize it). */
707 static int use_threadinfo_query;
708 static int use_threadextra_query;
711 set_remote_protocol_binary_download_cmd (char *args,
713 struct cmd_list_element *c)
715 set_packet_config_cmd (&remote_protocol_binary_download, c);
719 show_remote_protocol_binary_download_cmd (char *args,
722 show_packet_config_cmd (&remote_protocol_binary_download);
726 /* Tokens for use by the asynchronous signal handlers for SIGINT */
727 PTR sigint_remote_twice_token;
728 PTR sigint_remote_token;
730 /* These are pointers to hook functions that may be set in order to
731 modify resume/wait behavior for a particular architecture. */
733 void (*target_resume_hook) (void);
734 void (*target_wait_loop_hook) (void);
738 /* These are the threads which we last sent to the remote system.
739 -1 for all or -2 for not sent yet. */
740 static int general_thread;
741 static int continue_thread;
743 /* Call this function as a result of
744 1) A halt indication (T packet) containing a thread id
745 2) A direct query of currthread
746 3) Successful execution of set thread
750 record_currthread (currthread)
753 general_thread = currthread;
755 /* If this is a new thread, add it to GDB's thread list.
756 If we leave it up to WFI to do this, bad things will happen. */
757 if (!in_thread_list (currthread))
759 add_thread (currthread);
761 ui_out_text (uiout, "[New ");
762 ui_out_text (uiout, target_pid_to_str (currthread));
763 ui_out_text (uiout, "]\n");
765 printf_filtered ("[New %s]\n", target_pid_to_str (currthread));
770 #define MAGIC_NULL_PID 42000
777 char *buf = alloca (PBUFSIZ);
778 int state = gen ? general_thread : continue_thread;
784 buf[1] = gen ? 'g' : 'c';
785 if (th == MAGIC_NULL_PID)
791 sprintf (&buf[2], "-%x", -th);
793 sprintf (&buf[2], "%x", th);
795 getpkt (buf, PBUFSIZ, 0);
799 continue_thread = th;
802 /* Return nonzero if the thread TH is still alive on the remote system. */
805 remote_thread_alive (tid)
811 sprintf (buf, "T-%08x", -tid);
813 sprintf (buf, "T%08x", tid);
815 getpkt (buf, sizeof (buf), 0);
816 return (buf[0] == 'O' && buf[1] == 'K');
819 /* About these extended threadlist and threadinfo packets. They are
820 variable length packets but, the fields within them are often fixed
821 length. They are redundent enough to send over UDP as is the
822 remote protocol in general. There is a matching unit test module
825 #define OPAQUETHREADBYTES 8
827 /* a 64 bit opaque identifier */
828 typedef unsigned char threadref[OPAQUETHREADBYTES];
830 /* WARNING: This threadref data structure comes from the remote O.S., libstub
831 protocol encoding, and remote.c. it is not particularly changable */
833 /* Right now, the internal structure is int. We want it to be bigger.
837 typedef int gdb_threadref; /* internal GDB thread reference */
839 /* gdb_ext_thread_info is an internal GDB data structure which is
840 equivalint to the reply of the remote threadinfo packet */
842 struct gdb_ext_thread_info
844 threadref threadid; /* External form of thread reference */
845 int active; /* Has state interesting to GDB? , regs, stack */
846 char display[256]; /* Brief state display, name, blocked/syspended */
847 char shortname[32]; /* To be used to name threads */
848 char more_display[256]; /* Long info, statistics, queue depth, whatever */
851 /* The volume of remote transfers can be limited by submitting
852 a mask containing bits specifying the desired information.
853 Use a union of these values as the 'selection' parameter to
854 get_thread_info. FIXME: Make these TAG names more thread specific.
857 #define TAG_THREADID 1
859 #define TAG_DISPLAY 4
860 #define TAG_THREADNAME 8
861 #define TAG_MOREDISPLAY 16
863 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
865 char *unpack_varlen_hex (char *buff, int *result);
867 static char *unpack_nibble (char *buf, int *val);
869 static char *pack_nibble (char *buf, int nibble);
871 static char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
873 static char *unpack_byte (char *buf, int *value);
875 static char *pack_int (char *buf, int value);
877 static char *unpack_int (char *buf, int *value);
879 static char *unpack_string (char *src, char *dest, int length);
881 static char *pack_threadid (char *pkt, threadref * id);
883 static char *unpack_threadid (char *inbuf, threadref * id);
885 void int_to_threadref (threadref * id, int value);
887 static int threadref_to_int (threadref * ref);
889 static void copy_threadref (threadref * dest, threadref * src);
891 static int threadmatch (threadref * dest, threadref * src);
893 static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
895 static int remote_unpack_thread_info_response (char *pkt,
896 threadref * expectedref,
897 struct gdb_ext_thread_info
901 static int remote_get_threadinfo (threadref * threadid, int fieldset, /*TAG mask */
902 struct gdb_ext_thread_info *info);
904 static int adapt_remote_get_threadinfo (gdb_threadref * ref,
906 struct gdb_ext_thread_info *info);
908 static char *pack_threadlist_request (char *pkt, int startflag,
910 threadref * nextthread);
912 static int parse_threadlist_response (char *pkt,
914 threadref * original_echo,
915 threadref * resultlist, int *doneflag);
917 static int remote_get_threadlist (int startflag,
918 threadref * nextthread,
921 int *result_count, threadref * threadlist);
923 typedef int (*rmt_thread_action) (threadref * ref, void *context);
925 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
926 void *context, int looplimit);
928 static int remote_newthread_step (threadref * ref, void *context);
930 /* encode 64 bits in 16 chars of hex */
932 static const char hexchars[] = "0123456789abcdef";
939 if ((ch >= 'a') && (ch <= 'f'))
941 *val = ch - 'a' + 10;
944 if ((ch >= 'A') && (ch <= 'F'))
946 *val = ch - 'A' + 10;
949 if ((ch >= '0') && (ch <= '9'))
961 if (ch >= 'a' && ch <= 'f')
962 return ch - 'a' + 10;
963 if (ch >= '0' && ch <= '9')
965 if (ch >= 'A' && ch <= 'F')
966 return ch - 'A' + 10;
971 stub_unpack_int (buff, fieldlength)
980 nibble = stubhex (*buff++);
984 retval = retval << 4;
990 unpack_varlen_hex (buff, result)
991 char *buff; /* packet to parse */
997 while (ishex (*buff, &nibble))
1000 retval = retval << 4;
1001 retval |= nibble & 0x0f;
1008 unpack_nibble (buf, val)
1012 ishex (*buf++, val);
1017 pack_nibble (buf, nibble)
1021 *buf++ = hexchars[(nibble & 0x0f)];
1026 pack_hex_byte (pkt, byte)
1030 *pkt++ = hexchars[(byte >> 4) & 0xf];
1031 *pkt++ = hexchars[(byte & 0xf)];
1036 unpack_byte (buf, value)
1040 *value = stub_unpack_int (buf, 2);
1045 pack_int (buf, value)
1049 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1050 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1051 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1052 buf = pack_hex_byte (buf, (value & 0xff));
1057 unpack_int (buf, value)
1061 *value = stub_unpack_int (buf, 8);
1065 #if 0 /* currently unused, uncomment when needed */
1066 static char *pack_string (char *pkt, char *string);
1069 pack_string (pkt, string)
1076 len = strlen (string);
1078 len = 200; /* Bigger than most GDB packets, junk??? */
1079 pkt = pack_hex_byte (pkt, len);
1083 if ((ch == '\0') || (ch == '#'))
1084 ch = '*'; /* Protect encapsulation */
1089 #endif /* 0 (unused) */
1092 unpack_string (src, dest, length)
1104 pack_threadid (pkt, id)
1109 unsigned char *altid;
1111 altid = (unsigned char *) id;
1112 limit = pkt + BUF_THREAD_ID_SIZE;
1114 pkt = pack_hex_byte (pkt, *altid++);
1120 unpack_threadid (inbuf, id)
1125 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1128 altref = (char *) id;
1130 while (inbuf < limit)
1132 x = stubhex (*inbuf++);
1133 y = stubhex (*inbuf++);
1134 *altref++ = (x << 4) | y;
1139 /* Externally, threadrefs are 64 bits but internally, they are still
1140 ints. This is due to a mismatch of specifications. We would like
1141 to use 64bit thread references internally. This is an adapter
1145 int_to_threadref (id, value)
1149 unsigned char *scan;
1151 scan = (unsigned char *) id;
1157 *scan++ = (value >> 24) & 0xff;
1158 *scan++ = (value >> 16) & 0xff;
1159 *scan++ = (value >> 8) & 0xff;
1160 *scan++ = (value & 0xff);
1164 threadref_to_int (ref)
1168 unsigned char *scan;
1170 scan = (char *) ref;
1174 value = (value << 8) | ((*scan++) & 0xff);
1179 copy_threadref (dest, src)
1184 unsigned char *csrc, *cdest;
1186 csrc = (unsigned char *) src;
1187 cdest = (unsigned char *) dest;
1194 threadmatch (dest, src)
1198 /* things are broken right now, so just assume we got a match */
1200 unsigned char *srcp, *destp;
1202 srcp = (char *) src;
1203 destp = (char *) dest;
1207 result &= (*srcp++ == *destp++) ? 1 : 0;
1214 threadid:1, # always request threadid
1221 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1224 pack_threadinfo_request (pkt, mode, id)
1229 *pkt++ = 'q'; /* Info Query */
1230 *pkt++ = 'P'; /* process or thread info */
1231 pkt = pack_int (pkt, mode); /* mode */
1232 pkt = pack_threadid (pkt, id); /* threadid */
1233 *pkt = '\0'; /* terminate */
1237 /* These values tag the fields in a thread info response packet */
1238 /* Tagging the fields allows us to request specific fields and to
1239 add more fields as time goes by */
1241 #define TAG_THREADID 1 /* Echo the thread identifier */
1242 #define TAG_EXISTS 2 /* Is this process defined enough to
1243 fetch registers and its stack */
1244 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1245 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1246 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1250 remote_unpack_thread_info_response (pkt, expectedref, info)
1252 threadref *expectedref;
1253 struct gdb_ext_thread_info *info;
1258 char *limit = pkt + PBUFSIZ; /* plausable parsing limit */
1261 /* info->threadid = 0; FIXME: implement zero_threadref */
1263 info->display[0] = '\0';
1264 info->shortname[0] = '\0';
1265 info->more_display[0] = '\0';
1267 /* Assume the characters indicating the packet type have been stripped */
1268 pkt = unpack_int (pkt, &mask); /* arg mask */
1269 pkt = unpack_threadid (pkt, &ref);
1272 warning ("Incomplete response to threadinfo request\n");
1273 if (!threadmatch (&ref, expectedref))
1274 { /* This is an answer to a different request */
1275 warning ("ERROR RMT Thread info mismatch\n");
1278 copy_threadref (&info->threadid, &ref);
1280 /* Loop on tagged fields , try to bail if somthing goes wrong */
1282 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
1284 pkt = unpack_int (pkt, &tag); /* tag */
1285 pkt = unpack_byte (pkt, &length); /* length */
1286 if (!(tag & mask)) /* tags out of synch with mask */
1288 warning ("ERROR RMT: threadinfo tag mismatch\n");
1292 if (tag == TAG_THREADID)
1296 warning ("ERROR RMT: length of threadid is not 16\n");
1300 pkt = unpack_threadid (pkt, &ref);
1301 mask = mask & ~TAG_THREADID;
1304 if (tag == TAG_EXISTS)
1306 info->active = stub_unpack_int (pkt, length);
1308 mask = mask & ~(TAG_EXISTS);
1311 warning ("ERROR RMT: 'exists' length too long\n");
1317 if (tag == TAG_THREADNAME)
1319 pkt = unpack_string (pkt, &info->shortname[0], length);
1320 mask = mask & ~TAG_THREADNAME;
1323 if (tag == TAG_DISPLAY)
1325 pkt = unpack_string (pkt, &info->display[0], length);
1326 mask = mask & ~TAG_DISPLAY;
1329 if (tag == TAG_MOREDISPLAY)
1331 pkt = unpack_string (pkt, &info->more_display[0], length);
1332 mask = mask & ~TAG_MOREDISPLAY;
1335 warning ("ERROR RMT: unknown thread info tag\n");
1336 break; /* Not a tag we know about */
1342 remote_get_threadinfo (threadid, fieldset, info)
1343 threadref *threadid;
1344 int fieldset; /* TAG mask */
1345 struct gdb_ext_thread_info *info;
1348 char *threadinfo_pkt = alloca (PBUFSIZ);
1350 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1351 putpkt (threadinfo_pkt);
1352 getpkt (threadinfo_pkt, PBUFSIZ, 0);
1353 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1358 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1359 representation of a threadid. */
1362 adapt_remote_get_threadinfo (ref, selection, info)
1365 struct gdb_ext_thread_info *info;
1369 int_to_threadref (&lclref, *ref);
1370 return remote_get_threadinfo (&lclref, selection, info);
1373 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1376 pack_threadlist_request (pkt, startflag, threadcount, nextthread)
1380 threadref *nextthread;
1382 *pkt++ = 'q'; /* info query packet */
1383 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1384 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1385 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1386 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1391 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1394 parse_threadlist_response (pkt, result_limit, original_echo, resultlist,
1398 threadref *original_echo;
1399 threadref *resultlist;
1403 int count, resultcount, done;
1406 /* Assume the 'q' and 'M chars have been stripped. */
1407 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */
1408 pkt = unpack_byte (pkt, &count); /* count field */
1409 pkt = unpack_nibble (pkt, &done);
1410 /* The first threadid is the argument threadid. */
1411 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1412 while ((count-- > 0) && (pkt < limit))
1414 pkt = unpack_threadid (pkt, resultlist++);
1415 if (resultcount++ >= result_limit)
1424 remote_get_threadlist (startflag, nextthread, result_limit,
1425 done, result_count, threadlist)
1427 threadref *nextthread;
1431 threadref *threadlist;
1434 static threadref echo_nextthread;
1435 char *threadlist_packet = alloca (PBUFSIZ);
1436 char *t_response = alloca (PBUFSIZ);
1439 /* Trancate result limit to be smaller than the packet size */
1440 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
1441 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
1443 pack_threadlist_request (threadlist_packet,
1444 startflag, result_limit, nextthread);
1445 putpkt (threadlist_packet);
1446 getpkt (t_response, PBUFSIZ, 0);
1449 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1452 if (!threadmatch (&echo_nextthread, nextthread))
1454 /* FIXME: This is a good reason to drop the packet */
1455 /* Possably, there is a duplicate response */
1457 retransmit immediatly - race conditions
1458 retransmit after timeout - yes
1460 wait for packet, then exit
1462 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1463 return 0; /* I choose simply exiting */
1465 if (*result_count <= 0)
1469 warning ("RMT ERROR : failed to get remote thread list\n");
1472 return result; /* break; */
1474 if (*result_count > result_limit)
1477 warning ("RMT ERROR: threadlist response longer than requested\n");
1483 /* This is the interface between remote and threads, remotes upper interface */
1485 /* remote_find_new_threads retrieves the thread list and for each
1486 thread in the list, looks up the thread in GDB's internal list,
1487 ading the thread if it does not already exist. This involves
1488 getting partial thread lists from the remote target so, polling the
1489 quit_flag is required. */
1492 /* About this many threadisds fit in a packet. */
1494 #define MAXTHREADLISTRESULTS 32
1497 remote_threadlist_iterator (stepfunction, context, looplimit)
1498 rmt_thread_action stepfunction;
1502 int done, i, result_count;
1506 static threadref nextthread;
1507 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1512 if (loopcount++ > looplimit)
1515 warning ("Remote fetch threadlist -infinite loop-\n");
1518 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1519 &done, &result_count, resultthreadlist))
1524 /* clear for later iterations */
1526 /* Setup to resume next batch of thread references, set nextthread. */
1527 if (result_count >= 1)
1528 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1530 while (result_count--)
1531 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1538 remote_newthread_step (ref, context)
1544 pid = threadref_to_int (ref);
1545 if (!in_thread_list (pid))
1547 return 1; /* continue iterator */
1550 #define CRAZY_MAX_THREADS 1000
1553 remote_current_thread (oldpid)
1556 char *buf = alloca (PBUFSIZ);
1559 getpkt (buf, PBUFSIZ, 0);
1560 if (buf[0] == 'Q' && buf[1] == 'C')
1561 return strtol (&buf[2], NULL, 16);
1566 /* Find new threads for info threads command.
1567 * Original version, using John Metzler's thread protocol.
1571 remote_find_new_threads ()
1573 remote_threadlist_iterator (remote_newthread_step, 0,
1575 if (inferior_pid == MAGIC_NULL_PID) /* ack ack ack */
1576 inferior_pid = remote_current_thread (inferior_pid);
1580 * Find all threads for info threads command.
1581 * Uses new thread protocol contributed by Cisco.
1582 * Falls back and attempts to use the older method (above)
1583 * if the target doesn't respond to the new method.
1587 remote_threads_info (void)
1589 char *buf = alloca (PBUFSIZ);
1593 if (remote_desc == 0) /* paranoia */
1594 error ("Command can only be used when connected to the remote target.");
1596 if (use_threadinfo_query)
1598 putpkt ("qfThreadInfo");
1600 getpkt (bufp, PBUFSIZ, 0);
1601 if (bufp[0] != '\0') /* q packet recognized */
1603 while (*bufp++ == 'm') /* reply contains one or more TID */
1607 tid = strtol (bufp, &bufp, 16);
1608 if (tid != 0 && !in_thread_list (tid))
1611 while (*bufp++ == ','); /* comma-separated list */
1612 putpkt ("qsThreadInfo");
1614 getpkt (bufp, PBUFSIZ, 0);
1620 /* Else fall back to old method based on jmetzler protocol. */
1621 use_threadinfo_query = 0;
1622 remote_find_new_threads ();
1627 * Collect a descriptive string about the given thread.
1628 * The target may say anything it wants to about the thread
1629 * (typically info about its blocked / runnable state, name, etc.).
1630 * This string will appear in the info threads display.
1632 * Optional: targets are not required to implement this function.
1636 remote_threads_extra_info (struct thread_info *tp)
1641 struct gdb_ext_thread_info threadinfo;
1642 static char display_buf[100]; /* arbitrary... */
1643 char *bufp = alloca (PBUFSIZ);
1644 int n = 0; /* position in display_buf */
1646 if (remote_desc == 0) /* paranoia */
1647 internal_error ("remote_threads_extra_info");
1649 if (use_threadextra_query)
1651 sprintf (bufp, "qThreadExtraInfo,%x", tp->pid);
1653 getpkt (bufp, PBUFSIZ, 0);
1658 for (p = display_buf;
1659 p < display_buf + sizeof(display_buf) - 1 &&
1664 *p = fromhex (bufp[0]) * 16 + fromhex (bufp[1]);
1671 /* If the above query fails, fall back to the old method. */
1672 use_threadextra_query = 0;
1673 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1674 | TAG_MOREDISPLAY | TAG_DISPLAY;
1675 int_to_threadref (&id, tp->pid);
1676 if (remote_get_threadinfo (&id, set, &threadinfo))
1677 if (threadinfo.active)
1679 if (*threadinfo.shortname)
1680 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1681 if (*threadinfo.display)
1682 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1683 if (*threadinfo.more_display)
1684 n += sprintf(&display_buf[n], " Priority: %s",
1685 threadinfo.more_display);
1689 /* for purely cosmetic reasons, clear up trailing commas */
1690 if (',' == display_buf[n-1])
1691 display_buf[n-1] = ' ';
1700 /* Restart the remote side; this is an extended protocol operation. */
1703 extended_remote_restart ()
1705 char *buf = alloca (PBUFSIZ);
1707 /* Send the restart command; for reasons I don't understand the
1708 remote side really expects a number after the "R". */
1710 sprintf (&buf[1], "%x", 0);
1713 /* Now query for status so this looks just like we restarted
1714 gdbserver from scratch. */
1716 getpkt (buf, PBUFSIZ, 0);
1719 /* Clean up connection to a remote debugger. */
1723 remote_close (quitting)
1727 SERIAL_CLOSE (remote_desc);
1731 /* Query the remote side for the text, data and bss offsets. */
1736 char *buf = alloca (PBUFSIZ);
1739 CORE_ADDR text_addr, data_addr, bss_addr;
1740 struct section_offsets *offs;
1742 putpkt ("qOffsets");
1744 getpkt (buf, PBUFSIZ, 0);
1746 if (buf[0] == '\000')
1747 return; /* Return silently. Stub doesn't support
1751 warning ("Remote failure reply: %s", buf);
1755 /* Pick up each field in turn. This used to be done with scanf, but
1756 scanf will make trouble if CORE_ADDR size doesn't match
1757 conversion directives correctly. The following code will work
1758 with any size of CORE_ADDR. */
1759 text_addr = data_addr = bss_addr = 0;
1763 if (strncmp (ptr, "Text=", 5) == 0)
1766 /* Don't use strtol, could lose on big values. */
1767 while (*ptr && *ptr != ';')
1768 text_addr = (text_addr << 4) + fromhex (*ptr++);
1773 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1776 while (*ptr && *ptr != ';')
1777 data_addr = (data_addr << 4) + fromhex (*ptr++);
1782 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1785 while (*ptr && *ptr != ';')
1786 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1792 error ("Malformed response to offset query, %s", buf);
1794 if (symfile_objfile == NULL)
1797 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1798 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1800 ANOFFSET (offs, SECT_OFF_TEXT (symfile_objfile)) = text_addr;
1802 /* This is a temporary kludge to force data and bss to use the same offsets
1803 because that's what nlmconv does now. The real solution requires changes
1804 to the stub and remote.c that I don't have time to do right now. */
1806 ANOFFSET (offs, SECT_OFF_DATA (symfile_objfile)) = data_addr;
1807 ANOFFSET (offs, SECT_OFF_BSS (symfile_objfile)) = data_addr;
1809 objfile_relocate (symfile_objfile, offs);
1813 * Cisco version of section offsets:
1815 * Instead of having GDB query the target for the section offsets,
1816 * Cisco lets the target volunteer the information! It's also in
1817 * a different format, so here are the functions that will decode
1818 * a section offset packet from a Cisco target.
1822 * Function: remote_cisco_section_offsets
1824 * Returns: zero for success, non-zero for failure
1828 remote_cisco_section_offsets (bfd_vma text_addr,
1831 bfd_signed_vma *text_offs,
1832 bfd_signed_vma *data_offs,
1833 bfd_signed_vma *bss_offs)
1835 bfd_vma text_base, data_base, bss_base;
1836 struct minimal_symbol *start;
1842 if (symfile_objfile == NULL)
1843 return -1; /* no can do nothin' */
1845 start = lookup_minimal_symbol ("_start", NULL, NULL);
1847 return -1; /* Can't find "_start" symbol */
1849 data_base = bss_base = 0;
1850 text_base = SYMBOL_VALUE_ADDRESS (start);
1852 abfd = symfile_objfile->obfd;
1853 for (sect = abfd->sections;
1857 p = (unsigned char *) bfd_get_section_name (abfd, sect);
1859 if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */
1860 if (data_base == 0 ||
1861 data_base > bfd_get_section_vma (abfd, sect))
1862 data_base = bfd_get_section_vma (abfd, sect);
1863 if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */
1864 if (bss_base == 0 ||
1865 bss_base > bfd_get_section_vma (abfd, sect))
1866 bss_base = bfd_get_section_vma (abfd, sect);
1868 *text_offs = text_addr - text_base;
1869 *data_offs = data_addr - data_base;
1870 *bss_offs = bss_addr - bss_base;
1875 sprintf (tmp, "VMA: text = 0x");
1876 sprintf_vma (tmp + strlen (tmp), text_addr);
1877 sprintf (tmp + strlen (tmp), " data = 0x");
1878 sprintf_vma (tmp + strlen (tmp), data_addr);
1879 sprintf (tmp + strlen (tmp), " bss = 0x");
1880 sprintf_vma (tmp + strlen (tmp), bss_addr);
1881 fprintf_filtered (gdb_stdlog, tmp);
1882 fprintf_filtered (gdb_stdlog,
1883 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1884 paddr_nz (*text_offs),
1885 paddr_nz (*data_offs),
1886 paddr_nz (*bss_offs));
1893 * Function: remote_cisco_objfile_relocate
1895 * Relocate the symbol file for a remote target.
1899 remote_cisco_objfile_relocate (text_off, data_off, bss_off)
1900 bfd_signed_vma text_off;
1901 bfd_signed_vma data_off;
1902 bfd_signed_vma bss_off;
1904 struct section_offsets *offs;
1906 if (text_off != 0 || data_off != 0 || bss_off != 0)
1908 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1909 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1910 simple canonical representation for this stuff. */
1912 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1913 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1915 ANOFFSET (offs, SECT_OFF_TEXT (symfile_objfile)) = text_off;
1916 ANOFFSET (offs, SECT_OFF_DATA (symfile_objfile)) = data_off;
1917 ANOFFSET (offs, SECT_OFF_BSS (symfile_objfile)) = bss_off;
1919 /* First call the standard objfile_relocate. */
1920 objfile_relocate (symfile_objfile, offs);
1922 /* Now we need to fix up the section entries already attached to
1923 the exec target. These entries will control memory transfers
1924 from the exec file. */
1926 exec_set_section_offsets (text_off, data_off, bss_off);
1930 /* Stub for catch_errors. */
1933 remote_start_remote_dummy (void *dummy)
1935 start_remote (); /* Initialize gdb process mechanisms */
1940 remote_start_remote (dummy)
1943 immediate_quit = 1; /* Allow user to interrupt it */
1945 /* Ack any packet which the remote side has already sent. */
1946 SERIAL_WRITE (remote_desc, "+", 1);
1948 /* Let the stub know that we want it to return the thread. */
1951 inferior_pid = remote_current_thread (inferior_pid);
1953 get_offsets (); /* Get text, data & bss offsets */
1955 putpkt ("?"); /* initiate a query from remote machine */
1958 return remote_start_remote_dummy (dummy);
1961 /* Open a connection to a remote debugger.
1962 NAME is the filename used for communication. */
1965 remote_open (name, from_tty)
1969 remote_open_1 (name, from_tty, &remote_ops, 0);
1972 /* Just like remote_open, but with asynchronous support. */
1974 remote_async_open (name, from_tty)
1978 remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
1981 /* Open a connection to a remote debugger using the extended
1982 remote gdb protocol. NAME is the filename used for communication. */
1985 extended_remote_open (name, from_tty)
1989 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ );
1992 /* Just like extended_remote_open, but with asynchronous support. */
1994 extended_remote_async_open (name, from_tty)
1998 remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ );
2001 /* Generic code for opening a connection to a remote target. */
2003 static DCACHE *remote_dcache;
2006 remote_open_1 (name, from_tty, target, extended_p)
2009 struct target_ops *target;
2013 error ("To open a remote debug connection, you need to specify what\n\
2014 serial device is attached to the remote system\n\
2015 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2017 /* See FIXME above */
2018 wait_forever_enabled_p = 1;
2020 target_preopen (from_tty);
2022 unpush_target (target);
2024 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
2026 remote_desc = SERIAL_OPEN (name);
2028 perror_with_name (name);
2030 if (baud_rate != -1)
2032 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2034 SERIAL_CLOSE (remote_desc);
2035 perror_with_name (name);
2039 SERIAL_RAW (remote_desc);
2041 /* If there is something sitting in the buffer we might take it as a
2042 response to a command, which would be bad. */
2043 SERIAL_FLUSH_INPUT (remote_desc);
2047 puts_filtered ("Remote debugging using ");
2048 puts_filtered (name);
2049 puts_filtered ("\n");
2051 push_target (target); /* Switch to using remote target now */
2053 init_packet_config (&remote_protocol_P);
2054 init_packet_config (&remote_protocol_Z);
2056 general_thread = -2;
2057 continue_thread = -2;
2059 /* Force remote_write_bytes to check whether target supports
2060 binary downloading. */
2061 init_packet_config (&remote_protocol_binary_download);
2063 /* Probe for ability to use "ThreadInfo" query, as required. */
2064 use_threadinfo_query = 1;
2065 use_threadextra_query = 1;
2067 /* Without this, some commands which require an active target (such
2068 as kill) won't work. This variable serves (at least) double duty
2069 as both the pid of the target process (if it has such), and as a
2070 flag indicating that a target is active. These functions should
2071 be split out into seperate variables, especially since GDB will
2072 someday have a notion of debugging several processes. */
2074 inferior_pid = MAGIC_NULL_PID;
2075 /* Start the remote connection; if error (0), discard this target.
2076 In particular, if the user quits, be sure to discard it
2077 (we'd be in an inconsistent state otherwise). */
2078 if (!catch_errors (remote_start_remote, NULL,
2079 "Couldn't establish connection to remote target\n",
2088 /* tell the remote that we're using the extended protocol. */
2089 char *buf = alloca (PBUFSIZ);
2091 getpkt (buf, PBUFSIZ, 0);
2095 /* Just like remote_open but with asynchronous support. */
2097 remote_async_open_1 (name, from_tty, target, extended_p)
2100 struct target_ops *target;
2104 error ("To open a remote debug connection, you need to specify what\n\
2105 serial device is attached to the remote system\n\
2106 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2108 target_preopen (from_tty);
2110 unpush_target (target);
2112 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
2114 remote_desc = SERIAL_OPEN (name);
2116 perror_with_name (name);
2118 if (baud_rate != -1)
2120 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2122 SERIAL_CLOSE (remote_desc);
2123 perror_with_name (name);
2127 SERIAL_RAW (remote_desc);
2129 /* If there is something sitting in the buffer we might take it as a
2130 response to a command, which would be bad. */
2131 SERIAL_FLUSH_INPUT (remote_desc);
2135 puts_filtered ("Remote debugging using ");
2136 puts_filtered (name);
2137 puts_filtered ("\n");
2140 push_target (target); /* Switch to using remote target now */
2142 init_packet_config (&remote_protocol_P);
2143 init_packet_config (&remote_protocol_Z);
2145 general_thread = -2;
2146 continue_thread = -2;
2148 /* Force remote_write_bytes to check whether target supports
2149 binary downloading. */
2150 init_packet_config (&remote_protocol_binary_download);
2152 /* Probe for ability to use "ThreadInfo" query, as required. */
2153 use_threadinfo_query = 1;
2154 use_threadextra_query = 1;
2156 /* Without this, some commands which require an active target (such
2157 as kill) won't work. This variable serves (at least) double duty
2158 as both the pid of the target process (if it has such), and as a
2159 flag indicating that a target is active. These functions should
2160 be split out into seperate variables, especially since GDB will
2161 someday have a notion of debugging several processes. */
2162 inferior_pid = MAGIC_NULL_PID;
2164 /* With this target we start out by owning the terminal. */
2165 remote_async_terminal_ours_p = 1;
2167 /* FIXME: cagney/1999-09-23: During the initial connection it is
2168 assumed that the target is already ready and able to respond to
2169 requests. Unfortunatly remote_start_remote() eventually calls
2170 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2171 around this. Eventually a mechanism that allows
2172 wait_for_inferior() to expect/get timeouts will be
2174 wait_forever_enabled_p = 0;
2176 /* Start the remote connection; if error (0), discard this target.
2177 In particular, if the user quits, be sure to discard it
2178 (we'd be in an inconsistent state otherwise). */
2179 if (!catch_errors (remote_start_remote, NULL,
2180 "Couldn't establish connection to remote target\n",
2184 wait_forever_enabled_p = 1;
2188 wait_forever_enabled_p = 1;
2192 /* tell the remote that we're using the extended protocol. */
2193 char *buf = alloca (PBUFSIZ);
2195 getpkt (buf, PBUFSIZ, 0);
2199 /* This takes a program previously attached to and detaches it. After
2200 this is done, GDB can be used to debug some other program. We
2201 better not have left any breakpoints in the target program or it'll
2202 die when it hits one. */
2205 remote_detach (args, from_tty)
2209 char *buf = alloca (PBUFSIZ);
2212 error ("Argument given to \"detach\" when remotely debugging.");
2214 /* Tell the remote target to detach. */
2216 remote_send (buf, PBUFSIZ);
2218 target_mourn_inferior ();
2220 puts_filtered ("Ending remote debugging.\n");
2224 /* Same as remote_detach, but with async support. */
2226 remote_async_detach (args, from_tty)
2230 char *buf = alloca (PBUFSIZ);
2233 error ("Argument given to \"detach\" when remotely debugging.");
2235 /* Tell the remote target to detach. */
2237 remote_send (buf, PBUFSIZ);
2239 /* Unregister the file descriptor from the event loop. */
2240 if (target_is_async_p ())
2241 SERIAL_ASYNC (remote_desc, NULL, 0);
2243 target_mourn_inferior ();
2245 puts_filtered ("Ending remote debugging.\n");
2248 /* Convert hex digit A to a number. */
2254 if (a >= '0' && a <= '9')
2256 else if (a >= 'a' && a <= 'f')
2257 return a - 'a' + 10;
2258 else if (a >= 'A' && a <= 'F')
2259 return a - 'A' + 10;
2261 error ("Reply contains invalid hex digit %d", a);
2264 /* Convert number NIB to a hex digit. */
2273 return 'a' + nib - 10;
2276 /* Tell the remote machine to resume. */
2278 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2280 static int last_sent_step;
2283 remote_resume (pid, step, siggnal)
2285 enum target_signal siggnal;
2287 char *buf = alloca (PBUFSIZ);
2290 set_thread (0, 0); /* run any thread */
2292 set_thread (pid, 0); /* run this thread */
2294 dcache_flush (remote_dcache);
2296 last_sent_signal = siggnal;
2297 last_sent_step = step;
2299 /* A hook for when we need to do something at the last moment before
2301 if (target_resume_hook)
2302 (*target_resume_hook) ();
2304 if (siggnal != TARGET_SIGNAL_0)
2306 buf[0] = step ? 'S' : 'C';
2307 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2308 buf[2] = tohex ((int) siggnal & 0xf);
2312 strcpy (buf, step ? "s" : "c");
2317 /* Same as remote_resume, but with async support. */
2319 remote_async_resume (pid, step, siggnal)
2321 enum target_signal siggnal;
2323 char *buf = alloca (PBUFSIZ);
2326 set_thread (0, 0); /* run any thread */
2328 set_thread (pid, 0); /* run this thread */
2330 dcache_flush (remote_dcache);
2332 last_sent_signal = siggnal;
2333 last_sent_step = step;
2335 /* A hook for when we need to do something at the last moment before
2337 if (target_resume_hook)
2338 (*target_resume_hook) ();
2340 if (siggnal != TARGET_SIGNAL_0)
2342 buf[0] = step ? 'S' : 'C';
2343 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2344 buf[2] = tohex ((int) siggnal & 0xf);
2348 strcpy (buf, step ? "s" : "c");
2350 /* We are about to start executing the inferior, let's register it
2351 with the event loop. NOTE: this is the one place where all the
2352 execution commands end up. We could alternatively do this in each
2353 of the execution commands in infcmd.c.*/
2354 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2355 into infcmd.c in order to allow inferior function calls to work
2356 NOT asynchronously. */
2357 if (event_loop_p && target_can_async_p ())
2358 target_async (inferior_event_handler, 0);
2359 /* Tell the world that the target is now executing. */
2360 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2361 this? Instead, should the client of target just assume (for
2362 async targets) that the target is going to start executing? Is
2363 this information already found in the continuation block? */
2364 if (target_is_async_p ())
2365 target_executing = 1;
2370 /* Set up the signal handler for SIGINT, while the target is
2371 executing, ovewriting the 'regular' SIGINT signal handler. */
2373 initialize_sigint_signal_handler ()
2375 sigint_remote_token =
2376 create_async_signal_handler (async_remote_interrupt, NULL);
2377 signal (SIGINT, handle_remote_sigint);
2380 /* Signal handler for SIGINT, while the target is executing. */
2382 handle_remote_sigint (sig)
2385 signal (sig, handle_remote_sigint_twice);
2386 sigint_remote_twice_token =
2387 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2388 mark_async_signal_handler_wrapper (sigint_remote_token);
2391 /* Signal handler for SIGINT, installed after SIGINT has already been
2392 sent once. It will take effect the second time that the user sends
2395 handle_remote_sigint_twice (sig)
2398 signal (sig, handle_sigint);
2399 sigint_remote_twice_token =
2400 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2401 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2404 /* Perform the real interruption of the target execution, in response
2407 async_remote_interrupt (arg)
2408 gdb_client_data arg;
2411 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2416 /* Perform interrupt, if the first attempt did not succeed. Just give
2417 up on the target alltogether. */
2419 async_remote_interrupt_twice (arg)
2420 gdb_client_data arg;
2423 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2424 /* Do something only if the target was not killed by the previous
2426 if (target_executing)
2429 signal (SIGINT, handle_remote_sigint);
2433 /* Reinstall the usual SIGINT handlers, after the target has
2436 cleanup_sigint_signal_handler (void *dummy)
2438 signal (SIGINT, handle_sigint);
2439 if (sigint_remote_twice_token)
2440 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
2441 if (sigint_remote_token)
2442 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
2445 /* Send ^C to target to halt it. Target will respond, and send us a
2447 static void (*ofunc) (int);
2449 /* The command line interface's stop routine. This function is installed
2450 as a signal handler for SIGINT. The first time a user requests a
2451 stop, we call remote_stop to send a break or ^C. If there is no
2452 response from the target (it didn't stop when the user requested it),
2453 we ask the user if he'd like to detach from the target. */
2455 remote_interrupt (signo)
2458 /* If this doesn't work, try more severe steps. */
2459 signal (signo, remote_interrupt_twice);
2462 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2467 /* The user typed ^C twice. */
2470 remote_interrupt_twice (signo)
2473 signal (signo, ofunc);
2475 signal (signo, remote_interrupt);
2478 /* This is the generic stop called via the target vector. When a target
2479 interrupt is requested, either by the command line or the GUI, we
2480 will eventually end up here. */
2484 /* Send a break or a ^C, depending on user preference. */
2486 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2489 SERIAL_SEND_BREAK (remote_desc);
2491 SERIAL_WRITE (remote_desc, "\003", 1);
2494 /* Ask the user what to do when an interrupt is received. */
2499 target_terminal_ours ();
2501 if (query ("Interrupted while waiting for the program.\n\
2502 Give up (and stop debugging it)? "))
2504 target_mourn_inferior ();
2505 return_to_top_level (RETURN_QUIT);
2508 target_terminal_inferior ();
2511 /* Enable/disable target terminal ownership. Most targets can use
2512 terminal groups to control terminal ownership. Remote targets are
2513 different in that explicit transfer of ownership to/from GDB/target
2517 remote_async_terminal_inferior (void)
2519 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2520 sync_execution here. This function should only be called when
2521 GDB is resuming the inferior in the forground. A background
2522 resume (``run&'') should leave GDB in control of the terminal and
2523 consequently should not call this code. */
2524 if (!sync_execution)
2526 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2527 calls target_terminal_*() idenpotent. The event-loop GDB talking
2528 to an asynchronous target with a synchronous command calls this
2529 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2530 stops trying to transfer the terminal to the target when it
2531 shouldn't this guard can go away. */
2532 if (!remote_async_terminal_ours_p)
2534 delete_file_handler (input_fd);
2535 remote_async_terminal_ours_p = 0;
2536 initialize_sigint_signal_handler ();
2537 /* NOTE: At this point we could also register our selves as the
2538 recipient of all input. Any characters typed could then be
2539 passed on down to the target. */
2543 remote_async_terminal_ours (void)
2545 /* See FIXME in remote_async_terminal_inferior. */
2546 if (!sync_execution)
2548 /* See FIXME in remote_async_terminal_inferior. */
2549 if (remote_async_terminal_ours_p)
2551 cleanup_sigint_signal_handler (NULL);
2552 add_file_handler (input_fd, stdin_event_handler, 0);
2553 remote_async_terminal_ours_p = 1;
2556 /* If nonzero, ignore the next kill. */
2561 remote_console_output (char *msg)
2565 for (p = msg; p[0] && p[1]; p += 2)
2568 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2571 fputs_unfiltered (tb, gdb_stdtarg);
2573 gdb_flush (gdb_stdtarg);
2576 /* Wait until the remote machine stops, then return,
2577 storing status in STATUS just as `wait' would.
2578 Returns "pid", which in the case of a multi-threaded
2579 remote OS, is the thread-id. */
2582 remote_wait (pid, status)
2584 struct target_waitstatus *status;
2586 unsigned char *buf = alloca (PBUFSIZ);
2587 int thread_num = -1;
2589 status->kind = TARGET_WAITKIND_EXITED;
2590 status->value.integer = 0;
2596 ofunc = signal (SIGINT, remote_interrupt);
2597 getpkt (buf, PBUFSIZ, 1);
2598 signal (SIGINT, ofunc);
2600 /* This is a hook for when we need to do something (perhaps the
2601 collection of trace data) every time the target stops. */
2602 if (target_wait_loop_hook)
2603 (*target_wait_loop_hook) ();
2607 case 'E': /* Error of some sort */
2608 warning ("Remote failure reply: %s", buf);
2610 case 'T': /* Status with PC, SP, FP, ... */
2614 char regs[MAX_REGISTER_RAW_SIZE];
2616 /* Expedited reply, containing Signal, {regno, reg} repeat */
2617 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2619 n... = register number
2620 r... = register contents
2622 p = &buf[3]; /* after Txx */
2629 /* Read the register number */
2630 regno = strtol ((const char *) p, &p_temp, 16);
2631 p1 = (unsigned char *) p_temp;
2633 if (p1 == p) /* No register number present here */
2635 p1 = (unsigned char *) strchr ((const char *) p, ':');
2637 warning ("Malformed packet(a) (missing colon): %s\n\
2640 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2642 p_temp = unpack_varlen_hex (++p1, &thread_num);
2643 record_currthread (thread_num);
2644 p = (unsigned char *) p_temp;
2652 warning ("Malformed packet(b) (missing colon): %s\n\
2656 if (regno >= NUM_REGS)
2657 warning ("Remote sent bad register number %ld: %s\n\
2661 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
2663 if (p[0] == 0 || p[1] == 0)
2664 warning ("Remote reply is too short: %s", buf);
2665 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
2668 supply_register (regno, regs);
2673 warning ("Remote register badly formatted: %s", buf);
2674 warning (" here: %s", p);
2679 case 'S': /* Old style status, just signal only */
2680 status->kind = TARGET_WAITKIND_STOPPED;
2681 status->value.sig = (enum target_signal)
2682 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2686 /* Export Cisco kernel mode as a convenience variable
2687 (so that it can be used in the GDB prompt if desired). */
2689 if (cisco_kernel_mode == 1)
2690 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2691 value_from_string ("PDEBUG-"));
2692 cisco_kernel_mode = 0;
2693 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2694 record_currthread (thread_num);
2696 else if (buf[3] == 'k')
2698 /* Export Cisco kernel mode as a convenience variable
2699 (so that it can be used in the GDB prompt if desired). */
2701 if (cisco_kernel_mode == 1)
2702 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2703 value_from_string ("KDEBUG-"));
2704 cisco_kernel_mode = 1;
2707 case 'N': /* Cisco special: status and offsets */
2709 bfd_vma text_addr, data_addr, bss_addr;
2710 bfd_signed_vma text_off, data_off, bss_off;
2713 status->kind = TARGET_WAITKIND_STOPPED;
2714 status->value.sig = (enum target_signal)
2715 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2717 if (symfile_objfile == NULL)
2719 warning ("Relocation packet received with no symbol file. \
2724 /* Relocate object file. Buffer format is NAATT;DD;BB
2725 * where AA is the signal number, TT is the new text
2726 * address, DD * is the new data address, and BB is the
2727 * new bss address. */
2730 text_addr = strtoul (p, (char **) &p1, 16);
2731 if (p1 == p || *p1 != ';')
2732 warning ("Malformed relocation packet: Packet '%s'", buf);
2734 data_addr = strtoul (p, (char **) &p1, 16);
2735 if (p1 == p || *p1 != ';')
2736 warning ("Malformed relocation packet: Packet '%s'", buf);
2738 bss_addr = strtoul (p, (char **) &p1, 16);
2740 warning ("Malformed relocation packet: Packet '%s'", buf);
2742 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2743 &text_off, &data_off, &bss_off)
2745 if (text_off != 0 || data_off != 0 || bss_off != 0)
2746 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2750 case 'W': /* Target exited */
2752 /* The remote process exited. */
2753 status->kind = TARGET_WAITKIND_EXITED;
2754 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2758 status->kind = TARGET_WAITKIND_SIGNALLED;
2759 status->value.sig = (enum target_signal)
2760 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2764 case 'O': /* Console output */
2765 remote_console_output (buf + 1);
2768 if (last_sent_signal != TARGET_SIGNAL_0)
2770 /* Zero length reply means that we tried 'S' or 'C' and
2771 the remote system doesn't support it. */
2772 target_terminal_ours_for_output ();
2774 ("Can't send signals to this remote system. %s not sent.\n",
2775 target_signal_to_name (last_sent_signal));
2776 last_sent_signal = TARGET_SIGNAL_0;
2777 target_terminal_inferior ();
2779 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2780 putpkt ((char *) buf);
2783 /* else fallthrough */
2785 warning ("Invalid remote reply: %s", buf);
2790 if (thread_num != -1)
2794 return inferior_pid;
2797 /* Async version of remote_wait. */
2799 remote_async_wait (pid, status)
2801 struct target_waitstatus *status;
2803 unsigned char *buf = alloca (PBUFSIZ);
2804 int thread_num = -1;
2806 status->kind = TARGET_WAITKIND_EXITED;
2807 status->value.integer = 0;
2813 if (!target_is_async_p ())
2814 ofunc = signal (SIGINT, remote_interrupt);
2815 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2816 _never_ wait for ever -> test on target_is_async_p().
2817 However, before we do that we need to ensure that the caller
2818 knows how to take the target into/out of async mode. */
2819 getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
2820 if (!target_is_async_p ())
2821 signal (SIGINT, ofunc);
2823 /* This is a hook for when we need to do something (perhaps the
2824 collection of trace data) every time the target stops. */
2825 if (target_wait_loop_hook)
2826 (*target_wait_loop_hook) ();
2830 case 'E': /* Error of some sort */
2831 warning ("Remote failure reply: %s", buf);
2833 case 'T': /* Status with PC, SP, FP, ... */
2837 char regs[MAX_REGISTER_RAW_SIZE];
2839 /* Expedited reply, containing Signal, {regno, reg} repeat */
2840 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2842 n... = register number
2843 r... = register contents
2845 p = &buf[3]; /* after Txx */
2852 /* Read the register number */
2853 regno = strtol ((const char *) p, &p_temp, 16);
2854 p1 = (unsigned char *) p_temp;
2856 if (p1 == p) /* No register number present here */
2858 p1 = (unsigned char *) strchr ((const char *) p, ':');
2860 warning ("Malformed packet(a) (missing colon): %s\n\
2863 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2865 p_temp = unpack_varlen_hex (++p1, &thread_num);
2866 record_currthread (thread_num);
2867 p = (unsigned char *) p_temp;
2875 warning ("Malformed packet(b) (missing colon): %s\n\
2879 if (regno >= NUM_REGS)
2880 warning ("Remote sent bad register number %ld: %s\n\
2884 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
2886 if (p[0] == 0 || p[1] == 0)
2887 warning ("Remote reply is too short: %s", buf);
2888 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
2891 supply_register (regno, regs);
2896 warning ("Remote register badly formatted: %s", buf);
2897 warning (" here: %s", p);
2902 case 'S': /* Old style status, just signal only */
2903 status->kind = TARGET_WAITKIND_STOPPED;
2904 status->value.sig = (enum target_signal)
2905 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2909 /* Export Cisco kernel mode as a convenience variable
2910 (so that it can be used in the GDB prompt if desired). */
2912 if (cisco_kernel_mode == 1)
2913 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2914 value_from_string ("PDEBUG-"));
2915 cisco_kernel_mode = 0;
2916 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2917 record_currthread (thread_num);
2919 else if (buf[3] == 'k')
2921 /* Export Cisco kernel mode as a convenience variable
2922 (so that it can be used in the GDB prompt if desired). */
2924 if (cisco_kernel_mode == 1)
2925 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2926 value_from_string ("KDEBUG-"));
2927 cisco_kernel_mode = 1;
2930 case 'N': /* Cisco special: status and offsets */
2932 bfd_vma text_addr, data_addr, bss_addr;
2933 bfd_signed_vma text_off, data_off, bss_off;
2936 status->kind = TARGET_WAITKIND_STOPPED;
2937 status->value.sig = (enum target_signal)
2938 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2940 if (symfile_objfile == NULL)
2942 warning ("Relocation packet recieved with no symbol file. \
2947 /* Relocate object file. Buffer format is NAATT;DD;BB
2948 * where AA is the signal number, TT is the new text
2949 * address, DD * is the new data address, and BB is the
2950 * new bss address. */
2953 text_addr = strtoul (p, (char **) &p1, 16);
2954 if (p1 == p || *p1 != ';')
2955 warning ("Malformed relocation packet: Packet '%s'", buf);
2957 data_addr = strtoul (p, (char **) &p1, 16);
2958 if (p1 == p || *p1 != ';')
2959 warning ("Malformed relocation packet: Packet '%s'", buf);
2961 bss_addr = strtoul (p, (char **) &p1, 16);
2963 warning ("Malformed relocation packet: Packet '%s'", buf);
2965 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2966 &text_off, &data_off, &bss_off)
2968 if (text_off != 0 || data_off != 0 || bss_off != 0)
2969 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2973 case 'W': /* Target exited */
2975 /* The remote process exited. */
2976 status->kind = TARGET_WAITKIND_EXITED;
2977 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2981 status->kind = TARGET_WAITKIND_SIGNALLED;
2982 status->value.sig = (enum target_signal)
2983 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2987 case 'O': /* Console output */
2988 remote_console_output (buf + 1);
2989 /* Return immediately to the event loop. The event loop will
2990 still be waiting on the inferior afterwards. */
2991 status->kind = TARGET_WAITKIND_IGNORE;
2994 if (last_sent_signal != TARGET_SIGNAL_0)
2996 /* Zero length reply means that we tried 'S' or 'C' and
2997 the remote system doesn't support it. */
2998 target_terminal_ours_for_output ();
3000 ("Can't send signals to this remote system. %s not sent.\n",
3001 target_signal_to_name (last_sent_signal));
3002 last_sent_signal = TARGET_SIGNAL_0;
3003 target_terminal_inferior ();
3005 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3006 putpkt ((char *) buf);
3009 /* else fallthrough */
3011 warning ("Invalid remote reply: %s", buf);
3016 if (thread_num != -1)
3020 return inferior_pid;
3023 /* Number of bytes of registers this stub implements. */
3025 static int register_bytes_found;
3027 /* Read the remote registers into the block REGS. */
3028 /* Currently we just read all the registers, so we don't use regno. */
3032 remote_fetch_registers (regno)
3035 char *buf = alloca (PBUFSIZ);
3038 char regs[REGISTER_BYTES];
3040 set_thread (inferior_pid, 1);
3043 remote_send (buf, PBUFSIZ);
3045 /* Save the size of the packet sent to us by the target. Its used
3046 as a heuristic when determining the max size of packets that the
3047 target can safely receive. */
3048 if (actual_register_packet_size == 0)
3049 actual_register_packet_size = strlen (buf);
3051 /* Unimplemented registers read as all bits zero. */
3052 memset (regs, 0, REGISTER_BYTES);
3054 /* We can get out of synch in various cases. If the first character
3055 in the buffer is not a hex character, assume that has happened
3056 and try to fetch another packet to read. */
3057 while ((buf[0] < '0' || buf[0] > '9')
3058 && (buf[0] < 'a' || buf[0] > 'f')
3059 && buf[0] != 'x') /* New: unavailable register value */
3062 fprintf_unfiltered (gdb_stdlog,
3063 "Bad register packet; fetching a new packet\n");
3064 getpkt (buf, PBUFSIZ, 0);
3067 /* Reply describes registers byte by byte, each byte encoded as two
3068 hex characters. Suck them all up, then supply them to the
3069 register cacheing/storage mechanism. */
3072 for (i = 0; i < REGISTER_BYTES; i++)
3078 warning ("Remote reply is of odd length: %s", buf);
3079 /* Don't change register_bytes_found in this case, and don't
3080 print a second warning. */
3083 if (p[0] == 'x' && p[1] == 'x')
3084 regs[i] = 0; /* 'x' */
3086 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3090 if (i != register_bytes_found)
3092 register_bytes_found = i;
3093 #ifdef REGISTER_BYTES_OK
3094 if (!REGISTER_BYTES_OK (i))
3095 warning ("Remote reply is too short: %s", buf);
3100 for (i = 0; i < NUM_REGS; i++)
3102 supply_register (i, ®s[REGISTER_BYTE (i)]);
3103 if (buf[REGISTER_BYTE (i) * 2] == 'x')
3104 register_valid[i] = -1; /* register value not available */
3108 /* Prepare to store registers. Since we may send them all (using a
3109 'G' request), we have to read out the ones we don't want to change
3113 remote_prepare_to_store ()
3115 /* Make sure the entire registers array is valid. */
3116 switch (remote_protocol_P.support)
3118 case PACKET_DISABLE:
3119 case PACKET_SUPPORT_UNKNOWN:
3120 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
3127 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
3128 packet was not recognized. */
3131 store_register_using_P (int regno)
3133 /* Try storing a single register. */
3134 char *buf = alloca (PBUFSIZ);
3139 sprintf (buf, "P%x=", regno);
3140 p = buf + strlen (buf);
3141 regp = ®isters[REGISTER_BYTE (regno)];
3142 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
3144 *p++ = tohex ((regp[i] >> 4) & 0xf);
3145 *p++ = tohex (regp[i] & 0xf);
3148 remote_send (buf, PBUFSIZ);
3150 return buf[0] != '\0';
3154 /* Store register REGNO, or all registers if REGNO == -1, from the contents
3155 of REGISTERS. FIXME: ignores errors. */
3158 remote_store_registers (regno)
3161 char *buf = alloca (PBUFSIZ);
3165 set_thread (inferior_pid, 1);
3169 switch (remote_protocol_P.support)
3171 case PACKET_DISABLE:
3174 if (store_register_using_P (regno))
3177 error ("Protocol error: P packet not recognized by stub");
3178 case PACKET_SUPPORT_UNKNOWN:
3179 if (store_register_using_P (regno))
3181 /* The stub recognized the 'P' packet. Remember this. */
3182 remote_protocol_P.support = PACKET_ENABLE;
3187 /* The stub does not support the 'P' packet. Use 'G'
3188 instead, and don't try using 'P' in the future (it
3189 will just waste our time). */
3190 remote_protocol_P.support = PACKET_DISABLE;
3198 /* Command describes registers byte by byte,
3199 each byte encoded as two hex characters. */
3202 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3203 for (i = 0; i < register_bytes_found; i++)
3205 *p++ = tohex ((registers[i] >> 4) & 0xf);
3206 *p++ = tohex (registers[i] & 0xf);
3210 remote_send (buf, PBUFSIZ);
3214 /* Return the number of hex digits in num. */
3222 for (i = 0; num != 0; i++)
3228 /* Set BUF to the minimum number of hex digits representing NUM. */
3231 hexnumstr (buf, num)
3235 int len = hexnumlen (num);
3236 return hexnumnstr (buf, num, len);
3240 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3243 hexnumnstr (buf, num, width)
3252 for (i = width - 1; i >= 0; i--)
3254 buf[i] = "0123456789abcdef"[(num & 0xf)];
3261 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3264 remote_address_masked (addr)
3267 if (remote_address_size > 0
3268 && remote_address_size < (sizeof (ULONGEST) * 8))
3270 /* Only create a mask when that mask can safely be constructed
3271 in a ULONGEST variable. */
3273 mask = (mask << remote_address_size) - 1;
3279 /* Determine whether the remote target supports binary downloading.
3280 This is accomplished by sending a no-op memory write of zero length
3281 to the target at the specified address. It does not suffice to send
3282 the whole packet, since many stubs strip the eighth bit and subsequently
3283 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3285 NOTE: This can still lose if the serial line is not eight-bit
3286 clean. In cases like this, the user should clear "remote
3290 check_binary_download (addr)
3293 switch (remote_protocol_binary_download.support)
3295 case PACKET_DISABLE:
3299 case PACKET_SUPPORT_UNKNOWN:
3301 char *buf = alloca (PBUFSIZ);
3306 p += hexnumstr (p, (ULONGEST) addr);
3308 p += hexnumstr (p, (ULONGEST) 0);
3312 putpkt_binary (buf, (int) (p - buf));
3313 getpkt (buf, PBUFSIZ, 0);
3318 fprintf_unfiltered (gdb_stdlog,
3319 "binary downloading NOT suppported by target\n");
3320 remote_protocol_binary_download.support = PACKET_DISABLE;
3325 fprintf_unfiltered (gdb_stdlog,
3326 "binary downloading suppported by target\n");
3327 remote_protocol_binary_download.support = PACKET_ENABLE;
3334 /* Write memory data directly to the remote machine.
3335 This does not inform the data cache; the data cache uses this.
3336 MEMADDR is the address in the remote memory space.
3337 MYADDR is the address of the buffer in our space.
3338 LEN is the number of bytes.
3340 Returns number of bytes transferred, or 0 (setting errno) for
3341 error. Only transfer a single packet. */
3344 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3347 int max_buf_size; /* Max size of packet output buffer */
3349 unsigned char *plen;
3355 /* Verify that the target can support a binary download */
3356 check_binary_download (memaddr);
3358 /* Determine the max packet size. */
3359 max_buf_size = get_memory_write_packet_size ();
3360 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3361 buf = alloca (sizeof_buf);
3363 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3364 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3366 /* construct "M"<memaddr>","<len>":" */
3367 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3370 /* Append [XM]. Compute a best guess of the number of bytes
3371 actually transfered. */
3372 switch (remote_protocol_binary_download.support)
3376 /* Best guess at number of bytes that will fit. */
3377 todo = min (len, max_buf_size);
3379 case PACKET_DISABLE:
3381 /* num bytes that will fit */
3382 todo = min (len, max_buf_size / 2);
3384 case PACKET_SUPPORT_UNKNOWN:
3385 internal_error ("remote_write_bytes: bad switch");
3388 /* Append <memaddr> */
3389 memaddr = remote_address_masked (memaddr);
3390 p += hexnumstr (p, (ULONGEST) memaddr);
3393 /* Append <len>. Retain the location/size of <len>. It may
3394 need to be adjusted once the packet body has been created. */
3396 plenlen = hexnumstr (p, (ULONGEST) todo);
3401 /* Append the packet body. */
3402 switch (remote_protocol_binary_download.support)
3405 /* Binary mode. Send target system values byte by byte, in
3406 increasing byte addresses. Only escape certain critical
3409 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3412 switch (myaddr[nr_bytes] & 0xff)
3417 /* These must be escaped */
3419 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3422 *p++ = myaddr[nr_bytes] & 0xff;
3426 if (nr_bytes < todo)
3428 /* Escape chars have filled up the buffer prematurely,
3429 and we have actually sent fewer bytes than planned.
3430 Fix-up the length field of the packet. Use the same
3431 number of characters as before. */
3433 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3434 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3437 case PACKET_DISABLE:
3438 /* Normal mode: Send target system values byte by byte, in
3439 increasing byte addresses. Each byte is encoded as a two hex
3441 for (nr_bytes = 0; nr_bytes < todo; nr_bytes++)
3443 *p++ = tohex ((myaddr[nr_bytes] >> 4) & 0xf);
3444 *p++ = tohex (myaddr[nr_bytes] & 0xf);
3448 case PACKET_SUPPORT_UNKNOWN:
3449 internal_error ("remote_write_bytes: bad switch");
3452 putpkt_binary (buf, (int) (p - buf));
3453 getpkt (buf, sizeof_buf, 0);
3457 /* There is no correspondance between what the remote protocol
3458 uses for errors and errno codes. We would like a cleaner way
3459 of representing errors (big enough to include errno codes,
3460 bfd_error codes, and others). But for now just return EIO. */
3465 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3466 bytes than we'd planned. */
3470 /* Read memory data directly from the remote machine.
3471 This does not use the data cache; the data cache uses this.
3472 MEMADDR is the address in the remote memory space.
3473 MYADDR is the address of the buffer in our space.
3474 LEN is the number of bytes.
3476 Returns number of bytes transferred, or 0 for error. */
3478 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3479 remote targets) shouldn't attempt to read the entire buffer.
3480 Instead it should read a single packet worth of data and then
3481 return the byte size of that packet to the caller. The caller (its
3482 caller and its callers caller ;-) already contains code for
3483 handling partial reads. */
3486 remote_read_bytes (memaddr, myaddr, len)
3492 int max_buf_size; /* Max size of packet output buffer */
3496 /* Create a buffer big enough for this packet. */
3497 max_buf_size = get_memory_read_packet_size ();
3498 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3499 buf = alloca (sizeof_buf);
3508 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3510 /* construct "m"<memaddr>","<len>" */
3511 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3512 memaddr = remote_address_masked (memaddr);
3515 p += hexnumstr (p, (ULONGEST) memaddr);
3517 p += hexnumstr (p, (ULONGEST) todo);
3521 getpkt (buf, sizeof_buf, 0);
3525 /* There is no correspondance between what the remote protocol uses
3526 for errors and errno codes. We would like a cleaner way of
3527 representing errors (big enough to include errno codes, bfd_error
3528 codes, and others). But for now just return EIO. */
3533 /* Reply describes memory byte by byte,
3534 each byte encoded as two hex characters. */
3537 for (i = 0; i < todo; i++)
3539 if (p[0] == 0 || p[1] == 0)
3540 /* Reply is short. This means that we were able to read
3541 only part of what we wanted to. */
3542 return i + (origlen - len);
3543 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3553 /* Read or write LEN bytes from inferior memory at MEMADDR,
3554 transferring to or from debugger address BUFFER. Write to inferior if
3555 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3560 remote_xfer_memory (mem_addr, buffer, mem_len, should_write, target)
3565 struct target_ops *target; /* ignored */
3567 CORE_ADDR targ_addr;
3569 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3573 return dcache_xfer_memory (remote_dcache, targ_addr, buffer,
3574 targ_len, should_write);
3579 /* Enable after 4.12. */
3582 remote_search (len, data, mask, startaddr, increment, lorange, hirange
3583 addr_found, data_found)
3587 CORE_ADDR startaddr;
3591 CORE_ADDR *addr_found;
3594 if (increment == -4 && len == 4)
3596 long mask_long, data_long;
3597 long data_found_long;
3598 CORE_ADDR addr_we_found;
3599 char *buf = alloca (PBUFSIZ);
3600 long returned_long[2];
3603 mask_long = extract_unsigned_integer (mask, len);
3604 data_long = extract_unsigned_integer (data, len);
3605 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3607 getpkt (buf, PBUFSIZ, 0);
3610 /* The stub doesn't support the 't' request. We might want to
3611 remember this fact, but on the other hand the stub could be
3612 switched on us. Maybe we should remember it only until
3613 the next "target remote". */
3614 generic_search (len, data, mask, startaddr, increment, lorange,
3615 hirange, addr_found, data_found);
3620 /* There is no correspondance between what the remote protocol uses
3621 for errors and errno codes. We would like a cleaner way of
3622 representing errors (big enough to include errno codes, bfd_error
3623 codes, and others). But for now just use EIO. */
3624 memory_error (EIO, startaddr);
3627 while (*p != '\0' && *p != ',')
3628 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3630 error ("Protocol error: short return for search");
3632 data_found_long = 0;
3633 while (*p != '\0' && *p != ',')
3634 data_found_long = (data_found_long << 4) + fromhex (*p++);
3635 /* Ignore anything after this comma, for future extensions. */
3637 if (addr_we_found < lorange || addr_we_found >= hirange)
3643 *addr_found = addr_we_found;
3644 *data_found = store_unsigned_integer (data_we_found, len);
3647 generic_search (len, data, mask, startaddr, increment, lorange,
3648 hirange, addr_found, data_found);
3653 remote_files_info (ignore)
3654 struct target_ops *ignore;
3656 puts_filtered ("Debugging a target over a serial line.\n");
3659 /* Stuff for dealing with the packets which are part of this protocol.
3660 See comment at top of file for details. */
3662 /* Read a single character from the remote end, masking it down to 7 bits. */
3670 ch = SERIAL_READCHAR (remote_desc, timeout);
3675 switch ((enum serial_rc) ch)
3678 target_mourn_inferior ();
3679 error ("Remote connection closed");
3682 perror_with_name ("Remote communication error");
3684 case SERIAL_TIMEOUT:
3690 /* Send the command in BUF to the remote machine, and read the reply
3691 into BUF. Report an error if we get an error reply. */
3694 remote_send (char *buf,
3698 getpkt (buf, sizeof_buf, 0);
3701 error ("Remote failure reply: %s", buf);
3704 /* Display a null-terminated packet on stdout, for debugging, using C
3711 puts_filtered ("\"");
3712 fputstr_filtered (buf, '"', gdb_stdout);
3713 puts_filtered ("\"");
3720 return putpkt_binary (buf, strlen (buf));
3723 /* Send a packet to the remote machine, with error checking. The data
3724 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3725 to account for the $, # and checksum, and for a possible /0 if we are
3726 debugging (remote_debug) and want to print the sent packet as a string */
3729 putpkt_binary (buf, cnt)
3734 unsigned char csum = 0;
3735 char *buf2 = alloca (cnt + 6);
3736 long sizeof_junkbuf = PBUFSIZ;
3737 char *junkbuf = alloca (sizeof_junkbuf);
3743 /* Copy the packet into buffer BUF2, encapsulating it
3744 and giving it a checksum. */
3749 for (i = 0; i < cnt; i++)
3755 *p++ = tohex ((csum >> 4) & 0xf);
3756 *p++ = tohex (csum & 0xf);
3758 /* Send it over and over until we get a positive ack. */
3762 int started_error_output = 0;
3767 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3768 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
3769 fprintf_unfiltered (gdb_stdlog, "...");
3770 gdb_flush (gdb_stdlog);
3772 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
3773 perror_with_name ("putpkt: write failed");
3775 /* read until either a timeout occurs (-2) or '+' is read */
3778 ch = readchar (remote_timeout);
3786 case SERIAL_TIMEOUT:
3788 if (started_error_output)
3790 putchar_unfiltered ('\n');
3791 started_error_output = 0;
3800 fprintf_unfiltered (gdb_stdlog, "Ack\n");
3804 fprintf_unfiltered (gdb_stdlog, "Nak\n");
3805 case SERIAL_TIMEOUT:
3809 break; /* Retransmit buffer */
3812 /* It's probably an old response, and we're out of sync.
3813 Just gobble up the packet and ignore it. */
3814 getpkt (junkbuf, sizeof_junkbuf, 0);
3815 continue; /* Now, go look for + */
3820 if (!started_error_output)
3822 started_error_output = 1;
3823 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
3825 fputc_unfiltered (ch & 0177, gdb_stdlog);
3829 break; /* Here to retransmit */
3833 /* This is wrong. If doing a long backtrace, the user should be
3834 able to get out next time we call QUIT, without anything as
3835 violent as interrupt_query. If we want to provide a way out of
3836 here without getting to the next QUIT, it should be based on
3837 hitting ^C twice as in remote_wait. */
3847 static int remote_cisco_mode;
3849 /* Come here after finding the start of the frame. Collect the rest
3850 into BUF, verifying the checksum, length, and handling run-length
3851 compression. No more than sizeof_buf-1 characters are read so that
3852 the buffer can be NUL terminated.
3854 Returns -1 on error, number of characters in buffer (ignoring the
3855 trailing NULL) on success. (could be extended to return one of the
3856 SERIAL status indications). */
3859 read_frame (char *buf,
3871 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
3872 c = readchar (remote_timeout);
3875 case SERIAL_TIMEOUT:
3877 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
3881 fputs_filtered ("Saw new packet start in middle of old one\n",
3883 return -1; /* Start a new packet, count retries */
3886 unsigned char pktcsum;
3890 pktcsum = fromhex (readchar (remote_timeout)) << 4;
3891 pktcsum |= fromhex (readchar (remote_timeout));
3893 if (csum == pktcsum)
3898 fprintf_filtered (gdb_stdlog,
3899 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3901 fputs_filtered (buf, gdb_stdlog);
3902 fputs_filtered ("\n", gdb_stdlog);
3904 /* Number of characters in buffer ignoring trailing
3908 case '*': /* Run length encoding */
3913 if (remote_cisco_mode == 0)
3915 c = readchar (remote_timeout);
3917 repeat = c - ' ' + 3; /* Compute repeat count */
3921 /* Cisco's run-length encoding variant uses two
3922 hex chars to represent the repeat count. */
3924 c = readchar (remote_timeout);
3926 repeat = fromhex (c) << 4;
3927 c = readchar (remote_timeout);
3929 repeat += fromhex (c);
3932 /* The character before ``*'' is repeated. */
3934 if (repeat > 0 && repeat <= 255
3936 && bc + repeat < sizeof_buf - 1)
3938 memset (&buf[bc], buf[bc - 1], repeat);
3944 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
3945 puts_filtered (buf);
3946 puts_filtered ("\n");
3950 if (bc < sizeof_buf - 1)
3958 puts_filtered ("Remote packet too long: ");
3959 puts_filtered (buf);
3960 puts_filtered ("\n");
3967 /* Read a packet from the remote machine, with error checking, and
3968 store it in BUF. If FOREVER, wait forever rather than timing out;
3969 this is used (in synchronous mode) to wait for a target that is is
3970 executing user code to stop. */
3971 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
3972 don't have to change all the calls to getpkt to deal with the
3973 return value, because at the moment I don't know what the right
3974 thing to do it for those. */
3982 timed_out = getpkt_sane (buf, sizeof_buf, forever);
3986 /* Read a packet from the remote machine, with error checking, and
3987 store it in BUF. If FOREVER, wait forever rather than timing out;
3988 this is used (in synchronous mode) to wait for a target that is is
3989 executing user code to stop. If FOREVER == 0, this function is
3990 allowed to time out gracefully and return an indication of this to
3993 getpkt_sane (char *buf,
4002 strcpy (buf, "timeout");
4006 timeout = watchdog > 0 ? watchdog : -1;
4010 timeout = remote_timeout;
4014 for (tries = 1; tries <= MAX_TRIES; tries++)
4016 /* This can loop forever if the remote side sends us characters
4017 continuously, but if it pauses, we'll get a zero from readchar
4018 because of timeout. Then we'll count that as a retry. */
4020 /* Note that we will only wait forever prior to the start of a packet.
4021 After that, we expect characters to arrive at a brisk pace. They
4022 should show up within remote_timeout intervals. */
4026 c = readchar (timeout);
4028 if (c == SERIAL_TIMEOUT)
4030 if (forever) /* Watchdog went off? Kill the target. */
4033 target_mourn_inferior ();
4034 error ("Watchdog has expired. Target detached.\n");
4037 fputs_filtered ("Timed out.\n", gdb_stdlog);
4043 /* We've found the start of a packet, now collect the data. */
4045 val = read_frame (buf, sizeof_buf);
4051 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4052 fputstr_unfiltered (buf, 0, gdb_stdlog);
4053 fprintf_unfiltered (gdb_stdlog, "\n");
4055 SERIAL_WRITE (remote_desc, "+", 1);
4059 /* Try the whole thing again. */
4061 SERIAL_WRITE (remote_desc, "-", 1);
4064 /* We have tried hard enough, and just can't receive the packet. Give up. */
4066 printf_unfiltered ("Ignoring packet error, continuing...\n");
4067 SERIAL_WRITE (remote_desc, "+", 1);
4074 /* For some mysterious reason, wait_for_inferior calls kill instead of
4075 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4079 target_mourn_inferior ();
4083 /* Use catch_errors so the user can quit from gdb even when we aren't on
4084 speaking terms with the remote system. */
4085 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4087 /* Don't wait for it to die. I'm not really sure it matters whether
4088 we do or not. For the existing stubs, kill is a noop. */
4089 target_mourn_inferior ();
4092 /* Async version of remote_kill. */
4094 remote_async_kill ()
4096 /* Unregister the file descriptor from the event loop. */
4097 if (target_is_async_p ())
4098 SERIAL_ASYNC (remote_desc, NULL, 0);
4100 /* For some mysterious reason, wait_for_inferior calls kill instead of
4101 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4105 target_mourn_inferior ();
4109 /* Use catch_errors so the user can quit from gdb even when we aren't on
4110 speaking terms with the remote system. */
4111 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4113 /* Don't wait for it to die. I'm not really sure it matters whether
4114 we do or not. For the existing stubs, kill is a noop. */
4115 target_mourn_inferior ();
4121 remote_mourn_1 (&remote_ops);
4125 remote_async_mourn ()
4127 remote_mourn_1 (&remote_async_ops);
4131 extended_remote_mourn ()
4133 /* We do _not_ want to mourn the target like this; this will
4134 remove the extended remote target from the target stack,
4135 and the next time the user says "run" it'll fail.
4137 FIXME: What is the right thing to do here? */
4139 remote_mourn_1 (&extended_remote_ops);
4143 /* Worker function for remote_mourn. */
4145 remote_mourn_1 (target)
4146 struct target_ops *target;
4148 unpush_target (target);
4149 generic_mourn_inferior ();
4152 /* In the extended protocol we want to be able to do things like
4153 "run" and have them basically work as expected. So we need
4154 a special create_inferior function.
4156 FIXME: One day add support for changing the exec file
4157 we're debugging, arguments and an environment. */
4160 extended_remote_create_inferior (exec_file, args, env)
4165 /* Rip out the breakpoints; we'll reinsert them after restarting
4166 the remote server. */
4167 remove_breakpoints ();
4169 /* Now restart the remote server. */
4170 extended_remote_restart ();
4172 /* Now put the breakpoints back in. This way we're safe if the
4173 restart function works via a unix fork on the remote side. */
4174 insert_breakpoints ();
4176 /* Clean up from the last time we were running. */
4177 clear_proceed_status ();
4179 /* Let the remote process run. */
4180 proceed (-1, TARGET_SIGNAL_0, 0);
4183 /* Async version of extended_remote_create_inferior. */
4185 extended_remote_async_create_inferior (exec_file, args, env)
4190 /* Rip out the breakpoints; we'll reinsert them after restarting
4191 the remote server. */
4192 remove_breakpoints ();
4194 /* If running asynchronously, register the target file descriptor
4195 with the event loop. */
4196 if (event_loop_p && target_can_async_p ())
4197 target_async (inferior_event_handler, 0);
4199 /* Now restart the remote server. */
4200 extended_remote_restart ();
4202 /* Now put the breakpoints back in. This way we're safe if the
4203 restart function works via a unix fork on the remote side. */
4204 insert_breakpoints ();
4206 /* Clean up from the last time we were running. */
4207 clear_proceed_status ();
4209 /* Let the remote process run. */
4210 proceed (-1, TARGET_SIGNAL_0, 0);
4214 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
4215 than other targets; in those use REMOTE_BREAKPOINT instead of just
4216 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4217 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4218 the standard routines that are in mem-break.c. */
4220 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
4221 the choice of breakpoint instruction affects target program design and
4222 vice versa, and by making it user-tweakable, the special code here
4223 goes away and we need fewer special GDB configurations. */
4225 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4226 #define REMOTE_BREAKPOINT
4229 #ifdef REMOTE_BREAKPOINT
4231 /* If the target isn't bi-endian, just pretend it is. */
4232 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4233 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4234 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4237 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4238 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4240 #endif /* REMOTE_BREAKPOINT */
4242 /* Insert a breakpoint on targets that don't have any better breakpoint
4243 support. We read the contents of the target location and stash it,
4244 then overwrite it with a breakpoint instruction. ADDR is the target
4245 location in the target machine. CONTENTS_CACHE is a pointer to
4246 memory allocated for saving the target contents. It is guaranteed
4247 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4248 is accomplished via BREAKPOINT_MAX). */
4251 remote_insert_breakpoint (addr, contents_cache)
4253 char *contents_cache;
4255 #ifdef REMOTE_BREAKPOINT
4260 /* Try the "Z" packet if it is not already disabled.
4261 If it succeeds, then set the support to PACKET_ENABLE.
4262 If it fails, and the user has explicitly requested the Z support
4263 then report an error, otherwise, mark it disabled and go on. */
4265 if ((remote_protocol_Z.support == PACKET_ENABLE)
4266 || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN))
4268 char *buf = alloca (PBUFSIZ);
4271 addr = remote_address_masked (addr);
4275 p += hexnumstr (p, (ULONGEST) addr);
4276 BREAKPOINT_FROM_PC (&addr, &bp_size);
4277 sprintf (p, ",%d", bp_size);
4280 getpkt (buf, PBUFSIZ, 0);
4284 remote_protocol_Z.support = PACKET_ENABLE;
4285 return (buf[0] == 'E');
4288 /* The stub does not support the 'Z' request. If the user has
4289 explicitly requested the Z support, or if the stub previously
4290 said it supported the packet, this is an error,
4291 otherwise, mark it disabled. */
4293 else if (remote_protocol_Z.support == PACKET_ENABLE)
4295 error ("Protocol error: Z packet not recognized by stub");
4299 remote_protocol_Z.support = PACKET_DISABLE;
4303 #ifdef REMOTE_BREAKPOINT
4304 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4308 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4309 val = target_write_memory (addr, (char *) big_break_insn,
4310 sizeof big_break_insn);
4312 val = target_write_memory (addr, (char *) little_break_insn,
4313 sizeof little_break_insn);
4318 return memory_insert_breakpoint (addr, contents_cache);
4319 #endif /* REMOTE_BREAKPOINT */
4323 remote_remove_breakpoint (addr, contents_cache)
4325 char *contents_cache;
4329 if ((remote_protocol_Z.support == PACKET_ENABLE)
4330 || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN))
4332 char *buf = alloca (PBUFSIZ);
4339 addr = remote_address_masked (addr);
4340 p += hexnumstr (p, (ULONGEST) addr);
4341 BREAKPOINT_FROM_PC (&addr, &bp_size);
4342 sprintf (p, ",%d", bp_size);
4345 getpkt (buf, PBUFSIZ, 0);
4347 return (buf[0] == 'E');
4350 #ifdef REMOTE_BREAKPOINT
4351 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4353 return memory_remove_breakpoint (addr, contents_cache);
4354 #endif /* REMOTE_BREAKPOINT */
4357 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
4359 remote_insert_watchpoint (addr, len, type)
4364 char *buf = alloca (PBUFSIZ);
4367 if (remote_protocol_Z.support == PACKET_DISABLE)
4368 error ("Can't set hardware watchpoints without the 'Z' packet\n");
4370 sprintf (buf, "Z%x,", type + 2 );
4371 p = strchr (buf, '\0');
4372 addr = remote_address_masked (addr);
4373 p += hexnumstr (p, (ULONGEST) addr);
4374 sprintf (p, ",%x", len);
4377 getpkt (buf, PBUFSIZ, 0);
4379 if (buf[0] == '\0' || buf [0] == 'E')
4386 remote_remove_watchpoint (addr, len, type)
4391 char *buf = alloca (PBUFSIZ);
4394 sprintf (buf, "z%x,", type + 2 );
4395 p = strchr (buf, '\0');
4396 addr = remote_address_masked (addr);
4397 p += hexnumstr (p, (ULONGEST) addr);
4398 sprintf (p, ",%x", len);
4400 getpkt (buf, PBUFSIZ, 0);
4402 if (buf[0] == '\0' || buf [0] == 'E')
4409 remote_insert_hw_breakpoint (addr, len)
4413 char *buf = alloca (PBUFSIZ);
4416 if (remote_protocol_Z.support == PACKET_DISABLE)
4417 error ("Can't set hardware breakpoints without the 'Z' packet\n");
4423 addr = remote_address_masked (addr);
4424 p += hexnumstr (p, (ULONGEST) addr);
4428 getpkt (buf, PBUFSIZ, 0);
4430 if (buf[0] == '\0' || buf [0] == 'E')
4437 remote_remove_hw_breakpoint (addr, len)
4441 char *buf = alloca (PBUFSIZ);
4448 addr = remote_address_masked (addr);
4449 p += hexnumstr (p, (ULONGEST) addr);
4453 getpkt (buf, PBUFSIZ, 0);
4455 if (buf[0] == '\0' || buf [0] == 'E')
4462 /* Some targets are only capable of doing downloads, and afterwards
4463 they switch to the remote serial protocol. This function provides
4464 a clean way to get from the download target to the remote target.
4465 It's basically just a wrapper so that we don't have to expose any
4466 of the internal workings of remote.c.
4468 Prior to calling this routine, you should shutdown the current
4469 target code, else you will get the "A program is being debugged
4470 already..." message. Usually a call to pop_target() suffices. */
4473 push_remote_target (name, from_tty)
4477 printf_filtered ("Switching to remote protocol\n");
4478 remote_open (name, from_tty);
4481 /* Other targets want to use the entire remote serial module but with
4482 certain remote_ops overridden. */
4485 open_remote_target (name, from_tty, target, extended_p)
4488 struct target_ops *target;
4491 printf_filtered ("Selecting the %sremote protocol\n",
4492 (extended_p ? "extended-" : ""));
4493 remote_open_1 (name, from_tty, target, extended_p);
4496 /* Table used by the crc32 function to calcuate the checksum. */
4498 static unsigned long crc32_table[256] =
4501 static unsigned long
4502 crc32 (buf, len, crc)
4507 if (!crc32_table[1])
4509 /* Initialize the CRC table and the decoding table. */
4513 for (i = 0; i < 256; i++)
4515 for (c = i << 24, j = 8; j > 0; --j)
4516 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4523 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4529 /* compare-sections command
4531 With no arguments, compares each loadable section in the exec bfd
4532 with the same memory range on the target, and reports mismatches.
4533 Useful for verifying the image on the target against the exec file.
4534 Depends on the target understanding the new "qCRC:" request. */
4536 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4537 target method (target verify memory) and generic version of the
4538 actual command. This will allow other high-level code (especially
4539 generic_load()) to make use of this target functionality. */
4542 compare_sections_command (args, from_tty)
4547 unsigned long host_crc, target_crc;
4548 extern bfd *exec_bfd;
4549 struct cleanup *old_chain;
4553 char *buf = alloca (PBUFSIZ);
4560 error ("command cannot be used without an exec file");
4561 if (!current_target.to_shortname ||
4562 strcmp (current_target.to_shortname, "remote") != 0)
4563 error ("command can only be used with remote target");
4565 for (s = exec_bfd->sections; s; s = s->next)
4567 if (!(s->flags & SEC_LOAD))
4568 continue; /* skip non-loadable section */
4570 size = bfd_get_section_size_before_reloc (s);
4572 continue; /* skip zero-length section */
4574 sectname = (char *) bfd_get_section_name (exec_bfd, s);
4575 if (args && strcmp (args, sectname) != 0)
4576 continue; /* not the section selected by user */
4578 matched = 1; /* do this section */
4580 /* FIXME: assumes lma can fit into long */
4581 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4584 /* be clever; compute the host_crc before waiting for target reply */
4585 sectdata = xmalloc (size);
4586 old_chain = make_cleanup (free, sectdata);
4587 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4588 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4590 getpkt (buf, PBUFSIZ, 0);
4592 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4593 sectname, lma, lma + size);
4595 error ("remote target does not support this operation");
4597 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4598 target_crc = target_crc * 16 + fromhex (*tmp);
4600 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4601 sectname, paddr (lma), paddr (lma + size));
4602 if (host_crc == target_crc)
4603 printf_filtered ("matched.\n");
4606 printf_filtered ("MIS-MATCHED!\n");
4610 do_cleanups (old_chain);
4613 warning ("One or more sections of the remote executable does not match\n\
4614 the loaded file\n");
4615 if (args && !matched)
4616 printf_filtered ("No loaded section named '%s'.\n", args);
4620 remote_query (query_type, buf, outbuf, bufsiz)
4627 char *buf2 = alloca (PBUFSIZ);
4628 char *p2 = &buf2[0];
4631 error ("null pointer to remote bufer size specified");
4633 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4634 the caller know and return what the minimum size is */
4635 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4636 if (*bufsiz < PBUFSIZ)
4642 /* except for querying the minimum buffer size, target must be open */
4644 error ("remote query is only available after target open");
4646 /* we only take uppercase letters as query types, at least for now */
4647 if ((query_type < 'A') || (query_type > 'Z'))
4648 error ("invalid remote query type");
4651 error ("null remote query specified");
4654 error ("remote query requires a buffer to receive data");
4661 /* we used one buffer char for the remote protocol q command and another
4662 for the query type. As the remote protocol encapsulation uses 4 chars
4663 plus one extra in case we are debugging (remote_debug),
4664 we have PBUFZIZ - 7 left to pack the query string */
4666 while (buf[i] && (i < (PBUFSIZ - 8)))
4668 /* bad caller may have sent forbidden characters */
4669 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
4670 error ("illegal characters in query string");
4678 error ("query larger than available buffer");
4684 getpkt (outbuf, *bufsiz, 0);
4690 remote_rcmd (char *command,
4691 struct ui_file *outbuf)
4694 char *buf = alloca (PBUFSIZ);
4698 error ("remote rcmd is only available after target open");
4700 /* Send a NULL command across as an empty command */
4701 if (command == NULL)
4704 /* The query prefix */
4705 strcpy (buf, "qRcmd,");
4706 p = strchr (buf, '\0');
4708 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ)
4709 error ("\"monitor\" command ``%s'' is too long\n", command);
4711 /* Encode the actual command */
4712 for (i = 0; command[i]; i++)
4714 *p++ = tohex ((command[i] >> 4) & 0xf);
4715 *p++ = tohex (command[i] & 0xf);
4719 if (putpkt (buf) < 0)
4720 error ("Communication problem with target\n");
4722 /* get/display the response */
4725 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4727 getpkt (buf, PBUFSIZ, 0);
4729 error ("Target does not support this command\n");
4730 if (buf[0] == 'O' && buf[1] != 'K')
4732 remote_console_output (buf + 1); /* 'O' message from stub */
4735 if (strcmp (buf, "OK") == 0)
4737 if (strlen (buf) == 3 && buf[0] == 'E'
4738 && isdigit (buf[1]) && isdigit (buf[2]))
4740 error ("Protocol error with Rcmd");
4742 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
4744 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
4745 fputc_unfiltered (c, outbuf);
4752 packet_command (args, from_tty)
4756 char *buf = alloca (PBUFSIZ);
4759 error ("command can only be used with remote target");
4762 error ("remote-packet command requires packet text as argument");
4764 puts_filtered ("sending: ");
4765 print_packet (args);
4766 puts_filtered ("\n");
4769 getpkt (buf, PBUFSIZ, 0);
4770 puts_filtered ("received: ");
4772 puts_filtered ("\n");
4776 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4778 static void display_thread_info (struct gdb_ext_thread_info *info);
4780 static void threadset_test_cmd (char *cmd, int tty);
4782 static void threadalive_test (char *cmd, int tty);
4784 static void threadlist_test_cmd (char *cmd, int tty);
4786 int get_and_display_threadinfo (threadref * ref);
4788 static void threadinfo_test_cmd (char *cmd, int tty);
4790 static int thread_display_step (threadref * ref, void *context);
4792 static void threadlist_update_test_cmd (char *cmd, int tty);
4794 static void init_remote_threadtests (void);
4796 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
4799 threadset_test_cmd (cmd, tty)
4803 int sample_thread = SAMPLE_THREAD;
4805 printf_filtered ("Remote threadset test\n");
4806 set_thread (sample_thread, 1);
4811 threadalive_test (cmd, tty)
4815 int sample_thread = SAMPLE_THREAD;
4817 if (remote_thread_alive (sample_thread))
4818 printf_filtered ("PASS: Thread alive test\n");
4820 printf_filtered ("FAIL: Thread alive test\n");
4823 void output_threadid (char *title, threadref * ref);
4826 output_threadid (title, ref)
4832 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
4834 printf_filtered ("%s %s\n", title, (&hexid[0]));
4838 threadlist_test_cmd (cmd, tty)
4843 threadref nextthread;
4844 int done, result_count;
4845 threadref threadlist[3];
4847 printf_filtered ("Remote Threadlist test\n");
4848 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
4849 &result_count, &threadlist[0]))
4850 printf_filtered ("FAIL: threadlist test\n");
4853 threadref *scan = threadlist;
4854 threadref *limit = scan + result_count;
4856 while (scan < limit)
4857 output_threadid (" thread ", scan++);
4862 display_thread_info (info)
4863 struct gdb_ext_thread_info *info;
4865 output_threadid ("Threadid: ", &info->threadid);
4866 printf_filtered ("Name: %s\n ", info->shortname);
4867 printf_filtered ("State: %s\n", info->display);
4868 printf_filtered ("other: %s\n\n", info->more_display);
4872 get_and_display_threadinfo (ref)
4877 struct gdb_ext_thread_info threadinfo;
4879 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
4880 | TAG_MOREDISPLAY | TAG_DISPLAY;
4881 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
4882 display_thread_info (&threadinfo);
4887 threadinfo_test_cmd (cmd, tty)
4891 int athread = SAMPLE_THREAD;
4895 int_to_threadref (&thread, athread);
4896 printf_filtered ("Remote Threadinfo test\n");
4897 if (!get_and_display_threadinfo (&thread))
4898 printf_filtered ("FAIL cannot get thread info\n");
4902 thread_display_step (ref, context)
4906 /* output_threadid(" threadstep ",ref); *//* simple test */
4907 return get_and_display_threadinfo (ref);
4911 threadlist_update_test_cmd (cmd, tty)
4915 printf_filtered ("Remote Threadlist update test\n");
4916 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
4920 init_remote_threadtests (void)
4922 add_com ("tlist", class_obscure, threadlist_test_cmd,
4923 "Fetch and print the remote list of thread identifiers, one pkt only");
4924 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
4925 "Fetch and display info about one thread");
4926 add_com ("tset", class_obscure, threadset_test_cmd,
4927 "Test setting to a different thread");
4928 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
4929 "Iterate through updating all remote thread info");
4930 add_com ("talive", class_obscure, threadalive_test,
4931 " Remote thread alive test ");
4939 remote_ops.to_shortname = "remote";
4940 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
4942 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4943 Specify the serial device it is connected to\n\
4944 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
4945 remote_ops.to_open = remote_open;
4946 remote_ops.to_close = remote_close;
4947 remote_ops.to_detach = remote_detach;
4948 remote_ops.to_resume = remote_resume;
4949 remote_ops.to_wait = remote_wait;
4950 remote_ops.to_fetch_registers = remote_fetch_registers;
4951 remote_ops.to_store_registers = remote_store_registers;
4952 remote_ops.to_prepare_to_store = remote_prepare_to_store;
4953 remote_ops.to_xfer_memory = remote_xfer_memory;
4954 remote_ops.to_files_info = remote_files_info;
4955 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
4956 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
4957 remote_ops.to_kill = remote_kill;
4958 remote_ops.to_load = generic_load;
4959 remote_ops.to_mourn_inferior = remote_mourn;
4960 remote_ops.to_thread_alive = remote_thread_alive;
4961 remote_ops.to_find_new_threads = remote_threads_info;
4962 remote_ops.to_extra_thread_info = remote_threads_extra_info;
4963 remote_ops.to_stop = remote_stop;
4964 remote_ops.to_query = remote_query;
4965 remote_ops.to_rcmd = remote_rcmd;
4966 remote_ops.to_stratum = process_stratum;
4967 remote_ops.to_has_all_memory = 1;
4968 remote_ops.to_has_memory = 1;
4969 remote_ops.to_has_stack = 1;
4970 remote_ops.to_has_registers = 1;
4971 remote_ops.to_has_execution = 1;
4972 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
4973 remote_ops.to_magic = OPS_MAGIC;
4976 /* Set up the extended remote vector by making a copy of the standard
4977 remote vector and adding to it. */
4980 init_extended_remote_ops ()
4982 extended_remote_ops = remote_ops;
4984 extended_remote_ops.to_shortname = "extended-remote";
4985 extended_remote_ops.to_longname =
4986 "Extended remote serial target in gdb-specific protocol";
4987 extended_remote_ops.to_doc =
4988 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4989 Specify the serial device it is connected to (e.g. /dev/ttya).",
4990 extended_remote_ops.to_open = extended_remote_open;
4991 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
4992 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
4996 * Command: info remote-process
4998 * This implements Cisco's version of the "info proc" command.
5000 * This query allows the target stub to return an arbitrary string
5001 * (or strings) giving arbitrary information about the target process.
5002 * This is optional; the target stub isn't required to implement it.
5004 * Syntax: qfProcessInfo request first string
5005 * qsProcessInfo request subsequent string
5006 * reply: 'O'<hex-encoded-string>
5007 * 'l' last reply (empty)
5011 remote_info_process (char *args, int from_tty)
5013 char *buf = alloca (PBUFSIZ);
5015 if (remote_desc == 0)
5016 error ("Command can only be used when connected to the remote target.");
5018 putpkt ("qfProcessInfo");
5019 getpkt (buf, PBUFSIZ, 0);
5021 return; /* Silently: target does not support this feature. */
5024 error ("info proc: target error.");
5026 while (buf[0] == 'O') /* Capitol-O packet */
5028 remote_console_output (&buf[1]);
5029 putpkt ("qsProcessInfo");
5030 getpkt (buf, PBUFSIZ, 0);
5039 remote_cisco_open (char *name, int from_tty)
5043 "To open a remote debug connection, you need to specify what \n\
5044 device is attached to the remote system (e.g. host:port).");
5046 /* See FIXME above */
5047 wait_forever_enabled_p = 1;
5049 target_preopen (from_tty);
5051 unpush_target (&remote_cisco_ops);
5053 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
5055 remote_desc = SERIAL_OPEN (name);
5057 perror_with_name (name);
5060 * If a baud rate was specified on the gdb command line it will
5061 * be greater than the initial value of -1. If it is, use it otherwise
5065 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
5066 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
5068 SERIAL_CLOSE (remote_desc);
5069 perror_with_name (name);
5072 SERIAL_RAW (remote_desc);
5074 /* If there is something sitting in the buffer we might take it as a
5075 response to a command, which would be bad. */
5076 SERIAL_FLUSH_INPUT (remote_desc);
5080 puts_filtered ("Remote debugging using ");
5081 puts_filtered (name);
5082 puts_filtered ("\n");
5085 remote_cisco_mode = 1;
5087 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5089 init_packet_config (&remote_protocol_P);
5090 init_packet_config (&remote_protocol_Z);
5092 general_thread = -2;
5093 continue_thread = -2;
5095 /* Force remote_write_bytes to check whether target supports
5096 binary downloading. */
5097 init_packet_config (&remote_protocol_binary_download);
5099 /* Probe for ability to use "ThreadInfo" query, as required. */
5100 use_threadinfo_query = 1;
5101 use_threadextra_query = 1;
5103 /* Without this, some commands which require an active target (such
5104 as kill) won't work. This variable serves (at least) double duty
5105 as both the pid of the target process (if it has such), and as a
5106 flag indicating that a target is active. These functions should
5107 be split out into seperate variables, especially since GDB will
5108 someday have a notion of debugging several processes. */
5109 inferior_pid = MAGIC_NULL_PID;
5111 /* Start the remote connection; if error (0), discard this target. */
5113 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5114 "Couldn't establish connection to remote target\n",
5123 remote_cisco_close (int quitting)
5125 remote_cisco_mode = 0;
5126 remote_close (quitting);
5130 remote_cisco_mourn (void)
5132 remote_mourn_1 (&remote_cisco_ops);
5144 /* shared between readsocket() and readtty() */
5145 static char *tty_input;
5147 static int escape_count;
5148 static int echo_check;
5149 extern int quit_flag;
5156 /* Loop until the socket doesn't have any more data */
5158 while ((data = readchar (0)) >= 0)
5160 /* Check for the escape sequence */
5163 /* If this is the fourth escape, get out */
5164 if (++escape_count == 4)
5169 { /* This is a '|', but not the fourth in a row.
5170 Continue without echoing it. If it isn't actually
5171 one of four in a row, it'll be echoed later. */
5178 /* Ensure any pending '|'s are flushed. */
5180 for (; escape_count > 0; escape_count--)
5184 if (data == '\r') /* If this is a return character, */
5185 continue; /* - just supress it. */
5187 if (echo_check != -1) /* Check for echo of user input. */
5189 if (tty_input[echo_check] == data)
5191 echo_check++; /* Character matched user input: */
5192 continue; /* Continue without echoing it. */
5194 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5195 { /* End of the line (and of echo checking). */
5196 echo_check = -1; /* No more echo supression */
5197 continue; /* Continue without echoing. */
5200 { /* Failed check for echo of user input.
5201 We now have some suppressed output to flush! */
5204 for (j = 0; j < echo_check; j++)
5205 putchar (tty_input[j]);
5209 putchar (data); /* Default case: output the char. */
5212 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5213 return READ_MORE; /* Try to read some more */
5215 return FATAL_ERROR; /* Trouble, bail out */
5223 /* First, read a buffer full from the terminal */
5224 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
5225 if (tty_bytecount == -1)
5227 perror ("readtty: read failed");
5231 /* Remove a quoted newline. */
5232 if (tty_input[tty_bytecount - 1] == '\n' &&
5233 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5235 tty_input[--tty_bytecount] = 0; /* remove newline */
5236 tty_input[--tty_bytecount] = 0; /* remove backslash */
5239 /* Turn trailing newlines into returns */
5240 if (tty_input[tty_bytecount - 1] == '\n')
5241 tty_input[tty_bytecount - 1] = '\r';
5243 /* If the line consists of a ~, enter debugging mode. */
5244 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5247 /* Make this a zero terminated string and write it out */
5248 tty_input[tty_bytecount] = 0;
5249 if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount))
5251 perror_with_name ("readtty: write failed");
5261 fd_set input; /* file descriptors for select */
5262 int tablesize; /* max number of FDs for select */
5266 extern int escape_count; /* global shared by readsocket */
5267 extern int echo_check; /* ditto */
5272 tablesize = 8 * sizeof (input);
5276 /* Check for anything from our socket - doesn't block. Note that
5277 this must be done *before* the select as there may be
5278 buffered I/O waiting to be processed. */
5280 if ((status = readsocket ()) == FATAL_ERROR)
5282 error ("Debugging terminated by communications error");
5284 else if (status != READ_MORE)
5289 fflush (stdout); /* Flush output before blocking */
5291 /* Now block on more socket input or TTY input */
5294 FD_SET (fileno (stdin), &input);
5295 FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
5297 status = select (tablesize, &input, 0, 0, 0);
5298 if ((status == -1) && (errno != EINTR))
5300 error ("Communications error on select %d", errno);
5303 /* Handle Control-C typed */
5307 if ((++quit_count) == 2)
5309 if (query ("Interrupt GDB? "))
5311 printf_filtered ("Interrupted by user.\n");
5312 return_to_top_level (RETURN_QUIT);
5319 SERIAL_SEND_BREAK (remote_desc);
5321 SERIAL_WRITE (remote_desc, "\003", 1);
5326 /* Handle console input */
5328 if (FD_ISSET (fileno (stdin), &input))
5332 status = readtty ();
5333 if (status == READ_MORE)
5336 return status; /* telnet session ended */
5342 remote_cisco_wait (int pid, struct target_waitstatus *status)
5344 if (minitelnet () != ENTER_DEBUG)
5346 error ("Debugging session terminated by protocol error");
5349 return remote_wait (pid, status);
5353 init_remote_cisco_ops ()
5355 remote_cisco_ops.to_shortname = "cisco";
5356 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5357 remote_cisco_ops.to_doc =
5358 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5359 Specify the serial device it is connected to (e.g. host:2020).";
5360 remote_cisco_ops.to_open = remote_cisco_open;
5361 remote_cisco_ops.to_close = remote_cisco_close;
5362 remote_cisco_ops.to_detach = remote_detach;
5363 remote_cisco_ops.to_resume = remote_resume;
5364 remote_cisco_ops.to_wait = remote_cisco_wait;
5365 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5366 remote_cisco_ops.to_store_registers = remote_store_registers;
5367 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5368 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5369 remote_cisco_ops.to_files_info = remote_files_info;
5370 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5371 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
5372 remote_cisco_ops.to_kill = remote_kill;
5373 remote_cisco_ops.to_load = generic_load;
5374 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5375 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5376 remote_cisco_ops.to_find_new_threads = remote_threads_info;
5377 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5378 remote_cisco_ops.to_stratum = process_stratum;
5379 remote_cisco_ops.to_has_all_memory = 1;
5380 remote_cisco_ops.to_has_memory = 1;
5381 remote_cisco_ops.to_has_stack = 1;
5382 remote_cisco_ops.to_has_registers = 1;
5383 remote_cisco_ops.to_has_execution = 1;
5384 remote_cisco_ops.to_magic = OPS_MAGIC;
5388 remote_can_async_p (void)
5390 /* We're async whenever the serial device is. */
5391 return (current_target.to_async_mask_value) && SERIAL_CAN_ASYNC_P (remote_desc);
5395 remote_is_async_p (void)
5397 /* We're async whenever the serial device is. */
5398 return (current_target.to_async_mask_value) && SERIAL_IS_ASYNC_P (remote_desc);
5401 /* Pass the SERIAL event on and up to the client. One day this code
5402 will be able to delay notifying the client of an event until the
5403 point where an entire packet has been received. */
5405 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5406 static void *async_client_context;
5407 static serial_event_ftype remote_async_serial_handler;
5410 remote_async_serial_handler (serial_t scb, void *context)
5412 /* Don't propogate error information up to the client. Instead let
5413 the client find out about the error by querying the target. */
5414 async_client_callback (INF_REG_EVENT, async_client_context);
5418 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5420 if (current_target.to_async_mask_value == 0)
5421 internal_error ("Calling remote_async when async is masked");
5423 if (callback != NULL)
5425 SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
5426 async_client_callback = callback;
5427 async_client_context = context;
5430 SERIAL_ASYNC (remote_desc, NULL, NULL);
5433 /* Target async and target extended-async.
5435 This are temporary targets, until it is all tested. Eventually
5436 async support will be incorporated int the usual 'remote'
5440 init_remote_async_ops (void)
5442 remote_async_ops.to_shortname = "async";
5443 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5444 remote_async_ops.to_doc =
5445 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5446 Specify the serial device it is connected to (e.g. /dev/ttya).";
5447 remote_async_ops.to_open = remote_async_open;
5448 remote_async_ops.to_close = remote_close;
5449 remote_async_ops.to_detach = remote_async_detach;
5450 remote_async_ops.to_resume = remote_async_resume;
5451 remote_async_ops.to_wait = remote_async_wait;
5452 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5453 remote_async_ops.to_store_registers = remote_store_registers;
5454 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5455 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5456 remote_async_ops.to_files_info = remote_files_info;
5457 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5458 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5459 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5460 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5461 remote_async_ops.to_kill = remote_async_kill;
5462 remote_async_ops.to_load = generic_load;
5463 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5464 remote_async_ops.to_thread_alive = remote_thread_alive;
5465 remote_async_ops.to_find_new_threads = remote_threads_info;
5466 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5467 remote_async_ops.to_stop = remote_stop;
5468 remote_async_ops.to_query = remote_query;
5469 remote_async_ops.to_rcmd = remote_rcmd;
5470 remote_async_ops.to_stratum = process_stratum;
5471 remote_async_ops.to_has_all_memory = 1;
5472 remote_async_ops.to_has_memory = 1;
5473 remote_async_ops.to_has_stack = 1;
5474 remote_async_ops.to_has_registers = 1;
5475 remote_async_ops.to_has_execution = 1;
5476 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5477 remote_async_ops.to_can_async_p = remote_can_async_p;
5478 remote_async_ops.to_is_async_p = remote_is_async_p;
5479 remote_async_ops.to_async = remote_async;
5480 remote_async_ops.to_async_mask_value = 1;
5481 remote_async_ops.to_magic = OPS_MAGIC;
5484 /* Set up the async extended remote vector by making a copy of the standard
5485 remote vector and adding to it. */
5488 init_extended_async_remote_ops (void)
5490 extended_async_remote_ops = remote_async_ops;
5492 extended_async_remote_ops.to_shortname = "extended-async";
5493 extended_async_remote_ops.to_longname =
5494 "Extended remote serial target in async gdb-specific protocol";
5495 extended_async_remote_ops.to_doc =
5496 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5497 Specify the serial device it is connected to (e.g. /dev/ttya).",
5498 extended_async_remote_ops.to_open = extended_remote_async_open;
5499 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5500 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5504 set_remote_cmd (char *args, int from_tty)
5511 build_remote_gdbarch_data ()
5513 build_remote_packet_sizes ();
5516 tty_input = xmalloc (PBUFSIZ);
5517 remote_address_size = TARGET_PTR_BIT;
5521 _initialize_remote ()
5523 static struct cmd_list_element *remote_set_cmdlist;
5524 static struct cmd_list_element *remote_show_cmdlist;
5525 struct cmd_list_element *tmpcmd;
5527 /* architecture specific data */
5528 build_remote_gdbarch_data ();
5529 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
5530 register_remote_packet_sizes ();
5531 register_gdbarch_swap (&remote_address_size,
5532 sizeof (&remote_address_size), NULL);
5533 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5536 add_target (&remote_ops);
5538 init_extended_remote_ops ();
5539 add_target (&extended_remote_ops);
5541 init_remote_async_ops ();
5542 add_target (&remote_async_ops);
5544 init_extended_async_remote_ops ();
5545 add_target (&extended_async_remote_ops);
5547 init_remote_cisco_ops ();
5548 add_target (&remote_cisco_ops);
5551 init_remote_threadtests ();
5554 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5555 Remote protocol specific variables\n\
5556 Configure various remote-protocol specific variables such as\n\
5557 the packets being used",
5558 &remote_set_cmdlist, "set remote ",
5559 0/*allow-unknown*/, &setlist);
5560 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5561 Remote protocol specific variables\n\
5562 Configure various remote-protocol specific variables such as\n\
5563 the packets being used",
5564 &remote_show_cmdlist, "show remote ",
5565 0/*allow-unknown*/, &showlist);
5567 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5568 "Compare section data on target to the exec file.\n\
5569 Argument is a single section name (default: all loaded sections).",
5572 add_cmd ("packet", class_maintenance, packet_command,
5573 "Send an arbitrary packet to a remote target.\n\
5574 maintenance packet TEXT\n\
5575 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5576 this command sends the string TEXT to the inferior, and displays the\n\
5577 response packet. GDB supplies the initial `$' character, and the\n\
5578 terminating `#' character and checksum.",
5582 (add_set_cmd ("remotebreak", no_class,
5583 var_boolean, (char *) &remote_break,
5584 "Set whether to send break if interrupted.\n",
5588 /* Install commands for configuring memory read/write packets. */
5590 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5591 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5593 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5594 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5596 add_cmd ("memory-write-packet-size", no_class,
5597 set_memory_write_packet_size,
5598 "Set the maximum number of bytes per memory-write packet.\n"
5599 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5600 "default packet size. The actual limit is further reduced\n"
5601 "dependent on the target. Specify ``fixed'' to disable the\n"
5602 "further restriction and ``limit'' to enable that restriction\n",
5603 &remote_set_cmdlist);
5604 add_cmd ("memory-read-packet-size", no_class,
5605 set_memory_read_packet_size,
5606 "Set the maximum number of bytes per memory-read packet.\n"
5607 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5608 "default packet size. The actual limit is further reduced\n"
5609 "dependent on the target. Specify ``fixed'' to disable the\n"
5610 "further restriction and ``limit'' to enable that restriction\n",
5611 &remote_set_cmdlist);
5612 add_cmd ("memory-write-packet-size", no_class,
5613 show_memory_write_packet_size,
5614 "Show the maximum number of bytes per memory-write packet.\n",
5615 &remote_show_cmdlist);
5616 add_cmd ("memory-read-packet-size", no_class,
5617 show_memory_read_packet_size,
5618 "Show the maximum number of bytes per memory-read packet.\n",
5619 &remote_show_cmdlist);
5622 (add_set_cmd ("remoteaddresssize", class_obscure,
5623 var_integer, (char *) &remote_address_size,
5624 "Set the maximum size of the address (in bits) \
5625 in a memory packet.\n",
5629 add_packet_config_cmd (&remote_protocol_binary_download,
5630 "X", "binary-download",
5631 set_remote_protocol_binary_download_cmd,
5632 show_remote_protocol_binary_download_cmd,
5633 &remote_set_cmdlist, &remote_show_cmdlist);
5635 /* XXXX - should ``set remotebinarydownload'' be retained for
5638 (add_set_cmd ("remotebinarydownload", no_class,
5639 var_boolean, (char *) &remote_binary_download,
5640 "Set binary downloads.\n", &setlist),
5644 add_info ("remote-process", remote_info_process,
5645 "Query the remote system for process info.");
5647 add_packet_config_cmd (&remote_protocol_P, "P", "set-register",
5648 set_remote_protocol_P_packet_cmd,
5649 show_remote_protocol_P_packet_cmd,
5650 &remote_set_cmdlist, &remote_show_cmdlist);
5652 add_packet_config_cmd (&remote_protocol_Z, "Z", "breakpoint",
5653 set_remote_protocol_Z_packet_cmd,
5654 show_remote_protocol_Z_packet_cmd,
5655 &remote_set_cmdlist, &remote_show_cmdlist);