1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
30 /*#include "terminal.h" */
33 #include "gdb-stabs.h"
34 #include "gdbthread.h"
36 #include "remote-notif.h"
41 #include "cli/cli-decode.h"
42 #include "cli/cli-setshow.h"
43 #include "target-descriptions.h"
45 #include "filestuff.h"
50 #include "event-loop.h"
51 #include "event-top.h"
57 #include "gdbcore.h" /* for exec_bfd */
59 #include "remote-fileio.h"
60 #include "gdb/fileio.h"
62 #include "xml-support.h"
64 #include "memory-map.h"
66 #include "tracepoint.h"
72 /* Temp hacks for tracepoint encoding migration. */
73 static char *target_buf;
74 static long target_buf_size;
76 /* The size to align memory write packets, when practical. The protocol
77 does not guarantee any alignment, and gdb will generate short
78 writes and unaligned writes, but even as a best-effort attempt this
79 can improve bulk transfers. For instance, if a write is misaligned
80 relative to the target's data bus, the stub may need to make an extra
81 round trip fetching data from the target. This doesn't make a
82 huge difference, but it's easy to do, so we try to be helpful.
84 The alignment chosen is arbitrary; usually data bus width is
85 important here, not the possibly larger cache line size. */
86 enum { REMOTE_ALIGN_WRITES = 16 };
88 /* Prototypes for local functions. */
89 static void async_cleanup_sigint_signal_handler (void *dummy);
90 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
91 static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
92 int forever, int *is_notif);
94 static void async_handle_remote_sigint (int);
95 static void async_handle_remote_sigint_twice (int);
97 static void remote_files_info (struct target_ops *ignore);
99 static void remote_prepare_to_store (struct target_ops *self,
100 struct regcache *regcache);
102 static void remote_open_1 (const char *, int, struct target_ops *,
105 static void remote_close (struct target_ops *self);
109 static int remote_vkill (int pid, struct remote_state *rs);
111 static void remote_mourn (struct target_ops *ops);
113 static void extended_remote_restart (void);
115 static void extended_remote_mourn (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, int enable);
131 static void sync_remote_interrupt_twice (int signo);
133 static void interrupt_query (void);
135 static void set_general_thread (struct ptid ptid);
136 static void set_continue_thread (struct ptid ptid);
138 static void get_offsets (void);
140 static void skip_frame (void);
142 static long read_frame (char **buf_p, long *sizeof_buf);
144 static int hexnumlen (ULONGEST num);
146 static void init_remote_ops (void);
148 static void init_extended_remote_ops (void);
150 static void remote_stop (struct target_ops *self, ptid_t);
152 static int stubhex (int ch);
154 static int hexnumstr (char *, ULONGEST);
156 static int hexnumnstr (char *, ULONGEST, int);
158 static CORE_ADDR remote_address_masked (CORE_ADDR);
160 static void print_packet (const char *);
162 static void compare_sections_command (char *, int);
164 static void packet_command (char *, int);
166 static int stub_unpack_int (char *buff, int fieldlength);
168 static ptid_t remote_current_thread (ptid_t oldptid);
170 static int putpkt_binary (const char *buf, int cnt);
172 static void check_binary_download (CORE_ADDR addr);
174 struct packet_config;
176 static void show_packet_config_cmd (struct packet_config *config);
178 static void show_remote_protocol_packet_cmd (struct ui_file *file,
180 struct cmd_list_element *c,
183 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
184 static ptid_t read_ptid (char *buf, char **obuf);
186 static void remote_set_permissions (struct target_ops *self);
188 static int remote_get_trace_status (struct target_ops *self,
189 struct trace_status *ts);
191 static int remote_upload_tracepoints (struct target_ops *self,
192 struct uploaded_tp **utpp);
194 static int remote_upload_trace_state_variables (struct target_ops *self,
195 struct uploaded_tsv **utsvp);
197 static void remote_query_supported (void);
199 static void remote_check_symbols (void);
201 void _initialize_remote (void);
204 static void stop_reply_xfree (struct stop_reply *);
205 static void remote_parse_stop_reply (char *, struct stop_reply *);
206 static void push_stop_reply (struct stop_reply *);
207 static void discard_pending_stop_replies_in_queue (struct remote_state *);
208 static int peek_stop_reply (ptid_t ptid);
210 struct threads_listing_context;
211 static void remove_new_fork_children (struct threads_listing_context *);
213 static void remote_async_inferior_event_handler (gdb_client_data);
215 static void remote_terminal_ours (struct target_ops *self);
217 static int remote_read_description_p (struct target_ops *target);
219 static void remote_console_output (char *msg);
221 static int remote_supports_cond_breakpoints (struct target_ops *self);
223 static int remote_can_run_breakpoint_commands (struct target_ops *self);
225 static void remote_btrace_reset (void);
229 static struct cmd_list_element *remote_cmdlist;
231 /* For "set remote" and "show remote". */
233 static struct cmd_list_element *remote_set_cmdlist;
234 static struct cmd_list_element *remote_show_cmdlist;
236 /* Stub vCont actions support.
238 Each field is a boolean flag indicating whether the stub reports
239 support for the corresponding action. */
241 struct vCont_action_support
250 /* Controls whether GDB is willing to use range stepping. */
252 static int use_range_stepping = 1;
254 #define OPAQUETHREADBYTES 8
256 /* a 64 bit opaque identifier */
257 typedef unsigned char threadref[OPAQUETHREADBYTES];
259 /* About this many threadisds fit in a packet. */
261 #define MAXTHREADLISTRESULTS 32
263 /* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
278 /* True if we're going through initial connection setup (finding out
279 about the remote side's threads, relocating symbols, etc.). */
282 /* If we negotiated packet size explicitly (and thus can bypass
283 heuristics for the largest packet size that will not overflow
284 a buffer in the stub), this will be set to that packet size.
285 Otherwise zero, meaning to use the guessed size. */
286 long explicit_packet_size;
288 /* remote_wait is normally called when the target is running and
289 waits for a stop reply packet. But sometimes we need to call it
290 when the target is already stopped. We can send a "?" packet
291 and have remote_wait read the response. Or, if we already have
292 the response, we can stash it in BUF and tell remote_wait to
293 skip calling getpkt. This flag is set when BUF contains a
294 stop reply packet and the target is not waiting. */
295 int cached_wait_status;
297 /* True, if in no ack mode. That is, neither GDB nor the stub will
298 expect acks from each other. The connection is assumed to be
302 /* True if we're connected in extended remote mode. */
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply;
313 /* The status of the stub support for the various vCont actions. */
314 struct vCont_action_support supports_vCont;
316 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
317 responded to that. */
320 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
321 remote_open knows that we don't have a file open when the program
323 struct serial *remote_desc;
325 /* These are the threads which we last sent to the remote system. The
326 TID member will be -1 for all or -2 for not sent yet. */
327 ptid_t general_thread;
328 ptid_t continue_thread;
330 /* This is the traceframe which we last selected on the remote system.
331 It will be -1 if no traceframe is selected. */
332 int remote_traceframe_number;
334 char *last_pass_packet;
336 /* The last QProgramSignals packet sent to the target. We bypass
337 sending a new program signals list down to the target if the new
338 packet is exactly the same as the last we sent. IOW, we only let
339 the target know about program signals list changes. */
340 char *last_program_signals_packet;
342 enum gdb_signal last_sent_signal;
346 char *finished_object;
347 char *finished_annex;
348 ULONGEST finished_offset;
350 /* Should we try the 'ThreadInfo' query packet?
352 This variable (NOT available to the user: auto-detect only!)
353 determines whether GDB will use the new, simpler "ThreadInfo"
354 query or the older, more complex syntax for thread queries.
355 This is an auto-detect variable (set to true at each connect,
356 and set to false when the target fails to recognize it). */
357 int use_threadinfo_query;
358 int use_threadextra_query;
360 /* This is set to the data address of the access causing the target
361 to stop for a watchpoint. */
362 CORE_ADDR remote_watch_data_address;
364 /* Whether the target stopped for a breakpoint/watchpoint. */
365 enum target_stop_reason stop_reason;
367 threadref echo_nextthread;
368 threadref nextthread;
369 threadref resultthreadlist[MAXTHREADLISTRESULTS];
371 /* The state of remote notification. */
372 struct remote_notif_state *notif_state;
374 /* The branch trace configuration. */
375 struct btrace_config btrace_config;
377 /* The argument to the last "vFile:setfs:" packet we sent, used
378 to avoid sending repeated unnecessary "vFile:setfs:" packets.
379 Initialized to -1 to indicate that no "vFile:setfs:" packet
380 has yet been sent. */
384 /* Private data that we'll store in (struct thread_info)->private. */
385 struct private_thread_info
392 free_private_thread_info (struct private_thread_info *info)
398 /* This data could be associated with a target, but we do not always
399 have access to the current target when we need it, so for now it is
400 static. This will be fine for as long as only one target is in use
402 static struct remote_state *remote_state;
404 static struct remote_state *
405 get_remote_state_raw (void)
410 /* Allocate a new struct remote_state with xmalloc, initialize it, and
413 static struct remote_state *
414 new_remote_state (void)
416 struct remote_state *result = XCNEW (struct remote_state);
418 /* The default buffer size is unimportant; it will be expanded
419 whenever a larger buffer is needed. */
420 result->buf_size = 400;
421 result->buf = xmalloc (result->buf_size);
422 result->remote_traceframe_number = -1;
423 result->last_sent_signal = GDB_SIGNAL_0;
429 /* Description of the remote protocol for a given architecture. */
433 long offset; /* Offset into G packet. */
434 long regnum; /* GDB's internal register number. */
435 LONGEST pnum; /* Remote protocol register number. */
436 int in_g_packet; /* Always part of G packet. */
437 /* long size in bytes; == register_size (target_gdbarch (), regnum);
439 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
443 struct remote_arch_state
445 /* Description of the remote protocol registers. */
446 long sizeof_g_packet;
448 /* Description of the remote protocol registers indexed by REGNUM
449 (making an array gdbarch_num_regs in size). */
450 struct packet_reg *regs;
452 /* This is the size (in chars) of the first response to the ``g''
453 packet. It is used as a heuristic when determining the maximum
454 size of memory-read and memory-write packets. A target will
455 typically only reserve a buffer large enough to hold the ``g''
456 packet. The size does not include packet overhead (headers and
458 long actual_register_packet_size;
460 /* This is the maximum size (in chars) of a non read/write packet.
461 It is also used as a cap on the size of read/write packets. */
462 long remote_packet_size;
465 /* Utility: generate error from an incoming stub packet. */
467 trace_error (char *buf)
470 return; /* not an error msg */
473 case '1': /* malformed packet error */
474 if (*++buf == '0') /* general case: */
475 error (_("remote.c: error in outgoing packet."));
477 error (_("remote.c: error in outgoing packet at field #%ld."),
478 strtol (buf, NULL, 16));
480 error (_("Target returns error code '%s'."), buf);
484 /* Utility: wait for reply from stub, while accepting "O" packets. */
486 remote_get_noisy_reply (char **buf_p,
489 do /* Loop on reply from remote stub. */
493 QUIT; /* Allow user to bail out with ^C. */
494 getpkt (buf_p, sizeof_buf, 0);
498 else if (startswith (buf, "qRelocInsn:"))
501 CORE_ADDR from, to, org_to;
503 int adjusted_size = 0;
506 p = buf + strlen ("qRelocInsn:");
507 pp = unpack_varlen_hex (p, &ul);
509 error (_("invalid qRelocInsn packet: %s"), buf);
513 unpack_varlen_hex (p, &ul);
520 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
523 CATCH (ex, RETURN_MASK_ALL)
525 if (ex.error == MEMORY_ERROR)
527 /* Propagate memory errors silently back to the
528 target. The stub may have limited the range of
529 addresses we can write to, for example. */
533 /* Something unexpectedly bad happened. Be verbose
534 so we can tell what, and propagate the error back
535 to the stub, so it doesn't get stuck waiting for
537 exception_fprintf (gdb_stderr, ex,
538 _("warning: relocating instruction: "));
546 adjusted_size = to - org_to;
548 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
552 else if (buf[0] == 'O' && buf[1] != 'K')
553 remote_console_output (buf + 1); /* 'O' message from stub */
555 return buf; /* Here's the actual reply. */
560 /* Handle for retreving the remote protocol data from gdbarch. */
561 static struct gdbarch_data *remote_gdbarch_data_handle;
563 static struct remote_arch_state *
564 get_remote_arch_state (void)
566 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
569 /* Fetch the global remote target state. */
571 static struct remote_state *
572 get_remote_state (void)
574 /* Make sure that the remote architecture state has been
575 initialized, because doing so might reallocate rs->buf. Any
576 function which calls getpkt also needs to be mindful of changes
577 to rs->buf, but this call limits the number of places which run
579 get_remote_arch_state ();
581 return get_remote_state_raw ();
585 compare_pnums (const void *lhs_, const void *rhs_)
587 const struct packet_reg * const *lhs = lhs_;
588 const struct packet_reg * const *rhs = rhs_;
590 if ((*lhs)->pnum < (*rhs)->pnum)
592 else if ((*lhs)->pnum == (*rhs)->pnum)
599 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
601 int regnum, num_remote_regs, offset;
602 struct packet_reg **remote_regs;
604 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
606 struct packet_reg *r = ®s[regnum];
608 if (register_size (gdbarch, regnum) == 0)
609 /* Do not try to fetch zero-sized (placeholder) registers. */
612 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
617 /* Define the g/G packet format as the contents of each register
618 with a remote protocol number, in order of ascending protocol
621 remote_regs = alloca (gdbarch_num_regs (gdbarch)
622 * sizeof (struct packet_reg *));
623 for (num_remote_regs = 0, regnum = 0;
624 regnum < gdbarch_num_regs (gdbarch);
626 if (regs[regnum].pnum != -1)
627 remote_regs[num_remote_regs++] = ®s[regnum];
629 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
632 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
634 remote_regs[regnum]->in_g_packet = 1;
635 remote_regs[regnum]->offset = offset;
636 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
642 /* Given the architecture described by GDBARCH, return the remote
643 protocol register's number and the register's offset in the g/G
644 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
645 If the target does not have a mapping for REGNUM, return false,
646 otherwise, return true. */
649 remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
650 int *pnum, int *poffset)
653 struct packet_reg *regs;
654 struct cleanup *old_chain;
656 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
658 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
659 old_chain = make_cleanup (xfree, regs);
661 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
663 *pnum = regs[regnum].pnum;
664 *poffset = regs[regnum].offset;
666 do_cleanups (old_chain);
672 init_remote_state (struct gdbarch *gdbarch)
674 struct remote_state *rs = get_remote_state_raw ();
675 struct remote_arch_state *rsa;
677 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
679 /* Use the architecture to build a regnum<->pnum table, which will be
680 1:1 unless a feature set specifies otherwise. */
681 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
682 gdbarch_num_regs (gdbarch),
685 /* Record the maximum possible size of the g packet - it may turn out
687 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
689 /* Default maximum number of characters in a packet body. Many
690 remote stubs have a hardwired buffer size of 400 bytes
691 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
692 as the maximum packet-size to ensure that the packet and an extra
693 NUL character can always fit in the buffer. This stops GDB
694 trashing stubs that try to squeeze an extra NUL into what is
695 already a full buffer (As of 1999-12-04 that was most stubs). */
696 rsa->remote_packet_size = 400 - 1;
698 /* This one is filled in when a ``g'' packet is received. */
699 rsa->actual_register_packet_size = 0;
701 /* Should rsa->sizeof_g_packet needs more space than the
702 default, adjust the size accordingly. Remember that each byte is
703 encoded as two characters. 32 is the overhead for the packet
704 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
705 (``$NN:G...#NN'') is a better guess, the below has been padded a
707 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
708 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
710 /* Make sure that the packet buffer is plenty big enough for
711 this architecture. */
712 if (rs->buf_size < rsa->remote_packet_size)
714 rs->buf_size = 2 * rsa->remote_packet_size;
715 rs->buf = xrealloc (rs->buf, rs->buf_size);
721 /* Return the current allowed size of a remote packet. This is
722 inferred from the current architecture, and should be used to
723 limit the length of outgoing packets. */
725 get_remote_packet_size (void)
727 struct remote_state *rs = get_remote_state ();
728 struct remote_arch_state *rsa = get_remote_arch_state ();
730 if (rs->explicit_packet_size)
731 return rs->explicit_packet_size;
733 return rsa->remote_packet_size;
736 static struct packet_reg *
737 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
739 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
743 struct packet_reg *r = &rsa->regs[regnum];
745 gdb_assert (r->regnum == regnum);
750 static struct packet_reg *
751 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
755 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
757 struct packet_reg *r = &rsa->regs[i];
765 static struct target_ops remote_ops;
767 static struct target_ops extended_remote_ops;
769 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
770 ``forever'' still use the normal timeout mechanism. This is
771 currently used by the ASYNC code to guarentee that target reads
772 during the initial connect always time-out. Once getpkt has been
773 modified to return a timeout indication and, in turn
774 remote_wait()/wait_for_inferior() have gained a timeout parameter
776 static int wait_forever_enabled_p = 1;
778 /* Allow the user to specify what sequence to send to the remote
779 when he requests a program interruption: Although ^C is usually
780 what remote systems expect (this is the default, here), it is
781 sometimes preferable to send a break. On other systems such
782 as the Linux kernel, a break followed by g, which is Magic SysRq g
783 is required in order to interrupt the execution. */
784 const char interrupt_sequence_control_c[] = "Ctrl-C";
785 const char interrupt_sequence_break[] = "BREAK";
786 const char interrupt_sequence_break_g[] = "BREAK-g";
787 static const char *const interrupt_sequence_modes[] =
789 interrupt_sequence_control_c,
790 interrupt_sequence_break,
791 interrupt_sequence_break_g,
794 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
797 show_interrupt_sequence (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c,
801 if (interrupt_sequence_mode == interrupt_sequence_control_c)
802 fprintf_filtered (file,
803 _("Send the ASCII ETX character (Ctrl-c) "
804 "to the remote target to interrupt the "
805 "execution of the program.\n"));
806 else if (interrupt_sequence_mode == interrupt_sequence_break)
807 fprintf_filtered (file,
808 _("send a break signal to the remote target "
809 "to interrupt the execution of the program.\n"));
810 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
811 fprintf_filtered (file,
812 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
813 "the remote target to interrupt the execution "
814 "of Linux kernel.\n"));
816 internal_error (__FILE__, __LINE__,
817 _("Invalid value for interrupt_sequence_mode: %s."),
818 interrupt_sequence_mode);
821 /* This boolean variable specifies whether interrupt_sequence is sent
822 to the remote target when gdb connects to it.
823 This is mostly needed when you debug the Linux kernel: The Linux kernel
824 expects BREAK g which is Magic SysRq g for connecting gdb. */
825 static int interrupt_on_connect = 0;
827 /* This variable is used to implement the "set/show remotebreak" commands.
828 Since these commands are now deprecated in favor of "set/show remote
829 interrupt-sequence", it no longer has any effect on the code. */
830 static int remote_break;
833 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
836 interrupt_sequence_mode = interrupt_sequence_break;
838 interrupt_sequence_mode = interrupt_sequence_control_c;
842 show_remotebreak (struct ui_file *file, int from_tty,
843 struct cmd_list_element *c,
848 /* This variable sets the number of bits in an address that are to be
849 sent in a memory ("M" or "m") packet. Normally, after stripping
850 leading zeros, the entire address would be sent. This variable
851 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
852 initial implementation of remote.c restricted the address sent in
853 memory packets to ``host::sizeof long'' bytes - (typically 32
854 bits). Consequently, for 64 bit targets, the upper 32 bits of an
855 address was never sent. Since fixing this bug may cause a break in
856 some remote targets this variable is principly provided to
857 facilitate backward compatibility. */
859 static unsigned int remote_address_size;
861 /* Temporary to track who currently owns the terminal. See
862 remote_terminal_* for more details. */
864 static int remote_async_terminal_ours_p;
866 /* The executable file to use for "run" on the remote side. */
868 static char *remote_exec_file = "";
871 /* User configurable variables for the number of characters in a
872 memory read/write packet. MIN (rsa->remote_packet_size,
873 rsa->sizeof_g_packet) is the default. Some targets need smaller
874 values (fifo overruns, et.al.) and some users need larger values
875 (speed up transfers). The variables ``preferred_*'' (the user
876 request), ``current_*'' (what was actually set) and ``forced_*''
877 (Positive - a soft limit, negative - a hard limit). */
879 struct memory_packet_config
886 /* Compute the current size of a read/write packet. Since this makes
887 use of ``actual_register_packet_size'' the computation is dynamic. */
890 get_memory_packet_size (struct memory_packet_config *config)
892 struct remote_state *rs = get_remote_state ();
893 struct remote_arch_state *rsa = get_remote_arch_state ();
895 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
896 law?) that some hosts don't cope very well with large alloca()
897 calls. Eventually the alloca() code will be replaced by calls to
898 xmalloc() and make_cleanups() allowing this restriction to either
899 be lifted or removed. */
900 #ifndef MAX_REMOTE_PACKET_SIZE
901 #define MAX_REMOTE_PACKET_SIZE 16384
903 /* NOTE: 20 ensures we can write at least one byte. */
904 #ifndef MIN_REMOTE_PACKET_SIZE
905 #define MIN_REMOTE_PACKET_SIZE 20
910 if (config->size <= 0)
911 what_they_get = MAX_REMOTE_PACKET_SIZE;
913 what_they_get = config->size;
917 what_they_get = get_remote_packet_size ();
918 /* Limit the packet to the size specified by the user. */
920 && what_they_get > config->size)
921 what_they_get = config->size;
923 /* Limit it to the size of the targets ``g'' response unless we have
924 permission from the stub to use a larger packet size. */
925 if (rs->explicit_packet_size == 0
926 && rsa->actual_register_packet_size > 0
927 && what_they_get > rsa->actual_register_packet_size)
928 what_they_get = rsa->actual_register_packet_size;
930 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
931 what_they_get = MAX_REMOTE_PACKET_SIZE;
932 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
933 what_they_get = MIN_REMOTE_PACKET_SIZE;
935 /* Make sure there is room in the global buffer for this packet
936 (including its trailing NUL byte). */
937 if (rs->buf_size < what_they_get + 1)
939 rs->buf_size = 2 * what_they_get;
940 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
943 return what_they_get;
946 /* Update the size of a read/write packet. If they user wants
947 something really big then do a sanity check. */
950 set_memory_packet_size (char *args, struct memory_packet_config *config)
952 int fixed_p = config->fixed_p;
953 long size = config->size;
956 error (_("Argument required (integer, `fixed' or `limited')."));
957 else if (strcmp (args, "hard") == 0
958 || strcmp (args, "fixed") == 0)
960 else if (strcmp (args, "soft") == 0
961 || strcmp (args, "limit") == 0)
967 size = strtoul (args, &end, 0);
969 error (_("Invalid %s (bad syntax)."), config->name);
971 /* Instead of explicitly capping the size of a packet to
972 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
973 instead allowed to set the size to something arbitrarily
975 if (size > MAX_REMOTE_PACKET_SIZE)
976 error (_("Invalid %s (too large)."), config->name);
980 if (fixed_p && !config->fixed_p)
982 if (! query (_("The target may not be able to correctly handle a %s\n"
983 "of %ld bytes. Change the packet size? "),
985 error (_("Packet size not changed."));
987 /* Update the config. */
988 config->fixed_p = fixed_p;
993 show_memory_packet_size (struct memory_packet_config *config)
995 printf_filtered (_("The %s is %ld. "), config->name, config->size);
997 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
998 get_memory_packet_size (config));
1000 printf_filtered (_("Packets are limited to %ld bytes.\n"),
1001 get_memory_packet_size (config));
1004 static struct memory_packet_config memory_write_packet_config =
1006 "memory-write-packet-size",
1010 set_memory_write_packet_size (char *args, int from_tty)
1012 set_memory_packet_size (args, &memory_write_packet_config);
1016 show_memory_write_packet_size (char *args, int from_tty)
1018 show_memory_packet_size (&memory_write_packet_config);
1022 get_memory_write_packet_size (void)
1024 return get_memory_packet_size (&memory_write_packet_config);
1027 static struct memory_packet_config memory_read_packet_config =
1029 "memory-read-packet-size",
1033 set_memory_read_packet_size (char *args, int from_tty)
1035 set_memory_packet_size (args, &memory_read_packet_config);
1039 show_memory_read_packet_size (char *args, int from_tty)
1041 show_memory_packet_size (&memory_read_packet_config);
1045 get_memory_read_packet_size (void)
1047 long size = get_memory_packet_size (&memory_read_packet_config);
1049 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1050 extra buffer size argument before the memory read size can be
1051 increased beyond this. */
1052 if (size > get_remote_packet_size ())
1053 size = get_remote_packet_size ();
1058 /* Generic configuration support for packets the stub optionally
1059 supports. Allows the user to specify the use of the packet as well
1060 as allowing GDB to auto-detect support in the remote stub. */
1064 PACKET_SUPPORT_UNKNOWN = 0,
1069 struct packet_config
1074 /* If auto, GDB auto-detects support for this packet or feature,
1075 either through qSupported, or by trying the packet and looking
1076 at the response. If true, GDB assumes the target supports this
1077 packet. If false, the packet is disabled. Configs that don't
1078 have an associated command always have this set to auto. */
1079 enum auto_boolean detect;
1081 /* Does the target support this packet? */
1082 enum packet_support support;
1085 /* Analyze a packet's return value and update the packet config
1095 static enum packet_support packet_config_support (struct packet_config *config);
1096 static enum packet_support packet_support (int packet);
1099 show_packet_config_cmd (struct packet_config *config)
1101 char *support = "internal-error";
1103 switch (packet_config_support (config))
1106 support = "enabled";
1108 case PACKET_DISABLE:
1109 support = "disabled";
1111 case PACKET_SUPPORT_UNKNOWN:
1112 support = "unknown";
1115 switch (config->detect)
1117 case AUTO_BOOLEAN_AUTO:
1118 printf_filtered (_("Support for the `%s' packet "
1119 "is auto-detected, currently %s.\n"),
1120 config->name, support);
1122 case AUTO_BOOLEAN_TRUE:
1123 case AUTO_BOOLEAN_FALSE:
1124 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1125 config->name, support);
1131 add_packet_config_cmd (struct packet_config *config, const char *name,
1132 const char *title, int legacy)
1138 config->name = name;
1139 config->title = title;
1140 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1142 show_doc = xstrprintf ("Show current use of remote "
1143 "protocol `%s' (%s) packet",
1145 /* set/show TITLE-packet {auto,on,off} */
1146 cmd_name = xstrprintf ("%s-packet", title);
1147 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1148 &config->detect, set_doc,
1149 show_doc, NULL, /* help_doc */
1151 show_remote_protocol_packet_cmd,
1152 &remote_set_cmdlist, &remote_show_cmdlist);
1153 /* The command code copies the documentation strings. */
1156 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1161 legacy_name = xstrprintf ("%s-packet", name);
1162 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1163 &remote_set_cmdlist);
1164 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1165 &remote_show_cmdlist);
1169 static enum packet_result
1170 packet_check_result (const char *buf)
1174 /* The stub recognized the packet request. Check that the
1175 operation succeeded. */
1177 && isxdigit (buf[1]) && isxdigit (buf[2])
1179 /* "Enn" - definitly an error. */
1180 return PACKET_ERROR;
1182 /* Always treat "E." as an error. This will be used for
1183 more verbose error messages, such as E.memtypes. */
1184 if (buf[0] == 'E' && buf[1] == '.')
1185 return PACKET_ERROR;
1187 /* The packet may or may not be OK. Just assume it is. */
1191 /* The stub does not support the packet. */
1192 return PACKET_UNKNOWN;
1195 static enum packet_result
1196 packet_ok (const char *buf, struct packet_config *config)
1198 enum packet_result result;
1200 if (config->detect != AUTO_BOOLEAN_TRUE
1201 && config->support == PACKET_DISABLE)
1202 internal_error (__FILE__, __LINE__,
1203 _("packet_ok: attempt to use a disabled packet"));
1205 result = packet_check_result (buf);
1210 /* The stub recognized the packet request. */
1211 if (config->support == PACKET_SUPPORT_UNKNOWN)
1214 fprintf_unfiltered (gdb_stdlog,
1215 "Packet %s (%s) is supported\n",
1216 config->name, config->title);
1217 config->support = PACKET_ENABLE;
1220 case PACKET_UNKNOWN:
1221 /* The stub does not support the packet. */
1222 if (config->detect == AUTO_BOOLEAN_AUTO
1223 && config->support == PACKET_ENABLE)
1225 /* If the stub previously indicated that the packet was
1226 supported then there is a protocol error. */
1227 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1228 config->name, config->title);
1230 else if (config->detect == AUTO_BOOLEAN_TRUE)
1232 /* The user set it wrong. */
1233 error (_("Enabled packet %s (%s) not recognized by stub"),
1234 config->name, config->title);
1238 fprintf_unfiltered (gdb_stdlog,
1239 "Packet %s (%s) is NOT supported\n",
1240 config->name, config->title);
1241 config->support = PACKET_DISABLE;
1262 PACKET_vFile_pwrite,
1264 PACKET_vFile_unlink,
1265 PACKET_vFile_readlink,
1268 PACKET_qXfer_features,
1269 PACKET_qXfer_exec_file,
1270 PACKET_qXfer_libraries,
1271 PACKET_qXfer_libraries_svr4,
1272 PACKET_qXfer_memory_map,
1273 PACKET_qXfer_spu_read,
1274 PACKET_qXfer_spu_write,
1275 PACKET_qXfer_osdata,
1276 PACKET_qXfer_threads,
1277 PACKET_qXfer_statictrace_read,
1278 PACKET_qXfer_traceframe_info,
1284 PACKET_QPassSignals,
1285 PACKET_QProgramSignals,
1287 PACKET_qSearch_memory,
1290 PACKET_QStartNoAckMode,
1292 PACKET_qXfer_siginfo_read,
1293 PACKET_qXfer_siginfo_write,
1296 /* Support for conditional tracepoints. */
1297 PACKET_ConditionalTracepoints,
1299 /* Support for target-side breakpoint conditions. */
1300 PACKET_ConditionalBreakpoints,
1302 /* Support for target-side breakpoint commands. */
1303 PACKET_BreakpointCommands,
1305 /* Support for fast tracepoints. */
1306 PACKET_FastTracepoints,
1308 /* Support for static tracepoints. */
1309 PACKET_StaticTracepoints,
1311 /* Support for installing tracepoints while a trace experiment is
1313 PACKET_InstallInTrace,
1317 PACKET_TracepointSource,
1320 PACKET_QDisableRandomization,
1322 PACKET_QTBuffer_size,
1325 PACKET_qXfer_btrace,
1327 /* Support for the QNonStop packet. */
1330 /* Support for multi-process extensions. */
1331 PACKET_multiprocess_feature,
1333 /* Support for enabling and disabling tracepoints while a trace
1334 experiment is running. */
1335 PACKET_EnableDisableTracepoints_feature,
1337 /* Support for collecting strings using the tracenz bytecode. */
1338 PACKET_tracenz_feature,
1340 /* Support for continuing to run a trace experiment while GDB is
1342 PACKET_DisconnectedTracing_feature,
1344 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1345 PACKET_augmented_libraries_svr4_read_feature,
1347 /* Support for the qXfer:btrace-conf:read packet. */
1348 PACKET_qXfer_btrace_conf,
1350 /* Support for the Qbtrace-conf:bts:size packet. */
1351 PACKET_Qbtrace_conf_bts_size,
1353 /* Support for swbreak+ feature. */
1354 PACKET_swbreak_feature,
1356 /* Support for hwbreak+ feature. */
1357 PACKET_hwbreak_feature,
1359 /* Support for fork events. */
1360 PACKET_fork_event_feature,
1362 /* Support for vfork events. */
1363 PACKET_vfork_event_feature,
1368 static struct packet_config remote_protocol_packets[PACKET_MAX];
1370 /* Returns the packet's corresponding "set remote foo-packet" command
1371 state. See struct packet_config for more details. */
1373 static enum auto_boolean
1374 packet_set_cmd_state (int packet)
1376 return remote_protocol_packets[packet].detect;
1379 /* Returns whether a given packet or feature is supported. This takes
1380 into account the state of the corresponding "set remote foo-packet"
1381 command, which may be used to bypass auto-detection. */
1383 static enum packet_support
1384 packet_config_support (struct packet_config *config)
1386 switch (config->detect)
1388 case AUTO_BOOLEAN_TRUE:
1389 return PACKET_ENABLE;
1390 case AUTO_BOOLEAN_FALSE:
1391 return PACKET_DISABLE;
1392 case AUTO_BOOLEAN_AUTO:
1393 return config->support;
1395 gdb_assert_not_reached (_("bad switch"));
1399 /* Same as packet_config_support, but takes the packet's enum value as
1402 static enum packet_support
1403 packet_support (int packet)
1405 struct packet_config *config = &remote_protocol_packets[packet];
1407 return packet_config_support (config);
1411 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1412 struct cmd_list_element *c,
1415 struct packet_config *packet;
1417 for (packet = remote_protocol_packets;
1418 packet < &remote_protocol_packets[PACKET_MAX];
1421 if (&packet->detect == c->var)
1423 show_packet_config_cmd (packet);
1427 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1431 /* Should we try one of the 'Z' requests? */
1435 Z_PACKET_SOFTWARE_BP,
1436 Z_PACKET_HARDWARE_BP,
1443 /* For compatibility with older distributions. Provide a ``set remote
1444 Z-packet ...'' command that updates all the Z packet types. */
1446 static enum auto_boolean remote_Z_packet_detect;
1449 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1450 struct cmd_list_element *c)
1454 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1455 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1459 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1460 struct cmd_list_element *c,
1465 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1467 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1471 /* Returns true if the multi-process extensions are in effect. */
1474 remote_multi_process_p (struct remote_state *rs)
1476 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1479 /* Returns true if fork events are supported. */
1482 remote_fork_event_p (struct remote_state *rs)
1484 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1487 /* Returns true if vfork events are supported. */
1490 remote_vfork_event_p (struct remote_state *rs)
1492 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1495 /* Insert fork catchpoint target routine. If fork events are enabled
1496 then return success, nothing more to do. */
1499 remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1501 struct remote_state *rs = get_remote_state ();
1503 return !remote_fork_event_p (rs);
1506 /* Remove fork catchpoint target routine. Nothing to do, just
1510 remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1515 /* Insert vfork catchpoint target routine. If vfork events are enabled
1516 then return success, nothing more to do. */
1519 remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1521 struct remote_state *rs = get_remote_state ();
1523 return !remote_vfork_event_p (rs);
1526 /* Remove vfork catchpoint target routine. Nothing to do, just
1530 remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1535 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1536 static struct async_signal_handler *async_sigint_remote_twice_token;
1537 static struct async_signal_handler *async_sigint_remote_token;
1540 /* Asynchronous signal handle registered as event loop source for
1541 when we have pending events ready to be passed to the core. */
1543 static struct async_event_handler *remote_async_inferior_event_token;
1547 static ptid_t magic_null_ptid;
1548 static ptid_t not_sent_ptid;
1549 static ptid_t any_thread_ptid;
1551 /* Find out if the stub attached to PID (and hence GDB should offer to
1552 detach instead of killing it when bailing out). */
1555 remote_query_attached (int pid)
1557 struct remote_state *rs = get_remote_state ();
1558 size_t size = get_remote_packet_size ();
1560 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
1563 if (remote_multi_process_p (rs))
1564 xsnprintf (rs->buf, size, "qAttached:%x", pid);
1566 xsnprintf (rs->buf, size, "qAttached");
1569 getpkt (&rs->buf, &rs->buf_size, 0);
1571 switch (packet_ok (rs->buf,
1572 &remote_protocol_packets[PACKET_qAttached]))
1575 if (strcmp (rs->buf, "1") == 0)
1579 warning (_("Remote failure reply: %s"), rs->buf);
1581 case PACKET_UNKNOWN:
1588 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1589 has been invented by GDB, instead of reported by the target. Since
1590 we can be connected to a remote system before before knowing about
1591 any inferior, mark the target with execution when we find the first
1592 inferior. If ATTACHED is 1, then we had just attached to this
1593 inferior. If it is 0, then we just created this inferior. If it
1594 is -1, then try querying the remote stub to find out if it had
1595 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1596 attempt to open this inferior's executable as the main executable
1597 if no main executable is open already. */
1599 static struct inferior *
1600 remote_add_inferior (int fake_pid_p, int pid, int attached,
1603 struct inferior *inf;
1605 /* Check whether this process we're learning about is to be
1606 considered attached, or if is to be considered to have been
1607 spawned by the stub. */
1609 attached = remote_query_attached (pid);
1611 if (gdbarch_has_global_solist (target_gdbarch ()))
1613 /* If the target shares code across all inferiors, then every
1614 attach adds a new inferior. */
1615 inf = add_inferior (pid);
1617 /* ... and every inferior is bound to the same program space.
1618 However, each inferior may still have its own address
1620 inf->aspace = maybe_new_address_space ();
1621 inf->pspace = current_program_space;
1625 /* In the traditional debugging scenario, there's a 1-1 match
1626 between program/address spaces. We simply bind the inferior
1627 to the program space's address space. */
1628 inf = current_inferior ();
1629 inferior_appeared (inf, pid);
1632 inf->attach_flag = attached;
1633 inf->fake_pid_p = fake_pid_p;
1635 /* If no main executable is currently open then attempt to
1636 open the file that was executed to create this inferior. */
1637 if (try_open_exec && get_exec_file (0) == NULL)
1638 exec_file_locate_attach (pid, 1);
1643 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1644 according to RUNNING. */
1647 remote_add_thread (ptid_t ptid, int running)
1649 struct remote_state *rs = get_remote_state ();
1651 /* GDB historically didn't pull threads in the initial connection
1652 setup. If the remote target doesn't even have a concept of
1653 threads (e.g., a bare-metal target), even if internally we
1654 consider that a single-threaded target, mentioning a new thread
1655 might be confusing to the user. Be silent then, preserving the
1656 age old behavior. */
1657 if (rs->starting_up)
1658 add_thread_silent (ptid);
1662 set_executing (ptid, running);
1663 set_running (ptid, running);
1666 /* Come here when we learn about a thread id from the remote target.
1667 It may be the first time we hear about such thread, so take the
1668 opportunity to add it to GDB's thread list. In case this is the
1669 first time we're noticing its corresponding inferior, add it to
1670 GDB's inferior list as well. */
1673 remote_notice_new_inferior (ptid_t currthread, int running)
1675 /* If this is a new thread, add it to GDB's thread list.
1676 If we leave it up to WFI to do this, bad things will happen. */
1678 if (in_thread_list (currthread) && is_exited (currthread))
1680 /* We're seeing an event on a thread id we knew had exited.
1681 This has to be a new thread reusing the old id. Add it. */
1682 remote_add_thread (currthread, running);
1686 if (!in_thread_list (currthread))
1688 struct inferior *inf = NULL;
1689 int pid = ptid_get_pid (currthread);
1691 if (ptid_is_pid (inferior_ptid)
1692 && pid == ptid_get_pid (inferior_ptid))
1694 /* inferior_ptid has no thread member yet. This can happen
1695 with the vAttach -> remote_wait,"TAAthread:" path if the
1696 stub doesn't support qC. This is the first stop reported
1697 after an attach, so this is the main thread. Update the
1698 ptid in the thread list. */
1699 if (in_thread_list (pid_to_ptid (pid)))
1700 thread_change_ptid (inferior_ptid, currthread);
1703 remote_add_thread (currthread, running);
1704 inferior_ptid = currthread;
1709 if (ptid_equal (magic_null_ptid, inferior_ptid))
1711 /* inferior_ptid is not set yet. This can happen with the
1712 vRun -> remote_wait,"TAAthread:" path if the stub
1713 doesn't support qC. This is the first stop reported
1714 after an attach, so this is the main thread. Update the
1715 ptid in the thread list. */
1716 thread_change_ptid (inferior_ptid, currthread);
1720 /* When connecting to a target remote, or to a target
1721 extended-remote which already was debugging an inferior, we
1722 may not know about it yet. Add it before adding its child
1723 thread, so notifications are emitted in a sensible order. */
1724 if (!in_inferior_list (ptid_get_pid (currthread)))
1726 struct remote_state *rs = get_remote_state ();
1727 int fake_pid_p = !remote_multi_process_p (rs);
1729 inf = remote_add_inferior (fake_pid_p,
1730 ptid_get_pid (currthread), -1, 1);
1733 /* This is really a new thread. Add it. */
1734 remote_add_thread (currthread, running);
1736 /* If we found a new inferior, let the common code do whatever
1737 it needs to with it (e.g., read shared libraries, insert
1738 breakpoints), unless we're just setting up an all-stop
1742 struct remote_state *rs = get_remote_state ();
1744 if (non_stop || !rs->starting_up)
1745 notice_new_inferior (currthread, running, 0);
1750 /* Return the private thread data, creating it if necessary. */
1752 static struct private_thread_info *
1753 demand_private_info (ptid_t ptid)
1755 struct thread_info *info = find_thread_ptid (ptid);
1761 info->priv = xmalloc (sizeof (*(info->priv)));
1762 info->private_dtor = free_private_thread_info;
1763 info->priv->core = -1;
1764 info->priv->extra = 0;
1770 /* Call this function as a result of
1771 1) A halt indication (T packet) containing a thread id
1772 2) A direct query of currthread
1773 3) Successful execution of set thread */
1776 record_currthread (struct remote_state *rs, ptid_t currthread)
1778 rs->general_thread = currthread;
1781 /* If 'QPassSignals' is supported, tell the remote stub what signals
1782 it can simply pass through to the inferior without reporting. */
1785 remote_pass_signals (struct target_ops *self,
1786 int numsigs, unsigned char *pass_signals)
1788 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
1790 char *pass_packet, *p;
1792 struct remote_state *rs = get_remote_state ();
1794 gdb_assert (numsigs < 256);
1795 for (i = 0; i < numsigs; i++)
1797 if (pass_signals[i])
1800 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1801 strcpy (pass_packet, "QPassSignals:");
1802 p = pass_packet + strlen (pass_packet);
1803 for (i = 0; i < numsigs; i++)
1805 if (pass_signals[i])
1808 *p++ = tohex (i >> 4);
1809 *p++ = tohex (i & 15);
1818 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
1820 putpkt (pass_packet);
1821 getpkt (&rs->buf, &rs->buf_size, 0);
1822 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
1823 if (rs->last_pass_packet)
1824 xfree (rs->last_pass_packet);
1825 rs->last_pass_packet = pass_packet;
1828 xfree (pass_packet);
1832 /* If 'QProgramSignals' is supported, tell the remote stub what
1833 signals it should pass through to the inferior when detaching. */
1836 remote_program_signals (struct target_ops *self,
1837 int numsigs, unsigned char *signals)
1839 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
1843 struct remote_state *rs = get_remote_state ();
1845 gdb_assert (numsigs < 256);
1846 for (i = 0; i < numsigs; i++)
1851 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1852 strcpy (packet, "QProgramSignals:");
1853 p = packet + strlen (packet);
1854 for (i = 0; i < numsigs; i++)
1856 if (signal_pass_state (i))
1859 *p++ = tohex (i >> 4);
1860 *p++ = tohex (i & 15);
1869 if (!rs->last_program_signals_packet
1870 || strcmp (rs->last_program_signals_packet, packet) != 0)
1873 getpkt (&rs->buf, &rs->buf_size, 0);
1874 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1875 xfree (rs->last_program_signals_packet);
1876 rs->last_program_signals_packet = packet;
1883 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1884 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1885 thread. If GEN is set, set the general thread, if not, then set
1886 the step/continue thread. */
1888 set_thread (struct ptid ptid, int gen)
1890 struct remote_state *rs = get_remote_state ();
1891 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
1892 char *buf = rs->buf;
1893 char *endbuf = rs->buf + get_remote_packet_size ();
1895 if (ptid_equal (state, ptid))
1899 *buf++ = gen ? 'g' : 'c';
1900 if (ptid_equal (ptid, magic_null_ptid))
1901 xsnprintf (buf, endbuf - buf, "0");
1902 else if (ptid_equal (ptid, any_thread_ptid))
1903 xsnprintf (buf, endbuf - buf, "0");
1904 else if (ptid_equal (ptid, minus_one_ptid))
1905 xsnprintf (buf, endbuf - buf, "-1");
1907 write_ptid (buf, endbuf, ptid);
1909 getpkt (&rs->buf, &rs->buf_size, 0);
1911 rs->general_thread = ptid;
1913 rs->continue_thread = ptid;
1917 set_general_thread (struct ptid ptid)
1919 set_thread (ptid, 1);
1923 set_continue_thread (struct ptid ptid)
1925 set_thread (ptid, 0);
1928 /* Change the remote current process. Which thread within the process
1929 ends up selected isn't important, as long as it is the same process
1930 as what INFERIOR_PTID points to.
1932 This comes from that fact that there is no explicit notion of
1933 "selected process" in the protocol. The selected process for
1934 general operations is the process the selected general thread
1938 set_general_process (void)
1940 struct remote_state *rs = get_remote_state ();
1942 /* If the remote can't handle multiple processes, don't bother. */
1943 if (!rs->extended || !remote_multi_process_p (rs))
1946 /* We only need to change the remote current thread if it's pointing
1947 at some other process. */
1948 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
1949 set_general_thread (inferior_ptid);
1953 /* Return nonzero if this is the main thread that we made up ourselves
1954 to model non-threaded targets as single-threaded. */
1957 remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
1959 struct remote_state *rs = get_remote_state ();
1962 if (ptid_equal (ptid, magic_null_ptid))
1963 /* The main thread is always alive. */
1966 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
1967 /* The main thread is always alive. This can happen after a
1968 vAttach, if the remote side doesn't support
1975 /* Return nonzero if the thread PTID is still alive on the remote
1979 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1981 struct remote_state *rs = get_remote_state ();
1984 /* Check if this is a thread that we made up ourselves to model
1985 non-threaded targets as single-threaded. */
1986 if (remote_thread_always_alive (ops, ptid))
1990 endp = rs->buf + get_remote_packet_size ();
1993 write_ptid (p, endp, ptid);
1996 getpkt (&rs->buf, &rs->buf_size, 0);
1997 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
2000 /* About these extended threadlist and threadinfo packets. They are
2001 variable length packets but, the fields within them are often fixed
2002 length. They are redundent enough to send over UDP as is the
2003 remote protocol in general. There is a matching unit test module
2006 /* WARNING: This threadref data structure comes from the remote O.S.,
2007 libstub protocol encoding, and remote.c. It is not particularly
2010 /* Right now, the internal structure is int. We want it to be bigger.
2011 Plan to fix this. */
2013 typedef int gdb_threadref; /* Internal GDB thread reference. */
2015 /* gdb_ext_thread_info is an internal GDB data structure which is
2016 equivalent to the reply of the remote threadinfo packet. */
2018 struct gdb_ext_thread_info
2020 threadref threadid; /* External form of thread reference. */
2021 int active; /* Has state interesting to GDB?
2023 char display[256]; /* Brief state display, name,
2024 blocked/suspended. */
2025 char shortname[32]; /* To be used to name threads. */
2026 char more_display[256]; /* Long info, statistics, queue depth,
2030 /* The volume of remote transfers can be limited by submitting
2031 a mask containing bits specifying the desired information.
2032 Use a union of these values as the 'selection' parameter to
2033 get_thread_info. FIXME: Make these TAG names more thread specific. */
2035 #define TAG_THREADID 1
2036 #define TAG_EXISTS 2
2037 #define TAG_DISPLAY 4
2038 #define TAG_THREADNAME 8
2039 #define TAG_MOREDISPLAY 16
2041 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
2043 static char *unpack_nibble (char *buf, int *val);
2045 static char *unpack_byte (char *buf, int *value);
2047 static char *pack_int (char *buf, int value);
2049 static char *unpack_int (char *buf, int *value);
2051 static char *unpack_string (char *src, char *dest, int length);
2053 static char *pack_threadid (char *pkt, threadref *id);
2055 static char *unpack_threadid (char *inbuf, threadref *id);
2057 void int_to_threadref (threadref *id, int value);
2059 static int threadref_to_int (threadref *ref);
2061 static void copy_threadref (threadref *dest, threadref *src);
2063 static int threadmatch (threadref *dest, threadref *src);
2065 static char *pack_threadinfo_request (char *pkt, int mode,
2068 static int remote_unpack_thread_info_response (char *pkt,
2069 threadref *expectedref,
2070 struct gdb_ext_thread_info
2074 static int remote_get_threadinfo (threadref *threadid,
2075 int fieldset, /*TAG mask */
2076 struct gdb_ext_thread_info *info);
2078 static char *pack_threadlist_request (char *pkt, int startflag,
2080 threadref *nextthread);
2082 static int parse_threadlist_response (char *pkt,
2084 threadref *original_echo,
2085 threadref *resultlist,
2088 static int remote_get_threadlist (int startflag,
2089 threadref *nextthread,
2093 threadref *threadlist);
2095 typedef int (*rmt_thread_action) (threadref *ref, void *context);
2097 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2098 void *context, int looplimit);
2100 static int remote_newthread_step (threadref *ref, void *context);
2103 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2104 buffer we're allowed to write to. Returns
2105 BUF+CHARACTERS_WRITTEN. */
2108 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2111 struct remote_state *rs = get_remote_state ();
2113 if (remote_multi_process_p (rs))
2115 pid = ptid_get_pid (ptid);
2117 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2119 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2121 tid = ptid_get_lwp (ptid);
2123 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2125 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2130 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2131 passed the last parsed char. Returns null_ptid on error. */
2134 read_ptid (char *buf, char **obuf)
2138 ULONGEST pid = 0, tid = 0;
2142 /* Multi-process ptid. */
2143 pp = unpack_varlen_hex (p + 1, &pid);
2145 error (_("invalid remote ptid: %s"), p);
2148 pp = unpack_varlen_hex (p + 1, &tid);
2151 return ptid_build (pid, tid, 0);
2154 /* No multi-process. Just a tid. */
2155 pp = unpack_varlen_hex (p, &tid);
2157 /* Since the stub is not sending a process id, then default to
2158 what's in inferior_ptid, unless it's null at this point. If so,
2159 then since there's no way to know the pid of the reported
2160 threads, use the magic number. */
2161 if (ptid_equal (inferior_ptid, null_ptid))
2162 pid = ptid_get_pid (magic_null_ptid);
2164 pid = ptid_get_pid (inferior_ptid);
2168 return ptid_build (pid, tid, 0);
2174 if (ch >= 'a' && ch <= 'f')
2175 return ch - 'a' + 10;
2176 if (ch >= '0' && ch <= '9')
2178 if (ch >= 'A' && ch <= 'F')
2179 return ch - 'A' + 10;
2184 stub_unpack_int (char *buff, int fieldlength)
2191 nibble = stubhex (*buff++);
2195 retval = retval << 4;
2201 unpack_nibble (char *buf, int *val)
2203 *val = fromhex (*buf++);
2208 unpack_byte (char *buf, int *value)
2210 *value = stub_unpack_int (buf, 2);
2215 pack_int (char *buf, int value)
2217 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2218 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2219 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2220 buf = pack_hex_byte (buf, (value & 0xff));
2225 unpack_int (char *buf, int *value)
2227 *value = stub_unpack_int (buf, 8);
2231 #if 0 /* Currently unused, uncomment when needed. */
2232 static char *pack_string (char *pkt, char *string);
2235 pack_string (char *pkt, char *string)
2240 len = strlen (string);
2242 len = 200; /* Bigger than most GDB packets, junk??? */
2243 pkt = pack_hex_byte (pkt, len);
2247 if ((ch == '\0') || (ch == '#'))
2248 ch = '*'; /* Protect encapsulation. */
2253 #endif /* 0 (unused) */
2256 unpack_string (char *src, char *dest, int length)
2265 pack_threadid (char *pkt, threadref *id)
2268 unsigned char *altid;
2270 altid = (unsigned char *) id;
2271 limit = pkt + BUF_THREAD_ID_SIZE;
2273 pkt = pack_hex_byte (pkt, *altid++);
2279 unpack_threadid (char *inbuf, threadref *id)
2282 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2285 altref = (char *) id;
2287 while (inbuf < limit)
2289 x = stubhex (*inbuf++);
2290 y = stubhex (*inbuf++);
2291 *altref++ = (x << 4) | y;
2296 /* Externally, threadrefs are 64 bits but internally, they are still
2297 ints. This is due to a mismatch of specifications. We would like
2298 to use 64bit thread references internally. This is an adapter
2302 int_to_threadref (threadref *id, int value)
2304 unsigned char *scan;
2306 scan = (unsigned char *) id;
2312 *scan++ = (value >> 24) & 0xff;
2313 *scan++ = (value >> 16) & 0xff;
2314 *scan++ = (value >> 8) & 0xff;
2315 *scan++ = (value & 0xff);
2319 threadref_to_int (threadref *ref)
2322 unsigned char *scan;
2328 value = (value << 8) | ((*scan++) & 0xff);
2333 copy_threadref (threadref *dest, threadref *src)
2336 unsigned char *csrc, *cdest;
2338 csrc = (unsigned char *) src;
2339 cdest = (unsigned char *) dest;
2346 threadmatch (threadref *dest, threadref *src)
2348 /* Things are broken right now, so just assume we got a match. */
2350 unsigned char *srcp, *destp;
2352 srcp = (char *) src;
2353 destp = (char *) dest;
2357 result &= (*srcp++ == *destp++) ? 1 : 0;
2364 threadid:1, # always request threadid
2371 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2374 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2376 *pkt++ = 'q'; /* Info Query */
2377 *pkt++ = 'P'; /* process or thread info */
2378 pkt = pack_int (pkt, mode); /* mode */
2379 pkt = pack_threadid (pkt, id); /* threadid */
2380 *pkt = '\0'; /* terminate */
2384 /* These values tag the fields in a thread info response packet. */
2385 /* Tagging the fields allows us to request specific fields and to
2386 add more fields as time goes by. */
2388 #define TAG_THREADID 1 /* Echo the thread identifier. */
2389 #define TAG_EXISTS 2 /* Is this process defined enough to
2390 fetch registers and its stack? */
2391 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2392 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2393 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2397 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2398 struct gdb_ext_thread_info *info)
2400 struct remote_state *rs = get_remote_state ();
2404 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2407 /* info->threadid = 0; FIXME: implement zero_threadref. */
2409 info->display[0] = '\0';
2410 info->shortname[0] = '\0';
2411 info->more_display[0] = '\0';
2413 /* Assume the characters indicating the packet type have been
2415 pkt = unpack_int (pkt, &mask); /* arg mask */
2416 pkt = unpack_threadid (pkt, &ref);
2419 warning (_("Incomplete response to threadinfo request."));
2420 if (!threadmatch (&ref, expectedref))
2421 { /* This is an answer to a different request. */
2422 warning (_("ERROR RMT Thread info mismatch."));
2425 copy_threadref (&info->threadid, &ref);
2427 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2429 /* Packets are terminated with nulls. */
2430 while ((pkt < limit) && mask && *pkt)
2432 pkt = unpack_int (pkt, &tag); /* tag */
2433 pkt = unpack_byte (pkt, &length); /* length */
2434 if (!(tag & mask)) /* Tags out of synch with mask. */
2436 warning (_("ERROR RMT: threadinfo tag mismatch."));
2440 if (tag == TAG_THREADID)
2444 warning (_("ERROR RMT: length of threadid is not 16."));
2448 pkt = unpack_threadid (pkt, &ref);
2449 mask = mask & ~TAG_THREADID;
2452 if (tag == TAG_EXISTS)
2454 info->active = stub_unpack_int (pkt, length);
2456 mask = mask & ~(TAG_EXISTS);
2459 warning (_("ERROR RMT: 'exists' length too long."));
2465 if (tag == TAG_THREADNAME)
2467 pkt = unpack_string (pkt, &info->shortname[0], length);
2468 mask = mask & ~TAG_THREADNAME;
2471 if (tag == TAG_DISPLAY)
2473 pkt = unpack_string (pkt, &info->display[0], length);
2474 mask = mask & ~TAG_DISPLAY;
2477 if (tag == TAG_MOREDISPLAY)
2479 pkt = unpack_string (pkt, &info->more_display[0], length);
2480 mask = mask & ~TAG_MOREDISPLAY;
2483 warning (_("ERROR RMT: unknown thread info tag."));
2484 break; /* Not a tag we know about. */
2490 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2491 struct gdb_ext_thread_info *info)
2493 struct remote_state *rs = get_remote_state ();
2496 pack_threadinfo_request (rs->buf, fieldset, threadid);
2498 getpkt (&rs->buf, &rs->buf_size, 0);
2500 if (rs->buf[0] == '\0')
2503 result = remote_unpack_thread_info_response (rs->buf + 2,
2508 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2511 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2512 threadref *nextthread)
2514 *pkt++ = 'q'; /* info query packet */
2515 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2516 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2517 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2518 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2523 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2526 parse_threadlist_response (char *pkt, int result_limit,
2527 threadref *original_echo, threadref *resultlist,
2530 struct remote_state *rs = get_remote_state ();
2532 int count, resultcount, done;
2535 /* Assume the 'q' and 'M chars have been stripped. */
2536 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2537 /* done parse past here */
2538 pkt = unpack_byte (pkt, &count); /* count field */
2539 pkt = unpack_nibble (pkt, &done);
2540 /* The first threadid is the argument threadid. */
2541 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2542 while ((count-- > 0) && (pkt < limit))
2544 pkt = unpack_threadid (pkt, resultlist++);
2545 if (resultcount++ >= result_limit)
2553 /* Fetch the next batch of threads from the remote. Returns -1 if the
2554 qL packet is not supported, 0 on error and 1 on success. */
2557 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2558 int *done, int *result_count, threadref *threadlist)
2560 struct remote_state *rs = get_remote_state ();
2563 /* Trancate result limit to be smaller than the packet size. */
2564 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2565 >= get_remote_packet_size ())
2566 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2568 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2570 getpkt (&rs->buf, &rs->buf_size, 0);
2571 if (*rs->buf == '\0')
2573 /* Packet not supported. */
2578 parse_threadlist_response (rs->buf + 2, result_limit,
2579 &rs->echo_nextthread, threadlist, done);
2581 if (!threadmatch (&rs->echo_nextthread, nextthread))
2583 /* FIXME: This is a good reason to drop the packet. */
2584 /* Possably, there is a duplicate response. */
2586 retransmit immediatly - race conditions
2587 retransmit after timeout - yes
2589 wait for packet, then exit
2591 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2592 return 0; /* I choose simply exiting. */
2594 if (*result_count <= 0)
2598 warning (_("RMT ERROR : failed to get remote thread list."));
2601 return result; /* break; */
2603 if (*result_count > result_limit)
2606 warning (_("RMT ERROR: threadlist response longer than requested."));
2612 /* Fetch the list of remote threads, with the qL packet, and call
2613 STEPFUNCTION for each thread found. Stops iterating and returns 1
2614 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2615 STEPFUNCTION returns false. If the packet is not supported,
2619 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2622 struct remote_state *rs = get_remote_state ();
2623 int done, i, result_count;
2631 if (loopcount++ > looplimit)
2634 warning (_("Remote fetch threadlist -infinite loop-."));
2637 result = remote_get_threadlist (startflag, &rs->nextthread,
2638 MAXTHREADLISTRESULTS,
2639 &done, &result_count,
2640 rs->resultthreadlist);
2643 /* Clear for later iterations. */
2645 /* Setup to resume next batch of thread references, set nextthread. */
2646 if (result_count >= 1)
2647 copy_threadref (&rs->nextthread,
2648 &rs->resultthreadlist[result_count - 1]);
2650 while (result_count--)
2652 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2662 /* A thread found on the remote target. */
2664 typedef struct thread_item
2666 /* The thread's PTID. */
2669 /* The thread's extra info. May be NULL. */
2672 /* The core the thread was running on. -1 if not known. */
2675 DEF_VEC_O(thread_item_t);
2677 /* Context passed around to the various methods listing remote
2678 threads. As new threads are found, they're added to the ITEMS
2681 struct threads_listing_context
2683 /* The threads found on the remote target. */
2684 VEC (thread_item_t) *items;
2687 /* Discard the contents of the constructed thread listing context. */
2690 clear_threads_listing_context (void *p)
2692 struct threads_listing_context *context = p;
2694 struct thread_item *item;
2696 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2697 xfree (item->extra);
2699 VEC_free (thread_item_t, context->items);
2702 /* Remove the thread specified as the related_pid field of WS
2703 from the CONTEXT list. */
2706 threads_listing_context_remove (struct target_waitstatus *ws,
2707 struct threads_listing_context *context)
2709 struct thread_item *item;
2711 ptid_t child_ptid = ws->value.related_pid;
2713 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2715 if (ptid_equal (item->ptid, child_ptid))
2717 VEC_ordered_remove (thread_item_t, context->items, i);
2724 remote_newthread_step (threadref *ref, void *data)
2726 struct threads_listing_context *context = data;
2727 struct thread_item item;
2728 int pid = ptid_get_pid (inferior_ptid);
2730 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2734 VEC_safe_push (thread_item_t, context->items, &item);
2736 return 1; /* continue iterator */
2739 #define CRAZY_MAX_THREADS 1000
2742 remote_current_thread (ptid_t oldpid)
2744 struct remote_state *rs = get_remote_state ();
2747 getpkt (&rs->buf, &rs->buf_size, 0);
2748 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2749 return read_ptid (&rs->buf[2], NULL);
2754 /* List remote threads using the deprecated qL packet. */
2757 remote_get_threads_with_ql (struct target_ops *ops,
2758 struct threads_listing_context *context)
2760 if (remote_threadlist_iterator (remote_newthread_step, context,
2761 CRAZY_MAX_THREADS) >= 0)
2767 #if defined(HAVE_LIBEXPAT)
2770 start_thread (struct gdb_xml_parser *parser,
2771 const struct gdb_xml_element *element,
2772 void *user_data, VEC(gdb_xml_value_s) *attributes)
2774 struct threads_listing_context *data = user_data;
2776 struct thread_item item;
2778 struct gdb_xml_value *attr;
2780 id = xml_find_attribute (attributes, "id")->value;
2781 item.ptid = read_ptid (id, NULL);
2783 attr = xml_find_attribute (attributes, "core");
2785 item.core = *(ULONGEST *) attr->value;
2791 VEC_safe_push (thread_item_t, data->items, &item);
2795 end_thread (struct gdb_xml_parser *parser,
2796 const struct gdb_xml_element *element,
2797 void *user_data, const char *body_text)
2799 struct threads_listing_context *data = user_data;
2801 if (body_text && *body_text)
2802 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2805 const struct gdb_xml_attribute thread_attributes[] = {
2806 { "id", GDB_XML_AF_NONE, NULL, NULL },
2807 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2808 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2811 const struct gdb_xml_element thread_children[] = {
2812 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2815 const struct gdb_xml_element threads_children[] = {
2816 { "thread", thread_attributes, thread_children,
2817 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2818 start_thread, end_thread },
2819 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2822 const struct gdb_xml_element threads_elements[] = {
2823 { "threads", NULL, threads_children,
2824 GDB_XML_EF_NONE, NULL, NULL },
2825 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2830 /* List remote threads using qXfer:threads:read. */
2833 remote_get_threads_with_qxfer (struct target_ops *ops,
2834 struct threads_listing_context *context)
2836 #if defined(HAVE_LIBEXPAT)
2837 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
2839 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
2840 struct cleanup *back_to = make_cleanup (xfree, xml);
2842 if (xml != NULL && *xml != '\0')
2844 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2845 threads_elements, xml, context);
2848 do_cleanups (back_to);
2856 /* List remote threads using qfThreadInfo/qsThreadInfo. */
2859 remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2860 struct threads_listing_context *context)
2862 struct remote_state *rs = get_remote_state ();
2864 if (rs->use_threadinfo_query)
2868 putpkt ("qfThreadInfo");
2869 getpkt (&rs->buf, &rs->buf_size, 0);
2871 if (bufp[0] != '\0') /* q packet recognized */
2873 while (*bufp++ == 'm') /* reply contains one or more TID */
2877 struct thread_item item;
2879 item.ptid = read_ptid (bufp, &bufp);
2883 VEC_safe_push (thread_item_t, context->items, &item);
2885 while (*bufp++ == ','); /* comma-separated list */
2886 putpkt ("qsThreadInfo");
2887 getpkt (&rs->buf, &rs->buf_size, 0);
2894 /* Packet not recognized. */
2895 rs->use_threadinfo_query = 0;
2902 /* Implement the to_update_thread_list function for the remote
2906 remote_update_thread_list (struct target_ops *ops)
2908 struct remote_state *rs = get_remote_state ();
2909 struct threads_listing_context context;
2910 struct cleanup *old_chain;
2913 context.items = NULL;
2914 old_chain = make_cleanup (clear_threads_listing_context, &context);
2916 /* We have a few different mechanisms to fetch the thread list. Try
2917 them all, starting with the most preferred one first, falling
2918 back to older methods. */
2919 if (remote_get_threads_with_qxfer (ops, &context)
2920 || remote_get_threads_with_qthreadinfo (ops, &context)
2921 || remote_get_threads_with_ql (ops, &context))
2924 struct thread_item *item;
2925 struct thread_info *tp, *tmp;
2929 if (VEC_empty (thread_item_t, context.items)
2930 && remote_thread_always_alive (ops, inferior_ptid))
2932 /* Some targets don't really support threads, but still
2933 reply an (empty) thread list in response to the thread
2934 listing packets, instead of replying "packet not
2935 supported". Exit early so we don't delete the main
2937 do_cleanups (old_chain);
2941 /* CONTEXT now holds the current thread list on the remote
2942 target end. Delete GDB-side threads no longer found on the
2944 ALL_THREADS_SAFE (tp, tmp)
2947 VEC_iterate (thread_item_t, context.items, i, item);
2950 if (ptid_equal (item->ptid, tp->ptid))
2954 if (i == VEC_length (thread_item_t, context.items))
2957 delete_thread (tp->ptid);
2961 /* Remove any unreported fork child threads from CONTEXT so
2962 that we don't interfere with follow fork, which is where
2963 creation of such threads is handled. */
2964 remove_new_fork_children (&context);
2966 /* And now add threads we don't know about yet to our list. */
2968 VEC_iterate (thread_item_t, context.items, i, item);
2971 if (!ptid_equal (item->ptid, null_ptid))
2973 struct private_thread_info *info;
2974 /* In non-stop mode, we assume new found threads are
2975 running until proven otherwise with a stop reply. In
2976 all-stop, we can only get here if all threads are
2978 int running = non_stop ? 1 : 0;
2980 remote_notice_new_inferior (item->ptid, running);
2982 info = demand_private_info (item->ptid);
2983 info->core = item->core;
2984 info->extra = item->extra;
2992 /* If no thread listing method is supported, then query whether
2993 each known thread is alive, one by one, with the T packet.
2994 If the target doesn't support threads at all, then this is a
2995 no-op. See remote_thread_alive. */
2999 do_cleanups (old_chain);
3003 * Collect a descriptive string about the given thread.
3004 * The target may say anything it wants to about the thread
3005 * (typically info about its blocked / runnable state, name, etc.).
3006 * This string will appear in the info threads display.
3008 * Optional: targets are not required to implement this function.
3012 remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
3014 struct remote_state *rs = get_remote_state ();
3018 struct gdb_ext_thread_info threadinfo;
3019 static char display_buf[100]; /* arbitrary... */
3020 int n = 0; /* position in display_buf */
3022 if (rs->remote_desc == 0) /* paranoia */
3023 internal_error (__FILE__, __LINE__,
3024 _("remote_threads_extra_info"));
3026 if (ptid_equal (tp->ptid, magic_null_ptid)
3027 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
3028 /* This is the main thread which was added by GDB. The remote
3029 server doesn't know about it. */
3032 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
3034 struct thread_info *info = find_thread_ptid (tp->ptid);
3036 if (info && info->priv)
3037 return info->priv->extra;
3042 if (rs->use_threadextra_query)
3045 char *endb = rs->buf + get_remote_packet_size ();
3047 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3049 write_ptid (b, endb, tp->ptid);
3052 getpkt (&rs->buf, &rs->buf_size, 0);
3053 if (rs->buf[0] != 0)
3055 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3056 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
3057 display_buf [result] = '\0';
3062 /* If the above query fails, fall back to the old method. */
3063 rs->use_threadextra_query = 0;
3064 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3065 | TAG_MOREDISPLAY | TAG_DISPLAY;
3066 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
3067 if (remote_get_threadinfo (&id, set, &threadinfo))
3068 if (threadinfo.active)
3070 if (*threadinfo.shortname)
3071 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
3072 " Name: %s,", threadinfo.shortname);
3073 if (*threadinfo.display)
3074 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
3075 " State: %s,", threadinfo.display);
3076 if (*threadinfo.more_display)
3077 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
3078 " Priority: %s", threadinfo.more_display);
3082 /* For purely cosmetic reasons, clear up trailing commas. */
3083 if (',' == display_buf[n-1])
3084 display_buf[n-1] = ' ';
3093 remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
3094 struct static_tracepoint_marker *marker)
3096 struct remote_state *rs = get_remote_state ();
3099 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
3101 p += hexnumstr (p, addr);
3103 getpkt (&rs->buf, &rs->buf_size, 0);
3107 error (_("Remote failure reply: %s"), p);
3111 parse_static_tracepoint_marker_definition (p, &p, marker);
3118 static VEC(static_tracepoint_marker_p) *
3119 remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3122 struct remote_state *rs = get_remote_state ();
3123 VEC(static_tracepoint_marker_p) *markers = NULL;
3124 struct static_tracepoint_marker *marker = NULL;
3125 struct cleanup *old_chain;
3128 /* Ask for a first packet of static tracepoint marker
3131 getpkt (&rs->buf, &rs->buf_size, 0);
3134 error (_("Remote failure reply: %s"), p);
3136 old_chain = make_cleanup (free_current_marker, &marker);
3141 marker = XCNEW (struct static_tracepoint_marker);
3145 parse_static_tracepoint_marker_definition (p, &p, marker);
3147 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3149 VEC_safe_push (static_tracepoint_marker_p,
3155 release_static_tracepoint_marker (marker);
3156 memset (marker, 0, sizeof (*marker));
3159 while (*p++ == ','); /* comma-separated list */
3160 /* Ask for another packet of static tracepoint definition. */
3162 getpkt (&rs->buf, &rs->buf_size, 0);
3166 do_cleanups (old_chain);
3171 /* Implement the to_get_ada_task_ptid function for the remote targets. */
3174 remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
3176 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
3180 /* Restart the remote side; this is an extended protocol operation. */
3183 extended_remote_restart (void)
3185 struct remote_state *rs = get_remote_state ();
3187 /* Send the restart command; for reasons I don't understand the
3188 remote side really expects a number after the "R". */
3189 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
3192 remote_fileio_reset ();
3195 /* Clean up connection to a remote debugger. */
3198 remote_close (struct target_ops *self)
3200 struct remote_state *rs = get_remote_state ();
3202 if (rs->remote_desc == NULL)
3203 return; /* already closed */
3205 /* Make sure we leave stdin registered in the event loop, and we
3206 don't leave the async SIGINT signal handler installed. */
3207 remote_terminal_ours (self);
3209 serial_close (rs->remote_desc);
3210 rs->remote_desc = NULL;
3212 /* We don't have a connection to the remote stub anymore. Get rid
3213 of all the inferiors and their threads we were controlling.
3214 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3215 will be unable to find the thread corresponding to (pid, 0, 0). */
3216 inferior_ptid = null_ptid;
3217 discard_all_inferiors ();
3219 /* We are closing the remote target, so we should discard
3220 everything of this target. */
3221 discard_pending_stop_replies_in_queue (rs);
3223 if (remote_async_inferior_event_token)
3224 delete_async_event_handler (&remote_async_inferior_event_token);
3226 remote_notif_state_xfree (rs->notif_state);
3228 trace_reset_local_state ();
3231 /* Query the remote side for the text, data and bss offsets. */
3236 struct remote_state *rs = get_remote_state ();
3239 int lose, num_segments = 0, do_sections, do_segments;
3240 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
3241 struct section_offsets *offs;
3242 struct symfile_segment_data *data;
3244 if (symfile_objfile == NULL)
3247 putpkt ("qOffsets");
3248 getpkt (&rs->buf, &rs->buf_size, 0);
3251 if (buf[0] == '\000')
3252 return; /* Return silently. Stub doesn't support
3256 warning (_("Remote failure reply: %s"), buf);
3260 /* Pick up each field in turn. This used to be done with scanf, but
3261 scanf will make trouble if CORE_ADDR size doesn't match
3262 conversion directives correctly. The following code will work
3263 with any size of CORE_ADDR. */
3264 text_addr = data_addr = bss_addr = 0;
3268 if (startswith (ptr, "Text="))
3271 /* Don't use strtol, could lose on big values. */
3272 while (*ptr && *ptr != ';')
3273 text_addr = (text_addr << 4) + fromhex (*ptr++);
3275 if (startswith (ptr, ";Data="))
3278 while (*ptr && *ptr != ';')
3279 data_addr = (data_addr << 4) + fromhex (*ptr++);
3284 if (!lose && startswith (ptr, ";Bss="))
3287 while (*ptr && *ptr != ';')
3288 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
3290 if (bss_addr != data_addr)
3291 warning (_("Target reported unsupported offsets: %s"), buf);
3296 else if (startswith (ptr, "TextSeg="))
3299 /* Don't use strtol, could lose on big values. */
3300 while (*ptr && *ptr != ';')
3301 text_addr = (text_addr << 4) + fromhex (*ptr++);
3304 if (startswith (ptr, ";DataSeg="))
3307 while (*ptr && *ptr != ';')
3308 data_addr = (data_addr << 4) + fromhex (*ptr++);
3316 error (_("Malformed response to offset query, %s"), buf);
3317 else if (*ptr != '\0')
3318 warning (_("Target reported unsupported offsets: %s"), buf);
3320 offs = ((struct section_offsets *)
3321 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3322 memcpy (offs, symfile_objfile->section_offsets,
3323 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3325 data = get_symfile_segment_data (symfile_objfile->obfd);
3326 do_segments = (data != NULL);
3327 do_sections = num_segments == 0;
3329 if (num_segments > 0)
3331 segments[0] = text_addr;
3332 segments[1] = data_addr;
3334 /* If we have two segments, we can still try to relocate everything
3335 by assuming that the .text and .data offsets apply to the whole
3336 text and data segments. Convert the offsets given in the packet
3337 to base addresses for symfile_map_offsets_to_segments. */
3338 else if (data && data->num_segments == 2)
3340 segments[0] = data->segment_bases[0] + text_addr;
3341 segments[1] = data->segment_bases[1] + data_addr;
3344 /* If the object file has only one segment, assume that it is text
3345 rather than data; main programs with no writable data are rare,
3346 but programs with no code are useless. Of course the code might
3347 have ended up in the data segment... to detect that we would need
3348 the permissions here. */
3349 else if (data && data->num_segments == 1)
3351 segments[0] = data->segment_bases[0] + text_addr;
3354 /* There's no way to relocate by segment. */
3360 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3361 offs, num_segments, segments);
3363 if (ret == 0 && !do_sections)
3364 error (_("Can not handle qOffsets TextSeg "
3365 "response with this symbol file"));
3372 free_symfile_segment_data (data);
3376 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3378 /* This is a temporary kludge to force data and bss to use the
3379 same offsets because that's what nlmconv does now. The real
3380 solution requires changes to the stub and remote.c that I
3381 don't have time to do right now. */
3383 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3384 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3387 objfile_relocate (symfile_objfile, offs);
3390 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3391 threads we know are stopped already. This is used during the
3392 initial remote connection in non-stop mode --- threads that are
3393 reported as already being stopped are left stopped. */
3396 set_stop_requested_callback (struct thread_info *thread, void *data)
3398 /* If we have a stop reply for this thread, it must be stopped. */
3399 if (peek_stop_reply (thread->ptid))
3400 set_stop_requested (thread->ptid, 1);
3405 /* Send interrupt_sequence to remote target. */
3407 send_interrupt_sequence (void)
3409 struct remote_state *rs = get_remote_state ();
3411 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3412 remote_serial_write ("\x03", 1);
3413 else if (interrupt_sequence_mode == interrupt_sequence_break)
3414 serial_send_break (rs->remote_desc);
3415 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3417 serial_send_break (rs->remote_desc);
3418 remote_serial_write ("g", 1);
3421 internal_error (__FILE__, __LINE__,
3422 _("Invalid value for interrupt_sequence_mode: %s."),
3423 interrupt_sequence_mode);
3427 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3428 and extract the PTID. Returns NULL_PTID if not found. */
3431 stop_reply_extract_thread (char *stop_reply)
3433 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3437 /* Txx r:val ; r:val (...) */
3440 /* Look for "register" named "thread". */
3445 p1 = strchr (p, ':');
3449 if (strncmp (p, "thread", p1 - p) == 0)
3450 return read_ptid (++p1, &p);
3452 p1 = strchr (p, ';');
3464 /* Determine the remote side's current thread. If we have a stop
3465 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3466 "thread" register we can extract the current thread from. If not,
3467 ask the remote which is the current thread with qC. The former
3468 method avoids a roundtrip. */
3471 get_current_thread (char *wait_status)
3475 /* Note we don't use remote_parse_stop_reply as that makes use of
3476 the target architecture, which we haven't yet fully determined at
3478 if (wait_status != NULL)
3479 ptid = stop_reply_extract_thread (wait_status);
3480 if (ptid_equal (ptid, null_ptid))
3481 ptid = remote_current_thread (inferior_ptid);
3486 /* Query the remote target for which is the current thread/process,
3487 add it to our tables, and update INFERIOR_PTID. The caller is
3488 responsible for setting the state such that the remote end is ready
3489 to return the current thread.
3491 This function is called after handling the '?' or 'vRun' packets,
3492 whose response is a stop reply from which we can also try
3493 extracting the thread. If the target doesn't support the explicit
3494 qC query, we infer the current thread from that stop reply, passed
3495 in in WAIT_STATUS, which may be NULL. */
3498 add_current_inferior_and_thread (char *wait_status)
3500 struct remote_state *rs = get_remote_state ();
3502 ptid_t ptid = null_ptid;
3504 inferior_ptid = null_ptid;
3506 /* Now, if we have thread information, update inferior_ptid. */
3507 ptid = get_current_thread (wait_status);
3509 if (!ptid_equal (ptid, null_ptid))
3511 if (!remote_multi_process_p (rs))
3514 inferior_ptid = ptid;
3518 /* Without this, some commands which require an active target
3519 (such as kill) won't work. This variable serves (at least)
3520 double duty as both the pid of the target process (if it has
3521 such), and as a flag indicating that a target is active. */
3522 inferior_ptid = magic_null_ptid;
3526 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
3528 /* Add the main thread. */
3529 add_thread_silent (inferior_ptid);
3533 remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
3535 struct remote_state *rs = get_remote_state ();
3536 struct packet_config *noack_config;
3537 char *wait_status = NULL;
3539 immediate_quit++; /* Allow user to interrupt it. */
3542 if (interrupt_on_connect)
3543 send_interrupt_sequence ();
3545 /* Ack any packet which the remote side has already sent. */
3546 serial_write (rs->remote_desc, "+", 1);
3548 /* Signal other parts that we're going through the initial setup,
3549 and so things may not be stable yet. */
3550 rs->starting_up = 1;
3552 /* The first packet we send to the target is the optional "supported
3553 packets" request. If the target can answer this, it will tell us
3554 which later probes to skip. */
3555 remote_query_supported ();
3557 /* If the stub wants to get a QAllow, compose one and send it. */
3558 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
3559 remote_set_permissions (target);
3561 /* Next, we possibly activate noack mode.
3563 If the QStartNoAckMode packet configuration is set to AUTO,
3564 enable noack mode if the stub reported a wish for it with
3567 If set to TRUE, then enable noack mode even if the stub didn't
3568 report it in qSupported. If the stub doesn't reply OK, the
3569 session ends with an error.
3571 If FALSE, then don't activate noack mode, regardless of what the
3572 stub claimed should be the default with qSupported. */
3574 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3575 if (packet_config_support (noack_config) != PACKET_DISABLE)
3577 putpkt ("QStartNoAckMode");
3578 getpkt (&rs->buf, &rs->buf_size, 0);
3579 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3585 /* Tell the remote that we are using the extended protocol. */
3587 getpkt (&rs->buf, &rs->buf_size, 0);
3590 /* Let the target know which signals it is allowed to pass down to
3592 update_signals_program_target ();
3594 /* Next, if the target can specify a description, read it. We do
3595 this before anything involving memory or registers. */
3596 target_find_description ();
3598 /* Next, now that we know something about the target, update the
3599 address spaces in the program spaces. */
3600 update_address_spaces ();
3602 /* On OSs where the list of libraries is global to all
3603 processes, we fetch them early. */
3604 if (gdbarch_has_global_solist (target_gdbarch ()))
3605 solib_add (NULL, from_tty, target, auto_solib_add);
3609 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3610 error (_("Non-stop mode requested, but remote "
3611 "does not support non-stop"));
3613 putpkt ("QNonStop:1");
3614 getpkt (&rs->buf, &rs->buf_size, 0);
3616 if (strcmp (rs->buf, "OK") != 0)
3617 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3619 /* Find about threads and processes the stub is already
3620 controlling. We default to adding them in the running state.
3621 The '?' query below will then tell us about which threads are
3623 remote_update_thread_list (target);
3625 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
3627 /* Don't assume that the stub can operate in all-stop mode.
3628 Request it explicitly. */
3629 putpkt ("QNonStop:0");
3630 getpkt (&rs->buf, &rs->buf_size, 0);
3632 if (strcmp (rs->buf, "OK") != 0)
3633 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3636 /* Upload TSVs regardless of whether the target is running or not. The
3637 remote stub, such as GDBserver, may have some predefined or builtin
3638 TSVs, even if the target is not running. */
3639 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3641 struct uploaded_tsv *uploaded_tsvs = NULL;
3643 remote_upload_trace_state_variables (target, &uploaded_tsvs);
3644 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3647 /* Check whether the target is running now. */
3649 getpkt (&rs->buf, &rs->buf_size, 0);
3655 struct inferior *inf;
3657 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3660 error (_("The target is not running (try extended-remote?)"));
3662 /* We're connected, but not running. Drop out before we
3663 call start_remote. */
3664 rs->starting_up = 0;
3669 /* Save the reply for later. */
3670 wait_status = alloca (strlen (rs->buf) + 1);
3671 strcpy (wait_status, rs->buf);
3674 /* Fetch thread list. */
3675 target_update_thread_list ();
3677 /* Let the stub know that we want it to return the thread. */
3678 set_continue_thread (minus_one_ptid);
3680 if (thread_count () == 0)
3682 /* Target has no concept of threads at all. GDB treats
3683 non-threaded target as single-threaded; add a main
3685 add_current_inferior_and_thread (wait_status);
3689 /* We have thread information; select the thread the target
3690 says should be current. If we're reconnecting to a
3691 multi-threaded program, this will ideally be the thread
3692 that last reported an event before GDB disconnected. */
3693 inferior_ptid = get_current_thread (wait_status);
3694 if (ptid_equal (inferior_ptid, null_ptid))
3696 /* Odd... The target was able to list threads, but not
3697 tell us which thread was current (no "thread"
3698 register in T stop reply?). Just pick the first
3699 thread in the thread list then. */
3700 inferior_ptid = thread_list->ptid;
3704 /* init_wait_for_inferior should be called before get_offsets in order
3705 to manage `inserted' flag in bp loc in a correct state.
3706 breakpoint_init_inferior, called from init_wait_for_inferior, set
3707 `inserted' flag to 0, while before breakpoint_re_set, called from
3708 start_remote, set `inserted' flag to 1. In the initialization of
3709 inferior, breakpoint_init_inferior should be called first, and then
3710 breakpoint_re_set can be called. If this order is broken, state of
3711 `inserted' flag is wrong, and cause some problems on breakpoint
3713 init_wait_for_inferior ();
3715 get_offsets (); /* Get text, data & bss offsets. */
3717 /* If we could not find a description using qXfer, and we know
3718 how to do it some other way, try again. This is not
3719 supported for non-stop; it could be, but it is tricky if
3720 there are no stopped threads when we connect. */
3721 if (remote_read_description_p (target)
3722 && gdbarch_target_desc (target_gdbarch ()) == NULL)
3724 target_clear_description ();
3725 target_find_description ();
3728 /* Use the previously fetched status. */
3729 gdb_assert (wait_status != NULL);
3730 strcpy (rs->buf, wait_status);
3731 rs->cached_wait_status = 1;
3734 start_remote (from_tty); /* Initialize gdb process mechanisms. */
3738 /* Clear WFI global state. Do this before finding about new
3739 threads and inferiors, and setting the current inferior.
3740 Otherwise we would clear the proceed status of the current
3741 inferior when we want its stop_soon state to be preserved
3742 (see notice_new_inferior). */
3743 init_wait_for_inferior ();
3745 /* In non-stop, we will either get an "OK", meaning that there
3746 are no stopped threads at this time; or, a regular stop
3747 reply. In the latter case, there may be more than one thread
3748 stopped --- we pull them all out using the vStopped
3750 if (strcmp (rs->buf, "OK") != 0)
3752 struct notif_client *notif = ¬if_client_stop;
3754 /* remote_notif_get_pending_replies acks this one, and gets
3756 rs->notif_state->pending_event[notif_client_stop.id]
3757 = remote_notif_parse (notif, rs->buf);
3758 remote_notif_get_pending_events (notif);
3760 /* Make sure that threads that were stopped remain
3762 iterate_over_threads (set_stop_requested_callback, NULL);
3765 if (target_can_async_p ())
3768 if (thread_count () == 0)
3771 error (_("The target is not running (try extended-remote?)"));
3773 /* We're connected, but not running. Drop out before we
3774 call start_remote. */
3775 rs->starting_up = 0;
3779 /* Let the stub know that we want it to return the thread. */
3781 /* Force the stub to choose a thread. */
3782 set_general_thread (null_ptid);
3785 inferior_ptid = remote_current_thread (minus_one_ptid);
3786 if (ptid_equal (inferior_ptid, minus_one_ptid))
3787 error (_("remote didn't report the current thread in non-stop mode"));
3789 get_offsets (); /* Get text, data & bss offsets. */
3791 /* In non-stop mode, any cached wait status will be stored in
3792 the stop reply queue. */
3793 gdb_assert (wait_status == NULL);
3795 /* Report all signals during attach/startup. */
3796 remote_pass_signals (target, 0, NULL);
3799 /* If we connected to a live target, do some additional setup. */
3800 if (target_has_execution)
3802 if (symfile_objfile) /* No use without a symbol-file. */
3803 remote_check_symbols ();
3806 /* Possibly the target has been engaged in a trace run started
3807 previously; find out where things are at. */
3808 if (remote_get_trace_status (target, current_trace_status ()) != -1)
3810 struct uploaded_tp *uploaded_tps = NULL;
3812 if (current_trace_status ()->running)
3813 printf_filtered (_("Trace is already running on the target.\n"));
3815 remote_upload_tracepoints (target, &uploaded_tps);
3817 merge_uploaded_tracepoints (&uploaded_tps);
3820 /* The thread and inferior lists are now synchronized with the
3821 target, our symbols have been relocated, and we're merged the
3822 target's tracepoints with ours. We're done with basic start
3824 rs->starting_up = 0;
3826 /* Maybe breakpoints are global and need to be inserted now. */
3827 if (breakpoints_should_be_inserted_now ())
3828 insert_breakpoints ();
3831 /* Open a connection to a remote debugger.
3832 NAME is the filename used for communication. */
3835 remote_open (const char *name, int from_tty)
3837 remote_open_1 (name, from_tty, &remote_ops, 0);
3840 /* Open a connection to a remote debugger using the extended
3841 remote gdb protocol. NAME is the filename used for communication. */
3844 extended_remote_open (const char *name, int from_tty)
3846 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3849 /* Reset all packets back to "unknown support". Called when opening a
3850 new connection to a remote target. */
3853 reset_all_packet_configs_support (void)
3857 for (i = 0; i < PACKET_MAX; i++)
3858 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3861 /* Initialize all packet configs. */
3864 init_all_packet_configs (void)
3868 for (i = 0; i < PACKET_MAX; i++)
3870 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3871 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3875 /* Symbol look-up. */
3878 remote_check_symbols (void)
3880 struct remote_state *rs = get_remote_state ();
3881 char *msg, *reply, *tmp;
3882 struct bound_minimal_symbol sym;
3885 /* The remote side has no concept of inferiors that aren't running
3886 yet, it only knows about running processes. If we're connected
3887 but our current inferior is not running, we should not invite the
3888 remote target to request symbol lookups related to its
3889 (unrelated) current process. */
3890 if (!target_has_execution)
3893 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
3896 /* Make sure the remote is pointing at the right process. Note
3897 there's no way to select "no process". */
3898 set_general_process ();
3900 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3901 because we need both at the same time. */
3902 msg = alloca (get_remote_packet_size ());
3904 /* Invite target to request symbol lookups. */
3906 putpkt ("qSymbol::");
3907 getpkt (&rs->buf, &rs->buf_size, 0);
3908 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3911 while (startswith (reply, "qSymbol:"))
3913 struct bound_minimal_symbol sym;
3916 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3918 sym = lookup_minimal_symbol (msg, NULL, NULL);
3919 if (sym.minsym == NULL)
3920 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3923 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
3924 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
3926 /* If this is a function address, return the start of code
3927 instead of any data function descriptor. */
3928 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3932 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3933 phex_nz (sym_addr, addr_size), &reply[8]);
3937 getpkt (&rs->buf, &rs->buf_size, 0);
3942 static struct serial *
3943 remote_serial_open (const char *name)
3945 static int udp_warning = 0;
3947 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3948 of in ser-tcp.c, because it is the remote protocol assuming that the
3949 serial connection is reliable and not the serial connection promising
3951 if (!udp_warning && startswith (name, "udp:"))
3953 warning (_("The remote protocol may be unreliable over UDP.\n"
3954 "Some events may be lost, rendering further debugging "
3959 return serial_open (name);
3962 /* Inform the target of our permission settings. The permission flags
3963 work without this, but if the target knows the settings, it can do
3964 a couple things. First, it can add its own check, to catch cases
3965 that somehow manage to get by the permissions checks in target
3966 methods. Second, if the target is wired to disallow particular
3967 settings (for instance, a system in the field that is not set up to
3968 be able to stop at a breakpoint), it can object to any unavailable
3972 remote_set_permissions (struct target_ops *self)
3974 struct remote_state *rs = get_remote_state ();
3976 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3977 "WriteReg:%x;WriteMem:%x;"
3978 "InsertBreak:%x;InsertTrace:%x;"
3979 "InsertFastTrace:%x;Stop:%x",
3980 may_write_registers, may_write_memory,
3981 may_insert_breakpoints, may_insert_tracepoints,
3982 may_insert_fast_tracepoints, may_stop);
3984 getpkt (&rs->buf, &rs->buf_size, 0);
3986 /* If the target didn't like the packet, warn the user. Do not try
3987 to undo the user's settings, that would just be maddening. */
3988 if (strcmp (rs->buf, "OK") != 0)
3989 warning (_("Remote refused setting permissions with: %s"), rs->buf);
3992 /* This type describes each known response to the qSupported
3994 struct protocol_feature
3996 /* The name of this protocol feature. */
3999 /* The default for this protocol feature. */
4000 enum packet_support default_support;
4002 /* The function to call when this feature is reported, or after
4003 qSupported processing if the feature is not supported.
4004 The first argument points to this structure. The second
4005 argument indicates whether the packet requested support be
4006 enabled, disabled, or probed (or the default, if this function
4007 is being called at the end of processing and this feature was
4008 not reported). The third argument may be NULL; if not NULL, it
4009 is a NUL-terminated string taken from the packet following
4010 this feature's name and an equals sign. */
4011 void (*func) (const struct protocol_feature *, enum packet_support,
4014 /* The corresponding packet for this feature. Only used if
4015 FUNC is remote_supported_packet. */
4020 remote_supported_packet (const struct protocol_feature *feature,
4021 enum packet_support support,
4022 const char *argument)
4026 warning (_("Remote qSupported response supplied an unexpected value for"
4027 " \"%s\"."), feature->name);
4031 remote_protocol_packets[feature->packet].support = support;
4035 remote_packet_size (const struct protocol_feature *feature,
4036 enum packet_support support, const char *value)
4038 struct remote_state *rs = get_remote_state ();
4043 if (support != PACKET_ENABLE)
4046 if (value == NULL || *value == '\0')
4048 warning (_("Remote target reported \"%s\" without a size."),
4054 packet_size = strtol (value, &value_end, 16);
4055 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4057 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4058 feature->name, value);
4062 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4064 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4065 packet_size, MAX_REMOTE_PACKET_SIZE);
4066 packet_size = MAX_REMOTE_PACKET_SIZE;
4069 /* Record the new maximum packet size. */
4070 rs->explicit_packet_size = packet_size;
4073 static const struct protocol_feature remote_protocol_features[] = {
4074 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
4075 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
4076 PACKET_qXfer_auxv },
4077 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4078 PACKET_qXfer_exec_file },
4079 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4080 PACKET_qXfer_features },
4081 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4082 PACKET_qXfer_libraries },
4083 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4084 PACKET_qXfer_libraries_svr4 },
4085 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4086 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
4087 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
4088 PACKET_qXfer_memory_map },
4089 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4090 PACKET_qXfer_spu_read },
4091 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4092 PACKET_qXfer_spu_write },
4093 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4094 PACKET_qXfer_osdata },
4095 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4096 PACKET_qXfer_threads },
4097 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4098 PACKET_qXfer_traceframe_info },
4099 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4100 PACKET_QPassSignals },
4101 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4102 PACKET_QProgramSignals },
4103 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4104 PACKET_QStartNoAckMode },
4105 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4106 PACKET_multiprocess_feature },
4107 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4108 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4109 PACKET_qXfer_siginfo_read },
4110 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4111 PACKET_qXfer_siginfo_write },
4112 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
4113 PACKET_ConditionalTracepoints },
4114 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
4115 PACKET_ConditionalBreakpoints },
4116 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
4117 PACKET_BreakpointCommands },
4118 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
4119 PACKET_FastTracepoints },
4120 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
4121 PACKET_StaticTracepoints },
4122 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
4123 PACKET_InstallInTrace},
4124 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4125 PACKET_DisconnectedTracing_feature },
4126 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4128 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4130 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4131 PACKET_TracepointSource },
4132 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4134 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4135 PACKET_EnableDisableTracepoints_feature },
4136 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4137 PACKET_qXfer_fdpic },
4138 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4140 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4141 PACKET_QDisableRandomization },
4142 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
4143 { "QTBuffer:size", PACKET_DISABLE,
4144 remote_supported_packet, PACKET_QTBuffer_size},
4145 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
4146 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4147 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4148 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
4149 PACKET_qXfer_btrace },
4150 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
4151 PACKET_qXfer_btrace_conf },
4152 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
4153 PACKET_Qbtrace_conf_bts_size },
4154 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
4155 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
4156 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4157 PACKET_fork_event_feature },
4158 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4159 PACKET_vfork_event_feature },
4162 static char *remote_support_xml;
4164 /* Register string appended to "xmlRegisters=" in qSupported query. */
4167 register_remote_support_xml (const char *xml)
4169 #if defined(HAVE_LIBEXPAT)
4170 if (remote_support_xml == NULL)
4171 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
4174 char *copy = xstrdup (remote_support_xml + 13);
4175 char *p = strtok (copy, ",");
4179 if (strcmp (p, xml) == 0)
4186 while ((p = strtok (NULL, ",")) != NULL);
4189 remote_support_xml = reconcat (remote_support_xml,
4190 remote_support_xml, ",", xml,
4197 remote_query_supported_append (char *msg, const char *append)
4200 return reconcat (msg, msg, ";", append, (char *) NULL);
4202 return xstrdup (append);
4206 remote_query_supported (void)
4208 struct remote_state *rs = get_remote_state ();
4211 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4213 /* The packet support flags are handled differently for this packet
4214 than for most others. We treat an error, a disabled packet, and
4215 an empty response identically: any features which must be reported
4216 to be used will be automatically disabled. An empty buffer
4217 accomplishes this, since that is also the representation for a list
4218 containing no features. */
4221 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
4224 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
4226 q = remote_query_supported_append (q, "multiprocess+");
4228 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4229 q = remote_query_supported_append (q, "swbreak+");
4230 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4231 q = remote_query_supported_append (q, "hwbreak+");
4233 if (remote_support_xml)
4234 q = remote_query_supported_append (q, remote_support_xml);
4236 q = remote_query_supported_append (q, "qRelocInsn+");
4240 if (packet_set_cmd_state (PACKET_fork_event_feature)
4241 != AUTO_BOOLEAN_FALSE)
4242 q = remote_query_supported_append (q, "fork-events+");
4243 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4244 != AUTO_BOOLEAN_FALSE)
4245 q = remote_query_supported_append (q, "vfork-events+");
4248 q = reconcat (q, "qSupported:", q, (char *) NULL);
4251 do_cleanups (old_chain);
4253 getpkt (&rs->buf, &rs->buf_size, 0);
4255 /* If an error occured, warn, but do not return - just reset the
4256 buffer to empty and go on to disable features. */
4257 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4260 warning (_("Remote failure reply: %s"), rs->buf);
4265 memset (seen, 0, sizeof (seen));
4270 enum packet_support is_supported;
4271 char *p, *end, *name_end, *value;
4273 /* First separate out this item from the rest of the packet. If
4274 there's another item after this, we overwrite the separator
4275 (terminated strings are much easier to work with). */
4277 end = strchr (p, ';');
4280 end = p + strlen (p);
4290 warning (_("empty item in \"qSupported\" response"));
4295 name_end = strchr (p, '=');
4298 /* This is a name=value entry. */
4299 is_supported = PACKET_ENABLE;
4300 value = name_end + 1;
4309 is_supported = PACKET_ENABLE;
4313 is_supported = PACKET_DISABLE;
4317 is_supported = PACKET_SUPPORT_UNKNOWN;
4321 warning (_("unrecognized item \"%s\" "
4322 "in \"qSupported\" response"), p);
4328 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4329 if (strcmp (remote_protocol_features[i].name, p) == 0)
4331 const struct protocol_feature *feature;
4334 feature = &remote_protocol_features[i];
4335 feature->func (feature, is_supported, value);
4340 /* If we increased the packet size, make sure to increase the global
4341 buffer size also. We delay this until after parsing the entire
4342 qSupported packet, because this is the same buffer we were
4344 if (rs->buf_size < rs->explicit_packet_size)
4346 rs->buf_size = rs->explicit_packet_size;
4347 rs->buf = xrealloc (rs->buf, rs->buf_size);
4350 /* Handle the defaults for unmentioned features. */
4351 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4354 const struct protocol_feature *feature;
4356 feature = &remote_protocol_features[i];
4357 feature->func (feature, feature->default_support, NULL);
4361 /* Remove any of the remote.c targets from target stack. Upper targets depend
4362 on it so remove them first. */
4365 remote_unpush_target (void)
4367 pop_all_targets_above (process_stratum - 1);
4371 remote_open_1 (const char *name, int from_tty,
4372 struct target_ops *target, int extended_p)
4374 struct remote_state *rs = get_remote_state ();
4377 error (_("To open a remote debug connection, you need to specify what\n"
4378 "serial device is attached to the remote system\n"
4379 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4381 /* See FIXME above. */
4382 if (!target_async_permitted)
4383 wait_forever_enabled_p = 1;
4385 /* If we're connected to a running target, target_preopen will kill it.
4386 Ask this question first, before target_preopen has a chance to kill
4388 if (rs->remote_desc != NULL && !have_inferiors ())
4391 && !query (_("Already connected to a remote target. Disconnect? ")))
4392 error (_("Still connected."));
4395 /* Here the possibly existing remote target gets unpushed. */
4396 target_preopen (from_tty);
4398 /* Make sure we send the passed signals list the next time we resume. */
4399 xfree (rs->last_pass_packet);
4400 rs->last_pass_packet = NULL;
4402 /* Make sure we send the program signals list the next time we
4404 xfree (rs->last_program_signals_packet);
4405 rs->last_program_signals_packet = NULL;
4407 remote_fileio_reset ();
4408 reopen_exec_file ();
4411 rs->remote_desc = remote_serial_open (name);
4412 if (!rs->remote_desc)
4413 perror_with_name (name);
4415 if (baud_rate != -1)
4417 if (serial_setbaudrate (rs->remote_desc, baud_rate))
4419 /* The requested speed could not be set. Error out to
4420 top level after closing remote_desc. Take care to
4421 set remote_desc to NULL to avoid closing remote_desc
4423 serial_close (rs->remote_desc);
4424 rs->remote_desc = NULL;
4425 perror_with_name (name);
4429 serial_setparity (rs->remote_desc, serial_parity);
4430 serial_raw (rs->remote_desc);
4432 /* If there is something sitting in the buffer we might take it as a
4433 response to a command, which would be bad. */
4434 serial_flush_input (rs->remote_desc);
4438 puts_filtered ("Remote debugging using ");
4439 puts_filtered (name);
4440 puts_filtered ("\n");
4442 push_target (target); /* Switch to using remote target now. */
4444 /* Register extra event sources in the event loop. */
4445 remote_async_inferior_event_token
4446 = create_async_event_handler (remote_async_inferior_event_handler,
4448 rs->notif_state = remote_notif_state_allocate ();
4450 /* Reset the target state; these things will be queried either by
4451 remote_query_supported or as they are needed. */
4452 reset_all_packet_configs_support ();
4453 rs->cached_wait_status = 0;
4454 rs->explicit_packet_size = 0;
4456 rs->extended = extended_p;
4457 rs->waiting_for_stop_reply = 0;
4458 rs->ctrlc_pending_p = 0;
4460 rs->general_thread = not_sent_ptid;
4461 rs->continue_thread = not_sent_ptid;
4462 rs->remote_traceframe_number = -1;
4464 /* Probe for ability to use "ThreadInfo" query, as required. */
4465 rs->use_threadinfo_query = 1;
4466 rs->use_threadextra_query = 1;
4468 if (target_async_permitted)
4470 /* With this target we start out by owning the terminal. */
4471 remote_async_terminal_ours_p = 1;
4473 /* FIXME: cagney/1999-09-23: During the initial connection it is
4474 assumed that the target is already ready and able to respond to
4475 requests. Unfortunately remote_start_remote() eventually calls
4476 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4477 around this. Eventually a mechanism that allows
4478 wait_for_inferior() to expect/get timeouts will be
4480 wait_forever_enabled_p = 0;
4483 /* First delete any symbols previously loaded from shared libraries. */
4484 no_shared_libraries (NULL, 0);
4487 init_thread_list ();
4489 /* Start the remote connection. If error() or QUIT, discard this
4490 target (we'd otherwise be in an inconsistent state) and then
4491 propogate the error on up the exception chain. This ensures that
4492 the caller doesn't stumble along blindly assuming that the
4493 function succeeded. The CLI doesn't have this problem but other
4494 UI's, such as MI do.
4496 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4497 this function should return an error indication letting the
4498 caller restore the previous state. Unfortunately the command
4499 ``target remote'' is directly wired to this function making that
4500 impossible. On a positive note, the CLI side of this problem has
4501 been fixed - the function set_cmd_context() makes it possible for
4502 all the ``target ....'' commands to share a common callback
4503 function. See cli-dump.c. */
4508 remote_start_remote (from_tty, target, extended_p);
4510 CATCH (ex, RETURN_MASK_ALL)
4512 /* Pop the partially set up target - unless something else did
4513 already before throwing the exception. */
4514 if (rs->remote_desc != NULL)
4515 remote_unpush_target ();
4516 if (target_async_permitted)
4517 wait_forever_enabled_p = 1;
4518 throw_exception (ex);
4523 remote_btrace_reset ();
4525 if (target_async_permitted)
4526 wait_forever_enabled_p = 1;
4529 /* Detach the specified process. */
4532 remote_detach_pid (int pid)
4534 struct remote_state *rs = get_remote_state ();
4536 if (remote_multi_process_p (rs))
4537 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4539 strcpy (rs->buf, "D");
4542 getpkt (&rs->buf, &rs->buf_size, 0);
4544 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4546 else if (rs->buf[0] == '\0')
4547 error (_("Remote doesn't know how to detach"));
4549 error (_("Can't detach process."));
4552 /* This detaches a program to which we previously attached, using
4553 inferior_ptid to identify the process. After this is done, GDB
4554 can be used to debug some other program. We better not have left
4555 any breakpoints in the target program or it'll die when it hits
4559 remote_detach_1 (const char *args, int from_tty)
4561 int pid = ptid_get_pid (inferior_ptid);
4562 struct remote_state *rs = get_remote_state ();
4563 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4567 error (_("Argument given to \"detach\" when remotely debugging."));
4569 if (!target_has_execution)
4570 error (_("No process to detach from."));
4574 char *exec_file = get_exec_file (0);
4575 if (exec_file == NULL)
4577 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4578 target_pid_to_str (pid_to_ptid (pid)));
4579 gdb_flush (gdb_stdout);
4582 /* Tell the remote target to detach. */
4583 remote_detach_pid (pid);
4585 if (from_tty && !rs->extended)
4586 puts_filtered (_("Ending remote debugging.\n"));
4588 /* Check to see if we are detaching a fork parent. Note that if we
4589 are detaching a fork child, tp == NULL. */
4590 is_fork_parent = (tp != NULL
4591 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4593 /* If doing detach-on-fork, we don't mourn, because that will delete
4594 breakpoints that should be available for the followed inferior. */
4595 if (!is_fork_parent)
4596 target_mourn_inferior ();
4599 inferior_ptid = null_ptid;
4600 detach_inferior (pid);
4605 remote_detach (struct target_ops *ops, const char *args, int from_tty)
4607 remote_detach_1 (args, from_tty);
4611 extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
4613 remote_detach_1 (args, from_tty);
4616 /* Target follow-fork function for remote targets. On entry, and
4617 at return, the current inferior is the fork parent.
4619 Note that although this is currently only used for extended-remote,
4620 it is named remote_follow_fork in anticipation of using it for the
4621 remote target as well. */
4624 remote_follow_fork (struct target_ops *ops, int follow_child,
4627 struct remote_state *rs = get_remote_state ();
4628 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
4630 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4631 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
4633 /* When following the parent and detaching the child, we detach
4634 the child here. For the case of following the child and
4635 detaching the parent, the detach is done in the target-
4636 independent follow fork code in infrun.c. We can't use
4637 target_detach when detaching an unfollowed child because
4638 the client side doesn't know anything about the child. */
4639 if (detach_fork && !follow_child)
4641 /* Detach the fork child. */
4645 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4646 child_pid = ptid_get_pid (child_ptid);
4648 remote_detach_pid (child_pid);
4649 detach_inferior (child_pid);
4655 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4658 remote_disconnect (struct target_ops *target, const char *args, int from_tty)
4661 error (_("Argument given to \"disconnect\" when remotely debugging."));
4663 /* Make sure we unpush even the extended remote targets; mourn
4664 won't do it. So call remote_mourn directly instead of
4665 target_mourn_inferior. */
4666 remote_mourn (target);
4669 puts_filtered ("Ending remote debugging.\n");
4672 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4673 be chatty about it. */
4676 extended_remote_attach (struct target_ops *target, const char *args,
4679 struct remote_state *rs = get_remote_state ();
4681 char *wait_status = NULL;
4683 pid = parse_pid_to_attach (args);
4685 /* Remote PID can be freely equal to getpid, do not check it here the same
4686 way as in other targets. */
4688 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
4689 error (_("This target does not support attaching to a process"));
4693 char *exec_file = get_exec_file (0);
4696 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4697 target_pid_to_str (pid_to_ptid (pid)));
4699 printf_unfiltered (_("Attaching to %s\n"),
4700 target_pid_to_str (pid_to_ptid (pid)));
4702 gdb_flush (gdb_stdout);
4705 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
4707 getpkt (&rs->buf, &rs->buf_size, 0);
4709 switch (packet_ok (rs->buf,
4710 &remote_protocol_packets[PACKET_vAttach]))
4715 /* Save the reply for later. */
4716 wait_status = alloca (strlen (rs->buf) + 1);
4717 strcpy (wait_status, rs->buf);
4719 else if (strcmp (rs->buf, "OK") != 0)
4720 error (_("Attaching to %s failed with: %s"),
4721 target_pid_to_str (pid_to_ptid (pid)),
4724 case PACKET_UNKNOWN:
4725 error (_("This target does not support attaching to a process"));
4727 error (_("Attaching to %s failed"),
4728 target_pid_to_str (pid_to_ptid (pid)));
4731 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
4733 inferior_ptid = pid_to_ptid (pid);
4737 struct thread_info *thread;
4739 /* Get list of threads. */
4740 remote_update_thread_list (target);
4742 thread = first_thread_of_process (pid);
4744 inferior_ptid = thread->ptid;
4746 inferior_ptid = pid_to_ptid (pid);
4748 /* Invalidate our notion of the remote current thread. */
4749 record_currthread (rs, minus_one_ptid);
4753 /* Now, if we have thread information, update inferior_ptid. */
4754 inferior_ptid = remote_current_thread (inferior_ptid);
4756 /* Add the main thread to the thread list. */
4757 add_thread_silent (inferior_ptid);
4760 /* Next, if the target can specify a description, read it. We do
4761 this before anything involving memory or registers. */
4762 target_find_description ();
4766 /* Use the previously fetched status. */
4767 gdb_assert (wait_status != NULL);
4769 if (target_can_async_p ())
4771 struct notif_event *reply
4772 = remote_notif_parse (¬if_client_stop, wait_status);
4774 push_stop_reply ((struct stop_reply *) reply);
4780 gdb_assert (wait_status != NULL);
4781 strcpy (rs->buf, wait_status);
4782 rs->cached_wait_status = 1;
4786 gdb_assert (wait_status == NULL);
4789 /* Implementation of the to_post_attach method. */
4792 extended_remote_post_attach (struct target_ops *ops, int pid)
4794 /* In certain cases GDB might not have had the chance to start
4795 symbol lookup up until now. This could happen if the debugged
4796 binary is not using shared libraries, the vsyscall page is not
4797 present (on Linux) and the binary itself hadn't changed since the
4798 debugging process was started. */
4799 if (symfile_objfile != NULL)
4800 remote_check_symbols();
4804 /* Check for the availability of vCont. This function should also check
4808 remote_vcont_probe (struct remote_state *rs)
4812 strcpy (rs->buf, "vCont?");
4814 getpkt (&rs->buf, &rs->buf_size, 0);
4817 /* Make sure that the features we assume are supported. */
4818 if (startswith (buf, "vCont"))
4821 int support_s, support_S, support_c, support_C;
4827 rs->supports_vCont.t = 0;
4828 rs->supports_vCont.r = 0;
4829 while (p && *p == ';')
4832 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4834 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4836 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4838 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4840 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4841 rs->supports_vCont.t = 1;
4842 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4843 rs->supports_vCont.r = 1;
4845 p = strchr (p, ';');
4848 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4849 BUF will make packet_ok disable the packet. */
4850 if (!support_s || !support_S || !support_c || !support_C)
4854 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4857 /* Helper function for building "vCont" resumptions. Write a
4858 resumption to P. ENDP points to one-passed-the-end of the buffer
4859 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4860 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4861 resumed thread should be single-stepped and/or signalled. If PTID
4862 equals minus_one_ptid, then all threads are resumed; if PTID
4863 represents a process, then all threads of the process are resumed;
4864 the thread to be stepped and/or signalled is given in the global
4868 append_resumption (char *p, char *endp,
4869 ptid_t ptid, int step, enum gdb_signal siggnal)
4871 struct remote_state *rs = get_remote_state ();
4873 if (step && siggnal != GDB_SIGNAL_0)
4874 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4876 /* GDB is willing to range step. */
4877 && use_range_stepping
4878 /* Target supports range stepping. */
4879 && rs->supports_vCont.r
4880 /* We don't currently support range stepping multiple
4881 threads with a wildcard (though the protocol allows it,
4882 so stubs shouldn't make an active effort to forbid
4884 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4886 struct thread_info *tp;
4888 if (ptid_equal (ptid, minus_one_ptid))
4890 /* If we don't know about the target thread's tid, then
4891 we're resuming magic_null_ptid (see caller). */
4892 tp = find_thread_ptid (magic_null_ptid);
4895 tp = find_thread_ptid (ptid);
4896 gdb_assert (tp != NULL);
4898 if (tp->control.may_range_step)
4900 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4902 p += xsnprintf (p, endp - p, ";r%s,%s",
4903 phex_nz (tp->control.step_range_start,
4905 phex_nz (tp->control.step_range_end,
4909 p += xsnprintf (p, endp - p, ";s");
4912 p += xsnprintf (p, endp - p, ";s");
4913 else if (siggnal != GDB_SIGNAL_0)
4914 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4916 p += xsnprintf (p, endp - p, ";c");
4918 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4922 /* All (-1) threads of process. */
4923 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
4925 p += xsnprintf (p, endp - p, ":");
4926 p = write_ptid (p, endp, nptid);
4928 else if (!ptid_equal (ptid, minus_one_ptid))
4930 p += xsnprintf (p, endp - p, ":");
4931 p = write_ptid (p, endp, ptid);
4937 /* Append a vCont continue-with-signal action for threads that have a
4938 non-zero stop signal. */
4941 append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4943 struct thread_info *thread;
4945 ALL_NON_EXITED_THREADS (thread)
4946 if (ptid_match (thread->ptid, ptid)
4947 && !ptid_equal (inferior_ptid, thread->ptid)
4948 && thread->suspend.stop_signal != GDB_SIGNAL_0)
4950 p = append_resumption (p, endp, thread->ptid,
4951 0, thread->suspend.stop_signal);
4952 thread->suspend.stop_signal = GDB_SIGNAL_0;
4958 /* Resume the remote inferior by using a "vCont" packet. The thread
4959 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4960 resumed thread should be single-stepped and/or signalled. If PTID
4961 equals minus_one_ptid, then all threads are resumed; the thread to
4962 be stepped and/or signalled is given in the global INFERIOR_PTID.
4963 This function returns non-zero iff it resumes the inferior.
4965 This function issues a strict subset of all possible vCont commands at the
4969 remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
4971 struct remote_state *rs = get_remote_state ();
4975 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
4976 remote_vcont_probe (rs);
4978 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
4982 endp = rs->buf + get_remote_packet_size ();
4984 /* If we could generate a wider range of packets, we'd have to worry
4985 about overflowing BUF. Should there be a generic
4986 "multi-part-packet" packet? */
4988 p += xsnprintf (p, endp - p, "vCont");
4990 if (ptid_equal (ptid, magic_null_ptid))
4992 /* MAGIC_NULL_PTID means that we don't have any active threads,
4993 so we don't have any TID numbers the inferior will
4994 understand. Make sure to only send forms that do not specify
4996 append_resumption (p, endp, minus_one_ptid, step, siggnal);
4998 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
5000 /* Resume all threads (of all processes, or of a single
5001 process), with preference for INFERIOR_PTID. This assumes
5002 inferior_ptid belongs to the set of all threads we are about
5004 if (step || siggnal != GDB_SIGNAL_0)
5006 /* Step inferior_ptid, with or without signal. */
5007 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
5010 /* Also pass down any pending signaled resumption for other
5011 threads not the current. */
5012 p = append_pending_thread_resumptions (p, endp, ptid);
5014 /* And continue others without a signal. */
5015 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
5019 /* Scheduler locking; resume only PTID. */
5020 append_resumption (p, endp, ptid, step, siggnal);
5023 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5028 /* In non-stop, the stub replies to vCont with "OK". The stop
5029 reply will be reported asynchronously by means of a `%Stop'
5031 getpkt (&rs->buf, &rs->buf_size, 0);
5032 if (strcmp (rs->buf, "OK") != 0)
5033 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5039 /* Tell the remote machine to resume. */
5042 remote_resume (struct target_ops *ops,
5043 ptid_t ptid, int step, enum gdb_signal siggnal)
5045 struct remote_state *rs = get_remote_state ();
5048 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5049 (explained in remote-notif.c:handle_notification) so
5050 remote_notif_process is not called. We need find a place where
5051 it is safe to start a 'vNotif' sequence. It is good to do it
5052 before resuming inferior, because inferior was stopped and no RSP
5053 traffic at that moment. */
5055 remote_notif_process (rs->notif_state, ¬if_client_stop);
5057 rs->last_sent_signal = siggnal;
5058 rs->last_sent_step = step;
5060 /* The vCont packet doesn't need to specify threads via Hc. */
5061 /* No reverse support (yet) for vCont. */
5062 if (execution_direction != EXEC_REVERSE)
5063 if (remote_vcont_resume (ptid, step, siggnal))
5066 /* All other supported resume packets do use Hc, so set the continue
5068 if (ptid_equal (ptid, minus_one_ptid))
5069 set_continue_thread (any_thread_ptid);
5071 set_continue_thread (ptid);
5074 if (execution_direction == EXEC_REVERSE)
5076 /* We don't pass signals to the target in reverse exec mode. */
5077 if (info_verbose && siggnal != GDB_SIGNAL_0)
5078 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5081 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5082 error (_("Remote reverse-step not supported."));
5083 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5084 error (_("Remote reverse-continue not supported."));
5086 strcpy (buf, step ? "bs" : "bc");
5088 else if (siggnal != GDB_SIGNAL_0)
5090 buf[0] = step ? 'S' : 'C';
5091 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5092 buf[2] = tohex (((int) siggnal) & 0xf);
5096 strcpy (buf, step ? "s" : "c");
5101 /* We are about to start executing the inferior, let's register it
5102 with the event loop. NOTE: this is the one place where all the
5103 execution commands end up. We could alternatively do this in each
5104 of the execution commands in infcmd.c. */
5105 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5106 into infcmd.c in order to allow inferior function calls to work
5107 NOT asynchronously. */
5108 if (target_can_async_p ())
5111 /* We've just told the target to resume. The remote server will
5112 wait for the inferior to stop, and then send a stop reply. In
5113 the mean time, we can't start another command/query ourselves
5114 because the stub wouldn't be ready to process it. This applies
5115 only to the base all-stop protocol, however. In non-stop (which
5116 only supports vCont), the stub replies with an "OK", and is
5117 immediate able to process further serial input. */
5119 rs->waiting_for_stop_reply = 1;
5123 /* Set up the signal handler for SIGINT, while the target is
5124 executing, ovewriting the 'regular' SIGINT signal handler. */
5126 async_initialize_sigint_signal_handler (void)
5128 signal (SIGINT, async_handle_remote_sigint);
5131 /* Signal handler for SIGINT, while the target is executing. */
5133 async_handle_remote_sigint (int sig)
5135 signal (sig, async_handle_remote_sigint_twice);
5136 /* Note we need to go through gdb_call_async_signal_handler in order
5137 to wake up the event loop on Windows. */
5138 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
5141 /* Signal handler for SIGINT, installed after SIGINT has already been
5142 sent once. It will take effect the second time that the user sends
5145 async_handle_remote_sigint_twice (int sig)
5147 signal (sig, async_handle_remote_sigint);
5148 /* See note in async_handle_remote_sigint. */
5149 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
5152 /* Perform the real interruption of the target execution, in response
5155 async_remote_interrupt (gdb_client_data arg)
5158 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
5160 target_stop (inferior_ptid);
5163 /* Perform interrupt, if the first attempt did not succeed. Just give
5164 up on the target alltogether. */
5166 async_remote_interrupt_twice (gdb_client_data arg)
5169 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
5174 /* Reinstall the usual SIGINT handlers, after the target has
5177 async_cleanup_sigint_signal_handler (void *dummy)
5179 signal (SIGINT, handle_sigint);
5182 /* Send ^C to target to halt it. Target will respond, and send us a
5184 static void (*ofunc) (int);
5186 /* The command line interface's stop routine. This function is installed
5187 as a signal handler for SIGINT. The first time a user requests a
5188 stop, we call remote_stop to send a break or ^C. If there is no
5189 response from the target (it didn't stop when the user requested it),
5190 we ask the user if he'd like to detach from the target. */
5192 sync_remote_interrupt (int signo)
5194 /* If this doesn't work, try more severe steps. */
5195 signal (signo, sync_remote_interrupt_twice);
5197 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
5200 /* The user typed ^C twice. */
5203 sync_remote_interrupt_twice (int signo)
5205 signal (signo, ofunc);
5206 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5207 signal (signo, sync_remote_interrupt);
5210 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5211 thread, all threads of a remote process, or all threads of all
5215 remote_stop_ns (ptid_t ptid)
5217 struct remote_state *rs = get_remote_state ();
5219 char *endp = rs->buf + get_remote_packet_size ();
5221 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
5222 remote_vcont_probe (rs);
5224 if (!rs->supports_vCont.t)
5225 error (_("Remote server does not support stopping threads"));
5227 if (ptid_equal (ptid, minus_one_ptid)
5228 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5229 p += xsnprintf (p, endp - p, "vCont;t");
5234 p += xsnprintf (p, endp - p, "vCont;t:");
5236 if (ptid_is_pid (ptid))
5237 /* All (-1) threads of process. */
5238 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
5241 /* Small optimization: if we already have a stop reply for
5242 this thread, no use in telling the stub we want this
5244 if (peek_stop_reply (ptid))
5250 write_ptid (p, endp, nptid);
5253 /* In non-stop, we get an immediate OK reply. The stop reply will
5254 come in asynchronously by notification. */
5256 getpkt (&rs->buf, &rs->buf_size, 0);
5257 if (strcmp (rs->buf, "OK") != 0)
5258 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5261 /* All-stop version of target_stop. Sends a break or a ^C to stop the
5262 remote target. It is undefined which thread of which process
5263 reports the stop. */
5266 remote_stop_as (ptid_t ptid)
5268 struct remote_state *rs = get_remote_state ();
5270 rs->ctrlc_pending_p = 1;
5272 /* If the inferior is stopped already, but the core didn't know
5273 about it yet, just ignore the request. The cached wait status
5274 will be collected in remote_wait. */
5275 if (rs->cached_wait_status)
5278 /* Send interrupt_sequence to remote target. */
5279 send_interrupt_sequence ();
5282 /* This is the generic stop called via the target vector. When a target
5283 interrupt is requested, either by the command line or the GUI, we
5284 will eventually end up here. */
5287 remote_stop (struct target_ops *self, ptid_t ptid)
5290 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
5293 remote_stop_ns (ptid);
5295 remote_stop_as (ptid);
5298 /* Ask the user what to do when an interrupt is received. */
5301 interrupt_query (void)
5303 target_terminal_ours ();
5305 if (target_is_async_p ())
5307 signal (SIGINT, handle_sigint);
5312 if (query (_("Interrupted while waiting for the program.\n\
5313 Give up (and stop debugging it)? ")))
5315 remote_unpush_target ();
5320 target_terminal_inferior ();
5323 /* Enable/disable target terminal ownership. Most targets can use
5324 terminal groups to control terminal ownership. Remote targets are
5325 different in that explicit transfer of ownership to/from GDB/target
5329 remote_terminal_inferior (struct target_ops *self)
5331 if (!target_async_permitted)
5332 /* Nothing to do. */
5335 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5336 idempotent. The event-loop GDB talking to an asynchronous target
5337 with a synchronous command calls this function from both
5338 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5339 transfer the terminal to the target when it shouldn't this guard
5341 if (!remote_async_terminal_ours_p)
5343 delete_file_handler (input_fd);
5344 remote_async_terminal_ours_p = 0;
5345 async_initialize_sigint_signal_handler ();
5346 /* NOTE: At this point we could also register our selves as the
5347 recipient of all input. Any characters typed could then be
5348 passed on down to the target. */
5352 remote_terminal_ours (struct target_ops *self)
5354 if (!target_async_permitted)
5355 /* Nothing to do. */
5358 /* See FIXME in remote_terminal_inferior. */
5359 if (remote_async_terminal_ours_p)
5361 async_cleanup_sigint_signal_handler (NULL);
5362 add_file_handler (input_fd, stdin_event_handler, 0);
5363 remote_async_terminal_ours_p = 1;
5367 remote_console_output (char *msg)
5371 for (p = msg; p[0] && p[1]; p += 2)
5374 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5378 fputs_unfiltered (tb, gdb_stdtarg);
5380 gdb_flush (gdb_stdtarg);
5383 typedef struct cached_reg
5386 gdb_byte data[MAX_REGISTER_SIZE];
5389 DEF_VEC_O(cached_reg_t);
5391 typedef struct stop_reply
5393 struct notif_event base;
5395 /* The identifier of the thread about this event */
5398 /* The remote state this event is associated with. When the remote
5399 connection, represented by a remote_state object, is closed,
5400 all the associated stop_reply events should be released. */
5401 struct remote_state *rs;
5403 struct target_waitstatus ws;
5405 /* Expedited registers. This makes remote debugging a bit more
5406 efficient for those targets that provide critical registers as
5407 part of their normal status mechanism (as another roundtrip to
5408 fetch them is avoided). */
5409 VEC(cached_reg_t) *regcache;
5411 enum target_stop_reason stop_reason;
5413 CORE_ADDR watch_data_address;
5418 DECLARE_QUEUE_P (stop_reply_p);
5419 DEFINE_QUEUE_P (stop_reply_p);
5420 /* The list of already fetched and acknowledged stop events. This
5421 queue is used for notification Stop, and other notifications
5422 don't need queue for their events, because the notification events
5423 of Stop can't be consumed immediately, so that events should be
5424 queued first, and be consumed by remote_wait_{ns,as} one per
5425 time. Other notifications can consume their events immediately,
5426 so queue is not needed for them. */
5427 static QUEUE (stop_reply_p) *stop_reply_queue;
5430 stop_reply_xfree (struct stop_reply *r)
5432 notif_event_xfree ((struct notif_event *) r);
5436 remote_notif_stop_parse (struct notif_client *self, char *buf,
5437 struct notif_event *event)
5439 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5443 remote_notif_stop_ack (struct notif_client *self, char *buf,
5444 struct notif_event *event)
5446 struct stop_reply *stop_reply = (struct stop_reply *) event;
5449 putpkt ((char *) self->ack_command);
5451 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5452 /* We got an unknown stop reply. */
5453 error (_("Unknown stop reply"));
5455 push_stop_reply (stop_reply);
5459 remote_notif_stop_can_get_pending_events (struct notif_client *self)
5461 /* We can't get pending events in remote_notif_process for
5462 notification stop, and we have to do this in remote_wait_ns
5463 instead. If we fetch all queued events from stub, remote stub
5464 may exit and we have no chance to process them back in
5466 mark_async_event_handler (remote_async_inferior_event_token);
5471 stop_reply_dtr (struct notif_event *event)
5473 struct stop_reply *r = (struct stop_reply *) event;
5475 VEC_free (cached_reg_t, r->regcache);
5478 static struct notif_event *
5479 remote_notif_stop_alloc_reply (void)
5481 struct notif_event *r
5482 = (struct notif_event *) XNEW (struct stop_reply);
5484 r->dtr = stop_reply_dtr;
5489 /* A client of notification Stop. */
5491 struct notif_client notif_client_stop =
5495 remote_notif_stop_parse,
5496 remote_notif_stop_ack,
5497 remote_notif_stop_can_get_pending_events,
5498 remote_notif_stop_alloc_reply,
5502 /* A parameter to pass data in and out. */
5504 struct queue_iter_param
5507 struct stop_reply *output;
5510 /* Determine if THREAD is a pending fork parent thread. ARG contains
5511 the pid of the process that owns the threads we want to check, or
5512 -1 if we want to check all threads. */
5515 is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5518 if (ws->kind == TARGET_WAITKIND_FORKED
5519 || ws->kind == TARGET_WAITKIND_VFORKED)
5521 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5528 /* Check whether EVENT is a fork event, and if it is, remove the
5529 fork child from the context list passed in DATA. */
5532 remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5533 QUEUE_ITER (stop_reply_p) *iter,
5537 struct queue_iter_param *param = data;
5538 struct threads_listing_context *context = param->input;
5540 if (event->ws.kind == TARGET_WAITKIND_FORKED
5541 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5543 threads_listing_context_remove (&event->ws, context);
5549 /* If CONTEXT contains any fork child threads that have not been
5550 reported yet, remove them from the CONTEXT list. If such a
5551 thread exists it is because we are stopped at a fork catchpoint
5552 and have not yet called follow_fork, which will set up the
5553 host-side data structures for the new process. */
5556 remove_new_fork_children (struct threads_listing_context *context)
5558 struct thread_info * thread;
5560 struct notif_client *notif = ¬if_client_stop;
5561 struct queue_iter_param param;
5563 /* For any threads stopped at a fork event, remove the corresponding
5564 fork child threads from the CONTEXT list. */
5565 ALL_NON_EXITED_THREADS (thread)
5567 struct target_waitstatus *ws = &thread->pending_follow;
5569 if (is_pending_fork_parent (ws, pid, thread->ptid))
5571 threads_listing_context_remove (ws, context);
5575 /* Check for any pending fork events (not reported or processed yet)
5576 in process PID and remove those fork child threads from the
5577 CONTEXT list as well. */
5578 remote_notif_get_pending_events (notif);
5579 param.input = context;
5580 param.output = NULL;
5581 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5582 remove_child_of_pending_fork, ¶m);
5585 /* Remove stop replies in the queue if its pid is equal to the given
5589 remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5590 QUEUE_ITER (stop_reply_p) *iter,
5594 struct queue_iter_param *param = data;
5595 struct inferior *inf = param->input;
5597 if (ptid_get_pid (event->ptid) == inf->pid)
5599 stop_reply_xfree (event);
5600 QUEUE_remove_elem (stop_reply_p, q, iter);
5606 /* Discard all pending stop replies of inferior INF. */
5609 discard_pending_stop_replies (struct inferior *inf)
5612 struct queue_iter_param param;
5613 struct stop_reply *reply;
5614 struct remote_state *rs = get_remote_state ();
5615 struct remote_notif_state *rns = rs->notif_state;
5617 /* This function can be notified when an inferior exists. When the
5618 target is not remote, the notification state is NULL. */
5619 if (rs->remote_desc == NULL)
5622 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
5624 /* Discard the in-flight notification. */
5625 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
5627 stop_reply_xfree (reply);
5628 rns->pending_event[notif_client_stop.id] = NULL;
5632 param.output = NULL;
5633 /* Discard the stop replies we have already pulled with
5635 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5636 remove_stop_reply_for_inferior, ¶m);
5639 /* If its remote state is equal to the given remote state,
5640 remove EVENT from the stop reply queue. */
5643 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5644 QUEUE_ITER (stop_reply_p) *iter,
5648 struct queue_iter_param *param = data;
5649 struct remote_state *rs = param->input;
5651 if (event->rs == rs)
5653 stop_reply_xfree (event);
5654 QUEUE_remove_elem (stop_reply_p, q, iter);
5660 /* Discard the stop replies for RS in stop_reply_queue. */
5663 discard_pending_stop_replies_in_queue (struct remote_state *rs)
5665 struct queue_iter_param param;
5668 param.output = NULL;
5669 /* Discard the stop replies we have already pulled with
5671 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5672 remove_stop_reply_of_remote_state, ¶m);
5675 /* A parameter to pass data in and out. */
5678 remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5679 QUEUE_ITER (stop_reply_p) *iter,
5683 struct queue_iter_param *param = data;
5684 ptid_t *ptid = param->input;
5686 if (ptid_match (event->ptid, *ptid))
5688 param->output = event;
5689 QUEUE_remove_elem (stop_reply_p, q, iter);
5696 /* Remove the first reply in 'stop_reply_queue' which matches
5699 static struct stop_reply *
5700 remote_notif_remove_queued_reply (ptid_t ptid)
5702 struct queue_iter_param param;
5704 param.input = &ptid;
5705 param.output = NULL;
5707 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5708 remote_notif_remove_once_on_match, ¶m);
5710 fprintf_unfiltered (gdb_stdlog,
5711 "notif: discard queued event: 'Stop' in %s\n",
5712 target_pid_to_str (ptid));
5714 return param.output;
5717 /* Look for a queued stop reply belonging to PTID. If one is found,
5718 remove it from the queue, and return it. Returns NULL if none is
5719 found. If there are still queued events left to process, tell the
5720 event loop to get back to target_wait soon. */
5722 static struct stop_reply *
5723 queued_stop_reply (ptid_t ptid)
5725 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
5727 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
5728 /* There's still at least an event left. */
5729 mark_async_event_handler (remote_async_inferior_event_token);
5734 /* Push a fully parsed stop reply in the stop reply queue. Since we
5735 know that we now have at least one queued event left to pass to the
5736 core side, tell the event loop to get back to target_wait soon. */
5739 push_stop_reply (struct stop_reply *new_event)
5741 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
5744 fprintf_unfiltered (gdb_stdlog,
5745 "notif: push 'Stop' %s to queue %d\n",
5746 target_pid_to_str (new_event->ptid),
5747 QUEUE_length (stop_reply_p,
5750 mark_async_event_handler (remote_async_inferior_event_token);
5754 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5755 QUEUE_ITER (stop_reply_p) *iter,
5756 struct stop_reply *event,
5759 ptid_t *ptid = data;
5761 return !(ptid_equal (*ptid, event->ptid)
5762 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5765 /* Returns true if we have a stop reply for PTID. */
5768 peek_stop_reply (ptid_t ptid)
5770 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5771 stop_reply_match_ptid_and_ws, &ptid);
5774 /* Skip PACKET until the next semi-colon (or end of string). */
5777 skip_to_semicolon (char *p)
5779 while (*p != '\0' && *p != ';')
5784 /* Parse the stop reply in BUF. Either the function succeeds, and the
5785 result is stored in EVENT, or throws an error. */
5788 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5790 struct remote_arch_state *rsa = get_remote_arch_state ();
5794 event->ptid = null_ptid;
5795 event->rs = get_remote_state ();
5796 event->ws.kind = TARGET_WAITKIND_IGNORE;
5797 event->ws.value.integer = 0;
5798 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5799 event->regcache = NULL;
5804 case 'T': /* Status with PC, SP, FP, ... */
5805 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5806 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5808 n... = register number
5809 r... = register contents
5812 p = &buf[3]; /* after Txx */
5818 p1 = strchr (p, ':');
5820 error (_("Malformed packet(a) (missing colon): %s\n\
5824 error (_("Malformed packet(a) (missing register number): %s\n\
5828 /* Some "registers" are actually extended stop information.
5829 Note if you're adding a new entry here: GDB 7.9 and
5830 earlier assume that all register "numbers" that start
5831 with an hex digit are real register numbers. Make sure
5832 the server only sends such a packet if it knows the
5833 client understands it. */
5835 if (strncmp (p, "thread", p1 - p) == 0)
5836 event->ptid = read_ptid (++p1, &p);
5837 else if ((strncmp (p, "watch", p1 - p) == 0)
5838 || (strncmp (p, "rwatch", p1 - p) == 0)
5839 || (strncmp (p, "awatch", p1 - p) == 0))
5841 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
5842 p = unpack_varlen_hex (++p1, &addr);
5843 event->watch_data_address = (CORE_ADDR) addr;
5845 else if (strncmp (p, "swbreak", p1 - p) == 0)
5847 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5849 /* Make sure the stub doesn't forget to indicate support
5851 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5852 error (_("Unexpected swbreak stop reason"));
5854 /* The value part is documented as "must be empty",
5855 though we ignore it, in case we ever decide to make
5856 use of it in a backward compatible way. */
5857 p = skip_to_semicolon (p1 + 1);
5859 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5861 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5863 /* Make sure the stub doesn't forget to indicate support
5865 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5866 error (_("Unexpected hwbreak stop reason"));
5869 p = skip_to_semicolon (p1 + 1);
5871 else if (strncmp (p, "library", p1 - p) == 0)
5873 event->ws.kind = TARGET_WAITKIND_LOADED;
5874 p = skip_to_semicolon (p1 + 1);
5876 else if (strncmp (p, "replaylog", p1 - p) == 0)
5878 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5879 /* p1 will indicate "begin" or "end", but it makes
5880 no difference for now, so ignore it. */
5881 p = skip_to_semicolon (p1 + 1);
5883 else if (strncmp (p, "core", p1 - p) == 0)
5887 p = unpack_varlen_hex (++p1, &c);
5890 else if (strncmp (p, "fork", p1 - p) == 0)
5892 event->ws.value.related_pid = read_ptid (++p1, &p);
5893 event->ws.kind = TARGET_WAITKIND_FORKED;
5895 else if (strncmp (p, "vfork", p1 - p) == 0)
5897 event->ws.value.related_pid = read_ptid (++p1, &p);
5898 event->ws.kind = TARGET_WAITKIND_VFORKED;
5900 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5902 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5903 p = skip_to_semicolon (p1 + 1);
5910 /* Maybe a real ``P'' register number. */
5911 p_temp = unpack_varlen_hex (p, &pnum);
5912 /* If the first invalid character is the colon, we got a
5913 register number. Otherwise, it's an unknown stop
5917 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5918 cached_reg_t cached_reg;
5921 error (_("Remote sent bad register number %s: %s\n\
5923 hex_string (pnum), p, buf);
5925 cached_reg.num = reg->regnum;
5928 fieldsize = hex2bin (p, cached_reg.data,
5929 register_size (target_gdbarch (),
5932 if (fieldsize < register_size (target_gdbarch (),
5934 warning (_("Remote reply is too short: %s"), buf);
5936 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5940 /* Not a number. Silently skip unknown optional
5942 p = skip_to_semicolon (p1 + 1);
5947 error (_("Remote register badly formatted: %s\nhere: %s"),
5952 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5956 case 'S': /* Old style status, just signal only. */
5960 event->ws.kind = TARGET_WAITKIND_STOPPED;
5961 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5962 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5963 event->ws.value.sig = (enum gdb_signal) sig;
5965 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5968 case 'W': /* Target exited. */
5975 /* GDB used to accept only 2 hex chars here. Stubs should
5976 only send more if they detect GDB supports multi-process
5978 p = unpack_varlen_hex (&buf[1], &value);
5982 /* The remote process exited. */
5983 event->ws.kind = TARGET_WAITKIND_EXITED;
5984 event->ws.value.integer = value;
5988 /* The remote process exited with a signal. */
5989 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5990 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5991 event->ws.value.sig = (enum gdb_signal) value;
5993 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5996 /* If no process is specified, assume inferior_ptid. */
5997 pid = ptid_get_pid (inferior_ptid);
6006 else if (startswith (p, "process:"))
6010 p += sizeof ("process:") - 1;
6011 unpack_varlen_hex (p, &upid);
6015 error (_("unknown stop reply packet: %s"), buf);
6018 error (_("unknown stop reply packet: %s"), buf);
6019 event->ptid = pid_to_ptid (pid);
6024 if (non_stop && ptid_equal (event->ptid, null_ptid))
6025 error (_("No process or thread specified in stop reply: %s"), buf);
6028 /* When the stub wants to tell GDB about a new notification reply, it
6029 sends a notification (%Stop, for example). Those can come it at
6030 any time, hence, we have to make sure that any pending
6031 putpkt/getpkt sequence we're making is finished, before querying
6032 the stub for more events with the corresponding ack command
6033 (vStopped, for example). E.g., if we started a vStopped sequence
6034 immediately upon receiving the notification, something like this
6042 1.6) <-- (registers reply to step #1.3)
6044 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6047 To solve this, whenever we parse a %Stop notification successfully,
6048 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6049 doing whatever we were doing:
6055 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6056 2.5) <-- (registers reply to step #2.3)
6058 Eventualy after step #2.5, we return to the event loop, which
6059 notices there's an event on the
6060 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6061 associated callback --- the function below. At this point, we're
6062 always safe to start a vStopped sequence. :
6065 2.7) <-- T05 thread:2
6071 remote_notif_get_pending_events (struct notif_client *nc)
6073 struct remote_state *rs = get_remote_state ();
6075 if (rs->notif_state->pending_event[nc->id] != NULL)
6078 fprintf_unfiltered (gdb_stdlog,
6079 "notif: process: '%s' ack pending event\n",
6083 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6084 rs->notif_state->pending_event[nc->id] = NULL;
6088 getpkt (&rs->buf, &rs->buf_size, 0);
6089 if (strcmp (rs->buf, "OK") == 0)
6092 remote_notif_ack (nc, rs->buf);
6098 fprintf_unfiltered (gdb_stdlog,
6099 "notif: process: '%s' no pending reply\n",
6104 /* Called when it is decided that STOP_REPLY holds the info of the
6105 event that is to be returned to the core. This function always
6106 destroys STOP_REPLY. */
6109 process_stop_reply (struct stop_reply *stop_reply,
6110 struct target_waitstatus *status)
6114 *status = stop_reply->ws;
6115 ptid = stop_reply->ptid;
6117 /* If no thread/process was reported by the stub, assume the current
6119 if (ptid_equal (ptid, null_ptid))
6120 ptid = inferior_ptid;
6122 if (status->kind != TARGET_WAITKIND_EXITED
6123 && status->kind != TARGET_WAITKIND_SIGNALLED)
6125 struct remote_state *rs = get_remote_state ();
6127 /* Expedited registers. */
6128 if (stop_reply->regcache)
6130 struct regcache *regcache
6131 = get_thread_arch_regcache (ptid, target_gdbarch ());
6136 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6138 regcache_raw_supply (regcache, reg->num, reg->data);
6139 VEC_free (cached_reg_t, stop_reply->regcache);
6142 rs->stop_reason = stop_reply->stop_reason;
6143 rs->remote_watch_data_address = stop_reply->watch_data_address;
6145 remote_notice_new_inferior (ptid, 0);
6146 demand_private_info (ptid)->core = stop_reply->core;
6149 stop_reply_xfree (stop_reply);
6153 /* The non-stop mode version of target_wait. */
6156 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
6158 struct remote_state *rs = get_remote_state ();
6159 struct stop_reply *stop_reply;
6163 /* If in non-stop mode, get out of getpkt even if a
6164 notification is received. */
6166 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6167 0 /* forever */, &is_notif);
6170 if (ret != -1 && !is_notif)
6173 case 'E': /* Error of some sort. */
6174 /* We're out of sync with the target now. Did it continue
6175 or not? We can't tell which thread it was in non-stop,
6176 so just ignore this. */
6177 warning (_("Remote failure reply: %s"), rs->buf);
6179 case 'O': /* Console output. */
6180 remote_console_output (rs->buf + 1);
6183 warning (_("Invalid remote reply: %s"), rs->buf);
6187 /* Acknowledge a pending stop reply that may have arrived in the
6189 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
6190 remote_notif_get_pending_events (¬if_client_stop);
6192 /* If indeed we noticed a stop reply, we're done. */
6193 stop_reply = queued_stop_reply (ptid);
6194 if (stop_reply != NULL)
6195 return process_stop_reply (stop_reply, status);
6197 /* Still no event. If we're just polling for an event, then
6198 return to the event loop. */
6199 if (options & TARGET_WNOHANG)
6201 status->kind = TARGET_WAITKIND_IGNORE;
6202 return minus_one_ptid;
6205 /* Otherwise do a blocking wait. */
6206 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6207 1 /* forever */, &is_notif);
6211 /* Wait until the remote machine stops, then return, storing status in
6212 STATUS just as `wait' would. */
6215 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
6217 struct remote_state *rs = get_remote_state ();
6218 ptid_t event_ptid = null_ptid;
6220 struct stop_reply *stop_reply;
6224 status->kind = TARGET_WAITKIND_IGNORE;
6225 status->value.integer = 0;
6227 stop_reply = queued_stop_reply (ptid);
6228 if (stop_reply != NULL)
6229 return process_stop_reply (stop_reply, status);
6231 if (rs->cached_wait_status)
6232 /* Use the cached wait status, but only once. */
6233 rs->cached_wait_status = 0;
6239 if (!target_is_async_p ())
6241 ofunc = signal (SIGINT, sync_remote_interrupt);
6242 /* If the user hit C-c before this packet, or between packets,
6243 pretend that it was hit right here. */
6244 if (check_quit_flag ())
6247 sync_remote_interrupt (SIGINT);
6251 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6252 _never_ wait for ever -> test on target_is_async_p().
6253 However, before we do that we need to ensure that the caller
6254 knows how to take the target into/out of async mode. */
6255 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6256 wait_forever_enabled_p, &is_notif);
6258 if (!target_is_async_p ())
6259 signal (SIGINT, ofunc);
6261 /* GDB gets a notification. Return to core as this event is
6263 if (ret != -1 && is_notif)
6264 return minus_one_ptid;
6269 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
6271 /* We got something. */
6272 rs->waiting_for_stop_reply = 0;
6274 /* Assume that the target has acknowledged Ctrl-C unless we receive
6275 an 'F' or 'O' packet. */
6276 if (buf[0] != 'F' && buf[0] != 'O')
6277 rs->ctrlc_pending_p = 0;
6281 case 'E': /* Error of some sort. */
6282 /* We're out of sync with the target now. Did it continue or
6283 not? Not is more likely, so report a stop. */
6284 warning (_("Remote failure reply: %s"), buf);
6285 status->kind = TARGET_WAITKIND_STOPPED;
6286 status->value.sig = GDB_SIGNAL_0;
6288 case 'F': /* File-I/O request. */
6289 remote_fileio_request (buf, rs->ctrlc_pending_p);
6290 rs->ctrlc_pending_p = 0;
6292 case 'T': case 'S': case 'X': case 'W':
6294 struct stop_reply *stop_reply
6295 = (struct stop_reply *) remote_notif_parse (¬if_client_stop,
6298 event_ptid = process_stop_reply (stop_reply, status);
6301 case 'O': /* Console output. */
6302 remote_console_output (buf + 1);
6304 /* The target didn't really stop; keep waiting. */
6305 rs->waiting_for_stop_reply = 1;
6309 if (rs->last_sent_signal != GDB_SIGNAL_0)
6311 /* Zero length reply means that we tried 'S' or 'C' and the
6312 remote system doesn't support it. */
6313 target_terminal_ours_for_output ();
6315 ("Can't send signals to this remote system. %s not sent.\n",
6316 gdb_signal_to_name (rs->last_sent_signal));
6317 rs->last_sent_signal = GDB_SIGNAL_0;
6318 target_terminal_inferior ();
6320 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
6321 putpkt ((char *) buf);
6323 /* We just told the target to resume, so a stop reply is in
6325 rs->waiting_for_stop_reply = 1;
6328 /* else fallthrough */
6330 warning (_("Invalid remote reply: %s"), buf);
6332 rs->waiting_for_stop_reply = 1;
6336 if (status->kind == TARGET_WAITKIND_IGNORE)
6338 /* Nothing interesting happened. If we're doing a non-blocking
6339 poll, we're done. Otherwise, go back to waiting. */
6340 if (options & TARGET_WNOHANG)
6341 return minus_one_ptid;
6345 else if (status->kind != TARGET_WAITKIND_EXITED
6346 && status->kind != TARGET_WAITKIND_SIGNALLED)
6348 if (!ptid_equal (event_ptid, null_ptid))
6349 record_currthread (rs, event_ptid);
6351 event_ptid = inferior_ptid;
6354 /* A process exit. Invalidate our notion of current thread. */
6355 record_currthread (rs, minus_one_ptid);
6360 /* Wait until the remote machine stops, then return, storing status in
6361 STATUS just as `wait' would. */
6364 remote_wait (struct target_ops *ops,
6365 ptid_t ptid, struct target_waitstatus *status, int options)
6370 event_ptid = remote_wait_ns (ptid, status, options);
6372 event_ptid = remote_wait_as (ptid, status, options);
6374 if (target_is_async_p ())
6376 /* If there are are events left in the queue tell the event loop
6378 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
6379 mark_async_event_handler (remote_async_inferior_event_token);
6385 /* Fetch a single register using a 'p' packet. */
6388 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
6390 struct remote_state *rs = get_remote_state ();
6392 char regp[MAX_REGISTER_SIZE];
6395 if (packet_support (PACKET_p) == PACKET_DISABLE)
6398 if (reg->pnum == -1)
6403 p += hexnumstr (p, reg->pnum);
6406 getpkt (&rs->buf, &rs->buf_size, 0);
6410 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6414 case PACKET_UNKNOWN:
6417 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6418 gdbarch_register_name (get_regcache_arch (regcache),
6423 /* If this register is unfetchable, tell the regcache. */
6426 regcache_raw_supply (regcache, reg->regnum, NULL);
6430 /* Otherwise, parse and supply the value. */
6436 error (_("fetch_register_using_p: early buf termination"));
6438 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6441 regcache_raw_supply (regcache, reg->regnum, regp);
6445 /* Fetch the registers included in the target's 'g' packet. */
6448 send_g_packet (void)
6450 struct remote_state *rs = get_remote_state ();
6453 xsnprintf (rs->buf, get_remote_packet_size (), "g");
6454 remote_send (&rs->buf, &rs->buf_size);
6456 /* We can get out of synch in various cases. If the first character
6457 in the buffer is not a hex character, assume that has happened
6458 and try to fetch another packet to read. */
6459 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6460 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6461 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6462 && rs->buf[0] != 'x') /* New: unavailable register value. */
6465 fprintf_unfiltered (gdb_stdlog,
6466 "Bad register packet; fetching a new packet\n");
6467 getpkt (&rs->buf, &rs->buf_size, 0);
6470 buf_len = strlen (rs->buf);
6472 /* Sanity check the received packet. */
6473 if (buf_len % 2 != 0)
6474 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
6480 process_g_packet (struct regcache *regcache)
6482 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6483 struct remote_state *rs = get_remote_state ();
6484 struct remote_arch_state *rsa = get_remote_arch_state ();
6489 buf_len = strlen (rs->buf);
6491 /* Further sanity checks, with knowledge of the architecture. */
6492 if (buf_len > 2 * rsa->sizeof_g_packet)
6493 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6495 /* Save the size of the packet sent to us by the target. It is used
6496 as a heuristic when determining the max size of packets that the
6497 target can safely receive. */
6498 if (rsa->actual_register_packet_size == 0)
6499 rsa->actual_register_packet_size = buf_len;
6501 /* If this is smaller than we guessed the 'g' packet would be,
6502 update our records. A 'g' reply that doesn't include a register's
6503 value implies either that the register is not available, or that
6504 the 'p' packet must be used. */
6505 if (buf_len < 2 * rsa->sizeof_g_packet)
6507 rsa->sizeof_g_packet = buf_len / 2;
6509 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6511 if (rsa->regs[i].pnum == -1)
6514 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6515 rsa->regs[i].in_g_packet = 0;
6517 rsa->regs[i].in_g_packet = 1;
6521 regs = alloca (rsa->sizeof_g_packet);
6523 /* Unimplemented registers read as all bits zero. */
6524 memset (regs, 0, rsa->sizeof_g_packet);
6526 /* Reply describes registers byte by byte, each byte encoded as two
6527 hex characters. Suck them all up, then supply them to the
6528 register cacheing/storage mechanism. */
6531 for (i = 0; i < rsa->sizeof_g_packet; i++)
6533 if (p[0] == 0 || p[1] == 0)
6534 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6535 internal_error (__FILE__, __LINE__,
6536 _("unexpected end of 'g' packet reply"));
6538 if (p[0] == 'x' && p[1] == 'x')
6539 regs[i] = 0; /* 'x' */
6541 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6545 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6547 struct packet_reg *r = &rsa->regs[i];
6551 if (r->offset * 2 >= strlen (rs->buf))
6552 /* This shouldn't happen - we adjusted in_g_packet above. */
6553 internal_error (__FILE__, __LINE__,
6554 _("unexpected end of 'g' packet reply"));
6555 else if (rs->buf[r->offset * 2] == 'x')
6557 gdb_assert (r->offset * 2 < strlen (rs->buf));
6558 /* The register isn't available, mark it as such (at
6559 the same time setting the value to zero). */
6560 regcache_raw_supply (regcache, r->regnum, NULL);
6563 regcache_raw_supply (regcache, r->regnum,
6570 fetch_registers_using_g (struct regcache *regcache)
6573 process_g_packet (regcache);
6576 /* Make the remote selected traceframe match GDB's selected
6580 set_remote_traceframe (void)
6583 struct remote_state *rs = get_remote_state ();
6585 if (rs->remote_traceframe_number == get_traceframe_number ())
6588 /* Avoid recursion, remote_trace_find calls us again. */
6589 rs->remote_traceframe_number = get_traceframe_number ();
6591 newnum = target_trace_find (tfind_number,
6592 get_traceframe_number (), 0, 0, NULL);
6594 /* Should not happen. If it does, all bets are off. */
6595 if (newnum != get_traceframe_number ())
6596 warning (_("could not set remote traceframe"));
6600 remote_fetch_registers (struct target_ops *ops,
6601 struct regcache *regcache, int regnum)
6603 struct remote_arch_state *rsa = get_remote_arch_state ();
6606 set_remote_traceframe ();
6607 set_general_thread (inferior_ptid);
6611 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6613 gdb_assert (reg != NULL);
6615 /* If this register might be in the 'g' packet, try that first -
6616 we are likely to read more than one register. If this is the
6617 first 'g' packet, we might be overly optimistic about its
6618 contents, so fall back to 'p'. */
6619 if (reg->in_g_packet)
6621 fetch_registers_using_g (regcache);
6622 if (reg->in_g_packet)
6626 if (fetch_register_using_p (regcache, reg))
6629 /* This register is not available. */
6630 regcache_raw_supply (regcache, reg->regnum, NULL);
6635 fetch_registers_using_g (regcache);
6637 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6638 if (!rsa->regs[i].in_g_packet)
6639 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
6641 /* This register is not available. */
6642 regcache_raw_supply (regcache, i, NULL);
6646 /* Prepare to store registers. Since we may send them all (using a
6647 'G' request), we have to read out the ones we don't want to change
6651 remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
6653 struct remote_arch_state *rsa = get_remote_arch_state ();
6655 gdb_byte buf[MAX_REGISTER_SIZE];
6657 /* Make sure the entire registers array is valid. */
6658 switch (packet_support (PACKET_P))
6660 case PACKET_DISABLE:
6661 case PACKET_SUPPORT_UNKNOWN:
6662 /* Make sure all the necessary registers are cached. */
6663 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6664 if (rsa->regs[i].in_g_packet)
6665 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
6672 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6673 packet was not recognized. */
6676 store_register_using_P (const struct regcache *regcache,
6677 struct packet_reg *reg)
6679 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6680 struct remote_state *rs = get_remote_state ();
6681 /* Try storing a single register. */
6682 char *buf = rs->buf;
6683 gdb_byte regp[MAX_REGISTER_SIZE];
6686 if (packet_support (PACKET_P) == PACKET_DISABLE)
6689 if (reg->pnum == -1)
6692 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
6693 p = buf + strlen (buf);
6694 regcache_raw_collect (regcache, reg->regnum, regp);
6695 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6697 getpkt (&rs->buf, &rs->buf_size, 0);
6699 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6704 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6705 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
6706 case PACKET_UNKNOWN:
6709 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6713 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6714 contents of the register cache buffer. FIXME: ignores errors. */
6717 store_registers_using_G (const struct regcache *regcache)
6719 struct remote_state *rs = get_remote_state ();
6720 struct remote_arch_state *rsa = get_remote_arch_state ();
6724 /* Extract all the registers in the regcache copying them into a
6729 regs = alloca (rsa->sizeof_g_packet);
6730 memset (regs, 0, rsa->sizeof_g_packet);
6731 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6733 struct packet_reg *r = &rsa->regs[i];
6736 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
6740 /* Command describes registers byte by byte,
6741 each byte encoded as two hex characters. */
6744 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6746 bin2hex (regs, p, rsa->sizeof_g_packet);
6748 getpkt (&rs->buf, &rs->buf_size, 0);
6749 if (packet_check_result (rs->buf) == PACKET_ERROR)
6750 error (_("Could not write registers; remote failure reply '%s'"),
6754 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6755 of the register cache buffer. FIXME: ignores errors. */
6758 remote_store_registers (struct target_ops *ops,
6759 struct regcache *regcache, int regnum)
6761 struct remote_arch_state *rsa = get_remote_arch_state ();
6764 set_remote_traceframe ();
6765 set_general_thread (inferior_ptid);
6769 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6771 gdb_assert (reg != NULL);
6773 /* Always prefer to store registers using the 'P' packet if
6774 possible; we often change only a small number of registers.
6775 Sometimes we change a larger number; we'd need help from a
6776 higher layer to know to use 'G'. */
6777 if (store_register_using_P (regcache, reg))
6780 /* For now, don't complain if we have no way to write the
6781 register. GDB loses track of unavailable registers too
6782 easily. Some day, this may be an error. We don't have
6783 any way to read the register, either... */
6784 if (!reg->in_g_packet)
6787 store_registers_using_G (regcache);
6791 store_registers_using_G (regcache);
6793 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6794 if (!rsa->regs[i].in_g_packet)
6795 if (!store_register_using_P (regcache, &rsa->regs[i]))
6796 /* See above for why we do not issue an error here. */
6801 /* Return the number of hex digits in num. */
6804 hexnumlen (ULONGEST num)
6808 for (i = 0; num != 0; i++)
6814 /* Set BUF to the minimum number of hex digits representing NUM. */
6817 hexnumstr (char *buf, ULONGEST num)
6819 int len = hexnumlen (num);
6821 return hexnumnstr (buf, num, len);
6825 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6828 hexnumnstr (char *buf, ULONGEST num, int width)
6834 for (i = width - 1; i >= 0; i--)
6836 buf[i] = "0123456789abcdef"[(num & 0xf)];
6843 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6846 remote_address_masked (CORE_ADDR addr)
6848 unsigned int address_size = remote_address_size;
6850 /* If "remoteaddresssize" was not set, default to target address size. */
6852 address_size = gdbarch_addr_bit (target_gdbarch ());
6854 if (address_size > 0
6855 && address_size < (sizeof (ULONGEST) * 8))
6857 /* Only create a mask when that mask can safely be constructed
6858 in a ULONGEST variable. */
6861 mask = (mask << address_size) - 1;
6867 /* Determine whether the remote target supports binary downloading.
6868 This is accomplished by sending a no-op memory write of zero length
6869 to the target at the specified address. It does not suffice to send
6870 the whole packet, since many stubs strip the eighth bit and
6871 subsequently compute a wrong checksum, which causes real havoc with
6874 NOTE: This can still lose if the serial line is not eight-bit
6875 clean. In cases like this, the user should clear "remote
6879 check_binary_download (CORE_ADDR addr)
6881 struct remote_state *rs = get_remote_state ();
6883 switch (packet_support (PACKET_X))
6885 case PACKET_DISABLE:
6889 case PACKET_SUPPORT_UNKNOWN:
6895 p += hexnumstr (p, (ULONGEST) addr);
6897 p += hexnumstr (p, (ULONGEST) 0);
6901 putpkt_binary (rs->buf, (int) (p - rs->buf));
6902 getpkt (&rs->buf, &rs->buf_size, 0);
6904 if (rs->buf[0] == '\0')
6907 fprintf_unfiltered (gdb_stdlog,
6908 "binary downloading NOT "
6909 "supported by target\n");
6910 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6915 fprintf_unfiltered (gdb_stdlog,
6916 "binary downloading supported by target\n");
6917 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6924 /* Helper function to resize the payload in order to try to get a good
6925 alignment. We try to write an amount of data such that the next write will
6926 start on an address aligned on REMOTE_ALIGN_WRITES. */
6929 align_for_efficient_write (int todo, CORE_ADDR memaddr)
6931 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6934 /* Write memory data directly to the remote machine.
6935 This does not inform the data cache; the data cache uses this.
6936 HEADER is the starting part of the packet.
6937 MEMADDR is the address in the remote memory space.
6938 MYADDR is the address of the buffer in our space.
6939 LEN_UNITS is the number of addressable units to write.
6940 UNIT_SIZE is the length in bytes of an addressable unit.
6941 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6942 should send data as binary ('X'), or hex-encoded ('M').
6944 The function creates packet of the form
6945 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6947 where encoding of <DATA> is terminated by PACKET_FORMAT.
6949 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6952 Return the transferred status, error or OK (an
6953 'enum target_xfer_status' value). Save the number of addressable units
6954 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
6956 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
6957 exchange between gdb and the stub could look like (?? in place of the
6963 -> $M1000,3:eeeeffffeeee#??
6967 <- eeeeffffeeeedddd */
6969 static enum target_xfer_status
6970 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6971 const gdb_byte *myaddr, ULONGEST len_units,
6972 int unit_size, ULONGEST *xfered_len_units,
6973 char packet_format, int use_length)
6975 struct remote_state *rs = get_remote_state ();
6981 int payload_capacity_bytes;
6982 int payload_length_bytes;
6984 if (packet_format != 'X' && packet_format != 'M')
6985 internal_error (__FILE__, __LINE__,
6986 _("remote_write_bytes_aux: bad packet format"));
6989 return TARGET_XFER_EOF;
6991 payload_capacity_bytes = get_memory_write_packet_size ();
6993 /* The packet buffer will be large enough for the payload;
6994 get_memory_packet_size ensures this. */
6997 /* Compute the size of the actual payload by subtracting out the
6998 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7000 payload_capacity_bytes -= strlen ("$,:#NN");
7002 /* The comma won't be used. */
7003 payload_capacity_bytes += 1;
7004 payload_capacity_bytes -= strlen (header);
7005 payload_capacity_bytes -= hexnumlen (memaddr);
7007 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
7009 strcat (rs->buf, header);
7010 p = rs->buf + strlen (header);
7012 /* Compute a best guess of the number of bytes actually transfered. */
7013 if (packet_format == 'X')
7015 /* Best guess at number of bytes that will fit. */
7016 todo_units = min (len_units, payload_capacity_bytes / unit_size);
7018 payload_capacity_bytes -= hexnumlen (todo_units);
7019 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
7023 /* Number of bytes that will fit. */
7024 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
7026 payload_capacity_bytes -= hexnumlen (todo_units);
7027 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
7030 if (todo_units <= 0)
7031 internal_error (__FILE__, __LINE__,
7032 _("minimum packet size too small to write data"));
7034 /* If we already need another packet, then try to align the end
7035 of this packet to a useful boundary. */
7036 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7037 todo_units = align_for_efficient_write (todo_units, memaddr);
7039 /* Append "<memaddr>". */
7040 memaddr = remote_address_masked (memaddr);
7041 p += hexnumstr (p, (ULONGEST) memaddr);
7048 /* Append the length and retain its location and size. It may need to be
7049 adjusted once the packet body has been created. */
7051 plenlen = hexnumstr (p, (ULONGEST) todo_units);
7059 /* Append the packet body. */
7060 if (packet_format == 'X')
7062 /* Binary mode. Send target system values byte by byte, in
7063 increasing byte addresses. Only escape certain critical
7065 payload_length_bytes =
7066 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7067 &units_written, payload_capacity_bytes);
7069 /* If not all TODO units fit, then we'll need another packet. Make
7070 a second try to keep the end of the packet aligned. Don't do
7071 this if the packet is tiny. */
7072 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
7076 new_todo_units = align_for_efficient_write (units_written, memaddr);
7078 if (new_todo_units != units_written)
7079 payload_length_bytes =
7080 remote_escape_output (myaddr, new_todo_units, unit_size,
7081 (gdb_byte *) p, &units_written,
7082 payload_capacity_bytes);
7085 p += payload_length_bytes;
7086 if (use_length && units_written < todo_units)
7088 /* Escape chars have filled up the buffer prematurely,
7089 and we have actually sent fewer units than planned.
7090 Fix-up the length field of the packet. Use the same
7091 number of characters as before. */
7092 plen += hexnumnstr (plen, (ULONGEST) units_written,
7094 *plen = ':'; /* overwrite \0 from hexnumnstr() */
7099 /* Normal mode: Send target system values byte by byte, in
7100 increasing byte addresses. Each byte is encoded as a two hex
7102 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7103 units_written = todo_units;
7106 putpkt_binary (rs->buf, (int) (p - rs->buf));
7107 getpkt (&rs->buf, &rs->buf_size, 0);
7109 if (rs->buf[0] == 'E')
7110 return TARGET_XFER_E_IO;
7112 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7113 send fewer units than we'd planned. */
7114 *xfered_len_units = (ULONGEST) units_written;
7115 return TARGET_XFER_OK;
7118 /* Write memory data directly to the remote machine.
7119 This does not inform the data cache; the data cache uses this.
7120 MEMADDR is the address in the remote memory space.
7121 MYADDR is the address of the buffer in our space.
7122 LEN is the number of bytes.
7124 Return the transferred status, error or OK (an
7125 'enum target_xfer_status' value). Save the number of bytes
7126 transferred in *XFERED_LEN. Only transfer a single packet. */
7128 static enum target_xfer_status
7129 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
7130 int unit_size, ULONGEST *xfered_len)
7132 char *packet_format = 0;
7134 /* Check whether the target supports binary download. */
7135 check_binary_download (memaddr);
7137 switch (packet_support (PACKET_X))
7140 packet_format = "X";
7142 case PACKET_DISABLE:
7143 packet_format = "M";
7145 case PACKET_SUPPORT_UNKNOWN:
7146 internal_error (__FILE__, __LINE__,
7147 _("remote_write_bytes: bad internal state"));
7149 internal_error (__FILE__, __LINE__, _("bad switch"));
7152 return remote_write_bytes_aux (packet_format,
7153 memaddr, myaddr, len, unit_size, xfered_len,
7154 packet_format[0], 1);
7157 /* Read memory data directly from the remote machine.
7158 This does not use the data cache; the data cache uses this.
7159 MEMADDR is the address in the remote memory space.
7160 MYADDR is the address of the buffer in our space.
7161 LEN_UNITS is the number of addressable memory units to read..
7162 UNIT_SIZE is the length in bytes of an addressable unit.
7164 Return the transferred status, error or OK (an
7165 'enum target_xfer_status' value). Save the number of bytes
7166 transferred in *XFERED_LEN_UNITS.
7168 See the comment of remote_write_bytes_aux for an example of
7169 memory read/write exchange between gdb and the stub. */
7171 static enum target_xfer_status
7172 remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7173 int unit_size, ULONGEST *xfered_len_units)
7175 struct remote_state *rs = get_remote_state ();
7176 int buf_size_bytes; /* Max size of packet output buffer. */
7181 buf_size_bytes = get_memory_read_packet_size ();
7182 /* The packet buffer will be large enough for the payload;
7183 get_memory_packet_size ensures this. */
7185 /* Number of units that will fit. */
7186 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
7188 /* Construct "m"<memaddr>","<len>". */
7189 memaddr = remote_address_masked (memaddr);
7192 p += hexnumstr (p, (ULONGEST) memaddr);
7194 p += hexnumstr (p, (ULONGEST) todo_units);
7197 getpkt (&rs->buf, &rs->buf_size, 0);
7198 if (rs->buf[0] == 'E'
7199 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7200 && rs->buf[3] == '\0')
7201 return TARGET_XFER_E_IO;
7202 /* Reply describes memory byte by byte, each byte encoded as two hex
7205 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
7206 /* Return what we have. Let higher layers handle partial reads. */
7207 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
7208 return TARGET_XFER_OK;
7211 /* Using the set of read-only target sections of remote, read live
7214 For interface/parameters/return description see target.h,
7217 static enum target_xfer_status
7218 remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7219 ULONGEST memaddr, ULONGEST len,
7220 int unit_size, ULONGEST *xfered_len)
7222 struct target_section *secp;
7223 struct target_section_table *table;
7225 secp = target_section_by_addr (ops, memaddr);
7227 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7228 secp->the_bfd_section)
7231 struct target_section *p;
7232 ULONGEST memend = memaddr + len;
7234 table = target_get_section_table (ops);
7236 for (p = table->sections; p < table->sections_end; p++)
7238 if (memaddr >= p->addr)
7240 if (memend <= p->endaddr)
7242 /* Entire transfer is within this section. */
7243 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
7246 else if (memaddr >= p->endaddr)
7248 /* This section ends before the transfer starts. */
7253 /* This section overlaps the transfer. Just do half. */
7254 len = p->endaddr - memaddr;
7255 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
7262 return TARGET_XFER_EOF;
7265 /* Similar to remote_read_bytes_1, but it reads from the remote stub
7266 first if the requested memory is unavailable in traceframe.
7267 Otherwise, fall back to remote_read_bytes_1. */
7269 static enum target_xfer_status
7270 remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
7271 gdb_byte *myaddr, ULONGEST len, int unit_size,
7272 ULONGEST *xfered_len)
7275 return TARGET_XFER_EOF;
7277 if (get_traceframe_number () != -1)
7279 VEC(mem_range_s) *available;
7281 /* If we fail to get the set of available memory, then the
7282 target does not support querying traceframe info, and so we
7283 attempt reading from the traceframe anyway (assuming the
7284 target implements the old QTro packet then). */
7285 if (traceframe_available_memory (&available, memaddr, len))
7287 struct cleanup *old_chain;
7289 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7291 if (VEC_empty (mem_range_s, available)
7292 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7294 enum target_xfer_status res;
7296 /* Don't read into the traceframe's available
7298 if (!VEC_empty (mem_range_s, available))
7300 LONGEST oldlen = len;
7302 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7303 gdb_assert (len <= oldlen);
7306 do_cleanups (old_chain);
7308 /* This goes through the topmost target again. */
7309 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
7310 len, unit_size, xfered_len);
7311 if (res == TARGET_XFER_OK)
7312 return TARGET_XFER_OK;
7315 /* No use trying further, we know some memory starting
7316 at MEMADDR isn't available. */
7318 return TARGET_XFER_UNAVAILABLE;
7322 /* Don't try to read more than how much is available, in
7323 case the target implements the deprecated QTro packet to
7324 cater for older GDBs (the target's knowledge of read-only
7325 sections may be outdated by now). */
7326 len = VEC_index (mem_range_s, available, 0)->length;
7328 do_cleanups (old_chain);
7332 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
7337 /* Sends a packet with content determined by the printf format string
7338 FORMAT and the remaining arguments, then gets the reply. Returns
7339 whether the packet was a success, a failure, or unknown. */
7341 static enum packet_result remote_send_printf (const char *format, ...)
7342 ATTRIBUTE_PRINTF (1, 2);
7344 static enum packet_result
7345 remote_send_printf (const char *format, ...)
7347 struct remote_state *rs = get_remote_state ();
7348 int max_size = get_remote_packet_size ();
7351 va_start (ap, format);
7354 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
7355 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
7357 if (putpkt (rs->buf) < 0)
7358 error (_("Communication problem with target."));
7361 getpkt (&rs->buf, &rs->buf_size, 0);
7363 return packet_check_result (rs->buf);
7367 restore_remote_timeout (void *p)
7369 int value = *(int *)p;
7371 remote_timeout = value;
7374 /* Flash writing can take quite some time. We'll set
7375 effectively infinite timeout for flash operations.
7376 In future, we'll need to decide on a better approach. */
7377 static const int remote_flash_timeout = 1000;
7380 remote_flash_erase (struct target_ops *ops,
7381 ULONGEST address, LONGEST length)
7383 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
7384 int saved_remote_timeout = remote_timeout;
7385 enum packet_result ret;
7386 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7387 &saved_remote_timeout);
7389 remote_timeout = remote_flash_timeout;
7391 ret = remote_send_printf ("vFlashErase:%s,%s",
7392 phex (address, addr_size),
7396 case PACKET_UNKNOWN:
7397 error (_("Remote target does not support flash erase"));
7399 error (_("Error erasing flash with vFlashErase packet"));
7404 do_cleanups (back_to);
7407 static enum target_xfer_status
7408 remote_flash_write (struct target_ops *ops, ULONGEST address,
7409 ULONGEST length, ULONGEST *xfered_len,
7410 const gdb_byte *data)
7412 int saved_remote_timeout = remote_timeout;
7413 enum target_xfer_status ret;
7414 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7415 &saved_remote_timeout);
7417 remote_timeout = remote_flash_timeout;
7418 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
7420 do_cleanups (back_to);
7426 remote_flash_done (struct target_ops *ops)
7428 int saved_remote_timeout = remote_timeout;
7430 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7431 &saved_remote_timeout);
7433 remote_timeout = remote_flash_timeout;
7434 ret = remote_send_printf ("vFlashDone");
7435 do_cleanups (back_to);
7439 case PACKET_UNKNOWN:
7440 error (_("Remote target does not support vFlashDone"));
7442 error (_("Error finishing flash operation"));
7449 remote_files_info (struct target_ops *ignore)
7451 puts_filtered ("Debugging a target over a serial line.\n");
7454 /* Stuff for dealing with the packets which are part of this protocol.
7455 See comment at top of file for details. */
7457 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7458 error to higher layers. Called when a serial error is detected.
7459 The exception message is STRING, followed by a colon and a blank,
7460 the system error message for errno at function entry and final dot
7461 for output compatibility with throw_perror_with_name. */
7464 unpush_and_perror (const char *string)
7466 int saved_errno = errno;
7468 remote_unpush_target ();
7469 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7470 safe_strerror (saved_errno));
7473 /* Read a single character from the remote end. */
7476 readchar (int timeout)
7479 struct remote_state *rs = get_remote_state ();
7481 ch = serial_readchar (rs->remote_desc, timeout);
7486 switch ((enum serial_rc) ch)
7489 remote_unpush_target ();
7490 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
7493 unpush_and_perror (_("Remote communication error. "
7494 "Target disconnected."));
7496 case SERIAL_TIMEOUT:
7502 /* Wrapper for serial_write that closes the target and throws if
7506 remote_serial_write (const char *str, int len)
7508 struct remote_state *rs = get_remote_state ();
7510 if (serial_write (rs->remote_desc, str, len))
7512 unpush_and_perror (_("Remote communication error. "
7513 "Target disconnected."));
7517 /* Send the command in *BUF to the remote machine, and read the reply
7518 into *BUF. Report an error if we get an error reply. Resize
7519 *BUF using xrealloc if necessary to hold the result, and update
7523 remote_send (char **buf,
7527 getpkt (buf, sizeof_buf, 0);
7529 if ((*buf)[0] == 'E')
7530 error (_("Remote failure reply: %s"), *buf);
7533 /* Return a pointer to an xmalloc'ed string representing an escaped
7534 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7535 etc. The caller is responsible for releasing the returned
7539 escape_buffer (const char *buf, int n)
7541 struct cleanup *old_chain;
7542 struct ui_file *stb;
7545 stb = mem_fileopen ();
7546 old_chain = make_cleanup_ui_file_delete (stb);
7548 fputstrn_unfiltered (buf, n, '\\', stb);
7549 str = ui_file_xstrdup (stb, NULL);
7550 do_cleanups (old_chain);
7554 /* Display a null-terminated packet on stdout, for debugging, using C
7558 print_packet (const char *buf)
7560 puts_filtered ("\"");
7561 fputstr_filtered (buf, '"', gdb_stdout);
7562 puts_filtered ("\"");
7566 putpkt (const char *buf)
7568 return putpkt_binary (buf, strlen (buf));
7571 /* Send a packet to the remote machine, with error checking. The data
7572 of the packet is in BUF. The string in BUF can be at most
7573 get_remote_packet_size () - 5 to account for the $, # and checksum,
7574 and for a possible /0 if we are debugging (remote_debug) and want
7575 to print the sent packet as a string. */
7578 putpkt_binary (const char *buf, int cnt)
7580 struct remote_state *rs = get_remote_state ();
7582 unsigned char csum = 0;
7583 char *buf2 = alloca (cnt + 6);
7590 /* Catch cases like trying to read memory or listing threads while
7591 we're waiting for a stop reply. The remote server wouldn't be
7592 ready to handle this request, so we'd hang and timeout. We don't
7593 have to worry about this in synchronous mode, because in that
7594 case it's not possible to issue a command while the target is
7595 running. This is not a problem in non-stop mode, because in that
7596 case, the stub is always ready to process serial input. */
7597 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
7599 error (_("Cannot execute this command while the target is running.\n"
7600 "Use the \"interrupt\" command to stop the target\n"
7601 "and then try again."));
7604 /* We're sending out a new packet. Make sure we don't look at a
7605 stale cached response. */
7606 rs->cached_wait_status = 0;
7608 /* Copy the packet into buffer BUF2, encapsulating it
7609 and giving it a checksum. */
7614 for (i = 0; i < cnt; i++)
7620 *p++ = tohex ((csum >> 4) & 0xf);
7621 *p++ = tohex (csum & 0xf);
7623 /* Send it over and over until we get a positive ack. */
7627 int started_error_output = 0;
7631 struct cleanup *old_chain;
7635 str = escape_buffer (buf2, p - buf2);
7636 old_chain = make_cleanup (xfree, str);
7637 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
7638 gdb_flush (gdb_stdlog);
7639 do_cleanups (old_chain);
7641 remote_serial_write (buf2, p - buf2);
7643 /* If this is a no acks version of the remote protocol, send the
7644 packet and move on. */
7648 /* Read until either a timeout occurs (-2) or '+' is read.
7649 Handle any notification that arrives in the mean time. */
7652 ch = readchar (remote_timeout);
7660 case SERIAL_TIMEOUT:
7663 if (started_error_output)
7665 putchar_unfiltered ('\n');
7666 started_error_output = 0;
7675 fprintf_unfiltered (gdb_stdlog, "Ack\n");
7679 fprintf_unfiltered (gdb_stdlog, "Nak\n");
7681 case SERIAL_TIMEOUT:
7685 break; /* Retransmit buffer. */
7689 fprintf_unfiltered (gdb_stdlog,
7690 "Packet instead of Ack, ignoring it\n");
7691 /* It's probably an old response sent because an ACK
7692 was lost. Gobble up the packet and ack it so it
7693 doesn't get retransmitted when we resend this
7696 remote_serial_write ("+", 1);
7697 continue; /* Now, go look for +. */
7704 /* If we got a notification, handle it, and go back to looking
7706 /* We've found the start of a notification. Now
7707 collect the data. */
7708 val = read_frame (&rs->buf, &rs->buf_size);
7713 struct cleanup *old_chain;
7716 str = escape_buffer (rs->buf, val);
7717 old_chain = make_cleanup (xfree, str);
7718 fprintf_unfiltered (gdb_stdlog,
7719 " Notification received: %s\n",
7721 do_cleanups (old_chain);
7723 handle_notification (rs->notif_state, rs->buf);
7724 /* We're in sync now, rewait for the ack. */
7731 if (!started_error_output)
7733 started_error_output = 1;
7734 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7736 fputc_unfiltered (ch & 0177, gdb_stdlog);
7737 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7746 if (!started_error_output)
7748 started_error_output = 1;
7749 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7751 fputc_unfiltered (ch & 0177, gdb_stdlog);
7755 break; /* Here to retransmit. */
7759 /* This is wrong. If doing a long backtrace, the user should be
7760 able to get out next time we call QUIT, without anything as
7761 violent as interrupt_query. If we want to provide a way out of
7762 here without getting to the next QUIT, it should be based on
7763 hitting ^C twice as in remote_wait. */
7774 /* Come here after finding the start of a frame when we expected an
7775 ack. Do our best to discard the rest of this packet. */
7784 c = readchar (remote_timeout);
7787 case SERIAL_TIMEOUT:
7788 /* Nothing we can do. */
7791 /* Discard the two bytes of checksum and stop. */
7792 c = readchar (remote_timeout);
7794 c = readchar (remote_timeout);
7797 case '*': /* Run length encoding. */
7798 /* Discard the repeat count. */
7799 c = readchar (remote_timeout);
7804 /* A regular character. */
7810 /* Come here after finding the start of the frame. Collect the rest
7811 into *BUF, verifying the checksum, length, and handling run-length
7812 compression. NUL terminate the buffer. If there is not enough room,
7813 expand *BUF using xrealloc.
7815 Returns -1 on error, number of characters in buffer (ignoring the
7816 trailing NULL) on success. (could be extended to return one of the
7817 SERIAL status indications). */
7820 read_frame (char **buf_p,
7827 struct remote_state *rs = get_remote_state ();
7834 c = readchar (remote_timeout);
7837 case SERIAL_TIMEOUT:
7839 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
7843 fputs_filtered ("Saw new packet start in middle of old one\n",
7845 return -1; /* Start a new packet, count retries. */
7848 unsigned char pktcsum;
7854 check_0 = readchar (remote_timeout);
7856 check_1 = readchar (remote_timeout);
7858 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7861 fputs_filtered ("Timeout in checksum, retrying\n",
7865 else if (check_0 < 0 || check_1 < 0)
7868 fputs_filtered ("Communication error in checksum\n",
7873 /* Don't recompute the checksum; with no ack packets we
7874 don't have any way to indicate a packet retransmission
7879 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7880 if (csum == pktcsum)
7885 struct cleanup *old_chain;
7888 str = escape_buffer (buf, bc);
7889 old_chain = make_cleanup (xfree, str);
7890 fprintf_unfiltered (gdb_stdlog,
7891 "Bad checksum, sentsum=0x%x, "
7892 "csum=0x%x, buf=%s\n",
7893 pktcsum, csum, str);
7894 do_cleanups (old_chain);
7896 /* Number of characters in buffer ignoring trailing
7900 case '*': /* Run length encoding. */
7905 c = readchar (remote_timeout);
7907 repeat = c - ' ' + 3; /* Compute repeat count. */
7909 /* The character before ``*'' is repeated. */
7911 if (repeat > 0 && repeat <= 255 && bc > 0)
7913 if (bc + repeat - 1 >= *sizeof_buf - 1)
7915 /* Make some more room in the buffer. */
7916 *sizeof_buf += repeat;
7917 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7921 memset (&buf[bc], buf[bc - 1], repeat);
7927 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7931 if (bc >= *sizeof_buf - 1)
7933 /* Make some more room in the buffer. */
7935 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7946 /* Read a packet from the remote machine, with error checking, and
7947 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7948 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7949 rather than timing out; this is used (in synchronous mode) to wait
7950 for a target that is is executing user code to stop. */
7951 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7952 don't have to change all the calls to getpkt to deal with the
7953 return value, because at the moment I don't know what the right
7954 thing to do it for those. */
7962 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7966 /* Read a packet from the remote machine, with error checking, and
7967 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7968 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7969 rather than timing out; this is used (in synchronous mode) to wait
7970 for a target that is is executing user code to stop. If FOREVER ==
7971 0, this function is allowed to time out gracefully and return an
7972 indication of this to the caller. Otherwise return the number of
7973 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7974 enough reason to return to the caller. *IS_NOTIF is an output
7975 boolean that indicates whether *BUF holds a notification or not
7976 (a regular packet). */
7979 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7980 int expecting_notif, int *is_notif)
7982 struct remote_state *rs = get_remote_state ();
7988 /* We're reading a new response. Make sure we don't look at a
7989 previously cached response. */
7990 rs->cached_wait_status = 0;
7992 strcpy (*buf, "timeout");
7995 timeout = watchdog > 0 ? watchdog : -1;
7996 else if (expecting_notif)
7997 timeout = 0; /* There should already be a char in the buffer. If
8000 timeout = remote_timeout;
8004 /* Process any number of notifications, and then return when
8008 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
8010 for (tries = 1; tries <= MAX_TRIES; tries++)
8012 /* This can loop forever if the remote side sends us
8013 characters continuously, but if it pauses, we'll get
8014 SERIAL_TIMEOUT from readchar because of timeout. Then
8015 we'll count that as a retry.
8017 Note that even when forever is set, we will only wait
8018 forever prior to the start of a packet. After that, we
8019 expect characters to arrive at a brisk pace. They should
8020 show up within remote_timeout intervals. */
8022 c = readchar (timeout);
8023 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
8025 if (c == SERIAL_TIMEOUT)
8027 if (expecting_notif)
8028 return -1; /* Don't complain, it's normal to not get
8029 anything in this case. */
8031 if (forever) /* Watchdog went off? Kill the target. */
8034 remote_unpush_target ();
8035 throw_error (TARGET_CLOSE_ERROR,
8036 _("Watchdog timeout has expired. "
8037 "Target detached."));
8040 fputs_filtered ("Timed out.\n", gdb_stdlog);
8044 /* We've found the start of a packet or notification.
8045 Now collect the data. */
8046 val = read_frame (buf, sizeof_buf);
8051 remote_serial_write ("-", 1);
8054 if (tries > MAX_TRIES)
8056 /* We have tried hard enough, and just can't receive the
8057 packet/notification. Give up. */
8058 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
8060 /* Skip the ack char if we're in no-ack mode. */
8061 if (!rs->noack_mode)
8062 remote_serial_write ("+", 1);
8066 /* If we got an ordinary packet, return that to our caller. */
8071 struct cleanup *old_chain;
8074 str = escape_buffer (*buf, val);
8075 old_chain = make_cleanup (xfree, str);
8076 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8077 do_cleanups (old_chain);
8080 /* Skip the ack char if we're in no-ack mode. */
8081 if (!rs->noack_mode)
8082 remote_serial_write ("+", 1);
8083 if (is_notif != NULL)
8088 /* If we got a notification, handle it, and go back to looking
8092 gdb_assert (c == '%');
8096 struct cleanup *old_chain;
8099 str = escape_buffer (*buf, val);
8100 old_chain = make_cleanup (xfree, str);
8101 fprintf_unfiltered (gdb_stdlog,
8102 " Notification received: %s\n",
8104 do_cleanups (old_chain);
8106 if (is_notif != NULL)
8109 handle_notification (rs->notif_state, *buf);
8111 /* Notifications require no acknowledgement. */
8113 if (expecting_notif)
8120 getpkt_sane (char **buf, long *sizeof_buf, int forever)
8122 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
8126 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8129 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8133 /* Check whether EVENT is a fork event for the process specified
8134 by the pid passed in DATA, and if it is, kill the fork child. */
8137 kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8138 QUEUE_ITER (stop_reply_p) *iter,
8142 struct queue_iter_param *param = data;
8143 int parent_pid = *(int *) param->input;
8145 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8147 struct remote_state *rs = get_remote_state ();
8148 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8151 res = remote_vkill (child_pid, rs);
8153 error (_("Can't kill fork child process %d"), child_pid);
8159 /* Kill any new fork children of process PID that haven't been
8160 processed by follow_fork. */
8163 kill_new_fork_children (int pid, struct remote_state *rs)
8165 struct thread_info *thread;
8166 struct notif_client *notif = ¬if_client_stop;
8167 struct queue_iter_param param;
8169 /* Kill the fork child threads of any threads in process PID
8170 that are stopped at a fork event. */
8171 ALL_NON_EXITED_THREADS (thread)
8173 struct target_waitstatus *ws = &thread->pending_follow;
8175 if (is_pending_fork_parent (ws, pid, thread->ptid))
8177 struct remote_state *rs = get_remote_state ();
8178 int child_pid = ptid_get_pid (ws->value.related_pid);
8181 res = remote_vkill (child_pid, rs);
8183 error (_("Can't kill fork child process %d"), child_pid);
8187 /* Check for any pending fork events (not reported or processed yet)
8188 in process PID and kill those fork child threads as well. */
8189 remote_notif_get_pending_events (notif);
8191 param.output = NULL;
8192 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8193 kill_child_of_pending_fork, ¶m);
8198 remote_kill (struct target_ops *ops)
8201 /* Catch errors so the user can quit from gdb even when we
8202 aren't on speaking terms with the remote system. */
8207 CATCH (ex, RETURN_MASK_ERROR)
8209 if (ex.error == TARGET_CLOSE_ERROR)
8211 /* If we got an (EOF) error that caused the target
8212 to go away, then we're done, that's what we wanted.
8213 "k" is susceptible to cause a premature EOF, given
8214 that the remote server isn't actually required to
8215 reply to "k", and it can happen that it doesn't
8216 even get to reply ACK to the "k". */
8220 /* Otherwise, something went wrong. We didn't actually kill
8221 the target. Just propagate the exception, and let the
8222 user or higher layers decide what to do. */
8223 throw_exception (ex);
8227 /* We've killed the remote end, we get to mourn it. Since this is
8228 target remote, single-process, mourning the inferior also
8229 unpushes remote_ops. */
8230 target_mourn_inferior ();
8234 remote_vkill (int pid, struct remote_state *rs)
8236 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
8239 /* Tell the remote target to detach. */
8240 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
8242 getpkt (&rs->buf, &rs->buf_size, 0);
8244 switch (packet_ok (rs->buf,
8245 &remote_protocol_packets[PACKET_vKill]))
8251 case PACKET_UNKNOWN:
8254 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8259 extended_remote_kill (struct target_ops *ops)
8262 int pid = ptid_get_pid (inferior_ptid);
8263 struct remote_state *rs = get_remote_state ();
8265 /* If we're stopped while forking and we haven't followed yet, kill the
8266 child task. We need to do this before killing the parent task
8267 because if this is a vfork then the parent will be sleeping. */
8268 kill_new_fork_children (pid, rs);
8270 res = remote_vkill (pid, rs);
8271 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
8273 /* Don't try 'k' on a multi-process aware stub -- it has no way
8274 to specify the pid. */
8278 getpkt (&rs->buf, &rs->buf_size, 0);
8279 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8282 /* Don't wait for it to die. I'm not really sure it matters whether
8283 we do or not. For the existing stubs, kill is a noop. */
8289 error (_("Can't kill process"));
8291 target_mourn_inferior ();
8295 remote_mourn (struct target_ops *target)
8297 unpush_target (target);
8299 /* remote_close takes care of doing most of the clean up. */
8300 generic_mourn_inferior ();
8304 extended_remote_mourn (struct target_ops *target)
8306 struct remote_state *rs = get_remote_state ();
8308 /* In case we got here due to an error, but we're going to stay
8310 rs->waiting_for_stop_reply = 0;
8312 /* If the current general thread belonged to the process we just
8313 detached from or has exited, the remote side current general
8314 thread becomes undefined. Considering a case like this:
8316 - We just got here due to a detach.
8317 - The process that we're detaching from happens to immediately
8318 report a global breakpoint being hit in non-stop mode, in the
8319 same thread we had selected before.
8320 - GDB attaches to this process again.
8321 - This event happens to be the next event we handle.
8323 GDB would consider that the current general thread didn't need to
8324 be set on the stub side (with Hg), since for all it knew,
8325 GENERAL_THREAD hadn't changed.
8327 Notice that although in all-stop mode, the remote server always
8328 sets the current thread to the thread reporting the stop event,
8329 that doesn't happen in non-stop mode; in non-stop, the stub *must
8330 not* change the current thread when reporting a breakpoint hit,
8331 due to the decoupling of event reporting and event handling.
8333 To keep things simple, we always invalidate our notion of the
8335 record_currthread (rs, minus_one_ptid);
8337 /* Unlike "target remote", we do not want to unpush the target; then
8338 the next time the user says "run", we won't be connected. */
8340 /* Call common code to mark the inferior as not running. */
8341 generic_mourn_inferior ();
8343 if (!have_inferiors ())
8345 if (!remote_multi_process_p (rs))
8347 /* Check whether the target is running now - some remote stubs
8348 automatically restart after kill. */
8350 getpkt (&rs->buf, &rs->buf_size, 0);
8352 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8354 /* Assume that the target has been restarted. Set
8355 inferior_ptid so that bits of core GDB realizes
8356 there's something here, e.g., so that the user can
8357 say "kill" again. */
8358 inferior_ptid = magic_null_ptid;
8365 extended_remote_supports_disable_randomization (struct target_ops *self)
8367 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
8371 extended_remote_disable_randomization (int val)
8373 struct remote_state *rs = get_remote_state ();
8376 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8379 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8381 error (_("Target does not support QDisableRandomization."));
8382 if (strcmp (reply, "OK") != 0)
8383 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8387 extended_remote_run (char *args)
8389 struct remote_state *rs = get_remote_state ();
8392 /* If the user has disabled vRun support, or we have detected that
8393 support is not available, do not try it. */
8394 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
8397 strcpy (rs->buf, "vRun;");
8398 len = strlen (rs->buf);
8400 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8401 error (_("Remote file name too long for run packet"));
8402 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8403 strlen (remote_exec_file));
8405 gdb_assert (args != NULL);
8408 struct cleanup *back_to;
8412 argv = gdb_buildargv (args);
8413 back_to = make_cleanup_freeargv (argv);
8414 for (i = 0; argv[i] != NULL; i++)
8416 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8417 error (_("Argument list too long for run packet"));
8418 rs->buf[len++] = ';';
8419 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8422 do_cleanups (back_to);
8425 rs->buf[len++] = '\0';
8428 getpkt (&rs->buf, &rs->buf_size, 0);
8430 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
8433 /* We have a wait response. All is well. */
8435 case PACKET_UNKNOWN:
8438 if (remote_exec_file[0] == '\0')
8439 error (_("Running the default executable on the remote target failed; "
8440 "try \"set remote exec-file\"?"));
8442 error (_("Running \"%s\" on the remote target failed"),
8445 gdb_assert_not_reached (_("bad switch"));
8449 /* In the extended protocol we want to be able to do things like
8450 "run" and have them basically work as expected. So we need
8451 a special create_inferior function. We support changing the
8452 executable file and the command line arguments, but not the
8456 extended_remote_create_inferior (struct target_ops *ops,
8457 char *exec_file, char *args,
8458 char **env, int from_tty)
8462 struct remote_state *rs = get_remote_state ();
8464 /* If running asynchronously, register the target file descriptor
8465 with the event loop. */
8466 if (target_can_async_p ())
8469 /* Disable address space randomization if requested (and supported). */
8470 if (extended_remote_supports_disable_randomization (ops))
8471 extended_remote_disable_randomization (disable_randomization);
8473 /* Now restart the remote server. */
8474 run_worked = extended_remote_run (args) != -1;
8477 /* vRun was not supported. Fail if we need it to do what the
8479 if (remote_exec_file[0])
8480 error (_("Remote target does not support \"set remote exec-file\""));
8482 error (_("Remote target does not support \"set args\" or run <ARGS>"));
8484 /* Fall back to "R". */
8485 extended_remote_restart ();
8488 if (!have_inferiors ())
8490 /* Clean up from the last time we ran, before we mark the target
8491 running again. This will mark breakpoints uninserted, and
8492 get_offsets may insert breakpoints. */
8493 init_thread_list ();
8494 init_wait_for_inferior ();
8497 /* vRun's success return is a stop reply. */
8498 stop_reply = run_worked ? rs->buf : NULL;
8499 add_current_inferior_and_thread (stop_reply);
8501 /* Get updated offsets, if the stub uses qOffsets. */
8506 /* Given a location's target info BP_TGT and the packet buffer BUF, output
8507 the list of conditions (in agent expression bytecode format), if any, the
8508 target needs to evaluate. The output is placed into the packet buffer
8509 started from BUF and ended at BUF_END. */
8512 remote_add_target_side_condition (struct gdbarch *gdbarch,
8513 struct bp_target_info *bp_tgt, char *buf,
8516 struct agent_expr *aexpr = NULL;
8519 char *buf_start = buf;
8521 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8524 buf += strlen (buf);
8525 xsnprintf (buf, buf_end - buf, "%s", ";");
8528 /* Send conditions to the target and free the vector. */
8530 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8533 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
8534 buf += strlen (buf);
8535 for (i = 0; i < aexpr->len; ++i)
8536 buf = pack_hex_byte (buf, aexpr->buf[i]);
8543 remote_add_target_side_commands (struct gdbarch *gdbarch,
8544 struct bp_target_info *bp_tgt, char *buf)
8546 struct agent_expr *aexpr = NULL;
8549 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8552 buf += strlen (buf);
8554 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8555 buf += strlen (buf);
8557 /* Concatenate all the agent expressions that are commands into the
8560 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8563 sprintf (buf, "X%x,", aexpr->len);
8564 buf += strlen (buf);
8565 for (i = 0; i < aexpr->len; ++i)
8566 buf = pack_hex_byte (buf, aexpr->buf[i]);
8571 /* Insert a breakpoint. On targets that have software breakpoint
8572 support, we ask the remote target to do the work; on targets
8573 which don't, we insert a traditional memory breakpoint. */
8576 remote_insert_breakpoint (struct target_ops *ops,
8577 struct gdbarch *gdbarch,
8578 struct bp_target_info *bp_tgt)
8580 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8581 If it succeeds, then set the support to PACKET_ENABLE. If it
8582 fails, and the user has explicitly requested the Z support then
8583 report an error, otherwise, mark it disabled and go on. */
8585 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8587 CORE_ADDR addr = bp_tgt->reqstd_address;
8588 struct remote_state *rs;
8591 struct condition_list *cond = NULL;
8593 /* Make sure the remote is pointing at the right process, if
8595 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8596 set_general_process ();
8598 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8600 rs = get_remote_state ();
8602 endbuf = rs->buf + get_remote_packet_size ();
8607 addr = (ULONGEST) remote_address_masked (addr);
8608 p += hexnumstr (p, addr);
8609 xsnprintf (p, endbuf - p, ",%d", bpsize);
8611 if (remote_supports_cond_breakpoints (ops))
8612 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8614 if (remote_can_run_breakpoint_commands (ops))
8615 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8618 getpkt (&rs->buf, &rs->buf_size, 0);
8620 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
8625 bp_tgt->placed_address = addr;
8626 bp_tgt->placed_size = bpsize;
8628 case PACKET_UNKNOWN:
8633 /* If this breakpoint has target-side commands but this stub doesn't
8634 support Z0 packets, throw error. */
8635 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8636 throw_error (NOT_SUPPORTED_ERROR, _("\
8637 Target doesn't support breakpoints that have target side commands."));
8639 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
8643 remote_remove_breakpoint (struct target_ops *ops,
8644 struct gdbarch *gdbarch,
8645 struct bp_target_info *bp_tgt)
8647 CORE_ADDR addr = bp_tgt->placed_address;
8648 struct remote_state *rs = get_remote_state ();
8650 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
8653 char *endbuf = rs->buf + get_remote_packet_size ();
8655 /* Make sure the remote is pointing at the right process, if
8657 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8658 set_general_process ();
8664 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8665 p += hexnumstr (p, addr);
8666 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
8669 getpkt (&rs->buf, &rs->buf_size, 0);
8671 return (rs->buf[0] == 'E');
8674 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
8678 watchpoint_to_Z_packet (int type)
8683 return Z_PACKET_WRITE_WP;
8686 return Z_PACKET_READ_WP;
8689 return Z_PACKET_ACCESS_WP;
8692 internal_error (__FILE__, __LINE__,
8693 _("hw_bp_to_z: bad watchpoint type %d"), type);
8698 remote_insert_watchpoint (struct target_ops *self,
8699 CORE_ADDR addr, int len, int type,
8700 struct expression *cond)
8702 struct remote_state *rs = get_remote_state ();
8703 char *endbuf = rs->buf + get_remote_packet_size ();
8705 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8707 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8710 /* Make sure the remote is pointing at the right process, if
8712 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8713 set_general_process ();
8715 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
8716 p = strchr (rs->buf, '\0');
8717 addr = remote_address_masked (addr);
8718 p += hexnumstr (p, (ULONGEST) addr);
8719 xsnprintf (p, endbuf - p, ",%x", len);
8722 getpkt (&rs->buf, &rs->buf_size, 0);
8724 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8728 case PACKET_UNKNOWN:
8733 internal_error (__FILE__, __LINE__,
8734 _("remote_insert_watchpoint: reached end of function"));
8738 remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8739 CORE_ADDR start, int length)
8741 CORE_ADDR diff = remote_address_masked (addr - start);
8743 return diff < length;
8748 remote_remove_watchpoint (struct target_ops *self,
8749 CORE_ADDR addr, int len, int type,
8750 struct expression *cond)
8752 struct remote_state *rs = get_remote_state ();
8753 char *endbuf = rs->buf + get_remote_packet_size ();
8755 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8757 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
8760 /* Make sure the remote is pointing at the right process, if
8762 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8763 set_general_process ();
8765 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
8766 p = strchr (rs->buf, '\0');
8767 addr = remote_address_masked (addr);
8768 p += hexnumstr (p, (ULONGEST) addr);
8769 xsnprintf (p, endbuf - p, ",%x", len);
8771 getpkt (&rs->buf, &rs->buf_size, 0);
8773 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8776 case PACKET_UNKNOWN:
8781 internal_error (__FILE__, __LINE__,
8782 _("remote_remove_watchpoint: reached end of function"));
8786 int remote_hw_watchpoint_limit = -1;
8787 int remote_hw_watchpoint_length_limit = -1;
8788 int remote_hw_breakpoint_limit = -1;
8791 remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8792 CORE_ADDR addr, int len)
8794 if (remote_hw_watchpoint_length_limit == 0)
8796 else if (remote_hw_watchpoint_length_limit < 0)
8798 else if (len <= remote_hw_watchpoint_length_limit)
8805 remote_check_watch_resources (struct target_ops *self,
8806 int type, int cnt, int ot)
8808 if (type == bp_hardware_breakpoint)
8810 if (remote_hw_breakpoint_limit == 0)
8812 else if (remote_hw_breakpoint_limit < 0)
8814 else if (cnt <= remote_hw_breakpoint_limit)
8819 if (remote_hw_watchpoint_limit == 0)
8821 else if (remote_hw_watchpoint_limit < 0)
8825 else if (cnt <= remote_hw_watchpoint_limit)
8831 /* The to_stopped_by_sw_breakpoint method of target remote. */
8834 remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8836 struct remote_state *rs = get_remote_state ();
8838 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8841 /* The to_supports_stopped_by_sw_breakpoint method of target
8845 remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8847 struct remote_state *rs = get_remote_state ();
8849 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8852 /* The to_stopped_by_hw_breakpoint method of target remote. */
8855 remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8857 struct remote_state *rs = get_remote_state ();
8859 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8862 /* The to_supports_stopped_by_hw_breakpoint method of target
8866 remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8868 struct remote_state *rs = get_remote_state ();
8870 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8874 remote_stopped_by_watchpoint (struct target_ops *ops)
8876 struct remote_state *rs = get_remote_state ();
8878 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
8882 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
8884 struct remote_state *rs = get_remote_state ();
8887 if (remote_stopped_by_watchpoint (target))
8889 *addr_p = rs->remote_watch_data_address;
8898 remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8899 struct bp_target_info *bp_tgt)
8901 CORE_ADDR addr = bp_tgt->reqstd_address;
8902 struct remote_state *rs;
8907 /* The length field should be set to the size of a breakpoint
8908 instruction, even though we aren't inserting one ourselves. */
8910 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
8912 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8915 /* Make sure the remote is pointing at the right process, if
8917 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8918 set_general_process ();
8920 rs = get_remote_state ();
8922 endbuf = rs->buf + get_remote_packet_size ();
8928 addr = remote_address_masked (addr);
8929 p += hexnumstr (p, (ULONGEST) addr);
8930 xsnprintf (p, endbuf - p, ",%x", bpsize);
8932 if (remote_supports_cond_breakpoints (self))
8933 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8935 if (remote_can_run_breakpoint_commands (self))
8936 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8939 getpkt (&rs->buf, &rs->buf_size, 0);
8941 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8944 if (rs->buf[1] == '.')
8946 message = strchr (rs->buf + 2, '.');
8948 error (_("Remote failure reply: %s"), message + 1);
8951 case PACKET_UNKNOWN:
8954 bp_tgt->placed_address = addr;
8955 bp_tgt->placed_size = bpsize;
8958 internal_error (__FILE__, __LINE__,
8959 _("remote_insert_hw_breakpoint: reached end of function"));
8964 remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
8965 struct bp_target_info *bp_tgt)
8968 struct remote_state *rs = get_remote_state ();
8970 char *endbuf = rs->buf + get_remote_packet_size ();
8972 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
8975 /* Make sure the remote is pointing at the right process, if
8977 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8978 set_general_process ();
8984 addr = remote_address_masked (bp_tgt->placed_address);
8985 p += hexnumstr (p, (ULONGEST) addr);
8986 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8989 getpkt (&rs->buf, &rs->buf_size, 0);
8991 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8994 case PACKET_UNKNOWN:
8999 internal_error (__FILE__, __LINE__,
9000 _("remote_remove_hw_breakpoint: reached end of function"));
9003 /* Verify memory using the "qCRC:" request. */
9006 remote_verify_memory (struct target_ops *ops,
9007 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9009 struct remote_state *rs = get_remote_state ();
9010 unsigned long host_crc, target_crc;
9013 /* It doesn't make sense to use qCRC if the remote target is
9014 connected but not running. */
9015 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9017 enum packet_result result;
9019 /* Make sure the remote is pointing at the right process. */
9020 set_general_process ();
9022 /* FIXME: assumes lma can fit into long. */
9023 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9024 (long) lma, (long) size);
9027 /* Be clever; compute the host_crc before waiting for target
9029 host_crc = xcrc32 (data, size, 0xffffffff);
9031 getpkt (&rs->buf, &rs->buf_size, 0);
9033 result = packet_ok (rs->buf,
9034 &remote_protocol_packets[PACKET_qCRC]);
9035 if (result == PACKET_ERROR)
9037 else if (result == PACKET_OK)
9039 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9040 target_crc = target_crc * 16 + fromhex (*tmp);
9042 return (host_crc == target_crc);
9046 return simple_verify_memory (ops, data, lma, size);
9049 /* compare-sections command
9051 With no arguments, compares each loadable section in the exec bfd
9052 with the same memory range on the target, and reports mismatches.
9053 Useful for verifying the image on the target against the exec file. */
9056 compare_sections_command (char *args, int from_tty)
9059 struct cleanup *old_chain;
9061 const char *sectname;
9070 error (_("command cannot be used without an exec file"));
9072 /* Make sure the remote is pointing at the right process. */
9073 set_general_process ();
9075 if (args != NULL && strcmp (args, "-r") == 0)
9081 for (s = exec_bfd->sections; s; s = s->next)
9083 if (!(s->flags & SEC_LOAD))
9084 continue; /* Skip non-loadable section. */
9086 if (read_only && (s->flags & SEC_READONLY) == 0)
9087 continue; /* Skip writeable sections */
9089 size = bfd_get_section_size (s);
9091 continue; /* Skip zero-length section. */
9093 sectname = bfd_get_section_name (exec_bfd, s);
9094 if (args && strcmp (args, sectname) != 0)
9095 continue; /* Not the section selected by user. */
9097 matched = 1; /* Do this section. */
9100 sectdata = xmalloc (size);
9101 old_chain = make_cleanup (xfree, sectdata);
9102 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
9104 res = target_verify_memory (sectdata, lma, size);
9107 error (_("target memory fault, section %s, range %s -- %s"), sectname,
9108 paddress (target_gdbarch (), lma),
9109 paddress (target_gdbarch (), lma + size));
9111 printf_filtered ("Section %s, range %s -- %s: ", sectname,
9112 paddress (target_gdbarch (), lma),
9113 paddress (target_gdbarch (), lma + size));
9115 printf_filtered ("matched.\n");
9118 printf_filtered ("MIS-MATCHED!\n");
9122 do_cleanups (old_chain);
9125 warning (_("One or more sections of the target image does not match\n\
9126 the loaded file\n"));
9127 if (args && !matched)
9128 printf_filtered (_("No loaded section named '%s'.\n"), args);
9131 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9132 into remote target. The number of bytes written to the remote
9133 target is returned, or -1 for error. */
9135 static enum target_xfer_status
9136 remote_write_qxfer (struct target_ops *ops, const char *object_name,
9137 const char *annex, const gdb_byte *writebuf,
9138 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
9139 struct packet_config *packet)
9143 struct remote_state *rs = get_remote_state ();
9144 int max_size = get_memory_write_packet_size ();
9146 if (packet->support == PACKET_DISABLE)
9147 return TARGET_XFER_E_IO;
9149 /* Insert header. */
9150 i = snprintf (rs->buf, max_size,
9151 "qXfer:%s:write:%s:%s:",
9152 object_name, annex ? annex : "",
9153 phex_nz (offset, sizeof offset));
9154 max_size -= (i + 1);
9156 /* Escape as much data as fits into rs->buf. */
9157 buf_len = remote_escape_output
9158 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
9160 if (putpkt_binary (rs->buf, i + buf_len) < 0
9161 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9162 || packet_ok (rs->buf, packet) != PACKET_OK)
9163 return TARGET_XFER_E_IO;
9165 unpack_varlen_hex (rs->buf, &n);
9168 return TARGET_XFER_OK;
9171 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9172 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9173 number of bytes read is returned, or 0 for EOF, or -1 for error.
9174 The number of bytes read may be less than LEN without indicating an
9175 EOF. PACKET is checked and updated to indicate whether the remote
9176 target supports this object. */
9178 static enum target_xfer_status
9179 remote_read_qxfer (struct target_ops *ops, const char *object_name,
9181 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9182 ULONGEST *xfered_len,
9183 struct packet_config *packet)
9185 struct remote_state *rs = get_remote_state ();
9186 LONGEST i, n, packet_len;
9188 if (packet->support == PACKET_DISABLE)
9189 return TARGET_XFER_E_IO;
9191 /* Check whether we've cached an end-of-object packet that matches
9193 if (rs->finished_object)
9195 if (strcmp (object_name, rs->finished_object) == 0
9196 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9197 && offset == rs->finished_offset)
9198 return TARGET_XFER_EOF;
9201 /* Otherwise, we're now reading something different. Discard
9203 xfree (rs->finished_object);
9204 xfree (rs->finished_annex);
9205 rs->finished_object = NULL;
9206 rs->finished_annex = NULL;
9209 /* Request only enough to fit in a single packet. The actual data
9210 may not, since we don't know how much of it will need to be escaped;
9211 the target is free to respond with slightly less data. We subtract
9212 five to account for the response type and the protocol frame. */
9213 n = min (get_remote_packet_size () - 5, len);
9214 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9215 object_name, annex ? annex : "",
9216 phex_nz (offset, sizeof offset),
9217 phex_nz (n, sizeof n));
9218 i = putpkt (rs->buf);
9220 return TARGET_XFER_E_IO;
9223 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9224 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
9225 return TARGET_XFER_E_IO;
9227 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9228 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9230 /* 'm' means there is (or at least might be) more data after this
9231 batch. That does not make sense unless there's at least one byte
9232 of data in this reply. */
9233 if (rs->buf[0] == 'm' && packet_len == 1)
9234 error (_("Remote qXfer reply contained no data."));
9236 /* Got some data. */
9237 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9238 packet_len - 1, readbuf, n);
9240 /* 'l' is an EOF marker, possibly including a final block of data,
9241 or possibly empty. If we have the final block of a non-empty
9242 object, record this fact to bypass a subsequent partial read. */
9243 if (rs->buf[0] == 'l' && offset + i > 0)
9245 rs->finished_object = xstrdup (object_name);
9246 rs->finished_annex = xstrdup (annex ? annex : "");
9247 rs->finished_offset = offset + i;
9251 return TARGET_XFER_EOF;
9255 return TARGET_XFER_OK;
9259 static enum target_xfer_status
9260 remote_xfer_partial (struct target_ops *ops, enum target_object object,
9261 const char *annex, gdb_byte *readbuf,
9262 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9263 ULONGEST *xfered_len)
9265 struct remote_state *rs;
9269 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
9271 set_remote_traceframe ();
9272 set_general_thread (inferior_ptid);
9274 rs = get_remote_state ();
9276 /* Handle memory using the standard memory routines. */
9277 if (object == TARGET_OBJECT_MEMORY)
9279 /* If the remote target is connected but not running, we should
9280 pass this request down to a lower stratum (e.g. the executable
9282 if (!target_has_execution)
9283 return TARGET_XFER_EOF;
9285 if (writebuf != NULL)
9286 return remote_write_bytes (offset, writebuf, len, unit_size,
9289 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9293 /* Handle SPU memory using qxfer packets. */
9294 if (object == TARGET_OBJECT_SPU)
9297 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9298 xfered_len, &remote_protocol_packets
9299 [PACKET_qXfer_spu_read]);
9301 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9302 xfered_len, &remote_protocol_packets
9303 [PACKET_qXfer_spu_write]);
9306 /* Handle extra signal info using qxfer packets. */
9307 if (object == TARGET_OBJECT_SIGNAL_INFO)
9310 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9311 xfered_len, &remote_protocol_packets
9312 [PACKET_qXfer_siginfo_read]);
9314 return remote_write_qxfer (ops, "siginfo", annex,
9315 writebuf, offset, len, xfered_len,
9316 &remote_protocol_packets
9317 [PACKET_qXfer_siginfo_write]);
9320 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9323 return remote_read_qxfer (ops, "statictrace", annex,
9324 readbuf, offset, len, xfered_len,
9325 &remote_protocol_packets
9326 [PACKET_qXfer_statictrace_read]);
9328 return TARGET_XFER_E_IO;
9331 /* Only handle flash writes. */
9332 if (writebuf != NULL)
9338 case TARGET_OBJECT_FLASH:
9339 return remote_flash_write (ops, offset, len, xfered_len,
9343 return TARGET_XFER_E_IO;
9347 /* Map pre-existing objects onto letters. DO NOT do this for new
9348 objects!!! Instead specify new query packets. */
9351 case TARGET_OBJECT_AVR:
9355 case TARGET_OBJECT_AUXV:
9356 gdb_assert (annex == NULL);
9357 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9359 &remote_protocol_packets[PACKET_qXfer_auxv]);
9361 case TARGET_OBJECT_AVAILABLE_FEATURES:
9362 return remote_read_qxfer
9363 (ops, "features", annex, readbuf, offset, len, xfered_len,
9364 &remote_protocol_packets[PACKET_qXfer_features]);
9366 case TARGET_OBJECT_LIBRARIES:
9367 return remote_read_qxfer
9368 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
9369 &remote_protocol_packets[PACKET_qXfer_libraries]);
9371 case TARGET_OBJECT_LIBRARIES_SVR4:
9372 return remote_read_qxfer
9373 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
9374 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9376 case TARGET_OBJECT_MEMORY_MAP:
9377 gdb_assert (annex == NULL);
9378 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9380 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9382 case TARGET_OBJECT_OSDATA:
9383 /* Should only get here if we're connected. */
9384 gdb_assert (rs->remote_desc);
9385 return remote_read_qxfer
9386 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
9387 &remote_protocol_packets[PACKET_qXfer_osdata]);
9389 case TARGET_OBJECT_THREADS:
9390 gdb_assert (annex == NULL);
9391 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9393 &remote_protocol_packets[PACKET_qXfer_threads]);
9395 case TARGET_OBJECT_TRACEFRAME_INFO:
9396 gdb_assert (annex == NULL);
9397 return remote_read_qxfer
9398 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
9399 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
9401 case TARGET_OBJECT_FDPIC:
9402 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9404 &remote_protocol_packets[PACKET_qXfer_fdpic]);
9406 case TARGET_OBJECT_OPENVMS_UIB:
9407 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9409 &remote_protocol_packets[PACKET_qXfer_uib]);
9411 case TARGET_OBJECT_BTRACE:
9412 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9414 &remote_protocol_packets[PACKET_qXfer_btrace]);
9416 case TARGET_OBJECT_BTRACE_CONF:
9417 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9419 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9421 case TARGET_OBJECT_EXEC_FILE:
9422 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9424 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9427 return TARGET_XFER_E_IO;
9430 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
9431 large enough let the caller deal with it. */
9432 if (len < get_remote_packet_size ())
9433 return TARGET_XFER_E_IO;
9434 len = get_remote_packet_size ();
9436 /* Except for querying the minimum buffer size, target must be open. */
9437 if (!rs->remote_desc)
9438 error (_("remote query is only available after target open"));
9440 gdb_assert (annex != NULL);
9441 gdb_assert (readbuf != NULL);
9447 /* We used one buffer char for the remote protocol q command and
9448 another for the query type. As the remote protocol encapsulation
9449 uses 4 chars plus one extra in case we are debugging
9450 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9453 while (annex[i] && (i < (get_remote_packet_size () - 8)))
9455 /* Bad caller may have sent forbidden characters. */
9456 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9461 gdb_assert (annex[i] == '\0');
9463 i = putpkt (rs->buf);
9465 return TARGET_XFER_E_IO;
9467 getpkt (&rs->buf, &rs->buf_size, 0);
9468 strcpy ((char *) readbuf, rs->buf);
9470 *xfered_len = strlen ((char *) readbuf);
9471 return TARGET_XFER_OK;
9475 remote_search_memory (struct target_ops* ops,
9476 CORE_ADDR start_addr, ULONGEST search_space_len,
9477 const gdb_byte *pattern, ULONGEST pattern_len,
9478 CORE_ADDR *found_addrp)
9480 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
9481 struct remote_state *rs = get_remote_state ();
9482 int max_size = get_memory_write_packet_size ();
9483 struct packet_config *packet =
9484 &remote_protocol_packets[PACKET_qSearch_memory];
9485 /* Number of packet bytes used to encode the pattern;
9486 this could be more than PATTERN_LEN due to escape characters. */
9487 int escaped_pattern_len;
9488 /* Amount of pattern that was encodable in the packet. */
9489 int used_pattern_len;
9492 ULONGEST found_addr;
9494 /* Don't go to the target if we don't have to.
9495 This is done before checking packet->support to avoid the possibility that
9496 a success for this edge case means the facility works in general. */
9497 if (pattern_len > search_space_len)
9499 if (pattern_len == 0)
9501 *found_addrp = start_addr;
9505 /* If we already know the packet isn't supported, fall back to the simple
9506 way of searching memory. */
9508 if (packet_config_support (packet) == PACKET_DISABLE)
9510 /* Target doesn't provided special support, fall back and use the
9511 standard support (copy memory and do the search here). */
9512 return simple_search_memory (ops, start_addr, search_space_len,
9513 pattern, pattern_len, found_addrp);
9516 /* Make sure the remote is pointing at the right process. */
9517 set_general_process ();
9519 /* Insert header. */
9520 i = snprintf (rs->buf, max_size,
9521 "qSearch:memory:%s;%s;",
9522 phex_nz (start_addr, addr_size),
9523 phex_nz (search_space_len, sizeof (search_space_len)));
9524 max_size -= (i + 1);
9526 /* Escape as much data as fits into rs->buf. */
9527 escaped_pattern_len =
9528 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
9529 &used_pattern_len, max_size);
9531 /* Bail if the pattern is too large. */
9532 if (used_pattern_len != pattern_len)
9533 error (_("Pattern is too large to transmit to remote target."));
9535 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9536 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9537 || packet_ok (rs->buf, packet) != PACKET_OK)
9539 /* The request may not have worked because the command is not
9540 supported. If so, fall back to the simple way. */
9541 if (packet->support == PACKET_DISABLE)
9543 return simple_search_memory (ops, start_addr, search_space_len,
9544 pattern, pattern_len, found_addrp);
9549 if (rs->buf[0] == '0')
9551 else if (rs->buf[0] == '1')
9554 if (rs->buf[1] != ',')
9555 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9556 unpack_varlen_hex (rs->buf + 2, &found_addr);
9557 *found_addrp = found_addr;
9560 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
9566 remote_rcmd (struct target_ops *self, const char *command,
9567 struct ui_file *outbuf)
9569 struct remote_state *rs = get_remote_state ();
9572 if (!rs->remote_desc)
9573 error (_("remote rcmd is only available after target open"));
9575 /* Send a NULL command across as an empty command. */
9576 if (command == NULL)
9579 /* The query prefix. */
9580 strcpy (rs->buf, "qRcmd,");
9581 p = strchr (rs->buf, '\0');
9583 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9584 > get_remote_packet_size ())
9585 error (_("\"monitor\" command ``%s'' is too long."), command);
9587 /* Encode the actual command. */
9588 bin2hex ((const gdb_byte *) command, p, strlen (command));
9590 if (putpkt (rs->buf) < 0)
9591 error (_("Communication problem with target."));
9593 /* get/display the response */
9598 /* XXX - see also remote_get_noisy_reply(). */
9599 QUIT; /* Allow user to bail out with ^C. */
9601 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9603 /* Timeout. Continue to (try to) read responses.
9604 This is better than stopping with an error, assuming the stub
9605 is still executing the (long) monitor command.
9606 If needed, the user can interrupt gdb using C-c, obtaining
9607 an effect similar to stop on timeout. */
9612 error (_("Target does not support this command."));
9613 if (buf[0] == 'O' && buf[1] != 'K')
9615 remote_console_output (buf + 1); /* 'O' message from stub. */
9618 if (strcmp (buf, "OK") == 0)
9620 if (strlen (buf) == 3 && buf[0] == 'E'
9621 && isdigit (buf[1]) && isdigit (buf[2]))
9623 error (_("Protocol error with Rcmd"));
9625 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9627 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
9629 fputc_unfiltered (c, outbuf);
9635 static VEC(mem_region_s) *
9636 remote_memory_map (struct target_ops *ops)
9638 VEC(mem_region_s) *result = NULL;
9639 char *text = target_read_stralloc (¤t_target,
9640 TARGET_OBJECT_MEMORY_MAP, NULL);
9644 struct cleanup *back_to = make_cleanup (xfree, text);
9646 result = parse_memory_map (text);
9647 do_cleanups (back_to);
9654 packet_command (char *args, int from_tty)
9656 struct remote_state *rs = get_remote_state ();
9658 if (!rs->remote_desc)
9659 error (_("command can only be used with remote target"));
9662 error (_("remote-packet command requires packet text as argument"));
9664 puts_filtered ("sending: ");
9665 print_packet (args);
9666 puts_filtered ("\n");
9669 getpkt (&rs->buf, &rs->buf_size, 0);
9670 puts_filtered ("received: ");
9671 print_packet (rs->buf);
9672 puts_filtered ("\n");
9676 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9678 static void display_thread_info (struct gdb_ext_thread_info *info);
9680 static void threadset_test_cmd (char *cmd, int tty);
9682 static void threadalive_test (char *cmd, int tty);
9684 static void threadlist_test_cmd (char *cmd, int tty);
9686 int get_and_display_threadinfo (threadref *ref);
9688 static void threadinfo_test_cmd (char *cmd, int tty);
9690 static int thread_display_step (threadref *ref, void *context);
9692 static void threadlist_update_test_cmd (char *cmd, int tty);
9694 static void init_remote_threadtests (void);
9696 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9699 threadset_test_cmd (char *cmd, int tty)
9701 int sample_thread = SAMPLE_THREAD;
9703 printf_filtered (_("Remote threadset test\n"));
9704 set_general_thread (sample_thread);
9709 threadalive_test (char *cmd, int tty)
9711 int sample_thread = SAMPLE_THREAD;
9712 int pid = ptid_get_pid (inferior_ptid);
9713 ptid_t ptid = ptid_build (pid, sample_thread, 0);
9715 if (remote_thread_alive (ptid))
9716 printf_filtered ("PASS: Thread alive test\n");
9718 printf_filtered ("FAIL: Thread alive test\n");
9721 void output_threadid (char *title, threadref *ref);
9724 output_threadid (char *title, threadref *ref)
9728 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
9730 printf_filtered ("%s %s\n", title, (&hexid[0]));
9734 threadlist_test_cmd (char *cmd, int tty)
9737 threadref nextthread;
9738 int done, result_count;
9739 threadref threadlist[3];
9741 printf_filtered ("Remote Threadlist test\n");
9742 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9743 &result_count, &threadlist[0]))
9744 printf_filtered ("FAIL: threadlist test\n");
9747 threadref *scan = threadlist;
9748 threadref *limit = scan + result_count;
9750 while (scan < limit)
9751 output_threadid (" thread ", scan++);
9756 display_thread_info (struct gdb_ext_thread_info *info)
9758 output_threadid ("Threadid: ", &info->threadid);
9759 printf_filtered ("Name: %s\n ", info->shortname);
9760 printf_filtered ("State: %s\n", info->display);
9761 printf_filtered ("other: %s\n\n", info->more_display);
9765 get_and_display_threadinfo (threadref *ref)
9769 struct gdb_ext_thread_info threadinfo;
9771 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9772 | TAG_MOREDISPLAY | TAG_DISPLAY;
9773 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9774 display_thread_info (&threadinfo);
9779 threadinfo_test_cmd (char *cmd, int tty)
9781 int athread = SAMPLE_THREAD;
9785 int_to_threadref (&thread, athread);
9786 printf_filtered ("Remote Threadinfo test\n");
9787 if (!get_and_display_threadinfo (&thread))
9788 printf_filtered ("FAIL cannot get thread info\n");
9792 thread_display_step (threadref *ref, void *context)
9794 /* output_threadid(" threadstep ",ref); *//* simple test */
9795 return get_and_display_threadinfo (ref);
9799 threadlist_update_test_cmd (char *cmd, int tty)
9801 printf_filtered ("Remote Threadlist update test\n");
9802 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9806 init_remote_threadtests (void)
9808 add_com ("tlist", class_obscure, threadlist_test_cmd,
9809 _("Fetch and print the remote list of "
9810 "thread identifiers, one pkt only"));
9811 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
9812 _("Fetch and display info about one thread"));
9813 add_com ("tset", class_obscure, threadset_test_cmd,
9814 _("Test setting to a different thread"));
9815 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
9816 _("Iterate through updating all remote thread info"));
9817 add_com ("talive", class_obscure, threadalive_test,
9818 _(" Remote thread alive test "));
9823 /* Convert a thread ID to a string. Returns the string in a static
9827 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
9829 static char buf[64];
9830 struct remote_state *rs = get_remote_state ();
9832 if (ptid_equal (ptid, null_ptid))
9833 return normal_pid_to_str (ptid);
9834 else if (ptid_is_pid (ptid))
9836 /* Printing an inferior target id. */
9838 /* When multi-process extensions are off, there's no way in the
9839 remote protocol to know the remote process id, if there's any
9840 at all. There's one exception --- when we're connected with
9841 target extended-remote, and we manually attached to a process
9842 with "attach PID". We don't record anywhere a flag that
9843 allows us to distinguish that case from the case of
9844 connecting with extended-remote and the stub already being
9845 attached to a process, and reporting yes to qAttached, hence
9846 no smart special casing here. */
9847 if (!remote_multi_process_p (rs))
9849 xsnprintf (buf, sizeof buf, "Remote target");
9853 return normal_pid_to_str (ptid);
9857 if (ptid_equal (magic_null_ptid, ptid))
9858 xsnprintf (buf, sizeof buf, "Thread <main>");
9859 else if (rs->extended && remote_multi_process_p (rs))
9860 if (ptid_get_lwp (ptid) == 0)
9861 return normal_pid_to_str (ptid);
9863 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9864 ptid_get_pid (ptid), ptid_get_lwp (ptid));
9866 xsnprintf (buf, sizeof buf, "Thread %ld",
9867 ptid_get_lwp (ptid));
9872 /* Get the address of the thread local variable in OBJFILE which is
9873 stored at OFFSET within the thread local storage for thread PTID. */
9876 remote_get_thread_local_address (struct target_ops *ops,
9877 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
9879 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
9881 struct remote_state *rs = get_remote_state ();
9883 char *endp = rs->buf + get_remote_packet_size ();
9884 enum packet_result result;
9886 strcpy (p, "qGetTLSAddr:");
9888 p = write_ptid (p, endp, ptid);
9890 p += hexnumstr (p, offset);
9892 p += hexnumstr (p, lm);
9896 getpkt (&rs->buf, &rs->buf_size, 0);
9897 result = packet_ok (rs->buf,
9898 &remote_protocol_packets[PACKET_qGetTLSAddr]);
9899 if (result == PACKET_OK)
9903 unpack_varlen_hex (rs->buf, &result);
9906 else if (result == PACKET_UNKNOWN)
9907 throw_error (TLS_GENERIC_ERROR,
9908 _("Remote target doesn't support qGetTLSAddr packet"));
9910 throw_error (TLS_GENERIC_ERROR,
9911 _("Remote target failed to process qGetTLSAddr request"));
9914 throw_error (TLS_GENERIC_ERROR,
9915 _("TLS not supported or disabled on this target"));
9920 /* Provide thread local base, i.e. Thread Information Block address.
9921 Returns 1 if ptid is found and thread_local_base is non zero. */
9924 remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
9926 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
9928 struct remote_state *rs = get_remote_state ();
9930 char *endp = rs->buf + get_remote_packet_size ();
9931 enum packet_result result;
9933 strcpy (p, "qGetTIBAddr:");
9935 p = write_ptid (p, endp, ptid);
9939 getpkt (&rs->buf, &rs->buf_size, 0);
9940 result = packet_ok (rs->buf,
9941 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9942 if (result == PACKET_OK)
9946 unpack_varlen_hex (rs->buf, &result);
9948 *addr = (CORE_ADDR) result;
9951 else if (result == PACKET_UNKNOWN)
9952 error (_("Remote target doesn't support qGetTIBAddr packet"));
9954 error (_("Remote target failed to process qGetTIBAddr request"));
9957 error (_("qGetTIBAddr not supported or disabled on this target"));
9962 /* Support for inferring a target description based on the current
9963 architecture and the size of a 'g' packet. While the 'g' packet
9964 can have any size (since optional registers can be left off the
9965 end), some sizes are easily recognizable given knowledge of the
9966 approximate architecture. */
9968 struct remote_g_packet_guess
9971 const struct target_desc *tdesc;
9973 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9974 DEF_VEC_O(remote_g_packet_guess_s);
9976 struct remote_g_packet_data
9978 VEC(remote_g_packet_guess_s) *guesses;
9981 static struct gdbarch_data *remote_g_packet_data_handle;
9984 remote_g_packet_data_init (struct obstack *obstack)
9986 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9990 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9991 const struct target_desc *tdesc)
9993 struct remote_g_packet_data *data
9994 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9995 struct remote_g_packet_guess new_guess, *guess;
9998 gdb_assert (tdesc != NULL);
10001 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10003 if (guess->bytes == bytes)
10004 internal_error (__FILE__, __LINE__,
10005 _("Duplicate g packet description added for size %d"),
10008 new_guess.bytes = bytes;
10009 new_guess.tdesc = tdesc;
10010 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10013 /* Return 1 if remote_read_description would do anything on this target
10014 and architecture, 0 otherwise. */
10017 remote_read_description_p (struct target_ops *target)
10019 struct remote_g_packet_data *data
10020 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
10022 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10028 static const struct target_desc *
10029 remote_read_description (struct target_ops *target)
10031 struct remote_g_packet_data *data
10032 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
10034 /* Do not try this during initial connection, when we do not know
10035 whether there is a running but stopped thread. */
10036 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
10037 return target->beneath->to_read_description (target->beneath);
10039 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10041 struct remote_g_packet_guess *guess;
10043 int bytes = send_g_packet ();
10046 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10048 if (guess->bytes == bytes)
10049 return guess->tdesc;
10051 /* We discard the g packet. A minor optimization would be to
10052 hold on to it, and fill the register cache once we have selected
10053 an architecture, but it's too tricky to do safely. */
10056 return target->beneath->to_read_description (target->beneath);
10059 /* Remote file transfer support. This is host-initiated I/O, not
10060 target-initiated; for target-initiated, see remote-fileio.c. */
10062 /* If *LEFT is at least the length of STRING, copy STRING to
10063 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10064 decrease *LEFT. Otherwise raise an error. */
10067 remote_buffer_add_string (char **buffer, int *left, char *string)
10069 int len = strlen (string);
10072 error (_("Packet too long for target."));
10074 memcpy (*buffer, string, len);
10078 /* NUL-terminate the buffer as a convenience, if there is
10084 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10085 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10086 decrease *LEFT. Otherwise raise an error. */
10089 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10092 if (2 * len > *left)
10093 error (_("Packet too long for target."));
10095 bin2hex (bytes, *buffer, len);
10096 *buffer += 2 * len;
10099 /* NUL-terminate the buffer as a convenience, if there is
10105 /* If *LEFT is large enough, convert VALUE to hex and add it to
10106 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10107 decrease *LEFT. Otherwise raise an error. */
10110 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10112 int len = hexnumlen (value);
10115 error (_("Packet too long for target."));
10117 hexnumstr (*buffer, value);
10121 /* NUL-terminate the buffer as a convenience, if there is
10127 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10128 value, *REMOTE_ERRNO to the remote error number or zero if none
10129 was included, and *ATTACHMENT to point to the start of the annex
10130 if any. The length of the packet isn't needed here; there may
10131 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10133 Return 0 if the packet could be parsed, -1 if it could not. If
10134 -1 is returned, the other variables may not be initialized. */
10137 remote_hostio_parse_result (char *buffer, int *retcode,
10138 int *remote_errno, char **attachment)
10143 *attachment = NULL;
10145 if (buffer[0] != 'F')
10149 *retcode = strtol (&buffer[1], &p, 16);
10150 if (errno != 0 || p == &buffer[1])
10153 /* Check for ",errno". */
10157 *remote_errno = strtol (p + 1, &p2, 16);
10158 if (errno != 0 || p + 1 == p2)
10163 /* Check for ";attachment". If there is no attachment, the
10164 packet should end here. */
10167 *attachment = p + 1;
10170 else if (*p == '\0')
10176 /* Send a prepared I/O packet to the target and read its response.
10177 The prepared packet is in the global RS->BUF before this function
10178 is called, and the answer is there when we return.
10180 COMMAND_BYTES is the length of the request to send, which may include
10181 binary data. WHICH_PACKET is the packet configuration to check
10182 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10183 is set to the error number and -1 is returned. Otherwise the value
10184 returned by the function is returned.
10186 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10187 attachment is expected; an error will be reported if there's a
10188 mismatch. If one is found, *ATTACHMENT will be set to point into
10189 the packet buffer and *ATTACHMENT_LEN will be set to the
10190 attachment's length. */
10193 remote_hostio_send_command (int command_bytes, int which_packet,
10194 int *remote_errno, char **attachment,
10195 int *attachment_len)
10197 struct remote_state *rs = get_remote_state ();
10198 int ret, bytes_read;
10199 char *attachment_tmp;
10201 if (!rs->remote_desc
10202 || packet_support (which_packet) == PACKET_DISABLE)
10204 *remote_errno = FILEIO_ENOSYS;
10208 putpkt_binary (rs->buf, command_bytes);
10209 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10211 /* If it timed out, something is wrong. Don't try to parse the
10213 if (bytes_read < 0)
10215 *remote_errno = FILEIO_EINVAL;
10219 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10222 *remote_errno = FILEIO_EINVAL;
10224 case PACKET_UNKNOWN:
10225 *remote_errno = FILEIO_ENOSYS;
10231 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10234 *remote_errno = FILEIO_EINVAL;
10238 /* Make sure we saw an attachment if and only if we expected one. */
10239 if ((attachment_tmp == NULL && attachment != NULL)
10240 || (attachment_tmp != NULL && attachment == NULL))
10242 *remote_errno = FILEIO_EINVAL;
10246 /* If an attachment was found, it must point into the packet buffer;
10247 work out how many bytes there were. */
10248 if (attachment_tmp != NULL)
10250 *attachment = attachment_tmp;
10251 *attachment_len = bytes_read - (*attachment - rs->buf);
10257 /* Set the filesystem remote_hostio functions that take FILENAME
10258 arguments will use. Return 0 on success, or -1 if an error
10259 occurs (and set *REMOTE_ERRNO). */
10262 remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10264 struct remote_state *rs = get_remote_state ();
10265 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10267 int left = get_remote_packet_size () - 1;
10271 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10274 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10277 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10279 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10280 remote_buffer_add_string (&p, &left, arg);
10282 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10283 remote_errno, NULL, NULL);
10285 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10289 rs->fs_pid = required_pid;
10294 /* Implementation of to_fileio_open. */
10297 remote_hostio_open (struct target_ops *self,
10298 struct inferior *inf, const char *filename,
10299 int flags, int mode, int *remote_errno)
10301 struct remote_state *rs = get_remote_state ();
10303 int left = get_remote_packet_size () - 1;
10305 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10308 remote_buffer_add_string (&p, &left, "vFile:open:");
10310 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10311 strlen (filename));
10312 remote_buffer_add_string (&p, &left, ",");
10314 remote_buffer_add_int (&p, &left, flags);
10315 remote_buffer_add_string (&p, &left, ",");
10317 remote_buffer_add_int (&p, &left, mode);
10319 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10320 remote_errno, NULL, NULL);
10323 /* Implementation of to_fileio_pwrite. */
10326 remote_hostio_pwrite (struct target_ops *self,
10327 int fd, const gdb_byte *write_buf, int len,
10328 ULONGEST offset, int *remote_errno)
10330 struct remote_state *rs = get_remote_state ();
10332 int left = get_remote_packet_size ();
10335 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10337 remote_buffer_add_int (&p, &left, fd);
10338 remote_buffer_add_string (&p, &left, ",");
10340 remote_buffer_add_int (&p, &left, offset);
10341 remote_buffer_add_string (&p, &left, ",");
10343 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
10344 get_remote_packet_size () - (p - rs->buf));
10346 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10347 remote_errno, NULL, NULL);
10350 /* Implementation of to_fileio_pread. */
10353 remote_hostio_pread (struct target_ops *self,
10354 int fd, gdb_byte *read_buf, int len,
10355 ULONGEST offset, int *remote_errno)
10357 struct remote_state *rs = get_remote_state ();
10360 int left = get_remote_packet_size ();
10361 int ret, attachment_len;
10364 remote_buffer_add_string (&p, &left, "vFile:pread:");
10366 remote_buffer_add_int (&p, &left, fd);
10367 remote_buffer_add_string (&p, &left, ",");
10369 remote_buffer_add_int (&p, &left, len);
10370 remote_buffer_add_string (&p, &left, ",");
10372 remote_buffer_add_int (&p, &left, offset);
10374 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10375 remote_errno, &attachment,
10381 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10383 if (read_len != ret)
10384 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10389 /* Implementation of to_fileio_close. */
10392 remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
10394 struct remote_state *rs = get_remote_state ();
10396 int left = get_remote_packet_size () - 1;
10398 remote_buffer_add_string (&p, &left, "vFile:close:");
10400 remote_buffer_add_int (&p, &left, fd);
10402 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10403 remote_errno, NULL, NULL);
10406 /* Implementation of to_fileio_unlink. */
10409 remote_hostio_unlink (struct target_ops *self,
10410 struct inferior *inf, const char *filename,
10413 struct remote_state *rs = get_remote_state ();
10415 int left = get_remote_packet_size () - 1;
10417 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10420 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10422 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10423 strlen (filename));
10425 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10426 remote_errno, NULL, NULL);
10429 /* Implementation of to_fileio_readlink. */
10432 remote_hostio_readlink (struct target_ops *self,
10433 struct inferior *inf, const char *filename,
10436 struct remote_state *rs = get_remote_state ();
10439 int left = get_remote_packet_size ();
10440 int len, attachment_len;
10444 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10447 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10449 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10450 strlen (filename));
10452 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10453 remote_errno, &attachment,
10459 ret = xmalloc (len + 1);
10461 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10462 (gdb_byte *) ret, len);
10463 if (read_len != len)
10464 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10470 /* Implementation of to_fileio_fstat. */
10473 remote_hostio_fstat (struct target_ops *self,
10474 int fd, struct stat *st,
10477 struct remote_state *rs = get_remote_state ();
10479 int left = get_remote_packet_size ();
10480 int attachment_len, ret;
10482 struct fio_stat fst;
10485 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10487 remote_buffer_add_int (&p, &left, fd);
10489 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10490 remote_errno, &attachment,
10494 if (*remote_errno != FILEIO_ENOSYS)
10497 /* Strictly we should return -1, ENOSYS here, but when
10498 "set sysroot remote:" was implemented in August 2008
10499 BFD's need for a stat function was sidestepped with
10500 this hack. This was not remedied until March 2015
10501 so we retain the previous behavior to avoid breaking
10504 Note that the memset is a March 2015 addition; older
10505 GDBs set st_size *and nothing else* so the structure
10506 would have garbage in all other fields. This might
10507 break something but retaining the previous behavior
10508 here would be just too wrong. */
10510 memset (st, 0, sizeof (struct stat));
10511 st->st_size = INT_MAX;
10515 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10516 (gdb_byte *) &fst, sizeof (fst));
10518 if (read_len != ret)
10519 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10521 if (read_len != sizeof (fst))
10522 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10523 read_len, (int) sizeof (fst));
10525 remote_fileio_to_host_stat (&fst, st);
10530 /* Implementation of to_filesystem_is_local. */
10533 remote_filesystem_is_local (struct target_ops *self)
10535 /* Valgrind GDB presents itself as a remote target but works
10536 on the local filesystem: it does not implement remote get
10537 and users are not expected to set a sysroot. To handle
10538 this case we treat the remote filesystem as local if the
10539 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10540 does not support vFile:open. */
10541 if (gdb_sysroot != NULL
10542 && strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
10544 enum packet_support ps = packet_support (PACKET_vFile_open);
10546 if (ps == PACKET_SUPPORT_UNKNOWN)
10548 int fd, remote_errno;
10550 /* Try opening a file to probe support. The supplied
10551 filename is irrelevant, we only care about whether
10552 the stub recognizes the packet or not. */
10553 fd = remote_hostio_open (self, NULL, "just probing",
10554 FILEIO_O_RDONLY, 0700,
10558 remote_hostio_close (self, fd, &remote_errno);
10560 ps = packet_support (PACKET_vFile_open);
10563 if (ps == PACKET_DISABLE)
10565 static int warning_issued = 0;
10567 if (!warning_issued)
10569 warning (_("remote target does not support file"
10570 " transfer, attempting to access files"
10571 " from local filesystem."));
10572 warning_issued = 1;
10583 remote_fileio_errno_to_host (int errnum)
10589 case FILEIO_ENOENT:
10597 case FILEIO_EACCES:
10599 case FILEIO_EFAULT:
10603 case FILEIO_EEXIST:
10605 case FILEIO_ENODEV:
10607 case FILEIO_ENOTDIR:
10609 case FILEIO_EISDIR:
10611 case FILEIO_EINVAL:
10613 case FILEIO_ENFILE:
10615 case FILEIO_EMFILE:
10619 case FILEIO_ENOSPC:
10621 case FILEIO_ESPIPE:
10625 case FILEIO_ENOSYS:
10627 case FILEIO_ENAMETOOLONG:
10628 return ENAMETOOLONG;
10634 remote_hostio_error (int errnum)
10636 int host_error = remote_fileio_errno_to_host (errnum);
10638 if (host_error == -1)
10639 error (_("Unknown remote I/O error %d"), errnum);
10641 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10645 remote_hostio_close_cleanup (void *opaque)
10647 int fd = *(int *) opaque;
10650 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
10654 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10656 struct cleanup *back_to, *close_cleanup;
10657 int retcode, fd, remote_errno, bytes, io_size;
10660 int bytes_in_buffer;
10663 struct remote_state *rs = get_remote_state ();
10665 if (!rs->remote_desc)
10666 error (_("command can only be used with remote target"));
10668 file = gdb_fopen_cloexec (local_file, "rb");
10670 perror_with_name (local_file);
10671 back_to = make_cleanup_fclose (file);
10673 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
10674 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
10676 0700, &remote_errno);
10678 remote_hostio_error (remote_errno);
10680 /* Send up to this many bytes at once. They won't all fit in the
10681 remote packet limit, so we'll transfer slightly fewer. */
10682 io_size = get_remote_packet_size ();
10683 buffer = xmalloc (io_size);
10684 make_cleanup (xfree, buffer);
10686 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10688 bytes_in_buffer = 0;
10691 while (bytes_in_buffer || !saw_eof)
10695 bytes = fread (buffer + bytes_in_buffer, 1,
10696 io_size - bytes_in_buffer,
10701 error (_("Error reading %s."), local_file);
10704 /* EOF. Unless there is something still in the
10705 buffer from the last iteration, we are done. */
10707 if (bytes_in_buffer == 0)
10715 bytes += bytes_in_buffer;
10716 bytes_in_buffer = 0;
10718 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10720 offset, &remote_errno);
10723 remote_hostio_error (remote_errno);
10724 else if (retcode == 0)
10725 error (_("Remote write of %d bytes returned 0!"), bytes);
10726 else if (retcode < bytes)
10728 /* Short write. Save the rest of the read data for the next
10730 bytes_in_buffer = bytes - retcode;
10731 memmove (buffer, buffer + retcode, bytes_in_buffer);
10737 discard_cleanups (close_cleanup);
10738 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10739 remote_hostio_error (remote_errno);
10742 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10743 do_cleanups (back_to);
10747 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10749 struct cleanup *back_to, *close_cleanup;
10750 int fd, remote_errno, bytes, io_size;
10754 struct remote_state *rs = get_remote_state ();
10756 if (!rs->remote_desc)
10757 error (_("command can only be used with remote target"));
10759 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
10760 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
10762 remote_hostio_error (remote_errno);
10764 file = gdb_fopen_cloexec (local_file, "wb");
10766 perror_with_name (local_file);
10767 back_to = make_cleanup_fclose (file);
10769 /* Send up to this many bytes at once. They won't all fit in the
10770 remote packet limit, so we'll transfer slightly fewer. */
10771 io_size = get_remote_packet_size ();
10772 buffer = xmalloc (io_size);
10773 make_cleanup (xfree, buffer);
10775 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10780 bytes = remote_hostio_pread (find_target_at (process_stratum),
10781 fd, buffer, io_size, offset, &remote_errno);
10783 /* Success, but no bytes, means end-of-file. */
10786 remote_hostio_error (remote_errno);
10790 bytes = fwrite (buffer, 1, bytes, file);
10792 perror_with_name (local_file);
10795 discard_cleanups (close_cleanup);
10796 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
10797 remote_hostio_error (remote_errno);
10800 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10801 do_cleanups (back_to);
10805 remote_file_delete (const char *remote_file, int from_tty)
10807 int retcode, remote_errno;
10808 struct remote_state *rs = get_remote_state ();
10810 if (!rs->remote_desc)
10811 error (_("command can only be used with remote target"));
10813 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10814 NULL, remote_file, &remote_errno);
10816 remote_hostio_error (remote_errno);
10819 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10823 remote_put_command (char *args, int from_tty)
10825 struct cleanup *back_to;
10829 error_no_arg (_("file to put"));
10831 argv = gdb_buildargv (args);
10832 back_to = make_cleanup_freeargv (argv);
10833 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10834 error (_("Invalid parameters to remote put"));
10836 remote_file_put (argv[0], argv[1], from_tty);
10838 do_cleanups (back_to);
10842 remote_get_command (char *args, int from_tty)
10844 struct cleanup *back_to;
10848 error_no_arg (_("file to get"));
10850 argv = gdb_buildargv (args);
10851 back_to = make_cleanup_freeargv (argv);
10852 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10853 error (_("Invalid parameters to remote get"));
10855 remote_file_get (argv[0], argv[1], from_tty);
10857 do_cleanups (back_to);
10861 remote_delete_command (char *args, int from_tty)
10863 struct cleanup *back_to;
10867 error_no_arg (_("file to delete"));
10869 argv = gdb_buildargv (args);
10870 back_to = make_cleanup_freeargv (argv);
10871 if (argv[0] == NULL || argv[1] != NULL)
10872 error (_("Invalid parameters to remote delete"));
10874 remote_file_delete (argv[0], from_tty);
10876 do_cleanups (back_to);
10880 remote_command (char *args, int from_tty)
10882 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
10886 remote_can_execute_reverse (struct target_ops *self)
10888 if (packet_support (PACKET_bs) == PACKET_ENABLE
10889 || packet_support (PACKET_bc) == PACKET_ENABLE)
10896 remote_supports_non_stop (struct target_ops *self)
10902 remote_supports_disable_randomization (struct target_ops *self)
10904 /* Only supported in extended mode. */
10909 remote_supports_multi_process (struct target_ops *self)
10911 struct remote_state *rs = get_remote_state ();
10913 /* Only extended-remote handles being attached to multiple
10914 processes, even though plain remote can use the multi-process
10915 thread id extensions, so that GDB knows the target process's
10917 return rs->extended && remote_multi_process_p (rs);
10921 remote_supports_cond_tracepoints (void)
10923 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
10927 remote_supports_cond_breakpoints (struct target_ops *self)
10929 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
10933 remote_supports_fast_tracepoints (void)
10935 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
10939 remote_supports_static_tracepoints (void)
10941 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
10945 remote_supports_install_in_trace (void)
10947 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
10951 remote_supports_enable_disable_tracepoint (struct target_ops *self)
10953 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10958 remote_supports_string_tracing (struct target_ops *self)
10960 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
10964 remote_can_run_breakpoint_commands (struct target_ops *self)
10966 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
10970 remote_trace_init (struct target_ops *self)
10973 remote_get_noisy_reply (&target_buf, &target_buf_size);
10974 if (strcmp (target_buf, "OK") != 0)
10975 error (_("Target does not support this command."));
10978 static void free_actions_list (char **actions_list);
10979 static void free_actions_list_cleanup_wrapper (void *);
10981 free_actions_list_cleanup_wrapper (void *al)
10983 free_actions_list (al);
10987 free_actions_list (char **actions_list)
10991 if (actions_list == 0)
10994 for (ndx = 0; actions_list[ndx]; ndx++)
10995 xfree (actions_list[ndx]);
10997 xfree (actions_list);
11000 /* Recursive routine to walk through command list including loops, and
11001 download packets for each command. */
11004 remote_download_command_source (int num, ULONGEST addr,
11005 struct command_line *cmds)
11007 struct remote_state *rs = get_remote_state ();
11008 struct command_line *cmd;
11010 for (cmd = cmds; cmd; cmd = cmd->next)
11012 QUIT; /* Allow user to bail out with ^C. */
11013 strcpy (rs->buf, "QTDPsrc:");
11014 encode_source_string (num, addr, "cmd", cmd->line,
11015 rs->buf + strlen (rs->buf),
11016 rs->buf_size - strlen (rs->buf));
11018 remote_get_noisy_reply (&target_buf, &target_buf_size);
11019 if (strcmp (target_buf, "OK"))
11020 warning (_("Target does not support source download."));
11022 if (cmd->control_type == while_control
11023 || cmd->control_type == while_stepping_control)
11025 remote_download_command_source (num, addr, *cmd->body_list);
11027 QUIT; /* Allow user to bail out with ^C. */
11028 strcpy (rs->buf, "QTDPsrc:");
11029 encode_source_string (num, addr, "cmd", "end",
11030 rs->buf + strlen (rs->buf),
11031 rs->buf_size - strlen (rs->buf));
11033 remote_get_noisy_reply (&target_buf, &target_buf_size);
11034 if (strcmp (target_buf, "OK"))
11035 warning (_("Target does not support source download."));
11041 remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
11043 #define BUF_SIZE 2048
11047 char buf[BUF_SIZE];
11048 char **tdp_actions;
11049 char **stepping_actions;
11051 struct cleanup *old_chain = NULL;
11052 struct agent_expr *aexpr;
11053 struct cleanup *aexpr_chain = NULL;
11055 struct breakpoint *b = loc->owner;
11056 struct tracepoint *t = (struct tracepoint *) b;
11058 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
11059 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11061 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11064 tpaddr = loc->address;
11065 sprintf_vma (addrbuf, tpaddr);
11066 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11067 addrbuf, /* address */
11068 (b->enable_state == bp_enabled ? 'E' : 'D'),
11069 t->step_count, t->pass_count);
11070 /* Fast tracepoints are mostly handled by the target, but we can
11071 tell the target how big of an instruction block should be moved
11073 if (b->type == bp_fast_tracepoint)
11075 /* Only test for support at download time; we may not know
11076 target capabilities at definition time. */
11077 if (remote_supports_fast_tracepoints ())
11081 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
11082 tpaddr, &isize, NULL))
11083 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
11086 /* If it passed validation at definition but fails now,
11087 something is very wrong. */
11088 internal_error (__FILE__, __LINE__,
11089 _("Fast tracepoint not "
11090 "valid during download"));
11093 /* Fast tracepoints are functionally identical to regular
11094 tracepoints, so don't take lack of support as a reason to
11095 give up on the trace run. */
11096 warning (_("Target does not support fast tracepoints, "
11097 "downloading %d as regular tracepoint"), b->number);
11099 else if (b->type == bp_static_tracepoint)
11101 /* Only test for support at download time; we may not know
11102 target capabilities at definition time. */
11103 if (remote_supports_static_tracepoints ())
11105 struct static_tracepoint_marker marker;
11107 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11108 strcat (buf, ":S");
11110 error (_("Static tracepoint not valid during download"));
11113 /* Fast tracepoints are functionally identical to regular
11114 tracepoints, so don't take lack of support as a reason
11115 to give up on the trace run. */
11116 error (_("Target does not support static tracepoints"));
11118 /* If the tracepoint has a conditional, make it into an agent
11119 expression and append to the definition. */
11122 /* Only test support at download time, we may not know target
11123 capabilities at definition time. */
11124 if (remote_supports_cond_tracepoints ())
11126 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11127 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
11128 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11130 pkt = buf + strlen (buf);
11131 for (ndx = 0; ndx < aexpr->len; ++ndx)
11132 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11134 do_cleanups (aexpr_chain);
11137 warning (_("Target does not support conditional tracepoints, "
11138 "ignoring tp %d cond"), b->number);
11141 if (b->commands || *default_collect)
11144 remote_get_noisy_reply (&target_buf, &target_buf_size);
11145 if (strcmp (target_buf, "OK"))
11146 error (_("Target does not support tracepoints."));
11148 /* do_single_steps (t); */
11151 for (ndx = 0; tdp_actions[ndx]; ndx++)
11153 QUIT; /* Allow user to bail out with ^C. */
11154 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11155 b->number, addrbuf, /* address */
11157 ((tdp_actions[ndx + 1] || stepping_actions)
11160 remote_get_noisy_reply (&target_buf,
11162 if (strcmp (target_buf, "OK"))
11163 error (_("Error on target while setting tracepoints."));
11166 if (stepping_actions)
11168 for (ndx = 0; stepping_actions[ndx]; ndx++)
11170 QUIT; /* Allow user to bail out with ^C. */
11171 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11172 b->number, addrbuf, /* address */
11173 ((ndx == 0) ? "S" : ""),
11174 stepping_actions[ndx],
11175 (stepping_actions[ndx + 1] ? "-" : ""));
11177 remote_get_noisy_reply (&target_buf,
11179 if (strcmp (target_buf, "OK"))
11180 error (_("Error on target while setting tracepoints."));
11184 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
11186 if (b->addr_string)
11188 strcpy (buf, "QTDPsrc:");
11189 encode_source_string (b->number, loc->address,
11190 "at", b->addr_string, buf + strlen (buf),
11191 2048 - strlen (buf));
11194 remote_get_noisy_reply (&target_buf, &target_buf_size);
11195 if (strcmp (target_buf, "OK"))
11196 warning (_("Target does not support source download."));
11198 if (b->cond_string)
11200 strcpy (buf, "QTDPsrc:");
11201 encode_source_string (b->number, loc->address,
11202 "cond", b->cond_string, buf + strlen (buf),
11203 2048 - strlen (buf));
11205 remote_get_noisy_reply (&target_buf, &target_buf_size);
11206 if (strcmp (target_buf, "OK"))
11207 warning (_("Target does not support source download."));
11209 remote_download_command_source (b->number, loc->address,
11210 breakpoint_commands (b));
11213 do_cleanups (old_chain);
11217 remote_can_download_tracepoint (struct target_ops *self)
11219 struct remote_state *rs = get_remote_state ();
11220 struct trace_status *ts;
11223 /* Don't try to install tracepoints until we've relocated our
11224 symbols, and fetched and merged the target's tracepoint list with
11226 if (rs->starting_up)
11229 ts = current_trace_status ();
11230 status = remote_get_trace_status (self, ts);
11232 if (status == -1 || !ts->running_known || !ts->running)
11235 /* If we are in a tracing experiment, but remote stub doesn't support
11236 installing tracepoint in trace, we have to return. */
11237 if (!remote_supports_install_in_trace ())
11245 remote_download_trace_state_variable (struct target_ops *self,
11246 struct trace_state_variable *tsv)
11248 struct remote_state *rs = get_remote_state ();
11251 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11252 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11254 p = rs->buf + strlen (rs->buf);
11255 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11256 error (_("Trace state variable name too long for tsv definition packet"));
11257 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
11260 remote_get_noisy_reply (&target_buf, &target_buf_size);
11261 if (*target_buf == '\0')
11262 error (_("Target does not support this command."));
11263 if (strcmp (target_buf, "OK") != 0)
11264 error (_("Error on target while downloading trace state variable."));
11268 remote_enable_tracepoint (struct target_ops *self,
11269 struct bp_location *location)
11271 struct remote_state *rs = get_remote_state ();
11274 sprintf_vma (addr_buf, location->address);
11275 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11276 location->owner->number, addr_buf);
11278 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11279 if (*rs->buf == '\0')
11280 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11281 if (strcmp (rs->buf, "OK") != 0)
11282 error (_("Error on target while enabling tracepoint."));
11286 remote_disable_tracepoint (struct target_ops *self,
11287 struct bp_location *location)
11289 struct remote_state *rs = get_remote_state ();
11292 sprintf_vma (addr_buf, location->address);
11293 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11294 location->owner->number, addr_buf);
11296 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11297 if (*rs->buf == '\0')
11298 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11299 if (strcmp (rs->buf, "OK") != 0)
11300 error (_("Error on target while disabling tracepoint."));
11304 remote_trace_set_readonly_regions (struct target_ops *self)
11308 bfd_size_type size;
11314 return; /* No information to give. */
11316 strcpy (target_buf, "QTro");
11317 offset = strlen (target_buf);
11318 for (s = exec_bfd->sections; s; s = s->next)
11320 char tmp1[40], tmp2[40];
11323 if ((s->flags & SEC_LOAD) == 0 ||
11324 /* (s->flags & SEC_CODE) == 0 || */
11325 (s->flags & SEC_READONLY) == 0)
11329 vma = bfd_get_section_vma (abfd, s);
11330 size = bfd_get_section_size (s);
11331 sprintf_vma (tmp1, vma);
11332 sprintf_vma (tmp2, vma + size);
11333 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11334 if (offset + sec_length + 1 > target_buf_size)
11336 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
11338 Too many sections for read-only sections definition packet."));
11341 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11343 offset += sec_length;
11347 putpkt (target_buf);
11348 getpkt (&target_buf, &target_buf_size, 0);
11353 remote_trace_start (struct target_ops *self)
11355 putpkt ("QTStart");
11356 remote_get_noisy_reply (&target_buf, &target_buf_size);
11357 if (*target_buf == '\0')
11358 error (_("Target does not support this command."));
11359 if (strcmp (target_buf, "OK") != 0)
11360 error (_("Bogus reply from target: %s"), target_buf);
11364 remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
11366 /* Initialize it just to avoid a GCC false warning. */
11368 /* FIXME we need to get register block size some other way. */
11369 extern int trace_regblock_size;
11370 enum packet_result result;
11372 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
11375 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11377 putpkt ("qTStatus");
11381 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11383 CATCH (ex, RETURN_MASK_ERROR)
11385 if (ex.error != TARGET_CLOSE_ERROR)
11387 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11390 throw_exception (ex);
11394 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11396 /* If the remote target doesn't do tracing, flag it. */
11397 if (result == PACKET_UNKNOWN)
11400 /* We're working with a live target. */
11401 ts->filename = NULL;
11404 error (_("Bogus trace status reply from target: %s"), target_buf);
11406 /* Function 'parse_trace_status' sets default value of each field of
11407 'ts' at first, so we don't have to do it here. */
11408 parse_trace_status (p, ts);
11410 return ts->running;
11414 remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
11415 struct uploaded_tp *utp)
11417 struct remote_state *rs = get_remote_state ();
11419 struct bp_location *loc;
11420 struct tracepoint *tp = (struct tracepoint *) bp;
11421 size_t size = get_remote_packet_size ();
11425 tp->base.hit_count = 0;
11426 tp->traceframe_usage = 0;
11427 for (loc = tp->base.loc; loc; loc = loc->next)
11429 /* If the tracepoint was never downloaded, don't go asking for
11431 if (tp->number_on_target == 0)
11433 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11434 phex_nz (loc->address, 0));
11436 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11437 if (reply && *reply)
11440 parse_tracepoint_status (reply + 1, bp, utp);
11446 utp->hit_count = 0;
11447 utp->traceframe_usage = 0;
11448 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11449 phex_nz (utp->addr, 0));
11451 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11452 if (reply && *reply)
11455 parse_tracepoint_status (reply + 1, bp, utp);
11461 remote_trace_stop (struct target_ops *self)
11464 remote_get_noisy_reply (&target_buf, &target_buf_size);
11465 if (*target_buf == '\0')
11466 error (_("Target does not support this command."));
11467 if (strcmp (target_buf, "OK") != 0)
11468 error (_("Bogus reply from target: %s"), target_buf);
11472 remote_trace_find (struct target_ops *self,
11473 enum trace_find_type type, int num,
11474 CORE_ADDR addr1, CORE_ADDR addr2,
11477 struct remote_state *rs = get_remote_state ();
11478 char *endbuf = rs->buf + get_remote_packet_size ();
11480 int target_frameno = -1, target_tracept = -1;
11482 /* Lookups other than by absolute frame number depend on the current
11483 trace selected, so make sure it is correct on the remote end
11485 if (type != tfind_number)
11486 set_remote_traceframe ();
11489 strcpy (p, "QTFrame:");
11490 p = strchr (p, '\0');
11494 xsnprintf (p, endbuf - p, "%x", num);
11497 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
11500 xsnprintf (p, endbuf - p, "tdp:%x", num);
11503 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11504 phex_nz (addr2, 0));
11506 case tfind_outside:
11507 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11508 phex_nz (addr2, 0));
11511 error (_("Unknown trace find type %d"), type);
11515 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
11516 if (*reply == '\0')
11517 error (_("Target does not support this command."));
11519 while (reply && *reply)
11524 target_frameno = (int) strtol (p, &reply, 16);
11526 error (_("Unable to parse trace frame number"));
11527 /* Don't update our remote traceframe number cache on failure
11528 to select a remote traceframe. */
11529 if (target_frameno == -1)
11534 target_tracept = (int) strtol (p, &reply, 16);
11536 error (_("Unable to parse tracepoint number"));
11538 case 'O': /* "OK"? */
11539 if (reply[1] == 'K' && reply[2] == '\0')
11542 error (_("Bogus reply from target: %s"), reply);
11545 error (_("Bogus reply from target: %s"), reply);
11548 *tpp = target_tracept;
11550 rs->remote_traceframe_number = target_frameno;
11551 return target_frameno;
11555 remote_get_trace_state_variable_value (struct target_ops *self,
11556 int tsvnum, LONGEST *val)
11558 struct remote_state *rs = get_remote_state ();
11562 set_remote_traceframe ();
11564 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
11566 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11567 if (reply && *reply)
11571 unpack_varlen_hex (reply + 1, &uval);
11572 *val = (LONGEST) uval;
11580 remote_save_trace_data (struct target_ops *self, const char *filename)
11582 struct remote_state *rs = get_remote_state ();
11586 strcpy (p, "QTSave:");
11588 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11589 error (_("Remote file name too long for trace save packet"));
11590 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
11593 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11594 if (*reply == '\0')
11595 error (_("Target does not support this command."));
11596 if (strcmp (reply, "OK") != 0)
11597 error (_("Bogus reply from target: %s"), reply);
11601 /* This is basically a memory transfer, but needs to be its own packet
11602 because we don't know how the target actually organizes its trace
11603 memory, plus we want to be able to ask for as much as possible, but
11604 not be unhappy if we don't get as much as we ask for. */
11607 remote_get_raw_trace_data (struct target_ops *self,
11608 gdb_byte *buf, ULONGEST offset, LONGEST len)
11610 struct remote_state *rs = get_remote_state ();
11616 strcpy (p, "qTBuffer:");
11618 p += hexnumstr (p, offset);
11620 p += hexnumstr (p, len);
11624 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11625 if (reply && *reply)
11627 /* 'l' by itself means we're at the end of the buffer and
11628 there is nothing more to get. */
11632 /* Convert the reply into binary. Limit the number of bytes to
11633 convert according to our passed-in buffer size, rather than
11634 what was returned in the packet; if the target is
11635 unexpectedly generous and gives us a bigger reply than we
11636 asked for, we don't want to crash. */
11637 rslt = hex2bin (target_buf, buf, len);
11641 /* Something went wrong, flag as an error. */
11646 remote_set_disconnected_tracing (struct target_ops *self, int val)
11648 struct remote_state *rs = get_remote_state ();
11650 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
11654 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
11656 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11657 if (*reply == '\0')
11658 error (_("Target does not support this command."));
11659 if (strcmp (reply, "OK") != 0)
11660 error (_("Bogus reply from target: %s"), reply);
11663 warning (_("Target does not support disconnected tracing."));
11667 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11669 struct thread_info *info = find_thread_ptid (ptid);
11671 if (info && info->priv)
11672 return info->priv->core;
11677 remote_set_circular_trace_buffer (struct target_ops *self, int val)
11679 struct remote_state *rs = get_remote_state ();
11682 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
11684 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11685 if (*reply == '\0')
11686 error (_("Target does not support this command."));
11687 if (strcmp (reply, "OK") != 0)
11688 error (_("Bogus reply from target: %s"), reply);
11691 static struct traceframe_info *
11692 remote_traceframe_info (struct target_ops *self)
11696 text = target_read_stralloc (¤t_target,
11697 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11700 struct traceframe_info *info;
11701 struct cleanup *back_to = make_cleanup (xfree, text);
11703 info = parse_traceframe_info (text);
11704 do_cleanups (back_to);
11711 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11712 instruction on which a fast tracepoint may be placed. Returns -1
11713 if the packet is not supported, and 0 if the minimum instruction
11714 length is unknown. */
11717 remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
11719 struct remote_state *rs = get_remote_state ();
11722 /* If we're not debugging a process yet, the IPA can't be
11724 if (!target_has_execution)
11727 /* Make sure the remote is pointing at the right process. */
11728 set_general_process ();
11730 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
11732 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11733 if (*reply == '\0')
11737 ULONGEST min_insn_len;
11739 unpack_varlen_hex (reply, &min_insn_len);
11741 return (int) min_insn_len;
11746 remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
11748 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
11750 struct remote_state *rs = get_remote_state ();
11751 char *buf = rs->buf;
11752 char *endbuf = rs->buf + get_remote_packet_size ();
11753 enum packet_result result;
11755 gdb_assert (val >= 0 || val == -1);
11756 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11757 /* Send -1 as literal "-1" to avoid host size dependency. */
11761 buf += hexnumstr (buf, (ULONGEST) -val);
11764 buf += hexnumstr (buf, (ULONGEST) val);
11767 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11768 result = packet_ok (rs->buf,
11769 &remote_protocol_packets[PACKET_QTBuffer_size]);
11771 if (result != PACKET_OK)
11772 warning (_("Bogus reply from target: %s"), rs->buf);
11777 remote_set_trace_notes (struct target_ops *self,
11778 const char *user, const char *notes,
11779 const char *stop_notes)
11781 struct remote_state *rs = get_remote_state ();
11783 char *buf = rs->buf;
11784 char *endbuf = rs->buf + get_remote_packet_size ();
11787 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11790 buf += xsnprintf (buf, endbuf - buf, "user:");
11791 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
11797 buf += xsnprintf (buf, endbuf - buf, "notes:");
11798 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
11804 buf += xsnprintf (buf, endbuf - buf, "tstop:");
11805 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
11809 /* Ensure the buffer is terminated. */
11813 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11814 if (*reply == '\0')
11817 if (strcmp (reply, "OK") != 0)
11818 error (_("Bogus reply from target: %s"), reply);
11824 remote_use_agent (struct target_ops *self, int use)
11826 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
11828 struct remote_state *rs = get_remote_state ();
11830 /* If the stub supports QAgent. */
11831 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
11833 getpkt (&rs->buf, &rs->buf_size, 0);
11835 if (strcmp (rs->buf, "OK") == 0)
11846 remote_can_use_agent (struct target_ops *self)
11848 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
11851 struct btrace_target_info
11853 /* The ptid of the traced thread. */
11856 /* The obtained branch trace configuration. */
11857 struct btrace_config conf;
11860 /* Reset our idea of our target's btrace configuration. */
11863 remote_btrace_reset (void)
11865 struct remote_state *rs = get_remote_state ();
11867 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11870 /* Check whether the target supports branch tracing. */
11873 remote_supports_btrace (struct target_ops *self, enum btrace_format format)
11875 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
11877 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
11882 case BTRACE_FORMAT_NONE:
11885 case BTRACE_FORMAT_BTS:
11886 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
11889 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
11892 /* Synchronize the configuration with the target. */
11895 btrace_sync_conf (const struct btrace_config *conf)
11897 struct packet_config *packet;
11898 struct remote_state *rs;
11899 char *buf, *pos, *endbuf;
11901 rs = get_remote_state ();
11903 endbuf = buf + get_remote_packet_size ();
11905 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11906 if (packet_config_support (packet) == PACKET_ENABLE
11907 && conf->bts.size != rs->btrace_config.bts.size)
11910 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11914 getpkt (&buf, &rs->buf_size, 0);
11916 if (packet_ok (buf, packet) == PACKET_ERROR)
11918 if (buf[0] == 'E' && buf[1] == '.')
11919 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11921 error (_("Failed to configure the BTS buffer size."));
11924 rs->btrace_config.bts.size = conf->bts.size;
11928 /* Read the current thread's btrace configuration from the target and
11929 store it into CONF. */
11932 btrace_read_config (struct btrace_config *conf)
11936 xml = target_read_stralloc (¤t_target,
11937 TARGET_OBJECT_BTRACE_CONF, "");
11940 struct cleanup *cleanup;
11942 cleanup = make_cleanup (xfree, xml);
11943 parse_xml_btrace_conf (conf, xml);
11944 do_cleanups (cleanup);
11948 /* Enable branch tracing. */
11950 static struct btrace_target_info *
11951 remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11952 const struct btrace_config *conf)
11954 struct btrace_target_info *tinfo = NULL;
11955 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11956 struct remote_state *rs = get_remote_state ();
11957 char *buf = rs->buf;
11958 char *endbuf = rs->buf + get_remote_packet_size ();
11960 if (packet_config_support (packet) != PACKET_ENABLE)
11961 error (_("Target does not support branch tracing."));
11963 btrace_sync_conf (conf);
11965 set_general_thread (ptid);
11967 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11969 getpkt (&rs->buf, &rs->buf_size, 0);
11971 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11973 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11974 error (_("Could not enable branch tracing for %s: %s"),
11975 target_pid_to_str (ptid), rs->buf + 2);
11977 error (_("Could not enable branch tracing for %s."),
11978 target_pid_to_str (ptid));
11981 tinfo = xzalloc (sizeof (*tinfo));
11982 tinfo->ptid = ptid;
11984 /* If we fail to read the configuration, we lose some information, but the
11985 tracing itself is not impacted. */
11988 btrace_read_config (&tinfo->conf);
11990 CATCH (err, RETURN_MASK_ERROR)
11992 if (err.message != NULL)
11993 warning ("%s", err.message);
12000 /* Disable branch tracing. */
12003 remote_disable_btrace (struct target_ops *self,
12004 struct btrace_target_info *tinfo)
12006 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12007 struct remote_state *rs = get_remote_state ();
12008 char *buf = rs->buf;
12009 char *endbuf = rs->buf + get_remote_packet_size ();
12011 if (packet_config_support (packet) != PACKET_ENABLE)
12012 error (_("Target does not support branch tracing."));
12014 set_general_thread (tinfo->ptid);
12016 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12018 getpkt (&rs->buf, &rs->buf_size, 0);
12020 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12022 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12023 error (_("Could not disable branch tracing for %s: %s"),
12024 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12026 error (_("Could not disable branch tracing for %s."),
12027 target_pid_to_str (tinfo->ptid));
12033 /* Teardown branch tracing. */
12036 remote_teardown_btrace (struct target_ops *self,
12037 struct btrace_target_info *tinfo)
12039 /* We must not talk to the target during teardown. */
12043 /* Read the branch trace. */
12045 static enum btrace_error
12046 remote_read_btrace (struct target_ops *self,
12047 struct btrace_data *btrace,
12048 struct btrace_target_info *tinfo,
12049 enum btrace_read_type type)
12051 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12052 struct remote_state *rs = get_remote_state ();
12053 struct cleanup *cleanup;
12057 if (packet_config_support (packet) != PACKET_ENABLE)
12058 error (_("Target does not support branch tracing."));
12060 #if !defined(HAVE_LIBEXPAT)
12061 error (_("Cannot process branch tracing result. XML parsing not supported."));
12066 case BTRACE_READ_ALL:
12069 case BTRACE_READ_NEW:
12072 case BTRACE_READ_DELTA:
12076 internal_error (__FILE__, __LINE__,
12077 _("Bad branch tracing read type: %u."),
12078 (unsigned int) type);
12081 xml = target_read_stralloc (¤t_target,
12082 TARGET_OBJECT_BTRACE, annex);
12084 return BTRACE_ERR_UNKNOWN;
12086 cleanup = make_cleanup (xfree, xml);
12087 parse_xml_btrace (btrace, xml);
12088 do_cleanups (cleanup);
12090 return BTRACE_ERR_NONE;
12093 static const struct btrace_config *
12094 remote_btrace_conf (struct target_ops *self,
12095 const struct btrace_target_info *tinfo)
12097 return &tinfo->conf;
12101 remote_augmented_libraries_svr4_read (struct target_ops *self)
12103 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12107 /* Implementation of to_load. */
12110 remote_load (struct target_ops *self, const char *name, int from_tty)
12112 generic_load (name, from_tty);
12115 /* Accepts an integer PID; returns a string representing a file that
12116 can be opened on the remote side to get the symbols for the child
12117 process. Returns NULL if the operation is not supported. */
12120 remote_pid_to_exec_file (struct target_ops *self, int pid)
12122 static char *filename = NULL;
12123 struct inferior *inf;
12124 char *annex = NULL;
12126 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12129 if (filename != NULL)
12132 inf = find_inferior_pid (pid);
12134 internal_error (__FILE__, __LINE__,
12135 _("not currently attached to process %d"), pid);
12137 if (!inf->fake_pid_p)
12139 const int annex_size = 9;
12141 annex = alloca (annex_size);
12142 xsnprintf (annex, annex_size, "%x", pid);
12145 filename = target_read_stralloc (¤t_target,
12146 TARGET_OBJECT_EXEC_FILE, annex);
12152 init_remote_ops (void)
12154 remote_ops.to_shortname = "remote";
12155 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
12156 remote_ops.to_doc =
12157 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
12158 Specify the serial device it is connected to\n\
12159 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
12160 remote_ops.to_open = remote_open;
12161 remote_ops.to_close = remote_close;
12162 remote_ops.to_detach = remote_detach;
12163 remote_ops.to_disconnect = remote_disconnect;
12164 remote_ops.to_resume = remote_resume;
12165 remote_ops.to_wait = remote_wait;
12166 remote_ops.to_fetch_registers = remote_fetch_registers;
12167 remote_ops.to_store_registers = remote_store_registers;
12168 remote_ops.to_prepare_to_store = remote_prepare_to_store;
12169 remote_ops.to_files_info = remote_files_info;
12170 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12171 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
12172 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12173 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12174 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12175 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
12176 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12177 remote_ops.to_stopped_data_address = remote_stopped_data_address;
12178 remote_ops.to_watchpoint_addr_within_range =
12179 remote_watchpoint_addr_within_range;
12180 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12181 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12182 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
12183 remote_ops.to_region_ok_for_hw_watchpoint
12184 = remote_region_ok_for_hw_watchpoint;
12185 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12186 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
12187 remote_ops.to_kill = remote_kill;
12188 remote_ops.to_load = remote_load;
12189 remote_ops.to_mourn_inferior = remote_mourn;
12190 remote_ops.to_pass_signals = remote_pass_signals;
12191 remote_ops.to_program_signals = remote_program_signals;
12192 remote_ops.to_thread_alive = remote_thread_alive;
12193 remote_ops.to_update_thread_list = remote_update_thread_list;
12194 remote_ops.to_pid_to_str = remote_pid_to_str;
12195 remote_ops.to_extra_thread_info = remote_threads_extra_info;
12196 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
12197 remote_ops.to_stop = remote_stop;
12198 remote_ops.to_xfer_partial = remote_xfer_partial;
12199 remote_ops.to_rcmd = remote_rcmd;
12200 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
12201 remote_ops.to_log_command = serial_log_command;
12202 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
12203 remote_ops.to_stratum = process_stratum;
12204 remote_ops.to_has_all_memory = default_child_has_all_memory;
12205 remote_ops.to_has_memory = default_child_has_memory;
12206 remote_ops.to_has_stack = default_child_has_stack;
12207 remote_ops.to_has_registers = default_child_has_registers;
12208 remote_ops.to_has_execution = default_child_has_execution;
12209 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
12210 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
12211 remote_ops.to_magic = OPS_MAGIC;
12212 remote_ops.to_memory_map = remote_memory_map;
12213 remote_ops.to_flash_erase = remote_flash_erase;
12214 remote_ops.to_flash_done = remote_flash_done;
12215 remote_ops.to_read_description = remote_read_description;
12216 remote_ops.to_search_memory = remote_search_memory;
12217 remote_ops.to_can_async_p = remote_can_async_p;
12218 remote_ops.to_is_async_p = remote_is_async_p;
12219 remote_ops.to_async = remote_async;
12220 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12221 remote_ops.to_terminal_ours = remote_terminal_ours;
12222 remote_ops.to_supports_non_stop = remote_supports_non_stop;
12223 remote_ops.to_supports_multi_process = remote_supports_multi_process;
12224 remote_ops.to_supports_disable_randomization
12225 = remote_supports_disable_randomization;
12226 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
12227 remote_ops.to_fileio_open = remote_hostio_open;
12228 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12229 remote_ops.to_fileio_pread = remote_hostio_pread;
12230 remote_ops.to_fileio_fstat = remote_hostio_fstat;
12231 remote_ops.to_fileio_close = remote_hostio_close;
12232 remote_ops.to_fileio_unlink = remote_hostio_unlink;
12233 remote_ops.to_fileio_readlink = remote_hostio_readlink;
12234 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
12235 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
12236 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
12237 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
12238 remote_ops.to_trace_init = remote_trace_init;
12239 remote_ops.to_download_tracepoint = remote_download_tracepoint;
12240 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
12241 remote_ops.to_download_trace_state_variable
12242 = remote_download_trace_state_variable;
12243 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12244 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
12245 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12246 remote_ops.to_trace_start = remote_trace_start;
12247 remote_ops.to_get_trace_status = remote_get_trace_status;
12248 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
12249 remote_ops.to_trace_stop = remote_trace_stop;
12250 remote_ops.to_trace_find = remote_trace_find;
12251 remote_ops.to_get_trace_state_variable_value
12252 = remote_get_trace_state_variable_value;
12253 remote_ops.to_save_trace_data = remote_save_trace_data;
12254 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
12255 remote_ops.to_upload_trace_state_variables
12256 = remote_upload_trace_state_variables;
12257 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
12258 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
12259 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
12260 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
12261 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
12262 remote_ops.to_set_trace_notes = remote_set_trace_notes;
12263 remote_ops.to_core_of_thread = remote_core_of_thread;
12264 remote_ops.to_verify_memory = remote_verify_memory;
12265 remote_ops.to_get_tib_address = remote_get_tib_address;
12266 remote_ops.to_set_permissions = remote_set_permissions;
12267 remote_ops.to_static_tracepoint_marker_at
12268 = remote_static_tracepoint_marker_at;
12269 remote_ops.to_static_tracepoint_markers_by_strid
12270 = remote_static_tracepoint_markers_by_strid;
12271 remote_ops.to_traceframe_info = remote_traceframe_info;
12272 remote_ops.to_use_agent = remote_use_agent;
12273 remote_ops.to_can_use_agent = remote_can_use_agent;
12274 remote_ops.to_supports_btrace = remote_supports_btrace;
12275 remote_ops.to_enable_btrace = remote_enable_btrace;
12276 remote_ops.to_disable_btrace = remote_disable_btrace;
12277 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12278 remote_ops.to_read_btrace = remote_read_btrace;
12279 remote_ops.to_btrace_conf = remote_btrace_conf;
12280 remote_ops.to_augmented_libraries_svr4_read =
12281 remote_augmented_libraries_svr4_read;
12284 /* Set up the extended remote vector by making a copy of the standard
12285 remote vector and adding to it. */
12288 init_extended_remote_ops (void)
12290 extended_remote_ops = remote_ops;
12292 extended_remote_ops.to_shortname = "extended-remote";
12293 extended_remote_ops.to_longname =
12294 "Extended remote serial target in gdb-specific protocol";
12295 extended_remote_ops.to_doc =
12296 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
12297 Specify the serial device it is connected to (e.g. /dev/ttya).";
12298 extended_remote_ops.to_open = extended_remote_open;
12299 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12300 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
12301 extended_remote_ops.to_detach = extended_remote_detach;
12302 extended_remote_ops.to_attach = extended_remote_attach;
12303 extended_remote_ops.to_post_attach = extended_remote_post_attach;
12304 extended_remote_ops.to_kill = extended_remote_kill;
12305 extended_remote_ops.to_supports_disable_randomization
12306 = extended_remote_supports_disable_randomization;
12307 extended_remote_ops.to_follow_fork = remote_follow_fork;
12308 extended_remote_ops.to_insert_fork_catchpoint
12309 = remote_insert_fork_catchpoint;
12310 extended_remote_ops.to_remove_fork_catchpoint
12311 = remote_remove_fork_catchpoint;
12312 extended_remote_ops.to_insert_vfork_catchpoint
12313 = remote_insert_vfork_catchpoint;
12314 extended_remote_ops.to_remove_vfork_catchpoint
12315 = remote_remove_vfork_catchpoint;
12319 remote_can_async_p (struct target_ops *ops)
12321 struct remote_state *rs = get_remote_state ();
12323 if (!target_async_permitted)
12324 /* We only enable async when the user specifically asks for it. */
12327 /* We're async whenever the serial device is. */
12328 return serial_can_async_p (rs->remote_desc);
12332 remote_is_async_p (struct target_ops *ops)
12334 struct remote_state *rs = get_remote_state ();
12336 if (!target_async_permitted)
12337 /* We only enable async when the user specifically asks for it. */
12340 /* We're async whenever the serial device is. */
12341 return serial_is_async_p (rs->remote_desc);
12344 /* Pass the SERIAL event on and up to the client. One day this code
12345 will be able to delay notifying the client of an event until the
12346 point where an entire packet has been received. */
12348 static serial_event_ftype remote_async_serial_handler;
12351 remote_async_serial_handler (struct serial *scb, void *context)
12353 struct remote_state *rs = context;
12355 /* Don't propogate error information up to the client. Instead let
12356 the client find out about the error by querying the target. */
12357 inferior_event_handler (INF_REG_EVENT, NULL);
12361 remote_async_inferior_event_handler (gdb_client_data data)
12363 inferior_event_handler (INF_REG_EVENT, NULL);
12367 remote_async (struct target_ops *ops, int enable)
12369 struct remote_state *rs = get_remote_state ();
12373 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
12375 /* If there are pending events in the stop reply queue tell the
12376 event loop to process them. */
12377 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12378 mark_async_event_handler (remote_async_inferior_event_token);
12382 serial_async (rs->remote_desc, NULL, NULL);
12383 clear_async_event_handler (remote_async_inferior_event_token);
12388 set_remote_cmd (char *args, int from_tty)
12390 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
12394 show_remote_cmd (char *args, int from_tty)
12396 /* We can't just use cmd_show_list here, because we want to skip
12397 the redundant "show remote Z-packet" and the legacy aliases. */
12398 struct cleanup *showlist_chain;
12399 struct cmd_list_element *list = remote_show_cmdlist;
12400 struct ui_out *uiout = current_uiout;
12402 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12403 for (; list != NULL; list = list->next)
12404 if (strcmp (list->name, "Z-packet") == 0)
12406 else if (list->type == not_set_cmd)
12407 /* Alias commands are exactly like the original, except they
12408 don't have the normal type. */
12412 struct cleanup *option_chain
12413 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
12415 ui_out_field_string (uiout, "name", list->name);
12416 ui_out_text (uiout, ": ");
12417 if (list->type == show_cmd)
12418 do_show_command ((char *) NULL, from_tty, list);
12420 cmd_func (list, NULL, from_tty);
12421 /* Close the tuple. */
12422 do_cleanups (option_chain);
12425 /* Close the tuple. */
12426 do_cleanups (showlist_chain);
12430 /* Function to be called whenever a new objfile (shlib) is detected. */
12432 remote_new_objfile (struct objfile *objfile)
12434 struct remote_state *rs = get_remote_state ();
12436 if (rs->remote_desc != 0) /* Have a remote connection. */
12437 remote_check_symbols ();
12440 /* Pull all the tracepoints defined on the target and create local
12441 data structures representing them. We don't want to create real
12442 tracepoints yet, we don't want to mess up the user's existing
12446 remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
12448 struct remote_state *rs = get_remote_state ();
12451 /* Ask for a first packet of tracepoint definition. */
12453 getpkt (&rs->buf, &rs->buf_size, 0);
12455 while (*p && *p != 'l')
12457 parse_tracepoint_definition (p, utpp);
12458 /* Ask for another packet of tracepoint definition. */
12460 getpkt (&rs->buf, &rs->buf_size, 0);
12467 remote_upload_trace_state_variables (struct target_ops *self,
12468 struct uploaded_tsv **utsvp)
12470 struct remote_state *rs = get_remote_state ();
12473 /* Ask for a first packet of variable definition. */
12475 getpkt (&rs->buf, &rs->buf_size, 0);
12477 while (*p && *p != 'l')
12479 parse_tsv_definition (p, utsvp);
12480 /* Ask for another packet of variable definition. */
12482 getpkt (&rs->buf, &rs->buf_size, 0);
12488 /* The "set/show range-stepping" show hook. */
12491 show_range_stepping (struct ui_file *file, int from_tty,
12492 struct cmd_list_element *c,
12495 fprintf_filtered (file,
12496 _("Debugger's willingness to use range stepping "
12497 "is %s.\n"), value);
12500 /* The "set/show range-stepping" set hook. */
12503 set_range_stepping (char *ignore_args, int from_tty,
12504 struct cmd_list_element *c)
12506 struct remote_state *rs = get_remote_state ();
12508 /* Whene enabling, check whether range stepping is actually
12509 supported by the target, and warn if not. */
12510 if (use_range_stepping)
12512 if (rs->remote_desc != NULL)
12514 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
12515 remote_vcont_probe (rs);
12517 if (packet_support (PACKET_vCont) == PACKET_ENABLE
12518 && rs->supports_vCont.r)
12522 warning (_("Range stepping is not supported by the current target"));
12527 _initialize_remote (void)
12529 struct remote_state *rs;
12530 struct cmd_list_element *cmd;
12531 const char *cmd_name;
12533 /* architecture specific data */
12534 remote_gdbarch_data_handle =
12535 gdbarch_data_register_post_init (init_remote_state);
12536 remote_g_packet_data_handle =
12537 gdbarch_data_register_pre_init (remote_g_packet_data_init);
12539 /* Initialize the per-target state. At the moment there is only one
12540 of these, not one per target. Only one target is active at a
12542 remote_state = new_remote_state ();
12544 init_remote_ops ();
12545 add_target (&remote_ops);
12547 init_extended_remote_ops ();
12548 add_target (&extended_remote_ops);
12550 /* Hook into new objfile notification. */
12551 observer_attach_new_objfile (remote_new_objfile);
12552 /* We're no longer interested in notification events of an inferior
12554 observer_attach_inferior_exit (discard_pending_stop_replies);
12556 /* Set up signal handlers. */
12557 async_sigint_remote_token =
12558 create_async_signal_handler (async_remote_interrupt, NULL);
12559 async_sigint_remote_twice_token =
12560 create_async_signal_handler (async_remote_interrupt_twice, NULL);
12563 init_remote_threadtests ();
12566 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
12567 /* set/show remote ... */
12569 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
12570 Remote protocol specific variables\n\
12571 Configure various remote-protocol specific variables such as\n\
12572 the packets being used"),
12573 &remote_set_cmdlist, "set remote ",
12574 0 /* allow-unknown */, &setlist);
12575 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
12576 Remote protocol specific variables\n\
12577 Configure various remote-protocol specific variables such as\n\
12578 the packets being used"),
12579 &remote_show_cmdlist, "show remote ",
12580 0 /* allow-unknown */, &showlist);
12582 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12583 Compare section data on target to the exec file.\n\
12584 Argument is a single section name (default: all loaded sections).\n\
12585 To compare only read-only loaded sections, specify the -r option."),
12588 add_cmd ("packet", class_maintenance, packet_command, _("\
12589 Send an arbitrary packet to a remote target.\n\
12590 maintenance packet TEXT\n\
12591 If GDB is talking to an inferior via the GDB serial protocol, then\n\
12592 this command sends the string TEXT to the inferior, and displays the\n\
12593 response packet. GDB supplies the initial `$' character, and the\n\
12594 terminating `#' character and checksum."),
12597 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12598 Set whether to send break if interrupted."), _("\
12599 Show whether to send break if interrupted."), _("\
12600 If set, a break, instead of a cntrl-c, is sent to the remote target."),
12601 set_remotebreak, show_remotebreak,
12602 &setlist, &showlist);
12603 cmd_name = "remotebreak";
12604 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12605 deprecate_cmd (cmd, "set remote interrupt-sequence");
12606 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12607 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12608 deprecate_cmd (cmd, "show remote interrupt-sequence");
12610 add_setshow_enum_cmd ("interrupt-sequence", class_support,
12611 interrupt_sequence_modes, &interrupt_sequence_mode,
12613 Set interrupt sequence to remote target."), _("\
12614 Show interrupt sequence to remote target."), _("\
12615 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12616 NULL, show_interrupt_sequence,
12617 &remote_set_cmdlist,
12618 &remote_show_cmdlist);
12620 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12621 &interrupt_on_connect, _("\
12622 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12623 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12624 If set, interrupt sequence is sent to remote target."),
12626 &remote_set_cmdlist, &remote_show_cmdlist);
12628 /* Install commands for configuring memory read/write packets. */
12630 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12631 Set the maximum number of bytes per memory write packet (deprecated)."),
12633 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12634 Show the maximum number of bytes per memory write packet (deprecated)."),
12636 add_cmd ("memory-write-packet-size", no_class,
12637 set_memory_write_packet_size, _("\
12638 Set the maximum number of bytes per memory-write packet.\n\
12639 Specify the number of bytes in a packet or 0 (zero) for the\n\
12640 default packet size. The actual limit is further reduced\n\
12641 dependent on the target. Specify ``fixed'' to disable the\n\
12642 further restriction and ``limit'' to enable that restriction."),
12643 &remote_set_cmdlist);
12644 add_cmd ("memory-read-packet-size", no_class,
12645 set_memory_read_packet_size, _("\
12646 Set the maximum number of bytes per memory-read packet.\n\
12647 Specify the number of bytes in a packet or 0 (zero) for the\n\
12648 default packet size. The actual limit is further reduced\n\
12649 dependent on the target. Specify ``fixed'' to disable the\n\
12650 further restriction and ``limit'' to enable that restriction."),
12651 &remote_set_cmdlist);
12652 add_cmd ("memory-write-packet-size", no_class,
12653 show_memory_write_packet_size,
12654 _("Show the maximum number of bytes per memory-write packet."),
12655 &remote_show_cmdlist);
12656 add_cmd ("memory-read-packet-size", no_class,
12657 show_memory_read_packet_size,
12658 _("Show the maximum number of bytes per memory-read packet."),
12659 &remote_show_cmdlist);
12661 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
12662 &remote_hw_watchpoint_limit, _("\
12663 Set the maximum number of target hardware watchpoints."), _("\
12664 Show the maximum number of target hardware watchpoints."), _("\
12665 Specify a negative limit for unlimited."),
12666 NULL, NULL, /* FIXME: i18n: The maximum
12667 number of target hardware
12668 watchpoints is %s. */
12669 &remote_set_cmdlist, &remote_show_cmdlist);
12670 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12671 &remote_hw_watchpoint_length_limit, _("\
12672 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12673 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12674 Specify a negative limit for unlimited."),
12675 NULL, NULL, /* FIXME: i18n: The maximum
12676 length (in bytes) of a target
12677 hardware watchpoint is %s. */
12678 &remote_set_cmdlist, &remote_show_cmdlist);
12679 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
12680 &remote_hw_breakpoint_limit, _("\
12681 Set the maximum number of target hardware breakpoints."), _("\
12682 Show the maximum number of target hardware breakpoints."), _("\
12683 Specify a negative limit for unlimited."),
12684 NULL, NULL, /* FIXME: i18n: The maximum
12685 number of target hardware
12686 breakpoints is %s. */
12687 &remote_set_cmdlist, &remote_show_cmdlist);
12689 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12690 &remote_address_size, _("\
12691 Set the maximum size of the address (in bits) in a memory packet."), _("\
12692 Show the maximum size of the address (in bits) in a memory packet."), NULL,
12694 NULL, /* FIXME: i18n: */
12695 &setlist, &showlist);
12697 init_all_packet_configs ();
12699 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
12700 "X", "binary-download", 1);
12702 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
12703 "vCont", "verbose-resume", 0);
12705 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12706 "QPassSignals", "pass-signals", 0);
12708 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12709 "QProgramSignals", "program-signals", 0);
12711 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
12712 "qSymbol", "symbol-lookup", 0);
12714 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
12715 "P", "set-register", 1);
12717 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
12718 "p", "fetch-register", 1);
12720 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
12721 "Z0", "software-breakpoint", 0);
12723 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
12724 "Z1", "hardware-breakpoint", 0);
12726 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
12727 "Z2", "write-watchpoint", 0);
12729 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
12730 "Z3", "read-watchpoint", 0);
12732 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
12733 "Z4", "access-watchpoint", 0);
12735 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12736 "qXfer:auxv:read", "read-aux-vector", 0);
12738 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12739 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12741 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12742 "qXfer:features:read", "target-features", 0);
12744 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12745 "qXfer:libraries:read", "library-info", 0);
12747 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12748 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12750 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12751 "qXfer:memory-map:read", "memory-map", 0);
12753 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12754 "qXfer:spu:read", "read-spu-object", 0);
12756 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12757 "qXfer:spu:write", "write-spu-object", 0);
12759 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12760 "qXfer:osdata:read", "osdata", 0);
12762 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12763 "qXfer:threads:read", "threads", 0);
12765 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12766 "qXfer:siginfo:read", "read-siginfo-object", 0);
12768 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12769 "qXfer:siginfo:write", "write-siginfo-object", 0);
12771 add_packet_config_cmd
12772 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
12773 "qXfer:traceframe-info:read", "traceframe-info", 0);
12775 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12776 "qXfer:uib:read", "unwind-info-block", 0);
12778 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
12779 "qGetTLSAddr", "get-thread-local-storage-address",
12782 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12783 "qGetTIBAddr", "get-thread-information-block-address",
12786 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12787 "bc", "reverse-continue", 0);
12789 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12790 "bs", "reverse-step", 0);
12792 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12793 "qSupported", "supported-packets", 0);
12795 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12796 "qSearch:memory", "search-memory", 0);
12798 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12799 "qTStatus", "trace-status", 0);
12801 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
12802 "vFile:setfs", "hostio-setfs", 0);
12804 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12805 "vFile:open", "hostio-open", 0);
12807 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12808 "vFile:pread", "hostio-pread", 0);
12810 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12811 "vFile:pwrite", "hostio-pwrite", 0);
12813 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12814 "vFile:close", "hostio-close", 0);
12816 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12817 "vFile:unlink", "hostio-unlink", 0);
12819 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12820 "vFile:readlink", "hostio-readlink", 0);
12822 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12823 "vFile:fstat", "hostio-fstat", 0);
12825 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12826 "vAttach", "attach", 0);
12828 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12831 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12832 "QStartNoAckMode", "noack", 0);
12834 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12835 "vKill", "kill", 0);
12837 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12838 "qAttached", "query-attached", 0);
12840 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
12841 "ConditionalTracepoints",
12842 "conditional-tracepoints", 0);
12844 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12845 "ConditionalBreakpoints",
12846 "conditional-breakpoints", 0);
12848 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12849 "BreakpointCommands",
12850 "breakpoint-commands", 0);
12852 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12853 "FastTracepoints", "fast-tracepoints", 0);
12855 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12856 "TracepointSource", "TracepointSource", 0);
12858 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12859 "QAllow", "allow", 0);
12861 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12862 "StaticTracepoints", "static-tracepoints", 0);
12864 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12865 "InstallInTrace", "install-in-trace", 0);
12867 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12868 "qXfer:statictrace:read", "read-sdata-object", 0);
12870 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12871 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12873 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12874 "QDisableRandomization", "disable-randomization", 0);
12876 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12877 "QAgent", "agent", 0);
12879 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12880 "QTBuffer:size", "trace-buffer-size", 0);
12882 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12883 "Qbtrace:off", "disable-btrace", 0);
12885 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12886 "Qbtrace:bts", "enable-btrace", 0);
12888 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12889 "qXfer:btrace", "read-btrace", 0);
12891 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12892 "qXfer:btrace-conf", "read-btrace-conf", 0);
12894 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12895 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12897 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12898 "swbreak-feature", "swbreak-feature", 0);
12900 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12901 "hwbreak-feature", "hwbreak-feature", 0);
12903 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12904 "fork-event-feature", "fork-event-feature", 0);
12906 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12907 "vfork-event-feature", "vfork-event-feature", 0);
12909 /* Assert that we've registered "set remote foo-packet" commands
12910 for all packet configs. */
12914 for (i = 0; i < PACKET_MAX; i++)
12916 /* Ideally all configs would have a command associated. Some
12917 still don't though. */
12922 case PACKET_QNonStop:
12923 case PACKET_multiprocess_feature:
12924 case PACKET_EnableDisableTracepoints_feature:
12925 case PACKET_tracenz_feature:
12926 case PACKET_DisconnectedTracing_feature:
12927 case PACKET_augmented_libraries_svr4_read_feature:
12929 /* Additions to this list need to be well justified:
12930 pre-existing packets are OK; new packets are not. */
12938 /* This catches both forgetting to add a config command, and
12939 forgetting to remove a packet from the exception list. */
12940 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12944 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12945 Z sub-packet has its own set and show commands, but users may
12946 have sets to this variable in their .gdbinit files (or in their
12948 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
12949 &remote_Z_packet_detect, _("\
12950 Set use of remote protocol `Z' packets"), _("\
12951 Show use of remote protocol `Z' packets "), _("\
12952 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12954 set_remote_protocol_Z_packet_cmd,
12955 show_remote_protocol_Z_packet_cmd,
12956 /* FIXME: i18n: Use of remote protocol
12957 `Z' packets is %s. */
12958 &remote_set_cmdlist, &remote_show_cmdlist);
12960 add_prefix_cmd ("remote", class_files, remote_command, _("\
12961 Manipulate files on the remote system\n\
12962 Transfer files to and from the remote target system."),
12963 &remote_cmdlist, "remote ",
12964 0 /* allow-unknown */, &cmdlist);
12966 add_cmd ("put", class_files, remote_put_command,
12967 _("Copy a local file to the remote system."),
12970 add_cmd ("get", class_files, remote_get_command,
12971 _("Copy a remote file to the local system."),
12974 add_cmd ("delete", class_files, remote_delete_command,
12975 _("Delete a remote file."),
12978 remote_exec_file = xstrdup ("");
12979 add_setshow_string_noescape_cmd ("exec-file", class_files,
12980 &remote_exec_file, _("\
12981 Set the remote pathname for \"run\""), _("\
12982 Show the remote pathname for \"run\""), NULL, NULL, NULL,
12983 &remote_set_cmdlist, &remote_show_cmdlist);
12985 add_setshow_boolean_cmd ("range-stepping", class_run,
12986 &use_range_stepping, _("\
12987 Enable or disable range stepping."), _("\
12988 Show whether target-assisted range stepping is enabled."), _("\
12989 If on, and the target supports it, when stepping a source line, GDB\n\
12990 tells the target to step the corresponding range of addresses itself instead\n\
12991 of issuing multiple single-steps. This speeds up source level\n\
12992 stepping. If off, GDB always issues single-steps, even if range\n\
12993 stepping is supported by the target. The default is on."),
12994 set_range_stepping,
12995 show_range_stepping,
12999 /* Eventually initialize fileio. See fileio.c */
13000 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
13002 /* Take advantage of the fact that the TID field is not used, to tag
13003 special ptids with it set to != 0. */
13004 magic_null_ptid = ptid_build (42000, -1, 1);
13005 not_sent_ptid = ptid_build (42000, -2, 1);
13006 any_thread_ptid = ptid_build (42000, 0, 1);
13008 target_buf_size = 2048;
13009 target_buf = xmalloc (target_buf_size);