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, 2011 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 reports support for static tracepoints. */
326 int static_tracepoints
;
328 /* True if the stub can continue running a trace while GDB is
330 int disconnected_tracing
;
332 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
333 responded to that. */
337 /* Private data that we'll store in (struct thread_info)->private. */
338 struct private_thread_info
345 free_private_thread_info (struct private_thread_info
*info
)
351 /* Returns true if the multi-process extensions are in effect. */
353 remote_multi_process_p (struct remote_state
*rs
)
355 return rs
->extended
&& rs
->multi_process_aware
;
358 /* This data could be associated with a target, but we do not always
359 have access to the current target when we need it, so for now it is
360 static. This will be fine for as long as only one target is in use
362 static struct remote_state remote_state
;
364 static struct remote_state
*
365 get_remote_state_raw (void)
367 return &remote_state
;
370 /* Description of the remote protocol for a given architecture. */
374 long offset
; /* Offset into G packet. */
375 long regnum
; /* GDB's internal register number. */
376 LONGEST pnum
; /* Remote protocol register number. */
377 int in_g_packet
; /* Always part of G packet. */
378 /* long size in bytes; == register_size (target_gdbarch, regnum);
380 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
384 struct remote_arch_state
386 /* Description of the remote protocol registers. */
387 long sizeof_g_packet
;
389 /* Description of the remote protocol registers indexed by REGNUM
390 (making an array gdbarch_num_regs in size). */
391 struct packet_reg
*regs
;
393 /* This is the size (in chars) of the first response to the ``g''
394 packet. It is used as a heuristic when determining the maximum
395 size of memory-read and memory-write packets. A target will
396 typically only reserve a buffer large enough to hold the ``g''
397 packet. The size does not include packet overhead (headers and
399 long actual_register_packet_size
;
401 /* This is the maximum size (in chars) of a non read/write packet.
402 It is also used as a cap on the size of read/write packets. */
403 long remote_packet_size
;
406 long sizeof_pkt
= 2000;
408 /* Utility: generate error from an incoming stub packet. */
410 trace_error (char *buf
)
413 return; /* not an error msg */
416 case '1': /* malformed packet error */
417 if (*++buf
== '0') /* general case: */
418 error (_("remote.c: error in outgoing packet."));
420 error (_("remote.c: error in outgoing packet at field #%ld."),
421 strtol (buf
, NULL
, 16));
423 error (_("trace API error 0x%s."), ++buf
);
425 error (_("Target returns error code '%s'."), buf
);
429 /* Utility: wait for reply from stub, while accepting "O" packets. */
431 remote_get_noisy_reply (char **buf_p
,
434 do /* Loop on reply from remote stub. */
438 QUIT
; /* Allow user to bail out with ^C. */
439 getpkt (buf_p
, sizeof_buf
, 0);
443 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
446 CORE_ADDR from
, to
, org_to
;
448 int adjusted_size
= 0;
449 volatile struct gdb_exception ex
;
451 p
= buf
+ strlen ("qRelocInsn:");
452 pp
= unpack_varlen_hex (p
, &ul
);
454 error (_("invalid qRelocInsn packet: %s"), buf
);
458 unpack_varlen_hex (p
, &ul
);
463 TRY_CATCH (ex
, RETURN_MASK_ALL
)
465 gdbarch_relocate_instruction (target_gdbarch
, &to
, from
);
469 adjusted_size
= to
- org_to
;
471 sprintf (buf
, "qRelocInsn:%x", adjusted_size
);
474 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
476 /* Propagate memory errors silently back to the target.
477 The stub may have limited the range of addresses we
478 can write to, for example. */
483 /* Something unexpectedly bad happened. Be verbose so
484 we can tell what, and propagate the error back to the
485 stub, so it doesn't get stuck waiting for a
487 exception_fprintf (gdb_stderr
, ex
,
488 _("warning: relocating instruction: "));
492 else if (buf
[0] == 'O' && buf
[1] != 'K')
493 remote_console_output (buf
+ 1); /* 'O' message from stub */
495 return buf
; /* Here's the actual reply. */
500 /* Handle for retreving the remote protocol data from gdbarch. */
501 static struct gdbarch_data
*remote_gdbarch_data_handle
;
503 static struct remote_arch_state
*
504 get_remote_arch_state (void)
506 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
509 /* Fetch the global remote target state. */
511 static struct remote_state
*
512 get_remote_state (void)
514 /* Make sure that the remote architecture state has been
515 initialized, because doing so might reallocate rs->buf. Any
516 function which calls getpkt also needs to be mindful of changes
517 to rs->buf, but this call limits the number of places which run
519 get_remote_arch_state ();
521 return get_remote_state_raw ();
525 compare_pnums (const void *lhs_
, const void *rhs_
)
527 const struct packet_reg
* const *lhs
= lhs_
;
528 const struct packet_reg
* const *rhs
= rhs_
;
530 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
532 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
539 init_remote_state (struct gdbarch
*gdbarch
)
541 int regnum
, num_remote_regs
, offset
;
542 struct remote_state
*rs
= get_remote_state_raw ();
543 struct remote_arch_state
*rsa
;
544 struct packet_reg
**remote_regs
;
546 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
548 /* Use the architecture to build a regnum<->pnum table, which will be
549 1:1 unless a feature set specifies otherwise. */
550 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
551 gdbarch_num_regs (gdbarch
),
553 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
555 struct packet_reg
*r
= &rsa
->regs
[regnum
];
557 if (register_size (gdbarch
, regnum
) == 0)
558 /* Do not try to fetch zero-sized (placeholder) registers. */
561 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
566 /* Define the g/G packet format as the contents of each register
567 with a remote protocol number, in order of ascending protocol
570 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
571 * sizeof (struct packet_reg
*));
572 for (num_remote_regs
= 0, regnum
= 0;
573 regnum
< gdbarch_num_regs (gdbarch
);
575 if (rsa
->regs
[regnum
].pnum
!= -1)
576 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
578 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
581 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
583 remote_regs
[regnum
]->in_g_packet
= 1;
584 remote_regs
[regnum
]->offset
= offset
;
585 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
588 /* Record the maximum possible size of the g packet - it may turn out
590 rsa
->sizeof_g_packet
= offset
;
592 /* Default maximum number of characters in a packet body. Many
593 remote stubs have a hardwired buffer size of 400 bytes
594 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
595 as the maximum packet-size to ensure that the packet and an extra
596 NUL character can always fit in the buffer. This stops GDB
597 trashing stubs that try to squeeze an extra NUL into what is
598 already a full buffer (As of 1999-12-04 that was most stubs). */
599 rsa
->remote_packet_size
= 400 - 1;
601 /* This one is filled in when a ``g'' packet is received. */
602 rsa
->actual_register_packet_size
= 0;
604 /* Should rsa->sizeof_g_packet needs more space than the
605 default, adjust the size accordingly. Remember that each byte is
606 encoded as two characters. 32 is the overhead for the packet
607 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
608 (``$NN:G...#NN'') is a better guess, the below has been padded a
610 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
611 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
613 /* Make sure that the packet buffer is plenty big enough for
614 this architecture. */
615 if (rs
->buf_size
< rsa
->remote_packet_size
)
617 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
618 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
624 /* Return the current allowed size of a remote packet. This is
625 inferred from the current architecture, and should be used to
626 limit the length of outgoing packets. */
628 get_remote_packet_size (void)
630 struct remote_state
*rs
= get_remote_state ();
631 struct remote_arch_state
*rsa
= get_remote_arch_state ();
633 if (rs
->explicit_packet_size
)
634 return rs
->explicit_packet_size
;
636 return rsa
->remote_packet_size
;
639 static struct packet_reg
*
640 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
642 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
646 struct packet_reg
*r
= &rsa
->regs
[regnum
];
648 gdb_assert (r
->regnum
== regnum
);
653 static struct packet_reg
*
654 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
658 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
660 struct packet_reg
*r
= &rsa
->regs
[i
];
668 /* FIXME: graces/2002-08-08: These variables should eventually be
669 bound to an instance of the target object (as in gdbarch-tdep()),
670 when such a thing exists. */
672 /* This is set to the data address of the access causing the target
673 to stop for a watchpoint. */
674 static CORE_ADDR remote_watch_data_address
;
676 /* This is non-zero if target stopped for a watchpoint. */
677 static int remote_stopped_by_watchpoint_p
;
679 static struct target_ops remote_ops
;
681 static struct target_ops extended_remote_ops
;
683 static int remote_async_mask_value
= 1;
685 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
686 ``forever'' still use the normal timeout mechanism. This is
687 currently used by the ASYNC code to guarentee that target reads
688 during the initial connect always time-out. Once getpkt has been
689 modified to return a timeout indication and, in turn
690 remote_wait()/wait_for_inferior() have gained a timeout parameter
692 static int wait_forever_enabled_p
= 1;
694 /* Allow the user to specify what sequence to send to the remote
695 when he requests a program interruption: Although ^C is usually
696 what remote systems expect (this is the default, here), it is
697 sometimes preferable to send a break. On other systems such
698 as the Linux kernel, a break followed by g, which is Magic SysRq g
699 is required in order to interrupt the execution. */
700 const char interrupt_sequence_control_c
[] = "Ctrl-C";
701 const char interrupt_sequence_break
[] = "BREAK";
702 const char interrupt_sequence_break_g
[] = "BREAK-g";
703 static const char *interrupt_sequence_modes
[] =
705 interrupt_sequence_control_c
,
706 interrupt_sequence_break
,
707 interrupt_sequence_break_g
,
710 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
713 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
714 struct cmd_list_element
*c
,
717 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
718 fprintf_filtered (file
,
719 _("Send the ASCII ETX character (Ctrl-c) "
720 "to the remote target to interrupt the "
721 "execution of the program.\n"));
722 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
723 fprintf_filtered (file
,
724 _("send a break signal to the remote target "
725 "to interrupt the execution of the program.\n"));
726 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
727 fprintf_filtered (file
,
728 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
729 "the remote target to interrupt the execution "
730 "of Linux kernel.\n"));
732 internal_error (__FILE__
, __LINE__
,
733 _("Invalid value for interrupt_sequence_mode: %s."),
734 interrupt_sequence_mode
);
737 /* This boolean variable specifies whether interrupt_sequence is sent
738 to the remote target when gdb connects to it.
739 This is mostly needed when you debug the Linux kernel: The Linux kernel
740 expects BREAK g which is Magic SysRq g for connecting gdb. */
741 static int interrupt_on_connect
= 0;
743 /* This variable is used to implement the "set/show remotebreak" commands.
744 Since these commands are now deprecated in favor of "set/show remote
745 interrupt-sequence", it no longer has any effect on the code. */
746 static int remote_break
;
749 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
752 interrupt_sequence_mode
= interrupt_sequence_break
;
754 interrupt_sequence_mode
= interrupt_sequence_control_c
;
758 show_remotebreak (struct ui_file
*file
, int from_tty
,
759 struct cmd_list_element
*c
,
764 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
765 remote_open knows that we don't have a file open when the program
767 static struct serial
*remote_desc
= NULL
;
769 /* This variable sets the number of bits in an address that are to be
770 sent in a memory ("M" or "m") packet. Normally, after stripping
771 leading zeros, the entire address would be sent. This variable
772 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
773 initial implementation of remote.c restricted the address sent in
774 memory packets to ``host::sizeof long'' bytes - (typically 32
775 bits). Consequently, for 64 bit targets, the upper 32 bits of an
776 address was never sent. Since fixing this bug may cause a break in
777 some remote targets this variable is principly provided to
778 facilitate backward compatibility. */
780 static int remote_address_size
;
782 /* Temporary to track who currently owns the terminal. See
783 remote_terminal_* for more details. */
785 static int remote_async_terminal_ours_p
;
787 /* The executable file to use for "run" on the remote side. */
789 static char *remote_exec_file
= "";
792 /* User configurable variables for the number of characters in a
793 memory read/write packet. MIN (rsa->remote_packet_size,
794 rsa->sizeof_g_packet) is the default. Some targets need smaller
795 values (fifo overruns, et.al.) and some users need larger values
796 (speed up transfers). The variables ``preferred_*'' (the user
797 request), ``current_*'' (what was actually set) and ``forced_*''
798 (Positive - a soft limit, negative - a hard limit). */
800 struct memory_packet_config
807 /* Compute the current size of a read/write packet. Since this makes
808 use of ``actual_register_packet_size'' the computation is dynamic. */
811 get_memory_packet_size (struct memory_packet_config
*config
)
813 struct remote_state
*rs
= get_remote_state ();
814 struct remote_arch_state
*rsa
= get_remote_arch_state ();
816 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
817 law?) that some hosts don't cope very well with large alloca()
818 calls. Eventually the alloca() code will be replaced by calls to
819 xmalloc() and make_cleanups() allowing this restriction to either
820 be lifted or removed. */
821 #ifndef MAX_REMOTE_PACKET_SIZE
822 #define MAX_REMOTE_PACKET_SIZE 16384
824 /* NOTE: 20 ensures we can write at least one byte. */
825 #ifndef MIN_REMOTE_PACKET_SIZE
826 #define MIN_REMOTE_PACKET_SIZE 20
831 if (config
->size
<= 0)
832 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
834 what_they_get
= config
->size
;
838 what_they_get
= get_remote_packet_size ();
839 /* Limit the packet to the size specified by the user. */
841 && what_they_get
> config
->size
)
842 what_they_get
= config
->size
;
844 /* Limit it to the size of the targets ``g'' response unless we have
845 permission from the stub to use a larger packet size. */
846 if (rs
->explicit_packet_size
== 0
847 && rsa
->actual_register_packet_size
> 0
848 && what_they_get
> rsa
->actual_register_packet_size
)
849 what_they_get
= rsa
->actual_register_packet_size
;
851 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
852 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
853 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
854 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
856 /* Make sure there is room in the global buffer for this packet
857 (including its trailing NUL byte). */
858 if (rs
->buf_size
< what_they_get
+ 1)
860 rs
->buf_size
= 2 * what_they_get
;
861 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
864 return what_they_get
;
867 /* Update the size of a read/write packet. If they user wants
868 something really big then do a sanity check. */
871 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
873 int fixed_p
= config
->fixed_p
;
874 long size
= config
->size
;
877 error (_("Argument required (integer, `fixed' or `limited')."));
878 else if (strcmp (args
, "hard") == 0
879 || strcmp (args
, "fixed") == 0)
881 else if (strcmp (args
, "soft") == 0
882 || strcmp (args
, "limit") == 0)
888 size
= strtoul (args
, &end
, 0);
890 error (_("Invalid %s (bad syntax)."), config
->name
);
892 /* Instead of explicitly capping the size of a packet to
893 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
894 instead allowed to set the size to something arbitrarily
896 if (size
> MAX_REMOTE_PACKET_SIZE
)
897 error (_("Invalid %s (too large)."), config
->name
);
901 if (fixed_p
&& !config
->fixed_p
)
903 if (! query (_("The target may not be able to correctly handle a %s\n"
904 "of %ld bytes. Change the packet size? "),
906 error (_("Packet size not changed."));
908 /* Update the config. */
909 config
->fixed_p
= fixed_p
;
914 show_memory_packet_size (struct memory_packet_config
*config
)
916 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
918 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
919 get_memory_packet_size (config
));
921 printf_filtered (_("Packets are limited to %ld bytes.\n"),
922 get_memory_packet_size (config
));
925 static struct memory_packet_config memory_write_packet_config
=
927 "memory-write-packet-size",
931 set_memory_write_packet_size (char *args
, int from_tty
)
933 set_memory_packet_size (args
, &memory_write_packet_config
);
937 show_memory_write_packet_size (char *args
, int from_tty
)
939 show_memory_packet_size (&memory_write_packet_config
);
943 get_memory_write_packet_size (void)
945 return get_memory_packet_size (&memory_write_packet_config
);
948 static struct memory_packet_config memory_read_packet_config
=
950 "memory-read-packet-size",
954 set_memory_read_packet_size (char *args
, int from_tty
)
956 set_memory_packet_size (args
, &memory_read_packet_config
);
960 show_memory_read_packet_size (char *args
, int from_tty
)
962 show_memory_packet_size (&memory_read_packet_config
);
966 get_memory_read_packet_size (void)
968 long size
= get_memory_packet_size (&memory_read_packet_config
);
970 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
971 extra buffer size argument before the memory read size can be
972 increased beyond this. */
973 if (size
> get_remote_packet_size ())
974 size
= get_remote_packet_size ();
979 /* Generic configuration support for packets the stub optionally
980 supports. Allows the user to specify the use of the packet as well
981 as allowing GDB to auto-detect support in the remote stub. */
985 PACKET_SUPPORT_UNKNOWN
= 0,
994 enum auto_boolean detect
;
995 enum packet_support support
;
998 /* Analyze a packet's return value and update the packet config
1009 update_packet_config (struct packet_config
*config
)
1011 switch (config
->detect
)
1013 case AUTO_BOOLEAN_TRUE
:
1014 config
->support
= PACKET_ENABLE
;
1016 case AUTO_BOOLEAN_FALSE
:
1017 config
->support
= PACKET_DISABLE
;
1019 case AUTO_BOOLEAN_AUTO
:
1020 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1026 show_packet_config_cmd (struct packet_config
*config
)
1028 char *support
= "internal-error";
1030 switch (config
->support
)
1033 support
= "enabled";
1035 case PACKET_DISABLE
:
1036 support
= "disabled";
1038 case PACKET_SUPPORT_UNKNOWN
:
1039 support
= "unknown";
1042 switch (config
->detect
)
1044 case AUTO_BOOLEAN_AUTO
:
1045 printf_filtered (_("Support for the `%s' packet "
1046 "is auto-detected, currently %s.\n"),
1047 config
->name
, support
);
1049 case AUTO_BOOLEAN_TRUE
:
1050 case AUTO_BOOLEAN_FALSE
:
1051 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1052 config
->name
, support
);
1058 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1059 const char *title
, int legacy
)
1065 config
->name
= name
;
1066 config
->title
= title
;
1067 config
->detect
= AUTO_BOOLEAN_AUTO
;
1068 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1069 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1071 show_doc
= xstrprintf ("Show current use of remote "
1072 "protocol `%s' (%s) packet",
1074 /* set/show TITLE-packet {auto,on,off} */
1075 cmd_name
= xstrprintf ("%s-packet", title
);
1076 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1077 &config
->detect
, set_doc
,
1078 show_doc
, NULL
, /* help_doc */
1079 set_remote_protocol_packet_cmd
,
1080 show_remote_protocol_packet_cmd
,
1081 &remote_set_cmdlist
, &remote_show_cmdlist
);
1082 /* The command code copies the documentation strings. */
1085 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1090 legacy_name
= xstrprintf ("%s-packet", name
);
1091 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1092 &remote_set_cmdlist
);
1093 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1094 &remote_show_cmdlist
);
1098 static enum packet_result
1099 packet_check_result (const char *buf
)
1103 /* The stub recognized the packet request. Check that the
1104 operation succeeded. */
1106 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1108 /* "Enn" - definitly an error. */
1109 return PACKET_ERROR
;
1111 /* Always treat "E." as an error. This will be used for
1112 more verbose error messages, such as E.memtypes. */
1113 if (buf
[0] == 'E' && buf
[1] == '.')
1114 return PACKET_ERROR
;
1116 /* The packet may or may not be OK. Just assume it is. */
1120 /* The stub does not support the packet. */
1121 return PACKET_UNKNOWN
;
1124 static enum packet_result
1125 packet_ok (const char *buf
, struct packet_config
*config
)
1127 enum packet_result result
;
1129 result
= packet_check_result (buf
);
1134 /* The stub recognized the packet request. */
1135 switch (config
->support
)
1137 case PACKET_SUPPORT_UNKNOWN
:
1139 fprintf_unfiltered (gdb_stdlog
,
1140 "Packet %s (%s) is supported\n",
1141 config
->name
, config
->title
);
1142 config
->support
= PACKET_ENABLE
;
1144 case PACKET_DISABLE
:
1145 internal_error (__FILE__
, __LINE__
,
1146 _("packet_ok: attempt to use a disabled packet"));
1152 case PACKET_UNKNOWN
:
1153 /* The stub does not support the packet. */
1154 switch (config
->support
)
1157 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1158 /* If the stub previously indicated that the packet was
1159 supported then there is a protocol error.. */
1160 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1161 config
->name
, config
->title
);
1163 /* The user set it wrong. */
1164 error (_("Enabled packet %s (%s) not recognized by stub"),
1165 config
->name
, config
->title
);
1167 case PACKET_SUPPORT_UNKNOWN
:
1169 fprintf_unfiltered (gdb_stdlog
,
1170 "Packet %s (%s) is NOT supported\n",
1171 config
->name
, config
->title
);
1172 config
->support
= PACKET_DISABLE
;
1174 case PACKET_DISABLE
:
1196 PACKET_vFile_pwrite
,
1198 PACKET_vFile_unlink
,
1200 PACKET_qXfer_features
,
1201 PACKET_qXfer_libraries
,
1202 PACKET_qXfer_memory_map
,
1203 PACKET_qXfer_spu_read
,
1204 PACKET_qXfer_spu_write
,
1205 PACKET_qXfer_osdata
,
1206 PACKET_qXfer_threads
,
1207 PACKET_qXfer_statictrace_read
,
1208 PACKET_qXfer_traceframe_info
,
1212 PACKET_QPassSignals
,
1213 PACKET_qSearch_memory
,
1216 PACKET_QStartNoAckMode
,
1218 PACKET_qXfer_siginfo_read
,
1219 PACKET_qXfer_siginfo_write
,
1221 PACKET_ConditionalTracepoints
,
1222 PACKET_FastTracepoints
,
1223 PACKET_StaticTracepoints
,
1226 PACKET_TracepointSource
,
1231 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1234 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1235 struct cmd_list_element
*c
)
1237 struct packet_config
*packet
;
1239 for (packet
= remote_protocol_packets
;
1240 packet
< &remote_protocol_packets
[PACKET_MAX
];
1243 if (&packet
->detect
== c
->var
)
1245 update_packet_config (packet
);
1249 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1254 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1255 struct cmd_list_element
*c
,
1258 struct packet_config
*packet
;
1260 for (packet
= remote_protocol_packets
;
1261 packet
< &remote_protocol_packets
[PACKET_MAX
];
1264 if (&packet
->detect
== c
->var
)
1266 show_packet_config_cmd (packet
);
1270 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1274 /* Should we try one of the 'Z' requests? */
1278 Z_PACKET_SOFTWARE_BP
,
1279 Z_PACKET_HARDWARE_BP
,
1286 /* For compatibility with older distributions. Provide a ``set remote
1287 Z-packet ...'' command that updates all the Z packet types. */
1289 static enum auto_boolean remote_Z_packet_detect
;
1292 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1293 struct cmd_list_element
*c
)
1297 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1299 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1300 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1305 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1306 struct cmd_list_element
*c
,
1311 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1313 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1317 /* Should we try the 'ThreadInfo' query packet?
1319 This variable (NOT available to the user: auto-detect only!)
1320 determines whether GDB will use the new, simpler "ThreadInfo"
1321 query or the older, more complex syntax for thread queries.
1322 This is an auto-detect variable (set to true at each connect,
1323 and set to false when the target fails to recognize it). */
1325 static int use_threadinfo_query
;
1326 static int use_threadextra_query
;
1328 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1329 static struct async_signal_handler
*sigint_remote_twice_token
;
1330 static struct async_signal_handler
*sigint_remote_token
;
1333 /* Asynchronous signal handle registered as event loop source for
1334 when we have pending events ready to be passed to the core. */
1336 static struct async_event_handler
*remote_async_inferior_event_token
;
1338 /* Asynchronous signal handle registered as event loop source for when
1339 the remote sent us a %Stop notification. The registered callback
1340 will do a vStopped sequence to pull the rest of the events out of
1341 the remote side into our event queue. */
1343 static struct async_event_handler
*remote_async_get_pending_events_token
;
1346 static ptid_t magic_null_ptid
;
1347 static ptid_t not_sent_ptid
;
1348 static ptid_t any_thread_ptid
;
1350 /* These are the threads which we last sent to the remote system. The
1351 TID member will be -1 for all or -2 for not sent yet. */
1353 static ptid_t general_thread
;
1354 static ptid_t continue_thread
;
1356 /* This the traceframe which we last selected on the remote system.
1357 It will be -1 if no traceframe is selected. */
1358 static int remote_traceframe_number
= -1;
1360 /* Find out if the stub attached to PID (and hence GDB should offer to
1361 detach instead of killing it when bailing out). */
1364 remote_query_attached (int pid
)
1366 struct remote_state
*rs
= get_remote_state ();
1368 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1371 if (remote_multi_process_p (rs
))
1372 sprintf (rs
->buf
, "qAttached:%x", pid
);
1374 sprintf (rs
->buf
, "qAttached");
1377 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1379 switch (packet_ok (rs
->buf
,
1380 &remote_protocol_packets
[PACKET_qAttached
]))
1383 if (strcmp (rs
->buf
, "1") == 0)
1387 warning (_("Remote failure reply: %s"), rs
->buf
);
1389 case PACKET_UNKNOWN
:
1396 /* Add PID to GDB's inferior table. Since we can be connected to a
1397 remote system before before knowing about any inferior, mark the
1398 target with execution when we find the first inferior. If ATTACHED
1399 is 1, then we had just attached to this inferior. If it is 0, then
1400 we just created this inferior. If it is -1, then try querying the
1401 remote stub to find out if it had attached to the inferior or
1404 static struct inferior
*
1405 remote_add_inferior (int pid
, int attached
)
1407 struct inferior
*inf
;
1409 /* Check whether this process we're learning about is to be
1410 considered attached, or if is to be considered to have been
1411 spawned by the stub. */
1413 attached
= remote_query_attached (pid
);
1415 if (gdbarch_has_global_solist (target_gdbarch
))
1417 /* If the target shares code across all inferiors, then every
1418 attach adds a new inferior. */
1419 inf
= add_inferior (pid
);
1421 /* ... and every inferior is bound to the same program space.
1422 However, each inferior may still have its own address
1424 inf
->aspace
= maybe_new_address_space ();
1425 inf
->pspace
= current_program_space
;
1429 /* In the traditional debugging scenario, there's a 1-1 match
1430 between program/address spaces. We simply bind the inferior
1431 to the program space's address space. */
1432 inf
= current_inferior ();
1433 inferior_appeared (inf
, pid
);
1436 inf
->attach_flag
= attached
;
1441 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1442 according to RUNNING. */
1445 remote_add_thread (ptid_t ptid
, int running
)
1449 set_executing (ptid
, running
);
1450 set_running (ptid
, running
);
1453 /* Come here when we learn about a thread id from the remote target.
1454 It may be the first time we hear about such thread, so take the
1455 opportunity to add it to GDB's thread list. In case this is the
1456 first time we're noticing its corresponding inferior, add it to
1457 GDB's inferior list as well. */
1460 remote_notice_new_inferior (ptid_t currthread
, int running
)
1462 /* If this is a new thread, add it to GDB's thread list.
1463 If we leave it up to WFI to do this, bad things will happen. */
1465 if (in_thread_list (currthread
) && is_exited (currthread
))
1467 /* We're seeing an event on a thread id we knew had exited.
1468 This has to be a new thread reusing the old id. Add it. */
1469 remote_add_thread (currthread
, running
);
1473 if (!in_thread_list (currthread
))
1475 struct inferior
*inf
= NULL
;
1476 int pid
= ptid_get_pid (currthread
);
1478 if (ptid_is_pid (inferior_ptid
)
1479 && pid
== ptid_get_pid (inferior_ptid
))
1481 /* inferior_ptid has no thread member yet. This can happen
1482 with the vAttach -> remote_wait,"TAAthread:" path if the
1483 stub doesn't support qC. This is the first stop reported
1484 after an attach, so this is the main thread. Update the
1485 ptid in the thread list. */
1486 if (in_thread_list (pid_to_ptid (pid
)))
1487 thread_change_ptid (inferior_ptid
, currthread
);
1490 remote_add_thread (currthread
, running
);
1491 inferior_ptid
= currthread
;
1496 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1498 /* inferior_ptid is not set yet. This can happen with the
1499 vRun -> remote_wait,"TAAthread:" path if the stub
1500 doesn't support qC. This is the first stop reported
1501 after an attach, so this is the main thread. Update the
1502 ptid in the thread list. */
1503 thread_change_ptid (inferior_ptid
, currthread
);
1507 /* When connecting to a target remote, or to a target
1508 extended-remote which already was debugging an inferior, we
1509 may not know about it yet. Add it before adding its child
1510 thread, so notifications are emitted in a sensible order. */
1511 if (!in_inferior_list (ptid_get_pid (currthread
)))
1512 inf
= remote_add_inferior (ptid_get_pid (currthread
), -1);
1514 /* This is really a new thread. Add it. */
1515 remote_add_thread (currthread
, running
);
1517 /* If we found a new inferior, let the common code do whatever
1518 it needs to with it (e.g., read shared libraries, insert
1521 notice_new_inferior (currthread
, running
, 0);
1525 /* Return the private thread data, creating it if necessary. */
1527 struct private_thread_info
*
1528 demand_private_info (ptid_t ptid
)
1530 struct thread_info
*info
= find_thread_ptid (ptid
);
1536 info
->private = xmalloc (sizeof (*(info
->private)));
1537 info
->private_dtor
= free_private_thread_info
;
1538 info
->private->core
= -1;
1539 info
->private->extra
= 0;
1542 return info
->private;
1545 /* Call this function as a result of
1546 1) A halt indication (T packet) containing a thread id
1547 2) A direct query of currthread
1548 3) Successful execution of set thread */
1551 record_currthread (ptid_t currthread
)
1553 general_thread
= currthread
;
1556 static char *last_pass_packet
;
1558 /* If 'QPassSignals' is supported, tell the remote stub what signals
1559 it can simply pass through to the inferior without reporting. */
1562 remote_pass_signals (void)
1564 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1566 char *pass_packet
, *p
;
1567 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1570 gdb_assert (numsigs
< 256);
1571 for (i
= 0; i
< numsigs
; i
++)
1573 if (signal_stop_state (i
) == 0
1574 && signal_print_state (i
) == 0
1575 && signal_pass_state (i
) == 1)
1578 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1579 strcpy (pass_packet
, "QPassSignals:");
1580 p
= pass_packet
+ strlen (pass_packet
);
1581 for (i
= 0; i
< numsigs
; i
++)
1583 if (signal_stop_state (i
) == 0
1584 && signal_print_state (i
) == 0
1585 && signal_pass_state (i
) == 1)
1588 *p
++ = tohex (i
>> 4);
1589 *p
++ = tohex (i
& 15);
1598 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1600 struct remote_state
*rs
= get_remote_state ();
1601 char *buf
= rs
->buf
;
1603 putpkt (pass_packet
);
1604 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1605 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1606 if (last_pass_packet
)
1607 xfree (last_pass_packet
);
1608 last_pass_packet
= pass_packet
;
1611 xfree (pass_packet
);
1616 remote_notice_signals (ptid_t ptid
)
1618 /* Update the remote on signals to silently pass, if they've
1620 remote_pass_signals ();
1623 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1624 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1625 thread. If GEN is set, set the general thread, if not, then set
1626 the step/continue thread. */
1628 set_thread (struct ptid ptid
, int gen
)
1630 struct remote_state
*rs
= get_remote_state ();
1631 ptid_t state
= gen
? general_thread
: continue_thread
;
1632 char *buf
= rs
->buf
;
1633 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1635 if (ptid_equal (state
, ptid
))
1639 *buf
++ = gen
? 'g' : 'c';
1640 if (ptid_equal (ptid
, magic_null_ptid
))
1641 xsnprintf (buf
, endbuf
- buf
, "0");
1642 else if (ptid_equal (ptid
, any_thread_ptid
))
1643 xsnprintf (buf
, endbuf
- buf
, "0");
1644 else if (ptid_equal (ptid
, minus_one_ptid
))
1645 xsnprintf (buf
, endbuf
- buf
, "-1");
1647 write_ptid (buf
, endbuf
, ptid
);
1649 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1651 general_thread
= ptid
;
1653 continue_thread
= ptid
;
1657 set_general_thread (struct ptid ptid
)
1659 set_thread (ptid
, 1);
1663 set_continue_thread (struct ptid ptid
)
1665 set_thread (ptid
, 0);
1668 /* Change the remote current process. Which thread within the process
1669 ends up selected isn't important, as long as it is the same process
1670 as what INFERIOR_PTID points to.
1672 This comes from that fact that there is no explicit notion of
1673 "selected process" in the protocol. The selected process for
1674 general operations is the process the selected general thread
1678 set_general_process (void)
1680 struct remote_state
*rs
= get_remote_state ();
1682 /* If the remote can't handle multiple processes, don't bother. */
1683 if (!remote_multi_process_p (rs
))
1686 /* We only need to change the remote current thread if it's pointing
1687 at some other process. */
1688 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1689 set_general_thread (inferior_ptid
);
1693 /* Return nonzero if the thread PTID is still alive on the remote
1697 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1699 struct remote_state
*rs
= get_remote_state ();
1702 if (ptid_equal (ptid
, magic_null_ptid
))
1703 /* The main thread is always alive. */
1706 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1707 /* The main thread is always alive. This can happen after a
1708 vAttach, if the remote side doesn't support
1713 endp
= rs
->buf
+ get_remote_packet_size ();
1716 write_ptid (p
, endp
, ptid
);
1719 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1720 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1723 /* About these extended threadlist and threadinfo packets. They are
1724 variable length packets but, the fields within them are often fixed
1725 length. They are redundent enough to send over UDP as is the
1726 remote protocol in general. There is a matching unit test module
1729 #define OPAQUETHREADBYTES 8
1731 /* a 64 bit opaque identifier */
1732 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1734 /* WARNING: This threadref data structure comes from the remote O.S.,
1735 libstub protocol encoding, and remote.c. It is not particularly
1738 /* Right now, the internal structure is int. We want it to be bigger.
1739 Plan to fix this. */
1741 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1743 /* gdb_ext_thread_info is an internal GDB data structure which is
1744 equivalent to the reply of the remote threadinfo packet. */
1746 struct gdb_ext_thread_info
1748 threadref threadid
; /* External form of thread reference. */
1749 int active
; /* Has state interesting to GDB?
1751 char display
[256]; /* Brief state display, name,
1752 blocked/suspended. */
1753 char shortname
[32]; /* To be used to name threads. */
1754 char more_display
[256]; /* Long info, statistics, queue depth,
1758 /* The volume of remote transfers can be limited by submitting
1759 a mask containing bits specifying the desired information.
1760 Use a union of these values as the 'selection' parameter to
1761 get_thread_info. FIXME: Make these TAG names more thread specific. */
1763 #define TAG_THREADID 1
1764 #define TAG_EXISTS 2
1765 #define TAG_DISPLAY 4
1766 #define TAG_THREADNAME 8
1767 #define TAG_MOREDISPLAY 16
1769 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1771 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1773 static char *unpack_nibble (char *buf
, int *val
);
1775 static char *pack_nibble (char *buf
, int nibble
);
1777 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1779 static char *unpack_byte (char *buf
, int *value
);
1781 static char *pack_int (char *buf
, int value
);
1783 static char *unpack_int (char *buf
, int *value
);
1785 static char *unpack_string (char *src
, char *dest
, int length
);
1787 static char *pack_threadid (char *pkt
, threadref
*id
);
1789 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1791 void int_to_threadref (threadref
*id
, int value
);
1793 static int threadref_to_int (threadref
*ref
);
1795 static void copy_threadref (threadref
*dest
, threadref
*src
);
1797 static int threadmatch (threadref
*dest
, threadref
*src
);
1799 static char *pack_threadinfo_request (char *pkt
, int mode
,
1802 static int remote_unpack_thread_info_response (char *pkt
,
1803 threadref
*expectedref
,
1804 struct gdb_ext_thread_info
1808 static int remote_get_threadinfo (threadref
*threadid
,
1809 int fieldset
, /*TAG mask */
1810 struct gdb_ext_thread_info
*info
);
1812 static char *pack_threadlist_request (char *pkt
, int startflag
,
1814 threadref
*nextthread
);
1816 static int parse_threadlist_response (char *pkt
,
1818 threadref
*original_echo
,
1819 threadref
*resultlist
,
1822 static int remote_get_threadlist (int startflag
,
1823 threadref
*nextthread
,
1827 threadref
*threadlist
);
1829 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1831 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1832 void *context
, int looplimit
);
1834 static int remote_newthread_step (threadref
*ref
, void *context
);
1837 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1838 buffer we're allowed to write to. Returns
1839 BUF+CHARACTERS_WRITTEN. */
1842 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1845 struct remote_state
*rs
= get_remote_state ();
1847 if (remote_multi_process_p (rs
))
1849 pid
= ptid_get_pid (ptid
);
1851 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1853 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1855 tid
= ptid_get_tid (ptid
);
1857 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1859 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1864 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1865 passed the last parsed char. Returns null_ptid on error. */
1868 read_ptid (char *buf
, char **obuf
)
1872 ULONGEST pid
= 0, tid
= 0;
1876 /* Multi-process ptid. */
1877 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1879 error (_("invalid remote ptid: %s"), p
);
1882 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1885 return ptid_build (pid
, 0, tid
);
1888 /* No multi-process. Just a tid. */
1889 pp
= unpack_varlen_hex (p
, &tid
);
1891 /* Since the stub is not sending a process id, then default to
1892 what's in inferior_ptid, unless it's null at this point. If so,
1893 then since there's no way to know the pid of the reported
1894 threads, use the magic number. */
1895 if (ptid_equal (inferior_ptid
, null_ptid
))
1896 pid
= ptid_get_pid (magic_null_ptid
);
1898 pid
= ptid_get_pid (inferior_ptid
);
1902 return ptid_build (pid
, 0, tid
);
1905 /* Encode 64 bits in 16 chars of hex. */
1907 static const char hexchars
[] = "0123456789abcdef";
1910 ishex (int ch
, int *val
)
1912 if ((ch
>= 'a') && (ch
<= 'f'))
1914 *val
= ch
- 'a' + 10;
1917 if ((ch
>= 'A') && (ch
<= 'F'))
1919 *val
= ch
- 'A' + 10;
1922 if ((ch
>= '0') && (ch
<= '9'))
1933 if (ch
>= 'a' && ch
<= 'f')
1934 return ch
- 'a' + 10;
1935 if (ch
>= '0' && ch
<= '9')
1937 if (ch
>= 'A' && ch
<= 'F')
1938 return ch
- 'A' + 10;
1943 stub_unpack_int (char *buff
, int fieldlength
)
1950 nibble
= stubhex (*buff
++);
1954 retval
= retval
<< 4;
1960 unpack_varlen_hex (char *buff
, /* packet to parse */
1964 ULONGEST retval
= 0;
1966 while (ishex (*buff
, &nibble
))
1969 retval
= retval
<< 4;
1970 retval
|= nibble
& 0x0f;
1977 unpack_nibble (char *buf
, int *val
)
1979 *val
= fromhex (*buf
++);
1984 pack_nibble (char *buf
, int nibble
)
1986 *buf
++ = hexchars
[(nibble
& 0x0f)];
1991 pack_hex_byte (char *pkt
, int byte
)
1993 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1994 *pkt
++ = hexchars
[(byte
& 0xf)];
1999 unpack_byte (char *buf
, int *value
)
2001 *value
= stub_unpack_int (buf
, 2);
2006 pack_int (char *buf
, int value
)
2008 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
2009 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2010 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2011 buf
= pack_hex_byte (buf
, (value
& 0xff));
2016 unpack_int (char *buf
, int *value
)
2018 *value
= stub_unpack_int (buf
, 8);
2022 #if 0 /* Currently unused, uncomment when needed. */
2023 static char *pack_string (char *pkt
, char *string
);
2026 pack_string (char *pkt
, char *string
)
2031 len
= strlen (string
);
2033 len
= 200; /* Bigger than most GDB packets, junk??? */
2034 pkt
= pack_hex_byte (pkt
, len
);
2038 if ((ch
== '\0') || (ch
== '#'))
2039 ch
= '*'; /* Protect encapsulation. */
2044 #endif /* 0 (unused) */
2047 unpack_string (char *src
, char *dest
, int length
)
2056 pack_threadid (char *pkt
, threadref
*id
)
2059 unsigned char *altid
;
2061 altid
= (unsigned char *) id
;
2062 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2064 pkt
= pack_hex_byte (pkt
, *altid
++);
2070 unpack_threadid (char *inbuf
, threadref
*id
)
2073 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2076 altref
= (char *) id
;
2078 while (inbuf
< limit
)
2080 x
= stubhex (*inbuf
++);
2081 y
= stubhex (*inbuf
++);
2082 *altref
++ = (x
<< 4) | y
;
2087 /* Externally, threadrefs are 64 bits but internally, they are still
2088 ints. This is due to a mismatch of specifications. We would like
2089 to use 64bit thread references internally. This is an adapter
2093 int_to_threadref (threadref
*id
, int value
)
2095 unsigned char *scan
;
2097 scan
= (unsigned char *) id
;
2103 *scan
++ = (value
>> 24) & 0xff;
2104 *scan
++ = (value
>> 16) & 0xff;
2105 *scan
++ = (value
>> 8) & 0xff;
2106 *scan
++ = (value
& 0xff);
2110 threadref_to_int (threadref
*ref
)
2113 unsigned char *scan
;
2119 value
= (value
<< 8) | ((*scan
++) & 0xff);
2124 copy_threadref (threadref
*dest
, threadref
*src
)
2127 unsigned char *csrc
, *cdest
;
2129 csrc
= (unsigned char *) src
;
2130 cdest
= (unsigned char *) dest
;
2137 threadmatch (threadref
*dest
, threadref
*src
)
2139 /* Things are broken right now, so just assume we got a match. */
2141 unsigned char *srcp
, *destp
;
2143 srcp
= (char *) src
;
2144 destp
= (char *) dest
;
2148 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2155 threadid:1, # always request threadid
2162 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2165 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2167 *pkt
++ = 'q'; /* Info Query */
2168 *pkt
++ = 'P'; /* process or thread info */
2169 pkt
= pack_int (pkt
, mode
); /* mode */
2170 pkt
= pack_threadid (pkt
, id
); /* threadid */
2171 *pkt
= '\0'; /* terminate */
2175 /* These values tag the fields in a thread info response packet. */
2176 /* Tagging the fields allows us to request specific fields and to
2177 add more fields as time goes by. */
2179 #define TAG_THREADID 1 /* Echo the thread identifier. */
2180 #define TAG_EXISTS 2 /* Is this process defined enough to
2181 fetch registers and its stack? */
2182 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2183 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2184 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2188 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2189 struct gdb_ext_thread_info
*info
)
2191 struct remote_state
*rs
= get_remote_state ();
2195 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2198 /* info->threadid = 0; FIXME: implement zero_threadref. */
2200 info
->display
[0] = '\0';
2201 info
->shortname
[0] = '\0';
2202 info
->more_display
[0] = '\0';
2204 /* Assume the characters indicating the packet type have been
2206 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2207 pkt
= unpack_threadid (pkt
, &ref
);
2210 warning (_("Incomplete response to threadinfo request."));
2211 if (!threadmatch (&ref
, expectedref
))
2212 { /* This is an answer to a different request. */
2213 warning (_("ERROR RMT Thread info mismatch."));
2216 copy_threadref (&info
->threadid
, &ref
);
2218 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2220 /* Packets are terminated with nulls. */
2221 while ((pkt
< limit
) && mask
&& *pkt
)
2223 pkt
= unpack_int (pkt
, &tag
); /* tag */
2224 pkt
= unpack_byte (pkt
, &length
); /* length */
2225 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2227 warning (_("ERROR RMT: threadinfo tag mismatch."));
2231 if (tag
== TAG_THREADID
)
2235 warning (_("ERROR RMT: length of threadid is not 16."));
2239 pkt
= unpack_threadid (pkt
, &ref
);
2240 mask
= mask
& ~TAG_THREADID
;
2243 if (tag
== TAG_EXISTS
)
2245 info
->active
= stub_unpack_int (pkt
, length
);
2247 mask
= mask
& ~(TAG_EXISTS
);
2250 warning (_("ERROR RMT: 'exists' length too long."));
2256 if (tag
== TAG_THREADNAME
)
2258 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2259 mask
= mask
& ~TAG_THREADNAME
;
2262 if (tag
== TAG_DISPLAY
)
2264 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2265 mask
= mask
& ~TAG_DISPLAY
;
2268 if (tag
== TAG_MOREDISPLAY
)
2270 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2271 mask
= mask
& ~TAG_MOREDISPLAY
;
2274 warning (_("ERROR RMT: unknown thread info tag."));
2275 break; /* Not a tag we know about. */
2281 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2282 struct gdb_ext_thread_info
*info
)
2284 struct remote_state
*rs
= get_remote_state ();
2287 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2289 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2291 if (rs
->buf
[0] == '\0')
2294 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2299 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2302 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2303 threadref
*nextthread
)
2305 *pkt
++ = 'q'; /* info query packet */
2306 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2307 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2308 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2309 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2314 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2317 parse_threadlist_response (char *pkt
, int result_limit
,
2318 threadref
*original_echo
, threadref
*resultlist
,
2321 struct remote_state
*rs
= get_remote_state ();
2323 int count
, resultcount
, done
;
2326 /* Assume the 'q' and 'M chars have been stripped. */
2327 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2328 /* done parse past here */
2329 pkt
= unpack_byte (pkt
, &count
); /* count field */
2330 pkt
= unpack_nibble (pkt
, &done
);
2331 /* The first threadid is the argument threadid. */
2332 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2333 while ((count
-- > 0) && (pkt
< limit
))
2335 pkt
= unpack_threadid (pkt
, resultlist
++);
2336 if (resultcount
++ >= result_limit
)
2345 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2346 int *done
, int *result_count
, threadref
*threadlist
)
2348 struct remote_state
*rs
= get_remote_state ();
2349 static threadref echo_nextthread
;
2352 /* Trancate result limit to be smaller than the packet size. */
2353 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
2354 >= get_remote_packet_size ())
2355 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2357 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2359 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2361 if (*rs
->buf
== '\0')
2365 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2368 if (!threadmatch (&echo_nextthread
, nextthread
))
2370 /* FIXME: This is a good reason to drop the packet. */
2371 /* Possably, there is a duplicate response. */
2373 retransmit immediatly - race conditions
2374 retransmit after timeout - yes
2376 wait for packet, then exit
2378 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2379 return 0; /* I choose simply exiting. */
2381 if (*result_count
<= 0)
2385 warning (_("RMT ERROR : failed to get remote thread list."));
2388 return result
; /* break; */
2390 if (*result_count
> result_limit
)
2393 warning (_("RMT ERROR: threadlist response longer than requested."));
2399 /* This is the interface between remote and threads, remotes upper
2402 /* remote_find_new_threads retrieves the thread list and for each
2403 thread in the list, looks up the thread in GDB's internal list,
2404 adding the thread if it does not already exist. This involves
2405 getting partial thread lists from the remote target so, polling the
2406 quit_flag is required. */
2409 /* About this many threadisds fit in a packet. */
2411 #define MAXTHREADLISTRESULTS 32
2414 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2417 int done
, i
, result_count
;
2421 static threadref nextthread
;
2422 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2427 if (loopcount
++ > looplimit
)
2430 warning (_("Remote fetch threadlist -infinite loop-."));
2433 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2434 &done
, &result_count
, resultthreadlist
))
2439 /* Clear for later iterations. */
2441 /* Setup to resume next batch of thread references, set nextthread. */
2442 if (result_count
>= 1)
2443 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2445 while (result_count
--)
2446 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2453 remote_newthread_step (threadref
*ref
, void *context
)
2455 int pid
= ptid_get_pid (inferior_ptid
);
2456 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2458 if (!in_thread_list (ptid
))
2460 return 1; /* continue iterator */
2463 #define CRAZY_MAX_THREADS 1000
2466 remote_current_thread (ptid_t oldpid
)
2468 struct remote_state
*rs
= get_remote_state ();
2471 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2472 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2473 return read_ptid (&rs
->buf
[2], NULL
);
2478 /* Find new threads for info threads command.
2479 * Original version, using John Metzler's thread protocol.
2483 remote_find_new_threads (void)
2485 remote_threadlist_iterator (remote_newthread_step
, 0,
2489 #if defined(HAVE_LIBEXPAT)
2491 typedef struct thread_item
2497 DEF_VEC_O(thread_item_t
);
2499 struct threads_parsing_context
2501 VEC (thread_item_t
) *items
;
2505 start_thread (struct gdb_xml_parser
*parser
,
2506 const struct gdb_xml_element
*element
,
2507 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2509 struct threads_parsing_context
*data
= user_data
;
2511 struct thread_item item
;
2513 struct gdb_xml_value
*attr
;
2515 id
= xml_find_attribute (attributes
, "id")->value
;
2516 item
.ptid
= read_ptid (id
, NULL
);
2518 attr
= xml_find_attribute (attributes
, "core");
2520 item
.core
= *(ULONGEST
*) attr
->value
;
2526 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2530 end_thread (struct gdb_xml_parser
*parser
,
2531 const struct gdb_xml_element
*element
,
2532 void *user_data
, const char *body_text
)
2534 struct threads_parsing_context
*data
= user_data
;
2536 if (body_text
&& *body_text
)
2537 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2540 const struct gdb_xml_attribute thread_attributes
[] = {
2541 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2542 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2543 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2546 const struct gdb_xml_element thread_children
[] = {
2547 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2550 const struct gdb_xml_element threads_children
[] = {
2551 { "thread", thread_attributes
, thread_children
,
2552 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2553 start_thread
, end_thread
},
2554 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2557 const struct gdb_xml_element threads_elements
[] = {
2558 { "threads", NULL
, threads_children
,
2559 GDB_XML_EF_NONE
, NULL
, NULL
},
2560 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2563 /* Discard the contents of the constructed thread info context. */
2566 clear_threads_parsing_context (void *p
)
2568 struct threads_parsing_context
*context
= p
;
2570 struct thread_item
*item
;
2572 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2573 xfree (item
->extra
);
2575 VEC_free (thread_item_t
, context
->items
);
2581 * Find all threads for info threads command.
2582 * Uses new thread protocol contributed by Cisco.
2583 * Falls back and attempts to use the older method (above)
2584 * if the target doesn't respond to the new method.
2588 remote_threads_info (struct target_ops
*ops
)
2590 struct remote_state
*rs
= get_remote_state ();
2594 if (remote_desc
== 0) /* paranoia */
2595 error (_("Command can only be used when connected to the remote target."));
2597 #if defined(HAVE_LIBEXPAT)
2598 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2600 char *xml
= target_read_stralloc (¤t_target
,
2601 TARGET_OBJECT_THREADS
, NULL
);
2603 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2607 struct threads_parsing_context context
;
2609 context
.items
= NULL
;
2610 make_cleanup (clear_threads_parsing_context
, &context
);
2612 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2613 threads_elements
, xml
, &context
) == 0)
2616 struct thread_item
*item
;
2619 VEC_iterate (thread_item_t
, context
.items
, i
, item
);
2622 if (!ptid_equal (item
->ptid
, null_ptid
))
2624 struct private_thread_info
*info
;
2625 /* In non-stop mode, we assume new found threads
2626 are running until proven otherwise with a
2627 stop reply. In all-stop, we can only get
2628 here if all threads are stopped. */
2629 int running
= non_stop
? 1 : 0;
2631 remote_notice_new_inferior (item
->ptid
, running
);
2633 info
= demand_private_info (item
->ptid
);
2634 info
->core
= item
->core
;
2635 info
->extra
= item
->extra
;
2642 do_cleanups (back_to
);
2647 if (use_threadinfo_query
)
2649 putpkt ("qfThreadInfo");
2650 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2652 if (bufp
[0] != '\0') /* q packet recognized */
2654 while (*bufp
++ == 'm') /* reply contains one or more TID */
2658 new_thread
= read_ptid (bufp
, &bufp
);
2659 if (!ptid_equal (new_thread
, null_ptid
))
2661 /* In non-stop mode, we assume new found threads
2662 are running until proven otherwise with a
2663 stop reply. In all-stop, we can only get
2664 here if all threads are stopped. */
2665 int running
= non_stop
? 1 : 0;
2667 remote_notice_new_inferior (new_thread
, running
);
2670 while (*bufp
++ == ','); /* comma-separated list */
2671 putpkt ("qsThreadInfo");
2672 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2679 /* Only qfThreadInfo is supported in non-stop mode. */
2683 /* Else fall back to old method based on jmetzler protocol. */
2684 use_threadinfo_query
= 0;
2685 remote_find_new_threads ();
2690 * Collect a descriptive string about the given thread.
2691 * The target may say anything it wants to about the thread
2692 * (typically info about its blocked / runnable state, name, etc.).
2693 * This string will appear in the info threads display.
2695 * Optional: targets are not required to implement this function.
2699 remote_threads_extra_info (struct thread_info
*tp
)
2701 struct remote_state
*rs
= get_remote_state ();
2705 struct gdb_ext_thread_info threadinfo
;
2706 static char display_buf
[100]; /* arbitrary... */
2707 int n
= 0; /* position in display_buf */
2709 if (remote_desc
== 0) /* paranoia */
2710 internal_error (__FILE__
, __LINE__
,
2711 _("remote_threads_extra_info"));
2713 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2714 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2715 /* This is the main thread which was added by GDB. The remote
2716 server doesn't know about it. */
2719 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2721 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2723 if (info
&& info
->private)
2724 return info
->private->extra
;
2729 if (use_threadextra_query
)
2732 char *endb
= rs
->buf
+ get_remote_packet_size ();
2734 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2736 write_ptid (b
, endb
, tp
->ptid
);
2739 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2740 if (rs
->buf
[0] != 0)
2742 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2743 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2744 display_buf
[result
] = '\0';
2749 /* If the above query fails, fall back to the old method. */
2750 use_threadextra_query
= 0;
2751 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2752 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2753 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2754 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2755 if (threadinfo
.active
)
2757 if (*threadinfo
.shortname
)
2758 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2759 " Name: %s,", threadinfo
.shortname
);
2760 if (*threadinfo
.display
)
2761 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2762 " State: %s,", threadinfo
.display
);
2763 if (*threadinfo
.more_display
)
2764 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2765 " Priority: %s", threadinfo
.more_display
);
2769 /* For purely cosmetic reasons, clear up trailing commas. */
2770 if (',' == display_buf
[n
-1])
2771 display_buf
[n
-1] = ' ';
2780 remote_static_tracepoint_marker_at (CORE_ADDR addr
,
2781 struct static_tracepoint_marker
*marker
)
2783 struct remote_state
*rs
= get_remote_state ();
2786 sprintf (p
, "qTSTMat:");
2788 p
+= hexnumstr (p
, addr
);
2790 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2794 error (_("Remote failure reply: %s"), p
);
2798 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2806 free_current_marker (void *arg
)
2808 struct static_tracepoint_marker
**marker_p
= arg
;
2810 if (*marker_p
!= NULL
)
2812 release_static_tracepoint_marker (*marker_p
);
2819 static VEC(static_tracepoint_marker_p
) *
2820 remote_static_tracepoint_markers_by_strid (const char *strid
)
2822 struct remote_state
*rs
= get_remote_state ();
2823 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2824 struct static_tracepoint_marker
*marker
= NULL
;
2825 struct cleanup
*old_chain
;
2828 /* Ask for a first packet of static tracepoint marker
2831 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2834 error (_("Remote failure reply: %s"), p
);
2836 old_chain
= make_cleanup (free_current_marker
, &marker
);
2841 marker
= XCNEW (struct static_tracepoint_marker
);
2845 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2847 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2849 VEC_safe_push (static_tracepoint_marker_p
,
2855 release_static_tracepoint_marker (marker
);
2856 memset (marker
, 0, sizeof (*marker
));
2859 while (*p
++ == ','); /* comma-separated list */
2860 /* Ask for another packet of static tracepoint definition. */
2862 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2866 do_cleanups (old_chain
);
2871 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2874 remote_get_ada_task_ptid (long lwp
, long thread
)
2876 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2880 /* Restart the remote side; this is an extended protocol operation. */
2883 extended_remote_restart (void)
2885 struct remote_state
*rs
= get_remote_state ();
2887 /* Send the restart command; for reasons I don't understand the
2888 remote side really expects a number after the "R". */
2889 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2892 remote_fileio_reset ();
2895 /* Clean up connection to a remote debugger. */
2898 remote_close (int quitting
)
2900 if (remote_desc
== NULL
)
2901 return; /* already closed */
2903 /* Make sure we leave stdin registered in the event loop, and we
2904 don't leave the async SIGINT signal handler installed. */
2905 remote_terminal_ours ();
2907 serial_close (remote_desc
);
2910 /* We don't have a connection to the remote stub anymore. Get rid
2911 of all the inferiors and their threads we were controlling.
2912 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
2913 will be unable to find the thread corresponding to (pid, 0, 0). */
2914 inferior_ptid
= null_ptid
;
2915 discard_all_inferiors ();
2917 /* We're no longer interested in any of these events. */
2918 discard_pending_stop_replies (-1);
2920 if (remote_async_inferior_event_token
)
2921 delete_async_event_handler (&remote_async_inferior_event_token
);
2922 if (remote_async_get_pending_events_token
)
2923 delete_async_event_handler (&remote_async_get_pending_events_token
);
2926 /* Query the remote side for the text, data and bss offsets. */
2931 struct remote_state
*rs
= get_remote_state ();
2934 int lose
, num_segments
= 0, do_sections
, do_segments
;
2935 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2936 struct section_offsets
*offs
;
2937 struct symfile_segment_data
*data
;
2939 if (symfile_objfile
== NULL
)
2942 putpkt ("qOffsets");
2943 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2946 if (buf
[0] == '\000')
2947 return; /* Return silently. Stub doesn't support
2951 warning (_("Remote failure reply: %s"), buf
);
2955 /* Pick up each field in turn. This used to be done with scanf, but
2956 scanf will make trouble if CORE_ADDR size doesn't match
2957 conversion directives correctly. The following code will work
2958 with any size of CORE_ADDR. */
2959 text_addr
= data_addr
= bss_addr
= 0;
2963 if (strncmp (ptr
, "Text=", 5) == 0)
2966 /* Don't use strtol, could lose on big values. */
2967 while (*ptr
&& *ptr
!= ';')
2968 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2970 if (strncmp (ptr
, ";Data=", 6) == 0)
2973 while (*ptr
&& *ptr
!= ';')
2974 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2979 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2982 while (*ptr
&& *ptr
!= ';')
2983 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2985 if (bss_addr
!= data_addr
)
2986 warning (_("Target reported unsupported offsets: %s"), buf
);
2991 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2994 /* Don't use strtol, could lose on big values. */
2995 while (*ptr
&& *ptr
!= ';')
2996 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2999 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3002 while (*ptr
&& *ptr
!= ';')
3003 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3011 error (_("Malformed response to offset query, %s"), buf
);
3012 else if (*ptr
!= '\0')
3013 warning (_("Target reported unsupported offsets: %s"), buf
);
3015 offs
= ((struct section_offsets
*)
3016 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3017 memcpy (offs
, symfile_objfile
->section_offsets
,
3018 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3020 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3021 do_segments
= (data
!= NULL
);
3022 do_sections
= num_segments
== 0;
3024 if (num_segments
> 0)
3026 segments
[0] = text_addr
;
3027 segments
[1] = data_addr
;
3029 /* If we have two segments, we can still try to relocate everything
3030 by assuming that the .text and .data offsets apply to the whole
3031 text and data segments. Convert the offsets given in the packet
3032 to base addresses for symfile_map_offsets_to_segments. */
3033 else if (data
&& data
->num_segments
== 2)
3035 segments
[0] = data
->segment_bases
[0] + text_addr
;
3036 segments
[1] = data
->segment_bases
[1] + data_addr
;
3039 /* If the object file has only one segment, assume that it is text
3040 rather than data; main programs with no writable data are rare,
3041 but programs with no code are useless. Of course the code might
3042 have ended up in the data segment... to detect that we would need
3043 the permissions here. */
3044 else if (data
&& data
->num_segments
== 1)
3046 segments
[0] = data
->segment_bases
[0] + text_addr
;
3049 /* There's no way to relocate by segment. */
3055 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3056 offs
, num_segments
, segments
);
3058 if (ret
== 0 && !do_sections
)
3059 error (_("Can not handle qOffsets TextSeg "
3060 "response with this symbol file"));
3067 free_symfile_segment_data (data
);
3071 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3073 /* This is a temporary kludge to force data and bss to use the
3074 same offsets because that's what nlmconv does now. The real
3075 solution requires changes to the stub and remote.c that I
3076 don't have time to do right now. */
3078 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3079 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3082 objfile_relocate (symfile_objfile
, offs
);
3085 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3086 threads we know are stopped already. This is used during the
3087 initial remote connection in non-stop mode --- threads that are
3088 reported as already being stopped are left stopped. */
3091 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3093 /* If we have a stop reply for this thread, it must be stopped. */
3094 if (peek_stop_reply (thread
->ptid
))
3095 set_stop_requested (thread
->ptid
, 1);
3100 /* Stub for catch_exception. */
3102 struct start_remote_args
3106 /* The current target. */
3107 struct target_ops
*target
;
3109 /* Non-zero if this is an extended-remote target. */
3113 /* Send interrupt_sequence to remote target. */
3115 send_interrupt_sequence (void)
3117 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3118 serial_write (remote_desc
, "\x03", 1);
3119 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3120 serial_send_break (remote_desc
);
3121 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3123 serial_send_break (remote_desc
);
3124 serial_write (remote_desc
, "g", 1);
3127 internal_error (__FILE__
, __LINE__
,
3128 _("Invalid value for interrupt_sequence_mode: %s."),
3129 interrupt_sequence_mode
);
3133 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
3135 struct start_remote_args
*args
= opaque
;
3136 struct remote_state
*rs
= get_remote_state ();
3137 struct packet_config
*noack_config
;
3138 char *wait_status
= NULL
;
3140 immediate_quit
++; /* Allow user to interrupt it. */
3142 /* Ack any packet which the remote side has already sent. */
3143 serial_write (remote_desc
, "+", 1);
3145 if (interrupt_on_connect
)
3146 send_interrupt_sequence ();
3148 /* The first packet we send to the target is the optional "supported
3149 packets" request. If the target can answer this, it will tell us
3150 which later probes to skip. */
3151 remote_query_supported ();
3153 /* If the stub wants to get a QAllow, compose one and send it. */
3154 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3155 remote_set_permissions ();
3157 /* Next, we possibly activate noack mode.
3159 If the QStartNoAckMode packet configuration is set to AUTO,
3160 enable noack mode if the stub reported a wish for it with
3163 If set to TRUE, then enable noack mode even if the stub didn't
3164 report it in qSupported. If the stub doesn't reply OK, the
3165 session ends with an error.
3167 If FALSE, then don't activate noack mode, regardless of what the
3168 stub claimed should be the default with qSupported. */
3170 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3172 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3173 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3174 && noack_config
->support
== PACKET_ENABLE
))
3176 putpkt ("QStartNoAckMode");
3177 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3178 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3182 if (args
->extended_p
)
3184 /* Tell the remote that we are using the extended protocol. */
3186 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3189 /* Next, if the target can specify a description, read it. We do
3190 this before anything involving memory or registers. */
3191 target_find_description ();
3193 /* Next, now that we know something about the target, update the
3194 address spaces in the program spaces. */
3195 update_address_spaces ();
3197 /* On OSs where the list of libraries is global to all
3198 processes, we fetch them early. */
3199 if (gdbarch_has_global_solist (target_gdbarch
))
3200 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
3204 if (!rs
->non_stop_aware
)
3205 error (_("Non-stop mode requested, but remote "
3206 "does not support non-stop"));
3208 putpkt ("QNonStop:1");
3209 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3211 if (strcmp (rs
->buf
, "OK") != 0)
3212 error (_("Remote refused setting non-stop mode with: %s"), rs
->buf
);
3214 /* Find about threads and processes the stub is already
3215 controlling. We default to adding them in the running state.
3216 The '?' query below will then tell us about which threads are
3218 remote_threads_info (args
->target
);
3220 else if (rs
->non_stop_aware
)
3222 /* Don't assume that the stub can operate in all-stop mode.
3223 Request it explicitely. */
3224 putpkt ("QNonStop:0");
3225 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3227 if (strcmp (rs
->buf
, "OK") != 0)
3228 error (_("Remote refused setting all-stop mode with: %s"), rs
->buf
);
3231 /* Check whether the target is running now. */
3233 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3237 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3239 if (!args
->extended_p
)
3240 error (_("The target is not running (try extended-remote?)"));
3242 /* We're connected, but not running. Drop out before we
3243 call start_remote. */
3248 /* Save the reply for later. */
3249 wait_status
= alloca (strlen (rs
->buf
) + 1);
3250 strcpy (wait_status
, rs
->buf
);
3253 /* Let the stub know that we want it to return the thread. */
3254 set_continue_thread (minus_one_ptid
);
3256 /* Without this, some commands which require an active target
3257 (such as kill) won't work. This variable serves (at least)
3258 double duty as both the pid of the target process (if it has
3259 such), and as a flag indicating that a target is active.
3260 These functions should be split out into seperate variables,
3261 especially since GDB will someday have a notion of debugging
3262 several processes. */
3263 inferior_ptid
= magic_null_ptid
;
3265 /* Now, if we have thread information, update inferior_ptid. */
3266 inferior_ptid
= remote_current_thread (inferior_ptid
);
3268 remote_add_inferior (ptid_get_pid (inferior_ptid
), -1);
3270 /* Always add the main thread. */
3271 add_thread_silent (inferior_ptid
);
3273 get_offsets (); /* Get text, data & bss offsets. */
3275 /* If we could not find a description using qXfer, and we know
3276 how to do it some other way, try again. This is not
3277 supported for non-stop; it could be, but it is tricky if
3278 there are no stopped threads when we connect. */
3279 if (remote_read_description_p (args
->target
)
3280 && gdbarch_target_desc (target_gdbarch
) == NULL
)
3282 target_clear_description ();
3283 target_find_description ();
3286 /* Use the previously fetched status. */
3287 gdb_assert (wait_status
!= NULL
);
3288 strcpy (rs
->buf
, wait_status
);
3289 rs
->cached_wait_status
= 1;
3292 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
3296 /* Clear WFI global state. Do this before finding about new
3297 threads and inferiors, and setting the current inferior.
3298 Otherwise we would clear the proceed status of the current
3299 inferior when we want its stop_soon state to be preserved
3300 (see notice_new_inferior). */
3301 init_wait_for_inferior ();
3303 /* In non-stop, we will either get an "OK", meaning that there
3304 are no stopped threads at this time; or, a regular stop
3305 reply. In the latter case, there may be more than one thread
3306 stopped --- we pull them all out using the vStopped
3308 if (strcmp (rs
->buf
, "OK") != 0)
3310 struct stop_reply
*stop_reply
;
3311 struct cleanup
*old_chain
;
3313 stop_reply
= stop_reply_xmalloc ();
3314 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3316 remote_parse_stop_reply (rs
->buf
, stop_reply
);
3317 discard_cleanups (old_chain
);
3319 /* get_pending_stop_replies acks this one, and gets the rest
3321 pending_stop_reply
= stop_reply
;
3322 remote_get_pending_stop_replies ();
3324 /* Make sure that threads that were stopped remain
3326 iterate_over_threads (set_stop_requested_callback
, NULL
);
3329 if (target_can_async_p ())
3330 target_async (inferior_event_handler
, 0);
3332 if (thread_count () == 0)
3334 if (!args
->extended_p
)
3335 error (_("The target is not running (try extended-remote?)"));
3337 /* We're connected, but not running. Drop out before we
3338 call start_remote. */
3342 /* Let the stub know that we want it to return the thread. */
3344 /* Force the stub to choose a thread. */
3345 set_general_thread (null_ptid
);
3348 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3349 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3350 error (_("remote didn't report the current thread in non-stop mode"));
3352 get_offsets (); /* Get text, data & bss offsets. */
3354 /* In non-stop mode, any cached wait status will be stored in
3355 the stop reply queue. */
3356 gdb_assert (wait_status
== NULL
);
3358 /* Update the remote on signals to silently pass, or more
3359 importantly, which to not ignore, in case a previous session
3360 had set some different set of signals to be ignored. */
3361 remote_pass_signals ();
3364 /* If we connected to a live target, do some additional setup. */
3365 if (target_has_execution
)
3367 if (exec_bfd
) /* No use without an exec file. */
3368 remote_check_symbols (symfile_objfile
);
3371 /* Possibly the target has been engaged in a trace run started
3372 previously; find out where things are at. */
3373 if (remote_get_trace_status (current_trace_status ()) != -1)
3375 struct uploaded_tp
*uploaded_tps
= NULL
;
3376 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3378 if (current_trace_status ()->running
)
3379 printf_filtered (_("Trace is already running on the target.\n"));
3381 /* Get trace state variables first, they may be checked when
3382 parsing uploaded commands. */
3384 remote_upload_trace_state_variables (&uploaded_tsvs
);
3386 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3388 remote_upload_tracepoints (&uploaded_tps
);
3390 merge_uploaded_tracepoints (&uploaded_tps
);
3393 /* If breakpoints are global, insert them now. */
3394 if (gdbarch_has_global_breakpoints (target_gdbarch
)
3395 && breakpoints_always_inserted_mode ())
3396 insert_breakpoints ();
3399 /* Open a connection to a remote debugger.
3400 NAME is the filename used for communication. */
3403 remote_open (char *name
, int from_tty
)
3405 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3408 /* Open a connection to a remote debugger using the extended
3409 remote gdb protocol. NAME is the filename used for communication. */
3412 extended_remote_open (char *name
, int from_tty
)
3414 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3417 /* Generic code for opening a connection to a remote target. */
3420 init_all_packet_configs (void)
3424 for (i
= 0; i
< PACKET_MAX
; i
++)
3425 update_packet_config (&remote_protocol_packets
[i
]);
3428 /* Symbol look-up. */
3431 remote_check_symbols (struct objfile
*objfile
)
3433 struct remote_state
*rs
= get_remote_state ();
3434 char *msg
, *reply
, *tmp
;
3435 struct minimal_symbol
*sym
;
3438 /* The remote side has no concept of inferiors that aren't running
3439 yet, it only knows about running processes. If we're connected
3440 but our current inferior is not running, we should not invite the
3441 remote target to request symbol lookups related to its
3442 (unrelated) current process. */
3443 if (!target_has_execution
)
3446 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3449 /* Make sure the remote is pointing at the right process. Note
3450 there's no way to select "no process". */
3451 set_general_process ();
3453 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3454 because we need both at the same time. */
3455 msg
= alloca (get_remote_packet_size ());
3457 /* Invite target to request symbol lookups. */
3459 putpkt ("qSymbol::");
3460 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3461 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3464 while (strncmp (reply
, "qSymbol:", 8) == 0)
3467 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3469 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3471 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3474 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
3475 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3477 /* If this is a function address, return the start of code
3478 instead of any data function descriptor. */
3479 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
3483 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3484 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3488 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3493 static struct serial
*
3494 remote_serial_open (char *name
)
3496 static int udp_warning
= 0;
3498 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3499 of in ser-tcp.c, because it is the remote protocol assuming that the
3500 serial connection is reliable and not the serial connection promising
3502 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3504 warning (_("The remote protocol may be unreliable over UDP.\n"
3505 "Some events may be lost, rendering further debugging "
3510 return serial_open (name
);
3513 /* Inform the target of our permission settings. The permission flags
3514 work without this, but if the target knows the settings, it can do
3515 a couple things. First, it can add its own check, to catch cases
3516 that somehow manage to get by the permissions checks in target
3517 methods. Second, if the target is wired to disallow particular
3518 settings (for instance, a system in the field that is not set up to
3519 be able to stop at a breakpoint), it can object to any unavailable
3523 remote_set_permissions (void)
3525 struct remote_state
*rs
= get_remote_state ();
3527 sprintf (rs
->buf
, "QAllow:"
3528 "WriteReg:%x;WriteMem:%x;"
3529 "InsertBreak:%x;InsertTrace:%x;"
3530 "InsertFastTrace:%x;Stop:%x",
3531 may_write_registers
, may_write_memory
,
3532 may_insert_breakpoints
, may_insert_tracepoints
,
3533 may_insert_fast_tracepoints
, may_stop
);
3535 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3537 /* If the target didn't like the packet, warn the user. Do not try
3538 to undo the user's settings, that would just be maddening. */
3539 if (strcmp (rs
->buf
, "OK") != 0)
3540 warning (_("Remote refused setting permissions with: %s"), rs
->buf
);
3543 /* This type describes each known response to the qSupported
3545 struct protocol_feature
3547 /* The name of this protocol feature. */
3550 /* The default for this protocol feature. */
3551 enum packet_support default_support
;
3553 /* The function to call when this feature is reported, or after
3554 qSupported processing if the feature is not supported.
3555 The first argument points to this structure. The second
3556 argument indicates whether the packet requested support be
3557 enabled, disabled, or probed (or the default, if this function
3558 is being called at the end of processing and this feature was
3559 not reported). The third argument may be NULL; if not NULL, it
3560 is a NUL-terminated string taken from the packet following
3561 this feature's name and an equals sign. */
3562 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3565 /* The corresponding packet for this feature. Only used if
3566 FUNC is remote_supported_packet. */
3571 remote_supported_packet (const struct protocol_feature
*feature
,
3572 enum packet_support support
,
3573 const char *argument
)
3577 warning (_("Remote qSupported response supplied an unexpected value for"
3578 " \"%s\"."), feature
->name
);
3582 if (remote_protocol_packets
[feature
->packet
].support
3583 == PACKET_SUPPORT_UNKNOWN
)
3584 remote_protocol_packets
[feature
->packet
].support
= support
;
3588 remote_packet_size (const struct protocol_feature
*feature
,
3589 enum packet_support support
, const char *value
)
3591 struct remote_state
*rs
= get_remote_state ();
3596 if (support
!= PACKET_ENABLE
)
3599 if (value
== NULL
|| *value
== '\0')
3601 warning (_("Remote target reported \"%s\" without a size."),
3607 packet_size
= strtol (value
, &value_end
, 16);
3608 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3610 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3611 feature
->name
, value
);
3615 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3617 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3618 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3619 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3622 /* Record the new maximum packet size. */
3623 rs
->explicit_packet_size
= packet_size
;
3627 remote_multi_process_feature (const struct protocol_feature
*feature
,
3628 enum packet_support support
, const char *value
)
3630 struct remote_state
*rs
= get_remote_state ();
3632 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3636 remote_non_stop_feature (const struct protocol_feature
*feature
,
3637 enum packet_support support
, const char *value
)
3639 struct remote_state
*rs
= get_remote_state ();
3641 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3645 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3646 enum packet_support support
,
3649 struct remote_state
*rs
= get_remote_state ();
3651 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3655 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3656 enum packet_support support
,
3659 struct remote_state
*rs
= get_remote_state ();
3661 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3665 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3666 enum packet_support support
,
3669 struct remote_state
*rs
= get_remote_state ();
3671 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3675 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3676 enum packet_support support
,
3679 struct remote_state
*rs
= get_remote_state ();
3681 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3684 static struct protocol_feature remote_protocol_features
[] = {
3685 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3686 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3687 PACKET_qXfer_auxv
},
3688 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3689 PACKET_qXfer_features
},
3690 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3691 PACKET_qXfer_libraries
},
3692 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3693 PACKET_qXfer_memory_map
},
3694 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3695 PACKET_qXfer_spu_read
},
3696 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3697 PACKET_qXfer_spu_write
},
3698 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3699 PACKET_qXfer_osdata
},
3700 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
3701 PACKET_qXfer_threads
},
3702 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
3703 PACKET_qXfer_traceframe_info
},
3704 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
3705 PACKET_QPassSignals
},
3706 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
3707 PACKET_QStartNoAckMode
},
3708 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
3709 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
3710 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
3711 PACKET_qXfer_siginfo_read
},
3712 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
3713 PACKET_qXfer_siginfo_write
},
3714 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
3715 PACKET_ConditionalTracepoints
},
3716 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
3717 PACKET_FastTracepoints
},
3718 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
3719 PACKET_StaticTracepoints
},
3720 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
3722 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
3724 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
3726 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
3727 PACKET_TracepointSource
},
3728 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
3732 static char *remote_support_xml
;
3734 /* Register string appended to "xmlRegisters=" in qSupported query. */
3737 register_remote_support_xml (const char *xml
)
3739 #if defined(HAVE_LIBEXPAT)
3740 if (remote_support_xml
== NULL
)
3741 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
3744 char *copy
= xstrdup (remote_support_xml
+ 13);
3745 char *p
= strtok (copy
, ",");
3749 if (strcmp (p
, xml
) == 0)
3756 while ((p
= strtok (NULL
, ",")) != NULL
);
3759 remote_support_xml
= reconcat (remote_support_xml
,
3760 remote_support_xml
, ",", xml
,
3767 remote_query_supported_append (char *msg
, const char *append
)
3770 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
3772 return xstrdup (append
);
3776 remote_query_supported (void)
3778 struct remote_state
*rs
= get_remote_state ();
3781 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
3783 /* The packet support flags are handled differently for this packet
3784 than for most others. We treat an error, a disabled packet, and
3785 an empty response identically: any features which must be reported
3786 to be used will be automatically disabled. An empty buffer
3787 accomplishes this, since that is also the representation for a list
3788 containing no features. */
3791 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
3794 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
3797 q
= remote_query_supported_append (q
, "multiprocess+");
3799 if (remote_support_xml
)
3800 q
= remote_query_supported_append (q
, remote_support_xml
);
3802 q
= remote_query_supported_append (q
, "qRelocInsn+");
3804 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
3807 do_cleanups (old_chain
);
3809 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3811 /* If an error occured, warn, but do not return - just reset the
3812 buffer to empty and go on to disable features. */
3813 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3816 warning (_("Remote failure reply: %s"), rs
->buf
);
3821 memset (seen
, 0, sizeof (seen
));
3826 enum packet_support is_supported
;
3827 char *p
, *end
, *name_end
, *value
;
3829 /* First separate out this item from the rest of the packet. If
3830 there's another item after this, we overwrite the separator
3831 (terminated strings are much easier to work with). */
3833 end
= strchr (p
, ';');
3836 end
= p
+ strlen (p
);
3846 warning (_("empty item in \"qSupported\" response"));
3851 name_end
= strchr (p
, '=');
3854 /* This is a name=value entry. */
3855 is_supported
= PACKET_ENABLE
;
3856 value
= name_end
+ 1;
3865 is_supported
= PACKET_ENABLE
;
3869 is_supported
= PACKET_DISABLE
;
3873 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3877 warning (_("unrecognized item \"%s\" "
3878 "in \"qSupported\" response"), p
);
3884 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3885 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3887 const struct protocol_feature
*feature
;
3890 feature
= &remote_protocol_features
[i
];
3891 feature
->func (feature
, is_supported
, value
);
3896 /* If we increased the packet size, make sure to increase the global
3897 buffer size also. We delay this until after parsing the entire
3898 qSupported packet, because this is the same buffer we were
3900 if (rs
->buf_size
< rs
->explicit_packet_size
)
3902 rs
->buf_size
= rs
->explicit_packet_size
;
3903 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3906 /* Handle the defaults for unmentioned features. */
3907 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3910 const struct protocol_feature
*feature
;
3912 feature
= &remote_protocol_features
[i
];
3913 feature
->func (feature
, feature
->default_support
, NULL
);
3919 remote_open_1 (char *name
, int from_tty
,
3920 struct target_ops
*target
, int extended_p
)
3922 struct remote_state
*rs
= get_remote_state ();
3925 error (_("To open a remote debug connection, you need to specify what\n"
3926 "serial device is attached to the remote system\n"
3927 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3929 /* See FIXME above. */
3930 if (!target_async_permitted
)
3931 wait_forever_enabled_p
= 1;
3933 /* If we're connected to a running target, target_preopen will kill it.
3934 But if we're connected to a target system with no running process,
3935 then we will still be connected when it returns. Ask this question
3936 first, before target_preopen has a chance to kill anything. */
3937 if (remote_desc
!= NULL
&& !have_inferiors ())
3940 || query (_("Already connected to a remote target. Disconnect? ")))
3943 error (_("Still connected."));
3946 target_preopen (from_tty
);
3948 unpush_target (target
);
3950 /* This time without a query. If we were connected to an
3951 extended-remote target and target_preopen killed the running
3952 process, we may still be connected. If we are starting "target
3953 remote" now, the extended-remote target will not have been
3954 removed by unpush_target. */
3955 if (remote_desc
!= NULL
&& !have_inferiors ())
3958 /* Make sure we send the passed signals list the next time we resume. */
3959 xfree (last_pass_packet
);
3960 last_pass_packet
= NULL
;
3962 remote_fileio_reset ();
3963 reopen_exec_file ();
3966 remote_desc
= remote_serial_open (name
);
3968 perror_with_name (name
);
3970 if (baud_rate
!= -1)
3972 if (serial_setbaudrate (remote_desc
, baud_rate
))
3974 /* The requested speed could not be set. Error out to
3975 top level after closing remote_desc. Take care to
3976 set remote_desc to NULL to avoid closing remote_desc
3978 serial_close (remote_desc
);
3980 perror_with_name (name
);
3984 serial_raw (remote_desc
);
3986 /* If there is something sitting in the buffer we might take it as a
3987 response to a command, which would be bad. */
3988 serial_flush_input (remote_desc
);
3992 puts_filtered ("Remote debugging using ");
3993 puts_filtered (name
);
3994 puts_filtered ("\n");
3996 push_target (target
); /* Switch to using remote target now. */
3998 /* Register extra event sources in the event loop. */
3999 remote_async_inferior_event_token
4000 = create_async_event_handler (remote_async_inferior_event_handler
,
4002 remote_async_get_pending_events_token
4003 = create_async_event_handler (remote_async_get_pending_events_handler
,
4006 /* Reset the target state; these things will be queried either by
4007 remote_query_supported or as they are needed. */
4008 init_all_packet_configs ();
4009 rs
->cached_wait_status
= 0;
4010 rs
->explicit_packet_size
= 0;
4012 rs
->multi_process_aware
= 0;
4013 rs
->extended
= extended_p
;
4014 rs
->non_stop_aware
= 0;
4015 rs
->waiting_for_stop_reply
= 0;
4016 rs
->ctrlc_pending_p
= 0;
4018 general_thread
= not_sent_ptid
;
4019 continue_thread
= not_sent_ptid
;
4020 remote_traceframe_number
= -1;
4022 /* Probe for ability to use "ThreadInfo" query, as required. */
4023 use_threadinfo_query
= 1;
4024 use_threadextra_query
= 1;
4026 if (target_async_permitted
)
4028 /* With this target we start out by owning the terminal. */
4029 remote_async_terminal_ours_p
= 1;
4031 /* FIXME: cagney/1999-09-23: During the initial connection it is
4032 assumed that the target is already ready and able to respond to
4033 requests. Unfortunately remote_start_remote() eventually calls
4034 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4035 around this. Eventually a mechanism that allows
4036 wait_for_inferior() to expect/get timeouts will be
4038 wait_forever_enabled_p
= 0;
4041 /* First delete any symbols previously loaded from shared libraries. */
4042 no_shared_libraries (NULL
, 0);
4045 init_thread_list ();
4047 /* Start the remote connection. If error() or QUIT, discard this
4048 target (we'd otherwise be in an inconsistent state) and then
4049 propogate the error on up the exception chain. This ensures that
4050 the caller doesn't stumble along blindly assuming that the
4051 function succeeded. The CLI doesn't have this problem but other
4052 UI's, such as MI do.
4054 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4055 this function should return an error indication letting the
4056 caller restore the previous state. Unfortunately the command
4057 ``target remote'' is directly wired to this function making that
4058 impossible. On a positive note, the CLI side of this problem has
4059 been fixed - the function set_cmd_context() makes it possible for
4060 all the ``target ....'' commands to share a common callback
4061 function. See cli-dump.c. */
4063 struct gdb_exception ex
;
4064 struct start_remote_args args
;
4066 args
.from_tty
= from_tty
;
4067 args
.target
= target
;
4068 args
.extended_p
= extended_p
;
4070 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
4073 /* Pop the partially set up target - unless something else did
4074 already before throwing the exception. */
4075 if (remote_desc
!= NULL
)
4077 if (target_async_permitted
)
4078 wait_forever_enabled_p
= 1;
4079 throw_exception (ex
);
4083 if (target_async_permitted
)
4084 wait_forever_enabled_p
= 1;
4087 /* This takes a program previously attached to and detaches it. After
4088 this is done, GDB can be used to debug some other program. We
4089 better not have left any breakpoints in the target program or it'll
4090 die when it hits one. */
4093 remote_detach_1 (char *args
, int from_tty
, int extended
)
4095 int pid
= ptid_get_pid (inferior_ptid
);
4096 struct remote_state
*rs
= get_remote_state ();
4099 error (_("Argument given to \"detach\" when remotely debugging."));
4101 if (!target_has_execution
)
4102 error (_("No process to detach from."));
4104 /* Tell the remote target to detach. */
4105 if (remote_multi_process_p (rs
))
4106 sprintf (rs
->buf
, "D;%x", pid
);
4108 strcpy (rs
->buf
, "D");
4111 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4113 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4115 else if (rs
->buf
[0] == '\0')
4116 error (_("Remote doesn't know how to detach"));
4118 error (_("Can't detach process."));
4122 if (remote_multi_process_p (rs
))
4123 printf_filtered (_("Detached from remote %s.\n"),
4124 target_pid_to_str (pid_to_ptid (pid
)));
4128 puts_filtered (_("Detached from remote process.\n"));
4130 puts_filtered (_("Ending remote debugging.\n"));
4134 discard_pending_stop_replies (pid
);
4135 target_mourn_inferior ();
4139 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4141 remote_detach_1 (args
, from_tty
, 0);
4145 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4147 remote_detach_1 (args
, from_tty
, 1);
4150 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4153 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4156 error (_("Argument given to \"disconnect\" when remotely debugging."));
4158 /* Make sure we unpush even the extended remote targets; mourn
4159 won't do it. So call remote_mourn_1 directly instead of
4160 target_mourn_inferior. */
4161 remote_mourn_1 (target
);
4164 puts_filtered ("Ending remote debugging.\n");
4167 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4168 be chatty about it. */
4171 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4173 struct remote_state
*rs
= get_remote_state ();
4175 char *wait_status
= NULL
;
4177 pid
= parse_pid_to_attach (args
);
4179 /* Remote PID can be freely equal to getpid, do not check it here the same
4180 way as in other targets. */
4182 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4183 error (_("This target does not support attaching to a process"));
4185 sprintf (rs
->buf
, "vAttach;%x", pid
);
4187 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4189 if (packet_ok (rs
->buf
,
4190 &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4193 printf_unfiltered (_("Attached to %s\n"),
4194 target_pid_to_str (pid_to_ptid (pid
)));
4198 /* Save the reply for later. */
4199 wait_status
= alloca (strlen (rs
->buf
) + 1);
4200 strcpy (wait_status
, rs
->buf
);
4202 else if (strcmp (rs
->buf
, "OK") != 0)
4203 error (_("Attaching to %s failed with: %s"),
4204 target_pid_to_str (pid_to_ptid (pid
)),
4207 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4208 error (_("This target does not support attaching to a process"));
4210 error (_("Attaching to %s failed"),
4211 target_pid_to_str (pid_to_ptid (pid
)));
4213 set_current_inferior (remote_add_inferior (pid
, 1));
4215 inferior_ptid
= pid_to_ptid (pid
);
4219 struct thread_info
*thread
;
4221 /* Get list of threads. */
4222 remote_threads_info (target
);
4224 thread
= first_thread_of_process (pid
);
4226 inferior_ptid
= thread
->ptid
;
4228 inferior_ptid
= pid_to_ptid (pid
);
4230 /* Invalidate our notion of the remote current thread. */
4231 record_currthread (minus_one_ptid
);
4235 /* Now, if we have thread information, update inferior_ptid. */
4236 inferior_ptid
= remote_current_thread (inferior_ptid
);
4238 /* Add the main thread to the thread list. */
4239 add_thread_silent (inferior_ptid
);
4242 /* Next, if the target can specify a description, read it. We do
4243 this before anything involving memory or registers. */
4244 target_find_description ();
4248 /* Use the previously fetched status. */
4249 gdb_assert (wait_status
!= NULL
);
4251 if (target_can_async_p ())
4253 struct stop_reply
*stop_reply
;
4254 struct cleanup
*old_chain
;
4256 stop_reply
= stop_reply_xmalloc ();
4257 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4258 remote_parse_stop_reply (wait_status
, stop_reply
);
4259 discard_cleanups (old_chain
);
4260 push_stop_reply (stop_reply
);
4262 target_async (inferior_event_handler
, 0);
4266 gdb_assert (wait_status
!= NULL
);
4267 strcpy (rs
->buf
, wait_status
);
4268 rs
->cached_wait_status
= 1;
4272 gdb_assert (wait_status
== NULL
);
4276 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4278 extended_remote_attach_1 (ops
, args
, from_tty
);
4281 /* Convert hex digit A to a number. */
4286 if (a
>= '0' && a
<= '9')
4288 else if (a
>= 'a' && a
<= 'f')
4289 return a
- 'a' + 10;
4290 else if (a
>= 'A' && a
<= 'F')
4291 return a
- 'A' + 10;
4293 error (_("Reply contains invalid hex digit %d"), a
);
4297 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
4301 for (i
= 0; i
< count
; i
++)
4303 if (hex
[0] == 0 || hex
[1] == 0)
4305 /* Hex string is short, or of uneven length.
4306 Return the count that has been converted so far. */
4309 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
4315 /* Convert number NIB to a hex digit. */
4323 return 'a' + nib
- 10;
4327 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
4331 /* May use a length, or a nul-terminated string as input. */
4333 count
= strlen ((char *) bin
);
4335 for (i
= 0; i
< count
; i
++)
4337 *hex
++ = tohex ((*bin
>> 4) & 0xf);
4338 *hex
++ = tohex (*bin
++ & 0xf);
4344 /* Check for the availability of vCont. This function should also check
4348 remote_vcont_probe (struct remote_state
*rs
)
4352 strcpy (rs
->buf
, "vCont?");
4354 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4357 /* Make sure that the features we assume are supported. */
4358 if (strncmp (buf
, "vCont", 5) == 0)
4361 int support_s
, support_S
, support_c
, support_C
;
4367 rs
->support_vCont_t
= 0;
4368 while (p
&& *p
== ';')
4371 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4373 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4375 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4377 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4379 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4380 rs
->support_vCont_t
= 1;
4382 p
= strchr (p
, ';');
4385 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4386 BUF will make packet_ok disable the packet. */
4387 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4391 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4394 /* Helper function for building "vCont" resumptions. Write a
4395 resumption to P. ENDP points to one-passed-the-end of the buffer
4396 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4397 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4398 resumed thread should be single-stepped and/or signalled. If PTID
4399 equals minus_one_ptid, then all threads are resumed; if PTID
4400 represents a process, then all threads of the process are resumed;
4401 the thread to be stepped and/or signalled is given in the global
4405 append_resumption (char *p
, char *endp
,
4406 ptid_t ptid
, int step
, enum target_signal siggnal
)
4408 struct remote_state
*rs
= get_remote_state ();
4410 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
4411 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4413 p
+= xsnprintf (p
, endp
- p
, ";s");
4414 else if (siggnal
!= TARGET_SIGNAL_0
)
4415 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4417 p
+= xsnprintf (p
, endp
- p
, ";c");
4419 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4423 /* All (-1) threads of process. */
4424 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4426 p
+= xsnprintf (p
, endp
- p
, ":");
4427 p
= write_ptid (p
, endp
, nptid
);
4429 else if (!ptid_equal (ptid
, minus_one_ptid
))
4431 p
+= xsnprintf (p
, endp
- p
, ":");
4432 p
= write_ptid (p
, endp
, ptid
);
4438 /* Resume the remote inferior by using a "vCont" packet. The thread
4439 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4440 resumed thread should be single-stepped and/or signalled. If PTID
4441 equals minus_one_ptid, then all threads are resumed; the thread to
4442 be stepped and/or signalled is given in the global INFERIOR_PTID.
4443 This function returns non-zero iff it resumes the inferior.
4445 This function issues a strict subset of all possible vCont commands at the
4449 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
4451 struct remote_state
*rs
= get_remote_state ();
4455 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4456 remote_vcont_probe (rs
);
4458 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4462 endp
= rs
->buf
+ get_remote_packet_size ();
4464 /* If we could generate a wider range of packets, we'd have to worry
4465 about overflowing BUF. Should there be a generic
4466 "multi-part-packet" packet? */
4468 p
+= xsnprintf (p
, endp
- p
, "vCont");
4470 if (ptid_equal (ptid
, magic_null_ptid
))
4472 /* MAGIC_NULL_PTID means that we don't have any active threads,
4473 so we don't have any TID numbers the inferior will
4474 understand. Make sure to only send forms that do not specify
4476 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4478 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4480 /* Resume all threads (of all processes, or of a single
4481 process), with preference for INFERIOR_PTID. This assumes
4482 inferior_ptid belongs to the set of all threads we are about
4484 if (step
|| siggnal
!= TARGET_SIGNAL_0
)
4486 /* Step inferior_ptid, with or without signal. */
4487 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4490 /* And continue others without a signal. */
4491 append_resumption (p
, endp
, ptid
, /*step=*/ 0, TARGET_SIGNAL_0
);
4495 /* Scheduler locking; resume only PTID. */
4496 append_resumption (p
, endp
, ptid
, step
, siggnal
);
4499 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4504 /* In non-stop, the stub replies to vCont with "OK". The stop
4505 reply will be reported asynchronously by means of a `%Stop'
4507 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4508 if (strcmp (rs
->buf
, "OK") != 0)
4509 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4515 /* Tell the remote machine to resume. */
4517 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
4519 static int last_sent_step
;
4522 remote_resume (struct target_ops
*ops
,
4523 ptid_t ptid
, int step
, enum target_signal siggnal
)
4525 struct remote_state
*rs
= get_remote_state ();
4528 last_sent_signal
= siggnal
;
4529 last_sent_step
= step
;
4531 /* Update the inferior on signals to silently pass, if they've changed. */
4532 remote_pass_signals ();
4534 /* The vCont packet doesn't need to specify threads via Hc. */
4535 /* No reverse support (yet) for vCont. */
4536 if (execution_direction
!= EXEC_REVERSE
)
4537 if (remote_vcont_resume (ptid
, step
, siggnal
))
4540 /* All other supported resume packets do use Hc, so set the continue
4542 if (ptid_equal (ptid
, minus_one_ptid
))
4543 set_continue_thread (any_thread_ptid
);
4545 set_continue_thread (ptid
);
4548 if (execution_direction
== EXEC_REVERSE
)
4550 /* We don't pass signals to the target in reverse exec mode. */
4551 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
4552 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4556 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4557 error (_("Remote reverse-step not supported."));
4559 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4560 error (_("Remote reverse-continue not supported."));
4562 strcpy (buf
, step
? "bs" : "bc");
4564 else if (siggnal
!= TARGET_SIGNAL_0
)
4566 buf
[0] = step
? 'S' : 'C';
4567 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4568 buf
[2] = tohex (((int) siggnal
) & 0xf);
4572 strcpy (buf
, step
? "s" : "c");
4577 /* We are about to start executing the inferior, let's register it
4578 with the event loop. NOTE: this is the one place where all the
4579 execution commands end up. We could alternatively do this in each
4580 of the execution commands in infcmd.c. */
4581 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4582 into infcmd.c in order to allow inferior function calls to work
4583 NOT asynchronously. */
4584 if (target_can_async_p ())
4585 target_async (inferior_event_handler
, 0);
4587 /* We've just told the target to resume. The remote server will
4588 wait for the inferior to stop, and then send a stop reply. In
4589 the mean time, we can't start another command/query ourselves
4590 because the stub wouldn't be ready to process it. This applies
4591 only to the base all-stop protocol, however. In non-stop (which
4592 only supports vCont), the stub replies with an "OK", and is
4593 immediate able to process further serial input. */
4595 rs
->waiting_for_stop_reply
= 1;
4599 /* Set up the signal handler for SIGINT, while the target is
4600 executing, ovewriting the 'regular' SIGINT signal handler. */
4602 initialize_sigint_signal_handler (void)
4604 signal (SIGINT
, handle_remote_sigint
);
4607 /* Signal handler for SIGINT, while the target is executing. */
4609 handle_remote_sigint (int sig
)
4611 signal (sig
, handle_remote_sigint_twice
);
4612 mark_async_signal_handler_wrapper (sigint_remote_token
);
4615 /* Signal handler for SIGINT, installed after SIGINT has already been
4616 sent once. It will take effect the second time that the user sends
4619 handle_remote_sigint_twice (int sig
)
4621 signal (sig
, handle_remote_sigint
);
4622 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
4625 /* Perform the real interruption of the target execution, in response
4628 async_remote_interrupt (gdb_client_data arg
)
4631 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
4633 target_stop (inferior_ptid
);
4636 /* Perform interrupt, if the first attempt did not succeed. Just give
4637 up on the target alltogether. */
4639 async_remote_interrupt_twice (gdb_client_data arg
)
4642 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
4647 /* Reinstall the usual SIGINT handlers, after the target has
4650 cleanup_sigint_signal_handler (void *dummy
)
4652 signal (SIGINT
, handle_sigint
);
4655 /* Send ^C to target to halt it. Target will respond, and send us a
4657 static void (*ofunc
) (int);
4659 /* The command line interface's stop routine. This function is installed
4660 as a signal handler for SIGINT. The first time a user requests a
4661 stop, we call remote_stop to send a break or ^C. If there is no
4662 response from the target (it didn't stop when the user requested it),
4663 we ask the user if he'd like to detach from the target. */
4665 remote_interrupt (int signo
)
4667 /* If this doesn't work, try more severe steps. */
4668 signal (signo
, remote_interrupt_twice
);
4670 gdb_call_async_signal_handler (sigint_remote_token
, 1);
4673 /* The user typed ^C twice. */
4676 remote_interrupt_twice (int signo
)
4678 signal (signo
, ofunc
);
4679 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
4680 signal (signo
, remote_interrupt
);
4683 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4684 thread, all threads of a remote process, or all threads of all
4688 remote_stop_ns (ptid_t ptid
)
4690 struct remote_state
*rs
= get_remote_state ();
4692 char *endp
= rs
->buf
+ get_remote_packet_size ();
4694 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4695 remote_vcont_probe (rs
);
4697 if (!rs
->support_vCont_t
)
4698 error (_("Remote server does not support stopping threads"));
4700 if (ptid_equal (ptid
, minus_one_ptid
)
4701 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4702 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
4707 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
4709 if (ptid_is_pid (ptid
))
4710 /* All (-1) threads of process. */
4711 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4714 /* Small optimization: if we already have a stop reply for
4715 this thread, no use in telling the stub we want this
4717 if (peek_stop_reply (ptid
))
4723 write_ptid (p
, endp
, nptid
);
4726 /* In non-stop, we get an immediate OK reply. The stop reply will
4727 come in asynchronously by notification. */
4729 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4730 if (strcmp (rs
->buf
, "OK") != 0)
4731 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
4734 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4735 remote target. It is undefined which thread of which process
4736 reports the stop. */
4739 remote_stop_as (ptid_t ptid
)
4741 struct remote_state
*rs
= get_remote_state ();
4743 rs
->ctrlc_pending_p
= 1;
4745 /* If the inferior is stopped already, but the core didn't know
4746 about it yet, just ignore the request. The cached wait status
4747 will be collected in remote_wait. */
4748 if (rs
->cached_wait_status
)
4751 /* Send interrupt_sequence to remote target. */
4752 send_interrupt_sequence ();
4755 /* This is the generic stop called via the target vector. When a target
4756 interrupt is requested, either by the command line or the GUI, we
4757 will eventually end up here. */
4760 remote_stop (ptid_t ptid
)
4763 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
4766 remote_stop_ns (ptid
);
4768 remote_stop_as (ptid
);
4771 /* Ask the user what to do when an interrupt is received. */
4774 interrupt_query (void)
4776 target_terminal_ours ();
4778 if (target_can_async_p ())
4780 signal (SIGINT
, handle_sigint
);
4781 deprecated_throw_reason (RETURN_QUIT
);
4785 if (query (_("Interrupted while waiting for the program.\n\
4786 Give up (and stop debugging it)? ")))
4789 deprecated_throw_reason (RETURN_QUIT
);
4793 target_terminal_inferior ();
4796 /* Enable/disable target terminal ownership. Most targets can use
4797 terminal groups to control terminal ownership. Remote targets are
4798 different in that explicit transfer of ownership to/from GDB/target
4802 remote_terminal_inferior (void)
4804 if (!target_async_permitted
)
4805 /* Nothing to do. */
4808 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4809 idempotent. The event-loop GDB talking to an asynchronous target
4810 with a synchronous command calls this function from both
4811 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
4812 transfer the terminal to the target when it shouldn't this guard
4814 if (!remote_async_terminal_ours_p
)
4816 delete_file_handler (input_fd
);
4817 remote_async_terminal_ours_p
= 0;
4818 initialize_sigint_signal_handler ();
4819 /* NOTE: At this point we could also register our selves as the
4820 recipient of all input. Any characters typed could then be
4821 passed on down to the target. */
4825 remote_terminal_ours (void)
4827 if (!target_async_permitted
)
4828 /* Nothing to do. */
4831 /* See FIXME in remote_terminal_inferior. */
4832 if (remote_async_terminal_ours_p
)
4834 cleanup_sigint_signal_handler (NULL
);
4835 add_file_handler (input_fd
, stdin_event_handler
, 0);
4836 remote_async_terminal_ours_p
= 1;
4840 remote_console_output (char *msg
)
4844 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4847 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4851 fputs_unfiltered (tb
, gdb_stdtarg
);
4853 gdb_flush (gdb_stdtarg
);
4856 typedef struct cached_reg
4859 gdb_byte data
[MAX_REGISTER_SIZE
];
4862 DEF_VEC_O(cached_reg_t
);
4866 struct stop_reply
*next
;
4870 struct target_waitstatus ws
;
4872 VEC(cached_reg_t
) *regcache
;
4874 int stopped_by_watchpoint_p
;
4875 CORE_ADDR watch_data_address
;
4883 /* The list of already fetched and acknowledged stop events. */
4884 static struct stop_reply
*stop_reply_queue
;
4886 static struct stop_reply
*
4887 stop_reply_xmalloc (void)
4889 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4896 stop_reply_xfree (struct stop_reply
*r
)
4900 VEC_free (cached_reg_t
, r
->regcache
);
4905 /* Discard all pending stop replies of inferior PID. If PID is -1,
4906 discard everything. */
4909 discard_pending_stop_replies (int pid
)
4911 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4913 /* Discard the in-flight notification. */
4914 if (pending_stop_reply
!= NULL
4916 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4918 stop_reply_xfree (pending_stop_reply
);
4919 pending_stop_reply
= NULL
;
4922 /* Discard the stop replies we have already pulled with
4924 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4928 || ptid_get_pid (reply
->ptid
) == pid
)
4930 if (reply
== stop_reply_queue
)
4931 stop_reply_queue
= reply
->next
;
4933 prev
->next
= reply
->next
;
4935 stop_reply_xfree (reply
);
4942 /* Cleanup wrapper. */
4945 do_stop_reply_xfree (void *arg
)
4947 struct stop_reply
*r
= arg
;
4949 stop_reply_xfree (r
);
4952 /* Look for a queued stop reply belonging to PTID. If one is found,
4953 remove it from the queue, and return it. Returns NULL if none is
4954 found. If there are still queued events left to process, tell the
4955 event loop to get back to target_wait soon. */
4957 static struct stop_reply
*
4958 queued_stop_reply (ptid_t ptid
)
4960 struct stop_reply
*it
;
4961 struct stop_reply
**it_link
;
4963 it
= stop_reply_queue
;
4964 it_link
= &stop_reply_queue
;
4967 if (ptid_match (it
->ptid
, ptid
))
4969 *it_link
= it
->next
;
4974 it_link
= &it
->next
;
4978 if (stop_reply_queue
)
4979 /* There's still at least an event left. */
4980 mark_async_event_handler (remote_async_inferior_event_token
);
4985 /* Push a fully parsed stop reply in the stop reply queue. Since we
4986 know that we now have at least one queued event left to pass to the
4987 core side, tell the event loop to get back to target_wait soon. */
4990 push_stop_reply (struct stop_reply
*new_event
)
4992 struct stop_reply
*event
;
4994 if (stop_reply_queue
)
4996 for (event
= stop_reply_queue
;
4997 event
&& event
->next
;
4998 event
= event
->next
)
5001 event
->next
= new_event
;
5004 stop_reply_queue
= new_event
;
5006 mark_async_event_handler (remote_async_inferior_event_token
);
5009 /* Returns true if we have a stop reply for PTID. */
5012 peek_stop_reply (ptid_t ptid
)
5014 struct stop_reply
*it
;
5016 for (it
= stop_reply_queue
; it
; it
= it
->next
)
5017 if (ptid_equal (ptid
, it
->ptid
))
5019 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
5026 /* Parse the stop reply in BUF. Either the function succeeds, and the
5027 result is stored in EVENT, or throws an error. */
5030 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5032 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5036 event
->ptid
= null_ptid
;
5037 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5038 event
->ws
.value
.integer
= 0;
5039 event
->solibs_changed
= 0;
5040 event
->replay_event
= 0;
5041 event
->stopped_by_watchpoint_p
= 0;
5042 event
->regcache
= NULL
;
5047 case 'T': /* Status with PC, SP, FP, ... */
5048 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5049 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5051 n... = register number
5052 r... = register contents
5055 p
= &buf
[3]; /* after Txx */
5063 /* If the packet contains a register number, save it in
5064 pnum and set p1 to point to the character following it.
5065 Otherwise p1 points to p. */
5067 /* If this packet is an awatch packet, don't parse the 'a'
5068 as a register number. */
5070 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5071 && strncmp (p
, "core", strlen ("core") != 0))
5073 /* Read the ``P'' register number. */
5074 pnum
= strtol (p
, &p_temp
, 16);
5080 if (p1
== p
) /* No register number present here. */
5082 p1
= strchr (p
, ':');
5084 error (_("Malformed packet(a) (missing colon): %s\n\
5087 if (strncmp (p
, "thread", p1
- p
) == 0)
5088 event
->ptid
= read_ptid (++p1
, &p
);
5089 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5090 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5091 || (strncmp (p
, "awatch", p1
- p
) == 0))
5093 event
->stopped_by_watchpoint_p
= 1;
5094 p
= unpack_varlen_hex (++p1
, &addr
);
5095 event
->watch_data_address
= (CORE_ADDR
) addr
;
5097 else if (strncmp (p
, "library", p1
- p
) == 0)
5101 while (*p_temp
&& *p_temp
!= ';')
5104 event
->solibs_changed
= 1;
5107 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5109 /* NO_HISTORY event.
5110 p1 will indicate "begin" or "end", but
5111 it makes no difference for now, so ignore it. */
5112 event
->replay_event
= 1;
5113 p_temp
= strchr (p1
+ 1, ';');
5117 else if (strncmp (p
, "core", p1
- p
) == 0)
5121 p
= unpack_varlen_hex (++p1
, &c
);
5126 /* Silently skip unknown optional info. */
5127 p_temp
= strchr (p1
+ 1, ';');
5134 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5135 cached_reg_t cached_reg
;
5140 error (_("Malformed packet(b) (missing colon): %s\n\
5146 error (_("Remote sent bad register number %s: %s\n\
5148 hex_string (pnum
), p
, buf
);
5150 cached_reg
.num
= reg
->regnum
;
5152 fieldsize
= hex2bin (p
, cached_reg
.data
,
5153 register_size (target_gdbarch
,
5156 if (fieldsize
< register_size (target_gdbarch
,
5158 warning (_("Remote reply is too short: %s"), buf
);
5160 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5164 error (_("Remote register badly formatted: %s\nhere: %s"),
5169 case 'S': /* Old style status, just signal only. */
5170 if (event
->solibs_changed
)
5171 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5172 else if (event
->replay_event
)
5173 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5176 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5177 event
->ws
.value
.sig
= (enum target_signal
)
5178 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
5181 case 'W': /* Target exited. */
5188 /* GDB used to accept only 2 hex chars here. Stubs should
5189 only send more if they detect GDB supports multi-process
5191 p
= unpack_varlen_hex (&buf
[1], &value
);
5195 /* The remote process exited. */
5196 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5197 event
->ws
.value
.integer
= value
;
5201 /* The remote process exited with a signal. */
5202 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5203 event
->ws
.value
.sig
= (enum target_signal
) value
;
5206 /* If no process is specified, assume inferior_ptid. */
5207 pid
= ptid_get_pid (inferior_ptid
);
5216 else if (strncmp (p
,
5217 "process:", sizeof ("process:") - 1) == 0)
5221 p
+= sizeof ("process:") - 1;
5222 unpack_varlen_hex (p
, &upid
);
5226 error (_("unknown stop reply packet: %s"), buf
);
5229 error (_("unknown stop reply packet: %s"), buf
);
5230 event
->ptid
= pid_to_ptid (pid
);
5235 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5236 error (_("No process or thread specified in stop reply: %s"), buf
);
5239 /* When the stub wants to tell GDB about a new stop reply, it sends a
5240 stop notification (%Stop). Those can come it at any time, hence,
5241 we have to make sure that any pending putpkt/getpkt sequence we're
5242 making is finished, before querying the stub for more events with
5243 vStopped. E.g., if we started a vStopped sequence immediatelly
5244 upon receiving the %Stop notification, something like this could
5252 1.6) <-- (registers reply to step #1.3)
5254 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5257 To solve this, whenever we parse a %Stop notification sucessfully,
5258 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5259 doing whatever we were doing:
5265 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5266 2.5) <-- (registers reply to step #2.3)
5268 Eventualy after step #2.5, we return to the event loop, which
5269 notices there's an event on the
5270 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5271 associated callback --- the function below. At this point, we're
5272 always safe to start a vStopped sequence. :
5275 2.7) <-- T05 thread:2
5281 remote_get_pending_stop_replies (void)
5283 struct remote_state
*rs
= get_remote_state ();
5285 if (pending_stop_reply
)
5288 putpkt ("vStopped");
5290 /* Now we can rely on it. */
5291 push_stop_reply (pending_stop_reply
);
5292 pending_stop_reply
= NULL
;
5296 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5297 if (strcmp (rs
->buf
, "OK") == 0)
5301 struct cleanup
*old_chain
;
5302 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
5304 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5305 remote_parse_stop_reply (rs
->buf
, stop_reply
);
5308 putpkt ("vStopped");
5310 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5312 /* Now we can rely on it. */
5313 discard_cleanups (old_chain
);
5314 push_stop_reply (stop_reply
);
5317 /* We got an unknown stop reply. */
5318 do_cleanups (old_chain
);
5325 /* Called when it is decided that STOP_REPLY holds the info of the
5326 event that is to be returned to the core. This function always
5327 destroys STOP_REPLY. */
5330 process_stop_reply (struct stop_reply
*stop_reply
,
5331 struct target_waitstatus
*status
)
5335 *status
= stop_reply
->ws
;
5336 ptid
= stop_reply
->ptid
;
5338 /* If no thread/process was reported by the stub, assume the current
5340 if (ptid_equal (ptid
, null_ptid
))
5341 ptid
= inferior_ptid
;
5343 if (status
->kind
!= TARGET_WAITKIND_EXITED
5344 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5346 /* Expedited registers. */
5347 if (stop_reply
->regcache
)
5349 struct regcache
*regcache
5350 = get_thread_arch_regcache (ptid
, target_gdbarch
);
5355 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5357 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5358 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5361 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5362 remote_watch_data_address
= stop_reply
->watch_data_address
;
5364 remote_notice_new_inferior (ptid
, 0);
5365 demand_private_info (ptid
)->core
= stop_reply
->core
;
5368 stop_reply_xfree (stop_reply
);
5372 /* The non-stop mode version of target_wait. */
5375 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5377 struct remote_state
*rs
= get_remote_state ();
5378 struct stop_reply
*stop_reply
;
5381 /* If in non-stop mode, get out of getpkt even if a
5382 notification is received. */
5384 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5391 case 'E': /* Error of some sort. */
5392 /* We're out of sync with the target now. Did it continue
5393 or not? We can't tell which thread it was in non-stop,
5394 so just ignore this. */
5395 warning (_("Remote failure reply: %s"), rs
->buf
);
5397 case 'O': /* Console output. */
5398 remote_console_output (rs
->buf
+ 1);
5401 warning (_("Invalid remote reply: %s"), rs
->buf
);
5405 /* Acknowledge a pending stop reply that may have arrived in the
5407 if (pending_stop_reply
!= NULL
)
5408 remote_get_pending_stop_replies ();
5410 /* If indeed we noticed a stop reply, we're done. */
5411 stop_reply
= queued_stop_reply (ptid
);
5412 if (stop_reply
!= NULL
)
5413 return process_stop_reply (stop_reply
, status
);
5415 /* Still no event. If we're just polling for an event, then
5416 return to the event loop. */
5417 if (options
& TARGET_WNOHANG
)
5419 status
->kind
= TARGET_WAITKIND_IGNORE
;
5420 return minus_one_ptid
;
5423 /* Otherwise do a blocking wait. */
5424 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5429 /* Wait until the remote machine stops, then return, storing status in
5430 STATUS just as `wait' would. */
5433 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5435 struct remote_state
*rs
= get_remote_state ();
5436 ptid_t event_ptid
= null_ptid
;
5438 struct stop_reply
*stop_reply
;
5442 status
->kind
= TARGET_WAITKIND_IGNORE
;
5443 status
->value
.integer
= 0;
5445 stop_reply
= queued_stop_reply (ptid
);
5446 if (stop_reply
!= NULL
)
5447 return process_stop_reply (stop_reply
, status
);
5449 if (rs
->cached_wait_status
)
5450 /* Use the cached wait status, but only once. */
5451 rs
->cached_wait_status
= 0;
5456 if (!target_is_async_p ())
5458 ofunc
= signal (SIGINT
, remote_interrupt
);
5459 /* If the user hit C-c before this packet, or between packets,
5460 pretend that it was hit right here. */
5464 remote_interrupt (SIGINT
);
5468 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5469 _never_ wait for ever -> test on target_is_async_p().
5470 However, before we do that we need to ensure that the caller
5471 knows how to take the target into/out of async mode. */
5472 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
5473 if (!target_is_async_p ())
5474 signal (SIGINT
, ofunc
);
5479 remote_stopped_by_watchpoint_p
= 0;
5481 /* We got something. */
5482 rs
->waiting_for_stop_reply
= 0;
5484 /* Assume that the target has acknowledged Ctrl-C unless we receive
5485 an 'F' or 'O' packet. */
5486 if (buf
[0] != 'F' && buf
[0] != 'O')
5487 rs
->ctrlc_pending_p
= 0;
5491 case 'E': /* Error of some sort. */
5492 /* We're out of sync with the target now. Did it continue or
5493 not? Not is more likely, so report a stop. */
5494 warning (_("Remote failure reply: %s"), buf
);
5495 status
->kind
= TARGET_WAITKIND_STOPPED
;
5496 status
->value
.sig
= TARGET_SIGNAL_0
;
5498 case 'F': /* File-I/O request. */
5499 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5500 rs
->ctrlc_pending_p
= 0;
5502 case 'T': case 'S': case 'X': case 'W':
5504 struct stop_reply
*stop_reply
;
5505 struct cleanup
*old_chain
;
5507 stop_reply
= stop_reply_xmalloc ();
5508 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5509 remote_parse_stop_reply (buf
, stop_reply
);
5510 discard_cleanups (old_chain
);
5511 event_ptid
= process_stop_reply (stop_reply
, status
);
5514 case 'O': /* Console output. */
5515 remote_console_output (buf
+ 1);
5517 /* The target didn't really stop; keep waiting. */
5518 rs
->waiting_for_stop_reply
= 1;
5522 if (last_sent_signal
!= TARGET_SIGNAL_0
)
5524 /* Zero length reply means that we tried 'S' or 'C' and the
5525 remote system doesn't support it. */
5526 target_terminal_ours_for_output ();
5528 ("Can't send signals to this remote system. %s not sent.\n",
5529 target_signal_to_name (last_sent_signal
));
5530 last_sent_signal
= TARGET_SIGNAL_0
;
5531 target_terminal_inferior ();
5533 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
5534 putpkt ((char *) buf
);
5536 /* We just told the target to resume, so a stop reply is in
5538 rs
->waiting_for_stop_reply
= 1;
5541 /* else fallthrough */
5543 warning (_("Invalid remote reply: %s"), buf
);
5545 rs
->waiting_for_stop_reply
= 1;
5549 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
5551 /* Nothing interesting happened. If we're doing a non-blocking
5552 poll, we're done. Otherwise, go back to waiting. */
5553 if (options
& TARGET_WNOHANG
)
5554 return minus_one_ptid
;
5558 else if (status
->kind
!= TARGET_WAITKIND_EXITED
5559 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5561 if (!ptid_equal (event_ptid
, null_ptid
))
5562 record_currthread (event_ptid
);
5564 event_ptid
= inferior_ptid
;
5567 /* A process exit. Invalidate our notion of current thread. */
5568 record_currthread (minus_one_ptid
);
5573 /* Wait until the remote machine stops, then return, storing status in
5574 STATUS just as `wait' would. */
5577 remote_wait (struct target_ops
*ops
,
5578 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5583 event_ptid
= remote_wait_ns (ptid
, status
, options
);
5585 event_ptid
= remote_wait_as (ptid
, status
, options
);
5587 if (target_can_async_p ())
5589 /* If there are are events left in the queue tell the event loop
5591 if (stop_reply_queue
)
5592 mark_async_event_handler (remote_async_inferior_event_token
);
5598 /* Fetch a single register using a 'p' packet. */
5601 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
5603 struct remote_state
*rs
= get_remote_state ();
5605 char regp
[MAX_REGISTER_SIZE
];
5608 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
5611 if (reg
->pnum
== -1)
5616 p
+= hexnumstr (p
, reg
->pnum
);
5619 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5623 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
5627 case PACKET_UNKNOWN
:
5630 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5631 gdbarch_register_name (get_regcache_arch (regcache
),
5636 /* If this register is unfetchable, tell the regcache. */
5639 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5643 /* Otherwise, parse and supply the value. */
5649 error (_("fetch_register_using_p: early buf termination"));
5651 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5654 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
5658 /* Fetch the registers included in the target's 'g' packet. */
5661 send_g_packet (void)
5663 struct remote_state
*rs
= get_remote_state ();
5666 sprintf (rs
->buf
, "g");
5667 remote_send (&rs
->buf
, &rs
->buf_size
);
5669 /* We can get out of synch in various cases. If the first character
5670 in the buffer is not a hex character, assume that has happened
5671 and try to fetch another packet to read. */
5672 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
5673 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
5674 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
5675 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
5678 fprintf_unfiltered (gdb_stdlog
,
5679 "Bad register packet; fetching a new packet\n");
5680 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5683 buf_len
= strlen (rs
->buf
);
5685 /* Sanity check the received packet. */
5686 if (buf_len
% 2 != 0)
5687 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
5693 process_g_packet (struct regcache
*regcache
)
5695 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5696 struct remote_state
*rs
= get_remote_state ();
5697 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5702 buf_len
= strlen (rs
->buf
);
5704 /* Further sanity checks, with knowledge of the architecture. */
5705 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
5706 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
5708 /* Save the size of the packet sent to us by the target. It is used
5709 as a heuristic when determining the max size of packets that the
5710 target can safely receive. */
5711 if (rsa
->actual_register_packet_size
== 0)
5712 rsa
->actual_register_packet_size
= buf_len
;
5714 /* If this is smaller than we guessed the 'g' packet would be,
5715 update our records. A 'g' reply that doesn't include a register's
5716 value implies either that the register is not available, or that
5717 the 'p' packet must be used. */
5718 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
5720 rsa
->sizeof_g_packet
= buf_len
/ 2;
5722 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5724 if (rsa
->regs
[i
].pnum
== -1)
5727 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
5728 rsa
->regs
[i
].in_g_packet
= 0;
5730 rsa
->regs
[i
].in_g_packet
= 1;
5734 regs
= alloca (rsa
->sizeof_g_packet
);
5736 /* Unimplemented registers read as all bits zero. */
5737 memset (regs
, 0, rsa
->sizeof_g_packet
);
5739 /* Reply describes registers byte by byte, each byte encoded as two
5740 hex characters. Suck them all up, then supply them to the
5741 register cacheing/storage mechanism. */
5744 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
5746 if (p
[0] == 0 || p
[1] == 0)
5747 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5748 internal_error (__FILE__
, __LINE__
,
5749 _("unexpected end of 'g' packet reply"));
5751 if (p
[0] == 'x' && p
[1] == 'x')
5752 regs
[i
] = 0; /* 'x' */
5754 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5758 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5760 struct packet_reg
*r
= &rsa
->regs
[i
];
5764 if (r
->offset
* 2 >= strlen (rs
->buf
))
5765 /* This shouldn't happen - we adjusted in_g_packet above. */
5766 internal_error (__FILE__
, __LINE__
,
5767 _("unexpected end of 'g' packet reply"));
5768 else if (rs
->buf
[r
->offset
* 2] == 'x')
5770 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
5771 /* The register isn't available, mark it as such (at
5772 the same time setting the value to zero). */
5773 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
5776 regcache_raw_supply (regcache
, r
->regnum
,
5783 fetch_registers_using_g (struct regcache
*regcache
)
5786 process_g_packet (regcache
);
5789 /* Make the remote selected traceframe match GDB's selected
5793 set_remote_traceframe (void)
5797 if (remote_traceframe_number
== get_traceframe_number ())
5800 /* Avoid recursion, remote_trace_find calls us again. */
5801 remote_traceframe_number
= get_traceframe_number ();
5803 newnum
= target_trace_find (tfind_number
,
5804 get_traceframe_number (), 0, 0, NULL
);
5806 /* Should not happen. If it does, all bets are off. */
5807 if (newnum
!= get_traceframe_number ())
5808 warning (_("could not set remote traceframe"));
5812 remote_fetch_registers (struct target_ops
*ops
,
5813 struct regcache
*regcache
, int regnum
)
5815 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5818 set_remote_traceframe ();
5819 set_general_thread (inferior_ptid
);
5823 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5825 gdb_assert (reg
!= NULL
);
5827 /* If this register might be in the 'g' packet, try that first -
5828 we are likely to read more than one register. If this is the
5829 first 'g' packet, we might be overly optimistic about its
5830 contents, so fall back to 'p'. */
5831 if (reg
->in_g_packet
)
5833 fetch_registers_using_g (regcache
);
5834 if (reg
->in_g_packet
)
5838 if (fetch_register_using_p (regcache
, reg
))
5841 /* This register is not available. */
5842 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5847 fetch_registers_using_g (regcache
);
5849 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5850 if (!rsa
->regs
[i
].in_g_packet
)
5851 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5853 /* This register is not available. */
5854 regcache_raw_supply (regcache
, i
, NULL
);
5858 /* Prepare to store registers. Since we may send them all (using a
5859 'G' request), we have to read out the ones we don't want to change
5863 remote_prepare_to_store (struct regcache
*regcache
)
5865 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5867 gdb_byte buf
[MAX_REGISTER_SIZE
];
5869 /* Make sure the entire registers array is valid. */
5870 switch (remote_protocol_packets
[PACKET_P
].support
)
5872 case PACKET_DISABLE
:
5873 case PACKET_SUPPORT_UNKNOWN
:
5874 /* Make sure all the necessary registers are cached. */
5875 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5876 if (rsa
->regs
[i
].in_g_packet
)
5877 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5884 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5885 packet was not recognized. */
5888 store_register_using_P (const struct regcache
*regcache
,
5889 struct packet_reg
*reg
)
5891 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5892 struct remote_state
*rs
= get_remote_state ();
5893 /* Try storing a single register. */
5894 char *buf
= rs
->buf
;
5895 gdb_byte regp
[MAX_REGISTER_SIZE
];
5898 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5901 if (reg
->pnum
== -1)
5904 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5905 p
= buf
+ strlen (buf
);
5906 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5907 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5909 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5911 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5916 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
5917 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
5918 case PACKET_UNKNOWN
:
5921 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5925 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5926 contents of the register cache buffer. FIXME: ignores errors. */
5929 store_registers_using_G (const struct regcache
*regcache
)
5931 struct remote_state
*rs
= get_remote_state ();
5932 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5936 /* Extract all the registers in the regcache copying them into a
5941 regs
= alloca (rsa
->sizeof_g_packet
);
5942 memset (regs
, 0, rsa
->sizeof_g_packet
);
5943 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5945 struct packet_reg
*r
= &rsa
->regs
[i
];
5948 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5952 /* Command describes registers byte by byte,
5953 each byte encoded as two hex characters. */
5956 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5958 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5960 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5961 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
5962 error (_("Could not write registers; remote failure reply '%s'"),
5966 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5967 of the register cache buffer. FIXME: ignores errors. */
5970 remote_store_registers (struct target_ops
*ops
,
5971 struct regcache
*regcache
, int regnum
)
5973 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5976 set_remote_traceframe ();
5977 set_general_thread (inferior_ptid
);
5981 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5983 gdb_assert (reg
!= NULL
);
5985 /* Always prefer to store registers using the 'P' packet if
5986 possible; we often change only a small number of registers.
5987 Sometimes we change a larger number; we'd need help from a
5988 higher layer to know to use 'G'. */
5989 if (store_register_using_P (regcache
, reg
))
5992 /* For now, don't complain if we have no way to write the
5993 register. GDB loses track of unavailable registers too
5994 easily. Some day, this may be an error. We don't have
5995 any way to read the register, either... */
5996 if (!reg
->in_g_packet
)
5999 store_registers_using_G (regcache
);
6003 store_registers_using_G (regcache
);
6005 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6006 if (!rsa
->regs
[i
].in_g_packet
)
6007 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
6008 /* See above for why we do not issue an error here. */
6013 /* Return the number of hex digits in num. */
6016 hexnumlen (ULONGEST num
)
6020 for (i
= 0; num
!= 0; i
++)
6026 /* Set BUF to the minimum number of hex digits representing NUM. */
6029 hexnumstr (char *buf
, ULONGEST num
)
6031 int len
= hexnumlen (num
);
6033 return hexnumnstr (buf
, num
, len
);
6037 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6040 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6046 for (i
= width
- 1; i
>= 0; i
--)
6048 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6055 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6058 remote_address_masked (CORE_ADDR addr
)
6060 int address_size
= remote_address_size
;
6062 /* If "remoteaddresssize" was not set, default to target address size. */
6064 address_size
= gdbarch_addr_bit (target_gdbarch
);
6066 if (address_size
> 0
6067 && address_size
< (sizeof (ULONGEST
) * 8))
6069 /* Only create a mask when that mask can safely be constructed
6070 in a ULONGEST variable. */
6073 mask
= (mask
<< address_size
) - 1;
6079 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6080 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6081 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6082 (which may be more than *OUT_LEN due to escape characters). The
6083 total number of bytes in the output buffer will be at most
6087 remote_escape_output (const gdb_byte
*buffer
, int len
,
6088 gdb_byte
*out_buf
, int *out_len
,
6091 int input_index
, output_index
;
6094 for (input_index
= 0; input_index
< len
; input_index
++)
6096 gdb_byte b
= buffer
[input_index
];
6098 if (b
== '$' || b
== '#' || b
== '}')
6100 /* These must be escaped. */
6101 if (output_index
+ 2 > out_maxlen
)
6103 out_buf
[output_index
++] = '}';
6104 out_buf
[output_index
++] = b
^ 0x20;
6108 if (output_index
+ 1 > out_maxlen
)
6110 out_buf
[output_index
++] = b
;
6114 *out_len
= input_index
;
6115 return output_index
;
6118 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6119 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6120 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6122 This function reverses remote_escape_output. It allows more
6123 escaped characters than that function does, in particular because
6124 '*' must be escaped to avoid the run-length encoding processing
6125 in reading packets. */
6128 remote_unescape_input (const gdb_byte
*buffer
, int len
,
6129 gdb_byte
*out_buf
, int out_maxlen
)
6131 int input_index
, output_index
;
6136 for (input_index
= 0; input_index
< len
; input_index
++)
6138 gdb_byte b
= buffer
[input_index
];
6140 if (output_index
+ 1 > out_maxlen
)
6142 warning (_("Received too much data from remote target;"
6143 " ignoring overflow."));
6144 return output_index
;
6149 out_buf
[output_index
++] = b
^ 0x20;
6155 out_buf
[output_index
++] = b
;
6159 error (_("Unmatched escape character in target response."));
6161 return output_index
;
6164 /* Determine whether the remote target supports binary downloading.
6165 This is accomplished by sending a no-op memory write of zero length
6166 to the target at the specified address. It does not suffice to send
6167 the whole packet, since many stubs strip the eighth bit and
6168 subsequently compute a wrong checksum, which causes real havoc with
6171 NOTE: This can still lose if the serial line is not eight-bit
6172 clean. In cases like this, the user should clear "remote
6176 check_binary_download (CORE_ADDR addr
)
6178 struct remote_state
*rs
= get_remote_state ();
6180 switch (remote_protocol_packets
[PACKET_X
].support
)
6182 case PACKET_DISABLE
:
6186 case PACKET_SUPPORT_UNKNOWN
:
6192 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6194 p
+= hexnumstr (p
, (ULONGEST
) 0);
6198 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6199 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6201 if (rs
->buf
[0] == '\0')
6204 fprintf_unfiltered (gdb_stdlog
,
6205 "binary downloading NOT "
6206 "supported by target\n");
6207 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6212 fprintf_unfiltered (gdb_stdlog
,
6213 "binary downloading suppported by target\n");
6214 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6221 /* Write memory data directly to the remote machine.
6222 This does not inform the data cache; the data cache uses this.
6223 HEADER is the starting part of the packet.
6224 MEMADDR is the address in the remote memory space.
6225 MYADDR is the address of the buffer in our space.
6226 LEN is the number of bytes.
6227 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6228 should send data as binary ('X'), or hex-encoded ('M').
6230 The function creates packet of the form
6231 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6233 where encoding of <DATA> is termined by PACKET_FORMAT.
6235 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6238 Returns the number of bytes transferred, or 0 (setting errno) for
6239 error. Only transfer a single packet. */
6242 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6243 const gdb_byte
*myaddr
, int len
,
6244 char packet_format
, int use_length
)
6246 struct remote_state
*rs
= get_remote_state ();
6256 if (packet_format
!= 'X' && packet_format
!= 'M')
6257 internal_error (__FILE__
, __LINE__
,
6258 _("remote_write_bytes_aux: bad packet format"));
6263 payload_size
= get_memory_write_packet_size ();
6265 /* The packet buffer will be large enough for the payload;
6266 get_memory_packet_size ensures this. */
6269 /* Compute the size of the actual payload by subtracting out the
6270 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6272 payload_size
-= strlen ("$,:#NN");
6274 /* The comma won't be used. */
6276 header_length
= strlen (header
);
6277 payload_size
-= header_length
;
6278 payload_size
-= hexnumlen (memaddr
);
6280 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6282 strcat (rs
->buf
, header
);
6283 p
= rs
->buf
+ strlen (header
);
6285 /* Compute a best guess of the number of bytes actually transfered. */
6286 if (packet_format
== 'X')
6288 /* Best guess at number of bytes that will fit. */
6289 todo
= min (len
, payload_size
);
6291 payload_size
-= hexnumlen (todo
);
6292 todo
= min (todo
, payload_size
);
6296 /* Num bytes that will fit. */
6297 todo
= min (len
, payload_size
/ 2);
6299 payload_size
-= hexnumlen (todo
);
6300 todo
= min (todo
, payload_size
/ 2);
6304 internal_error (__FILE__
, __LINE__
,
6305 _("minumum packet size too small to write data"));
6307 /* If we already need another packet, then try to align the end
6308 of this packet to a useful boundary. */
6309 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6310 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6312 /* Append "<memaddr>". */
6313 memaddr
= remote_address_masked (memaddr
);
6314 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6321 /* Append <len>. Retain the location/size of <len>. It may need to
6322 be adjusted once the packet body has been created. */
6324 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6332 /* Append the packet body. */
6333 if (packet_format
== 'X')
6335 /* Binary mode. Send target system values byte by byte, in
6336 increasing byte addresses. Only escape certain critical
6338 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
6341 /* If not all TODO bytes fit, then we'll need another packet. Make
6342 a second try to keep the end of the packet aligned. Don't do
6343 this if the packet is tiny. */
6344 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6348 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6350 if (new_nr_bytes
!= nr_bytes
)
6351 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6356 p
+= payload_length
;
6357 if (use_length
&& nr_bytes
< todo
)
6359 /* Escape chars have filled up the buffer prematurely,
6360 and we have actually sent fewer bytes than planned.
6361 Fix-up the length field of the packet. Use the same
6362 number of characters as before. */
6363 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6364 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6369 /* Normal mode: Send target system values byte by byte, in
6370 increasing byte addresses. Each byte is encoded as a two hex
6372 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6376 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6377 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6379 if (rs
->buf
[0] == 'E')
6381 /* There is no correspondance between what the remote protocol
6382 uses for errors and errno codes. We would like a cleaner way
6383 of representing errors (big enough to include errno codes,
6384 bfd_error codes, and others). But for now just return EIO. */
6389 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6390 fewer bytes than we'd planned. */
6394 /* Write memory data directly to the remote machine.
6395 This does not inform the data cache; the data cache uses this.
6396 MEMADDR is the address in the remote memory space.
6397 MYADDR is the address of the buffer in our space.
6398 LEN is the number of bytes.
6400 Returns number of bytes transferred, or 0 (setting errno) for
6401 error. Only transfer a single packet. */
6404 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
6406 char *packet_format
= 0;
6408 /* Check whether the target supports binary download. */
6409 check_binary_download (memaddr
);
6411 switch (remote_protocol_packets
[PACKET_X
].support
)
6414 packet_format
= "X";
6416 case PACKET_DISABLE
:
6417 packet_format
= "M";
6419 case PACKET_SUPPORT_UNKNOWN
:
6420 internal_error (__FILE__
, __LINE__
,
6421 _("remote_write_bytes: bad internal state"));
6423 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6426 return remote_write_bytes_aux (packet_format
,
6427 memaddr
, myaddr
, len
, packet_format
[0], 1);
6430 /* Read memory data directly from the remote machine.
6431 This does not use the data cache; the data cache uses this.
6432 MEMADDR is the address in the remote memory space.
6433 MYADDR is the address of the buffer in our space.
6434 LEN is the number of bytes.
6436 Returns number of bytes transferred, or 0 for error. */
6439 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
6441 struct remote_state
*rs
= get_remote_state ();
6442 int max_buf_size
; /* Max size of packet output buffer. */
6450 max_buf_size
= get_memory_read_packet_size ();
6451 /* The packet buffer will be large enough for the payload;
6452 get_memory_packet_size ensures this. */
6454 /* Number if bytes that will fit. */
6455 todo
= min (len
, max_buf_size
/ 2);
6457 /* Construct "m"<memaddr>","<len>". */
6458 memaddr
= remote_address_masked (memaddr
);
6461 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6463 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6466 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6467 if (rs
->buf
[0] == 'E'
6468 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6469 && rs
->buf
[3] == '\0')
6471 /* There is no correspondance between what the remote protocol
6472 uses for errors and errno codes. We would like a cleaner way
6473 of representing errors (big enough to include errno codes,
6474 bfd_error codes, and others). But for now just return
6479 /* Reply describes memory byte by byte, each byte encoded as two hex
6482 i
= hex2bin (p
, myaddr
, todo
);
6483 /* Return what we have. Let higher layers handle partial reads. */
6488 /* Remote notification handler. */
6491 handle_notification (char *buf
, size_t length
)
6493 if (strncmp (buf
, "Stop:", 5) == 0)
6495 if (pending_stop_reply
)
6497 /* We've already parsed the in-flight stop-reply, but the
6498 stub for some reason thought we didn't, possibly due to
6499 timeout on its side. Just ignore it. */
6501 fprintf_unfiltered (gdb_stdlog
, "ignoring resent notification\n");
6505 struct cleanup
*old_chain
;
6506 struct stop_reply
*reply
= stop_reply_xmalloc ();
6508 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
6510 remote_parse_stop_reply (buf
+ 5, reply
);
6512 discard_cleanups (old_chain
);
6514 /* Be careful to only set it after parsing, since an error
6515 may be thrown then. */
6516 pending_stop_reply
= reply
;
6518 /* Notify the event loop there's a stop reply to acknowledge
6519 and that there may be more events to fetch. */
6520 mark_async_event_handler (remote_async_get_pending_events_token
);
6523 fprintf_unfiltered (gdb_stdlog
, "stop notification captured\n");
6527 /* We ignore notifications we don't recognize, for compatibility
6528 with newer stubs. */
6533 /* Read or write LEN bytes from inferior memory at MEMADDR,
6534 transferring to or from debugger address BUFFER. Write to inferior
6535 if SHOULD_WRITE is nonzero. Returns length of data written or
6536 read; 0 for error. TARGET is unused. */
6539 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
6540 int should_write
, struct mem_attrib
*attrib
,
6541 struct target_ops
*target
)
6545 set_remote_traceframe ();
6546 set_general_thread (inferior_ptid
);
6549 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
6551 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
6556 /* Sends a packet with content determined by the printf format string
6557 FORMAT and the remaining arguments, then gets the reply. Returns
6558 whether the packet was a success, a failure, or unknown. */
6560 static enum packet_result
6561 remote_send_printf (const char *format
, ...)
6563 struct remote_state
*rs
= get_remote_state ();
6564 int max_size
= get_remote_packet_size ();
6567 va_start (ap
, format
);
6570 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6571 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
6573 if (putpkt (rs
->buf
) < 0)
6574 error (_("Communication problem with target."));
6577 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6579 return packet_check_result (rs
->buf
);
6583 restore_remote_timeout (void *p
)
6585 int value
= *(int *)p
;
6587 remote_timeout
= value
;
6590 /* Flash writing can take quite some time. We'll set
6591 effectively infinite timeout for flash operations.
6592 In future, we'll need to decide on a better approach. */
6593 static const int remote_flash_timeout
= 1000;
6596 remote_flash_erase (struct target_ops
*ops
,
6597 ULONGEST address
, LONGEST length
)
6599 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
6600 int saved_remote_timeout
= remote_timeout
;
6601 enum packet_result ret
;
6602 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6603 &saved_remote_timeout
);
6605 remote_timeout
= remote_flash_timeout
;
6607 ret
= remote_send_printf ("vFlashErase:%s,%s",
6608 phex (address
, addr_size
),
6612 case PACKET_UNKNOWN
:
6613 error (_("Remote target does not support flash erase"));
6615 error (_("Error erasing flash with vFlashErase packet"));
6620 do_cleanups (back_to
);
6624 remote_flash_write (struct target_ops
*ops
,
6625 ULONGEST address
, LONGEST length
,
6626 const gdb_byte
*data
)
6628 int saved_remote_timeout
= remote_timeout
;
6630 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6631 &saved_remote_timeout
);
6633 remote_timeout
= remote_flash_timeout
;
6634 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
6635 do_cleanups (back_to
);
6641 remote_flash_done (struct target_ops
*ops
)
6643 int saved_remote_timeout
= remote_timeout
;
6645 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6646 &saved_remote_timeout
);
6648 remote_timeout
= remote_flash_timeout
;
6649 ret
= remote_send_printf ("vFlashDone");
6650 do_cleanups (back_to
);
6654 case PACKET_UNKNOWN
:
6655 error (_("Remote target does not support vFlashDone"));
6657 error (_("Error finishing flash operation"));
6664 remote_files_info (struct target_ops
*ignore
)
6666 puts_filtered ("Debugging a target over a serial line.\n");
6669 /* Stuff for dealing with the packets which are part of this protocol.
6670 See comment at top of file for details. */
6672 /* Read a single character from the remote end. */
6675 readchar (int timeout
)
6679 ch
= serial_readchar (remote_desc
, timeout
);
6684 switch ((enum serial_rc
) ch
)
6688 error (_("Remote connection closed"));
6692 perror_with_name (_("Remote communication error. "
6693 "Target disconnected."));
6695 case SERIAL_TIMEOUT
:
6701 /* Send the command in *BUF to the remote machine, and read the reply
6702 into *BUF. Report an error if we get an error reply. Resize
6703 *BUF using xrealloc if necessary to hold the result, and update
6707 remote_send (char **buf
,
6711 getpkt (buf
, sizeof_buf
, 0);
6713 if ((*buf
)[0] == 'E')
6714 error (_("Remote failure reply: %s"), *buf
);
6717 /* Return a pointer to an xmalloc'ed string representing an escaped
6718 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6719 etc. The caller is responsible for releasing the returned
6723 escape_buffer (const char *buf
, int n
)
6725 struct cleanup
*old_chain
;
6726 struct ui_file
*stb
;
6729 stb
= mem_fileopen ();
6730 old_chain
= make_cleanup_ui_file_delete (stb
);
6732 fputstrn_unfiltered (buf
, n
, 0, stb
);
6733 str
= ui_file_xstrdup (stb
, NULL
);
6734 do_cleanups (old_chain
);
6738 /* Display a null-terminated packet on stdout, for debugging, using C
6742 print_packet (char *buf
)
6744 puts_filtered ("\"");
6745 fputstr_filtered (buf
, '"', gdb_stdout
);
6746 puts_filtered ("\"");
6752 return putpkt_binary (buf
, strlen (buf
));
6755 /* Send a packet to the remote machine, with error checking. The data
6756 of the packet is in BUF. The string in BUF can be at most
6757 get_remote_packet_size () - 5 to account for the $, # and checksum,
6758 and for a possible /0 if we are debugging (remote_debug) and want
6759 to print the sent packet as a string. */
6762 putpkt_binary (char *buf
, int cnt
)
6764 struct remote_state
*rs
= get_remote_state ();
6766 unsigned char csum
= 0;
6767 char *buf2
= alloca (cnt
+ 6);
6773 /* Catch cases like trying to read memory or listing threads while
6774 we're waiting for a stop reply. The remote server wouldn't be
6775 ready to handle this request, so we'd hang and timeout. We don't
6776 have to worry about this in synchronous mode, because in that
6777 case it's not possible to issue a command while the target is
6778 running. This is not a problem in non-stop mode, because in that
6779 case, the stub is always ready to process serial input. */
6780 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
6781 error (_("Cannot execute this command while the target is running."));
6783 /* We're sending out a new packet. Make sure we don't look at a
6784 stale cached response. */
6785 rs
->cached_wait_status
= 0;
6787 /* Copy the packet into buffer BUF2, encapsulating it
6788 and giving it a checksum. */
6793 for (i
= 0; i
< cnt
; i
++)
6799 *p
++ = tohex ((csum
>> 4) & 0xf);
6800 *p
++ = tohex (csum
& 0xf);
6802 /* Send it over and over until we get a positive ack. */
6806 int started_error_output
= 0;
6810 struct cleanup
*old_chain
;
6814 str
= escape_buffer (buf2
, p
- buf2
);
6815 old_chain
= make_cleanup (xfree
, str
);
6816 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
6817 gdb_flush (gdb_stdlog
);
6818 do_cleanups (old_chain
);
6820 if (serial_write (remote_desc
, buf2
, p
- buf2
))
6821 perror_with_name (_("putpkt: write failed"));
6823 /* If this is a no acks version of the remote protocol, send the
6824 packet and move on. */
6828 /* Read until either a timeout occurs (-2) or '+' is read.
6829 Handle any notification that arrives in the mean time. */
6832 ch
= readchar (remote_timeout
);
6840 case SERIAL_TIMEOUT
:
6843 if (started_error_output
)
6845 putchar_unfiltered ('\n');
6846 started_error_output
= 0;
6855 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6859 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6861 case SERIAL_TIMEOUT
:
6865 break; /* Retransmit buffer. */
6869 fprintf_unfiltered (gdb_stdlog
,
6870 "Packet instead of Ack, ignoring it\n");
6871 /* It's probably an old response sent because an ACK
6872 was lost. Gobble up the packet and ack it so it
6873 doesn't get retransmitted when we resend this
6876 serial_write (remote_desc
, "+", 1);
6877 continue; /* Now, go look for +. */
6884 /* If we got a notification, handle it, and go back to looking
6886 /* We've found the start of a notification. Now
6887 collect the data. */
6888 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6893 struct cleanup
*old_chain
;
6896 str
= escape_buffer (rs
->buf
, val
);
6897 old_chain
= make_cleanup (xfree
, str
);
6898 fprintf_unfiltered (gdb_stdlog
,
6899 " Notification received: %s\n",
6901 do_cleanups (old_chain
);
6903 handle_notification (rs
->buf
, val
);
6904 /* We're in sync now, rewait for the ack. */
6911 if (!started_error_output
)
6913 started_error_output
= 1;
6914 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6916 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6917 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6926 if (!started_error_output
)
6928 started_error_output
= 1;
6929 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6931 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6935 break; /* Here to retransmit. */
6939 /* This is wrong. If doing a long backtrace, the user should be
6940 able to get out next time we call QUIT, without anything as
6941 violent as interrupt_query. If we want to provide a way out of
6942 here without getting to the next QUIT, it should be based on
6943 hitting ^C twice as in remote_wait. */
6954 /* Come here after finding the start of a frame when we expected an
6955 ack. Do our best to discard the rest of this packet. */
6964 c
= readchar (remote_timeout
);
6967 case SERIAL_TIMEOUT
:
6968 /* Nothing we can do. */
6971 /* Discard the two bytes of checksum and stop. */
6972 c
= readchar (remote_timeout
);
6974 c
= readchar (remote_timeout
);
6977 case '*': /* Run length encoding. */
6978 /* Discard the repeat count. */
6979 c
= readchar (remote_timeout
);
6984 /* A regular character. */
6990 /* Come here after finding the start of the frame. Collect the rest
6991 into *BUF, verifying the checksum, length, and handling run-length
6992 compression. NUL terminate the buffer. If there is not enough room,
6993 expand *BUF using xrealloc.
6995 Returns -1 on error, number of characters in buffer (ignoring the
6996 trailing NULL) on success. (could be extended to return one of the
6997 SERIAL status indications). */
7000 read_frame (char **buf_p
,
7007 struct remote_state
*rs
= get_remote_state ();
7014 c
= readchar (remote_timeout
);
7017 case SERIAL_TIMEOUT
:
7019 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7023 fputs_filtered ("Saw new packet start in middle of old one\n",
7025 return -1; /* Start a new packet, count retries. */
7028 unsigned char pktcsum
;
7034 check_0
= readchar (remote_timeout
);
7036 check_1
= readchar (remote_timeout
);
7038 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7041 fputs_filtered ("Timeout in checksum, retrying\n",
7045 else if (check_0
< 0 || check_1
< 0)
7048 fputs_filtered ("Communication error in checksum\n",
7053 /* Don't recompute the checksum; with no ack packets we
7054 don't have any way to indicate a packet retransmission
7059 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7060 if (csum
== pktcsum
)
7065 struct cleanup
*old_chain
;
7068 str
= escape_buffer (buf
, bc
);
7069 old_chain
= make_cleanup (xfree
, str
);
7070 fprintf_unfiltered (gdb_stdlog
,
7071 "Bad checksum, sentsum=0x%x, "
7072 "csum=0x%x, buf=%s\n",
7073 pktcsum
, csum
, str
);
7074 do_cleanups (old_chain
);
7076 /* Number of characters in buffer ignoring trailing
7080 case '*': /* Run length encoding. */
7085 c
= readchar (remote_timeout
);
7087 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7089 /* The character before ``*'' is repeated. */
7091 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7093 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7095 /* Make some more room in the buffer. */
7096 *sizeof_buf
+= repeat
;
7097 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7101 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7107 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7111 if (bc
>= *sizeof_buf
- 1)
7113 /* Make some more room in the buffer. */
7115 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7126 /* Read a packet from the remote machine, with error checking, and
7127 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7128 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7129 rather than timing out; this is used (in synchronous mode) to wait
7130 for a target that is is executing user code to stop. */
7131 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7132 don't have to change all the calls to getpkt to deal with the
7133 return value, because at the moment I don't know what the right
7134 thing to do it for those. */
7142 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7146 /* Read a packet from the remote machine, with error checking, and
7147 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7148 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7149 rather than timing out; this is used (in synchronous mode) to wait
7150 for a target that is is executing user code to stop. If FOREVER ==
7151 0, this function is allowed to time out gracefully and return an
7152 indication of this to the caller. Otherwise return the number of
7153 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7154 enough reason to return to the caller. */
7157 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7158 int expecting_notif
)
7160 struct remote_state
*rs
= get_remote_state ();
7166 /* We're reading a new response. Make sure we don't look at a
7167 previously cached response. */
7168 rs
->cached_wait_status
= 0;
7170 strcpy (*buf
, "timeout");
7173 timeout
= watchdog
> 0 ? watchdog
: -1;
7174 else if (expecting_notif
)
7175 timeout
= 0; /* There should already be a char in the buffer. If
7178 timeout
= remote_timeout
;
7182 /* Process any number of notifications, and then return when
7186 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7188 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7190 /* This can loop forever if the remote side sends us
7191 characters continuously, but if it pauses, we'll get
7192 SERIAL_TIMEOUT from readchar because of timeout. Then
7193 we'll count that as a retry.
7195 Note that even when forever is set, we will only wait
7196 forever prior to the start of a packet. After that, we
7197 expect characters to arrive at a brisk pace. They should
7198 show up within remote_timeout intervals. */
7200 c
= readchar (timeout
);
7201 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7203 if (c
== SERIAL_TIMEOUT
)
7205 if (expecting_notif
)
7206 return -1; /* Don't complain, it's normal to not get
7207 anything in this case. */
7209 if (forever
) /* Watchdog went off? Kill the target. */
7213 error (_("Watchdog timeout has expired. Target detached."));
7216 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7220 /* We've found the start of a packet or notification.
7221 Now collect the data. */
7222 val
= read_frame (buf
, sizeof_buf
);
7227 serial_write (remote_desc
, "-", 1);
7230 if (tries
> MAX_TRIES
)
7232 /* We have tried hard enough, and just can't receive the
7233 packet/notification. Give up. */
7234 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7236 /* Skip the ack char if we're in no-ack mode. */
7237 if (!rs
->noack_mode
)
7238 serial_write (remote_desc
, "+", 1);
7242 /* If we got an ordinary packet, return that to our caller. */
7247 struct cleanup
*old_chain
;
7250 str
= escape_buffer (*buf
, val
);
7251 old_chain
= make_cleanup (xfree
, str
);
7252 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7253 do_cleanups (old_chain
);
7256 /* Skip the ack char if we're in no-ack mode. */
7257 if (!rs
->noack_mode
)
7258 serial_write (remote_desc
, "+", 1);
7262 /* If we got a notification, handle it, and go back to looking
7266 gdb_assert (c
== '%');
7270 struct cleanup
*old_chain
;
7273 str
= escape_buffer (*buf
, val
);
7274 old_chain
= make_cleanup (xfree
, str
);
7275 fprintf_unfiltered (gdb_stdlog
,
7276 " Notification received: %s\n",
7278 do_cleanups (old_chain
);
7281 handle_notification (*buf
, val
);
7283 /* Notifications require no acknowledgement. */
7285 if (expecting_notif
)
7292 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7294 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
7298 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
7300 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
7305 remote_kill (struct target_ops
*ops
)
7307 /* Use catch_errors so the user can quit from gdb even when we
7308 aren't on speaking terms with the remote system. */
7309 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
7311 /* Don't wait for it to die. I'm not really sure it matters whether
7312 we do or not. For the existing stubs, kill is a noop. */
7313 target_mourn_inferior ();
7317 remote_vkill (int pid
, struct remote_state
*rs
)
7319 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7322 /* Tell the remote target to detach. */
7323 sprintf (rs
->buf
, "vKill;%x", pid
);
7325 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7327 if (packet_ok (rs
->buf
,
7328 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7330 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7337 extended_remote_kill (struct target_ops
*ops
)
7340 int pid
= ptid_get_pid (inferior_ptid
);
7341 struct remote_state
*rs
= get_remote_state ();
7343 res
= remote_vkill (pid
, rs
);
7344 if (res
== -1 && !remote_multi_process_p (rs
))
7346 /* Don't try 'k' on a multi-process aware stub -- it has no way
7347 to specify the pid. */
7351 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7352 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7355 /* Don't wait for it to die. I'm not really sure it matters whether
7356 we do or not. For the existing stubs, kill is a noop. */
7362 error (_("Can't kill process"));
7364 target_mourn_inferior ();
7368 remote_mourn (struct target_ops
*ops
)
7370 remote_mourn_1 (ops
);
7373 /* Worker function for remote_mourn. */
7375 remote_mourn_1 (struct target_ops
*target
)
7377 unpush_target (target
);
7379 /* remote_close takes care of doing most of the clean up. */
7380 generic_mourn_inferior ();
7384 extended_remote_mourn_1 (struct target_ops
*target
)
7386 struct remote_state
*rs
= get_remote_state ();
7388 /* In case we got here due to an error, but we're going to stay
7390 rs
->waiting_for_stop_reply
= 0;
7392 /* We're no longer interested in these events. */
7393 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
7395 /* If the current general thread belonged to the process we just
7396 detached from or has exited, the remote side current general
7397 thread becomes undefined. Considering a case like this:
7399 - We just got here due to a detach.
7400 - The process that we're detaching from happens to immediately
7401 report a global breakpoint being hit in non-stop mode, in the
7402 same thread we had selected before.
7403 - GDB attaches to this process again.
7404 - This event happens to be the next event we handle.
7406 GDB would consider that the current general thread didn't need to
7407 be set on the stub side (with Hg), since for all it knew,
7408 GENERAL_THREAD hadn't changed.
7410 Notice that although in all-stop mode, the remote server always
7411 sets the current thread to the thread reporting the stop event,
7412 that doesn't happen in non-stop mode; in non-stop, the stub *must
7413 not* change the current thread when reporting a breakpoint hit,
7414 due to the decoupling of event reporting and event handling.
7416 To keep things simple, we always invalidate our notion of the
7418 record_currthread (minus_one_ptid
);
7420 /* Unlike "target remote", we do not want to unpush the target; then
7421 the next time the user says "run", we won't be connected. */
7423 /* Call common code to mark the inferior as not running. */
7424 generic_mourn_inferior ();
7426 if (!have_inferiors ())
7428 if (!remote_multi_process_p (rs
))
7430 /* Check whether the target is running now - some remote stubs
7431 automatically restart after kill. */
7433 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7435 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7437 /* Assume that the target has been restarted. Set
7438 inferior_ptid so that bits of core GDB realizes
7439 there's something here, e.g., so that the user can
7440 say "kill" again. */
7441 inferior_ptid
= magic_null_ptid
;
7448 extended_remote_mourn (struct target_ops
*ops
)
7450 extended_remote_mourn_1 (ops
);
7454 extended_remote_run (char *args
)
7456 struct remote_state
*rs
= get_remote_state ();
7459 /* If the user has disabled vRun support, or we have detected that
7460 support is not available, do not try it. */
7461 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7464 strcpy (rs
->buf
, "vRun;");
7465 len
= strlen (rs
->buf
);
7467 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7468 error (_("Remote file name too long for run packet"));
7469 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
7471 gdb_assert (args
!= NULL
);
7474 struct cleanup
*back_to
;
7478 argv
= gdb_buildargv (args
);
7479 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7480 for (i
= 0; argv
[i
] != NULL
; i
++)
7482 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7483 error (_("Argument list too long for run packet"));
7484 rs
->buf
[len
++] = ';';
7485 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
7487 do_cleanups (back_to
);
7490 rs
->buf
[len
++] = '\0';
7493 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7495 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7497 /* We have a wait response; we don't need it, though. All is well. */
7500 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7501 /* It wasn't disabled before, but it is now. */
7505 if (remote_exec_file
[0] == '\0')
7506 error (_("Running the default executable on the remote target failed; "
7507 "try \"set remote exec-file\"?"));
7509 error (_("Running \"%s\" on the remote target failed"),
7514 /* In the extended protocol we want to be able to do things like
7515 "run" and have them basically work as expected. So we need
7516 a special create_inferior function. We support changing the
7517 executable file and the command line arguments, but not the
7521 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
7522 char **env
, int from_tty
)
7524 /* If running asynchronously, register the target file descriptor
7525 with the event loop. */
7526 if (target_can_async_p ())
7527 target_async (inferior_event_handler
, 0);
7529 /* Now restart the remote server. */
7530 if (extended_remote_run (args
) == -1)
7532 /* vRun was not supported. Fail if we need it to do what the
7534 if (remote_exec_file
[0])
7535 error (_("Remote target does not support \"set remote exec-file\""));
7537 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7539 /* Fall back to "R". */
7540 extended_remote_restart ();
7543 if (!have_inferiors ())
7545 /* Clean up from the last time we ran, before we mark the target
7546 running again. This will mark breakpoints uninserted, and
7547 get_offsets may insert breakpoints. */
7548 init_thread_list ();
7549 init_wait_for_inferior ();
7552 /* Now mark the inferior as running before we do anything else. */
7553 inferior_ptid
= magic_null_ptid
;
7555 /* Now, if we have thread information, update inferior_ptid. */
7556 inferior_ptid
= remote_current_thread (inferior_ptid
);
7558 remote_add_inferior (ptid_get_pid (inferior_ptid
), 0);
7559 add_thread_silent (inferior_ptid
);
7561 /* Get updated offsets, if the stub uses qOffsets. */
7566 extended_remote_create_inferior (struct target_ops
*ops
,
7567 char *exec_file
, char *args
,
7568 char **env
, int from_tty
)
7570 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
7574 /* Insert a breakpoint. On targets that have software breakpoint
7575 support, we ask the remote target to do the work; on targets
7576 which don't, we insert a traditional memory breakpoint. */
7579 remote_insert_breakpoint (struct gdbarch
*gdbarch
,
7580 struct bp_target_info
*bp_tgt
)
7582 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7583 If it succeeds, then set the support to PACKET_ENABLE. If it
7584 fails, and the user has explicitly requested the Z support then
7585 report an error, otherwise, mark it disabled and go on. */
7587 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7589 CORE_ADDR addr
= bp_tgt
->placed_address
;
7590 struct remote_state
*rs
;
7594 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
7596 rs
= get_remote_state ();
7602 addr
= (ULONGEST
) remote_address_masked (addr
);
7603 p
+= hexnumstr (p
, addr
);
7604 sprintf (p
, ",%d", bpsize
);
7607 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7609 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
7614 bp_tgt
->placed_address
= addr
;
7615 bp_tgt
->placed_size
= bpsize
;
7617 case PACKET_UNKNOWN
:
7622 return memory_insert_breakpoint (gdbarch
, bp_tgt
);
7626 remote_remove_breakpoint (struct gdbarch
*gdbarch
,
7627 struct bp_target_info
*bp_tgt
)
7629 CORE_ADDR addr
= bp_tgt
->placed_address
;
7630 struct remote_state
*rs
= get_remote_state ();
7632 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7640 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
7641 p
+= hexnumstr (p
, addr
);
7642 sprintf (p
, ",%d", bp_tgt
->placed_size
);
7645 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7647 return (rs
->buf
[0] == 'E');
7650 return memory_remove_breakpoint (gdbarch
, bp_tgt
);
7654 watchpoint_to_Z_packet (int type
)
7659 return Z_PACKET_WRITE_WP
;
7662 return Z_PACKET_READ_WP
;
7665 return Z_PACKET_ACCESS_WP
;
7668 internal_error (__FILE__
, __LINE__
,
7669 _("hw_bp_to_z: bad watchpoint type %d"), type
);
7674 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
7675 struct expression
*cond
)
7677 struct remote_state
*rs
= get_remote_state ();
7679 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7681 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7684 sprintf (rs
->buf
, "Z%x,", packet
);
7685 p
= strchr (rs
->buf
, '\0');
7686 addr
= remote_address_masked (addr
);
7687 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7688 sprintf (p
, ",%x", len
);
7691 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7693 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7697 case PACKET_UNKNOWN
:
7702 internal_error (__FILE__
, __LINE__
,
7703 _("remote_insert_watchpoint: reached end of function"));
7708 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
7709 struct expression
*cond
)
7711 struct remote_state
*rs
= get_remote_state ();
7713 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7715 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7718 sprintf (rs
->buf
, "z%x,", packet
);
7719 p
= strchr (rs
->buf
, '\0');
7720 addr
= remote_address_masked (addr
);
7721 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7722 sprintf (p
, ",%x", len
);
7724 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7726 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7729 case PACKET_UNKNOWN
:
7734 internal_error (__FILE__
, __LINE__
,
7735 _("remote_remove_watchpoint: reached end of function"));
7739 int remote_hw_watchpoint_limit
= -1;
7740 int remote_hw_breakpoint_limit
= -1;
7743 remote_check_watch_resources (int type
, int cnt
, int ot
)
7745 if (type
== bp_hardware_breakpoint
)
7747 if (remote_hw_breakpoint_limit
== 0)
7749 else if (remote_hw_breakpoint_limit
< 0)
7751 else if (cnt
<= remote_hw_breakpoint_limit
)
7756 if (remote_hw_watchpoint_limit
== 0)
7758 else if (remote_hw_watchpoint_limit
< 0)
7762 else if (cnt
<= remote_hw_watchpoint_limit
)
7769 remote_stopped_by_watchpoint (void)
7771 return remote_stopped_by_watchpoint_p
;
7775 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
7779 if (remote_stopped_by_watchpoint ())
7781 *addr_p
= remote_watch_data_address
;
7790 remote_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
7791 struct bp_target_info
*bp_tgt
)
7794 struct remote_state
*rs
;
7797 /* The length field should be set to the size of a breakpoint
7798 instruction, even though we aren't inserting one ourselves. */
7800 gdbarch_remote_breakpoint_from_pc
7801 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
7803 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7806 rs
= get_remote_state ();
7813 addr
= remote_address_masked (bp_tgt
->placed_address
);
7814 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7815 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7818 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7820 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7823 case PACKET_UNKNOWN
:
7828 internal_error (__FILE__
, __LINE__
,
7829 _("remote_insert_hw_breakpoint: reached end of function"));
7834 remote_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
7835 struct bp_target_info
*bp_tgt
)
7838 struct remote_state
*rs
= get_remote_state ();
7841 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7848 addr
= remote_address_masked (bp_tgt
->placed_address
);
7849 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7850 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7853 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7855 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7858 case PACKET_UNKNOWN
:
7863 internal_error (__FILE__
, __LINE__
,
7864 _("remote_remove_hw_breakpoint: reached end of function"));
7867 /* Table used by the crc32 function to calcuate the checksum. */
7869 static unsigned long crc32_table
[256] =
7872 static unsigned long
7873 crc32 (const unsigned char *buf
, int len
, unsigned int crc
)
7875 if (!crc32_table
[1])
7877 /* Initialize the CRC table and the decoding table. */
7881 for (i
= 0; i
< 256; i
++)
7883 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7884 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7891 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7897 /* Verify memory using the "qCRC:" request. */
7900 remote_verify_memory (struct target_ops
*ops
,
7901 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
7903 struct remote_state
*rs
= get_remote_state ();
7904 unsigned long host_crc
, target_crc
;
7907 /* FIXME: assumes lma can fit into long. */
7908 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7909 (long) lma
, (long) size
);
7912 /* Be clever; compute the host_crc before waiting for target
7914 host_crc
= crc32 (data
, size
, 0xffffffff);
7916 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7917 if (rs
->buf
[0] == 'E')
7920 if (rs
->buf
[0] != 'C')
7921 error (_("remote target does not support this operation"));
7923 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7924 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7926 return (host_crc
== target_crc
);
7929 /* compare-sections command
7931 With no arguments, compares each loadable section in the exec bfd
7932 with the same memory range on the target, and reports mismatches.
7933 Useful for verifying the image on the target against the exec file. */
7936 compare_sections_command (char *args
, int from_tty
)
7939 struct cleanup
*old_chain
;
7941 const char *sectname
;
7949 error (_("command cannot be used without an exec file"));
7951 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7953 if (!(s
->flags
& SEC_LOAD
))
7954 continue; /* Skip non-loadable section. */
7956 size
= bfd_get_section_size (s
);
7958 continue; /* Skip zero-length section. */
7960 sectname
= bfd_get_section_name (exec_bfd
, s
);
7961 if (args
&& strcmp (args
, sectname
) != 0)
7962 continue; /* Not the section selected by user. */
7964 matched
= 1; /* Do this section. */
7967 sectdata
= xmalloc (size
);
7968 old_chain
= make_cleanup (xfree
, sectdata
);
7969 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7971 res
= target_verify_memory (sectdata
, lma
, size
);
7974 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
7975 paddress (target_gdbarch
, lma
),
7976 paddress (target_gdbarch
, lma
+ size
));
7978 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
7979 paddress (target_gdbarch
, lma
),
7980 paddress (target_gdbarch
, lma
+ size
));
7982 printf_filtered ("matched.\n");
7985 printf_filtered ("MIS-MATCHED!\n");
7989 do_cleanups (old_chain
);
7992 warning (_("One or more sections of the remote executable does not match\n\
7993 the loaded file\n"));
7994 if (args
&& !matched
)
7995 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7998 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7999 into remote target. The number of bytes written to the remote
8000 target is returned, or -1 for error. */
8003 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
8004 const char *annex
, const gdb_byte
*writebuf
,
8005 ULONGEST offset
, LONGEST len
,
8006 struct packet_config
*packet
)
8010 struct remote_state
*rs
= get_remote_state ();
8011 int max_size
= get_memory_write_packet_size ();
8013 if (packet
->support
== PACKET_DISABLE
)
8016 /* Insert header. */
8017 i
= snprintf (rs
->buf
, max_size
,
8018 "qXfer:%s:write:%s:%s:",
8019 object_name
, annex
? annex
: "",
8020 phex_nz (offset
, sizeof offset
));
8021 max_size
-= (i
+ 1);
8023 /* Escape as much data as fits into rs->buf. */
8024 buf_len
= remote_escape_output
8025 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
8027 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8028 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8029 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8032 unpack_varlen_hex (rs
->buf
, &n
);
8036 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8037 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8038 number of bytes read is returned, or 0 for EOF, or -1 for error.
8039 The number of bytes read may be less than LEN without indicating an
8040 EOF. PACKET is checked and updated to indicate whether the remote
8041 target supports this object. */
8044 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8046 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8047 struct packet_config
*packet
)
8049 static char *finished_object
;
8050 static char *finished_annex
;
8051 static ULONGEST finished_offset
;
8053 struct remote_state
*rs
= get_remote_state ();
8054 LONGEST i
, n
, packet_len
;
8056 if (packet
->support
== PACKET_DISABLE
)
8059 /* Check whether we've cached an end-of-object packet that matches
8061 if (finished_object
)
8063 if (strcmp (object_name
, finished_object
) == 0
8064 && strcmp (annex
? annex
: "", finished_annex
) == 0
8065 && offset
== finished_offset
)
8068 /* Otherwise, we're now reading something different. Discard
8070 xfree (finished_object
);
8071 xfree (finished_annex
);
8072 finished_object
= NULL
;
8073 finished_annex
= NULL
;
8076 /* Request only enough to fit in a single packet. The actual data
8077 may not, since we don't know how much of it will need to be escaped;
8078 the target is free to respond with slightly less data. We subtract
8079 five to account for the response type and the protocol frame. */
8080 n
= min (get_remote_packet_size () - 5, len
);
8081 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8082 object_name
, annex
? annex
: "",
8083 phex_nz (offset
, sizeof offset
),
8084 phex_nz (n
, sizeof n
));
8085 i
= putpkt (rs
->buf
);
8090 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8091 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8094 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8095 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8097 /* 'm' means there is (or at least might be) more data after this
8098 batch. That does not make sense unless there's at least one byte
8099 of data in this reply. */
8100 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8101 error (_("Remote qXfer reply contained no data."));
8103 /* Got some data. */
8104 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
8106 /* 'l' is an EOF marker, possibly including a final block of data,
8107 or possibly empty. If we have the final block of a non-empty
8108 object, record this fact to bypass a subsequent partial read. */
8109 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8111 finished_object
= xstrdup (object_name
);
8112 finished_annex
= xstrdup (annex
? annex
: "");
8113 finished_offset
= offset
+ i
;
8120 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8121 const char *annex
, gdb_byte
*readbuf
,
8122 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
8124 struct remote_state
*rs
;
8129 set_remote_traceframe ();
8130 set_general_thread (inferior_ptid
);
8132 rs
= get_remote_state ();
8134 /* Handle memory using the standard memory routines. */
8135 if (object
== TARGET_OBJECT_MEMORY
)
8141 /* If the remote target is connected but not running, we should
8142 pass this request down to a lower stratum (e.g. the executable
8144 if (!target_has_execution
)
8147 if (writebuf
!= NULL
)
8148 xfered
= remote_write_bytes (offset
, writebuf
, len
);
8150 xfered
= remote_read_bytes (offset
, readbuf
, len
);
8154 else if (xfered
== 0 && errno
== 0)
8160 /* Handle SPU memory using qxfer packets. */
8161 if (object
== TARGET_OBJECT_SPU
)
8164 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8165 &remote_protocol_packets
8166 [PACKET_qXfer_spu_read
]);
8168 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8169 &remote_protocol_packets
8170 [PACKET_qXfer_spu_write
]);
8173 /* Handle extra signal info using qxfer packets. */
8174 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8177 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8178 &remote_protocol_packets
8179 [PACKET_qXfer_siginfo_read
]);
8181 return remote_write_qxfer (ops
, "siginfo", annex
,
8182 writebuf
, offset
, len
,
8183 &remote_protocol_packets
8184 [PACKET_qXfer_siginfo_write
]);
8187 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
8190 return remote_read_qxfer (ops
, "statictrace", annex
,
8191 readbuf
, offset
, len
,
8192 &remote_protocol_packets
8193 [PACKET_qXfer_statictrace_read
]);
8198 /* Only handle flash writes. */
8199 if (writebuf
!= NULL
)
8205 case TARGET_OBJECT_FLASH
:
8206 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
8210 else if (xfered
== 0 && errno
== 0)
8220 /* Map pre-existing objects onto letters. DO NOT do this for new
8221 objects!!! Instead specify new query packets. */
8224 case TARGET_OBJECT_AVR
:
8228 case TARGET_OBJECT_AUXV
:
8229 gdb_assert (annex
== NULL
);
8230 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8231 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8233 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8234 return remote_read_qxfer
8235 (ops
, "features", annex
, readbuf
, offset
, len
,
8236 &remote_protocol_packets
[PACKET_qXfer_features
]);
8238 case TARGET_OBJECT_LIBRARIES
:
8239 return remote_read_qxfer
8240 (ops
, "libraries", annex
, readbuf
, offset
, len
,
8241 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8243 case TARGET_OBJECT_MEMORY_MAP
:
8244 gdb_assert (annex
== NULL
);
8245 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8246 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8248 case TARGET_OBJECT_OSDATA
:
8249 /* Should only get here if we're connected. */
8250 gdb_assert (remote_desc
);
8251 return remote_read_qxfer
8252 (ops
, "osdata", annex
, readbuf
, offset
, len
,
8253 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8255 case TARGET_OBJECT_THREADS
:
8256 gdb_assert (annex
== NULL
);
8257 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8258 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8260 case TARGET_OBJECT_TRACEFRAME_INFO
:
8261 gdb_assert (annex
== NULL
);
8262 return remote_read_qxfer
8263 (ops
, "traceframe-info", annex
, readbuf
, offset
, len
,
8264 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
8269 /* Note: a zero OFFSET and LEN can be used to query the minimum
8271 if (offset
== 0 && len
== 0)
8272 return (get_remote_packet_size ());
8273 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8274 large enough let the caller deal with it. */
8275 if (len
< get_remote_packet_size ())
8277 len
= get_remote_packet_size ();
8279 /* Except for querying the minimum buffer size, target must be open. */
8281 error (_("remote query is only available after target open"));
8283 gdb_assert (annex
!= NULL
);
8284 gdb_assert (readbuf
!= NULL
);
8290 /* We used one buffer char for the remote protocol q command and
8291 another for the query type. As the remote protocol encapsulation
8292 uses 4 chars plus one extra in case we are debugging
8293 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8296 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8298 /* Bad caller may have sent forbidden characters. */
8299 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8304 gdb_assert (annex
[i
] == '\0');
8306 i
= putpkt (rs
->buf
);
8310 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8311 strcpy ((char *) readbuf
, rs
->buf
);
8313 return strlen ((char *) readbuf
);
8317 remote_search_memory (struct target_ops
* ops
,
8318 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8319 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8320 CORE_ADDR
*found_addrp
)
8322 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
8323 struct remote_state
*rs
= get_remote_state ();
8324 int max_size
= get_memory_write_packet_size ();
8325 struct packet_config
*packet
=
8326 &remote_protocol_packets
[PACKET_qSearch_memory
];
8327 /* Number of packet bytes used to encode the pattern;
8328 this could be more than PATTERN_LEN due to escape characters. */
8329 int escaped_pattern_len
;
8330 /* Amount of pattern that was encodable in the packet. */
8331 int used_pattern_len
;
8334 ULONGEST found_addr
;
8336 /* Don't go to the target if we don't have to.
8337 This is done before checking packet->support to avoid the possibility that
8338 a success for this edge case means the facility works in general. */
8339 if (pattern_len
> search_space_len
)
8341 if (pattern_len
== 0)
8343 *found_addrp
= start_addr
;
8347 /* If we already know the packet isn't supported, fall back to the simple
8348 way of searching memory. */
8350 if (packet
->support
== PACKET_DISABLE
)
8352 /* Target doesn't provided special support, fall back and use the
8353 standard support (copy memory and do the search here). */
8354 return simple_search_memory (ops
, start_addr
, search_space_len
,
8355 pattern
, pattern_len
, found_addrp
);
8358 /* Insert header. */
8359 i
= snprintf (rs
->buf
, max_size
,
8360 "qSearch:memory:%s;%s;",
8361 phex_nz (start_addr
, addr_size
),
8362 phex_nz (search_space_len
, sizeof (search_space_len
)));
8363 max_size
-= (i
+ 1);
8365 /* Escape as much data as fits into rs->buf. */
8366 escaped_pattern_len
=
8367 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
8368 &used_pattern_len
, max_size
);
8370 /* Bail if the pattern is too large. */
8371 if (used_pattern_len
!= pattern_len
)
8372 error (_("Pattern is too large to transmit to remote target."));
8374 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8375 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8376 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8378 /* The request may not have worked because the command is not
8379 supported. If so, fall back to the simple way. */
8380 if (packet
->support
== PACKET_DISABLE
)
8382 return simple_search_memory (ops
, start_addr
, search_space_len
,
8383 pattern
, pattern_len
, found_addrp
);
8388 if (rs
->buf
[0] == '0')
8390 else if (rs
->buf
[0] == '1')
8393 if (rs
->buf
[1] != ',')
8394 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8395 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8396 *found_addrp
= found_addr
;
8399 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8405 remote_rcmd (char *command
,
8406 struct ui_file
*outbuf
)
8408 struct remote_state
*rs
= get_remote_state ();
8412 error (_("remote rcmd is only available after target open"));
8414 /* Send a NULL command across as an empty command. */
8415 if (command
== NULL
)
8418 /* The query prefix. */
8419 strcpy (rs
->buf
, "qRcmd,");
8420 p
= strchr (rs
->buf
, '\0');
8422 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/)
8423 > get_remote_packet_size ())
8424 error (_("\"monitor\" command ``%s'' is too long."), command
);
8426 /* Encode the actual command. */
8427 bin2hex ((gdb_byte
*) command
, p
, 0);
8429 if (putpkt (rs
->buf
) < 0)
8430 error (_("Communication problem with target."));
8432 /* get/display the response */
8437 /* XXX - see also remote_get_noisy_reply(). */
8439 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8442 error (_("Target does not support this command."));
8443 if (buf
[0] == 'O' && buf
[1] != 'K')
8445 remote_console_output (buf
+ 1); /* 'O' message from stub. */
8448 if (strcmp (buf
, "OK") == 0)
8450 if (strlen (buf
) == 3 && buf
[0] == 'E'
8451 && isdigit (buf
[1]) && isdigit (buf
[2]))
8453 error (_("Protocol error with Rcmd"));
8455 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
8457 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
8459 fputc_unfiltered (c
, outbuf
);
8465 static VEC(mem_region_s
) *
8466 remote_memory_map (struct target_ops
*ops
)
8468 VEC(mem_region_s
) *result
= NULL
;
8469 char *text
= target_read_stralloc (¤t_target
,
8470 TARGET_OBJECT_MEMORY_MAP
, NULL
);
8474 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
8476 result
= parse_memory_map (text
);
8477 do_cleanups (back_to
);
8484 packet_command (char *args
, int from_tty
)
8486 struct remote_state
*rs
= get_remote_state ();
8489 error (_("command can only be used with remote target"));
8492 error (_("remote-packet command requires packet text as argument"));
8494 puts_filtered ("sending: ");
8495 print_packet (args
);
8496 puts_filtered ("\n");
8499 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8500 puts_filtered ("received: ");
8501 print_packet (rs
->buf
);
8502 puts_filtered ("\n");
8506 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8508 static void display_thread_info (struct gdb_ext_thread_info
*info
);
8510 static void threadset_test_cmd (char *cmd
, int tty
);
8512 static void threadalive_test (char *cmd
, int tty
);
8514 static void threadlist_test_cmd (char *cmd
, int tty
);
8516 int get_and_display_threadinfo (threadref
*ref
);
8518 static void threadinfo_test_cmd (char *cmd
, int tty
);
8520 static int thread_display_step (threadref
*ref
, void *context
);
8522 static void threadlist_update_test_cmd (char *cmd
, int tty
);
8524 static void init_remote_threadtests (void);
8526 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8529 threadset_test_cmd (char *cmd
, int tty
)
8531 int sample_thread
= SAMPLE_THREAD
;
8533 printf_filtered (_("Remote threadset test\n"));
8534 set_general_thread (sample_thread
);
8539 threadalive_test (char *cmd
, int tty
)
8541 int sample_thread
= SAMPLE_THREAD
;
8542 int pid
= ptid_get_pid (inferior_ptid
);
8543 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
8545 if (remote_thread_alive (ptid
))
8546 printf_filtered ("PASS: Thread alive test\n");
8548 printf_filtered ("FAIL: Thread alive test\n");
8551 void output_threadid (char *title
, threadref
*ref
);
8554 output_threadid (char *title
, threadref
*ref
)
8558 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
8560 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
8564 threadlist_test_cmd (char *cmd
, int tty
)
8567 threadref nextthread
;
8568 int done
, result_count
;
8569 threadref threadlist
[3];
8571 printf_filtered ("Remote Threadlist test\n");
8572 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
8573 &result_count
, &threadlist
[0]))
8574 printf_filtered ("FAIL: threadlist test\n");
8577 threadref
*scan
= threadlist
;
8578 threadref
*limit
= scan
+ result_count
;
8580 while (scan
< limit
)
8581 output_threadid (" thread ", scan
++);
8586 display_thread_info (struct gdb_ext_thread_info
*info
)
8588 output_threadid ("Threadid: ", &info
->threadid
);
8589 printf_filtered ("Name: %s\n ", info
->shortname
);
8590 printf_filtered ("State: %s\n", info
->display
);
8591 printf_filtered ("other: %s\n\n", info
->more_display
);
8595 get_and_display_threadinfo (threadref
*ref
)
8599 struct gdb_ext_thread_info threadinfo
;
8601 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
8602 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
8603 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
8604 display_thread_info (&threadinfo
);
8609 threadinfo_test_cmd (char *cmd
, int tty
)
8611 int athread
= SAMPLE_THREAD
;
8615 int_to_threadref (&thread
, athread
);
8616 printf_filtered ("Remote Threadinfo test\n");
8617 if (!get_and_display_threadinfo (&thread
))
8618 printf_filtered ("FAIL cannot get thread info\n");
8622 thread_display_step (threadref
*ref
, void *context
)
8624 /* output_threadid(" threadstep ",ref); *//* simple test */
8625 return get_and_display_threadinfo (ref
);
8629 threadlist_update_test_cmd (char *cmd
, int tty
)
8631 printf_filtered ("Remote Threadlist update test\n");
8632 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
8636 init_remote_threadtests (void)
8638 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
8639 _("Fetch and print the remote list of "
8640 "thread identifiers, one pkt only"));
8641 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
8642 _("Fetch and display info about one thread"));
8643 add_com ("tset", class_obscure
, threadset_test_cmd
,
8644 _("Test setting to a different thread"));
8645 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
8646 _("Iterate through updating all remote thread info"));
8647 add_com ("talive", class_obscure
, threadalive_test
,
8648 _(" Remote thread alive test "));
8653 /* Convert a thread ID to a string. Returns the string in a static
8657 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
8659 static char buf
[64];
8660 struct remote_state
*rs
= get_remote_state ();
8662 if (ptid_is_pid (ptid
))
8664 /* Printing an inferior target id. */
8666 /* When multi-process extensions are off, there's no way in the
8667 remote protocol to know the remote process id, if there's any
8668 at all. There's one exception --- when we're connected with
8669 target extended-remote, and we manually attached to a process
8670 with "attach PID". We don't record anywhere a flag that
8671 allows us to distinguish that case from the case of
8672 connecting with extended-remote and the stub already being
8673 attached to a process, and reporting yes to qAttached, hence
8674 no smart special casing here. */
8675 if (!remote_multi_process_p (rs
))
8677 xsnprintf (buf
, sizeof buf
, "Remote target");
8681 return normal_pid_to_str (ptid
);
8685 if (ptid_equal (magic_null_ptid
, ptid
))
8686 xsnprintf (buf
, sizeof buf
, "Thread <main>");
8687 else if (remote_multi_process_p (rs
))
8688 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
8689 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
8691 xsnprintf (buf
, sizeof buf
, "Thread %ld",
8692 ptid_get_tid (ptid
));
8697 /* Get the address of the thread local variable in OBJFILE which is
8698 stored at OFFSET within the thread local storage for thread PTID. */
8701 remote_get_thread_local_address (struct target_ops
*ops
,
8702 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
8704 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
8706 struct remote_state
*rs
= get_remote_state ();
8708 char *endp
= rs
->buf
+ get_remote_packet_size ();
8709 enum packet_result result
;
8711 strcpy (p
, "qGetTLSAddr:");
8713 p
= write_ptid (p
, endp
, ptid
);
8715 p
+= hexnumstr (p
, offset
);
8717 p
+= hexnumstr (p
, lm
);
8721 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8722 result
= packet_ok (rs
->buf
,
8723 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
8724 if (result
== PACKET_OK
)
8728 unpack_varlen_hex (rs
->buf
, &result
);
8731 else if (result
== PACKET_UNKNOWN
)
8732 throw_error (TLS_GENERIC_ERROR
,
8733 _("Remote target doesn't support qGetTLSAddr packet"));
8735 throw_error (TLS_GENERIC_ERROR
,
8736 _("Remote target failed to process qGetTLSAddr request"));
8739 throw_error (TLS_GENERIC_ERROR
,
8740 _("TLS not supported or disabled on this target"));
8745 /* Provide thread local base, i.e. Thread Information Block address.
8746 Returns 1 if ptid is found and thread_local_base is non zero. */
8749 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
8751 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
8753 struct remote_state
*rs
= get_remote_state ();
8755 char *endp
= rs
->buf
+ get_remote_packet_size ();
8756 enum packet_result result
;
8758 strcpy (p
, "qGetTIBAddr:");
8760 p
= write_ptid (p
, endp
, ptid
);
8764 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8765 result
= packet_ok (rs
->buf
,
8766 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
8767 if (result
== PACKET_OK
)
8771 unpack_varlen_hex (rs
->buf
, &result
);
8773 *addr
= (CORE_ADDR
) result
;
8776 else if (result
== PACKET_UNKNOWN
)
8777 error (_("Remote target doesn't support qGetTIBAddr packet"));
8779 error (_("Remote target failed to process qGetTIBAddr request"));
8782 error (_("qGetTIBAddr not supported or disabled on this target"));
8787 /* Support for inferring a target description based on the current
8788 architecture and the size of a 'g' packet. While the 'g' packet
8789 can have any size (since optional registers can be left off the
8790 end), some sizes are easily recognizable given knowledge of the
8791 approximate architecture. */
8793 struct remote_g_packet_guess
8796 const struct target_desc
*tdesc
;
8798 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
8799 DEF_VEC_O(remote_g_packet_guess_s
);
8801 struct remote_g_packet_data
8803 VEC(remote_g_packet_guess_s
) *guesses
;
8806 static struct gdbarch_data
*remote_g_packet_data_handle
;
8809 remote_g_packet_data_init (struct obstack
*obstack
)
8811 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
8815 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
8816 const struct target_desc
*tdesc
)
8818 struct remote_g_packet_data
*data
8819 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
8820 struct remote_g_packet_guess new_guess
, *guess
;
8823 gdb_assert (tdesc
!= NULL
);
8826 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8828 if (guess
->bytes
== bytes
)
8829 internal_error (__FILE__
, __LINE__
,
8830 _("Duplicate g packet description added for size %d"),
8833 new_guess
.bytes
= bytes
;
8834 new_guess
.tdesc
= tdesc
;
8835 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
8838 /* Return 1 if remote_read_description would do anything on this target
8839 and architecture, 0 otherwise. */
8842 remote_read_description_p (struct target_ops
*target
)
8844 struct remote_g_packet_data
*data
8845 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8847 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8853 static const struct target_desc
*
8854 remote_read_description (struct target_ops
*target
)
8856 struct remote_g_packet_data
*data
8857 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8859 /* Do not try this during initial connection, when we do not know
8860 whether there is a running but stopped thread. */
8861 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
8864 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8866 struct remote_g_packet_guess
*guess
;
8868 int bytes
= send_g_packet ();
8871 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8873 if (guess
->bytes
== bytes
)
8874 return guess
->tdesc
;
8876 /* We discard the g packet. A minor optimization would be to
8877 hold on to it, and fill the register cache once we have selected
8878 an architecture, but it's too tricky to do safely. */
8884 /* Remote file transfer support. This is host-initiated I/O, not
8885 target-initiated; for target-initiated, see remote-fileio.c. */
8887 /* If *LEFT is at least the length of STRING, copy STRING to
8888 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8889 decrease *LEFT. Otherwise raise an error. */
8892 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
8894 int len
= strlen (string
);
8897 error (_("Packet too long for target."));
8899 memcpy (*buffer
, string
, len
);
8903 /* NUL-terminate the buffer as a convenience, if there is
8909 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
8910 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8911 decrease *LEFT. Otherwise raise an error. */
8914 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
8917 if (2 * len
> *left
)
8918 error (_("Packet too long for target."));
8920 bin2hex (bytes
, *buffer
, len
);
8924 /* NUL-terminate the buffer as a convenience, if there is
8930 /* If *LEFT is large enough, convert VALUE to hex and add it to
8931 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8932 decrease *LEFT. Otherwise raise an error. */
8935 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
8937 int len
= hexnumlen (value
);
8940 error (_("Packet too long for target."));
8942 hexnumstr (*buffer
, value
);
8946 /* NUL-terminate the buffer as a convenience, if there is
8952 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8953 value, *REMOTE_ERRNO to the remote error number or zero if none
8954 was included, and *ATTACHMENT to point to the start of the annex
8955 if any. The length of the packet isn't needed here; there may
8956 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8958 Return 0 if the packet could be parsed, -1 if it could not. If
8959 -1 is returned, the other variables may not be initialized. */
8962 remote_hostio_parse_result (char *buffer
, int *retcode
,
8963 int *remote_errno
, char **attachment
)
8970 if (buffer
[0] != 'F')
8974 *retcode
= strtol (&buffer
[1], &p
, 16);
8975 if (errno
!= 0 || p
== &buffer
[1])
8978 /* Check for ",errno". */
8982 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8983 if (errno
!= 0 || p
+ 1 == p2
)
8988 /* Check for ";attachment". If there is no attachment, the
8989 packet should end here. */
8992 *attachment
= p
+ 1;
8995 else if (*p
== '\0')
9001 /* Send a prepared I/O packet to the target and read its response.
9002 The prepared packet is in the global RS->BUF before this function
9003 is called, and the answer is there when we return.
9005 COMMAND_BYTES is the length of the request to send, which may include
9006 binary data. WHICH_PACKET is the packet configuration to check
9007 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9008 is set to the error number and -1 is returned. Otherwise the value
9009 returned by the function is returned.
9011 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9012 attachment is expected; an error will be reported if there's a
9013 mismatch. If one is found, *ATTACHMENT will be set to point into
9014 the packet buffer and *ATTACHMENT_LEN will be set to the
9015 attachment's length. */
9018 remote_hostio_send_command (int command_bytes
, int which_packet
,
9019 int *remote_errno
, char **attachment
,
9020 int *attachment_len
)
9022 struct remote_state
*rs
= get_remote_state ();
9023 int ret
, bytes_read
;
9024 char *attachment_tmp
;
9027 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9029 *remote_errno
= FILEIO_ENOSYS
;
9033 putpkt_binary (rs
->buf
, command_bytes
);
9034 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9036 /* If it timed out, something is wrong. Don't try to parse the
9040 *remote_errno
= FILEIO_EINVAL
;
9044 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9047 *remote_errno
= FILEIO_EINVAL
;
9049 case PACKET_UNKNOWN
:
9050 *remote_errno
= FILEIO_ENOSYS
;
9056 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9059 *remote_errno
= FILEIO_EINVAL
;
9063 /* Make sure we saw an attachment if and only if we expected one. */
9064 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9065 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9067 *remote_errno
= FILEIO_EINVAL
;
9071 /* If an attachment was found, it must point into the packet buffer;
9072 work out how many bytes there were. */
9073 if (attachment_tmp
!= NULL
)
9075 *attachment
= attachment_tmp
;
9076 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9082 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9083 remote file descriptor, or -1 if an error occurs (and set
9087 remote_hostio_open (const char *filename
, int flags
, int mode
,
9090 struct remote_state
*rs
= get_remote_state ();
9092 int left
= get_remote_packet_size () - 1;
9094 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9096 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9098 remote_buffer_add_string (&p
, &left
, ",");
9100 remote_buffer_add_int (&p
, &left
, flags
);
9101 remote_buffer_add_string (&p
, &left
, ",");
9103 remote_buffer_add_int (&p
, &left
, mode
);
9105 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9106 remote_errno
, NULL
, NULL
);
9109 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9110 Return the number of bytes written, or -1 if an error occurs (and
9111 set *REMOTE_ERRNO). */
9114 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
9115 ULONGEST offset
, int *remote_errno
)
9117 struct remote_state
*rs
= get_remote_state ();
9119 int left
= get_remote_packet_size ();
9122 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9124 remote_buffer_add_int (&p
, &left
, fd
);
9125 remote_buffer_add_string (&p
, &left
, ",");
9127 remote_buffer_add_int (&p
, &left
, offset
);
9128 remote_buffer_add_string (&p
, &left
, ",");
9130 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
9131 get_remote_packet_size () - (p
- rs
->buf
));
9133 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9134 remote_errno
, NULL
, NULL
);
9137 /* Read up to LEN bytes FD on the remote target into READ_BUF
9138 Return the number of bytes read, or -1 if an error occurs (and
9139 set *REMOTE_ERRNO). */
9142 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
9143 ULONGEST offset
, int *remote_errno
)
9145 struct remote_state
*rs
= get_remote_state ();
9148 int left
= get_remote_packet_size ();
9149 int ret
, attachment_len
;
9152 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9154 remote_buffer_add_int (&p
, &left
, fd
);
9155 remote_buffer_add_string (&p
, &left
, ",");
9157 remote_buffer_add_int (&p
, &left
, len
);
9158 remote_buffer_add_string (&p
, &left
, ",");
9160 remote_buffer_add_int (&p
, &left
, offset
);
9162 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9163 remote_errno
, &attachment
,
9169 read_len
= remote_unescape_input (attachment
, attachment_len
,
9171 if (read_len
!= ret
)
9172 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9177 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9178 (and set *REMOTE_ERRNO). */
9181 remote_hostio_close (int fd
, int *remote_errno
)
9183 struct remote_state
*rs
= get_remote_state ();
9185 int left
= get_remote_packet_size () - 1;
9187 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9189 remote_buffer_add_int (&p
, &left
, fd
);
9191 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9192 remote_errno
, NULL
, NULL
);
9195 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9196 occurs (and set *REMOTE_ERRNO). */
9199 remote_hostio_unlink (const char *filename
, int *remote_errno
)
9201 struct remote_state
*rs
= get_remote_state ();
9203 int left
= get_remote_packet_size () - 1;
9205 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9207 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9210 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9211 remote_errno
, NULL
, NULL
);
9215 remote_fileio_errno_to_host (int errnum
)
9239 case FILEIO_ENOTDIR
:
9259 case FILEIO_ENAMETOOLONG
:
9260 return ENAMETOOLONG
;
9266 remote_hostio_error (int errnum
)
9268 int host_error
= remote_fileio_errno_to_host (errnum
);
9270 if (host_error
== -1)
9271 error (_("Unknown remote I/O error %d"), errnum
);
9273 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9277 remote_hostio_close_cleanup (void *opaque
)
9279 int fd
= *(int *) opaque
;
9282 remote_hostio_close (fd
, &remote_errno
);
9287 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9289 const char *filename
= bfd_get_filename (abfd
);
9290 int fd
, remote_errno
;
9293 gdb_assert (remote_filename_p (filename
));
9295 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9298 errno
= remote_fileio_errno_to_host (remote_errno
);
9299 bfd_set_error (bfd_error_system_call
);
9303 stream
= xmalloc (sizeof (int));
9309 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9311 int fd
= *(int *)stream
;
9316 /* Ignore errors on close; these may happen if the remote
9317 connection was already torn down. */
9318 remote_hostio_close (fd
, &remote_errno
);
9324 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9325 file_ptr nbytes
, file_ptr offset
)
9327 int fd
= *(int *)stream
;
9329 file_ptr pos
, bytes
;
9332 while (nbytes
> pos
)
9334 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
9335 offset
+ pos
, &remote_errno
);
9337 /* Success, but no bytes, means end-of-file. */
9341 errno
= remote_fileio_errno_to_host (remote_errno
);
9342 bfd_set_error (bfd_error_system_call
);
9353 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9355 /* FIXME: We should probably implement remote_hostio_stat. */
9356 sb
->st_size
= INT_MAX
;
9361 remote_filename_p (const char *filename
)
9363 return strncmp (filename
, "remote:", 7) == 0;
9367 remote_bfd_open (const char *remote_file
, const char *target
)
9369 return bfd_openr_iovec (remote_file
, target
,
9370 remote_bfd_iovec_open
, NULL
,
9371 remote_bfd_iovec_pread
,
9372 remote_bfd_iovec_close
,
9373 remote_bfd_iovec_stat
);
9377 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
9379 struct cleanup
*back_to
, *close_cleanup
;
9380 int retcode
, fd
, remote_errno
, bytes
, io_size
;
9383 int bytes_in_buffer
;
9388 error (_("command can only be used with remote target"));
9390 file
= fopen (local_file
, "rb");
9392 perror_with_name (local_file
);
9393 back_to
= make_cleanup_fclose (file
);
9395 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
9397 0700, &remote_errno
);
9399 remote_hostio_error (remote_errno
);
9401 /* Send up to this many bytes at once. They won't all fit in the
9402 remote packet limit, so we'll transfer slightly fewer. */
9403 io_size
= get_remote_packet_size ();
9404 buffer
= xmalloc (io_size
);
9405 make_cleanup (xfree
, buffer
);
9407 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9409 bytes_in_buffer
= 0;
9412 while (bytes_in_buffer
|| !saw_eof
)
9416 bytes
= fread (buffer
+ bytes_in_buffer
, 1,
9417 io_size
- bytes_in_buffer
,
9422 error (_("Error reading %s."), local_file
);
9425 /* EOF. Unless there is something still in the
9426 buffer from the last iteration, we are done. */
9428 if (bytes_in_buffer
== 0)
9436 bytes
+= bytes_in_buffer
;
9437 bytes_in_buffer
= 0;
9439 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
,
9440 offset
, &remote_errno
);
9443 remote_hostio_error (remote_errno
);
9444 else if (retcode
== 0)
9445 error (_("Remote write of %d bytes returned 0!"), bytes
);
9446 else if (retcode
< bytes
)
9448 /* Short write. Save the rest of the read data for the next
9450 bytes_in_buffer
= bytes
- retcode
;
9451 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
9457 discard_cleanups (close_cleanup
);
9458 if (remote_hostio_close (fd
, &remote_errno
))
9459 remote_hostio_error (remote_errno
);
9462 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
9463 do_cleanups (back_to
);
9467 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
9469 struct cleanup
*back_to
, *close_cleanup
;
9470 int fd
, remote_errno
, bytes
, io_size
;
9476 error (_("command can only be used with remote target"));
9478 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
9480 remote_hostio_error (remote_errno
);
9482 file
= fopen (local_file
, "wb");
9484 perror_with_name (local_file
);
9485 back_to
= make_cleanup_fclose (file
);
9487 /* Send up to this many bytes at once. They won't all fit in the
9488 remote packet limit, so we'll transfer slightly fewer. */
9489 io_size
= get_remote_packet_size ();
9490 buffer
= xmalloc (io_size
);
9491 make_cleanup (xfree
, buffer
);
9493 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9498 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
9500 /* Success, but no bytes, means end-of-file. */
9503 remote_hostio_error (remote_errno
);
9507 bytes
= fwrite (buffer
, 1, bytes
, file
);
9509 perror_with_name (local_file
);
9512 discard_cleanups (close_cleanup
);
9513 if (remote_hostio_close (fd
, &remote_errno
))
9514 remote_hostio_error (remote_errno
);
9517 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
9518 do_cleanups (back_to
);
9522 remote_file_delete (const char *remote_file
, int from_tty
)
9524 int retcode
, remote_errno
;
9527 error (_("command can only be used with remote target"));
9529 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
9531 remote_hostio_error (remote_errno
);
9534 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
9538 remote_put_command (char *args
, int from_tty
)
9540 struct cleanup
*back_to
;
9544 error_no_arg (_("file to put"));
9546 argv
= gdb_buildargv (args
);
9547 back_to
= make_cleanup_freeargv (argv
);
9548 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9549 error (_("Invalid parameters to remote put"));
9551 remote_file_put (argv
[0], argv
[1], from_tty
);
9553 do_cleanups (back_to
);
9557 remote_get_command (char *args
, int from_tty
)
9559 struct cleanup
*back_to
;
9563 error_no_arg (_("file to get"));
9565 argv
= gdb_buildargv (args
);
9566 back_to
= make_cleanup_freeargv (argv
);
9567 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9568 error (_("Invalid parameters to remote get"));
9570 remote_file_get (argv
[0], argv
[1], from_tty
);
9572 do_cleanups (back_to
);
9576 remote_delete_command (char *args
, int from_tty
)
9578 struct cleanup
*back_to
;
9582 error_no_arg (_("file to delete"));
9584 argv
= gdb_buildargv (args
);
9585 back_to
= make_cleanup_freeargv (argv
);
9586 if (argv
[0] == NULL
|| argv
[1] != NULL
)
9587 error (_("Invalid parameters to remote delete"));
9589 remote_file_delete (argv
[0], from_tty
);
9591 do_cleanups (back_to
);
9595 remote_command (char *args
, int from_tty
)
9597 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
9601 remote_can_execute_reverse (void)
9603 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
9604 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
9611 remote_supports_non_stop (void)
9617 remote_supports_multi_process (void)
9619 struct remote_state
*rs
= get_remote_state ();
9621 return remote_multi_process_p (rs
);
9625 remote_supports_cond_tracepoints (void)
9627 struct remote_state
*rs
= get_remote_state ();
9629 return rs
->cond_tracepoints
;
9633 remote_supports_fast_tracepoints (void)
9635 struct remote_state
*rs
= get_remote_state ();
9637 return rs
->fast_tracepoints
;
9641 remote_supports_static_tracepoints (void)
9643 struct remote_state
*rs
= get_remote_state ();
9645 return rs
->static_tracepoints
;
9649 remote_trace_init (void)
9652 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9653 if (strcmp (target_buf
, "OK") != 0)
9654 error (_("Target does not support this command."));
9657 static void free_actions_list (char **actions_list
);
9658 static void free_actions_list_cleanup_wrapper (void *);
9660 free_actions_list_cleanup_wrapper (void *al
)
9662 free_actions_list (al
);
9666 free_actions_list (char **actions_list
)
9670 if (actions_list
== 0)
9673 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
9674 xfree (actions_list
[ndx
]);
9676 xfree (actions_list
);
9679 /* Recursive routine to walk through command list including loops, and
9680 download packets for each command. */
9683 remote_download_command_source (int num
, ULONGEST addr
,
9684 struct command_line
*cmds
)
9686 struct remote_state
*rs
= get_remote_state ();
9687 struct command_line
*cmd
;
9689 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
9691 QUIT
; /* Allow user to bail out with ^C. */
9692 strcpy (rs
->buf
, "QTDPsrc:");
9693 encode_source_string (num
, addr
, "cmd", cmd
->line
,
9694 rs
->buf
+ strlen (rs
->buf
),
9695 rs
->buf_size
- strlen (rs
->buf
));
9697 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9698 if (strcmp (target_buf
, "OK"))
9699 warning (_("Target does not support source download."));
9701 if (cmd
->control_type
== while_control
9702 || cmd
->control_type
== while_stepping_control
)
9704 remote_download_command_source (num
, addr
, *cmd
->body_list
);
9706 QUIT
; /* Allow user to bail out with ^C. */
9707 strcpy (rs
->buf
, "QTDPsrc:");
9708 encode_source_string (num
, addr
, "cmd", "end",
9709 rs
->buf
+ strlen (rs
->buf
),
9710 rs
->buf_size
- strlen (rs
->buf
));
9712 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9713 if (strcmp (target_buf
, "OK"))
9714 warning (_("Target does not support source download."));
9720 remote_download_tracepoint (struct breakpoint
*t
)
9722 struct bp_location
*loc
;
9727 char **stepping_actions
;
9729 struct cleanup
*old_chain
= NULL
;
9730 struct agent_expr
*aexpr
;
9731 struct cleanup
*aexpr_chain
= NULL
;
9734 /* Iterate over all the tracepoint locations. It's up to the target to
9735 notice multiple tracepoint packets with the same number but different
9736 addresses, and treat them as multiple locations. */
9737 for (loc
= t
->loc
; loc
; loc
= loc
->next
)
9739 encode_actions (t
, loc
, &tdp_actions
, &stepping_actions
);
9740 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
9742 (void) make_cleanup (free_actions_list_cleanup_wrapper
,
9745 tpaddr
= loc
->address
;
9746 sprintf_vma (addrbuf
, tpaddr
);
9747 sprintf (buf
, "QTDP:%x:%s:%c:%lx:%x", t
->number
,
9748 addrbuf
, /* address */
9749 (t
->enable_state
== bp_enabled
? 'E' : 'D'),
9750 t
->step_count
, t
->pass_count
);
9751 /* Fast tracepoints are mostly handled by the target, but we can
9752 tell the target how big of an instruction block should be moved
9754 if (t
->type
== bp_fast_tracepoint
)
9756 /* Only test for support at download time; we may not know
9757 target capabilities at definition time. */
9758 if (remote_supports_fast_tracepoints ())
9762 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch
,
9763 tpaddr
, &isize
, NULL
))
9764 sprintf (buf
+ strlen (buf
), ":F%x", isize
);
9766 /* If it passed validation at definition but fails now,
9767 something is very wrong. */
9768 internal_error (__FILE__
, __LINE__
,
9769 _("Fast tracepoint not "
9770 "valid during download"));
9773 /* Fast tracepoints are functionally identical to regular
9774 tracepoints, so don't take lack of support as a reason to
9775 give up on the trace run. */
9776 warning (_("Target does not support fast tracepoints, "
9777 "downloading %d as regular tracepoint"), t
->number
);
9779 else if (t
->type
== bp_static_tracepoint
)
9781 /* Only test for support at download time; we may not know
9782 target capabilities at definition time. */
9783 if (remote_supports_static_tracepoints ())
9785 struct static_tracepoint_marker marker
;
9787 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
9790 error (_("Static tracepoint not valid during download"));
9793 /* Fast tracepoints are functionally identical to regular
9794 tracepoints, so don't take lack of support as a reason
9795 to give up on the trace run. */
9796 error (_("Target does not support static tracepoints"));
9798 /* If the tracepoint has a conditional, make it into an agent
9799 expression and append to the definition. */
9802 /* Only test support at download time, we may not know target
9803 capabilities at definition time. */
9804 if (remote_supports_cond_tracepoints ())
9806 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
9807 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
9808 sprintf (buf
+ strlen (buf
), ":X%x,", aexpr
->len
);
9809 pkt
= buf
+ strlen (buf
);
9810 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
9811 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
9813 do_cleanups (aexpr_chain
);
9816 warning (_("Target does not support conditional tracepoints, "
9817 "ignoring tp %d cond"), t
->number
);
9820 if (t
->commands
|| *default_collect
)
9823 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9824 if (strcmp (target_buf
, "OK"))
9825 error (_("Target does not support tracepoints."));
9827 /* do_single_steps (t); */
9830 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
9832 QUIT
; /* Allow user to bail out with ^C. */
9833 sprintf (buf
, "QTDP:-%x:%s:%s%c",
9834 t
->number
, addrbuf
, /* address */
9836 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
9839 remote_get_noisy_reply (&target_buf
,
9841 if (strcmp (target_buf
, "OK"))
9842 error (_("Error on target while setting tracepoints."));
9845 if (stepping_actions
)
9847 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
9849 QUIT
; /* Allow user to bail out with ^C. */
9850 sprintf (buf
, "QTDP:-%x:%s:%s%s%s",
9851 t
->number
, addrbuf
, /* address */
9852 ((ndx
== 0) ? "S" : ""),
9853 stepping_actions
[ndx
],
9854 (stepping_actions
[ndx
+ 1] ? "-" : ""));
9856 remote_get_noisy_reply (&target_buf
,
9858 if (strcmp (target_buf
, "OK"))
9859 error (_("Error on target while setting tracepoints."));
9863 if (remote_protocol_packets
[PACKET_TracepointSource
].support
9868 strcpy (buf
, "QTDPsrc:");
9869 encode_source_string (t
->number
, loc
->address
,
9870 "at", t
->addr_string
, buf
+ strlen (buf
),
9871 2048 - strlen (buf
));
9874 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9875 if (strcmp (target_buf
, "OK"))
9876 warning (_("Target does not support source download."));
9880 strcpy (buf
, "QTDPsrc:");
9881 encode_source_string (t
->number
, loc
->address
,
9882 "cond", t
->cond_string
, buf
+ strlen (buf
),
9883 2048 - strlen (buf
));
9885 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9886 if (strcmp (target_buf
, "OK"))
9887 warning (_("Target does not support source download."));
9889 remote_download_command_source (t
->number
, loc
->address
,
9890 breakpoint_commands (t
));
9893 do_cleanups (old_chain
);
9898 remote_download_trace_state_variable (struct trace_state_variable
*tsv
)
9900 struct remote_state
*rs
= get_remote_state ();
9903 sprintf (rs
->buf
, "QTDV:%x:%s:%x:",
9904 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8), tsv
->builtin
);
9905 p
= rs
->buf
+ strlen (rs
->buf
);
9906 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
9907 error (_("Trace state variable name too long for tsv definition packet"));
9908 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, 0);
9911 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9912 if (*target_buf
== '\0')
9913 error (_("Target does not support this command."));
9914 if (strcmp (target_buf
, "OK") != 0)
9915 error (_("Error on target while downloading trace state variable."));
9919 remote_trace_set_readonly_regions (void)
9927 return; /* No information to give. */
9929 strcpy (target_buf
, "QTro");
9930 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
9932 char tmp1
[40], tmp2
[40];
9934 if ((s
->flags
& SEC_LOAD
) == 0 ||
9935 /* (s->flags & SEC_CODE) == 0 || */
9936 (s
->flags
& SEC_READONLY
) == 0)
9940 vma
= bfd_get_section_vma (,s
);
9941 size
= bfd_get_section_size (s
);
9942 sprintf_vma (tmp1
, vma
);
9943 sprintf_vma (tmp2
, vma
+ size
);
9944 sprintf (target_buf
+ strlen (target_buf
),
9945 ":%s,%s", tmp1
, tmp2
);
9949 putpkt (target_buf
);
9950 getpkt (&target_buf
, &target_buf_size
, 0);
9955 remote_trace_start (void)
9958 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9959 if (*target_buf
== '\0')
9960 error (_("Target does not support this command."));
9961 if (strcmp (target_buf
, "OK") != 0)
9962 error (_("Bogus reply from target: %s"), target_buf
);
9966 remote_get_trace_status (struct trace_status
*ts
)
9969 /* FIXME we need to get register block size some other way. */
9970 extern int trace_regblock_size
;
9972 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
9974 putpkt ("qTStatus");
9975 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9977 /* If the remote target doesn't do tracing, flag it. */
9981 /* We're working with a live target. */
9984 /* Set some defaults. */
9985 ts
->running_known
= 0;
9986 ts
->stop_reason
= trace_stop_reason_unknown
;
9987 ts
->traceframe_count
= -1;
9988 ts
->buffer_free
= 0;
9991 error (_("Bogus trace status reply from target: %s"), target_buf
);
9993 parse_trace_status (p
, ts
);
9999 remote_trace_stop (void)
10002 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10003 if (*target_buf
== '\0')
10004 error (_("Target does not support this command."));
10005 if (strcmp (target_buf
, "OK") != 0)
10006 error (_("Bogus reply from target: %s"), target_buf
);
10010 remote_trace_find (enum trace_find_type type
, int num
,
10011 ULONGEST addr1
, ULONGEST addr2
,
10014 struct remote_state
*rs
= get_remote_state ();
10016 int target_frameno
= -1, target_tracept
= -1;
10018 /* Lookups other than by absolute frame number depend on the current
10019 trace selected, so make sure it is correct on the remote end
10021 if (type
!= tfind_number
)
10022 set_remote_traceframe ();
10025 strcpy (p
, "QTFrame:");
10026 p
= strchr (p
, '\0');
10030 sprintf (p
, "%x", num
);
10033 sprintf (p
, "pc:%s", phex_nz (addr1
, 0));
10036 sprintf (p
, "tdp:%x", num
);
10039 sprintf (p
, "range:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10041 case tfind_outside
:
10042 sprintf (p
, "outside:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10045 error (_("Unknown trace find type %d"), type
);
10049 reply
= remote_get_noisy_reply (&(rs
->buf
), &sizeof_pkt
);
10050 if (*reply
== '\0')
10051 error (_("Target does not support this command."));
10053 while (reply
&& *reply
)
10058 target_frameno
= (int) strtol (p
, &reply
, 16);
10060 error (_("Unable to parse trace frame number"));
10061 /* Don't update our remote traceframe number cache on failure
10062 to select a remote traceframe. */
10063 if (target_frameno
== -1)
10068 target_tracept
= (int) strtol (p
, &reply
, 16);
10070 error (_("Unable to parse tracepoint number"));
10072 case 'O': /* "OK"? */
10073 if (reply
[1] == 'K' && reply
[2] == '\0')
10076 error (_("Bogus reply from target: %s"), reply
);
10079 error (_("Bogus reply from target: %s"), reply
);
10082 *tpp
= target_tracept
;
10084 remote_traceframe_number
= target_frameno
;
10085 return target_frameno
;
10089 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
10091 struct remote_state
*rs
= get_remote_state ();
10095 set_remote_traceframe ();
10097 sprintf (rs
->buf
, "qTV:%x", tsvnum
);
10099 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10100 if (reply
&& *reply
)
10104 unpack_varlen_hex (reply
+ 1, &uval
);
10105 *val
= (LONGEST
) uval
;
10113 remote_save_trace_data (const char *filename
)
10115 struct remote_state
*rs
= get_remote_state ();
10119 strcpy (p
, "QTSave:");
10121 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10122 error (_("Remote file name too long for trace save packet"));
10123 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, 0);
10126 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10127 if (*reply
!= '\0')
10128 error (_("Target does not support this command."));
10129 if (strcmp (reply
, "OK") != 0)
10130 error (_("Bogus reply from target: %s"), reply
);
10134 /* This is basically a memory transfer, but needs to be its own packet
10135 because we don't know how the target actually organizes its trace
10136 memory, plus we want to be able to ask for as much as possible, but
10137 not be unhappy if we don't get as much as we ask for. */
10140 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10142 struct remote_state
*rs
= get_remote_state ();
10148 strcpy (p
, "qTBuffer:");
10150 p
+= hexnumstr (p
, offset
);
10152 p
+= hexnumstr (p
, len
);
10156 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10157 if (reply
&& *reply
)
10159 /* 'l' by itself means we're at the end of the buffer and
10160 there is nothing more to get. */
10164 /* Convert the reply into binary. Limit the number of bytes to
10165 convert according to our passed-in buffer size, rather than
10166 what was returned in the packet; if the target is
10167 unexpectedly generous and gives us a bigger reply than we
10168 asked for, we don't want to crash. */
10169 rslt
= hex2bin (target_buf
, buf
, len
);
10173 /* Something went wrong, flag as an error. */
10178 remote_set_disconnected_tracing (int val
)
10180 struct remote_state
*rs
= get_remote_state ();
10182 if (rs
->disconnected_tracing
)
10186 sprintf (rs
->buf
, "QTDisconnected:%x", val
);
10188 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10189 if (*reply
== '\0')
10190 error (_("Target does not support this command."));
10191 if (strcmp (reply
, "OK") != 0)
10192 error (_("Bogus reply from target: %s"), reply
);
10195 warning (_("Target does not support disconnected tracing."));
10199 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
10201 struct thread_info
*info
= find_thread_ptid (ptid
);
10203 if (info
&& info
->private)
10204 return info
->private->core
;
10209 remote_set_circular_trace_buffer (int val
)
10211 struct remote_state
*rs
= get_remote_state ();
10214 sprintf (rs
->buf
, "QTBuffer:circular:%x", val
);
10216 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10217 if (*reply
== '\0')
10218 error (_("Target does not support this command."));
10219 if (strcmp (reply
, "OK") != 0)
10220 error (_("Bogus reply from target: %s"), reply
);
10223 static struct traceframe_info
*
10224 remote_traceframe_info (void)
10228 text
= target_read_stralloc (¤t_target
,
10229 TARGET_OBJECT_TRACEFRAME_INFO
, NULL
);
10232 struct traceframe_info
*info
;
10233 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
10235 info
= parse_traceframe_info (text
);
10236 do_cleanups (back_to
);
10244 init_remote_ops (void)
10246 remote_ops
.to_shortname
= "remote";
10247 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
10248 remote_ops
.to_doc
=
10249 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10250 Specify the serial device it is connected to\n\
10251 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10252 remote_ops
.to_open
= remote_open
;
10253 remote_ops
.to_close
= remote_close
;
10254 remote_ops
.to_detach
= remote_detach
;
10255 remote_ops
.to_disconnect
= remote_disconnect
;
10256 remote_ops
.to_resume
= remote_resume
;
10257 remote_ops
.to_wait
= remote_wait
;
10258 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
10259 remote_ops
.to_store_registers
= remote_store_registers
;
10260 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
10261 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
10262 remote_ops
.to_files_info
= remote_files_info
;
10263 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
10264 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
10265 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
10266 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
10267 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
10268 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
10269 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
10270 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
10271 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
10272 remote_ops
.to_kill
= remote_kill
;
10273 remote_ops
.to_load
= generic_load
;
10274 remote_ops
.to_mourn_inferior
= remote_mourn
;
10275 remote_ops
.to_notice_signals
= remote_notice_signals
;
10276 remote_ops
.to_thread_alive
= remote_thread_alive
;
10277 remote_ops
.to_find_new_threads
= remote_threads_info
;
10278 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
10279 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
10280 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
10281 remote_ops
.to_stop
= remote_stop
;
10282 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
10283 remote_ops
.to_rcmd
= remote_rcmd
;
10284 remote_ops
.to_log_command
= serial_log_command
;
10285 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
10286 remote_ops
.to_stratum
= process_stratum
;
10287 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
10288 remote_ops
.to_has_memory
= default_child_has_memory
;
10289 remote_ops
.to_has_stack
= default_child_has_stack
;
10290 remote_ops
.to_has_registers
= default_child_has_registers
;
10291 remote_ops
.to_has_execution
= default_child_has_execution
;
10292 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
10293 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
10294 remote_ops
.to_magic
= OPS_MAGIC
;
10295 remote_ops
.to_memory_map
= remote_memory_map
;
10296 remote_ops
.to_flash_erase
= remote_flash_erase
;
10297 remote_ops
.to_flash_done
= remote_flash_done
;
10298 remote_ops
.to_read_description
= remote_read_description
;
10299 remote_ops
.to_search_memory
= remote_search_memory
;
10300 remote_ops
.to_can_async_p
= remote_can_async_p
;
10301 remote_ops
.to_is_async_p
= remote_is_async_p
;
10302 remote_ops
.to_async
= remote_async
;
10303 remote_ops
.to_async_mask
= remote_async_mask
;
10304 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
10305 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
10306 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
10307 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
10308 remote_ops
.to_trace_init
= remote_trace_init
;
10309 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
10310 remote_ops
.to_download_trace_state_variable
10311 = remote_download_trace_state_variable
;
10312 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
10313 remote_ops
.to_trace_start
= remote_trace_start
;
10314 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
10315 remote_ops
.to_trace_stop
= remote_trace_stop
;
10316 remote_ops
.to_trace_find
= remote_trace_find
;
10317 remote_ops
.to_get_trace_state_variable_value
10318 = remote_get_trace_state_variable_value
;
10319 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
10320 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
10321 remote_ops
.to_upload_trace_state_variables
10322 = remote_upload_trace_state_variables
;
10323 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
10324 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
10325 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
10326 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
10327 remote_ops
.to_verify_memory
= remote_verify_memory
;
10328 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
10329 remote_ops
.to_set_permissions
= remote_set_permissions
;
10330 remote_ops
.to_static_tracepoint_marker_at
10331 = remote_static_tracepoint_marker_at
;
10332 remote_ops
.to_static_tracepoint_markers_by_strid
10333 = remote_static_tracepoint_markers_by_strid
;
10334 remote_ops
.to_traceframe_info
= remote_traceframe_info
;
10337 /* Set up the extended remote vector by making a copy of the standard
10338 remote vector and adding to it. */
10341 init_extended_remote_ops (void)
10343 extended_remote_ops
= remote_ops
;
10345 extended_remote_ops
.to_shortname
= "extended-remote";
10346 extended_remote_ops
.to_longname
=
10347 "Extended remote serial target in gdb-specific protocol";
10348 extended_remote_ops
.to_doc
=
10349 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10350 Specify the serial device it is connected to (e.g. /dev/ttya).";
10351 extended_remote_ops
.to_open
= extended_remote_open
;
10352 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
10353 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
10354 extended_remote_ops
.to_detach
= extended_remote_detach
;
10355 extended_remote_ops
.to_attach
= extended_remote_attach
;
10356 extended_remote_ops
.to_kill
= extended_remote_kill
;
10360 remote_can_async_p (void)
10362 if (!target_async_permitted
)
10363 /* We only enable async when the user specifically asks for it. */
10366 /* We're async whenever the serial device is. */
10367 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
10371 remote_is_async_p (void)
10373 if (!target_async_permitted
)
10374 /* We only enable async when the user specifically asks for it. */
10377 /* We're async whenever the serial device is. */
10378 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
10381 /* Pass the SERIAL event on and up to the client. One day this code
10382 will be able to delay notifying the client of an event until the
10383 point where an entire packet has been received. */
10385 static void (*async_client_callback
) (enum inferior_event_type event_type
,
10387 static void *async_client_context
;
10388 static serial_event_ftype remote_async_serial_handler
;
10391 remote_async_serial_handler (struct serial
*scb
, void *context
)
10393 /* Don't propogate error information up to the client. Instead let
10394 the client find out about the error by querying the target. */
10395 async_client_callback (INF_REG_EVENT
, async_client_context
);
10399 remote_async_inferior_event_handler (gdb_client_data data
)
10401 inferior_event_handler (INF_REG_EVENT
, NULL
);
10405 remote_async_get_pending_events_handler (gdb_client_data data
)
10407 remote_get_pending_stop_replies ();
10411 remote_async (void (*callback
) (enum inferior_event_type event_type
,
10412 void *context
), void *context
)
10414 if (remote_async_mask_value
== 0)
10415 internal_error (__FILE__
, __LINE__
,
10416 _("Calling remote_async when async is masked"));
10418 if (callback
!= NULL
)
10420 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
10421 async_client_callback
= callback
;
10422 async_client_context
= context
;
10425 serial_async (remote_desc
, NULL
, NULL
);
10429 remote_async_mask (int new_mask
)
10431 int curr_mask
= remote_async_mask_value
;
10433 remote_async_mask_value
= new_mask
;
10438 set_remote_cmd (char *args
, int from_tty
)
10440 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
10444 show_remote_cmd (char *args
, int from_tty
)
10446 /* We can't just use cmd_show_list here, because we want to skip
10447 the redundant "show remote Z-packet" and the legacy aliases. */
10448 struct cleanup
*showlist_chain
;
10449 struct cmd_list_element
*list
= remote_show_cmdlist
;
10451 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
10452 for (; list
!= NULL
; list
= list
->next
)
10453 if (strcmp (list
->name
, "Z-packet") == 0)
10455 else if (list
->type
== not_set_cmd
)
10456 /* Alias commands are exactly like the original, except they
10457 don't have the normal type. */
10461 struct cleanup
*option_chain
10462 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
10464 ui_out_field_string (uiout
, "name", list
->name
);
10465 ui_out_text (uiout
, ": ");
10466 if (list
->type
== show_cmd
)
10467 do_setshow_command ((char *) NULL
, from_tty
, list
);
10469 cmd_func (list
, NULL
, from_tty
);
10470 /* Close the tuple. */
10471 do_cleanups (option_chain
);
10474 /* Close the tuple. */
10475 do_cleanups (showlist_chain
);
10479 /* Function to be called whenever a new objfile (shlib) is detected. */
10481 remote_new_objfile (struct objfile
*objfile
)
10483 if (remote_desc
!= 0) /* Have a remote connection. */
10484 remote_check_symbols (objfile
);
10487 /* Pull all the tracepoints defined on the target and create local
10488 data structures representing them. We don't want to create real
10489 tracepoints yet, we don't want to mess up the user's existing
10493 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
10495 struct remote_state
*rs
= get_remote_state ();
10498 /* Ask for a first packet of tracepoint definition. */
10500 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10502 while (*p
&& *p
!= 'l')
10504 parse_tracepoint_definition (p
, utpp
);
10505 /* Ask for another packet of tracepoint definition. */
10507 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10514 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
10516 struct remote_state
*rs
= get_remote_state ();
10519 /* Ask for a first packet of variable definition. */
10521 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10523 while (*p
&& *p
!= 'l')
10525 parse_tsv_definition (p
, utsvp
);
10526 /* Ask for another packet of variable definition. */
10528 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10535 _initialize_remote (void)
10537 struct remote_state
*rs
;
10538 struct cmd_list_element
*cmd
;
10541 /* architecture specific data */
10542 remote_gdbarch_data_handle
=
10543 gdbarch_data_register_post_init (init_remote_state
);
10544 remote_g_packet_data_handle
=
10545 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
10547 /* Initialize the per-target state. At the moment there is only one
10548 of these, not one per target. Only one target is active at a
10549 time. The default buffer size is unimportant; it will be expanded
10550 whenever a larger buffer is needed. */
10551 rs
= get_remote_state_raw ();
10552 rs
->buf_size
= 400;
10553 rs
->buf
= xmalloc (rs
->buf_size
);
10555 init_remote_ops ();
10556 add_target (&remote_ops
);
10558 init_extended_remote_ops ();
10559 add_target (&extended_remote_ops
);
10561 /* Hook into new objfile notification. */
10562 observer_attach_new_objfile (remote_new_objfile
);
10564 /* Set up signal handlers. */
10565 sigint_remote_token
=
10566 create_async_signal_handler (async_remote_interrupt
, NULL
);
10567 sigint_remote_twice_token
=
10568 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
10571 init_remote_threadtests ();
10574 /* set/show remote ... */
10576 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
10577 Remote protocol specific variables\n\
10578 Configure various remote-protocol specific variables such as\n\
10579 the packets being used"),
10580 &remote_set_cmdlist
, "set remote ",
10581 0 /* allow-unknown */, &setlist
);
10582 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
10583 Remote protocol specific variables\n\
10584 Configure various remote-protocol specific variables such as\n\
10585 the packets being used"),
10586 &remote_show_cmdlist
, "show remote ",
10587 0 /* allow-unknown */, &showlist
);
10589 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
10590 Compare section data on target to the exec file.\n\
10591 Argument is a single section name (default: all loaded sections)."),
10594 add_cmd ("packet", class_maintenance
, packet_command
, _("\
10595 Send an arbitrary packet to a remote target.\n\
10596 maintenance packet TEXT\n\
10597 If GDB is talking to an inferior via the GDB serial protocol, then\n\
10598 this command sends the string TEXT to the inferior, and displays the\n\
10599 response packet. GDB supplies the initial `$' character, and the\n\
10600 terminating `#' character and checksum."),
10603 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
10604 Set whether to send break if interrupted."), _("\
10605 Show whether to send break if interrupted."), _("\
10606 If set, a break, instead of a cntrl-c, is sent to the remote target."),
10607 set_remotebreak
, show_remotebreak
,
10608 &setlist
, &showlist
);
10609 cmd_name
= "remotebreak";
10610 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
10611 deprecate_cmd (cmd
, "set remote interrupt-sequence");
10612 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
10613 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
10614 deprecate_cmd (cmd
, "show remote interrupt-sequence");
10616 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
10617 interrupt_sequence_modes
, &interrupt_sequence_mode
,
10619 Set interrupt sequence to remote target."), _("\
10620 Show interrupt sequence to remote target."), _("\
10621 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
10622 NULL
, show_interrupt_sequence
,
10623 &remote_set_cmdlist
,
10624 &remote_show_cmdlist
);
10626 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
10627 &interrupt_on_connect
, _("\
10628 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10629 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10630 If set, interrupt sequence is sent to remote target."),
10632 &remote_set_cmdlist
, &remote_show_cmdlist
);
10634 /* Install commands for configuring memory read/write packets. */
10636 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
10637 Set the maximum number of bytes per memory write packet (deprecated)."),
10639 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
10640 Show the maximum number of bytes per memory write packet (deprecated)."),
10642 add_cmd ("memory-write-packet-size", no_class
,
10643 set_memory_write_packet_size
, _("\
10644 Set the maximum number of bytes per memory-write packet.\n\
10645 Specify the number of bytes in a packet or 0 (zero) for the\n\
10646 default packet size. The actual limit is further reduced\n\
10647 dependent on the target. Specify ``fixed'' to disable the\n\
10648 further restriction and ``limit'' to enable that restriction."),
10649 &remote_set_cmdlist
);
10650 add_cmd ("memory-read-packet-size", no_class
,
10651 set_memory_read_packet_size
, _("\
10652 Set the maximum number of bytes per memory-read packet.\n\
10653 Specify the number of bytes in a packet or 0 (zero) for the\n\
10654 default packet size. The actual limit is further reduced\n\
10655 dependent on the target. Specify ``fixed'' to disable the\n\
10656 further restriction and ``limit'' to enable that restriction."),
10657 &remote_set_cmdlist
);
10658 add_cmd ("memory-write-packet-size", no_class
,
10659 show_memory_write_packet_size
,
10660 _("Show the maximum number of bytes per memory-write packet."),
10661 &remote_show_cmdlist
);
10662 add_cmd ("memory-read-packet-size", no_class
,
10663 show_memory_read_packet_size
,
10664 _("Show the maximum number of bytes per memory-read packet."),
10665 &remote_show_cmdlist
);
10667 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
10668 &remote_hw_watchpoint_limit
, _("\
10669 Set the maximum number of target hardware watchpoints."), _("\
10670 Show the maximum number of target hardware watchpoints."), _("\
10671 Specify a negative limit for unlimited."),
10672 NULL
, NULL
, /* FIXME: i18n: The maximum
10673 number of target hardware
10674 watchpoints is %s. */
10675 &remote_set_cmdlist
, &remote_show_cmdlist
);
10676 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
10677 &remote_hw_breakpoint_limit
, _("\
10678 Set the maximum number of target hardware breakpoints."), _("\
10679 Show the maximum number of target hardware breakpoints."), _("\
10680 Specify a negative limit for unlimited."),
10681 NULL
, NULL
, /* FIXME: i18n: The maximum
10682 number of target hardware
10683 breakpoints is %s. */
10684 &remote_set_cmdlist
, &remote_show_cmdlist
);
10686 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
10687 &remote_address_size
, _("\
10688 Set the maximum size of the address (in bits) in a memory packet."), _("\
10689 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
10691 NULL
, /* FIXME: i18n: */
10692 &setlist
, &showlist
);
10694 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
10695 "X", "binary-download", 1);
10697 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
10698 "vCont", "verbose-resume", 0);
10700 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
10701 "QPassSignals", "pass-signals", 0);
10703 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
10704 "qSymbol", "symbol-lookup", 0);
10706 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
10707 "P", "set-register", 1);
10709 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
10710 "p", "fetch-register", 1);
10712 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
10713 "Z0", "software-breakpoint", 0);
10715 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
10716 "Z1", "hardware-breakpoint", 0);
10718 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
10719 "Z2", "write-watchpoint", 0);
10721 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
10722 "Z3", "read-watchpoint", 0);
10724 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
10725 "Z4", "access-watchpoint", 0);
10727 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
10728 "qXfer:auxv:read", "read-aux-vector", 0);
10730 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
10731 "qXfer:features:read", "target-features", 0);
10733 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
10734 "qXfer:libraries:read", "library-info", 0);
10736 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
10737 "qXfer:memory-map:read", "memory-map", 0);
10739 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
10740 "qXfer:spu:read", "read-spu-object", 0);
10742 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
10743 "qXfer:spu:write", "write-spu-object", 0);
10745 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
10746 "qXfer:osdata:read", "osdata", 0);
10748 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
10749 "qXfer:threads:read", "threads", 0);
10751 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
10752 "qXfer:siginfo:read", "read-siginfo-object", 0);
10754 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
10755 "qXfer:siginfo:write", "write-siginfo-object", 0);
10757 add_packet_config_cmd
10758 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
10759 "qXfer:trace-frame-info:read", "traceframe-info", 0);
10761 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
10762 "qGetTLSAddr", "get-thread-local-storage-address",
10765 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
10766 "qGetTIBAddr", "get-thread-information-block-address",
10769 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
10770 "bc", "reverse-continue", 0);
10772 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
10773 "bs", "reverse-step", 0);
10775 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
10776 "qSupported", "supported-packets", 0);
10778 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
10779 "qSearch:memory", "search-memory", 0);
10781 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
10782 "vFile:open", "hostio-open", 0);
10784 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
10785 "vFile:pread", "hostio-pread", 0);
10787 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
10788 "vFile:pwrite", "hostio-pwrite", 0);
10790 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
10791 "vFile:close", "hostio-close", 0);
10793 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
10794 "vFile:unlink", "hostio-unlink", 0);
10796 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
10797 "vAttach", "attach", 0);
10799 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
10802 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
10803 "QStartNoAckMode", "noack", 0);
10805 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
10806 "vKill", "kill", 0);
10808 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
10809 "qAttached", "query-attached", 0);
10811 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
10812 "ConditionalTracepoints",
10813 "conditional-tracepoints", 0);
10814 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
10815 "FastTracepoints", "fast-tracepoints", 0);
10817 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
10818 "TracepointSource", "TracepointSource", 0);
10820 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
10821 "QAllow", "allow", 0);
10823 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
10824 "StaticTracepoints", "static-tracepoints", 0);
10826 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
10827 "qXfer:statictrace:read", "read-sdata-object", 0);
10829 /* Keep the old ``set remote Z-packet ...'' working. Each individual
10830 Z sub-packet has its own set and show commands, but users may
10831 have sets to this variable in their .gdbinit files (or in their
10833 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
10834 &remote_Z_packet_detect
, _("\
10835 Set use of remote protocol `Z' packets"), _("\
10836 Show use of remote protocol `Z' packets "), _("\
10837 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
10839 set_remote_protocol_Z_packet_cmd
,
10840 show_remote_protocol_Z_packet_cmd
,
10841 /* FIXME: i18n: Use of remote protocol
10842 `Z' packets is %s. */
10843 &remote_set_cmdlist
, &remote_show_cmdlist
);
10845 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
10846 Manipulate files on the remote system\n\
10847 Transfer files to and from the remote target system."),
10848 &remote_cmdlist
, "remote ",
10849 0 /* allow-unknown */, &cmdlist
);
10851 add_cmd ("put", class_files
, remote_put_command
,
10852 _("Copy a local file to the remote system."),
10855 add_cmd ("get", class_files
, remote_get_command
,
10856 _("Copy a remote file to the local system."),
10859 add_cmd ("delete", class_files
, remote_delete_command
,
10860 _("Delete a remote file."),
10863 remote_exec_file
= xstrdup ("");
10864 add_setshow_string_noescape_cmd ("exec-file", class_files
,
10865 &remote_exec_file
, _("\
10866 Set the remote pathname for \"run\""), _("\
10867 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
10868 &remote_set_cmdlist
, &remote_show_cmdlist
);
10870 /* Eventually initialize fileio. See fileio.c */
10871 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
10873 /* Take advantage of the fact that the LWP field is not used, to tag
10874 special ptids with it set to != 0. */
10875 magic_null_ptid
= ptid_build (42000, 1, -1);
10876 not_sent_ptid
= ptid_build (42000, 1, -2);
10877 any_thread_ptid
= ptid_build (42000, 1, 0);
10879 target_buf_size
= 2048;
10880 target_buf
= xmalloc (target_buf_size
);