1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
30 /*#include "terminal.h" */
33 #include "gdb-stabs.h"
34 #include "gdbthread.h"
36 #include "remote-notif.h"
41 #include "cli/cli-decode.h"
42 #include "cli/cli-setshow.h"
43 #include "target-descriptions.h"
45 #include "filestuff.h"
50 #include "event-loop.h"
51 #include "event-top.h"
57 #include "gdbcore.h" /* for exec_bfd */
59 #include "remote-fileio.h"
60 #include "gdb/fileio.h"
62 #include "xml-support.h"
64 #include "memory-map.h"
66 #include "tracepoint.h"
72 /* Temp hacks for tracepoint encoding migration. */
73 static char *target_buf;
74 static long target_buf_size;
76 /* The size to align memory write packets, when practical. The protocol
77 does not guarantee any alignment, and gdb will generate short
78 writes and unaligned writes, but even as a best-effort attempt this
79 can improve bulk transfers. For instance, if a write is misaligned
80 relative to the target's data bus, the stub may need to make an extra
81 round trip fetching data from the target. This doesn't make a
82 huge difference, but it's easy to do, so we try to be helpful.
84 The alignment chosen is arbitrary; usually data bus width is
85 important here, not the possibly larger cache line size. */
86 enum { REMOTE_ALIGN_WRITES = 16 };
88 /* Prototypes for local functions. */
89 static void async_cleanup_sigint_signal_handler (void *dummy);
90 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
91 static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
92 int forever, int *is_notif);
94 static void async_handle_remote_sigint (int);
95 static void async_handle_remote_sigint_twice (int);
97 static void remote_files_info (struct target_ops *ignore);
99 static void remote_prepare_to_store (struct target_ops *self,
100 struct regcache *regcache);
102 static void remote_open_1 (const char *, int, struct target_ops *,
105 static void remote_close (struct target_ops *self);
107 static void remote_mourn (struct target_ops *ops);
109 static void extended_remote_restart (void);
111 static void extended_remote_mourn (struct target_ops *);
113 static void remote_mourn_1 (struct target_ops *);
115 static void remote_send (char **buf, long *sizeof_buf_p);
117 static int readchar (int timeout);
119 static void remote_serial_write (const char *str, int len);
121 static void remote_kill (struct target_ops *ops);
123 static int remote_can_async_p (struct target_ops *);
125 static int remote_is_async_p (struct target_ops *);
127 static void remote_async (struct target_ops *ops,
128 void (*callback) (enum inferior_event_type event_type,
132 static void sync_remote_interrupt_twice (int signo);
134 static void interrupt_query (void);
136 static void set_general_thread (struct ptid ptid);
137 static void set_continue_thread (struct ptid ptid);
139 static void get_offsets (void);
141 static void skip_frame (void);
143 static long read_frame (char **buf_p, long *sizeof_buf);
145 static int hexnumlen (ULONGEST num);
147 static void init_remote_ops (void);
149 static void init_extended_remote_ops (void);
151 static void remote_stop (struct target_ops *self, ptid_t);
153 static int stubhex (int ch);
155 static int hexnumstr (char *, ULONGEST);
157 static int hexnumnstr (char *, ULONGEST, int);
159 static CORE_ADDR remote_address_masked (CORE_ADDR);
161 static void print_packet (const char *);
163 static void compare_sections_command (char *, int);
165 static void packet_command (char *, int);
167 static int stub_unpack_int (char *buff, int fieldlength);
169 static ptid_t remote_current_thread (ptid_t oldptid);
171 static int putpkt_binary (const char *buf, int cnt);
173 static void check_binary_download (CORE_ADDR addr);
175 struct packet_config;
177 static void show_packet_config_cmd (struct packet_config *config);
179 static void show_remote_protocol_packet_cmd (struct ui_file *file,
181 struct cmd_list_element *c,
184 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
185 static ptid_t read_ptid (char *buf, char **obuf);
187 static void remote_set_permissions (struct target_ops *self);
190 static int remote_get_trace_status (struct target_ops *self,
191 struct trace_status *ts);
193 static int remote_upload_tracepoints (struct target_ops *self,
194 struct uploaded_tp **utpp);
196 static int remote_upload_trace_state_variables (struct target_ops *self,
197 struct uploaded_tsv **utsvp);
199 static void remote_query_supported (void);
201 static void remote_check_symbols (void);
203 void _initialize_remote (void);
206 static void stop_reply_xfree (struct stop_reply *);
207 static void remote_parse_stop_reply (char *, struct stop_reply *);
208 static void push_stop_reply (struct stop_reply *);
209 static void discard_pending_stop_replies_in_queue (struct remote_state *);
210 static int peek_stop_reply (ptid_t ptid);
212 static void remote_async_inferior_event_handler (gdb_client_data);
214 static void remote_terminal_ours (struct target_ops *self);
216 static int remote_read_description_p (struct target_ops *target);
218 static void remote_console_output (char *msg);
220 static int remote_supports_cond_breakpoints (struct target_ops *self);
222 static int remote_can_run_breakpoint_commands (struct target_ops *self);
224 static void remote_btrace_reset (void);
228 static struct cmd_list_element *remote_cmdlist;
230 /* For "set remote" and "show remote". */
232 static struct cmd_list_element *remote_set_cmdlist;
233 static struct cmd_list_element *remote_show_cmdlist;
235 /* Stub vCont actions support.
237 Each field is a boolean flag indicating whether the stub reports
238 support for the corresponding action. */
240 struct vCont_action_support
249 /* Controls whether GDB is willing to use range stepping. */
251 static int use_range_stepping = 1;
253 #define OPAQUETHREADBYTES 8
255 /* a 64 bit opaque identifier */
256 typedef unsigned char threadref[OPAQUETHREADBYTES];
258 /* About this many threadisds fit in a packet. */
260 #define MAXTHREADLISTRESULTS 32
262 /* Description of the remote protocol state for the currently
263 connected target. This is per-target state, and independent of the
264 selected architecture. */
268 /* A buffer to use for incoming packets, and its current size. The
269 buffer is grown dynamically for larger incoming packets.
270 Outgoing packets may also be constructed in this buffer.
271 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
272 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
277 /* True if we're going through initial connection setup (finding out
278 about the remote side's threads, relocating symbols, etc.). */
281 /* If we negotiated packet size explicitly (and thus can bypass
282 heuristics for the largest packet size that will not overflow
283 a buffer in the stub), this will be set to that packet size.
284 Otherwise zero, meaning to use the guessed size. */
285 long explicit_packet_size;
287 /* remote_wait is normally called when the target is running and
288 waits for a stop reply packet. But sometimes we need to call it
289 when the target is already stopped. We can send a "?" packet
290 and have remote_wait read the response. Or, if we already have
291 the response, we can stash it in BUF and tell remote_wait to
292 skip calling getpkt. This flag is set when BUF contains a
293 stop reply packet and the target is not waiting. */
294 int cached_wait_status;
296 /* True, if in no ack mode. That is, neither GDB nor the stub will
297 expect acks from each other. The connection is assumed to be
301 /* True if we're connected in extended remote mode. */
304 /* True if we resumed the target and we're waiting for the target to
305 stop. In the mean time, we can't start another command/query.
306 The remote server wouldn't be ready to process it, so we'd
307 timeout waiting for a reply that would never come and eventually
308 we'd close the connection. This can happen in asynchronous mode
309 because we allow GDB commands while the target is running. */
310 int waiting_for_stop_reply;
312 /* The status of the stub support for the various vCont actions. */
313 struct vCont_action_support supports_vCont;
315 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
316 responded to that. */
319 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
320 remote_open knows that we don't have a file open when the program
322 struct serial *remote_desc;
324 /* These are the threads which we last sent to the remote system. The
325 TID member will be -1 for all or -2 for not sent yet. */
326 ptid_t general_thread;
327 ptid_t continue_thread;
329 /* This is the traceframe which we last selected on the remote system.
330 It will be -1 if no traceframe is selected. */
331 int remote_traceframe_number;
333 char *last_pass_packet;
335 /* The last QProgramSignals packet sent to the target. We bypass
336 sending a new program signals list down to the target if the new
337 packet is exactly the same as the last we sent. IOW, we only let
338 the target know about program signals list changes. */
339 char *last_program_signals_packet;
341 enum gdb_signal last_sent_signal;
345 char *finished_object;
346 char *finished_annex;
347 ULONGEST finished_offset;
349 /* Should we try the 'ThreadInfo' query packet?
351 This variable (NOT available to the user: auto-detect only!)
352 determines whether GDB will use the new, simpler "ThreadInfo"
353 query or the older, more complex syntax for thread queries.
354 This is an auto-detect variable (set to true at each connect,
355 and set to false when the target fails to recognize it). */
356 int use_threadinfo_query;
357 int use_threadextra_query;
359 void (*async_client_callback) (enum inferior_event_type event_type,
361 void *async_client_context;
363 /* This is set to the data address of the access causing the target
364 to stop for a watchpoint. */
365 CORE_ADDR remote_watch_data_address;
367 /* This is non-zero if target stopped for a watchpoint. */
368 int remote_stopped_by_watchpoint_p;
370 threadref echo_nextthread;
371 threadref nextthread;
372 threadref resultthreadlist[MAXTHREADLISTRESULTS];
374 /* The state of remote notification. */
375 struct remote_notif_state *notif_state;
377 /* The branch trace configuration. */
378 struct btrace_config btrace_config;
381 /* Private data that we'll store in (struct thread_info)->private. */
382 struct private_thread_info
389 free_private_thread_info (struct private_thread_info *info)
395 /* This data could be associated with a target, but we do not always
396 have access to the current target when we need it, so for now it is
397 static. This will be fine for as long as only one target is in use
399 static struct remote_state *remote_state;
401 static struct remote_state *
402 get_remote_state_raw (void)
407 /* Allocate a new struct remote_state with xmalloc, initialize it, and
410 static struct remote_state *
411 new_remote_state (void)
413 struct remote_state *result = XCNEW (struct remote_state);
415 /* The default buffer size is unimportant; it will be expanded
416 whenever a larger buffer is needed. */
417 result->buf_size = 400;
418 result->buf = xmalloc (result->buf_size);
419 result->remote_traceframe_number = -1;
420 result->last_sent_signal = GDB_SIGNAL_0;
425 /* Description of the remote protocol for a given architecture. */
429 long offset; /* Offset into G packet. */
430 long regnum; /* GDB's internal register number. */
431 LONGEST pnum; /* Remote protocol register number. */
432 int in_g_packet; /* Always part of G packet. */
433 /* long size in bytes; == register_size (target_gdbarch (), regnum);
435 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
439 struct remote_arch_state
441 /* Description of the remote protocol registers. */
442 long sizeof_g_packet;
444 /* Description of the remote protocol registers indexed by REGNUM
445 (making an array gdbarch_num_regs in size). */
446 struct packet_reg *regs;
448 /* This is the size (in chars) of the first response to the ``g''
449 packet. It is used as a heuristic when determining the maximum
450 size of memory-read and memory-write packets. A target will
451 typically only reserve a buffer large enough to hold the ``g''
452 packet. The size does not include packet overhead (headers and
454 long actual_register_packet_size;
456 /* This is the maximum size (in chars) of a non read/write packet.
457 It is also used as a cap on the size of read/write packets. */
458 long remote_packet_size;
461 /* Utility: generate error from an incoming stub packet. */
463 trace_error (char *buf)
466 return; /* not an error msg */
469 case '1': /* malformed packet error */
470 if (*++buf == '0') /* general case: */
471 error (_("remote.c: error in outgoing packet."));
473 error (_("remote.c: error in outgoing packet at field #%ld."),
474 strtol (buf, NULL, 16));
476 error (_("Target returns error code '%s'."), buf);
480 /* Utility: wait for reply from stub, while accepting "O" packets. */
482 remote_get_noisy_reply (char **buf_p,
485 do /* Loop on reply from remote stub. */
489 QUIT; /* Allow user to bail out with ^C. */
490 getpkt (buf_p, sizeof_buf, 0);
494 else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
497 CORE_ADDR from, to, org_to;
499 int adjusted_size = 0;
500 volatile struct gdb_exception ex;
502 p = buf + strlen ("qRelocInsn:");
503 pp = unpack_varlen_hex (p, &ul);
505 error (_("invalid qRelocInsn packet: %s"), buf);
509 unpack_varlen_hex (p, &ul);
514 TRY_CATCH (ex, RETURN_MASK_ALL)
516 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
520 adjusted_size = to - org_to;
522 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
525 else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
527 /* Propagate memory errors silently back to the target.
528 The stub may have limited the range of addresses we
529 can write to, for example. */
534 /* Something unexpectedly bad happened. Be verbose so
535 we can tell what, and propagate the error back to the
536 stub, so it doesn't get stuck waiting for a
538 exception_fprintf (gdb_stderr, ex,
539 _("warning: relocating instruction: "));
543 else if (buf[0] == 'O' && buf[1] != 'K')
544 remote_console_output (buf + 1); /* 'O' message from stub */
546 return buf; /* Here's the actual reply. */
551 /* Handle for retreving the remote protocol data from gdbarch. */
552 static struct gdbarch_data *remote_gdbarch_data_handle;
554 static struct remote_arch_state *
555 get_remote_arch_state (void)
557 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
560 /* Fetch the global remote target state. */
562 static struct remote_state *
563 get_remote_state (void)
565 /* Make sure that the remote architecture state has been
566 initialized, because doing so might reallocate rs->buf. Any
567 function which calls getpkt also needs to be mindful of changes
568 to rs->buf, but this call limits the number of places which run
570 get_remote_arch_state ();
572 return get_remote_state_raw ();
576 compare_pnums (const void *lhs_, const void *rhs_)
578 const struct packet_reg * const *lhs = lhs_;
579 const struct packet_reg * const *rhs = rhs_;
581 if ((*lhs)->pnum < (*rhs)->pnum)
583 else if ((*lhs)->pnum == (*rhs)->pnum)
590 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
592 int regnum, num_remote_regs, offset;
593 struct packet_reg **remote_regs;
595 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
597 struct packet_reg *r = ®s[regnum];
599 if (register_size (gdbarch, regnum) == 0)
600 /* Do not try to fetch zero-sized (placeholder) registers. */
603 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
608 /* Define the g/G packet format as the contents of each register
609 with a remote protocol number, in order of ascending protocol
612 remote_regs = alloca (gdbarch_num_regs (gdbarch)
613 * sizeof (struct packet_reg *));
614 for (num_remote_regs = 0, regnum = 0;
615 regnum < gdbarch_num_regs (gdbarch);
617 if (regs[regnum].pnum != -1)
618 remote_regs[num_remote_regs++] = ®s[regnum];
620 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
623 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
625 remote_regs[regnum]->in_g_packet = 1;
626 remote_regs[regnum]->offset = offset;
627 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
633 /* Given the architecture described by GDBARCH, return the remote
634 protocol register's number and the register's offset in the g/G
635 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
636 If the target does not have a mapping for REGNUM, return false,
637 otherwise, return true. */
640 remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
641 int *pnum, int *poffset)
644 struct packet_reg *regs;
645 struct cleanup *old_chain;
647 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
649 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
650 old_chain = make_cleanup (xfree, regs);
652 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
654 *pnum = regs[regnum].pnum;
655 *poffset = regs[regnum].offset;
657 do_cleanups (old_chain);
663 init_remote_state (struct gdbarch *gdbarch)
665 struct remote_state *rs = get_remote_state_raw ();
666 struct remote_arch_state *rsa;
668 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
670 /* Use the architecture to build a regnum<->pnum table, which will be
671 1:1 unless a feature set specifies otherwise. */
672 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
673 gdbarch_num_regs (gdbarch),
676 /* Record the maximum possible size of the g packet - it may turn out
678 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
680 /* Default maximum number of characters in a packet body. Many
681 remote stubs have a hardwired buffer size of 400 bytes
682 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
683 as the maximum packet-size to ensure that the packet and an extra
684 NUL character can always fit in the buffer. This stops GDB
685 trashing stubs that try to squeeze an extra NUL into what is
686 already a full buffer (As of 1999-12-04 that was most stubs). */
687 rsa->remote_packet_size = 400 - 1;
689 /* This one is filled in when a ``g'' packet is received. */
690 rsa->actual_register_packet_size = 0;
692 /* Should rsa->sizeof_g_packet needs more space than the
693 default, adjust the size accordingly. Remember that each byte is
694 encoded as two characters. 32 is the overhead for the packet
695 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
696 (``$NN:G...#NN'') is a better guess, the below has been padded a
698 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
699 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
701 /* Make sure that the packet buffer is plenty big enough for
702 this architecture. */
703 if (rs->buf_size < rsa->remote_packet_size)
705 rs->buf_size = 2 * rsa->remote_packet_size;
706 rs->buf = xrealloc (rs->buf, rs->buf_size);
712 /* Return the current allowed size of a remote packet. This is
713 inferred from the current architecture, and should be used to
714 limit the length of outgoing packets. */
716 get_remote_packet_size (void)
718 struct remote_state *rs = get_remote_state ();
719 struct remote_arch_state *rsa = get_remote_arch_state ();
721 if (rs->explicit_packet_size)
722 return rs->explicit_packet_size;
724 return rsa->remote_packet_size;
727 static struct packet_reg *
728 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
730 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
734 struct packet_reg *r = &rsa->regs[regnum];
736 gdb_assert (r->regnum == regnum);
741 static struct packet_reg *
742 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
746 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
748 struct packet_reg *r = &rsa->regs[i];
756 static struct target_ops remote_ops;
758 static struct target_ops extended_remote_ops;
760 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
761 ``forever'' still use the normal timeout mechanism. This is
762 currently used by the ASYNC code to guarentee that target reads
763 during the initial connect always time-out. Once getpkt has been
764 modified to return a timeout indication and, in turn
765 remote_wait()/wait_for_inferior() have gained a timeout parameter
767 static int wait_forever_enabled_p = 1;
769 /* Allow the user to specify what sequence to send to the remote
770 when he requests a program interruption: Although ^C is usually
771 what remote systems expect (this is the default, here), it is
772 sometimes preferable to send a break. On other systems such
773 as the Linux kernel, a break followed by g, which is Magic SysRq g
774 is required in order to interrupt the execution. */
775 const char interrupt_sequence_control_c[] = "Ctrl-C";
776 const char interrupt_sequence_break[] = "BREAK";
777 const char interrupt_sequence_break_g[] = "BREAK-g";
778 static const char *const interrupt_sequence_modes[] =
780 interrupt_sequence_control_c,
781 interrupt_sequence_break,
782 interrupt_sequence_break_g,
785 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
788 show_interrupt_sequence (struct ui_file *file, int from_tty,
789 struct cmd_list_element *c,
792 if (interrupt_sequence_mode == interrupt_sequence_control_c)
793 fprintf_filtered (file,
794 _("Send the ASCII ETX character (Ctrl-c) "
795 "to the remote target to interrupt the "
796 "execution of the program.\n"));
797 else if (interrupt_sequence_mode == interrupt_sequence_break)
798 fprintf_filtered (file,
799 _("send a break signal to the remote target "
800 "to interrupt the execution of the program.\n"));
801 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
802 fprintf_filtered (file,
803 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
804 "the remote target to interrupt the execution "
805 "of Linux kernel.\n"));
807 internal_error (__FILE__, __LINE__,
808 _("Invalid value for interrupt_sequence_mode: %s."),
809 interrupt_sequence_mode);
812 /* This boolean variable specifies whether interrupt_sequence is sent
813 to the remote target when gdb connects to it.
814 This is mostly needed when you debug the Linux kernel: The Linux kernel
815 expects BREAK g which is Magic SysRq g for connecting gdb. */
816 static int interrupt_on_connect = 0;
818 /* This variable is used to implement the "set/show remotebreak" commands.
819 Since these commands are now deprecated in favor of "set/show remote
820 interrupt-sequence", it no longer has any effect on the code. */
821 static int remote_break;
824 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
827 interrupt_sequence_mode = interrupt_sequence_break;
829 interrupt_sequence_mode = interrupt_sequence_control_c;
833 show_remotebreak (struct ui_file *file, int from_tty,
834 struct cmd_list_element *c,
839 /* This variable sets the number of bits in an address that are to be
840 sent in a memory ("M" or "m") packet. Normally, after stripping
841 leading zeros, the entire address would be sent. This variable
842 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
843 initial implementation of remote.c restricted the address sent in
844 memory packets to ``host::sizeof long'' bytes - (typically 32
845 bits). Consequently, for 64 bit targets, the upper 32 bits of an
846 address was never sent. Since fixing this bug may cause a break in
847 some remote targets this variable is principly provided to
848 facilitate backward compatibility. */
850 static unsigned int remote_address_size;
852 /* Temporary to track who currently owns the terminal. See
853 remote_terminal_* for more details. */
855 static int remote_async_terminal_ours_p;
857 /* The executable file to use for "run" on the remote side. */
859 static char *remote_exec_file = "";
862 /* User configurable variables for the number of characters in a
863 memory read/write packet. MIN (rsa->remote_packet_size,
864 rsa->sizeof_g_packet) is the default. Some targets need smaller
865 values (fifo overruns, et.al.) and some users need larger values
866 (speed up transfers). The variables ``preferred_*'' (the user
867 request), ``current_*'' (what was actually set) and ``forced_*''
868 (Positive - a soft limit, negative - a hard limit). */
870 struct memory_packet_config
877 /* Compute the current size of a read/write packet. Since this makes
878 use of ``actual_register_packet_size'' the computation is dynamic. */
881 get_memory_packet_size (struct memory_packet_config *config)
883 struct remote_state *rs = get_remote_state ();
884 struct remote_arch_state *rsa = get_remote_arch_state ();
886 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
887 law?) that some hosts don't cope very well with large alloca()
888 calls. Eventually the alloca() code will be replaced by calls to
889 xmalloc() and make_cleanups() allowing this restriction to either
890 be lifted or removed. */
891 #ifndef MAX_REMOTE_PACKET_SIZE
892 #define MAX_REMOTE_PACKET_SIZE 16384
894 /* NOTE: 20 ensures we can write at least one byte. */
895 #ifndef MIN_REMOTE_PACKET_SIZE
896 #define MIN_REMOTE_PACKET_SIZE 20
901 if (config->size <= 0)
902 what_they_get = MAX_REMOTE_PACKET_SIZE;
904 what_they_get = config->size;
908 what_they_get = get_remote_packet_size ();
909 /* Limit the packet to the size specified by the user. */
911 && what_they_get > config->size)
912 what_they_get = config->size;
914 /* Limit it to the size of the targets ``g'' response unless we have
915 permission from the stub to use a larger packet size. */
916 if (rs->explicit_packet_size == 0
917 && rsa->actual_register_packet_size > 0
918 && what_they_get > rsa->actual_register_packet_size)
919 what_they_get = rsa->actual_register_packet_size;
921 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
922 what_they_get = MAX_REMOTE_PACKET_SIZE;
923 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
924 what_they_get = MIN_REMOTE_PACKET_SIZE;
926 /* Make sure there is room in the global buffer for this packet
927 (including its trailing NUL byte). */
928 if (rs->buf_size < what_they_get + 1)
930 rs->buf_size = 2 * what_they_get;
931 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
934 return what_they_get;
937 /* Update the size of a read/write packet. If they user wants
938 something really big then do a sanity check. */
941 set_memory_packet_size (char *args, struct memory_packet_config *config)
943 int fixed_p = config->fixed_p;
944 long size = config->size;
947 error (_("Argument required (integer, `fixed' or `limited')."));
948 else if (strcmp (args, "hard") == 0
949 || strcmp (args, "fixed") == 0)
951 else if (strcmp (args, "soft") == 0
952 || strcmp (args, "limit") == 0)
958 size = strtoul (args, &end, 0);
960 error (_("Invalid %s (bad syntax)."), config->name);
962 /* Instead of explicitly capping the size of a packet to
963 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
964 instead allowed to set the size to something arbitrarily
966 if (size > MAX_REMOTE_PACKET_SIZE)
967 error (_("Invalid %s (too large)."), config->name);
971 if (fixed_p && !config->fixed_p)
973 if (! query (_("The target may not be able to correctly handle a %s\n"
974 "of %ld bytes. Change the packet size? "),
976 error (_("Packet size not changed."));
978 /* Update the config. */
979 config->fixed_p = fixed_p;
984 show_memory_packet_size (struct memory_packet_config *config)
986 printf_filtered (_("The %s is %ld. "), config->name, config->size);
988 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
989 get_memory_packet_size (config));
991 printf_filtered (_("Packets are limited to %ld bytes.\n"),
992 get_memory_packet_size (config));
995 static struct memory_packet_config memory_write_packet_config =
997 "memory-write-packet-size",
1001 set_memory_write_packet_size (char *args, int from_tty)
1003 set_memory_packet_size (args, &memory_write_packet_config);
1007 show_memory_write_packet_size (char *args, int from_tty)
1009 show_memory_packet_size (&memory_write_packet_config);
1013 get_memory_write_packet_size (void)
1015 return get_memory_packet_size (&memory_write_packet_config);
1018 static struct memory_packet_config memory_read_packet_config =
1020 "memory-read-packet-size",
1024 set_memory_read_packet_size (char *args, int from_tty)
1026 set_memory_packet_size (args, &memory_read_packet_config);
1030 show_memory_read_packet_size (char *args, int from_tty)
1032 show_memory_packet_size (&memory_read_packet_config);
1036 get_memory_read_packet_size (void)
1038 long size = get_memory_packet_size (&memory_read_packet_config);
1040 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1041 extra buffer size argument before the memory read size can be
1042 increased beyond this. */
1043 if (size > get_remote_packet_size ())
1044 size = get_remote_packet_size ();
1049 /* Generic configuration support for packets the stub optionally
1050 supports. Allows the user to specify the use of the packet as well
1051 as allowing GDB to auto-detect support in the remote stub. */
1055 PACKET_SUPPORT_UNKNOWN = 0,
1060 struct packet_config
1065 /* If auto, GDB auto-detects support for this packet or feature,
1066 either through qSupported, or by trying the packet and looking
1067 at the response. If true, GDB assumes the target supports this
1068 packet. If false, the packet is disabled. Configs that don't
1069 have an associated command always have this set to auto. */
1070 enum auto_boolean detect;
1072 /* Does the target support this packet? */
1073 enum packet_support support;
1076 /* Analyze a packet's return value and update the packet config
1086 static enum packet_support packet_config_support (struct packet_config *config);
1087 static enum packet_support packet_support (int packet);
1090 show_packet_config_cmd (struct packet_config *config)
1092 char *support = "internal-error";
1094 switch (packet_config_support (config))
1097 support = "enabled";
1099 case PACKET_DISABLE:
1100 support = "disabled";
1102 case PACKET_SUPPORT_UNKNOWN:
1103 support = "unknown";
1106 switch (config->detect)
1108 case AUTO_BOOLEAN_AUTO:
1109 printf_filtered (_("Support for the `%s' packet "
1110 "is auto-detected, currently %s.\n"),
1111 config->name, support);
1113 case AUTO_BOOLEAN_TRUE:
1114 case AUTO_BOOLEAN_FALSE:
1115 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1116 config->name, support);
1122 add_packet_config_cmd (struct packet_config *config, const char *name,
1123 const char *title, int legacy)
1129 config->name = name;
1130 config->title = title;
1131 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1133 show_doc = xstrprintf ("Show current use of remote "
1134 "protocol `%s' (%s) packet",
1136 /* set/show TITLE-packet {auto,on,off} */
1137 cmd_name = xstrprintf ("%s-packet", title);
1138 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1139 &config->detect, set_doc,
1140 show_doc, NULL, /* help_doc */
1142 show_remote_protocol_packet_cmd,
1143 &remote_set_cmdlist, &remote_show_cmdlist);
1144 /* The command code copies the documentation strings. */
1147 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1152 legacy_name = xstrprintf ("%s-packet", name);
1153 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1154 &remote_set_cmdlist);
1155 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1156 &remote_show_cmdlist);
1160 static enum packet_result
1161 packet_check_result (const char *buf)
1165 /* The stub recognized the packet request. Check that the
1166 operation succeeded. */
1168 && isxdigit (buf[1]) && isxdigit (buf[2])
1170 /* "Enn" - definitly an error. */
1171 return PACKET_ERROR;
1173 /* Always treat "E." as an error. This will be used for
1174 more verbose error messages, such as E.memtypes. */
1175 if (buf[0] == 'E' && buf[1] == '.')
1176 return PACKET_ERROR;
1178 /* The packet may or may not be OK. Just assume it is. */
1182 /* The stub does not support the packet. */
1183 return PACKET_UNKNOWN;
1186 static enum packet_result
1187 packet_ok (const char *buf, struct packet_config *config)
1189 enum packet_result result;
1191 if (config->detect != AUTO_BOOLEAN_TRUE
1192 && config->support == PACKET_DISABLE)
1193 internal_error (__FILE__, __LINE__,
1194 _("packet_ok: attempt to use a disabled packet"));
1196 result = packet_check_result (buf);
1201 /* The stub recognized the packet request. */
1202 if (config->support == PACKET_SUPPORT_UNKNOWN)
1205 fprintf_unfiltered (gdb_stdlog,
1206 "Packet %s (%s) is supported\n",
1207 config->name, config->title);
1208 config->support = PACKET_ENABLE;
1211 case PACKET_UNKNOWN:
1212 /* The stub does not support the packet. */
1213 if (config->detect == AUTO_BOOLEAN_AUTO
1214 && config->support == PACKET_ENABLE)
1216 /* If the stub previously indicated that the packet was
1217 supported then there is a protocol error. */
1218 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1219 config->name, config->title);
1221 else if (config->detect == AUTO_BOOLEAN_TRUE)
1223 /* The user set it wrong. */
1224 error (_("Enabled packet %s (%s) not recognized by stub"),
1225 config->name, config->title);
1229 fprintf_unfiltered (gdb_stdlog,
1230 "Packet %s (%s) is NOT supported\n",
1231 config->name, config->title);
1232 config->support = PACKET_DISABLE;
1252 PACKET_vFile_pwrite,
1254 PACKET_vFile_unlink,
1255 PACKET_vFile_readlink,
1257 PACKET_qXfer_features,
1258 PACKET_qXfer_libraries,
1259 PACKET_qXfer_libraries_svr4,
1260 PACKET_qXfer_memory_map,
1261 PACKET_qXfer_spu_read,
1262 PACKET_qXfer_spu_write,
1263 PACKET_qXfer_osdata,
1264 PACKET_qXfer_threads,
1265 PACKET_qXfer_statictrace_read,
1266 PACKET_qXfer_traceframe_info,
1272 PACKET_QPassSignals,
1273 PACKET_QProgramSignals,
1275 PACKET_qSearch_memory,
1278 PACKET_QStartNoAckMode,
1280 PACKET_qXfer_siginfo_read,
1281 PACKET_qXfer_siginfo_write,
1284 /* Support for conditional tracepoints. */
1285 PACKET_ConditionalTracepoints,
1287 /* Support for target-side breakpoint conditions. */
1288 PACKET_ConditionalBreakpoints,
1290 /* Support for target-side breakpoint commands. */
1291 PACKET_BreakpointCommands,
1293 /* Support for fast tracepoints. */
1294 PACKET_FastTracepoints,
1296 /* Support for static tracepoints. */
1297 PACKET_StaticTracepoints,
1299 /* Support for installing tracepoints while a trace experiment is
1301 PACKET_InstallInTrace,
1305 PACKET_TracepointSource,
1308 PACKET_QDisableRandomization,
1310 PACKET_QTBuffer_size,
1313 PACKET_qXfer_btrace,
1315 /* Support for the QNonStop packet. */
1318 /* Support for multi-process extensions. */
1319 PACKET_multiprocess_feature,
1321 /* Support for enabling and disabling tracepoints while a trace
1322 experiment is running. */
1323 PACKET_EnableDisableTracepoints_feature,
1325 /* Support for collecting strings using the tracenz bytecode. */
1326 PACKET_tracenz_feature,
1328 /* Support for continuing to run a trace experiment while GDB is
1330 PACKET_DisconnectedTracing_feature,
1332 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1333 PACKET_augmented_libraries_svr4_read_feature,
1335 /* Support for the qXfer:btrace-conf:read packet. */
1336 PACKET_qXfer_btrace_conf,
1338 /* Support for the Qbtrace-conf:bts:size packet. */
1339 PACKET_Qbtrace_conf_bts_size,
1344 static struct packet_config remote_protocol_packets[PACKET_MAX];
1346 /* Returns whether a given packet or feature is supported. This takes
1347 into account the state of the corresponding "set remote foo-packet"
1348 command, which may be used to bypass auto-detection. */
1350 static enum packet_support
1351 packet_config_support (struct packet_config *config)
1353 switch (config->detect)
1355 case AUTO_BOOLEAN_TRUE:
1356 return PACKET_ENABLE;
1357 case AUTO_BOOLEAN_FALSE:
1358 return PACKET_DISABLE;
1359 case AUTO_BOOLEAN_AUTO:
1360 return config->support;
1362 gdb_assert_not_reached (_("bad switch"));
1366 /* Same as packet_config_support, but takes the packet's enum value as
1369 static enum packet_support
1370 packet_support (int packet)
1372 struct packet_config *config = &remote_protocol_packets[packet];
1374 return packet_config_support (config);
1378 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1379 struct cmd_list_element *c,
1382 struct packet_config *packet;
1384 for (packet = remote_protocol_packets;
1385 packet < &remote_protocol_packets[PACKET_MAX];
1388 if (&packet->detect == c->var)
1390 show_packet_config_cmd (packet);
1394 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1398 /* Should we try one of the 'Z' requests? */
1402 Z_PACKET_SOFTWARE_BP,
1403 Z_PACKET_HARDWARE_BP,
1410 /* For compatibility with older distributions. Provide a ``set remote
1411 Z-packet ...'' command that updates all the Z packet types. */
1413 static enum auto_boolean remote_Z_packet_detect;
1416 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1417 struct cmd_list_element *c)
1421 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1422 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1426 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1427 struct cmd_list_element *c,
1432 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1434 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1438 /* Returns true if the multi-process extensions are in effect. */
1441 remote_multi_process_p (struct remote_state *rs)
1443 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1446 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1447 static struct async_signal_handler *async_sigint_remote_twice_token;
1448 static struct async_signal_handler *async_sigint_remote_token;
1451 /* Asynchronous signal handle registered as event loop source for
1452 when we have pending events ready to be passed to the core. */
1454 static struct async_event_handler *remote_async_inferior_event_token;
1458 static ptid_t magic_null_ptid;
1459 static ptid_t not_sent_ptid;
1460 static ptid_t any_thread_ptid;
1462 /* Find out if the stub attached to PID (and hence GDB should offer to
1463 detach instead of killing it when bailing out). */
1466 remote_query_attached (int pid)
1468 struct remote_state *rs = get_remote_state ();
1469 size_t size = get_remote_packet_size ();
1471 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
1474 if (remote_multi_process_p (rs))
1475 xsnprintf (rs->buf, size, "qAttached:%x", pid);
1477 xsnprintf (rs->buf, size, "qAttached");
1480 getpkt (&rs->buf, &rs->buf_size, 0);
1482 switch (packet_ok (rs->buf,
1483 &remote_protocol_packets[PACKET_qAttached]))
1486 if (strcmp (rs->buf, "1") == 0)
1490 warning (_("Remote failure reply: %s"), rs->buf);
1492 case PACKET_UNKNOWN:
1499 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1500 has been invented by GDB, instead of reported by the target. Since
1501 we can be connected to a remote system before before knowing about
1502 any inferior, mark the target with execution when we find the first
1503 inferior. If ATTACHED is 1, then we had just attached to this
1504 inferior. If it is 0, then we just created this inferior. If it
1505 is -1, then try querying the remote stub to find out if it had
1506 attached to the inferior or not. */
1508 static struct inferior *
1509 remote_add_inferior (int fake_pid_p, int pid, int attached)
1511 struct inferior *inf;
1513 /* Check whether this process we're learning about is to be
1514 considered attached, or if is to be considered to have been
1515 spawned by the stub. */
1517 attached = remote_query_attached (pid);
1519 if (gdbarch_has_global_solist (target_gdbarch ()))
1521 /* If the target shares code across all inferiors, then every
1522 attach adds a new inferior. */
1523 inf = add_inferior (pid);
1525 /* ... and every inferior is bound to the same program space.
1526 However, each inferior may still have its own address
1528 inf->aspace = maybe_new_address_space ();
1529 inf->pspace = current_program_space;
1533 /* In the traditional debugging scenario, there's a 1-1 match
1534 between program/address spaces. We simply bind the inferior
1535 to the program space's address space. */
1536 inf = current_inferior ();
1537 inferior_appeared (inf, pid);
1540 inf->attach_flag = attached;
1541 inf->fake_pid_p = fake_pid_p;
1546 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1547 according to RUNNING. */
1550 remote_add_thread (ptid_t ptid, int running)
1552 struct remote_state *rs = get_remote_state ();
1554 /* GDB historically didn't pull threads in the initial connection
1555 setup. If the remote target doesn't even have a concept of
1556 threads (e.g., a bare-metal target), even if internally we
1557 consider that a single-threaded target, mentioning a new thread
1558 might be confusing to the user. Be silent then, preserving the
1559 age old behavior. */
1560 if (rs->starting_up)
1561 add_thread_silent (ptid);
1565 set_executing (ptid, running);
1566 set_running (ptid, running);
1569 /* Come here when we learn about a thread id from the remote target.
1570 It may be the first time we hear about such thread, so take the
1571 opportunity to add it to GDB's thread list. In case this is the
1572 first time we're noticing its corresponding inferior, add it to
1573 GDB's inferior list as well. */
1576 remote_notice_new_inferior (ptid_t currthread, int running)
1578 /* If this is a new thread, add it to GDB's thread list.
1579 If we leave it up to WFI to do this, bad things will happen. */
1581 if (in_thread_list (currthread) && is_exited (currthread))
1583 /* We're seeing an event on a thread id we knew had exited.
1584 This has to be a new thread reusing the old id. Add it. */
1585 remote_add_thread (currthread, running);
1589 if (!in_thread_list (currthread))
1591 struct inferior *inf = NULL;
1592 int pid = ptid_get_pid (currthread);
1594 if (ptid_is_pid (inferior_ptid)
1595 && pid == ptid_get_pid (inferior_ptid))
1597 /* inferior_ptid has no thread member yet. This can happen
1598 with the vAttach -> remote_wait,"TAAthread:" path if the
1599 stub doesn't support qC. This is the first stop reported
1600 after an attach, so this is the main thread. Update the
1601 ptid in the thread list. */
1602 if (in_thread_list (pid_to_ptid (pid)))
1603 thread_change_ptid (inferior_ptid, currthread);
1606 remote_add_thread (currthread, running);
1607 inferior_ptid = currthread;
1612 if (ptid_equal (magic_null_ptid, inferior_ptid))
1614 /* inferior_ptid is not set yet. This can happen with the
1615 vRun -> remote_wait,"TAAthread:" path if the stub
1616 doesn't support qC. This is the first stop reported
1617 after an attach, so this is the main thread. Update the
1618 ptid in the thread list. */
1619 thread_change_ptid (inferior_ptid, currthread);
1623 /* When connecting to a target remote, or to a target
1624 extended-remote which already was debugging an inferior, we
1625 may not know about it yet. Add it before adding its child
1626 thread, so notifications are emitted in a sensible order. */
1627 if (!in_inferior_list (ptid_get_pid (currthread)))
1629 struct remote_state *rs = get_remote_state ();
1630 int fake_pid_p = !remote_multi_process_p (rs);
1632 inf = remote_add_inferior (fake_pid_p,
1633 ptid_get_pid (currthread), -1);
1636 /* This is really a new thread. Add it. */
1637 remote_add_thread (currthread, running);
1639 /* If we found a new inferior, let the common code do whatever
1640 it needs to with it (e.g., read shared libraries, insert
1641 breakpoints), unless we're just setting up an all-stop
1645 struct remote_state *rs = get_remote_state ();
1647 if (non_stop || !rs->starting_up)
1648 notice_new_inferior (currthread, running, 0);
1653 /* Return the private thread data, creating it if necessary. */
1655 static struct private_thread_info *
1656 demand_private_info (ptid_t ptid)
1658 struct thread_info *info = find_thread_ptid (ptid);
1664 info->private = xmalloc (sizeof (*(info->private)));
1665 info->private_dtor = free_private_thread_info;
1666 info->private->core = -1;
1667 info->private->extra = 0;
1670 return info->private;
1673 /* Call this function as a result of
1674 1) A halt indication (T packet) containing a thread id
1675 2) A direct query of currthread
1676 3) Successful execution of set thread */
1679 record_currthread (struct remote_state *rs, ptid_t currthread)
1681 rs->general_thread = currthread;
1684 /* If 'QPassSignals' is supported, tell the remote stub what signals
1685 it can simply pass through to the inferior without reporting. */
1688 remote_pass_signals (struct target_ops *self,
1689 int numsigs, unsigned char *pass_signals)
1691 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
1693 char *pass_packet, *p;
1695 struct remote_state *rs = get_remote_state ();
1697 gdb_assert (numsigs < 256);
1698 for (i = 0; i < numsigs; i++)
1700 if (pass_signals[i])
1703 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1704 strcpy (pass_packet, "QPassSignals:");
1705 p = pass_packet + strlen (pass_packet);
1706 for (i = 0; i < numsigs; i++)
1708 if (pass_signals[i])
1711 *p++ = tohex (i >> 4);
1712 *p++ = tohex (i & 15);
1721 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
1723 putpkt (pass_packet);
1724 getpkt (&rs->buf, &rs->buf_size, 0);
1725 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
1726 if (rs->last_pass_packet)
1727 xfree (rs->last_pass_packet);
1728 rs->last_pass_packet = pass_packet;
1731 xfree (pass_packet);
1735 /* If 'QProgramSignals' is supported, tell the remote stub what
1736 signals it should pass through to the inferior when detaching. */
1739 remote_program_signals (struct target_ops *self,
1740 int numsigs, unsigned char *signals)
1742 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
1746 struct remote_state *rs = get_remote_state ();
1748 gdb_assert (numsigs < 256);
1749 for (i = 0; i < numsigs; i++)
1754 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1755 strcpy (packet, "QProgramSignals:");
1756 p = packet + strlen (packet);
1757 for (i = 0; i < numsigs; i++)
1759 if (signal_pass_state (i))
1762 *p++ = tohex (i >> 4);
1763 *p++ = tohex (i & 15);
1772 if (!rs->last_program_signals_packet
1773 || strcmp (rs->last_program_signals_packet, packet) != 0)
1776 getpkt (&rs->buf, &rs->buf_size, 0);
1777 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1778 xfree (rs->last_program_signals_packet);
1779 rs->last_program_signals_packet = packet;
1786 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1787 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1788 thread. If GEN is set, set the general thread, if not, then set
1789 the step/continue thread. */
1791 set_thread (struct ptid ptid, int gen)
1793 struct remote_state *rs = get_remote_state ();
1794 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
1795 char *buf = rs->buf;
1796 char *endbuf = rs->buf + get_remote_packet_size ();
1798 if (ptid_equal (state, ptid))
1802 *buf++ = gen ? 'g' : 'c';
1803 if (ptid_equal (ptid, magic_null_ptid))
1804 xsnprintf (buf, endbuf - buf, "0");
1805 else if (ptid_equal (ptid, any_thread_ptid))
1806 xsnprintf (buf, endbuf - buf, "0");
1807 else if (ptid_equal (ptid, minus_one_ptid))
1808 xsnprintf (buf, endbuf - buf, "-1");
1810 write_ptid (buf, endbuf, ptid);
1812 getpkt (&rs->buf, &rs->buf_size, 0);
1814 rs->general_thread = ptid;
1816 rs->continue_thread = ptid;
1820 set_general_thread (struct ptid ptid)
1822 set_thread (ptid, 1);
1826 set_continue_thread (struct ptid ptid)
1828 set_thread (ptid, 0);
1831 /* Change the remote current process. Which thread within the process
1832 ends up selected isn't important, as long as it is the same process
1833 as what INFERIOR_PTID points to.
1835 This comes from that fact that there is no explicit notion of
1836 "selected process" in the protocol. The selected process for
1837 general operations is the process the selected general thread
1841 set_general_process (void)
1843 struct remote_state *rs = get_remote_state ();
1845 /* If the remote can't handle multiple processes, don't bother. */
1846 if (!rs->extended || !remote_multi_process_p (rs))
1849 /* We only need to change the remote current thread if it's pointing
1850 at some other process. */
1851 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
1852 set_general_thread (inferior_ptid);
1856 /* Return nonzero if this is the main thread that we made up ourselves
1857 to model non-threaded targets as single-threaded. */
1860 remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
1862 struct remote_state *rs = get_remote_state ();
1865 if (ptid_equal (ptid, magic_null_ptid))
1866 /* The main thread is always alive. */
1869 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
1870 /* The main thread is always alive. This can happen after a
1871 vAttach, if the remote side doesn't support
1878 /* Return nonzero if the thread PTID is still alive on the remote
1882 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1884 struct remote_state *rs = get_remote_state ();
1887 /* Check if this is a thread that we made up ourselves to model
1888 non-threaded targets as single-threaded. */
1889 if (remote_thread_always_alive (ops, ptid))
1893 endp = rs->buf + get_remote_packet_size ();
1896 write_ptid (p, endp, ptid);
1899 getpkt (&rs->buf, &rs->buf_size, 0);
1900 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
1903 /* About these extended threadlist and threadinfo packets. They are
1904 variable length packets but, the fields within them are often fixed
1905 length. They are redundent enough to send over UDP as is the
1906 remote protocol in general. There is a matching unit test module
1909 /* WARNING: This threadref data structure comes from the remote O.S.,
1910 libstub protocol encoding, and remote.c. It is not particularly
1913 /* Right now, the internal structure is int. We want it to be bigger.
1914 Plan to fix this. */
1916 typedef int gdb_threadref; /* Internal GDB thread reference. */
1918 /* gdb_ext_thread_info is an internal GDB data structure which is
1919 equivalent to the reply of the remote threadinfo packet. */
1921 struct gdb_ext_thread_info
1923 threadref threadid; /* External form of thread reference. */
1924 int active; /* Has state interesting to GDB?
1926 char display[256]; /* Brief state display, name,
1927 blocked/suspended. */
1928 char shortname[32]; /* To be used to name threads. */
1929 char more_display[256]; /* Long info, statistics, queue depth,
1933 /* The volume of remote transfers can be limited by submitting
1934 a mask containing bits specifying the desired information.
1935 Use a union of these values as the 'selection' parameter to
1936 get_thread_info. FIXME: Make these TAG names more thread specific. */
1938 #define TAG_THREADID 1
1939 #define TAG_EXISTS 2
1940 #define TAG_DISPLAY 4
1941 #define TAG_THREADNAME 8
1942 #define TAG_MOREDISPLAY 16
1944 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1946 static char *unpack_nibble (char *buf, int *val);
1948 static char *unpack_byte (char *buf, int *value);
1950 static char *pack_int (char *buf, int value);
1952 static char *unpack_int (char *buf, int *value);
1954 static char *unpack_string (char *src, char *dest, int length);
1956 static char *pack_threadid (char *pkt, threadref *id);
1958 static char *unpack_threadid (char *inbuf, threadref *id);
1960 void int_to_threadref (threadref *id, int value);
1962 static int threadref_to_int (threadref *ref);
1964 static void copy_threadref (threadref *dest, threadref *src);
1966 static int threadmatch (threadref *dest, threadref *src);
1968 static char *pack_threadinfo_request (char *pkt, int mode,
1971 static int remote_unpack_thread_info_response (char *pkt,
1972 threadref *expectedref,
1973 struct gdb_ext_thread_info
1977 static int remote_get_threadinfo (threadref *threadid,
1978 int fieldset, /*TAG mask */
1979 struct gdb_ext_thread_info *info);
1981 static char *pack_threadlist_request (char *pkt, int startflag,
1983 threadref *nextthread);
1985 static int parse_threadlist_response (char *pkt,
1987 threadref *original_echo,
1988 threadref *resultlist,
1991 static int remote_get_threadlist (int startflag,
1992 threadref *nextthread,
1996 threadref *threadlist);
1998 typedef int (*rmt_thread_action) (threadref *ref, void *context);
2000 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2001 void *context, int looplimit);
2003 static int remote_newthread_step (threadref *ref, void *context);
2006 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2007 buffer we're allowed to write to. Returns
2008 BUF+CHARACTERS_WRITTEN. */
2011 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2014 struct remote_state *rs = get_remote_state ();
2016 if (remote_multi_process_p (rs))
2018 pid = ptid_get_pid (ptid);
2020 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2022 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2024 tid = ptid_get_lwp (ptid);
2026 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2028 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2033 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2034 passed the last parsed char. Returns null_ptid on error. */
2037 read_ptid (char *buf, char **obuf)
2041 ULONGEST pid = 0, tid = 0;
2045 /* Multi-process ptid. */
2046 pp = unpack_varlen_hex (p + 1, &pid);
2048 error (_("invalid remote ptid: %s"), p);
2051 pp = unpack_varlen_hex (p + 1, &tid);
2054 return ptid_build (pid, tid, 0);
2057 /* No multi-process. Just a tid. */
2058 pp = unpack_varlen_hex (p, &tid);
2060 /* Since the stub is not sending a process id, then default to
2061 what's in inferior_ptid, unless it's null at this point. If so,
2062 then since there's no way to know the pid of the reported
2063 threads, use the magic number. */
2064 if (ptid_equal (inferior_ptid, null_ptid))
2065 pid = ptid_get_pid (magic_null_ptid);
2067 pid = ptid_get_pid (inferior_ptid);
2071 return ptid_build (pid, tid, 0);
2077 if (ch >= 'a' && ch <= 'f')
2078 return ch - 'a' + 10;
2079 if (ch >= '0' && ch <= '9')
2081 if (ch >= 'A' && ch <= 'F')
2082 return ch - 'A' + 10;
2087 stub_unpack_int (char *buff, int fieldlength)
2094 nibble = stubhex (*buff++);
2098 retval = retval << 4;
2104 unpack_nibble (char *buf, int *val)
2106 *val = fromhex (*buf++);
2111 unpack_byte (char *buf, int *value)
2113 *value = stub_unpack_int (buf, 2);
2118 pack_int (char *buf, int value)
2120 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2121 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2122 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2123 buf = pack_hex_byte (buf, (value & 0xff));
2128 unpack_int (char *buf, int *value)
2130 *value = stub_unpack_int (buf, 8);
2134 #if 0 /* Currently unused, uncomment when needed. */
2135 static char *pack_string (char *pkt, char *string);
2138 pack_string (char *pkt, char *string)
2143 len = strlen (string);
2145 len = 200; /* Bigger than most GDB packets, junk??? */
2146 pkt = pack_hex_byte (pkt, len);
2150 if ((ch == '\0') || (ch == '#'))
2151 ch = '*'; /* Protect encapsulation. */
2156 #endif /* 0 (unused) */
2159 unpack_string (char *src, char *dest, int length)
2168 pack_threadid (char *pkt, threadref *id)
2171 unsigned char *altid;
2173 altid = (unsigned char *) id;
2174 limit = pkt + BUF_THREAD_ID_SIZE;
2176 pkt = pack_hex_byte (pkt, *altid++);
2182 unpack_threadid (char *inbuf, threadref *id)
2185 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2188 altref = (char *) id;
2190 while (inbuf < limit)
2192 x = stubhex (*inbuf++);
2193 y = stubhex (*inbuf++);
2194 *altref++ = (x << 4) | y;
2199 /* Externally, threadrefs are 64 bits but internally, they are still
2200 ints. This is due to a mismatch of specifications. We would like
2201 to use 64bit thread references internally. This is an adapter
2205 int_to_threadref (threadref *id, int value)
2207 unsigned char *scan;
2209 scan = (unsigned char *) id;
2215 *scan++ = (value >> 24) & 0xff;
2216 *scan++ = (value >> 16) & 0xff;
2217 *scan++ = (value >> 8) & 0xff;
2218 *scan++ = (value & 0xff);
2222 threadref_to_int (threadref *ref)
2225 unsigned char *scan;
2231 value = (value << 8) | ((*scan++) & 0xff);
2236 copy_threadref (threadref *dest, threadref *src)
2239 unsigned char *csrc, *cdest;
2241 csrc = (unsigned char *) src;
2242 cdest = (unsigned char *) dest;
2249 threadmatch (threadref *dest, threadref *src)
2251 /* Things are broken right now, so just assume we got a match. */
2253 unsigned char *srcp, *destp;
2255 srcp = (char *) src;
2256 destp = (char *) dest;
2260 result &= (*srcp++ == *destp++) ? 1 : 0;
2267 threadid:1, # always request threadid
2274 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2277 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2279 *pkt++ = 'q'; /* Info Query */
2280 *pkt++ = 'P'; /* process or thread info */
2281 pkt = pack_int (pkt, mode); /* mode */
2282 pkt = pack_threadid (pkt, id); /* threadid */
2283 *pkt = '\0'; /* terminate */
2287 /* These values tag the fields in a thread info response packet. */
2288 /* Tagging the fields allows us to request specific fields and to
2289 add more fields as time goes by. */
2291 #define TAG_THREADID 1 /* Echo the thread identifier. */
2292 #define TAG_EXISTS 2 /* Is this process defined enough to
2293 fetch registers and its stack? */
2294 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2295 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2296 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2300 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2301 struct gdb_ext_thread_info *info)
2303 struct remote_state *rs = get_remote_state ();
2307 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2310 /* info->threadid = 0; FIXME: implement zero_threadref. */
2312 info->display[0] = '\0';
2313 info->shortname[0] = '\0';
2314 info->more_display[0] = '\0';
2316 /* Assume the characters indicating the packet type have been
2318 pkt = unpack_int (pkt, &mask); /* arg mask */
2319 pkt = unpack_threadid (pkt, &ref);
2322 warning (_("Incomplete response to threadinfo request."));
2323 if (!threadmatch (&ref, expectedref))
2324 { /* This is an answer to a different request. */
2325 warning (_("ERROR RMT Thread info mismatch."));
2328 copy_threadref (&info->threadid, &ref);
2330 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2332 /* Packets are terminated with nulls. */
2333 while ((pkt < limit) && mask && *pkt)
2335 pkt = unpack_int (pkt, &tag); /* tag */
2336 pkt = unpack_byte (pkt, &length); /* length */
2337 if (!(tag & mask)) /* Tags out of synch with mask. */
2339 warning (_("ERROR RMT: threadinfo tag mismatch."));
2343 if (tag == TAG_THREADID)
2347 warning (_("ERROR RMT: length of threadid is not 16."));
2351 pkt = unpack_threadid (pkt, &ref);
2352 mask = mask & ~TAG_THREADID;
2355 if (tag == TAG_EXISTS)
2357 info->active = stub_unpack_int (pkt, length);
2359 mask = mask & ~(TAG_EXISTS);
2362 warning (_("ERROR RMT: 'exists' length too long."));
2368 if (tag == TAG_THREADNAME)
2370 pkt = unpack_string (pkt, &info->shortname[0], length);
2371 mask = mask & ~TAG_THREADNAME;
2374 if (tag == TAG_DISPLAY)
2376 pkt = unpack_string (pkt, &info->display[0], length);
2377 mask = mask & ~TAG_DISPLAY;
2380 if (tag == TAG_MOREDISPLAY)
2382 pkt = unpack_string (pkt, &info->more_display[0], length);
2383 mask = mask & ~TAG_MOREDISPLAY;
2386 warning (_("ERROR RMT: unknown thread info tag."));
2387 break; /* Not a tag we know about. */
2393 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2394 struct gdb_ext_thread_info *info)
2396 struct remote_state *rs = get_remote_state ();
2399 pack_threadinfo_request (rs->buf, fieldset, threadid);
2401 getpkt (&rs->buf, &rs->buf_size, 0);
2403 if (rs->buf[0] == '\0')
2406 result = remote_unpack_thread_info_response (rs->buf + 2,
2411 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2414 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2415 threadref *nextthread)
2417 *pkt++ = 'q'; /* info query packet */
2418 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2419 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2420 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2421 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2426 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2429 parse_threadlist_response (char *pkt, int result_limit,
2430 threadref *original_echo, threadref *resultlist,
2433 struct remote_state *rs = get_remote_state ();
2435 int count, resultcount, done;
2438 /* Assume the 'q' and 'M chars have been stripped. */
2439 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2440 /* done parse past here */
2441 pkt = unpack_byte (pkt, &count); /* count field */
2442 pkt = unpack_nibble (pkt, &done);
2443 /* The first threadid is the argument threadid. */
2444 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2445 while ((count-- > 0) && (pkt < limit))
2447 pkt = unpack_threadid (pkt, resultlist++);
2448 if (resultcount++ >= result_limit)
2456 /* Fetch the next batch of threads from the remote. Returns -1 if the
2457 qL packet is not supported, 0 on error and 1 on success. */
2460 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2461 int *done, int *result_count, threadref *threadlist)
2463 struct remote_state *rs = get_remote_state ();
2466 /* Trancate result limit to be smaller than the packet size. */
2467 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2468 >= get_remote_packet_size ())
2469 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2471 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2473 getpkt (&rs->buf, &rs->buf_size, 0);
2474 if (*rs->buf == '\0')
2476 /* Packet not supported. */
2481 parse_threadlist_response (rs->buf + 2, result_limit,
2482 &rs->echo_nextthread, threadlist, done);
2484 if (!threadmatch (&rs->echo_nextthread, nextthread))
2486 /* FIXME: This is a good reason to drop the packet. */
2487 /* Possably, there is a duplicate response. */
2489 retransmit immediatly - race conditions
2490 retransmit after timeout - yes
2492 wait for packet, then exit
2494 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2495 return 0; /* I choose simply exiting. */
2497 if (*result_count <= 0)
2501 warning (_("RMT ERROR : failed to get remote thread list."));
2504 return result; /* break; */
2506 if (*result_count > result_limit)
2509 warning (_("RMT ERROR: threadlist response longer than requested."));
2515 /* Fetch the list of remote threads, with the qL packet, and call
2516 STEPFUNCTION for each thread found. Stops iterating and returns 1
2517 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2518 STEPFUNCTION returns false. If the packet is not supported,
2522 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2525 struct remote_state *rs = get_remote_state ();
2526 int done, i, result_count;
2534 if (loopcount++ > looplimit)
2537 warning (_("Remote fetch threadlist -infinite loop-."));
2540 result = remote_get_threadlist (startflag, &rs->nextthread,
2541 MAXTHREADLISTRESULTS,
2542 &done, &result_count,
2543 rs->resultthreadlist);
2546 /* Clear for later iterations. */
2548 /* Setup to resume next batch of thread references, set nextthread. */
2549 if (result_count >= 1)
2550 copy_threadref (&rs->nextthread,
2551 &rs->resultthreadlist[result_count - 1]);
2553 while (result_count--)
2555 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2565 /* A thread found on the remote target. */
2567 typedef struct thread_item
2569 /* The thread's PTID. */
2572 /* The thread's extra info. May be NULL. */
2575 /* The core the thread was running on. -1 if not known. */
2578 DEF_VEC_O(thread_item_t);
2580 /* Context passed around to the various methods listing remote
2581 threads. As new threads are found, they're added to the ITEMS
2584 struct threads_listing_context
2586 /* The threads found on the remote target. */
2587 VEC (thread_item_t) *items;
2590 /* Discard the contents of the constructed thread listing context. */
2593 clear_threads_listing_context (void *p)
2595 struct threads_listing_context *context = p;
2597 struct thread_item *item;
2599 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2600 xfree (item->extra);
2602 VEC_free (thread_item_t, context->items);
2606 remote_newthread_step (threadref *ref, void *data)
2608 struct threads_listing_context *context = data;
2609 struct thread_item item;
2610 int pid = ptid_get_pid (inferior_ptid);
2612 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2616 VEC_safe_push (thread_item_t, context->items, &item);
2618 return 1; /* continue iterator */
2621 #define CRAZY_MAX_THREADS 1000
2624 remote_current_thread (ptid_t oldpid)
2626 struct remote_state *rs = get_remote_state ();
2629 getpkt (&rs->buf, &rs->buf_size, 0);
2630 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2631 return read_ptid (&rs->buf[2], NULL);
2636 /* List remote threads using the deprecated qL packet. */
2639 remote_get_threads_with_ql (struct target_ops *ops,
2640 struct threads_listing_context *context)
2642 if (remote_threadlist_iterator (remote_newthread_step, context,
2643 CRAZY_MAX_THREADS) >= 0)
2649 #if defined(HAVE_LIBEXPAT)
2652 start_thread (struct gdb_xml_parser *parser,
2653 const struct gdb_xml_element *element,
2654 void *user_data, VEC(gdb_xml_value_s) *attributes)
2656 struct threads_listing_context *data = user_data;
2658 struct thread_item item;
2660 struct gdb_xml_value *attr;
2662 id = xml_find_attribute (attributes, "id")->value;
2663 item.ptid = read_ptid (id, NULL);
2665 attr = xml_find_attribute (attributes, "core");
2667 item.core = *(ULONGEST *) attr->value;
2673 VEC_safe_push (thread_item_t, data->items, &item);
2677 end_thread (struct gdb_xml_parser *parser,
2678 const struct gdb_xml_element *element,
2679 void *user_data, const char *body_text)
2681 struct threads_listing_context *data = user_data;
2683 if (body_text && *body_text)
2684 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2687 const struct gdb_xml_attribute thread_attributes[] = {
2688 { "id", GDB_XML_AF_NONE, NULL, NULL },
2689 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2690 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2693 const struct gdb_xml_element thread_children[] = {
2694 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2697 const struct gdb_xml_element threads_children[] = {
2698 { "thread", thread_attributes, thread_children,
2699 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2700 start_thread, end_thread },
2701 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2704 const struct gdb_xml_element threads_elements[] = {
2705 { "threads", NULL, threads_children,
2706 GDB_XML_EF_NONE, NULL, NULL },
2707 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2712 /* List remote threads using qXfer:threads:read. */
2715 remote_get_threads_with_qxfer (struct target_ops *ops,
2716 struct threads_listing_context *context)
2718 #if defined(HAVE_LIBEXPAT)
2719 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2721 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
2722 struct cleanup *back_to = make_cleanup (xfree, xml);
2724 if (xml != NULL && *xml != '\0')
2726 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2727 threads_elements, xml, context);
2730 do_cleanups (back_to);
2738 /* List remote threads using qfThreadInfo/qsThreadInfo. */
2741 remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2742 struct threads_listing_context *context)
2744 struct remote_state *rs = get_remote_state ();
2746 if (rs->use_threadinfo_query)
2750 putpkt ("qfThreadInfo");
2751 getpkt (&rs->buf, &rs->buf_size, 0);
2753 if (bufp[0] != '\0') /* q packet recognized */
2755 while (*bufp++ == 'm') /* reply contains one or more TID */
2759 struct thread_item item;
2761 item.ptid = read_ptid (bufp, &bufp);
2765 VEC_safe_push (thread_item_t, context->items, &item);
2767 while (*bufp++ == ','); /* comma-separated list */
2768 putpkt ("qsThreadInfo");
2769 getpkt (&rs->buf, &rs->buf_size, 0);
2776 /* Packet not recognized. */
2777 rs->use_threadinfo_query = 0;
2784 /* Implement the to_update_thread_list function for the remote
2788 remote_update_thread_list (struct target_ops *ops)
2790 struct remote_state *rs = get_remote_state ();
2791 struct threads_listing_context context;
2792 struct cleanup *old_chain;
2795 context.items = NULL;
2796 old_chain = make_cleanup (clear_threads_listing_context, &context);
2798 /* We have a few different mechanisms to fetch the thread list. Try
2799 them all, starting with the most preferred one first, falling
2800 back to older methods. */
2801 if (remote_get_threads_with_qxfer (ops, &context)
2802 || remote_get_threads_with_qthreadinfo (ops, &context)
2803 || remote_get_threads_with_ql (ops, &context))
2806 struct thread_item *item;
2807 struct thread_info *tp, *tmp;
2811 if (VEC_empty (thread_item_t, context.items)
2812 && remote_thread_always_alive (ops, inferior_ptid))
2814 /* Some targets don't really support threads, but still
2815 reply an (empty) thread list in response to the thread
2816 listing packets, instead of replying "packet not
2817 supported". Exit early so we don't delete the main
2819 do_cleanups (old_chain);
2823 /* CONTEXT now holds the current thread list on the remote
2824 target end. Delete GDB-side threads no longer found on the
2826 ALL_NON_EXITED_THREADS_SAFE (tp, tmp)
2829 VEC_iterate (thread_item_t, context.items, i, item);
2832 if (ptid_equal (item->ptid, tp->ptid))
2836 if (i == VEC_length (thread_item_t, context.items))
2839 delete_thread (tp->ptid);
2843 /* And now add threads we don't know about yet to our list. */
2845 VEC_iterate (thread_item_t, context.items, i, item);
2848 if (!ptid_equal (item->ptid, null_ptid))
2850 struct private_thread_info *info;
2851 /* In non-stop mode, we assume new found threads are
2852 running until proven otherwise with a stop reply. In
2853 all-stop, we can only get here if all threads are
2855 int running = non_stop ? 1 : 0;
2857 remote_notice_new_inferior (item->ptid, running);
2859 info = demand_private_info (item->ptid);
2860 info->core = item->core;
2861 info->extra = item->extra;
2869 /* If no thread listing method is supported, then query whether
2870 each known thread is alive, one by one, with the T packet.
2871 If the target doesn't support threads at all, then this is a
2872 no-op. See remote_thread_alive. */
2876 do_cleanups (old_chain);
2880 * Collect a descriptive string about the given thread.
2881 * The target may say anything it wants to about the thread
2882 * (typically info about its blocked / runnable state, name, etc.).
2883 * This string will appear in the info threads display.
2885 * Optional: targets are not required to implement this function.
2889 remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
2891 struct remote_state *rs = get_remote_state ();
2895 struct gdb_ext_thread_info threadinfo;
2896 static char display_buf[100]; /* arbitrary... */
2897 int n = 0; /* position in display_buf */
2899 if (rs->remote_desc == 0) /* paranoia */
2900 internal_error (__FILE__, __LINE__,
2901 _("remote_threads_extra_info"));
2903 if (ptid_equal (tp->ptid, magic_null_ptid)
2904 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
2905 /* This is the main thread which was added by GDB. The remote
2906 server doesn't know about it. */
2909 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2911 struct thread_info *info = find_thread_ptid (tp->ptid);
2913 if (info && info->private)
2914 return info->private->extra;
2919 if (rs->use_threadextra_query)
2922 char *endb = rs->buf + get_remote_packet_size ();
2924 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2926 write_ptid (b, endb, tp->ptid);
2929 getpkt (&rs->buf, &rs->buf_size, 0);
2930 if (rs->buf[0] != 0)
2932 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2933 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
2934 display_buf [result] = '\0';
2939 /* If the above query fails, fall back to the old method. */
2940 rs->use_threadextra_query = 0;
2941 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2942 | TAG_MOREDISPLAY | TAG_DISPLAY;
2943 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
2944 if (remote_get_threadinfo (&id, set, &threadinfo))
2945 if (threadinfo.active)
2947 if (*threadinfo.shortname)
2948 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
2949 " Name: %s,", threadinfo.shortname);
2950 if (*threadinfo.display)
2951 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2952 " State: %s,", threadinfo.display);
2953 if (*threadinfo.more_display)
2954 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2955 " Priority: %s", threadinfo.more_display);
2959 /* For purely cosmetic reasons, clear up trailing commas. */
2960 if (',' == display_buf[n-1])
2961 display_buf[n-1] = ' ';
2970 remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
2971 struct static_tracepoint_marker *marker)
2973 struct remote_state *rs = get_remote_state ();
2976 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
2978 p += hexnumstr (p, addr);
2980 getpkt (&rs->buf, &rs->buf_size, 0);
2984 error (_("Remote failure reply: %s"), p);
2988 parse_static_tracepoint_marker_definition (p, &p, marker);
2995 static VEC(static_tracepoint_marker_p) *
2996 remote_static_tracepoint_markers_by_strid (struct target_ops *self,
2999 struct remote_state *rs = get_remote_state ();
3000 VEC(static_tracepoint_marker_p) *markers = NULL;
3001 struct static_tracepoint_marker *marker = NULL;
3002 struct cleanup *old_chain;
3005 /* Ask for a first packet of static tracepoint marker
3008 getpkt (&rs->buf, &rs->buf_size, 0);
3011 error (_("Remote failure reply: %s"), p);
3013 old_chain = make_cleanup (free_current_marker, &marker);
3018 marker = XCNEW (struct static_tracepoint_marker);
3022 parse_static_tracepoint_marker_definition (p, &p, marker);
3024 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3026 VEC_safe_push (static_tracepoint_marker_p,
3032 release_static_tracepoint_marker (marker);
3033 memset (marker, 0, sizeof (*marker));
3036 while (*p++ == ','); /* comma-separated list */
3037 /* Ask for another packet of static tracepoint definition. */
3039 getpkt (&rs->buf, &rs->buf_size, 0);
3043 do_cleanups (old_chain);
3048 /* Implement the to_get_ada_task_ptid function for the remote targets. */
3051 remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
3053 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
3057 /* Restart the remote side; this is an extended protocol operation. */
3060 extended_remote_restart (void)
3062 struct remote_state *rs = get_remote_state ();
3064 /* Send the restart command; for reasons I don't understand the
3065 remote side really expects a number after the "R". */
3066 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
3069 remote_fileio_reset ();
3072 /* Clean up connection to a remote debugger. */
3075 remote_close (struct target_ops *self)
3077 struct remote_state *rs = get_remote_state ();
3079 if (rs->remote_desc == NULL)
3080 return; /* already closed */
3082 /* Make sure we leave stdin registered in the event loop, and we
3083 don't leave the async SIGINT signal handler installed. */
3084 remote_terminal_ours (self);
3086 serial_close (rs->remote_desc);
3087 rs->remote_desc = NULL;
3089 /* We don't have a connection to the remote stub anymore. Get rid
3090 of all the inferiors and their threads we were controlling.
3091 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3092 will be unable to find the thread corresponding to (pid, 0, 0). */
3093 inferior_ptid = null_ptid;
3094 discard_all_inferiors ();
3096 /* We are closing the remote target, so we should discard
3097 everything of this target. */
3098 discard_pending_stop_replies_in_queue (rs);
3100 if (remote_async_inferior_event_token)
3101 delete_async_event_handler (&remote_async_inferior_event_token);
3103 remote_notif_state_xfree (rs->notif_state);
3105 trace_reset_local_state ();
3108 /* Query the remote side for the text, data and bss offsets. */
3113 struct remote_state *rs = get_remote_state ();
3116 int lose, num_segments = 0, do_sections, do_segments;
3117 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
3118 struct section_offsets *offs;
3119 struct symfile_segment_data *data;
3121 if (symfile_objfile == NULL)
3124 putpkt ("qOffsets");
3125 getpkt (&rs->buf, &rs->buf_size, 0);
3128 if (buf[0] == '\000')
3129 return; /* Return silently. Stub doesn't support
3133 warning (_("Remote failure reply: %s"), buf);
3137 /* Pick up each field in turn. This used to be done with scanf, but
3138 scanf will make trouble if CORE_ADDR size doesn't match
3139 conversion directives correctly. The following code will work
3140 with any size of CORE_ADDR. */
3141 text_addr = data_addr = bss_addr = 0;
3145 if (strncmp (ptr, "Text=", 5) == 0)
3148 /* Don't use strtol, could lose on big values. */
3149 while (*ptr && *ptr != ';')
3150 text_addr = (text_addr << 4) + fromhex (*ptr++);
3152 if (strncmp (ptr, ";Data=", 6) == 0)
3155 while (*ptr && *ptr != ';')
3156 data_addr = (data_addr << 4) + fromhex (*ptr++);
3161 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
3164 while (*ptr && *ptr != ';')
3165 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
3167 if (bss_addr != data_addr)
3168 warning (_("Target reported unsupported offsets: %s"), buf);
3173 else if (strncmp (ptr, "TextSeg=", 8) == 0)
3176 /* Don't use strtol, could lose on big values. */
3177 while (*ptr && *ptr != ';')
3178 text_addr = (text_addr << 4) + fromhex (*ptr++);
3181 if (strncmp (ptr, ";DataSeg=", 9) == 0)
3184 while (*ptr && *ptr != ';')
3185 data_addr = (data_addr << 4) + fromhex (*ptr++);
3193 error (_("Malformed response to offset query, %s"), buf);
3194 else if (*ptr != '\0')
3195 warning (_("Target reported unsupported offsets: %s"), buf);
3197 offs = ((struct section_offsets *)
3198 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3199 memcpy (offs, symfile_objfile->section_offsets,
3200 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3202 data = get_symfile_segment_data (symfile_objfile->obfd);
3203 do_segments = (data != NULL);
3204 do_sections = num_segments == 0;
3206 if (num_segments > 0)
3208 segments[0] = text_addr;
3209 segments[1] = data_addr;
3211 /* If we have two segments, we can still try to relocate everything
3212 by assuming that the .text and .data offsets apply to the whole
3213 text and data segments. Convert the offsets given in the packet
3214 to base addresses for symfile_map_offsets_to_segments. */
3215 else if (data && data->num_segments == 2)
3217 segments[0] = data->segment_bases[0] + text_addr;
3218 segments[1] = data->segment_bases[1] + data_addr;
3221 /* If the object file has only one segment, assume that it is text
3222 rather than data; main programs with no writable data are rare,
3223 but programs with no code are useless. Of course the code might
3224 have ended up in the data segment... to detect that we would need
3225 the permissions here. */
3226 else if (data && data->num_segments == 1)
3228 segments[0] = data->segment_bases[0] + text_addr;
3231 /* There's no way to relocate by segment. */
3237 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3238 offs, num_segments, segments);
3240 if (ret == 0 && !do_sections)
3241 error (_("Can not handle qOffsets TextSeg "
3242 "response with this symbol file"));
3249 free_symfile_segment_data (data);
3253 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3255 /* This is a temporary kludge to force data and bss to use the
3256 same offsets because that's what nlmconv does now. The real
3257 solution requires changes to the stub and remote.c that I
3258 don't have time to do right now. */
3260 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3261 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3264 objfile_relocate (symfile_objfile, offs);
3267 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3268 threads we know are stopped already. This is used during the
3269 initial remote connection in non-stop mode --- threads that are
3270 reported as already being stopped are left stopped. */
3273 set_stop_requested_callback (struct thread_info *thread, void *data)
3275 /* If we have a stop reply for this thread, it must be stopped. */
3276 if (peek_stop_reply (thread->ptid))
3277 set_stop_requested (thread->ptid, 1);
3282 /* Send interrupt_sequence to remote target. */
3284 send_interrupt_sequence (void)
3286 struct remote_state *rs = get_remote_state ();
3288 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3289 remote_serial_write ("\x03", 1);
3290 else if (interrupt_sequence_mode == interrupt_sequence_break)
3291 serial_send_break (rs->remote_desc);
3292 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3294 serial_send_break (rs->remote_desc);
3295 remote_serial_write ("g", 1);
3298 internal_error (__FILE__, __LINE__,
3299 _("Invalid value for interrupt_sequence_mode: %s."),
3300 interrupt_sequence_mode);
3304 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3305 and extract the PTID. Returns NULL_PTID if not found. */
3308 stop_reply_extract_thread (char *stop_reply)
3310 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3314 /* Txx r:val ; r:val (...) */
3317 /* Look for "register" named "thread". */
3322 p1 = strchr (p, ':');
3326 if (strncmp (p, "thread", p1 - p) == 0)
3327 return read_ptid (++p1, &p);
3329 p1 = strchr (p, ';');
3341 /* Determine the remote side's current thread. If we have a stop
3342 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3343 "thread" register we can extract the current thread from. If not,
3344 ask the remote which is the current thread with qC. The former
3345 method avoids a roundtrip. */
3348 get_current_thread (char *wait_status)
3352 /* Note we don't use remote_parse_stop_reply as that makes use of
3353 the target architecture, which we haven't yet fully determined at
3355 if (wait_status != NULL)
3356 ptid = stop_reply_extract_thread (wait_status);
3357 if (ptid_equal (ptid, null_ptid))
3358 ptid = remote_current_thread (inferior_ptid);
3363 /* Query the remote target for which is the current thread/process,
3364 add it to our tables, and update INFERIOR_PTID. The caller is
3365 responsible for setting the state such that the remote end is ready
3366 to return the current thread.
3368 This function is called after handling the '?' or 'vRun' packets,
3369 whose response is a stop reply from which we can also try
3370 extracting the thread. If the target doesn't support the explicit
3371 qC query, we infer the current thread from that stop reply, passed
3372 in in WAIT_STATUS, which may be NULL. */
3375 add_current_inferior_and_thread (char *wait_status)
3377 struct remote_state *rs = get_remote_state ();
3379 ptid_t ptid = null_ptid;
3381 inferior_ptid = null_ptid;
3383 /* Now, if we have thread information, update inferior_ptid. */
3384 ptid = get_current_thread (wait_status);
3386 if (!ptid_equal (ptid, null_ptid))
3388 if (!remote_multi_process_p (rs))
3391 inferior_ptid = ptid;
3395 /* Without this, some commands which require an active target
3396 (such as kill) won't work. This variable serves (at least)
3397 double duty as both the pid of the target process (if it has
3398 such), and as a flag indicating that a target is active. */
3399 inferior_ptid = magic_null_ptid;
3403 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1);
3405 /* Add the main thread. */
3406 add_thread_silent (inferior_ptid);
3410 remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
3412 struct remote_state *rs = get_remote_state ();
3413 struct packet_config *noack_config;
3414 char *wait_status = NULL;
3416 immediate_quit++; /* Allow user to interrupt it. */
3419 if (interrupt_on_connect)
3420 send_interrupt_sequence ();
3422 /* Ack any packet which the remote side has already sent. */
3423 serial_write (rs->remote_desc, "+", 1);
3425 /* Signal other parts that we're going through the initial setup,
3426 and so things may not be stable yet. */
3427 rs->starting_up = 1;
3429 /* The first packet we send to the target is the optional "supported
3430 packets" request. If the target can answer this, it will tell us
3431 which later probes to skip. */
3432 remote_query_supported ();
3434 /* If the stub wants to get a QAllow, compose one and send it. */
3435 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
3436 remote_set_permissions (target);
3438 /* Next, we possibly activate noack mode.
3440 If the QStartNoAckMode packet configuration is set to AUTO,
3441 enable noack mode if the stub reported a wish for it with
3444 If set to TRUE, then enable noack mode even if the stub didn't
3445 report it in qSupported. If the stub doesn't reply OK, the
3446 session ends with an error.
3448 If FALSE, then don't activate noack mode, regardless of what the
3449 stub claimed should be the default with qSupported. */
3451 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3452 if (packet_config_support (noack_config) != PACKET_DISABLE)
3454 putpkt ("QStartNoAckMode");
3455 getpkt (&rs->buf, &rs->buf_size, 0);
3456 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3462 /* Tell the remote that we are using the extended protocol. */
3464 getpkt (&rs->buf, &rs->buf_size, 0);
3467 /* Let the target know which signals it is allowed to pass down to
3469 update_signals_program_target ();
3471 /* Next, if the target can specify a description, read it. We do
3472 this before anything involving memory or registers. */
3473 target_find_description ();
3475 /* Next, now that we know something about the target, update the
3476 address spaces in the program spaces. */
3477 update_address_spaces ();
3479 /* On OSs where the list of libraries is global to all
3480 processes, we fetch them early. */
3481 if (gdbarch_has_global_solist (target_gdbarch ()))
3482 solib_add (NULL, from_tty, target, auto_solib_add);
3486 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3487 error (_("Non-stop mode requested, but remote "
3488 "does not support non-stop"));
3490 putpkt ("QNonStop:1");
3491 getpkt (&rs->buf, &rs->buf_size, 0);
3493 if (strcmp (rs->buf, "OK") != 0)
3494 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3496 /* Find about threads and processes the stub is already
3497 controlling. We default to adding them in the running state.
3498 The '?' query below will then tell us about which threads are
3500 remote_update_thread_list (target);
3502 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
3504 /* Don't assume that the stub can operate in all-stop mode.
3505 Request it explicitly. */
3506 putpkt ("QNonStop:0");
3507 getpkt (&rs->buf, &rs->buf_size, 0);
3509 if (strcmp (rs->buf, "OK") != 0)
3510 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3513 /* Upload TSVs regardless of whether the target is running or not. The
3514 remote stub, such as GDBserver, may have some predefined or builtin
3515 TSVs, even if the target is not running. */
3516 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3518 struct uploaded_tsv *uploaded_tsvs = NULL;
3520 remote_upload_trace_state_variables (target, &uploaded_tsvs);
3521 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3524 /* Check whether the target is running now. */
3526 getpkt (&rs->buf, &rs->buf_size, 0);
3532 struct inferior *inf;
3534 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3537 error (_("The target is not running (try extended-remote?)"));
3539 /* We're connected, but not running. Drop out before we
3540 call start_remote. */
3541 rs->starting_up = 0;
3546 /* Save the reply for later. */
3547 wait_status = alloca (strlen (rs->buf) + 1);
3548 strcpy (wait_status, rs->buf);
3551 /* Fetch thread list. */
3552 target_update_thread_list ();
3554 /* Let the stub know that we want it to return the thread. */
3555 set_continue_thread (minus_one_ptid);
3557 if (thread_count () == 0)
3559 /* Target has no concept of threads at all. GDB treats
3560 non-threaded target as single-threaded; add a main
3562 add_current_inferior_and_thread (wait_status);
3566 /* We have thread information; select the thread the target
3567 says should be current. If we're reconnecting to a
3568 multi-threaded program, this will ideally be the thread
3569 that last reported an event before GDB disconnected. */
3570 inferior_ptid = get_current_thread (wait_status);
3571 if (ptid_equal (inferior_ptid, null_ptid))
3573 /* Odd... The target was able to list threads, but not
3574 tell us which thread was current (no "thread"
3575 register in T stop reply?). Just pick the first
3576 thread in the thread list then. */
3577 inferior_ptid = thread_list->ptid;
3581 /* init_wait_for_inferior should be called before get_offsets in order
3582 to manage `inserted' flag in bp loc in a correct state.
3583 breakpoint_init_inferior, called from init_wait_for_inferior, set
3584 `inserted' flag to 0, while before breakpoint_re_set, called from
3585 start_remote, set `inserted' flag to 1. In the initialization of
3586 inferior, breakpoint_init_inferior should be called first, and then
3587 breakpoint_re_set can be called. If this order is broken, state of
3588 `inserted' flag is wrong, and cause some problems on breakpoint
3590 init_wait_for_inferior ();
3592 get_offsets (); /* Get text, data & bss offsets. */
3594 /* If we could not find a description using qXfer, and we know
3595 how to do it some other way, try again. This is not
3596 supported for non-stop; it could be, but it is tricky if
3597 there are no stopped threads when we connect. */
3598 if (remote_read_description_p (target)
3599 && gdbarch_target_desc (target_gdbarch ()) == NULL)
3601 target_clear_description ();
3602 target_find_description ();
3605 /* Use the previously fetched status. */
3606 gdb_assert (wait_status != NULL);
3607 strcpy (rs->buf, wait_status);
3608 rs->cached_wait_status = 1;
3611 start_remote (from_tty); /* Initialize gdb process mechanisms. */
3615 /* Clear WFI global state. Do this before finding about new
3616 threads and inferiors, and setting the current inferior.
3617 Otherwise we would clear the proceed status of the current
3618 inferior when we want its stop_soon state to be preserved
3619 (see notice_new_inferior). */
3620 init_wait_for_inferior ();
3622 /* In non-stop, we will either get an "OK", meaning that there
3623 are no stopped threads at this time; or, a regular stop
3624 reply. In the latter case, there may be more than one thread
3625 stopped --- we pull them all out using the vStopped
3627 if (strcmp (rs->buf, "OK") != 0)
3629 struct notif_client *notif = ¬if_client_stop;
3631 /* remote_notif_get_pending_replies acks this one, and gets
3633 rs->notif_state->pending_event[notif_client_stop.id]
3634 = remote_notif_parse (notif, rs->buf);
3635 remote_notif_get_pending_events (notif);
3637 /* Make sure that threads that were stopped remain
3639 iterate_over_threads (set_stop_requested_callback, NULL);
3642 if (target_can_async_p ())
3643 target_async (inferior_event_handler, 0);
3645 if (thread_count () == 0)
3648 error (_("The target is not running (try extended-remote?)"));
3650 /* We're connected, but not running. Drop out before we
3651 call start_remote. */
3652 rs->starting_up = 0;
3656 /* Let the stub know that we want it to return the thread. */
3658 /* Force the stub to choose a thread. */
3659 set_general_thread (null_ptid);
3662 inferior_ptid = remote_current_thread (minus_one_ptid);
3663 if (ptid_equal (inferior_ptid, minus_one_ptid))
3664 error (_("remote didn't report the current thread in non-stop mode"));
3666 get_offsets (); /* Get text, data & bss offsets. */
3668 /* In non-stop mode, any cached wait status will be stored in
3669 the stop reply queue. */
3670 gdb_assert (wait_status == NULL);
3672 /* Report all signals during attach/startup. */
3673 remote_pass_signals (target, 0, NULL);
3676 /* If we connected to a live target, do some additional setup. */
3677 if (target_has_execution)
3679 if (symfile_objfile) /* No use without a symbol-file. */
3680 remote_check_symbols ();
3683 /* Possibly the target has been engaged in a trace run started
3684 previously; find out where things are at. */
3685 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3687 struct uploaded_tp *uploaded_tps = NULL;
3689 if (current_trace_status ()->running)
3690 printf_filtered (_("Trace is already running on the target.\n"));
3692 remote_upload_tracepoints (target, &uploaded_tps);
3694 merge_uploaded_tracepoints (&uploaded_tps);
3697 /* The thread and inferior lists are now synchronized with the
3698 target, our symbols have been relocated, and we're merged the
3699 target's tracepoints with ours. We're done with basic start
3701 rs->starting_up = 0;
3703 /* Maybe breakpoints are global and need to be inserted now. */
3704 if (breakpoints_should_be_inserted_now ())
3705 insert_breakpoints ();
3708 /* Open a connection to a remote debugger.
3709 NAME is the filename used for communication. */
3712 remote_open (const char *name, int from_tty)
3714 remote_open_1 (name, from_tty, &remote_ops, 0);
3717 /* Open a connection to a remote debugger using the extended
3718 remote gdb protocol. NAME is the filename used for communication. */
3721 extended_remote_open (const char *name, int from_tty)
3723 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3726 /* Reset all packets back to "unknown support". Called when opening a
3727 new connection to a remote target. */
3730 reset_all_packet_configs_support (void)
3734 for (i = 0; i < PACKET_MAX; i++)
3735 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3738 /* Initialize all packet configs. */
3741 init_all_packet_configs (void)
3745 for (i = 0; i < PACKET_MAX; i++)
3747 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3748 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3752 /* Symbol look-up. */
3755 remote_check_symbols (void)
3757 struct remote_state *rs = get_remote_state ();
3758 char *msg, *reply, *tmp;
3759 struct bound_minimal_symbol sym;
3762 /* The remote side has no concept of inferiors that aren't running
3763 yet, it only knows about running processes. If we're connected
3764 but our current inferior is not running, we should not invite the
3765 remote target to request symbol lookups related to its
3766 (unrelated) current process. */
3767 if (!target_has_execution)
3770 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
3773 /* Make sure the remote is pointing at the right process. Note
3774 there's no way to select "no process". */
3775 set_general_process ();
3777 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3778 because we need both at the same time. */
3779 msg = alloca (get_remote_packet_size ());
3781 /* Invite target to request symbol lookups. */
3783 putpkt ("qSymbol::");
3784 getpkt (&rs->buf, &rs->buf_size, 0);
3785 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3788 while (strncmp (reply, "qSymbol:", 8) == 0)
3790 struct bound_minimal_symbol sym;
3793 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3795 sym = lookup_minimal_symbol (msg, NULL, NULL);
3796 if (sym.minsym == NULL)
3797 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3800 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
3801 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
3803 /* If this is a function address, return the start of code
3804 instead of any data function descriptor. */
3805 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3809 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3810 phex_nz (sym_addr, addr_size), &reply[8]);
3814 getpkt (&rs->buf, &rs->buf_size, 0);
3819 static struct serial *
3820 remote_serial_open (const char *name)
3822 static int udp_warning = 0;
3824 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3825 of in ser-tcp.c, because it is the remote protocol assuming that the
3826 serial connection is reliable and not the serial connection promising
3828 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3830 warning (_("The remote protocol may be unreliable over UDP.\n"
3831 "Some events may be lost, rendering further debugging "
3836 return serial_open (name);
3839 /* Inform the target of our permission settings. The permission flags
3840 work without this, but if the target knows the settings, it can do
3841 a couple things. First, it can add its own check, to catch cases
3842 that somehow manage to get by the permissions checks in target
3843 methods. Second, if the target is wired to disallow particular
3844 settings (for instance, a system in the field that is not set up to
3845 be able to stop at a breakpoint), it can object to any unavailable
3849 remote_set_permissions (struct target_ops *self)
3851 struct remote_state *rs = get_remote_state ();
3853 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3854 "WriteReg:%x;WriteMem:%x;"
3855 "InsertBreak:%x;InsertTrace:%x;"
3856 "InsertFastTrace:%x;Stop:%x",
3857 may_write_registers, may_write_memory,
3858 may_insert_breakpoints, may_insert_tracepoints,
3859 may_insert_fast_tracepoints, may_stop);
3861 getpkt (&rs->buf, &rs->buf_size, 0);
3863 /* If the target didn't like the packet, warn the user. Do not try
3864 to undo the user's settings, that would just be maddening. */
3865 if (strcmp (rs->buf, "OK") != 0)
3866 warning (_("Remote refused setting permissions with: %s"), rs->buf);
3869 /* This type describes each known response to the qSupported
3871 struct protocol_feature
3873 /* The name of this protocol feature. */
3876 /* The default for this protocol feature. */
3877 enum packet_support default_support;
3879 /* The function to call when this feature is reported, or after
3880 qSupported processing if the feature is not supported.
3881 The first argument points to this structure. The second
3882 argument indicates whether the packet requested support be
3883 enabled, disabled, or probed (or the default, if this function
3884 is being called at the end of processing and this feature was
3885 not reported). The third argument may be NULL; if not NULL, it
3886 is a NUL-terminated string taken from the packet following
3887 this feature's name and an equals sign. */
3888 void (*func) (const struct protocol_feature *, enum packet_support,
3891 /* The corresponding packet for this feature. Only used if
3892 FUNC is remote_supported_packet. */
3897 remote_supported_packet (const struct protocol_feature *feature,
3898 enum packet_support support,
3899 const char *argument)
3903 warning (_("Remote qSupported response supplied an unexpected value for"
3904 " \"%s\"."), feature->name);
3908 remote_protocol_packets[feature->packet].support = support;
3912 remote_packet_size (const struct protocol_feature *feature,
3913 enum packet_support support, const char *value)
3915 struct remote_state *rs = get_remote_state ();
3920 if (support != PACKET_ENABLE)
3923 if (value == NULL || *value == '\0')
3925 warning (_("Remote target reported \"%s\" without a size."),
3931 packet_size = strtol (value, &value_end, 16);
3932 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3934 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3935 feature->name, value);
3939 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3941 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3942 packet_size, MAX_REMOTE_PACKET_SIZE);
3943 packet_size = MAX_REMOTE_PACKET_SIZE;
3946 /* Record the new maximum packet size. */
3947 rs->explicit_packet_size = packet_size;
3950 static const struct protocol_feature remote_protocol_features[] = {
3951 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
3952 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
3953 PACKET_qXfer_auxv },
3954 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3955 PACKET_qXfer_features },
3956 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3957 PACKET_qXfer_libraries },
3958 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3959 PACKET_qXfer_libraries_svr4 },
3960 { "augmented-libraries-svr4-read", PACKET_DISABLE,
3961 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
3962 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
3963 PACKET_qXfer_memory_map },
3964 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3965 PACKET_qXfer_spu_read },
3966 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3967 PACKET_qXfer_spu_write },
3968 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3969 PACKET_qXfer_osdata },
3970 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3971 PACKET_qXfer_threads },
3972 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3973 PACKET_qXfer_traceframe_info },
3974 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3975 PACKET_QPassSignals },
3976 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3977 PACKET_QProgramSignals },
3978 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3979 PACKET_QStartNoAckMode },
3980 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
3981 PACKET_multiprocess_feature },
3982 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
3983 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3984 PACKET_qXfer_siginfo_read },
3985 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3986 PACKET_qXfer_siginfo_write },
3987 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
3988 PACKET_ConditionalTracepoints },
3989 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3990 PACKET_ConditionalBreakpoints },
3991 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
3992 PACKET_BreakpointCommands },
3993 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
3994 PACKET_FastTracepoints },
3995 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
3996 PACKET_StaticTracepoints },
3997 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
3998 PACKET_InstallInTrace},
3999 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4000 PACKET_DisconnectedTracing_feature },
4001 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4003 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4005 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4006 PACKET_TracepointSource },
4007 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4009 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4010 PACKET_EnableDisableTracepoints_feature },
4011 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4012 PACKET_qXfer_fdpic },
4013 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4015 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4016 PACKET_QDisableRandomization },
4017 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
4018 { "QTBuffer:size", PACKET_DISABLE,
4019 remote_supported_packet, PACKET_QTBuffer_size},
4020 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
4021 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4022 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4023 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
4024 PACKET_qXfer_btrace },
4025 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
4026 PACKET_qXfer_btrace_conf },
4027 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
4028 PACKET_Qbtrace_conf_bts_size }
4031 static char *remote_support_xml;
4033 /* Register string appended to "xmlRegisters=" in qSupported query. */
4036 register_remote_support_xml (const char *xml)
4038 #if defined(HAVE_LIBEXPAT)
4039 if (remote_support_xml == NULL)
4040 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
4043 char *copy = xstrdup (remote_support_xml + 13);
4044 char *p = strtok (copy, ",");
4048 if (strcmp (p, xml) == 0)
4055 while ((p = strtok (NULL, ",")) != NULL);
4058 remote_support_xml = reconcat (remote_support_xml,
4059 remote_support_xml, ",", xml,
4066 remote_query_supported_append (char *msg, const char *append)
4069 return reconcat (msg, msg, ";", append, (char *) NULL);
4071 return xstrdup (append);
4075 remote_query_supported (void)
4077 struct remote_state *rs = get_remote_state ();
4080 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4082 /* The packet support flags are handled differently for this packet
4083 than for most others. We treat an error, a disabled packet, and
4084 an empty response identically: any features which must be reported
4085 to be used will be automatically disabled. An empty buffer
4086 accomplishes this, since that is also the representation for a list
4087 containing no features. */
4090 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
4093 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
4095 q = remote_query_supported_append (q, "multiprocess+");
4097 if (remote_support_xml)
4098 q = remote_query_supported_append (q, remote_support_xml);
4100 q = remote_query_supported_append (q, "qRelocInsn+");
4102 q = reconcat (q, "qSupported:", q, (char *) NULL);
4105 do_cleanups (old_chain);
4107 getpkt (&rs->buf, &rs->buf_size, 0);
4109 /* If an error occured, warn, but do not return - just reset the
4110 buffer to empty and go on to disable features. */
4111 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4114 warning (_("Remote failure reply: %s"), rs->buf);
4119 memset (seen, 0, sizeof (seen));
4124 enum packet_support is_supported;
4125 char *p, *end, *name_end, *value;
4127 /* First separate out this item from the rest of the packet. If
4128 there's another item after this, we overwrite the separator
4129 (terminated strings are much easier to work with). */
4131 end = strchr (p, ';');
4134 end = p + strlen (p);
4144 warning (_("empty item in \"qSupported\" response"));
4149 name_end = strchr (p, '=');
4152 /* This is a name=value entry. */
4153 is_supported = PACKET_ENABLE;
4154 value = name_end + 1;
4163 is_supported = PACKET_ENABLE;
4167 is_supported = PACKET_DISABLE;
4171 is_supported = PACKET_SUPPORT_UNKNOWN;
4175 warning (_("unrecognized item \"%s\" "
4176 "in \"qSupported\" response"), p);
4182 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4183 if (strcmp (remote_protocol_features[i].name, p) == 0)
4185 const struct protocol_feature *feature;
4188 feature = &remote_protocol_features[i];
4189 feature->func (feature, is_supported, value);
4194 /* If we increased the packet size, make sure to increase the global
4195 buffer size also. We delay this until after parsing the entire
4196 qSupported packet, because this is the same buffer we were
4198 if (rs->buf_size < rs->explicit_packet_size)
4200 rs->buf_size = rs->explicit_packet_size;
4201 rs->buf = xrealloc (rs->buf, rs->buf_size);
4204 /* Handle the defaults for unmentioned features. */
4205 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4208 const struct protocol_feature *feature;
4210 feature = &remote_protocol_features[i];
4211 feature->func (feature, feature->default_support, NULL);
4215 /* Remove any of the remote.c targets from target stack. Upper targets depend
4216 on it so remove them first. */
4219 remote_unpush_target (void)
4221 pop_all_targets_above (process_stratum - 1);
4225 remote_open_1 (const char *name, int from_tty,
4226 struct target_ops *target, int extended_p)
4228 struct remote_state *rs = get_remote_state ();
4231 error (_("To open a remote debug connection, you need to specify what\n"
4232 "serial device is attached to the remote system\n"
4233 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4235 /* See FIXME above. */
4236 if (!target_async_permitted)
4237 wait_forever_enabled_p = 1;
4239 /* If we're connected to a running target, target_preopen will kill it.
4240 Ask this question first, before target_preopen has a chance to kill
4242 if (rs->remote_desc != NULL && !have_inferiors ())
4245 && !query (_("Already connected to a remote target. Disconnect? ")))
4246 error (_("Still connected."));
4249 /* Here the possibly existing remote target gets unpushed. */
4250 target_preopen (from_tty);
4252 /* Make sure we send the passed signals list the next time we resume. */
4253 xfree (rs->last_pass_packet);
4254 rs->last_pass_packet = NULL;
4256 /* Make sure we send the program signals list the next time we
4258 xfree (rs->last_program_signals_packet);
4259 rs->last_program_signals_packet = NULL;
4261 remote_fileio_reset ();
4262 reopen_exec_file ();
4265 rs->remote_desc = remote_serial_open (name);
4266 if (!rs->remote_desc)
4267 perror_with_name (name);
4269 if (baud_rate != -1)
4271 if (serial_setbaudrate (rs->remote_desc, baud_rate))
4273 /* The requested speed could not be set. Error out to
4274 top level after closing remote_desc. Take care to
4275 set remote_desc to NULL to avoid closing remote_desc
4277 serial_close (rs->remote_desc);
4278 rs->remote_desc = NULL;
4279 perror_with_name (name);
4283 serial_raw (rs->remote_desc);
4285 /* If there is something sitting in the buffer we might take it as a
4286 response to a command, which would be bad. */
4287 serial_flush_input (rs->remote_desc);
4291 puts_filtered ("Remote debugging using ");
4292 puts_filtered (name);
4293 puts_filtered ("\n");
4295 push_target (target); /* Switch to using remote target now. */
4297 /* Register extra event sources in the event loop. */
4298 remote_async_inferior_event_token
4299 = create_async_event_handler (remote_async_inferior_event_handler,
4301 rs->notif_state = remote_notif_state_allocate ();
4303 /* Reset the target state; these things will be queried either by
4304 remote_query_supported or as they are needed. */
4305 reset_all_packet_configs_support ();
4306 rs->cached_wait_status = 0;
4307 rs->explicit_packet_size = 0;
4309 rs->extended = extended_p;
4310 rs->waiting_for_stop_reply = 0;
4311 rs->ctrlc_pending_p = 0;
4313 rs->general_thread = not_sent_ptid;
4314 rs->continue_thread = not_sent_ptid;
4315 rs->remote_traceframe_number = -1;
4317 /* Probe for ability to use "ThreadInfo" query, as required. */
4318 rs->use_threadinfo_query = 1;
4319 rs->use_threadextra_query = 1;
4321 if (target_async_permitted)
4323 /* With this target we start out by owning the terminal. */
4324 remote_async_terminal_ours_p = 1;
4326 /* FIXME: cagney/1999-09-23: During the initial connection it is
4327 assumed that the target is already ready and able to respond to
4328 requests. Unfortunately remote_start_remote() eventually calls
4329 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4330 around this. Eventually a mechanism that allows
4331 wait_for_inferior() to expect/get timeouts will be
4333 wait_forever_enabled_p = 0;
4336 /* First delete any symbols previously loaded from shared libraries. */
4337 no_shared_libraries (NULL, 0);
4340 init_thread_list ();
4342 /* Start the remote connection. If error() or QUIT, discard this
4343 target (we'd otherwise be in an inconsistent state) and then
4344 propogate the error on up the exception chain. This ensures that
4345 the caller doesn't stumble along blindly assuming that the
4346 function succeeded. The CLI doesn't have this problem but other
4347 UI's, such as MI do.
4349 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4350 this function should return an error indication letting the
4351 caller restore the previous state. Unfortunately the command
4352 ``target remote'' is directly wired to this function making that
4353 impossible. On a positive note, the CLI side of this problem has
4354 been fixed - the function set_cmd_context() makes it possible for
4355 all the ``target ....'' commands to share a common callback
4356 function. See cli-dump.c. */
4358 volatile struct gdb_exception ex;
4360 TRY_CATCH (ex, RETURN_MASK_ALL)
4362 remote_start_remote (from_tty, target, extended_p);
4366 /* Pop the partially set up target - unless something else did
4367 already before throwing the exception. */
4368 if (rs->remote_desc != NULL)
4369 remote_unpush_target ();
4370 if (target_async_permitted)
4371 wait_forever_enabled_p = 1;
4372 throw_exception (ex);
4376 remote_btrace_reset ();
4378 if (target_async_permitted)
4379 wait_forever_enabled_p = 1;
4382 /* This takes a program previously attached to and detaches it. After
4383 this is done, GDB can be used to debug some other program. We
4384 better not have left any breakpoints in the target program or it'll
4385 die when it hits one. */
4388 remote_detach_1 (const char *args, int from_tty, int extended)
4390 int pid = ptid_get_pid (inferior_ptid);
4391 struct remote_state *rs = get_remote_state ();
4394 error (_("Argument given to \"detach\" when remotely debugging."));
4396 if (!target_has_execution)
4397 error (_("No process to detach from."));
4401 char *exec_file = get_exec_file (0);
4402 if (exec_file == NULL)
4404 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4405 target_pid_to_str (pid_to_ptid (pid)));
4406 gdb_flush (gdb_stdout);
4409 /* Tell the remote target to detach. */
4410 if (remote_multi_process_p (rs))
4411 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4413 strcpy (rs->buf, "D");
4416 getpkt (&rs->buf, &rs->buf_size, 0);
4418 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4420 else if (rs->buf[0] == '\0')
4421 error (_("Remote doesn't know how to detach"));
4423 error (_("Can't detach process."));
4425 if (from_tty && !extended)
4426 puts_filtered (_("Ending remote debugging.\n"));
4428 target_mourn_inferior ();
4432 remote_detach (struct target_ops *ops, const char *args, int from_tty)
4434 remote_detach_1 (args, from_tty, 0);
4438 extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
4440 remote_detach_1 (args, from_tty, 1);
4443 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4446 remote_disconnect (struct target_ops *target, const char *args, int from_tty)
4449 error (_("Argument given to \"disconnect\" when remotely debugging."));
4451 /* Make sure we unpush even the extended remote targets; mourn
4452 won't do it. So call remote_mourn_1 directly instead of
4453 target_mourn_inferior. */
4454 remote_mourn_1 (target);
4457 puts_filtered ("Ending remote debugging.\n");
4460 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4461 be chatty about it. */
4464 extended_remote_attach_1 (struct target_ops *target, const char *args,
4467 struct remote_state *rs = get_remote_state ();
4469 char *wait_status = NULL;
4471 pid = parse_pid_to_attach (args);
4473 /* Remote PID can be freely equal to getpid, do not check it here the same
4474 way as in other targets. */
4476 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
4477 error (_("This target does not support attaching to a process"));
4481 char *exec_file = get_exec_file (0);
4484 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4485 target_pid_to_str (pid_to_ptid (pid)));
4487 printf_unfiltered (_("Attaching to %s\n"),
4488 target_pid_to_str (pid_to_ptid (pid)));
4490 gdb_flush (gdb_stdout);
4493 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
4495 getpkt (&rs->buf, &rs->buf_size, 0);
4497 switch (packet_ok (rs->buf,
4498 &remote_protocol_packets[PACKET_vAttach]))
4503 /* Save the reply for later. */
4504 wait_status = alloca (strlen (rs->buf) + 1);
4505 strcpy (wait_status, rs->buf);
4507 else if (strcmp (rs->buf, "OK") != 0)
4508 error (_("Attaching to %s failed with: %s"),
4509 target_pid_to_str (pid_to_ptid (pid)),
4512 case PACKET_UNKNOWN:
4513 error (_("This target does not support attaching to a process"));
4515 error (_("Attaching to %s failed"),
4516 target_pid_to_str (pid_to_ptid (pid)));
4519 set_current_inferior (remote_add_inferior (0, pid, 1));
4521 inferior_ptid = pid_to_ptid (pid);
4525 struct thread_info *thread;
4527 /* Get list of threads. */
4528 remote_update_thread_list (target);
4530 thread = first_thread_of_process (pid);
4532 inferior_ptid = thread->ptid;
4534 inferior_ptid = pid_to_ptid (pid);
4536 /* Invalidate our notion of the remote current thread. */
4537 record_currthread (rs, minus_one_ptid);
4541 /* Now, if we have thread information, update inferior_ptid. */
4542 inferior_ptid = remote_current_thread (inferior_ptid);
4544 /* Add the main thread to the thread list. */
4545 add_thread_silent (inferior_ptid);
4548 /* Next, if the target can specify a description, read it. We do
4549 this before anything involving memory or registers. */
4550 target_find_description ();
4554 /* Use the previously fetched status. */
4555 gdb_assert (wait_status != NULL);
4557 if (target_can_async_p ())
4559 struct notif_event *reply
4560 = remote_notif_parse (¬if_client_stop, wait_status);
4562 push_stop_reply ((struct stop_reply *) reply);
4564 target_async (inferior_event_handler, 0);
4568 gdb_assert (wait_status != NULL);
4569 strcpy (rs->buf, wait_status);
4570 rs->cached_wait_status = 1;
4574 gdb_assert (wait_status == NULL);
4578 extended_remote_attach (struct target_ops *ops, const char *args, int from_tty)
4580 extended_remote_attach_1 (ops, args, from_tty);
4583 /* Implementation of the to_post_attach method. */
4586 extended_remote_post_attach (struct target_ops *ops, int pid)
4588 /* In certain cases GDB might not have had the chance to start
4589 symbol lookup up until now. This could happen if the debugged
4590 binary is not using shared libraries, the vsyscall page is not
4591 present (on Linux) and the binary itself hadn't changed since the
4592 debugging process was started. */
4593 if (symfile_objfile != NULL)
4594 remote_check_symbols();
4598 /* Check for the availability of vCont. This function should also check
4602 remote_vcont_probe (struct remote_state *rs)
4606 strcpy (rs->buf, "vCont?");
4608 getpkt (&rs->buf, &rs->buf_size, 0);
4611 /* Make sure that the features we assume are supported. */
4612 if (strncmp (buf, "vCont", 5) == 0)
4615 int support_s, support_S, support_c, support_C;
4621 rs->supports_vCont.t = 0;
4622 rs->supports_vCont.r = 0;
4623 while (p && *p == ';')
4626 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4628 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4630 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4632 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4634 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4635 rs->supports_vCont.t = 1;
4636 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4637 rs->supports_vCont.r = 1;
4639 p = strchr (p, ';');
4642 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4643 BUF will make packet_ok disable the packet. */
4644 if (!support_s || !support_S || !support_c || !support_C)
4648 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4651 /* Helper function for building "vCont" resumptions. Write a
4652 resumption to P. ENDP points to one-passed-the-end of the buffer
4653 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4654 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4655 resumed thread should be single-stepped and/or signalled. If PTID
4656 equals minus_one_ptid, then all threads are resumed; if PTID
4657 represents a process, then all threads of the process are resumed;
4658 the thread to be stepped and/or signalled is given in the global
4662 append_resumption (char *p, char *endp,
4663 ptid_t ptid, int step, enum gdb_signal siggnal)
4665 struct remote_state *rs = get_remote_state ();
4667 if (step && siggnal != GDB_SIGNAL_0)
4668 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4670 /* GDB is willing to range step. */
4671 && use_range_stepping
4672 /* Target supports range stepping. */
4673 && rs->supports_vCont.r
4674 /* We don't currently support range stepping multiple
4675 threads with a wildcard (though the protocol allows it,
4676 so stubs shouldn't make an active effort to forbid
4678 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4680 struct thread_info *tp;
4682 if (ptid_equal (ptid, minus_one_ptid))
4684 /* If we don't know about the target thread's tid, then
4685 we're resuming magic_null_ptid (see caller). */
4686 tp = find_thread_ptid (magic_null_ptid);
4689 tp = find_thread_ptid (ptid);
4690 gdb_assert (tp != NULL);
4692 if (tp->control.may_range_step)
4694 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4696 p += xsnprintf (p, endp - p, ";r%s,%s",
4697 phex_nz (tp->control.step_range_start,
4699 phex_nz (tp->control.step_range_end,
4703 p += xsnprintf (p, endp - p, ";s");
4706 p += xsnprintf (p, endp - p, ";s");
4707 else if (siggnal != GDB_SIGNAL_0)
4708 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4710 p += xsnprintf (p, endp - p, ";c");
4712 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4716 /* All (-1) threads of process. */
4717 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4719 p += xsnprintf (p, endp - p, ":");
4720 p = write_ptid (p, endp, nptid);
4722 else if (!ptid_equal (ptid, minus_one_ptid))
4724 p += xsnprintf (p, endp - p, ":");
4725 p = write_ptid (p, endp, ptid);
4731 /* Append a vCont continue-with-signal action for threads that have a
4732 non-zero stop signal. */
4735 append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4737 struct thread_info *thread;
4739 ALL_NON_EXITED_THREADS (thread)
4740 if (ptid_match (thread->ptid, ptid)
4741 && !ptid_equal (inferior_ptid, thread->ptid)
4742 && thread->suspend.stop_signal != GDB_SIGNAL_0)
4744 p = append_resumption (p, endp, thread->ptid,
4745 0, thread->suspend.stop_signal);
4746 thread->suspend.stop_signal = GDB_SIGNAL_0;
4752 /* Resume the remote inferior by using a "vCont" packet. The thread
4753 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4754 resumed thread should be single-stepped and/or signalled. If PTID
4755 equals minus_one_ptid, then all threads are resumed; the thread to
4756 be stepped and/or signalled is given in the global INFERIOR_PTID.
4757 This function returns non-zero iff it resumes the inferior.
4759 This function issues a strict subset of all possible vCont commands at the
4763 remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
4765 struct remote_state *rs = get_remote_state ();
4769 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4770 remote_vcont_probe (rs);
4772 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
4776 endp = rs->buf + get_remote_packet_size ();
4778 /* If we could generate a wider range of packets, we'd have to worry
4779 about overflowing BUF. Should there be a generic
4780 "multi-part-packet" packet? */
4782 p += xsnprintf (p, endp - p, "vCont");
4784 if (ptid_equal (ptid, magic_null_ptid))
4786 /* MAGIC_NULL_PTID means that we don't have any active threads,
4787 so we don't have any TID numbers the inferior will
4788 understand. Make sure to only send forms that do not specify
4790 append_resumption (p, endp, minus_one_ptid, step, siggnal);
4792 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
4794 /* Resume all threads (of all processes, or of a single
4795 process), with preference for INFERIOR_PTID. This assumes
4796 inferior_ptid belongs to the set of all threads we are about
4798 if (step || siggnal != GDB_SIGNAL_0)
4800 /* Step inferior_ptid, with or without signal. */
4801 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
4804 /* Also pass down any pending signaled resumption for other
4805 threads not the current. */
4806 p = append_pending_thread_resumptions (p, endp, ptid);
4808 /* And continue others without a signal. */
4809 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
4813 /* Scheduler locking; resume only PTID. */
4814 append_resumption (p, endp, ptid, step, siggnal);
4817 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4822 /* In non-stop, the stub replies to vCont with "OK". The stop
4823 reply will be reported asynchronously by means of a `%Stop'
4825 getpkt (&rs->buf, &rs->buf_size, 0);
4826 if (strcmp (rs->buf, "OK") != 0)
4827 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4833 /* Tell the remote machine to resume. */
4836 remote_resume (struct target_ops *ops,
4837 ptid_t ptid, int step, enum gdb_signal siggnal)
4839 struct remote_state *rs = get_remote_state ();
4842 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4843 (explained in remote-notif.c:handle_notification) so
4844 remote_notif_process is not called. We need find a place where
4845 it is safe to start a 'vNotif' sequence. It is good to do it
4846 before resuming inferior, because inferior was stopped and no RSP
4847 traffic at that moment. */
4849 remote_notif_process (rs->notif_state, ¬if_client_stop);
4851 rs->last_sent_signal = siggnal;
4852 rs->last_sent_step = step;
4854 /* The vCont packet doesn't need to specify threads via Hc. */
4855 /* No reverse support (yet) for vCont. */
4856 if (execution_direction != EXEC_REVERSE)
4857 if (remote_vcont_resume (ptid, step, siggnal))
4860 /* All other supported resume packets do use Hc, so set the continue
4862 if (ptid_equal (ptid, minus_one_ptid))
4863 set_continue_thread (any_thread_ptid);
4865 set_continue_thread (ptid);
4868 if (execution_direction == EXEC_REVERSE)
4870 /* We don't pass signals to the target in reverse exec mode. */
4871 if (info_verbose && siggnal != GDB_SIGNAL_0)
4872 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4875 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
4876 error (_("Remote reverse-step not supported."));
4877 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
4878 error (_("Remote reverse-continue not supported."));
4880 strcpy (buf, step ? "bs" : "bc");
4882 else if (siggnal != GDB_SIGNAL_0)
4884 buf[0] = step ? 'S' : 'C';
4885 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
4886 buf[2] = tohex (((int) siggnal) & 0xf);
4890 strcpy (buf, step ? "s" : "c");
4895 /* We are about to start executing the inferior, let's register it
4896 with the event loop. NOTE: this is the one place where all the
4897 execution commands end up. We could alternatively do this in each
4898 of the execution commands in infcmd.c. */
4899 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4900 into infcmd.c in order to allow inferior function calls to work
4901 NOT asynchronously. */
4902 if (target_can_async_p ())
4903 target_async (inferior_event_handler, 0);
4905 /* We've just told the target to resume. The remote server will
4906 wait for the inferior to stop, and then send a stop reply. In
4907 the mean time, we can't start another command/query ourselves
4908 because the stub wouldn't be ready to process it. This applies
4909 only to the base all-stop protocol, however. In non-stop (which
4910 only supports vCont), the stub replies with an "OK", and is
4911 immediate able to process further serial input. */
4913 rs->waiting_for_stop_reply = 1;
4917 /* Set up the signal handler for SIGINT, while the target is
4918 executing, ovewriting the 'regular' SIGINT signal handler. */
4920 async_initialize_sigint_signal_handler (void)
4922 signal (SIGINT, async_handle_remote_sigint);
4925 /* Signal handler for SIGINT, while the target is executing. */
4927 async_handle_remote_sigint (int sig)
4929 signal (sig, async_handle_remote_sigint_twice);
4930 /* Note we need to go through gdb_call_async_signal_handler in order
4931 to wake up the event loop on Windows. */
4932 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
4935 /* Signal handler for SIGINT, installed after SIGINT has already been
4936 sent once. It will take effect the second time that the user sends
4939 async_handle_remote_sigint_twice (int sig)
4941 signal (sig, async_handle_remote_sigint);
4942 /* See note in async_handle_remote_sigint. */
4943 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
4946 /* Perform the real interruption of the target execution, in response
4949 async_remote_interrupt (gdb_client_data arg)
4952 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
4954 target_stop (inferior_ptid);
4957 /* Perform interrupt, if the first attempt did not succeed. Just give
4958 up on the target alltogether. */
4960 async_remote_interrupt_twice (gdb_client_data arg)
4963 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
4968 /* Reinstall the usual SIGINT handlers, after the target has
4971 async_cleanup_sigint_signal_handler (void *dummy)
4973 signal (SIGINT, handle_sigint);
4976 /* Send ^C to target to halt it. Target will respond, and send us a
4978 static void (*ofunc) (int);
4980 /* The command line interface's stop routine. This function is installed
4981 as a signal handler for SIGINT. The first time a user requests a
4982 stop, we call remote_stop to send a break or ^C. If there is no
4983 response from the target (it didn't stop when the user requested it),
4984 we ask the user if he'd like to detach from the target. */
4986 sync_remote_interrupt (int signo)
4988 /* If this doesn't work, try more severe steps. */
4989 signal (signo, sync_remote_interrupt_twice);
4991 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
4994 /* The user typed ^C twice. */
4997 sync_remote_interrupt_twice (int signo)
4999 signal (signo, ofunc);
5000 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5001 signal (signo, sync_remote_interrupt);
5004 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5005 thread, all threads of a remote process, or all threads of all
5009 remote_stop_ns (ptid_t ptid)
5011 struct remote_state *rs = get_remote_state ();
5013 char *endp = rs->buf + get_remote_packet_size ();
5015 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
5016 remote_vcont_probe (rs);
5018 if (!rs->supports_vCont.t)
5019 error (_("Remote server does not support stopping threads"));
5021 if (ptid_equal (ptid, minus_one_ptid)
5022 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5023 p += xsnprintf (p, endp - p, "vCont;t");
5028 p += xsnprintf (p, endp - p, "vCont;t:");
5030 if (ptid_is_pid (ptid))
5031 /* All (-1) threads of process. */
5032 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
5035 /* Small optimization: if we already have a stop reply for
5036 this thread, no use in telling the stub we want this
5038 if (peek_stop_reply (ptid))
5044 write_ptid (p, endp, nptid);
5047 /* In non-stop, we get an immediate OK reply. The stop reply will
5048 come in asynchronously by notification. */
5050 getpkt (&rs->buf, &rs->buf_size, 0);
5051 if (strcmp (rs->buf, "OK") != 0)
5052 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5055 /* All-stop version of target_stop. Sends a break or a ^C to stop the
5056 remote target. It is undefined which thread of which process
5057 reports the stop. */
5060 remote_stop_as (ptid_t ptid)
5062 struct remote_state *rs = get_remote_state ();
5064 rs->ctrlc_pending_p = 1;
5066 /* If the inferior is stopped already, but the core didn't know
5067 about it yet, just ignore the request. The cached wait status
5068 will be collected in remote_wait. */
5069 if (rs->cached_wait_status)
5072 /* Send interrupt_sequence to remote target. */
5073 send_interrupt_sequence ();
5076 /* This is the generic stop called via the target vector. When a target
5077 interrupt is requested, either by the command line or the GUI, we
5078 will eventually end up here. */
5081 remote_stop (struct target_ops *self, ptid_t ptid)
5084 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
5087 remote_stop_ns (ptid);
5089 remote_stop_as (ptid);
5092 /* Ask the user what to do when an interrupt is received. */
5095 interrupt_query (void)
5097 target_terminal_ours ();
5099 if (target_is_async_p ())
5101 signal (SIGINT, handle_sigint);
5106 if (query (_("Interrupted while waiting for the program.\n\
5107 Give up (and stop debugging it)? ")))
5109 remote_unpush_target ();
5114 target_terminal_inferior ();
5117 /* Enable/disable target terminal ownership. Most targets can use
5118 terminal groups to control terminal ownership. Remote targets are
5119 different in that explicit transfer of ownership to/from GDB/target
5123 remote_terminal_inferior (struct target_ops *self)
5125 if (!target_async_permitted)
5126 /* Nothing to do. */
5129 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5130 idempotent. The event-loop GDB talking to an asynchronous target
5131 with a synchronous command calls this function from both
5132 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5133 transfer the terminal to the target when it shouldn't this guard
5135 if (!remote_async_terminal_ours_p)
5137 delete_file_handler (input_fd);
5138 remote_async_terminal_ours_p = 0;
5139 async_initialize_sigint_signal_handler ();
5140 /* NOTE: At this point we could also register our selves as the
5141 recipient of all input. Any characters typed could then be
5142 passed on down to the target. */
5146 remote_terminal_ours (struct target_ops *self)
5148 if (!target_async_permitted)
5149 /* Nothing to do. */
5152 /* See FIXME in remote_terminal_inferior. */
5153 if (remote_async_terminal_ours_p)
5155 async_cleanup_sigint_signal_handler (NULL);
5156 add_file_handler (input_fd, stdin_event_handler, 0);
5157 remote_async_terminal_ours_p = 1;
5161 remote_console_output (char *msg)
5165 for (p = msg; p[0] && p[1]; p += 2)
5168 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5172 fputs_unfiltered (tb, gdb_stdtarg);
5174 gdb_flush (gdb_stdtarg);
5177 typedef struct cached_reg
5180 gdb_byte data[MAX_REGISTER_SIZE];
5183 DEF_VEC_O(cached_reg_t);
5185 typedef struct stop_reply
5187 struct notif_event base;
5189 /* The identifier of the thread about this event */
5192 /* The remote state this event is associated with. When the remote
5193 connection, represented by a remote_state object, is closed,
5194 all the associated stop_reply events should be released. */
5195 struct remote_state *rs;
5197 struct target_waitstatus ws;
5199 /* Expedited registers. This makes remote debugging a bit more
5200 efficient for those targets that provide critical registers as
5201 part of their normal status mechanism (as another roundtrip to
5202 fetch them is avoided). */
5203 VEC(cached_reg_t) *regcache;
5205 int stopped_by_watchpoint_p;
5206 CORE_ADDR watch_data_address;
5211 DECLARE_QUEUE_P (stop_reply_p);
5212 DEFINE_QUEUE_P (stop_reply_p);
5213 /* The list of already fetched and acknowledged stop events. This
5214 queue is used for notification Stop, and other notifications
5215 don't need queue for their events, because the notification events
5216 of Stop can't be consumed immediately, so that events should be
5217 queued first, and be consumed by remote_wait_{ns,as} one per
5218 time. Other notifications can consume their events immediately,
5219 so queue is not needed for them. */
5220 static QUEUE (stop_reply_p) *stop_reply_queue;
5223 stop_reply_xfree (struct stop_reply *r)
5225 notif_event_xfree ((struct notif_event *) r);
5229 remote_notif_stop_parse (struct notif_client *self, char *buf,
5230 struct notif_event *event)
5232 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5236 remote_notif_stop_ack (struct notif_client *self, char *buf,
5237 struct notif_event *event)
5239 struct stop_reply *stop_reply = (struct stop_reply *) event;
5242 putpkt ((char *) self->ack_command);
5244 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5245 /* We got an unknown stop reply. */
5246 error (_("Unknown stop reply"));
5248 push_stop_reply (stop_reply);
5252 remote_notif_stop_can_get_pending_events (struct notif_client *self)
5254 /* We can't get pending events in remote_notif_process for
5255 notification stop, and we have to do this in remote_wait_ns
5256 instead. If we fetch all queued events from stub, remote stub
5257 may exit and we have no chance to process them back in
5259 mark_async_event_handler (remote_async_inferior_event_token);
5264 stop_reply_dtr (struct notif_event *event)
5266 struct stop_reply *r = (struct stop_reply *) event;
5268 VEC_free (cached_reg_t, r->regcache);
5271 static struct notif_event *
5272 remote_notif_stop_alloc_reply (void)
5274 struct notif_event *r
5275 = (struct notif_event *) XNEW (struct stop_reply);
5277 r->dtr = stop_reply_dtr;
5282 /* A client of notification Stop. */
5284 struct notif_client notif_client_stop =
5288 remote_notif_stop_parse,
5289 remote_notif_stop_ack,
5290 remote_notif_stop_can_get_pending_events,
5291 remote_notif_stop_alloc_reply,
5295 /* A parameter to pass data in and out. */
5297 struct queue_iter_param
5300 struct stop_reply *output;
5303 /* Remove stop replies in the queue if its pid is equal to the given
5307 remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5308 QUEUE_ITER (stop_reply_p) *iter,
5312 struct queue_iter_param *param = data;
5313 struct inferior *inf = param->input;
5315 if (ptid_get_pid (event->ptid) == inf->pid)
5317 stop_reply_xfree (event);
5318 QUEUE_remove_elem (stop_reply_p, q, iter);
5324 /* Discard all pending stop replies of inferior INF. */
5327 discard_pending_stop_replies (struct inferior *inf)
5330 struct queue_iter_param param;
5331 struct stop_reply *reply;
5332 struct remote_state *rs = get_remote_state ();
5333 struct remote_notif_state *rns = rs->notif_state;
5335 /* This function can be notified when an inferior exists. When the
5336 target is not remote, the notification state is NULL. */
5337 if (rs->remote_desc == NULL)
5340 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
5342 /* Discard the in-flight notification. */
5343 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
5345 stop_reply_xfree (reply);
5346 rns->pending_event[notif_client_stop.id] = NULL;
5350 param.output = NULL;
5351 /* Discard the stop replies we have already pulled with
5353 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5354 remove_stop_reply_for_inferior, ¶m);
5357 /* If its remote state is equal to the given remote state,
5358 remove EVENT from the stop reply queue. */
5361 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5362 QUEUE_ITER (stop_reply_p) *iter,
5366 struct queue_iter_param *param = data;
5367 struct remote_state *rs = param->input;
5369 if (event->rs == rs)
5371 stop_reply_xfree (event);
5372 QUEUE_remove_elem (stop_reply_p, q, iter);
5378 /* Discard the stop replies for RS in stop_reply_queue. */
5381 discard_pending_stop_replies_in_queue (struct remote_state *rs)
5383 struct queue_iter_param param;
5386 param.output = NULL;
5387 /* Discard the stop replies we have already pulled with
5389 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5390 remove_stop_reply_of_remote_state, ¶m);
5393 /* A parameter to pass data in and out. */
5396 remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5397 QUEUE_ITER (stop_reply_p) *iter,
5401 struct queue_iter_param *param = data;
5402 ptid_t *ptid = param->input;
5404 if (ptid_match (event->ptid, *ptid))
5406 param->output = event;
5407 QUEUE_remove_elem (stop_reply_p, q, iter);
5414 /* Remove the first reply in 'stop_reply_queue' which matches
5417 static struct stop_reply *
5418 remote_notif_remove_queued_reply (ptid_t ptid)
5420 struct queue_iter_param param;
5422 param.input = &ptid;
5423 param.output = NULL;
5425 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5426 remote_notif_remove_once_on_match, ¶m);
5428 fprintf_unfiltered (gdb_stdlog,
5429 "notif: discard queued event: 'Stop' in %s\n",
5430 target_pid_to_str (ptid));
5432 return param.output;
5435 /* Look for a queued stop reply belonging to PTID. If one is found,
5436 remove it from the queue, and return it. Returns NULL if none is
5437 found. If there are still queued events left to process, tell the
5438 event loop to get back to target_wait soon. */
5440 static struct stop_reply *
5441 queued_stop_reply (ptid_t ptid)
5443 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
5445 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5446 /* There's still at least an event left. */
5447 mark_async_event_handler (remote_async_inferior_event_token);
5452 /* Push a fully parsed stop reply in the stop reply queue. Since we
5453 know that we now have at least one queued event left to pass to the
5454 core side, tell the event loop to get back to target_wait soon. */
5457 push_stop_reply (struct stop_reply *new_event)
5459 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
5462 fprintf_unfiltered (gdb_stdlog,
5463 "notif: push 'Stop' %s to queue %d\n",
5464 target_pid_to_str (new_event->ptid),
5465 QUEUE_length (stop_reply_p,
5468 mark_async_event_handler (remote_async_inferior_event_token);
5472 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5473 QUEUE_ITER (stop_reply_p) *iter,
5474 struct stop_reply *event,
5477 ptid_t *ptid = data;
5479 return !(ptid_equal (*ptid, event->ptid)
5480 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5483 /* Returns true if we have a stop reply for PTID. */
5486 peek_stop_reply (ptid_t ptid)
5488 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5489 stop_reply_match_ptid_and_ws, &ptid);
5492 /* Parse the stop reply in BUF. Either the function succeeds, and the
5493 result is stored in EVENT, or throws an error. */
5496 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5498 struct remote_arch_state *rsa = get_remote_arch_state ();
5502 event->ptid = null_ptid;
5503 event->rs = get_remote_state ();
5504 event->ws.kind = TARGET_WAITKIND_IGNORE;
5505 event->ws.value.integer = 0;
5506 event->stopped_by_watchpoint_p = 0;
5507 event->regcache = NULL;
5512 case 'T': /* Status with PC, SP, FP, ... */
5513 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5514 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5516 n... = register number
5517 r... = register contents
5520 p = &buf[3]; /* after Txx */
5528 /* If the packet contains a register number, save it in
5529 pnum and set p1 to point to the character following it.
5530 Otherwise p1 points to p. */
5532 /* If this packet is an awatch packet, don't parse the 'a'
5533 as a register number. */
5535 if (strncmp (p, "awatch", strlen("awatch")) != 0
5536 && strncmp (p, "core", strlen ("core") != 0))
5538 /* Read the ``P'' register number. */
5539 pnum = strtol (p, &p_temp, 16);
5545 if (p1 == p) /* No register number present here. */
5547 p1 = strchr (p, ':');
5549 error (_("Malformed packet(a) (missing colon): %s\n\
5552 if (strncmp (p, "thread", p1 - p) == 0)
5553 event->ptid = read_ptid (++p1, &p);
5554 else if ((strncmp (p, "watch", p1 - p) == 0)
5555 || (strncmp (p, "rwatch", p1 - p) == 0)
5556 || (strncmp (p, "awatch", p1 - p) == 0))
5558 event->stopped_by_watchpoint_p = 1;
5559 p = unpack_varlen_hex (++p1, &addr);
5560 event->watch_data_address = (CORE_ADDR) addr;
5562 else if (strncmp (p, "library", p1 - p) == 0)
5566 while (*p_temp && *p_temp != ';')
5569 event->ws.kind = TARGET_WAITKIND_LOADED;
5572 else if (strncmp (p, "replaylog", p1 - p) == 0)
5574 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5575 /* p1 will indicate "begin" or "end", but it makes
5576 no difference for now, so ignore it. */
5577 p_temp = strchr (p1 + 1, ';');
5581 else if (strncmp (p, "core", p1 - p) == 0)
5585 p = unpack_varlen_hex (++p1, &c);
5590 /* Silently skip unknown optional info. */
5591 p_temp = strchr (p1 + 1, ';');
5598 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5599 cached_reg_t cached_reg;
5604 error (_("Malformed packet(b) (missing colon): %s\n\
5610 error (_("Remote sent bad register number %s: %s\n\
5612 hex_string (pnum), p, buf);
5614 cached_reg.num = reg->regnum;
5616 fieldsize = hex2bin (p, cached_reg.data,
5617 register_size (target_gdbarch (),
5620 if (fieldsize < register_size (target_gdbarch (),
5622 warning (_("Remote reply is too short: %s"), buf);
5624 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5628 error (_("Remote register badly formatted: %s\nhere: %s"),
5633 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5637 case 'S': /* Old style status, just signal only. */
5641 event->ws.kind = TARGET_WAITKIND_STOPPED;
5642 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5643 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5644 event->ws.value.sig = (enum gdb_signal) sig;
5646 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5649 case 'W': /* Target exited. */
5656 /* GDB used to accept only 2 hex chars here. Stubs should
5657 only send more if they detect GDB supports multi-process
5659 p = unpack_varlen_hex (&buf[1], &value);
5663 /* The remote process exited. */
5664 event->ws.kind = TARGET_WAITKIND_EXITED;
5665 event->ws.value.integer = value;
5669 /* The remote process exited with a signal. */
5670 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5671 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5672 event->ws.value.sig = (enum gdb_signal) value;
5674 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5677 /* If no process is specified, assume inferior_ptid. */
5678 pid = ptid_get_pid (inferior_ptid);
5687 else if (strncmp (p,
5688 "process:", sizeof ("process:") - 1) == 0)
5692 p += sizeof ("process:") - 1;
5693 unpack_varlen_hex (p, &upid);
5697 error (_("unknown stop reply packet: %s"), buf);
5700 error (_("unknown stop reply packet: %s"), buf);
5701 event->ptid = pid_to_ptid (pid);
5706 if (non_stop && ptid_equal (event->ptid, null_ptid))
5707 error (_("No process or thread specified in stop reply: %s"), buf);
5710 /* When the stub wants to tell GDB about a new notification reply, it
5711 sends a notification (%Stop, for example). Those can come it at
5712 any time, hence, we have to make sure that any pending
5713 putpkt/getpkt sequence we're making is finished, before querying
5714 the stub for more events with the corresponding ack command
5715 (vStopped, for example). E.g., if we started a vStopped sequence
5716 immediately upon receiving the notification, something like this
5724 1.6) <-- (registers reply to step #1.3)
5726 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5729 To solve this, whenever we parse a %Stop notification successfully,
5730 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5731 doing whatever we were doing:
5737 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5738 2.5) <-- (registers reply to step #2.3)
5740 Eventualy after step #2.5, we return to the event loop, which
5741 notices there's an event on the
5742 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5743 associated callback --- the function below. At this point, we're
5744 always safe to start a vStopped sequence. :
5747 2.7) <-- T05 thread:2
5753 remote_notif_get_pending_events (struct notif_client *nc)
5755 struct remote_state *rs = get_remote_state ();
5757 if (rs->notif_state->pending_event[nc->id] != NULL)
5760 fprintf_unfiltered (gdb_stdlog,
5761 "notif: process: '%s' ack pending event\n",
5765 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
5766 rs->notif_state->pending_event[nc->id] = NULL;
5770 getpkt (&rs->buf, &rs->buf_size, 0);
5771 if (strcmp (rs->buf, "OK") == 0)
5774 remote_notif_ack (nc, rs->buf);
5780 fprintf_unfiltered (gdb_stdlog,
5781 "notif: process: '%s' no pending reply\n",
5786 /* Called when it is decided that STOP_REPLY holds the info of the
5787 event that is to be returned to the core. This function always
5788 destroys STOP_REPLY. */
5791 process_stop_reply (struct stop_reply *stop_reply,
5792 struct target_waitstatus *status)
5796 *status = stop_reply->ws;
5797 ptid = stop_reply->ptid;
5799 /* If no thread/process was reported by the stub, assume the current
5801 if (ptid_equal (ptid, null_ptid))
5802 ptid = inferior_ptid;
5804 if (status->kind != TARGET_WAITKIND_EXITED
5805 && status->kind != TARGET_WAITKIND_SIGNALLED)
5807 struct remote_state *rs = get_remote_state ();
5809 /* Expedited registers. */
5810 if (stop_reply->regcache)
5812 struct regcache *regcache
5813 = get_thread_arch_regcache (ptid, target_gdbarch ());
5818 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5820 regcache_raw_supply (regcache, reg->num, reg->data);
5821 VEC_free (cached_reg_t, stop_reply->regcache);
5824 rs->remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5825 rs->remote_watch_data_address = stop_reply->watch_data_address;
5827 remote_notice_new_inferior (ptid, 0);
5828 demand_private_info (ptid)->core = stop_reply->core;
5831 stop_reply_xfree (stop_reply);
5835 /* The non-stop mode version of target_wait. */
5838 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
5840 struct remote_state *rs = get_remote_state ();
5841 struct stop_reply *stop_reply;
5845 /* If in non-stop mode, get out of getpkt even if a
5846 notification is received. */
5848 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5849 0 /* forever */, &is_notif);
5852 if (ret != -1 && !is_notif)
5855 case 'E': /* Error of some sort. */
5856 /* We're out of sync with the target now. Did it continue
5857 or not? We can't tell which thread it was in non-stop,
5858 so just ignore this. */
5859 warning (_("Remote failure reply: %s"), rs->buf);
5861 case 'O': /* Console output. */
5862 remote_console_output (rs->buf + 1);
5865 warning (_("Invalid remote reply: %s"), rs->buf);
5869 /* Acknowledge a pending stop reply that may have arrived in the
5871 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
5872 remote_notif_get_pending_events (¬if_client_stop);
5874 /* If indeed we noticed a stop reply, we're done. */
5875 stop_reply = queued_stop_reply (ptid);
5876 if (stop_reply != NULL)
5877 return process_stop_reply (stop_reply, status);
5879 /* Still no event. If we're just polling for an event, then
5880 return to the event loop. */
5881 if (options & TARGET_WNOHANG)
5883 status->kind = TARGET_WAITKIND_IGNORE;
5884 return minus_one_ptid;
5887 /* Otherwise do a blocking wait. */
5888 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5889 1 /* forever */, &is_notif);
5893 /* Wait until the remote machine stops, then return, storing status in
5894 STATUS just as `wait' would. */
5897 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
5899 struct remote_state *rs = get_remote_state ();
5900 ptid_t event_ptid = null_ptid;
5902 struct stop_reply *stop_reply;
5906 status->kind = TARGET_WAITKIND_IGNORE;
5907 status->value.integer = 0;
5909 stop_reply = queued_stop_reply (ptid);
5910 if (stop_reply != NULL)
5911 return process_stop_reply (stop_reply, status);
5913 if (rs->cached_wait_status)
5914 /* Use the cached wait status, but only once. */
5915 rs->cached_wait_status = 0;
5921 if (!target_is_async_p ())
5923 ofunc = signal (SIGINT, sync_remote_interrupt);
5924 /* If the user hit C-c before this packet, or between packets,
5925 pretend that it was hit right here. */
5926 if (check_quit_flag ())
5929 sync_remote_interrupt (SIGINT);
5933 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5934 _never_ wait for ever -> test on target_is_async_p().
5935 However, before we do that we need to ensure that the caller
5936 knows how to take the target into/out of async mode. */
5937 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5938 wait_forever_enabled_p, &is_notif);
5940 if (!target_is_async_p ())
5941 signal (SIGINT, ofunc);
5943 /* GDB gets a notification. Return to core as this event is
5945 if (ret != -1 && is_notif)
5946 return minus_one_ptid;
5951 rs->remote_stopped_by_watchpoint_p = 0;
5953 /* We got something. */
5954 rs->waiting_for_stop_reply = 0;
5956 /* Assume that the target has acknowledged Ctrl-C unless we receive
5957 an 'F' or 'O' packet. */
5958 if (buf[0] != 'F' && buf[0] != 'O')
5959 rs->ctrlc_pending_p = 0;
5963 case 'E': /* Error of some sort. */
5964 /* We're out of sync with the target now. Did it continue or
5965 not? Not is more likely, so report a stop. */
5966 warning (_("Remote failure reply: %s"), buf);
5967 status->kind = TARGET_WAITKIND_STOPPED;
5968 status->value.sig = GDB_SIGNAL_0;
5970 case 'F': /* File-I/O request. */
5971 remote_fileio_request (buf, rs->ctrlc_pending_p);
5972 rs->ctrlc_pending_p = 0;
5974 case 'T': case 'S': case 'X': case 'W':
5976 struct stop_reply *stop_reply
5977 = (struct stop_reply *) remote_notif_parse (¬if_client_stop,
5980 event_ptid = process_stop_reply (stop_reply, status);
5983 case 'O': /* Console output. */
5984 remote_console_output (buf + 1);
5986 /* The target didn't really stop; keep waiting. */
5987 rs->waiting_for_stop_reply = 1;
5991 if (rs->last_sent_signal != GDB_SIGNAL_0)
5993 /* Zero length reply means that we tried 'S' or 'C' and the
5994 remote system doesn't support it. */
5995 target_terminal_ours_for_output ();
5997 ("Can't send signals to this remote system. %s not sent.\n",
5998 gdb_signal_to_name (rs->last_sent_signal));
5999 rs->last_sent_signal = GDB_SIGNAL_0;
6000 target_terminal_inferior ();
6002 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
6003 putpkt ((char *) buf);
6005 /* We just told the target to resume, so a stop reply is in
6007 rs->waiting_for_stop_reply = 1;
6010 /* else fallthrough */
6012 warning (_("Invalid remote reply: %s"), buf);
6014 rs->waiting_for_stop_reply = 1;
6018 if (status->kind == TARGET_WAITKIND_IGNORE)
6020 /* Nothing interesting happened. If we're doing a non-blocking
6021 poll, we're done. Otherwise, go back to waiting. */
6022 if (options & TARGET_WNOHANG)
6023 return minus_one_ptid;
6027 else if (status->kind != TARGET_WAITKIND_EXITED
6028 && status->kind != TARGET_WAITKIND_SIGNALLED)
6030 if (!ptid_equal (event_ptid, null_ptid))
6031 record_currthread (rs, event_ptid);
6033 event_ptid = inferior_ptid;
6036 /* A process exit. Invalidate our notion of current thread. */
6037 record_currthread (rs, minus_one_ptid);
6042 /* Wait until the remote machine stops, then return, storing status in
6043 STATUS just as `wait' would. */
6046 remote_wait (struct target_ops *ops,
6047 ptid_t ptid, struct target_waitstatus *status, int options)
6052 event_ptid = remote_wait_ns (ptid, status, options);
6054 event_ptid = remote_wait_as (ptid, status, options);
6056 if (target_is_async_p ())
6058 /* If there are are events left in the queue tell the event loop
6060 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
6061 mark_async_event_handler (remote_async_inferior_event_token);
6067 /* Fetch a single register using a 'p' packet. */
6070 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
6072 struct remote_state *rs = get_remote_state ();
6074 char regp[MAX_REGISTER_SIZE];
6077 if (packet_support (PACKET_p) == PACKET_DISABLE)
6080 if (reg->pnum == -1)
6085 p += hexnumstr (p, reg->pnum);
6088 getpkt (&rs->buf, &rs->buf_size, 0);
6092 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6096 case PACKET_UNKNOWN:
6099 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6100 gdbarch_register_name (get_regcache_arch (regcache),
6105 /* If this register is unfetchable, tell the regcache. */
6108 regcache_raw_supply (regcache, reg->regnum, NULL);
6112 /* Otherwise, parse and supply the value. */
6118 error (_("fetch_register_using_p: early buf termination"));
6120 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6123 regcache_raw_supply (regcache, reg->regnum, regp);
6127 /* Fetch the registers included in the target's 'g' packet. */
6130 send_g_packet (void)
6132 struct remote_state *rs = get_remote_state ();
6135 xsnprintf (rs->buf, get_remote_packet_size (), "g");
6136 remote_send (&rs->buf, &rs->buf_size);
6138 /* We can get out of synch in various cases. If the first character
6139 in the buffer is not a hex character, assume that has happened
6140 and try to fetch another packet to read. */
6141 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6142 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6143 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6144 && rs->buf[0] != 'x') /* New: unavailable register value. */
6147 fprintf_unfiltered (gdb_stdlog,
6148 "Bad register packet; fetching a new packet\n");
6149 getpkt (&rs->buf, &rs->buf_size, 0);
6152 buf_len = strlen (rs->buf);
6154 /* Sanity check the received packet. */
6155 if (buf_len % 2 != 0)
6156 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
6162 process_g_packet (struct regcache *regcache)
6164 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6165 struct remote_state *rs = get_remote_state ();
6166 struct remote_arch_state *rsa = get_remote_arch_state ();
6171 buf_len = strlen (rs->buf);
6173 /* Further sanity checks, with knowledge of the architecture. */
6174 if (buf_len > 2 * rsa->sizeof_g_packet)
6175 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6177 /* Save the size of the packet sent to us by the target. It is used
6178 as a heuristic when determining the max size of packets that the
6179 target can safely receive. */
6180 if (rsa->actual_register_packet_size == 0)
6181 rsa->actual_register_packet_size = buf_len;
6183 /* If this is smaller than we guessed the 'g' packet would be,
6184 update our records. A 'g' reply that doesn't include a register's
6185 value implies either that the register is not available, or that
6186 the 'p' packet must be used. */
6187 if (buf_len < 2 * rsa->sizeof_g_packet)
6189 rsa->sizeof_g_packet = buf_len / 2;
6191 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6193 if (rsa->regs[i].pnum == -1)
6196 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6197 rsa->regs[i].in_g_packet = 0;
6199 rsa->regs[i].in_g_packet = 1;
6203 regs = alloca (rsa->sizeof_g_packet);
6205 /* Unimplemented registers read as all bits zero. */
6206 memset (regs, 0, rsa->sizeof_g_packet);
6208 /* Reply describes registers byte by byte, each byte encoded as two
6209 hex characters. Suck them all up, then supply them to the
6210 register cacheing/storage mechanism. */
6213 for (i = 0; i < rsa->sizeof_g_packet; i++)
6215 if (p[0] == 0 || p[1] == 0)
6216 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6217 internal_error (__FILE__, __LINE__,
6218 _("unexpected end of 'g' packet reply"));
6220 if (p[0] == 'x' && p[1] == 'x')
6221 regs[i] = 0; /* 'x' */
6223 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6227 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6229 struct packet_reg *r = &rsa->regs[i];
6233 if (r->offset * 2 >= strlen (rs->buf))
6234 /* This shouldn't happen - we adjusted in_g_packet above. */
6235 internal_error (__FILE__, __LINE__,
6236 _("unexpected end of 'g' packet reply"));
6237 else if (rs->buf[r->offset * 2] == 'x')
6239 gdb_assert (r->offset * 2 < strlen (rs->buf));
6240 /* The register isn't available, mark it as such (at
6241 the same time setting the value to zero). */
6242 regcache_raw_supply (regcache, r->regnum, NULL);
6245 regcache_raw_supply (regcache, r->regnum,
6252 fetch_registers_using_g (struct regcache *regcache)
6255 process_g_packet (regcache);
6258 /* Make the remote selected traceframe match GDB's selected
6262 set_remote_traceframe (void)
6265 struct remote_state *rs = get_remote_state ();
6267 if (rs->remote_traceframe_number == get_traceframe_number ())
6270 /* Avoid recursion, remote_trace_find calls us again. */
6271 rs->remote_traceframe_number = get_traceframe_number ();
6273 newnum = target_trace_find (tfind_number,
6274 get_traceframe_number (), 0, 0, NULL);
6276 /* Should not happen. If it does, all bets are off. */
6277 if (newnum != get_traceframe_number ())
6278 warning (_("could not set remote traceframe"));
6282 remote_fetch_registers (struct target_ops *ops,
6283 struct regcache *regcache, int regnum)
6285 struct remote_arch_state *rsa = get_remote_arch_state ();
6288 set_remote_traceframe ();
6289 set_general_thread (inferior_ptid);
6293 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6295 gdb_assert (reg != NULL);
6297 /* If this register might be in the 'g' packet, try that first -
6298 we are likely to read more than one register. If this is the
6299 first 'g' packet, we might be overly optimistic about its
6300 contents, so fall back to 'p'. */
6301 if (reg->in_g_packet)
6303 fetch_registers_using_g (regcache);
6304 if (reg->in_g_packet)
6308 if (fetch_register_using_p (regcache, reg))
6311 /* This register is not available. */
6312 regcache_raw_supply (regcache, reg->regnum, NULL);
6317 fetch_registers_using_g (regcache);
6319 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6320 if (!rsa->regs[i].in_g_packet)
6321 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
6323 /* This register is not available. */
6324 regcache_raw_supply (regcache, i, NULL);
6328 /* Prepare to store registers. Since we may send them all (using a
6329 'G' request), we have to read out the ones we don't want to change
6333 remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
6335 struct remote_arch_state *rsa = get_remote_arch_state ();
6337 gdb_byte buf[MAX_REGISTER_SIZE];
6339 /* Make sure the entire registers array is valid. */
6340 switch (packet_support (PACKET_P))
6342 case PACKET_DISABLE:
6343 case PACKET_SUPPORT_UNKNOWN:
6344 /* Make sure all the necessary registers are cached. */
6345 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6346 if (rsa->regs[i].in_g_packet)
6347 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
6354 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6355 packet was not recognized. */
6358 store_register_using_P (const struct regcache *regcache,
6359 struct packet_reg *reg)
6361 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6362 struct remote_state *rs = get_remote_state ();
6363 /* Try storing a single register. */
6364 char *buf = rs->buf;
6365 gdb_byte regp[MAX_REGISTER_SIZE];
6368 if (packet_support (PACKET_P) == PACKET_DISABLE)
6371 if (reg->pnum == -1)
6374 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
6375 p = buf + strlen (buf);
6376 regcache_raw_collect (regcache, reg->regnum, regp);
6377 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6379 getpkt (&rs->buf, &rs->buf_size, 0);
6381 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6386 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6387 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
6388 case PACKET_UNKNOWN:
6391 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6395 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6396 contents of the register cache buffer. FIXME: ignores errors. */
6399 store_registers_using_G (const struct regcache *regcache)
6401 struct remote_state *rs = get_remote_state ();
6402 struct remote_arch_state *rsa = get_remote_arch_state ();
6406 /* Extract all the registers in the regcache copying them into a
6411 regs = alloca (rsa->sizeof_g_packet);
6412 memset (regs, 0, rsa->sizeof_g_packet);
6413 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6415 struct packet_reg *r = &rsa->regs[i];
6418 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
6422 /* Command describes registers byte by byte,
6423 each byte encoded as two hex characters. */
6426 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6428 bin2hex (regs, p, rsa->sizeof_g_packet);
6430 getpkt (&rs->buf, &rs->buf_size, 0);
6431 if (packet_check_result (rs->buf) == PACKET_ERROR)
6432 error (_("Could not write registers; remote failure reply '%s'"),
6436 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6437 of the register cache buffer. FIXME: ignores errors. */
6440 remote_store_registers (struct target_ops *ops,
6441 struct regcache *regcache, int regnum)
6443 struct remote_arch_state *rsa = get_remote_arch_state ();
6446 set_remote_traceframe ();
6447 set_general_thread (inferior_ptid);
6451 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6453 gdb_assert (reg != NULL);
6455 /* Always prefer to store registers using the 'P' packet if
6456 possible; we often change only a small number of registers.
6457 Sometimes we change a larger number; we'd need help from a
6458 higher layer to know to use 'G'. */
6459 if (store_register_using_P (regcache, reg))
6462 /* For now, don't complain if we have no way to write the
6463 register. GDB loses track of unavailable registers too
6464 easily. Some day, this may be an error. We don't have
6465 any way to read the register, either... */
6466 if (!reg->in_g_packet)
6469 store_registers_using_G (regcache);
6473 store_registers_using_G (regcache);
6475 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6476 if (!rsa->regs[i].in_g_packet)
6477 if (!store_register_using_P (regcache, &rsa->regs[i]))
6478 /* See above for why we do not issue an error here. */
6483 /* Return the number of hex digits in num. */
6486 hexnumlen (ULONGEST num)
6490 for (i = 0; num != 0; i++)
6496 /* Set BUF to the minimum number of hex digits representing NUM. */
6499 hexnumstr (char *buf, ULONGEST num)
6501 int len = hexnumlen (num);
6503 return hexnumnstr (buf, num, len);
6507 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6510 hexnumnstr (char *buf, ULONGEST num, int width)
6516 for (i = width - 1; i >= 0; i--)
6518 buf[i] = "0123456789abcdef"[(num & 0xf)];
6525 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6528 remote_address_masked (CORE_ADDR addr)
6530 unsigned int address_size = remote_address_size;
6532 /* If "remoteaddresssize" was not set, default to target address size. */
6534 address_size = gdbarch_addr_bit (target_gdbarch ());
6536 if (address_size > 0
6537 && address_size < (sizeof (ULONGEST) * 8))
6539 /* Only create a mask when that mask can safely be constructed
6540 in a ULONGEST variable. */
6543 mask = (mask << address_size) - 1;
6549 /* Determine whether the remote target supports binary downloading.
6550 This is accomplished by sending a no-op memory write of zero length
6551 to the target at the specified address. It does not suffice to send
6552 the whole packet, since many stubs strip the eighth bit and
6553 subsequently compute a wrong checksum, which causes real havoc with
6556 NOTE: This can still lose if the serial line is not eight-bit
6557 clean. In cases like this, the user should clear "remote
6561 check_binary_download (CORE_ADDR addr)
6563 struct remote_state *rs = get_remote_state ();
6565 switch (packet_support (PACKET_X))
6567 case PACKET_DISABLE:
6571 case PACKET_SUPPORT_UNKNOWN:
6577 p += hexnumstr (p, (ULONGEST) addr);
6579 p += hexnumstr (p, (ULONGEST) 0);
6583 putpkt_binary (rs->buf, (int) (p - rs->buf));
6584 getpkt (&rs->buf, &rs->buf_size, 0);
6586 if (rs->buf[0] == '\0')
6589 fprintf_unfiltered (gdb_stdlog,
6590 "binary downloading NOT "
6591 "supported by target\n");
6592 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6597 fprintf_unfiltered (gdb_stdlog,
6598 "binary downloading supported by target\n");
6599 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6606 /* Write memory data directly to the remote machine.
6607 This does not inform the data cache; the data cache uses this.
6608 HEADER is the starting part of the packet.
6609 MEMADDR is the address in the remote memory space.
6610 MYADDR is the address of the buffer in our space.
6611 LEN is the number of bytes.
6612 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6613 should send data as binary ('X'), or hex-encoded ('M').
6615 The function creates packet of the form
6616 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6618 where encoding of <DATA> is termined by PACKET_FORMAT.
6620 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6623 Return the transferred status, error or OK (an
6624 'enum target_xfer_status' value). Save the number of bytes
6625 transferred in *XFERED_LEN. Only transfer a single packet. */
6627 static enum target_xfer_status
6628 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6629 const gdb_byte *myaddr, ULONGEST len,
6630 ULONGEST *xfered_len, char packet_format,
6633 struct remote_state *rs = get_remote_state ();
6643 if (packet_format != 'X' && packet_format != 'M')
6644 internal_error (__FILE__, __LINE__,
6645 _("remote_write_bytes_aux: bad packet format"));
6648 return TARGET_XFER_EOF;
6650 payload_size = get_memory_write_packet_size ();
6652 /* The packet buffer will be large enough for the payload;
6653 get_memory_packet_size ensures this. */
6656 /* Compute the size of the actual payload by subtracting out the
6657 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6659 payload_size -= strlen ("$,:#NN");
6661 /* The comma won't be used. */
6663 header_length = strlen (header);
6664 payload_size -= header_length;
6665 payload_size -= hexnumlen (memaddr);
6667 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6669 strcat (rs->buf, header);
6670 p = rs->buf + strlen (header);
6672 /* Compute a best guess of the number of bytes actually transfered. */
6673 if (packet_format == 'X')
6675 /* Best guess at number of bytes that will fit. */
6676 todo = min (len, payload_size);
6678 payload_size -= hexnumlen (todo);
6679 todo = min (todo, payload_size);
6683 /* Num bytes that will fit. */
6684 todo = min (len, payload_size / 2);
6686 payload_size -= hexnumlen (todo);
6687 todo = min (todo, payload_size / 2);
6691 internal_error (__FILE__, __LINE__,
6692 _("minimum packet size too small to write data"));
6694 /* If we already need another packet, then try to align the end
6695 of this packet to a useful boundary. */
6696 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6697 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6699 /* Append "<memaddr>". */
6700 memaddr = remote_address_masked (memaddr);
6701 p += hexnumstr (p, (ULONGEST) memaddr);
6708 /* Append <len>. Retain the location/size of <len>. It may need to
6709 be adjusted once the packet body has been created. */
6711 plenlen = hexnumstr (p, (ULONGEST) todo);
6719 /* Append the packet body. */
6720 if (packet_format == 'X')
6722 /* Binary mode. Send target system values byte by byte, in
6723 increasing byte addresses. Only escape certain critical
6725 payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p,
6726 &nr_bytes, payload_size);
6728 /* If not all TODO bytes fit, then we'll need another packet. Make
6729 a second try to keep the end of the packet aligned. Don't do
6730 this if the packet is tiny. */
6731 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6735 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6737 if (new_nr_bytes != nr_bytes)
6738 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6739 (gdb_byte *) p, &nr_bytes,
6743 p += payload_length;
6744 if (use_length && nr_bytes < todo)
6746 /* Escape chars have filled up the buffer prematurely,
6747 and we have actually sent fewer bytes than planned.
6748 Fix-up the length field of the packet. Use the same
6749 number of characters as before. */
6750 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6751 *plen = ':'; /* overwrite \0 from hexnumnstr() */
6756 /* Normal mode: Send target system values byte by byte, in
6757 increasing byte addresses. Each byte is encoded as a two hex
6759 nr_bytes = bin2hex (myaddr, p, todo);
6763 putpkt_binary (rs->buf, (int) (p - rs->buf));
6764 getpkt (&rs->buf, &rs->buf_size, 0);
6766 if (rs->buf[0] == 'E')
6767 return TARGET_XFER_E_IO;
6769 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6770 fewer bytes than we'd planned. */
6771 *xfered_len = (ULONGEST) nr_bytes;
6772 return TARGET_XFER_OK;
6775 /* Write memory data directly to the remote machine.
6776 This does not inform the data cache; the data cache uses this.
6777 MEMADDR is the address in the remote memory space.
6778 MYADDR is the address of the buffer in our space.
6779 LEN is the number of bytes.
6781 Return the transferred status, error or OK (an
6782 'enum target_xfer_status' value). Save the number of bytes
6783 transferred in *XFERED_LEN. Only transfer a single packet. */
6785 static enum target_xfer_status
6786 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
6787 ULONGEST *xfered_len)
6789 char *packet_format = 0;
6791 /* Check whether the target supports binary download. */
6792 check_binary_download (memaddr);
6794 switch (packet_support (PACKET_X))
6797 packet_format = "X";
6799 case PACKET_DISABLE:
6800 packet_format = "M";
6802 case PACKET_SUPPORT_UNKNOWN:
6803 internal_error (__FILE__, __LINE__,
6804 _("remote_write_bytes: bad internal state"));
6806 internal_error (__FILE__, __LINE__, _("bad switch"));
6809 return remote_write_bytes_aux (packet_format,
6810 memaddr, myaddr, len, xfered_len,
6811 packet_format[0], 1);
6814 /* Read memory data directly from the remote machine.
6815 This does not use the data cache; the data cache uses this.
6816 MEMADDR is the address in the remote memory space.
6817 MYADDR is the address of the buffer in our space.
6818 LEN is the number of bytes.
6820 Return the transferred status, error or OK (an
6821 'enum target_xfer_status' value). Save the number of bytes
6822 transferred in *XFERED_LEN. */
6824 static enum target_xfer_status
6825 remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len,
6826 ULONGEST *xfered_len)
6828 struct remote_state *rs = get_remote_state ();
6829 int max_buf_size; /* Max size of packet output buffer. */
6834 max_buf_size = get_memory_read_packet_size ();
6835 /* The packet buffer will be large enough for the payload;
6836 get_memory_packet_size ensures this. */
6838 /* Number if bytes that will fit. */
6839 todo = min (len, max_buf_size / 2);
6841 /* Construct "m"<memaddr>","<len>". */
6842 memaddr = remote_address_masked (memaddr);
6845 p += hexnumstr (p, (ULONGEST) memaddr);
6847 p += hexnumstr (p, (ULONGEST) todo);
6850 getpkt (&rs->buf, &rs->buf_size, 0);
6851 if (rs->buf[0] == 'E'
6852 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6853 && rs->buf[3] == '\0')
6854 return TARGET_XFER_E_IO;
6855 /* Reply describes memory byte by byte, each byte encoded as two hex
6858 i = hex2bin (p, myaddr, todo);
6859 /* Return what we have. Let higher layers handle partial reads. */
6860 *xfered_len = (ULONGEST) i;
6861 return TARGET_XFER_OK;
6864 /* Using the set of read-only target sections of remote, read live
6867 For interface/parameters/return description see target.h,
6870 static enum target_xfer_status
6871 remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
6872 ULONGEST memaddr, ULONGEST len,
6873 ULONGEST *xfered_len)
6875 struct target_section *secp;
6876 struct target_section_table *table;
6878 secp = target_section_by_addr (ops, memaddr);
6880 && (bfd_get_section_flags (secp->the_bfd_section->owner,
6881 secp->the_bfd_section)
6884 struct target_section *p;
6885 ULONGEST memend = memaddr + len;
6887 table = target_get_section_table (ops);
6889 for (p = table->sections; p < table->sections_end; p++)
6891 if (memaddr >= p->addr)
6893 if (memend <= p->endaddr)
6895 /* Entire transfer is within this section. */
6896 return remote_read_bytes_1 (memaddr, readbuf, len,
6899 else if (memaddr >= p->endaddr)
6901 /* This section ends before the transfer starts. */
6906 /* This section overlaps the transfer. Just do half. */
6907 len = p->endaddr - memaddr;
6908 return remote_read_bytes_1 (memaddr, readbuf, len,
6915 return TARGET_XFER_EOF;
6918 /* Similar to remote_read_bytes_1, but it reads from the remote stub
6919 first if the requested memory is unavailable in traceframe.
6920 Otherwise, fall back to remote_read_bytes_1. */
6922 static enum target_xfer_status
6923 remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
6924 gdb_byte *myaddr, ULONGEST len, ULONGEST *xfered_len)
6927 return TARGET_XFER_EOF;
6929 if (get_traceframe_number () != -1)
6931 VEC(mem_range_s) *available;
6933 /* If we fail to get the set of available memory, then the
6934 target does not support querying traceframe info, and so we
6935 attempt reading from the traceframe anyway (assuming the
6936 target implements the old QTro packet then). */
6937 if (traceframe_available_memory (&available, memaddr, len))
6939 struct cleanup *old_chain;
6941 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
6943 if (VEC_empty (mem_range_s, available)
6944 || VEC_index (mem_range_s, available, 0)->start != memaddr)
6946 enum target_xfer_status res;
6948 /* Don't read into the traceframe's available
6950 if (!VEC_empty (mem_range_s, available))
6952 LONGEST oldlen = len;
6954 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
6955 gdb_assert (len <= oldlen);
6958 do_cleanups (old_chain);
6960 /* This goes through the topmost target again. */
6961 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
6963 if (res == TARGET_XFER_OK)
6964 return TARGET_XFER_OK;
6967 /* No use trying further, we know some memory starting
6968 at MEMADDR isn't available. */
6970 return TARGET_XFER_UNAVAILABLE;
6974 /* Don't try to read more than how much is available, in
6975 case the target implements the deprecated QTro packet to
6976 cater for older GDBs (the target's knowledge of read-only
6977 sections may be outdated by now). */
6978 len = VEC_index (mem_range_s, available, 0)->length;
6980 do_cleanups (old_chain);
6984 return remote_read_bytes_1 (memaddr, myaddr, len, xfered_len);
6989 /* Sends a packet with content determined by the printf format string
6990 FORMAT and the remaining arguments, then gets the reply. Returns
6991 whether the packet was a success, a failure, or unknown. */
6993 static enum packet_result
6994 remote_send_printf (const char *format, ...)
6996 struct remote_state *rs = get_remote_state ();
6997 int max_size = get_remote_packet_size ();
7000 va_start (ap, format);
7003 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
7004 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
7006 if (putpkt (rs->buf) < 0)
7007 error (_("Communication problem with target."));
7010 getpkt (&rs->buf, &rs->buf_size, 0);
7012 return packet_check_result (rs->buf);
7016 restore_remote_timeout (void *p)
7018 int value = *(int *)p;
7020 remote_timeout = value;
7023 /* Flash writing can take quite some time. We'll set
7024 effectively infinite timeout for flash operations.
7025 In future, we'll need to decide on a better approach. */
7026 static const int remote_flash_timeout = 1000;
7029 remote_flash_erase (struct target_ops *ops,
7030 ULONGEST address, LONGEST length)
7032 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
7033 int saved_remote_timeout = remote_timeout;
7034 enum packet_result ret;
7035 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7036 &saved_remote_timeout);
7038 remote_timeout = remote_flash_timeout;
7040 ret = remote_send_printf ("vFlashErase:%s,%s",
7041 phex (address, addr_size),
7045 case PACKET_UNKNOWN:
7046 error (_("Remote target does not support flash erase"));
7048 error (_("Error erasing flash with vFlashErase packet"));
7053 do_cleanups (back_to);
7056 static enum target_xfer_status
7057 remote_flash_write (struct target_ops *ops, ULONGEST address,
7058 ULONGEST length, ULONGEST *xfered_len,
7059 const gdb_byte *data)
7061 int saved_remote_timeout = remote_timeout;
7062 enum target_xfer_status ret;
7063 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7064 &saved_remote_timeout);
7066 remote_timeout = remote_flash_timeout;
7067 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length,
7069 do_cleanups (back_to);
7075 remote_flash_done (struct target_ops *ops)
7077 int saved_remote_timeout = remote_timeout;
7079 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7080 &saved_remote_timeout);
7082 remote_timeout = remote_flash_timeout;
7083 ret = remote_send_printf ("vFlashDone");
7084 do_cleanups (back_to);
7088 case PACKET_UNKNOWN:
7089 error (_("Remote target does not support vFlashDone"));
7091 error (_("Error finishing flash operation"));
7098 remote_files_info (struct target_ops *ignore)
7100 puts_filtered ("Debugging a target over a serial line.\n");
7103 /* Stuff for dealing with the packets which are part of this protocol.
7104 See comment at top of file for details. */
7106 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7107 error to higher layers. Called when a serial error is detected.
7108 The exception message is STRING, followed by a colon and a blank,
7109 the system error message for errno at function entry and final dot
7110 for output compatibility with throw_perror_with_name. */
7113 unpush_and_perror (const char *string)
7115 int saved_errno = errno;
7117 remote_unpush_target ();
7118 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7119 safe_strerror (saved_errno));
7122 /* Read a single character from the remote end. */
7125 readchar (int timeout)
7128 struct remote_state *rs = get_remote_state ();
7130 ch = serial_readchar (rs->remote_desc, timeout);
7135 switch ((enum serial_rc) ch)
7138 remote_unpush_target ();
7139 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
7142 unpush_and_perror (_("Remote communication error. "
7143 "Target disconnected."));
7145 case SERIAL_TIMEOUT:
7151 /* Wrapper for serial_write that closes the target and throws if
7155 remote_serial_write (const char *str, int len)
7157 struct remote_state *rs = get_remote_state ();
7159 if (serial_write (rs->remote_desc, str, len))
7161 unpush_and_perror (_("Remote communication error. "
7162 "Target disconnected."));
7166 /* Send the command in *BUF to the remote machine, and read the reply
7167 into *BUF. Report an error if we get an error reply. Resize
7168 *BUF using xrealloc if necessary to hold the result, and update
7172 remote_send (char **buf,
7176 getpkt (buf, sizeof_buf, 0);
7178 if ((*buf)[0] == 'E')
7179 error (_("Remote failure reply: %s"), *buf);
7182 /* Return a pointer to an xmalloc'ed string representing an escaped
7183 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7184 etc. The caller is responsible for releasing the returned
7188 escape_buffer (const char *buf, int n)
7190 struct cleanup *old_chain;
7191 struct ui_file *stb;
7194 stb = mem_fileopen ();
7195 old_chain = make_cleanup_ui_file_delete (stb);
7197 fputstrn_unfiltered (buf, n, '\\', stb);
7198 str = ui_file_xstrdup (stb, NULL);
7199 do_cleanups (old_chain);
7203 /* Display a null-terminated packet on stdout, for debugging, using C
7207 print_packet (const char *buf)
7209 puts_filtered ("\"");
7210 fputstr_filtered (buf, '"', gdb_stdout);
7211 puts_filtered ("\"");
7215 putpkt (const char *buf)
7217 return putpkt_binary (buf, strlen (buf));
7220 /* Send a packet to the remote machine, with error checking. The data
7221 of the packet is in BUF. The string in BUF can be at most
7222 get_remote_packet_size () - 5 to account for the $, # and checksum,
7223 and for a possible /0 if we are debugging (remote_debug) and want
7224 to print the sent packet as a string. */
7227 putpkt_binary (const char *buf, int cnt)
7229 struct remote_state *rs = get_remote_state ();
7231 unsigned char csum = 0;
7232 char *buf2 = alloca (cnt + 6);
7239 /* Catch cases like trying to read memory or listing threads while
7240 we're waiting for a stop reply. The remote server wouldn't be
7241 ready to handle this request, so we'd hang and timeout. We don't
7242 have to worry about this in synchronous mode, because in that
7243 case it's not possible to issue a command while the target is
7244 running. This is not a problem in non-stop mode, because in that
7245 case, the stub is always ready to process serial input. */
7246 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
7248 error (_("Cannot execute this command while the target is running.\n"
7249 "Use the \"interrupt\" command to stop the target\n"
7250 "and then try again."));
7253 /* We're sending out a new packet. Make sure we don't look at a
7254 stale cached response. */
7255 rs->cached_wait_status = 0;
7257 /* Copy the packet into buffer BUF2, encapsulating it
7258 and giving it a checksum. */
7263 for (i = 0; i < cnt; i++)
7269 *p++ = tohex ((csum >> 4) & 0xf);
7270 *p++ = tohex (csum & 0xf);
7272 /* Send it over and over until we get a positive ack. */
7276 int started_error_output = 0;
7280 struct cleanup *old_chain;
7284 str = escape_buffer (buf2, p - buf2);
7285 old_chain = make_cleanup (xfree, str);
7286 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
7287 gdb_flush (gdb_stdlog);
7288 do_cleanups (old_chain);
7290 remote_serial_write (buf2, p - buf2);
7292 /* If this is a no acks version of the remote protocol, send the
7293 packet and move on. */
7297 /* Read until either a timeout occurs (-2) or '+' is read.
7298 Handle any notification that arrives in the mean time. */
7301 ch = readchar (remote_timeout);
7309 case SERIAL_TIMEOUT:
7312 if (started_error_output)
7314 putchar_unfiltered ('\n');
7315 started_error_output = 0;
7324 fprintf_unfiltered (gdb_stdlog, "Ack\n");
7328 fprintf_unfiltered (gdb_stdlog, "Nak\n");
7330 case SERIAL_TIMEOUT:
7334 break; /* Retransmit buffer. */
7338 fprintf_unfiltered (gdb_stdlog,
7339 "Packet instead of Ack, ignoring it\n");
7340 /* It's probably an old response sent because an ACK
7341 was lost. Gobble up the packet and ack it so it
7342 doesn't get retransmitted when we resend this
7345 remote_serial_write ("+", 1);
7346 continue; /* Now, go look for +. */
7353 /* If we got a notification, handle it, and go back to looking
7355 /* We've found the start of a notification. Now
7356 collect the data. */
7357 val = read_frame (&rs->buf, &rs->buf_size);
7362 struct cleanup *old_chain;
7365 str = escape_buffer (rs->buf, val);
7366 old_chain = make_cleanup (xfree, str);
7367 fprintf_unfiltered (gdb_stdlog,
7368 " Notification received: %s\n",
7370 do_cleanups (old_chain);
7372 handle_notification (rs->notif_state, rs->buf);
7373 /* We're in sync now, rewait for the ack. */
7380 if (!started_error_output)
7382 started_error_output = 1;
7383 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7385 fputc_unfiltered (ch & 0177, gdb_stdlog);
7386 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7395 if (!started_error_output)
7397 started_error_output = 1;
7398 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7400 fputc_unfiltered (ch & 0177, gdb_stdlog);
7404 break; /* Here to retransmit. */
7408 /* This is wrong. If doing a long backtrace, the user should be
7409 able to get out next time we call QUIT, without anything as
7410 violent as interrupt_query. If we want to provide a way out of
7411 here without getting to the next QUIT, it should be based on
7412 hitting ^C twice as in remote_wait. */
7423 /* Come here after finding the start of a frame when we expected an
7424 ack. Do our best to discard the rest of this packet. */
7433 c = readchar (remote_timeout);
7436 case SERIAL_TIMEOUT:
7437 /* Nothing we can do. */
7440 /* Discard the two bytes of checksum and stop. */
7441 c = readchar (remote_timeout);
7443 c = readchar (remote_timeout);
7446 case '*': /* Run length encoding. */
7447 /* Discard the repeat count. */
7448 c = readchar (remote_timeout);
7453 /* A regular character. */
7459 /* Come here after finding the start of the frame. Collect the rest
7460 into *BUF, verifying the checksum, length, and handling run-length
7461 compression. NUL terminate the buffer. If there is not enough room,
7462 expand *BUF using xrealloc.
7464 Returns -1 on error, number of characters in buffer (ignoring the
7465 trailing NULL) on success. (could be extended to return one of the
7466 SERIAL status indications). */
7469 read_frame (char **buf_p,
7476 struct remote_state *rs = get_remote_state ();
7483 c = readchar (remote_timeout);
7486 case SERIAL_TIMEOUT:
7488 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
7492 fputs_filtered ("Saw new packet start in middle of old one\n",
7494 return -1; /* Start a new packet, count retries. */
7497 unsigned char pktcsum;
7503 check_0 = readchar (remote_timeout);
7505 check_1 = readchar (remote_timeout);
7507 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7510 fputs_filtered ("Timeout in checksum, retrying\n",
7514 else if (check_0 < 0 || check_1 < 0)
7517 fputs_filtered ("Communication error in checksum\n",
7522 /* Don't recompute the checksum; with no ack packets we
7523 don't have any way to indicate a packet retransmission
7528 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7529 if (csum == pktcsum)
7534 struct cleanup *old_chain;
7537 str = escape_buffer (buf, bc);
7538 old_chain = make_cleanup (xfree, str);
7539 fprintf_unfiltered (gdb_stdlog,
7540 "Bad checksum, sentsum=0x%x, "
7541 "csum=0x%x, buf=%s\n",
7542 pktcsum, csum, str);
7543 do_cleanups (old_chain);
7545 /* Number of characters in buffer ignoring trailing
7549 case '*': /* Run length encoding. */
7554 c = readchar (remote_timeout);
7556 repeat = c - ' ' + 3; /* Compute repeat count. */
7558 /* The character before ``*'' is repeated. */
7560 if (repeat > 0 && repeat <= 255 && bc > 0)
7562 if (bc + repeat - 1 >= *sizeof_buf - 1)
7564 /* Make some more room in the buffer. */
7565 *sizeof_buf += repeat;
7566 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7570 memset (&buf[bc], buf[bc - 1], repeat);
7576 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7580 if (bc >= *sizeof_buf - 1)
7582 /* Make some more room in the buffer. */
7584 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7595 /* Read a packet from the remote machine, with error checking, and
7596 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7597 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7598 rather than timing out; this is used (in synchronous mode) to wait
7599 for a target that is is executing user code to stop. */
7600 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7601 don't have to change all the calls to getpkt to deal with the
7602 return value, because at the moment I don't know what the right
7603 thing to do it for those. */
7611 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7615 /* Read a packet from the remote machine, with error checking, and
7616 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7617 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7618 rather than timing out; this is used (in synchronous mode) to wait
7619 for a target that is is executing user code to stop. If FOREVER ==
7620 0, this function is allowed to time out gracefully and return an
7621 indication of this to the caller. Otherwise return the number of
7622 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7623 enough reason to return to the caller. *IS_NOTIF is an output
7624 boolean that indicates whether *BUF holds a notification or not
7625 (a regular packet). */
7628 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7629 int expecting_notif, int *is_notif)
7631 struct remote_state *rs = get_remote_state ();
7637 /* We're reading a new response. Make sure we don't look at a
7638 previously cached response. */
7639 rs->cached_wait_status = 0;
7641 strcpy (*buf, "timeout");
7644 timeout = watchdog > 0 ? watchdog : -1;
7645 else if (expecting_notif)
7646 timeout = 0; /* There should already be a char in the buffer. If
7649 timeout = remote_timeout;
7653 /* Process any number of notifications, and then return when
7657 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
7659 for (tries = 1; tries <= MAX_TRIES; tries++)
7661 /* This can loop forever if the remote side sends us
7662 characters continuously, but if it pauses, we'll get
7663 SERIAL_TIMEOUT from readchar because of timeout. Then
7664 we'll count that as a retry.
7666 Note that even when forever is set, we will only wait
7667 forever prior to the start of a packet. After that, we
7668 expect characters to arrive at a brisk pace. They should
7669 show up within remote_timeout intervals. */
7671 c = readchar (timeout);
7672 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
7674 if (c == SERIAL_TIMEOUT)
7676 if (expecting_notif)
7677 return -1; /* Don't complain, it's normal to not get
7678 anything in this case. */
7680 if (forever) /* Watchdog went off? Kill the target. */
7683 remote_unpush_target ();
7684 throw_error (TARGET_CLOSE_ERROR,
7685 _("Watchdog timeout has expired. "
7686 "Target detached."));
7689 fputs_filtered ("Timed out.\n", gdb_stdlog);
7693 /* We've found the start of a packet or notification.
7694 Now collect the data. */
7695 val = read_frame (buf, sizeof_buf);
7700 remote_serial_write ("-", 1);
7703 if (tries > MAX_TRIES)
7705 /* We have tried hard enough, and just can't receive the
7706 packet/notification. Give up. */
7707 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7709 /* Skip the ack char if we're in no-ack mode. */
7710 if (!rs->noack_mode)
7711 remote_serial_write ("+", 1);
7715 /* If we got an ordinary packet, return that to our caller. */
7720 struct cleanup *old_chain;
7723 str = escape_buffer (*buf, val);
7724 old_chain = make_cleanup (xfree, str);
7725 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7726 do_cleanups (old_chain);
7729 /* Skip the ack char if we're in no-ack mode. */
7730 if (!rs->noack_mode)
7731 remote_serial_write ("+", 1);
7732 if (is_notif != NULL)
7737 /* If we got a notification, handle it, and go back to looking
7741 gdb_assert (c == '%');
7745 struct cleanup *old_chain;
7748 str = escape_buffer (*buf, val);
7749 old_chain = make_cleanup (xfree, str);
7750 fprintf_unfiltered (gdb_stdlog,
7751 " Notification received: %s\n",
7753 do_cleanups (old_chain);
7755 if (is_notif != NULL)
7758 handle_notification (rs->notif_state, *buf);
7760 /* Notifications require no acknowledgement. */
7762 if (expecting_notif)
7769 getpkt_sane (char **buf, long *sizeof_buf, int forever)
7771 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
7775 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
7778 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
7784 remote_kill (struct target_ops *ops)
7786 volatile struct gdb_exception ex;
7788 /* Catch errors so the user can quit from gdb even when we
7789 aren't on speaking terms with the remote system. */
7790 TRY_CATCH (ex, RETURN_MASK_ERROR)
7796 if (ex.error == TARGET_CLOSE_ERROR)
7798 /* If we got an (EOF) error that caused the target
7799 to go away, then we're done, that's what we wanted.
7800 "k" is susceptible to cause a premature EOF, given
7801 that the remote server isn't actually required to
7802 reply to "k", and it can happen that it doesn't
7803 even get to reply ACK to the "k". */
7807 /* Otherwise, something went wrong. We didn't actually kill
7808 the target. Just propagate the exception, and let the
7809 user or higher layers decide what to do. */
7810 throw_exception (ex);
7813 /* We've killed the remote end, we get to mourn it. Since this is
7814 target remote, single-process, mourning the inferior also
7815 unpushes remote_ops. */
7816 target_mourn_inferior ();
7820 remote_vkill (int pid, struct remote_state *rs)
7822 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
7825 /* Tell the remote target to detach. */
7826 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
7828 getpkt (&rs->buf, &rs->buf_size, 0);
7830 switch (packet_ok (rs->buf,
7831 &remote_protocol_packets[PACKET_vKill]))
7837 case PACKET_UNKNOWN:
7840 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7845 extended_remote_kill (struct target_ops *ops)
7848 int pid = ptid_get_pid (inferior_ptid);
7849 struct remote_state *rs = get_remote_state ();
7851 res = remote_vkill (pid, rs);
7852 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
7854 /* Don't try 'k' on a multi-process aware stub -- it has no way
7855 to specify the pid. */
7859 getpkt (&rs->buf, &rs->buf_size, 0);
7860 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7863 /* Don't wait for it to die. I'm not really sure it matters whether
7864 we do or not. For the existing stubs, kill is a noop. */
7870 error (_("Can't kill process"));
7872 target_mourn_inferior ();
7876 remote_mourn (struct target_ops *ops)
7878 remote_mourn_1 (ops);
7881 /* Worker function for remote_mourn. */
7883 remote_mourn_1 (struct target_ops *target)
7885 unpush_target (target);
7887 /* remote_close takes care of doing most of the clean up. */
7888 generic_mourn_inferior ();
7892 extended_remote_mourn_1 (struct target_ops *target)
7894 struct remote_state *rs = get_remote_state ();
7896 /* In case we got here due to an error, but we're going to stay
7898 rs->waiting_for_stop_reply = 0;
7900 /* If the current general thread belonged to the process we just
7901 detached from or has exited, the remote side current general
7902 thread becomes undefined. Considering a case like this:
7904 - We just got here due to a detach.
7905 - The process that we're detaching from happens to immediately
7906 report a global breakpoint being hit in non-stop mode, in the
7907 same thread we had selected before.
7908 - GDB attaches to this process again.
7909 - This event happens to be the next event we handle.
7911 GDB would consider that the current general thread didn't need to
7912 be set on the stub side (with Hg), since for all it knew,
7913 GENERAL_THREAD hadn't changed.
7915 Notice that although in all-stop mode, the remote server always
7916 sets the current thread to the thread reporting the stop event,
7917 that doesn't happen in non-stop mode; in non-stop, the stub *must
7918 not* change the current thread when reporting a breakpoint hit,
7919 due to the decoupling of event reporting and event handling.
7921 To keep things simple, we always invalidate our notion of the
7923 record_currthread (rs, minus_one_ptid);
7925 /* Unlike "target remote", we do not want to unpush the target; then
7926 the next time the user says "run", we won't be connected. */
7928 /* Call common code to mark the inferior as not running. */
7929 generic_mourn_inferior ();
7931 if (!have_inferiors ())
7933 if (!remote_multi_process_p (rs))
7935 /* Check whether the target is running now - some remote stubs
7936 automatically restart after kill. */
7938 getpkt (&rs->buf, &rs->buf_size, 0);
7940 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7942 /* Assume that the target has been restarted. Set
7943 inferior_ptid so that bits of core GDB realizes
7944 there's something here, e.g., so that the user can
7945 say "kill" again. */
7946 inferior_ptid = magic_null_ptid;
7953 extended_remote_mourn (struct target_ops *ops)
7955 extended_remote_mourn_1 (ops);
7959 extended_remote_supports_disable_randomization (struct target_ops *self)
7961 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
7965 extended_remote_disable_randomization (int val)
7967 struct remote_state *rs = get_remote_state ();
7970 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7973 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7975 error (_("Target does not support QDisableRandomization."));
7976 if (strcmp (reply, "OK") != 0)
7977 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7981 extended_remote_run (char *args)
7983 struct remote_state *rs = get_remote_state ();
7986 /* If the user has disabled vRun support, or we have detected that
7987 support is not available, do not try it. */
7988 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
7991 strcpy (rs->buf, "vRun;");
7992 len = strlen (rs->buf);
7994 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7995 error (_("Remote file name too long for run packet"));
7996 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
7997 strlen (remote_exec_file));
7999 gdb_assert (args != NULL);
8002 struct cleanup *back_to;
8006 argv = gdb_buildargv (args);
8007 back_to = make_cleanup_freeargv (argv);
8008 for (i = 0; argv[i] != NULL; i++)
8010 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8011 error (_("Argument list too long for run packet"));
8012 rs->buf[len++] = ';';
8013 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8016 do_cleanups (back_to);
8019 rs->buf[len++] = '\0';
8022 getpkt (&rs->buf, &rs->buf_size, 0);
8024 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
8027 /* We have a wait response. All is well. */
8029 case PACKET_UNKNOWN:
8032 if (remote_exec_file[0] == '\0')
8033 error (_("Running the default executable on the remote target failed; "
8034 "try \"set remote exec-file\"?"));
8036 error (_("Running \"%s\" on the remote target failed"),
8039 gdb_assert_not_reached (_("bad switch"));
8043 /* In the extended protocol we want to be able to do things like
8044 "run" and have them basically work as expected. So we need
8045 a special create_inferior function. We support changing the
8046 executable file and the command line arguments, but not the
8050 extended_remote_create_inferior (struct target_ops *ops,
8051 char *exec_file, char *args,
8052 char **env, int from_tty)
8056 struct remote_state *rs = get_remote_state ();
8058 /* If running asynchronously, register the target file descriptor
8059 with the event loop. */
8060 if (target_can_async_p ())
8061 target_async (inferior_event_handler, 0);
8063 /* Disable address space randomization if requested (and supported). */
8064 if (extended_remote_supports_disable_randomization (ops))
8065 extended_remote_disable_randomization (disable_randomization);
8067 /* Now restart the remote server. */
8068 run_worked = extended_remote_run (args) != -1;
8071 /* vRun was not supported. Fail if we need it to do what the
8073 if (remote_exec_file[0])
8074 error (_("Remote target does not support \"set remote exec-file\""));
8076 error (_("Remote target does not support \"set args\" or run <ARGS>"));
8078 /* Fall back to "R". */
8079 extended_remote_restart ();
8082 if (!have_inferiors ())
8084 /* Clean up from the last time we ran, before we mark the target
8085 running again. This will mark breakpoints uninserted, and
8086 get_offsets may insert breakpoints. */
8087 init_thread_list ();
8088 init_wait_for_inferior ();
8091 /* vRun's success return is a stop reply. */
8092 stop_reply = run_worked ? rs->buf : NULL;
8093 add_current_inferior_and_thread (stop_reply);
8095 /* Get updated offsets, if the stub uses qOffsets. */
8100 /* Given a location's target info BP_TGT and the packet buffer BUF, output
8101 the list of conditions (in agent expression bytecode format), if any, the
8102 target needs to evaluate. The output is placed into the packet buffer
8103 started from BUF and ended at BUF_END. */
8106 remote_add_target_side_condition (struct gdbarch *gdbarch,
8107 struct bp_target_info *bp_tgt, char *buf,
8110 struct agent_expr *aexpr = NULL;
8113 char *buf_start = buf;
8115 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8118 buf += strlen (buf);
8119 xsnprintf (buf, buf_end - buf, "%s", ";");
8122 /* Send conditions to the target and free the vector. */
8124 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8127 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
8128 buf += strlen (buf);
8129 for (i = 0; i < aexpr->len; ++i)
8130 buf = pack_hex_byte (buf, aexpr->buf[i]);
8137 remote_add_target_side_commands (struct gdbarch *gdbarch,
8138 struct bp_target_info *bp_tgt, char *buf)
8140 struct agent_expr *aexpr = NULL;
8143 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8146 buf += strlen (buf);
8148 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8149 buf += strlen (buf);
8151 /* Concatenate all the agent expressions that are commands into the
8154 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8157 sprintf (buf, "X%x,", aexpr->len);
8158 buf += strlen (buf);
8159 for (i = 0; i < aexpr->len; ++i)
8160 buf = pack_hex_byte (buf, aexpr->buf[i]);
8165 /* Insert a breakpoint. On targets that have software breakpoint
8166 support, we ask the remote target to do the work; on targets
8167 which don't, we insert a traditional memory breakpoint. */
8170 remote_insert_breakpoint (struct target_ops *ops,
8171 struct gdbarch *gdbarch,
8172 struct bp_target_info *bp_tgt)
8174 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8175 If it succeeds, then set the support to PACKET_ENABLE. If it
8176 fails, and the user has explicitly requested the Z support then
8177 report an error, otherwise, mark it disabled and go on. */
8179 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8181 CORE_ADDR addr = bp_tgt->reqstd_address;
8182 struct remote_state *rs;
8185 struct condition_list *cond = NULL;
8187 /* Make sure the remote is pointing at the right process, if
8189 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8190 set_general_process ();
8192 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8194 rs = get_remote_state ();
8196 endbuf = rs->buf + get_remote_packet_size ();
8201 addr = (ULONGEST) remote_address_masked (addr);
8202 p += hexnumstr (p, addr);
8203 xsnprintf (p, endbuf - p, ",%d", bpsize);
8205 if (remote_supports_cond_breakpoints (ops))
8206 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8208 if (remote_can_run_breakpoint_commands (ops))
8209 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8212 getpkt (&rs->buf, &rs->buf_size, 0);
8214 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
8219 bp_tgt->placed_address = addr;
8220 bp_tgt->placed_size = bpsize;
8222 case PACKET_UNKNOWN:
8227 /* If this breakpoint has target-side commands but this stub doesn't
8228 support Z0 packets, throw error. */
8229 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8230 throw_error (NOT_SUPPORTED_ERROR, _("\
8231 Target doesn't support breakpoints that have target side commands."));
8233 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
8237 remote_remove_breakpoint (struct target_ops *ops,
8238 struct gdbarch *gdbarch,
8239 struct bp_target_info *bp_tgt)
8241 CORE_ADDR addr = bp_tgt->placed_address;
8242 struct remote_state *rs = get_remote_state ();
8244 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8247 char *endbuf = rs->buf + get_remote_packet_size ();
8249 /* Make sure the remote is pointing at the right process, if
8251 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8252 set_general_process ();
8258 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8259 p += hexnumstr (p, addr);
8260 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
8263 getpkt (&rs->buf, &rs->buf_size, 0);
8265 return (rs->buf[0] == 'E');
8268 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
8272 watchpoint_to_Z_packet (int type)
8277 return Z_PACKET_WRITE_WP;
8280 return Z_PACKET_READ_WP;
8283 return Z_PACKET_ACCESS_WP;
8286 internal_error (__FILE__, __LINE__,
8287 _("hw_bp_to_z: bad watchpoint type %d"), type);
8292 remote_insert_watchpoint (struct target_ops *self,
8293 CORE_ADDR addr, int len, int type,
8294 struct expression *cond)
8296 struct remote_state *rs = get_remote_state ();
8297 char *endbuf = rs->buf + get_remote_packet_size ();
8299 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8301 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8304 /* Make sure the remote is pointing at the right process, if
8306 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8307 set_general_process ();
8309 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
8310 p = strchr (rs->buf, '\0');
8311 addr = remote_address_masked (addr);
8312 p += hexnumstr (p, (ULONGEST) addr);
8313 xsnprintf (p, endbuf - p, ",%x", len);
8316 getpkt (&rs->buf, &rs->buf_size, 0);
8318 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8322 case PACKET_UNKNOWN:
8327 internal_error (__FILE__, __LINE__,
8328 _("remote_insert_watchpoint: reached end of function"));
8332 remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8333 CORE_ADDR start, int length)
8335 CORE_ADDR diff = remote_address_masked (addr - start);
8337 return diff < length;
8342 remote_remove_watchpoint (struct target_ops *self,
8343 CORE_ADDR addr, int len, int type,
8344 struct expression *cond)
8346 struct remote_state *rs = get_remote_state ();
8347 char *endbuf = rs->buf + get_remote_packet_size ();
8349 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8351 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8354 /* Make sure the remote is pointing at the right process, if
8356 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8357 set_general_process ();
8359 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
8360 p = strchr (rs->buf, '\0');
8361 addr = remote_address_masked (addr);
8362 p += hexnumstr (p, (ULONGEST) addr);
8363 xsnprintf (p, endbuf - p, ",%x", len);
8365 getpkt (&rs->buf, &rs->buf_size, 0);
8367 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8370 case PACKET_UNKNOWN:
8375 internal_error (__FILE__, __LINE__,
8376 _("remote_remove_watchpoint: reached end of function"));
8380 int remote_hw_watchpoint_limit = -1;
8381 int remote_hw_watchpoint_length_limit = -1;
8382 int remote_hw_breakpoint_limit = -1;
8385 remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8386 CORE_ADDR addr, int len)
8388 if (remote_hw_watchpoint_length_limit == 0)
8390 else if (remote_hw_watchpoint_length_limit < 0)
8392 else if (len <= remote_hw_watchpoint_length_limit)
8399 remote_check_watch_resources (struct target_ops *self,
8400 int type, int cnt, int ot)
8402 if (type == bp_hardware_breakpoint)
8404 if (remote_hw_breakpoint_limit == 0)
8406 else if (remote_hw_breakpoint_limit < 0)
8408 else if (cnt <= remote_hw_breakpoint_limit)
8413 if (remote_hw_watchpoint_limit == 0)
8415 else if (remote_hw_watchpoint_limit < 0)
8419 else if (cnt <= remote_hw_watchpoint_limit)
8426 remote_stopped_by_watchpoint (struct target_ops *ops)
8428 struct remote_state *rs = get_remote_state ();
8430 return rs->remote_stopped_by_watchpoint_p;
8434 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
8436 struct remote_state *rs = get_remote_state ();
8439 if (remote_stopped_by_watchpoint (target))
8441 *addr_p = rs->remote_watch_data_address;
8450 remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8451 struct bp_target_info *bp_tgt)
8453 CORE_ADDR addr = bp_tgt->reqstd_address;
8454 struct remote_state *rs;
8459 /* The length field should be set to the size of a breakpoint
8460 instruction, even though we aren't inserting one ourselves. */
8462 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8464 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8467 /* Make sure the remote is pointing at the right process, if
8469 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8470 set_general_process ();
8472 rs = get_remote_state ();
8474 endbuf = rs->buf + get_remote_packet_size ();
8480 addr = remote_address_masked (addr);
8481 p += hexnumstr (p, (ULONGEST) addr);
8482 xsnprintf (p, endbuf - p, ",%x", bpsize);
8484 if (remote_supports_cond_breakpoints (self))
8485 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8487 if (remote_can_run_breakpoint_commands (self))
8488 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8491 getpkt (&rs->buf, &rs->buf_size, 0);
8493 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8496 if (rs->buf[1] == '.')
8498 message = strchr (rs->buf + 2, '.');
8500 error (_("Remote failure reply: %s"), message + 1);
8503 case PACKET_UNKNOWN:
8506 bp_tgt->placed_address = addr;
8507 bp_tgt->placed_size = bpsize;
8510 internal_error (__FILE__, __LINE__,
8511 _("remote_insert_hw_breakpoint: reached end of function"));
8516 remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8517 struct bp_target_info *bp_tgt)
8520 struct remote_state *rs = get_remote_state ();
8522 char *endbuf = rs->buf + get_remote_packet_size ();
8524 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8527 /* Make sure the remote is pointing at the right process, if
8529 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8530 set_general_process ();
8536 addr = remote_address_masked (bp_tgt->placed_address);
8537 p += hexnumstr (p, (ULONGEST) addr);
8538 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8541 getpkt (&rs->buf, &rs->buf_size, 0);
8543 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8546 case PACKET_UNKNOWN:
8551 internal_error (__FILE__, __LINE__,
8552 _("remote_remove_hw_breakpoint: reached end of function"));
8555 /* Verify memory using the "qCRC:" request. */
8558 remote_verify_memory (struct target_ops *ops,
8559 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8561 struct remote_state *rs = get_remote_state ();
8562 unsigned long host_crc, target_crc;
8565 /* It doesn't make sense to use qCRC if the remote target is
8566 connected but not running. */
8567 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
8569 enum packet_result result;
8571 /* Make sure the remote is pointing at the right process. */
8572 set_general_process ();
8574 /* FIXME: assumes lma can fit into long. */
8575 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8576 (long) lma, (long) size);
8579 /* Be clever; compute the host_crc before waiting for target
8581 host_crc = xcrc32 (data, size, 0xffffffff);
8583 getpkt (&rs->buf, &rs->buf_size, 0);
8585 result = packet_ok (rs->buf,
8586 &remote_protocol_packets[PACKET_qCRC]);
8587 if (result == PACKET_ERROR)
8589 else if (result == PACKET_OK)
8591 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8592 target_crc = target_crc * 16 + fromhex (*tmp);
8594 return (host_crc == target_crc);
8598 return simple_verify_memory (ops, data, lma, size);
8601 /* compare-sections command
8603 With no arguments, compares each loadable section in the exec bfd
8604 with the same memory range on the target, and reports mismatches.
8605 Useful for verifying the image on the target against the exec file. */
8608 compare_sections_command (char *args, int from_tty)
8611 struct cleanup *old_chain;
8613 const char *sectname;
8622 error (_("command cannot be used without an exec file"));
8624 /* Make sure the remote is pointing at the right process. */
8625 set_general_process ();
8627 if (args != NULL && strcmp (args, "-r") == 0)
8633 for (s = exec_bfd->sections; s; s = s->next)
8635 if (!(s->flags & SEC_LOAD))
8636 continue; /* Skip non-loadable section. */
8638 if (read_only && (s->flags & SEC_READONLY) == 0)
8639 continue; /* Skip writeable sections */
8641 size = bfd_get_section_size (s);
8643 continue; /* Skip zero-length section. */
8645 sectname = bfd_get_section_name (exec_bfd, s);
8646 if (args && strcmp (args, sectname) != 0)
8647 continue; /* Not the section selected by user. */
8649 matched = 1; /* Do this section. */
8652 sectdata = xmalloc (size);
8653 old_chain = make_cleanup (xfree, sectdata);
8654 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
8656 res = target_verify_memory (sectdata, lma, size);
8659 error (_("target memory fault, section %s, range %s -- %s"), sectname,
8660 paddress (target_gdbarch (), lma),
8661 paddress (target_gdbarch (), lma + size));
8663 printf_filtered ("Section %s, range %s -- %s: ", sectname,
8664 paddress (target_gdbarch (), lma),
8665 paddress (target_gdbarch (), lma + size));
8667 printf_filtered ("matched.\n");
8670 printf_filtered ("MIS-MATCHED!\n");
8674 do_cleanups (old_chain);
8677 warning (_("One or more sections of the target image does not match\n\
8678 the loaded file\n"));
8679 if (args && !matched)
8680 printf_filtered (_("No loaded section named '%s'.\n"), args);
8683 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8684 into remote target. The number of bytes written to the remote
8685 target is returned, or -1 for error. */
8687 static enum target_xfer_status
8688 remote_write_qxfer (struct target_ops *ops, const char *object_name,
8689 const char *annex, const gdb_byte *writebuf,
8690 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
8691 struct packet_config *packet)
8695 struct remote_state *rs = get_remote_state ();
8696 int max_size = get_memory_write_packet_size ();
8698 if (packet->support == PACKET_DISABLE)
8699 return TARGET_XFER_E_IO;
8701 /* Insert header. */
8702 i = snprintf (rs->buf, max_size,
8703 "qXfer:%s:write:%s:%s:",
8704 object_name, annex ? annex : "",
8705 phex_nz (offset, sizeof offset));
8706 max_size -= (i + 1);
8708 /* Escape as much data as fits into rs->buf. */
8709 buf_len = remote_escape_output
8710 (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size);
8712 if (putpkt_binary (rs->buf, i + buf_len) < 0
8713 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8714 || packet_ok (rs->buf, packet) != PACKET_OK)
8715 return TARGET_XFER_E_IO;
8717 unpack_varlen_hex (rs->buf, &n);
8720 return TARGET_XFER_OK;
8723 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8724 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8725 number of bytes read is returned, or 0 for EOF, or -1 for error.
8726 The number of bytes read may be less than LEN without indicating an
8727 EOF. PACKET is checked and updated to indicate whether the remote
8728 target supports this object. */
8730 static enum target_xfer_status
8731 remote_read_qxfer (struct target_ops *ops, const char *object_name,
8733 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8734 ULONGEST *xfered_len,
8735 struct packet_config *packet)
8737 struct remote_state *rs = get_remote_state ();
8738 LONGEST i, n, packet_len;
8740 if (packet->support == PACKET_DISABLE)
8741 return TARGET_XFER_E_IO;
8743 /* Check whether we've cached an end-of-object packet that matches
8745 if (rs->finished_object)
8747 if (strcmp (object_name, rs->finished_object) == 0
8748 && strcmp (annex ? annex : "", rs->finished_annex) == 0
8749 && offset == rs->finished_offset)
8750 return TARGET_XFER_EOF;
8753 /* Otherwise, we're now reading something different. Discard
8755 xfree (rs->finished_object);
8756 xfree (rs->finished_annex);
8757 rs->finished_object = NULL;
8758 rs->finished_annex = NULL;
8761 /* Request only enough to fit in a single packet. The actual data
8762 may not, since we don't know how much of it will need to be escaped;
8763 the target is free to respond with slightly less data. We subtract
8764 five to account for the response type and the protocol frame. */
8765 n = min (get_remote_packet_size () - 5, len);
8766 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8767 object_name, annex ? annex : "",
8768 phex_nz (offset, sizeof offset),
8769 phex_nz (n, sizeof n));
8770 i = putpkt (rs->buf);
8772 return TARGET_XFER_E_IO;
8775 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8776 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8777 return TARGET_XFER_E_IO;
8779 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8780 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8782 /* 'm' means there is (or at least might be) more data after this
8783 batch. That does not make sense unless there's at least one byte
8784 of data in this reply. */
8785 if (rs->buf[0] == 'm' && packet_len == 1)
8786 error (_("Remote qXfer reply contained no data."));
8788 /* Got some data. */
8789 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
8790 packet_len - 1, readbuf, n);
8792 /* 'l' is an EOF marker, possibly including a final block of data,
8793 or possibly empty. If we have the final block of a non-empty
8794 object, record this fact to bypass a subsequent partial read. */
8795 if (rs->buf[0] == 'l' && offset + i > 0)
8797 rs->finished_object = xstrdup (object_name);
8798 rs->finished_annex = xstrdup (annex ? annex : "");
8799 rs->finished_offset = offset + i;
8803 return TARGET_XFER_EOF;
8807 return TARGET_XFER_OK;
8811 static enum target_xfer_status
8812 remote_xfer_partial (struct target_ops *ops, enum target_object object,
8813 const char *annex, gdb_byte *readbuf,
8814 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
8815 ULONGEST *xfered_len)
8817 struct remote_state *rs;
8822 set_remote_traceframe ();
8823 set_general_thread (inferior_ptid);
8825 rs = get_remote_state ();
8827 /* Handle memory using the standard memory routines. */
8828 if (object == TARGET_OBJECT_MEMORY)
8830 /* If the remote target is connected but not running, we should
8831 pass this request down to a lower stratum (e.g. the executable
8833 if (!target_has_execution)
8834 return TARGET_XFER_EOF;
8836 if (writebuf != NULL)
8837 return remote_write_bytes (offset, writebuf, len, xfered_len);
8839 return remote_read_bytes (ops, offset, readbuf, len, xfered_len);
8842 /* Handle SPU memory using qxfer packets. */
8843 if (object == TARGET_OBJECT_SPU)
8846 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8847 xfered_len, &remote_protocol_packets
8848 [PACKET_qXfer_spu_read]);
8850 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8851 xfered_len, &remote_protocol_packets
8852 [PACKET_qXfer_spu_write]);
8855 /* Handle extra signal info using qxfer packets. */
8856 if (object == TARGET_OBJECT_SIGNAL_INFO)
8859 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8860 xfered_len, &remote_protocol_packets
8861 [PACKET_qXfer_siginfo_read]);
8863 return remote_write_qxfer (ops, "siginfo", annex,
8864 writebuf, offset, len, xfered_len,
8865 &remote_protocol_packets
8866 [PACKET_qXfer_siginfo_write]);
8869 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8872 return remote_read_qxfer (ops, "statictrace", annex,
8873 readbuf, offset, len, xfered_len,
8874 &remote_protocol_packets
8875 [PACKET_qXfer_statictrace_read]);
8877 return TARGET_XFER_E_IO;
8880 /* Only handle flash writes. */
8881 if (writebuf != NULL)
8887 case TARGET_OBJECT_FLASH:
8888 return remote_flash_write (ops, offset, len, xfered_len,
8892 return TARGET_XFER_E_IO;
8896 /* Map pre-existing objects onto letters. DO NOT do this for new
8897 objects!!! Instead specify new query packets. */
8900 case TARGET_OBJECT_AVR:
8904 case TARGET_OBJECT_AUXV:
8905 gdb_assert (annex == NULL);
8906 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8908 &remote_protocol_packets[PACKET_qXfer_auxv]);
8910 case TARGET_OBJECT_AVAILABLE_FEATURES:
8911 return remote_read_qxfer
8912 (ops, "features", annex, readbuf, offset, len, xfered_len,
8913 &remote_protocol_packets[PACKET_qXfer_features]);
8915 case TARGET_OBJECT_LIBRARIES:
8916 return remote_read_qxfer
8917 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
8918 &remote_protocol_packets[PACKET_qXfer_libraries]);
8920 case TARGET_OBJECT_LIBRARIES_SVR4:
8921 return remote_read_qxfer
8922 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
8923 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8925 case TARGET_OBJECT_MEMORY_MAP:
8926 gdb_assert (annex == NULL);
8927 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8929 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8931 case TARGET_OBJECT_OSDATA:
8932 /* Should only get here if we're connected. */
8933 gdb_assert (rs->remote_desc);
8934 return remote_read_qxfer
8935 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
8936 &remote_protocol_packets[PACKET_qXfer_osdata]);
8938 case TARGET_OBJECT_THREADS:
8939 gdb_assert (annex == NULL);
8940 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8942 &remote_protocol_packets[PACKET_qXfer_threads]);
8944 case TARGET_OBJECT_TRACEFRAME_INFO:
8945 gdb_assert (annex == NULL);
8946 return remote_read_qxfer
8947 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
8948 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8950 case TARGET_OBJECT_FDPIC:
8951 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8953 &remote_protocol_packets[PACKET_qXfer_fdpic]);
8955 case TARGET_OBJECT_OPENVMS_UIB:
8956 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8958 &remote_protocol_packets[PACKET_qXfer_uib]);
8960 case TARGET_OBJECT_BTRACE:
8961 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
8963 &remote_protocol_packets[PACKET_qXfer_btrace]);
8965 case TARGET_OBJECT_BTRACE_CONF:
8966 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
8968 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
8971 return TARGET_XFER_E_IO;
8974 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8975 large enough let the caller deal with it. */
8976 if (len < get_remote_packet_size ())
8977 return TARGET_XFER_E_IO;
8978 len = get_remote_packet_size ();
8980 /* Except for querying the minimum buffer size, target must be open. */
8981 if (!rs->remote_desc)
8982 error (_("remote query is only available after target open"));
8984 gdb_assert (annex != NULL);
8985 gdb_assert (readbuf != NULL);
8991 /* We used one buffer char for the remote protocol q command and
8992 another for the query type. As the remote protocol encapsulation
8993 uses 4 chars plus one extra in case we are debugging
8994 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8997 while (annex[i] && (i < (get_remote_packet_size () - 8)))
8999 /* Bad caller may have sent forbidden characters. */
9000 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9005 gdb_assert (annex[i] == '\0');
9007 i = putpkt (rs->buf);
9009 return TARGET_XFER_E_IO;
9011 getpkt (&rs->buf, &rs->buf_size, 0);
9012 strcpy ((char *) readbuf, rs->buf);
9014 *xfered_len = strlen ((char *) readbuf);
9015 return TARGET_XFER_OK;
9019 remote_search_memory (struct target_ops* ops,
9020 CORE_ADDR start_addr, ULONGEST search_space_len,
9021 const gdb_byte *pattern, ULONGEST pattern_len,
9022 CORE_ADDR *found_addrp)
9024 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
9025 struct remote_state *rs = get_remote_state ();
9026 int max_size = get_memory_write_packet_size ();
9027 struct packet_config *packet =
9028 &remote_protocol_packets[PACKET_qSearch_memory];
9029 /* Number of packet bytes used to encode the pattern;
9030 this could be more than PATTERN_LEN due to escape characters. */
9031 int escaped_pattern_len;
9032 /* Amount of pattern that was encodable in the packet. */
9033 int used_pattern_len;
9036 ULONGEST found_addr;
9038 /* Don't go to the target if we don't have to.
9039 This is done before checking packet->support to avoid the possibility that
9040 a success for this edge case means the facility works in general. */
9041 if (pattern_len > search_space_len)
9043 if (pattern_len == 0)
9045 *found_addrp = start_addr;
9049 /* If we already know the packet isn't supported, fall back to the simple
9050 way of searching memory. */
9052 if (packet_config_support (packet) == PACKET_DISABLE)
9054 /* Target doesn't provided special support, fall back and use the
9055 standard support (copy memory and do the search here). */
9056 return simple_search_memory (ops, start_addr, search_space_len,
9057 pattern, pattern_len, found_addrp);
9060 /* Make sure the remote is pointing at the right process. */
9061 set_general_process ();
9063 /* Insert header. */
9064 i = snprintf (rs->buf, max_size,
9065 "qSearch:memory:%s;%s;",
9066 phex_nz (start_addr, addr_size),
9067 phex_nz (search_space_len, sizeof (search_space_len)));
9068 max_size -= (i + 1);
9070 /* Escape as much data as fits into rs->buf. */
9071 escaped_pattern_len =
9072 remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i,
9073 &used_pattern_len, max_size);
9075 /* Bail if the pattern is too large. */
9076 if (used_pattern_len != pattern_len)
9077 error (_("Pattern is too large to transmit to remote target."));
9079 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9080 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9081 || packet_ok (rs->buf, packet) != PACKET_OK)
9083 /* The request may not have worked because the command is not
9084 supported. If so, fall back to the simple way. */
9085 if (packet->support == PACKET_DISABLE)
9087 return simple_search_memory (ops, start_addr, search_space_len,
9088 pattern, pattern_len, found_addrp);
9093 if (rs->buf[0] == '0')
9095 else if (rs->buf[0] == '1')
9098 if (rs->buf[1] != ',')
9099 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9100 unpack_varlen_hex (rs->buf + 2, &found_addr);
9101 *found_addrp = found_addr;
9104 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9110 remote_rcmd (struct target_ops *self, const char *command,
9111 struct ui_file *outbuf)
9113 struct remote_state *rs = get_remote_state ();
9116 if (!rs->remote_desc)
9117 error (_("remote rcmd is only available after target open"));
9119 /* Send a NULL command across as an empty command. */
9120 if (command == NULL)
9123 /* The query prefix. */
9124 strcpy (rs->buf, "qRcmd,");
9125 p = strchr (rs->buf, '\0');
9127 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9128 > get_remote_packet_size ())
9129 error (_("\"monitor\" command ``%s'' is too long."), command);
9131 /* Encode the actual command. */
9132 bin2hex ((const gdb_byte *) command, p, strlen (command));
9134 if (putpkt (rs->buf) < 0)
9135 error (_("Communication problem with target."));
9137 /* get/display the response */
9142 /* XXX - see also remote_get_noisy_reply(). */
9143 QUIT; /* Allow user to bail out with ^C. */
9145 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9147 /* Timeout. Continue to (try to) read responses.
9148 This is better than stopping with an error, assuming the stub
9149 is still executing the (long) monitor command.
9150 If needed, the user can interrupt gdb using C-c, obtaining
9151 an effect similar to stop on timeout. */
9156 error (_("Target does not support this command."));
9157 if (buf[0] == 'O' && buf[1] != 'K')
9159 remote_console_output (buf + 1); /* 'O' message from stub. */
9162 if (strcmp (buf, "OK") == 0)
9164 if (strlen (buf) == 3 && buf[0] == 'E'
9165 && isdigit (buf[1]) && isdigit (buf[2]))
9167 error (_("Protocol error with Rcmd"));
9169 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9171 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
9173 fputc_unfiltered (c, outbuf);
9179 static VEC(mem_region_s) *
9180 remote_memory_map (struct target_ops *ops)
9182 VEC(mem_region_s) *result = NULL;
9183 char *text = target_read_stralloc (¤t_target,
9184 TARGET_OBJECT_MEMORY_MAP, NULL);
9188 struct cleanup *back_to = make_cleanup (xfree, text);
9190 result = parse_memory_map (text);
9191 do_cleanups (back_to);
9198 packet_command (char *args, int from_tty)
9200 struct remote_state *rs = get_remote_state ();
9202 if (!rs->remote_desc)
9203 error (_("command can only be used with remote target"));
9206 error (_("remote-packet command requires packet text as argument"));
9208 puts_filtered ("sending: ");
9209 print_packet (args);
9210 puts_filtered ("\n");
9213 getpkt (&rs->buf, &rs->buf_size, 0);
9214 puts_filtered ("received: ");
9215 print_packet (rs->buf);
9216 puts_filtered ("\n");
9220 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9222 static void display_thread_info (struct gdb_ext_thread_info *info);
9224 static void threadset_test_cmd (char *cmd, int tty);
9226 static void threadalive_test (char *cmd, int tty);
9228 static void threadlist_test_cmd (char *cmd, int tty);
9230 int get_and_display_threadinfo (threadref *ref);
9232 static void threadinfo_test_cmd (char *cmd, int tty);
9234 static int thread_display_step (threadref *ref, void *context);
9236 static void threadlist_update_test_cmd (char *cmd, int tty);
9238 static void init_remote_threadtests (void);
9240 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9243 threadset_test_cmd (char *cmd, int tty)
9245 int sample_thread = SAMPLE_THREAD;
9247 printf_filtered (_("Remote threadset test\n"));
9248 set_general_thread (sample_thread);
9253 threadalive_test (char *cmd, int tty)
9255 int sample_thread = SAMPLE_THREAD;
9256 int pid = ptid_get_pid (inferior_ptid);
9257 ptid_t ptid = ptid_build (pid, sample_thread, 0);
9259 if (remote_thread_alive (ptid))
9260 printf_filtered ("PASS: Thread alive test\n");
9262 printf_filtered ("FAIL: Thread alive test\n");
9265 void output_threadid (char *title, threadref *ref);
9268 output_threadid (char *title, threadref *ref)
9272 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
9274 printf_filtered ("%s %s\n", title, (&hexid[0]));
9278 threadlist_test_cmd (char *cmd, int tty)
9281 threadref nextthread;
9282 int done, result_count;
9283 threadref threadlist[3];
9285 printf_filtered ("Remote Threadlist test\n");
9286 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9287 &result_count, &threadlist[0]))
9288 printf_filtered ("FAIL: threadlist test\n");
9291 threadref *scan = threadlist;
9292 threadref *limit = scan + result_count;
9294 while (scan < limit)
9295 output_threadid (" thread ", scan++);
9300 display_thread_info (struct gdb_ext_thread_info *info)
9302 output_threadid ("Threadid: ", &info->threadid);
9303 printf_filtered ("Name: %s\n ", info->shortname);
9304 printf_filtered ("State: %s\n", info->display);
9305 printf_filtered ("other: %s\n\n", info->more_display);
9309 get_and_display_threadinfo (threadref *ref)
9313 struct gdb_ext_thread_info threadinfo;
9315 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9316 | TAG_MOREDISPLAY | TAG_DISPLAY;
9317 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9318 display_thread_info (&threadinfo);
9323 threadinfo_test_cmd (char *cmd, int tty)
9325 int athread = SAMPLE_THREAD;
9329 int_to_threadref (&thread, athread);
9330 printf_filtered ("Remote Threadinfo test\n");
9331 if (!get_and_display_threadinfo (&thread))
9332 printf_filtered ("FAIL cannot get thread info\n");
9336 thread_display_step (threadref *ref, void *context)
9338 /* output_threadid(" threadstep ",ref); *//* simple test */
9339 return get_and_display_threadinfo (ref);
9343 threadlist_update_test_cmd (char *cmd, int tty)
9345 printf_filtered ("Remote Threadlist update test\n");
9346 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9350 init_remote_threadtests (void)
9352 add_com ("tlist", class_obscure, threadlist_test_cmd,
9353 _("Fetch and print the remote list of "
9354 "thread identifiers, one pkt only"));
9355 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
9356 _("Fetch and display info about one thread"));
9357 add_com ("tset", class_obscure, threadset_test_cmd,
9358 _("Test setting to a different thread"));
9359 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
9360 _("Iterate through updating all remote thread info"));
9361 add_com ("talive", class_obscure, threadalive_test,
9362 _(" Remote thread alive test "));
9367 /* Convert a thread ID to a string. Returns the string in a static
9371 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
9373 static char buf[64];
9374 struct remote_state *rs = get_remote_state ();
9376 if (ptid_equal (ptid, null_ptid))
9377 return normal_pid_to_str (ptid);
9378 else if (ptid_is_pid (ptid))
9380 /* Printing an inferior target id. */
9382 /* When multi-process extensions are off, there's no way in the
9383 remote protocol to know the remote process id, if there's any
9384 at all. There's one exception --- when we're connected with
9385 target extended-remote, and we manually attached to a process
9386 with "attach PID". We don't record anywhere a flag that
9387 allows us to distinguish that case from the case of
9388 connecting with extended-remote and the stub already being
9389 attached to a process, and reporting yes to qAttached, hence
9390 no smart special casing here. */
9391 if (!remote_multi_process_p (rs))
9393 xsnprintf (buf, sizeof buf, "Remote target");
9397 return normal_pid_to_str (ptid);
9401 if (ptid_equal (magic_null_ptid, ptid))
9402 xsnprintf (buf, sizeof buf, "Thread <main>");
9403 else if (rs->extended && remote_multi_process_p (rs))
9404 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9405 ptid_get_pid (ptid), ptid_get_lwp (ptid));
9407 xsnprintf (buf, sizeof buf, "Thread %ld",
9408 ptid_get_lwp (ptid));
9413 /* Get the address of the thread local variable in OBJFILE which is
9414 stored at OFFSET within the thread local storage for thread PTID. */
9417 remote_get_thread_local_address (struct target_ops *ops,
9418 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
9420 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
9422 struct remote_state *rs = get_remote_state ();
9424 char *endp = rs->buf + get_remote_packet_size ();
9425 enum packet_result result;
9427 strcpy (p, "qGetTLSAddr:");
9429 p = write_ptid (p, endp, ptid);
9431 p += hexnumstr (p, offset);
9433 p += hexnumstr (p, lm);
9437 getpkt (&rs->buf, &rs->buf_size, 0);
9438 result = packet_ok (rs->buf,
9439 &remote_protocol_packets[PACKET_qGetTLSAddr]);
9440 if (result == PACKET_OK)
9444 unpack_varlen_hex (rs->buf, &result);
9447 else if (result == PACKET_UNKNOWN)
9448 throw_error (TLS_GENERIC_ERROR,
9449 _("Remote target doesn't support qGetTLSAddr packet"));
9451 throw_error (TLS_GENERIC_ERROR,
9452 _("Remote target failed to process qGetTLSAddr request"));
9455 throw_error (TLS_GENERIC_ERROR,
9456 _("TLS not supported or disabled on this target"));
9461 /* Provide thread local base, i.e. Thread Information Block address.
9462 Returns 1 if ptid is found and thread_local_base is non zero. */
9465 remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
9467 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
9469 struct remote_state *rs = get_remote_state ();
9471 char *endp = rs->buf + get_remote_packet_size ();
9472 enum packet_result result;
9474 strcpy (p, "qGetTIBAddr:");
9476 p = write_ptid (p, endp, ptid);
9480 getpkt (&rs->buf, &rs->buf_size, 0);
9481 result = packet_ok (rs->buf,
9482 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9483 if (result == PACKET_OK)
9487 unpack_varlen_hex (rs->buf, &result);
9489 *addr = (CORE_ADDR) result;
9492 else if (result == PACKET_UNKNOWN)
9493 error (_("Remote target doesn't support qGetTIBAddr packet"));
9495 error (_("Remote target failed to process qGetTIBAddr request"));
9498 error (_("qGetTIBAddr not supported or disabled on this target"));
9503 /* Support for inferring a target description based on the current
9504 architecture and the size of a 'g' packet. While the 'g' packet
9505 can have any size (since optional registers can be left off the
9506 end), some sizes are easily recognizable given knowledge of the
9507 approximate architecture. */
9509 struct remote_g_packet_guess
9512 const struct target_desc *tdesc;
9514 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9515 DEF_VEC_O(remote_g_packet_guess_s);
9517 struct remote_g_packet_data
9519 VEC(remote_g_packet_guess_s) *guesses;
9522 static struct gdbarch_data *remote_g_packet_data_handle;
9525 remote_g_packet_data_init (struct obstack *obstack)
9527 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9531 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9532 const struct target_desc *tdesc)
9534 struct remote_g_packet_data *data
9535 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9536 struct remote_g_packet_guess new_guess, *guess;
9539 gdb_assert (tdesc != NULL);
9542 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9544 if (guess->bytes == bytes)
9545 internal_error (__FILE__, __LINE__,
9546 _("Duplicate g packet description added for size %d"),
9549 new_guess.bytes = bytes;
9550 new_guess.tdesc = tdesc;
9551 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9554 /* Return 1 if remote_read_description would do anything on this target
9555 and architecture, 0 otherwise. */
9558 remote_read_description_p (struct target_ops *target)
9560 struct remote_g_packet_data *data
9561 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9563 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9569 static const struct target_desc *
9570 remote_read_description (struct target_ops *target)
9572 struct remote_g_packet_data *data
9573 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9575 /* Do not try this during initial connection, when we do not know
9576 whether there is a running but stopped thread. */
9577 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
9578 return target->beneath->to_read_description (target->beneath);
9580 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9582 struct remote_g_packet_guess *guess;
9584 int bytes = send_g_packet ();
9587 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9589 if (guess->bytes == bytes)
9590 return guess->tdesc;
9592 /* We discard the g packet. A minor optimization would be to
9593 hold on to it, and fill the register cache once we have selected
9594 an architecture, but it's too tricky to do safely. */
9597 return target->beneath->to_read_description (target->beneath);
9600 /* Remote file transfer support. This is host-initiated I/O, not
9601 target-initiated; for target-initiated, see remote-fileio.c. */
9603 /* If *LEFT is at least the length of STRING, copy STRING to
9604 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9605 decrease *LEFT. Otherwise raise an error. */
9608 remote_buffer_add_string (char **buffer, int *left, char *string)
9610 int len = strlen (string);
9613 error (_("Packet too long for target."));
9615 memcpy (*buffer, string, len);
9619 /* NUL-terminate the buffer as a convenience, if there is
9625 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9626 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9627 decrease *LEFT. Otherwise raise an error. */
9630 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9633 if (2 * len > *left)
9634 error (_("Packet too long for target."));
9636 bin2hex (bytes, *buffer, len);
9640 /* NUL-terminate the buffer as a convenience, if there is
9646 /* If *LEFT is large enough, convert VALUE to hex and add it to
9647 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9648 decrease *LEFT. Otherwise raise an error. */
9651 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9653 int len = hexnumlen (value);
9656 error (_("Packet too long for target."));
9658 hexnumstr (*buffer, value);
9662 /* NUL-terminate the buffer as a convenience, if there is
9668 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9669 value, *REMOTE_ERRNO to the remote error number or zero if none
9670 was included, and *ATTACHMENT to point to the start of the annex
9671 if any. The length of the packet isn't needed here; there may
9672 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9674 Return 0 if the packet could be parsed, -1 if it could not. If
9675 -1 is returned, the other variables may not be initialized. */
9678 remote_hostio_parse_result (char *buffer, int *retcode,
9679 int *remote_errno, char **attachment)
9686 if (buffer[0] != 'F')
9690 *retcode = strtol (&buffer[1], &p, 16);
9691 if (errno != 0 || p == &buffer[1])
9694 /* Check for ",errno". */
9698 *remote_errno = strtol (p + 1, &p2, 16);
9699 if (errno != 0 || p + 1 == p2)
9704 /* Check for ";attachment". If there is no attachment, the
9705 packet should end here. */
9708 *attachment = p + 1;
9711 else if (*p == '\0')
9717 /* Send a prepared I/O packet to the target and read its response.
9718 The prepared packet is in the global RS->BUF before this function
9719 is called, and the answer is there when we return.
9721 COMMAND_BYTES is the length of the request to send, which may include
9722 binary data. WHICH_PACKET is the packet configuration to check
9723 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9724 is set to the error number and -1 is returned. Otherwise the value
9725 returned by the function is returned.
9727 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9728 attachment is expected; an error will be reported if there's a
9729 mismatch. If one is found, *ATTACHMENT will be set to point into
9730 the packet buffer and *ATTACHMENT_LEN will be set to the
9731 attachment's length. */
9734 remote_hostio_send_command (int command_bytes, int which_packet,
9735 int *remote_errno, char **attachment,
9736 int *attachment_len)
9738 struct remote_state *rs = get_remote_state ();
9739 int ret, bytes_read;
9740 char *attachment_tmp;
9742 if (!rs->remote_desc
9743 || packet_support (which_packet) == PACKET_DISABLE)
9745 *remote_errno = FILEIO_ENOSYS;
9749 putpkt_binary (rs->buf, command_bytes);
9750 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9752 /* If it timed out, something is wrong. Don't try to parse the
9756 *remote_errno = FILEIO_EINVAL;
9760 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9763 *remote_errno = FILEIO_EINVAL;
9765 case PACKET_UNKNOWN:
9766 *remote_errno = FILEIO_ENOSYS;
9772 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9775 *remote_errno = FILEIO_EINVAL;
9779 /* Make sure we saw an attachment if and only if we expected one. */
9780 if ((attachment_tmp == NULL && attachment != NULL)
9781 || (attachment_tmp != NULL && attachment == NULL))
9783 *remote_errno = FILEIO_EINVAL;
9787 /* If an attachment was found, it must point into the packet buffer;
9788 work out how many bytes there were. */
9789 if (attachment_tmp != NULL)
9791 *attachment = attachment_tmp;
9792 *attachment_len = bytes_read - (*attachment - rs->buf);
9798 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9799 remote file descriptor, or -1 if an error occurs (and set
9803 remote_hostio_open (struct target_ops *self,
9804 const char *filename, int flags, int mode,
9807 struct remote_state *rs = get_remote_state ();
9809 int left = get_remote_packet_size () - 1;
9811 remote_buffer_add_string (&p, &left, "vFile:open:");
9813 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9815 remote_buffer_add_string (&p, &left, ",");
9817 remote_buffer_add_int (&p, &left, flags);
9818 remote_buffer_add_string (&p, &left, ",");
9820 remote_buffer_add_int (&p, &left, mode);
9822 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9823 remote_errno, NULL, NULL);
9826 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9827 Return the number of bytes written, or -1 if an error occurs (and
9828 set *REMOTE_ERRNO). */
9831 remote_hostio_pwrite (struct target_ops *self,
9832 int fd, const gdb_byte *write_buf, int len,
9833 ULONGEST offset, int *remote_errno)
9835 struct remote_state *rs = get_remote_state ();
9837 int left = get_remote_packet_size ();
9840 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9842 remote_buffer_add_int (&p, &left, fd);
9843 remote_buffer_add_string (&p, &left, ",");
9845 remote_buffer_add_int (&p, &left, offset);
9846 remote_buffer_add_string (&p, &left, ",");
9848 p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len,
9849 get_remote_packet_size () - (p - rs->buf));
9851 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9852 remote_errno, NULL, NULL);
9855 /* Read up to LEN bytes FD on the remote target into READ_BUF
9856 Return the number of bytes read, or -1 if an error occurs (and
9857 set *REMOTE_ERRNO). */
9860 remote_hostio_pread (struct target_ops *self,
9861 int fd, gdb_byte *read_buf, int len,
9862 ULONGEST offset, int *remote_errno)
9864 struct remote_state *rs = get_remote_state ();
9867 int left = get_remote_packet_size ();
9868 int ret, attachment_len;
9871 remote_buffer_add_string (&p, &left, "vFile:pread:");
9873 remote_buffer_add_int (&p, &left, fd);
9874 remote_buffer_add_string (&p, &left, ",");
9876 remote_buffer_add_int (&p, &left, len);
9877 remote_buffer_add_string (&p, &left, ",");
9879 remote_buffer_add_int (&p, &left, offset);
9881 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9882 remote_errno, &attachment,
9888 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9890 if (read_len != ret)
9891 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9896 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9897 (and set *REMOTE_ERRNO). */
9900 remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
9902 struct remote_state *rs = get_remote_state ();
9904 int left = get_remote_packet_size () - 1;
9906 remote_buffer_add_string (&p, &left, "vFile:close:");
9908 remote_buffer_add_int (&p, &left, fd);
9910 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9911 remote_errno, NULL, NULL);
9914 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9915 occurs (and set *REMOTE_ERRNO). */
9918 remote_hostio_unlink (struct target_ops *self,
9919 const char *filename, int *remote_errno)
9921 struct remote_state *rs = get_remote_state ();
9923 int left = get_remote_packet_size () - 1;
9925 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9927 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9930 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9931 remote_errno, NULL, NULL);
9934 /* Read value of symbolic link FILENAME on the remote target. Return
9935 a null-terminated string allocated via xmalloc, or NULL if an error
9936 occurs (and set *REMOTE_ERRNO). */
9939 remote_hostio_readlink (struct target_ops *self,
9940 const char *filename, int *remote_errno)
9942 struct remote_state *rs = get_remote_state ();
9945 int left = get_remote_packet_size ();
9946 int len, attachment_len;
9950 remote_buffer_add_string (&p, &left, "vFile:readlink:");
9952 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9955 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9956 remote_errno, &attachment,
9962 ret = xmalloc (len + 1);
9964 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9965 (gdb_byte *) ret, len);
9966 if (read_len != len)
9967 error (_("Readlink returned %d, but %d bytes."), len, read_len);
9974 remote_fileio_errno_to_host (int errnum)
9998 case FILEIO_ENOTDIR:
10000 case FILEIO_EISDIR:
10002 case FILEIO_EINVAL:
10004 case FILEIO_ENFILE:
10006 case FILEIO_EMFILE:
10010 case FILEIO_ENOSPC:
10012 case FILEIO_ESPIPE:
10016 case FILEIO_ENOSYS:
10018 case FILEIO_ENAMETOOLONG:
10019 return ENAMETOOLONG;
10025 remote_hostio_error (int errnum)
10027 int host_error = remote_fileio_errno_to_host (errnum);
10029 if (host_error == -1)
10030 error (_("Unknown remote I/O error %d"), errnum);
10032 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10036 remote_hostio_close_cleanup (void *opaque)
10038 int fd = *(int *) opaque;
10041 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
10046 remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
10048 const char *filename = bfd_get_filename (abfd);
10049 int fd, remote_errno;
10052 gdb_assert (remote_filename_p (filename));
10054 fd = remote_hostio_open (find_target_at (process_stratum),
10055 filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
10058 errno = remote_fileio_errno_to_host (remote_errno);
10059 bfd_set_error (bfd_error_system_call);
10063 stream = xmalloc (sizeof (int));
10069 remote_bfd_iovec_close (struct bfd *abfd, void *stream)
10071 int fd = *(int *)stream;
10076 /* Ignore errors on close; these may happen if the remote
10077 connection was already torn down. */
10078 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
10080 /* Zero means success. */
10085 remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
10086 file_ptr nbytes, file_ptr offset)
10088 int fd = *(int *)stream;
10090 file_ptr pos, bytes;
10093 while (nbytes > pos)
10095 bytes = remote_hostio_pread (find_target_at (process_stratum),
10096 fd, (gdb_byte *) buf + pos, nbytes - pos,
10097 offset + pos, &remote_errno);
10099 /* Success, but no bytes, means end-of-file. */
10103 errno = remote_fileio_errno_to_host (remote_errno);
10104 bfd_set_error (bfd_error_system_call);
10115 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
10117 /* FIXME: We should probably implement remote_hostio_stat. */
10118 sb->st_size = INT_MAX;
10123 remote_filename_p (const char *filename)
10125 return strncmp (filename,
10126 REMOTE_SYSROOT_PREFIX,
10127 sizeof (REMOTE_SYSROOT_PREFIX) - 1) == 0;
10131 remote_bfd_open (const char *remote_file, const char *target)
10133 bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
10134 remote_bfd_iovec_open, NULL,
10135 remote_bfd_iovec_pread,
10136 remote_bfd_iovec_close,
10137 remote_bfd_iovec_stat);
10143 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10145 struct cleanup *back_to, *close_cleanup;
10146 int retcode, fd, remote_errno, bytes, io_size;
10149 int bytes_in_buffer;
10152 struct remote_state *rs = get_remote_state ();
10154 if (!rs->remote_desc)
10155 error (_("command can only be used with remote target"));
10157 file = gdb_fopen_cloexec (local_file, "rb");
10159 perror_with_name (local_file);
10160 back_to = make_cleanup_fclose (file);
10162 fd = remote_hostio_open (find_target_at (process_stratum),
10163 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
10165 0700, &remote_errno);
10167 remote_hostio_error (remote_errno);
10169 /* Send up to this many bytes at once. They won't all fit in the
10170 remote packet limit, so we'll transfer slightly fewer. */
10171 io_size = get_remote_packet_size ();
10172 buffer = xmalloc (io_size);
10173 make_cleanup (xfree, buffer);
10175 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10177 bytes_in_buffer = 0;
10180 while (bytes_in_buffer || !saw_eof)
10184 bytes = fread (buffer + bytes_in_buffer, 1,
10185 io_size - bytes_in_buffer,
10190 error (_("Error reading %s."), local_file);
10193 /* EOF. Unless there is something still in the
10194 buffer from the last iteration, we are done. */
10196 if (bytes_in_buffer == 0)
10204 bytes += bytes_in_buffer;
10205 bytes_in_buffer = 0;
10207 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10209 offset, &remote_errno);
10212 remote_hostio_error (remote_errno);
10213 else if (retcode == 0)
10214 error (_("Remote write of %d bytes returned 0!"), bytes);
10215 else if (retcode < bytes)
10217 /* Short write. Save the rest of the read data for the next
10219 bytes_in_buffer = bytes - retcode;
10220 memmove (buffer, buffer + retcode, bytes_in_buffer);
10226 discard_cleanups (close_cleanup);
10227 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10228 remote_hostio_error (remote_errno);
10231 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10232 do_cleanups (back_to);
10236 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10238 struct cleanup *back_to, *close_cleanup;
10239 int fd, remote_errno, bytes, io_size;
10243 struct remote_state *rs = get_remote_state ();
10245 if (!rs->remote_desc)
10246 error (_("command can only be used with remote target"));
10248 fd = remote_hostio_open (find_target_at (process_stratum),
10249 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
10251 remote_hostio_error (remote_errno);
10253 file = gdb_fopen_cloexec (local_file, "wb");
10255 perror_with_name (local_file);
10256 back_to = make_cleanup_fclose (file);
10258 /* Send up to this many bytes at once. They won't all fit in the
10259 remote packet limit, so we'll transfer slightly fewer. */
10260 io_size = get_remote_packet_size ();
10261 buffer = xmalloc (io_size);
10262 make_cleanup (xfree, buffer);
10264 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10269 bytes = remote_hostio_pread (find_target_at (process_stratum),
10270 fd, buffer, io_size, offset, &remote_errno);
10272 /* Success, but no bytes, means end-of-file. */
10275 remote_hostio_error (remote_errno);
10279 bytes = fwrite (buffer, 1, bytes, file);
10281 perror_with_name (local_file);
10284 discard_cleanups (close_cleanup);
10285 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10286 remote_hostio_error (remote_errno);
10289 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10290 do_cleanups (back_to);
10294 remote_file_delete (const char *remote_file, int from_tty)
10296 int retcode, remote_errno;
10297 struct remote_state *rs = get_remote_state ();
10299 if (!rs->remote_desc)
10300 error (_("command can only be used with remote target"));
10302 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10303 remote_file, &remote_errno);
10305 remote_hostio_error (remote_errno);
10308 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10312 remote_put_command (char *args, int from_tty)
10314 struct cleanup *back_to;
10318 error_no_arg (_("file to put"));
10320 argv = gdb_buildargv (args);
10321 back_to = make_cleanup_freeargv (argv);
10322 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10323 error (_("Invalid parameters to remote put"));
10325 remote_file_put (argv[0], argv[1], from_tty);
10327 do_cleanups (back_to);
10331 remote_get_command (char *args, int from_tty)
10333 struct cleanup *back_to;
10337 error_no_arg (_("file to get"));
10339 argv = gdb_buildargv (args);
10340 back_to = make_cleanup_freeargv (argv);
10341 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10342 error (_("Invalid parameters to remote get"));
10344 remote_file_get (argv[0], argv[1], from_tty);
10346 do_cleanups (back_to);
10350 remote_delete_command (char *args, int from_tty)
10352 struct cleanup *back_to;
10356 error_no_arg (_("file to delete"));
10358 argv = gdb_buildargv (args);
10359 back_to = make_cleanup_freeargv (argv);
10360 if (argv[0] == NULL || argv[1] != NULL)
10361 error (_("Invalid parameters to remote delete"));
10363 remote_file_delete (argv[0], from_tty);
10365 do_cleanups (back_to);
10369 remote_command (char *args, int from_tty)
10371 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
10375 remote_can_execute_reverse (struct target_ops *self)
10377 if (packet_support (PACKET_bs) == PACKET_ENABLE
10378 || packet_support (PACKET_bc) == PACKET_ENABLE)
10385 remote_supports_non_stop (struct target_ops *self)
10391 remote_supports_disable_randomization (struct target_ops *self)
10393 /* Only supported in extended mode. */
10398 remote_supports_multi_process (struct target_ops *self)
10400 struct remote_state *rs = get_remote_state ();
10402 /* Only extended-remote handles being attached to multiple
10403 processes, even though plain remote can use the multi-process
10404 thread id extensions, so that GDB knows the target process's
10406 return rs->extended && remote_multi_process_p (rs);
10410 remote_supports_cond_tracepoints (void)
10412 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
10416 remote_supports_cond_breakpoints (struct target_ops *self)
10418 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
10422 remote_supports_fast_tracepoints (void)
10424 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
10428 remote_supports_static_tracepoints (void)
10430 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
10434 remote_supports_install_in_trace (void)
10436 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
10440 remote_supports_enable_disable_tracepoint (struct target_ops *self)
10442 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10447 remote_supports_string_tracing (struct target_ops *self)
10449 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
10453 remote_can_run_breakpoint_commands (struct target_ops *self)
10455 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
10459 remote_trace_init (struct target_ops *self)
10462 remote_get_noisy_reply (&target_buf, &target_buf_size);
10463 if (strcmp (target_buf, "OK") != 0)
10464 error (_("Target does not support this command."));
10467 static void free_actions_list (char **actions_list);
10468 static void free_actions_list_cleanup_wrapper (void *);
10470 free_actions_list_cleanup_wrapper (void *al)
10472 free_actions_list (al);
10476 free_actions_list (char **actions_list)
10480 if (actions_list == 0)
10483 for (ndx = 0; actions_list[ndx]; ndx++)
10484 xfree (actions_list[ndx]);
10486 xfree (actions_list);
10489 /* Recursive routine to walk through command list including loops, and
10490 download packets for each command. */
10493 remote_download_command_source (int num, ULONGEST addr,
10494 struct command_line *cmds)
10496 struct remote_state *rs = get_remote_state ();
10497 struct command_line *cmd;
10499 for (cmd = cmds; cmd; cmd = cmd->next)
10501 QUIT; /* Allow user to bail out with ^C. */
10502 strcpy (rs->buf, "QTDPsrc:");
10503 encode_source_string (num, addr, "cmd", cmd->line,
10504 rs->buf + strlen (rs->buf),
10505 rs->buf_size - strlen (rs->buf));
10507 remote_get_noisy_reply (&target_buf, &target_buf_size);
10508 if (strcmp (target_buf, "OK"))
10509 warning (_("Target does not support source download."));
10511 if (cmd->control_type == while_control
10512 || cmd->control_type == while_stepping_control)
10514 remote_download_command_source (num, addr, *cmd->body_list);
10516 QUIT; /* Allow user to bail out with ^C. */
10517 strcpy (rs->buf, "QTDPsrc:");
10518 encode_source_string (num, addr, "cmd", "end",
10519 rs->buf + strlen (rs->buf),
10520 rs->buf_size - strlen (rs->buf));
10522 remote_get_noisy_reply (&target_buf, &target_buf_size);
10523 if (strcmp (target_buf, "OK"))
10524 warning (_("Target does not support source download."));
10530 remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
10532 #define BUF_SIZE 2048
10536 char buf[BUF_SIZE];
10537 char **tdp_actions;
10538 char **stepping_actions;
10540 struct cleanup *old_chain = NULL;
10541 struct agent_expr *aexpr;
10542 struct cleanup *aexpr_chain = NULL;
10544 struct breakpoint *b = loc->owner;
10545 struct tracepoint *t = (struct tracepoint *) b;
10547 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
10548 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10550 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10553 tpaddr = loc->address;
10554 sprintf_vma (addrbuf, tpaddr);
10555 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10556 addrbuf, /* address */
10557 (b->enable_state == bp_enabled ? 'E' : 'D'),
10558 t->step_count, t->pass_count);
10559 /* Fast tracepoints are mostly handled by the target, but we can
10560 tell the target how big of an instruction block should be moved
10562 if (b->type == bp_fast_tracepoint)
10564 /* Only test for support at download time; we may not know
10565 target capabilities at definition time. */
10566 if (remote_supports_fast_tracepoints ())
10570 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10571 tpaddr, &isize, NULL))
10572 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10575 /* If it passed validation at definition but fails now,
10576 something is very wrong. */
10577 internal_error (__FILE__, __LINE__,
10578 _("Fast tracepoint not "
10579 "valid during download"));
10582 /* Fast tracepoints are functionally identical to regular
10583 tracepoints, so don't take lack of support as a reason to
10584 give up on the trace run. */
10585 warning (_("Target does not support fast tracepoints, "
10586 "downloading %d as regular tracepoint"), b->number);
10588 else if (b->type == bp_static_tracepoint)
10590 /* Only test for support at download time; we may not know
10591 target capabilities at definition time. */
10592 if (remote_supports_static_tracepoints ())
10594 struct static_tracepoint_marker marker;
10596 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10597 strcat (buf, ":S");
10599 error (_("Static tracepoint not valid during download"));
10602 /* Fast tracepoints are functionally identical to regular
10603 tracepoints, so don't take lack of support as a reason
10604 to give up on the trace run. */
10605 error (_("Target does not support static tracepoints"));
10607 /* If the tracepoint has a conditional, make it into an agent
10608 expression and append to the definition. */
10611 /* Only test support at download time, we may not know target
10612 capabilities at definition time. */
10613 if (remote_supports_cond_tracepoints ())
10615 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10616 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
10617 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10619 pkt = buf + strlen (buf);
10620 for (ndx = 0; ndx < aexpr->len; ++ndx)
10621 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10623 do_cleanups (aexpr_chain);
10626 warning (_("Target does not support conditional tracepoints, "
10627 "ignoring tp %d cond"), b->number);
10630 if (b->commands || *default_collect)
10633 remote_get_noisy_reply (&target_buf, &target_buf_size);
10634 if (strcmp (target_buf, "OK"))
10635 error (_("Target does not support tracepoints."));
10637 /* do_single_steps (t); */
10640 for (ndx = 0; tdp_actions[ndx]; ndx++)
10642 QUIT; /* Allow user to bail out with ^C. */
10643 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10644 b->number, addrbuf, /* address */
10646 ((tdp_actions[ndx + 1] || stepping_actions)
10649 remote_get_noisy_reply (&target_buf,
10651 if (strcmp (target_buf, "OK"))
10652 error (_("Error on target while setting tracepoints."));
10655 if (stepping_actions)
10657 for (ndx = 0; stepping_actions[ndx]; ndx++)
10659 QUIT; /* Allow user to bail out with ^C. */
10660 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10661 b->number, addrbuf, /* address */
10662 ((ndx == 0) ? "S" : ""),
10663 stepping_actions[ndx],
10664 (stepping_actions[ndx + 1] ? "-" : ""));
10666 remote_get_noisy_reply (&target_buf,
10668 if (strcmp (target_buf, "OK"))
10669 error (_("Error on target while setting tracepoints."));
10673 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
10675 if (b->addr_string)
10677 strcpy (buf, "QTDPsrc:");
10678 encode_source_string (b->number, loc->address,
10679 "at", b->addr_string, buf + strlen (buf),
10680 2048 - strlen (buf));
10683 remote_get_noisy_reply (&target_buf, &target_buf_size);
10684 if (strcmp (target_buf, "OK"))
10685 warning (_("Target does not support source download."));
10687 if (b->cond_string)
10689 strcpy (buf, "QTDPsrc:");
10690 encode_source_string (b->number, loc->address,
10691 "cond", b->cond_string, buf + strlen (buf),
10692 2048 - strlen (buf));
10694 remote_get_noisy_reply (&target_buf, &target_buf_size);
10695 if (strcmp (target_buf, "OK"))
10696 warning (_("Target does not support source download."));
10698 remote_download_command_source (b->number, loc->address,
10699 breakpoint_commands (b));
10702 do_cleanups (old_chain);
10706 remote_can_download_tracepoint (struct target_ops *self)
10708 struct remote_state *rs = get_remote_state ();
10709 struct trace_status *ts;
10712 /* Don't try to install tracepoints until we've relocated our
10713 symbols, and fetched and merged the target's tracepoint list with
10715 if (rs->starting_up)
10718 ts = current_trace_status ();
10719 status = remote_get_trace_status (self, ts);
10721 if (status == -1 || !ts->running_known || !ts->running)
10724 /* If we are in a tracing experiment, but remote stub doesn't support
10725 installing tracepoint in trace, we have to return. */
10726 if (!remote_supports_install_in_trace ())
10734 remote_download_trace_state_variable (struct target_ops *self,
10735 struct trace_state_variable *tsv)
10737 struct remote_state *rs = get_remote_state ();
10740 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10741 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10743 p = rs->buf + strlen (rs->buf);
10744 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10745 error (_("Trace state variable name too long for tsv definition packet"));
10746 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
10749 remote_get_noisy_reply (&target_buf, &target_buf_size);
10750 if (*target_buf == '\0')
10751 error (_("Target does not support this command."));
10752 if (strcmp (target_buf, "OK") != 0)
10753 error (_("Error on target while downloading trace state variable."));
10757 remote_enable_tracepoint (struct target_ops *self,
10758 struct bp_location *location)
10760 struct remote_state *rs = get_remote_state ();
10763 sprintf_vma (addr_buf, location->address);
10764 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10765 location->owner->number, addr_buf);
10767 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10768 if (*rs->buf == '\0')
10769 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10770 if (strcmp (rs->buf, "OK") != 0)
10771 error (_("Error on target while enabling tracepoint."));
10775 remote_disable_tracepoint (struct target_ops *self,
10776 struct bp_location *location)
10778 struct remote_state *rs = get_remote_state ();
10781 sprintf_vma (addr_buf, location->address);
10782 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10783 location->owner->number, addr_buf);
10785 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10786 if (*rs->buf == '\0')
10787 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10788 if (strcmp (rs->buf, "OK") != 0)
10789 error (_("Error on target while disabling tracepoint."));
10793 remote_trace_set_readonly_regions (struct target_ops *self)
10797 bfd_size_type size;
10803 return; /* No information to give. */
10805 strcpy (target_buf, "QTro");
10806 offset = strlen (target_buf);
10807 for (s = exec_bfd->sections; s; s = s->next)
10809 char tmp1[40], tmp2[40];
10812 if ((s->flags & SEC_LOAD) == 0 ||
10813 /* (s->flags & SEC_CODE) == 0 || */
10814 (s->flags & SEC_READONLY) == 0)
10818 vma = bfd_get_section_vma (abfd, s);
10819 size = bfd_get_section_size (s);
10820 sprintf_vma (tmp1, vma);
10821 sprintf_vma (tmp2, vma + size);
10822 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10823 if (offset + sec_length + 1 > target_buf_size)
10825 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
10827 Too many sections for read-only sections definition packet."));
10830 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10832 offset += sec_length;
10836 putpkt (target_buf);
10837 getpkt (&target_buf, &target_buf_size, 0);
10842 remote_trace_start (struct target_ops *self)
10844 putpkt ("QTStart");
10845 remote_get_noisy_reply (&target_buf, &target_buf_size);
10846 if (*target_buf == '\0')
10847 error (_("Target does not support this command."));
10848 if (strcmp (target_buf, "OK") != 0)
10849 error (_("Bogus reply from target: %s"), target_buf);
10853 remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
10855 /* Initialize it just to avoid a GCC false warning. */
10857 /* FIXME we need to get register block size some other way. */
10858 extern int trace_regblock_size;
10859 volatile struct gdb_exception ex;
10860 enum packet_result result;
10862 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
10865 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10867 putpkt ("qTStatus");
10869 TRY_CATCH (ex, RETURN_MASK_ERROR)
10871 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10875 if (ex.error != TARGET_CLOSE_ERROR)
10877 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10880 throw_exception (ex);
10883 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
10885 /* If the remote target doesn't do tracing, flag it. */
10886 if (result == PACKET_UNKNOWN)
10889 /* We're working with a live target. */
10890 ts->filename = NULL;
10893 error (_("Bogus trace status reply from target: %s"), target_buf);
10895 /* Function 'parse_trace_status' sets default value of each field of
10896 'ts' at first, so we don't have to do it here. */
10897 parse_trace_status (p, ts);
10899 return ts->running;
10903 remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
10904 struct uploaded_tp *utp)
10906 struct remote_state *rs = get_remote_state ();
10908 struct bp_location *loc;
10909 struct tracepoint *tp = (struct tracepoint *) bp;
10910 size_t size = get_remote_packet_size ();
10914 tp->base.hit_count = 0;
10915 tp->traceframe_usage = 0;
10916 for (loc = tp->base.loc; loc; loc = loc->next)
10918 /* If the tracepoint was never downloaded, don't go asking for
10920 if (tp->number_on_target == 0)
10922 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10923 phex_nz (loc->address, 0));
10925 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10926 if (reply && *reply)
10929 parse_tracepoint_status (reply + 1, bp, utp);
10935 utp->hit_count = 0;
10936 utp->traceframe_usage = 0;
10937 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10938 phex_nz (utp->addr, 0));
10940 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10941 if (reply && *reply)
10944 parse_tracepoint_status (reply + 1, bp, utp);
10950 remote_trace_stop (struct target_ops *self)
10953 remote_get_noisy_reply (&target_buf, &target_buf_size);
10954 if (*target_buf == '\0')
10955 error (_("Target does not support this command."));
10956 if (strcmp (target_buf, "OK") != 0)
10957 error (_("Bogus reply from target: %s"), target_buf);
10961 remote_trace_find (struct target_ops *self,
10962 enum trace_find_type type, int num,
10963 CORE_ADDR addr1, CORE_ADDR addr2,
10966 struct remote_state *rs = get_remote_state ();
10967 char *endbuf = rs->buf + get_remote_packet_size ();
10969 int target_frameno = -1, target_tracept = -1;
10971 /* Lookups other than by absolute frame number depend on the current
10972 trace selected, so make sure it is correct on the remote end
10974 if (type != tfind_number)
10975 set_remote_traceframe ();
10978 strcpy (p, "QTFrame:");
10979 p = strchr (p, '\0');
10983 xsnprintf (p, endbuf - p, "%x", num);
10986 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
10989 xsnprintf (p, endbuf - p, "tdp:%x", num);
10992 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10993 phex_nz (addr2, 0));
10995 case tfind_outside:
10996 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10997 phex_nz (addr2, 0));
11000 error (_("Unknown trace find type %d"), type);
11004 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
11005 if (*reply == '\0')
11006 error (_("Target does not support this command."));
11008 while (reply && *reply)
11013 target_frameno = (int) strtol (p, &reply, 16);
11015 error (_("Unable to parse trace frame number"));
11016 /* Don't update our remote traceframe number cache on failure
11017 to select a remote traceframe. */
11018 if (target_frameno == -1)
11023 target_tracept = (int) strtol (p, &reply, 16);
11025 error (_("Unable to parse tracepoint number"));
11027 case 'O': /* "OK"? */
11028 if (reply[1] == 'K' && reply[2] == '\0')
11031 error (_("Bogus reply from target: %s"), reply);
11034 error (_("Bogus reply from target: %s"), reply);
11037 *tpp = target_tracept;
11039 rs->remote_traceframe_number = target_frameno;
11040 return target_frameno;
11044 remote_get_trace_state_variable_value (struct target_ops *self,
11045 int tsvnum, LONGEST *val)
11047 struct remote_state *rs = get_remote_state ();
11051 set_remote_traceframe ();
11053 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
11055 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11056 if (reply && *reply)
11060 unpack_varlen_hex (reply + 1, &uval);
11061 *val = (LONGEST) uval;
11069 remote_save_trace_data (struct target_ops *self, const char *filename)
11071 struct remote_state *rs = get_remote_state ();
11075 strcpy (p, "QTSave:");
11077 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11078 error (_("Remote file name too long for trace save packet"));
11079 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
11082 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11083 if (*reply == '\0')
11084 error (_("Target does not support this command."));
11085 if (strcmp (reply, "OK") != 0)
11086 error (_("Bogus reply from target: %s"), reply);
11090 /* This is basically a memory transfer, but needs to be its own packet
11091 because we don't know how the target actually organizes its trace
11092 memory, plus we want to be able to ask for as much as possible, but
11093 not be unhappy if we don't get as much as we ask for. */
11096 remote_get_raw_trace_data (struct target_ops *self,
11097 gdb_byte *buf, ULONGEST offset, LONGEST len)
11099 struct remote_state *rs = get_remote_state ();
11105 strcpy (p, "qTBuffer:");
11107 p += hexnumstr (p, offset);
11109 p += hexnumstr (p, len);
11113 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11114 if (reply && *reply)
11116 /* 'l' by itself means we're at the end of the buffer and
11117 there is nothing more to get. */
11121 /* Convert the reply into binary. Limit the number of bytes to
11122 convert according to our passed-in buffer size, rather than
11123 what was returned in the packet; if the target is
11124 unexpectedly generous and gives us a bigger reply than we
11125 asked for, we don't want to crash. */
11126 rslt = hex2bin (target_buf, buf, len);
11130 /* Something went wrong, flag as an error. */
11135 remote_set_disconnected_tracing (struct target_ops *self, int val)
11137 struct remote_state *rs = get_remote_state ();
11139 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
11143 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
11145 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11146 if (*reply == '\0')
11147 error (_("Target does not support this command."));
11148 if (strcmp (reply, "OK") != 0)
11149 error (_("Bogus reply from target: %s"), reply);
11152 warning (_("Target does not support disconnected tracing."));
11156 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11158 struct thread_info *info = find_thread_ptid (ptid);
11160 if (info && info->private)
11161 return info->private->core;
11166 remote_set_circular_trace_buffer (struct target_ops *self, int val)
11168 struct remote_state *rs = get_remote_state ();
11171 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
11173 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11174 if (*reply == '\0')
11175 error (_("Target does not support this command."));
11176 if (strcmp (reply, "OK") != 0)
11177 error (_("Bogus reply from target: %s"), reply);
11180 static struct traceframe_info *
11181 remote_traceframe_info (struct target_ops *self)
11185 text = target_read_stralloc (¤t_target,
11186 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11189 struct traceframe_info *info;
11190 struct cleanup *back_to = make_cleanup (xfree, text);
11192 info = parse_traceframe_info (text);
11193 do_cleanups (back_to);
11200 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11201 instruction on which a fast tracepoint may be placed. Returns -1
11202 if the packet is not supported, and 0 if the minimum instruction
11203 length is unknown. */
11206 remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
11208 struct remote_state *rs = get_remote_state ();
11211 /* If we're not debugging a process yet, the IPA can't be
11213 if (!target_has_execution)
11216 /* Make sure the remote is pointing at the right process. */
11217 set_general_process ();
11219 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
11221 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11222 if (*reply == '\0')
11226 ULONGEST min_insn_len;
11228 unpack_varlen_hex (reply, &min_insn_len);
11230 return (int) min_insn_len;
11235 remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
11237 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
11239 struct remote_state *rs = get_remote_state ();
11240 char *buf = rs->buf;
11241 char *endbuf = rs->buf + get_remote_packet_size ();
11242 enum packet_result result;
11244 gdb_assert (val >= 0 || val == -1);
11245 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11246 /* Send -1 as literal "-1" to avoid host size dependency. */
11250 buf += hexnumstr (buf, (ULONGEST) -val);
11253 buf += hexnumstr (buf, (ULONGEST) val);
11256 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11257 result = packet_ok (rs->buf,
11258 &remote_protocol_packets[PACKET_QTBuffer_size]);
11260 if (result != PACKET_OK)
11261 warning (_("Bogus reply from target: %s"), rs->buf);
11266 remote_set_trace_notes (struct target_ops *self,
11267 const char *user, const char *notes,
11268 const char *stop_notes)
11270 struct remote_state *rs = get_remote_state ();
11272 char *buf = rs->buf;
11273 char *endbuf = rs->buf + get_remote_packet_size ();
11276 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11279 buf += xsnprintf (buf, endbuf - buf, "user:");
11280 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
11286 buf += xsnprintf (buf, endbuf - buf, "notes:");
11287 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
11293 buf += xsnprintf (buf, endbuf - buf, "tstop:");
11294 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
11298 /* Ensure the buffer is terminated. */
11302 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11303 if (*reply == '\0')
11306 if (strcmp (reply, "OK") != 0)
11307 error (_("Bogus reply from target: %s"), reply);
11313 remote_use_agent (struct target_ops *self, int use)
11315 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
11317 struct remote_state *rs = get_remote_state ();
11319 /* If the stub supports QAgent. */
11320 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
11322 getpkt (&rs->buf, &rs->buf_size, 0);
11324 if (strcmp (rs->buf, "OK") == 0)
11335 remote_can_use_agent (struct target_ops *self)
11337 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
11340 struct btrace_target_info
11342 /* The ptid of the traced thread. */
11345 /* The obtained branch trace configuration. */
11346 struct btrace_config conf;
11349 /* Reset our idea of our target's btrace configuration. */
11352 remote_btrace_reset (void)
11354 struct remote_state *rs = get_remote_state ();
11356 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11359 /* Check whether the target supports branch tracing. */
11362 remote_supports_btrace (struct target_ops *self, enum btrace_format format)
11364 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
11366 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
11371 case BTRACE_FORMAT_NONE:
11374 case BTRACE_FORMAT_BTS:
11375 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
11378 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
11381 /* Synchronize the configuration with the target. */
11384 btrace_sync_conf (const struct btrace_config *conf)
11386 struct packet_config *packet;
11387 struct remote_state *rs;
11388 char *buf, *pos, *endbuf;
11390 rs = get_remote_state ();
11392 endbuf = buf + get_remote_packet_size ();
11394 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11395 if (packet_config_support (packet) == PACKET_ENABLE
11396 && conf->bts.size != rs->btrace_config.bts.size)
11399 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11403 getpkt (&buf, &rs->buf_size, 0);
11405 if (packet_ok (buf, packet) == PACKET_ERROR)
11407 if (buf[0] == 'E' && buf[1] == '.')
11408 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11410 error (_("Failed to configure the BTS buffer size."));
11413 rs->btrace_config.bts.size = conf->bts.size;
11417 /* Read the current thread's btrace configuration from the target and
11418 store it into CONF. */
11421 btrace_read_config (struct btrace_config *conf)
11425 xml = target_read_stralloc (¤t_target,
11426 TARGET_OBJECT_BTRACE_CONF, "");
11429 struct cleanup *cleanup;
11431 cleanup = make_cleanup (xfree, xml);
11432 parse_xml_btrace_conf (conf, xml);
11433 do_cleanups (cleanup);
11437 /* Enable branch tracing. */
11439 static struct btrace_target_info *
11440 remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11441 const struct btrace_config *conf)
11443 struct btrace_target_info *tinfo = NULL;
11444 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11445 struct remote_state *rs = get_remote_state ();
11446 char *buf = rs->buf;
11447 char *endbuf = rs->buf + get_remote_packet_size ();
11448 volatile struct gdb_exception err;
11450 if (packet_config_support (packet) != PACKET_ENABLE)
11451 error (_("Target does not support branch tracing."));
11453 btrace_sync_conf (conf);
11455 set_general_thread (ptid);
11457 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11459 getpkt (&rs->buf, &rs->buf_size, 0);
11461 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11463 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11464 error (_("Could not enable branch tracing for %s: %s"),
11465 target_pid_to_str (ptid), rs->buf + 2);
11467 error (_("Could not enable branch tracing for %s."),
11468 target_pid_to_str (ptid));
11471 tinfo = xzalloc (sizeof (*tinfo));
11472 tinfo->ptid = ptid;
11474 /* If we fail to read the configuration, we lose some information, but the
11475 tracing itself is not impacted. */
11476 TRY_CATCH (err, RETURN_MASK_ERROR)
11477 btrace_read_config (&tinfo->conf);
11479 if (err.message != NULL)
11480 warning ("%s", err.message);
11485 /* Disable branch tracing. */
11488 remote_disable_btrace (struct target_ops *self,
11489 struct btrace_target_info *tinfo)
11491 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11492 struct remote_state *rs = get_remote_state ();
11493 char *buf = rs->buf;
11494 char *endbuf = rs->buf + get_remote_packet_size ();
11496 if (packet_config_support (packet) != PACKET_ENABLE)
11497 error (_("Target does not support branch tracing."));
11499 set_general_thread (tinfo->ptid);
11501 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11503 getpkt (&rs->buf, &rs->buf_size, 0);
11505 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11507 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11508 error (_("Could not disable branch tracing for %s: %s"),
11509 target_pid_to_str (tinfo->ptid), rs->buf + 2);
11511 error (_("Could not disable branch tracing for %s."),
11512 target_pid_to_str (tinfo->ptid));
11518 /* Teardown branch tracing. */
11521 remote_teardown_btrace (struct target_ops *self,
11522 struct btrace_target_info *tinfo)
11524 /* We must not talk to the target during teardown. */
11528 /* Read the branch trace. */
11530 static enum btrace_error
11531 remote_read_btrace (struct target_ops *self,
11532 struct btrace_data *btrace,
11533 struct btrace_target_info *tinfo,
11534 enum btrace_read_type type)
11536 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11537 struct remote_state *rs = get_remote_state ();
11538 struct cleanup *cleanup;
11542 if (packet_config_support (packet) != PACKET_ENABLE)
11543 error (_("Target does not support branch tracing."));
11545 #if !defined(HAVE_LIBEXPAT)
11546 error (_("Cannot process branch tracing result. XML parsing not supported."));
11551 case BTRACE_READ_ALL:
11554 case BTRACE_READ_NEW:
11557 case BTRACE_READ_DELTA:
11561 internal_error (__FILE__, __LINE__,
11562 _("Bad branch tracing read type: %u."),
11563 (unsigned int) type);
11566 xml = target_read_stralloc (¤t_target,
11567 TARGET_OBJECT_BTRACE, annex);
11569 return BTRACE_ERR_UNKNOWN;
11571 cleanup = make_cleanup (xfree, xml);
11572 parse_xml_btrace (btrace, xml);
11573 do_cleanups (cleanup);
11575 return BTRACE_ERR_NONE;
11578 static const struct btrace_config *
11579 remote_btrace_conf (struct target_ops *self,
11580 const struct btrace_target_info *tinfo)
11582 return &tinfo->conf;
11586 remote_augmented_libraries_svr4_read (struct target_ops *self)
11588 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
11592 /* Implementation of to_load. */
11595 remote_load (struct target_ops *self, const char *name, int from_tty)
11597 generic_load (name, from_tty);
11601 init_remote_ops (void)
11603 remote_ops.to_shortname = "remote";
11604 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
11605 remote_ops.to_doc =
11606 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11607 Specify the serial device it is connected to\n\
11608 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11609 remote_ops.to_open = remote_open;
11610 remote_ops.to_close = remote_close;
11611 remote_ops.to_detach = remote_detach;
11612 remote_ops.to_disconnect = remote_disconnect;
11613 remote_ops.to_resume = remote_resume;
11614 remote_ops.to_wait = remote_wait;
11615 remote_ops.to_fetch_registers = remote_fetch_registers;
11616 remote_ops.to_store_registers = remote_store_registers;
11617 remote_ops.to_prepare_to_store = remote_prepare_to_store;
11618 remote_ops.to_files_info = remote_files_info;
11619 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
11620 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
11621 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
11622 remote_ops.to_stopped_data_address = remote_stopped_data_address;
11623 remote_ops.to_watchpoint_addr_within_range =
11624 remote_watchpoint_addr_within_range;
11625 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
11626 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
11627 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
11628 remote_ops.to_region_ok_for_hw_watchpoint
11629 = remote_region_ok_for_hw_watchpoint;
11630 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
11631 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
11632 remote_ops.to_kill = remote_kill;
11633 remote_ops.to_load = remote_load;
11634 remote_ops.to_mourn_inferior = remote_mourn;
11635 remote_ops.to_pass_signals = remote_pass_signals;
11636 remote_ops.to_program_signals = remote_program_signals;
11637 remote_ops.to_thread_alive = remote_thread_alive;
11638 remote_ops.to_update_thread_list = remote_update_thread_list;
11639 remote_ops.to_pid_to_str = remote_pid_to_str;
11640 remote_ops.to_extra_thread_info = remote_threads_extra_info;
11641 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
11642 remote_ops.to_stop = remote_stop;
11643 remote_ops.to_xfer_partial = remote_xfer_partial;
11644 remote_ops.to_rcmd = remote_rcmd;
11645 remote_ops.to_log_command = serial_log_command;
11646 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
11647 remote_ops.to_stratum = process_stratum;
11648 remote_ops.to_has_all_memory = default_child_has_all_memory;
11649 remote_ops.to_has_memory = default_child_has_memory;
11650 remote_ops.to_has_stack = default_child_has_stack;
11651 remote_ops.to_has_registers = default_child_has_registers;
11652 remote_ops.to_has_execution = default_child_has_execution;
11653 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
11654 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
11655 remote_ops.to_magic = OPS_MAGIC;
11656 remote_ops.to_memory_map = remote_memory_map;
11657 remote_ops.to_flash_erase = remote_flash_erase;
11658 remote_ops.to_flash_done = remote_flash_done;
11659 remote_ops.to_read_description = remote_read_description;
11660 remote_ops.to_search_memory = remote_search_memory;
11661 remote_ops.to_can_async_p = remote_can_async_p;
11662 remote_ops.to_is_async_p = remote_is_async_p;
11663 remote_ops.to_async = remote_async;
11664 remote_ops.to_terminal_inferior = remote_terminal_inferior;
11665 remote_ops.to_terminal_ours = remote_terminal_ours;
11666 remote_ops.to_supports_non_stop = remote_supports_non_stop;
11667 remote_ops.to_supports_multi_process = remote_supports_multi_process;
11668 remote_ops.to_supports_disable_randomization
11669 = remote_supports_disable_randomization;
11670 remote_ops.to_fileio_open = remote_hostio_open;
11671 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11672 remote_ops.to_fileio_pread = remote_hostio_pread;
11673 remote_ops.to_fileio_close = remote_hostio_close;
11674 remote_ops.to_fileio_unlink = remote_hostio_unlink;
11675 remote_ops.to_fileio_readlink = remote_hostio_readlink;
11676 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
11677 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
11678 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
11679 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
11680 remote_ops.to_trace_init = remote_trace_init;
11681 remote_ops.to_download_tracepoint = remote_download_tracepoint;
11682 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
11683 remote_ops.to_download_trace_state_variable
11684 = remote_download_trace_state_variable;
11685 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11686 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
11687 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11688 remote_ops.to_trace_start = remote_trace_start;
11689 remote_ops.to_get_trace_status = remote_get_trace_status;
11690 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
11691 remote_ops.to_trace_stop = remote_trace_stop;
11692 remote_ops.to_trace_find = remote_trace_find;
11693 remote_ops.to_get_trace_state_variable_value
11694 = remote_get_trace_state_variable_value;
11695 remote_ops.to_save_trace_data = remote_save_trace_data;
11696 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
11697 remote_ops.to_upload_trace_state_variables
11698 = remote_upload_trace_state_variables;
11699 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
11700 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
11701 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
11702 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
11703 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
11704 remote_ops.to_set_trace_notes = remote_set_trace_notes;
11705 remote_ops.to_core_of_thread = remote_core_of_thread;
11706 remote_ops.to_verify_memory = remote_verify_memory;
11707 remote_ops.to_get_tib_address = remote_get_tib_address;
11708 remote_ops.to_set_permissions = remote_set_permissions;
11709 remote_ops.to_static_tracepoint_marker_at
11710 = remote_static_tracepoint_marker_at;
11711 remote_ops.to_static_tracepoint_markers_by_strid
11712 = remote_static_tracepoint_markers_by_strid;
11713 remote_ops.to_traceframe_info = remote_traceframe_info;
11714 remote_ops.to_use_agent = remote_use_agent;
11715 remote_ops.to_can_use_agent = remote_can_use_agent;
11716 remote_ops.to_supports_btrace = remote_supports_btrace;
11717 remote_ops.to_enable_btrace = remote_enable_btrace;
11718 remote_ops.to_disable_btrace = remote_disable_btrace;
11719 remote_ops.to_teardown_btrace = remote_teardown_btrace;
11720 remote_ops.to_read_btrace = remote_read_btrace;
11721 remote_ops.to_btrace_conf = remote_btrace_conf;
11722 remote_ops.to_augmented_libraries_svr4_read =
11723 remote_augmented_libraries_svr4_read;
11726 /* Set up the extended remote vector by making a copy of the standard
11727 remote vector and adding to it. */
11730 init_extended_remote_ops (void)
11732 extended_remote_ops = remote_ops;
11734 extended_remote_ops.to_shortname = "extended-remote";
11735 extended_remote_ops.to_longname =
11736 "Extended remote serial target in gdb-specific protocol";
11737 extended_remote_ops.to_doc =
11738 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11739 Specify the serial device it is connected to (e.g. /dev/ttya).";
11740 extended_remote_ops.to_open = extended_remote_open;
11741 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11742 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
11743 extended_remote_ops.to_detach = extended_remote_detach;
11744 extended_remote_ops.to_attach = extended_remote_attach;
11745 extended_remote_ops.to_post_attach = extended_remote_post_attach;
11746 extended_remote_ops.to_kill = extended_remote_kill;
11747 extended_remote_ops.to_supports_disable_randomization
11748 = extended_remote_supports_disable_randomization;
11752 remote_can_async_p (struct target_ops *ops)
11754 struct remote_state *rs = get_remote_state ();
11756 if (!target_async_permitted)
11757 /* We only enable async when the user specifically asks for it. */
11760 /* We're async whenever the serial device is. */
11761 return serial_can_async_p (rs->remote_desc);
11765 remote_is_async_p (struct target_ops *ops)
11767 struct remote_state *rs = get_remote_state ();
11769 if (!target_async_permitted)
11770 /* We only enable async when the user specifically asks for it. */
11773 /* We're async whenever the serial device is. */
11774 return serial_is_async_p (rs->remote_desc);
11777 /* Pass the SERIAL event on and up to the client. One day this code
11778 will be able to delay notifying the client of an event until the
11779 point where an entire packet has been received. */
11781 static serial_event_ftype remote_async_serial_handler;
11784 remote_async_serial_handler (struct serial *scb, void *context)
11786 struct remote_state *rs = context;
11788 /* Don't propogate error information up to the client. Instead let
11789 the client find out about the error by querying the target. */
11790 rs->async_client_callback (INF_REG_EVENT, rs->async_client_context);
11794 remote_async_inferior_event_handler (gdb_client_data data)
11796 inferior_event_handler (INF_REG_EVENT, NULL);
11800 remote_async (struct target_ops *ops,
11801 void (*callback) (enum inferior_event_type event_type,
11805 struct remote_state *rs = get_remote_state ();
11807 if (callback != NULL)
11809 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
11810 rs->async_client_callback = callback;
11811 rs->async_client_context = context;
11813 /* If there are pending events in the stop reply queue tell the
11814 event loop to process them. */
11815 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
11816 mark_async_event_handler (remote_async_inferior_event_token);
11820 serial_async (rs->remote_desc, NULL, NULL);
11821 clear_async_event_handler (remote_async_inferior_event_token);
11826 set_remote_cmd (char *args, int from_tty)
11828 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
11832 show_remote_cmd (char *args, int from_tty)
11834 /* We can't just use cmd_show_list here, because we want to skip
11835 the redundant "show remote Z-packet" and the legacy aliases. */
11836 struct cleanup *showlist_chain;
11837 struct cmd_list_element *list = remote_show_cmdlist;
11838 struct ui_out *uiout = current_uiout;
11840 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11841 for (; list != NULL; list = list->next)
11842 if (strcmp (list->name, "Z-packet") == 0)
11844 else if (list->type == not_set_cmd)
11845 /* Alias commands are exactly like the original, except they
11846 don't have the normal type. */
11850 struct cleanup *option_chain
11851 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
11853 ui_out_field_string (uiout, "name", list->name);
11854 ui_out_text (uiout, ": ");
11855 if (list->type == show_cmd)
11856 do_show_command ((char *) NULL, from_tty, list);
11858 cmd_func (list, NULL, from_tty);
11859 /* Close the tuple. */
11860 do_cleanups (option_chain);
11863 /* Close the tuple. */
11864 do_cleanups (showlist_chain);
11868 /* Function to be called whenever a new objfile (shlib) is detected. */
11870 remote_new_objfile (struct objfile *objfile)
11872 struct remote_state *rs = get_remote_state ();
11874 if (rs->remote_desc != 0) /* Have a remote connection. */
11875 remote_check_symbols ();
11878 /* Pull all the tracepoints defined on the target and create local
11879 data structures representing them. We don't want to create real
11880 tracepoints yet, we don't want to mess up the user's existing
11884 remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
11886 struct remote_state *rs = get_remote_state ();
11889 /* Ask for a first packet of tracepoint definition. */
11891 getpkt (&rs->buf, &rs->buf_size, 0);
11893 while (*p && *p != 'l')
11895 parse_tracepoint_definition (p, utpp);
11896 /* Ask for another packet of tracepoint definition. */
11898 getpkt (&rs->buf, &rs->buf_size, 0);
11905 remote_upload_trace_state_variables (struct target_ops *self,
11906 struct uploaded_tsv **utsvp)
11908 struct remote_state *rs = get_remote_state ();
11911 /* Ask for a first packet of variable definition. */
11913 getpkt (&rs->buf, &rs->buf_size, 0);
11915 while (*p && *p != 'l')
11917 parse_tsv_definition (p, utsvp);
11918 /* Ask for another packet of variable definition. */
11920 getpkt (&rs->buf, &rs->buf_size, 0);
11926 /* The "set/show range-stepping" show hook. */
11929 show_range_stepping (struct ui_file *file, int from_tty,
11930 struct cmd_list_element *c,
11933 fprintf_filtered (file,
11934 _("Debugger's willingness to use range stepping "
11935 "is %s.\n"), value);
11938 /* The "set/show range-stepping" set hook. */
11941 set_range_stepping (char *ignore_args, int from_tty,
11942 struct cmd_list_element *c)
11944 struct remote_state *rs = get_remote_state ();
11946 /* Whene enabling, check whether range stepping is actually
11947 supported by the target, and warn if not. */
11948 if (use_range_stepping)
11950 if (rs->remote_desc != NULL)
11952 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
11953 remote_vcont_probe (rs);
11955 if (packet_support (PACKET_vCont) == PACKET_ENABLE
11956 && rs->supports_vCont.r)
11960 warning (_("Range stepping is not supported by the current target"));
11965 _initialize_remote (void)
11967 struct remote_state *rs;
11968 struct cmd_list_element *cmd;
11969 const char *cmd_name;
11971 /* architecture specific data */
11972 remote_gdbarch_data_handle =
11973 gdbarch_data_register_post_init (init_remote_state);
11974 remote_g_packet_data_handle =
11975 gdbarch_data_register_pre_init (remote_g_packet_data_init);
11977 /* Initialize the per-target state. At the moment there is only one
11978 of these, not one per target. Only one target is active at a
11980 remote_state = new_remote_state ();
11982 init_remote_ops ();
11983 add_target (&remote_ops);
11985 init_extended_remote_ops ();
11986 add_target (&extended_remote_ops);
11988 /* Hook into new objfile notification. */
11989 observer_attach_new_objfile (remote_new_objfile);
11990 /* We're no longer interested in notification events of an inferior
11992 observer_attach_inferior_exit (discard_pending_stop_replies);
11994 /* Set up signal handlers. */
11995 async_sigint_remote_token =
11996 create_async_signal_handler (async_remote_interrupt, NULL);
11997 async_sigint_remote_twice_token =
11998 create_async_signal_handler (async_remote_interrupt_twice, NULL);
12001 init_remote_threadtests ();
12004 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
12005 /* set/show remote ... */
12007 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
12008 Remote protocol specific variables\n\
12009 Configure various remote-protocol specific variables such as\n\
12010 the packets being used"),
12011 &remote_set_cmdlist, "set remote ",
12012 0 /* allow-unknown */, &setlist);
12013 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
12014 Remote protocol specific variables\n\
12015 Configure various remote-protocol specific variables such as\n\
12016 the packets being used"),
12017 &remote_show_cmdlist, "show remote ",
12018 0 /* allow-unknown */, &showlist);
12020 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12021 Compare section data on target to the exec file.\n\
12022 Argument is a single section name (default: all loaded sections).\n\
12023 To compare only read-only loaded sections, specify the -r option."),
12026 add_cmd ("packet", class_maintenance, packet_command, _("\
12027 Send an arbitrary packet to a remote target.\n\
12028 maintenance packet TEXT\n\
12029 If GDB is talking to an inferior via the GDB serial protocol, then\n\
12030 this command sends the string TEXT to the inferior, and displays the\n\
12031 response packet. GDB supplies the initial `$' character, and the\n\
12032 terminating `#' character and checksum."),
12035 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12036 Set whether to send break if interrupted."), _("\
12037 Show whether to send break if interrupted."), _("\
12038 If set, a break, instead of a cntrl-c, is sent to the remote target."),
12039 set_remotebreak, show_remotebreak,
12040 &setlist, &showlist);
12041 cmd_name = "remotebreak";
12042 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12043 deprecate_cmd (cmd, "set remote interrupt-sequence");
12044 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12045 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12046 deprecate_cmd (cmd, "show remote interrupt-sequence");
12048 add_setshow_enum_cmd ("interrupt-sequence", class_support,
12049 interrupt_sequence_modes, &interrupt_sequence_mode,
12051 Set interrupt sequence to remote target."), _("\
12052 Show interrupt sequence to remote target."), _("\
12053 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12054 NULL, show_interrupt_sequence,
12055 &remote_set_cmdlist,
12056 &remote_show_cmdlist);
12058 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12059 &interrupt_on_connect, _("\
12060 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12061 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12062 If set, interrupt sequence is sent to remote target."),
12064 &remote_set_cmdlist, &remote_show_cmdlist);
12066 /* Install commands for configuring memory read/write packets. */
12068 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12069 Set the maximum number of bytes per memory write packet (deprecated)."),
12071 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12072 Show the maximum number of bytes per memory write packet (deprecated)."),
12074 add_cmd ("memory-write-packet-size", no_class,
12075 set_memory_write_packet_size, _("\
12076 Set the maximum number of bytes per memory-write packet.\n\
12077 Specify the number of bytes in a packet or 0 (zero) for the\n\
12078 default packet size. The actual limit is further reduced\n\
12079 dependent on the target. Specify ``fixed'' to disable the\n\
12080 further restriction and ``limit'' to enable that restriction."),
12081 &remote_set_cmdlist);
12082 add_cmd ("memory-read-packet-size", no_class,
12083 set_memory_read_packet_size, _("\
12084 Set the maximum number of bytes per memory-read packet.\n\
12085 Specify the number of bytes in a packet or 0 (zero) for the\n\
12086 default packet size. The actual limit is further reduced\n\
12087 dependent on the target. Specify ``fixed'' to disable the\n\
12088 further restriction and ``limit'' to enable that restriction."),
12089 &remote_set_cmdlist);
12090 add_cmd ("memory-write-packet-size", no_class,
12091 show_memory_write_packet_size,
12092 _("Show the maximum number of bytes per memory-write packet."),
12093 &remote_show_cmdlist);
12094 add_cmd ("memory-read-packet-size", no_class,
12095 show_memory_read_packet_size,
12096 _("Show the maximum number of bytes per memory-read packet."),
12097 &remote_show_cmdlist);
12099 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
12100 &remote_hw_watchpoint_limit, _("\
12101 Set the maximum number of target hardware watchpoints."), _("\
12102 Show the maximum number of target hardware watchpoints."), _("\
12103 Specify a negative limit for unlimited."),
12104 NULL, NULL, /* FIXME: i18n: The maximum
12105 number of target hardware
12106 watchpoints is %s. */
12107 &remote_set_cmdlist, &remote_show_cmdlist);
12108 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12109 &remote_hw_watchpoint_length_limit, _("\
12110 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12111 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12112 Specify a negative limit for unlimited."),
12113 NULL, NULL, /* FIXME: i18n: The maximum
12114 length (in bytes) of a target
12115 hardware watchpoint is %s. */
12116 &remote_set_cmdlist, &remote_show_cmdlist);
12117 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
12118 &remote_hw_breakpoint_limit, _("\
12119 Set the maximum number of target hardware breakpoints."), _("\
12120 Show the maximum number of target hardware breakpoints."), _("\
12121 Specify a negative limit for unlimited."),
12122 NULL, NULL, /* FIXME: i18n: The maximum
12123 number of target hardware
12124 breakpoints is %s. */
12125 &remote_set_cmdlist, &remote_show_cmdlist);
12127 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12128 &remote_address_size, _("\
12129 Set the maximum size of the address (in bits) in a memory packet."), _("\
12130 Show the maximum size of the address (in bits) in a memory packet."), NULL,
12132 NULL, /* FIXME: i18n: */
12133 &setlist, &showlist);
12135 init_all_packet_configs ();
12137 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
12138 "X", "binary-download", 1);
12140 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
12141 "vCont", "verbose-resume", 0);
12143 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12144 "QPassSignals", "pass-signals", 0);
12146 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12147 "QProgramSignals", "program-signals", 0);
12149 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
12150 "qSymbol", "symbol-lookup", 0);
12152 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
12153 "P", "set-register", 1);
12155 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
12156 "p", "fetch-register", 1);
12158 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
12159 "Z0", "software-breakpoint", 0);
12161 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
12162 "Z1", "hardware-breakpoint", 0);
12164 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
12165 "Z2", "write-watchpoint", 0);
12167 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
12168 "Z3", "read-watchpoint", 0);
12170 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
12171 "Z4", "access-watchpoint", 0);
12173 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12174 "qXfer:auxv:read", "read-aux-vector", 0);
12176 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12177 "qXfer:features:read", "target-features", 0);
12179 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12180 "qXfer:libraries:read", "library-info", 0);
12182 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12183 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12185 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12186 "qXfer:memory-map:read", "memory-map", 0);
12188 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12189 "qXfer:spu:read", "read-spu-object", 0);
12191 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12192 "qXfer:spu:write", "write-spu-object", 0);
12194 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12195 "qXfer:osdata:read", "osdata", 0);
12197 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12198 "qXfer:threads:read", "threads", 0);
12200 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12201 "qXfer:siginfo:read", "read-siginfo-object", 0);
12203 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12204 "qXfer:siginfo:write", "write-siginfo-object", 0);
12206 add_packet_config_cmd
12207 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
12208 "qXfer:traceframe-info:read", "traceframe-info", 0);
12210 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12211 "qXfer:uib:read", "unwind-info-block", 0);
12213 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
12214 "qGetTLSAddr", "get-thread-local-storage-address",
12217 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12218 "qGetTIBAddr", "get-thread-information-block-address",
12221 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12222 "bc", "reverse-continue", 0);
12224 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12225 "bs", "reverse-step", 0);
12227 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12228 "qSupported", "supported-packets", 0);
12230 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12231 "qSearch:memory", "search-memory", 0);
12233 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12234 "qTStatus", "trace-status", 0);
12236 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12237 "vFile:open", "hostio-open", 0);
12239 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12240 "vFile:pread", "hostio-pread", 0);
12242 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12243 "vFile:pwrite", "hostio-pwrite", 0);
12245 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12246 "vFile:close", "hostio-close", 0);
12248 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12249 "vFile:unlink", "hostio-unlink", 0);
12251 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12252 "vFile:readlink", "hostio-readlink", 0);
12254 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12255 "vAttach", "attach", 0);
12257 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12260 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12261 "QStartNoAckMode", "noack", 0);
12263 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12264 "vKill", "kill", 0);
12266 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12267 "qAttached", "query-attached", 0);
12269 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
12270 "ConditionalTracepoints",
12271 "conditional-tracepoints", 0);
12273 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12274 "ConditionalBreakpoints",
12275 "conditional-breakpoints", 0);
12277 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12278 "BreakpointCommands",
12279 "breakpoint-commands", 0);
12281 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12282 "FastTracepoints", "fast-tracepoints", 0);
12284 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12285 "TracepointSource", "TracepointSource", 0);
12287 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12288 "QAllow", "allow", 0);
12290 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12291 "StaticTracepoints", "static-tracepoints", 0);
12293 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12294 "InstallInTrace", "install-in-trace", 0);
12296 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12297 "qXfer:statictrace:read", "read-sdata-object", 0);
12299 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12300 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12302 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12303 "QDisableRandomization", "disable-randomization", 0);
12305 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12306 "QAgent", "agent", 0);
12308 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12309 "QTBuffer:size", "trace-buffer-size", 0);
12311 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12312 "Qbtrace:off", "disable-btrace", 0);
12314 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12315 "Qbtrace:bts", "enable-btrace", 0);
12317 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12318 "qXfer:btrace", "read-btrace", 0);
12320 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12321 "qXfer:btrace-conf", "read-btrace-conf", 0);
12323 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12324 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12326 /* Assert that we've registered commands for all packet configs. */
12330 for (i = 0; i < PACKET_MAX; i++)
12332 /* Ideally all configs would have a command associated. Some
12333 still don't though. */
12338 case PACKET_QNonStop:
12339 case PACKET_multiprocess_feature:
12340 case PACKET_EnableDisableTracepoints_feature:
12341 case PACKET_tracenz_feature:
12342 case PACKET_DisconnectedTracing_feature:
12343 case PACKET_augmented_libraries_svr4_read_feature:
12345 /* Additions to this list need to be well justified:
12346 pre-existing packets are OK; new packets are not. */
12354 /* This catches both forgetting to add a config command, and
12355 forgetting to remove a packet from the exception list. */
12356 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12360 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12361 Z sub-packet has its own set and show commands, but users may
12362 have sets to this variable in their .gdbinit files (or in their
12364 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
12365 &remote_Z_packet_detect, _("\
12366 Set use of remote protocol `Z' packets"), _("\
12367 Show use of remote protocol `Z' packets "), _("\
12368 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12370 set_remote_protocol_Z_packet_cmd,
12371 show_remote_protocol_Z_packet_cmd,
12372 /* FIXME: i18n: Use of remote protocol
12373 `Z' packets is %s. */
12374 &remote_set_cmdlist, &remote_show_cmdlist);
12376 add_prefix_cmd ("remote", class_files, remote_command, _("\
12377 Manipulate files on the remote system\n\
12378 Transfer files to and from the remote target system."),
12379 &remote_cmdlist, "remote ",
12380 0 /* allow-unknown */, &cmdlist);
12382 add_cmd ("put", class_files, remote_put_command,
12383 _("Copy a local file to the remote system."),
12386 add_cmd ("get", class_files, remote_get_command,
12387 _("Copy a remote file to the local system."),
12390 add_cmd ("delete", class_files, remote_delete_command,
12391 _("Delete a remote file."),
12394 remote_exec_file = xstrdup ("");
12395 add_setshow_string_noescape_cmd ("exec-file", class_files,
12396 &remote_exec_file, _("\
12397 Set the remote pathname for \"run\""), _("\
12398 Show the remote pathname for \"run\""), NULL, NULL, NULL,
12399 &remote_set_cmdlist, &remote_show_cmdlist);
12401 add_setshow_boolean_cmd ("range-stepping", class_run,
12402 &use_range_stepping, _("\
12403 Enable or disable range stepping."), _("\
12404 Show whether target-assisted range stepping is enabled."), _("\
12405 If on, and the target supports it, when stepping a source line, GDB\n\
12406 tells the target to step the corresponding range of addresses itself instead\n\
12407 of issuing multiple single-steps. This speeds up source level\n\
12408 stepping. If off, GDB always issues single-steps, even if range\n\
12409 stepping is supported by the target. The default is on."),
12410 set_range_stepping,
12411 show_range_stepping,
12415 /* Eventually initialize fileio. See fileio.c */
12416 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
12418 /* Take advantage of the fact that the TID field is not used, to tag
12419 special ptids with it set to != 0. */
12420 magic_null_ptid = ptid_build (42000, -1, 1);
12421 not_sent_ptid = ptid_build (42000, -2, 1);
12422 any_thread_ptid = ptid_build (42000, 0, 1);
12424 target_buf_size = 2048;
12425 target_buf = xmalloc (target_buf_size);