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 char packet_support_auto[] = "auto";
537 static char packet_enable[] = "enable";
538 static char packet_disable[] = "disable";
539 static 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) PARAMS ((char *args, int from_tty,
607 struct cmd_list_element * c));
608 void (*show_func) PARAMS ((char *name, int from_tty));
609 struct cmd_list_element **setlist;
610 struct cmd_list_element **showlist;
612 struct cmd_list_element *c;
617 config->title = title;
618 asprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
620 asprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
622 asprintf (&full_name, "%s-packet", name);
623 c = add_set_enum_cmd (full_name,
624 class_obscure, packet_support_enums,
627 c->function.sfunc = set_func;
628 add_cmd (full_name, class_obscure, show_func, show_doc, showlist);
632 init_packet_config (config)
633 struct packet_config *config;
635 switch (config->detect)
637 case PACKET_AUTO_DETECT:
638 config->support = PACKET_SUPPORT_UNKNOWN;
640 case PACKET_MANUAL_DETECT:
641 /* let the user beware */
646 /* Should we try the 'P' (set register) request? */
648 static struct packet_config remote_protocol_P;
651 set_remote_protocol_P_packet_cmd (args, from_tty, c)
654 struct cmd_list_element *c;
656 set_packet_config_cmd (&remote_protocol_P, c);
660 show_remote_protocol_P_packet_cmd (args, from_tty)
664 show_packet_config_cmd (&remote_protocol_P);
667 /* Should we try the 'Z' (set breakpoint) request? */
669 static struct packet_config remote_protocol_Z;
672 set_remote_protocol_Z_packet_cmd (args, from_tty, c)
675 struct cmd_list_element *c;
677 set_packet_config_cmd (&remote_protocol_Z, c);
681 show_remote_protocol_Z_packet_cmd (args, from_tty)
685 show_packet_config_cmd (&remote_protocol_Z);
688 /* Should we try the 'X' (remote binary download) packet?
690 This variable (available to the user via "set remote X-packet")
691 dictates whether downloads are sent in binary (via the 'X' packet).
692 We assume that the stub can, and attempt to do it. This will be
693 cleared if the stub does not understand it. This switch is still
694 needed, though in cases when the packet is supported in the stub,
695 but the connection does not allow it (i.e., 7-bit serial connection
698 static struct packet_config remote_protocol_binary_download;
700 /* Should we try the 'ThreadInfo' query packet?
702 This variable (NOT available to the user: auto-detect only!)
703 determines whether GDB will use the new, simpler "ThreadInfo"
704 query or the older, more complex syntax for thread queries.
705 This is an auto-detect variable (set to true at each connect,
706 and set to false when the target fails to recognize it). */
708 static int use_threadinfo_query;
709 static int use_threadextra_query;
712 set_remote_protocol_binary_download_cmd (char *args,
714 struct cmd_list_element *c)
716 set_packet_config_cmd (&remote_protocol_binary_download, c);
720 show_remote_protocol_binary_download_cmd (char *args,
723 show_packet_config_cmd (&remote_protocol_binary_download);
727 /* Tokens for use by the asynchronous signal handlers for SIGINT */
728 PTR sigint_remote_twice_token;
729 PTR sigint_remote_token;
731 /* These are pointers to hook functions that may be set in order to
732 modify resume/wait behavior for a particular architecture. */
734 void (*target_resume_hook) PARAMS ((void));
735 void (*target_wait_loop_hook) PARAMS ((void));
739 /* These are the threads which we last sent to the remote system.
740 -1 for all or -2 for not sent yet. */
741 static int general_thread;
742 static int continue_thread;
744 /* Call this function as a result of
745 1) A halt indication (T packet) containing a thread id
746 2) A direct query of currthread
747 3) Successful execution of set thread
751 record_currthread (currthread)
754 general_thread = currthread;
756 /* If this is a new thread, add it to GDB's thread list.
757 If we leave it up to WFI to do this, bad things will happen. */
758 if (!in_thread_list (currthread))
760 add_thread (currthread);
762 ui_out_text (uiout, "[New ");
763 ui_out_text (uiout, target_pid_to_str (currthread));
764 ui_out_text (uiout, "]\n");
766 printf_filtered ("[New %s]\n", target_pid_to_str (currthread));
771 #define MAGIC_NULL_PID 42000
778 char *buf = alloca (PBUFSIZ);
779 int state = gen ? general_thread : continue_thread;
785 buf[1] = gen ? 'g' : 'c';
786 if (th == MAGIC_NULL_PID)
792 sprintf (&buf[2], "-%x", -th);
794 sprintf (&buf[2], "%x", th);
796 getpkt (buf, PBUFSIZ, 0);
800 continue_thread = th;
803 /* Return nonzero if the thread TH is still alive on the remote system. */
806 remote_thread_alive (tid)
812 sprintf (buf, "T-%08x", -tid);
814 sprintf (buf, "T%08x", tid);
816 getpkt (buf, sizeof (buf), 0);
817 return (buf[0] == 'O' && buf[1] == 'K');
820 /* About these extended threadlist and threadinfo packets. They are
821 variable length packets but, the fields within them are often fixed
822 length. They are redundent enough to send over UDP as is the
823 remote protocol in general. There is a matching unit test module
826 #define OPAQUETHREADBYTES 8
828 /* a 64 bit opaque identifier */
829 typedef unsigned char threadref[OPAQUETHREADBYTES];
831 /* WARNING: This threadref data structure comes from the remote O.S., libstub
832 protocol encoding, and remote.c. it is not particularly changable */
834 /* Right now, the internal structure is int. We want it to be bigger.
838 typedef int gdb_threadref; /* internal GDB thread reference */
840 /* gdb_ext_thread_info is an internal GDB data structure which is
841 equivalint to the reply of the remote threadinfo packet */
843 struct gdb_ext_thread_info
845 threadref threadid; /* External form of thread reference */
846 int active; /* Has state interesting to GDB? , regs, stack */
847 char display[256]; /* Brief state display, name, blocked/syspended */
848 char shortname[32]; /* To be used to name threads */
849 char more_display[256]; /* Long info, statistics, queue depth, whatever */
852 /* The volume of remote transfers can be limited by submitting
853 a mask containing bits specifying the desired information.
854 Use a union of these values as the 'selection' parameter to
855 get_thread_info. FIXME: Make these TAG names more thread specific.
858 #define TAG_THREADID 1
860 #define TAG_DISPLAY 4
861 #define TAG_THREADNAME 8
862 #define TAG_MOREDISPLAY 16
864 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
866 char *unpack_varlen_hex (char *buff, int *result);
868 static char *unpack_nibble (char *buf, int *val);
870 static char *pack_nibble (char *buf, int nibble);
872 static char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
874 static char *unpack_byte (char *buf, int *value);
876 static char *pack_int (char *buf, int value);
878 static char *unpack_int (char *buf, int *value);
880 static char *unpack_string (char *src, char *dest, int length);
882 static char *pack_threadid (char *pkt, threadref * id);
884 static char *unpack_threadid (char *inbuf, threadref * id);
886 void int_to_threadref (threadref * id, int value);
888 static int threadref_to_int (threadref * ref);
890 static void copy_threadref (threadref * dest, threadref * src);
892 static int threadmatch (threadref * dest, threadref * src);
894 static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
896 static int remote_unpack_thread_info_response (char *pkt,
897 threadref * expectedref,
898 struct gdb_ext_thread_info
902 static int remote_get_threadinfo (threadref * threadid, int fieldset, /*TAG mask */
903 struct gdb_ext_thread_info *info);
905 static int adapt_remote_get_threadinfo (gdb_threadref * ref,
907 struct gdb_ext_thread_info *info);
909 static char *pack_threadlist_request (char *pkt, int startflag,
911 threadref * nextthread);
913 static int parse_threadlist_response (char *pkt,
915 threadref * original_echo,
916 threadref * resultlist, int *doneflag);
918 static int remote_get_threadlist (int startflag,
919 threadref * nextthread,
922 int *result_count, threadref * threadlist);
924 typedef int (*rmt_thread_action) (threadref * ref, void *context);
926 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
927 void *context, int looplimit);
929 static int remote_newthread_step (threadref * ref, void *context);
931 /* encode 64 bits in 16 chars of hex */
933 static const char hexchars[] = "0123456789abcdef";
940 if ((ch >= 'a') && (ch <= 'f'))
942 *val = ch - 'a' + 10;
945 if ((ch >= 'A') && (ch <= 'F'))
947 *val = ch - 'A' + 10;
950 if ((ch >= '0') && (ch <= '9'))
962 if (ch >= 'a' && ch <= 'f')
963 return ch - 'a' + 10;
964 if (ch >= '0' && ch <= '9')
966 if (ch >= 'A' && ch <= 'F')
967 return ch - 'A' + 10;
972 stub_unpack_int (buff, fieldlength)
981 nibble = stubhex (*buff++);
985 retval = retval << 4;
991 unpack_varlen_hex (buff, result)
992 char *buff; /* packet to parse */
998 while (ishex (*buff, &nibble))
1001 retval = retval << 4;
1002 retval |= nibble & 0x0f;
1009 unpack_nibble (buf, val)
1013 ishex (*buf++, val);
1018 pack_nibble (buf, nibble)
1022 *buf++ = hexchars[(nibble & 0x0f)];
1027 pack_hex_byte (pkt, byte)
1031 *pkt++ = hexchars[(byte >> 4) & 0xf];
1032 *pkt++ = hexchars[(byte & 0xf)];
1037 unpack_byte (buf, value)
1041 *value = stub_unpack_int (buf, 2);
1046 pack_int (buf, value)
1050 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1051 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1052 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1053 buf = pack_hex_byte (buf, (value & 0xff));
1058 unpack_int (buf, value)
1062 *value = stub_unpack_int (buf, 8);
1066 #if 0 /* currently unused, uncomment when needed */
1067 static char *pack_string (char *pkt, char *string);
1070 pack_string (pkt, string)
1077 len = strlen (string);
1079 len = 200; /* Bigger than most GDB packets, junk??? */
1080 pkt = pack_hex_byte (pkt, len);
1084 if ((ch == '\0') || (ch == '#'))
1085 ch = '*'; /* Protect encapsulation */
1090 #endif /* 0 (unused) */
1093 unpack_string (src, dest, length)
1105 pack_threadid (pkt, id)
1110 unsigned char *altid;
1112 altid = (unsigned char *) id;
1113 limit = pkt + BUF_THREAD_ID_SIZE;
1115 pkt = pack_hex_byte (pkt, *altid++);
1121 unpack_threadid (inbuf, id)
1126 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1129 altref = (char *) id;
1131 while (inbuf < limit)
1133 x = stubhex (*inbuf++);
1134 y = stubhex (*inbuf++);
1135 *altref++ = (x << 4) | y;
1140 /* Externally, threadrefs are 64 bits but internally, they are still
1141 ints. This is due to a mismatch of specifications. We would like
1142 to use 64bit thread references internally. This is an adapter
1146 int_to_threadref (id, value)
1150 unsigned char *scan;
1152 scan = (unsigned char *) id;
1158 *scan++ = (value >> 24) & 0xff;
1159 *scan++ = (value >> 16) & 0xff;
1160 *scan++ = (value >> 8) & 0xff;
1161 *scan++ = (value & 0xff);
1165 threadref_to_int (ref)
1169 unsigned char *scan;
1171 scan = (char *) ref;
1175 value = (value << 8) | ((*scan++) & 0xff);
1180 copy_threadref (dest, src)
1185 unsigned char *csrc, *cdest;
1187 csrc = (unsigned char *) src;
1188 cdest = (unsigned char *) dest;
1195 threadmatch (dest, src)
1199 /* things are broken right now, so just assume we got a match */
1201 unsigned char *srcp, *destp;
1203 srcp = (char *) src;
1204 destp = (char *) dest;
1208 result &= (*srcp++ == *destp++) ? 1 : 0;
1215 threadid:1, # always request threadid
1222 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1225 pack_threadinfo_request (pkt, mode, id)
1230 *pkt++ = 'q'; /* Info Query */
1231 *pkt++ = 'P'; /* process or thread info */
1232 pkt = pack_int (pkt, mode); /* mode */
1233 pkt = pack_threadid (pkt, id); /* threadid */
1234 *pkt = '\0'; /* terminate */
1238 /* These values tag the fields in a thread info response packet */
1239 /* Tagging the fields allows us to request specific fields and to
1240 add more fields as time goes by */
1242 #define TAG_THREADID 1 /* Echo the thread identifier */
1243 #define TAG_EXISTS 2 /* Is this process defined enough to
1244 fetch registers and its stack */
1245 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1246 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1247 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1251 remote_unpack_thread_info_response (pkt, expectedref, info)
1253 threadref *expectedref;
1254 struct gdb_ext_thread_info *info;
1259 char *limit = pkt + PBUFSIZ; /* plausable parsing limit */
1262 /* info->threadid = 0; FIXME: implement zero_threadref */
1264 info->display[0] = '\0';
1265 info->shortname[0] = '\0';
1266 info->more_display[0] = '\0';
1268 /* Assume the characters indicating the packet type have been stripped */
1269 pkt = unpack_int (pkt, &mask); /* arg mask */
1270 pkt = unpack_threadid (pkt, &ref);
1273 warning ("Incomplete response to threadinfo request\n");
1274 if (!threadmatch (&ref, expectedref))
1275 { /* This is an answer to a different request */
1276 warning ("ERROR RMT Thread info mismatch\n");
1279 copy_threadref (&info->threadid, &ref);
1281 /* Loop on tagged fields , try to bail if somthing goes wrong */
1283 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
1285 pkt = unpack_int (pkt, &tag); /* tag */
1286 pkt = unpack_byte (pkt, &length); /* length */
1287 if (!(tag & mask)) /* tags out of synch with mask */
1289 warning ("ERROR RMT: threadinfo tag mismatch\n");
1293 if (tag == TAG_THREADID)
1297 warning ("ERROR RMT: length of threadid is not 16\n");
1301 pkt = unpack_threadid (pkt, &ref);
1302 mask = mask & ~TAG_THREADID;
1305 if (tag == TAG_EXISTS)
1307 info->active = stub_unpack_int (pkt, length);
1309 mask = mask & ~(TAG_EXISTS);
1312 warning ("ERROR RMT: 'exists' length too long\n");
1318 if (tag == TAG_THREADNAME)
1320 pkt = unpack_string (pkt, &info->shortname[0], length);
1321 mask = mask & ~TAG_THREADNAME;
1324 if (tag == TAG_DISPLAY)
1326 pkt = unpack_string (pkt, &info->display[0], length);
1327 mask = mask & ~TAG_DISPLAY;
1330 if (tag == TAG_MOREDISPLAY)
1332 pkt = unpack_string (pkt, &info->more_display[0], length);
1333 mask = mask & ~TAG_MOREDISPLAY;
1336 warning ("ERROR RMT: unknown thread info tag\n");
1337 break; /* Not a tag we know about */
1343 remote_get_threadinfo (threadid, fieldset, info)
1344 threadref *threadid;
1345 int fieldset; /* TAG mask */
1346 struct gdb_ext_thread_info *info;
1349 char *threadinfo_pkt = alloca (PBUFSIZ);
1351 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1352 putpkt (threadinfo_pkt);
1353 getpkt (threadinfo_pkt, PBUFSIZ, 0);
1354 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1359 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1360 representation of a threadid. */
1363 adapt_remote_get_threadinfo (ref, selection, info)
1366 struct gdb_ext_thread_info *info;
1370 int_to_threadref (&lclref, *ref);
1371 return remote_get_threadinfo (&lclref, selection, info);
1374 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1377 pack_threadlist_request (pkt, startflag, threadcount, nextthread)
1381 threadref *nextthread;
1383 *pkt++ = 'q'; /* info query packet */
1384 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1385 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1386 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1387 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1392 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1395 parse_threadlist_response (pkt, result_limit, original_echo, resultlist,
1399 threadref *original_echo;
1400 threadref *resultlist;
1404 int count, resultcount, done;
1407 /* Assume the 'q' and 'M chars have been stripped. */
1408 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */
1409 pkt = unpack_byte (pkt, &count); /* count field */
1410 pkt = unpack_nibble (pkt, &done);
1411 /* The first threadid is the argument threadid. */
1412 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1413 while ((count-- > 0) && (pkt < limit))
1415 pkt = unpack_threadid (pkt, resultlist++);
1416 if (resultcount++ >= result_limit)
1425 remote_get_threadlist (startflag, nextthread, result_limit,
1426 done, result_count, threadlist)
1428 threadref *nextthread;
1432 threadref *threadlist;
1435 static threadref echo_nextthread;
1436 char *threadlist_packet = alloca (PBUFSIZ);
1437 char *t_response = alloca (PBUFSIZ);
1440 /* Trancate result limit to be smaller than the packet size */
1441 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
1442 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
1444 pack_threadlist_request (threadlist_packet,
1445 startflag, result_limit, nextthread);
1446 putpkt (threadlist_packet);
1447 getpkt (t_response, PBUFSIZ, 0);
1450 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1453 if (!threadmatch (&echo_nextthread, nextthread))
1455 /* FIXME: This is a good reason to drop the packet */
1456 /* Possably, there is a duplicate response */
1458 retransmit immediatly - race conditions
1459 retransmit after timeout - yes
1461 wait for packet, then exit
1463 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1464 return 0; /* I choose simply exiting */
1466 if (*result_count <= 0)
1470 warning ("RMT ERROR : failed to get remote thread list\n");
1473 return result; /* break; */
1475 if (*result_count > result_limit)
1478 warning ("RMT ERROR: threadlist response longer than requested\n");
1484 /* This is the interface between remote and threads, remotes upper interface */
1486 /* remote_find_new_threads retrieves the thread list and for each
1487 thread in the list, looks up the thread in GDB's internal list,
1488 ading the thread if it does not already exist. This involves
1489 getting partial thread lists from the remote target so, polling the
1490 quit_flag is required. */
1493 /* About this many threadisds fit in a packet. */
1495 #define MAXTHREADLISTRESULTS 32
1498 remote_threadlist_iterator (stepfunction, context, looplimit)
1499 rmt_thread_action stepfunction;
1503 int done, i, result_count;
1507 static threadref nextthread;
1508 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1513 if (loopcount++ > looplimit)
1516 warning ("Remote fetch threadlist -infinite loop-\n");
1519 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1520 &done, &result_count, resultthreadlist))
1525 /* clear for later iterations */
1527 /* Setup to resume next batch of thread references, set nextthread. */
1528 if (result_count >= 1)
1529 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1531 while (result_count--)
1532 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1539 remote_newthread_step (ref, context)
1545 pid = threadref_to_int (ref);
1546 if (!in_thread_list (pid))
1548 return 1; /* continue iterator */
1551 #define CRAZY_MAX_THREADS 1000
1554 remote_current_thread (oldpid)
1557 char *buf = alloca (PBUFSIZ);
1560 getpkt (buf, PBUFSIZ, 0);
1561 if (buf[0] == 'Q' && buf[1] == 'C')
1562 return strtol (&buf[2], NULL, 16);
1567 /* Find new threads for info threads command.
1568 * Original version, using John Metzler's thread protocol.
1572 remote_find_new_threads ()
1574 remote_threadlist_iterator (remote_newthread_step, 0,
1576 if (inferior_pid == MAGIC_NULL_PID) /* ack ack ack */
1577 inferior_pid = remote_current_thread (inferior_pid);
1581 * Find all threads for info threads command.
1582 * Uses new thread protocol contributed by Cisco.
1583 * Falls back and attempts to use the older method (above)
1584 * if the target doesn't respond to the new method.
1588 remote_threads_info (void)
1590 char *buf = alloca (PBUFSIZ);
1594 if (remote_desc == 0) /* paranoia */
1595 error ("Command can only be used when connected to the remote target.");
1597 if (use_threadinfo_query)
1599 putpkt ("qfThreadInfo");
1601 getpkt (bufp, PBUFSIZ, 0);
1602 if (bufp[0] != '\0') /* q packet recognized */
1604 while (*bufp++ == 'm') /* reply contains one or more TID */
1608 tid = strtol (bufp, &bufp, 16);
1609 if (tid != 0 && !in_thread_list (tid))
1612 while (*bufp++ == ','); /* comma-separated list */
1613 putpkt ("qsThreadInfo");
1615 getpkt (bufp, PBUFSIZ, 0);
1621 /* Else fall back to old method based on jmetzler protocol. */
1622 use_threadinfo_query = 0;
1623 remote_find_new_threads ();
1628 * Collect a descriptive string about the given thread.
1629 * The target may say anything it wants to about the thread
1630 * (typically info about its blocked / runnable state, name, etc.).
1631 * This string will appear in the info threads display.
1633 * Optional: targets are not required to implement this function.
1637 remote_threads_extra_info (struct thread_info *tp)
1642 struct gdb_ext_thread_info threadinfo;
1643 static char display_buf[100]; /* arbitrary... */
1644 char *bufp = alloca (PBUFSIZ);
1645 int n = 0; /* position in display_buf */
1647 if (remote_desc == 0) /* paranoia */
1648 internal_error ("remote_threads_extra_info");
1650 if (use_threadextra_query)
1652 sprintf (bufp, "qThreadExtraInfo,%x", tp->pid);
1654 getpkt (bufp, PBUFSIZ, 0);
1659 for (p = display_buf;
1660 p < display_buf + sizeof(display_buf) - 1 &&
1665 *p = fromhex (bufp[0]) * 16 + fromhex (bufp[1]);
1672 /* If the above query fails, fall back to the old method. */
1673 use_threadextra_query = 0;
1674 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1675 | TAG_MOREDISPLAY | TAG_DISPLAY;
1676 int_to_threadref (&id, tp->pid);
1677 if (remote_get_threadinfo (&id, set, &threadinfo))
1678 if (threadinfo.active)
1680 if (*threadinfo.shortname)
1681 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1682 if (*threadinfo.display)
1683 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1684 if (*threadinfo.more_display)
1685 n += sprintf(&display_buf[n], " Priority: %s",
1686 threadinfo.more_display);
1690 /* for purely cosmetic reasons, clear up trailing commas */
1691 if (',' == display_buf[n-1])
1692 display_buf[n-1] = ' ';
1701 /* Restart the remote side; this is an extended protocol operation. */
1704 extended_remote_restart ()
1706 char *buf = alloca (PBUFSIZ);
1708 /* Send the restart command; for reasons I don't understand the
1709 remote side really expects a number after the "R". */
1711 sprintf (&buf[1], "%x", 0);
1714 /* Now query for status so this looks just like we restarted
1715 gdbserver from scratch. */
1717 getpkt (buf, PBUFSIZ, 0);
1720 /* Clean up connection to a remote debugger. */
1724 remote_close (quitting)
1728 SERIAL_CLOSE (remote_desc);
1732 /* Query the remote side for the text, data and bss offsets. */
1737 char *buf = alloca (PBUFSIZ);
1740 CORE_ADDR text_addr, data_addr, bss_addr;
1741 struct section_offsets *offs;
1743 putpkt ("qOffsets");
1745 getpkt (buf, PBUFSIZ, 0);
1747 if (buf[0] == '\000')
1748 return; /* Return silently. Stub doesn't support
1752 warning ("Remote failure reply: %s", buf);
1756 /* Pick up each field in turn. This used to be done with scanf, but
1757 scanf will make trouble if CORE_ADDR size doesn't match
1758 conversion directives correctly. The following code will work
1759 with any size of CORE_ADDR. */
1760 text_addr = data_addr = bss_addr = 0;
1764 if (strncmp (ptr, "Text=", 5) == 0)
1767 /* Don't use strtol, could lose on big values. */
1768 while (*ptr && *ptr != ';')
1769 text_addr = (text_addr << 4) + fromhex (*ptr++);
1774 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1777 while (*ptr && *ptr != ';')
1778 data_addr = (data_addr << 4) + fromhex (*ptr++);
1783 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1786 while (*ptr && *ptr != ';')
1787 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1793 error ("Malformed response to offset query, %s", buf);
1795 if (symfile_objfile == NULL)
1798 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1799 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1801 ANOFFSET (offs, SECT_OFF_TEXT (symfile_objfile)) = text_addr;
1803 /* This is a temporary kludge to force data and bss to use the same offsets
1804 because that's what nlmconv does now. The real solution requires changes
1805 to the stub and remote.c that I don't have time to do right now. */
1807 ANOFFSET (offs, SECT_OFF_DATA (symfile_objfile)) = data_addr;
1808 ANOFFSET (offs, SECT_OFF_BSS (symfile_objfile)) = data_addr;
1810 objfile_relocate (symfile_objfile, offs);
1814 * Cisco version of section offsets:
1816 * Instead of having GDB query the target for the section offsets,
1817 * Cisco lets the target volunteer the information! It's also in
1818 * a different format, so here are the functions that will decode
1819 * a section offset packet from a Cisco target.
1823 * Function: remote_cisco_section_offsets
1825 * Returns: zero for success, non-zero for failure
1829 remote_cisco_section_offsets (bfd_vma text_addr,
1832 bfd_signed_vma *text_offs,
1833 bfd_signed_vma *data_offs,
1834 bfd_signed_vma *bss_offs)
1836 bfd_vma text_base, data_base, bss_base;
1837 struct minimal_symbol *start;
1843 if (symfile_objfile == NULL)
1844 return -1; /* no can do nothin' */
1846 start = lookup_minimal_symbol ("_start", NULL, NULL);
1848 return -1; /* Can't find "_start" symbol */
1850 data_base = bss_base = 0;
1851 text_base = SYMBOL_VALUE_ADDRESS (start);
1853 abfd = symfile_objfile->obfd;
1854 for (sect = abfd->sections;
1858 p = (unsigned char *) bfd_get_section_name (abfd, sect);
1860 if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */
1861 if (data_base == 0 ||
1862 data_base > bfd_get_section_vma (abfd, sect))
1863 data_base = bfd_get_section_vma (abfd, sect);
1864 if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */
1865 if (bss_base == 0 ||
1866 bss_base > bfd_get_section_vma (abfd, sect))
1867 bss_base = bfd_get_section_vma (abfd, sect);
1869 *text_offs = text_addr - text_base;
1870 *data_offs = data_addr - data_base;
1871 *bss_offs = bss_addr - bss_base;
1876 sprintf (tmp, "VMA: text = 0x");
1877 sprintf_vma (tmp + strlen (tmp), text_addr);
1878 sprintf (tmp + strlen (tmp), " data = 0x");
1879 sprintf_vma (tmp + strlen (tmp), data_addr);
1880 sprintf (tmp + strlen (tmp), " bss = 0x");
1881 sprintf_vma (tmp + strlen (tmp), bss_addr);
1882 fprintf_filtered (gdb_stdlog, tmp);
1883 fprintf_filtered (gdb_stdlog,
1884 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1885 paddr_nz (*text_offs),
1886 paddr_nz (*data_offs),
1887 paddr_nz (*bss_offs));
1894 * Function: remote_cisco_objfile_relocate
1896 * Relocate the symbol file for a remote target.
1900 remote_cisco_objfile_relocate (text_off, data_off, bss_off)
1901 bfd_signed_vma text_off;
1902 bfd_signed_vma data_off;
1903 bfd_signed_vma bss_off;
1905 struct section_offsets *offs;
1907 if (text_off != 0 || data_off != 0 || bss_off != 0)
1909 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1910 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1911 simple canonical representation for this stuff. */
1913 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1914 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
1916 ANOFFSET (offs, SECT_OFF_TEXT (symfile_objfile)) = text_off;
1917 ANOFFSET (offs, SECT_OFF_DATA (symfile_objfile)) = data_off;
1918 ANOFFSET (offs, SECT_OFF_BSS (symfile_objfile)) = bss_off;
1920 /* First call the standard objfile_relocate. */
1921 objfile_relocate (symfile_objfile, offs);
1923 /* Now we need to fix up the section entries already attached to
1924 the exec target. These entries will control memory transfers
1925 from the exec file. */
1927 exec_set_section_offsets (text_off, data_off, bss_off);
1931 /* Stub for catch_errors. */
1934 remote_start_remote_dummy (void *dummy)
1936 start_remote (); /* Initialize gdb process mechanisms */
1941 remote_start_remote (dummy)
1944 immediate_quit = 1; /* Allow user to interrupt it */
1946 /* Ack any packet which the remote side has already sent. */
1947 SERIAL_WRITE (remote_desc, "+", 1);
1949 /* Let the stub know that we want it to return the thread. */
1952 inferior_pid = remote_current_thread (inferior_pid);
1954 get_offsets (); /* Get text, data & bss offsets */
1956 putpkt ("?"); /* initiate a query from remote machine */
1959 return remote_start_remote_dummy (dummy);
1962 /* Open a connection to a remote debugger.
1963 NAME is the filename used for communication. */
1966 remote_open (name, from_tty)
1970 remote_open_1 (name, from_tty, &remote_ops, 0);
1973 /* Just like remote_open, but with asynchronous support. */
1975 remote_async_open (name, from_tty)
1979 remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
1982 /* Open a connection to a remote debugger using the extended
1983 remote gdb protocol. NAME is the filename used for communication. */
1986 extended_remote_open (name, from_tty)
1990 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ );
1993 /* Just like extended_remote_open, but with asynchronous support. */
1995 extended_remote_async_open (name, from_tty)
1999 remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ );
2002 /* Generic code for opening a connection to a remote target. */
2004 static DCACHE *remote_dcache;
2007 remote_open_1 (name, from_tty, target, extended_p)
2010 struct target_ops *target;
2014 error ("To open a remote debug connection, you need to specify what\n\
2015 serial device is attached to the remote system\n\
2016 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2018 /* See FIXME above */
2019 wait_forever_enabled_p = 1;
2021 target_preopen (from_tty);
2023 unpush_target (target);
2025 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
2027 remote_desc = SERIAL_OPEN (name);
2029 perror_with_name (name);
2031 if (baud_rate != -1)
2033 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2035 SERIAL_CLOSE (remote_desc);
2036 perror_with_name (name);
2040 SERIAL_RAW (remote_desc);
2042 /* If there is something sitting in the buffer we might take it as a
2043 response to a command, which would be bad. */
2044 SERIAL_FLUSH_INPUT (remote_desc);
2048 puts_filtered ("Remote debugging using ");
2049 puts_filtered (name);
2050 puts_filtered ("\n");
2052 push_target (target); /* Switch to using remote target now */
2054 init_packet_config (&remote_protocol_P);
2055 init_packet_config (&remote_protocol_Z);
2057 general_thread = -2;
2058 continue_thread = -2;
2060 /* Force remote_write_bytes to check whether target supports
2061 binary downloading. */
2062 init_packet_config (&remote_protocol_binary_download);
2064 /* Probe for ability to use "ThreadInfo" query, as required. */
2065 use_threadinfo_query = 1;
2066 use_threadextra_query = 1;
2068 /* Without this, some commands which require an active target (such
2069 as kill) won't work. This variable serves (at least) double duty
2070 as both the pid of the target process (if it has such), and as a
2071 flag indicating that a target is active. These functions should
2072 be split out into seperate variables, especially since GDB will
2073 someday have a notion of debugging several processes. */
2075 inferior_pid = MAGIC_NULL_PID;
2076 /* Start the remote connection; if error (0), discard this target.
2077 In particular, if the user quits, be sure to discard it
2078 (we'd be in an inconsistent state otherwise). */
2079 if (!catch_errors (remote_start_remote, NULL,
2080 "Couldn't establish connection to remote target\n",
2089 /* tell the remote that we're using the extended protocol. */
2090 char *buf = alloca (PBUFSIZ);
2092 getpkt (buf, PBUFSIZ, 0);
2096 /* Just like remote_open but with asynchronous support. */
2098 remote_async_open_1 (name, from_tty, target, extended_p)
2101 struct target_ops *target;
2105 error ("To open a remote debug connection, you need to specify what\n\
2106 serial device is attached to the remote system\n\
2107 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2109 target_preopen (from_tty);
2111 unpush_target (target);
2113 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
2115 remote_desc = SERIAL_OPEN (name);
2117 perror_with_name (name);
2119 if (baud_rate != -1)
2121 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2123 SERIAL_CLOSE (remote_desc);
2124 perror_with_name (name);
2128 SERIAL_RAW (remote_desc);
2130 /* If there is something sitting in the buffer we might take it as a
2131 response to a command, which would be bad. */
2132 SERIAL_FLUSH_INPUT (remote_desc);
2136 puts_filtered ("Remote debugging using ");
2137 puts_filtered (name);
2138 puts_filtered ("\n");
2141 push_target (target); /* Switch to using remote target now */
2143 init_packet_config (&remote_protocol_P);
2144 init_packet_config (&remote_protocol_Z);
2146 general_thread = -2;
2147 continue_thread = -2;
2149 /* Force remote_write_bytes to check whether target supports
2150 binary downloading. */
2151 init_packet_config (&remote_protocol_binary_download);
2153 /* Probe for ability to use "ThreadInfo" query, as required. */
2154 use_threadinfo_query = 1;
2155 use_threadextra_query = 1;
2157 /* Without this, some commands which require an active target (such
2158 as kill) won't work. This variable serves (at least) double duty
2159 as both the pid of the target process (if it has such), and as a
2160 flag indicating that a target is active. These functions should
2161 be split out into seperate variables, especially since GDB will
2162 someday have a notion of debugging several processes. */
2163 inferior_pid = MAGIC_NULL_PID;
2165 /* With this target we start out by owning the terminal. */
2166 remote_async_terminal_ours_p = 1;
2168 /* FIXME: cagney/1999-09-23: During the initial connection it is
2169 assumed that the target is already ready and able to respond to
2170 requests. Unfortunatly remote_start_remote() eventually calls
2171 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2172 around this. Eventually a mechanism that allows
2173 wait_for_inferior() to expect/get timeouts will be
2175 wait_forever_enabled_p = 0;
2177 /* Start the remote connection; if error (0), discard this target.
2178 In particular, if the user quits, be sure to discard it
2179 (we'd be in an inconsistent state otherwise). */
2180 if (!catch_errors (remote_start_remote, NULL,
2181 "Couldn't establish connection to remote target\n",
2185 wait_forever_enabled_p = 1;
2189 wait_forever_enabled_p = 1;
2193 /* tell the remote that we're using the extended protocol. */
2194 char *buf = alloca (PBUFSIZ);
2196 getpkt (buf, PBUFSIZ, 0);
2200 /* This takes a program previously attached to and detaches it. After
2201 this is done, GDB can be used to debug some other program. We
2202 better not have left any breakpoints in the target program or it'll
2203 die when it hits one. */
2206 remote_detach (args, from_tty)
2210 char *buf = alloca (PBUFSIZ);
2213 error ("Argument given to \"detach\" when remotely debugging.");
2215 /* Tell the remote target to detach. */
2217 remote_send (buf, PBUFSIZ);
2219 target_mourn_inferior ();
2221 puts_filtered ("Ending remote debugging.\n");
2225 /* Same as remote_detach, but with async support. */
2227 remote_async_detach (args, from_tty)
2231 char *buf = alloca (PBUFSIZ);
2234 error ("Argument given to \"detach\" when remotely debugging.");
2236 /* Tell the remote target to detach. */
2238 remote_send (buf, PBUFSIZ);
2240 /* Unregister the file descriptor from the event loop. */
2241 if (target_is_async_p ())
2242 SERIAL_ASYNC (remote_desc, NULL, 0);
2244 target_mourn_inferior ();
2246 puts_filtered ("Ending remote debugging.\n");
2249 /* Convert hex digit A to a number. */
2255 if (a >= '0' && a <= '9')
2257 else if (a >= 'a' && a <= 'f')
2258 return a - 'a' + 10;
2259 else if (a >= 'A' && a <= 'F')
2260 return a - 'A' + 10;
2262 error ("Reply contains invalid hex digit %d", a);
2265 /* Convert number NIB to a hex digit. */
2274 return 'a' + nib - 10;
2277 /* Tell the remote machine to resume. */
2279 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2281 static int last_sent_step;
2284 remote_resume (pid, step, siggnal)
2286 enum target_signal siggnal;
2288 char *buf = alloca (PBUFSIZ);
2291 set_thread (0, 0); /* run any thread */
2293 set_thread (pid, 0); /* run this thread */
2295 dcache_flush (remote_dcache);
2297 last_sent_signal = siggnal;
2298 last_sent_step = step;
2300 /* A hook for when we need to do something at the last moment before
2302 if (target_resume_hook)
2303 (*target_resume_hook) ();
2305 if (siggnal != TARGET_SIGNAL_0)
2307 buf[0] = step ? 'S' : 'C';
2308 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2309 buf[2] = tohex ((int) siggnal & 0xf);
2313 strcpy (buf, step ? "s" : "c");
2318 /* Same as remote_resume, but with async support. */
2320 remote_async_resume (pid, step, siggnal)
2322 enum target_signal siggnal;
2324 char *buf = alloca (PBUFSIZ);
2327 set_thread (0, 0); /* run any thread */
2329 set_thread (pid, 0); /* run this thread */
2331 dcache_flush (remote_dcache);
2333 last_sent_signal = siggnal;
2334 last_sent_step = step;
2336 /* A hook for when we need to do something at the last moment before
2338 if (target_resume_hook)
2339 (*target_resume_hook) ();
2341 if (siggnal != TARGET_SIGNAL_0)
2343 buf[0] = step ? 'S' : 'C';
2344 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2345 buf[2] = tohex ((int) siggnal & 0xf);
2349 strcpy (buf, step ? "s" : "c");
2351 /* We are about to start executing the inferior, let's register it
2352 with the event loop. NOTE: this is the one place where all the
2353 execution commands end up. We could alternatively do this in each
2354 of the execution commands in infcmd.c.*/
2355 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2356 into infcmd.c in order to allow inferior function calls to work
2357 NOT asynchronously. */
2358 if (event_loop_p && target_can_async_p ())
2359 target_async (inferior_event_handler, 0);
2360 /* Tell the world that the target is now executing. */
2361 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2362 this? Instead, should the client of target just assume (for
2363 async targets) that the target is going to start executing? Is
2364 this information already found in the continuation block? */
2365 if (target_is_async_p ())
2366 target_executing = 1;
2371 /* Set up the signal handler for SIGINT, while the target is
2372 executing, ovewriting the 'regular' SIGINT signal handler. */
2374 initialize_sigint_signal_handler ()
2376 sigint_remote_token =
2377 create_async_signal_handler (async_remote_interrupt, NULL);
2378 signal (SIGINT, handle_remote_sigint);
2381 /* Signal handler for SIGINT, while the target is executing. */
2383 handle_remote_sigint (sig)
2386 signal (sig, handle_remote_sigint_twice);
2387 sigint_remote_twice_token =
2388 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2389 mark_async_signal_handler_wrapper (sigint_remote_token);
2392 /* Signal handler for SIGINT, installed after SIGINT has already been
2393 sent once. It will take effect the second time that the user sends
2396 handle_remote_sigint_twice (sig)
2399 signal (sig, handle_sigint);
2400 sigint_remote_twice_token =
2401 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
2402 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2405 /* Perform the real interruption of the target execution, in response
2408 async_remote_interrupt (arg)
2409 gdb_client_data arg;
2412 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2417 /* Perform interrupt, if the first attempt did not succeed. Just give
2418 up on the target alltogether. */
2420 async_remote_interrupt_twice (arg)
2421 gdb_client_data arg;
2424 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
2425 /* Do something only if the target was not killed by the previous
2427 if (target_executing)
2430 signal (SIGINT, handle_remote_sigint);
2434 /* Reinstall the usual SIGINT handlers, after the target has
2437 cleanup_sigint_signal_handler (void *dummy)
2439 signal (SIGINT, handle_sigint);
2440 if (sigint_remote_twice_token)
2441 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
2442 if (sigint_remote_token)
2443 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
2446 /* Send ^C to target to halt it. Target will respond, and send us a
2448 static void (*ofunc) PARAMS ((int));
2450 /* The command line interface's stop routine. This function is installed
2451 as a signal handler for SIGINT. The first time a user requests a
2452 stop, we call remote_stop to send a break or ^C. If there is no
2453 response from the target (it didn't stop when the user requested it),
2454 we ask the user if he'd like to detach from the target. */
2456 remote_interrupt (signo)
2459 /* If this doesn't work, try more severe steps. */
2460 signal (signo, remote_interrupt_twice);
2463 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2468 /* The user typed ^C twice. */
2471 remote_interrupt_twice (signo)
2474 signal (signo, ofunc);
2476 signal (signo, remote_interrupt);
2479 /* This is the generic stop called via the target vector. When a target
2480 interrupt is requested, either by the command line or the GUI, we
2481 will eventually end up here. */
2485 /* Send a break or a ^C, depending on user preference. */
2487 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
2490 SERIAL_SEND_BREAK (remote_desc);
2492 SERIAL_WRITE (remote_desc, "\003", 1);
2495 /* Ask the user what to do when an interrupt is received. */
2500 target_terminal_ours ();
2502 if (query ("Interrupted while waiting for the program.\n\
2503 Give up (and stop debugging it)? "))
2505 target_mourn_inferior ();
2506 return_to_top_level (RETURN_QUIT);
2509 target_terminal_inferior ();
2512 /* Enable/disable target terminal ownership. Most targets can use
2513 terminal groups to control terminal ownership. Remote targets are
2514 different in that explicit transfer of ownership to/from GDB/target
2518 remote_async_terminal_inferior (void)
2520 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2521 sync_execution here. This function should only be called when
2522 GDB is resuming the inferior in the forground. A background
2523 resume (``run&'') should leave GDB in control of the terminal and
2524 consequently should not call this code. */
2525 if (!sync_execution)
2527 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2528 calls target_terminal_*() idenpotent. The event-loop GDB talking
2529 to an asynchronous target with a synchronous command calls this
2530 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2531 stops trying to transfer the terminal to the target when it
2532 shouldn't this guard can go away. */
2533 if (!remote_async_terminal_ours_p)
2535 delete_file_handler (input_fd);
2536 remote_async_terminal_ours_p = 0;
2537 initialize_sigint_signal_handler ();
2538 /* NOTE: At this point we could also register our selves as the
2539 recipient of all input. Any characters typed could then be
2540 passed on down to the target. */
2544 remote_async_terminal_ours (void)
2546 /* See FIXME in remote_async_terminal_inferior. */
2547 if (!sync_execution)
2549 /* See FIXME in remote_async_terminal_inferior. */
2550 if (remote_async_terminal_ours_p)
2552 cleanup_sigint_signal_handler (NULL);
2553 add_file_handler (input_fd, stdin_event_handler, 0);
2554 remote_async_terminal_ours_p = 1;
2557 /* If nonzero, ignore the next kill. */
2562 remote_console_output (char *msg)
2566 for (p = msg; p[0] && p[1]; p += 2)
2569 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2572 fputs_unfiltered (tb, gdb_stdtarg);
2574 gdb_flush (gdb_stdtarg);
2577 /* Wait until the remote machine stops, then return,
2578 storing status in STATUS just as `wait' would.
2579 Returns "pid", which in the case of a multi-threaded
2580 remote OS, is the thread-id. */
2583 remote_wait (pid, status)
2585 struct target_waitstatus *status;
2587 unsigned char *buf = alloca (PBUFSIZ);
2588 int thread_num = -1;
2590 status->kind = TARGET_WAITKIND_EXITED;
2591 status->value.integer = 0;
2597 ofunc = signal (SIGINT, remote_interrupt);
2598 getpkt (buf, PBUFSIZ, 1);
2599 signal (SIGINT, ofunc);
2601 /* This is a hook for when we need to do something (perhaps the
2602 collection of trace data) every time the target stops. */
2603 if (target_wait_loop_hook)
2604 (*target_wait_loop_hook) ();
2608 case 'E': /* Error of some sort */
2609 warning ("Remote failure reply: %s", buf);
2611 case 'T': /* Status with PC, SP, FP, ... */
2615 char regs[MAX_REGISTER_RAW_SIZE];
2617 /* Expedited reply, containing Signal, {regno, reg} repeat */
2618 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2620 n... = register number
2621 r... = register contents
2623 p = &buf[3]; /* after Txx */
2630 /* Read the register number */
2631 regno = strtol ((const char *) p, &p_temp, 16);
2632 p1 = (unsigned char *) p_temp;
2634 if (p1 == p) /* No register number present here */
2636 p1 = (unsigned char *) strchr ((const char *) p, ':');
2638 warning ("Malformed packet(a) (missing colon): %s\n\
2641 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2643 p_temp = unpack_varlen_hex (++p1, &thread_num);
2644 record_currthread (thread_num);
2645 p = (unsigned char *) p_temp;
2653 warning ("Malformed packet(b) (missing colon): %s\n\
2657 if (regno >= NUM_REGS)
2658 warning ("Remote sent bad register number %ld: %s\n\
2662 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
2664 if (p[0] == 0 || p[1] == 0)
2665 warning ("Remote reply is too short: %s", buf);
2666 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
2669 supply_register (regno, regs);
2674 warning ("Remote register badly formatted: %s", buf);
2675 warning (" here: %s", p);
2680 case 'S': /* Old style status, just signal only */
2681 status->kind = TARGET_WAITKIND_STOPPED;
2682 status->value.sig = (enum target_signal)
2683 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2687 /* Export Cisco kernel mode as a convenience variable
2688 (so that it can be used in the GDB prompt if desired). */
2690 if (cisco_kernel_mode == 1)
2691 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2692 value_from_string ("PDEBUG-"));
2693 cisco_kernel_mode = 0;
2694 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2695 record_currthread (thread_num);
2697 else if (buf[3] == 'k')
2699 /* Export Cisco kernel mode as a convenience variable
2700 (so that it can be used in the GDB prompt if desired). */
2702 if (cisco_kernel_mode == 1)
2703 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2704 value_from_string ("KDEBUG-"));
2705 cisco_kernel_mode = 1;
2708 case 'N': /* Cisco special: status and offsets */
2710 bfd_vma text_addr, data_addr, bss_addr;
2711 bfd_signed_vma text_off, data_off, bss_off;
2714 status->kind = TARGET_WAITKIND_STOPPED;
2715 status->value.sig = (enum target_signal)
2716 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2718 if (symfile_objfile == NULL)
2720 warning ("Relocation packet received with no symbol file. \
2725 /* Relocate object file. Buffer format is NAATT;DD;BB
2726 * where AA is the signal number, TT is the new text
2727 * address, DD * is the new data address, and BB is the
2728 * new bss address. */
2731 text_addr = strtoul (p, (char **) &p1, 16);
2732 if (p1 == p || *p1 != ';')
2733 warning ("Malformed relocation packet: Packet '%s'", buf);
2735 data_addr = strtoul (p, (char **) &p1, 16);
2736 if (p1 == p || *p1 != ';')
2737 warning ("Malformed relocation packet: Packet '%s'", buf);
2739 bss_addr = strtoul (p, (char **) &p1, 16);
2741 warning ("Malformed relocation packet: Packet '%s'", buf);
2743 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2744 &text_off, &data_off, &bss_off)
2746 if (text_off != 0 || data_off != 0 || bss_off != 0)
2747 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2751 case 'W': /* Target exited */
2753 /* The remote process exited. */
2754 status->kind = TARGET_WAITKIND_EXITED;
2755 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2759 status->kind = TARGET_WAITKIND_SIGNALLED;
2760 status->value.sig = (enum target_signal)
2761 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2765 case 'O': /* Console output */
2766 remote_console_output (buf + 1);
2769 if (last_sent_signal != TARGET_SIGNAL_0)
2771 /* Zero length reply means that we tried 'S' or 'C' and
2772 the remote system doesn't support it. */
2773 target_terminal_ours_for_output ();
2775 ("Can't send signals to this remote system. %s not sent.\n",
2776 target_signal_to_name (last_sent_signal));
2777 last_sent_signal = TARGET_SIGNAL_0;
2778 target_terminal_inferior ();
2780 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2781 putpkt ((char *) buf);
2784 /* else fallthrough */
2786 warning ("Invalid remote reply: %s", buf);
2791 if (thread_num != -1)
2795 return inferior_pid;
2798 /* Async version of remote_wait. */
2800 remote_async_wait (pid, status)
2802 struct target_waitstatus *status;
2804 unsigned char *buf = alloca (PBUFSIZ);
2805 int thread_num = -1;
2807 status->kind = TARGET_WAITKIND_EXITED;
2808 status->value.integer = 0;
2814 if (!target_is_async_p ())
2815 ofunc = signal (SIGINT, remote_interrupt);
2816 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2817 _never_ wait for ever -> test on target_is_async_p().
2818 However, before we do that we need to ensure that the caller
2819 knows how to take the target into/out of async mode. */
2820 getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
2821 if (!target_is_async_p ())
2822 signal (SIGINT, ofunc);
2824 /* This is a hook for when we need to do something (perhaps the
2825 collection of trace data) every time the target stops. */
2826 if (target_wait_loop_hook)
2827 (*target_wait_loop_hook) ();
2831 case 'E': /* Error of some sort */
2832 warning ("Remote failure reply: %s", buf);
2834 case 'T': /* Status with PC, SP, FP, ... */
2838 char regs[MAX_REGISTER_RAW_SIZE];
2840 /* Expedited reply, containing Signal, {regno, reg} repeat */
2841 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2843 n... = register number
2844 r... = register contents
2846 p = &buf[3]; /* after Txx */
2853 /* Read the register number */
2854 regno = strtol ((const char *) p, &p_temp, 16);
2855 p1 = (unsigned char *) p_temp;
2857 if (p1 == p) /* No register number present here */
2859 p1 = (unsigned char *) strchr ((const char *) p, ':');
2861 warning ("Malformed packet(a) (missing colon): %s\n\
2864 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2866 p_temp = unpack_varlen_hex (++p1, &thread_num);
2867 record_currthread (thread_num);
2868 p = (unsigned char *) p_temp;
2876 warning ("Malformed packet(b) (missing colon): %s\n\
2880 if (regno >= NUM_REGS)
2881 warning ("Remote sent bad register number %ld: %s\n\
2885 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
2887 if (p[0] == 0 || p[1] == 0)
2888 warning ("Remote reply is too short: %s", buf);
2889 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
2892 supply_register (regno, regs);
2897 warning ("Remote register badly formatted: %s", buf);
2898 warning (" here: %s", p);
2903 case 'S': /* Old style status, just signal only */
2904 status->kind = TARGET_WAITKIND_STOPPED;
2905 status->value.sig = (enum target_signal)
2906 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2910 /* Export Cisco kernel mode as a convenience variable
2911 (so that it can be used in the GDB prompt if desired). */
2913 if (cisco_kernel_mode == 1)
2914 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2915 value_from_string ("PDEBUG-"));
2916 cisco_kernel_mode = 0;
2917 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2918 record_currthread (thread_num);
2920 else if (buf[3] == 'k')
2922 /* Export Cisco kernel mode as a convenience variable
2923 (so that it can be used in the GDB prompt if desired). */
2925 if (cisco_kernel_mode == 1)
2926 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2927 value_from_string ("KDEBUG-"));
2928 cisco_kernel_mode = 1;
2931 case 'N': /* Cisco special: status and offsets */
2933 bfd_vma text_addr, data_addr, bss_addr;
2934 bfd_signed_vma text_off, data_off, bss_off;
2937 status->kind = TARGET_WAITKIND_STOPPED;
2938 status->value.sig = (enum target_signal)
2939 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2941 if (symfile_objfile == NULL)
2943 warning ("Relocation packet recieved with no symbol file. \
2948 /* Relocate object file. Buffer format is NAATT;DD;BB
2949 * where AA is the signal number, TT is the new text
2950 * address, DD * is the new data address, and BB is the
2951 * new bss address. */
2954 text_addr = strtoul (p, (char **) &p1, 16);
2955 if (p1 == p || *p1 != ';')
2956 warning ("Malformed relocation packet: Packet '%s'", buf);
2958 data_addr = strtoul (p, (char **) &p1, 16);
2959 if (p1 == p || *p1 != ';')
2960 warning ("Malformed relocation packet: Packet '%s'", buf);
2962 bss_addr = strtoul (p, (char **) &p1, 16);
2964 warning ("Malformed relocation packet: Packet '%s'", buf);
2966 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2967 &text_off, &data_off, &bss_off)
2969 if (text_off != 0 || data_off != 0 || bss_off != 0)
2970 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2974 case 'W': /* Target exited */
2976 /* The remote process exited. */
2977 status->kind = TARGET_WAITKIND_EXITED;
2978 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2982 status->kind = TARGET_WAITKIND_SIGNALLED;
2983 status->value.sig = (enum target_signal)
2984 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2988 case 'O': /* Console output */
2989 remote_console_output (buf + 1);
2990 /* Return immediately to the event loop. The event loop will
2991 still be waiting on the inferior afterwards. */
2992 status->kind = TARGET_WAITKIND_IGNORE;
2995 if (last_sent_signal != TARGET_SIGNAL_0)
2997 /* Zero length reply means that we tried 'S' or 'C' and
2998 the remote system doesn't support it. */
2999 target_terminal_ours_for_output ();
3001 ("Can't send signals to this remote system. %s not sent.\n",
3002 target_signal_to_name (last_sent_signal));
3003 last_sent_signal = TARGET_SIGNAL_0;
3004 target_terminal_inferior ();
3006 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3007 putpkt ((char *) buf);
3010 /* else fallthrough */
3012 warning ("Invalid remote reply: %s", buf);
3017 if (thread_num != -1)
3021 return inferior_pid;
3024 /* Number of bytes of registers this stub implements. */
3026 static int register_bytes_found;
3028 /* Read the remote registers into the block REGS. */
3029 /* Currently we just read all the registers, so we don't use regno. */
3033 remote_fetch_registers (regno)
3036 char *buf = alloca (PBUFSIZ);
3039 char regs[REGISTER_BYTES];
3041 set_thread (inferior_pid, 1);
3044 remote_send (buf, PBUFSIZ);
3046 /* Save the size of the packet sent to us by the target. Its used
3047 as a heuristic when determining the max size of packets that the
3048 target can safely receive. */
3049 if (actual_register_packet_size == 0)
3050 actual_register_packet_size = strlen (buf);
3052 /* Unimplemented registers read as all bits zero. */
3053 memset (regs, 0, REGISTER_BYTES);
3055 /* We can get out of synch in various cases. If the first character
3056 in the buffer is not a hex character, assume that has happened
3057 and try to fetch another packet to read. */
3058 while ((buf[0] < '0' || buf[0] > '9')
3059 && (buf[0] < 'a' || buf[0] > 'f')
3060 && buf[0] != 'x') /* New: unavailable register value */
3063 fprintf_unfiltered (gdb_stdlog,
3064 "Bad register packet; fetching a new packet\n");
3065 getpkt (buf, PBUFSIZ, 0);
3068 /* Reply describes registers byte by byte, each byte encoded as two
3069 hex characters. Suck them all up, then supply them to the
3070 register cacheing/storage mechanism. */
3073 for (i = 0; i < REGISTER_BYTES; i++)
3079 warning ("Remote reply is of odd length: %s", buf);
3080 /* Don't change register_bytes_found in this case, and don't
3081 print a second warning. */
3084 if (p[0] == 'x' && p[1] == 'x')
3085 regs[i] = 0; /* 'x' */
3087 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3091 if (i != register_bytes_found)
3093 register_bytes_found = i;
3094 #ifdef REGISTER_BYTES_OK
3095 if (!REGISTER_BYTES_OK (i))
3096 warning ("Remote reply is too short: %s", buf);
3101 for (i = 0; i < NUM_REGS; i++)
3103 supply_register (i, ®s[REGISTER_BYTE (i)]);
3104 if (buf[REGISTER_BYTE (i) * 2] == 'x')
3105 register_valid[i] = -1; /* register value not available */
3109 /* Prepare to store registers. Since we may send them all (using a
3110 'G' request), we have to read out the ones we don't want to change
3114 remote_prepare_to_store ()
3116 /* Make sure the entire registers array is valid. */
3117 switch (remote_protocol_P.support)
3119 case PACKET_DISABLE:
3120 case PACKET_SUPPORT_UNKNOWN:
3121 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
3128 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
3129 packet was not recognized. */
3132 store_register_using_P (int regno)
3134 /* Try storing a single register. */
3135 char *buf = alloca (PBUFSIZ);
3140 sprintf (buf, "P%x=", regno);
3141 p = buf + strlen (buf);
3142 regp = ®isters[REGISTER_BYTE (regno)];
3143 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
3145 *p++ = tohex ((regp[i] >> 4) & 0xf);
3146 *p++ = tohex (regp[i] & 0xf);
3149 remote_send (buf, PBUFSIZ);
3151 return buf[0] != '\0';
3155 /* Store register REGNO, or all registers if REGNO == -1, from the contents
3156 of REGISTERS. FIXME: ignores errors. */
3159 remote_store_registers (regno)
3162 char *buf = alloca (PBUFSIZ);
3166 set_thread (inferior_pid, 1);
3170 switch (remote_protocol_P.support)
3172 case PACKET_DISABLE:
3175 if (store_register_using_P (regno))
3178 error ("Protocol error: P packet not recognized by stub");
3179 case PACKET_SUPPORT_UNKNOWN:
3180 if (store_register_using_P (regno))
3182 /* The stub recognized the 'P' packet. Remember this. */
3183 remote_protocol_P.support = PACKET_ENABLE;
3188 /* The stub does not support the 'P' packet. Use 'G'
3189 instead, and don't try using 'P' in the future (it
3190 will just waste our time). */
3191 remote_protocol_P.support = PACKET_DISABLE;
3199 /* Command describes registers byte by byte,
3200 each byte encoded as two hex characters. */
3203 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3204 for (i = 0; i < register_bytes_found; i++)
3206 *p++ = tohex ((registers[i] >> 4) & 0xf);
3207 *p++ = tohex (registers[i] & 0xf);
3211 remote_send (buf, PBUFSIZ);
3214 /* Use of the data cache *used* to be disabled because it loses for looking
3215 at and changing hardware I/O ports and the like. Accepting `volatile'
3216 would perhaps be one way to fix it. Another idea would be to use the
3217 executable file for the text segment (for all SEC_CODE sections?
3218 For all SEC_READONLY sections?). This has problems if you want to
3219 actually see what the memory contains (e.g. self-modifying code,
3220 clobbered memory, user downloaded the wrong thing).
3222 Because it speeds so much up, it's now enabled, if you're playing
3223 with registers you turn it of (set remotecache 0). */
3225 /* Read a word from remote address ADDR and return it.
3226 This goes through the data cache. */
3230 remote_fetch_word (addr)
3233 return dcache_fetch (remote_dcache, addr);
3236 /* Write a word WORD into remote address ADDR.
3237 This goes through the data cache. */
3240 remote_store_word (addr, word)
3244 dcache_poke (remote_dcache, addr, word);
3246 #endif /* 0 (unused?) */
3250 /* Return the number of hex digits in num. */
3258 for (i = 0; num != 0; i++)
3264 /* Set BUF to the minimum number of hex digits representing NUM. */
3267 hexnumstr (buf, num)
3271 int len = hexnumlen (num);
3272 return hexnumnstr (buf, num, len);
3276 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3279 hexnumnstr (buf, num, width)
3288 for (i = width - 1; i >= 0; i--)
3290 buf[i] = "0123456789abcdef"[(num & 0xf)];
3297 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3300 remote_address_masked (addr)
3303 if (remote_address_size > 0
3304 && remote_address_size < (sizeof (ULONGEST) * 8))
3306 /* Only create a mask when that mask can safely be constructed
3307 in a ULONGEST variable. */
3309 mask = (mask << remote_address_size) - 1;
3315 /* Determine whether the remote target supports binary downloading.
3316 This is accomplished by sending a no-op memory write of zero length
3317 to the target at the specified address. It does not suffice to send
3318 the whole packet, since many stubs strip the eighth bit and subsequently
3319 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3321 NOTE: This can still lose if the serial line is not eight-bit
3322 clean. In cases like this, the user should clear "remote
3326 check_binary_download (addr)
3329 switch (remote_protocol_binary_download.support)
3331 case PACKET_DISABLE:
3335 case PACKET_SUPPORT_UNKNOWN:
3337 char *buf = alloca (PBUFSIZ);
3342 p += hexnumstr (p, (ULONGEST) addr);
3344 p += hexnumstr (p, (ULONGEST) 0);
3348 putpkt_binary (buf, (int) (p - buf));
3349 getpkt (buf, PBUFSIZ, 0);
3354 fprintf_unfiltered (gdb_stdlog,
3355 "binary downloading NOT suppported by target\n");
3356 remote_protocol_binary_download.support = PACKET_DISABLE;
3361 fprintf_unfiltered (gdb_stdlog,
3362 "binary downloading suppported by target\n");
3363 remote_protocol_binary_download.support = PACKET_ENABLE;
3370 /* Write memory data directly to the remote machine.
3371 This does not inform the data cache; the data cache uses this.
3372 MEMADDR is the address in the remote memory space.
3373 MYADDR is the address of the buffer in our space.
3374 LEN is the number of bytes.
3376 Returns number of bytes transferred, or 0 (setting errno) for
3377 error. Only transfer a single packet. */
3380 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3383 int max_buf_size; /* Max size of packet output buffer */
3385 unsigned char *plen;
3391 /* Verify that the target can support a binary download */
3392 check_binary_download (memaddr);
3394 /* Determine the max packet size. */
3395 max_buf_size = get_memory_write_packet_size ();
3396 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3397 buf = alloca (sizeof_buf);
3399 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3400 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3402 /* construct "M"<memaddr>","<len>":" */
3403 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3406 /* Append [XM]. Compute a best guess of the number of bytes
3407 actually transfered. */
3408 switch (remote_protocol_binary_download.support)
3412 /* Best guess at number of bytes that will fit. */
3413 todo = min (len, max_buf_size);
3415 case PACKET_DISABLE:
3417 /* num bytes that will fit */
3418 todo = min (len, max_buf_size / 2);
3420 case PACKET_SUPPORT_UNKNOWN:
3421 internal_error ("remote_write_bytes: bad switch");
3424 /* Append <memaddr> */
3425 memaddr = remote_address_masked (memaddr);
3426 p += hexnumstr (p, (ULONGEST) memaddr);
3429 /* Append <len>. Retain the location/size of <len>. It may
3430 need to be adjusted once the packet body has been created. */
3432 plenlen = hexnumstr (p, (ULONGEST) todo);
3437 /* Append the packet body. */
3438 switch (remote_protocol_binary_download.support)
3441 /* Binary mode. Send target system values byte by byte, in
3442 increasing byte addresses. Only escape certain critical
3445 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3448 switch (myaddr[nr_bytes] & 0xff)
3453 /* These must be escaped */
3455 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3458 *p++ = myaddr[nr_bytes] & 0xff;
3462 if (nr_bytes < todo)
3464 /* Escape chars have filled up the buffer prematurely,
3465 and we have actually sent fewer bytes than planned.
3466 Fix-up the length field of the packet. Use the same
3467 number of characters as before. */
3469 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3470 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3473 case PACKET_DISABLE:
3474 /* Normal mode: Send target system values byte by byte, in
3475 increasing byte addresses. Each byte is encoded as a two hex
3477 for (nr_bytes = 0; nr_bytes < todo; nr_bytes++)
3479 *p++ = tohex ((myaddr[nr_bytes] >> 4) & 0xf);
3480 *p++ = tohex (myaddr[nr_bytes] & 0xf);
3484 case PACKET_SUPPORT_UNKNOWN:
3485 internal_error ("remote_write_bytes: bad switch");
3488 putpkt_binary (buf, (int) (p - buf));
3489 getpkt (buf, sizeof_buf, 0);
3493 /* There is no correspondance between what the remote protocol
3494 uses for errors and errno codes. We would like a cleaner way
3495 of representing errors (big enough to include errno codes,
3496 bfd_error codes, and others). But for now just return EIO. */
3501 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3502 bytes than we'd planned. */
3506 /* Read memory data directly from the remote machine.
3507 This does not use the data cache; the data cache uses this.
3508 MEMADDR is the address in the remote memory space.
3509 MYADDR is the address of the buffer in our space.
3510 LEN is the number of bytes.
3512 Returns number of bytes transferred, or 0 for error. */
3514 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3515 remote targets) shouldn't attempt to read the entire buffer.
3516 Instead it should read a single packet worth of data and then
3517 return the byte size of that packet to the caller. The caller (its
3518 caller and its callers caller ;-) already contains code for
3519 handling partial reads. */
3522 remote_read_bytes (memaddr, myaddr, len)
3528 int max_buf_size; /* Max size of packet output buffer */
3532 /* Create a buffer big enough for this packet. */
3533 max_buf_size = get_memory_read_packet_size ();
3534 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3535 buf = alloca (sizeof_buf);
3544 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3546 /* construct "m"<memaddr>","<len>" */
3547 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3548 memaddr = remote_address_masked (memaddr);
3551 p += hexnumstr (p, (ULONGEST) memaddr);
3553 p += hexnumstr (p, (ULONGEST) todo);
3557 getpkt (buf, sizeof_buf, 0);
3561 /* There is no correspondance between what the remote protocol uses
3562 for errors and errno codes. We would like a cleaner way of
3563 representing errors (big enough to include errno codes, bfd_error
3564 codes, and others). But for now just return EIO. */
3569 /* Reply describes memory byte by byte,
3570 each byte encoded as two hex characters. */
3573 for (i = 0; i < todo; i++)
3575 if (p[0] == 0 || p[1] == 0)
3576 /* Reply is short. This means that we were able to read
3577 only part of what we wanted to. */
3578 return i + (origlen - len);
3579 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3589 /* Read or write LEN bytes from inferior memory at MEMADDR,
3590 transferring to or from debugger address BUFFER. Write to inferior if
3591 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3596 remote_xfer_memory (mem_addr, buffer, mem_len, should_write, target)
3601 struct target_ops *target; /* ignored */
3603 CORE_ADDR targ_addr;
3605 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3609 return dcache_xfer_memory (remote_dcache, targ_addr, buffer,
3610 targ_len, should_write);
3615 /* Enable after 4.12. */
3618 remote_search (len, data, mask, startaddr, increment, lorange, hirange
3619 addr_found, data_found)
3623 CORE_ADDR startaddr;
3627 CORE_ADDR *addr_found;
3630 if (increment == -4 && len == 4)
3632 long mask_long, data_long;
3633 long data_found_long;
3634 CORE_ADDR addr_we_found;
3635 char *buf = alloca (PBUFSIZ);
3636 long returned_long[2];
3639 mask_long = extract_unsigned_integer (mask, len);
3640 data_long = extract_unsigned_integer (data, len);
3641 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3643 getpkt (buf, PBUFSIZ, 0);
3646 /* The stub doesn't support the 't' request. We might want to
3647 remember this fact, but on the other hand the stub could be
3648 switched on us. Maybe we should remember it only until
3649 the next "target remote". */
3650 generic_search (len, data, mask, startaddr, increment, lorange,
3651 hirange, addr_found, data_found);
3656 /* There is no correspondance between what the remote protocol uses
3657 for errors and errno codes. We would like a cleaner way of
3658 representing errors (big enough to include errno codes, bfd_error
3659 codes, and others). But for now just use EIO. */
3660 memory_error (EIO, startaddr);
3663 while (*p != '\0' && *p != ',')
3664 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3666 error ("Protocol error: short return for search");
3668 data_found_long = 0;
3669 while (*p != '\0' && *p != ',')
3670 data_found_long = (data_found_long << 4) + fromhex (*p++);
3671 /* Ignore anything after this comma, for future extensions. */
3673 if (addr_we_found < lorange || addr_we_found >= hirange)
3679 *addr_found = addr_we_found;
3680 *data_found = store_unsigned_integer (data_we_found, len);
3683 generic_search (len, data, mask, startaddr, increment, lorange,
3684 hirange, addr_found, data_found);
3689 remote_files_info (ignore)
3690 struct target_ops *ignore;
3692 puts_filtered ("Debugging a target over a serial line.\n");
3695 /* Stuff for dealing with the packets which are part of this protocol.
3696 See comment at top of file for details. */
3698 /* Read a single character from the remote end, masking it down to 7 bits. */
3706 ch = SERIAL_READCHAR (remote_desc, timeout);
3711 switch ((enum serial_rc) ch)
3714 target_mourn_inferior ();
3715 error ("Remote connection closed");
3718 perror_with_name ("Remote communication error");
3720 case SERIAL_TIMEOUT:
3726 /* Send the command in BUF to the remote machine, and read the reply
3727 into BUF. Report an error if we get an error reply. */
3730 remote_send (char *buf,
3734 getpkt (buf, sizeof_buf, 0);
3737 error ("Remote failure reply: %s", buf);
3740 /* Display a null-terminated packet on stdout, for debugging, using C
3747 puts_filtered ("\"");
3748 fputstr_filtered (buf, '"', gdb_stdout);
3749 puts_filtered ("\"");
3756 return putpkt_binary (buf, strlen (buf));
3759 /* Send a packet to the remote machine, with error checking. The data
3760 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3761 to account for the $, # and checksum, and for a possible /0 if we are
3762 debugging (remote_debug) and want to print the sent packet as a string */
3765 putpkt_binary (buf, cnt)
3770 unsigned char csum = 0;
3771 char *buf2 = alloca (cnt + 6);
3772 long sizeof_junkbuf = PBUFSIZ;
3773 char *junkbuf = alloca (sizeof_junkbuf);
3779 /* Copy the packet into buffer BUF2, encapsulating it
3780 and giving it a checksum. */
3785 for (i = 0; i < cnt; i++)
3791 *p++ = tohex ((csum >> 4) & 0xf);
3792 *p++ = tohex (csum & 0xf);
3794 /* Send it over and over until we get a positive ack. */
3798 int started_error_output = 0;
3803 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3804 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
3805 fprintf_unfiltered (gdb_stdlog, "...");
3806 gdb_flush (gdb_stdlog);
3808 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
3809 perror_with_name ("putpkt: write failed");
3811 /* read until either a timeout occurs (-2) or '+' is read */
3814 ch = readchar (remote_timeout);
3822 case SERIAL_TIMEOUT:
3824 if (started_error_output)
3826 putchar_unfiltered ('\n');
3827 started_error_output = 0;
3836 fprintf_unfiltered (gdb_stdlog, "Ack\n");
3840 fprintf_unfiltered (gdb_stdlog, "Nak\n");
3841 case SERIAL_TIMEOUT:
3845 break; /* Retransmit buffer */
3848 /* It's probably an old response, and we're out of sync.
3849 Just gobble up the packet and ignore it. */
3850 getpkt (junkbuf, sizeof_junkbuf, 0);
3851 continue; /* Now, go look for + */
3856 if (!started_error_output)
3858 started_error_output = 1;
3859 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
3861 fputc_unfiltered (ch & 0177, gdb_stdlog);
3865 break; /* Here to retransmit */
3869 /* This is wrong. If doing a long backtrace, the user should be
3870 able to get out next time we call QUIT, without anything as
3871 violent as interrupt_query. If we want to provide a way out of
3872 here without getting to the next QUIT, it should be based on
3873 hitting ^C twice as in remote_wait. */
3883 static int remote_cisco_mode;
3885 /* Come here after finding the start of the frame. Collect the rest
3886 into BUF, verifying the checksum, length, and handling run-length
3887 compression. No more than sizeof_buf-1 characters are read so that
3888 the buffer can be NUL terminated.
3890 Returns -1 on error, number of characters in buffer (ignoring the
3891 trailing NULL) on success. (could be extended to return one of the
3892 SERIAL status indications). */
3895 read_frame (char *buf,
3907 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
3908 c = readchar (remote_timeout);
3911 case SERIAL_TIMEOUT:
3913 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
3917 fputs_filtered ("Saw new packet start in middle of old one\n",
3919 return -1; /* Start a new packet, count retries */
3922 unsigned char pktcsum;
3926 pktcsum = fromhex (readchar (remote_timeout)) << 4;
3927 pktcsum |= fromhex (readchar (remote_timeout));
3929 if (csum == pktcsum)
3934 fprintf_filtered (gdb_stdlog,
3935 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3937 fputs_filtered (buf, gdb_stdlog);
3938 fputs_filtered ("\n", gdb_stdlog);
3940 /* Number of characters in buffer ignoring trailing
3944 case '*': /* Run length encoding */
3949 if (remote_cisco_mode == 0)
3951 c = readchar (remote_timeout);
3953 repeat = c - ' ' + 3; /* Compute repeat count */
3957 /* Cisco's run-length encoding variant uses two
3958 hex chars to represent the repeat count. */
3960 c = readchar (remote_timeout);
3962 repeat = fromhex (c) << 4;
3963 c = readchar (remote_timeout);
3965 repeat += fromhex (c);
3968 /* The character before ``*'' is repeated. */
3970 if (repeat > 0 && repeat <= 255
3972 && bc + repeat < sizeof_buf - 1)
3974 memset (&buf[bc], buf[bc - 1], repeat);
3980 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
3981 puts_filtered (buf);
3982 puts_filtered ("\n");
3986 if (bc < sizeof_buf - 1)
3994 puts_filtered ("Remote packet too long: ");
3995 puts_filtered (buf);
3996 puts_filtered ("\n");
4003 /* Read a packet from the remote machine, with error checking, and
4004 store it in BUF. If FOREVER, wait forever rather than timing out;
4005 this is used (in synchronous mode) to wait for a target that is is
4006 executing user code to stop. */
4007 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4008 don't have to change all the calls to getpkt to deal with the
4009 return value, because at the moment I don't know what the right
4010 thing to do it for those. */
4018 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4022 /* Read a packet from the remote machine, with error checking, and
4023 store it in BUF. If FOREVER, wait forever rather than timing out;
4024 this is used (in synchronous mode) to wait for a target that is is
4025 executing user code to stop. If FOREVER == 0, this function is
4026 allowed to time out gracefully and return an indication of this to
4029 getpkt_sane (char *buf,
4038 strcpy (buf, "timeout");
4042 timeout = watchdog > 0 ? watchdog : -1;
4046 timeout = remote_timeout;
4050 for (tries = 1; tries <= MAX_TRIES; tries++)
4052 /* This can loop forever if the remote side sends us characters
4053 continuously, but if it pauses, we'll get a zero from readchar
4054 because of timeout. Then we'll count that as a retry. */
4056 /* Note that we will only wait forever prior to the start of a packet.
4057 After that, we expect characters to arrive at a brisk pace. They
4058 should show up within remote_timeout intervals. */
4062 c = readchar (timeout);
4064 if (c == SERIAL_TIMEOUT)
4066 if (forever) /* Watchdog went off? Kill the target. */
4069 target_mourn_inferior ();
4070 error ("Watchdog has expired. Target detached.\n");
4073 fputs_filtered ("Timed out.\n", gdb_stdlog);
4079 /* We've found the start of a packet, now collect the data. */
4081 val = read_frame (buf, sizeof_buf);
4087 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4088 fputstr_unfiltered (buf, 0, gdb_stdlog);
4089 fprintf_unfiltered (gdb_stdlog, "\n");
4091 SERIAL_WRITE (remote_desc, "+", 1);
4095 /* Try the whole thing again. */
4097 SERIAL_WRITE (remote_desc, "-", 1);
4100 /* We have tried hard enough, and just can't receive the packet. Give up. */
4102 printf_unfiltered ("Ignoring packet error, continuing...\n");
4103 SERIAL_WRITE (remote_desc, "+", 1);
4110 /* For some mysterious reason, wait_for_inferior calls kill instead of
4111 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4115 target_mourn_inferior ();
4119 /* Use catch_errors so the user can quit from gdb even when we aren't on
4120 speaking terms with the remote system. */
4121 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4123 /* Don't wait for it to die. I'm not really sure it matters whether
4124 we do or not. For the existing stubs, kill is a noop. */
4125 target_mourn_inferior ();
4128 /* Async version of remote_kill. */
4130 remote_async_kill ()
4132 /* Unregister the file descriptor from the event loop. */
4133 if (target_is_async_p ())
4134 SERIAL_ASYNC (remote_desc, NULL, 0);
4136 /* For some mysterious reason, wait_for_inferior calls kill instead of
4137 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4141 target_mourn_inferior ();
4145 /* Use catch_errors so the user can quit from gdb even when we aren't on
4146 speaking terms with the remote system. */
4147 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4149 /* Don't wait for it to die. I'm not really sure it matters whether
4150 we do or not. For the existing stubs, kill is a noop. */
4151 target_mourn_inferior ();
4157 remote_mourn_1 (&remote_ops);
4161 remote_async_mourn ()
4163 remote_mourn_1 (&remote_async_ops);
4167 extended_remote_mourn ()
4169 /* We do _not_ want to mourn the target like this; this will
4170 remove the extended remote target from the target stack,
4171 and the next time the user says "run" it'll fail.
4173 FIXME: What is the right thing to do here? */
4175 remote_mourn_1 (&extended_remote_ops);
4179 /* Worker function for remote_mourn. */
4181 remote_mourn_1 (target)
4182 struct target_ops *target;
4184 unpush_target (target);
4185 generic_mourn_inferior ();
4188 /* In the extended protocol we want to be able to do things like
4189 "run" and have them basically work as expected. So we need
4190 a special create_inferior function.
4192 FIXME: One day add support for changing the exec file
4193 we're debugging, arguments and an environment. */
4196 extended_remote_create_inferior (exec_file, args, env)
4201 /* Rip out the breakpoints; we'll reinsert them after restarting
4202 the remote server. */
4203 remove_breakpoints ();
4205 /* Now restart the remote server. */
4206 extended_remote_restart ();
4208 /* Now put the breakpoints back in. This way we're safe if the
4209 restart function works via a unix fork on the remote side. */
4210 insert_breakpoints ();
4212 /* Clean up from the last time we were running. */
4213 clear_proceed_status ();
4215 /* Let the remote process run. */
4216 proceed (-1, TARGET_SIGNAL_0, 0);
4219 /* Async version of extended_remote_create_inferior. */
4221 extended_remote_async_create_inferior (exec_file, args, env)
4226 /* Rip out the breakpoints; we'll reinsert them after restarting
4227 the remote server. */
4228 remove_breakpoints ();
4230 /* If running asynchronously, register the target file descriptor
4231 with the event loop. */
4232 if (event_loop_p && target_can_async_p ())
4233 target_async (inferior_event_handler, 0);
4235 /* Now restart the remote server. */
4236 extended_remote_restart ();
4238 /* Now put the breakpoints back in. This way we're safe if the
4239 restart function works via a unix fork on the remote side. */
4240 insert_breakpoints ();
4242 /* Clean up from the last time we were running. */
4243 clear_proceed_status ();
4245 /* Let the remote process run. */
4246 proceed (-1, TARGET_SIGNAL_0, 0);
4250 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
4251 than other targets; in those use REMOTE_BREAKPOINT instead of just
4252 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4253 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4254 the standard routines that are in mem-break.c. */
4256 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
4257 the choice of breakpoint instruction affects target program design and
4258 vice versa, and by making it user-tweakable, the special code here
4259 goes away and we need fewer special GDB configurations. */
4261 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4262 #define REMOTE_BREAKPOINT
4265 #ifdef REMOTE_BREAKPOINT
4267 /* If the target isn't bi-endian, just pretend it is. */
4268 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4269 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4270 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4273 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4274 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4276 #endif /* REMOTE_BREAKPOINT */
4278 /* Insert a breakpoint on targets that don't have any better breakpoint
4279 support. We read the contents of the target location and stash it,
4280 then overwrite it with a breakpoint instruction. ADDR is the target
4281 location in the target machine. CONTENTS_CACHE is a pointer to
4282 memory allocated for saving the target contents. It is guaranteed
4283 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4284 is accomplished via BREAKPOINT_MAX). */
4287 remote_insert_breakpoint (addr, contents_cache)
4289 char *contents_cache;
4291 #ifdef REMOTE_BREAKPOINT
4296 /* Try the "Z" packet if it is not already disabled.
4297 If it succeeds, then set the support to PACKET_ENABLE.
4298 If it fails, and the user has explicitly requested the Z support
4299 then report an error, otherwise, mark it disabled and go on. */
4301 if ((remote_protocol_Z.support == PACKET_ENABLE)
4302 || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN))
4304 char *buf = alloca (PBUFSIZ);
4307 addr = remote_address_masked (addr);
4311 p += hexnumstr (p, (ULONGEST) addr);
4312 BREAKPOINT_FROM_PC (&addr, &bp_size);
4313 sprintf (p, ",%d", bp_size);
4316 getpkt (buf, PBUFSIZ, 0);
4320 remote_protocol_Z.support = PACKET_ENABLE;
4321 return (buf[0] == 'E');
4324 /* The stub does not support the 'Z' request. If the user has
4325 explicitly requested the Z support, or if the stub previously
4326 said it supported the packet, this is an error,
4327 otherwise, mark it disabled. */
4329 else if (remote_protocol_Z.support == PACKET_ENABLE)
4331 error ("Protocol error: Z packet not recognized by stub");
4335 remote_protocol_Z.support = PACKET_DISABLE;
4339 #ifdef REMOTE_BREAKPOINT
4340 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4344 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4345 val = target_write_memory (addr, (char *) big_break_insn,
4346 sizeof big_break_insn);
4348 val = target_write_memory (addr, (char *) little_break_insn,
4349 sizeof little_break_insn);
4354 return memory_insert_breakpoint (addr, contents_cache);
4355 #endif /* REMOTE_BREAKPOINT */
4359 remote_remove_breakpoint (addr, contents_cache)
4361 char *contents_cache;
4365 if ((remote_protocol_Z.support == PACKET_ENABLE)
4366 || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN))
4368 char *buf = alloca (PBUFSIZ);
4375 addr = remote_address_masked (addr);
4376 p += hexnumstr (p, (ULONGEST) addr);
4377 BREAKPOINT_FROM_PC (&addr, &bp_size);
4378 sprintf (p, ",%d", bp_size);
4381 getpkt (buf, PBUFSIZ, 0);
4383 return (buf[0] == 'E');
4386 #ifdef REMOTE_BREAKPOINT
4387 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4389 return memory_remove_breakpoint (addr, contents_cache);
4390 #endif /* REMOTE_BREAKPOINT */
4393 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
4395 remote_insert_watchpoint (addr, len, type)
4400 char *buf = alloca (PBUFSIZ);
4403 if (remote_protocol_Z.support == PACKET_DISABLE)
4404 error ("Can't set hardware watchpoints without the 'Z' packet\n");
4406 sprintf (buf, "Z%x,", type + 2 );
4407 p = strchr (buf, '\0');
4408 addr = remote_address_masked (addr);
4409 p += hexnumstr (p, (ULONGEST) addr);
4410 sprintf (p, ",%x", len);
4413 getpkt (buf, PBUFSIZ, 0);
4415 if (buf[0] == '\0' || buf [0] == 'E')
4422 remote_remove_watchpoint (addr, len, type)
4427 char *buf = alloca (PBUFSIZ);
4430 sprintf (buf, "z%x,", type + 2 );
4431 p = strchr (buf, '\0');
4432 addr = remote_address_masked (addr);
4433 p += hexnumstr (p, (ULONGEST) addr);
4434 sprintf (p, ",%x", len);
4436 getpkt (buf, PBUFSIZ, 0);
4438 if (buf[0] == '\0' || buf [0] == 'E')
4445 remote_insert_hw_breakpoint (addr, len)
4449 char *buf = alloca (PBUFSIZ);
4452 if (remote_protocol_Z.support == PACKET_DISABLE)
4453 error ("Can't set hardware breakpoints without the 'Z' packet\n");
4459 addr = remote_address_masked (addr);
4460 p += hexnumstr (p, (ULONGEST) addr);
4464 getpkt (buf, PBUFSIZ, 0);
4466 if (buf[0] == '\0' || buf [0] == 'E')
4473 remote_remove_hw_breakpoint (addr, len)
4477 char *buf = alloca (PBUFSIZ);
4484 addr = remote_address_masked (addr);
4485 p += hexnumstr (p, (ULONGEST) addr);
4489 getpkt (buf, PBUFSIZ, 0);
4491 if (buf[0] == '\0' || buf [0] == 'E')
4498 /* Some targets are only capable of doing downloads, and afterwards
4499 they switch to the remote serial protocol. This function provides
4500 a clean way to get from the download target to the remote target.
4501 It's basically just a wrapper so that we don't have to expose any
4502 of the internal workings of remote.c.
4504 Prior to calling this routine, you should shutdown the current
4505 target code, else you will get the "A program is being debugged
4506 already..." message. Usually a call to pop_target() suffices. */
4509 push_remote_target (name, from_tty)
4513 printf_filtered ("Switching to remote protocol\n");
4514 remote_open (name, from_tty);
4517 /* Other targets want to use the entire remote serial module but with
4518 certain remote_ops overridden. */
4521 open_remote_target (name, from_tty, target, extended_p)
4524 struct target_ops *target;
4527 printf_filtered ("Selecting the %sremote protocol\n",
4528 (extended_p ? "extended-" : ""));
4529 remote_open_1 (name, from_tty, target, extended_p);
4532 /* Table used by the crc32 function to calcuate the checksum. */
4534 static unsigned long crc32_table[256] =
4537 static unsigned long
4538 crc32 (buf, len, crc)
4543 if (!crc32_table[1])
4545 /* Initialize the CRC table and the decoding table. */
4549 for (i = 0; i < 256; i++)
4551 for (c = i << 24, j = 8; j > 0; --j)
4552 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4559 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4565 /* compare-sections command
4567 With no arguments, compares each loadable section in the exec bfd
4568 with the same memory range on the target, and reports mismatches.
4569 Useful for verifying the image on the target against the exec file.
4570 Depends on the target understanding the new "qCRC:" request. */
4572 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4573 target method (target verify memory) and generic version of the
4574 actual command. This will allow other high-level code (especially
4575 generic_load()) to make use of this target functionality. */
4578 compare_sections_command (args, from_tty)
4583 unsigned long host_crc, target_crc;
4584 extern bfd *exec_bfd;
4585 struct cleanup *old_chain;
4589 char *buf = alloca (PBUFSIZ);
4596 error ("command cannot be used without an exec file");
4597 if (!current_target.to_shortname ||
4598 strcmp (current_target.to_shortname, "remote") != 0)
4599 error ("command can only be used with remote target");
4601 for (s = exec_bfd->sections; s; s = s->next)
4603 if (!(s->flags & SEC_LOAD))
4604 continue; /* skip non-loadable section */
4606 size = bfd_get_section_size_before_reloc (s);
4608 continue; /* skip zero-length section */
4610 sectname = (char *) bfd_get_section_name (exec_bfd, s);
4611 if (args && strcmp (args, sectname) != 0)
4612 continue; /* not the section selected by user */
4614 matched = 1; /* do this section */
4616 /* FIXME: assumes lma can fit into long */
4617 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4620 /* be clever; compute the host_crc before waiting for target reply */
4621 sectdata = xmalloc (size);
4622 old_chain = make_cleanup (free, sectdata);
4623 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4624 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4626 getpkt (buf, PBUFSIZ, 0);
4628 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4629 sectname, lma, lma + size);
4631 error ("remote target does not support this operation");
4633 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4634 target_crc = target_crc * 16 + fromhex (*tmp);
4636 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4637 sectname, paddr (lma), paddr (lma + size));
4638 if (host_crc == target_crc)
4639 printf_filtered ("matched.\n");
4642 printf_filtered ("MIS-MATCHED!\n");
4646 do_cleanups (old_chain);
4649 warning ("One or more sections of the remote executable does not match\n\
4650 the loaded file\n");
4651 if (args && !matched)
4652 printf_filtered ("No loaded section named '%s'.\n", args);
4656 remote_query (query_type, buf, outbuf, bufsiz)
4663 char *buf2 = alloca (PBUFSIZ);
4664 char *p2 = &buf2[0];
4667 error ("null pointer to remote bufer size specified");
4669 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4670 the caller know and return what the minimum size is */
4671 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4672 if (*bufsiz < PBUFSIZ)
4678 /* except for querying the minimum buffer size, target must be open */
4680 error ("remote query is only available after target open");
4682 /* we only take uppercase letters as query types, at least for now */
4683 if ((query_type < 'A') || (query_type > 'Z'))
4684 error ("invalid remote query type");
4687 error ("null remote query specified");
4690 error ("remote query requires a buffer to receive data");
4697 /* we used one buffer char for the remote protocol q command and another
4698 for the query type. As the remote protocol encapsulation uses 4 chars
4699 plus one extra in case we are debugging (remote_debug),
4700 we have PBUFZIZ - 7 left to pack the query string */
4702 while (buf[i] && (i < (PBUFSIZ - 8)))
4704 /* bad caller may have sent forbidden characters */
4705 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
4706 error ("illegal characters in query string");
4714 error ("query larger than available buffer");
4720 getpkt (outbuf, *bufsiz, 0);
4726 remote_rcmd (char *command,
4727 struct ui_file *outbuf)
4730 char *buf = alloca (PBUFSIZ);
4734 error ("remote rcmd is only available after target open");
4736 /* Send a NULL command across as an empty command */
4737 if (command == NULL)
4740 /* The query prefix */
4741 strcpy (buf, "qRcmd,");
4742 p = strchr (buf, '\0');
4744 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ)
4745 error ("\"monitor\" command ``%s'' is too long\n", command);
4747 /* Encode the actual command */
4748 for (i = 0; command[i]; i++)
4750 *p++ = tohex ((command[i] >> 4) & 0xf);
4751 *p++ = tohex (command[i] & 0xf);
4755 if (putpkt (buf) < 0)
4756 error ("Communication problem with target\n");
4758 /* get/display the response */
4761 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4763 getpkt (buf, PBUFSIZ, 0);
4765 error ("Target does not support this command\n");
4766 if (buf[0] == 'O' && buf[1] != 'K')
4768 remote_console_output (buf + 1); /* 'O' message from stub */
4771 if (strcmp (buf, "OK") == 0)
4773 if (strlen (buf) == 3 && buf[0] == 'E'
4774 && isdigit (buf[1]) && isdigit (buf[2]))
4776 error ("Protocol error with Rcmd");
4778 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
4780 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
4781 fputc_unfiltered (c, outbuf);
4788 packet_command (args, from_tty)
4792 char *buf = alloca (PBUFSIZ);
4795 error ("command can only be used with remote target");
4798 error ("remote-packet command requires packet text as argument");
4800 puts_filtered ("sending: ");
4801 print_packet (args);
4802 puts_filtered ("\n");
4805 getpkt (buf, PBUFSIZ, 0);
4806 puts_filtered ("received: ");
4808 puts_filtered ("\n");
4812 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4814 static void display_thread_info (struct gdb_ext_thread_info *info);
4816 static void threadset_test_cmd (char *cmd, int tty);
4818 static void threadalive_test (char *cmd, int tty);
4820 static void threadlist_test_cmd (char *cmd, int tty);
4822 int get_and_display_threadinfo (threadref * ref);
4824 static void threadinfo_test_cmd (char *cmd, int tty);
4826 static int thread_display_step (threadref * ref, void *context);
4828 static void threadlist_update_test_cmd (char *cmd, int tty);
4830 static void init_remote_threadtests (void);
4832 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
4835 threadset_test_cmd (cmd, tty)
4839 int sample_thread = SAMPLE_THREAD;
4841 printf_filtered ("Remote threadset test\n");
4842 set_thread (sample_thread, 1);
4847 threadalive_test (cmd, tty)
4851 int sample_thread = SAMPLE_THREAD;
4853 if (remote_thread_alive (sample_thread))
4854 printf_filtered ("PASS: Thread alive test\n");
4856 printf_filtered ("FAIL: Thread alive test\n");
4859 void output_threadid (char *title, threadref * ref);
4862 output_threadid (title, ref)
4868 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
4870 printf_filtered ("%s %s\n", title, (&hexid[0]));
4874 threadlist_test_cmd (cmd, tty)
4879 threadref nextthread;
4880 int done, result_count;
4881 threadref threadlist[3];
4883 printf_filtered ("Remote Threadlist test\n");
4884 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
4885 &result_count, &threadlist[0]))
4886 printf_filtered ("FAIL: threadlist test\n");
4889 threadref *scan = threadlist;
4890 threadref *limit = scan + result_count;
4892 while (scan < limit)
4893 output_threadid (" thread ", scan++);
4898 display_thread_info (info)
4899 struct gdb_ext_thread_info *info;
4901 output_threadid ("Threadid: ", &info->threadid);
4902 printf_filtered ("Name: %s\n ", info->shortname);
4903 printf_filtered ("State: %s\n", info->display);
4904 printf_filtered ("other: %s\n\n", info->more_display);
4908 get_and_display_threadinfo (ref)
4913 struct gdb_ext_thread_info threadinfo;
4915 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
4916 | TAG_MOREDISPLAY | TAG_DISPLAY;
4917 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
4918 display_thread_info (&threadinfo);
4923 threadinfo_test_cmd (cmd, tty)
4927 int athread = SAMPLE_THREAD;
4931 int_to_threadref (&thread, athread);
4932 printf_filtered ("Remote Threadinfo test\n");
4933 if (!get_and_display_threadinfo (&thread))
4934 printf_filtered ("FAIL cannot get thread info\n");
4938 thread_display_step (ref, context)
4942 /* output_threadid(" threadstep ",ref); *//* simple test */
4943 return get_and_display_threadinfo (ref);
4947 threadlist_update_test_cmd (cmd, tty)
4951 printf_filtered ("Remote Threadlist update test\n");
4952 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
4956 init_remote_threadtests (void)
4958 add_com ("tlist", class_obscure, threadlist_test_cmd,
4959 "Fetch and print the remote list of thread identifiers, one pkt only");
4960 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
4961 "Fetch and display info about one thread");
4962 add_com ("tset", class_obscure, threadset_test_cmd,
4963 "Test setting to a different thread");
4964 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
4965 "Iterate through updating all remote thread info");
4966 add_com ("talive", class_obscure, threadalive_test,
4967 " Remote thread alive test ");
4975 remote_ops.to_shortname = "remote";
4976 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
4978 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4979 Specify the serial device it is connected to\n\
4980 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
4981 remote_ops.to_open = remote_open;
4982 remote_ops.to_close = remote_close;
4983 remote_ops.to_detach = remote_detach;
4984 remote_ops.to_resume = remote_resume;
4985 remote_ops.to_wait = remote_wait;
4986 remote_ops.to_fetch_registers = remote_fetch_registers;
4987 remote_ops.to_store_registers = remote_store_registers;
4988 remote_ops.to_prepare_to_store = remote_prepare_to_store;
4989 remote_ops.to_xfer_memory = remote_xfer_memory;
4990 remote_ops.to_files_info = remote_files_info;
4991 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
4992 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
4993 remote_ops.to_kill = remote_kill;
4994 remote_ops.to_load = generic_load;
4995 remote_ops.to_mourn_inferior = remote_mourn;
4996 remote_ops.to_thread_alive = remote_thread_alive;
4997 remote_ops.to_find_new_threads = remote_threads_info;
4998 remote_ops.to_extra_thread_info = remote_threads_extra_info;
4999 remote_ops.to_stop = remote_stop;
5000 remote_ops.to_query = remote_query;
5001 remote_ops.to_rcmd = remote_rcmd;
5002 remote_ops.to_stratum = process_stratum;
5003 remote_ops.to_has_all_memory = 1;
5004 remote_ops.to_has_memory = 1;
5005 remote_ops.to_has_stack = 1;
5006 remote_ops.to_has_registers = 1;
5007 remote_ops.to_has_execution = 1;
5008 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5009 remote_ops.to_magic = OPS_MAGIC;
5012 /* Set up the extended remote vector by making a copy of the standard
5013 remote vector and adding to it. */
5016 init_extended_remote_ops ()
5018 extended_remote_ops = remote_ops;
5020 extended_remote_ops.to_shortname = "extended-remote";
5021 extended_remote_ops.to_longname =
5022 "Extended remote serial target in gdb-specific protocol";
5023 extended_remote_ops.to_doc =
5024 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5025 Specify the serial device it is connected to (e.g. /dev/ttya).",
5026 extended_remote_ops.to_open = extended_remote_open;
5027 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5028 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5032 * Command: info remote-process
5034 * This implements Cisco's version of the "info proc" command.
5036 * This query allows the target stub to return an arbitrary string
5037 * (or strings) giving arbitrary information about the target process.
5038 * This is optional; the target stub isn't required to implement it.
5040 * Syntax: qfProcessInfo request first string
5041 * qsProcessInfo request subsequent string
5042 * reply: 'O'<hex-encoded-string>
5043 * 'l' last reply (empty)
5047 remote_info_process (char *args, int from_tty)
5049 char *buf = alloca (PBUFSIZ);
5051 if (remote_desc == 0)
5052 error ("Command can only be used when connected to the remote target.");
5054 putpkt ("qfProcessInfo");
5055 getpkt (buf, PBUFSIZ, 0);
5057 return; /* Silently: target does not support this feature. */
5060 error ("info proc: target error.");
5062 while (buf[0] == 'O') /* Capitol-O packet */
5064 remote_console_output (&buf[1]);
5065 putpkt ("qsProcessInfo");
5066 getpkt (buf, PBUFSIZ, 0);
5075 remote_cisco_open (char *name, int from_tty)
5079 "To open a remote debug connection, you need to specify what \n\
5080 device is attached to the remote system (e.g. host:port).");
5082 /* See FIXME above */
5083 wait_forever_enabled_p = 1;
5085 target_preopen (from_tty);
5087 unpush_target (&remote_cisco_ops);
5089 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
5091 remote_desc = SERIAL_OPEN (name);
5093 perror_with_name (name);
5096 * If a baud rate was specified on the gdb command line it will
5097 * be greater than the initial value of -1. If it is, use it otherwise
5101 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
5102 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
5104 SERIAL_CLOSE (remote_desc);
5105 perror_with_name (name);
5108 SERIAL_RAW (remote_desc);
5110 /* If there is something sitting in the buffer we might take it as a
5111 response to a command, which would be bad. */
5112 SERIAL_FLUSH_INPUT (remote_desc);
5116 puts_filtered ("Remote debugging using ");
5117 puts_filtered (name);
5118 puts_filtered ("\n");
5121 remote_cisco_mode = 1;
5123 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5125 init_packet_config (&remote_protocol_P);
5126 init_packet_config (&remote_protocol_Z);
5128 general_thread = -2;
5129 continue_thread = -2;
5131 /* Force remote_write_bytes to check whether target supports
5132 binary downloading. */
5133 init_packet_config (&remote_protocol_binary_download);
5135 /* Probe for ability to use "ThreadInfo" query, as required. */
5136 use_threadinfo_query = 1;
5137 use_threadextra_query = 1;
5139 /* Without this, some commands which require an active target (such
5140 as kill) won't work. This variable serves (at least) double duty
5141 as both the pid of the target process (if it has such), and as a
5142 flag indicating that a target is active. These functions should
5143 be split out into seperate variables, especially since GDB will
5144 someday have a notion of debugging several processes. */
5145 inferior_pid = MAGIC_NULL_PID;
5147 /* Start the remote connection; if error (0), discard this target. */
5149 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5150 "Couldn't establish connection to remote target\n",
5159 remote_cisco_close (int quitting)
5161 remote_cisco_mode = 0;
5162 remote_close (quitting);
5169 remote_mourn_1 (&remote_cisco_ops);
5181 /* shared between readsocket() and readtty() */
5182 static char *tty_input;
5184 static int escape_count;
5185 static int echo_check;
5186 extern int quit_flag;
5193 /* Loop until the socket doesn't have any more data */
5195 while ((data = readchar (0)) >= 0)
5197 /* Check for the escape sequence */
5200 /* If this is the fourth escape, get out */
5201 if (++escape_count == 4)
5206 { /* This is a '|', but not the fourth in a row.
5207 Continue without echoing it. If it isn't actually
5208 one of four in a row, it'll be echoed later. */
5215 /* Ensure any pending '|'s are flushed. */
5217 for (; escape_count > 0; escape_count--)
5221 if (data == '\r') /* If this is a return character, */
5222 continue; /* - just supress it. */
5224 if (echo_check != -1) /* Check for echo of user input. */
5226 if (tty_input[echo_check] == data)
5228 echo_check++; /* Character matched user input: */
5229 continue; /* Continue without echoing it. */
5231 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5232 { /* End of the line (and of echo checking). */
5233 echo_check = -1; /* No more echo supression */
5234 continue; /* Continue without echoing. */
5237 { /* Failed check for echo of user input.
5238 We now have some suppressed output to flush! */
5241 for (j = 0; j < echo_check; j++)
5242 putchar (tty_input[j]);
5246 putchar (data); /* Default case: output the char. */
5249 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5250 return READ_MORE; /* Try to read some more */
5252 return FATAL_ERROR; /* Trouble, bail out */
5260 /* First, read a buffer full from the terminal */
5261 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
5262 if (tty_bytecount == -1)
5264 perror ("readtty: read failed");
5268 /* Remove a quoted newline. */
5269 if (tty_input[tty_bytecount - 1] == '\n' &&
5270 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5272 tty_input[--tty_bytecount] = 0; /* remove newline */
5273 tty_input[--tty_bytecount] = 0; /* remove backslash */
5276 /* Turn trailing newlines into returns */
5277 if (tty_input[tty_bytecount - 1] == '\n')
5278 tty_input[tty_bytecount - 1] = '\r';
5280 /* If the line consists of a ~, enter debugging mode. */
5281 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5284 /* Make this a zero terminated string and write it out */
5285 tty_input[tty_bytecount] = 0;
5286 if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount))
5288 perror_with_name ("readtty: write failed");
5298 fd_set input; /* file descriptors for select */
5299 int tablesize; /* max number of FDs for select */
5303 extern int escape_count; /* global shared by readsocket */
5304 extern int echo_check; /* ditto */
5309 tablesize = 8 * sizeof (input);
5313 /* Check for anything from our socket - doesn't block. Note that
5314 this must be done *before* the select as there may be
5315 buffered I/O waiting to be processed. */
5317 if ((status = readsocket ()) == FATAL_ERROR)
5319 error ("Debugging terminated by communications error");
5321 else if (status != READ_MORE)
5326 fflush (stdout); /* Flush output before blocking */
5328 /* Now block on more socket input or TTY input */
5331 FD_SET (fileno (stdin), &input);
5332 FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
5334 status = select (tablesize, &input, 0, 0, 0);
5335 if ((status == -1) && (errno != EINTR))
5337 error ("Communications error on select %d", errno);
5340 /* Handle Control-C typed */
5344 if ((++quit_count) == 2)
5346 if (query ("Interrupt GDB? "))
5348 printf_filtered ("Interrupted by user.\n");
5349 return_to_top_level (RETURN_QUIT);
5356 SERIAL_SEND_BREAK (remote_desc);
5358 SERIAL_WRITE (remote_desc, "\003", 1);
5363 /* Handle console input */
5365 if (FD_ISSET (fileno (stdin), &input))
5369 status = readtty ();
5370 if (status == READ_MORE)
5373 return status; /* telnet session ended */
5379 remote_cisco_wait (int pid, struct target_waitstatus *status)
5381 if (minitelnet () != ENTER_DEBUG)
5383 error ("Debugging session terminated by protocol error");
5386 return remote_wait (pid, status);
5390 init_remote_cisco_ops ()
5392 remote_cisco_ops.to_shortname = "cisco";
5393 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5394 remote_cisco_ops.to_doc =
5395 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5396 Specify the serial device it is connected to (e.g. host:2020).";
5397 remote_cisco_ops.to_open = remote_cisco_open;
5398 remote_cisco_ops.to_close = remote_cisco_close;
5399 remote_cisco_ops.to_detach = remote_detach;
5400 remote_cisco_ops.to_resume = remote_resume;
5401 remote_cisco_ops.to_wait = remote_cisco_wait;
5402 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5403 remote_cisco_ops.to_store_registers = remote_store_registers;
5404 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5405 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5406 remote_cisco_ops.to_files_info = remote_files_info;
5407 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5408 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
5409 remote_cisco_ops.to_kill = remote_kill;
5410 remote_cisco_ops.to_load = generic_load;
5411 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5412 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5413 remote_cisco_ops.to_find_new_threads = remote_threads_info;
5414 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5415 remote_cisco_ops.to_stratum = process_stratum;
5416 remote_cisco_ops.to_has_all_memory = 1;
5417 remote_cisco_ops.to_has_memory = 1;
5418 remote_cisco_ops.to_has_stack = 1;
5419 remote_cisco_ops.to_has_registers = 1;
5420 remote_cisco_ops.to_has_execution = 1;
5421 remote_cisco_ops.to_magic = OPS_MAGIC;
5425 remote_can_async_p (void)
5427 /* We're async whenever the serial device is. */
5428 return (current_target.to_async_mask_value) && SERIAL_CAN_ASYNC_P (remote_desc);
5432 remote_is_async_p (void)
5434 /* We're async whenever the serial device is. */
5435 return (current_target.to_async_mask_value) && SERIAL_IS_ASYNC_P (remote_desc);
5438 /* Pass the SERIAL event on and up to the client. One day this code
5439 will be able to delay notifying the client of an event until the
5440 point where an entire packet has been received. */
5442 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5443 static void *async_client_context;
5444 static serial_event_ftype remote_async_serial_handler;
5447 remote_async_serial_handler (serial_t scb, void *context)
5449 /* Don't propogate error information up to the client. Instead let
5450 the client find out about the error by querying the target. */
5451 async_client_callback (INF_REG_EVENT, async_client_context);
5455 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5457 if (current_target.to_async_mask_value == 0)
5458 internal_error ("Calling remote_async when async is masked");
5460 if (callback != NULL)
5462 SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
5463 async_client_callback = callback;
5464 async_client_context = context;
5467 SERIAL_ASYNC (remote_desc, NULL, NULL);
5470 /* Target async and target extended-async.
5472 This are temporary targets, until it is all tested. Eventually
5473 async support will be incorporated int the usual 'remote'
5477 init_remote_async_ops (void)
5479 remote_async_ops.to_shortname = "async";
5480 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5481 remote_async_ops.to_doc =
5482 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5483 Specify the serial device it is connected to (e.g. /dev/ttya).";
5484 remote_async_ops.to_open = remote_async_open;
5485 remote_async_ops.to_close = remote_close;
5486 remote_async_ops.to_detach = remote_async_detach;
5487 remote_async_ops.to_resume = remote_async_resume;
5488 remote_async_ops.to_wait = remote_async_wait;
5489 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5490 remote_async_ops.to_store_registers = remote_store_registers;
5491 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5492 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5493 remote_async_ops.to_files_info = remote_files_info;
5494 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5495 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5496 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5497 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5498 remote_async_ops.to_kill = remote_async_kill;
5499 remote_async_ops.to_load = generic_load;
5500 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5501 remote_async_ops.to_thread_alive = remote_thread_alive;
5502 remote_async_ops.to_find_new_threads = remote_threads_info;
5503 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5504 remote_async_ops.to_stop = remote_stop;
5505 remote_async_ops.to_query = remote_query;
5506 remote_async_ops.to_rcmd = remote_rcmd;
5507 remote_async_ops.to_stratum = process_stratum;
5508 remote_async_ops.to_has_all_memory = 1;
5509 remote_async_ops.to_has_memory = 1;
5510 remote_async_ops.to_has_stack = 1;
5511 remote_async_ops.to_has_registers = 1;
5512 remote_async_ops.to_has_execution = 1;
5513 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5514 remote_async_ops.to_can_async_p = remote_can_async_p;
5515 remote_async_ops.to_is_async_p = remote_is_async_p;
5516 remote_async_ops.to_async = remote_async;
5517 remote_async_ops.to_async_mask_value = 1;
5518 remote_async_ops.to_magic = OPS_MAGIC;
5521 /* Set up the async extended remote vector by making a copy of the standard
5522 remote vector and adding to it. */
5525 init_extended_async_remote_ops (void)
5527 extended_async_remote_ops = remote_async_ops;
5529 extended_async_remote_ops.to_shortname = "extended-async";
5530 extended_async_remote_ops.to_longname =
5531 "Extended remote serial target in async gdb-specific protocol";
5532 extended_async_remote_ops.to_doc =
5533 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5534 Specify the serial device it is connected to (e.g. /dev/ttya).",
5535 extended_async_remote_ops.to_open = extended_remote_async_open;
5536 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5537 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5541 set_remote_cmd (char *args, int from_tty)
5548 build_remote_gdbarch_data ()
5550 build_remote_packet_sizes ();
5553 tty_input = xmalloc (PBUFSIZ);
5554 remote_address_size = TARGET_PTR_BIT;
5558 _initialize_remote ()
5560 static struct cmd_list_element *remote_set_cmdlist;
5561 static struct cmd_list_element *remote_show_cmdlist;
5562 struct cmd_list_element *tmpcmd;
5564 /* architecture specific data */
5565 build_remote_gdbarch_data ();
5566 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
5567 register_remote_packet_sizes ();
5568 register_gdbarch_swap (&remote_address_size,
5569 sizeof (&remote_address_size), NULL);
5570 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5573 add_target (&remote_ops);
5575 init_extended_remote_ops ();
5576 add_target (&extended_remote_ops);
5578 init_remote_async_ops ();
5579 add_target (&remote_async_ops);
5581 init_extended_async_remote_ops ();
5582 add_target (&extended_async_remote_ops);
5584 init_remote_cisco_ops ();
5585 add_target (&remote_cisco_ops);
5588 init_remote_threadtests ();
5591 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5592 Remote protocol specific variables\n\
5593 Configure various remote-protocol specific variables such as\n\
5594 the packets being used",
5595 &remote_set_cmdlist, "set remote ",
5596 0/*allow-unknown*/, &setlist);
5597 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5598 Remote protocol specific variables\n\
5599 Configure various remote-protocol specific variables such as\n\
5600 the packets being used",
5601 &remote_show_cmdlist, "show remote ",
5602 0/*allow-unknown*/, &showlist);
5604 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5605 "Compare section data on target to the exec file.\n\
5606 Argument is a single section name (default: all loaded sections).",
5609 add_cmd ("packet", class_maintenance, packet_command,
5610 "Send an arbitrary packet to a remote target.\n\
5611 maintenance packet TEXT\n\
5612 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5613 this command sends the string TEXT to the inferior, and displays the\n\
5614 response packet. GDB supplies the initial `$' character, and the\n\
5615 terminating `#' character and checksum.",
5619 (add_set_cmd ("remotebreak", no_class,
5620 var_boolean, (char *) &remote_break,
5621 "Set whether to send break if interrupted.\n",
5625 /* Install commands for configuring memory read/write packets. */
5627 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5628 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5630 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5631 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5633 add_cmd ("memory-write-packet-size", no_class,
5634 set_memory_write_packet_size,
5635 "Set the maximum number of bytes per memory-write packet.\n"
5636 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5637 "default packet size. The actual limit is further reduced\n"
5638 "dependent on the target. Specify ``fixed'' to disable the\n"
5639 "further restriction and ``limit'' to enable that restriction\n",
5640 &remote_set_cmdlist);
5641 add_cmd ("memory-read-packet-size", no_class,
5642 set_memory_read_packet_size,
5643 "Set the maximum number of bytes per memory-read packet.\n"
5644 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5645 "default packet size. The actual limit is further reduced\n"
5646 "dependent on the target. Specify ``fixed'' to disable the\n"
5647 "further restriction and ``limit'' to enable that restriction\n",
5648 &remote_set_cmdlist);
5649 add_cmd ("memory-write-packet-size", no_class,
5650 show_memory_write_packet_size,
5651 "Show the maximum number of bytes per memory-write packet.\n",
5652 &remote_show_cmdlist);
5653 add_cmd ("memory-read-packet-size", no_class,
5654 show_memory_read_packet_size,
5655 "Show the maximum number of bytes per memory-read packet.\n",
5656 &remote_show_cmdlist);
5659 (add_set_cmd ("remoteaddresssize", class_obscure,
5660 var_integer, (char *) &remote_address_size,
5661 "Set the maximum size of the address (in bits) \
5662 in a memory packet.\n",
5666 add_packet_config_cmd (&remote_protocol_binary_download,
5667 "X", "binary-download",
5668 set_remote_protocol_binary_download_cmd,
5669 show_remote_protocol_binary_download_cmd,
5670 &remote_set_cmdlist, &remote_show_cmdlist);
5672 /* XXXX - should ``set remotebinarydownload'' be retained for
5675 (add_set_cmd ("remotebinarydownload", no_class,
5676 var_boolean, (char *) &remote_binary_download,
5677 "Set binary downloads.\n", &setlist),
5681 add_info ("remote-process", remote_info_process,
5682 "Query the remote system for process info.");
5684 add_packet_config_cmd (&remote_protocol_P, "P", "set-register",
5685 set_remote_protocol_P_packet_cmd,
5686 show_remote_protocol_P_packet_cmd,
5687 &remote_set_cmdlist, &remote_show_cmdlist);
5689 add_packet_config_cmd (&remote_protocol_Z, "Z", "breakpoint",
5690 set_remote_protocol_Z_packet_cmd,
5691 show_remote_protocol_Z_packet_cmd,
5692 &remote_set_cmdlist, &remote_show_cmdlist);