1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2014 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 void remote_find_new_threads (void);
173 static int putpkt_binary (const char *buf, int cnt);
175 static void check_binary_download (CORE_ADDR addr);
177 struct packet_config;
179 static void show_packet_config_cmd (struct packet_config *config);
181 static void show_remote_protocol_packet_cmd (struct ui_file *file,
183 struct cmd_list_element *c,
186 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
187 static ptid_t read_ptid (char *buf, char **obuf);
189 static void remote_set_permissions (struct target_ops *self);
192 static int remote_get_trace_status (struct target_ops *self,
193 struct trace_status *ts);
195 static int remote_upload_tracepoints (struct target_ops *self,
196 struct uploaded_tp **utpp);
198 static int remote_upload_trace_state_variables (struct target_ops *self,
199 struct uploaded_tsv **utsvp);
201 static void remote_query_supported (void);
203 static void remote_check_symbols (void);
205 void _initialize_remote (void);
208 static void stop_reply_xfree (struct stop_reply *);
209 static void remote_parse_stop_reply (char *, struct stop_reply *);
210 static void push_stop_reply (struct stop_reply *);
211 static void discard_pending_stop_replies_in_queue (struct remote_state *);
212 static int peek_stop_reply (ptid_t ptid);
214 static void remote_async_inferior_event_handler (gdb_client_data);
216 static void remote_terminal_ours (struct target_ops *self);
218 static int remote_read_description_p (struct target_ops *target);
220 static void remote_console_output (char *msg);
222 static int remote_supports_cond_breakpoints (struct target_ops *self);
224 static int remote_can_run_breakpoint_commands (struct target_ops *self);
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;
378 /* Private data that we'll store in (struct thread_info)->private. */
379 struct private_thread_info
386 free_private_thread_info (struct private_thread_info *info)
392 /* This data could be associated with a target, but we do not always
393 have access to the current target when we need it, so for now it is
394 static. This will be fine for as long as only one target is in use
396 static struct remote_state *remote_state;
398 static struct remote_state *
399 get_remote_state_raw (void)
404 /* Allocate a new struct remote_state with xmalloc, initialize it, and
407 static struct remote_state *
408 new_remote_state (void)
410 struct remote_state *result = XCNEW (struct remote_state);
412 /* The default buffer size is unimportant; it will be expanded
413 whenever a larger buffer is needed. */
414 result->buf_size = 400;
415 result->buf = xmalloc (result->buf_size);
416 result->remote_traceframe_number = -1;
417 result->last_sent_signal = GDB_SIGNAL_0;
422 /* Description of the remote protocol for a given architecture. */
426 long offset; /* Offset into G packet. */
427 long regnum; /* GDB's internal register number. */
428 LONGEST pnum; /* Remote protocol register number. */
429 int in_g_packet; /* Always part of G packet. */
430 /* long size in bytes; == register_size (target_gdbarch (), regnum);
432 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
436 struct remote_arch_state
438 /* Description of the remote protocol registers. */
439 long sizeof_g_packet;
441 /* Description of the remote protocol registers indexed by REGNUM
442 (making an array gdbarch_num_regs in size). */
443 struct packet_reg *regs;
445 /* This is the size (in chars) of the first response to the ``g''
446 packet. It is used as a heuristic when determining the maximum
447 size of memory-read and memory-write packets. A target will
448 typically only reserve a buffer large enough to hold the ``g''
449 packet. The size does not include packet overhead (headers and
451 long actual_register_packet_size;
453 /* This is the maximum size (in chars) of a non read/write packet.
454 It is also used as a cap on the size of read/write packets. */
455 long remote_packet_size;
458 /* Utility: generate error from an incoming stub packet. */
460 trace_error (char *buf)
463 return; /* not an error msg */
466 case '1': /* malformed packet error */
467 if (*++buf == '0') /* general case: */
468 error (_("remote.c: error in outgoing packet."));
470 error (_("remote.c: error in outgoing packet at field #%ld."),
471 strtol (buf, NULL, 16));
473 error (_("Target returns error code '%s'."), buf);
477 /* Utility: wait for reply from stub, while accepting "O" packets. */
479 remote_get_noisy_reply (char **buf_p,
482 do /* Loop on reply from remote stub. */
486 QUIT; /* Allow user to bail out with ^C. */
487 getpkt (buf_p, sizeof_buf, 0);
491 else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
494 CORE_ADDR from, to, org_to;
496 int adjusted_size = 0;
497 volatile struct gdb_exception ex;
499 p = buf + strlen ("qRelocInsn:");
500 pp = unpack_varlen_hex (p, &ul);
502 error (_("invalid qRelocInsn packet: %s"), buf);
506 unpack_varlen_hex (p, &ul);
511 TRY_CATCH (ex, RETURN_MASK_ALL)
513 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
517 adjusted_size = to - org_to;
519 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
522 else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
524 /* Propagate memory errors silently back to the target.
525 The stub may have limited the range of addresses we
526 can write to, for example. */
531 /* Something unexpectedly bad happened. Be verbose so
532 we can tell what, and propagate the error back to the
533 stub, so it doesn't get stuck waiting for a
535 exception_fprintf (gdb_stderr, ex,
536 _("warning: relocating instruction: "));
540 else if (buf[0] == 'O' && buf[1] != 'K')
541 remote_console_output (buf + 1); /* 'O' message from stub */
543 return buf; /* Here's the actual reply. */
548 /* Handle for retreving the remote protocol data from gdbarch. */
549 static struct gdbarch_data *remote_gdbarch_data_handle;
551 static struct remote_arch_state *
552 get_remote_arch_state (void)
554 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
557 /* Fetch the global remote target state. */
559 static struct remote_state *
560 get_remote_state (void)
562 /* Make sure that the remote architecture state has been
563 initialized, because doing so might reallocate rs->buf. Any
564 function which calls getpkt also needs to be mindful of changes
565 to rs->buf, but this call limits the number of places which run
567 get_remote_arch_state ();
569 return get_remote_state_raw ();
573 compare_pnums (const void *lhs_, const void *rhs_)
575 const struct packet_reg * const *lhs = lhs_;
576 const struct packet_reg * const *rhs = rhs_;
578 if ((*lhs)->pnum < (*rhs)->pnum)
580 else if ((*lhs)->pnum == (*rhs)->pnum)
587 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
589 int regnum, num_remote_regs, offset;
590 struct packet_reg **remote_regs;
592 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
594 struct packet_reg *r = ®s[regnum];
596 if (register_size (gdbarch, regnum) == 0)
597 /* Do not try to fetch zero-sized (placeholder) registers. */
600 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
605 /* Define the g/G packet format as the contents of each register
606 with a remote protocol number, in order of ascending protocol
609 remote_regs = alloca (gdbarch_num_regs (gdbarch)
610 * sizeof (struct packet_reg *));
611 for (num_remote_regs = 0, regnum = 0;
612 regnum < gdbarch_num_regs (gdbarch);
614 if (regs[regnum].pnum != -1)
615 remote_regs[num_remote_regs++] = ®s[regnum];
617 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
620 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
622 remote_regs[regnum]->in_g_packet = 1;
623 remote_regs[regnum]->offset = offset;
624 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
630 /* Given the architecture described by GDBARCH, return the remote
631 protocol register's number and the register's offset in the g/G
632 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
633 If the target does not have a mapping for REGNUM, return false,
634 otherwise, return true. */
637 remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
638 int *pnum, int *poffset)
641 struct packet_reg *regs;
642 struct cleanup *old_chain;
644 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
646 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
647 old_chain = make_cleanup (xfree, regs);
649 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
651 *pnum = regs[regnum].pnum;
652 *poffset = regs[regnum].offset;
654 do_cleanups (old_chain);
660 init_remote_state (struct gdbarch *gdbarch)
662 struct remote_state *rs = get_remote_state_raw ();
663 struct remote_arch_state *rsa;
665 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
667 /* Use the architecture to build a regnum<->pnum table, which will be
668 1:1 unless a feature set specifies otherwise. */
669 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
670 gdbarch_num_regs (gdbarch),
673 /* Record the maximum possible size of the g packet - it may turn out
675 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
677 /* Default maximum number of characters in a packet body. Many
678 remote stubs have a hardwired buffer size of 400 bytes
679 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
680 as the maximum packet-size to ensure that the packet and an extra
681 NUL character can always fit in the buffer. This stops GDB
682 trashing stubs that try to squeeze an extra NUL into what is
683 already a full buffer (As of 1999-12-04 that was most stubs). */
684 rsa->remote_packet_size = 400 - 1;
686 /* This one is filled in when a ``g'' packet is received. */
687 rsa->actual_register_packet_size = 0;
689 /* Should rsa->sizeof_g_packet needs more space than the
690 default, adjust the size accordingly. Remember that each byte is
691 encoded as two characters. 32 is the overhead for the packet
692 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
693 (``$NN:G...#NN'') is a better guess, the below has been padded a
695 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
696 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
698 /* Make sure that the packet buffer is plenty big enough for
699 this architecture. */
700 if (rs->buf_size < rsa->remote_packet_size)
702 rs->buf_size = 2 * rsa->remote_packet_size;
703 rs->buf = xrealloc (rs->buf, rs->buf_size);
709 /* Return the current allowed size of a remote packet. This is
710 inferred from the current architecture, and should be used to
711 limit the length of outgoing packets. */
713 get_remote_packet_size (void)
715 struct remote_state *rs = get_remote_state ();
716 struct remote_arch_state *rsa = get_remote_arch_state ();
718 if (rs->explicit_packet_size)
719 return rs->explicit_packet_size;
721 return rsa->remote_packet_size;
724 static struct packet_reg *
725 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
727 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
731 struct packet_reg *r = &rsa->regs[regnum];
733 gdb_assert (r->regnum == regnum);
738 static struct packet_reg *
739 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
743 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
745 struct packet_reg *r = &rsa->regs[i];
753 static struct target_ops remote_ops;
755 static struct target_ops extended_remote_ops;
757 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
758 ``forever'' still use the normal timeout mechanism. This is
759 currently used by the ASYNC code to guarentee that target reads
760 during the initial connect always time-out. Once getpkt has been
761 modified to return a timeout indication and, in turn
762 remote_wait()/wait_for_inferior() have gained a timeout parameter
764 static int wait_forever_enabled_p = 1;
766 /* Allow the user to specify what sequence to send to the remote
767 when he requests a program interruption: Although ^C is usually
768 what remote systems expect (this is the default, here), it is
769 sometimes preferable to send a break. On other systems such
770 as the Linux kernel, a break followed by g, which is Magic SysRq g
771 is required in order to interrupt the execution. */
772 const char interrupt_sequence_control_c[] = "Ctrl-C";
773 const char interrupt_sequence_break[] = "BREAK";
774 const char interrupt_sequence_break_g[] = "BREAK-g";
775 static const char *const interrupt_sequence_modes[] =
777 interrupt_sequence_control_c,
778 interrupt_sequence_break,
779 interrupt_sequence_break_g,
782 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
785 show_interrupt_sequence (struct ui_file *file, int from_tty,
786 struct cmd_list_element *c,
789 if (interrupt_sequence_mode == interrupt_sequence_control_c)
790 fprintf_filtered (file,
791 _("Send the ASCII ETX character (Ctrl-c) "
792 "to the remote target to interrupt the "
793 "execution of the program.\n"));
794 else if (interrupt_sequence_mode == interrupt_sequence_break)
795 fprintf_filtered (file,
796 _("send a break signal to the remote target "
797 "to interrupt the execution of the program.\n"));
798 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
799 fprintf_filtered (file,
800 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
801 "the remote target to interrupt the execution "
802 "of Linux kernel.\n"));
804 internal_error (__FILE__, __LINE__,
805 _("Invalid value for interrupt_sequence_mode: %s."),
806 interrupt_sequence_mode);
809 /* This boolean variable specifies whether interrupt_sequence is sent
810 to the remote target when gdb connects to it.
811 This is mostly needed when you debug the Linux kernel: The Linux kernel
812 expects BREAK g which is Magic SysRq g for connecting gdb. */
813 static int interrupt_on_connect = 0;
815 /* This variable is used to implement the "set/show remotebreak" commands.
816 Since these commands are now deprecated in favor of "set/show remote
817 interrupt-sequence", it no longer has any effect on the code. */
818 static int remote_break;
821 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
824 interrupt_sequence_mode = interrupt_sequence_break;
826 interrupt_sequence_mode = interrupt_sequence_control_c;
830 show_remotebreak (struct ui_file *file, int from_tty,
831 struct cmd_list_element *c,
836 /* This variable sets the number of bits in an address that are to be
837 sent in a memory ("M" or "m") packet. Normally, after stripping
838 leading zeros, the entire address would be sent. This variable
839 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
840 initial implementation of remote.c restricted the address sent in
841 memory packets to ``host::sizeof long'' bytes - (typically 32
842 bits). Consequently, for 64 bit targets, the upper 32 bits of an
843 address was never sent. Since fixing this bug may cause a break in
844 some remote targets this variable is principly provided to
845 facilitate backward compatibility. */
847 static unsigned int remote_address_size;
849 /* Temporary to track who currently owns the terminal. See
850 remote_terminal_* for more details. */
852 static int remote_async_terminal_ours_p;
854 /* The executable file to use for "run" on the remote side. */
856 static char *remote_exec_file = "";
859 /* User configurable variables for the number of characters in a
860 memory read/write packet. MIN (rsa->remote_packet_size,
861 rsa->sizeof_g_packet) is the default. Some targets need smaller
862 values (fifo overruns, et.al.) and some users need larger values
863 (speed up transfers). The variables ``preferred_*'' (the user
864 request), ``current_*'' (what was actually set) and ``forced_*''
865 (Positive - a soft limit, negative - a hard limit). */
867 struct memory_packet_config
874 /* Compute the current size of a read/write packet. Since this makes
875 use of ``actual_register_packet_size'' the computation is dynamic. */
878 get_memory_packet_size (struct memory_packet_config *config)
880 struct remote_state *rs = get_remote_state ();
881 struct remote_arch_state *rsa = get_remote_arch_state ();
883 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
884 law?) that some hosts don't cope very well with large alloca()
885 calls. Eventually the alloca() code will be replaced by calls to
886 xmalloc() and make_cleanups() allowing this restriction to either
887 be lifted or removed. */
888 #ifndef MAX_REMOTE_PACKET_SIZE
889 #define MAX_REMOTE_PACKET_SIZE 16384
891 /* NOTE: 20 ensures we can write at least one byte. */
892 #ifndef MIN_REMOTE_PACKET_SIZE
893 #define MIN_REMOTE_PACKET_SIZE 20
898 if (config->size <= 0)
899 what_they_get = MAX_REMOTE_PACKET_SIZE;
901 what_they_get = config->size;
905 what_they_get = get_remote_packet_size ();
906 /* Limit the packet to the size specified by the user. */
908 && what_they_get > config->size)
909 what_they_get = config->size;
911 /* Limit it to the size of the targets ``g'' response unless we have
912 permission from the stub to use a larger packet size. */
913 if (rs->explicit_packet_size == 0
914 && rsa->actual_register_packet_size > 0
915 && what_they_get > rsa->actual_register_packet_size)
916 what_they_get = rsa->actual_register_packet_size;
918 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
919 what_they_get = MAX_REMOTE_PACKET_SIZE;
920 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
921 what_they_get = MIN_REMOTE_PACKET_SIZE;
923 /* Make sure there is room in the global buffer for this packet
924 (including its trailing NUL byte). */
925 if (rs->buf_size < what_they_get + 1)
927 rs->buf_size = 2 * what_they_get;
928 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
931 return what_they_get;
934 /* Update the size of a read/write packet. If they user wants
935 something really big then do a sanity check. */
938 set_memory_packet_size (char *args, struct memory_packet_config *config)
940 int fixed_p = config->fixed_p;
941 long size = config->size;
944 error (_("Argument required (integer, `fixed' or `limited')."));
945 else if (strcmp (args, "hard") == 0
946 || strcmp (args, "fixed") == 0)
948 else if (strcmp (args, "soft") == 0
949 || strcmp (args, "limit") == 0)
955 size = strtoul (args, &end, 0);
957 error (_("Invalid %s (bad syntax)."), config->name);
959 /* Instead of explicitly capping the size of a packet to
960 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
961 instead allowed to set the size to something arbitrarily
963 if (size > MAX_REMOTE_PACKET_SIZE)
964 error (_("Invalid %s (too large)."), config->name);
968 if (fixed_p && !config->fixed_p)
970 if (! query (_("The target may not be able to correctly handle a %s\n"
971 "of %ld bytes. Change the packet size? "),
973 error (_("Packet size not changed."));
975 /* Update the config. */
976 config->fixed_p = fixed_p;
981 show_memory_packet_size (struct memory_packet_config *config)
983 printf_filtered (_("The %s is %ld. "), config->name, config->size);
985 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
986 get_memory_packet_size (config));
988 printf_filtered (_("Packets are limited to %ld bytes.\n"),
989 get_memory_packet_size (config));
992 static struct memory_packet_config memory_write_packet_config =
994 "memory-write-packet-size",
998 set_memory_write_packet_size (char *args, int from_tty)
1000 set_memory_packet_size (args, &memory_write_packet_config);
1004 show_memory_write_packet_size (char *args, int from_tty)
1006 show_memory_packet_size (&memory_write_packet_config);
1010 get_memory_write_packet_size (void)
1012 return get_memory_packet_size (&memory_write_packet_config);
1015 static struct memory_packet_config memory_read_packet_config =
1017 "memory-read-packet-size",
1021 set_memory_read_packet_size (char *args, int from_tty)
1023 set_memory_packet_size (args, &memory_read_packet_config);
1027 show_memory_read_packet_size (char *args, int from_tty)
1029 show_memory_packet_size (&memory_read_packet_config);
1033 get_memory_read_packet_size (void)
1035 long size = get_memory_packet_size (&memory_read_packet_config);
1037 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1038 extra buffer size argument before the memory read size can be
1039 increased beyond this. */
1040 if (size > get_remote_packet_size ())
1041 size = get_remote_packet_size ();
1046 /* Generic configuration support for packets the stub optionally
1047 supports. Allows the user to specify the use of the packet as well
1048 as allowing GDB to auto-detect support in the remote stub. */
1052 PACKET_SUPPORT_UNKNOWN = 0,
1057 struct packet_config
1062 /* If auto, GDB auto-detects support for this packet or feature,
1063 either through qSupported, or by trying the packet and looking
1064 at the response. If true, GDB assumes the target supports this
1065 packet. If false, the packet is disabled. Configs that don't
1066 have an associated command always have this set to auto. */
1067 enum auto_boolean detect;
1069 /* Does the target support this packet? */
1070 enum packet_support support;
1073 /* Analyze a packet's return value and update the packet config
1083 static enum packet_support packet_config_support (struct packet_config *config);
1084 static enum packet_support packet_support (int packet);
1087 show_packet_config_cmd (struct packet_config *config)
1089 char *support = "internal-error";
1091 switch (packet_config_support (config))
1094 support = "enabled";
1096 case PACKET_DISABLE:
1097 support = "disabled";
1099 case PACKET_SUPPORT_UNKNOWN:
1100 support = "unknown";
1103 switch (config->detect)
1105 case AUTO_BOOLEAN_AUTO:
1106 printf_filtered (_("Support for the `%s' packet "
1107 "is auto-detected, currently %s.\n"),
1108 config->name, support);
1110 case AUTO_BOOLEAN_TRUE:
1111 case AUTO_BOOLEAN_FALSE:
1112 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1113 config->name, support);
1119 add_packet_config_cmd (struct packet_config *config, const char *name,
1120 const char *title, int legacy)
1126 config->name = name;
1127 config->title = title;
1128 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1130 show_doc = xstrprintf ("Show current use of remote "
1131 "protocol `%s' (%s) packet",
1133 /* set/show TITLE-packet {auto,on,off} */
1134 cmd_name = xstrprintf ("%s-packet", title);
1135 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1136 &config->detect, set_doc,
1137 show_doc, NULL, /* help_doc */
1139 show_remote_protocol_packet_cmd,
1140 &remote_set_cmdlist, &remote_show_cmdlist);
1141 /* The command code copies the documentation strings. */
1144 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1149 legacy_name = xstrprintf ("%s-packet", name);
1150 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1151 &remote_set_cmdlist);
1152 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1153 &remote_show_cmdlist);
1157 static enum packet_result
1158 packet_check_result (const char *buf)
1162 /* The stub recognized the packet request. Check that the
1163 operation succeeded. */
1165 && isxdigit (buf[1]) && isxdigit (buf[2])
1167 /* "Enn" - definitly an error. */
1168 return PACKET_ERROR;
1170 /* Always treat "E." as an error. This will be used for
1171 more verbose error messages, such as E.memtypes. */
1172 if (buf[0] == 'E' && buf[1] == '.')
1173 return PACKET_ERROR;
1175 /* The packet may or may not be OK. Just assume it is. */
1179 /* The stub does not support the packet. */
1180 return PACKET_UNKNOWN;
1183 static enum packet_result
1184 packet_ok (const char *buf, struct packet_config *config)
1186 enum packet_result result;
1188 if (config->detect != AUTO_BOOLEAN_TRUE
1189 && config->support == PACKET_DISABLE)
1190 internal_error (__FILE__, __LINE__,
1191 _("packet_ok: attempt to use a disabled packet"));
1193 result = packet_check_result (buf);
1198 /* The stub recognized the packet request. */
1199 if (config->support == PACKET_SUPPORT_UNKNOWN)
1202 fprintf_unfiltered (gdb_stdlog,
1203 "Packet %s (%s) is supported\n",
1204 config->name, config->title);
1205 config->support = PACKET_ENABLE;
1208 case PACKET_UNKNOWN:
1209 /* The stub does not support the packet. */
1210 if (config->detect == AUTO_BOOLEAN_AUTO
1211 && config->support == PACKET_ENABLE)
1213 /* If the stub previously indicated that the packet was
1214 supported then there is a protocol error. */
1215 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1216 config->name, config->title);
1218 else if (config->detect == AUTO_BOOLEAN_TRUE)
1220 /* The user set it wrong. */
1221 error (_("Enabled packet %s (%s) not recognized by stub"),
1222 config->name, config->title);
1226 fprintf_unfiltered (gdb_stdlog,
1227 "Packet %s (%s) is NOT supported\n",
1228 config->name, config->title);
1229 config->support = PACKET_DISABLE;
1249 PACKET_vFile_pwrite,
1251 PACKET_vFile_unlink,
1252 PACKET_vFile_readlink,
1254 PACKET_qXfer_features,
1255 PACKET_qXfer_libraries,
1256 PACKET_qXfer_libraries_svr4,
1257 PACKET_qXfer_memory_map,
1258 PACKET_qXfer_spu_read,
1259 PACKET_qXfer_spu_write,
1260 PACKET_qXfer_osdata,
1261 PACKET_qXfer_threads,
1262 PACKET_qXfer_statictrace_read,
1263 PACKET_qXfer_traceframe_info,
1269 PACKET_QPassSignals,
1270 PACKET_QProgramSignals,
1272 PACKET_qSearch_memory,
1275 PACKET_QStartNoAckMode,
1277 PACKET_qXfer_siginfo_read,
1278 PACKET_qXfer_siginfo_write,
1281 /* Support for conditional tracepoints. */
1282 PACKET_ConditionalTracepoints,
1284 /* Support for target-side breakpoint conditions. */
1285 PACKET_ConditionalBreakpoints,
1287 /* Support for target-side breakpoint commands. */
1288 PACKET_BreakpointCommands,
1290 /* Support for fast tracepoints. */
1291 PACKET_FastTracepoints,
1293 /* Support for static tracepoints. */
1294 PACKET_StaticTracepoints,
1296 /* Support for installing tracepoints while a trace experiment is
1298 PACKET_InstallInTrace,
1302 PACKET_TracepointSource,
1305 PACKET_QDisableRandomization,
1307 PACKET_QTBuffer_size,
1310 PACKET_qXfer_btrace,
1312 /* Support for the QNonStop packet. */
1315 /* Support for multi-process extensions. */
1316 PACKET_multiprocess_feature,
1318 /* Support for enabling and disabling tracepoints while a trace
1319 experiment is running. */
1320 PACKET_EnableDisableTracepoints_feature,
1322 /* Support for collecting strings using the tracenz bytecode. */
1323 PACKET_tracenz_feature,
1325 /* Support for continuing to run a trace experiment while GDB is
1327 PACKET_DisconnectedTracing_feature,
1329 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1330 PACKET_augmented_libraries_svr4_read_feature,
1335 static struct packet_config remote_protocol_packets[PACKET_MAX];
1337 /* Returns whether a given packet or feature is supported. This takes
1338 into account the state of the corresponding "set remote foo-packet"
1339 command, which may be used to bypass auto-detection. */
1341 static enum packet_support
1342 packet_config_support (struct packet_config *config)
1344 switch (config->detect)
1346 case AUTO_BOOLEAN_TRUE:
1347 return PACKET_ENABLE;
1348 case AUTO_BOOLEAN_FALSE:
1349 return PACKET_DISABLE;
1350 case AUTO_BOOLEAN_AUTO:
1351 return config->support;
1353 gdb_assert_not_reached (_("bad switch"));
1357 /* Same as packet_config_support, but takes the packet's enum value as
1360 static enum packet_support
1361 packet_support (int packet)
1363 struct packet_config *config = &remote_protocol_packets[packet];
1365 return packet_config_support (config);
1369 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1370 struct cmd_list_element *c,
1373 struct packet_config *packet;
1375 for (packet = remote_protocol_packets;
1376 packet < &remote_protocol_packets[PACKET_MAX];
1379 if (&packet->detect == c->var)
1381 show_packet_config_cmd (packet);
1385 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1389 /* Should we try one of the 'Z' requests? */
1393 Z_PACKET_SOFTWARE_BP,
1394 Z_PACKET_HARDWARE_BP,
1401 /* For compatibility with older distributions. Provide a ``set remote
1402 Z-packet ...'' command that updates all the Z packet types. */
1404 static enum auto_boolean remote_Z_packet_detect;
1407 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1408 struct cmd_list_element *c)
1412 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1413 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1417 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1418 struct cmd_list_element *c,
1423 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1425 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1429 /* Returns true if the multi-process extensions are in effect. */
1432 remote_multi_process_p (struct remote_state *rs)
1434 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1437 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1438 static struct async_signal_handler *async_sigint_remote_twice_token;
1439 static struct async_signal_handler *async_sigint_remote_token;
1442 /* Asynchronous signal handle registered as event loop source for
1443 when we have pending events ready to be passed to the core. */
1445 static struct async_event_handler *remote_async_inferior_event_token;
1449 static ptid_t magic_null_ptid;
1450 static ptid_t not_sent_ptid;
1451 static ptid_t any_thread_ptid;
1453 /* Find out if the stub attached to PID (and hence GDB should offer to
1454 detach instead of killing it when bailing out). */
1457 remote_query_attached (int pid)
1459 struct remote_state *rs = get_remote_state ();
1460 size_t size = get_remote_packet_size ();
1462 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
1465 if (remote_multi_process_p (rs))
1466 xsnprintf (rs->buf, size, "qAttached:%x", pid);
1468 xsnprintf (rs->buf, size, "qAttached");
1471 getpkt (&rs->buf, &rs->buf_size, 0);
1473 switch (packet_ok (rs->buf,
1474 &remote_protocol_packets[PACKET_qAttached]))
1477 if (strcmp (rs->buf, "1") == 0)
1481 warning (_("Remote failure reply: %s"), rs->buf);
1483 case PACKET_UNKNOWN:
1490 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1491 has been invented by GDB, instead of reported by the target. Since
1492 we can be connected to a remote system before before knowing about
1493 any inferior, mark the target with execution when we find the first
1494 inferior. If ATTACHED is 1, then we had just attached to this
1495 inferior. If it is 0, then we just created this inferior. If it
1496 is -1, then try querying the remote stub to find out if it had
1497 attached to the inferior or not. */
1499 static struct inferior *
1500 remote_add_inferior (int fake_pid_p, int pid, int attached)
1502 struct inferior *inf;
1504 /* Check whether this process we're learning about is to be
1505 considered attached, or if is to be considered to have been
1506 spawned by the stub. */
1508 attached = remote_query_attached (pid);
1510 if (gdbarch_has_global_solist (target_gdbarch ()))
1512 /* If the target shares code across all inferiors, then every
1513 attach adds a new inferior. */
1514 inf = add_inferior (pid);
1516 /* ... and every inferior is bound to the same program space.
1517 However, each inferior may still have its own address
1519 inf->aspace = maybe_new_address_space ();
1520 inf->pspace = current_program_space;
1524 /* In the traditional debugging scenario, there's a 1-1 match
1525 between program/address spaces. We simply bind the inferior
1526 to the program space's address space. */
1527 inf = current_inferior ();
1528 inferior_appeared (inf, pid);
1531 inf->attach_flag = attached;
1532 inf->fake_pid_p = fake_pid_p;
1537 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1538 according to RUNNING. */
1541 remote_add_thread (ptid_t ptid, int running)
1543 struct remote_state *rs = get_remote_state ();
1545 /* GDB historically didn't pull threads in the initial connection
1546 setup. If the remote target doesn't even have a concept of
1547 threads (e.g., a bare-metal target), even if internally we
1548 consider that a single-threaded target, mentioning a new thread
1549 might be confusing to the user. Be silent then, preserving the
1550 age old behavior. */
1551 if (rs->starting_up)
1552 add_thread_silent (ptid);
1556 set_executing (ptid, running);
1557 set_running (ptid, running);
1560 /* Come here when we learn about a thread id from the remote target.
1561 It may be the first time we hear about such thread, so take the
1562 opportunity to add it to GDB's thread list. In case this is the
1563 first time we're noticing its corresponding inferior, add it to
1564 GDB's inferior list as well. */
1567 remote_notice_new_inferior (ptid_t currthread, int running)
1569 /* If this is a new thread, add it to GDB's thread list.
1570 If we leave it up to WFI to do this, bad things will happen. */
1572 if (in_thread_list (currthread) && is_exited (currthread))
1574 /* We're seeing an event on a thread id we knew had exited.
1575 This has to be a new thread reusing the old id. Add it. */
1576 remote_add_thread (currthread, running);
1580 if (!in_thread_list (currthread))
1582 struct inferior *inf = NULL;
1583 int pid = ptid_get_pid (currthread);
1585 if (ptid_is_pid (inferior_ptid)
1586 && pid == ptid_get_pid (inferior_ptid))
1588 /* inferior_ptid has no thread member yet. This can happen
1589 with the vAttach -> remote_wait,"TAAthread:" path if the
1590 stub doesn't support qC. This is the first stop reported
1591 after an attach, so this is the main thread. Update the
1592 ptid in the thread list. */
1593 if (in_thread_list (pid_to_ptid (pid)))
1594 thread_change_ptid (inferior_ptid, currthread);
1597 remote_add_thread (currthread, running);
1598 inferior_ptid = currthread;
1603 if (ptid_equal (magic_null_ptid, inferior_ptid))
1605 /* inferior_ptid is not set yet. This can happen with the
1606 vRun -> remote_wait,"TAAthread:" path if the stub
1607 doesn't support qC. This is the first stop reported
1608 after an attach, so this is the main thread. Update the
1609 ptid in the thread list. */
1610 thread_change_ptid (inferior_ptid, currthread);
1614 /* When connecting to a target remote, or to a target
1615 extended-remote which already was debugging an inferior, we
1616 may not know about it yet. Add it before adding its child
1617 thread, so notifications are emitted in a sensible order. */
1618 if (!in_inferior_list (ptid_get_pid (currthread)))
1620 struct remote_state *rs = get_remote_state ();
1621 int fake_pid_p = !remote_multi_process_p (rs);
1623 inf = remote_add_inferior (fake_pid_p,
1624 ptid_get_pid (currthread), -1);
1627 /* This is really a new thread. Add it. */
1628 remote_add_thread (currthread, running);
1630 /* If we found a new inferior, let the common code do whatever
1631 it needs to with it (e.g., read shared libraries, insert
1632 breakpoints), unless we're just setting up an all-stop
1636 struct remote_state *rs = get_remote_state ();
1638 if (non_stop || !rs->starting_up)
1639 notice_new_inferior (currthread, running, 0);
1644 /* Return the private thread data, creating it if necessary. */
1646 static struct private_thread_info *
1647 demand_private_info (ptid_t ptid)
1649 struct thread_info *info = find_thread_ptid (ptid);
1655 info->private = xmalloc (sizeof (*(info->private)));
1656 info->private_dtor = free_private_thread_info;
1657 info->private->core = -1;
1658 info->private->extra = 0;
1661 return info->private;
1664 /* Call this function as a result of
1665 1) A halt indication (T packet) containing a thread id
1666 2) A direct query of currthread
1667 3) Successful execution of set thread */
1670 record_currthread (struct remote_state *rs, ptid_t currthread)
1672 rs->general_thread = currthread;
1675 /* If 'QPassSignals' is supported, tell the remote stub what signals
1676 it can simply pass through to the inferior without reporting. */
1679 remote_pass_signals (struct target_ops *self,
1680 int numsigs, unsigned char *pass_signals)
1682 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
1684 char *pass_packet, *p;
1686 struct remote_state *rs = get_remote_state ();
1688 gdb_assert (numsigs < 256);
1689 for (i = 0; i < numsigs; i++)
1691 if (pass_signals[i])
1694 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1695 strcpy (pass_packet, "QPassSignals:");
1696 p = pass_packet + strlen (pass_packet);
1697 for (i = 0; i < numsigs; i++)
1699 if (pass_signals[i])
1702 *p++ = tohex (i >> 4);
1703 *p++ = tohex (i & 15);
1712 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
1714 putpkt (pass_packet);
1715 getpkt (&rs->buf, &rs->buf_size, 0);
1716 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
1717 if (rs->last_pass_packet)
1718 xfree (rs->last_pass_packet);
1719 rs->last_pass_packet = pass_packet;
1722 xfree (pass_packet);
1726 /* If 'QProgramSignals' is supported, tell the remote stub what
1727 signals it should pass through to the inferior when detaching. */
1730 remote_program_signals (struct target_ops *self,
1731 int numsigs, unsigned char *signals)
1733 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
1737 struct remote_state *rs = get_remote_state ();
1739 gdb_assert (numsigs < 256);
1740 for (i = 0; i < numsigs; i++)
1745 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1746 strcpy (packet, "QProgramSignals:");
1747 p = packet + strlen (packet);
1748 for (i = 0; i < numsigs; i++)
1750 if (signal_pass_state (i))
1753 *p++ = tohex (i >> 4);
1754 *p++ = tohex (i & 15);
1763 if (!rs->last_program_signals_packet
1764 || strcmp (rs->last_program_signals_packet, packet) != 0)
1767 getpkt (&rs->buf, &rs->buf_size, 0);
1768 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1769 xfree (rs->last_program_signals_packet);
1770 rs->last_program_signals_packet = packet;
1777 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1778 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1779 thread. If GEN is set, set the general thread, if not, then set
1780 the step/continue thread. */
1782 set_thread (struct ptid ptid, int gen)
1784 struct remote_state *rs = get_remote_state ();
1785 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
1786 char *buf = rs->buf;
1787 char *endbuf = rs->buf + get_remote_packet_size ();
1789 if (ptid_equal (state, ptid))
1793 *buf++ = gen ? 'g' : 'c';
1794 if (ptid_equal (ptid, magic_null_ptid))
1795 xsnprintf (buf, endbuf - buf, "0");
1796 else if (ptid_equal (ptid, any_thread_ptid))
1797 xsnprintf (buf, endbuf - buf, "0");
1798 else if (ptid_equal (ptid, minus_one_ptid))
1799 xsnprintf (buf, endbuf - buf, "-1");
1801 write_ptid (buf, endbuf, ptid);
1803 getpkt (&rs->buf, &rs->buf_size, 0);
1805 rs->general_thread = ptid;
1807 rs->continue_thread = ptid;
1811 set_general_thread (struct ptid ptid)
1813 set_thread (ptid, 1);
1817 set_continue_thread (struct ptid ptid)
1819 set_thread (ptid, 0);
1822 /* Change the remote current process. Which thread within the process
1823 ends up selected isn't important, as long as it is the same process
1824 as what INFERIOR_PTID points to.
1826 This comes from that fact that there is no explicit notion of
1827 "selected process" in the protocol. The selected process for
1828 general operations is the process the selected general thread
1832 set_general_process (void)
1834 struct remote_state *rs = get_remote_state ();
1836 /* If the remote can't handle multiple processes, don't bother. */
1837 if (!rs->extended || !remote_multi_process_p (rs))
1840 /* We only need to change the remote current thread if it's pointing
1841 at some other process. */
1842 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
1843 set_general_thread (inferior_ptid);
1847 /* Return nonzero if the thread PTID is still alive on the remote
1851 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1853 struct remote_state *rs = get_remote_state ();
1856 if (ptid_equal (ptid, magic_null_ptid))
1857 /* The main thread is always alive. */
1860 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
1861 /* The main thread is always alive. This can happen after a
1862 vAttach, if the remote side doesn't support
1867 endp = rs->buf + get_remote_packet_size ();
1870 write_ptid (p, endp, ptid);
1873 getpkt (&rs->buf, &rs->buf_size, 0);
1874 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
1877 /* About these extended threadlist and threadinfo packets. They are
1878 variable length packets but, the fields within them are often fixed
1879 length. They are redundent enough to send over UDP as is the
1880 remote protocol in general. There is a matching unit test module
1883 /* WARNING: This threadref data structure comes from the remote O.S.,
1884 libstub protocol encoding, and remote.c. It is not particularly
1887 /* Right now, the internal structure is int. We want it to be bigger.
1888 Plan to fix this. */
1890 typedef int gdb_threadref; /* Internal GDB thread reference. */
1892 /* gdb_ext_thread_info is an internal GDB data structure which is
1893 equivalent to the reply of the remote threadinfo packet. */
1895 struct gdb_ext_thread_info
1897 threadref threadid; /* External form of thread reference. */
1898 int active; /* Has state interesting to GDB?
1900 char display[256]; /* Brief state display, name,
1901 blocked/suspended. */
1902 char shortname[32]; /* To be used to name threads. */
1903 char more_display[256]; /* Long info, statistics, queue depth,
1907 /* The volume of remote transfers can be limited by submitting
1908 a mask containing bits specifying the desired information.
1909 Use a union of these values as the 'selection' parameter to
1910 get_thread_info. FIXME: Make these TAG names more thread specific. */
1912 #define TAG_THREADID 1
1913 #define TAG_EXISTS 2
1914 #define TAG_DISPLAY 4
1915 #define TAG_THREADNAME 8
1916 #define TAG_MOREDISPLAY 16
1918 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1920 static char *unpack_nibble (char *buf, int *val);
1922 static char *unpack_byte (char *buf, int *value);
1924 static char *pack_int (char *buf, int value);
1926 static char *unpack_int (char *buf, int *value);
1928 static char *unpack_string (char *src, char *dest, int length);
1930 static char *pack_threadid (char *pkt, threadref *id);
1932 static char *unpack_threadid (char *inbuf, threadref *id);
1934 void int_to_threadref (threadref *id, int value);
1936 static int threadref_to_int (threadref *ref);
1938 static void copy_threadref (threadref *dest, threadref *src);
1940 static int threadmatch (threadref *dest, threadref *src);
1942 static char *pack_threadinfo_request (char *pkt, int mode,
1945 static int remote_unpack_thread_info_response (char *pkt,
1946 threadref *expectedref,
1947 struct gdb_ext_thread_info
1951 static int remote_get_threadinfo (threadref *threadid,
1952 int fieldset, /*TAG mask */
1953 struct gdb_ext_thread_info *info);
1955 static char *pack_threadlist_request (char *pkt, int startflag,
1957 threadref *nextthread);
1959 static int parse_threadlist_response (char *pkt,
1961 threadref *original_echo,
1962 threadref *resultlist,
1965 static int remote_get_threadlist (int startflag,
1966 threadref *nextthread,
1970 threadref *threadlist);
1972 typedef int (*rmt_thread_action) (threadref *ref, void *context);
1974 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1975 void *context, int looplimit);
1977 static int remote_newthread_step (threadref *ref, void *context);
1980 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1981 buffer we're allowed to write to. Returns
1982 BUF+CHARACTERS_WRITTEN. */
1985 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
1988 struct remote_state *rs = get_remote_state ();
1990 if (remote_multi_process_p (rs))
1992 pid = ptid_get_pid (ptid);
1994 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
1996 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
1998 tid = ptid_get_lwp (ptid);
2000 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2002 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2007 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2008 passed the last parsed char. Returns null_ptid on error. */
2011 read_ptid (char *buf, char **obuf)
2015 ULONGEST pid = 0, tid = 0;
2019 /* Multi-process ptid. */
2020 pp = unpack_varlen_hex (p + 1, &pid);
2022 error (_("invalid remote ptid: %s"), p);
2025 pp = unpack_varlen_hex (p + 1, &tid);
2028 return ptid_build (pid, tid, 0);
2031 /* No multi-process. Just a tid. */
2032 pp = unpack_varlen_hex (p, &tid);
2034 /* Since the stub is not sending a process id, then default to
2035 what's in inferior_ptid, unless it's null at this point. If so,
2036 then since there's no way to know the pid of the reported
2037 threads, use the magic number. */
2038 if (ptid_equal (inferior_ptid, null_ptid))
2039 pid = ptid_get_pid (magic_null_ptid);
2041 pid = ptid_get_pid (inferior_ptid);
2045 return ptid_build (pid, tid, 0);
2051 if (ch >= 'a' && ch <= 'f')
2052 return ch - 'a' + 10;
2053 if (ch >= '0' && ch <= '9')
2055 if (ch >= 'A' && ch <= 'F')
2056 return ch - 'A' + 10;
2061 stub_unpack_int (char *buff, int fieldlength)
2068 nibble = stubhex (*buff++);
2072 retval = retval << 4;
2078 unpack_nibble (char *buf, int *val)
2080 *val = fromhex (*buf++);
2085 unpack_byte (char *buf, int *value)
2087 *value = stub_unpack_int (buf, 2);
2092 pack_int (char *buf, int value)
2094 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2095 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2096 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2097 buf = pack_hex_byte (buf, (value & 0xff));
2102 unpack_int (char *buf, int *value)
2104 *value = stub_unpack_int (buf, 8);
2108 #if 0 /* Currently unused, uncomment when needed. */
2109 static char *pack_string (char *pkt, char *string);
2112 pack_string (char *pkt, char *string)
2117 len = strlen (string);
2119 len = 200; /* Bigger than most GDB packets, junk??? */
2120 pkt = pack_hex_byte (pkt, len);
2124 if ((ch == '\0') || (ch == '#'))
2125 ch = '*'; /* Protect encapsulation. */
2130 #endif /* 0 (unused) */
2133 unpack_string (char *src, char *dest, int length)
2142 pack_threadid (char *pkt, threadref *id)
2145 unsigned char *altid;
2147 altid = (unsigned char *) id;
2148 limit = pkt + BUF_THREAD_ID_SIZE;
2150 pkt = pack_hex_byte (pkt, *altid++);
2156 unpack_threadid (char *inbuf, threadref *id)
2159 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2162 altref = (char *) id;
2164 while (inbuf < limit)
2166 x = stubhex (*inbuf++);
2167 y = stubhex (*inbuf++);
2168 *altref++ = (x << 4) | y;
2173 /* Externally, threadrefs are 64 bits but internally, they are still
2174 ints. This is due to a mismatch of specifications. We would like
2175 to use 64bit thread references internally. This is an adapter
2179 int_to_threadref (threadref *id, int value)
2181 unsigned char *scan;
2183 scan = (unsigned char *) id;
2189 *scan++ = (value >> 24) & 0xff;
2190 *scan++ = (value >> 16) & 0xff;
2191 *scan++ = (value >> 8) & 0xff;
2192 *scan++ = (value & 0xff);
2196 threadref_to_int (threadref *ref)
2199 unsigned char *scan;
2205 value = (value << 8) | ((*scan++) & 0xff);
2210 copy_threadref (threadref *dest, threadref *src)
2213 unsigned char *csrc, *cdest;
2215 csrc = (unsigned char *) src;
2216 cdest = (unsigned char *) dest;
2223 threadmatch (threadref *dest, threadref *src)
2225 /* Things are broken right now, so just assume we got a match. */
2227 unsigned char *srcp, *destp;
2229 srcp = (char *) src;
2230 destp = (char *) dest;
2234 result &= (*srcp++ == *destp++) ? 1 : 0;
2241 threadid:1, # always request threadid
2248 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2251 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2253 *pkt++ = 'q'; /* Info Query */
2254 *pkt++ = 'P'; /* process or thread info */
2255 pkt = pack_int (pkt, mode); /* mode */
2256 pkt = pack_threadid (pkt, id); /* threadid */
2257 *pkt = '\0'; /* terminate */
2261 /* These values tag the fields in a thread info response packet. */
2262 /* Tagging the fields allows us to request specific fields and to
2263 add more fields as time goes by. */
2265 #define TAG_THREADID 1 /* Echo the thread identifier. */
2266 #define TAG_EXISTS 2 /* Is this process defined enough to
2267 fetch registers and its stack? */
2268 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2269 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2270 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2274 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2275 struct gdb_ext_thread_info *info)
2277 struct remote_state *rs = get_remote_state ();
2281 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2284 /* info->threadid = 0; FIXME: implement zero_threadref. */
2286 info->display[0] = '\0';
2287 info->shortname[0] = '\0';
2288 info->more_display[0] = '\0';
2290 /* Assume the characters indicating the packet type have been
2292 pkt = unpack_int (pkt, &mask); /* arg mask */
2293 pkt = unpack_threadid (pkt, &ref);
2296 warning (_("Incomplete response to threadinfo request."));
2297 if (!threadmatch (&ref, expectedref))
2298 { /* This is an answer to a different request. */
2299 warning (_("ERROR RMT Thread info mismatch."));
2302 copy_threadref (&info->threadid, &ref);
2304 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2306 /* Packets are terminated with nulls. */
2307 while ((pkt < limit) && mask && *pkt)
2309 pkt = unpack_int (pkt, &tag); /* tag */
2310 pkt = unpack_byte (pkt, &length); /* length */
2311 if (!(tag & mask)) /* Tags out of synch with mask. */
2313 warning (_("ERROR RMT: threadinfo tag mismatch."));
2317 if (tag == TAG_THREADID)
2321 warning (_("ERROR RMT: length of threadid is not 16."));
2325 pkt = unpack_threadid (pkt, &ref);
2326 mask = mask & ~TAG_THREADID;
2329 if (tag == TAG_EXISTS)
2331 info->active = stub_unpack_int (pkt, length);
2333 mask = mask & ~(TAG_EXISTS);
2336 warning (_("ERROR RMT: 'exists' length too long."));
2342 if (tag == TAG_THREADNAME)
2344 pkt = unpack_string (pkt, &info->shortname[0], length);
2345 mask = mask & ~TAG_THREADNAME;
2348 if (tag == TAG_DISPLAY)
2350 pkt = unpack_string (pkt, &info->display[0], length);
2351 mask = mask & ~TAG_DISPLAY;
2354 if (tag == TAG_MOREDISPLAY)
2356 pkt = unpack_string (pkt, &info->more_display[0], length);
2357 mask = mask & ~TAG_MOREDISPLAY;
2360 warning (_("ERROR RMT: unknown thread info tag."));
2361 break; /* Not a tag we know about. */
2367 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2368 struct gdb_ext_thread_info *info)
2370 struct remote_state *rs = get_remote_state ();
2373 pack_threadinfo_request (rs->buf, fieldset, threadid);
2375 getpkt (&rs->buf, &rs->buf_size, 0);
2377 if (rs->buf[0] == '\0')
2380 result = remote_unpack_thread_info_response (rs->buf + 2,
2385 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2388 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2389 threadref *nextthread)
2391 *pkt++ = 'q'; /* info query packet */
2392 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2393 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2394 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2395 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2400 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2403 parse_threadlist_response (char *pkt, int result_limit,
2404 threadref *original_echo, threadref *resultlist,
2407 struct remote_state *rs = get_remote_state ();
2409 int count, resultcount, done;
2412 /* Assume the 'q' and 'M chars have been stripped. */
2413 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2414 /* done parse past here */
2415 pkt = unpack_byte (pkt, &count); /* count field */
2416 pkt = unpack_nibble (pkt, &done);
2417 /* The first threadid is the argument threadid. */
2418 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2419 while ((count-- > 0) && (pkt < limit))
2421 pkt = unpack_threadid (pkt, resultlist++);
2422 if (resultcount++ >= result_limit)
2431 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2432 int *done, int *result_count, threadref *threadlist)
2434 struct remote_state *rs = get_remote_state ();
2437 /* Trancate result limit to be smaller than the packet size. */
2438 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2439 >= get_remote_packet_size ())
2440 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2442 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2444 getpkt (&rs->buf, &rs->buf_size, 0);
2446 if (*rs->buf == '\0')
2450 parse_threadlist_response (rs->buf + 2, result_limit,
2451 &rs->echo_nextthread, threadlist, done);
2453 if (!threadmatch (&rs->echo_nextthread, nextthread))
2455 /* FIXME: This is a good reason to drop the packet. */
2456 /* Possably, there is a duplicate response. */
2458 retransmit immediatly - race conditions
2459 retransmit after timeout - yes
2461 wait for packet, then exit
2463 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2464 return 0; /* I choose simply exiting. */
2466 if (*result_count <= 0)
2470 warning (_("RMT ERROR : failed to get remote thread list."));
2473 return result; /* break; */
2475 if (*result_count > result_limit)
2478 warning (_("RMT ERROR: threadlist response longer than requested."));
2484 /* This is the interface between remote and threads, remotes upper
2487 /* remote_find_new_threads retrieves the thread list and for each
2488 thread in the list, looks up the thread in GDB's internal list,
2489 adding the thread if it does not already exist. This involves
2490 getting partial thread lists from the remote target so, polling the
2491 quit_flag is required. */
2495 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2498 struct remote_state *rs = get_remote_state ();
2499 int done, i, result_count;
2507 if (loopcount++ > looplimit)
2510 warning (_("Remote fetch threadlist -infinite loop-."));
2513 if (!remote_get_threadlist (startflag, &rs->nextthread,
2514 MAXTHREADLISTRESULTS,
2515 &done, &result_count, rs->resultthreadlist))
2520 /* Clear for later iterations. */
2522 /* Setup to resume next batch of thread references, set nextthread. */
2523 if (result_count >= 1)
2524 copy_threadref (&rs->nextthread,
2525 &rs->resultthreadlist[result_count - 1]);
2527 while (result_count--)
2528 if (!(result = (*stepfunction) (&rs->resultthreadlist[i++], context)))
2535 remote_newthread_step (threadref *ref, void *context)
2537 int pid = ptid_get_pid (inferior_ptid);
2538 ptid_t ptid = ptid_build (pid, threadref_to_int (ref), 0);
2540 if (!in_thread_list (ptid))
2542 return 1; /* continue iterator */
2545 #define CRAZY_MAX_THREADS 1000
2548 remote_current_thread (ptid_t oldpid)
2550 struct remote_state *rs = get_remote_state ();
2553 getpkt (&rs->buf, &rs->buf_size, 0);
2554 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2555 return read_ptid (&rs->buf[2], NULL);
2560 /* Find new threads for info threads command.
2561 * Original version, using John Metzler's thread protocol.
2565 remote_find_new_threads (void)
2567 remote_threadlist_iterator (remote_newthread_step, 0,
2571 #if defined(HAVE_LIBEXPAT)
2573 typedef struct thread_item
2579 DEF_VEC_O(thread_item_t);
2581 struct threads_parsing_context
2583 VEC (thread_item_t) *items;
2587 start_thread (struct gdb_xml_parser *parser,
2588 const struct gdb_xml_element *element,
2589 void *user_data, VEC(gdb_xml_value_s) *attributes)
2591 struct threads_parsing_context *data = user_data;
2593 struct thread_item item;
2595 struct gdb_xml_value *attr;
2597 id = xml_find_attribute (attributes, "id")->value;
2598 item.ptid = read_ptid (id, NULL);
2600 attr = xml_find_attribute (attributes, "core");
2602 item.core = *(ULONGEST *) attr->value;
2608 VEC_safe_push (thread_item_t, data->items, &item);
2612 end_thread (struct gdb_xml_parser *parser,
2613 const struct gdb_xml_element *element,
2614 void *user_data, const char *body_text)
2616 struct threads_parsing_context *data = user_data;
2618 if (body_text && *body_text)
2619 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2622 const struct gdb_xml_attribute thread_attributes[] = {
2623 { "id", GDB_XML_AF_NONE, NULL, NULL },
2624 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2625 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2628 const struct gdb_xml_element thread_children[] = {
2629 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2632 const struct gdb_xml_element threads_children[] = {
2633 { "thread", thread_attributes, thread_children,
2634 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2635 start_thread, end_thread },
2636 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2639 const struct gdb_xml_element threads_elements[] = {
2640 { "threads", NULL, threads_children,
2641 GDB_XML_EF_NONE, NULL, NULL },
2642 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2645 /* Discard the contents of the constructed thread info context. */
2648 clear_threads_parsing_context (void *p)
2650 struct threads_parsing_context *context = p;
2652 struct thread_item *item;
2654 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2655 xfree (item->extra);
2657 VEC_free (thread_item_t, context->items);
2663 * Find all threads for info threads command.
2664 * Uses new thread protocol contributed by Cisco.
2665 * Falls back and attempts to use the older method (above)
2666 * if the target doesn't respond to the new method.
2670 remote_threads_info (struct target_ops *ops)
2672 struct remote_state *rs = get_remote_state ();
2676 if (rs->remote_desc == 0) /* paranoia */
2677 error (_("Command can only be used when connected to the remote target."));
2679 #if defined(HAVE_LIBEXPAT)
2680 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2682 char *xml = target_read_stralloc (¤t_target,
2683 TARGET_OBJECT_THREADS, NULL);
2685 struct cleanup *back_to = make_cleanup (xfree, xml);
2689 struct threads_parsing_context context;
2691 context.items = NULL;
2692 make_cleanup (clear_threads_parsing_context, &context);
2694 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2695 threads_elements, xml, &context) == 0)
2698 struct thread_item *item;
2701 VEC_iterate (thread_item_t, context.items, i, item);
2704 if (!ptid_equal (item->ptid, null_ptid))
2706 struct private_thread_info *info;
2707 /* In non-stop mode, we assume new found threads
2708 are running until proven otherwise with a
2709 stop reply. In all-stop, we can only get
2710 here if all threads are stopped. */
2711 int running = non_stop ? 1 : 0;
2713 remote_notice_new_inferior (item->ptid, running);
2715 info = demand_private_info (item->ptid);
2716 info->core = item->core;
2717 info->extra = item->extra;
2724 do_cleanups (back_to);
2729 if (rs->use_threadinfo_query)
2731 putpkt ("qfThreadInfo");
2732 getpkt (&rs->buf, &rs->buf_size, 0);
2734 if (bufp[0] != '\0') /* q packet recognized */
2736 struct cleanup *old_chain;
2739 /* remote_notice_new_inferior (in the loop below) may make
2740 new RSP calls, which clobber rs->buf. Work with a
2742 bufp = saved_reply = xstrdup (rs->buf);
2743 old_chain = make_cleanup (free_current_contents, &saved_reply);
2745 while (*bufp++ == 'm') /* reply contains one or more TID */
2749 new_thread = read_ptid (bufp, &bufp);
2750 if (!ptid_equal (new_thread, null_ptid))
2752 /* In non-stop mode, we assume new found threads
2753 are running until proven otherwise with a
2754 stop reply. In all-stop, we can only get
2755 here if all threads are stopped. */
2756 int running = non_stop ? 1 : 0;
2758 remote_notice_new_inferior (new_thread, running);
2761 while (*bufp++ == ','); /* comma-separated list */
2762 free_current_contents (&saved_reply);
2763 putpkt ("qsThreadInfo");
2764 getpkt (&rs->buf, &rs->buf_size, 0);
2765 bufp = saved_reply = xstrdup (rs->buf);
2767 do_cleanups (old_chain);
2772 /* Only qfThreadInfo is supported in non-stop mode. */
2776 /* Else fall back to old method based on jmetzler protocol. */
2777 rs->use_threadinfo_query = 0;
2778 remote_find_new_threads ();
2783 * Collect a descriptive string about the given thread.
2784 * The target may say anything it wants to about the thread
2785 * (typically info about its blocked / runnable state, name, etc.).
2786 * This string will appear in the info threads display.
2788 * Optional: targets are not required to implement this function.
2792 remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
2794 struct remote_state *rs = get_remote_state ();
2798 struct gdb_ext_thread_info threadinfo;
2799 static char display_buf[100]; /* arbitrary... */
2800 int n = 0; /* position in display_buf */
2802 if (rs->remote_desc == 0) /* paranoia */
2803 internal_error (__FILE__, __LINE__,
2804 _("remote_threads_extra_info"));
2806 if (ptid_equal (tp->ptid, magic_null_ptid)
2807 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
2808 /* This is the main thread which was added by GDB. The remote
2809 server doesn't know about it. */
2812 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2814 struct thread_info *info = find_thread_ptid (tp->ptid);
2816 if (info && info->private)
2817 return info->private->extra;
2822 if (rs->use_threadextra_query)
2825 char *endb = rs->buf + get_remote_packet_size ();
2827 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2829 write_ptid (b, endb, tp->ptid);
2832 getpkt (&rs->buf, &rs->buf_size, 0);
2833 if (rs->buf[0] != 0)
2835 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2836 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
2837 display_buf [result] = '\0';
2842 /* If the above query fails, fall back to the old method. */
2843 rs->use_threadextra_query = 0;
2844 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2845 | TAG_MOREDISPLAY | TAG_DISPLAY;
2846 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
2847 if (remote_get_threadinfo (&id, set, &threadinfo))
2848 if (threadinfo.active)
2850 if (*threadinfo.shortname)
2851 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
2852 " Name: %s,", threadinfo.shortname);
2853 if (*threadinfo.display)
2854 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2855 " State: %s,", threadinfo.display);
2856 if (*threadinfo.more_display)
2857 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2858 " Priority: %s", threadinfo.more_display);
2862 /* For purely cosmetic reasons, clear up trailing commas. */
2863 if (',' == display_buf[n-1])
2864 display_buf[n-1] = ' ';
2873 remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
2874 struct static_tracepoint_marker *marker)
2876 struct remote_state *rs = get_remote_state ();
2879 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
2881 p += hexnumstr (p, addr);
2883 getpkt (&rs->buf, &rs->buf_size, 0);
2887 error (_("Remote failure reply: %s"), p);
2891 parse_static_tracepoint_marker_definition (p, &p, marker);
2898 static VEC(static_tracepoint_marker_p) *
2899 remote_static_tracepoint_markers_by_strid (struct target_ops *self,
2902 struct remote_state *rs = get_remote_state ();
2903 VEC(static_tracepoint_marker_p) *markers = NULL;
2904 struct static_tracepoint_marker *marker = NULL;
2905 struct cleanup *old_chain;
2908 /* Ask for a first packet of static tracepoint marker
2911 getpkt (&rs->buf, &rs->buf_size, 0);
2914 error (_("Remote failure reply: %s"), p);
2916 old_chain = make_cleanup (free_current_marker, &marker);
2921 marker = XCNEW (struct static_tracepoint_marker);
2925 parse_static_tracepoint_marker_definition (p, &p, marker);
2927 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
2929 VEC_safe_push (static_tracepoint_marker_p,
2935 release_static_tracepoint_marker (marker);
2936 memset (marker, 0, sizeof (*marker));
2939 while (*p++ == ','); /* comma-separated list */
2940 /* Ask for another packet of static tracepoint definition. */
2942 getpkt (&rs->buf, &rs->buf_size, 0);
2946 do_cleanups (old_chain);
2951 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2954 remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
2956 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
2960 /* Restart the remote side; this is an extended protocol operation. */
2963 extended_remote_restart (void)
2965 struct remote_state *rs = get_remote_state ();
2967 /* Send the restart command; for reasons I don't understand the
2968 remote side really expects a number after the "R". */
2969 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
2972 remote_fileio_reset ();
2975 /* Clean up connection to a remote debugger. */
2978 remote_close (struct target_ops *self)
2980 struct remote_state *rs = get_remote_state ();
2982 if (rs->remote_desc == NULL)
2983 return; /* already closed */
2985 /* Make sure we leave stdin registered in the event loop, and we
2986 don't leave the async SIGINT signal handler installed. */
2987 remote_terminal_ours (self);
2989 serial_close (rs->remote_desc);
2990 rs->remote_desc = NULL;
2992 /* We don't have a connection to the remote stub anymore. Get rid
2993 of all the inferiors and their threads we were controlling.
2994 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
2995 will be unable to find the thread corresponding to (pid, 0, 0). */
2996 inferior_ptid = null_ptid;
2997 discard_all_inferiors ();
2999 /* We are closing the remote target, so we should discard
3000 everything of this target. */
3001 discard_pending_stop_replies_in_queue (rs);
3003 if (remote_async_inferior_event_token)
3004 delete_async_event_handler (&remote_async_inferior_event_token);
3006 remote_notif_state_xfree (rs->notif_state);
3008 trace_reset_local_state ();
3011 /* Query the remote side for the text, data and bss offsets. */
3016 struct remote_state *rs = get_remote_state ();
3019 int lose, num_segments = 0, do_sections, do_segments;
3020 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
3021 struct section_offsets *offs;
3022 struct symfile_segment_data *data;
3024 if (symfile_objfile == NULL)
3027 putpkt ("qOffsets");
3028 getpkt (&rs->buf, &rs->buf_size, 0);
3031 if (buf[0] == '\000')
3032 return; /* Return silently. Stub doesn't support
3036 warning (_("Remote failure reply: %s"), buf);
3040 /* Pick up each field in turn. This used to be done with scanf, but
3041 scanf will make trouble if CORE_ADDR size doesn't match
3042 conversion directives correctly. The following code will work
3043 with any size of CORE_ADDR. */
3044 text_addr = data_addr = bss_addr = 0;
3048 if (strncmp (ptr, "Text=", 5) == 0)
3051 /* Don't use strtol, could lose on big values. */
3052 while (*ptr && *ptr != ';')
3053 text_addr = (text_addr << 4) + fromhex (*ptr++);
3055 if (strncmp (ptr, ";Data=", 6) == 0)
3058 while (*ptr && *ptr != ';')
3059 data_addr = (data_addr << 4) + fromhex (*ptr++);
3064 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
3067 while (*ptr && *ptr != ';')
3068 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
3070 if (bss_addr != data_addr)
3071 warning (_("Target reported unsupported offsets: %s"), buf);
3076 else if (strncmp (ptr, "TextSeg=", 8) == 0)
3079 /* Don't use strtol, could lose on big values. */
3080 while (*ptr && *ptr != ';')
3081 text_addr = (text_addr << 4) + fromhex (*ptr++);
3084 if (strncmp (ptr, ";DataSeg=", 9) == 0)
3087 while (*ptr && *ptr != ';')
3088 data_addr = (data_addr << 4) + fromhex (*ptr++);
3096 error (_("Malformed response to offset query, %s"), buf);
3097 else if (*ptr != '\0')
3098 warning (_("Target reported unsupported offsets: %s"), buf);
3100 offs = ((struct section_offsets *)
3101 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3102 memcpy (offs, symfile_objfile->section_offsets,
3103 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3105 data = get_symfile_segment_data (symfile_objfile->obfd);
3106 do_segments = (data != NULL);
3107 do_sections = num_segments == 0;
3109 if (num_segments > 0)
3111 segments[0] = text_addr;
3112 segments[1] = data_addr;
3114 /* If we have two segments, we can still try to relocate everything
3115 by assuming that the .text and .data offsets apply to the whole
3116 text and data segments. Convert the offsets given in the packet
3117 to base addresses for symfile_map_offsets_to_segments. */
3118 else if (data && data->num_segments == 2)
3120 segments[0] = data->segment_bases[0] + text_addr;
3121 segments[1] = data->segment_bases[1] + data_addr;
3124 /* If the object file has only one segment, assume that it is text
3125 rather than data; main programs with no writable data are rare,
3126 but programs with no code are useless. Of course the code might
3127 have ended up in the data segment... to detect that we would need
3128 the permissions here. */
3129 else if (data && data->num_segments == 1)
3131 segments[0] = data->segment_bases[0] + text_addr;
3134 /* There's no way to relocate by segment. */
3140 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3141 offs, num_segments, segments);
3143 if (ret == 0 && !do_sections)
3144 error (_("Can not handle qOffsets TextSeg "
3145 "response with this symbol file"));
3152 free_symfile_segment_data (data);
3156 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3158 /* This is a temporary kludge to force data and bss to use the
3159 same offsets because that's what nlmconv does now. The real
3160 solution requires changes to the stub and remote.c that I
3161 don't have time to do right now. */
3163 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3164 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3167 objfile_relocate (symfile_objfile, offs);
3170 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3171 threads we know are stopped already. This is used during the
3172 initial remote connection in non-stop mode --- threads that are
3173 reported as already being stopped are left stopped. */
3176 set_stop_requested_callback (struct thread_info *thread, void *data)
3178 /* If we have a stop reply for this thread, it must be stopped. */
3179 if (peek_stop_reply (thread->ptid))
3180 set_stop_requested (thread->ptid, 1);
3185 /* Send interrupt_sequence to remote target. */
3187 send_interrupt_sequence (void)
3189 struct remote_state *rs = get_remote_state ();
3191 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3192 remote_serial_write ("\x03", 1);
3193 else if (interrupt_sequence_mode == interrupt_sequence_break)
3194 serial_send_break (rs->remote_desc);
3195 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3197 serial_send_break (rs->remote_desc);
3198 remote_serial_write ("g", 1);
3201 internal_error (__FILE__, __LINE__,
3202 _("Invalid value for interrupt_sequence_mode: %s."),
3203 interrupt_sequence_mode);
3207 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3208 and extract the PTID. Returns NULL_PTID if not found. */
3211 stop_reply_extract_thread (char *stop_reply)
3213 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3217 /* Txx r:val ; r:val (...) */
3220 /* Look for "register" named "thread". */
3225 p1 = strchr (p, ':');
3229 if (strncmp (p, "thread", p1 - p) == 0)
3230 return read_ptid (++p1, &p);
3232 p1 = strchr (p, ';');
3244 /* Determine the remote side's current thread. If we have a stop
3245 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3246 "thread" register we can extract the current thread from. If not,
3247 ask the remote which is the current thread with qC. The former
3248 method avoids a roundtrip. */
3251 get_current_thread (char *wait_status)
3255 /* Note we don't use remote_parse_stop_reply as that makes use of
3256 the target architecture, which we haven't yet fully determined at
3258 if (wait_status != NULL)
3259 ptid = stop_reply_extract_thread (wait_status);
3260 if (ptid_equal (ptid, null_ptid))
3261 ptid = remote_current_thread (inferior_ptid);
3266 /* Query the remote target for which is the current thread/process,
3267 add it to our tables, and update INFERIOR_PTID. The caller is
3268 responsible for setting the state such that the remote end is ready
3269 to return the current thread.
3271 This function is called after handling the '?' or 'vRun' packets,
3272 whose response is a stop reply from which we can also try
3273 extracting the thread. If the target doesn't support the explicit
3274 qC query, we infer the current thread from that stop reply, passed
3275 in in WAIT_STATUS, which may be NULL. */
3278 add_current_inferior_and_thread (char *wait_status)
3280 struct remote_state *rs = get_remote_state ();
3282 ptid_t ptid = null_ptid;
3284 inferior_ptid = null_ptid;
3286 /* Now, if we have thread information, update inferior_ptid. */
3287 ptid = get_current_thread (wait_status);
3289 if (!ptid_equal (ptid, null_ptid))
3291 if (!remote_multi_process_p (rs))
3294 inferior_ptid = ptid;
3298 /* Without this, some commands which require an active target
3299 (such as kill) won't work. This variable serves (at least)
3300 double duty as both the pid of the target process (if it has
3301 such), and as a flag indicating that a target is active. */
3302 inferior_ptid = magic_null_ptid;
3306 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1);
3308 /* Add the main thread. */
3309 add_thread_silent (inferior_ptid);
3313 remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
3315 struct remote_state *rs = get_remote_state ();
3316 struct packet_config *noack_config;
3317 char *wait_status = NULL;
3319 immediate_quit++; /* Allow user to interrupt it. */
3322 if (interrupt_on_connect)
3323 send_interrupt_sequence ();
3325 /* Ack any packet which the remote side has already sent. */
3326 serial_write (rs->remote_desc, "+", 1);
3328 /* Signal other parts that we're going through the initial setup,
3329 and so things may not be stable yet. */
3330 rs->starting_up = 1;
3332 /* The first packet we send to the target is the optional "supported
3333 packets" request. If the target can answer this, it will tell us
3334 which later probes to skip. */
3335 remote_query_supported ();
3337 /* If the stub wants to get a QAllow, compose one and send it. */
3338 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
3339 remote_set_permissions (target);
3341 /* Next, we possibly activate noack mode.
3343 If the QStartNoAckMode packet configuration is set to AUTO,
3344 enable noack mode if the stub reported a wish for it with
3347 If set to TRUE, then enable noack mode even if the stub didn't
3348 report it in qSupported. If the stub doesn't reply OK, the
3349 session ends with an error.
3351 If FALSE, then don't activate noack mode, regardless of what the
3352 stub claimed should be the default with qSupported. */
3354 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3355 if (packet_config_support (noack_config) != PACKET_DISABLE)
3357 putpkt ("QStartNoAckMode");
3358 getpkt (&rs->buf, &rs->buf_size, 0);
3359 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3365 /* Tell the remote that we are using the extended protocol. */
3367 getpkt (&rs->buf, &rs->buf_size, 0);
3370 /* Let the target know which signals it is allowed to pass down to
3372 update_signals_program_target ();
3374 /* Next, if the target can specify a description, read it. We do
3375 this before anything involving memory or registers. */
3376 target_find_description ();
3378 /* Next, now that we know something about the target, update the
3379 address spaces in the program spaces. */
3380 update_address_spaces ();
3382 /* On OSs where the list of libraries is global to all
3383 processes, we fetch them early. */
3384 if (gdbarch_has_global_solist (target_gdbarch ()))
3385 solib_add (NULL, from_tty, target, auto_solib_add);
3389 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3390 error (_("Non-stop mode requested, but remote "
3391 "does not support non-stop"));
3393 putpkt ("QNonStop:1");
3394 getpkt (&rs->buf, &rs->buf_size, 0);
3396 if (strcmp (rs->buf, "OK") != 0)
3397 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3399 /* Find about threads and processes the stub is already
3400 controlling. We default to adding them in the running state.
3401 The '?' query below will then tell us about which threads are
3403 remote_threads_info (target);
3405 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
3407 /* Don't assume that the stub can operate in all-stop mode.
3408 Request it explicitly. */
3409 putpkt ("QNonStop:0");
3410 getpkt (&rs->buf, &rs->buf_size, 0);
3412 if (strcmp (rs->buf, "OK") != 0)
3413 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3416 /* Upload TSVs regardless of whether the target is running or not. The
3417 remote stub, such as GDBserver, may have some predefined or builtin
3418 TSVs, even if the target is not running. */
3419 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3421 struct uploaded_tsv *uploaded_tsvs = NULL;
3423 remote_upload_trace_state_variables (target, &uploaded_tsvs);
3424 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3427 /* Check whether the target is running now. */
3429 getpkt (&rs->buf, &rs->buf_size, 0);
3435 struct inferior *inf;
3437 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3440 error (_("The target is not running (try extended-remote?)"));
3442 /* We're connected, but not running. Drop out before we
3443 call start_remote. */
3444 rs->starting_up = 0;
3449 /* Save the reply for later. */
3450 wait_status = alloca (strlen (rs->buf) + 1);
3451 strcpy (wait_status, rs->buf);
3454 /* Fetch thread list. */
3455 target_find_new_threads ();
3457 /* Let the stub know that we want it to return the thread. */
3458 set_continue_thread (minus_one_ptid);
3460 if (thread_count () == 0)
3462 /* Target has no concept of threads at all. GDB treats
3463 non-threaded target as single-threaded; add a main
3465 add_current_inferior_and_thread (wait_status);
3469 /* We have thread information; select the thread the target
3470 says should be current. If we're reconnecting to a
3471 multi-threaded program, this will ideally be the thread
3472 that last reported an event before GDB disconnected. */
3473 inferior_ptid = get_current_thread (wait_status);
3474 if (ptid_equal (inferior_ptid, null_ptid))
3476 /* Odd... The target was able to list threads, but not
3477 tell us which thread was current (no "thread"
3478 register in T stop reply?). Just pick the first
3479 thread in the thread list then. */
3480 inferior_ptid = thread_list->ptid;
3484 /* init_wait_for_inferior should be called before get_offsets in order
3485 to manage `inserted' flag in bp loc in a correct state.
3486 breakpoint_init_inferior, called from init_wait_for_inferior, set
3487 `inserted' flag to 0, while before breakpoint_re_set, called from
3488 start_remote, set `inserted' flag to 1. In the initialization of
3489 inferior, breakpoint_init_inferior should be called first, and then
3490 breakpoint_re_set can be called. If this order is broken, state of
3491 `inserted' flag is wrong, and cause some problems on breakpoint
3493 init_wait_for_inferior ();
3495 get_offsets (); /* Get text, data & bss offsets. */
3497 /* If we could not find a description using qXfer, and we know
3498 how to do it some other way, try again. This is not
3499 supported for non-stop; it could be, but it is tricky if
3500 there are no stopped threads when we connect. */
3501 if (remote_read_description_p (target)
3502 && gdbarch_target_desc (target_gdbarch ()) == NULL)
3504 target_clear_description ();
3505 target_find_description ();
3508 /* Use the previously fetched status. */
3509 gdb_assert (wait_status != NULL);
3510 strcpy (rs->buf, wait_status);
3511 rs->cached_wait_status = 1;
3514 start_remote (from_tty); /* Initialize gdb process mechanisms. */
3518 /* Clear WFI global state. Do this before finding about new
3519 threads and inferiors, and setting the current inferior.
3520 Otherwise we would clear the proceed status of the current
3521 inferior when we want its stop_soon state to be preserved
3522 (see notice_new_inferior). */
3523 init_wait_for_inferior ();
3525 /* In non-stop, we will either get an "OK", meaning that there
3526 are no stopped threads at this time; or, a regular stop
3527 reply. In the latter case, there may be more than one thread
3528 stopped --- we pull them all out using the vStopped
3530 if (strcmp (rs->buf, "OK") != 0)
3532 struct notif_client *notif = ¬if_client_stop;
3534 /* remote_notif_get_pending_replies acks this one, and gets
3536 rs->notif_state->pending_event[notif_client_stop.id]
3537 = remote_notif_parse (notif, rs->buf);
3538 remote_notif_get_pending_events (notif);
3540 /* Make sure that threads that were stopped remain
3542 iterate_over_threads (set_stop_requested_callback, NULL);
3545 if (target_can_async_p ())
3546 target_async (inferior_event_handler, 0);
3548 if (thread_count () == 0)
3551 error (_("The target is not running (try extended-remote?)"));
3553 /* We're connected, but not running. Drop out before we
3554 call start_remote. */
3555 rs->starting_up = 0;
3559 /* Let the stub know that we want it to return the thread. */
3561 /* Force the stub to choose a thread. */
3562 set_general_thread (null_ptid);
3565 inferior_ptid = remote_current_thread (minus_one_ptid);
3566 if (ptid_equal (inferior_ptid, minus_one_ptid))
3567 error (_("remote didn't report the current thread in non-stop mode"));
3569 get_offsets (); /* Get text, data & bss offsets. */
3571 /* In non-stop mode, any cached wait status will be stored in
3572 the stop reply queue. */
3573 gdb_assert (wait_status == NULL);
3575 /* Report all signals during attach/startup. */
3576 remote_pass_signals (target, 0, NULL);
3579 /* If we connected to a live target, do some additional setup. */
3580 if (target_has_execution)
3582 if (symfile_objfile) /* No use without a symbol-file. */
3583 remote_check_symbols ();
3586 /* Possibly the target has been engaged in a trace run started
3587 previously; find out where things are at. */
3588 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3590 struct uploaded_tp *uploaded_tps = NULL;
3592 if (current_trace_status ()->running)
3593 printf_filtered (_("Trace is already running on the target.\n"));
3595 remote_upload_tracepoints (target, &uploaded_tps);
3597 merge_uploaded_tracepoints (&uploaded_tps);
3600 /* The thread and inferior lists are now synchronized with the
3601 target, our symbols have been relocated, and we're merged the
3602 target's tracepoints with ours. We're done with basic start
3604 rs->starting_up = 0;
3606 /* Maybe breakpoints are global and need to be inserted now. */
3607 if (breakpoints_should_be_inserted_now ())
3608 insert_breakpoints ();
3611 /* Open a connection to a remote debugger.
3612 NAME is the filename used for communication. */
3615 remote_open (const char *name, int from_tty)
3617 remote_open_1 (name, from_tty, &remote_ops, 0);
3620 /* Open a connection to a remote debugger using the extended
3621 remote gdb protocol. NAME is the filename used for communication. */
3624 extended_remote_open (const char *name, int from_tty)
3626 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3629 /* Reset all packets back to "unknown support". Called when opening a
3630 new connection to a remote target. */
3633 reset_all_packet_configs_support (void)
3637 for (i = 0; i < PACKET_MAX; i++)
3638 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3641 /* Initialize all packet configs. */
3644 init_all_packet_configs (void)
3648 for (i = 0; i < PACKET_MAX; i++)
3650 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3651 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3655 /* Symbol look-up. */
3658 remote_check_symbols (void)
3660 struct remote_state *rs = get_remote_state ();
3661 char *msg, *reply, *tmp;
3662 struct bound_minimal_symbol sym;
3665 /* The remote side has no concept of inferiors that aren't running
3666 yet, it only knows about running processes. If we're connected
3667 but our current inferior is not running, we should not invite the
3668 remote target to request symbol lookups related to its
3669 (unrelated) current process. */
3670 if (!target_has_execution)
3673 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
3676 /* Make sure the remote is pointing at the right process. Note
3677 there's no way to select "no process". */
3678 set_general_process ();
3680 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3681 because we need both at the same time. */
3682 msg = alloca (get_remote_packet_size ());
3684 /* Invite target to request symbol lookups. */
3686 putpkt ("qSymbol::");
3687 getpkt (&rs->buf, &rs->buf_size, 0);
3688 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3691 while (strncmp (reply, "qSymbol:", 8) == 0)
3693 struct bound_minimal_symbol sym;
3696 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3698 sym = lookup_minimal_symbol (msg, NULL, NULL);
3699 if (sym.minsym == NULL)
3700 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3703 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
3704 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
3706 /* If this is a function address, return the start of code
3707 instead of any data function descriptor. */
3708 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3712 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3713 phex_nz (sym_addr, addr_size), &reply[8]);
3717 getpkt (&rs->buf, &rs->buf_size, 0);
3722 static struct serial *
3723 remote_serial_open (const char *name)
3725 static int udp_warning = 0;
3727 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3728 of in ser-tcp.c, because it is the remote protocol assuming that the
3729 serial connection is reliable and not the serial connection promising
3731 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3733 warning (_("The remote protocol may be unreliable over UDP.\n"
3734 "Some events may be lost, rendering further debugging "
3739 return serial_open (name);
3742 /* Inform the target of our permission settings. The permission flags
3743 work without this, but if the target knows the settings, it can do
3744 a couple things. First, it can add its own check, to catch cases
3745 that somehow manage to get by the permissions checks in target
3746 methods. Second, if the target is wired to disallow particular
3747 settings (for instance, a system in the field that is not set up to
3748 be able to stop at a breakpoint), it can object to any unavailable
3752 remote_set_permissions (struct target_ops *self)
3754 struct remote_state *rs = get_remote_state ();
3756 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3757 "WriteReg:%x;WriteMem:%x;"
3758 "InsertBreak:%x;InsertTrace:%x;"
3759 "InsertFastTrace:%x;Stop:%x",
3760 may_write_registers, may_write_memory,
3761 may_insert_breakpoints, may_insert_tracepoints,
3762 may_insert_fast_tracepoints, may_stop);
3764 getpkt (&rs->buf, &rs->buf_size, 0);
3766 /* If the target didn't like the packet, warn the user. Do not try
3767 to undo the user's settings, that would just be maddening. */
3768 if (strcmp (rs->buf, "OK") != 0)
3769 warning (_("Remote refused setting permissions with: %s"), rs->buf);
3772 /* This type describes each known response to the qSupported
3774 struct protocol_feature
3776 /* The name of this protocol feature. */
3779 /* The default for this protocol feature. */
3780 enum packet_support default_support;
3782 /* The function to call when this feature is reported, or after
3783 qSupported processing if the feature is not supported.
3784 The first argument points to this structure. The second
3785 argument indicates whether the packet requested support be
3786 enabled, disabled, or probed (or the default, if this function
3787 is being called at the end of processing and this feature was
3788 not reported). The third argument may be NULL; if not NULL, it
3789 is a NUL-terminated string taken from the packet following
3790 this feature's name and an equals sign. */
3791 void (*func) (const struct protocol_feature *, enum packet_support,
3794 /* The corresponding packet for this feature. Only used if
3795 FUNC is remote_supported_packet. */
3800 remote_supported_packet (const struct protocol_feature *feature,
3801 enum packet_support support,
3802 const char *argument)
3806 warning (_("Remote qSupported response supplied an unexpected value for"
3807 " \"%s\"."), feature->name);
3811 remote_protocol_packets[feature->packet].support = support;
3815 remote_packet_size (const struct protocol_feature *feature,
3816 enum packet_support support, const char *value)
3818 struct remote_state *rs = get_remote_state ();
3823 if (support != PACKET_ENABLE)
3826 if (value == NULL || *value == '\0')
3828 warning (_("Remote target reported \"%s\" without a size."),
3834 packet_size = strtol (value, &value_end, 16);
3835 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3837 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3838 feature->name, value);
3842 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3844 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3845 packet_size, MAX_REMOTE_PACKET_SIZE);
3846 packet_size = MAX_REMOTE_PACKET_SIZE;
3849 /* Record the new maximum packet size. */
3850 rs->explicit_packet_size = packet_size;
3853 static const struct protocol_feature remote_protocol_features[] = {
3854 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
3855 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
3856 PACKET_qXfer_auxv },
3857 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3858 PACKET_qXfer_features },
3859 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3860 PACKET_qXfer_libraries },
3861 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3862 PACKET_qXfer_libraries_svr4 },
3863 { "augmented-libraries-svr4-read", PACKET_DISABLE,
3864 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
3865 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
3866 PACKET_qXfer_memory_map },
3867 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3868 PACKET_qXfer_spu_read },
3869 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3870 PACKET_qXfer_spu_write },
3871 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3872 PACKET_qXfer_osdata },
3873 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3874 PACKET_qXfer_threads },
3875 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3876 PACKET_qXfer_traceframe_info },
3877 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3878 PACKET_QPassSignals },
3879 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3880 PACKET_QProgramSignals },
3881 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3882 PACKET_QStartNoAckMode },
3883 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
3884 PACKET_multiprocess_feature },
3885 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
3886 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3887 PACKET_qXfer_siginfo_read },
3888 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3889 PACKET_qXfer_siginfo_write },
3890 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
3891 PACKET_ConditionalTracepoints },
3892 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3893 PACKET_ConditionalBreakpoints },
3894 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
3895 PACKET_BreakpointCommands },
3896 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
3897 PACKET_FastTracepoints },
3898 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
3899 PACKET_StaticTracepoints },
3900 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
3901 PACKET_InstallInTrace},
3902 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
3903 PACKET_DisconnectedTracing_feature },
3904 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
3906 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
3908 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3909 PACKET_TracepointSource },
3910 { "QAllow", PACKET_DISABLE, remote_supported_packet,
3912 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
3913 PACKET_EnableDisableTracepoints_feature },
3914 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
3915 PACKET_qXfer_fdpic },
3916 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
3918 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
3919 PACKET_QDisableRandomization },
3920 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
3921 { "QTBuffer:size", PACKET_DISABLE,
3922 remote_supported_packet, PACKET_QTBuffer_size},
3923 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
3924 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
3925 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
3926 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
3927 PACKET_qXfer_btrace }
3930 static char *remote_support_xml;
3932 /* Register string appended to "xmlRegisters=" in qSupported query. */
3935 register_remote_support_xml (const char *xml)
3937 #if defined(HAVE_LIBEXPAT)
3938 if (remote_support_xml == NULL)
3939 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
3942 char *copy = xstrdup (remote_support_xml + 13);
3943 char *p = strtok (copy, ",");
3947 if (strcmp (p, xml) == 0)
3954 while ((p = strtok (NULL, ",")) != NULL);
3957 remote_support_xml = reconcat (remote_support_xml,
3958 remote_support_xml, ",", xml,
3965 remote_query_supported_append (char *msg, const char *append)
3968 return reconcat (msg, msg, ";", append, (char *) NULL);
3970 return xstrdup (append);
3974 remote_query_supported (void)
3976 struct remote_state *rs = get_remote_state ();
3979 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
3981 /* The packet support flags are handled differently for this packet
3982 than for most others. We treat an error, a disabled packet, and
3983 an empty response identically: any features which must be reported
3984 to be used will be automatically disabled. An empty buffer
3985 accomplishes this, since that is also the representation for a list
3986 containing no features. */
3989 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
3992 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
3994 q = remote_query_supported_append (q, "multiprocess+");
3996 if (remote_support_xml)
3997 q = remote_query_supported_append (q, remote_support_xml);
3999 q = remote_query_supported_append (q, "qRelocInsn+");
4001 q = reconcat (q, "qSupported:", q, (char *) NULL);
4004 do_cleanups (old_chain);
4006 getpkt (&rs->buf, &rs->buf_size, 0);
4008 /* If an error occured, warn, but do not return - just reset the
4009 buffer to empty and go on to disable features. */
4010 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4013 warning (_("Remote failure reply: %s"), rs->buf);
4018 memset (seen, 0, sizeof (seen));
4023 enum packet_support is_supported;
4024 char *p, *end, *name_end, *value;
4026 /* First separate out this item from the rest of the packet. If
4027 there's another item after this, we overwrite the separator
4028 (terminated strings are much easier to work with). */
4030 end = strchr (p, ';');
4033 end = p + strlen (p);
4043 warning (_("empty item in \"qSupported\" response"));
4048 name_end = strchr (p, '=');
4051 /* This is a name=value entry. */
4052 is_supported = PACKET_ENABLE;
4053 value = name_end + 1;
4062 is_supported = PACKET_ENABLE;
4066 is_supported = PACKET_DISABLE;
4070 is_supported = PACKET_SUPPORT_UNKNOWN;
4074 warning (_("unrecognized item \"%s\" "
4075 "in \"qSupported\" response"), p);
4081 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4082 if (strcmp (remote_protocol_features[i].name, p) == 0)
4084 const struct protocol_feature *feature;
4087 feature = &remote_protocol_features[i];
4088 feature->func (feature, is_supported, value);
4093 /* If we increased the packet size, make sure to increase the global
4094 buffer size also. We delay this until after parsing the entire
4095 qSupported packet, because this is the same buffer we were
4097 if (rs->buf_size < rs->explicit_packet_size)
4099 rs->buf_size = rs->explicit_packet_size;
4100 rs->buf = xrealloc (rs->buf, rs->buf_size);
4103 /* Handle the defaults for unmentioned features. */
4104 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4107 const struct protocol_feature *feature;
4109 feature = &remote_protocol_features[i];
4110 feature->func (feature, feature->default_support, NULL);
4114 /* Remove any of the remote.c targets from target stack. Upper targets depend
4115 on it so remove them first. */
4118 remote_unpush_target (void)
4120 pop_all_targets_above (process_stratum - 1);
4124 remote_open_1 (const char *name, int from_tty,
4125 struct target_ops *target, int extended_p)
4127 struct remote_state *rs = get_remote_state ();
4130 error (_("To open a remote debug connection, you need to specify what\n"
4131 "serial device is attached to the remote system\n"
4132 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4134 /* See FIXME above. */
4135 if (!target_async_permitted)
4136 wait_forever_enabled_p = 1;
4138 /* If we're connected to a running target, target_preopen will kill it.
4139 Ask this question first, before target_preopen has a chance to kill
4141 if (rs->remote_desc != NULL && !have_inferiors ())
4144 && !query (_("Already connected to a remote target. Disconnect? ")))
4145 error (_("Still connected."));
4148 /* Here the possibly existing remote target gets unpushed. */
4149 target_preopen (from_tty);
4151 /* Make sure we send the passed signals list the next time we resume. */
4152 xfree (rs->last_pass_packet);
4153 rs->last_pass_packet = NULL;
4155 /* Make sure we send the program signals list the next time we
4157 xfree (rs->last_program_signals_packet);
4158 rs->last_program_signals_packet = NULL;
4160 remote_fileio_reset ();
4161 reopen_exec_file ();
4164 rs->remote_desc = remote_serial_open (name);
4165 if (!rs->remote_desc)
4166 perror_with_name (name);
4168 if (baud_rate != -1)
4170 if (serial_setbaudrate (rs->remote_desc, baud_rate))
4172 /* The requested speed could not be set. Error out to
4173 top level after closing remote_desc. Take care to
4174 set remote_desc to NULL to avoid closing remote_desc
4176 serial_close (rs->remote_desc);
4177 rs->remote_desc = NULL;
4178 perror_with_name (name);
4182 serial_raw (rs->remote_desc);
4184 /* If there is something sitting in the buffer we might take it as a
4185 response to a command, which would be bad. */
4186 serial_flush_input (rs->remote_desc);
4190 puts_filtered ("Remote debugging using ");
4191 puts_filtered (name);
4192 puts_filtered ("\n");
4194 push_target (target); /* Switch to using remote target now. */
4196 /* Register extra event sources in the event loop. */
4197 remote_async_inferior_event_token
4198 = create_async_event_handler (remote_async_inferior_event_handler,
4200 rs->notif_state = remote_notif_state_allocate ();
4202 /* Reset the target state; these things will be queried either by
4203 remote_query_supported or as they are needed. */
4204 reset_all_packet_configs_support ();
4205 rs->cached_wait_status = 0;
4206 rs->explicit_packet_size = 0;
4208 rs->extended = extended_p;
4209 rs->waiting_for_stop_reply = 0;
4210 rs->ctrlc_pending_p = 0;
4212 rs->general_thread = not_sent_ptid;
4213 rs->continue_thread = not_sent_ptid;
4214 rs->remote_traceframe_number = -1;
4216 /* Probe for ability to use "ThreadInfo" query, as required. */
4217 rs->use_threadinfo_query = 1;
4218 rs->use_threadextra_query = 1;
4220 if (target_async_permitted)
4222 /* With this target we start out by owning the terminal. */
4223 remote_async_terminal_ours_p = 1;
4225 /* FIXME: cagney/1999-09-23: During the initial connection it is
4226 assumed that the target is already ready and able to respond to
4227 requests. Unfortunately remote_start_remote() eventually calls
4228 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4229 around this. Eventually a mechanism that allows
4230 wait_for_inferior() to expect/get timeouts will be
4232 wait_forever_enabled_p = 0;
4235 /* First delete any symbols previously loaded from shared libraries. */
4236 no_shared_libraries (NULL, 0);
4239 init_thread_list ();
4241 /* Start the remote connection. If error() or QUIT, discard this
4242 target (we'd otherwise be in an inconsistent state) and then
4243 propogate the error on up the exception chain. This ensures that
4244 the caller doesn't stumble along blindly assuming that the
4245 function succeeded. The CLI doesn't have this problem but other
4246 UI's, such as MI do.
4248 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4249 this function should return an error indication letting the
4250 caller restore the previous state. Unfortunately the command
4251 ``target remote'' is directly wired to this function making that
4252 impossible. On a positive note, the CLI side of this problem has
4253 been fixed - the function set_cmd_context() makes it possible for
4254 all the ``target ....'' commands to share a common callback
4255 function. See cli-dump.c. */
4257 volatile struct gdb_exception ex;
4259 TRY_CATCH (ex, RETURN_MASK_ALL)
4261 remote_start_remote (from_tty, target, extended_p);
4265 /* Pop the partially set up target - unless something else did
4266 already before throwing the exception. */
4267 if (rs->remote_desc != NULL)
4268 remote_unpush_target ();
4269 if (target_async_permitted)
4270 wait_forever_enabled_p = 1;
4271 throw_exception (ex);
4275 if (target_async_permitted)
4276 wait_forever_enabled_p = 1;
4279 /* This takes a program previously attached to and detaches it. After
4280 this is done, GDB can be used to debug some other program. We
4281 better not have left any breakpoints in the target program or it'll
4282 die when it hits one. */
4285 remote_detach_1 (const char *args, int from_tty, int extended)
4287 int pid = ptid_get_pid (inferior_ptid);
4288 struct remote_state *rs = get_remote_state ();
4291 error (_("Argument given to \"detach\" when remotely debugging."));
4293 if (!target_has_execution)
4294 error (_("No process to detach from."));
4298 char *exec_file = get_exec_file (0);
4299 if (exec_file == NULL)
4301 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4302 target_pid_to_str (pid_to_ptid (pid)));
4303 gdb_flush (gdb_stdout);
4306 /* Tell the remote target to detach. */
4307 if (remote_multi_process_p (rs))
4308 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4310 strcpy (rs->buf, "D");
4313 getpkt (&rs->buf, &rs->buf_size, 0);
4315 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4317 else if (rs->buf[0] == '\0')
4318 error (_("Remote doesn't know how to detach"));
4320 error (_("Can't detach process."));
4322 if (from_tty && !extended)
4323 puts_filtered (_("Ending remote debugging.\n"));
4325 target_mourn_inferior ();
4329 remote_detach (struct target_ops *ops, const char *args, int from_tty)
4331 remote_detach_1 (args, from_tty, 0);
4335 extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
4337 remote_detach_1 (args, from_tty, 1);
4340 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4343 remote_disconnect (struct target_ops *target, const char *args, int from_tty)
4346 error (_("Argument given to \"disconnect\" when remotely debugging."));
4348 /* Make sure we unpush even the extended remote targets; mourn
4349 won't do it. So call remote_mourn_1 directly instead of
4350 target_mourn_inferior. */
4351 remote_mourn_1 (target);
4354 puts_filtered ("Ending remote debugging.\n");
4357 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4358 be chatty about it. */
4361 extended_remote_attach_1 (struct target_ops *target, const char *args,
4364 struct remote_state *rs = get_remote_state ();
4366 char *wait_status = NULL;
4368 pid = parse_pid_to_attach (args);
4370 /* Remote PID can be freely equal to getpid, do not check it here the same
4371 way as in other targets. */
4373 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
4374 error (_("This target does not support attaching to a process"));
4378 char *exec_file = get_exec_file (0);
4381 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4382 target_pid_to_str (pid_to_ptid (pid)));
4384 printf_unfiltered (_("Attaching to %s\n"),
4385 target_pid_to_str (pid_to_ptid (pid)));
4387 gdb_flush (gdb_stdout);
4390 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
4392 getpkt (&rs->buf, &rs->buf_size, 0);
4394 switch (packet_ok (rs->buf,
4395 &remote_protocol_packets[PACKET_vAttach]))
4400 /* Save the reply for later. */
4401 wait_status = alloca (strlen (rs->buf) + 1);
4402 strcpy (wait_status, rs->buf);
4404 else if (strcmp (rs->buf, "OK") != 0)
4405 error (_("Attaching to %s failed with: %s"),
4406 target_pid_to_str (pid_to_ptid (pid)),
4409 case PACKET_UNKNOWN:
4410 error (_("This target does not support attaching to a process"));
4412 error (_("Attaching to %s failed"),
4413 target_pid_to_str (pid_to_ptid (pid)));
4416 set_current_inferior (remote_add_inferior (0, pid, 1));
4418 inferior_ptid = pid_to_ptid (pid);
4422 struct thread_info *thread;
4424 /* Get list of threads. */
4425 remote_threads_info (target);
4427 thread = first_thread_of_process (pid);
4429 inferior_ptid = thread->ptid;
4431 inferior_ptid = pid_to_ptid (pid);
4433 /* Invalidate our notion of the remote current thread. */
4434 record_currthread (rs, minus_one_ptid);
4438 /* Now, if we have thread information, update inferior_ptid. */
4439 inferior_ptid = remote_current_thread (inferior_ptid);
4441 /* Add the main thread to the thread list. */
4442 add_thread_silent (inferior_ptid);
4445 /* Next, if the target can specify a description, read it. We do
4446 this before anything involving memory or registers. */
4447 target_find_description ();
4451 /* Use the previously fetched status. */
4452 gdb_assert (wait_status != NULL);
4454 if (target_can_async_p ())
4456 struct notif_event *reply
4457 = remote_notif_parse (¬if_client_stop, wait_status);
4459 push_stop_reply ((struct stop_reply *) reply);
4461 target_async (inferior_event_handler, 0);
4465 gdb_assert (wait_status != NULL);
4466 strcpy (rs->buf, wait_status);
4467 rs->cached_wait_status = 1;
4471 gdb_assert (wait_status == NULL);
4475 extended_remote_attach (struct target_ops *ops, const char *args, int from_tty)
4477 extended_remote_attach_1 (ops, args, from_tty);
4480 /* Implementation of the to_post_attach method. */
4483 extended_remote_post_attach (struct target_ops *ops, int pid)
4485 /* In certain cases GDB might not have had the chance to start
4486 symbol lookup up until now. This could happen if the debugged
4487 binary is not using shared libraries, the vsyscall page is not
4488 present (on Linux) and the binary itself hadn't changed since the
4489 debugging process was started. */
4490 if (symfile_objfile != NULL)
4491 remote_check_symbols();
4495 /* Check for the availability of vCont. This function should also check
4499 remote_vcont_probe (struct remote_state *rs)
4503 strcpy (rs->buf, "vCont?");
4505 getpkt (&rs->buf, &rs->buf_size, 0);
4508 /* Make sure that the features we assume are supported. */
4509 if (strncmp (buf, "vCont", 5) == 0)
4512 int support_s, support_S, support_c, support_C;
4518 rs->supports_vCont.t = 0;
4519 rs->supports_vCont.r = 0;
4520 while (p && *p == ';')
4523 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4525 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4527 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4529 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4531 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4532 rs->supports_vCont.t = 1;
4533 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4534 rs->supports_vCont.r = 1;
4536 p = strchr (p, ';');
4539 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4540 BUF will make packet_ok disable the packet. */
4541 if (!support_s || !support_S || !support_c || !support_C)
4545 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4548 /* Helper function for building "vCont" resumptions. Write a
4549 resumption to P. ENDP points to one-passed-the-end of the buffer
4550 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4551 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4552 resumed thread should be single-stepped and/or signalled. If PTID
4553 equals minus_one_ptid, then all threads are resumed; if PTID
4554 represents a process, then all threads of the process are resumed;
4555 the thread to be stepped and/or signalled is given in the global
4559 append_resumption (char *p, char *endp,
4560 ptid_t ptid, int step, enum gdb_signal siggnal)
4562 struct remote_state *rs = get_remote_state ();
4564 if (step && siggnal != GDB_SIGNAL_0)
4565 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4567 /* GDB is willing to range step. */
4568 && use_range_stepping
4569 /* Target supports range stepping. */
4570 && rs->supports_vCont.r
4571 /* We don't currently support range stepping multiple
4572 threads with a wildcard (though the protocol allows it,
4573 so stubs shouldn't make an active effort to forbid
4575 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4577 struct thread_info *tp;
4579 if (ptid_equal (ptid, minus_one_ptid))
4581 /* If we don't know about the target thread's tid, then
4582 we're resuming magic_null_ptid (see caller). */
4583 tp = find_thread_ptid (magic_null_ptid);
4586 tp = find_thread_ptid (ptid);
4587 gdb_assert (tp != NULL);
4589 if (tp->control.may_range_step)
4591 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4593 p += xsnprintf (p, endp - p, ";r%s,%s",
4594 phex_nz (tp->control.step_range_start,
4596 phex_nz (tp->control.step_range_end,
4600 p += xsnprintf (p, endp - p, ";s");
4603 p += xsnprintf (p, endp - p, ";s");
4604 else if (siggnal != GDB_SIGNAL_0)
4605 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4607 p += xsnprintf (p, endp - p, ";c");
4609 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4613 /* All (-1) threads of process. */
4614 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4616 p += xsnprintf (p, endp - p, ":");
4617 p = write_ptid (p, endp, nptid);
4619 else if (!ptid_equal (ptid, minus_one_ptid))
4621 p += xsnprintf (p, endp - p, ":");
4622 p = write_ptid (p, endp, ptid);
4628 /* Append a vCont continue-with-signal action for threads that have a
4629 non-zero stop signal. */
4632 append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4634 struct thread_info *thread;
4636 ALL_NON_EXITED_THREADS (thread)
4637 if (ptid_match (thread->ptid, ptid)
4638 && !ptid_equal (inferior_ptid, thread->ptid)
4639 && thread->suspend.stop_signal != GDB_SIGNAL_0)
4641 p = append_resumption (p, endp, thread->ptid,
4642 0, thread->suspend.stop_signal);
4643 thread->suspend.stop_signal = GDB_SIGNAL_0;
4649 /* Resume the remote inferior by using a "vCont" packet. The thread
4650 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4651 resumed thread should be single-stepped and/or signalled. If PTID
4652 equals minus_one_ptid, then all threads are resumed; the thread to
4653 be stepped and/or signalled is given in the global INFERIOR_PTID.
4654 This function returns non-zero iff it resumes the inferior.
4656 This function issues a strict subset of all possible vCont commands at the
4660 remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
4662 struct remote_state *rs = get_remote_state ();
4666 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4667 remote_vcont_probe (rs);
4669 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
4673 endp = rs->buf + get_remote_packet_size ();
4675 /* If we could generate a wider range of packets, we'd have to worry
4676 about overflowing BUF. Should there be a generic
4677 "multi-part-packet" packet? */
4679 p += xsnprintf (p, endp - p, "vCont");
4681 if (ptid_equal (ptid, magic_null_ptid))
4683 /* MAGIC_NULL_PTID means that we don't have any active threads,
4684 so we don't have any TID numbers the inferior will
4685 understand. Make sure to only send forms that do not specify
4687 append_resumption (p, endp, minus_one_ptid, step, siggnal);
4689 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
4691 /* Resume all threads (of all processes, or of a single
4692 process), with preference for INFERIOR_PTID. This assumes
4693 inferior_ptid belongs to the set of all threads we are about
4695 if (step || siggnal != GDB_SIGNAL_0)
4697 /* Step inferior_ptid, with or without signal. */
4698 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
4701 /* Also pass down any pending signaled resumption for other
4702 threads not the current. */
4703 p = append_pending_thread_resumptions (p, endp, ptid);
4705 /* And continue others without a signal. */
4706 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
4710 /* Scheduler locking; resume only PTID. */
4711 append_resumption (p, endp, ptid, step, siggnal);
4714 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4719 /* In non-stop, the stub replies to vCont with "OK". The stop
4720 reply will be reported asynchronously by means of a `%Stop'
4722 getpkt (&rs->buf, &rs->buf_size, 0);
4723 if (strcmp (rs->buf, "OK") != 0)
4724 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4730 /* Tell the remote machine to resume. */
4733 remote_resume (struct target_ops *ops,
4734 ptid_t ptid, int step, enum gdb_signal siggnal)
4736 struct remote_state *rs = get_remote_state ();
4739 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4740 (explained in remote-notif.c:handle_notification) so
4741 remote_notif_process is not called. We need find a place where
4742 it is safe to start a 'vNotif' sequence. It is good to do it
4743 before resuming inferior, because inferior was stopped and no RSP
4744 traffic at that moment. */
4746 remote_notif_process (rs->notif_state, ¬if_client_stop);
4748 rs->last_sent_signal = siggnal;
4749 rs->last_sent_step = step;
4751 /* The vCont packet doesn't need to specify threads via Hc. */
4752 /* No reverse support (yet) for vCont. */
4753 if (execution_direction != EXEC_REVERSE)
4754 if (remote_vcont_resume (ptid, step, siggnal))
4757 /* All other supported resume packets do use Hc, so set the continue
4759 if (ptid_equal (ptid, minus_one_ptid))
4760 set_continue_thread (any_thread_ptid);
4762 set_continue_thread (ptid);
4765 if (execution_direction == EXEC_REVERSE)
4767 /* We don't pass signals to the target in reverse exec mode. */
4768 if (info_verbose && siggnal != GDB_SIGNAL_0)
4769 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4772 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
4773 error (_("Remote reverse-step not supported."));
4774 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
4775 error (_("Remote reverse-continue not supported."));
4777 strcpy (buf, step ? "bs" : "bc");
4779 else if (siggnal != GDB_SIGNAL_0)
4781 buf[0] = step ? 'S' : 'C';
4782 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
4783 buf[2] = tohex (((int) siggnal) & 0xf);
4787 strcpy (buf, step ? "s" : "c");
4792 /* We are about to start executing the inferior, let's register it
4793 with the event loop. NOTE: this is the one place where all the
4794 execution commands end up. We could alternatively do this in each
4795 of the execution commands in infcmd.c. */
4796 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4797 into infcmd.c in order to allow inferior function calls to work
4798 NOT asynchronously. */
4799 if (target_can_async_p ())
4800 target_async (inferior_event_handler, 0);
4802 /* We've just told the target to resume. The remote server will
4803 wait for the inferior to stop, and then send a stop reply. In
4804 the mean time, we can't start another command/query ourselves
4805 because the stub wouldn't be ready to process it. This applies
4806 only to the base all-stop protocol, however. In non-stop (which
4807 only supports vCont), the stub replies with an "OK", and is
4808 immediate able to process further serial input. */
4810 rs->waiting_for_stop_reply = 1;
4814 /* Set up the signal handler for SIGINT, while the target is
4815 executing, ovewriting the 'regular' SIGINT signal handler. */
4817 async_initialize_sigint_signal_handler (void)
4819 signal (SIGINT, async_handle_remote_sigint);
4822 /* Signal handler for SIGINT, while the target is executing. */
4824 async_handle_remote_sigint (int sig)
4826 signal (sig, async_handle_remote_sigint_twice);
4827 /* Note we need to go through gdb_call_async_signal_handler in order
4828 to wake up the event loop on Windows. */
4829 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
4832 /* Signal handler for SIGINT, installed after SIGINT has already been
4833 sent once. It will take effect the second time that the user sends
4836 async_handle_remote_sigint_twice (int sig)
4838 signal (sig, async_handle_remote_sigint);
4839 /* See note in async_handle_remote_sigint. */
4840 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
4843 /* Perform the real interruption of the target execution, in response
4846 async_remote_interrupt (gdb_client_data arg)
4849 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
4851 target_stop (inferior_ptid);
4854 /* Perform interrupt, if the first attempt did not succeed. Just give
4855 up on the target alltogether. */
4857 async_remote_interrupt_twice (gdb_client_data arg)
4860 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
4865 /* Reinstall the usual SIGINT handlers, after the target has
4868 async_cleanup_sigint_signal_handler (void *dummy)
4870 signal (SIGINT, handle_sigint);
4873 /* Send ^C to target to halt it. Target will respond, and send us a
4875 static void (*ofunc) (int);
4877 /* The command line interface's stop routine. This function is installed
4878 as a signal handler for SIGINT. The first time a user requests a
4879 stop, we call remote_stop to send a break or ^C. If there is no
4880 response from the target (it didn't stop when the user requested it),
4881 we ask the user if he'd like to detach from the target. */
4883 sync_remote_interrupt (int signo)
4885 /* If this doesn't work, try more severe steps. */
4886 signal (signo, sync_remote_interrupt_twice);
4888 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
4891 /* The user typed ^C twice. */
4894 sync_remote_interrupt_twice (int signo)
4896 signal (signo, ofunc);
4897 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
4898 signal (signo, sync_remote_interrupt);
4901 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4902 thread, all threads of a remote process, or all threads of all
4906 remote_stop_ns (ptid_t ptid)
4908 struct remote_state *rs = get_remote_state ();
4910 char *endp = rs->buf + get_remote_packet_size ();
4912 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4913 remote_vcont_probe (rs);
4915 if (!rs->supports_vCont.t)
4916 error (_("Remote server does not support stopping threads"));
4918 if (ptid_equal (ptid, minus_one_ptid)
4919 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4920 p += xsnprintf (p, endp - p, "vCont;t");
4925 p += xsnprintf (p, endp - p, "vCont;t:");
4927 if (ptid_is_pid (ptid))
4928 /* All (-1) threads of process. */
4929 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4932 /* Small optimization: if we already have a stop reply for
4933 this thread, no use in telling the stub we want this
4935 if (peek_stop_reply (ptid))
4941 write_ptid (p, endp, nptid);
4944 /* In non-stop, we get an immediate OK reply. The stop reply will
4945 come in asynchronously by notification. */
4947 getpkt (&rs->buf, &rs->buf_size, 0);
4948 if (strcmp (rs->buf, "OK") != 0)
4949 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
4952 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4953 remote target. It is undefined which thread of which process
4954 reports the stop. */
4957 remote_stop_as (ptid_t ptid)
4959 struct remote_state *rs = get_remote_state ();
4961 rs->ctrlc_pending_p = 1;
4963 /* If the inferior is stopped already, but the core didn't know
4964 about it yet, just ignore the request. The cached wait status
4965 will be collected in remote_wait. */
4966 if (rs->cached_wait_status)
4969 /* Send interrupt_sequence to remote target. */
4970 send_interrupt_sequence ();
4973 /* This is the generic stop called via the target vector. When a target
4974 interrupt is requested, either by the command line or the GUI, we
4975 will eventually end up here. */
4978 remote_stop (struct target_ops *self, ptid_t ptid)
4981 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
4984 remote_stop_ns (ptid);
4986 remote_stop_as (ptid);
4989 /* Ask the user what to do when an interrupt is received. */
4992 interrupt_query (void)
4994 target_terminal_ours ();
4996 if (target_can_async_p ())
4998 signal (SIGINT, handle_sigint);
5003 if (query (_("Interrupted while waiting for the program.\n\
5004 Give up (and stop debugging it)? ")))
5006 remote_unpush_target ();
5011 target_terminal_inferior ();
5014 /* Enable/disable target terminal ownership. Most targets can use
5015 terminal groups to control terminal ownership. Remote targets are
5016 different in that explicit transfer of ownership to/from GDB/target
5020 remote_terminal_inferior (struct target_ops *self)
5022 if (!target_async_permitted)
5023 /* Nothing to do. */
5026 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5027 idempotent. The event-loop GDB talking to an asynchronous target
5028 with a synchronous command calls this function from both
5029 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5030 transfer the terminal to the target when it shouldn't this guard
5032 if (!remote_async_terminal_ours_p)
5034 delete_file_handler (input_fd);
5035 remote_async_terminal_ours_p = 0;
5036 async_initialize_sigint_signal_handler ();
5037 /* NOTE: At this point we could also register our selves as the
5038 recipient of all input. Any characters typed could then be
5039 passed on down to the target. */
5043 remote_terminal_ours (struct target_ops *self)
5045 if (!target_async_permitted)
5046 /* Nothing to do. */
5049 /* See FIXME in remote_terminal_inferior. */
5050 if (remote_async_terminal_ours_p)
5052 async_cleanup_sigint_signal_handler (NULL);
5053 add_file_handler (input_fd, stdin_event_handler, 0);
5054 remote_async_terminal_ours_p = 1;
5058 remote_console_output (char *msg)
5062 for (p = msg; p[0] && p[1]; p += 2)
5065 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5069 fputs_unfiltered (tb, gdb_stdtarg);
5071 gdb_flush (gdb_stdtarg);
5074 typedef struct cached_reg
5077 gdb_byte data[MAX_REGISTER_SIZE];
5080 DEF_VEC_O(cached_reg_t);
5082 typedef struct stop_reply
5084 struct notif_event base;
5086 /* The identifier of the thread about this event */
5089 /* The remote state this event is associated with. When the remote
5090 connection, represented by a remote_state object, is closed,
5091 all the associated stop_reply events should be released. */
5092 struct remote_state *rs;
5094 struct target_waitstatus ws;
5096 /* Expedited registers. This makes remote debugging a bit more
5097 efficient for those targets that provide critical registers as
5098 part of their normal status mechanism (as another roundtrip to
5099 fetch them is avoided). */
5100 VEC(cached_reg_t) *regcache;
5102 int stopped_by_watchpoint_p;
5103 CORE_ADDR watch_data_address;
5108 DECLARE_QUEUE_P (stop_reply_p);
5109 DEFINE_QUEUE_P (stop_reply_p);
5110 /* The list of already fetched and acknowledged stop events. This
5111 queue is used for notification Stop, and other notifications
5112 don't need queue for their events, because the notification events
5113 of Stop can't be consumed immediately, so that events should be
5114 queued first, and be consumed by remote_wait_{ns,as} one per
5115 time. Other notifications can consume their events immediately,
5116 so queue is not needed for them. */
5117 static QUEUE (stop_reply_p) *stop_reply_queue;
5120 stop_reply_xfree (struct stop_reply *r)
5122 notif_event_xfree ((struct notif_event *) r);
5126 remote_notif_stop_parse (struct notif_client *self, char *buf,
5127 struct notif_event *event)
5129 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5133 remote_notif_stop_ack (struct notif_client *self, char *buf,
5134 struct notif_event *event)
5136 struct stop_reply *stop_reply = (struct stop_reply *) event;
5139 putpkt ((char *) self->ack_command);
5141 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5142 /* We got an unknown stop reply. */
5143 error (_("Unknown stop reply"));
5145 push_stop_reply (stop_reply);
5149 remote_notif_stop_can_get_pending_events (struct notif_client *self)
5151 /* We can't get pending events in remote_notif_process for
5152 notification stop, and we have to do this in remote_wait_ns
5153 instead. If we fetch all queued events from stub, remote stub
5154 may exit and we have no chance to process them back in
5156 mark_async_event_handler (remote_async_inferior_event_token);
5161 stop_reply_dtr (struct notif_event *event)
5163 struct stop_reply *r = (struct stop_reply *) event;
5165 VEC_free (cached_reg_t, r->regcache);
5168 static struct notif_event *
5169 remote_notif_stop_alloc_reply (void)
5171 struct notif_event *r
5172 = (struct notif_event *) XNEW (struct stop_reply);
5174 r->dtr = stop_reply_dtr;
5179 /* A client of notification Stop. */
5181 struct notif_client notif_client_stop =
5185 remote_notif_stop_parse,
5186 remote_notif_stop_ack,
5187 remote_notif_stop_can_get_pending_events,
5188 remote_notif_stop_alloc_reply,
5192 /* A parameter to pass data in and out. */
5194 struct queue_iter_param
5197 struct stop_reply *output;
5200 /* Remove stop replies in the queue if its pid is equal to the given
5204 remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5205 QUEUE_ITER (stop_reply_p) *iter,
5209 struct queue_iter_param *param = data;
5210 struct inferior *inf = param->input;
5212 if (ptid_get_pid (event->ptid) == inf->pid)
5214 stop_reply_xfree (event);
5215 QUEUE_remove_elem (stop_reply_p, q, iter);
5221 /* Discard all pending stop replies of inferior INF. */
5224 discard_pending_stop_replies (struct inferior *inf)
5227 struct queue_iter_param param;
5228 struct stop_reply *reply;
5229 struct remote_state *rs = get_remote_state ();
5230 struct remote_notif_state *rns = rs->notif_state;
5232 /* This function can be notified when an inferior exists. When the
5233 target is not remote, the notification state is NULL. */
5234 if (rs->remote_desc == NULL)
5237 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
5239 /* Discard the in-flight notification. */
5240 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
5242 stop_reply_xfree (reply);
5243 rns->pending_event[notif_client_stop.id] = NULL;
5247 param.output = NULL;
5248 /* Discard the stop replies we have already pulled with
5250 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5251 remove_stop_reply_for_inferior, ¶m);
5254 /* If its remote state is equal to the given remote state,
5255 remove EVENT from the stop reply queue. */
5258 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5259 QUEUE_ITER (stop_reply_p) *iter,
5263 struct queue_iter_param *param = data;
5264 struct remote_state *rs = param->input;
5266 if (event->rs == rs)
5268 stop_reply_xfree (event);
5269 QUEUE_remove_elem (stop_reply_p, q, iter);
5275 /* Discard the stop replies for RS in stop_reply_queue. */
5278 discard_pending_stop_replies_in_queue (struct remote_state *rs)
5280 struct queue_iter_param param;
5283 param.output = NULL;
5284 /* Discard the stop replies we have already pulled with
5286 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5287 remove_stop_reply_of_remote_state, ¶m);
5290 /* A parameter to pass data in and out. */
5293 remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5294 QUEUE_ITER (stop_reply_p) *iter,
5298 struct queue_iter_param *param = data;
5299 ptid_t *ptid = param->input;
5301 if (ptid_match (event->ptid, *ptid))
5303 param->output = event;
5304 QUEUE_remove_elem (stop_reply_p, q, iter);
5311 /* Remove the first reply in 'stop_reply_queue' which matches
5314 static struct stop_reply *
5315 remote_notif_remove_queued_reply (ptid_t ptid)
5317 struct queue_iter_param param;
5319 param.input = &ptid;
5320 param.output = NULL;
5322 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5323 remote_notif_remove_once_on_match, ¶m);
5325 fprintf_unfiltered (gdb_stdlog,
5326 "notif: discard queued event: 'Stop' in %s\n",
5327 target_pid_to_str (ptid));
5329 return param.output;
5332 /* Look for a queued stop reply belonging to PTID. If one is found,
5333 remove it from the queue, and return it. Returns NULL if none is
5334 found. If there are still queued events left to process, tell the
5335 event loop to get back to target_wait soon. */
5337 static struct stop_reply *
5338 queued_stop_reply (ptid_t ptid)
5340 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
5342 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5343 /* There's still at least an event left. */
5344 mark_async_event_handler (remote_async_inferior_event_token);
5349 /* Push a fully parsed stop reply in the stop reply queue. Since we
5350 know that we now have at least one queued event left to pass to the
5351 core side, tell the event loop to get back to target_wait soon. */
5354 push_stop_reply (struct stop_reply *new_event)
5356 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
5359 fprintf_unfiltered (gdb_stdlog,
5360 "notif: push 'Stop' %s to queue %d\n",
5361 target_pid_to_str (new_event->ptid),
5362 QUEUE_length (stop_reply_p,
5365 mark_async_event_handler (remote_async_inferior_event_token);
5369 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5370 QUEUE_ITER (stop_reply_p) *iter,
5371 struct stop_reply *event,
5374 ptid_t *ptid = data;
5376 return !(ptid_equal (*ptid, event->ptid)
5377 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5380 /* Returns true if we have a stop reply for PTID. */
5383 peek_stop_reply (ptid_t ptid)
5385 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5386 stop_reply_match_ptid_and_ws, &ptid);
5389 /* Parse the stop reply in BUF. Either the function succeeds, and the
5390 result is stored in EVENT, or throws an error. */
5393 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5395 struct remote_arch_state *rsa = get_remote_arch_state ();
5399 event->ptid = null_ptid;
5400 event->rs = get_remote_state ();
5401 event->ws.kind = TARGET_WAITKIND_IGNORE;
5402 event->ws.value.integer = 0;
5403 event->stopped_by_watchpoint_p = 0;
5404 event->regcache = NULL;
5409 case 'T': /* Status with PC, SP, FP, ... */
5410 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5411 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5413 n... = register number
5414 r... = register contents
5417 p = &buf[3]; /* after Txx */
5425 /* If the packet contains a register number, save it in
5426 pnum and set p1 to point to the character following it.
5427 Otherwise p1 points to p. */
5429 /* If this packet is an awatch packet, don't parse the 'a'
5430 as a register number. */
5432 if (strncmp (p, "awatch", strlen("awatch")) != 0
5433 && strncmp (p, "core", strlen ("core") != 0))
5435 /* Read the ``P'' register number. */
5436 pnum = strtol (p, &p_temp, 16);
5442 if (p1 == p) /* No register number present here. */
5444 p1 = strchr (p, ':');
5446 error (_("Malformed packet(a) (missing colon): %s\n\
5449 if (strncmp (p, "thread", p1 - p) == 0)
5450 event->ptid = read_ptid (++p1, &p);
5451 else if ((strncmp (p, "watch", p1 - p) == 0)
5452 || (strncmp (p, "rwatch", p1 - p) == 0)
5453 || (strncmp (p, "awatch", p1 - p) == 0))
5455 event->stopped_by_watchpoint_p = 1;
5456 p = unpack_varlen_hex (++p1, &addr);
5457 event->watch_data_address = (CORE_ADDR) addr;
5459 else if (strncmp (p, "library", p1 - p) == 0)
5463 while (*p_temp && *p_temp != ';')
5466 event->ws.kind = TARGET_WAITKIND_LOADED;
5469 else if (strncmp (p, "replaylog", p1 - p) == 0)
5471 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5472 /* p1 will indicate "begin" or "end", but it makes
5473 no difference for now, so ignore it. */
5474 p_temp = strchr (p1 + 1, ';');
5478 else if (strncmp (p, "core", p1 - p) == 0)
5482 p = unpack_varlen_hex (++p1, &c);
5487 /* Silently skip unknown optional info. */
5488 p_temp = strchr (p1 + 1, ';');
5495 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5496 cached_reg_t cached_reg;
5501 error (_("Malformed packet(b) (missing colon): %s\n\
5507 error (_("Remote sent bad register number %s: %s\n\
5509 hex_string (pnum), p, buf);
5511 cached_reg.num = reg->regnum;
5513 fieldsize = hex2bin (p, cached_reg.data,
5514 register_size (target_gdbarch (),
5517 if (fieldsize < register_size (target_gdbarch (),
5519 warning (_("Remote reply is too short: %s"), buf);
5521 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5525 error (_("Remote register badly formatted: %s\nhere: %s"),
5530 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5534 case 'S': /* Old style status, just signal only. */
5538 event->ws.kind = TARGET_WAITKIND_STOPPED;
5539 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5540 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5541 event->ws.value.sig = (enum gdb_signal) sig;
5543 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5546 case 'W': /* Target exited. */
5553 /* GDB used to accept only 2 hex chars here. Stubs should
5554 only send more if they detect GDB supports multi-process
5556 p = unpack_varlen_hex (&buf[1], &value);
5560 /* The remote process exited. */
5561 event->ws.kind = TARGET_WAITKIND_EXITED;
5562 event->ws.value.integer = value;
5566 /* The remote process exited with a signal. */
5567 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5568 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5569 event->ws.value.sig = (enum gdb_signal) value;
5571 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5574 /* If no process is specified, assume inferior_ptid. */
5575 pid = ptid_get_pid (inferior_ptid);
5584 else if (strncmp (p,
5585 "process:", sizeof ("process:") - 1) == 0)
5589 p += sizeof ("process:") - 1;
5590 unpack_varlen_hex (p, &upid);
5594 error (_("unknown stop reply packet: %s"), buf);
5597 error (_("unknown stop reply packet: %s"), buf);
5598 event->ptid = pid_to_ptid (pid);
5603 if (non_stop && ptid_equal (event->ptid, null_ptid))
5604 error (_("No process or thread specified in stop reply: %s"), buf);
5607 /* When the stub wants to tell GDB about a new notification reply, it
5608 sends a notification (%Stop, for example). Those can come it at
5609 any time, hence, we have to make sure that any pending
5610 putpkt/getpkt sequence we're making is finished, before querying
5611 the stub for more events with the corresponding ack command
5612 (vStopped, for example). E.g., if we started a vStopped sequence
5613 immediately upon receiving the notification, something like this
5621 1.6) <-- (registers reply to step #1.3)
5623 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5626 To solve this, whenever we parse a %Stop notification successfully,
5627 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5628 doing whatever we were doing:
5634 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5635 2.5) <-- (registers reply to step #2.3)
5637 Eventualy after step #2.5, we return to the event loop, which
5638 notices there's an event on the
5639 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5640 associated callback --- the function below. At this point, we're
5641 always safe to start a vStopped sequence. :
5644 2.7) <-- T05 thread:2
5650 remote_notif_get_pending_events (struct notif_client *nc)
5652 struct remote_state *rs = get_remote_state ();
5654 if (rs->notif_state->pending_event[nc->id] != NULL)
5657 fprintf_unfiltered (gdb_stdlog,
5658 "notif: process: '%s' ack pending event\n",
5662 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
5663 rs->notif_state->pending_event[nc->id] = NULL;
5667 getpkt (&rs->buf, &rs->buf_size, 0);
5668 if (strcmp (rs->buf, "OK") == 0)
5671 remote_notif_ack (nc, rs->buf);
5677 fprintf_unfiltered (gdb_stdlog,
5678 "notif: process: '%s' no pending reply\n",
5683 /* Called when it is decided that STOP_REPLY holds the info of the
5684 event that is to be returned to the core. This function always
5685 destroys STOP_REPLY. */
5688 process_stop_reply (struct stop_reply *stop_reply,
5689 struct target_waitstatus *status)
5693 *status = stop_reply->ws;
5694 ptid = stop_reply->ptid;
5696 /* If no thread/process was reported by the stub, assume the current
5698 if (ptid_equal (ptid, null_ptid))
5699 ptid = inferior_ptid;
5701 if (status->kind != TARGET_WAITKIND_EXITED
5702 && status->kind != TARGET_WAITKIND_SIGNALLED)
5704 struct remote_state *rs = get_remote_state ();
5706 /* Expedited registers. */
5707 if (stop_reply->regcache)
5709 struct regcache *regcache
5710 = get_thread_arch_regcache (ptid, target_gdbarch ());
5715 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5717 regcache_raw_supply (regcache, reg->num, reg->data);
5718 VEC_free (cached_reg_t, stop_reply->regcache);
5721 rs->remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5722 rs->remote_watch_data_address = stop_reply->watch_data_address;
5724 remote_notice_new_inferior (ptid, 0);
5725 demand_private_info (ptid)->core = stop_reply->core;
5728 stop_reply_xfree (stop_reply);
5732 /* The non-stop mode version of target_wait. */
5735 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
5737 struct remote_state *rs = get_remote_state ();
5738 struct stop_reply *stop_reply;
5742 /* If in non-stop mode, get out of getpkt even if a
5743 notification is received. */
5745 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5746 0 /* forever */, &is_notif);
5749 if (ret != -1 && !is_notif)
5752 case 'E': /* Error of some sort. */
5753 /* We're out of sync with the target now. Did it continue
5754 or not? We can't tell which thread it was in non-stop,
5755 so just ignore this. */
5756 warning (_("Remote failure reply: %s"), rs->buf);
5758 case 'O': /* Console output. */
5759 remote_console_output (rs->buf + 1);
5762 warning (_("Invalid remote reply: %s"), rs->buf);
5766 /* Acknowledge a pending stop reply that may have arrived in the
5768 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
5769 remote_notif_get_pending_events (¬if_client_stop);
5771 /* If indeed we noticed a stop reply, we're done. */
5772 stop_reply = queued_stop_reply (ptid);
5773 if (stop_reply != NULL)
5774 return process_stop_reply (stop_reply, status);
5776 /* Still no event. If we're just polling for an event, then
5777 return to the event loop. */
5778 if (options & TARGET_WNOHANG)
5780 status->kind = TARGET_WAITKIND_IGNORE;
5781 return minus_one_ptid;
5784 /* Otherwise do a blocking wait. */
5785 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5786 1 /* forever */, &is_notif);
5790 /* Wait until the remote machine stops, then return, storing status in
5791 STATUS just as `wait' would. */
5794 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
5796 struct remote_state *rs = get_remote_state ();
5797 ptid_t event_ptid = null_ptid;
5799 struct stop_reply *stop_reply;
5803 status->kind = TARGET_WAITKIND_IGNORE;
5804 status->value.integer = 0;
5806 stop_reply = queued_stop_reply (ptid);
5807 if (stop_reply != NULL)
5808 return process_stop_reply (stop_reply, status);
5810 if (rs->cached_wait_status)
5811 /* Use the cached wait status, but only once. */
5812 rs->cached_wait_status = 0;
5818 if (!target_is_async_p ())
5820 ofunc = signal (SIGINT, sync_remote_interrupt);
5821 /* If the user hit C-c before this packet, or between packets,
5822 pretend that it was hit right here. */
5823 if (check_quit_flag ())
5826 sync_remote_interrupt (SIGINT);
5830 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5831 _never_ wait for ever -> test on target_is_async_p().
5832 However, before we do that we need to ensure that the caller
5833 knows how to take the target into/out of async mode. */
5834 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5835 wait_forever_enabled_p, &is_notif);
5837 if (!target_is_async_p ())
5838 signal (SIGINT, ofunc);
5840 /* GDB gets a notification. Return to core as this event is
5842 if (ret != -1 && is_notif)
5843 return minus_one_ptid;
5848 rs->remote_stopped_by_watchpoint_p = 0;
5850 /* We got something. */
5851 rs->waiting_for_stop_reply = 0;
5853 /* Assume that the target has acknowledged Ctrl-C unless we receive
5854 an 'F' or 'O' packet. */
5855 if (buf[0] != 'F' && buf[0] != 'O')
5856 rs->ctrlc_pending_p = 0;
5860 case 'E': /* Error of some sort. */
5861 /* We're out of sync with the target now. Did it continue or
5862 not? Not is more likely, so report a stop. */
5863 warning (_("Remote failure reply: %s"), buf);
5864 status->kind = TARGET_WAITKIND_STOPPED;
5865 status->value.sig = GDB_SIGNAL_0;
5867 case 'F': /* File-I/O request. */
5868 remote_fileio_request (buf, rs->ctrlc_pending_p);
5869 rs->ctrlc_pending_p = 0;
5871 case 'T': case 'S': case 'X': case 'W':
5873 struct stop_reply *stop_reply
5874 = (struct stop_reply *) remote_notif_parse (¬if_client_stop,
5877 event_ptid = process_stop_reply (stop_reply, status);
5880 case 'O': /* Console output. */
5881 remote_console_output (buf + 1);
5883 /* The target didn't really stop; keep waiting. */
5884 rs->waiting_for_stop_reply = 1;
5888 if (rs->last_sent_signal != GDB_SIGNAL_0)
5890 /* Zero length reply means that we tried 'S' or 'C' and the
5891 remote system doesn't support it. */
5892 target_terminal_ours_for_output ();
5894 ("Can't send signals to this remote system. %s not sent.\n",
5895 gdb_signal_to_name (rs->last_sent_signal));
5896 rs->last_sent_signal = GDB_SIGNAL_0;
5897 target_terminal_inferior ();
5899 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
5900 putpkt ((char *) buf);
5902 /* We just told the target to resume, so a stop reply is in
5904 rs->waiting_for_stop_reply = 1;
5907 /* else fallthrough */
5909 warning (_("Invalid remote reply: %s"), buf);
5911 rs->waiting_for_stop_reply = 1;
5915 if (status->kind == TARGET_WAITKIND_IGNORE)
5917 /* Nothing interesting happened. If we're doing a non-blocking
5918 poll, we're done. Otherwise, go back to waiting. */
5919 if (options & TARGET_WNOHANG)
5920 return minus_one_ptid;
5924 else if (status->kind != TARGET_WAITKIND_EXITED
5925 && status->kind != TARGET_WAITKIND_SIGNALLED)
5927 if (!ptid_equal (event_ptid, null_ptid))
5928 record_currthread (rs, event_ptid);
5930 event_ptid = inferior_ptid;
5933 /* A process exit. Invalidate our notion of current thread. */
5934 record_currthread (rs, minus_one_ptid);
5939 /* Wait until the remote machine stops, then return, storing status in
5940 STATUS just as `wait' would. */
5943 remote_wait (struct target_ops *ops,
5944 ptid_t ptid, struct target_waitstatus *status, int options)
5949 event_ptid = remote_wait_ns (ptid, status, options);
5951 event_ptid = remote_wait_as (ptid, status, options);
5953 if (target_can_async_p ())
5955 /* If there are are events left in the queue tell the event loop
5957 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5958 mark_async_event_handler (remote_async_inferior_event_token);
5964 /* Fetch a single register using a 'p' packet. */
5967 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
5969 struct remote_state *rs = get_remote_state ();
5971 char regp[MAX_REGISTER_SIZE];
5974 if (packet_support (PACKET_p) == PACKET_DISABLE)
5977 if (reg->pnum == -1)
5982 p += hexnumstr (p, reg->pnum);
5985 getpkt (&rs->buf, &rs->buf_size, 0);
5989 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
5993 case PACKET_UNKNOWN:
5996 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5997 gdbarch_register_name (get_regcache_arch (regcache),
6002 /* If this register is unfetchable, tell the regcache. */
6005 regcache_raw_supply (regcache, reg->regnum, NULL);
6009 /* Otherwise, parse and supply the value. */
6015 error (_("fetch_register_using_p: early buf termination"));
6017 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6020 regcache_raw_supply (regcache, reg->regnum, regp);
6024 /* Fetch the registers included in the target's 'g' packet. */
6027 send_g_packet (void)
6029 struct remote_state *rs = get_remote_state ();
6032 xsnprintf (rs->buf, get_remote_packet_size (), "g");
6033 remote_send (&rs->buf, &rs->buf_size);
6035 /* We can get out of synch in various cases. If the first character
6036 in the buffer is not a hex character, assume that has happened
6037 and try to fetch another packet to read. */
6038 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6039 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6040 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6041 && rs->buf[0] != 'x') /* New: unavailable register value. */
6044 fprintf_unfiltered (gdb_stdlog,
6045 "Bad register packet; fetching a new packet\n");
6046 getpkt (&rs->buf, &rs->buf_size, 0);
6049 buf_len = strlen (rs->buf);
6051 /* Sanity check the received packet. */
6052 if (buf_len % 2 != 0)
6053 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
6059 process_g_packet (struct regcache *regcache)
6061 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6062 struct remote_state *rs = get_remote_state ();
6063 struct remote_arch_state *rsa = get_remote_arch_state ();
6068 buf_len = strlen (rs->buf);
6070 /* Further sanity checks, with knowledge of the architecture. */
6071 if (buf_len > 2 * rsa->sizeof_g_packet)
6072 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6074 /* Save the size of the packet sent to us by the target. It is used
6075 as a heuristic when determining the max size of packets that the
6076 target can safely receive. */
6077 if (rsa->actual_register_packet_size == 0)
6078 rsa->actual_register_packet_size = buf_len;
6080 /* If this is smaller than we guessed the 'g' packet would be,
6081 update our records. A 'g' reply that doesn't include a register's
6082 value implies either that the register is not available, or that
6083 the 'p' packet must be used. */
6084 if (buf_len < 2 * rsa->sizeof_g_packet)
6086 rsa->sizeof_g_packet = buf_len / 2;
6088 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6090 if (rsa->regs[i].pnum == -1)
6093 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6094 rsa->regs[i].in_g_packet = 0;
6096 rsa->regs[i].in_g_packet = 1;
6100 regs = alloca (rsa->sizeof_g_packet);
6102 /* Unimplemented registers read as all bits zero. */
6103 memset (regs, 0, rsa->sizeof_g_packet);
6105 /* Reply describes registers byte by byte, each byte encoded as two
6106 hex characters. Suck them all up, then supply them to the
6107 register cacheing/storage mechanism. */
6110 for (i = 0; i < rsa->sizeof_g_packet; i++)
6112 if (p[0] == 0 || p[1] == 0)
6113 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6114 internal_error (__FILE__, __LINE__,
6115 _("unexpected end of 'g' packet reply"));
6117 if (p[0] == 'x' && p[1] == 'x')
6118 regs[i] = 0; /* 'x' */
6120 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6124 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6126 struct packet_reg *r = &rsa->regs[i];
6130 if (r->offset * 2 >= strlen (rs->buf))
6131 /* This shouldn't happen - we adjusted in_g_packet above. */
6132 internal_error (__FILE__, __LINE__,
6133 _("unexpected end of 'g' packet reply"));
6134 else if (rs->buf[r->offset * 2] == 'x')
6136 gdb_assert (r->offset * 2 < strlen (rs->buf));
6137 /* The register isn't available, mark it as such (at
6138 the same time setting the value to zero). */
6139 regcache_raw_supply (regcache, r->regnum, NULL);
6142 regcache_raw_supply (regcache, r->regnum,
6149 fetch_registers_using_g (struct regcache *regcache)
6152 process_g_packet (regcache);
6155 /* Make the remote selected traceframe match GDB's selected
6159 set_remote_traceframe (void)
6162 struct remote_state *rs = get_remote_state ();
6164 if (rs->remote_traceframe_number == get_traceframe_number ())
6167 /* Avoid recursion, remote_trace_find calls us again. */
6168 rs->remote_traceframe_number = get_traceframe_number ();
6170 newnum = target_trace_find (tfind_number,
6171 get_traceframe_number (), 0, 0, NULL);
6173 /* Should not happen. If it does, all bets are off. */
6174 if (newnum != get_traceframe_number ())
6175 warning (_("could not set remote traceframe"));
6179 remote_fetch_registers (struct target_ops *ops,
6180 struct regcache *regcache, int regnum)
6182 struct remote_arch_state *rsa = get_remote_arch_state ();
6185 set_remote_traceframe ();
6186 set_general_thread (inferior_ptid);
6190 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6192 gdb_assert (reg != NULL);
6194 /* If this register might be in the 'g' packet, try that first -
6195 we are likely to read more than one register. If this is the
6196 first 'g' packet, we might be overly optimistic about its
6197 contents, so fall back to 'p'. */
6198 if (reg->in_g_packet)
6200 fetch_registers_using_g (regcache);
6201 if (reg->in_g_packet)
6205 if (fetch_register_using_p (regcache, reg))
6208 /* This register is not available. */
6209 regcache_raw_supply (regcache, reg->regnum, NULL);
6214 fetch_registers_using_g (regcache);
6216 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6217 if (!rsa->regs[i].in_g_packet)
6218 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
6220 /* This register is not available. */
6221 regcache_raw_supply (regcache, i, NULL);
6225 /* Prepare to store registers. Since we may send them all (using a
6226 'G' request), we have to read out the ones we don't want to change
6230 remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
6232 struct remote_arch_state *rsa = get_remote_arch_state ();
6234 gdb_byte buf[MAX_REGISTER_SIZE];
6236 /* Make sure the entire registers array is valid. */
6237 switch (packet_support (PACKET_P))
6239 case PACKET_DISABLE:
6240 case PACKET_SUPPORT_UNKNOWN:
6241 /* Make sure all the necessary registers are cached. */
6242 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6243 if (rsa->regs[i].in_g_packet)
6244 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
6251 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6252 packet was not recognized. */
6255 store_register_using_P (const struct regcache *regcache,
6256 struct packet_reg *reg)
6258 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6259 struct remote_state *rs = get_remote_state ();
6260 /* Try storing a single register. */
6261 char *buf = rs->buf;
6262 gdb_byte regp[MAX_REGISTER_SIZE];
6265 if (packet_support (PACKET_P) == PACKET_DISABLE)
6268 if (reg->pnum == -1)
6271 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
6272 p = buf + strlen (buf);
6273 regcache_raw_collect (regcache, reg->regnum, regp);
6274 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6276 getpkt (&rs->buf, &rs->buf_size, 0);
6278 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6283 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6284 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
6285 case PACKET_UNKNOWN:
6288 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6292 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6293 contents of the register cache buffer. FIXME: ignores errors. */
6296 store_registers_using_G (const struct regcache *regcache)
6298 struct remote_state *rs = get_remote_state ();
6299 struct remote_arch_state *rsa = get_remote_arch_state ();
6303 /* Extract all the registers in the regcache copying them into a
6308 regs = alloca (rsa->sizeof_g_packet);
6309 memset (regs, 0, rsa->sizeof_g_packet);
6310 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6312 struct packet_reg *r = &rsa->regs[i];
6315 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
6319 /* Command describes registers byte by byte,
6320 each byte encoded as two hex characters. */
6323 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6325 bin2hex (regs, p, rsa->sizeof_g_packet);
6327 getpkt (&rs->buf, &rs->buf_size, 0);
6328 if (packet_check_result (rs->buf) == PACKET_ERROR)
6329 error (_("Could not write registers; remote failure reply '%s'"),
6333 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6334 of the register cache buffer. FIXME: ignores errors. */
6337 remote_store_registers (struct target_ops *ops,
6338 struct regcache *regcache, int regnum)
6340 struct remote_arch_state *rsa = get_remote_arch_state ();
6343 set_remote_traceframe ();
6344 set_general_thread (inferior_ptid);
6348 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6350 gdb_assert (reg != NULL);
6352 /* Always prefer to store registers using the 'P' packet if
6353 possible; we often change only a small number of registers.
6354 Sometimes we change a larger number; we'd need help from a
6355 higher layer to know to use 'G'. */
6356 if (store_register_using_P (regcache, reg))
6359 /* For now, don't complain if we have no way to write the
6360 register. GDB loses track of unavailable registers too
6361 easily. Some day, this may be an error. We don't have
6362 any way to read the register, either... */
6363 if (!reg->in_g_packet)
6366 store_registers_using_G (regcache);
6370 store_registers_using_G (regcache);
6372 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6373 if (!rsa->regs[i].in_g_packet)
6374 if (!store_register_using_P (regcache, &rsa->regs[i]))
6375 /* See above for why we do not issue an error here. */
6380 /* Return the number of hex digits in num. */
6383 hexnumlen (ULONGEST num)
6387 for (i = 0; num != 0; i++)
6393 /* Set BUF to the minimum number of hex digits representing NUM. */
6396 hexnumstr (char *buf, ULONGEST num)
6398 int len = hexnumlen (num);
6400 return hexnumnstr (buf, num, len);
6404 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6407 hexnumnstr (char *buf, ULONGEST num, int width)
6413 for (i = width - 1; i >= 0; i--)
6415 buf[i] = "0123456789abcdef"[(num & 0xf)];
6422 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6425 remote_address_masked (CORE_ADDR addr)
6427 unsigned int address_size = remote_address_size;
6429 /* If "remoteaddresssize" was not set, default to target address size. */
6431 address_size = gdbarch_addr_bit (target_gdbarch ());
6433 if (address_size > 0
6434 && address_size < (sizeof (ULONGEST) * 8))
6436 /* Only create a mask when that mask can safely be constructed
6437 in a ULONGEST variable. */
6440 mask = (mask << address_size) - 1;
6446 /* Determine whether the remote target supports binary downloading.
6447 This is accomplished by sending a no-op memory write of zero length
6448 to the target at the specified address. It does not suffice to send
6449 the whole packet, since many stubs strip the eighth bit and
6450 subsequently compute a wrong checksum, which causes real havoc with
6453 NOTE: This can still lose if the serial line is not eight-bit
6454 clean. In cases like this, the user should clear "remote
6458 check_binary_download (CORE_ADDR addr)
6460 struct remote_state *rs = get_remote_state ();
6462 switch (packet_support (PACKET_X))
6464 case PACKET_DISABLE:
6468 case PACKET_SUPPORT_UNKNOWN:
6474 p += hexnumstr (p, (ULONGEST) addr);
6476 p += hexnumstr (p, (ULONGEST) 0);
6480 putpkt_binary (rs->buf, (int) (p - rs->buf));
6481 getpkt (&rs->buf, &rs->buf_size, 0);
6483 if (rs->buf[0] == '\0')
6486 fprintf_unfiltered (gdb_stdlog,
6487 "binary downloading NOT "
6488 "supported by target\n");
6489 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6494 fprintf_unfiltered (gdb_stdlog,
6495 "binary downloading supported by target\n");
6496 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6503 /* Write memory data directly to the remote machine.
6504 This does not inform the data cache; the data cache uses this.
6505 HEADER is the starting part of the packet.
6506 MEMADDR is the address in the remote memory space.
6507 MYADDR is the address of the buffer in our space.
6508 LEN is the number of bytes.
6509 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6510 should send data as binary ('X'), or hex-encoded ('M').
6512 The function creates packet of the form
6513 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6515 where encoding of <DATA> is termined by PACKET_FORMAT.
6517 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6520 Return the transferred status, error or OK (an
6521 'enum target_xfer_status' value). Save the number of bytes
6522 transferred in *XFERED_LEN. Only transfer a single packet. */
6524 static enum target_xfer_status
6525 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6526 const gdb_byte *myaddr, ULONGEST len,
6527 ULONGEST *xfered_len, char packet_format,
6530 struct remote_state *rs = get_remote_state ();
6540 if (packet_format != 'X' && packet_format != 'M')
6541 internal_error (__FILE__, __LINE__,
6542 _("remote_write_bytes_aux: bad packet format"));
6545 return TARGET_XFER_EOF;
6547 payload_size = get_memory_write_packet_size ();
6549 /* The packet buffer will be large enough for the payload;
6550 get_memory_packet_size ensures this. */
6553 /* Compute the size of the actual payload by subtracting out the
6554 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6556 payload_size -= strlen ("$,:#NN");
6558 /* The comma won't be used. */
6560 header_length = strlen (header);
6561 payload_size -= header_length;
6562 payload_size -= hexnumlen (memaddr);
6564 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6566 strcat (rs->buf, header);
6567 p = rs->buf + strlen (header);
6569 /* Compute a best guess of the number of bytes actually transfered. */
6570 if (packet_format == 'X')
6572 /* Best guess at number of bytes that will fit. */
6573 todo = min (len, payload_size);
6575 payload_size -= hexnumlen (todo);
6576 todo = min (todo, payload_size);
6580 /* Num bytes that will fit. */
6581 todo = min (len, payload_size / 2);
6583 payload_size -= hexnumlen (todo);
6584 todo = min (todo, payload_size / 2);
6588 internal_error (__FILE__, __LINE__,
6589 _("minimum packet size too small to write data"));
6591 /* If we already need another packet, then try to align the end
6592 of this packet to a useful boundary. */
6593 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6594 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6596 /* Append "<memaddr>". */
6597 memaddr = remote_address_masked (memaddr);
6598 p += hexnumstr (p, (ULONGEST) memaddr);
6605 /* Append <len>. Retain the location/size of <len>. It may need to
6606 be adjusted once the packet body has been created. */
6608 plenlen = hexnumstr (p, (ULONGEST) todo);
6616 /* Append the packet body. */
6617 if (packet_format == 'X')
6619 /* Binary mode. Send target system values byte by byte, in
6620 increasing byte addresses. Only escape certain critical
6622 payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p,
6623 &nr_bytes, payload_size);
6625 /* If not all TODO bytes fit, then we'll need another packet. Make
6626 a second try to keep the end of the packet aligned. Don't do
6627 this if the packet is tiny. */
6628 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6632 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6634 if (new_nr_bytes != nr_bytes)
6635 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6636 (gdb_byte *) p, &nr_bytes,
6640 p += payload_length;
6641 if (use_length && nr_bytes < todo)
6643 /* Escape chars have filled up the buffer prematurely,
6644 and we have actually sent fewer bytes than planned.
6645 Fix-up the length field of the packet. Use the same
6646 number of characters as before. */
6647 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6648 *plen = ':'; /* overwrite \0 from hexnumnstr() */
6653 /* Normal mode: Send target system values byte by byte, in
6654 increasing byte addresses. Each byte is encoded as a two hex
6656 nr_bytes = bin2hex (myaddr, p, todo);
6660 putpkt_binary (rs->buf, (int) (p - rs->buf));
6661 getpkt (&rs->buf, &rs->buf_size, 0);
6663 if (rs->buf[0] == 'E')
6664 return TARGET_XFER_E_IO;
6666 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6667 fewer bytes than we'd planned. */
6668 *xfered_len = (ULONGEST) nr_bytes;
6669 return TARGET_XFER_OK;
6672 /* Write memory data directly to the remote machine.
6673 This does not inform the data cache; the data cache uses this.
6674 MEMADDR is the address in the remote memory space.
6675 MYADDR is the address of the buffer in our space.
6676 LEN is the number of bytes.
6678 Return the transferred status, error or OK (an
6679 'enum target_xfer_status' value). Save the number of bytes
6680 transferred in *XFERED_LEN. Only transfer a single packet. */
6682 static enum target_xfer_status
6683 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
6684 ULONGEST *xfered_len)
6686 char *packet_format = 0;
6688 /* Check whether the target supports binary download. */
6689 check_binary_download (memaddr);
6691 switch (packet_support (PACKET_X))
6694 packet_format = "X";
6696 case PACKET_DISABLE:
6697 packet_format = "M";
6699 case PACKET_SUPPORT_UNKNOWN:
6700 internal_error (__FILE__, __LINE__,
6701 _("remote_write_bytes: bad internal state"));
6703 internal_error (__FILE__, __LINE__, _("bad switch"));
6706 return remote_write_bytes_aux (packet_format,
6707 memaddr, myaddr, len, xfered_len,
6708 packet_format[0], 1);
6711 /* Read memory data directly from the remote machine.
6712 This does not use the data cache; the data cache uses this.
6713 MEMADDR is the address in the remote memory space.
6714 MYADDR is the address of the buffer in our space.
6715 LEN is the number of bytes.
6717 Return the transferred status, error or OK (an
6718 'enum target_xfer_status' value). Save the number of bytes
6719 transferred in *XFERED_LEN. */
6721 static enum target_xfer_status
6722 remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len,
6723 ULONGEST *xfered_len)
6725 struct remote_state *rs = get_remote_state ();
6726 int max_buf_size; /* Max size of packet output buffer. */
6731 max_buf_size = get_memory_read_packet_size ();
6732 /* The packet buffer will be large enough for the payload;
6733 get_memory_packet_size ensures this. */
6735 /* Number if bytes that will fit. */
6736 todo = min (len, max_buf_size / 2);
6738 /* Construct "m"<memaddr>","<len>". */
6739 memaddr = remote_address_masked (memaddr);
6742 p += hexnumstr (p, (ULONGEST) memaddr);
6744 p += hexnumstr (p, (ULONGEST) todo);
6747 getpkt (&rs->buf, &rs->buf_size, 0);
6748 if (rs->buf[0] == 'E'
6749 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6750 && rs->buf[3] == '\0')
6751 return TARGET_XFER_E_IO;
6752 /* Reply describes memory byte by byte, each byte encoded as two hex
6755 i = hex2bin (p, myaddr, todo);
6756 /* Return what we have. Let higher layers handle partial reads. */
6757 *xfered_len = (ULONGEST) i;
6758 return TARGET_XFER_OK;
6761 /* Using the set of read-only target sections of remote, read live
6764 For interface/parameters/return description see target.h,
6767 static enum target_xfer_status
6768 remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
6769 ULONGEST memaddr, ULONGEST len,
6770 ULONGEST *xfered_len)
6772 struct target_section *secp;
6773 struct target_section_table *table;
6775 secp = target_section_by_addr (ops, memaddr);
6777 && (bfd_get_section_flags (secp->the_bfd_section->owner,
6778 secp->the_bfd_section)
6781 struct target_section *p;
6782 ULONGEST memend = memaddr + len;
6784 table = target_get_section_table (ops);
6786 for (p = table->sections; p < table->sections_end; p++)
6788 if (memaddr >= p->addr)
6790 if (memend <= p->endaddr)
6792 /* Entire transfer is within this section. */
6793 return remote_read_bytes_1 (memaddr, readbuf, len,
6796 else if (memaddr >= p->endaddr)
6798 /* This section ends before the transfer starts. */
6803 /* This section overlaps the transfer. Just do half. */
6804 len = p->endaddr - memaddr;
6805 return remote_read_bytes_1 (memaddr, readbuf, len,
6812 return TARGET_XFER_EOF;
6815 /* Similar to remote_read_bytes_1, but it reads from the remote stub
6816 first if the requested memory is unavailable in traceframe.
6817 Otherwise, fall back to remote_read_bytes_1. */
6819 static enum target_xfer_status
6820 remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
6821 gdb_byte *myaddr, ULONGEST len, ULONGEST *xfered_len)
6824 return TARGET_XFER_EOF;
6826 if (get_traceframe_number () != -1)
6828 VEC(mem_range_s) *available;
6830 /* If we fail to get the set of available memory, then the
6831 target does not support querying traceframe info, and so we
6832 attempt reading from the traceframe anyway (assuming the
6833 target implements the old QTro packet then). */
6834 if (traceframe_available_memory (&available, memaddr, len))
6836 struct cleanup *old_chain;
6838 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
6840 if (VEC_empty (mem_range_s, available)
6841 || VEC_index (mem_range_s, available, 0)->start != memaddr)
6843 enum target_xfer_status res;
6845 /* Don't read into the traceframe's available
6847 if (!VEC_empty (mem_range_s, available))
6849 LONGEST oldlen = len;
6851 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
6852 gdb_assert (len <= oldlen);
6855 do_cleanups (old_chain);
6857 /* This goes through the topmost target again. */
6858 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
6860 if (res == TARGET_XFER_OK)
6861 return TARGET_XFER_OK;
6864 /* No use trying further, we know some memory starting
6865 at MEMADDR isn't available. */
6867 return TARGET_XFER_UNAVAILABLE;
6871 /* Don't try to read more than how much is available, in
6872 case the target implements the deprecated QTro packet to
6873 cater for older GDBs (the target's knowledge of read-only
6874 sections may be outdated by now). */
6875 len = VEC_index (mem_range_s, available, 0)->length;
6877 do_cleanups (old_chain);
6881 return remote_read_bytes_1 (memaddr, myaddr, len, xfered_len);
6886 /* Sends a packet with content determined by the printf format string
6887 FORMAT and the remaining arguments, then gets the reply. Returns
6888 whether the packet was a success, a failure, or unknown. */
6890 static enum packet_result
6891 remote_send_printf (const char *format, ...)
6893 struct remote_state *rs = get_remote_state ();
6894 int max_size = get_remote_packet_size ();
6897 va_start (ap, format);
6900 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
6901 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
6903 if (putpkt (rs->buf) < 0)
6904 error (_("Communication problem with target."));
6907 getpkt (&rs->buf, &rs->buf_size, 0);
6909 return packet_check_result (rs->buf);
6913 restore_remote_timeout (void *p)
6915 int value = *(int *)p;
6917 remote_timeout = value;
6920 /* Flash writing can take quite some time. We'll set
6921 effectively infinite timeout for flash operations.
6922 In future, we'll need to decide on a better approach. */
6923 static const int remote_flash_timeout = 1000;
6926 remote_flash_erase (struct target_ops *ops,
6927 ULONGEST address, LONGEST length)
6929 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
6930 int saved_remote_timeout = remote_timeout;
6931 enum packet_result ret;
6932 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6933 &saved_remote_timeout);
6935 remote_timeout = remote_flash_timeout;
6937 ret = remote_send_printf ("vFlashErase:%s,%s",
6938 phex (address, addr_size),
6942 case PACKET_UNKNOWN:
6943 error (_("Remote target does not support flash erase"));
6945 error (_("Error erasing flash with vFlashErase packet"));
6950 do_cleanups (back_to);
6953 static enum target_xfer_status
6954 remote_flash_write (struct target_ops *ops, ULONGEST address,
6955 ULONGEST length, ULONGEST *xfered_len,
6956 const gdb_byte *data)
6958 int saved_remote_timeout = remote_timeout;
6959 enum target_xfer_status ret;
6960 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6961 &saved_remote_timeout);
6963 remote_timeout = remote_flash_timeout;
6964 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length,
6966 do_cleanups (back_to);
6972 remote_flash_done (struct target_ops *ops)
6974 int saved_remote_timeout = remote_timeout;
6976 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6977 &saved_remote_timeout);
6979 remote_timeout = remote_flash_timeout;
6980 ret = remote_send_printf ("vFlashDone");
6981 do_cleanups (back_to);
6985 case PACKET_UNKNOWN:
6986 error (_("Remote target does not support vFlashDone"));
6988 error (_("Error finishing flash operation"));
6995 remote_files_info (struct target_ops *ignore)
6997 puts_filtered ("Debugging a target over a serial line.\n");
7000 /* Stuff for dealing with the packets which are part of this protocol.
7001 See comment at top of file for details. */
7003 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7004 error to higher layers. Called when a serial error is detected.
7005 The exception message is STRING, followed by a colon and a blank,
7006 the system error message for errno at function entry and final dot
7007 for output compatibility with throw_perror_with_name. */
7010 unpush_and_perror (const char *string)
7012 int saved_errno = errno;
7014 remote_unpush_target ();
7015 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7016 safe_strerror (saved_errno));
7019 /* Read a single character from the remote end. */
7022 readchar (int timeout)
7025 struct remote_state *rs = get_remote_state ();
7027 ch = serial_readchar (rs->remote_desc, timeout);
7032 switch ((enum serial_rc) ch)
7035 remote_unpush_target ();
7036 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
7039 unpush_and_perror (_("Remote communication error. "
7040 "Target disconnected."));
7042 case SERIAL_TIMEOUT:
7048 /* Wrapper for serial_write that closes the target and throws if
7052 remote_serial_write (const char *str, int len)
7054 struct remote_state *rs = get_remote_state ();
7056 if (serial_write (rs->remote_desc, str, len))
7058 unpush_and_perror (_("Remote communication error. "
7059 "Target disconnected."));
7063 /* Send the command in *BUF to the remote machine, and read the reply
7064 into *BUF. Report an error if we get an error reply. Resize
7065 *BUF using xrealloc if necessary to hold the result, and update
7069 remote_send (char **buf,
7073 getpkt (buf, sizeof_buf, 0);
7075 if ((*buf)[0] == 'E')
7076 error (_("Remote failure reply: %s"), *buf);
7079 /* Return a pointer to an xmalloc'ed string representing an escaped
7080 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7081 etc. The caller is responsible for releasing the returned
7085 escape_buffer (const char *buf, int n)
7087 struct cleanup *old_chain;
7088 struct ui_file *stb;
7091 stb = mem_fileopen ();
7092 old_chain = make_cleanup_ui_file_delete (stb);
7094 fputstrn_unfiltered (buf, n, '\\', stb);
7095 str = ui_file_xstrdup (stb, NULL);
7096 do_cleanups (old_chain);
7100 /* Display a null-terminated packet on stdout, for debugging, using C
7104 print_packet (const char *buf)
7106 puts_filtered ("\"");
7107 fputstr_filtered (buf, '"', gdb_stdout);
7108 puts_filtered ("\"");
7112 putpkt (const char *buf)
7114 return putpkt_binary (buf, strlen (buf));
7117 /* Send a packet to the remote machine, with error checking. The data
7118 of the packet is in BUF. The string in BUF can be at most
7119 get_remote_packet_size () - 5 to account for the $, # and checksum,
7120 and for a possible /0 if we are debugging (remote_debug) and want
7121 to print the sent packet as a string. */
7124 putpkt_binary (const char *buf, int cnt)
7126 struct remote_state *rs = get_remote_state ();
7128 unsigned char csum = 0;
7129 char *buf2 = alloca (cnt + 6);
7136 /* Catch cases like trying to read memory or listing threads while
7137 we're waiting for a stop reply. The remote server wouldn't be
7138 ready to handle this request, so we'd hang and timeout. We don't
7139 have to worry about this in synchronous mode, because in that
7140 case it's not possible to issue a command while the target is
7141 running. This is not a problem in non-stop mode, because in that
7142 case, the stub is always ready to process serial input. */
7143 if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
7145 error (_("Cannot execute this command while the target is running.\n"
7146 "Use the \"interrupt\" command to stop the target\n"
7147 "and then try again."));
7150 /* We're sending out a new packet. Make sure we don't look at a
7151 stale cached response. */
7152 rs->cached_wait_status = 0;
7154 /* Copy the packet into buffer BUF2, encapsulating it
7155 and giving it a checksum. */
7160 for (i = 0; i < cnt; i++)
7166 *p++ = tohex ((csum >> 4) & 0xf);
7167 *p++ = tohex (csum & 0xf);
7169 /* Send it over and over until we get a positive ack. */
7173 int started_error_output = 0;
7177 struct cleanup *old_chain;
7181 str = escape_buffer (buf2, p - buf2);
7182 old_chain = make_cleanup (xfree, str);
7183 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
7184 gdb_flush (gdb_stdlog);
7185 do_cleanups (old_chain);
7187 remote_serial_write (buf2, p - buf2);
7189 /* If this is a no acks version of the remote protocol, send the
7190 packet and move on. */
7194 /* Read until either a timeout occurs (-2) or '+' is read.
7195 Handle any notification that arrives in the mean time. */
7198 ch = readchar (remote_timeout);
7206 case SERIAL_TIMEOUT:
7209 if (started_error_output)
7211 putchar_unfiltered ('\n');
7212 started_error_output = 0;
7221 fprintf_unfiltered (gdb_stdlog, "Ack\n");
7225 fprintf_unfiltered (gdb_stdlog, "Nak\n");
7227 case SERIAL_TIMEOUT:
7231 break; /* Retransmit buffer. */
7235 fprintf_unfiltered (gdb_stdlog,
7236 "Packet instead of Ack, ignoring it\n");
7237 /* It's probably an old response sent because an ACK
7238 was lost. Gobble up the packet and ack it so it
7239 doesn't get retransmitted when we resend this
7242 remote_serial_write ("+", 1);
7243 continue; /* Now, go look for +. */
7250 /* If we got a notification, handle it, and go back to looking
7252 /* We've found the start of a notification. Now
7253 collect the data. */
7254 val = read_frame (&rs->buf, &rs->buf_size);
7259 struct cleanup *old_chain;
7262 str = escape_buffer (rs->buf, val);
7263 old_chain = make_cleanup (xfree, str);
7264 fprintf_unfiltered (gdb_stdlog,
7265 " Notification received: %s\n",
7267 do_cleanups (old_chain);
7269 handle_notification (rs->notif_state, rs->buf);
7270 /* We're in sync now, rewait for the ack. */
7277 if (!started_error_output)
7279 started_error_output = 1;
7280 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7282 fputc_unfiltered (ch & 0177, gdb_stdlog);
7283 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7292 if (!started_error_output)
7294 started_error_output = 1;
7295 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7297 fputc_unfiltered (ch & 0177, gdb_stdlog);
7301 break; /* Here to retransmit. */
7305 /* This is wrong. If doing a long backtrace, the user should be
7306 able to get out next time we call QUIT, without anything as
7307 violent as interrupt_query. If we want to provide a way out of
7308 here without getting to the next QUIT, it should be based on
7309 hitting ^C twice as in remote_wait. */
7320 /* Come here after finding the start of a frame when we expected an
7321 ack. Do our best to discard the rest of this packet. */
7330 c = readchar (remote_timeout);
7333 case SERIAL_TIMEOUT:
7334 /* Nothing we can do. */
7337 /* Discard the two bytes of checksum and stop. */
7338 c = readchar (remote_timeout);
7340 c = readchar (remote_timeout);
7343 case '*': /* Run length encoding. */
7344 /* Discard the repeat count. */
7345 c = readchar (remote_timeout);
7350 /* A regular character. */
7356 /* Come here after finding the start of the frame. Collect the rest
7357 into *BUF, verifying the checksum, length, and handling run-length
7358 compression. NUL terminate the buffer. If there is not enough room,
7359 expand *BUF using xrealloc.
7361 Returns -1 on error, number of characters in buffer (ignoring the
7362 trailing NULL) on success. (could be extended to return one of the
7363 SERIAL status indications). */
7366 read_frame (char **buf_p,
7373 struct remote_state *rs = get_remote_state ();
7380 c = readchar (remote_timeout);
7383 case SERIAL_TIMEOUT:
7385 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
7389 fputs_filtered ("Saw new packet start in middle of old one\n",
7391 return -1; /* Start a new packet, count retries. */
7394 unsigned char pktcsum;
7400 check_0 = readchar (remote_timeout);
7402 check_1 = readchar (remote_timeout);
7404 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7407 fputs_filtered ("Timeout in checksum, retrying\n",
7411 else if (check_0 < 0 || check_1 < 0)
7414 fputs_filtered ("Communication error in checksum\n",
7419 /* Don't recompute the checksum; with no ack packets we
7420 don't have any way to indicate a packet retransmission
7425 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7426 if (csum == pktcsum)
7431 struct cleanup *old_chain;
7434 str = escape_buffer (buf, bc);
7435 old_chain = make_cleanup (xfree, str);
7436 fprintf_unfiltered (gdb_stdlog,
7437 "Bad checksum, sentsum=0x%x, "
7438 "csum=0x%x, buf=%s\n",
7439 pktcsum, csum, str);
7440 do_cleanups (old_chain);
7442 /* Number of characters in buffer ignoring trailing
7446 case '*': /* Run length encoding. */
7451 c = readchar (remote_timeout);
7453 repeat = c - ' ' + 3; /* Compute repeat count. */
7455 /* The character before ``*'' is repeated. */
7457 if (repeat > 0 && repeat <= 255 && bc > 0)
7459 if (bc + repeat - 1 >= *sizeof_buf - 1)
7461 /* Make some more room in the buffer. */
7462 *sizeof_buf += repeat;
7463 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7467 memset (&buf[bc], buf[bc - 1], repeat);
7473 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7477 if (bc >= *sizeof_buf - 1)
7479 /* Make some more room in the buffer. */
7481 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7492 /* Read a packet from the remote machine, with error checking, and
7493 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7494 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7495 rather than timing out; this is used (in synchronous mode) to wait
7496 for a target that is is executing user code to stop. */
7497 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7498 don't have to change all the calls to getpkt to deal with the
7499 return value, because at the moment I don't know what the right
7500 thing to do it for those. */
7508 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7512 /* Read a packet from the remote machine, with error checking, and
7513 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7514 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7515 rather than timing out; this is used (in synchronous mode) to wait
7516 for a target that is is executing user code to stop. If FOREVER ==
7517 0, this function is allowed to time out gracefully and return an
7518 indication of this to the caller. Otherwise return the number of
7519 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7520 enough reason to return to the caller. *IS_NOTIF is an output
7521 boolean that indicates whether *BUF holds a notification or not
7522 (a regular packet). */
7525 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7526 int expecting_notif, int *is_notif)
7528 struct remote_state *rs = get_remote_state ();
7534 /* We're reading a new response. Make sure we don't look at a
7535 previously cached response. */
7536 rs->cached_wait_status = 0;
7538 strcpy (*buf, "timeout");
7541 timeout = watchdog > 0 ? watchdog : -1;
7542 else if (expecting_notif)
7543 timeout = 0; /* There should already be a char in the buffer. If
7546 timeout = remote_timeout;
7550 /* Process any number of notifications, and then return when
7554 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
7556 for (tries = 1; tries <= MAX_TRIES; tries++)
7558 /* This can loop forever if the remote side sends us
7559 characters continuously, but if it pauses, we'll get
7560 SERIAL_TIMEOUT from readchar because of timeout. Then
7561 we'll count that as a retry.
7563 Note that even when forever is set, we will only wait
7564 forever prior to the start of a packet. After that, we
7565 expect characters to arrive at a brisk pace. They should
7566 show up within remote_timeout intervals. */
7568 c = readchar (timeout);
7569 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
7571 if (c == SERIAL_TIMEOUT)
7573 if (expecting_notif)
7574 return -1; /* Don't complain, it's normal to not get
7575 anything in this case. */
7577 if (forever) /* Watchdog went off? Kill the target. */
7580 remote_unpush_target ();
7581 throw_error (TARGET_CLOSE_ERROR,
7582 _("Watchdog timeout has expired. "
7583 "Target detached."));
7586 fputs_filtered ("Timed out.\n", gdb_stdlog);
7590 /* We've found the start of a packet or notification.
7591 Now collect the data. */
7592 val = read_frame (buf, sizeof_buf);
7597 remote_serial_write ("-", 1);
7600 if (tries > MAX_TRIES)
7602 /* We have tried hard enough, and just can't receive the
7603 packet/notification. Give up. */
7604 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7606 /* Skip the ack char if we're in no-ack mode. */
7607 if (!rs->noack_mode)
7608 remote_serial_write ("+", 1);
7612 /* If we got an ordinary packet, return that to our caller. */
7617 struct cleanup *old_chain;
7620 str = escape_buffer (*buf, val);
7621 old_chain = make_cleanup (xfree, str);
7622 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7623 do_cleanups (old_chain);
7626 /* Skip the ack char if we're in no-ack mode. */
7627 if (!rs->noack_mode)
7628 remote_serial_write ("+", 1);
7629 if (is_notif != NULL)
7634 /* If we got a notification, handle it, and go back to looking
7638 gdb_assert (c == '%');
7642 struct cleanup *old_chain;
7645 str = escape_buffer (*buf, val);
7646 old_chain = make_cleanup (xfree, str);
7647 fprintf_unfiltered (gdb_stdlog,
7648 " Notification received: %s\n",
7650 do_cleanups (old_chain);
7652 if (is_notif != NULL)
7655 handle_notification (rs->notif_state, *buf);
7657 /* Notifications require no acknowledgement. */
7659 if (expecting_notif)
7666 getpkt_sane (char **buf, long *sizeof_buf, int forever)
7668 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
7672 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
7675 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
7681 remote_kill (struct target_ops *ops)
7683 volatile struct gdb_exception ex;
7685 /* Catch errors so the user can quit from gdb even when we
7686 aren't on speaking terms with the remote system. */
7687 TRY_CATCH (ex, RETURN_MASK_ERROR)
7693 if (ex.error == TARGET_CLOSE_ERROR)
7695 /* If we got an (EOF) error that caused the target
7696 to go away, then we're done, that's what we wanted.
7697 "k" is susceptible to cause a premature EOF, given
7698 that the remote server isn't actually required to
7699 reply to "k", and it can happen that it doesn't
7700 even get to reply ACK to the "k". */
7704 /* Otherwise, something went wrong. We didn't actually kill
7705 the target. Just propagate the exception, and let the
7706 user or higher layers decide what to do. */
7707 throw_exception (ex);
7710 /* We've killed the remote end, we get to mourn it. Since this is
7711 target remote, single-process, mourning the inferior also
7712 unpushes remote_ops. */
7713 target_mourn_inferior ();
7717 remote_vkill (int pid, struct remote_state *rs)
7719 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
7722 /* Tell the remote target to detach. */
7723 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
7725 getpkt (&rs->buf, &rs->buf_size, 0);
7727 switch (packet_ok (rs->buf,
7728 &remote_protocol_packets[PACKET_vKill]))
7734 case PACKET_UNKNOWN:
7737 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7742 extended_remote_kill (struct target_ops *ops)
7745 int pid = ptid_get_pid (inferior_ptid);
7746 struct remote_state *rs = get_remote_state ();
7748 res = remote_vkill (pid, rs);
7749 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
7751 /* Don't try 'k' on a multi-process aware stub -- it has no way
7752 to specify the pid. */
7756 getpkt (&rs->buf, &rs->buf_size, 0);
7757 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7760 /* Don't wait for it to die. I'm not really sure it matters whether
7761 we do or not. For the existing stubs, kill is a noop. */
7767 error (_("Can't kill process"));
7769 target_mourn_inferior ();
7773 remote_mourn (struct target_ops *ops)
7775 remote_mourn_1 (ops);
7778 /* Worker function for remote_mourn. */
7780 remote_mourn_1 (struct target_ops *target)
7782 unpush_target (target);
7784 /* remote_close takes care of doing most of the clean up. */
7785 generic_mourn_inferior ();
7789 extended_remote_mourn_1 (struct target_ops *target)
7791 struct remote_state *rs = get_remote_state ();
7793 /* In case we got here due to an error, but we're going to stay
7795 rs->waiting_for_stop_reply = 0;
7797 /* If the current general thread belonged to the process we just
7798 detached from or has exited, the remote side current general
7799 thread becomes undefined. Considering a case like this:
7801 - We just got here due to a detach.
7802 - The process that we're detaching from happens to immediately
7803 report a global breakpoint being hit in non-stop mode, in the
7804 same thread we had selected before.
7805 - GDB attaches to this process again.
7806 - This event happens to be the next event we handle.
7808 GDB would consider that the current general thread didn't need to
7809 be set on the stub side (with Hg), since for all it knew,
7810 GENERAL_THREAD hadn't changed.
7812 Notice that although in all-stop mode, the remote server always
7813 sets the current thread to the thread reporting the stop event,
7814 that doesn't happen in non-stop mode; in non-stop, the stub *must
7815 not* change the current thread when reporting a breakpoint hit,
7816 due to the decoupling of event reporting and event handling.
7818 To keep things simple, we always invalidate our notion of the
7820 record_currthread (rs, minus_one_ptid);
7822 /* Unlike "target remote", we do not want to unpush the target; then
7823 the next time the user says "run", we won't be connected. */
7825 /* Call common code to mark the inferior as not running. */
7826 generic_mourn_inferior ();
7828 if (!have_inferiors ())
7830 if (!remote_multi_process_p (rs))
7832 /* Check whether the target is running now - some remote stubs
7833 automatically restart after kill. */
7835 getpkt (&rs->buf, &rs->buf_size, 0);
7837 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7839 /* Assume that the target has been restarted. Set
7840 inferior_ptid so that bits of core GDB realizes
7841 there's something here, e.g., so that the user can
7842 say "kill" again. */
7843 inferior_ptid = magic_null_ptid;
7850 extended_remote_mourn (struct target_ops *ops)
7852 extended_remote_mourn_1 (ops);
7856 extended_remote_supports_disable_randomization (struct target_ops *self)
7858 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
7862 extended_remote_disable_randomization (int val)
7864 struct remote_state *rs = get_remote_state ();
7867 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7870 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7872 error (_("Target does not support QDisableRandomization."));
7873 if (strcmp (reply, "OK") != 0)
7874 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7878 extended_remote_run (char *args)
7880 struct remote_state *rs = get_remote_state ();
7883 /* If the user has disabled vRun support, or we have detected that
7884 support is not available, do not try it. */
7885 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
7888 strcpy (rs->buf, "vRun;");
7889 len = strlen (rs->buf);
7891 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7892 error (_("Remote file name too long for run packet"));
7893 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
7894 strlen (remote_exec_file));
7896 gdb_assert (args != NULL);
7899 struct cleanup *back_to;
7903 argv = gdb_buildargv (args);
7904 back_to = make_cleanup_freeargv (argv);
7905 for (i = 0; argv[i] != NULL; i++)
7907 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
7908 error (_("Argument list too long for run packet"));
7909 rs->buf[len++] = ';';
7910 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
7913 do_cleanups (back_to);
7916 rs->buf[len++] = '\0';
7919 getpkt (&rs->buf, &rs->buf_size, 0);
7921 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
7924 /* We have a wait response. All is well. */
7926 case PACKET_UNKNOWN:
7929 if (remote_exec_file[0] == '\0')
7930 error (_("Running the default executable on the remote target failed; "
7931 "try \"set remote exec-file\"?"));
7933 error (_("Running \"%s\" on the remote target failed"),
7936 gdb_assert_not_reached (_("bad switch"));
7940 /* In the extended protocol we want to be able to do things like
7941 "run" and have them basically work as expected. So we need
7942 a special create_inferior function. We support changing the
7943 executable file and the command line arguments, but not the
7947 extended_remote_create_inferior (struct target_ops *ops,
7948 char *exec_file, char *args,
7949 char **env, int from_tty)
7953 struct remote_state *rs = get_remote_state ();
7955 /* If running asynchronously, register the target file descriptor
7956 with the event loop. */
7957 if (target_can_async_p ())
7958 target_async (inferior_event_handler, 0);
7960 /* Disable address space randomization if requested (and supported). */
7961 if (extended_remote_supports_disable_randomization (ops))
7962 extended_remote_disable_randomization (disable_randomization);
7964 /* Now restart the remote server. */
7965 run_worked = extended_remote_run (args) != -1;
7968 /* vRun was not supported. Fail if we need it to do what the
7970 if (remote_exec_file[0])
7971 error (_("Remote target does not support \"set remote exec-file\""));
7973 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7975 /* Fall back to "R". */
7976 extended_remote_restart ();
7979 if (!have_inferiors ())
7981 /* Clean up from the last time we ran, before we mark the target
7982 running again. This will mark breakpoints uninserted, and
7983 get_offsets may insert breakpoints. */
7984 init_thread_list ();
7985 init_wait_for_inferior ();
7988 /* vRun's success return is a stop reply. */
7989 stop_reply = run_worked ? rs->buf : NULL;
7990 add_current_inferior_and_thread (stop_reply);
7992 /* Get updated offsets, if the stub uses qOffsets. */
7997 /* Given a location's target info BP_TGT and the packet buffer BUF, output
7998 the list of conditions (in agent expression bytecode format), if any, the
7999 target needs to evaluate. The output is placed into the packet buffer
8000 started from BUF and ended at BUF_END. */
8003 remote_add_target_side_condition (struct gdbarch *gdbarch,
8004 struct bp_target_info *bp_tgt, char *buf,
8007 struct agent_expr *aexpr = NULL;
8010 char *buf_start = buf;
8012 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8015 buf += strlen (buf);
8016 xsnprintf (buf, buf_end - buf, "%s", ";");
8019 /* Send conditions to the target and free the vector. */
8021 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8024 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
8025 buf += strlen (buf);
8026 for (i = 0; i < aexpr->len; ++i)
8027 buf = pack_hex_byte (buf, aexpr->buf[i]);
8034 remote_add_target_side_commands (struct gdbarch *gdbarch,
8035 struct bp_target_info *bp_tgt, char *buf)
8037 struct agent_expr *aexpr = NULL;
8040 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8043 buf += strlen (buf);
8045 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8046 buf += strlen (buf);
8048 /* Concatenate all the agent expressions that are commands into the
8051 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8054 sprintf (buf, "X%x,", aexpr->len);
8055 buf += strlen (buf);
8056 for (i = 0; i < aexpr->len; ++i)
8057 buf = pack_hex_byte (buf, aexpr->buf[i]);
8062 /* Insert a breakpoint. On targets that have software breakpoint
8063 support, we ask the remote target to do the work; on targets
8064 which don't, we insert a traditional memory breakpoint. */
8067 remote_insert_breakpoint (struct target_ops *ops,
8068 struct gdbarch *gdbarch,
8069 struct bp_target_info *bp_tgt)
8071 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8072 If it succeeds, then set the support to PACKET_ENABLE. If it
8073 fails, and the user has explicitly requested the Z support then
8074 report an error, otherwise, mark it disabled and go on. */
8076 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8078 CORE_ADDR addr = bp_tgt->reqstd_address;
8079 struct remote_state *rs;
8082 struct condition_list *cond = NULL;
8084 /* Make sure the remote is pointing at the right process, if
8086 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8087 set_general_process ();
8089 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8091 rs = get_remote_state ();
8093 endbuf = rs->buf + get_remote_packet_size ();
8098 addr = (ULONGEST) remote_address_masked (addr);
8099 p += hexnumstr (p, addr);
8100 xsnprintf (p, endbuf - p, ",%d", bpsize);
8102 if (remote_supports_cond_breakpoints (ops))
8103 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8105 if (remote_can_run_breakpoint_commands (ops))
8106 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8109 getpkt (&rs->buf, &rs->buf_size, 0);
8111 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
8116 bp_tgt->placed_address = addr;
8117 bp_tgt->placed_size = bpsize;
8119 case PACKET_UNKNOWN:
8124 /* If this breakpoint has target-side commands but this stub doesn't
8125 support Z0 packets, throw error. */
8126 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8127 throw_error (NOT_SUPPORTED_ERROR, _("\
8128 Target doesn't support breakpoints that have target side commands."));
8130 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
8134 remote_remove_breakpoint (struct target_ops *ops,
8135 struct gdbarch *gdbarch,
8136 struct bp_target_info *bp_tgt)
8138 CORE_ADDR addr = bp_tgt->placed_address;
8139 struct remote_state *rs = get_remote_state ();
8141 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8144 char *endbuf = rs->buf + get_remote_packet_size ();
8146 /* Make sure the remote is pointing at the right process, if
8148 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8149 set_general_process ();
8155 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8156 p += hexnumstr (p, addr);
8157 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
8160 getpkt (&rs->buf, &rs->buf_size, 0);
8162 return (rs->buf[0] == 'E');
8165 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
8169 watchpoint_to_Z_packet (int type)
8174 return Z_PACKET_WRITE_WP;
8177 return Z_PACKET_READ_WP;
8180 return Z_PACKET_ACCESS_WP;
8183 internal_error (__FILE__, __LINE__,
8184 _("hw_bp_to_z: bad watchpoint type %d"), type);
8189 remote_insert_watchpoint (struct target_ops *self,
8190 CORE_ADDR addr, int len, int type,
8191 struct expression *cond)
8193 struct remote_state *rs = get_remote_state ();
8194 char *endbuf = rs->buf + get_remote_packet_size ();
8196 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8198 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8201 /* Make sure the remote is pointing at the right process, if
8203 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8204 set_general_process ();
8206 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
8207 p = strchr (rs->buf, '\0');
8208 addr = remote_address_masked (addr);
8209 p += hexnumstr (p, (ULONGEST) addr);
8210 xsnprintf (p, endbuf - p, ",%x", len);
8213 getpkt (&rs->buf, &rs->buf_size, 0);
8215 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8219 case PACKET_UNKNOWN:
8224 internal_error (__FILE__, __LINE__,
8225 _("remote_insert_watchpoint: reached end of function"));
8229 remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8230 CORE_ADDR start, int length)
8232 CORE_ADDR diff = remote_address_masked (addr - start);
8234 return diff < length;
8239 remote_remove_watchpoint (struct target_ops *self,
8240 CORE_ADDR addr, int len, int type,
8241 struct expression *cond)
8243 struct remote_state *rs = get_remote_state ();
8244 char *endbuf = rs->buf + get_remote_packet_size ();
8246 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8248 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8251 /* Make sure the remote is pointing at the right process, if
8253 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8254 set_general_process ();
8256 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
8257 p = strchr (rs->buf, '\0');
8258 addr = remote_address_masked (addr);
8259 p += hexnumstr (p, (ULONGEST) addr);
8260 xsnprintf (p, endbuf - p, ",%x", len);
8262 getpkt (&rs->buf, &rs->buf_size, 0);
8264 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8267 case PACKET_UNKNOWN:
8272 internal_error (__FILE__, __LINE__,
8273 _("remote_remove_watchpoint: reached end of function"));
8277 int remote_hw_watchpoint_limit = -1;
8278 int remote_hw_watchpoint_length_limit = -1;
8279 int remote_hw_breakpoint_limit = -1;
8282 remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8283 CORE_ADDR addr, int len)
8285 if (remote_hw_watchpoint_length_limit == 0)
8287 else if (remote_hw_watchpoint_length_limit < 0)
8289 else if (len <= remote_hw_watchpoint_length_limit)
8296 remote_check_watch_resources (struct target_ops *self,
8297 int type, int cnt, int ot)
8299 if (type == bp_hardware_breakpoint)
8301 if (remote_hw_breakpoint_limit == 0)
8303 else if (remote_hw_breakpoint_limit < 0)
8305 else if (cnt <= remote_hw_breakpoint_limit)
8310 if (remote_hw_watchpoint_limit == 0)
8312 else if (remote_hw_watchpoint_limit < 0)
8316 else if (cnt <= remote_hw_watchpoint_limit)
8323 remote_stopped_by_watchpoint (struct target_ops *ops)
8325 struct remote_state *rs = get_remote_state ();
8327 return rs->remote_stopped_by_watchpoint_p;
8331 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
8333 struct remote_state *rs = get_remote_state ();
8336 if (remote_stopped_by_watchpoint (target))
8338 *addr_p = rs->remote_watch_data_address;
8347 remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8348 struct bp_target_info *bp_tgt)
8350 CORE_ADDR addr = bp_tgt->reqstd_address;
8351 struct remote_state *rs;
8356 /* The length field should be set to the size of a breakpoint
8357 instruction, even though we aren't inserting one ourselves. */
8359 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8361 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8364 /* Make sure the remote is pointing at the right process, if
8366 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8367 set_general_process ();
8369 rs = get_remote_state ();
8371 endbuf = rs->buf + get_remote_packet_size ();
8377 addr = remote_address_masked (addr);
8378 p += hexnumstr (p, (ULONGEST) addr);
8379 xsnprintf (p, endbuf - p, ",%x", bpsize);
8381 if (remote_supports_cond_breakpoints (self))
8382 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8384 if (remote_can_run_breakpoint_commands (self))
8385 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8388 getpkt (&rs->buf, &rs->buf_size, 0);
8390 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8393 if (rs->buf[1] == '.')
8395 message = strchr (rs->buf + 2, '.');
8397 error (_("Remote failure reply: %s"), message + 1);
8400 case PACKET_UNKNOWN:
8403 bp_tgt->placed_address = addr;
8404 bp_tgt->placed_size = bpsize;
8407 internal_error (__FILE__, __LINE__,
8408 _("remote_insert_hw_breakpoint: reached end of function"));
8413 remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8414 struct bp_target_info *bp_tgt)
8417 struct remote_state *rs = get_remote_state ();
8419 char *endbuf = rs->buf + get_remote_packet_size ();
8421 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8424 /* Make sure the remote is pointing at the right process, if
8426 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8427 set_general_process ();
8433 addr = remote_address_masked (bp_tgt->placed_address);
8434 p += hexnumstr (p, (ULONGEST) addr);
8435 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8438 getpkt (&rs->buf, &rs->buf_size, 0);
8440 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8443 case PACKET_UNKNOWN:
8448 internal_error (__FILE__, __LINE__,
8449 _("remote_remove_hw_breakpoint: reached end of function"));
8452 /* Verify memory using the "qCRC:" request. */
8455 remote_verify_memory (struct target_ops *ops,
8456 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8458 struct remote_state *rs = get_remote_state ();
8459 unsigned long host_crc, target_crc;
8462 /* It doesn't make sense to use qCRC if the remote target is
8463 connected but not running. */
8464 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
8466 enum packet_result result;
8468 /* Make sure the remote is pointing at the right process. */
8469 set_general_process ();
8471 /* FIXME: assumes lma can fit into long. */
8472 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8473 (long) lma, (long) size);
8476 /* Be clever; compute the host_crc before waiting for target
8478 host_crc = xcrc32 (data, size, 0xffffffff);
8480 getpkt (&rs->buf, &rs->buf_size, 0);
8482 result = packet_ok (rs->buf,
8483 &remote_protocol_packets[PACKET_qCRC]);
8484 if (result == PACKET_ERROR)
8486 else if (result == PACKET_OK)
8488 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8489 target_crc = target_crc * 16 + fromhex (*tmp);
8491 return (host_crc == target_crc);
8495 return simple_verify_memory (ops, data, lma, size);
8498 /* compare-sections command
8500 With no arguments, compares each loadable section in the exec bfd
8501 with the same memory range on the target, and reports mismatches.
8502 Useful for verifying the image on the target against the exec file. */
8505 compare_sections_command (char *args, int from_tty)
8508 struct cleanup *old_chain;
8510 const char *sectname;
8519 error (_("command cannot be used without an exec file"));
8521 /* Make sure the remote is pointing at the right process. */
8522 set_general_process ();
8524 if (args != NULL && strcmp (args, "-r") == 0)
8530 for (s = exec_bfd->sections; s; s = s->next)
8532 if (!(s->flags & SEC_LOAD))
8533 continue; /* Skip non-loadable section. */
8535 if (read_only && (s->flags & SEC_READONLY) == 0)
8536 continue; /* Skip writeable sections */
8538 size = bfd_get_section_size (s);
8540 continue; /* Skip zero-length section. */
8542 sectname = bfd_get_section_name (exec_bfd, s);
8543 if (args && strcmp (args, sectname) != 0)
8544 continue; /* Not the section selected by user. */
8546 matched = 1; /* Do this section. */
8549 sectdata = xmalloc (size);
8550 old_chain = make_cleanup (xfree, sectdata);
8551 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
8553 res = target_verify_memory (sectdata, lma, size);
8556 error (_("target memory fault, section %s, range %s -- %s"), sectname,
8557 paddress (target_gdbarch (), lma),
8558 paddress (target_gdbarch (), lma + size));
8560 printf_filtered ("Section %s, range %s -- %s: ", sectname,
8561 paddress (target_gdbarch (), lma),
8562 paddress (target_gdbarch (), lma + size));
8564 printf_filtered ("matched.\n");
8567 printf_filtered ("MIS-MATCHED!\n");
8571 do_cleanups (old_chain);
8574 warning (_("One or more sections of the target image does not match\n\
8575 the loaded file\n"));
8576 if (args && !matched)
8577 printf_filtered (_("No loaded section named '%s'.\n"), args);
8580 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8581 into remote target. The number of bytes written to the remote
8582 target is returned, or -1 for error. */
8584 static enum target_xfer_status
8585 remote_write_qxfer (struct target_ops *ops, const char *object_name,
8586 const char *annex, const gdb_byte *writebuf,
8587 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
8588 struct packet_config *packet)
8592 struct remote_state *rs = get_remote_state ();
8593 int max_size = get_memory_write_packet_size ();
8595 if (packet->support == PACKET_DISABLE)
8596 return TARGET_XFER_E_IO;
8598 /* Insert header. */
8599 i = snprintf (rs->buf, max_size,
8600 "qXfer:%s:write:%s:%s:",
8601 object_name, annex ? annex : "",
8602 phex_nz (offset, sizeof offset));
8603 max_size -= (i + 1);
8605 /* Escape as much data as fits into rs->buf. */
8606 buf_len = remote_escape_output
8607 (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size);
8609 if (putpkt_binary (rs->buf, i + buf_len) < 0
8610 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8611 || packet_ok (rs->buf, packet) != PACKET_OK)
8612 return TARGET_XFER_E_IO;
8614 unpack_varlen_hex (rs->buf, &n);
8617 return TARGET_XFER_OK;
8620 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8621 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8622 number of bytes read is returned, or 0 for EOF, or -1 for error.
8623 The number of bytes read may be less than LEN without indicating an
8624 EOF. PACKET is checked and updated to indicate whether the remote
8625 target supports this object. */
8627 static enum target_xfer_status
8628 remote_read_qxfer (struct target_ops *ops, const char *object_name,
8630 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8631 ULONGEST *xfered_len,
8632 struct packet_config *packet)
8634 struct remote_state *rs = get_remote_state ();
8635 LONGEST i, n, packet_len;
8637 if (packet->support == PACKET_DISABLE)
8638 return TARGET_XFER_E_IO;
8640 /* Check whether we've cached an end-of-object packet that matches
8642 if (rs->finished_object)
8644 if (strcmp (object_name, rs->finished_object) == 0
8645 && strcmp (annex ? annex : "", rs->finished_annex) == 0
8646 && offset == rs->finished_offset)
8647 return TARGET_XFER_EOF;
8650 /* Otherwise, we're now reading something different. Discard
8652 xfree (rs->finished_object);
8653 xfree (rs->finished_annex);
8654 rs->finished_object = NULL;
8655 rs->finished_annex = NULL;
8658 /* Request only enough to fit in a single packet. The actual data
8659 may not, since we don't know how much of it will need to be escaped;
8660 the target is free to respond with slightly less data. We subtract
8661 five to account for the response type and the protocol frame. */
8662 n = min (get_remote_packet_size () - 5, len);
8663 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8664 object_name, annex ? annex : "",
8665 phex_nz (offset, sizeof offset),
8666 phex_nz (n, sizeof n));
8667 i = putpkt (rs->buf);
8669 return TARGET_XFER_E_IO;
8672 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8673 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8674 return TARGET_XFER_E_IO;
8676 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8677 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8679 /* 'm' means there is (or at least might be) more data after this
8680 batch. That does not make sense unless there's at least one byte
8681 of data in this reply. */
8682 if (rs->buf[0] == 'm' && packet_len == 1)
8683 error (_("Remote qXfer reply contained no data."));
8685 /* Got some data. */
8686 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
8687 packet_len - 1, readbuf, n);
8689 /* 'l' is an EOF marker, possibly including a final block of data,
8690 or possibly empty. If we have the final block of a non-empty
8691 object, record this fact to bypass a subsequent partial read. */
8692 if (rs->buf[0] == 'l' && offset + i > 0)
8694 rs->finished_object = xstrdup (object_name);
8695 rs->finished_annex = xstrdup (annex ? annex : "");
8696 rs->finished_offset = offset + i;
8700 return TARGET_XFER_EOF;
8704 return TARGET_XFER_OK;
8708 static enum target_xfer_status
8709 remote_xfer_partial (struct target_ops *ops, enum target_object object,
8710 const char *annex, gdb_byte *readbuf,
8711 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
8712 ULONGEST *xfered_len)
8714 struct remote_state *rs;
8719 set_remote_traceframe ();
8720 set_general_thread (inferior_ptid);
8722 rs = get_remote_state ();
8724 /* Handle memory using the standard memory routines. */
8725 if (object == TARGET_OBJECT_MEMORY)
8727 /* If the remote target is connected but not running, we should
8728 pass this request down to a lower stratum (e.g. the executable
8730 if (!target_has_execution)
8731 return TARGET_XFER_EOF;
8733 if (writebuf != NULL)
8734 return remote_write_bytes (offset, writebuf, len, xfered_len);
8736 return remote_read_bytes (ops, offset, readbuf, len, xfered_len);
8739 /* Handle SPU memory using qxfer packets. */
8740 if (object == TARGET_OBJECT_SPU)
8743 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8744 xfered_len, &remote_protocol_packets
8745 [PACKET_qXfer_spu_read]);
8747 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8748 xfered_len, &remote_protocol_packets
8749 [PACKET_qXfer_spu_write]);
8752 /* Handle extra signal info using qxfer packets. */
8753 if (object == TARGET_OBJECT_SIGNAL_INFO)
8756 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8757 xfered_len, &remote_protocol_packets
8758 [PACKET_qXfer_siginfo_read]);
8760 return remote_write_qxfer (ops, "siginfo", annex,
8761 writebuf, offset, len, xfered_len,
8762 &remote_protocol_packets
8763 [PACKET_qXfer_siginfo_write]);
8766 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8769 return remote_read_qxfer (ops, "statictrace", annex,
8770 readbuf, offset, len, xfered_len,
8771 &remote_protocol_packets
8772 [PACKET_qXfer_statictrace_read]);
8774 return TARGET_XFER_E_IO;
8777 /* Only handle flash writes. */
8778 if (writebuf != NULL)
8784 case TARGET_OBJECT_FLASH:
8785 return remote_flash_write (ops, offset, len, xfered_len,
8789 return TARGET_XFER_E_IO;
8793 /* Map pre-existing objects onto letters. DO NOT do this for new
8794 objects!!! Instead specify new query packets. */
8797 case TARGET_OBJECT_AVR:
8801 case TARGET_OBJECT_AUXV:
8802 gdb_assert (annex == NULL);
8803 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8805 &remote_protocol_packets[PACKET_qXfer_auxv]);
8807 case TARGET_OBJECT_AVAILABLE_FEATURES:
8808 return remote_read_qxfer
8809 (ops, "features", annex, readbuf, offset, len, xfered_len,
8810 &remote_protocol_packets[PACKET_qXfer_features]);
8812 case TARGET_OBJECT_LIBRARIES:
8813 return remote_read_qxfer
8814 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
8815 &remote_protocol_packets[PACKET_qXfer_libraries]);
8817 case TARGET_OBJECT_LIBRARIES_SVR4:
8818 return remote_read_qxfer
8819 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
8820 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8822 case TARGET_OBJECT_MEMORY_MAP:
8823 gdb_assert (annex == NULL);
8824 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8826 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8828 case TARGET_OBJECT_OSDATA:
8829 /* Should only get here if we're connected. */
8830 gdb_assert (rs->remote_desc);
8831 return remote_read_qxfer
8832 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
8833 &remote_protocol_packets[PACKET_qXfer_osdata]);
8835 case TARGET_OBJECT_THREADS:
8836 gdb_assert (annex == NULL);
8837 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8839 &remote_protocol_packets[PACKET_qXfer_threads]);
8841 case TARGET_OBJECT_TRACEFRAME_INFO:
8842 gdb_assert (annex == NULL);
8843 return remote_read_qxfer
8844 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
8845 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8847 case TARGET_OBJECT_FDPIC:
8848 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8850 &remote_protocol_packets[PACKET_qXfer_fdpic]);
8852 case TARGET_OBJECT_OPENVMS_UIB:
8853 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8855 &remote_protocol_packets[PACKET_qXfer_uib]);
8857 case TARGET_OBJECT_BTRACE:
8858 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
8860 &remote_protocol_packets[PACKET_qXfer_btrace]);
8863 return TARGET_XFER_E_IO;
8866 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8867 large enough let the caller deal with it. */
8868 if (len < get_remote_packet_size ())
8869 return TARGET_XFER_E_IO;
8870 len = get_remote_packet_size ();
8872 /* Except for querying the minimum buffer size, target must be open. */
8873 if (!rs->remote_desc)
8874 error (_("remote query is only available after target open"));
8876 gdb_assert (annex != NULL);
8877 gdb_assert (readbuf != NULL);
8883 /* We used one buffer char for the remote protocol q command and
8884 another for the query type. As the remote protocol encapsulation
8885 uses 4 chars plus one extra in case we are debugging
8886 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8889 while (annex[i] && (i < (get_remote_packet_size () - 8)))
8891 /* Bad caller may have sent forbidden characters. */
8892 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
8897 gdb_assert (annex[i] == '\0');
8899 i = putpkt (rs->buf);
8901 return TARGET_XFER_E_IO;
8903 getpkt (&rs->buf, &rs->buf_size, 0);
8904 strcpy ((char *) readbuf, rs->buf);
8906 *xfered_len = strlen ((char *) readbuf);
8907 return TARGET_XFER_OK;
8911 remote_search_memory (struct target_ops* ops,
8912 CORE_ADDR start_addr, ULONGEST search_space_len,
8913 const gdb_byte *pattern, ULONGEST pattern_len,
8914 CORE_ADDR *found_addrp)
8916 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
8917 struct remote_state *rs = get_remote_state ();
8918 int max_size = get_memory_write_packet_size ();
8919 struct packet_config *packet =
8920 &remote_protocol_packets[PACKET_qSearch_memory];
8921 /* Number of packet bytes used to encode the pattern;
8922 this could be more than PATTERN_LEN due to escape characters. */
8923 int escaped_pattern_len;
8924 /* Amount of pattern that was encodable in the packet. */
8925 int used_pattern_len;
8928 ULONGEST found_addr;
8930 /* Don't go to the target if we don't have to.
8931 This is done before checking packet->support to avoid the possibility that
8932 a success for this edge case means the facility works in general. */
8933 if (pattern_len > search_space_len)
8935 if (pattern_len == 0)
8937 *found_addrp = start_addr;
8941 /* If we already know the packet isn't supported, fall back to the simple
8942 way of searching memory. */
8944 if (packet_config_support (packet) == PACKET_DISABLE)
8946 /* Target doesn't provided special support, fall back and use the
8947 standard support (copy memory and do the search here). */
8948 return simple_search_memory (ops, start_addr, search_space_len,
8949 pattern, pattern_len, found_addrp);
8952 /* Make sure the remote is pointing at the right process. */
8953 set_general_process ();
8955 /* Insert header. */
8956 i = snprintf (rs->buf, max_size,
8957 "qSearch:memory:%s;%s;",
8958 phex_nz (start_addr, addr_size),
8959 phex_nz (search_space_len, sizeof (search_space_len)));
8960 max_size -= (i + 1);
8962 /* Escape as much data as fits into rs->buf. */
8963 escaped_pattern_len =
8964 remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i,
8965 &used_pattern_len, max_size);
8967 /* Bail if the pattern is too large. */
8968 if (used_pattern_len != pattern_len)
8969 error (_("Pattern is too large to transmit to remote target."));
8971 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
8972 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8973 || packet_ok (rs->buf, packet) != PACKET_OK)
8975 /* The request may not have worked because the command is not
8976 supported. If so, fall back to the simple way. */
8977 if (packet->support == PACKET_DISABLE)
8979 return simple_search_memory (ops, start_addr, search_space_len,
8980 pattern, pattern_len, found_addrp);
8985 if (rs->buf[0] == '0')
8987 else if (rs->buf[0] == '1')
8990 if (rs->buf[1] != ',')
8991 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8992 unpack_varlen_hex (rs->buf + 2, &found_addr);
8993 *found_addrp = found_addr;
8996 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9002 remote_rcmd (struct target_ops *self, const char *command,
9003 struct ui_file *outbuf)
9005 struct remote_state *rs = get_remote_state ();
9008 if (!rs->remote_desc)
9009 error (_("remote rcmd is only available after target open"));
9011 /* Send a NULL command across as an empty command. */
9012 if (command == NULL)
9015 /* The query prefix. */
9016 strcpy (rs->buf, "qRcmd,");
9017 p = strchr (rs->buf, '\0');
9019 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9020 > get_remote_packet_size ())
9021 error (_("\"monitor\" command ``%s'' is too long."), command);
9023 /* Encode the actual command. */
9024 bin2hex ((const gdb_byte *) command, p, strlen (command));
9026 if (putpkt (rs->buf) < 0)
9027 error (_("Communication problem with target."));
9029 /* get/display the response */
9034 /* XXX - see also remote_get_noisy_reply(). */
9035 QUIT; /* Allow user to bail out with ^C. */
9037 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9039 /* Timeout. Continue to (try to) read responses.
9040 This is better than stopping with an error, assuming the stub
9041 is still executing the (long) monitor command.
9042 If needed, the user can interrupt gdb using C-c, obtaining
9043 an effect similar to stop on timeout. */
9048 error (_("Target does not support this command."));
9049 if (buf[0] == 'O' && buf[1] != 'K')
9051 remote_console_output (buf + 1); /* 'O' message from stub. */
9054 if (strcmp (buf, "OK") == 0)
9056 if (strlen (buf) == 3 && buf[0] == 'E'
9057 && isdigit (buf[1]) && isdigit (buf[2]))
9059 error (_("Protocol error with Rcmd"));
9061 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9063 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
9065 fputc_unfiltered (c, outbuf);
9071 static VEC(mem_region_s) *
9072 remote_memory_map (struct target_ops *ops)
9074 VEC(mem_region_s) *result = NULL;
9075 char *text = target_read_stralloc (¤t_target,
9076 TARGET_OBJECT_MEMORY_MAP, NULL);
9080 struct cleanup *back_to = make_cleanup (xfree, text);
9082 result = parse_memory_map (text);
9083 do_cleanups (back_to);
9090 packet_command (char *args, int from_tty)
9092 struct remote_state *rs = get_remote_state ();
9094 if (!rs->remote_desc)
9095 error (_("command can only be used with remote target"));
9098 error (_("remote-packet command requires packet text as argument"));
9100 puts_filtered ("sending: ");
9101 print_packet (args);
9102 puts_filtered ("\n");
9105 getpkt (&rs->buf, &rs->buf_size, 0);
9106 puts_filtered ("received: ");
9107 print_packet (rs->buf);
9108 puts_filtered ("\n");
9112 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9114 static void display_thread_info (struct gdb_ext_thread_info *info);
9116 static void threadset_test_cmd (char *cmd, int tty);
9118 static void threadalive_test (char *cmd, int tty);
9120 static void threadlist_test_cmd (char *cmd, int tty);
9122 int get_and_display_threadinfo (threadref *ref);
9124 static void threadinfo_test_cmd (char *cmd, int tty);
9126 static int thread_display_step (threadref *ref, void *context);
9128 static void threadlist_update_test_cmd (char *cmd, int tty);
9130 static void init_remote_threadtests (void);
9132 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9135 threadset_test_cmd (char *cmd, int tty)
9137 int sample_thread = SAMPLE_THREAD;
9139 printf_filtered (_("Remote threadset test\n"));
9140 set_general_thread (sample_thread);
9145 threadalive_test (char *cmd, int tty)
9147 int sample_thread = SAMPLE_THREAD;
9148 int pid = ptid_get_pid (inferior_ptid);
9149 ptid_t ptid = ptid_build (pid, sample_thread, 0);
9151 if (remote_thread_alive (ptid))
9152 printf_filtered ("PASS: Thread alive test\n");
9154 printf_filtered ("FAIL: Thread alive test\n");
9157 void output_threadid (char *title, threadref *ref);
9160 output_threadid (char *title, threadref *ref)
9164 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
9166 printf_filtered ("%s %s\n", title, (&hexid[0]));
9170 threadlist_test_cmd (char *cmd, int tty)
9173 threadref nextthread;
9174 int done, result_count;
9175 threadref threadlist[3];
9177 printf_filtered ("Remote Threadlist test\n");
9178 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9179 &result_count, &threadlist[0]))
9180 printf_filtered ("FAIL: threadlist test\n");
9183 threadref *scan = threadlist;
9184 threadref *limit = scan + result_count;
9186 while (scan < limit)
9187 output_threadid (" thread ", scan++);
9192 display_thread_info (struct gdb_ext_thread_info *info)
9194 output_threadid ("Threadid: ", &info->threadid);
9195 printf_filtered ("Name: %s\n ", info->shortname);
9196 printf_filtered ("State: %s\n", info->display);
9197 printf_filtered ("other: %s\n\n", info->more_display);
9201 get_and_display_threadinfo (threadref *ref)
9205 struct gdb_ext_thread_info threadinfo;
9207 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9208 | TAG_MOREDISPLAY | TAG_DISPLAY;
9209 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9210 display_thread_info (&threadinfo);
9215 threadinfo_test_cmd (char *cmd, int tty)
9217 int athread = SAMPLE_THREAD;
9221 int_to_threadref (&thread, athread);
9222 printf_filtered ("Remote Threadinfo test\n");
9223 if (!get_and_display_threadinfo (&thread))
9224 printf_filtered ("FAIL cannot get thread info\n");
9228 thread_display_step (threadref *ref, void *context)
9230 /* output_threadid(" threadstep ",ref); *//* simple test */
9231 return get_and_display_threadinfo (ref);
9235 threadlist_update_test_cmd (char *cmd, int tty)
9237 printf_filtered ("Remote Threadlist update test\n");
9238 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9242 init_remote_threadtests (void)
9244 add_com ("tlist", class_obscure, threadlist_test_cmd,
9245 _("Fetch and print the remote list of "
9246 "thread identifiers, one pkt only"));
9247 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
9248 _("Fetch and display info about one thread"));
9249 add_com ("tset", class_obscure, threadset_test_cmd,
9250 _("Test setting to a different thread"));
9251 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
9252 _("Iterate through updating all remote thread info"));
9253 add_com ("talive", class_obscure, threadalive_test,
9254 _(" Remote thread alive test "));
9259 /* Convert a thread ID to a string. Returns the string in a static
9263 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
9265 static char buf[64];
9266 struct remote_state *rs = get_remote_state ();
9268 if (ptid_equal (ptid, null_ptid))
9269 return normal_pid_to_str (ptid);
9270 else if (ptid_is_pid (ptid))
9272 /* Printing an inferior target id. */
9274 /* When multi-process extensions are off, there's no way in the
9275 remote protocol to know the remote process id, if there's any
9276 at all. There's one exception --- when we're connected with
9277 target extended-remote, and we manually attached to a process
9278 with "attach PID". We don't record anywhere a flag that
9279 allows us to distinguish that case from the case of
9280 connecting with extended-remote and the stub already being
9281 attached to a process, and reporting yes to qAttached, hence
9282 no smart special casing here. */
9283 if (!remote_multi_process_p (rs))
9285 xsnprintf (buf, sizeof buf, "Remote target");
9289 return normal_pid_to_str (ptid);
9293 if (ptid_equal (magic_null_ptid, ptid))
9294 xsnprintf (buf, sizeof buf, "Thread <main>");
9295 else if (rs->extended && remote_multi_process_p (rs))
9296 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9297 ptid_get_pid (ptid), ptid_get_lwp (ptid));
9299 xsnprintf (buf, sizeof buf, "Thread %ld",
9300 ptid_get_lwp (ptid));
9305 /* Get the address of the thread local variable in OBJFILE which is
9306 stored at OFFSET within the thread local storage for thread PTID. */
9309 remote_get_thread_local_address (struct target_ops *ops,
9310 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
9312 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
9314 struct remote_state *rs = get_remote_state ();
9316 char *endp = rs->buf + get_remote_packet_size ();
9317 enum packet_result result;
9319 strcpy (p, "qGetTLSAddr:");
9321 p = write_ptid (p, endp, ptid);
9323 p += hexnumstr (p, offset);
9325 p += hexnumstr (p, lm);
9329 getpkt (&rs->buf, &rs->buf_size, 0);
9330 result = packet_ok (rs->buf,
9331 &remote_protocol_packets[PACKET_qGetTLSAddr]);
9332 if (result == PACKET_OK)
9336 unpack_varlen_hex (rs->buf, &result);
9339 else if (result == PACKET_UNKNOWN)
9340 throw_error (TLS_GENERIC_ERROR,
9341 _("Remote target doesn't support qGetTLSAddr packet"));
9343 throw_error (TLS_GENERIC_ERROR,
9344 _("Remote target failed to process qGetTLSAddr request"));
9347 throw_error (TLS_GENERIC_ERROR,
9348 _("TLS not supported or disabled on this target"));
9353 /* Provide thread local base, i.e. Thread Information Block address.
9354 Returns 1 if ptid is found and thread_local_base is non zero. */
9357 remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
9359 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
9361 struct remote_state *rs = get_remote_state ();
9363 char *endp = rs->buf + get_remote_packet_size ();
9364 enum packet_result result;
9366 strcpy (p, "qGetTIBAddr:");
9368 p = write_ptid (p, endp, ptid);
9372 getpkt (&rs->buf, &rs->buf_size, 0);
9373 result = packet_ok (rs->buf,
9374 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9375 if (result == PACKET_OK)
9379 unpack_varlen_hex (rs->buf, &result);
9381 *addr = (CORE_ADDR) result;
9384 else if (result == PACKET_UNKNOWN)
9385 error (_("Remote target doesn't support qGetTIBAddr packet"));
9387 error (_("Remote target failed to process qGetTIBAddr request"));
9390 error (_("qGetTIBAddr not supported or disabled on this target"));
9395 /* Support for inferring a target description based on the current
9396 architecture and the size of a 'g' packet. While the 'g' packet
9397 can have any size (since optional registers can be left off the
9398 end), some sizes are easily recognizable given knowledge of the
9399 approximate architecture. */
9401 struct remote_g_packet_guess
9404 const struct target_desc *tdesc;
9406 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9407 DEF_VEC_O(remote_g_packet_guess_s);
9409 struct remote_g_packet_data
9411 VEC(remote_g_packet_guess_s) *guesses;
9414 static struct gdbarch_data *remote_g_packet_data_handle;
9417 remote_g_packet_data_init (struct obstack *obstack)
9419 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9423 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9424 const struct target_desc *tdesc)
9426 struct remote_g_packet_data *data
9427 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9428 struct remote_g_packet_guess new_guess, *guess;
9431 gdb_assert (tdesc != NULL);
9434 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9436 if (guess->bytes == bytes)
9437 internal_error (__FILE__, __LINE__,
9438 _("Duplicate g packet description added for size %d"),
9441 new_guess.bytes = bytes;
9442 new_guess.tdesc = tdesc;
9443 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9446 /* Return 1 if remote_read_description would do anything on this target
9447 and architecture, 0 otherwise. */
9450 remote_read_description_p (struct target_ops *target)
9452 struct remote_g_packet_data *data
9453 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9455 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9461 static const struct target_desc *
9462 remote_read_description (struct target_ops *target)
9464 struct remote_g_packet_data *data
9465 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9467 /* Do not try this during initial connection, when we do not know
9468 whether there is a running but stopped thread. */
9469 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
9470 return target->beneath->to_read_description (target->beneath);
9472 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9474 struct remote_g_packet_guess *guess;
9476 int bytes = send_g_packet ();
9479 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9481 if (guess->bytes == bytes)
9482 return guess->tdesc;
9484 /* We discard the g packet. A minor optimization would be to
9485 hold on to it, and fill the register cache once we have selected
9486 an architecture, but it's too tricky to do safely. */
9489 return target->beneath->to_read_description (target->beneath);
9492 /* Remote file transfer support. This is host-initiated I/O, not
9493 target-initiated; for target-initiated, see remote-fileio.c. */
9495 /* If *LEFT is at least the length of STRING, copy STRING to
9496 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9497 decrease *LEFT. Otherwise raise an error. */
9500 remote_buffer_add_string (char **buffer, int *left, char *string)
9502 int len = strlen (string);
9505 error (_("Packet too long for target."));
9507 memcpy (*buffer, string, len);
9511 /* NUL-terminate the buffer as a convenience, if there is
9517 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9518 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9519 decrease *LEFT. Otherwise raise an error. */
9522 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9525 if (2 * len > *left)
9526 error (_("Packet too long for target."));
9528 bin2hex (bytes, *buffer, len);
9532 /* NUL-terminate the buffer as a convenience, if there is
9538 /* If *LEFT is large enough, convert VALUE to hex and add it to
9539 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9540 decrease *LEFT. Otherwise raise an error. */
9543 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9545 int len = hexnumlen (value);
9548 error (_("Packet too long for target."));
9550 hexnumstr (*buffer, value);
9554 /* NUL-terminate the buffer as a convenience, if there is
9560 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9561 value, *REMOTE_ERRNO to the remote error number or zero if none
9562 was included, and *ATTACHMENT to point to the start of the annex
9563 if any. The length of the packet isn't needed here; there may
9564 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9566 Return 0 if the packet could be parsed, -1 if it could not. If
9567 -1 is returned, the other variables may not be initialized. */
9570 remote_hostio_parse_result (char *buffer, int *retcode,
9571 int *remote_errno, char **attachment)
9578 if (buffer[0] != 'F')
9582 *retcode = strtol (&buffer[1], &p, 16);
9583 if (errno != 0 || p == &buffer[1])
9586 /* Check for ",errno". */
9590 *remote_errno = strtol (p + 1, &p2, 16);
9591 if (errno != 0 || p + 1 == p2)
9596 /* Check for ";attachment". If there is no attachment, the
9597 packet should end here. */
9600 *attachment = p + 1;
9603 else if (*p == '\0')
9609 /* Send a prepared I/O packet to the target and read its response.
9610 The prepared packet is in the global RS->BUF before this function
9611 is called, and the answer is there when we return.
9613 COMMAND_BYTES is the length of the request to send, which may include
9614 binary data. WHICH_PACKET is the packet configuration to check
9615 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9616 is set to the error number and -1 is returned. Otherwise the value
9617 returned by the function is returned.
9619 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9620 attachment is expected; an error will be reported if there's a
9621 mismatch. If one is found, *ATTACHMENT will be set to point into
9622 the packet buffer and *ATTACHMENT_LEN will be set to the
9623 attachment's length. */
9626 remote_hostio_send_command (int command_bytes, int which_packet,
9627 int *remote_errno, char **attachment,
9628 int *attachment_len)
9630 struct remote_state *rs = get_remote_state ();
9631 int ret, bytes_read;
9632 char *attachment_tmp;
9634 if (!rs->remote_desc
9635 || packet_support (which_packet) == PACKET_DISABLE)
9637 *remote_errno = FILEIO_ENOSYS;
9641 putpkt_binary (rs->buf, command_bytes);
9642 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9644 /* If it timed out, something is wrong. Don't try to parse the
9648 *remote_errno = FILEIO_EINVAL;
9652 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9655 *remote_errno = FILEIO_EINVAL;
9657 case PACKET_UNKNOWN:
9658 *remote_errno = FILEIO_ENOSYS;
9664 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9667 *remote_errno = FILEIO_EINVAL;
9671 /* Make sure we saw an attachment if and only if we expected one. */
9672 if ((attachment_tmp == NULL && attachment != NULL)
9673 || (attachment_tmp != NULL && attachment == NULL))
9675 *remote_errno = FILEIO_EINVAL;
9679 /* If an attachment was found, it must point into the packet buffer;
9680 work out how many bytes there were. */
9681 if (attachment_tmp != NULL)
9683 *attachment = attachment_tmp;
9684 *attachment_len = bytes_read - (*attachment - rs->buf);
9690 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9691 remote file descriptor, or -1 if an error occurs (and set
9695 remote_hostio_open (struct target_ops *self,
9696 const char *filename, int flags, int mode,
9699 struct remote_state *rs = get_remote_state ();
9701 int left = get_remote_packet_size () - 1;
9703 remote_buffer_add_string (&p, &left, "vFile:open:");
9705 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9707 remote_buffer_add_string (&p, &left, ",");
9709 remote_buffer_add_int (&p, &left, flags);
9710 remote_buffer_add_string (&p, &left, ",");
9712 remote_buffer_add_int (&p, &left, mode);
9714 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9715 remote_errno, NULL, NULL);
9718 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9719 Return the number of bytes written, or -1 if an error occurs (and
9720 set *REMOTE_ERRNO). */
9723 remote_hostio_pwrite (struct target_ops *self,
9724 int fd, const gdb_byte *write_buf, int len,
9725 ULONGEST offset, int *remote_errno)
9727 struct remote_state *rs = get_remote_state ();
9729 int left = get_remote_packet_size ();
9732 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9734 remote_buffer_add_int (&p, &left, fd);
9735 remote_buffer_add_string (&p, &left, ",");
9737 remote_buffer_add_int (&p, &left, offset);
9738 remote_buffer_add_string (&p, &left, ",");
9740 p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len,
9741 get_remote_packet_size () - (p - rs->buf));
9743 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9744 remote_errno, NULL, NULL);
9747 /* Read up to LEN bytes FD on the remote target into READ_BUF
9748 Return the number of bytes read, or -1 if an error occurs (and
9749 set *REMOTE_ERRNO). */
9752 remote_hostio_pread (struct target_ops *self,
9753 int fd, gdb_byte *read_buf, int len,
9754 ULONGEST offset, int *remote_errno)
9756 struct remote_state *rs = get_remote_state ();
9759 int left = get_remote_packet_size ();
9760 int ret, attachment_len;
9763 remote_buffer_add_string (&p, &left, "vFile:pread:");
9765 remote_buffer_add_int (&p, &left, fd);
9766 remote_buffer_add_string (&p, &left, ",");
9768 remote_buffer_add_int (&p, &left, len);
9769 remote_buffer_add_string (&p, &left, ",");
9771 remote_buffer_add_int (&p, &left, offset);
9773 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9774 remote_errno, &attachment,
9780 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9782 if (read_len != ret)
9783 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9788 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9789 (and set *REMOTE_ERRNO). */
9792 remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
9794 struct remote_state *rs = get_remote_state ();
9796 int left = get_remote_packet_size () - 1;
9798 remote_buffer_add_string (&p, &left, "vFile:close:");
9800 remote_buffer_add_int (&p, &left, fd);
9802 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9803 remote_errno, NULL, NULL);
9806 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9807 occurs (and set *REMOTE_ERRNO). */
9810 remote_hostio_unlink (struct target_ops *self,
9811 const char *filename, int *remote_errno)
9813 struct remote_state *rs = get_remote_state ();
9815 int left = get_remote_packet_size () - 1;
9817 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9819 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9822 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9823 remote_errno, NULL, NULL);
9826 /* Read value of symbolic link FILENAME on the remote target. Return
9827 a null-terminated string allocated via xmalloc, or NULL if an error
9828 occurs (and set *REMOTE_ERRNO). */
9831 remote_hostio_readlink (struct target_ops *self,
9832 const char *filename, int *remote_errno)
9834 struct remote_state *rs = get_remote_state ();
9837 int left = get_remote_packet_size ();
9838 int len, attachment_len;
9842 remote_buffer_add_string (&p, &left, "vFile:readlink:");
9844 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9847 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9848 remote_errno, &attachment,
9854 ret = xmalloc (len + 1);
9856 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9857 (gdb_byte *) ret, len);
9858 if (read_len != len)
9859 error (_("Readlink returned %d, but %d bytes."), len, read_len);
9866 remote_fileio_errno_to_host (int errnum)
9890 case FILEIO_ENOTDIR:
9910 case FILEIO_ENAMETOOLONG:
9911 return ENAMETOOLONG;
9917 remote_hostio_error (int errnum)
9919 int host_error = remote_fileio_errno_to_host (errnum);
9921 if (host_error == -1)
9922 error (_("Unknown remote I/O error %d"), errnum);
9924 error (_("Remote I/O error: %s"), safe_strerror (host_error));
9928 remote_hostio_close_cleanup (void *opaque)
9930 int fd = *(int *) opaque;
9933 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
9938 remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
9940 const char *filename = bfd_get_filename (abfd);
9941 int fd, remote_errno;
9944 gdb_assert (remote_filename_p (filename));
9946 fd = remote_hostio_open (find_target_at (process_stratum),
9947 filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
9950 errno = remote_fileio_errno_to_host (remote_errno);
9951 bfd_set_error (bfd_error_system_call);
9955 stream = xmalloc (sizeof (int));
9961 remote_bfd_iovec_close (struct bfd *abfd, void *stream)
9963 int fd = *(int *)stream;
9968 /* Ignore errors on close; these may happen if the remote
9969 connection was already torn down. */
9970 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
9972 /* Zero means success. */
9977 remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
9978 file_ptr nbytes, file_ptr offset)
9980 int fd = *(int *)stream;
9982 file_ptr pos, bytes;
9985 while (nbytes > pos)
9987 bytes = remote_hostio_pread (find_target_at (process_stratum),
9988 fd, (gdb_byte *) buf + pos, nbytes - pos,
9989 offset + pos, &remote_errno);
9991 /* Success, but no bytes, means end-of-file. */
9995 errno = remote_fileio_errno_to_host (remote_errno);
9996 bfd_set_error (bfd_error_system_call);
10007 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
10009 /* FIXME: We should probably implement remote_hostio_stat. */
10010 sb->st_size = INT_MAX;
10015 remote_filename_p (const char *filename)
10017 return strncmp (filename,
10018 REMOTE_SYSROOT_PREFIX,
10019 sizeof (REMOTE_SYSROOT_PREFIX) - 1) == 0;
10023 remote_bfd_open (const char *remote_file, const char *target)
10025 bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
10026 remote_bfd_iovec_open, NULL,
10027 remote_bfd_iovec_pread,
10028 remote_bfd_iovec_close,
10029 remote_bfd_iovec_stat);
10035 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10037 struct cleanup *back_to, *close_cleanup;
10038 int retcode, fd, remote_errno, bytes, io_size;
10041 int bytes_in_buffer;
10044 struct remote_state *rs = get_remote_state ();
10046 if (!rs->remote_desc)
10047 error (_("command can only be used with remote target"));
10049 file = gdb_fopen_cloexec (local_file, "rb");
10051 perror_with_name (local_file);
10052 back_to = make_cleanup_fclose (file);
10054 fd = remote_hostio_open (find_target_at (process_stratum),
10055 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
10057 0700, &remote_errno);
10059 remote_hostio_error (remote_errno);
10061 /* Send up to this many bytes at once. They won't all fit in the
10062 remote packet limit, so we'll transfer slightly fewer. */
10063 io_size = get_remote_packet_size ();
10064 buffer = xmalloc (io_size);
10065 make_cleanup (xfree, buffer);
10067 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10069 bytes_in_buffer = 0;
10072 while (bytes_in_buffer || !saw_eof)
10076 bytes = fread (buffer + bytes_in_buffer, 1,
10077 io_size - bytes_in_buffer,
10082 error (_("Error reading %s."), local_file);
10085 /* EOF. Unless there is something still in the
10086 buffer from the last iteration, we are done. */
10088 if (bytes_in_buffer == 0)
10096 bytes += bytes_in_buffer;
10097 bytes_in_buffer = 0;
10099 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10101 offset, &remote_errno);
10104 remote_hostio_error (remote_errno);
10105 else if (retcode == 0)
10106 error (_("Remote write of %d bytes returned 0!"), bytes);
10107 else if (retcode < bytes)
10109 /* Short write. Save the rest of the read data for the next
10111 bytes_in_buffer = bytes - retcode;
10112 memmove (buffer, buffer + retcode, bytes_in_buffer);
10118 discard_cleanups (close_cleanup);
10119 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10120 remote_hostio_error (remote_errno);
10123 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10124 do_cleanups (back_to);
10128 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10130 struct cleanup *back_to, *close_cleanup;
10131 int fd, remote_errno, bytes, io_size;
10135 struct remote_state *rs = get_remote_state ();
10137 if (!rs->remote_desc)
10138 error (_("command can only be used with remote target"));
10140 fd = remote_hostio_open (find_target_at (process_stratum),
10141 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
10143 remote_hostio_error (remote_errno);
10145 file = gdb_fopen_cloexec (local_file, "wb");
10147 perror_with_name (local_file);
10148 back_to = make_cleanup_fclose (file);
10150 /* Send up to this many bytes at once. They won't all fit in the
10151 remote packet limit, so we'll transfer slightly fewer. */
10152 io_size = get_remote_packet_size ();
10153 buffer = xmalloc (io_size);
10154 make_cleanup (xfree, buffer);
10156 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10161 bytes = remote_hostio_pread (find_target_at (process_stratum),
10162 fd, buffer, io_size, offset, &remote_errno);
10164 /* Success, but no bytes, means end-of-file. */
10167 remote_hostio_error (remote_errno);
10171 bytes = fwrite (buffer, 1, bytes, file);
10173 perror_with_name (local_file);
10176 discard_cleanups (close_cleanup);
10177 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10178 remote_hostio_error (remote_errno);
10181 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10182 do_cleanups (back_to);
10186 remote_file_delete (const char *remote_file, int from_tty)
10188 int retcode, remote_errno;
10189 struct remote_state *rs = get_remote_state ();
10191 if (!rs->remote_desc)
10192 error (_("command can only be used with remote target"));
10194 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10195 remote_file, &remote_errno);
10197 remote_hostio_error (remote_errno);
10200 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10204 remote_put_command (char *args, int from_tty)
10206 struct cleanup *back_to;
10210 error_no_arg (_("file to put"));
10212 argv = gdb_buildargv (args);
10213 back_to = make_cleanup_freeargv (argv);
10214 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10215 error (_("Invalid parameters to remote put"));
10217 remote_file_put (argv[0], argv[1], from_tty);
10219 do_cleanups (back_to);
10223 remote_get_command (char *args, int from_tty)
10225 struct cleanup *back_to;
10229 error_no_arg (_("file to get"));
10231 argv = gdb_buildargv (args);
10232 back_to = make_cleanup_freeargv (argv);
10233 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10234 error (_("Invalid parameters to remote get"));
10236 remote_file_get (argv[0], argv[1], from_tty);
10238 do_cleanups (back_to);
10242 remote_delete_command (char *args, int from_tty)
10244 struct cleanup *back_to;
10248 error_no_arg (_("file to delete"));
10250 argv = gdb_buildargv (args);
10251 back_to = make_cleanup_freeargv (argv);
10252 if (argv[0] == NULL || argv[1] != NULL)
10253 error (_("Invalid parameters to remote delete"));
10255 remote_file_delete (argv[0], from_tty);
10257 do_cleanups (back_to);
10261 remote_command (char *args, int from_tty)
10263 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
10267 remote_can_execute_reverse (struct target_ops *self)
10269 if (packet_support (PACKET_bs) == PACKET_ENABLE
10270 || packet_support (PACKET_bc) == PACKET_ENABLE)
10277 remote_supports_non_stop (struct target_ops *self)
10283 remote_supports_disable_randomization (struct target_ops *self)
10285 /* Only supported in extended mode. */
10290 remote_supports_multi_process (struct target_ops *self)
10292 struct remote_state *rs = get_remote_state ();
10294 /* Only extended-remote handles being attached to multiple
10295 processes, even though plain remote can use the multi-process
10296 thread id extensions, so that GDB knows the target process's
10298 return rs->extended && remote_multi_process_p (rs);
10302 remote_supports_cond_tracepoints (void)
10304 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
10308 remote_supports_cond_breakpoints (struct target_ops *self)
10310 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
10314 remote_supports_fast_tracepoints (void)
10316 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
10320 remote_supports_static_tracepoints (void)
10322 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
10326 remote_supports_install_in_trace (void)
10328 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
10332 remote_supports_enable_disable_tracepoint (struct target_ops *self)
10334 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10339 remote_supports_string_tracing (struct target_ops *self)
10341 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
10345 remote_can_run_breakpoint_commands (struct target_ops *self)
10347 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
10351 remote_trace_init (struct target_ops *self)
10354 remote_get_noisy_reply (&target_buf, &target_buf_size);
10355 if (strcmp (target_buf, "OK") != 0)
10356 error (_("Target does not support this command."));
10359 static void free_actions_list (char **actions_list);
10360 static void free_actions_list_cleanup_wrapper (void *);
10362 free_actions_list_cleanup_wrapper (void *al)
10364 free_actions_list (al);
10368 free_actions_list (char **actions_list)
10372 if (actions_list == 0)
10375 for (ndx = 0; actions_list[ndx]; ndx++)
10376 xfree (actions_list[ndx]);
10378 xfree (actions_list);
10381 /* Recursive routine to walk through command list including loops, and
10382 download packets for each command. */
10385 remote_download_command_source (int num, ULONGEST addr,
10386 struct command_line *cmds)
10388 struct remote_state *rs = get_remote_state ();
10389 struct command_line *cmd;
10391 for (cmd = cmds; cmd; cmd = cmd->next)
10393 QUIT; /* Allow user to bail out with ^C. */
10394 strcpy (rs->buf, "QTDPsrc:");
10395 encode_source_string (num, addr, "cmd", cmd->line,
10396 rs->buf + strlen (rs->buf),
10397 rs->buf_size - strlen (rs->buf));
10399 remote_get_noisy_reply (&target_buf, &target_buf_size);
10400 if (strcmp (target_buf, "OK"))
10401 warning (_("Target does not support source download."));
10403 if (cmd->control_type == while_control
10404 || cmd->control_type == while_stepping_control)
10406 remote_download_command_source (num, addr, *cmd->body_list);
10408 QUIT; /* Allow user to bail out with ^C. */
10409 strcpy (rs->buf, "QTDPsrc:");
10410 encode_source_string (num, addr, "cmd", "end",
10411 rs->buf + strlen (rs->buf),
10412 rs->buf_size - strlen (rs->buf));
10414 remote_get_noisy_reply (&target_buf, &target_buf_size);
10415 if (strcmp (target_buf, "OK"))
10416 warning (_("Target does not support source download."));
10422 remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
10424 #define BUF_SIZE 2048
10428 char buf[BUF_SIZE];
10429 char **tdp_actions;
10430 char **stepping_actions;
10432 struct cleanup *old_chain = NULL;
10433 struct agent_expr *aexpr;
10434 struct cleanup *aexpr_chain = NULL;
10436 struct breakpoint *b = loc->owner;
10437 struct tracepoint *t = (struct tracepoint *) b;
10439 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
10440 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10442 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10445 tpaddr = loc->address;
10446 sprintf_vma (addrbuf, tpaddr);
10447 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10448 addrbuf, /* address */
10449 (b->enable_state == bp_enabled ? 'E' : 'D'),
10450 t->step_count, t->pass_count);
10451 /* Fast tracepoints are mostly handled by the target, but we can
10452 tell the target how big of an instruction block should be moved
10454 if (b->type == bp_fast_tracepoint)
10456 /* Only test for support at download time; we may not know
10457 target capabilities at definition time. */
10458 if (remote_supports_fast_tracepoints ())
10462 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10463 tpaddr, &isize, NULL))
10464 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10467 /* If it passed validation at definition but fails now,
10468 something is very wrong. */
10469 internal_error (__FILE__, __LINE__,
10470 _("Fast tracepoint not "
10471 "valid during download"));
10474 /* Fast tracepoints are functionally identical to regular
10475 tracepoints, so don't take lack of support as a reason to
10476 give up on the trace run. */
10477 warning (_("Target does not support fast tracepoints, "
10478 "downloading %d as regular tracepoint"), b->number);
10480 else if (b->type == bp_static_tracepoint)
10482 /* Only test for support at download time; we may not know
10483 target capabilities at definition time. */
10484 if (remote_supports_static_tracepoints ())
10486 struct static_tracepoint_marker marker;
10488 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10489 strcat (buf, ":S");
10491 error (_("Static tracepoint not valid during download"));
10494 /* Fast tracepoints are functionally identical to regular
10495 tracepoints, so don't take lack of support as a reason
10496 to give up on the trace run. */
10497 error (_("Target does not support static tracepoints"));
10499 /* If the tracepoint has a conditional, make it into an agent
10500 expression and append to the definition. */
10503 /* Only test support at download time, we may not know target
10504 capabilities at definition time. */
10505 if (remote_supports_cond_tracepoints ())
10507 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10508 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
10509 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10511 pkt = buf + strlen (buf);
10512 for (ndx = 0; ndx < aexpr->len; ++ndx)
10513 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10515 do_cleanups (aexpr_chain);
10518 warning (_("Target does not support conditional tracepoints, "
10519 "ignoring tp %d cond"), b->number);
10522 if (b->commands || *default_collect)
10525 remote_get_noisy_reply (&target_buf, &target_buf_size);
10526 if (strcmp (target_buf, "OK"))
10527 error (_("Target does not support tracepoints."));
10529 /* do_single_steps (t); */
10532 for (ndx = 0; tdp_actions[ndx]; ndx++)
10534 QUIT; /* Allow user to bail out with ^C. */
10535 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10536 b->number, addrbuf, /* address */
10538 ((tdp_actions[ndx + 1] || stepping_actions)
10541 remote_get_noisy_reply (&target_buf,
10543 if (strcmp (target_buf, "OK"))
10544 error (_("Error on target while setting tracepoints."));
10547 if (stepping_actions)
10549 for (ndx = 0; stepping_actions[ndx]; ndx++)
10551 QUIT; /* Allow user to bail out with ^C. */
10552 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10553 b->number, addrbuf, /* address */
10554 ((ndx == 0) ? "S" : ""),
10555 stepping_actions[ndx],
10556 (stepping_actions[ndx + 1] ? "-" : ""));
10558 remote_get_noisy_reply (&target_buf,
10560 if (strcmp (target_buf, "OK"))
10561 error (_("Error on target while setting tracepoints."));
10565 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
10567 if (b->addr_string)
10569 strcpy (buf, "QTDPsrc:");
10570 encode_source_string (b->number, loc->address,
10571 "at", b->addr_string, buf + strlen (buf),
10572 2048 - strlen (buf));
10575 remote_get_noisy_reply (&target_buf, &target_buf_size);
10576 if (strcmp (target_buf, "OK"))
10577 warning (_("Target does not support source download."));
10579 if (b->cond_string)
10581 strcpy (buf, "QTDPsrc:");
10582 encode_source_string (b->number, loc->address,
10583 "cond", b->cond_string, buf + strlen (buf),
10584 2048 - strlen (buf));
10586 remote_get_noisy_reply (&target_buf, &target_buf_size);
10587 if (strcmp (target_buf, "OK"))
10588 warning (_("Target does not support source download."));
10590 remote_download_command_source (b->number, loc->address,
10591 breakpoint_commands (b));
10594 do_cleanups (old_chain);
10598 remote_can_download_tracepoint (struct target_ops *self)
10600 struct remote_state *rs = get_remote_state ();
10601 struct trace_status *ts;
10604 /* Don't try to install tracepoints until we've relocated our
10605 symbols, and fetched and merged the target's tracepoint list with
10607 if (rs->starting_up)
10610 ts = current_trace_status ();
10611 status = remote_get_trace_status (self, ts);
10613 if (status == -1 || !ts->running_known || !ts->running)
10616 /* If we are in a tracing experiment, but remote stub doesn't support
10617 installing tracepoint in trace, we have to return. */
10618 if (!remote_supports_install_in_trace ())
10626 remote_download_trace_state_variable (struct target_ops *self,
10627 struct trace_state_variable *tsv)
10629 struct remote_state *rs = get_remote_state ();
10632 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10633 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10635 p = rs->buf + strlen (rs->buf);
10636 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10637 error (_("Trace state variable name too long for tsv definition packet"));
10638 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
10641 remote_get_noisy_reply (&target_buf, &target_buf_size);
10642 if (*target_buf == '\0')
10643 error (_("Target does not support this command."));
10644 if (strcmp (target_buf, "OK") != 0)
10645 error (_("Error on target while downloading trace state variable."));
10649 remote_enable_tracepoint (struct target_ops *self,
10650 struct bp_location *location)
10652 struct remote_state *rs = get_remote_state ();
10655 sprintf_vma (addr_buf, location->address);
10656 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10657 location->owner->number, addr_buf);
10659 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10660 if (*rs->buf == '\0')
10661 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10662 if (strcmp (rs->buf, "OK") != 0)
10663 error (_("Error on target while enabling tracepoint."));
10667 remote_disable_tracepoint (struct target_ops *self,
10668 struct bp_location *location)
10670 struct remote_state *rs = get_remote_state ();
10673 sprintf_vma (addr_buf, location->address);
10674 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10675 location->owner->number, addr_buf);
10677 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10678 if (*rs->buf == '\0')
10679 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10680 if (strcmp (rs->buf, "OK") != 0)
10681 error (_("Error on target while disabling tracepoint."));
10685 remote_trace_set_readonly_regions (struct target_ops *self)
10689 bfd_size_type size;
10695 return; /* No information to give. */
10697 strcpy (target_buf, "QTro");
10698 offset = strlen (target_buf);
10699 for (s = exec_bfd->sections; s; s = s->next)
10701 char tmp1[40], tmp2[40];
10704 if ((s->flags & SEC_LOAD) == 0 ||
10705 /* (s->flags & SEC_CODE) == 0 || */
10706 (s->flags & SEC_READONLY) == 0)
10710 vma = bfd_get_section_vma (abfd, s);
10711 size = bfd_get_section_size (s);
10712 sprintf_vma (tmp1, vma);
10713 sprintf_vma (tmp2, vma + size);
10714 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10715 if (offset + sec_length + 1 > target_buf_size)
10717 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
10719 Too many sections for read-only sections definition packet."));
10722 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10724 offset += sec_length;
10728 putpkt (target_buf);
10729 getpkt (&target_buf, &target_buf_size, 0);
10734 remote_trace_start (struct target_ops *self)
10736 putpkt ("QTStart");
10737 remote_get_noisy_reply (&target_buf, &target_buf_size);
10738 if (*target_buf == '\0')
10739 error (_("Target does not support this command."));
10740 if (strcmp (target_buf, "OK") != 0)
10741 error (_("Bogus reply from target: %s"), target_buf);
10745 remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
10747 /* Initialize it just to avoid a GCC false warning. */
10749 /* FIXME we need to get register block size some other way. */
10750 extern int trace_regblock_size;
10751 volatile struct gdb_exception ex;
10752 enum packet_result result;
10754 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
10757 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10759 putpkt ("qTStatus");
10761 TRY_CATCH (ex, RETURN_MASK_ERROR)
10763 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10767 if (ex.error != TARGET_CLOSE_ERROR)
10769 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10772 throw_exception (ex);
10775 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
10777 /* If the remote target doesn't do tracing, flag it. */
10778 if (result == PACKET_UNKNOWN)
10781 /* We're working with a live target. */
10782 ts->filename = NULL;
10785 error (_("Bogus trace status reply from target: %s"), target_buf);
10787 /* Function 'parse_trace_status' sets default value of each field of
10788 'ts' at first, so we don't have to do it here. */
10789 parse_trace_status (p, ts);
10791 return ts->running;
10795 remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
10796 struct uploaded_tp *utp)
10798 struct remote_state *rs = get_remote_state ();
10800 struct bp_location *loc;
10801 struct tracepoint *tp = (struct tracepoint *) bp;
10802 size_t size = get_remote_packet_size ();
10806 tp->base.hit_count = 0;
10807 tp->traceframe_usage = 0;
10808 for (loc = tp->base.loc; loc; loc = loc->next)
10810 /* If the tracepoint was never downloaded, don't go asking for
10812 if (tp->number_on_target == 0)
10814 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10815 phex_nz (loc->address, 0));
10817 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10818 if (reply && *reply)
10821 parse_tracepoint_status (reply + 1, bp, utp);
10827 utp->hit_count = 0;
10828 utp->traceframe_usage = 0;
10829 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10830 phex_nz (utp->addr, 0));
10832 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10833 if (reply && *reply)
10836 parse_tracepoint_status (reply + 1, bp, utp);
10842 remote_trace_stop (struct target_ops *self)
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_trace_find (struct target_ops *self,
10854 enum trace_find_type type, int num,
10855 CORE_ADDR addr1, CORE_ADDR addr2,
10858 struct remote_state *rs = get_remote_state ();
10859 char *endbuf = rs->buf + get_remote_packet_size ();
10861 int target_frameno = -1, target_tracept = -1;
10863 /* Lookups other than by absolute frame number depend on the current
10864 trace selected, so make sure it is correct on the remote end
10866 if (type != tfind_number)
10867 set_remote_traceframe ();
10870 strcpy (p, "QTFrame:");
10871 p = strchr (p, '\0');
10875 xsnprintf (p, endbuf - p, "%x", num);
10878 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
10881 xsnprintf (p, endbuf - p, "tdp:%x", num);
10884 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10885 phex_nz (addr2, 0));
10887 case tfind_outside:
10888 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10889 phex_nz (addr2, 0));
10892 error (_("Unknown trace find type %d"), type);
10896 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
10897 if (*reply == '\0')
10898 error (_("Target does not support this command."));
10900 while (reply && *reply)
10905 target_frameno = (int) strtol (p, &reply, 16);
10907 error (_("Unable to parse trace frame number"));
10908 /* Don't update our remote traceframe number cache on failure
10909 to select a remote traceframe. */
10910 if (target_frameno == -1)
10915 target_tracept = (int) strtol (p, &reply, 16);
10917 error (_("Unable to parse tracepoint number"));
10919 case 'O': /* "OK"? */
10920 if (reply[1] == 'K' && reply[2] == '\0')
10923 error (_("Bogus reply from target: %s"), reply);
10926 error (_("Bogus reply from target: %s"), reply);
10929 *tpp = target_tracept;
10931 rs->remote_traceframe_number = target_frameno;
10932 return target_frameno;
10936 remote_get_trace_state_variable_value (struct target_ops *self,
10937 int tsvnum, LONGEST *val)
10939 struct remote_state *rs = get_remote_state ();
10943 set_remote_traceframe ();
10945 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
10947 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10948 if (reply && *reply)
10952 unpack_varlen_hex (reply + 1, &uval);
10953 *val = (LONGEST) uval;
10961 remote_save_trace_data (struct target_ops *self, const char *filename)
10963 struct remote_state *rs = get_remote_state ();
10967 strcpy (p, "QTSave:");
10969 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10970 error (_("Remote file name too long for trace save packet"));
10971 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
10974 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10975 if (*reply == '\0')
10976 error (_("Target does not support this command."));
10977 if (strcmp (reply, "OK") != 0)
10978 error (_("Bogus reply from target: %s"), reply);
10982 /* This is basically a memory transfer, but needs to be its own packet
10983 because we don't know how the target actually organizes its trace
10984 memory, plus we want to be able to ask for as much as possible, but
10985 not be unhappy if we don't get as much as we ask for. */
10988 remote_get_raw_trace_data (struct target_ops *self,
10989 gdb_byte *buf, ULONGEST offset, LONGEST len)
10991 struct remote_state *rs = get_remote_state ();
10997 strcpy (p, "qTBuffer:");
10999 p += hexnumstr (p, offset);
11001 p += hexnumstr (p, len);
11005 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11006 if (reply && *reply)
11008 /* 'l' by itself means we're at the end of the buffer and
11009 there is nothing more to get. */
11013 /* Convert the reply into binary. Limit the number of bytes to
11014 convert according to our passed-in buffer size, rather than
11015 what was returned in the packet; if the target is
11016 unexpectedly generous and gives us a bigger reply than we
11017 asked for, we don't want to crash. */
11018 rslt = hex2bin (target_buf, buf, len);
11022 /* Something went wrong, flag as an error. */
11027 remote_set_disconnected_tracing (struct target_ops *self, int val)
11029 struct remote_state *rs = get_remote_state ();
11031 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
11035 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
11037 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11038 if (*reply == '\0')
11039 error (_("Target does not support this command."));
11040 if (strcmp (reply, "OK") != 0)
11041 error (_("Bogus reply from target: %s"), reply);
11044 warning (_("Target does not support disconnected tracing."));
11048 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11050 struct thread_info *info = find_thread_ptid (ptid);
11052 if (info && info->private)
11053 return info->private->core;
11058 remote_set_circular_trace_buffer (struct target_ops *self, int val)
11060 struct remote_state *rs = get_remote_state ();
11063 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
11065 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11066 if (*reply == '\0')
11067 error (_("Target does not support this command."));
11068 if (strcmp (reply, "OK") != 0)
11069 error (_("Bogus reply from target: %s"), reply);
11072 static struct traceframe_info *
11073 remote_traceframe_info (struct target_ops *self)
11077 text = target_read_stralloc (¤t_target,
11078 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11081 struct traceframe_info *info;
11082 struct cleanup *back_to = make_cleanup (xfree, text);
11084 info = parse_traceframe_info (text);
11085 do_cleanups (back_to);
11092 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11093 instruction on which a fast tracepoint may be placed. Returns -1
11094 if the packet is not supported, and 0 if the minimum instruction
11095 length is unknown. */
11098 remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
11100 struct remote_state *rs = get_remote_state ();
11103 /* If we're not debugging a process yet, the IPA can't be
11105 if (!target_has_execution)
11108 /* Make sure the remote is pointing at the right process. */
11109 set_general_process ();
11111 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
11113 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11114 if (*reply == '\0')
11118 ULONGEST min_insn_len;
11120 unpack_varlen_hex (reply, &min_insn_len);
11122 return (int) min_insn_len;
11127 remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
11129 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
11131 struct remote_state *rs = get_remote_state ();
11132 char *buf = rs->buf;
11133 char *endbuf = rs->buf + get_remote_packet_size ();
11134 enum packet_result result;
11136 gdb_assert (val >= 0 || val == -1);
11137 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11138 /* Send -1 as literal "-1" to avoid host size dependency. */
11142 buf += hexnumstr (buf, (ULONGEST) -val);
11145 buf += hexnumstr (buf, (ULONGEST) val);
11148 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11149 result = packet_ok (rs->buf,
11150 &remote_protocol_packets[PACKET_QTBuffer_size]);
11152 if (result != PACKET_OK)
11153 warning (_("Bogus reply from target: %s"), rs->buf);
11158 remote_set_trace_notes (struct target_ops *self,
11159 const char *user, const char *notes,
11160 const char *stop_notes)
11162 struct remote_state *rs = get_remote_state ();
11164 char *buf = rs->buf;
11165 char *endbuf = rs->buf + get_remote_packet_size ();
11168 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11171 buf += xsnprintf (buf, endbuf - buf, "user:");
11172 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
11178 buf += xsnprintf (buf, endbuf - buf, "notes:");
11179 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
11185 buf += xsnprintf (buf, endbuf - buf, "tstop:");
11186 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
11190 /* Ensure the buffer is terminated. */
11194 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11195 if (*reply == '\0')
11198 if (strcmp (reply, "OK") != 0)
11199 error (_("Bogus reply from target: %s"), reply);
11205 remote_use_agent (struct target_ops *self, int use)
11207 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
11209 struct remote_state *rs = get_remote_state ();
11211 /* If the stub supports QAgent. */
11212 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
11214 getpkt (&rs->buf, &rs->buf_size, 0);
11216 if (strcmp (rs->buf, "OK") == 0)
11227 remote_can_use_agent (struct target_ops *self)
11229 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
11232 struct btrace_target_info
11234 /* The ptid of the traced thread. */
11238 /* Check whether the target supports branch tracing. */
11241 remote_supports_btrace (struct target_ops *self)
11243 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
11245 if (packet_support (PACKET_Qbtrace_bts) != PACKET_ENABLE)
11247 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
11253 /* Enable branch tracing. */
11255 static struct btrace_target_info *
11256 remote_enable_btrace (struct target_ops *self, ptid_t ptid)
11258 struct btrace_target_info *tinfo = NULL;
11259 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11260 struct remote_state *rs = get_remote_state ();
11261 char *buf = rs->buf;
11262 char *endbuf = rs->buf + get_remote_packet_size ();
11264 if (packet_config_support (packet) != PACKET_ENABLE)
11265 error (_("Target does not support branch tracing."));
11267 set_general_thread (ptid);
11269 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11271 getpkt (&rs->buf, &rs->buf_size, 0);
11273 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11275 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11276 error (_("Could not enable branch tracing for %s: %s"),
11277 target_pid_to_str (ptid), rs->buf + 2);
11279 error (_("Could not enable branch tracing for %s."),
11280 target_pid_to_str (ptid));
11283 tinfo = xzalloc (sizeof (*tinfo));
11284 tinfo->ptid = ptid;
11289 /* Disable branch tracing. */
11292 remote_disable_btrace (struct target_ops *self,
11293 struct btrace_target_info *tinfo)
11295 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11296 struct remote_state *rs = get_remote_state ();
11297 char *buf = rs->buf;
11298 char *endbuf = rs->buf + get_remote_packet_size ();
11300 if (packet_config_support (packet) != PACKET_ENABLE)
11301 error (_("Target does not support branch tracing."));
11303 set_general_thread (tinfo->ptid);
11305 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11307 getpkt (&rs->buf, &rs->buf_size, 0);
11309 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11311 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11312 error (_("Could not disable branch tracing for %s: %s"),
11313 target_pid_to_str (tinfo->ptid), rs->buf + 2);
11315 error (_("Could not disable branch tracing for %s."),
11316 target_pid_to_str (tinfo->ptid));
11322 /* Teardown branch tracing. */
11325 remote_teardown_btrace (struct target_ops *self,
11326 struct btrace_target_info *tinfo)
11328 /* We must not talk to the target during teardown. */
11332 /* Read the branch trace. */
11334 static enum btrace_error
11335 remote_read_btrace (struct target_ops *self,
11336 VEC (btrace_block_s) **btrace,
11337 struct btrace_target_info *tinfo,
11338 enum btrace_read_type type)
11340 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11341 struct remote_state *rs = get_remote_state ();
11342 struct cleanup *cleanup;
11346 if (packet_config_support (packet) != PACKET_ENABLE)
11347 error (_("Target does not support branch tracing."));
11349 #if !defined(HAVE_LIBEXPAT)
11350 error (_("Cannot process branch tracing result. XML parsing not supported."));
11355 case BTRACE_READ_ALL:
11358 case BTRACE_READ_NEW:
11361 case BTRACE_READ_DELTA:
11365 internal_error (__FILE__, __LINE__,
11366 _("Bad branch tracing read type: %u."),
11367 (unsigned int) type);
11370 xml = target_read_stralloc (¤t_target,
11371 TARGET_OBJECT_BTRACE, annex);
11373 return BTRACE_ERR_UNKNOWN;
11375 cleanup = make_cleanup (xfree, xml);
11376 *btrace = parse_xml_btrace (xml);
11377 do_cleanups (cleanup);
11379 return BTRACE_ERR_NONE;
11383 remote_augmented_libraries_svr4_read (struct target_ops *self)
11385 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
11389 /* Implementation of to_load. */
11392 remote_load (struct target_ops *self, const char *name, int from_tty)
11394 generic_load (name, from_tty);
11398 init_remote_ops (void)
11400 remote_ops.to_shortname = "remote";
11401 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
11402 remote_ops.to_doc =
11403 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11404 Specify the serial device it is connected to\n\
11405 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11406 remote_ops.to_open = remote_open;
11407 remote_ops.to_close = remote_close;
11408 remote_ops.to_detach = remote_detach;
11409 remote_ops.to_disconnect = remote_disconnect;
11410 remote_ops.to_resume = remote_resume;
11411 remote_ops.to_wait = remote_wait;
11412 remote_ops.to_fetch_registers = remote_fetch_registers;
11413 remote_ops.to_store_registers = remote_store_registers;
11414 remote_ops.to_prepare_to_store = remote_prepare_to_store;
11415 remote_ops.to_files_info = remote_files_info;
11416 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
11417 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
11418 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
11419 remote_ops.to_stopped_data_address = remote_stopped_data_address;
11420 remote_ops.to_watchpoint_addr_within_range =
11421 remote_watchpoint_addr_within_range;
11422 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
11423 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
11424 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
11425 remote_ops.to_region_ok_for_hw_watchpoint
11426 = remote_region_ok_for_hw_watchpoint;
11427 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
11428 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
11429 remote_ops.to_kill = remote_kill;
11430 remote_ops.to_load = remote_load;
11431 remote_ops.to_mourn_inferior = remote_mourn;
11432 remote_ops.to_pass_signals = remote_pass_signals;
11433 remote_ops.to_program_signals = remote_program_signals;
11434 remote_ops.to_thread_alive = remote_thread_alive;
11435 remote_ops.to_find_new_threads = remote_threads_info;
11436 remote_ops.to_pid_to_str = remote_pid_to_str;
11437 remote_ops.to_extra_thread_info = remote_threads_extra_info;
11438 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
11439 remote_ops.to_stop = remote_stop;
11440 remote_ops.to_xfer_partial = remote_xfer_partial;
11441 remote_ops.to_rcmd = remote_rcmd;
11442 remote_ops.to_log_command = serial_log_command;
11443 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
11444 remote_ops.to_stratum = process_stratum;
11445 remote_ops.to_has_all_memory = default_child_has_all_memory;
11446 remote_ops.to_has_memory = default_child_has_memory;
11447 remote_ops.to_has_stack = default_child_has_stack;
11448 remote_ops.to_has_registers = default_child_has_registers;
11449 remote_ops.to_has_execution = default_child_has_execution;
11450 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
11451 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
11452 remote_ops.to_magic = OPS_MAGIC;
11453 remote_ops.to_memory_map = remote_memory_map;
11454 remote_ops.to_flash_erase = remote_flash_erase;
11455 remote_ops.to_flash_done = remote_flash_done;
11456 remote_ops.to_read_description = remote_read_description;
11457 remote_ops.to_search_memory = remote_search_memory;
11458 remote_ops.to_can_async_p = remote_can_async_p;
11459 remote_ops.to_is_async_p = remote_is_async_p;
11460 remote_ops.to_async = remote_async;
11461 remote_ops.to_terminal_inferior = remote_terminal_inferior;
11462 remote_ops.to_terminal_ours = remote_terminal_ours;
11463 remote_ops.to_supports_non_stop = remote_supports_non_stop;
11464 remote_ops.to_supports_multi_process = remote_supports_multi_process;
11465 remote_ops.to_supports_disable_randomization
11466 = remote_supports_disable_randomization;
11467 remote_ops.to_fileio_open = remote_hostio_open;
11468 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11469 remote_ops.to_fileio_pread = remote_hostio_pread;
11470 remote_ops.to_fileio_close = remote_hostio_close;
11471 remote_ops.to_fileio_unlink = remote_hostio_unlink;
11472 remote_ops.to_fileio_readlink = remote_hostio_readlink;
11473 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
11474 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
11475 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
11476 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
11477 remote_ops.to_trace_init = remote_trace_init;
11478 remote_ops.to_download_tracepoint = remote_download_tracepoint;
11479 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
11480 remote_ops.to_download_trace_state_variable
11481 = remote_download_trace_state_variable;
11482 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11483 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
11484 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11485 remote_ops.to_trace_start = remote_trace_start;
11486 remote_ops.to_get_trace_status = remote_get_trace_status;
11487 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
11488 remote_ops.to_trace_stop = remote_trace_stop;
11489 remote_ops.to_trace_find = remote_trace_find;
11490 remote_ops.to_get_trace_state_variable_value
11491 = remote_get_trace_state_variable_value;
11492 remote_ops.to_save_trace_data = remote_save_trace_data;
11493 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
11494 remote_ops.to_upload_trace_state_variables
11495 = remote_upload_trace_state_variables;
11496 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
11497 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
11498 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
11499 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
11500 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
11501 remote_ops.to_set_trace_notes = remote_set_trace_notes;
11502 remote_ops.to_core_of_thread = remote_core_of_thread;
11503 remote_ops.to_verify_memory = remote_verify_memory;
11504 remote_ops.to_get_tib_address = remote_get_tib_address;
11505 remote_ops.to_set_permissions = remote_set_permissions;
11506 remote_ops.to_static_tracepoint_marker_at
11507 = remote_static_tracepoint_marker_at;
11508 remote_ops.to_static_tracepoint_markers_by_strid
11509 = remote_static_tracepoint_markers_by_strid;
11510 remote_ops.to_traceframe_info = remote_traceframe_info;
11511 remote_ops.to_use_agent = remote_use_agent;
11512 remote_ops.to_can_use_agent = remote_can_use_agent;
11513 remote_ops.to_supports_btrace = remote_supports_btrace;
11514 remote_ops.to_enable_btrace = remote_enable_btrace;
11515 remote_ops.to_disable_btrace = remote_disable_btrace;
11516 remote_ops.to_teardown_btrace = remote_teardown_btrace;
11517 remote_ops.to_read_btrace = remote_read_btrace;
11518 remote_ops.to_augmented_libraries_svr4_read =
11519 remote_augmented_libraries_svr4_read;
11522 /* Set up the extended remote vector by making a copy of the standard
11523 remote vector and adding to it. */
11526 init_extended_remote_ops (void)
11528 extended_remote_ops = remote_ops;
11530 extended_remote_ops.to_shortname = "extended-remote";
11531 extended_remote_ops.to_longname =
11532 "Extended remote serial target in gdb-specific protocol";
11533 extended_remote_ops.to_doc =
11534 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11535 Specify the serial device it is connected to (e.g. /dev/ttya).";
11536 extended_remote_ops.to_open = extended_remote_open;
11537 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11538 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
11539 extended_remote_ops.to_detach = extended_remote_detach;
11540 extended_remote_ops.to_attach = extended_remote_attach;
11541 extended_remote_ops.to_post_attach = extended_remote_post_attach;
11542 extended_remote_ops.to_kill = extended_remote_kill;
11543 extended_remote_ops.to_supports_disable_randomization
11544 = extended_remote_supports_disable_randomization;
11548 remote_can_async_p (struct target_ops *ops)
11550 struct remote_state *rs = get_remote_state ();
11552 if (!target_async_permitted)
11553 /* We only enable async when the user specifically asks for it. */
11556 /* We're async whenever the serial device is. */
11557 return serial_can_async_p (rs->remote_desc);
11561 remote_is_async_p (struct target_ops *ops)
11563 struct remote_state *rs = get_remote_state ();
11565 if (!target_async_permitted)
11566 /* We only enable async when the user specifically asks for it. */
11569 /* We're async whenever the serial device is. */
11570 return serial_is_async_p (rs->remote_desc);
11573 /* Pass the SERIAL event on and up to the client. One day this code
11574 will be able to delay notifying the client of an event until the
11575 point where an entire packet has been received. */
11577 static serial_event_ftype remote_async_serial_handler;
11580 remote_async_serial_handler (struct serial *scb, void *context)
11582 struct remote_state *rs = context;
11584 /* Don't propogate error information up to the client. Instead let
11585 the client find out about the error by querying the target. */
11586 rs->async_client_callback (INF_REG_EVENT, rs->async_client_context);
11590 remote_async_inferior_event_handler (gdb_client_data data)
11592 inferior_event_handler (INF_REG_EVENT, NULL);
11596 remote_async (struct target_ops *ops,
11597 void (*callback) (enum inferior_event_type event_type,
11601 struct remote_state *rs = get_remote_state ();
11603 if (callback != NULL)
11605 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
11606 rs->async_client_callback = callback;
11607 rs->async_client_context = context;
11610 serial_async (rs->remote_desc, NULL, NULL);
11614 set_remote_cmd (char *args, int from_tty)
11616 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
11620 show_remote_cmd (char *args, int from_tty)
11622 /* We can't just use cmd_show_list here, because we want to skip
11623 the redundant "show remote Z-packet" and the legacy aliases. */
11624 struct cleanup *showlist_chain;
11625 struct cmd_list_element *list = remote_show_cmdlist;
11626 struct ui_out *uiout = current_uiout;
11628 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11629 for (; list != NULL; list = list->next)
11630 if (strcmp (list->name, "Z-packet") == 0)
11632 else if (list->type == not_set_cmd)
11633 /* Alias commands are exactly like the original, except they
11634 don't have the normal type. */
11638 struct cleanup *option_chain
11639 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
11641 ui_out_field_string (uiout, "name", list->name);
11642 ui_out_text (uiout, ": ");
11643 if (list->type == show_cmd)
11644 do_show_command ((char *) NULL, from_tty, list);
11646 cmd_func (list, NULL, from_tty);
11647 /* Close the tuple. */
11648 do_cleanups (option_chain);
11651 /* Close the tuple. */
11652 do_cleanups (showlist_chain);
11656 /* Function to be called whenever a new objfile (shlib) is detected. */
11658 remote_new_objfile (struct objfile *objfile)
11660 struct remote_state *rs = get_remote_state ();
11662 if (rs->remote_desc != 0) /* Have a remote connection. */
11663 remote_check_symbols ();
11666 /* Pull all the tracepoints defined on the target and create local
11667 data structures representing them. We don't want to create real
11668 tracepoints yet, we don't want to mess up the user's existing
11672 remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
11674 struct remote_state *rs = get_remote_state ();
11677 /* Ask for a first packet of tracepoint definition. */
11679 getpkt (&rs->buf, &rs->buf_size, 0);
11681 while (*p && *p != 'l')
11683 parse_tracepoint_definition (p, utpp);
11684 /* Ask for another packet of tracepoint definition. */
11686 getpkt (&rs->buf, &rs->buf_size, 0);
11693 remote_upload_trace_state_variables (struct target_ops *self,
11694 struct uploaded_tsv **utsvp)
11696 struct remote_state *rs = get_remote_state ();
11699 /* Ask for a first packet of variable definition. */
11701 getpkt (&rs->buf, &rs->buf_size, 0);
11703 while (*p && *p != 'l')
11705 parse_tsv_definition (p, utsvp);
11706 /* Ask for another packet of variable definition. */
11708 getpkt (&rs->buf, &rs->buf_size, 0);
11714 /* The "set/show range-stepping" show hook. */
11717 show_range_stepping (struct ui_file *file, int from_tty,
11718 struct cmd_list_element *c,
11721 fprintf_filtered (file,
11722 _("Debugger's willingness to use range stepping "
11723 "is %s.\n"), value);
11726 /* The "set/show range-stepping" set hook. */
11729 set_range_stepping (char *ignore_args, int from_tty,
11730 struct cmd_list_element *c)
11732 struct remote_state *rs = get_remote_state ();
11734 /* Whene enabling, check whether range stepping is actually
11735 supported by the target, and warn if not. */
11736 if (use_range_stepping)
11738 if (rs->remote_desc != NULL)
11740 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
11741 remote_vcont_probe (rs);
11743 if (packet_support (PACKET_vCont) == PACKET_ENABLE
11744 && rs->supports_vCont.r)
11748 warning (_("Range stepping is not supported by the current target"));
11753 _initialize_remote (void)
11755 struct remote_state *rs;
11756 struct cmd_list_element *cmd;
11757 const char *cmd_name;
11759 /* architecture specific data */
11760 remote_gdbarch_data_handle =
11761 gdbarch_data_register_post_init (init_remote_state);
11762 remote_g_packet_data_handle =
11763 gdbarch_data_register_pre_init (remote_g_packet_data_init);
11765 /* Initialize the per-target state. At the moment there is only one
11766 of these, not one per target. Only one target is active at a
11768 remote_state = new_remote_state ();
11770 init_remote_ops ();
11771 add_target (&remote_ops);
11773 init_extended_remote_ops ();
11774 add_target (&extended_remote_ops);
11776 /* Hook into new objfile notification. */
11777 observer_attach_new_objfile (remote_new_objfile);
11778 /* We're no longer interested in notification events of an inferior
11780 observer_attach_inferior_exit (discard_pending_stop_replies);
11782 /* Set up signal handlers. */
11783 async_sigint_remote_token =
11784 create_async_signal_handler (async_remote_interrupt, NULL);
11785 async_sigint_remote_twice_token =
11786 create_async_signal_handler (async_remote_interrupt_twice, NULL);
11789 init_remote_threadtests ();
11792 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
11793 /* set/show remote ... */
11795 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
11796 Remote protocol specific variables\n\
11797 Configure various remote-protocol specific variables such as\n\
11798 the packets being used"),
11799 &remote_set_cmdlist, "set remote ",
11800 0 /* allow-unknown */, &setlist);
11801 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
11802 Remote protocol specific variables\n\
11803 Configure various remote-protocol specific variables such as\n\
11804 the packets being used"),
11805 &remote_show_cmdlist, "show remote ",
11806 0 /* allow-unknown */, &showlist);
11808 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
11809 Compare section data on target to the exec file.\n\
11810 Argument is a single section name (default: all loaded sections).\n\
11811 To compare only read-only loaded sections, specify the -r option."),
11814 add_cmd ("packet", class_maintenance, packet_command, _("\
11815 Send an arbitrary packet to a remote target.\n\
11816 maintenance packet TEXT\n\
11817 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11818 this command sends the string TEXT to the inferior, and displays the\n\
11819 response packet. GDB supplies the initial `$' character, and the\n\
11820 terminating `#' character and checksum."),
11823 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
11824 Set whether to send break if interrupted."), _("\
11825 Show whether to send break if interrupted."), _("\
11826 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11827 set_remotebreak, show_remotebreak,
11828 &setlist, &showlist);
11829 cmd_name = "remotebreak";
11830 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
11831 deprecate_cmd (cmd, "set remote interrupt-sequence");
11832 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
11833 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
11834 deprecate_cmd (cmd, "show remote interrupt-sequence");
11836 add_setshow_enum_cmd ("interrupt-sequence", class_support,
11837 interrupt_sequence_modes, &interrupt_sequence_mode,
11839 Set interrupt sequence to remote target."), _("\
11840 Show interrupt sequence to remote target."), _("\
11841 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11842 NULL, show_interrupt_sequence,
11843 &remote_set_cmdlist,
11844 &remote_show_cmdlist);
11846 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
11847 &interrupt_on_connect, _("\
11848 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11849 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11850 If set, interrupt sequence is sent to remote target."),
11852 &remote_set_cmdlist, &remote_show_cmdlist);
11854 /* Install commands for configuring memory read/write packets. */
11856 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
11857 Set the maximum number of bytes per memory write packet (deprecated)."),
11859 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
11860 Show the maximum number of bytes per memory write packet (deprecated)."),
11862 add_cmd ("memory-write-packet-size", no_class,
11863 set_memory_write_packet_size, _("\
11864 Set the maximum number of bytes per memory-write packet.\n\
11865 Specify the number of bytes in a packet or 0 (zero) for the\n\
11866 default packet size. The actual limit is further reduced\n\
11867 dependent on the target. Specify ``fixed'' to disable the\n\
11868 further restriction and ``limit'' to enable that restriction."),
11869 &remote_set_cmdlist);
11870 add_cmd ("memory-read-packet-size", no_class,
11871 set_memory_read_packet_size, _("\
11872 Set the maximum number of bytes per memory-read packet.\n\
11873 Specify the number of bytes in a packet or 0 (zero) for the\n\
11874 default packet size. The actual limit is further reduced\n\
11875 dependent on the target. Specify ``fixed'' to disable the\n\
11876 further restriction and ``limit'' to enable that restriction."),
11877 &remote_set_cmdlist);
11878 add_cmd ("memory-write-packet-size", no_class,
11879 show_memory_write_packet_size,
11880 _("Show the maximum number of bytes per memory-write packet."),
11881 &remote_show_cmdlist);
11882 add_cmd ("memory-read-packet-size", no_class,
11883 show_memory_read_packet_size,
11884 _("Show the maximum number of bytes per memory-read packet."),
11885 &remote_show_cmdlist);
11887 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
11888 &remote_hw_watchpoint_limit, _("\
11889 Set the maximum number of target hardware watchpoints."), _("\
11890 Show the maximum number of target hardware watchpoints."), _("\
11891 Specify a negative limit for unlimited."),
11892 NULL, NULL, /* FIXME: i18n: The maximum
11893 number of target hardware
11894 watchpoints is %s. */
11895 &remote_set_cmdlist, &remote_show_cmdlist);
11896 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
11897 &remote_hw_watchpoint_length_limit, _("\
11898 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11899 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11900 Specify a negative limit for unlimited."),
11901 NULL, NULL, /* FIXME: i18n: The maximum
11902 length (in bytes) of a target
11903 hardware watchpoint is %s. */
11904 &remote_set_cmdlist, &remote_show_cmdlist);
11905 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
11906 &remote_hw_breakpoint_limit, _("\
11907 Set the maximum number of target hardware breakpoints."), _("\
11908 Show the maximum number of target hardware breakpoints."), _("\
11909 Specify a negative limit for unlimited."),
11910 NULL, NULL, /* FIXME: i18n: The maximum
11911 number of target hardware
11912 breakpoints is %s. */
11913 &remote_set_cmdlist, &remote_show_cmdlist);
11915 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
11916 &remote_address_size, _("\
11917 Set the maximum size of the address (in bits) in a memory packet."), _("\
11918 Show the maximum size of the address (in bits) in a memory packet."), NULL,
11920 NULL, /* FIXME: i18n: */
11921 &setlist, &showlist);
11923 init_all_packet_configs ();
11925 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
11926 "X", "binary-download", 1);
11928 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
11929 "vCont", "verbose-resume", 0);
11931 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
11932 "QPassSignals", "pass-signals", 0);
11934 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
11935 "QProgramSignals", "program-signals", 0);
11937 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
11938 "qSymbol", "symbol-lookup", 0);
11940 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
11941 "P", "set-register", 1);
11943 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
11944 "p", "fetch-register", 1);
11946 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
11947 "Z0", "software-breakpoint", 0);
11949 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
11950 "Z1", "hardware-breakpoint", 0);
11952 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
11953 "Z2", "write-watchpoint", 0);
11955 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
11956 "Z3", "read-watchpoint", 0);
11958 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
11959 "Z4", "access-watchpoint", 0);
11961 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
11962 "qXfer:auxv:read", "read-aux-vector", 0);
11964 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
11965 "qXfer:features:read", "target-features", 0);
11967 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
11968 "qXfer:libraries:read", "library-info", 0);
11970 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
11971 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11973 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
11974 "qXfer:memory-map:read", "memory-map", 0);
11976 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
11977 "qXfer:spu:read", "read-spu-object", 0);
11979 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
11980 "qXfer:spu:write", "write-spu-object", 0);
11982 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
11983 "qXfer:osdata:read", "osdata", 0);
11985 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
11986 "qXfer:threads:read", "threads", 0);
11988 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
11989 "qXfer:siginfo:read", "read-siginfo-object", 0);
11991 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
11992 "qXfer:siginfo:write", "write-siginfo-object", 0);
11994 add_packet_config_cmd
11995 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
11996 "qXfer:traceframe-info:read", "traceframe-info", 0);
11998 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
11999 "qXfer:uib:read", "unwind-info-block", 0);
12001 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
12002 "qGetTLSAddr", "get-thread-local-storage-address",
12005 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12006 "qGetTIBAddr", "get-thread-information-block-address",
12009 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12010 "bc", "reverse-continue", 0);
12012 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12013 "bs", "reverse-step", 0);
12015 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12016 "qSupported", "supported-packets", 0);
12018 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12019 "qSearch:memory", "search-memory", 0);
12021 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12022 "qTStatus", "trace-status", 0);
12024 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12025 "vFile:open", "hostio-open", 0);
12027 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12028 "vFile:pread", "hostio-pread", 0);
12030 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12031 "vFile:pwrite", "hostio-pwrite", 0);
12033 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12034 "vFile:close", "hostio-close", 0);
12036 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12037 "vFile:unlink", "hostio-unlink", 0);
12039 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12040 "vFile:readlink", "hostio-readlink", 0);
12042 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12043 "vAttach", "attach", 0);
12045 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12048 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12049 "QStartNoAckMode", "noack", 0);
12051 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12052 "vKill", "kill", 0);
12054 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12055 "qAttached", "query-attached", 0);
12057 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
12058 "ConditionalTracepoints",
12059 "conditional-tracepoints", 0);
12061 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12062 "ConditionalBreakpoints",
12063 "conditional-breakpoints", 0);
12065 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12066 "BreakpointCommands",
12067 "breakpoint-commands", 0);
12069 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12070 "FastTracepoints", "fast-tracepoints", 0);
12072 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12073 "TracepointSource", "TracepointSource", 0);
12075 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12076 "QAllow", "allow", 0);
12078 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12079 "StaticTracepoints", "static-tracepoints", 0);
12081 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12082 "InstallInTrace", "install-in-trace", 0);
12084 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12085 "qXfer:statictrace:read", "read-sdata-object", 0);
12087 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12088 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12090 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12091 "QDisableRandomization", "disable-randomization", 0);
12093 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12094 "QAgent", "agent", 0);
12096 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12097 "QTBuffer:size", "trace-buffer-size", 0);
12099 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12100 "Qbtrace:off", "disable-btrace", 0);
12102 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12103 "Qbtrace:bts", "enable-btrace", 0);
12105 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12106 "qXfer:btrace", "read-btrace", 0);
12108 /* Assert that we've registered commands for all packet configs. */
12112 for (i = 0; i < PACKET_MAX; i++)
12114 /* Ideally all configs would have a command associated. Some
12115 still don't though. */
12120 case PACKET_QNonStop:
12121 case PACKET_multiprocess_feature:
12122 case PACKET_EnableDisableTracepoints_feature:
12123 case PACKET_tracenz_feature:
12124 case PACKET_DisconnectedTracing_feature:
12125 case PACKET_augmented_libraries_svr4_read_feature:
12127 /* Additions to this list need to be well justified:
12128 pre-existing packets are OK; new packets are not. */
12136 /* This catches both forgetting to add a config command, and
12137 forgetting to remove a packet from the exception list. */
12138 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12142 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12143 Z sub-packet has its own set and show commands, but users may
12144 have sets to this variable in their .gdbinit files (or in their
12146 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
12147 &remote_Z_packet_detect, _("\
12148 Set use of remote protocol `Z' packets"), _("\
12149 Show use of remote protocol `Z' packets "), _("\
12150 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12152 set_remote_protocol_Z_packet_cmd,
12153 show_remote_protocol_Z_packet_cmd,
12154 /* FIXME: i18n: Use of remote protocol
12155 `Z' packets is %s. */
12156 &remote_set_cmdlist, &remote_show_cmdlist);
12158 add_prefix_cmd ("remote", class_files, remote_command, _("\
12159 Manipulate files on the remote system\n\
12160 Transfer files to and from the remote target system."),
12161 &remote_cmdlist, "remote ",
12162 0 /* allow-unknown */, &cmdlist);
12164 add_cmd ("put", class_files, remote_put_command,
12165 _("Copy a local file to the remote system."),
12168 add_cmd ("get", class_files, remote_get_command,
12169 _("Copy a remote file to the local system."),
12172 add_cmd ("delete", class_files, remote_delete_command,
12173 _("Delete a remote file."),
12176 remote_exec_file = xstrdup ("");
12177 add_setshow_string_noescape_cmd ("exec-file", class_files,
12178 &remote_exec_file, _("\
12179 Set the remote pathname for \"run\""), _("\
12180 Show the remote pathname for \"run\""), NULL, NULL, NULL,
12181 &remote_set_cmdlist, &remote_show_cmdlist);
12183 add_setshow_boolean_cmd ("range-stepping", class_run,
12184 &use_range_stepping, _("\
12185 Enable or disable range stepping."), _("\
12186 Show whether target-assisted range stepping is enabled."), _("\
12187 If on, and the target supports it, when stepping a source line, GDB\n\
12188 tells the target to step the corresponding range of addresses itself instead\n\
12189 of issuing multiple single-steps. This speeds up source level\n\
12190 stepping. If off, GDB always issues single-steps, even if range\n\
12191 stepping is supported by the target. The default is on."),
12192 set_range_stepping,
12193 show_range_stepping,
12197 /* Eventually initialize fileio. See fileio.c */
12198 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
12200 /* Take advantage of the fact that the TID field is not used, to tag
12201 special ptids with it set to != 0. */
12202 magic_null_ptid = ptid_build (42000, -1, 1);
12203 not_sent_ptid = ptid_build (42000, -2, 1);
12204 any_thread_ptid = ptid_build (42000, 0, 1);
12206 target_buf_size = 2048;
12207 target_buf = xmalloc (target_buf_size);