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 "exceptions.h"
32 /*#include "terminal.h" */
35 #include "gdb-stabs.h"
36 #include "gdbthread.h"
38 #include "remote-notif.h"
43 #include "cli/cli-decode.h"
44 #include "cli/cli-setshow.h"
45 #include "target-descriptions.h"
47 #include "filestuff.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
62 #include "gdb/fileio.h"
64 #include "xml-support.h"
66 #include "memory-map.h"
68 #include "tracepoint.h"
74 /* Temp hacks for tracepoint encoding migration. */
75 static char *target_buf;
76 static long target_buf_size;
78 /* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88 enum { REMOTE_ALIGN_WRITES = 16 };
90 /* Prototypes for local functions. */
91 static void async_cleanup_sigint_signal_handler (void *dummy);
92 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
93 static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
94 int forever, int *is_notif);
96 static void async_handle_remote_sigint (int);
97 static void async_handle_remote_sigint_twice (int);
99 static void remote_files_info (struct target_ops *ignore);
101 static void remote_prepare_to_store (struct target_ops *self,
102 struct regcache *regcache);
104 static void remote_open_1 (const char *, int, struct target_ops *,
107 static void remote_close (struct target_ops *self);
109 static void remote_mourn (struct target_ops *ops);
111 static void extended_remote_restart (void);
113 static void extended_remote_mourn (struct target_ops *);
115 static void remote_mourn_1 (struct target_ops *);
117 static void remote_send (char **buf, long *sizeof_buf_p);
119 static int readchar (int timeout);
121 static void remote_serial_write (const char *str, int len);
123 static void remote_kill (struct target_ops *ops);
125 static int remote_can_async_p (struct target_ops *);
127 static int remote_is_async_p (struct target_ops *);
129 static void remote_async (struct target_ops *ops,
130 void (*callback) (enum inferior_event_type event_type,
134 static void sync_remote_interrupt_twice (int signo);
136 static void interrupt_query (void);
138 static void set_general_thread (struct ptid ptid);
139 static void set_continue_thread (struct ptid ptid);
141 static void get_offsets (void);
143 static void skip_frame (void);
145 static long read_frame (char **buf_p, long *sizeof_buf);
147 static int hexnumlen (ULONGEST num);
149 static void init_remote_ops (void);
151 static void init_extended_remote_ops (void);
153 static void remote_stop (struct target_ops *self, ptid_t);
155 static int stubhex (int ch);
157 static int hexnumstr (char *, ULONGEST);
159 static int hexnumnstr (char *, ULONGEST, int);
161 static CORE_ADDR remote_address_masked (CORE_ADDR);
163 static void print_packet (const char *);
165 static void compare_sections_command (char *, int);
167 static void packet_command (char *, int);
169 static int stub_unpack_int (char *buff, int fieldlength);
171 static ptid_t remote_current_thread (ptid_t oldptid);
173 static void remote_find_new_threads (void);
175 static int putpkt_binary (const char *buf, int cnt);
177 static void check_binary_download (CORE_ADDR addr);
179 struct packet_config;
181 static void show_packet_config_cmd (struct packet_config *config);
183 static void show_remote_protocol_packet_cmd (struct ui_file *file,
185 struct cmd_list_element *c,
188 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
189 static ptid_t read_ptid (char *buf, char **obuf);
191 static void remote_set_permissions (struct target_ops *self);
194 static int remote_get_trace_status (struct target_ops *self,
195 struct trace_status *ts);
197 static int remote_upload_tracepoints (struct target_ops *self,
198 struct uploaded_tp **utpp);
200 static int remote_upload_trace_state_variables (struct target_ops *self,
201 struct uploaded_tsv **utsvp);
203 static void remote_query_supported (void);
205 static void remote_check_symbols (void);
207 void _initialize_remote (void);
210 static void stop_reply_xfree (struct stop_reply *);
211 static void remote_parse_stop_reply (char *, struct stop_reply *);
212 static void push_stop_reply (struct stop_reply *);
213 static void discard_pending_stop_replies_in_queue (struct remote_state *);
214 static int peek_stop_reply (ptid_t ptid);
216 static void remote_async_inferior_event_handler (gdb_client_data);
218 static void remote_terminal_ours (struct target_ops *self);
220 static int remote_read_description_p (struct target_ops *target);
222 static void remote_console_output (char *msg);
224 static int remote_supports_cond_breakpoints (struct target_ops *self);
226 static int remote_can_run_breakpoint_commands (struct target_ops *self);
230 static struct cmd_list_element *remote_cmdlist;
232 /* For "set remote" and "show remote". */
234 static struct cmd_list_element *remote_set_cmdlist;
235 static struct cmd_list_element *remote_show_cmdlist;
237 /* Stub vCont actions support.
239 Each field is a boolean flag indicating whether the stub reports
240 support for the corresponding action. */
242 struct vCont_action_support
251 /* Controls whether GDB is willing to use range stepping. */
253 static int use_range_stepping = 1;
255 #define OPAQUETHREADBYTES 8
257 /* a 64 bit opaque identifier */
258 typedef unsigned char threadref[OPAQUETHREADBYTES];
260 /* About this many threadisds fit in a packet. */
262 #define MAXTHREADLISTRESULTS 32
264 /* Description of the remote protocol state for the currently
265 connected target. This is per-target state, and independent of the
266 selected architecture. */
270 /* A buffer to use for incoming packets, and its current size. The
271 buffer is grown dynamically for larger incoming packets.
272 Outgoing packets may also be constructed in this buffer.
273 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
274 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
279 /* True if we're going through initial connection setup (finding out
280 about the remote side's threads, relocating symbols, etc.). */
283 /* If we negotiated packet size explicitly (and thus can bypass
284 heuristics for the largest packet size that will not overflow
285 a buffer in the stub), this will be set to that packet size.
286 Otherwise zero, meaning to use the guessed size. */
287 long explicit_packet_size;
289 /* remote_wait is normally called when the target is running and
290 waits for a stop reply packet. But sometimes we need to call it
291 when the target is already stopped. We can send a "?" packet
292 and have remote_wait read the response. Or, if we already have
293 the response, we can stash it in BUF and tell remote_wait to
294 skip calling getpkt. This flag is set when BUF contains a
295 stop reply packet and the target is not waiting. */
296 int cached_wait_status;
298 /* True, if in no ack mode. That is, neither GDB nor the stub will
299 expect acks from each other. The connection is assumed to be
303 /* True if we're connected in extended remote mode. */
306 /* True if we resumed the target and we're waiting for the target to
307 stop. In the mean time, we can't start another command/query.
308 The remote server wouldn't be ready to process it, so we'd
309 timeout waiting for a reply that would never come and eventually
310 we'd close the connection. This can happen in asynchronous mode
311 because we allow GDB commands while the target is running. */
312 int waiting_for_stop_reply;
314 /* The status of the stub support for the various vCont actions. */
315 struct vCont_action_support supports_vCont;
317 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
318 responded to that. */
321 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
322 remote_open knows that we don't have a file open when the program
324 struct serial *remote_desc;
326 /* These are the threads which we last sent to the remote system. The
327 TID member will be -1 for all or -2 for not sent yet. */
328 ptid_t general_thread;
329 ptid_t continue_thread;
331 /* This is the traceframe which we last selected on the remote system.
332 It will be -1 if no traceframe is selected. */
333 int remote_traceframe_number;
335 char *last_pass_packet;
337 /* The last QProgramSignals packet sent to the target. We bypass
338 sending a new program signals list down to the target if the new
339 packet is exactly the same as the last we sent. IOW, we only let
340 the target know about program signals list changes. */
341 char *last_program_signals_packet;
343 enum gdb_signal last_sent_signal;
347 char *finished_object;
348 char *finished_annex;
349 ULONGEST finished_offset;
351 /* Should we try the 'ThreadInfo' query packet?
353 This variable (NOT available to the user: auto-detect only!)
354 determines whether GDB will use the new, simpler "ThreadInfo"
355 query or the older, more complex syntax for thread queries.
356 This is an auto-detect variable (set to true at each connect,
357 and set to false when the target fails to recognize it). */
358 int use_threadinfo_query;
359 int use_threadextra_query;
361 void (*async_client_callback) (enum inferior_event_type event_type,
363 void *async_client_context;
365 /* This is set to the data address of the access causing the target
366 to stop for a watchpoint. */
367 CORE_ADDR remote_watch_data_address;
369 /* This is non-zero if target stopped for a watchpoint. */
370 int remote_stopped_by_watchpoint_p;
372 threadref echo_nextthread;
373 threadref nextthread;
374 threadref resultthreadlist[MAXTHREADLISTRESULTS];
376 /* The state of remote notification. */
377 struct remote_notif_state *notif_state;
380 /* Private data that we'll store in (struct thread_info)->private. */
381 struct private_thread_info
388 free_private_thread_info (struct private_thread_info *info)
394 /* This data could be associated with a target, but we do not always
395 have access to the current target when we need it, so for now it is
396 static. This will be fine for as long as only one target is in use
398 static struct remote_state *remote_state;
400 static struct remote_state *
401 get_remote_state_raw (void)
406 /* Allocate a new struct remote_state with xmalloc, initialize it, and
409 static struct remote_state *
410 new_remote_state (void)
412 struct remote_state *result = XCNEW (struct remote_state);
414 /* The default buffer size is unimportant; it will be expanded
415 whenever a larger buffer is needed. */
416 result->buf_size = 400;
417 result->buf = xmalloc (result->buf_size);
418 result->remote_traceframe_number = -1;
419 result->last_sent_signal = GDB_SIGNAL_0;
424 /* Description of the remote protocol for a given architecture. */
428 long offset; /* Offset into G packet. */
429 long regnum; /* GDB's internal register number. */
430 LONGEST pnum; /* Remote protocol register number. */
431 int in_g_packet; /* Always part of G packet. */
432 /* long size in bytes; == register_size (target_gdbarch (), regnum);
434 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
438 struct remote_arch_state
440 /* Description of the remote protocol registers. */
441 long sizeof_g_packet;
443 /* Description of the remote protocol registers indexed by REGNUM
444 (making an array gdbarch_num_regs in size). */
445 struct packet_reg *regs;
447 /* This is the size (in chars) of the first response to the ``g''
448 packet. It is used as a heuristic when determining the maximum
449 size of memory-read and memory-write packets. A target will
450 typically only reserve a buffer large enough to hold the ``g''
451 packet. The size does not include packet overhead (headers and
453 long actual_register_packet_size;
455 /* This is the maximum size (in chars) of a non read/write packet.
456 It is also used as a cap on the size of read/write packets. */
457 long remote_packet_size;
460 /* Utility: generate error from an incoming stub packet. */
462 trace_error (char *buf)
465 return; /* not an error msg */
468 case '1': /* malformed packet error */
469 if (*++buf == '0') /* general case: */
470 error (_("remote.c: error in outgoing packet."));
472 error (_("remote.c: error in outgoing packet at field #%ld."),
473 strtol (buf, NULL, 16));
475 error (_("Target returns error code '%s'."), buf);
479 /* Utility: wait for reply from stub, while accepting "O" packets. */
481 remote_get_noisy_reply (char **buf_p,
484 do /* Loop on reply from remote stub. */
488 QUIT; /* Allow user to bail out with ^C. */
489 getpkt (buf_p, sizeof_buf, 0);
493 else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
496 CORE_ADDR from, to, org_to;
498 int adjusted_size = 0;
499 volatile struct gdb_exception ex;
501 p = buf + strlen ("qRelocInsn:");
502 pp = unpack_varlen_hex (p, &ul);
504 error (_("invalid qRelocInsn packet: %s"), buf);
508 unpack_varlen_hex (p, &ul);
513 TRY_CATCH (ex, RETURN_MASK_ALL)
515 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
519 adjusted_size = to - org_to;
521 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
524 else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
526 /* Propagate memory errors silently back to the target.
527 The stub may have limited the range of addresses we
528 can write to, for example. */
533 /* Something unexpectedly bad happened. Be verbose so
534 we can tell what, and propagate the error back to the
535 stub, so it doesn't get stuck waiting for a
537 exception_fprintf (gdb_stderr, ex,
538 _("warning: relocating instruction: "));
542 else if (buf[0] == 'O' && buf[1] != 'K')
543 remote_console_output (buf + 1); /* 'O' message from stub */
545 return buf; /* Here's the actual reply. */
550 /* Handle for retreving the remote protocol data from gdbarch. */
551 static struct gdbarch_data *remote_gdbarch_data_handle;
553 static struct remote_arch_state *
554 get_remote_arch_state (void)
556 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
559 /* Fetch the global remote target state. */
561 static struct remote_state *
562 get_remote_state (void)
564 /* Make sure that the remote architecture state has been
565 initialized, because doing so might reallocate rs->buf. Any
566 function which calls getpkt also needs to be mindful of changes
567 to rs->buf, but this call limits the number of places which run
569 get_remote_arch_state ();
571 return get_remote_state_raw ();
575 compare_pnums (const void *lhs_, const void *rhs_)
577 const struct packet_reg * const *lhs = lhs_;
578 const struct packet_reg * const *rhs = rhs_;
580 if ((*lhs)->pnum < (*rhs)->pnum)
582 else if ((*lhs)->pnum == (*rhs)->pnum)
589 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
591 int regnum, num_remote_regs, offset;
592 struct packet_reg **remote_regs;
594 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
596 struct packet_reg *r = ®s[regnum];
598 if (register_size (gdbarch, regnum) == 0)
599 /* Do not try to fetch zero-sized (placeholder) registers. */
602 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
607 /* Define the g/G packet format as the contents of each register
608 with a remote protocol number, in order of ascending protocol
611 remote_regs = alloca (gdbarch_num_regs (gdbarch)
612 * sizeof (struct packet_reg *));
613 for (num_remote_regs = 0, regnum = 0;
614 regnum < gdbarch_num_regs (gdbarch);
616 if (regs[regnum].pnum != -1)
617 remote_regs[num_remote_regs++] = ®s[regnum];
619 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
622 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
624 remote_regs[regnum]->in_g_packet = 1;
625 remote_regs[regnum]->offset = offset;
626 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
632 /* Given the architecture described by GDBARCH, return the remote
633 protocol register's number and the register's offset in the g/G
634 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
635 If the target does not have a mapping for REGNUM, return false,
636 otherwise, return true. */
639 remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
640 int *pnum, int *poffset)
643 struct packet_reg *regs;
644 struct cleanup *old_chain;
646 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
648 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
649 old_chain = make_cleanup (xfree, regs);
651 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
653 *pnum = regs[regnum].pnum;
654 *poffset = regs[regnum].offset;
656 do_cleanups (old_chain);
662 init_remote_state (struct gdbarch *gdbarch)
664 struct remote_state *rs = get_remote_state_raw ();
665 struct remote_arch_state *rsa;
667 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
669 /* Use the architecture to build a regnum<->pnum table, which will be
670 1:1 unless a feature set specifies otherwise. */
671 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
672 gdbarch_num_regs (gdbarch),
675 /* Record the maximum possible size of the g packet - it may turn out
677 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
679 /* Default maximum number of characters in a packet body. Many
680 remote stubs have a hardwired buffer size of 400 bytes
681 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
682 as the maximum packet-size to ensure that the packet and an extra
683 NUL character can always fit in the buffer. This stops GDB
684 trashing stubs that try to squeeze an extra NUL into what is
685 already a full buffer (As of 1999-12-04 that was most stubs). */
686 rsa->remote_packet_size = 400 - 1;
688 /* This one is filled in when a ``g'' packet is received. */
689 rsa->actual_register_packet_size = 0;
691 /* Should rsa->sizeof_g_packet needs more space than the
692 default, adjust the size accordingly. Remember that each byte is
693 encoded as two characters. 32 is the overhead for the packet
694 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
695 (``$NN:G...#NN'') is a better guess, the below has been padded a
697 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
698 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
700 /* Make sure that the packet buffer is plenty big enough for
701 this architecture. */
702 if (rs->buf_size < rsa->remote_packet_size)
704 rs->buf_size = 2 * rsa->remote_packet_size;
705 rs->buf = xrealloc (rs->buf, rs->buf_size);
711 /* Return the current allowed size of a remote packet. This is
712 inferred from the current architecture, and should be used to
713 limit the length of outgoing packets. */
715 get_remote_packet_size (void)
717 struct remote_state *rs = get_remote_state ();
718 struct remote_arch_state *rsa = get_remote_arch_state ();
720 if (rs->explicit_packet_size)
721 return rs->explicit_packet_size;
723 return rsa->remote_packet_size;
726 static struct packet_reg *
727 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
729 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
733 struct packet_reg *r = &rsa->regs[regnum];
735 gdb_assert (r->regnum == regnum);
740 static struct packet_reg *
741 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
745 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
747 struct packet_reg *r = &rsa->regs[i];
755 static struct target_ops remote_ops;
757 static struct target_ops extended_remote_ops;
759 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
760 ``forever'' still use the normal timeout mechanism. This is
761 currently used by the ASYNC code to guarentee that target reads
762 during the initial connect always time-out. Once getpkt has been
763 modified to return a timeout indication and, in turn
764 remote_wait()/wait_for_inferior() have gained a timeout parameter
766 static int wait_forever_enabled_p = 1;
768 /* Allow the user to specify what sequence to send to the remote
769 when he requests a program interruption: Although ^C is usually
770 what remote systems expect (this is the default, here), it is
771 sometimes preferable to send a break. On other systems such
772 as the Linux kernel, a break followed by g, which is Magic SysRq g
773 is required in order to interrupt the execution. */
774 const char interrupt_sequence_control_c[] = "Ctrl-C";
775 const char interrupt_sequence_break[] = "BREAK";
776 const char interrupt_sequence_break_g[] = "BREAK-g";
777 static const char *const interrupt_sequence_modes[] =
779 interrupt_sequence_control_c,
780 interrupt_sequence_break,
781 interrupt_sequence_break_g,
784 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
787 show_interrupt_sequence (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c,
791 if (interrupt_sequence_mode == interrupt_sequence_control_c)
792 fprintf_filtered (file,
793 _("Send the ASCII ETX character (Ctrl-c) "
794 "to the remote target to interrupt the "
795 "execution of the program.\n"));
796 else if (interrupt_sequence_mode == interrupt_sequence_break)
797 fprintf_filtered (file,
798 _("send a break signal to the remote target "
799 "to interrupt the execution of the program.\n"));
800 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
801 fprintf_filtered (file,
802 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
803 "the remote target to interrupt the execution "
804 "of Linux kernel.\n"));
806 internal_error (__FILE__, __LINE__,
807 _("Invalid value for interrupt_sequence_mode: %s."),
808 interrupt_sequence_mode);
811 /* This boolean variable specifies whether interrupt_sequence is sent
812 to the remote target when gdb connects to it.
813 This is mostly needed when you debug the Linux kernel: The Linux kernel
814 expects BREAK g which is Magic SysRq g for connecting gdb. */
815 static int interrupt_on_connect = 0;
817 /* This variable is used to implement the "set/show remotebreak" commands.
818 Since these commands are now deprecated in favor of "set/show remote
819 interrupt-sequence", it no longer has any effect on the code. */
820 static int remote_break;
823 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
826 interrupt_sequence_mode = interrupt_sequence_break;
828 interrupt_sequence_mode = interrupt_sequence_control_c;
832 show_remotebreak (struct ui_file *file, int from_tty,
833 struct cmd_list_element *c,
838 /* This variable sets the number of bits in an address that are to be
839 sent in a memory ("M" or "m") packet. Normally, after stripping
840 leading zeros, the entire address would be sent. This variable
841 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
842 initial implementation of remote.c restricted the address sent in
843 memory packets to ``host::sizeof long'' bytes - (typically 32
844 bits). Consequently, for 64 bit targets, the upper 32 bits of an
845 address was never sent. Since fixing this bug may cause a break in
846 some remote targets this variable is principly provided to
847 facilitate backward compatibility. */
849 static unsigned int remote_address_size;
851 /* Temporary to track who currently owns the terminal. See
852 remote_terminal_* for more details. */
854 static int remote_async_terminal_ours_p;
856 /* The executable file to use for "run" on the remote side. */
858 static char *remote_exec_file = "";
861 /* User configurable variables for the number of characters in a
862 memory read/write packet. MIN (rsa->remote_packet_size,
863 rsa->sizeof_g_packet) is the default. Some targets need smaller
864 values (fifo overruns, et.al.) and some users need larger values
865 (speed up transfers). The variables ``preferred_*'' (the user
866 request), ``current_*'' (what was actually set) and ``forced_*''
867 (Positive - a soft limit, negative - a hard limit). */
869 struct memory_packet_config
876 /* Compute the current size of a read/write packet. Since this makes
877 use of ``actual_register_packet_size'' the computation is dynamic. */
880 get_memory_packet_size (struct memory_packet_config *config)
882 struct remote_state *rs = get_remote_state ();
883 struct remote_arch_state *rsa = get_remote_arch_state ();
885 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
886 law?) that some hosts don't cope very well with large alloca()
887 calls. Eventually the alloca() code will be replaced by calls to
888 xmalloc() and make_cleanups() allowing this restriction to either
889 be lifted or removed. */
890 #ifndef MAX_REMOTE_PACKET_SIZE
891 #define MAX_REMOTE_PACKET_SIZE 16384
893 /* NOTE: 20 ensures we can write at least one byte. */
894 #ifndef MIN_REMOTE_PACKET_SIZE
895 #define MIN_REMOTE_PACKET_SIZE 20
900 if (config->size <= 0)
901 what_they_get = MAX_REMOTE_PACKET_SIZE;
903 what_they_get = config->size;
907 what_they_get = get_remote_packet_size ();
908 /* Limit the packet to the size specified by the user. */
910 && what_they_get > config->size)
911 what_they_get = config->size;
913 /* Limit it to the size of the targets ``g'' response unless we have
914 permission from the stub to use a larger packet size. */
915 if (rs->explicit_packet_size == 0
916 && rsa->actual_register_packet_size > 0
917 && what_they_get > rsa->actual_register_packet_size)
918 what_they_get = rsa->actual_register_packet_size;
920 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
921 what_they_get = MAX_REMOTE_PACKET_SIZE;
922 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
923 what_they_get = MIN_REMOTE_PACKET_SIZE;
925 /* Make sure there is room in the global buffer for this packet
926 (including its trailing NUL byte). */
927 if (rs->buf_size < what_they_get + 1)
929 rs->buf_size = 2 * what_they_get;
930 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
933 return what_they_get;
936 /* Update the size of a read/write packet. If they user wants
937 something really big then do a sanity check. */
940 set_memory_packet_size (char *args, struct memory_packet_config *config)
942 int fixed_p = config->fixed_p;
943 long size = config->size;
946 error (_("Argument required (integer, `fixed' or `limited')."));
947 else if (strcmp (args, "hard") == 0
948 || strcmp (args, "fixed") == 0)
950 else if (strcmp (args, "soft") == 0
951 || strcmp (args, "limit") == 0)
957 size = strtoul (args, &end, 0);
959 error (_("Invalid %s (bad syntax)."), config->name);
961 /* Instead of explicitly capping the size of a packet to
962 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
963 instead allowed to set the size to something arbitrarily
965 if (size > MAX_REMOTE_PACKET_SIZE)
966 error (_("Invalid %s (too large)."), config->name);
970 if (fixed_p && !config->fixed_p)
972 if (! query (_("The target may not be able to correctly handle a %s\n"
973 "of %ld bytes. Change the packet size? "),
975 error (_("Packet size not changed."));
977 /* Update the config. */
978 config->fixed_p = fixed_p;
983 show_memory_packet_size (struct memory_packet_config *config)
985 printf_filtered (_("The %s is %ld. "), config->name, config->size);
987 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
988 get_memory_packet_size (config));
990 printf_filtered (_("Packets are limited to %ld bytes.\n"),
991 get_memory_packet_size (config));
994 static struct memory_packet_config memory_write_packet_config =
996 "memory-write-packet-size",
1000 set_memory_write_packet_size (char *args, int from_tty)
1002 set_memory_packet_size (args, &memory_write_packet_config);
1006 show_memory_write_packet_size (char *args, int from_tty)
1008 show_memory_packet_size (&memory_write_packet_config);
1012 get_memory_write_packet_size (void)
1014 return get_memory_packet_size (&memory_write_packet_config);
1017 static struct memory_packet_config memory_read_packet_config =
1019 "memory-read-packet-size",
1023 set_memory_read_packet_size (char *args, int from_tty)
1025 set_memory_packet_size (args, &memory_read_packet_config);
1029 show_memory_read_packet_size (char *args, int from_tty)
1031 show_memory_packet_size (&memory_read_packet_config);
1035 get_memory_read_packet_size (void)
1037 long size = get_memory_packet_size (&memory_read_packet_config);
1039 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1040 extra buffer size argument before the memory read size can be
1041 increased beyond this. */
1042 if (size > get_remote_packet_size ())
1043 size = get_remote_packet_size ();
1048 /* Generic configuration support for packets the stub optionally
1049 supports. Allows the user to specify the use of the packet as well
1050 as allowing GDB to auto-detect support in the remote stub. */
1054 PACKET_SUPPORT_UNKNOWN = 0,
1059 struct packet_config
1064 /* If auto, GDB auto-detects support for this packet or feature,
1065 either through qSupported, or by trying the packet and looking
1066 at the response. If true, GDB assumes the target supports this
1067 packet. If false, the packet is disabled. Configs that don't
1068 have an associated command always have this set to auto. */
1069 enum auto_boolean detect;
1071 /* Does the target support this packet? */
1072 enum packet_support support;
1075 /* Analyze a packet's return value and update the packet config
1085 static enum packet_support packet_config_support (struct packet_config *config);
1086 static enum packet_support packet_support (int packet);
1089 show_packet_config_cmd (struct packet_config *config)
1091 char *support = "internal-error";
1093 switch (packet_config_support (config))
1096 support = "enabled";
1098 case PACKET_DISABLE:
1099 support = "disabled";
1101 case PACKET_SUPPORT_UNKNOWN:
1102 support = "unknown";
1105 switch (config->detect)
1107 case AUTO_BOOLEAN_AUTO:
1108 printf_filtered (_("Support for the `%s' packet "
1109 "is auto-detected, currently %s.\n"),
1110 config->name, support);
1112 case AUTO_BOOLEAN_TRUE:
1113 case AUTO_BOOLEAN_FALSE:
1114 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1115 config->name, support);
1121 add_packet_config_cmd (struct packet_config *config, const char *name,
1122 const char *title, int legacy)
1128 config->name = name;
1129 config->title = title;
1130 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1132 show_doc = xstrprintf ("Show current use of remote "
1133 "protocol `%s' (%s) packet",
1135 /* set/show TITLE-packet {auto,on,off} */
1136 cmd_name = xstrprintf ("%s-packet", title);
1137 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1138 &config->detect, set_doc,
1139 show_doc, NULL, /* help_doc */
1141 show_remote_protocol_packet_cmd,
1142 &remote_set_cmdlist, &remote_show_cmdlist);
1143 /* The command code copies the documentation strings. */
1146 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1151 legacy_name = xstrprintf ("%s-packet", name);
1152 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1153 &remote_set_cmdlist);
1154 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1155 &remote_show_cmdlist);
1159 static enum packet_result
1160 packet_check_result (const char *buf)
1164 /* The stub recognized the packet request. Check that the
1165 operation succeeded. */
1167 && isxdigit (buf[1]) && isxdigit (buf[2])
1169 /* "Enn" - definitly an error. */
1170 return PACKET_ERROR;
1172 /* Always treat "E." as an error. This will be used for
1173 more verbose error messages, such as E.memtypes. */
1174 if (buf[0] == 'E' && buf[1] == '.')
1175 return PACKET_ERROR;
1177 /* The packet may or may not be OK. Just assume it is. */
1181 /* The stub does not support the packet. */
1182 return PACKET_UNKNOWN;
1185 static enum packet_result
1186 packet_ok (const char *buf, struct packet_config *config)
1188 enum packet_result result;
1190 if (config->detect != AUTO_BOOLEAN_TRUE
1191 && config->support == PACKET_DISABLE)
1192 internal_error (__FILE__, __LINE__,
1193 _("packet_ok: attempt to use a disabled packet"));
1195 result = packet_check_result (buf);
1200 /* The stub recognized the packet request. */
1201 if (config->support == PACKET_SUPPORT_UNKNOWN)
1204 fprintf_unfiltered (gdb_stdlog,
1205 "Packet %s (%s) is supported\n",
1206 config->name, config->title);
1207 config->support = PACKET_ENABLE;
1210 case PACKET_UNKNOWN:
1211 /* The stub does not support the packet. */
1212 if (config->detect == AUTO_BOOLEAN_AUTO
1213 && config->support == PACKET_ENABLE)
1215 /* If the stub previously indicated that the packet was
1216 supported then there is a protocol error. */
1217 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1218 config->name, config->title);
1220 else if (config->detect == AUTO_BOOLEAN_TRUE)
1222 /* The user set it wrong. */
1223 error (_("Enabled packet %s (%s) not recognized by stub"),
1224 config->name, config->title);
1228 fprintf_unfiltered (gdb_stdlog,
1229 "Packet %s (%s) is NOT supported\n",
1230 config->name, config->title);
1231 config->support = PACKET_DISABLE;
1251 PACKET_vFile_pwrite,
1253 PACKET_vFile_unlink,
1254 PACKET_vFile_readlink,
1256 PACKET_qXfer_features,
1257 PACKET_qXfer_libraries,
1258 PACKET_qXfer_libraries_svr4,
1259 PACKET_qXfer_memory_map,
1260 PACKET_qXfer_spu_read,
1261 PACKET_qXfer_spu_write,
1262 PACKET_qXfer_osdata,
1263 PACKET_qXfer_threads,
1264 PACKET_qXfer_statictrace_read,
1265 PACKET_qXfer_traceframe_info,
1271 PACKET_QPassSignals,
1272 PACKET_QProgramSignals,
1274 PACKET_qSearch_memory,
1277 PACKET_QStartNoAckMode,
1279 PACKET_qXfer_siginfo_read,
1280 PACKET_qXfer_siginfo_write,
1283 /* Support for conditional tracepoints. */
1284 PACKET_ConditionalTracepoints,
1286 /* Support for target-side breakpoint conditions. */
1287 PACKET_ConditionalBreakpoints,
1289 /* Support for target-side breakpoint commands. */
1290 PACKET_BreakpointCommands,
1292 /* Support for fast tracepoints. */
1293 PACKET_FastTracepoints,
1295 /* Support for static tracepoints. */
1296 PACKET_StaticTracepoints,
1298 /* Support for installing tracepoints while a trace experiment is
1300 PACKET_InstallInTrace,
1304 PACKET_TracepointSource,
1307 PACKET_QDisableRandomization,
1309 PACKET_QTBuffer_size,
1312 PACKET_qXfer_btrace,
1314 /* Support for the QNonStop packet. */
1317 /* Support for multi-process extensions. */
1318 PACKET_multiprocess_feature,
1320 /* Support for enabling and disabling tracepoints while a trace
1321 experiment is running. */
1322 PACKET_EnableDisableTracepoints_feature,
1324 /* Support for collecting strings using the tracenz bytecode. */
1325 PACKET_tracenz_feature,
1327 /* Support for continuing to run a trace experiment while GDB is
1329 PACKET_DisconnectedTracing_feature,
1331 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1332 PACKET_augmented_libraries_svr4_read_feature,
1337 static struct packet_config remote_protocol_packets[PACKET_MAX];
1339 /* Returns whether a given packet or feature is supported. This takes
1340 into account the state of the corresponding "set remote foo-packet"
1341 command, which may be used to bypass auto-detection. */
1343 static enum packet_support
1344 packet_config_support (struct packet_config *config)
1346 switch (config->detect)
1348 case AUTO_BOOLEAN_TRUE:
1349 return PACKET_ENABLE;
1350 case AUTO_BOOLEAN_FALSE:
1351 return PACKET_DISABLE;
1352 case AUTO_BOOLEAN_AUTO:
1353 return config->support;
1355 gdb_assert_not_reached (_("bad switch"));
1359 /* Same as packet_config_support, but takes the packet's enum value as
1362 static enum packet_support
1363 packet_support (int packet)
1365 struct packet_config *config = &remote_protocol_packets[packet];
1367 return packet_config_support (config);
1371 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1372 struct cmd_list_element *c,
1375 struct packet_config *packet;
1377 for (packet = remote_protocol_packets;
1378 packet < &remote_protocol_packets[PACKET_MAX];
1381 if (&packet->detect == c->var)
1383 show_packet_config_cmd (packet);
1387 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1391 /* Should we try one of the 'Z' requests? */
1395 Z_PACKET_SOFTWARE_BP,
1396 Z_PACKET_HARDWARE_BP,
1403 /* For compatibility with older distributions. Provide a ``set remote
1404 Z-packet ...'' command that updates all the Z packet types. */
1406 static enum auto_boolean remote_Z_packet_detect;
1409 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1410 struct cmd_list_element *c)
1414 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1415 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1419 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1420 struct cmd_list_element *c,
1425 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1427 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1431 /* Returns true if the multi-process extensions are in effect. */
1434 remote_multi_process_p (struct remote_state *rs)
1436 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1439 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1440 static struct async_signal_handler *async_sigint_remote_twice_token;
1441 static struct async_signal_handler *async_sigint_remote_token;
1444 /* Asynchronous signal handle registered as event loop source for
1445 when we have pending events ready to be passed to the core. */
1447 static struct async_event_handler *remote_async_inferior_event_token;
1451 static ptid_t magic_null_ptid;
1452 static ptid_t not_sent_ptid;
1453 static ptid_t any_thread_ptid;
1455 /* Find out if the stub attached to PID (and hence GDB should offer to
1456 detach instead of killing it when bailing out). */
1459 remote_query_attached (int pid)
1461 struct remote_state *rs = get_remote_state ();
1462 size_t size = get_remote_packet_size ();
1464 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
1467 if (remote_multi_process_p (rs))
1468 xsnprintf (rs->buf, size, "qAttached:%x", pid);
1470 xsnprintf (rs->buf, size, "qAttached");
1473 getpkt (&rs->buf, &rs->buf_size, 0);
1475 switch (packet_ok (rs->buf,
1476 &remote_protocol_packets[PACKET_qAttached]))
1479 if (strcmp (rs->buf, "1") == 0)
1483 warning (_("Remote failure reply: %s"), rs->buf);
1485 case PACKET_UNKNOWN:
1492 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1493 has been invented by GDB, instead of reported by the target. Since
1494 we can be connected to a remote system before before knowing about
1495 any inferior, mark the target with execution when we find the first
1496 inferior. If ATTACHED is 1, then we had just attached to this
1497 inferior. If it is 0, then we just created this inferior. If it
1498 is -1, then try querying the remote stub to find out if it had
1499 attached to the inferior or not. */
1501 static struct inferior *
1502 remote_add_inferior (int fake_pid_p, int pid, int attached)
1504 struct inferior *inf;
1506 /* Check whether this process we're learning about is to be
1507 considered attached, or if is to be considered to have been
1508 spawned by the stub. */
1510 attached = remote_query_attached (pid);
1512 if (gdbarch_has_global_solist (target_gdbarch ()))
1514 /* If the target shares code across all inferiors, then every
1515 attach adds a new inferior. */
1516 inf = add_inferior (pid);
1518 /* ... and every inferior is bound to the same program space.
1519 However, each inferior may still have its own address
1521 inf->aspace = maybe_new_address_space ();
1522 inf->pspace = current_program_space;
1526 /* In the traditional debugging scenario, there's a 1-1 match
1527 between program/address spaces. We simply bind the inferior
1528 to the program space's address space. */
1529 inf = current_inferior ();
1530 inferior_appeared (inf, pid);
1533 inf->attach_flag = attached;
1534 inf->fake_pid_p = fake_pid_p;
1539 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1540 according to RUNNING. */
1543 remote_add_thread (ptid_t ptid, int running)
1545 struct remote_state *rs = get_remote_state ();
1547 /* GDB historically didn't pull threads in the initial connection
1548 setup. If the remote target doesn't even have a concept of
1549 threads (e.g., a bare-metal target), even if internally we
1550 consider that a single-threaded target, mentioning a new thread
1551 might be confusing to the user. Be silent then, preserving the
1552 age old behavior. */
1553 if (rs->starting_up)
1554 add_thread_silent (ptid);
1558 set_executing (ptid, running);
1559 set_running (ptid, running);
1562 /* Come here when we learn about a thread id from the remote target.
1563 It may be the first time we hear about such thread, so take the
1564 opportunity to add it to GDB's thread list. In case this is the
1565 first time we're noticing its corresponding inferior, add it to
1566 GDB's inferior list as well. */
1569 remote_notice_new_inferior (ptid_t currthread, int running)
1571 /* If this is a new thread, add it to GDB's thread list.
1572 If we leave it up to WFI to do this, bad things will happen. */
1574 if (in_thread_list (currthread) && is_exited (currthread))
1576 /* We're seeing an event on a thread id we knew had exited.
1577 This has to be a new thread reusing the old id. Add it. */
1578 remote_add_thread (currthread, running);
1582 if (!in_thread_list (currthread))
1584 struct inferior *inf = NULL;
1585 int pid = ptid_get_pid (currthread);
1587 if (ptid_is_pid (inferior_ptid)
1588 && pid == ptid_get_pid (inferior_ptid))
1590 /* inferior_ptid has no thread member yet. This can happen
1591 with the vAttach -> remote_wait,"TAAthread:" path if the
1592 stub doesn't support qC. This is the first stop reported
1593 after an attach, so this is the main thread. Update the
1594 ptid in the thread list. */
1595 if (in_thread_list (pid_to_ptid (pid)))
1596 thread_change_ptid (inferior_ptid, currthread);
1599 remote_add_thread (currthread, running);
1600 inferior_ptid = currthread;
1605 if (ptid_equal (magic_null_ptid, inferior_ptid))
1607 /* inferior_ptid is not set yet. This can happen with the
1608 vRun -> remote_wait,"TAAthread:" path if the stub
1609 doesn't support qC. This is the first stop reported
1610 after an attach, so this is the main thread. Update the
1611 ptid in the thread list. */
1612 thread_change_ptid (inferior_ptid, currthread);
1616 /* When connecting to a target remote, or to a target
1617 extended-remote which already was debugging an inferior, we
1618 may not know about it yet. Add it before adding its child
1619 thread, so notifications are emitted in a sensible order. */
1620 if (!in_inferior_list (ptid_get_pid (currthread)))
1622 struct remote_state *rs = get_remote_state ();
1623 int fake_pid_p = !remote_multi_process_p (rs);
1625 inf = remote_add_inferior (fake_pid_p,
1626 ptid_get_pid (currthread), -1);
1629 /* This is really a new thread. Add it. */
1630 remote_add_thread (currthread, running);
1632 /* If we found a new inferior, let the common code do whatever
1633 it needs to with it (e.g., read shared libraries, insert
1634 breakpoints), unless we're just setting up an all-stop
1638 struct remote_state *rs = get_remote_state ();
1640 if (non_stop || !rs->starting_up)
1641 notice_new_inferior (currthread, running, 0);
1646 /* Return the private thread data, creating it if necessary. */
1648 static struct private_thread_info *
1649 demand_private_info (ptid_t ptid)
1651 struct thread_info *info = find_thread_ptid (ptid);
1657 info->private = xmalloc (sizeof (*(info->private)));
1658 info->private_dtor = free_private_thread_info;
1659 info->private->core = -1;
1660 info->private->extra = 0;
1663 return info->private;
1666 /* Call this function as a result of
1667 1) A halt indication (T packet) containing a thread id
1668 2) A direct query of currthread
1669 3) Successful execution of set thread */
1672 record_currthread (struct remote_state *rs, ptid_t currthread)
1674 rs->general_thread = currthread;
1677 /* If 'QPassSignals' is supported, tell the remote stub what signals
1678 it can simply pass through to the inferior without reporting. */
1681 remote_pass_signals (struct target_ops *self,
1682 int numsigs, unsigned char *pass_signals)
1684 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
1686 char *pass_packet, *p;
1688 struct remote_state *rs = get_remote_state ();
1690 gdb_assert (numsigs < 256);
1691 for (i = 0; i < numsigs; i++)
1693 if (pass_signals[i])
1696 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1697 strcpy (pass_packet, "QPassSignals:");
1698 p = pass_packet + strlen (pass_packet);
1699 for (i = 0; i < numsigs; i++)
1701 if (pass_signals[i])
1704 *p++ = tohex (i >> 4);
1705 *p++ = tohex (i & 15);
1714 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
1716 putpkt (pass_packet);
1717 getpkt (&rs->buf, &rs->buf_size, 0);
1718 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
1719 if (rs->last_pass_packet)
1720 xfree (rs->last_pass_packet);
1721 rs->last_pass_packet = pass_packet;
1724 xfree (pass_packet);
1728 /* If 'QProgramSignals' is supported, tell the remote stub what
1729 signals it should pass through to the inferior when detaching. */
1732 remote_program_signals (struct target_ops *self,
1733 int numsigs, unsigned char *signals)
1735 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
1739 struct remote_state *rs = get_remote_state ();
1741 gdb_assert (numsigs < 256);
1742 for (i = 0; i < numsigs; i++)
1747 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1748 strcpy (packet, "QProgramSignals:");
1749 p = packet + strlen (packet);
1750 for (i = 0; i < numsigs; i++)
1752 if (signal_pass_state (i))
1755 *p++ = tohex (i >> 4);
1756 *p++ = tohex (i & 15);
1765 if (!rs->last_program_signals_packet
1766 || strcmp (rs->last_program_signals_packet, packet) != 0)
1769 getpkt (&rs->buf, &rs->buf_size, 0);
1770 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1771 xfree (rs->last_program_signals_packet);
1772 rs->last_program_signals_packet = packet;
1779 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1780 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1781 thread. If GEN is set, set the general thread, if not, then set
1782 the step/continue thread. */
1784 set_thread (struct ptid ptid, int gen)
1786 struct remote_state *rs = get_remote_state ();
1787 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
1788 char *buf = rs->buf;
1789 char *endbuf = rs->buf + get_remote_packet_size ();
1791 if (ptid_equal (state, ptid))
1795 *buf++ = gen ? 'g' : 'c';
1796 if (ptid_equal (ptid, magic_null_ptid))
1797 xsnprintf (buf, endbuf - buf, "0");
1798 else if (ptid_equal (ptid, any_thread_ptid))
1799 xsnprintf (buf, endbuf - buf, "0");
1800 else if (ptid_equal (ptid, minus_one_ptid))
1801 xsnprintf (buf, endbuf - buf, "-1");
1803 write_ptid (buf, endbuf, ptid);
1805 getpkt (&rs->buf, &rs->buf_size, 0);
1807 rs->general_thread = ptid;
1809 rs->continue_thread = ptid;
1813 set_general_thread (struct ptid ptid)
1815 set_thread (ptid, 1);
1819 set_continue_thread (struct ptid ptid)
1821 set_thread (ptid, 0);
1824 /* Change the remote current process. Which thread within the process
1825 ends up selected isn't important, as long as it is the same process
1826 as what INFERIOR_PTID points to.
1828 This comes from that fact that there is no explicit notion of
1829 "selected process" in the protocol. The selected process for
1830 general operations is the process the selected general thread
1834 set_general_process (void)
1836 struct remote_state *rs = get_remote_state ();
1838 /* If the remote can't handle multiple processes, don't bother. */
1839 if (!rs->extended || !remote_multi_process_p (rs))
1842 /* We only need to change the remote current thread if it's pointing
1843 at some other process. */
1844 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
1845 set_general_thread (inferior_ptid);
1849 /* Return nonzero if the thread PTID is still alive on the remote
1853 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1855 struct remote_state *rs = get_remote_state ();
1858 if (ptid_equal (ptid, magic_null_ptid))
1859 /* The main thread is always alive. */
1862 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
1863 /* The main thread is always alive. This can happen after a
1864 vAttach, if the remote side doesn't support
1869 endp = rs->buf + get_remote_packet_size ();
1872 write_ptid (p, endp, ptid);
1875 getpkt (&rs->buf, &rs->buf_size, 0);
1876 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
1879 /* About these extended threadlist and threadinfo packets. They are
1880 variable length packets but, the fields within them are often fixed
1881 length. They are redundent enough to send over UDP as is the
1882 remote protocol in general. There is a matching unit test module
1885 /* WARNING: This threadref data structure comes from the remote O.S.,
1886 libstub protocol encoding, and remote.c. It is not particularly
1889 /* Right now, the internal structure is int. We want it to be bigger.
1890 Plan to fix this. */
1892 typedef int gdb_threadref; /* Internal GDB thread reference. */
1894 /* gdb_ext_thread_info is an internal GDB data structure which is
1895 equivalent to the reply of the remote threadinfo packet. */
1897 struct gdb_ext_thread_info
1899 threadref threadid; /* External form of thread reference. */
1900 int active; /* Has state interesting to GDB?
1902 char display[256]; /* Brief state display, name,
1903 blocked/suspended. */
1904 char shortname[32]; /* To be used to name threads. */
1905 char more_display[256]; /* Long info, statistics, queue depth,
1909 /* The volume of remote transfers can be limited by submitting
1910 a mask containing bits specifying the desired information.
1911 Use a union of these values as the 'selection' parameter to
1912 get_thread_info. FIXME: Make these TAG names more thread specific. */
1914 #define TAG_THREADID 1
1915 #define TAG_EXISTS 2
1916 #define TAG_DISPLAY 4
1917 #define TAG_THREADNAME 8
1918 #define TAG_MOREDISPLAY 16
1920 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1922 static char *unpack_nibble (char *buf, int *val);
1924 static char *unpack_byte (char *buf, int *value);
1926 static char *pack_int (char *buf, int value);
1928 static char *unpack_int (char *buf, int *value);
1930 static char *unpack_string (char *src, char *dest, int length);
1932 static char *pack_threadid (char *pkt, threadref *id);
1934 static char *unpack_threadid (char *inbuf, threadref *id);
1936 void int_to_threadref (threadref *id, int value);
1938 static int threadref_to_int (threadref *ref);
1940 static void copy_threadref (threadref *dest, threadref *src);
1942 static int threadmatch (threadref *dest, threadref *src);
1944 static char *pack_threadinfo_request (char *pkt, int mode,
1947 static int remote_unpack_thread_info_response (char *pkt,
1948 threadref *expectedref,
1949 struct gdb_ext_thread_info
1953 static int remote_get_threadinfo (threadref *threadid,
1954 int fieldset, /*TAG mask */
1955 struct gdb_ext_thread_info *info);
1957 static char *pack_threadlist_request (char *pkt, int startflag,
1959 threadref *nextthread);
1961 static int parse_threadlist_response (char *pkt,
1963 threadref *original_echo,
1964 threadref *resultlist,
1967 static int remote_get_threadlist (int startflag,
1968 threadref *nextthread,
1972 threadref *threadlist);
1974 typedef int (*rmt_thread_action) (threadref *ref, void *context);
1976 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1977 void *context, int looplimit);
1979 static int remote_newthread_step (threadref *ref, void *context);
1982 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1983 buffer we're allowed to write to. Returns
1984 BUF+CHARACTERS_WRITTEN. */
1987 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
1990 struct remote_state *rs = get_remote_state ();
1992 if (remote_multi_process_p (rs))
1994 pid = ptid_get_pid (ptid);
1996 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
1998 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2000 tid = ptid_get_lwp (ptid);
2002 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2004 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2009 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2010 passed the last parsed char. Returns null_ptid on error. */
2013 read_ptid (char *buf, char **obuf)
2017 ULONGEST pid = 0, tid = 0;
2021 /* Multi-process ptid. */
2022 pp = unpack_varlen_hex (p + 1, &pid);
2024 error (_("invalid remote ptid: %s"), p);
2027 pp = unpack_varlen_hex (p + 1, &tid);
2030 return ptid_build (pid, tid, 0);
2033 /* No multi-process. Just a tid. */
2034 pp = unpack_varlen_hex (p, &tid);
2036 /* Since the stub is not sending a process id, then default to
2037 what's in inferior_ptid, unless it's null at this point. If so,
2038 then since there's no way to know the pid of the reported
2039 threads, use the magic number. */
2040 if (ptid_equal (inferior_ptid, null_ptid))
2041 pid = ptid_get_pid (magic_null_ptid);
2043 pid = ptid_get_pid (inferior_ptid);
2047 return ptid_build (pid, tid, 0);
2053 if (ch >= 'a' && ch <= 'f')
2054 return ch - 'a' + 10;
2055 if (ch >= '0' && ch <= '9')
2057 if (ch >= 'A' && ch <= 'F')
2058 return ch - 'A' + 10;
2063 stub_unpack_int (char *buff, int fieldlength)
2070 nibble = stubhex (*buff++);
2074 retval = retval << 4;
2080 unpack_nibble (char *buf, int *val)
2082 *val = fromhex (*buf++);
2087 unpack_byte (char *buf, int *value)
2089 *value = stub_unpack_int (buf, 2);
2094 pack_int (char *buf, int value)
2096 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2097 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2098 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2099 buf = pack_hex_byte (buf, (value & 0xff));
2104 unpack_int (char *buf, int *value)
2106 *value = stub_unpack_int (buf, 8);
2110 #if 0 /* Currently unused, uncomment when needed. */
2111 static char *pack_string (char *pkt, char *string);
2114 pack_string (char *pkt, char *string)
2119 len = strlen (string);
2121 len = 200; /* Bigger than most GDB packets, junk??? */
2122 pkt = pack_hex_byte (pkt, len);
2126 if ((ch == '\0') || (ch == '#'))
2127 ch = '*'; /* Protect encapsulation. */
2132 #endif /* 0 (unused) */
2135 unpack_string (char *src, char *dest, int length)
2144 pack_threadid (char *pkt, threadref *id)
2147 unsigned char *altid;
2149 altid = (unsigned char *) id;
2150 limit = pkt + BUF_THREAD_ID_SIZE;
2152 pkt = pack_hex_byte (pkt, *altid++);
2158 unpack_threadid (char *inbuf, threadref *id)
2161 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2164 altref = (char *) id;
2166 while (inbuf < limit)
2168 x = stubhex (*inbuf++);
2169 y = stubhex (*inbuf++);
2170 *altref++ = (x << 4) | y;
2175 /* Externally, threadrefs are 64 bits but internally, they are still
2176 ints. This is due to a mismatch of specifications. We would like
2177 to use 64bit thread references internally. This is an adapter
2181 int_to_threadref (threadref *id, int value)
2183 unsigned char *scan;
2185 scan = (unsigned char *) id;
2191 *scan++ = (value >> 24) & 0xff;
2192 *scan++ = (value >> 16) & 0xff;
2193 *scan++ = (value >> 8) & 0xff;
2194 *scan++ = (value & 0xff);
2198 threadref_to_int (threadref *ref)
2201 unsigned char *scan;
2207 value = (value << 8) | ((*scan++) & 0xff);
2212 copy_threadref (threadref *dest, threadref *src)
2215 unsigned char *csrc, *cdest;
2217 csrc = (unsigned char *) src;
2218 cdest = (unsigned char *) dest;
2225 threadmatch (threadref *dest, threadref *src)
2227 /* Things are broken right now, so just assume we got a match. */
2229 unsigned char *srcp, *destp;
2231 srcp = (char *) src;
2232 destp = (char *) dest;
2236 result &= (*srcp++ == *destp++) ? 1 : 0;
2243 threadid:1, # always request threadid
2250 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2253 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2255 *pkt++ = 'q'; /* Info Query */
2256 *pkt++ = 'P'; /* process or thread info */
2257 pkt = pack_int (pkt, mode); /* mode */
2258 pkt = pack_threadid (pkt, id); /* threadid */
2259 *pkt = '\0'; /* terminate */
2263 /* These values tag the fields in a thread info response packet. */
2264 /* Tagging the fields allows us to request specific fields and to
2265 add more fields as time goes by. */
2267 #define TAG_THREADID 1 /* Echo the thread identifier. */
2268 #define TAG_EXISTS 2 /* Is this process defined enough to
2269 fetch registers and its stack? */
2270 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2271 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2272 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2276 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2277 struct gdb_ext_thread_info *info)
2279 struct remote_state *rs = get_remote_state ();
2283 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2286 /* info->threadid = 0; FIXME: implement zero_threadref. */
2288 info->display[0] = '\0';
2289 info->shortname[0] = '\0';
2290 info->more_display[0] = '\0';
2292 /* Assume the characters indicating the packet type have been
2294 pkt = unpack_int (pkt, &mask); /* arg mask */
2295 pkt = unpack_threadid (pkt, &ref);
2298 warning (_("Incomplete response to threadinfo request."));
2299 if (!threadmatch (&ref, expectedref))
2300 { /* This is an answer to a different request. */
2301 warning (_("ERROR RMT Thread info mismatch."));
2304 copy_threadref (&info->threadid, &ref);
2306 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2308 /* Packets are terminated with nulls. */
2309 while ((pkt < limit) && mask && *pkt)
2311 pkt = unpack_int (pkt, &tag); /* tag */
2312 pkt = unpack_byte (pkt, &length); /* length */
2313 if (!(tag & mask)) /* Tags out of synch with mask. */
2315 warning (_("ERROR RMT: threadinfo tag mismatch."));
2319 if (tag == TAG_THREADID)
2323 warning (_("ERROR RMT: length of threadid is not 16."));
2327 pkt = unpack_threadid (pkt, &ref);
2328 mask = mask & ~TAG_THREADID;
2331 if (tag == TAG_EXISTS)
2333 info->active = stub_unpack_int (pkt, length);
2335 mask = mask & ~(TAG_EXISTS);
2338 warning (_("ERROR RMT: 'exists' length too long."));
2344 if (tag == TAG_THREADNAME)
2346 pkt = unpack_string (pkt, &info->shortname[0], length);
2347 mask = mask & ~TAG_THREADNAME;
2350 if (tag == TAG_DISPLAY)
2352 pkt = unpack_string (pkt, &info->display[0], length);
2353 mask = mask & ~TAG_DISPLAY;
2356 if (tag == TAG_MOREDISPLAY)
2358 pkt = unpack_string (pkt, &info->more_display[0], length);
2359 mask = mask & ~TAG_MOREDISPLAY;
2362 warning (_("ERROR RMT: unknown thread info tag."));
2363 break; /* Not a tag we know about. */
2369 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2370 struct gdb_ext_thread_info *info)
2372 struct remote_state *rs = get_remote_state ();
2375 pack_threadinfo_request (rs->buf, fieldset, threadid);
2377 getpkt (&rs->buf, &rs->buf_size, 0);
2379 if (rs->buf[0] == '\0')
2382 result = remote_unpack_thread_info_response (rs->buf + 2,
2387 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2390 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2391 threadref *nextthread)
2393 *pkt++ = 'q'; /* info query packet */
2394 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2395 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2396 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2397 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2402 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2405 parse_threadlist_response (char *pkt, int result_limit,
2406 threadref *original_echo, threadref *resultlist,
2409 struct remote_state *rs = get_remote_state ();
2411 int count, resultcount, done;
2414 /* Assume the 'q' and 'M chars have been stripped. */
2415 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2416 /* done parse past here */
2417 pkt = unpack_byte (pkt, &count); /* count field */
2418 pkt = unpack_nibble (pkt, &done);
2419 /* The first threadid is the argument threadid. */
2420 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2421 while ((count-- > 0) && (pkt < limit))
2423 pkt = unpack_threadid (pkt, resultlist++);
2424 if (resultcount++ >= result_limit)
2433 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2434 int *done, int *result_count, threadref *threadlist)
2436 struct remote_state *rs = get_remote_state ();
2439 /* Trancate result limit to be smaller than the packet size. */
2440 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2441 >= get_remote_packet_size ())
2442 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2444 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2446 getpkt (&rs->buf, &rs->buf_size, 0);
2448 if (*rs->buf == '\0')
2452 parse_threadlist_response (rs->buf + 2, result_limit,
2453 &rs->echo_nextthread, threadlist, done);
2455 if (!threadmatch (&rs->echo_nextthread, nextthread))
2457 /* FIXME: This is a good reason to drop the packet. */
2458 /* Possably, there is a duplicate response. */
2460 retransmit immediatly - race conditions
2461 retransmit after timeout - yes
2463 wait for packet, then exit
2465 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2466 return 0; /* I choose simply exiting. */
2468 if (*result_count <= 0)
2472 warning (_("RMT ERROR : failed to get remote thread list."));
2475 return result; /* break; */
2477 if (*result_count > result_limit)
2480 warning (_("RMT ERROR: threadlist response longer than requested."));
2486 /* This is the interface between remote and threads, remotes upper
2489 /* remote_find_new_threads retrieves the thread list and for each
2490 thread in the list, looks up the thread in GDB's internal list,
2491 adding the thread if it does not already exist. This involves
2492 getting partial thread lists from the remote target so, polling the
2493 quit_flag is required. */
2497 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2500 struct remote_state *rs = get_remote_state ();
2501 int done, i, result_count;
2509 if (loopcount++ > looplimit)
2512 warning (_("Remote fetch threadlist -infinite loop-."));
2515 if (!remote_get_threadlist (startflag, &rs->nextthread,
2516 MAXTHREADLISTRESULTS,
2517 &done, &result_count, rs->resultthreadlist))
2522 /* Clear for later iterations. */
2524 /* Setup to resume next batch of thread references, set nextthread. */
2525 if (result_count >= 1)
2526 copy_threadref (&rs->nextthread,
2527 &rs->resultthreadlist[result_count - 1]);
2529 while (result_count--)
2530 if (!(result = (*stepfunction) (&rs->resultthreadlist[i++], context)))
2537 remote_newthread_step (threadref *ref, void *context)
2539 int pid = ptid_get_pid (inferior_ptid);
2540 ptid_t ptid = ptid_build (pid, threadref_to_int (ref), 0);
2542 if (!in_thread_list (ptid))
2544 return 1; /* continue iterator */
2547 #define CRAZY_MAX_THREADS 1000
2550 remote_current_thread (ptid_t oldpid)
2552 struct remote_state *rs = get_remote_state ();
2555 getpkt (&rs->buf, &rs->buf_size, 0);
2556 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2557 return read_ptid (&rs->buf[2], NULL);
2562 /* Find new threads for info threads command.
2563 * Original version, using John Metzler's thread protocol.
2567 remote_find_new_threads (void)
2569 remote_threadlist_iterator (remote_newthread_step, 0,
2573 #if defined(HAVE_LIBEXPAT)
2575 typedef struct thread_item
2581 DEF_VEC_O(thread_item_t);
2583 struct threads_parsing_context
2585 VEC (thread_item_t) *items;
2589 start_thread (struct gdb_xml_parser *parser,
2590 const struct gdb_xml_element *element,
2591 void *user_data, VEC(gdb_xml_value_s) *attributes)
2593 struct threads_parsing_context *data = user_data;
2595 struct thread_item item;
2597 struct gdb_xml_value *attr;
2599 id = xml_find_attribute (attributes, "id")->value;
2600 item.ptid = read_ptid (id, NULL);
2602 attr = xml_find_attribute (attributes, "core");
2604 item.core = *(ULONGEST *) attr->value;
2610 VEC_safe_push (thread_item_t, data->items, &item);
2614 end_thread (struct gdb_xml_parser *parser,
2615 const struct gdb_xml_element *element,
2616 void *user_data, const char *body_text)
2618 struct threads_parsing_context *data = user_data;
2620 if (body_text && *body_text)
2621 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2624 const struct gdb_xml_attribute thread_attributes[] = {
2625 { "id", GDB_XML_AF_NONE, NULL, NULL },
2626 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2627 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2630 const struct gdb_xml_element thread_children[] = {
2631 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2634 const struct gdb_xml_element threads_children[] = {
2635 { "thread", thread_attributes, thread_children,
2636 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2637 start_thread, end_thread },
2638 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2641 const struct gdb_xml_element threads_elements[] = {
2642 { "threads", NULL, threads_children,
2643 GDB_XML_EF_NONE, NULL, NULL },
2644 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2647 /* Discard the contents of the constructed thread info context. */
2650 clear_threads_parsing_context (void *p)
2652 struct threads_parsing_context *context = p;
2654 struct thread_item *item;
2656 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2657 xfree (item->extra);
2659 VEC_free (thread_item_t, context->items);
2665 * Find all threads for info threads command.
2666 * Uses new thread protocol contributed by Cisco.
2667 * Falls back and attempts to use the older method (above)
2668 * if the target doesn't respond to the new method.
2672 remote_threads_info (struct target_ops *ops)
2674 struct remote_state *rs = get_remote_state ();
2678 if (rs->remote_desc == 0) /* paranoia */
2679 error (_("Command can only be used when connected to the remote target."));
2681 #if defined(HAVE_LIBEXPAT)
2682 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2684 char *xml = target_read_stralloc (¤t_target,
2685 TARGET_OBJECT_THREADS, NULL);
2687 struct cleanup *back_to = make_cleanup (xfree, xml);
2691 struct threads_parsing_context context;
2693 context.items = NULL;
2694 make_cleanup (clear_threads_parsing_context, &context);
2696 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2697 threads_elements, xml, &context) == 0)
2700 struct thread_item *item;
2703 VEC_iterate (thread_item_t, context.items, i, item);
2706 if (!ptid_equal (item->ptid, null_ptid))
2708 struct private_thread_info *info;
2709 /* In non-stop mode, we assume new found threads
2710 are running until proven otherwise with a
2711 stop reply. In all-stop, we can only get
2712 here if all threads are stopped. */
2713 int running = non_stop ? 1 : 0;
2715 remote_notice_new_inferior (item->ptid, running);
2717 info = demand_private_info (item->ptid);
2718 info->core = item->core;
2719 info->extra = item->extra;
2726 do_cleanups (back_to);
2731 if (rs->use_threadinfo_query)
2733 putpkt ("qfThreadInfo");
2734 getpkt (&rs->buf, &rs->buf_size, 0);
2736 if (bufp[0] != '\0') /* q packet recognized */
2738 struct cleanup *old_chain;
2741 /* remote_notice_new_inferior (in the loop below) may make
2742 new RSP calls, which clobber rs->buf. Work with a
2744 bufp = saved_reply = xstrdup (rs->buf);
2745 old_chain = make_cleanup (free_current_contents, &saved_reply);
2747 while (*bufp++ == 'm') /* reply contains one or more TID */
2751 new_thread = read_ptid (bufp, &bufp);
2752 if (!ptid_equal (new_thread, null_ptid))
2754 /* In non-stop mode, we assume new found threads
2755 are running until proven otherwise with a
2756 stop reply. In all-stop, we can only get
2757 here if all threads are stopped. */
2758 int running = non_stop ? 1 : 0;
2760 remote_notice_new_inferior (new_thread, running);
2763 while (*bufp++ == ','); /* comma-separated list */
2764 free_current_contents (&saved_reply);
2765 putpkt ("qsThreadInfo");
2766 getpkt (&rs->buf, &rs->buf_size, 0);
2767 bufp = saved_reply = xstrdup (rs->buf);
2769 do_cleanups (old_chain);
2774 /* Only qfThreadInfo is supported in non-stop mode. */
2778 /* Else fall back to old method based on jmetzler protocol. */
2779 rs->use_threadinfo_query = 0;
2780 remote_find_new_threads ();
2785 * Collect a descriptive string about the given thread.
2786 * The target may say anything it wants to about the thread
2787 * (typically info about its blocked / runnable state, name, etc.).
2788 * This string will appear in the info threads display.
2790 * Optional: targets are not required to implement this function.
2794 remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
2796 struct remote_state *rs = get_remote_state ();
2800 struct gdb_ext_thread_info threadinfo;
2801 static char display_buf[100]; /* arbitrary... */
2802 int n = 0; /* position in display_buf */
2804 if (rs->remote_desc == 0) /* paranoia */
2805 internal_error (__FILE__, __LINE__,
2806 _("remote_threads_extra_info"));
2808 if (ptid_equal (tp->ptid, magic_null_ptid)
2809 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
2810 /* This is the main thread which was added by GDB. The remote
2811 server doesn't know about it. */
2814 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2816 struct thread_info *info = find_thread_ptid (tp->ptid);
2818 if (info && info->private)
2819 return info->private->extra;
2824 if (rs->use_threadextra_query)
2827 char *endb = rs->buf + get_remote_packet_size ();
2829 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2831 write_ptid (b, endb, tp->ptid);
2834 getpkt (&rs->buf, &rs->buf_size, 0);
2835 if (rs->buf[0] != 0)
2837 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2838 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
2839 display_buf [result] = '\0';
2844 /* If the above query fails, fall back to the old method. */
2845 rs->use_threadextra_query = 0;
2846 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2847 | TAG_MOREDISPLAY | TAG_DISPLAY;
2848 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
2849 if (remote_get_threadinfo (&id, set, &threadinfo))
2850 if (threadinfo.active)
2852 if (*threadinfo.shortname)
2853 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
2854 " Name: %s,", threadinfo.shortname);
2855 if (*threadinfo.display)
2856 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2857 " State: %s,", threadinfo.display);
2858 if (*threadinfo.more_display)
2859 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2860 " Priority: %s", threadinfo.more_display);
2864 /* For purely cosmetic reasons, clear up trailing commas. */
2865 if (',' == display_buf[n-1])
2866 display_buf[n-1] = ' ';
2875 remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
2876 struct static_tracepoint_marker *marker)
2878 struct remote_state *rs = get_remote_state ();
2881 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
2883 p += hexnumstr (p, addr);
2885 getpkt (&rs->buf, &rs->buf_size, 0);
2889 error (_("Remote failure reply: %s"), p);
2893 parse_static_tracepoint_marker_definition (p, &p, marker);
2900 static VEC(static_tracepoint_marker_p) *
2901 remote_static_tracepoint_markers_by_strid (struct target_ops *self,
2904 struct remote_state *rs = get_remote_state ();
2905 VEC(static_tracepoint_marker_p) *markers = NULL;
2906 struct static_tracepoint_marker *marker = NULL;
2907 struct cleanup *old_chain;
2910 /* Ask for a first packet of static tracepoint marker
2913 getpkt (&rs->buf, &rs->buf_size, 0);
2916 error (_("Remote failure reply: %s"), p);
2918 old_chain = make_cleanup (free_current_marker, &marker);
2923 marker = XCNEW (struct static_tracepoint_marker);
2927 parse_static_tracepoint_marker_definition (p, &p, marker);
2929 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
2931 VEC_safe_push (static_tracepoint_marker_p,
2937 release_static_tracepoint_marker (marker);
2938 memset (marker, 0, sizeof (*marker));
2941 while (*p++ == ','); /* comma-separated list */
2942 /* Ask for another packet of static tracepoint definition. */
2944 getpkt (&rs->buf, &rs->buf_size, 0);
2948 do_cleanups (old_chain);
2953 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2956 remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
2958 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
2962 /* Restart the remote side; this is an extended protocol operation. */
2965 extended_remote_restart (void)
2967 struct remote_state *rs = get_remote_state ();
2969 /* Send the restart command; for reasons I don't understand the
2970 remote side really expects a number after the "R". */
2971 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
2974 remote_fileio_reset ();
2977 /* Clean up connection to a remote debugger. */
2980 remote_close (struct target_ops *self)
2982 struct remote_state *rs = get_remote_state ();
2984 if (rs->remote_desc == NULL)
2985 return; /* already closed */
2987 /* Make sure we leave stdin registered in the event loop, and we
2988 don't leave the async SIGINT signal handler installed. */
2989 remote_terminal_ours (self);
2991 serial_close (rs->remote_desc);
2992 rs->remote_desc = NULL;
2994 /* We don't have a connection to the remote stub anymore. Get rid
2995 of all the inferiors and their threads we were controlling.
2996 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
2997 will be unable to find the thread corresponding to (pid, 0, 0). */
2998 inferior_ptid = null_ptid;
2999 discard_all_inferiors ();
3001 /* We are closing the remote target, so we should discard
3002 everything of this target. */
3003 discard_pending_stop_replies_in_queue (rs);
3005 if (remote_async_inferior_event_token)
3006 delete_async_event_handler (&remote_async_inferior_event_token);
3008 remote_notif_state_xfree (rs->notif_state);
3010 trace_reset_local_state ();
3013 /* Query the remote side for the text, data and bss offsets. */
3018 struct remote_state *rs = get_remote_state ();
3021 int lose, num_segments = 0, do_sections, do_segments;
3022 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
3023 struct section_offsets *offs;
3024 struct symfile_segment_data *data;
3026 if (symfile_objfile == NULL)
3029 putpkt ("qOffsets");
3030 getpkt (&rs->buf, &rs->buf_size, 0);
3033 if (buf[0] == '\000')
3034 return; /* Return silently. Stub doesn't support
3038 warning (_("Remote failure reply: %s"), buf);
3042 /* Pick up each field in turn. This used to be done with scanf, but
3043 scanf will make trouble if CORE_ADDR size doesn't match
3044 conversion directives correctly. The following code will work
3045 with any size of CORE_ADDR. */
3046 text_addr = data_addr = bss_addr = 0;
3050 if (strncmp (ptr, "Text=", 5) == 0)
3053 /* Don't use strtol, could lose on big values. */
3054 while (*ptr && *ptr != ';')
3055 text_addr = (text_addr << 4) + fromhex (*ptr++);
3057 if (strncmp (ptr, ";Data=", 6) == 0)
3060 while (*ptr && *ptr != ';')
3061 data_addr = (data_addr << 4) + fromhex (*ptr++);
3066 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
3069 while (*ptr && *ptr != ';')
3070 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
3072 if (bss_addr != data_addr)
3073 warning (_("Target reported unsupported offsets: %s"), buf);
3078 else if (strncmp (ptr, "TextSeg=", 8) == 0)
3081 /* Don't use strtol, could lose on big values. */
3082 while (*ptr && *ptr != ';')
3083 text_addr = (text_addr << 4) + fromhex (*ptr++);
3086 if (strncmp (ptr, ";DataSeg=", 9) == 0)
3089 while (*ptr && *ptr != ';')
3090 data_addr = (data_addr << 4) + fromhex (*ptr++);
3098 error (_("Malformed response to offset query, %s"), buf);
3099 else if (*ptr != '\0')
3100 warning (_("Target reported unsupported offsets: %s"), buf);
3102 offs = ((struct section_offsets *)
3103 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3104 memcpy (offs, symfile_objfile->section_offsets,
3105 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3107 data = get_symfile_segment_data (symfile_objfile->obfd);
3108 do_segments = (data != NULL);
3109 do_sections = num_segments == 0;
3111 if (num_segments > 0)
3113 segments[0] = text_addr;
3114 segments[1] = data_addr;
3116 /* If we have two segments, we can still try to relocate everything
3117 by assuming that the .text and .data offsets apply to the whole
3118 text and data segments. Convert the offsets given in the packet
3119 to base addresses for symfile_map_offsets_to_segments. */
3120 else if (data && data->num_segments == 2)
3122 segments[0] = data->segment_bases[0] + text_addr;
3123 segments[1] = data->segment_bases[1] + data_addr;
3126 /* If the object file has only one segment, assume that it is text
3127 rather than data; main programs with no writable data are rare,
3128 but programs with no code are useless. Of course the code might
3129 have ended up in the data segment... to detect that we would need
3130 the permissions here. */
3131 else if (data && data->num_segments == 1)
3133 segments[0] = data->segment_bases[0] + text_addr;
3136 /* There's no way to relocate by segment. */
3142 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3143 offs, num_segments, segments);
3145 if (ret == 0 && !do_sections)
3146 error (_("Can not handle qOffsets TextSeg "
3147 "response with this symbol file"));
3154 free_symfile_segment_data (data);
3158 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3160 /* This is a temporary kludge to force data and bss to use the
3161 same offsets because that's what nlmconv does now. The real
3162 solution requires changes to the stub and remote.c that I
3163 don't have time to do right now. */
3165 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3166 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3169 objfile_relocate (symfile_objfile, offs);
3172 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3173 threads we know are stopped already. This is used during the
3174 initial remote connection in non-stop mode --- threads that are
3175 reported as already being stopped are left stopped. */
3178 set_stop_requested_callback (struct thread_info *thread, void *data)
3180 /* If we have a stop reply for this thread, it must be stopped. */
3181 if (peek_stop_reply (thread->ptid))
3182 set_stop_requested (thread->ptid, 1);
3187 /* Send interrupt_sequence to remote target. */
3189 send_interrupt_sequence (void)
3191 struct remote_state *rs = get_remote_state ();
3193 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3194 remote_serial_write ("\x03", 1);
3195 else if (interrupt_sequence_mode == interrupt_sequence_break)
3196 serial_send_break (rs->remote_desc);
3197 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3199 serial_send_break (rs->remote_desc);
3200 remote_serial_write ("g", 1);
3203 internal_error (__FILE__, __LINE__,
3204 _("Invalid value for interrupt_sequence_mode: %s."),
3205 interrupt_sequence_mode);
3209 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3210 and extract the PTID. Returns NULL_PTID if not found. */
3213 stop_reply_extract_thread (char *stop_reply)
3215 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3219 /* Txx r:val ; r:val (...) */
3222 /* Look for "register" named "thread". */
3227 p1 = strchr (p, ':');
3231 if (strncmp (p, "thread", p1 - p) == 0)
3232 return read_ptid (++p1, &p);
3234 p1 = strchr (p, ';');
3246 /* Determine the remote side's current thread. If we have a stop
3247 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3248 "thread" register we can extract the current thread from. If not,
3249 ask the remote which is the current thread with qC. The former
3250 method avoids a roundtrip. */
3253 get_current_thread (char *wait_status)
3257 /* Note we don't use remote_parse_stop_reply as that makes use of
3258 the target architecture, which we haven't yet fully determined at
3260 if (wait_status != NULL)
3261 ptid = stop_reply_extract_thread (wait_status);
3262 if (ptid_equal (ptid, null_ptid))
3263 ptid = remote_current_thread (inferior_ptid);
3268 /* Query the remote target for which is the current thread/process,
3269 add it to our tables, and update INFERIOR_PTID. The caller is
3270 responsible for setting the state such that the remote end is ready
3271 to return the current thread.
3273 This function is called after handling the '?' or 'vRun' packets,
3274 whose response is a stop reply from which we can also try
3275 extracting the thread. If the target doesn't support the explicit
3276 qC query, we infer the current thread from that stop reply, passed
3277 in in WAIT_STATUS, which may be NULL. */
3280 add_current_inferior_and_thread (char *wait_status)
3282 struct remote_state *rs = get_remote_state ();
3284 ptid_t ptid = null_ptid;
3286 inferior_ptid = null_ptid;
3288 /* Now, if we have thread information, update inferior_ptid. */
3289 ptid = get_current_thread (wait_status);
3291 if (!ptid_equal (ptid, null_ptid))
3293 if (!remote_multi_process_p (rs))
3296 inferior_ptid = ptid;
3300 /* Without this, some commands which require an active target
3301 (such as kill) won't work. This variable serves (at least)
3302 double duty as both the pid of the target process (if it has
3303 such), and as a flag indicating that a target is active. */
3304 inferior_ptid = magic_null_ptid;
3308 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1);
3310 /* Add the main thread. */
3311 add_thread_silent (inferior_ptid);
3315 remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
3317 struct remote_state *rs = get_remote_state ();
3318 struct packet_config *noack_config;
3319 char *wait_status = NULL;
3321 immediate_quit++; /* Allow user to interrupt it. */
3324 if (interrupt_on_connect)
3325 send_interrupt_sequence ();
3327 /* Ack any packet which the remote side has already sent. */
3328 serial_write (rs->remote_desc, "+", 1);
3330 /* Signal other parts that we're going through the initial setup,
3331 and so things may not be stable yet. */
3332 rs->starting_up = 1;
3334 /* The first packet we send to the target is the optional "supported
3335 packets" request. If the target can answer this, it will tell us
3336 which later probes to skip. */
3337 remote_query_supported ();
3339 /* If the stub wants to get a QAllow, compose one and send it. */
3340 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
3341 remote_set_permissions (target);
3343 /* Next, we possibly activate noack mode.
3345 If the QStartNoAckMode packet configuration is set to AUTO,
3346 enable noack mode if the stub reported a wish for it with
3349 If set to TRUE, then enable noack mode even if the stub didn't
3350 report it in qSupported. If the stub doesn't reply OK, the
3351 session ends with an error.
3353 If FALSE, then don't activate noack mode, regardless of what the
3354 stub claimed should be the default with qSupported. */
3356 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3357 if (packet_config_support (noack_config) != PACKET_DISABLE)
3359 putpkt ("QStartNoAckMode");
3360 getpkt (&rs->buf, &rs->buf_size, 0);
3361 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3367 /* Tell the remote that we are using the extended protocol. */
3369 getpkt (&rs->buf, &rs->buf_size, 0);
3372 /* Let the target know which signals it is allowed to pass down to
3374 update_signals_program_target ();
3376 /* Next, if the target can specify a description, read it. We do
3377 this before anything involving memory or registers. */
3378 target_find_description ();
3380 /* Next, now that we know something about the target, update the
3381 address spaces in the program spaces. */
3382 update_address_spaces ();
3384 /* On OSs where the list of libraries is global to all
3385 processes, we fetch them early. */
3386 if (gdbarch_has_global_solist (target_gdbarch ()))
3387 solib_add (NULL, from_tty, target, auto_solib_add);
3391 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3392 error (_("Non-stop mode requested, but remote "
3393 "does not support non-stop"));
3395 putpkt ("QNonStop:1");
3396 getpkt (&rs->buf, &rs->buf_size, 0);
3398 if (strcmp (rs->buf, "OK") != 0)
3399 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3401 /* Find about threads and processes the stub is already
3402 controlling. We default to adding them in the running state.
3403 The '?' query below will then tell us about which threads are
3405 remote_threads_info (target);
3407 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
3409 /* Don't assume that the stub can operate in all-stop mode.
3410 Request it explicitly. */
3411 putpkt ("QNonStop:0");
3412 getpkt (&rs->buf, &rs->buf_size, 0);
3414 if (strcmp (rs->buf, "OK") != 0)
3415 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3418 /* Upload TSVs regardless of whether the target is running or not. The
3419 remote stub, such as GDBserver, may have some predefined or builtin
3420 TSVs, even if the target is not running. */
3421 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3423 struct uploaded_tsv *uploaded_tsvs = NULL;
3425 remote_upload_trace_state_variables (target, &uploaded_tsvs);
3426 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3429 /* Check whether the target is running now. */
3431 getpkt (&rs->buf, &rs->buf_size, 0);
3437 struct inferior *inf;
3439 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3442 error (_("The target is not running (try extended-remote?)"));
3444 /* We're connected, but not running. Drop out before we
3445 call start_remote. */
3446 rs->starting_up = 0;
3451 /* Save the reply for later. */
3452 wait_status = alloca (strlen (rs->buf) + 1);
3453 strcpy (wait_status, rs->buf);
3456 /* Fetch thread list. */
3457 target_find_new_threads ();
3459 /* Let the stub know that we want it to return the thread. */
3460 set_continue_thread (minus_one_ptid);
3462 if (thread_count () == 0)
3464 /* Target has no concept of threads at all. GDB treats
3465 non-threaded target as single-threaded; add a main
3467 add_current_inferior_and_thread (wait_status);
3471 /* We have thread information; select the thread the target
3472 says should be current. If we're reconnecting to a
3473 multi-threaded program, this will ideally be the thread
3474 that last reported an event before GDB disconnected. */
3475 inferior_ptid = get_current_thread (wait_status);
3476 if (ptid_equal (inferior_ptid, null_ptid))
3478 /* Odd... The target was able to list threads, but not
3479 tell us which thread was current (no "thread"
3480 register in T stop reply?). Just pick the first
3481 thread in the thread list then. */
3482 inferior_ptid = thread_list->ptid;
3486 /* init_wait_for_inferior should be called before get_offsets in order
3487 to manage `inserted' flag in bp loc in a correct state.
3488 breakpoint_init_inferior, called from init_wait_for_inferior, set
3489 `inserted' flag to 0, while before breakpoint_re_set, called from
3490 start_remote, set `inserted' flag to 1. In the initialization of
3491 inferior, breakpoint_init_inferior should be called first, and then
3492 breakpoint_re_set can be called. If this order is broken, state of
3493 `inserted' flag is wrong, and cause some problems on breakpoint
3495 init_wait_for_inferior ();
3497 get_offsets (); /* Get text, data & bss offsets. */
3499 /* If we could not find a description using qXfer, and we know
3500 how to do it some other way, try again. This is not
3501 supported for non-stop; it could be, but it is tricky if
3502 there are no stopped threads when we connect. */
3503 if (remote_read_description_p (target)
3504 && gdbarch_target_desc (target_gdbarch ()) == NULL)
3506 target_clear_description ();
3507 target_find_description ();
3510 /* Use the previously fetched status. */
3511 gdb_assert (wait_status != NULL);
3512 strcpy (rs->buf, wait_status);
3513 rs->cached_wait_status = 1;
3516 start_remote (from_tty); /* Initialize gdb process mechanisms. */
3520 /* Clear WFI global state. Do this before finding about new
3521 threads and inferiors, and setting the current inferior.
3522 Otherwise we would clear the proceed status of the current
3523 inferior when we want its stop_soon state to be preserved
3524 (see notice_new_inferior). */
3525 init_wait_for_inferior ();
3527 /* In non-stop, we will either get an "OK", meaning that there
3528 are no stopped threads at this time; or, a regular stop
3529 reply. In the latter case, there may be more than one thread
3530 stopped --- we pull them all out using the vStopped
3532 if (strcmp (rs->buf, "OK") != 0)
3534 struct notif_client *notif = ¬if_client_stop;
3536 /* remote_notif_get_pending_replies acks this one, and gets
3538 rs->notif_state->pending_event[notif_client_stop.id]
3539 = remote_notif_parse (notif, rs->buf);
3540 remote_notif_get_pending_events (notif);
3542 /* Make sure that threads that were stopped remain
3544 iterate_over_threads (set_stop_requested_callback, NULL);
3547 if (target_can_async_p ())
3548 target_async (inferior_event_handler, 0);
3550 if (thread_count () == 0)
3553 error (_("The target is not running (try extended-remote?)"));
3555 /* We're connected, but not running. Drop out before we
3556 call start_remote. */
3557 rs->starting_up = 0;
3561 /* Let the stub know that we want it to return the thread. */
3563 /* Force the stub to choose a thread. */
3564 set_general_thread (null_ptid);
3567 inferior_ptid = remote_current_thread (minus_one_ptid);
3568 if (ptid_equal (inferior_ptid, minus_one_ptid))
3569 error (_("remote didn't report the current thread in non-stop mode"));
3571 get_offsets (); /* Get text, data & bss offsets. */
3573 /* In non-stop mode, any cached wait status will be stored in
3574 the stop reply queue. */
3575 gdb_assert (wait_status == NULL);
3577 /* Report all signals during attach/startup. */
3578 remote_pass_signals (target, 0, NULL);
3581 /* If we connected to a live target, do some additional setup. */
3582 if (target_has_execution)
3584 if (symfile_objfile) /* No use without a symbol-file. */
3585 remote_check_symbols ();
3588 /* Possibly the target has been engaged in a trace run started
3589 previously; find out where things are at. */
3590 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3592 struct uploaded_tp *uploaded_tps = NULL;
3594 if (current_trace_status ()->running)
3595 printf_filtered (_("Trace is already running on the target.\n"));
3597 remote_upload_tracepoints (target, &uploaded_tps);
3599 merge_uploaded_tracepoints (&uploaded_tps);
3602 /* The thread and inferior lists are now synchronized with the
3603 target, our symbols have been relocated, and we're merged the
3604 target's tracepoints with ours. We're done with basic start
3606 rs->starting_up = 0;
3608 /* If breakpoints are global, insert them now. */
3609 if (gdbarch_has_global_breakpoints (target_gdbarch ())
3610 && breakpoints_always_inserted_mode ())
3611 insert_breakpoints ();
3614 /* Open a connection to a remote debugger.
3615 NAME is the filename used for communication. */
3618 remote_open (const char *name, int from_tty)
3620 remote_open_1 (name, from_tty, &remote_ops, 0);
3623 /* Open a connection to a remote debugger using the extended
3624 remote gdb protocol. NAME is the filename used for communication. */
3627 extended_remote_open (const char *name, int from_tty)
3629 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3632 /* Reset all packets back to "unknown support". Called when opening a
3633 new connection to a remote target. */
3636 reset_all_packet_configs_support (void)
3640 for (i = 0; i < PACKET_MAX; i++)
3641 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3644 /* Initialize all packet configs. */
3647 init_all_packet_configs (void)
3651 for (i = 0; i < PACKET_MAX; i++)
3653 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3654 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3658 /* Symbol look-up. */
3661 remote_check_symbols (void)
3663 struct remote_state *rs = get_remote_state ();
3664 char *msg, *reply, *tmp;
3665 struct bound_minimal_symbol sym;
3668 /* The remote side has no concept of inferiors that aren't running
3669 yet, it only knows about running processes. If we're connected
3670 but our current inferior is not running, we should not invite the
3671 remote target to request symbol lookups related to its
3672 (unrelated) current process. */
3673 if (!target_has_execution)
3676 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
3679 /* Make sure the remote is pointing at the right process. Note
3680 there's no way to select "no process". */
3681 set_general_process ();
3683 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3684 because we need both at the same time. */
3685 msg = alloca (get_remote_packet_size ());
3687 /* Invite target to request symbol lookups. */
3689 putpkt ("qSymbol::");
3690 getpkt (&rs->buf, &rs->buf_size, 0);
3691 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3694 while (strncmp (reply, "qSymbol:", 8) == 0)
3696 struct bound_minimal_symbol sym;
3699 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3701 sym = lookup_minimal_symbol (msg, NULL, NULL);
3702 if (sym.minsym == NULL)
3703 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3706 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
3707 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
3709 /* If this is a function address, return the start of code
3710 instead of any data function descriptor. */
3711 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3715 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3716 phex_nz (sym_addr, addr_size), &reply[8]);
3720 getpkt (&rs->buf, &rs->buf_size, 0);
3725 static struct serial *
3726 remote_serial_open (const char *name)
3728 static int udp_warning = 0;
3730 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3731 of in ser-tcp.c, because it is the remote protocol assuming that the
3732 serial connection is reliable and not the serial connection promising
3734 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3736 warning (_("The remote protocol may be unreliable over UDP.\n"
3737 "Some events may be lost, rendering further debugging "
3742 return serial_open (name);
3745 /* Inform the target of our permission settings. The permission flags
3746 work without this, but if the target knows the settings, it can do
3747 a couple things. First, it can add its own check, to catch cases
3748 that somehow manage to get by the permissions checks in target
3749 methods. Second, if the target is wired to disallow particular
3750 settings (for instance, a system in the field that is not set up to
3751 be able to stop at a breakpoint), it can object to any unavailable
3755 remote_set_permissions (struct target_ops *self)
3757 struct remote_state *rs = get_remote_state ();
3759 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3760 "WriteReg:%x;WriteMem:%x;"
3761 "InsertBreak:%x;InsertTrace:%x;"
3762 "InsertFastTrace:%x;Stop:%x",
3763 may_write_registers, may_write_memory,
3764 may_insert_breakpoints, may_insert_tracepoints,
3765 may_insert_fast_tracepoints, may_stop);
3767 getpkt (&rs->buf, &rs->buf_size, 0);
3769 /* If the target didn't like the packet, warn the user. Do not try
3770 to undo the user's settings, that would just be maddening. */
3771 if (strcmp (rs->buf, "OK") != 0)
3772 warning (_("Remote refused setting permissions with: %s"), rs->buf);
3775 /* This type describes each known response to the qSupported
3777 struct protocol_feature
3779 /* The name of this protocol feature. */
3782 /* The default for this protocol feature. */
3783 enum packet_support default_support;
3785 /* The function to call when this feature is reported, or after
3786 qSupported processing if the feature is not supported.
3787 The first argument points to this structure. The second
3788 argument indicates whether the packet requested support be
3789 enabled, disabled, or probed (or the default, if this function
3790 is being called at the end of processing and this feature was
3791 not reported). The third argument may be NULL; if not NULL, it
3792 is a NUL-terminated string taken from the packet following
3793 this feature's name and an equals sign. */
3794 void (*func) (const struct protocol_feature *, enum packet_support,
3797 /* The corresponding packet for this feature. Only used if
3798 FUNC is remote_supported_packet. */
3803 remote_supported_packet (const struct protocol_feature *feature,
3804 enum packet_support support,
3805 const char *argument)
3809 warning (_("Remote qSupported response supplied an unexpected value for"
3810 " \"%s\"."), feature->name);
3814 remote_protocol_packets[feature->packet].support = support;
3818 remote_packet_size (const struct protocol_feature *feature,
3819 enum packet_support support, const char *value)
3821 struct remote_state *rs = get_remote_state ();
3826 if (support != PACKET_ENABLE)
3829 if (value == NULL || *value == '\0')
3831 warning (_("Remote target reported \"%s\" without a size."),
3837 packet_size = strtol (value, &value_end, 16);
3838 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3840 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3841 feature->name, value);
3845 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3847 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3848 packet_size, MAX_REMOTE_PACKET_SIZE);
3849 packet_size = MAX_REMOTE_PACKET_SIZE;
3852 /* Record the new maximum packet size. */
3853 rs->explicit_packet_size = packet_size;
3856 static const struct protocol_feature remote_protocol_features[] = {
3857 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
3858 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
3859 PACKET_qXfer_auxv },
3860 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3861 PACKET_qXfer_features },
3862 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3863 PACKET_qXfer_libraries },
3864 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3865 PACKET_qXfer_libraries_svr4 },
3866 { "augmented-libraries-svr4-read", PACKET_DISABLE,
3867 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
3868 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
3869 PACKET_qXfer_memory_map },
3870 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3871 PACKET_qXfer_spu_read },
3872 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3873 PACKET_qXfer_spu_write },
3874 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3875 PACKET_qXfer_osdata },
3876 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3877 PACKET_qXfer_threads },
3878 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3879 PACKET_qXfer_traceframe_info },
3880 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3881 PACKET_QPassSignals },
3882 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3883 PACKET_QProgramSignals },
3884 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3885 PACKET_QStartNoAckMode },
3886 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
3887 PACKET_multiprocess_feature },
3888 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
3889 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3890 PACKET_qXfer_siginfo_read },
3891 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3892 PACKET_qXfer_siginfo_write },
3893 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
3894 PACKET_ConditionalTracepoints },
3895 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3896 PACKET_ConditionalBreakpoints },
3897 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
3898 PACKET_BreakpointCommands },
3899 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
3900 PACKET_FastTracepoints },
3901 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
3902 PACKET_StaticTracepoints },
3903 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
3904 PACKET_InstallInTrace},
3905 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
3906 PACKET_DisconnectedTracing_feature },
3907 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
3909 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
3911 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3912 PACKET_TracepointSource },
3913 { "QAllow", PACKET_DISABLE, remote_supported_packet,
3915 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
3916 PACKET_EnableDisableTracepoints_feature },
3917 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
3918 PACKET_qXfer_fdpic },
3919 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
3921 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
3922 PACKET_QDisableRandomization },
3923 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
3924 { "QTBuffer:size", PACKET_DISABLE,
3925 remote_supported_packet, PACKET_QTBuffer_size},
3926 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
3927 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
3928 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
3929 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
3930 PACKET_qXfer_btrace }
3933 static char *remote_support_xml;
3935 /* Register string appended to "xmlRegisters=" in qSupported query. */
3938 register_remote_support_xml (const char *xml)
3940 #if defined(HAVE_LIBEXPAT)
3941 if (remote_support_xml == NULL)
3942 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
3945 char *copy = xstrdup (remote_support_xml + 13);
3946 char *p = strtok (copy, ",");
3950 if (strcmp (p, xml) == 0)
3957 while ((p = strtok (NULL, ",")) != NULL);
3960 remote_support_xml = reconcat (remote_support_xml,
3961 remote_support_xml, ",", xml,
3968 remote_query_supported_append (char *msg, const char *append)
3971 return reconcat (msg, msg, ";", append, (char *) NULL);
3973 return xstrdup (append);
3977 remote_query_supported (void)
3979 struct remote_state *rs = get_remote_state ();
3982 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
3984 /* The packet support flags are handled differently for this packet
3985 than for most others. We treat an error, a disabled packet, and
3986 an empty response identically: any features which must be reported
3987 to be used will be automatically disabled. An empty buffer
3988 accomplishes this, since that is also the representation for a list
3989 containing no features. */
3992 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
3995 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
3997 q = remote_query_supported_append (q, "multiprocess+");
3999 if (remote_support_xml)
4000 q = remote_query_supported_append (q, remote_support_xml);
4002 q = remote_query_supported_append (q, "qRelocInsn+");
4004 q = reconcat (q, "qSupported:", q, (char *) NULL);
4007 do_cleanups (old_chain);
4009 getpkt (&rs->buf, &rs->buf_size, 0);
4011 /* If an error occured, warn, but do not return - just reset the
4012 buffer to empty and go on to disable features. */
4013 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4016 warning (_("Remote failure reply: %s"), rs->buf);
4021 memset (seen, 0, sizeof (seen));
4026 enum packet_support is_supported;
4027 char *p, *end, *name_end, *value;
4029 /* First separate out this item from the rest of the packet. If
4030 there's another item after this, we overwrite the separator
4031 (terminated strings are much easier to work with). */
4033 end = strchr (p, ';');
4036 end = p + strlen (p);
4046 warning (_("empty item in \"qSupported\" response"));
4051 name_end = strchr (p, '=');
4054 /* This is a name=value entry. */
4055 is_supported = PACKET_ENABLE;
4056 value = name_end + 1;
4065 is_supported = PACKET_ENABLE;
4069 is_supported = PACKET_DISABLE;
4073 is_supported = PACKET_SUPPORT_UNKNOWN;
4077 warning (_("unrecognized item \"%s\" "
4078 "in \"qSupported\" response"), p);
4084 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4085 if (strcmp (remote_protocol_features[i].name, p) == 0)
4087 const struct protocol_feature *feature;
4090 feature = &remote_protocol_features[i];
4091 feature->func (feature, is_supported, value);
4096 /* If we increased the packet size, make sure to increase the global
4097 buffer size also. We delay this until after parsing the entire
4098 qSupported packet, because this is the same buffer we were
4100 if (rs->buf_size < rs->explicit_packet_size)
4102 rs->buf_size = rs->explicit_packet_size;
4103 rs->buf = xrealloc (rs->buf, rs->buf_size);
4106 /* Handle the defaults for unmentioned features. */
4107 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4110 const struct protocol_feature *feature;
4112 feature = &remote_protocol_features[i];
4113 feature->func (feature, feature->default_support, NULL);
4117 /* Remove any of the remote.c targets from target stack. Upper targets depend
4118 on it so remove them first. */
4121 remote_unpush_target (void)
4123 pop_all_targets_above (process_stratum - 1);
4127 remote_open_1 (const char *name, int from_tty,
4128 struct target_ops *target, int extended_p)
4130 struct remote_state *rs = get_remote_state ();
4133 error (_("To open a remote debug connection, you need to specify what\n"
4134 "serial device is attached to the remote system\n"
4135 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4137 /* See FIXME above. */
4138 if (!target_async_permitted)
4139 wait_forever_enabled_p = 1;
4141 /* If we're connected to a running target, target_preopen will kill it.
4142 Ask this question first, before target_preopen has a chance to kill
4144 if (rs->remote_desc != NULL && !have_inferiors ())
4147 && !query (_("Already connected to a remote target. Disconnect? ")))
4148 error (_("Still connected."));
4151 /* Here the possibly existing remote target gets unpushed. */
4152 target_preopen (from_tty);
4154 /* Make sure we send the passed signals list the next time we resume. */
4155 xfree (rs->last_pass_packet);
4156 rs->last_pass_packet = NULL;
4158 /* Make sure we send the program signals list the next time we
4160 xfree (rs->last_program_signals_packet);
4161 rs->last_program_signals_packet = NULL;
4163 remote_fileio_reset ();
4164 reopen_exec_file ();
4167 rs->remote_desc = remote_serial_open (name);
4168 if (!rs->remote_desc)
4169 perror_with_name (name);
4171 if (baud_rate != -1)
4173 if (serial_setbaudrate (rs->remote_desc, baud_rate))
4175 /* The requested speed could not be set. Error out to
4176 top level after closing remote_desc. Take care to
4177 set remote_desc to NULL to avoid closing remote_desc
4179 serial_close (rs->remote_desc);
4180 rs->remote_desc = NULL;
4181 perror_with_name (name);
4185 serial_raw (rs->remote_desc);
4187 /* If there is something sitting in the buffer we might take it as a
4188 response to a command, which would be bad. */
4189 serial_flush_input (rs->remote_desc);
4193 puts_filtered ("Remote debugging using ");
4194 puts_filtered (name);
4195 puts_filtered ("\n");
4197 push_target (target); /* Switch to using remote target now. */
4199 /* Register extra event sources in the event loop. */
4200 remote_async_inferior_event_token
4201 = create_async_event_handler (remote_async_inferior_event_handler,
4203 rs->notif_state = remote_notif_state_allocate ();
4205 /* Reset the target state; these things will be queried either by
4206 remote_query_supported or as they are needed. */
4207 reset_all_packet_configs_support ();
4208 rs->cached_wait_status = 0;
4209 rs->explicit_packet_size = 0;
4211 rs->extended = extended_p;
4212 rs->waiting_for_stop_reply = 0;
4213 rs->ctrlc_pending_p = 0;
4215 rs->general_thread = not_sent_ptid;
4216 rs->continue_thread = not_sent_ptid;
4217 rs->remote_traceframe_number = -1;
4219 /* Probe for ability to use "ThreadInfo" query, as required. */
4220 rs->use_threadinfo_query = 1;
4221 rs->use_threadextra_query = 1;
4223 if (target_async_permitted)
4225 /* With this target we start out by owning the terminal. */
4226 remote_async_terminal_ours_p = 1;
4228 /* FIXME: cagney/1999-09-23: During the initial connection it is
4229 assumed that the target is already ready and able to respond to
4230 requests. Unfortunately remote_start_remote() eventually calls
4231 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4232 around this. Eventually a mechanism that allows
4233 wait_for_inferior() to expect/get timeouts will be
4235 wait_forever_enabled_p = 0;
4238 /* First delete any symbols previously loaded from shared libraries. */
4239 no_shared_libraries (NULL, 0);
4242 init_thread_list ();
4244 /* Start the remote connection. If error() or QUIT, discard this
4245 target (we'd otherwise be in an inconsistent state) and then
4246 propogate the error on up the exception chain. This ensures that
4247 the caller doesn't stumble along blindly assuming that the
4248 function succeeded. The CLI doesn't have this problem but other
4249 UI's, such as MI do.
4251 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4252 this function should return an error indication letting the
4253 caller restore the previous state. Unfortunately the command
4254 ``target remote'' is directly wired to this function making that
4255 impossible. On a positive note, the CLI side of this problem has
4256 been fixed - the function set_cmd_context() makes it possible for
4257 all the ``target ....'' commands to share a common callback
4258 function. See cli-dump.c. */
4260 volatile struct gdb_exception ex;
4262 TRY_CATCH (ex, RETURN_MASK_ALL)
4264 remote_start_remote (from_tty, target, extended_p);
4268 /* Pop the partially set up target - unless something else did
4269 already before throwing the exception. */
4270 if (rs->remote_desc != NULL)
4271 remote_unpush_target ();
4272 if (target_async_permitted)
4273 wait_forever_enabled_p = 1;
4274 throw_exception (ex);
4278 if (target_async_permitted)
4279 wait_forever_enabled_p = 1;
4282 /* This takes a program previously attached to and detaches it. After
4283 this is done, GDB can be used to debug some other program. We
4284 better not have left any breakpoints in the target program or it'll
4285 die when it hits one. */
4288 remote_detach_1 (const char *args, int from_tty, int extended)
4290 int pid = ptid_get_pid (inferior_ptid);
4291 struct remote_state *rs = get_remote_state ();
4294 error (_("Argument given to \"detach\" when remotely debugging."));
4296 if (!target_has_execution)
4297 error (_("No process to detach from."));
4301 char *exec_file = get_exec_file (0);
4302 if (exec_file == NULL)
4304 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4305 target_pid_to_str (pid_to_ptid (pid)));
4306 gdb_flush (gdb_stdout);
4309 /* Tell the remote target to detach. */
4310 if (remote_multi_process_p (rs))
4311 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4313 strcpy (rs->buf, "D");
4316 getpkt (&rs->buf, &rs->buf_size, 0);
4318 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4320 else if (rs->buf[0] == '\0')
4321 error (_("Remote doesn't know how to detach"));
4323 error (_("Can't detach process."));
4325 if (from_tty && !extended)
4326 puts_filtered (_("Ending remote debugging.\n"));
4328 target_mourn_inferior ();
4332 remote_detach (struct target_ops *ops, const char *args, int from_tty)
4334 remote_detach_1 (args, from_tty, 0);
4338 extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
4340 remote_detach_1 (args, from_tty, 1);
4343 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4346 remote_disconnect (struct target_ops *target, const char *args, int from_tty)
4349 error (_("Argument given to \"disconnect\" when remotely debugging."));
4351 /* Make sure we unpush even the extended remote targets; mourn
4352 won't do it. So call remote_mourn_1 directly instead of
4353 target_mourn_inferior. */
4354 remote_mourn_1 (target);
4357 puts_filtered ("Ending remote debugging.\n");
4360 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4361 be chatty about it. */
4364 extended_remote_attach_1 (struct target_ops *target, const char *args,
4367 struct remote_state *rs = get_remote_state ();
4369 char *wait_status = NULL;
4371 pid = parse_pid_to_attach (args);
4373 /* Remote PID can be freely equal to getpid, do not check it here the same
4374 way as in other targets. */
4376 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
4377 error (_("This target does not support attaching to a process"));
4381 char *exec_file = get_exec_file (0);
4384 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4385 target_pid_to_str (pid_to_ptid (pid)));
4387 printf_unfiltered (_("Attaching to %s\n"),
4388 target_pid_to_str (pid_to_ptid (pid)));
4390 gdb_flush (gdb_stdout);
4393 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
4395 getpkt (&rs->buf, &rs->buf_size, 0);
4397 switch (packet_ok (rs->buf,
4398 &remote_protocol_packets[PACKET_vAttach]))
4403 /* Save the reply for later. */
4404 wait_status = alloca (strlen (rs->buf) + 1);
4405 strcpy (wait_status, rs->buf);
4407 else if (strcmp (rs->buf, "OK") != 0)
4408 error (_("Attaching to %s failed with: %s"),
4409 target_pid_to_str (pid_to_ptid (pid)),
4412 case PACKET_UNKNOWN:
4413 error (_("This target does not support attaching to a process"));
4415 error (_("Attaching to %s failed"),
4416 target_pid_to_str (pid_to_ptid (pid)));
4419 set_current_inferior (remote_add_inferior (0, pid, 1));
4421 inferior_ptid = pid_to_ptid (pid);
4425 struct thread_info *thread;
4427 /* Get list of threads. */
4428 remote_threads_info (target);
4430 thread = first_thread_of_process (pid);
4432 inferior_ptid = thread->ptid;
4434 inferior_ptid = pid_to_ptid (pid);
4436 /* Invalidate our notion of the remote current thread. */
4437 record_currthread (rs, minus_one_ptid);
4441 /* Now, if we have thread information, update inferior_ptid. */
4442 inferior_ptid = remote_current_thread (inferior_ptid);
4444 /* Add the main thread to the thread list. */
4445 add_thread_silent (inferior_ptid);
4448 /* Next, if the target can specify a description, read it. We do
4449 this before anything involving memory or registers. */
4450 target_find_description ();
4454 /* Use the previously fetched status. */
4455 gdb_assert (wait_status != NULL);
4457 if (target_can_async_p ())
4459 struct notif_event *reply
4460 = remote_notif_parse (¬if_client_stop, wait_status);
4462 push_stop_reply ((struct stop_reply *) reply);
4464 target_async (inferior_event_handler, 0);
4468 gdb_assert (wait_status != NULL);
4469 strcpy (rs->buf, wait_status);
4470 rs->cached_wait_status = 1;
4474 gdb_assert (wait_status == NULL);
4478 extended_remote_attach (struct target_ops *ops, const char *args, int from_tty)
4480 extended_remote_attach_1 (ops, args, from_tty);
4483 /* Implementation of the to_post_attach method. */
4486 extended_remote_post_attach (struct target_ops *ops, int pid)
4488 /* In certain cases GDB might not have had the chance to start
4489 symbol lookup up until now. This could happen if the debugged
4490 binary is not using shared libraries, the vsyscall page is not
4491 present (on Linux) and the binary itself hadn't changed since the
4492 debugging process was started. */
4493 if (symfile_objfile != NULL)
4494 remote_check_symbols();
4498 /* Check for the availability of vCont. This function should also check
4502 remote_vcont_probe (struct remote_state *rs)
4506 strcpy (rs->buf, "vCont?");
4508 getpkt (&rs->buf, &rs->buf_size, 0);
4511 /* Make sure that the features we assume are supported. */
4512 if (strncmp (buf, "vCont", 5) == 0)
4515 int support_s, support_S, support_c, support_C;
4521 rs->supports_vCont.t = 0;
4522 rs->supports_vCont.r = 0;
4523 while (p && *p == ';')
4526 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4528 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4530 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4532 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4534 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4535 rs->supports_vCont.t = 1;
4536 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4537 rs->supports_vCont.r = 1;
4539 p = strchr (p, ';');
4542 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4543 BUF will make packet_ok disable the packet. */
4544 if (!support_s || !support_S || !support_c || !support_C)
4548 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4551 /* Helper function for building "vCont" resumptions. Write a
4552 resumption to P. ENDP points to one-passed-the-end of the buffer
4553 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4554 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4555 resumed thread should be single-stepped and/or signalled. If PTID
4556 equals minus_one_ptid, then all threads are resumed; if PTID
4557 represents a process, then all threads of the process are resumed;
4558 the thread to be stepped and/or signalled is given in the global
4562 append_resumption (char *p, char *endp,
4563 ptid_t ptid, int step, enum gdb_signal siggnal)
4565 struct remote_state *rs = get_remote_state ();
4567 if (step && siggnal != GDB_SIGNAL_0)
4568 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4570 /* GDB is willing to range step. */
4571 && use_range_stepping
4572 /* Target supports range stepping. */
4573 && rs->supports_vCont.r
4574 /* We don't currently support range stepping multiple
4575 threads with a wildcard (though the protocol allows it,
4576 so stubs shouldn't make an active effort to forbid
4578 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4580 struct thread_info *tp;
4582 if (ptid_equal (ptid, minus_one_ptid))
4584 /* If we don't know about the target thread's tid, then
4585 we're resuming magic_null_ptid (see caller). */
4586 tp = find_thread_ptid (magic_null_ptid);
4589 tp = find_thread_ptid (ptid);
4590 gdb_assert (tp != NULL);
4592 if (tp->control.may_range_step)
4594 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4596 p += xsnprintf (p, endp - p, ";r%s,%s",
4597 phex_nz (tp->control.step_range_start,
4599 phex_nz (tp->control.step_range_end,
4603 p += xsnprintf (p, endp - p, ";s");
4606 p += xsnprintf (p, endp - p, ";s");
4607 else if (siggnal != GDB_SIGNAL_0)
4608 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4610 p += xsnprintf (p, endp - p, ";c");
4612 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4616 /* All (-1) threads of process. */
4617 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4619 p += xsnprintf (p, endp - p, ":");
4620 p = write_ptid (p, endp, nptid);
4622 else if (!ptid_equal (ptid, minus_one_ptid))
4624 p += xsnprintf (p, endp - p, ":");
4625 p = write_ptid (p, endp, ptid);
4631 /* Append a vCont continue-with-signal action for threads that have a
4632 non-zero stop signal. */
4635 append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4637 struct thread_info *thread;
4639 ALL_NON_EXITED_THREADS (thread)
4640 if (ptid_match (thread->ptid, ptid)
4641 && !ptid_equal (inferior_ptid, thread->ptid)
4642 && thread->suspend.stop_signal != GDB_SIGNAL_0)
4644 p = append_resumption (p, endp, thread->ptid,
4645 0, thread->suspend.stop_signal);
4646 thread->suspend.stop_signal = GDB_SIGNAL_0;
4652 /* Resume the remote inferior by using a "vCont" packet. The thread
4653 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4654 resumed thread should be single-stepped and/or signalled. If PTID
4655 equals minus_one_ptid, then all threads are resumed; the thread to
4656 be stepped and/or signalled is given in the global INFERIOR_PTID.
4657 This function returns non-zero iff it resumes the inferior.
4659 This function issues a strict subset of all possible vCont commands at the
4663 remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
4665 struct remote_state *rs = get_remote_state ();
4669 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4670 remote_vcont_probe (rs);
4672 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
4676 endp = rs->buf + get_remote_packet_size ();
4678 /* If we could generate a wider range of packets, we'd have to worry
4679 about overflowing BUF. Should there be a generic
4680 "multi-part-packet" packet? */
4682 p += xsnprintf (p, endp - p, "vCont");
4684 if (ptid_equal (ptid, magic_null_ptid))
4686 /* MAGIC_NULL_PTID means that we don't have any active threads,
4687 so we don't have any TID numbers the inferior will
4688 understand. Make sure to only send forms that do not specify
4690 append_resumption (p, endp, minus_one_ptid, step, siggnal);
4692 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
4694 /* Resume all threads (of all processes, or of a single
4695 process), with preference for INFERIOR_PTID. This assumes
4696 inferior_ptid belongs to the set of all threads we are about
4698 if (step || siggnal != GDB_SIGNAL_0)
4700 /* Step inferior_ptid, with or without signal. */
4701 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
4704 /* Also pass down any pending signaled resumption for other
4705 threads not the current. */
4706 p = append_pending_thread_resumptions (p, endp, ptid);
4708 /* And continue others without a signal. */
4709 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
4713 /* Scheduler locking; resume only PTID. */
4714 append_resumption (p, endp, ptid, step, siggnal);
4717 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4722 /* In non-stop, the stub replies to vCont with "OK". The stop
4723 reply will be reported asynchronously by means of a `%Stop'
4725 getpkt (&rs->buf, &rs->buf_size, 0);
4726 if (strcmp (rs->buf, "OK") != 0)
4727 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4733 /* Tell the remote machine to resume. */
4736 remote_resume (struct target_ops *ops,
4737 ptid_t ptid, int step, enum gdb_signal siggnal)
4739 struct remote_state *rs = get_remote_state ();
4742 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4743 (explained in remote-notif.c:handle_notification) so
4744 remote_notif_process is not called. We need find a place where
4745 it is safe to start a 'vNotif' sequence. It is good to do it
4746 before resuming inferior, because inferior was stopped and no RSP
4747 traffic at that moment. */
4749 remote_notif_process (rs->notif_state, ¬if_client_stop);
4751 rs->last_sent_signal = siggnal;
4752 rs->last_sent_step = step;
4754 /* The vCont packet doesn't need to specify threads via Hc. */
4755 /* No reverse support (yet) for vCont. */
4756 if (execution_direction != EXEC_REVERSE)
4757 if (remote_vcont_resume (ptid, step, siggnal))
4760 /* All other supported resume packets do use Hc, so set the continue
4762 if (ptid_equal (ptid, minus_one_ptid))
4763 set_continue_thread (any_thread_ptid);
4765 set_continue_thread (ptid);
4768 if (execution_direction == EXEC_REVERSE)
4770 /* We don't pass signals to the target in reverse exec mode. */
4771 if (info_verbose && siggnal != GDB_SIGNAL_0)
4772 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4775 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
4776 error (_("Remote reverse-step not supported."));
4777 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
4778 error (_("Remote reverse-continue not supported."));
4780 strcpy (buf, step ? "bs" : "bc");
4782 else if (siggnal != GDB_SIGNAL_0)
4784 buf[0] = step ? 'S' : 'C';
4785 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
4786 buf[2] = tohex (((int) siggnal) & 0xf);
4790 strcpy (buf, step ? "s" : "c");
4795 /* We are about to start executing the inferior, let's register it
4796 with the event loop. NOTE: this is the one place where all the
4797 execution commands end up. We could alternatively do this in each
4798 of the execution commands in infcmd.c. */
4799 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4800 into infcmd.c in order to allow inferior function calls to work
4801 NOT asynchronously. */
4802 if (target_can_async_p ())
4803 target_async (inferior_event_handler, 0);
4805 /* We've just told the target to resume. The remote server will
4806 wait for the inferior to stop, and then send a stop reply. In
4807 the mean time, we can't start another command/query ourselves
4808 because the stub wouldn't be ready to process it. This applies
4809 only to the base all-stop protocol, however. In non-stop (which
4810 only supports vCont), the stub replies with an "OK", and is
4811 immediate able to process further serial input. */
4813 rs->waiting_for_stop_reply = 1;
4817 /* Set up the signal handler for SIGINT, while the target is
4818 executing, ovewriting the 'regular' SIGINT signal handler. */
4820 async_initialize_sigint_signal_handler (void)
4822 signal (SIGINT, async_handle_remote_sigint);
4825 /* Signal handler for SIGINT, while the target is executing. */
4827 async_handle_remote_sigint (int sig)
4829 signal (sig, async_handle_remote_sigint_twice);
4830 /* Note we need to go through gdb_call_async_signal_handler in order
4831 to wake up the event loop on Windows. */
4832 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
4835 /* Signal handler for SIGINT, installed after SIGINT has already been
4836 sent once. It will take effect the second time that the user sends
4839 async_handle_remote_sigint_twice (int sig)
4841 signal (sig, async_handle_remote_sigint);
4842 /* See note in async_handle_remote_sigint. */
4843 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
4846 /* Perform the real interruption of the target execution, in response
4849 async_remote_interrupt (gdb_client_data arg)
4852 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
4854 target_stop (inferior_ptid);
4857 /* Perform interrupt, if the first attempt did not succeed. Just give
4858 up on the target alltogether. */
4860 async_remote_interrupt_twice (gdb_client_data arg)
4863 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
4868 /* Reinstall the usual SIGINT handlers, after the target has
4871 async_cleanup_sigint_signal_handler (void *dummy)
4873 signal (SIGINT, handle_sigint);
4876 /* Send ^C to target to halt it. Target will respond, and send us a
4878 static void (*ofunc) (int);
4880 /* The command line interface's stop routine. This function is installed
4881 as a signal handler for SIGINT. The first time a user requests a
4882 stop, we call remote_stop to send a break or ^C. If there is no
4883 response from the target (it didn't stop when the user requested it),
4884 we ask the user if he'd like to detach from the target. */
4886 sync_remote_interrupt (int signo)
4888 /* If this doesn't work, try more severe steps. */
4889 signal (signo, sync_remote_interrupt_twice);
4891 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
4894 /* The user typed ^C twice. */
4897 sync_remote_interrupt_twice (int signo)
4899 signal (signo, ofunc);
4900 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
4901 signal (signo, sync_remote_interrupt);
4904 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4905 thread, all threads of a remote process, or all threads of all
4909 remote_stop_ns (ptid_t ptid)
4911 struct remote_state *rs = get_remote_state ();
4913 char *endp = rs->buf + get_remote_packet_size ();
4915 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4916 remote_vcont_probe (rs);
4918 if (!rs->supports_vCont.t)
4919 error (_("Remote server does not support stopping threads"));
4921 if (ptid_equal (ptid, minus_one_ptid)
4922 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4923 p += xsnprintf (p, endp - p, "vCont;t");
4928 p += xsnprintf (p, endp - p, "vCont;t:");
4930 if (ptid_is_pid (ptid))
4931 /* All (-1) threads of process. */
4932 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4935 /* Small optimization: if we already have a stop reply for
4936 this thread, no use in telling the stub we want this
4938 if (peek_stop_reply (ptid))
4944 write_ptid (p, endp, nptid);
4947 /* In non-stop, we get an immediate OK reply. The stop reply will
4948 come in asynchronously by notification. */
4950 getpkt (&rs->buf, &rs->buf_size, 0);
4951 if (strcmp (rs->buf, "OK") != 0)
4952 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
4955 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4956 remote target. It is undefined which thread of which process
4957 reports the stop. */
4960 remote_stop_as (ptid_t ptid)
4962 struct remote_state *rs = get_remote_state ();
4964 rs->ctrlc_pending_p = 1;
4966 /* If the inferior is stopped already, but the core didn't know
4967 about it yet, just ignore the request. The cached wait status
4968 will be collected in remote_wait. */
4969 if (rs->cached_wait_status)
4972 /* Send interrupt_sequence to remote target. */
4973 send_interrupt_sequence ();
4976 /* This is the generic stop called via the target vector. When a target
4977 interrupt is requested, either by the command line or the GUI, we
4978 will eventually end up here. */
4981 remote_stop (struct target_ops *self, ptid_t ptid)
4984 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
4987 remote_stop_ns (ptid);
4989 remote_stop_as (ptid);
4992 /* Ask the user what to do when an interrupt is received. */
4995 interrupt_query (void)
4997 target_terminal_ours ();
4999 if (target_can_async_p ())
5001 signal (SIGINT, handle_sigint);
5006 if (query (_("Interrupted while waiting for the program.\n\
5007 Give up (and stop debugging it)? ")))
5009 remote_unpush_target ();
5014 target_terminal_inferior ();
5017 /* Enable/disable target terminal ownership. Most targets can use
5018 terminal groups to control terminal ownership. Remote targets are
5019 different in that explicit transfer of ownership to/from GDB/target
5023 remote_terminal_inferior (struct target_ops *self)
5025 if (!target_async_permitted)
5026 /* Nothing to do. */
5029 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5030 idempotent. The event-loop GDB talking to an asynchronous target
5031 with a synchronous command calls this function from both
5032 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5033 transfer the terminal to the target when it shouldn't this guard
5035 if (!remote_async_terminal_ours_p)
5037 delete_file_handler (input_fd);
5038 remote_async_terminal_ours_p = 0;
5039 async_initialize_sigint_signal_handler ();
5040 /* NOTE: At this point we could also register our selves as the
5041 recipient of all input. Any characters typed could then be
5042 passed on down to the target. */
5046 remote_terminal_ours (struct target_ops *self)
5048 if (!target_async_permitted)
5049 /* Nothing to do. */
5052 /* See FIXME in remote_terminal_inferior. */
5053 if (remote_async_terminal_ours_p)
5055 async_cleanup_sigint_signal_handler (NULL);
5056 add_file_handler (input_fd, stdin_event_handler, 0);
5057 remote_async_terminal_ours_p = 1;
5061 remote_console_output (char *msg)
5065 for (p = msg; p[0] && p[1]; p += 2)
5068 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5072 fputs_unfiltered (tb, gdb_stdtarg);
5074 gdb_flush (gdb_stdtarg);
5077 typedef struct cached_reg
5080 gdb_byte data[MAX_REGISTER_SIZE];
5083 DEF_VEC_O(cached_reg_t);
5085 typedef struct stop_reply
5087 struct notif_event base;
5089 /* The identifier of the thread about this event */
5092 /* The remote state this event is associated with. When the remote
5093 connection, represented by a remote_state object, is closed,
5094 all the associated stop_reply events should be released. */
5095 struct remote_state *rs;
5097 struct target_waitstatus ws;
5099 /* Expedited registers. This makes remote debugging a bit more
5100 efficient for those targets that provide critical registers as
5101 part of their normal status mechanism (as another roundtrip to
5102 fetch them is avoided). */
5103 VEC(cached_reg_t) *regcache;
5105 int stopped_by_watchpoint_p;
5106 CORE_ADDR watch_data_address;
5111 DECLARE_QUEUE_P (stop_reply_p);
5112 DEFINE_QUEUE_P (stop_reply_p);
5113 /* The list of already fetched and acknowledged stop events. This
5114 queue is used for notification Stop, and other notifications
5115 don't need queue for their events, because the notification events
5116 of Stop can't be consumed immediately, so that events should be
5117 queued first, and be consumed by remote_wait_{ns,as} one per
5118 time. Other notifications can consume their events immediately,
5119 so queue is not needed for them. */
5120 static QUEUE (stop_reply_p) *stop_reply_queue;
5123 stop_reply_xfree (struct stop_reply *r)
5125 notif_event_xfree ((struct notif_event *) r);
5129 remote_notif_stop_parse (struct notif_client *self, char *buf,
5130 struct notif_event *event)
5132 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5136 remote_notif_stop_ack (struct notif_client *self, char *buf,
5137 struct notif_event *event)
5139 struct stop_reply *stop_reply = (struct stop_reply *) event;
5142 putpkt ((char *) self->ack_command);
5144 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5145 /* We got an unknown stop reply. */
5146 error (_("Unknown stop reply"));
5148 push_stop_reply (stop_reply);
5152 remote_notif_stop_can_get_pending_events (struct notif_client *self)
5154 /* We can't get pending events in remote_notif_process for
5155 notification stop, and we have to do this in remote_wait_ns
5156 instead. If we fetch all queued events from stub, remote stub
5157 may exit and we have no chance to process them back in
5159 mark_async_event_handler (remote_async_inferior_event_token);
5164 stop_reply_dtr (struct notif_event *event)
5166 struct stop_reply *r = (struct stop_reply *) event;
5168 VEC_free (cached_reg_t, r->regcache);
5171 static struct notif_event *
5172 remote_notif_stop_alloc_reply (void)
5174 struct notif_event *r
5175 = (struct notif_event *) XNEW (struct stop_reply);
5177 r->dtr = stop_reply_dtr;
5182 /* A client of notification Stop. */
5184 struct notif_client notif_client_stop =
5188 remote_notif_stop_parse,
5189 remote_notif_stop_ack,
5190 remote_notif_stop_can_get_pending_events,
5191 remote_notif_stop_alloc_reply,
5195 /* A parameter to pass data in and out. */
5197 struct queue_iter_param
5200 struct stop_reply *output;
5203 /* Remove stop replies in the queue if its pid is equal to the given
5207 remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5208 QUEUE_ITER (stop_reply_p) *iter,
5212 struct queue_iter_param *param = data;
5213 struct inferior *inf = param->input;
5215 if (ptid_get_pid (event->ptid) == inf->pid)
5217 stop_reply_xfree (event);
5218 QUEUE_remove_elem (stop_reply_p, q, iter);
5224 /* Discard all pending stop replies of inferior INF. */
5227 discard_pending_stop_replies (struct inferior *inf)
5230 struct queue_iter_param param;
5231 struct stop_reply *reply;
5232 struct remote_state *rs = get_remote_state ();
5233 struct remote_notif_state *rns = rs->notif_state;
5235 /* This function can be notified when an inferior exists. When the
5236 target is not remote, the notification state is NULL. */
5237 if (rs->remote_desc == NULL)
5240 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
5242 /* Discard the in-flight notification. */
5243 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
5245 stop_reply_xfree (reply);
5246 rns->pending_event[notif_client_stop.id] = NULL;
5250 param.output = NULL;
5251 /* Discard the stop replies we have already pulled with
5253 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5254 remove_stop_reply_for_inferior, ¶m);
5257 /* If its remote state is equal to the given remote state,
5258 remove EVENT from the stop reply queue. */
5261 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5262 QUEUE_ITER (stop_reply_p) *iter,
5266 struct queue_iter_param *param = data;
5267 struct remote_state *rs = param->input;
5269 if (event->rs == rs)
5271 stop_reply_xfree (event);
5272 QUEUE_remove_elem (stop_reply_p, q, iter);
5278 /* Discard the stop replies for RS in stop_reply_queue. */
5281 discard_pending_stop_replies_in_queue (struct remote_state *rs)
5283 struct queue_iter_param param;
5286 param.output = NULL;
5287 /* Discard the stop replies we have already pulled with
5289 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5290 remove_stop_reply_of_remote_state, ¶m);
5293 /* A parameter to pass data in and out. */
5296 remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5297 QUEUE_ITER (stop_reply_p) *iter,
5301 struct queue_iter_param *param = data;
5302 ptid_t *ptid = param->input;
5304 if (ptid_match (event->ptid, *ptid))
5306 param->output = event;
5307 QUEUE_remove_elem (stop_reply_p, q, iter);
5314 /* Remove the first reply in 'stop_reply_queue' which matches
5317 static struct stop_reply *
5318 remote_notif_remove_queued_reply (ptid_t ptid)
5320 struct queue_iter_param param;
5322 param.input = &ptid;
5323 param.output = NULL;
5325 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5326 remote_notif_remove_once_on_match, ¶m);
5328 fprintf_unfiltered (gdb_stdlog,
5329 "notif: discard queued event: 'Stop' in %s\n",
5330 target_pid_to_str (ptid));
5332 return param.output;
5335 /* Look for a queued stop reply belonging to PTID. If one is found,
5336 remove it from the queue, and return it. Returns NULL if none is
5337 found. If there are still queued events left to process, tell the
5338 event loop to get back to target_wait soon. */
5340 static struct stop_reply *
5341 queued_stop_reply (ptid_t ptid)
5343 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
5345 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5346 /* There's still at least an event left. */
5347 mark_async_event_handler (remote_async_inferior_event_token);
5352 /* Push a fully parsed stop reply in the stop reply queue. Since we
5353 know that we now have at least one queued event left to pass to the
5354 core side, tell the event loop to get back to target_wait soon. */
5357 push_stop_reply (struct stop_reply *new_event)
5359 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
5362 fprintf_unfiltered (gdb_stdlog,
5363 "notif: push 'Stop' %s to queue %d\n",
5364 target_pid_to_str (new_event->ptid),
5365 QUEUE_length (stop_reply_p,
5368 mark_async_event_handler (remote_async_inferior_event_token);
5372 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5373 QUEUE_ITER (stop_reply_p) *iter,
5374 struct stop_reply *event,
5377 ptid_t *ptid = data;
5379 return !(ptid_equal (*ptid, event->ptid)
5380 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5383 /* Returns true if we have a stop reply for PTID. */
5386 peek_stop_reply (ptid_t ptid)
5388 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5389 stop_reply_match_ptid_and_ws, &ptid);
5392 /* Parse the stop reply in BUF. Either the function succeeds, and the
5393 result is stored in EVENT, or throws an error. */
5396 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5398 struct remote_arch_state *rsa = get_remote_arch_state ();
5402 event->ptid = null_ptid;
5403 event->rs = get_remote_state ();
5404 event->ws.kind = TARGET_WAITKIND_IGNORE;
5405 event->ws.value.integer = 0;
5406 event->stopped_by_watchpoint_p = 0;
5407 event->regcache = NULL;
5412 case 'T': /* Status with PC, SP, FP, ... */
5413 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5414 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5416 n... = register number
5417 r... = register contents
5420 p = &buf[3]; /* after Txx */
5428 /* If the packet contains a register number, save it in
5429 pnum and set p1 to point to the character following it.
5430 Otherwise p1 points to p. */
5432 /* If this packet is an awatch packet, don't parse the 'a'
5433 as a register number. */
5435 if (strncmp (p, "awatch", strlen("awatch")) != 0
5436 && strncmp (p, "core", strlen ("core") != 0))
5438 /* Read the ``P'' register number. */
5439 pnum = strtol (p, &p_temp, 16);
5445 if (p1 == p) /* No register number present here. */
5447 p1 = strchr (p, ':');
5449 error (_("Malformed packet(a) (missing colon): %s\n\
5452 if (strncmp (p, "thread", p1 - p) == 0)
5453 event->ptid = read_ptid (++p1, &p);
5454 else if ((strncmp (p, "watch", p1 - p) == 0)
5455 || (strncmp (p, "rwatch", p1 - p) == 0)
5456 || (strncmp (p, "awatch", p1 - p) == 0))
5458 event->stopped_by_watchpoint_p = 1;
5459 p = unpack_varlen_hex (++p1, &addr);
5460 event->watch_data_address = (CORE_ADDR) addr;
5462 else if (strncmp (p, "library", p1 - p) == 0)
5466 while (*p_temp && *p_temp != ';')
5469 event->ws.kind = TARGET_WAITKIND_LOADED;
5472 else if (strncmp (p, "replaylog", p1 - p) == 0)
5474 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5475 /* p1 will indicate "begin" or "end", but it makes
5476 no difference for now, so ignore it. */
5477 p_temp = strchr (p1 + 1, ';');
5481 else if (strncmp (p, "core", p1 - p) == 0)
5485 p = unpack_varlen_hex (++p1, &c);
5490 /* Silently skip unknown optional info. */
5491 p_temp = strchr (p1 + 1, ';');
5498 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5499 cached_reg_t cached_reg;
5504 error (_("Malformed packet(b) (missing colon): %s\n\
5510 error (_("Remote sent bad register number %s: %s\n\
5512 hex_string (pnum), p, buf);
5514 cached_reg.num = reg->regnum;
5516 fieldsize = hex2bin (p, cached_reg.data,
5517 register_size (target_gdbarch (),
5520 if (fieldsize < register_size (target_gdbarch (),
5522 warning (_("Remote reply is too short: %s"), buf);
5524 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5528 error (_("Remote register badly formatted: %s\nhere: %s"),
5533 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5537 case 'S': /* Old style status, just signal only. */
5541 event->ws.kind = TARGET_WAITKIND_STOPPED;
5542 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5543 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5544 event->ws.value.sig = (enum gdb_signal) sig;
5546 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5549 case 'W': /* Target exited. */
5556 /* GDB used to accept only 2 hex chars here. Stubs should
5557 only send more if they detect GDB supports multi-process
5559 p = unpack_varlen_hex (&buf[1], &value);
5563 /* The remote process exited. */
5564 event->ws.kind = TARGET_WAITKIND_EXITED;
5565 event->ws.value.integer = value;
5569 /* The remote process exited with a signal. */
5570 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5571 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5572 event->ws.value.sig = (enum gdb_signal) value;
5574 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5577 /* If no process is specified, assume inferior_ptid. */
5578 pid = ptid_get_pid (inferior_ptid);
5587 else if (strncmp (p,
5588 "process:", sizeof ("process:") - 1) == 0)
5592 p += sizeof ("process:") - 1;
5593 unpack_varlen_hex (p, &upid);
5597 error (_("unknown stop reply packet: %s"), buf);
5600 error (_("unknown stop reply packet: %s"), buf);
5601 event->ptid = pid_to_ptid (pid);
5606 if (non_stop && ptid_equal (event->ptid, null_ptid))
5607 error (_("No process or thread specified in stop reply: %s"), buf);
5610 /* When the stub wants to tell GDB about a new notification reply, it
5611 sends a notification (%Stop, for example). Those can come it at
5612 any time, hence, we have to make sure that any pending
5613 putpkt/getpkt sequence we're making is finished, before querying
5614 the stub for more events with the corresponding ack command
5615 (vStopped, for example). E.g., if we started a vStopped sequence
5616 immediately upon receiving the notification, something like this
5624 1.6) <-- (registers reply to step #1.3)
5626 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5629 To solve this, whenever we parse a %Stop notification successfully,
5630 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5631 doing whatever we were doing:
5637 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5638 2.5) <-- (registers reply to step #2.3)
5640 Eventualy after step #2.5, we return to the event loop, which
5641 notices there's an event on the
5642 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5643 associated callback --- the function below. At this point, we're
5644 always safe to start a vStopped sequence. :
5647 2.7) <-- T05 thread:2
5653 remote_notif_get_pending_events (struct notif_client *nc)
5655 struct remote_state *rs = get_remote_state ();
5657 if (rs->notif_state->pending_event[nc->id] != NULL)
5660 fprintf_unfiltered (gdb_stdlog,
5661 "notif: process: '%s' ack pending event\n",
5665 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
5666 rs->notif_state->pending_event[nc->id] = NULL;
5670 getpkt (&rs->buf, &rs->buf_size, 0);
5671 if (strcmp (rs->buf, "OK") == 0)
5674 remote_notif_ack (nc, rs->buf);
5680 fprintf_unfiltered (gdb_stdlog,
5681 "notif: process: '%s' no pending reply\n",
5686 /* Called when it is decided that STOP_REPLY holds the info of the
5687 event that is to be returned to the core. This function always
5688 destroys STOP_REPLY. */
5691 process_stop_reply (struct stop_reply *stop_reply,
5692 struct target_waitstatus *status)
5696 *status = stop_reply->ws;
5697 ptid = stop_reply->ptid;
5699 /* If no thread/process was reported by the stub, assume the current
5701 if (ptid_equal (ptid, null_ptid))
5702 ptid = inferior_ptid;
5704 if (status->kind != TARGET_WAITKIND_EXITED
5705 && status->kind != TARGET_WAITKIND_SIGNALLED)
5707 struct remote_state *rs = get_remote_state ();
5709 /* Expedited registers. */
5710 if (stop_reply->regcache)
5712 struct regcache *regcache
5713 = get_thread_arch_regcache (ptid, target_gdbarch ());
5718 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5720 regcache_raw_supply (regcache, reg->num, reg->data);
5721 VEC_free (cached_reg_t, stop_reply->regcache);
5724 rs->remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5725 rs->remote_watch_data_address = stop_reply->watch_data_address;
5727 remote_notice_new_inferior (ptid, 0);
5728 demand_private_info (ptid)->core = stop_reply->core;
5731 stop_reply_xfree (stop_reply);
5735 /* The non-stop mode version of target_wait. */
5738 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
5740 struct remote_state *rs = get_remote_state ();
5741 struct stop_reply *stop_reply;
5745 /* If in non-stop mode, get out of getpkt even if a
5746 notification is received. */
5748 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5749 0 /* forever */, &is_notif);
5752 if (ret != -1 && !is_notif)
5755 case 'E': /* Error of some sort. */
5756 /* We're out of sync with the target now. Did it continue
5757 or not? We can't tell which thread it was in non-stop,
5758 so just ignore this. */
5759 warning (_("Remote failure reply: %s"), rs->buf);
5761 case 'O': /* Console output. */
5762 remote_console_output (rs->buf + 1);
5765 warning (_("Invalid remote reply: %s"), rs->buf);
5769 /* Acknowledge a pending stop reply that may have arrived in the
5771 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
5772 remote_notif_get_pending_events (¬if_client_stop);
5774 /* If indeed we noticed a stop reply, we're done. */
5775 stop_reply = queued_stop_reply (ptid);
5776 if (stop_reply != NULL)
5777 return process_stop_reply (stop_reply, status);
5779 /* Still no event. If we're just polling for an event, then
5780 return to the event loop. */
5781 if (options & TARGET_WNOHANG)
5783 status->kind = TARGET_WAITKIND_IGNORE;
5784 return minus_one_ptid;
5787 /* Otherwise do a blocking wait. */
5788 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5789 1 /* forever */, &is_notif);
5793 /* Wait until the remote machine stops, then return, storing status in
5794 STATUS just as `wait' would. */
5797 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
5799 struct remote_state *rs = get_remote_state ();
5800 ptid_t event_ptid = null_ptid;
5802 struct stop_reply *stop_reply;
5806 status->kind = TARGET_WAITKIND_IGNORE;
5807 status->value.integer = 0;
5809 stop_reply = queued_stop_reply (ptid);
5810 if (stop_reply != NULL)
5811 return process_stop_reply (stop_reply, status);
5813 if (rs->cached_wait_status)
5814 /* Use the cached wait status, but only once. */
5815 rs->cached_wait_status = 0;
5821 if (!target_is_async_p ())
5823 ofunc = signal (SIGINT, sync_remote_interrupt);
5824 /* If the user hit C-c before this packet, or between packets,
5825 pretend that it was hit right here. */
5826 if (check_quit_flag ())
5829 sync_remote_interrupt (SIGINT);
5833 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5834 _never_ wait for ever -> test on target_is_async_p().
5835 However, before we do that we need to ensure that the caller
5836 knows how to take the target into/out of async mode. */
5837 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5838 wait_forever_enabled_p, &is_notif);
5840 if (!target_is_async_p ())
5841 signal (SIGINT, ofunc);
5843 /* GDB gets a notification. Return to core as this event is
5845 if (ret != -1 && is_notif)
5846 return minus_one_ptid;
5851 rs->remote_stopped_by_watchpoint_p = 0;
5853 /* We got something. */
5854 rs->waiting_for_stop_reply = 0;
5856 /* Assume that the target has acknowledged Ctrl-C unless we receive
5857 an 'F' or 'O' packet. */
5858 if (buf[0] != 'F' && buf[0] != 'O')
5859 rs->ctrlc_pending_p = 0;
5863 case 'E': /* Error of some sort. */
5864 /* We're out of sync with the target now. Did it continue or
5865 not? Not is more likely, so report a stop. */
5866 warning (_("Remote failure reply: %s"), buf);
5867 status->kind = TARGET_WAITKIND_STOPPED;
5868 status->value.sig = GDB_SIGNAL_0;
5870 case 'F': /* File-I/O request. */
5871 remote_fileio_request (buf, rs->ctrlc_pending_p);
5872 rs->ctrlc_pending_p = 0;
5874 case 'T': case 'S': case 'X': case 'W':
5876 struct stop_reply *stop_reply
5877 = (struct stop_reply *) remote_notif_parse (¬if_client_stop,
5880 event_ptid = process_stop_reply (stop_reply, status);
5883 case 'O': /* Console output. */
5884 remote_console_output (buf + 1);
5886 /* The target didn't really stop; keep waiting. */
5887 rs->waiting_for_stop_reply = 1;
5891 if (rs->last_sent_signal != GDB_SIGNAL_0)
5893 /* Zero length reply means that we tried 'S' or 'C' and the
5894 remote system doesn't support it. */
5895 target_terminal_ours_for_output ();
5897 ("Can't send signals to this remote system. %s not sent.\n",
5898 gdb_signal_to_name (rs->last_sent_signal));
5899 rs->last_sent_signal = GDB_SIGNAL_0;
5900 target_terminal_inferior ();
5902 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
5903 putpkt ((char *) buf);
5905 /* We just told the target to resume, so a stop reply is in
5907 rs->waiting_for_stop_reply = 1;
5910 /* else fallthrough */
5912 warning (_("Invalid remote reply: %s"), buf);
5914 rs->waiting_for_stop_reply = 1;
5918 if (status->kind == TARGET_WAITKIND_IGNORE)
5920 /* Nothing interesting happened. If we're doing a non-blocking
5921 poll, we're done. Otherwise, go back to waiting. */
5922 if (options & TARGET_WNOHANG)
5923 return minus_one_ptid;
5927 else if (status->kind != TARGET_WAITKIND_EXITED
5928 && status->kind != TARGET_WAITKIND_SIGNALLED)
5930 if (!ptid_equal (event_ptid, null_ptid))
5931 record_currthread (rs, event_ptid);
5933 event_ptid = inferior_ptid;
5936 /* A process exit. Invalidate our notion of current thread. */
5937 record_currthread (rs, minus_one_ptid);
5942 /* Wait until the remote machine stops, then return, storing status in
5943 STATUS just as `wait' would. */
5946 remote_wait (struct target_ops *ops,
5947 ptid_t ptid, struct target_waitstatus *status, int options)
5952 event_ptid = remote_wait_ns (ptid, status, options);
5954 event_ptid = remote_wait_as (ptid, status, options);
5956 if (target_can_async_p ())
5958 /* If there are are events left in the queue tell the event loop
5960 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5961 mark_async_event_handler (remote_async_inferior_event_token);
5967 /* Fetch a single register using a 'p' packet. */
5970 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
5972 struct remote_state *rs = get_remote_state ();
5974 char regp[MAX_REGISTER_SIZE];
5977 if (packet_support (PACKET_p) == PACKET_DISABLE)
5980 if (reg->pnum == -1)
5985 p += hexnumstr (p, reg->pnum);
5988 getpkt (&rs->buf, &rs->buf_size, 0);
5992 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
5996 case PACKET_UNKNOWN:
5999 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6000 gdbarch_register_name (get_regcache_arch (regcache),
6005 /* If this register is unfetchable, tell the regcache. */
6008 regcache_raw_supply (regcache, reg->regnum, NULL);
6012 /* Otherwise, parse and supply the value. */
6018 error (_("fetch_register_using_p: early buf termination"));
6020 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6023 regcache_raw_supply (regcache, reg->regnum, regp);
6027 /* Fetch the registers included in the target's 'g' packet. */
6030 send_g_packet (void)
6032 struct remote_state *rs = get_remote_state ();
6035 xsnprintf (rs->buf, get_remote_packet_size (), "g");
6036 remote_send (&rs->buf, &rs->buf_size);
6038 /* We can get out of synch in various cases. If the first character
6039 in the buffer is not a hex character, assume that has happened
6040 and try to fetch another packet to read. */
6041 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6042 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6043 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6044 && rs->buf[0] != 'x') /* New: unavailable register value. */
6047 fprintf_unfiltered (gdb_stdlog,
6048 "Bad register packet; fetching a new packet\n");
6049 getpkt (&rs->buf, &rs->buf_size, 0);
6052 buf_len = strlen (rs->buf);
6054 /* Sanity check the received packet. */
6055 if (buf_len % 2 != 0)
6056 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
6062 process_g_packet (struct regcache *regcache)
6064 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6065 struct remote_state *rs = get_remote_state ();
6066 struct remote_arch_state *rsa = get_remote_arch_state ();
6071 buf_len = strlen (rs->buf);
6073 /* Further sanity checks, with knowledge of the architecture. */
6074 if (buf_len > 2 * rsa->sizeof_g_packet)
6075 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6077 /* Save the size of the packet sent to us by the target. It is used
6078 as a heuristic when determining the max size of packets that the
6079 target can safely receive. */
6080 if (rsa->actual_register_packet_size == 0)
6081 rsa->actual_register_packet_size = buf_len;
6083 /* If this is smaller than we guessed the 'g' packet would be,
6084 update our records. A 'g' reply that doesn't include a register's
6085 value implies either that the register is not available, or that
6086 the 'p' packet must be used. */
6087 if (buf_len < 2 * rsa->sizeof_g_packet)
6089 rsa->sizeof_g_packet = buf_len / 2;
6091 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6093 if (rsa->regs[i].pnum == -1)
6096 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6097 rsa->regs[i].in_g_packet = 0;
6099 rsa->regs[i].in_g_packet = 1;
6103 regs = alloca (rsa->sizeof_g_packet);
6105 /* Unimplemented registers read as all bits zero. */
6106 memset (regs, 0, rsa->sizeof_g_packet);
6108 /* Reply describes registers byte by byte, each byte encoded as two
6109 hex characters. Suck them all up, then supply them to the
6110 register cacheing/storage mechanism. */
6113 for (i = 0; i < rsa->sizeof_g_packet; i++)
6115 if (p[0] == 0 || p[1] == 0)
6116 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6117 internal_error (__FILE__, __LINE__,
6118 _("unexpected end of 'g' packet reply"));
6120 if (p[0] == 'x' && p[1] == 'x')
6121 regs[i] = 0; /* 'x' */
6123 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6127 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6129 struct packet_reg *r = &rsa->regs[i];
6133 if (r->offset * 2 >= strlen (rs->buf))
6134 /* This shouldn't happen - we adjusted in_g_packet above. */
6135 internal_error (__FILE__, __LINE__,
6136 _("unexpected end of 'g' packet reply"));
6137 else if (rs->buf[r->offset * 2] == 'x')
6139 gdb_assert (r->offset * 2 < strlen (rs->buf));
6140 /* The register isn't available, mark it as such (at
6141 the same time setting the value to zero). */
6142 regcache_raw_supply (regcache, r->regnum, NULL);
6145 regcache_raw_supply (regcache, r->regnum,
6152 fetch_registers_using_g (struct regcache *regcache)
6155 process_g_packet (regcache);
6158 /* Make the remote selected traceframe match GDB's selected
6162 set_remote_traceframe (void)
6165 struct remote_state *rs = get_remote_state ();
6167 if (rs->remote_traceframe_number == get_traceframe_number ())
6170 /* Avoid recursion, remote_trace_find calls us again. */
6171 rs->remote_traceframe_number = get_traceframe_number ();
6173 newnum = target_trace_find (tfind_number,
6174 get_traceframe_number (), 0, 0, NULL);
6176 /* Should not happen. If it does, all bets are off. */
6177 if (newnum != get_traceframe_number ())
6178 warning (_("could not set remote traceframe"));
6182 remote_fetch_registers (struct target_ops *ops,
6183 struct regcache *regcache, int regnum)
6185 struct remote_arch_state *rsa = get_remote_arch_state ();
6188 set_remote_traceframe ();
6189 set_general_thread (inferior_ptid);
6193 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6195 gdb_assert (reg != NULL);
6197 /* If this register might be in the 'g' packet, try that first -
6198 we are likely to read more than one register. If this is the
6199 first 'g' packet, we might be overly optimistic about its
6200 contents, so fall back to 'p'. */
6201 if (reg->in_g_packet)
6203 fetch_registers_using_g (regcache);
6204 if (reg->in_g_packet)
6208 if (fetch_register_using_p (regcache, reg))
6211 /* This register is not available. */
6212 regcache_raw_supply (regcache, reg->regnum, NULL);
6217 fetch_registers_using_g (regcache);
6219 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6220 if (!rsa->regs[i].in_g_packet)
6221 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
6223 /* This register is not available. */
6224 regcache_raw_supply (regcache, i, NULL);
6228 /* Prepare to store registers. Since we may send them all (using a
6229 'G' request), we have to read out the ones we don't want to change
6233 remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
6235 struct remote_arch_state *rsa = get_remote_arch_state ();
6237 gdb_byte buf[MAX_REGISTER_SIZE];
6239 /* Make sure the entire registers array is valid. */
6240 switch (packet_support (PACKET_P))
6242 case PACKET_DISABLE:
6243 case PACKET_SUPPORT_UNKNOWN:
6244 /* Make sure all the necessary registers are cached. */
6245 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6246 if (rsa->regs[i].in_g_packet)
6247 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
6254 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6255 packet was not recognized. */
6258 store_register_using_P (const struct regcache *regcache,
6259 struct packet_reg *reg)
6261 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6262 struct remote_state *rs = get_remote_state ();
6263 /* Try storing a single register. */
6264 char *buf = rs->buf;
6265 gdb_byte regp[MAX_REGISTER_SIZE];
6268 if (packet_support (PACKET_P) == PACKET_DISABLE)
6271 if (reg->pnum == -1)
6274 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
6275 p = buf + strlen (buf);
6276 regcache_raw_collect (regcache, reg->regnum, regp);
6277 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6279 getpkt (&rs->buf, &rs->buf_size, 0);
6281 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6286 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6287 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
6288 case PACKET_UNKNOWN:
6291 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6295 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6296 contents of the register cache buffer. FIXME: ignores errors. */
6299 store_registers_using_G (const struct regcache *regcache)
6301 struct remote_state *rs = get_remote_state ();
6302 struct remote_arch_state *rsa = get_remote_arch_state ();
6306 /* Extract all the registers in the regcache copying them into a
6311 regs = alloca (rsa->sizeof_g_packet);
6312 memset (regs, 0, rsa->sizeof_g_packet);
6313 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6315 struct packet_reg *r = &rsa->regs[i];
6318 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
6322 /* Command describes registers byte by byte,
6323 each byte encoded as two hex characters. */
6326 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6328 bin2hex (regs, p, rsa->sizeof_g_packet);
6330 getpkt (&rs->buf, &rs->buf_size, 0);
6331 if (packet_check_result (rs->buf) == PACKET_ERROR)
6332 error (_("Could not write registers; remote failure reply '%s'"),
6336 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6337 of the register cache buffer. FIXME: ignores errors. */
6340 remote_store_registers (struct target_ops *ops,
6341 struct regcache *regcache, int regnum)
6343 struct remote_arch_state *rsa = get_remote_arch_state ();
6346 set_remote_traceframe ();
6347 set_general_thread (inferior_ptid);
6351 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6353 gdb_assert (reg != NULL);
6355 /* Always prefer to store registers using the 'P' packet if
6356 possible; we often change only a small number of registers.
6357 Sometimes we change a larger number; we'd need help from a
6358 higher layer to know to use 'G'. */
6359 if (store_register_using_P (regcache, reg))
6362 /* For now, don't complain if we have no way to write the
6363 register. GDB loses track of unavailable registers too
6364 easily. Some day, this may be an error. We don't have
6365 any way to read the register, either... */
6366 if (!reg->in_g_packet)
6369 store_registers_using_G (regcache);
6373 store_registers_using_G (regcache);
6375 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6376 if (!rsa->regs[i].in_g_packet)
6377 if (!store_register_using_P (regcache, &rsa->regs[i]))
6378 /* See above for why we do not issue an error here. */
6383 /* Return the number of hex digits in num. */
6386 hexnumlen (ULONGEST num)
6390 for (i = 0; num != 0; i++)
6396 /* Set BUF to the minimum number of hex digits representing NUM. */
6399 hexnumstr (char *buf, ULONGEST num)
6401 int len = hexnumlen (num);
6403 return hexnumnstr (buf, num, len);
6407 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6410 hexnumnstr (char *buf, ULONGEST num, int width)
6416 for (i = width - 1; i >= 0; i--)
6418 buf[i] = "0123456789abcdef"[(num & 0xf)];
6425 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6428 remote_address_masked (CORE_ADDR addr)
6430 unsigned int address_size = remote_address_size;
6432 /* If "remoteaddresssize" was not set, default to target address size. */
6434 address_size = gdbarch_addr_bit (target_gdbarch ());
6436 if (address_size > 0
6437 && address_size < (sizeof (ULONGEST) * 8))
6439 /* Only create a mask when that mask can safely be constructed
6440 in a ULONGEST variable. */
6443 mask = (mask << address_size) - 1;
6449 /* Determine whether the remote target supports binary downloading.
6450 This is accomplished by sending a no-op memory write of zero length
6451 to the target at the specified address. It does not suffice to send
6452 the whole packet, since many stubs strip the eighth bit and
6453 subsequently compute a wrong checksum, which causes real havoc with
6456 NOTE: This can still lose if the serial line is not eight-bit
6457 clean. In cases like this, the user should clear "remote
6461 check_binary_download (CORE_ADDR addr)
6463 struct remote_state *rs = get_remote_state ();
6465 switch (packet_support (PACKET_X))
6467 case PACKET_DISABLE:
6471 case PACKET_SUPPORT_UNKNOWN:
6477 p += hexnumstr (p, (ULONGEST) addr);
6479 p += hexnumstr (p, (ULONGEST) 0);
6483 putpkt_binary (rs->buf, (int) (p - rs->buf));
6484 getpkt (&rs->buf, &rs->buf_size, 0);
6486 if (rs->buf[0] == '\0')
6489 fprintf_unfiltered (gdb_stdlog,
6490 "binary downloading NOT "
6491 "supported by target\n");
6492 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6497 fprintf_unfiltered (gdb_stdlog,
6498 "binary downloading supported by target\n");
6499 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6506 /* Write memory data directly to the remote machine.
6507 This does not inform the data cache; the data cache uses this.
6508 HEADER is the starting part of the packet.
6509 MEMADDR is the address in the remote memory space.
6510 MYADDR is the address of the buffer in our space.
6511 LEN is the number of bytes.
6512 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6513 should send data as binary ('X'), or hex-encoded ('M').
6515 The function creates packet of the form
6516 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6518 where encoding of <DATA> is termined by PACKET_FORMAT.
6520 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6523 Return the transferred status, error or OK (an
6524 'enum target_xfer_status' value). Save the number of bytes
6525 transferred in *XFERED_LEN. Only transfer a single packet. */
6527 static enum target_xfer_status
6528 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6529 const gdb_byte *myaddr, ULONGEST len,
6530 ULONGEST *xfered_len, char packet_format,
6533 struct remote_state *rs = get_remote_state ();
6543 if (packet_format != 'X' && packet_format != 'M')
6544 internal_error (__FILE__, __LINE__,
6545 _("remote_write_bytes_aux: bad packet format"));
6548 return TARGET_XFER_EOF;
6550 payload_size = get_memory_write_packet_size ();
6552 /* The packet buffer will be large enough for the payload;
6553 get_memory_packet_size ensures this. */
6556 /* Compute the size of the actual payload by subtracting out the
6557 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6559 payload_size -= strlen ("$,:#NN");
6561 /* The comma won't be used. */
6563 header_length = strlen (header);
6564 payload_size -= header_length;
6565 payload_size -= hexnumlen (memaddr);
6567 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6569 strcat (rs->buf, header);
6570 p = rs->buf + strlen (header);
6572 /* Compute a best guess of the number of bytes actually transfered. */
6573 if (packet_format == 'X')
6575 /* Best guess at number of bytes that will fit. */
6576 todo = min (len, payload_size);
6578 payload_size -= hexnumlen (todo);
6579 todo = min (todo, payload_size);
6583 /* Num bytes that will fit. */
6584 todo = min (len, payload_size / 2);
6586 payload_size -= hexnumlen (todo);
6587 todo = min (todo, payload_size / 2);
6591 internal_error (__FILE__, __LINE__,
6592 _("minimum packet size too small to write data"));
6594 /* If we already need another packet, then try to align the end
6595 of this packet to a useful boundary. */
6596 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6597 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6599 /* Append "<memaddr>". */
6600 memaddr = remote_address_masked (memaddr);
6601 p += hexnumstr (p, (ULONGEST) memaddr);
6608 /* Append <len>. Retain the location/size of <len>. It may need to
6609 be adjusted once the packet body has been created. */
6611 plenlen = hexnumstr (p, (ULONGEST) todo);
6619 /* Append the packet body. */
6620 if (packet_format == 'X')
6622 /* Binary mode. Send target system values byte by byte, in
6623 increasing byte addresses. Only escape certain critical
6625 payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p,
6626 &nr_bytes, payload_size);
6628 /* If not all TODO bytes fit, then we'll need another packet. Make
6629 a second try to keep the end of the packet aligned. Don't do
6630 this if the packet is tiny. */
6631 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6635 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6637 if (new_nr_bytes != nr_bytes)
6638 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6639 (gdb_byte *) p, &nr_bytes,
6643 p += payload_length;
6644 if (use_length && nr_bytes < todo)
6646 /* Escape chars have filled up the buffer prematurely,
6647 and we have actually sent fewer bytes than planned.
6648 Fix-up the length field of the packet. Use the same
6649 number of characters as before. */
6650 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6651 *plen = ':'; /* overwrite \0 from hexnumnstr() */
6656 /* Normal mode: Send target system values byte by byte, in
6657 increasing byte addresses. Each byte is encoded as a two hex
6659 nr_bytes = bin2hex (myaddr, p, todo);
6663 putpkt_binary (rs->buf, (int) (p - rs->buf));
6664 getpkt (&rs->buf, &rs->buf_size, 0);
6666 if (rs->buf[0] == 'E')
6667 return TARGET_XFER_E_IO;
6669 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6670 fewer bytes than we'd planned. */
6671 *xfered_len = (ULONGEST) nr_bytes;
6672 return TARGET_XFER_OK;
6675 /* Write memory data directly to the remote machine.
6676 This does not inform the data cache; the data cache uses this.
6677 MEMADDR is the address in the remote memory space.
6678 MYADDR is the address of the buffer in our space.
6679 LEN is the number of bytes.
6681 Return the transferred status, error or OK (an
6682 'enum target_xfer_status' value). Save the number of bytes
6683 transferred in *XFERED_LEN. Only transfer a single packet. */
6685 static enum target_xfer_status
6686 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
6687 ULONGEST *xfered_len)
6689 char *packet_format = 0;
6691 /* Check whether the target supports binary download. */
6692 check_binary_download (memaddr);
6694 switch (packet_support (PACKET_X))
6697 packet_format = "X";
6699 case PACKET_DISABLE:
6700 packet_format = "M";
6702 case PACKET_SUPPORT_UNKNOWN:
6703 internal_error (__FILE__, __LINE__,
6704 _("remote_write_bytes: bad internal state"));
6706 internal_error (__FILE__, __LINE__, _("bad switch"));
6709 return remote_write_bytes_aux (packet_format,
6710 memaddr, myaddr, len, xfered_len,
6711 packet_format[0], 1);
6714 /* Read memory data directly from the remote machine.
6715 This does not use the data cache; the data cache uses this.
6716 MEMADDR is the address in the remote memory space.
6717 MYADDR is the address of the buffer in our space.
6718 LEN is the number of bytes.
6720 Return the transferred status, error or OK (an
6721 'enum target_xfer_status' value). Save the number of bytes
6722 transferred in *XFERED_LEN. */
6724 static enum target_xfer_status
6725 remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len,
6726 ULONGEST *xfered_len)
6728 struct remote_state *rs = get_remote_state ();
6729 int max_buf_size; /* Max size of packet output buffer. */
6734 max_buf_size = get_memory_read_packet_size ();
6735 /* The packet buffer will be large enough for the payload;
6736 get_memory_packet_size ensures this. */
6738 /* Number if bytes that will fit. */
6739 todo = min (len, max_buf_size / 2);
6741 /* Construct "m"<memaddr>","<len>". */
6742 memaddr = remote_address_masked (memaddr);
6745 p += hexnumstr (p, (ULONGEST) memaddr);
6747 p += hexnumstr (p, (ULONGEST) todo);
6750 getpkt (&rs->buf, &rs->buf_size, 0);
6751 if (rs->buf[0] == 'E'
6752 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6753 && rs->buf[3] == '\0')
6754 return TARGET_XFER_E_IO;
6755 /* Reply describes memory byte by byte, each byte encoded as two hex
6758 i = hex2bin (p, myaddr, todo);
6759 /* Return what we have. Let higher layers handle partial reads. */
6760 *xfered_len = (ULONGEST) i;
6761 return TARGET_XFER_OK;
6764 /* Using the set of read-only target sections of remote, read live
6767 For interface/parameters/return description see target.h,
6770 static enum target_xfer_status
6771 remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
6772 ULONGEST memaddr, ULONGEST len,
6773 ULONGEST *xfered_len)
6775 struct target_section *secp;
6776 struct target_section_table *table;
6778 secp = target_section_by_addr (ops, memaddr);
6780 && (bfd_get_section_flags (secp->the_bfd_section->owner,
6781 secp->the_bfd_section)
6784 struct target_section *p;
6785 ULONGEST memend = memaddr + len;
6787 table = target_get_section_table (ops);
6789 for (p = table->sections; p < table->sections_end; p++)
6791 if (memaddr >= p->addr)
6793 if (memend <= p->endaddr)
6795 /* Entire transfer is within this section. */
6796 return remote_read_bytes_1 (memaddr, readbuf, len,
6799 else if (memaddr >= p->endaddr)
6801 /* This section ends before the transfer starts. */
6806 /* This section overlaps the transfer. Just do half. */
6807 len = p->endaddr - memaddr;
6808 return remote_read_bytes_1 (memaddr, readbuf, len,
6815 return TARGET_XFER_EOF;
6818 /* Similar to remote_read_bytes_1, but it reads from the remote stub
6819 first if the requested memory is unavailable in traceframe.
6820 Otherwise, fall back to remote_read_bytes_1. */
6822 static enum target_xfer_status
6823 remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
6824 gdb_byte *myaddr, ULONGEST len, ULONGEST *xfered_len)
6829 if (get_traceframe_number () != -1)
6831 VEC(mem_range_s) *available;
6833 /* If we fail to get the set of available memory, then the
6834 target does not support querying traceframe info, and so we
6835 attempt reading from the traceframe anyway (assuming the
6836 target implements the old QTro packet then). */
6837 if (traceframe_available_memory (&available, memaddr, len))
6839 struct cleanup *old_chain;
6841 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
6843 if (VEC_empty (mem_range_s, available)
6844 || VEC_index (mem_range_s, available, 0)->start != memaddr)
6846 enum target_xfer_status res;
6848 /* Don't read into the traceframe's available
6850 if (!VEC_empty (mem_range_s, available))
6852 LONGEST oldlen = len;
6854 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
6855 gdb_assert (len <= oldlen);
6858 do_cleanups (old_chain);
6860 /* This goes through the topmost target again. */
6861 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
6863 if (res == TARGET_XFER_OK)
6864 return TARGET_XFER_OK;
6867 /* No use trying further, we know some memory starting
6868 at MEMADDR isn't available. */
6870 return TARGET_XFER_UNAVAILABLE;
6874 /* Don't try to read more than how much is available, in
6875 case the target implements the deprecated QTro packet to
6876 cater for older GDBs (the target's knowledge of read-only
6877 sections may be outdated by now). */
6878 len = VEC_index (mem_range_s, available, 0)->length;
6880 do_cleanups (old_chain);
6884 return remote_read_bytes_1 (memaddr, myaddr, len, xfered_len);
6889 /* Sends a packet with content determined by the printf format string
6890 FORMAT and the remaining arguments, then gets the reply. Returns
6891 whether the packet was a success, a failure, or unknown. */
6893 static enum packet_result
6894 remote_send_printf (const char *format, ...)
6896 struct remote_state *rs = get_remote_state ();
6897 int max_size = get_remote_packet_size ();
6900 va_start (ap, format);
6903 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
6904 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
6906 if (putpkt (rs->buf) < 0)
6907 error (_("Communication problem with target."));
6910 getpkt (&rs->buf, &rs->buf_size, 0);
6912 return packet_check_result (rs->buf);
6916 restore_remote_timeout (void *p)
6918 int value = *(int *)p;
6920 remote_timeout = value;
6923 /* Flash writing can take quite some time. We'll set
6924 effectively infinite timeout for flash operations.
6925 In future, we'll need to decide on a better approach. */
6926 static const int remote_flash_timeout = 1000;
6929 remote_flash_erase (struct target_ops *ops,
6930 ULONGEST address, LONGEST length)
6932 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
6933 int saved_remote_timeout = remote_timeout;
6934 enum packet_result ret;
6935 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6936 &saved_remote_timeout);
6938 remote_timeout = remote_flash_timeout;
6940 ret = remote_send_printf ("vFlashErase:%s,%s",
6941 phex (address, addr_size),
6945 case PACKET_UNKNOWN:
6946 error (_("Remote target does not support flash erase"));
6948 error (_("Error erasing flash with vFlashErase packet"));
6953 do_cleanups (back_to);
6956 static enum target_xfer_status
6957 remote_flash_write (struct target_ops *ops, ULONGEST address,
6958 ULONGEST length, ULONGEST *xfered_len,
6959 const gdb_byte *data)
6961 int saved_remote_timeout = remote_timeout;
6962 enum target_xfer_status ret;
6963 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6964 &saved_remote_timeout);
6966 remote_timeout = remote_flash_timeout;
6967 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length,
6969 do_cleanups (back_to);
6975 remote_flash_done (struct target_ops *ops)
6977 int saved_remote_timeout = remote_timeout;
6979 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6980 &saved_remote_timeout);
6982 remote_timeout = remote_flash_timeout;
6983 ret = remote_send_printf ("vFlashDone");
6984 do_cleanups (back_to);
6988 case PACKET_UNKNOWN:
6989 error (_("Remote target does not support vFlashDone"));
6991 error (_("Error finishing flash operation"));
6998 remote_files_info (struct target_ops *ignore)
7000 puts_filtered ("Debugging a target over a serial line.\n");
7003 /* Stuff for dealing with the packets which are part of this protocol.
7004 See comment at top of file for details. */
7006 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7007 error to higher layers. Called when a serial error is detected.
7008 The exception message is STRING, followed by a colon and a blank,
7009 the system error message for errno at function entry and final dot
7010 for output compatibility with throw_perror_with_name. */
7013 unpush_and_perror (const char *string)
7015 int saved_errno = errno;
7017 remote_unpush_target ();
7018 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7019 safe_strerror (saved_errno));
7022 /* Read a single character from the remote end. */
7025 readchar (int timeout)
7028 struct remote_state *rs = get_remote_state ();
7030 ch = serial_readchar (rs->remote_desc, timeout);
7035 switch ((enum serial_rc) ch)
7038 remote_unpush_target ();
7039 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
7042 unpush_and_perror (_("Remote communication error. "
7043 "Target disconnected."));
7045 case SERIAL_TIMEOUT:
7051 /* Wrapper for serial_write that closes the target and throws if
7055 remote_serial_write (const char *str, int len)
7057 struct remote_state *rs = get_remote_state ();
7059 if (serial_write (rs->remote_desc, str, len))
7061 unpush_and_perror (_("Remote communication error. "
7062 "Target disconnected."));
7066 /* Send the command in *BUF to the remote machine, and read the reply
7067 into *BUF. Report an error if we get an error reply. Resize
7068 *BUF using xrealloc if necessary to hold the result, and update
7072 remote_send (char **buf,
7076 getpkt (buf, sizeof_buf, 0);
7078 if ((*buf)[0] == 'E')
7079 error (_("Remote failure reply: %s"), *buf);
7082 /* Return a pointer to an xmalloc'ed string representing an escaped
7083 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7084 etc. The caller is responsible for releasing the returned
7088 escape_buffer (const char *buf, int n)
7090 struct cleanup *old_chain;
7091 struct ui_file *stb;
7094 stb = mem_fileopen ();
7095 old_chain = make_cleanup_ui_file_delete (stb);
7097 fputstrn_unfiltered (buf, n, '\\', stb);
7098 str = ui_file_xstrdup (stb, NULL);
7099 do_cleanups (old_chain);
7103 /* Display a null-terminated packet on stdout, for debugging, using C
7107 print_packet (const char *buf)
7109 puts_filtered ("\"");
7110 fputstr_filtered (buf, '"', gdb_stdout);
7111 puts_filtered ("\"");
7115 putpkt (const char *buf)
7117 return putpkt_binary (buf, strlen (buf));
7120 /* Send a packet to the remote machine, with error checking. The data
7121 of the packet is in BUF. The string in BUF can be at most
7122 get_remote_packet_size () - 5 to account for the $, # and checksum,
7123 and for a possible /0 if we are debugging (remote_debug) and want
7124 to print the sent packet as a string. */
7127 putpkt_binary (const char *buf, int cnt)
7129 struct remote_state *rs = get_remote_state ();
7131 unsigned char csum = 0;
7132 char *buf2 = alloca (cnt + 6);
7139 /* Catch cases like trying to read memory or listing threads while
7140 we're waiting for a stop reply. The remote server wouldn't be
7141 ready to handle this request, so we'd hang and timeout. We don't
7142 have to worry about this in synchronous mode, because in that
7143 case it's not possible to issue a command while the target is
7144 running. This is not a problem in non-stop mode, because in that
7145 case, the stub is always ready to process serial input. */
7146 if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
7148 error (_("Cannot execute this command while the target is running.\n"
7149 "Use the \"interrupt\" command to stop the target\n"
7150 "and then try again."));
7153 /* We're sending out a new packet. Make sure we don't look at a
7154 stale cached response. */
7155 rs->cached_wait_status = 0;
7157 /* Copy the packet into buffer BUF2, encapsulating it
7158 and giving it a checksum. */
7163 for (i = 0; i < cnt; i++)
7169 *p++ = tohex ((csum >> 4) & 0xf);
7170 *p++ = tohex (csum & 0xf);
7172 /* Send it over and over until we get a positive ack. */
7176 int started_error_output = 0;
7180 struct cleanup *old_chain;
7184 str = escape_buffer (buf2, p - buf2);
7185 old_chain = make_cleanup (xfree, str);
7186 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
7187 gdb_flush (gdb_stdlog);
7188 do_cleanups (old_chain);
7190 remote_serial_write (buf2, p - buf2);
7192 /* If this is a no acks version of the remote protocol, send the
7193 packet and move on. */
7197 /* Read until either a timeout occurs (-2) or '+' is read.
7198 Handle any notification that arrives in the mean time. */
7201 ch = readchar (remote_timeout);
7209 case SERIAL_TIMEOUT:
7212 if (started_error_output)
7214 putchar_unfiltered ('\n');
7215 started_error_output = 0;
7224 fprintf_unfiltered (gdb_stdlog, "Ack\n");
7228 fprintf_unfiltered (gdb_stdlog, "Nak\n");
7230 case SERIAL_TIMEOUT:
7234 break; /* Retransmit buffer. */
7238 fprintf_unfiltered (gdb_stdlog,
7239 "Packet instead of Ack, ignoring it\n");
7240 /* It's probably an old response sent because an ACK
7241 was lost. Gobble up the packet and ack it so it
7242 doesn't get retransmitted when we resend this
7245 remote_serial_write ("+", 1);
7246 continue; /* Now, go look for +. */
7253 /* If we got a notification, handle it, and go back to looking
7255 /* We've found the start of a notification. Now
7256 collect the data. */
7257 val = read_frame (&rs->buf, &rs->buf_size);
7262 struct cleanup *old_chain;
7265 str = escape_buffer (rs->buf, val);
7266 old_chain = make_cleanup (xfree, str);
7267 fprintf_unfiltered (gdb_stdlog,
7268 " Notification received: %s\n",
7270 do_cleanups (old_chain);
7272 handle_notification (rs->notif_state, rs->buf);
7273 /* We're in sync now, rewait for the ack. */
7280 if (!started_error_output)
7282 started_error_output = 1;
7283 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7285 fputc_unfiltered (ch & 0177, gdb_stdlog);
7286 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7295 if (!started_error_output)
7297 started_error_output = 1;
7298 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7300 fputc_unfiltered (ch & 0177, gdb_stdlog);
7304 break; /* Here to retransmit. */
7308 /* This is wrong. If doing a long backtrace, the user should be
7309 able to get out next time we call QUIT, without anything as
7310 violent as interrupt_query. If we want to provide a way out of
7311 here without getting to the next QUIT, it should be based on
7312 hitting ^C twice as in remote_wait. */
7323 /* Come here after finding the start of a frame when we expected an
7324 ack. Do our best to discard the rest of this packet. */
7333 c = readchar (remote_timeout);
7336 case SERIAL_TIMEOUT:
7337 /* Nothing we can do. */
7340 /* Discard the two bytes of checksum and stop. */
7341 c = readchar (remote_timeout);
7343 c = readchar (remote_timeout);
7346 case '*': /* Run length encoding. */
7347 /* Discard the repeat count. */
7348 c = readchar (remote_timeout);
7353 /* A regular character. */
7359 /* Come here after finding the start of the frame. Collect the rest
7360 into *BUF, verifying the checksum, length, and handling run-length
7361 compression. NUL terminate the buffer. If there is not enough room,
7362 expand *BUF using xrealloc.
7364 Returns -1 on error, number of characters in buffer (ignoring the
7365 trailing NULL) on success. (could be extended to return one of the
7366 SERIAL status indications). */
7369 read_frame (char **buf_p,
7376 struct remote_state *rs = get_remote_state ();
7383 c = readchar (remote_timeout);
7386 case SERIAL_TIMEOUT:
7388 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
7392 fputs_filtered ("Saw new packet start in middle of old one\n",
7394 return -1; /* Start a new packet, count retries. */
7397 unsigned char pktcsum;
7403 check_0 = readchar (remote_timeout);
7405 check_1 = readchar (remote_timeout);
7407 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7410 fputs_filtered ("Timeout in checksum, retrying\n",
7414 else if (check_0 < 0 || check_1 < 0)
7417 fputs_filtered ("Communication error in checksum\n",
7422 /* Don't recompute the checksum; with no ack packets we
7423 don't have any way to indicate a packet retransmission
7428 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7429 if (csum == pktcsum)
7434 struct cleanup *old_chain;
7437 str = escape_buffer (buf, bc);
7438 old_chain = make_cleanup (xfree, str);
7439 fprintf_unfiltered (gdb_stdlog,
7440 "Bad checksum, sentsum=0x%x, "
7441 "csum=0x%x, buf=%s\n",
7442 pktcsum, csum, str);
7443 do_cleanups (old_chain);
7445 /* Number of characters in buffer ignoring trailing
7449 case '*': /* Run length encoding. */
7454 c = readchar (remote_timeout);
7456 repeat = c - ' ' + 3; /* Compute repeat count. */
7458 /* The character before ``*'' is repeated. */
7460 if (repeat > 0 && repeat <= 255 && bc > 0)
7462 if (bc + repeat - 1 >= *sizeof_buf - 1)
7464 /* Make some more room in the buffer. */
7465 *sizeof_buf += repeat;
7466 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7470 memset (&buf[bc], buf[bc - 1], repeat);
7476 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7480 if (bc >= *sizeof_buf - 1)
7482 /* Make some more room in the buffer. */
7484 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7495 /* Read a packet from the remote machine, with error checking, and
7496 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7497 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7498 rather than timing out; this is used (in synchronous mode) to wait
7499 for a target that is is executing user code to stop. */
7500 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7501 don't have to change all the calls to getpkt to deal with the
7502 return value, because at the moment I don't know what the right
7503 thing to do it for those. */
7511 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7515 /* Read a packet from the remote machine, with error checking, and
7516 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7517 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7518 rather than timing out; this is used (in synchronous mode) to wait
7519 for a target that is is executing user code to stop. If FOREVER ==
7520 0, this function is allowed to time out gracefully and return an
7521 indication of this to the caller. Otherwise return the number of
7522 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7523 enough reason to return to the caller. *IS_NOTIF is an output
7524 boolean that indicates whether *BUF holds a notification or not
7525 (a regular packet). */
7528 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7529 int expecting_notif, int *is_notif)
7531 struct remote_state *rs = get_remote_state ();
7537 /* We're reading a new response. Make sure we don't look at a
7538 previously cached response. */
7539 rs->cached_wait_status = 0;
7541 strcpy (*buf, "timeout");
7544 timeout = watchdog > 0 ? watchdog : -1;
7545 else if (expecting_notif)
7546 timeout = 0; /* There should already be a char in the buffer. If
7549 timeout = remote_timeout;
7553 /* Process any number of notifications, and then return when
7557 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
7559 for (tries = 1; tries <= MAX_TRIES; tries++)
7561 /* This can loop forever if the remote side sends us
7562 characters continuously, but if it pauses, we'll get
7563 SERIAL_TIMEOUT from readchar because of timeout. Then
7564 we'll count that as a retry.
7566 Note that even when forever is set, we will only wait
7567 forever prior to the start of a packet. After that, we
7568 expect characters to arrive at a brisk pace. They should
7569 show up within remote_timeout intervals. */
7571 c = readchar (timeout);
7572 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
7574 if (c == SERIAL_TIMEOUT)
7576 if (expecting_notif)
7577 return -1; /* Don't complain, it's normal to not get
7578 anything in this case. */
7580 if (forever) /* Watchdog went off? Kill the target. */
7583 remote_unpush_target ();
7584 throw_error (TARGET_CLOSE_ERROR,
7585 _("Watchdog timeout has expired. "
7586 "Target detached."));
7589 fputs_filtered ("Timed out.\n", gdb_stdlog);
7593 /* We've found the start of a packet or notification.
7594 Now collect the data. */
7595 val = read_frame (buf, sizeof_buf);
7600 remote_serial_write ("-", 1);
7603 if (tries > MAX_TRIES)
7605 /* We have tried hard enough, and just can't receive the
7606 packet/notification. Give up. */
7607 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7609 /* Skip the ack char if we're in no-ack mode. */
7610 if (!rs->noack_mode)
7611 remote_serial_write ("+", 1);
7615 /* If we got an ordinary packet, return that to our caller. */
7620 struct cleanup *old_chain;
7623 str = escape_buffer (*buf, val);
7624 old_chain = make_cleanup (xfree, str);
7625 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7626 do_cleanups (old_chain);
7629 /* Skip the ack char if we're in no-ack mode. */
7630 if (!rs->noack_mode)
7631 remote_serial_write ("+", 1);
7632 if (is_notif != NULL)
7637 /* If we got a notification, handle it, and go back to looking
7641 gdb_assert (c == '%');
7645 struct cleanup *old_chain;
7648 str = escape_buffer (*buf, val);
7649 old_chain = make_cleanup (xfree, str);
7650 fprintf_unfiltered (gdb_stdlog,
7651 " Notification received: %s\n",
7653 do_cleanups (old_chain);
7655 if (is_notif != NULL)
7658 handle_notification (rs->notif_state, *buf);
7660 /* Notifications require no acknowledgement. */
7662 if (expecting_notif)
7669 getpkt_sane (char **buf, long *sizeof_buf, int forever)
7671 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
7675 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
7678 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
7684 remote_kill (struct target_ops *ops)
7686 volatile struct gdb_exception ex;
7688 /* Catch errors so the user can quit from gdb even when we
7689 aren't on speaking terms with the remote system. */
7690 TRY_CATCH (ex, RETURN_MASK_ERROR)
7696 if (ex.error == TARGET_CLOSE_ERROR)
7698 /* If we got an (EOF) error that caused the target
7699 to go away, then we're done, that's what we wanted.
7700 "k" is susceptible to cause a premature EOF, given
7701 that the remote server isn't actually required to
7702 reply to "k", and it can happen that it doesn't
7703 even get to reply ACK to the "k". */
7707 /* Otherwise, something went wrong. We didn't actually kill
7708 the target. Just propagate the exception, and let the
7709 user or higher layers decide what to do. */
7710 throw_exception (ex);
7713 /* We've killed the remote end, we get to mourn it. Since this is
7714 target remote, single-process, mourning the inferior also
7715 unpushes remote_ops. */
7716 target_mourn_inferior ();
7720 remote_vkill (int pid, struct remote_state *rs)
7722 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
7725 /* Tell the remote target to detach. */
7726 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
7728 getpkt (&rs->buf, &rs->buf_size, 0);
7730 switch (packet_ok (rs->buf,
7731 &remote_protocol_packets[PACKET_vKill]))
7737 case PACKET_UNKNOWN:
7740 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7745 extended_remote_kill (struct target_ops *ops)
7748 int pid = ptid_get_pid (inferior_ptid);
7749 struct remote_state *rs = get_remote_state ();
7751 res = remote_vkill (pid, rs);
7752 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
7754 /* Don't try 'k' on a multi-process aware stub -- it has no way
7755 to specify the pid. */
7759 getpkt (&rs->buf, &rs->buf_size, 0);
7760 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7763 /* Don't wait for it to die. I'm not really sure it matters whether
7764 we do or not. For the existing stubs, kill is a noop. */
7770 error (_("Can't kill process"));
7772 target_mourn_inferior ();
7776 remote_mourn (struct target_ops *ops)
7778 remote_mourn_1 (ops);
7781 /* Worker function for remote_mourn. */
7783 remote_mourn_1 (struct target_ops *target)
7785 unpush_target (target);
7787 /* remote_close takes care of doing most of the clean up. */
7788 generic_mourn_inferior ();
7792 extended_remote_mourn_1 (struct target_ops *target)
7794 struct remote_state *rs = get_remote_state ();
7796 /* In case we got here due to an error, but we're going to stay
7798 rs->waiting_for_stop_reply = 0;
7800 /* If the current general thread belonged to the process we just
7801 detached from or has exited, the remote side current general
7802 thread becomes undefined. Considering a case like this:
7804 - We just got here due to a detach.
7805 - The process that we're detaching from happens to immediately
7806 report a global breakpoint being hit in non-stop mode, in the
7807 same thread we had selected before.
7808 - GDB attaches to this process again.
7809 - This event happens to be the next event we handle.
7811 GDB would consider that the current general thread didn't need to
7812 be set on the stub side (with Hg), since for all it knew,
7813 GENERAL_THREAD hadn't changed.
7815 Notice that although in all-stop mode, the remote server always
7816 sets the current thread to the thread reporting the stop event,
7817 that doesn't happen in non-stop mode; in non-stop, the stub *must
7818 not* change the current thread when reporting a breakpoint hit,
7819 due to the decoupling of event reporting and event handling.
7821 To keep things simple, we always invalidate our notion of the
7823 record_currthread (rs, minus_one_ptid);
7825 /* Unlike "target remote", we do not want to unpush the target; then
7826 the next time the user says "run", we won't be connected. */
7828 /* Call common code to mark the inferior as not running. */
7829 generic_mourn_inferior ();
7831 if (!have_inferiors ())
7833 if (!remote_multi_process_p (rs))
7835 /* Check whether the target is running now - some remote stubs
7836 automatically restart after kill. */
7838 getpkt (&rs->buf, &rs->buf_size, 0);
7840 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7842 /* Assume that the target has been restarted. Set
7843 inferior_ptid so that bits of core GDB realizes
7844 there's something here, e.g., so that the user can
7845 say "kill" again. */
7846 inferior_ptid = magic_null_ptid;
7853 extended_remote_mourn (struct target_ops *ops)
7855 extended_remote_mourn_1 (ops);
7859 extended_remote_supports_disable_randomization (struct target_ops *self)
7861 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
7865 extended_remote_disable_randomization (int val)
7867 struct remote_state *rs = get_remote_state ();
7870 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7873 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7875 error (_("Target does not support QDisableRandomization."));
7876 if (strcmp (reply, "OK") != 0)
7877 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7881 extended_remote_run (char *args)
7883 struct remote_state *rs = get_remote_state ();
7886 /* If the user has disabled vRun support, or we have detected that
7887 support is not available, do not try it. */
7888 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
7891 strcpy (rs->buf, "vRun;");
7892 len = strlen (rs->buf);
7894 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7895 error (_("Remote file name too long for run packet"));
7896 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
7897 strlen (remote_exec_file));
7899 gdb_assert (args != NULL);
7902 struct cleanup *back_to;
7906 argv = gdb_buildargv (args);
7907 back_to = make_cleanup_freeargv (argv);
7908 for (i = 0; argv[i] != NULL; i++)
7910 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
7911 error (_("Argument list too long for run packet"));
7912 rs->buf[len++] = ';';
7913 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
7916 do_cleanups (back_to);
7919 rs->buf[len++] = '\0';
7922 getpkt (&rs->buf, &rs->buf_size, 0);
7924 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
7927 /* We have a wait response. All is well. */
7929 case PACKET_UNKNOWN:
7932 if (remote_exec_file[0] == '\0')
7933 error (_("Running the default executable on the remote target failed; "
7934 "try \"set remote exec-file\"?"));
7936 error (_("Running \"%s\" on the remote target failed"),
7939 gdb_assert_not_reached (_("bad switch"));
7943 /* In the extended protocol we want to be able to do things like
7944 "run" and have them basically work as expected. So we need
7945 a special create_inferior function. We support changing the
7946 executable file and the command line arguments, but not the
7950 extended_remote_create_inferior (struct target_ops *ops,
7951 char *exec_file, char *args,
7952 char **env, int from_tty)
7956 struct remote_state *rs = get_remote_state ();
7958 /* If running asynchronously, register the target file descriptor
7959 with the event loop. */
7960 if (target_can_async_p ())
7961 target_async (inferior_event_handler, 0);
7963 /* Disable address space randomization if requested (and supported). */
7964 if (extended_remote_supports_disable_randomization (ops))
7965 extended_remote_disable_randomization (disable_randomization);
7967 /* Now restart the remote server. */
7968 run_worked = extended_remote_run (args) != -1;
7971 /* vRun was not supported. Fail if we need it to do what the
7973 if (remote_exec_file[0])
7974 error (_("Remote target does not support \"set remote exec-file\""));
7976 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7978 /* Fall back to "R". */
7979 extended_remote_restart ();
7982 if (!have_inferiors ())
7984 /* Clean up from the last time we ran, before we mark the target
7985 running again. This will mark breakpoints uninserted, and
7986 get_offsets may insert breakpoints. */
7987 init_thread_list ();
7988 init_wait_for_inferior ();
7991 /* vRun's success return is a stop reply. */
7992 stop_reply = run_worked ? rs->buf : NULL;
7993 add_current_inferior_and_thread (stop_reply);
7995 /* Get updated offsets, if the stub uses qOffsets. */
8000 /* Given a location's target info BP_TGT and the packet buffer BUF, output
8001 the list of conditions (in agent expression bytecode format), if any, the
8002 target needs to evaluate. The output is placed into the packet buffer
8003 started from BUF and ended at BUF_END. */
8006 remote_add_target_side_condition (struct gdbarch *gdbarch,
8007 struct bp_target_info *bp_tgt, char *buf,
8010 struct agent_expr *aexpr = NULL;
8013 char *buf_start = buf;
8015 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8018 buf += strlen (buf);
8019 xsnprintf (buf, buf_end - buf, "%s", ";");
8022 /* Send conditions to the target and free the vector. */
8024 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8027 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
8028 buf += strlen (buf);
8029 for (i = 0; i < aexpr->len; ++i)
8030 buf = pack_hex_byte (buf, aexpr->buf[i]);
8037 remote_add_target_side_commands (struct gdbarch *gdbarch,
8038 struct bp_target_info *bp_tgt, char *buf)
8040 struct agent_expr *aexpr = NULL;
8043 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8046 buf += strlen (buf);
8048 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8049 buf += strlen (buf);
8051 /* Concatenate all the agent expressions that are commands into the
8054 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8057 sprintf (buf, "X%x,", aexpr->len);
8058 buf += strlen (buf);
8059 for (i = 0; i < aexpr->len; ++i)
8060 buf = pack_hex_byte (buf, aexpr->buf[i]);
8065 /* Insert a breakpoint. On targets that have software breakpoint
8066 support, we ask the remote target to do the work; on targets
8067 which don't, we insert a traditional memory breakpoint. */
8070 remote_insert_breakpoint (struct target_ops *ops,
8071 struct gdbarch *gdbarch,
8072 struct bp_target_info *bp_tgt)
8074 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8075 If it succeeds, then set the support to PACKET_ENABLE. If it
8076 fails, and the user has explicitly requested the Z support then
8077 report an error, otherwise, mark it disabled and go on. */
8079 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8081 CORE_ADDR addr = bp_tgt->placed_address;
8082 struct remote_state *rs;
8085 struct condition_list *cond = NULL;
8087 /* Make sure the remote is pointing at the right process, if
8089 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8090 set_general_process ();
8092 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8094 rs = get_remote_state ();
8096 endbuf = rs->buf + get_remote_packet_size ();
8101 addr = (ULONGEST) remote_address_masked (addr);
8102 p += hexnumstr (p, addr);
8103 xsnprintf (p, endbuf - p, ",%d", bpsize);
8105 if (remote_supports_cond_breakpoints (ops))
8106 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8108 if (remote_can_run_breakpoint_commands (ops))
8109 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8112 getpkt (&rs->buf, &rs->buf_size, 0);
8114 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
8119 bp_tgt->placed_address = addr;
8120 bp_tgt->placed_size = bpsize;
8122 case PACKET_UNKNOWN:
8127 /* If this breakpoint has target-side commands but this stub doesn't
8128 support Z0 packets, throw error. */
8129 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8130 throw_error (NOT_SUPPORTED_ERROR, _("\
8131 Target doesn't support breakpoints that have target side commands."));
8133 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
8137 remote_remove_breakpoint (struct target_ops *ops,
8138 struct gdbarch *gdbarch,
8139 struct bp_target_info *bp_tgt)
8141 CORE_ADDR addr = bp_tgt->placed_address;
8142 struct remote_state *rs = get_remote_state ();
8144 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8147 char *endbuf = rs->buf + get_remote_packet_size ();
8149 /* Make sure the remote is pointing at the right process, if
8151 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8152 set_general_process ();
8158 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8159 p += hexnumstr (p, addr);
8160 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
8163 getpkt (&rs->buf, &rs->buf_size, 0);
8165 return (rs->buf[0] == 'E');
8168 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
8172 watchpoint_to_Z_packet (int type)
8177 return Z_PACKET_WRITE_WP;
8180 return Z_PACKET_READ_WP;
8183 return Z_PACKET_ACCESS_WP;
8186 internal_error (__FILE__, __LINE__,
8187 _("hw_bp_to_z: bad watchpoint type %d"), type);
8192 remote_insert_watchpoint (struct target_ops *self,
8193 CORE_ADDR addr, int len, int type,
8194 struct expression *cond)
8196 struct remote_state *rs = get_remote_state ();
8197 char *endbuf = rs->buf + get_remote_packet_size ();
8199 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8201 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8204 /* Make sure the remote is pointing at the right process, if
8206 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8207 set_general_process ();
8209 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
8210 p = strchr (rs->buf, '\0');
8211 addr = remote_address_masked (addr);
8212 p += hexnumstr (p, (ULONGEST) addr);
8213 xsnprintf (p, endbuf - p, ",%x", len);
8216 getpkt (&rs->buf, &rs->buf_size, 0);
8218 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8222 case PACKET_UNKNOWN:
8227 internal_error (__FILE__, __LINE__,
8228 _("remote_insert_watchpoint: reached end of function"));
8232 remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8233 CORE_ADDR start, int length)
8235 CORE_ADDR diff = remote_address_masked (addr - start);
8237 return diff < length;
8242 remote_remove_watchpoint (struct target_ops *self,
8243 CORE_ADDR addr, int len, int type,
8244 struct expression *cond)
8246 struct remote_state *rs = get_remote_state ();
8247 char *endbuf = rs->buf + get_remote_packet_size ();
8249 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8251 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8254 /* Make sure the remote is pointing at the right process, if
8256 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8257 set_general_process ();
8259 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
8260 p = strchr (rs->buf, '\0');
8261 addr = remote_address_masked (addr);
8262 p += hexnumstr (p, (ULONGEST) addr);
8263 xsnprintf (p, endbuf - p, ",%x", len);
8265 getpkt (&rs->buf, &rs->buf_size, 0);
8267 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8270 case PACKET_UNKNOWN:
8275 internal_error (__FILE__, __LINE__,
8276 _("remote_remove_watchpoint: reached end of function"));
8280 int remote_hw_watchpoint_limit = -1;
8281 int remote_hw_watchpoint_length_limit = -1;
8282 int remote_hw_breakpoint_limit = -1;
8285 remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8286 CORE_ADDR addr, int len)
8288 if (remote_hw_watchpoint_length_limit == 0)
8290 else if (remote_hw_watchpoint_length_limit < 0)
8292 else if (len <= remote_hw_watchpoint_length_limit)
8299 remote_check_watch_resources (struct target_ops *self,
8300 int type, int cnt, int ot)
8302 if (type == bp_hardware_breakpoint)
8304 if (remote_hw_breakpoint_limit == 0)
8306 else if (remote_hw_breakpoint_limit < 0)
8308 else if (cnt <= remote_hw_breakpoint_limit)
8313 if (remote_hw_watchpoint_limit == 0)
8315 else if (remote_hw_watchpoint_limit < 0)
8319 else if (cnt <= remote_hw_watchpoint_limit)
8326 remote_stopped_by_watchpoint (struct target_ops *ops)
8328 struct remote_state *rs = get_remote_state ();
8330 return rs->remote_stopped_by_watchpoint_p;
8334 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
8336 struct remote_state *rs = get_remote_state ();
8339 if (remote_stopped_by_watchpoint (target))
8341 *addr_p = rs->remote_watch_data_address;
8350 remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8351 struct bp_target_info *bp_tgt)
8354 struct remote_state *rs;
8358 /* The length field should be set to the size of a breakpoint
8359 instruction, even though we aren't inserting one ourselves. */
8361 gdbarch_remote_breakpoint_from_pc
8362 (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
8364 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8367 /* Make sure the remote is pointing at the right process, if
8369 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8370 set_general_process ();
8372 rs = get_remote_state ();
8374 endbuf = rs->buf + get_remote_packet_size ();
8380 addr = remote_address_masked (bp_tgt->placed_address);
8381 p += hexnumstr (p, (ULONGEST) addr);
8382 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8384 if (remote_supports_cond_breakpoints (self))
8385 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8387 if (remote_can_run_breakpoint_commands (self))
8388 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8391 getpkt (&rs->buf, &rs->buf_size, 0);
8393 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8396 if (rs->buf[1] == '.')
8398 message = strchr (rs->buf + 2, '.');
8400 error (_("Remote failure reply: %s"), message + 1);
8403 case PACKET_UNKNOWN:
8408 internal_error (__FILE__, __LINE__,
8409 _("remote_insert_hw_breakpoint: reached end of function"));
8414 remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8415 struct bp_target_info *bp_tgt)
8418 struct remote_state *rs = get_remote_state ();
8420 char *endbuf = rs->buf + get_remote_packet_size ();
8422 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8425 /* Make sure the remote is pointing at the right process, if
8427 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8428 set_general_process ();
8434 addr = remote_address_masked (bp_tgt->placed_address);
8435 p += hexnumstr (p, (ULONGEST) addr);
8436 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8439 getpkt (&rs->buf, &rs->buf_size, 0);
8441 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8444 case PACKET_UNKNOWN:
8449 internal_error (__FILE__, __LINE__,
8450 _("remote_remove_hw_breakpoint: reached end of function"));
8453 /* Verify memory using the "qCRC:" request. */
8456 remote_verify_memory (struct target_ops *ops,
8457 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8459 struct remote_state *rs = get_remote_state ();
8460 unsigned long host_crc, target_crc;
8463 /* It doesn't make sense to use qCRC if the remote target is
8464 connected but not running. */
8465 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
8467 enum packet_result result;
8469 /* Make sure the remote is pointing at the right process. */
8470 set_general_process ();
8472 /* FIXME: assumes lma can fit into long. */
8473 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8474 (long) lma, (long) size);
8477 /* Be clever; compute the host_crc before waiting for target
8479 host_crc = xcrc32 (data, size, 0xffffffff);
8481 getpkt (&rs->buf, &rs->buf_size, 0);
8483 result = packet_ok (rs->buf,
8484 &remote_protocol_packets[PACKET_qCRC]);
8485 if (result == PACKET_ERROR)
8487 else if (result == PACKET_OK)
8489 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8490 target_crc = target_crc * 16 + fromhex (*tmp);
8492 return (host_crc == target_crc);
8496 return simple_verify_memory (ops, data, lma, size);
8499 /* compare-sections command
8501 With no arguments, compares each loadable section in the exec bfd
8502 with the same memory range on the target, and reports mismatches.
8503 Useful for verifying the image on the target against the exec file. */
8506 compare_sections_command (char *args, int from_tty)
8509 struct cleanup *old_chain;
8511 const char *sectname;
8520 error (_("command cannot be used without an exec file"));
8522 /* Make sure the remote is pointing at the right process. */
8523 set_general_process ();
8525 if (args != NULL && strcmp (args, "-r") == 0)
8531 for (s = exec_bfd->sections; s; s = s->next)
8533 if (!(s->flags & SEC_LOAD))
8534 continue; /* Skip non-loadable section. */
8536 if (read_only && (s->flags & SEC_READONLY) == 0)
8537 continue; /* Skip writeable sections */
8539 size = bfd_get_section_size (s);
8541 continue; /* Skip zero-length section. */
8543 sectname = bfd_get_section_name (exec_bfd, s);
8544 if (args && strcmp (args, sectname) != 0)
8545 continue; /* Not the section selected by user. */
8547 matched = 1; /* Do this section. */
8550 sectdata = xmalloc (size);
8551 old_chain = make_cleanup (xfree, sectdata);
8552 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
8554 res = target_verify_memory (sectdata, lma, size);
8557 error (_("target memory fault, section %s, range %s -- %s"), sectname,
8558 paddress (target_gdbarch (), lma),
8559 paddress (target_gdbarch (), lma + size));
8561 printf_filtered ("Section %s, range %s -- %s: ", sectname,
8562 paddress (target_gdbarch (), lma),
8563 paddress (target_gdbarch (), lma + size));
8565 printf_filtered ("matched.\n");
8568 printf_filtered ("MIS-MATCHED!\n");
8572 do_cleanups (old_chain);
8575 warning (_("One or more sections of the target image does not match\n\
8576 the loaded file\n"));
8577 if (args && !matched)
8578 printf_filtered (_("No loaded section named '%s'.\n"), args);
8581 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8582 into remote target. The number of bytes written to the remote
8583 target is returned, or -1 for error. */
8585 static enum target_xfer_status
8586 remote_write_qxfer (struct target_ops *ops, const char *object_name,
8587 const char *annex, const gdb_byte *writebuf,
8588 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
8589 struct packet_config *packet)
8593 struct remote_state *rs = get_remote_state ();
8594 int max_size = get_memory_write_packet_size ();
8596 if (packet->support == PACKET_DISABLE)
8597 return TARGET_XFER_E_IO;
8599 /* Insert header. */
8600 i = snprintf (rs->buf, max_size,
8601 "qXfer:%s:write:%s:%s:",
8602 object_name, annex ? annex : "",
8603 phex_nz (offset, sizeof offset));
8604 max_size -= (i + 1);
8606 /* Escape as much data as fits into rs->buf. */
8607 buf_len = remote_escape_output
8608 (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size);
8610 if (putpkt_binary (rs->buf, i + buf_len) < 0
8611 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8612 || packet_ok (rs->buf, packet) != PACKET_OK)
8613 return TARGET_XFER_E_IO;
8615 unpack_varlen_hex (rs->buf, &n);
8618 return TARGET_XFER_OK;
8621 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8622 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8623 number of bytes read is returned, or 0 for EOF, or -1 for error.
8624 The number of bytes read may be less than LEN without indicating an
8625 EOF. PACKET is checked and updated to indicate whether the remote
8626 target supports this object. */
8628 static enum target_xfer_status
8629 remote_read_qxfer (struct target_ops *ops, const char *object_name,
8631 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8632 ULONGEST *xfered_len,
8633 struct packet_config *packet)
8635 struct remote_state *rs = get_remote_state ();
8636 LONGEST i, n, packet_len;
8638 if (packet->support == PACKET_DISABLE)
8639 return TARGET_XFER_E_IO;
8641 /* Check whether we've cached an end-of-object packet that matches
8643 if (rs->finished_object)
8645 if (strcmp (object_name, rs->finished_object) == 0
8646 && strcmp (annex ? annex : "", rs->finished_annex) == 0
8647 && offset == rs->finished_offset)
8648 return TARGET_XFER_EOF;
8651 /* Otherwise, we're now reading something different. Discard
8653 xfree (rs->finished_object);
8654 xfree (rs->finished_annex);
8655 rs->finished_object = NULL;
8656 rs->finished_annex = NULL;
8659 /* Request only enough to fit in a single packet. The actual data
8660 may not, since we don't know how much of it will need to be escaped;
8661 the target is free to respond with slightly less data. We subtract
8662 five to account for the response type and the protocol frame. */
8663 n = min (get_remote_packet_size () - 5, len);
8664 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8665 object_name, annex ? annex : "",
8666 phex_nz (offset, sizeof offset),
8667 phex_nz (n, sizeof n));
8668 i = putpkt (rs->buf);
8670 return TARGET_XFER_E_IO;
8673 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8674 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8675 return TARGET_XFER_E_IO;
8677 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8678 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8680 /* 'm' means there is (or at least might be) more data after this
8681 batch. That does not make sense unless there's at least one byte
8682 of data in this reply. */
8683 if (rs->buf[0] == 'm' && packet_len == 1)
8684 error (_("Remote qXfer reply contained no data."));
8686 /* Got some data. */
8687 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
8688 packet_len - 1, readbuf, n);
8690 /* 'l' is an EOF marker, possibly including a final block of data,
8691 or possibly empty. If we have the final block of a non-empty
8692 object, record this fact to bypass a subsequent partial read. */
8693 if (rs->buf[0] == 'l' && offset + i > 0)
8695 rs->finished_object = xstrdup (object_name);
8696 rs->finished_annex = xstrdup (annex ? annex : "");
8697 rs->finished_offset = offset + i;
8701 return TARGET_XFER_EOF;
8705 return TARGET_XFER_OK;
8709 static enum target_xfer_status
8710 remote_xfer_partial (struct target_ops *ops, enum target_object object,
8711 const char *annex, gdb_byte *readbuf,
8712 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
8713 ULONGEST *xfered_len)
8715 struct remote_state *rs;
8720 set_remote_traceframe ();
8721 set_general_thread (inferior_ptid);
8723 rs = get_remote_state ();
8725 /* Handle memory using the standard memory routines. */
8726 if (object == TARGET_OBJECT_MEMORY)
8728 /* If the remote target is connected but not running, we should
8729 pass this request down to a lower stratum (e.g. the executable
8731 if (!target_has_execution)
8732 return TARGET_XFER_EOF;
8734 if (writebuf != NULL)
8735 return remote_write_bytes (offset, writebuf, len, xfered_len);
8737 return remote_read_bytes (ops, offset, readbuf, len, xfered_len);
8740 /* Handle SPU memory using qxfer packets. */
8741 if (object == TARGET_OBJECT_SPU)
8744 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8745 xfered_len, &remote_protocol_packets
8746 [PACKET_qXfer_spu_read]);
8748 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8749 xfered_len, &remote_protocol_packets
8750 [PACKET_qXfer_spu_write]);
8753 /* Handle extra signal info using qxfer packets. */
8754 if (object == TARGET_OBJECT_SIGNAL_INFO)
8757 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8758 xfered_len, &remote_protocol_packets
8759 [PACKET_qXfer_siginfo_read]);
8761 return remote_write_qxfer (ops, "siginfo", annex,
8762 writebuf, offset, len, xfered_len,
8763 &remote_protocol_packets
8764 [PACKET_qXfer_siginfo_write]);
8767 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8770 return remote_read_qxfer (ops, "statictrace", annex,
8771 readbuf, offset, len, xfered_len,
8772 &remote_protocol_packets
8773 [PACKET_qXfer_statictrace_read]);
8775 return TARGET_XFER_E_IO;
8778 /* Only handle flash writes. */
8779 if (writebuf != NULL)
8785 case TARGET_OBJECT_FLASH:
8786 return remote_flash_write (ops, offset, len, xfered_len,
8790 return TARGET_XFER_E_IO;
8794 /* Map pre-existing objects onto letters. DO NOT do this for new
8795 objects!!! Instead specify new query packets. */
8798 case TARGET_OBJECT_AVR:
8802 case TARGET_OBJECT_AUXV:
8803 gdb_assert (annex == NULL);
8804 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8806 &remote_protocol_packets[PACKET_qXfer_auxv]);
8808 case TARGET_OBJECT_AVAILABLE_FEATURES:
8809 return remote_read_qxfer
8810 (ops, "features", annex, readbuf, offset, len, xfered_len,
8811 &remote_protocol_packets[PACKET_qXfer_features]);
8813 case TARGET_OBJECT_LIBRARIES:
8814 return remote_read_qxfer
8815 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
8816 &remote_protocol_packets[PACKET_qXfer_libraries]);
8818 case TARGET_OBJECT_LIBRARIES_SVR4:
8819 return remote_read_qxfer
8820 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
8821 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8823 case TARGET_OBJECT_MEMORY_MAP:
8824 gdb_assert (annex == NULL);
8825 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8827 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8829 case TARGET_OBJECT_OSDATA:
8830 /* Should only get here if we're connected. */
8831 gdb_assert (rs->remote_desc);
8832 return remote_read_qxfer
8833 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
8834 &remote_protocol_packets[PACKET_qXfer_osdata]);
8836 case TARGET_OBJECT_THREADS:
8837 gdb_assert (annex == NULL);
8838 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8840 &remote_protocol_packets[PACKET_qXfer_threads]);
8842 case TARGET_OBJECT_TRACEFRAME_INFO:
8843 gdb_assert (annex == NULL);
8844 return remote_read_qxfer
8845 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
8846 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8848 case TARGET_OBJECT_FDPIC:
8849 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8851 &remote_protocol_packets[PACKET_qXfer_fdpic]);
8853 case TARGET_OBJECT_OPENVMS_UIB:
8854 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8856 &remote_protocol_packets[PACKET_qXfer_uib]);
8858 case TARGET_OBJECT_BTRACE:
8859 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
8861 &remote_protocol_packets[PACKET_qXfer_btrace]);
8864 return TARGET_XFER_E_IO;
8867 /* Note: a zero OFFSET and LEN can be used to query the minimum
8869 if (offset == 0 && len == 0)
8870 return (get_remote_packet_size ());
8871 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8872 large enough let the caller deal with it. */
8873 if (len < get_remote_packet_size ())
8874 return TARGET_XFER_E_IO;
8875 len = get_remote_packet_size ();
8877 /* Except for querying the minimum buffer size, target must be open. */
8878 if (!rs->remote_desc)
8879 error (_("remote query is only available after target open"));
8881 gdb_assert (annex != NULL);
8882 gdb_assert (readbuf != NULL);
8888 /* We used one buffer char for the remote protocol q command and
8889 another for the query type. As the remote protocol encapsulation
8890 uses 4 chars plus one extra in case we are debugging
8891 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8894 while (annex[i] && (i < (get_remote_packet_size () - 8)))
8896 /* Bad caller may have sent forbidden characters. */
8897 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
8902 gdb_assert (annex[i] == '\0');
8904 i = putpkt (rs->buf);
8906 return TARGET_XFER_E_IO;
8908 getpkt (&rs->buf, &rs->buf_size, 0);
8909 strcpy ((char *) readbuf, rs->buf);
8911 *xfered_len = strlen ((char *) readbuf);
8912 return TARGET_XFER_OK;
8916 remote_search_memory (struct target_ops* ops,
8917 CORE_ADDR start_addr, ULONGEST search_space_len,
8918 const gdb_byte *pattern, ULONGEST pattern_len,
8919 CORE_ADDR *found_addrp)
8921 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
8922 struct remote_state *rs = get_remote_state ();
8923 int max_size = get_memory_write_packet_size ();
8924 struct packet_config *packet =
8925 &remote_protocol_packets[PACKET_qSearch_memory];
8926 /* Number of packet bytes used to encode the pattern;
8927 this could be more than PATTERN_LEN due to escape characters. */
8928 int escaped_pattern_len;
8929 /* Amount of pattern that was encodable in the packet. */
8930 int used_pattern_len;
8933 ULONGEST found_addr;
8935 /* Don't go to the target if we don't have to.
8936 This is done before checking packet->support to avoid the possibility that
8937 a success for this edge case means the facility works in general. */
8938 if (pattern_len > search_space_len)
8940 if (pattern_len == 0)
8942 *found_addrp = start_addr;
8946 /* If we already know the packet isn't supported, fall back to the simple
8947 way of searching memory. */
8949 if (packet_config_support (packet) == PACKET_DISABLE)
8951 /* Target doesn't provided special support, fall back and use the
8952 standard support (copy memory and do the search here). */
8953 return simple_search_memory (ops, start_addr, search_space_len,
8954 pattern, pattern_len, found_addrp);
8957 /* Make sure the remote is pointing at the right process. */
8958 set_general_process ();
8960 /* Insert header. */
8961 i = snprintf (rs->buf, max_size,
8962 "qSearch:memory:%s;%s;",
8963 phex_nz (start_addr, addr_size),
8964 phex_nz (search_space_len, sizeof (search_space_len)));
8965 max_size -= (i + 1);
8967 /* Escape as much data as fits into rs->buf. */
8968 escaped_pattern_len =
8969 remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i,
8970 &used_pattern_len, max_size);
8972 /* Bail if the pattern is too large. */
8973 if (used_pattern_len != pattern_len)
8974 error (_("Pattern is too large to transmit to remote target."));
8976 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
8977 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8978 || packet_ok (rs->buf, packet) != PACKET_OK)
8980 /* The request may not have worked because the command is not
8981 supported. If so, fall back to the simple way. */
8982 if (packet->support == PACKET_DISABLE)
8984 return simple_search_memory (ops, start_addr, search_space_len,
8985 pattern, pattern_len, found_addrp);
8990 if (rs->buf[0] == '0')
8992 else if (rs->buf[0] == '1')
8995 if (rs->buf[1] != ',')
8996 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8997 unpack_varlen_hex (rs->buf + 2, &found_addr);
8998 *found_addrp = found_addr;
9001 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9007 remote_rcmd (struct target_ops *self, const char *command,
9008 struct ui_file *outbuf)
9010 struct remote_state *rs = get_remote_state ();
9013 if (!rs->remote_desc)
9014 error (_("remote rcmd is only available after target open"));
9016 /* Send a NULL command across as an empty command. */
9017 if (command == NULL)
9020 /* The query prefix. */
9021 strcpy (rs->buf, "qRcmd,");
9022 p = strchr (rs->buf, '\0');
9024 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9025 > get_remote_packet_size ())
9026 error (_("\"monitor\" command ``%s'' is too long."), command);
9028 /* Encode the actual command. */
9029 bin2hex ((const gdb_byte *) command, p, strlen (command));
9031 if (putpkt (rs->buf) < 0)
9032 error (_("Communication problem with target."));
9034 /* get/display the response */
9039 /* XXX - see also remote_get_noisy_reply(). */
9040 QUIT; /* Allow user to bail out with ^C. */
9042 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9044 /* Timeout. Continue to (try to) read responses.
9045 This is better than stopping with an error, assuming the stub
9046 is still executing the (long) monitor command.
9047 If needed, the user can interrupt gdb using C-c, obtaining
9048 an effect similar to stop on timeout. */
9053 error (_("Target does not support this command."));
9054 if (buf[0] == 'O' && buf[1] != 'K')
9056 remote_console_output (buf + 1); /* 'O' message from stub. */
9059 if (strcmp (buf, "OK") == 0)
9061 if (strlen (buf) == 3 && buf[0] == 'E'
9062 && isdigit (buf[1]) && isdigit (buf[2]))
9064 error (_("Protocol error with Rcmd"));
9066 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9068 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
9070 fputc_unfiltered (c, outbuf);
9076 static VEC(mem_region_s) *
9077 remote_memory_map (struct target_ops *ops)
9079 VEC(mem_region_s) *result = NULL;
9080 char *text = target_read_stralloc (¤t_target,
9081 TARGET_OBJECT_MEMORY_MAP, NULL);
9085 struct cleanup *back_to = make_cleanup (xfree, text);
9087 result = parse_memory_map (text);
9088 do_cleanups (back_to);
9095 packet_command (char *args, int from_tty)
9097 struct remote_state *rs = get_remote_state ();
9099 if (!rs->remote_desc)
9100 error (_("command can only be used with remote target"));
9103 error (_("remote-packet command requires packet text as argument"));
9105 puts_filtered ("sending: ");
9106 print_packet (args);
9107 puts_filtered ("\n");
9110 getpkt (&rs->buf, &rs->buf_size, 0);
9111 puts_filtered ("received: ");
9112 print_packet (rs->buf);
9113 puts_filtered ("\n");
9117 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9119 static void display_thread_info (struct gdb_ext_thread_info *info);
9121 static void threadset_test_cmd (char *cmd, int tty);
9123 static void threadalive_test (char *cmd, int tty);
9125 static void threadlist_test_cmd (char *cmd, int tty);
9127 int get_and_display_threadinfo (threadref *ref);
9129 static void threadinfo_test_cmd (char *cmd, int tty);
9131 static int thread_display_step (threadref *ref, void *context);
9133 static void threadlist_update_test_cmd (char *cmd, int tty);
9135 static void init_remote_threadtests (void);
9137 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9140 threadset_test_cmd (char *cmd, int tty)
9142 int sample_thread = SAMPLE_THREAD;
9144 printf_filtered (_("Remote threadset test\n"));
9145 set_general_thread (sample_thread);
9150 threadalive_test (char *cmd, int tty)
9152 int sample_thread = SAMPLE_THREAD;
9153 int pid = ptid_get_pid (inferior_ptid);
9154 ptid_t ptid = ptid_build (pid, sample_thread, 0);
9156 if (remote_thread_alive (ptid))
9157 printf_filtered ("PASS: Thread alive test\n");
9159 printf_filtered ("FAIL: Thread alive test\n");
9162 void output_threadid (char *title, threadref *ref);
9165 output_threadid (char *title, threadref *ref)
9169 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
9171 printf_filtered ("%s %s\n", title, (&hexid[0]));
9175 threadlist_test_cmd (char *cmd, int tty)
9178 threadref nextthread;
9179 int done, result_count;
9180 threadref threadlist[3];
9182 printf_filtered ("Remote Threadlist test\n");
9183 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9184 &result_count, &threadlist[0]))
9185 printf_filtered ("FAIL: threadlist test\n");
9188 threadref *scan = threadlist;
9189 threadref *limit = scan + result_count;
9191 while (scan < limit)
9192 output_threadid (" thread ", scan++);
9197 display_thread_info (struct gdb_ext_thread_info *info)
9199 output_threadid ("Threadid: ", &info->threadid);
9200 printf_filtered ("Name: %s\n ", info->shortname);
9201 printf_filtered ("State: %s\n", info->display);
9202 printf_filtered ("other: %s\n\n", info->more_display);
9206 get_and_display_threadinfo (threadref *ref)
9210 struct gdb_ext_thread_info threadinfo;
9212 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9213 | TAG_MOREDISPLAY | TAG_DISPLAY;
9214 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9215 display_thread_info (&threadinfo);
9220 threadinfo_test_cmd (char *cmd, int tty)
9222 int athread = SAMPLE_THREAD;
9226 int_to_threadref (&thread, athread);
9227 printf_filtered ("Remote Threadinfo test\n");
9228 if (!get_and_display_threadinfo (&thread))
9229 printf_filtered ("FAIL cannot get thread info\n");
9233 thread_display_step (threadref *ref, void *context)
9235 /* output_threadid(" threadstep ",ref); *//* simple test */
9236 return get_and_display_threadinfo (ref);
9240 threadlist_update_test_cmd (char *cmd, int tty)
9242 printf_filtered ("Remote Threadlist update test\n");
9243 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9247 init_remote_threadtests (void)
9249 add_com ("tlist", class_obscure, threadlist_test_cmd,
9250 _("Fetch and print the remote list of "
9251 "thread identifiers, one pkt only"));
9252 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
9253 _("Fetch and display info about one thread"));
9254 add_com ("tset", class_obscure, threadset_test_cmd,
9255 _("Test setting to a different thread"));
9256 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
9257 _("Iterate through updating all remote thread info"));
9258 add_com ("talive", class_obscure, threadalive_test,
9259 _(" Remote thread alive test "));
9264 /* Convert a thread ID to a string. Returns the string in a static
9268 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
9270 static char buf[64];
9271 struct remote_state *rs = get_remote_state ();
9273 if (ptid_equal (ptid, null_ptid))
9274 return normal_pid_to_str (ptid);
9275 else if (ptid_is_pid (ptid))
9277 /* Printing an inferior target id. */
9279 /* When multi-process extensions are off, there's no way in the
9280 remote protocol to know the remote process id, if there's any
9281 at all. There's one exception --- when we're connected with
9282 target extended-remote, and we manually attached to a process
9283 with "attach PID". We don't record anywhere a flag that
9284 allows us to distinguish that case from the case of
9285 connecting with extended-remote and the stub already being
9286 attached to a process, and reporting yes to qAttached, hence
9287 no smart special casing here. */
9288 if (!remote_multi_process_p (rs))
9290 xsnprintf (buf, sizeof buf, "Remote target");
9294 return normal_pid_to_str (ptid);
9298 if (ptid_equal (magic_null_ptid, ptid))
9299 xsnprintf (buf, sizeof buf, "Thread <main>");
9300 else if (rs->extended && remote_multi_process_p (rs))
9301 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9302 ptid_get_pid (ptid), ptid_get_lwp (ptid));
9304 xsnprintf (buf, sizeof buf, "Thread %ld",
9305 ptid_get_lwp (ptid));
9310 /* Get the address of the thread local variable in OBJFILE which is
9311 stored at OFFSET within the thread local storage for thread PTID. */
9314 remote_get_thread_local_address (struct target_ops *ops,
9315 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
9317 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
9319 struct remote_state *rs = get_remote_state ();
9321 char *endp = rs->buf + get_remote_packet_size ();
9322 enum packet_result result;
9324 strcpy (p, "qGetTLSAddr:");
9326 p = write_ptid (p, endp, ptid);
9328 p += hexnumstr (p, offset);
9330 p += hexnumstr (p, lm);
9334 getpkt (&rs->buf, &rs->buf_size, 0);
9335 result = packet_ok (rs->buf,
9336 &remote_protocol_packets[PACKET_qGetTLSAddr]);
9337 if (result == PACKET_OK)
9341 unpack_varlen_hex (rs->buf, &result);
9344 else if (result == PACKET_UNKNOWN)
9345 throw_error (TLS_GENERIC_ERROR,
9346 _("Remote target doesn't support qGetTLSAddr packet"));
9348 throw_error (TLS_GENERIC_ERROR,
9349 _("Remote target failed to process qGetTLSAddr request"));
9352 throw_error (TLS_GENERIC_ERROR,
9353 _("TLS not supported or disabled on this target"));
9358 /* Provide thread local base, i.e. Thread Information Block address.
9359 Returns 1 if ptid is found and thread_local_base is non zero. */
9362 remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
9364 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
9366 struct remote_state *rs = get_remote_state ();
9368 char *endp = rs->buf + get_remote_packet_size ();
9369 enum packet_result result;
9371 strcpy (p, "qGetTIBAddr:");
9373 p = write_ptid (p, endp, ptid);
9377 getpkt (&rs->buf, &rs->buf_size, 0);
9378 result = packet_ok (rs->buf,
9379 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9380 if (result == PACKET_OK)
9384 unpack_varlen_hex (rs->buf, &result);
9386 *addr = (CORE_ADDR) result;
9389 else if (result == PACKET_UNKNOWN)
9390 error (_("Remote target doesn't support qGetTIBAddr packet"));
9392 error (_("Remote target failed to process qGetTIBAddr request"));
9395 error (_("qGetTIBAddr not supported or disabled on this target"));
9400 /* Support for inferring a target description based on the current
9401 architecture and the size of a 'g' packet. While the 'g' packet
9402 can have any size (since optional registers can be left off the
9403 end), some sizes are easily recognizable given knowledge of the
9404 approximate architecture. */
9406 struct remote_g_packet_guess
9409 const struct target_desc *tdesc;
9411 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9412 DEF_VEC_O(remote_g_packet_guess_s);
9414 struct remote_g_packet_data
9416 VEC(remote_g_packet_guess_s) *guesses;
9419 static struct gdbarch_data *remote_g_packet_data_handle;
9422 remote_g_packet_data_init (struct obstack *obstack)
9424 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9428 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9429 const struct target_desc *tdesc)
9431 struct remote_g_packet_data *data
9432 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9433 struct remote_g_packet_guess new_guess, *guess;
9436 gdb_assert (tdesc != NULL);
9439 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9441 if (guess->bytes == bytes)
9442 internal_error (__FILE__, __LINE__,
9443 _("Duplicate g packet description added for size %d"),
9446 new_guess.bytes = bytes;
9447 new_guess.tdesc = tdesc;
9448 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9451 /* Return 1 if remote_read_description would do anything on this target
9452 and architecture, 0 otherwise. */
9455 remote_read_description_p (struct target_ops *target)
9457 struct remote_g_packet_data *data
9458 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9460 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9466 static const struct target_desc *
9467 remote_read_description (struct target_ops *target)
9469 struct remote_g_packet_data *data
9470 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
9472 /* Do not try this during initial connection, when we do not know
9473 whether there is a running but stopped thread. */
9474 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
9475 return target->beneath->to_read_description (target->beneath);
9477 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9479 struct remote_g_packet_guess *guess;
9481 int bytes = send_g_packet ();
9484 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9486 if (guess->bytes == bytes)
9487 return guess->tdesc;
9489 /* We discard the g packet. A minor optimization would be to
9490 hold on to it, and fill the register cache once we have selected
9491 an architecture, but it's too tricky to do safely. */
9494 return target->beneath->to_read_description (target->beneath);
9497 /* Remote file transfer support. This is host-initiated I/O, not
9498 target-initiated; for target-initiated, see remote-fileio.c. */
9500 /* If *LEFT is at least the length of STRING, copy STRING to
9501 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9502 decrease *LEFT. Otherwise raise an error. */
9505 remote_buffer_add_string (char **buffer, int *left, char *string)
9507 int len = strlen (string);
9510 error (_("Packet too long for target."));
9512 memcpy (*buffer, string, len);
9516 /* NUL-terminate the buffer as a convenience, if there is
9522 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9523 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9524 decrease *LEFT. Otherwise raise an error. */
9527 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9530 if (2 * len > *left)
9531 error (_("Packet too long for target."));
9533 bin2hex (bytes, *buffer, len);
9537 /* NUL-terminate the buffer as a convenience, if there is
9543 /* If *LEFT is large enough, convert VALUE to hex and add it to
9544 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9545 decrease *LEFT. Otherwise raise an error. */
9548 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9550 int len = hexnumlen (value);
9553 error (_("Packet too long for target."));
9555 hexnumstr (*buffer, value);
9559 /* NUL-terminate the buffer as a convenience, if there is
9565 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9566 value, *REMOTE_ERRNO to the remote error number or zero if none
9567 was included, and *ATTACHMENT to point to the start of the annex
9568 if any. The length of the packet isn't needed here; there may
9569 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9571 Return 0 if the packet could be parsed, -1 if it could not. If
9572 -1 is returned, the other variables may not be initialized. */
9575 remote_hostio_parse_result (char *buffer, int *retcode,
9576 int *remote_errno, char **attachment)
9583 if (buffer[0] != 'F')
9587 *retcode = strtol (&buffer[1], &p, 16);
9588 if (errno != 0 || p == &buffer[1])
9591 /* Check for ",errno". */
9595 *remote_errno = strtol (p + 1, &p2, 16);
9596 if (errno != 0 || p + 1 == p2)
9601 /* Check for ";attachment". If there is no attachment, the
9602 packet should end here. */
9605 *attachment = p + 1;
9608 else if (*p == '\0')
9614 /* Send a prepared I/O packet to the target and read its response.
9615 The prepared packet is in the global RS->BUF before this function
9616 is called, and the answer is there when we return.
9618 COMMAND_BYTES is the length of the request to send, which may include
9619 binary data. WHICH_PACKET is the packet configuration to check
9620 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9621 is set to the error number and -1 is returned. Otherwise the value
9622 returned by the function is returned.
9624 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9625 attachment is expected; an error will be reported if there's a
9626 mismatch. If one is found, *ATTACHMENT will be set to point into
9627 the packet buffer and *ATTACHMENT_LEN will be set to the
9628 attachment's length. */
9631 remote_hostio_send_command (int command_bytes, int which_packet,
9632 int *remote_errno, char **attachment,
9633 int *attachment_len)
9635 struct remote_state *rs = get_remote_state ();
9636 int ret, bytes_read;
9637 char *attachment_tmp;
9639 if (!rs->remote_desc
9640 || packet_support (which_packet) == PACKET_DISABLE)
9642 *remote_errno = FILEIO_ENOSYS;
9646 putpkt_binary (rs->buf, command_bytes);
9647 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9649 /* If it timed out, something is wrong. Don't try to parse the
9653 *remote_errno = FILEIO_EINVAL;
9657 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9660 *remote_errno = FILEIO_EINVAL;
9662 case PACKET_UNKNOWN:
9663 *remote_errno = FILEIO_ENOSYS;
9669 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9672 *remote_errno = FILEIO_EINVAL;
9676 /* Make sure we saw an attachment if and only if we expected one. */
9677 if ((attachment_tmp == NULL && attachment != NULL)
9678 || (attachment_tmp != NULL && attachment == NULL))
9680 *remote_errno = FILEIO_EINVAL;
9684 /* If an attachment was found, it must point into the packet buffer;
9685 work out how many bytes there were. */
9686 if (attachment_tmp != NULL)
9688 *attachment = attachment_tmp;
9689 *attachment_len = bytes_read - (*attachment - rs->buf);
9695 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9696 remote file descriptor, or -1 if an error occurs (and set
9700 remote_hostio_open (struct target_ops *self,
9701 const char *filename, int flags, int mode,
9704 struct remote_state *rs = get_remote_state ();
9706 int left = get_remote_packet_size () - 1;
9708 remote_buffer_add_string (&p, &left, "vFile:open:");
9710 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9712 remote_buffer_add_string (&p, &left, ",");
9714 remote_buffer_add_int (&p, &left, flags);
9715 remote_buffer_add_string (&p, &left, ",");
9717 remote_buffer_add_int (&p, &left, mode);
9719 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9720 remote_errno, NULL, NULL);
9723 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9724 Return the number of bytes written, or -1 if an error occurs (and
9725 set *REMOTE_ERRNO). */
9728 remote_hostio_pwrite (struct target_ops *self,
9729 int fd, const gdb_byte *write_buf, int len,
9730 ULONGEST offset, int *remote_errno)
9732 struct remote_state *rs = get_remote_state ();
9734 int left = get_remote_packet_size ();
9737 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9739 remote_buffer_add_int (&p, &left, fd);
9740 remote_buffer_add_string (&p, &left, ",");
9742 remote_buffer_add_int (&p, &left, offset);
9743 remote_buffer_add_string (&p, &left, ",");
9745 p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len,
9746 get_remote_packet_size () - (p - rs->buf));
9748 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9749 remote_errno, NULL, NULL);
9752 /* Read up to LEN bytes FD on the remote target into READ_BUF
9753 Return the number of bytes read, or -1 if an error occurs (and
9754 set *REMOTE_ERRNO). */
9757 remote_hostio_pread (struct target_ops *self,
9758 int fd, gdb_byte *read_buf, int len,
9759 ULONGEST offset, int *remote_errno)
9761 struct remote_state *rs = get_remote_state ();
9764 int left = get_remote_packet_size ();
9765 int ret, attachment_len;
9768 remote_buffer_add_string (&p, &left, "vFile:pread:");
9770 remote_buffer_add_int (&p, &left, fd);
9771 remote_buffer_add_string (&p, &left, ",");
9773 remote_buffer_add_int (&p, &left, len);
9774 remote_buffer_add_string (&p, &left, ",");
9776 remote_buffer_add_int (&p, &left, offset);
9778 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9779 remote_errno, &attachment,
9785 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9787 if (read_len != ret)
9788 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9793 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9794 (and set *REMOTE_ERRNO). */
9797 remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
9799 struct remote_state *rs = get_remote_state ();
9801 int left = get_remote_packet_size () - 1;
9803 remote_buffer_add_string (&p, &left, "vFile:close:");
9805 remote_buffer_add_int (&p, &left, fd);
9807 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9808 remote_errno, NULL, NULL);
9811 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9812 occurs (and set *REMOTE_ERRNO). */
9815 remote_hostio_unlink (struct target_ops *self,
9816 const char *filename, int *remote_errno)
9818 struct remote_state *rs = get_remote_state ();
9820 int left = get_remote_packet_size () - 1;
9822 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9824 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9827 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9828 remote_errno, NULL, NULL);
9831 /* Read value of symbolic link FILENAME on the remote target. Return
9832 a null-terminated string allocated via xmalloc, or NULL if an error
9833 occurs (and set *REMOTE_ERRNO). */
9836 remote_hostio_readlink (struct target_ops *self,
9837 const char *filename, int *remote_errno)
9839 struct remote_state *rs = get_remote_state ();
9842 int left = get_remote_packet_size ();
9843 int len, attachment_len;
9847 remote_buffer_add_string (&p, &left, "vFile:readlink:");
9849 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9852 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9853 remote_errno, &attachment,
9859 ret = xmalloc (len + 1);
9861 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
9862 (gdb_byte *) ret, len);
9863 if (read_len != len)
9864 error (_("Readlink returned %d, but %d bytes."), len, read_len);
9871 remote_fileio_errno_to_host (int errnum)
9895 case FILEIO_ENOTDIR:
9915 case FILEIO_ENAMETOOLONG:
9916 return ENAMETOOLONG;
9922 remote_hostio_error (int errnum)
9924 int host_error = remote_fileio_errno_to_host (errnum);
9926 if (host_error == -1)
9927 error (_("Unknown remote I/O error %d"), errnum);
9929 error (_("Remote I/O error: %s"), safe_strerror (host_error));
9933 remote_hostio_close_cleanup (void *opaque)
9935 int fd = *(int *) opaque;
9938 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
9943 remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
9945 const char *filename = bfd_get_filename (abfd);
9946 int fd, remote_errno;
9949 gdb_assert (remote_filename_p (filename));
9951 fd = remote_hostio_open (find_target_at (process_stratum),
9952 filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
9955 errno = remote_fileio_errno_to_host (remote_errno);
9956 bfd_set_error (bfd_error_system_call);
9960 stream = xmalloc (sizeof (int));
9966 remote_bfd_iovec_close (struct bfd *abfd, void *stream)
9968 int fd = *(int *)stream;
9973 /* Ignore errors on close; these may happen if the remote
9974 connection was already torn down. */
9975 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
9977 /* Zero means success. */
9982 remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
9983 file_ptr nbytes, file_ptr offset)
9985 int fd = *(int *)stream;
9987 file_ptr pos, bytes;
9990 while (nbytes > pos)
9992 bytes = remote_hostio_pread (find_target_at (process_stratum),
9993 fd, (gdb_byte *) buf + pos, nbytes - pos,
9994 offset + pos, &remote_errno);
9996 /* Success, but no bytes, means end-of-file. */
10000 errno = remote_fileio_errno_to_host (remote_errno);
10001 bfd_set_error (bfd_error_system_call);
10012 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
10014 /* FIXME: We should probably implement remote_hostio_stat. */
10015 sb->st_size = INT_MAX;
10020 remote_filename_p (const char *filename)
10022 return strncmp (filename,
10023 REMOTE_SYSROOT_PREFIX,
10024 sizeof (REMOTE_SYSROOT_PREFIX) - 1) == 0;
10028 remote_bfd_open (const char *remote_file, const char *target)
10030 bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
10031 remote_bfd_iovec_open, NULL,
10032 remote_bfd_iovec_pread,
10033 remote_bfd_iovec_close,
10034 remote_bfd_iovec_stat);
10040 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10042 struct cleanup *back_to, *close_cleanup;
10043 int retcode, fd, remote_errno, bytes, io_size;
10046 int bytes_in_buffer;
10049 struct remote_state *rs = get_remote_state ();
10051 if (!rs->remote_desc)
10052 error (_("command can only be used with remote target"));
10054 file = gdb_fopen_cloexec (local_file, "rb");
10056 perror_with_name (local_file);
10057 back_to = make_cleanup_fclose (file);
10059 fd = remote_hostio_open (find_target_at (process_stratum),
10060 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
10062 0700, &remote_errno);
10064 remote_hostio_error (remote_errno);
10066 /* Send up to this many bytes at once. They won't all fit in the
10067 remote packet limit, so we'll transfer slightly fewer. */
10068 io_size = get_remote_packet_size ();
10069 buffer = xmalloc (io_size);
10070 make_cleanup (xfree, buffer);
10072 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10074 bytes_in_buffer = 0;
10077 while (bytes_in_buffer || !saw_eof)
10081 bytes = fread (buffer + bytes_in_buffer, 1,
10082 io_size - bytes_in_buffer,
10087 error (_("Error reading %s."), local_file);
10090 /* EOF. Unless there is something still in the
10091 buffer from the last iteration, we are done. */
10093 if (bytes_in_buffer == 0)
10101 bytes += bytes_in_buffer;
10102 bytes_in_buffer = 0;
10104 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10106 offset, &remote_errno);
10109 remote_hostio_error (remote_errno);
10110 else if (retcode == 0)
10111 error (_("Remote write of %d bytes returned 0!"), bytes);
10112 else if (retcode < bytes)
10114 /* Short write. Save the rest of the read data for the next
10116 bytes_in_buffer = bytes - retcode;
10117 memmove (buffer, buffer + retcode, bytes_in_buffer);
10123 discard_cleanups (close_cleanup);
10124 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10125 remote_hostio_error (remote_errno);
10128 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10129 do_cleanups (back_to);
10133 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10135 struct cleanup *back_to, *close_cleanup;
10136 int fd, remote_errno, bytes, io_size;
10140 struct remote_state *rs = get_remote_state ();
10142 if (!rs->remote_desc)
10143 error (_("command can only be used with remote target"));
10145 fd = remote_hostio_open (find_target_at (process_stratum),
10146 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
10148 remote_hostio_error (remote_errno);
10150 file = gdb_fopen_cloexec (local_file, "wb");
10152 perror_with_name (local_file);
10153 back_to = make_cleanup_fclose (file);
10155 /* Send up to this many bytes at once. They won't all fit in the
10156 remote packet limit, so we'll transfer slightly fewer. */
10157 io_size = get_remote_packet_size ();
10158 buffer = xmalloc (io_size);
10159 make_cleanup (xfree, buffer);
10161 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10166 bytes = remote_hostio_pread (find_target_at (process_stratum),
10167 fd, buffer, io_size, offset, &remote_errno);
10169 /* Success, but no bytes, means end-of-file. */
10172 remote_hostio_error (remote_errno);
10176 bytes = fwrite (buffer, 1, bytes, file);
10178 perror_with_name (local_file);
10181 discard_cleanups (close_cleanup);
10182 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10183 remote_hostio_error (remote_errno);
10186 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10187 do_cleanups (back_to);
10191 remote_file_delete (const char *remote_file, int from_tty)
10193 int retcode, remote_errno;
10194 struct remote_state *rs = get_remote_state ();
10196 if (!rs->remote_desc)
10197 error (_("command can only be used with remote target"));
10199 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10200 remote_file, &remote_errno);
10202 remote_hostio_error (remote_errno);
10205 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10209 remote_put_command (char *args, int from_tty)
10211 struct cleanup *back_to;
10215 error_no_arg (_("file to put"));
10217 argv = gdb_buildargv (args);
10218 back_to = make_cleanup_freeargv (argv);
10219 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10220 error (_("Invalid parameters to remote put"));
10222 remote_file_put (argv[0], argv[1], from_tty);
10224 do_cleanups (back_to);
10228 remote_get_command (char *args, int from_tty)
10230 struct cleanup *back_to;
10234 error_no_arg (_("file to get"));
10236 argv = gdb_buildargv (args);
10237 back_to = make_cleanup_freeargv (argv);
10238 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10239 error (_("Invalid parameters to remote get"));
10241 remote_file_get (argv[0], argv[1], from_tty);
10243 do_cleanups (back_to);
10247 remote_delete_command (char *args, int from_tty)
10249 struct cleanup *back_to;
10253 error_no_arg (_("file to delete"));
10255 argv = gdb_buildargv (args);
10256 back_to = make_cleanup_freeargv (argv);
10257 if (argv[0] == NULL || argv[1] != NULL)
10258 error (_("Invalid parameters to remote delete"));
10260 remote_file_delete (argv[0], from_tty);
10262 do_cleanups (back_to);
10266 remote_command (char *args, int from_tty)
10268 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
10272 remote_can_execute_reverse (struct target_ops *self)
10274 if (packet_support (PACKET_bs) == PACKET_ENABLE
10275 || packet_support (PACKET_bc) == PACKET_ENABLE)
10282 remote_supports_non_stop (struct target_ops *self)
10288 remote_supports_disable_randomization (struct target_ops *self)
10290 /* Only supported in extended mode. */
10295 remote_supports_multi_process (struct target_ops *self)
10297 struct remote_state *rs = get_remote_state ();
10299 /* Only extended-remote handles being attached to multiple
10300 processes, even though plain remote can use the multi-process
10301 thread id extensions, so that GDB knows the target process's
10303 return rs->extended && remote_multi_process_p (rs);
10307 remote_supports_cond_tracepoints (void)
10309 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
10313 remote_supports_cond_breakpoints (struct target_ops *self)
10315 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
10319 remote_supports_fast_tracepoints (void)
10321 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
10325 remote_supports_static_tracepoints (void)
10327 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
10331 remote_supports_install_in_trace (void)
10333 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
10337 remote_supports_enable_disable_tracepoint (struct target_ops *self)
10339 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10344 remote_supports_string_tracing (struct target_ops *self)
10346 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
10350 remote_can_run_breakpoint_commands (struct target_ops *self)
10352 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
10356 remote_trace_init (struct target_ops *self)
10359 remote_get_noisy_reply (&target_buf, &target_buf_size);
10360 if (strcmp (target_buf, "OK") != 0)
10361 error (_("Target does not support this command."));
10364 static void free_actions_list (char **actions_list);
10365 static void free_actions_list_cleanup_wrapper (void *);
10367 free_actions_list_cleanup_wrapper (void *al)
10369 free_actions_list (al);
10373 free_actions_list (char **actions_list)
10377 if (actions_list == 0)
10380 for (ndx = 0; actions_list[ndx]; ndx++)
10381 xfree (actions_list[ndx]);
10383 xfree (actions_list);
10386 /* Recursive routine to walk through command list including loops, and
10387 download packets for each command. */
10390 remote_download_command_source (int num, ULONGEST addr,
10391 struct command_line *cmds)
10393 struct remote_state *rs = get_remote_state ();
10394 struct command_line *cmd;
10396 for (cmd = cmds; cmd; cmd = cmd->next)
10398 QUIT; /* Allow user to bail out with ^C. */
10399 strcpy (rs->buf, "QTDPsrc:");
10400 encode_source_string (num, addr, "cmd", cmd->line,
10401 rs->buf + strlen (rs->buf),
10402 rs->buf_size - strlen (rs->buf));
10404 remote_get_noisy_reply (&target_buf, &target_buf_size);
10405 if (strcmp (target_buf, "OK"))
10406 warning (_("Target does not support source download."));
10408 if (cmd->control_type == while_control
10409 || cmd->control_type == while_stepping_control)
10411 remote_download_command_source (num, addr, *cmd->body_list);
10413 QUIT; /* Allow user to bail out with ^C. */
10414 strcpy (rs->buf, "QTDPsrc:");
10415 encode_source_string (num, addr, "cmd", "end",
10416 rs->buf + strlen (rs->buf),
10417 rs->buf_size - strlen (rs->buf));
10419 remote_get_noisy_reply (&target_buf, &target_buf_size);
10420 if (strcmp (target_buf, "OK"))
10421 warning (_("Target does not support source download."));
10427 remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
10429 #define BUF_SIZE 2048
10433 char buf[BUF_SIZE];
10434 char **tdp_actions;
10435 char **stepping_actions;
10437 struct cleanup *old_chain = NULL;
10438 struct agent_expr *aexpr;
10439 struct cleanup *aexpr_chain = NULL;
10441 struct breakpoint *b = loc->owner;
10442 struct tracepoint *t = (struct tracepoint *) b;
10444 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
10445 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10447 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10450 tpaddr = loc->address;
10451 sprintf_vma (addrbuf, tpaddr);
10452 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10453 addrbuf, /* address */
10454 (b->enable_state == bp_enabled ? 'E' : 'D'),
10455 t->step_count, t->pass_count);
10456 /* Fast tracepoints are mostly handled by the target, but we can
10457 tell the target how big of an instruction block should be moved
10459 if (b->type == bp_fast_tracepoint)
10461 /* Only test for support at download time; we may not know
10462 target capabilities at definition time. */
10463 if (remote_supports_fast_tracepoints ())
10467 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10468 tpaddr, &isize, NULL))
10469 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10472 /* If it passed validation at definition but fails now,
10473 something is very wrong. */
10474 internal_error (__FILE__, __LINE__,
10475 _("Fast tracepoint not "
10476 "valid during download"));
10479 /* Fast tracepoints are functionally identical to regular
10480 tracepoints, so don't take lack of support as a reason to
10481 give up on the trace run. */
10482 warning (_("Target does not support fast tracepoints, "
10483 "downloading %d as regular tracepoint"), b->number);
10485 else if (b->type == bp_static_tracepoint)
10487 /* Only test for support at download time; we may not know
10488 target capabilities at definition time. */
10489 if (remote_supports_static_tracepoints ())
10491 struct static_tracepoint_marker marker;
10493 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10494 strcat (buf, ":S");
10496 error (_("Static tracepoint not valid during download"));
10499 /* Fast tracepoints are functionally identical to regular
10500 tracepoints, so don't take lack of support as a reason
10501 to give up on the trace run. */
10502 error (_("Target does not support static tracepoints"));
10504 /* If the tracepoint has a conditional, make it into an agent
10505 expression and append to the definition. */
10508 /* Only test support at download time, we may not know target
10509 capabilities at definition time. */
10510 if (remote_supports_cond_tracepoints ())
10512 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10513 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
10514 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10516 pkt = buf + strlen (buf);
10517 for (ndx = 0; ndx < aexpr->len; ++ndx)
10518 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10520 do_cleanups (aexpr_chain);
10523 warning (_("Target does not support conditional tracepoints, "
10524 "ignoring tp %d cond"), b->number);
10527 if (b->commands || *default_collect)
10530 remote_get_noisy_reply (&target_buf, &target_buf_size);
10531 if (strcmp (target_buf, "OK"))
10532 error (_("Target does not support tracepoints."));
10534 /* do_single_steps (t); */
10537 for (ndx = 0; tdp_actions[ndx]; ndx++)
10539 QUIT; /* Allow user to bail out with ^C. */
10540 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10541 b->number, addrbuf, /* address */
10543 ((tdp_actions[ndx + 1] || stepping_actions)
10546 remote_get_noisy_reply (&target_buf,
10548 if (strcmp (target_buf, "OK"))
10549 error (_("Error on target while setting tracepoints."));
10552 if (stepping_actions)
10554 for (ndx = 0; stepping_actions[ndx]; ndx++)
10556 QUIT; /* Allow user to bail out with ^C. */
10557 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10558 b->number, addrbuf, /* address */
10559 ((ndx == 0) ? "S" : ""),
10560 stepping_actions[ndx],
10561 (stepping_actions[ndx + 1] ? "-" : ""));
10563 remote_get_noisy_reply (&target_buf,
10565 if (strcmp (target_buf, "OK"))
10566 error (_("Error on target while setting tracepoints."));
10570 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
10572 if (b->addr_string)
10574 strcpy (buf, "QTDPsrc:");
10575 encode_source_string (b->number, loc->address,
10576 "at", b->addr_string, buf + strlen (buf),
10577 2048 - strlen (buf));
10580 remote_get_noisy_reply (&target_buf, &target_buf_size);
10581 if (strcmp (target_buf, "OK"))
10582 warning (_("Target does not support source download."));
10584 if (b->cond_string)
10586 strcpy (buf, "QTDPsrc:");
10587 encode_source_string (b->number, loc->address,
10588 "cond", b->cond_string, buf + strlen (buf),
10589 2048 - strlen (buf));
10591 remote_get_noisy_reply (&target_buf, &target_buf_size);
10592 if (strcmp (target_buf, "OK"))
10593 warning (_("Target does not support source download."));
10595 remote_download_command_source (b->number, loc->address,
10596 breakpoint_commands (b));
10599 do_cleanups (old_chain);
10603 remote_can_download_tracepoint (struct target_ops *self)
10605 struct remote_state *rs = get_remote_state ();
10606 struct trace_status *ts;
10609 /* Don't try to install tracepoints until we've relocated our
10610 symbols, and fetched and merged the target's tracepoint list with
10612 if (rs->starting_up)
10615 ts = current_trace_status ();
10616 status = remote_get_trace_status (self, ts);
10618 if (status == -1 || !ts->running_known || !ts->running)
10621 /* If we are in a tracing experiment, but remote stub doesn't support
10622 installing tracepoint in trace, we have to return. */
10623 if (!remote_supports_install_in_trace ())
10631 remote_download_trace_state_variable (struct target_ops *self,
10632 struct trace_state_variable *tsv)
10634 struct remote_state *rs = get_remote_state ();
10637 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10638 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10640 p = rs->buf + strlen (rs->buf);
10641 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10642 error (_("Trace state variable name too long for tsv definition packet"));
10643 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
10646 remote_get_noisy_reply (&target_buf, &target_buf_size);
10647 if (*target_buf == '\0')
10648 error (_("Target does not support this command."));
10649 if (strcmp (target_buf, "OK") != 0)
10650 error (_("Error on target while downloading trace state variable."));
10654 remote_enable_tracepoint (struct target_ops *self,
10655 struct bp_location *location)
10657 struct remote_state *rs = get_remote_state ();
10660 sprintf_vma (addr_buf, location->address);
10661 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10662 location->owner->number, addr_buf);
10664 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10665 if (*rs->buf == '\0')
10666 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10667 if (strcmp (rs->buf, "OK") != 0)
10668 error (_("Error on target while enabling tracepoint."));
10672 remote_disable_tracepoint (struct target_ops *self,
10673 struct bp_location *location)
10675 struct remote_state *rs = get_remote_state ();
10678 sprintf_vma (addr_buf, location->address);
10679 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10680 location->owner->number, addr_buf);
10682 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10683 if (*rs->buf == '\0')
10684 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10685 if (strcmp (rs->buf, "OK") != 0)
10686 error (_("Error on target while disabling tracepoint."));
10690 remote_trace_set_readonly_regions (struct target_ops *self)
10694 bfd_size_type size;
10700 return; /* No information to give. */
10702 strcpy (target_buf, "QTro");
10703 offset = strlen (target_buf);
10704 for (s = exec_bfd->sections; s; s = s->next)
10706 char tmp1[40], tmp2[40];
10709 if ((s->flags & SEC_LOAD) == 0 ||
10710 /* (s->flags & SEC_CODE) == 0 || */
10711 (s->flags & SEC_READONLY) == 0)
10715 vma = bfd_get_section_vma (abfd, s);
10716 size = bfd_get_section_size (s);
10717 sprintf_vma (tmp1, vma);
10718 sprintf_vma (tmp2, vma + size);
10719 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10720 if (offset + sec_length + 1 > target_buf_size)
10722 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
10724 Too many sections for read-only sections definition packet."));
10727 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10729 offset += sec_length;
10733 putpkt (target_buf);
10734 getpkt (&target_buf, &target_buf_size, 0);
10739 remote_trace_start (struct target_ops *self)
10741 putpkt ("QTStart");
10742 remote_get_noisy_reply (&target_buf, &target_buf_size);
10743 if (*target_buf == '\0')
10744 error (_("Target does not support this command."));
10745 if (strcmp (target_buf, "OK") != 0)
10746 error (_("Bogus reply from target: %s"), target_buf);
10750 remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
10752 /* Initialize it just to avoid a GCC false warning. */
10754 /* FIXME we need to get register block size some other way. */
10755 extern int trace_regblock_size;
10756 volatile struct gdb_exception ex;
10757 enum packet_result result;
10759 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
10762 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10764 putpkt ("qTStatus");
10766 TRY_CATCH (ex, RETURN_MASK_ERROR)
10768 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10772 if (ex.error != TARGET_CLOSE_ERROR)
10774 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10777 throw_exception (ex);
10780 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
10782 /* If the remote target doesn't do tracing, flag it. */
10783 if (result == PACKET_UNKNOWN)
10786 /* We're working with a live target. */
10787 ts->filename = NULL;
10790 error (_("Bogus trace status reply from target: %s"), target_buf);
10792 /* Function 'parse_trace_status' sets default value of each field of
10793 'ts' at first, so we don't have to do it here. */
10794 parse_trace_status (p, ts);
10796 return ts->running;
10800 remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
10801 struct uploaded_tp *utp)
10803 struct remote_state *rs = get_remote_state ();
10805 struct bp_location *loc;
10806 struct tracepoint *tp = (struct tracepoint *) bp;
10807 size_t size = get_remote_packet_size ();
10811 tp->base.hit_count = 0;
10812 tp->traceframe_usage = 0;
10813 for (loc = tp->base.loc; loc; loc = loc->next)
10815 /* If the tracepoint was never downloaded, don't go asking for
10817 if (tp->number_on_target == 0)
10819 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10820 phex_nz (loc->address, 0));
10822 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10823 if (reply && *reply)
10826 parse_tracepoint_status (reply + 1, bp, utp);
10832 utp->hit_count = 0;
10833 utp->traceframe_usage = 0;
10834 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10835 phex_nz (utp->addr, 0));
10837 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10838 if (reply && *reply)
10841 parse_tracepoint_status (reply + 1, bp, utp);
10847 remote_trace_stop (struct target_ops *self)
10850 remote_get_noisy_reply (&target_buf, &target_buf_size);
10851 if (*target_buf == '\0')
10852 error (_("Target does not support this command."));
10853 if (strcmp (target_buf, "OK") != 0)
10854 error (_("Bogus reply from target: %s"), target_buf);
10858 remote_trace_find (struct target_ops *self,
10859 enum trace_find_type type, int num,
10860 CORE_ADDR addr1, CORE_ADDR addr2,
10863 struct remote_state *rs = get_remote_state ();
10864 char *endbuf = rs->buf + get_remote_packet_size ();
10866 int target_frameno = -1, target_tracept = -1;
10868 /* Lookups other than by absolute frame number depend on the current
10869 trace selected, so make sure it is correct on the remote end
10871 if (type != tfind_number)
10872 set_remote_traceframe ();
10875 strcpy (p, "QTFrame:");
10876 p = strchr (p, '\0');
10880 xsnprintf (p, endbuf - p, "%x", num);
10883 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
10886 xsnprintf (p, endbuf - p, "tdp:%x", num);
10889 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10890 phex_nz (addr2, 0));
10892 case tfind_outside:
10893 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10894 phex_nz (addr2, 0));
10897 error (_("Unknown trace find type %d"), type);
10901 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
10902 if (*reply == '\0')
10903 error (_("Target does not support this command."));
10905 while (reply && *reply)
10910 target_frameno = (int) strtol (p, &reply, 16);
10912 error (_("Unable to parse trace frame number"));
10913 /* Don't update our remote traceframe number cache on failure
10914 to select a remote traceframe. */
10915 if (target_frameno == -1)
10920 target_tracept = (int) strtol (p, &reply, 16);
10922 error (_("Unable to parse tracepoint number"));
10924 case 'O': /* "OK"? */
10925 if (reply[1] == 'K' && reply[2] == '\0')
10928 error (_("Bogus reply from target: %s"), reply);
10931 error (_("Bogus reply from target: %s"), reply);
10934 *tpp = target_tracept;
10936 rs->remote_traceframe_number = target_frameno;
10937 return target_frameno;
10941 remote_get_trace_state_variable_value (struct target_ops *self,
10942 int tsvnum, LONGEST *val)
10944 struct remote_state *rs = get_remote_state ();
10948 set_remote_traceframe ();
10950 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
10952 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10953 if (reply && *reply)
10957 unpack_varlen_hex (reply + 1, &uval);
10958 *val = (LONGEST) uval;
10966 remote_save_trace_data (struct target_ops *self, const char *filename)
10968 struct remote_state *rs = get_remote_state ();
10972 strcpy (p, "QTSave:");
10974 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10975 error (_("Remote file name too long for trace save packet"));
10976 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
10979 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10980 if (*reply == '\0')
10981 error (_("Target does not support this command."));
10982 if (strcmp (reply, "OK") != 0)
10983 error (_("Bogus reply from target: %s"), reply);
10987 /* This is basically a memory transfer, but needs to be its own packet
10988 because we don't know how the target actually organizes its trace
10989 memory, plus we want to be able to ask for as much as possible, but
10990 not be unhappy if we don't get as much as we ask for. */
10993 remote_get_raw_trace_data (struct target_ops *self,
10994 gdb_byte *buf, ULONGEST offset, LONGEST len)
10996 struct remote_state *rs = get_remote_state ();
11002 strcpy (p, "qTBuffer:");
11004 p += hexnumstr (p, offset);
11006 p += hexnumstr (p, len);
11010 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11011 if (reply && *reply)
11013 /* 'l' by itself means we're at the end of the buffer and
11014 there is nothing more to get. */
11018 /* Convert the reply into binary. Limit the number of bytes to
11019 convert according to our passed-in buffer size, rather than
11020 what was returned in the packet; if the target is
11021 unexpectedly generous and gives us a bigger reply than we
11022 asked for, we don't want to crash. */
11023 rslt = hex2bin (target_buf, buf, len);
11027 /* Something went wrong, flag as an error. */
11032 remote_set_disconnected_tracing (struct target_ops *self, int val)
11034 struct remote_state *rs = get_remote_state ();
11036 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
11040 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
11042 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11043 if (*reply == '\0')
11044 error (_("Target does not support this command."));
11045 if (strcmp (reply, "OK") != 0)
11046 error (_("Bogus reply from target: %s"), reply);
11049 warning (_("Target does not support disconnected tracing."));
11053 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11055 struct thread_info *info = find_thread_ptid (ptid);
11057 if (info && info->private)
11058 return info->private->core;
11063 remote_set_circular_trace_buffer (struct target_ops *self, int val)
11065 struct remote_state *rs = get_remote_state ();
11068 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
11070 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11071 if (*reply == '\0')
11072 error (_("Target does not support this command."));
11073 if (strcmp (reply, "OK") != 0)
11074 error (_("Bogus reply from target: %s"), reply);
11077 static struct traceframe_info *
11078 remote_traceframe_info (struct target_ops *self)
11082 text = target_read_stralloc (¤t_target,
11083 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11086 struct traceframe_info *info;
11087 struct cleanup *back_to = make_cleanup (xfree, text);
11089 info = parse_traceframe_info (text);
11090 do_cleanups (back_to);
11097 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11098 instruction on which a fast tracepoint may be placed. Returns -1
11099 if the packet is not supported, and 0 if the minimum instruction
11100 length is unknown. */
11103 remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
11105 struct remote_state *rs = get_remote_state ();
11108 /* If we're not debugging a process yet, the IPA can't be
11110 if (!target_has_execution)
11113 /* Make sure the remote is pointing at the right process. */
11114 set_general_process ();
11116 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
11118 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11119 if (*reply == '\0')
11123 ULONGEST min_insn_len;
11125 unpack_varlen_hex (reply, &min_insn_len);
11127 return (int) min_insn_len;
11132 remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
11134 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
11136 struct remote_state *rs = get_remote_state ();
11137 char *buf = rs->buf;
11138 char *endbuf = rs->buf + get_remote_packet_size ();
11139 enum packet_result result;
11141 gdb_assert (val >= 0 || val == -1);
11142 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11143 /* Send -1 as literal "-1" to avoid host size dependency. */
11147 buf += hexnumstr (buf, (ULONGEST) -val);
11150 buf += hexnumstr (buf, (ULONGEST) val);
11153 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11154 result = packet_ok (rs->buf,
11155 &remote_protocol_packets[PACKET_QTBuffer_size]);
11157 if (result != PACKET_OK)
11158 warning (_("Bogus reply from target: %s"), rs->buf);
11163 remote_set_trace_notes (struct target_ops *self,
11164 const char *user, const char *notes,
11165 const char *stop_notes)
11167 struct remote_state *rs = get_remote_state ();
11169 char *buf = rs->buf;
11170 char *endbuf = rs->buf + get_remote_packet_size ();
11173 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11176 buf += xsnprintf (buf, endbuf - buf, "user:");
11177 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
11183 buf += xsnprintf (buf, endbuf - buf, "notes:");
11184 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
11190 buf += xsnprintf (buf, endbuf - buf, "tstop:");
11191 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
11195 /* Ensure the buffer is terminated. */
11199 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11200 if (*reply == '\0')
11203 if (strcmp (reply, "OK") != 0)
11204 error (_("Bogus reply from target: %s"), reply);
11210 remote_use_agent (struct target_ops *self, int use)
11212 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
11214 struct remote_state *rs = get_remote_state ();
11216 /* If the stub supports QAgent. */
11217 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
11219 getpkt (&rs->buf, &rs->buf_size, 0);
11221 if (strcmp (rs->buf, "OK") == 0)
11232 remote_can_use_agent (struct target_ops *self)
11234 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
11237 struct btrace_target_info
11239 /* The ptid of the traced thread. */
11243 /* Check whether the target supports branch tracing. */
11246 remote_supports_btrace (struct target_ops *self)
11248 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
11250 if (packet_support (PACKET_Qbtrace_bts) != PACKET_ENABLE)
11252 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
11258 /* Enable branch tracing. */
11260 static struct btrace_target_info *
11261 remote_enable_btrace (struct target_ops *self, ptid_t ptid)
11263 struct btrace_target_info *tinfo = NULL;
11264 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11265 struct remote_state *rs = get_remote_state ();
11266 char *buf = rs->buf;
11267 char *endbuf = rs->buf + get_remote_packet_size ();
11269 if (packet_config_support (packet) != PACKET_ENABLE)
11270 error (_("Target does not support branch tracing."));
11272 set_general_thread (ptid);
11274 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11276 getpkt (&rs->buf, &rs->buf_size, 0);
11278 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11280 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11281 error (_("Could not enable branch tracing for %s: %s"),
11282 target_pid_to_str (ptid), rs->buf + 2);
11284 error (_("Could not enable branch tracing for %s."),
11285 target_pid_to_str (ptid));
11288 tinfo = xzalloc (sizeof (*tinfo));
11289 tinfo->ptid = ptid;
11294 /* Disable branch tracing. */
11297 remote_disable_btrace (struct target_ops *self,
11298 struct btrace_target_info *tinfo)
11300 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11301 struct remote_state *rs = get_remote_state ();
11302 char *buf = rs->buf;
11303 char *endbuf = rs->buf + get_remote_packet_size ();
11305 if (packet_config_support (packet) != PACKET_ENABLE)
11306 error (_("Target does not support branch tracing."));
11308 set_general_thread (tinfo->ptid);
11310 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11312 getpkt (&rs->buf, &rs->buf_size, 0);
11314 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11316 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11317 error (_("Could not disable branch tracing for %s: %s"),
11318 target_pid_to_str (tinfo->ptid), rs->buf + 2);
11320 error (_("Could not disable branch tracing for %s."),
11321 target_pid_to_str (tinfo->ptid));
11327 /* Teardown branch tracing. */
11330 remote_teardown_btrace (struct target_ops *self,
11331 struct btrace_target_info *tinfo)
11333 /* We must not talk to the target during teardown. */
11337 /* Read the branch trace. */
11339 static enum btrace_error
11340 remote_read_btrace (struct target_ops *self,
11341 VEC (btrace_block_s) **btrace,
11342 struct btrace_target_info *tinfo,
11343 enum btrace_read_type type)
11345 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11346 struct remote_state *rs = get_remote_state ();
11347 struct cleanup *cleanup;
11351 if (packet_config_support (packet) != PACKET_ENABLE)
11352 error (_("Target does not support branch tracing."));
11354 #if !defined(HAVE_LIBEXPAT)
11355 error (_("Cannot process branch tracing result. XML parsing not supported."));
11360 case BTRACE_READ_ALL:
11363 case BTRACE_READ_NEW:
11366 case BTRACE_READ_DELTA:
11370 internal_error (__FILE__, __LINE__,
11371 _("Bad branch tracing read type: %u."),
11372 (unsigned int) type);
11375 xml = target_read_stralloc (¤t_target,
11376 TARGET_OBJECT_BTRACE, annex);
11378 return BTRACE_ERR_UNKNOWN;
11380 cleanup = make_cleanup (xfree, xml);
11381 *btrace = parse_xml_btrace (xml);
11382 do_cleanups (cleanup);
11384 return BTRACE_ERR_NONE;
11388 remote_augmented_libraries_svr4_read (struct target_ops *self)
11390 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
11394 /* Implementation of to_load. */
11397 remote_load (struct target_ops *self, const char *name, int from_tty)
11399 generic_load (name, from_tty);
11403 init_remote_ops (void)
11405 remote_ops.to_shortname = "remote";
11406 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
11407 remote_ops.to_doc =
11408 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11409 Specify the serial device it is connected to\n\
11410 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11411 remote_ops.to_open = remote_open;
11412 remote_ops.to_close = remote_close;
11413 remote_ops.to_detach = remote_detach;
11414 remote_ops.to_disconnect = remote_disconnect;
11415 remote_ops.to_resume = remote_resume;
11416 remote_ops.to_wait = remote_wait;
11417 remote_ops.to_fetch_registers = remote_fetch_registers;
11418 remote_ops.to_store_registers = remote_store_registers;
11419 remote_ops.to_prepare_to_store = remote_prepare_to_store;
11420 remote_ops.to_files_info = remote_files_info;
11421 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
11422 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
11423 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
11424 remote_ops.to_stopped_data_address = remote_stopped_data_address;
11425 remote_ops.to_watchpoint_addr_within_range =
11426 remote_watchpoint_addr_within_range;
11427 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
11428 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
11429 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
11430 remote_ops.to_region_ok_for_hw_watchpoint
11431 = remote_region_ok_for_hw_watchpoint;
11432 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
11433 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
11434 remote_ops.to_kill = remote_kill;
11435 remote_ops.to_load = remote_load;
11436 remote_ops.to_mourn_inferior = remote_mourn;
11437 remote_ops.to_pass_signals = remote_pass_signals;
11438 remote_ops.to_program_signals = remote_program_signals;
11439 remote_ops.to_thread_alive = remote_thread_alive;
11440 remote_ops.to_find_new_threads = remote_threads_info;
11441 remote_ops.to_pid_to_str = remote_pid_to_str;
11442 remote_ops.to_extra_thread_info = remote_threads_extra_info;
11443 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
11444 remote_ops.to_stop = remote_stop;
11445 remote_ops.to_xfer_partial = remote_xfer_partial;
11446 remote_ops.to_rcmd = remote_rcmd;
11447 remote_ops.to_log_command = serial_log_command;
11448 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
11449 remote_ops.to_stratum = process_stratum;
11450 remote_ops.to_has_all_memory = default_child_has_all_memory;
11451 remote_ops.to_has_memory = default_child_has_memory;
11452 remote_ops.to_has_stack = default_child_has_stack;
11453 remote_ops.to_has_registers = default_child_has_registers;
11454 remote_ops.to_has_execution = default_child_has_execution;
11455 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
11456 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
11457 remote_ops.to_magic = OPS_MAGIC;
11458 remote_ops.to_memory_map = remote_memory_map;
11459 remote_ops.to_flash_erase = remote_flash_erase;
11460 remote_ops.to_flash_done = remote_flash_done;
11461 remote_ops.to_read_description = remote_read_description;
11462 remote_ops.to_search_memory = remote_search_memory;
11463 remote_ops.to_can_async_p = remote_can_async_p;
11464 remote_ops.to_is_async_p = remote_is_async_p;
11465 remote_ops.to_async = remote_async;
11466 remote_ops.to_terminal_inferior = remote_terminal_inferior;
11467 remote_ops.to_terminal_ours = remote_terminal_ours;
11468 remote_ops.to_supports_non_stop = remote_supports_non_stop;
11469 remote_ops.to_supports_multi_process = remote_supports_multi_process;
11470 remote_ops.to_supports_disable_randomization
11471 = remote_supports_disable_randomization;
11472 remote_ops.to_fileio_open = remote_hostio_open;
11473 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11474 remote_ops.to_fileio_pread = remote_hostio_pread;
11475 remote_ops.to_fileio_close = remote_hostio_close;
11476 remote_ops.to_fileio_unlink = remote_hostio_unlink;
11477 remote_ops.to_fileio_readlink = remote_hostio_readlink;
11478 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
11479 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
11480 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
11481 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
11482 remote_ops.to_trace_init = remote_trace_init;
11483 remote_ops.to_download_tracepoint = remote_download_tracepoint;
11484 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
11485 remote_ops.to_download_trace_state_variable
11486 = remote_download_trace_state_variable;
11487 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11488 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
11489 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11490 remote_ops.to_trace_start = remote_trace_start;
11491 remote_ops.to_get_trace_status = remote_get_trace_status;
11492 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
11493 remote_ops.to_trace_stop = remote_trace_stop;
11494 remote_ops.to_trace_find = remote_trace_find;
11495 remote_ops.to_get_trace_state_variable_value
11496 = remote_get_trace_state_variable_value;
11497 remote_ops.to_save_trace_data = remote_save_trace_data;
11498 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
11499 remote_ops.to_upload_trace_state_variables
11500 = remote_upload_trace_state_variables;
11501 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
11502 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
11503 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
11504 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
11505 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
11506 remote_ops.to_set_trace_notes = remote_set_trace_notes;
11507 remote_ops.to_core_of_thread = remote_core_of_thread;
11508 remote_ops.to_verify_memory = remote_verify_memory;
11509 remote_ops.to_get_tib_address = remote_get_tib_address;
11510 remote_ops.to_set_permissions = remote_set_permissions;
11511 remote_ops.to_static_tracepoint_marker_at
11512 = remote_static_tracepoint_marker_at;
11513 remote_ops.to_static_tracepoint_markers_by_strid
11514 = remote_static_tracepoint_markers_by_strid;
11515 remote_ops.to_traceframe_info = remote_traceframe_info;
11516 remote_ops.to_use_agent = remote_use_agent;
11517 remote_ops.to_can_use_agent = remote_can_use_agent;
11518 remote_ops.to_supports_btrace = remote_supports_btrace;
11519 remote_ops.to_enable_btrace = remote_enable_btrace;
11520 remote_ops.to_disable_btrace = remote_disable_btrace;
11521 remote_ops.to_teardown_btrace = remote_teardown_btrace;
11522 remote_ops.to_read_btrace = remote_read_btrace;
11523 remote_ops.to_augmented_libraries_svr4_read =
11524 remote_augmented_libraries_svr4_read;
11527 /* Set up the extended remote vector by making a copy of the standard
11528 remote vector and adding to it. */
11531 init_extended_remote_ops (void)
11533 extended_remote_ops = remote_ops;
11535 extended_remote_ops.to_shortname = "extended-remote";
11536 extended_remote_ops.to_longname =
11537 "Extended remote serial target in gdb-specific protocol";
11538 extended_remote_ops.to_doc =
11539 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11540 Specify the serial device it is connected to (e.g. /dev/ttya).";
11541 extended_remote_ops.to_open = extended_remote_open;
11542 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11543 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
11544 extended_remote_ops.to_detach = extended_remote_detach;
11545 extended_remote_ops.to_attach = extended_remote_attach;
11546 extended_remote_ops.to_post_attach = extended_remote_post_attach;
11547 extended_remote_ops.to_kill = extended_remote_kill;
11548 extended_remote_ops.to_supports_disable_randomization
11549 = extended_remote_supports_disable_randomization;
11553 remote_can_async_p (struct target_ops *ops)
11555 struct remote_state *rs = get_remote_state ();
11557 if (!target_async_permitted)
11558 /* We only enable async when the user specifically asks for it. */
11561 /* We're async whenever the serial device is. */
11562 return serial_can_async_p (rs->remote_desc);
11566 remote_is_async_p (struct target_ops *ops)
11568 struct remote_state *rs = get_remote_state ();
11570 if (!target_async_permitted)
11571 /* We only enable async when the user specifically asks for it. */
11574 /* We're async whenever the serial device is. */
11575 return serial_is_async_p (rs->remote_desc);
11578 /* Pass the SERIAL event on and up to the client. One day this code
11579 will be able to delay notifying the client of an event until the
11580 point where an entire packet has been received. */
11582 static serial_event_ftype remote_async_serial_handler;
11585 remote_async_serial_handler (struct serial *scb, void *context)
11587 struct remote_state *rs = context;
11589 /* Don't propogate error information up to the client. Instead let
11590 the client find out about the error by querying the target. */
11591 rs->async_client_callback (INF_REG_EVENT, rs->async_client_context);
11595 remote_async_inferior_event_handler (gdb_client_data data)
11597 inferior_event_handler (INF_REG_EVENT, NULL);
11601 remote_async (struct target_ops *ops,
11602 void (*callback) (enum inferior_event_type event_type,
11606 struct remote_state *rs = get_remote_state ();
11608 if (callback != NULL)
11610 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
11611 rs->async_client_callback = callback;
11612 rs->async_client_context = context;
11615 serial_async (rs->remote_desc, NULL, NULL);
11619 set_remote_cmd (char *args, int from_tty)
11621 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
11625 show_remote_cmd (char *args, int from_tty)
11627 /* We can't just use cmd_show_list here, because we want to skip
11628 the redundant "show remote Z-packet" and the legacy aliases. */
11629 struct cleanup *showlist_chain;
11630 struct cmd_list_element *list = remote_show_cmdlist;
11631 struct ui_out *uiout = current_uiout;
11633 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11634 for (; list != NULL; list = list->next)
11635 if (strcmp (list->name, "Z-packet") == 0)
11637 else if (list->type == not_set_cmd)
11638 /* Alias commands are exactly like the original, except they
11639 don't have the normal type. */
11643 struct cleanup *option_chain
11644 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
11646 ui_out_field_string (uiout, "name", list->name);
11647 ui_out_text (uiout, ": ");
11648 if (list->type == show_cmd)
11649 do_show_command ((char *) NULL, from_tty, list);
11651 cmd_func (list, NULL, from_tty);
11652 /* Close the tuple. */
11653 do_cleanups (option_chain);
11656 /* Close the tuple. */
11657 do_cleanups (showlist_chain);
11661 /* Function to be called whenever a new objfile (shlib) is detected. */
11663 remote_new_objfile (struct objfile *objfile)
11665 struct remote_state *rs = get_remote_state ();
11667 if (rs->remote_desc != 0) /* Have a remote connection. */
11668 remote_check_symbols ();
11671 /* Pull all the tracepoints defined on the target and create local
11672 data structures representing them. We don't want to create real
11673 tracepoints yet, we don't want to mess up the user's existing
11677 remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
11679 struct remote_state *rs = get_remote_state ();
11682 /* Ask for a first packet of tracepoint definition. */
11684 getpkt (&rs->buf, &rs->buf_size, 0);
11686 while (*p && *p != 'l')
11688 parse_tracepoint_definition (p, utpp);
11689 /* Ask for another packet of tracepoint definition. */
11691 getpkt (&rs->buf, &rs->buf_size, 0);
11698 remote_upload_trace_state_variables (struct target_ops *self,
11699 struct uploaded_tsv **utsvp)
11701 struct remote_state *rs = get_remote_state ();
11704 /* Ask for a first packet of variable definition. */
11706 getpkt (&rs->buf, &rs->buf_size, 0);
11708 while (*p && *p != 'l')
11710 parse_tsv_definition (p, utsvp);
11711 /* Ask for another packet of variable definition. */
11713 getpkt (&rs->buf, &rs->buf_size, 0);
11719 /* The "set/show range-stepping" show hook. */
11722 show_range_stepping (struct ui_file *file, int from_tty,
11723 struct cmd_list_element *c,
11726 fprintf_filtered (file,
11727 _("Debugger's willingness to use range stepping "
11728 "is %s.\n"), value);
11731 /* The "set/show range-stepping" set hook. */
11734 set_range_stepping (char *ignore_args, int from_tty,
11735 struct cmd_list_element *c)
11737 struct remote_state *rs = get_remote_state ();
11739 /* Whene enabling, check whether range stepping is actually
11740 supported by the target, and warn if not. */
11741 if (use_range_stepping)
11743 if (rs->remote_desc != NULL)
11745 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
11746 remote_vcont_probe (rs);
11748 if (packet_support (PACKET_vCont) == PACKET_ENABLE
11749 && rs->supports_vCont.r)
11753 warning (_("Range stepping is not supported by the current target"));
11758 _initialize_remote (void)
11760 struct remote_state *rs;
11761 struct cmd_list_element *cmd;
11762 const char *cmd_name;
11764 /* architecture specific data */
11765 remote_gdbarch_data_handle =
11766 gdbarch_data_register_post_init (init_remote_state);
11767 remote_g_packet_data_handle =
11768 gdbarch_data_register_pre_init (remote_g_packet_data_init);
11770 /* Initialize the per-target state. At the moment there is only one
11771 of these, not one per target. Only one target is active at a
11773 remote_state = new_remote_state ();
11775 init_remote_ops ();
11776 add_target (&remote_ops);
11778 init_extended_remote_ops ();
11779 add_target (&extended_remote_ops);
11781 /* Hook into new objfile notification. */
11782 observer_attach_new_objfile (remote_new_objfile);
11783 /* We're no longer interested in notification events of an inferior
11785 observer_attach_inferior_exit (discard_pending_stop_replies);
11787 /* Set up signal handlers. */
11788 async_sigint_remote_token =
11789 create_async_signal_handler (async_remote_interrupt, NULL);
11790 async_sigint_remote_twice_token =
11791 create_async_signal_handler (async_remote_interrupt_twice, NULL);
11794 init_remote_threadtests ();
11797 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
11798 /* set/show remote ... */
11800 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
11801 Remote protocol specific variables\n\
11802 Configure various remote-protocol specific variables such as\n\
11803 the packets being used"),
11804 &remote_set_cmdlist, "set remote ",
11805 0 /* allow-unknown */, &setlist);
11806 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
11807 Remote protocol specific variables\n\
11808 Configure various remote-protocol specific variables such as\n\
11809 the packets being used"),
11810 &remote_show_cmdlist, "show remote ",
11811 0 /* allow-unknown */, &showlist);
11813 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
11814 Compare section data on target to the exec file.\n\
11815 Argument is a single section name (default: all loaded sections).\n\
11816 To compare only read-only loaded sections, specify the -r option."),
11819 add_cmd ("packet", class_maintenance, packet_command, _("\
11820 Send an arbitrary packet to a remote target.\n\
11821 maintenance packet TEXT\n\
11822 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11823 this command sends the string TEXT to the inferior, and displays the\n\
11824 response packet. GDB supplies the initial `$' character, and the\n\
11825 terminating `#' character and checksum."),
11828 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
11829 Set whether to send break if interrupted."), _("\
11830 Show whether to send break if interrupted."), _("\
11831 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11832 set_remotebreak, show_remotebreak,
11833 &setlist, &showlist);
11834 cmd_name = "remotebreak";
11835 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
11836 deprecate_cmd (cmd, "set remote interrupt-sequence");
11837 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
11838 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
11839 deprecate_cmd (cmd, "show remote interrupt-sequence");
11841 add_setshow_enum_cmd ("interrupt-sequence", class_support,
11842 interrupt_sequence_modes, &interrupt_sequence_mode,
11844 Set interrupt sequence to remote target."), _("\
11845 Show interrupt sequence to remote target."), _("\
11846 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11847 NULL, show_interrupt_sequence,
11848 &remote_set_cmdlist,
11849 &remote_show_cmdlist);
11851 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
11852 &interrupt_on_connect, _("\
11853 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11854 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11855 If set, interrupt sequence is sent to remote target."),
11857 &remote_set_cmdlist, &remote_show_cmdlist);
11859 /* Install commands for configuring memory read/write packets. */
11861 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
11862 Set the maximum number of bytes per memory write packet (deprecated)."),
11864 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
11865 Show the maximum number of bytes per memory write packet (deprecated)."),
11867 add_cmd ("memory-write-packet-size", no_class,
11868 set_memory_write_packet_size, _("\
11869 Set the maximum number of bytes per memory-write packet.\n\
11870 Specify the number of bytes in a packet or 0 (zero) for the\n\
11871 default packet size. The actual limit is further reduced\n\
11872 dependent on the target. Specify ``fixed'' to disable the\n\
11873 further restriction and ``limit'' to enable that restriction."),
11874 &remote_set_cmdlist);
11875 add_cmd ("memory-read-packet-size", no_class,
11876 set_memory_read_packet_size, _("\
11877 Set the maximum number of bytes per memory-read packet.\n\
11878 Specify the number of bytes in a packet or 0 (zero) for the\n\
11879 default packet size. The actual limit is further reduced\n\
11880 dependent on the target. Specify ``fixed'' to disable the\n\
11881 further restriction and ``limit'' to enable that restriction."),
11882 &remote_set_cmdlist);
11883 add_cmd ("memory-write-packet-size", no_class,
11884 show_memory_write_packet_size,
11885 _("Show the maximum number of bytes per memory-write packet."),
11886 &remote_show_cmdlist);
11887 add_cmd ("memory-read-packet-size", no_class,
11888 show_memory_read_packet_size,
11889 _("Show the maximum number of bytes per memory-read packet."),
11890 &remote_show_cmdlist);
11892 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
11893 &remote_hw_watchpoint_limit, _("\
11894 Set the maximum number of target hardware watchpoints."), _("\
11895 Show the maximum number of target hardware watchpoints."), _("\
11896 Specify a negative limit for unlimited."),
11897 NULL, NULL, /* FIXME: i18n: The maximum
11898 number of target hardware
11899 watchpoints is %s. */
11900 &remote_set_cmdlist, &remote_show_cmdlist);
11901 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
11902 &remote_hw_watchpoint_length_limit, _("\
11903 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11904 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11905 Specify a negative limit for unlimited."),
11906 NULL, NULL, /* FIXME: i18n: The maximum
11907 length (in bytes) of a target
11908 hardware watchpoint is %s. */
11909 &remote_set_cmdlist, &remote_show_cmdlist);
11910 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
11911 &remote_hw_breakpoint_limit, _("\
11912 Set the maximum number of target hardware breakpoints."), _("\
11913 Show the maximum number of target hardware breakpoints."), _("\
11914 Specify a negative limit for unlimited."),
11915 NULL, NULL, /* FIXME: i18n: The maximum
11916 number of target hardware
11917 breakpoints is %s. */
11918 &remote_set_cmdlist, &remote_show_cmdlist);
11920 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
11921 &remote_address_size, _("\
11922 Set the maximum size of the address (in bits) in a memory packet."), _("\
11923 Show the maximum size of the address (in bits) in a memory packet."), NULL,
11925 NULL, /* FIXME: i18n: */
11926 &setlist, &showlist);
11928 init_all_packet_configs ();
11930 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
11931 "X", "binary-download", 1);
11933 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
11934 "vCont", "verbose-resume", 0);
11936 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
11937 "QPassSignals", "pass-signals", 0);
11939 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
11940 "QProgramSignals", "program-signals", 0);
11942 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
11943 "qSymbol", "symbol-lookup", 0);
11945 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
11946 "P", "set-register", 1);
11948 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
11949 "p", "fetch-register", 1);
11951 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
11952 "Z0", "software-breakpoint", 0);
11954 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
11955 "Z1", "hardware-breakpoint", 0);
11957 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
11958 "Z2", "write-watchpoint", 0);
11960 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
11961 "Z3", "read-watchpoint", 0);
11963 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
11964 "Z4", "access-watchpoint", 0);
11966 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
11967 "qXfer:auxv:read", "read-aux-vector", 0);
11969 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
11970 "qXfer:features:read", "target-features", 0);
11972 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
11973 "qXfer:libraries:read", "library-info", 0);
11975 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
11976 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11978 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
11979 "qXfer:memory-map:read", "memory-map", 0);
11981 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
11982 "qXfer:spu:read", "read-spu-object", 0);
11984 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
11985 "qXfer:spu:write", "write-spu-object", 0);
11987 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
11988 "qXfer:osdata:read", "osdata", 0);
11990 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
11991 "qXfer:threads:read", "threads", 0);
11993 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
11994 "qXfer:siginfo:read", "read-siginfo-object", 0);
11996 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
11997 "qXfer:siginfo:write", "write-siginfo-object", 0);
11999 add_packet_config_cmd
12000 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
12001 "qXfer:traceframe-info:read", "traceframe-info", 0);
12003 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12004 "qXfer:uib:read", "unwind-info-block", 0);
12006 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
12007 "qGetTLSAddr", "get-thread-local-storage-address",
12010 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12011 "qGetTIBAddr", "get-thread-information-block-address",
12014 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12015 "bc", "reverse-continue", 0);
12017 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12018 "bs", "reverse-step", 0);
12020 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12021 "qSupported", "supported-packets", 0);
12023 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12024 "qSearch:memory", "search-memory", 0);
12026 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12027 "qTStatus", "trace-status", 0);
12029 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12030 "vFile:open", "hostio-open", 0);
12032 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12033 "vFile:pread", "hostio-pread", 0);
12035 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12036 "vFile:pwrite", "hostio-pwrite", 0);
12038 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12039 "vFile:close", "hostio-close", 0);
12041 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12042 "vFile:unlink", "hostio-unlink", 0);
12044 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12045 "vFile:readlink", "hostio-readlink", 0);
12047 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12048 "vAttach", "attach", 0);
12050 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12053 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12054 "QStartNoAckMode", "noack", 0);
12056 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12057 "vKill", "kill", 0);
12059 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12060 "qAttached", "query-attached", 0);
12062 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
12063 "ConditionalTracepoints",
12064 "conditional-tracepoints", 0);
12066 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12067 "ConditionalBreakpoints",
12068 "conditional-breakpoints", 0);
12070 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12071 "BreakpointCommands",
12072 "breakpoint-commands", 0);
12074 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12075 "FastTracepoints", "fast-tracepoints", 0);
12077 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12078 "TracepointSource", "TracepointSource", 0);
12080 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12081 "QAllow", "allow", 0);
12083 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12084 "StaticTracepoints", "static-tracepoints", 0);
12086 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12087 "InstallInTrace", "install-in-trace", 0);
12089 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12090 "qXfer:statictrace:read", "read-sdata-object", 0);
12092 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12093 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12095 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12096 "QDisableRandomization", "disable-randomization", 0);
12098 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12099 "QAgent", "agent", 0);
12101 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12102 "QTBuffer:size", "trace-buffer-size", 0);
12104 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12105 "Qbtrace:off", "disable-btrace", 0);
12107 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12108 "Qbtrace:bts", "enable-btrace", 0);
12110 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12111 "qXfer:btrace", "read-btrace", 0);
12113 /* Assert that we've registered commands for all packet configs. */
12117 for (i = 0; i < PACKET_MAX; i++)
12119 /* Ideally all configs would have a command associated. Some
12120 still don't though. */
12125 case PACKET_QNonStop:
12126 case PACKET_multiprocess_feature:
12127 case PACKET_EnableDisableTracepoints_feature:
12128 case PACKET_tracenz_feature:
12129 case PACKET_DisconnectedTracing_feature:
12130 case PACKET_augmented_libraries_svr4_read_feature:
12132 /* Additions to this list need to be well justified:
12133 pre-existing packets are OK; new packets are not. */
12141 /* This catches both forgetting to add a config command, and
12142 forgetting to remove a packet from the exception list. */
12143 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12147 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12148 Z sub-packet has its own set and show commands, but users may
12149 have sets to this variable in their .gdbinit files (or in their
12151 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
12152 &remote_Z_packet_detect, _("\
12153 Set use of remote protocol `Z' packets"), _("\
12154 Show use of remote protocol `Z' packets "), _("\
12155 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12157 set_remote_protocol_Z_packet_cmd,
12158 show_remote_protocol_Z_packet_cmd,
12159 /* FIXME: i18n: Use of remote protocol
12160 `Z' packets is %s. */
12161 &remote_set_cmdlist, &remote_show_cmdlist);
12163 add_prefix_cmd ("remote", class_files, remote_command, _("\
12164 Manipulate files on the remote system\n\
12165 Transfer files to and from the remote target system."),
12166 &remote_cmdlist, "remote ",
12167 0 /* allow-unknown */, &cmdlist);
12169 add_cmd ("put", class_files, remote_put_command,
12170 _("Copy a local file to the remote system."),
12173 add_cmd ("get", class_files, remote_get_command,
12174 _("Copy a remote file to the local system."),
12177 add_cmd ("delete", class_files, remote_delete_command,
12178 _("Delete a remote file."),
12181 remote_exec_file = xstrdup ("");
12182 add_setshow_string_noescape_cmd ("exec-file", class_files,
12183 &remote_exec_file, _("\
12184 Set the remote pathname for \"run\""), _("\
12185 Show the remote pathname for \"run\""), NULL, NULL, NULL,
12186 &remote_set_cmdlist, &remote_show_cmdlist);
12188 add_setshow_boolean_cmd ("range-stepping", class_run,
12189 &use_range_stepping, _("\
12190 Enable or disable range stepping."), _("\
12191 Show whether target-assisted range stepping is enabled."), _("\
12192 If on, and the target supports it, when stepping a source line, GDB\n\
12193 tells the target to step the corresponding range of addresses itself instead\n\
12194 of issuing multiple single-steps. This speeds up source level\n\
12195 stepping. If off, GDB always issues single-steps, even if range\n\
12196 stepping is supported by the target. The default is on."),
12197 set_range_stepping,
12198 show_range_stepping,
12202 /* Eventually initialize fileio. See fileio.c */
12203 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
12205 /* Take advantage of the fact that the TID field is not used, to tag
12206 special ptids with it set to != 0. */
12207 magic_null_ptid = ptid_build (42000, -1, 1);
12208 not_sent_ptid = ptid_build (42000, -2, 1);
12209 any_thread_ptid = ptid_build (42000, 0, 1);
12211 target_buf_size = 2048;
12212 target_buf = xmalloc (target_buf_size);