1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2012 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. */
23 #include "gdb_string.h"
29 #include "exceptions.h"
31 /*#include "terminal.h" */
34 #include "gdb-stabs.h"
35 #include "gdbthread.h"
39 #include "gdb_assert.h"
42 #include "cli/cli-decode.h"
43 #include "cli/cli-setshow.h"
44 #include "target-descriptions.h"
49 #include "event-loop.h"
50 #include "event-top.h"
56 #include "gdbcore.h" /* for exec_bfd */
58 #include "remote-fileio.h"
59 #include "gdb/fileio.h"
61 #include "xml-support.h"
63 #include "memory-map.h"
65 #include "tracepoint.h"
70 /* Temp hacks for tracepoint encoding migration. */
71 static char *target_buf;
72 static long target_buf_size;
74 /* The size to align memory write packets, when practical. The protocol
75 does not guarantee any alignment, and gdb will generate short
76 writes and unaligned writes, but even as a best-effort attempt this
77 can improve bulk transfers. For instance, if a write is misaligned
78 relative to the target's data bus, the stub may need to make an extra
79 round trip fetching data from the target. This doesn't make a
80 huge difference, but it's easy to do, so we try to be helpful.
82 The alignment chosen is arbitrary; usually data bus width is
83 important here, not the possibly larger cache line size. */
84 enum { REMOTE_ALIGN_WRITES = 16 };
86 /* Prototypes for local functions. */
87 static void cleanup_sigint_signal_handler (void *dummy);
88 static void initialize_sigint_signal_handler (void);
89 static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
90 static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
93 static void handle_remote_sigint (int);
94 static void handle_remote_sigint_twice (int);
95 static void async_remote_interrupt (gdb_client_data);
96 void async_remote_interrupt_twice (gdb_client_data);
98 static void remote_files_info (struct target_ops *ignore);
100 static void remote_prepare_to_store (struct regcache *regcache);
102 static void remote_open (char *name, int from_tty);
104 static void extended_remote_open (char *name, int from_tty);
106 static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
108 static void remote_close (int quitting);
110 static void remote_mourn (struct target_ops *ops);
112 static void extended_remote_restart (void);
114 static void extended_remote_mourn (struct target_ops *);
116 static void remote_mourn_1 (struct target_ops *);
118 static void remote_send (char **buf, long *sizeof_buf_p);
120 static int readchar (int timeout);
122 static void remote_kill (struct target_ops *ops);
124 static int tohex (int nib);
126 static int remote_can_async_p (void);
128 static int remote_is_async_p (void);
130 static void remote_async (void (*callback) (enum inferior_event_type event_type,
131 void *context), void *context);
133 static void remote_detach (struct target_ops *ops, char *args, int from_tty);
135 static void remote_interrupt (int signo);
137 static void remote_interrupt_twice (int signo);
139 static void interrupt_query (void);
141 static void set_general_thread (struct ptid ptid);
142 static void set_continue_thread (struct ptid ptid);
144 static void get_offsets (void);
146 static void skip_frame (void);
148 static long read_frame (char **buf_p, long *sizeof_buf);
150 static int hexnumlen (ULONGEST num);
152 static void init_remote_ops (void);
154 static void init_extended_remote_ops (void);
156 static void remote_stop (ptid_t);
158 static int ishex (int ch, int *val);
160 static int stubhex (int ch);
162 static int hexnumstr (char *, ULONGEST);
164 static int hexnumnstr (char *, ULONGEST, int);
166 static CORE_ADDR remote_address_masked (CORE_ADDR);
168 static void print_packet (char *);
170 static void compare_sections_command (char *, int);
172 static void packet_command (char *, int);
174 static int stub_unpack_int (char *buff, int fieldlength);
176 static ptid_t remote_current_thread (ptid_t oldptid);
178 static void remote_find_new_threads (void);
180 static void record_currthread (ptid_t currthread);
182 static int fromhex (int a);
184 extern int hex2bin (const char *hex, gdb_byte *bin, int count);
186 extern int bin2hex (const gdb_byte *bin, char *hex, int count);
188 static int putpkt_binary (char *buf, int cnt);
190 static void check_binary_download (CORE_ADDR addr);
192 struct packet_config;
194 static void show_packet_config_cmd (struct packet_config *config);
196 static void update_packet_config (struct packet_config *config);
198 static void set_remote_protocol_packet_cmd (char *args, int from_tty,
199 struct cmd_list_element *c);
201 static void show_remote_protocol_packet_cmd (struct ui_file *file,
203 struct cmd_list_element *c,
206 static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
207 static ptid_t read_ptid (char *buf, char **obuf);
209 static void remote_set_permissions (void);
212 static int remote_get_trace_status (struct trace_status *ts);
214 static int remote_upload_tracepoints (struct uploaded_tp **utpp);
216 static int remote_upload_trace_state_variables (struct uploaded_tsv **utsvp);
218 static void remote_query_supported (void);
220 static void remote_check_symbols (struct objfile *objfile);
222 void _initialize_remote (void);
225 static struct stop_reply *stop_reply_xmalloc (void);
226 static void stop_reply_xfree (struct stop_reply *);
227 static void do_stop_reply_xfree (void *arg);
228 static void remote_parse_stop_reply (char *buf, struct stop_reply *);
229 static void push_stop_reply (struct stop_reply *);
230 static void remote_get_pending_stop_replies (void);
231 static void discard_pending_stop_replies (int pid);
232 static int peek_stop_reply (ptid_t ptid);
234 static void remote_async_inferior_event_handler (gdb_client_data);
235 static void remote_async_get_pending_events_handler (gdb_client_data);
237 static void remote_terminal_ours (void);
239 static int remote_read_description_p (struct target_ops *target);
241 static void remote_console_output (char *msg);
243 static int remote_supports_cond_breakpoints (void);
245 static int remote_can_run_breakpoint_commands (void);
247 /* The non-stop remote protocol provisions for one pending stop reply.
248 This is where we keep it until it is acknowledged. */
250 static struct stop_reply *pending_stop_reply = NULL;
254 static struct cmd_list_element *remote_cmdlist;
256 /* For "set remote" and "show remote". */
258 static struct cmd_list_element *remote_set_cmdlist;
259 static struct cmd_list_element *remote_show_cmdlist;
261 /* Description of the remote protocol state for the currently
262 connected target. This is per-target state, and independent of the
263 selected architecture. */
267 /* A buffer to use for incoming packets, and its current size. The
268 buffer is grown dynamically for larger incoming packets.
269 Outgoing packets may also be constructed in this buffer.
270 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
271 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
276 /* True if we're going through initial connection setup (finding out
277 about the remote side's threads, relocating symbols, etc.). */
280 /* If we negotiated packet size explicitly (and thus can bypass
281 heuristics for the largest packet size that will not overflow
282 a buffer in the stub), this will be set to that packet size.
283 Otherwise zero, meaning to use the guessed size. */
284 long explicit_packet_size;
286 /* remote_wait is normally called when the target is running and
287 waits for a stop reply packet. But sometimes we need to call it
288 when the target is already stopped. We can send a "?" packet
289 and have remote_wait read the response. Or, if we already have
290 the response, we can stash it in BUF and tell remote_wait to
291 skip calling getpkt. This flag is set when BUF contains a
292 stop reply packet and the target is not waiting. */
293 int cached_wait_status;
295 /* True, if in no ack mode. That is, neither GDB nor the stub will
296 expect acks from each other. The connection is assumed to be
300 /* True if we're connected in extended remote mode. */
303 /* True if the stub reported support for multi-process
305 int multi_process_aware;
307 /* True if we resumed the target and we're waiting for the target to
308 stop. In the mean time, we can't start another command/query.
309 The remote server wouldn't be ready to process it, so we'd
310 timeout waiting for a reply that would never come and eventually
311 we'd close the connection. This can happen in asynchronous mode
312 because we allow GDB commands while the target is running. */
313 int waiting_for_stop_reply;
315 /* True if the stub reports support for non-stop mode. */
318 /* True if the stub reports support for vCont;t. */
321 /* True if the stub reports support for conditional tracepoints. */
322 int cond_tracepoints;
324 /* True if the stub reports support for target-side breakpoint
326 int cond_breakpoints;
328 /* True if the stub reports support for target-side breakpoint
330 int breakpoint_commands;
332 /* True if the stub reports support for fast tracepoints. */
333 int fast_tracepoints;
335 /* True if the stub reports support for static tracepoints. */
336 int static_tracepoints;
338 /* True if the stub reports support for installing tracepoint while
340 int install_in_trace;
342 /* True if the stub can continue running a trace while GDB is
344 int disconnected_tracing;
346 /* True if the stub reports support for enabling and disabling
347 tracepoints while a trace experiment is running. */
348 int enable_disable_tracepoints;
350 /* True if the stub can collect strings using tracenz bytecode. */
353 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
354 responded to that. */
358 /* Private data that we'll store in (struct thread_info)->private. */
359 struct private_thread_info
366 free_private_thread_info (struct private_thread_info *info)
372 /* Returns true if the multi-process extensions are in effect. */
374 remote_multi_process_p (struct remote_state *rs)
376 return rs->multi_process_aware;
379 /* This data could be associated with a target, but we do not always
380 have access to the current target when we need it, so for now it is
381 static. This will be fine for as long as only one target is in use
383 static struct remote_state remote_state;
385 static struct remote_state *
386 get_remote_state_raw (void)
388 return &remote_state;
391 /* Description of the remote protocol for a given architecture. */
395 long offset; /* Offset into G packet. */
396 long regnum; /* GDB's internal register number. */
397 LONGEST pnum; /* Remote protocol register number. */
398 int in_g_packet; /* Always part of G packet. */
399 /* long size in bytes; == register_size (target_gdbarch, regnum);
401 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
405 struct remote_arch_state
407 /* Description of the remote protocol registers. */
408 long sizeof_g_packet;
410 /* Description of the remote protocol registers indexed by REGNUM
411 (making an array gdbarch_num_regs in size). */
412 struct packet_reg *regs;
414 /* This is the size (in chars) of the first response to the ``g''
415 packet. It is used as a heuristic when determining the maximum
416 size of memory-read and memory-write packets. A target will
417 typically only reserve a buffer large enough to hold the ``g''
418 packet. The size does not include packet overhead (headers and
420 long actual_register_packet_size;
422 /* This is the maximum size (in chars) of a non read/write packet.
423 It is also used as a cap on the size of read/write packets. */
424 long remote_packet_size;
427 long sizeof_pkt = 2000;
429 /* Utility: generate error from an incoming stub packet. */
431 trace_error (char *buf)
434 return; /* not an error msg */
437 case '1': /* malformed packet error */
438 if (*++buf == '0') /* general case: */
439 error (_("remote.c: error in outgoing packet."));
441 error (_("remote.c: error in outgoing packet at field #%ld."),
442 strtol (buf, NULL, 16));
444 error (_("trace API error 0x%s."), ++buf);
446 error (_("Target returns error code '%s'."), buf);
450 /* Utility: wait for reply from stub, while accepting "O" packets. */
452 remote_get_noisy_reply (char **buf_p,
455 do /* Loop on reply from remote stub. */
459 QUIT; /* Allow user to bail out with ^C. */
460 getpkt (buf_p, sizeof_buf, 0);
464 else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
467 CORE_ADDR from, to, org_to;
469 int adjusted_size = 0;
470 volatile struct gdb_exception ex;
472 p = buf + strlen ("qRelocInsn:");
473 pp = unpack_varlen_hex (p, &ul);
475 error (_("invalid qRelocInsn packet: %s"), buf);
479 unpack_varlen_hex (p, &ul);
484 TRY_CATCH (ex, RETURN_MASK_ALL)
486 gdbarch_relocate_instruction (target_gdbarch, &to, from);
490 adjusted_size = to - org_to;
492 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
495 else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
497 /* Propagate memory errors silently back to the target.
498 The stub may have limited the range of addresses we
499 can write to, for example. */
504 /* Something unexpectedly bad happened. Be verbose so
505 we can tell what, and propagate the error back to the
506 stub, so it doesn't get stuck waiting for a
508 exception_fprintf (gdb_stderr, ex,
509 _("warning: relocating instruction: "));
513 else if (buf[0] == 'O' && buf[1] != 'K')
514 remote_console_output (buf + 1); /* 'O' message from stub */
516 return buf; /* Here's the actual reply. */
521 /* Handle for retreving the remote protocol data from gdbarch. */
522 static struct gdbarch_data *remote_gdbarch_data_handle;
524 static struct remote_arch_state *
525 get_remote_arch_state (void)
527 return gdbarch_data (target_gdbarch, remote_gdbarch_data_handle);
530 /* Fetch the global remote target state. */
532 static struct remote_state *
533 get_remote_state (void)
535 /* Make sure that the remote architecture state has been
536 initialized, because doing so might reallocate rs->buf. Any
537 function which calls getpkt also needs to be mindful of changes
538 to rs->buf, but this call limits the number of places which run
540 get_remote_arch_state ();
542 return get_remote_state_raw ();
546 compare_pnums (const void *lhs_, const void *rhs_)
548 const struct packet_reg * const *lhs = lhs_;
549 const struct packet_reg * const *rhs = rhs_;
551 if ((*lhs)->pnum < (*rhs)->pnum)
553 else if ((*lhs)->pnum == (*rhs)->pnum)
560 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
562 int regnum, num_remote_regs, offset;
563 struct packet_reg **remote_regs;
565 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
567 struct packet_reg *r = ®s[regnum];
569 if (register_size (gdbarch, regnum) == 0)
570 /* Do not try to fetch zero-sized (placeholder) registers. */
573 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
578 /* Define the g/G packet format as the contents of each register
579 with a remote protocol number, in order of ascending protocol
582 remote_regs = alloca (gdbarch_num_regs (gdbarch)
583 * sizeof (struct packet_reg *));
584 for (num_remote_regs = 0, regnum = 0;
585 regnum < gdbarch_num_regs (gdbarch);
587 if (regs[regnum].pnum != -1)
588 remote_regs[num_remote_regs++] = ®s[regnum];
590 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
593 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
595 remote_regs[regnum]->in_g_packet = 1;
596 remote_regs[regnum]->offset = offset;
597 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
603 /* Given the architecture described by GDBARCH, return the remote
604 protocol register's number and the register's offset in the g/G
605 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
606 If the target does not have a mapping for REGNUM, return false,
607 otherwise, return true. */
610 remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
611 int *pnum, int *poffset)
614 struct packet_reg *regs;
615 struct cleanup *old_chain;
617 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
619 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
620 old_chain = make_cleanup (xfree, regs);
622 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
624 *pnum = regs[regnum].pnum;
625 *poffset = regs[regnum].offset;
627 do_cleanups (old_chain);
633 init_remote_state (struct gdbarch *gdbarch)
635 struct remote_state *rs = get_remote_state_raw ();
636 struct remote_arch_state *rsa;
638 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
640 /* Use the architecture to build a regnum<->pnum table, which will be
641 1:1 unless a feature set specifies otherwise. */
642 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
643 gdbarch_num_regs (gdbarch),
646 /* Record the maximum possible size of the g packet - it may turn out
648 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
650 /* Default maximum number of characters in a packet body. Many
651 remote stubs have a hardwired buffer size of 400 bytes
652 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
653 as the maximum packet-size to ensure that the packet and an extra
654 NUL character can always fit in the buffer. This stops GDB
655 trashing stubs that try to squeeze an extra NUL into what is
656 already a full buffer (As of 1999-12-04 that was most stubs). */
657 rsa->remote_packet_size = 400 - 1;
659 /* This one is filled in when a ``g'' packet is received. */
660 rsa->actual_register_packet_size = 0;
662 /* Should rsa->sizeof_g_packet needs more space than the
663 default, adjust the size accordingly. Remember that each byte is
664 encoded as two characters. 32 is the overhead for the packet
665 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
666 (``$NN:G...#NN'') is a better guess, the below has been padded a
668 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
669 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
671 /* Make sure that the packet buffer is plenty big enough for
672 this architecture. */
673 if (rs->buf_size < rsa->remote_packet_size)
675 rs->buf_size = 2 * rsa->remote_packet_size;
676 rs->buf = xrealloc (rs->buf, rs->buf_size);
682 /* Return the current allowed size of a remote packet. This is
683 inferred from the current architecture, and should be used to
684 limit the length of outgoing packets. */
686 get_remote_packet_size (void)
688 struct remote_state *rs = get_remote_state ();
689 struct remote_arch_state *rsa = get_remote_arch_state ();
691 if (rs->explicit_packet_size)
692 return rs->explicit_packet_size;
694 return rsa->remote_packet_size;
697 static struct packet_reg *
698 packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
700 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch))
704 struct packet_reg *r = &rsa->regs[regnum];
706 gdb_assert (r->regnum == regnum);
711 static struct packet_reg *
712 packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
716 for (i = 0; i < gdbarch_num_regs (target_gdbarch); i++)
718 struct packet_reg *r = &rsa->regs[i];
726 /* FIXME: graces/2002-08-08: These variables should eventually be
727 bound to an instance of the target object (as in gdbarch-tdep()),
728 when such a thing exists. */
730 /* This is set to the data address of the access causing the target
731 to stop for a watchpoint. */
732 static CORE_ADDR remote_watch_data_address;
734 /* This is non-zero if target stopped for a watchpoint. */
735 static int remote_stopped_by_watchpoint_p;
737 static struct target_ops remote_ops;
739 static struct target_ops extended_remote_ops;
741 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
742 ``forever'' still use the normal timeout mechanism. This is
743 currently used by the ASYNC code to guarentee that target reads
744 during the initial connect always time-out. Once getpkt has been
745 modified to return a timeout indication and, in turn
746 remote_wait()/wait_for_inferior() have gained a timeout parameter
748 static int wait_forever_enabled_p = 1;
750 /* Allow the user to specify what sequence to send to the remote
751 when he requests a program interruption: Although ^C is usually
752 what remote systems expect (this is the default, here), it is
753 sometimes preferable to send a break. On other systems such
754 as the Linux kernel, a break followed by g, which is Magic SysRq g
755 is required in order to interrupt the execution. */
756 const char interrupt_sequence_control_c[] = "Ctrl-C";
757 const char interrupt_sequence_break[] = "BREAK";
758 const char interrupt_sequence_break_g[] = "BREAK-g";
759 static const char *const interrupt_sequence_modes[] =
761 interrupt_sequence_control_c,
762 interrupt_sequence_break,
763 interrupt_sequence_break_g,
766 static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
769 show_interrupt_sequence (struct ui_file *file, int from_tty,
770 struct cmd_list_element *c,
773 if (interrupt_sequence_mode == interrupt_sequence_control_c)
774 fprintf_filtered (file,
775 _("Send the ASCII ETX character (Ctrl-c) "
776 "to the remote target to interrupt the "
777 "execution of the program.\n"));
778 else if (interrupt_sequence_mode == interrupt_sequence_break)
779 fprintf_filtered (file,
780 _("send a break signal to the remote target "
781 "to interrupt the execution of the program.\n"));
782 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
783 fprintf_filtered (file,
784 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
785 "the remote target to interrupt the execution "
786 "of Linux kernel.\n"));
788 internal_error (__FILE__, __LINE__,
789 _("Invalid value for interrupt_sequence_mode: %s."),
790 interrupt_sequence_mode);
793 /* This boolean variable specifies whether interrupt_sequence is sent
794 to the remote target when gdb connects to it.
795 This is mostly needed when you debug the Linux kernel: The Linux kernel
796 expects BREAK g which is Magic SysRq g for connecting gdb. */
797 static int interrupt_on_connect = 0;
799 /* This variable is used to implement the "set/show remotebreak" commands.
800 Since these commands are now deprecated in favor of "set/show remote
801 interrupt-sequence", it no longer has any effect on the code. */
802 static int remote_break;
805 set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
808 interrupt_sequence_mode = interrupt_sequence_break;
810 interrupt_sequence_mode = interrupt_sequence_control_c;
814 show_remotebreak (struct ui_file *file, int from_tty,
815 struct cmd_list_element *c,
820 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
821 remote_open knows that we don't have a file open when the program
823 static struct serial *remote_desc = NULL;
825 /* This variable sets the number of bits in an address that are to be
826 sent in a memory ("M" or "m") packet. Normally, after stripping
827 leading zeros, the entire address would be sent. This variable
828 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
829 initial implementation of remote.c restricted the address sent in
830 memory packets to ``host::sizeof long'' bytes - (typically 32
831 bits). Consequently, for 64 bit targets, the upper 32 bits of an
832 address was never sent. Since fixing this bug may cause a break in
833 some remote targets this variable is principly provided to
834 facilitate backward compatibility. */
836 static int remote_address_size;
838 /* Temporary to track who currently owns the terminal. See
839 remote_terminal_* for more details. */
841 static int remote_async_terminal_ours_p;
843 /* The executable file to use for "run" on the remote side. */
845 static char *remote_exec_file = "";
848 /* User configurable variables for the number of characters in a
849 memory read/write packet. MIN (rsa->remote_packet_size,
850 rsa->sizeof_g_packet) is the default. Some targets need smaller
851 values (fifo overruns, et.al.) and some users need larger values
852 (speed up transfers). The variables ``preferred_*'' (the user
853 request), ``current_*'' (what was actually set) and ``forced_*''
854 (Positive - a soft limit, negative - a hard limit). */
856 struct memory_packet_config
863 /* Compute the current size of a read/write packet. Since this makes
864 use of ``actual_register_packet_size'' the computation is dynamic. */
867 get_memory_packet_size (struct memory_packet_config *config)
869 struct remote_state *rs = get_remote_state ();
870 struct remote_arch_state *rsa = get_remote_arch_state ();
872 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
873 law?) that some hosts don't cope very well with large alloca()
874 calls. Eventually the alloca() code will be replaced by calls to
875 xmalloc() and make_cleanups() allowing this restriction to either
876 be lifted or removed. */
877 #ifndef MAX_REMOTE_PACKET_SIZE
878 #define MAX_REMOTE_PACKET_SIZE 16384
880 /* NOTE: 20 ensures we can write at least one byte. */
881 #ifndef MIN_REMOTE_PACKET_SIZE
882 #define MIN_REMOTE_PACKET_SIZE 20
887 if (config->size <= 0)
888 what_they_get = MAX_REMOTE_PACKET_SIZE;
890 what_they_get = config->size;
894 what_they_get = get_remote_packet_size ();
895 /* Limit the packet to the size specified by the user. */
897 && what_they_get > config->size)
898 what_they_get = config->size;
900 /* Limit it to the size of the targets ``g'' response unless we have
901 permission from the stub to use a larger packet size. */
902 if (rs->explicit_packet_size == 0
903 && rsa->actual_register_packet_size > 0
904 && what_they_get > rsa->actual_register_packet_size)
905 what_they_get = rsa->actual_register_packet_size;
907 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
908 what_they_get = MAX_REMOTE_PACKET_SIZE;
909 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
910 what_they_get = MIN_REMOTE_PACKET_SIZE;
912 /* Make sure there is room in the global buffer for this packet
913 (including its trailing NUL byte). */
914 if (rs->buf_size < what_they_get + 1)
916 rs->buf_size = 2 * what_they_get;
917 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
920 return what_they_get;
923 /* Update the size of a read/write packet. If they user wants
924 something really big then do a sanity check. */
927 set_memory_packet_size (char *args, struct memory_packet_config *config)
929 int fixed_p = config->fixed_p;
930 long size = config->size;
933 error (_("Argument required (integer, `fixed' or `limited')."));
934 else if (strcmp (args, "hard") == 0
935 || strcmp (args, "fixed") == 0)
937 else if (strcmp (args, "soft") == 0
938 || strcmp (args, "limit") == 0)
944 size = strtoul (args, &end, 0);
946 error (_("Invalid %s (bad syntax)."), config->name);
948 /* Instead of explicitly capping the size of a packet to
949 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
950 instead allowed to set the size to something arbitrarily
952 if (size > MAX_REMOTE_PACKET_SIZE)
953 error (_("Invalid %s (too large)."), config->name);
957 if (fixed_p && !config->fixed_p)
959 if (! query (_("The target may not be able to correctly handle a %s\n"
960 "of %ld bytes. Change the packet size? "),
962 error (_("Packet size not changed."));
964 /* Update the config. */
965 config->fixed_p = fixed_p;
970 show_memory_packet_size (struct memory_packet_config *config)
972 printf_filtered (_("The %s is %ld. "), config->name, config->size);
974 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
975 get_memory_packet_size (config));
977 printf_filtered (_("Packets are limited to %ld bytes.\n"),
978 get_memory_packet_size (config));
981 static struct memory_packet_config memory_write_packet_config =
983 "memory-write-packet-size",
987 set_memory_write_packet_size (char *args, int from_tty)
989 set_memory_packet_size (args, &memory_write_packet_config);
993 show_memory_write_packet_size (char *args, int from_tty)
995 show_memory_packet_size (&memory_write_packet_config);
999 get_memory_write_packet_size (void)
1001 return get_memory_packet_size (&memory_write_packet_config);
1004 static struct memory_packet_config memory_read_packet_config =
1006 "memory-read-packet-size",
1010 set_memory_read_packet_size (char *args, int from_tty)
1012 set_memory_packet_size (args, &memory_read_packet_config);
1016 show_memory_read_packet_size (char *args, int from_tty)
1018 show_memory_packet_size (&memory_read_packet_config);
1022 get_memory_read_packet_size (void)
1024 long size = get_memory_packet_size (&memory_read_packet_config);
1026 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1027 extra buffer size argument before the memory read size can be
1028 increased beyond this. */
1029 if (size > get_remote_packet_size ())
1030 size = get_remote_packet_size ();
1035 /* Generic configuration support for packets the stub optionally
1036 supports. Allows the user to specify the use of the packet as well
1037 as allowing GDB to auto-detect support in the remote stub. */
1041 PACKET_SUPPORT_UNKNOWN = 0,
1046 struct packet_config
1050 enum auto_boolean detect;
1051 enum packet_support support;
1054 /* Analyze a packet's return value and update the packet config
1065 update_packet_config (struct packet_config *config)
1067 switch (config->detect)
1069 case AUTO_BOOLEAN_TRUE:
1070 config->support = PACKET_ENABLE;
1072 case AUTO_BOOLEAN_FALSE:
1073 config->support = PACKET_DISABLE;
1075 case AUTO_BOOLEAN_AUTO:
1076 config->support = PACKET_SUPPORT_UNKNOWN;
1082 show_packet_config_cmd (struct packet_config *config)
1084 char *support = "internal-error";
1086 switch (config->support)
1089 support = "enabled";
1091 case PACKET_DISABLE:
1092 support = "disabled";
1094 case PACKET_SUPPORT_UNKNOWN:
1095 support = "unknown";
1098 switch (config->detect)
1100 case AUTO_BOOLEAN_AUTO:
1101 printf_filtered (_("Support for the `%s' packet "
1102 "is auto-detected, currently %s.\n"),
1103 config->name, support);
1105 case AUTO_BOOLEAN_TRUE:
1106 case AUTO_BOOLEAN_FALSE:
1107 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1108 config->name, support);
1114 add_packet_config_cmd (struct packet_config *config, const char *name,
1115 const char *title, int legacy)
1121 config->name = name;
1122 config->title = title;
1123 config->detect = AUTO_BOOLEAN_AUTO;
1124 config->support = PACKET_SUPPORT_UNKNOWN;
1125 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1127 show_doc = xstrprintf ("Show current use of remote "
1128 "protocol `%s' (%s) packet",
1130 /* set/show TITLE-packet {auto,on,off} */
1131 cmd_name = xstrprintf ("%s-packet", title);
1132 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1133 &config->detect, set_doc,
1134 show_doc, NULL, /* help_doc */
1135 set_remote_protocol_packet_cmd,
1136 show_remote_protocol_packet_cmd,
1137 &remote_set_cmdlist, &remote_show_cmdlist);
1138 /* The command code copies the documentation strings. */
1141 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1146 legacy_name = xstrprintf ("%s-packet", name);
1147 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1148 &remote_set_cmdlist);
1149 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
1150 &remote_show_cmdlist);
1154 static enum packet_result
1155 packet_check_result (const char *buf)
1159 /* The stub recognized the packet request. Check that the
1160 operation succeeded. */
1162 && isxdigit (buf[1]) && isxdigit (buf[2])
1164 /* "Enn" - definitly an error. */
1165 return PACKET_ERROR;
1167 /* Always treat "E." as an error. This will be used for
1168 more verbose error messages, such as E.memtypes. */
1169 if (buf[0] == 'E' && buf[1] == '.')
1170 return PACKET_ERROR;
1172 /* The packet may or may not be OK. Just assume it is. */
1176 /* The stub does not support the packet. */
1177 return PACKET_UNKNOWN;
1180 static enum packet_result
1181 packet_ok (const char *buf, struct packet_config *config)
1183 enum packet_result result;
1185 result = packet_check_result (buf);
1190 /* The stub recognized the packet request. */
1191 switch (config->support)
1193 case PACKET_SUPPORT_UNKNOWN:
1195 fprintf_unfiltered (gdb_stdlog,
1196 "Packet %s (%s) is supported\n",
1197 config->name, config->title);
1198 config->support = PACKET_ENABLE;
1200 case PACKET_DISABLE:
1201 internal_error (__FILE__, __LINE__,
1202 _("packet_ok: attempt to use a disabled packet"));
1208 case PACKET_UNKNOWN:
1209 /* The stub does not support the packet. */
1210 switch (config->support)
1213 if (config->detect == AUTO_BOOLEAN_AUTO)
1214 /* If the stub previously indicated that the packet was
1215 supported then there is a protocol error.. */
1216 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1217 config->name, config->title);
1219 /* The user set it wrong. */
1220 error (_("Enabled packet %s (%s) not recognized by stub"),
1221 config->name, config->title);
1223 case PACKET_SUPPORT_UNKNOWN:
1225 fprintf_unfiltered (gdb_stdlog,
1226 "Packet %s (%s) is NOT supported\n",
1227 config->name, config->title);
1228 config->support = PACKET_DISABLE;
1230 case PACKET_DISABLE:
1252 PACKET_vFile_pwrite,
1254 PACKET_vFile_unlink,
1255 PACKET_vFile_readlink,
1257 PACKET_qXfer_features,
1258 PACKET_qXfer_libraries,
1259 PACKET_qXfer_libraries_svr4,
1260 PACKET_qXfer_memory_map,
1261 PACKET_qXfer_spu_read,
1262 PACKET_qXfer_spu_write,
1263 PACKET_qXfer_osdata,
1264 PACKET_qXfer_threads,
1265 PACKET_qXfer_statictrace_read,
1266 PACKET_qXfer_traceframe_info,
1271 PACKET_QPassSignals,
1272 PACKET_QProgramSignals,
1273 PACKET_qSearch_memory,
1276 PACKET_QStartNoAckMode,
1278 PACKET_qXfer_siginfo_read,
1279 PACKET_qXfer_siginfo_write,
1281 PACKET_ConditionalTracepoints,
1282 PACKET_ConditionalBreakpoints,
1283 PACKET_BreakpointCommands,
1284 PACKET_FastTracepoints,
1285 PACKET_StaticTracepoints,
1286 PACKET_InstallInTrace,
1289 PACKET_TracepointSource,
1292 PACKET_QDisableRandomization,
1297 static struct packet_config remote_protocol_packets[PACKET_MAX];
1300 set_remote_protocol_packet_cmd (char *args, int from_tty,
1301 struct cmd_list_element *c)
1303 struct packet_config *packet;
1305 for (packet = remote_protocol_packets;
1306 packet < &remote_protocol_packets[PACKET_MAX];
1309 if (&packet->detect == c->var)
1311 update_packet_config (packet);
1315 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1320 show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1321 struct cmd_list_element *c,
1324 struct packet_config *packet;
1326 for (packet = remote_protocol_packets;
1327 packet < &remote_protocol_packets[PACKET_MAX];
1330 if (&packet->detect == c->var)
1332 show_packet_config_cmd (packet);
1336 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
1340 /* Should we try one of the 'Z' requests? */
1344 Z_PACKET_SOFTWARE_BP,
1345 Z_PACKET_HARDWARE_BP,
1352 /* For compatibility with older distributions. Provide a ``set remote
1353 Z-packet ...'' command that updates all the Z packet types. */
1355 static enum auto_boolean remote_Z_packet_detect;
1358 set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1359 struct cmd_list_element *c)
1363 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1365 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
1366 update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
1371 show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1372 struct cmd_list_element *c,
1377 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1379 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
1383 /* Should we try the 'ThreadInfo' query packet?
1385 This variable (NOT available to the user: auto-detect only!)
1386 determines whether GDB will use the new, simpler "ThreadInfo"
1387 query or the older, more complex syntax for thread queries.
1388 This is an auto-detect variable (set to true at each connect,
1389 and set to false when the target fails to recognize it). */
1391 static int use_threadinfo_query;
1392 static int use_threadextra_query;
1394 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1395 static struct async_signal_handler *sigint_remote_twice_token;
1396 static struct async_signal_handler *sigint_remote_token;
1399 /* Asynchronous signal handle registered as event loop source for
1400 when we have pending events ready to be passed to the core. */
1402 static struct async_event_handler *remote_async_inferior_event_token;
1404 /* Asynchronous signal handle registered as event loop source for when
1405 the remote sent us a %Stop notification. The registered callback
1406 will do a vStopped sequence to pull the rest of the events out of
1407 the remote side into our event queue. */
1409 static struct async_event_handler *remote_async_get_pending_events_token;
1412 static ptid_t magic_null_ptid;
1413 static ptid_t not_sent_ptid;
1414 static ptid_t any_thread_ptid;
1416 /* These are the threads which we last sent to the remote system. The
1417 TID member will be -1 for all or -2 for not sent yet. */
1419 static ptid_t general_thread;
1420 static ptid_t continue_thread;
1422 /* This the traceframe which we last selected on the remote system.
1423 It will be -1 if no traceframe is selected. */
1424 static int remote_traceframe_number = -1;
1426 /* Find out if the stub attached to PID (and hence GDB should offer to
1427 detach instead of killing it when bailing out). */
1430 remote_query_attached (int pid)
1432 struct remote_state *rs = get_remote_state ();
1433 size_t size = get_remote_packet_size ();
1435 if (remote_protocol_packets[PACKET_qAttached].support == PACKET_DISABLE)
1438 if (remote_multi_process_p (rs))
1439 xsnprintf (rs->buf, size, "qAttached:%x", pid);
1441 xsnprintf (rs->buf, size, "qAttached");
1444 getpkt (&rs->buf, &rs->buf_size, 0);
1446 switch (packet_ok (rs->buf,
1447 &remote_protocol_packets[PACKET_qAttached]))
1450 if (strcmp (rs->buf, "1") == 0)
1454 warning (_("Remote failure reply: %s"), rs->buf);
1456 case PACKET_UNKNOWN:
1463 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1464 has been invented by GDB, instead of reported by the target. Since
1465 we can be connected to a remote system before before knowing about
1466 any inferior, mark the target with execution when we find the first
1467 inferior. If ATTACHED is 1, then we had just attached to this
1468 inferior. If it is 0, then we just created this inferior. If it
1469 is -1, then try querying the remote stub to find out if it had
1470 attached to the inferior or not. */
1472 static struct inferior *
1473 remote_add_inferior (int fake_pid_p, int pid, int attached)
1475 struct inferior *inf;
1477 /* Check whether this process we're learning about is to be
1478 considered attached, or if is to be considered to have been
1479 spawned by the stub. */
1481 attached = remote_query_attached (pid);
1483 if (gdbarch_has_global_solist (target_gdbarch))
1485 /* If the target shares code across all inferiors, then every
1486 attach adds a new inferior. */
1487 inf = add_inferior (pid);
1489 /* ... and every inferior is bound to the same program space.
1490 However, each inferior may still have its own address
1492 inf->aspace = maybe_new_address_space ();
1493 inf->pspace = current_program_space;
1497 /* In the traditional debugging scenario, there's a 1-1 match
1498 between program/address spaces. We simply bind the inferior
1499 to the program space's address space. */
1500 inf = current_inferior ();
1501 inferior_appeared (inf, pid);
1504 inf->attach_flag = attached;
1505 inf->fake_pid_p = fake_pid_p;
1510 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1511 according to RUNNING. */
1514 remote_add_thread (ptid_t ptid, int running)
1518 set_executing (ptid, running);
1519 set_running (ptid, running);
1522 /* Come here when we learn about a thread id from the remote target.
1523 It may be the first time we hear about such thread, so take the
1524 opportunity to add it to GDB's thread list. In case this is the
1525 first time we're noticing its corresponding inferior, add it to
1526 GDB's inferior list as well. */
1529 remote_notice_new_inferior (ptid_t currthread, int running)
1531 /* If this is a new thread, add it to GDB's thread list.
1532 If we leave it up to WFI to do this, bad things will happen. */
1534 if (in_thread_list (currthread) && is_exited (currthread))
1536 /* We're seeing an event on a thread id we knew had exited.
1537 This has to be a new thread reusing the old id. Add it. */
1538 remote_add_thread (currthread, running);
1542 if (!in_thread_list (currthread))
1544 struct inferior *inf = NULL;
1545 int pid = ptid_get_pid (currthread);
1547 if (ptid_is_pid (inferior_ptid)
1548 && pid == ptid_get_pid (inferior_ptid))
1550 /* inferior_ptid has no thread member yet. This can happen
1551 with the vAttach -> remote_wait,"TAAthread:" path if the
1552 stub doesn't support qC. This is the first stop reported
1553 after an attach, so this is the main thread. Update the
1554 ptid in the thread list. */
1555 if (in_thread_list (pid_to_ptid (pid)))
1556 thread_change_ptid (inferior_ptid, currthread);
1559 remote_add_thread (currthread, running);
1560 inferior_ptid = currthread;
1565 if (ptid_equal (magic_null_ptid, inferior_ptid))
1567 /* inferior_ptid is not set yet. This can happen with the
1568 vRun -> remote_wait,"TAAthread:" path if the stub
1569 doesn't support qC. This is the first stop reported
1570 after an attach, so this is the main thread. Update the
1571 ptid in the thread list. */
1572 thread_change_ptid (inferior_ptid, currthread);
1576 /* When connecting to a target remote, or to a target
1577 extended-remote which already was debugging an inferior, we
1578 may not know about it yet. Add it before adding its child
1579 thread, so notifications are emitted in a sensible order. */
1580 if (!in_inferior_list (ptid_get_pid (currthread)))
1582 struct remote_state *rs = get_remote_state ();
1583 int fake_pid_p = !remote_multi_process_p (rs);
1585 inf = remote_add_inferior (fake_pid_p,
1586 ptid_get_pid (currthread), -1);
1589 /* This is really a new thread. Add it. */
1590 remote_add_thread (currthread, running);
1592 /* If we found a new inferior, let the common code do whatever
1593 it needs to with it (e.g., read shared libraries, insert
1596 notice_new_inferior (currthread, running, 0);
1600 /* Return the private thread data, creating it if necessary. */
1602 static struct private_thread_info *
1603 demand_private_info (ptid_t ptid)
1605 struct thread_info *info = find_thread_ptid (ptid);
1611 info->private = xmalloc (sizeof (*(info->private)));
1612 info->private_dtor = free_private_thread_info;
1613 info->private->core = -1;
1614 info->private->extra = 0;
1617 return info->private;
1620 /* Call this function as a result of
1621 1) A halt indication (T packet) containing a thread id
1622 2) A direct query of currthread
1623 3) Successful execution of set thread */
1626 record_currthread (ptid_t currthread)
1628 general_thread = currthread;
1631 static char *last_pass_packet;
1633 /* If 'QPassSignals' is supported, tell the remote stub what signals
1634 it can simply pass through to the inferior without reporting. */
1637 remote_pass_signals (int numsigs, unsigned char *pass_signals)
1639 if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE)
1641 char *pass_packet, *p;
1644 gdb_assert (numsigs < 256);
1645 for (i = 0; i < numsigs; i++)
1647 if (pass_signals[i])
1650 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1651 strcpy (pass_packet, "QPassSignals:");
1652 p = pass_packet + strlen (pass_packet);
1653 for (i = 0; i < numsigs; i++)
1655 if (pass_signals[i])
1658 *p++ = tohex (i >> 4);
1659 *p++ = tohex (i & 15);
1668 if (!last_pass_packet || strcmp (last_pass_packet, pass_packet))
1670 struct remote_state *rs = get_remote_state ();
1671 char *buf = rs->buf;
1673 putpkt (pass_packet);
1674 getpkt (&rs->buf, &rs->buf_size, 0);
1675 packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]);
1676 if (last_pass_packet)
1677 xfree (last_pass_packet);
1678 last_pass_packet = pass_packet;
1681 xfree (pass_packet);
1685 /* The last QProgramSignals packet sent to the target. We bypass
1686 sending a new program signals list down to the target if the new
1687 packet is exactly the same as the last we sent. IOW, we only let
1688 the target know about program signals list changes. */
1690 static char *last_program_signals_packet;
1692 /* If 'QProgramSignals' is supported, tell the remote stub what
1693 signals it should pass through to the inferior when detaching. */
1696 remote_program_signals (int numsigs, unsigned char *signals)
1698 if (remote_protocol_packets[PACKET_QProgramSignals].support != PACKET_DISABLE)
1703 gdb_assert (numsigs < 256);
1704 for (i = 0; i < numsigs; i++)
1709 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1710 strcpy (packet, "QProgramSignals:");
1711 p = packet + strlen (packet);
1712 for (i = 0; i < numsigs; i++)
1714 if (signal_pass_state (i))
1717 *p++ = tohex (i >> 4);
1718 *p++ = tohex (i & 15);
1727 if (!last_program_signals_packet
1728 || strcmp (last_program_signals_packet, packet) != 0)
1730 struct remote_state *rs = get_remote_state ();
1731 char *buf = rs->buf;
1734 getpkt (&rs->buf, &rs->buf_size, 0);
1735 packet_ok (buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1736 xfree (last_program_signals_packet);
1737 last_program_signals_packet = packet;
1744 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1745 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1746 thread. If GEN is set, set the general thread, if not, then set
1747 the step/continue thread. */
1749 set_thread (struct ptid ptid, int gen)
1751 struct remote_state *rs = get_remote_state ();
1752 ptid_t state = gen ? general_thread : continue_thread;
1753 char *buf = rs->buf;
1754 char *endbuf = rs->buf + get_remote_packet_size ();
1756 if (ptid_equal (state, ptid))
1760 *buf++ = gen ? 'g' : 'c';
1761 if (ptid_equal (ptid, magic_null_ptid))
1762 xsnprintf (buf, endbuf - buf, "0");
1763 else if (ptid_equal (ptid, any_thread_ptid))
1764 xsnprintf (buf, endbuf - buf, "0");
1765 else if (ptid_equal (ptid, minus_one_ptid))
1766 xsnprintf (buf, endbuf - buf, "-1");
1768 write_ptid (buf, endbuf, ptid);
1770 getpkt (&rs->buf, &rs->buf_size, 0);
1772 general_thread = ptid;
1774 continue_thread = ptid;
1778 set_general_thread (struct ptid ptid)
1780 set_thread (ptid, 1);
1784 set_continue_thread (struct ptid ptid)
1786 set_thread (ptid, 0);
1789 /* Change the remote current process. Which thread within the process
1790 ends up selected isn't important, as long as it is the same process
1791 as what INFERIOR_PTID points to.
1793 This comes from that fact that there is no explicit notion of
1794 "selected process" in the protocol. The selected process for
1795 general operations is the process the selected general thread
1799 set_general_process (void)
1801 struct remote_state *rs = get_remote_state ();
1803 /* If the remote can't handle multiple processes, don't bother. */
1804 if (!rs->extended || !remote_multi_process_p (rs))
1807 /* We only need to change the remote current thread if it's pointing
1808 at some other process. */
1809 if (ptid_get_pid (general_thread) != ptid_get_pid (inferior_ptid))
1810 set_general_thread (inferior_ptid);
1814 /* Return nonzero if the thread PTID is still alive on the remote
1818 remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1820 struct remote_state *rs = get_remote_state ();
1823 if (ptid_equal (ptid, magic_null_ptid))
1824 /* The main thread is always alive. */
1827 if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0)
1828 /* The main thread is always alive. This can happen after a
1829 vAttach, if the remote side doesn't support
1834 endp = rs->buf + get_remote_packet_size ();
1837 write_ptid (p, endp, ptid);
1840 getpkt (&rs->buf, &rs->buf_size, 0);
1841 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
1844 /* About these extended threadlist and threadinfo packets. They are
1845 variable length packets but, the fields within them are often fixed
1846 length. They are redundent enough to send over UDP as is the
1847 remote protocol in general. There is a matching unit test module
1850 #define OPAQUETHREADBYTES 8
1852 /* a 64 bit opaque identifier */
1853 typedef unsigned char threadref[OPAQUETHREADBYTES];
1855 /* WARNING: This threadref data structure comes from the remote O.S.,
1856 libstub protocol encoding, and remote.c. It is not particularly
1859 /* Right now, the internal structure is int. We want it to be bigger.
1860 Plan to fix this. */
1862 typedef int gdb_threadref; /* Internal GDB thread reference. */
1864 /* gdb_ext_thread_info is an internal GDB data structure which is
1865 equivalent to the reply of the remote threadinfo packet. */
1867 struct gdb_ext_thread_info
1869 threadref threadid; /* External form of thread reference. */
1870 int active; /* Has state interesting to GDB?
1872 char display[256]; /* Brief state display, name,
1873 blocked/suspended. */
1874 char shortname[32]; /* To be used to name threads. */
1875 char more_display[256]; /* Long info, statistics, queue depth,
1879 /* The volume of remote transfers can be limited by submitting
1880 a mask containing bits specifying the desired information.
1881 Use a union of these values as the 'selection' parameter to
1882 get_thread_info. FIXME: Make these TAG names more thread specific. */
1884 #define TAG_THREADID 1
1885 #define TAG_EXISTS 2
1886 #define TAG_DISPLAY 4
1887 #define TAG_THREADNAME 8
1888 #define TAG_MOREDISPLAY 16
1890 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1892 char *unpack_varlen_hex (char *buff, ULONGEST *result);
1894 static char *unpack_nibble (char *buf, int *val);
1896 static char *pack_nibble (char *buf, int nibble);
1898 static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
1900 static char *unpack_byte (char *buf, int *value);
1902 static char *pack_int (char *buf, int value);
1904 static char *unpack_int (char *buf, int *value);
1906 static char *unpack_string (char *src, char *dest, int length);
1908 static char *pack_threadid (char *pkt, threadref *id);
1910 static char *unpack_threadid (char *inbuf, threadref *id);
1912 void int_to_threadref (threadref *id, int value);
1914 static int threadref_to_int (threadref *ref);
1916 static void copy_threadref (threadref *dest, threadref *src);
1918 static int threadmatch (threadref *dest, threadref *src);
1920 static char *pack_threadinfo_request (char *pkt, int mode,
1923 static int remote_unpack_thread_info_response (char *pkt,
1924 threadref *expectedref,
1925 struct gdb_ext_thread_info
1929 static int remote_get_threadinfo (threadref *threadid,
1930 int fieldset, /*TAG mask */
1931 struct gdb_ext_thread_info *info);
1933 static char *pack_threadlist_request (char *pkt, int startflag,
1935 threadref *nextthread);
1937 static int parse_threadlist_response (char *pkt,
1939 threadref *original_echo,
1940 threadref *resultlist,
1943 static int remote_get_threadlist (int startflag,
1944 threadref *nextthread,
1948 threadref *threadlist);
1950 typedef int (*rmt_thread_action) (threadref *ref, void *context);
1952 static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1953 void *context, int looplimit);
1955 static int remote_newthread_step (threadref *ref, void *context);
1958 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1959 buffer we're allowed to write to. Returns
1960 BUF+CHARACTERS_WRITTEN. */
1963 write_ptid (char *buf, const char *endbuf, ptid_t ptid)
1966 struct remote_state *rs = get_remote_state ();
1968 if (remote_multi_process_p (rs))
1970 pid = ptid_get_pid (ptid);
1972 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
1974 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
1976 tid = ptid_get_tid (ptid);
1978 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
1980 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
1985 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1986 passed the last parsed char. Returns null_ptid on error. */
1989 read_ptid (char *buf, char **obuf)
1993 ULONGEST pid = 0, tid = 0;
1997 /* Multi-process ptid. */
1998 pp = unpack_varlen_hex (p + 1, &pid);
2000 error (_("invalid remote ptid: %s"), p);
2003 pp = unpack_varlen_hex (p + 1, &tid);
2006 return ptid_build (pid, 0, tid);
2009 /* No multi-process. Just a tid. */
2010 pp = unpack_varlen_hex (p, &tid);
2012 /* Since the stub is not sending a process id, then default to
2013 what's in inferior_ptid, unless it's null at this point. If so,
2014 then since there's no way to know the pid of the reported
2015 threads, use the magic number. */
2016 if (ptid_equal (inferior_ptid, null_ptid))
2017 pid = ptid_get_pid (magic_null_ptid);
2019 pid = ptid_get_pid (inferior_ptid);
2023 return ptid_build (pid, 0, tid);
2026 /* Encode 64 bits in 16 chars of hex. */
2028 static const char hexchars[] = "0123456789abcdef";
2031 ishex (int ch, int *val)
2033 if ((ch >= 'a') && (ch <= 'f'))
2035 *val = ch - 'a' + 10;
2038 if ((ch >= 'A') && (ch <= 'F'))
2040 *val = ch - 'A' + 10;
2043 if ((ch >= '0') && (ch <= '9'))
2054 if (ch >= 'a' && ch <= 'f')
2055 return ch - 'a' + 10;
2056 if (ch >= '0' && ch <= '9')
2058 if (ch >= 'A' && ch <= 'F')
2059 return ch - 'A' + 10;
2064 stub_unpack_int (char *buff, int fieldlength)
2071 nibble = stubhex (*buff++);
2075 retval = retval << 4;
2081 unpack_varlen_hex (char *buff, /* packet to parse */
2085 ULONGEST retval = 0;
2087 while (ishex (*buff, &nibble))
2090 retval = retval << 4;
2091 retval |= nibble & 0x0f;
2098 unpack_nibble (char *buf, int *val)
2100 *val = fromhex (*buf++);
2105 pack_nibble (char *buf, int nibble)
2107 *buf++ = hexchars[(nibble & 0x0f)];
2112 pack_hex_byte (char *pkt, int byte)
2114 *pkt++ = hexchars[(byte >> 4) & 0xf];
2115 *pkt++ = hexchars[(byte & 0xf)];
2120 unpack_byte (char *buf, int *value)
2122 *value = stub_unpack_int (buf, 2);
2127 pack_int (char *buf, int value)
2129 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2130 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2131 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2132 buf = pack_hex_byte (buf, (value & 0xff));
2137 unpack_int (char *buf, int *value)
2139 *value = stub_unpack_int (buf, 8);
2143 #if 0 /* Currently unused, uncomment when needed. */
2144 static char *pack_string (char *pkt, char *string);
2147 pack_string (char *pkt, char *string)
2152 len = strlen (string);
2154 len = 200; /* Bigger than most GDB packets, junk??? */
2155 pkt = pack_hex_byte (pkt, len);
2159 if ((ch == '\0') || (ch == '#'))
2160 ch = '*'; /* Protect encapsulation. */
2165 #endif /* 0 (unused) */
2168 unpack_string (char *src, char *dest, int length)
2177 pack_threadid (char *pkt, threadref *id)
2180 unsigned char *altid;
2182 altid = (unsigned char *) id;
2183 limit = pkt + BUF_THREAD_ID_SIZE;
2185 pkt = pack_hex_byte (pkt, *altid++);
2191 unpack_threadid (char *inbuf, threadref *id)
2194 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2197 altref = (char *) id;
2199 while (inbuf < limit)
2201 x = stubhex (*inbuf++);
2202 y = stubhex (*inbuf++);
2203 *altref++ = (x << 4) | y;
2208 /* Externally, threadrefs are 64 bits but internally, they are still
2209 ints. This is due to a mismatch of specifications. We would like
2210 to use 64bit thread references internally. This is an adapter
2214 int_to_threadref (threadref *id, int value)
2216 unsigned char *scan;
2218 scan = (unsigned char *) id;
2224 *scan++ = (value >> 24) & 0xff;
2225 *scan++ = (value >> 16) & 0xff;
2226 *scan++ = (value >> 8) & 0xff;
2227 *scan++ = (value & 0xff);
2231 threadref_to_int (threadref *ref)
2234 unsigned char *scan;
2240 value = (value << 8) | ((*scan++) & 0xff);
2245 copy_threadref (threadref *dest, threadref *src)
2248 unsigned char *csrc, *cdest;
2250 csrc = (unsigned char *) src;
2251 cdest = (unsigned char *) dest;
2258 threadmatch (threadref *dest, threadref *src)
2260 /* Things are broken right now, so just assume we got a match. */
2262 unsigned char *srcp, *destp;
2264 srcp = (char *) src;
2265 destp = (char *) dest;
2269 result &= (*srcp++ == *destp++) ? 1 : 0;
2276 threadid:1, # always request threadid
2283 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2286 pack_threadinfo_request (char *pkt, int mode, threadref *id)
2288 *pkt++ = 'q'; /* Info Query */
2289 *pkt++ = 'P'; /* process or thread info */
2290 pkt = pack_int (pkt, mode); /* mode */
2291 pkt = pack_threadid (pkt, id); /* threadid */
2292 *pkt = '\0'; /* terminate */
2296 /* These values tag the fields in a thread info response packet. */
2297 /* Tagging the fields allows us to request specific fields and to
2298 add more fields as time goes by. */
2300 #define TAG_THREADID 1 /* Echo the thread identifier. */
2301 #define TAG_EXISTS 2 /* Is this process defined enough to
2302 fetch registers and its stack? */
2303 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2304 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2305 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2309 remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2310 struct gdb_ext_thread_info *info)
2312 struct remote_state *rs = get_remote_state ();
2316 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
2319 /* info->threadid = 0; FIXME: implement zero_threadref. */
2321 info->display[0] = '\0';
2322 info->shortname[0] = '\0';
2323 info->more_display[0] = '\0';
2325 /* Assume the characters indicating the packet type have been
2327 pkt = unpack_int (pkt, &mask); /* arg mask */
2328 pkt = unpack_threadid (pkt, &ref);
2331 warning (_("Incomplete response to threadinfo request."));
2332 if (!threadmatch (&ref, expectedref))
2333 { /* This is an answer to a different request. */
2334 warning (_("ERROR RMT Thread info mismatch."));
2337 copy_threadref (&info->threadid, &ref);
2339 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2341 /* Packets are terminated with nulls. */
2342 while ((pkt < limit) && mask && *pkt)
2344 pkt = unpack_int (pkt, &tag); /* tag */
2345 pkt = unpack_byte (pkt, &length); /* length */
2346 if (!(tag & mask)) /* Tags out of synch with mask. */
2348 warning (_("ERROR RMT: threadinfo tag mismatch."));
2352 if (tag == TAG_THREADID)
2356 warning (_("ERROR RMT: length of threadid is not 16."));
2360 pkt = unpack_threadid (pkt, &ref);
2361 mask = mask & ~TAG_THREADID;
2364 if (tag == TAG_EXISTS)
2366 info->active = stub_unpack_int (pkt, length);
2368 mask = mask & ~(TAG_EXISTS);
2371 warning (_("ERROR RMT: 'exists' length too long."));
2377 if (tag == TAG_THREADNAME)
2379 pkt = unpack_string (pkt, &info->shortname[0], length);
2380 mask = mask & ~TAG_THREADNAME;
2383 if (tag == TAG_DISPLAY)
2385 pkt = unpack_string (pkt, &info->display[0], length);
2386 mask = mask & ~TAG_DISPLAY;
2389 if (tag == TAG_MOREDISPLAY)
2391 pkt = unpack_string (pkt, &info->more_display[0], length);
2392 mask = mask & ~TAG_MOREDISPLAY;
2395 warning (_("ERROR RMT: unknown thread info tag."));
2396 break; /* Not a tag we know about. */
2402 remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2403 struct gdb_ext_thread_info *info)
2405 struct remote_state *rs = get_remote_state ();
2408 pack_threadinfo_request (rs->buf, fieldset, threadid);
2410 getpkt (&rs->buf, &rs->buf_size, 0);
2412 if (rs->buf[0] == '\0')
2415 result = remote_unpack_thread_info_response (rs->buf + 2,
2420 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2423 pack_threadlist_request (char *pkt, int startflag, int threadcount,
2424 threadref *nextthread)
2426 *pkt++ = 'q'; /* info query packet */
2427 *pkt++ = 'L'; /* Process LIST or threadLIST request */
2428 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
2429 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2430 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2435 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2438 parse_threadlist_response (char *pkt, int result_limit,
2439 threadref *original_echo, threadref *resultlist,
2442 struct remote_state *rs = get_remote_state ();
2444 int count, resultcount, done;
2447 /* Assume the 'q' and 'M chars have been stripped. */
2448 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
2449 /* done parse past here */
2450 pkt = unpack_byte (pkt, &count); /* count field */
2451 pkt = unpack_nibble (pkt, &done);
2452 /* The first threadid is the argument threadid. */
2453 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2454 while ((count-- > 0) && (pkt < limit))
2456 pkt = unpack_threadid (pkt, resultlist++);
2457 if (resultcount++ >= result_limit)
2466 remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2467 int *done, int *result_count, threadref *threadlist)
2469 struct remote_state *rs = get_remote_state ();
2470 static threadref echo_nextthread;
2473 /* Trancate result limit to be smaller than the packet size. */
2474 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2475 >= get_remote_packet_size ())
2476 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
2478 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2480 getpkt (&rs->buf, &rs->buf_size, 0);
2482 if (*rs->buf == '\0')
2486 parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
2489 if (!threadmatch (&echo_nextthread, nextthread))
2491 /* FIXME: This is a good reason to drop the packet. */
2492 /* Possably, there is a duplicate response. */
2494 retransmit immediatly - race conditions
2495 retransmit after timeout - yes
2497 wait for packet, then exit
2499 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2500 return 0; /* I choose simply exiting. */
2502 if (*result_count <= 0)
2506 warning (_("RMT ERROR : failed to get remote thread list."));
2509 return result; /* break; */
2511 if (*result_count > result_limit)
2514 warning (_("RMT ERROR: threadlist response longer than requested."));
2520 /* This is the interface between remote and threads, remotes upper
2523 /* remote_find_new_threads retrieves the thread list and for each
2524 thread in the list, looks up the thread in GDB's internal list,
2525 adding the thread if it does not already exist. This involves
2526 getting partial thread lists from the remote target so, polling the
2527 quit_flag is required. */
2530 /* About this many threadisds fit in a packet. */
2532 #define MAXTHREADLISTRESULTS 32
2535 remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2538 int done, i, result_count;
2542 static threadref nextthread;
2543 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
2548 if (loopcount++ > looplimit)
2551 warning (_("Remote fetch threadlist -infinite loop-."));
2554 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
2555 &done, &result_count, resultthreadlist))
2560 /* Clear for later iterations. */
2562 /* Setup to resume next batch of thread references, set nextthread. */
2563 if (result_count >= 1)
2564 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
2566 while (result_count--)
2567 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
2574 remote_newthread_step (threadref *ref, void *context)
2576 int pid = ptid_get_pid (inferior_ptid);
2577 ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref));
2579 if (!in_thread_list (ptid))
2581 return 1; /* continue iterator */
2584 #define CRAZY_MAX_THREADS 1000
2587 remote_current_thread (ptid_t oldpid)
2589 struct remote_state *rs = get_remote_state ();
2592 getpkt (&rs->buf, &rs->buf_size, 0);
2593 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
2594 return read_ptid (&rs->buf[2], NULL);
2599 /* Find new threads for info threads command.
2600 * Original version, using John Metzler's thread protocol.
2604 remote_find_new_threads (void)
2606 remote_threadlist_iterator (remote_newthread_step, 0,
2610 #if defined(HAVE_LIBEXPAT)
2612 typedef struct thread_item
2618 DEF_VEC_O(thread_item_t);
2620 struct threads_parsing_context
2622 VEC (thread_item_t) *items;
2626 start_thread (struct gdb_xml_parser *parser,
2627 const struct gdb_xml_element *element,
2628 void *user_data, VEC(gdb_xml_value_s) *attributes)
2630 struct threads_parsing_context *data = user_data;
2632 struct thread_item item;
2634 struct gdb_xml_value *attr;
2636 id = xml_find_attribute (attributes, "id")->value;
2637 item.ptid = read_ptid (id, NULL);
2639 attr = xml_find_attribute (attributes, "core");
2641 item.core = *(ULONGEST *) attr->value;
2647 VEC_safe_push (thread_item_t, data->items, &item);
2651 end_thread (struct gdb_xml_parser *parser,
2652 const struct gdb_xml_element *element,
2653 void *user_data, const char *body_text)
2655 struct threads_parsing_context *data = user_data;
2657 if (body_text && *body_text)
2658 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2661 const struct gdb_xml_attribute thread_attributes[] = {
2662 { "id", GDB_XML_AF_NONE, NULL, NULL },
2663 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2664 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2667 const struct gdb_xml_element thread_children[] = {
2668 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2671 const struct gdb_xml_element threads_children[] = {
2672 { "thread", thread_attributes, thread_children,
2673 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2674 start_thread, end_thread },
2675 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2678 const struct gdb_xml_element threads_elements[] = {
2679 { "threads", NULL, threads_children,
2680 GDB_XML_EF_NONE, NULL, NULL },
2681 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2684 /* Discard the contents of the constructed thread info context. */
2687 clear_threads_parsing_context (void *p)
2689 struct threads_parsing_context *context = p;
2691 struct thread_item *item;
2693 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2694 xfree (item->extra);
2696 VEC_free (thread_item_t, context->items);
2702 * Find all threads for info threads command.
2703 * Uses new thread protocol contributed by Cisco.
2704 * Falls back and attempts to use the older method (above)
2705 * if the target doesn't respond to the new method.
2709 remote_threads_info (struct target_ops *ops)
2711 struct remote_state *rs = get_remote_state ();
2715 if (remote_desc == 0) /* paranoia */
2716 error (_("Command can only be used when connected to the remote target."));
2718 #if defined(HAVE_LIBEXPAT)
2719 if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2721 char *xml = target_read_stralloc (¤t_target,
2722 TARGET_OBJECT_THREADS, NULL);
2724 struct cleanup *back_to = make_cleanup (xfree, xml);
2728 struct threads_parsing_context context;
2730 context.items = NULL;
2731 make_cleanup (clear_threads_parsing_context, &context);
2733 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2734 threads_elements, xml, &context) == 0)
2737 struct thread_item *item;
2740 VEC_iterate (thread_item_t, context.items, i, item);
2743 if (!ptid_equal (item->ptid, null_ptid))
2745 struct private_thread_info *info;
2746 /* In non-stop mode, we assume new found threads
2747 are running until proven otherwise with a
2748 stop reply. In all-stop, we can only get
2749 here if all threads are stopped. */
2750 int running = non_stop ? 1 : 0;
2752 remote_notice_new_inferior (item->ptid, running);
2754 info = demand_private_info (item->ptid);
2755 info->core = item->core;
2756 info->extra = item->extra;
2763 do_cleanups (back_to);
2768 if (use_threadinfo_query)
2770 putpkt ("qfThreadInfo");
2771 getpkt (&rs->buf, &rs->buf_size, 0);
2773 if (bufp[0] != '\0') /* q packet recognized */
2775 while (*bufp++ == 'm') /* reply contains one or more TID */
2779 new_thread = read_ptid (bufp, &bufp);
2780 if (!ptid_equal (new_thread, null_ptid))
2782 /* In non-stop mode, we assume new found threads
2783 are running until proven otherwise with a
2784 stop reply. In all-stop, we can only get
2785 here if all threads are stopped. */
2786 int running = non_stop ? 1 : 0;
2788 remote_notice_new_inferior (new_thread, running);
2791 while (*bufp++ == ','); /* comma-separated list */
2792 putpkt ("qsThreadInfo");
2793 getpkt (&rs->buf, &rs->buf_size, 0);
2800 /* Only qfThreadInfo is supported in non-stop mode. */
2804 /* Else fall back to old method based on jmetzler protocol. */
2805 use_threadinfo_query = 0;
2806 remote_find_new_threads ();
2811 * Collect a descriptive string about the given thread.
2812 * The target may say anything it wants to about the thread
2813 * (typically info about its blocked / runnable state, name, etc.).
2814 * This string will appear in the info threads display.
2816 * Optional: targets are not required to implement this function.
2820 remote_threads_extra_info (struct thread_info *tp)
2822 struct remote_state *rs = get_remote_state ();
2826 struct gdb_ext_thread_info threadinfo;
2827 static char display_buf[100]; /* arbitrary... */
2828 int n = 0; /* position in display_buf */
2830 if (remote_desc == 0) /* paranoia */
2831 internal_error (__FILE__, __LINE__,
2832 _("remote_threads_extra_info"));
2834 if (ptid_equal (tp->ptid, magic_null_ptid)
2835 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0))
2836 /* This is the main thread which was added by GDB. The remote
2837 server doesn't know about it. */
2840 if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2842 struct thread_info *info = find_thread_ptid (tp->ptid);
2844 if (info && info->private)
2845 return info->private->extra;
2850 if (use_threadextra_query)
2853 char *endb = rs->buf + get_remote_packet_size ();
2855 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2857 write_ptid (b, endb, tp->ptid);
2860 getpkt (&rs->buf, &rs->buf_size, 0);
2861 if (rs->buf[0] != 0)
2863 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2864 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
2865 display_buf [result] = '\0';
2870 /* If the above query fails, fall back to the old method. */
2871 use_threadextra_query = 0;
2872 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2873 | TAG_MOREDISPLAY | TAG_DISPLAY;
2874 int_to_threadref (&id, ptid_get_tid (tp->ptid));
2875 if (remote_get_threadinfo (&id, set, &threadinfo))
2876 if (threadinfo.active)
2878 if (*threadinfo.shortname)
2879 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
2880 " Name: %s,", threadinfo.shortname);
2881 if (*threadinfo.display)
2882 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2883 " State: %s,", threadinfo.display);
2884 if (*threadinfo.more_display)
2885 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
2886 " Priority: %s", threadinfo.more_display);
2890 /* For purely cosmetic reasons, clear up trailing commas. */
2891 if (',' == display_buf[n-1])
2892 display_buf[n-1] = ' ';
2901 remote_static_tracepoint_marker_at (CORE_ADDR addr,
2902 struct static_tracepoint_marker *marker)
2904 struct remote_state *rs = get_remote_state ();
2907 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
2909 p += hexnumstr (p, addr);
2911 getpkt (&rs->buf, &rs->buf_size, 0);
2915 error (_("Remote failure reply: %s"), p);
2919 parse_static_tracepoint_marker_definition (p, &p, marker);
2926 static VEC(static_tracepoint_marker_p) *
2927 remote_static_tracepoint_markers_by_strid (const char *strid)
2929 struct remote_state *rs = get_remote_state ();
2930 VEC(static_tracepoint_marker_p) *markers = NULL;
2931 struct static_tracepoint_marker *marker = NULL;
2932 struct cleanup *old_chain;
2935 /* Ask for a first packet of static tracepoint marker
2938 getpkt (&rs->buf, &rs->buf_size, 0);
2941 error (_("Remote failure reply: %s"), p);
2943 old_chain = make_cleanup (free_current_marker, &marker);
2948 marker = XCNEW (struct static_tracepoint_marker);
2952 parse_static_tracepoint_marker_definition (p, &p, marker);
2954 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
2956 VEC_safe_push (static_tracepoint_marker_p,
2962 release_static_tracepoint_marker (marker);
2963 memset (marker, 0, sizeof (*marker));
2966 while (*p++ == ','); /* comma-separated list */
2967 /* Ask for another packet of static tracepoint definition. */
2969 getpkt (&rs->buf, &rs->buf_size, 0);
2973 do_cleanups (old_chain);
2978 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2981 remote_get_ada_task_ptid (long lwp, long thread)
2983 return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp);
2987 /* Restart the remote side; this is an extended protocol operation. */
2990 extended_remote_restart (void)
2992 struct remote_state *rs = get_remote_state ();
2994 /* Send the restart command; for reasons I don't understand the
2995 remote side really expects a number after the "R". */
2996 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
2999 remote_fileio_reset ();
3002 /* Clean up connection to a remote debugger. */
3005 remote_close (int quitting)
3007 if (remote_desc == NULL)
3008 return; /* already closed */
3010 /* Make sure we leave stdin registered in the event loop, and we
3011 don't leave the async SIGINT signal handler installed. */
3012 remote_terminal_ours ();
3014 serial_close (remote_desc);
3017 /* We don't have a connection to the remote stub anymore. Get rid
3018 of all the inferiors and their threads we were controlling.
3019 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3020 will be unable to find the thread corresponding to (pid, 0, 0). */
3021 inferior_ptid = null_ptid;
3022 discard_all_inferiors ();
3024 /* We're no longer interested in any of these events. */
3025 discard_pending_stop_replies (-1);
3027 if (remote_async_inferior_event_token)
3028 delete_async_event_handler (&remote_async_inferior_event_token);
3029 if (remote_async_get_pending_events_token)
3030 delete_async_event_handler (&remote_async_get_pending_events_token);
3033 /* Query the remote side for the text, data and bss offsets. */
3038 struct remote_state *rs = get_remote_state ();
3041 int lose, num_segments = 0, do_sections, do_segments;
3042 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
3043 struct section_offsets *offs;
3044 struct symfile_segment_data *data;
3046 if (symfile_objfile == NULL)
3049 putpkt ("qOffsets");
3050 getpkt (&rs->buf, &rs->buf_size, 0);
3053 if (buf[0] == '\000')
3054 return; /* Return silently. Stub doesn't support
3058 warning (_("Remote failure reply: %s"), buf);
3062 /* Pick up each field in turn. This used to be done with scanf, but
3063 scanf will make trouble if CORE_ADDR size doesn't match
3064 conversion directives correctly. The following code will work
3065 with any size of CORE_ADDR. */
3066 text_addr = data_addr = bss_addr = 0;
3070 if (strncmp (ptr, "Text=", 5) == 0)
3073 /* Don't use strtol, could lose on big values. */
3074 while (*ptr && *ptr != ';')
3075 text_addr = (text_addr << 4) + fromhex (*ptr++);
3077 if (strncmp (ptr, ";Data=", 6) == 0)
3080 while (*ptr && *ptr != ';')
3081 data_addr = (data_addr << 4) + fromhex (*ptr++);
3086 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
3089 while (*ptr && *ptr != ';')
3090 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
3092 if (bss_addr != data_addr)
3093 warning (_("Target reported unsupported offsets: %s"), buf);
3098 else if (strncmp (ptr, "TextSeg=", 8) == 0)
3101 /* Don't use strtol, could lose on big values. */
3102 while (*ptr && *ptr != ';')
3103 text_addr = (text_addr << 4) + fromhex (*ptr++);
3106 if (strncmp (ptr, ";DataSeg=", 9) == 0)
3109 while (*ptr && *ptr != ';')
3110 data_addr = (data_addr << 4) + fromhex (*ptr++);
3118 error (_("Malformed response to offset query, %s"), buf);
3119 else if (*ptr != '\0')
3120 warning (_("Target reported unsupported offsets: %s"), buf);
3122 offs = ((struct section_offsets *)
3123 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
3124 memcpy (offs, symfile_objfile->section_offsets,
3125 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
3127 data = get_symfile_segment_data (symfile_objfile->obfd);
3128 do_segments = (data != NULL);
3129 do_sections = num_segments == 0;
3131 if (num_segments > 0)
3133 segments[0] = text_addr;
3134 segments[1] = data_addr;
3136 /* If we have two segments, we can still try to relocate everything
3137 by assuming that the .text and .data offsets apply to the whole
3138 text and data segments. Convert the offsets given in the packet
3139 to base addresses for symfile_map_offsets_to_segments. */
3140 else if (data && data->num_segments == 2)
3142 segments[0] = data->segment_bases[0] + text_addr;
3143 segments[1] = data->segment_bases[1] + data_addr;
3146 /* If the object file has only one segment, assume that it is text
3147 rather than data; main programs with no writable data are rare,
3148 but programs with no code are useless. Of course the code might
3149 have ended up in the data segment... to detect that we would need
3150 the permissions here. */
3151 else if (data && data->num_segments == 1)
3153 segments[0] = data->segment_bases[0] + text_addr;
3156 /* There's no way to relocate by segment. */
3162 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3163 offs, num_segments, segments);
3165 if (ret == 0 && !do_sections)
3166 error (_("Can not handle qOffsets TextSeg "
3167 "response with this symbol file"));
3174 free_symfile_segment_data (data);
3178 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3180 /* This is a temporary kludge to force data and bss to use the
3181 same offsets because that's what nlmconv does now. The real
3182 solution requires changes to the stub and remote.c that I
3183 don't have time to do right now. */
3185 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3186 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3189 objfile_relocate (symfile_objfile, offs);
3192 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3193 threads we know are stopped already. This is used during the
3194 initial remote connection in non-stop mode --- threads that are
3195 reported as already being stopped are left stopped. */
3198 set_stop_requested_callback (struct thread_info *thread, void *data)
3200 /* If we have a stop reply for this thread, it must be stopped. */
3201 if (peek_stop_reply (thread->ptid))
3202 set_stop_requested (thread->ptid, 1);
3207 /* Send interrupt_sequence to remote target. */
3209 send_interrupt_sequence (void)
3211 if (interrupt_sequence_mode == interrupt_sequence_control_c)
3212 serial_write (remote_desc, "\x03", 1);
3213 else if (interrupt_sequence_mode == interrupt_sequence_break)
3214 serial_send_break (remote_desc);
3215 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3217 serial_send_break (remote_desc);
3218 serial_write (remote_desc, "g", 1);
3221 internal_error (__FILE__, __LINE__,
3222 _("Invalid value for interrupt_sequence_mode: %s."),
3223 interrupt_sequence_mode);
3226 /* Query the remote target for which is the current thread/process,
3227 add it to our tables, and update INFERIOR_PTID. The caller is
3228 responsible for setting the state such that the remote end is ready
3229 to return the current thread. */
3232 add_current_inferior_and_thread (void)
3234 struct remote_state *rs = get_remote_state ();
3238 inferior_ptid = null_ptid;
3240 /* Now, if we have thread information, update inferior_ptid. */
3241 ptid = remote_current_thread (inferior_ptid);
3242 if (!ptid_equal (ptid, null_ptid))
3244 if (!remote_multi_process_p (rs))
3247 inferior_ptid = ptid;
3251 /* Without this, some commands which require an active target
3252 (such as kill) won't work. This variable serves (at least)
3253 double duty as both the pid of the target process (if it has
3254 such), and as a flag indicating that a target is active. */
3255 inferior_ptid = magic_null_ptid;
3259 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1);
3261 /* Add the main thread. */
3262 add_thread_silent (inferior_ptid);
3266 remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
3268 struct remote_state *rs = get_remote_state ();
3269 struct packet_config *noack_config;
3270 char *wait_status = NULL;
3272 immediate_quit++; /* Allow user to interrupt it. */
3274 if (interrupt_on_connect)
3275 send_interrupt_sequence ();
3277 /* Ack any packet which the remote side has already sent. */
3278 serial_write (remote_desc, "+", 1);
3280 /* Signal other parts that we're going through the initial setup,
3281 and so things may not be stable yet. */
3282 rs->starting_up = 1;
3284 /* The first packet we send to the target is the optional "supported
3285 packets" request. If the target can answer this, it will tell us
3286 which later probes to skip. */
3287 remote_query_supported ();
3289 /* If the stub wants to get a QAllow, compose one and send it. */
3290 if (remote_protocol_packets[PACKET_QAllow].support != PACKET_DISABLE)
3291 remote_set_permissions ();
3293 /* Next, we possibly activate noack mode.
3295 If the QStartNoAckMode packet configuration is set to AUTO,
3296 enable noack mode if the stub reported a wish for it with
3299 If set to TRUE, then enable noack mode even if the stub didn't
3300 report it in qSupported. If the stub doesn't reply OK, the
3301 session ends with an error.
3303 If FALSE, then don't activate noack mode, regardless of what the
3304 stub claimed should be the default with qSupported. */
3306 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
3308 if (noack_config->detect == AUTO_BOOLEAN_TRUE
3309 || (noack_config->detect == AUTO_BOOLEAN_AUTO
3310 && noack_config->support == PACKET_ENABLE))
3312 putpkt ("QStartNoAckMode");
3313 getpkt (&rs->buf, &rs->buf_size, 0);
3314 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3320 /* Tell the remote that we are using the extended protocol. */
3322 getpkt (&rs->buf, &rs->buf_size, 0);
3325 /* Let the target know which signals it is allowed to pass down to
3327 update_signals_program_target ();
3329 /* Next, if the target can specify a description, read it. We do
3330 this before anything involving memory or registers. */
3331 target_find_description ();
3333 /* Next, now that we know something about the target, update the
3334 address spaces in the program spaces. */
3335 update_address_spaces ();
3337 /* On OSs where the list of libraries is global to all
3338 processes, we fetch them early. */
3339 if (gdbarch_has_global_solist (target_gdbarch))
3340 solib_add (NULL, from_tty, target, auto_solib_add);
3344 if (!rs->non_stop_aware)
3345 error (_("Non-stop mode requested, but remote "
3346 "does not support non-stop"));
3348 putpkt ("QNonStop:1");
3349 getpkt (&rs->buf, &rs->buf_size, 0);
3351 if (strcmp (rs->buf, "OK") != 0)
3352 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
3354 /* Find about threads and processes the stub is already
3355 controlling. We default to adding them in the running state.
3356 The '?' query below will then tell us about which threads are
3358 remote_threads_info (target);
3360 else if (rs->non_stop_aware)
3362 /* Don't assume that the stub can operate in all-stop mode.
3363 Request it explicitely. */
3364 putpkt ("QNonStop:0");
3365 getpkt (&rs->buf, &rs->buf_size, 0);
3367 if (strcmp (rs->buf, "OK") != 0)
3368 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
3371 /* Check whether the target is running now. */
3373 getpkt (&rs->buf, &rs->buf_size, 0);
3379 struct inferior *inf;
3381 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
3384 error (_("The target is not running (try extended-remote?)"));
3386 /* We're connected, but not running. Drop out before we
3387 call start_remote. */
3388 rs->starting_up = 0;
3393 /* Save the reply for later. */
3394 wait_status = alloca (strlen (rs->buf) + 1);
3395 strcpy (wait_status, rs->buf);
3398 /* Let the stub know that we want it to return the thread. */
3399 set_continue_thread (minus_one_ptid);
3401 add_current_inferior_and_thread ();
3403 /* init_wait_for_inferior should be called before get_offsets in order
3404 to manage `inserted' flag in bp loc in a correct state.
3405 breakpoint_init_inferior, called from init_wait_for_inferior, set
3406 `inserted' flag to 0, while before breakpoint_re_set, called from
3407 start_remote, set `inserted' flag to 1. In the initialization of
3408 inferior, breakpoint_init_inferior should be called first, and then
3409 breakpoint_re_set can be called. If this order is broken, state of
3410 `inserted' flag is wrong, and cause some problems on breakpoint
3412 init_wait_for_inferior ();
3414 get_offsets (); /* Get text, data & bss offsets. */
3416 /* If we could not find a description using qXfer, and we know
3417 how to do it some other way, try again. This is not
3418 supported for non-stop; it could be, but it is tricky if
3419 there are no stopped threads when we connect. */
3420 if (remote_read_description_p (target)
3421 && gdbarch_target_desc (target_gdbarch) == NULL)
3423 target_clear_description ();
3424 target_find_description ();
3427 /* Use the previously fetched status. */
3428 gdb_assert (wait_status != NULL);
3429 strcpy (rs->buf, wait_status);
3430 rs->cached_wait_status = 1;
3433 start_remote (from_tty); /* Initialize gdb process mechanisms. */
3437 /* Clear WFI global state. Do this before finding about new
3438 threads and inferiors, and setting the current inferior.
3439 Otherwise we would clear the proceed status of the current
3440 inferior when we want its stop_soon state to be preserved
3441 (see notice_new_inferior). */
3442 init_wait_for_inferior ();
3444 /* In non-stop, we will either get an "OK", meaning that there
3445 are no stopped threads at this time; or, a regular stop
3446 reply. In the latter case, there may be more than one thread
3447 stopped --- we pull them all out using the vStopped
3449 if (strcmp (rs->buf, "OK") != 0)
3451 struct stop_reply *stop_reply;
3452 struct cleanup *old_chain;
3454 stop_reply = stop_reply_xmalloc ();
3455 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
3457 remote_parse_stop_reply (rs->buf, stop_reply);
3458 discard_cleanups (old_chain);
3460 /* get_pending_stop_replies acks this one, and gets the rest
3462 pending_stop_reply = stop_reply;
3463 remote_get_pending_stop_replies ();
3465 /* Make sure that threads that were stopped remain
3467 iterate_over_threads (set_stop_requested_callback, NULL);
3470 if (target_can_async_p ())
3471 target_async (inferior_event_handler, 0);
3473 if (thread_count () == 0)
3476 error (_("The target is not running (try extended-remote?)"));
3478 /* We're connected, but not running. Drop out before we
3479 call start_remote. */
3480 rs->starting_up = 0;
3484 /* Let the stub know that we want it to return the thread. */
3486 /* Force the stub to choose a thread. */
3487 set_general_thread (null_ptid);
3490 inferior_ptid = remote_current_thread (minus_one_ptid);
3491 if (ptid_equal (inferior_ptid, minus_one_ptid))
3492 error (_("remote didn't report the current thread in non-stop mode"));
3494 get_offsets (); /* Get text, data & bss offsets. */
3496 /* In non-stop mode, any cached wait status will be stored in
3497 the stop reply queue. */
3498 gdb_assert (wait_status == NULL);
3500 /* Report all signals during attach/startup. */
3501 remote_pass_signals (0, NULL);
3504 /* If we connected to a live target, do some additional setup. */
3505 if (target_has_execution)
3507 if (exec_bfd) /* No use without an exec file. */
3508 remote_check_symbols (symfile_objfile);
3511 /* Possibly the target has been engaged in a trace run started
3512 previously; find out where things are at. */
3513 if (remote_get_trace_status (current_trace_status ()) != -1)
3515 struct uploaded_tp *uploaded_tps = NULL;
3516 struct uploaded_tsv *uploaded_tsvs = NULL;
3518 if (current_trace_status ()->running)
3519 printf_filtered (_("Trace is already running on the target.\n"));
3521 /* Get trace state variables first, they may be checked when
3522 parsing uploaded commands. */
3524 remote_upload_trace_state_variables (&uploaded_tsvs);
3526 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3528 remote_upload_tracepoints (&uploaded_tps);
3530 merge_uploaded_tracepoints (&uploaded_tps);
3533 /* The thread and inferior lists are now synchronized with the
3534 target, our symbols have been relocated, and we're merged the
3535 target's tracepoints with ours. We're done with basic start
3537 rs->starting_up = 0;
3539 /* If breakpoints are global, insert them now. */
3540 if (gdbarch_has_global_breakpoints (target_gdbarch)
3541 && breakpoints_always_inserted_mode ())
3542 insert_breakpoints ();
3545 /* Open a connection to a remote debugger.
3546 NAME is the filename used for communication. */
3549 remote_open (char *name, int from_tty)
3551 remote_open_1 (name, from_tty, &remote_ops, 0);
3554 /* Open a connection to a remote debugger using the extended
3555 remote gdb protocol. NAME is the filename used for communication. */
3558 extended_remote_open (char *name, int from_tty)
3560 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
3563 /* Generic code for opening a connection to a remote target. */
3566 init_all_packet_configs (void)
3570 for (i = 0; i < PACKET_MAX; i++)
3571 update_packet_config (&remote_protocol_packets[i]);
3574 /* Symbol look-up. */
3577 remote_check_symbols (struct objfile *objfile)
3579 struct remote_state *rs = get_remote_state ();
3580 char *msg, *reply, *tmp;
3581 struct minimal_symbol *sym;
3584 /* The remote side has no concept of inferiors that aren't running
3585 yet, it only knows about running processes. If we're connected
3586 but our current inferior is not running, we should not invite the
3587 remote target to request symbol lookups related to its
3588 (unrelated) current process. */
3589 if (!target_has_execution)
3592 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
3595 /* Make sure the remote is pointing at the right process. Note
3596 there's no way to select "no process". */
3597 set_general_process ();
3599 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3600 because we need both at the same time. */
3601 msg = alloca (get_remote_packet_size ());
3603 /* Invite target to request symbol lookups. */
3605 putpkt ("qSymbol::");
3606 getpkt (&rs->buf, &rs->buf_size, 0);
3607 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
3610 while (strncmp (reply, "qSymbol:", 8) == 0)
3613 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
3615 sym = lookup_minimal_symbol (msg, NULL, NULL);
3617 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
3620 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
3621 CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym);
3623 /* If this is a function address, return the start of code
3624 instead of any data function descriptor. */
3625 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch,
3629 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
3630 phex_nz (sym_addr, addr_size), &reply[8]);
3634 getpkt (&rs->buf, &rs->buf_size, 0);
3639 static struct serial *
3640 remote_serial_open (char *name)
3642 static int udp_warning = 0;
3644 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3645 of in ser-tcp.c, because it is the remote protocol assuming that the
3646 serial connection is reliable and not the serial connection promising
3648 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3650 warning (_("The remote protocol may be unreliable over UDP.\n"
3651 "Some events may be lost, rendering further debugging "
3656 return serial_open (name);
3659 /* Inform the target of our permission settings. The permission flags
3660 work without this, but if the target knows the settings, it can do
3661 a couple things. First, it can add its own check, to catch cases
3662 that somehow manage to get by the permissions checks in target
3663 methods. Second, if the target is wired to disallow particular
3664 settings (for instance, a system in the field that is not set up to
3665 be able to stop at a breakpoint), it can object to any unavailable
3669 remote_set_permissions (void)
3671 struct remote_state *rs = get_remote_state ();
3673 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3674 "WriteReg:%x;WriteMem:%x;"
3675 "InsertBreak:%x;InsertTrace:%x;"
3676 "InsertFastTrace:%x;Stop:%x",
3677 may_write_registers, may_write_memory,
3678 may_insert_breakpoints, may_insert_tracepoints,
3679 may_insert_fast_tracepoints, may_stop);
3681 getpkt (&rs->buf, &rs->buf_size, 0);
3683 /* If the target didn't like the packet, warn the user. Do not try
3684 to undo the user's settings, that would just be maddening. */
3685 if (strcmp (rs->buf, "OK") != 0)
3686 warning (_("Remote refused setting permissions with: %s"), rs->buf);
3689 /* This type describes each known response to the qSupported
3691 struct protocol_feature
3693 /* The name of this protocol feature. */
3696 /* The default for this protocol feature. */
3697 enum packet_support default_support;
3699 /* The function to call when this feature is reported, or after
3700 qSupported processing if the feature is not supported.
3701 The first argument points to this structure. The second
3702 argument indicates whether the packet requested support be
3703 enabled, disabled, or probed (or the default, if this function
3704 is being called at the end of processing and this feature was
3705 not reported). The third argument may be NULL; if not NULL, it
3706 is a NUL-terminated string taken from the packet following
3707 this feature's name and an equals sign. */
3708 void (*func) (const struct protocol_feature *, enum packet_support,
3711 /* The corresponding packet for this feature. Only used if
3712 FUNC is remote_supported_packet. */
3717 remote_supported_packet (const struct protocol_feature *feature,
3718 enum packet_support support,
3719 const char *argument)
3723 warning (_("Remote qSupported response supplied an unexpected value for"
3724 " \"%s\"."), feature->name);
3728 if (remote_protocol_packets[feature->packet].support
3729 == PACKET_SUPPORT_UNKNOWN)
3730 remote_protocol_packets[feature->packet].support = support;
3734 remote_packet_size (const struct protocol_feature *feature,
3735 enum packet_support support, const char *value)
3737 struct remote_state *rs = get_remote_state ();
3742 if (support != PACKET_ENABLE)
3745 if (value == NULL || *value == '\0')
3747 warning (_("Remote target reported \"%s\" without a size."),
3753 packet_size = strtol (value, &value_end, 16);
3754 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3756 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3757 feature->name, value);
3761 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3763 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3764 packet_size, MAX_REMOTE_PACKET_SIZE);
3765 packet_size = MAX_REMOTE_PACKET_SIZE;
3768 /* Record the new maximum packet size. */
3769 rs->explicit_packet_size = packet_size;
3773 remote_multi_process_feature (const struct protocol_feature *feature,
3774 enum packet_support support, const char *value)
3776 struct remote_state *rs = get_remote_state ();
3778 rs->multi_process_aware = (support == PACKET_ENABLE);
3782 remote_non_stop_feature (const struct protocol_feature *feature,
3783 enum packet_support support, const char *value)
3785 struct remote_state *rs = get_remote_state ();
3787 rs->non_stop_aware = (support == PACKET_ENABLE);
3791 remote_cond_tracepoint_feature (const struct protocol_feature *feature,
3792 enum packet_support support,
3795 struct remote_state *rs = get_remote_state ();
3797 rs->cond_tracepoints = (support == PACKET_ENABLE);
3801 remote_cond_breakpoint_feature (const struct protocol_feature *feature,
3802 enum packet_support support,
3805 struct remote_state *rs = get_remote_state ();
3807 rs->cond_breakpoints = (support == PACKET_ENABLE);
3811 remote_breakpoint_commands_feature (const struct protocol_feature *feature,
3812 enum packet_support support,
3815 struct remote_state *rs = get_remote_state ();
3817 rs->breakpoint_commands = (support == PACKET_ENABLE);
3821 remote_fast_tracepoint_feature (const struct protocol_feature *feature,
3822 enum packet_support support,
3825 struct remote_state *rs = get_remote_state ();
3827 rs->fast_tracepoints = (support == PACKET_ENABLE);
3831 remote_static_tracepoint_feature (const struct protocol_feature *feature,
3832 enum packet_support support,
3835 struct remote_state *rs = get_remote_state ();
3837 rs->static_tracepoints = (support == PACKET_ENABLE);
3841 remote_install_in_trace_feature (const struct protocol_feature *feature,
3842 enum packet_support support,
3845 struct remote_state *rs = get_remote_state ();
3847 rs->install_in_trace = (support == PACKET_ENABLE);
3851 remote_disconnected_tracing_feature (const struct protocol_feature *feature,
3852 enum packet_support support,
3855 struct remote_state *rs = get_remote_state ();
3857 rs->disconnected_tracing = (support == PACKET_ENABLE);
3861 remote_enable_disable_tracepoint_feature (const struct protocol_feature *feature,
3862 enum packet_support support,
3865 struct remote_state *rs = get_remote_state ();
3867 rs->enable_disable_tracepoints = (support == PACKET_ENABLE);
3871 remote_string_tracing_feature (const struct protocol_feature *feature,
3872 enum packet_support support,
3875 struct remote_state *rs = get_remote_state ();
3877 rs->string_tracing = (support == PACKET_ENABLE);
3880 static struct protocol_feature remote_protocol_features[] = {
3881 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
3882 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
3883 PACKET_qXfer_auxv },
3884 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3885 PACKET_qXfer_features },
3886 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3887 PACKET_qXfer_libraries },
3888 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3889 PACKET_qXfer_libraries_svr4 },
3890 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
3891 PACKET_qXfer_memory_map },
3892 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3893 PACKET_qXfer_spu_read },
3894 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3895 PACKET_qXfer_spu_write },
3896 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3897 PACKET_qXfer_osdata },
3898 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3899 PACKET_qXfer_threads },
3900 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3901 PACKET_qXfer_traceframe_info },
3902 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3903 PACKET_QPassSignals },
3904 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3905 PACKET_QProgramSignals },
3906 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3907 PACKET_QStartNoAckMode },
3908 { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 },
3909 { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 },
3910 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3911 PACKET_qXfer_siginfo_read },
3912 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3913 PACKET_qXfer_siginfo_write },
3914 { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature,
3915 PACKET_ConditionalTracepoints },
3916 { "ConditionalBreakpoints", PACKET_DISABLE, remote_cond_breakpoint_feature,
3917 PACKET_ConditionalBreakpoints },
3918 { "BreakpointCommands", PACKET_DISABLE, remote_breakpoint_commands_feature,
3919 PACKET_BreakpointCommands },
3920 { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature,
3921 PACKET_FastTracepoints },
3922 { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature,
3923 PACKET_StaticTracepoints },
3924 {"InstallInTrace", PACKET_DISABLE, remote_install_in_trace_feature,
3925 PACKET_InstallInTrace},
3926 { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature,
3928 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
3930 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
3932 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3933 PACKET_TracepointSource },
3934 { "QAllow", PACKET_DISABLE, remote_supported_packet,
3936 { "EnableDisableTracepoints", PACKET_DISABLE,
3937 remote_enable_disable_tracepoint_feature, -1 },
3938 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
3939 PACKET_qXfer_fdpic },
3940 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
3942 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
3943 PACKET_QDisableRandomization },
3944 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
3945 { "tracenz", PACKET_DISABLE,
3946 remote_string_tracing_feature, -1 },
3949 static char *remote_support_xml;
3951 /* Register string appended to "xmlRegisters=" in qSupported query. */
3954 register_remote_support_xml (const char *xml)
3956 #if defined(HAVE_LIBEXPAT)
3957 if (remote_support_xml == NULL)
3958 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
3961 char *copy = xstrdup (remote_support_xml + 13);
3962 char *p = strtok (copy, ",");
3966 if (strcmp (p, xml) == 0)
3973 while ((p = strtok (NULL, ",")) != NULL);
3976 remote_support_xml = reconcat (remote_support_xml,
3977 remote_support_xml, ",", xml,
3984 remote_query_supported_append (char *msg, const char *append)
3987 return reconcat (msg, msg, ";", append, (char *) NULL);
3989 return xstrdup (append);
3993 remote_query_supported (void)
3995 struct remote_state *rs = get_remote_state ();
3998 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4000 /* The packet support flags are handled differently for this packet
4001 than for most others. We treat an error, a disabled packet, and
4002 an empty response identically: any features which must be reported
4003 to be used will be automatically disabled. An empty buffer
4004 accomplishes this, since that is also the representation for a list
4005 containing no features. */
4008 if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
4011 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
4013 q = remote_query_supported_append (q, "multiprocess+");
4015 if (remote_support_xml)
4016 q = remote_query_supported_append (q, remote_support_xml);
4018 q = remote_query_supported_append (q, "qRelocInsn+");
4020 q = reconcat (q, "qSupported:", q, (char *) NULL);
4023 do_cleanups (old_chain);
4025 getpkt (&rs->buf, &rs->buf_size, 0);
4027 /* If an error occured, warn, but do not return - just reset the
4028 buffer to empty and go on to disable features. */
4029 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4032 warning (_("Remote failure reply: %s"), rs->buf);
4037 memset (seen, 0, sizeof (seen));
4042 enum packet_support is_supported;
4043 char *p, *end, *name_end, *value;
4045 /* First separate out this item from the rest of the packet. If
4046 there's another item after this, we overwrite the separator
4047 (terminated strings are much easier to work with). */
4049 end = strchr (p, ';');
4052 end = p + strlen (p);
4062 warning (_("empty item in \"qSupported\" response"));
4067 name_end = strchr (p, '=');
4070 /* This is a name=value entry. */
4071 is_supported = PACKET_ENABLE;
4072 value = name_end + 1;
4081 is_supported = PACKET_ENABLE;
4085 is_supported = PACKET_DISABLE;
4089 is_supported = PACKET_SUPPORT_UNKNOWN;
4093 warning (_("unrecognized item \"%s\" "
4094 "in \"qSupported\" response"), p);
4100 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4101 if (strcmp (remote_protocol_features[i].name, p) == 0)
4103 const struct protocol_feature *feature;
4106 feature = &remote_protocol_features[i];
4107 feature->func (feature, is_supported, value);
4112 /* If we increased the packet size, make sure to increase the global
4113 buffer size also. We delay this until after parsing the entire
4114 qSupported packet, because this is the same buffer we were
4116 if (rs->buf_size < rs->explicit_packet_size)
4118 rs->buf_size = rs->explicit_packet_size;
4119 rs->buf = xrealloc (rs->buf, rs->buf_size);
4122 /* Handle the defaults for unmentioned features. */
4123 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4126 const struct protocol_feature *feature;
4128 feature = &remote_protocol_features[i];
4129 feature->func (feature, feature->default_support, NULL);
4135 remote_open_1 (char *name, int from_tty,
4136 struct target_ops *target, int extended_p)
4138 struct remote_state *rs = get_remote_state ();
4141 error (_("To open a remote debug connection, you need to specify what\n"
4142 "serial device is attached to the remote system\n"
4143 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4145 /* See FIXME above. */
4146 if (!target_async_permitted)
4147 wait_forever_enabled_p = 1;
4149 /* If we're connected to a running target, target_preopen will kill it.
4150 But if we're connected to a target system with no running process,
4151 then we will still be connected when it returns. Ask this question
4152 first, before target_preopen has a chance to kill anything. */
4153 if (remote_desc != NULL && !have_inferiors ())
4156 || query (_("Already connected to a remote target. Disconnect? ")))
4159 error (_("Still connected."));
4162 target_preopen (from_tty);
4164 unpush_target (target);
4166 /* This time without a query. If we were connected to an
4167 extended-remote target and target_preopen killed the running
4168 process, we may still be connected. If we are starting "target
4169 remote" now, the extended-remote target will not have been
4170 removed by unpush_target. */
4171 if (remote_desc != NULL && !have_inferiors ())
4174 /* Make sure we send the passed signals list the next time we resume. */
4175 xfree (last_pass_packet);
4176 last_pass_packet = NULL;
4178 /* Make sure we send the program signals list the next time we
4180 xfree (last_program_signals_packet);
4181 last_program_signals_packet = NULL;
4183 remote_fileio_reset ();
4184 reopen_exec_file ();
4187 remote_desc = remote_serial_open (name);
4189 perror_with_name (name);
4191 if (baud_rate != -1)
4193 if (serial_setbaudrate (remote_desc, baud_rate))
4195 /* The requested speed could not be set. Error out to
4196 top level after closing remote_desc. Take care to
4197 set remote_desc to NULL to avoid closing remote_desc
4199 serial_close (remote_desc);
4201 perror_with_name (name);
4205 serial_raw (remote_desc);
4207 /* If there is something sitting in the buffer we might take it as a
4208 response to a command, which would be bad. */
4209 serial_flush_input (remote_desc);
4213 puts_filtered ("Remote debugging using ");
4214 puts_filtered (name);
4215 puts_filtered ("\n");
4217 push_target (target); /* Switch to using remote target now. */
4219 /* Register extra event sources in the event loop. */
4220 remote_async_inferior_event_token
4221 = create_async_event_handler (remote_async_inferior_event_handler,
4223 remote_async_get_pending_events_token
4224 = create_async_event_handler (remote_async_get_pending_events_handler,
4227 /* Reset the target state; these things will be queried either by
4228 remote_query_supported or as they are needed. */
4229 init_all_packet_configs ();
4230 rs->cached_wait_status = 0;
4231 rs->explicit_packet_size = 0;
4233 rs->multi_process_aware = 0;
4234 rs->extended = extended_p;
4235 rs->non_stop_aware = 0;
4236 rs->waiting_for_stop_reply = 0;
4237 rs->ctrlc_pending_p = 0;
4239 general_thread = not_sent_ptid;
4240 continue_thread = not_sent_ptid;
4241 remote_traceframe_number = -1;
4243 /* Probe for ability to use "ThreadInfo" query, as required. */
4244 use_threadinfo_query = 1;
4245 use_threadextra_query = 1;
4247 if (target_async_permitted)
4249 /* With this target we start out by owning the terminal. */
4250 remote_async_terminal_ours_p = 1;
4252 /* FIXME: cagney/1999-09-23: During the initial connection it is
4253 assumed that the target is already ready and able to respond to
4254 requests. Unfortunately remote_start_remote() eventually calls
4255 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4256 around this. Eventually a mechanism that allows
4257 wait_for_inferior() to expect/get timeouts will be
4259 wait_forever_enabled_p = 0;
4262 /* First delete any symbols previously loaded from shared libraries. */
4263 no_shared_libraries (NULL, 0);
4266 init_thread_list ();
4268 /* Start the remote connection. If error() or QUIT, discard this
4269 target (we'd otherwise be in an inconsistent state) and then
4270 propogate the error on up the exception chain. This ensures that
4271 the caller doesn't stumble along blindly assuming that the
4272 function succeeded. The CLI doesn't have this problem but other
4273 UI's, such as MI do.
4275 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4276 this function should return an error indication letting the
4277 caller restore the previous state. Unfortunately the command
4278 ``target remote'' is directly wired to this function making that
4279 impossible. On a positive note, the CLI side of this problem has
4280 been fixed - the function set_cmd_context() makes it possible for
4281 all the ``target ....'' commands to share a common callback
4282 function. See cli-dump.c. */
4284 volatile struct gdb_exception ex;
4286 TRY_CATCH (ex, RETURN_MASK_ALL)
4288 remote_start_remote (from_tty, target, extended_p);
4292 /* Pop the partially set up target - unless something else did
4293 already before throwing the exception. */
4294 if (remote_desc != NULL)
4296 if (target_async_permitted)
4297 wait_forever_enabled_p = 1;
4298 throw_exception (ex);
4302 if (target_async_permitted)
4303 wait_forever_enabled_p = 1;
4306 /* This takes a program previously attached to and detaches it. After
4307 this is done, GDB can be used to debug some other program. We
4308 better not have left any breakpoints in the target program or it'll
4309 die when it hits one. */
4312 remote_detach_1 (char *args, int from_tty, int extended)
4314 int pid = ptid_get_pid (inferior_ptid);
4315 struct remote_state *rs = get_remote_state ();
4318 error (_("Argument given to \"detach\" when remotely debugging."));
4320 if (!target_has_execution)
4321 error (_("No process to detach from."));
4325 char *exec_file = get_exec_file (0);
4326 if (exec_file == NULL)
4328 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4329 target_pid_to_str (pid_to_ptid (pid)));
4330 gdb_flush (gdb_stdout);
4333 /* Tell the remote target to detach. */
4334 if (remote_multi_process_p (rs))
4335 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4337 strcpy (rs->buf, "D");
4340 getpkt (&rs->buf, &rs->buf_size, 0);
4342 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4344 else if (rs->buf[0] == '\0')
4345 error (_("Remote doesn't know how to detach"));
4347 error (_("Can't detach process."));
4349 if (from_tty && !extended)
4350 puts_filtered (_("Ending remote debugging.\n"));
4352 discard_pending_stop_replies (pid);
4353 target_mourn_inferior ();
4357 remote_detach (struct target_ops *ops, char *args, int from_tty)
4359 remote_detach_1 (args, from_tty, 0);
4363 extended_remote_detach (struct target_ops *ops, char *args, int from_tty)
4365 remote_detach_1 (args, from_tty, 1);
4368 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4371 remote_disconnect (struct target_ops *target, char *args, int from_tty)
4374 error (_("Argument given to \"disconnect\" when remotely debugging."));
4376 /* Make sure we unpush even the extended remote targets; mourn
4377 won't do it. So call remote_mourn_1 directly instead of
4378 target_mourn_inferior. */
4379 remote_mourn_1 (target);
4382 puts_filtered ("Ending remote debugging.\n");
4385 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4386 be chatty about it. */
4389 extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
4391 struct remote_state *rs = get_remote_state ();
4393 char *wait_status = NULL;
4395 pid = parse_pid_to_attach (args);
4397 /* Remote PID can be freely equal to getpid, do not check it here the same
4398 way as in other targets. */
4400 if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4401 error (_("This target does not support attaching to a process"));
4405 char *exec_file = get_exec_file (0);
4408 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4409 target_pid_to_str (pid_to_ptid (pid)));
4411 printf_unfiltered (_("Attaching to %s\n"),
4412 target_pid_to_str (pid_to_ptid (pid)));
4414 gdb_flush (gdb_stdout);
4417 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
4419 getpkt (&rs->buf, &rs->buf_size, 0);
4421 if (packet_ok (rs->buf,
4422 &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK)
4426 /* Save the reply for later. */
4427 wait_status = alloca (strlen (rs->buf) + 1);
4428 strcpy (wait_status, rs->buf);
4430 else if (strcmp (rs->buf, "OK") != 0)
4431 error (_("Attaching to %s failed with: %s"),
4432 target_pid_to_str (pid_to_ptid (pid)),
4435 else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4436 error (_("This target does not support attaching to a process"));
4438 error (_("Attaching to %s failed"),
4439 target_pid_to_str (pid_to_ptid (pid)));
4441 set_current_inferior (remote_add_inferior (0, pid, 1));
4443 inferior_ptid = pid_to_ptid (pid);
4447 struct thread_info *thread;
4449 /* Get list of threads. */
4450 remote_threads_info (target);
4452 thread = first_thread_of_process (pid);
4454 inferior_ptid = thread->ptid;
4456 inferior_ptid = pid_to_ptid (pid);
4458 /* Invalidate our notion of the remote current thread. */
4459 record_currthread (minus_one_ptid);
4463 /* Now, if we have thread information, update inferior_ptid. */
4464 inferior_ptid = remote_current_thread (inferior_ptid);
4466 /* Add the main thread to the thread list. */
4467 add_thread_silent (inferior_ptid);
4470 /* Next, if the target can specify a description, read it. We do
4471 this before anything involving memory or registers. */
4472 target_find_description ();
4476 /* Use the previously fetched status. */
4477 gdb_assert (wait_status != NULL);
4479 if (target_can_async_p ())
4481 struct stop_reply *stop_reply;
4482 struct cleanup *old_chain;
4484 stop_reply = stop_reply_xmalloc ();
4485 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
4486 remote_parse_stop_reply (wait_status, stop_reply);
4487 discard_cleanups (old_chain);
4488 push_stop_reply (stop_reply);
4490 target_async (inferior_event_handler, 0);
4494 gdb_assert (wait_status != NULL);
4495 strcpy (rs->buf, wait_status);
4496 rs->cached_wait_status = 1;
4500 gdb_assert (wait_status == NULL);
4504 extended_remote_attach (struct target_ops *ops, char *args, int from_tty)
4506 extended_remote_attach_1 (ops, args, from_tty);
4509 /* Convert hex digit A to a number. */
4514 if (a >= '0' && a <= '9')
4516 else if (a >= 'a' && a <= 'f')
4517 return a - 'a' + 10;
4518 else if (a >= 'A' && a <= 'F')
4519 return a - 'A' + 10;
4521 error (_("Reply contains invalid hex digit %d"), a);
4525 hex2bin (const char *hex, gdb_byte *bin, int count)
4529 for (i = 0; i < count; i++)
4531 if (hex[0] == 0 || hex[1] == 0)
4533 /* Hex string is short, or of uneven length.
4534 Return the count that has been converted so far. */
4537 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
4543 /* Convert number NIB to a hex digit. */
4551 return 'a' + nib - 10;
4555 bin2hex (const gdb_byte *bin, char *hex, int count)
4559 /* May use a length, or a nul-terminated string as input. */
4561 count = strlen ((char *) bin);
4563 for (i = 0; i < count; i++)
4565 *hex++ = tohex ((*bin >> 4) & 0xf);
4566 *hex++ = tohex (*bin++ & 0xf);
4572 /* Check for the availability of vCont. This function should also check
4576 remote_vcont_probe (struct remote_state *rs)
4580 strcpy (rs->buf, "vCont?");
4582 getpkt (&rs->buf, &rs->buf_size, 0);
4585 /* Make sure that the features we assume are supported. */
4586 if (strncmp (buf, "vCont", 5) == 0)
4589 int support_s, support_S, support_c, support_C;
4595 rs->support_vCont_t = 0;
4596 while (p && *p == ';')
4599 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4601 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4603 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4605 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4607 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4608 rs->support_vCont_t = 1;
4610 p = strchr (p, ';');
4613 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4614 BUF will make packet_ok disable the packet. */
4615 if (!support_s || !support_S || !support_c || !support_C)
4619 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
4622 /* Helper function for building "vCont" resumptions. Write a
4623 resumption to P. ENDP points to one-passed-the-end of the buffer
4624 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4625 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4626 resumed thread should be single-stepped and/or signalled. If PTID
4627 equals minus_one_ptid, then all threads are resumed; if PTID
4628 represents a process, then all threads of the process are resumed;
4629 the thread to be stepped and/or signalled is given in the global
4633 append_resumption (char *p, char *endp,
4634 ptid_t ptid, int step, enum gdb_signal siggnal)
4636 struct remote_state *rs = get_remote_state ();
4638 if (step && siggnal != GDB_SIGNAL_0)
4639 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4641 p += xsnprintf (p, endp - p, ";s");
4642 else if (siggnal != GDB_SIGNAL_0)
4643 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4645 p += xsnprintf (p, endp - p, ";c");
4647 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4651 /* All (-1) threads of process. */
4652 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4654 p += xsnprintf (p, endp - p, ":");
4655 p = write_ptid (p, endp, nptid);
4657 else if (!ptid_equal (ptid, minus_one_ptid))
4659 p += xsnprintf (p, endp - p, ":");
4660 p = write_ptid (p, endp, ptid);
4666 /* Append a vCont continue-with-signal action for threads that have a
4667 non-zero stop signal. */
4670 append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4672 struct thread_info *thread;
4674 ALL_THREADS (thread)
4675 if (ptid_match (thread->ptid, ptid)
4676 && !ptid_equal (inferior_ptid, thread->ptid)
4677 && thread->suspend.stop_signal != GDB_SIGNAL_0
4678 && signal_pass_state (thread->suspend.stop_signal))
4680 p = append_resumption (p, endp, thread->ptid,
4681 0, thread->suspend.stop_signal);
4682 thread->suspend.stop_signal = GDB_SIGNAL_0;
4688 /* Resume the remote inferior by using a "vCont" packet. The thread
4689 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4690 resumed thread should be single-stepped and/or signalled. If PTID
4691 equals minus_one_ptid, then all threads are resumed; the thread to
4692 be stepped and/or signalled is given in the global INFERIOR_PTID.
4693 This function returns non-zero iff it resumes the inferior.
4695 This function issues a strict subset of all possible vCont commands at the
4699 remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
4701 struct remote_state *rs = get_remote_state ();
4705 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
4706 remote_vcont_probe (rs);
4708 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
4712 endp = rs->buf + get_remote_packet_size ();
4714 /* If we could generate a wider range of packets, we'd have to worry
4715 about overflowing BUF. Should there be a generic
4716 "multi-part-packet" packet? */
4718 p += xsnprintf (p, endp - p, "vCont");
4720 if (ptid_equal (ptid, magic_null_ptid))
4722 /* MAGIC_NULL_PTID means that we don't have any active threads,
4723 so we don't have any TID numbers the inferior will
4724 understand. Make sure to only send forms that do not specify
4726 append_resumption (p, endp, minus_one_ptid, step, siggnal);
4728 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
4730 /* Resume all threads (of all processes, or of a single
4731 process), with preference for INFERIOR_PTID. This assumes
4732 inferior_ptid belongs to the set of all threads we are about
4734 if (step || siggnal != GDB_SIGNAL_0)
4736 /* Step inferior_ptid, with or without signal. */
4737 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
4740 /* Also pass down any pending signaled resumption for other
4741 threads not the current. */
4742 p = append_pending_thread_resumptions (p, endp, ptid);
4744 /* And continue others without a signal. */
4745 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
4749 /* Scheduler locking; resume only PTID. */
4750 append_resumption (p, endp, ptid, step, siggnal);
4753 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4758 /* In non-stop, the stub replies to vCont with "OK". The stop
4759 reply will be reported asynchronously by means of a `%Stop'
4761 getpkt (&rs->buf, &rs->buf_size, 0);
4762 if (strcmp (rs->buf, "OK") != 0)
4763 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4769 /* Tell the remote machine to resume. */
4771 static enum gdb_signal last_sent_signal = GDB_SIGNAL_0;
4773 static int last_sent_step;
4776 remote_resume (struct target_ops *ops,
4777 ptid_t ptid, int step, enum gdb_signal siggnal)
4779 struct remote_state *rs = get_remote_state ();
4782 last_sent_signal = siggnal;
4783 last_sent_step = step;
4785 /* The vCont packet doesn't need to specify threads via Hc. */
4786 /* No reverse support (yet) for vCont. */
4787 if (execution_direction != EXEC_REVERSE)
4788 if (remote_vcont_resume (ptid, step, siggnal))
4791 /* All other supported resume packets do use Hc, so set the continue
4793 if (ptid_equal (ptid, minus_one_ptid))
4794 set_continue_thread (any_thread_ptid);
4796 set_continue_thread (ptid);
4799 if (execution_direction == EXEC_REVERSE)
4801 /* We don't pass signals to the target in reverse exec mode. */
4802 if (info_verbose && siggnal != GDB_SIGNAL_0)
4803 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4807 && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE)
4808 error (_("Remote reverse-step not supported."));
4810 && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE)
4811 error (_("Remote reverse-continue not supported."));
4813 strcpy (buf, step ? "bs" : "bc");
4815 else if (siggnal != GDB_SIGNAL_0)
4817 buf[0] = step ? 'S' : 'C';
4818 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
4819 buf[2] = tohex (((int) siggnal) & 0xf);
4823 strcpy (buf, step ? "s" : "c");
4828 /* We are about to start executing the inferior, let's register it
4829 with the event loop. NOTE: this is the one place where all the
4830 execution commands end up. We could alternatively do this in each
4831 of the execution commands in infcmd.c. */
4832 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4833 into infcmd.c in order to allow inferior function calls to work
4834 NOT asynchronously. */
4835 if (target_can_async_p ())
4836 target_async (inferior_event_handler, 0);
4838 /* We've just told the target to resume. The remote server will
4839 wait for the inferior to stop, and then send a stop reply. In
4840 the mean time, we can't start another command/query ourselves
4841 because the stub wouldn't be ready to process it. This applies
4842 only to the base all-stop protocol, however. In non-stop (which
4843 only supports vCont), the stub replies with an "OK", and is
4844 immediate able to process further serial input. */
4846 rs->waiting_for_stop_reply = 1;
4850 /* Set up the signal handler for SIGINT, while the target is
4851 executing, ovewriting the 'regular' SIGINT signal handler. */
4853 initialize_sigint_signal_handler (void)
4855 signal (SIGINT, handle_remote_sigint);
4858 /* Signal handler for SIGINT, while the target is executing. */
4860 handle_remote_sigint (int sig)
4862 signal (sig, handle_remote_sigint_twice);
4863 mark_async_signal_handler_wrapper (sigint_remote_token);
4866 /* Signal handler for SIGINT, installed after SIGINT has already been
4867 sent once. It will take effect the second time that the user sends
4870 handle_remote_sigint_twice (int sig)
4872 signal (sig, handle_remote_sigint);
4873 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
4876 /* Perform the real interruption of the target execution, in response
4879 async_remote_interrupt (gdb_client_data arg)
4882 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
4884 target_stop (inferior_ptid);
4887 /* Perform interrupt, if the first attempt did not succeed. Just give
4888 up on the target alltogether. */
4890 async_remote_interrupt_twice (gdb_client_data arg)
4893 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
4898 /* Reinstall the usual SIGINT handlers, after the target has
4901 cleanup_sigint_signal_handler (void *dummy)
4903 signal (SIGINT, handle_sigint);
4906 /* Send ^C to target to halt it. Target will respond, and send us a
4908 static void (*ofunc) (int);
4910 /* The command line interface's stop routine. This function is installed
4911 as a signal handler for SIGINT. The first time a user requests a
4912 stop, we call remote_stop to send a break or ^C. If there is no
4913 response from the target (it didn't stop when the user requested it),
4914 we ask the user if he'd like to detach from the target. */
4916 remote_interrupt (int signo)
4918 /* If this doesn't work, try more severe steps. */
4919 signal (signo, remote_interrupt_twice);
4921 gdb_call_async_signal_handler (sigint_remote_token, 1);
4924 /* The user typed ^C twice. */
4927 remote_interrupt_twice (int signo)
4929 signal (signo, ofunc);
4930 gdb_call_async_signal_handler (sigint_remote_twice_token, 1);
4931 signal (signo, remote_interrupt);
4934 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4935 thread, all threads of a remote process, or all threads of all
4939 remote_stop_ns (ptid_t ptid)
4941 struct remote_state *rs = get_remote_state ();
4943 char *endp = rs->buf + get_remote_packet_size ();
4945 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
4946 remote_vcont_probe (rs);
4948 if (!rs->support_vCont_t)
4949 error (_("Remote server does not support stopping threads"));
4951 if (ptid_equal (ptid, minus_one_ptid)
4952 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4953 p += xsnprintf (p, endp - p, "vCont;t");
4958 p += xsnprintf (p, endp - p, "vCont;t:");
4960 if (ptid_is_pid (ptid))
4961 /* All (-1) threads of process. */
4962 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4965 /* Small optimization: if we already have a stop reply for
4966 this thread, no use in telling the stub we want this
4968 if (peek_stop_reply (ptid))
4974 write_ptid (p, endp, nptid);
4977 /* In non-stop, we get an immediate OK reply. The stop reply will
4978 come in asynchronously by notification. */
4980 getpkt (&rs->buf, &rs->buf_size, 0);
4981 if (strcmp (rs->buf, "OK") != 0)
4982 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
4985 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4986 remote target. It is undefined which thread of which process
4987 reports the stop. */
4990 remote_stop_as (ptid_t ptid)
4992 struct remote_state *rs = get_remote_state ();
4994 rs->ctrlc_pending_p = 1;
4996 /* If the inferior is stopped already, but the core didn't know
4997 about it yet, just ignore the request. The cached wait status
4998 will be collected in remote_wait. */
4999 if (rs->cached_wait_status)
5002 /* Send interrupt_sequence to remote target. */
5003 send_interrupt_sequence ();
5006 /* This is the generic stop called via the target vector. When a target
5007 interrupt is requested, either by the command line or the GUI, we
5008 will eventually end up here. */
5011 remote_stop (ptid_t ptid)
5014 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
5017 remote_stop_ns (ptid);
5019 remote_stop_as (ptid);
5022 /* Ask the user what to do when an interrupt is received. */
5025 interrupt_query (void)
5027 target_terminal_ours ();
5029 if (target_can_async_p ())
5031 signal (SIGINT, handle_sigint);
5032 deprecated_throw_reason (RETURN_QUIT);
5036 if (query (_("Interrupted while waiting for the program.\n\
5037 Give up (and stop debugging it)? ")))
5040 deprecated_throw_reason (RETURN_QUIT);
5044 target_terminal_inferior ();
5047 /* Enable/disable target terminal ownership. Most targets can use
5048 terminal groups to control terminal ownership. Remote targets are
5049 different in that explicit transfer of ownership to/from GDB/target
5053 remote_terminal_inferior (void)
5055 if (!target_async_permitted)
5056 /* Nothing to do. */
5059 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5060 idempotent. The event-loop GDB talking to an asynchronous target
5061 with a synchronous command calls this function from both
5062 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5063 transfer the terminal to the target when it shouldn't this guard
5065 if (!remote_async_terminal_ours_p)
5067 delete_file_handler (input_fd);
5068 remote_async_terminal_ours_p = 0;
5069 initialize_sigint_signal_handler ();
5070 /* NOTE: At this point we could also register our selves as the
5071 recipient of all input. Any characters typed could then be
5072 passed on down to the target. */
5076 remote_terminal_ours (void)
5078 if (!target_async_permitted)
5079 /* Nothing to do. */
5082 /* See FIXME in remote_terminal_inferior. */
5083 if (remote_async_terminal_ours_p)
5085 cleanup_sigint_signal_handler (NULL);
5086 add_file_handler (input_fd, stdin_event_handler, 0);
5087 remote_async_terminal_ours_p = 1;
5091 remote_console_output (char *msg)
5095 for (p = msg; p[0] && p[1]; p += 2)
5098 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5102 fputs_unfiltered (tb, gdb_stdtarg);
5104 gdb_flush (gdb_stdtarg);
5107 typedef struct cached_reg
5110 gdb_byte data[MAX_REGISTER_SIZE];
5113 DEF_VEC_O(cached_reg_t);
5117 struct stop_reply *next;
5121 struct target_waitstatus ws;
5123 /* Expedited registers. This makes remote debugging a bit more
5124 efficient for those targets that provide critical registers as
5125 part of their normal status mechanism (as another roundtrip to
5126 fetch them is avoided). */
5127 VEC(cached_reg_t) *regcache;
5129 int stopped_by_watchpoint_p;
5130 CORE_ADDR watch_data_address;
5138 /* The list of already fetched and acknowledged stop events. */
5139 static struct stop_reply *stop_reply_queue;
5141 static struct stop_reply *
5142 stop_reply_xmalloc (void)
5144 struct stop_reply *r = XMALLOC (struct stop_reply);
5151 stop_reply_xfree (struct stop_reply *r)
5155 VEC_free (cached_reg_t, r->regcache);
5160 /* Discard all pending stop replies of inferior PID. If PID is -1,
5161 discard everything. */
5164 discard_pending_stop_replies (int pid)
5166 struct stop_reply *prev = NULL, *reply, *next;
5168 /* Discard the in-flight notification. */
5169 if (pending_stop_reply != NULL
5171 || ptid_get_pid (pending_stop_reply->ptid) == pid))
5173 stop_reply_xfree (pending_stop_reply);
5174 pending_stop_reply = NULL;
5177 /* Discard the stop replies we have already pulled with
5179 for (reply = stop_reply_queue; reply; reply = next)
5183 || ptid_get_pid (reply->ptid) == pid)
5185 if (reply == stop_reply_queue)
5186 stop_reply_queue = reply->next;
5188 prev->next = reply->next;
5190 stop_reply_xfree (reply);
5197 /* Cleanup wrapper. */
5200 do_stop_reply_xfree (void *arg)
5202 struct stop_reply *r = arg;
5204 stop_reply_xfree (r);
5207 /* Look for a queued stop reply belonging to PTID. If one is found,
5208 remove it from the queue, and return it. Returns NULL if none is
5209 found. If there are still queued events left to process, tell the
5210 event loop to get back to target_wait soon. */
5212 static struct stop_reply *
5213 queued_stop_reply (ptid_t ptid)
5215 struct stop_reply *it;
5216 struct stop_reply **it_link;
5218 it = stop_reply_queue;
5219 it_link = &stop_reply_queue;
5222 if (ptid_match (it->ptid, ptid))
5224 *it_link = it->next;
5229 it_link = &it->next;
5233 if (stop_reply_queue)
5234 /* There's still at least an event left. */
5235 mark_async_event_handler (remote_async_inferior_event_token);
5240 /* Push a fully parsed stop reply in the stop reply queue. Since we
5241 know that we now have at least one queued event left to pass to the
5242 core side, tell the event loop to get back to target_wait soon. */
5245 push_stop_reply (struct stop_reply *new_event)
5247 struct stop_reply *event;
5249 if (stop_reply_queue)
5251 for (event = stop_reply_queue;
5252 event && event->next;
5253 event = event->next)
5256 event->next = new_event;
5259 stop_reply_queue = new_event;
5261 mark_async_event_handler (remote_async_inferior_event_token);
5264 /* Returns true if we have a stop reply for PTID. */
5267 peek_stop_reply (ptid_t ptid)
5269 struct stop_reply *it;
5271 for (it = stop_reply_queue; it; it = it->next)
5272 if (ptid_equal (ptid, it->ptid))
5274 if (it->ws.kind == TARGET_WAITKIND_STOPPED)
5281 /* Parse the stop reply in BUF. Either the function succeeds, and the
5282 result is stored in EVENT, or throws an error. */
5285 remote_parse_stop_reply (char *buf, struct stop_reply *event)
5287 struct remote_arch_state *rsa = get_remote_arch_state ();
5291 event->ptid = null_ptid;
5292 event->ws.kind = TARGET_WAITKIND_IGNORE;
5293 event->ws.value.integer = 0;
5294 event->solibs_changed = 0;
5295 event->replay_event = 0;
5296 event->stopped_by_watchpoint_p = 0;
5297 event->regcache = NULL;
5302 case 'T': /* Status with PC, SP, FP, ... */
5303 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5304 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5306 n... = register number
5307 r... = register contents
5310 p = &buf[3]; /* after Txx */
5318 /* If the packet contains a register number, save it in
5319 pnum and set p1 to point to the character following it.
5320 Otherwise p1 points to p. */
5322 /* If this packet is an awatch packet, don't parse the 'a'
5323 as a register number. */
5325 if (strncmp (p, "awatch", strlen("awatch")) != 0
5326 && strncmp (p, "core", strlen ("core") != 0))
5328 /* Read the ``P'' register number. */
5329 pnum = strtol (p, &p_temp, 16);
5335 if (p1 == p) /* No register number present here. */
5337 p1 = strchr (p, ':');
5339 error (_("Malformed packet(a) (missing colon): %s\n\
5342 if (strncmp (p, "thread", p1 - p) == 0)
5343 event->ptid = read_ptid (++p1, &p);
5344 else if ((strncmp (p, "watch", p1 - p) == 0)
5345 || (strncmp (p, "rwatch", p1 - p) == 0)
5346 || (strncmp (p, "awatch", p1 - p) == 0))
5348 event->stopped_by_watchpoint_p = 1;
5349 p = unpack_varlen_hex (++p1, &addr);
5350 event->watch_data_address = (CORE_ADDR) addr;
5352 else if (strncmp (p, "library", p1 - p) == 0)
5356 while (*p_temp && *p_temp != ';')
5359 event->solibs_changed = 1;
5362 else if (strncmp (p, "replaylog", p1 - p) == 0)
5364 /* NO_HISTORY event.
5365 p1 will indicate "begin" or "end", but
5366 it makes no difference for now, so ignore it. */
5367 event->replay_event = 1;
5368 p_temp = strchr (p1 + 1, ';');
5372 else if (strncmp (p, "core", p1 - p) == 0)
5376 p = unpack_varlen_hex (++p1, &c);
5381 /* Silently skip unknown optional info. */
5382 p_temp = strchr (p1 + 1, ';');
5389 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5390 cached_reg_t cached_reg;
5395 error (_("Malformed packet(b) (missing colon): %s\n\
5401 error (_("Remote sent bad register number %s: %s\n\
5403 hex_string (pnum), p, buf);
5405 cached_reg.num = reg->regnum;
5407 fieldsize = hex2bin (p, cached_reg.data,
5408 register_size (target_gdbarch,
5411 if (fieldsize < register_size (target_gdbarch,
5413 warning (_("Remote reply is too short: %s"), buf);
5415 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5419 error (_("Remote register badly formatted: %s\nhere: %s"),
5424 case 'S': /* Old style status, just signal only. */
5425 if (event->solibs_changed)
5426 event->ws.kind = TARGET_WAITKIND_LOADED;
5427 else if (event->replay_event)
5428 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5431 event->ws.kind = TARGET_WAITKIND_STOPPED;
5432 event->ws.value.sig = (enum gdb_signal)
5433 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
5436 case 'W': /* Target exited. */
5443 /* GDB used to accept only 2 hex chars here. Stubs should
5444 only send more if they detect GDB supports multi-process
5446 p = unpack_varlen_hex (&buf[1], &value);
5450 /* The remote process exited. */
5451 event->ws.kind = TARGET_WAITKIND_EXITED;
5452 event->ws.value.integer = value;
5456 /* The remote process exited with a signal. */
5457 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5458 event->ws.value.sig = (enum gdb_signal) value;
5461 /* If no process is specified, assume inferior_ptid. */
5462 pid = ptid_get_pid (inferior_ptid);
5471 else if (strncmp (p,
5472 "process:", sizeof ("process:") - 1) == 0)
5476 p += sizeof ("process:") - 1;
5477 unpack_varlen_hex (p, &upid);
5481 error (_("unknown stop reply packet: %s"), buf);
5484 error (_("unknown stop reply packet: %s"), buf);
5485 event->ptid = pid_to_ptid (pid);
5490 if (non_stop && ptid_equal (event->ptid, null_ptid))
5491 error (_("No process or thread specified in stop reply: %s"), buf);
5494 /* When the stub wants to tell GDB about a new stop reply, it sends a
5495 stop notification (%Stop). Those can come it at any time, hence,
5496 we have to make sure that any pending putpkt/getpkt sequence we're
5497 making is finished, before querying the stub for more events with
5498 vStopped. E.g., if we started a vStopped sequence immediatelly
5499 upon receiving the %Stop notification, something like this could
5507 1.6) <-- (registers reply to step #1.3)
5509 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5512 To solve this, whenever we parse a %Stop notification sucessfully,
5513 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5514 doing whatever we were doing:
5520 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5521 2.5) <-- (registers reply to step #2.3)
5523 Eventualy after step #2.5, we return to the event loop, which
5524 notices there's an event on the
5525 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5526 associated callback --- the function below. At this point, we're
5527 always safe to start a vStopped sequence. :
5530 2.7) <-- T05 thread:2
5536 remote_get_pending_stop_replies (void)
5538 struct remote_state *rs = get_remote_state ();
5540 if (pending_stop_reply)
5543 putpkt ("vStopped");
5545 /* Now we can rely on it. */
5546 push_stop_reply (pending_stop_reply);
5547 pending_stop_reply = NULL;
5551 getpkt (&rs->buf, &rs->buf_size, 0);
5552 if (strcmp (rs->buf, "OK") == 0)
5556 struct cleanup *old_chain;
5557 struct stop_reply *stop_reply = stop_reply_xmalloc ();
5559 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5560 remote_parse_stop_reply (rs->buf, stop_reply);
5563 putpkt ("vStopped");
5565 if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE)
5567 /* Now we can rely on it. */
5568 discard_cleanups (old_chain);
5569 push_stop_reply (stop_reply);
5572 /* We got an unknown stop reply. */
5573 do_cleanups (old_chain);
5580 /* Called when it is decided that STOP_REPLY holds the info of the
5581 event that is to be returned to the core. This function always
5582 destroys STOP_REPLY. */
5585 process_stop_reply (struct stop_reply *stop_reply,
5586 struct target_waitstatus *status)
5590 *status = stop_reply->ws;
5591 ptid = stop_reply->ptid;
5593 /* If no thread/process was reported by the stub, assume the current
5595 if (ptid_equal (ptid, null_ptid))
5596 ptid = inferior_ptid;
5598 if (status->kind != TARGET_WAITKIND_EXITED
5599 && status->kind != TARGET_WAITKIND_SIGNALLED)
5601 /* Expedited registers. */
5602 if (stop_reply->regcache)
5604 struct regcache *regcache
5605 = get_thread_arch_regcache (ptid, target_gdbarch);
5610 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5612 regcache_raw_supply (regcache, reg->num, reg->data);
5613 VEC_free (cached_reg_t, stop_reply->regcache);
5616 remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5617 remote_watch_data_address = stop_reply->watch_data_address;
5619 remote_notice_new_inferior (ptid, 0);
5620 demand_private_info (ptid)->core = stop_reply->core;
5623 stop_reply_xfree (stop_reply);
5627 /* The non-stop mode version of target_wait. */
5630 remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
5632 struct remote_state *rs = get_remote_state ();
5633 struct stop_reply *stop_reply;
5636 /* If in non-stop mode, get out of getpkt even if a
5637 notification is received. */
5639 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5646 case 'E': /* Error of some sort. */
5647 /* We're out of sync with the target now. Did it continue
5648 or not? We can't tell which thread it was in non-stop,
5649 so just ignore this. */
5650 warning (_("Remote failure reply: %s"), rs->buf);
5652 case 'O': /* Console output. */
5653 remote_console_output (rs->buf + 1);
5656 warning (_("Invalid remote reply: %s"), rs->buf);
5660 /* Acknowledge a pending stop reply that may have arrived in the
5662 if (pending_stop_reply != NULL)
5663 remote_get_pending_stop_replies ();
5665 /* If indeed we noticed a stop reply, we're done. */
5666 stop_reply = queued_stop_reply (ptid);
5667 if (stop_reply != NULL)
5668 return process_stop_reply (stop_reply, status);
5670 /* Still no event. If we're just polling for an event, then
5671 return to the event loop. */
5672 if (options & TARGET_WNOHANG)
5674 status->kind = TARGET_WAITKIND_IGNORE;
5675 return minus_one_ptid;
5678 /* Otherwise do a blocking wait. */
5679 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5684 /* Wait until the remote machine stops, then return, storing status in
5685 STATUS just as `wait' would. */
5688 remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
5690 struct remote_state *rs = get_remote_state ();
5691 ptid_t event_ptid = null_ptid;
5693 struct stop_reply *stop_reply;
5697 status->kind = TARGET_WAITKIND_IGNORE;
5698 status->value.integer = 0;
5700 stop_reply = queued_stop_reply (ptid);
5701 if (stop_reply != NULL)
5702 return process_stop_reply (stop_reply, status);
5704 if (rs->cached_wait_status)
5705 /* Use the cached wait status, but only once. */
5706 rs->cached_wait_status = 0;
5711 if (!target_is_async_p ())
5713 ofunc = signal (SIGINT, remote_interrupt);
5714 /* If the user hit C-c before this packet, or between packets,
5715 pretend that it was hit right here. */
5719 remote_interrupt (SIGINT);
5723 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5724 _never_ wait for ever -> test on target_is_async_p().
5725 However, before we do that we need to ensure that the caller
5726 knows how to take the target into/out of async mode. */
5727 ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
5728 if (!target_is_async_p ())
5729 signal (SIGINT, ofunc);
5734 remote_stopped_by_watchpoint_p = 0;
5736 /* We got something. */
5737 rs->waiting_for_stop_reply = 0;
5739 /* Assume that the target has acknowledged Ctrl-C unless we receive
5740 an 'F' or 'O' packet. */
5741 if (buf[0] != 'F' && buf[0] != 'O')
5742 rs->ctrlc_pending_p = 0;
5746 case 'E': /* Error of some sort. */
5747 /* We're out of sync with the target now. Did it continue or
5748 not? Not is more likely, so report a stop. */
5749 warning (_("Remote failure reply: %s"), buf);
5750 status->kind = TARGET_WAITKIND_STOPPED;
5751 status->value.sig = GDB_SIGNAL_0;
5753 case 'F': /* File-I/O request. */
5754 remote_fileio_request (buf, rs->ctrlc_pending_p);
5755 rs->ctrlc_pending_p = 0;
5757 case 'T': case 'S': case 'X': case 'W':
5759 struct stop_reply *stop_reply;
5760 struct cleanup *old_chain;
5762 stop_reply = stop_reply_xmalloc ();
5763 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5764 remote_parse_stop_reply (buf, stop_reply);
5765 discard_cleanups (old_chain);
5766 event_ptid = process_stop_reply (stop_reply, status);
5769 case 'O': /* Console output. */
5770 remote_console_output (buf + 1);
5772 /* The target didn't really stop; keep waiting. */
5773 rs->waiting_for_stop_reply = 1;
5777 if (last_sent_signal != GDB_SIGNAL_0)
5779 /* Zero length reply means that we tried 'S' or 'C' and the
5780 remote system doesn't support it. */
5781 target_terminal_ours_for_output ();
5783 ("Can't send signals to this remote system. %s not sent.\n",
5784 gdb_signal_to_name (last_sent_signal));
5785 last_sent_signal = GDB_SIGNAL_0;
5786 target_terminal_inferior ();
5788 strcpy ((char *) buf, last_sent_step ? "s" : "c");
5789 putpkt ((char *) buf);
5791 /* We just told the target to resume, so a stop reply is in
5793 rs->waiting_for_stop_reply = 1;
5796 /* else fallthrough */
5798 warning (_("Invalid remote reply: %s"), buf);
5800 rs->waiting_for_stop_reply = 1;
5804 if (status->kind == TARGET_WAITKIND_IGNORE)
5806 /* Nothing interesting happened. If we're doing a non-blocking
5807 poll, we're done. Otherwise, go back to waiting. */
5808 if (options & TARGET_WNOHANG)
5809 return minus_one_ptid;
5813 else if (status->kind != TARGET_WAITKIND_EXITED
5814 && status->kind != TARGET_WAITKIND_SIGNALLED)
5816 if (!ptid_equal (event_ptid, null_ptid))
5817 record_currthread (event_ptid);
5819 event_ptid = inferior_ptid;
5822 /* A process exit. Invalidate our notion of current thread. */
5823 record_currthread (minus_one_ptid);
5828 /* Wait until the remote machine stops, then return, storing status in
5829 STATUS just as `wait' would. */
5832 remote_wait (struct target_ops *ops,
5833 ptid_t ptid, struct target_waitstatus *status, int options)
5838 event_ptid = remote_wait_ns (ptid, status, options);
5840 event_ptid = remote_wait_as (ptid, status, options);
5842 if (target_can_async_p ())
5844 /* If there are are events left in the queue tell the event loop
5846 if (stop_reply_queue)
5847 mark_async_event_handler (remote_async_inferior_event_token);
5853 /* Fetch a single register using a 'p' packet. */
5856 fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
5858 struct remote_state *rs = get_remote_state ();
5860 char regp[MAX_REGISTER_SIZE];
5863 if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE)
5866 if (reg->pnum == -1)
5871 p += hexnumstr (p, reg->pnum);
5874 getpkt (&rs->buf, &rs->buf_size, 0);
5878 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
5882 case PACKET_UNKNOWN:
5885 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5886 gdbarch_register_name (get_regcache_arch (regcache),
5891 /* If this register is unfetchable, tell the regcache. */
5894 regcache_raw_supply (regcache, reg->regnum, NULL);
5898 /* Otherwise, parse and supply the value. */
5904 error (_("fetch_register_using_p: early buf termination"));
5906 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
5909 regcache_raw_supply (regcache, reg->regnum, regp);
5913 /* Fetch the registers included in the target's 'g' packet. */
5916 send_g_packet (void)
5918 struct remote_state *rs = get_remote_state ();
5921 xsnprintf (rs->buf, get_remote_packet_size (), "g");
5922 remote_send (&rs->buf, &rs->buf_size);
5924 /* We can get out of synch in various cases. If the first character
5925 in the buffer is not a hex character, assume that has happened
5926 and try to fetch another packet to read. */
5927 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
5928 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
5929 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
5930 && rs->buf[0] != 'x') /* New: unavailable register value. */
5933 fprintf_unfiltered (gdb_stdlog,
5934 "Bad register packet; fetching a new packet\n");
5935 getpkt (&rs->buf, &rs->buf_size, 0);
5938 buf_len = strlen (rs->buf);
5940 /* Sanity check the received packet. */
5941 if (buf_len % 2 != 0)
5942 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
5948 process_g_packet (struct regcache *regcache)
5950 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5951 struct remote_state *rs = get_remote_state ();
5952 struct remote_arch_state *rsa = get_remote_arch_state ();
5957 buf_len = strlen (rs->buf);
5959 /* Further sanity checks, with knowledge of the architecture. */
5960 if (buf_len > 2 * rsa->sizeof_g_packet)
5961 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
5963 /* Save the size of the packet sent to us by the target. It is used
5964 as a heuristic when determining the max size of packets that the
5965 target can safely receive. */
5966 if (rsa->actual_register_packet_size == 0)
5967 rsa->actual_register_packet_size = buf_len;
5969 /* If this is smaller than we guessed the 'g' packet would be,
5970 update our records. A 'g' reply that doesn't include a register's
5971 value implies either that the register is not available, or that
5972 the 'p' packet must be used. */
5973 if (buf_len < 2 * rsa->sizeof_g_packet)
5975 rsa->sizeof_g_packet = buf_len / 2;
5977 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
5979 if (rsa->regs[i].pnum == -1)
5982 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
5983 rsa->regs[i].in_g_packet = 0;
5985 rsa->regs[i].in_g_packet = 1;
5989 regs = alloca (rsa->sizeof_g_packet);
5991 /* Unimplemented registers read as all bits zero. */
5992 memset (regs, 0, rsa->sizeof_g_packet);
5994 /* Reply describes registers byte by byte, each byte encoded as two
5995 hex characters. Suck them all up, then supply them to the
5996 register cacheing/storage mechanism. */
5999 for (i = 0; i < rsa->sizeof_g_packet; i++)
6001 if (p[0] == 0 || p[1] == 0)
6002 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6003 internal_error (__FILE__, __LINE__,
6004 _("unexpected end of 'g' packet reply"));
6006 if (p[0] == 'x' && p[1] == 'x')
6007 regs[i] = 0; /* 'x' */
6009 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6013 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6015 struct packet_reg *r = &rsa->regs[i];
6019 if (r->offset * 2 >= strlen (rs->buf))
6020 /* This shouldn't happen - we adjusted in_g_packet above. */
6021 internal_error (__FILE__, __LINE__,
6022 _("unexpected end of 'g' packet reply"));
6023 else if (rs->buf[r->offset * 2] == 'x')
6025 gdb_assert (r->offset * 2 < strlen (rs->buf));
6026 /* The register isn't available, mark it as such (at
6027 the same time setting the value to zero). */
6028 regcache_raw_supply (regcache, r->regnum, NULL);
6031 regcache_raw_supply (regcache, r->regnum,
6038 fetch_registers_using_g (struct regcache *regcache)
6041 process_g_packet (regcache);
6044 /* Make the remote selected traceframe match GDB's selected
6048 set_remote_traceframe (void)
6052 if (remote_traceframe_number == get_traceframe_number ())
6055 /* Avoid recursion, remote_trace_find calls us again. */
6056 remote_traceframe_number = get_traceframe_number ();
6058 newnum = target_trace_find (tfind_number,
6059 get_traceframe_number (), 0, 0, NULL);
6061 /* Should not happen. If it does, all bets are off. */
6062 if (newnum != get_traceframe_number ())
6063 warning (_("could not set remote traceframe"));
6067 remote_fetch_registers (struct target_ops *ops,
6068 struct regcache *regcache, int regnum)
6070 struct remote_arch_state *rsa = get_remote_arch_state ();
6073 set_remote_traceframe ();
6074 set_general_thread (inferior_ptid);
6078 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6080 gdb_assert (reg != NULL);
6082 /* If this register might be in the 'g' packet, try that first -
6083 we are likely to read more than one register. If this is the
6084 first 'g' packet, we might be overly optimistic about its
6085 contents, so fall back to 'p'. */
6086 if (reg->in_g_packet)
6088 fetch_registers_using_g (regcache);
6089 if (reg->in_g_packet)
6093 if (fetch_register_using_p (regcache, reg))
6096 /* This register is not available. */
6097 regcache_raw_supply (regcache, reg->regnum, NULL);
6102 fetch_registers_using_g (regcache);
6104 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6105 if (!rsa->regs[i].in_g_packet)
6106 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
6108 /* This register is not available. */
6109 regcache_raw_supply (regcache, i, NULL);
6113 /* Prepare to store registers. Since we may send them all (using a
6114 'G' request), we have to read out the ones we don't want to change
6118 remote_prepare_to_store (struct regcache *regcache)
6120 struct remote_arch_state *rsa = get_remote_arch_state ();
6122 gdb_byte buf[MAX_REGISTER_SIZE];
6124 /* Make sure the entire registers array is valid. */
6125 switch (remote_protocol_packets[PACKET_P].support)
6127 case PACKET_DISABLE:
6128 case PACKET_SUPPORT_UNKNOWN:
6129 /* Make sure all the necessary registers are cached. */
6130 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6131 if (rsa->regs[i].in_g_packet)
6132 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
6139 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6140 packet was not recognized. */
6143 store_register_using_P (const struct regcache *regcache,
6144 struct packet_reg *reg)
6146 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6147 struct remote_state *rs = get_remote_state ();
6148 /* Try storing a single register. */
6149 char *buf = rs->buf;
6150 gdb_byte regp[MAX_REGISTER_SIZE];
6153 if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE)
6156 if (reg->pnum == -1)
6159 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
6160 p = buf + strlen (buf);
6161 regcache_raw_collect (regcache, reg->regnum, regp);
6162 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6164 getpkt (&rs->buf, &rs->buf_size, 0);
6166 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6171 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6172 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
6173 case PACKET_UNKNOWN:
6176 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6180 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6181 contents of the register cache buffer. FIXME: ignores errors. */
6184 store_registers_using_G (const struct regcache *regcache)
6186 struct remote_state *rs = get_remote_state ();
6187 struct remote_arch_state *rsa = get_remote_arch_state ();
6191 /* Extract all the registers in the regcache copying them into a
6196 regs = alloca (rsa->sizeof_g_packet);
6197 memset (regs, 0, rsa->sizeof_g_packet);
6198 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6200 struct packet_reg *r = &rsa->regs[i];
6203 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
6207 /* Command describes registers byte by byte,
6208 each byte encoded as two hex characters. */
6211 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6213 bin2hex (regs, p, rsa->sizeof_g_packet);
6215 getpkt (&rs->buf, &rs->buf_size, 0);
6216 if (packet_check_result (rs->buf) == PACKET_ERROR)
6217 error (_("Could not write registers; remote failure reply '%s'"),
6221 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6222 of the register cache buffer. FIXME: ignores errors. */
6225 remote_store_registers (struct target_ops *ops,
6226 struct regcache *regcache, int regnum)
6228 struct remote_arch_state *rsa = get_remote_arch_state ();
6231 set_remote_traceframe ();
6232 set_general_thread (inferior_ptid);
6236 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6238 gdb_assert (reg != NULL);
6240 /* Always prefer to store registers using the 'P' packet if
6241 possible; we often change only a small number of registers.
6242 Sometimes we change a larger number; we'd need help from a
6243 higher layer to know to use 'G'. */
6244 if (store_register_using_P (regcache, reg))
6247 /* For now, don't complain if we have no way to write the
6248 register. GDB loses track of unavailable registers too
6249 easily. Some day, this may be an error. We don't have
6250 any way to read the register, either... */
6251 if (!reg->in_g_packet)
6254 store_registers_using_G (regcache);
6258 store_registers_using_G (regcache);
6260 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6261 if (!rsa->regs[i].in_g_packet)
6262 if (!store_register_using_P (regcache, &rsa->regs[i]))
6263 /* See above for why we do not issue an error here. */
6268 /* Return the number of hex digits in num. */
6271 hexnumlen (ULONGEST num)
6275 for (i = 0; num != 0; i++)
6281 /* Set BUF to the minimum number of hex digits representing NUM. */
6284 hexnumstr (char *buf, ULONGEST num)
6286 int len = hexnumlen (num);
6288 return hexnumnstr (buf, num, len);
6292 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6295 hexnumnstr (char *buf, ULONGEST num, int width)
6301 for (i = width - 1; i >= 0; i--)
6303 buf[i] = "0123456789abcdef"[(num & 0xf)];
6310 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6313 remote_address_masked (CORE_ADDR addr)
6315 int address_size = remote_address_size;
6317 /* If "remoteaddresssize" was not set, default to target address size. */
6319 address_size = gdbarch_addr_bit (target_gdbarch);
6321 if (address_size > 0
6322 && address_size < (sizeof (ULONGEST) * 8))
6324 /* Only create a mask when that mask can safely be constructed
6325 in a ULONGEST variable. */
6328 mask = (mask << address_size) - 1;
6334 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6335 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6336 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6337 (which may be more than *OUT_LEN due to escape characters). The
6338 total number of bytes in the output buffer will be at most
6342 remote_escape_output (const gdb_byte *buffer, int len,
6343 gdb_byte *out_buf, int *out_len,
6346 int input_index, output_index;
6349 for (input_index = 0; input_index < len; input_index++)
6351 gdb_byte b = buffer[input_index];
6353 if (b == '$' || b == '#' || b == '}')
6355 /* These must be escaped. */
6356 if (output_index + 2 > out_maxlen)
6358 out_buf[output_index++] = '}';
6359 out_buf[output_index++] = b ^ 0x20;
6363 if (output_index + 1 > out_maxlen)
6365 out_buf[output_index++] = b;
6369 *out_len = input_index;
6370 return output_index;
6373 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6374 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6375 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6377 This function reverses remote_escape_output. It allows more
6378 escaped characters than that function does, in particular because
6379 '*' must be escaped to avoid the run-length encoding processing
6380 in reading packets. */
6383 remote_unescape_input (const gdb_byte *buffer, int len,
6384 gdb_byte *out_buf, int out_maxlen)
6386 int input_index, output_index;
6391 for (input_index = 0; input_index < len; input_index++)
6393 gdb_byte b = buffer[input_index];
6395 if (output_index + 1 > out_maxlen)
6397 warning (_("Received too much data from remote target;"
6398 " ignoring overflow."));
6399 return output_index;
6404 out_buf[output_index++] = b ^ 0x20;
6410 out_buf[output_index++] = b;
6414 error (_("Unmatched escape character in target response."));
6416 return output_index;
6419 /* Determine whether the remote target supports binary downloading.
6420 This is accomplished by sending a no-op memory write of zero length
6421 to the target at the specified address. It does not suffice to send
6422 the whole packet, since many stubs strip the eighth bit and
6423 subsequently compute a wrong checksum, which causes real havoc with
6426 NOTE: This can still lose if the serial line is not eight-bit
6427 clean. In cases like this, the user should clear "remote
6431 check_binary_download (CORE_ADDR addr)
6433 struct remote_state *rs = get_remote_state ();
6435 switch (remote_protocol_packets[PACKET_X].support)
6437 case PACKET_DISABLE:
6441 case PACKET_SUPPORT_UNKNOWN:
6447 p += hexnumstr (p, (ULONGEST) addr);
6449 p += hexnumstr (p, (ULONGEST) 0);
6453 putpkt_binary (rs->buf, (int) (p - rs->buf));
6454 getpkt (&rs->buf, &rs->buf_size, 0);
6456 if (rs->buf[0] == '\0')
6459 fprintf_unfiltered (gdb_stdlog,
6460 "binary downloading NOT "
6461 "supported by target\n");
6462 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
6467 fprintf_unfiltered (gdb_stdlog,
6468 "binary downloading supported by target\n");
6469 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
6476 /* Write memory data directly to the remote machine.
6477 This does not inform the data cache; the data cache uses this.
6478 HEADER is the starting part of the packet.
6479 MEMADDR is the address in the remote memory space.
6480 MYADDR is the address of the buffer in our space.
6481 LEN is the number of bytes.
6482 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6483 should send data as binary ('X'), or hex-encoded ('M').
6485 The function creates packet of the form
6486 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6488 where encoding of <DATA> is termined by PACKET_FORMAT.
6490 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6493 Returns the number of bytes transferred, or 0 (setting errno) for
6494 error. Only transfer a single packet. */
6497 remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6498 const gdb_byte *myaddr, ssize_t len,
6499 char packet_format, int use_length)
6501 struct remote_state *rs = get_remote_state ();
6511 if (packet_format != 'X' && packet_format != 'M')
6512 internal_error (__FILE__, __LINE__,
6513 _("remote_write_bytes_aux: bad packet format"));
6518 payload_size = get_memory_write_packet_size ();
6520 /* The packet buffer will be large enough for the payload;
6521 get_memory_packet_size ensures this. */
6524 /* Compute the size of the actual payload by subtracting out the
6525 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6527 payload_size -= strlen ("$,:#NN");
6529 /* The comma won't be used. */
6531 header_length = strlen (header);
6532 payload_size -= header_length;
6533 payload_size -= hexnumlen (memaddr);
6535 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6537 strcat (rs->buf, header);
6538 p = rs->buf + strlen (header);
6540 /* Compute a best guess of the number of bytes actually transfered. */
6541 if (packet_format == 'X')
6543 /* Best guess at number of bytes that will fit. */
6544 todo = min (len, payload_size);
6546 payload_size -= hexnumlen (todo);
6547 todo = min (todo, payload_size);
6551 /* Num bytes that will fit. */
6552 todo = min (len, payload_size / 2);
6554 payload_size -= hexnumlen (todo);
6555 todo = min (todo, payload_size / 2);
6559 internal_error (__FILE__, __LINE__,
6560 _("minimum packet size too small to write data"));
6562 /* If we already need another packet, then try to align the end
6563 of this packet to a useful boundary. */
6564 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6565 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6567 /* Append "<memaddr>". */
6568 memaddr = remote_address_masked (memaddr);
6569 p += hexnumstr (p, (ULONGEST) memaddr);
6576 /* Append <len>. Retain the location/size of <len>. It may need to
6577 be adjusted once the packet body has been created. */
6579 plenlen = hexnumstr (p, (ULONGEST) todo);
6587 /* Append the packet body. */
6588 if (packet_format == 'X')
6590 /* Binary mode. Send target system values byte by byte, in
6591 increasing byte addresses. Only escape certain critical
6593 payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes,
6596 /* If not all TODO bytes fit, then we'll need another packet. Make
6597 a second try to keep the end of the packet aligned. Don't do
6598 this if the packet is tiny. */
6599 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6603 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6605 if (new_nr_bytes != nr_bytes)
6606 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6611 p += payload_length;
6612 if (use_length && nr_bytes < todo)
6614 /* Escape chars have filled up the buffer prematurely,
6615 and we have actually sent fewer bytes than planned.
6616 Fix-up the length field of the packet. Use the same
6617 number of characters as before. */
6618 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6619 *plen = ':'; /* overwrite \0 from hexnumnstr() */
6624 /* Normal mode: Send target system values byte by byte, in
6625 increasing byte addresses. Each byte is encoded as a two hex
6627 nr_bytes = bin2hex (myaddr, p, todo);
6631 putpkt_binary (rs->buf, (int) (p - rs->buf));
6632 getpkt (&rs->buf, &rs->buf_size, 0);
6634 if (rs->buf[0] == 'E')
6636 /* There is no correspondance between what the remote protocol
6637 uses for errors and errno codes. We would like a cleaner way
6638 of representing errors (big enough to include errno codes,
6639 bfd_error codes, and others). But for now just return EIO. */
6644 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6645 fewer bytes than we'd planned. */
6649 /* Write memory data directly to the remote machine.
6650 This does not inform the data cache; the data cache uses this.
6651 MEMADDR is the address in the remote memory space.
6652 MYADDR is the address of the buffer in our space.
6653 LEN is the number of bytes.
6655 Returns number of bytes transferred, or 0 (setting errno) for
6656 error. Only transfer a single packet. */
6659 remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
6661 char *packet_format = 0;
6663 /* Check whether the target supports binary download. */
6664 check_binary_download (memaddr);
6666 switch (remote_protocol_packets[PACKET_X].support)
6669 packet_format = "X";
6671 case PACKET_DISABLE:
6672 packet_format = "M";
6674 case PACKET_SUPPORT_UNKNOWN:
6675 internal_error (__FILE__, __LINE__,
6676 _("remote_write_bytes: bad internal state"));
6678 internal_error (__FILE__, __LINE__, _("bad switch"));
6681 return remote_write_bytes_aux (packet_format,
6682 memaddr, myaddr, len, packet_format[0], 1);
6685 /* Read memory data directly from the remote machine.
6686 This does not use the data cache; the data cache uses this.
6687 MEMADDR is the address in the remote memory space.
6688 MYADDR is the address of the buffer in our space.
6689 LEN is the number of bytes.
6691 Returns number of bytes transferred, or 0 for error. */
6694 remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
6696 struct remote_state *rs = get_remote_state ();
6697 int max_buf_size; /* Max size of packet output buffer. */
6705 max_buf_size = get_memory_read_packet_size ();
6706 /* The packet buffer will be large enough for the payload;
6707 get_memory_packet_size ensures this. */
6709 /* Number if bytes that will fit. */
6710 todo = min (len, max_buf_size / 2);
6712 /* Construct "m"<memaddr>","<len>". */
6713 memaddr = remote_address_masked (memaddr);
6716 p += hexnumstr (p, (ULONGEST) memaddr);
6718 p += hexnumstr (p, (ULONGEST) todo);
6721 getpkt (&rs->buf, &rs->buf_size, 0);
6722 if (rs->buf[0] == 'E'
6723 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6724 && rs->buf[3] == '\0')
6726 /* There is no correspondance between what the remote protocol
6727 uses for errors and errno codes. We would like a cleaner way
6728 of representing errors (big enough to include errno codes,
6729 bfd_error codes, and others). But for now just return
6734 /* Reply describes memory byte by byte, each byte encoded as two hex
6737 i = hex2bin (p, myaddr, todo);
6738 /* Return what we have. Let higher layers handle partial reads. */
6743 /* Remote notification handler. */
6746 handle_notification (char *buf, size_t length)
6748 if (strncmp (buf, "Stop:", 5) == 0)
6750 if (pending_stop_reply)
6752 /* We've already parsed the in-flight stop-reply, but the
6753 stub for some reason thought we didn't, possibly due to
6754 timeout on its side. Just ignore it. */
6756 fprintf_unfiltered (gdb_stdlog, "ignoring resent notification\n");
6760 struct cleanup *old_chain;
6761 struct stop_reply *reply = stop_reply_xmalloc ();
6763 old_chain = make_cleanup (do_stop_reply_xfree, reply);
6765 remote_parse_stop_reply (buf + 5, reply);
6767 discard_cleanups (old_chain);
6769 /* Be careful to only set it after parsing, since an error
6770 may be thrown then. */
6771 pending_stop_reply = reply;
6773 /* Notify the event loop there's a stop reply to acknowledge
6774 and that there may be more events to fetch. */
6775 mark_async_event_handler (remote_async_get_pending_events_token);
6778 fprintf_unfiltered (gdb_stdlog, "stop notification captured\n");
6782 /* We ignore notifications we don't recognize, for compatibility
6783 with newer stubs. */
6788 /* Read or write LEN bytes from inferior memory at MEMADDR,
6789 transferring to or from debugger address BUFFER. Write to inferior
6790 if SHOULD_WRITE is nonzero. Returns length of data written or
6791 read; 0 for error. TARGET is unused. */
6794 remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
6795 int should_write, struct mem_attrib *attrib,
6796 struct target_ops *target)
6800 set_remote_traceframe ();
6801 set_general_thread (inferior_ptid);
6804 res = remote_write_bytes (mem_addr, buffer, mem_len);
6806 res = remote_read_bytes (mem_addr, buffer, mem_len);
6811 /* Sends a packet with content determined by the printf format string
6812 FORMAT and the remaining arguments, then gets the reply. Returns
6813 whether the packet was a success, a failure, or unknown. */
6815 static enum packet_result
6816 remote_send_printf (const char *format, ...)
6818 struct remote_state *rs = get_remote_state ();
6819 int max_size = get_remote_packet_size ();
6822 va_start (ap, format);
6825 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
6826 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
6828 if (putpkt (rs->buf) < 0)
6829 error (_("Communication problem with target."));
6832 getpkt (&rs->buf, &rs->buf_size, 0);
6834 return packet_check_result (rs->buf);
6838 restore_remote_timeout (void *p)
6840 int value = *(int *)p;
6842 remote_timeout = value;
6845 /* Flash writing can take quite some time. We'll set
6846 effectively infinite timeout for flash operations.
6847 In future, we'll need to decide on a better approach. */
6848 static const int remote_flash_timeout = 1000;
6851 remote_flash_erase (struct target_ops *ops,
6852 ULONGEST address, LONGEST length)
6854 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
6855 int saved_remote_timeout = remote_timeout;
6856 enum packet_result ret;
6857 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6858 &saved_remote_timeout);
6860 remote_timeout = remote_flash_timeout;
6862 ret = remote_send_printf ("vFlashErase:%s,%s",
6863 phex (address, addr_size),
6867 case PACKET_UNKNOWN:
6868 error (_("Remote target does not support flash erase"));
6870 error (_("Error erasing flash with vFlashErase packet"));
6875 do_cleanups (back_to);
6879 remote_flash_write (struct target_ops *ops,
6880 ULONGEST address, LONGEST length,
6881 const gdb_byte *data)
6883 int saved_remote_timeout = remote_timeout;
6885 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6886 &saved_remote_timeout);
6888 remote_timeout = remote_flash_timeout;
6889 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0);
6890 do_cleanups (back_to);
6896 remote_flash_done (struct target_ops *ops)
6898 int saved_remote_timeout = remote_timeout;
6900 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6901 &saved_remote_timeout);
6903 remote_timeout = remote_flash_timeout;
6904 ret = remote_send_printf ("vFlashDone");
6905 do_cleanups (back_to);
6909 case PACKET_UNKNOWN:
6910 error (_("Remote target does not support vFlashDone"));
6912 error (_("Error finishing flash operation"));
6919 remote_files_info (struct target_ops *ignore)
6921 puts_filtered ("Debugging a target over a serial line.\n");
6924 /* Stuff for dealing with the packets which are part of this protocol.
6925 See comment at top of file for details. */
6927 /* Read a single character from the remote end. */
6930 readchar (int timeout)
6934 ch = serial_readchar (remote_desc, timeout);
6939 switch ((enum serial_rc) ch)
6943 error (_("Remote connection closed"));
6947 perror_with_name (_("Remote communication error. "
6948 "Target disconnected."));
6950 case SERIAL_TIMEOUT:
6956 /* Send the command in *BUF to the remote machine, and read the reply
6957 into *BUF. Report an error if we get an error reply. Resize
6958 *BUF using xrealloc if necessary to hold the result, and update
6962 remote_send (char **buf,
6966 getpkt (buf, sizeof_buf, 0);
6968 if ((*buf)[0] == 'E')
6969 error (_("Remote failure reply: %s"), *buf);
6972 /* Return a pointer to an xmalloc'ed string representing an escaped
6973 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6974 etc. The caller is responsible for releasing the returned
6978 escape_buffer (const char *buf, int n)
6980 struct cleanup *old_chain;
6981 struct ui_file *stb;
6984 stb = mem_fileopen ();
6985 old_chain = make_cleanup_ui_file_delete (stb);
6987 fputstrn_unfiltered (buf, n, 0, stb);
6988 str = ui_file_xstrdup (stb, NULL);
6989 do_cleanups (old_chain);
6993 /* Display a null-terminated packet on stdout, for debugging, using C
6997 print_packet (char *buf)
6999 puts_filtered ("\"");
7000 fputstr_filtered (buf, '"', gdb_stdout);
7001 puts_filtered ("\"");
7007 return putpkt_binary (buf, strlen (buf));
7010 /* Send a packet to the remote machine, with error checking. The data
7011 of the packet is in BUF. The string in BUF can be at most
7012 get_remote_packet_size () - 5 to account for the $, # and checksum,
7013 and for a possible /0 if we are debugging (remote_debug) and want
7014 to print the sent packet as a string. */
7017 putpkt_binary (char *buf, int cnt)
7019 struct remote_state *rs = get_remote_state ();
7021 unsigned char csum = 0;
7022 char *buf2 = alloca (cnt + 6);
7028 /* Catch cases like trying to read memory or listing threads while
7029 we're waiting for a stop reply. The remote server wouldn't be
7030 ready to handle this request, so we'd hang and timeout. We don't
7031 have to worry about this in synchronous mode, because in that
7032 case it's not possible to issue a command while the target is
7033 running. This is not a problem in non-stop mode, because in that
7034 case, the stub is always ready to process serial input. */
7035 if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
7036 error (_("Cannot execute this command while the target is running."));
7038 /* We're sending out a new packet. Make sure we don't look at a
7039 stale cached response. */
7040 rs->cached_wait_status = 0;
7042 /* Copy the packet into buffer BUF2, encapsulating it
7043 and giving it a checksum. */
7048 for (i = 0; i < cnt; i++)
7054 *p++ = tohex ((csum >> 4) & 0xf);
7055 *p++ = tohex (csum & 0xf);
7057 /* Send it over and over until we get a positive ack. */
7061 int started_error_output = 0;
7065 struct cleanup *old_chain;
7069 str = escape_buffer (buf2, p - buf2);
7070 old_chain = make_cleanup (xfree, str);
7071 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
7072 gdb_flush (gdb_stdlog);
7073 do_cleanups (old_chain);
7075 if (serial_write (remote_desc, buf2, p - buf2))
7076 perror_with_name (_("putpkt: write failed"));
7078 /* If this is a no acks version of the remote protocol, send the
7079 packet and move on. */
7083 /* Read until either a timeout occurs (-2) or '+' is read.
7084 Handle any notification that arrives in the mean time. */
7087 ch = readchar (remote_timeout);
7095 case SERIAL_TIMEOUT:
7098 if (started_error_output)
7100 putchar_unfiltered ('\n');
7101 started_error_output = 0;
7110 fprintf_unfiltered (gdb_stdlog, "Ack\n");
7114 fprintf_unfiltered (gdb_stdlog, "Nak\n");
7116 case SERIAL_TIMEOUT:
7120 break; /* Retransmit buffer. */
7124 fprintf_unfiltered (gdb_stdlog,
7125 "Packet instead of Ack, ignoring it\n");
7126 /* It's probably an old response sent because an ACK
7127 was lost. Gobble up the packet and ack it so it
7128 doesn't get retransmitted when we resend this
7131 serial_write (remote_desc, "+", 1);
7132 continue; /* Now, go look for +. */
7139 /* If we got a notification, handle it, and go back to looking
7141 /* We've found the start of a notification. Now
7142 collect the data. */
7143 val = read_frame (&rs->buf, &rs->buf_size);
7148 struct cleanup *old_chain;
7151 str = escape_buffer (rs->buf, val);
7152 old_chain = make_cleanup (xfree, str);
7153 fprintf_unfiltered (gdb_stdlog,
7154 " Notification received: %s\n",
7156 do_cleanups (old_chain);
7158 handle_notification (rs->buf, val);
7159 /* We're in sync now, rewait for the ack. */
7166 if (!started_error_output)
7168 started_error_output = 1;
7169 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7171 fputc_unfiltered (ch & 0177, gdb_stdlog);
7172 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7181 if (!started_error_output)
7183 started_error_output = 1;
7184 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7186 fputc_unfiltered (ch & 0177, gdb_stdlog);
7190 break; /* Here to retransmit. */
7194 /* This is wrong. If doing a long backtrace, the user should be
7195 able to get out next time we call QUIT, without anything as
7196 violent as interrupt_query. If we want to provide a way out of
7197 here without getting to the next QUIT, it should be based on
7198 hitting ^C twice as in remote_wait. */
7209 /* Come here after finding the start of a frame when we expected an
7210 ack. Do our best to discard the rest of this packet. */
7219 c = readchar (remote_timeout);
7222 case SERIAL_TIMEOUT:
7223 /* Nothing we can do. */
7226 /* Discard the two bytes of checksum and stop. */
7227 c = readchar (remote_timeout);
7229 c = readchar (remote_timeout);
7232 case '*': /* Run length encoding. */
7233 /* Discard the repeat count. */
7234 c = readchar (remote_timeout);
7239 /* A regular character. */
7245 /* Come here after finding the start of the frame. Collect the rest
7246 into *BUF, verifying the checksum, length, and handling run-length
7247 compression. NUL terminate the buffer. If there is not enough room,
7248 expand *BUF using xrealloc.
7250 Returns -1 on error, number of characters in buffer (ignoring the
7251 trailing NULL) on success. (could be extended to return one of the
7252 SERIAL status indications). */
7255 read_frame (char **buf_p,
7262 struct remote_state *rs = get_remote_state ();
7269 c = readchar (remote_timeout);
7272 case SERIAL_TIMEOUT:
7274 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
7278 fputs_filtered ("Saw new packet start in middle of old one\n",
7280 return -1; /* Start a new packet, count retries. */
7283 unsigned char pktcsum;
7289 check_0 = readchar (remote_timeout);
7291 check_1 = readchar (remote_timeout);
7293 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7296 fputs_filtered ("Timeout in checksum, retrying\n",
7300 else if (check_0 < 0 || check_1 < 0)
7303 fputs_filtered ("Communication error in checksum\n",
7308 /* Don't recompute the checksum; with no ack packets we
7309 don't have any way to indicate a packet retransmission
7314 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
7315 if (csum == pktcsum)
7320 struct cleanup *old_chain;
7323 str = escape_buffer (buf, bc);
7324 old_chain = make_cleanup (xfree, str);
7325 fprintf_unfiltered (gdb_stdlog,
7326 "Bad checksum, sentsum=0x%x, "
7327 "csum=0x%x, buf=%s\n",
7328 pktcsum, csum, str);
7329 do_cleanups (old_chain);
7331 /* Number of characters in buffer ignoring trailing
7335 case '*': /* Run length encoding. */
7340 c = readchar (remote_timeout);
7342 repeat = c - ' ' + 3; /* Compute repeat count. */
7344 /* The character before ``*'' is repeated. */
7346 if (repeat > 0 && repeat <= 255 && bc > 0)
7348 if (bc + repeat - 1 >= *sizeof_buf - 1)
7350 /* Make some more room in the buffer. */
7351 *sizeof_buf += repeat;
7352 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7356 memset (&buf[bc], buf[bc - 1], repeat);
7362 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
7366 if (bc >= *sizeof_buf - 1)
7368 /* Make some more room in the buffer. */
7370 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7381 /* Read a packet from the remote machine, with error checking, and
7382 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7383 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7384 rather than timing out; this is used (in synchronous mode) to wait
7385 for a target that is is executing user code to stop. */
7386 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7387 don't have to change all the calls to getpkt to deal with the
7388 return value, because at the moment I don't know what the right
7389 thing to do it for those. */
7397 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7401 /* Read a packet from the remote machine, with error checking, and
7402 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7403 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7404 rather than timing out; this is used (in synchronous mode) to wait
7405 for a target that is is executing user code to stop. If FOREVER ==
7406 0, this function is allowed to time out gracefully and return an
7407 indication of this to the caller. Otherwise return the number of
7408 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7409 enough reason to return to the caller. */
7412 getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7413 int expecting_notif)
7415 struct remote_state *rs = get_remote_state ();
7421 /* We're reading a new response. Make sure we don't look at a
7422 previously cached response. */
7423 rs->cached_wait_status = 0;
7425 strcpy (*buf, "timeout");
7428 timeout = watchdog > 0 ? watchdog : -1;
7429 else if (expecting_notif)
7430 timeout = 0; /* There should already be a char in the buffer. If
7433 timeout = remote_timeout;
7437 /* Process any number of notifications, and then return when
7441 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7443 for (tries = 1; tries <= MAX_TRIES; tries++)
7445 /* This can loop forever if the remote side sends us
7446 characters continuously, but if it pauses, we'll get
7447 SERIAL_TIMEOUT from readchar because of timeout. Then
7448 we'll count that as a retry.
7450 Note that even when forever is set, we will only wait
7451 forever prior to the start of a packet. After that, we
7452 expect characters to arrive at a brisk pace. They should
7453 show up within remote_timeout intervals. */
7455 c = readchar (timeout);
7456 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
7458 if (c == SERIAL_TIMEOUT)
7460 if (expecting_notif)
7461 return -1; /* Don't complain, it's normal to not get
7462 anything in this case. */
7464 if (forever) /* Watchdog went off? Kill the target. */
7468 error (_("Watchdog timeout has expired. Target detached."));
7471 fputs_filtered ("Timed out.\n", gdb_stdlog);
7475 /* We've found the start of a packet or notification.
7476 Now collect the data. */
7477 val = read_frame (buf, sizeof_buf);
7482 serial_write (remote_desc, "-", 1);
7485 if (tries > MAX_TRIES)
7487 /* We have tried hard enough, and just can't receive the
7488 packet/notification. Give up. */
7489 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7491 /* Skip the ack char if we're in no-ack mode. */
7492 if (!rs->noack_mode)
7493 serial_write (remote_desc, "+", 1);
7497 /* If we got an ordinary packet, return that to our caller. */
7502 struct cleanup *old_chain;
7505 str = escape_buffer (*buf, val);
7506 old_chain = make_cleanup (xfree, str);
7507 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7508 do_cleanups (old_chain);
7511 /* Skip the ack char if we're in no-ack mode. */
7512 if (!rs->noack_mode)
7513 serial_write (remote_desc, "+", 1);
7517 /* If we got a notification, handle it, and go back to looking
7521 gdb_assert (c == '%');
7525 struct cleanup *old_chain;
7528 str = escape_buffer (*buf, val);
7529 old_chain = make_cleanup (xfree, str);
7530 fprintf_unfiltered (gdb_stdlog,
7531 " Notification received: %s\n",
7533 do_cleanups (old_chain);
7536 handle_notification (*buf, val);
7538 /* Notifications require no acknowledgement. */
7540 if (expecting_notif)
7547 getpkt_sane (char **buf, long *sizeof_buf, int forever)
7549 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0);
7553 getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever)
7555 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1);
7559 /* A helper function that just calls putpkt; for type correctness. */
7562 putpkt_for_catch_errors (void *arg)
7564 return putpkt (arg);
7568 remote_kill (struct target_ops *ops)
7570 /* Use catch_errors so the user can quit from gdb even when we
7571 aren't on speaking terms with the remote system. */
7572 catch_errors (putpkt_for_catch_errors, "k", "", RETURN_MASK_ERROR);
7574 /* Don't wait for it to die. I'm not really sure it matters whether
7575 we do or not. For the existing stubs, kill is a noop. */
7576 target_mourn_inferior ();
7580 remote_vkill (int pid, struct remote_state *rs)
7582 if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7585 /* Tell the remote target to detach. */
7586 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
7588 getpkt (&rs->buf, &rs->buf_size, 0);
7590 if (packet_ok (rs->buf,
7591 &remote_protocol_packets[PACKET_vKill]) == PACKET_OK)
7593 else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7600 extended_remote_kill (struct target_ops *ops)
7603 int pid = ptid_get_pid (inferior_ptid);
7604 struct remote_state *rs = get_remote_state ();
7606 res = remote_vkill (pid, rs);
7607 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
7609 /* Don't try 'k' on a multi-process aware stub -- it has no way
7610 to specify the pid. */
7614 getpkt (&rs->buf, &rs->buf_size, 0);
7615 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7618 /* Don't wait for it to die. I'm not really sure it matters whether
7619 we do or not. For the existing stubs, kill is a noop. */
7625 error (_("Can't kill process"));
7627 target_mourn_inferior ();
7631 remote_mourn (struct target_ops *ops)
7633 remote_mourn_1 (ops);
7636 /* Worker function for remote_mourn. */
7638 remote_mourn_1 (struct target_ops *target)
7640 unpush_target (target);
7642 /* remote_close takes care of doing most of the clean up. */
7643 generic_mourn_inferior ();
7647 extended_remote_mourn_1 (struct target_ops *target)
7649 struct remote_state *rs = get_remote_state ();
7651 /* In case we got here due to an error, but we're going to stay
7653 rs->waiting_for_stop_reply = 0;
7655 /* We're no longer interested in these events. */
7656 discard_pending_stop_replies (ptid_get_pid (inferior_ptid));
7658 /* If the current general thread belonged to the process we just
7659 detached from or has exited, the remote side current general
7660 thread becomes undefined. Considering a case like this:
7662 - We just got here due to a detach.
7663 - The process that we're detaching from happens to immediately
7664 report a global breakpoint being hit in non-stop mode, in the
7665 same thread we had selected before.
7666 - GDB attaches to this process again.
7667 - This event happens to be the next event we handle.
7669 GDB would consider that the current general thread didn't need to
7670 be set on the stub side (with Hg), since for all it knew,
7671 GENERAL_THREAD hadn't changed.
7673 Notice that although in all-stop mode, the remote server always
7674 sets the current thread to the thread reporting the stop event,
7675 that doesn't happen in non-stop mode; in non-stop, the stub *must
7676 not* change the current thread when reporting a breakpoint hit,
7677 due to the decoupling of event reporting and event handling.
7679 To keep things simple, we always invalidate our notion of the
7681 record_currthread (minus_one_ptid);
7683 /* Unlike "target remote", we do not want to unpush the target; then
7684 the next time the user says "run", we won't be connected. */
7686 /* Call common code to mark the inferior as not running. */
7687 generic_mourn_inferior ();
7689 if (!have_inferiors ())
7691 if (!remote_multi_process_p (rs))
7693 /* Check whether the target is running now - some remote stubs
7694 automatically restart after kill. */
7696 getpkt (&rs->buf, &rs->buf_size, 0);
7698 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7700 /* Assume that the target has been restarted. Set
7701 inferior_ptid so that bits of core GDB realizes
7702 there's something here, e.g., so that the user can
7703 say "kill" again. */
7704 inferior_ptid = magic_null_ptid;
7711 extended_remote_mourn (struct target_ops *ops)
7713 extended_remote_mourn_1 (ops);
7717 extended_remote_supports_disable_randomization (void)
7719 return (remote_protocol_packets[PACKET_QDisableRandomization].support
7724 extended_remote_disable_randomization (int val)
7726 struct remote_state *rs = get_remote_state ();
7729 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7732 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7734 error (_("Target does not support QDisableRandomization."));
7735 if (strcmp (reply, "OK") != 0)
7736 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7740 extended_remote_run (char *args)
7742 struct remote_state *rs = get_remote_state ();
7745 /* If the user has disabled vRun support, or we have detected that
7746 support is not available, do not try it. */
7747 if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7750 strcpy (rs->buf, "vRun;");
7751 len = strlen (rs->buf);
7753 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7754 error (_("Remote file name too long for run packet"));
7755 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0);
7757 gdb_assert (args != NULL);
7760 struct cleanup *back_to;
7764 argv = gdb_buildargv (args);
7765 back_to = make_cleanup ((void (*) (void *)) freeargv, argv);
7766 for (i = 0; argv[i] != NULL; i++)
7768 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
7769 error (_("Argument list too long for run packet"));
7770 rs->buf[len++] = ';';
7771 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0);
7773 do_cleanups (back_to);
7776 rs->buf[len++] = '\0';
7779 getpkt (&rs->buf, &rs->buf_size, 0);
7781 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK)
7783 /* We have a wait response; we don't need it, though. All is well. */
7786 else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7787 /* It wasn't disabled before, but it is now. */
7791 if (remote_exec_file[0] == '\0')
7792 error (_("Running the default executable on the remote target failed; "
7793 "try \"set remote exec-file\"?"));
7795 error (_("Running \"%s\" on the remote target failed"),
7800 /* In the extended protocol we want to be able to do things like
7801 "run" and have them basically work as expected. So we need
7802 a special create_inferior function. We support changing the
7803 executable file and the command line arguments, but not the
7807 extended_remote_create_inferior_1 (char *exec_file, char *args,
7808 char **env, int from_tty)
7810 /* If running asynchronously, register the target file descriptor
7811 with the event loop. */
7812 if (target_can_async_p ())
7813 target_async (inferior_event_handler, 0);
7815 /* Disable address space randomization if requested (and supported). */
7816 if (extended_remote_supports_disable_randomization ())
7817 extended_remote_disable_randomization (disable_randomization);
7819 /* Now restart the remote server. */
7820 if (extended_remote_run (args) == -1)
7822 /* vRun was not supported. Fail if we need it to do what the
7824 if (remote_exec_file[0])
7825 error (_("Remote target does not support \"set remote exec-file\""));
7827 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7829 /* Fall back to "R". */
7830 extended_remote_restart ();
7833 if (!have_inferiors ())
7835 /* Clean up from the last time we ran, before we mark the target
7836 running again. This will mark breakpoints uninserted, and
7837 get_offsets may insert breakpoints. */
7838 init_thread_list ();
7839 init_wait_for_inferior ();
7842 add_current_inferior_and_thread ();
7844 /* Get updated offsets, if the stub uses qOffsets. */
7849 extended_remote_create_inferior (struct target_ops *ops,
7850 char *exec_file, char *args,
7851 char **env, int from_tty)
7853 extended_remote_create_inferior_1 (exec_file, args, env, from_tty);
7857 /* Given a location's target info BP_TGT and the packet buffer BUF, output
7858 the list of conditions (in agent expression bytecode format), if any, the
7859 target needs to evaluate. The output is placed into the packet buffer
7860 started from BUF and ended at BUF_END. */
7863 remote_add_target_side_condition (struct gdbarch *gdbarch,
7864 struct bp_target_info *bp_tgt, char *buf,
7867 struct agent_expr *aexpr = NULL;
7870 char *buf_start = buf;
7872 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
7875 buf += strlen (buf);
7876 xsnprintf (buf, buf_end - buf, "%s", ";");
7879 /* Send conditions to the target and free the vector. */
7881 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
7884 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
7885 buf += strlen (buf);
7886 for (i = 0; i < aexpr->len; ++i)
7887 buf = pack_hex_byte (buf, aexpr->buf[i]);
7891 VEC_free (agent_expr_p, bp_tgt->conditions);
7896 remote_add_target_side_commands (struct gdbarch *gdbarch,
7897 struct bp_target_info *bp_tgt, char *buf)
7899 struct agent_expr *aexpr = NULL;
7902 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
7905 buf += strlen (buf);
7907 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
7908 buf += strlen (buf);
7910 /* Concatenate all the agent expressions that are commands into the
7913 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
7916 sprintf (buf, "X%x,", aexpr->len);
7917 buf += strlen (buf);
7918 for (i = 0; i < aexpr->len; ++i)
7919 buf = pack_hex_byte (buf, aexpr->buf[i]);
7923 VEC_free (agent_expr_p, bp_tgt->tcommands);
7926 /* Insert a breakpoint. On targets that have software breakpoint
7927 support, we ask the remote target to do the work; on targets
7928 which don't, we insert a traditional memory breakpoint. */
7931 remote_insert_breakpoint (struct gdbarch *gdbarch,
7932 struct bp_target_info *bp_tgt)
7934 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7935 If it succeeds, then set the support to PACKET_ENABLE. If it
7936 fails, and the user has explicitly requested the Z support then
7937 report an error, otherwise, mark it disabled and go on. */
7939 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
7941 CORE_ADDR addr = bp_tgt->placed_address;
7942 struct remote_state *rs;
7945 struct condition_list *cond = NULL;
7947 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
7949 rs = get_remote_state ();
7951 endbuf = rs->buf + get_remote_packet_size ();
7956 addr = (ULONGEST) remote_address_masked (addr);
7957 p += hexnumstr (p, addr);
7958 xsnprintf (p, endbuf - p, ",%d", bpsize);
7960 if (remote_supports_cond_breakpoints ())
7961 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
7963 if (remote_can_run_breakpoint_commands ())
7964 remote_add_target_side_commands (gdbarch, bp_tgt, p);
7967 getpkt (&rs->buf, &rs->buf_size, 0);
7969 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
7974 bp_tgt->placed_address = addr;
7975 bp_tgt->placed_size = bpsize;
7977 case PACKET_UNKNOWN:
7982 return memory_insert_breakpoint (gdbarch, bp_tgt);
7986 remote_remove_breakpoint (struct gdbarch *gdbarch,
7987 struct bp_target_info *bp_tgt)
7989 CORE_ADDR addr = bp_tgt->placed_address;
7990 struct remote_state *rs = get_remote_state ();
7992 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
7995 char *endbuf = rs->buf + get_remote_packet_size ();
8001 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8002 p += hexnumstr (p, addr);
8003 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
8006 getpkt (&rs->buf, &rs->buf_size, 0);
8008 return (rs->buf[0] == 'E');
8011 return memory_remove_breakpoint (gdbarch, bp_tgt);
8015 watchpoint_to_Z_packet (int type)
8020 return Z_PACKET_WRITE_WP;
8023 return Z_PACKET_READ_WP;
8026 return Z_PACKET_ACCESS_WP;
8029 internal_error (__FILE__, __LINE__,
8030 _("hw_bp_to_z: bad watchpoint type %d"), type);
8035 remote_insert_watchpoint (CORE_ADDR addr, int len, int type,
8036 struct expression *cond)
8038 struct remote_state *rs = get_remote_state ();
8039 char *endbuf = rs->buf + get_remote_packet_size ();
8041 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8043 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
8046 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
8047 p = strchr (rs->buf, '\0');
8048 addr = remote_address_masked (addr);
8049 p += hexnumstr (p, (ULONGEST) addr);
8050 xsnprintf (p, endbuf - p, ",%x", len);
8053 getpkt (&rs->buf, &rs->buf_size, 0);
8055 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8059 case PACKET_UNKNOWN:
8064 internal_error (__FILE__, __LINE__,
8065 _("remote_insert_watchpoint: reached end of function"));
8069 remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8070 CORE_ADDR start, int length)
8072 CORE_ADDR diff = remote_address_masked (addr - start);
8074 return diff < length;
8079 remote_remove_watchpoint (CORE_ADDR addr, int len, int type,
8080 struct expression *cond)
8082 struct remote_state *rs = get_remote_state ();
8083 char *endbuf = rs->buf + get_remote_packet_size ();
8085 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8087 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
8090 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
8091 p = strchr (rs->buf, '\0');
8092 addr = remote_address_masked (addr);
8093 p += hexnumstr (p, (ULONGEST) addr);
8094 xsnprintf (p, endbuf - p, ",%x", len);
8096 getpkt (&rs->buf, &rs->buf_size, 0);
8098 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
8101 case PACKET_UNKNOWN:
8106 internal_error (__FILE__, __LINE__,
8107 _("remote_remove_watchpoint: reached end of function"));
8111 int remote_hw_watchpoint_limit = -1;
8112 int remote_hw_watchpoint_length_limit = -1;
8113 int remote_hw_breakpoint_limit = -1;
8116 remote_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
8118 if (remote_hw_watchpoint_length_limit == 0)
8120 else if (remote_hw_watchpoint_length_limit < 0)
8122 else if (len <= remote_hw_watchpoint_length_limit)
8129 remote_check_watch_resources (int type, int cnt, int ot)
8131 if (type == bp_hardware_breakpoint)
8133 if (remote_hw_breakpoint_limit == 0)
8135 else if (remote_hw_breakpoint_limit < 0)
8137 else if (cnt <= remote_hw_breakpoint_limit)
8142 if (remote_hw_watchpoint_limit == 0)
8144 else if (remote_hw_watchpoint_limit < 0)
8148 else if (cnt <= remote_hw_watchpoint_limit)
8155 remote_stopped_by_watchpoint (void)
8157 return remote_stopped_by_watchpoint_p;
8161 remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
8165 if (remote_stopped_by_watchpoint ())
8167 *addr_p = remote_watch_data_address;
8176 remote_insert_hw_breakpoint (struct gdbarch *gdbarch,
8177 struct bp_target_info *bp_tgt)
8180 struct remote_state *rs;
8183 /* The length field should be set to the size of a breakpoint
8184 instruction, even though we aren't inserting one ourselves. */
8186 gdbarch_remote_breakpoint_from_pc
8187 (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
8189 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
8192 rs = get_remote_state ();
8194 endbuf = rs->buf + get_remote_packet_size ();
8200 addr = remote_address_masked (bp_tgt->placed_address);
8201 p += hexnumstr (p, (ULONGEST) addr);
8202 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8204 if (remote_supports_cond_breakpoints ())
8205 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8207 if (remote_can_run_breakpoint_commands ())
8208 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8211 getpkt (&rs->buf, &rs->buf_size, 0);
8213 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8216 case PACKET_UNKNOWN:
8221 internal_error (__FILE__, __LINE__,
8222 _("remote_insert_hw_breakpoint: reached end of function"));
8227 remote_remove_hw_breakpoint (struct gdbarch *gdbarch,
8228 struct bp_target_info *bp_tgt)
8231 struct remote_state *rs = get_remote_state ();
8233 char *endbuf = rs->buf + get_remote_packet_size ();
8235 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
8242 addr = remote_address_masked (bp_tgt->placed_address);
8243 p += hexnumstr (p, (ULONGEST) addr);
8244 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8247 getpkt (&rs->buf, &rs->buf_size, 0);
8249 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
8252 case PACKET_UNKNOWN:
8257 internal_error (__FILE__, __LINE__,
8258 _("remote_remove_hw_breakpoint: reached end of function"));
8261 /* Table used by the crc32 function to calcuate the checksum. */
8263 static unsigned long crc32_table[256] =
8266 static unsigned long
8267 crc32 (const unsigned char *buf, int len, unsigned int crc)
8269 if (!crc32_table[1])
8271 /* Initialize the CRC table and the decoding table. */
8275 for (i = 0; i < 256; i++)
8277 for (c = i << 24, j = 8; j > 0; --j)
8278 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
8285 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
8291 /* Verify memory using the "qCRC:" request. */
8294 remote_verify_memory (struct target_ops *ops,
8295 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8297 struct remote_state *rs = get_remote_state ();
8298 unsigned long host_crc, target_crc;
8301 /* FIXME: assumes lma can fit into long. */
8302 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8303 (long) lma, (long) size);
8306 /* Be clever; compute the host_crc before waiting for target
8308 host_crc = crc32 (data, size, 0xffffffff);
8310 getpkt (&rs->buf, &rs->buf_size, 0);
8311 if (rs->buf[0] == 'E')
8314 if (rs->buf[0] != 'C')
8315 error (_("remote target does not support this operation"));
8317 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8318 target_crc = target_crc * 16 + fromhex (*tmp);
8320 return (host_crc == target_crc);
8323 /* compare-sections command
8325 With no arguments, compares each loadable section in the exec bfd
8326 with the same memory range on the target, and reports mismatches.
8327 Useful for verifying the image on the target against the exec file. */
8330 compare_sections_command (char *args, int from_tty)
8333 struct cleanup *old_chain;
8335 const char *sectname;
8343 error (_("command cannot be used without an exec file"));
8345 for (s = exec_bfd->sections; s; s = s->next)
8347 if (!(s->flags & SEC_LOAD))
8348 continue; /* Skip non-loadable section. */
8350 size = bfd_get_section_size (s);
8352 continue; /* Skip zero-length section. */
8354 sectname = bfd_get_section_name (exec_bfd, s);
8355 if (args && strcmp (args, sectname) != 0)
8356 continue; /* Not the section selected by user. */
8358 matched = 1; /* Do this section. */
8361 sectdata = xmalloc (size);
8362 old_chain = make_cleanup (xfree, sectdata);
8363 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
8365 res = target_verify_memory (sectdata, lma, size);
8368 error (_("target memory fault, section %s, range %s -- %s"), sectname,
8369 paddress (target_gdbarch, lma),
8370 paddress (target_gdbarch, lma + size));
8372 printf_filtered ("Section %s, range %s -- %s: ", sectname,
8373 paddress (target_gdbarch, lma),
8374 paddress (target_gdbarch, lma + size));
8376 printf_filtered ("matched.\n");
8379 printf_filtered ("MIS-MATCHED!\n");
8383 do_cleanups (old_chain);
8386 warning (_("One or more sections of the remote executable does not match\n\
8387 the loaded file\n"));
8388 if (args && !matched)
8389 printf_filtered (_("No loaded section named '%s'.\n"), args);
8392 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8393 into remote target. The number of bytes written to the remote
8394 target is returned, or -1 for error. */
8397 remote_write_qxfer (struct target_ops *ops, const char *object_name,
8398 const char *annex, const gdb_byte *writebuf,
8399 ULONGEST offset, LONGEST len,
8400 struct packet_config *packet)
8404 struct remote_state *rs = get_remote_state ();
8405 int max_size = get_memory_write_packet_size ();
8407 if (packet->support == PACKET_DISABLE)
8410 /* Insert header. */
8411 i = snprintf (rs->buf, max_size,
8412 "qXfer:%s:write:%s:%s:",
8413 object_name, annex ? annex : "",
8414 phex_nz (offset, sizeof offset));
8415 max_size -= (i + 1);
8417 /* Escape as much data as fits into rs->buf. */
8418 buf_len = remote_escape_output
8419 (writebuf, len, (rs->buf + i), &max_size, max_size);
8421 if (putpkt_binary (rs->buf, i + buf_len) < 0
8422 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8423 || packet_ok (rs->buf, packet) != PACKET_OK)
8426 unpack_varlen_hex (rs->buf, &n);
8430 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8431 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8432 number of bytes read is returned, or 0 for EOF, or -1 for error.
8433 The number of bytes read may be less than LEN without indicating an
8434 EOF. PACKET is checked and updated to indicate whether the remote
8435 target supports this object. */
8438 remote_read_qxfer (struct target_ops *ops, const char *object_name,
8440 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8441 struct packet_config *packet)
8443 static char *finished_object;
8444 static char *finished_annex;
8445 static ULONGEST finished_offset;
8447 struct remote_state *rs = get_remote_state ();
8448 LONGEST i, n, packet_len;
8450 if (packet->support == PACKET_DISABLE)
8453 /* Check whether we've cached an end-of-object packet that matches
8455 if (finished_object)
8457 if (strcmp (object_name, finished_object) == 0
8458 && strcmp (annex ? annex : "", finished_annex) == 0
8459 && offset == finished_offset)
8462 /* Otherwise, we're now reading something different. Discard
8464 xfree (finished_object);
8465 xfree (finished_annex);
8466 finished_object = NULL;
8467 finished_annex = NULL;
8470 /* Request only enough to fit in a single packet. The actual data
8471 may not, since we don't know how much of it will need to be escaped;
8472 the target is free to respond with slightly less data. We subtract
8473 five to account for the response type and the protocol frame. */
8474 n = min (get_remote_packet_size () - 5, len);
8475 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8476 object_name, annex ? annex : "",
8477 phex_nz (offset, sizeof offset),
8478 phex_nz (n, sizeof n));
8479 i = putpkt (rs->buf);
8484 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8485 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8488 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8489 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8491 /* 'm' means there is (or at least might be) more data after this
8492 batch. That does not make sense unless there's at least one byte
8493 of data in this reply. */
8494 if (rs->buf[0] == 'm' && packet_len == 1)
8495 error (_("Remote qXfer reply contained no data."));
8497 /* Got some data. */
8498 i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
8500 /* 'l' is an EOF marker, possibly including a final block of data,
8501 or possibly empty. If we have the final block of a non-empty
8502 object, record this fact to bypass a subsequent partial read. */
8503 if (rs->buf[0] == 'l' && offset + i > 0)
8505 finished_object = xstrdup (object_name);
8506 finished_annex = xstrdup (annex ? annex : "");
8507 finished_offset = offset + i;
8514 remote_xfer_partial (struct target_ops *ops, enum target_object object,
8515 const char *annex, gdb_byte *readbuf,
8516 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
8518 struct remote_state *rs;
8523 set_remote_traceframe ();
8524 set_general_thread (inferior_ptid);
8526 rs = get_remote_state ();
8528 /* Handle memory using the standard memory routines. */
8529 if (object == TARGET_OBJECT_MEMORY)
8535 /* If the remote target is connected but not running, we should
8536 pass this request down to a lower stratum (e.g. the executable
8538 if (!target_has_execution)
8541 if (writebuf != NULL)
8542 xfered = remote_write_bytes (offset, writebuf, len);
8544 xfered = remote_read_bytes (offset, readbuf, len);
8548 else if (xfered == 0 && errno == 0)
8554 /* Handle SPU memory using qxfer packets. */
8555 if (object == TARGET_OBJECT_SPU)
8558 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8559 &remote_protocol_packets
8560 [PACKET_qXfer_spu_read]);
8562 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8563 &remote_protocol_packets
8564 [PACKET_qXfer_spu_write]);
8567 /* Handle extra signal info using qxfer packets. */
8568 if (object == TARGET_OBJECT_SIGNAL_INFO)
8571 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8572 &remote_protocol_packets
8573 [PACKET_qXfer_siginfo_read]);
8575 return remote_write_qxfer (ops, "siginfo", annex,
8576 writebuf, offset, len,
8577 &remote_protocol_packets
8578 [PACKET_qXfer_siginfo_write]);
8581 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8584 return remote_read_qxfer (ops, "statictrace", annex,
8585 readbuf, offset, len,
8586 &remote_protocol_packets
8587 [PACKET_qXfer_statictrace_read]);
8592 /* Only handle flash writes. */
8593 if (writebuf != NULL)
8599 case TARGET_OBJECT_FLASH:
8600 xfered = remote_flash_write (ops, offset, len, writebuf);
8604 else if (xfered == 0 && errno == 0)
8614 /* Map pre-existing objects onto letters. DO NOT do this for new
8615 objects!!! Instead specify new query packets. */
8618 case TARGET_OBJECT_AVR:
8622 case TARGET_OBJECT_AUXV:
8623 gdb_assert (annex == NULL);
8624 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8625 &remote_protocol_packets[PACKET_qXfer_auxv]);
8627 case TARGET_OBJECT_AVAILABLE_FEATURES:
8628 return remote_read_qxfer
8629 (ops, "features", annex, readbuf, offset, len,
8630 &remote_protocol_packets[PACKET_qXfer_features]);
8632 case TARGET_OBJECT_LIBRARIES:
8633 return remote_read_qxfer
8634 (ops, "libraries", annex, readbuf, offset, len,
8635 &remote_protocol_packets[PACKET_qXfer_libraries]);
8637 case TARGET_OBJECT_LIBRARIES_SVR4:
8638 return remote_read_qxfer
8639 (ops, "libraries-svr4", annex, readbuf, offset, len,
8640 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8642 case TARGET_OBJECT_MEMORY_MAP:
8643 gdb_assert (annex == NULL);
8644 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8645 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8647 case TARGET_OBJECT_OSDATA:
8648 /* Should only get here if we're connected. */
8649 gdb_assert (remote_desc);
8650 return remote_read_qxfer
8651 (ops, "osdata", annex, readbuf, offset, len,
8652 &remote_protocol_packets[PACKET_qXfer_osdata]);
8654 case TARGET_OBJECT_THREADS:
8655 gdb_assert (annex == NULL);
8656 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8657 &remote_protocol_packets[PACKET_qXfer_threads]);
8659 case TARGET_OBJECT_TRACEFRAME_INFO:
8660 gdb_assert (annex == NULL);
8661 return remote_read_qxfer
8662 (ops, "traceframe-info", annex, readbuf, offset, len,
8663 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8665 case TARGET_OBJECT_FDPIC:
8666 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8667 &remote_protocol_packets[PACKET_qXfer_fdpic]);
8669 case TARGET_OBJECT_OPENVMS_UIB:
8670 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8671 &remote_protocol_packets[PACKET_qXfer_uib]);
8677 /* Note: a zero OFFSET and LEN can be used to query the minimum
8679 if (offset == 0 && len == 0)
8680 return (get_remote_packet_size ());
8681 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8682 large enough let the caller deal with it. */
8683 if (len < get_remote_packet_size ())
8685 len = get_remote_packet_size ();
8687 /* Except for querying the minimum buffer size, target must be open. */
8689 error (_("remote query is only available after target open"));
8691 gdb_assert (annex != NULL);
8692 gdb_assert (readbuf != NULL);
8698 /* We used one buffer char for the remote protocol q command and
8699 another for the query type. As the remote protocol encapsulation
8700 uses 4 chars plus one extra in case we are debugging
8701 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8704 while (annex[i] && (i < (get_remote_packet_size () - 8)))
8706 /* Bad caller may have sent forbidden characters. */
8707 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
8712 gdb_assert (annex[i] == '\0');
8714 i = putpkt (rs->buf);
8718 getpkt (&rs->buf, &rs->buf_size, 0);
8719 strcpy ((char *) readbuf, rs->buf);
8721 return strlen ((char *) readbuf);
8725 remote_search_memory (struct target_ops* ops,
8726 CORE_ADDR start_addr, ULONGEST search_space_len,
8727 const gdb_byte *pattern, ULONGEST pattern_len,
8728 CORE_ADDR *found_addrp)
8730 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
8731 struct remote_state *rs = get_remote_state ();
8732 int max_size = get_memory_write_packet_size ();
8733 struct packet_config *packet =
8734 &remote_protocol_packets[PACKET_qSearch_memory];
8735 /* Number of packet bytes used to encode the pattern;
8736 this could be more than PATTERN_LEN due to escape characters. */
8737 int escaped_pattern_len;
8738 /* Amount of pattern that was encodable in the packet. */
8739 int used_pattern_len;
8742 ULONGEST found_addr;
8744 /* Don't go to the target if we don't have to.
8745 This is done before checking packet->support to avoid the possibility that
8746 a success for this edge case means the facility works in general. */
8747 if (pattern_len > search_space_len)
8749 if (pattern_len == 0)
8751 *found_addrp = start_addr;
8755 /* If we already know the packet isn't supported, fall back to the simple
8756 way of searching memory. */
8758 if (packet->support == PACKET_DISABLE)
8760 /* Target doesn't provided special support, fall back and use the
8761 standard support (copy memory and do the search here). */
8762 return simple_search_memory (ops, start_addr, search_space_len,
8763 pattern, pattern_len, found_addrp);
8766 /* Insert header. */
8767 i = snprintf (rs->buf, max_size,
8768 "qSearch:memory:%s;%s;",
8769 phex_nz (start_addr, addr_size),
8770 phex_nz (search_space_len, sizeof (search_space_len)));
8771 max_size -= (i + 1);
8773 /* Escape as much data as fits into rs->buf. */
8774 escaped_pattern_len =
8775 remote_escape_output (pattern, pattern_len, (rs->buf + i),
8776 &used_pattern_len, max_size);
8778 /* Bail if the pattern is too large. */
8779 if (used_pattern_len != pattern_len)
8780 error (_("Pattern is too large to transmit to remote target."));
8782 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
8783 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8784 || packet_ok (rs->buf, packet) != PACKET_OK)
8786 /* The request may not have worked because the command is not
8787 supported. If so, fall back to the simple way. */
8788 if (packet->support == PACKET_DISABLE)
8790 return simple_search_memory (ops, start_addr, search_space_len,
8791 pattern, pattern_len, found_addrp);
8796 if (rs->buf[0] == '0')
8798 else if (rs->buf[0] == '1')
8801 if (rs->buf[1] != ',')
8802 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8803 unpack_varlen_hex (rs->buf + 2, &found_addr);
8804 *found_addrp = found_addr;
8807 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
8813 remote_rcmd (char *command,
8814 struct ui_file *outbuf)
8816 struct remote_state *rs = get_remote_state ();
8820 error (_("remote rcmd is only available after target open"));
8822 /* Send a NULL command across as an empty command. */
8823 if (command == NULL)
8826 /* The query prefix. */
8827 strcpy (rs->buf, "qRcmd,");
8828 p = strchr (rs->buf, '\0');
8830 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
8831 > get_remote_packet_size ())
8832 error (_("\"monitor\" command ``%s'' is too long."), command);
8834 /* Encode the actual command. */
8835 bin2hex ((gdb_byte *) command, p, 0);
8837 if (putpkt (rs->buf) < 0)
8838 error (_("Communication problem with target."));
8840 /* get/display the response */
8845 /* XXX - see also remote_get_noisy_reply(). */
8846 QUIT; /* Allow user to bail out with ^C. */
8848 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
8850 /* Timeout. Continue to (try to) read responses.
8851 This is better than stopping with an error, assuming the stub
8852 is still executing the (long) monitor command.
8853 If needed, the user can interrupt gdb using C-c, obtaining
8854 an effect similar to stop on timeout. */
8859 error (_("Target does not support this command."));
8860 if (buf[0] == 'O' && buf[1] != 'K')
8862 remote_console_output (buf + 1); /* 'O' message from stub. */
8865 if (strcmp (buf, "OK") == 0)
8867 if (strlen (buf) == 3 && buf[0] == 'E'
8868 && isdigit (buf[1]) && isdigit (buf[2]))
8870 error (_("Protocol error with Rcmd"));
8872 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
8874 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
8876 fputc_unfiltered (c, outbuf);
8882 static VEC(mem_region_s) *
8883 remote_memory_map (struct target_ops *ops)
8885 VEC(mem_region_s) *result = NULL;
8886 char *text = target_read_stralloc (¤t_target,
8887 TARGET_OBJECT_MEMORY_MAP, NULL);
8891 struct cleanup *back_to = make_cleanup (xfree, text);
8893 result = parse_memory_map (text);
8894 do_cleanups (back_to);
8901 packet_command (char *args, int from_tty)
8903 struct remote_state *rs = get_remote_state ();
8906 error (_("command can only be used with remote target"));
8909 error (_("remote-packet command requires packet text as argument"));
8911 puts_filtered ("sending: ");
8912 print_packet (args);
8913 puts_filtered ("\n");
8916 getpkt (&rs->buf, &rs->buf_size, 0);
8917 puts_filtered ("received: ");
8918 print_packet (rs->buf);
8919 puts_filtered ("\n");
8923 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8925 static void display_thread_info (struct gdb_ext_thread_info *info);
8927 static void threadset_test_cmd (char *cmd, int tty);
8929 static void threadalive_test (char *cmd, int tty);
8931 static void threadlist_test_cmd (char *cmd, int tty);
8933 int get_and_display_threadinfo (threadref *ref);
8935 static void threadinfo_test_cmd (char *cmd, int tty);
8937 static int thread_display_step (threadref *ref, void *context);
8939 static void threadlist_update_test_cmd (char *cmd, int tty);
8941 static void init_remote_threadtests (void);
8943 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8946 threadset_test_cmd (char *cmd, int tty)
8948 int sample_thread = SAMPLE_THREAD;
8950 printf_filtered (_("Remote threadset test\n"));
8951 set_general_thread (sample_thread);
8956 threadalive_test (char *cmd, int tty)
8958 int sample_thread = SAMPLE_THREAD;
8959 int pid = ptid_get_pid (inferior_ptid);
8960 ptid_t ptid = ptid_build (pid, 0, sample_thread);
8962 if (remote_thread_alive (ptid))
8963 printf_filtered ("PASS: Thread alive test\n");
8965 printf_filtered ("FAIL: Thread alive test\n");
8968 void output_threadid (char *title, threadref *ref);
8971 output_threadid (char *title, threadref *ref)
8975 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
8977 printf_filtered ("%s %s\n", title, (&hexid[0]));
8981 threadlist_test_cmd (char *cmd, int tty)
8984 threadref nextthread;
8985 int done, result_count;
8986 threadref threadlist[3];
8988 printf_filtered ("Remote Threadlist test\n");
8989 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
8990 &result_count, &threadlist[0]))
8991 printf_filtered ("FAIL: threadlist test\n");
8994 threadref *scan = threadlist;
8995 threadref *limit = scan + result_count;
8997 while (scan < limit)
8998 output_threadid (" thread ", scan++);
9003 display_thread_info (struct gdb_ext_thread_info *info)
9005 output_threadid ("Threadid: ", &info->threadid);
9006 printf_filtered ("Name: %s\n ", info->shortname);
9007 printf_filtered ("State: %s\n", info->display);
9008 printf_filtered ("other: %s\n\n", info->more_display);
9012 get_and_display_threadinfo (threadref *ref)
9016 struct gdb_ext_thread_info threadinfo;
9018 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9019 | TAG_MOREDISPLAY | TAG_DISPLAY;
9020 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9021 display_thread_info (&threadinfo);
9026 threadinfo_test_cmd (char *cmd, int tty)
9028 int athread = SAMPLE_THREAD;
9032 int_to_threadref (&thread, athread);
9033 printf_filtered ("Remote Threadinfo test\n");
9034 if (!get_and_display_threadinfo (&thread))
9035 printf_filtered ("FAIL cannot get thread info\n");
9039 thread_display_step (threadref *ref, void *context)
9041 /* output_threadid(" threadstep ",ref); *//* simple test */
9042 return get_and_display_threadinfo (ref);
9046 threadlist_update_test_cmd (char *cmd, int tty)
9048 printf_filtered ("Remote Threadlist update test\n");
9049 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9053 init_remote_threadtests (void)
9055 add_com ("tlist", class_obscure, threadlist_test_cmd,
9056 _("Fetch and print the remote list of "
9057 "thread identifiers, one pkt only"));
9058 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
9059 _("Fetch and display info about one thread"));
9060 add_com ("tset", class_obscure, threadset_test_cmd,
9061 _("Test setting to a different thread"));
9062 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
9063 _("Iterate through updating all remote thread info"));
9064 add_com ("talive", class_obscure, threadalive_test,
9065 _(" Remote thread alive test "));
9070 /* Convert a thread ID to a string. Returns the string in a static
9074 remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
9076 static char buf[64];
9077 struct remote_state *rs = get_remote_state ();
9079 if (ptid_equal (ptid, null_ptid))
9080 return normal_pid_to_str (ptid);
9081 else if (ptid_is_pid (ptid))
9083 /* Printing an inferior target id. */
9085 /* When multi-process extensions are off, there's no way in the
9086 remote protocol to know the remote process id, if there's any
9087 at all. There's one exception --- when we're connected with
9088 target extended-remote, and we manually attached to a process
9089 with "attach PID". We don't record anywhere a flag that
9090 allows us to distinguish that case from the case of
9091 connecting with extended-remote and the stub already being
9092 attached to a process, and reporting yes to qAttached, hence
9093 no smart special casing here. */
9094 if (!remote_multi_process_p (rs))
9096 xsnprintf (buf, sizeof buf, "Remote target");
9100 return normal_pid_to_str (ptid);
9104 if (ptid_equal (magic_null_ptid, ptid))
9105 xsnprintf (buf, sizeof buf, "Thread <main>");
9106 else if (rs->extended && remote_multi_process_p (rs))
9107 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9108 ptid_get_pid (ptid), ptid_get_tid (ptid));
9110 xsnprintf (buf, sizeof buf, "Thread %ld",
9111 ptid_get_tid (ptid));
9116 /* Get the address of the thread local variable in OBJFILE which is
9117 stored at OFFSET within the thread local storage for thread PTID. */
9120 remote_get_thread_local_address (struct target_ops *ops,
9121 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
9123 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
9125 struct remote_state *rs = get_remote_state ();
9127 char *endp = rs->buf + get_remote_packet_size ();
9128 enum packet_result result;
9130 strcpy (p, "qGetTLSAddr:");
9132 p = write_ptid (p, endp, ptid);
9134 p += hexnumstr (p, offset);
9136 p += hexnumstr (p, lm);
9140 getpkt (&rs->buf, &rs->buf_size, 0);
9141 result = packet_ok (rs->buf,
9142 &remote_protocol_packets[PACKET_qGetTLSAddr]);
9143 if (result == PACKET_OK)
9147 unpack_varlen_hex (rs->buf, &result);
9150 else if (result == PACKET_UNKNOWN)
9151 throw_error (TLS_GENERIC_ERROR,
9152 _("Remote target doesn't support qGetTLSAddr packet"));
9154 throw_error (TLS_GENERIC_ERROR,
9155 _("Remote target failed to process qGetTLSAddr request"));
9158 throw_error (TLS_GENERIC_ERROR,
9159 _("TLS not supported or disabled on this target"));
9164 /* Provide thread local base, i.e. Thread Information Block address.
9165 Returns 1 if ptid is found and thread_local_base is non zero. */
9168 remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
9170 if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE)
9172 struct remote_state *rs = get_remote_state ();
9174 char *endp = rs->buf + get_remote_packet_size ();
9175 enum packet_result result;
9177 strcpy (p, "qGetTIBAddr:");
9179 p = write_ptid (p, endp, ptid);
9183 getpkt (&rs->buf, &rs->buf_size, 0);
9184 result = packet_ok (rs->buf,
9185 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9186 if (result == PACKET_OK)
9190 unpack_varlen_hex (rs->buf, &result);
9192 *addr = (CORE_ADDR) result;
9195 else if (result == PACKET_UNKNOWN)
9196 error (_("Remote target doesn't support qGetTIBAddr packet"));
9198 error (_("Remote target failed to process qGetTIBAddr request"));
9201 error (_("qGetTIBAddr not supported or disabled on this target"));
9206 /* Support for inferring a target description based on the current
9207 architecture and the size of a 'g' packet. While the 'g' packet
9208 can have any size (since optional registers can be left off the
9209 end), some sizes are easily recognizable given knowledge of the
9210 approximate architecture. */
9212 struct remote_g_packet_guess
9215 const struct target_desc *tdesc;
9217 typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9218 DEF_VEC_O(remote_g_packet_guess_s);
9220 struct remote_g_packet_data
9222 VEC(remote_g_packet_guess_s) *guesses;
9225 static struct gdbarch_data *remote_g_packet_data_handle;
9228 remote_g_packet_data_init (struct obstack *obstack)
9230 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9234 register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9235 const struct target_desc *tdesc)
9237 struct remote_g_packet_data *data
9238 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9239 struct remote_g_packet_guess new_guess, *guess;
9242 gdb_assert (tdesc != NULL);
9245 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9247 if (guess->bytes == bytes)
9248 internal_error (__FILE__, __LINE__,
9249 _("Duplicate g packet description added for size %d"),
9252 new_guess.bytes = bytes;
9253 new_guess.tdesc = tdesc;
9254 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9257 /* Return 1 if remote_read_description would do anything on this target
9258 and architecture, 0 otherwise. */
9261 remote_read_description_p (struct target_ops *target)
9263 struct remote_g_packet_data *data
9264 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
9266 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9272 static const struct target_desc *
9273 remote_read_description (struct target_ops *target)
9275 struct remote_g_packet_data *data
9276 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
9278 /* Do not try this during initial connection, when we do not know
9279 whether there is a running but stopped thread. */
9280 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
9283 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9285 struct remote_g_packet_guess *guess;
9287 int bytes = send_g_packet ();
9290 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9292 if (guess->bytes == bytes)
9293 return guess->tdesc;
9295 /* We discard the g packet. A minor optimization would be to
9296 hold on to it, and fill the register cache once we have selected
9297 an architecture, but it's too tricky to do safely. */
9303 /* Remote file transfer support. This is host-initiated I/O, not
9304 target-initiated; for target-initiated, see remote-fileio.c. */
9306 /* If *LEFT is at least the length of STRING, copy STRING to
9307 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9308 decrease *LEFT. Otherwise raise an error. */
9311 remote_buffer_add_string (char **buffer, int *left, char *string)
9313 int len = strlen (string);
9316 error (_("Packet too long for target."));
9318 memcpy (*buffer, string, len);
9322 /* NUL-terminate the buffer as a convenience, if there is
9328 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9329 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9330 decrease *LEFT. Otherwise raise an error. */
9333 remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9336 if (2 * len > *left)
9337 error (_("Packet too long for target."));
9339 bin2hex (bytes, *buffer, len);
9343 /* NUL-terminate the buffer as a convenience, if there is
9349 /* If *LEFT is large enough, convert VALUE to hex and add it to
9350 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9351 decrease *LEFT. Otherwise raise an error. */
9354 remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9356 int len = hexnumlen (value);
9359 error (_("Packet too long for target."));
9361 hexnumstr (*buffer, value);
9365 /* NUL-terminate the buffer as a convenience, if there is
9371 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9372 value, *REMOTE_ERRNO to the remote error number or zero if none
9373 was included, and *ATTACHMENT to point to the start of the annex
9374 if any. The length of the packet isn't needed here; there may
9375 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9377 Return 0 if the packet could be parsed, -1 if it could not. If
9378 -1 is returned, the other variables may not be initialized. */
9381 remote_hostio_parse_result (char *buffer, int *retcode,
9382 int *remote_errno, char **attachment)
9389 if (buffer[0] != 'F')
9393 *retcode = strtol (&buffer[1], &p, 16);
9394 if (errno != 0 || p == &buffer[1])
9397 /* Check for ",errno". */
9401 *remote_errno = strtol (p + 1, &p2, 16);
9402 if (errno != 0 || p + 1 == p2)
9407 /* Check for ";attachment". If there is no attachment, the
9408 packet should end here. */
9411 *attachment = p + 1;
9414 else if (*p == '\0')
9420 /* Send a prepared I/O packet to the target and read its response.
9421 The prepared packet is in the global RS->BUF before this function
9422 is called, and the answer is there when we return.
9424 COMMAND_BYTES is the length of the request to send, which may include
9425 binary data. WHICH_PACKET is the packet configuration to check
9426 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9427 is set to the error number and -1 is returned. Otherwise the value
9428 returned by the function is returned.
9430 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9431 attachment is expected; an error will be reported if there's a
9432 mismatch. If one is found, *ATTACHMENT will be set to point into
9433 the packet buffer and *ATTACHMENT_LEN will be set to the
9434 attachment's length. */
9437 remote_hostio_send_command (int command_bytes, int which_packet,
9438 int *remote_errno, char **attachment,
9439 int *attachment_len)
9441 struct remote_state *rs = get_remote_state ();
9442 int ret, bytes_read;
9443 char *attachment_tmp;
9446 || remote_protocol_packets[which_packet].support == PACKET_DISABLE)
9448 *remote_errno = FILEIO_ENOSYS;
9452 putpkt_binary (rs->buf, command_bytes);
9453 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9455 /* If it timed out, something is wrong. Don't try to parse the
9459 *remote_errno = FILEIO_EINVAL;
9463 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9466 *remote_errno = FILEIO_EINVAL;
9468 case PACKET_UNKNOWN:
9469 *remote_errno = FILEIO_ENOSYS;
9475 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9478 *remote_errno = FILEIO_EINVAL;
9482 /* Make sure we saw an attachment if and only if we expected one. */
9483 if ((attachment_tmp == NULL && attachment != NULL)
9484 || (attachment_tmp != NULL && attachment == NULL))
9486 *remote_errno = FILEIO_EINVAL;
9490 /* If an attachment was found, it must point into the packet buffer;
9491 work out how many bytes there were. */
9492 if (attachment_tmp != NULL)
9494 *attachment = attachment_tmp;
9495 *attachment_len = bytes_read - (*attachment - rs->buf);
9501 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9502 remote file descriptor, or -1 if an error occurs (and set
9506 remote_hostio_open (const char *filename, int flags, int mode,
9509 struct remote_state *rs = get_remote_state ();
9511 int left = get_remote_packet_size () - 1;
9513 remote_buffer_add_string (&p, &left, "vFile:open:");
9515 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9517 remote_buffer_add_string (&p, &left, ",");
9519 remote_buffer_add_int (&p, &left, flags);
9520 remote_buffer_add_string (&p, &left, ",");
9522 remote_buffer_add_int (&p, &left, mode);
9524 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9525 remote_errno, NULL, NULL);
9528 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9529 Return the number of bytes written, or -1 if an error occurs (and
9530 set *REMOTE_ERRNO). */
9533 remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
9534 ULONGEST offset, int *remote_errno)
9536 struct remote_state *rs = get_remote_state ();
9538 int left = get_remote_packet_size ();
9541 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9543 remote_buffer_add_int (&p, &left, fd);
9544 remote_buffer_add_string (&p, &left, ",");
9546 remote_buffer_add_int (&p, &left, offset);
9547 remote_buffer_add_string (&p, &left, ",");
9549 p += remote_escape_output (write_buf, len, p, &out_len,
9550 get_remote_packet_size () - (p - rs->buf));
9552 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9553 remote_errno, NULL, NULL);
9556 /* Read up to LEN bytes FD on the remote target into READ_BUF
9557 Return the number of bytes read, or -1 if an error occurs (and
9558 set *REMOTE_ERRNO). */
9561 remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
9562 ULONGEST offset, int *remote_errno)
9564 struct remote_state *rs = get_remote_state ();
9567 int left = get_remote_packet_size ();
9568 int ret, attachment_len;
9571 remote_buffer_add_string (&p, &left, "vFile:pread:");
9573 remote_buffer_add_int (&p, &left, fd);
9574 remote_buffer_add_string (&p, &left, ",");
9576 remote_buffer_add_int (&p, &left, len);
9577 remote_buffer_add_string (&p, &left, ",");
9579 remote_buffer_add_int (&p, &left, offset);
9581 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9582 remote_errno, &attachment,
9588 read_len = remote_unescape_input (attachment, attachment_len,
9590 if (read_len != ret)
9591 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9596 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9597 (and set *REMOTE_ERRNO). */
9600 remote_hostio_close (int fd, int *remote_errno)
9602 struct remote_state *rs = get_remote_state ();
9604 int left = get_remote_packet_size () - 1;
9606 remote_buffer_add_string (&p, &left, "vFile:close:");
9608 remote_buffer_add_int (&p, &left, fd);
9610 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9611 remote_errno, NULL, NULL);
9614 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9615 occurs (and set *REMOTE_ERRNO). */
9618 remote_hostio_unlink (const char *filename, int *remote_errno)
9620 struct remote_state *rs = get_remote_state ();
9622 int left = get_remote_packet_size () - 1;
9624 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9626 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9629 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9630 remote_errno, NULL, NULL);
9633 /* Read value of symbolic link FILENAME on the remote target. Return
9634 a null-terminated string allocated via xmalloc, or NULL if an error
9635 occurs (and set *REMOTE_ERRNO). */
9638 remote_hostio_readlink (const char *filename, int *remote_errno)
9640 struct remote_state *rs = get_remote_state ();
9643 int left = get_remote_packet_size ();
9644 int len, attachment_len;
9648 remote_buffer_add_string (&p, &left, "vFile:readlink:");
9650 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9653 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9654 remote_errno, &attachment,
9660 ret = xmalloc (len + 1);
9662 read_len = remote_unescape_input (attachment, attachment_len,
9664 if (read_len != len)
9665 error (_("Readlink returned %d, but %d bytes."), len, read_len);
9672 remote_fileio_errno_to_host (int errnum)
9696 case FILEIO_ENOTDIR:
9716 case FILEIO_ENAMETOOLONG:
9717 return ENAMETOOLONG;
9723 remote_hostio_error (int errnum)
9725 int host_error = remote_fileio_errno_to_host (errnum);
9727 if (host_error == -1)
9728 error (_("Unknown remote I/O error %d"), errnum);
9730 error (_("Remote I/O error: %s"), safe_strerror (host_error));
9734 remote_hostio_close_cleanup (void *opaque)
9736 int fd = *(int *) opaque;
9739 remote_hostio_close (fd, &remote_errno);
9744 remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
9746 const char *filename = bfd_get_filename (abfd);
9747 int fd, remote_errno;
9750 gdb_assert (remote_filename_p (filename));
9752 fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
9755 errno = remote_fileio_errno_to_host (remote_errno);
9756 bfd_set_error (bfd_error_system_call);
9760 stream = xmalloc (sizeof (int));
9766 remote_bfd_iovec_close (struct bfd *abfd, void *stream)
9768 int fd = *(int *)stream;
9773 /* Ignore errors on close; these may happen if the remote
9774 connection was already torn down. */
9775 remote_hostio_close (fd, &remote_errno);
9781 remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
9782 file_ptr nbytes, file_ptr offset)
9784 int fd = *(int *)stream;
9786 file_ptr pos, bytes;
9789 while (nbytes > pos)
9791 bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos,
9792 offset + pos, &remote_errno);
9794 /* Success, but no bytes, means end-of-file. */
9798 errno = remote_fileio_errno_to_host (remote_errno);
9799 bfd_set_error (bfd_error_system_call);
9810 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
9812 /* FIXME: We should probably implement remote_hostio_stat. */
9813 sb->st_size = INT_MAX;
9818 remote_filename_p (const char *filename)
9820 return strncmp (filename, "remote:", 7) == 0;
9824 remote_bfd_open (const char *remote_file, const char *target)
9826 return bfd_openr_iovec (remote_file, target,
9827 remote_bfd_iovec_open, NULL,
9828 remote_bfd_iovec_pread,
9829 remote_bfd_iovec_close,
9830 remote_bfd_iovec_stat);
9834 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
9836 struct cleanup *back_to, *close_cleanup;
9837 int retcode, fd, remote_errno, bytes, io_size;
9840 int bytes_in_buffer;
9845 error (_("command can only be used with remote target"));
9847 file = fopen (local_file, "rb");
9849 perror_with_name (local_file);
9850 back_to = make_cleanup_fclose (file);
9852 fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
9854 0700, &remote_errno);
9856 remote_hostio_error (remote_errno);
9858 /* Send up to this many bytes at once. They won't all fit in the
9859 remote packet limit, so we'll transfer slightly fewer. */
9860 io_size = get_remote_packet_size ();
9861 buffer = xmalloc (io_size);
9862 make_cleanup (xfree, buffer);
9864 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9866 bytes_in_buffer = 0;
9869 while (bytes_in_buffer || !saw_eof)
9873 bytes = fread (buffer + bytes_in_buffer, 1,
9874 io_size - bytes_in_buffer,
9879 error (_("Error reading %s."), local_file);
9882 /* EOF. Unless there is something still in the
9883 buffer from the last iteration, we are done. */
9885 if (bytes_in_buffer == 0)
9893 bytes += bytes_in_buffer;
9894 bytes_in_buffer = 0;
9896 retcode = remote_hostio_pwrite (fd, buffer, bytes,
9897 offset, &remote_errno);
9900 remote_hostio_error (remote_errno);
9901 else if (retcode == 0)
9902 error (_("Remote write of %d bytes returned 0!"), bytes);
9903 else if (retcode < bytes)
9905 /* Short write. Save the rest of the read data for the next
9907 bytes_in_buffer = bytes - retcode;
9908 memmove (buffer, buffer + retcode, bytes_in_buffer);
9914 discard_cleanups (close_cleanup);
9915 if (remote_hostio_close (fd, &remote_errno))
9916 remote_hostio_error (remote_errno);
9919 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
9920 do_cleanups (back_to);
9924 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
9926 struct cleanup *back_to, *close_cleanup;
9927 int fd, remote_errno, bytes, io_size;
9933 error (_("command can only be used with remote target"));
9935 fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
9937 remote_hostio_error (remote_errno);
9939 file = fopen (local_file, "wb");
9941 perror_with_name (local_file);
9942 back_to = make_cleanup_fclose (file);
9944 /* Send up to this many bytes at once. They won't all fit in the
9945 remote packet limit, so we'll transfer slightly fewer. */
9946 io_size = get_remote_packet_size ();
9947 buffer = xmalloc (io_size);
9948 make_cleanup (xfree, buffer);
9950 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9955 bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
9957 /* Success, but no bytes, means end-of-file. */
9960 remote_hostio_error (remote_errno);
9964 bytes = fwrite (buffer, 1, bytes, file);
9966 perror_with_name (local_file);
9969 discard_cleanups (close_cleanup);
9970 if (remote_hostio_close (fd, &remote_errno))
9971 remote_hostio_error (remote_errno);
9974 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
9975 do_cleanups (back_to);
9979 remote_file_delete (const char *remote_file, int from_tty)
9981 int retcode, remote_errno;
9984 error (_("command can only be used with remote target"));
9986 retcode = remote_hostio_unlink (remote_file, &remote_errno);
9988 remote_hostio_error (remote_errno);
9991 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
9995 remote_put_command (char *args, int from_tty)
9997 struct cleanup *back_to;
10001 error_no_arg (_("file to put"));
10003 argv = gdb_buildargv (args);
10004 back_to = make_cleanup_freeargv (argv);
10005 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10006 error (_("Invalid parameters to remote put"));
10008 remote_file_put (argv[0], argv[1], from_tty);
10010 do_cleanups (back_to);
10014 remote_get_command (char *args, int from_tty)
10016 struct cleanup *back_to;
10020 error_no_arg (_("file to get"));
10022 argv = gdb_buildargv (args);
10023 back_to = make_cleanup_freeargv (argv);
10024 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10025 error (_("Invalid parameters to remote get"));
10027 remote_file_get (argv[0], argv[1], from_tty);
10029 do_cleanups (back_to);
10033 remote_delete_command (char *args, int from_tty)
10035 struct cleanup *back_to;
10039 error_no_arg (_("file to delete"));
10041 argv = gdb_buildargv (args);
10042 back_to = make_cleanup_freeargv (argv);
10043 if (argv[0] == NULL || argv[1] != NULL)
10044 error (_("Invalid parameters to remote delete"));
10046 remote_file_delete (argv[0], from_tty);
10048 do_cleanups (back_to);
10052 remote_command (char *args, int from_tty)
10054 help_list (remote_cmdlist, "remote ", -1, gdb_stdout);
10058 remote_can_execute_reverse (void)
10060 if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE
10061 || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE)
10068 remote_supports_non_stop (void)
10074 remote_supports_disable_randomization (void)
10076 /* Only supported in extended mode. */
10081 remote_supports_multi_process (void)
10083 struct remote_state *rs = get_remote_state ();
10085 /* Only extended-remote handles being attached to multiple
10086 processes, even though plain remote can use the multi-process
10087 thread id extensions, so that GDB knows the target process's
10089 return rs->extended && remote_multi_process_p (rs);
10093 remote_supports_cond_tracepoints (void)
10095 struct remote_state *rs = get_remote_state ();
10097 return rs->cond_tracepoints;
10101 remote_supports_cond_breakpoints (void)
10103 struct remote_state *rs = get_remote_state ();
10105 return rs->cond_breakpoints;
10109 remote_supports_fast_tracepoints (void)
10111 struct remote_state *rs = get_remote_state ();
10113 return rs->fast_tracepoints;
10117 remote_supports_static_tracepoints (void)
10119 struct remote_state *rs = get_remote_state ();
10121 return rs->static_tracepoints;
10125 remote_supports_install_in_trace (void)
10127 struct remote_state *rs = get_remote_state ();
10129 return rs->install_in_trace;
10133 remote_supports_enable_disable_tracepoint (void)
10135 struct remote_state *rs = get_remote_state ();
10137 return rs->enable_disable_tracepoints;
10141 remote_supports_string_tracing (void)
10143 struct remote_state *rs = get_remote_state ();
10145 return rs->string_tracing;
10149 remote_can_run_breakpoint_commands (void)
10151 struct remote_state *rs = get_remote_state ();
10153 return rs->breakpoint_commands;
10157 remote_trace_init (void)
10160 remote_get_noisy_reply (&target_buf, &target_buf_size);
10161 if (strcmp (target_buf, "OK") != 0)
10162 error (_("Target does not support this command."));
10165 static void free_actions_list (char **actions_list);
10166 static void free_actions_list_cleanup_wrapper (void *);
10168 free_actions_list_cleanup_wrapper (void *al)
10170 free_actions_list (al);
10174 free_actions_list (char **actions_list)
10178 if (actions_list == 0)
10181 for (ndx = 0; actions_list[ndx]; ndx++)
10182 xfree (actions_list[ndx]);
10184 xfree (actions_list);
10187 /* Recursive routine to walk through command list including loops, and
10188 download packets for each command. */
10191 remote_download_command_source (int num, ULONGEST addr,
10192 struct command_line *cmds)
10194 struct remote_state *rs = get_remote_state ();
10195 struct command_line *cmd;
10197 for (cmd = cmds; cmd; cmd = cmd->next)
10199 QUIT; /* Allow user to bail out with ^C. */
10200 strcpy (rs->buf, "QTDPsrc:");
10201 encode_source_string (num, addr, "cmd", cmd->line,
10202 rs->buf + strlen (rs->buf),
10203 rs->buf_size - strlen (rs->buf));
10205 remote_get_noisy_reply (&target_buf, &target_buf_size);
10206 if (strcmp (target_buf, "OK"))
10207 warning (_("Target does not support source download."));
10209 if (cmd->control_type == while_control
10210 || cmd->control_type == while_stepping_control)
10212 remote_download_command_source (num, addr, *cmd->body_list);
10214 QUIT; /* Allow user to bail out with ^C. */
10215 strcpy (rs->buf, "QTDPsrc:");
10216 encode_source_string (num, addr, "cmd", "end",
10217 rs->buf + strlen (rs->buf),
10218 rs->buf_size - strlen (rs->buf));
10220 remote_get_noisy_reply (&target_buf, &target_buf_size);
10221 if (strcmp (target_buf, "OK"))
10222 warning (_("Target does not support source download."));
10228 remote_download_tracepoint (struct bp_location *loc)
10230 #define BUF_SIZE 2048
10234 char buf[BUF_SIZE];
10235 char **tdp_actions;
10236 char **stepping_actions;
10238 struct cleanup *old_chain = NULL;
10239 struct agent_expr *aexpr;
10240 struct cleanup *aexpr_chain = NULL;
10242 struct breakpoint *b = loc->owner;
10243 struct tracepoint *t = (struct tracepoint *) b;
10245 encode_actions (loc->owner, loc, &tdp_actions, &stepping_actions);
10246 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10248 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10251 tpaddr = loc->address;
10252 sprintf_vma (addrbuf, tpaddr);
10253 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10254 addrbuf, /* address */
10255 (b->enable_state == bp_enabled ? 'E' : 'D'),
10256 t->step_count, t->pass_count);
10257 /* Fast tracepoints are mostly handled by the target, but we can
10258 tell the target how big of an instruction block should be moved
10260 if (b->type == bp_fast_tracepoint)
10262 /* Only test for support at download time; we may not know
10263 target capabilities at definition time. */
10264 if (remote_supports_fast_tracepoints ())
10268 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch,
10269 tpaddr, &isize, NULL))
10270 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10273 /* If it passed validation at definition but fails now,
10274 something is very wrong. */
10275 internal_error (__FILE__, __LINE__,
10276 _("Fast tracepoint not "
10277 "valid during download"));
10280 /* Fast tracepoints are functionally identical to regular
10281 tracepoints, so don't take lack of support as a reason to
10282 give up on the trace run. */
10283 warning (_("Target does not support fast tracepoints, "
10284 "downloading %d as regular tracepoint"), b->number);
10286 else if (b->type == bp_static_tracepoint)
10288 /* Only test for support at download time; we may not know
10289 target capabilities at definition time. */
10290 if (remote_supports_static_tracepoints ())
10292 struct static_tracepoint_marker marker;
10294 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10295 strcat (buf, ":S");
10297 error (_("Static tracepoint not valid during download"));
10300 /* Fast tracepoints are functionally identical to regular
10301 tracepoints, so don't take lack of support as a reason
10302 to give up on the trace run. */
10303 error (_("Target does not support static tracepoints"));
10305 /* If the tracepoint has a conditional, make it into an agent
10306 expression and append to the definition. */
10309 /* Only test support at download time, we may not know target
10310 capabilities at definition time. */
10311 if (remote_supports_cond_tracepoints ())
10313 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10314 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
10315 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10317 pkt = buf + strlen (buf);
10318 for (ndx = 0; ndx < aexpr->len; ++ndx)
10319 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10321 do_cleanups (aexpr_chain);
10324 warning (_("Target does not support conditional tracepoints, "
10325 "ignoring tp %d cond"), b->number);
10328 if (b->commands || *default_collect)
10331 remote_get_noisy_reply (&target_buf, &target_buf_size);
10332 if (strcmp (target_buf, "OK"))
10333 error (_("Target does not support tracepoints."));
10335 /* do_single_steps (t); */
10338 for (ndx = 0; tdp_actions[ndx]; ndx++)
10340 QUIT; /* Allow user to bail out with ^C. */
10341 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10342 b->number, addrbuf, /* address */
10344 ((tdp_actions[ndx + 1] || stepping_actions)
10347 remote_get_noisy_reply (&target_buf,
10349 if (strcmp (target_buf, "OK"))
10350 error (_("Error on target while setting tracepoints."));
10353 if (stepping_actions)
10355 for (ndx = 0; stepping_actions[ndx]; ndx++)
10357 QUIT; /* Allow user to bail out with ^C. */
10358 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10359 b->number, addrbuf, /* address */
10360 ((ndx == 0) ? "S" : ""),
10361 stepping_actions[ndx],
10362 (stepping_actions[ndx + 1] ? "-" : ""));
10364 remote_get_noisy_reply (&target_buf,
10366 if (strcmp (target_buf, "OK"))
10367 error (_("Error on target while setting tracepoints."));
10371 if (remote_protocol_packets[PACKET_TracepointSource].support
10374 if (b->addr_string)
10376 strcpy (buf, "QTDPsrc:");
10377 encode_source_string (b->number, loc->address,
10378 "at", b->addr_string, buf + strlen (buf),
10379 2048 - strlen (buf));
10382 remote_get_noisy_reply (&target_buf, &target_buf_size);
10383 if (strcmp (target_buf, "OK"))
10384 warning (_("Target does not support source download."));
10386 if (b->cond_string)
10388 strcpy (buf, "QTDPsrc:");
10389 encode_source_string (b->number, loc->address,
10390 "cond", b->cond_string, buf + strlen (buf),
10391 2048 - strlen (buf));
10393 remote_get_noisy_reply (&target_buf, &target_buf_size);
10394 if (strcmp (target_buf, "OK"))
10395 warning (_("Target does not support source download."));
10397 remote_download_command_source (b->number, loc->address,
10398 breakpoint_commands (b));
10401 do_cleanups (old_chain);
10405 remote_can_download_tracepoint (void)
10407 struct remote_state *rs = get_remote_state ();
10408 struct trace_status *ts;
10411 /* Don't try to install tracepoints until we've relocated our
10412 symbols, and fetched and merged the target's tracepoint list with
10414 if (rs->starting_up)
10417 ts = current_trace_status ();
10418 status = remote_get_trace_status (ts);
10420 if (status == -1 || !ts->running_known || !ts->running)
10423 /* If we are in a tracing experiment, but remote stub doesn't support
10424 installing tracepoint in trace, we have to return. */
10425 if (!remote_supports_install_in_trace ())
10433 remote_download_trace_state_variable (struct trace_state_variable *tsv)
10435 struct remote_state *rs = get_remote_state ();
10438 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10439 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10441 p = rs->buf + strlen (rs->buf);
10442 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10443 error (_("Trace state variable name too long for tsv definition packet"));
10444 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0);
10447 remote_get_noisy_reply (&target_buf, &target_buf_size);
10448 if (*target_buf == '\0')
10449 error (_("Target does not support this command."));
10450 if (strcmp (target_buf, "OK") != 0)
10451 error (_("Error on target while downloading trace state variable."));
10455 remote_enable_tracepoint (struct bp_location *location)
10457 struct remote_state *rs = get_remote_state ();
10460 sprintf_vma (addr_buf, location->address);
10461 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10462 location->owner->number, addr_buf);
10464 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10465 if (*rs->buf == '\0')
10466 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10467 if (strcmp (rs->buf, "OK") != 0)
10468 error (_("Error on target while enabling tracepoint."));
10472 remote_disable_tracepoint (struct bp_location *location)
10474 struct remote_state *rs = get_remote_state ();
10477 sprintf_vma (addr_buf, location->address);
10478 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10479 location->owner->number, addr_buf);
10481 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10482 if (*rs->buf == '\0')
10483 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10484 if (strcmp (rs->buf, "OK") != 0)
10485 error (_("Error on target while disabling tracepoint."));
10489 remote_trace_set_readonly_regions (void)
10493 bfd_size_type size;
10499 return; /* No information to give. */
10501 strcpy (target_buf, "QTro");
10502 for (s = exec_bfd->sections; s; s = s->next)
10504 char tmp1[40], tmp2[40];
10507 if ((s->flags & SEC_LOAD) == 0 ||
10508 /* (s->flags & SEC_CODE) == 0 || */
10509 (s->flags & SEC_READONLY) == 0)
10513 vma = bfd_get_section_vma (abfd, s);
10514 size = bfd_get_section_size (s);
10515 sprintf_vma (tmp1, vma);
10516 sprintf_vma (tmp2, vma + size);
10517 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10518 if (offset + sec_length + 1 > target_buf_size)
10520 if (remote_protocol_packets[PACKET_qXfer_traceframe_info].support
10523 Too many sections for read-only sections definition packet."));
10526 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10528 offset += sec_length;
10532 putpkt (target_buf);
10533 getpkt (&target_buf, &target_buf_size, 0);
10538 remote_trace_start (void)
10540 putpkt ("QTStart");
10541 remote_get_noisy_reply (&target_buf, &target_buf_size);
10542 if (*target_buf == '\0')
10543 error (_("Target does not support this command."));
10544 if (strcmp (target_buf, "OK") != 0)
10545 error (_("Bogus reply from target: %s"), target_buf);
10549 remote_get_trace_status (struct trace_status *ts)
10551 /* Initialize it just to avoid a GCC false warning. */
10553 /* FIXME we need to get register block size some other way. */
10554 extern int trace_regblock_size;
10555 volatile struct gdb_exception ex;
10557 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10559 putpkt ("qTStatus");
10561 TRY_CATCH (ex, RETURN_MASK_ERROR)
10563 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10567 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10571 /* If the remote target doesn't do tracing, flag it. */
10575 /* We're working with a live target. */
10578 /* Set some defaults. */
10579 ts->running_known = 0;
10580 ts->stop_reason = trace_stop_reason_unknown;
10581 ts->traceframe_count = -1;
10582 ts->buffer_free = 0;
10585 error (_("Bogus trace status reply from target: %s"), target_buf);
10587 parse_trace_status (p, ts);
10589 return ts->running;
10593 remote_get_tracepoint_status (struct breakpoint *bp,
10594 struct uploaded_tp *utp)
10596 struct remote_state *rs = get_remote_state ();
10598 struct bp_location *loc;
10599 struct tracepoint *tp = (struct tracepoint *) bp;
10600 size_t size = get_remote_packet_size ();
10604 tp->base.hit_count = 0;
10605 tp->traceframe_usage = 0;
10606 for (loc = tp->base.loc; loc; loc = loc->next)
10608 /* If the tracepoint was never downloaded, don't go asking for
10610 if (tp->number_on_target == 0)
10612 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10613 phex_nz (loc->address, 0));
10615 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10616 if (reply && *reply)
10619 parse_tracepoint_status (reply + 1, bp, utp);
10625 utp->hit_count = 0;
10626 utp->traceframe_usage = 0;
10627 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10628 phex_nz (utp->addr, 0));
10630 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10631 if (reply && *reply)
10634 parse_tracepoint_status (reply + 1, bp, utp);
10640 remote_trace_stop (void)
10643 remote_get_noisy_reply (&target_buf, &target_buf_size);
10644 if (*target_buf == '\0')
10645 error (_("Target does not support this command."));
10646 if (strcmp (target_buf, "OK") != 0)
10647 error (_("Bogus reply from target: %s"), target_buf);
10651 remote_trace_find (enum trace_find_type type, int num,
10652 ULONGEST addr1, ULONGEST addr2,
10655 struct remote_state *rs = get_remote_state ();
10656 char *endbuf = rs->buf + get_remote_packet_size ();
10658 int target_frameno = -1, target_tracept = -1;
10660 /* Lookups other than by absolute frame number depend on the current
10661 trace selected, so make sure it is correct on the remote end
10663 if (type != tfind_number)
10664 set_remote_traceframe ();
10667 strcpy (p, "QTFrame:");
10668 p = strchr (p, '\0');
10672 xsnprintf (p, endbuf - p, "%x", num);
10675 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
10678 xsnprintf (p, endbuf - p, "tdp:%x", num);
10681 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10682 phex_nz (addr2, 0));
10684 case tfind_outside:
10685 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10686 phex_nz (addr2, 0));
10689 error (_("Unknown trace find type %d"), type);
10693 reply = remote_get_noisy_reply (&(rs->buf), &sizeof_pkt);
10694 if (*reply == '\0')
10695 error (_("Target does not support this command."));
10697 while (reply && *reply)
10702 target_frameno = (int) strtol (p, &reply, 16);
10704 error (_("Unable to parse trace frame number"));
10705 /* Don't update our remote traceframe number cache on failure
10706 to select a remote traceframe. */
10707 if (target_frameno == -1)
10712 target_tracept = (int) strtol (p, &reply, 16);
10714 error (_("Unable to parse tracepoint number"));
10716 case 'O': /* "OK"? */
10717 if (reply[1] == 'K' && reply[2] == '\0')
10720 error (_("Bogus reply from target: %s"), reply);
10723 error (_("Bogus reply from target: %s"), reply);
10726 *tpp = target_tracept;
10728 remote_traceframe_number = target_frameno;
10729 return target_frameno;
10733 remote_get_trace_state_variable_value (int tsvnum, LONGEST *val)
10735 struct remote_state *rs = get_remote_state ();
10739 set_remote_traceframe ();
10741 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
10743 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10744 if (reply && *reply)
10748 unpack_varlen_hex (reply + 1, &uval);
10749 *val = (LONGEST) uval;
10757 remote_save_trace_data (const char *filename)
10759 struct remote_state *rs = get_remote_state ();
10763 strcpy (p, "QTSave:");
10765 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10766 error (_("Remote file name too long for trace save packet"));
10767 p += 2 * bin2hex ((gdb_byte *) filename, p, 0);
10770 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10771 if (*reply == '\0')
10772 error (_("Target does not support this command."));
10773 if (strcmp (reply, "OK") != 0)
10774 error (_("Bogus reply from target: %s"), reply);
10778 /* This is basically a memory transfer, but needs to be its own packet
10779 because we don't know how the target actually organizes its trace
10780 memory, plus we want to be able to ask for as much as possible, but
10781 not be unhappy if we don't get as much as we ask for. */
10784 remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
10786 struct remote_state *rs = get_remote_state ();
10792 strcpy (p, "qTBuffer:");
10794 p += hexnumstr (p, offset);
10796 p += hexnumstr (p, len);
10800 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10801 if (reply && *reply)
10803 /* 'l' by itself means we're at the end of the buffer and
10804 there is nothing more to get. */
10808 /* Convert the reply into binary. Limit the number of bytes to
10809 convert according to our passed-in buffer size, rather than
10810 what was returned in the packet; if the target is
10811 unexpectedly generous and gives us a bigger reply than we
10812 asked for, we don't want to crash. */
10813 rslt = hex2bin (target_buf, buf, len);
10817 /* Something went wrong, flag as an error. */
10822 remote_set_disconnected_tracing (int val)
10824 struct remote_state *rs = get_remote_state ();
10826 if (rs->disconnected_tracing)
10830 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
10832 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10833 if (*reply == '\0')
10834 error (_("Target does not support this command."));
10835 if (strcmp (reply, "OK") != 0)
10836 error (_("Bogus reply from target: %s"), reply);
10839 warning (_("Target does not support disconnected tracing."));
10843 remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
10845 struct thread_info *info = find_thread_ptid (ptid);
10847 if (info && info->private)
10848 return info->private->core;
10853 remote_set_circular_trace_buffer (int val)
10855 struct remote_state *rs = get_remote_state ();
10858 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
10860 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10861 if (*reply == '\0')
10862 error (_("Target does not support this command."));
10863 if (strcmp (reply, "OK") != 0)
10864 error (_("Bogus reply from target: %s"), reply);
10867 static struct traceframe_info *
10868 remote_traceframe_info (void)
10872 text = target_read_stralloc (¤t_target,
10873 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
10876 struct traceframe_info *info;
10877 struct cleanup *back_to = make_cleanup (xfree, text);
10879 info = parse_traceframe_info (text);
10880 do_cleanups (back_to);
10887 /* Handle the qTMinFTPILen packet. Returns the minimum length of
10888 instruction on which a fast tracepoint may be placed. Returns -1
10889 if the packet is not supported, and 0 if the minimum instruction
10890 length is unknown. */
10893 remote_get_min_fast_tracepoint_insn_len (void)
10895 struct remote_state *rs = get_remote_state ();
10898 /* If we're not debugging a process yet, the IPA can't be
10900 if (!target_has_execution)
10903 /* Make sure the remote is pointing at the right process. */
10904 set_general_process ();
10906 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
10908 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10909 if (*reply == '\0')
10913 ULONGEST min_insn_len;
10915 unpack_varlen_hex (reply, &min_insn_len);
10917 return (int) min_insn_len;
10922 remote_set_trace_notes (char *user, char *notes, char *stop_notes)
10924 struct remote_state *rs = get_remote_state ();
10926 char *buf = rs->buf;
10927 char *endbuf = rs->buf + get_remote_packet_size ();
10930 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
10933 buf += xsnprintf (buf, endbuf - buf, "user:");
10934 nbytes = bin2hex (user, buf, 0);
10940 buf += xsnprintf (buf, endbuf - buf, "notes:");
10941 nbytes = bin2hex (notes, buf, 0);
10947 buf += xsnprintf (buf, endbuf - buf, "tstop:");
10948 nbytes = bin2hex (stop_notes, buf, 0);
10952 /* Ensure the buffer is terminated. */
10956 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10957 if (*reply == '\0')
10960 if (strcmp (reply, "OK") != 0)
10961 error (_("Bogus reply from target: %s"), reply);
10967 remote_use_agent (int use)
10969 if (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE)
10971 struct remote_state *rs = get_remote_state ();
10973 /* If the stub supports QAgent. */
10974 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
10976 getpkt (&rs->buf, &rs->buf_size, 0);
10978 if (strcmp (rs->buf, "OK") == 0)
10989 remote_can_use_agent (void)
10991 return (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE);
10995 init_remote_ops (void)
10997 remote_ops.to_shortname = "remote";
10998 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
10999 remote_ops.to_doc =
11000 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11001 Specify the serial device it is connected to\n\
11002 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11003 remote_ops.to_open = remote_open;
11004 remote_ops.to_close = remote_close;
11005 remote_ops.to_detach = remote_detach;
11006 remote_ops.to_disconnect = remote_disconnect;
11007 remote_ops.to_resume = remote_resume;
11008 remote_ops.to_wait = remote_wait;
11009 remote_ops.to_fetch_registers = remote_fetch_registers;
11010 remote_ops.to_store_registers = remote_store_registers;
11011 remote_ops.to_prepare_to_store = remote_prepare_to_store;
11012 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
11013 remote_ops.to_files_info = remote_files_info;
11014 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
11015 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
11016 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
11017 remote_ops.to_stopped_data_address = remote_stopped_data_address;
11018 remote_ops.to_watchpoint_addr_within_range =
11019 remote_watchpoint_addr_within_range;
11020 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
11021 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
11022 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
11023 remote_ops.to_region_ok_for_hw_watchpoint
11024 = remote_region_ok_for_hw_watchpoint;
11025 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
11026 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
11027 remote_ops.to_kill = remote_kill;
11028 remote_ops.to_load = generic_load;
11029 remote_ops.to_mourn_inferior = remote_mourn;
11030 remote_ops.to_pass_signals = remote_pass_signals;
11031 remote_ops.to_program_signals = remote_program_signals;
11032 remote_ops.to_thread_alive = remote_thread_alive;
11033 remote_ops.to_find_new_threads = remote_threads_info;
11034 remote_ops.to_pid_to_str = remote_pid_to_str;
11035 remote_ops.to_extra_thread_info = remote_threads_extra_info;
11036 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
11037 remote_ops.to_stop = remote_stop;
11038 remote_ops.to_xfer_partial = remote_xfer_partial;
11039 remote_ops.to_rcmd = remote_rcmd;
11040 remote_ops.to_log_command = serial_log_command;
11041 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
11042 remote_ops.to_stratum = process_stratum;
11043 remote_ops.to_has_all_memory = default_child_has_all_memory;
11044 remote_ops.to_has_memory = default_child_has_memory;
11045 remote_ops.to_has_stack = default_child_has_stack;
11046 remote_ops.to_has_registers = default_child_has_registers;
11047 remote_ops.to_has_execution = default_child_has_execution;
11048 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
11049 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
11050 remote_ops.to_magic = OPS_MAGIC;
11051 remote_ops.to_memory_map = remote_memory_map;
11052 remote_ops.to_flash_erase = remote_flash_erase;
11053 remote_ops.to_flash_done = remote_flash_done;
11054 remote_ops.to_read_description = remote_read_description;
11055 remote_ops.to_search_memory = remote_search_memory;
11056 remote_ops.to_can_async_p = remote_can_async_p;
11057 remote_ops.to_is_async_p = remote_is_async_p;
11058 remote_ops.to_async = remote_async;
11059 remote_ops.to_terminal_inferior = remote_terminal_inferior;
11060 remote_ops.to_terminal_ours = remote_terminal_ours;
11061 remote_ops.to_supports_non_stop = remote_supports_non_stop;
11062 remote_ops.to_supports_multi_process = remote_supports_multi_process;
11063 remote_ops.to_supports_disable_randomization
11064 = remote_supports_disable_randomization;
11065 remote_ops.to_fileio_open = remote_hostio_open;
11066 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11067 remote_ops.to_fileio_pread = remote_hostio_pread;
11068 remote_ops.to_fileio_close = remote_hostio_close;
11069 remote_ops.to_fileio_unlink = remote_hostio_unlink;
11070 remote_ops.to_fileio_readlink = remote_hostio_readlink;
11071 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
11072 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
11073 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
11074 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
11075 remote_ops.to_trace_init = remote_trace_init;
11076 remote_ops.to_download_tracepoint = remote_download_tracepoint;
11077 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
11078 remote_ops.to_download_trace_state_variable
11079 = remote_download_trace_state_variable;
11080 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11081 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
11082 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11083 remote_ops.to_trace_start = remote_trace_start;
11084 remote_ops.to_get_trace_status = remote_get_trace_status;
11085 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
11086 remote_ops.to_trace_stop = remote_trace_stop;
11087 remote_ops.to_trace_find = remote_trace_find;
11088 remote_ops.to_get_trace_state_variable_value
11089 = remote_get_trace_state_variable_value;
11090 remote_ops.to_save_trace_data = remote_save_trace_data;
11091 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
11092 remote_ops.to_upload_trace_state_variables
11093 = remote_upload_trace_state_variables;
11094 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
11095 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
11096 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
11097 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
11098 remote_ops.to_set_trace_notes = remote_set_trace_notes;
11099 remote_ops.to_core_of_thread = remote_core_of_thread;
11100 remote_ops.to_verify_memory = remote_verify_memory;
11101 remote_ops.to_get_tib_address = remote_get_tib_address;
11102 remote_ops.to_set_permissions = remote_set_permissions;
11103 remote_ops.to_static_tracepoint_marker_at
11104 = remote_static_tracepoint_marker_at;
11105 remote_ops.to_static_tracepoint_markers_by_strid
11106 = remote_static_tracepoint_markers_by_strid;
11107 remote_ops.to_traceframe_info = remote_traceframe_info;
11108 remote_ops.to_use_agent = remote_use_agent;
11109 remote_ops.to_can_use_agent = remote_can_use_agent;
11112 /* Set up the extended remote vector by making a copy of the standard
11113 remote vector and adding to it. */
11116 init_extended_remote_ops (void)
11118 extended_remote_ops = remote_ops;
11120 extended_remote_ops.to_shortname = "extended-remote";
11121 extended_remote_ops.to_longname =
11122 "Extended remote serial target in gdb-specific protocol";
11123 extended_remote_ops.to_doc =
11124 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11125 Specify the serial device it is connected to (e.g. /dev/ttya).";
11126 extended_remote_ops.to_open = extended_remote_open;
11127 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11128 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
11129 extended_remote_ops.to_detach = extended_remote_detach;
11130 extended_remote_ops.to_attach = extended_remote_attach;
11131 extended_remote_ops.to_kill = extended_remote_kill;
11132 extended_remote_ops.to_supports_disable_randomization
11133 = extended_remote_supports_disable_randomization;
11137 remote_can_async_p (void)
11139 if (!target_async_permitted)
11140 /* We only enable async when the user specifically asks for it. */
11143 /* We're async whenever the serial device is. */
11144 return serial_can_async_p (remote_desc);
11148 remote_is_async_p (void)
11150 if (!target_async_permitted)
11151 /* We only enable async when the user specifically asks for it. */
11154 /* We're async whenever the serial device is. */
11155 return serial_is_async_p (remote_desc);
11158 /* Pass the SERIAL event on and up to the client. One day this code
11159 will be able to delay notifying the client of an event until the
11160 point where an entire packet has been received. */
11162 static void (*async_client_callback) (enum inferior_event_type event_type,
11164 static void *async_client_context;
11165 static serial_event_ftype remote_async_serial_handler;
11168 remote_async_serial_handler (struct serial *scb, void *context)
11170 /* Don't propogate error information up to the client. Instead let
11171 the client find out about the error by querying the target. */
11172 async_client_callback (INF_REG_EVENT, async_client_context);
11176 remote_async_inferior_event_handler (gdb_client_data data)
11178 inferior_event_handler (INF_REG_EVENT, NULL);
11182 remote_async_get_pending_events_handler (gdb_client_data data)
11184 remote_get_pending_stop_replies ();
11188 remote_async (void (*callback) (enum inferior_event_type event_type,
11189 void *context), void *context)
11191 if (callback != NULL)
11193 serial_async (remote_desc, remote_async_serial_handler, NULL);
11194 async_client_callback = callback;
11195 async_client_context = context;
11198 serial_async (remote_desc, NULL, NULL);
11202 set_remote_cmd (char *args, int from_tty)
11204 help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout);
11208 show_remote_cmd (char *args, int from_tty)
11210 /* We can't just use cmd_show_list here, because we want to skip
11211 the redundant "show remote Z-packet" and the legacy aliases. */
11212 struct cleanup *showlist_chain;
11213 struct cmd_list_element *list = remote_show_cmdlist;
11214 struct ui_out *uiout = current_uiout;
11216 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11217 for (; list != NULL; list = list->next)
11218 if (strcmp (list->name, "Z-packet") == 0)
11220 else if (list->type == not_set_cmd)
11221 /* Alias commands are exactly like the original, except they
11222 don't have the normal type. */
11226 struct cleanup *option_chain
11227 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
11229 ui_out_field_string (uiout, "name", list->name);
11230 ui_out_text (uiout, ": ");
11231 if (list->type == show_cmd)
11232 do_setshow_command ((char *) NULL, from_tty, list);
11234 cmd_func (list, NULL, from_tty);
11235 /* Close the tuple. */
11236 do_cleanups (option_chain);
11239 /* Close the tuple. */
11240 do_cleanups (showlist_chain);
11244 /* Function to be called whenever a new objfile (shlib) is detected. */
11246 remote_new_objfile (struct objfile *objfile)
11248 if (remote_desc != 0) /* Have a remote connection. */
11249 remote_check_symbols (objfile);
11252 /* Pull all the tracepoints defined on the target and create local
11253 data structures representing them. We don't want to create real
11254 tracepoints yet, we don't want to mess up the user's existing
11258 remote_upload_tracepoints (struct uploaded_tp **utpp)
11260 struct remote_state *rs = get_remote_state ();
11263 /* Ask for a first packet of tracepoint definition. */
11265 getpkt (&rs->buf, &rs->buf_size, 0);
11267 while (*p && *p != 'l')
11269 parse_tracepoint_definition (p, utpp);
11270 /* Ask for another packet of tracepoint definition. */
11272 getpkt (&rs->buf, &rs->buf_size, 0);
11279 remote_upload_trace_state_variables (struct uploaded_tsv **utsvp)
11281 struct remote_state *rs = get_remote_state ();
11284 /* Ask for a first packet of variable definition. */
11286 getpkt (&rs->buf, &rs->buf_size, 0);
11288 while (*p && *p != 'l')
11290 parse_tsv_definition (p, utsvp);
11291 /* Ask for another packet of variable definition. */
11293 getpkt (&rs->buf, &rs->buf_size, 0);
11300 _initialize_remote (void)
11302 struct remote_state *rs;
11303 struct cmd_list_element *cmd;
11306 /* architecture specific data */
11307 remote_gdbarch_data_handle =
11308 gdbarch_data_register_post_init (init_remote_state);
11309 remote_g_packet_data_handle =
11310 gdbarch_data_register_pre_init (remote_g_packet_data_init);
11312 /* Initialize the per-target state. At the moment there is only one
11313 of these, not one per target. Only one target is active at a
11314 time. The default buffer size is unimportant; it will be expanded
11315 whenever a larger buffer is needed. */
11316 rs = get_remote_state_raw ();
11317 rs->buf_size = 400;
11318 rs->buf = xmalloc (rs->buf_size);
11320 init_remote_ops ();
11321 add_target (&remote_ops);
11323 init_extended_remote_ops ();
11324 add_target (&extended_remote_ops);
11326 /* Hook into new objfile notification. */
11327 observer_attach_new_objfile (remote_new_objfile);
11329 /* Set up signal handlers. */
11330 sigint_remote_token =
11331 create_async_signal_handler (async_remote_interrupt, NULL);
11332 sigint_remote_twice_token =
11333 create_async_signal_handler (async_remote_interrupt_twice, NULL);
11336 init_remote_threadtests ();
11339 /* set/show remote ... */
11341 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
11342 Remote protocol specific variables\n\
11343 Configure various remote-protocol specific variables such as\n\
11344 the packets being used"),
11345 &remote_set_cmdlist, "set remote ",
11346 0 /* allow-unknown */, &setlist);
11347 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
11348 Remote protocol specific variables\n\
11349 Configure various remote-protocol specific variables such as\n\
11350 the packets being used"),
11351 &remote_show_cmdlist, "show remote ",
11352 0 /* allow-unknown */, &showlist);
11354 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
11355 Compare section data on target to the exec file.\n\
11356 Argument is a single section name (default: all loaded sections)."),
11359 add_cmd ("packet", class_maintenance, packet_command, _("\
11360 Send an arbitrary packet to a remote target.\n\
11361 maintenance packet TEXT\n\
11362 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11363 this command sends the string TEXT to the inferior, and displays the\n\
11364 response packet. GDB supplies the initial `$' character, and the\n\
11365 terminating `#' character and checksum."),
11368 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
11369 Set whether to send break if interrupted."), _("\
11370 Show whether to send break if interrupted."), _("\
11371 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11372 set_remotebreak, show_remotebreak,
11373 &setlist, &showlist);
11374 cmd_name = "remotebreak";
11375 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
11376 deprecate_cmd (cmd, "set remote interrupt-sequence");
11377 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
11378 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
11379 deprecate_cmd (cmd, "show remote interrupt-sequence");
11381 add_setshow_enum_cmd ("interrupt-sequence", class_support,
11382 interrupt_sequence_modes, &interrupt_sequence_mode,
11384 Set interrupt sequence to remote target."), _("\
11385 Show interrupt sequence to remote target."), _("\
11386 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11387 NULL, show_interrupt_sequence,
11388 &remote_set_cmdlist,
11389 &remote_show_cmdlist);
11391 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
11392 &interrupt_on_connect, _("\
11393 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11394 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11395 If set, interrupt sequence is sent to remote target."),
11397 &remote_set_cmdlist, &remote_show_cmdlist);
11399 /* Install commands for configuring memory read/write packets. */
11401 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
11402 Set the maximum number of bytes per memory write packet (deprecated)."),
11404 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
11405 Show the maximum number of bytes per memory write packet (deprecated)."),
11407 add_cmd ("memory-write-packet-size", no_class,
11408 set_memory_write_packet_size, _("\
11409 Set the maximum number of bytes per memory-write packet.\n\
11410 Specify the number of bytes in a packet or 0 (zero) for the\n\
11411 default packet size. The actual limit is further reduced\n\
11412 dependent on the target. Specify ``fixed'' to disable the\n\
11413 further restriction and ``limit'' to enable that restriction."),
11414 &remote_set_cmdlist);
11415 add_cmd ("memory-read-packet-size", no_class,
11416 set_memory_read_packet_size, _("\
11417 Set the maximum number of bytes per memory-read packet.\n\
11418 Specify the number of bytes in a packet or 0 (zero) for the\n\
11419 default packet size. The actual limit is further reduced\n\
11420 dependent on the target. Specify ``fixed'' to disable the\n\
11421 further restriction and ``limit'' to enable that restriction."),
11422 &remote_set_cmdlist);
11423 add_cmd ("memory-write-packet-size", no_class,
11424 show_memory_write_packet_size,
11425 _("Show the maximum number of bytes per memory-write packet."),
11426 &remote_show_cmdlist);
11427 add_cmd ("memory-read-packet-size", no_class,
11428 show_memory_read_packet_size,
11429 _("Show the maximum number of bytes per memory-read packet."),
11430 &remote_show_cmdlist);
11432 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
11433 &remote_hw_watchpoint_limit, _("\
11434 Set the maximum number of target hardware watchpoints."), _("\
11435 Show the maximum number of target hardware watchpoints."), _("\
11436 Specify a negative limit for unlimited."),
11437 NULL, NULL, /* FIXME: i18n: The maximum
11438 number of target hardware
11439 watchpoints is %s. */
11440 &remote_set_cmdlist, &remote_show_cmdlist);
11441 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
11442 &remote_hw_watchpoint_length_limit, _("\
11443 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11444 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11445 Specify a negative limit for unlimited."),
11446 NULL, NULL, /* FIXME: i18n: The maximum
11447 length (in bytes) of a target
11448 hardware watchpoint is %s. */
11449 &remote_set_cmdlist, &remote_show_cmdlist);
11450 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
11451 &remote_hw_breakpoint_limit, _("\
11452 Set the maximum number of target hardware breakpoints."), _("\
11453 Show the maximum number of target hardware breakpoints."), _("\
11454 Specify a negative limit for unlimited."),
11455 NULL, NULL, /* FIXME: i18n: The maximum
11456 number of target hardware
11457 breakpoints is %s. */
11458 &remote_set_cmdlist, &remote_show_cmdlist);
11460 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
11461 &remote_address_size, _("\
11462 Set the maximum size of the address (in bits) in a memory packet."), _("\
11463 Show the maximum size of the address (in bits) in a memory packet."), NULL,
11465 NULL, /* FIXME: i18n: */
11466 &setlist, &showlist);
11468 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
11469 "X", "binary-download", 1);
11471 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
11472 "vCont", "verbose-resume", 0);
11474 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
11475 "QPassSignals", "pass-signals", 0);
11477 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
11478 "QProgramSignals", "program-signals", 0);
11480 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
11481 "qSymbol", "symbol-lookup", 0);
11483 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
11484 "P", "set-register", 1);
11486 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
11487 "p", "fetch-register", 1);
11489 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
11490 "Z0", "software-breakpoint", 0);
11492 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
11493 "Z1", "hardware-breakpoint", 0);
11495 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
11496 "Z2", "write-watchpoint", 0);
11498 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
11499 "Z3", "read-watchpoint", 0);
11501 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
11502 "Z4", "access-watchpoint", 0);
11504 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
11505 "qXfer:auxv:read", "read-aux-vector", 0);
11507 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
11508 "qXfer:features:read", "target-features", 0);
11510 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
11511 "qXfer:libraries:read", "library-info", 0);
11513 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
11514 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11516 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
11517 "qXfer:memory-map:read", "memory-map", 0);
11519 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
11520 "qXfer:spu:read", "read-spu-object", 0);
11522 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
11523 "qXfer:spu:write", "write-spu-object", 0);
11525 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
11526 "qXfer:osdata:read", "osdata", 0);
11528 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
11529 "qXfer:threads:read", "threads", 0);
11531 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
11532 "qXfer:siginfo:read", "read-siginfo-object", 0);
11534 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
11535 "qXfer:siginfo:write", "write-siginfo-object", 0);
11537 add_packet_config_cmd
11538 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
11539 "qXfer:trace-frame-info:read", "traceframe-info", 0);
11541 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
11542 "qXfer:uib:read", "unwind-info-block", 0);
11544 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
11545 "qGetTLSAddr", "get-thread-local-storage-address",
11548 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
11549 "qGetTIBAddr", "get-thread-information-block-address",
11552 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
11553 "bc", "reverse-continue", 0);
11555 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
11556 "bs", "reverse-step", 0);
11558 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
11559 "qSupported", "supported-packets", 0);
11561 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
11562 "qSearch:memory", "search-memory", 0);
11564 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
11565 "vFile:open", "hostio-open", 0);
11567 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
11568 "vFile:pread", "hostio-pread", 0);
11570 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
11571 "vFile:pwrite", "hostio-pwrite", 0);
11573 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
11574 "vFile:close", "hostio-close", 0);
11576 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
11577 "vFile:unlink", "hostio-unlink", 0);
11579 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
11580 "vFile:readlink", "hostio-readlink", 0);
11582 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
11583 "vAttach", "attach", 0);
11585 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
11588 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
11589 "QStartNoAckMode", "noack", 0);
11591 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
11592 "vKill", "kill", 0);
11594 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
11595 "qAttached", "query-attached", 0);
11597 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
11598 "ConditionalTracepoints",
11599 "conditional-tracepoints", 0);
11601 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
11602 "ConditionalBreakpoints",
11603 "conditional-breakpoints", 0);
11605 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
11606 "BreakpointCommands",
11607 "breakpoint-commands", 0);
11609 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
11610 "FastTracepoints", "fast-tracepoints", 0);
11612 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
11613 "TracepointSource", "TracepointSource", 0);
11615 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
11616 "QAllow", "allow", 0);
11618 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
11619 "StaticTracepoints", "static-tracepoints", 0);
11621 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
11622 "InstallInTrace", "install-in-trace", 0);
11624 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
11625 "qXfer:statictrace:read", "read-sdata-object", 0);
11627 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
11628 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
11630 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
11631 "QDisableRandomization", "disable-randomization", 0);
11633 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
11634 "QAgent", "agent", 0);
11636 /* Keep the old ``set remote Z-packet ...'' working. Each individual
11637 Z sub-packet has its own set and show commands, but users may
11638 have sets to this variable in their .gdbinit files (or in their
11640 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
11641 &remote_Z_packet_detect, _("\
11642 Set use of remote protocol `Z' packets"), _("\
11643 Show use of remote protocol `Z' packets "), _("\
11644 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
11646 set_remote_protocol_Z_packet_cmd,
11647 show_remote_protocol_Z_packet_cmd,
11648 /* FIXME: i18n: Use of remote protocol
11649 `Z' packets is %s. */
11650 &remote_set_cmdlist, &remote_show_cmdlist);
11652 add_prefix_cmd ("remote", class_files, remote_command, _("\
11653 Manipulate files on the remote system\n\
11654 Transfer files to and from the remote target system."),
11655 &remote_cmdlist, "remote ",
11656 0 /* allow-unknown */, &cmdlist);
11658 add_cmd ("put", class_files, remote_put_command,
11659 _("Copy a local file to the remote system."),
11662 add_cmd ("get", class_files, remote_get_command,
11663 _("Copy a remote file to the local system."),
11666 add_cmd ("delete", class_files, remote_delete_command,
11667 _("Delete a remote file."),
11670 remote_exec_file = xstrdup ("");
11671 add_setshow_string_noescape_cmd ("exec-file", class_files,
11672 &remote_exec_file, _("\
11673 Set the remote pathname for \"run\""), _("\
11674 Show the remote pathname for \"run\""), NULL, NULL, NULL,
11675 &remote_set_cmdlist, &remote_show_cmdlist);
11677 /* Eventually initialize fileio. See fileio.c */
11678 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
11680 /* Take advantage of the fact that the LWP field is not used, to tag
11681 special ptids with it set to != 0. */
11682 magic_null_ptid = ptid_build (42000, 1, -1);
11683 not_sent_ptid = ptid_build (42000, 1, -2);
11684 any_thread_ptid = ptid_build (42000, 1, 0);
11686 target_buf_size = 2048;
11687 target_buf = xmalloc (target_buf_size);