1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 2010, 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
63 #include "xml-support.h"
65 #include "memory-map.h"
67 #include "tracepoint.h"
71 /* Temp hacks for tracepoint encoding migration. */
72 static char *target_buf;
73 static long target_buf_size;
75 encode_actions (struct breakpoint *t, struct bp_location *tloc,
76 char ***tdp_actions, char ***stepping_actions);
78 /* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88 enum { REMOTE_ALIGN_WRITES = 16 };
90 /* Prototypes for local functions. */
91 static void cleanup_sigint_signal_handler (void *dummy);
92 static void initialize_sigint_signal_handler (void);
93 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
94 static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
97 static void handle_remote_sigint (int);
98 static void handle_remote_sigint_twice (int);
99 static void async_remote_interrupt (gdb_client_data);
100 void async_remote_interrupt_twice (gdb_client_data);
102 static void remote_files_info (struct target_ops *ignore);
104 static void remote_prepare_to_store (struct regcache *regcache);
106 static void remote_open (char *name, int from_tty);
108 static void extended_remote_open (char *name, int from_tty);
110 static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
112 static void remote_close (int quitting);
114 static void remote_mourn (struct target_ops *ops);
116 static void extended_remote_restart (void);
118 static void extended_remote_mourn (struct target_ops *);
120 static void remote_mourn_1 (struct target_ops *);
122 static void remote_send (char **buf, long *sizeof_buf_p);
124 static int readchar (int timeout);
126 static void remote_kill (struct target_ops *ops);
128 static int tohex (int nib);
130 static int remote_can_async_p (void);
132 static int remote_is_async_p (void);
134 static void remote_async (void (*callback) (enum inferior_event_type event_type,
135 void *context), void *context);
137 static int remote_async_mask (int new_mask);
139 static void remote_detach (struct target_ops *ops, char *args, int from_tty);
141 static void remote_interrupt (int signo);
143 static void remote_interrupt_twice (int signo);
145 static void interrupt_query (void);
147 static void set_general_thread (struct ptid ptid);
148 static void set_continue_thread (struct ptid ptid);
150 static void get_offsets (void);
152 static void skip_frame (void);
154 static long read_frame (char **buf_p, long *sizeof_buf);
156 static int hexnumlen (ULONGEST num);
158 static void init_remote_ops (void);
160 static void init_extended_remote_ops (void);
162 static void remote_stop (ptid_t);
164 static int ishex (int ch, int *val);
166 static int stubhex (int ch);
168 static int hexnumstr (char *, ULONGEST);
170 static int hexnumnstr (char *, ULONGEST, int);
172 static CORE_ADDR remote_address_masked (CORE_ADDR);
174 static void print_packet (char *);
176 static void compare_sections_command (char *, int);
178 static void packet_command (char *, int);
180 static int stub_unpack_int (char *buff, int fieldlength);
182 static ptid_t remote_current_thread (ptid_t oldptid);
184 static void remote_find_new_threads (void);
186 static void record_currthread (ptid_t currthread);
188 static int fromhex (int a);
190 extern int hex2bin (const char *hex, gdb_byte *bin, int count);
192 extern int bin2hex (const gdb_byte *bin, char *hex, int count);
194 static int putpkt_binary (char *buf, int cnt);
196 static void check_binary_download (CORE_ADDR addr);
198 struct packet_config;
200 static void show_packet_config_cmd (struct packet_config *config);
202 static void update_packet_config (struct packet_config *config);
204 static void set_remote_protocol_packet_cmd (char *args, int from_tty,
205 struct cmd_list_element *c);
207 static void show_remote_protocol_packet_cmd (struct ui_file *file,
209 struct cmd_list_element *c,
212 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
213 static ptid_t read_ptid (char *buf, char **obuf);
215 static void remote_set_permissions (void);
218 static int remote_get_trace_status (struct trace_status *ts);
220 static int remote_upload_tracepoints (struct uploaded_tp **utpp);
222 static int remote_upload_trace_state_variables (struct uploaded_tsv **utsvp);
224 static void remote_query_supported (void);
226 static void remote_check_symbols (struct objfile *objfile);
228 void _initialize_remote (void);
231 static struct stop_reply *stop_reply_xmalloc (void);
232 static void stop_reply_xfree (struct stop_reply *);
233 static void do_stop_reply_xfree (void *arg);
234 static void remote_parse_stop_reply (char *buf, struct stop_reply *);
235 static void push_stop_reply (struct stop_reply *);
236 static void remote_get_pending_stop_replies (void);
237 static void discard_pending_stop_replies (int pid);
238 static int peek_stop_reply (ptid_t ptid);
240 static void remote_async_inferior_event_handler (gdb_client_data);
241 static void remote_async_get_pending_events_handler (gdb_client_data);
243 static void remote_terminal_ours (void);
245 static int remote_read_description_p (struct target_ops *target);
247 static void remote_console_output (char *msg);
249 /* The non-stop remote protocol provisions for one pending stop reply.
250 This is where we keep it until it is acknowledged. */
252 static struct stop_reply *pending_stop_reply = NULL;
256 static struct cmd_list_element *remote_cmdlist;
258 /* For "set remote" and "show remote". */
260 static struct cmd_list_element *remote_set_cmdlist;
261 static struct cmd_list_element *remote_show_cmdlist;
263 /* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
278 /* If we negotiated packet size explicitly (and thus can bypass
279 heuristics for the largest packet size that will not overflow
280 a buffer in the stub), this will be set to that packet size.
281 Otherwise zero, meaning to use the guessed size. */
282 long explicit_packet_size;
284 /* remote_wait is normally called when the target is running and
285 waits for a stop reply packet. But sometimes we need to call it
286 when the target is already stopped. We can send a "?" packet
287 and have remote_wait read the response. Or, if we already have
288 the response, we can stash it in BUF and tell remote_wait to
289 skip calling getpkt. This flag is set when BUF contains a
290 stop reply packet and the target is not waiting. */
291 int cached_wait_status;
293 /* True, if in no ack mode. That is, neither GDB nor the stub will
294 expect acks from each other. The connection is assumed to be
298 /* True if we're connected in extended remote mode. */
301 /* True if the stub reported support for multi-process
303 int multi_process_aware;
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply;
313 /* True if the stub reports support for non-stop mode. */
316 /* True if the stub reports support for vCont;t. */
319 /* True if the stub reports support for conditional tracepoints. */
320 int cond_tracepoints;
322 /* True if the stub reports support for fast tracepoints. */
323 int fast_tracepoints;
325 /* True if the stub reports support for static tracepoints. */
326 int static_tracepoints;
328 /* True if the stub can continue running a trace while GDB is
330 int disconnected_tracing;
332 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
333 responded to that. */
337 /* Private data that we'll store in (struct thread_info)->private. */
338 struct private_thread_info
345 free_private_thread_info (struct private_thread_info *info)
351 /* Returns true if the multi-process extensions are in effect. */
353 remote_multi_process_p (struct remote_state *rs)
355 return rs->extended && rs->multi_process_aware;
358 /* This data could be associated with a target, but we do not always
359 have access to the current target when we need it, so for now it is
360 static. This will be fine for as long as only one target is in use
362 static struct remote_state remote_state;
364 static struct remote_state *
365 get_remote_state_raw (void)
367 return &remote_state;
370 /* Description of the remote protocol for a given architecture. */
374 long offset; /* Offset into G packet. */
375 long regnum; /* GDB's internal register number. */
376 LONGEST pnum; /* Remote protocol register number. */
377 int in_g_packet; /* Always part of G packet. */
378 /* long size in bytes; == register_size (target_gdbarch, regnum);
380 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
384 struct remote_arch_state
386 /* Description of the remote protocol registers. */
387 long sizeof_g_packet;
389 /* Description of the remote protocol registers indexed by REGNUM
390 (making an array gdbarch_num_regs in size). */
391 struct packet_reg *regs;
393 /* This is the size (in chars) of the first response to the ``g''
394 packet. It is used as a heuristic when determining the maximum
395 size of memory-read and memory-write packets. A target will
396 typically only reserve a buffer large enough to hold the ``g''
397 packet. The size does not include packet overhead (headers and
399 long actual_register_packet_size;
401 /* This is the maximum size (in chars) of a non read/write packet.
402 It is also used as a cap on the size of read/write packets. */
403 long remote_packet_size;
406 long sizeof_pkt = 2000;
408 /* Utility: generate error from an incoming stub packet. */
410 trace_error (char *buf)
413 return; /* not an error msg */
416 case '1': /* malformed packet error */
417 if (*++buf == '0') /* general case: */
418 error (_("remote.c: error in outgoing packet."));
420 error (_("remote.c: error in outgoing packet at field #%ld."),
421 strtol (buf, NULL, 16));
423 error (_("trace API error 0x%s."), ++buf);
425 error (_("Target returns error code '%s'."), buf);
429 /* Utility: wait for reply from stub, while accepting "O" packets. */
431 remote_get_noisy_reply (char **buf_p,
434 do /* Loop on reply from remote stub. */
438 QUIT; /* Allow user to bail out with ^C. */
439 getpkt (buf_p, sizeof_buf, 0);
443 else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
446 CORE_ADDR from, to, org_to;
448 int adjusted_size = 0;
449 volatile struct gdb_exception ex;
451 p = buf + strlen ("qRelocInsn:");
452 pp = unpack_varlen_hex (p, &ul);
454 error (_("invalid qRelocInsn packet: %s"), buf);
458 pp = unpack_varlen_hex (p, &ul);
463 TRY_CATCH (ex, RETURN_MASK_ALL)
465 gdbarch_relocate_instruction (target_gdbarch, &to, from);
469 adjusted_size = to - org_to;
471 sprintf (buf, "qRelocInsn:%x", adjusted_size);
474 else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
476 /* Propagate memory errors silently back to the target.
477 The stub may have limited the range of addresses we
478 can write to, for example. */
483 /* Something unexpectedly bad happened. Be verbose so
484 we can tell what, and propagate the error back to the
485 stub, so it doesn't get stuck waiting for a
487 exception_fprintf (gdb_stderr, ex,
488 _("warning: relocating instruction: "));
492 else if (buf[0] == 'O' && buf[1] != 'K')
493 remote_console_output (buf + 1); /* 'O' message from stub */
495 return buf; /* Here's the actual reply. */
500 /* Handle for retreving the remote protocol data from gdbarch. */
501 static struct gdbarch_data *remote_gdbarch_data_handle;
503 static struct remote_arch_state *
504 get_remote_arch_state (void)
506 return gdbarch_data (target_gdbarch, remote_gdbarch_data_handle);
509 /* Fetch the global remote target state. */
511 static struct remote_state *
512 get_remote_state (void)
514 /* Make sure that the remote architecture state has been
515 initialized, because doing so might reallocate rs->buf. Any
516 function which calls getpkt also needs to be mindful of changes
517 to rs->buf, but this call limits the number of places which run
519 get_remote_arch_state ();
521 return get_remote_state_raw ();
525 compare_pnums (const void *lhs_, const void *rhs_)
527 const struct packet_reg * const *lhs = lhs_;
528 const struct packet_reg * const *rhs = rhs_;
530 if ((*lhs)->pnum < (*rhs)->pnum)
532 else if ((*lhs)->pnum == (*rhs)->pnum)
539 init_remote_state (struct gdbarch *gdbarch)
541 int regnum, num_remote_regs, offset;
542 struct remote_state *rs = get_remote_state_raw ();
543 struct remote_arch_state *rsa;
544 struct packet_reg **remote_regs;
546 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
548 /* Use the architecture to build a regnum<->pnum table, which will be
549 1:1 unless a feature set specifies otherwise. */
550 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
551 gdbarch_num_regs (gdbarch),
553 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
555 struct packet_reg *r = &rsa->regs[regnum];
557 if (register_size (gdbarch, regnum) == 0)
558 /* Do not try to fetch zero-sized (placeholder) registers. */
561 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
566 /* Define the g/G packet format as the contents of each register
567 with a remote protocol number, in order of ascending protocol
570 remote_regs = alloca (gdbarch_num_regs (gdbarch)
571 * sizeof (struct packet_reg *));
572 for (num_remote_regs = 0, regnum = 0;
573 regnum < gdbarch_num_regs (gdbarch);
575 if (rsa->regs[regnum].pnum != -1)
576 remote_regs[num_remote_regs++] = &rsa->regs[regnum];
578 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
581 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
583 remote_regs[regnum]->in_g_packet = 1;
584 remote_regs[regnum]->offset = offset;
585 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
588 /* Record the maximum possible size of the g packet - it may turn out
590 rsa->sizeof_g_packet = offset;
592 /* Default maximum number of characters in a packet body. Many
593 remote stubs have a hardwired buffer size of 400 bytes
594 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
595 as the maximum packet-size to ensure that the packet and an extra
596 NUL character can always fit in the buffer. This stops GDB
597 trashing stubs that try to squeeze an extra NUL into what is
598 already a full buffer (As of 1999-12-04 that was most stubs). */
599 rsa->remote_packet_size = 400 - 1;
601 /* This one is filled in when a ``g'' packet is received. */
602 rsa->actual_register_packet_size = 0;
604 /* Should rsa->sizeof_g_packet needs more space than the
605 default, adjust the size accordingly. Remember that each byte is
606 encoded as two characters. 32 is the overhead for the packet
607 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
608 (``$NN:G...#NN'') is a better guess, the below has been padded a
610 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
611 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
613 /* Make sure that the packet buffer is plenty big enough for
614 this architecture. */
615 if (rs->buf_size < rsa->remote_packet_size)
617 rs->buf_size = 2 * rsa->remote_packet_size;
618 rs->buf = xrealloc (rs->buf, rs->buf_size);
624 /* Return the current allowed size of a remote packet. This is
625 inferred from the current architecture, and should be used to
626 limit the length of outgoing packets. */
628 get_remote_packet_size (void)
630 struct remote_state *rs = get_remote_state ();
631 struct remote_arch_state *rsa = get_remote_arch_state ();
633 if (rs->explicit_packet_size)
634 return rs->explicit_packet_size;
636 return rsa->remote_packet_size;
639 static struct packet_reg *
640 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
642 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch))
646 struct packet_reg *r = &rsa->regs[regnum];
648 gdb_assert (r->regnum == regnum);
653 static struct packet_reg *
654 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
658 for (i = 0; i < gdbarch_num_regs (target_gdbarch); i++)
660 struct packet_reg *r = &rsa->regs[i];
668 /* FIXME: graces/2002-08-08: These variables should eventually be
669 bound to an instance of the target object (as in gdbarch-tdep()),
670 when such a thing exists. */
672 /* This is set to the data address of the access causing the target
673 to stop for a watchpoint. */
674 static CORE_ADDR remote_watch_data_address;
676 /* This is non-zero if target stopped for a watchpoint. */
677 static int remote_stopped_by_watchpoint_p;
679 static struct target_ops remote_ops;
681 static struct target_ops extended_remote_ops;
683 static int remote_async_mask_value = 1;
685 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
686 ``forever'' still use the normal timeout mechanism. This is
687 currently used by the ASYNC code to guarentee that target reads
688 during the initial connect always time-out. Once getpkt has been
689 modified to return a timeout indication and, in turn
690 remote_wait()/wait_for_inferior() have gained a timeout parameter
692 static int wait_forever_enabled_p = 1;
694 /* Allow the user to specify what sequence to send to the remote
695 when he requests a program interruption: Although ^C is usually
696 what remote systems expect (this is the default, here), it is
697 sometimes preferable to send a break. On other systems such
698 as the Linux kernel, a break followed by g, which is Magic SysRq g
699 is required in order to interrupt the execution. */
700 const char interrupt_sequence_control_c[] = "Ctrl-C";
701 const char interrupt_sequence_break[] = "BREAK";
702 const char interrupt_sequence_break_g[] = "BREAK-g";
703 static const char *interrupt_sequence_modes[] =
705 interrupt_sequence_control_c,
706 interrupt_sequence_break,
707 interrupt_sequence_break_g,
710 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
713 show_interrupt_sequence (struct ui_file *file, int from_tty,
714 struct cmd_list_element *c,
717 if (interrupt_sequence_mode == interrupt_sequence_control_c)
718 fprintf_filtered (file,
719 _("Send the ASCII ETX character (Ctrl-c) "
720 "to the remote target to interrupt the "
721 "execution of the program.\n"));
722 else if (interrupt_sequence_mode == interrupt_sequence_break)
723 fprintf_filtered (file,
724 _("send a break signal to the remote target "
725 "to interrupt the execution of the program.\n"));
726 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
727 fprintf_filtered (file,
728 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
729 "the remote target to interrupt the execution "
730 "of Linux kernel.\n"));
732 internal_error (__FILE__, __LINE__,
733 _("Invalid value for interrupt_sequence_mode: %s."),
734 interrupt_sequence_mode);
737 /* This boolean variable specifies whether interrupt_sequence is sent
738 to the remote target when gdb connects to it.
739 This is mostly needed when you debug the Linux kernel: The Linux kernel
740 expects BREAK g which is Magic SysRq g for connecting gdb. */
741 static int interrupt_on_connect = 0;
743 /* This variable is used to implement the "set/show remotebreak" commands.
744 Since these commands are now deprecated in favor of "set/show remote
745 interrupt-sequence", it no longer has any effect on the code. */
746 static int remote_break;
749 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
752 interrupt_sequence_mode = interrupt_sequence_break;
754 interrupt_sequence_mode = interrupt_sequence_control_c;
758 show_remotebreak (struct ui_file *file, int from_tty,
759 struct cmd_list_element *c,
764 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
765 remote_open knows that we don't have a file open when the program
767 static struct serial *remote_desc = NULL;
769 /* This variable sets the number of bits in an address that are to be
770 sent in a memory ("M" or "m") packet. Normally, after stripping
771 leading zeros, the entire address would be sent. This variable
772 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
773 initial implementation of remote.c restricted the address sent in
774 memory packets to ``host::sizeof long'' bytes - (typically 32
775 bits). Consequently, for 64 bit targets, the upper 32 bits of an
776 address was never sent. Since fixing this bug may cause a break in
777 some remote targets this variable is principly provided to
778 facilitate backward compatibility. */
780 static int remote_address_size;
782 /* Temporary to track who currently owns the terminal. See
783 remote_terminal_* for more details. */
785 static int remote_async_terminal_ours_p;
787 /* The executable file to use for "run" on the remote side. */
789 static char *remote_exec_file = "";
792 /* User configurable variables for the number of characters in a
793 memory read/write packet. MIN (rsa->remote_packet_size,
794 rsa->sizeof_g_packet) is the default. Some targets need smaller
795 values (fifo overruns, et.al.) and some users need larger values
796 (speed up transfers). The variables ``preferred_*'' (the user
797 request), ``current_*'' (what was actually set) and ``forced_*''
798 (Positive - a soft limit, negative - a hard limit). */
800 struct memory_packet_config
807 /* Compute the current size of a read/write packet. Since this makes
808 use of ``actual_register_packet_size'' the computation is dynamic. */
811 get_memory_packet_size (struct memory_packet_config *config)
813 struct remote_state *rs = get_remote_state ();
814 struct remote_arch_state *rsa = get_remote_arch_state ();
816 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
817 law?) that some hosts don't cope very well with large alloca()
818 calls. Eventually the alloca() code will be replaced by calls to
819 xmalloc() and make_cleanups() allowing this restriction to either
820 be lifted or removed. */
821 #ifndef MAX_REMOTE_PACKET_SIZE
822 #define MAX_REMOTE_PACKET_SIZE 16384
824 /* NOTE: 20 ensures we can write at least one byte. */
825 #ifndef MIN_REMOTE_PACKET_SIZE
826 #define MIN_REMOTE_PACKET_SIZE 20
831 if (config->size <= 0)
832 what_they_get = MAX_REMOTE_PACKET_SIZE;
834 what_they_get = config->size;
838 what_they_get = get_remote_packet_size ();
839 /* Limit the packet to the size specified by the user. */
841 && what_they_get > config->size)
842 what_they_get = config->size;
844 /* Limit it to the size of the targets ``g'' response unless we have
845 permission from the stub to use a larger packet size. */
846 if (rs->explicit_packet_size == 0
847 && rsa->actual_register_packet_size > 0
848 && what_they_get > rsa->actual_register_packet_size)
849 what_they_get = rsa->actual_register_packet_size;
851 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
852 what_they_get = MAX_REMOTE_PACKET_SIZE;
853 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
854 what_they_get = MIN_REMOTE_PACKET_SIZE;
856 /* Make sure there is room in the global buffer for this packet
857 (including its trailing NUL byte). */
858 if (rs->buf_size < what_they_get + 1)
860 rs->buf_size = 2 * what_they_get;
861 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
864 return what_they_get;
867 /* Update the size of a read/write packet. If they user wants
868 something really big then do a sanity check. */
871 set_memory_packet_size (char *args, struct memory_packet_config *config)
873 int fixed_p = config->fixed_p;
874 long size = config->size;
877 error (_("Argument required (integer, `fixed' or `limited')."));
878 else if (strcmp (args, "hard") == 0
879 || strcmp (args, "fixed") == 0)
881 else if (strcmp (args, "soft") == 0
882 || strcmp (args, "limit") == 0)
888 size = strtoul (args, &end, 0);
890 error (_("Invalid %s (bad syntax)."), config->name);
892 /* Instead of explicitly capping the size of a packet to
893 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
894 instead allowed to set the size to something arbitrarily
896 if (size > MAX_REMOTE_PACKET_SIZE)
897 error (_("Invalid %s (too large)."), config->name);
901 if (fixed_p && !config->fixed_p)
903 if (! query (_("The target may not be able to correctly handle a %s\n"
904 "of %ld bytes. Change the packet size? "),
906 error (_("Packet size not changed."));
908 /* Update the config. */
909 config->fixed_p = fixed_p;
914 show_memory_packet_size (struct memory_packet_config *config)
916 printf_filtered (_("The %s is %ld. "), config->name, config->size);
918 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
919 get_memory_packet_size (config));
921 printf_filtered (_("Packets are limited to %ld bytes.\n"),
922 get_memory_packet_size (config));
925 static struct memory_packet_config memory_write_packet_config =
927 "memory-write-packet-size",
931 set_memory_write_packet_size (char *args, int from_tty)
933 set_memory_packet_size (args, &memory_write_packet_config);
937 show_memory_write_packet_size (char *args, int from_tty)
939 show_memory_packet_size (&memory_write_packet_config);
943 get_memory_write_packet_size (void)
945 return get_memory_packet_size (&memory_write_packet_config);
948 static struct memory_packet_config memory_read_packet_config =
950 "memory-read-packet-size",
954 set_memory_read_packet_size (char *args, int from_tty)
956 set_memory_packet_size (args, &memory_read_packet_config);
960 show_memory_read_packet_size (char *args, int from_tty)
962 show_memory_packet_size (&memory_read_packet_config);
966 get_memory_read_packet_size (void)
968 long size = get_memory_packet_size (&memory_read_packet_config);
970 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
971 extra buffer size argument before the memory read size can be
972 increased beyond this. */
973 if (size > get_remote_packet_size ())
974 size = get_remote_packet_size ();
979 /* Generic configuration support for packets the stub optionally
980 supports. Allows the user to specify the use of the packet as well
981 as allowing GDB to auto-detect support in the remote stub. */
985 PACKET_SUPPORT_UNKNOWN = 0,
994 enum auto_boolean detect;
995 enum packet_support support;
998 /* Analyze a packet's return value and update the packet config
1009 update_packet_config (struct packet_config *config)
1011 switch (config->detect)
1013 case AUTO_BOOLEAN_TRUE:
1014 config->support = PACKET_ENABLE;
1016 case AUTO_BOOLEAN_FALSE:
1017 config->support = PACKET_DISABLE;
1019 case AUTO_BOOLEAN_AUTO:
1020 config->support = PACKET_SUPPORT_UNKNOWN;
1026 show_packet_config_cmd (struct packet_config *config)
1028 char *support = "internal-error";
1030 switch (config->support)
1033 support = "enabled";
1035 case PACKET_DISABLE:
1036 support = "disabled";
1038 case PACKET_SUPPORT_UNKNOWN:
1039 support = "unknown";
1042 switch (config->detect)
1044 case AUTO_BOOLEAN_AUTO:
1045 printf_filtered (_("Support for the `%s' packet "
1046 "is auto-detected, currently %s.\n"),
1047 config->name, support);
1049 case AUTO_BOOLEAN_TRUE:
1050 case AUTO_BOOLEAN_FALSE:
1051 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1052 config->name, support);
1058 add_packet_config_cmd (struct packet_config *config, const char *name,
1059 const char *title, int legacy)
1065 config->name = name;
1066 config->title = title;
1067 config->detect = AUTO_BOOLEAN_AUTO;
1068 config->support = PACKET_SUPPORT_UNKNOWN;
1069 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1071 show_doc = xstrprintf ("Show current use of remote "
1072 "protocol `%s' (%s) packet",
1074 /* set/show TITLE-packet {auto,on,off} */
1075 cmd_name = xstrprintf ("%s-packet", title);
1076 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1077 &config->detect, set_doc,
1078 show_doc, NULL, /* help_doc */
1079 set_remote_protocol_packet_cmd,
1080 show_remote_protocol_packet_cmd,
1081 &remote_set_cmdlist, &remote_show_cmdlist);
1082 /* The command code copies the documentation strings. */
1085 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1090 legacy_name = xstrprintf ("%s-packet", name);
1091 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1092 &remote_set_cmdlist);
1093 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1094 &remote_show_cmdlist);
1098 static enum packet_result
1099 packet_check_result (const char *buf)
1103 /* The stub recognized the packet request. Check that the
1104 operation succeeded. */
1106 && isxdigit (buf[1]) && isxdigit (buf[2])
1108 /* "Enn" - definitly an error. */
1109 return PACKET_ERROR;
1111 /* Always treat "E." as an error. This will be used for
1112 more verbose error messages, such as E.memtypes. */
1113 if (buf[0] == 'E' && buf[1] == '.')
1114 return PACKET_ERROR;
1116 /* The packet may or may not be OK. Just assume it is. */
1120 /* The stub does not support the packet. */
1121 return PACKET_UNKNOWN;
1124 static enum packet_result
1125 packet_ok (const char *buf, struct packet_config *config)
1127 enum packet_result result;
1129 result = packet_check_result (buf);
1134 /* The stub recognized the packet request. */
1135 switch (config->support)
1137 case PACKET_SUPPORT_UNKNOWN:
1139 fprintf_unfiltered (gdb_stdlog,
1140 "Packet %s (%s) is supported\n",
1141 config->name, config->title);
1142 config->support = PACKET_ENABLE;
1144 case PACKET_DISABLE:
1145 internal_error (__FILE__, __LINE__,
1146 _("packet_ok: attempt to use a disabled packet"));
1152 case PACKET_UNKNOWN:
1153 /* The stub does not support the packet. */
1154 switch (config->support)
1157 if (config->detect == AUTO_BOOLEAN_AUTO)
1158 /* If the stub previously indicated that the packet was
1159 supported then there is a protocol error.. */
1160 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1161 config->name, config->title);
1163 /* The user set it wrong. */
1164 error (_("Enabled packet %s (%s) not recognized by stub"),
1165 config->name, config->title);
1167 case PACKET_SUPPORT_UNKNOWN:
1169 fprintf_unfiltered (gdb_stdlog,
1170 "Packet %s (%s) is NOT supported\n",
1171 config->name, config->title);
1172 config->support = PACKET_DISABLE;
1174 case PACKET_DISABLE:
1196 PACKET_vFile_pwrite,
1198 PACKET_vFile_unlink,
1200 PACKET_qXfer_features,
1201 PACKET_qXfer_libraries,
1202 PACKET_qXfer_memory_map,
1203 PACKET_qXfer_spu_read,
1204 PACKET_qXfer_spu_write,
1205 PACKET_qXfer_osdata,
1206 PACKET_qXfer_threads,
1207 PACKET_qXfer_statictrace_read,
1208 PACKET_qXfer_traceframe_info,
1212 PACKET_QPassSignals,
1213 PACKET_qSearch_memory,
1216 PACKET_QStartNoAckMode,
1218 PACKET_qXfer_siginfo_read,
1219 PACKET_qXfer_siginfo_write,
1221 PACKET_ConditionalTracepoints,
1222 PACKET_FastTracepoints,
1223 PACKET_StaticTracepoints,
1226 PACKET_TracepointSource,
1231 static struct packet_config remote_protocol_packets[PACKET_MAX];
1234 set_remote_protocol_packet_cmd (char *args, int from_tty,
1235 struct cmd_list_element *c)
1237 struct packet_config *packet;
1239 for (packet = remote_protocol_packets;
1240 packet < &remote_protocol_packets[PACKET_MAX];
1243 if (&packet->detect == c->var)
1245 update_packet_config (packet);
1249 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1254 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1255 struct cmd_list_element *c,
1258 struct packet_config *packet;
1260 for (packet = remote_protocol_packets;
1261 packet < &remote_protocol_packets[PACKET_MAX];
1264 if (&packet->detect == c->var)
1266 show_packet_config_cmd (packet);
1270 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1274 /* Should we try one of the 'Z' requests? */
1278 Z_PACKET_SOFTWARE_BP,
1279 Z_PACKET_HARDWARE_BP,
1286 /* For compatibility with older distributions. Provide a ``set remote
1287 Z-packet ...'' command that updates all the Z packet types. */
1289 static enum auto_boolean remote_Z_packet_detect;
1292 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1293 struct cmd_list_element *c)
1297 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1299 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1300 update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
1305 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1306 struct cmd_list_element *c,
1311 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1313 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1317 /* Should we try the 'ThreadInfo' query packet?
1319 This variable (NOT available to the user: auto-detect only!)
1320 determines whether GDB will use the new, simpler "ThreadInfo"
1321 query or the older, more complex syntax for thread queries.
1322 This is an auto-detect variable (set to true at each connect,
1323 and set to false when the target fails to recognize it). */
1325 static int use_threadinfo_query;
1326 static int use_threadextra_query;
1328 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1329 static struct async_signal_handler *sigint_remote_twice_token;
1330 static struct async_signal_handler *sigint_remote_token;
1333 /* Asynchronous signal handle registered as event loop source for
1334 when we have pending events ready to be passed to the core. */
1336 static struct async_event_handler *remote_async_inferior_event_token;
1338 /* Asynchronous signal handle registered as event loop source for when
1339 the remote sent us a %Stop notification. The registered callback
1340 will do a vStopped sequence to pull the rest of the events out of
1341 the remote side into our event queue. */
1343 static struct async_event_handler *remote_async_get_pending_events_token;
1346 static ptid_t magic_null_ptid;
1347 static ptid_t not_sent_ptid;
1348 static ptid_t any_thread_ptid;
1350 /* These are the threads which we last sent to the remote system. The
1351 TID member will be -1 for all or -2 for not sent yet. */
1353 static ptid_t general_thread;
1354 static ptid_t continue_thread;
1356 /* Find out if the stub attached to PID (and hence GDB should offer to
1357 detach instead of killing it when bailing out). */
1360 remote_query_attached (int pid)
1362 struct remote_state *rs = get_remote_state ();
1364 if (remote_protocol_packets[PACKET_qAttached].support == PACKET_DISABLE)
1367 if (remote_multi_process_p (rs))
1368 sprintf (rs->buf, "qAttached:%x", pid);
1370 sprintf (rs->buf, "qAttached");
1373 getpkt (&rs->buf, &rs->buf_size, 0);
1375 switch (packet_ok (rs->buf,
1376 &remote_protocol_packets[PACKET_qAttached]))
1379 if (strcmp (rs->buf, "1") == 0)
1383 warning (_("Remote failure reply: %s"), rs->buf);
1385 case PACKET_UNKNOWN:
1392 /* Add PID to GDB's inferior table. Since we can be connected to a
1393 remote system before before knowing about any inferior, mark the
1394 target with execution when we find the first inferior. If ATTACHED
1395 is 1, then we had just attached to this inferior. If it is 0, then
1396 we just created this inferior. If it is -1, then try querying the
1397 remote stub to find out if it had attached to the inferior or
1400 static struct inferior *
1401 remote_add_inferior (int pid, int attached)
1403 struct inferior *inf;
1405 /* Check whether this process we're learning about is to be
1406 considered attached, or if is to be considered to have been
1407 spawned by the stub. */
1409 attached = remote_query_attached (pid);
1411 if (gdbarch_has_global_solist (target_gdbarch))
1413 /* If the target shares code across all inferiors, then every
1414 attach adds a new inferior. */
1415 inf = add_inferior (pid);
1417 /* ... and every inferior is bound to the same program space.
1418 However, each inferior may still have its own address
1420 inf->aspace = maybe_new_address_space ();
1421 inf->pspace = current_program_space;
1425 /* In the traditional debugging scenario, there's a 1-1 match
1426 between program/address spaces. We simply bind the inferior
1427 to the program space's address space. */
1428 inf = current_inferior ();
1429 inferior_appeared (inf, pid);
1432 inf->attach_flag = attached;
1437 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1438 according to RUNNING. */
1441 remote_add_thread (ptid_t ptid, int running)
1445 set_executing (ptid, running);
1446 set_running (ptid, running);
1449 /* Come here when we learn about a thread id from the remote target.
1450 It may be the first time we hear about such thread, so take the
1451 opportunity to add it to GDB's thread list. In case this is the
1452 first time we're noticing its corresponding inferior, add it to
1453 GDB's inferior list as well. */
1456 remote_notice_new_inferior (ptid_t currthread, int running)
1458 /* If this is a new thread, add it to GDB's thread list.
1459 If we leave it up to WFI to do this, bad things will happen. */
1461 if (in_thread_list (currthread) && is_exited (currthread))
1463 /* We're seeing an event on a thread id we knew had exited.
1464 This has to be a new thread reusing the old id. Add it. */
1465 remote_add_thread (currthread, running);
1469 if (!in_thread_list (currthread))
1471 struct inferior *inf = NULL;
1472 int pid = ptid_get_pid (currthread);
1474 if (ptid_is_pid (inferior_ptid)
1475 && pid == ptid_get_pid (inferior_ptid))
1477 /* inferior_ptid has no thread member yet. This can happen
1478 with the vAttach -> remote_wait,"TAAthread:" path if the
1479 stub doesn't support qC. This is the first stop reported
1480 after an attach, so this is the main thread. Update the
1481 ptid in the thread list. */
1482 if (in_thread_list (pid_to_ptid (pid)))
1483 thread_change_ptid (inferior_ptid, currthread);
1486 remote_add_thread (currthread, running);
1487 inferior_ptid = currthread;
1492 if (ptid_equal (magic_null_ptid, inferior_ptid))
1494 /* inferior_ptid is not set yet. This can happen with the
1495 vRun -> remote_wait,"TAAthread:" path if the stub
1496 doesn't support qC. This is the first stop reported
1497 after an attach, so this is the main thread. Update the
1498 ptid in the thread list. */
1499 thread_change_ptid (inferior_ptid, currthread);
1503 /* When connecting to a target remote, or to a target
1504 extended-remote which already was debugging an inferior, we
1505 may not know about it yet. Add it before adding its child
1506 thread, so notifications are emitted in a sensible order. */
1507 if (!in_inferior_list (ptid_get_pid (currthread)))
1508 inf = remote_add_inferior (ptid_get_pid (currthread), -1);
1510 /* This is really a new thread. Add it. */
1511 remote_add_thread (currthread, running);
1513 /* If we found a new inferior, let the common code do whatever
1514 it needs to with it (e.g., read shared libraries, insert
1517 notice_new_inferior (currthread, running, 0);
1521 /* Return the private thread data, creating it if necessary. */
1523 struct private_thread_info *
1524 demand_private_info (ptid_t ptid)
1526 struct thread_info *info = find_thread_ptid (ptid);
1532 info->private = xmalloc (sizeof (*(info->private)));
1533 info->private_dtor = free_private_thread_info;
1534 info->private->core = -1;
1535 info->private->extra = 0;
1538 return info->private;
1541 /* Call this function as a result of
1542 1) A halt indication (T packet) containing a thread id
1543 2) A direct query of currthread
1544 3) Successful execution of set thread */
1547 record_currthread (ptid_t currthread)
1549 general_thread = currthread;
1552 static char *last_pass_packet;
1554 /* If 'QPassSignals' is supported, tell the remote stub what signals
1555 it can simply pass through to the inferior without reporting. */
1558 remote_pass_signals (void)
1560 if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE)
1562 char *pass_packet, *p;
1563 int numsigs = (int) TARGET_SIGNAL_LAST;
1566 gdb_assert (numsigs < 256);
1567 for (i = 0; i < numsigs; i++)
1569 if (signal_stop_state (i) == 0
1570 && signal_print_state (i) == 0
1571 && signal_pass_state (i) == 1)
1574 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1575 strcpy (pass_packet, "QPassSignals:");
1576 p = pass_packet + strlen (pass_packet);
1577 for (i = 0; i < numsigs; i++)
1579 if (signal_stop_state (i) == 0
1580 && signal_print_state (i) == 0
1581 && signal_pass_state (i) == 1)
1584 *p++ = tohex (i >> 4);
1585 *p++ = tohex (i & 15);
1594 if (!last_pass_packet || strcmp (last_pass_packet, pass_packet))
1596 struct remote_state *rs = get_remote_state ();
1597 char *buf = rs->buf;
1599 putpkt (pass_packet);
1600 getpkt (&rs->buf, &rs->buf_size, 0);
1601 packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]);
1602 if (last_pass_packet)
1603 xfree (last_pass_packet);
1604 last_pass_packet = pass_packet;
1607 xfree (pass_packet);
1612 remote_notice_signals (ptid_t ptid)
1614 /* Update the remote on signals to silently pass, if they've
1616 remote_pass_signals ();
1619 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1620 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1621 thread. If GEN is set, set the general thread, if not, then set
1622 the step/continue thread. */
1624 set_thread (struct ptid ptid, int gen)
1626 struct remote_state *rs = get_remote_state ();
1627 ptid_t state = gen ? general_thread : continue_thread;
1628 char *buf = rs->buf;
1629 char *endbuf = rs->buf + get_remote_packet_size ();
1631 if (ptid_equal (state, ptid))
1635 *buf++ = gen ? 'g' : 'c';
1636 if (ptid_equal (ptid, magic_null_ptid))
1637 xsnprintf (buf, endbuf - buf, "0");
1638 else if (ptid_equal (ptid, any_thread_ptid))
1639 xsnprintf (buf, endbuf - buf, "0");
1640 else if (ptid_equal (ptid, minus_one_ptid))
1641 xsnprintf (buf, endbuf - buf, "-1");
1643 write_ptid (buf, endbuf, ptid);
1645 getpkt (&rs->buf, &rs->buf_size, 0);
1647 general_thread = ptid;
1649 continue_thread = ptid;
1653 set_general_thread (struct ptid ptid)
1655 set_thread (ptid, 1);
1659 set_continue_thread (struct ptid ptid)
1661 set_thread (ptid, 0);
1664 /* Change the remote current process. Which thread within the process
1665 ends up selected isn't important, as long as it is the same process
1666 as what INFERIOR_PTID points to.
1668 This comes from that fact that there is no explicit notion of
1669 "selected process" in the protocol. The selected process for
1670 general operations is the process the selected general thread
1674 set_general_process (void)
1676 struct remote_state *rs = get_remote_state ();
1678 /* If the remote can't handle multiple processes, don't bother. */
1679 if (!remote_multi_process_p (rs))
1682 /* We only need to change the remote current thread if it's pointing
1683 at some other process. */
1684 if (ptid_get_pid (general_thread) != ptid_get_pid (inferior_ptid))
1685 set_general_thread (inferior_ptid);
1689 /* Return nonzero if the thread PTID is still alive on the remote
1693 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1695 struct remote_state *rs = get_remote_state ();
1698 if (ptid_equal (ptid, magic_null_ptid))
1699 /* The main thread is always alive. */
1702 if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0)
1703 /* The main thread is always alive. This can happen after a
1704 vAttach, if the remote side doesn't support
1709 endp = rs->buf + get_remote_packet_size ();
1712 write_ptid (p, endp, ptid);
1715 getpkt (&rs->buf, &rs->buf_size, 0);
1716 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
1719 /* About these extended threadlist and threadinfo packets. They are
1720 variable length packets but, the fields within them are often fixed
1721 length. They are redundent enough to send over UDP as is the
1722 remote protocol in general. There is a matching unit test module
1725 #define OPAQUETHREADBYTES 8
1727 /* a 64 bit opaque identifier */
1728 typedef unsigned char threadref[OPAQUETHREADBYTES];
1730 /* WARNING: This threadref data structure comes from the remote O.S.,
1731 libstub protocol encoding, and remote.c. It is not particularly
1734 /* Right now, the internal structure is int. We want it to be bigger.
1735 Plan to fix this. */
1737 typedef int gdb_threadref; /* Internal GDB thread reference. */
1739 /* gdb_ext_thread_info is an internal GDB data structure which is
1740 equivalent to the reply of the remote threadinfo packet. */
1742 struct gdb_ext_thread_info
1744 threadref threadid; /* External form of thread reference. */
1745 int active; /* Has state interesting to GDB?
1747 char display[256]; /* Brief state display, name,
1748 blocked/suspended. */
1749 char shortname[32]; /* To be used to name threads. */
1750 char more_display[256]; /* Long info, statistics, queue depth,
1754 /* The volume of remote transfers can be limited by submitting
1755 a mask containing bits specifying the desired information.
1756 Use a union of these values as the 'selection' parameter to
1757 get_thread_info. FIXME: Make these TAG names more thread specific. */
1759 #define TAG_THREADID 1
1760 #define TAG_EXISTS 2
1761 #define TAG_DISPLAY 4
1762 #define TAG_THREADNAME 8
1763 #define TAG_MOREDISPLAY 16
1765 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1767 char *unpack_varlen_hex (char *buff, ULONGEST *result);
1769 static char *unpack_nibble (char *buf, int *val);
1771 static char *pack_nibble (char *buf, int nibble);
1773 static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
1775 static char *unpack_byte (char *buf, int *value);
1777 static char *pack_int (char *buf, int value);
1779 static char *unpack_int (char *buf, int *value);
1781 static char *unpack_string (char *src, char *dest, int length);
1783 static char *pack_threadid (char *pkt, threadref *id);
1785 static char *unpack_threadid (char *inbuf, threadref *id);
1787 void int_to_threadref (threadref *id, int value);
1789 static int threadref_to_int (threadref *ref);
1791 static void copy_threadref (threadref *dest, threadref *src);
1793 static int threadmatch (threadref *dest, threadref *src);
1795 static char *pack_threadinfo_request (char *pkt, int mode,
1798 static int remote_unpack_thread_info_response (char *pkt,
1799 threadref *expectedref,
1800 struct gdb_ext_thread_info
1804 static int remote_get_threadinfo (threadref *threadid,
1805 int fieldset, /*TAG mask */
1806 struct gdb_ext_thread_info *info);
1808 static char *pack_threadlist_request (char *pkt, int startflag,
1810 threadref *nextthread);
1812 static int parse_threadlist_response (char *pkt,
1814 threadref *original_echo,
1815 threadref *resultlist,
1818 static int remote_get_threadlist (int startflag,
1819 threadref *nextthread,
1823 threadref *threadlist);
1825 typedef int (*rmt_thread_action) (threadref *ref, void *context);
1827 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1828 void *context, int looplimit);
1830 static int remote_newthread_step (threadref *ref, void *context);
1833 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1834 buffer we're allowed to write to. Returns
1835 BUF+CHARACTERS_WRITTEN. */
1838 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
1841 struct remote_state *rs = get_remote_state ();
1843 if (remote_multi_process_p (rs))
1845 pid = ptid_get_pid (ptid);
1847 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
1849 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
1851 tid = ptid_get_tid (ptid);
1853 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
1855 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
1860 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1861 passed the last parsed char. Returns null_ptid on error. */
1864 read_ptid (char *buf, char **obuf)
1868 ULONGEST pid = 0, tid = 0;
1872 /* Multi-process ptid. */
1873 pp = unpack_varlen_hex (p + 1, &pid);
1875 error (_("invalid remote ptid: %s\n"), p);
1878 pp = unpack_varlen_hex (p + 1, &tid);
1881 return ptid_build (pid, 0, tid);
1884 /* No multi-process. Just a tid. */
1885 pp = unpack_varlen_hex (p, &tid);
1887 /* Since the stub is not sending a process id, then default to
1888 what's in inferior_ptid, unless it's null at this point. If so,
1889 then since there's no way to know the pid of the reported
1890 threads, use the magic number. */
1891 if (ptid_equal (inferior_ptid, null_ptid))
1892 pid = ptid_get_pid (magic_null_ptid);
1894 pid = ptid_get_pid (inferior_ptid);
1898 return ptid_build (pid, 0, tid);
1901 /* Encode 64 bits in 16 chars of hex. */
1903 static const char hexchars[] = "0123456789abcdef";
1906 ishex (int ch, int *val)
1908 if ((ch >= 'a') && (ch <= 'f'))
1910 *val = ch - 'a' + 10;
1913 if ((ch >= 'A') && (ch <= 'F'))
1915 *val = ch - 'A' + 10;
1918 if ((ch >= '0') && (ch <= '9'))
1929 if (ch >= 'a' && ch <= 'f')
1930 return ch - 'a' + 10;
1931 if (ch >= '0' && ch <= '9')
1933 if (ch >= 'A' && ch <= 'F')
1934 return ch - 'A' + 10;
1939 stub_unpack_int (char *buff, int fieldlength)
1946 nibble = stubhex (*buff++);
1950 retval = retval << 4;
1956 unpack_varlen_hex (char *buff, /* packet to parse */
1960 ULONGEST retval = 0;
1962 while (ishex (*buff, &nibble))
1965 retval = retval << 4;
1966 retval |= nibble & 0x0f;
1973 unpack_nibble (char *buf, int *val)
1975 *val = fromhex (*buf++);
1980 pack_nibble (char *buf, int nibble)
1982 *buf++ = hexchars[(nibble & 0x0f)];
1987 pack_hex_byte (char *pkt, int byte)
1989 *pkt++ = hexchars[(byte >> 4) & 0xf];
1990 *pkt++ = hexchars[(byte & 0xf)];
1995 unpack_byte (char *buf, int *value)
1997 *value = stub_unpack_int (buf, 2);
2002 pack_int (char *buf, int value)
2004 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2005 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2006 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2007 buf = pack_hex_byte (buf, (value & 0xff));
2012 unpack_int (char *buf, int *value)
2014 *value = stub_unpack_int (buf, 8);
2018 #if 0 /* Currently unused, uncomment when needed. */
2019 static char *pack_string (char *pkt, char *string);
2022 pack_string (char *pkt, char *string)
2027 len = strlen (string);
2029 len = 200; /* Bigger than most GDB packets, junk??? */
2030 pkt = pack_hex_byte (pkt, len);
2034 if ((ch == '\0') || (ch == '#'))
2035 ch = '*'; /* Protect encapsulation. */
2040 #endif /* 0 (unused) */
2043 unpack_string (char *src, char *dest, int length)
2052 pack_threadid (char *pkt, threadref *id)
2055 unsigned char *altid;
2057 altid = (unsigned char *) id;
2058 limit = pkt + BUF_THREAD_ID_SIZE;
2060 pkt = pack_hex_byte (pkt, *altid++);
2066 unpack_threadid (char *inbuf, threadref *id)
2069 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2072 altref = (char *) id;
2074 while (inbuf < limit)
2076 x = stubhex (*inbuf++);
2077 y = stubhex (*inbuf++);
2078 *altref++ = (x << 4) | y;
2083 /* Externally, threadrefs are 64 bits but internally, they are still
2084 ints. This is due to a mismatch of specifications. We would like
2085 to use 64bit thread references internally. This is an adapter
2089 int_to_threadref (threadref *id, int value)
2091 unsigned char *scan;
2093 scan = (unsigned char *) id;
2099 *scan++ = (value >> 24) & 0xff;
2100 *scan++ = (value >> 16) & 0xff;
2101 *scan++ = (value >> 8) & 0xff;
2102 *scan++ = (value & 0xff);
2106 threadref_to_int (threadref *ref)
2109 unsigned char *scan;
2115 value = (value << 8) | ((*scan++) & 0xff);
2120 copy_threadref (threadref *dest, threadref *src)
2123 unsigned char *csrc, *cdest;
2125 csrc = (unsigned char *) src;
2126 cdest = (unsigned char *) dest;
2133 threadmatch (threadref *dest, threadref *src)
2135 /* Things are broken right now, so just assume we got a match. */
2137 unsigned char *srcp, *destp;
2139 srcp = (char *) src;
2140 destp = (char *) dest;
2144 result &= (*srcp++ == *destp++) ? 1 : 0;
2151 threadid:1, # always request threadid
2158 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2161 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2163 *pkt++ = 'q'; /* Info Query */
2164 *pkt++ = 'P'; /* process or thread info */
2165 pkt = pack_int (pkt, mode); /* mode */
2166 pkt = pack_threadid (pkt, id); /* threadid */
2167 *pkt = '\0'; /* terminate */
2171 /* These values tag the fields in a thread info response packet. */
2172 /* Tagging the fields allows us to request specific fields and to
2173 add more fields as time goes by. */
2175 #define TAG_THREADID 1 /* Echo the thread identifier. */
2176 #define TAG_EXISTS 2 /* Is this process defined enough to
2177 fetch registers and its stack? */
2178 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2179 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2180 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2184 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2185 struct gdb_ext_thread_info *info)
2187 struct remote_state *rs = get_remote_state ();
2191 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2194 /* info->threadid = 0; FIXME: implement zero_threadref. */
2196 info->display[0] = '\0';
2197 info->shortname[0] = '\0';
2198 info->more_display[0] = '\0';
2200 /* Assume the characters indicating the packet type have been
2202 pkt = unpack_int (pkt, &mask); /* arg mask */
2203 pkt = unpack_threadid (pkt, &ref);
2206 warning (_("Incomplete response to threadinfo request."));
2207 if (!threadmatch (&ref, expectedref))
2208 { /* This is an answer to a different request. */
2209 warning (_("ERROR RMT Thread info mismatch."));
2212 copy_threadref (&info->threadid, &ref);
2214 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2216 /* Packets are terminated with nulls. */
2217 while ((pkt < limit) && mask && *pkt)
2219 pkt = unpack_int (pkt, &tag); /* tag */
2220 pkt = unpack_byte (pkt, &length); /* length */
2221 if (!(tag & mask)) /* Tags out of synch with mask. */
2223 warning (_("ERROR RMT: threadinfo tag mismatch."));
2227 if (tag == TAG_THREADID)
2231 warning (_("ERROR RMT: length of threadid is not 16."));
2235 pkt = unpack_threadid (pkt, &ref);
2236 mask = mask & ~TAG_THREADID;
2239 if (tag == TAG_EXISTS)
2241 info->active = stub_unpack_int (pkt, length);
2243 mask = mask & ~(TAG_EXISTS);
2246 warning (_("ERROR RMT: 'exists' length too long."));
2252 if (tag == TAG_THREADNAME)
2254 pkt = unpack_string (pkt, &info->shortname[0], length);
2255 mask = mask & ~TAG_THREADNAME;
2258 if (tag == TAG_DISPLAY)
2260 pkt = unpack_string (pkt, &info->display[0], length);
2261 mask = mask & ~TAG_DISPLAY;
2264 if (tag == TAG_MOREDISPLAY)
2266 pkt = unpack_string (pkt, &info->more_display[0], length);
2267 mask = mask & ~TAG_MOREDISPLAY;
2270 warning (_("ERROR RMT: unknown thread info tag."));
2271 break; /* Not a tag we know about. */
2277 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2278 struct gdb_ext_thread_info *info)
2280 struct remote_state *rs = get_remote_state ();
2283 pack_threadinfo_request (rs->buf, fieldset, threadid);
2285 getpkt (&rs->buf, &rs->buf_size, 0);
2287 if (rs->buf[0] == '\0')
2290 result = remote_unpack_thread_info_response (rs->buf + 2,
2295 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2298 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2299 threadref *nextthread)
2301 *pkt++ = 'q'; /* info query packet */
2302 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2303 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2304 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2305 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2310 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2313 parse_threadlist_response (char *pkt, int result_limit,
2314 threadref *original_echo, threadref *resultlist,
2317 struct remote_state *rs = get_remote_state ();
2319 int count, resultcount, done;
2322 /* Assume the 'q' and 'M chars have been stripped. */
2323 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2324 /* done parse past here */
2325 pkt = unpack_byte (pkt, &count); /* count field */
2326 pkt = unpack_nibble (pkt, &done);
2327 /* The first threadid is the argument threadid. */
2328 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2329 while ((count-- > 0) && (pkt < limit))
2331 pkt = unpack_threadid (pkt, resultlist++);
2332 if (resultcount++ >= result_limit)
2341 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2342 int *done, int *result_count, threadref *threadlist)
2344 struct remote_state *rs = get_remote_state ();
2345 static threadref echo_nextthread;
2348 /* Trancate result limit to be smaller than the packet size. */
2349 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2350 >= get_remote_packet_size ())
2351 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2353 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2355 getpkt (&rs->buf, &rs->buf_size, 0);
2357 if (*rs->buf == '\0')
2361 parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
2364 if (!threadmatch (&echo_nextthread, nextthread))
2366 /* FIXME: This is a good reason to drop the packet. */
2367 /* Possably, there is a duplicate response. */
2369 retransmit immediatly - race conditions
2370 retransmit after timeout - yes
2372 wait for packet, then exit
2374 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2375 return 0; /* I choose simply exiting. */
2377 if (*result_count <= 0)
2381 warning (_("RMT ERROR : failed to get remote thread list."));
2384 return result; /* break; */
2386 if (*result_count > result_limit)
2389 warning (_("RMT ERROR: threadlist response longer than requested."));
2395 /* This is the interface between remote and threads, remotes upper
2398 /* remote_find_new_threads retrieves the thread list and for each
2399 thread in the list, looks up the thread in GDB's internal list,
2400 adding the thread if it does not already exist. This involves
2401 getting partial thread lists from the remote target so, polling the
2402 quit_flag is required. */
2405 /* About this many threadisds fit in a packet. */
2407 #define MAXTHREADLISTRESULTS 32
2410 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2413 int done, i, result_count;
2417 static threadref nextthread;
2418 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
2423 if (loopcount++ > looplimit)
2426 warning (_("Remote fetch threadlist -infinite loop-."));
2429 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
2430 &done, &result_count, resultthreadlist))
2435 /* Clear for later iterations. */
2437 /* Setup to resume next batch of thread references, set nextthread. */
2438 if (result_count >= 1)
2439 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
2441 while (result_count--)
2442 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
2449 remote_newthread_step (threadref *ref, void *context)
2451 int pid = ptid_get_pid (inferior_ptid);
2452 ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref));
2454 if (!in_thread_list (ptid))
2456 return 1; /* continue iterator */
2459 #define CRAZY_MAX_THREADS 1000
2462 remote_current_thread (ptid_t oldpid)
2464 struct remote_state *rs = get_remote_state ();
2467 getpkt (&rs->buf, &rs->buf_size, 0);
2468 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2469 return read_ptid (&rs->buf[2], NULL);
2474 /* Find new threads for info threads command.
2475 * Original version, using John Metzler's thread protocol.
2479 remote_find_new_threads (void)
2481 remote_threadlist_iterator (remote_newthread_step, 0,
2485 #if defined(HAVE_LIBEXPAT)
2487 typedef struct thread_item
2493 DEF_VEC_O(thread_item_t);
2495 struct threads_parsing_context
2497 VEC (thread_item_t) *items;
2501 start_thread (struct gdb_xml_parser *parser,
2502 const struct gdb_xml_element *element,
2503 void *user_data, VEC(gdb_xml_value_s) *attributes)
2505 struct threads_parsing_context *data = user_data;
2507 struct thread_item item;
2509 struct gdb_xml_value *attr;
2511 id = xml_find_attribute (attributes, "id")->value;
2512 item.ptid = read_ptid (id, NULL);
2514 attr = xml_find_attribute (attributes, "core");
2516 item.core = *(ULONGEST *) attr->value;
2522 VEC_safe_push (thread_item_t, data->items, &item);
2526 end_thread (struct gdb_xml_parser *parser,
2527 const struct gdb_xml_element *element,
2528 void *user_data, const char *body_text)
2530 struct threads_parsing_context *data = user_data;
2532 if (body_text && *body_text)
2533 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2536 const struct gdb_xml_attribute thread_attributes[] = {
2537 { "id", GDB_XML_AF_NONE, NULL, NULL },
2538 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2539 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2542 const struct gdb_xml_element thread_children[] = {
2543 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2546 const struct gdb_xml_element threads_children[] = {
2547 { "thread", thread_attributes, thread_children,
2548 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2549 start_thread, end_thread },
2550 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2553 const struct gdb_xml_element threads_elements[] = {
2554 { "threads", NULL, threads_children,
2555 GDB_XML_EF_NONE, NULL, NULL },
2556 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2559 /* Discard the contents of the constructed thread info context. */
2562 clear_threads_parsing_context (void *p)
2564 struct threads_parsing_context *context = p;
2566 struct thread_item *item;
2568 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2569 xfree (item->extra);
2571 VEC_free (thread_item_t, context->items);
2577 * Find all threads for info threads command.
2578 * Uses new thread protocol contributed by Cisco.
2579 * Falls back and attempts to use the older method (above)
2580 * if the target doesn't respond to the new method.
2584 remote_threads_info (struct target_ops *ops)
2586 struct remote_state *rs = get_remote_state ();
2590 if (remote_desc == 0) /* paranoia */
2591 error (_("Command can only be used when connected to the remote target."));
2593 #if defined(HAVE_LIBEXPAT)
2594 if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2596 char *xml = target_read_stralloc (¤t_target,
2597 TARGET_OBJECT_THREADS, NULL);
2599 struct cleanup *back_to = make_cleanup (xfree, xml);
2603 struct threads_parsing_context context;
2605 context.items = NULL;
2606 make_cleanup (clear_threads_parsing_context, &context);
2608 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2609 threads_elements, xml, &context) == 0)
2612 struct thread_item *item;
2615 VEC_iterate (thread_item_t, context.items, i, item);
2618 if (!ptid_equal (item->ptid, null_ptid))
2620 struct private_thread_info *info;
2621 /* In non-stop mode, we assume new found threads
2622 are running until proven otherwise with a
2623 stop reply. In all-stop, we can only get
2624 here if all threads are stopped. */
2625 int running = non_stop ? 1 : 0;
2627 remote_notice_new_inferior (item->ptid, running);
2629 info = demand_private_info (item->ptid);
2630 info->core = item->core;
2631 info->extra = item->extra;
2638 do_cleanups (back_to);
2643 if (use_threadinfo_query)
2645 putpkt ("qfThreadInfo");
2646 getpkt (&rs->buf, &rs->buf_size, 0);
2648 if (bufp[0] != '\0') /* q packet recognized */
2650 while (*bufp++ == 'm') /* reply contains one or more TID */
2654 new_thread = read_ptid (bufp, &bufp);
2655 if (!ptid_equal (new_thread, null_ptid))
2657 /* In non-stop mode, we assume new found threads
2658 are running until proven otherwise with a
2659 stop reply. In all-stop, we can only get
2660 here if all threads are stopped. */
2661 int running = non_stop ? 1 : 0;
2663 remote_notice_new_inferior (new_thread, running);
2666 while (*bufp++ == ','); /* comma-separated list */
2667 putpkt ("qsThreadInfo");
2668 getpkt (&rs->buf, &rs->buf_size, 0);
2675 /* Only qfThreadInfo is supported in non-stop mode. */
2679 /* Else fall back to old method based on jmetzler protocol. */
2680 use_threadinfo_query = 0;
2681 remote_find_new_threads ();
2686 * Collect a descriptive string about the given thread.
2687 * The target may say anything it wants to about the thread
2688 * (typically info about its blocked / runnable state, name, etc.).
2689 * This string will appear in the info threads display.
2691 * Optional: targets are not required to implement this function.
2695 remote_threads_extra_info (struct thread_info *tp)
2697 struct remote_state *rs = get_remote_state ();
2701 struct gdb_ext_thread_info threadinfo;
2702 static char display_buf[100]; /* arbitrary... */
2703 int n = 0; /* position in display_buf */
2705 if (remote_desc == 0) /* paranoia */
2706 internal_error (__FILE__, __LINE__,
2707 _("remote_threads_extra_info"));
2709 if (ptid_equal (tp->ptid, magic_null_ptid)
2710 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0))
2711 /* This is the main thread which was added by GDB. The remote
2712 server doesn't know about it. */
2715 if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2717 struct thread_info *info = find_thread_ptid (tp->ptid);
2719 if (info && info->private)
2720 return info->private->extra;
2725 if (use_threadextra_query)
2728 char *endb = rs->buf + get_remote_packet_size ();
2730 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2732 write_ptid (b, endb, tp->ptid);
2735 getpkt (&rs->buf, &rs->buf_size, 0);
2736 if (rs->buf[0] != 0)
2738 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2739 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
2740 display_buf [result] = '\0';
2745 /* If the above query fails, fall back to the old method. */
2746 use_threadextra_query = 0;
2747 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2748 | TAG_MOREDISPLAY | TAG_DISPLAY;
2749 int_to_threadref (&id, ptid_get_tid (tp->ptid));
2750 if (remote_get_threadinfo (&id, set, &threadinfo))
2751 if (threadinfo.active)
2753 if (*threadinfo.shortname)
2754 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
2755 " Name: %s,", threadinfo.shortname);
2756 if (*threadinfo.display)
2757 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2758 " State: %s,", threadinfo.display);
2759 if (*threadinfo.more_display)
2760 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2761 " Priority: %s", threadinfo.more_display);
2765 /* For purely cosmetic reasons, clear up trailing commas. */
2766 if (',' == display_buf[n-1])
2767 display_buf[n-1] = ' ';
2776 remote_static_tracepoint_marker_at (CORE_ADDR addr,
2777 struct static_tracepoint_marker *marker)
2779 struct remote_state *rs = get_remote_state ();
2782 sprintf (p, "qTSTMat:");
2784 p += hexnumstr (p, addr);
2786 getpkt (&rs->buf, &rs->buf_size, 0);
2790 error (_("Remote failure reply: %s"), p);
2794 parse_static_tracepoint_marker_definition (p, &p, marker);
2802 free_current_marker (void *arg)
2804 struct static_tracepoint_marker **marker_p = arg;
2806 if (*marker_p != NULL)
2808 release_static_tracepoint_marker (*marker_p);
2815 static VEC(static_tracepoint_marker_p) *
2816 remote_static_tracepoint_markers_by_strid (const char *strid)
2818 struct remote_state *rs = get_remote_state ();
2819 VEC(static_tracepoint_marker_p) *markers = NULL;
2820 struct static_tracepoint_marker *marker = NULL;
2821 struct cleanup *old_chain;
2824 /* Ask for a first packet of static tracepoint marker
2827 getpkt (&rs->buf, &rs->buf_size, 0);
2830 error (_("Remote failure reply: %s"), p);
2832 old_chain = make_cleanup (free_current_marker, &marker);
2837 marker = XCNEW (struct static_tracepoint_marker);
2841 parse_static_tracepoint_marker_definition (p, &p, marker);
2843 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
2845 VEC_safe_push (static_tracepoint_marker_p,
2851 release_static_tracepoint_marker (marker);
2852 memset (marker, 0, sizeof (*marker));
2855 while (*p++ == ','); /* comma-separated list */
2856 /* Ask for another packet of static tracepoint definition. */
2858 getpkt (&rs->buf, &rs->buf_size, 0);
2862 do_cleanups (old_chain);
2867 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2870 remote_get_ada_task_ptid (long lwp, long thread)
2872 return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp);
2876 /* Restart the remote side; this is an extended protocol operation. */
2879 extended_remote_restart (void)
2881 struct remote_state *rs = get_remote_state ();
2883 /* Send the restart command; for reasons I don't understand the
2884 remote side really expects a number after the "R". */
2885 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
2888 remote_fileio_reset ();
2891 /* Clean up connection to a remote debugger. */
2894 remote_close (int quitting)
2896 if (remote_desc == NULL)
2897 return; /* already closed */
2899 /* Make sure we leave stdin registered in the event loop, and we
2900 don't leave the async SIGINT signal handler installed. */
2901 remote_terminal_ours ();
2903 serial_close (remote_desc);
2906 /* We don't have a connection to the remote stub anymore. Get rid
2907 of all the inferiors and their threads we were controlling. */
2908 discard_all_inferiors ();
2909 inferior_ptid = null_ptid;
2911 /* We're no longer interested in any of these events. */
2912 discard_pending_stop_replies (-1);
2914 if (remote_async_inferior_event_token)
2915 delete_async_event_handler (&remote_async_inferior_event_token);
2916 if (remote_async_get_pending_events_token)
2917 delete_async_event_handler (&remote_async_get_pending_events_token);
2920 /* Query the remote side for the text, data and bss offsets. */
2925 struct remote_state *rs = get_remote_state ();
2928 int lose, num_segments = 0, do_sections, do_segments;
2929 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
2930 struct section_offsets *offs;
2931 struct symfile_segment_data *data;
2933 if (symfile_objfile == NULL)
2936 putpkt ("qOffsets");
2937 getpkt (&rs->buf, &rs->buf_size, 0);
2940 if (buf[0] == '\000')
2941 return; /* Return silently. Stub doesn't support
2945 warning (_("Remote failure reply: %s"), buf);
2949 /* Pick up each field in turn. This used to be done with scanf, but
2950 scanf will make trouble if CORE_ADDR size doesn't match
2951 conversion directives correctly. The following code will work
2952 with any size of CORE_ADDR. */
2953 text_addr = data_addr = bss_addr = 0;
2957 if (strncmp (ptr, "Text=", 5) == 0)
2960 /* Don't use strtol, could lose on big values. */
2961 while (*ptr && *ptr != ';')
2962 text_addr = (text_addr << 4) + fromhex (*ptr++);
2964 if (strncmp (ptr, ";Data=", 6) == 0)
2967 while (*ptr && *ptr != ';')
2968 data_addr = (data_addr << 4) + fromhex (*ptr++);
2973 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
2976 while (*ptr && *ptr != ';')
2977 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
2979 if (bss_addr != data_addr)
2980 warning (_("Target reported unsupported offsets: %s"), buf);
2985 else if (strncmp (ptr, "TextSeg=", 8) == 0)
2988 /* Don't use strtol, could lose on big values. */
2989 while (*ptr && *ptr != ';')
2990 text_addr = (text_addr << 4) + fromhex (*ptr++);
2993 if (strncmp (ptr, ";DataSeg=", 9) == 0)
2996 while (*ptr && *ptr != ';')
2997 data_addr = (data_addr << 4) + fromhex (*ptr++);
3005 error (_("Malformed response to offset query, %s"), buf);
3006 else if (*ptr != '\0')
3007 warning (_("Target reported unsupported offsets: %s"), buf);
3009 offs = ((struct section_offsets *)
3010 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3011 memcpy (offs, symfile_objfile->section_offsets,
3012 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3014 data = get_symfile_segment_data (symfile_objfile->obfd);
3015 do_segments = (data != NULL);
3016 do_sections = num_segments == 0;
3018 if (num_segments > 0)
3020 segments[0] = text_addr;
3021 segments[1] = data_addr;
3023 /* If we have two segments, we can still try to relocate everything
3024 by assuming that the .text and .data offsets apply to the whole
3025 text and data segments. Convert the offsets given in the packet
3026 to base addresses for symfile_map_offsets_to_segments. */
3027 else if (data && data->num_segments == 2)
3029 segments[0] = data->segment_bases[0] + text_addr;
3030 segments[1] = data->segment_bases[1] + data_addr;
3033 /* If the object file has only one segment, assume that it is text
3034 rather than data; main programs with no writable data are rare,
3035 but programs with no code are useless. Of course the code might
3036 have ended up in the data segment... to detect that we would need
3037 the permissions here. */
3038 else if (data && data->num_segments == 1)
3040 segments[0] = data->segment_bases[0] + text_addr;
3043 /* There's no way to relocate by segment. */
3049 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3050 offs, num_segments, segments);
3052 if (ret == 0 && !do_sections)
3053 error (_("Can not handle qOffsets TextSeg "
3054 "response with this symbol file"));
3061 free_symfile_segment_data (data);
3065 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3067 /* This is a temporary kludge to force data and bss to use the
3068 same offsets because that's what nlmconv does now. The real
3069 solution requires changes to the stub and remote.c that I
3070 don't have time to do right now. */
3072 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3073 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3076 objfile_relocate (symfile_objfile, offs);
3079 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3080 threads we know are stopped already. This is used during the
3081 initial remote connection in non-stop mode --- threads that are
3082 reported as already being stopped are left stopped. */
3085 set_stop_requested_callback (struct thread_info *thread, void *data)
3087 /* If we have a stop reply for this thread, it must be stopped. */
3088 if (peek_stop_reply (thread->ptid))
3089 set_stop_requested (thread->ptid, 1);
3094 /* Stub for catch_exception. */
3096 struct start_remote_args
3100 /* The current target. */
3101 struct target_ops *target;
3103 /* Non-zero if this is an extended-remote target. */
3107 /* Send interrupt_sequence to remote target. */
3109 send_interrupt_sequence ()
3111 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3112 serial_write (remote_desc, "\x03", 1);
3113 else if (interrupt_sequence_mode == interrupt_sequence_break)
3114 serial_send_break (remote_desc);
3115 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3117 serial_send_break (remote_desc);
3118 serial_write (remote_desc, "g", 1);
3121 internal_error (__FILE__, __LINE__,
3122 _("Invalid value for interrupt_sequence_mode: %s."),
3123 interrupt_sequence_mode);
3127 remote_start_remote (struct ui_out *uiout, void *opaque)
3129 struct start_remote_args *args = opaque;
3130 struct remote_state *rs = get_remote_state ();
3131 struct packet_config *noack_config;
3132 char *wait_status = NULL;
3134 immediate_quit++; /* Allow user to interrupt it. */
3136 /* Ack any packet which the remote side has already sent. */
3137 serial_write (remote_desc, "+", 1);
3139 if (interrupt_on_connect)
3140 send_interrupt_sequence ();
3142 /* The first packet we send to the target is the optional "supported
3143 packets" request. If the target can answer this, it will tell us
3144 which later probes to skip. */
3145 remote_query_supported ();
3147 /* If the stub wants to get a QAllow, compose one and send it. */
3148 if (remote_protocol_packets[PACKET_QAllow].support != PACKET_DISABLE)
3149 remote_set_permissions ();
3151 /* Next, we possibly activate noack mode.
3153 If the QStartNoAckMode packet configuration is set to AUTO,
3154 enable noack mode if the stub reported a wish for it with
3157 If set to TRUE, then enable noack mode even if the stub didn't
3158 report it in qSupported. If the stub doesn't reply OK, the
3159 session ends with an error.
3161 If FALSE, then don't activate noack mode, regardless of what the
3162 stub claimed should be the default with qSupported. */
3164 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3166 if (noack_config->detect == AUTO_BOOLEAN_TRUE
3167 || (noack_config->detect == AUTO_BOOLEAN_AUTO
3168 && noack_config->support == PACKET_ENABLE))
3170 putpkt ("QStartNoAckMode");
3171 getpkt (&rs->buf, &rs->buf_size, 0);
3172 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3176 if (args->extended_p)
3178 /* Tell the remote that we are using the extended protocol. */
3180 getpkt (&rs->buf, &rs->buf_size, 0);
3183 /* Next, if the target can specify a description, read it. We do
3184 this before anything involving memory or registers. */
3185 target_find_description ();
3187 /* Next, now that we know something about the target, update the
3188 address spaces in the program spaces. */
3189 update_address_spaces ();
3191 /* On OSs where the list of libraries is global to all
3192 processes, we fetch them early. */
3193 if (gdbarch_has_global_solist (target_gdbarch))
3194 solib_add (NULL, args->from_tty, args->target, auto_solib_add);
3198 if (!rs->non_stop_aware)
3199 error (_("Non-stop mode requested, but remote "
3200 "does not support non-stop"));
3202 putpkt ("QNonStop:1");
3203 getpkt (&rs->buf, &rs->buf_size, 0);
3205 if (strcmp (rs->buf, "OK") != 0)
3206 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3208 /* Find about threads and processes the stub is already
3209 controlling. We default to adding them in the running state.
3210 The '?' query below will then tell us about which threads are
3212 remote_threads_info (args->target);
3214 else if (rs->non_stop_aware)
3216 /* Don't assume that the stub can operate in all-stop mode.
3217 Request it explicitely. */
3218 putpkt ("QNonStop:0");
3219 getpkt (&rs->buf, &rs->buf_size, 0);
3221 if (strcmp (rs->buf, "OK") != 0)
3222 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3225 /* Check whether the target is running now. */
3227 getpkt (&rs->buf, &rs->buf_size, 0);
3231 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3233 if (!args->extended_p)
3234 error (_("The target is not running (try extended-remote?)"));
3236 /* We're connected, but not running. Drop out before we
3237 call start_remote. */
3242 /* Save the reply for later. */
3243 wait_status = alloca (strlen (rs->buf) + 1);
3244 strcpy (wait_status, rs->buf);
3247 /* Let the stub know that we want it to return the thread. */
3248 set_continue_thread (minus_one_ptid);
3250 /* Without this, some commands which require an active target
3251 (such as kill) won't work. This variable serves (at least)
3252 double duty as both the pid of the target process (if it has
3253 such), and as a flag indicating that a target is active.
3254 These functions should be split out into seperate variables,
3255 especially since GDB will someday have a notion of debugging
3256 several processes. */
3257 inferior_ptid = magic_null_ptid;
3259 /* Now, if we have thread information, update inferior_ptid. */
3260 inferior_ptid = remote_current_thread (inferior_ptid);
3262 remote_add_inferior (ptid_get_pid (inferior_ptid), -1);
3264 /* Always add the main thread. */
3265 add_thread_silent (inferior_ptid);
3267 get_offsets (); /* Get text, data & bss offsets. */
3269 /* If we could not find a description using qXfer, and we know
3270 how to do it some other way, try again. This is not
3271 supported for non-stop; it could be, but it is tricky if
3272 there are no stopped threads when we connect. */
3273 if (remote_read_description_p (args->target)
3274 && gdbarch_target_desc (target_gdbarch) == NULL)
3276 target_clear_description ();
3277 target_find_description ();
3280 /* Use the previously fetched status. */
3281 gdb_assert (wait_status != NULL);
3282 strcpy (rs->buf, wait_status);
3283 rs->cached_wait_status = 1;
3286 start_remote (args->from_tty); /* Initialize gdb process mechanisms. */
3290 /* Clear WFI global state. Do this before finding about new
3291 threads and inferiors, and setting the current inferior.
3292 Otherwise we would clear the proceed status of the current
3293 inferior when we want its stop_soon state to be preserved
3294 (see notice_new_inferior). */
3295 init_wait_for_inferior ();
3297 /* In non-stop, we will either get an "OK", meaning that there
3298 are no stopped threads at this time; or, a regular stop
3299 reply. In the latter case, there may be more than one thread
3300 stopped --- we pull them all out using the vStopped
3302 if (strcmp (rs->buf, "OK") != 0)
3304 struct stop_reply *stop_reply;
3305 struct cleanup *old_chain;
3307 stop_reply = stop_reply_xmalloc ();
3308 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
3310 remote_parse_stop_reply (rs->buf, stop_reply);
3311 discard_cleanups (old_chain);
3313 /* get_pending_stop_replies acks this one, and gets the rest
3315 pending_stop_reply = stop_reply;
3316 remote_get_pending_stop_replies ();
3318 /* Make sure that threads that were stopped remain
3320 iterate_over_threads (set_stop_requested_callback, NULL);
3323 if (target_can_async_p ())
3324 target_async (inferior_event_handler, 0);
3326 if (thread_count () == 0)
3328 if (!args->extended_p)
3329 error (_("The target is not running (try extended-remote?)"));
3331 /* We're connected, but not running. Drop out before we
3332 call start_remote. */
3336 /* Let the stub know that we want it to return the thread. */
3338 /* Force the stub to choose a thread. */
3339 set_general_thread (null_ptid);
3342 inferior_ptid = remote_current_thread (minus_one_ptid);
3343 if (ptid_equal (inferior_ptid, minus_one_ptid))
3344 error (_("remote didn't report the current thread in non-stop mode"));
3346 get_offsets (); /* Get text, data & bss offsets. */
3348 /* In non-stop mode, any cached wait status will be stored in
3349 the stop reply queue. */
3350 gdb_assert (wait_status == NULL);
3352 /* Update the remote on signals to silently pass, or more
3353 importantly, which to not ignore, in case a previous session
3354 had set some different set of signals to be ignored. */
3355 remote_pass_signals ();
3358 /* If we connected to a live target, do some additional setup. */
3359 if (target_has_execution)
3361 if (exec_bfd) /* No use without an exec file. */
3362 remote_check_symbols (symfile_objfile);
3365 /* Possibly the target has been engaged in a trace run started
3366 previously; find out where things are at. */
3367 if (remote_get_trace_status (current_trace_status ()) != -1)
3369 struct uploaded_tp *uploaded_tps = NULL;
3370 struct uploaded_tsv *uploaded_tsvs = NULL;
3372 if (current_trace_status ()->running)
3373 printf_filtered (_("Trace is already running on the target.\n"));
3375 /* Get trace state variables first, they may be checked when
3376 parsing uploaded commands. */
3378 remote_upload_trace_state_variables (&uploaded_tsvs);
3380 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3382 remote_upload_tracepoints (&uploaded_tps);
3384 merge_uploaded_tracepoints (&uploaded_tps);
3387 /* If breakpoints are global, insert them now. */
3388 if (gdbarch_has_global_breakpoints (target_gdbarch)
3389 && breakpoints_always_inserted_mode ())
3390 insert_breakpoints ();
3393 /* Open a connection to a remote debugger.
3394 NAME is the filename used for communication. */
3397 remote_open (char *name, int from_tty)
3399 remote_open_1 (name, from_tty, &remote_ops, 0);
3402 /* Open a connection to a remote debugger using the extended
3403 remote gdb protocol. NAME is the filename used for communication. */
3406 extended_remote_open (char *name, int from_tty)
3408 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3411 /* Generic code for opening a connection to a remote target. */
3414 init_all_packet_configs (void)
3418 for (i = 0; i < PACKET_MAX; i++)
3419 update_packet_config (&remote_protocol_packets[i]);
3422 /* Symbol look-up. */
3425 remote_check_symbols (struct objfile *objfile)
3427 struct remote_state *rs = get_remote_state ();
3428 char *msg, *reply, *tmp;
3429 struct minimal_symbol *sym;
3432 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
3435 /* Make sure the remote is pointing at the right process. */
3436 set_general_process ();
3438 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3439 because we need both at the same time. */
3440 msg = alloca (get_remote_packet_size ());
3442 /* Invite target to request symbol lookups. */
3444 putpkt ("qSymbol::");
3445 getpkt (&rs->buf, &rs->buf_size, 0);
3446 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3449 while (strncmp (reply, "qSymbol:", 8) == 0)
3452 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3454 sym = lookup_minimal_symbol (msg, NULL, NULL);
3456 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3459 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
3460 CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym);
3462 /* If this is a function address, return the start of code
3463 instead of any data function descriptor. */
3464 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch,
3468 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3469 phex_nz (sym_addr, addr_size), &reply[8]);
3473 getpkt (&rs->buf, &rs->buf_size, 0);
3478 static struct serial *
3479 remote_serial_open (char *name)
3481 static int udp_warning = 0;
3483 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3484 of in ser-tcp.c, because it is the remote protocol assuming that the
3485 serial connection is reliable and not the serial connection promising
3487 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3489 warning (_("The remote protocol may be unreliable over UDP.\n"
3490 "Some events may be lost, rendering further debugging "
3495 return serial_open (name);
3498 /* Inform the target of our permission settings. The permission flags
3499 work without this, but if the target knows the settings, it can do
3500 a couple things. First, it can add its own check, to catch cases
3501 that somehow manage to get by the permissions checks in target
3502 methods. Second, if the target is wired to disallow particular
3503 settings (for instance, a system in the field that is not set up to
3504 be able to stop at a breakpoint), it can object to any unavailable
3508 remote_set_permissions (void)
3510 struct remote_state *rs = get_remote_state ();
3512 sprintf (rs->buf, "QAllow:"
3513 "WriteReg:%x;WriteMem:%x;"
3514 "InsertBreak:%x;InsertTrace:%x;"
3515 "InsertFastTrace:%x;Stop:%x",
3516 may_write_registers, may_write_memory,
3517 may_insert_breakpoints, may_insert_tracepoints,
3518 may_insert_fast_tracepoints, may_stop);
3520 getpkt (&rs->buf, &rs->buf_size, 0);
3522 /* If the target didn't like the packet, warn the user. Do not try
3523 to undo the user's settings, that would just be maddening. */
3524 if (strcmp (rs->buf, "OK") != 0)
3525 warning ("Remote refused setting permissions with: %s", rs->buf);
3528 /* This type describes each known response to the qSupported
3530 struct protocol_feature
3532 /* The name of this protocol feature. */
3535 /* The default for this protocol feature. */
3536 enum packet_support default_support;
3538 /* The function to call when this feature is reported, or after
3539 qSupported processing if the feature is not supported.
3540 The first argument points to this structure. The second
3541 argument indicates whether the packet requested support be
3542 enabled, disabled, or probed (or the default, if this function
3543 is being called at the end of processing and this feature was
3544 not reported). The third argument may be NULL; if not NULL, it
3545 is a NUL-terminated string taken from the packet following
3546 this feature's name and an equals sign. */
3547 void (*func) (const struct protocol_feature *, enum packet_support,
3550 /* The corresponding packet for this feature. Only used if
3551 FUNC is remote_supported_packet. */
3556 remote_supported_packet (const struct protocol_feature *feature,
3557 enum packet_support support,
3558 const char *argument)
3562 warning (_("Remote qSupported response supplied an unexpected value for"
3563 " \"%s\"."), feature->name);
3567 if (remote_protocol_packets[feature->packet].support
3568 == PACKET_SUPPORT_UNKNOWN)
3569 remote_protocol_packets[feature->packet].support = support;
3573 remote_packet_size (const struct protocol_feature *feature,
3574 enum packet_support support, const char *value)
3576 struct remote_state *rs = get_remote_state ();
3581 if (support != PACKET_ENABLE)
3584 if (value == NULL || *value == '\0')
3586 warning (_("Remote target reported \"%s\" without a size."),
3592 packet_size = strtol (value, &value_end, 16);
3593 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3595 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3596 feature->name, value);
3600 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3602 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3603 packet_size, MAX_REMOTE_PACKET_SIZE);
3604 packet_size = MAX_REMOTE_PACKET_SIZE;
3607 /* Record the new maximum packet size. */
3608 rs->explicit_packet_size = packet_size;
3612 remote_multi_process_feature (const struct protocol_feature *feature,
3613 enum packet_support support, const char *value)
3615 struct remote_state *rs = get_remote_state ();
3617 rs->multi_process_aware = (support == PACKET_ENABLE);
3621 remote_non_stop_feature (const struct protocol_feature *feature,
3622 enum packet_support support, const char *value)
3624 struct remote_state *rs = get_remote_state ();
3626 rs->non_stop_aware = (support == PACKET_ENABLE);
3630 remote_cond_tracepoint_feature (const struct protocol_feature *feature,
3631 enum packet_support support,
3634 struct remote_state *rs = get_remote_state ();
3636 rs->cond_tracepoints = (support == PACKET_ENABLE);
3640 remote_fast_tracepoint_feature (const struct protocol_feature *feature,
3641 enum packet_support support,
3644 struct remote_state *rs = get_remote_state ();
3646 rs->fast_tracepoints = (support == PACKET_ENABLE);
3650 remote_static_tracepoint_feature (const struct protocol_feature *feature,
3651 enum packet_support support,
3654 struct remote_state *rs = get_remote_state ();
3656 rs->static_tracepoints = (support == PACKET_ENABLE);
3660 remote_disconnected_tracing_feature (const struct protocol_feature *feature,
3661 enum packet_support support,
3664 struct remote_state *rs = get_remote_state ();
3666 rs->disconnected_tracing = (support == PACKET_ENABLE);
3669 static struct protocol_feature remote_protocol_features[] = {
3670 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
3671 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
3672 PACKET_qXfer_auxv },
3673 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3674 PACKET_qXfer_features },
3675 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3676 PACKET_qXfer_libraries },
3677 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
3678 PACKET_qXfer_memory_map },
3679 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3680 PACKET_qXfer_spu_read },
3681 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3682 PACKET_qXfer_spu_write },
3683 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3684 PACKET_qXfer_osdata },
3685 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3686 PACKET_qXfer_threads },
3687 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3688 PACKET_qXfer_traceframe_info },
3689 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3690 PACKET_QPassSignals },
3691 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3692 PACKET_QStartNoAckMode },
3693 { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 },
3694 { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 },
3695 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3696 PACKET_qXfer_siginfo_read },
3697 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3698 PACKET_qXfer_siginfo_write },
3699 { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature,
3700 PACKET_ConditionalTracepoints },
3701 { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature,
3702 PACKET_FastTracepoints },
3703 { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature,
3704 PACKET_StaticTracepoints },
3705 { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature,
3707 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
3709 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
3711 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3712 PACKET_TracepointSource },
3713 { "QAllow", PACKET_DISABLE, remote_supported_packet,
3717 static char *remote_support_xml;
3719 /* Register string appended to "xmlRegisters=" in qSupported query. */
3722 register_remote_support_xml (const char *xml)
3724 #if defined(HAVE_LIBEXPAT)
3725 if (remote_support_xml == NULL)
3726 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
3729 char *copy = xstrdup (remote_support_xml + 13);
3730 char *p = strtok (copy, ",");
3734 if (strcmp (p, xml) == 0)
3741 while ((p = strtok (NULL, ",")) != NULL);
3744 remote_support_xml = reconcat (remote_support_xml,
3745 remote_support_xml, ",", xml,
3752 remote_query_supported_append (char *msg, const char *append)
3755 return reconcat (msg, msg, ";", append, (char *) NULL);
3757 return xstrdup (append);
3761 remote_query_supported (void)
3763 struct remote_state *rs = get_remote_state ();
3766 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
3768 /* The packet support flags are handled differently for this packet
3769 than for most others. We treat an error, a disabled packet, and
3770 an empty response identically: any features which must be reported
3771 to be used will be automatically disabled. An empty buffer
3772 accomplishes this, since that is also the representation for a list
3773 containing no features. */
3776 if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
3779 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
3782 q = remote_query_supported_append (q, "multiprocess+");
3784 if (remote_support_xml)
3785 q = remote_query_supported_append (q, remote_support_xml);
3787 q = remote_query_supported_append (q, "qRelocInsn+");
3789 q = reconcat (q, "qSupported:", q, (char *) NULL);
3792 do_cleanups (old_chain);
3794 getpkt (&rs->buf, &rs->buf_size, 0);
3796 /* If an error occured, warn, but do not return - just reset the
3797 buffer to empty and go on to disable features. */
3798 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
3801 warning (_("Remote failure reply: %s"), rs->buf);
3806 memset (seen, 0, sizeof (seen));
3811 enum packet_support is_supported;
3812 char *p, *end, *name_end, *value;
3814 /* First separate out this item from the rest of the packet. If
3815 there's another item after this, we overwrite the separator
3816 (terminated strings are much easier to work with). */
3818 end = strchr (p, ';');
3821 end = p + strlen (p);
3831 warning (_("empty item in \"qSupported\" response"));
3836 name_end = strchr (p, '=');
3839 /* This is a name=value entry. */
3840 is_supported = PACKET_ENABLE;
3841 value = name_end + 1;
3850 is_supported = PACKET_ENABLE;
3854 is_supported = PACKET_DISABLE;
3858 is_supported = PACKET_SUPPORT_UNKNOWN;
3862 warning (_("unrecognized item \"%s\" "
3863 "in \"qSupported\" response"), p);
3869 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
3870 if (strcmp (remote_protocol_features[i].name, p) == 0)
3872 const struct protocol_feature *feature;
3875 feature = &remote_protocol_features[i];
3876 feature->func (feature, is_supported, value);
3881 /* If we increased the packet size, make sure to increase the global
3882 buffer size also. We delay this until after parsing the entire
3883 qSupported packet, because this is the same buffer we were
3885 if (rs->buf_size < rs->explicit_packet_size)
3887 rs->buf_size = rs->explicit_packet_size;
3888 rs->buf = xrealloc (rs->buf, rs->buf_size);
3891 /* Handle the defaults for unmentioned features. */
3892 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
3895 const struct protocol_feature *feature;
3897 feature = &remote_protocol_features[i];
3898 feature->func (feature, feature->default_support, NULL);
3904 remote_open_1 (char *name, int from_tty,
3905 struct target_ops *target, int extended_p)
3907 struct remote_state *rs = get_remote_state ();
3910 error (_("To open a remote debug connection, you need to specify what\n"
3911 "serial device is attached to the remote system\n"
3912 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3914 /* See FIXME above. */
3915 if (!target_async_permitted)
3916 wait_forever_enabled_p = 1;
3918 /* If we're connected to a running target, target_preopen will kill it.
3919 But if we're connected to a target system with no running process,
3920 then we will still be connected when it returns. Ask this question
3921 first, before target_preopen has a chance to kill anything. */
3922 if (remote_desc != NULL && !have_inferiors ())
3925 || query (_("Already connected to a remote target. Disconnect? ")))
3928 error (_("Still connected."));
3931 target_preopen (from_tty);
3933 unpush_target (target);
3935 /* This time without a query. If we were connected to an
3936 extended-remote target and target_preopen killed the running
3937 process, we may still be connected. If we are starting "target
3938 remote" now, the extended-remote target will not have been
3939 removed by unpush_target. */
3940 if (remote_desc != NULL && !have_inferiors ())
3943 /* Make sure we send the passed signals list the next time we resume. */
3944 xfree (last_pass_packet);
3945 last_pass_packet = NULL;
3947 remote_fileio_reset ();
3948 reopen_exec_file ();
3951 remote_desc = remote_serial_open (name);
3953 perror_with_name (name);
3955 if (baud_rate != -1)
3957 if (serial_setbaudrate (remote_desc, baud_rate))
3959 /* The requested speed could not be set. Error out to
3960 top level after closing remote_desc. Take care to
3961 set remote_desc to NULL to avoid closing remote_desc
3963 serial_close (remote_desc);
3965 perror_with_name (name);
3969 serial_raw (remote_desc);
3971 /* If there is something sitting in the buffer we might take it as a
3972 response to a command, which would be bad. */
3973 serial_flush_input (remote_desc);
3977 puts_filtered ("Remote debugging using ");
3978 puts_filtered (name);
3979 puts_filtered ("\n");
3981 push_target (target); /* Switch to using remote target now. */
3983 /* Register extra event sources in the event loop. */
3984 remote_async_inferior_event_token
3985 = create_async_event_handler (remote_async_inferior_event_handler,
3987 remote_async_get_pending_events_token
3988 = create_async_event_handler (remote_async_get_pending_events_handler,
3991 /* Reset the target state; these things will be queried either by
3992 remote_query_supported or as they are needed. */
3993 init_all_packet_configs ();
3994 rs->cached_wait_status = 0;
3995 rs->explicit_packet_size = 0;
3997 rs->multi_process_aware = 0;
3998 rs->extended = extended_p;
3999 rs->non_stop_aware = 0;
4000 rs->waiting_for_stop_reply = 0;
4001 rs->ctrlc_pending_p = 0;
4003 general_thread = not_sent_ptid;
4004 continue_thread = not_sent_ptid;
4006 /* Probe for ability to use "ThreadInfo" query, as required. */
4007 use_threadinfo_query = 1;
4008 use_threadextra_query = 1;
4010 if (target_async_permitted)
4012 /* With this target we start out by owning the terminal. */
4013 remote_async_terminal_ours_p = 1;
4015 /* FIXME: cagney/1999-09-23: During the initial connection it is
4016 assumed that the target is already ready and able to respond to
4017 requests. Unfortunately remote_start_remote() eventually calls
4018 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4019 around this. Eventually a mechanism that allows
4020 wait_for_inferior() to expect/get timeouts will be
4022 wait_forever_enabled_p = 0;
4025 /* First delete any symbols previously loaded from shared libraries. */
4026 no_shared_libraries (NULL, 0);
4029 init_thread_list ();
4031 /* Start the remote connection. If error() or QUIT, discard this
4032 target (we'd otherwise be in an inconsistent state) and then
4033 propogate the error on up the exception chain. This ensures that
4034 the caller doesn't stumble along blindly assuming that the
4035 function succeeded. The CLI doesn't have this problem but other
4036 UI's, such as MI do.
4038 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4039 this function should return an error indication letting the
4040 caller restore the previous state. Unfortunately the command
4041 ``target remote'' is directly wired to this function making that
4042 impossible. On a positive note, the CLI side of this problem has
4043 been fixed - the function set_cmd_context() makes it possible for
4044 all the ``target ....'' commands to share a common callback
4045 function. See cli-dump.c. */
4047 struct gdb_exception ex;
4048 struct start_remote_args args;
4050 args.from_tty = from_tty;
4051 args.target = target;
4052 args.extended_p = extended_p;
4054 ex = catch_exception (uiout, remote_start_remote, &args, RETURN_MASK_ALL);
4057 /* Pop the partially set up target - unless something else did
4058 already before throwing the exception. */
4059 if (remote_desc != NULL)
4061 if (target_async_permitted)
4062 wait_forever_enabled_p = 1;
4063 throw_exception (ex);
4067 if (target_async_permitted)
4068 wait_forever_enabled_p = 1;
4071 /* This takes a program previously attached to and detaches it. After
4072 this is done, GDB can be used to debug some other program. We
4073 better not have left any breakpoints in the target program or it'll
4074 die when it hits one. */
4077 remote_detach_1 (char *args, int from_tty, int extended)
4079 int pid = ptid_get_pid (inferior_ptid);
4080 struct remote_state *rs = get_remote_state ();
4083 error (_("Argument given to \"detach\" when remotely debugging."));
4085 if (!target_has_execution)
4086 error (_("No process to detach from."));
4088 /* Tell the remote target to detach. */
4089 if (remote_multi_process_p (rs))
4090 sprintf (rs->buf, "D;%x", pid);
4092 strcpy (rs->buf, "D");
4095 getpkt (&rs->buf, &rs->buf_size, 0);
4097 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4099 else if (rs->buf[0] == '\0')
4100 error (_("Remote doesn't know how to detach"));
4102 error (_("Can't detach process."));
4106 if (remote_multi_process_p (rs))
4107 printf_filtered (_("Detached from remote %s.\n"),
4108 target_pid_to_str (pid_to_ptid (pid)));
4112 puts_filtered (_("Detached from remote process.\n"));
4114 puts_filtered (_("Ending remote debugging.\n"));
4118 discard_pending_stop_replies (pid);
4119 target_mourn_inferior ();
4123 remote_detach (struct target_ops *ops, char *args, int from_tty)
4125 remote_detach_1 (args, from_tty, 0);
4129 extended_remote_detach (struct target_ops *ops, char *args, int from_tty)
4131 remote_detach_1 (args, from_tty, 1);
4134 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4137 remote_disconnect (struct target_ops *target, char *args, int from_tty)
4140 error (_("Argument given to \"disconnect\" when remotely debugging."));
4142 /* Make sure we unpush even the extended remote targets; mourn
4143 won't do it. So call remote_mourn_1 directly instead of
4144 target_mourn_inferior. */
4145 remote_mourn_1 (target);
4148 puts_filtered ("Ending remote debugging.\n");
4151 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4152 be chatty about it. */
4155 extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
4157 struct remote_state *rs = get_remote_state ();
4159 char *wait_status = NULL;
4161 pid = parse_pid_to_attach (args);
4163 /* Remote PID can be freely equal to getpid, do not check it here the same
4164 way as in other targets. */
4166 if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4167 error (_("This target does not support attaching to a process"));
4169 sprintf (rs->buf, "vAttach;%x", pid);
4171 getpkt (&rs->buf, &rs->buf_size, 0);
4173 if (packet_ok (rs->buf,
4174 &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK)
4177 printf_unfiltered (_("Attached to %s\n"),
4178 target_pid_to_str (pid_to_ptid (pid)));
4182 /* Save the reply for later. */
4183 wait_status = alloca (strlen (rs->buf) + 1);
4184 strcpy (wait_status, rs->buf);
4186 else if (strcmp (rs->buf, "OK") != 0)
4187 error (_("Attaching to %s failed with: %s"),
4188 target_pid_to_str (pid_to_ptid (pid)),
4191 else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4192 error (_("This target does not support attaching to a process"));
4194 error (_("Attaching to %s failed"),
4195 target_pid_to_str (pid_to_ptid (pid)));
4197 set_current_inferior (remote_add_inferior (pid, 1));
4199 inferior_ptid = pid_to_ptid (pid);
4203 struct thread_info *thread;
4205 /* Get list of threads. */
4206 remote_threads_info (target);
4208 thread = first_thread_of_process (pid);
4210 inferior_ptid = thread->ptid;
4212 inferior_ptid = pid_to_ptid (pid);
4214 /* Invalidate our notion of the remote current thread. */
4215 record_currthread (minus_one_ptid);
4219 /* Now, if we have thread information, update inferior_ptid. */
4220 inferior_ptid = remote_current_thread (inferior_ptid);
4222 /* Add the main thread to the thread list. */
4223 add_thread_silent (inferior_ptid);
4226 /* Next, if the target can specify a description, read it. We do
4227 this before anything involving memory or registers. */
4228 target_find_description ();
4232 /* Use the previously fetched status. */
4233 gdb_assert (wait_status != NULL);
4235 if (target_can_async_p ())
4237 struct stop_reply *stop_reply;
4238 struct cleanup *old_chain;
4240 stop_reply = stop_reply_xmalloc ();
4241 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
4242 remote_parse_stop_reply (wait_status, stop_reply);
4243 discard_cleanups (old_chain);
4244 push_stop_reply (stop_reply);
4246 target_async (inferior_event_handler, 0);
4250 gdb_assert (wait_status != NULL);
4251 strcpy (rs->buf, wait_status);
4252 rs->cached_wait_status = 1;
4256 gdb_assert (wait_status == NULL);
4260 extended_remote_attach (struct target_ops *ops, char *args, int from_tty)
4262 extended_remote_attach_1 (ops, args, from_tty);
4265 /* Convert hex digit A to a number. */
4270 if (a >= '0' && a <= '9')
4272 else if (a >= 'a' && a <= 'f')
4273 return a - 'a' + 10;
4274 else if (a >= 'A' && a <= 'F')
4275 return a - 'A' + 10;
4277 error (_("Reply contains invalid hex digit %d"), a);
4281 hex2bin (const char *hex, gdb_byte *bin, int count)
4285 for (i = 0; i < count; i++)
4287 if (hex[0] == 0 || hex[1] == 0)
4289 /* Hex string is short, or of uneven length.
4290 Return the count that has been converted so far. */
4293 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
4299 /* Convert number NIB to a hex digit. */
4307 return 'a' + nib - 10;
4311 bin2hex (const gdb_byte *bin, char *hex, int count)
4315 /* May use a length, or a nul-terminated string as input. */
4317 count = strlen ((char *) bin);
4319 for (i = 0; i < count; i++)
4321 *hex++ = tohex ((*bin >> 4) & 0xf);
4322 *hex++ = tohex (*bin++ & 0xf);
4328 /* Check for the availability of vCont. This function should also check
4332 remote_vcont_probe (struct remote_state *rs)
4336 strcpy (rs->buf, "vCont?");
4338 getpkt (&rs->buf, &rs->buf_size, 0);
4341 /* Make sure that the features we assume are supported. */
4342 if (strncmp (buf, "vCont", 5) == 0)
4345 int support_s, support_S, support_c, support_C;
4351 rs->support_vCont_t = 0;
4352 while (p && *p == ';')
4355 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4357 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4359 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4361 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4363 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4364 rs->support_vCont_t = 1;
4366 p = strchr (p, ';');
4369 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4370 BUF will make packet_ok disable the packet. */
4371 if (!support_s || !support_S || !support_c || !support_C)
4375 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4378 /* Helper function for building "vCont" resumptions. Write a
4379 resumption to P. ENDP points to one-passed-the-end of the buffer
4380 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4381 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4382 resumed thread should be single-stepped and/or signalled. If PTID
4383 equals minus_one_ptid, then all threads are resumed; if PTID
4384 represents a process, then all threads of the process are resumed;
4385 the thread to be stepped and/or signalled is given in the global
4389 append_resumption (char *p, char *endp,
4390 ptid_t ptid, int step, enum target_signal siggnal)
4392 struct remote_state *rs = get_remote_state ();
4394 if (step && siggnal != TARGET_SIGNAL_0)
4395 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4397 p += xsnprintf (p, endp - p, ";s");
4398 else if (siggnal != TARGET_SIGNAL_0)
4399 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4401 p += xsnprintf (p, endp - p, ";c");
4403 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4407 /* All (-1) threads of process. */
4408 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4410 p += xsnprintf (p, endp - p, ":");
4411 p = write_ptid (p, endp, nptid);
4413 else if (!ptid_equal (ptid, minus_one_ptid))
4415 p += xsnprintf (p, endp - p, ":");
4416 p = write_ptid (p, endp, ptid);
4422 /* Resume the remote inferior by using a "vCont" packet. The thread
4423 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4424 resumed thread should be single-stepped and/or signalled. If PTID
4425 equals minus_one_ptid, then all threads are resumed; the thread to
4426 be stepped and/or signalled is given in the global INFERIOR_PTID.
4427 This function returns non-zero iff it resumes the inferior.
4429 This function issues a strict subset of all possible vCont commands at the
4433 remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
4435 struct remote_state *rs = get_remote_state ();
4439 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
4440 remote_vcont_probe (rs);
4442 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
4446 endp = rs->buf + get_remote_packet_size ();
4448 /* If we could generate a wider range of packets, we'd have to worry
4449 about overflowing BUF. Should there be a generic
4450 "multi-part-packet" packet? */
4452 p += xsnprintf (p, endp - p, "vCont");
4454 if (ptid_equal (ptid, magic_null_ptid))
4456 /* MAGIC_NULL_PTID means that we don't have any active threads,
4457 so we don't have any TID numbers the inferior will
4458 understand. Make sure to only send forms that do not specify
4460 p = append_resumption (p, endp, minus_one_ptid, step, siggnal);
4462 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
4464 /* Resume all threads (of all processes, or of a single
4465 process), with preference for INFERIOR_PTID. This assumes
4466 inferior_ptid belongs to the set of all threads we are about
4468 if (step || siggnal != TARGET_SIGNAL_0)
4470 /* Step inferior_ptid, with or without signal. */
4471 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
4474 /* And continue others without a signal. */
4475 p = append_resumption (p, endp, ptid, /*step=*/ 0, TARGET_SIGNAL_0);
4479 /* Scheduler locking; resume only PTID. */
4480 p = append_resumption (p, endp, ptid, step, siggnal);
4483 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4488 /* In non-stop, the stub replies to vCont with "OK". The stop
4489 reply will be reported asynchronously by means of a `%Stop'
4491 getpkt (&rs->buf, &rs->buf_size, 0);
4492 if (strcmp (rs->buf, "OK") != 0)
4493 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4499 /* Tell the remote machine to resume. */
4501 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
4503 static int last_sent_step;
4506 remote_resume (struct target_ops *ops,
4507 ptid_t ptid, int step, enum target_signal siggnal)
4509 struct remote_state *rs = get_remote_state ();
4512 last_sent_signal = siggnal;
4513 last_sent_step = step;
4515 /* Update the inferior on signals to silently pass, if they've changed. */
4516 remote_pass_signals ();
4518 /* The vCont packet doesn't need to specify threads via Hc. */
4519 /* No reverse support (yet) for vCont. */
4520 if (execution_direction != EXEC_REVERSE)
4521 if (remote_vcont_resume (ptid, step, siggnal))
4524 /* All other supported resume packets do use Hc, so set the continue
4526 if (ptid_equal (ptid, minus_one_ptid))
4527 set_continue_thread (any_thread_ptid);
4529 set_continue_thread (ptid);
4532 if (execution_direction == EXEC_REVERSE)
4534 /* We don't pass signals to the target in reverse exec mode. */
4535 if (info_verbose && siggnal != TARGET_SIGNAL_0)
4536 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
4540 && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE)
4541 error (_("Remote reverse-step not supported."));
4543 && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE)
4544 error (_("Remote reverse-continue not supported."));
4546 strcpy (buf, step ? "bs" : "bc");
4548 else if (siggnal != TARGET_SIGNAL_0)
4550 buf[0] = step ? 'S' : 'C';
4551 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
4552 buf[2] = tohex (((int) siggnal) & 0xf);
4556 strcpy (buf, step ? "s" : "c");
4561 /* We are about to start executing the inferior, let's register it
4562 with the event loop. NOTE: this is the one place where all the
4563 execution commands end up. We could alternatively do this in each
4564 of the execution commands in infcmd.c. */
4565 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4566 into infcmd.c in order to allow inferior function calls to work
4567 NOT asynchronously. */
4568 if (target_can_async_p ())
4569 target_async (inferior_event_handler, 0);
4571 /* We've just told the target to resume. The remote server will
4572 wait for the inferior to stop, and then send a stop reply. In
4573 the mean time, we can't start another command/query ourselves
4574 because the stub wouldn't be ready to process it. This applies
4575 only to the base all-stop protocol, however. In non-stop (which
4576 only supports vCont), the stub replies with an "OK", and is
4577 immediate able to process further serial input. */
4579 rs->waiting_for_stop_reply = 1;
4583 /* Set up the signal handler for SIGINT, while the target is
4584 executing, ovewriting the 'regular' SIGINT signal handler. */
4586 initialize_sigint_signal_handler (void)
4588 signal (SIGINT, handle_remote_sigint);
4591 /* Signal handler for SIGINT, while the target is executing. */
4593 handle_remote_sigint (int sig)
4595 signal (sig, handle_remote_sigint_twice);
4596 mark_async_signal_handler_wrapper (sigint_remote_token);
4599 /* Signal handler for SIGINT, installed after SIGINT has already been
4600 sent once. It will take effect the second time that the user sends
4603 handle_remote_sigint_twice (int sig)
4605 signal (sig, handle_remote_sigint);
4606 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
4609 /* Perform the real interruption of the target execution, in response
4612 async_remote_interrupt (gdb_client_data arg)
4615 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
4617 target_stop (inferior_ptid);
4620 /* Perform interrupt, if the first attempt did not succeed. Just give
4621 up on the target alltogether. */
4623 async_remote_interrupt_twice (gdb_client_data arg)
4626 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
4631 /* Reinstall the usual SIGINT handlers, after the target has
4634 cleanup_sigint_signal_handler (void *dummy)
4636 signal (SIGINT, handle_sigint);
4639 /* Send ^C to target to halt it. Target will respond, and send us a
4641 static void (*ofunc) (int);
4643 /* The command line interface's stop routine. This function is installed
4644 as a signal handler for SIGINT. The first time a user requests a
4645 stop, we call remote_stop to send a break or ^C. If there is no
4646 response from the target (it didn't stop when the user requested it),
4647 we ask the user if he'd like to detach from the target. */
4649 remote_interrupt (int signo)
4651 /* If this doesn't work, try more severe steps. */
4652 signal (signo, remote_interrupt_twice);
4654 gdb_call_async_signal_handler (sigint_remote_token, 1);
4657 /* The user typed ^C twice. */
4660 remote_interrupt_twice (int signo)
4662 signal (signo, ofunc);
4663 gdb_call_async_signal_handler (sigint_remote_twice_token, 1);
4664 signal (signo, remote_interrupt);
4667 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4668 thread, all threads of a remote process, or all threads of all
4672 remote_stop_ns (ptid_t ptid)
4674 struct remote_state *rs = get_remote_state ();
4676 char *endp = rs->buf + get_remote_packet_size ();
4678 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
4679 remote_vcont_probe (rs);
4681 if (!rs->support_vCont_t)
4682 error (_("Remote server does not support stopping threads"));
4684 if (ptid_equal (ptid, minus_one_ptid)
4685 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4686 p += xsnprintf (p, endp - p, "vCont;t");
4691 p += xsnprintf (p, endp - p, "vCont;t:");
4693 if (ptid_is_pid (ptid))
4694 /* All (-1) threads of process. */
4695 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4698 /* Small optimization: if we already have a stop reply for
4699 this thread, no use in telling the stub we want this
4701 if (peek_stop_reply (ptid))
4707 p = write_ptid (p, endp, nptid);
4710 /* In non-stop, we get an immediate OK reply. The stop reply will
4711 come in asynchronously by notification. */
4713 getpkt (&rs->buf, &rs->buf_size, 0);
4714 if (strcmp (rs->buf, "OK") != 0)
4715 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
4718 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4719 remote target. It is undefined which thread of which process
4720 reports the stop. */
4723 remote_stop_as (ptid_t ptid)
4725 struct remote_state *rs = get_remote_state ();
4727 rs->ctrlc_pending_p = 1;
4729 /* If the inferior is stopped already, but the core didn't know
4730 about it yet, just ignore the request. The cached wait status
4731 will be collected in remote_wait. */
4732 if (rs->cached_wait_status)
4735 /* Send interrupt_sequence to remote target. */
4736 send_interrupt_sequence ();
4739 /* This is the generic stop called via the target vector. When a target
4740 interrupt is requested, either by the command line or the GUI, we
4741 will eventually end up here. */
4744 remote_stop (ptid_t ptid)
4747 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
4750 remote_stop_ns (ptid);
4752 remote_stop_as (ptid);
4755 /* Ask the user what to do when an interrupt is received. */
4758 interrupt_query (void)
4760 target_terminal_ours ();
4762 if (target_can_async_p ())
4764 signal (SIGINT, handle_sigint);
4765 deprecated_throw_reason (RETURN_QUIT);
4769 if (query (_("Interrupted while waiting for the program.\n\
4770 Give up (and stop debugging it)? ")))
4773 deprecated_throw_reason (RETURN_QUIT);
4777 target_terminal_inferior ();
4780 /* Enable/disable target terminal ownership. Most targets can use
4781 terminal groups to control terminal ownership. Remote targets are
4782 different in that explicit transfer of ownership to/from GDB/target
4786 remote_terminal_inferior (void)
4788 if (!target_async_permitted)
4789 /* Nothing to do. */
4792 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4793 idempotent. The event-loop GDB talking to an asynchronous target
4794 with a synchronous command calls this function from both
4795 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
4796 transfer the terminal to the target when it shouldn't this guard
4798 if (!remote_async_terminal_ours_p)
4800 delete_file_handler (input_fd);
4801 remote_async_terminal_ours_p = 0;
4802 initialize_sigint_signal_handler ();
4803 /* NOTE: At this point we could also register our selves as the
4804 recipient of all input. Any characters typed could then be
4805 passed on down to the target. */
4809 remote_terminal_ours (void)
4811 if (!target_async_permitted)
4812 /* Nothing to do. */
4815 /* See FIXME in remote_terminal_inferior. */
4816 if (remote_async_terminal_ours_p)
4818 cleanup_sigint_signal_handler (NULL);
4819 add_file_handler (input_fd, stdin_event_handler, 0);
4820 remote_async_terminal_ours_p = 1;
4824 remote_console_output (char *msg)
4828 for (p = msg; p[0] && p[1]; p += 2)
4831 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
4835 fputs_unfiltered (tb, gdb_stdtarg);
4837 gdb_flush (gdb_stdtarg);
4840 typedef struct cached_reg
4843 gdb_byte data[MAX_REGISTER_SIZE];
4846 DEF_VEC_O(cached_reg_t);
4850 struct stop_reply *next;
4854 struct target_waitstatus ws;
4856 VEC(cached_reg_t) *regcache;
4858 int stopped_by_watchpoint_p;
4859 CORE_ADDR watch_data_address;
4867 /* The list of already fetched and acknowledged stop events. */
4868 static struct stop_reply *stop_reply_queue;
4870 static struct stop_reply *
4871 stop_reply_xmalloc (void)
4873 struct stop_reply *r = XMALLOC (struct stop_reply);
4880 stop_reply_xfree (struct stop_reply *r)
4884 VEC_free (cached_reg_t, r->regcache);
4889 /* Discard all pending stop replies of inferior PID. If PID is -1,
4890 discard everything. */
4893 discard_pending_stop_replies (int pid)
4895 struct stop_reply *prev = NULL, *reply, *next;
4897 /* Discard the in-flight notification. */
4898 if (pending_stop_reply != NULL
4900 || ptid_get_pid (pending_stop_reply->ptid) == pid))
4902 stop_reply_xfree (pending_stop_reply);
4903 pending_stop_reply = NULL;
4906 /* Discard the stop replies we have already pulled with
4908 for (reply = stop_reply_queue; reply; reply = next)
4912 || ptid_get_pid (reply->ptid) == pid)
4914 if (reply == stop_reply_queue)
4915 stop_reply_queue = reply->next;
4917 prev->next = reply->next;
4919 stop_reply_xfree (reply);
4926 /* Cleanup wrapper. */
4929 do_stop_reply_xfree (void *arg)
4931 struct stop_reply *r = arg;
4933 stop_reply_xfree (r);
4936 /* Look for a queued stop reply belonging to PTID. If one is found,
4937 remove it from the queue, and return it. Returns NULL if none is
4938 found. If there are still queued events left to process, tell the
4939 event loop to get back to target_wait soon. */
4941 static struct stop_reply *
4942 queued_stop_reply (ptid_t ptid)
4944 struct stop_reply *it;
4945 struct stop_reply **it_link;
4947 it = stop_reply_queue;
4948 it_link = &stop_reply_queue;
4951 if (ptid_match (it->ptid, ptid))
4953 *it_link = it->next;
4958 it_link = &it->next;
4962 if (stop_reply_queue)
4963 /* There's still at least an event left. */
4964 mark_async_event_handler (remote_async_inferior_event_token);
4969 /* Push a fully parsed stop reply in the stop reply queue. Since we
4970 know that we now have at least one queued event left to pass to the
4971 core side, tell the event loop to get back to target_wait soon. */
4974 push_stop_reply (struct stop_reply *new_event)
4976 struct stop_reply *event;
4978 if (stop_reply_queue)
4980 for (event = stop_reply_queue;
4981 event && event->next;
4982 event = event->next)
4985 event->next = new_event;
4988 stop_reply_queue = new_event;
4990 mark_async_event_handler (remote_async_inferior_event_token);
4993 /* Returns true if we have a stop reply for PTID. */
4996 peek_stop_reply (ptid_t ptid)
4998 struct stop_reply *it;
5000 for (it = stop_reply_queue; it; it = it->next)
5001 if (ptid_equal (ptid, it->ptid))
5003 if (it->ws.kind == TARGET_WAITKIND_STOPPED)
5010 /* Parse the stop reply in BUF. Either the function succeeds, and the
5011 result is stored in EVENT, or throws an error. */
5014 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5016 struct remote_arch_state *rsa = get_remote_arch_state ();
5020 event->ptid = null_ptid;
5021 event->ws.kind = TARGET_WAITKIND_IGNORE;
5022 event->ws.value.integer = 0;
5023 event->solibs_changed = 0;
5024 event->replay_event = 0;
5025 event->stopped_by_watchpoint_p = 0;
5026 event->regcache = NULL;
5031 case 'T': /* Status with PC, SP, FP, ... */
5032 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5033 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5035 n... = register number
5036 r... = register contents
5039 p = &buf[3]; /* after Txx */
5047 /* If the packet contains a register number, save it in
5048 pnum and set p1 to point to the character following it.
5049 Otherwise p1 points to p. */
5051 /* If this packet is an awatch packet, don't parse the 'a'
5052 as a register number. */
5054 if (strncmp (p, "awatch", strlen("awatch")) != 0
5055 && strncmp (p, "core", strlen ("core") != 0))
5057 /* Read the ``P'' register number. */
5058 pnum = strtol (p, &p_temp, 16);
5064 if (p1 == p) /* No register number present here. */
5066 p1 = strchr (p, ':');
5068 error (_("Malformed packet(a) (missing colon): %s\n\
5071 if (strncmp (p, "thread", p1 - p) == 0)
5072 event->ptid = read_ptid (++p1, &p);
5073 else if ((strncmp (p, "watch", p1 - p) == 0)
5074 || (strncmp (p, "rwatch", p1 - p) == 0)
5075 || (strncmp (p, "awatch", p1 - p) == 0))
5077 event->stopped_by_watchpoint_p = 1;
5078 p = unpack_varlen_hex (++p1, &addr);
5079 event->watch_data_address = (CORE_ADDR) addr;
5081 else if (strncmp (p, "library", p1 - p) == 0)
5085 while (*p_temp && *p_temp != ';')
5088 event->solibs_changed = 1;
5091 else if (strncmp (p, "replaylog", p1 - p) == 0)
5093 /* NO_HISTORY event.
5094 p1 will indicate "begin" or "end", but
5095 it makes no difference for now, so ignore it. */
5096 event->replay_event = 1;
5097 p_temp = strchr (p1 + 1, ';');
5101 else if (strncmp (p, "core", p1 - p) == 0)
5105 p = unpack_varlen_hex (++p1, &c);
5110 /* Silently skip unknown optional info. */
5111 p_temp = strchr (p1 + 1, ';');
5118 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5119 cached_reg_t cached_reg;
5124 error (_("Malformed packet(b) (missing colon): %s\n\
5130 error (_("Remote sent bad register number %s: %s\n\
5132 hex_string (pnum), p, buf);
5134 cached_reg.num = reg->regnum;
5136 fieldsize = hex2bin (p, cached_reg.data,
5137 register_size (target_gdbarch,
5140 if (fieldsize < register_size (target_gdbarch,
5142 warning (_("Remote reply is too short: %s"), buf);
5144 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5148 error (_("Remote register badly formatted: %s\nhere: %s"),
5153 case 'S': /* Old style status, just signal only. */
5154 if (event->solibs_changed)
5155 event->ws.kind = TARGET_WAITKIND_LOADED;
5156 else if (event->replay_event)
5157 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5160 event->ws.kind = TARGET_WAITKIND_STOPPED;
5161 event->ws.value.sig = (enum target_signal)
5162 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
5165 case 'W': /* Target exited. */
5172 /* GDB used to accept only 2 hex chars here. Stubs should
5173 only send more if they detect GDB supports multi-process
5175 p = unpack_varlen_hex (&buf[1], &value);
5179 /* The remote process exited. */
5180 event->ws.kind = TARGET_WAITKIND_EXITED;
5181 event->ws.value.integer = value;
5185 /* The remote process exited with a signal. */
5186 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5187 event->ws.value.sig = (enum target_signal) value;
5190 /* If no process is specified, assume inferior_ptid. */
5191 pid = ptid_get_pid (inferior_ptid);
5200 else if (strncmp (p,
5201 "process:", sizeof ("process:") - 1) == 0)
5205 p += sizeof ("process:") - 1;
5206 unpack_varlen_hex (p, &upid);
5210 error (_("unknown stop reply packet: %s"), buf);
5213 error (_("unknown stop reply packet: %s"), buf);
5214 event->ptid = pid_to_ptid (pid);
5219 if (non_stop && ptid_equal (event->ptid, null_ptid))
5220 error (_("No process or thread specified in stop reply: %s"), buf);
5223 /* When the stub wants to tell GDB about a new stop reply, it sends a
5224 stop notification (%Stop). Those can come it at any time, hence,
5225 we have to make sure that any pending putpkt/getpkt sequence we're
5226 making is finished, before querying the stub for more events with
5227 vStopped. E.g., if we started a vStopped sequence immediatelly
5228 upon receiving the %Stop notification, something like this could
5236 1.6) <-- (registers reply to step #1.3)
5238 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5241 To solve this, whenever we parse a %Stop notification sucessfully,
5242 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5243 doing whatever we were doing:
5249 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5250 2.5) <-- (registers reply to step #2.3)
5252 Eventualy after step #2.5, we return to the event loop, which
5253 notices there's an event on the
5254 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5255 associated callback --- the function below. At this point, we're
5256 always safe to start a vStopped sequence. :
5259 2.7) <-- T05 thread:2
5265 remote_get_pending_stop_replies (void)
5267 struct remote_state *rs = get_remote_state ();
5269 if (pending_stop_reply)
5272 putpkt ("vStopped");
5274 /* Now we can rely on it. */
5275 push_stop_reply (pending_stop_reply);
5276 pending_stop_reply = NULL;
5280 getpkt (&rs->buf, &rs->buf_size, 0);
5281 if (strcmp (rs->buf, "OK") == 0)
5285 struct cleanup *old_chain;
5286 struct stop_reply *stop_reply = stop_reply_xmalloc ();
5288 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5289 remote_parse_stop_reply (rs->buf, stop_reply);
5292 putpkt ("vStopped");
5294 if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE)
5296 /* Now we can rely on it. */
5297 discard_cleanups (old_chain);
5298 push_stop_reply (stop_reply);
5301 /* We got an unknown stop reply. */
5302 do_cleanups (old_chain);
5309 /* Called when it is decided that STOP_REPLY holds the info of the
5310 event that is to be returned to the core. This function always
5311 destroys STOP_REPLY. */
5314 process_stop_reply (struct stop_reply *stop_reply,
5315 struct target_waitstatus *status)
5319 *status = stop_reply->ws;
5320 ptid = stop_reply->ptid;
5322 /* If no thread/process was reported by the stub, assume the current
5324 if (ptid_equal (ptid, null_ptid))
5325 ptid = inferior_ptid;
5327 if (status->kind != TARGET_WAITKIND_EXITED
5328 && status->kind != TARGET_WAITKIND_SIGNALLED)
5330 /* Expedited registers. */
5331 if (stop_reply->regcache)
5333 struct regcache *regcache
5334 = get_thread_arch_regcache (ptid, target_gdbarch);
5339 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5341 regcache_raw_supply (regcache, reg->num, reg->data);
5342 VEC_free (cached_reg_t, stop_reply->regcache);
5345 remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5346 remote_watch_data_address = stop_reply->watch_data_address;
5348 remote_notice_new_inferior (ptid, 0);
5349 demand_private_info (ptid)->core = stop_reply->core;
5352 stop_reply_xfree (stop_reply);
5356 /* The non-stop mode version of target_wait. */
5359 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
5361 struct remote_state *rs = get_remote_state ();
5362 struct stop_reply *stop_reply;
5365 /* If in non-stop mode, get out of getpkt even if a
5366 notification is received. */
5368 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5375 case 'E': /* Error of some sort. */
5376 /* We're out of sync with the target now. Did it continue
5377 or not? We can't tell which thread it was in non-stop,
5378 so just ignore this. */
5379 warning (_("Remote failure reply: %s"), rs->buf);
5381 case 'O': /* Console output. */
5382 remote_console_output (rs->buf + 1);
5385 warning (_("Invalid remote reply: %s"), rs->buf);
5389 /* Acknowledge a pending stop reply that may have arrived in the
5391 if (pending_stop_reply != NULL)
5392 remote_get_pending_stop_replies ();
5394 /* If indeed we noticed a stop reply, we're done. */
5395 stop_reply = queued_stop_reply (ptid);
5396 if (stop_reply != NULL)
5397 return process_stop_reply (stop_reply, status);
5399 /* Still no event. If we're just polling for an event, then
5400 return to the event loop. */
5401 if (options & TARGET_WNOHANG)
5403 status->kind = TARGET_WAITKIND_IGNORE;
5404 return minus_one_ptid;
5407 /* Otherwise do a blocking wait. */
5408 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5413 /* Wait until the remote machine stops, then return, storing status in
5414 STATUS just as `wait' would. */
5417 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
5419 struct remote_state *rs = get_remote_state ();
5420 ptid_t event_ptid = null_ptid;
5422 struct stop_reply *stop_reply;
5426 status->kind = TARGET_WAITKIND_IGNORE;
5427 status->value.integer = 0;
5429 stop_reply = queued_stop_reply (ptid);
5430 if (stop_reply != NULL)
5431 return process_stop_reply (stop_reply, status);
5433 if (rs->cached_wait_status)
5434 /* Use the cached wait status, but only once. */
5435 rs->cached_wait_status = 0;
5440 if (!target_is_async_p ())
5442 ofunc = signal (SIGINT, remote_interrupt);
5443 /* If the user hit C-c before this packet, or between packets,
5444 pretend that it was hit right here. */
5448 remote_interrupt (SIGINT);
5452 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5453 _never_ wait for ever -> test on target_is_async_p().
5454 However, before we do that we need to ensure that the caller
5455 knows how to take the target into/out of async mode. */
5456 ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
5457 if (!target_is_async_p ())
5458 signal (SIGINT, ofunc);
5463 remote_stopped_by_watchpoint_p = 0;
5465 /* We got something. */
5466 rs->waiting_for_stop_reply = 0;
5468 /* Assume that the target has acknowledged Ctrl-C unless we receive
5469 an 'F' or 'O' packet. */
5470 if (buf[0] != 'F' && buf[0] != 'O')
5471 rs->ctrlc_pending_p = 0;
5475 case 'E': /* Error of some sort. */
5476 /* We're out of sync with the target now. Did it continue or
5477 not? Not is more likely, so report a stop. */
5478 warning (_("Remote failure reply: %s"), buf);
5479 status->kind = TARGET_WAITKIND_STOPPED;
5480 status->value.sig = TARGET_SIGNAL_0;
5482 case 'F': /* File-I/O request. */
5483 remote_fileio_request (buf, rs->ctrlc_pending_p);
5484 rs->ctrlc_pending_p = 0;
5486 case 'T': case 'S': case 'X': case 'W':
5488 struct stop_reply *stop_reply;
5489 struct cleanup *old_chain;
5491 stop_reply = stop_reply_xmalloc ();
5492 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5493 remote_parse_stop_reply (buf, stop_reply);
5494 discard_cleanups (old_chain);
5495 event_ptid = process_stop_reply (stop_reply, status);
5498 case 'O': /* Console output. */
5499 remote_console_output (buf + 1);
5501 /* The target didn't really stop; keep waiting. */
5502 rs->waiting_for_stop_reply = 1;
5506 if (last_sent_signal != TARGET_SIGNAL_0)
5508 /* Zero length reply means that we tried 'S' or 'C' and the
5509 remote system doesn't support it. */
5510 target_terminal_ours_for_output ();
5512 ("Can't send signals to this remote system. %s not sent.\n",
5513 target_signal_to_name (last_sent_signal));
5514 last_sent_signal = TARGET_SIGNAL_0;
5515 target_terminal_inferior ();
5517 strcpy ((char *) buf, last_sent_step ? "s" : "c");
5518 putpkt ((char *) buf);
5520 /* We just told the target to resume, so a stop reply is in
5522 rs->waiting_for_stop_reply = 1;
5525 /* else fallthrough */
5527 warning (_("Invalid remote reply: %s"), buf);
5529 rs->waiting_for_stop_reply = 1;
5533 if (status->kind == TARGET_WAITKIND_IGNORE)
5535 /* Nothing interesting happened. If we're doing a non-blocking
5536 poll, we're done. Otherwise, go back to waiting. */
5537 if (options & TARGET_WNOHANG)
5538 return minus_one_ptid;
5542 else if (status->kind != TARGET_WAITKIND_EXITED
5543 && status->kind != TARGET_WAITKIND_SIGNALLED)
5545 if (!ptid_equal (event_ptid, null_ptid))
5546 record_currthread (event_ptid);
5548 event_ptid = inferior_ptid;
5551 /* A process exit. Invalidate our notion of current thread. */
5552 record_currthread (minus_one_ptid);
5557 /* Wait until the remote machine stops, then return, storing status in
5558 STATUS just as `wait' would. */
5561 remote_wait (struct target_ops *ops,
5562 ptid_t ptid, struct target_waitstatus *status, int options)
5567 event_ptid = remote_wait_ns (ptid, status, options);
5569 event_ptid = remote_wait_as (ptid, status, options);
5571 if (target_can_async_p ())
5573 /* If there are are events left in the queue tell the event loop
5575 if (stop_reply_queue)
5576 mark_async_event_handler (remote_async_inferior_event_token);
5582 /* Fetch a single register using a 'p' packet. */
5585 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
5587 struct remote_state *rs = get_remote_state ();
5589 char regp[MAX_REGISTER_SIZE];
5592 if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE)
5595 if (reg->pnum == -1)
5600 p += hexnumstr (p, reg->pnum);
5603 getpkt (&rs->buf, &rs->buf_size, 0);
5607 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
5611 case PACKET_UNKNOWN:
5614 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5615 gdbarch_register_name (get_regcache_arch (regcache),
5620 /* If this register is unfetchable, tell the regcache. */
5623 regcache_raw_supply (regcache, reg->regnum, NULL);
5627 /* Otherwise, parse and supply the value. */
5633 error (_("fetch_register_using_p: early buf termination"));
5635 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
5638 regcache_raw_supply (regcache, reg->regnum, regp);
5642 /* Fetch the registers included in the target's 'g' packet. */
5645 send_g_packet (void)
5647 struct remote_state *rs = get_remote_state ();
5650 sprintf (rs->buf, "g");
5651 remote_send (&rs->buf, &rs->buf_size);
5653 /* We can get out of synch in various cases. If the first character
5654 in the buffer is not a hex character, assume that has happened
5655 and try to fetch another packet to read. */
5656 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
5657 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
5658 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
5659 && rs->buf[0] != 'x') /* New: unavailable register value. */
5662 fprintf_unfiltered (gdb_stdlog,
5663 "Bad register packet; fetching a new packet\n");
5664 getpkt (&rs->buf, &rs->buf_size, 0);
5667 buf_len = strlen (rs->buf);
5669 /* Sanity check the received packet. */
5670 if (buf_len % 2 != 0)
5671 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
5677 process_g_packet (struct regcache *regcache)
5679 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5680 struct remote_state *rs = get_remote_state ();
5681 struct remote_arch_state *rsa = get_remote_arch_state ();
5686 buf_len = strlen (rs->buf);
5688 /* Further sanity checks, with knowledge of the architecture. */
5689 if (buf_len > 2 * rsa->sizeof_g_packet)
5690 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
5692 /* Save the size of the packet sent to us by the target. It is used
5693 as a heuristic when determining the max size of packets that the
5694 target can safely receive. */
5695 if (rsa->actual_register_packet_size == 0)
5696 rsa->actual_register_packet_size = buf_len;
5698 /* If this is smaller than we guessed the 'g' packet would be,
5699 update our records. A 'g' reply that doesn't include a register's
5700 value implies either that the register is not available, or that
5701 the 'p' packet must be used. */
5702 if (buf_len < 2 * rsa->sizeof_g_packet)
5704 rsa->sizeof_g_packet = buf_len / 2;
5706 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
5708 if (rsa->regs[i].pnum == -1)
5711 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
5712 rsa->regs[i].in_g_packet = 0;
5714 rsa->regs[i].in_g_packet = 1;
5718 regs = alloca (rsa->sizeof_g_packet);
5720 /* Unimplemented registers read as all bits zero. */
5721 memset (regs, 0, rsa->sizeof_g_packet);
5723 /* Reply describes registers byte by byte, each byte encoded as two
5724 hex characters. Suck them all up, then supply them to the
5725 register cacheing/storage mechanism. */
5728 for (i = 0; i < rsa->sizeof_g_packet; i++)
5730 if (p[0] == 0 || p[1] == 0)
5731 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5732 internal_error (__FILE__, __LINE__,
5733 _("unexpected end of 'g' packet reply"));
5735 if (p[0] == 'x' && p[1] == 'x')
5736 regs[i] = 0; /* 'x' */
5738 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
5742 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
5744 struct packet_reg *r = &rsa->regs[i];
5748 if (r->offset * 2 >= strlen (rs->buf))
5749 /* This shouldn't happen - we adjusted in_g_packet above. */
5750 internal_error (__FILE__, __LINE__,
5751 _("unexpected end of 'g' packet reply"));
5752 else if (rs->buf[r->offset * 2] == 'x')
5754 gdb_assert (r->offset * 2 < strlen (rs->buf));
5755 /* The register isn't available, mark it as such (at
5756 the same time setting the value to zero). */
5757 regcache_raw_supply (regcache, r->regnum, NULL);
5760 regcache_raw_supply (regcache, r->regnum,
5767 fetch_registers_using_g (struct regcache *regcache)
5770 process_g_packet (regcache);
5774 remote_fetch_registers (struct target_ops *ops,
5775 struct regcache *regcache, int regnum)
5777 struct remote_arch_state *rsa = get_remote_arch_state ();
5780 set_general_thread (inferior_ptid);
5784 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
5786 gdb_assert (reg != NULL);
5788 /* If this register might be in the 'g' packet, try that first -
5789 we are likely to read more than one register. If this is the
5790 first 'g' packet, we might be overly optimistic about its
5791 contents, so fall back to 'p'. */
5792 if (reg->in_g_packet)
5794 fetch_registers_using_g (regcache);
5795 if (reg->in_g_packet)
5799 if (fetch_register_using_p (regcache, reg))
5802 /* This register is not available. */
5803 regcache_raw_supply (regcache, reg->regnum, NULL);
5808 fetch_registers_using_g (regcache);
5810 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
5811 if (!rsa->regs[i].in_g_packet)
5812 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
5814 /* This register is not available. */
5815 regcache_raw_supply (regcache, i, NULL);
5819 /* Prepare to store registers. Since we may send them all (using a
5820 'G' request), we have to read out the ones we don't want to change
5824 remote_prepare_to_store (struct regcache *regcache)
5826 struct remote_arch_state *rsa = get_remote_arch_state ();
5828 gdb_byte buf[MAX_REGISTER_SIZE];
5830 /* Make sure the entire registers array is valid. */
5831 switch (remote_protocol_packets[PACKET_P].support)
5833 case PACKET_DISABLE:
5834 case PACKET_SUPPORT_UNKNOWN:
5835 /* Make sure all the necessary registers are cached. */
5836 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
5837 if (rsa->regs[i].in_g_packet)
5838 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5845 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5846 packet was not recognized. */
5849 store_register_using_P (const struct regcache *regcache,
5850 struct packet_reg *reg)
5852 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5853 struct remote_state *rs = get_remote_state ();
5854 /* Try storing a single register. */
5855 char *buf = rs->buf;
5856 gdb_byte regp[MAX_REGISTER_SIZE];
5859 if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE)
5862 if (reg->pnum == -1)
5865 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5866 p = buf + strlen (buf);
5867 regcache_raw_collect (regcache, reg->regnum, regp);
5868 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
5870 getpkt (&rs->buf, &rs->buf_size, 0);
5872 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
5877 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
5878 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
5879 case PACKET_UNKNOWN:
5882 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
5886 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5887 contents of the register cache buffer. FIXME: ignores errors. */
5890 store_registers_using_G (const struct regcache *regcache)
5892 struct remote_state *rs = get_remote_state ();
5893 struct remote_arch_state *rsa = get_remote_arch_state ();
5897 /* Extract all the registers in the regcache copying them into a
5902 regs = alloca (rsa->sizeof_g_packet);
5903 memset (regs, 0, rsa->sizeof_g_packet);
5904 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
5906 struct packet_reg *r = &rsa->regs[i];
5909 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
5913 /* Command describes registers byte by byte,
5914 each byte encoded as two hex characters. */
5917 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5919 bin2hex (regs, p, rsa->sizeof_g_packet);
5921 getpkt (&rs->buf, &rs->buf_size, 0);
5922 if (packet_check_result (rs->buf) == PACKET_ERROR)
5923 error (_("Could not write registers; remote failure reply '%s'"),
5927 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5928 of the register cache buffer. FIXME: ignores errors. */
5931 remote_store_registers (struct target_ops *ops,
5932 struct regcache *regcache, int regnum)
5934 struct remote_arch_state *rsa = get_remote_arch_state ();
5937 set_general_thread (inferior_ptid);
5941 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
5943 gdb_assert (reg != NULL);
5945 /* Always prefer to store registers using the 'P' packet if
5946 possible; we often change only a small number of registers.
5947 Sometimes we change a larger number; we'd need help from a
5948 higher layer to know to use 'G'. */
5949 if (store_register_using_P (regcache, reg))
5952 /* For now, don't complain if we have no way to write the
5953 register. GDB loses track of unavailable registers too
5954 easily. Some day, this may be an error. We don't have
5955 any way to read the register, either... */
5956 if (!reg->in_g_packet)
5959 store_registers_using_G (regcache);
5963 store_registers_using_G (regcache);
5965 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
5966 if (!rsa->regs[i].in_g_packet)
5967 if (!store_register_using_P (regcache, &rsa->regs[i]))
5968 /* See above for why we do not issue an error here. */
5973 /* Return the number of hex digits in num. */
5976 hexnumlen (ULONGEST num)
5980 for (i = 0; num != 0; i++)
5986 /* Set BUF to the minimum number of hex digits representing NUM. */
5989 hexnumstr (char *buf, ULONGEST num)
5991 int len = hexnumlen (num);
5993 return hexnumnstr (buf, num, len);
5997 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6000 hexnumnstr (char *buf, ULONGEST num, int width)
6006 for (i = width - 1; i >= 0; i--)
6008 buf[i] = "0123456789abcdef"[(num & 0xf)];
6015 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6018 remote_address_masked (CORE_ADDR addr)
6020 int address_size = remote_address_size;
6022 /* If "remoteaddresssize" was not set, default to target address size. */
6024 address_size = gdbarch_addr_bit (target_gdbarch);
6026 if (address_size > 0
6027 && address_size < (sizeof (ULONGEST) * 8))
6029 /* Only create a mask when that mask can safely be constructed
6030 in a ULONGEST variable. */
6033 mask = (mask << address_size) - 1;
6039 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6040 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6041 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6042 (which may be more than *OUT_LEN due to escape characters). The
6043 total number of bytes in the output buffer will be at most
6047 remote_escape_output (const gdb_byte *buffer, int len,
6048 gdb_byte *out_buf, int *out_len,
6051 int input_index, output_index;
6054 for (input_index = 0; input_index < len; input_index++)
6056 gdb_byte b = buffer[input_index];
6058 if (b == '$' || b == '#' || b == '}')
6060 /* These must be escaped. */
6061 if (output_index + 2 > out_maxlen)
6063 out_buf[output_index++] = '}';
6064 out_buf[output_index++] = b ^ 0x20;
6068 if (output_index + 1 > out_maxlen)
6070 out_buf[output_index++] = b;
6074 *out_len = input_index;
6075 return output_index;
6078 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6079 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6080 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6082 This function reverses remote_escape_output. It allows more
6083 escaped characters than that function does, in particular because
6084 '*' must be escaped to avoid the run-length encoding processing
6085 in reading packets. */
6088 remote_unescape_input (const gdb_byte *buffer, int len,
6089 gdb_byte *out_buf, int out_maxlen)
6091 int input_index, output_index;
6096 for (input_index = 0; input_index < len; input_index++)
6098 gdb_byte b = buffer[input_index];
6100 if (output_index + 1 > out_maxlen)
6102 warning (_("Received too much data from remote target;"
6103 " ignoring overflow."));
6104 return output_index;
6109 out_buf[output_index++] = b ^ 0x20;
6115 out_buf[output_index++] = b;
6119 error (_("Unmatched escape character in target response."));
6121 return output_index;
6124 /* Determine whether the remote target supports binary downloading.
6125 This is accomplished by sending a no-op memory write of zero length
6126 to the target at the specified address. It does not suffice to send
6127 the whole packet, since many stubs strip the eighth bit and
6128 subsequently compute a wrong checksum, which causes real havoc with
6131 NOTE: This can still lose if the serial line is not eight-bit
6132 clean. In cases like this, the user should clear "remote
6136 check_binary_download (CORE_ADDR addr)
6138 struct remote_state *rs = get_remote_state ();
6140 switch (remote_protocol_packets[PACKET_X].support)
6142 case PACKET_DISABLE:
6146 case PACKET_SUPPORT_UNKNOWN:
6152 p += hexnumstr (p, (ULONGEST) addr);
6154 p += hexnumstr (p, (ULONGEST) 0);
6158 putpkt_binary (rs->buf, (int) (p - rs->buf));
6159 getpkt (&rs->buf, &rs->buf_size, 0);
6161 if (rs->buf[0] == '\0')
6164 fprintf_unfiltered (gdb_stdlog,
6165 "binary downloading NOT "
6166 "supported by target\n");
6167 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6172 fprintf_unfiltered (gdb_stdlog,
6173 "binary downloading suppported by target\n");
6174 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6181 /* Write memory data directly to the remote machine.
6182 This does not inform the data cache; the data cache uses this.
6183 HEADER is the starting part of the packet.
6184 MEMADDR is the address in the remote memory space.
6185 MYADDR is the address of the buffer in our space.
6186 LEN is the number of bytes.
6187 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6188 should send data as binary ('X'), or hex-encoded ('M').
6190 The function creates packet of the form
6191 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6193 where encoding of <DATA> is termined by PACKET_FORMAT.
6195 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6198 Returns the number of bytes transferred, or 0 (setting errno) for
6199 error. Only transfer a single packet. */
6202 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6203 const gdb_byte *myaddr, int len,
6204 char packet_format, int use_length)
6206 struct remote_state *rs = get_remote_state ();
6216 if (packet_format != 'X' && packet_format != 'M')
6217 internal_error (__FILE__, __LINE__,
6218 _("remote_write_bytes_aux: bad packet format"));
6223 payload_size = get_memory_write_packet_size ();
6225 /* The packet buffer will be large enough for the payload;
6226 get_memory_packet_size ensures this. */
6229 /* Compute the size of the actual payload by subtracting out the
6230 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6232 payload_size -= strlen ("$,:#NN");
6234 /* The comma won't be used. */
6236 header_length = strlen (header);
6237 payload_size -= header_length;
6238 payload_size -= hexnumlen (memaddr);
6240 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6242 strcat (rs->buf, header);
6243 p = rs->buf + strlen (header);
6245 /* Compute a best guess of the number of bytes actually transfered. */
6246 if (packet_format == 'X')
6248 /* Best guess at number of bytes that will fit. */
6249 todo = min (len, payload_size);
6251 payload_size -= hexnumlen (todo);
6252 todo = min (todo, payload_size);
6256 /* Num bytes that will fit. */
6257 todo = min (len, payload_size / 2);
6259 payload_size -= hexnumlen (todo);
6260 todo = min (todo, payload_size / 2);
6264 internal_error (__FILE__, __LINE__,
6265 _("minumum packet size too small to write data"));
6267 /* If we already need another packet, then try to align the end
6268 of this packet to a useful boundary. */
6269 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6270 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6272 /* Append "<memaddr>". */
6273 memaddr = remote_address_masked (memaddr);
6274 p += hexnumstr (p, (ULONGEST) memaddr);
6281 /* Append <len>. Retain the location/size of <len>. It may need to
6282 be adjusted once the packet body has been created. */
6284 plenlen = hexnumstr (p, (ULONGEST) todo);
6292 /* Append the packet body. */
6293 if (packet_format == 'X')
6295 /* Binary mode. Send target system values byte by byte, in
6296 increasing byte addresses. Only escape certain critical
6298 payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes,
6301 /* If not all TODO bytes fit, then we'll need another packet. Make
6302 a second try to keep the end of the packet aligned. Don't do
6303 this if the packet is tiny. */
6304 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6308 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6310 if (new_nr_bytes != nr_bytes)
6311 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6316 p += payload_length;
6317 if (use_length && nr_bytes < todo)
6319 /* Escape chars have filled up the buffer prematurely,
6320 and we have actually sent fewer bytes than planned.
6321 Fix-up the length field of the packet. Use the same
6322 number of characters as before. */
6323 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6324 *plen = ':'; /* overwrite \0 from hexnumnstr() */
6329 /* Normal mode: Send target system values byte by byte, in
6330 increasing byte addresses. Each byte is encoded as a two hex
6332 nr_bytes = bin2hex (myaddr, p, todo);
6336 putpkt_binary (rs->buf, (int) (p - rs->buf));
6337 getpkt (&rs->buf, &rs->buf_size, 0);
6339 if (rs->buf[0] == 'E')
6341 /* There is no correspondance between what the remote protocol
6342 uses for errors and errno codes. We would like a cleaner way
6343 of representing errors (big enough to include errno codes,
6344 bfd_error codes, and others). But for now just return EIO. */
6349 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6350 fewer bytes than we'd planned. */
6354 /* Write memory data directly to the remote machine.
6355 This does not inform the data cache; the data cache uses this.
6356 MEMADDR is the address in the remote memory space.
6357 MYADDR is the address of the buffer in our space.
6358 LEN is the number of bytes.
6360 Returns number of bytes transferred, or 0 (setting errno) for
6361 error. Only transfer a single packet. */
6364 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
6366 char *packet_format = 0;
6368 /* Check whether the target supports binary download. */
6369 check_binary_download (memaddr);
6371 switch (remote_protocol_packets[PACKET_X].support)
6374 packet_format = "X";
6376 case PACKET_DISABLE:
6377 packet_format = "M";
6379 case PACKET_SUPPORT_UNKNOWN:
6380 internal_error (__FILE__, __LINE__,
6381 _("remote_write_bytes: bad internal state"));
6383 internal_error (__FILE__, __LINE__, _("bad switch"));
6386 return remote_write_bytes_aux (packet_format,
6387 memaddr, myaddr, len, packet_format[0], 1);
6390 /* Read memory data directly from the remote machine.
6391 This does not use the data cache; the data cache uses this.
6392 MEMADDR is the address in the remote memory space.
6393 MYADDR is the address of the buffer in our space.
6394 LEN is the number of bytes.
6396 Returns number of bytes transferred, or 0 for error. */
6399 remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
6401 struct remote_state *rs = get_remote_state ();
6402 int max_buf_size; /* Max size of packet output buffer. */
6410 max_buf_size = get_memory_read_packet_size ();
6411 /* The packet buffer will be large enough for the payload;
6412 get_memory_packet_size ensures this. */
6414 /* Number if bytes that will fit. */
6415 todo = min (len, max_buf_size / 2);
6417 /* Construct "m"<memaddr>","<len>". */
6418 memaddr = remote_address_masked (memaddr);
6421 p += hexnumstr (p, (ULONGEST) memaddr);
6423 p += hexnumstr (p, (ULONGEST) todo);
6426 getpkt (&rs->buf, &rs->buf_size, 0);
6427 if (rs->buf[0] == 'E'
6428 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6429 && rs->buf[3] == '\0')
6431 /* There is no correspondance between what the remote protocol
6432 uses for errors and errno codes. We would like a cleaner way
6433 of representing errors (big enough to include errno codes,
6434 bfd_error codes, and others). But for now just return
6439 /* Reply describes memory byte by byte, each byte encoded as two hex
6442 i = hex2bin (p, myaddr, todo);
6443 /* Return what we have. Let higher layers handle partial reads. */
6448 /* Remote notification handler. */
6451 handle_notification (char *buf, size_t length)
6453 if (strncmp (buf, "Stop:", 5) == 0)
6455 if (pending_stop_reply)
6457 /* We've already parsed the in-flight stop-reply, but the
6458 stub for some reason thought we didn't, possibly due to
6459 timeout on its side. Just ignore it. */
6461 fprintf_unfiltered (gdb_stdlog, "ignoring resent notification\n");
6465 struct cleanup *old_chain;
6466 struct stop_reply *reply = stop_reply_xmalloc ();
6468 old_chain = make_cleanup (do_stop_reply_xfree, reply);
6470 remote_parse_stop_reply (buf + 5, reply);
6472 discard_cleanups (old_chain);
6474 /* Be careful to only set it after parsing, since an error
6475 may be thrown then. */
6476 pending_stop_reply = reply;
6478 /* Notify the event loop there's a stop reply to acknowledge
6479 and that there may be more events to fetch. */
6480 mark_async_event_handler (remote_async_get_pending_events_token);
6483 fprintf_unfiltered (gdb_stdlog, "stop notification captured\n");
6487 /* We ignore notifications we don't recognize, for compatibility
6488 with newer stubs. */
6493 /* Read or write LEN bytes from inferior memory at MEMADDR,
6494 transferring to or from debugger address BUFFER. Write to inferior
6495 if SHOULD_WRITE is nonzero. Returns length of data written or
6496 read; 0 for error. TARGET is unused. */
6499 remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
6500 int should_write, struct mem_attrib *attrib,
6501 struct target_ops *target)
6505 set_general_thread (inferior_ptid);
6508 res = remote_write_bytes (mem_addr, buffer, mem_len);
6510 res = remote_read_bytes (mem_addr, buffer, mem_len);
6515 /* Sends a packet with content determined by the printf format string
6516 FORMAT and the remaining arguments, then gets the reply. Returns
6517 whether the packet was a success, a failure, or unknown. */
6519 static enum packet_result
6520 remote_send_printf (const char *format, ...)
6522 struct remote_state *rs = get_remote_state ();
6523 int max_size = get_remote_packet_size ();
6526 va_start (ap, format);
6529 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
6530 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
6532 if (putpkt (rs->buf) < 0)
6533 error (_("Communication problem with target."));
6536 getpkt (&rs->buf, &rs->buf_size, 0);
6538 return packet_check_result (rs->buf);
6542 restore_remote_timeout (void *p)
6544 int value = *(int *)p;
6546 remote_timeout = value;
6549 /* Flash writing can take quite some time. We'll set
6550 effectively infinite timeout for flash operations.
6551 In future, we'll need to decide on a better approach. */
6552 static const int remote_flash_timeout = 1000;
6555 remote_flash_erase (struct target_ops *ops,
6556 ULONGEST address, LONGEST length)
6558 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
6559 int saved_remote_timeout = remote_timeout;
6560 enum packet_result ret;
6561 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6562 &saved_remote_timeout);
6564 remote_timeout = remote_flash_timeout;
6566 ret = remote_send_printf ("vFlashErase:%s,%s",
6567 phex (address, addr_size),
6571 case PACKET_UNKNOWN:
6572 error (_("Remote target does not support flash erase"));
6574 error (_("Error erasing flash with vFlashErase packet"));
6579 do_cleanups (back_to);
6583 remote_flash_write (struct target_ops *ops,
6584 ULONGEST address, LONGEST length,
6585 const gdb_byte *data)
6587 int saved_remote_timeout = remote_timeout;
6589 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6590 &saved_remote_timeout);
6592 remote_timeout = remote_flash_timeout;
6593 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0);
6594 do_cleanups (back_to);
6600 remote_flash_done (struct target_ops *ops)
6602 int saved_remote_timeout = remote_timeout;
6604 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6605 &saved_remote_timeout);
6607 remote_timeout = remote_flash_timeout;
6608 ret = remote_send_printf ("vFlashDone");
6609 do_cleanups (back_to);
6613 case PACKET_UNKNOWN:
6614 error (_("Remote target does not support vFlashDone"));
6616 error (_("Error finishing flash operation"));
6623 remote_files_info (struct target_ops *ignore)
6625 puts_filtered ("Debugging a target over a serial line.\n");
6628 /* Stuff for dealing with the packets which are part of this protocol.
6629 See comment at top of file for details. */
6631 /* Read a single character from the remote end. */
6634 readchar (int timeout)
6638 ch = serial_readchar (remote_desc, timeout);
6643 switch ((enum serial_rc) ch)
6647 error (_("Remote connection closed"));
6651 perror_with_name (_("Remote communication error. "
6652 "Target disconnected."));
6654 case SERIAL_TIMEOUT:
6660 /* Send the command in *BUF to the remote machine, and read the reply
6661 into *BUF. Report an error if we get an error reply. Resize
6662 *BUF using xrealloc if necessary to hold the result, and update
6666 remote_send (char **buf,
6670 getpkt (buf, sizeof_buf, 0);
6672 if ((*buf)[0] == 'E')
6673 error (_("Remote failure reply: %s"), *buf);
6676 /* Return a pointer to an xmalloc'ed string representing an escaped
6677 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6678 etc. The caller is responsible for releasing the returned
6682 escape_buffer (const char *buf, int n)
6684 struct cleanup *old_chain;
6685 struct ui_file *stb;
6688 stb = mem_fileopen ();
6689 old_chain = make_cleanup_ui_file_delete (stb);
6691 fputstrn_unfiltered (buf, n, 0, stb);
6692 str = ui_file_xstrdup (stb, NULL);
6693 do_cleanups (old_chain);
6697 /* Display a null-terminated packet on stdout, for debugging, using C
6701 print_packet (char *buf)
6703 puts_filtered ("\"");
6704 fputstr_filtered (buf, '"', gdb_stdout);
6705 puts_filtered ("\"");
6711 return putpkt_binary (buf, strlen (buf));
6714 /* Send a packet to the remote machine, with error checking. The data
6715 of the packet is in BUF. The string in BUF can be at most
6716 get_remote_packet_size () - 5 to account for the $, # and checksum,
6717 and for a possible /0 if we are debugging (remote_debug) and want
6718 to print the sent packet as a string. */
6721 putpkt_binary (char *buf, int cnt)
6723 struct remote_state *rs = get_remote_state ();
6725 unsigned char csum = 0;
6726 char *buf2 = alloca (cnt + 6);
6732 /* Catch cases like trying to read memory or listing threads while
6733 we're waiting for a stop reply. The remote server wouldn't be
6734 ready to handle this request, so we'd hang and timeout. We don't
6735 have to worry about this in synchronous mode, because in that
6736 case it's not possible to issue a command while the target is
6737 running. This is not a problem in non-stop mode, because in that
6738 case, the stub is always ready to process serial input. */
6739 if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
6740 error (_("Cannot execute this command while the target is running."));
6742 /* We're sending out a new packet. Make sure we don't look at a
6743 stale cached response. */
6744 rs->cached_wait_status = 0;
6746 /* Copy the packet into buffer BUF2, encapsulating it
6747 and giving it a checksum. */
6752 for (i = 0; i < cnt; i++)
6758 *p++ = tohex ((csum >> 4) & 0xf);
6759 *p++ = tohex (csum & 0xf);
6761 /* Send it over and over until we get a positive ack. */
6765 int started_error_output = 0;
6769 struct cleanup *old_chain;
6773 str = escape_buffer (buf2, p - buf2);
6774 old_chain = make_cleanup (xfree, str);
6775 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
6776 gdb_flush (gdb_stdlog);
6777 do_cleanups (old_chain);
6779 if (serial_write (remote_desc, buf2, p - buf2))
6780 perror_with_name (_("putpkt: write failed"));
6782 /* If this is a no acks version of the remote protocol, send the
6783 packet and move on. */
6787 /* Read until either a timeout occurs (-2) or '+' is read.
6788 Handle any notification that arrives in the mean time. */
6791 ch = readchar (remote_timeout);
6799 case SERIAL_TIMEOUT:
6802 if (started_error_output)
6804 putchar_unfiltered ('\n');
6805 started_error_output = 0;
6814 fprintf_unfiltered (gdb_stdlog, "Ack\n");
6818 fprintf_unfiltered (gdb_stdlog, "Nak\n");
6819 case SERIAL_TIMEOUT:
6823 break; /* Retransmit buffer. */
6827 fprintf_unfiltered (gdb_stdlog,
6828 "Packet instead of Ack, ignoring it\n");
6829 /* It's probably an old response sent because an ACK
6830 was lost. Gobble up the packet and ack it so it
6831 doesn't get retransmitted when we resend this
6834 serial_write (remote_desc, "+", 1);
6835 continue; /* Now, go look for +. */
6842 /* If we got a notification, handle it, and go back to looking
6844 /* We've found the start of a notification. Now
6845 collect the data. */
6846 val = read_frame (&rs->buf, &rs->buf_size);
6851 struct cleanup *old_chain;
6854 str = escape_buffer (rs->buf, val);
6855 old_chain = make_cleanup (xfree, str);
6856 fprintf_unfiltered (gdb_stdlog,
6857 " Notification received: %s\n",
6859 do_cleanups (old_chain);
6861 handle_notification (rs->buf, val);
6862 /* We're in sync now, rewait for the ack. */
6869 if (!started_error_output)
6871 started_error_output = 1;
6872 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
6874 fputc_unfiltered (ch & 0177, gdb_stdlog);
6875 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
6884 if (!started_error_output)
6886 started_error_output = 1;
6887 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
6889 fputc_unfiltered (ch & 0177, gdb_stdlog);
6893 break; /* Here to retransmit. */
6897 /* This is wrong. If doing a long backtrace, the user should be
6898 able to get out next time we call QUIT, without anything as
6899 violent as interrupt_query. If we want to provide a way out of
6900 here without getting to the next QUIT, it should be based on
6901 hitting ^C twice as in remote_wait. */
6912 /* Come here after finding the start of a frame when we expected an
6913 ack. Do our best to discard the rest of this packet. */
6922 c = readchar (remote_timeout);
6925 case SERIAL_TIMEOUT:
6926 /* Nothing we can do. */
6929 /* Discard the two bytes of checksum and stop. */
6930 c = readchar (remote_timeout);
6932 c = readchar (remote_timeout);
6935 case '*': /* Run length encoding. */
6936 /* Discard the repeat count. */
6937 c = readchar (remote_timeout);
6942 /* A regular character. */
6948 /* Come here after finding the start of the frame. Collect the rest
6949 into *BUF, verifying the checksum, length, and handling run-length
6950 compression. NUL terminate the buffer. If there is not enough room,
6951 expand *BUF using xrealloc.
6953 Returns -1 on error, number of characters in buffer (ignoring the
6954 trailing NULL) on success. (could be extended to return one of the
6955 SERIAL status indications). */
6958 read_frame (char **buf_p,
6965 struct remote_state *rs = get_remote_state ();
6972 c = readchar (remote_timeout);
6975 case SERIAL_TIMEOUT:
6977 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
6981 fputs_filtered ("Saw new packet start in middle of old one\n",
6983 return -1; /* Start a new packet, count retries. */
6986 unsigned char pktcsum;
6992 check_0 = readchar (remote_timeout);
6994 check_1 = readchar (remote_timeout);
6996 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
6999 fputs_filtered ("Timeout in checksum, retrying\n",
7003 else if (check_0 < 0 || check_1 < 0)
7006 fputs_filtered ("Communication error in checksum\n",
7011 /* Don't recompute the checksum; with no ack packets we
7012 don't have any way to indicate a packet retransmission
7017 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7018 if (csum == pktcsum)
7023 struct cleanup *old_chain;
7026 str = escape_buffer (buf, bc);
7027 old_chain = make_cleanup (xfree, str);
7028 fprintf_unfiltered (gdb_stdlog,
7029 "Bad checksum, sentsum=0x%x, "
7030 "csum=0x%x, buf=%s\n",
7031 pktcsum, csum, str);
7032 do_cleanups (old_chain);
7034 /* Number of characters in buffer ignoring trailing
7038 case '*': /* Run length encoding. */
7043 c = readchar (remote_timeout);
7045 repeat = c - ' ' + 3; /* Compute repeat count. */
7047 /* The character before ``*'' is repeated. */
7049 if (repeat > 0 && repeat <= 255 && bc > 0)
7051 if (bc + repeat - 1 >= *sizeof_buf - 1)
7053 /* Make some more room in the buffer. */
7054 *sizeof_buf += repeat;
7055 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7059 memset (&buf[bc], buf[bc - 1], repeat);
7065 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7069 if (bc >= *sizeof_buf - 1)
7071 /* Make some more room in the buffer. */
7073 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7084 /* Read a packet from the remote machine, with error checking, and
7085 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7086 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7087 rather than timing out; this is used (in synchronous mode) to wait
7088 for a target that is is executing user code to stop. */
7089 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7090 don't have to change all the calls to getpkt to deal with the
7091 return value, because at the moment I don't know what the right
7092 thing to do it for those. */
7100 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7104 /* Read a packet from the remote machine, with error checking, and
7105 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7106 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7107 rather than timing out; this is used (in synchronous mode) to wait
7108 for a target that is is executing user code to stop. If FOREVER ==
7109 0, this function is allowed to time out gracefully and return an
7110 indication of this to the caller. Otherwise return the number of
7111 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7112 enough reason to return to the caller. */
7115 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7116 int expecting_notif)
7118 struct remote_state *rs = get_remote_state ();
7124 /* We're reading a new response. Make sure we don't look at a
7125 previously cached response. */
7126 rs->cached_wait_status = 0;
7128 strcpy (*buf, "timeout");
7131 timeout = watchdog > 0 ? watchdog : -1;
7132 else if (expecting_notif)
7133 timeout = 0; /* There should already be a char in the buffer. If
7136 timeout = remote_timeout;
7140 /* Process any number of notifications, and then return when
7144 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7146 for (tries = 1; tries <= MAX_TRIES; tries++)
7148 /* This can loop forever if the remote side sends us
7149 characters continuously, but if it pauses, we'll get
7150 SERIAL_TIMEOUT from readchar because of timeout. Then
7151 we'll count that as a retry.
7153 Note that even when forever is set, we will only wait
7154 forever prior to the start of a packet. After that, we
7155 expect characters to arrive at a brisk pace. They should
7156 show up within remote_timeout intervals. */
7158 c = readchar (timeout);
7159 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
7161 if (c == SERIAL_TIMEOUT)
7163 if (expecting_notif)
7164 return -1; /* Don't complain, it's normal to not get
7165 anything in this case. */
7167 if (forever) /* Watchdog went off? Kill the target. */
7171 error (_("Watchdog timeout has expired. Target detached."));
7174 fputs_filtered ("Timed out.\n", gdb_stdlog);
7178 /* We've found the start of a packet or notification.
7179 Now collect the data. */
7180 val = read_frame (buf, sizeof_buf);
7185 serial_write (remote_desc, "-", 1);
7188 if (tries > MAX_TRIES)
7190 /* We have tried hard enough, and just can't receive the
7191 packet/notification. Give up. */
7192 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7194 /* Skip the ack char if we're in no-ack mode. */
7195 if (!rs->noack_mode)
7196 serial_write (remote_desc, "+", 1);
7200 /* If we got an ordinary packet, return that to our caller. */
7205 struct cleanup *old_chain;
7208 str = escape_buffer (*buf, val);
7209 old_chain = make_cleanup (xfree, str);
7210 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7211 do_cleanups (old_chain);
7214 /* Skip the ack char if we're in no-ack mode. */
7215 if (!rs->noack_mode)
7216 serial_write (remote_desc, "+", 1);
7220 /* If we got a notification, handle it, and go back to looking
7224 gdb_assert (c == '%');
7228 struct cleanup *old_chain;
7231 str = escape_buffer (*buf, val);
7232 old_chain = make_cleanup (xfree, str);
7233 fprintf_unfiltered (gdb_stdlog,
7234 " Notification received: %s\n",
7236 do_cleanups (old_chain);
7239 handle_notification (*buf, val);
7241 /* Notifications require no acknowledgement. */
7243 if (expecting_notif)
7250 getpkt_sane (char **buf, long *sizeof_buf, int forever)
7252 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0);
7256 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever)
7258 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1);
7263 remote_kill (struct target_ops *ops)
7265 /* Use catch_errors so the user can quit from gdb even when we
7266 aren't on speaking terms with the remote system. */
7267 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
7269 /* Don't wait for it to die. I'm not really sure it matters whether
7270 we do or not. For the existing stubs, kill is a noop. */
7271 target_mourn_inferior ();
7275 remote_vkill (int pid, struct remote_state *rs)
7277 if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7280 /* Tell the remote target to detach. */
7281 sprintf (rs->buf, "vKill;%x", pid);
7283 getpkt (&rs->buf, &rs->buf_size, 0);
7285 if (packet_ok (rs->buf,
7286 &remote_protocol_packets[PACKET_vKill]) == PACKET_OK)
7288 else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7295 extended_remote_kill (struct target_ops *ops)
7298 int pid = ptid_get_pid (inferior_ptid);
7299 struct remote_state *rs = get_remote_state ();
7301 res = remote_vkill (pid, rs);
7302 if (res == -1 && !remote_multi_process_p (rs))
7304 /* Don't try 'k' on a multi-process aware stub -- it has no way
7305 to specify the pid. */
7309 getpkt (&rs->buf, &rs->buf_size, 0);
7310 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7313 /* Don't wait for it to die. I'm not really sure it matters whether
7314 we do or not. For the existing stubs, kill is a noop. */
7320 error (_("Can't kill process"));
7322 target_mourn_inferior ();
7326 remote_mourn (struct target_ops *ops)
7328 remote_mourn_1 (ops);
7331 /* Worker function for remote_mourn. */
7333 remote_mourn_1 (struct target_ops *target)
7335 unpush_target (target);
7337 /* remote_close takes care of doing most of the clean up. */
7338 generic_mourn_inferior ();
7342 extended_remote_mourn_1 (struct target_ops *target)
7344 struct remote_state *rs = get_remote_state ();
7346 /* In case we got here due to an error, but we're going to stay
7348 rs->waiting_for_stop_reply = 0;
7350 /* We're no longer interested in these events. */
7351 discard_pending_stop_replies (ptid_get_pid (inferior_ptid));
7353 /* If the current general thread belonged to the process we just
7354 detached from or has exited, the remote side current general
7355 thread becomes undefined. Considering a case like this:
7357 - We just got here due to a detach.
7358 - The process that we're detaching from happens to immediately
7359 report a global breakpoint being hit in non-stop mode, in the
7360 same thread we had selected before.
7361 - GDB attaches to this process again.
7362 - This event happens to be the next event we handle.
7364 GDB would consider that the current general thread didn't need to
7365 be set on the stub side (with Hg), since for all it knew,
7366 GENERAL_THREAD hadn't changed.
7368 Notice that although in all-stop mode, the remote server always
7369 sets the current thread to the thread reporting the stop event,
7370 that doesn't happen in non-stop mode; in non-stop, the stub *must
7371 not* change the current thread when reporting a breakpoint hit,
7372 due to the decoupling of event reporting and event handling.
7374 To keep things simple, we always invalidate our notion of the
7376 record_currthread (minus_one_ptid);
7378 /* Unlike "target remote", we do not want to unpush the target; then
7379 the next time the user says "run", we won't be connected. */
7381 /* Call common code to mark the inferior as not running. */
7382 generic_mourn_inferior ();
7384 if (!have_inferiors ())
7386 if (!remote_multi_process_p (rs))
7388 /* Check whether the target is running now - some remote stubs
7389 automatically restart after kill. */
7391 getpkt (&rs->buf, &rs->buf_size, 0);
7393 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7395 /* Assume that the target has been restarted. Set
7396 inferior_ptid so that bits of core GDB realizes
7397 there's something here, e.g., so that the user can
7398 say "kill" again. */
7399 inferior_ptid = magic_null_ptid;
7406 extended_remote_mourn (struct target_ops *ops)
7408 extended_remote_mourn_1 (ops);
7412 extended_remote_run (char *args)
7414 struct remote_state *rs = get_remote_state ();
7417 /* If the user has disabled vRun support, or we have detected that
7418 support is not available, do not try it. */
7419 if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7422 strcpy (rs->buf, "vRun;");
7423 len = strlen (rs->buf);
7425 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7426 error (_("Remote file name too long for run packet"));
7427 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0);
7429 gdb_assert (args != NULL);
7432 struct cleanup *back_to;
7436 argv = gdb_buildargv (args);
7437 back_to = make_cleanup ((void (*) (void *)) freeargv, argv);
7438 for (i = 0; argv[i] != NULL; i++)
7440 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
7441 error (_("Argument list too long for run packet"));
7442 rs->buf[len++] = ';';
7443 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0);
7445 do_cleanups (back_to);
7448 rs->buf[len++] = '\0';
7451 getpkt (&rs->buf, &rs->buf_size, 0);
7453 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK)
7455 /* We have a wait response; we don't need it, though. All is well. */
7458 else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7459 /* It wasn't disabled before, but it is now. */
7463 if (remote_exec_file[0] == '\0')
7464 error (_("Running the default executable on the remote target failed; "
7465 "try \"set remote exec-file\"?"));
7467 error (_("Running \"%s\" on the remote target failed"),
7472 /* In the extended protocol we want to be able to do things like
7473 "run" and have them basically work as expected. So we need
7474 a special create_inferior function. We support changing the
7475 executable file and the command line arguments, but not the
7479 extended_remote_create_inferior_1 (char *exec_file, char *args,
7480 char **env, int from_tty)
7482 /* If running asynchronously, register the target file descriptor
7483 with the event loop. */
7484 if (target_can_async_p ())
7485 target_async (inferior_event_handler, 0);
7487 /* Now restart the remote server. */
7488 if (extended_remote_run (args) == -1)
7490 /* vRun was not supported. Fail if we need it to do what the
7492 if (remote_exec_file[0])
7493 error (_("Remote target does not support \"set remote exec-file\""));
7495 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7497 /* Fall back to "R". */
7498 extended_remote_restart ();
7501 if (!have_inferiors ())
7503 /* Clean up from the last time we ran, before we mark the target
7504 running again. This will mark breakpoints uninserted, and
7505 get_offsets may insert breakpoints. */
7506 init_thread_list ();
7507 init_wait_for_inferior ();
7510 /* Now mark the inferior as running before we do anything else. */
7511 inferior_ptid = magic_null_ptid;
7513 /* Now, if we have thread information, update inferior_ptid. */
7514 inferior_ptid = remote_current_thread (inferior_ptid);
7516 remote_add_inferior (ptid_get_pid (inferior_ptid), 0);
7517 add_thread_silent (inferior_ptid);
7519 /* Get updated offsets, if the stub uses qOffsets. */
7524 extended_remote_create_inferior (struct target_ops *ops,
7525 char *exec_file, char *args,
7526 char **env, int from_tty)
7528 extended_remote_create_inferior_1 (exec_file, args, env, from_tty);
7532 /* Insert a breakpoint. On targets that have software breakpoint
7533 support, we ask the remote target to do the work; on targets
7534 which don't, we insert a traditional memory breakpoint. */
7537 remote_insert_breakpoint (struct gdbarch *gdbarch,
7538 struct bp_target_info *bp_tgt)
7540 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7541 If it succeeds, then set the support to PACKET_ENABLE. If it
7542 fails, and the user has explicitly requested the Z support then
7543 report an error, otherwise, mark it disabled and go on. */
7545 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
7547 CORE_ADDR addr = bp_tgt->placed_address;
7548 struct remote_state *rs;
7552 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
7554 rs = get_remote_state ();
7560 addr = (ULONGEST) remote_address_masked (addr);
7561 p += hexnumstr (p, addr);
7562 sprintf (p, ",%d", bpsize);
7565 getpkt (&rs->buf, &rs->buf_size, 0);
7567 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
7572 bp_tgt->placed_address = addr;
7573 bp_tgt->placed_size = bpsize;
7575 case PACKET_UNKNOWN:
7580 return memory_insert_breakpoint (gdbarch, bp_tgt);
7584 remote_remove_breakpoint (struct gdbarch *gdbarch,
7585 struct bp_target_info *bp_tgt)
7587 CORE_ADDR addr = bp_tgt->placed_address;
7588 struct remote_state *rs = get_remote_state ();
7590 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
7598 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
7599 p += hexnumstr (p, addr);
7600 sprintf (p, ",%d", bp_tgt->placed_size);
7603 getpkt (&rs->buf, &rs->buf_size, 0);
7605 return (rs->buf[0] == 'E');
7608 return memory_remove_breakpoint (gdbarch, bp_tgt);
7612 watchpoint_to_Z_packet (int type)
7617 return Z_PACKET_WRITE_WP;
7620 return Z_PACKET_READ_WP;
7623 return Z_PACKET_ACCESS_WP;
7626 internal_error (__FILE__, __LINE__,
7627 _("hw_bp_to_z: bad watchpoint type %d"), type);
7632 remote_insert_watchpoint (CORE_ADDR addr, int len, int type,
7633 struct expression *cond)
7635 struct remote_state *rs = get_remote_state ();
7637 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
7639 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
7642 sprintf (rs->buf, "Z%x,", packet);
7643 p = strchr (rs->buf, '\0');
7644 addr = remote_address_masked (addr);
7645 p += hexnumstr (p, (ULONGEST) addr);
7646 sprintf (p, ",%x", len);
7649 getpkt (&rs->buf, &rs->buf_size, 0);
7651 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
7655 case PACKET_UNKNOWN:
7660 internal_error (__FILE__, __LINE__,
7661 _("remote_insert_watchpoint: reached end of function"));
7666 remote_remove_watchpoint (CORE_ADDR addr, int len, int type,
7667 struct expression *cond)
7669 struct remote_state *rs = get_remote_state ();
7671 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
7673 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
7676 sprintf (rs->buf, "z%x,", packet);
7677 p = strchr (rs->buf, '\0');
7678 addr = remote_address_masked (addr);
7679 p += hexnumstr (p, (ULONGEST) addr);
7680 sprintf (p, ",%x", len);
7682 getpkt (&rs->buf, &rs->buf_size, 0);
7684 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
7687 case PACKET_UNKNOWN:
7692 internal_error (__FILE__, __LINE__,
7693 _("remote_remove_watchpoint: reached end of function"));
7697 int remote_hw_watchpoint_limit = -1;
7698 int remote_hw_breakpoint_limit = -1;
7701 remote_check_watch_resources (int type, int cnt, int ot)
7703 if (type == bp_hardware_breakpoint)
7705 if (remote_hw_breakpoint_limit == 0)
7707 else if (remote_hw_breakpoint_limit < 0)
7709 else if (cnt <= remote_hw_breakpoint_limit)
7714 if (remote_hw_watchpoint_limit == 0)
7716 else if (remote_hw_watchpoint_limit < 0)
7720 else if (cnt <= remote_hw_watchpoint_limit)
7727 remote_stopped_by_watchpoint (void)
7729 return remote_stopped_by_watchpoint_p;
7733 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
7737 if (remote_stopped_by_watchpoint ())
7739 *addr_p = remote_watch_data_address;
7748 remote_insert_hw_breakpoint (struct gdbarch *gdbarch,
7749 struct bp_target_info *bp_tgt)
7752 struct remote_state *rs;
7755 /* The length field should be set to the size of a breakpoint
7756 instruction, even though we aren't inserting one ourselves. */
7758 gdbarch_remote_breakpoint_from_pc
7759 (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
7761 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
7764 rs = get_remote_state ();
7771 addr = remote_address_masked (bp_tgt->placed_address);
7772 p += hexnumstr (p, (ULONGEST) addr);
7773 sprintf (p, ",%x", bp_tgt->placed_size);
7776 getpkt (&rs->buf, &rs->buf_size, 0);
7778 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
7781 case PACKET_UNKNOWN:
7786 internal_error (__FILE__, __LINE__,
7787 _("remote_insert_hw_breakpoint: reached end of function"));
7792 remote_remove_hw_breakpoint (struct gdbarch *gdbarch,
7793 struct bp_target_info *bp_tgt)
7796 struct remote_state *rs = get_remote_state ();
7799 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
7806 addr = remote_address_masked (bp_tgt->placed_address);
7807 p += hexnumstr (p, (ULONGEST) addr);
7808 sprintf (p, ",%x", bp_tgt->placed_size);
7811 getpkt (&rs->buf, &rs->buf_size, 0);
7813 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
7816 case PACKET_UNKNOWN:
7821 internal_error (__FILE__, __LINE__,
7822 _("remote_remove_hw_breakpoint: reached end of function"));
7825 /* Table used by the crc32 function to calcuate the checksum. */
7827 static unsigned long crc32_table[256] =
7830 static unsigned long
7831 crc32 (const unsigned char *buf, int len, unsigned int crc)
7833 if (!crc32_table[1])
7835 /* Initialize the CRC table and the decoding table. */
7839 for (i = 0; i < 256; i++)
7841 for (c = i << 24, j = 8; j > 0; --j)
7842 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
7849 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
7855 /* Verify memory using the "qCRC:" request. */
7858 remote_verify_memory (struct target_ops *ops,
7859 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
7861 struct remote_state *rs = get_remote_state ();
7862 unsigned long host_crc, target_crc;
7865 /* FIXME: assumes lma can fit into long. */
7866 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
7867 (long) lma, (long) size);
7870 /* Be clever; compute the host_crc before waiting for target
7872 host_crc = crc32 (data, size, 0xffffffff);
7874 getpkt (&rs->buf, &rs->buf_size, 0);
7875 if (rs->buf[0] == 'E')
7878 if (rs->buf[0] != 'C')
7879 error (_("remote target does not support this operation"));
7881 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
7882 target_crc = target_crc * 16 + fromhex (*tmp);
7884 return (host_crc == target_crc);
7887 /* compare-sections command
7889 With no arguments, compares each loadable section in the exec bfd
7890 with the same memory range on the target, and reports mismatches.
7891 Useful for verifying the image on the target against the exec file. */
7894 compare_sections_command (char *args, int from_tty)
7897 struct cleanup *old_chain;
7899 const char *sectname;
7907 error (_("command cannot be used without an exec file"));
7909 for (s = exec_bfd->sections; s; s = s->next)
7911 if (!(s->flags & SEC_LOAD))
7912 continue; /* Skip non-loadable section. */
7914 size = bfd_get_section_size (s);
7916 continue; /* Skip zero-length section. */
7918 sectname = bfd_get_section_name (exec_bfd, s);
7919 if (args && strcmp (args, sectname) != 0)
7920 continue; /* Not the section selected by user. */
7922 matched = 1; /* Do this section. */
7925 sectdata = xmalloc (size);
7926 old_chain = make_cleanup (xfree, sectdata);
7927 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
7929 res = target_verify_memory (sectdata, lma, size);
7932 error (_("target memory fault, section %s, range %s -- %s"), sectname,
7933 paddress (target_gdbarch, lma),
7934 paddress (target_gdbarch, lma + size));
7936 printf_filtered ("Section %s, range %s -- %s: ", sectname,
7937 paddress (target_gdbarch, lma),
7938 paddress (target_gdbarch, lma + size));
7940 printf_filtered ("matched.\n");
7943 printf_filtered ("MIS-MATCHED!\n");
7947 do_cleanups (old_chain);
7950 warning (_("One or more sections of the remote executable does not match\n\
7951 the loaded file\n"));
7952 if (args && !matched)
7953 printf_filtered (_("No loaded section named '%s'.\n"), args);
7956 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7957 into remote target. The number of bytes written to the remote
7958 target is returned, or -1 for error. */
7961 remote_write_qxfer (struct target_ops *ops, const char *object_name,
7962 const char *annex, const gdb_byte *writebuf,
7963 ULONGEST offset, LONGEST len,
7964 struct packet_config *packet)
7968 struct remote_state *rs = get_remote_state ();
7969 int max_size = get_memory_write_packet_size ();
7971 if (packet->support == PACKET_DISABLE)
7974 /* Insert header. */
7975 i = snprintf (rs->buf, max_size,
7976 "qXfer:%s:write:%s:%s:",
7977 object_name, annex ? annex : "",
7978 phex_nz (offset, sizeof offset));
7979 max_size -= (i + 1);
7981 /* Escape as much data as fits into rs->buf. */
7982 buf_len = remote_escape_output
7983 (writebuf, len, (rs->buf + i), &max_size, max_size);
7985 if (putpkt_binary (rs->buf, i + buf_len) < 0
7986 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
7987 || packet_ok (rs->buf, packet) != PACKET_OK)
7990 unpack_varlen_hex (rs->buf, &n);
7994 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7995 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7996 number of bytes read is returned, or 0 for EOF, or -1 for error.
7997 The number of bytes read may be less than LEN without indicating an
7998 EOF. PACKET is checked and updated to indicate whether the remote
7999 target supports this object. */
8002 remote_read_qxfer (struct target_ops *ops, const char *object_name,
8004 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8005 struct packet_config *packet)
8007 static char *finished_object;
8008 static char *finished_annex;
8009 static ULONGEST finished_offset;
8011 struct remote_state *rs = get_remote_state ();
8012 LONGEST i, n, packet_len;
8014 if (packet->support == PACKET_DISABLE)
8017 /* Check whether we've cached an end-of-object packet that matches
8019 if (finished_object)
8021 if (strcmp (object_name, finished_object) == 0
8022 && strcmp (annex ? annex : "", finished_annex) == 0
8023 && offset == finished_offset)
8026 /* Otherwise, we're now reading something different. Discard
8028 xfree (finished_object);
8029 xfree (finished_annex);
8030 finished_object = NULL;
8031 finished_annex = NULL;
8034 /* Request only enough to fit in a single packet. The actual data
8035 may not, since we don't know how much of it will need to be escaped;
8036 the target is free to respond with slightly less data. We subtract
8037 five to account for the response type and the protocol frame. */
8038 n = min (get_remote_packet_size () - 5, len);
8039 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8040 object_name, annex ? annex : "",
8041 phex_nz (offset, sizeof offset),
8042 phex_nz (n, sizeof n));
8043 i = putpkt (rs->buf);
8048 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8049 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8052 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8053 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8055 /* 'm' means there is (or at least might be) more data after this
8056 batch. That does not make sense unless there's at least one byte
8057 of data in this reply. */
8058 if (rs->buf[0] == 'm' && packet_len == 1)
8059 error (_("Remote qXfer reply contained no data."));
8061 /* Got some data. */
8062 i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
8064 /* 'l' is an EOF marker, possibly including a final block of data,
8065 or possibly empty. If we have the final block of a non-empty
8066 object, record this fact to bypass a subsequent partial read. */
8067 if (rs->buf[0] == 'l' && offset + i > 0)
8069 finished_object = xstrdup (object_name);
8070 finished_annex = xstrdup (annex ? annex : "");
8071 finished_offset = offset + i;
8078 remote_xfer_partial (struct target_ops *ops, enum target_object object,
8079 const char *annex, gdb_byte *readbuf,
8080 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
8082 struct remote_state *rs;
8087 set_general_thread (inferior_ptid);
8089 rs = get_remote_state ();
8091 /* Handle memory using the standard memory routines. */
8092 if (object == TARGET_OBJECT_MEMORY)
8098 /* If the remote target is connected but not running, we should
8099 pass this request down to a lower stratum (e.g. the executable
8101 if (!target_has_execution)
8104 if (writebuf != NULL)
8105 xfered = remote_write_bytes (offset, writebuf, len);
8107 xfered = remote_read_bytes (offset, readbuf, len);
8111 else if (xfered == 0 && errno == 0)
8117 /* Handle SPU memory using qxfer packets. */
8118 if (object == TARGET_OBJECT_SPU)
8121 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8122 &remote_protocol_packets
8123 [PACKET_qXfer_spu_read]);
8125 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8126 &remote_protocol_packets
8127 [PACKET_qXfer_spu_write]);
8130 /* Handle extra signal info using qxfer packets. */
8131 if (object == TARGET_OBJECT_SIGNAL_INFO)
8134 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8135 &remote_protocol_packets
8136 [PACKET_qXfer_siginfo_read]);
8138 return remote_write_qxfer (ops, "siginfo", annex,
8139 writebuf, offset, len,
8140 &remote_protocol_packets
8141 [PACKET_qXfer_siginfo_write]);
8144 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8147 return remote_read_qxfer (ops, "statictrace", annex,
8148 readbuf, offset, len,
8149 &remote_protocol_packets
8150 [PACKET_qXfer_statictrace_read]);
8155 /* Only handle flash writes. */
8156 if (writebuf != NULL)
8162 case TARGET_OBJECT_FLASH:
8163 xfered = remote_flash_write (ops, offset, len, writebuf);
8167 else if (xfered == 0 && errno == 0)
8177 /* Map pre-existing objects onto letters. DO NOT do this for new
8178 objects!!! Instead specify new query packets. */
8181 case TARGET_OBJECT_AVR:
8185 case TARGET_OBJECT_AUXV:
8186 gdb_assert (annex == NULL);
8187 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8188 &remote_protocol_packets[PACKET_qXfer_auxv]);
8190 case TARGET_OBJECT_AVAILABLE_FEATURES:
8191 return remote_read_qxfer
8192 (ops, "features", annex, readbuf, offset, len,
8193 &remote_protocol_packets[PACKET_qXfer_features]);
8195 case TARGET_OBJECT_LIBRARIES:
8196 return remote_read_qxfer
8197 (ops, "libraries", annex, readbuf, offset, len,
8198 &remote_protocol_packets[PACKET_qXfer_libraries]);
8200 case TARGET_OBJECT_MEMORY_MAP:
8201 gdb_assert (annex == NULL);
8202 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8203 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8205 case TARGET_OBJECT_OSDATA:
8206 /* Should only get here if we're connected. */
8207 gdb_assert (remote_desc);
8208 return remote_read_qxfer
8209 (ops, "osdata", annex, readbuf, offset, len,
8210 &remote_protocol_packets[PACKET_qXfer_osdata]);
8212 case TARGET_OBJECT_THREADS:
8213 gdb_assert (annex == NULL);
8214 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8215 &remote_protocol_packets[PACKET_qXfer_threads]);
8217 case TARGET_OBJECT_TRACEFRAME_INFO:
8218 gdb_assert (annex == NULL);
8219 return remote_read_qxfer
8220 (ops, "traceframe-info", annex, readbuf, offset, len,
8221 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8226 /* Note: a zero OFFSET and LEN can be used to query the minimum
8228 if (offset == 0 && len == 0)
8229 return (get_remote_packet_size ());
8230 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8231 large enough let the caller deal with it. */
8232 if (len < get_remote_packet_size ())
8234 len = get_remote_packet_size ();
8236 /* Except for querying the minimum buffer size, target must be open. */
8238 error (_("remote query is only available after target open"));
8240 gdb_assert (annex != NULL);
8241 gdb_assert (readbuf != NULL);
8247 /* We used one buffer char for the remote protocol q command and
8248 another for the query type. As the remote protocol encapsulation
8249 uses 4 chars plus one extra in case we are debugging
8250 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8253 while (annex[i] && (i < (get_remote_packet_size () - 8)))
8255 /* Bad caller may have sent forbidden characters. */
8256 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
8261 gdb_assert (annex[i] == '\0');
8263 i = putpkt (rs->buf);
8267 getpkt (&rs->buf, &rs->buf_size, 0);
8268 strcpy ((char *) readbuf, rs->buf);
8270 return strlen ((char *) readbuf);
8274 remote_search_memory (struct target_ops* ops,
8275 CORE_ADDR start_addr, ULONGEST search_space_len,
8276 const gdb_byte *pattern, ULONGEST pattern_len,
8277 CORE_ADDR *found_addrp)
8279 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
8280 struct remote_state *rs = get_remote_state ();
8281 int max_size = get_memory_write_packet_size ();
8282 struct packet_config *packet =
8283 &remote_protocol_packets[PACKET_qSearch_memory];
8284 /* Number of packet bytes used to encode the pattern;
8285 this could be more than PATTERN_LEN due to escape characters. */
8286 int escaped_pattern_len;
8287 /* Amount of pattern that was encodable in the packet. */
8288 int used_pattern_len;
8291 ULONGEST found_addr;
8293 /* Don't go to the target if we don't have to.
8294 This is done before checking packet->support to avoid the possibility that
8295 a success for this edge case means the facility works in general. */
8296 if (pattern_len > search_space_len)
8298 if (pattern_len == 0)
8300 *found_addrp = start_addr;
8304 /* If we already know the packet isn't supported, fall back to the simple
8305 way of searching memory. */
8307 if (packet->support == PACKET_DISABLE)
8309 /* Target doesn't provided special support, fall back and use the
8310 standard support (copy memory and do the search here). */
8311 return simple_search_memory (ops, start_addr, search_space_len,
8312 pattern, pattern_len, found_addrp);
8315 /* Insert header. */
8316 i = snprintf (rs->buf, max_size,
8317 "qSearch:memory:%s;%s;",
8318 phex_nz (start_addr, addr_size),
8319 phex_nz (search_space_len, sizeof (search_space_len)));
8320 max_size -= (i + 1);
8322 /* Escape as much data as fits into rs->buf. */
8323 escaped_pattern_len =
8324 remote_escape_output (pattern, pattern_len, (rs->buf + i),
8325 &used_pattern_len, max_size);
8327 /* Bail if the pattern is too large. */
8328 if (used_pattern_len != pattern_len)
8329 error (_("Pattern is too large to transmit to remote target."));
8331 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
8332 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8333 || packet_ok (rs->buf, packet) != PACKET_OK)
8335 /* The request may not have worked because the command is not
8336 supported. If so, fall back to the simple way. */
8337 if (packet->support == PACKET_DISABLE)
8339 return simple_search_memory (ops, start_addr, search_space_len,
8340 pattern, pattern_len, found_addrp);
8345 if (rs->buf[0] == '0')
8347 else if (rs->buf[0] == '1')
8350 if (rs->buf[1] != ',')
8351 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8352 unpack_varlen_hex (rs->buf + 2, &found_addr);
8353 *found_addrp = found_addr;
8356 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8362 remote_rcmd (char *command,
8363 struct ui_file *outbuf)
8365 struct remote_state *rs = get_remote_state ();
8369 error (_("remote rcmd is only available after target open"));
8371 /* Send a NULL command across as an empty command. */
8372 if (command == NULL)
8375 /* The query prefix. */
8376 strcpy (rs->buf, "qRcmd,");
8377 p = strchr (rs->buf, '\0');
8379 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
8380 > get_remote_packet_size ())
8381 error (_("\"monitor\" command ``%s'' is too long."), command);
8383 /* Encode the actual command. */
8384 bin2hex ((gdb_byte *) command, p, 0);
8386 if (putpkt (rs->buf) < 0)
8387 error (_("Communication problem with target."));
8389 /* get/display the response */
8394 /* XXX - see also remote_get_noisy_reply(). */
8396 getpkt (&rs->buf, &rs->buf_size, 0);
8399 error (_("Target does not support this command."));
8400 if (buf[0] == 'O' && buf[1] != 'K')
8402 remote_console_output (buf + 1); /* 'O' message from stub. */
8405 if (strcmp (buf, "OK") == 0)
8407 if (strlen (buf) == 3 && buf[0] == 'E'
8408 && isdigit (buf[1]) && isdigit (buf[2]))
8410 error (_("Protocol error with Rcmd"));
8412 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
8414 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
8416 fputc_unfiltered (c, outbuf);
8422 static VEC(mem_region_s) *
8423 remote_memory_map (struct target_ops *ops)
8425 VEC(mem_region_s) *result = NULL;
8426 char *text = target_read_stralloc (¤t_target,
8427 TARGET_OBJECT_MEMORY_MAP, NULL);
8431 struct cleanup *back_to = make_cleanup (xfree, text);
8433 result = parse_memory_map (text);
8434 do_cleanups (back_to);
8441 packet_command (char *args, int from_tty)
8443 struct remote_state *rs = get_remote_state ();
8446 error (_("command can only be used with remote target"));
8449 error (_("remote-packet command requires packet text as argument"));
8451 puts_filtered ("sending: ");
8452 print_packet (args);
8453 puts_filtered ("\n");
8456 getpkt (&rs->buf, &rs->buf_size, 0);
8457 puts_filtered ("received: ");
8458 print_packet (rs->buf);
8459 puts_filtered ("\n");
8463 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8465 static void display_thread_info (struct gdb_ext_thread_info *info);
8467 static void threadset_test_cmd (char *cmd, int tty);
8469 static void threadalive_test (char *cmd, int tty);
8471 static void threadlist_test_cmd (char *cmd, int tty);
8473 int get_and_display_threadinfo (threadref *ref);
8475 static void threadinfo_test_cmd (char *cmd, int tty);
8477 static int thread_display_step (threadref *ref, void *context);
8479 static void threadlist_update_test_cmd (char *cmd, int tty);
8481 static void init_remote_threadtests (void);
8483 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8486 threadset_test_cmd (char *cmd, int tty)
8488 int sample_thread = SAMPLE_THREAD;
8490 printf_filtered (_("Remote threadset test\n"));
8491 set_general_thread (sample_thread);
8496 threadalive_test (char *cmd, int tty)
8498 int sample_thread = SAMPLE_THREAD;
8499 int pid = ptid_get_pid (inferior_ptid);
8500 ptid_t ptid = ptid_build (pid, 0, sample_thread);
8502 if (remote_thread_alive (ptid))
8503 printf_filtered ("PASS: Thread alive test\n");
8505 printf_filtered ("FAIL: Thread alive test\n");
8508 void output_threadid (char *title, threadref *ref);
8511 output_threadid (char *title, threadref *ref)
8515 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
8517 printf_filtered ("%s %s\n", title, (&hexid[0]));
8521 threadlist_test_cmd (char *cmd, int tty)
8524 threadref nextthread;
8525 int done, result_count;
8526 threadref threadlist[3];
8528 printf_filtered ("Remote Threadlist test\n");
8529 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
8530 &result_count, &threadlist[0]))
8531 printf_filtered ("FAIL: threadlist test\n");
8534 threadref *scan = threadlist;
8535 threadref *limit = scan + result_count;
8537 while (scan < limit)
8538 output_threadid (" thread ", scan++);
8543 display_thread_info (struct gdb_ext_thread_info *info)
8545 output_threadid ("Threadid: ", &info->threadid);
8546 printf_filtered ("Name: %s\n ", info->shortname);
8547 printf_filtered ("State: %s\n", info->display);
8548 printf_filtered ("other: %s\n\n", info->more_display);
8552 get_and_display_threadinfo (threadref *ref)
8556 struct gdb_ext_thread_info threadinfo;
8558 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
8559 | TAG_MOREDISPLAY | TAG_DISPLAY;
8560 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
8561 display_thread_info (&threadinfo);
8566 threadinfo_test_cmd (char *cmd, int tty)
8568 int athread = SAMPLE_THREAD;
8572 int_to_threadref (&thread, athread);
8573 printf_filtered ("Remote Threadinfo test\n");
8574 if (!get_and_display_threadinfo (&thread))
8575 printf_filtered ("FAIL cannot get thread info\n");
8579 thread_display_step (threadref *ref, void *context)
8581 /* output_threadid(" threadstep ",ref); *//* simple test */
8582 return get_and_display_threadinfo (ref);
8586 threadlist_update_test_cmd (char *cmd, int tty)
8588 printf_filtered ("Remote Threadlist update test\n");
8589 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
8593 init_remote_threadtests (void)
8595 add_com ("tlist", class_obscure, threadlist_test_cmd,
8596 _("Fetch and print the remote list of "
8597 "thread identifiers, one pkt only"));
8598 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
8599 _("Fetch and display info about one thread"));
8600 add_com ("tset", class_obscure, threadset_test_cmd,
8601 _("Test setting to a different thread"));
8602 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
8603 _("Iterate through updating all remote thread info"));
8604 add_com ("talive", class_obscure, threadalive_test,
8605 _(" Remote thread alive test "));
8610 /* Convert a thread ID to a string. Returns the string in a static
8614 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
8616 static char buf[64];
8617 struct remote_state *rs = get_remote_state ();
8619 if (ptid_is_pid (ptid))
8621 /* Printing an inferior target id. */
8623 /* When multi-process extensions are off, there's no way in the
8624 remote protocol to know the remote process id, if there's any
8625 at all. There's one exception --- when we're connected with
8626 target extended-remote, and we manually attached to a process
8627 with "attach PID". We don't record anywhere a flag that
8628 allows us to distinguish that case from the case of
8629 connecting with extended-remote and the stub already being
8630 attached to a process, and reporting yes to qAttached, hence
8631 no smart special casing here. */
8632 if (!remote_multi_process_p (rs))
8634 xsnprintf (buf, sizeof buf, "Remote target");
8638 return normal_pid_to_str (ptid);
8642 if (ptid_equal (magic_null_ptid, ptid))
8643 xsnprintf (buf, sizeof buf, "Thread <main>");
8644 else if (remote_multi_process_p (rs))
8645 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
8646 ptid_get_pid (ptid), ptid_get_tid (ptid));
8648 xsnprintf (buf, sizeof buf, "Thread %ld",
8649 ptid_get_tid (ptid));
8654 /* Get the address of the thread local variable in OBJFILE which is
8655 stored at OFFSET within the thread local storage for thread PTID. */
8658 remote_get_thread_local_address (struct target_ops *ops,
8659 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
8661 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
8663 struct remote_state *rs = get_remote_state ();
8665 char *endp = rs->buf + get_remote_packet_size ();
8666 enum packet_result result;
8668 strcpy (p, "qGetTLSAddr:");
8670 p = write_ptid (p, endp, ptid);
8672 p += hexnumstr (p, offset);
8674 p += hexnumstr (p, lm);
8678 getpkt (&rs->buf, &rs->buf_size, 0);
8679 result = packet_ok (rs->buf,
8680 &remote_protocol_packets[PACKET_qGetTLSAddr]);
8681 if (result == PACKET_OK)
8685 unpack_varlen_hex (rs->buf, &result);
8688 else if (result == PACKET_UNKNOWN)
8689 throw_error (TLS_GENERIC_ERROR,
8690 _("Remote target doesn't support qGetTLSAddr packet"));
8692 throw_error (TLS_GENERIC_ERROR,
8693 _("Remote target failed to process qGetTLSAddr request"));
8696 throw_error (TLS_GENERIC_ERROR,
8697 _("TLS not supported or disabled on this target"));
8702 /* Provide thread local base, i.e. Thread Information Block address.
8703 Returns 1 if ptid is found and thread_local_base is non zero. */
8706 remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
8708 if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE)
8710 struct remote_state *rs = get_remote_state ();
8712 char *endp = rs->buf + get_remote_packet_size ();
8713 enum packet_result result;
8715 strcpy (p, "qGetTIBAddr:");
8717 p = write_ptid (p, endp, ptid);
8721 getpkt (&rs->buf, &rs->buf_size, 0);
8722 result = packet_ok (rs->buf,
8723 &remote_protocol_packets[PACKET_qGetTIBAddr]);
8724 if (result == PACKET_OK)
8728 unpack_varlen_hex (rs->buf, &result);
8730 *addr = (CORE_ADDR) result;
8733 else if (result == PACKET_UNKNOWN)
8734 error (_("Remote target doesn't support qGetTIBAddr packet"));
8736 error (_("Remote target failed to process qGetTIBAddr request"));
8739 error (_("qGetTIBAddr not supported or disabled on this target"));
8744 /* Support for inferring a target description based on the current
8745 architecture and the size of a 'g' packet. While the 'g' packet
8746 can have any size (since optional registers can be left off the
8747 end), some sizes are easily recognizable given knowledge of the
8748 approximate architecture. */
8750 struct remote_g_packet_guess
8753 const struct target_desc *tdesc;
8755 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
8756 DEF_VEC_O(remote_g_packet_guess_s);
8758 struct remote_g_packet_data
8760 VEC(remote_g_packet_guess_s) *guesses;
8763 static struct gdbarch_data *remote_g_packet_data_handle;
8766 remote_g_packet_data_init (struct obstack *obstack)
8768 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
8772 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
8773 const struct target_desc *tdesc)
8775 struct remote_g_packet_data *data
8776 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
8777 struct remote_g_packet_guess new_guess, *guess;
8780 gdb_assert (tdesc != NULL);
8783 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
8785 if (guess->bytes == bytes)
8786 internal_error (__FILE__, __LINE__,
8787 _("Duplicate g packet description added for size %d"),
8790 new_guess.bytes = bytes;
8791 new_guess.tdesc = tdesc;
8792 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
8795 /* Return 1 if remote_read_description would do anything on this target
8796 and architecture, 0 otherwise. */
8799 remote_read_description_p (struct target_ops *target)
8801 struct remote_g_packet_data *data
8802 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
8804 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
8810 static const struct target_desc *
8811 remote_read_description (struct target_ops *target)
8813 struct remote_g_packet_data *data
8814 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
8816 /* Do not try this during initial connection, when we do not know
8817 whether there is a running but stopped thread. */
8818 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
8821 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
8823 struct remote_g_packet_guess *guess;
8825 int bytes = send_g_packet ();
8828 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
8830 if (guess->bytes == bytes)
8831 return guess->tdesc;
8833 /* We discard the g packet. A minor optimization would be to
8834 hold on to it, and fill the register cache once we have selected
8835 an architecture, but it's too tricky to do safely. */
8841 /* Remote file transfer support. This is host-initiated I/O, not
8842 target-initiated; for target-initiated, see remote-fileio.c. */
8844 /* If *LEFT is at least the length of STRING, copy STRING to
8845 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8846 decrease *LEFT. Otherwise raise an error. */
8849 remote_buffer_add_string (char **buffer, int *left, char *string)
8851 int len = strlen (string);
8854 error (_("Packet too long for target."));
8856 memcpy (*buffer, string, len);
8860 /* NUL-terminate the buffer as a convenience, if there is
8866 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
8867 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8868 decrease *LEFT. Otherwise raise an error. */
8871 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
8874 if (2 * len > *left)
8875 error (_("Packet too long for target."));
8877 bin2hex (bytes, *buffer, len);
8881 /* NUL-terminate the buffer as a convenience, if there is
8887 /* If *LEFT is large enough, convert VALUE to hex and add it to
8888 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8889 decrease *LEFT. Otherwise raise an error. */
8892 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
8894 int len = hexnumlen (value);
8897 error (_("Packet too long for target."));
8899 hexnumstr (*buffer, value);
8903 /* NUL-terminate the buffer as a convenience, if there is
8909 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8910 value, *REMOTE_ERRNO to the remote error number or zero if none
8911 was included, and *ATTACHMENT to point to the start of the annex
8912 if any. The length of the packet isn't needed here; there may
8913 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8915 Return 0 if the packet could be parsed, -1 if it could not. If
8916 -1 is returned, the other variables may not be initialized. */
8919 remote_hostio_parse_result (char *buffer, int *retcode,
8920 int *remote_errno, char **attachment)
8927 if (buffer[0] != 'F')
8931 *retcode = strtol (&buffer[1], &p, 16);
8932 if (errno != 0 || p == &buffer[1])
8935 /* Check for ",errno". */
8939 *remote_errno = strtol (p + 1, &p2, 16);
8940 if (errno != 0 || p + 1 == p2)
8945 /* Check for ";attachment". If there is no attachment, the
8946 packet should end here. */
8949 *attachment = p + 1;
8952 else if (*p == '\0')
8958 /* Send a prepared I/O packet to the target and read its response.
8959 The prepared packet is in the global RS->BUF before this function
8960 is called, and the answer is there when we return.
8962 COMMAND_BYTES is the length of the request to send, which may include
8963 binary data. WHICH_PACKET is the packet configuration to check
8964 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8965 is set to the error number and -1 is returned. Otherwise the value
8966 returned by the function is returned.
8968 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8969 attachment is expected; an error will be reported if there's a
8970 mismatch. If one is found, *ATTACHMENT will be set to point into
8971 the packet buffer and *ATTACHMENT_LEN will be set to the
8972 attachment's length. */
8975 remote_hostio_send_command (int command_bytes, int which_packet,
8976 int *remote_errno, char **attachment,
8977 int *attachment_len)
8979 struct remote_state *rs = get_remote_state ();
8980 int ret, bytes_read;
8981 char *attachment_tmp;
8984 || remote_protocol_packets[which_packet].support == PACKET_DISABLE)
8986 *remote_errno = FILEIO_ENOSYS;
8990 putpkt_binary (rs->buf, command_bytes);
8991 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8993 /* If it timed out, something is wrong. Don't try to parse the
8997 *remote_errno = FILEIO_EINVAL;
9001 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9004 *remote_errno = FILEIO_EINVAL;
9006 case PACKET_UNKNOWN:
9007 *remote_errno = FILEIO_ENOSYS;
9013 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9016 *remote_errno = FILEIO_EINVAL;
9020 /* Make sure we saw an attachment if and only if we expected one. */
9021 if ((attachment_tmp == NULL && attachment != NULL)
9022 || (attachment_tmp != NULL && attachment == NULL))
9024 *remote_errno = FILEIO_EINVAL;
9028 /* If an attachment was found, it must point into the packet buffer;
9029 work out how many bytes there were. */
9030 if (attachment_tmp != NULL)
9032 *attachment = attachment_tmp;
9033 *attachment_len = bytes_read - (*attachment - rs->buf);
9039 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9040 remote file descriptor, or -1 if an error occurs (and set
9044 remote_hostio_open (const char *filename, int flags, int mode,
9047 struct remote_state *rs = get_remote_state ();
9049 int left = get_remote_packet_size () - 1;
9051 remote_buffer_add_string (&p, &left, "vFile:open:");
9053 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9055 remote_buffer_add_string (&p, &left, ",");
9057 remote_buffer_add_int (&p, &left, flags);
9058 remote_buffer_add_string (&p, &left, ",");
9060 remote_buffer_add_int (&p, &left, mode);
9062 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9063 remote_errno, NULL, NULL);
9066 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9067 Return the number of bytes written, or -1 if an error occurs (and
9068 set *REMOTE_ERRNO). */
9071 remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
9072 ULONGEST offset, int *remote_errno)
9074 struct remote_state *rs = get_remote_state ();
9076 int left = get_remote_packet_size ();
9079 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9081 remote_buffer_add_int (&p, &left, fd);
9082 remote_buffer_add_string (&p, &left, ",");
9084 remote_buffer_add_int (&p, &left, offset);
9085 remote_buffer_add_string (&p, &left, ",");
9087 p += remote_escape_output (write_buf, len, p, &out_len,
9088 get_remote_packet_size () - (p - rs->buf));
9090 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9091 remote_errno, NULL, NULL);
9094 /* Read up to LEN bytes FD on the remote target into READ_BUF
9095 Return the number of bytes read, or -1 if an error occurs (and
9096 set *REMOTE_ERRNO). */
9099 remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
9100 ULONGEST offset, int *remote_errno)
9102 struct remote_state *rs = get_remote_state ();
9105 int left = get_remote_packet_size ();
9106 int ret, attachment_len;
9109 remote_buffer_add_string (&p, &left, "vFile:pread:");
9111 remote_buffer_add_int (&p, &left, fd);
9112 remote_buffer_add_string (&p, &left, ",");
9114 remote_buffer_add_int (&p, &left, len);
9115 remote_buffer_add_string (&p, &left, ",");
9117 remote_buffer_add_int (&p, &left, offset);
9119 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9120 remote_errno, &attachment,
9126 read_len = remote_unescape_input (attachment, attachment_len,
9128 if (read_len != ret)
9129 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9134 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9135 (and set *REMOTE_ERRNO). */
9138 remote_hostio_close (int fd, int *remote_errno)
9140 struct remote_state *rs = get_remote_state ();
9142 int left = get_remote_packet_size () - 1;
9144 remote_buffer_add_string (&p, &left, "vFile:close:");
9146 remote_buffer_add_int (&p, &left, fd);
9148 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9149 remote_errno, NULL, NULL);
9152 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9153 occurs (and set *REMOTE_ERRNO). */
9156 remote_hostio_unlink (const char *filename, int *remote_errno)
9158 struct remote_state *rs = get_remote_state ();
9160 int left = get_remote_packet_size () - 1;
9162 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9164 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9167 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9168 remote_errno, NULL, NULL);
9172 remote_fileio_errno_to_host (int errnum)
9196 case FILEIO_ENOTDIR:
9216 case FILEIO_ENAMETOOLONG:
9217 return ENAMETOOLONG;
9223 remote_hostio_error (int errnum)
9225 int host_error = remote_fileio_errno_to_host (errnum);
9227 if (host_error == -1)
9228 error (_("Unknown remote I/O error %d"), errnum);
9230 error (_("Remote I/O error: %s"), safe_strerror (host_error));
9234 remote_hostio_close_cleanup (void *opaque)
9236 int fd = *(int *) opaque;
9239 remote_hostio_close (fd, &remote_errno);
9244 remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
9246 const char *filename = bfd_get_filename (abfd);
9247 int fd, remote_errno;
9250 gdb_assert (remote_filename_p (filename));
9252 fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
9255 errno = remote_fileio_errno_to_host (remote_errno);
9256 bfd_set_error (bfd_error_system_call);
9260 stream = xmalloc (sizeof (int));
9266 remote_bfd_iovec_close (struct bfd *abfd, void *stream)
9268 int fd = *(int *)stream;
9273 /* Ignore errors on close; these may happen if the remote
9274 connection was already torn down. */
9275 remote_hostio_close (fd, &remote_errno);
9281 remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
9282 file_ptr nbytes, file_ptr offset)
9284 int fd = *(int *)stream;
9286 file_ptr pos, bytes;
9289 while (nbytes > pos)
9291 bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos,
9292 offset + pos, &remote_errno);
9294 /* Success, but no bytes, means end-of-file. */
9298 errno = remote_fileio_errno_to_host (remote_errno);
9299 bfd_set_error (bfd_error_system_call);
9310 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
9312 /* FIXME: We should probably implement remote_hostio_stat. */
9313 sb->st_size = INT_MAX;
9318 remote_filename_p (const char *filename)
9320 return strncmp (filename, "remote:", 7) == 0;
9324 remote_bfd_open (const char *remote_file, const char *target)
9326 return bfd_openr_iovec (remote_file, target,
9327 remote_bfd_iovec_open, NULL,
9328 remote_bfd_iovec_pread,
9329 remote_bfd_iovec_close,
9330 remote_bfd_iovec_stat);
9334 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
9336 struct cleanup *back_to, *close_cleanup;
9337 int retcode, fd, remote_errno, bytes, io_size;
9340 int bytes_in_buffer;
9345 error (_("command can only be used with remote target"));
9347 file = fopen (local_file, "rb");
9349 perror_with_name (local_file);
9350 back_to = make_cleanup_fclose (file);
9352 fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
9354 0700, &remote_errno);
9356 remote_hostio_error (remote_errno);
9358 /* Send up to this many bytes at once. They won't all fit in the
9359 remote packet limit, so we'll transfer slightly fewer. */
9360 io_size = get_remote_packet_size ();
9361 buffer = xmalloc (io_size);
9362 make_cleanup (xfree, buffer);
9364 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9366 bytes_in_buffer = 0;
9369 while (bytes_in_buffer || !saw_eof)
9373 bytes = fread (buffer + bytes_in_buffer, 1,
9374 io_size - bytes_in_buffer,
9379 error (_("Error reading %s."), local_file);
9382 /* EOF. Unless there is something still in the
9383 buffer from the last iteration, we are done. */
9385 if (bytes_in_buffer == 0)
9393 bytes += bytes_in_buffer;
9394 bytes_in_buffer = 0;
9396 retcode = remote_hostio_pwrite (fd, buffer, bytes,
9397 offset, &remote_errno);
9400 remote_hostio_error (remote_errno);
9401 else if (retcode == 0)
9402 error (_("Remote write of %d bytes returned 0!"), bytes);
9403 else if (retcode < bytes)
9405 /* Short write. Save the rest of the read data for the next
9407 bytes_in_buffer = bytes - retcode;
9408 memmove (buffer, buffer + retcode, bytes_in_buffer);
9414 discard_cleanups (close_cleanup);
9415 if (remote_hostio_close (fd, &remote_errno))
9416 remote_hostio_error (remote_errno);
9419 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
9420 do_cleanups (back_to);
9424 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
9426 struct cleanup *back_to, *close_cleanup;
9427 int fd, remote_errno, bytes, io_size;
9433 error (_("command can only be used with remote target"));
9435 fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
9437 remote_hostio_error (remote_errno);
9439 file = fopen (local_file, "wb");
9441 perror_with_name (local_file);
9442 back_to = make_cleanup_fclose (file);
9444 /* Send up to this many bytes at once. They won't all fit in the
9445 remote packet limit, so we'll transfer slightly fewer. */
9446 io_size = get_remote_packet_size ();
9447 buffer = xmalloc (io_size);
9448 make_cleanup (xfree, buffer);
9450 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9455 bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
9457 /* Success, but no bytes, means end-of-file. */
9460 remote_hostio_error (remote_errno);
9464 bytes = fwrite (buffer, 1, bytes, file);
9466 perror_with_name (local_file);
9469 discard_cleanups (close_cleanup);
9470 if (remote_hostio_close (fd, &remote_errno))
9471 remote_hostio_error (remote_errno);
9474 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
9475 do_cleanups (back_to);
9479 remote_file_delete (const char *remote_file, int from_tty)
9481 int retcode, remote_errno;
9484 error (_("command can only be used with remote target"));
9486 retcode = remote_hostio_unlink (remote_file, &remote_errno);
9488 remote_hostio_error (remote_errno);
9491 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
9495 remote_put_command (char *args, int from_tty)
9497 struct cleanup *back_to;
9501 error_no_arg (_("file to put"));
9503 argv = gdb_buildargv (args);
9504 back_to = make_cleanup_freeargv (argv);
9505 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
9506 error (_("Invalid parameters to remote put"));
9508 remote_file_put (argv[0], argv[1], from_tty);
9510 do_cleanups (back_to);
9514 remote_get_command (char *args, int from_tty)
9516 struct cleanup *back_to;
9520 error_no_arg (_("file to get"));
9522 argv = gdb_buildargv (args);
9523 back_to = make_cleanup_freeargv (argv);
9524 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
9525 error (_("Invalid parameters to remote get"));
9527 remote_file_get (argv[0], argv[1], from_tty);
9529 do_cleanups (back_to);
9533 remote_delete_command (char *args, int from_tty)
9535 struct cleanup *back_to;
9539 error_no_arg (_("file to delete"));
9541 argv = gdb_buildargv (args);
9542 back_to = make_cleanup_freeargv (argv);
9543 if (argv[0] == NULL || argv[1] != NULL)
9544 error (_("Invalid parameters to remote delete"));
9546 remote_file_delete (argv[0], from_tty);
9548 do_cleanups (back_to);
9552 remote_command (char *args, int from_tty)
9554 help_list (remote_cmdlist, "remote ", -1, gdb_stdout);
9558 remote_can_execute_reverse (void)
9560 if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE
9561 || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE)
9568 remote_supports_non_stop (void)
9574 remote_supports_multi_process (void)
9576 struct remote_state *rs = get_remote_state ();
9578 return remote_multi_process_p (rs);
9582 remote_supports_cond_tracepoints (void)
9584 struct remote_state *rs = get_remote_state ();
9586 return rs->cond_tracepoints;
9590 remote_supports_fast_tracepoints (void)
9592 struct remote_state *rs = get_remote_state ();
9594 return rs->fast_tracepoints;
9598 remote_supports_static_tracepoints (void)
9600 struct remote_state *rs = get_remote_state ();
9602 return rs->static_tracepoints;
9606 remote_trace_init (void)
9609 remote_get_noisy_reply (&target_buf, &target_buf_size);
9610 if (strcmp (target_buf, "OK") != 0)
9611 error (_("Target does not support this command."));
9614 static void free_actions_list (char **actions_list);
9615 static void free_actions_list_cleanup_wrapper (void *);
9617 free_actions_list_cleanup_wrapper (void *al)
9619 free_actions_list (al);
9623 free_actions_list (char **actions_list)
9627 if (actions_list == 0)
9630 for (ndx = 0; actions_list[ndx]; ndx++)
9631 xfree (actions_list[ndx]);
9633 xfree (actions_list);
9636 /* Recursive routine to walk through command list including loops, and
9637 download packets for each command. */
9640 remote_download_command_source (int num, ULONGEST addr,
9641 struct command_line *cmds)
9643 struct remote_state *rs = get_remote_state ();
9644 struct command_line *cmd;
9646 for (cmd = cmds; cmd; cmd = cmd->next)
9648 QUIT; /* Allow user to bail out with ^C. */
9649 strcpy (rs->buf, "QTDPsrc:");
9650 encode_source_string (num, addr, "cmd", cmd->line,
9651 rs->buf + strlen (rs->buf),
9652 rs->buf_size - strlen (rs->buf));
9654 remote_get_noisy_reply (&target_buf, &target_buf_size);
9655 if (strcmp (target_buf, "OK"))
9656 warning (_("Target does not support source download."));
9658 if (cmd->control_type == while_control
9659 || cmd->control_type == while_stepping_control)
9661 remote_download_command_source (num, addr, *cmd->body_list);
9663 QUIT; /* Allow user to bail out with ^C. */
9664 strcpy (rs->buf, "QTDPsrc:");
9665 encode_source_string (num, addr, "cmd", "end",
9666 rs->buf + strlen (rs->buf),
9667 rs->buf_size - strlen (rs->buf));
9669 remote_get_noisy_reply (&target_buf, &target_buf_size);
9670 if (strcmp (target_buf, "OK"))
9671 warning (_("Target does not support source download."));
9677 remote_download_tracepoint (struct breakpoint *t)
9679 struct bp_location *loc;
9684 char **stepping_actions;
9686 struct cleanup *old_chain = NULL;
9687 struct agent_expr *aexpr;
9688 struct cleanup *aexpr_chain = NULL;
9691 /* Iterate over all the tracepoint locations. It's up to the target to
9692 notice multiple tracepoint packets with the same number but different
9693 addresses, and treat them as multiple locations. */
9694 for (loc = t->loc; loc; loc = loc->next)
9696 encode_actions (t, loc, &tdp_actions, &stepping_actions);
9697 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
9699 (void) make_cleanup (free_actions_list_cleanup_wrapper,
9702 tpaddr = loc->address;
9703 sprintf_vma (addrbuf, tpaddr);
9704 sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number,
9705 addrbuf, /* address */
9706 (t->enable_state == bp_enabled ? 'E' : 'D'),
9707 t->step_count, t->pass_count);
9708 /* Fast tracepoints are mostly handled by the target, but we can
9709 tell the target how big of an instruction block should be moved
9711 if (t->type == bp_fast_tracepoint)
9713 /* Only test for support at download time; we may not know
9714 target capabilities at definition time. */
9715 if (remote_supports_fast_tracepoints ())
9719 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch,
9720 tpaddr, &isize, NULL))
9721 sprintf (buf + strlen (buf), ":F%x", isize);
9723 /* If it passed validation at definition but fails now,
9724 something is very wrong. */
9725 internal_error (__FILE__, __LINE__,
9726 _("Fast tracepoint not "
9727 "valid during download"));
9730 /* Fast tracepoints are functionally identical to regular
9731 tracepoints, so don't take lack of support as a reason to
9732 give up on the trace run. */
9733 warning (_("Target does not support fast tracepoints, "
9734 "downloading %d as regular tracepoint"), t->number);
9736 else if (t->type == bp_static_tracepoint)
9738 /* Only test for support at download time; we may not know
9739 target capabilities at definition time. */
9740 if (remote_supports_static_tracepoints ())
9742 struct static_tracepoint_marker marker;
9744 if (target_static_tracepoint_marker_at (tpaddr, &marker))
9747 error (_("Static tracepoint not valid during download"));
9750 /* Fast tracepoints are functionally identical to regular
9751 tracepoints, so don't take lack of support as a reason
9752 to give up on the trace run. */
9753 error (_("Target does not support static tracepoints"));
9755 /* If the tracepoint has a conditional, make it into an agent
9756 expression and append to the definition. */
9759 /* Only test support at download time, we may not know target
9760 capabilities at definition time. */
9761 if (remote_supports_cond_tracepoints ())
9763 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
9764 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
9765 sprintf (buf + strlen (buf), ":X%x,", aexpr->len);
9766 pkt = buf + strlen (buf);
9767 for (ndx = 0; ndx < aexpr->len; ++ndx)
9768 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
9770 do_cleanups (aexpr_chain);
9773 warning (_("Target does not support conditional tracepoints, "
9774 "ignoring tp %d cond"), t->number);
9777 if (t->commands || *default_collect)
9780 remote_get_noisy_reply (&target_buf, &target_buf_size);
9781 if (strcmp (target_buf, "OK"))
9782 error (_("Target does not support tracepoints."));
9784 /* do_single_steps (t); */
9787 for (ndx = 0; tdp_actions[ndx]; ndx++)
9789 QUIT; /* Allow user to bail out with ^C. */
9790 sprintf (buf, "QTDP:-%x:%s:%s%c",
9791 t->number, addrbuf, /* address */
9793 ((tdp_actions[ndx + 1] || stepping_actions)
9796 remote_get_noisy_reply (&target_buf,
9798 if (strcmp (target_buf, "OK"))
9799 error (_("Error on target while setting tracepoints."));
9802 if (stepping_actions)
9804 for (ndx = 0; stepping_actions[ndx]; ndx++)
9806 QUIT; /* Allow user to bail out with ^C. */
9807 sprintf (buf, "QTDP:-%x:%s:%s%s%s",
9808 t->number, addrbuf, /* address */
9809 ((ndx == 0) ? "S" : ""),
9810 stepping_actions[ndx],
9811 (stepping_actions[ndx + 1] ? "-" : ""));
9813 remote_get_noisy_reply (&target_buf,
9815 if (strcmp (target_buf, "OK"))
9816 error (_("Error on target while setting tracepoints."));
9820 if (remote_protocol_packets[PACKET_TracepointSource].support
9825 strcpy (buf, "QTDPsrc:");
9826 encode_source_string (t->number, loc->address,
9827 "at", t->addr_string, buf + strlen (buf),
9828 2048 - strlen (buf));
9831 remote_get_noisy_reply (&target_buf, &target_buf_size);
9832 if (strcmp (target_buf, "OK"))
9833 warning (_("Target does not support source download."));
9837 strcpy (buf, "QTDPsrc:");
9838 encode_source_string (t->number, loc->address,
9839 "cond", t->cond_string, buf + strlen (buf),
9840 2048 - strlen (buf));
9842 remote_get_noisy_reply (&target_buf, &target_buf_size);
9843 if (strcmp (target_buf, "OK"))
9844 warning (_("Target does not support source download."));
9846 remote_download_command_source (t->number, loc->address,
9847 breakpoint_commands (t));
9850 do_cleanups (old_chain);
9855 remote_download_trace_state_variable (struct trace_state_variable *tsv)
9857 struct remote_state *rs = get_remote_state ();
9860 sprintf (rs->buf, "QTDV:%x:%s:%x:",
9861 tsv->number, phex ((ULONGEST) tsv->initial_value, 8), tsv->builtin);
9862 p = rs->buf + strlen (rs->buf);
9863 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
9864 error (_("Trace state variable name too long for tsv definition packet"));
9865 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0);
9868 remote_get_noisy_reply (&target_buf, &target_buf_size);
9869 if (*target_buf == '\0')
9870 error (_("Target does not support this command."));
9871 if (strcmp (target_buf, "OK") != 0)
9872 error (_("Error on target while downloading trace state variable."));
9876 remote_trace_set_readonly_regions (void)
9884 return; /* No information to give. */
9886 strcpy (target_buf, "QTro");
9887 for (s = exec_bfd->sections; s; s = s->next)
9889 char tmp1[40], tmp2[40];
9891 if ((s->flags & SEC_LOAD) == 0 ||
9892 /* (s->flags & SEC_CODE) == 0 || */
9893 (s->flags & SEC_READONLY) == 0)
9897 vma = bfd_get_section_vma (,s);
9898 size = bfd_get_section_size (s);
9899 sprintf_vma (tmp1, vma);
9900 sprintf_vma (tmp2, vma + size);
9901 sprintf (target_buf + strlen (target_buf),
9902 ":%s,%s", tmp1, tmp2);
9906 putpkt (target_buf);
9907 getpkt (&target_buf, &target_buf_size, 0);
9912 remote_trace_start (void)
9915 remote_get_noisy_reply (&target_buf, &target_buf_size);
9916 if (*target_buf == '\0')
9917 error (_("Target does not support this command."));
9918 if (strcmp (target_buf, "OK") != 0)
9919 error (_("Bogus reply from target: %s"), target_buf);
9923 remote_get_trace_status (struct trace_status *ts)
9926 /* FIXME we need to get register block size some other way. */
9927 extern int trace_regblock_size;
9929 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
9931 putpkt ("qTStatus");
9932 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
9934 /* If the remote target doesn't do tracing, flag it. */
9938 /* We're working with a live target. */
9941 /* Set some defaults. */
9942 ts->running_known = 0;
9943 ts->stop_reason = trace_stop_reason_unknown;
9944 ts->traceframe_count = -1;
9945 ts->buffer_free = 0;
9948 error (_("Bogus trace status reply from target: %s"), target_buf);
9950 parse_trace_status (p, ts);
9956 remote_trace_stop (void)
9959 remote_get_noisy_reply (&target_buf, &target_buf_size);
9960 if (*target_buf == '\0')
9961 error (_("Target does not support this command."));
9962 if (strcmp (target_buf, "OK") != 0)
9963 error (_("Bogus reply from target: %s"), target_buf);
9967 remote_trace_find (enum trace_find_type type, int num,
9968 ULONGEST addr1, ULONGEST addr2,
9971 struct remote_state *rs = get_remote_state ();
9973 int target_frameno = -1, target_tracept = -1;
9976 strcpy (p, "QTFrame:");
9977 p = strchr (p, '\0');
9981 sprintf (p, "%x", num);
9984 sprintf (p, "pc:%s", phex_nz (addr1, 0));
9987 sprintf (p, "tdp:%x", num);
9990 sprintf (p, "range:%s:%s", phex_nz (addr1, 0), phex_nz (addr2, 0));
9993 sprintf (p, "outside:%s:%s", phex_nz (addr1, 0), phex_nz (addr2, 0));
9996 error (_("Unknown trace find type %d"), type);
10000 reply = remote_get_noisy_reply (&(rs->buf), &sizeof_pkt);
10001 if (*reply == '\0')
10002 error (_("Target does not support this command."));
10004 while (reply && *reply)
10009 target_frameno = (int) strtol (p, &reply, 16);
10011 error (_("Unable to parse trace frame number"));
10012 if (target_frameno == -1)
10017 target_tracept = (int) strtol (p, &reply, 16);
10019 error (_("Unable to parse tracepoint number"));
10021 case 'O': /* "OK"? */
10022 if (reply[1] == 'K' && reply[2] == '\0')
10025 error (_("Bogus reply from target: %s"), reply);
10028 error (_("Bogus reply from target: %s"), reply);
10031 *tpp = target_tracept;
10032 return target_frameno;
10036 remote_get_trace_state_variable_value (int tsvnum, LONGEST *val)
10038 struct remote_state *rs = get_remote_state ();
10042 sprintf (rs->buf, "qTV:%x", tsvnum);
10044 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10045 if (reply && *reply)
10049 unpack_varlen_hex (reply + 1, &uval);
10050 *val = (LONGEST) uval;
10058 remote_save_trace_data (const char *filename)
10060 struct remote_state *rs = get_remote_state ();
10064 strcpy (p, "QTSave:");
10066 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10067 error (_("Remote file name too long for trace save packet"));
10068 p += 2 * bin2hex ((gdb_byte *) filename, p, 0);
10071 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10072 if (*reply != '\0')
10073 error (_("Target does not support this command."));
10074 if (strcmp (reply, "OK") != 0)
10075 error (_("Bogus reply from target: %s"), reply);
10079 /* This is basically a memory transfer, but needs to be its own packet
10080 because we don't know how the target actually organizes its trace
10081 memory, plus we want to be able to ask for as much as possible, but
10082 not be unhappy if we don't get as much as we ask for. */
10085 remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
10087 struct remote_state *rs = get_remote_state ();
10093 strcpy (p, "qTBuffer:");
10095 p += hexnumstr (p, offset);
10097 p += hexnumstr (p, len);
10101 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10102 if (reply && *reply)
10104 /* 'l' by itself means we're at the end of the buffer and
10105 there is nothing more to get. */
10109 /* Convert the reply into binary. Limit the number of bytes to
10110 convert according to our passed-in buffer size, rather than
10111 what was returned in the packet; if the target is
10112 unexpectedly generous and gives us a bigger reply than we
10113 asked for, we don't want to crash. */
10114 rslt = hex2bin (target_buf, buf, len);
10118 /* Something went wrong, flag as an error. */
10123 remote_set_disconnected_tracing (int val)
10125 struct remote_state *rs = get_remote_state ();
10127 if (rs->disconnected_tracing)
10131 sprintf (rs->buf, "QTDisconnected:%x", val);
10133 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10134 if (*reply == '\0')
10135 error (_("Target does not support this command."));
10136 if (strcmp (reply, "OK") != 0)
10137 error (_("Bogus reply from target: %s"), reply);
10140 warning (_("Target does not support disconnected tracing."));
10144 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
10146 struct thread_info *info = find_thread_ptid (ptid);
10148 if (info && info->private)
10149 return info->private->core;
10154 remote_set_circular_trace_buffer (int val)
10156 struct remote_state *rs = get_remote_state ();
10159 sprintf (rs->buf, "QTBuffer:circular:%x", val);
10161 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10162 if (*reply == '\0')
10163 error (_("Target does not support this command."));
10164 if (strcmp (reply, "OK") != 0)
10165 error (_("Bogus reply from target: %s"), reply);
10168 static struct traceframe_info *
10169 remote_traceframe_info (void)
10173 text = target_read_stralloc (¤t_target,
10174 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
10177 struct traceframe_info *info;
10178 struct cleanup *back_to = make_cleanup (xfree, text);
10180 info = parse_traceframe_info (text);
10181 do_cleanups (back_to);
10189 init_remote_ops (void)
10191 remote_ops.to_shortname = "remote";
10192 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
10193 remote_ops.to_doc =
10194 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10195 Specify the serial device it is connected to\n\
10196 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10197 remote_ops.to_open = remote_open;
10198 remote_ops.to_close = remote_close;
10199 remote_ops.to_detach = remote_detach;
10200 remote_ops.to_disconnect = remote_disconnect;
10201 remote_ops.to_resume = remote_resume;
10202 remote_ops.to_wait = remote_wait;
10203 remote_ops.to_fetch_registers = remote_fetch_registers;
10204 remote_ops.to_store_registers = remote_store_registers;
10205 remote_ops.to_prepare_to_store = remote_prepare_to_store;
10206 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
10207 remote_ops.to_files_info = remote_files_info;
10208 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
10209 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
10210 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
10211 remote_ops.to_stopped_data_address = remote_stopped_data_address;
10212 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
10213 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
10214 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
10215 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
10216 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
10217 remote_ops.to_kill = remote_kill;
10218 remote_ops.to_load = generic_load;
10219 remote_ops.to_mourn_inferior = remote_mourn;
10220 remote_ops.to_notice_signals = remote_notice_signals;
10221 remote_ops.to_thread_alive = remote_thread_alive;
10222 remote_ops.to_find_new_threads = remote_threads_info;
10223 remote_ops.to_pid_to_str = remote_pid_to_str;
10224 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10225 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
10226 remote_ops.to_stop = remote_stop;
10227 remote_ops.to_xfer_partial = remote_xfer_partial;
10228 remote_ops.to_rcmd = remote_rcmd;
10229 remote_ops.to_log_command = serial_log_command;
10230 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
10231 remote_ops.to_stratum = process_stratum;
10232 remote_ops.to_has_all_memory = default_child_has_all_memory;
10233 remote_ops.to_has_memory = default_child_has_memory;
10234 remote_ops.to_has_stack = default_child_has_stack;
10235 remote_ops.to_has_registers = default_child_has_registers;
10236 remote_ops.to_has_execution = default_child_has_execution;
10237 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
10238 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
10239 remote_ops.to_magic = OPS_MAGIC;
10240 remote_ops.to_memory_map = remote_memory_map;
10241 remote_ops.to_flash_erase = remote_flash_erase;
10242 remote_ops.to_flash_done = remote_flash_done;
10243 remote_ops.to_read_description = remote_read_description;
10244 remote_ops.to_search_memory = remote_search_memory;
10245 remote_ops.to_can_async_p = remote_can_async_p;
10246 remote_ops.to_is_async_p = remote_is_async_p;
10247 remote_ops.to_async = remote_async;
10248 remote_ops.to_async_mask = remote_async_mask;
10249 remote_ops.to_terminal_inferior = remote_terminal_inferior;
10250 remote_ops.to_terminal_ours = remote_terminal_ours;
10251 remote_ops.to_supports_non_stop = remote_supports_non_stop;
10252 remote_ops.to_supports_multi_process = remote_supports_multi_process;
10253 remote_ops.to_trace_init = remote_trace_init;
10254 remote_ops.to_download_tracepoint = remote_download_tracepoint;
10255 remote_ops.to_download_trace_state_variable
10256 = remote_download_trace_state_variable;
10257 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
10258 remote_ops.to_trace_start = remote_trace_start;
10259 remote_ops.to_get_trace_status = remote_get_trace_status;
10260 remote_ops.to_trace_stop = remote_trace_stop;
10261 remote_ops.to_trace_find = remote_trace_find;
10262 remote_ops.to_get_trace_state_variable_value
10263 = remote_get_trace_state_variable_value;
10264 remote_ops.to_save_trace_data = remote_save_trace_data;
10265 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
10266 remote_ops.to_upload_trace_state_variables
10267 = remote_upload_trace_state_variables;
10268 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
10269 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
10270 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
10271 remote_ops.to_core_of_thread = remote_core_of_thread;
10272 remote_ops.to_verify_memory = remote_verify_memory;
10273 remote_ops.to_get_tib_address = remote_get_tib_address;
10274 remote_ops.to_set_permissions = remote_set_permissions;
10275 remote_ops.to_static_tracepoint_marker_at
10276 = remote_static_tracepoint_marker_at;
10277 remote_ops.to_static_tracepoint_markers_by_strid
10278 = remote_static_tracepoint_markers_by_strid;
10279 remote_ops.to_traceframe_info = remote_traceframe_info;
10282 /* Set up the extended remote vector by making a copy of the standard
10283 remote vector and adding to it. */
10286 init_extended_remote_ops (void)
10288 extended_remote_ops = remote_ops;
10290 extended_remote_ops.to_shortname = "extended-remote";
10291 extended_remote_ops.to_longname =
10292 "Extended remote serial target in gdb-specific protocol";
10293 extended_remote_ops.to_doc =
10294 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10295 Specify the serial device it is connected to (e.g. /dev/ttya).";
10296 extended_remote_ops.to_open = extended_remote_open;
10297 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
10298 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
10299 extended_remote_ops.to_detach = extended_remote_detach;
10300 extended_remote_ops.to_attach = extended_remote_attach;
10301 extended_remote_ops.to_kill = extended_remote_kill;
10305 remote_can_async_p (void)
10307 if (!target_async_permitted)
10308 /* We only enable async when the user specifically asks for it. */
10311 /* We're async whenever the serial device is. */
10312 return remote_async_mask_value && serial_can_async_p (remote_desc);
10316 remote_is_async_p (void)
10318 if (!target_async_permitted)
10319 /* We only enable async when the user specifically asks for it. */
10322 /* We're async whenever the serial device is. */
10323 return remote_async_mask_value && serial_is_async_p (remote_desc);
10326 /* Pass the SERIAL event on and up to the client. One day this code
10327 will be able to delay notifying the client of an event until the
10328 point where an entire packet has been received. */
10330 static void (*async_client_callback) (enum inferior_event_type event_type,
10332 static void *async_client_context;
10333 static serial_event_ftype remote_async_serial_handler;
10336 remote_async_serial_handler (struct serial *scb, void *context)
10338 /* Don't propogate error information up to the client. Instead let
10339 the client find out about the error by querying the target. */
10340 async_client_callback (INF_REG_EVENT, async_client_context);
10344 remote_async_inferior_event_handler (gdb_client_data data)
10346 inferior_event_handler (INF_REG_EVENT, NULL);
10350 remote_async_get_pending_events_handler (gdb_client_data data)
10352 remote_get_pending_stop_replies ();
10356 remote_async (void (*callback) (enum inferior_event_type event_type,
10357 void *context), void *context)
10359 if (remote_async_mask_value == 0)
10360 internal_error (__FILE__, __LINE__,
10361 _("Calling remote_async when async is masked"));
10363 if (callback != NULL)
10365 serial_async (remote_desc, remote_async_serial_handler, NULL);
10366 async_client_callback = callback;
10367 async_client_context = context;
10370 serial_async (remote_desc, NULL, NULL);
10374 remote_async_mask (int new_mask)
10376 int curr_mask = remote_async_mask_value;
10378 remote_async_mask_value = new_mask;
10383 set_remote_cmd (char *args, int from_tty)
10385 help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout);
10389 show_remote_cmd (char *args, int from_tty)
10391 /* We can't just use cmd_show_list here, because we want to skip
10392 the redundant "show remote Z-packet" and the legacy aliases. */
10393 struct cleanup *showlist_chain;
10394 struct cmd_list_element *list = remote_show_cmdlist;
10396 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
10397 for (; list != NULL; list = list->next)
10398 if (strcmp (list->name, "Z-packet") == 0)
10400 else if (list->type == not_set_cmd)
10401 /* Alias commands are exactly like the original, except they
10402 don't have the normal type. */
10406 struct cleanup *option_chain
10407 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
10409 ui_out_field_string (uiout, "name", list->name);
10410 ui_out_text (uiout, ": ");
10411 if (list->type == show_cmd)
10412 do_setshow_command ((char *) NULL, from_tty, list);
10414 cmd_func (list, NULL, from_tty);
10415 /* Close the tuple. */
10416 do_cleanups (option_chain);
10419 /* Close the tuple. */
10420 do_cleanups (showlist_chain);
10424 /* Function to be called whenever a new objfile (shlib) is detected. */
10426 remote_new_objfile (struct objfile *objfile)
10428 if (remote_desc != 0) /* Have a remote connection. */
10429 remote_check_symbols (objfile);
10432 /* Pull all the tracepoints defined on the target and create local
10433 data structures representing them. We don't want to create real
10434 tracepoints yet, we don't want to mess up the user's existing
10438 remote_upload_tracepoints (struct uploaded_tp **utpp)
10440 struct remote_state *rs = get_remote_state ();
10443 /* Ask for a first packet of tracepoint definition. */
10445 getpkt (&rs->buf, &rs->buf_size, 0);
10447 while (*p && *p != 'l')
10449 parse_tracepoint_definition (p, utpp);
10450 /* Ask for another packet of tracepoint definition. */
10452 getpkt (&rs->buf, &rs->buf_size, 0);
10459 remote_upload_trace_state_variables (struct uploaded_tsv **utsvp)
10461 struct remote_state *rs = get_remote_state ();
10464 /* Ask for a first packet of variable definition. */
10466 getpkt (&rs->buf, &rs->buf_size, 0);
10468 while (*p && *p != 'l')
10470 parse_tsv_definition (p, utsvp);
10471 /* Ask for another packet of variable definition. */
10473 getpkt (&rs->buf, &rs->buf_size, 0);
10480 _initialize_remote (void)
10482 struct remote_state *rs;
10483 struct cmd_list_element *cmd;
10486 /* architecture specific data */
10487 remote_gdbarch_data_handle =
10488 gdbarch_data_register_post_init (init_remote_state);
10489 remote_g_packet_data_handle =
10490 gdbarch_data_register_pre_init (remote_g_packet_data_init);
10492 /* Initialize the per-target state. At the moment there is only one
10493 of these, not one per target. Only one target is active at a
10494 time. The default buffer size is unimportant; it will be expanded
10495 whenever a larger buffer is needed. */
10496 rs = get_remote_state_raw ();
10497 rs->buf_size = 400;
10498 rs->buf = xmalloc (rs->buf_size);
10500 init_remote_ops ();
10501 add_target (&remote_ops);
10503 init_extended_remote_ops ();
10504 add_target (&extended_remote_ops);
10506 /* Hook into new objfile notification. */
10507 observer_attach_new_objfile (remote_new_objfile);
10509 /* Set up signal handlers. */
10510 sigint_remote_token =
10511 create_async_signal_handler (async_remote_interrupt, NULL);
10512 sigint_remote_twice_token =
10513 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
10516 init_remote_threadtests ();
10519 /* set/show remote ... */
10521 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
10522 Remote protocol specific variables\n\
10523 Configure various remote-protocol specific variables such as\n\
10524 the packets being used"),
10525 &remote_set_cmdlist, "set remote ",
10526 0 /* allow-unknown */, &setlist);
10527 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
10528 Remote protocol specific variables\n\
10529 Configure various remote-protocol specific variables such as\n\
10530 the packets being used"),
10531 &remote_show_cmdlist, "show remote ",
10532 0 /* allow-unknown */, &showlist);
10534 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
10535 Compare section data on target to the exec file.\n\
10536 Argument is a single section name (default: all loaded sections)."),
10539 add_cmd ("packet", class_maintenance, packet_command, _("\
10540 Send an arbitrary packet to a remote target.\n\
10541 maintenance packet TEXT\n\
10542 If GDB is talking to an inferior via the GDB serial protocol, then\n\
10543 this command sends the string TEXT to the inferior, and displays the\n\
10544 response packet. GDB supplies the initial `$' character, and the\n\
10545 terminating `#' character and checksum."),
10548 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
10549 Set whether to send break if interrupted."), _("\
10550 Show whether to send break if interrupted."), _("\
10551 If set, a break, instead of a cntrl-c, is sent to the remote target."),
10552 set_remotebreak, show_remotebreak,
10553 &setlist, &showlist);
10554 cmd_name = "remotebreak";
10555 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
10556 deprecate_cmd (cmd, "set remote interrupt-sequence");
10557 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
10558 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
10559 deprecate_cmd (cmd, "show remote interrupt-sequence");
10561 add_setshow_enum_cmd ("interrupt-sequence", class_support,
10562 interrupt_sequence_modes, &interrupt_sequence_mode,
10564 Set interrupt sequence to remote target."), _("\
10565 Show interrupt sequence to remote target."), _("\
10566 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
10567 NULL, show_interrupt_sequence,
10568 &remote_set_cmdlist,
10569 &remote_show_cmdlist);
10571 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
10572 &interrupt_on_connect, _("\
10573 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10574 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10575 If set, interrupt sequence is sent to remote target."),
10577 &remote_set_cmdlist, &remote_show_cmdlist);
10579 /* Install commands for configuring memory read/write packets. */
10581 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
10582 Set the maximum number of bytes per memory write packet (deprecated)."),
10584 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
10585 Show the maximum number of bytes per memory write packet (deprecated)."),
10587 add_cmd ("memory-write-packet-size", no_class,
10588 set_memory_write_packet_size, _("\
10589 Set the maximum number of bytes per memory-write packet.\n\
10590 Specify the number of bytes in a packet or 0 (zero) for the\n\
10591 default packet size. The actual limit is further reduced\n\
10592 dependent on the target. Specify ``fixed'' to disable the\n\
10593 further restriction and ``limit'' to enable that restriction."),
10594 &remote_set_cmdlist);
10595 add_cmd ("memory-read-packet-size", no_class,
10596 set_memory_read_packet_size, _("\
10597 Set the maximum number of bytes per memory-read packet.\n\
10598 Specify the number of bytes in a packet or 0 (zero) for the\n\
10599 default packet size. The actual limit is further reduced\n\
10600 dependent on the target. Specify ``fixed'' to disable the\n\
10601 further restriction and ``limit'' to enable that restriction."),
10602 &remote_set_cmdlist);
10603 add_cmd ("memory-write-packet-size", no_class,
10604 show_memory_write_packet_size,
10605 _("Show the maximum number of bytes per memory-write packet."),
10606 &remote_show_cmdlist);
10607 add_cmd ("memory-read-packet-size", no_class,
10608 show_memory_read_packet_size,
10609 _("Show the maximum number of bytes per memory-read packet."),
10610 &remote_show_cmdlist);
10612 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
10613 &remote_hw_watchpoint_limit, _("\
10614 Set the maximum number of target hardware watchpoints."), _("\
10615 Show the maximum number of target hardware watchpoints."), _("\
10616 Specify a negative limit for unlimited."),
10617 NULL, NULL, /* FIXME: i18n: The maximum
10618 number of target hardware
10619 watchpoints is %s. */
10620 &remote_set_cmdlist, &remote_show_cmdlist);
10621 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
10622 &remote_hw_breakpoint_limit, _("\
10623 Set the maximum number of target hardware breakpoints."), _("\
10624 Show the maximum number of target hardware breakpoints."), _("\
10625 Specify a negative limit for unlimited."),
10626 NULL, NULL, /* FIXME: i18n: The maximum
10627 number of target hardware
10628 breakpoints is %s. */
10629 &remote_set_cmdlist, &remote_show_cmdlist);
10631 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
10632 &remote_address_size, _("\
10633 Set the maximum size of the address (in bits) in a memory packet."), _("\
10634 Show the maximum size of the address (in bits) in a memory packet."), NULL,
10636 NULL, /* FIXME: i18n: */
10637 &setlist, &showlist);
10639 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
10640 "X", "binary-download", 1);
10642 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
10643 "vCont", "verbose-resume", 0);
10645 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
10646 "QPassSignals", "pass-signals", 0);
10648 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
10649 "qSymbol", "symbol-lookup", 0);
10651 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
10652 "P", "set-register", 1);
10654 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
10655 "p", "fetch-register", 1);
10657 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
10658 "Z0", "software-breakpoint", 0);
10660 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
10661 "Z1", "hardware-breakpoint", 0);
10663 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
10664 "Z2", "write-watchpoint", 0);
10666 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
10667 "Z3", "read-watchpoint", 0);
10669 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
10670 "Z4", "access-watchpoint", 0);
10672 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
10673 "qXfer:auxv:read", "read-aux-vector", 0);
10675 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
10676 "qXfer:features:read", "target-features", 0);
10678 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
10679 "qXfer:libraries:read", "library-info", 0);
10681 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
10682 "qXfer:memory-map:read", "memory-map", 0);
10684 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
10685 "qXfer:spu:read", "read-spu-object", 0);
10687 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
10688 "qXfer:spu:write", "write-spu-object", 0);
10690 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
10691 "qXfer:osdata:read", "osdata", 0);
10693 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
10694 "qXfer:threads:read", "threads", 0);
10696 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
10697 "qXfer:siginfo:read", "read-siginfo-object", 0);
10699 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
10700 "qXfer:siginfo:write", "write-siginfo-object", 0);
10702 add_packet_config_cmd
10703 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
10704 "qXfer:trace-frame-info:read", "traceframe-info", 0);
10706 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
10707 "qGetTLSAddr", "get-thread-local-storage-address",
10710 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
10711 "qGetTIBAddr", "get-thread-information-block-address",
10714 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
10715 "bc", "reverse-continue", 0);
10717 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
10718 "bs", "reverse-step", 0);
10720 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
10721 "qSupported", "supported-packets", 0);
10723 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
10724 "qSearch:memory", "search-memory", 0);
10726 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
10727 "vFile:open", "hostio-open", 0);
10729 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
10730 "vFile:pread", "hostio-pread", 0);
10732 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
10733 "vFile:pwrite", "hostio-pwrite", 0);
10735 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
10736 "vFile:close", "hostio-close", 0);
10738 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
10739 "vFile:unlink", "hostio-unlink", 0);
10741 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
10742 "vAttach", "attach", 0);
10744 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
10747 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
10748 "QStartNoAckMode", "noack", 0);
10750 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
10751 "vKill", "kill", 0);
10753 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
10754 "qAttached", "query-attached", 0);
10756 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
10757 "ConditionalTracepoints",
10758 "conditional-tracepoints", 0);
10759 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
10760 "FastTracepoints", "fast-tracepoints", 0);
10762 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
10763 "TracepointSource", "TracepointSource", 0);
10765 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
10766 "QAllow", "allow", 0);
10768 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
10769 "StaticTracepoints", "static-tracepoints", 0);
10771 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
10772 "qXfer:statictrace:read", "read-sdata-object", 0);
10774 /* Keep the old ``set remote Z-packet ...'' working. Each individual
10775 Z sub-packet has its own set and show commands, but users may
10776 have sets to this variable in their .gdbinit files (or in their
10778 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
10779 &remote_Z_packet_detect, _("\
10780 Set use of remote protocol `Z' packets"), _("\
10781 Show use of remote protocol `Z' packets "), _("\
10782 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
10784 set_remote_protocol_Z_packet_cmd,
10785 show_remote_protocol_Z_packet_cmd,
10786 /* FIXME: i18n: Use of remote protocol
10787 `Z' packets is %s. */
10788 &remote_set_cmdlist, &remote_show_cmdlist);
10790 add_prefix_cmd ("remote", class_files, remote_command, _("\
10791 Manipulate files on the remote system\n\
10792 Transfer files to and from the remote target system."),
10793 &remote_cmdlist, "remote ",
10794 0 /* allow-unknown */, &cmdlist);
10796 add_cmd ("put", class_files, remote_put_command,
10797 _("Copy a local file to the remote system."),
10800 add_cmd ("get", class_files, remote_get_command,
10801 _("Copy a remote file to the local system."),
10804 add_cmd ("delete", class_files, remote_delete_command,
10805 _("Delete a remote file."),
10808 remote_exec_file = xstrdup ("");
10809 add_setshow_string_noescape_cmd ("exec-file", class_files,
10810 &remote_exec_file, _("\
10811 Set the remote pathname for \"run\""), _("\
10812 Show the remote pathname for \"run\""), NULL, NULL, NULL,
10813 &remote_set_cmdlist, &remote_show_cmdlist);
10815 /* Eventually initialize fileio. See fileio.c */
10816 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
10818 /* Take advantage of the fact that the LWP field is not used, to tag
10819 special ptids with it set to != 0. */
10820 magic_null_ptid = ptid_build (42000, 1, -1);
10821 not_sent_ptid = ptid_build (42000, 1, -2);
10822 any_thread_ptid = ptid_build (42000, 1, 0);
10824 target_buf_size = 2048;
10825 target_buf = xmalloc (target_buf_size);