1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 2010 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
63 #include "xml-support.h"
65 #include "memory-map.h"
67 #include "tracepoint.h"
71 /* temp hacks for tracepoint encoding migration */
72 static char *target_buf
;
73 static long target_buf_size
;
75 encode_actions (struct breakpoint
*t
, struct bp_location
*tloc
,
76 char ***tdp_actions
, char ***stepping_actions
);
78 /* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88 enum { REMOTE_ALIGN_WRITES
= 16 };
90 /* Prototypes for local functions. */
91 static void cleanup_sigint_signal_handler (void *dummy
);
92 static void initialize_sigint_signal_handler (void);
93 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
94 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
97 static void handle_remote_sigint (int);
98 static void handle_remote_sigint_twice (int);
99 static void async_remote_interrupt (gdb_client_data
);
100 void async_remote_interrupt_twice (gdb_client_data
);
102 static void remote_files_info (struct target_ops
*ignore
);
104 static void remote_prepare_to_store (struct regcache
*regcache
);
106 static void remote_open (char *name
, int from_tty
);
108 static void extended_remote_open (char *name
, int from_tty
);
110 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
112 static void remote_close (int quitting
);
114 static void remote_mourn (struct target_ops
*ops
);
116 static void extended_remote_restart (void);
118 static void extended_remote_mourn (struct target_ops
*);
120 static void remote_mourn_1 (struct target_ops
*);
122 static void remote_send (char **buf
, long *sizeof_buf_p
);
124 static int readchar (int timeout
);
126 static void remote_kill (struct target_ops
*ops
);
128 static int tohex (int nib
);
130 static int remote_can_async_p (void);
132 static int remote_is_async_p (void);
134 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
135 void *context
), void *context
);
137 static int remote_async_mask (int new_mask
);
139 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
141 static void remote_interrupt (int signo
);
143 static void remote_interrupt_twice (int signo
);
145 static void interrupt_query (void);
147 static void set_general_thread (struct ptid ptid
);
148 static void set_continue_thread (struct ptid ptid
);
150 static void get_offsets (void);
152 static void skip_frame (void);
154 static long read_frame (char **buf_p
, long *sizeof_buf
);
156 static int hexnumlen (ULONGEST num
);
158 static void init_remote_ops (void);
160 static void init_extended_remote_ops (void);
162 static void remote_stop (ptid_t
);
164 static int ishex (int ch
, int *val
);
166 static int stubhex (int ch
);
168 static int hexnumstr (char *, ULONGEST
);
170 static int hexnumnstr (char *, ULONGEST
, int);
172 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
174 static void print_packet (char *);
176 static void compare_sections_command (char *, int);
178 static void packet_command (char *, int);
180 static int stub_unpack_int (char *buff
, int fieldlength
);
182 static ptid_t
remote_current_thread (ptid_t oldptid
);
184 static void remote_find_new_threads (void);
186 static void record_currthread (ptid_t currthread
);
188 static int fromhex (int a
);
190 extern int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
192 extern int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
194 static int putpkt_binary (char *buf
, int cnt
);
196 static void check_binary_download (CORE_ADDR addr
);
198 struct packet_config
;
200 static void show_packet_config_cmd (struct packet_config
*config
);
202 static void update_packet_config (struct packet_config
*config
);
204 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
205 struct cmd_list_element
*c
);
207 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
209 struct cmd_list_element
*c
,
212 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
213 static ptid_t
read_ptid (char *buf
, char **obuf
);
215 static void remote_set_permissions (void);
218 static int remote_get_trace_status (struct trace_status
*ts
);
220 static int remote_upload_tracepoints (struct uploaded_tp
**utpp
);
222 static int remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
);
224 static void remote_query_supported (void);
226 static void remote_check_symbols (struct objfile
*objfile
);
228 void _initialize_remote (void);
231 static struct stop_reply
*stop_reply_xmalloc (void);
232 static void stop_reply_xfree (struct stop_reply
*);
233 static void do_stop_reply_xfree (void *arg
);
234 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
235 static void push_stop_reply (struct stop_reply
*);
236 static void remote_get_pending_stop_replies (void);
237 static void discard_pending_stop_replies (int pid
);
238 static int peek_stop_reply (ptid_t ptid
);
240 static void remote_async_inferior_event_handler (gdb_client_data
);
241 static void remote_async_get_pending_events_handler (gdb_client_data
);
243 static void remote_terminal_ours (void);
245 static int remote_read_description_p (struct target_ops
*target
);
247 static void remote_console_output (char *msg
);
249 /* The non-stop remote protocol provisions for one pending stop reply.
250 This is where we keep it until it is acknowledged. */
252 static struct stop_reply
*pending_stop_reply
= NULL
;
256 static struct cmd_list_element
*remote_cmdlist
;
258 /* For "set remote" and "show remote". */
260 static struct cmd_list_element
*remote_set_cmdlist
;
261 static struct cmd_list_element
*remote_show_cmdlist
;
263 /* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
278 /* If we negotiated packet size explicitly (and thus can bypass
279 heuristics for the largest packet size that will not overflow
280 a buffer in the stub), this will be set to that packet size.
281 Otherwise zero, meaning to use the guessed size. */
282 long explicit_packet_size
;
284 /* remote_wait is normally called when the target is running and
285 waits for a stop reply packet. But sometimes we need to call it
286 when the target is already stopped. We can send a "?" packet
287 and have remote_wait read the response. Or, if we already have
288 the response, we can stash it in BUF and tell remote_wait to
289 skip calling getpkt. This flag is set when BUF contains a
290 stop reply packet and the target is not waiting. */
291 int cached_wait_status
;
293 /* True, if in no ack mode. That is, neither GDB nor the stub will
294 expect acks from each other. The connection is assumed to be
298 /* True if we're connected in extended remote mode. */
301 /* True if the stub reported support for multi-process
303 int multi_process_aware
;
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply
;
313 /* True if the stub reports support for non-stop mode. */
316 /* True if the stub reports support for vCont;t. */
319 /* True if the stub reports support for conditional tracepoints. */
320 int cond_tracepoints
;
322 /* True if the stub reports support for fast tracepoints. */
323 int fast_tracepoints
;
325 /* True if the stub can continue running a trace while GDB is
327 int disconnected_tracing
;
329 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
330 responded to that. */
334 /* Private data that we'll store in (struct thread_info)->private. */
335 struct private_thread_info
342 free_private_thread_info (struct private_thread_info
*info
)
348 /* Returns true if the multi-process extensions are in effect. */
350 remote_multi_process_p (struct remote_state
*rs
)
352 return rs
->extended
&& rs
->multi_process_aware
;
355 /* This data could be associated with a target, but we do not always
356 have access to the current target when we need it, so for now it is
357 static. This will be fine for as long as only one target is in use
359 static struct remote_state remote_state
;
361 static struct remote_state
*
362 get_remote_state_raw (void)
364 return &remote_state
;
367 /* Description of the remote protocol for a given architecture. */
371 long offset
; /* Offset into G packet. */
372 long regnum
; /* GDB's internal register number. */
373 LONGEST pnum
; /* Remote protocol register number. */
374 int in_g_packet
; /* Always part of G packet. */
375 /* long size in bytes; == register_size (target_gdbarch, regnum);
377 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
381 struct remote_arch_state
383 /* Description of the remote protocol registers. */
384 long sizeof_g_packet
;
386 /* Description of the remote protocol registers indexed by REGNUM
387 (making an array gdbarch_num_regs in size). */
388 struct packet_reg
*regs
;
390 /* This is the size (in chars) of the first response to the ``g''
391 packet. It is used as a heuristic when determining the maximum
392 size of memory-read and memory-write packets. A target will
393 typically only reserve a buffer large enough to hold the ``g''
394 packet. The size does not include packet overhead (headers and
396 long actual_register_packet_size
;
398 /* This is the maximum size (in chars) of a non read/write packet.
399 It is also used as a cap on the size of read/write packets. */
400 long remote_packet_size
;
403 long sizeof_pkt
= 2000;
405 /* Utility: generate error from an incoming stub packet. */
407 trace_error (char *buf
)
410 return; /* not an error msg */
413 case '1': /* malformed packet error */
414 if (*++buf
== '0') /* general case: */
415 error (_("remote.c: error in outgoing packet."));
417 error (_("remote.c: error in outgoing packet at field #%ld."),
418 strtol (buf
, NULL
, 16));
420 error (_("trace API error 0x%s."), ++buf
);
422 error (_("Target returns error code '%s'."), buf
);
426 /* Utility: wait for reply from stub, while accepting "O" packets. */
428 remote_get_noisy_reply (char **buf_p
,
431 do /* Loop on reply from remote stub. */
435 QUIT
; /* allow user to bail out with ^C */
436 getpkt (buf_p
, sizeof_buf
, 0);
440 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
443 CORE_ADDR from
, to
, org_to
;
445 int adjusted_size
= 0;
446 volatile struct gdb_exception ex
;
448 p
= buf
+ strlen ("qRelocInsn:");
449 pp
= unpack_varlen_hex (p
, &ul
);
451 error (_("invalid qRelocInsn packet: %s"), buf
);
455 pp
= unpack_varlen_hex (p
, &ul
);
460 TRY_CATCH (ex
, RETURN_MASK_ALL
)
462 gdbarch_relocate_instruction (target_gdbarch
, &to
, from
);
466 adjusted_size
= to
- org_to
;
468 sprintf (buf
, "qRelocInsn:%x", adjusted_size
);
471 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
473 /* Propagate memory errors silently back to the target.
474 The stub may have limited the range of addresses we
475 can write to, for example. */
480 /* Something unexpectedly bad happened. Be verbose so
481 we can tell what, and propagate the error back to the
482 stub, so it doesn't get stuck waiting for a
484 exception_fprintf (gdb_stderr
, ex
,
485 _("warning: relocating instruction: "));
489 else if (buf
[0] == 'O' && buf
[1] != 'K')
490 remote_console_output (buf
+ 1); /* 'O' message from stub */
492 return buf
; /* here's the actual reply */
497 /* Handle for retreving the remote protocol data from gdbarch. */
498 static struct gdbarch_data
*remote_gdbarch_data_handle
;
500 static struct remote_arch_state
*
501 get_remote_arch_state (void)
503 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
506 /* Fetch the global remote target state. */
508 static struct remote_state
*
509 get_remote_state (void)
511 /* Make sure that the remote architecture state has been
512 initialized, because doing so might reallocate rs->buf. Any
513 function which calls getpkt also needs to be mindful of changes
514 to rs->buf, but this call limits the number of places which run
516 get_remote_arch_state ();
518 return get_remote_state_raw ();
522 compare_pnums (const void *lhs_
, const void *rhs_
)
524 const struct packet_reg
* const *lhs
= lhs_
;
525 const struct packet_reg
* const *rhs
= rhs_
;
527 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
529 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
536 init_remote_state (struct gdbarch
*gdbarch
)
538 int regnum
, num_remote_regs
, offset
;
539 struct remote_state
*rs
= get_remote_state_raw ();
540 struct remote_arch_state
*rsa
;
541 struct packet_reg
**remote_regs
;
543 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
545 /* Use the architecture to build a regnum<->pnum table, which will be
546 1:1 unless a feature set specifies otherwise. */
547 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
548 gdbarch_num_regs (gdbarch
),
550 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
552 struct packet_reg
*r
= &rsa
->regs
[regnum
];
554 if (register_size (gdbarch
, regnum
) == 0)
555 /* Do not try to fetch zero-sized (placeholder) registers. */
558 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
563 /* Define the g/G packet format as the contents of each register
564 with a remote protocol number, in order of ascending protocol
567 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
568 * sizeof (struct packet_reg
*));
569 for (num_remote_regs
= 0, regnum
= 0;
570 regnum
< gdbarch_num_regs (gdbarch
);
572 if (rsa
->regs
[regnum
].pnum
!= -1)
573 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
575 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
578 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
580 remote_regs
[regnum
]->in_g_packet
= 1;
581 remote_regs
[regnum
]->offset
= offset
;
582 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
585 /* Record the maximum possible size of the g packet - it may turn out
587 rsa
->sizeof_g_packet
= offset
;
589 /* Default maximum number of characters in a packet body. Many
590 remote stubs have a hardwired buffer size of 400 bytes
591 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
592 as the maximum packet-size to ensure that the packet and an extra
593 NUL character can always fit in the buffer. This stops GDB
594 trashing stubs that try to squeeze an extra NUL into what is
595 already a full buffer (As of 1999-12-04 that was most stubs). */
596 rsa
->remote_packet_size
= 400 - 1;
598 /* This one is filled in when a ``g'' packet is received. */
599 rsa
->actual_register_packet_size
= 0;
601 /* Should rsa->sizeof_g_packet needs more space than the
602 default, adjust the size accordingly. Remember that each byte is
603 encoded as two characters. 32 is the overhead for the packet
604 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
605 (``$NN:G...#NN'') is a better guess, the below has been padded a
607 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
608 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
610 /* Make sure that the packet buffer is plenty big enough for
611 this architecture. */
612 if (rs
->buf_size
< rsa
->remote_packet_size
)
614 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
615 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
621 /* Return the current allowed size of a remote packet. This is
622 inferred from the current architecture, and should be used to
623 limit the length of outgoing packets. */
625 get_remote_packet_size (void)
627 struct remote_state
*rs
= get_remote_state ();
628 struct remote_arch_state
*rsa
= get_remote_arch_state ();
630 if (rs
->explicit_packet_size
)
631 return rs
->explicit_packet_size
;
633 return rsa
->remote_packet_size
;
636 static struct packet_reg
*
637 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
639 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
643 struct packet_reg
*r
= &rsa
->regs
[regnum
];
645 gdb_assert (r
->regnum
== regnum
);
650 static struct packet_reg
*
651 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
655 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
657 struct packet_reg
*r
= &rsa
->regs
[i
];
665 /* FIXME: graces/2002-08-08: These variables should eventually be
666 bound to an instance of the target object (as in gdbarch-tdep()),
667 when such a thing exists. */
669 /* This is set to the data address of the access causing the target
670 to stop for a watchpoint. */
671 static CORE_ADDR remote_watch_data_address
;
673 /* This is non-zero if target stopped for a watchpoint. */
674 static int remote_stopped_by_watchpoint_p
;
676 static struct target_ops remote_ops
;
678 static struct target_ops extended_remote_ops
;
680 static int remote_async_mask_value
= 1;
682 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
683 ``forever'' still use the normal timeout mechanism. This is
684 currently used by the ASYNC code to guarentee that target reads
685 during the initial connect always time-out. Once getpkt has been
686 modified to return a timeout indication and, in turn
687 remote_wait()/wait_for_inferior() have gained a timeout parameter
689 static int wait_forever_enabled_p
= 1;
691 /* Allow the user to specify what sequence to send to the remote
692 when he requests a program interruption: Although ^C is usually
693 what remote systems expect (this is the default, here), it is
694 sometimes preferable to send a break. On other systems such
695 as the Linux kernel, a break followed by g, which is Magic SysRq g
696 is required in order to interrupt the execution. */
697 const char interrupt_sequence_control_c
[] = "Ctrl-C";
698 const char interrupt_sequence_break
[] = "BREAK";
699 const char interrupt_sequence_break_g
[] = "BREAK-g";
700 static const char *interrupt_sequence_modes
[] =
702 interrupt_sequence_control_c
,
703 interrupt_sequence_break
,
704 interrupt_sequence_break_g
,
707 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
710 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
711 struct cmd_list_element
*c
,
714 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
715 fprintf_filtered (file
,
716 _("Send the ASCII ETX character (Ctrl-c) "
717 "to the remote target to interrupt the "
718 "execution of the program.\n"));
719 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
720 fprintf_filtered (file
,
721 _("send a break signal to the remote target "
722 "to interrupt the execution of the program.\n"));
723 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
724 fprintf_filtered (file
,
725 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
726 "the remote target to interrupt the execution "
727 "of Linux kernel.\n"));
729 internal_error (__FILE__
, __LINE__
,
730 _("Invalid value for interrupt_sequence_mode: %s."),
731 interrupt_sequence_mode
);
734 /* This boolean variable specifies whether interrupt_sequence is sent
735 to the remote target when gdb connects to it.
736 This is mostly needed when you debug the Linux kernel: The Linux kernel
737 expects BREAK g which is Magic SysRq g for connecting gdb. */
738 static int interrupt_on_connect
= 0;
740 /* This variable is used to implement the "set/show remotebreak" commands.
741 Since these commands are now deprecated in favor of "set/show remote
742 interrupt-sequence", it no longer has any effect on the code. */
743 static int remote_break
;
746 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
749 interrupt_sequence_mode
= interrupt_sequence_break
;
751 interrupt_sequence_mode
= interrupt_sequence_control_c
;
755 show_remotebreak (struct ui_file
*file
, int from_tty
,
756 struct cmd_list_element
*c
,
761 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
762 remote_open knows that we don't have a file open when the program
764 static struct serial
*remote_desc
= NULL
;
766 /* This variable sets the number of bits in an address that are to be
767 sent in a memory ("M" or "m") packet. Normally, after stripping
768 leading zeros, the entire address would be sent. This variable
769 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
770 initial implementation of remote.c restricted the address sent in
771 memory packets to ``host::sizeof long'' bytes - (typically 32
772 bits). Consequently, for 64 bit targets, the upper 32 bits of an
773 address was never sent. Since fixing this bug may cause a break in
774 some remote targets this variable is principly provided to
775 facilitate backward compatibility. */
777 static int remote_address_size
;
779 /* Temporary to track who currently owns the terminal. See
780 remote_terminal_* for more details. */
782 static int remote_async_terminal_ours_p
;
784 /* The executable file to use for "run" on the remote side. */
786 static char *remote_exec_file
= "";
789 /* User configurable variables for the number of characters in a
790 memory read/write packet. MIN (rsa->remote_packet_size,
791 rsa->sizeof_g_packet) is the default. Some targets need smaller
792 values (fifo overruns, et.al.) and some users need larger values
793 (speed up transfers). The variables ``preferred_*'' (the user
794 request), ``current_*'' (what was actually set) and ``forced_*''
795 (Positive - a soft limit, negative - a hard limit). */
797 struct memory_packet_config
804 /* Compute the current size of a read/write packet. Since this makes
805 use of ``actual_register_packet_size'' the computation is dynamic. */
808 get_memory_packet_size (struct memory_packet_config
*config
)
810 struct remote_state
*rs
= get_remote_state ();
811 struct remote_arch_state
*rsa
= get_remote_arch_state ();
813 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
814 law?) that some hosts don't cope very well with large alloca()
815 calls. Eventually the alloca() code will be replaced by calls to
816 xmalloc() and make_cleanups() allowing this restriction to either
817 be lifted or removed. */
818 #ifndef MAX_REMOTE_PACKET_SIZE
819 #define MAX_REMOTE_PACKET_SIZE 16384
821 /* NOTE: 20 ensures we can write at least one byte. */
822 #ifndef MIN_REMOTE_PACKET_SIZE
823 #define MIN_REMOTE_PACKET_SIZE 20
828 if (config
->size
<= 0)
829 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
831 what_they_get
= config
->size
;
835 what_they_get
= get_remote_packet_size ();
836 /* Limit the packet to the size specified by the user. */
838 && what_they_get
> config
->size
)
839 what_they_get
= config
->size
;
841 /* Limit it to the size of the targets ``g'' response unless we have
842 permission from the stub to use a larger packet size. */
843 if (rs
->explicit_packet_size
== 0
844 && rsa
->actual_register_packet_size
> 0
845 && what_they_get
> rsa
->actual_register_packet_size
)
846 what_they_get
= rsa
->actual_register_packet_size
;
848 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
849 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
850 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
851 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
853 /* Make sure there is room in the global buffer for this packet
854 (including its trailing NUL byte). */
855 if (rs
->buf_size
< what_they_get
+ 1)
857 rs
->buf_size
= 2 * what_they_get
;
858 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
861 return what_they_get
;
864 /* Update the size of a read/write packet. If they user wants
865 something really big then do a sanity check. */
868 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
870 int fixed_p
= config
->fixed_p
;
871 long size
= config
->size
;
874 error (_("Argument required (integer, `fixed' or `limited')."));
875 else if (strcmp (args
, "hard") == 0
876 || strcmp (args
, "fixed") == 0)
878 else if (strcmp (args
, "soft") == 0
879 || strcmp (args
, "limit") == 0)
885 size
= strtoul (args
, &end
, 0);
887 error (_("Invalid %s (bad syntax)."), config
->name
);
889 /* Instead of explicitly capping the size of a packet to
890 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
891 instead allowed to set the size to something arbitrarily
893 if (size
> MAX_REMOTE_PACKET_SIZE
)
894 error (_("Invalid %s (too large)."), config
->name
);
898 if (fixed_p
&& !config
->fixed_p
)
900 if (! query (_("The target may not be able to correctly handle a %s\n"
901 "of %ld bytes. Change the packet size? "),
903 error (_("Packet size not changed."));
905 /* Update the config. */
906 config
->fixed_p
= fixed_p
;
911 show_memory_packet_size (struct memory_packet_config
*config
)
913 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
915 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
916 get_memory_packet_size (config
));
918 printf_filtered (_("Packets are limited to %ld bytes.\n"),
919 get_memory_packet_size (config
));
922 static struct memory_packet_config memory_write_packet_config
=
924 "memory-write-packet-size",
928 set_memory_write_packet_size (char *args
, int from_tty
)
930 set_memory_packet_size (args
, &memory_write_packet_config
);
934 show_memory_write_packet_size (char *args
, int from_tty
)
936 show_memory_packet_size (&memory_write_packet_config
);
940 get_memory_write_packet_size (void)
942 return get_memory_packet_size (&memory_write_packet_config
);
945 static struct memory_packet_config memory_read_packet_config
=
947 "memory-read-packet-size",
951 set_memory_read_packet_size (char *args
, int from_tty
)
953 set_memory_packet_size (args
, &memory_read_packet_config
);
957 show_memory_read_packet_size (char *args
, int from_tty
)
959 show_memory_packet_size (&memory_read_packet_config
);
963 get_memory_read_packet_size (void)
965 long size
= get_memory_packet_size (&memory_read_packet_config
);
967 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
968 extra buffer size argument before the memory read size can be
969 increased beyond this. */
970 if (size
> get_remote_packet_size ())
971 size
= get_remote_packet_size ();
976 /* Generic configuration support for packets the stub optionally
977 supports. Allows the user to specify the use of the packet as well
978 as allowing GDB to auto-detect support in the remote stub. */
982 PACKET_SUPPORT_UNKNOWN
= 0,
991 enum auto_boolean detect
;
992 enum packet_support support
;
995 /* Analyze a packet's return value and update the packet config
1006 update_packet_config (struct packet_config
*config
)
1008 switch (config
->detect
)
1010 case AUTO_BOOLEAN_TRUE
:
1011 config
->support
= PACKET_ENABLE
;
1013 case AUTO_BOOLEAN_FALSE
:
1014 config
->support
= PACKET_DISABLE
;
1016 case AUTO_BOOLEAN_AUTO
:
1017 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1023 show_packet_config_cmd (struct packet_config
*config
)
1025 char *support
= "internal-error";
1027 switch (config
->support
)
1030 support
= "enabled";
1032 case PACKET_DISABLE
:
1033 support
= "disabled";
1035 case PACKET_SUPPORT_UNKNOWN
:
1036 support
= "unknown";
1039 switch (config
->detect
)
1041 case AUTO_BOOLEAN_AUTO
:
1042 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
1043 config
->name
, support
);
1045 case AUTO_BOOLEAN_TRUE
:
1046 case AUTO_BOOLEAN_FALSE
:
1047 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1048 config
->name
, support
);
1054 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1055 const char *title
, int legacy
)
1061 config
->name
= name
;
1062 config
->title
= title
;
1063 config
->detect
= AUTO_BOOLEAN_AUTO
;
1064 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1065 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1067 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
1069 /* set/show TITLE-packet {auto,on,off} */
1070 cmd_name
= xstrprintf ("%s-packet", title
);
1071 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1072 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
1073 set_remote_protocol_packet_cmd
,
1074 show_remote_protocol_packet_cmd
,
1075 &remote_set_cmdlist
, &remote_show_cmdlist
);
1076 /* The command code copies the documentation strings. */
1079 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1084 legacy_name
= xstrprintf ("%s-packet", name
);
1085 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1086 &remote_set_cmdlist
);
1087 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1088 &remote_show_cmdlist
);
1092 static enum packet_result
1093 packet_check_result (const char *buf
)
1097 /* The stub recognized the packet request. Check that the
1098 operation succeeded. */
1100 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1102 /* "Enn" - definitly an error. */
1103 return PACKET_ERROR
;
1105 /* Always treat "E." as an error. This will be used for
1106 more verbose error messages, such as E.memtypes. */
1107 if (buf
[0] == 'E' && buf
[1] == '.')
1108 return PACKET_ERROR
;
1110 /* The packet may or may not be OK. Just assume it is. */
1114 /* The stub does not support the packet. */
1115 return PACKET_UNKNOWN
;
1118 static enum packet_result
1119 packet_ok (const char *buf
, struct packet_config
*config
)
1121 enum packet_result result
;
1123 result
= packet_check_result (buf
);
1128 /* The stub recognized the packet request. */
1129 switch (config
->support
)
1131 case PACKET_SUPPORT_UNKNOWN
:
1133 fprintf_unfiltered (gdb_stdlog
,
1134 "Packet %s (%s) is supported\n",
1135 config
->name
, config
->title
);
1136 config
->support
= PACKET_ENABLE
;
1138 case PACKET_DISABLE
:
1139 internal_error (__FILE__
, __LINE__
,
1140 _("packet_ok: attempt to use a disabled packet"));
1146 case PACKET_UNKNOWN
:
1147 /* The stub does not support the packet. */
1148 switch (config
->support
)
1151 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1152 /* If the stub previously indicated that the packet was
1153 supported then there is a protocol error.. */
1154 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1155 config
->name
, config
->title
);
1157 /* The user set it wrong. */
1158 error (_("Enabled packet %s (%s) not recognized by stub"),
1159 config
->name
, config
->title
);
1161 case PACKET_SUPPORT_UNKNOWN
:
1163 fprintf_unfiltered (gdb_stdlog
,
1164 "Packet %s (%s) is NOT supported\n",
1165 config
->name
, config
->title
);
1166 config
->support
= PACKET_DISABLE
;
1168 case PACKET_DISABLE
:
1190 PACKET_vFile_pwrite
,
1192 PACKET_vFile_unlink
,
1194 PACKET_qXfer_features
,
1195 PACKET_qXfer_libraries
,
1196 PACKET_qXfer_memory_map
,
1197 PACKET_qXfer_spu_read
,
1198 PACKET_qXfer_spu_write
,
1199 PACKET_qXfer_osdata
,
1200 PACKET_qXfer_threads
,
1204 PACKET_QPassSignals
,
1205 PACKET_qSearch_memory
,
1208 PACKET_QStartNoAckMode
,
1210 PACKET_qXfer_siginfo_read
,
1211 PACKET_qXfer_siginfo_write
,
1213 PACKET_ConditionalTracepoints
,
1214 PACKET_FastTracepoints
,
1217 PACKET_TracepointSource
,
1222 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1225 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1226 struct cmd_list_element
*c
)
1228 struct packet_config
*packet
;
1230 for (packet
= remote_protocol_packets
;
1231 packet
< &remote_protocol_packets
[PACKET_MAX
];
1234 if (&packet
->detect
== c
->var
)
1236 update_packet_config (packet
);
1240 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1245 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1246 struct cmd_list_element
*c
,
1249 struct packet_config
*packet
;
1251 for (packet
= remote_protocol_packets
;
1252 packet
< &remote_protocol_packets
[PACKET_MAX
];
1255 if (&packet
->detect
== c
->var
)
1257 show_packet_config_cmd (packet
);
1261 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1265 /* Should we try one of the 'Z' requests? */
1269 Z_PACKET_SOFTWARE_BP
,
1270 Z_PACKET_HARDWARE_BP
,
1277 /* For compatibility with older distributions. Provide a ``set remote
1278 Z-packet ...'' command that updates all the Z packet types. */
1280 static enum auto_boolean remote_Z_packet_detect
;
1283 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1284 struct cmd_list_element
*c
)
1288 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1290 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1291 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1296 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1297 struct cmd_list_element
*c
,
1302 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1304 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1308 /* Should we try the 'ThreadInfo' query packet?
1310 This variable (NOT available to the user: auto-detect only!)
1311 determines whether GDB will use the new, simpler "ThreadInfo"
1312 query or the older, more complex syntax for thread queries.
1313 This is an auto-detect variable (set to true at each connect,
1314 and set to false when the target fails to recognize it). */
1316 static int use_threadinfo_query
;
1317 static int use_threadextra_query
;
1319 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1320 static struct async_signal_handler
*sigint_remote_twice_token
;
1321 static struct async_signal_handler
*sigint_remote_token
;
1324 /* Asynchronous signal handle registered as event loop source for
1325 when we have pending events ready to be passed to the core. */
1327 static struct async_event_handler
*remote_async_inferior_event_token
;
1329 /* Asynchronous signal handle registered as event loop source for when
1330 the remote sent us a %Stop notification. The registered callback
1331 will do a vStopped sequence to pull the rest of the events out of
1332 the remote side into our event queue. */
1334 static struct async_event_handler
*remote_async_get_pending_events_token
;
1337 static ptid_t magic_null_ptid
;
1338 static ptid_t not_sent_ptid
;
1339 static ptid_t any_thread_ptid
;
1341 /* These are the threads which we last sent to the remote system. The
1342 TID member will be -1 for all or -2 for not sent yet. */
1344 static ptid_t general_thread
;
1345 static ptid_t continue_thread
;
1347 /* Find out if the stub attached to PID (and hence GDB should offer to
1348 detach instead of killing it when bailing out). */
1351 remote_query_attached (int pid
)
1353 struct remote_state
*rs
= get_remote_state ();
1355 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1358 if (remote_multi_process_p (rs
))
1359 sprintf (rs
->buf
, "qAttached:%x", pid
);
1361 sprintf (rs
->buf
, "qAttached");
1364 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1366 switch (packet_ok (rs
->buf
,
1367 &remote_protocol_packets
[PACKET_qAttached
]))
1370 if (strcmp (rs
->buf
, "1") == 0)
1374 warning (_("Remote failure reply: %s"), rs
->buf
);
1376 case PACKET_UNKNOWN
:
1383 /* Add PID to GDB's inferior table. Since we can be connected to a
1384 remote system before before knowing about any inferior, mark the
1385 target with execution when we find the first inferior. If ATTACHED
1386 is 1, then we had just attached to this inferior. If it is 0, then
1387 we just created this inferior. If it is -1, then try querying the
1388 remote stub to find out if it had attached to the inferior or
1391 static struct inferior
*
1392 remote_add_inferior (int pid
, int attached
)
1394 struct inferior
*inf
;
1396 /* Check whether this process we're learning about is to be
1397 considered attached, or if is to be considered to have been
1398 spawned by the stub. */
1400 attached
= remote_query_attached (pid
);
1402 if (gdbarch_has_global_solist (target_gdbarch
))
1404 /* If the target shares code across all inferiors, then every
1405 attach adds a new inferior. */
1406 inf
= add_inferior (pid
);
1408 /* ... and every inferior is bound to the same program space.
1409 However, each inferior may still have its own address
1411 inf
->aspace
= maybe_new_address_space ();
1412 inf
->pspace
= current_program_space
;
1416 /* In the traditional debugging scenario, there's a 1-1 match
1417 between program/address spaces. We simply bind the inferior
1418 to the program space's address space. */
1419 inf
= current_inferior ();
1420 inferior_appeared (inf
, pid
);
1423 inf
->attach_flag
= attached
;
1428 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1429 according to RUNNING. */
1432 remote_add_thread (ptid_t ptid
, int running
)
1436 set_executing (ptid
, running
);
1437 set_running (ptid
, running
);
1440 /* Come here when we learn about a thread id from the remote target.
1441 It may be the first time we hear about such thread, so take the
1442 opportunity to add it to GDB's thread list. In case this is the
1443 first time we're noticing its corresponding inferior, add it to
1444 GDB's inferior list as well. */
1447 remote_notice_new_inferior (ptid_t currthread
, int running
)
1449 /* If this is a new thread, add it to GDB's thread list.
1450 If we leave it up to WFI to do this, bad things will happen. */
1452 if (in_thread_list (currthread
) && is_exited (currthread
))
1454 /* We're seeing an event on a thread id we knew had exited.
1455 This has to be a new thread reusing the old id. Add it. */
1456 remote_add_thread (currthread
, running
);
1460 if (!in_thread_list (currthread
))
1462 struct inferior
*inf
= NULL
;
1463 int pid
= ptid_get_pid (currthread
);
1465 if (ptid_is_pid (inferior_ptid
)
1466 && pid
== ptid_get_pid (inferior_ptid
))
1468 /* inferior_ptid has no thread member yet. This can happen
1469 with the vAttach -> remote_wait,"TAAthread:" path if the
1470 stub doesn't support qC. This is the first stop reported
1471 after an attach, so this is the main thread. Update the
1472 ptid in the thread list. */
1473 if (in_thread_list (pid_to_ptid (pid
)))
1474 thread_change_ptid (inferior_ptid
, currthread
);
1477 remote_add_thread (currthread
, running
);
1478 inferior_ptid
= currthread
;
1483 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1485 /* inferior_ptid is not set yet. This can happen with the
1486 vRun -> remote_wait,"TAAthread:" path if the stub
1487 doesn't support qC. This is the first stop reported
1488 after an attach, so this is the main thread. Update the
1489 ptid in the thread list. */
1490 thread_change_ptid (inferior_ptid
, currthread
);
1494 /* When connecting to a target remote, or to a target
1495 extended-remote which already was debugging an inferior, we
1496 may not know about it yet. Add it before adding its child
1497 thread, so notifications are emitted in a sensible order. */
1498 if (!in_inferior_list (ptid_get_pid (currthread
)))
1499 inf
= remote_add_inferior (ptid_get_pid (currthread
), -1);
1501 /* This is really a new thread. Add it. */
1502 remote_add_thread (currthread
, running
);
1504 /* If we found a new inferior, let the common code do whatever
1505 it needs to with it (e.g., read shared libraries, insert
1508 notice_new_inferior (currthread
, running
, 0);
1512 /* Return the private thread data, creating it if necessary. */
1514 struct private_thread_info
*
1515 demand_private_info (ptid_t ptid
)
1517 struct thread_info
*info
= find_thread_ptid (ptid
);
1523 info
->private = xmalloc (sizeof (*(info
->private)));
1524 info
->private_dtor
= free_private_thread_info
;
1525 info
->private->core
= -1;
1526 info
->private->extra
= 0;
1529 return info
->private;
1532 /* Call this function as a result of
1533 1) A halt indication (T packet) containing a thread id
1534 2) A direct query of currthread
1535 3) Successful execution of set thread
1539 record_currthread (ptid_t currthread
)
1541 general_thread
= currthread
;
1544 static char *last_pass_packet
;
1546 /* If 'QPassSignals' is supported, tell the remote stub what signals
1547 it can simply pass through to the inferior without reporting. */
1550 remote_pass_signals (void)
1552 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1554 char *pass_packet
, *p
;
1555 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1558 gdb_assert (numsigs
< 256);
1559 for (i
= 0; i
< numsigs
; i
++)
1561 if (signal_stop_state (i
) == 0
1562 && signal_print_state (i
) == 0
1563 && signal_pass_state (i
) == 1)
1566 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1567 strcpy (pass_packet
, "QPassSignals:");
1568 p
= pass_packet
+ strlen (pass_packet
);
1569 for (i
= 0; i
< numsigs
; i
++)
1571 if (signal_stop_state (i
) == 0
1572 && signal_print_state (i
) == 0
1573 && signal_pass_state (i
) == 1)
1576 *p
++ = tohex (i
>> 4);
1577 *p
++ = tohex (i
& 15);
1586 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1588 struct remote_state
*rs
= get_remote_state ();
1589 char *buf
= rs
->buf
;
1591 putpkt (pass_packet
);
1592 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1593 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1594 if (last_pass_packet
)
1595 xfree (last_pass_packet
);
1596 last_pass_packet
= pass_packet
;
1599 xfree (pass_packet
);
1604 remote_notice_signals (ptid_t ptid
)
1606 /* Update the remote on signals to silently pass, if they've
1608 remote_pass_signals ();
1611 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1612 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1613 thread. If GEN is set, set the general thread, if not, then set
1614 the step/continue thread. */
1616 set_thread (struct ptid ptid
, int gen
)
1618 struct remote_state
*rs
= get_remote_state ();
1619 ptid_t state
= gen
? general_thread
: continue_thread
;
1620 char *buf
= rs
->buf
;
1621 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1623 if (ptid_equal (state
, ptid
))
1627 *buf
++ = gen
? 'g' : 'c';
1628 if (ptid_equal (ptid
, magic_null_ptid
))
1629 xsnprintf (buf
, endbuf
- buf
, "0");
1630 else if (ptid_equal (ptid
, any_thread_ptid
))
1631 xsnprintf (buf
, endbuf
- buf
, "0");
1632 else if (ptid_equal (ptid
, minus_one_ptid
))
1633 xsnprintf (buf
, endbuf
- buf
, "-1");
1635 write_ptid (buf
, endbuf
, ptid
);
1637 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1639 general_thread
= ptid
;
1641 continue_thread
= ptid
;
1645 set_general_thread (struct ptid ptid
)
1647 set_thread (ptid
, 1);
1651 set_continue_thread (struct ptid ptid
)
1653 set_thread (ptid
, 0);
1656 /* Change the remote current process. Which thread within the process
1657 ends up selected isn't important, as long as it is the same process
1658 as what INFERIOR_PTID points to.
1660 This comes from that fact that there is no explicit notion of
1661 "selected process" in the protocol. The selected process for
1662 general operations is the process the selected general thread
1666 set_general_process (void)
1668 struct remote_state
*rs
= get_remote_state ();
1670 /* If the remote can't handle multiple processes, don't bother. */
1671 if (!remote_multi_process_p (rs
))
1674 /* We only need to change the remote current thread if it's pointing
1675 at some other process. */
1676 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1677 set_general_thread (inferior_ptid
);
1681 /* Return nonzero if the thread PTID is still alive on the remote
1685 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1687 struct remote_state
*rs
= get_remote_state ();
1690 if (ptid_equal (ptid
, magic_null_ptid
))
1691 /* The main thread is always alive. */
1694 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1695 /* The main thread is always alive. This can happen after a
1696 vAttach, if the remote side doesn't support
1701 endp
= rs
->buf
+ get_remote_packet_size ();
1704 write_ptid (p
, endp
, ptid
);
1707 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1708 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1711 /* About these extended threadlist and threadinfo packets. They are
1712 variable length packets but, the fields within them are often fixed
1713 length. They are redundent enough to send over UDP as is the
1714 remote protocol in general. There is a matching unit test module
1717 #define OPAQUETHREADBYTES 8
1719 /* a 64 bit opaque identifier */
1720 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1722 /* WARNING: This threadref data structure comes from the remote O.S.,
1723 libstub protocol encoding, and remote.c. it is not particularly
1726 /* Right now, the internal structure is int. We want it to be bigger.
1730 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1732 /* gdb_ext_thread_info is an internal GDB data structure which is
1733 equivalent to the reply of the remote threadinfo packet. */
1735 struct gdb_ext_thread_info
1737 threadref threadid
; /* External form of thread reference. */
1738 int active
; /* Has state interesting to GDB?
1740 char display
[256]; /* Brief state display, name,
1741 blocked/suspended. */
1742 char shortname
[32]; /* To be used to name threads. */
1743 char more_display
[256]; /* Long info, statistics, queue depth,
1747 /* The volume of remote transfers can be limited by submitting
1748 a mask containing bits specifying the desired information.
1749 Use a union of these values as the 'selection' parameter to
1750 get_thread_info. FIXME: Make these TAG names more thread specific.
1753 #define TAG_THREADID 1
1754 #define TAG_EXISTS 2
1755 #define TAG_DISPLAY 4
1756 #define TAG_THREADNAME 8
1757 #define TAG_MOREDISPLAY 16
1759 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1761 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1763 static char *unpack_nibble (char *buf
, int *val
);
1765 static char *pack_nibble (char *buf
, int nibble
);
1767 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1769 static char *unpack_byte (char *buf
, int *value
);
1771 static char *pack_int (char *buf
, int value
);
1773 static char *unpack_int (char *buf
, int *value
);
1775 static char *unpack_string (char *src
, char *dest
, int length
);
1777 static char *pack_threadid (char *pkt
, threadref
*id
);
1779 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1781 void int_to_threadref (threadref
*id
, int value
);
1783 static int threadref_to_int (threadref
*ref
);
1785 static void copy_threadref (threadref
*dest
, threadref
*src
);
1787 static int threadmatch (threadref
*dest
, threadref
*src
);
1789 static char *pack_threadinfo_request (char *pkt
, int mode
,
1792 static int remote_unpack_thread_info_response (char *pkt
,
1793 threadref
*expectedref
,
1794 struct gdb_ext_thread_info
1798 static int remote_get_threadinfo (threadref
*threadid
,
1799 int fieldset
, /*TAG mask */
1800 struct gdb_ext_thread_info
*info
);
1802 static char *pack_threadlist_request (char *pkt
, int startflag
,
1804 threadref
*nextthread
);
1806 static int parse_threadlist_response (char *pkt
,
1808 threadref
*original_echo
,
1809 threadref
*resultlist
,
1812 static int remote_get_threadlist (int startflag
,
1813 threadref
*nextthread
,
1817 threadref
*threadlist
);
1819 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1821 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1822 void *context
, int looplimit
);
1824 static int remote_newthread_step (threadref
*ref
, void *context
);
1827 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1828 buffer we're allowed to write to. Returns
1829 BUF+CHARACTERS_WRITTEN. */
1832 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1835 struct remote_state
*rs
= get_remote_state ();
1837 if (remote_multi_process_p (rs
))
1839 pid
= ptid_get_pid (ptid
);
1841 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1843 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1845 tid
= ptid_get_tid (ptid
);
1847 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1849 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1854 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1855 passed the last parsed char. Returns null_ptid on error. */
1858 read_ptid (char *buf
, char **obuf
)
1862 ULONGEST pid
= 0, tid
= 0;
1866 /* Multi-process ptid. */
1867 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1869 error (_("invalid remote ptid: %s\n"), p
);
1872 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1875 return ptid_build (pid
, 0, tid
);
1878 /* No multi-process. Just a tid. */
1879 pp
= unpack_varlen_hex (p
, &tid
);
1881 /* Since the stub is not sending a process id, then default to
1882 what's in inferior_ptid, unless it's null at this point. If so,
1883 then since there's no way to know the pid of the reported
1884 threads, use the magic number. */
1885 if (ptid_equal (inferior_ptid
, null_ptid
))
1886 pid
= ptid_get_pid (magic_null_ptid
);
1888 pid
= ptid_get_pid (inferior_ptid
);
1892 return ptid_build (pid
, 0, tid
);
1895 /* Encode 64 bits in 16 chars of hex. */
1897 static const char hexchars
[] = "0123456789abcdef";
1900 ishex (int ch
, int *val
)
1902 if ((ch
>= 'a') && (ch
<= 'f'))
1904 *val
= ch
- 'a' + 10;
1907 if ((ch
>= 'A') && (ch
<= 'F'))
1909 *val
= ch
- 'A' + 10;
1912 if ((ch
>= '0') && (ch
<= '9'))
1923 if (ch
>= 'a' && ch
<= 'f')
1924 return ch
- 'a' + 10;
1925 if (ch
>= '0' && ch
<= '9')
1927 if (ch
>= 'A' && ch
<= 'F')
1928 return ch
- 'A' + 10;
1933 stub_unpack_int (char *buff
, int fieldlength
)
1940 nibble
= stubhex (*buff
++);
1944 retval
= retval
<< 4;
1950 unpack_varlen_hex (char *buff
, /* packet to parse */
1954 ULONGEST retval
= 0;
1956 while (ishex (*buff
, &nibble
))
1959 retval
= retval
<< 4;
1960 retval
|= nibble
& 0x0f;
1967 unpack_nibble (char *buf
, int *val
)
1969 *val
= fromhex (*buf
++);
1974 pack_nibble (char *buf
, int nibble
)
1976 *buf
++ = hexchars
[(nibble
& 0x0f)];
1981 pack_hex_byte (char *pkt
, int byte
)
1983 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1984 *pkt
++ = hexchars
[(byte
& 0xf)];
1989 unpack_byte (char *buf
, int *value
)
1991 *value
= stub_unpack_int (buf
, 2);
1996 pack_int (char *buf
, int value
)
1998 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1999 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2000 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2001 buf
= pack_hex_byte (buf
, (value
& 0xff));
2006 unpack_int (char *buf
, int *value
)
2008 *value
= stub_unpack_int (buf
, 8);
2012 #if 0 /* Currently unused, uncomment when needed. */
2013 static char *pack_string (char *pkt
, char *string
);
2016 pack_string (char *pkt
, char *string
)
2021 len
= strlen (string
);
2023 len
= 200; /* Bigger than most GDB packets, junk??? */
2024 pkt
= pack_hex_byte (pkt
, len
);
2028 if ((ch
== '\0') || (ch
== '#'))
2029 ch
= '*'; /* Protect encapsulation. */
2034 #endif /* 0 (unused) */
2037 unpack_string (char *src
, char *dest
, int length
)
2046 pack_threadid (char *pkt
, threadref
*id
)
2049 unsigned char *altid
;
2051 altid
= (unsigned char *) id
;
2052 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2054 pkt
= pack_hex_byte (pkt
, *altid
++);
2060 unpack_threadid (char *inbuf
, threadref
*id
)
2063 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2066 altref
= (char *) id
;
2068 while (inbuf
< limit
)
2070 x
= stubhex (*inbuf
++);
2071 y
= stubhex (*inbuf
++);
2072 *altref
++ = (x
<< 4) | y
;
2077 /* Externally, threadrefs are 64 bits but internally, they are still
2078 ints. This is due to a mismatch of specifications. We would like
2079 to use 64bit thread references internally. This is an adapter
2083 int_to_threadref (threadref
*id
, int value
)
2085 unsigned char *scan
;
2087 scan
= (unsigned char *) id
;
2093 *scan
++ = (value
>> 24) & 0xff;
2094 *scan
++ = (value
>> 16) & 0xff;
2095 *scan
++ = (value
>> 8) & 0xff;
2096 *scan
++ = (value
& 0xff);
2100 threadref_to_int (threadref
*ref
)
2103 unsigned char *scan
;
2109 value
= (value
<< 8) | ((*scan
++) & 0xff);
2114 copy_threadref (threadref
*dest
, threadref
*src
)
2117 unsigned char *csrc
, *cdest
;
2119 csrc
= (unsigned char *) src
;
2120 cdest
= (unsigned char *) dest
;
2127 threadmatch (threadref
*dest
, threadref
*src
)
2129 /* Things are broken right now, so just assume we got a match. */
2131 unsigned char *srcp
, *destp
;
2133 srcp
= (char *) src
;
2134 destp
= (char *) dest
;
2138 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2145 threadid:1, # always request threadid
2152 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2155 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2157 *pkt
++ = 'q'; /* Info Query */
2158 *pkt
++ = 'P'; /* process or thread info */
2159 pkt
= pack_int (pkt
, mode
); /* mode */
2160 pkt
= pack_threadid (pkt
, id
); /* threadid */
2161 *pkt
= '\0'; /* terminate */
2165 /* These values tag the fields in a thread info response packet. */
2166 /* Tagging the fields allows us to request specific fields and to
2167 add more fields as time goes by. */
2169 #define TAG_THREADID 1 /* Echo the thread identifier. */
2170 #define TAG_EXISTS 2 /* Is this process defined enough to
2171 fetch registers and its stack? */
2172 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2173 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2174 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2178 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2179 struct gdb_ext_thread_info
*info
)
2181 struct remote_state
*rs
= get_remote_state ();
2185 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2188 /* info->threadid = 0; FIXME: implement zero_threadref. */
2190 info
->display
[0] = '\0';
2191 info
->shortname
[0] = '\0';
2192 info
->more_display
[0] = '\0';
2194 /* Assume the characters indicating the packet type have been
2196 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2197 pkt
= unpack_threadid (pkt
, &ref
);
2200 warning (_("Incomplete response to threadinfo request."));
2201 if (!threadmatch (&ref
, expectedref
))
2202 { /* This is an answer to a different request. */
2203 warning (_("ERROR RMT Thread info mismatch."));
2206 copy_threadref (&info
->threadid
, &ref
);
2208 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2210 /* Packets are terminated with nulls. */
2211 while ((pkt
< limit
) && mask
&& *pkt
)
2213 pkt
= unpack_int (pkt
, &tag
); /* tag */
2214 pkt
= unpack_byte (pkt
, &length
); /* length */
2215 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2217 warning (_("ERROR RMT: threadinfo tag mismatch."));
2221 if (tag
== TAG_THREADID
)
2225 warning (_("ERROR RMT: length of threadid is not 16."));
2229 pkt
= unpack_threadid (pkt
, &ref
);
2230 mask
= mask
& ~TAG_THREADID
;
2233 if (tag
== TAG_EXISTS
)
2235 info
->active
= stub_unpack_int (pkt
, length
);
2237 mask
= mask
& ~(TAG_EXISTS
);
2240 warning (_("ERROR RMT: 'exists' length too long."));
2246 if (tag
== TAG_THREADNAME
)
2248 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2249 mask
= mask
& ~TAG_THREADNAME
;
2252 if (tag
== TAG_DISPLAY
)
2254 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2255 mask
= mask
& ~TAG_DISPLAY
;
2258 if (tag
== TAG_MOREDISPLAY
)
2260 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2261 mask
= mask
& ~TAG_MOREDISPLAY
;
2264 warning (_("ERROR RMT: unknown thread info tag."));
2265 break; /* Not a tag we know about. */
2271 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2272 struct gdb_ext_thread_info
*info
)
2274 struct remote_state
*rs
= get_remote_state ();
2277 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2279 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2281 if (rs
->buf
[0] == '\0')
2284 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2289 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2292 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2293 threadref
*nextthread
)
2295 *pkt
++ = 'q'; /* info query packet */
2296 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2297 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2298 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2299 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2304 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2307 parse_threadlist_response (char *pkt
, int result_limit
,
2308 threadref
*original_echo
, threadref
*resultlist
,
2311 struct remote_state
*rs
= get_remote_state ();
2313 int count
, resultcount
, done
;
2316 /* Assume the 'q' and 'M chars have been stripped. */
2317 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2318 /* done parse past here */
2319 pkt
= unpack_byte (pkt
, &count
); /* count field */
2320 pkt
= unpack_nibble (pkt
, &done
);
2321 /* The first threadid is the argument threadid. */
2322 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2323 while ((count
-- > 0) && (pkt
< limit
))
2325 pkt
= unpack_threadid (pkt
, resultlist
++);
2326 if (resultcount
++ >= result_limit
)
2335 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2336 int *done
, int *result_count
, threadref
*threadlist
)
2338 struct remote_state
*rs
= get_remote_state ();
2339 static threadref echo_nextthread
;
2342 /* Trancate result limit to be smaller than the packet size. */
2343 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
2344 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2346 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2348 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2350 if (*rs
->buf
== '\0')
2354 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2357 if (!threadmatch (&echo_nextthread
, nextthread
))
2359 /* FIXME: This is a good reason to drop the packet. */
2360 /* Possably, there is a duplicate response. */
2362 retransmit immediatly - race conditions
2363 retransmit after timeout - yes
2365 wait for packet, then exit
2367 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2368 return 0; /* I choose simply exiting. */
2370 if (*result_count
<= 0)
2374 warning (_("RMT ERROR : failed to get remote thread list."));
2377 return result
; /* break; */
2379 if (*result_count
> result_limit
)
2382 warning (_("RMT ERROR: threadlist response longer than requested."));
2388 /* This is the interface between remote and threads, remotes upper
2391 /* remote_find_new_threads retrieves the thread list and for each
2392 thread in the list, looks up the thread in GDB's internal list,
2393 adding the thread if it does not already exist. This involves
2394 getting partial thread lists from the remote target so, polling the
2395 quit_flag is required. */
2398 /* About this many threadisds fit in a packet. */
2400 #define MAXTHREADLISTRESULTS 32
2403 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2406 int done
, i
, result_count
;
2410 static threadref nextthread
;
2411 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2416 if (loopcount
++ > looplimit
)
2419 warning (_("Remote fetch threadlist -infinite loop-."));
2422 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2423 &done
, &result_count
, resultthreadlist
))
2428 /* Clear for later iterations. */
2430 /* Setup to resume next batch of thread references, set nextthread. */
2431 if (result_count
>= 1)
2432 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2434 while (result_count
--)
2435 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2442 remote_newthread_step (threadref
*ref
, void *context
)
2444 int pid
= ptid_get_pid (inferior_ptid
);
2445 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2447 if (!in_thread_list (ptid
))
2449 return 1; /* continue iterator */
2452 #define CRAZY_MAX_THREADS 1000
2455 remote_current_thread (ptid_t oldpid
)
2457 struct remote_state
*rs
= get_remote_state ();
2460 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2461 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2462 return read_ptid (&rs
->buf
[2], NULL
);
2467 /* Find new threads for info threads command.
2468 * Original version, using John Metzler's thread protocol.
2472 remote_find_new_threads (void)
2474 remote_threadlist_iterator (remote_newthread_step
, 0,
2478 #if defined(HAVE_LIBEXPAT)
2480 typedef struct thread_item
2486 DEF_VEC_O(thread_item_t
);
2488 struct threads_parsing_context
2490 VEC (thread_item_t
) *items
;
2494 start_thread (struct gdb_xml_parser
*parser
,
2495 const struct gdb_xml_element
*element
,
2496 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2498 struct threads_parsing_context
*data
= user_data
;
2500 struct thread_item item
;
2503 id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
2504 item
.ptid
= read_ptid (id
, NULL
);
2506 if (VEC_length (gdb_xml_value_s
, attributes
) > 1)
2507 item
.core
= *(ULONGEST
*) VEC_index (gdb_xml_value_s
, attributes
, 1)->value
;
2513 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2517 end_thread (struct gdb_xml_parser
*parser
,
2518 const struct gdb_xml_element
*element
,
2519 void *user_data
, const char *body_text
)
2521 struct threads_parsing_context
*data
= user_data
;
2523 if (body_text
&& *body_text
)
2524 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2527 const struct gdb_xml_attribute thread_attributes
[] = {
2528 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2529 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2530 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2533 const struct gdb_xml_element thread_children
[] = {
2534 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2537 const struct gdb_xml_element threads_children
[] = {
2538 { "thread", thread_attributes
, thread_children
,
2539 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2540 start_thread
, end_thread
},
2541 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2544 const struct gdb_xml_element threads_elements
[] = {
2545 { "threads", NULL
, threads_children
,
2546 GDB_XML_EF_NONE
, NULL
, NULL
},
2547 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2550 /* Discard the contents of the constructed thread info context. */
2553 clear_threads_parsing_context (void *p
)
2555 struct threads_parsing_context
*context
= p
;
2557 struct thread_item
*item
;
2559 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2560 xfree (item
->extra
);
2562 VEC_free (thread_item_t
, context
->items
);
2568 * Find all threads for info threads command.
2569 * Uses new thread protocol contributed by Cisco.
2570 * Falls back and attempts to use the older method (above)
2571 * if the target doesn't respond to the new method.
2575 remote_threads_info (struct target_ops
*ops
)
2577 struct remote_state
*rs
= get_remote_state ();
2581 if (remote_desc
== 0) /* paranoia */
2582 error (_("Command can only be used when connected to the remote target."));
2584 #if defined(HAVE_LIBEXPAT)
2585 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2587 char *xml
= target_read_stralloc (¤t_target
,
2588 TARGET_OBJECT_THREADS
, NULL
);
2590 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2593 struct gdb_xml_parser
*parser
;
2594 struct threads_parsing_context context
;
2595 struct cleanup
*clear_parsing_context
;
2598 /* Note: this parser cleanup is already guarded by BACK_TO
2600 parser
= gdb_xml_create_parser_and_cleanup (_("threads"),
2604 gdb_xml_use_dtd (parser
, "threads.dtd");
2606 clear_parsing_context
2607 = make_cleanup (clear_threads_parsing_context
, &context
);
2609 if (gdb_xml_parse (parser
, xml
) == 0)
2612 struct thread_item
*item
;
2614 for (i
= 0; VEC_iterate (thread_item_t
, context
.items
, i
, item
); ++i
)
2616 if (!ptid_equal (item
->ptid
, null_ptid
))
2618 struct private_thread_info
*info
;
2619 /* In non-stop mode, we assume new found threads
2620 are running until proven otherwise with a
2621 stop reply. In all-stop, we can only get
2622 here if all threads are stopped. */
2623 int running
= non_stop
? 1 : 0;
2625 remote_notice_new_inferior (item
->ptid
, running
);
2627 info
= demand_private_info (item
->ptid
);
2628 info
->core
= item
->core
;
2629 info
->extra
= item
->extra
;
2635 do_cleanups (clear_parsing_context
);
2638 do_cleanups (back_to
);
2643 if (use_threadinfo_query
)
2645 putpkt ("qfThreadInfo");
2646 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2648 if (bufp
[0] != '\0') /* q packet recognized */
2650 while (*bufp
++ == 'm') /* reply contains one or more TID */
2654 new_thread
= read_ptid (bufp
, &bufp
);
2655 if (!ptid_equal (new_thread
, null_ptid
))
2657 /* In non-stop mode, we assume new found threads
2658 are running until proven otherwise with a
2659 stop reply. In all-stop, we can only get
2660 here if all threads are stopped. */
2661 int running
= non_stop
? 1 : 0;
2663 remote_notice_new_inferior (new_thread
, running
);
2666 while (*bufp
++ == ','); /* comma-separated list */
2667 putpkt ("qsThreadInfo");
2668 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2675 /* Only qfThreadInfo is supported in non-stop mode. */
2679 /* Else fall back to old method based on jmetzler protocol. */
2680 use_threadinfo_query
= 0;
2681 remote_find_new_threads ();
2686 * Collect a descriptive string about the given thread.
2687 * The target may say anything it wants to about the thread
2688 * (typically info about its blocked / runnable state, name, etc.).
2689 * This string will appear in the info threads display.
2691 * Optional: targets are not required to implement this function.
2695 remote_threads_extra_info (struct thread_info
*tp
)
2697 struct remote_state
*rs
= get_remote_state ();
2701 struct gdb_ext_thread_info threadinfo
;
2702 static char display_buf
[100]; /* arbitrary... */
2703 int n
= 0; /* position in display_buf */
2705 if (remote_desc
== 0) /* paranoia */
2706 internal_error (__FILE__
, __LINE__
,
2707 _("remote_threads_extra_info"));
2709 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2710 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2711 /* This is the main thread which was added by GDB. The remote
2712 server doesn't know about it. */
2715 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2717 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2719 if (info
&& info
->private)
2720 return info
->private->extra
;
2725 if (use_threadextra_query
)
2728 char *endb
= rs
->buf
+ get_remote_packet_size ();
2730 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2732 write_ptid (b
, endb
, tp
->ptid
);
2735 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2736 if (rs
->buf
[0] != 0)
2738 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2739 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2740 display_buf
[result
] = '\0';
2745 /* If the above query fails, fall back to the old method. */
2746 use_threadextra_query
= 0;
2747 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2748 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2749 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2750 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2751 if (threadinfo
.active
)
2753 if (*threadinfo
.shortname
)
2754 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2755 " Name: %s,", threadinfo
.shortname
);
2756 if (*threadinfo
.display
)
2757 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2758 " State: %s,", threadinfo
.display
);
2759 if (*threadinfo
.more_display
)
2760 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2761 " Priority: %s", threadinfo
.more_display
);
2765 /* For purely cosmetic reasons, clear up trailing commas. */
2766 if (',' == display_buf
[n
-1])
2767 display_buf
[n
-1] = ' ';
2775 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2778 remote_get_ada_task_ptid (long lwp
, long thread
)
2780 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2784 /* Restart the remote side; this is an extended protocol operation. */
2787 extended_remote_restart (void)
2789 struct remote_state
*rs
= get_remote_state ();
2791 /* Send the restart command; for reasons I don't understand the
2792 remote side really expects a number after the "R". */
2793 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2796 remote_fileio_reset ();
2799 /* Clean up connection to a remote debugger. */
2802 remote_close (int quitting
)
2804 if (remote_desc
== NULL
)
2805 return; /* already closed */
2807 /* Make sure we leave stdin registered in the event loop, and we
2808 don't leave the async SIGINT signal handler installed. */
2809 remote_terminal_ours ();
2811 serial_close (remote_desc
);
2814 /* We don't have a connection to the remote stub anymore. Get rid
2815 of all the inferiors and their threads we were controlling. */
2816 discard_all_inferiors ();
2817 inferior_ptid
= null_ptid
;
2819 /* We're no longer interested in any of these events. */
2820 discard_pending_stop_replies (-1);
2822 if (remote_async_inferior_event_token
)
2823 delete_async_event_handler (&remote_async_inferior_event_token
);
2824 if (remote_async_get_pending_events_token
)
2825 delete_async_event_handler (&remote_async_get_pending_events_token
);
2828 /* Query the remote side for the text, data and bss offsets. */
2833 struct remote_state
*rs
= get_remote_state ();
2836 int lose
, num_segments
= 0, do_sections
, do_segments
;
2837 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2838 struct section_offsets
*offs
;
2839 struct symfile_segment_data
*data
;
2841 if (symfile_objfile
== NULL
)
2844 putpkt ("qOffsets");
2845 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2848 if (buf
[0] == '\000')
2849 return; /* Return silently. Stub doesn't support
2853 warning (_("Remote failure reply: %s"), buf
);
2857 /* Pick up each field in turn. This used to be done with scanf, but
2858 scanf will make trouble if CORE_ADDR size doesn't match
2859 conversion directives correctly. The following code will work
2860 with any size of CORE_ADDR. */
2861 text_addr
= data_addr
= bss_addr
= 0;
2865 if (strncmp (ptr
, "Text=", 5) == 0)
2868 /* Don't use strtol, could lose on big values. */
2869 while (*ptr
&& *ptr
!= ';')
2870 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2872 if (strncmp (ptr
, ";Data=", 6) == 0)
2875 while (*ptr
&& *ptr
!= ';')
2876 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2881 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2884 while (*ptr
&& *ptr
!= ';')
2885 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2887 if (bss_addr
!= data_addr
)
2888 warning (_("Target reported unsupported offsets: %s"), buf
);
2893 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2896 /* Don't use strtol, could lose on big values. */
2897 while (*ptr
&& *ptr
!= ';')
2898 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2901 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2904 while (*ptr
&& *ptr
!= ';')
2905 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2913 error (_("Malformed response to offset query, %s"), buf
);
2914 else if (*ptr
!= '\0')
2915 warning (_("Target reported unsupported offsets: %s"), buf
);
2917 offs
= ((struct section_offsets
*)
2918 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2919 memcpy (offs
, symfile_objfile
->section_offsets
,
2920 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2922 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2923 do_segments
= (data
!= NULL
);
2924 do_sections
= num_segments
== 0;
2926 if (num_segments
> 0)
2928 segments
[0] = text_addr
;
2929 segments
[1] = data_addr
;
2931 /* If we have two segments, we can still try to relocate everything
2932 by assuming that the .text and .data offsets apply to the whole
2933 text and data segments. Convert the offsets given in the packet
2934 to base addresses for symfile_map_offsets_to_segments. */
2935 else if (data
&& data
->num_segments
== 2)
2937 segments
[0] = data
->segment_bases
[0] + text_addr
;
2938 segments
[1] = data
->segment_bases
[1] + data_addr
;
2941 /* If the object file has only one segment, assume that it is text
2942 rather than data; main programs with no writable data are rare,
2943 but programs with no code are useless. Of course the code might
2944 have ended up in the data segment... to detect that we would need
2945 the permissions here. */
2946 else if (data
&& data
->num_segments
== 1)
2948 segments
[0] = data
->segment_bases
[0] + text_addr
;
2951 /* There's no way to relocate by segment. */
2957 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2958 offs
, num_segments
, segments
);
2960 if (ret
== 0 && !do_sections
)
2961 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2968 free_symfile_segment_data (data
);
2972 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2974 /* This is a temporary kludge to force data and bss to use the same offsets
2975 because that's what nlmconv does now. The real solution requires changes
2976 to the stub and remote.c that I don't have time to do right now. */
2978 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2979 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2982 objfile_relocate (symfile_objfile
, offs
);
2985 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
2986 threads we know are stopped already. This is used during the
2987 initial remote connection in non-stop mode --- threads that are
2988 reported as already being stopped are left stopped. */
2991 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
2993 /* If we have a stop reply for this thread, it must be stopped. */
2994 if (peek_stop_reply (thread
->ptid
))
2995 set_stop_requested (thread
->ptid
, 1);
3000 /* Stub for catch_exception. */
3002 struct start_remote_args
3006 /* The current target. */
3007 struct target_ops
*target
;
3009 /* Non-zero if this is an extended-remote target. */
3013 /* Send interrupt_sequence to remote target. */
3015 send_interrupt_sequence ()
3017 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3018 serial_write (remote_desc
, "\x03", 1);
3019 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3020 serial_send_break (remote_desc
);
3021 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3023 serial_send_break (remote_desc
);
3024 serial_write (remote_desc
, "g", 1);
3027 internal_error (__FILE__
, __LINE__
,
3028 _("Invalid value for interrupt_sequence_mode: %s."),
3029 interrupt_sequence_mode
);
3033 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
3035 struct start_remote_args
*args
= opaque
;
3036 struct remote_state
*rs
= get_remote_state ();
3037 struct packet_config
*noack_config
;
3038 char *wait_status
= NULL
;
3040 immediate_quit
++; /* Allow user to interrupt it. */
3042 /* Ack any packet which the remote side has already sent. */
3043 serial_write (remote_desc
, "+", 1);
3045 if (interrupt_on_connect
)
3046 send_interrupt_sequence ();
3048 /* The first packet we send to the target is the optional "supported
3049 packets" request. If the target can answer this, it will tell us
3050 which later probes to skip. */
3051 remote_query_supported ();
3053 /* If the stub wants to get a QAllow, compose one and send it. */
3054 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3055 remote_set_permissions ();
3057 /* Next, we possibly activate noack mode.
3059 If the QStartNoAckMode packet configuration is set to AUTO,
3060 enable noack mode if the stub reported a wish for it with
3063 If set to TRUE, then enable noack mode even if the stub didn't
3064 report it in qSupported. If the stub doesn't reply OK, the
3065 session ends with an error.
3067 If FALSE, then don't activate noack mode, regardless of what the
3068 stub claimed should be the default with qSupported. */
3070 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3072 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3073 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3074 && noack_config
->support
== PACKET_ENABLE
))
3076 putpkt ("QStartNoAckMode");
3077 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3078 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3082 if (args
->extended_p
)
3084 /* Tell the remote that we are using the extended protocol. */
3086 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3089 /* Next, if the target can specify a description, read it. We do
3090 this before anything involving memory or registers. */
3091 target_find_description ();
3093 /* Next, now that we know something about the target, update the
3094 address spaces in the program spaces. */
3095 update_address_spaces ();
3097 /* On OSs where the list of libraries is global to all
3098 processes, we fetch them early. */
3099 if (gdbarch_has_global_solist (target_gdbarch
))
3100 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
3104 if (!rs
->non_stop_aware
)
3105 error (_("Non-stop mode requested, but remote does not support non-stop"));
3107 putpkt ("QNonStop:1");
3108 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3110 if (strcmp (rs
->buf
, "OK") != 0)
3111 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
3113 /* Find about threads and processes the stub is already
3114 controlling. We default to adding them in the running state.
3115 The '?' query below will then tell us about which threads are
3117 remote_threads_info (args
->target
);
3119 else if (rs
->non_stop_aware
)
3121 /* Don't assume that the stub can operate in all-stop mode.
3122 Request it explicitely. */
3123 putpkt ("QNonStop:0");
3124 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3126 if (strcmp (rs
->buf
, "OK") != 0)
3127 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
3130 /* Check whether the target is running now. */
3132 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3136 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3138 if (!args
->extended_p
)
3139 error (_("The target is not running (try extended-remote?)"));
3141 /* We're connected, but not running. Drop out before we
3142 call start_remote. */
3147 /* Save the reply for later. */
3148 wait_status
= alloca (strlen (rs
->buf
) + 1);
3149 strcpy (wait_status
, rs
->buf
);
3152 /* Let the stub know that we want it to return the thread. */
3153 set_continue_thread (minus_one_ptid
);
3155 /* Without this, some commands which require an active target
3156 (such as kill) won't work. This variable serves (at least)
3157 double duty as both the pid of the target process (if it has
3158 such), and as a flag indicating that a target is active.
3159 These functions should be split out into seperate variables,
3160 especially since GDB will someday have a notion of debugging
3161 several processes. */
3162 inferior_ptid
= magic_null_ptid
;
3164 /* Now, if we have thread information, update inferior_ptid. */
3165 inferior_ptid
= remote_current_thread (inferior_ptid
);
3167 remote_add_inferior (ptid_get_pid (inferior_ptid
), -1);
3169 /* Always add the main thread. */
3170 add_thread_silent (inferior_ptid
);
3172 get_offsets (); /* Get text, data & bss offsets. */
3174 /* If we could not find a description using qXfer, and we know
3175 how to do it some other way, try again. This is not
3176 supported for non-stop; it could be, but it is tricky if
3177 there are no stopped threads when we connect. */
3178 if (remote_read_description_p (args
->target
)
3179 && gdbarch_target_desc (target_gdbarch
) == NULL
)
3181 target_clear_description ();
3182 target_find_description ();
3185 /* Use the previously fetched status. */
3186 gdb_assert (wait_status
!= NULL
);
3187 strcpy (rs
->buf
, wait_status
);
3188 rs
->cached_wait_status
= 1;
3191 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
3195 /* Clear WFI global state. Do this before finding about new
3196 threads and inferiors, and setting the current inferior.
3197 Otherwise we would clear the proceed status of the current
3198 inferior when we want its stop_soon state to be preserved
3199 (see notice_new_inferior). */
3200 init_wait_for_inferior ();
3202 /* In non-stop, we will either get an "OK", meaning that there
3203 are no stopped threads at this time; or, a regular stop
3204 reply. In the latter case, there may be more than one thread
3205 stopped --- we pull them all out using the vStopped
3207 if (strcmp (rs
->buf
, "OK") != 0)
3209 struct stop_reply
*stop_reply
;
3210 struct cleanup
*old_chain
;
3212 stop_reply
= stop_reply_xmalloc ();
3213 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3215 remote_parse_stop_reply (rs
->buf
, stop_reply
);
3216 discard_cleanups (old_chain
);
3218 /* get_pending_stop_replies acks this one, and gets the rest
3220 pending_stop_reply
= stop_reply
;
3221 remote_get_pending_stop_replies ();
3223 /* Make sure that threads that were stopped remain
3225 iterate_over_threads (set_stop_requested_callback
, NULL
);
3228 if (target_can_async_p ())
3229 target_async (inferior_event_handler
, 0);
3231 if (thread_count () == 0)
3233 if (!args
->extended_p
)
3234 error (_("The target is not running (try extended-remote?)"));
3236 /* We're connected, but not running. Drop out before we
3237 call start_remote. */
3241 /* Let the stub know that we want it to return the thread. */
3243 /* Force the stub to choose a thread. */
3244 set_general_thread (null_ptid
);
3247 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3248 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3249 error (_("remote didn't report the current thread in non-stop mode"));
3251 get_offsets (); /* Get text, data & bss offsets. */
3253 /* In non-stop mode, any cached wait status will be stored in
3254 the stop reply queue. */
3255 gdb_assert (wait_status
== NULL
);
3257 /* Update the remote on signals to silently pass, or more
3258 importantly, which to not ignore, in case a previous session
3259 had set some different set of signals to be ignored. */
3260 remote_pass_signals ();
3263 /* If we connected to a live target, do some additional setup. */
3264 if (target_has_execution
)
3266 if (exec_bfd
) /* No use without an exec file. */
3267 remote_check_symbols (symfile_objfile
);
3270 /* Possibly the target has been engaged in a trace run started
3271 previously; find out where things are at. */
3272 if (remote_get_trace_status (current_trace_status ()) != -1)
3274 struct uploaded_tp
*uploaded_tps
= NULL
;
3275 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3277 if (current_trace_status ()->running
)
3278 printf_filtered (_("Trace is already running on the target.\n"));
3280 /* Get trace state variables first, they may be checked when
3281 parsing uploaded commands. */
3283 remote_upload_trace_state_variables (&uploaded_tsvs
);
3285 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3287 remote_upload_tracepoints (&uploaded_tps
);
3289 merge_uploaded_tracepoints (&uploaded_tps
);
3292 /* If breakpoints are global, insert them now. */
3293 if (gdbarch_has_global_breakpoints (target_gdbarch
)
3294 && breakpoints_always_inserted_mode ())
3295 insert_breakpoints ();
3298 /* Open a connection to a remote debugger.
3299 NAME is the filename used for communication. */
3302 remote_open (char *name
, int from_tty
)
3304 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3307 /* Open a connection to a remote debugger using the extended
3308 remote gdb protocol. NAME is the filename used for communication. */
3311 extended_remote_open (char *name
, int from_tty
)
3313 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3316 /* Generic code for opening a connection to a remote target. */
3319 init_all_packet_configs (void)
3323 for (i
= 0; i
< PACKET_MAX
; i
++)
3324 update_packet_config (&remote_protocol_packets
[i
]);
3327 /* Symbol look-up. */
3330 remote_check_symbols (struct objfile
*objfile
)
3332 struct remote_state
*rs
= get_remote_state ();
3333 char *msg
, *reply
, *tmp
;
3334 struct minimal_symbol
*sym
;
3337 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3340 /* Make sure the remote is pointing at the right process. */
3341 set_general_process ();
3343 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3344 because we need both at the same time. */
3345 msg
= alloca (get_remote_packet_size ());
3347 /* Invite target to request symbol lookups. */
3349 putpkt ("qSymbol::");
3350 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3351 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3354 while (strncmp (reply
, "qSymbol:", 8) == 0)
3357 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3359 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3361 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3364 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
3365 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3367 /* If this is a function address, return the start of code
3368 instead of any data function descriptor. */
3369 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
3373 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3374 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3378 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3383 static struct serial
*
3384 remote_serial_open (char *name
)
3386 static int udp_warning
= 0;
3388 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3389 of in ser-tcp.c, because it is the remote protocol assuming that the
3390 serial connection is reliable and not the serial connection promising
3392 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3395 The remote protocol may be unreliable over UDP.\n\
3396 Some events may be lost, rendering further debugging impossible."));
3400 return serial_open (name
);
3403 /* Inform the target of our permission settings. The permission flags
3404 work without this, but if the target knows the settings, it can do
3405 a couple things. First, it can add its own check, to catch cases
3406 that somehow manage to get by the permissions checks in target
3407 methods. Second, if the target is wired to disallow particular
3408 settings (for instance, a system in the field that is not set up to
3409 be able to stop at a breakpoint), it can object to any unavailable
3413 remote_set_permissions (void)
3415 struct remote_state
*rs
= get_remote_state ();
3417 sprintf (rs
->buf
, "QAllow:"
3418 "WriteReg:%x;WriteMem:%x;"
3419 "InsertBreak:%x;InsertTrace:%x;"
3420 "InsertFastTrace:%x;Stop:%x",
3421 may_write_registers
, may_write_memory
,
3422 may_insert_breakpoints
, may_insert_tracepoints
,
3423 may_insert_fast_tracepoints
, may_stop
);
3425 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3427 /* If the target didn't like the packet, warn the user. Do not try
3428 to undo the user's settings, that would just be maddening. */
3429 if (strcmp (rs
->buf
, "OK") != 0)
3430 warning ("Remote refused setting permissions with: %s", rs
->buf
);
3433 /* This type describes each known response to the qSupported
3435 struct protocol_feature
3437 /* The name of this protocol feature. */
3440 /* The default for this protocol feature. */
3441 enum packet_support default_support
;
3443 /* The function to call when this feature is reported, or after
3444 qSupported processing if the feature is not supported.
3445 The first argument points to this structure. The second
3446 argument indicates whether the packet requested support be
3447 enabled, disabled, or probed (or the default, if this function
3448 is being called at the end of processing and this feature was
3449 not reported). The third argument may be NULL; if not NULL, it
3450 is a NUL-terminated string taken from the packet following
3451 this feature's name and an equals sign. */
3452 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3455 /* The corresponding packet for this feature. Only used if
3456 FUNC is remote_supported_packet. */
3461 remote_supported_packet (const struct protocol_feature
*feature
,
3462 enum packet_support support
,
3463 const char *argument
)
3467 warning (_("Remote qSupported response supplied an unexpected value for"
3468 " \"%s\"."), feature
->name
);
3472 if (remote_protocol_packets
[feature
->packet
].support
3473 == PACKET_SUPPORT_UNKNOWN
)
3474 remote_protocol_packets
[feature
->packet
].support
= support
;
3478 remote_packet_size (const struct protocol_feature
*feature
,
3479 enum packet_support support
, const char *value
)
3481 struct remote_state
*rs
= get_remote_state ();
3486 if (support
!= PACKET_ENABLE
)
3489 if (value
== NULL
|| *value
== '\0')
3491 warning (_("Remote target reported \"%s\" without a size."),
3497 packet_size
= strtol (value
, &value_end
, 16);
3498 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3500 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3501 feature
->name
, value
);
3505 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3507 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3508 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3509 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3512 /* Record the new maximum packet size. */
3513 rs
->explicit_packet_size
= packet_size
;
3517 remote_multi_process_feature (const struct protocol_feature
*feature
,
3518 enum packet_support support
, const char *value
)
3520 struct remote_state
*rs
= get_remote_state ();
3522 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3526 remote_non_stop_feature (const struct protocol_feature
*feature
,
3527 enum packet_support support
, const char *value
)
3529 struct remote_state
*rs
= get_remote_state ();
3531 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3535 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3536 enum packet_support support
,
3539 struct remote_state
*rs
= get_remote_state ();
3541 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3545 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3546 enum packet_support support
,
3549 struct remote_state
*rs
= get_remote_state ();
3551 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3555 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3556 enum packet_support support
,
3559 struct remote_state
*rs
= get_remote_state ();
3561 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3564 static struct protocol_feature remote_protocol_features
[] = {
3565 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3566 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3567 PACKET_qXfer_auxv
},
3568 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3569 PACKET_qXfer_features
},
3570 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3571 PACKET_qXfer_libraries
},
3572 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3573 PACKET_qXfer_memory_map
},
3574 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3575 PACKET_qXfer_spu_read
},
3576 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3577 PACKET_qXfer_spu_write
},
3578 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3579 PACKET_qXfer_osdata
},
3580 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
3581 PACKET_qXfer_threads
},
3582 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
3583 PACKET_QPassSignals
},
3584 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
3585 PACKET_QStartNoAckMode
},
3586 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
3587 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
3588 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
3589 PACKET_qXfer_siginfo_read
},
3590 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
3591 PACKET_qXfer_siginfo_write
},
3592 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
3593 PACKET_ConditionalTracepoints
},
3594 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
3595 PACKET_FastTracepoints
},
3596 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
3598 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
3600 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
3602 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
3603 PACKET_TracepointSource
},
3604 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
3608 static char *remote_support_xml
;
3610 /* Register string appended to "xmlRegisters=" in qSupported query. */
3613 register_remote_support_xml (const char *xml
)
3615 #if defined(HAVE_LIBEXPAT)
3616 if (remote_support_xml
== NULL
)
3617 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
3620 char *copy
= xstrdup (remote_support_xml
+ 13);
3621 char *p
= strtok (copy
, ",");
3625 if (strcmp (p
, xml
) == 0)
3632 while ((p
= strtok (NULL
, ",")) != NULL
);
3635 remote_support_xml
= reconcat (remote_support_xml
,
3636 remote_support_xml
, ",", xml
,
3643 remote_query_supported_append (char *msg
, const char *append
)
3646 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
3648 return xstrdup (append
);
3652 remote_query_supported (void)
3654 struct remote_state
*rs
= get_remote_state ();
3657 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
3659 /* The packet support flags are handled differently for this packet
3660 than for most others. We treat an error, a disabled packet, and
3661 an empty response identically: any features which must be reported
3662 to be used will be automatically disabled. An empty buffer
3663 accomplishes this, since that is also the representation for a list
3664 containing no features. */
3667 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
3670 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
3673 q
= remote_query_supported_append (q
, "multiprocess+");
3675 if (remote_support_xml
)
3676 q
= remote_query_supported_append (q
, remote_support_xml
);
3678 q
= remote_query_supported_append (q
, "qRelocInsn+");
3680 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
3683 do_cleanups (old_chain
);
3685 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3687 /* If an error occured, warn, but do not return - just reset the
3688 buffer to empty and go on to disable features. */
3689 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3692 warning (_("Remote failure reply: %s"), rs
->buf
);
3697 memset (seen
, 0, sizeof (seen
));
3702 enum packet_support is_supported
;
3703 char *p
, *end
, *name_end
, *value
;
3705 /* First separate out this item from the rest of the packet. If
3706 there's another item after this, we overwrite the separator
3707 (terminated strings are much easier to work with). */
3709 end
= strchr (p
, ';');
3712 end
= p
+ strlen (p
);
3722 warning (_("empty item in \"qSupported\" response"));
3727 name_end
= strchr (p
, '=');
3730 /* This is a name=value entry. */
3731 is_supported
= PACKET_ENABLE
;
3732 value
= name_end
+ 1;
3741 is_supported
= PACKET_ENABLE
;
3745 is_supported
= PACKET_DISABLE
;
3749 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3753 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3759 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3760 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3762 const struct protocol_feature
*feature
;
3765 feature
= &remote_protocol_features
[i
];
3766 feature
->func (feature
, is_supported
, value
);
3771 /* If we increased the packet size, make sure to increase the global
3772 buffer size also. We delay this until after parsing the entire
3773 qSupported packet, because this is the same buffer we were
3775 if (rs
->buf_size
< rs
->explicit_packet_size
)
3777 rs
->buf_size
= rs
->explicit_packet_size
;
3778 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3781 /* Handle the defaults for unmentioned features. */
3782 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3785 const struct protocol_feature
*feature
;
3787 feature
= &remote_protocol_features
[i
];
3788 feature
->func (feature
, feature
->default_support
, NULL
);
3794 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3796 struct remote_state
*rs
= get_remote_state ();
3799 error (_("To open a remote debug connection, you need to specify what\n"
3800 "serial device is attached to the remote system\n"
3801 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3803 /* See FIXME above. */
3804 if (!target_async_permitted
)
3805 wait_forever_enabled_p
= 1;
3807 /* If we're connected to a running target, target_preopen will kill it.
3808 But if we're connected to a target system with no running process,
3809 then we will still be connected when it returns. Ask this question
3810 first, before target_preopen has a chance to kill anything. */
3811 if (remote_desc
!= NULL
&& !have_inferiors ())
3814 || query (_("Already connected to a remote target. Disconnect? ")))
3817 error (_("Still connected."));
3820 target_preopen (from_tty
);
3822 unpush_target (target
);
3824 /* This time without a query. If we were connected to an
3825 extended-remote target and target_preopen killed the running
3826 process, we may still be connected. If we are starting "target
3827 remote" now, the extended-remote target will not have been
3828 removed by unpush_target. */
3829 if (remote_desc
!= NULL
&& !have_inferiors ())
3832 /* Make sure we send the passed signals list the next time we resume. */
3833 xfree (last_pass_packet
);
3834 last_pass_packet
= NULL
;
3836 remote_fileio_reset ();
3837 reopen_exec_file ();
3840 remote_desc
= remote_serial_open (name
);
3842 perror_with_name (name
);
3844 if (baud_rate
!= -1)
3846 if (serial_setbaudrate (remote_desc
, baud_rate
))
3848 /* The requested speed could not be set. Error out to
3849 top level after closing remote_desc. Take care to
3850 set remote_desc to NULL to avoid closing remote_desc
3852 serial_close (remote_desc
);
3854 perror_with_name (name
);
3858 serial_raw (remote_desc
);
3860 /* If there is something sitting in the buffer we might take it as a
3861 response to a command, which would be bad. */
3862 serial_flush_input (remote_desc
);
3866 puts_filtered ("Remote debugging using ");
3867 puts_filtered (name
);
3868 puts_filtered ("\n");
3870 push_target (target
); /* Switch to using remote target now. */
3872 /* Register extra event sources in the event loop. */
3873 remote_async_inferior_event_token
3874 = create_async_event_handler (remote_async_inferior_event_handler
,
3876 remote_async_get_pending_events_token
3877 = create_async_event_handler (remote_async_get_pending_events_handler
,
3880 /* Reset the target state; these things will be queried either by
3881 remote_query_supported or as they are needed. */
3882 init_all_packet_configs ();
3883 rs
->cached_wait_status
= 0;
3884 rs
->explicit_packet_size
= 0;
3886 rs
->multi_process_aware
= 0;
3887 rs
->extended
= extended_p
;
3888 rs
->non_stop_aware
= 0;
3889 rs
->waiting_for_stop_reply
= 0;
3890 rs
->ctrlc_pending_p
= 0;
3892 general_thread
= not_sent_ptid
;
3893 continue_thread
= not_sent_ptid
;
3895 /* Probe for ability to use "ThreadInfo" query, as required. */
3896 use_threadinfo_query
= 1;
3897 use_threadextra_query
= 1;
3899 if (target_async_permitted
)
3901 /* With this target we start out by owning the terminal. */
3902 remote_async_terminal_ours_p
= 1;
3904 /* FIXME: cagney/1999-09-23: During the initial connection it is
3905 assumed that the target is already ready and able to respond to
3906 requests. Unfortunately remote_start_remote() eventually calls
3907 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
3908 around this. Eventually a mechanism that allows
3909 wait_for_inferior() to expect/get timeouts will be
3911 wait_forever_enabled_p
= 0;
3914 /* First delete any symbols previously loaded from shared libraries. */
3915 no_shared_libraries (NULL
, 0);
3918 init_thread_list ();
3920 /* Start the remote connection. If error() or QUIT, discard this
3921 target (we'd otherwise be in an inconsistent state) and then
3922 propogate the error on up the exception chain. This ensures that
3923 the caller doesn't stumble along blindly assuming that the
3924 function succeeded. The CLI doesn't have this problem but other
3925 UI's, such as MI do.
3927 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
3928 this function should return an error indication letting the
3929 caller restore the previous state. Unfortunately the command
3930 ``target remote'' is directly wired to this function making that
3931 impossible. On a positive note, the CLI side of this problem has
3932 been fixed - the function set_cmd_context() makes it possible for
3933 all the ``target ....'' commands to share a common callback
3934 function. See cli-dump.c. */
3936 struct gdb_exception ex
;
3937 struct start_remote_args args
;
3939 args
.from_tty
= from_tty
;
3940 args
.target
= target
;
3941 args
.extended_p
= extended_p
;
3943 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
3946 /* Pop the partially set up target - unless something else did
3947 already before throwing the exception. */
3948 if (remote_desc
!= NULL
)
3950 if (target_async_permitted
)
3951 wait_forever_enabled_p
= 1;
3952 throw_exception (ex
);
3956 if (target_async_permitted
)
3957 wait_forever_enabled_p
= 1;
3960 /* This takes a program previously attached to and detaches it. After
3961 this is done, GDB can be used to debug some other program. We
3962 better not have left any breakpoints in the target program or it'll
3963 die when it hits one. */
3966 remote_detach_1 (char *args
, int from_tty
, int extended
)
3968 int pid
= ptid_get_pid (inferior_ptid
);
3969 struct remote_state
*rs
= get_remote_state ();
3972 error (_("Argument given to \"detach\" when remotely debugging."));
3974 if (!target_has_execution
)
3975 error (_("No process to detach from."));
3977 /* Tell the remote target to detach. */
3978 if (remote_multi_process_p (rs
))
3979 sprintf (rs
->buf
, "D;%x", pid
);
3981 strcpy (rs
->buf
, "D");
3984 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3986 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
3988 else if (rs
->buf
[0] == '\0')
3989 error (_("Remote doesn't know how to detach"));
3991 error (_("Can't detach process."));
3995 if (remote_multi_process_p (rs
))
3996 printf_filtered (_("Detached from remote %s.\n"),
3997 target_pid_to_str (pid_to_ptid (pid
)));
4001 puts_filtered (_("Detached from remote process.\n"));
4003 puts_filtered (_("Ending remote debugging.\n"));
4007 discard_pending_stop_replies (pid
);
4008 target_mourn_inferior ();
4012 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4014 remote_detach_1 (args
, from_tty
, 0);
4018 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4020 remote_detach_1 (args
, from_tty
, 1);
4023 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4026 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4029 error (_("Argument given to \"disconnect\" when remotely debugging."));
4031 /* Make sure we unpush even the extended remote targets; mourn
4032 won't do it. So call remote_mourn_1 directly instead of
4033 target_mourn_inferior. */
4034 remote_mourn_1 (target
);
4037 puts_filtered ("Ending remote debugging.\n");
4040 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4041 be chatty about it. */
4044 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4046 struct remote_state
*rs
= get_remote_state ();
4048 char *wait_status
= NULL
;
4050 pid
= parse_pid_to_attach (args
);
4052 /* Remote PID can be freely equal to getpid, do not check it here the same
4053 way as in other targets. */
4055 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4056 error (_("This target does not support attaching to a process"));
4058 sprintf (rs
->buf
, "vAttach;%x", pid
);
4060 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4062 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4065 printf_unfiltered (_("Attached to %s\n"),
4066 target_pid_to_str (pid_to_ptid (pid
)));
4070 /* Save the reply for later. */
4071 wait_status
= alloca (strlen (rs
->buf
) + 1);
4072 strcpy (wait_status
, rs
->buf
);
4074 else if (strcmp (rs
->buf
, "OK") != 0)
4075 error (_("Attaching to %s failed with: %s"),
4076 target_pid_to_str (pid_to_ptid (pid
)),
4079 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4080 error (_("This target does not support attaching to a process"));
4082 error (_("Attaching to %s failed"),
4083 target_pid_to_str (pid_to_ptid (pid
)));
4085 set_current_inferior (remote_add_inferior (pid
, 1));
4087 inferior_ptid
= pid_to_ptid (pid
);
4091 struct thread_info
*thread
;
4093 /* Get list of threads. */
4094 remote_threads_info (target
);
4096 thread
= first_thread_of_process (pid
);
4098 inferior_ptid
= thread
->ptid
;
4100 inferior_ptid
= pid_to_ptid (pid
);
4102 /* Invalidate our notion of the remote current thread. */
4103 record_currthread (minus_one_ptid
);
4107 /* Now, if we have thread information, update inferior_ptid. */
4108 inferior_ptid
= remote_current_thread (inferior_ptid
);
4110 /* Add the main thread to the thread list. */
4111 add_thread_silent (inferior_ptid
);
4114 /* Next, if the target can specify a description, read it. We do
4115 this before anything involving memory or registers. */
4116 target_find_description ();
4120 /* Use the previously fetched status. */
4121 gdb_assert (wait_status
!= NULL
);
4123 if (target_can_async_p ())
4125 struct stop_reply
*stop_reply
;
4126 struct cleanup
*old_chain
;
4128 stop_reply
= stop_reply_xmalloc ();
4129 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4130 remote_parse_stop_reply (wait_status
, stop_reply
);
4131 discard_cleanups (old_chain
);
4132 push_stop_reply (stop_reply
);
4134 target_async (inferior_event_handler
, 0);
4138 gdb_assert (wait_status
!= NULL
);
4139 strcpy (rs
->buf
, wait_status
);
4140 rs
->cached_wait_status
= 1;
4144 gdb_assert (wait_status
== NULL
);
4148 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4150 extended_remote_attach_1 (ops
, args
, from_tty
);
4153 /* Convert hex digit A to a number. */
4158 if (a
>= '0' && a
<= '9')
4160 else if (a
>= 'a' && a
<= 'f')
4161 return a
- 'a' + 10;
4162 else if (a
>= 'A' && a
<= 'F')
4163 return a
- 'A' + 10;
4165 error (_("Reply contains invalid hex digit %d"), a
);
4169 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
4173 for (i
= 0; i
< count
; i
++)
4175 if (hex
[0] == 0 || hex
[1] == 0)
4177 /* Hex string is short, or of uneven length.
4178 Return the count that has been converted so far. */
4181 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
4187 /* Convert number NIB to a hex digit. */
4195 return 'a' + nib
- 10;
4199 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
4203 /* May use a length, or a nul-terminated string as input. */
4205 count
= strlen ((char *) bin
);
4207 for (i
= 0; i
< count
; i
++)
4209 *hex
++ = tohex ((*bin
>> 4) & 0xf);
4210 *hex
++ = tohex (*bin
++ & 0xf);
4216 /* Check for the availability of vCont. This function should also check
4220 remote_vcont_probe (struct remote_state
*rs
)
4224 strcpy (rs
->buf
, "vCont?");
4226 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4229 /* Make sure that the features we assume are supported. */
4230 if (strncmp (buf
, "vCont", 5) == 0)
4233 int support_s
, support_S
, support_c
, support_C
;
4239 rs
->support_vCont_t
= 0;
4240 while (p
&& *p
== ';')
4243 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4245 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4247 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4249 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4251 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4252 rs
->support_vCont_t
= 1;
4254 p
= strchr (p
, ';');
4257 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4258 BUF will make packet_ok disable the packet. */
4259 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4263 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4266 /* Helper function for building "vCont" resumptions. Write a
4267 resumption to P. ENDP points to one-passed-the-end of the buffer
4268 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4269 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4270 resumed thread should be single-stepped and/or signalled. If PTID
4271 equals minus_one_ptid, then all threads are resumed; if PTID
4272 represents a process, then all threads of the process are resumed;
4273 the thread to be stepped and/or signalled is given in the global
4277 append_resumption (char *p
, char *endp
,
4278 ptid_t ptid
, int step
, enum target_signal siggnal
)
4280 struct remote_state
*rs
= get_remote_state ();
4282 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
4283 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4285 p
+= xsnprintf (p
, endp
- p
, ";s");
4286 else if (siggnal
!= TARGET_SIGNAL_0
)
4287 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4289 p
+= xsnprintf (p
, endp
- p
, ";c");
4291 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4295 /* All (-1) threads of process. */
4296 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4298 p
+= xsnprintf (p
, endp
- p
, ":");
4299 p
= write_ptid (p
, endp
, nptid
);
4301 else if (!ptid_equal (ptid
, minus_one_ptid
))
4303 p
+= xsnprintf (p
, endp
- p
, ":");
4304 p
= write_ptid (p
, endp
, ptid
);
4310 /* Resume the remote inferior by using a "vCont" packet. The thread
4311 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4312 resumed thread should be single-stepped and/or signalled. If PTID
4313 equals minus_one_ptid, then all threads are resumed; the thread to
4314 be stepped and/or signalled is given in the global INFERIOR_PTID.
4315 This function returns non-zero iff it resumes the inferior.
4317 This function issues a strict subset of all possible vCont commands at the
4321 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
4323 struct remote_state
*rs
= get_remote_state ();
4327 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4328 remote_vcont_probe (rs
);
4330 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4334 endp
= rs
->buf
+ get_remote_packet_size ();
4336 /* If we could generate a wider range of packets, we'd have to worry
4337 about overflowing BUF. Should there be a generic
4338 "multi-part-packet" packet? */
4340 p
+= xsnprintf (p
, endp
- p
, "vCont");
4342 if (ptid_equal (ptid
, magic_null_ptid
))
4344 /* MAGIC_NULL_PTID means that we don't have any active threads,
4345 so we don't have any TID numbers the inferior will
4346 understand. Make sure to only send forms that do not specify
4348 p
= append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4350 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4352 /* Resume all threads (of all processes, or of a single
4353 process), with preference for INFERIOR_PTID. This assumes
4354 inferior_ptid belongs to the set of all threads we are about
4356 if (step
|| siggnal
!= TARGET_SIGNAL_0
)
4358 /* Step inferior_ptid, with or without signal. */
4359 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4362 /* And continue others without a signal. */
4363 p
= append_resumption (p
, endp
, ptid
, /*step=*/ 0, TARGET_SIGNAL_0
);
4367 /* Scheduler locking; resume only PTID. */
4368 p
= append_resumption (p
, endp
, ptid
, step
, siggnal
);
4371 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4376 /* In non-stop, the stub replies to vCont with "OK". The stop
4377 reply will be reported asynchronously by means of a `%Stop'
4379 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4380 if (strcmp (rs
->buf
, "OK") != 0)
4381 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4387 /* Tell the remote machine to resume. */
4389 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
4391 static int last_sent_step
;
4394 remote_resume (struct target_ops
*ops
,
4395 ptid_t ptid
, int step
, enum target_signal siggnal
)
4397 struct remote_state
*rs
= get_remote_state ();
4400 last_sent_signal
= siggnal
;
4401 last_sent_step
= step
;
4403 /* Update the inferior on signals to silently pass, if they've changed. */
4404 remote_pass_signals ();
4406 /* The vCont packet doesn't need to specify threads via Hc. */
4407 /* No reverse support (yet) for vCont. */
4408 if (execution_direction
!= EXEC_REVERSE
)
4409 if (remote_vcont_resume (ptid
, step
, siggnal
))
4412 /* All other supported resume packets do use Hc, so set the continue
4414 if (ptid_equal (ptid
, minus_one_ptid
))
4415 set_continue_thread (any_thread_ptid
);
4417 set_continue_thread (ptid
);
4420 if (execution_direction
== EXEC_REVERSE
)
4422 /* We don't pass signals to the target in reverse exec mode. */
4423 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
4424 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
4428 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4429 error (_("Remote reverse-step not supported."));
4431 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4432 error (_("Remote reverse-continue not supported."));
4434 strcpy (buf
, step
? "bs" : "bc");
4436 else if (siggnal
!= TARGET_SIGNAL_0
)
4438 buf
[0] = step
? 'S' : 'C';
4439 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4440 buf
[2] = tohex (((int) siggnal
) & 0xf);
4444 strcpy (buf
, step
? "s" : "c");
4449 /* We are about to start executing the inferior, let's register it
4450 with the event loop. NOTE: this is the one place where all the
4451 execution commands end up. We could alternatively do this in each
4452 of the execution commands in infcmd.c. */
4453 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4454 into infcmd.c in order to allow inferior function calls to work
4455 NOT asynchronously. */
4456 if (target_can_async_p ())
4457 target_async (inferior_event_handler
, 0);
4459 /* We've just told the target to resume. The remote server will
4460 wait for the inferior to stop, and then send a stop reply. In
4461 the mean time, we can't start another command/query ourselves
4462 because the stub wouldn't be ready to process it. This applies
4463 only to the base all-stop protocol, however. In non-stop (which
4464 only supports vCont), the stub replies with an "OK", and is
4465 immediate able to process further serial input. */
4467 rs
->waiting_for_stop_reply
= 1;
4471 /* Set up the signal handler for SIGINT, while the target is
4472 executing, ovewriting the 'regular' SIGINT signal handler. */
4474 initialize_sigint_signal_handler (void)
4476 signal (SIGINT
, handle_remote_sigint
);
4479 /* Signal handler for SIGINT, while the target is executing. */
4481 handle_remote_sigint (int sig
)
4483 signal (sig
, handle_remote_sigint_twice
);
4484 mark_async_signal_handler_wrapper (sigint_remote_token
);
4487 /* Signal handler for SIGINT, installed after SIGINT has already been
4488 sent once. It will take effect the second time that the user sends
4491 handle_remote_sigint_twice (int sig
)
4493 signal (sig
, handle_remote_sigint
);
4494 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
4497 /* Perform the real interruption of the target execution, in response
4500 async_remote_interrupt (gdb_client_data arg
)
4503 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
4505 target_stop (inferior_ptid
);
4508 /* Perform interrupt, if the first attempt did not succeed. Just give
4509 up on the target alltogether. */
4511 async_remote_interrupt_twice (gdb_client_data arg
)
4514 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
4519 /* Reinstall the usual SIGINT handlers, after the target has
4522 cleanup_sigint_signal_handler (void *dummy
)
4524 signal (SIGINT
, handle_sigint
);
4527 /* Send ^C to target to halt it. Target will respond, and send us a
4529 static void (*ofunc
) (int);
4531 /* The command line interface's stop routine. This function is installed
4532 as a signal handler for SIGINT. The first time a user requests a
4533 stop, we call remote_stop to send a break or ^C. If there is no
4534 response from the target (it didn't stop when the user requested it),
4535 we ask the user if he'd like to detach from the target. */
4537 remote_interrupt (int signo
)
4539 /* If this doesn't work, try more severe steps. */
4540 signal (signo
, remote_interrupt_twice
);
4542 gdb_call_async_signal_handler (sigint_remote_token
, 1);
4545 /* The user typed ^C twice. */
4548 remote_interrupt_twice (int signo
)
4550 signal (signo
, ofunc
);
4551 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
4552 signal (signo
, remote_interrupt
);
4555 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4556 thread, all threads of a remote process, or all threads of all
4560 remote_stop_ns (ptid_t ptid
)
4562 struct remote_state
*rs
= get_remote_state ();
4564 char *endp
= rs
->buf
+ get_remote_packet_size ();
4566 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4567 remote_vcont_probe (rs
);
4569 if (!rs
->support_vCont_t
)
4570 error (_("Remote server does not support stopping threads"));
4572 if (ptid_equal (ptid
, minus_one_ptid
)
4573 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4574 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
4579 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
4581 if (ptid_is_pid (ptid
))
4582 /* All (-1) threads of process. */
4583 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4586 /* Small optimization: if we already have a stop reply for
4587 this thread, no use in telling the stub we want this
4589 if (peek_stop_reply (ptid
))
4595 p
= write_ptid (p
, endp
, nptid
);
4598 /* In non-stop, we get an immediate OK reply. The stop reply will
4599 come in asynchronously by notification. */
4601 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4602 if (strcmp (rs
->buf
, "OK") != 0)
4603 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
4606 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4607 remote target. It is undefined which thread of which process
4608 reports the stop. */
4611 remote_stop_as (ptid_t ptid
)
4613 struct remote_state
*rs
= get_remote_state ();
4615 rs
->ctrlc_pending_p
= 1;
4617 /* If the inferior is stopped already, but the core didn't know
4618 about it yet, just ignore the request. The cached wait status
4619 will be collected in remote_wait. */
4620 if (rs
->cached_wait_status
)
4623 /* Send interrupt_sequence to remote target. */
4624 send_interrupt_sequence ();
4627 /* This is the generic stop called via the target vector. When a target
4628 interrupt is requested, either by the command line or the GUI, we
4629 will eventually end up here. */
4632 remote_stop (ptid_t ptid
)
4635 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
4638 remote_stop_ns (ptid
);
4640 remote_stop_as (ptid
);
4643 /* Ask the user what to do when an interrupt is received. */
4646 interrupt_query (void)
4648 target_terminal_ours ();
4650 if (target_can_async_p ())
4652 signal (SIGINT
, handle_sigint
);
4653 deprecated_throw_reason (RETURN_QUIT
);
4657 if (query (_("Interrupted while waiting for the program.\n\
4658 Give up (and stop debugging it)? ")))
4661 deprecated_throw_reason (RETURN_QUIT
);
4665 target_terminal_inferior ();
4668 /* Enable/disable target terminal ownership. Most targets can use
4669 terminal groups to control terminal ownership. Remote targets are
4670 different in that explicit transfer of ownership to/from GDB/target
4674 remote_terminal_inferior (void)
4676 if (!target_async_permitted
)
4677 /* Nothing to do. */
4680 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4681 idempotent. The event-loop GDB talking to an asynchronous target
4682 with a synchronous command calls this function from both
4683 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
4684 transfer the terminal to the target when it shouldn't this guard
4686 if (!remote_async_terminal_ours_p
)
4688 delete_file_handler (input_fd
);
4689 remote_async_terminal_ours_p
= 0;
4690 initialize_sigint_signal_handler ();
4691 /* NOTE: At this point we could also register our selves as the
4692 recipient of all input. Any characters typed could then be
4693 passed on down to the target. */
4697 remote_terminal_ours (void)
4699 if (!target_async_permitted
)
4700 /* Nothing to do. */
4703 /* See FIXME in remote_terminal_inferior. */
4704 if (remote_async_terminal_ours_p
)
4706 cleanup_sigint_signal_handler (NULL
);
4707 add_file_handler (input_fd
, stdin_event_handler
, 0);
4708 remote_async_terminal_ours_p
= 1;
4712 remote_console_output (char *msg
)
4716 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4719 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4723 fputs_unfiltered (tb
, gdb_stdtarg
);
4725 gdb_flush (gdb_stdtarg
);
4728 typedef struct cached_reg
4731 gdb_byte data
[MAX_REGISTER_SIZE
];
4734 DEF_VEC_O(cached_reg_t
);
4738 struct stop_reply
*next
;
4742 struct target_waitstatus ws
;
4744 VEC(cached_reg_t
) *regcache
;
4746 int stopped_by_watchpoint_p
;
4747 CORE_ADDR watch_data_address
;
4755 /* The list of already fetched and acknowledged stop events. */
4756 static struct stop_reply
*stop_reply_queue
;
4758 static struct stop_reply
*
4759 stop_reply_xmalloc (void)
4761 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4768 stop_reply_xfree (struct stop_reply
*r
)
4772 VEC_free (cached_reg_t
, r
->regcache
);
4777 /* Discard all pending stop replies of inferior PID. If PID is -1,
4778 discard everything. */
4781 discard_pending_stop_replies (int pid
)
4783 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4785 /* Discard the in-flight notification. */
4786 if (pending_stop_reply
!= NULL
4788 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4790 stop_reply_xfree (pending_stop_reply
);
4791 pending_stop_reply
= NULL
;
4794 /* Discard the stop replies we have already pulled with
4796 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4800 || ptid_get_pid (reply
->ptid
) == pid
)
4802 if (reply
== stop_reply_queue
)
4803 stop_reply_queue
= reply
->next
;
4805 prev
->next
= reply
->next
;
4807 stop_reply_xfree (reply
);
4814 /* Cleanup wrapper. */
4817 do_stop_reply_xfree (void *arg
)
4819 struct stop_reply
*r
= arg
;
4821 stop_reply_xfree (r
);
4824 /* Look for a queued stop reply belonging to PTID. If one is found,
4825 remove it from the queue, and return it. Returns NULL if none is
4826 found. If there are still queued events left to process, tell the
4827 event loop to get back to target_wait soon. */
4829 static struct stop_reply
*
4830 queued_stop_reply (ptid_t ptid
)
4832 struct stop_reply
*it
;
4833 struct stop_reply
**it_link
;
4835 it
= stop_reply_queue
;
4836 it_link
= &stop_reply_queue
;
4839 if (ptid_match (it
->ptid
, ptid
))
4841 *it_link
= it
->next
;
4846 it_link
= &it
->next
;
4850 if (stop_reply_queue
)
4851 /* There's still at least an event left. */
4852 mark_async_event_handler (remote_async_inferior_event_token
);
4857 /* Push a fully parsed stop reply in the stop reply queue. Since we
4858 know that we now have at least one queued event left to pass to the
4859 core side, tell the event loop to get back to target_wait soon. */
4862 push_stop_reply (struct stop_reply
*new_event
)
4864 struct stop_reply
*event
;
4866 if (stop_reply_queue
)
4868 for (event
= stop_reply_queue
;
4869 event
&& event
->next
;
4870 event
= event
->next
)
4873 event
->next
= new_event
;
4876 stop_reply_queue
= new_event
;
4878 mark_async_event_handler (remote_async_inferior_event_token
);
4881 /* Returns true if we have a stop reply for PTID. */
4884 peek_stop_reply (ptid_t ptid
)
4886 struct stop_reply
*it
;
4888 for (it
= stop_reply_queue
; it
; it
= it
->next
)
4889 if (ptid_equal (ptid
, it
->ptid
))
4891 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
4898 /* Parse the stop reply in BUF. Either the function succeeds, and the
4899 result is stored in EVENT, or throws an error. */
4902 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
4904 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4908 event
->ptid
= null_ptid
;
4909 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
4910 event
->ws
.value
.integer
= 0;
4911 event
->solibs_changed
= 0;
4912 event
->replay_event
= 0;
4913 event
->stopped_by_watchpoint_p
= 0;
4914 event
->regcache
= NULL
;
4919 case 'T': /* Status with PC, SP, FP, ... */
4920 /* Expedited reply, containing Signal, {regno, reg} repeat. */
4921 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
4923 n... = register number
4924 r... = register contents
4927 p
= &buf
[3]; /* after Txx */
4935 /* If the packet contains a register number, save it in
4936 pnum and set p1 to point to the character following it.
4937 Otherwise p1 points to p. */
4939 /* If this packet is an awatch packet, don't parse the 'a'
4940 as a register number. */
4942 if (strncmp (p
, "awatch", strlen("awatch")) != 0
4943 && strncmp (p
, "core", strlen ("core") != 0))
4945 /* Read the ``P'' register number. */
4946 pnum
= strtol (p
, &p_temp
, 16);
4952 if (p1
== p
) /* No register number present here. */
4954 p1
= strchr (p
, ':');
4956 error (_("Malformed packet(a) (missing colon): %s\n\
4959 if (strncmp (p
, "thread", p1
- p
) == 0)
4960 event
->ptid
= read_ptid (++p1
, &p
);
4961 else if ((strncmp (p
, "watch", p1
- p
) == 0)
4962 || (strncmp (p
, "rwatch", p1
- p
) == 0)
4963 || (strncmp (p
, "awatch", p1
- p
) == 0))
4965 event
->stopped_by_watchpoint_p
= 1;
4966 p
= unpack_varlen_hex (++p1
, &addr
);
4967 event
->watch_data_address
= (CORE_ADDR
) addr
;
4969 else if (strncmp (p
, "library", p1
- p
) == 0)
4973 while (*p_temp
&& *p_temp
!= ';')
4976 event
->solibs_changed
= 1;
4979 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
4981 /* NO_HISTORY event.
4982 p1 will indicate "begin" or "end", but
4983 it makes no difference for now, so ignore it. */
4984 event
->replay_event
= 1;
4985 p_temp
= strchr (p1
+ 1, ';');
4989 else if (strncmp (p
, "core", p1
- p
) == 0)
4993 p
= unpack_varlen_hex (++p1
, &c
);
4998 /* Silently skip unknown optional info. */
4999 p_temp
= strchr (p1
+ 1, ';');
5006 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5007 cached_reg_t cached_reg
;
5012 error (_("Malformed packet(b) (missing colon): %s\n\
5018 error (_("Remote sent bad register number %s: %s\n\
5020 hex_string (pnum
), p
, buf
);
5022 cached_reg
.num
= reg
->regnum
;
5024 fieldsize
= hex2bin (p
, cached_reg
.data
,
5025 register_size (target_gdbarch
,
5028 if (fieldsize
< register_size (target_gdbarch
,
5030 warning (_("Remote reply is too short: %s"), buf
);
5032 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5036 error (_("Remote register badly formatted: %s\nhere: %s"),
5041 case 'S': /* Old style status, just signal only. */
5042 if (event
->solibs_changed
)
5043 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5044 else if (event
->replay_event
)
5045 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5048 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5049 event
->ws
.value
.sig
= (enum target_signal
)
5050 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
5053 case 'W': /* Target exited. */
5060 /* GDB used to accept only 2 hex chars here. Stubs should
5061 only send more if they detect GDB supports multi-process
5063 p
= unpack_varlen_hex (&buf
[1], &value
);
5067 /* The remote process exited. */
5068 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5069 event
->ws
.value
.integer
= value
;
5073 /* The remote process exited with a signal. */
5074 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5075 event
->ws
.value
.sig
= (enum target_signal
) value
;
5078 /* If no process is specified, assume inferior_ptid. */
5079 pid
= ptid_get_pid (inferior_ptid
);
5088 else if (strncmp (p
,
5089 "process:", sizeof ("process:") - 1) == 0)
5093 p
+= sizeof ("process:") - 1;
5094 unpack_varlen_hex (p
, &upid
);
5098 error (_("unknown stop reply packet: %s"), buf
);
5101 error (_("unknown stop reply packet: %s"), buf
);
5102 event
->ptid
= pid_to_ptid (pid
);
5107 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5108 error (_("No process or thread specified in stop reply: %s"), buf
);
5111 /* When the stub wants to tell GDB about a new stop reply, it sends a
5112 stop notification (%Stop). Those can come it at any time, hence,
5113 we have to make sure that any pending putpkt/getpkt sequence we're
5114 making is finished, before querying the stub for more events with
5115 vStopped. E.g., if we started a vStopped sequence immediatelly
5116 upon receiving the %Stop notification, something like this could
5124 1.6) <-- (registers reply to step #1.3)
5126 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5129 To solve this, whenever we parse a %Stop notification sucessfully,
5130 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5131 doing whatever we were doing:
5137 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5138 2.5) <-- (registers reply to step #2.3)
5140 Eventualy after step #2.5, we return to the event loop, which
5141 notices there's an event on the
5142 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5143 associated callback --- the function below. At this point, we're
5144 always safe to start a vStopped sequence. :
5147 2.7) <-- T05 thread:2
5153 remote_get_pending_stop_replies (void)
5155 struct remote_state
*rs
= get_remote_state ();
5157 if (pending_stop_reply
)
5160 putpkt ("vStopped");
5162 /* Now we can rely on it. */
5163 push_stop_reply (pending_stop_reply
);
5164 pending_stop_reply
= NULL
;
5168 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5169 if (strcmp (rs
->buf
, "OK") == 0)
5173 struct cleanup
*old_chain
;
5174 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
5176 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5177 remote_parse_stop_reply (rs
->buf
, stop_reply
);
5180 putpkt ("vStopped");
5182 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5184 /* Now we can rely on it. */
5185 discard_cleanups (old_chain
);
5186 push_stop_reply (stop_reply
);
5189 /* We got an unknown stop reply. */
5190 do_cleanups (old_chain
);
5197 /* Called when it is decided that STOP_REPLY holds the info of the
5198 event that is to be returned to the core. This function always
5199 destroys STOP_REPLY. */
5202 process_stop_reply (struct stop_reply
*stop_reply
,
5203 struct target_waitstatus
*status
)
5207 *status
= stop_reply
->ws
;
5208 ptid
= stop_reply
->ptid
;
5210 /* If no thread/process was reported by the stub, assume the current
5212 if (ptid_equal (ptid
, null_ptid
))
5213 ptid
= inferior_ptid
;
5215 if (status
->kind
!= TARGET_WAITKIND_EXITED
5216 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5218 /* Expedited registers. */
5219 if (stop_reply
->regcache
)
5221 struct regcache
*regcache
5222 = get_thread_arch_regcache (ptid
, target_gdbarch
);
5227 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5229 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5230 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5233 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5234 remote_watch_data_address
= stop_reply
->watch_data_address
;
5236 remote_notice_new_inferior (ptid
, 0);
5237 demand_private_info (ptid
)->core
= stop_reply
->core
;
5240 stop_reply_xfree (stop_reply
);
5244 /* The non-stop mode version of target_wait. */
5247 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5249 struct remote_state
*rs
= get_remote_state ();
5250 struct stop_reply
*stop_reply
;
5253 /* If in non-stop mode, get out of getpkt even if a
5254 notification is received. */
5256 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5263 case 'E': /* Error of some sort. */
5264 /* We're out of sync with the target now. Did it continue
5265 or not? We can't tell which thread it was in non-stop,
5266 so just ignore this. */
5267 warning (_("Remote failure reply: %s"), rs
->buf
);
5269 case 'O': /* Console output. */
5270 remote_console_output (rs
->buf
+ 1);
5273 warning (_("Invalid remote reply: %s"), rs
->buf
);
5277 /* Acknowledge a pending stop reply that may have arrived in the
5279 if (pending_stop_reply
!= NULL
)
5280 remote_get_pending_stop_replies ();
5282 /* If indeed we noticed a stop reply, we're done. */
5283 stop_reply
= queued_stop_reply (ptid
);
5284 if (stop_reply
!= NULL
)
5285 return process_stop_reply (stop_reply
, status
);
5287 /* Still no event. If we're just polling for an event, then
5288 return to the event loop. */
5289 if (options
& TARGET_WNOHANG
)
5291 status
->kind
= TARGET_WAITKIND_IGNORE
;
5292 return minus_one_ptid
;
5295 /* Otherwise do a blocking wait. */
5296 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5301 /* Wait until the remote machine stops, then return, storing status in
5302 STATUS just as `wait' would. */
5305 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5307 struct remote_state
*rs
= get_remote_state ();
5308 ptid_t event_ptid
= null_ptid
;
5310 struct stop_reply
*stop_reply
;
5314 status
->kind
= TARGET_WAITKIND_IGNORE
;
5315 status
->value
.integer
= 0;
5317 stop_reply
= queued_stop_reply (ptid
);
5318 if (stop_reply
!= NULL
)
5319 return process_stop_reply (stop_reply
, status
);
5321 if (rs
->cached_wait_status
)
5322 /* Use the cached wait status, but only once. */
5323 rs
->cached_wait_status
= 0;
5328 if (!target_is_async_p ())
5330 ofunc
= signal (SIGINT
, remote_interrupt
);
5331 /* If the user hit C-c before this packet, or between packets,
5332 pretend that it was hit right here. */
5336 remote_interrupt (SIGINT
);
5340 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5341 _never_ wait for ever -> test on target_is_async_p().
5342 However, before we do that we need to ensure that the caller
5343 knows how to take the target into/out of async mode. */
5344 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
5345 if (!target_is_async_p ())
5346 signal (SIGINT
, ofunc
);
5351 remote_stopped_by_watchpoint_p
= 0;
5353 /* We got something. */
5354 rs
->waiting_for_stop_reply
= 0;
5356 /* Assume that the target has acknowledged Ctrl-C unless we receive
5357 an 'F' or 'O' packet. */
5358 if (buf
[0] != 'F' && buf
[0] != 'O')
5359 rs
->ctrlc_pending_p
= 0;
5363 case 'E': /* Error of some sort. */
5364 /* We're out of sync with the target now. Did it continue or
5365 not? Not is more likely, so report a stop. */
5366 warning (_("Remote failure reply: %s"), buf
);
5367 status
->kind
= TARGET_WAITKIND_STOPPED
;
5368 status
->value
.sig
= TARGET_SIGNAL_0
;
5370 case 'F': /* File-I/O request. */
5371 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5372 rs
->ctrlc_pending_p
= 0;
5374 case 'T': case 'S': case 'X': case 'W':
5376 struct stop_reply
*stop_reply
;
5377 struct cleanup
*old_chain
;
5379 stop_reply
= stop_reply_xmalloc ();
5380 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5381 remote_parse_stop_reply (buf
, stop_reply
);
5382 discard_cleanups (old_chain
);
5383 event_ptid
= process_stop_reply (stop_reply
, status
);
5386 case 'O': /* Console output. */
5387 remote_console_output (buf
+ 1);
5389 /* The target didn't really stop; keep waiting. */
5390 rs
->waiting_for_stop_reply
= 1;
5394 if (last_sent_signal
!= TARGET_SIGNAL_0
)
5396 /* Zero length reply means that we tried 'S' or 'C' and the
5397 remote system doesn't support it. */
5398 target_terminal_ours_for_output ();
5400 ("Can't send signals to this remote system. %s not sent.\n",
5401 target_signal_to_name (last_sent_signal
));
5402 last_sent_signal
= TARGET_SIGNAL_0
;
5403 target_terminal_inferior ();
5405 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
5406 putpkt ((char *) buf
);
5408 /* We just told the target to resume, so a stop reply is in
5410 rs
->waiting_for_stop_reply
= 1;
5413 /* else fallthrough */
5415 warning (_("Invalid remote reply: %s"), buf
);
5417 rs
->waiting_for_stop_reply
= 1;
5421 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
5423 /* Nothing interesting happened. If we're doing a non-blocking
5424 poll, we're done. Otherwise, go back to waiting. */
5425 if (options
& TARGET_WNOHANG
)
5426 return minus_one_ptid
;
5430 else if (status
->kind
!= TARGET_WAITKIND_EXITED
5431 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5433 if (!ptid_equal (event_ptid
, null_ptid
))
5434 record_currthread (event_ptid
);
5436 event_ptid
= inferior_ptid
;
5439 /* A process exit. Invalidate our notion of current thread. */
5440 record_currthread (minus_one_ptid
);
5445 /* Wait until the remote machine stops, then return, storing status in
5446 STATUS just as `wait' would. */
5449 remote_wait (struct target_ops
*ops
,
5450 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5455 event_ptid
= remote_wait_ns (ptid
, status
, options
);
5457 event_ptid
= remote_wait_as (ptid
, status
, options
);
5459 if (target_can_async_p ())
5461 /* If there are are events left in the queue tell the event loop
5463 if (stop_reply_queue
)
5464 mark_async_event_handler (remote_async_inferior_event_token
);
5470 /* Fetch a single register using a 'p' packet. */
5473 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
5475 struct remote_state
*rs
= get_remote_state ();
5477 char regp
[MAX_REGISTER_SIZE
];
5480 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
5483 if (reg
->pnum
== -1)
5488 p
+= hexnumstr (p
, reg
->pnum
);
5491 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5495 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
5499 case PACKET_UNKNOWN
:
5502 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5503 gdbarch_register_name (get_regcache_arch (regcache
),
5508 /* If this register is unfetchable, tell the regcache. */
5511 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5515 /* Otherwise, parse and supply the value. */
5521 error (_("fetch_register_using_p: early buf termination"));
5523 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5526 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
5530 /* Fetch the registers included in the target's 'g' packet. */
5533 send_g_packet (void)
5535 struct remote_state
*rs
= get_remote_state ();
5538 sprintf (rs
->buf
, "g");
5539 remote_send (&rs
->buf
, &rs
->buf_size
);
5541 /* We can get out of synch in various cases. If the first character
5542 in the buffer is not a hex character, assume that has happened
5543 and try to fetch another packet to read. */
5544 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
5545 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
5546 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
5547 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
5550 fprintf_unfiltered (gdb_stdlog
,
5551 "Bad register packet; fetching a new packet\n");
5552 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5555 buf_len
= strlen (rs
->buf
);
5557 /* Sanity check the received packet. */
5558 if (buf_len
% 2 != 0)
5559 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
5565 process_g_packet (struct regcache
*regcache
)
5567 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5568 struct remote_state
*rs
= get_remote_state ();
5569 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5574 buf_len
= strlen (rs
->buf
);
5576 /* Further sanity checks, with knowledge of the architecture. */
5577 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
5578 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
5580 /* Save the size of the packet sent to us by the target. It is used
5581 as a heuristic when determining the max size of packets that the
5582 target can safely receive. */
5583 if (rsa
->actual_register_packet_size
== 0)
5584 rsa
->actual_register_packet_size
= buf_len
;
5586 /* If this is smaller than we guessed the 'g' packet would be,
5587 update our records. A 'g' reply that doesn't include a register's
5588 value implies either that the register is not available, or that
5589 the 'p' packet must be used. */
5590 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
5592 rsa
->sizeof_g_packet
= buf_len
/ 2;
5594 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5596 if (rsa
->regs
[i
].pnum
== -1)
5599 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
5600 rsa
->regs
[i
].in_g_packet
= 0;
5602 rsa
->regs
[i
].in_g_packet
= 1;
5606 regs
= alloca (rsa
->sizeof_g_packet
);
5608 /* Unimplemented registers read as all bits zero. */
5609 memset (regs
, 0, rsa
->sizeof_g_packet
);
5611 /* Reply describes registers byte by byte, each byte encoded as two
5612 hex characters. Suck them all up, then supply them to the
5613 register cacheing/storage mechanism. */
5616 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
5618 if (p
[0] == 0 || p
[1] == 0)
5619 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5620 internal_error (__FILE__
, __LINE__
,
5621 "unexpected end of 'g' packet reply");
5623 if (p
[0] == 'x' && p
[1] == 'x')
5624 regs
[i
] = 0; /* 'x' */
5626 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5630 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5632 struct packet_reg
*r
= &rsa
->regs
[i
];
5636 if (r
->offset
* 2 >= strlen (rs
->buf
))
5637 /* This shouldn't happen - we adjusted in_g_packet above. */
5638 internal_error (__FILE__
, __LINE__
,
5639 "unexpected end of 'g' packet reply");
5640 else if (rs
->buf
[r
->offset
* 2] == 'x')
5642 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
5643 /* The register isn't available, mark it as such (at
5644 the same time setting the value to zero). */
5645 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
5648 regcache_raw_supply (regcache
, r
->regnum
,
5655 fetch_registers_using_g (struct regcache
*regcache
)
5658 process_g_packet (regcache
);
5662 remote_fetch_registers (struct target_ops
*ops
,
5663 struct regcache
*regcache
, int regnum
)
5665 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5668 set_general_thread (inferior_ptid
);
5672 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5674 gdb_assert (reg
!= NULL
);
5676 /* If this register might be in the 'g' packet, try that first -
5677 we are likely to read more than one register. If this is the
5678 first 'g' packet, we might be overly optimistic about its
5679 contents, so fall back to 'p'. */
5680 if (reg
->in_g_packet
)
5682 fetch_registers_using_g (regcache
);
5683 if (reg
->in_g_packet
)
5687 if (fetch_register_using_p (regcache
, reg
))
5690 /* This register is not available. */
5691 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5696 fetch_registers_using_g (regcache
);
5698 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5699 if (!rsa
->regs
[i
].in_g_packet
)
5700 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5702 /* This register is not available. */
5703 regcache_raw_supply (regcache
, i
, NULL
);
5707 /* Prepare to store registers. Since we may send them all (using a
5708 'G' request), we have to read out the ones we don't want to change
5712 remote_prepare_to_store (struct regcache
*regcache
)
5714 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5716 gdb_byte buf
[MAX_REGISTER_SIZE
];
5718 /* Make sure the entire registers array is valid. */
5719 switch (remote_protocol_packets
[PACKET_P
].support
)
5721 case PACKET_DISABLE
:
5722 case PACKET_SUPPORT_UNKNOWN
:
5723 /* Make sure all the necessary registers are cached. */
5724 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5725 if (rsa
->regs
[i
].in_g_packet
)
5726 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5733 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5734 packet was not recognized. */
5737 store_register_using_P (const struct regcache
*regcache
,
5738 struct packet_reg
*reg
)
5740 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5741 struct remote_state
*rs
= get_remote_state ();
5742 /* Try storing a single register. */
5743 char *buf
= rs
->buf
;
5744 gdb_byte regp
[MAX_REGISTER_SIZE
];
5747 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5750 if (reg
->pnum
== -1)
5753 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5754 p
= buf
+ strlen (buf
);
5755 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5756 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5758 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5760 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5765 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
5766 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
5767 case PACKET_UNKNOWN
:
5770 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5774 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5775 contents of the register cache buffer. FIXME: ignores errors. */
5778 store_registers_using_G (const struct regcache
*regcache
)
5780 struct remote_state
*rs
= get_remote_state ();
5781 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5785 /* Extract all the registers in the regcache copying them into a
5790 regs
= alloca (rsa
->sizeof_g_packet
);
5791 memset (regs
, 0, rsa
->sizeof_g_packet
);
5792 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5794 struct packet_reg
*r
= &rsa
->regs
[i
];
5797 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5801 /* Command describes registers byte by byte,
5802 each byte encoded as two hex characters. */
5805 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5807 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5809 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5810 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
5811 error (_("Could not write registers; remote failure reply '%s'"),
5815 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5816 of the register cache buffer. FIXME: ignores errors. */
5819 remote_store_registers (struct target_ops
*ops
,
5820 struct regcache
*regcache
, int regnum
)
5822 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5825 set_general_thread (inferior_ptid
);
5829 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5831 gdb_assert (reg
!= NULL
);
5833 /* Always prefer to store registers using the 'P' packet if
5834 possible; we often change only a small number of registers.
5835 Sometimes we change a larger number; we'd need help from a
5836 higher layer to know to use 'G'. */
5837 if (store_register_using_P (regcache
, reg
))
5840 /* For now, don't complain if we have no way to write the
5841 register. GDB loses track of unavailable registers too
5842 easily. Some day, this may be an error. We don't have
5843 any way to read the register, either... */
5844 if (!reg
->in_g_packet
)
5847 store_registers_using_G (regcache
);
5851 store_registers_using_G (regcache
);
5853 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5854 if (!rsa
->regs
[i
].in_g_packet
)
5855 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5856 /* See above for why we do not issue an error here. */
5861 /* Return the number of hex digits in num. */
5864 hexnumlen (ULONGEST num
)
5868 for (i
= 0; num
!= 0; i
++)
5874 /* Set BUF to the minimum number of hex digits representing NUM. */
5877 hexnumstr (char *buf
, ULONGEST num
)
5879 int len
= hexnumlen (num
);
5881 return hexnumnstr (buf
, num
, len
);
5885 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
5888 hexnumnstr (char *buf
, ULONGEST num
, int width
)
5894 for (i
= width
- 1; i
>= 0; i
--)
5896 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
5903 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
5906 remote_address_masked (CORE_ADDR addr
)
5908 int address_size
= remote_address_size
;
5910 /* If "remoteaddresssize" was not set, default to target address size. */
5912 address_size
= gdbarch_addr_bit (target_gdbarch
);
5914 if (address_size
> 0
5915 && address_size
< (sizeof (ULONGEST
) * 8))
5917 /* Only create a mask when that mask can safely be constructed
5918 in a ULONGEST variable. */
5921 mask
= (mask
<< address_size
) - 1;
5927 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
5928 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
5929 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
5930 (which may be more than *OUT_LEN due to escape characters). The
5931 total number of bytes in the output buffer will be at most
5935 remote_escape_output (const gdb_byte
*buffer
, int len
,
5936 gdb_byte
*out_buf
, int *out_len
,
5939 int input_index
, output_index
;
5942 for (input_index
= 0; input_index
< len
; input_index
++)
5944 gdb_byte b
= buffer
[input_index
];
5946 if (b
== '$' || b
== '#' || b
== '}')
5948 /* These must be escaped. */
5949 if (output_index
+ 2 > out_maxlen
)
5951 out_buf
[output_index
++] = '}';
5952 out_buf
[output_index
++] = b
^ 0x20;
5956 if (output_index
+ 1 > out_maxlen
)
5958 out_buf
[output_index
++] = b
;
5962 *out_len
= input_index
;
5963 return output_index
;
5966 /* Convert BUFFER, escaped data LEN bytes long, into binary data
5967 in OUT_BUF. Return the number of bytes written to OUT_BUF.
5968 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
5970 This function reverses remote_escape_output. It allows more
5971 escaped characters than that function does, in particular because
5972 '*' must be escaped to avoid the run-length encoding processing
5973 in reading packets. */
5976 remote_unescape_input (const gdb_byte
*buffer
, int len
,
5977 gdb_byte
*out_buf
, int out_maxlen
)
5979 int input_index
, output_index
;
5984 for (input_index
= 0; input_index
< len
; input_index
++)
5986 gdb_byte b
= buffer
[input_index
];
5988 if (output_index
+ 1 > out_maxlen
)
5990 warning (_("Received too much data from remote target;"
5991 " ignoring overflow."));
5992 return output_index
;
5997 out_buf
[output_index
++] = b
^ 0x20;
6003 out_buf
[output_index
++] = b
;
6007 error (_("Unmatched escape character in target response."));
6009 return output_index
;
6012 /* Determine whether the remote target supports binary downloading.
6013 This is accomplished by sending a no-op memory write of zero length
6014 to the target at the specified address. It does not suffice to send
6015 the whole packet, since many stubs strip the eighth bit and
6016 subsequently compute a wrong checksum, which causes real havoc with
6019 NOTE: This can still lose if the serial line is not eight-bit
6020 clean. In cases like this, the user should clear "remote
6024 check_binary_download (CORE_ADDR addr
)
6026 struct remote_state
*rs
= get_remote_state ();
6028 switch (remote_protocol_packets
[PACKET_X
].support
)
6030 case PACKET_DISABLE
:
6034 case PACKET_SUPPORT_UNKNOWN
:
6040 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6042 p
+= hexnumstr (p
, (ULONGEST
) 0);
6046 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6047 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6049 if (rs
->buf
[0] == '\0')
6052 fprintf_unfiltered (gdb_stdlog
,
6053 "binary downloading NOT suppported by target\n");
6054 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6059 fprintf_unfiltered (gdb_stdlog
,
6060 "binary downloading suppported by target\n");
6061 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6068 /* Write memory data directly to the remote machine.
6069 This does not inform the data cache; the data cache uses this.
6070 HEADER is the starting part of the packet.
6071 MEMADDR is the address in the remote memory space.
6072 MYADDR is the address of the buffer in our space.
6073 LEN is the number of bytes.
6074 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6075 should send data as binary ('X'), or hex-encoded ('M').
6077 The function creates packet of the form
6078 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6080 where encoding of <DATA> is termined by PACKET_FORMAT.
6082 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6085 Returns the number of bytes transferred, or 0 (setting errno) for
6086 error. Only transfer a single packet. */
6089 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6090 const gdb_byte
*myaddr
, int len
,
6091 char packet_format
, int use_length
)
6093 struct remote_state
*rs
= get_remote_state ();
6103 if (packet_format
!= 'X' && packet_format
!= 'M')
6104 internal_error (__FILE__
, __LINE__
,
6105 "remote_write_bytes_aux: bad packet format");
6110 payload_size
= get_memory_write_packet_size ();
6112 /* The packet buffer will be large enough for the payload;
6113 get_memory_packet_size ensures this. */
6116 /* Compute the size of the actual payload by subtracting out the
6117 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
6119 payload_size
-= strlen ("$,:#NN");
6121 /* The comma won't be used. */
6123 header_length
= strlen (header
);
6124 payload_size
-= header_length
;
6125 payload_size
-= hexnumlen (memaddr
);
6127 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6129 strcat (rs
->buf
, header
);
6130 p
= rs
->buf
+ strlen (header
);
6132 /* Compute a best guess of the number of bytes actually transfered. */
6133 if (packet_format
== 'X')
6135 /* Best guess at number of bytes that will fit. */
6136 todo
= min (len
, payload_size
);
6138 payload_size
-= hexnumlen (todo
);
6139 todo
= min (todo
, payload_size
);
6143 /* Num bytes that will fit. */
6144 todo
= min (len
, payload_size
/ 2);
6146 payload_size
-= hexnumlen (todo
);
6147 todo
= min (todo
, payload_size
/ 2);
6151 internal_error (__FILE__
, __LINE__
,
6152 _("minumum packet size too small to write data"));
6154 /* If we already need another packet, then try to align the end
6155 of this packet to a useful boundary. */
6156 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6157 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6159 /* Append "<memaddr>". */
6160 memaddr
= remote_address_masked (memaddr
);
6161 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6168 /* Append <len>. Retain the location/size of <len>. It may need to
6169 be adjusted once the packet body has been created. */
6171 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6179 /* Append the packet body. */
6180 if (packet_format
== 'X')
6182 /* Binary mode. Send target system values byte by byte, in
6183 increasing byte addresses. Only escape certain critical
6185 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
6188 /* If not all TODO bytes fit, then we'll need another packet. Make
6189 a second try to keep the end of the packet aligned. Don't do
6190 this if the packet is tiny. */
6191 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6195 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6197 if (new_nr_bytes
!= nr_bytes
)
6198 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6203 p
+= payload_length
;
6204 if (use_length
&& nr_bytes
< todo
)
6206 /* Escape chars have filled up the buffer prematurely,
6207 and we have actually sent fewer bytes than planned.
6208 Fix-up the length field of the packet. Use the same
6209 number of characters as before. */
6210 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6211 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6216 /* Normal mode: Send target system values byte by byte, in
6217 increasing byte addresses. Each byte is encoded as a two hex
6219 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6223 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6224 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6226 if (rs
->buf
[0] == 'E')
6228 /* There is no correspondance between what the remote protocol
6229 uses for errors and errno codes. We would like a cleaner way
6230 of representing errors (big enough to include errno codes,
6231 bfd_error codes, and others). But for now just return EIO. */
6236 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6237 fewer bytes than we'd planned. */
6241 /* Write memory data directly to the remote machine.
6242 This does not inform the data cache; the data cache uses this.
6243 MEMADDR is the address in the remote memory space.
6244 MYADDR is the address of the buffer in our space.
6245 LEN is the number of bytes.
6247 Returns number of bytes transferred, or 0 (setting errno) for
6248 error. Only transfer a single packet. */
6251 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
6253 char *packet_format
= 0;
6255 /* Check whether the target supports binary download. */
6256 check_binary_download (memaddr
);
6258 switch (remote_protocol_packets
[PACKET_X
].support
)
6261 packet_format
= "X";
6263 case PACKET_DISABLE
:
6264 packet_format
= "M";
6266 case PACKET_SUPPORT_UNKNOWN
:
6267 internal_error (__FILE__
, __LINE__
,
6268 _("remote_write_bytes: bad internal state"));
6270 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6273 return remote_write_bytes_aux (packet_format
,
6274 memaddr
, myaddr
, len
, packet_format
[0], 1);
6277 /* Read memory data directly from the remote machine.
6278 This does not use the data cache; the data cache uses this.
6279 MEMADDR is the address in the remote memory space.
6280 MYADDR is the address of the buffer in our space.
6281 LEN is the number of bytes.
6283 Returns number of bytes transferred, or 0 for error. */
6285 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
6286 remote targets) shouldn't attempt to read the entire buffer.
6287 Instead it should read a single packet worth of data and then
6288 return the byte size of that packet to the caller. The caller (its
6289 caller and its callers caller ;-) already contains code for
6290 handling partial reads. */
6293 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
6295 struct remote_state
*rs
= get_remote_state ();
6296 int max_buf_size
; /* Max size of packet output buffer. */
6302 max_buf_size
= get_memory_read_packet_size ();
6303 /* The packet buffer will be large enough for the payload;
6304 get_memory_packet_size ensures this. */
6313 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
6315 /* construct "m"<memaddr>","<len>" */
6316 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
6317 memaddr
= remote_address_masked (memaddr
);
6320 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6322 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6326 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6328 if (rs
->buf
[0] == 'E'
6329 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6330 && rs
->buf
[3] == '\0')
6332 /* There is no correspondance between what the remote
6333 protocol uses for errors and errno codes. We would like
6334 a cleaner way of representing errors (big enough to
6335 include errno codes, bfd_error codes, and others). But
6336 for now just return EIO. */
6341 /* Reply describes memory byte by byte,
6342 each byte encoded as two hex characters. */
6345 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
6347 /* Reply is short. This means that we were able to read
6348 only part of what we wanted to. */
6349 return i
+ (origlen
- len
);
6359 /* Remote notification handler. */
6362 handle_notification (char *buf
, size_t length
)
6364 if (strncmp (buf
, "Stop:", 5) == 0)
6366 if (pending_stop_reply
)
6368 /* We've already parsed the in-flight stop-reply, but the
6369 stub for some reason thought we didn't, possibly due to
6370 timeout on its side. Just ignore it. */
6372 fprintf_unfiltered (gdb_stdlog
, "ignoring resent notification\n");
6376 struct cleanup
*old_chain
;
6377 struct stop_reply
*reply
= stop_reply_xmalloc ();
6379 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
6381 remote_parse_stop_reply (buf
+ 5, reply
);
6383 discard_cleanups (old_chain
);
6385 /* Be careful to only set it after parsing, since an error
6386 may be thrown then. */
6387 pending_stop_reply
= reply
;
6389 /* Notify the event loop there's a stop reply to acknowledge
6390 and that there may be more events to fetch. */
6391 mark_async_event_handler (remote_async_get_pending_events_token
);
6394 fprintf_unfiltered (gdb_stdlog
, "stop notification captured\n");
6398 /* We ignore notifications we don't recognize, for compatibility
6399 with newer stubs. */
6404 /* Read or write LEN bytes from inferior memory at MEMADDR,
6405 transferring to or from debugger address BUFFER. Write to inferior
6406 if SHOULD_WRITE is nonzero. Returns length of data written or
6407 read; 0 for error. TARGET is unused. */
6410 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
6411 int should_write
, struct mem_attrib
*attrib
,
6412 struct target_ops
*target
)
6416 set_general_thread (inferior_ptid
);
6419 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
6421 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
6426 /* Sends a packet with content determined by the printf format string
6427 FORMAT and the remaining arguments, then gets the reply. Returns
6428 whether the packet was a success, a failure, or unknown. */
6430 static enum packet_result
6431 remote_send_printf (const char *format
, ...)
6433 struct remote_state
*rs
= get_remote_state ();
6434 int max_size
= get_remote_packet_size ();
6437 va_start (ap
, format
);
6440 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6441 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
6443 if (putpkt (rs
->buf
) < 0)
6444 error (_("Communication problem with target."));
6447 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6449 return packet_check_result (rs
->buf
);
6453 restore_remote_timeout (void *p
)
6455 int value
= *(int *)p
;
6457 remote_timeout
= value
;
6460 /* Flash writing can take quite some time. We'll set
6461 effectively infinite timeout for flash operations.
6462 In future, we'll need to decide on a better approach. */
6463 static const int remote_flash_timeout
= 1000;
6466 remote_flash_erase (struct target_ops
*ops
,
6467 ULONGEST address
, LONGEST length
)
6469 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
6470 int saved_remote_timeout
= remote_timeout
;
6471 enum packet_result ret
;
6472 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6473 &saved_remote_timeout
);
6475 remote_timeout
= remote_flash_timeout
;
6477 ret
= remote_send_printf ("vFlashErase:%s,%s",
6478 phex (address
, addr_size
),
6482 case PACKET_UNKNOWN
:
6483 error (_("Remote target does not support flash erase"));
6485 error (_("Error erasing flash with vFlashErase packet"));
6490 do_cleanups (back_to
);
6494 remote_flash_write (struct target_ops
*ops
,
6495 ULONGEST address
, LONGEST length
,
6496 const gdb_byte
*data
)
6498 int saved_remote_timeout
= remote_timeout
;
6500 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6501 &saved_remote_timeout
);
6503 remote_timeout
= remote_flash_timeout
;
6504 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
6505 do_cleanups (back_to
);
6511 remote_flash_done (struct target_ops
*ops
)
6513 int saved_remote_timeout
= remote_timeout
;
6515 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6516 &saved_remote_timeout
);
6518 remote_timeout
= remote_flash_timeout
;
6519 ret
= remote_send_printf ("vFlashDone");
6520 do_cleanups (back_to
);
6524 case PACKET_UNKNOWN
:
6525 error (_("Remote target does not support vFlashDone"));
6527 error (_("Error finishing flash operation"));
6534 remote_files_info (struct target_ops
*ignore
)
6536 puts_filtered ("Debugging a target over a serial line.\n");
6539 /* Stuff for dealing with the packets which are part of this protocol.
6540 See comment at top of file for details. */
6542 /* Read a single character from the remote end. */
6545 readchar (int timeout
)
6549 ch
= serial_readchar (remote_desc
, timeout
);
6554 switch ((enum serial_rc
) ch
)
6558 error (_("Remote connection closed"));
6561 perror_with_name (_("Remote communication error"));
6563 case SERIAL_TIMEOUT
:
6569 /* Send the command in *BUF to the remote machine, and read the reply
6570 into *BUF. Report an error if we get an error reply. Resize
6571 *BUF using xrealloc if necessary to hold the result, and update
6575 remote_send (char **buf
,
6579 getpkt (buf
, sizeof_buf
, 0);
6581 if ((*buf
)[0] == 'E')
6582 error (_("Remote failure reply: %s"), *buf
);
6585 /* Return a pointer to an xmalloc'ed string representing an escaped
6586 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6587 etc. The caller is responsible for releasing the returned
6591 escape_buffer (const char *buf
, int n
)
6593 struct cleanup
*old_chain
;
6594 struct ui_file
*stb
;
6597 stb
= mem_fileopen ();
6598 old_chain
= make_cleanup_ui_file_delete (stb
);
6600 fputstrn_unfiltered (buf
, n
, 0, stb
);
6601 str
= ui_file_xstrdup (stb
, NULL
);
6602 do_cleanups (old_chain
);
6606 /* Display a null-terminated packet on stdout, for debugging, using C
6610 print_packet (char *buf
)
6612 puts_filtered ("\"");
6613 fputstr_filtered (buf
, '"', gdb_stdout
);
6614 puts_filtered ("\"");
6620 return putpkt_binary (buf
, strlen (buf
));
6623 /* Send a packet to the remote machine, with error checking. The data
6624 of the packet is in BUF. The string in BUF can be at most
6625 get_remote_packet_size () - 5 to account for the $, # and checksum,
6626 and for a possible /0 if we are debugging (remote_debug) and want
6627 to print the sent packet as a string. */
6630 putpkt_binary (char *buf
, int cnt
)
6632 struct remote_state
*rs
= get_remote_state ();
6634 unsigned char csum
= 0;
6635 char *buf2
= alloca (cnt
+ 6);
6641 /* Catch cases like trying to read memory or listing threads while
6642 we're waiting for a stop reply. The remote server wouldn't be
6643 ready to handle this request, so we'd hang and timeout. We don't
6644 have to worry about this in synchronous mode, because in that
6645 case it's not possible to issue a command while the target is
6646 running. This is not a problem in non-stop mode, because in that
6647 case, the stub is always ready to process serial input. */
6648 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
6649 error (_("Cannot execute this command while the target is running."));
6651 /* We're sending out a new packet. Make sure we don't look at a
6652 stale cached response. */
6653 rs
->cached_wait_status
= 0;
6655 /* Copy the packet into buffer BUF2, encapsulating it
6656 and giving it a checksum. */
6661 for (i
= 0; i
< cnt
; i
++)
6667 *p
++ = tohex ((csum
>> 4) & 0xf);
6668 *p
++ = tohex (csum
& 0xf);
6670 /* Send it over and over until we get a positive ack. */
6674 int started_error_output
= 0;
6678 struct cleanup
*old_chain
;
6682 str
= escape_buffer (buf2
, p
- buf2
);
6683 old_chain
= make_cleanup (xfree
, str
);
6684 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
6685 gdb_flush (gdb_stdlog
);
6686 do_cleanups (old_chain
);
6688 if (serial_write (remote_desc
, buf2
, p
- buf2
))
6689 perror_with_name (_("putpkt: write failed"));
6691 /* If this is a no acks version of the remote protocol, send the
6692 packet and move on. */
6696 /* Read until either a timeout occurs (-2) or '+' is read.
6697 Handle any notification that arrives in the mean time. */
6700 ch
= readchar (remote_timeout
);
6708 case SERIAL_TIMEOUT
:
6711 if (started_error_output
)
6713 putchar_unfiltered ('\n');
6714 started_error_output
= 0;
6723 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6727 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6728 case SERIAL_TIMEOUT
:
6732 break; /* Retransmit buffer. */
6736 fprintf_unfiltered (gdb_stdlog
,
6737 "Packet instead of Ack, ignoring it\n");
6738 /* It's probably an old response sent because an ACK
6739 was lost. Gobble up the packet and ack it so it
6740 doesn't get retransmitted when we resend this
6743 serial_write (remote_desc
, "+", 1);
6744 continue; /* Now, go look for +. */
6751 /* If we got a notification, handle it, and go back to looking
6753 /* We've found the start of a notification. Now
6754 collect the data. */
6755 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6760 struct cleanup
*old_chain
;
6763 str
= escape_buffer (rs
->buf
, val
);
6764 old_chain
= make_cleanup (xfree
, str
);
6765 fprintf_unfiltered (gdb_stdlog
,
6766 " Notification received: %s\n",
6768 do_cleanups (old_chain
);
6770 handle_notification (rs
->buf
, val
);
6771 /* We're in sync now, rewait for the ack. */
6778 if (!started_error_output
)
6780 started_error_output
= 1;
6781 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6783 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6784 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6793 if (!started_error_output
)
6795 started_error_output
= 1;
6796 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6798 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6802 break; /* Here to retransmit. */
6806 /* This is wrong. If doing a long backtrace, the user should be
6807 able to get out next time we call QUIT, without anything as
6808 violent as interrupt_query. If we want to provide a way out of
6809 here without getting to the next QUIT, it should be based on
6810 hitting ^C twice as in remote_wait. */
6821 /* Come here after finding the start of a frame when we expected an
6822 ack. Do our best to discard the rest of this packet. */
6831 c
= readchar (remote_timeout
);
6834 case SERIAL_TIMEOUT
:
6835 /* Nothing we can do. */
6838 /* Discard the two bytes of checksum and stop. */
6839 c
= readchar (remote_timeout
);
6841 c
= readchar (remote_timeout
);
6844 case '*': /* Run length encoding. */
6845 /* Discard the repeat count. */
6846 c
= readchar (remote_timeout
);
6851 /* A regular character. */
6857 /* Come here after finding the start of the frame. Collect the rest
6858 into *BUF, verifying the checksum, length, and handling run-length
6859 compression. NUL terminate the buffer. If there is not enough room,
6860 expand *BUF using xrealloc.
6862 Returns -1 on error, number of characters in buffer (ignoring the
6863 trailing NULL) on success. (could be extended to return one of the
6864 SERIAL status indications). */
6867 read_frame (char **buf_p
,
6874 struct remote_state
*rs
= get_remote_state ();
6881 c
= readchar (remote_timeout
);
6884 case SERIAL_TIMEOUT
:
6886 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
6890 fputs_filtered ("Saw new packet start in middle of old one\n",
6892 return -1; /* Start a new packet, count retries. */
6895 unsigned char pktcsum
;
6901 check_0
= readchar (remote_timeout
);
6903 check_1
= readchar (remote_timeout
);
6905 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
6908 fputs_filtered ("Timeout in checksum, retrying\n",
6912 else if (check_0
< 0 || check_1
< 0)
6915 fputs_filtered ("Communication error in checksum\n",
6920 /* Don't recompute the checksum; with no ack packets we
6921 don't have any way to indicate a packet retransmission
6926 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
6927 if (csum
== pktcsum
)
6932 struct cleanup
*old_chain
;
6935 str
= escape_buffer (buf
, bc
);
6936 old_chain
= make_cleanup (xfree
, str
);
6937 fprintf_unfiltered (gdb_stdlog
,
6939 Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
6940 pktcsum
, csum
, str
);
6941 do_cleanups (old_chain
);
6943 /* Number of characters in buffer ignoring trailing
6947 case '*': /* Run length encoding. */
6952 c
= readchar (remote_timeout
);
6954 repeat
= c
- ' ' + 3; /* Compute repeat count. */
6956 /* The character before ``*'' is repeated. */
6958 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
6960 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
6962 /* Make some more room in the buffer. */
6963 *sizeof_buf
+= repeat
;
6964 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6968 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
6974 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
6978 if (bc
>= *sizeof_buf
- 1)
6980 /* Make some more room in the buffer. */
6982 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6993 /* Read a packet from the remote machine, with error checking, and
6994 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6995 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6996 rather than timing out; this is used (in synchronous mode) to wait
6997 for a target that is is executing user code to stop. */
6998 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
6999 don't have to change all the calls to getpkt to deal with the
7000 return value, because at the moment I don't know what the right
7001 thing to do it for those. */
7009 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7013 /* Read a packet from the remote machine, with error checking, and
7014 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7015 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7016 rather than timing out; this is used (in synchronous mode) to wait
7017 for a target that is is executing user code to stop. If FOREVER ==
7018 0, this function is allowed to time out gracefully and return an
7019 indication of this to the caller. Otherwise return the number of
7020 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7021 enough reason to return to the caller. */
7024 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7025 int expecting_notif
)
7027 struct remote_state
*rs
= get_remote_state ();
7033 /* We're reading a new response. Make sure we don't look at a
7034 previously cached response. */
7035 rs
->cached_wait_status
= 0;
7037 strcpy (*buf
, "timeout");
7040 timeout
= watchdog
> 0 ? watchdog
: -1;
7041 else if (expecting_notif
)
7042 timeout
= 0; /* There should already be a char in the buffer. If
7045 timeout
= remote_timeout
;
7049 /* Process any number of notifications, and then return when
7053 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7055 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7057 /* This can loop forever if the remote side sends us
7058 characters continuously, but if it pauses, we'll get
7059 SERIAL_TIMEOUT from readchar because of timeout. Then
7060 we'll count that as a retry.
7062 Note that even when forever is set, we will only wait
7063 forever prior to the start of a packet. After that, we
7064 expect characters to arrive at a brisk pace. They should
7065 show up within remote_timeout intervals. */
7067 c
= readchar (timeout
);
7068 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7070 if (c
== SERIAL_TIMEOUT
)
7072 if (expecting_notif
)
7073 return -1; /* Don't complain, it's normal to not get
7074 anything in this case. */
7076 if (forever
) /* Watchdog went off? Kill the target. */
7080 error (_("Watchdog timeout has expired. Target detached."));
7083 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7087 /* We've found the start of a packet or notification.
7088 Now collect the data. */
7089 val
= read_frame (buf
, sizeof_buf
);
7094 serial_write (remote_desc
, "-", 1);
7097 if (tries
> MAX_TRIES
)
7099 /* We have tried hard enough, and just can't receive the
7100 packet/notification. Give up. */
7101 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7103 /* Skip the ack char if we're in no-ack mode. */
7104 if (!rs
->noack_mode
)
7105 serial_write (remote_desc
, "+", 1);
7109 /* If we got an ordinary packet, return that to our caller. */
7114 struct cleanup
*old_chain
;
7117 str
= escape_buffer (*buf
, val
);
7118 old_chain
= make_cleanup (xfree
, str
);
7119 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7120 do_cleanups (old_chain
);
7123 /* Skip the ack char if we're in no-ack mode. */
7124 if (!rs
->noack_mode
)
7125 serial_write (remote_desc
, "+", 1);
7129 /* If we got a notification, handle it, and go back to looking
7133 gdb_assert (c
== '%');
7137 struct cleanup
*old_chain
;
7140 str
= escape_buffer (*buf
, val
);
7141 old_chain
= make_cleanup (xfree
, str
);
7142 fprintf_unfiltered (gdb_stdlog
,
7143 " Notification received: %s\n",
7145 do_cleanups (old_chain
);
7148 handle_notification (*buf
, val
);
7150 /* Notifications require no acknowledgement. */
7152 if (expecting_notif
)
7159 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7161 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
7165 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
7167 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
7172 remote_kill (struct target_ops
*ops
)
7174 /* Use catch_errors so the user can quit from gdb even when we
7175 aren't on speaking terms with the remote system. */
7176 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
7178 /* Don't wait for it to die. I'm not really sure it matters whether
7179 we do or not. For the existing stubs, kill is a noop. */
7180 target_mourn_inferior ();
7184 remote_vkill (int pid
, struct remote_state
*rs
)
7186 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7189 /* Tell the remote target to detach. */
7190 sprintf (rs
->buf
, "vKill;%x", pid
);
7192 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7194 if (packet_ok (rs
->buf
,
7195 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7197 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7204 extended_remote_kill (struct target_ops
*ops
)
7207 int pid
= ptid_get_pid (inferior_ptid
);
7208 struct remote_state
*rs
= get_remote_state ();
7210 res
= remote_vkill (pid
, rs
);
7211 if (res
== -1 && !remote_multi_process_p (rs
))
7213 /* Don't try 'k' on a multi-process aware stub -- it has no way
7214 to specify the pid. */
7218 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7219 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7222 /* Don't wait for it to die. I'm not really sure it matters whether
7223 we do or not. For the existing stubs, kill is a noop. */
7229 error (_("Can't kill process"));
7231 target_mourn_inferior ();
7235 remote_mourn (struct target_ops
*ops
)
7237 remote_mourn_1 (ops
);
7240 /* Worker function for remote_mourn. */
7242 remote_mourn_1 (struct target_ops
*target
)
7244 unpush_target (target
);
7246 /* remote_close takes care of doing most of the clean up. */
7247 generic_mourn_inferior ();
7251 extended_remote_mourn_1 (struct target_ops
*target
)
7253 struct remote_state
*rs
= get_remote_state ();
7255 /* In case we got here due to an error, but we're going to stay
7257 rs
->waiting_for_stop_reply
= 0;
7259 /* We're no longer interested in these events. */
7260 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
7262 /* If the current general thread belonged to the process we just
7263 detached from or has exited, the remote side current general
7264 thread becomes undefined. Considering a case like this:
7266 - We just got here due to a detach.
7267 - The process that we're detaching from happens to immediately
7268 report a global breakpoint being hit in non-stop mode, in the
7269 same thread we had selected before.
7270 - GDB attaches to this process again.
7271 - This event happens to be the next event we handle.
7273 GDB would consider that the current general thread didn't need to
7274 be set on the stub side (with Hg), since for all it knew,
7275 GENERAL_THREAD hadn't changed.
7277 Notice that although in all-stop mode, the remote server always
7278 sets the current thread to the thread reporting the stop event,
7279 that doesn't happen in non-stop mode; in non-stop, the stub *must
7280 not* change the current thread when reporting a breakpoint hit,
7281 due to the decoupling of event reporting and event handling.
7283 To keep things simple, we always invalidate our notion of the
7285 record_currthread (minus_one_ptid
);
7287 /* Unlike "target remote", we do not want to unpush the target; then
7288 the next time the user says "run", we won't be connected. */
7290 /* Call common code to mark the inferior as not running. */
7291 generic_mourn_inferior ();
7293 if (!have_inferiors ())
7295 if (!remote_multi_process_p (rs
))
7297 /* Check whether the target is running now - some remote stubs
7298 automatically restart after kill. */
7300 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7302 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7304 /* Assume that the target has been restarted. Set inferior_ptid
7305 so that bits of core GDB realizes there's something here, e.g.,
7306 so that the user can say "kill" again. */
7307 inferior_ptid
= magic_null_ptid
;
7314 extended_remote_mourn (struct target_ops
*ops
)
7316 extended_remote_mourn_1 (ops
);
7320 extended_remote_run (char *args
)
7322 struct remote_state
*rs
= get_remote_state ();
7325 /* If the user has disabled vRun support, or we have detected that
7326 support is not available, do not try it. */
7327 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7330 strcpy (rs
->buf
, "vRun;");
7331 len
= strlen (rs
->buf
);
7333 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7334 error (_("Remote file name too long for run packet"));
7335 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
7337 gdb_assert (args
!= NULL
);
7340 struct cleanup
*back_to
;
7344 argv
= gdb_buildargv (args
);
7345 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7346 for (i
= 0; argv
[i
] != NULL
; i
++)
7348 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7349 error (_("Argument list too long for run packet"));
7350 rs
->buf
[len
++] = ';';
7351 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
7353 do_cleanups (back_to
);
7356 rs
->buf
[len
++] = '\0';
7359 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7361 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7363 /* We have a wait response; we don't need it, though. All is well. */
7366 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7367 /* It wasn't disabled before, but it is now. */
7371 if (remote_exec_file
[0] == '\0')
7372 error (_("Running the default executable on the remote target failed; "
7373 "try \"set remote exec-file\"?"));
7375 error (_("Running \"%s\" on the remote target failed"),
7380 /* In the extended protocol we want to be able to do things like
7381 "run" and have them basically work as expected. So we need
7382 a special create_inferior function. We support changing the
7383 executable file and the command line arguments, but not the
7387 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
7388 char **env
, int from_tty
)
7390 /* If running asynchronously, register the target file descriptor
7391 with the event loop. */
7392 if (target_can_async_p ())
7393 target_async (inferior_event_handler
, 0);
7395 /* Now restart the remote server. */
7396 if (extended_remote_run (args
) == -1)
7398 /* vRun was not supported. Fail if we need it to do what the
7400 if (remote_exec_file
[0])
7401 error (_("Remote target does not support \"set remote exec-file\""));
7403 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7405 /* Fall back to "R". */
7406 extended_remote_restart ();
7409 if (!have_inferiors ())
7411 /* Clean up from the last time we ran, before we mark the target
7412 running again. This will mark breakpoints uninserted, and
7413 get_offsets may insert breakpoints. */
7414 init_thread_list ();
7415 init_wait_for_inferior ();
7418 /* Now mark the inferior as running before we do anything else. */
7419 inferior_ptid
= magic_null_ptid
;
7421 /* Now, if we have thread information, update inferior_ptid. */
7422 inferior_ptid
= remote_current_thread (inferior_ptid
);
7424 remote_add_inferior (ptid_get_pid (inferior_ptid
), 0);
7425 add_thread_silent (inferior_ptid
);
7427 /* Get updated offsets, if the stub uses qOffsets. */
7432 extended_remote_create_inferior (struct target_ops
*ops
,
7433 char *exec_file
, char *args
,
7434 char **env
, int from_tty
)
7436 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
7440 /* Insert a breakpoint. On targets that have software breakpoint
7441 support, we ask the remote target to do the work; on targets
7442 which don't, we insert a traditional memory breakpoint. */
7445 remote_insert_breakpoint (struct gdbarch
*gdbarch
,
7446 struct bp_target_info
*bp_tgt
)
7448 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7449 If it succeeds, then set the support to PACKET_ENABLE. If it
7450 fails, and the user has explicitly requested the Z support then
7451 report an error, otherwise, mark it disabled and go on. */
7453 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7455 CORE_ADDR addr
= bp_tgt
->placed_address
;
7456 struct remote_state
*rs
;
7460 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
7462 rs
= get_remote_state ();
7468 addr
= (ULONGEST
) remote_address_masked (addr
);
7469 p
+= hexnumstr (p
, addr
);
7470 sprintf (p
, ",%d", bpsize
);
7473 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7475 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
7480 bp_tgt
->placed_address
= addr
;
7481 bp_tgt
->placed_size
= bpsize
;
7483 case PACKET_UNKNOWN
:
7488 return memory_insert_breakpoint (gdbarch
, bp_tgt
);
7492 remote_remove_breakpoint (struct gdbarch
*gdbarch
,
7493 struct bp_target_info
*bp_tgt
)
7495 CORE_ADDR addr
= bp_tgt
->placed_address
;
7496 struct remote_state
*rs
= get_remote_state ();
7498 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7506 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
7507 p
+= hexnumstr (p
, addr
);
7508 sprintf (p
, ",%d", bp_tgt
->placed_size
);
7511 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7513 return (rs
->buf
[0] == 'E');
7516 return memory_remove_breakpoint (gdbarch
, bp_tgt
);
7520 watchpoint_to_Z_packet (int type
)
7525 return Z_PACKET_WRITE_WP
;
7528 return Z_PACKET_READ_WP
;
7531 return Z_PACKET_ACCESS_WP
;
7534 internal_error (__FILE__
, __LINE__
,
7535 _("hw_bp_to_z: bad watchpoint type %d"), type
);
7540 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
7542 struct remote_state
*rs
= get_remote_state ();
7544 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7546 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7549 sprintf (rs
->buf
, "Z%x,", packet
);
7550 p
= strchr (rs
->buf
, '\0');
7551 addr
= remote_address_masked (addr
);
7552 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7553 sprintf (p
, ",%x", len
);
7556 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7558 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7562 case PACKET_UNKNOWN
:
7567 internal_error (__FILE__
, __LINE__
,
7568 _("remote_insert_watchpoint: reached end of function"));
7573 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
7575 struct remote_state
*rs
= get_remote_state ();
7577 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7579 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7582 sprintf (rs
->buf
, "z%x,", packet
);
7583 p
= strchr (rs
->buf
, '\0');
7584 addr
= remote_address_masked (addr
);
7585 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7586 sprintf (p
, ",%x", len
);
7588 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7590 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7593 case PACKET_UNKNOWN
:
7598 internal_error (__FILE__
, __LINE__
,
7599 _("remote_remove_watchpoint: reached end of function"));
7603 int remote_hw_watchpoint_limit
= -1;
7604 int remote_hw_breakpoint_limit
= -1;
7607 remote_check_watch_resources (int type
, int cnt
, int ot
)
7609 if (type
== bp_hardware_breakpoint
)
7611 if (remote_hw_breakpoint_limit
== 0)
7613 else if (remote_hw_breakpoint_limit
< 0)
7615 else if (cnt
<= remote_hw_breakpoint_limit
)
7620 if (remote_hw_watchpoint_limit
== 0)
7622 else if (remote_hw_watchpoint_limit
< 0)
7626 else if (cnt
<= remote_hw_watchpoint_limit
)
7633 remote_stopped_by_watchpoint (void)
7635 return remote_stopped_by_watchpoint_p
;
7639 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
7643 if (remote_stopped_by_watchpoint ())
7645 *addr_p
= remote_watch_data_address
;
7654 remote_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
7655 struct bp_target_info
*bp_tgt
)
7658 struct remote_state
*rs
;
7661 /* The length field should be set to the size of a breakpoint
7662 instruction, even though we aren't inserting one ourselves. */
7664 gdbarch_remote_breakpoint_from_pc
7665 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
7667 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7670 rs
= get_remote_state ();
7677 addr
= remote_address_masked (bp_tgt
->placed_address
);
7678 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7679 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7682 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7684 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7687 case PACKET_UNKNOWN
:
7692 internal_error (__FILE__
, __LINE__
,
7693 _("remote_insert_hw_breakpoint: reached end of function"));
7698 remote_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
7699 struct bp_target_info
*bp_tgt
)
7702 struct remote_state
*rs
= get_remote_state ();
7705 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7712 addr
= remote_address_masked (bp_tgt
->placed_address
);
7713 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7714 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7717 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7719 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7722 case PACKET_UNKNOWN
:
7727 internal_error (__FILE__
, __LINE__
,
7728 _("remote_remove_hw_breakpoint: reached end of function"));
7731 /* Table used by the crc32 function to calcuate the checksum. */
7733 static unsigned long crc32_table
[256] =
7736 static unsigned long
7737 crc32 (const unsigned char *buf
, int len
, unsigned int crc
)
7739 if (!crc32_table
[1])
7741 /* Initialize the CRC table and the decoding table. */
7745 for (i
= 0; i
< 256; i
++)
7747 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7748 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7755 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7761 /* Verify memory using the "qCRC:" request. */
7764 remote_verify_memory (struct target_ops
*ops
,
7765 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
7767 struct remote_state
*rs
= get_remote_state ();
7768 unsigned long host_crc
, target_crc
;
7771 /* FIXME: assumes lma can fit into long. */
7772 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7773 (long) lma
, (long) size
);
7776 /* Be clever; compute the host_crc before waiting for target
7778 host_crc
= crc32 (data
, size
, 0xffffffff);
7780 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7781 if (rs
->buf
[0] == 'E')
7784 if (rs
->buf
[0] != 'C')
7785 error (_("remote target does not support this operation"));
7787 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7788 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7790 return (host_crc
== target_crc
);
7793 /* compare-sections command
7795 With no arguments, compares each loadable section in the exec bfd
7796 with the same memory range on the target, and reports mismatches.
7797 Useful for verifying the image on the target against the exec file. */
7800 compare_sections_command (char *args
, int from_tty
)
7803 struct cleanup
*old_chain
;
7805 const char *sectname
;
7813 error (_("command cannot be used without an exec file"));
7815 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7817 if (!(s
->flags
& SEC_LOAD
))
7818 continue; /* skip non-loadable section */
7820 size
= bfd_get_section_size (s
);
7822 continue; /* skip zero-length section */
7824 sectname
= bfd_get_section_name (exec_bfd
, s
);
7825 if (args
&& strcmp (args
, sectname
) != 0)
7826 continue; /* not the section selected by user */
7828 matched
= 1; /* do this section */
7831 sectdata
= xmalloc (size
);
7832 old_chain
= make_cleanup (xfree
, sectdata
);
7833 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7835 res
= target_verify_memory (sectdata
, lma
, size
);
7838 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
7839 paddress (target_gdbarch
, lma
),
7840 paddress (target_gdbarch
, lma
+ size
));
7842 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
7843 paddress (target_gdbarch
, lma
),
7844 paddress (target_gdbarch
, lma
+ size
));
7846 printf_filtered ("matched.\n");
7849 printf_filtered ("MIS-MATCHED!\n");
7853 do_cleanups (old_chain
);
7856 warning (_("One or more sections of the remote executable does not match\n\
7857 the loaded file\n"));
7858 if (args
&& !matched
)
7859 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7862 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7863 into remote target. The number of bytes written to the remote
7864 target is returned, or -1 for error. */
7867 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7868 const char *annex
, const gdb_byte
*writebuf
,
7869 ULONGEST offset
, LONGEST len
,
7870 struct packet_config
*packet
)
7874 struct remote_state
*rs
= get_remote_state ();
7875 int max_size
= get_memory_write_packet_size ();
7877 if (packet
->support
== PACKET_DISABLE
)
7880 /* Insert header. */
7881 i
= snprintf (rs
->buf
, max_size
,
7882 "qXfer:%s:write:%s:%s:",
7883 object_name
, annex
? annex
: "",
7884 phex_nz (offset
, sizeof offset
));
7885 max_size
-= (i
+ 1);
7887 /* Escape as much data as fits into rs->buf. */
7888 buf_len
= remote_escape_output
7889 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
7891 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
7892 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7893 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7896 unpack_varlen_hex (rs
->buf
, &n
);
7900 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7901 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7902 number of bytes read is returned, or 0 for EOF, or -1 for error.
7903 The number of bytes read may be less than LEN without indicating an
7904 EOF. PACKET is checked and updated to indicate whether the remote
7905 target supports this object. */
7908 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
7910 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
7911 struct packet_config
*packet
)
7913 static char *finished_object
;
7914 static char *finished_annex
;
7915 static ULONGEST finished_offset
;
7917 struct remote_state
*rs
= get_remote_state ();
7918 LONGEST i
, n
, packet_len
;
7920 if (packet
->support
== PACKET_DISABLE
)
7923 /* Check whether we've cached an end-of-object packet that matches
7925 if (finished_object
)
7927 if (strcmp (object_name
, finished_object
) == 0
7928 && strcmp (annex
? annex
: "", finished_annex
) == 0
7929 && offset
== finished_offset
)
7932 /* Otherwise, we're now reading something different. Discard
7934 xfree (finished_object
);
7935 xfree (finished_annex
);
7936 finished_object
= NULL
;
7937 finished_annex
= NULL
;
7940 /* Request only enough to fit in a single packet. The actual data
7941 may not, since we don't know how much of it will need to be escaped;
7942 the target is free to respond with slightly less data. We subtract
7943 five to account for the response type and the protocol frame. */
7944 n
= min (get_remote_packet_size () - 5, len
);
7945 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
7946 object_name
, annex
? annex
: "",
7947 phex_nz (offset
, sizeof offset
),
7948 phex_nz (n
, sizeof n
));
7949 i
= putpkt (rs
->buf
);
7954 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7955 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7958 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
7959 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
7961 /* 'm' means there is (or at least might be) more data after this
7962 batch. That does not make sense unless there's at least one byte
7963 of data in this reply. */
7964 if (rs
->buf
[0] == 'm' && packet_len
== 1)
7965 error (_("Remote qXfer reply contained no data."));
7967 /* Got some data. */
7968 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
7970 /* 'l' is an EOF marker, possibly including a final block of data,
7971 or possibly empty. If we have the final block of a non-empty
7972 object, record this fact to bypass a subsequent partial read. */
7973 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
7975 finished_object
= xstrdup (object_name
);
7976 finished_annex
= xstrdup (annex
? annex
: "");
7977 finished_offset
= offset
+ i
;
7984 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
7985 const char *annex
, gdb_byte
*readbuf
,
7986 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
7988 struct remote_state
*rs
;
7993 set_general_thread (inferior_ptid
);
7995 rs
= get_remote_state ();
7997 /* Handle memory using the standard memory routines. */
7998 if (object
== TARGET_OBJECT_MEMORY
)
8004 /* If the remote target is connected but not running, we should
8005 pass this request down to a lower stratum (e.g. the executable
8007 if (!target_has_execution
)
8010 if (writebuf
!= NULL
)
8011 xfered
= remote_write_bytes (offset
, writebuf
, len
);
8013 xfered
= remote_read_bytes (offset
, readbuf
, len
);
8017 else if (xfered
== 0 && errno
== 0)
8023 /* Handle SPU memory using qxfer packets. */
8024 if (object
== TARGET_OBJECT_SPU
)
8027 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8028 &remote_protocol_packets
8029 [PACKET_qXfer_spu_read
]);
8031 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8032 &remote_protocol_packets
8033 [PACKET_qXfer_spu_write
]);
8036 /* Handle extra signal info using qxfer packets. */
8037 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8040 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8041 &remote_protocol_packets
8042 [PACKET_qXfer_siginfo_read
]);
8044 return remote_write_qxfer (ops
, "siginfo", annex
, writebuf
, offset
, len
,
8045 &remote_protocol_packets
8046 [PACKET_qXfer_siginfo_write
]);
8049 /* Only handle flash writes. */
8050 if (writebuf
!= NULL
)
8056 case TARGET_OBJECT_FLASH
:
8057 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
8061 else if (xfered
== 0 && errno
== 0)
8071 /* Map pre-existing objects onto letters. DO NOT do this for new
8072 objects!!! Instead specify new query packets. */
8075 case TARGET_OBJECT_AVR
:
8079 case TARGET_OBJECT_AUXV
:
8080 gdb_assert (annex
== NULL
);
8081 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8082 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8084 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8085 return remote_read_qxfer
8086 (ops
, "features", annex
, readbuf
, offset
, len
,
8087 &remote_protocol_packets
[PACKET_qXfer_features
]);
8089 case TARGET_OBJECT_LIBRARIES
:
8090 return remote_read_qxfer
8091 (ops
, "libraries", annex
, readbuf
, offset
, len
,
8092 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8094 case TARGET_OBJECT_MEMORY_MAP
:
8095 gdb_assert (annex
== NULL
);
8096 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8097 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8099 case TARGET_OBJECT_OSDATA
:
8100 /* Should only get here if we're connected. */
8101 gdb_assert (remote_desc
);
8102 return remote_read_qxfer
8103 (ops
, "osdata", annex
, readbuf
, offset
, len
,
8104 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8106 case TARGET_OBJECT_THREADS
:
8107 gdb_assert (annex
== NULL
);
8108 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8109 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8115 /* Note: a zero OFFSET and LEN can be used to query the minimum
8117 if (offset
== 0 && len
== 0)
8118 return (get_remote_packet_size ());
8119 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8120 large enough let the caller deal with it. */
8121 if (len
< get_remote_packet_size ())
8123 len
= get_remote_packet_size ();
8125 /* Except for querying the minimum buffer size, target must be open. */
8127 error (_("remote query is only available after target open"));
8129 gdb_assert (annex
!= NULL
);
8130 gdb_assert (readbuf
!= NULL
);
8136 /* We used one buffer char for the remote protocol q command and
8137 another for the query type. As the remote protocol encapsulation
8138 uses 4 chars plus one extra in case we are debugging
8139 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8142 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8144 /* Bad caller may have sent forbidden characters. */
8145 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8150 gdb_assert (annex
[i
] == '\0');
8152 i
= putpkt (rs
->buf
);
8156 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8157 strcpy ((char *) readbuf
, rs
->buf
);
8159 return strlen ((char *) readbuf
);
8163 remote_search_memory (struct target_ops
* ops
,
8164 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8165 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8166 CORE_ADDR
*found_addrp
)
8168 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
8169 struct remote_state
*rs
= get_remote_state ();
8170 int max_size
= get_memory_write_packet_size ();
8171 struct packet_config
*packet
=
8172 &remote_protocol_packets
[PACKET_qSearch_memory
];
8173 /* number of packet bytes used to encode the pattern,
8174 this could be more than PATTERN_LEN due to escape characters */
8175 int escaped_pattern_len
;
8176 /* amount of pattern that was encodable in the packet */
8177 int used_pattern_len
;
8180 ULONGEST found_addr
;
8182 /* Don't go to the target if we don't have to.
8183 This is done before checking packet->support to avoid the possibility that
8184 a success for this edge case means the facility works in general. */
8185 if (pattern_len
> search_space_len
)
8187 if (pattern_len
== 0)
8189 *found_addrp
= start_addr
;
8193 /* If we already know the packet isn't supported, fall back to the simple
8194 way of searching memory. */
8196 if (packet
->support
== PACKET_DISABLE
)
8198 /* Target doesn't provided special support, fall back and use the
8199 standard support (copy memory and do the search here). */
8200 return simple_search_memory (ops
, start_addr
, search_space_len
,
8201 pattern
, pattern_len
, found_addrp
);
8204 /* Insert header. */
8205 i
= snprintf (rs
->buf
, max_size
,
8206 "qSearch:memory:%s;%s;",
8207 phex_nz (start_addr
, addr_size
),
8208 phex_nz (search_space_len
, sizeof (search_space_len
)));
8209 max_size
-= (i
+ 1);
8211 /* Escape as much data as fits into rs->buf. */
8212 escaped_pattern_len
=
8213 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
8214 &used_pattern_len
, max_size
);
8216 /* Bail if the pattern is too large. */
8217 if (used_pattern_len
!= pattern_len
)
8218 error ("Pattern is too large to transmit to remote target.");
8220 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8221 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8222 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8224 /* The request may not have worked because the command is not
8225 supported. If so, fall back to the simple way. */
8226 if (packet
->support
== PACKET_DISABLE
)
8228 return simple_search_memory (ops
, start_addr
, search_space_len
,
8229 pattern
, pattern_len
, found_addrp
);
8234 if (rs
->buf
[0] == '0')
8236 else if (rs
->buf
[0] == '1')
8239 if (rs
->buf
[1] != ',')
8240 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8241 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8242 *found_addrp
= found_addr
;
8245 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8251 remote_rcmd (char *command
,
8252 struct ui_file
*outbuf
)
8254 struct remote_state
*rs
= get_remote_state ();
8258 error (_("remote rcmd is only available after target open"));
8260 /* Send a NULL command across as an empty command. */
8261 if (command
== NULL
)
8264 /* The query prefix. */
8265 strcpy (rs
->buf
, "qRcmd,");
8266 p
= strchr (rs
->buf
, '\0');
8268 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
8269 error (_("\"monitor\" command ``%s'' is too long."), command
);
8271 /* Encode the actual command. */
8272 bin2hex ((gdb_byte
*) command
, p
, 0);
8274 if (putpkt (rs
->buf
) < 0)
8275 error (_("Communication problem with target."));
8277 /* get/display the response */
8282 /* XXX - see also remote_get_noisy_reply(). */
8284 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8287 error (_("Target does not support this command."));
8288 if (buf
[0] == 'O' && buf
[1] != 'K')
8290 remote_console_output (buf
+ 1); /* 'O' message from stub. */
8293 if (strcmp (buf
, "OK") == 0)
8295 if (strlen (buf
) == 3 && buf
[0] == 'E'
8296 && isdigit (buf
[1]) && isdigit (buf
[2]))
8298 error (_("Protocol error with Rcmd"));
8300 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
8302 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
8304 fputc_unfiltered (c
, outbuf
);
8310 static VEC(mem_region_s
) *
8311 remote_memory_map (struct target_ops
*ops
)
8313 VEC(mem_region_s
) *result
= NULL
;
8314 char *text
= target_read_stralloc (¤t_target
,
8315 TARGET_OBJECT_MEMORY_MAP
, NULL
);
8319 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
8321 result
= parse_memory_map (text
);
8322 do_cleanups (back_to
);
8329 packet_command (char *args
, int from_tty
)
8331 struct remote_state
*rs
= get_remote_state ();
8334 error (_("command can only be used with remote target"));
8337 error (_("remote-packet command requires packet text as argument"));
8339 puts_filtered ("sending: ");
8340 print_packet (args
);
8341 puts_filtered ("\n");
8344 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8345 puts_filtered ("received: ");
8346 print_packet (rs
->buf
);
8347 puts_filtered ("\n");
8351 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8353 static void display_thread_info (struct gdb_ext_thread_info
*info
);
8355 static void threadset_test_cmd (char *cmd
, int tty
);
8357 static void threadalive_test (char *cmd
, int tty
);
8359 static void threadlist_test_cmd (char *cmd
, int tty
);
8361 int get_and_display_threadinfo (threadref
*ref
);
8363 static void threadinfo_test_cmd (char *cmd
, int tty
);
8365 static int thread_display_step (threadref
*ref
, void *context
);
8367 static void threadlist_update_test_cmd (char *cmd
, int tty
);
8369 static void init_remote_threadtests (void);
8371 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8374 threadset_test_cmd (char *cmd
, int tty
)
8376 int sample_thread
= SAMPLE_THREAD
;
8378 printf_filtered (_("Remote threadset test\n"));
8379 set_general_thread (sample_thread
);
8384 threadalive_test (char *cmd
, int tty
)
8386 int sample_thread
= SAMPLE_THREAD
;
8387 int pid
= ptid_get_pid (inferior_ptid
);
8388 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
8390 if (remote_thread_alive (ptid
))
8391 printf_filtered ("PASS: Thread alive test\n");
8393 printf_filtered ("FAIL: Thread alive test\n");
8396 void output_threadid (char *title
, threadref
*ref
);
8399 output_threadid (char *title
, threadref
*ref
)
8403 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
8405 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
8409 threadlist_test_cmd (char *cmd
, int tty
)
8412 threadref nextthread
;
8413 int done
, result_count
;
8414 threadref threadlist
[3];
8416 printf_filtered ("Remote Threadlist test\n");
8417 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
8418 &result_count
, &threadlist
[0]))
8419 printf_filtered ("FAIL: threadlist test\n");
8422 threadref
*scan
= threadlist
;
8423 threadref
*limit
= scan
+ result_count
;
8425 while (scan
< limit
)
8426 output_threadid (" thread ", scan
++);
8431 display_thread_info (struct gdb_ext_thread_info
*info
)
8433 output_threadid ("Threadid: ", &info
->threadid
);
8434 printf_filtered ("Name: %s\n ", info
->shortname
);
8435 printf_filtered ("State: %s\n", info
->display
);
8436 printf_filtered ("other: %s\n\n", info
->more_display
);
8440 get_and_display_threadinfo (threadref
*ref
)
8444 struct gdb_ext_thread_info threadinfo
;
8446 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
8447 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
8448 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
8449 display_thread_info (&threadinfo
);
8454 threadinfo_test_cmd (char *cmd
, int tty
)
8456 int athread
= SAMPLE_THREAD
;
8460 int_to_threadref (&thread
, athread
);
8461 printf_filtered ("Remote Threadinfo test\n");
8462 if (!get_and_display_threadinfo (&thread
))
8463 printf_filtered ("FAIL cannot get thread info\n");
8467 thread_display_step (threadref
*ref
, void *context
)
8469 /* output_threadid(" threadstep ",ref); *//* simple test */
8470 return get_and_display_threadinfo (ref
);
8474 threadlist_update_test_cmd (char *cmd
, int tty
)
8476 printf_filtered ("Remote Threadlist update test\n");
8477 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
8481 init_remote_threadtests (void)
8483 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
8484 Fetch and print the remote list of thread identifiers, one pkt only"));
8485 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
8486 _("Fetch and display info about one thread"));
8487 add_com ("tset", class_obscure
, threadset_test_cmd
,
8488 _("Test setting to a different thread"));
8489 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
8490 _("Iterate through updating all remote thread info"));
8491 add_com ("talive", class_obscure
, threadalive_test
,
8492 _(" Remote thread alive test "));
8497 /* Convert a thread ID to a string. Returns the string in a static
8501 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
8503 static char buf
[64];
8504 struct remote_state
*rs
= get_remote_state ();
8506 if (ptid_is_pid (ptid
))
8508 /* Printing an inferior target id. */
8510 /* When multi-process extensions are off, there's no way in the
8511 remote protocol to know the remote process id, if there's any
8512 at all. There's one exception --- when we're connected with
8513 target extended-remote, and we manually attached to a process
8514 with "attach PID". We don't record anywhere a flag that
8515 allows us to distinguish that case from the case of
8516 connecting with extended-remote and the stub already being
8517 attached to a process, and reporting yes to qAttached, hence
8518 no smart special casing here. */
8519 if (!remote_multi_process_p (rs
))
8521 xsnprintf (buf
, sizeof buf
, "Remote target");
8525 return normal_pid_to_str (ptid
);
8529 if (ptid_equal (magic_null_ptid
, ptid
))
8530 xsnprintf (buf
, sizeof buf
, "Thread <main>");
8531 else if (remote_multi_process_p (rs
))
8532 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
8533 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
8535 xsnprintf (buf
, sizeof buf
, "Thread %ld",
8536 ptid_get_tid (ptid
));
8541 /* Get the address of the thread local variable in OBJFILE which is
8542 stored at OFFSET within the thread local storage for thread PTID. */
8545 remote_get_thread_local_address (struct target_ops
*ops
,
8546 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
8548 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
8550 struct remote_state
*rs
= get_remote_state ();
8552 char *endp
= rs
->buf
+ get_remote_packet_size ();
8553 enum packet_result result
;
8555 strcpy (p
, "qGetTLSAddr:");
8557 p
= write_ptid (p
, endp
, ptid
);
8559 p
+= hexnumstr (p
, offset
);
8561 p
+= hexnumstr (p
, lm
);
8565 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8566 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
8567 if (result
== PACKET_OK
)
8571 unpack_varlen_hex (rs
->buf
, &result
);
8574 else if (result
== PACKET_UNKNOWN
)
8575 throw_error (TLS_GENERIC_ERROR
,
8576 _("Remote target doesn't support qGetTLSAddr packet"));
8578 throw_error (TLS_GENERIC_ERROR
,
8579 _("Remote target failed to process qGetTLSAddr request"));
8582 throw_error (TLS_GENERIC_ERROR
,
8583 _("TLS not supported or disabled on this target"));
8588 /* Provide thread local base, i.e. Thread Information Block address.
8589 Returns 1 if ptid is found and thread_local_base is non zero. */
8592 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
8594 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
8596 struct remote_state
*rs
= get_remote_state ();
8598 char *endp
= rs
->buf
+ get_remote_packet_size ();
8599 enum packet_result result
;
8601 strcpy (p
, "qGetTIBAddr:");
8603 p
= write_ptid (p
, endp
, ptid
);
8607 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8608 result
= packet_ok (rs
->buf
,
8609 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
8610 if (result
== PACKET_OK
)
8614 unpack_varlen_hex (rs
->buf
, &result
);
8616 *addr
= (CORE_ADDR
) result
;
8619 else if (result
== PACKET_UNKNOWN
)
8620 error (_("Remote target doesn't support qGetTIBAddr packet"));
8622 error (_("Remote target failed to process qGetTIBAddr request"));
8625 error (_("qGetTIBAddr not supported or disabled on this target"));
8630 /* Support for inferring a target description based on the current
8631 architecture and the size of a 'g' packet. While the 'g' packet
8632 can have any size (since optional registers can be left off the
8633 end), some sizes are easily recognizable given knowledge of the
8634 approximate architecture. */
8636 struct remote_g_packet_guess
8639 const struct target_desc
*tdesc
;
8641 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
8642 DEF_VEC_O(remote_g_packet_guess_s
);
8644 struct remote_g_packet_data
8646 VEC(remote_g_packet_guess_s
) *guesses
;
8649 static struct gdbarch_data
*remote_g_packet_data_handle
;
8652 remote_g_packet_data_init (struct obstack
*obstack
)
8654 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
8658 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
8659 const struct target_desc
*tdesc
)
8661 struct remote_g_packet_data
*data
8662 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
8663 struct remote_g_packet_guess new_guess
, *guess
;
8666 gdb_assert (tdesc
!= NULL
);
8669 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8671 if (guess
->bytes
== bytes
)
8672 internal_error (__FILE__
, __LINE__
,
8673 "Duplicate g packet description added for size %d",
8676 new_guess
.bytes
= bytes
;
8677 new_guess
.tdesc
= tdesc
;
8678 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
8681 /* Return 1 if remote_read_description would do anything on this target
8682 and architecture, 0 otherwise. */
8685 remote_read_description_p (struct target_ops
*target
)
8687 struct remote_g_packet_data
*data
8688 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8690 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8696 static const struct target_desc
*
8697 remote_read_description (struct target_ops
*target
)
8699 struct remote_g_packet_data
*data
8700 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8702 /* Do not try this during initial connection, when we do not know
8703 whether there is a running but stopped thread. */
8704 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
8707 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8709 struct remote_g_packet_guess
*guess
;
8711 int bytes
= send_g_packet ();
8714 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8716 if (guess
->bytes
== bytes
)
8717 return guess
->tdesc
;
8719 /* We discard the g packet. A minor optimization would be to
8720 hold on to it, and fill the register cache once we have selected
8721 an architecture, but it's too tricky to do safely. */
8727 /* Remote file transfer support. This is host-initiated I/O, not
8728 target-initiated; for target-initiated, see remote-fileio.c. */
8730 /* If *LEFT is at least the length of STRING, copy STRING to
8731 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8732 decrease *LEFT. Otherwise raise an error. */
8735 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
8737 int len
= strlen (string
);
8740 error (_("Packet too long for target."));
8742 memcpy (*buffer
, string
, len
);
8746 /* NUL-terminate the buffer as a convenience, if there is
8752 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
8753 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8754 decrease *LEFT. Otherwise raise an error. */
8757 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
8760 if (2 * len
> *left
)
8761 error (_("Packet too long for target."));
8763 bin2hex (bytes
, *buffer
, len
);
8767 /* NUL-terminate the buffer as a convenience, if there is
8773 /* If *LEFT is large enough, convert VALUE to hex and add it to
8774 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8775 decrease *LEFT. Otherwise raise an error. */
8778 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
8780 int len
= hexnumlen (value
);
8783 error (_("Packet too long for target."));
8785 hexnumstr (*buffer
, value
);
8789 /* NUL-terminate the buffer as a convenience, if there is
8795 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8796 value, *REMOTE_ERRNO to the remote error number or zero if none
8797 was included, and *ATTACHMENT to point to the start of the annex
8798 if any. The length of the packet isn't needed here; there may
8799 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8801 Return 0 if the packet could be parsed, -1 if it could not. If
8802 -1 is returned, the other variables may not be initialized. */
8805 remote_hostio_parse_result (char *buffer
, int *retcode
,
8806 int *remote_errno
, char **attachment
)
8813 if (buffer
[0] != 'F')
8817 *retcode
= strtol (&buffer
[1], &p
, 16);
8818 if (errno
!= 0 || p
== &buffer
[1])
8821 /* Check for ",errno". */
8825 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8826 if (errno
!= 0 || p
+ 1 == p2
)
8831 /* Check for ";attachment". If there is no attachment, the
8832 packet should end here. */
8835 *attachment
= p
+ 1;
8838 else if (*p
== '\0')
8844 /* Send a prepared I/O packet to the target and read its response.
8845 The prepared packet is in the global RS->BUF before this function
8846 is called, and the answer is there when we return.
8848 COMMAND_BYTES is the length of the request to send, which may include
8849 binary data. WHICH_PACKET is the packet configuration to check
8850 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8851 is set to the error number and -1 is returned. Otherwise the value
8852 returned by the function is returned.
8854 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8855 attachment is expected; an error will be reported if there's a
8856 mismatch. If one is found, *ATTACHMENT will be set to point into
8857 the packet buffer and *ATTACHMENT_LEN will be set to the
8858 attachment's length. */
8861 remote_hostio_send_command (int command_bytes
, int which_packet
,
8862 int *remote_errno
, char **attachment
,
8863 int *attachment_len
)
8865 struct remote_state
*rs
= get_remote_state ();
8866 int ret
, bytes_read
;
8867 char *attachment_tmp
;
8870 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
8872 *remote_errno
= FILEIO_ENOSYS
;
8876 putpkt_binary (rs
->buf
, command_bytes
);
8877 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8879 /* If it timed out, something is wrong. Don't try to parse the
8883 *remote_errno
= FILEIO_EINVAL
;
8887 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
8890 *remote_errno
= FILEIO_EINVAL
;
8892 case PACKET_UNKNOWN
:
8893 *remote_errno
= FILEIO_ENOSYS
;
8899 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
8902 *remote_errno
= FILEIO_EINVAL
;
8906 /* Make sure we saw an attachment if and only if we expected one. */
8907 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
8908 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
8910 *remote_errno
= FILEIO_EINVAL
;
8914 /* If an attachment was found, it must point into the packet buffer;
8915 work out how many bytes there were. */
8916 if (attachment_tmp
!= NULL
)
8918 *attachment
= attachment_tmp
;
8919 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
8925 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
8926 remote file descriptor, or -1 if an error occurs (and set
8930 remote_hostio_open (const char *filename
, int flags
, int mode
,
8933 struct remote_state
*rs
= get_remote_state ();
8935 int left
= get_remote_packet_size () - 1;
8937 remote_buffer_add_string (&p
, &left
, "vFile:open:");
8939 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8941 remote_buffer_add_string (&p
, &left
, ",");
8943 remote_buffer_add_int (&p
, &left
, flags
);
8944 remote_buffer_add_string (&p
, &left
, ",");
8946 remote_buffer_add_int (&p
, &left
, mode
);
8948 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
8949 remote_errno
, NULL
, NULL
);
8952 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
8953 Return the number of bytes written, or -1 if an error occurs (and
8954 set *REMOTE_ERRNO). */
8957 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
8958 ULONGEST offset
, int *remote_errno
)
8960 struct remote_state
*rs
= get_remote_state ();
8962 int left
= get_remote_packet_size ();
8965 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
8967 remote_buffer_add_int (&p
, &left
, fd
);
8968 remote_buffer_add_string (&p
, &left
, ",");
8970 remote_buffer_add_int (&p
, &left
, offset
);
8971 remote_buffer_add_string (&p
, &left
, ",");
8973 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
8974 get_remote_packet_size () - (p
- rs
->buf
));
8976 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
8977 remote_errno
, NULL
, NULL
);
8980 /* Read up to LEN bytes FD on the remote target into READ_BUF
8981 Return the number of bytes read, or -1 if an error occurs (and
8982 set *REMOTE_ERRNO). */
8985 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
8986 ULONGEST offset
, int *remote_errno
)
8988 struct remote_state
*rs
= get_remote_state ();
8991 int left
= get_remote_packet_size ();
8992 int ret
, attachment_len
;
8995 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
8997 remote_buffer_add_int (&p
, &left
, fd
);
8998 remote_buffer_add_string (&p
, &left
, ",");
9000 remote_buffer_add_int (&p
, &left
, len
);
9001 remote_buffer_add_string (&p
, &left
, ",");
9003 remote_buffer_add_int (&p
, &left
, offset
);
9005 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9006 remote_errno
, &attachment
,
9012 read_len
= remote_unescape_input (attachment
, attachment_len
,
9014 if (read_len
!= ret
)
9015 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9020 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9021 (and set *REMOTE_ERRNO). */
9024 remote_hostio_close (int fd
, int *remote_errno
)
9026 struct remote_state
*rs
= get_remote_state ();
9028 int left
= get_remote_packet_size () - 1;
9030 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9032 remote_buffer_add_int (&p
, &left
, fd
);
9034 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9035 remote_errno
, NULL
, NULL
);
9038 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9039 occurs (and set *REMOTE_ERRNO). */
9042 remote_hostio_unlink (const char *filename
, int *remote_errno
)
9044 struct remote_state
*rs
= get_remote_state ();
9046 int left
= get_remote_packet_size () - 1;
9048 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9050 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9053 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9054 remote_errno
, NULL
, NULL
);
9058 remote_fileio_errno_to_host (int errnum
)
9082 case FILEIO_ENOTDIR
:
9102 case FILEIO_ENAMETOOLONG
:
9103 return ENAMETOOLONG
;
9109 remote_hostio_error (int errnum
)
9111 int host_error
= remote_fileio_errno_to_host (errnum
);
9113 if (host_error
== -1)
9114 error (_("Unknown remote I/O error %d"), errnum
);
9116 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9120 remote_hostio_close_cleanup (void *opaque
)
9122 int fd
= *(int *) opaque
;
9125 remote_hostio_close (fd
, &remote_errno
);
9130 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9132 const char *filename
= bfd_get_filename (abfd
);
9133 int fd
, remote_errno
;
9136 gdb_assert (remote_filename_p (filename
));
9138 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9141 errno
= remote_fileio_errno_to_host (remote_errno
);
9142 bfd_set_error (bfd_error_system_call
);
9146 stream
= xmalloc (sizeof (int));
9152 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9154 int fd
= *(int *)stream
;
9159 /* Ignore errors on close; these may happen if the remote
9160 connection was already torn down. */
9161 remote_hostio_close (fd
, &remote_errno
);
9167 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9168 file_ptr nbytes
, file_ptr offset
)
9170 int fd
= *(int *)stream
;
9172 file_ptr pos
, bytes
;
9175 while (nbytes
> pos
)
9177 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
9178 offset
+ pos
, &remote_errno
);
9180 /* Success, but no bytes, means end-of-file. */
9184 errno
= remote_fileio_errno_to_host (remote_errno
);
9185 bfd_set_error (bfd_error_system_call
);
9196 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9198 /* FIXME: We should probably implement remote_hostio_stat. */
9199 sb
->st_size
= INT_MAX
;
9204 remote_filename_p (const char *filename
)
9206 return strncmp (filename
, "remote:", 7) == 0;
9210 remote_bfd_open (const char *remote_file
, const char *target
)
9212 return bfd_openr_iovec (remote_file
, target
,
9213 remote_bfd_iovec_open
, NULL
,
9214 remote_bfd_iovec_pread
,
9215 remote_bfd_iovec_close
,
9216 remote_bfd_iovec_stat
);
9220 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
9222 struct cleanup
*back_to
, *close_cleanup
;
9223 int retcode
, fd
, remote_errno
, bytes
, io_size
;
9226 int bytes_in_buffer
;
9231 error (_("command can only be used with remote target"));
9233 file
= fopen (local_file
, "rb");
9235 perror_with_name (local_file
);
9236 back_to
= make_cleanup_fclose (file
);
9238 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
9240 0700, &remote_errno
);
9242 remote_hostio_error (remote_errno
);
9244 /* Send up to this many bytes at once. They won't all fit in the
9245 remote packet limit, so we'll transfer slightly fewer. */
9246 io_size
= get_remote_packet_size ();
9247 buffer
= xmalloc (io_size
);
9248 make_cleanup (xfree
, buffer
);
9250 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9252 bytes_in_buffer
= 0;
9255 while (bytes_in_buffer
|| !saw_eof
)
9259 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
9264 error (_("Error reading %s."), local_file
);
9267 /* EOF. Unless there is something still in the
9268 buffer from the last iteration, we are done. */
9270 if (bytes_in_buffer
== 0)
9278 bytes
+= bytes_in_buffer
;
9279 bytes_in_buffer
= 0;
9281 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
9284 remote_hostio_error (remote_errno
);
9285 else if (retcode
== 0)
9286 error (_("Remote write of %d bytes returned 0!"), bytes
);
9287 else if (retcode
< bytes
)
9289 /* Short write. Save the rest of the read data for the next
9291 bytes_in_buffer
= bytes
- retcode
;
9292 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
9298 discard_cleanups (close_cleanup
);
9299 if (remote_hostio_close (fd
, &remote_errno
))
9300 remote_hostio_error (remote_errno
);
9303 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
9304 do_cleanups (back_to
);
9308 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
9310 struct cleanup
*back_to
, *close_cleanup
;
9311 int fd
, remote_errno
, bytes
, io_size
;
9317 error (_("command can only be used with remote target"));
9319 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
9321 remote_hostio_error (remote_errno
);
9323 file
= fopen (local_file
, "wb");
9325 perror_with_name (local_file
);
9326 back_to
= make_cleanup_fclose (file
);
9328 /* Send up to this many bytes at once. They won't all fit in the
9329 remote packet limit, so we'll transfer slightly fewer. */
9330 io_size
= get_remote_packet_size ();
9331 buffer
= xmalloc (io_size
);
9332 make_cleanup (xfree
, buffer
);
9334 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9339 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
9341 /* Success, but no bytes, means end-of-file. */
9344 remote_hostio_error (remote_errno
);
9348 bytes
= fwrite (buffer
, 1, bytes
, file
);
9350 perror_with_name (local_file
);
9353 discard_cleanups (close_cleanup
);
9354 if (remote_hostio_close (fd
, &remote_errno
))
9355 remote_hostio_error (remote_errno
);
9358 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
9359 do_cleanups (back_to
);
9363 remote_file_delete (const char *remote_file
, int from_tty
)
9365 int retcode
, remote_errno
;
9368 error (_("command can only be used with remote target"));
9370 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
9372 remote_hostio_error (remote_errno
);
9375 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
9379 remote_put_command (char *args
, int from_tty
)
9381 struct cleanup
*back_to
;
9385 error_no_arg (_("file to put"));
9387 argv
= gdb_buildargv (args
);
9388 back_to
= make_cleanup_freeargv (argv
);
9389 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9390 error (_("Invalid parameters to remote put"));
9392 remote_file_put (argv
[0], argv
[1], from_tty
);
9394 do_cleanups (back_to
);
9398 remote_get_command (char *args
, int from_tty
)
9400 struct cleanup
*back_to
;
9404 error_no_arg (_("file to get"));
9406 argv
= gdb_buildargv (args
);
9407 back_to
= make_cleanup_freeargv (argv
);
9408 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9409 error (_("Invalid parameters to remote get"));
9411 remote_file_get (argv
[0], argv
[1], from_tty
);
9413 do_cleanups (back_to
);
9417 remote_delete_command (char *args
, int from_tty
)
9419 struct cleanup
*back_to
;
9423 error_no_arg (_("file to delete"));
9425 argv
= gdb_buildargv (args
);
9426 back_to
= make_cleanup_freeargv (argv
);
9427 if (argv
[0] == NULL
|| argv
[1] != NULL
)
9428 error (_("Invalid parameters to remote delete"));
9430 remote_file_delete (argv
[0], from_tty
);
9432 do_cleanups (back_to
);
9436 remote_command (char *args
, int from_tty
)
9438 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
9442 remote_can_execute_reverse (void)
9444 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
9445 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
9452 remote_supports_non_stop (void)
9458 remote_supports_multi_process (void)
9460 struct remote_state
*rs
= get_remote_state ();
9462 return remote_multi_process_p (rs
);
9466 remote_supports_cond_tracepoints (void)
9468 struct remote_state
*rs
= get_remote_state ();
9470 return rs
->cond_tracepoints
;
9474 remote_supports_fast_tracepoints (void)
9476 struct remote_state
*rs
= get_remote_state ();
9478 return rs
->fast_tracepoints
;
9482 remote_trace_init (void)
9485 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9486 if (strcmp (target_buf
, "OK") != 0)
9487 error (_("Target does not support this command."));
9490 static void free_actions_list (char **actions_list
);
9491 static void free_actions_list_cleanup_wrapper (void *);
9493 free_actions_list_cleanup_wrapper (void *al
)
9495 free_actions_list (al
);
9499 free_actions_list (char **actions_list
)
9503 if (actions_list
== 0)
9506 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
9507 xfree (actions_list
[ndx
]);
9509 xfree (actions_list
);
9512 /* Recursive routine to walk through command list including loops, and
9513 download packets for each command. */
9516 remote_download_command_source (int num
, ULONGEST addr
,
9517 struct command_line
*cmds
)
9519 struct remote_state
*rs
= get_remote_state ();
9520 struct command_line
*cmd
;
9522 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
9524 QUIT
; /* allow user to bail out with ^C */
9525 strcpy (rs
->buf
, "QTDPsrc:");
9526 encode_source_string (num
, addr
, "cmd", cmd
->line
,
9527 rs
->buf
+ strlen (rs
->buf
),
9528 rs
->buf_size
- strlen (rs
->buf
));
9530 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9531 if (strcmp (target_buf
, "OK"))
9532 warning (_("Target does not support source download."));
9534 if (cmd
->control_type
== while_control
9535 || cmd
->control_type
== while_stepping_control
)
9537 remote_download_command_source (num
, addr
, *cmd
->body_list
);
9539 QUIT
; /* allow user to bail out with ^C */
9540 strcpy (rs
->buf
, "QTDPsrc:");
9541 encode_source_string (num
, addr
, "cmd", "end",
9542 rs
->buf
+ strlen (rs
->buf
),
9543 rs
->buf_size
- strlen (rs
->buf
));
9545 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9546 if (strcmp (target_buf
, "OK"))
9547 warning (_("Target does not support source download."));
9553 remote_download_tracepoint (struct breakpoint
*t
)
9555 struct bp_location
*loc
;
9560 char **stepping_actions
;
9562 struct cleanup
*old_chain
= NULL
;
9563 struct agent_expr
*aexpr
;
9564 struct cleanup
*aexpr_chain
= NULL
;
9567 /* Iterate over all the tracepoint locations. It's up to the target to
9568 notice multiple tracepoint packets with the same number but different
9569 addresses, and treat them as multiple locations. */
9570 for (loc
= t
->loc
; loc
; loc
= loc
->next
)
9572 encode_actions (t
, loc
, &tdp_actions
, &stepping_actions
);
9573 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
9575 (void) make_cleanup (free_actions_list_cleanup_wrapper
, stepping_actions
);
9577 tpaddr
= loc
->address
;
9578 sprintf_vma (addrbuf
, tpaddr
);
9579 sprintf (buf
, "QTDP:%x:%s:%c:%lx:%x", t
->number
,
9580 addrbuf
, /* address */
9581 (t
->enable_state
== bp_enabled
? 'E' : 'D'),
9582 t
->step_count
, t
->pass_count
);
9583 /* Fast tracepoints are mostly handled by the target, but we can
9584 tell the target how big of an instruction block should be moved
9586 if (t
->type
== bp_fast_tracepoint
)
9588 /* Only test for support at download time; we may not know
9589 target capabilities at definition time. */
9590 if (remote_supports_fast_tracepoints ())
9594 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch
,
9595 tpaddr
, &isize
, NULL
))
9596 sprintf (buf
+ strlen (buf
), ":F%x", isize
);
9598 /* If it passed validation at definition but fails now,
9599 something is very wrong. */
9600 internal_error (__FILE__
, __LINE__
,
9601 "Fast tracepoint not valid during download");
9604 /* Fast tracepoints are functionally identical to regular
9605 tracepoints, so don't take lack of support as a reason to
9606 give up on the trace run. */
9607 warning (_("Target does not support fast tracepoints, downloading %d as regular tracepoint"), t
->number
);
9609 /* If the tracepoint has a conditional, make it into an agent
9610 expression and append to the definition. */
9613 /* Only test support at download time, we may not know target
9614 capabilities at definition time. */
9615 if (remote_supports_cond_tracepoints ())
9617 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
9618 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
9619 sprintf (buf
+ strlen (buf
), ":X%x,", aexpr
->len
);
9620 pkt
= buf
+ strlen (buf
);
9621 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
9622 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
9624 do_cleanups (aexpr_chain
);
9627 warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t
->number
);
9630 if (t
->commands
|| *default_collect
)
9633 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9634 if (strcmp (target_buf
, "OK"))
9635 error (_("Target does not support tracepoints."));
9637 /* do_single_steps (t); */
9640 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
9642 QUIT
; /* allow user to bail out with ^C */
9643 sprintf (buf
, "QTDP:-%x:%s:%s%c",
9644 t
->number
, addrbuf
, /* address */
9646 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
9649 remote_get_noisy_reply (&target_buf
,
9651 if (strcmp (target_buf
, "OK"))
9652 error (_("Error on target while setting tracepoints."));
9655 if (stepping_actions
)
9657 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
9659 QUIT
; /* allow user to bail out with ^C */
9660 sprintf (buf
, "QTDP:-%x:%s:%s%s%s",
9661 t
->number
, addrbuf
, /* address */
9662 ((ndx
== 0) ? "S" : ""),
9663 stepping_actions
[ndx
],
9664 (stepping_actions
[ndx
+ 1] ? "-" : ""));
9666 remote_get_noisy_reply (&target_buf
,
9668 if (strcmp (target_buf
, "OK"))
9669 error (_("Error on target while setting tracepoints."));
9673 if (remote_protocol_packets
[PACKET_TracepointSource
].support
== PACKET_ENABLE
)
9677 strcpy (buf
, "QTDPsrc:");
9678 encode_source_string (t
->number
, loc
->address
,
9679 "at", t
->addr_string
, buf
+ strlen (buf
),
9680 2048 - strlen (buf
));
9683 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9684 if (strcmp (target_buf
, "OK"))
9685 warning (_("Target does not support source download."));
9689 strcpy (buf
, "QTDPsrc:");
9690 encode_source_string (t
->number
, loc
->address
,
9691 "cond", t
->cond_string
, buf
+ strlen (buf
),
9692 2048 - strlen (buf
));
9694 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9695 if (strcmp (target_buf
, "OK"))
9696 warning (_("Target does not support source download."));
9698 remote_download_command_source (t
->number
, loc
->address
,
9699 breakpoint_commands (t
));
9702 do_cleanups (old_chain
);
9707 remote_download_trace_state_variable (struct trace_state_variable
*tsv
)
9709 struct remote_state
*rs
= get_remote_state ();
9712 sprintf (rs
->buf
, "QTDV:%x:%s:%x:",
9713 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8), tsv
->builtin
);
9714 p
= rs
->buf
+ strlen (rs
->buf
);
9715 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
9716 error (_("Trace state variable name too long for tsv definition packet"));
9717 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, 0);
9720 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9721 if (*target_buf
== '\0')
9722 error (_("Target does not support this command."));
9723 if (strcmp (target_buf
, "OK") != 0)
9724 error (_("Error on target while downloading trace state variable."));
9728 remote_trace_set_readonly_regions (void)
9736 return; /* No information to give. */
9738 strcpy (target_buf
, "QTro");
9739 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
9741 char tmp1
[40], tmp2
[40];
9743 if ((s
->flags
& SEC_LOAD
) == 0 ||
9744 /* (s->flags & SEC_CODE) == 0 || */
9745 (s
->flags
& SEC_READONLY
) == 0)
9750 size
= bfd_get_section_size (s
);
9751 sprintf_vma (tmp1
, lma
);
9752 sprintf_vma (tmp2
, lma
+ size
);
9753 sprintf (target_buf
+ strlen (target_buf
),
9754 ":%s,%s", tmp1
, tmp2
);
9758 putpkt (target_buf
);
9759 getpkt (&target_buf
, &target_buf_size
, 0);
9764 remote_trace_start (void)
9767 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9768 if (*target_buf
== '\0')
9769 error (_("Target does not support this command."));
9770 if (strcmp (target_buf
, "OK") != 0)
9771 error (_("Bogus reply from target: %s"), target_buf
);
9775 remote_get_trace_status (struct trace_status
*ts
)
9778 /* FIXME we need to get register block size some other way */
9779 extern int trace_regblock_size
;
9781 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
9783 putpkt ("qTStatus");
9784 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9786 /* If the remote target doesn't do tracing, flag it. */
9790 /* We're working with a live target. */
9793 /* Set some defaults. */
9794 ts
->running_known
= 0;
9795 ts
->stop_reason
= trace_stop_reason_unknown
;
9796 ts
->traceframe_count
= -1;
9797 ts
->buffer_free
= 0;
9800 error (_("Bogus trace status reply from target: %s"), target_buf
);
9802 parse_trace_status (p
, ts
);
9808 remote_trace_stop (void)
9811 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9812 if (*target_buf
== '\0')
9813 error (_("Target does not support this command."));
9814 if (strcmp (target_buf
, "OK") != 0)
9815 error (_("Bogus reply from target: %s"), target_buf
);
9819 remote_trace_find (enum trace_find_type type
, int num
,
9820 ULONGEST addr1
, ULONGEST addr2
,
9823 struct remote_state
*rs
= get_remote_state ();
9825 int target_frameno
= -1, target_tracept
= -1;
9828 strcpy (p
, "QTFrame:");
9829 p
= strchr (p
, '\0');
9833 sprintf (p
, "%x", num
);
9836 sprintf (p
, "pc:%s", phex_nz (addr1
, 0));
9839 sprintf (p
, "tdp:%x", num
);
9842 sprintf (p
, "range:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
9845 sprintf (p
, "outside:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
9848 error ("Unknown trace find type %d", type
);
9852 reply
= remote_get_noisy_reply (&(rs
->buf
), &sizeof_pkt
);
9854 error (_("Target does not support this command."));
9856 while (reply
&& *reply
)
9861 target_frameno
= (int) strtol (p
, &reply
, 16);
9863 error (_("Unable to parse trace frame number"));
9864 if (target_frameno
== -1)
9869 target_tracept
= (int) strtol (p
, &reply
, 16);
9871 error (_("Unable to parse tracepoint number"));
9873 case 'O': /* "OK"? */
9874 if (reply
[1] == 'K' && reply
[2] == '\0')
9877 error (_("Bogus reply from target: %s"), reply
);
9880 error (_("Bogus reply from target: %s"), reply
);
9883 *tpp
= target_tracept
;
9884 return target_frameno
;
9888 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
9890 struct remote_state
*rs
= get_remote_state ();
9894 sprintf (rs
->buf
, "qTV:%x", tsvnum
);
9896 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9897 if (reply
&& *reply
)
9901 unpack_varlen_hex (reply
+ 1, &uval
);
9902 *val
= (LONGEST
) uval
;
9910 remote_save_trace_data (const char *filename
)
9912 struct remote_state
*rs
= get_remote_state ();
9916 strcpy (p
, "QTSave:");
9918 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
9919 error (_("Remote file name too long for trace save packet"));
9920 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, 0);
9923 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9925 error (_("Target does not support this command."));
9926 if (strcmp (reply
, "OK") != 0)
9927 error (_("Bogus reply from target: %s"), reply
);
9931 /* This is basically a memory transfer, but needs to be its own packet
9932 because we don't know how the target actually organizes its trace
9933 memory, plus we want to be able to ask for as much as possible, but
9934 not be unhappy if we don't get as much as we ask for. */
9937 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
9939 struct remote_state
*rs
= get_remote_state ();
9945 strcpy (p
, "qTBuffer:");
9947 p
+= hexnumstr (p
, offset
);
9949 p
+= hexnumstr (p
, len
);
9953 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9954 if (reply
&& *reply
)
9956 /* 'l' by itself means we're at the end of the buffer and
9957 there is nothing more to get. */
9961 /* Convert the reply into binary. Limit the number of bytes to
9962 convert according to our passed-in buffer size, rather than
9963 what was returned in the packet; if the target is
9964 unexpectedly generous and gives us a bigger reply than we
9965 asked for, we don't want to crash. */
9966 rslt
= hex2bin (target_buf
, buf
, len
);
9970 /* Something went wrong, flag as an error. */
9975 remote_set_disconnected_tracing (int val
)
9977 struct remote_state
*rs
= get_remote_state ();
9979 if (rs
->disconnected_tracing
)
9983 sprintf (rs
->buf
, "QTDisconnected:%x", val
);
9985 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9987 error (_("Target does not support this command."));
9988 if (strcmp (reply
, "OK") != 0)
9989 error (_("Bogus reply from target: %s"), reply
);
9992 warning (_("Target does not support disconnected tracing."));
9996 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
9998 struct thread_info
*info
= find_thread_ptid (ptid
);
10000 if (info
&& info
->private)
10001 return info
->private->core
;
10006 remote_set_circular_trace_buffer (int val
)
10008 struct remote_state
*rs
= get_remote_state ();
10011 sprintf (rs
->buf
, "QTBuffer:circular:%x", val
);
10013 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10014 if (*reply
== '\0')
10015 error (_("Target does not support this command."));
10016 if (strcmp (reply
, "OK") != 0)
10017 error (_("Bogus reply from target: %s"), reply
);
10021 init_remote_ops (void)
10023 remote_ops
.to_shortname
= "remote";
10024 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
10025 remote_ops
.to_doc
=
10026 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10027 Specify the serial device it is connected to\n\
10028 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10029 remote_ops
.to_open
= remote_open
;
10030 remote_ops
.to_close
= remote_close
;
10031 remote_ops
.to_detach
= remote_detach
;
10032 remote_ops
.to_disconnect
= remote_disconnect
;
10033 remote_ops
.to_resume
= remote_resume
;
10034 remote_ops
.to_wait
= remote_wait
;
10035 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
10036 remote_ops
.to_store_registers
= remote_store_registers
;
10037 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
10038 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
10039 remote_ops
.to_files_info
= remote_files_info
;
10040 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
10041 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
10042 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
10043 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
10044 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
10045 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
10046 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
10047 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
10048 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
10049 remote_ops
.to_kill
= remote_kill
;
10050 remote_ops
.to_load
= generic_load
;
10051 remote_ops
.to_mourn_inferior
= remote_mourn
;
10052 remote_ops
.to_notice_signals
= remote_notice_signals
;
10053 remote_ops
.to_thread_alive
= remote_thread_alive
;
10054 remote_ops
.to_find_new_threads
= remote_threads_info
;
10055 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
10056 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
10057 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
10058 remote_ops
.to_stop
= remote_stop
;
10059 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
10060 remote_ops
.to_rcmd
= remote_rcmd
;
10061 remote_ops
.to_log_command
= serial_log_command
;
10062 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
10063 remote_ops
.to_stratum
= process_stratum
;
10064 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
10065 remote_ops
.to_has_memory
= default_child_has_memory
;
10066 remote_ops
.to_has_stack
= default_child_has_stack
;
10067 remote_ops
.to_has_registers
= default_child_has_registers
;
10068 remote_ops
.to_has_execution
= default_child_has_execution
;
10069 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
10070 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
10071 remote_ops
.to_magic
= OPS_MAGIC
;
10072 remote_ops
.to_memory_map
= remote_memory_map
;
10073 remote_ops
.to_flash_erase
= remote_flash_erase
;
10074 remote_ops
.to_flash_done
= remote_flash_done
;
10075 remote_ops
.to_read_description
= remote_read_description
;
10076 remote_ops
.to_search_memory
= remote_search_memory
;
10077 remote_ops
.to_can_async_p
= remote_can_async_p
;
10078 remote_ops
.to_is_async_p
= remote_is_async_p
;
10079 remote_ops
.to_async
= remote_async
;
10080 remote_ops
.to_async_mask
= remote_async_mask
;
10081 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
10082 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
10083 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
10084 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
10085 remote_ops
.to_trace_init
= remote_trace_init
;
10086 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
10087 remote_ops
.to_download_trace_state_variable
= remote_download_trace_state_variable
;
10088 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
10089 remote_ops
.to_trace_start
= remote_trace_start
;
10090 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
10091 remote_ops
.to_trace_stop
= remote_trace_stop
;
10092 remote_ops
.to_trace_find
= remote_trace_find
;
10093 remote_ops
.to_get_trace_state_variable_value
= remote_get_trace_state_variable_value
;
10094 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
10095 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
10096 remote_ops
.to_upload_trace_state_variables
= remote_upload_trace_state_variables
;
10097 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
10098 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
10099 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
10100 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
10101 remote_ops
.to_verify_memory
= remote_verify_memory
;
10102 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
10103 remote_ops
.to_set_permissions
= remote_set_permissions
;
10106 /* Set up the extended remote vector by making a copy of the standard
10107 remote vector and adding to it. */
10110 init_extended_remote_ops (void)
10112 extended_remote_ops
= remote_ops
;
10114 extended_remote_ops
.to_shortname
= "extended-remote";
10115 extended_remote_ops
.to_longname
=
10116 "Extended remote serial target in gdb-specific protocol";
10117 extended_remote_ops
.to_doc
=
10118 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10119 Specify the serial device it is connected to (e.g. /dev/ttya).";
10120 extended_remote_ops
.to_open
= extended_remote_open
;
10121 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
10122 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
10123 extended_remote_ops
.to_detach
= extended_remote_detach
;
10124 extended_remote_ops
.to_attach
= extended_remote_attach
;
10125 extended_remote_ops
.to_kill
= extended_remote_kill
;
10129 remote_can_async_p (void)
10131 if (!target_async_permitted
)
10132 /* We only enable async when the user specifically asks for it. */
10135 /* We're async whenever the serial device is. */
10136 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
10140 remote_is_async_p (void)
10142 if (!target_async_permitted
)
10143 /* We only enable async when the user specifically asks for it. */
10146 /* We're async whenever the serial device is. */
10147 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
10150 /* Pass the SERIAL event on and up to the client. One day this code
10151 will be able to delay notifying the client of an event until the
10152 point where an entire packet has been received. */
10154 static void (*async_client_callback
) (enum inferior_event_type event_type
,
10156 static void *async_client_context
;
10157 static serial_event_ftype remote_async_serial_handler
;
10160 remote_async_serial_handler (struct serial
*scb
, void *context
)
10162 /* Don't propogate error information up to the client. Instead let
10163 the client find out about the error by querying the target. */
10164 async_client_callback (INF_REG_EVENT
, async_client_context
);
10168 remote_async_inferior_event_handler (gdb_client_data data
)
10170 inferior_event_handler (INF_REG_EVENT
, NULL
);
10174 remote_async_get_pending_events_handler (gdb_client_data data
)
10176 remote_get_pending_stop_replies ();
10180 remote_async (void (*callback
) (enum inferior_event_type event_type
,
10181 void *context
), void *context
)
10183 if (remote_async_mask_value
== 0)
10184 internal_error (__FILE__
, __LINE__
,
10185 _("Calling remote_async when async is masked"));
10187 if (callback
!= NULL
)
10189 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
10190 async_client_callback
= callback
;
10191 async_client_context
= context
;
10194 serial_async (remote_desc
, NULL
, NULL
);
10198 remote_async_mask (int new_mask
)
10200 int curr_mask
= remote_async_mask_value
;
10202 remote_async_mask_value
= new_mask
;
10207 set_remote_cmd (char *args
, int from_tty
)
10209 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
10213 show_remote_cmd (char *args
, int from_tty
)
10215 /* We can't just use cmd_show_list here, because we want to skip
10216 the redundant "show remote Z-packet" and the legacy aliases. */
10217 struct cleanup
*showlist_chain
;
10218 struct cmd_list_element
*list
= remote_show_cmdlist
;
10220 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
10221 for (; list
!= NULL
; list
= list
->next
)
10222 if (strcmp (list
->name
, "Z-packet") == 0)
10224 else if (list
->type
== not_set_cmd
)
10225 /* Alias commands are exactly like the original, except they
10226 don't have the normal type. */
10230 struct cleanup
*option_chain
10231 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
10233 ui_out_field_string (uiout
, "name", list
->name
);
10234 ui_out_text (uiout
, ": ");
10235 if (list
->type
== show_cmd
)
10236 do_setshow_command ((char *) NULL
, from_tty
, list
);
10238 cmd_func (list
, NULL
, from_tty
);
10239 /* Close the tuple. */
10240 do_cleanups (option_chain
);
10243 /* Close the tuple. */
10244 do_cleanups (showlist_chain
);
10248 /* Function to be called whenever a new objfile (shlib) is detected. */
10250 remote_new_objfile (struct objfile
*objfile
)
10252 if (remote_desc
!= 0) /* Have a remote connection. */
10253 remote_check_symbols (objfile
);
10256 /* Pull all the tracepoints defined on the target and create local
10257 data structures representing them. We don't want to create real
10258 tracepoints yet, we don't want to mess up the user's existing
10262 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
10264 struct remote_state
*rs
= get_remote_state ();
10267 /* Ask for a first packet of tracepoint definition. */
10269 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10271 while (*p
&& *p
!= 'l')
10273 parse_tracepoint_definition (p
, utpp
);
10274 /* Ask for another packet of tracepoint definition. */
10276 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10283 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
10285 struct remote_state
*rs
= get_remote_state ();
10288 /* Ask for a first packet of variable definition. */
10290 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10292 while (*p
&& *p
!= 'l')
10294 parse_tsv_definition (p
, utsvp
);
10295 /* Ask for another packet of variable definition. */
10297 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10304 _initialize_remote (void)
10306 struct remote_state
*rs
;
10307 struct cmd_list_element
*cmd
;
10310 /* architecture specific data */
10311 remote_gdbarch_data_handle
=
10312 gdbarch_data_register_post_init (init_remote_state
);
10313 remote_g_packet_data_handle
=
10314 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
10316 /* Initialize the per-target state. At the moment there is only one
10317 of these, not one per target. Only one target is active at a
10318 time. The default buffer size is unimportant; it will be expanded
10319 whenever a larger buffer is needed. */
10320 rs
= get_remote_state_raw ();
10321 rs
->buf_size
= 400;
10322 rs
->buf
= xmalloc (rs
->buf_size
);
10324 init_remote_ops ();
10325 add_target (&remote_ops
);
10327 init_extended_remote_ops ();
10328 add_target (&extended_remote_ops
);
10330 /* Hook into new objfile notification. */
10331 observer_attach_new_objfile (remote_new_objfile
);
10333 /* Set up signal handlers. */
10334 sigint_remote_token
=
10335 create_async_signal_handler (async_remote_interrupt
, NULL
);
10336 sigint_remote_twice_token
=
10337 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
10340 init_remote_threadtests ();
10343 /* set/show remote ... */
10345 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
10346 Remote protocol specific variables\n\
10347 Configure various remote-protocol specific variables such as\n\
10348 the packets being used"),
10349 &remote_set_cmdlist
, "set remote ",
10350 0 /* allow-unknown */, &setlist
);
10351 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
10352 Remote protocol specific variables\n\
10353 Configure various remote-protocol specific variables such as\n\
10354 the packets being used"),
10355 &remote_show_cmdlist
, "show remote ",
10356 0 /* allow-unknown */, &showlist
);
10358 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
10359 Compare section data on target to the exec file.\n\
10360 Argument is a single section name (default: all loaded sections)."),
10363 add_cmd ("packet", class_maintenance
, packet_command
, _("\
10364 Send an arbitrary packet to a remote target.\n\
10365 maintenance packet TEXT\n\
10366 If GDB is talking to an inferior via the GDB serial protocol, then\n\
10367 this command sends the string TEXT to the inferior, and displays the\n\
10368 response packet. GDB supplies the initial `$' character, and the\n\
10369 terminating `#' character and checksum."),
10372 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
10373 Set whether to send break if interrupted."), _("\
10374 Show whether to send break if interrupted."), _("\
10375 If set, a break, instead of a cntrl-c, is sent to the remote target."),
10376 set_remotebreak
, show_remotebreak
,
10377 &setlist
, &showlist
);
10378 cmd_name
= "remotebreak";
10379 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
10380 deprecate_cmd (cmd
, "set remote interrupt-sequence");
10381 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
10382 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
10383 deprecate_cmd (cmd
, "show remote interrupt-sequence");
10385 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
10386 interrupt_sequence_modes
, &interrupt_sequence_mode
, _("\
10387 Set interrupt sequence to remote target."), _("\
10388 Show interrupt sequence to remote target."), _("\
10389 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
10390 NULL
, show_interrupt_sequence
,
10391 &remote_set_cmdlist
,
10392 &remote_show_cmdlist
);
10394 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
10395 &interrupt_on_connect
, _("\
10396 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10397 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10398 If set, interrupt sequence is sent to remote target."),
10400 &remote_set_cmdlist
, &remote_show_cmdlist
);
10402 /* Install commands for configuring memory read/write packets. */
10404 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
10405 Set the maximum number of bytes per memory write packet (deprecated)."),
10407 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
10408 Show the maximum number of bytes per memory write packet (deprecated)."),
10410 add_cmd ("memory-write-packet-size", no_class
,
10411 set_memory_write_packet_size
, _("\
10412 Set the maximum number of bytes per memory-write packet.\n\
10413 Specify the number of bytes in a packet or 0 (zero) for the\n\
10414 default packet size. The actual limit is further reduced\n\
10415 dependent on the target. Specify ``fixed'' to disable the\n\
10416 further restriction and ``limit'' to enable that restriction."),
10417 &remote_set_cmdlist
);
10418 add_cmd ("memory-read-packet-size", no_class
,
10419 set_memory_read_packet_size
, _("\
10420 Set the maximum number of bytes per memory-read packet.\n\
10421 Specify the number of bytes in a packet or 0 (zero) for the\n\
10422 default packet size. The actual limit is further reduced\n\
10423 dependent on the target. Specify ``fixed'' to disable the\n\
10424 further restriction and ``limit'' to enable that restriction."),
10425 &remote_set_cmdlist
);
10426 add_cmd ("memory-write-packet-size", no_class
,
10427 show_memory_write_packet_size
,
10428 _("Show the maximum number of bytes per memory-write packet."),
10429 &remote_show_cmdlist
);
10430 add_cmd ("memory-read-packet-size", no_class
,
10431 show_memory_read_packet_size
,
10432 _("Show the maximum number of bytes per memory-read packet."),
10433 &remote_show_cmdlist
);
10435 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
10436 &remote_hw_watchpoint_limit
, _("\
10437 Set the maximum number of target hardware watchpoints."), _("\
10438 Show the maximum number of target hardware watchpoints."), _("\
10439 Specify a negative limit for unlimited."),
10440 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
10441 &remote_set_cmdlist
, &remote_show_cmdlist
);
10442 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
10443 &remote_hw_breakpoint_limit
, _("\
10444 Set the maximum number of target hardware breakpoints."), _("\
10445 Show the maximum number of target hardware breakpoints."), _("\
10446 Specify a negative limit for unlimited."),
10447 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
10448 &remote_set_cmdlist
, &remote_show_cmdlist
);
10450 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
10451 &remote_address_size
, _("\
10452 Set the maximum size of the address (in bits) in a memory packet."), _("\
10453 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
10455 NULL
, /* FIXME: i18n: */
10456 &setlist
, &showlist
);
10458 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
10459 "X", "binary-download", 1);
10461 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
10462 "vCont", "verbose-resume", 0);
10464 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
10465 "QPassSignals", "pass-signals", 0);
10467 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
10468 "qSymbol", "symbol-lookup", 0);
10470 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
10471 "P", "set-register", 1);
10473 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
10474 "p", "fetch-register", 1);
10476 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
10477 "Z0", "software-breakpoint", 0);
10479 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
10480 "Z1", "hardware-breakpoint", 0);
10482 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
10483 "Z2", "write-watchpoint", 0);
10485 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
10486 "Z3", "read-watchpoint", 0);
10488 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
10489 "Z4", "access-watchpoint", 0);
10491 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
10492 "qXfer:auxv:read", "read-aux-vector", 0);
10494 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
10495 "qXfer:features:read", "target-features", 0);
10497 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
10498 "qXfer:libraries:read", "library-info", 0);
10500 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
10501 "qXfer:memory-map:read", "memory-map", 0);
10503 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
10504 "qXfer:spu:read", "read-spu-object", 0);
10506 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
10507 "qXfer:spu:write", "write-spu-object", 0);
10509 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
10510 "qXfer:osdata:read", "osdata", 0);
10512 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
10513 "qXfer:threads:read", "threads", 0);
10515 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
10516 "qXfer:siginfo:read", "read-siginfo-object", 0);
10518 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
10519 "qXfer:siginfo:write", "write-siginfo-object", 0);
10521 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
10522 "qGetTLSAddr", "get-thread-local-storage-address",
10525 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
10526 "qGetTIBAddr", "get-thread-information-block-address",
10529 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
10530 "bc", "reverse-continue", 0);
10532 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
10533 "bs", "reverse-step", 0);
10535 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
10536 "qSupported", "supported-packets", 0);
10538 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
10539 "qSearch:memory", "search-memory", 0);
10541 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
10542 "vFile:open", "hostio-open", 0);
10544 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
10545 "vFile:pread", "hostio-pread", 0);
10547 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
10548 "vFile:pwrite", "hostio-pwrite", 0);
10550 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
10551 "vFile:close", "hostio-close", 0);
10553 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
10554 "vFile:unlink", "hostio-unlink", 0);
10556 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
10557 "vAttach", "attach", 0);
10559 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
10562 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
10563 "QStartNoAckMode", "noack", 0);
10565 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
10566 "vKill", "kill", 0);
10568 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
10569 "qAttached", "query-attached", 0);
10571 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
10572 "ConditionalTracepoints", "conditional-tracepoints", 0);
10573 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
10574 "FastTracepoints", "fast-tracepoints", 0);
10576 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
10577 "TracepointSource", "TracepointSource", 0);
10579 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
10580 "QAllow", "allow", 0);
10582 /* Keep the old ``set remote Z-packet ...'' working. Each individual
10583 Z sub-packet has its own set and show commands, but users may
10584 have sets to this variable in their .gdbinit files (or in their
10586 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
10587 &remote_Z_packet_detect
, _("\
10588 Set use of remote protocol `Z' packets"), _("\
10589 Show use of remote protocol `Z' packets "), _("\
10590 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
10592 set_remote_protocol_Z_packet_cmd
,
10593 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
10594 &remote_set_cmdlist
, &remote_show_cmdlist
);
10596 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
10597 Manipulate files on the remote system\n\
10598 Transfer files to and from the remote target system."),
10599 &remote_cmdlist
, "remote ",
10600 0 /* allow-unknown */, &cmdlist
);
10602 add_cmd ("put", class_files
, remote_put_command
,
10603 _("Copy a local file to the remote system."),
10606 add_cmd ("get", class_files
, remote_get_command
,
10607 _("Copy a remote file to the local system."),
10610 add_cmd ("delete", class_files
, remote_delete_command
,
10611 _("Delete a remote file."),
10614 remote_exec_file
= xstrdup ("");
10615 add_setshow_string_noescape_cmd ("exec-file", class_files
,
10616 &remote_exec_file
, _("\
10617 Set the remote pathname for \"run\""), _("\
10618 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
10619 &remote_set_cmdlist
, &remote_show_cmdlist
);
10621 /* Eventually initialize fileio. See fileio.c */
10622 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
10624 /* Take advantage of the fact that the LWP field is not used, to tag
10625 special ptids with it set to != 0. */
10626 magic_null_ptid
= ptid_build (42000, 1, -1);
10627 not_sent_ptid
= ptid_build (42000, 1, -2);
10628 any_thread_ptid
= ptid_build (42000, 1, 0);
10630 target_buf_size
= 2048;
10631 target_buf
= xmalloc (target_buf_size
);