remote: Small cleanup in compare_section_command
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
e2882c85 3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
c906108c 4
c5aa993b
JM
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b
JM
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c5aa993b 19
23860348 20/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 21
c906108c 22#include "defs.h"
c906108c
SS
23#include <ctype.h>
24#include <fcntl.h>
c906108c 25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "bfd.h"
28#include "symfile.h"
29#include "target.h"
c5aa993b 30/*#include "terminal.h" */
c906108c
SS
31#include "gdbcmd.h"
32#include "objfiles.h"
33#include "gdb-stabs.h"
34#include "gdbthread.h"
c2c6d25f 35#include "remote.h"
722247f1 36#include "remote-notif.h"
4e052eda 37#include "regcache.h"
fd0407d6 38#include "value.h"
76727919 39#include "observable.h"
a77053c2 40#include "solib.h"
37a105a1
DJ
41#include "cli/cli-decode.h"
42#include "cli/cli-setshow.h"
424163ea 43#include "target-descriptions.h"
a4453b7e 44#include "gdb_bfd.h"
614c279d 45#include "filestuff.h"
9c3d6531 46#include "rsp-low.h"
6b940e6a 47#include "disasm.h"
f00aae0f 48#include "location.h"
c906108c 49
438e1e42 50#include "gdb_sys_time.h"
c906108c 51
43ff13b4 52#include "event-loop.h"
c2c6d25f 53#include "event-top.h"
2acceee2 54#include "inf-loop.h"
43ff13b4 55
c906108c
SS
56#include <signal.h>
57#include "serial.h"
58
6240bebf
MS
59#include "gdbcore.h" /* for exec_bfd */
60
449092f6 61#include "remote-fileio.h"
a6b151f1 62#include "gdb/fileio.h"
53ce3c39 63#include <sys/stat.h>
dc146f7c 64#include "xml-support.h"
449092f6 65
fd79ecee
DJ
66#include "memory-map.h"
67
35b1e5cc
SS
68#include "tracepoint.h"
69#include "ax.h"
70#include "ax-gdb.h"
d1feda86 71#include "agent.h"
9accd112 72#include "btrace.h"
c0272db5 73#include "record-btrace.h"
325fac50 74#include <algorithm>
2ec845e7 75#include "common/scoped_restore.h"
0a2dde4a 76#include "environ.h"
f6327dcb 77#include "common/byte-vector.h"
9d6eea31 78#include <unordered_map>
35b1e5cc 79
f6ac5f3d
PA
80/* The remote target. */
81
d9f719f1
PA
82static const char remote_doc[] = N_("\
83Use a remote computer via a serial line, using a gdb-specific protocol.\n\
84Specify the serial device it is connected to\n\
85(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
86
87static const target_info remote_target_info = {
88 "remote",
89 N_("Remote serial target in gdb-specific protocol"),
90 remote_doc
91};
92
f6ac5f3d
PA
93class remote_target : public target_ops
94{
95public:
96 remote_target ()
97 {
98 to_stratum = process_stratum;
99 }
100
d9f719f1
PA
101 const target_info &info () const override
102 { return remote_target_info; }
f6ac5f3d
PA
103
104 thread_control_capabilities get_thread_control_capabilities () override
105 { return tc_schedlock; }
106
d9f719f1
PA
107 /* Open a remote connection. */
108 static void open (const char *, int);
109
f6ac5f3d
PA
110 void close () override;
111
112 void detach (inferior *, int) override;
113 void disconnect (const char *, int) override;
114
115 void commit_resume () override;
116 void resume (ptid_t, int, enum gdb_signal) override;
117 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
118
119 void fetch_registers (struct regcache *, int) override;
120 void store_registers (struct regcache *, int) override;
121 void prepare_to_store (struct regcache *) override;
122
123 void files_info () override;
124
125 int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
126
127 int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
128 enum remove_bp_reason) override;
129
130
57810aa7
PA
131 bool stopped_by_sw_breakpoint () override;
132 bool supports_stopped_by_sw_breakpoint () override;
f6ac5f3d 133
57810aa7 134 bool stopped_by_hw_breakpoint () override;
f6ac5f3d 135
57810aa7 136 bool supports_stopped_by_hw_breakpoint () override;
f6ac5f3d 137
57810aa7 138 bool stopped_by_watchpoint () override;
f6ac5f3d 139
57810aa7 140 bool stopped_data_address (CORE_ADDR *) override;
f6ac5f3d 141
57810aa7 142 bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
f6ac5f3d
PA
143
144 int can_use_hw_breakpoint (enum bptype, int, int) override;
145
146 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
147
148 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
149
150 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
151
152 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
153 struct expression *) override;
154
155 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
156 struct expression *) override;
157
158 void kill () override;
159
160 void load (const char *, int) override;
161
162 void mourn_inferior () override;
163
164 void pass_signals (int, unsigned char *) override;
165
166 int set_syscall_catchpoint (int, bool, int,
167 gdb::array_view<const int>) override;
168
169 void program_signals (int, unsigned char *) override;
170
57810aa7 171 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
172
173 const char *thread_name (struct thread_info *) override;
174
175 void update_thread_list () override;
176
177 const char *pid_to_str (ptid_t) override;
178
179 const char *extra_thread_info (struct thread_info *) override;
180
181 ptid_t get_ada_task_ptid (long lwp, long thread) override;
182
183 thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle,
184 int handle_len,
185 inferior *inf) override;
186
187 void stop (ptid_t) override;
188
189 void interrupt () override;
190
191 void pass_ctrlc () override;
192
193 enum target_xfer_status xfer_partial (enum target_object object,
194 const char *annex,
195 gdb_byte *readbuf,
196 const gdb_byte *writebuf,
197 ULONGEST offset, ULONGEST len,
198 ULONGEST *xfered_len) override;
199
200 ULONGEST get_memory_xfer_limit () override;
201
202 void rcmd (const char *command, struct ui_file *output) override;
203
204 char *pid_to_exec_file (int pid) override;
205
206 void log_command (const char *cmd) override
207 {
208 serial_log_command (this, cmd);
209 }
210
211 CORE_ADDR get_thread_local_address (ptid_t ptid,
212 CORE_ADDR load_module_addr,
213 CORE_ADDR offset) override;
214
57810aa7
PA
215 bool has_all_memory () override { return default_child_has_all_memory (); }
216 bool has_memory () override { return default_child_has_memory (); }
217 bool has_stack () override { return default_child_has_stack (); }
218 bool has_registers () override { return default_child_has_registers (); }
219 bool has_execution (ptid_t ptid) override { return default_child_has_execution (ptid); }
f6ac5f3d 220
57810aa7 221 bool can_execute_reverse () override;
f6ac5f3d
PA
222
223 std::vector<mem_region> memory_map () override;
224
225 void flash_erase (ULONGEST address, LONGEST length) override;
226
227 void flash_done () override;
228
229 const struct target_desc *read_description () override;
230
231 int search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
232 const gdb_byte *pattern, ULONGEST pattern_len,
233 CORE_ADDR *found_addrp) override;
234
57810aa7 235 bool can_async_p () override;
f6ac5f3d 236
57810aa7 237 bool is_async_p () override;
f6ac5f3d
PA
238
239 void async (int) override;
240
241 void thread_events (int) override;
242
243 int can_do_single_step () override;
244
245 void terminal_inferior () override;
246
247 void terminal_ours () override;
248
57810aa7 249 bool supports_non_stop () override;
f6ac5f3d 250
57810aa7 251 bool supports_multi_process () override;
f6ac5f3d 252
57810aa7 253 bool supports_disable_randomization () override;
f6ac5f3d 254
57810aa7 255 bool filesystem_is_local () override;
f6ac5f3d
PA
256
257
258 int fileio_open (struct inferior *inf, const char *filename,
259 int flags, int mode, int warn_if_slow,
260 int *target_errno) override;
261
262 int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
263 ULONGEST offset, int *target_errno) override;
264
265 int fileio_pread (int fd, gdb_byte *read_buf, int len,
266 ULONGEST offset, int *target_errno) override;
267
268 int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
269
270 int fileio_close (int fd, int *target_errno) override;
271
272 int fileio_unlink (struct inferior *inf,
273 const char *filename,
274 int *target_errno) override;
275
276 gdb::optional<std::string>
277 fileio_readlink (struct inferior *inf,
278 const char *filename,
279 int *target_errno) override;
280
57810aa7 281 bool supports_enable_disable_tracepoint () override;
f6ac5f3d 282
57810aa7 283 bool supports_string_tracing () override;
f6ac5f3d 284
57810aa7 285 bool supports_evaluation_of_breakpoint_conditions () override;
f6ac5f3d 286
57810aa7 287 bool can_run_breakpoint_commands () override;
f6ac5f3d
PA
288
289 void trace_init () override;
290
291 void download_tracepoint (struct bp_location *location) override;
292
57810aa7 293 bool can_download_tracepoint () override;
f6ac5f3d
PA
294
295 void download_trace_state_variable (const trace_state_variable &tsv) override;
296
297 void enable_tracepoint (struct bp_location *location) override;
298
299 void disable_tracepoint (struct bp_location *location) override;
300
301 void trace_set_readonly_regions () override;
302
303 void trace_start () override;
304
305 int get_trace_status (struct trace_status *ts) override;
306
307 void get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
308 override;
309
310 void trace_stop () override;
311
312 int trace_find (enum trace_find_type type, int num,
313 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
314
57810aa7 315 bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
f6ac5f3d
PA
316
317 int save_trace_data (const char *filename) override;
318
319 int upload_tracepoints (struct uploaded_tp **utpp) override;
320
321 int upload_trace_state_variables (struct uploaded_tsv **utsvp) override;
322
323 LONGEST get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) override;
324
325 int get_min_fast_tracepoint_insn_len () override;
326
327 void set_disconnected_tracing (int val) override;
328
329 void set_circular_trace_buffer (int val) override;
330
331 void set_trace_buffer_size (LONGEST val) override;
332
57810aa7
PA
333 bool set_trace_notes (const char *user, const char *notes,
334 const char *stopnotes) override;
f6ac5f3d
PA
335
336 int core_of_thread (ptid_t ptid) override;
337
338 int verify_memory (const gdb_byte *data,
339 CORE_ADDR memaddr, ULONGEST size) override;
340
341
57810aa7 342 bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override;
f6ac5f3d
PA
343
344 void set_permissions () override;
345
346 bool static_tracepoint_marker_at (CORE_ADDR,
347 struct static_tracepoint_marker *marker)
348 override;
349
350 std::vector<static_tracepoint_marker>
351 static_tracepoint_markers_by_strid (const char *id) override;
352
353 traceframe_info_up traceframe_info () override;
354
57810aa7
PA
355 bool use_agent (bool use) override;
356 bool can_use_agent () override;
f6ac5f3d
PA
357
358 struct btrace_target_info *enable_btrace (ptid_t ptid,
359 const struct btrace_config *conf) override;
360
361 void disable_btrace (struct btrace_target_info *tinfo) override;
362
363 void teardown_btrace (struct btrace_target_info *tinfo) override;
364
365 enum btrace_error read_btrace (struct btrace_data *data,
366 struct btrace_target_info *btinfo,
367 enum btrace_read_type type) override;
368
369 const struct btrace_config *btrace_conf (const struct btrace_target_info *) override;
57810aa7 370 bool augmented_libraries_svr4_read () override;
f6ac5f3d
PA
371 int follow_fork (int, int) override;
372 void follow_exec (struct inferior *, char *) override;
373 int insert_fork_catchpoint (int) override;
374 int remove_fork_catchpoint (int) override;
375 int insert_vfork_catchpoint (int) override;
376 int remove_vfork_catchpoint (int) override;
377 int insert_exec_catchpoint (int) override;
378 int remove_exec_catchpoint (int) override;
379 enum exec_direction_kind execution_direction () override;
380
381protected:
d9f719f1 382 static void open_1 (const char *name, int from_tty, int extended_p);
f6ac5f3d
PA
383 void start_remote (int from_tty, int extended_p);
384};
385
d9f719f1
PA
386static const target_info extended_remote_target_info = {
387 "extended-remote",
388 N_("Extended remote serial target in gdb-specific protocol"),
389 remote_doc
390};
391
f6ac5f3d
PA
392/* Set up the extended remote target by extending the standard remote
393 target and adding to it. */
394
395class extended_remote_target final : public remote_target
396{
397public:
d9f719f1
PA
398 const target_info &info () const override
399 { return extended_remote_target_info; }
f6ac5f3d 400
d9f719f1
PA
401 /* Open an extended-remote connection. */
402 static void open (const char *, int);
f6ac5f3d
PA
403
404 bool can_create_inferior () override { return true; }
405 void create_inferior (const char *, const std::string &,
406 char **, int) override;
407
408 void detach (inferior *, int) override;
409
410 bool can_attach () override { return true; }
411 void attach (const char *, int) override;
412
413 void post_attach (int) override;
57810aa7 414 bool supports_disable_randomization () override;
f6ac5f3d
PA
415};
416
94585166
DB
417/* Per-program-space data key. */
418static const struct program_space_data *remote_pspace_data;
419
420/* The variable registered as the control variable used by the
421 remote exec-file commands. While the remote exec-file setting is
422 per-program-space, the set/show machinery uses this as the
423 location of the remote exec-file value. */
424static char *remote_exec_file_var;
425
6765f3e5
DJ
426/* The size to align memory write packets, when practical. The protocol
427 does not guarantee any alignment, and gdb will generate short
428 writes and unaligned writes, but even as a best-effort attempt this
429 can improve bulk transfers. For instance, if a write is misaligned
430 relative to the target's data bus, the stub may need to make an extra
431 round trip fetching data from the target. This doesn't make a
432 huge difference, but it's easy to do, so we try to be helpful.
433
434 The alignment chosen is arbitrary; usually data bus width is
435 important here, not the possibly larger cache line size. */
436enum { REMOTE_ALIGN_WRITES = 16 };
437
23860348 438/* Prototypes for local functions. */
6d820c5c 439static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 440static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 441 int forever, int *is_notif);
6426a772 442
cbb8991c
DB
443struct remote_state;
444
445static int remote_vkill (int pid, struct remote_state *rs);
446
8020350c
DB
447static void remote_kill_k (void);
448
a14ed312 449static int readchar (int timeout);
c906108c 450
c33e31fd
PA
451static void remote_serial_write (const char *str, int len);
452
a14ed312 453static void interrupt_query (void);
c906108c 454
d62a8ae2
SM
455static void set_general_thread (ptid_t ptid);
456static void set_continue_thread (ptid_t ptid);
c906108c 457
a14ed312 458static void get_offsets (void);
c906108c 459
6d820c5c
DJ
460static void skip_frame (void);
461
462static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 463
a14ed312 464static int hexnumlen (ULONGEST num);
c906108c 465
a14ed312 466static int stubhex (int ch);
c906108c 467
a14ed312 468static int hexnumstr (char *, ULONGEST);
c906108c 469
a14ed312 470static int hexnumnstr (char *, ULONGEST, int);
2df3850c 471
a14ed312 472static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 473
baa336ce 474static void print_packet (const char *);
c906108c 475
a14ed312 476static int stub_unpack_int (char *buff, int fieldlength);
c906108c 477
39f77062 478static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 479
baa336ce 480static int putpkt_binary (const char *buf, int cnt);
c906108c 481
a14ed312 482static void check_binary_download (CORE_ADDR addr);
c906108c 483
5a2468f5 484struct packet_config;
5a2468f5 485
a14ed312 486static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 487
bb572ddd
DJ
488static void show_remote_protocol_packet_cmd (struct ui_file *file,
489 int from_tty,
490 struct cmd_list_element *c,
491 const char *value);
492
82f73884 493static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
256642e8 494static ptid_t read_ptid (const char *buf, const char **obuf);
82f73884 495
c8d104ad
PA
496static void remote_query_supported (void);
497
36d25514 498static void remote_check_symbols (void);
c8d104ad 499
74531fed 500struct stop_reply;
74531fed 501static void stop_reply_xfree (struct stop_reply *);
722247f1 502static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 503static void push_stop_reply (struct stop_reply *);
bcc75809 504static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
505static int peek_stop_reply (ptid_t ptid);
506
cbb8991c
DB
507struct threads_listing_context;
508static void remove_new_fork_children (struct threads_listing_context *);
509
74531fed 510static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 511
d962ef82
DJ
512static int remote_read_description_p (struct target_ops *target);
513
176a6961 514static void remote_console_output (char *msg);
dde08ee1 515
f4abbc16
MM
516static void remote_btrace_reset (void);
517
c0272db5
TW
518static void remote_btrace_maybe_reopen (void);
519
221e1a37
PA
520static int stop_reply_queue_length (void);
521
048094ac
PA
522static void remote_unpush_and_throw (void);
523
b6bb3468
PA
524static struct remote_state *get_remote_state (void);
525
a6b151f1
DJ
526/* For "remote". */
527
528static struct cmd_list_element *remote_cmdlist;
529
bb572ddd
DJ
530/* For "set remote" and "show remote". */
531
532static struct cmd_list_element *remote_set_cmdlist;
533static struct cmd_list_element *remote_show_cmdlist;
534
d458bd84
PA
535/* Stub vCont actions support.
536
537 Each field is a boolean flag indicating whether the stub reports
538 support for the corresponding action. */
539
540struct vCont_action_support
541{
542 /* vCont;t */
de44f5a7 543 bool t = false;
c1e36e3e
PA
544
545 /* vCont;r */
de44f5a7 546 bool r = false;
750ce8d1
YQ
547
548 /* vCont;s */
de44f5a7 549 bool s = false;
750ce8d1
YQ
550
551 /* vCont;S */
de44f5a7 552 bool S = false;
d458bd84
PA
553};
554
c1e36e3e
PA
555/* Controls whether GDB is willing to use range stepping. */
556
557static int use_range_stepping = 1;
558
0d031856
TT
559#define OPAQUETHREADBYTES 8
560
561/* a 64 bit opaque identifier */
562typedef unsigned char threadref[OPAQUETHREADBYTES];
563
564/* About this many threadisds fit in a packet. */
565
566#define MAXTHREADLISTRESULTS 32
567
6f8976bf
YQ
568/* The max number of chars in debug output. The rest of chars are
569 omitted. */
570
571#define REMOTE_DEBUG_MAX_CHAR 512
572
80152258
PA
573/* Data for the vFile:pread readahead cache. */
574
575struct readahead_cache
576{
dd194f6b
PA
577 /* Invalidate the readahead cache. */
578 void invalidate ();
579
580 /* Invalidate the readahead cache if it is holding data for FD. */
581 void invalidate_fd (int fd);
582
583 /* Serve pread from the readahead cache. Returns number of bytes
584 read, or 0 if the request can't be served from the cache. */
585 int pread (int fd, gdb_byte *read_buf, size_t len, ULONGEST offset);
586
80152258
PA
587 /* The file descriptor for the file that is being cached. -1 if the
588 cache is invalid. */
de44f5a7 589 int fd = -1;
80152258
PA
590
591 /* The offset into the file that the cache buffer corresponds
592 to. */
de44f5a7 593 ULONGEST offset = 0;
80152258
PA
594
595 /* The buffer holding the cache contents. */
de44f5a7 596 gdb_byte *buf = nullptr;
80152258
PA
597 /* The buffer's size. We try to read as much as fits into a packet
598 at a time. */
de44f5a7 599 size_t bufsize = 0;
80152258
PA
600
601 /* Cache hit and miss counters. */
de44f5a7
PA
602 ULONGEST hit_count = 0;
603 ULONGEST miss_count = 0;
80152258
PA
604};
605
43c3a0e4
PA
606/* Description of the remote protocol for a given architecture. */
607
608struct packet_reg
609{
610 long offset; /* Offset into G packet. */
611 long regnum; /* GDB's internal register number. */
612 LONGEST pnum; /* Remote protocol register number. */
613 int in_g_packet; /* Always part of G packet. */
614 /* long size in bytes; == register_size (target_gdbarch (), regnum);
615 at present. */
616 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
617 at present. */
618};
619
620struct remote_arch_state
621{
622 explicit remote_arch_state (struct gdbarch *gdbarch);
623
624 /* Description of the remote protocol registers. */
625 long sizeof_g_packet;
626
627 /* Description of the remote protocol registers indexed by REGNUM
628 (making an array gdbarch_num_regs in size). */
629 std::unique_ptr<packet_reg[]> regs;
630
631 /* This is the size (in chars) of the first response to the ``g''
632 packet. It is used as a heuristic when determining the maximum
633 size of memory-read and memory-write packets. A target will
634 typically only reserve a buffer large enough to hold the ``g''
635 packet. The size does not include packet overhead (headers and
636 trailers). */
637 long actual_register_packet_size;
638
639 /* This is the maximum size (in chars) of a non read/write packet.
640 It is also used as a cap on the size of read/write packets. */
641 long remote_packet_size;
642};
643
ea9c271d
DJ
644/* Description of the remote protocol state for the currently
645 connected target. This is per-target state, and independent of the
646 selected architecture. */
647
9d6eea31 648class remote_state
ea9c271d 649{
9d6eea31
PA
650public:
651
de44f5a7
PA
652 remote_state ();
653 ~remote_state ();
654
9d6eea31
PA
655 /* Get the remote arch state for GDBARCH. */
656 struct remote_arch_state *get_remote_arch_state (struct gdbarch *gdbarch);
657
658public: /* data */
659
ea9c271d
DJ
660 /* A buffer to use for incoming packets, and its current size. The
661 buffer is grown dynamically for larger incoming packets.
662 Outgoing packets may also be constructed in this buffer.
663 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
664 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
665 packets. */
666 char *buf;
667 long buf_size;
be2a5f71 668
1e51243a
PA
669 /* True if we're going through initial connection setup (finding out
670 about the remote side's threads, relocating symbols, etc.). */
de44f5a7 671 bool starting_up = false;
1e51243a 672
be2a5f71
DJ
673 /* If we negotiated packet size explicitly (and thus can bypass
674 heuristics for the largest packet size that will not overflow
675 a buffer in the stub), this will be set to that packet size.
676 Otherwise zero, meaning to use the guessed size. */
de44f5a7 677 long explicit_packet_size = 0;
2d717e4f
DJ
678
679 /* remote_wait is normally called when the target is running and
680 waits for a stop reply packet. But sometimes we need to call it
681 when the target is already stopped. We can send a "?" packet
682 and have remote_wait read the response. Or, if we already have
683 the response, we can stash it in BUF and tell remote_wait to
684 skip calling getpkt. This flag is set when BUF contains a
685 stop reply packet and the target is not waiting. */
de44f5a7 686 int cached_wait_status = 0;
a6f3e723
SL
687
688 /* True, if in no ack mode. That is, neither GDB nor the stub will
689 expect acks from each other. The connection is assumed to be
690 reliable. */
de44f5a7 691 bool noack_mode = false;
82f73884
PA
692
693 /* True if we're connected in extended remote mode. */
de44f5a7 694 bool extended = false;
82f73884 695
e24a49d8
PA
696 /* True if we resumed the target and we're waiting for the target to
697 stop. In the mean time, we can't start another command/query.
698 The remote server wouldn't be ready to process it, so we'd
699 timeout waiting for a reply that would never come and eventually
700 we'd close the connection. This can happen in asynchronous mode
701 because we allow GDB commands while the target is running. */
de44f5a7 702 bool waiting_for_stop_reply = false;
74531fed 703
d458bd84 704 /* The status of the stub support for the various vCont actions. */
de44f5a7 705 vCont_action_support supports_vCont;
782b2b07 706
de44f5a7 707 /* True if the user has pressed Ctrl-C, but the target hasn't
3a29589a 708 responded to that. */
de44f5a7 709 bool ctrlc_pending_p = false;
5d93a237 710
048094ac
PA
711 /* True if we saw a Ctrl-C while reading or writing from/to the
712 remote descriptor. At that point it is not safe to send a remote
713 interrupt packet, so we instead remember we saw the Ctrl-C and
714 process it once we're done with sending/receiving the current
715 packet, which should be shortly. If however that takes too long,
716 and the user presses Ctrl-C again, we offer to disconnect. */
de44f5a7 717 bool got_ctrlc_during_io = false;
048094ac 718
5d93a237
TT
719 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
720 remote_open knows that we don't have a file open when the program
721 starts. */
de44f5a7 722 struct serial *remote_desc = nullptr;
47f8a51d
TT
723
724 /* These are the threads which we last sent to the remote system. The
725 TID member will be -1 for all or -2 for not sent yet. */
de44f5a7
PA
726 ptid_t general_thread = null_ptid;
727 ptid_t continue_thread = null_ptid;
262e1174
TT
728
729 /* This is the traceframe which we last selected on the remote system.
730 It will be -1 if no traceframe is selected. */
de44f5a7 731 int remote_traceframe_number = -1;
747dc59d 732
de44f5a7 733 char *last_pass_packet = nullptr;
5e4a05c4
TT
734
735 /* The last QProgramSignals packet sent to the target. We bypass
736 sending a new program signals list down to the target if the new
737 packet is exactly the same as the last we sent. IOW, we only let
738 the target know about program signals list changes. */
de44f5a7 739 char *last_program_signals_packet = nullptr;
b73be471 740
de44f5a7 741 gdb_signal last_sent_signal = GDB_SIGNAL_0;
280ceea3 742
de44f5a7 743 bool last_sent_step = false;
8e88304f 744
3a00c802 745 /* The execution direction of the last resume we got. */
de44f5a7 746 exec_direction_kind last_resume_exec_dir = EXEC_FORWARD;
3a00c802 747
de44f5a7
PA
748 char *finished_object = nullptr;
749 char *finished_annex = nullptr;
750 ULONGEST finished_offset = 0;
b80fafe3
TT
751
752 /* Should we try the 'ThreadInfo' query packet?
753
754 This variable (NOT available to the user: auto-detect only!)
755 determines whether GDB will use the new, simpler "ThreadInfo"
756 query or the older, more complex syntax for thread queries.
757 This is an auto-detect variable (set to true at each connect,
758 and set to false when the target fails to recognize it). */
de44f5a7
PA
759 bool use_threadinfo_query = false;
760 bool use_threadextra_query = false;
88b496c3 761
de44f5a7
PA
762 threadref echo_nextthread {};
763 threadref nextthread {};
764 threadref resultthreadlist[MAXTHREADLISTRESULTS] {};
5965e028
YQ
765
766 /* The state of remote notification. */
de44f5a7 767 struct remote_notif_state *notif_state = nullptr;
f4abbc16
MM
768
769 /* The branch trace configuration. */
de44f5a7 770 struct btrace_config btrace_config {};
15a201c8
GB
771
772 /* The argument to the last "vFile:setfs:" packet we sent, used
773 to avoid sending repeated unnecessary "vFile:setfs:" packets.
774 Initialized to -1 to indicate that no "vFile:setfs:" packet
775 has yet been sent. */
de44f5a7 776 int fs_pid = -1;
80152258
PA
777
778 /* A readahead cache for vFile:pread. Often, reading a binary
779 involves a sequence of small reads. E.g., when parsing an ELF
780 file. A readahead cache helps mostly the case of remote
781 debugging on a connection with higher latency, due to the
782 request/reply nature of the RSP. We only cache data for a single
783 file descriptor at a time. */
784 struct readahead_cache readahead_cache;
9d6eea31
PA
785
786private:
787 /* Mapping of remote protocol data for each gdbarch. Usually there
788 is only one entry here, though we may see more with stubs that
789 support multi-process. */
43c3a0e4 790 std::unordered_map<struct gdbarch *, remote_arch_state>
9d6eea31 791 m_arch_states;
ea9c271d
DJ
792};
793
7aabaf9d
SM
794/* Private data that we'll store in (struct thread_info)->priv. */
795struct remote_thread_info : public private_thread_info
dc146f7c 796{
7aabaf9d
SM
797 std::string extra;
798 std::string name;
799 int core = -1;
799a2abe 800
f6327dcb
KB
801 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
802 sequence of bytes. */
7aabaf9d 803 gdb::byte_vector thread_handle;
f6327dcb 804
799a2abe 805 /* Whether the target stopped for a breakpoint/watchpoint. */
7aabaf9d 806 enum target_stop_reason stop_reason = TARGET_STOPPED_BY_NO_REASON;
799a2abe
PA
807
808 /* This is set to the data address of the access causing the target
809 to stop for a watchpoint. */
7aabaf9d 810 CORE_ADDR watch_data_address = 0;
85ad3aaf
PA
811
812 /* Fields used by the vCont action coalescing implemented in
813 remote_resume / remote_commit_resume. remote_resume stores each
814 thread's last resume request in these fields, so that a later
815 remote_commit_resume knows which is the proper action for this
816 thread to include in the vCont packet. */
817
818 /* True if the last target_resume call for this thread was a step
819 request, false if a continue request. */
7aabaf9d 820 int last_resume_step = 0;
85ad3aaf
PA
821
822 /* The signal specified in the last target_resume call for this
823 thread. */
7aabaf9d 824 gdb_signal last_resume_sig = GDB_SIGNAL_0;
85ad3aaf
PA
825
826 /* Whether this thread was already vCont-resumed on the remote
827 side. */
7aabaf9d 828 int vcont_resumed = 0;
dc146f7c
VP
829};
830
de44f5a7
PA
831remote_state::remote_state ()
832{
833 /* The default buffer size is unimportant; it will be expanded
834 whenever a larger buffer is needed. */
835 this->buf_size = 400;
836 this->buf = (char *) xmalloc (this->buf_size);
837}
838
839remote_state::~remote_state ()
840{
841 xfree (this->last_pass_packet);
842 xfree (this->last_program_signals_packet);
843 xfree (this->buf);
844 xfree (this->finished_object);
845 xfree (this->finished_annex);
846}
847
ea9c271d
DJ
848/* This data could be associated with a target, but we do not always
849 have access to the current target when we need it, so for now it is
850 static. This will be fine for as long as only one target is in use
851 at a time. */
cf792862 852static struct remote_state *remote_state;
ea9c271d
DJ
853
854static struct remote_state *
0b83947e 855get_remote_state_raw (void)
ea9c271d 856{
cf792862
TT
857 return remote_state;
858}
859
35b1e5cc
SS
860/* Utility: generate error from an incoming stub packet. */
861static void
862trace_error (char *buf)
863{
864 if (*buf++ != 'E')
865 return; /* not an error msg */
866 switch (*buf)
867 {
868 case '1': /* malformed packet error */
869 if (*++buf == '0') /* general case: */
870 error (_("remote.c: error in outgoing packet."));
871 else
872 error (_("remote.c: error in outgoing packet at field #%ld."),
873 strtol (buf, NULL, 16));
35b1e5cc
SS
874 default:
875 error (_("Target returns error code '%s'."), buf);
876 }
877}
878
879/* Utility: wait for reply from stub, while accepting "O" packets. */
b6bb3468 880
35b1e5cc 881static char *
b6bb3468 882remote_get_noisy_reply ()
35b1e5cc 883{
b6bb3468
PA
884 struct remote_state *rs = get_remote_state ();
885
35b1e5cc
SS
886 do /* Loop on reply from remote stub. */
887 {
888 char *buf;
a744cf53 889
0df8b418 890 QUIT; /* Allow user to bail out with ^C. */
b6bb3468
PA
891 getpkt (&rs->buf, &rs->buf_size, 0);
892 buf = rs->buf;
ad91cd99 893 if (buf[0] == 'E')
35b1e5cc 894 trace_error (buf);
61012eef 895 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
896 {
897 ULONGEST ul;
898 CORE_ADDR from, to, org_to;
256642e8 899 const char *p, *pp;
dde08ee1 900 int adjusted_size = 0;
7556d4a4 901 int relocated = 0;
dde08ee1
PA
902
903 p = buf + strlen ("qRelocInsn:");
904 pp = unpack_varlen_hex (p, &ul);
905 if (*pp != ';')
cb91c06a 906 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
907 from = ul;
908
909 p = pp + 1;
a9cbf802 910 unpack_varlen_hex (p, &ul);
dde08ee1
PA
911 to = ul;
912
913 org_to = to;
914
492d29ea 915 TRY
dde08ee1 916 {
f5656ead 917 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 918 relocated = 1;
dde08ee1 919 }
492d29ea 920 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
921 {
922 if (ex.error == MEMORY_ERROR)
923 {
924 /* Propagate memory errors silently back to the
925 target. The stub may have limited the range of
926 addresses we can write to, for example. */
927 }
928 else
929 {
930 /* Something unexpectedly bad happened. Be verbose
931 so we can tell what, and propagate the error back
932 to the stub, so it doesn't get stuck waiting for
933 a response. */
934 exception_fprintf (gdb_stderr, ex,
935 _("warning: relocating instruction: "));
936 }
937 putpkt ("E01");
938 }
492d29ea 939 END_CATCH
7556d4a4
PA
940
941 if (relocated)
dde08ee1
PA
942 {
943 adjusted_size = to - org_to;
944
b6bb3468 945 xsnprintf (buf, rs->buf_size, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
946 putpkt (buf);
947 }
dde08ee1 948 }
ad91cd99 949 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
950 remote_console_output (buf + 1); /* 'O' message from stub */
951 else
0df8b418 952 return buf; /* Here's the actual reply. */
35b1e5cc
SS
953 }
954 while (1);
955}
3c3bea1c 956
9d6eea31
PA
957struct remote_arch_state *
958remote_state::get_remote_arch_state (struct gdbarch *gdbarch)
d01949b6 959{
43c3a0e4
PA
960 remote_arch_state *rsa;
961
962 auto it = this->m_arch_states.find (gdbarch);
963 if (it == this->m_arch_states.end ())
9d6eea31 964 {
43c3a0e4
PA
965 auto p = this->m_arch_states.emplace (std::piecewise_construct,
966 std::forward_as_tuple (gdbarch),
967 std::forward_as_tuple (gdbarch));
968 rsa = &p.first->second;
9d6eea31
PA
969
970 /* Make sure that the packet buffer is plenty big enough for
971 this architecture. */
972 if (this->buf_size < rsa->remote_packet_size)
973 {
974 this->buf_size = 2 * rsa->remote_packet_size;
975 this->buf = (char *) xrealloc (this->buf, this->buf_size);
976 }
977 }
43c3a0e4
PA
978 else
979 rsa = &it->second;
980
981 return rsa;
d01949b6
AC
982}
983
0b83947e
DJ
984/* Fetch the global remote target state. */
985
986static struct remote_state *
987get_remote_state (void)
988{
9d6eea31
PA
989 struct remote_state *rs = get_remote_state_raw ();
990
0b83947e
DJ
991 /* Make sure that the remote architecture state has been
992 initialized, because doing so might reallocate rs->buf. Any
993 function which calls getpkt also needs to be mindful of changes
994 to rs->buf, but this call limits the number of places which run
995 into trouble. */
9d6eea31 996 rs->get_remote_arch_state (target_gdbarch ());
0b83947e 997
9d6eea31 998 return rs;
0b83947e
DJ
999}
1000
94585166
DB
1001/* Cleanup routine for the remote module's pspace data. */
1002
1003static void
1004remote_pspace_data_cleanup (struct program_space *pspace, void *arg)
1005{
19ba03f4 1006 char *remote_exec_file = (char *) arg;
94585166
DB
1007
1008 xfree (remote_exec_file);
1009}
1010
1011/* Fetch the remote exec-file from the current program space. */
1012
1013static const char *
1014get_remote_exec_file (void)
1015{
1016 char *remote_exec_file;
1017
19ba03f4
SM
1018 remote_exec_file
1019 = (char *) program_space_data (current_program_space,
1020 remote_pspace_data);
94585166
DB
1021 if (remote_exec_file == NULL)
1022 return "";
1023
1024 return remote_exec_file;
1025}
1026
1027/* Set the remote exec file for PSPACE. */
1028
1029static void
1030set_pspace_remote_exec_file (struct program_space *pspace,
1031 char *remote_exec_file)
1032{
19ba03f4 1033 char *old_file = (char *) program_space_data (pspace, remote_pspace_data);
94585166
DB
1034
1035 xfree (old_file);
1036 set_program_space_data (pspace, remote_pspace_data,
1037 xstrdup (remote_exec_file));
1038}
1039
1040/* The "set/show remote exec-file" set command hook. */
1041
1042static void
eb4c3f4a 1043set_remote_exec_file (const char *ignored, int from_tty,
94585166
DB
1044 struct cmd_list_element *c)
1045{
1046 gdb_assert (remote_exec_file_var != NULL);
1047 set_pspace_remote_exec_file (current_program_space, remote_exec_file_var);
1048}
1049
1050/* The "set/show remote exec-file" show command hook. */
1051
1052static void
1053show_remote_exec_file (struct ui_file *file, int from_tty,
1054 struct cmd_list_element *cmd, const char *value)
1055{
1056 fprintf_filtered (file, "%s\n", remote_exec_file_var);
1057}
1058
74ca34ce
DJ
1059static int
1060compare_pnums (const void *lhs_, const void *rhs_)
1061{
19ba03f4
SM
1062 const struct packet_reg * const *lhs
1063 = (const struct packet_reg * const *) lhs_;
1064 const struct packet_reg * const *rhs
1065 = (const struct packet_reg * const *) rhs_;
74ca34ce
DJ
1066
1067 if ((*lhs)->pnum < (*rhs)->pnum)
1068 return -1;
1069 else if ((*lhs)->pnum == (*rhs)->pnum)
1070 return 0;
1071 else
1072 return 1;
1073}
1074
c21236dc
PA
1075static int
1076map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 1077{
74ca34ce 1078 int regnum, num_remote_regs, offset;
74ca34ce 1079 struct packet_reg **remote_regs;
ea9c271d 1080
4a22f64d 1081 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 1082 {
c21236dc 1083 struct packet_reg *r = &regs[regnum];
baef701f 1084
4a22f64d 1085 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
1086 /* Do not try to fetch zero-sized (placeholder) registers. */
1087 r->pnum = -1;
1088 else
1089 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
1090
b323314b 1091 r->regnum = regnum;
74ca34ce
DJ
1092 }
1093
1094 /* Define the g/G packet format as the contents of each register
1095 with a remote protocol number, in order of ascending protocol
1096 number. */
1097
224c3ddb 1098 remote_regs = XALLOCAVEC (struct packet_reg *, gdbarch_num_regs (gdbarch));
f57d151a 1099 for (num_remote_regs = 0, regnum = 0;
4a22f64d 1100 regnum < gdbarch_num_regs (gdbarch);
f57d151a 1101 regnum++)
c21236dc
PA
1102 if (regs[regnum].pnum != -1)
1103 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 1104
74ca34ce
DJ
1105 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
1106 compare_pnums);
1107
1108 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
1109 {
1110 remote_regs[regnum]->in_g_packet = 1;
1111 remote_regs[regnum]->offset = offset;
4a22f64d 1112 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
1113 }
1114
c21236dc
PA
1115 return offset;
1116}
1117
1118/* Given the architecture described by GDBARCH, return the remote
1119 protocol register's number and the register's offset in the g/G
1120 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1121 If the target does not have a mapping for REGNUM, return false,
1122 otherwise, return true. */
1123
1124int
1125remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
1126 int *pnum, int *poffset)
1127{
c21236dc
PA
1128 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
1129
b80406ac 1130 std::vector<packet_reg> regs (gdbarch_num_regs (gdbarch));
c21236dc 1131
b80406ac 1132 map_regcache_remote_table (gdbarch, regs.data ());
c21236dc
PA
1133
1134 *pnum = regs[regnum].pnum;
1135 *poffset = regs[regnum].offset;
1136
c21236dc
PA
1137 return *pnum != -1;
1138}
1139
9d6eea31 1140remote_arch_state::remote_arch_state (struct gdbarch *gdbarch)
c21236dc 1141{
c21236dc
PA
1142 /* Use the architecture to build a regnum<->pnum table, which will be
1143 1:1 unless a feature set specifies otherwise. */
9d6eea31 1144 this->regs.reset (new packet_reg [gdbarch_num_regs (gdbarch)] ());
c21236dc 1145
74ca34ce
DJ
1146 /* Record the maximum possible size of the g packet - it may turn out
1147 to be smaller. */
9d6eea31
PA
1148 this->sizeof_g_packet
1149 = map_regcache_remote_table (gdbarch, this->regs.get ());
74ca34ce 1150
0df8b418 1151 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
1152 remote stubs have a hardwired buffer size of 400 bytes
1153 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1154 as the maximum packet-size to ensure that the packet and an extra
1155 NUL character can always fit in the buffer. This stops GDB
1156 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d 1157 already a full buffer (As of 1999-12-04 that was most stubs). */
9d6eea31 1158 this->remote_packet_size = 400 - 1;
d01949b6 1159
ea9c271d 1160 /* This one is filled in when a ``g'' packet is received. */
9d6eea31 1161 this->actual_register_packet_size = 0;
ea9c271d
DJ
1162
1163 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
1164 default, adjust the size accordingly. Remember that each byte is
1165 encoded as two characters. 32 is the overhead for the packet
1166 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 1167 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 1168 little. */
9d6eea31
PA
1169 if (this->sizeof_g_packet > ((this->remote_packet_size - 32) / 2))
1170 this->remote_packet_size = (this->sizeof_g_packet * 2 + 32);
ea9c271d
DJ
1171}
1172
1173/* Return the current allowed size of a remote packet. This is
1174 inferred from the current architecture, and should be used to
1175 limit the length of outgoing packets. */
1176static long
1177get_remote_packet_size (void)
1178{
be2a5f71 1179 struct remote_state *rs = get_remote_state ();
9d6eea31 1180 remote_arch_state *rsa = rs->get_remote_arch_state (target_gdbarch ());
ea9c271d 1181
be2a5f71
DJ
1182 if (rs->explicit_packet_size)
1183 return rs->explicit_packet_size;
1184
ea9c271d 1185 return rsa->remote_packet_size;
d01949b6
AC
1186}
1187
ad10f812 1188static struct packet_reg *
5cd63fda
PA
1189packet_reg_from_regnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
1190 long regnum)
ad10f812 1191{
5cd63fda 1192 if (regnum < 0 && regnum >= gdbarch_num_regs (gdbarch))
b323314b
AC
1193 return NULL;
1194 else
ad10f812 1195 {
ea9c271d 1196 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 1197
b323314b
AC
1198 gdb_assert (r->regnum == regnum);
1199 return r;
ad10f812 1200 }
ad10f812
AC
1201}
1202
1203static struct packet_reg *
5cd63fda
PA
1204packet_reg_from_pnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
1205 LONGEST pnum)
ad10f812 1206{
b323314b 1207 int i;
a744cf53 1208
5cd63fda 1209 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
ad10f812 1210 {
ea9c271d 1211 struct packet_reg *r = &rsa->regs[i];
a744cf53 1212
b323314b
AC
1213 if (r->pnum == pnum)
1214 return r;
ad10f812
AC
1215 }
1216 return NULL;
d01949b6
AC
1217}
1218
f6ac5f3d 1219static remote_target remote_ops;
c906108c 1220
f6ac5f3d 1221static extended_remote_target extended_remote_ops;
c906108c 1222
6426a772
JM
1223/* FIXME: cagney/1999-09-23: Even though getpkt was called with
1224 ``forever'' still use the normal timeout mechanism. This is
1225 currently used by the ASYNC code to guarentee that target reads
1226 during the initial connect always time-out. Once getpkt has been
1227 modified to return a timeout indication and, in turn
1228 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 1229 this can go away. */
6426a772
JM
1230static int wait_forever_enabled_p = 1;
1231
9a7071a8
JB
1232/* Allow the user to specify what sequence to send to the remote
1233 when he requests a program interruption: Although ^C is usually
1234 what remote systems expect (this is the default, here), it is
1235 sometimes preferable to send a break. On other systems such
1236 as the Linux kernel, a break followed by g, which is Magic SysRq g
1237 is required in order to interrupt the execution. */
1238const char interrupt_sequence_control_c[] = "Ctrl-C";
1239const char interrupt_sequence_break[] = "BREAK";
1240const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 1241static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
1242 {
1243 interrupt_sequence_control_c,
1244 interrupt_sequence_break,
1245 interrupt_sequence_break_g,
1246 NULL
1247 };
1248static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
1249
1250static void
1251show_interrupt_sequence (struct ui_file *file, int from_tty,
1252 struct cmd_list_element *c,
1253 const char *value)
1254{
1255 if (interrupt_sequence_mode == interrupt_sequence_control_c)
1256 fprintf_filtered (file,
1257 _("Send the ASCII ETX character (Ctrl-c) "
1258 "to the remote target to interrupt the "
1259 "execution of the program.\n"));
1260 else if (interrupt_sequence_mode == interrupt_sequence_break)
1261 fprintf_filtered (file,
1262 _("send a break signal to the remote target "
1263 "to interrupt the execution of the program.\n"));
1264 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
1265 fprintf_filtered (file,
1266 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1267 "the remote target to interrupt the execution "
1268 "of Linux kernel.\n"));
1269 else
1270 internal_error (__FILE__, __LINE__,
1271 _("Invalid value for interrupt_sequence_mode: %s."),
1272 interrupt_sequence_mode);
1273}
6426a772 1274
9a7071a8
JB
1275/* This boolean variable specifies whether interrupt_sequence is sent
1276 to the remote target when gdb connects to it.
1277 This is mostly needed when you debug the Linux kernel: The Linux kernel
1278 expects BREAK g which is Magic SysRq g for connecting gdb. */
1279static int interrupt_on_connect = 0;
c906108c 1280
9a7071a8
JB
1281/* This variable is used to implement the "set/show remotebreak" commands.
1282 Since these commands are now deprecated in favor of "set/show remote
1283 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
1284static int remote_break;
1285
9a7071a8 1286static void
eb4c3f4a 1287set_remotebreak (const char *args, int from_tty, struct cmd_list_element *c)
9a7071a8
JB
1288{
1289 if (remote_break)
1290 interrupt_sequence_mode = interrupt_sequence_break;
1291 else
1292 interrupt_sequence_mode = interrupt_sequence_control_c;
1293}
1294
1295static void
1296show_remotebreak (struct ui_file *file, int from_tty,
1297 struct cmd_list_element *c,
1298 const char *value)
1299{
1300}
1301
c906108c
SS
1302/* This variable sets the number of bits in an address that are to be
1303 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 1304 leading zeros, the entire address would be sent. This variable
c906108c
SS
1305 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1306 initial implementation of remote.c restricted the address sent in
1307 memory packets to ``host::sizeof long'' bytes - (typically 32
1308 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1309 address was never sent. Since fixing this bug may cause a break in
1310 some remote targets this variable is principly provided to
23860348 1311 facilitate backward compatibility. */
c906108c 1312
883b9c6c 1313static unsigned int remote_address_size;
c906108c 1314
11cf8741 1315\f
11cf8741 1316/* User configurable variables for the number of characters in a
ea9c271d
DJ
1317 memory read/write packet. MIN (rsa->remote_packet_size,
1318 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 1319 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
1320 (speed up transfers). The variables ``preferred_*'' (the user
1321 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 1322 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
1323
1324struct memory_packet_config
1325{
a121b7c1 1326 const char *name;
11cf8741
JM
1327 long size;
1328 int fixed_p;
1329};
1330
a5c0808e
PA
1331/* The default max memory-write-packet-size. The 16k is historical.
1332 (It came from older GDB's using alloca for buffers and the
1333 knowledge (folklore?) that some hosts don't cope very well with
1334 large alloca calls.) */
1335#define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384
1336
1337/* The minimum remote packet size for memory transfers. Ensures we
1338 can write at least one byte. */
1339#define MIN_MEMORY_PACKET_SIZE 20
1340
11cf8741
JM
1341/* Compute the current size of a read/write packet. Since this makes
1342 use of ``actual_register_packet_size'' the computation is dynamic. */
1343
1344static long
1345get_memory_packet_size (struct memory_packet_config *config)
1346{
d01949b6 1347 struct remote_state *rs = get_remote_state ();
9d6eea31 1348 remote_arch_state *rsa = rs->get_remote_arch_state (target_gdbarch ());
ea9c271d 1349
11cf8741
JM
1350 long what_they_get;
1351 if (config->fixed_p)
1352 {
1353 if (config->size <= 0)
a5c0808e 1354 what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE;
11cf8741
JM
1355 else
1356 what_they_get = config->size;
1357 }
1358 else
1359 {
ea9c271d 1360 what_they_get = get_remote_packet_size ();
23860348 1361 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1362 if (config->size > 0
1363 && what_they_get > config->size)
1364 what_they_get = config->size;
be2a5f71
DJ
1365
1366 /* Limit it to the size of the targets ``g'' response unless we have
1367 permission from the stub to use a larger packet size. */
1368 if (rs->explicit_packet_size == 0
1369 && rsa->actual_register_packet_size > 0
1370 && what_they_get > rsa->actual_register_packet_size)
1371 what_they_get = rsa->actual_register_packet_size;
11cf8741 1372 }
a5c0808e
PA
1373 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1374 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1375
1376 /* Make sure there is room in the global buffer for this packet
1377 (including its trailing NUL byte). */
1378 if (rs->buf_size < what_they_get + 1)
1379 {
1380 rs->buf_size = 2 * what_they_get;
224c3ddb 1381 rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get);
6d820c5c
DJ
1382 }
1383
11cf8741
JM
1384 return what_they_get;
1385}
1386
0df8b418 1387/* Update the size of a read/write packet. If they user wants
23860348 1388 something really big then do a sanity check. */
11cf8741
JM
1389
1390static void
ac88e2de 1391set_memory_packet_size (const char *args, struct memory_packet_config *config)
11cf8741
JM
1392{
1393 int fixed_p = config->fixed_p;
1394 long size = config->size;
a744cf53 1395
11cf8741 1396 if (args == NULL)
8a3fe4f8 1397 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1398 else if (strcmp (args, "hard") == 0
1399 || strcmp (args, "fixed") == 0)
1400 fixed_p = 1;
1401 else if (strcmp (args, "soft") == 0
1402 || strcmp (args, "limit") == 0)
1403 fixed_p = 0;
1404 else
1405 {
1406 char *end;
a744cf53 1407
11cf8741
JM
1408 size = strtoul (args, &end, 0);
1409 if (args == end)
8a3fe4f8 1410 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1411
1412 /* Instead of explicitly capping the size of a packet to or
1413 disallowing it, the user is allowed to set the size to
1414 something arbitrarily large. */
11cf8741 1415 }
a5c0808e
PA
1416
1417 /* So that the query shows the correct value. */
1418 if (size <= 0)
1419 size = DEFAULT_MAX_MEMORY_PACKET_SIZE;
1420
23860348 1421 /* Extra checks? */
11cf8741
JM
1422 if (fixed_p && !config->fixed_p)
1423 {
e2e0b3e5
AC
1424 if (! query (_("The target may not be able to correctly handle a %s\n"
1425 "of %ld bytes. Change the packet size? "),
11cf8741 1426 config->name, size))
8a3fe4f8 1427 error (_("Packet size not changed."));
11cf8741 1428 }
23860348 1429 /* Update the config. */
11cf8741
JM
1430 config->fixed_p = fixed_p;
1431 config->size = size;
1432}
1433
1434static void
1435show_memory_packet_size (struct memory_packet_config *config)
1436{
a3f17187 1437 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1438 if (config->fixed_p)
a3f17187 1439 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1440 get_memory_packet_size (config));
1441 else
a3f17187 1442 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1443 get_memory_packet_size (config));
1444}
1445
1446static struct memory_packet_config memory_write_packet_config =
1447{
1448 "memory-write-packet-size",
1449};
1450
1451static void
ac88e2de 1452set_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1453{
1454 set_memory_packet_size (args, &memory_write_packet_config);
1455}
1456
1457static void
ac88e2de 1458show_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1459{
1460 show_memory_packet_size (&memory_write_packet_config);
1461}
1462
1463static long
1464get_memory_write_packet_size (void)
1465{
1466 return get_memory_packet_size (&memory_write_packet_config);
1467}
1468
1469static struct memory_packet_config memory_read_packet_config =
1470{
1471 "memory-read-packet-size",
1472};
1473
1474static void
ac88e2de 1475set_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1476{
1477 set_memory_packet_size (args, &memory_read_packet_config);
1478}
1479
1480static void
ac88e2de 1481show_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1482{
1483 show_memory_packet_size (&memory_read_packet_config);
1484}
1485
1486static long
1487get_memory_read_packet_size (void)
1488{
1489 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1490
11cf8741
JM
1491 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1492 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1493 increased beyond this. */
1494 if (size > get_remote_packet_size ())
1495 size = get_remote_packet_size ();
11cf8741
JM
1496 return size;
1497}
1498
11cf8741 1499\f
5a2468f5 1500/* Generic configuration support for packets the stub optionally
0df8b418 1501 supports. Allows the user to specify the use of the packet as well
23860348 1502 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1503
1504enum packet_support
1505 {
1506 PACKET_SUPPORT_UNKNOWN = 0,
1507 PACKET_ENABLE,
1508 PACKET_DISABLE
1509 };
1510
5a2468f5
JM
1511struct packet_config
1512 {
bb572ddd
DJ
1513 const char *name;
1514 const char *title;
4082afcc
PA
1515
1516 /* If auto, GDB auto-detects support for this packet or feature,
1517 either through qSupported, or by trying the packet and looking
1518 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1519 packet. If false, the packet is disabled. Configs that don't
1520 have an associated command always have this set to auto. */
7f19b9a2 1521 enum auto_boolean detect;
4082afcc
PA
1522
1523 /* Does the target support this packet? */
5a2468f5
JM
1524 enum packet_support support;
1525 };
1526
d471ea57 1527/* Analyze a packet's return value and update the packet config
23860348 1528 accordingly. */
d471ea57
AC
1529
1530enum packet_result
1531{
1532 PACKET_ERROR,
1533 PACKET_OK,
1534 PACKET_UNKNOWN
1535};
1536
4082afcc
PA
1537static enum packet_support packet_config_support (struct packet_config *config);
1538static enum packet_support packet_support (int packet);
5a2468f5
JM
1539
1540static void
fba45db2 1541show_packet_config_cmd (struct packet_config *config)
5a2468f5 1542{
a121b7c1 1543 const char *support = "internal-error";
a744cf53 1544
4082afcc 1545 switch (packet_config_support (config))
5a2468f5
JM
1546 {
1547 case PACKET_ENABLE:
1548 support = "enabled";
1549 break;
1550 case PACKET_DISABLE:
1551 support = "disabled";
1552 break;
1553 case PACKET_SUPPORT_UNKNOWN:
1554 support = "unknown";
1555 break;
1556 }
1557 switch (config->detect)
1558 {
7f19b9a2 1559 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1560 printf_filtered (_("Support for the `%s' packet "
1561 "is auto-detected, currently %s.\n"),
37a105a1 1562 config->name, support);
5a2468f5 1563 break;
7f19b9a2
AC
1564 case AUTO_BOOLEAN_TRUE:
1565 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1566 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1567 config->name, support);
8e248173 1568 break;
5a2468f5
JM
1569 }
1570}
1571
1572static void
bb572ddd
DJ
1573add_packet_config_cmd (struct packet_config *config, const char *name,
1574 const char *title, int legacy)
d471ea57 1575{
5a2468f5
JM
1576 char *set_doc;
1577 char *show_doc;
d471ea57 1578 char *cmd_name;
3ed07be4 1579
5a2468f5
JM
1580 config->name = name;
1581 config->title = title;
b435e160
AC
1582 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1583 name, title);
3e43a32a
MS
1584 show_doc = xstrprintf ("Show current use of remote "
1585 "protocol `%s' (%s) packet",
b435e160 1586 name, title);
d471ea57 1587 /* set/show TITLE-packet {auto,on,off} */
b435e160 1588 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1589 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1590 &config->detect, set_doc,
1591 show_doc, NULL, /* help_doc */
4082afcc 1592 NULL,
bb572ddd
DJ
1593 show_remote_protocol_packet_cmd,
1594 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1595 /* The command code copies the documentation strings. */
1596 xfree (set_doc);
1597 xfree (show_doc);
23860348 1598 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1599 if (legacy)
1600 {
1601 char *legacy_name;
a744cf53 1602
b435e160 1603 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1604 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1605 &remote_set_cmdlist);
d471ea57 1606 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1607 &remote_show_cmdlist);
d471ea57 1608 }
5a2468f5
JM
1609}
1610
d471ea57 1611static enum packet_result
a76d924d 1612packet_check_result (const char *buf)
5a2468f5 1613{
d471ea57 1614 if (buf[0] != '\0')
5a2468f5 1615 {
d471ea57 1616 /* The stub recognized the packet request. Check that the
23860348 1617 operation succeeded. */
a76d924d
DJ
1618 if (buf[0] == 'E'
1619 && isxdigit (buf[1]) && isxdigit (buf[2])
1620 && buf[3] == '\0')
1621 /* "Enn" - definitly an error. */
1622 return PACKET_ERROR;
1623
1624 /* Always treat "E." as an error. This will be used for
1625 more verbose error messages, such as E.memtypes. */
1626 if (buf[0] == 'E' && buf[1] == '.')
1627 return PACKET_ERROR;
1628
1629 /* The packet may or may not be OK. Just assume it is. */
1630 return PACKET_OK;
1631 }
1632 else
1633 /* The stub does not support the packet. */
1634 return PACKET_UNKNOWN;
1635}
1636
1637static enum packet_result
1638packet_ok (const char *buf, struct packet_config *config)
1639{
1640 enum packet_result result;
1641
4082afcc
PA
1642 if (config->detect != AUTO_BOOLEAN_TRUE
1643 && config->support == PACKET_DISABLE)
1644 internal_error (__FILE__, __LINE__,
1645 _("packet_ok: attempt to use a disabled packet"));
1646
a76d924d
DJ
1647 result = packet_check_result (buf);
1648 switch (result)
1649 {
1650 case PACKET_OK:
1651 case PACKET_ERROR:
1652 /* The stub recognized the packet request. */
4082afcc 1653 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1654 {
d471ea57
AC
1655 if (remote_debug)
1656 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1657 "Packet %s (%s) is supported\n",
1658 config->name, config->title);
d471ea57 1659 config->support = PACKET_ENABLE;
d471ea57 1660 }
a76d924d
DJ
1661 break;
1662 case PACKET_UNKNOWN:
23860348 1663 /* The stub does not support the packet. */
4082afcc
PA
1664 if (config->detect == AUTO_BOOLEAN_AUTO
1665 && config->support == PACKET_ENABLE)
d471ea57 1666 {
4082afcc
PA
1667 /* If the stub previously indicated that the packet was
1668 supported then there is a protocol error. */
1669 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1670 config->name, config->title);
1671 }
1672 else if (config->detect == AUTO_BOOLEAN_TRUE)
1673 {
1674 /* The user set it wrong. */
1675 error (_("Enabled packet %s (%s) not recognized by stub"),
1676 config->name, config->title);
d471ea57 1677 }
4082afcc
PA
1678
1679 if (remote_debug)
1680 fprintf_unfiltered (gdb_stdlog,
1681 "Packet %s (%s) is NOT supported\n",
1682 config->name, config->title);
1683 config->support = PACKET_DISABLE;
a76d924d 1684 break;
5a2468f5 1685 }
a76d924d
DJ
1686
1687 return result;
5a2468f5
JM
1688}
1689
444abaca
DJ
1690enum {
1691 PACKET_vCont = 0,
1692 PACKET_X,
1693 PACKET_qSymbol,
1694 PACKET_P,
1695 PACKET_p,
1696 PACKET_Z0,
1697 PACKET_Z1,
1698 PACKET_Z2,
1699 PACKET_Z3,
1700 PACKET_Z4,
15a201c8 1701 PACKET_vFile_setfs,
a6b151f1
DJ
1702 PACKET_vFile_open,
1703 PACKET_vFile_pread,
1704 PACKET_vFile_pwrite,
1705 PACKET_vFile_close,
1706 PACKET_vFile_unlink,
b9e7b9c3 1707 PACKET_vFile_readlink,
0a93529c 1708 PACKET_vFile_fstat,
0876f84a 1709 PACKET_qXfer_auxv,
23181151 1710 PACKET_qXfer_features,
c78fa86a 1711 PACKET_qXfer_exec_file,
cfa9d6d9 1712 PACKET_qXfer_libraries,
2268b414 1713 PACKET_qXfer_libraries_svr4,
fd79ecee 1714 PACKET_qXfer_memory_map,
0e7f50da
UW
1715 PACKET_qXfer_spu_read,
1716 PACKET_qXfer_spu_write,
07e059b5 1717 PACKET_qXfer_osdata,
dc146f7c 1718 PACKET_qXfer_threads,
0fb4aa4b 1719 PACKET_qXfer_statictrace_read,
b3b9301e 1720 PACKET_qXfer_traceframe_info,
169081d0 1721 PACKET_qXfer_uib,
711e434b 1722 PACKET_qGetTIBAddr,
444abaca 1723 PACKET_qGetTLSAddr,
be2a5f71 1724 PACKET_qSupported,
bd3eecc3 1725 PACKET_qTStatus,
89be2091 1726 PACKET_QPassSignals,
82075af2 1727 PACKET_QCatchSyscalls,
9b224c5e 1728 PACKET_QProgramSignals,
bc3b087d 1729 PACKET_QSetWorkingDir,
aefd8b33 1730 PACKET_QStartupWithShell,
0a2dde4a
SDJ
1731 PACKET_QEnvironmentHexEncoded,
1732 PACKET_QEnvironmentReset,
1733 PACKET_QEnvironmentUnset,
936d2992 1734 PACKET_qCRC,
08388c79 1735 PACKET_qSearch_memory,
2d717e4f
DJ
1736 PACKET_vAttach,
1737 PACKET_vRun,
a6f3e723 1738 PACKET_QStartNoAckMode,
82f73884 1739 PACKET_vKill,
4aa995e1
PA
1740 PACKET_qXfer_siginfo_read,
1741 PACKET_qXfer_siginfo_write,
0b16c5cf 1742 PACKET_qAttached,
4082afcc
PA
1743
1744 /* Support for conditional tracepoints. */
782b2b07 1745 PACKET_ConditionalTracepoints,
4082afcc
PA
1746
1747 /* Support for target-side breakpoint conditions. */
3788aec7 1748 PACKET_ConditionalBreakpoints,
4082afcc
PA
1749
1750 /* Support for target-side breakpoint commands. */
d3ce09f5 1751 PACKET_BreakpointCommands,
4082afcc
PA
1752
1753 /* Support for fast tracepoints. */
7a697b8d 1754 PACKET_FastTracepoints,
4082afcc
PA
1755
1756 /* Support for static tracepoints. */
0fb4aa4b 1757 PACKET_StaticTracepoints,
4082afcc
PA
1758
1759 /* Support for installing tracepoints while a trace experiment is
1760 running. */
1e4d1764 1761 PACKET_InstallInTrace,
4082afcc 1762
40ab02ce
MS
1763 PACKET_bc,
1764 PACKET_bs,
409873ef 1765 PACKET_TracepointSource,
d914c394 1766 PACKET_QAllow,
78d85199 1767 PACKET_qXfer_fdpic,
03583c20 1768 PACKET_QDisableRandomization,
d1feda86 1769 PACKET_QAgent,
f6f899bf 1770 PACKET_QTBuffer_size,
9accd112
MM
1771 PACKET_Qbtrace_off,
1772 PACKET_Qbtrace_bts,
b20a6524 1773 PACKET_Qbtrace_pt,
9accd112 1774 PACKET_qXfer_btrace,
4082afcc
PA
1775
1776 /* Support for the QNonStop packet. */
1777 PACKET_QNonStop,
1778
65706a29
PA
1779 /* Support for the QThreadEvents packet. */
1780 PACKET_QThreadEvents,
1781
4082afcc
PA
1782 /* Support for multi-process extensions. */
1783 PACKET_multiprocess_feature,
1784
1785 /* Support for enabling and disabling tracepoints while a trace
1786 experiment is running. */
1787 PACKET_EnableDisableTracepoints_feature,
1788
1789 /* Support for collecting strings using the tracenz bytecode. */
1790 PACKET_tracenz_feature,
1791
1792 /* Support for continuing to run a trace experiment while GDB is
1793 disconnected. */
1794 PACKET_DisconnectedTracing_feature,
1795
1796 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1797 PACKET_augmented_libraries_svr4_read_feature,
1798
f4abbc16
MM
1799 /* Support for the qXfer:btrace-conf:read packet. */
1800 PACKET_qXfer_btrace_conf,
1801
d33501a5
MM
1802 /* Support for the Qbtrace-conf:bts:size packet. */
1803 PACKET_Qbtrace_conf_bts_size,
1804
f7e6eed5
PA
1805 /* Support for swbreak+ feature. */
1806 PACKET_swbreak_feature,
1807
1808 /* Support for hwbreak+ feature. */
1809 PACKET_hwbreak_feature,
1810
89245bc0
DB
1811 /* Support for fork events. */
1812 PACKET_fork_event_feature,
1813
1814 /* Support for vfork events. */
1815 PACKET_vfork_event_feature,
1816
b20a6524
MM
1817 /* Support for the Qbtrace-conf:pt:size packet. */
1818 PACKET_Qbtrace_conf_pt_size,
1819
94585166
DB
1820 /* Support for exec events. */
1821 PACKET_exec_event_feature,
1822
750ce8d1
YQ
1823 /* Support for query supported vCont actions. */
1824 PACKET_vContSupported,
1825
de979965
PA
1826 /* Support remote CTRL-C. */
1827 PACKET_vCtrlC,
1828
f2faf941
PA
1829 /* Support TARGET_WAITKIND_NO_RESUMED. */
1830 PACKET_no_resumed,
1831
444abaca
DJ
1832 PACKET_MAX
1833};
506fb367 1834
444abaca 1835static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1836
f7e6eed5
PA
1837/* Returns the packet's corresponding "set remote foo-packet" command
1838 state. See struct packet_config for more details. */
1839
1840static enum auto_boolean
1841packet_set_cmd_state (int packet)
1842{
1843 return remote_protocol_packets[packet].detect;
1844}
1845
4082afcc
PA
1846/* Returns whether a given packet or feature is supported. This takes
1847 into account the state of the corresponding "set remote foo-packet"
1848 command, which may be used to bypass auto-detection. */
dc8acb97 1849
4082afcc
PA
1850static enum packet_support
1851packet_config_support (struct packet_config *config)
1852{
1853 switch (config->detect)
444abaca 1854 {
4082afcc
PA
1855 case AUTO_BOOLEAN_TRUE:
1856 return PACKET_ENABLE;
1857 case AUTO_BOOLEAN_FALSE:
1858 return PACKET_DISABLE;
1859 case AUTO_BOOLEAN_AUTO:
1860 return config->support;
1861 default:
1862 gdb_assert_not_reached (_("bad switch"));
444abaca 1863 }
4082afcc
PA
1864}
1865
1866/* Same as packet_config_support, but takes the packet's enum value as
1867 argument. */
1868
1869static enum packet_support
1870packet_support (int packet)
1871{
1872 struct packet_config *config = &remote_protocol_packets[packet];
1873
1874 return packet_config_support (config);
dc8acb97
MS
1875}
1876
5a2468f5 1877static void
444abaca
DJ
1878show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1879 struct cmd_list_element *c,
1880 const char *value)
5a2468f5 1881{
444abaca 1882 struct packet_config *packet;
5a2468f5 1883
444abaca
DJ
1884 for (packet = remote_protocol_packets;
1885 packet < &remote_protocol_packets[PACKET_MAX];
1886 packet++)
1887 {
1888 if (&packet->detect == c->var)
1889 {
1890 show_packet_config_cmd (packet);
1891 return;
1892 }
1893 }
9b20d036 1894 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1895 c->name);
5a2468f5
JM
1896}
1897
d471ea57
AC
1898/* Should we try one of the 'Z' requests? */
1899
1900enum Z_packet_type
1901{
1902 Z_PACKET_SOFTWARE_BP,
1903 Z_PACKET_HARDWARE_BP,
1904 Z_PACKET_WRITE_WP,
1905 Z_PACKET_READ_WP,
1906 Z_PACKET_ACCESS_WP,
1907 NR_Z_PACKET_TYPES
1908};
96baa820 1909
d471ea57 1910/* For compatibility with older distributions. Provide a ``set remote
23860348 1911 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1912
7f19b9a2 1913static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1914
1915static void
eb4c3f4a 1916set_remote_protocol_Z_packet_cmd (const char *args, int from_tty,
fba45db2 1917 struct cmd_list_element *c)
96baa820 1918{
d471ea57 1919 int i;
a744cf53 1920
d471ea57 1921 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1922 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1923}
1924
1925static void
08546159
AC
1926show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1927 struct cmd_list_element *c,
1928 const char *value)
96baa820 1929{
d471ea57 1930 int i;
a744cf53 1931
d471ea57
AC
1932 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1933 {
444abaca 1934 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1935 }
96baa820
JM
1936}
1937
4082afcc
PA
1938/* Returns true if the multi-process extensions are in effect. */
1939
1940static int
1941remote_multi_process_p (struct remote_state *rs)
1942{
1943 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1944}
1945
de0d863e
DB
1946/* Returns true if fork events are supported. */
1947
1948static int
1949remote_fork_event_p (struct remote_state *rs)
1950{
1951 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1952}
1953
c269dbdb
DB
1954/* Returns true if vfork events are supported. */
1955
1956static int
1957remote_vfork_event_p (struct remote_state *rs)
1958{
1959 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1960}
1961
d46addbb
DB
1962/* Returns true if exec events are supported. */
1963
1964static int
1965remote_exec_event_p (struct remote_state *rs)
1966{
1967 return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
1968}
1969
cbb8991c
DB
1970/* Insert fork catchpoint target routine. If fork events are enabled
1971 then return success, nothing more to do. */
1972
f6ac5f3d
PA
1973int
1974remote_target::insert_fork_catchpoint (int pid)
cbb8991c
DB
1975{
1976 struct remote_state *rs = get_remote_state ();
1977
1978 return !remote_fork_event_p (rs);
1979}
1980
1981/* Remove fork catchpoint target routine. Nothing to do, just
1982 return success. */
1983
f6ac5f3d
PA
1984int
1985remote_target::remove_fork_catchpoint (int pid)
cbb8991c
DB
1986{
1987 return 0;
1988}
1989
1990/* Insert vfork catchpoint target routine. If vfork events are enabled
1991 then return success, nothing more to do. */
1992
f6ac5f3d
PA
1993int
1994remote_target::insert_vfork_catchpoint (int pid)
cbb8991c
DB
1995{
1996 struct remote_state *rs = get_remote_state ();
1997
1998 return !remote_vfork_event_p (rs);
1999}
2000
2001/* Remove vfork catchpoint target routine. Nothing to do, just
2002 return success. */
2003
f6ac5f3d
PA
2004int
2005remote_target::remove_vfork_catchpoint (int pid)
cbb8991c
DB
2006{
2007 return 0;
2008}
2009
d46addbb
DB
2010/* Insert exec catchpoint target routine. If exec events are
2011 enabled, just return success. */
2012
f6ac5f3d
PA
2013int
2014remote_target::insert_exec_catchpoint (int pid)
d46addbb
DB
2015{
2016 struct remote_state *rs = get_remote_state ();
2017
2018 return !remote_exec_event_p (rs);
2019}
2020
2021/* Remove exec catchpoint target routine. Nothing to do, just
2022 return success. */
2023
f6ac5f3d
PA
2024int
2025remote_target::remove_exec_catchpoint (int pid)
d46addbb
DB
2026{
2027 return 0;
2028}
2029
74531fed
PA
2030\f
2031/* Asynchronous signal handle registered as event loop source for
2032 when we have pending events ready to be passed to the core. */
2033
2034static struct async_event_handler *remote_async_inferior_event_token;
2035
c906108c
SS
2036\f
2037
79d7f229
PA
2038static ptid_t magic_null_ptid;
2039static ptid_t not_sent_ptid;
2040static ptid_t any_thread_ptid;
2041
0b16c5cf
PA
2042/* Find out if the stub attached to PID (and hence GDB should offer to
2043 detach instead of killing it when bailing out). */
2044
2045static int
2046remote_query_attached (int pid)
2047{
2048 struct remote_state *rs = get_remote_state ();
bba74b36 2049 size_t size = get_remote_packet_size ();
0b16c5cf 2050
4082afcc 2051 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
2052 return 0;
2053
2054 if (remote_multi_process_p (rs))
bba74b36 2055 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 2056 else
bba74b36 2057 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
2058
2059 putpkt (rs->buf);
2060 getpkt (&rs->buf, &rs->buf_size, 0);
2061
2062 switch (packet_ok (rs->buf,
1554e9be 2063 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
2064 {
2065 case PACKET_OK:
2066 if (strcmp (rs->buf, "1") == 0)
2067 return 1;
2068 break;
2069 case PACKET_ERROR:
2070 warning (_("Remote failure reply: %s"), rs->buf);
2071 break;
2072 case PACKET_UNKNOWN:
2073 break;
2074 }
2075
2076 return 0;
2077}
2078
49c62f2e
PA
2079/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2080 has been invented by GDB, instead of reported by the target. Since
2081 we can be connected to a remote system before before knowing about
2082 any inferior, mark the target with execution when we find the first
2083 inferior. If ATTACHED is 1, then we had just attached to this
2084 inferior. If it is 0, then we just created this inferior. If it
2085 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
2086 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2087 attempt to open this inferior's executable as the main executable
2088 if no main executable is open already. */
1941c569
PA
2089
2090static struct inferior *
1b6e6f5c
GB
2091remote_add_inferior (int fake_pid_p, int pid, int attached,
2092 int try_open_exec)
1941c569 2093{
1941c569
PA
2094 struct inferior *inf;
2095
0b16c5cf
PA
2096 /* Check whether this process we're learning about is to be
2097 considered attached, or if is to be considered to have been
2098 spawned by the stub. */
2099 if (attached == -1)
2100 attached = remote_query_attached (pid);
2101
f5656ead 2102 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
2103 {
2104 /* If the target shares code across all inferiors, then every
2105 attach adds a new inferior. */
2106 inf = add_inferior (pid);
2107
2108 /* ... and every inferior is bound to the same program space.
2109 However, each inferior may still have its own address
2110 space. */
2111 inf->aspace = maybe_new_address_space ();
2112 inf->pspace = current_program_space;
2113 }
2114 else
2115 {
2116 /* In the traditional debugging scenario, there's a 1-1 match
2117 between program/address spaces. We simply bind the inferior
2118 to the program space's address space. */
2119 inf = current_inferior ();
2120 inferior_appeared (inf, pid);
2121 }
1941c569 2122
0b16c5cf 2123 inf->attach_flag = attached;
49c62f2e 2124 inf->fake_pid_p = fake_pid_p;
0b16c5cf 2125
1b6e6f5c
GB
2126 /* If no main executable is currently open then attempt to
2127 open the file that was executed to create this inferior. */
835205d0 2128 if (try_open_exec && get_exec_file (0) == NULL)
bb805577 2129 exec_file_locate_attach (pid, 0, 1);
1b6e6f5c 2130
1941c569
PA
2131 return inf;
2132}
2133
7aabaf9d 2134static remote_thread_info *get_remote_thread_info (thread_info *thread);
85ad3aaf 2135
1941c569
PA
2136/* Add thread PTID to GDB's thread list. Tag it as executing/running
2137 according to RUNNING. */
2138
c906108c 2139static void
0d5b594f 2140remote_add_thread (ptid_t ptid, int running, int executing)
c906108c 2141{
b7ea362b 2142 struct remote_state *rs = get_remote_state ();
85ad3aaf 2143 struct thread_info *thread;
b7ea362b
PA
2144
2145 /* GDB historically didn't pull threads in the initial connection
2146 setup. If the remote target doesn't even have a concept of
2147 threads (e.g., a bare-metal target), even if internally we
2148 consider that a single-threaded target, mentioning a new thread
2149 might be confusing to the user. Be silent then, preserving the
2150 age old behavior. */
2151 if (rs->starting_up)
85ad3aaf 2152 thread = add_thread_silent (ptid);
b7ea362b 2153 else
85ad3aaf 2154 thread = add_thread (ptid);
1941c569 2155
7aabaf9d 2156 get_remote_thread_info (thread)->vcont_resumed = executing;
0d5b594f 2157 set_executing (ptid, executing);
1941c569
PA
2158 set_running (ptid, running);
2159}
2160
2161/* Come here when we learn about a thread id from the remote target.
2162 It may be the first time we hear about such thread, so take the
2163 opportunity to add it to GDB's thread list. In case this is the
2164 first time we're noticing its corresponding inferior, add it to
0d5b594f
PA
2165 GDB's inferior list as well. EXECUTING indicates whether the
2166 thread is (internally) executing or stopped. */
1941c569
PA
2167
2168static void
0d5b594f 2169remote_notice_new_inferior (ptid_t currthread, int executing)
1941c569 2170{
0d5b594f
PA
2171 /* In non-stop mode, we assume new found threads are (externally)
2172 running until proven otherwise with a stop reply. In all-stop,
2173 we can only get here if all threads are stopped. */
2174 int running = target_is_non_stop_p () ? 1 : 0;
2175
c906108c
SS
2176 /* If this is a new thread, add it to GDB's thread list.
2177 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
2178
2179 if (in_thread_list (currthread) && is_exited (currthread))
2180 {
2181 /* We're seeing an event on a thread id we knew had exited.
2182 This has to be a new thread reusing the old id. Add it. */
0d5b594f 2183 remote_add_thread (currthread, running, executing);
82f73884
PA
2184 return;
2185 }
2186
79d7f229 2187 if (!in_thread_list (currthread))
c0a2216e 2188 {
1941c569 2189 struct inferior *inf = NULL;
bad34192 2190 int pid = ptid_get_pid (currthread);
1941c569 2191
bad34192
PA
2192 if (ptid_is_pid (inferior_ptid)
2193 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
2194 {
2195 /* inferior_ptid has no thread member yet. This can happen
2196 with the vAttach -> remote_wait,"TAAthread:" path if the
2197 stub doesn't support qC. This is the first stop reported
2198 after an attach, so this is the main thread. Update the
2199 ptid in the thread list. */
bad34192
PA
2200 if (in_thread_list (pid_to_ptid (pid)))
2201 thread_change_ptid (inferior_ptid, currthread);
2202 else
2203 {
0d5b594f 2204 remote_add_thread (currthread, running, executing);
bad34192
PA
2205 inferior_ptid = currthread;
2206 }
dc146f7c 2207 return;
c0a2216e 2208 }
82f73884
PA
2209
2210 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
2211 {
2212 /* inferior_ptid is not set yet. This can happen with the
2213 vRun -> remote_wait,"TAAthread:" path if the stub
2214 doesn't support qC. This is the first stop reported
2215 after an attach, so this is the main thread. Update the
2216 ptid in the thread list. */
dc146f7c 2217 thread_change_ptid (inferior_ptid, currthread);
82f73884 2218 return;
c0a2216e 2219 }
82f73884 2220
29c87f7f
PA
2221 /* When connecting to a target remote, or to a target
2222 extended-remote which already was debugging an inferior, we
2223 may not know about it yet. Add it before adding its child
2224 thread, so notifications are emitted in a sensible order. */
2225 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
2226 {
2227 struct remote_state *rs = get_remote_state ();
2228 int fake_pid_p = !remote_multi_process_p (rs);
2229
2230 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 2231 ptid_get_pid (currthread), -1, 1);
49c62f2e 2232 }
29c87f7f 2233
82f73884 2234 /* This is really a new thread. Add it. */
0d5b594f 2235 remote_add_thread (currthread, running, executing);
1941c569
PA
2236
2237 /* If we found a new inferior, let the common code do whatever
2238 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
2239 breakpoints), unless we're just setting up an all-stop
2240 connection. */
1941c569 2241 if (inf != NULL)
b7ea362b
PA
2242 {
2243 struct remote_state *rs = get_remote_state ();
2244
6efcd9a8 2245 if (!rs->starting_up)
0d5b594f 2246 notice_new_inferior (currthread, executing, 0);
b7ea362b 2247 }
c0a2216e 2248 }
c906108c
SS
2249}
2250
85ad3aaf 2251/* Return THREAD's private thread data, creating it if necessary. */
dc146f7c 2252
7aabaf9d
SM
2253static remote_thread_info *
2254get_remote_thread_info (thread_info *thread)
dc146f7c 2255{
85ad3aaf 2256 gdb_assert (thread != NULL);
dc146f7c 2257
85ad3aaf 2258 if (thread->priv == NULL)
7aabaf9d 2259 thread->priv.reset (new remote_thread_info);
dc146f7c 2260
7aabaf9d 2261 return static_cast<remote_thread_info *> (thread->priv.get ());
85ad3aaf
PA
2262}
2263
2264/* Return PTID's private thread data, creating it if necessary. */
2265
7aabaf9d
SM
2266static remote_thread_info *
2267get_remote_thread_info (ptid_t ptid)
85ad3aaf
PA
2268{
2269 struct thread_info *info = find_thread_ptid (ptid);
2270
7aabaf9d 2271 return get_remote_thread_info (info);
dc146f7c
VP
2272}
2273
74531fed
PA
2274/* Call this function as a result of
2275 1) A halt indication (T packet) containing a thread id
2276 2) A direct query of currthread
0df8b418 2277 3) Successful execution of set thread */
74531fed
PA
2278
2279static void
47f8a51d 2280record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 2281{
47f8a51d 2282 rs->general_thread = currthread;
74531fed
PA
2283}
2284
89be2091
DJ
2285/* If 'QPassSignals' is supported, tell the remote stub what signals
2286 it can simply pass through to the inferior without reporting. */
2287
f6ac5f3d
PA
2288void
2289remote_target::pass_signals (int numsigs, unsigned char *pass_signals)
89be2091 2290{
4082afcc 2291 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
2292 {
2293 char *pass_packet, *p;
89be2091 2294 int count = 0, i;
747dc59d 2295 struct remote_state *rs = get_remote_state ();
89be2091
DJ
2296
2297 gdb_assert (numsigs < 256);
2298 for (i = 0; i < numsigs; i++)
2299 {
2455069d 2300 if (pass_signals[i])
89be2091
DJ
2301 count++;
2302 }
224c3ddb 2303 pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
89be2091
DJ
2304 strcpy (pass_packet, "QPassSignals:");
2305 p = pass_packet + strlen (pass_packet);
2306 for (i = 0; i < numsigs; i++)
2307 {
2455069d 2308 if (pass_signals[i])
89be2091
DJ
2309 {
2310 if (i >= 16)
2311 *p++ = tohex (i >> 4);
2312 *p++ = tohex (i & 15);
2313 if (count)
2314 *p++ = ';';
2315 else
2316 break;
2317 count--;
2318 }
2319 }
2320 *p = 0;
747dc59d 2321 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 2322 {
89be2091
DJ
2323 putpkt (pass_packet);
2324 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2325 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
2326 if (rs->last_pass_packet)
2327 xfree (rs->last_pass_packet);
2328 rs->last_pass_packet = pass_packet;
89be2091
DJ
2329 }
2330 else
2331 xfree (pass_packet);
2332 }
2333}
2334
82075af2
JS
2335/* If 'QCatchSyscalls' is supported, tell the remote stub
2336 to report syscalls to GDB. */
2337
f6ac5f3d
PA
2338int
2339remote_target::set_syscall_catchpoint (int pid, bool needed, int any_count,
2340 gdb::array_view<const int> syscall_counts)
82075af2 2341{
b80406ac 2342 const char *catch_packet;
82075af2
JS
2343 enum packet_result result;
2344 int n_sysno = 0;
2345
2346 if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
2347 {
2348 /* Not supported. */
2349 return 1;
2350 }
2351
649a140c 2352 if (needed && any_count == 0)
82075af2 2353 {
649a140c
PA
2354 /* Count how many syscalls are to be caught. */
2355 for (size_t i = 0; i < syscall_counts.size (); i++)
82075af2 2356 {
649a140c 2357 if (syscall_counts[i] != 0)
82075af2
JS
2358 n_sysno++;
2359 }
2360 }
2361
2362 if (remote_debug)
2363 {
2364 fprintf_unfiltered (gdb_stdlog,
2365 "remote_set_syscall_catchpoint "
2366 "pid %d needed %d any_count %d n_sysno %d\n",
2367 pid, needed, any_count, n_sysno);
2368 }
2369
1b81856f 2370 std::string built_packet;
82075af2
JS
2371 if (needed)
2372 {
2373 /* Prepare a packet with the sysno list, assuming max 8+1
2374 characters for a sysno. If the resulting packet size is too
2375 big, fallback on the non-selective packet. */
2376 const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
1b81856f
PA
2377 built_packet.reserve (maxpktsz);
2378 built_packet = "QCatchSyscalls:1";
649a140c 2379 if (any_count == 0)
82075af2 2380 {
649a140c
PA
2381 /* Add in each syscall to be caught. */
2382 for (size_t i = 0; i < syscall_counts.size (); i++)
82075af2 2383 {
649a140c
PA
2384 if (syscall_counts[i] != 0)
2385 string_appendf (built_packet, ";%zx", i);
82075af2
JS
2386 }
2387 }
1b81856f 2388 if (built_packet.size () > get_remote_packet_size ())
82075af2
JS
2389 {
2390 /* catch_packet too big. Fallback to less efficient
2391 non selective mode, with GDB doing the filtering. */
b80406ac 2392 catch_packet = "QCatchSyscalls:1";
82075af2 2393 }
b80406ac 2394 else
1b81856f 2395 catch_packet = built_packet.c_str ();
82075af2
JS
2396 }
2397 else
b80406ac 2398 catch_packet = "QCatchSyscalls:0";
82075af2 2399
b80406ac 2400 struct remote_state *rs = get_remote_state ();
82075af2 2401
b80406ac
TT
2402 putpkt (catch_packet);
2403 getpkt (&rs->buf, &rs->buf_size, 0);
2404 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
2405 if (result == PACKET_OK)
2406 return 0;
2407 else
2408 return -1;
82075af2
JS
2409}
2410
9b224c5e
PA
2411/* If 'QProgramSignals' is supported, tell the remote stub what
2412 signals it should pass through to the inferior when detaching. */
2413
f6ac5f3d
PA
2414void
2415remote_target::program_signals (int numsigs, unsigned char *signals)
9b224c5e 2416{
4082afcc 2417 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
2418 {
2419 char *packet, *p;
2420 int count = 0, i;
5e4a05c4 2421 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
2422
2423 gdb_assert (numsigs < 256);
2424 for (i = 0; i < numsigs; i++)
2425 {
2426 if (signals[i])
2427 count++;
2428 }
224c3ddb 2429 packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
9b224c5e
PA
2430 strcpy (packet, "QProgramSignals:");
2431 p = packet + strlen (packet);
2432 for (i = 0; i < numsigs; i++)
2433 {
2434 if (signal_pass_state (i))
2435 {
2436 if (i >= 16)
2437 *p++ = tohex (i >> 4);
2438 *p++ = tohex (i & 15);
2439 if (count)
2440 *p++ = ';';
2441 else
2442 break;
2443 count--;
2444 }
2445 }
2446 *p = 0;
5e4a05c4
TT
2447 if (!rs->last_program_signals_packet
2448 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 2449 {
9b224c5e
PA
2450 putpkt (packet);
2451 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2452 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
2453 xfree (rs->last_program_signals_packet);
2454 rs->last_program_signals_packet = packet;
9b224c5e
PA
2455 }
2456 else
2457 xfree (packet);
2458 }
2459}
2460
79d7f229
PA
2461/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2462 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2463 thread. If GEN is set, set the general thread, if not, then set
2464 the step/continue thread. */
c906108c 2465static void
d62a8ae2 2466set_thread (ptid_t ptid, int gen)
c906108c 2467{
d01949b6 2468 struct remote_state *rs = get_remote_state ();
47f8a51d 2469 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 2470 char *buf = rs->buf;
79d7f229 2471 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 2472
79d7f229 2473 if (ptid_equal (state, ptid))
c906108c
SS
2474 return;
2475
79d7f229
PA
2476 *buf++ = 'H';
2477 *buf++ = gen ? 'g' : 'c';
2478 if (ptid_equal (ptid, magic_null_ptid))
2479 xsnprintf (buf, endbuf - buf, "0");
2480 else if (ptid_equal (ptid, any_thread_ptid))
2481 xsnprintf (buf, endbuf - buf, "0");
2482 else if (ptid_equal (ptid, minus_one_ptid))
2483 xsnprintf (buf, endbuf - buf, "-1");
2484 else
82f73884 2485 write_ptid (buf, endbuf, ptid);
79d7f229 2486 putpkt (rs->buf);
6d820c5c 2487 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2488 if (gen)
47f8a51d 2489 rs->general_thread = ptid;
c906108c 2490 else
47f8a51d 2491 rs->continue_thread = ptid;
c906108c 2492}
79d7f229
PA
2493
2494static void
d62a8ae2 2495set_general_thread (ptid_t ptid)
79d7f229
PA
2496{
2497 set_thread (ptid, 1);
2498}
2499
2500static void
d62a8ae2 2501set_continue_thread (ptid_t ptid)
79d7f229
PA
2502{
2503 set_thread (ptid, 0);
2504}
2505
3c9c4b83
PA
2506/* Change the remote current process. Which thread within the process
2507 ends up selected isn't important, as long as it is the same process
2508 as what INFERIOR_PTID points to.
2509
2510 This comes from that fact that there is no explicit notion of
2511 "selected process" in the protocol. The selected process for
2512 general operations is the process the selected general thread
2513 belongs to. */
2514
2515static void
2516set_general_process (void)
2517{
2518 struct remote_state *rs = get_remote_state ();
2519
2520 /* If the remote can't handle multiple processes, don't bother. */
8020350c 2521 if (!remote_multi_process_p (rs))
3c9c4b83
PA
2522 return;
2523
2524 /* We only need to change the remote current thread if it's pointing
2525 at some other process. */
47f8a51d 2526 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2527 set_general_thread (inferior_ptid);
2528}
2529
c906108c 2530\f
7d1a114c
PA
2531/* Return nonzero if this is the main thread that we made up ourselves
2532 to model non-threaded targets as single-threaded. */
c906108c
SS
2533
2534static int
f6ac5f3d 2535remote_thread_always_alive (ptid_t ptid)
c906108c 2536{
c0a2216e
PA
2537 if (ptid_equal (ptid, magic_null_ptid))
2538 /* The main thread is always alive. */
2539 return 1;
2540
ba348170 2541 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2542 /* The main thread is always alive. This can happen after a
2543 vAttach, if the remote side doesn't support
2544 multi-threading. */
2545 return 1;
2546
7d1a114c
PA
2547 return 0;
2548}
2549
2550/* Return nonzero if the thread PTID is still alive on the remote
2551 system. */
2552
57810aa7 2553bool
f6ac5f3d 2554remote_target::thread_alive (ptid_t ptid)
7d1a114c
PA
2555{
2556 struct remote_state *rs = get_remote_state ();
2557 char *p, *endp;
2558
2559 /* Check if this is a thread that we made up ourselves to model
2560 non-threaded targets as single-threaded. */
f6ac5f3d 2561 if (remote_thread_always_alive (ptid))
7d1a114c
PA
2562 return 1;
2563
82f73884
PA
2564 p = rs->buf;
2565 endp = rs->buf + get_remote_packet_size ();
2566
2567 *p++ = 'T';
2568 write_ptid (p, endp, ptid);
2569
2e9f7625 2570 putpkt (rs->buf);
6d820c5c 2571 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2572 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2573}
2574
79efa585
SM
2575/* Return a pointer to a thread name if we know it and NULL otherwise.
2576 The thread_info object owns the memory for the name. */
2577
f6ac5f3d
PA
2578const char *
2579remote_target::thread_name (struct thread_info *info)
79efa585
SM
2580{
2581 if (info->priv != NULL)
a9334058
SM
2582 {
2583 const std::string &name = get_remote_thread_info (info)->name;
2584 return !name.empty () ? name.c_str () : NULL;
2585 }
79efa585
SM
2586
2587 return NULL;
2588}
2589
c906108c
SS
2590/* About these extended threadlist and threadinfo packets. They are
2591 variable length packets but, the fields within them are often fixed
2592 length. They are redundent enough to send over UDP as is the
2593 remote protocol in general. There is a matching unit test module
2594 in libstub. */
2595
23860348 2596/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2597 libstub protocol encoding, and remote.c. It is not particularly
23860348 2598 changable. */
cce74817
JM
2599
2600/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2601 Plan to fix this. */
cce74817 2602
23860348 2603typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2604
9d1f7ab2 2605/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2606 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2607
2608struct gdb_ext_thread_info
c5aa993b 2609 {
23860348 2610 threadref threadid; /* External form of thread reference. */
2bc416ba 2611 int active; /* Has state interesting to GDB?
23860348 2612 regs, stack. */
2bc416ba 2613 char display[256]; /* Brief state display, name,
cedea757 2614 blocked/suspended. */
23860348 2615 char shortname[32]; /* To be used to name threads. */
2bc416ba 2616 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2617 whatever. */
c5aa993b 2618 };
cce74817
JM
2619
2620/* The volume of remote transfers can be limited by submitting
2621 a mask containing bits specifying the desired information.
2622 Use a union of these values as the 'selection' parameter to
0df8b418 2623 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2624
2625#define TAG_THREADID 1
2626#define TAG_EXISTS 2
2627#define TAG_DISPLAY 4
2628#define TAG_THREADNAME 8
c5aa993b 2629#define TAG_MOREDISPLAY 16
cce74817 2630
23860348 2631#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2632
a14ed312 2633static char *unpack_nibble (char *buf, int *val);
cce74817 2634
a14ed312 2635static char *unpack_byte (char *buf, int *value);
cce74817 2636
a14ed312 2637static char *pack_int (char *buf, int value);
cce74817 2638
a14ed312 2639static char *unpack_int (char *buf, int *value);
cce74817 2640
a14ed312 2641static char *unpack_string (char *src, char *dest, int length);
cce74817 2642
23860348 2643static char *pack_threadid (char *pkt, threadref *id);
cce74817 2644
23860348 2645static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2646
23860348 2647void int_to_threadref (threadref *id, int value);
cce74817 2648
23860348 2649static int threadref_to_int (threadref *ref);
cce74817 2650
23860348 2651static void copy_threadref (threadref *dest, threadref *src);
cce74817 2652
23860348 2653static int threadmatch (threadref *dest, threadref *src);
cce74817 2654
2bc416ba 2655static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2656 threadref *id);
cce74817 2657
a14ed312 2658static int remote_unpack_thread_info_response (char *pkt,
23860348 2659 threadref *expectedref,
a14ed312
KB
2660 struct gdb_ext_thread_info
2661 *info);
cce74817
JM
2662
2663
2bc416ba 2664static int remote_get_threadinfo (threadref *threadid,
23860348 2665 int fieldset, /*TAG mask */
a14ed312 2666 struct gdb_ext_thread_info *info);
cce74817 2667
a14ed312
KB
2668static char *pack_threadlist_request (char *pkt, int startflag,
2669 int threadcount,
23860348 2670 threadref *nextthread);
cce74817 2671
a14ed312
KB
2672static int parse_threadlist_response (char *pkt,
2673 int result_limit,
23860348 2674 threadref *original_echo,
2bc416ba 2675 threadref *resultlist,
23860348 2676 int *doneflag);
cce74817 2677
a14ed312 2678static int remote_get_threadlist (int startflag,
23860348 2679 threadref *nextthread,
a14ed312
KB
2680 int result_limit,
2681 int *done,
2bc416ba 2682 int *result_count,
23860348 2683 threadref *threadlist);
cce74817 2684
23860348 2685typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2686
a14ed312
KB
2687static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2688 void *context, int looplimit);
cce74817 2689
23860348 2690static int remote_newthread_step (threadref *ref, void *context);
cce74817 2691
82f73884
PA
2692
2693/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2694 buffer we're allowed to write to. Returns
2695 BUF+CHARACTERS_WRITTEN. */
2696
2697static char *
2698write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2699{
2700 int pid, tid;
2701 struct remote_state *rs = get_remote_state ();
2702
2703 if (remote_multi_process_p (rs))
2704 {
2705 pid = ptid_get_pid (ptid);
2706 if (pid < 0)
2707 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2708 else
2709 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2710 }
ba348170 2711 tid = ptid_get_lwp (ptid);
82f73884
PA
2712 if (tid < 0)
2713 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2714 else
2715 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2716
2717 return buf;
2718}
2719
256642e8
PA
2720/* Extract a PTID from BUF. If non-null, OBUF is set to one past the
2721 last parsed char. Returns null_ptid if no thread id is found, and
2722 throws an error if the thread id has an invalid format. */
82f73884
PA
2723
2724static ptid_t
256642e8 2725read_ptid (const char *buf, const char **obuf)
82f73884 2726{
256642e8
PA
2727 const char *p = buf;
2728 const char *pp;
82f73884 2729 ULONGEST pid = 0, tid = 0;
82f73884
PA
2730
2731 if (*p == 'p')
2732 {
2733 /* Multi-process ptid. */
2734 pp = unpack_varlen_hex (p + 1, &pid);
2735 if (*pp != '.')
b37520b6 2736 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2737
2738 p = pp;
2739 pp = unpack_varlen_hex (p + 1, &tid);
2740 if (obuf)
2741 *obuf = pp;
ba348170 2742 return ptid_build (pid, tid, 0);
82f73884
PA
2743 }
2744
2745 /* No multi-process. Just a tid. */
2746 pp = unpack_varlen_hex (p, &tid);
2747
c9f35b34
KB
2748 /* Return null_ptid when no thread id is found. */
2749 if (p == pp)
2750 {
2751 if (obuf)
2752 *obuf = pp;
2753 return null_ptid;
2754 }
2755
82f73884 2756 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2757 what's in inferior_ptid, unless it's null at this point. If so,
2758 then since there's no way to know the pid of the reported
2759 threads, use the magic number. */
2760 if (ptid_equal (inferior_ptid, null_ptid))
2761 pid = ptid_get_pid (magic_null_ptid);
2762 else
2763 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2764
2765 if (obuf)
2766 *obuf = pp;
ba348170 2767 return ptid_build (pid, tid, 0);
82f73884
PA
2768}
2769
c906108c 2770static int
fba45db2 2771stubhex (int ch)
c906108c
SS
2772{
2773 if (ch >= 'a' && ch <= 'f')
2774 return ch - 'a' + 10;
2775 if (ch >= '0' && ch <= '9')
2776 return ch - '0';
2777 if (ch >= 'A' && ch <= 'F')
2778 return ch - 'A' + 10;
2779 return -1;
2780}
2781
2782static int
fba45db2 2783stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2784{
2785 int nibble;
2786 int retval = 0;
2787
2788 while (fieldlength)
2789 {
2790 nibble = stubhex (*buff++);
2791 retval |= nibble;
2792 fieldlength--;
2793 if (fieldlength)
2794 retval = retval << 4;
2795 }
2796 return retval;
2797}
2798
c906108c 2799static char *
fba45db2 2800unpack_nibble (char *buf, int *val)
c906108c 2801{
b7589f7d 2802 *val = fromhex (*buf++);
c906108c
SS
2803 return buf;
2804}
2805
c906108c 2806static char *
fba45db2 2807unpack_byte (char *buf, int *value)
c906108c
SS
2808{
2809 *value = stub_unpack_int (buf, 2);
2810 return buf + 2;
2811}
2812
2813static char *
fba45db2 2814pack_int (char *buf, int value)
c906108c
SS
2815{
2816 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2817 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2818 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2819 buf = pack_hex_byte (buf, (value & 0xff));
2820 return buf;
2821}
2822
2823static char *
fba45db2 2824unpack_int (char *buf, int *value)
c906108c
SS
2825{
2826 *value = stub_unpack_int (buf, 8);
2827 return buf + 8;
2828}
2829
23860348 2830#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2831static char *pack_string (char *pkt, char *string);
c906108c
SS
2832
2833static char *
fba45db2 2834pack_string (char *pkt, char *string)
c906108c
SS
2835{
2836 char ch;
2837 int len;
2838
2839 len = strlen (string);
2840 if (len > 200)
23860348 2841 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2842 pkt = pack_hex_byte (pkt, len);
2843 while (len-- > 0)
2844 {
2845 ch = *string++;
2846 if ((ch == '\0') || (ch == '#'))
23860348 2847 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2848 *pkt++ = ch;
2849 }
2850 return pkt;
2851}
2852#endif /* 0 (unused) */
2853
2854static char *
fba45db2 2855unpack_string (char *src, char *dest, int length)
c906108c
SS
2856{
2857 while (length--)
2858 *dest++ = *src++;
2859 *dest = '\0';
2860 return src;
2861}
2862
2863static char *
fba45db2 2864pack_threadid (char *pkt, threadref *id)
c906108c
SS
2865{
2866 char *limit;
2867 unsigned char *altid;
2868
2869 altid = (unsigned char *) id;
2870 limit = pkt + BUF_THREAD_ID_SIZE;
2871 while (pkt < limit)
2872 pkt = pack_hex_byte (pkt, *altid++);
2873 return pkt;
2874}
2875
2876
2877static char *
fba45db2 2878unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2879{
2880 char *altref;
2881 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2882 int x, y;
2883
2884 altref = (char *) id;
2885
2886 while (inbuf < limit)
2887 {
2888 x = stubhex (*inbuf++);
2889 y = stubhex (*inbuf++);
2890 *altref++ = (x << 4) | y;
2891 }
2892 return inbuf;
2893}
2894
2895/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2896 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2897 to use 64bit thread references internally. This is an adapter
2898 function. */
2899
2900void
fba45db2 2901int_to_threadref (threadref *id, int value)
c906108c
SS
2902{
2903 unsigned char *scan;
2904
2905 scan = (unsigned char *) id;
2906 {
2907 int i = 4;
2908 while (i--)
2909 *scan++ = 0;
2910 }
2911 *scan++ = (value >> 24) & 0xff;
2912 *scan++ = (value >> 16) & 0xff;
2913 *scan++ = (value >> 8) & 0xff;
2914 *scan++ = (value & 0xff);
2915}
2916
2917static int
fba45db2 2918threadref_to_int (threadref *ref)
c906108c
SS
2919{
2920 int i, value = 0;
2921 unsigned char *scan;
2922
cfd77fa1 2923 scan = *ref;
c906108c
SS
2924 scan += 4;
2925 i = 4;
2926 while (i-- > 0)
2927 value = (value << 8) | ((*scan++) & 0xff);
2928 return value;
2929}
2930
2931static void
fba45db2 2932copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2933{
2934 int i;
2935 unsigned char *csrc, *cdest;
2936
2937 csrc = (unsigned char *) src;
2938 cdest = (unsigned char *) dest;
2939 i = 8;
2940 while (i--)
2941 *cdest++ = *csrc++;
2942}
2943
2944static int
fba45db2 2945threadmatch (threadref *dest, threadref *src)
c906108c 2946{
23860348 2947 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2948#if 0
2949 unsigned char *srcp, *destp;
2950 int i, result;
2951 srcp = (char *) src;
2952 destp = (char *) dest;
2953
2954 result = 1;
2955 while (i-- > 0)
2956 result &= (*srcp++ == *destp++) ? 1 : 0;
2957 return result;
2958#endif
2959 return 1;
2960}
2961
2962/*
c5aa993b
JM
2963 threadid:1, # always request threadid
2964 context_exists:2,
2965 display:4,
2966 unique_name:8,
2967 more_display:16
2968 */
c906108c
SS
2969
2970/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2971
2972static char *
fba45db2 2973pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2974{
23860348
MS
2975 *pkt++ = 'q'; /* Info Query */
2976 *pkt++ = 'P'; /* process or thread info */
2977 pkt = pack_int (pkt, mode); /* mode */
c906108c 2978 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2979 *pkt = '\0'; /* terminate */
c906108c
SS
2980 return pkt;
2981}
2982
23860348 2983/* These values tag the fields in a thread info response packet. */
c906108c 2984/* Tagging the fields allows us to request specific fields and to
23860348 2985 add more fields as time goes by. */
c906108c 2986
23860348 2987#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2988#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2989 fetch registers and its stack? */
c5aa993b 2990#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2991#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2992#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2993 the process. */
c906108c
SS
2994
2995static int
fba45db2
KB
2996remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2997 struct gdb_ext_thread_info *info)
c906108c 2998{
d01949b6 2999 struct remote_state *rs = get_remote_state ();
c906108c 3000 int mask, length;
cfd77fa1 3001 int tag;
c906108c 3002 threadref ref;
6d820c5c 3003 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
3004 int retval = 1;
3005
23860348 3006 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
3007 info->active = 0;
3008 info->display[0] = '\0';
3009 info->shortname[0] = '\0';
3010 info->more_display[0] = '\0';
3011
23860348
MS
3012 /* Assume the characters indicating the packet type have been
3013 stripped. */
c906108c
SS
3014 pkt = unpack_int (pkt, &mask); /* arg mask */
3015 pkt = unpack_threadid (pkt, &ref);
3016
3017 if (mask == 0)
8a3fe4f8 3018 warning (_("Incomplete response to threadinfo request."));
c906108c 3019 if (!threadmatch (&ref, expectedref))
23860348 3020 { /* This is an answer to a different request. */
8a3fe4f8 3021 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
3022 return 0;
3023 }
3024 copy_threadref (&info->threadid, &ref);
3025
23860348 3026 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 3027
23860348
MS
3028 /* Packets are terminated with nulls. */
3029 while ((pkt < limit) && mask && *pkt)
c906108c
SS
3030 {
3031 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
3032 pkt = unpack_byte (pkt, &length); /* length */
3033 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 3034 {
8a3fe4f8 3035 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
3036 retval = 0;
3037 break;
3038 }
3039 if (tag == TAG_THREADID)
3040 {
3041 if (length != 16)
3042 {
8a3fe4f8 3043 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
3044 retval = 0;
3045 break;
3046 }
3047 pkt = unpack_threadid (pkt, &ref);
3048 mask = mask & ~TAG_THREADID;
3049 continue;
3050 }
3051 if (tag == TAG_EXISTS)
3052 {
3053 info->active = stub_unpack_int (pkt, length);
3054 pkt += length;
3055 mask = mask & ~(TAG_EXISTS);
3056 if (length > 8)
3057 {
8a3fe4f8 3058 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
3059 retval = 0;
3060 break;
3061 }
3062 continue;
3063 }
3064 if (tag == TAG_THREADNAME)
3065 {
3066 pkt = unpack_string (pkt, &info->shortname[0], length);
3067 mask = mask & ~TAG_THREADNAME;
3068 continue;
3069 }
3070 if (tag == TAG_DISPLAY)
3071 {
3072 pkt = unpack_string (pkt, &info->display[0], length);
3073 mask = mask & ~TAG_DISPLAY;
3074 continue;
3075 }
3076 if (tag == TAG_MOREDISPLAY)
3077 {
3078 pkt = unpack_string (pkt, &info->more_display[0], length);
3079 mask = mask & ~TAG_MOREDISPLAY;
3080 continue;
3081 }
8a3fe4f8 3082 warning (_("ERROR RMT: unknown thread info tag."));
23860348 3083 break; /* Not a tag we know about. */
c906108c
SS
3084 }
3085 return retval;
3086}
3087
3088static int
fba45db2
KB
3089remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
3090 struct gdb_ext_thread_info *info)
c906108c 3091{
d01949b6 3092 struct remote_state *rs = get_remote_state ();
c906108c 3093 int result;
c906108c 3094
2e9f7625
DJ
3095 pack_threadinfo_request (rs->buf, fieldset, threadid);
3096 putpkt (rs->buf);
6d820c5c 3097 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
3098
3099 if (rs->buf[0] == '\0')
3100 return 0;
3101
2e9f7625 3102 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 3103 threadid, info);
c906108c
SS
3104 return result;
3105}
3106
c906108c
SS
3107/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3108
3109static char *
fba45db2
KB
3110pack_threadlist_request (char *pkt, int startflag, int threadcount,
3111 threadref *nextthread)
c906108c
SS
3112{
3113 *pkt++ = 'q'; /* info query packet */
3114 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 3115 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
3116 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
3117 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
3118 *pkt = '\0';
3119 return pkt;
3120}
3121
3122/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3123
3124static int
fba45db2
KB
3125parse_threadlist_response (char *pkt, int result_limit,
3126 threadref *original_echo, threadref *resultlist,
3127 int *doneflag)
c906108c 3128{
d01949b6 3129 struct remote_state *rs = get_remote_state ();
c906108c
SS
3130 char *limit;
3131 int count, resultcount, done;
3132
3133 resultcount = 0;
3134 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 3135 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 3136 /* done parse past here */
c906108c
SS
3137 pkt = unpack_byte (pkt, &count); /* count field */
3138 pkt = unpack_nibble (pkt, &done);
3139 /* The first threadid is the argument threadid. */
3140 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
3141 while ((count-- > 0) && (pkt < limit))
3142 {
3143 pkt = unpack_threadid (pkt, resultlist++);
3144 if (resultcount++ >= result_limit)
3145 break;
3146 }
3147 if (doneflag)
3148 *doneflag = done;
3149 return resultcount;
3150}
3151
6dc54d91
PA
3152/* Fetch the next batch of threads from the remote. Returns -1 if the
3153 qL packet is not supported, 0 on error and 1 on success. */
3154
c906108c 3155static int
fba45db2
KB
3156remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
3157 int *done, int *result_count, threadref *threadlist)
c906108c 3158{
d01949b6 3159 struct remote_state *rs = get_remote_state ();
c906108c
SS
3160 int result = 1;
3161
23860348 3162 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
3163 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
3164 >= get_remote_packet_size ())
ea9c271d 3165 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 3166
6d820c5c
DJ
3167 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
3168 putpkt (rs->buf);
3169 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 3170 if (*rs->buf == '\0')
6dc54d91
PA
3171 {
3172 /* Packet not supported. */
3173 return -1;
3174 }
3175
3176 *result_count =
3177 parse_threadlist_response (rs->buf + 2, result_limit,
3178 &rs->echo_nextthread, threadlist, done);
c906108c 3179
0d031856 3180 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 3181 {
23860348
MS
3182 /* FIXME: This is a good reason to drop the packet. */
3183 /* Possably, there is a duplicate response. */
c906108c
SS
3184 /* Possabilities :
3185 retransmit immediatly - race conditions
3186 retransmit after timeout - yes
3187 exit
3188 wait for packet, then exit
3189 */
8a3fe4f8 3190 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 3191 return 0; /* I choose simply exiting. */
c906108c
SS
3192 }
3193 if (*result_count <= 0)
3194 {
3195 if (*done != 1)
3196 {
8a3fe4f8 3197 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
3198 result = 0;
3199 }
3200 return result; /* break; */
3201 }
3202 if (*result_count > result_limit)
3203 {
3204 *result_count = 0;
8a3fe4f8 3205 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
3206 return 0;
3207 }
3208 return result;
3209}
3210
6dc54d91
PA
3211/* Fetch the list of remote threads, with the qL packet, and call
3212 STEPFUNCTION for each thread found. Stops iterating and returns 1
3213 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3214 STEPFUNCTION returns false. If the packet is not supported,
3215 returns -1. */
c906108c 3216
c906108c 3217static int
fba45db2
KB
3218remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
3219 int looplimit)
c906108c 3220{
0d031856 3221 struct remote_state *rs = get_remote_state ();
c906108c
SS
3222 int done, i, result_count;
3223 int startflag = 1;
3224 int result = 1;
3225 int loopcount = 0;
c906108c
SS
3226
3227 done = 0;
3228 while (!done)
3229 {
3230 if (loopcount++ > looplimit)
3231 {
3232 result = 0;
8a3fe4f8 3233 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
3234 break;
3235 }
6dc54d91
PA
3236 result = remote_get_threadlist (startflag, &rs->nextthread,
3237 MAXTHREADLISTRESULTS,
3238 &done, &result_count,
3239 rs->resultthreadlist);
3240 if (result <= 0)
3241 break;
23860348 3242 /* Clear for later iterations. */
c906108c
SS
3243 startflag = 0;
3244 /* Setup to resume next batch of thread references, set nextthread. */
3245 if (result_count >= 1)
0d031856
TT
3246 copy_threadref (&rs->nextthread,
3247 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
3248 i = 0;
3249 while (result_count--)
6dc54d91
PA
3250 {
3251 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
3252 {
3253 result = 0;
3254 break;
3255 }
3256 }
c906108c
SS
3257 }
3258 return result;
3259}
3260
6dc54d91
PA
3261/* A thread found on the remote target. */
3262
21fe1c75 3263struct thread_item
6dc54d91 3264{
21fe1c75
SM
3265 explicit thread_item (ptid_t ptid_)
3266 : ptid (ptid_)
3267 {}
3268
3269 thread_item (thread_item &&other) = default;
3270 thread_item &operator= (thread_item &&other) = default;
3271
3272 DISABLE_COPY_AND_ASSIGN (thread_item);
3273
6dc54d91
PA
3274 /* The thread's PTID. */
3275 ptid_t ptid;
3276
21fe1c75
SM
3277 /* The thread's extra info. */
3278 std::string extra;
6dc54d91 3279
21fe1c75
SM
3280 /* The thread's name. */
3281 std::string name;
79efa585 3282
6dc54d91 3283 /* The core the thread was running on. -1 if not known. */
21fe1c75 3284 int core = -1;
f6327dcb
KB
3285
3286 /* The thread handle associated with the thread. */
21fe1c75 3287 gdb::byte_vector thread_handle;
21fe1c75 3288};
6dc54d91
PA
3289
3290/* Context passed around to the various methods listing remote
3291 threads. As new threads are found, they're added to the ITEMS
3292 vector. */
3293
3294struct threads_listing_context
3295{
21fe1c75
SM
3296 /* Return true if this object contains an entry for a thread with ptid
3297 PTID. */
6dc54d91 3298
21fe1c75
SM
3299 bool contains_thread (ptid_t ptid) const
3300 {
3301 auto match_ptid = [&] (const thread_item &item)
3302 {
3303 return item.ptid == ptid;
3304 };
80134cf5 3305
21fe1c75
SM
3306 auto it = std::find_if (this->items.begin (),
3307 this->items.end (),
3308 match_ptid);
80134cf5 3309
21fe1c75
SM
3310 return it != this->items.end ();
3311 }
80134cf5 3312
21fe1c75 3313 /* Remove the thread with ptid PTID. */
80134cf5 3314
21fe1c75
SM
3315 void remove_thread (ptid_t ptid)
3316 {
3317 auto match_ptid = [&] (const thread_item &item)
3318 {
3319 return item.ptid == ptid;
3320 };
cbb8991c 3321
21fe1c75
SM
3322 auto it = std::remove_if (this->items.begin (),
3323 this->items.end (),
3324 match_ptid);
cbb8991c 3325
21fe1c75
SM
3326 if (it != this->items.end ())
3327 this->items.erase (it);
3328 }
3329
3330 /* The threads found on the remote target. */
3331 std::vector<thread_item> items;
3332};
cbb8991c 3333
c906108c 3334static int
6dc54d91 3335remote_newthread_step (threadref *ref, void *data)
c906108c 3336{
19ba03f4
SM
3337 struct threads_listing_context *context
3338 = (struct threads_listing_context *) data;
21fe1c75
SM
3339 int pid = inferior_ptid.pid ();
3340 int lwp = threadref_to_int (ref);
3341 ptid_t ptid (pid, lwp);
6dc54d91 3342
21fe1c75 3343 context->items.emplace_back (ptid);
6dc54d91 3344
c906108c
SS
3345 return 1; /* continue iterator */
3346}
3347
3348#define CRAZY_MAX_THREADS 1000
3349
39f77062
KB
3350static ptid_t
3351remote_current_thread (ptid_t oldpid)
c906108c 3352{
d01949b6 3353 struct remote_state *rs = get_remote_state ();
c906108c
SS
3354
3355 putpkt ("qC");
6d820c5c 3356 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3357 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34 3358 {
256642e8 3359 const char *obuf;
c9f35b34
KB
3360 ptid_t result;
3361
3362 result = read_ptid (&rs->buf[2], &obuf);
3363 if (*obuf != '\0' && remote_debug)
3364 fprintf_unfiltered (gdb_stdlog,
3365 "warning: garbage in qC reply\n");
3366
3367 return result;
3368 }
c906108c
SS
3369 else
3370 return oldpid;
3371}
3372
6dc54d91 3373/* List remote threads using the deprecated qL packet. */
cce74817 3374
6dc54d91
PA
3375static int
3376remote_get_threads_with_ql (struct target_ops *ops,
3377 struct threads_listing_context *context)
c906108c 3378{
6dc54d91
PA
3379 if (remote_threadlist_iterator (remote_newthread_step, context,
3380 CRAZY_MAX_THREADS) >= 0)
3381 return 1;
3382
3383 return 0;
c906108c
SS
3384}
3385
dc146f7c
VP
3386#if defined(HAVE_LIBEXPAT)
3387
dc146f7c
VP
3388static void
3389start_thread (struct gdb_xml_parser *parser,
3390 const struct gdb_xml_element *element,
4d0fdd9b
SM
3391 void *user_data,
3392 std::vector<gdb_xml_value> &attributes)
dc146f7c 3393{
19ba03f4
SM
3394 struct threads_listing_context *data
3395 = (struct threads_listing_context *) user_data;
3d2c1d41 3396 struct gdb_xml_value *attr;
dc146f7c 3397
4d0fdd9b 3398 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
21fe1c75
SM
3399 ptid_t ptid = read_ptid (id, NULL);
3400
3401 data->items.emplace_back (ptid);
3402 thread_item &item = data->items.back ();
dc146f7c 3403
3d2c1d41
PA
3404 attr = xml_find_attribute (attributes, "core");
3405 if (attr != NULL)
4d0fdd9b 3406 item.core = *(ULONGEST *) attr->value.get ();
dc146f7c 3407
79efa585 3408 attr = xml_find_attribute (attributes, "name");
21fe1c75 3409 if (attr != NULL)
4d0fdd9b 3410 item.name = (const char *) attr->value.get ();
79efa585 3411
f6327dcb
KB
3412 attr = xml_find_attribute (attributes, "handle");
3413 if (attr != NULL)
4d0fdd9b 3414 item.thread_handle = hex2bin ((const char *) attr->value.get ());
dc146f7c
VP
3415}
3416
3417static void
3418end_thread (struct gdb_xml_parser *parser,
3419 const struct gdb_xml_element *element,
3420 void *user_data, const char *body_text)
3421{
19ba03f4
SM
3422 struct threads_listing_context *data
3423 = (struct threads_listing_context *) user_data;
dc146f7c 3424
21fe1c75
SM
3425 if (body_text != NULL && *body_text != '\0')
3426 data->items.back ().extra = body_text;
dc146f7c
VP
3427}
3428
3429const struct gdb_xml_attribute thread_attributes[] = {
3430 { "id", GDB_XML_AF_NONE, NULL, NULL },
3431 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
79efa585 3432 { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
f6327dcb 3433 { "handle", GDB_XML_AF_OPTIONAL, NULL, NULL },
dc146f7c
VP
3434 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3435};
3436
3437const struct gdb_xml_element thread_children[] = {
3438 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3439};
3440
3441const struct gdb_xml_element threads_children[] = {
3442 { "thread", thread_attributes, thread_children,
3443 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3444 start_thread, end_thread },
3445 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3446};
3447
3448const struct gdb_xml_element threads_elements[] = {
3449 { "threads", NULL, threads_children,
3450 GDB_XML_EF_NONE, NULL, NULL },
3451 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3452};
3453
3454#endif
3455
6dc54d91 3456/* List remote threads using qXfer:threads:read. */
9d1f7ab2 3457
6dc54d91
PA
3458static int
3459remote_get_threads_with_qxfer (struct target_ops *ops,
3460 struct threads_listing_context *context)
0f71a2f6 3461{
dc146f7c 3462#if defined(HAVE_LIBEXPAT)
4082afcc 3463 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 3464 {
9018be22 3465 gdb::optional<gdb::char_vector> xml
b7b030ad 3466 = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
efc0eabd 3467
9018be22 3468 if (xml && (*xml)[0] != '\0')
dc146f7c 3469 {
6dc54d91 3470 gdb_xml_parse_quick (_("threads"), "threads.dtd",
9018be22 3471 threads_elements, xml->data (), context);
dc146f7c
VP
3472 }
3473
6dc54d91 3474 return 1;
dc146f7c
VP
3475 }
3476#endif
3477
6dc54d91
PA
3478 return 0;
3479}
3480
3481/* List remote threads using qfThreadInfo/qsThreadInfo. */
3482
3483static int
3484remote_get_threads_with_qthreadinfo (struct target_ops *ops,
3485 struct threads_listing_context *context)
3486{
3487 struct remote_state *rs = get_remote_state ();
3488
b80fafe3 3489 if (rs->use_threadinfo_query)
9d1f7ab2 3490 {
256642e8 3491 const char *bufp;
6dc54d91 3492
9d1f7ab2 3493 putpkt ("qfThreadInfo");
6d820c5c 3494 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3495 bufp = rs->buf;
9d1f7ab2 3496 if (bufp[0] != '\0') /* q packet recognized */
802188a7 3497 {
9d1f7ab2
MS
3498 while (*bufp++ == 'm') /* reply contains one or more TID */
3499 {
3500 do
3501 {
21fe1c75
SM
3502 ptid_t ptid = read_ptid (bufp, &bufp);
3503 context->items.emplace_back (ptid);
9d1f7ab2
MS
3504 }
3505 while (*bufp++ == ','); /* comma-separated list */
3506 putpkt ("qsThreadInfo");
6d820c5c 3507 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3508 bufp = rs->buf;
9d1f7ab2 3509 }
6dc54d91
PA
3510 return 1;
3511 }
3512 else
3513 {
3514 /* Packet not recognized. */
3515 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3516 }
3517 }
3518
6dc54d91
PA
3519 return 0;
3520}
3521
e8032dde 3522/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3523 targets. */
3524
f6ac5f3d
PA
3525void
3526remote_target::update_thread_list ()
6dc54d91 3527{
6dc54d91 3528 struct threads_listing_context context;
ab970af1 3529 int got_list = 0;
e8032dde 3530
6dc54d91
PA
3531 /* We have a few different mechanisms to fetch the thread list. Try
3532 them all, starting with the most preferred one first, falling
3533 back to older methods. */
f6ac5f3d
PA
3534 if (remote_get_threads_with_qxfer (this, &context)
3535 || remote_get_threads_with_qthreadinfo (this, &context)
3536 || remote_get_threads_with_ql (this, &context))
6dc54d91 3537 {
ab970af1
PA
3538 struct thread_info *tp, *tmp;
3539
3540 got_list = 1;
3541
21fe1c75 3542 if (context.items.empty ()
f6ac5f3d 3543 && remote_thread_always_alive (inferior_ptid))
7d1a114c
PA
3544 {
3545 /* Some targets don't really support threads, but still
3546 reply an (empty) thread list in response to the thread
3547 listing packets, instead of replying "packet not
3548 supported". Exit early so we don't delete the main
3549 thread. */
7d1a114c
PA
3550 return;
3551 }
3552
ab970af1
PA
3553 /* CONTEXT now holds the current thread list on the remote
3554 target end. Delete GDB-side threads no longer found on the
3555 target. */
8a06aea7 3556 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3557 {
21fe1c75 3558 if (!context.contains_thread (tp->ptid))
ab970af1
PA
3559 {
3560 /* Not found. */
3561 delete_thread (tp->ptid);
3562 }
cbb8991c
DB
3563 }
3564
3565 /* Remove any unreported fork child threads from CONTEXT so
3566 that we don't interfere with follow fork, which is where
3567 creation of such threads is handled. */
3568 remove_new_fork_children (&context);
74531fed 3569
ab970af1 3570 /* And now add threads we don't know about yet to our list. */
21fe1c75 3571 for (thread_item &item : context.items)
6dc54d91 3572 {
21fe1c75 3573 if (item.ptid != null_ptid)
6dc54d91 3574 {
6dc54d91 3575 /* In non-stop mode, we assume new found threads are
0d5b594f
PA
3576 executing until proven otherwise with a stop reply.
3577 In all-stop, we can only get here if all threads are
6dc54d91 3578 stopped. */
0d5b594f 3579 int executing = target_is_non_stop_p () ? 1 : 0;
6dc54d91 3580
21fe1c75 3581 remote_notice_new_inferior (item.ptid, executing);
6dc54d91 3582
7aabaf9d 3583 remote_thread_info *info = get_remote_thread_info (item.ptid);
21fe1c75 3584 info->core = item.core;
7aabaf9d
SM
3585 info->extra = std::move (item.extra);
3586 info->name = std::move (item.name);
3587 info->thread_handle = std::move (item.thread_handle);
6dc54d91
PA
3588 }
3589 }
3590 }
3591
ab970af1
PA
3592 if (!got_list)
3593 {
3594 /* If no thread listing method is supported, then query whether
3595 each known thread is alive, one by one, with the T packet.
3596 If the target doesn't support threads at all, then this is a
3597 no-op. See remote_thread_alive. */
3598 prune_threads ();
3599 }
9d1f7ab2
MS
3600}
3601
802188a7 3602/*
9d1f7ab2
MS
3603 * Collect a descriptive string about the given thread.
3604 * The target may say anything it wants to about the thread
3605 * (typically info about its blocked / runnable state, name, etc.).
3606 * This string will appear in the info threads display.
802188a7 3607 *
9d1f7ab2
MS
3608 * Optional: targets are not required to implement this function.
3609 */
3610
f6ac5f3d
PA
3611const char *
3612remote_target::extra_thread_info (thread_info *tp)
9d1f7ab2 3613{
d01949b6 3614 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3615 int result;
3616 int set;
3617 threadref id;
3618 struct gdb_ext_thread_info threadinfo;
23860348 3619 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3620 int n = 0; /* position in display_buf */
3621
5d93a237 3622 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3623 internal_error (__FILE__, __LINE__,
e2e0b3e5 3624 _("remote_threads_extra_info"));
9d1f7ab2 3625
60e569b9 3626 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3627 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3628 /* This is the main thread which was added by GDB. The remote
3629 server doesn't know about it. */
3630 return NULL;
3631
4082afcc 3632 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3633 {
3634 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3635
7aabaf9d 3636 if (info != NULL && info->priv != NULL)
a9334058
SM
3637 {
3638 const std::string &extra = get_remote_thread_info (info)->extra;
3639 return !extra.empty () ? extra.c_str () : NULL;
3640 }
dc146f7c
VP
3641 else
3642 return NULL;
3643 }
3644
b80fafe3 3645 if (rs->use_threadextra_query)
9d1f7ab2 3646 {
82f73884
PA
3647 char *b = rs->buf;
3648 char *endb = rs->buf + get_remote_packet_size ();
3649
3650 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3651 b += strlen (b);
3652 write_ptid (b, endb, tp->ptid);
3653
2e9f7625 3654 putpkt (rs->buf);
6d820c5c 3655 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3656 if (rs->buf[0] != 0)
9d1f7ab2 3657 {
325fac50 3658 n = std::min (strlen (rs->buf) / 2, sizeof (display_buf));
2e9f7625 3659 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3660 display_buf [result] = '\0';
9d1f7ab2
MS
3661 return display_buf;
3662 }
0f71a2f6 3663 }
9d1f7ab2
MS
3664
3665 /* If the above query fails, fall back to the old method. */
b80fafe3 3666 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3667 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3668 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3669 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3670 if (remote_get_threadinfo (&id, set, &threadinfo))
3671 if (threadinfo.active)
0f71a2f6 3672 {
9d1f7ab2 3673 if (*threadinfo.shortname)
2bc416ba 3674 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3675 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3676 if (*threadinfo.display)
2bc416ba 3677 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3678 " State: %s,", threadinfo.display);
9d1f7ab2 3679 if (*threadinfo.more_display)
2bc416ba 3680 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3681 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3682
3683 if (n > 0)
c5aa993b 3684 {
23860348 3685 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3686 if (',' == display_buf[n-1])
3687 display_buf[n-1] = ' ';
3688 return display_buf;
c5aa993b 3689 }
0f71a2f6 3690 }
9d1f7ab2 3691 return NULL;
0f71a2f6 3692}
c906108c 3693\f
c5aa993b 3694
f6ac5f3d
PA
3695bool
3696remote_target::static_tracepoint_marker_at (CORE_ADDR addr,
3697 struct static_tracepoint_marker *marker)
0fb4aa4b
PA
3698{
3699 struct remote_state *rs = get_remote_state ();
3700 char *p = rs->buf;
3701
bba74b36 3702 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3703 p += strlen (p);
3704 p += hexnumstr (p, addr);
3705 putpkt (rs->buf);
3706 getpkt (&rs->buf, &rs->buf_size, 0);
3707 p = rs->buf;
3708
3709 if (*p == 'E')
3710 error (_("Remote failure reply: %s"), p);
3711
3712 if (*p++ == 'm')
3713 {
256642e8 3714 parse_static_tracepoint_marker_definition (p, NULL, marker);
5d9310c4 3715 return true;
0fb4aa4b
PA
3716 }
3717
5d9310c4 3718 return false;
0fb4aa4b
PA
3719}
3720
f6ac5f3d
PA
3721std::vector<static_tracepoint_marker>
3722remote_target::static_tracepoint_markers_by_strid (const char *strid)
0fb4aa4b
PA
3723{
3724 struct remote_state *rs = get_remote_state ();
5d9310c4 3725 std::vector<static_tracepoint_marker> markers;
256642e8 3726 const char *p;
5d9310c4 3727 static_tracepoint_marker marker;
0fb4aa4b
PA
3728
3729 /* Ask for a first packet of static tracepoint marker
3730 definition. */
3731 putpkt ("qTfSTM");
3732 getpkt (&rs->buf, &rs->buf_size, 0);
3733 p = rs->buf;
3734 if (*p == 'E')
3735 error (_("Remote failure reply: %s"), p);
3736
0fb4aa4b
PA
3737 while (*p++ == 'm')
3738 {
0fb4aa4b
PA
3739 do
3740 {
5d9310c4 3741 parse_static_tracepoint_marker_definition (p, &p, &marker);
0fb4aa4b 3742
5d9310c4
SM
3743 if (strid == NULL || marker.str_id == strid)
3744 markers.push_back (std::move (marker));
0fb4aa4b
PA
3745 }
3746 while (*p++ == ','); /* comma-separated list */
3747 /* Ask for another packet of static tracepoint definition. */
3748 putpkt ("qTsSTM");
3749 getpkt (&rs->buf, &rs->buf_size, 0);
3750 p = rs->buf;
3751 }
3752
0fb4aa4b
PA
3753 return markers;
3754}
3755
3756\f
10760264
JB
3757/* Implement the to_get_ada_task_ptid function for the remote targets. */
3758
f6ac5f3d
PA
3759ptid_t
3760remote_target::get_ada_task_ptid (long lwp, long thread)
10760264 3761{
ba348170 3762 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3763}
3764\f
3765
24b06219 3766/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3767
3768static void
fba45db2 3769extended_remote_restart (void)
c906108c 3770{
d01949b6 3771 struct remote_state *rs = get_remote_state ();
c906108c
SS
3772
3773 /* Send the restart command; for reasons I don't understand the
3774 remote side really expects a number after the "R". */
ea9c271d 3775 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3776 putpkt (rs->buf);
c906108c 3777
ad9a8f3f 3778 remote_fileio_reset ();
c906108c
SS
3779}
3780\f
3781/* Clean up connection to a remote debugger. */
3782
f6ac5f3d
PA
3783void
3784remote_target::close ()
c906108c 3785{
5d93a237
TT
3786 struct remote_state *rs = get_remote_state ();
3787
3788 if (rs->remote_desc == NULL)
d3fd5342
PA
3789 return; /* already closed */
3790
048094ac 3791 /* Make sure we leave stdin registered in the event loop. */
f6ac5f3d 3792 terminal_ours ();
ce5ce7ed 3793
5d93a237
TT
3794 serial_close (rs->remote_desc);
3795 rs->remote_desc = NULL;
ce5ce7ed
PA
3796
3797 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3798 of all the inferiors and their threads we were controlling.
3799 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3800 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3801 inferior_ptid = null_ptid;
f67fd822 3802 discard_all_inferiors ();
ce5ce7ed 3803
f48ff2a7
YQ
3804 /* We are closing the remote target, so we should discard
3805 everything of this target. */
bcc75809 3806 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3807
3808 if (remote_async_inferior_event_token)
3809 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3810
5965e028 3811 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3812
3813 trace_reset_local_state ();
c906108c
SS
3814}
3815
23860348 3816/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3817
3818static void
fba45db2 3819get_offsets (void)
c906108c 3820{
d01949b6 3821 struct remote_state *rs = get_remote_state ();
2e9f7625 3822 char *buf;
085dd6e6 3823 char *ptr;
31d99776
DJ
3824 int lose, num_segments = 0, do_sections, do_segments;
3825 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3826 struct section_offsets *offs;
31d99776
DJ
3827 struct symfile_segment_data *data;
3828
3829 if (symfile_objfile == NULL)
3830 return;
c906108c
SS
3831
3832 putpkt ("qOffsets");
6d820c5c 3833 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3834 buf = rs->buf;
c906108c
SS
3835
3836 if (buf[0] == '\000')
3837 return; /* Return silently. Stub doesn't support
23860348 3838 this command. */
c906108c
SS
3839 if (buf[0] == 'E')
3840 {
8a3fe4f8 3841 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3842 return;
3843 }
3844
3845 /* Pick up each field in turn. This used to be done with scanf, but
3846 scanf will make trouble if CORE_ADDR size doesn't match
3847 conversion directives correctly. The following code will work
3848 with any size of CORE_ADDR. */
3849 text_addr = data_addr = bss_addr = 0;
3850 ptr = buf;
3851 lose = 0;
3852
61012eef 3853 if (startswith (ptr, "Text="))
c906108c
SS
3854 {
3855 ptr += 5;
3856 /* Don't use strtol, could lose on big values. */
3857 while (*ptr && *ptr != ';')
3858 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3859
61012eef 3860 if (startswith (ptr, ";Data="))
31d99776
DJ
3861 {
3862 ptr += 6;
3863 while (*ptr && *ptr != ';')
3864 data_addr = (data_addr << 4) + fromhex (*ptr++);
3865 }
3866 else
3867 lose = 1;
3868
61012eef 3869 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3870 {
3871 ptr += 5;
3872 while (*ptr && *ptr != ';')
3873 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3874
31d99776
DJ
3875 if (bss_addr != data_addr)
3876 warning (_("Target reported unsupported offsets: %s"), buf);
3877 }
3878 else
3879 lose = 1;
3880 }
61012eef 3881 else if (startswith (ptr, "TextSeg="))
c906108c 3882 {
31d99776
DJ
3883 ptr += 8;
3884 /* Don't use strtol, could lose on big values. */
c906108c 3885 while (*ptr && *ptr != ';')
31d99776
DJ
3886 text_addr = (text_addr << 4) + fromhex (*ptr++);
3887 num_segments = 1;
3888
61012eef 3889 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3890 {
3891 ptr += 9;
3892 while (*ptr && *ptr != ';')
3893 data_addr = (data_addr << 4) + fromhex (*ptr++);
3894 num_segments++;
3895 }
c906108c
SS
3896 }
3897 else
3898 lose = 1;
3899
3900 if (lose)
8a3fe4f8 3901 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3902 else if (*ptr != '\0')
3903 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3904
802188a7 3905 offs = ((struct section_offsets *)
a39a16c4 3906 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3907 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3908 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3909
31d99776
DJ
3910 data = get_symfile_segment_data (symfile_objfile->obfd);
3911 do_segments = (data != NULL);
3912 do_sections = num_segments == 0;
c906108c 3913
28c32713 3914 if (num_segments > 0)
31d99776 3915 {
31d99776
DJ
3916 segments[0] = text_addr;
3917 segments[1] = data_addr;
3918 }
28c32713
JB
3919 /* If we have two segments, we can still try to relocate everything
3920 by assuming that the .text and .data offsets apply to the whole
3921 text and data segments. Convert the offsets given in the packet
3922 to base addresses for symfile_map_offsets_to_segments. */
3923 else if (data && data->num_segments == 2)
3924 {
3925 segments[0] = data->segment_bases[0] + text_addr;
3926 segments[1] = data->segment_bases[1] + data_addr;
3927 num_segments = 2;
3928 }
8d385431
DJ
3929 /* If the object file has only one segment, assume that it is text
3930 rather than data; main programs with no writable data are rare,
3931 but programs with no code are useless. Of course the code might
3932 have ended up in the data segment... to detect that we would need
3933 the permissions here. */
3934 else if (data && data->num_segments == 1)
3935 {
3936 segments[0] = data->segment_bases[0] + text_addr;
3937 num_segments = 1;
3938 }
28c32713
JB
3939 /* There's no way to relocate by segment. */
3940 else
3941 do_segments = 0;
31d99776
DJ
3942
3943 if (do_segments)
3944 {
3945 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3946 offs, num_segments, segments);
3947
3948 if (ret == 0 && !do_sections)
3e43a32a
MS
3949 error (_("Can not handle qOffsets TextSeg "
3950 "response with this symbol file"));
31d99776
DJ
3951
3952 if (ret > 0)
3953 do_sections = 0;
3954 }
c906108c 3955
9ef895d6
DJ
3956 if (data)
3957 free_symfile_segment_data (data);
31d99776
DJ
3958
3959 if (do_sections)
3960 {
3961 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3962
3e43a32a
MS
3963 /* This is a temporary kludge to force data and bss to use the
3964 same offsets because that's what nlmconv does now. The real
3965 solution requires changes to the stub and remote.c that I
3966 don't have time to do right now. */
31d99776
DJ
3967
3968 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3969 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3970 }
c906108c
SS
3971
3972 objfile_relocate (symfile_objfile, offs);
3973}
3974
9a7071a8
JB
3975/* Send interrupt_sequence to remote target. */
3976static void
eeae04df 3977send_interrupt_sequence (void)
9a7071a8 3978{
5d93a237
TT
3979 struct remote_state *rs = get_remote_state ();
3980
9a7071a8 3981 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3982 remote_serial_write ("\x03", 1);
9a7071a8 3983 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3984 serial_send_break (rs->remote_desc);
9a7071a8
JB
3985 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3986 {
5d93a237 3987 serial_send_break (rs->remote_desc);
c33e31fd 3988 remote_serial_write ("g", 1);
9a7071a8
JB
3989 }
3990 else
3991 internal_error (__FILE__, __LINE__,
3992 _("Invalid value for interrupt_sequence_mode: %s."),
3993 interrupt_sequence_mode);
3994}
3995
3405876a
PA
3996
3997/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3998 and extract the PTID. Returns NULL_PTID if not found. */
3999
4000static ptid_t
4001stop_reply_extract_thread (char *stop_reply)
4002{
4003 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
4004 {
256642e8 4005 const char *p;
3405876a
PA
4006
4007 /* Txx r:val ; r:val (...) */
4008 p = &stop_reply[3];
4009
4010 /* Look for "register" named "thread". */
4011 while (*p != '\0')
4012 {
256642e8 4013 const char *p1;
3405876a
PA
4014
4015 p1 = strchr (p, ':');
4016 if (p1 == NULL)
4017 return null_ptid;
4018
4019 if (strncmp (p, "thread", p1 - p) == 0)
4020 return read_ptid (++p1, &p);
4021
4022 p1 = strchr (p, ';');
4023 if (p1 == NULL)
4024 return null_ptid;
4025 p1++;
4026
4027 p = p1;
4028 }
4029 }
4030
4031 return null_ptid;
4032}
4033
b7ea362b
PA
4034/* Determine the remote side's current thread. If we have a stop
4035 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4036 "thread" register we can extract the current thread from. If not,
4037 ask the remote which is the current thread with qC. The former
4038 method avoids a roundtrip. */
4039
4040static ptid_t
4041get_current_thread (char *wait_status)
4042{
6a49a997 4043 ptid_t ptid = null_ptid;
b7ea362b
PA
4044
4045 /* Note we don't use remote_parse_stop_reply as that makes use of
4046 the target architecture, which we haven't yet fully determined at
4047 this point. */
4048 if (wait_status != NULL)
4049 ptid = stop_reply_extract_thread (wait_status);
4050 if (ptid_equal (ptid, null_ptid))
4051 ptid = remote_current_thread (inferior_ptid);
4052
4053 return ptid;
4054}
4055
49c62f2e
PA
4056/* Query the remote target for which is the current thread/process,
4057 add it to our tables, and update INFERIOR_PTID. The caller is
4058 responsible for setting the state such that the remote end is ready
3405876a
PA
4059 to return the current thread.
4060
4061 This function is called after handling the '?' or 'vRun' packets,
4062 whose response is a stop reply from which we can also try
4063 extracting the thread. If the target doesn't support the explicit
4064 qC query, we infer the current thread from that stop reply, passed
4065 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
4066
4067static void
3405876a 4068add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
4069{
4070 struct remote_state *rs = get_remote_state ();
4071 int fake_pid_p = 0;
49c62f2e
PA
4072
4073 inferior_ptid = null_ptid;
4074
b7ea362b 4075 /* Now, if we have thread information, update inferior_ptid. */
87215ad1 4076 ptid_t curr_ptid = get_current_thread (wait_status);
3405876a 4077
87215ad1 4078 if (curr_ptid != null_ptid)
49c62f2e
PA
4079 {
4080 if (!remote_multi_process_p (rs))
4081 fake_pid_p = 1;
49c62f2e
PA
4082 }
4083 else
4084 {
4085 /* Without this, some commands which require an active target
4086 (such as kill) won't work. This variable serves (at least)
4087 double duty as both the pid of the target process (if it has
4088 such), and as a flag indicating that a target is active. */
87215ad1 4089 curr_ptid = magic_null_ptid;
49c62f2e
PA
4090 fake_pid_p = 1;
4091 }
4092
87215ad1 4093 remote_add_inferior (fake_pid_p, ptid_get_pid (curr_ptid), -1, 1);
49c62f2e 4094
87215ad1
SDJ
4095 /* Add the main thread and switch to it. Don't try reading
4096 registers yet, since we haven't fetched the target description
4097 yet. */
4098 thread_info *tp = add_thread_silent (curr_ptid);
4099 switch_to_thread_no_regs (tp);
49c62f2e
PA
4100}
4101
6efcd9a8
PA
4102/* Print info about a thread that was found already stopped on
4103 connection. */
4104
4105static void
4106print_one_stopped_thread (struct thread_info *thread)
4107{
4108 struct target_waitstatus *ws = &thread->suspend.waitstatus;
4109
4110 switch_to_thread (thread->ptid);
4111 stop_pc = get_frame_pc (get_current_frame ());
4112 set_current_sal_from_frame (get_current_frame ());
4113
4114 thread->suspend.waitstatus_pending_p = 0;
4115
4116 if (ws->kind == TARGET_WAITKIND_STOPPED)
4117 {
4118 enum gdb_signal sig = ws->value.sig;
4119
4120 if (signal_print_state (sig))
76727919 4121 gdb::observers::signal_received.notify (sig);
6efcd9a8 4122 }
76727919 4123 gdb::observers::normal_stop.notify (NULL, 1);
6efcd9a8
PA
4124}
4125
221e1a37
PA
4126/* Process all initial stop replies the remote side sent in response
4127 to the ? packet. These indicate threads that were already stopped
4128 on initial connection. We mark these threads as stopped and print
4129 their current frame before giving the user the prompt. */
4130
4131static void
6efcd9a8 4132process_initial_stop_replies (int from_tty)
221e1a37
PA
4133{
4134 int pending_stop_replies = stop_reply_queue_length ();
6efcd9a8
PA
4135 struct inferior *inf;
4136 struct thread_info *thread;
4137 struct thread_info *selected = NULL;
4138 struct thread_info *lowest_stopped = NULL;
4139 struct thread_info *first = NULL;
221e1a37
PA
4140
4141 /* Consume the initial pending events. */
4142 while (pending_stop_replies-- > 0)
4143 {
4144 ptid_t waiton_ptid = minus_one_ptid;
4145 ptid_t event_ptid;
4146 struct target_waitstatus ws;
4147 int ignore_event = 0;
6efcd9a8 4148 struct thread_info *thread;
221e1a37
PA
4149
4150 memset (&ws, 0, sizeof (ws));
4151 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
4152 if (remote_debug)
4153 print_target_wait_results (waiton_ptid, event_ptid, &ws);
4154
4155 switch (ws.kind)
4156 {
4157 case TARGET_WAITKIND_IGNORE:
4158 case TARGET_WAITKIND_NO_RESUMED:
4159 case TARGET_WAITKIND_SIGNALLED:
4160 case TARGET_WAITKIND_EXITED:
4161 /* We shouldn't see these, but if we do, just ignore. */
4162 if (remote_debug)
4163 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
4164 ignore_event = 1;
4165 break;
4166
4167 case TARGET_WAITKIND_EXECD:
4168 xfree (ws.value.execd_pathname);
4169 break;
4170 default:
4171 break;
4172 }
4173
4174 if (ignore_event)
4175 continue;
4176
6efcd9a8 4177 thread = find_thread_ptid (event_ptid);
221e1a37
PA
4178
4179 if (ws.kind == TARGET_WAITKIND_STOPPED)
4180 {
4181 enum gdb_signal sig = ws.value.sig;
4182
4183 /* Stubs traditionally report SIGTRAP as initial signal,
4184 instead of signal 0. Suppress it. */
4185 if (sig == GDB_SIGNAL_TRAP)
4186 sig = GDB_SIGNAL_0;
6efcd9a8
PA
4187 thread->suspend.stop_signal = sig;
4188 ws.value.sig = sig;
4189 }
221e1a37 4190
6efcd9a8
PA
4191 thread->suspend.waitstatus = ws;
4192
4193 if (ws.kind != TARGET_WAITKIND_STOPPED
4194 || ws.value.sig != GDB_SIGNAL_0)
4195 thread->suspend.waitstatus_pending_p = 1;
4196
4197 set_executing (event_ptid, 0);
4198 set_running (event_ptid, 0);
7aabaf9d 4199 get_remote_thread_info (thread)->vcont_resumed = 0;
6efcd9a8
PA
4200 }
4201
4202 /* "Notice" the new inferiors before anything related to
4203 registers/memory. */
4204 ALL_INFERIORS (inf)
4205 {
4206 if (inf->pid == 0)
4207 continue;
4208
4209 inf->needs_setup = 1;
4210
4211 if (non_stop)
4212 {
4213 thread = any_live_thread_of_process (inf->pid);
4214 notice_new_inferior (thread->ptid,
4215 thread->state == THREAD_RUNNING,
4216 from_tty);
4217 }
4218 }
4219
4220 /* If all-stop on top of non-stop, pause all threads. Note this
4221 records the threads' stop pc, so must be done after "noticing"
4222 the inferiors. */
4223 if (!non_stop)
4224 {
4225 stop_all_threads ();
4226
4227 /* If all threads of an inferior were already stopped, we
4228 haven't setup the inferior yet. */
4229 ALL_INFERIORS (inf)
4230 {
4231 if (inf->pid == 0)
4232 continue;
221e1a37 4233
6efcd9a8
PA
4234 if (inf->needs_setup)
4235 {
4236 thread = any_live_thread_of_process (inf->pid);
4237 switch_to_thread_no_regs (thread);
4238 setup_inferior (0);
4239 }
4240 }
221e1a37 4241 }
6efcd9a8
PA
4242
4243 /* Now go over all threads that are stopped, and print their current
4244 frame. If all-stop, then if there's a signalled thread, pick
4245 that as current. */
4246 ALL_NON_EXITED_THREADS (thread)
4247 {
6efcd9a8
PA
4248 if (first == NULL)
4249 first = thread;
4250
4251 if (!non_stop)
4252 set_running (thread->ptid, 0);
4253 else if (thread->state != THREAD_STOPPED)
4254 continue;
4255
6efcd9a8
PA
4256 if (selected == NULL
4257 && thread->suspend.waitstatus_pending_p)
4258 selected = thread;
4259
5d5658a1
PA
4260 if (lowest_stopped == NULL
4261 || thread->inf->num < lowest_stopped->inf->num
4262 || thread->per_inf_num < lowest_stopped->per_inf_num)
6efcd9a8
PA
4263 lowest_stopped = thread;
4264
4265 if (non_stop)
4266 print_one_stopped_thread (thread);
4267 }
4268
4269 /* In all-stop, we only print the status of one thread, and leave
4270 others with their status pending. */
4271 if (!non_stop)
4272 {
4273 thread = selected;
4274 if (thread == NULL)
4275 thread = lowest_stopped;
4276 if (thread == NULL)
4277 thread = first;
4278
4279 print_one_stopped_thread (thread);
4280 }
4281
4282 /* For "info program". */
4283 thread = inferior_thread ();
4284 if (thread->state == THREAD_STOPPED)
4285 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
221e1a37
PA
4286}
4287
048094ac
PA
4288/* Start the remote connection and sync state. */
4289
f6ac5f3d
PA
4290void
4291remote_target::start_remote (int from_tty, int extended_p)
c906108c 4292{
c8d104ad
PA
4293 struct remote_state *rs = get_remote_state ();
4294 struct packet_config *noack_config;
2d717e4f 4295 char *wait_status = NULL;
8621d6a9 4296
048094ac
PA
4297 /* Signal other parts that we're going through the initial setup,
4298 and so things may not be stable yet. E.g., we don't try to
4299 install tracepoints until we've relocated symbols. Also, a
4300 Ctrl-C before we're connected and synced up can't interrupt the
4301 target. Instead, it offers to drop the (potentially wedged)
4302 connection. */
4303 rs->starting_up = 1;
4304
522002f9 4305 QUIT;
c906108c 4306
9a7071a8
JB
4307 if (interrupt_on_connect)
4308 send_interrupt_sequence ();
4309
57e12211 4310 /* Ack any packet which the remote side has already sent. */
048094ac 4311 remote_serial_write ("+", 1);
1e51243a 4312
c8d104ad
PA
4313 /* The first packet we send to the target is the optional "supported
4314 packets" request. If the target can answer this, it will tell us
4315 which later probes to skip. */
4316 remote_query_supported ();
4317
d914c394 4318 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 4319 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
f6ac5f3d 4320 set_permissions ();
d914c394 4321
57809e5e
JK
4322 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4323 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4324 as a reply to known packet. For packet "vFile:setfs:" it is an
4325 invalid reply and GDB would return error in
4326 remote_hostio_set_filesystem, making remote files access impossible.
4327 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4328 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4329 {
4330 const char v_mustreplyempty[] = "vMustReplyEmpty";
4331
4332 putpkt (v_mustreplyempty);
4333 getpkt (&rs->buf, &rs->buf_size, 0);
4334 if (strcmp (rs->buf, "OK") == 0)
4335 remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
4336 else if (strcmp (rs->buf, "") != 0)
4337 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
4338 rs->buf);
4339 }
4340
c8d104ad
PA
4341 /* Next, we possibly activate noack mode.
4342
4343 If the QStartNoAckMode packet configuration is set to AUTO,
4344 enable noack mode if the stub reported a wish for it with
4345 qSupported.
4346
4347 If set to TRUE, then enable noack mode even if the stub didn't
4348 report it in qSupported. If the stub doesn't reply OK, the
4349 session ends with an error.
4350
4351 If FALSE, then don't activate noack mode, regardless of what the
4352 stub claimed should be the default with qSupported. */
4353
4354 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 4355 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
4356 {
4357 putpkt ("QStartNoAckMode");
4358 getpkt (&rs->buf, &rs->buf_size, 0);
4359 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
4360 rs->noack_mode = 1;
4361 }
4362
04bd08de 4363 if (extended_p)
5fe04517
PA
4364 {
4365 /* Tell the remote that we are using the extended protocol. */
4366 putpkt ("!");
4367 getpkt (&rs->buf, &rs->buf_size, 0);
4368 }
4369
9b224c5e
PA
4370 /* Let the target know which signals it is allowed to pass down to
4371 the program. */
4372 update_signals_program_target ();
4373
d962ef82
DJ
4374 /* Next, if the target can specify a description, read it. We do
4375 this before anything involving memory or registers. */
4376 target_find_description ();
4377
6c95b8df
PA
4378 /* Next, now that we know something about the target, update the
4379 address spaces in the program spaces. */
4380 update_address_spaces ();
4381
50c71eaf
PA
4382 /* On OSs where the list of libraries is global to all
4383 processes, we fetch them early. */
f5656ead 4384 if (gdbarch_has_global_solist (target_gdbarch ()))
e696b3ad 4385 solib_add (NULL, from_tty, auto_solib_add);
50c71eaf 4386
6efcd9a8 4387 if (target_is_non_stop_p ())
74531fed 4388 {
4082afcc 4389 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
4390 error (_("Non-stop mode requested, but remote "
4391 "does not support non-stop"));
74531fed
PA
4392
4393 putpkt ("QNonStop:1");
4394 getpkt (&rs->buf, &rs->buf_size, 0);
4395
4396 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4397 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
4398
4399 /* Find about threads and processes the stub is already
4400 controlling. We default to adding them in the running state.
4401 The '?' query below will then tell us about which threads are
4402 stopped. */
f6ac5f3d 4403 this->update_thread_list ();
74531fed 4404 }
4082afcc 4405 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
4406 {
4407 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 4408 Request it explicitly. */
74531fed
PA
4409 putpkt ("QNonStop:0");
4410 getpkt (&rs->buf, &rs->buf_size, 0);
4411
4412 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4413 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
4414 }
4415
a0743c90
YQ
4416 /* Upload TSVs regardless of whether the target is running or not. The
4417 remote stub, such as GDBserver, may have some predefined or builtin
4418 TSVs, even if the target is not running. */
f6ac5f3d 4419 if (get_trace_status (current_trace_status ()) != -1)
a0743c90
YQ
4420 {
4421 struct uploaded_tsv *uploaded_tsvs = NULL;
4422
f6ac5f3d 4423 upload_trace_state_variables (&uploaded_tsvs);
a0743c90
YQ
4424 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4425 }
4426
2d717e4f
DJ
4427 /* Check whether the target is running now. */
4428 putpkt ("?");
4429 getpkt (&rs->buf, &rs->buf_size, 0);
4430
6efcd9a8 4431 if (!target_is_non_stop_p ())
2d717e4f 4432 {
74531fed 4433 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 4434 {
04bd08de 4435 if (!extended_p)
74531fed 4436 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
4437
4438 /* We're connected, but not running. Drop out before we
4439 call start_remote. */
e278ad5b 4440 rs->starting_up = 0;
c35b1492 4441 return;
2d717e4f
DJ
4442 }
4443 else
74531fed 4444 {
74531fed 4445 /* Save the reply for later. */
224c3ddb 4446 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
4447 strcpy (wait_status, rs->buf);
4448 }
4449
b7ea362b 4450 /* Fetch thread list. */
e8032dde 4451 target_update_thread_list ();
b7ea362b 4452
74531fed
PA
4453 /* Let the stub know that we want it to return the thread. */
4454 set_continue_thread (minus_one_ptid);
4455
b7ea362b
PA
4456 if (thread_count () == 0)
4457 {
4458 /* Target has no concept of threads at all. GDB treats
4459 non-threaded target as single-threaded; add a main
4460 thread. */
4461 add_current_inferior_and_thread (wait_status);
4462 }
4463 else
4464 {
4465 /* We have thread information; select the thread the target
4466 says should be current. If we're reconnecting to a
4467 multi-threaded program, this will ideally be the thread
4468 that last reported an event before GDB disconnected. */
4469 inferior_ptid = get_current_thread (wait_status);
4470 if (ptid_equal (inferior_ptid, null_ptid))
4471 {
4472 /* Odd... The target was able to list threads, but not
4473 tell us which thread was current (no "thread"
4474 register in T stop reply?). Just pick the first
4475 thread in the thread list then. */
c9f35b34
KB
4476
4477 if (remote_debug)
4478 fprintf_unfiltered (gdb_stdlog,
4479 "warning: couldn't determine remote "
4480 "current thread; picking first in list.\n");
4481
b7ea362b
PA
4482 inferior_ptid = thread_list->ptid;
4483 }
4484 }
74531fed 4485
6e586cc5
YQ
4486 /* init_wait_for_inferior should be called before get_offsets in order
4487 to manage `inserted' flag in bp loc in a correct state.
4488 breakpoint_init_inferior, called from init_wait_for_inferior, set
4489 `inserted' flag to 0, while before breakpoint_re_set, called from
4490 start_remote, set `inserted' flag to 1. In the initialization of
4491 inferior, breakpoint_init_inferior should be called first, and then
4492 breakpoint_re_set can be called. If this order is broken, state of
4493 `inserted' flag is wrong, and cause some problems on breakpoint
4494 manipulation. */
4495 init_wait_for_inferior ();
4496
74531fed
PA
4497 get_offsets (); /* Get text, data & bss offsets. */
4498
d962ef82
DJ
4499 /* If we could not find a description using qXfer, and we know
4500 how to do it some other way, try again. This is not
4501 supported for non-stop; it could be, but it is tricky if
4502 there are no stopped threads when we connect. */
f6ac5f3d 4503 if (remote_read_description_p (this)
f5656ead 4504 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
4505 {
4506 target_clear_description ();
4507 target_find_description ();
4508 }
4509
74531fed
PA
4510 /* Use the previously fetched status. */
4511 gdb_assert (wait_status != NULL);
4512 strcpy (rs->buf, wait_status);
4513 rs->cached_wait_status = 1;
4514
f6ac5f3d 4515 ::start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
4516 }
4517 else
4518 {
68c97600
PA
4519 /* Clear WFI global state. Do this before finding about new
4520 threads and inferiors, and setting the current inferior.
4521 Otherwise we would clear the proceed status of the current
4522 inferior when we want its stop_soon state to be preserved
4523 (see notice_new_inferior). */
4524 init_wait_for_inferior ();
4525
74531fed
PA
4526 /* In non-stop, we will either get an "OK", meaning that there
4527 are no stopped threads at this time; or, a regular stop
4528 reply. In the latter case, there may be more than one thread
4529 stopped --- we pull them all out using the vStopped
4530 mechanism. */
4531 if (strcmp (rs->buf, "OK") != 0)
4532 {
722247f1 4533 struct notif_client *notif = &notif_client_stop;
2d717e4f 4534
722247f1
YQ
4535 /* remote_notif_get_pending_replies acks this one, and gets
4536 the rest out. */
f48ff2a7 4537 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
4538 = remote_notif_parse (notif, rs->buf);
4539 remote_notif_get_pending_events (notif);
74531fed 4540 }
2d717e4f 4541
74531fed
PA
4542 if (thread_count () == 0)
4543 {
04bd08de 4544 if (!extended_p)
74531fed 4545 error (_("The target is not running (try extended-remote?)"));
82f73884 4546
c35b1492
PA
4547 /* We're connected, but not running. Drop out before we
4548 call start_remote. */
e278ad5b 4549 rs->starting_up = 0;
c35b1492
PA
4550 return;
4551 }
74531fed 4552
74531fed
PA
4553 /* In non-stop mode, any cached wait status will be stored in
4554 the stop reply queue. */
4555 gdb_assert (wait_status == NULL);
f0223081 4556
2455069d 4557 /* Report all signals during attach/startup. */
f6ac5f3d 4558 pass_signals (0, NULL);
221e1a37
PA
4559
4560 /* If there are already stopped threads, mark them stopped and
4561 report their stops before giving the prompt to the user. */
6efcd9a8 4562 process_initial_stop_replies (from_tty);
221e1a37
PA
4563
4564 if (target_can_async_p ())
4565 target_async (1);
74531fed 4566 }
c8d104ad 4567
c8d104ad
PA
4568 /* If we connected to a live target, do some additional setup. */
4569 if (target_has_execution)
4570 {
f4ccffad 4571 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 4572 remote_check_symbols ();
c8d104ad 4573 }
50c71eaf 4574
d5551862
SS
4575 /* Possibly the target has been engaged in a trace run started
4576 previously; find out where things are at. */
f6ac5f3d 4577 if (get_trace_status (current_trace_status ()) != -1)
d5551862 4578 {
00bf0b85 4579 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4580
00bf0b85
SS
4581 if (current_trace_status ()->running)
4582 printf_filtered (_("Trace is already running on the target.\n"));
4583
f6ac5f3d 4584 upload_tracepoints (&uploaded_tps);
00bf0b85
SS
4585
4586 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4587 }
4588
c0272db5
TW
4589 /* Possibly the target has been engaged in a btrace record started
4590 previously; find out where things are at. */
4591 remote_btrace_maybe_reopen ();
4592
1e51243a
PA
4593 /* The thread and inferior lists are now synchronized with the
4594 target, our symbols have been relocated, and we're merged the
4595 target's tracepoints with ours. We're done with basic start
4596 up. */
4597 rs->starting_up = 0;
4598
a25a5a45
PA
4599 /* Maybe breakpoints are global and need to be inserted now. */
4600 if (breakpoints_should_be_inserted_now ())
50c71eaf 4601 insert_breakpoints ();
c906108c
SS
4602}
4603
4604/* Open a connection to a remote debugger.
4605 NAME is the filename used for communication. */
4606
f6ac5f3d
PA
4607void
4608remote_target::open (const char *name, int from_tty)
c906108c 4609{
f6ac5f3d 4610 open_1 (name, from_tty, 0);
43ff13b4
JM
4611}
4612
c906108c
SS
4613/* Open a connection to a remote debugger using the extended
4614 remote gdb protocol. NAME is the filename used for communication. */
4615
f6ac5f3d
PA
4616void
4617extended_remote_target::open (const char *name, int from_tty)
c906108c 4618{
f6ac5f3d 4619 open_1 (name, from_tty, 1 /*extended_p */);
43ff13b4
JM
4620}
4621
ca4f7f8b
PA
4622/* Reset all packets back to "unknown support". Called when opening a
4623 new connection to a remote target. */
c906108c 4624
d471ea57 4625static void
ca4f7f8b 4626reset_all_packet_configs_support (void)
d471ea57
AC
4627{
4628 int i;
a744cf53 4629
444abaca 4630 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4631 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4632}
4633
ca4f7f8b
PA
4634/* Initialize all packet configs. */
4635
4636static void
4637init_all_packet_configs (void)
4638{
4639 int i;
4640
4641 for (i = 0; i < PACKET_MAX; i++)
4642 {
4643 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4644 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4645 }
4646}
4647
23860348 4648/* Symbol look-up. */
dc8acb97
MS
4649
4650static void
36d25514 4651remote_check_symbols (void)
dc8acb97
MS
4652{
4653 char *msg, *reply, *tmp;
dc8acb97 4654 int end;
28170b88 4655 long reply_size;
a5c0808e 4656 struct cleanup *old_chain;
dc8acb97 4657
63154eca
PA
4658 /* The remote side has no concept of inferiors that aren't running
4659 yet, it only knows about running processes. If we're connected
4660 but our current inferior is not running, we should not invite the
4661 remote target to request symbol lookups related to its
4662 (unrelated) current process. */
4663 if (!target_has_execution)
4664 return;
4665
4082afcc 4666 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4667 return;
4668
63154eca
PA
4669 /* Make sure the remote is pointing at the right process. Note
4670 there's no way to select "no process". */
3c9c4b83
PA
4671 set_general_process ();
4672
6d820c5c
DJ
4673 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4674 because we need both at the same time. */
224c3ddb 4675 msg = (char *) xmalloc (get_remote_packet_size ());
a5c0808e 4676 old_chain = make_cleanup (xfree, msg);
28170b88
MK
4677 reply = (char *) xmalloc (get_remote_packet_size ());
4678 make_cleanup (free_current_contents, &reply);
4679 reply_size = get_remote_packet_size ();
6d820c5c 4680
23860348 4681 /* Invite target to request symbol lookups. */
dc8acb97
MS
4682
4683 putpkt ("qSymbol::");
28170b88
MK
4684 getpkt (&reply, &reply_size, 0);
4685 packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97 4686
61012eef 4687 while (startswith (reply, "qSymbol:"))
dc8acb97 4688 {
77e371c0
TT
4689 struct bound_minimal_symbol sym;
4690
dc8acb97 4691 tmp = &reply[8];
cfd77fa1 4692 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4693 msg[end] = '\0';
4694 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4695 if (sym.minsym == NULL)
ea9c271d 4696 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4697 else
2bbe3cc1 4698 {
f5656ead 4699 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4700 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4701
4702 /* If this is a function address, return the start of code
4703 instead of any data function descriptor. */
f5656ead 4704 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1 4705 sym_addr,
f6ac5f3d 4706 target_stack);
2bbe3cc1
DJ
4707
4708 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4709 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4710 }
4711
dc8acb97 4712 putpkt (msg);
28170b88 4713 getpkt (&reply, &reply_size, 0);
dc8acb97 4714 }
a5c0808e
PA
4715
4716 do_cleanups (old_chain);
dc8acb97
MS
4717}
4718
9db8d71f 4719static struct serial *
baa336ce 4720remote_serial_open (const char *name)
9db8d71f
DJ
4721{
4722 static int udp_warning = 0;
4723
4724 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4725 of in ser-tcp.c, because it is the remote protocol assuming that the
4726 serial connection is reliable and not the serial connection promising
4727 to be. */
61012eef 4728 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4729 {
3e43a32a
MS
4730 warning (_("The remote protocol may be unreliable over UDP.\n"
4731 "Some events may be lost, rendering further debugging "
4732 "impossible."));
9db8d71f
DJ
4733 udp_warning = 1;
4734 }
4735
4736 return serial_open (name);
4737}
4738
d914c394
SS
4739/* Inform the target of our permission settings. The permission flags
4740 work without this, but if the target knows the settings, it can do
4741 a couple things. First, it can add its own check, to catch cases
4742 that somehow manage to get by the permissions checks in target
4743 methods. Second, if the target is wired to disallow particular
4744 settings (for instance, a system in the field that is not set up to
4745 be able to stop at a breakpoint), it can object to any unavailable
4746 permissions. */
4747
4748void
f6ac5f3d 4749remote_target::set_permissions ()
d914c394
SS
4750{
4751 struct remote_state *rs = get_remote_state ();
4752
bba74b36
YQ
4753 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4754 "WriteReg:%x;WriteMem:%x;"
4755 "InsertBreak:%x;InsertTrace:%x;"
4756 "InsertFastTrace:%x;Stop:%x",
4757 may_write_registers, may_write_memory,
4758 may_insert_breakpoints, may_insert_tracepoints,
4759 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4760 putpkt (rs->buf);
4761 getpkt (&rs->buf, &rs->buf_size, 0);
4762
4763 /* If the target didn't like the packet, warn the user. Do not try
4764 to undo the user's settings, that would just be maddening. */
4765 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4766 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4767}
4768
be2a5f71
DJ
4769/* This type describes each known response to the qSupported
4770 packet. */
4771struct protocol_feature
4772{
4773 /* The name of this protocol feature. */
4774 const char *name;
4775
4776 /* The default for this protocol feature. */
4777 enum packet_support default_support;
4778
4779 /* The function to call when this feature is reported, or after
4780 qSupported processing if the feature is not supported.
4781 The first argument points to this structure. The second
4782 argument indicates whether the packet requested support be
4783 enabled, disabled, or probed (or the default, if this function
4784 is being called at the end of processing and this feature was
4785 not reported). The third argument may be NULL; if not NULL, it
4786 is a NUL-terminated string taken from the packet following
4787 this feature's name and an equals sign. */
4788 void (*func) (const struct protocol_feature *, enum packet_support,
4789 const char *);
4790
4791 /* The corresponding packet for this feature. Only used if
4792 FUNC is remote_supported_packet. */
4793 int packet;
4794};
4795
be2a5f71
DJ
4796static void
4797remote_supported_packet (const struct protocol_feature *feature,
4798 enum packet_support support,
4799 const char *argument)
4800{
4801 if (argument)
4802 {
4803 warning (_("Remote qSupported response supplied an unexpected value for"
4804 " \"%s\"."), feature->name);
4805 return;
4806 }
4807
4082afcc 4808 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4809}
be2a5f71
DJ
4810
4811static void
4812remote_packet_size (const struct protocol_feature *feature,
4813 enum packet_support support, const char *value)
4814{
4815 struct remote_state *rs = get_remote_state ();
4816
4817 int packet_size;
4818 char *value_end;
4819
4820 if (support != PACKET_ENABLE)
4821 return;
4822
4823 if (value == NULL || *value == '\0')
4824 {
4825 warning (_("Remote target reported \"%s\" without a size."),
4826 feature->name);
4827 return;
4828 }
4829
4830 errno = 0;
4831 packet_size = strtol (value, &value_end, 16);
4832 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4833 {
4834 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4835 feature->name, value);
4836 return;
4837 }
4838
be2a5f71
DJ
4839 /* Record the new maximum packet size. */
4840 rs->explicit_packet_size = packet_size;
4841}
4842
dc473cfb 4843static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4844 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4845 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4846 PACKET_qXfer_auxv },
c78fa86a
GB
4847 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4848 PACKET_qXfer_exec_file },
23181151
DJ
4849 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4850 PACKET_qXfer_features },
cfa9d6d9
DJ
4851 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4852 PACKET_qXfer_libraries },
2268b414
JK
4853 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4854 PACKET_qXfer_libraries_svr4 },
ced63ec0 4855 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4856 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4857 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4858 PACKET_qXfer_memory_map },
4de6483e
UW
4859 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4860 PACKET_qXfer_spu_read },
4861 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4862 PACKET_qXfer_spu_write },
07e059b5
VP
4863 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4864 PACKET_qXfer_osdata },
dc146f7c
VP
4865 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4866 PACKET_qXfer_threads },
b3b9301e
PA
4867 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4868 PACKET_qXfer_traceframe_info },
89be2091
DJ
4869 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4870 PACKET_QPassSignals },
82075af2
JS
4871 { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
4872 PACKET_QCatchSyscalls },
9b224c5e
PA
4873 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4874 PACKET_QProgramSignals },
bc3b087d
SDJ
4875 { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
4876 PACKET_QSetWorkingDir },
aefd8b33
SDJ
4877 { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
4878 PACKET_QStartupWithShell },
0a2dde4a
SDJ
4879 { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
4880 PACKET_QEnvironmentHexEncoded },
4881 { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
4882 PACKET_QEnvironmentReset },
4883 { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
4884 PACKET_QEnvironmentUnset },
a6f3e723
SL
4885 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4886 PACKET_QStartNoAckMode },
4082afcc
PA
4887 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4888 PACKET_multiprocess_feature },
4889 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4890 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4891 PACKET_qXfer_siginfo_read },
4892 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4893 PACKET_qXfer_siginfo_write },
4082afcc 4894 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4895 PACKET_ConditionalTracepoints },
4082afcc 4896 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4897 PACKET_ConditionalBreakpoints },
4082afcc 4898 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4899 PACKET_BreakpointCommands },
4082afcc 4900 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4901 PACKET_FastTracepoints },
4082afcc 4902 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4903 PACKET_StaticTracepoints },
4082afcc 4904 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4905 PACKET_InstallInTrace},
4082afcc
PA
4906 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4907 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4908 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4909 PACKET_bc },
4910 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4911 PACKET_bs },
409873ef
SS
4912 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4913 PACKET_TracepointSource },
d914c394
SS
4914 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4915 PACKET_QAllow },
4082afcc
PA
4916 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4917 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4918 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4919 PACKET_qXfer_fdpic },
169081d0
TG
4920 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4921 PACKET_qXfer_uib },
03583c20
UW
4922 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4923 PACKET_QDisableRandomization },
d1feda86 4924 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4925 { "QTBuffer:size", PACKET_DISABLE,
4926 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4927 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4928 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4929 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4930 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4931 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4932 PACKET_qXfer_btrace },
4933 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4934 PACKET_qXfer_btrace_conf },
4935 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4936 PACKET_Qbtrace_conf_bts_size },
4937 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4938 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4939 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4940 PACKET_fork_event_feature },
4941 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4942 PACKET_vfork_event_feature },
94585166
DB
4943 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4944 PACKET_exec_event_feature },
b20a6524 4945 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
750ce8d1 4946 PACKET_Qbtrace_conf_pt_size },
65706a29
PA
4947 { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
4948 { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
f2faf941 4949 { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
be2a5f71
DJ
4950};
4951
c8d5aac9
L
4952static char *remote_support_xml;
4953
4954/* Register string appended to "xmlRegisters=" in qSupported query. */
4955
4956void
6e39997a 4957register_remote_support_xml (const char *xml)
c8d5aac9
L
4958{
4959#if defined(HAVE_LIBEXPAT)
4960 if (remote_support_xml == NULL)
c4f7c687 4961 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4962 else
4963 {
4964 char *copy = xstrdup (remote_support_xml + 13);
4965 char *p = strtok (copy, ",");
4966
4967 do
4968 {
4969 if (strcmp (p, xml) == 0)
4970 {
4971 /* already there */
4972 xfree (copy);
4973 return;
4974 }
4975 }
4976 while ((p = strtok (NULL, ",")) != NULL);
4977 xfree (copy);
4978
94b0dee1
PA
4979 remote_support_xml = reconcat (remote_support_xml,
4980 remote_support_xml, ",", xml,
4981 (char *) NULL);
c8d5aac9
L
4982 }
4983#endif
4984}
4985
69b6ecb0
TT
4986static void
4987remote_query_supported_append (std::string *msg, const char *append)
c8d5aac9 4988{
69b6ecb0
TT
4989 if (!msg->empty ())
4990 msg->append (";");
4991 msg->append (append);
c8d5aac9
L
4992}
4993
be2a5f71
DJ
4994static void
4995remote_query_supported (void)
4996{
4997 struct remote_state *rs = get_remote_state ();
4998 char *next;
4999 int i;
5000 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
5001
5002 /* The packet support flags are handled differently for this packet
5003 than for most others. We treat an error, a disabled packet, and
5004 an empty response identically: any features which must be reported
5005 to be used will be automatically disabled. An empty buffer
5006 accomplishes this, since that is also the representation for a list
5007 containing no features. */
5008
5009 rs->buf[0] = 0;
4082afcc 5010 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 5011 {
69b6ecb0 5012 std::string q;
c8d5aac9 5013
73b8c1fd 5014 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5015 remote_query_supported_append (&q, "multiprocess+");
c8d5aac9 5016
f7e6eed5 5017 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5018 remote_query_supported_append (&q, "swbreak+");
f7e6eed5 5019 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5020 remote_query_supported_append (&q, "hwbreak+");
f7e6eed5 5021
69b6ecb0 5022 remote_query_supported_append (&q, "qRelocInsn+");
dde08ee1 5023
8020350c
DB
5024 if (packet_set_cmd_state (PACKET_fork_event_feature)
5025 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5026 remote_query_supported_append (&q, "fork-events+");
8020350c
DB
5027 if (packet_set_cmd_state (PACKET_vfork_event_feature)
5028 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5029 remote_query_supported_append (&q, "vfork-events+");
8020350c
DB
5030 if (packet_set_cmd_state (PACKET_exec_event_feature)
5031 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5032 remote_query_supported_append (&q, "exec-events+");
89245bc0 5033
750ce8d1 5034 if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5035 remote_query_supported_append (&q, "vContSupported+");
750ce8d1 5036
65706a29 5037 if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5038 remote_query_supported_append (&q, "QThreadEvents+");
65706a29 5039
f2faf941 5040 if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5041 remote_query_supported_append (&q, "no-resumed+");
f2faf941 5042
b35d5edb
PA
5043 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5044 the qSupported:xmlRegisters=i386 handling. */
7cc244de
PA
5045 if (remote_support_xml != NULL
5046 && packet_support (PACKET_qXfer_features) != PACKET_DISABLE)
69b6ecb0 5047 remote_query_supported_append (&q, remote_support_xml);
82f73884 5048
69b6ecb0
TT
5049 q = "qSupported:" + q;
5050 putpkt (q.c_str ());
94b0dee1 5051
be2a5f71
DJ
5052 getpkt (&rs->buf, &rs->buf_size, 0);
5053
5054 /* If an error occured, warn, but do not return - just reset the
5055 buffer to empty and go on to disable features. */
5056 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
5057 == PACKET_ERROR)
5058 {
5059 warning (_("Remote failure reply: %s"), rs->buf);
5060 rs->buf[0] = 0;
5061 }
5062 }
5063
5064 memset (seen, 0, sizeof (seen));
5065
5066 next = rs->buf;
5067 while (*next)
5068 {
5069 enum packet_support is_supported;
5070 char *p, *end, *name_end, *value;
5071
5072 /* First separate out this item from the rest of the packet. If
5073 there's another item after this, we overwrite the separator
5074 (terminated strings are much easier to work with). */
5075 p = next;
5076 end = strchr (p, ';');
5077 if (end == NULL)
5078 {
5079 end = p + strlen (p);
5080 next = end;
5081 }
5082 else
5083 {
89be2091
DJ
5084 *end = '\0';
5085 next = end + 1;
5086
be2a5f71
DJ
5087 if (end == p)
5088 {
5089 warning (_("empty item in \"qSupported\" response"));
5090 continue;
5091 }
be2a5f71
DJ
5092 }
5093
5094 name_end = strchr (p, '=');
5095 if (name_end)
5096 {
5097 /* This is a name=value entry. */
5098 is_supported = PACKET_ENABLE;
5099 value = name_end + 1;
5100 *name_end = '\0';
5101 }
5102 else
5103 {
5104 value = NULL;
5105 switch (end[-1])
5106 {
5107 case '+':
5108 is_supported = PACKET_ENABLE;
5109 break;
5110
5111 case '-':
5112 is_supported = PACKET_DISABLE;
5113 break;
5114
5115 case '?':
5116 is_supported = PACKET_SUPPORT_UNKNOWN;
5117 break;
5118
5119 default:
3e43a32a
MS
5120 warning (_("unrecognized item \"%s\" "
5121 "in \"qSupported\" response"), p);
be2a5f71
DJ
5122 continue;
5123 }
5124 end[-1] = '\0';
5125 }
5126
5127 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
5128 if (strcmp (remote_protocol_features[i].name, p) == 0)
5129 {
5130 const struct protocol_feature *feature;
5131
5132 seen[i] = 1;
5133 feature = &remote_protocol_features[i];
5134 feature->func (feature, is_supported, value);
5135 break;
5136 }
5137 }
5138
5139 /* If we increased the packet size, make sure to increase the global
5140 buffer size also. We delay this until after parsing the entire
5141 qSupported packet, because this is the same buffer we were
5142 parsing. */
5143 if (rs->buf_size < rs->explicit_packet_size)
5144 {
5145 rs->buf_size = rs->explicit_packet_size;
224c3ddb 5146 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
be2a5f71
DJ
5147 }
5148
5149 /* Handle the defaults for unmentioned features. */
5150 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
5151 if (!seen[i])
5152 {
5153 const struct protocol_feature *feature;
5154
5155 feature = &remote_protocol_features[i];
5156 feature->func (feature, feature->default_support, NULL);
5157 }
5158}
5159
048094ac
PA
5160/* Serial QUIT handler for the remote serial descriptor.
5161
5162 Defers handling a Ctrl-C until we're done with the current
5163 command/response packet sequence, unless:
5164
5165 - We're setting up the connection. Don't send a remote interrupt
5166 request, as we're not fully synced yet. Quit immediately
5167 instead.
5168
5169 - The target has been resumed in the foreground
223ffa71 5170 (target_terminal::is_ours is false) with a synchronous resume
048094ac
PA
5171 packet, and we're blocked waiting for the stop reply, thus a
5172 Ctrl-C should be immediately sent to the target.
5173
5174 - We get a second Ctrl-C while still within the same serial read or
5175 write. In that case the serial is seemingly wedged --- offer to
5176 quit/disconnect.
5177
5178 - We see a second Ctrl-C without target response, after having
5179 previously interrupted the target. In that case the target/stub
5180 is probably wedged --- offer to quit/disconnect.
5181*/
5182
5183static void
5184remote_serial_quit_handler (void)
5185{
5186 struct remote_state *rs = get_remote_state ();
5187
5188 if (check_quit_flag ())
5189 {
5190 /* If we're starting up, we're not fully synced yet. Quit
5191 immediately. */
5192 if (rs->starting_up)
5193 quit ();
5194 else if (rs->got_ctrlc_during_io)
5195 {
5196 if (query (_("The target is not responding to GDB commands.\n"
5197 "Stop debugging it? ")))
5198 remote_unpush_and_throw ();
5199 }
5200 /* If ^C has already been sent once, offer to disconnect. */
223ffa71 5201 else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
048094ac
PA
5202 interrupt_query ();
5203 /* All-stop protocol, and blocked waiting for stop reply. Send
5204 an interrupt request. */
223ffa71 5205 else if (!target_terminal::is_ours () && rs->waiting_for_stop_reply)
e671cd59 5206 target_interrupt ();
048094ac
PA
5207 else
5208 rs->got_ctrlc_during_io = 1;
5209 }
5210}
5211
78a095c3
JK
5212/* Remove any of the remote.c targets from target stack. Upper targets depend
5213 on it so remove them first. */
5214
5215static void
5216remote_unpush_target (void)
5217{
915ef8b1 5218 pop_all_targets_at_and_above (process_stratum);
78a095c3 5219}
be2a5f71 5220
048094ac
PA
5221static void
5222remote_unpush_and_throw (void)
5223{
5224 remote_unpush_target ();
5225 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
5226}
5227
f6ac5f3d
PA
5228void
5229remote_target::open_1 (const char *name, int from_tty, int extended_p)
c906108c 5230{
d01949b6 5231 struct remote_state *rs = get_remote_state ();
a6f3e723 5232
c906108c 5233 if (name == 0)
8a3fe4f8 5234 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 5235 "serial device is attached to the remote system\n"
8a3fe4f8 5236 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 5237
23860348 5238 /* See FIXME above. */
c6ebd6cf 5239 if (!target_async_permitted)
92d1e331 5240 wait_forever_enabled_p = 1;
6426a772 5241
2d717e4f 5242 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
5243 Ask this question first, before target_preopen has a chance to kill
5244 anything. */
5d93a237 5245 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 5246 {
78a095c3
JK
5247 if (from_tty
5248 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
5249 error (_("Still connected."));
5250 }
5251
78a095c3 5252 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
5253 target_preopen (from_tty);
5254
89be2091 5255 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
5256 xfree (rs->last_pass_packet);
5257 rs->last_pass_packet = NULL;
89be2091 5258
9b224c5e
PA
5259 /* Make sure we send the program signals list the next time we
5260 resume. */
5e4a05c4
TT
5261 xfree (rs->last_program_signals_packet);
5262 rs->last_program_signals_packet = NULL;
9b224c5e 5263
ad9a8f3f 5264 remote_fileio_reset ();
1dd41f16
NS
5265 reopen_exec_file ();
5266 reread_symbols ();
5267
5d93a237
TT
5268 rs->remote_desc = remote_serial_open (name);
5269 if (!rs->remote_desc)
c906108c
SS
5270 perror_with_name (name);
5271
5272 if (baud_rate != -1)
5273 {
5d93a237 5274 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 5275 {
9b74d5d3
KB
5276 /* The requested speed could not be set. Error out to
5277 top level after closing remote_desc. Take care to
5278 set remote_desc to NULL to avoid closing remote_desc
5279 more than once. */
5d93a237
TT
5280 serial_close (rs->remote_desc);
5281 rs->remote_desc = NULL;
c906108c
SS
5282 perror_with_name (name);
5283 }
5284 }
5285
236af5e3 5286 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 5287 serial_raw (rs->remote_desc);
c906108c
SS
5288
5289 /* If there is something sitting in the buffer we might take it as a
5290 response to a command, which would be bad. */
5d93a237 5291 serial_flush_input (rs->remote_desc);
c906108c
SS
5292
5293 if (from_tty)
5294 {
5295 puts_filtered ("Remote debugging using ");
5296 puts_filtered (name);
5297 puts_filtered ("\n");
5298 }
d9f719f1
PA
5299
5300 remote_target *target
5301 = extended_p ? &extended_remote_ops : &remote_ops;
5302 push_target (target); /* Switch to using remote target now. */
c906108c 5303
74531fed
PA
5304 /* Register extra event sources in the event loop. */
5305 remote_async_inferior_event_token
5306 = create_async_event_handler (remote_async_inferior_event_handler,
5307 NULL);
5965e028 5308 rs->notif_state = remote_notif_state_allocate ();
74531fed 5309
be2a5f71
DJ
5310 /* Reset the target state; these things will be queried either by
5311 remote_query_supported or as they are needed. */
ca4f7f8b 5312 reset_all_packet_configs_support ();
74531fed 5313 rs->cached_wait_status = 0;
be2a5f71 5314 rs->explicit_packet_size = 0;
a6f3e723 5315 rs->noack_mode = 0;
82f73884 5316 rs->extended = extended_p;
e24a49d8 5317 rs->waiting_for_stop_reply = 0;
3a29589a 5318 rs->ctrlc_pending_p = 0;
048094ac 5319 rs->got_ctrlc_during_io = 0;
802188a7 5320
47f8a51d
TT
5321 rs->general_thread = not_sent_ptid;
5322 rs->continue_thread = not_sent_ptid;
262e1174 5323 rs->remote_traceframe_number = -1;
c906108c 5324
3a00c802
PA
5325 rs->last_resume_exec_dir = EXEC_FORWARD;
5326
9d1f7ab2 5327 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
5328 rs->use_threadinfo_query = 1;
5329 rs->use_threadextra_query = 1;
9d1f7ab2 5330
dd194f6b 5331 rs->readahead_cache.invalidate ();
80152258 5332
c6ebd6cf 5333 if (target_async_permitted)
92d1e331 5334 {
92d1e331
DJ
5335 /* FIXME: cagney/1999-09-23: During the initial connection it is
5336 assumed that the target is already ready and able to respond to
0df8b418 5337 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 5338 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 5339 around this. Eventually a mechanism that allows
92d1e331 5340 wait_for_inferior() to expect/get timeouts will be
23860348 5341 implemented. */
92d1e331
DJ
5342 wait_forever_enabled_p = 0;
5343 }
5344
23860348 5345 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 5346 no_shared_libraries (NULL, 0);
f78f6cf1 5347
74531fed
PA
5348 /* Start afresh. */
5349 init_thread_list ();
5350
36918e70 5351 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
5352 target (we'd otherwise be in an inconsistent state) and then
5353 propogate the error on up the exception chain. This ensures that
5354 the caller doesn't stumble along blindly assuming that the
5355 function succeeded. The CLI doesn't have this problem but other
5356 UI's, such as MI do.
36918e70
AC
5357
5358 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5359 this function should return an error indication letting the
ce2826aa 5360 caller restore the previous state. Unfortunately the command
36918e70
AC
5361 ``target remote'' is directly wired to this function making that
5362 impossible. On a positive note, the CLI side of this problem has
5363 been fixed - the function set_cmd_context() makes it possible for
5364 all the ``target ....'' commands to share a common callback
5365 function. See cli-dump.c. */
109c3e39 5366 {
2d717e4f 5367
492d29ea 5368 TRY
04bd08de 5369 {
d9f719f1 5370 target->start_remote (from_tty, extended_p);
04bd08de 5371 }
492d29ea 5372 CATCH (ex, RETURN_MASK_ALL)
109c3e39 5373 {
c8d104ad
PA
5374 /* Pop the partially set up target - unless something else did
5375 already before throwing the exception. */
5d93a237 5376 if (rs->remote_desc != NULL)
78a095c3 5377 remote_unpush_target ();
c6ebd6cf 5378 if (target_async_permitted)
109c3e39
AC
5379 wait_forever_enabled_p = 1;
5380 throw_exception (ex);
5381 }
492d29ea 5382 END_CATCH
109c3e39 5383 }
c906108c 5384
f4abbc16
MM
5385 remote_btrace_reset ();
5386
c6ebd6cf 5387 if (target_async_permitted)
92d1e331 5388 wait_forever_enabled_p = 1;
43ff13b4
JM
5389}
5390
de0d863e
DB
5391/* Detach the specified process. */
5392
5393static void
5394remote_detach_pid (int pid)
5395{
5396 struct remote_state *rs = get_remote_state ();
5397
5398 if (remote_multi_process_p (rs))
5399 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
5400 else
5401 strcpy (rs->buf, "D");
5402
5403 putpkt (rs->buf);
5404 getpkt (&rs->buf, &rs->buf_size, 0);
5405
5406 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
5407 ;
5408 else if (rs->buf[0] == '\0')
5409 error (_("Remote doesn't know how to detach"));
5410 else
5411 error (_("Can't detach process."));
5412}
5413
5414/* This detaches a program to which we previously attached, using
5415 inferior_ptid to identify the process. After this is done, GDB
5416 can be used to debug some other program. We better not have left
5417 any breakpoints in the target program or it'll die when it hits
5418 one. */
c906108c
SS
5419
5420static void
6e1e1966 5421remote_detach_1 (int from_tty, inferior *inf)
c906108c 5422{
82f73884 5423 int pid = ptid_get_pid (inferior_ptid);
d01949b6 5424 struct remote_state *rs = get_remote_state ();
de0d863e
DB
5425 struct thread_info *tp = find_thread_ptid (inferior_ptid);
5426 int is_fork_parent;
c906108c 5427
2d717e4f
DJ
5428 if (!target_has_execution)
5429 error (_("No process to detach from."));
5430
0f48b757 5431 target_announce_detach (from_tty);
7cee1e54 5432
c906108c 5433 /* Tell the remote target to detach. */
de0d863e 5434 remote_detach_pid (pid);
82f73884 5435
8020350c
DB
5436 /* Exit only if this is the only active inferior. */
5437 if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
7cee1e54 5438 puts_filtered (_("Ending remote debugging.\n"));
82f73884 5439
de0d863e
DB
5440 /* Check to see if we are detaching a fork parent. Note that if we
5441 are detaching a fork child, tp == NULL. */
5442 is_fork_parent = (tp != NULL
5443 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
5444
5445 /* If doing detach-on-fork, we don't mourn, because that will delete
5446 breakpoints that should be available for the followed inferior. */
5447 if (!is_fork_parent)
f67c0c91 5448 {
249b5733
PA
5449 /* Save the pid as a string before mourning, since that will
5450 unpush the remote target, and we need the string after. */
5451 std::string infpid = target_pid_to_str (pid_to_ptid (pid));
f67c0c91
SDJ
5452
5453 target_mourn_inferior (inferior_ptid);
5454 if (print_inferior_events)
5455 printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
5456 inf->num, infpid.c_str ());
5457 }
de0d863e
DB
5458 else
5459 {
5460 inferior_ptid = null_ptid;
5461 detach_inferior (pid);
5462 }
2d717e4f
DJ
5463}
5464
f6ac5f3d
PA
5465void
5466remote_target::detach (inferior *inf, int from_tty)
2d717e4f 5467{
6e1e1966 5468 remote_detach_1 (from_tty, inf);
2d717e4f
DJ
5469}
5470
f6ac5f3d
PA
5471void
5472extended_remote_target::detach (inferior *inf, int from_tty)
2d717e4f 5473{
6e1e1966 5474 remote_detach_1 (from_tty, inf);
de0d863e
DB
5475}
5476
5477/* Target follow-fork function for remote targets. On entry, and
5478 at return, the current inferior is the fork parent.
5479
5480 Note that although this is currently only used for extended-remote,
5481 it is named remote_follow_fork in anticipation of using it for the
5482 remote target as well. */
5483
f6ac5f3d
PA
5484int
5485remote_target::follow_fork (int follow_child, int detach_fork)
de0d863e
DB
5486{
5487 struct remote_state *rs = get_remote_state ();
c269dbdb 5488 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 5489
c269dbdb
DB
5490 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
5491 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
5492 {
5493 /* When following the parent and detaching the child, we detach
5494 the child here. For the case of following the child and
5495 detaching the parent, the detach is done in the target-
5496 independent follow fork code in infrun.c. We can't use
5497 target_detach when detaching an unfollowed child because
5498 the client side doesn't know anything about the child. */
5499 if (detach_fork && !follow_child)
5500 {
5501 /* Detach the fork child. */
5502 ptid_t child_ptid;
5503 pid_t child_pid;
5504
5505 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
5506 child_pid = ptid_get_pid (child_ptid);
5507
5508 remote_detach_pid (child_pid);
de0d863e
DB
5509 }
5510 }
5511 return 0;
c906108c
SS
5512}
5513
94585166
DB
5514/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
5515 in the program space of the new inferior. On entry and at return the
5516 current inferior is the exec'ing inferior. INF is the new exec'd
5517 inferior, which may be the same as the exec'ing inferior unless
5518 follow-exec-mode is "new". */
5519
f6ac5f3d
PA
5520void
5521remote_target::follow_exec (struct inferior *inf, char *execd_pathname)
94585166
DB
5522{
5523 /* We know that this is a target file name, so if it has the "target:"
5524 prefix we strip it off before saving it in the program space. */
5525 if (is_target_filename (execd_pathname))
5526 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
5527
5528 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
5529}
5530
6ad8ae5c
DJ
5531/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
5532
f6ac5f3d
PA
5533void
5534remote_target::disconnect (const char *args, int from_tty)
43ff13b4 5535{
43ff13b4 5536 if (args)
2d717e4f 5537 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 5538
8020350c
DB
5539 /* Make sure we unpush even the extended remote targets. Calling
5540 target_mourn_inferior won't unpush, and remote_mourn won't
5541 unpush if there is more than one inferior left. */
f6ac5f3d 5542 unpush_target (this);
8020350c 5543 generic_mourn_inferior ();
2d717e4f 5544
43ff13b4
JM
5545 if (from_tty)
5546 puts_filtered ("Ending remote debugging.\n");
5547}
5548
2d717e4f
DJ
5549/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
5550 be chatty about it. */
5551
f6ac5f3d
PA
5552void
5553extended_remote_target::attach (const char *args, int from_tty)
2d717e4f
DJ
5554{
5555 struct remote_state *rs = get_remote_state ();
be86555c 5556 int pid;
96ef3384 5557 char *wait_status = NULL;
2d717e4f 5558
74164c56 5559 pid = parse_pid_to_attach (args);
2d717e4f 5560
74164c56
JK
5561 /* Remote PID can be freely equal to getpid, do not check it here the same
5562 way as in other targets. */
2d717e4f 5563
4082afcc 5564 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
5565 error (_("This target does not support attaching to a process"));
5566
7cee1e54
PA
5567 if (from_tty)
5568 {
5569 char *exec_file = get_exec_file (0);
5570
5571 if (exec_file)
5572 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5573 target_pid_to_str (pid_to_ptid (pid)));
5574 else
5575 printf_unfiltered (_("Attaching to %s\n"),
5576 target_pid_to_str (pid_to_ptid (pid)));
5577
5578 gdb_flush (gdb_stdout);
5579 }
5580
bba74b36 5581 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
5582 putpkt (rs->buf);
5583 getpkt (&rs->buf, &rs->buf_size, 0);
5584
4082afcc
PA
5585 switch (packet_ok (rs->buf,
5586 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 5587 {
4082afcc 5588 case PACKET_OK:
6efcd9a8 5589 if (!target_is_non_stop_p ())
74531fed
PA
5590 {
5591 /* Save the reply for later. */
224c3ddb 5592 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
5593 strcpy (wait_status, rs->buf);
5594 }
5595 else if (strcmp (rs->buf, "OK") != 0)
5596 error (_("Attaching to %s failed with: %s"),
5597 target_pid_to_str (pid_to_ptid (pid)),
5598 rs->buf);
4082afcc
PA
5599 break;
5600 case PACKET_UNKNOWN:
5601 error (_("This target does not support attaching to a process"));
5602 default:
5603 error (_("Attaching to %s failed"),
5604 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 5605 }
2d717e4f 5606
1b6e6f5c 5607 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 5608
2d717e4f 5609 inferior_ptid = pid_to_ptid (pid);
79d7f229 5610
6efcd9a8 5611 if (target_is_non_stop_p ())
bad34192
PA
5612 {
5613 struct thread_info *thread;
79d7f229 5614
bad34192 5615 /* Get list of threads. */
f6ac5f3d 5616 update_thread_list ();
82f73884 5617
bad34192
PA
5618 thread = first_thread_of_process (pid);
5619 if (thread)
5620 inferior_ptid = thread->ptid;
5621 else
5622 inferior_ptid = pid_to_ptid (pid);
5623
5624 /* Invalidate our notion of the remote current thread. */
47f8a51d 5625 record_currthread (rs, minus_one_ptid);
bad34192 5626 }
74531fed 5627 else
bad34192
PA
5628 {
5629 /* Now, if we have thread information, update inferior_ptid. */
5630 inferior_ptid = remote_current_thread (inferior_ptid);
5631
5632 /* Add the main thread to the thread list. */
00aecdcf
PA
5633 thread_info *thr = add_thread_silent (inferior_ptid);
5634 /* Don't consider the thread stopped until we've processed the
5635 saved stop reply. */
5636 set_executing (thr->ptid, true);
bad34192 5637 }
c0a2216e 5638
96ef3384
UW
5639 /* Next, if the target can specify a description, read it. We do
5640 this before anything involving memory or registers. */
5641 target_find_description ();
5642
6efcd9a8 5643 if (!target_is_non_stop_p ())
74531fed
PA
5644 {
5645 /* Use the previously fetched status. */
5646 gdb_assert (wait_status != NULL);
5647
5648 if (target_can_async_p ())
5649 {
722247f1
YQ
5650 struct notif_event *reply
5651 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 5652
722247f1 5653 push_stop_reply ((struct stop_reply *) reply);
74531fed 5654
6a3753b3 5655 target_async (1);
74531fed
PA
5656 }
5657 else
5658 {
5659 gdb_assert (wait_status != NULL);
5660 strcpy (rs->buf, wait_status);
5661 rs->cached_wait_status = 1;
5662 }
5663 }
5664 else
5665 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5666}
5667
b9c1d481
AS
5668/* Implementation of the to_post_attach method. */
5669
f6ac5f3d
PA
5670void
5671extended_remote_target::post_attach (int pid)
b9c1d481 5672{
6efcd9a8
PA
5673 /* Get text, data & bss offsets. */
5674 get_offsets ();
5675
b9c1d481
AS
5676 /* In certain cases GDB might not have had the chance to start
5677 symbol lookup up until now. This could happen if the debugged
5678 binary is not using shared libraries, the vsyscall page is not
5679 present (on Linux) and the binary itself hadn't changed since the
5680 debugging process was started. */
5681 if (symfile_objfile != NULL)
5682 remote_check_symbols();
5683}
5684
c906108c 5685\f
506fb367
DJ
5686/* Check for the availability of vCont. This function should also check
5687 the response. */
c906108c
SS
5688
5689static void
6d820c5c 5690remote_vcont_probe (struct remote_state *rs)
c906108c 5691{
2e9f7625 5692 char *buf;
6d820c5c 5693
2e9f7625
DJ
5694 strcpy (rs->buf, "vCont?");
5695 putpkt (rs->buf);
6d820c5c 5696 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5697 buf = rs->buf;
c906108c 5698
506fb367 5699 /* Make sure that the features we assume are supported. */
61012eef 5700 if (startswith (buf, "vCont"))
506fb367
DJ
5701 {
5702 char *p = &buf[5];
750ce8d1 5703 int support_c, support_C;
506fb367 5704
750ce8d1
YQ
5705 rs->supports_vCont.s = 0;
5706 rs->supports_vCont.S = 0;
506fb367
DJ
5707 support_c = 0;
5708 support_C = 0;
d458bd84 5709 rs->supports_vCont.t = 0;
c1e36e3e 5710 rs->supports_vCont.r = 0;
506fb367
DJ
5711 while (p && *p == ';')
5712 {
5713 p++;
5714 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5715 rs->supports_vCont.s = 1;
506fb367 5716 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5717 rs->supports_vCont.S = 1;
506fb367
DJ
5718 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5719 support_c = 1;
5720 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5721 support_C = 1;
74531fed 5722 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5723 rs->supports_vCont.t = 1;
c1e36e3e
PA
5724 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5725 rs->supports_vCont.r = 1;
506fb367
DJ
5726
5727 p = strchr (p, ';');
5728 }
c906108c 5729
750ce8d1
YQ
5730 /* If c, and C are not all supported, we can't use vCont. Clearing
5731 BUF will make packet_ok disable the packet. */
5732 if (!support_c || !support_C)
506fb367
DJ
5733 buf[0] = 0;
5734 }
c906108c 5735
444abaca 5736 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5737}
c906108c 5738
0d8f58ca
PA
5739/* Helper function for building "vCont" resumptions. Write a
5740 resumption to P. ENDP points to one-passed-the-end of the buffer
5741 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5742 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5743 resumed thread should be single-stepped and/or signalled. If PTID
5744 equals minus_one_ptid, then all threads are resumed; if PTID
5745 represents a process, then all threads of the process are resumed;
5746 the thread to be stepped and/or signalled is given in the global
5747 INFERIOR_PTID. */
5748
5749static char *
5750append_resumption (char *p, char *endp,
2ea28649 5751 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5752{
5753 struct remote_state *rs = get_remote_state ();
5754
a493e3e2 5755 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5756 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5757 else if (step
5758 /* GDB is willing to range step. */
5759 && use_range_stepping
5760 /* Target supports range stepping. */
5761 && rs->supports_vCont.r
5762 /* We don't currently support range stepping multiple
5763 threads with a wildcard (though the protocol allows it,
5764 so stubs shouldn't make an active effort to forbid
5765 it). */
5766 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5767 {
5768 struct thread_info *tp;
5769
5770 if (ptid_equal (ptid, minus_one_ptid))
5771 {
5772 /* If we don't know about the target thread's tid, then
5773 we're resuming magic_null_ptid (see caller). */
5774 tp = find_thread_ptid (magic_null_ptid);
5775 }
5776 else
5777 tp = find_thread_ptid (ptid);
5778 gdb_assert (tp != NULL);
5779
5780 if (tp->control.may_range_step)
5781 {
5782 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5783
5784 p += xsnprintf (p, endp - p, ";r%s,%s",
5785 phex_nz (tp->control.step_range_start,
5786 addr_size),
5787 phex_nz (tp->control.step_range_end,
5788 addr_size));
5789 }
5790 else
5791 p += xsnprintf (p, endp - p, ";s");
5792 }
0d8f58ca
PA
5793 else if (step)
5794 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5795 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5796 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5797 else
5798 p += xsnprintf (p, endp - p, ";c");
5799
5800 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5801 {
5802 ptid_t nptid;
5803
5804 /* All (-1) threads of process. */
ba348170 5805 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5806
5807 p += xsnprintf (p, endp - p, ":");
5808 p = write_ptid (p, endp, nptid);
5809 }
5810 else if (!ptid_equal (ptid, minus_one_ptid))
5811 {
5812 p += xsnprintf (p, endp - p, ":");
5813 p = write_ptid (p, endp, ptid);
5814 }
5815
5816 return p;
5817}
5818
799a2abe
PA
5819/* Clear the thread's private info on resume. */
5820
5821static void
5822resume_clear_thread_private_info (struct thread_info *thread)
5823{
5824 if (thread->priv != NULL)
5825 {
7aabaf9d
SM
5826 remote_thread_info *priv = get_remote_thread_info (thread);
5827
5828 priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5829 priv->watch_data_address = 0;
799a2abe
PA
5830 }
5831}
5832
e5ef252a
PA
5833/* Append a vCont continue-with-signal action for threads that have a
5834 non-zero stop signal. */
5835
5836static char *
5837append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5838{
5839 struct thread_info *thread;
5840
034f788c 5841 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5842 if (ptid_match (thread->ptid, ptid)
5843 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5844 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5845 {
5846 p = append_resumption (p, endp, thread->ptid,
5847 0, thread->suspend.stop_signal);
5848 thread->suspend.stop_signal = GDB_SIGNAL_0;
799a2abe 5849 resume_clear_thread_private_info (thread);
e5ef252a
PA
5850 }
5851
5852 return p;
5853}
5854
7b68ffbb
PA
5855/* Set the target running, using the packets that use Hc
5856 (c/s/C/S). */
5857
5858static void
5859remote_resume_with_hc (struct target_ops *ops,
5860 ptid_t ptid, int step, enum gdb_signal siggnal)
5861{
5862 struct remote_state *rs = get_remote_state ();
5863 struct thread_info *thread;
5864 char *buf;
5865
5866 rs->last_sent_signal = siggnal;
5867 rs->last_sent_step = step;
5868
5869 /* The c/s/C/S resume packets use Hc, so set the continue
5870 thread. */
5871 if (ptid_equal (ptid, minus_one_ptid))
5872 set_continue_thread (any_thread_ptid);
5873 else
5874 set_continue_thread (ptid);
5875
5876 ALL_NON_EXITED_THREADS (thread)
5877 resume_clear_thread_private_info (thread);
5878
5879 buf = rs->buf;
5880 if (execution_direction == EXEC_REVERSE)
5881 {
5882 /* We don't pass signals to the target in reverse exec mode. */
5883 if (info_verbose && siggnal != GDB_SIGNAL_0)
5884 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5885 siggnal);
5886
5887 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5888 error (_("Remote reverse-step not supported."));
5889 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5890 error (_("Remote reverse-continue not supported."));
5891
5892 strcpy (buf, step ? "bs" : "bc");
5893 }
5894 else if (siggnal != GDB_SIGNAL_0)
5895 {
5896 buf[0] = step ? 'S' : 'C';
5897 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5898 buf[2] = tohex (((int) siggnal) & 0xf);
5899 buf[3] = '\0';
5900 }
5901 else
5902 strcpy (buf, step ? "s" : "c");
5903
5904 putpkt (buf);
5905}
5906
506fb367
DJ
5907/* Resume the remote inferior by using a "vCont" packet. The thread
5908 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5909 resumed thread should be single-stepped and/or signalled. If PTID
5910 equals minus_one_ptid, then all threads are resumed; the thread to
5911 be stepped and/or signalled is given in the global INFERIOR_PTID.
5912 This function returns non-zero iff it resumes the inferior.
44eaed12 5913
7b68ffbb
PA
5914 This function issues a strict subset of all possible vCont commands
5915 at the moment. */
44eaed12 5916
506fb367 5917static int
7b68ffbb 5918remote_resume_with_vcont (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5919{
5920 struct remote_state *rs = get_remote_state ();
82f73884
PA
5921 char *p;
5922 char *endp;
44eaed12 5923
7b68ffbb
PA
5924 /* No reverse execution actions defined for vCont. */
5925 if (execution_direction == EXEC_REVERSE)
5926 return 0;
5927
4082afcc 5928 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5929 remote_vcont_probe (rs);
44eaed12 5930
4082afcc 5931 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5932 return 0;
44eaed12 5933
82f73884
PA
5934 p = rs->buf;
5935 endp = rs->buf + get_remote_packet_size ();
5936
506fb367
DJ
5937 /* If we could generate a wider range of packets, we'd have to worry
5938 about overflowing BUF. Should there be a generic
5939 "multi-part-packet" packet? */
5940
0d8f58ca
PA
5941 p += xsnprintf (p, endp - p, "vCont");
5942
79d7f229 5943 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5944 {
79d7f229
PA
5945 /* MAGIC_NULL_PTID means that we don't have any active threads,
5946 so we don't have any TID numbers the inferior will
5947 understand. Make sure to only send forms that do not specify
5948 a TID. */
a9cbf802 5949 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5950 }
0d8f58ca 5951 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5952 {
0d8f58ca
PA
5953 /* Resume all threads (of all processes, or of a single
5954 process), with preference for INFERIOR_PTID. This assumes
5955 inferior_ptid belongs to the set of all threads we are about
5956 to resume. */
a493e3e2 5957 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5958 {
0d8f58ca
PA
5959 /* Step inferior_ptid, with or without signal. */
5960 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5961 }
0d8f58ca 5962
e5ef252a
PA
5963 /* Also pass down any pending signaled resumption for other
5964 threads not the current. */
5965 p = append_pending_thread_resumptions (p, endp, ptid);
5966
0d8f58ca 5967 /* And continue others without a signal. */
a493e3e2 5968 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5969 }
5970 else
506fb367
DJ
5971 {
5972 /* Scheduler locking; resume only PTID. */
a9cbf802 5973 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5974 }
c906108c 5975
82f73884
PA
5976 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5977 putpkt (rs->buf);
506fb367 5978
6efcd9a8 5979 if (target_is_non_stop_p ())
74531fed
PA
5980 {
5981 /* In non-stop, the stub replies to vCont with "OK". The stop
5982 reply will be reported asynchronously by means of a `%Stop'
5983 notification. */
5984 getpkt (&rs->buf, &rs->buf_size, 0);
5985 if (strcmp (rs->buf, "OK") != 0)
5986 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5987 }
5988
506fb367 5989 return 1;
c906108c 5990}
43ff13b4 5991
506fb367
DJ
5992/* Tell the remote machine to resume. */
5993
f6ac5f3d
PA
5994void
5995remote_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5996{
d01949b6 5997 struct remote_state *rs = get_remote_state ();
43ff13b4 5998
85ad3aaf
PA
5999 /* When connected in non-stop mode, the core resumes threads
6000 individually. Resuming remote threads directly in target_resume
6001 would thus result in sending one packet per thread. Instead, to
6002 minimize roundtrip latency, here we just store the resume
6003 request; the actual remote resumption will be done in
6004 target_commit_resume / remote_commit_resume, where we'll be able
6005 to do vCont action coalescing. */
f6ac5f3d 6006 if (target_is_non_stop_p () && ::execution_direction != EXEC_REVERSE)
85ad3aaf 6007 {
7aabaf9d 6008 remote_thread_info *remote_thr;
85ad3aaf
PA
6009
6010 if (ptid_equal (minus_one_ptid, ptid) || ptid_is_pid (ptid))
7aabaf9d 6011 remote_thr = get_remote_thread_info (inferior_ptid);
85ad3aaf 6012 else
7aabaf9d
SM
6013 remote_thr = get_remote_thread_info (ptid);
6014
85ad3aaf
PA
6015 remote_thr->last_resume_step = step;
6016 remote_thr->last_resume_sig = siggnal;
6017 return;
6018 }
6019
722247f1
YQ
6020 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6021 (explained in remote-notif.c:handle_notification) so
6022 remote_notif_process is not called. We need find a place where
6023 it is safe to start a 'vNotif' sequence. It is good to do it
6024 before resuming inferior, because inferior was stopped and no RSP
6025 traffic at that moment. */
6efcd9a8 6026 if (!target_is_non_stop_p ())
5965e028 6027 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 6028
f6ac5f3d 6029 rs->last_resume_exec_dir = ::execution_direction;
3a00c802 6030
7b68ffbb
PA
6031 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6032 if (!remote_resume_with_vcont (ptid, step, siggnal))
f6ac5f3d 6033 remote_resume_with_hc (this, ptid, step, siggnal);
43ff13b4 6034
2acceee2 6035 /* We are about to start executing the inferior, let's register it
0df8b418
MS
6036 with the event loop. NOTE: this is the one place where all the
6037 execution commands end up. We could alternatively do this in each
23860348 6038 of the execution commands in infcmd.c. */
2acceee2
JM
6039 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
6040 into infcmd.c in order to allow inferior function calls to work
23860348 6041 NOT asynchronously. */
362646f5 6042 if (target_can_async_p ())
6a3753b3 6043 target_async (1);
e24a49d8
PA
6044
6045 /* We've just told the target to resume. The remote server will
6046 wait for the inferior to stop, and then send a stop reply. In
6047 the mean time, we can't start another command/query ourselves
74531fed
PA
6048 because the stub wouldn't be ready to process it. This applies
6049 only to the base all-stop protocol, however. In non-stop (which
6050 only supports vCont), the stub replies with an "OK", and is
6051 immediate able to process further serial input. */
6efcd9a8 6052 if (!target_is_non_stop_p ())
74531fed 6053 rs->waiting_for_stop_reply = 1;
43ff13b4 6054}
85ad3aaf
PA
6055
6056static void check_pending_events_prevent_wildcard_vcont
6057 (int *may_global_wildcard_vcont);
6058static int is_pending_fork_parent_thread (struct thread_info *thread);
6059
6060/* Private per-inferior info for target remote processes. */
6061
089354bb 6062struct remote_inferior : public private_inferior
85ad3aaf
PA
6063{
6064 /* Whether we can send a wildcard vCont for this process. */
089354bb 6065 bool may_wildcard_vcont = true;
85ad3aaf
PA
6066};
6067
089354bb
SM
6068/* Get the remote private inferior data associated to INF. */
6069
6070static remote_inferior *
6071get_remote_inferior (inferior *inf)
6072{
6073 if (inf->priv == NULL)
6074 inf->priv.reset (new remote_inferior);
6075
6076 return static_cast<remote_inferior *> (inf->priv.get ());
6077}
6078
85ad3aaf
PA
6079/* Structure used to track the construction of a vCont packet in the
6080 outgoing packet buffer. This is used to send multiple vCont
6081 packets if we have more actions than would fit a single packet. */
6082
6083struct vcont_builder
6084{
6085 /* Pointer to the first action. P points here if no action has been
6086 appended yet. */
6087 char *first_action;
6088
6089 /* Where the next action will be appended. */
6090 char *p;
6091
6092 /* The end of the buffer. Must never write past this. */
6093 char *endp;
6094};
6095
6096/* Prepare the outgoing buffer for a new vCont packet. */
6097
6098static void
6099vcont_builder_restart (struct vcont_builder *builder)
6100{
6101 struct remote_state *rs = get_remote_state ();
6102
6103 builder->p = rs->buf;
6104 builder->endp = rs->buf + get_remote_packet_size ();
6105 builder->p += xsnprintf (builder->p, builder->endp - builder->p, "vCont");
6106 builder->first_action = builder->p;
6107}
6108
6109/* If the vCont packet being built has any action, send it to the
6110 remote end. */
6111
6112static void
6113vcont_builder_flush (struct vcont_builder *builder)
6114{
6115 struct remote_state *rs;
6116
6117 if (builder->p == builder->first_action)
6118 return;
6119
6120 rs = get_remote_state ();
6121 putpkt (rs->buf);
6122 getpkt (&rs->buf, &rs->buf_size, 0);
6123 if (strcmp (rs->buf, "OK") != 0)
6124 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
6125}
6126
6127/* The largest action is range-stepping, with its two addresses. This
6128 is more than sufficient. If a new, bigger action is created, it'll
6129 quickly trigger a failed assertion in append_resumption (and we'll
6130 just bump this). */
6131#define MAX_ACTION_SIZE 200
6132
6133/* Append a new vCont action in the outgoing packet being built. If
6134 the action doesn't fit the packet along with previous actions, push
6135 what we've got so far to the remote end and start over a new vCont
6136 packet (with the new action). */
6137
6138static void
6139vcont_builder_push_action (struct vcont_builder *builder,
6140 ptid_t ptid, int step, enum gdb_signal siggnal)
6141{
6142 char buf[MAX_ACTION_SIZE + 1];
6143 char *endp;
6144 size_t rsize;
6145
6146 endp = append_resumption (buf, buf + sizeof (buf),
6147 ptid, step, siggnal);
6148
6149 /* Check whether this new action would fit in the vCont packet along
6150 with previous actions. If not, send what we've got so far and
6151 start a new vCont packet. */
6152 rsize = endp - buf;
6153 if (rsize > builder->endp - builder->p)
6154 {
6155 vcont_builder_flush (builder);
6156 vcont_builder_restart (builder);
6157
6158 /* Should now fit. */
6159 gdb_assert (rsize <= builder->endp - builder->p);
6160 }
6161
6162 memcpy (builder->p, buf, rsize);
6163 builder->p += rsize;
6164 *builder->p = '\0';
6165}
6166
6167/* to_commit_resume implementation. */
6168
f6ac5f3d
PA
6169void
6170remote_target::commit_resume ()
85ad3aaf 6171{
85ad3aaf
PA
6172 struct inferior *inf;
6173 struct thread_info *tp;
6174 int any_process_wildcard;
6175 int may_global_wildcard_vcont;
6176 struct vcont_builder vcont_builder;
6177
6178 /* If connected in all-stop mode, we'd send the remote resume
6179 request directly from remote_resume. Likewise if
6180 reverse-debugging, as there are no defined vCont actions for
6181 reverse execution. */
f6ac5f3d 6182 if (!target_is_non_stop_p () || ::execution_direction == EXEC_REVERSE)
85ad3aaf
PA
6183 return;
6184
6185 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6186 instead of resuming all threads of each process individually.
6187 However, if any thread of a process must remain halted, we can't
6188 send wildcard resumes and must send one action per thread.
6189
6190 Care must be taken to not resume threads/processes the server
6191 side already told us are stopped, but the core doesn't know about
6192 yet, because the events are still in the vStopped notification
6193 queue. For example:
6194
6195 #1 => vCont s:p1.1;c
6196 #2 <= OK
6197 #3 <= %Stopped T05 p1.1
6198 #4 => vStopped
6199 #5 <= T05 p1.2
6200 #6 => vStopped
6201 #7 <= OK
6202 #8 (infrun handles the stop for p1.1 and continues stepping)
6203 #9 => vCont s:p1.1;c
6204
6205 The last vCont above would resume thread p1.2 by mistake, because
6206 the server has no idea that the event for p1.2 had not been
6207 handled yet.
6208
6209 The server side must similarly ignore resume actions for the
6210 thread that has a pending %Stopped notification (and any other
6211 threads with events pending), until GDB acks the notification
6212 with vStopped. Otherwise, e.g., the following case is
6213 mishandled:
6214
6215 #1 => g (or any other packet)
6216 #2 <= [registers]
6217 #3 <= %Stopped T05 p1.2
6218 #4 => vCont s:p1.1;c
6219 #5 <= OK
6220
6221 Above, the server must not resume thread p1.2. GDB can't know
6222 that p1.2 stopped until it acks the %Stopped notification, and
6223 since from GDB's perspective all threads should be running, it
6224 sends a "c" action.
6225
6226 Finally, special care must also be given to handling fork/vfork
6227 events. A (v)fork event actually tells us that two processes
6228 stopped -- the parent and the child. Until we follow the fork,
6229 we must not resume the child. Therefore, if we have a pending
6230 fork follow, we must not send a global wildcard resume action
6231 (vCont;c). We can still send process-wide wildcards though. */
6232
6233 /* Start by assuming a global wildcard (vCont;c) is possible. */
6234 may_global_wildcard_vcont = 1;
6235
6236 /* And assume every process is individually wildcard-able too. */
6237 ALL_NON_EXITED_INFERIORS (inf)
6238 {
089354bb
SM
6239 remote_inferior *priv = get_remote_inferior (inf);
6240
6241 priv->may_wildcard_vcont = true;
85ad3aaf
PA
6242 }
6243
6244 /* Check for any pending events (not reported or processed yet) and
6245 disable process and global wildcard resumes appropriately. */
6246 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
6247
6248 ALL_NON_EXITED_THREADS (tp)
6249 {
6250 /* If a thread of a process is not meant to be resumed, then we
6251 can't wildcard that process. */
6252 if (!tp->executing)
6253 {
089354bb 6254 get_remote_inferior (tp->inf)->may_wildcard_vcont = false;
85ad3aaf
PA
6255
6256 /* And if we can't wildcard a process, we can't wildcard
6257 everything either. */
6258 may_global_wildcard_vcont = 0;
6259 continue;
6260 }
6261
6262 /* If a thread is the parent of an unfollowed fork, then we
6263 can't do a global wildcard, as that would resume the fork
6264 child. */
6265 if (is_pending_fork_parent_thread (tp))
6266 may_global_wildcard_vcont = 0;
6267 }
6268
6269 /* Now let's build the vCont packet(s). Actions must be appended
6270 from narrower to wider scopes (thread -> process -> global). If
6271 we end up with too many actions for a single packet vcont_builder
6272 flushes the current vCont packet to the remote side and starts a
6273 new one. */
6274 vcont_builder_restart (&vcont_builder);
6275
6276 /* Threads first. */
6277 ALL_NON_EXITED_THREADS (tp)
6278 {
7aabaf9d 6279 remote_thread_info *remote_thr = get_remote_thread_info (tp);
85ad3aaf
PA
6280
6281 if (!tp->executing || remote_thr->vcont_resumed)
6282 continue;
6283
6284 gdb_assert (!thread_is_in_step_over_chain (tp));
6285
6286 if (!remote_thr->last_resume_step
6287 && remote_thr->last_resume_sig == GDB_SIGNAL_0
089354bb 6288 && get_remote_inferior (tp->inf)->may_wildcard_vcont)
85ad3aaf
PA
6289 {
6290 /* We'll send a wildcard resume instead. */
6291 remote_thr->vcont_resumed = 1;
6292 continue;
6293 }
6294
6295 vcont_builder_push_action (&vcont_builder, tp->ptid,
6296 remote_thr->last_resume_step,
6297 remote_thr->last_resume_sig);
6298 remote_thr->vcont_resumed = 1;
6299 }
6300
6301 /* Now check whether we can send any process-wide wildcard. This is
6302 to avoid sending a global wildcard in the case nothing is
6303 supposed to be resumed. */
6304 any_process_wildcard = 0;
6305
6306 ALL_NON_EXITED_INFERIORS (inf)
6307 {
089354bb 6308 if (get_remote_inferior (inf)->may_wildcard_vcont)
85ad3aaf
PA
6309 {
6310 any_process_wildcard = 1;
6311 break;
6312 }
6313 }
6314
6315 if (any_process_wildcard)
6316 {
6317 /* If all processes are wildcard-able, then send a single "c"
6318 action, otherwise, send an "all (-1) threads of process"
6319 continue action for each running process, if any. */
6320 if (may_global_wildcard_vcont)
6321 {
6322 vcont_builder_push_action (&vcont_builder, minus_one_ptid,
6323 0, GDB_SIGNAL_0);
6324 }
6325 else
6326 {
6327 ALL_NON_EXITED_INFERIORS (inf)
6328 {
089354bb 6329 if (get_remote_inferior (inf)->may_wildcard_vcont)
85ad3aaf
PA
6330 {
6331 vcont_builder_push_action (&vcont_builder,
6332 pid_to_ptid (inf->pid),
6333 0, GDB_SIGNAL_0);
6334 }
6335 }
6336 }
6337 }
6338
6339 vcont_builder_flush (&vcont_builder);
6340}
6341
c906108c 6342\f
43ff13b4 6343
74531fed
PA
6344/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6345 thread, all threads of a remote process, or all threads of all
6346 processes. */
6347
6348static void
6349remote_stop_ns (ptid_t ptid)
6350{
6351 struct remote_state *rs = get_remote_state ();
6352 char *p = rs->buf;
6353 char *endp = rs->buf + get_remote_packet_size ();
74531fed 6354
4082afcc 6355 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
6356 remote_vcont_probe (rs);
6357
d458bd84 6358 if (!rs->supports_vCont.t)
74531fed
PA
6359 error (_("Remote server does not support stopping threads"));
6360
f91d3df5
PA
6361 if (ptid_equal (ptid, minus_one_ptid)
6362 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
6363 p += xsnprintf (p, endp - p, "vCont;t");
6364 else
6365 {
6366 ptid_t nptid;
6367
74531fed
PA
6368 p += xsnprintf (p, endp - p, "vCont;t:");
6369
6370 if (ptid_is_pid (ptid))
6371 /* All (-1) threads of process. */
ba348170 6372 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
6373 else
6374 {
6375 /* Small optimization: if we already have a stop reply for
6376 this thread, no use in telling the stub we want this
6377 stopped. */
6378 if (peek_stop_reply (ptid))
6379 return;
6380
6381 nptid = ptid;
6382 }
6383
a9cbf802 6384 write_ptid (p, endp, nptid);
74531fed
PA
6385 }
6386
6387 /* In non-stop, we get an immediate OK reply. The stop reply will
6388 come in asynchronously by notification. */
6389 putpkt (rs->buf);
6390 getpkt (&rs->buf, &rs->buf_size, 0);
6391 if (strcmp (rs->buf, "OK") != 0)
6392 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
6393}
6394
bfedc46a
PA
6395/* All-stop version of target_interrupt. Sends a break or a ^C to
6396 interrupt the remote target. It is undefined which thread of which
6397 process reports the interrupt. */
74531fed
PA
6398
6399static void
de979965 6400remote_interrupt_as (void)
74531fed
PA
6401{
6402 struct remote_state *rs = get_remote_state ();
6403
3a29589a
DJ
6404 rs->ctrlc_pending_p = 1;
6405
74531fed
PA
6406 /* If the inferior is stopped already, but the core didn't know
6407 about it yet, just ignore the request. The cached wait status
6408 will be collected in remote_wait. */
6409 if (rs->cached_wait_status)
6410 return;
6411
9a7071a8
JB
6412 /* Send interrupt_sequence to remote target. */
6413 send_interrupt_sequence ();
74531fed
PA
6414}
6415
de979965
PA
6416/* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
6417 the remote target. It is undefined which thread of which process
e42de8c7
PA
6418 reports the interrupt. Throws an error if the packet is not
6419 supported by the server. */
de979965 6420
e42de8c7 6421static void
de979965
PA
6422remote_interrupt_ns (void)
6423{
6424 struct remote_state *rs = get_remote_state ();
6425 char *p = rs->buf;
6426 char *endp = rs->buf + get_remote_packet_size ();
6427
6428 xsnprintf (p, endp - p, "vCtrlC");
6429
6430 /* In non-stop, we get an immediate OK reply. The stop reply will
6431 come in asynchronously by notification. */
6432 putpkt (rs->buf);
6433 getpkt (&rs->buf, &rs->buf_size, 0);
6434
6435 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
6436 {
6437 case PACKET_OK:
6438 break;
6439 case PACKET_UNKNOWN:
e42de8c7 6440 error (_("No support for interrupting the remote target."));
de979965
PA
6441 case PACKET_ERROR:
6442 error (_("Interrupting target failed: %s"), rs->buf);
6443 }
de979965
PA
6444}
6445
bfedc46a 6446/* Implement the to_stop function for the remote targets. */
74531fed 6447
f6ac5f3d
PA
6448void
6449remote_target::stop (ptid_t ptid)
c906108c 6450{
7a292a7a 6451 if (remote_debug)
0f71a2f6 6452 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 6453
6efcd9a8 6454 if (target_is_non_stop_p ())
74531fed 6455 remote_stop_ns (ptid);
c906108c 6456 else
bfedc46a
PA
6457 {
6458 /* We don't currently have a way to transparently pause the
6459 remote target in all-stop mode. Interrupt it instead. */
de979965 6460 remote_interrupt_as ();
bfedc46a
PA
6461 }
6462}
6463
6464/* Implement the to_interrupt function for the remote targets. */
6465
f6ac5f3d
PA
6466void
6467remote_target::interrupt ()
bfedc46a
PA
6468{
6469 if (remote_debug)
6470 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
6471
e42de8c7
PA
6472 if (target_is_non_stop_p ())
6473 remote_interrupt_ns ();
bfedc46a 6474 else
e42de8c7 6475 remote_interrupt_as ();
c906108c
SS
6476}
6477
93692b58
PA
6478/* Implement the to_pass_ctrlc function for the remote targets. */
6479
f6ac5f3d
PA
6480void
6481remote_target::pass_ctrlc ()
93692b58
PA
6482{
6483 struct remote_state *rs = get_remote_state ();
6484
6485 if (remote_debug)
6486 fprintf_unfiltered (gdb_stdlog, "remote_pass_ctrlc called\n");
6487
6488 /* If we're starting up, we're not fully synced yet. Quit
6489 immediately. */
6490 if (rs->starting_up)
6491 quit ();
6492 /* If ^C has already been sent once, offer to disconnect. */
6493 else if (rs->ctrlc_pending_p)
6494 interrupt_query ();
6495 else
e671cd59 6496 target_interrupt ();
93692b58
PA
6497}
6498
c906108c
SS
6499/* Ask the user what to do when an interrupt is received. */
6500
6501static void
fba45db2 6502interrupt_query (void)
c906108c 6503{
abc56d60 6504 struct remote_state *rs = get_remote_state ();
c906108c 6505
abc56d60 6506 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 6507 {
abc56d60
PA
6508 if (query (_("The target is not responding to interrupt requests.\n"
6509 "Stop debugging it? ")))
74531fed 6510 {
78a095c3 6511 remote_unpush_target ();
abc56d60 6512 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
6513 }
6514 }
abc56d60
PA
6515 else
6516 {
6517 if (query (_("Interrupted while waiting for the program.\n"
6518 "Give up waiting? ")))
6519 quit ();
6520 }
c906108c
SS
6521}
6522
6426a772
JM
6523/* Enable/disable target terminal ownership. Most targets can use
6524 terminal groups to control terminal ownership. Remote targets are
6525 different in that explicit transfer of ownership to/from GDB/target
23860348 6526 is required. */
6426a772 6527
f6ac5f3d
PA
6528void
6529remote_target::terminal_inferior ()
6426a772 6530{
6426a772
JM
6531 /* NOTE: At this point we could also register our selves as the
6532 recipient of all input. Any characters typed could then be
23860348 6533 passed on down to the target. */
6426a772
JM
6534}
6535
f6ac5f3d
PA
6536void
6537remote_target::terminal_ours ()
6426a772 6538{
6426a772
JM
6539}
6540
176a6961 6541static void
917317f4 6542remote_console_output (char *msg)
c906108c
SS
6543{
6544 char *p;
6545
c5aa993b 6546 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
6547 {
6548 char tb[2];
6549 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 6550
c906108c
SS
6551 tb[0] = c;
6552 tb[1] = 0;
43ff13b4 6553 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 6554 }
00db5b94
PA
6555 gdb_flush (gdb_stdtarg);
6556}
74531fed 6557
74531fed
PA
6558DEF_VEC_O(cached_reg_t);
6559
722247f1 6560typedef struct stop_reply
74531fed 6561{
722247f1 6562 struct notif_event base;
74531fed 6563
722247f1 6564 /* The identifier of the thread about this event */
74531fed
PA
6565 ptid_t ptid;
6566
340e3c99 6567 /* The remote state this event is associated with. When the remote
bcc75809
YQ
6568 connection, represented by a remote_state object, is closed,
6569 all the associated stop_reply events should be released. */
6570 struct remote_state *rs;
6571
74531fed
PA
6572 struct target_waitstatus ws;
6573
5cd63fda
PA
6574 /* The architecture associated with the expedited registers. */
6575 gdbarch *arch;
6576
15148d6a
PA
6577 /* Expedited registers. This makes remote debugging a bit more
6578 efficient for those targets that provide critical registers as
6579 part of their normal status mechanism (as another roundtrip to
6580 fetch them is avoided). */
74531fed
PA
6581 VEC(cached_reg_t) *regcache;
6582
f7e6eed5
PA
6583 enum target_stop_reason stop_reason;
6584
74531fed
PA
6585 CORE_ADDR watch_data_address;
6586
dc146f7c 6587 int core;
722247f1 6588} *stop_reply_p;
a744cf53 6589
722247f1
YQ
6590DECLARE_QUEUE_P (stop_reply_p);
6591DEFINE_QUEUE_P (stop_reply_p);
6592/* The list of already fetched and acknowledged stop events. This
6593 queue is used for notification Stop, and other notifications
6594 don't need queue for their events, because the notification events
6595 of Stop can't be consumed immediately, so that events should be
6596 queued first, and be consumed by remote_wait_{ns,as} one per
6597 time. Other notifications can consume their events immediately,
6598 so queue is not needed for them. */
6599static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
6600
6601static void
6602stop_reply_xfree (struct stop_reply *r)
6603{
f48ff2a7 6604 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
6605}
6606
221e1a37
PA
6607/* Return the length of the stop reply queue. */
6608
6609static int
6610stop_reply_queue_length (void)
6611{
6612 return QUEUE_length (stop_reply_p, stop_reply_queue);
6613}
6614
722247f1
YQ
6615static void
6616remote_notif_stop_parse (struct notif_client *self, char *buf,
6617 struct notif_event *event)
6618{
6619 remote_parse_stop_reply (buf, (struct stop_reply *) event);
6620}
6621
6622static void
6623remote_notif_stop_ack (struct notif_client *self, char *buf,
6624 struct notif_event *event)
6625{
6626 struct stop_reply *stop_reply = (struct stop_reply *) event;
6627
6628 /* acknowledge */
f5c4fcd9 6629 putpkt (self->ack_command);
722247f1
YQ
6630
6631 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
6632 /* We got an unknown stop reply. */
6633 error (_("Unknown stop reply"));
6634
6635 push_stop_reply (stop_reply);
6636}
6637
6638static int
6639remote_notif_stop_can_get_pending_events (struct notif_client *self)
6640{
6641 /* We can't get pending events in remote_notif_process for
6642 notification stop, and we have to do this in remote_wait_ns
6643 instead. If we fetch all queued events from stub, remote stub
6644 may exit and we have no chance to process them back in
6645 remote_wait_ns. */
6646 mark_async_event_handler (remote_async_inferior_event_token);
6647 return 0;
6648}
6649
6650static void
6651stop_reply_dtr (struct notif_event *event)
6652{
6653 struct stop_reply *r = (struct stop_reply *) event;
d1dff226
AH
6654 cached_reg_t *reg;
6655 int ix;
6656
6657 for (ix = 0;
6658 VEC_iterate (cached_reg_t, r->regcache, ix, reg);
6659 ix++)
6660 xfree (reg->data);
722247f1
YQ
6661
6662 VEC_free (cached_reg_t, r->regcache);
6663}
6664
6665static struct notif_event *
6666remote_notif_stop_alloc_reply (void)
6667{
8d749320
SM
6668 /* We cast to a pointer to the "base class". */
6669 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
6670
6671 r->dtr = stop_reply_dtr;
6672
6673 return r;
6674}
6675
6676/* A client of notification Stop. */
6677
6678struct notif_client notif_client_stop =
6679{
6680 "Stop",
6681 "vStopped",
6682 remote_notif_stop_parse,
6683 remote_notif_stop_ack,
6684 remote_notif_stop_can_get_pending_events,
6685 remote_notif_stop_alloc_reply,
f48ff2a7 6686 REMOTE_NOTIF_STOP,
722247f1
YQ
6687};
6688
6689/* A parameter to pass data in and out. */
6690
6691struct queue_iter_param
6692{
6693 void *input;
6694 struct stop_reply *output;
6695};
6696
85ad3aaf 6697/* Determine if THREAD_PTID is a pending fork parent thread. ARG contains
cbb8991c
DB
6698 the pid of the process that owns the threads we want to check, or
6699 -1 if we want to check all threads. */
6700
6701static int
6702is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
6703 ptid_t thread_ptid)
6704{
6705 if (ws->kind == TARGET_WAITKIND_FORKED
6706 || ws->kind == TARGET_WAITKIND_VFORKED)
6707 {
6708 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
6709 return 1;
6710 }
6711
6712 return 0;
6713}
6714
85ad3aaf
PA
6715/* Return the thread's pending status used to determine whether the
6716 thread is a fork parent stopped at a fork event. */
6717
6718static struct target_waitstatus *
6719thread_pending_fork_status (struct thread_info *thread)
6720{
6721 if (thread->suspend.waitstatus_pending_p)
6722 return &thread->suspend.waitstatus;
6723 else
6724 return &thread->pending_follow;
6725}
6726
6727/* Determine if THREAD is a pending fork parent thread. */
6728
6729static int
6730is_pending_fork_parent_thread (struct thread_info *thread)
6731{
6732 struct target_waitstatus *ws = thread_pending_fork_status (thread);
6733 int pid = -1;
6734
6735 return is_pending_fork_parent (ws, pid, thread->ptid);
6736}
6737
cbb8991c
DB
6738/* Check whether EVENT is a fork event, and if it is, remove the
6739 fork child from the context list passed in DATA. */
6740
6741static int
6742remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
6743 QUEUE_ITER (stop_reply_p) *iter,
6744 stop_reply_p event,
6745 void *data)
6746{
19ba03f4
SM
6747 struct queue_iter_param *param = (struct queue_iter_param *) data;
6748 struct threads_listing_context *context
6749 = (struct threads_listing_context *) param->input;
cbb8991c
DB
6750
6751 if (event->ws.kind == TARGET_WAITKIND_FORKED
65706a29
PA
6752 || event->ws.kind == TARGET_WAITKIND_VFORKED
6753 || event->ws.kind == TARGET_WAITKIND_THREAD_EXITED)
21fe1c75 6754 context->remove_thread (event->ws.value.related_pid);
cbb8991c
DB
6755
6756 return 1;
6757}
6758
6759/* If CONTEXT contains any fork child threads that have not been
6760 reported yet, remove them from the CONTEXT list. If such a
6761 thread exists it is because we are stopped at a fork catchpoint
6762 and have not yet called follow_fork, which will set up the
6763 host-side data structures for the new process. */
6764
6765static void
6766remove_new_fork_children (struct threads_listing_context *context)
6767{
6768 struct thread_info * thread;
6769 int pid = -1;
6770 struct notif_client *notif = &notif_client_stop;
6771 struct queue_iter_param param;
6772
6773 /* For any threads stopped at a fork event, remove the corresponding
6774 fork child threads from the CONTEXT list. */
6775 ALL_NON_EXITED_THREADS (thread)
6776 {
85ad3aaf 6777 struct target_waitstatus *ws = thread_pending_fork_status (thread);
cbb8991c
DB
6778
6779 if (is_pending_fork_parent (ws, pid, thread->ptid))
21fe1c75 6780 context->remove_thread (ws->value.related_pid);
cbb8991c
DB
6781 }
6782
6783 /* Check for any pending fork events (not reported or processed yet)
6784 in process PID and remove those fork child threads from the
6785 CONTEXT list as well. */
6786 remote_notif_get_pending_events (notif);
6787 param.input = context;
6788 param.output = NULL;
6789 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6790 remove_child_of_pending_fork, &param);
6791}
6792
85ad3aaf
PA
6793/* Check whether EVENT would prevent a global or process wildcard
6794 vCont action. */
6795
6796static int
6797check_pending_event_prevents_wildcard_vcont_callback
6798 (QUEUE (stop_reply_p) *q,
6799 QUEUE_ITER (stop_reply_p) *iter,
6800 stop_reply_p event,
6801 void *data)
6802{
6803 struct inferior *inf;
6804 int *may_global_wildcard_vcont = (int *) data;
6805
6806 if (event->ws.kind == TARGET_WAITKIND_NO_RESUMED
6807 || event->ws.kind == TARGET_WAITKIND_NO_HISTORY)
6808 return 1;
6809
6810 if (event->ws.kind == TARGET_WAITKIND_FORKED
6811 || event->ws.kind == TARGET_WAITKIND_VFORKED)
6812 *may_global_wildcard_vcont = 0;
6813
6814 inf = find_inferior_ptid (event->ptid);
6815
6816 /* This may be the first time we heard about this process.
6817 Regardless, we must not do a global wildcard resume, otherwise
6818 we'd resume this process too. */
6819 *may_global_wildcard_vcont = 0;
6820 if (inf != NULL)
089354bb 6821 get_remote_inferior (inf)->may_wildcard_vcont = false;
85ad3aaf
PA
6822
6823 return 1;
6824}
6825
6826/* Check whether any event pending in the vStopped queue would prevent
6827 a global or process wildcard vCont action. Clear
6828 *may_global_wildcard if we can't do a global wildcard (vCont;c),
6829 and clear the event inferior's may_wildcard_vcont flag if we can't
6830 do a process-wide wildcard resume (vCont;c:pPID.-1). */
6831
6832static void
6833check_pending_events_prevent_wildcard_vcont (int *may_global_wildcard)
6834{
6835 struct notif_client *notif = &notif_client_stop;
6836
6837 remote_notif_get_pending_events (notif);
6838 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6839 check_pending_event_prevents_wildcard_vcont_callback,
6840 may_global_wildcard);
6841}
6842
f48ff2a7
YQ
6843/* Remove stop replies in the queue if its pid is equal to the given
6844 inferior's pid. */
722247f1
YQ
6845
6846static int
f48ff2a7
YQ
6847remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
6848 QUEUE_ITER (stop_reply_p) *iter,
6849 stop_reply_p event,
6850 void *data)
722247f1 6851{
19ba03f4
SM
6852 struct queue_iter_param *param = (struct queue_iter_param *) data;
6853 struct inferior *inf = (struct inferior *) param->input;
722247f1 6854
f48ff2a7 6855 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
6856 {
6857 stop_reply_xfree (event);
6858 QUEUE_remove_elem (stop_reply_p, q, iter);
6859 }
6860
6861 return 1;
6862}
6863
f48ff2a7 6864/* Discard all pending stop replies of inferior INF. */
c906108c 6865
74531fed 6866static void
5f4cf0bb 6867discard_pending_stop_replies (struct inferior *inf)
c906108c 6868{
722247f1 6869 struct queue_iter_param param;
f48ff2a7
YQ
6870 struct stop_reply *reply;
6871 struct remote_state *rs = get_remote_state ();
6872 struct remote_notif_state *rns = rs->notif_state;
6873
6874 /* This function can be notified when an inferior exists. When the
6875 target is not remote, the notification state is NULL. */
6876 if (rs->remote_desc == NULL)
6877 return;
6878
6879 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 6880
74531fed 6881 /* Discard the in-flight notification. */
f48ff2a7 6882 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 6883 {
722247f1 6884 stop_reply_xfree (reply);
f48ff2a7 6885 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 6886 }
c906108c 6887
722247f1
YQ
6888 param.input = inf;
6889 param.output = NULL;
74531fed
PA
6890 /* Discard the stop replies we have already pulled with
6891 vStopped. */
722247f1 6892 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
6893 remove_stop_reply_for_inferior, &param);
6894}
6895
bcc75809
YQ
6896/* If its remote state is equal to the given remote state,
6897 remove EVENT from the stop reply queue. */
6898
6899static int
6900remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
6901 QUEUE_ITER (stop_reply_p) *iter,
6902 stop_reply_p event,
6903 void *data)
6904{
19ba03f4
SM
6905 struct queue_iter_param *param = (struct queue_iter_param *) data;
6906 struct remote_state *rs = (struct remote_state *) param->input;
bcc75809
YQ
6907
6908 if (event->rs == rs)
6909 {
6910 stop_reply_xfree (event);
6911 QUEUE_remove_elem (stop_reply_p, q, iter);
6912 }
6913
6914 return 1;
6915}
6916
6917/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
6918
6919static void
bcc75809 6920discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
6921{
6922 struct queue_iter_param param;
6923
bcc75809 6924 param.input = rs;
f48ff2a7
YQ
6925 param.output = NULL;
6926 /* Discard the stop replies we have already pulled with
6927 vStopped. */
6928 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 6929 remove_stop_reply_of_remote_state, &param);
722247f1 6930}
74531fed 6931
722247f1
YQ
6932/* A parameter to pass data in and out. */
6933
6934static int
6935remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
6936 QUEUE_ITER (stop_reply_p) *iter,
6937 stop_reply_p event,
6938 void *data)
6939{
19ba03f4
SM
6940 struct queue_iter_param *param = (struct queue_iter_param *) data;
6941 ptid_t *ptid = (ptid_t *) param->input;
722247f1
YQ
6942
6943 if (ptid_match (event->ptid, *ptid))
6944 {
6945 param->output = event;
6946 QUEUE_remove_elem (stop_reply_p, q, iter);
6947 return 0;
c8e38a49 6948 }
722247f1
YQ
6949
6950 return 1;
74531fed 6951}
43ff13b4 6952
722247f1
YQ
6953/* Remove the first reply in 'stop_reply_queue' which matches
6954 PTID. */
2e9f7625 6955
722247f1
YQ
6956static struct stop_reply *
6957remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 6958{
722247f1
YQ
6959 struct queue_iter_param param;
6960
6961 param.input = &ptid;
6962 param.output = NULL;
6963
6964 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6965 remote_notif_remove_once_on_match, &param);
6966 if (notif_debug)
6967 fprintf_unfiltered (gdb_stdlog,
6968 "notif: discard queued event: 'Stop' in %s\n",
6969 target_pid_to_str (ptid));
a744cf53 6970
722247f1 6971 return param.output;
74531fed 6972}
75c99385 6973
74531fed
PA
6974/* Look for a queued stop reply belonging to PTID. If one is found,
6975 remove it from the queue, and return it. Returns NULL if none is
6976 found. If there are still queued events left to process, tell the
6977 event loop to get back to target_wait soon. */
e24a49d8 6978
74531fed
PA
6979static struct stop_reply *
6980queued_stop_reply (ptid_t ptid)
6981{
722247f1 6982 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 6983
722247f1 6984 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
6985 /* There's still at least an event left. */
6986 mark_async_event_handler (remote_async_inferior_event_token);
6987
722247f1 6988 return r;
74531fed
PA
6989}
6990
6991/* Push a fully parsed stop reply in the stop reply queue. Since we
6992 know that we now have at least one queued event left to pass to the
6993 core side, tell the event loop to get back to target_wait soon. */
6994
6995static void
6996push_stop_reply (struct stop_reply *new_event)
6997{
722247f1 6998 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 6999
722247f1
YQ
7000 if (notif_debug)
7001 fprintf_unfiltered (gdb_stdlog,
7002 "notif: push 'Stop' %s to queue %d\n",
7003 target_pid_to_str (new_event->ptid),
7004 QUEUE_length (stop_reply_p,
7005 stop_reply_queue));
74531fed
PA
7006
7007 mark_async_event_handler (remote_async_inferior_event_token);
7008}
7009
722247f1
YQ
7010static int
7011stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
7012 QUEUE_ITER (stop_reply_p) *iter,
7013 struct stop_reply *event,
7014 void *data)
7015{
19ba03f4 7016 ptid_t *ptid = (ptid_t *) data;
722247f1
YQ
7017
7018 return !(ptid_equal (*ptid, event->ptid)
7019 && event->ws.kind == TARGET_WAITKIND_STOPPED);
7020}
7021
74531fed
PA
7022/* Returns true if we have a stop reply for PTID. */
7023
7024static int
7025peek_stop_reply (ptid_t ptid)
7026{
722247f1
YQ
7027 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
7028 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
7029}
7030
26d56a93
SL
7031/* Helper for remote_parse_stop_reply. Return nonzero if the substring
7032 starting with P and ending with PEND matches PREFIX. */
7033
7034static int
7035strprefix (const char *p, const char *pend, const char *prefix)
7036{
7037 for ( ; p < pend; p++, prefix++)
7038 if (*p != *prefix)
7039 return 0;
7040 return *prefix == '\0';
7041}
7042
74531fed
PA
7043/* Parse the stop reply in BUF. Either the function succeeds, and the
7044 result is stored in EVENT, or throws an error. */
7045
7046static void
7047remote_parse_stop_reply (char *buf, struct stop_reply *event)
7048{
5cd63fda 7049 remote_arch_state *rsa = NULL;
74531fed 7050 ULONGEST addr;
256642e8 7051 const char *p;
94585166 7052 int skipregs = 0;
74531fed
PA
7053
7054 event->ptid = null_ptid;
bcc75809 7055 event->rs = get_remote_state ();
74531fed
PA
7056 event->ws.kind = TARGET_WAITKIND_IGNORE;
7057 event->ws.value.integer = 0;
f7e6eed5 7058 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 7059 event->regcache = NULL;
dc146f7c 7060 event->core = -1;
74531fed
PA
7061
7062 switch (buf[0])
7063 {
7064 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
7065 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7066 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7067 ss = signal number
7068 n... = register number
7069 r... = register contents
7070 */
7071
7072 p = &buf[3]; /* after Txx */
7073 while (*p)
7074 {
256642e8 7075 const char *p1;
cea39f65 7076 int fieldsize;
43ff13b4 7077
1f10ba14
PA
7078 p1 = strchr (p, ':');
7079 if (p1 == NULL)
7080 error (_("Malformed packet(a) (missing colon): %s\n\
7081Packet: '%s'\n"),
7082 p, buf);
7083 if (p == p1)
7084 error (_("Malformed packet(a) (missing register number): %s\n\
7085Packet: '%s'\n"),
7086 p, buf);
3c3bea1c 7087
1f10ba14
PA
7088 /* Some "registers" are actually extended stop information.
7089 Note if you're adding a new entry here: GDB 7.9 and
7090 earlier assume that all register "numbers" that start
7091 with an hex digit are real register numbers. Make sure
7092 the server only sends such a packet if it knows the
7093 client understands it. */
c8e38a49 7094
26d56a93 7095 if (strprefix (p, p1, "thread"))
1f10ba14 7096 event->ptid = read_ptid (++p1, &p);
82075af2
JS
7097 else if (strprefix (p, p1, "syscall_entry"))
7098 {
7099 ULONGEST sysno;
7100
7101 event->ws.kind = TARGET_WAITKIND_SYSCALL_ENTRY;
7102 p = unpack_varlen_hex (++p1, &sysno);
7103 event->ws.value.syscall_number = (int) sysno;
7104 }
7105 else if (strprefix (p, p1, "syscall_return"))
7106 {
7107 ULONGEST sysno;
7108
7109 event->ws.kind = TARGET_WAITKIND_SYSCALL_RETURN;
7110 p = unpack_varlen_hex (++p1, &sysno);
7111 event->ws.value.syscall_number = (int) sysno;
7112 }
26d56a93
SL
7113 else if (strprefix (p, p1, "watch")
7114 || strprefix (p, p1, "rwatch")
7115 || strprefix (p, p1, "awatch"))
cea39f65 7116 {
f7e6eed5 7117 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
7118 p = unpack_varlen_hex (++p1, &addr);
7119 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 7120 }
26d56a93 7121 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
7122 {
7123 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
7124
7125 /* Make sure the stub doesn't forget to indicate support
7126 with qSupported. */
7127 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
7128 error (_("Unexpected swbreak stop reason"));
7129
7130 /* The value part is documented as "must be empty",
7131 though we ignore it, in case we ever decide to make
7132 use of it in a backward compatible way. */
8424cc97 7133 p = strchrnul (p1 + 1, ';');
f7e6eed5 7134 }
26d56a93 7135 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
7136 {
7137 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
7138
7139 /* Make sure the stub doesn't forget to indicate support
7140 with qSupported. */
7141 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
7142 error (_("Unexpected hwbreak stop reason"));
7143
7144 /* See above. */
8424cc97 7145 p = strchrnul (p1 + 1, ';');
f7e6eed5 7146 }
26d56a93 7147 else if (strprefix (p, p1, "library"))
cea39f65 7148 {
1f10ba14 7149 event->ws.kind = TARGET_WAITKIND_LOADED;
8424cc97 7150 p = strchrnul (p1 + 1, ';');
1f10ba14 7151 }
26d56a93 7152 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
7153 {
7154 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
7155 /* p1 will indicate "begin" or "end", but it makes
7156 no difference for now, so ignore it. */
8424cc97 7157 p = strchrnul (p1 + 1, ';');
1f10ba14 7158 }
26d56a93 7159 else if (strprefix (p, p1, "core"))
1f10ba14
PA
7160 {
7161 ULONGEST c;
a744cf53 7162
1f10ba14
PA
7163 p = unpack_varlen_hex (++p1, &c);
7164 event->core = c;
cea39f65 7165 }
26d56a93 7166 else if (strprefix (p, p1, "fork"))
de0d863e
DB
7167 {
7168 event->ws.value.related_pid = read_ptid (++p1, &p);
7169 event->ws.kind = TARGET_WAITKIND_FORKED;
7170 }
26d56a93 7171 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
7172 {
7173 event->ws.value.related_pid = read_ptid (++p1, &p);
7174 event->ws.kind = TARGET_WAITKIND_VFORKED;
7175 }
26d56a93 7176 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
7177 {
7178 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
8424cc97 7179 p = strchrnul (p1 + 1, ';');
c269dbdb 7180 }
6ab24463 7181 else if (strprefix (p, p1, "exec"))
94585166
DB
7182 {
7183 ULONGEST ignored;
7184 char pathname[PATH_MAX];
7185 int pathlen;
7186
7187 /* Determine the length of the execd pathname. */
7188 p = unpack_varlen_hex (++p1, &ignored);
7189 pathlen = (p - p1) / 2;
7190
7191 /* Save the pathname for event reporting and for
7192 the next run command. */
7193 hex2bin (p1, (gdb_byte *) pathname, pathlen);
7194 pathname[pathlen] = '\0';
7195
7196 /* This is freed during event handling. */
7197 event->ws.value.execd_pathname = xstrdup (pathname);
7198 event->ws.kind = TARGET_WAITKIND_EXECD;
7199
7200 /* Skip the registers included in this packet, since
7201 they may be for an architecture different from the
7202 one used by the original program. */
7203 skipregs = 1;
7204 }
65706a29
PA
7205 else if (strprefix (p, p1, "create"))
7206 {
7207 event->ws.kind = TARGET_WAITKIND_THREAD_CREATED;
8424cc97 7208 p = strchrnul (p1 + 1, ';');
65706a29 7209 }
cea39f65
MS
7210 else
7211 {
1f10ba14 7212 ULONGEST pnum;
256642e8 7213 const char *p_temp;
1f10ba14 7214
94585166
DB
7215 if (skipregs)
7216 {
8424cc97 7217 p = strchrnul (p1 + 1, ';');
94585166
DB
7218 p++;
7219 continue;
7220 }
7221
1f10ba14
PA
7222 /* Maybe a real ``P'' register number. */
7223 p_temp = unpack_varlen_hex (p, &pnum);
7224 /* If the first invalid character is the colon, we got a
7225 register number. Otherwise, it's an unknown stop
7226 reason. */
7227 if (p_temp == p1)
7228 {
5cd63fda
PA
7229 /* If we haven't parsed the event's thread yet, find
7230 it now, in order to find the architecture of the
7231 reported expedited registers. */
7232 if (event->ptid == null_ptid)
7233 {
7234 const char *thr = strstr (p1 + 1, ";thread:");
7235 if (thr != NULL)
7236 event->ptid = read_ptid (thr + strlen (";thread:"),
7237 NULL);
7238 else
3cada740
PA
7239 {
7240 /* Either the current thread hasn't changed,
7241 or the inferior is not multi-threaded.
7242 The event must be for the thread we last
7243 set as (or learned as being) current. */
7244 event->ptid = event->rs->general_thread;
7245 }
5cd63fda
PA
7246 }
7247
7248 if (rsa == NULL)
7249 {
7250 inferior *inf = (event->ptid == null_ptid
7251 ? NULL
7252 : find_inferior_ptid (event->ptid));
7253 /* If this is the first time we learn anything
7254 about this process, skip the registers
7255 included in this packet, since we don't yet
7256 know which architecture to use to parse them.
7257 We'll determine the architecture later when
7258 we process the stop reply and retrieve the
7259 target description, via
7260 remote_notice_new_inferior ->
7261 post_create_inferior. */
7262 if (inf == NULL)
7263 {
7264 p = strchrnul (p1 + 1, ';');
7265 p++;
7266 continue;
7267 }
7268
7269 event->arch = inf->gdbarch;
9d6eea31 7270 rsa = event->rs->get_remote_arch_state (event->arch);
5cd63fda
PA
7271 }
7272
7273 packet_reg *reg
7274 = packet_reg_from_pnum (event->arch, rsa, pnum);
1f10ba14 7275 cached_reg_t cached_reg;
43ff13b4 7276
1f10ba14
PA
7277 if (reg == NULL)
7278 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 7279Packet: '%s'\n"),
1f10ba14 7280 hex_string (pnum), p, buf);
c8e38a49 7281
1f10ba14 7282 cached_reg.num = reg->regnum;
d1dff226 7283 cached_reg.data = (gdb_byte *)
5cd63fda 7284 xmalloc (register_size (event->arch, reg->regnum));
4100683b 7285
1f10ba14
PA
7286 p = p1 + 1;
7287 fieldsize = hex2bin (p, cached_reg.data,
5cd63fda 7288 register_size (event->arch, reg->regnum));
1f10ba14 7289 p += 2 * fieldsize;
5cd63fda 7290 if (fieldsize < register_size (event->arch, reg->regnum))
1f10ba14 7291 warning (_("Remote reply is too short: %s"), buf);
74531fed 7292
1f10ba14
PA
7293 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
7294 }
7295 else
7296 {
7297 /* Not a number. Silently skip unknown optional
7298 info. */
8424cc97 7299 p = strchrnul (p1 + 1, ';');
1f10ba14 7300 }
cea39f65 7301 }
c8e38a49 7302
cea39f65
MS
7303 if (*p != ';')
7304 error (_("Remote register badly formatted: %s\nhere: %s"),
7305 buf, p);
7306 ++p;
7307 }
5b5596ff
PA
7308
7309 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
7310 break;
7311
c8e38a49
PA
7312 /* fall through */
7313 case 'S': /* Old style status, just signal only. */
3a09da41
PA
7314 {
7315 int sig;
7316
7317 event->ws.kind = TARGET_WAITKIND_STOPPED;
7318 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
7319 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
7320 event->ws.value.sig = (enum gdb_signal) sig;
7321 else
7322 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
7323 }
c8e38a49 7324 break;
65706a29
PA
7325 case 'w': /* Thread exited. */
7326 {
256642e8 7327 const char *p;
65706a29
PA
7328 ULONGEST value;
7329
7330 event->ws.kind = TARGET_WAITKIND_THREAD_EXITED;
7331 p = unpack_varlen_hex (&buf[1], &value);
7332 event->ws.value.integer = value;
7333 if (*p != ';')
7334 error (_("stop reply packet badly formatted: %s"), buf);
974eac9d 7335 event->ptid = read_ptid (++p, NULL);
65706a29
PA
7336 break;
7337 }
c8e38a49
PA
7338 case 'W': /* Target exited. */
7339 case 'X':
7340 {
256642e8 7341 const char *p;
c8e38a49
PA
7342 int pid;
7343 ULONGEST value;
82f73884 7344
c8e38a49
PA
7345 /* GDB used to accept only 2 hex chars here. Stubs should
7346 only send more if they detect GDB supports multi-process
7347 support. */
7348 p = unpack_varlen_hex (&buf[1], &value);
82f73884 7349
c8e38a49
PA
7350 if (buf[0] == 'W')
7351 {
7352 /* The remote process exited. */
74531fed
PA
7353 event->ws.kind = TARGET_WAITKIND_EXITED;
7354 event->ws.value.integer = value;
c8e38a49
PA
7355 }
7356 else
7357 {
7358 /* The remote process exited with a signal. */
74531fed 7359 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
7360 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
7361 event->ws.value.sig = (enum gdb_signal) value;
7362 else
7363 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 7364 }
82f73884 7365
c8e38a49
PA
7366 /* If no process is specified, assume inferior_ptid. */
7367 pid = ptid_get_pid (inferior_ptid);
7368 if (*p == '\0')
7369 ;
7370 else if (*p == ';')
7371 {
7372 p++;
7373
0b24eb2d 7374 if (*p == '\0')
82f73884 7375 ;
61012eef 7376 else if (startswith (p, "process:"))
82f73884 7377 {
c8e38a49 7378 ULONGEST upid;
a744cf53 7379
c8e38a49
PA
7380 p += sizeof ("process:") - 1;
7381 unpack_varlen_hex (p, &upid);
7382 pid = upid;
82f73884
PA
7383 }
7384 else
7385 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 7386 }
c8e38a49
PA
7387 else
7388 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
7389 event->ptid = pid_to_ptid (pid);
7390 }
7391 break;
f2faf941
PA
7392 case 'N':
7393 event->ws.kind = TARGET_WAITKIND_NO_RESUMED;
7394 event->ptid = minus_one_ptid;
7395 break;
74531fed
PA
7396 }
7397
6efcd9a8 7398 if (target_is_non_stop_p () && ptid_equal (event->ptid, null_ptid))
74531fed
PA
7399 error (_("No process or thread specified in stop reply: %s"), buf);
7400}
7401
722247f1
YQ
7402/* When the stub wants to tell GDB about a new notification reply, it
7403 sends a notification (%Stop, for example). Those can come it at
7404 any time, hence, we have to make sure that any pending
7405 putpkt/getpkt sequence we're making is finished, before querying
7406 the stub for more events with the corresponding ack command
7407 (vStopped, for example). E.g., if we started a vStopped sequence
7408 immediately upon receiving the notification, something like this
7409 could happen:
74531fed
PA
7410
7411 1.1) --> Hg 1
7412 1.2) <-- OK
7413 1.3) --> g
7414 1.4) <-- %Stop
7415 1.5) --> vStopped
7416 1.6) <-- (registers reply to step #1.3)
7417
7418 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7419 query.
7420
796cb314 7421 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
7422 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7423 doing whatever we were doing:
7424
7425 2.1) --> Hg 1
7426 2.2) <-- OK
7427 2.3) --> g
7428 2.4) <-- %Stop
7429 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7430 2.5) <-- (registers reply to step #2.3)
7431
7432 Eventualy after step #2.5, we return to the event loop, which
7433 notices there's an event on the
7434 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7435 associated callback --- the function below. At this point, we're
7436 always safe to start a vStopped sequence. :
7437
7438 2.6) --> vStopped
7439 2.7) <-- T05 thread:2
7440 2.8) --> vStopped
7441 2.9) --> OK
7442*/
7443
722247f1
YQ
7444void
7445remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
7446{
7447 struct remote_state *rs = get_remote_state ();
74531fed 7448
f48ff2a7 7449 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 7450 {
722247f1
YQ
7451 if (notif_debug)
7452 fprintf_unfiltered (gdb_stdlog,
7453 "notif: process: '%s' ack pending event\n",
7454 nc->name);
74531fed 7455
722247f1 7456 /* acknowledge */
f48ff2a7
YQ
7457 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
7458 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
7459
7460 while (1)
7461 {
7462 getpkt (&rs->buf, &rs->buf_size, 0);
7463 if (strcmp (rs->buf, "OK") == 0)
7464 break;
7465 else
722247f1 7466 remote_notif_ack (nc, rs->buf);
74531fed
PA
7467 }
7468 }
722247f1
YQ
7469 else
7470 {
7471 if (notif_debug)
7472 fprintf_unfiltered (gdb_stdlog,
7473 "notif: process: '%s' no pending reply\n",
7474 nc->name);
7475 }
74531fed
PA
7476}
7477
74531fed
PA
7478/* Called when it is decided that STOP_REPLY holds the info of the
7479 event that is to be returned to the core. This function always
7480 destroys STOP_REPLY. */
7481
7482static ptid_t
7483process_stop_reply (struct stop_reply *stop_reply,
7484 struct target_waitstatus *status)
7485{
7486 ptid_t ptid;
7487
7488 *status = stop_reply->ws;
7489 ptid = stop_reply->ptid;
7490
7491 /* If no thread/process was reported by the stub, assume the current
7492 inferior. */
7493 if (ptid_equal (ptid, null_ptid))
7494 ptid = inferior_ptid;
7495
5f3563ea 7496 if (status->kind != TARGET_WAITKIND_EXITED
f2faf941
PA
7497 && status->kind != TARGET_WAITKIND_SIGNALLED
7498 && status->kind != TARGET_WAITKIND_NO_RESUMED)
74531fed 7499 {
5f3563ea
PA
7500 /* Expedited registers. */
7501 if (stop_reply->regcache)
7502 {
217f1f79 7503 struct regcache *regcache
5cd63fda 7504 = get_thread_arch_regcache (ptid, stop_reply->arch);
5f3563ea
PA
7505 cached_reg_t *reg;
7506 int ix;
7507
7508 for (ix = 0;
d1dff226 7509 VEC_iterate (cached_reg_t, stop_reply->regcache, ix, reg);
5f3563ea 7510 ix++)
d1dff226 7511 {
217f1f79 7512 regcache_raw_supply (regcache, reg->num, reg->data);
d1dff226
AH
7513 xfree (reg->data);
7514 }
7515
5f3563ea
PA
7516 VEC_free (cached_reg_t, stop_reply->regcache);
7517 }
74531fed 7518
1941c569 7519 remote_notice_new_inferior (ptid, 0);
7aabaf9d 7520 remote_thread_info *remote_thr = get_remote_thread_info (ptid);
799a2abe
PA
7521 remote_thr->core = stop_reply->core;
7522 remote_thr->stop_reason = stop_reply->stop_reason;
7523 remote_thr->watch_data_address = stop_reply->watch_data_address;
85ad3aaf 7524 remote_thr->vcont_resumed = 0;
74531fed
PA
7525 }
7526
74531fed
PA
7527 stop_reply_xfree (stop_reply);
7528 return ptid;
7529}
7530
7531/* The non-stop mode version of target_wait. */
7532
7533static ptid_t
47608cb1 7534remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7535{
7536 struct remote_state *rs = get_remote_state ();
74531fed
PA
7537 struct stop_reply *stop_reply;
7538 int ret;
fee9eda9 7539 int is_notif = 0;
74531fed
PA
7540
7541 /* If in non-stop mode, get out of getpkt even if a
7542 notification is received. */
7543
7544 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7545 0 /* forever */, &is_notif);
74531fed
PA
7546 while (1)
7547 {
fee9eda9 7548 if (ret != -1 && !is_notif)
74531fed
PA
7549 switch (rs->buf[0])
7550 {
7551 case 'E': /* Error of some sort. */
7552 /* We're out of sync with the target now. Did it continue
7553 or not? We can't tell which thread it was in non-stop,
7554 so just ignore this. */
7555 warning (_("Remote failure reply: %s"), rs->buf);
7556 break;
7557 case 'O': /* Console output. */
7558 remote_console_output (rs->buf + 1);
7559 break;
7560 default:
7561 warning (_("Invalid remote reply: %s"), rs->buf);
7562 break;
7563 }
7564
7565 /* Acknowledge a pending stop reply that may have arrived in the
7566 mean time. */
f48ff2a7 7567 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 7568 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
7569
7570 /* If indeed we noticed a stop reply, we're done. */
7571 stop_reply = queued_stop_reply (ptid);
7572 if (stop_reply != NULL)
7573 return process_stop_reply (stop_reply, status);
7574
47608cb1 7575 /* Still no event. If we're just polling for an event, then
74531fed 7576 return to the event loop. */
47608cb1 7577 if (options & TARGET_WNOHANG)
74531fed
PA
7578 {
7579 status->kind = TARGET_WAITKIND_IGNORE;
7580 return minus_one_ptid;
7581 }
7582
47608cb1 7583 /* Otherwise do a blocking wait. */
74531fed 7584 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7585 1 /* forever */, &is_notif);
74531fed
PA
7586 }
7587}
7588
7589/* Wait until the remote machine stops, then return, storing status in
7590 STATUS just as `wait' would. */
7591
7592static ptid_t
47608cb1 7593remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7594{
7595 struct remote_state *rs = get_remote_state ();
74531fed 7596 ptid_t event_ptid = null_ptid;
cea39f65 7597 char *buf;
74531fed
PA
7598 struct stop_reply *stop_reply;
7599
47608cb1
PA
7600 again:
7601
74531fed
PA
7602 status->kind = TARGET_WAITKIND_IGNORE;
7603 status->value.integer = 0;
7604
7605 stop_reply = queued_stop_reply (ptid);
7606 if (stop_reply != NULL)
7607 return process_stop_reply (stop_reply, status);
7608
7609 if (rs->cached_wait_status)
7610 /* Use the cached wait status, but only once. */
7611 rs->cached_wait_status = 0;
7612 else
7613 {
7614 int ret;
722247f1 7615 int is_notif;
567420d1
PA
7616 int forever = ((options & TARGET_WNOHANG) == 0
7617 && wait_forever_enabled_p);
7618
7619 if (!rs->waiting_for_stop_reply)
7620 {
7621 status->kind = TARGET_WAITKIND_NO_RESUMED;
7622 return minus_one_ptid;
7623 }
74531fed 7624
74531fed
PA
7625 /* FIXME: cagney/1999-09-27: If we're in async mode we should
7626 _never_ wait for ever -> test on target_is_async_p().
7627 However, before we do that we need to ensure that the caller
7628 knows how to take the target into/out of async mode. */
722247f1 7629 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 7630 forever, &is_notif);
722247f1
YQ
7631
7632 /* GDB gets a notification. Return to core as this event is
7633 not interesting. */
7634 if (ret != -1 && is_notif)
7635 return minus_one_ptid;
567420d1
PA
7636
7637 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
7638 return minus_one_ptid;
74531fed
PA
7639 }
7640
7641 buf = rs->buf;
7642
3a29589a
DJ
7643 /* Assume that the target has acknowledged Ctrl-C unless we receive
7644 an 'F' or 'O' packet. */
7645 if (buf[0] != 'F' && buf[0] != 'O')
7646 rs->ctrlc_pending_p = 0;
7647
74531fed
PA
7648 switch (buf[0])
7649 {
7650 case 'E': /* Error of some sort. */
7651 /* We're out of sync with the target now. Did it continue or
7652 not? Not is more likely, so report a stop. */
29090fb6
LM
7653 rs->waiting_for_stop_reply = 0;
7654
74531fed
PA
7655 warning (_("Remote failure reply: %s"), buf);
7656 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 7657 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
7658 break;
7659 case 'F': /* File-I/O request. */
e42e5352
YQ
7660 /* GDB may access the inferior memory while handling the File-I/O
7661 request, but we don't want GDB accessing memory while waiting
7662 for a stop reply. See the comments in putpkt_binary. Set
7663 waiting_for_stop_reply to 0 temporarily. */
7664 rs->waiting_for_stop_reply = 0;
3a29589a
DJ
7665 remote_fileio_request (buf, rs->ctrlc_pending_p);
7666 rs->ctrlc_pending_p = 0;
e42e5352
YQ
7667 /* GDB handled the File-I/O request, and the target is running
7668 again. Keep waiting for events. */
7669 rs->waiting_for_stop_reply = 1;
74531fed 7670 break;
f2faf941 7671 case 'N': case 'T': case 'S': case 'X': case 'W':
74531fed 7672 {
29090fb6
LM
7673 struct stop_reply *stop_reply;
7674
7675 /* There is a stop reply to handle. */
7676 rs->waiting_for_stop_reply = 0;
7677
7678 stop_reply
722247f1
YQ
7679 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
7680 rs->buf);
74531fed 7681
74531fed 7682 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
7683 break;
7684 }
7685 case 'O': /* Console output. */
7686 remote_console_output (buf + 1);
c8e38a49
PA
7687 break;
7688 case '\0':
b73be471 7689 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
7690 {
7691 /* Zero length reply means that we tried 'S' or 'C' and the
7692 remote system doesn't support it. */
223ffa71 7693 target_terminal::ours_for_output ();
c8e38a49
PA
7694 printf_filtered
7695 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
7696 gdb_signal_to_name (rs->last_sent_signal));
7697 rs->last_sent_signal = GDB_SIGNAL_0;
223ffa71 7698 target_terminal::inferior ();
c8e38a49 7699
f5c4fcd9
TT
7700 strcpy (buf, rs->last_sent_step ? "s" : "c");
7701 putpkt (buf);
c8e38a49 7702 break;
43ff13b4 7703 }
86a73007 7704 /* fallthrough */
c8e38a49
PA
7705 default:
7706 warning (_("Invalid remote reply: %s"), buf);
c8e38a49 7707 break;
43ff13b4 7708 }
c8e38a49 7709
f2faf941
PA
7710 if (status->kind == TARGET_WAITKIND_NO_RESUMED)
7711 return minus_one_ptid;
7712 else if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
7713 {
7714 /* Nothing interesting happened. If we're doing a non-blocking
7715 poll, we're done. Otherwise, go back to waiting. */
7716 if (options & TARGET_WNOHANG)
7717 return minus_one_ptid;
7718 else
7719 goto again;
7720 }
74531fed
PA
7721 else if (status->kind != TARGET_WAITKIND_EXITED
7722 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
7723 {
7724 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 7725 record_currthread (rs, event_ptid);
82f73884
PA
7726 else
7727 event_ptid = inferior_ptid;
43ff13b4 7728 }
74531fed
PA
7729 else
7730 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 7731 record_currthread (rs, minus_one_ptid);
79d7f229 7732
82f73884 7733 return event_ptid;
43ff13b4
JM
7734}
7735
74531fed
PA
7736/* Wait until the remote machine stops, then return, storing status in
7737 STATUS just as `wait' would. */
7738
f6ac5f3d
PA
7739ptid_t
7740remote_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
7741{
7742 ptid_t event_ptid;
7743
6efcd9a8 7744 if (target_is_non_stop_p ())
47608cb1 7745 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 7746 else
47608cb1 7747 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 7748
d9d41e78 7749 if (target_is_async_p ())
c8e38a49 7750 {
74531fed
PA
7751 /* If there are are events left in the queue tell the event loop
7752 to return here. */
722247f1 7753 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 7754 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 7755 }
c8e38a49
PA
7756
7757 return event_ptid;
7758}
7759
74ca34ce 7760/* Fetch a single register using a 'p' packet. */
c906108c 7761
b96ec7ac 7762static int
56be3814 7763fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac 7764{
ac7936df 7765 struct gdbarch *gdbarch = regcache->arch ();
b96ec7ac 7766 struct remote_state *rs = get_remote_state ();
2e9f7625 7767 char *buf, *p;
9890e433 7768 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
b96ec7ac
AC
7769 int i;
7770
4082afcc 7771 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
7772 return 0;
7773
7774 if (reg->pnum == -1)
7775 return 0;
7776
2e9f7625 7777 p = rs->buf;
fcad0fa4 7778 *p++ = 'p';
74ca34ce 7779 p += hexnumstr (p, reg->pnum);
fcad0fa4 7780 *p++ = '\0';
1f4437a4
MS
7781 putpkt (rs->buf);
7782 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 7783
2e9f7625
DJ
7784 buf = rs->buf;
7785
74ca34ce
DJ
7786 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
7787 {
7788 case PACKET_OK:
7789 break;
7790 case PACKET_UNKNOWN:
7791 return 0;
7792 case PACKET_ERROR:
27a9c0bf 7793 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
ac7936df 7794 gdbarch_register_name (regcache->arch (),
27a9c0bf
MS
7795 reg->regnum),
7796 buf);
74ca34ce 7797 }
3f9a994c
JB
7798
7799 /* If this register is unfetchable, tell the regcache. */
7800 if (buf[0] == 'x')
8480adf2 7801 {
56be3814 7802 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 7803 return 1;
b96ec7ac 7804 }
b96ec7ac 7805
3f9a994c
JB
7806 /* Otherwise, parse and supply the value. */
7807 p = buf;
7808 i = 0;
7809 while (p[0] != 0)
7810 {
7811 if (p[1] == 0)
74ca34ce 7812 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
7813
7814 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
7815 p += 2;
7816 }
56be3814 7817 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 7818 return 1;
b96ec7ac
AC
7819}
7820
74ca34ce
DJ
7821/* Fetch the registers included in the target's 'g' packet. */
7822
29709017
DJ
7823static int
7824send_g_packet (void)
c906108c 7825{
d01949b6 7826 struct remote_state *rs = get_remote_state ();
cea39f65 7827 int buf_len;
c906108c 7828
bba74b36 7829 xsnprintf (rs->buf, get_remote_packet_size (), "g");
b75abf5b
AK
7830 putpkt (rs->buf);
7831 getpkt (&rs->buf, &rs->buf_size, 0);
7832 if (packet_check_result (rs->buf) == PACKET_ERROR)
7833 error (_("Could not read registers; remote failure reply '%s'"),
7834 rs->buf);
c906108c 7835
29709017
DJ
7836 /* We can get out of synch in various cases. If the first character
7837 in the buffer is not a hex character, assume that has happened
7838 and try to fetch another packet to read. */
7839 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
7840 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
7841 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
7842 && rs->buf[0] != 'x') /* New: unavailable register value. */
7843 {
7844 if (remote_debug)
7845 fprintf_unfiltered (gdb_stdlog,
7846 "Bad register packet; fetching a new packet\n");
7847 getpkt (&rs->buf, &rs->buf_size, 0);
7848 }
7849
74ca34ce
DJ
7850 buf_len = strlen (rs->buf);
7851
7852 /* Sanity check the received packet. */
7853 if (buf_len % 2 != 0)
7854 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
7855
7856 return buf_len / 2;
7857}
7858
7859static void
56be3814 7860process_g_packet (struct regcache *regcache)
29709017 7861{
ac7936df 7862 struct gdbarch *gdbarch = regcache->arch ();
29709017 7863 struct remote_state *rs = get_remote_state ();
9d6eea31 7864 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
29709017
DJ
7865 int i, buf_len;
7866 char *p;
7867 char *regs;
7868
7869 buf_len = strlen (rs->buf);
7870
7871 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce 7872 if (buf_len > 2 * rsa->sizeof_g_packet)
fc809827
SM
7873 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
7874 "bytes): %s"), rsa->sizeof_g_packet, buf_len / 2, rs->buf);
74ca34ce
DJ
7875
7876 /* Save the size of the packet sent to us by the target. It is used
7877 as a heuristic when determining the max size of packets that the
7878 target can safely receive. */
7879 if (rsa->actual_register_packet_size == 0)
7880 rsa->actual_register_packet_size = buf_len;
7881
7882 /* If this is smaller than we guessed the 'g' packet would be,
7883 update our records. A 'g' reply that doesn't include a register's
7884 value implies either that the register is not available, or that
7885 the 'p' packet must be used. */
7886 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 7887 {
9dc193c3 7888 long sizeof_g_packet = buf_len / 2;
74ca34ce 7889
4a22f64d 7890 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 7891 {
9dc193c3
LF
7892 long offset = rsa->regs[i].offset;
7893 long reg_size = register_size (gdbarch, i);
7894
74ca34ce
DJ
7895 if (rsa->regs[i].pnum == -1)
7896 continue;
7897
9dc193c3 7898 if (offset >= sizeof_g_packet)
74ca34ce 7899 rsa->regs[i].in_g_packet = 0;
9dc193c3
LF
7900 else if (offset + reg_size > sizeof_g_packet)
7901 error (_("Truncated register %d in remote 'g' packet"), i);
b96ec7ac 7902 else
74ca34ce 7903 rsa->regs[i].in_g_packet = 1;
b96ec7ac 7904 }
9dc193c3
LF
7905
7906 /* Looks valid enough, we can assume this is the correct length
7907 for a 'g' packet. It's important not to adjust
7908 rsa->sizeof_g_packet if we have truncated registers otherwise
7909 this "if" won't be run the next time the method is called
7910 with a packet of the same size and one of the internal errors
7911 below will trigger instead. */
7912 rsa->sizeof_g_packet = sizeof_g_packet;
74ca34ce 7913 }
b323314b 7914
224c3ddb 7915 regs = (char *) alloca (rsa->sizeof_g_packet);
c906108c
SS
7916
7917 /* Unimplemented registers read as all bits zero. */
ea9c271d 7918 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 7919
c906108c
SS
7920 /* Reply describes registers byte by byte, each byte encoded as two
7921 hex characters. Suck them all up, then supply them to the
7922 register cacheing/storage mechanism. */
7923
74ca34ce 7924 p = rs->buf;
ea9c271d 7925 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 7926 {
74ca34ce
DJ
7927 if (p[0] == 0 || p[1] == 0)
7928 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
7929 internal_error (__FILE__, __LINE__,
9b20d036 7930 _("unexpected end of 'g' packet reply"));
74ca34ce 7931
c906108c 7932 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 7933 regs[i] = 0; /* 'x' */
c906108c
SS
7934 else
7935 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
7936 p += 2;
7937 }
7938
a744cf53
MS
7939 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
7940 {
7941 struct packet_reg *r = &rsa->regs[i];
9dc193c3 7942 long reg_size = register_size (gdbarch, i);
a744cf53
MS
7943
7944 if (r->in_g_packet)
7945 {
9dc193c3 7946 if ((r->offset + reg_size) * 2 > strlen (rs->buf))
a744cf53
MS
7947 /* This shouldn't happen - we adjusted in_g_packet above. */
7948 internal_error (__FILE__, __LINE__,
9b20d036 7949 _("unexpected end of 'g' packet reply"));
a744cf53
MS
7950 else if (rs->buf[r->offset * 2] == 'x')
7951 {
7952 gdb_assert (r->offset * 2 < strlen (rs->buf));
7953 /* The register isn't available, mark it as such (at
7954 the same time setting the value to zero). */
7955 regcache_raw_supply (regcache, r->regnum, NULL);
7956 }
7957 else
7958 regcache_raw_supply (regcache, r->regnum,
7959 regs + r->offset);
7960 }
7961 }
c906108c
SS
7962}
7963
29709017 7964static void
56be3814 7965fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
7966{
7967 send_g_packet ();
56be3814 7968 process_g_packet (regcache);
29709017
DJ
7969}
7970
e6e4e701
PA
7971/* Make the remote selected traceframe match GDB's selected
7972 traceframe. */
7973
7974static void
7975set_remote_traceframe (void)
7976{
7977 int newnum;
262e1174 7978 struct remote_state *rs = get_remote_state ();
e6e4e701 7979
262e1174 7980 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
7981 return;
7982
7983 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 7984 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
7985
7986 newnum = target_trace_find (tfind_number,
7987 get_traceframe_number (), 0, 0, NULL);
7988
7989 /* Should not happen. If it does, all bets are off. */
7990 if (newnum != get_traceframe_number ())
7991 warning (_("could not set remote traceframe"));
7992}
7993
f6ac5f3d
PA
7994void
7995remote_target::fetch_registers (struct regcache *regcache, int regnum)
74ca34ce 7996{
ac7936df 7997 struct gdbarch *gdbarch = regcache->arch ();
9d6eea31
PA
7998 struct remote_state *rs = get_remote_state ();
7999 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
74ca34ce
DJ
8000 int i;
8001
e6e4e701 8002 set_remote_traceframe ();
bcc0c096 8003 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
8004
8005 if (regnum >= 0)
8006 {
5cd63fda 8007 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 8008
74ca34ce
DJ
8009 gdb_assert (reg != NULL);
8010
8011 /* If this register might be in the 'g' packet, try that first -
8012 we are likely to read more than one register. If this is the
8013 first 'g' packet, we might be overly optimistic about its
8014 contents, so fall back to 'p'. */
8015 if (reg->in_g_packet)
8016 {
56be3814 8017 fetch_registers_using_g (regcache);
74ca34ce
DJ
8018 if (reg->in_g_packet)
8019 return;
8020 }
8021
56be3814 8022 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
8023 return;
8024
8025 /* This register is not available. */
56be3814 8026 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
8027
8028 return;
8029 }
8030
56be3814 8031 fetch_registers_using_g (regcache);
74ca34ce 8032
5cd63fda 8033 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 8034 if (!rsa->regs[i].in_g_packet)
56be3814 8035 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
8036 {
8037 /* This register is not available. */
56be3814 8038 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
8039 }
8040}
8041
c906108c
SS
8042/* Prepare to store registers. Since we may send them all (using a
8043 'G' request), we have to read out the ones we don't want to change
8044 first. */
8045
f6ac5f3d
PA
8046void
8047remote_target::prepare_to_store (struct regcache *regcache)
c906108c 8048{
9d6eea31
PA
8049 struct remote_state *rs = get_remote_state ();
8050 remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
cf0e1e0d 8051 int i;
cf0e1e0d 8052
c906108c 8053 /* Make sure the entire registers array is valid. */
4082afcc 8054 switch (packet_support (PACKET_P))
5a2468f5
JM
8055 {
8056 case PACKET_DISABLE:
8057 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 8058 /* Make sure all the necessary registers are cached. */
ac7936df 8059 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
ea9c271d 8060 if (rsa->regs[i].in_g_packet)
8e368124 8061 regcache_raw_update (regcache, rsa->regs[i].regnum);
5a2468f5
JM
8062 break;
8063 case PACKET_ENABLE:
8064 break;
8065 }
8066}
8067
ad10f812 8068/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 8069 packet was not recognized. */
5a2468f5
JM
8070
8071static int
1f4437a4
MS
8072store_register_using_P (const struct regcache *regcache,
8073 struct packet_reg *reg)
5a2468f5 8074{
ac7936df 8075 struct gdbarch *gdbarch = regcache->arch ();
d01949b6 8076 struct remote_state *rs = get_remote_state ();
5a2468f5 8077 /* Try storing a single register. */
6d820c5c 8078 char *buf = rs->buf;
9890e433 8079 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
5a2468f5 8080 char *p;
5a2468f5 8081
4082afcc 8082 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
8083 return 0;
8084
8085 if (reg->pnum == -1)
8086 return 0;
8087
ea9c271d 8088 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 8089 p = buf + strlen (buf);
56be3814 8090 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 8091 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
8092 putpkt (rs->buf);
8093 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 8094
74ca34ce
DJ
8095 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
8096 {
8097 case PACKET_OK:
8098 return 1;
8099 case PACKET_ERROR:
27a9c0bf
MS
8100 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8101 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
8102 case PACKET_UNKNOWN:
8103 return 0;
8104 default:
8105 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8106 }
c906108c
SS
8107}
8108
23860348
MS
8109/* Store register REGNUM, or all registers if REGNUM == -1, from the
8110 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
8111
8112static void
56be3814 8113store_registers_using_G (const struct regcache *regcache)
c906108c 8114{
d01949b6 8115 struct remote_state *rs = get_remote_state ();
9d6eea31 8116 remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
cfd77fa1 8117 gdb_byte *regs;
c906108c
SS
8118 char *p;
8119
193cb69f
AC
8120 /* Extract all the registers in the regcache copying them into a
8121 local buffer. */
8122 {
b323314b 8123 int i;
a744cf53 8124
224c3ddb 8125 regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
ea9c271d 8126 memset (regs, 0, rsa->sizeof_g_packet);
ac7936df 8127 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
193cb69f 8128 {
ea9c271d 8129 struct packet_reg *r = &rsa->regs[i];
a744cf53 8130
b323314b 8131 if (r->in_g_packet)
56be3814 8132 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
8133 }
8134 }
c906108c
SS
8135
8136 /* Command describes registers byte by byte,
8137 each byte encoded as two hex characters. */
6d820c5c 8138 p = rs->buf;
193cb69f 8139 *p++ = 'G';
74ca34ce 8140 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
8141 putpkt (rs->buf);
8142 getpkt (&rs->buf, &rs->buf_size, 0);
8143 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
8144 error (_("Could not write registers; remote failure reply '%s'"),
8145 rs->buf);
c906108c 8146}
74ca34ce
DJ
8147
8148/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8149 of the register cache buffer. FIXME: ignores errors. */
8150
f6ac5f3d
PA
8151void
8152remote_target::store_registers (struct regcache *regcache, int regnum)
74ca34ce 8153{
5cd63fda 8154 struct gdbarch *gdbarch = regcache->arch ();
9d6eea31
PA
8155 struct remote_state *rs = get_remote_state ();
8156 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
74ca34ce
DJ
8157 int i;
8158
e6e4e701 8159 set_remote_traceframe ();
bcc0c096 8160 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
8161
8162 if (regnum >= 0)
8163 {
5cd63fda 8164 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 8165
74ca34ce
DJ
8166 gdb_assert (reg != NULL);
8167
8168 /* Always prefer to store registers using the 'P' packet if
8169 possible; we often change only a small number of registers.
8170 Sometimes we change a larger number; we'd need help from a
8171 higher layer to know to use 'G'. */
56be3814 8172 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
8173 return;
8174
8175 /* For now, don't complain if we have no way to write the
8176 register. GDB loses track of unavailable registers too
8177 easily. Some day, this may be an error. We don't have
0df8b418 8178 any way to read the register, either... */
74ca34ce
DJ
8179 if (!reg->in_g_packet)
8180 return;
8181
56be3814 8182 store_registers_using_G (regcache);
74ca34ce
DJ
8183 return;
8184 }
8185
56be3814 8186 store_registers_using_G (regcache);
74ca34ce 8187
5cd63fda 8188 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 8189 if (!rsa->regs[i].in_g_packet)
56be3814 8190 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
8191 /* See above for why we do not issue an error here. */
8192 continue;
8193}
c906108c
SS
8194\f
8195
8196/* Return the number of hex digits in num. */
8197
8198static int
fba45db2 8199hexnumlen (ULONGEST num)
c906108c
SS
8200{
8201 int i;
8202
8203 for (i = 0; num != 0; i++)
8204 num >>= 4;
8205
325fac50 8206 return std::max (i, 1);
c906108c
SS
8207}
8208
2df3850c 8209/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
8210
8211static int
fba45db2 8212hexnumstr (char *buf, ULONGEST num)
c906108c 8213{
c906108c 8214 int len = hexnumlen (num);
a744cf53 8215
2df3850c
JM
8216 return hexnumnstr (buf, num, len);
8217}
8218
c906108c 8219
2df3850c 8220/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 8221
2df3850c 8222static int
fba45db2 8223hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
8224{
8225 int i;
8226
8227 buf[width] = '\0';
8228
8229 for (i = width - 1; i >= 0; i--)
c906108c 8230 {
c5aa993b 8231 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
8232 num >>= 4;
8233 }
8234
2df3850c 8235 return width;
c906108c
SS
8236}
8237
23860348 8238/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
8239
8240static CORE_ADDR
fba45db2 8241remote_address_masked (CORE_ADDR addr)
c906108c 8242{
883b9c6c 8243 unsigned int address_size = remote_address_size;
a744cf53 8244
911c95a5
UW
8245 /* If "remoteaddresssize" was not set, default to target address size. */
8246 if (!address_size)
f5656ead 8247 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
8248
8249 if (address_size > 0
8250 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
8251 {
8252 /* Only create a mask when that mask can safely be constructed
23860348 8253 in a ULONGEST variable. */
c906108c 8254 ULONGEST mask = 1;
a744cf53 8255
911c95a5 8256 mask = (mask << address_size) - 1;
c906108c
SS
8257 addr &= mask;
8258 }
8259 return addr;
8260}
8261
8262/* Determine whether the remote target supports binary downloading.
8263 This is accomplished by sending a no-op memory write of zero length
8264 to the target at the specified address. It does not suffice to send
23860348
MS
8265 the whole packet, since many stubs strip the eighth bit and
8266 subsequently compute a wrong checksum, which causes real havoc with
8267 remote_write_bytes.
7a292a7a 8268
96baa820 8269 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 8270 clean. In cases like this, the user should clear "remote
23860348 8271 X-packet". */
96baa820 8272
c906108c 8273static void
fba45db2 8274check_binary_download (CORE_ADDR addr)
c906108c 8275{
d01949b6 8276 struct remote_state *rs = get_remote_state ();
24b06219 8277
4082afcc 8278 switch (packet_support (PACKET_X))
c906108c 8279 {
96baa820
JM
8280 case PACKET_DISABLE:
8281 break;
8282 case PACKET_ENABLE:
8283 break;
8284 case PACKET_SUPPORT_UNKNOWN:
8285 {
96baa820 8286 char *p;
802188a7 8287
2e9f7625 8288 p = rs->buf;
96baa820
JM
8289 *p++ = 'X';
8290 p += hexnumstr (p, (ULONGEST) addr);
8291 *p++ = ',';
8292 p += hexnumstr (p, (ULONGEST) 0);
8293 *p++ = ':';
8294 *p = '\0';
802188a7 8295
2e9f7625 8296 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8297 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 8298
2e9f7625 8299 if (rs->buf[0] == '\0')
96baa820
JM
8300 {
8301 if (remote_debug)
8302 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8303 "binary downloading NOT "
8304 "supported by target\n");
444abaca 8305 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
8306 }
8307 else
8308 {
8309 if (remote_debug)
8310 fprintf_unfiltered (gdb_stdlog,
64b9b334 8311 "binary downloading supported by target\n");
444abaca 8312 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
8313 }
8314 break;
8315 }
c906108c
SS
8316 }
8317}
8318
124e13d9
SM
8319/* Helper function to resize the payload in order to try to get a good
8320 alignment. We try to write an amount of data such that the next write will
8321 start on an address aligned on REMOTE_ALIGN_WRITES. */
8322
8323static int
8324align_for_efficient_write (int todo, CORE_ADDR memaddr)
8325{
8326 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
8327}
8328
c906108c
SS
8329/* Write memory data directly to the remote machine.
8330 This does not inform the data cache; the data cache uses this.
a76d924d 8331 HEADER is the starting part of the packet.
c906108c
SS
8332 MEMADDR is the address in the remote memory space.
8333 MYADDR is the address of the buffer in our space.
124e13d9
SM
8334 LEN_UNITS is the number of addressable units to write.
8335 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
8336 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8337 should send data as binary ('X'), or hex-encoded ('M').
8338
8339 The function creates packet of the form
8340 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8341
124e13d9 8342 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
8343
8344 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8345 are omitted.
8346
9b409511 8347 Return the transferred status, error or OK (an
124e13d9
SM
8348 'enum target_xfer_status' value). Save the number of addressable units
8349 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8350
8351 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8352 exchange between gdb and the stub could look like (?? in place of the
8353 checksum):
8354
8355 -> $m1000,4#??
8356 <- aaaabbbbccccdddd
8357
8358 -> $M1000,3:eeeeffffeeee#??
8359 <- OK
8360
8361 -> $m1000,4#??
8362 <- eeeeffffeeeedddd */
c906108c 8363
9b409511 8364static enum target_xfer_status
a76d924d 8365remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
8366 const gdb_byte *myaddr, ULONGEST len_units,
8367 int unit_size, ULONGEST *xfered_len_units,
8368 char packet_format, int use_length)
c906108c 8369{
6d820c5c 8370 struct remote_state *rs = get_remote_state ();
cfd77fa1 8371 char *p;
a76d924d
DJ
8372 char *plen = NULL;
8373 int plenlen = 0;
124e13d9
SM
8374 int todo_units;
8375 int units_written;
8376 int payload_capacity_bytes;
8377 int payload_length_bytes;
a76d924d
DJ
8378
8379 if (packet_format != 'X' && packet_format != 'M')
8380 internal_error (__FILE__, __LINE__,
9b20d036 8381 _("remote_write_bytes_aux: bad packet format"));
c906108c 8382
124e13d9 8383 if (len_units == 0)
9b409511 8384 return TARGET_XFER_EOF;
b2182ed2 8385
124e13d9 8386 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 8387
6d820c5c
DJ
8388 /* The packet buffer will be large enough for the payload;
8389 get_memory_packet_size ensures this. */
a76d924d 8390 rs->buf[0] = '\0';
c906108c 8391
a257b5bb 8392 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
8393 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
8394
124e13d9 8395 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 8396 if (!use_length)
0df8b418 8397 /* The comma won't be used. */
124e13d9
SM
8398 payload_capacity_bytes += 1;
8399 payload_capacity_bytes -= strlen (header);
8400 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 8401
a76d924d 8402 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 8403
a76d924d
DJ
8404 strcat (rs->buf, header);
8405 p = rs->buf + strlen (header);
8406
8407 /* Compute a best guess of the number of bytes actually transfered. */
8408 if (packet_format == 'X')
c906108c 8409 {
23860348 8410 /* Best guess at number of bytes that will fit. */
325fac50
PA
8411 todo_units = std::min (len_units,
8412 (ULONGEST) payload_capacity_bytes / unit_size);
a76d924d 8413 if (use_length)
124e13d9 8414 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50 8415 todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
8416 }
8417 else
8418 {
124e13d9 8419 /* Number of bytes that will fit. */
325fac50
PA
8420 todo_units
8421 = std::min (len_units,
8422 (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
a76d924d 8423 if (use_length)
124e13d9 8424 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50
PA
8425 todo_units = std::min (todo_units,
8426 (payload_capacity_bytes / unit_size) / 2);
917317f4 8427 }
a76d924d 8428
124e13d9 8429 if (todo_units <= 0)
3de11b2e 8430 internal_error (__FILE__, __LINE__,
405f8e94 8431 _("minimum packet size too small to write data"));
802188a7 8432
6765f3e5
DJ
8433 /* If we already need another packet, then try to align the end
8434 of this packet to a useful boundary. */
124e13d9
SM
8435 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
8436 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 8437
a257b5bb 8438 /* Append "<memaddr>". */
917317f4
JM
8439 memaddr = remote_address_masked (memaddr);
8440 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 8441
a76d924d
DJ
8442 if (use_length)
8443 {
8444 /* Append ",". */
8445 *p++ = ',';
802188a7 8446
124e13d9
SM
8447 /* Append the length and retain its location and size. It may need to be
8448 adjusted once the packet body has been created. */
a76d924d 8449 plen = p;
124e13d9 8450 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
8451 p += plenlen;
8452 }
a257b5bb
AC
8453
8454 /* Append ":". */
917317f4
JM
8455 *p++ = ':';
8456 *p = '\0';
802188a7 8457
a257b5bb 8458 /* Append the packet body. */
a76d924d 8459 if (packet_format == 'X')
917317f4 8460 {
917317f4
JM
8461 /* Binary mode. Send target system values byte by byte, in
8462 increasing byte addresses. Only escape certain critical
8463 characters. */
124e13d9
SM
8464 payload_length_bytes =
8465 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
8466 &units_written, payload_capacity_bytes);
6765f3e5 8467
124e13d9 8468 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
8469 a second try to keep the end of the packet aligned. Don't do
8470 this if the packet is tiny. */
124e13d9 8471 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 8472 {
124e13d9
SM
8473 int new_todo_units;
8474
8475 new_todo_units = align_for_efficient_write (units_written, memaddr);
8476
8477 if (new_todo_units != units_written)
8478 payload_length_bytes =
8479 remote_escape_output (myaddr, new_todo_units, unit_size,
8480 (gdb_byte *) p, &units_written,
8481 payload_capacity_bytes);
6765f3e5
DJ
8482 }
8483
124e13d9
SM
8484 p += payload_length_bytes;
8485 if (use_length && units_written < todo_units)
c906108c 8486 {
802188a7 8487 /* Escape chars have filled up the buffer prematurely,
124e13d9 8488 and we have actually sent fewer units than planned.
917317f4
JM
8489 Fix-up the length field of the packet. Use the same
8490 number of characters as before. */
124e13d9
SM
8491 plen += hexnumnstr (plen, (ULONGEST) units_written,
8492 plenlen);
917317f4 8493 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 8494 }
a76d924d
DJ
8495 }
8496 else
8497 {
917317f4
JM
8498 /* Normal mode: Send target system values byte by byte, in
8499 increasing byte addresses. Each byte is encoded as a two hex
8500 value. */
124e13d9
SM
8501 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
8502 units_written = todo_units;
c906108c 8503 }
802188a7 8504
2e9f7625 8505 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8506 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8507
2e9f7625 8508 if (rs->buf[0] == 'E')
00d84524 8509 return TARGET_XFER_E_IO;
802188a7 8510
124e13d9
SM
8511 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
8512 send fewer units than we'd planned. */
8513 *xfered_len_units = (ULONGEST) units_written;
92ffd475 8514 return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
c906108c
SS
8515}
8516
a76d924d
DJ
8517/* Write memory data directly to the remote machine.
8518 This does not inform the data cache; the data cache uses this.
8519 MEMADDR is the address in the remote memory space.
8520 MYADDR is the address of the buffer in our space.
8521 LEN is the number of bytes.
8522
9b409511
YQ
8523 Return the transferred status, error or OK (an
8524 'enum target_xfer_status' value). Save the number of bytes
8525 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 8526
9b409511
YQ
8527static enum target_xfer_status
8528remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 8529 int unit_size, ULONGEST *xfered_len)
a76d924d 8530{
a121b7c1 8531 const char *packet_format = NULL;
a76d924d
DJ
8532
8533 /* Check whether the target supports binary download. */
8534 check_binary_download (memaddr);
8535
4082afcc 8536 switch (packet_support (PACKET_X))
a76d924d
DJ
8537 {
8538 case PACKET_ENABLE:
8539 packet_format = "X";
8540 break;
8541 case PACKET_DISABLE:
8542 packet_format = "M";
8543 break;
8544 case PACKET_SUPPORT_UNKNOWN:
8545 internal_error (__FILE__, __LINE__,
8546 _("remote_write_bytes: bad internal state"));
8547 default:
8548 internal_error (__FILE__, __LINE__, _("bad switch"));
8549 }
8550
8551 return remote_write_bytes_aux (packet_format,
124e13d9 8552 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 8553 packet_format[0], 1);
a76d924d
DJ
8554}
8555
9217e74e
YQ
8556/* Read memory data directly from the remote machine.
8557 This does not use the data cache; the data cache uses this.
8558 MEMADDR is the address in the remote memory space.
8559 MYADDR is the address of the buffer in our space.
124e13d9
SM
8560 LEN_UNITS is the number of addressable memory units to read..
8561 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
8562
8563 Return the transferred status, error or OK (an
8564 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
8565 transferred in *XFERED_LEN_UNITS.
8566
8567 See the comment of remote_write_bytes_aux for an example of
8568 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
8569
8570static enum target_xfer_status
124e13d9
SM
8571remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
8572 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
8573{
8574 struct remote_state *rs = get_remote_state ();
124e13d9 8575 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 8576 char *p;
124e13d9
SM
8577 int todo_units;
8578 int decoded_bytes;
9217e74e 8579
124e13d9 8580 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
8581 /* The packet buffer will be large enough for the payload;
8582 get_memory_packet_size ensures this. */
8583
124e13d9 8584 /* Number of units that will fit. */
325fac50
PA
8585 todo_units = std::min (len_units,
8586 (ULONGEST) (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
8587
8588 /* Construct "m"<memaddr>","<len>". */
8589 memaddr = remote_address_masked (memaddr);
8590 p = rs->buf;
8591 *p++ = 'm';
8592 p += hexnumstr (p, (ULONGEST) memaddr);
8593 *p++ = ',';
124e13d9 8594 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
8595 *p = '\0';
8596 putpkt (rs->buf);
8597 getpkt (&rs->buf, &rs->buf_size, 0);
8598 if (rs->buf[0] == 'E'
8599 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
8600 && rs->buf[3] == '\0')
8601 return TARGET_XFER_E_IO;
8602 /* Reply describes memory byte by byte, each byte encoded as two hex
8603 characters. */
8604 p = rs->buf;
124e13d9 8605 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 8606 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 8607 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
92ffd475 8608 return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
9217e74e
YQ
8609}
8610
b55fbac4
YQ
8611/* Using the set of read-only target sections of remote, read live
8612 read-only memory.
8acf9577
YQ
8613
8614 For interface/parameters/return description see target.h,
8615 to_xfer_partial. */
8616
8617static enum target_xfer_status
b55fbac4
YQ
8618remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
8619 ULONGEST memaddr, ULONGEST len,
124e13d9 8620 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
8621{
8622 struct target_section *secp;
8623 struct target_section_table *table;
8624
8625 secp = target_section_by_addr (ops, memaddr);
8626 if (secp != NULL
8627 && (bfd_get_section_flags (secp->the_bfd_section->owner,
8628 secp->the_bfd_section)
8629 & SEC_READONLY))
8630 {
8631 struct target_section *p;
8632 ULONGEST memend = memaddr + len;
8633
8634 table = target_get_section_table (ops);
8635
8636 for (p = table->sections; p < table->sections_end; p++)
8637 {
8638 if (memaddr >= p->addr)
8639 {
8640 if (memend <= p->endaddr)
8641 {
8642 /* Entire transfer is within this section. */
124e13d9 8643 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8644 xfered_len);
8acf9577
YQ
8645 }
8646 else if (memaddr >= p->endaddr)
8647 {
8648 /* This section ends before the transfer starts. */
8649 continue;
8650 }
8651 else
8652 {
8653 /* This section overlaps the transfer. Just do half. */
8654 len = p->endaddr - memaddr;
124e13d9 8655 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8656 xfered_len);
8acf9577
YQ
8657 }
8658 }
8659 }
8660 }
8661
8662 return TARGET_XFER_EOF;
8663}
8664
9217e74e
YQ
8665/* Similar to remote_read_bytes_1, but it reads from the remote stub
8666 first if the requested memory is unavailable in traceframe.
8667 Otherwise, fall back to remote_read_bytes_1. */
c906108c 8668
9b409511 8669static enum target_xfer_status
8acf9577 8670remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
8671 gdb_byte *myaddr, ULONGEST len, int unit_size,
8672 ULONGEST *xfered_len)
c906108c 8673{
6b6aa828 8674 if (len == 0)
96c4f946 8675 return TARGET_XFER_EOF;
b2182ed2 8676
8acf9577
YQ
8677 if (get_traceframe_number () != -1)
8678 {
a79b1bc6 8679 std::vector<mem_range> available;
8acf9577
YQ
8680
8681 /* If we fail to get the set of available memory, then the
8682 target does not support querying traceframe info, and so we
8683 attempt reading from the traceframe anyway (assuming the
8684 target implements the old QTro packet then). */
8685 if (traceframe_available_memory (&available, memaddr, len))
8686 {
a79b1bc6 8687 if (available.empty () || available[0].start != memaddr)
8acf9577
YQ
8688 {
8689 enum target_xfer_status res;
8690
8691 /* Don't read into the traceframe's available
8692 memory. */
a79b1bc6 8693 if (!available.empty ())
8acf9577
YQ
8694 {
8695 LONGEST oldlen = len;
8696
a79b1bc6 8697 len = available[0].start - memaddr;
8acf9577
YQ
8698 gdb_assert (len <= oldlen);
8699 }
8700
8acf9577 8701 /* This goes through the topmost target again. */
b55fbac4 8702 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 8703 len, unit_size, xfered_len);
8acf9577
YQ
8704 if (res == TARGET_XFER_OK)
8705 return TARGET_XFER_OK;
8706 else
8707 {
8708 /* No use trying further, we know some memory starting
8709 at MEMADDR isn't available. */
8710 *xfered_len = len;
92ffd475
PC
8711 return (*xfered_len != 0) ?
8712 TARGET_XFER_UNAVAILABLE : TARGET_XFER_EOF;
8acf9577
YQ
8713 }
8714 }
8715
8716 /* Don't try to read more than how much is available, in
8717 case the target implements the deprecated QTro packet to
8718 cater for older GDBs (the target's knowledge of read-only
8719 sections may be outdated by now). */
a79b1bc6 8720 len = available[0].length;
8acf9577
YQ
8721 }
8722 }
8723
124e13d9 8724 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 8725}
74531fed 8726
c906108c 8727\f
c906108c 8728
a76d924d
DJ
8729/* Sends a packet with content determined by the printf format string
8730 FORMAT and the remaining arguments, then gets the reply. Returns
8731 whether the packet was a success, a failure, or unknown. */
8732
77b64a49
PA
8733static enum packet_result remote_send_printf (const char *format, ...)
8734 ATTRIBUTE_PRINTF (1, 2);
8735
2c0b251b 8736static enum packet_result
a76d924d
DJ
8737remote_send_printf (const char *format, ...)
8738{
8739 struct remote_state *rs = get_remote_state ();
8740 int max_size = get_remote_packet_size ();
a76d924d 8741 va_list ap;
a744cf53 8742
a76d924d
DJ
8743 va_start (ap, format);
8744
8745 rs->buf[0] = '\0';
8746 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 8747 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
8748
8749 if (putpkt (rs->buf) < 0)
8750 error (_("Communication problem with target."));
8751
8752 rs->buf[0] = '\0';
8753 getpkt (&rs->buf, &rs->buf_size, 0);
8754
8755 return packet_check_result (rs->buf);
8756}
8757
a76d924d
DJ
8758/* Flash writing can take quite some time. We'll set
8759 effectively infinite timeout for flash operations.
8760 In future, we'll need to decide on a better approach. */
8761static const int remote_flash_timeout = 1000;
8762
f6ac5f3d
PA
8763void
8764remote_target::flash_erase (ULONGEST address, LONGEST length)
a76d924d 8765{
f5656ead 8766 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d 8767 enum packet_result ret;
2ec845e7
TT
8768 scoped_restore restore_timeout
8769 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
a76d924d
DJ
8770
8771 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 8772 phex (address, addr_size),
a76d924d
DJ
8773 phex (length, 4));
8774 switch (ret)
8775 {
8776 case PACKET_UNKNOWN:
8777 error (_("Remote target does not support flash erase"));
8778 case PACKET_ERROR:
8779 error (_("Error erasing flash with vFlashErase packet"));
8780 default:
8781 break;
8782 }
a76d924d
DJ
8783}
8784
9b409511
YQ
8785static enum target_xfer_status
8786remote_flash_write (struct target_ops *ops, ULONGEST address,
8787 ULONGEST length, ULONGEST *xfered_len,
8788 const gdb_byte *data)
a76d924d 8789{
2ec845e7
TT
8790 scoped_restore restore_timeout
8791 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8792 return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
8793 xfered_len,'X', 0);
a76d924d
DJ
8794}
8795
f6ac5f3d
PA
8796void
8797remote_target::flash_done ()
a76d924d 8798{
a76d924d 8799 int ret;
a76d924d 8800
2ec845e7
TT
8801 scoped_restore restore_timeout
8802 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8803
a76d924d 8804 ret = remote_send_printf ("vFlashDone");
a76d924d
DJ
8805
8806 switch (ret)
8807 {
8808 case PACKET_UNKNOWN:
8809 error (_("Remote target does not support vFlashDone"));
8810 case PACKET_ERROR:
8811 error (_("Error finishing flash operation"));
8812 default:
8813 break;
8814 }
8815}
8816
f6ac5f3d
PA
8817void
8818remote_target::files_info ()
c906108c
SS
8819{
8820 puts_filtered ("Debugging a target over a serial line.\n");
8821}
8822\f
8823/* Stuff for dealing with the packets which are part of this protocol.
8824 See comment at top of file for details. */
8825
1927e618
PA
8826/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
8827 error to higher layers. Called when a serial error is detected.
8828 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
8829 the system error message for errno at function entry and final dot
8830 for output compatibility with throw_perror_with_name. */
1927e618
PA
8831
8832static void
8833unpush_and_perror (const char *string)
8834{
d6cb50a2 8835 int saved_errno = errno;
1927e618
PA
8836
8837 remote_unpush_target ();
d6cb50a2
JK
8838 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
8839 safe_strerror (saved_errno));
1927e618
PA
8840}
8841
048094ac
PA
8842/* Read a single character from the remote end. The current quit
8843 handler is overridden to avoid quitting in the middle of packet
8844 sequence, as that would break communication with the remote server.
8845 See remote_serial_quit_handler for more detail. */
c906108c
SS
8846
8847static int
fba45db2 8848readchar (int timeout)
c906108c
SS
8849{
8850 int ch;
5d93a237 8851 struct remote_state *rs = get_remote_state ();
048094ac 8852
2ec845e7
TT
8853 {
8854 scoped_restore restore_quit
8855 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
c906108c 8856
2ec845e7 8857 rs->got_ctrlc_during_io = 0;
c906108c 8858
2ec845e7 8859 ch = serial_readchar (rs->remote_desc, timeout);
048094ac 8860
2ec845e7
TT
8861 if (rs->got_ctrlc_during_io)
8862 set_quit_flag ();
8863 }
048094ac 8864
2acceee2 8865 if (ch >= 0)
0876f84a 8866 return ch;
2acceee2
JM
8867
8868 switch ((enum serial_rc) ch)
c906108c
SS
8869 {
8870 case SERIAL_EOF:
78a095c3 8871 remote_unpush_target ();
598d3636 8872 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 8873 /* no return */
c906108c 8874 case SERIAL_ERROR:
1927e618
PA
8875 unpush_and_perror (_("Remote communication error. "
8876 "Target disconnected."));
2acceee2 8877 /* no return */
c906108c 8878 case SERIAL_TIMEOUT:
2acceee2 8879 break;
c906108c 8880 }
2acceee2 8881 return ch;
c906108c
SS
8882}
8883
c33e31fd 8884/* Wrapper for serial_write that closes the target and throws if
048094ac
PA
8885 writing fails. The current quit handler is overridden to avoid
8886 quitting in the middle of packet sequence, as that would break
8887 communication with the remote server. See
8888 remote_serial_quit_handler for more detail. */
c33e31fd
PA
8889
8890static void
8891remote_serial_write (const char *str, int len)
8892{
5d93a237 8893 struct remote_state *rs = get_remote_state ();
048094ac 8894
2ec845e7
TT
8895 scoped_restore restore_quit
8896 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
048094ac
PA
8897
8898 rs->got_ctrlc_during_io = 0;
5d93a237
TT
8899
8900 if (serial_write (rs->remote_desc, str, len))
c33e31fd 8901 {
1927e618
PA
8902 unpush_and_perror (_("Remote communication error. "
8903 "Target disconnected."));
c33e31fd 8904 }
048094ac
PA
8905
8906 if (rs->got_ctrlc_during_io)
8907 set_quit_flag ();
c33e31fd
PA
8908}
8909
b3ced9ba
PA
8910/* Return a string representing an escaped version of BUF, of len N.
8911 E.g. \n is converted to \\n, \t to \\t, etc. */
6e5abd65 8912
b3ced9ba 8913static std::string
6e5abd65
PA
8914escape_buffer (const char *buf, int n)
8915{
d7e74731 8916 string_file stb;
6e5abd65 8917
d7e74731
PA
8918 stb.putstrn (buf, n, '\\');
8919 return std::move (stb.string ());
6e5abd65
PA
8920}
8921
c906108c
SS
8922/* Display a null-terminated packet on stdout, for debugging, using C
8923 string notation. */
8924
8925static void
baa336ce 8926print_packet (const char *buf)
c906108c
SS
8927{
8928 puts_filtered ("\"");
43e526b9 8929 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
8930 puts_filtered ("\"");
8931}
8932
8933int
baa336ce 8934putpkt (const char *buf)
c906108c
SS
8935{
8936 return putpkt_binary (buf, strlen (buf));
8937}
8938
8939/* Send a packet to the remote machine, with error checking. The data
23860348 8940 of the packet is in BUF. The string in BUF can be at most
ea9c271d 8941 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
8942 and for a possible /0 if we are debugging (remote_debug) and want
8943 to print the sent packet as a string. */
c906108c
SS
8944
8945static int
baa336ce 8946putpkt_binary (const char *buf, int cnt)
c906108c 8947{
2d717e4f 8948 struct remote_state *rs = get_remote_state ();
c906108c
SS
8949 int i;
8950 unsigned char csum = 0;
b80406ac
TT
8951 gdb::def_vector<char> data (cnt + 6);
8952 char *buf2 = data.data ();
085dd6e6 8953
c906108c
SS
8954 int ch;
8955 int tcount = 0;
8956 char *p;
8957
e24a49d8
PA
8958 /* Catch cases like trying to read memory or listing threads while
8959 we're waiting for a stop reply. The remote server wouldn't be
8960 ready to handle this request, so we'd hang and timeout. We don't
8961 have to worry about this in synchronous mode, because in that
8962 case it's not possible to issue a command while the target is
74531fed
PA
8963 running. This is not a problem in non-stop mode, because in that
8964 case, the stub is always ready to process serial input. */
6efcd9a8
PA
8965 if (!target_is_non_stop_p ()
8966 && target_is_async_p ()
8967 && rs->waiting_for_stop_reply)
9597b22a
DE
8968 {
8969 error (_("Cannot execute this command while the target is running.\n"
8970 "Use the \"interrupt\" command to stop the target\n"
8971 "and then try again."));
8972 }
e24a49d8 8973
2d717e4f
DJ
8974 /* We're sending out a new packet. Make sure we don't look at a
8975 stale cached response. */
8976 rs->cached_wait_status = 0;
8977
c906108c
SS
8978 /* Copy the packet into buffer BUF2, encapsulating it
8979 and giving it a checksum. */
8980
c906108c
SS
8981 p = buf2;
8982 *p++ = '$';
8983
8984 for (i = 0; i < cnt; i++)
8985 {
8986 csum += buf[i];
8987 *p++ = buf[i];
8988 }
8989 *p++ = '#';
8990 *p++ = tohex ((csum >> 4) & 0xf);
8991 *p++ = tohex (csum & 0xf);
8992
8993 /* Send it over and over until we get a positive ack. */
8994
8995 while (1)
8996 {
8997 int started_error_output = 0;
8998
8999 if (remote_debug)
9000 {
9001 *p = '\0';
b3ced9ba 9002
6f8976bf
YQ
9003 int len = (int) (p - buf2);
9004
9005 std::string str
9006 = escape_buffer (buf2, std::min (len, REMOTE_DEBUG_MAX_CHAR));
9007
9008 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s", str.c_str ());
9009
567a3e54
SM
9010 if (len > REMOTE_DEBUG_MAX_CHAR)
9011 fprintf_unfiltered (gdb_stdlog, "[%d bytes omitted]",
9012 len - REMOTE_DEBUG_MAX_CHAR);
6f8976bf
YQ
9013
9014 fprintf_unfiltered (gdb_stdlog, "...");
b3ced9ba 9015
0f71a2f6 9016 gdb_flush (gdb_stdlog);
c906108c 9017 }
c33e31fd 9018 remote_serial_write (buf2, p - buf2);
c906108c 9019
a6f3e723
SL
9020 /* If this is a no acks version of the remote protocol, send the
9021 packet and move on. */
9022 if (rs->noack_mode)
9023 break;
9024
74531fed
PA
9025 /* Read until either a timeout occurs (-2) or '+' is read.
9026 Handle any notification that arrives in the mean time. */
c906108c
SS
9027 while (1)
9028 {
9029 ch = readchar (remote_timeout);
9030
c5aa993b 9031 if (remote_debug)
c906108c
SS
9032 {
9033 switch (ch)
9034 {
9035 case '+':
1216fa2c 9036 case '-':
c906108c
SS
9037 case SERIAL_TIMEOUT:
9038 case '$':
74531fed 9039 case '%':
c906108c
SS
9040 if (started_error_output)
9041 {
9042 putchar_unfiltered ('\n');
9043 started_error_output = 0;
9044 }
9045 }
9046 }
9047
9048 switch (ch)
9049 {
9050 case '+':
9051 if (remote_debug)
0f71a2f6 9052 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 9053 return 1;
1216fa2c
AC
9054 case '-':
9055 if (remote_debug)
9056 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 9057 /* FALLTHROUGH */
c906108c 9058 case SERIAL_TIMEOUT:
c5aa993b 9059 tcount++;
c906108c 9060 if (tcount > 3)
b80406ac 9061 return 0;
23860348 9062 break; /* Retransmit buffer. */
c906108c
SS
9063 case '$':
9064 {
40e3f985 9065 if (remote_debug)
2bc416ba 9066 fprintf_unfiltered (gdb_stdlog,
23860348 9067 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
9068 /* It's probably an old response sent because an ACK
9069 was lost. Gobble up the packet and ack it so it
9070 doesn't get retransmitted when we resend this
9071 packet. */
6d820c5c 9072 skip_frame ();
c33e31fd 9073 remote_serial_write ("+", 1);
23860348 9074 continue; /* Now, go look for +. */
c906108c 9075 }
74531fed
PA
9076
9077 case '%':
9078 {
9079 int val;
9080
9081 /* If we got a notification, handle it, and go back to looking
9082 for an ack. */
9083 /* We've found the start of a notification. Now
9084 collect the data. */
9085 val = read_frame (&rs->buf, &rs->buf_size);
9086 if (val >= 0)
9087 {
9088 if (remote_debug)
9089 {
b3ced9ba 9090 std::string str = escape_buffer (rs->buf, val);
6e5abd65 9091
6e5abd65
PA
9092 fprintf_unfiltered (gdb_stdlog,
9093 " Notification received: %s\n",
b3ced9ba 9094 str.c_str ());
74531fed 9095 }
5965e028 9096 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
9097 /* We're in sync now, rewait for the ack. */
9098 tcount = 0;
9099 }
9100 else
9101 {
9102 if (remote_debug)
9103 {
9104 if (!started_error_output)
9105 {
9106 started_error_output = 1;
9107 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
9108 }
9109 fputc_unfiltered (ch & 0177, gdb_stdlog);
9110 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
9111 }
9112 }
9113 continue;
9114 }
9115 /* fall-through */
c906108c
SS
9116 default:
9117 if (remote_debug)
9118 {
9119 if (!started_error_output)
9120 {
9121 started_error_output = 1;
0f71a2f6 9122 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 9123 }
0f71a2f6 9124 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
9125 }
9126 continue;
9127 }
23860348 9128 break; /* Here to retransmit. */
c906108c
SS
9129 }
9130
9131#if 0
9132 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
9133 able to get out next time we call QUIT, without anything as
9134 violent as interrupt_query. If we want to provide a way out of
9135 here without getting to the next QUIT, it should be based on
9136 hitting ^C twice as in remote_wait. */
c906108c
SS
9137 if (quit_flag)
9138 {
9139 quit_flag = 0;
9140 interrupt_query ();
9141 }
9142#endif
9143 }
a5c0808e 9144
a6f3e723 9145 return 0;
c906108c
SS
9146}
9147
6d820c5c
DJ
9148/* Come here after finding the start of a frame when we expected an
9149 ack. Do our best to discard the rest of this packet. */
9150
9151static void
9152skip_frame (void)
9153{
9154 int c;
9155
9156 while (1)
9157 {
9158 c = readchar (remote_timeout);
9159 switch (c)
9160 {
9161 case SERIAL_TIMEOUT:
9162 /* Nothing we can do. */
9163 return;
9164 case '#':
9165 /* Discard the two bytes of checksum and stop. */
9166 c = readchar (remote_timeout);
9167 if (c >= 0)
9168 c = readchar (remote_timeout);
9169
9170 return;
9171 case '*': /* Run length encoding. */
9172 /* Discard the repeat count. */
9173 c = readchar (remote_timeout);
9174 if (c < 0)
9175 return;
9176 break;
9177 default:
9178 /* A regular character. */
9179 break;
9180 }
9181 }
9182}
9183
c906108c 9184/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
9185 into *BUF, verifying the checksum, length, and handling run-length
9186 compression. NUL terminate the buffer. If there is not enough room,
9187 expand *BUF using xrealloc.
c906108c 9188
c2d11a7d
JM
9189 Returns -1 on error, number of characters in buffer (ignoring the
9190 trailing NULL) on success. (could be extended to return one of the
23860348 9191 SERIAL status indications). */
c2d11a7d
JM
9192
9193static long
6d820c5c
DJ
9194read_frame (char **buf_p,
9195 long *sizeof_buf)
c906108c
SS
9196{
9197 unsigned char csum;
c2d11a7d 9198 long bc;
c906108c 9199 int c;
6d820c5c 9200 char *buf = *buf_p;
a6f3e723 9201 struct remote_state *rs = get_remote_state ();
c906108c
SS
9202
9203 csum = 0;
c2d11a7d 9204 bc = 0;
c906108c
SS
9205
9206 while (1)
9207 {
9208 c = readchar (remote_timeout);
c906108c
SS
9209 switch (c)
9210 {
9211 case SERIAL_TIMEOUT:
9212 if (remote_debug)
0f71a2f6 9213 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 9214 return -1;
c906108c
SS
9215 case '$':
9216 if (remote_debug)
0f71a2f6
JM
9217 fputs_filtered ("Saw new packet start in middle of old one\n",
9218 gdb_stdlog);
23860348 9219 return -1; /* Start a new packet, count retries. */
c906108c
SS
9220 case '#':
9221 {
9222 unsigned char pktcsum;
e1b09194
AC
9223 int check_0 = 0;
9224 int check_1 = 0;
c906108c 9225
c2d11a7d 9226 buf[bc] = '\0';
c906108c 9227
e1b09194
AC
9228 check_0 = readchar (remote_timeout);
9229 if (check_0 >= 0)
9230 check_1 = readchar (remote_timeout);
802188a7 9231
e1b09194
AC
9232 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
9233 {
9234 if (remote_debug)
2bc416ba 9235 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 9236 gdb_stdlog);
e1b09194
AC
9237 return -1;
9238 }
9239 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
9240 {
9241 if (remote_debug)
2bc416ba 9242 fputs_filtered ("Communication error in checksum\n",
23860348 9243 gdb_stdlog);
40e3f985
FN
9244 return -1;
9245 }
c906108c 9246
a6f3e723
SL
9247 /* Don't recompute the checksum; with no ack packets we
9248 don't have any way to indicate a packet retransmission
9249 is necessary. */
9250 if (rs->noack_mode)
9251 return bc;
9252
e1b09194 9253 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 9254 if (csum == pktcsum)
c2d11a7d 9255 return bc;
c906108c 9256
c5aa993b 9257 if (remote_debug)
c906108c 9258 {
b3ced9ba 9259 std::string str = escape_buffer (buf, bc);
6e5abd65 9260
6e5abd65 9261 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
9262 "Bad checksum, sentsum=0x%x, "
9263 "csum=0x%x, buf=%s\n",
b3ced9ba 9264 pktcsum, csum, str.c_str ());
c906108c 9265 }
c2d11a7d 9266 /* Number of characters in buffer ignoring trailing
23860348 9267 NULL. */
c2d11a7d 9268 return -1;
c906108c 9269 }
23860348 9270 case '*': /* Run length encoding. */
c2c6d25f
JM
9271 {
9272 int repeat;
c906108c 9273
a744cf53 9274 csum += c;
b4501125
AC
9275 c = readchar (remote_timeout);
9276 csum += c;
23860348 9277 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 9278
23860348 9279 /* The character before ``*'' is repeated. */
c2d11a7d 9280
6d820c5c 9281 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 9282 {
6d820c5c
DJ
9283 if (bc + repeat - 1 >= *sizeof_buf - 1)
9284 {
9285 /* Make some more room in the buffer. */
9286 *sizeof_buf += repeat;
224c3ddb 9287 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c
DJ
9288 buf = *buf_p;
9289 }
9290
c2d11a7d
JM
9291 memset (&buf[bc], buf[bc - 1], repeat);
9292 bc += repeat;
c2c6d25f
JM
9293 continue;
9294 }
9295
c2d11a7d 9296 buf[bc] = '\0';
6d820c5c 9297 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 9298 return -1;
c2c6d25f 9299 }
c906108c 9300 default:
6d820c5c 9301 if (bc >= *sizeof_buf - 1)
c906108c 9302 {
6d820c5c
DJ
9303 /* Make some more room in the buffer. */
9304 *sizeof_buf *= 2;
224c3ddb 9305 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c 9306 buf = *buf_p;
c906108c
SS
9307 }
9308
6d820c5c
DJ
9309 buf[bc++] = c;
9310 csum += c;
9311 continue;
c906108c
SS
9312 }
9313 }
9314}
9315
9316/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9317 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9318 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9319 rather than timing out; this is used (in synchronous mode) to wait
9320 for a target that is is executing user code to stop. */
d9fcf2fb
JM
9321/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9322 don't have to change all the calls to getpkt to deal with the
9323 return value, because at the moment I don't know what the right
23860348 9324 thing to do it for those. */
c906108c 9325void
6d820c5c
DJ
9326getpkt (char **buf,
9327 long *sizeof_buf,
c2d11a7d 9328 int forever)
d9fcf2fb 9329{
54887903 9330 getpkt_sane (buf, sizeof_buf, forever);
d9fcf2fb
JM
9331}
9332
9333
9334/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9335 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9336 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9337 rather than timing out; this is used (in synchronous mode) to wait
9338 for a target that is is executing user code to stop. If FOREVER ==
9339 0, this function is allowed to time out gracefully and return an
74531fed
PA
9340 indication of this to the caller. Otherwise return the number of
9341 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
9342 enough reason to return to the caller. *IS_NOTIF is an output
9343 boolean that indicates whether *BUF holds a notification or not
9344 (a regular packet). */
74531fed 9345
3172dc30 9346static int
74531fed 9347getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 9348 int expecting_notif, int *is_notif)
c906108c 9349{
2d717e4f 9350 struct remote_state *rs = get_remote_state ();
c906108c
SS
9351 int c;
9352 int tries;
9353 int timeout;
df4b58fe 9354 int val = -1;
c906108c 9355
2d717e4f
DJ
9356 /* We're reading a new response. Make sure we don't look at a
9357 previously cached response. */
9358 rs->cached_wait_status = 0;
9359
6d820c5c 9360 strcpy (*buf, "timeout");
c906108c
SS
9361
9362 if (forever)
74531fed
PA
9363 timeout = watchdog > 0 ? watchdog : -1;
9364 else if (expecting_notif)
9365 timeout = 0; /* There should already be a char in the buffer. If
9366 not, bail out. */
c906108c
SS
9367 else
9368 timeout = remote_timeout;
9369
9370#define MAX_TRIES 3
9371
74531fed
PA
9372 /* Process any number of notifications, and then return when
9373 we get a packet. */
9374 for (;;)
c906108c 9375 {
d9c43928 9376 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
9377 times. */
9378 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 9379 {
74531fed
PA
9380 /* This can loop forever if the remote side sends us
9381 characters continuously, but if it pauses, we'll get
9382 SERIAL_TIMEOUT from readchar because of timeout. Then
9383 we'll count that as a retry.
9384
9385 Note that even when forever is set, we will only wait
9386 forever prior to the start of a packet. After that, we
9387 expect characters to arrive at a brisk pace. They should
9388 show up within remote_timeout intervals. */
9389 do
9390 c = readchar (timeout);
9391 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
9392
9393 if (c == SERIAL_TIMEOUT)
9394 {
74531fed
PA
9395 if (expecting_notif)
9396 return -1; /* Don't complain, it's normal to not get
9397 anything in this case. */
9398
23860348 9399 if (forever) /* Watchdog went off? Kill the target. */
c906108c 9400 {
78a095c3 9401 remote_unpush_target ();
598d3636
JK
9402 throw_error (TARGET_CLOSE_ERROR,
9403 _("Watchdog timeout has expired. "
9404 "Target detached."));
c906108c 9405 }
c906108c 9406 if (remote_debug)
0f71a2f6 9407 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 9408 }
74531fed
PA
9409 else
9410 {
9411 /* We've found the start of a packet or notification.
9412 Now collect the data. */
9413 val = read_frame (buf, sizeof_buf);
9414 if (val >= 0)
9415 break;
9416 }
9417
c33e31fd 9418 remote_serial_write ("-", 1);
c906108c 9419 }
c906108c 9420
74531fed
PA
9421 if (tries > MAX_TRIES)
9422 {
9423 /* We have tried hard enough, and just can't receive the
9424 packet/notification. Give up. */
9425 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 9426
74531fed
PA
9427 /* Skip the ack char if we're in no-ack mode. */
9428 if (!rs->noack_mode)
c33e31fd 9429 remote_serial_write ("+", 1);
74531fed
PA
9430 return -1;
9431 }
c906108c 9432
74531fed
PA
9433 /* If we got an ordinary packet, return that to our caller. */
9434 if (c == '$')
c906108c
SS
9435 {
9436 if (remote_debug)
43e526b9 9437 {
6f8976bf
YQ
9438 std::string str
9439 = escape_buffer (*buf,
9440 std::min (val, REMOTE_DEBUG_MAX_CHAR));
9441
9442 fprintf_unfiltered (gdb_stdlog, "Packet received: %s",
9443 str.c_str ());
9444
567a3e54
SM
9445 if (val > REMOTE_DEBUG_MAX_CHAR)
9446 fprintf_unfiltered (gdb_stdlog, "[%d bytes omitted]",
9447 val - REMOTE_DEBUG_MAX_CHAR);
6e5abd65 9448
6f8976bf 9449 fprintf_unfiltered (gdb_stdlog, "\n");
43e526b9 9450 }
a6f3e723
SL
9451
9452 /* Skip the ack char if we're in no-ack mode. */
9453 if (!rs->noack_mode)
c33e31fd 9454 remote_serial_write ("+", 1);
fee9eda9
YQ
9455 if (is_notif != NULL)
9456 *is_notif = 0;
0876f84a 9457 return val;
c906108c
SS
9458 }
9459
74531fed
PA
9460 /* If we got a notification, handle it, and go back to looking
9461 for a packet. */
9462 else
9463 {
9464 gdb_assert (c == '%');
9465
9466 if (remote_debug)
9467 {
b3ced9ba 9468 std::string str = escape_buffer (*buf, val);
6e5abd65 9469
6e5abd65
PA
9470 fprintf_unfiltered (gdb_stdlog,
9471 " Notification received: %s\n",
b3ced9ba 9472 str.c_str ());
74531fed 9473 }
fee9eda9
YQ
9474 if (is_notif != NULL)
9475 *is_notif = 1;
c906108c 9476
5965e028 9477 handle_notification (rs->notif_state, *buf);
c906108c 9478
74531fed 9479 /* Notifications require no acknowledgement. */
a6f3e723 9480
74531fed 9481 if (expecting_notif)
fee9eda9 9482 return val;
74531fed
PA
9483 }
9484 }
9485}
9486
9487static int
9488getpkt_sane (char **buf, long *sizeof_buf, int forever)
9489{
fee9eda9 9490 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
9491}
9492
9493static int
fee9eda9
YQ
9494getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
9495 int *is_notif)
74531fed 9496{
fee9eda9
YQ
9497 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
9498 is_notif);
c906108c 9499}
74531fed 9500
cbb8991c
DB
9501/* Check whether EVENT is a fork event for the process specified
9502 by the pid passed in DATA, and if it is, kill the fork child. */
9503
9504static int
9505kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
9506 QUEUE_ITER (stop_reply_p) *iter,
9507 stop_reply_p event,
9508 void *data)
9509{
19ba03f4 9510 struct queue_iter_param *param = (struct queue_iter_param *) data;
cbb8991c
DB
9511 int parent_pid = *(int *) param->input;
9512
9513 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
9514 {
9515 struct remote_state *rs = get_remote_state ();
9516 int child_pid = ptid_get_pid (event->ws.value.related_pid);
9517 int res;
9518
9519 res = remote_vkill (child_pid, rs);
9520 if (res != 0)
9521 error (_("Can't kill fork child process %d"), child_pid);
9522 }
9523
9524 return 1;
9525}
9526
9527/* Kill any new fork children of process PID that haven't been
9528 processed by follow_fork. */
9529
9530static void
9531kill_new_fork_children (int pid, struct remote_state *rs)
9532{
9533 struct thread_info *thread;
9534 struct notif_client *notif = &notif_client_stop;
9535 struct queue_iter_param param;
9536
9537 /* Kill the fork child threads of any threads in process PID
9538 that are stopped at a fork event. */
9539 ALL_NON_EXITED_THREADS (thread)
9540 {
9541 struct target_waitstatus *ws = &thread->pending_follow;
9542
9543 if (is_pending_fork_parent (ws, pid, thread->ptid))
9544 {
9545 struct remote_state *rs = get_remote_state ();
9546 int child_pid = ptid_get_pid (ws->value.related_pid);
9547 int res;
9548
9549 res = remote_vkill (child_pid, rs);
9550 if (res != 0)
9551 error (_("Can't kill fork child process %d"), child_pid);
9552 }
9553 }
9554
9555 /* Check for any pending fork events (not reported or processed yet)
9556 in process PID and kill those fork child threads as well. */
9557 remote_notif_get_pending_events (notif);
9558 param.input = &pid;
9559 param.output = NULL;
9560 QUEUE_iterate (stop_reply_p, stop_reply_queue,
9561 kill_child_of_pending_fork, &param);
9562}
9563
c906108c 9564\f
8020350c
DB
9565/* Target hook to kill the current inferior. */
9566
f6ac5f3d
PA
9567void
9568remote_target::kill ()
43ff13b4 9569{
8020350c
DB
9570 int res = -1;
9571 int pid = ptid_get_pid (inferior_ptid);
9572 struct remote_state *rs = get_remote_state ();
0fdf84ca 9573
8020350c 9574 if (packet_support (PACKET_vKill) != PACKET_DISABLE)
0fdf84ca 9575 {
8020350c
DB
9576 /* If we're stopped while forking and we haven't followed yet,
9577 kill the child task. We need to do this before killing the
9578 parent task because if this is a vfork then the parent will
9579 be sleeping. */
9580 kill_new_fork_children (pid, rs);
9581
9582 res = remote_vkill (pid, rs);
9583 if (res == 0)
0fdf84ca 9584 {
bc1e6c81 9585 target_mourn_inferior (inferior_ptid);
0fdf84ca
PA
9586 return;
9587 }
8020350c 9588 }
0fdf84ca 9589
8020350c
DB
9590 /* If we are in 'target remote' mode and we are killing the only
9591 inferior, then we will tell gdbserver to exit and unpush the
9592 target. */
9593 if (res == -1 && !remote_multi_process_p (rs)
9594 && number_of_live_inferiors () == 1)
9595 {
9596 remote_kill_k ();
9597
9598 /* We've killed the remote end, we get to mourn it. If we are
9599 not in extended mode, mourning the inferior also unpushes
9600 remote_ops from the target stack, which closes the remote
9601 connection. */
bc1e6c81 9602 target_mourn_inferior (inferior_ptid);
8020350c
DB
9603
9604 return;
0fdf84ca 9605 }
43ff13b4 9606
8020350c 9607 error (_("Can't kill process"));
43ff13b4
JM
9608}
9609
8020350c
DB
9610/* Send a kill request to the target using the 'vKill' packet. */
9611
82f73884
PA
9612static int
9613remote_vkill (int pid, struct remote_state *rs)
9614{
4082afcc 9615 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
9616 return -1;
9617
9618 /* Tell the remote target to detach. */
bba74b36 9619 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
9620 putpkt (rs->buf);
9621 getpkt (&rs->buf, &rs->buf_size, 0);
9622
4082afcc
PA
9623 switch (packet_ok (rs->buf,
9624 &remote_protocol_packets[PACKET_vKill]))
9625 {
9626 case PACKET_OK:
9627 return 0;
9628 case PACKET_ERROR:
9629 return 1;
9630 case PACKET_UNKNOWN:
9631 return -1;
9632 default:
9633 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
9634 }
82f73884
PA
9635}
9636
8020350c
DB
9637/* Send a kill request to the target using the 'k' packet. */
9638
82f73884 9639static void
8020350c 9640remote_kill_k (void)
82f73884 9641{
8020350c
DB
9642 /* Catch errors so the user can quit from gdb even when we
9643 aren't on speaking terms with the remote system. */
9644 TRY
82f73884 9645 {
82f73884 9646 putpkt ("k");
82f73884 9647 }
8020350c
DB
9648 CATCH (ex, RETURN_MASK_ERROR)
9649 {
9650 if (ex.error == TARGET_CLOSE_ERROR)
9651 {
9652 /* If we got an (EOF) error that caused the target
9653 to go away, then we're done, that's what we wanted.
9654 "k" is susceptible to cause a premature EOF, given
9655 that the remote server isn't actually required to
9656 reply to "k", and it can happen that it doesn't
9657 even get to reply ACK to the "k". */
9658 return;
9659 }
82f73884 9660
8020350c
DB
9661 /* Otherwise, something went wrong. We didn't actually kill
9662 the target. Just propagate the exception, and let the
9663 user or higher layers decide what to do. */
9664 throw_exception (ex);
9665 }
9666 END_CATCH
82f73884
PA
9667}
9668
f6ac5f3d
PA
9669void
9670remote_target::mourn_inferior ()
c906108c 9671{
8020350c 9672 struct remote_state *rs = get_remote_state ();
ce5ce7ed 9673
8020350c
DB
9674 /* In 'target remote' mode with one inferior, we close the connection. */
9675 if (!rs->extended && number_of_live_inferiors () <= 1)
9676 {
f6ac5f3d 9677 unpush_target (this);
c906108c 9678
8020350c
DB
9679 /* remote_close takes care of doing most of the clean up. */
9680 generic_mourn_inferior ();
9681 return;
9682 }
c906108c 9683
e24a49d8
PA
9684 /* In case we got here due to an error, but we're going to stay
9685 connected. */
9686 rs->waiting_for_stop_reply = 0;
9687
dc1981d7
PA
9688 /* If the current general thread belonged to the process we just
9689 detached from or has exited, the remote side current general
9690 thread becomes undefined. Considering a case like this:
9691
9692 - We just got here due to a detach.
9693 - The process that we're detaching from happens to immediately
9694 report a global breakpoint being hit in non-stop mode, in the
9695 same thread we had selected before.
9696 - GDB attaches to this process again.
9697 - This event happens to be the next event we handle.
9698
9699 GDB would consider that the current general thread didn't need to
9700 be set on the stub side (with Hg), since for all it knew,
9701 GENERAL_THREAD hadn't changed.
9702
9703 Notice that although in all-stop mode, the remote server always
9704 sets the current thread to the thread reporting the stop event,
9705 that doesn't happen in non-stop mode; in non-stop, the stub *must
9706 not* change the current thread when reporting a breakpoint hit,
9707 due to the decoupling of event reporting and event handling.
9708
9709 To keep things simple, we always invalidate our notion of the
9710 current thread. */
47f8a51d 9711 record_currthread (rs, minus_one_ptid);
dc1981d7 9712
8020350c 9713 /* Call common code to mark the inferior as not running. */
48aa3c27
PA
9714 generic_mourn_inferior ();
9715
d729566a 9716 if (!have_inferiors ())
2d717e4f 9717 {
82f73884
PA
9718 if (!remote_multi_process_p (rs))
9719 {
9720 /* Check whether the target is running now - some remote stubs
9721 automatically restart after kill. */
9722 putpkt ("?");
9723 getpkt (&rs->buf, &rs->buf_size, 0);
9724
9725 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
9726 {
3e43a32a
MS
9727 /* Assume that the target has been restarted. Set
9728 inferior_ptid so that bits of core GDB realizes
9729 there's something here, e.g., so that the user can
9730 say "kill" again. */
82f73884
PA
9731 inferior_ptid = magic_null_ptid;
9732 }
82f73884 9733 }
2d717e4f
DJ
9734 }
9735}
c906108c 9736
57810aa7 9737bool
f6ac5f3d 9738extended_remote_target::supports_disable_randomization ()
03583c20 9739{
4082afcc 9740 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
9741}
9742
9743static void
9744extended_remote_disable_randomization (int val)
9745{
9746 struct remote_state *rs = get_remote_state ();
9747 char *reply;
9748
bba74b36
YQ
9749 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
9750 val);
03583c20 9751 putpkt (rs->buf);
b6bb3468 9752 reply = remote_get_noisy_reply ();
03583c20
UW
9753 if (*reply == '\0')
9754 error (_("Target does not support QDisableRandomization."));
9755 if (strcmp (reply, "OK") != 0)
9756 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
9757}
9758
2d717e4f 9759static int
7c5ded6a 9760extended_remote_run (const std::string &args)
2d717e4f
DJ
9761{
9762 struct remote_state *rs = get_remote_state ();
2d717e4f 9763 int len;
94585166 9764 const char *remote_exec_file = get_remote_exec_file ();
c906108c 9765
2d717e4f
DJ
9766 /* If the user has disabled vRun support, or we have detected that
9767 support is not available, do not try it. */
4082afcc 9768 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 9769 return -1;
424163ea 9770
2d717e4f
DJ
9771 strcpy (rs->buf, "vRun;");
9772 len = strlen (rs->buf);
c906108c 9773
2d717e4f
DJ
9774 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
9775 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
9776 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
9777 strlen (remote_exec_file));
2d717e4f 9778
7c5ded6a 9779 if (!args.empty ())
2d717e4f 9780 {
2d717e4f 9781 int i;
2d717e4f 9782
773a1edc 9783 gdb_argv argv (args.c_str ());
2d717e4f
DJ
9784 for (i = 0; argv[i] != NULL; i++)
9785 {
9786 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
9787 error (_("Argument list too long for run packet"));
9788 rs->buf[len++] = ';';
9f1b45b0
TT
9789 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
9790 strlen (argv[i]));
2d717e4f 9791 }
2d717e4f
DJ
9792 }
9793
9794 rs->buf[len++] = '\0';
9795
9796 putpkt (rs->buf);
9797 getpkt (&rs->buf, &rs->buf_size, 0);
9798
4082afcc 9799 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 9800 {
4082afcc 9801 case PACKET_OK:
3405876a 9802 /* We have a wait response. All is well. */
2d717e4f 9803 return 0;
4082afcc
PA
9804 case PACKET_UNKNOWN:
9805 return -1;
9806 case PACKET_ERROR:
2d717e4f
DJ
9807 if (remote_exec_file[0] == '\0')
9808 error (_("Running the default executable on the remote target failed; "
9809 "try \"set remote exec-file\"?"));
9810 else
9811 error (_("Running \"%s\" on the remote target failed"),
9812 remote_exec_file);
4082afcc
PA
9813 default:
9814 gdb_assert_not_reached (_("bad switch"));
2d717e4f 9815 }
c906108c
SS
9816}
9817
0a2dde4a
SDJ
9818/* Helper function to send set/unset environment packets. ACTION is
9819 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
9820 or "QEnvironmentUnsetVariable". VALUE is the variable to be
9821 sent. */
9822
9823static void
9824send_environment_packet (struct remote_state *rs,
9825 const char *action,
9826 const char *packet,
9827 const char *value)
9828{
9829 /* Convert the environment variable to an hex string, which
9830 is the best format to be transmitted over the wire. */
9831 std::string encoded_value = bin2hex ((const gdb_byte *) value,
9832 strlen (value));
9833
9834 xsnprintf (rs->buf, get_remote_packet_size (),
9835 "%s:%s", packet, encoded_value.c_str ());
9836
9837 putpkt (rs->buf);
9838 getpkt (&rs->buf, &rs->buf_size, 0);
9839 if (strcmp (rs->buf, "OK") != 0)
9840 warning (_("Unable to %s environment variable '%s' on remote."),
9841 action, value);
9842}
9843
9844/* Helper function to handle the QEnvironment* packets. */
9845
9846static void
9847extended_remote_environment_support (struct remote_state *rs)
9848{
9849 if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
9850 {
9851 putpkt ("QEnvironmentReset");
9852 getpkt (&rs->buf, &rs->buf_size, 0);
9853 if (strcmp (rs->buf, "OK") != 0)
9854 warning (_("Unable to reset environment on remote."));
9855 }
9856
9857 gdb_environ *e = &current_inferior ()->environment;
9858
9859 if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
9860 for (const std::string &el : e->user_set_env ())
9861 send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
9862 el.c_str ());
9863
9864 if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
9865 for (const std::string &el : e->user_unset_env ())
9866 send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
9867}
9868
bc3b087d
SDJ
9869/* Helper function to set the current working directory for the
9870 inferior in the remote target. */
9871
9872static void
9873extended_remote_set_inferior_cwd (struct remote_state *rs)
9874{
9875 if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
9876 {
9877 const char *inferior_cwd = get_inferior_cwd ();
9878
9879 if (inferior_cwd != NULL)
9880 {
9881 std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
9882 strlen (inferior_cwd));
9883
9884 xsnprintf (rs->buf, get_remote_packet_size (),
9885 "QSetWorkingDir:%s", hexpath.c_str ());
9886 }
9887 else
9888 {
9889 /* An empty inferior_cwd means that the user wants us to
9890 reset the remote server's inferior's cwd. */
9891 xsnprintf (rs->buf, get_remote_packet_size (),
9892 "QSetWorkingDir:");
9893 }
9894
9895 putpkt (rs->buf);
9896 getpkt (&rs->buf, &rs->buf_size, 0);
9897 if (packet_ok (rs->buf,
9898 &remote_protocol_packets[PACKET_QSetWorkingDir])
9899 != PACKET_OK)
9900 error (_("\
9901Remote replied unexpectedly while setting the inferior's working\n\
9902directory: %s"),
9903 rs->buf);
9904
9905 }
9906}
9907
2d717e4f
DJ
9908/* In the extended protocol we want to be able to do things like
9909 "run" and have them basically work as expected. So we need
9910 a special create_inferior function. We support changing the
9911 executable file and the command line arguments, but not the
9912 environment. */
9913
f6ac5f3d
PA
9914void
9915extended_remote_target::create_inferior (const char *exec_file,
9916 const std::string &args,
9917 char **env, int from_tty)
43ff13b4 9918{
3405876a
PA
9919 int run_worked;
9920 char *stop_reply;
9921 struct remote_state *rs = get_remote_state ();
94585166 9922 const char *remote_exec_file = get_remote_exec_file ();
3405876a 9923
43ff13b4 9924 /* If running asynchronously, register the target file descriptor
23860348 9925 with the event loop. */
75c99385 9926 if (target_can_async_p ())
6a3753b3 9927 target_async (1);
43ff13b4 9928
03583c20 9929 /* Disable address space randomization if requested (and supported). */
f6ac5f3d 9930 if (supports_disable_randomization ())
03583c20
UW
9931 extended_remote_disable_randomization (disable_randomization);
9932
aefd8b33
SDJ
9933 /* If startup-with-shell is on, we inform gdbserver to start the
9934 remote inferior using a shell. */
9935 if (packet_support (PACKET_QStartupWithShell) != PACKET_DISABLE)
9936 {
9937 xsnprintf (rs->buf, get_remote_packet_size (),
9938 "QStartupWithShell:%d", startup_with_shell ? 1 : 0);
9939 putpkt (rs->buf);
9940 getpkt (&rs->buf, &rs->buf_size, 0);
9941 if (strcmp (rs->buf, "OK") != 0)
9942 error (_("\
9943Remote replied unexpectedly while setting startup-with-shell: %s"),
9944 rs->buf);
9945 }
9946
0a2dde4a
SDJ
9947 extended_remote_environment_support (rs);
9948
bc3b087d
SDJ
9949 extended_remote_set_inferior_cwd (rs);
9950
43ff13b4 9951 /* Now restart the remote server. */
3405876a
PA
9952 run_worked = extended_remote_run (args) != -1;
9953 if (!run_worked)
2d717e4f
DJ
9954 {
9955 /* vRun was not supported. Fail if we need it to do what the
9956 user requested. */
9957 if (remote_exec_file[0])
9958 error (_("Remote target does not support \"set remote exec-file\""));
7c5ded6a 9959 if (!args.empty ())
2d717e4f 9960 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 9961
2d717e4f
DJ
9962 /* Fall back to "R". */
9963 extended_remote_restart ();
9964 }
424163ea 9965
6c95b8df
PA
9966 if (!have_inferiors ())
9967 {
9968 /* Clean up from the last time we ran, before we mark the target
9969 running again. This will mark breakpoints uninserted, and
9970 get_offsets may insert breakpoints. */
9971 init_thread_list ();
9972 init_wait_for_inferior ();
9973 }
45280a52 9974
3405876a
PA
9975 /* vRun's success return is a stop reply. */
9976 stop_reply = run_worked ? rs->buf : NULL;
9977 add_current_inferior_and_thread (stop_reply);
c0a2216e 9978
2d717e4f
DJ
9979 /* Get updated offsets, if the stub uses qOffsets. */
9980 get_offsets ();
2d717e4f 9981}
c906108c 9982\f
c5aa993b 9983
b775012e
LM
9984/* Given a location's target info BP_TGT and the packet buffer BUF, output
9985 the list of conditions (in agent expression bytecode format), if any, the
9986 target needs to evaluate. The output is placed into the packet buffer
bba74b36 9987 started from BUF and ended at BUF_END. */
b775012e
LM
9988
9989static int
9990remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
9991 struct bp_target_info *bp_tgt, char *buf,
9992 char *buf_end)
b775012e 9993{
3cde5c42 9994 if (bp_tgt->conditions.empty ())
b775012e
LM
9995 return 0;
9996
9997 buf += strlen (buf);
bba74b36 9998 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
9999 buf++;
10000
83621223 10001 /* Send conditions to the target. */
d538e36d 10002 for (agent_expr *aexpr : bp_tgt->conditions)
b775012e 10003 {
bba74b36 10004 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e 10005 buf += strlen (buf);
3cde5c42 10006 for (int i = 0; i < aexpr->len; ++i)
b775012e
LM
10007 buf = pack_hex_byte (buf, aexpr->buf[i]);
10008 *buf = '\0';
10009 }
b775012e
LM
10010 return 0;
10011}
10012
d3ce09f5
SS
10013static void
10014remote_add_target_side_commands (struct gdbarch *gdbarch,
10015 struct bp_target_info *bp_tgt, char *buf)
10016{
3cde5c42 10017 if (bp_tgt->tcommands.empty ())
d3ce09f5
SS
10018 return;
10019
10020 buf += strlen (buf);
10021
10022 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
10023 buf += strlen (buf);
10024
10025 /* Concatenate all the agent expressions that are commands into the
10026 cmds parameter. */
df97be55 10027 for (agent_expr *aexpr : bp_tgt->tcommands)
d3ce09f5
SS
10028 {
10029 sprintf (buf, "X%x,", aexpr->len);
10030 buf += strlen (buf);
3cde5c42 10031 for (int i = 0; i < aexpr->len; ++i)
d3ce09f5
SS
10032 buf = pack_hex_byte (buf, aexpr->buf[i]);
10033 *buf = '\0';
10034 }
d3ce09f5
SS
10035}
10036
8181d85f
DJ
10037/* Insert a breakpoint. On targets that have software breakpoint
10038 support, we ask the remote target to do the work; on targets
10039 which don't, we insert a traditional memory breakpoint. */
c906108c 10040
f6ac5f3d
PA
10041int
10042remote_target::insert_breakpoint (struct gdbarch *gdbarch,
10043 struct bp_target_info *bp_tgt)
c906108c 10044{
d471ea57
AC
10045 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10046 If it succeeds, then set the support to PACKET_ENABLE. If it
10047 fails, and the user has explicitly requested the Z support then
23860348 10048 report an error, otherwise, mark it disabled and go on. */
802188a7 10049
4082afcc 10050 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 10051 {
0d5ed153 10052 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10053 struct remote_state *rs;
bba74b36 10054 char *p, *endbuf;
4fff2411 10055
28439a30
PA
10056 /* Make sure the remote is pointing at the right process, if
10057 necessary. */
10058 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10059 set_general_process ();
10060
4fff2411
JZ
10061 rs = get_remote_state ();
10062 p = rs->buf;
bba74b36 10063 endbuf = rs->buf + get_remote_packet_size ();
802188a7 10064
96baa820
JM
10065 *(p++) = 'Z';
10066 *(p++) = '0';
10067 *(p++) = ',';
7c0f6dcc 10068 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 10069 p += hexnumstr (p, addr);
579c6ad9 10070 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 10071
f6ac5f3d 10072 if (supports_evaluation_of_breakpoint_conditions ())
bba74b36 10073 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10074
f6ac5f3d 10075 if (can_run_breakpoint_commands ())
d3ce09f5
SS
10076 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10077
6d820c5c
DJ
10078 putpkt (rs->buf);
10079 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10080
6d820c5c 10081 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 10082 {
d471ea57
AC
10083 case PACKET_ERROR:
10084 return -1;
10085 case PACKET_OK:
10086 return 0;
10087 case PACKET_UNKNOWN:
10088 break;
96baa820
JM
10089 }
10090 }
c906108c 10091
0000e5cc
PA
10092 /* If this breakpoint has target-side commands but this stub doesn't
10093 support Z0 packets, throw error. */
3cde5c42 10094 if (!bp_tgt->tcommands.empty ())
0000e5cc
PA
10095 throw_error (NOT_SUPPORTED_ERROR, _("\
10096Target doesn't support breakpoints that have target side commands."));
10097
f6ac5f3d 10098 return memory_insert_breakpoint (this, gdbarch, bp_tgt);
c906108c
SS
10099}
10100
f6ac5f3d
PA
10101int
10102remote_target::remove_breakpoint (struct gdbarch *gdbarch,
10103 struct bp_target_info *bp_tgt,
10104 enum remove_bp_reason reason)
c906108c 10105{
8181d85f 10106 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 10107 struct remote_state *rs = get_remote_state ();
96baa820 10108
4082afcc 10109 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 10110 {
6d820c5c 10111 char *p = rs->buf;
bba74b36 10112 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 10113
28439a30
PA
10114 /* Make sure the remote is pointing at the right process, if
10115 necessary. */
10116 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10117 set_general_process ();
10118
96baa820
JM
10119 *(p++) = 'z';
10120 *(p++) = '0';
10121 *(p++) = ',';
10122
8181d85f
DJ
10123 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
10124 p += hexnumstr (p, addr);
579c6ad9 10125 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 10126
6d820c5c
DJ
10127 putpkt (rs->buf);
10128 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10129
6d820c5c 10130 return (rs->buf[0] == 'E');
96baa820
JM
10131 }
10132
f6ac5f3d 10133 return memory_remove_breakpoint (this, gdbarch, bp_tgt, reason);
c906108c
SS
10134}
10135
f486487f 10136static enum Z_packet_type
d471ea57
AC
10137watchpoint_to_Z_packet (int type)
10138{
10139 switch (type)
10140 {
10141 case hw_write:
bb858e6a 10142 return Z_PACKET_WRITE_WP;
d471ea57
AC
10143 break;
10144 case hw_read:
bb858e6a 10145 return Z_PACKET_READ_WP;
d471ea57
AC
10146 break;
10147 case hw_access:
bb858e6a 10148 return Z_PACKET_ACCESS_WP;
d471ea57
AC
10149 break;
10150 default:
8e65ff28 10151 internal_error (__FILE__, __LINE__,
e2e0b3e5 10152 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
10153 }
10154}
10155
f6ac5f3d
PA
10156int
10157remote_target::insert_watchpoint (CORE_ADDR addr, int len,
10158 enum target_hw_bp_type type, struct expression *cond)
96baa820 10159{
d01949b6 10160 struct remote_state *rs = get_remote_state ();
bba74b36 10161 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 10162 char *p;
d471ea57 10163 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 10164
4082afcc 10165 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 10166 return 1;
802188a7 10167
28439a30
PA
10168 /* Make sure the remote is pointing at the right process, if
10169 necessary. */
10170 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10171 set_general_process ();
10172
bba74b36 10173 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 10174 p = strchr (rs->buf, '\0');
96baa820
JM
10175 addr = remote_address_masked (addr);
10176 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10177 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 10178
6d820c5c
DJ
10179 putpkt (rs->buf);
10180 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10181
6d820c5c 10182 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10183 {
10184 case PACKET_ERROR:
d471ea57 10185 return -1;
85d721b8
PA
10186 case PACKET_UNKNOWN:
10187 return 1;
d471ea57
AC
10188 case PACKET_OK:
10189 return 0;
10190 }
8e65ff28 10191 internal_error (__FILE__, __LINE__,
e2e0b3e5 10192 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
10193}
10194
57810aa7 10195bool
f6ac5f3d
PA
10196remote_target::watchpoint_addr_within_range (CORE_ADDR addr,
10197 CORE_ADDR start, int length)
283002cf
MR
10198{
10199 CORE_ADDR diff = remote_address_masked (addr - start);
10200
10201 return diff < length;
10202}
10203
d471ea57 10204
f6ac5f3d
PA
10205int
10206remote_target::remove_watchpoint (CORE_ADDR addr, int len,
10207 enum target_hw_bp_type type, struct expression *cond)
96baa820 10208{
d01949b6 10209 struct remote_state *rs = get_remote_state ();
bba74b36 10210 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 10211 char *p;
d471ea57
AC
10212 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
10213
4082afcc 10214 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 10215 return -1;
802188a7 10216
28439a30
PA
10217 /* Make sure the remote is pointing at the right process, if
10218 necessary. */
10219 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10220 set_general_process ();
10221
bba74b36 10222 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 10223 p = strchr (rs->buf, '\0');
96baa820
JM
10224 addr = remote_address_masked (addr);
10225 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10226 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
10227 putpkt (rs->buf);
10228 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10229
6d820c5c 10230 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10231 {
10232 case PACKET_ERROR:
10233 case PACKET_UNKNOWN:
10234 return -1;
10235 case PACKET_OK:
10236 return 0;
10237 }
8e65ff28 10238 internal_error (__FILE__, __LINE__,
e2e0b3e5 10239 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
10240}
10241
3c3bea1c 10242
501eef12 10243int remote_hw_watchpoint_limit = -1;
480a3f21 10244int remote_hw_watchpoint_length_limit = -1;
501eef12 10245int remote_hw_breakpoint_limit = -1;
d471ea57 10246
f6ac5f3d
PA
10247int
10248remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
480a3f21
PW
10249{
10250 if (remote_hw_watchpoint_length_limit == 0)
10251 return 0;
10252 else if (remote_hw_watchpoint_length_limit < 0)
10253 return 1;
10254 else if (len <= remote_hw_watchpoint_length_limit)
10255 return 1;
10256 else
10257 return 0;
10258}
10259
f6ac5f3d
PA
10260int
10261remote_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
96baa820 10262{
3c3bea1c
GS
10263 if (type == bp_hardware_breakpoint)
10264 {
10265 if (remote_hw_breakpoint_limit == 0)
10266 return 0;
501eef12
AC
10267 else if (remote_hw_breakpoint_limit < 0)
10268 return 1;
3c3bea1c
GS
10269 else if (cnt <= remote_hw_breakpoint_limit)
10270 return 1;
10271 }
10272 else
10273 {
10274 if (remote_hw_watchpoint_limit == 0)
10275 return 0;
501eef12
AC
10276 else if (remote_hw_watchpoint_limit < 0)
10277 return 1;
3c3bea1c
GS
10278 else if (ot)
10279 return -1;
10280 else if (cnt <= remote_hw_watchpoint_limit)
10281 return 1;
10282 }
10283 return -1;
10284}
10285
f7e6eed5
PA
10286/* The to_stopped_by_sw_breakpoint method of target remote. */
10287
57810aa7 10288bool
f6ac5f3d 10289remote_target::stopped_by_sw_breakpoint ()
f7e6eed5 10290{
799a2abe 10291 struct thread_info *thread = inferior_thread ();
f7e6eed5 10292
799a2abe 10293 return (thread->priv != NULL
7aabaf9d
SM
10294 && (get_remote_thread_info (thread)->stop_reason
10295 == TARGET_STOPPED_BY_SW_BREAKPOINT));
f7e6eed5
PA
10296}
10297
10298/* The to_supports_stopped_by_sw_breakpoint method of target
10299 remote. */
10300
57810aa7 10301bool
f6ac5f3d 10302remote_target::supports_stopped_by_sw_breakpoint ()
f7e6eed5 10303{
f7e6eed5
PA
10304 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
10305}
10306
10307/* The to_stopped_by_hw_breakpoint method of target remote. */
10308
57810aa7 10309bool
f6ac5f3d 10310remote_target::stopped_by_hw_breakpoint ()
f7e6eed5 10311{
799a2abe 10312 struct thread_info *thread = inferior_thread ();
f7e6eed5 10313
799a2abe 10314 return (thread->priv != NULL
7aabaf9d
SM
10315 && (get_remote_thread_info (thread)->stop_reason
10316 == TARGET_STOPPED_BY_HW_BREAKPOINT));
f7e6eed5
PA
10317}
10318
10319/* The to_supports_stopped_by_hw_breakpoint method of target
10320 remote. */
10321
57810aa7 10322bool
f6ac5f3d 10323remote_target::supports_stopped_by_hw_breakpoint ()
f7e6eed5 10324{
f7e6eed5
PA
10325 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
10326}
10327
57810aa7 10328bool
f6ac5f3d 10329remote_target::stopped_by_watchpoint ()
3c3bea1c 10330{
799a2abe 10331 struct thread_info *thread = inferior_thread ();
ee154bee 10332
799a2abe 10333 return (thread->priv != NULL
7aabaf9d
SM
10334 && (get_remote_thread_info (thread)->stop_reason
10335 == TARGET_STOPPED_BY_WATCHPOINT));
3c3bea1c
GS
10336}
10337
57810aa7 10338bool
f6ac5f3d 10339remote_target::stopped_data_address (CORE_ADDR *addr_p)
3c3bea1c 10340{
799a2abe 10341 struct thread_info *thread = inferior_thread ();
a744cf53 10342
799a2abe 10343 if (thread->priv != NULL
7aabaf9d
SM
10344 && (get_remote_thread_info (thread)->stop_reason
10345 == TARGET_STOPPED_BY_WATCHPOINT))
4aa7a7f5 10346 {
7aabaf9d 10347 *addr_p = get_remote_thread_info (thread)->watch_data_address;
57810aa7 10348 return true;
4aa7a7f5
JJ
10349 }
10350
57810aa7 10351 return false;
3c3bea1c
GS
10352}
10353
10354
f6ac5f3d
PA
10355int
10356remote_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
10357 struct bp_target_info *bp_tgt)
3c3bea1c 10358{
0d5ed153 10359 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10360 struct remote_state *rs;
bba74b36 10361 char *p, *endbuf;
dd61ec5c 10362 char *message;
3c3bea1c 10363
4082afcc 10364 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10365 return -1;
2bc416ba 10366
28439a30
PA
10367 /* Make sure the remote is pointing at the right process, if
10368 necessary. */
10369 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10370 set_general_process ();
10371
4fff2411
JZ
10372 rs = get_remote_state ();
10373 p = rs->buf;
bba74b36 10374 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 10375
96baa820
JM
10376 *(p++) = 'Z';
10377 *(p++) = '1';
10378 *(p++) = ',';
802188a7 10379
0d5ed153 10380 addr = remote_address_masked (addr);
96baa820 10381 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10382 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10383
f6ac5f3d 10384 if (supports_evaluation_of_breakpoint_conditions ())
bba74b36 10385 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10386
f6ac5f3d 10387 if (can_run_breakpoint_commands ())
d3ce09f5
SS
10388 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10389
6d820c5c
DJ
10390 putpkt (rs->buf);
10391 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10392
6d820c5c 10393 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10394 {
10395 case PACKET_ERROR:
dd61ec5c
MW
10396 if (rs->buf[1] == '.')
10397 {
10398 message = strchr (rs->buf + 2, '.');
10399 if (message)
0316657e 10400 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
10401 }
10402 return -1;
d471ea57
AC
10403 case PACKET_UNKNOWN:
10404 return -1;
10405 case PACKET_OK:
10406 return 0;
10407 }
8e65ff28 10408 internal_error (__FILE__, __LINE__,
e2e0b3e5 10409 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
10410}
10411
d471ea57 10412
f6ac5f3d
PA
10413int
10414remote_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
10415 struct bp_target_info *bp_tgt)
96baa820 10416{
8181d85f 10417 CORE_ADDR addr;
d01949b6 10418 struct remote_state *rs = get_remote_state ();
6d820c5c 10419 char *p = rs->buf;
bba74b36 10420 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 10421
4082afcc 10422 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10423 return -1;
802188a7 10424
28439a30
PA
10425 /* Make sure the remote is pointing at the right process, if
10426 necessary. */
10427 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10428 set_general_process ();
10429
96baa820
JM
10430 *(p++) = 'z';
10431 *(p++) = '1';
10432 *(p++) = ',';
802188a7 10433
8181d85f 10434 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 10435 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10436 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10437
6d820c5c
DJ
10438 putpkt (rs->buf);
10439 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 10440
6d820c5c 10441 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10442 {
10443 case PACKET_ERROR:
10444 case PACKET_UNKNOWN:
10445 return -1;
10446 case PACKET_OK:
10447 return 0;
10448 }
8e65ff28 10449 internal_error (__FILE__, __LINE__,
e2e0b3e5 10450 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 10451}
96baa820 10452
4a5e7a5b
PA
10453/* Verify memory using the "qCRC:" request. */
10454
f6ac5f3d
PA
10455int
10456remote_target::verify_memory (const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
4a5e7a5b
PA
10457{
10458 struct remote_state *rs = get_remote_state ();
10459 unsigned long host_crc, target_crc;
10460 char *tmp;
10461
936d2992
PA
10462 /* It doesn't make sense to use qCRC if the remote target is
10463 connected but not running. */
10464 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
10465 {
10466 enum packet_result result;
28439a30 10467
936d2992
PA
10468 /* Make sure the remote is pointing at the right process. */
10469 set_general_process ();
4a5e7a5b 10470
936d2992
PA
10471 /* FIXME: assumes lma can fit into long. */
10472 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
10473 (long) lma, (long) size);
10474 putpkt (rs->buf);
4a5e7a5b 10475
936d2992
PA
10476 /* Be clever; compute the host_crc before waiting for target
10477 reply. */
10478 host_crc = xcrc32 (data, size, 0xffffffff);
10479
10480 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 10481
936d2992
PA
10482 result = packet_ok (rs->buf,
10483 &remote_protocol_packets[PACKET_qCRC]);
10484 if (result == PACKET_ERROR)
10485 return -1;
10486 else if (result == PACKET_OK)
10487 {
10488 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
10489 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 10490
936d2992
PA
10491 return (host_crc == target_crc);
10492 }
10493 }
4a5e7a5b 10494
f6ac5f3d 10495 return simple_verify_memory (this, data, lma, size);
4a5e7a5b
PA
10496}
10497
c906108c
SS
10498/* compare-sections command
10499
10500 With no arguments, compares each loadable section in the exec bfd
10501 with the same memory range on the target, and reports mismatches.
4a5e7a5b 10502 Useful for verifying the image on the target against the exec file. */
e514a9d6 10503
c906108c 10504static void
ac88e2de 10505compare_sections_command (const char *args, int from_tty)
c906108c
SS
10506{
10507 asection *s;
ce359b09 10508 const char *sectname;
c906108c
SS
10509 bfd_size_type size;
10510 bfd_vma lma;
10511 int matched = 0;
10512 int mismatched = 0;
4a5e7a5b 10513 int res;
95cf3b38 10514 int read_only = 0;
c906108c
SS
10515
10516 if (!exec_bfd)
8a3fe4f8 10517 error (_("command cannot be used without an exec file"));
c906108c 10518
95cf3b38
DT
10519 if (args != NULL && strcmp (args, "-r") == 0)
10520 {
10521 read_only = 1;
10522 args = NULL;
10523 }
10524
c5aa993b 10525 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
10526 {
10527 if (!(s->flags & SEC_LOAD))
0df8b418 10528 continue; /* Skip non-loadable section. */
c906108c 10529
95cf3b38
DT
10530 if (read_only && (s->flags & SEC_READONLY) == 0)
10531 continue; /* Skip writeable sections */
10532
2c500098 10533 size = bfd_get_section_size (s);
c906108c 10534 if (size == 0)
0df8b418 10535 continue; /* Skip zero-length section. */
c906108c 10536
ce359b09 10537 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 10538 if (args && strcmp (args, sectname) != 0)
0df8b418 10539 continue; /* Not the section selected by user. */
c906108c 10540
0df8b418 10541 matched = 1; /* Do this section. */
c906108c 10542 lma = s->lma;
c906108c 10543
b80406ac
TT
10544 gdb::byte_vector sectdata (size);
10545 bfd_get_section_contents (exec_bfd, s, sectdata.data (), 0, size);
c906108c 10546
b80406ac 10547 res = target_verify_memory (sectdata.data (), lma, size);
4a5e7a5b
PA
10548
10549 if (res == -1)
5af949e3 10550 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
10551 paddress (target_gdbarch (), lma),
10552 paddress (target_gdbarch (), lma + size));
c906108c 10553
5af949e3 10554 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
10555 paddress (target_gdbarch (), lma),
10556 paddress (target_gdbarch (), lma + size));
4a5e7a5b 10557 if (res)
c906108c
SS
10558 printf_filtered ("matched.\n");
10559 else
c5aa993b
JM
10560 {
10561 printf_filtered ("MIS-MATCHED!\n");
10562 mismatched++;
10563 }
c906108c
SS
10564 }
10565 if (mismatched > 0)
936d2992 10566 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 10567the loaded file\n"));
c906108c 10568 if (args && !matched)
a3f17187 10569 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
10570}
10571
0e7f50da
UW
10572/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
10573 into remote target. The number of bytes written to the remote
10574 target is returned, or -1 for error. */
10575
9b409511 10576static enum target_xfer_status
f6ac5f3d 10577remote_write_qxfer (const char *object_name,
0e7f50da 10578 const char *annex, const gdb_byte *writebuf,
9b409511 10579 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
10580 struct packet_config *packet)
10581{
10582 int i, buf_len;
10583 ULONGEST n;
0e7f50da
UW
10584 struct remote_state *rs = get_remote_state ();
10585 int max_size = get_memory_write_packet_size ();
10586
7cc244de 10587 if (packet_config_support (packet) == PACKET_DISABLE)
2ed4b548 10588 return TARGET_XFER_E_IO;
0e7f50da
UW
10589
10590 /* Insert header. */
10591 i = snprintf (rs->buf, max_size,
10592 "qXfer:%s:write:%s:%s:",
10593 object_name, annex ? annex : "",
10594 phex_nz (offset, sizeof offset));
10595 max_size -= (i + 1);
10596
10597 /* Escape as much data as fits into rs->buf. */
10598 buf_len = remote_escape_output
124e13d9 10599 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
10600
10601 if (putpkt_binary (rs->buf, i + buf_len) < 0
10602 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10603 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10604 return TARGET_XFER_E_IO;
0e7f50da
UW
10605
10606 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
10607
10608 *xfered_len = n;
92ffd475 10609 return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
0e7f50da
UW
10610}
10611
0876f84a
DJ
10612/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
10613 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
10614 number of bytes read is returned, or 0 for EOF, or -1 for error.
10615 The number of bytes read may be less than LEN without indicating an
10616 EOF. PACKET is checked and updated to indicate whether the remote
10617 target supports this object. */
10618
9b409511 10619static enum target_xfer_status
f6ac5f3d 10620remote_read_qxfer (const char *object_name,
0876f84a
DJ
10621 const char *annex,
10622 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 10623 ULONGEST *xfered_len,
0876f84a
DJ
10624 struct packet_config *packet)
10625{
0876f84a 10626 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
10627 LONGEST i, n, packet_len;
10628
7cc244de 10629 if (packet_config_support (packet) == PACKET_DISABLE)
2ed4b548 10630 return TARGET_XFER_E_IO;
0876f84a
DJ
10631
10632 /* Check whether we've cached an end-of-object packet that matches
10633 this request. */
8e88304f 10634 if (rs->finished_object)
0876f84a 10635 {
8e88304f
TT
10636 if (strcmp (object_name, rs->finished_object) == 0
10637 && strcmp (annex ? annex : "", rs->finished_annex) == 0
10638 && offset == rs->finished_offset)
9b409511
YQ
10639 return TARGET_XFER_EOF;
10640
0876f84a
DJ
10641
10642 /* Otherwise, we're now reading something different. Discard
10643 the cache. */
8e88304f
TT
10644 xfree (rs->finished_object);
10645 xfree (rs->finished_annex);
10646 rs->finished_object = NULL;
10647 rs->finished_annex = NULL;
0876f84a
DJ
10648 }
10649
10650 /* Request only enough to fit in a single packet. The actual data
10651 may not, since we don't know how much of it will need to be escaped;
10652 the target is free to respond with slightly less data. We subtract
10653 five to account for the response type and the protocol frame. */
768adc05 10654 n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
0876f84a
DJ
10655 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
10656 object_name, annex ? annex : "",
10657 phex_nz (offset, sizeof offset),
10658 phex_nz (n, sizeof n));
10659 i = putpkt (rs->buf);
10660 if (i < 0)
2ed4b548 10661 return TARGET_XFER_E_IO;
0876f84a
DJ
10662
10663 rs->buf[0] = '\0';
10664 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10665 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10666 return TARGET_XFER_E_IO;
0876f84a
DJ
10667
10668 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
10669 error (_("Unknown remote qXfer reply: %s"), rs->buf);
10670
10671 /* 'm' means there is (or at least might be) more data after this
10672 batch. That does not make sense unless there's at least one byte
10673 of data in this reply. */
10674 if (rs->buf[0] == 'm' && packet_len == 1)
10675 error (_("Remote qXfer reply contained no data."));
10676
10677 /* Got some data. */
bc20a4af
PA
10678 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
10679 packet_len - 1, readbuf, n);
0876f84a
DJ
10680
10681 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
10682 or possibly empty. If we have the final block of a non-empty
10683 object, record this fact to bypass a subsequent partial read. */
10684 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 10685 {
8e88304f
TT
10686 rs->finished_object = xstrdup (object_name);
10687 rs->finished_annex = xstrdup (annex ? annex : "");
10688 rs->finished_offset = offset + i;
0876f84a
DJ
10689 }
10690
9b409511
YQ
10691 if (i == 0)
10692 return TARGET_XFER_EOF;
10693 else
10694 {
10695 *xfered_len = i;
10696 return TARGET_XFER_OK;
10697 }
0876f84a
DJ
10698}
10699
f6ac5f3d
PA
10700enum target_xfer_status
10701remote_target::xfer_partial (enum target_object object,
10702 const char *annex, gdb_byte *readbuf,
10703 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
10704 ULONGEST *xfered_len)
c906108c 10705{
82f73884 10706 struct remote_state *rs;
c906108c 10707 int i;
6d820c5c 10708 char *p2;
1e3ff5ad 10709 char query_type;
124e13d9 10710 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 10711
e6e4e701 10712 set_remote_traceframe ();
82f73884
PA
10713 set_general_thread (inferior_ptid);
10714
10715 rs = get_remote_state ();
10716
b2182ed2 10717 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
10718 if (object == TARGET_OBJECT_MEMORY)
10719 {
2d717e4f
DJ
10720 /* If the remote target is connected but not running, we should
10721 pass this request down to a lower stratum (e.g. the executable
10722 file). */
10723 if (!target_has_execution)
9b409511 10724 return TARGET_XFER_EOF;
2d717e4f 10725
21e3b9b9 10726 if (writebuf != NULL)
124e13d9
SM
10727 return remote_write_bytes (offset, writebuf, len, unit_size,
10728 xfered_len);
21e3b9b9 10729 else
f6ac5f3d 10730 return remote_read_bytes (this, offset, readbuf, len, unit_size,
124e13d9 10731 xfered_len);
21e3b9b9
DJ
10732 }
10733
0df8b418 10734 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
10735 if (object == TARGET_OBJECT_SPU)
10736 {
10737 if (readbuf)
f6ac5f3d 10738 return remote_read_qxfer ("spu", annex, readbuf, offset, len,
9b409511
YQ
10739 xfered_len, &remote_protocol_packets
10740 [PACKET_qXfer_spu_read]);
0e7f50da 10741 else
f6ac5f3d 10742 return remote_write_qxfer ("spu", annex, writebuf, offset, len,
9b409511
YQ
10743 xfered_len, &remote_protocol_packets
10744 [PACKET_qXfer_spu_write]);
0e7f50da
UW
10745 }
10746
4aa995e1
PA
10747 /* Handle extra signal info using qxfer packets. */
10748 if (object == TARGET_OBJECT_SIGNAL_INFO)
10749 {
10750 if (readbuf)
f6ac5f3d 10751 return remote_read_qxfer ("siginfo", annex, readbuf, offset, len,
9b409511 10752 xfered_len, &remote_protocol_packets
4aa995e1
PA
10753 [PACKET_qXfer_siginfo_read]);
10754 else
f6ac5f3d 10755 return remote_write_qxfer ("siginfo", annex,
9b409511 10756 writebuf, offset, len, xfered_len,
4aa995e1
PA
10757 &remote_protocol_packets
10758 [PACKET_qXfer_siginfo_write]);
10759 }
10760
0fb4aa4b
PA
10761 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
10762 {
10763 if (readbuf)
f6ac5f3d 10764 return remote_read_qxfer ("statictrace", annex,
9b409511 10765 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
10766 &remote_protocol_packets
10767 [PACKET_qXfer_statictrace_read]);
10768 else
2ed4b548 10769 return TARGET_XFER_E_IO;
0fb4aa4b
PA
10770 }
10771
a76d924d
DJ
10772 /* Only handle flash writes. */
10773 if (writebuf != NULL)
10774 {
a76d924d
DJ
10775 switch (object)
10776 {
10777 case TARGET_OBJECT_FLASH:
f6ac5f3d 10778 return remote_flash_write (this, offset, len, xfered_len,
9b409511 10779 writebuf);
a76d924d
DJ
10780
10781 default:
2ed4b548 10782 return TARGET_XFER_E_IO;
a76d924d
DJ
10783 }
10784 }
4b8a223f 10785
1e3ff5ad
AC
10786 /* Map pre-existing objects onto letters. DO NOT do this for new
10787 objects!!! Instead specify new query packets. */
10788 switch (object)
c906108c 10789 {
1e3ff5ad
AC
10790 case TARGET_OBJECT_AVR:
10791 query_type = 'R';
10792 break;
802188a7
RM
10793
10794 case TARGET_OBJECT_AUXV:
0876f84a 10795 gdb_assert (annex == NULL);
f6ac5f3d 10796 return remote_read_qxfer ("auxv", annex, readbuf, offset, len,
9b409511 10797 xfered_len,
0876f84a 10798 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 10799
23181151
DJ
10800 case TARGET_OBJECT_AVAILABLE_FEATURES:
10801 return remote_read_qxfer
f6ac5f3d 10802 ("features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
10803 &remote_protocol_packets[PACKET_qXfer_features]);
10804
cfa9d6d9
DJ
10805 case TARGET_OBJECT_LIBRARIES:
10806 return remote_read_qxfer
f6ac5f3d 10807 ("libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
10808 &remote_protocol_packets[PACKET_qXfer_libraries]);
10809
2268b414
JK
10810 case TARGET_OBJECT_LIBRARIES_SVR4:
10811 return remote_read_qxfer
f6ac5f3d 10812 ("libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
10813 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
10814
fd79ecee
DJ
10815 case TARGET_OBJECT_MEMORY_MAP:
10816 gdb_assert (annex == NULL);
f6ac5f3d 10817 return remote_read_qxfer ("memory-map", annex, readbuf, offset, len,
9b409511 10818 xfered_len,
fd79ecee
DJ
10819 &remote_protocol_packets[PACKET_qXfer_memory_map]);
10820
07e059b5
VP
10821 case TARGET_OBJECT_OSDATA:
10822 /* Should only get here if we're connected. */
5d93a237 10823 gdb_assert (rs->remote_desc);
07e059b5 10824 return remote_read_qxfer
f6ac5f3d 10825 ("osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
10826 &remote_protocol_packets[PACKET_qXfer_osdata]);
10827
dc146f7c
VP
10828 case TARGET_OBJECT_THREADS:
10829 gdb_assert (annex == NULL);
f6ac5f3d 10830 return remote_read_qxfer ("threads", annex, readbuf, offset, len,
9b409511 10831 xfered_len,
dc146f7c
VP
10832 &remote_protocol_packets[PACKET_qXfer_threads]);
10833
b3b9301e
PA
10834 case TARGET_OBJECT_TRACEFRAME_INFO:
10835 gdb_assert (annex == NULL);
10836 return remote_read_qxfer
f6ac5f3d 10837 ("traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 10838 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
10839
10840 case TARGET_OBJECT_FDPIC:
f6ac5f3d 10841 return remote_read_qxfer ("fdpic", annex, readbuf, offset, len,
9b409511 10842 xfered_len,
78d85199 10843 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
10844
10845 case TARGET_OBJECT_OPENVMS_UIB:
f6ac5f3d 10846 return remote_read_qxfer ("uib", annex, readbuf, offset, len,
9b409511 10847 xfered_len,
169081d0
TG
10848 &remote_protocol_packets[PACKET_qXfer_uib]);
10849
9accd112 10850 case TARGET_OBJECT_BTRACE:
f6ac5f3d 10851 return remote_read_qxfer ("btrace", annex, readbuf, offset, len,
9b409511 10852 xfered_len,
9accd112
MM
10853 &remote_protocol_packets[PACKET_qXfer_btrace]);
10854
f4abbc16 10855 case TARGET_OBJECT_BTRACE_CONF:
f6ac5f3d 10856 return remote_read_qxfer ("btrace-conf", annex, readbuf, offset,
f4abbc16
MM
10857 len, xfered_len,
10858 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
10859
c78fa86a 10860 case TARGET_OBJECT_EXEC_FILE:
f6ac5f3d 10861 return remote_read_qxfer ("exec-file", annex, readbuf, offset,
c78fa86a
GB
10862 len, xfered_len,
10863 &remote_protocol_packets[PACKET_qXfer_exec_file]);
10864
1e3ff5ad 10865 default:
2ed4b548 10866 return TARGET_XFER_E_IO;
c906108c
SS
10867 }
10868
0df8b418 10869 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 10870 large enough let the caller deal with it. */
ea9c271d 10871 if (len < get_remote_packet_size ())
2ed4b548 10872 return TARGET_XFER_E_IO;
ea9c271d 10873 len = get_remote_packet_size ();
1e3ff5ad 10874
23860348 10875 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 10876 if (!rs->remote_desc)
8a3fe4f8 10877 error (_("remote query is only available after target open"));
c906108c 10878
1e3ff5ad 10879 gdb_assert (annex != NULL);
4b8a223f 10880 gdb_assert (readbuf != NULL);
c906108c 10881
6d820c5c 10882 p2 = rs->buf;
c906108c
SS
10883 *p2++ = 'q';
10884 *p2++ = query_type;
10885
23860348
MS
10886 /* We used one buffer char for the remote protocol q command and
10887 another for the query type. As the remote protocol encapsulation
10888 uses 4 chars plus one extra in case we are debugging
10889 (remote_debug), we have PBUFZIZ - 7 left to pack the query
10890 string. */
c906108c 10891 i = 0;
ea9c271d 10892 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 10893 {
1e3ff5ad
AC
10894 /* Bad caller may have sent forbidden characters. */
10895 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
10896 *p2++ = annex[i];
c906108c
SS
10897 i++;
10898 }
1e3ff5ad
AC
10899 *p2 = '\0';
10900 gdb_assert (annex[i] == '\0');
c906108c 10901
6d820c5c 10902 i = putpkt (rs->buf);
c5aa993b 10903 if (i < 0)
2ed4b548 10904 return TARGET_XFER_E_IO;
c906108c 10905
6d820c5c
DJ
10906 getpkt (&rs->buf, &rs->buf_size, 0);
10907 strcpy ((char *) readbuf, rs->buf);
c906108c 10908
9b409511 10909 *xfered_len = strlen ((char *) readbuf);
92ffd475 10910 return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
c906108c
SS
10911}
10912
09c98b44
DB
10913/* Implementation of to_get_memory_xfer_limit. */
10914
f6ac5f3d
PA
10915ULONGEST
10916remote_target::get_memory_xfer_limit ()
09c98b44
DB
10917{
10918 return get_memory_write_packet_size ();
10919}
10920
f6ac5f3d
PA
10921int
10922remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
10923 const gdb_byte *pattern, ULONGEST pattern_len,
10924 CORE_ADDR *found_addrp)
08388c79 10925{
f5656ead 10926 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
10927 struct remote_state *rs = get_remote_state ();
10928 int max_size = get_memory_write_packet_size ();
10929 struct packet_config *packet =
10930 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
10931 /* Number of packet bytes used to encode the pattern;
10932 this could be more than PATTERN_LEN due to escape characters. */
08388c79 10933 int escaped_pattern_len;
0df8b418 10934 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
10935 int used_pattern_len;
10936 int i;
10937 int found;
10938 ULONGEST found_addr;
10939
7cc244de
PA
10940 /* Don't go to the target if we don't have to. This is done before
10941 checking packet_config_support to avoid the possibility that a
10942 success for this edge case means the facility works in
10943 general. */
08388c79
DE
10944 if (pattern_len > search_space_len)
10945 return 0;
10946 if (pattern_len == 0)
10947 {
10948 *found_addrp = start_addr;
10949 return 1;
10950 }
10951
10952 /* If we already know the packet isn't supported, fall back to the simple
10953 way of searching memory. */
10954
4082afcc 10955 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
10956 {
10957 /* Target doesn't provided special support, fall back and use the
10958 standard support (copy memory and do the search here). */
f6ac5f3d 10959 return simple_search_memory (this, start_addr, search_space_len,
08388c79
DE
10960 pattern, pattern_len, found_addrp);
10961 }
10962
28439a30
PA
10963 /* Make sure the remote is pointing at the right process. */
10964 set_general_process ();
10965
08388c79
DE
10966 /* Insert header. */
10967 i = snprintf (rs->buf, max_size,
10968 "qSearch:memory:%s;%s;",
5af949e3 10969 phex_nz (start_addr, addr_size),
08388c79
DE
10970 phex_nz (search_space_len, sizeof (search_space_len)));
10971 max_size -= (i + 1);
10972
10973 /* Escape as much data as fits into rs->buf. */
10974 escaped_pattern_len =
124e13d9 10975 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
10976 &used_pattern_len, max_size);
10977
10978 /* Bail if the pattern is too large. */
10979 if (used_pattern_len != pattern_len)
9b20d036 10980 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
10981
10982 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
10983 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10984 || packet_ok (rs->buf, packet) != PACKET_OK)
10985 {
10986 /* The request may not have worked because the command is not
10987 supported. If so, fall back to the simple way. */
7cc244de 10988 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79 10989 {
f6ac5f3d 10990 return simple_search_memory (this, start_addr, search_space_len,
08388c79
DE
10991 pattern, pattern_len, found_addrp);
10992 }
10993 return -1;
10994 }
10995
10996 if (rs->buf[0] == '0')
10997 found = 0;
10998 else if (rs->buf[0] == '1')
10999 {
11000 found = 1;
11001 if (rs->buf[1] != ',')
10e0fa18 11002 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
11003 unpack_varlen_hex (rs->buf + 2, &found_addr);
11004 *found_addrp = found_addr;
11005 }
11006 else
10e0fa18 11007 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
11008
11009 return found;
11010}
11011
f6ac5f3d
PA
11012void
11013remote_target::rcmd (const char *command, struct ui_file *outbuf)
96baa820 11014{
d01949b6 11015 struct remote_state *rs = get_remote_state ();
2e9f7625 11016 char *p = rs->buf;
96baa820 11017
5d93a237 11018 if (!rs->remote_desc)
8a3fe4f8 11019 error (_("remote rcmd is only available after target open"));
96baa820 11020
23860348 11021 /* Send a NULL command across as an empty command. */
7be570e7
JM
11022 if (command == NULL)
11023 command = "";
11024
23860348 11025 /* The query prefix. */
2e9f7625
DJ
11026 strcpy (rs->buf, "qRcmd,");
11027 p = strchr (rs->buf, '\0');
96baa820 11028
3e43a32a
MS
11029 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
11030 > get_remote_packet_size ())
8a3fe4f8 11031 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 11032
23860348 11033 /* Encode the actual command. */
a30bf1f1 11034 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 11035
6d820c5c 11036 if (putpkt (rs->buf) < 0)
8a3fe4f8 11037 error (_("Communication problem with target."));
96baa820
JM
11038
11039 /* get/display the response */
11040 while (1)
11041 {
2e9f7625
DJ
11042 char *buf;
11043
00bf0b85 11044 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 11045 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 11046 rs->buf[0] = '\0';
5b37825d
PW
11047 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
11048 {
11049 /* Timeout. Continue to (try to) read responses.
11050 This is better than stopping with an error, assuming the stub
11051 is still executing the (long) monitor command.
11052 If needed, the user can interrupt gdb using C-c, obtaining
11053 an effect similar to stop on timeout. */
11054 continue;
11055 }
2e9f7625 11056 buf = rs->buf;
96baa820 11057 if (buf[0] == '\0')
8a3fe4f8 11058 error (_("Target does not support this command."));
96baa820
JM
11059 if (buf[0] == 'O' && buf[1] != 'K')
11060 {
23860348 11061 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
11062 continue;
11063 }
11064 if (strcmp (buf, "OK") == 0)
11065 break;
7be570e7
JM
11066 if (strlen (buf) == 3 && buf[0] == 'E'
11067 && isdigit (buf[1]) && isdigit (buf[2]))
11068 {
8a3fe4f8 11069 error (_("Protocol error with Rcmd"));
7be570e7 11070 }
96baa820
JM
11071 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
11072 {
11073 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 11074
96baa820
JM
11075 fputc_unfiltered (c, outbuf);
11076 }
11077 break;
11078 }
11079}
11080
f6ac5f3d
PA
11081std::vector<mem_region>
11082remote_target::memory_map ()
fd79ecee 11083{
a664f67e 11084 std::vector<mem_region> result;
9018be22 11085 gdb::optional<gdb::char_vector> text
f6ac5f3d 11086 = target_read_stralloc (target_stack, TARGET_OBJECT_MEMORY_MAP, NULL);
fd79ecee
DJ
11087
11088 if (text)
9018be22 11089 result = parse_memory_map (text->data ());
fd79ecee
DJ
11090
11091 return result;
11092}
11093
c906108c 11094static void
ac88e2de 11095packet_command (const char *args, int from_tty)
c906108c 11096{
d01949b6 11097 struct remote_state *rs = get_remote_state ();
c906108c 11098
5d93a237 11099 if (!rs->remote_desc)
8a3fe4f8 11100 error (_("command can only be used with remote target"));
c906108c 11101
c5aa993b 11102 if (!args)
8a3fe4f8 11103 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
11104
11105 puts_filtered ("sending: ");
11106 print_packet (args);
11107 puts_filtered ("\n");
11108 putpkt (args);
11109
6d820c5c 11110 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 11111 puts_filtered ("received: ");
6d820c5c 11112 print_packet (rs->buf);
c906108c
SS
11113 puts_filtered ("\n");
11114}
11115
11116#if 0
23860348 11117/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 11118
a14ed312 11119static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 11120
a14ed312 11121static void threadset_test_cmd (char *cmd, int tty);
c906108c 11122
a14ed312 11123static void threadalive_test (char *cmd, int tty);
c906108c 11124
a14ed312 11125static void threadlist_test_cmd (char *cmd, int tty);
c906108c 11126
23860348 11127int get_and_display_threadinfo (threadref *ref);
c906108c 11128
a14ed312 11129static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 11130
23860348 11131static int thread_display_step (threadref *ref, void *context);
c906108c 11132
a14ed312 11133static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 11134
a14ed312 11135static void init_remote_threadtests (void);
c906108c 11136
23860348 11137#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
11138
11139static void
0b39b52e 11140threadset_test_cmd (const char *cmd, int tty)
c906108c
SS
11141{
11142 int sample_thread = SAMPLE_THREAD;
11143
a3f17187 11144 printf_filtered (_("Remote threadset test\n"));
79d7f229 11145 set_general_thread (sample_thread);
c906108c
SS
11146}
11147
11148
11149static void
0b39b52e 11150threadalive_test (const char *cmd, int tty)
c906108c
SS
11151{
11152 int sample_thread = SAMPLE_THREAD;
79d7f229 11153 int pid = ptid_get_pid (inferior_ptid);
ba348170 11154 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 11155
79d7f229 11156 if (remote_thread_alive (ptid))
c906108c
SS
11157 printf_filtered ("PASS: Thread alive test\n");
11158 else
11159 printf_filtered ("FAIL: Thread alive test\n");
11160}
11161
23860348 11162void output_threadid (char *title, threadref *ref);
c906108c
SS
11163
11164void
fba45db2 11165output_threadid (char *title, threadref *ref)
c906108c
SS
11166{
11167 char hexid[20];
11168
23860348 11169 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
11170 hexid[16] = 0;
11171 printf_filtered ("%s %s\n", title, (&hexid[0]));
11172}
11173
11174static void
0b39b52e 11175threadlist_test_cmd (const char *cmd, int tty)
c906108c
SS
11176{
11177 int startflag = 1;
11178 threadref nextthread;
11179 int done, result_count;
11180 threadref threadlist[3];
11181
11182 printf_filtered ("Remote Threadlist test\n");
11183 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
11184 &result_count, &threadlist[0]))
11185 printf_filtered ("FAIL: threadlist test\n");
11186 else
11187 {
11188 threadref *scan = threadlist;
11189 threadref *limit = scan + result_count;
11190
11191 while (scan < limit)
11192 output_threadid (" thread ", scan++);
11193 }
11194}
11195
11196void
fba45db2 11197display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
11198{
11199 output_threadid ("Threadid: ", &info->threadid);
11200 printf_filtered ("Name: %s\n ", info->shortname);
11201 printf_filtered ("State: %s\n", info->display);
11202 printf_filtered ("other: %s\n\n", info->more_display);
11203}
11204
11205int
fba45db2 11206get_and_display_threadinfo (threadref *ref)
c906108c
SS
11207{
11208 int result;
11209 int set;
11210 struct gdb_ext_thread_info threadinfo;
11211
11212 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
11213 | TAG_MOREDISPLAY | TAG_DISPLAY;
11214 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
11215 display_thread_info (&threadinfo);
11216 return result;
11217}
11218
11219static void
0b39b52e 11220threadinfo_test_cmd (const char *cmd, int tty)
c906108c
SS
11221{
11222 int athread = SAMPLE_THREAD;
11223 threadref thread;
11224 int set;
11225
11226 int_to_threadref (&thread, athread);
11227 printf_filtered ("Remote Threadinfo test\n");
11228 if (!get_and_display_threadinfo (&thread))
11229 printf_filtered ("FAIL cannot get thread info\n");
11230}
11231
11232static int
fba45db2 11233thread_display_step (threadref *ref, void *context)
c906108c
SS
11234{
11235 /* output_threadid(" threadstep ",ref); *//* simple test */
11236 return get_and_display_threadinfo (ref);
11237}
11238
11239static void
0b39b52e 11240threadlist_update_test_cmd (const char *cmd, int tty)
c906108c
SS
11241{
11242 printf_filtered ("Remote Threadlist update test\n");
11243 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
11244}
11245
11246static void
11247init_remote_threadtests (void)
11248{
3e43a32a
MS
11249 add_com ("tlist", class_obscure, threadlist_test_cmd,
11250 _("Fetch and print the remote list of "
11251 "thread identifiers, one pkt only"));
c906108c 11252 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 11253 _("Fetch and display info about one thread"));
c906108c 11254 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 11255 _("Test setting to a different thread"));
c906108c 11256 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 11257 _("Iterate through updating all remote thread info"));
c906108c 11258 add_com ("talive", class_obscure, threadalive_test,
1bedd215 11259 _(" Remote thread alive test "));
c906108c
SS
11260}
11261
11262#endif /* 0 */
11263
f3fb8c85
MS
11264/* Convert a thread ID to a string. Returns the string in a static
11265 buffer. */
11266
f6ac5f3d
PA
11267const char *
11268remote_target::pid_to_str (ptid_t ptid)
f3fb8c85 11269{
79d7f229 11270 static char buf[64];
82f73884 11271 struct remote_state *rs = get_remote_state ();
f3fb8c85 11272
7cee1e54
PA
11273 if (ptid_equal (ptid, null_ptid))
11274 return normal_pid_to_str (ptid);
11275 else if (ptid_is_pid (ptid))
ecd0ada5
PA
11276 {
11277 /* Printing an inferior target id. */
11278
11279 /* When multi-process extensions are off, there's no way in the
11280 remote protocol to know the remote process id, if there's any
11281 at all. There's one exception --- when we're connected with
11282 target extended-remote, and we manually attached to a process
11283 with "attach PID". We don't record anywhere a flag that
11284 allows us to distinguish that case from the case of
11285 connecting with extended-remote and the stub already being
11286 attached to a process, and reporting yes to qAttached, hence
11287 no smart special casing here. */
11288 if (!remote_multi_process_p (rs))
11289 {
11290 xsnprintf (buf, sizeof buf, "Remote target");
11291 return buf;
11292 }
11293
11294 return normal_pid_to_str (ptid);
82f73884 11295 }
ecd0ada5 11296 else
79d7f229 11297 {
ecd0ada5
PA
11298 if (ptid_equal (magic_null_ptid, ptid))
11299 xsnprintf (buf, sizeof buf, "Thread <main>");
8020350c 11300 else if (remote_multi_process_p (rs))
de0d863e
DB
11301 if (ptid_get_lwp (ptid) == 0)
11302 return normal_pid_to_str (ptid);
11303 else
11304 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
11305 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
11306 else
11307 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 11308 ptid_get_lwp (ptid));
79d7f229
PA
11309 return buf;
11310 }
f3fb8c85
MS
11311}
11312
38691318
KB
11313/* Get the address of the thread local variable in OBJFILE which is
11314 stored at OFFSET within the thread local storage for thread PTID. */
11315
f6ac5f3d
PA
11316CORE_ADDR
11317remote_target::get_thread_local_address (ptid_t ptid, CORE_ADDR lm,
11318 CORE_ADDR offset)
38691318 11319{
4082afcc 11320 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
11321 {
11322 struct remote_state *rs = get_remote_state ();
6d820c5c 11323 char *p = rs->buf;
82f73884 11324 char *endp = rs->buf + get_remote_packet_size ();
571dd617 11325 enum packet_result result;
38691318
KB
11326
11327 strcpy (p, "qGetTLSAddr:");
11328 p += strlen (p);
82f73884 11329 p = write_ptid (p, endp, ptid);
38691318
KB
11330 *p++ = ',';
11331 p += hexnumstr (p, offset);
11332 *p++ = ',';
11333 p += hexnumstr (p, lm);
11334 *p++ = '\0';
11335
6d820c5c
DJ
11336 putpkt (rs->buf);
11337 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
11338 result = packet_ok (rs->buf,
11339 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 11340 if (result == PACKET_OK)
38691318
KB
11341 {
11342 ULONGEST result;
11343
6d820c5c 11344 unpack_varlen_hex (rs->buf, &result);
38691318
KB
11345 return result;
11346 }
571dd617 11347 else if (result == PACKET_UNKNOWN)
109c3e39
AC
11348 throw_error (TLS_GENERIC_ERROR,
11349 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 11350 else
109c3e39
AC
11351 throw_error (TLS_GENERIC_ERROR,
11352 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
11353 }
11354 else
109c3e39
AC
11355 throw_error (TLS_GENERIC_ERROR,
11356 _("TLS not supported or disabled on this target"));
38691318
KB
11357 /* Not reached. */
11358 return 0;
11359}
11360
711e434b
PM
11361/* Provide thread local base, i.e. Thread Information Block address.
11362 Returns 1 if ptid is found and thread_local_base is non zero. */
11363
57810aa7 11364bool
f6ac5f3d 11365remote_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
711e434b 11366{
4082afcc 11367 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
11368 {
11369 struct remote_state *rs = get_remote_state ();
11370 char *p = rs->buf;
11371 char *endp = rs->buf + get_remote_packet_size ();
11372 enum packet_result result;
11373
11374 strcpy (p, "qGetTIBAddr:");
11375 p += strlen (p);
11376 p = write_ptid (p, endp, ptid);
11377 *p++ = '\0';
11378
11379 putpkt (rs->buf);
11380 getpkt (&rs->buf, &rs->buf_size, 0);
11381 result = packet_ok (rs->buf,
11382 &remote_protocol_packets[PACKET_qGetTIBAddr]);
11383 if (result == PACKET_OK)
11384 {
11385 ULONGEST result;
11386
11387 unpack_varlen_hex (rs->buf, &result);
11388 if (addr)
11389 *addr = (CORE_ADDR) result;
57810aa7 11390 return true;
711e434b
PM
11391 }
11392 else if (result == PACKET_UNKNOWN)
11393 error (_("Remote target doesn't support qGetTIBAddr packet"));
11394 else
11395 error (_("Remote target failed to process qGetTIBAddr request"));
11396 }
11397 else
11398 error (_("qGetTIBAddr not supported or disabled on this target"));
11399 /* Not reached. */
57810aa7 11400 return false;
711e434b
PM
11401}
11402
29709017
DJ
11403/* Support for inferring a target description based on the current
11404 architecture and the size of a 'g' packet. While the 'g' packet
11405 can have any size (since optional registers can be left off the
11406 end), some sizes are easily recognizable given knowledge of the
11407 approximate architecture. */
11408
11409struct remote_g_packet_guess
11410{
11411 int bytes;
11412 const struct target_desc *tdesc;
11413};
11414typedef struct remote_g_packet_guess remote_g_packet_guess_s;
11415DEF_VEC_O(remote_g_packet_guess_s);
11416
11417struct remote_g_packet_data
11418{
11419 VEC(remote_g_packet_guess_s) *guesses;
11420};
11421
11422static struct gdbarch_data *remote_g_packet_data_handle;
11423
11424static void *
11425remote_g_packet_data_init (struct obstack *obstack)
11426{
11427 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
11428}
11429
11430void
11431register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
11432 const struct target_desc *tdesc)
11433{
11434 struct remote_g_packet_data *data
19ba03f4
SM
11435 = ((struct remote_g_packet_data *)
11436 gdbarch_data (gdbarch, remote_g_packet_data_handle));
29709017
DJ
11437 struct remote_g_packet_guess new_guess, *guess;
11438 int ix;
11439
11440 gdb_assert (tdesc != NULL);
11441
11442 for (ix = 0;
11443 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11444 ix++)
11445 if (guess->bytes == bytes)
11446 internal_error (__FILE__, __LINE__,
9b20d036 11447 _("Duplicate g packet description added for size %d"),
29709017
DJ
11448 bytes);
11449
11450 new_guess.bytes = bytes;
11451 new_guess.tdesc = tdesc;
11452 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
11453}
11454
d962ef82
DJ
11455/* Return 1 if remote_read_description would do anything on this target
11456 and architecture, 0 otherwise. */
11457
11458static int
11459remote_read_description_p (struct target_ops *target)
11460{
11461 struct remote_g_packet_data *data
19ba03f4
SM
11462 = ((struct remote_g_packet_data *)
11463 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
d962ef82
DJ
11464
11465 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11466 return 1;
11467
11468 return 0;
11469}
11470
f6ac5f3d
PA
11471const struct target_desc *
11472remote_target::read_description ()
29709017
DJ
11473{
11474 struct remote_g_packet_data *data
19ba03f4
SM
11475 = ((struct remote_g_packet_data *)
11476 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
29709017 11477
d962ef82
DJ
11478 /* Do not try this during initial connection, when we do not know
11479 whether there is a running but stopped thread. */
11480 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
f6ac5f3d 11481 return beneath->read_description ();
d962ef82 11482
29709017
DJ
11483 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11484 {
11485 struct remote_g_packet_guess *guess;
11486 int ix;
11487 int bytes = send_g_packet ();
11488
11489 for (ix = 0;
11490 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11491 ix++)
11492 if (guess->bytes == bytes)
11493 return guess->tdesc;
11494
11495 /* We discard the g packet. A minor optimization would be to
11496 hold on to it, and fill the register cache once we have selected
11497 an architecture, but it's too tricky to do safely. */
11498 }
11499
f6ac5f3d 11500 return beneath->read_description ();
29709017
DJ
11501}
11502
a6b151f1
DJ
11503/* Remote file transfer support. This is host-initiated I/O, not
11504 target-initiated; for target-initiated, see remote-fileio.c. */
11505
11506/* If *LEFT is at least the length of STRING, copy STRING to
11507 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11508 decrease *LEFT. Otherwise raise an error. */
11509
11510static void
a121b7c1 11511remote_buffer_add_string (char **buffer, int *left, const char *string)
a6b151f1
DJ
11512{
11513 int len = strlen (string);
11514
11515 if (len > *left)
11516 error (_("Packet too long for target."));
11517
11518 memcpy (*buffer, string, len);
11519 *buffer += len;
11520 *left -= len;
11521
11522 /* NUL-terminate the buffer as a convenience, if there is
11523 room. */
11524 if (*left)
11525 **buffer = '\0';
11526}
11527
11528/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
11529 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11530 decrease *LEFT. Otherwise raise an error. */
11531
11532static void
11533remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
11534 int len)
11535{
11536 if (2 * len > *left)
11537 error (_("Packet too long for target."));
11538
11539 bin2hex (bytes, *buffer, len);
11540 *buffer += 2 * len;
11541 *left -= 2 * len;
11542
11543 /* NUL-terminate the buffer as a convenience, if there is
11544 room. */
11545 if (*left)
11546 **buffer = '\0';
11547}
11548
11549/* If *LEFT is large enough, convert VALUE to hex and add it to
11550 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11551 decrease *LEFT. Otherwise raise an error. */
11552
11553static void
11554remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
11555{
11556 int len = hexnumlen (value);
11557
11558 if (len > *left)
11559 error (_("Packet too long for target."));
11560
11561 hexnumstr (*buffer, value);
11562 *buffer += len;
11563 *left -= len;
11564
11565 /* NUL-terminate the buffer as a convenience, if there is
11566 room. */
11567 if (*left)
11568 **buffer = '\0';
11569}
11570
11571/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
11572 value, *REMOTE_ERRNO to the remote error number or zero if none
11573 was included, and *ATTACHMENT to point to the start of the annex
11574 if any. The length of the packet isn't needed here; there may
11575 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
11576
11577 Return 0 if the packet could be parsed, -1 if it could not. If
11578 -1 is returned, the other variables may not be initialized. */
11579
11580static int
11581remote_hostio_parse_result (char *buffer, int *retcode,
11582 int *remote_errno, char **attachment)
11583{
11584 char *p, *p2;
11585
11586 *remote_errno = 0;
11587 *attachment = NULL;
11588
11589 if (buffer[0] != 'F')
11590 return -1;
11591
11592 errno = 0;
11593 *retcode = strtol (&buffer[1], &p, 16);
11594 if (errno != 0 || p == &buffer[1])
11595 return -1;
11596
11597 /* Check for ",errno". */
11598 if (*p == ',')
11599 {
11600 errno = 0;
11601 *remote_errno = strtol (p + 1, &p2, 16);
11602 if (errno != 0 || p + 1 == p2)
11603 return -1;
11604 p = p2;
11605 }
11606
11607 /* Check for ";attachment". If there is no attachment, the
11608 packet should end here. */
11609 if (*p == ';')
11610 {
11611 *attachment = p + 1;
11612 return 0;
11613 }
11614 else if (*p == '\0')
11615 return 0;
11616 else
11617 return -1;
11618}
11619
11620/* Send a prepared I/O packet to the target and read its response.
11621 The prepared packet is in the global RS->BUF before this function
11622 is called, and the answer is there when we return.
11623
11624 COMMAND_BYTES is the length of the request to send, which may include
11625 binary data. WHICH_PACKET is the packet configuration to check
11626 before attempting a packet. If an error occurs, *REMOTE_ERRNO
11627 is set to the error number and -1 is returned. Otherwise the value
11628 returned by the function is returned.
11629
11630 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
11631 attachment is expected; an error will be reported if there's a
11632 mismatch. If one is found, *ATTACHMENT will be set to point into
11633 the packet buffer and *ATTACHMENT_LEN will be set to the
11634 attachment's length. */
11635
11636static int
11637remote_hostio_send_command (int command_bytes, int which_packet,
11638 int *remote_errno, char **attachment,
11639 int *attachment_len)
11640{
11641 struct remote_state *rs = get_remote_state ();
11642 int ret, bytes_read;
11643 char *attachment_tmp;
11644
20db9c52 11645 if (packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
11646 {
11647 *remote_errno = FILEIO_ENOSYS;
11648 return -1;
11649 }
11650
11651 putpkt_binary (rs->buf, command_bytes);
11652 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
11653
11654 /* If it timed out, something is wrong. Don't try to parse the
11655 buffer. */
11656 if (bytes_read < 0)
11657 {
11658 *remote_errno = FILEIO_EINVAL;
11659 return -1;
11660 }
11661
11662 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
11663 {
11664 case PACKET_ERROR:
11665 *remote_errno = FILEIO_EINVAL;
11666 return -1;
11667 case PACKET_UNKNOWN:
11668 *remote_errno = FILEIO_ENOSYS;
11669 return -1;
11670 case PACKET_OK:
11671 break;
11672 }
11673
11674 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
11675 &attachment_tmp))
11676 {
11677 *remote_errno = FILEIO_EINVAL;
11678 return -1;
11679 }
11680
11681 /* Make sure we saw an attachment if and only if we expected one. */
11682 if ((attachment_tmp == NULL && attachment != NULL)
11683 || (attachment_tmp != NULL && attachment == NULL))
11684 {
11685 *remote_errno = FILEIO_EINVAL;
11686 return -1;
11687 }
11688
11689 /* If an attachment was found, it must point into the packet buffer;
11690 work out how many bytes there were. */
11691 if (attachment_tmp != NULL)
11692 {
11693 *attachment = attachment_tmp;
11694 *attachment_len = bytes_read - (*attachment - rs->buf);
11695 }
11696
11697 return ret;
11698}
11699
dd194f6b 11700/* See declaration.h. */
80152258 11701
dd194f6b
PA
11702void
11703readahead_cache::invalidate ()
80152258 11704{
dd194f6b 11705 this->fd = -1;
80152258
PA
11706}
11707
dd194f6b 11708/* See declaration.h. */
80152258 11709
dd194f6b
PA
11710void
11711readahead_cache::invalidate_fd (int fd)
80152258 11712{
dd194f6b
PA
11713 if (this->fd == fd)
11714 this->fd = -1;
80152258
PA
11715}
11716
15a201c8
GB
11717/* Set the filesystem remote_hostio functions that take FILENAME
11718 arguments will use. Return 0 on success, or -1 if an error
11719 occurs (and set *REMOTE_ERRNO). */
11720
11721static int
11722remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
11723{
11724 struct remote_state *rs = get_remote_state ();
11725 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
11726 char *p = rs->buf;
11727 int left = get_remote_packet_size () - 1;
11728 char arg[9];
11729 int ret;
11730
11731 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11732 return 0;
11733
11734 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
11735 return 0;
11736
11737 remote_buffer_add_string (&p, &left, "vFile:setfs:");
11738
11739 xsnprintf (arg, sizeof (arg), "%x", required_pid);
11740 remote_buffer_add_string (&p, &left, arg);
11741
11742 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
11743 remote_errno, NULL, NULL);
11744
11745 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11746 return 0;
11747
11748 if (ret == 0)
11749 rs->fs_pid = required_pid;
11750
11751 return ret;
11752}
11753
12e2a5fd 11754/* Implementation of to_fileio_open. */
a6b151f1
DJ
11755
11756static int
cd897586 11757remote_hostio_open (struct target_ops *self,
07c138c8 11758 struct inferior *inf, const char *filename,
4313b8c0
GB
11759 int flags, int mode, int warn_if_slow,
11760 int *remote_errno)
a6b151f1
DJ
11761{
11762 struct remote_state *rs = get_remote_state ();
11763 char *p = rs->buf;
11764 int left = get_remote_packet_size () - 1;
11765
4313b8c0
GB
11766 if (warn_if_slow)
11767 {
11768 static int warning_issued = 0;
11769
11770 printf_unfiltered (_("Reading %s from remote target...\n"),
11771 filename);
11772
11773 if (!warning_issued)
11774 {
11775 warning (_("File transfers from remote targets can be slow."
11776 " Use \"set sysroot\" to access files locally"
11777 " instead."));
11778 warning_issued = 1;
11779 }
11780 }
11781
15a201c8
GB
11782 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11783 return -1;
11784
a6b151f1
DJ
11785 remote_buffer_add_string (&p, &left, "vFile:open:");
11786
11787 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11788 strlen (filename));
11789 remote_buffer_add_string (&p, &left, ",");
11790
11791 remote_buffer_add_int (&p, &left, flags);
11792 remote_buffer_add_string (&p, &left, ",");
11793
11794 remote_buffer_add_int (&p, &left, mode);
11795
11796 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
11797 remote_errno, NULL, NULL);
11798}
11799
f6ac5f3d
PA
11800int
11801remote_target::fileio_open (struct inferior *inf, const char *filename,
11802 int flags, int mode, int warn_if_slow,
11803 int *remote_errno)
11804{
11805 return remote_hostio_open (this, inf, filename, flags, mode, warn_if_slow,
11806 remote_errno);
11807}
11808
12e2a5fd 11809/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
11810
11811static int
0d866f62
TT
11812remote_hostio_pwrite (struct target_ops *self,
11813 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
11814 ULONGEST offset, int *remote_errno)
11815{
11816 struct remote_state *rs = get_remote_state ();
11817 char *p = rs->buf;
11818 int left = get_remote_packet_size ();
11819 int out_len;
11820
dd194f6b 11821 rs->readahead_cache.invalidate_fd (fd);
80152258 11822
a6b151f1
DJ
11823 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
11824
11825 remote_buffer_add_int (&p, &left, fd);
11826 remote_buffer_add_string (&p, &left, ",");
11827
11828 remote_buffer_add_int (&p, &left, offset);
11829 remote_buffer_add_string (&p, &left, ",");
11830
124e13d9 11831 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
11832 get_remote_packet_size () - (p - rs->buf));
11833
11834 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
11835 remote_errno, NULL, NULL);
11836}
11837
f6ac5f3d
PA
11838int
11839remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
11840 ULONGEST offset, int *remote_errno)
11841{
11842 return remote_hostio_pwrite (this, fd, write_buf, len, offset, remote_errno);
11843}
11844
80152258
PA
11845/* Helper for the implementation of to_fileio_pread. Read the file
11846 from the remote side with vFile:pread. */
a6b151f1
DJ
11847
11848static int
80152258
PA
11849remote_hostio_pread_vFile (struct target_ops *self,
11850 int fd, gdb_byte *read_buf, int len,
11851 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
11852{
11853 struct remote_state *rs = get_remote_state ();
11854 char *p = rs->buf;
11855 char *attachment;
11856 int left = get_remote_packet_size ();
11857 int ret, attachment_len;
11858 int read_len;
11859
11860 remote_buffer_add_string (&p, &left, "vFile:pread:");
11861
11862 remote_buffer_add_int (&p, &left, fd);
11863 remote_buffer_add_string (&p, &left, ",");
11864
11865 remote_buffer_add_int (&p, &left, len);
11866 remote_buffer_add_string (&p, &left, ",");
11867
11868 remote_buffer_add_int (&p, &left, offset);
11869
11870 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
11871 remote_errno, &attachment,
11872 &attachment_len);
11873
11874 if (ret < 0)
11875 return ret;
11876
bc20a4af 11877 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
11878 read_buf, len);
11879 if (read_len != ret)
11880 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
11881
11882 return ret;
11883}
11884
dd194f6b 11885/* See declaration.h. */
80152258 11886
dd194f6b
PA
11887int
11888readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
11889 ULONGEST offset)
80152258 11890{
dd194f6b
PA
11891 if (this->fd == fd
11892 && this->offset <= offset
11893 && offset < this->offset + this->bufsize)
80152258 11894 {
dd194f6b 11895 ULONGEST max = this->offset + this->bufsize;
80152258
PA
11896
11897 if (offset + len > max)
11898 len = max - offset;
11899
dd194f6b 11900 memcpy (read_buf, this->buf + offset - this->offset, len);
80152258
PA
11901 return len;
11902 }
11903
11904 return 0;
11905}
11906
11907/* Implementation of to_fileio_pread. */
11908
11909static int
11910remote_hostio_pread (struct target_ops *self,
11911 int fd, gdb_byte *read_buf, int len,
11912 ULONGEST offset, int *remote_errno)
11913{
11914 int ret;
11915 struct remote_state *rs = get_remote_state ();
dd194f6b 11916 readahead_cache *cache = &rs->readahead_cache;
80152258 11917
dd194f6b 11918 ret = cache->pread (fd, read_buf, len, offset);
80152258
PA
11919 if (ret > 0)
11920 {
11921 cache->hit_count++;
11922
11923 if (remote_debug)
11924 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
11925 pulongest (cache->hit_count));
11926 return ret;
11927 }
11928
11929 cache->miss_count++;
11930 if (remote_debug)
11931 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
11932 pulongest (cache->miss_count));
11933
11934 cache->fd = fd;
11935 cache->offset = offset;
11936 cache->bufsize = get_remote_packet_size ();
224c3ddb 11937 cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
80152258
PA
11938
11939 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
11940 cache->offset, remote_errno);
11941 if (ret <= 0)
11942 {
dd194f6b 11943 cache->invalidate_fd (fd);
80152258
PA
11944 return ret;
11945 }
11946
11947 cache->bufsize = ret;
dd194f6b 11948 return cache->pread (fd, read_buf, len, offset);
80152258
PA
11949}
11950
f6ac5f3d
PA
11951int
11952remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
11953 ULONGEST offset, int *remote_errno)
11954{
11955 return remote_hostio_pread (this, fd, read_buf, len, offset, remote_errno);
11956}
11957
12e2a5fd 11958/* Implementation of to_fileio_close. */
a6b151f1
DJ
11959
11960static int
df39ea25 11961remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
11962{
11963 struct remote_state *rs = get_remote_state ();
11964 char *p = rs->buf;
11965 int left = get_remote_packet_size () - 1;
11966
dd194f6b 11967 rs->readahead_cache.invalidate_fd (fd);
80152258 11968
a6b151f1
DJ
11969 remote_buffer_add_string (&p, &left, "vFile:close:");
11970
11971 remote_buffer_add_int (&p, &left, fd);
11972
11973 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
11974 remote_errno, NULL, NULL);
11975}
11976
f6ac5f3d
PA
11977int
11978remote_target::fileio_close (int fd, int *remote_errno)
11979{
11980 return remote_hostio_close (this, fd, remote_errno);
11981}
11982
12e2a5fd 11983/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
11984
11985static int
dbbca37d 11986remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
11987 struct inferior *inf, const char *filename,
11988 int *remote_errno)
a6b151f1
DJ
11989{
11990 struct remote_state *rs = get_remote_state ();
11991 char *p = rs->buf;
11992 int left = get_remote_packet_size () - 1;
11993
15a201c8
GB
11994 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11995 return -1;
11996
a6b151f1
DJ
11997 remote_buffer_add_string (&p, &left, "vFile:unlink:");
11998
11999 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
12000 strlen (filename));
12001
12002 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
12003 remote_errno, NULL, NULL);
12004}
12005
f6ac5f3d
PA
12006int
12007remote_target::fileio_unlink (struct inferior *inf, const char *filename,
12008 int *remote_errno)
12009{
12010 return remote_hostio_unlink (this, inf, filename, remote_errno);
12011}
12012
12e2a5fd 12013/* Implementation of to_fileio_readlink. */
b9e7b9c3 12014
f6ac5f3d
PA
12015gdb::optional<std::string>
12016remote_target::fileio_readlink (struct inferior *inf, const char *filename,
12017 int *remote_errno)
b9e7b9c3
UW
12018{
12019 struct remote_state *rs = get_remote_state ();
12020 char *p = rs->buf;
12021 char *attachment;
12022 int left = get_remote_packet_size ();
12023 int len, attachment_len;
12024 int read_len;
b9e7b9c3 12025
15a201c8 12026 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
e0d3522b 12027 return {};
15a201c8 12028
b9e7b9c3
UW
12029 remote_buffer_add_string (&p, &left, "vFile:readlink:");
12030
12031 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
12032 strlen (filename));
12033
12034 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
12035 remote_errno, &attachment,
12036 &attachment_len);
12037
12038 if (len < 0)
e0d3522b 12039 return {};
b9e7b9c3 12040
e0d3522b 12041 std::string ret (len, '\0');
b9e7b9c3 12042
bc20a4af 12043 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
e0d3522b 12044 (gdb_byte *) &ret[0], len);
b9e7b9c3
UW
12045 if (read_len != len)
12046 error (_("Readlink returned %d, but %d bytes."), len, read_len);
12047
b9e7b9c3
UW
12048 return ret;
12049}
12050
12e2a5fd 12051/* Implementation of to_fileio_fstat. */
0a93529c 12052
f6ac5f3d
PA
12053int
12054remote_target::fileio_fstat (int fd, struct stat *st, int *remote_errno)
0a93529c
GB
12055{
12056 struct remote_state *rs = get_remote_state ();
12057 char *p = rs->buf;
12058 int left = get_remote_packet_size ();
12059 int attachment_len, ret;
12060 char *attachment;
12061 struct fio_stat fst;
12062 int read_len;
12063
464b0089
GB
12064 remote_buffer_add_string (&p, &left, "vFile:fstat:");
12065
12066 remote_buffer_add_int (&p, &left, fd);
12067
12068 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
12069 remote_errno, &attachment,
12070 &attachment_len);
12071 if (ret < 0)
0a93529c 12072 {
464b0089
GB
12073 if (*remote_errno != FILEIO_ENOSYS)
12074 return ret;
12075
0a93529c
GB
12076 /* Strictly we should return -1, ENOSYS here, but when
12077 "set sysroot remote:" was implemented in August 2008
12078 BFD's need for a stat function was sidestepped with
12079 this hack. This was not remedied until March 2015
12080 so we retain the previous behavior to avoid breaking
12081 compatibility.
12082
12083 Note that the memset is a March 2015 addition; older
12084 GDBs set st_size *and nothing else* so the structure
12085 would have garbage in all other fields. This might
12086 break something but retaining the previous behavior
12087 here would be just too wrong. */
12088
12089 memset (st, 0, sizeof (struct stat));
12090 st->st_size = INT_MAX;
12091 return 0;
12092 }
12093
0a93529c
GB
12094 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
12095 (gdb_byte *) &fst, sizeof (fst));
12096
12097 if (read_len != ret)
12098 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
12099
12100 if (read_len != sizeof (fst))
12101 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12102 read_len, (int) sizeof (fst));
12103
12104 remote_fileio_to_host_stat (&fst, st);
12105
12106 return 0;
12107}
12108
12e2a5fd 12109/* Implementation of to_filesystem_is_local. */
e3dd7556 12110
57810aa7 12111bool
f6ac5f3d 12112remote_target::filesystem_is_local ()
e3dd7556
GB
12113{
12114 /* Valgrind GDB presents itself as a remote target but works
12115 on the local filesystem: it does not implement remote get
12116 and users are not expected to set a sysroot. To handle
12117 this case we treat the remote filesystem as local if the
12118 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12119 does not support vFile:open. */
a3be80c3 12120 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
12121 {
12122 enum packet_support ps = packet_support (PACKET_vFile_open);
12123
12124 if (ps == PACKET_SUPPORT_UNKNOWN)
12125 {
12126 int fd, remote_errno;
12127
12128 /* Try opening a file to probe support. The supplied
12129 filename is irrelevant, we only care about whether
12130 the stub recognizes the packet or not. */
f6ac5f3d 12131 fd = remote_hostio_open (this, NULL, "just probing",
4313b8c0 12132 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
12133 &remote_errno);
12134
12135 if (fd >= 0)
f6ac5f3d 12136 remote_hostio_close (this, fd, &remote_errno);
e3dd7556
GB
12137
12138 ps = packet_support (PACKET_vFile_open);
12139 }
12140
12141 if (ps == PACKET_DISABLE)
12142 {
12143 static int warning_issued = 0;
12144
12145 if (!warning_issued)
12146 {
12147 warning (_("remote target does not support file"
12148 " transfer, attempting to access files"
12149 " from local filesystem."));
12150 warning_issued = 1;
12151 }
12152
57810aa7 12153 return true;
e3dd7556
GB
12154 }
12155 }
12156
57810aa7 12157 return false;
e3dd7556
GB
12158}
12159
a6b151f1
DJ
12160static int
12161remote_fileio_errno_to_host (int errnum)
12162{
12163 switch (errnum)
12164 {
12165 case FILEIO_EPERM:
12166 return EPERM;
12167 case FILEIO_ENOENT:
12168 return ENOENT;
12169 case FILEIO_EINTR:
12170 return EINTR;
12171 case FILEIO_EIO:
12172 return EIO;
12173 case FILEIO_EBADF:
12174 return EBADF;
12175 case FILEIO_EACCES:
12176 return EACCES;
12177 case FILEIO_EFAULT:
12178 return EFAULT;
12179 case FILEIO_EBUSY:
12180 return EBUSY;
12181 case FILEIO_EEXIST:
12182 return EEXIST;
12183 case FILEIO_ENODEV:
12184 return ENODEV;
12185 case FILEIO_ENOTDIR:
12186 return ENOTDIR;
12187 case FILEIO_EISDIR:
12188 return EISDIR;
12189 case FILEIO_EINVAL:
12190 return EINVAL;
12191 case FILEIO_ENFILE:
12192 return ENFILE;
12193 case FILEIO_EMFILE:
12194 return EMFILE;
12195 case FILEIO_EFBIG:
12196 return EFBIG;
12197 case FILEIO_ENOSPC:
12198 return ENOSPC;
12199 case FILEIO_ESPIPE:
12200 return ESPIPE;
12201 case FILEIO_EROFS:
12202 return EROFS;
12203 case FILEIO_ENOSYS:
12204 return ENOSYS;
12205 case FILEIO_ENAMETOOLONG:
12206 return ENAMETOOLONG;
12207 }
12208 return -1;
12209}
12210
12211static char *
12212remote_hostio_error (int errnum)
12213{
12214 int host_error = remote_fileio_errno_to_host (errnum);
12215
12216 if (host_error == -1)
12217 error (_("Unknown remote I/O error %d"), errnum);
12218 else
12219 error (_("Remote I/O error: %s"), safe_strerror (host_error));
12220}
12221
440b7aec
PA
12222/* A RAII wrapper around a remote file descriptor. */
12223
12224class scoped_remote_fd
a6b151f1 12225{
440b7aec
PA
12226public:
12227 explicit scoped_remote_fd (int fd)
12228 : m_fd (fd)
12229 {
12230 }
a6b151f1 12231
440b7aec
PA
12232 ~scoped_remote_fd ()
12233 {
12234 if (m_fd != -1)
12235 {
12236 try
12237 {
12238 int remote_errno;
12239 remote_hostio_close (find_target_at (process_stratum),
12240 m_fd, &remote_errno);
12241 }
12242 catch (...)
12243 {
12244 /* Swallow exception before it escapes the dtor. If
12245 something goes wrong, likely the connection is gone,
12246 and there's nothing else that can be done. */
12247 }
12248 }
12249 }
12250
12251 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd);
12252
12253 /* Release ownership of the file descriptor, and return it. */
12254 int release () noexcept
12255 {
12256 int fd = m_fd;
12257 m_fd = -1;
12258 return fd;
12259 }
12260
12261 /* Return the owned file descriptor. */
12262 int get () const noexcept
12263 {
12264 return m_fd;
12265 }
12266
12267private:
12268 /* The owned remote I/O file descriptor. */
12269 int m_fd;
12270};
a6b151f1
DJ
12271
12272void
12273remote_file_put (const char *local_file, const char *remote_file, int from_tty)
12274{
440b7aec
PA
12275 struct cleanup *back_to;
12276 int retcode, remote_errno, bytes, io_size;
a6b151f1
DJ
12277 gdb_byte *buffer;
12278 int bytes_in_buffer;
12279 int saw_eof;
12280 ULONGEST offset;
5d93a237 12281 struct remote_state *rs = get_remote_state ();
a6b151f1 12282
5d93a237 12283 if (!rs->remote_desc)
a6b151f1
DJ
12284 error (_("command can only be used with remote target"));
12285
d419f42d 12286 gdb_file_up file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
12287 if (file == NULL)
12288 perror_with_name (local_file);
a6b151f1 12289
440b7aec
PA
12290 scoped_remote_fd fd
12291 (remote_hostio_open (find_target_at (process_stratum), NULL,
12292 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
12293 | FILEIO_O_TRUNC),
12294 0700, 0, &remote_errno));
12295 if (fd.get () == -1)
a6b151f1
DJ
12296 remote_hostio_error (remote_errno);
12297
12298 /* Send up to this many bytes at once. They won't all fit in the
12299 remote packet limit, so we'll transfer slightly fewer. */
12300 io_size = get_remote_packet_size ();
224c3ddb 12301 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12302 back_to = make_cleanup (xfree, buffer);
a6b151f1 12303
a6b151f1
DJ
12304 bytes_in_buffer = 0;
12305 saw_eof = 0;
12306 offset = 0;
12307 while (bytes_in_buffer || !saw_eof)
12308 {
12309 if (!saw_eof)
12310 {
3e43a32a
MS
12311 bytes = fread (buffer + bytes_in_buffer, 1,
12312 io_size - bytes_in_buffer,
d419f42d 12313 file.get ());
a6b151f1
DJ
12314 if (bytes == 0)
12315 {
d419f42d 12316 if (ferror (file.get ()))
a6b151f1
DJ
12317 error (_("Error reading %s."), local_file);
12318 else
12319 {
12320 /* EOF. Unless there is something still in the
12321 buffer from the last iteration, we are done. */
12322 saw_eof = 1;
12323 if (bytes_in_buffer == 0)
12324 break;
12325 }
12326 }
12327 }
12328 else
12329 bytes = 0;
12330
12331 bytes += bytes_in_buffer;
12332 bytes_in_buffer = 0;
12333
0d866f62 12334 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
440b7aec 12335 fd.get (), buffer, bytes,
3e43a32a 12336 offset, &remote_errno);
a6b151f1
DJ
12337
12338 if (retcode < 0)
12339 remote_hostio_error (remote_errno);
12340 else if (retcode == 0)
12341 error (_("Remote write of %d bytes returned 0!"), bytes);
12342 else if (retcode < bytes)
12343 {
12344 /* Short write. Save the rest of the read data for the next
12345 write. */
12346 bytes_in_buffer = bytes - retcode;
12347 memmove (buffer, buffer + retcode, bytes_in_buffer);
12348 }
12349
12350 offset += retcode;
12351 }
12352
440b7aec
PA
12353 if (remote_hostio_close (find_target_at (process_stratum),
12354 fd.release (), &remote_errno))
a6b151f1
DJ
12355 remote_hostio_error (remote_errno);
12356
12357 if (from_tty)
12358 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
12359 do_cleanups (back_to);
12360}
12361
12362void
12363remote_file_get (const char *remote_file, const char *local_file, int from_tty)
12364{
440b7aec
PA
12365 struct cleanup *back_to;
12366 int remote_errno, bytes, io_size;
a6b151f1
DJ
12367 gdb_byte *buffer;
12368 ULONGEST offset;
5d93a237 12369 struct remote_state *rs = get_remote_state ();
a6b151f1 12370
5d93a237 12371 if (!rs->remote_desc)
a6b151f1
DJ
12372 error (_("command can only be used with remote target"));
12373
440b7aec
PA
12374 scoped_remote_fd fd
12375 (remote_hostio_open (find_target_at (process_stratum), NULL,
12376 remote_file, FILEIO_O_RDONLY, 0, 0,
12377 &remote_errno));
12378 if (fd.get () == -1)
a6b151f1
DJ
12379 remote_hostio_error (remote_errno);
12380
d419f42d 12381 gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
12382 if (file == NULL)
12383 perror_with_name (local_file);
a6b151f1
DJ
12384
12385 /* Send up to this many bytes at once. They won't all fit in the
12386 remote packet limit, so we'll transfer slightly fewer. */
12387 io_size = get_remote_packet_size ();
224c3ddb 12388 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12389 back_to = make_cleanup (xfree, buffer);
a6b151f1 12390
a6b151f1
DJ
12391 offset = 0;
12392 while (1)
12393 {
a3be983c 12394 bytes = remote_hostio_pread (find_target_at (process_stratum),
440b7aec
PA
12395 fd.get (), buffer, io_size, offset,
12396 &remote_errno);
a6b151f1
DJ
12397 if (bytes == 0)
12398 /* Success, but no bytes, means end-of-file. */
12399 break;
12400 if (bytes == -1)
12401 remote_hostio_error (remote_errno);
12402
12403 offset += bytes;
12404
d419f42d 12405 bytes = fwrite (buffer, 1, bytes, file.get ());
a6b151f1
DJ
12406 if (bytes == 0)
12407 perror_with_name (local_file);
12408 }
12409
440b7aec
PA
12410 if (remote_hostio_close (find_target_at (process_stratum),
12411 fd.release (), &remote_errno))
a6b151f1
DJ
12412 remote_hostio_error (remote_errno);
12413
12414 if (from_tty)
12415 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
12416 do_cleanups (back_to);
12417}
12418
12419void
12420remote_file_delete (const char *remote_file, int from_tty)
12421{
12422 int retcode, remote_errno;
5d93a237 12423 struct remote_state *rs = get_remote_state ();
a6b151f1 12424
5d93a237 12425 if (!rs->remote_desc)
a6b151f1
DJ
12426 error (_("command can only be used with remote target"));
12427
dbbca37d 12428 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 12429 NULL, remote_file, &remote_errno);
a6b151f1
DJ
12430 if (retcode == -1)
12431 remote_hostio_error (remote_errno);
12432
12433 if (from_tty)
12434 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
12435}
12436
12437static void
ac88e2de 12438remote_put_command (const char *args, int from_tty)
a6b151f1 12439{
d1a41061
PP
12440 if (args == NULL)
12441 error_no_arg (_("file to put"));
12442
773a1edc 12443 gdb_argv argv (args);
a6b151f1
DJ
12444 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12445 error (_("Invalid parameters to remote put"));
12446
12447 remote_file_put (argv[0], argv[1], from_tty);
a6b151f1
DJ
12448}
12449
12450static void
ac88e2de 12451remote_get_command (const char *args, int from_tty)
a6b151f1 12452{
d1a41061
PP
12453 if (args == NULL)
12454 error_no_arg (_("file to get"));
12455
773a1edc 12456 gdb_argv argv (args);
a6b151f1
DJ
12457 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12458 error (_("Invalid parameters to remote get"));
12459
12460 remote_file_get (argv[0], argv[1], from_tty);
a6b151f1
DJ
12461}
12462
12463static void
ac88e2de 12464remote_delete_command (const char *args, int from_tty)
a6b151f1 12465{
d1a41061
PP
12466 if (args == NULL)
12467 error_no_arg (_("file to delete"));
12468
773a1edc 12469 gdb_argv argv (args);
a6b151f1
DJ
12470 if (argv[0] == NULL || argv[1] != NULL)
12471 error (_("Invalid parameters to remote delete"));
12472
12473 remote_file_delete (argv[0], from_tty);
a6b151f1
DJ
12474}
12475
12476static void
981a3fb3 12477remote_command (const char *args, int from_tty)
a6b151f1 12478{
635c7e8a 12479 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
12480}
12481
57810aa7 12482bool
f6ac5f3d 12483remote_target::can_execute_reverse ()
b2175913 12484{
4082afcc
PA
12485 if (packet_support (PACKET_bs) == PACKET_ENABLE
12486 || packet_support (PACKET_bc) == PACKET_ENABLE)
57810aa7 12487 return true;
40ab02ce 12488 else
57810aa7 12489 return false;
b2175913
MS
12490}
12491
57810aa7 12492bool
f6ac5f3d 12493remote_target::supports_non_stop ()
74531fed 12494{
57810aa7 12495 return true;
74531fed
PA
12496}
12497
57810aa7 12498bool
f6ac5f3d 12499remote_target::supports_disable_randomization ()
03583c20
UW
12500{
12501 /* Only supported in extended mode. */
57810aa7 12502 return false;
03583c20
UW
12503}
12504
57810aa7 12505bool
f6ac5f3d 12506remote_target::supports_multi_process ()
8a305172
PA
12507{
12508 struct remote_state *rs = get_remote_state ();
a744cf53 12509
8020350c 12510 return remote_multi_process_p (rs);
8a305172
PA
12511}
12512
70221824 12513static int
f6ac5f3d 12514remote_supports_cond_tracepoints ()
782b2b07 12515{
4082afcc 12516 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
12517}
12518
57810aa7 12519bool
f6ac5f3d 12520remote_target::supports_evaluation_of_breakpoint_conditions ()
3788aec7 12521{
4082afcc 12522 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
12523}
12524
70221824 12525static int
f6ac5f3d 12526remote_supports_fast_tracepoints ()
7a697b8d 12527{
4082afcc 12528 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
12529}
12530
0fb4aa4b 12531static int
f6ac5f3d 12532remote_supports_static_tracepoints ()
0fb4aa4b 12533{
4082afcc 12534 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
12535}
12536
1e4d1764 12537static int
f6ac5f3d 12538remote_supports_install_in_trace ()
1e4d1764 12539{
4082afcc 12540 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
12541}
12542
57810aa7 12543bool
f6ac5f3d 12544remote_target::supports_enable_disable_tracepoint ()
d248b706 12545{
4082afcc
PA
12546 return (packet_support (PACKET_EnableDisableTracepoints_feature)
12547 == PACKET_ENABLE);
d248b706
KY
12548}
12549
57810aa7 12550bool
f6ac5f3d 12551remote_target::supports_string_tracing ()
3065dfb6 12552{
4082afcc 12553 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
12554}
12555
57810aa7 12556bool
f6ac5f3d 12557remote_target::can_run_breakpoint_commands ()
d3ce09f5 12558{
4082afcc 12559 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
12560}
12561
f6ac5f3d
PA
12562void
12563remote_target::trace_init ()
35b1e5cc 12564{
b6bb3468
PA
12565 struct remote_state *rs = get_remote_state ();
12566
35b1e5cc 12567 putpkt ("QTinit");
b6bb3468
PA
12568 remote_get_noisy_reply ();
12569 if (strcmp (rs->buf, "OK") != 0)
35b1e5cc
SS
12570 error (_("Target does not support this command."));
12571}
12572
409873ef
SS
12573/* Recursive routine to walk through command list including loops, and
12574 download packets for each command. */
12575
12576static void
12577remote_download_command_source (int num, ULONGEST addr,
12578 struct command_line *cmds)
12579{
12580 struct remote_state *rs = get_remote_state ();
12581 struct command_line *cmd;
12582
12583 for (cmd = cmds; cmd; cmd = cmd->next)
12584 {
0df8b418 12585 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12586 strcpy (rs->buf, "QTDPsrc:");
12587 encode_source_string (num, addr, "cmd", cmd->line,
12588 rs->buf + strlen (rs->buf),
12589 rs->buf_size - strlen (rs->buf));
12590 putpkt (rs->buf);
b6bb3468
PA
12591 remote_get_noisy_reply ();
12592 if (strcmp (rs->buf, "OK"))
409873ef
SS
12593 warning (_("Target does not support source download."));
12594
12595 if (cmd->control_type == while_control
12596 || cmd->control_type == while_stepping_control)
12597 {
12973681 12598 remote_download_command_source (num, addr, cmd->body_list_0.get ());
409873ef 12599
0df8b418 12600 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12601 strcpy (rs->buf, "QTDPsrc:");
12602 encode_source_string (num, addr, "cmd", "end",
12603 rs->buf + strlen (rs->buf),
12604 rs->buf_size - strlen (rs->buf));
12605 putpkt (rs->buf);
b6bb3468
PA
12606 remote_get_noisy_reply ();
12607 if (strcmp (rs->buf, "OK"))
409873ef
SS
12608 warning (_("Target does not support source download."));
12609 }
12610 }
12611}
12612
f6ac5f3d
PA
12613void
12614remote_target::download_tracepoint (struct bp_location *loc)
35b1e5cc 12615{
bba74b36 12616#define BUF_SIZE 2048
e8ba3115 12617
35b1e5cc 12618 CORE_ADDR tpaddr;
409873ef 12619 char addrbuf[40];
bba74b36 12620 char buf[BUF_SIZE];
b44ec619
SM
12621 std::vector<std::string> tdp_actions;
12622 std::vector<std::string> stepping_actions;
35b1e5cc 12623 char *pkt;
e8ba3115 12624 struct breakpoint *b = loc->owner;
d9b3f62e 12625 struct tracepoint *t = (struct tracepoint *) b;
b6bb3468 12626 struct remote_state *rs = get_remote_state ();
35b1e5cc 12627
dc673c81 12628 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
12629
12630 tpaddr = loc->address;
12631 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
12632 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
12633 addrbuf, /* address */
12634 (b->enable_state == bp_enabled ? 'E' : 'D'),
12635 t->step_count, t->pass_count);
e8ba3115
YQ
12636 /* Fast tracepoints are mostly handled by the target, but we can
12637 tell the target how big of an instruction block should be moved
12638 around. */
12639 if (b->type == bp_fast_tracepoint)
12640 {
12641 /* Only test for support at download time; we may not know
12642 target capabilities at definition time. */
12643 if (remote_supports_fast_tracepoints ())
35b1e5cc 12644 {
6b940e6a
PL
12645 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
12646 NULL))
bba74b36 12647 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 12648 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 12649 else
e8ba3115
YQ
12650 /* If it passed validation at definition but fails now,
12651 something is very wrong. */
12652 internal_error (__FILE__, __LINE__,
12653 _("Fast tracepoint not "
12654 "valid during download"));
35b1e5cc 12655 }
e8ba3115
YQ
12656 else
12657 /* Fast tracepoints are functionally identical to regular
12658 tracepoints, so don't take lack of support as a reason to
12659 give up on the trace run. */
12660 warning (_("Target does not support fast tracepoints, "
12661 "downloading %d as regular tracepoint"), b->number);
12662 }
12663 else if (b->type == bp_static_tracepoint)
12664 {
12665 /* Only test for support at download time; we may not know
12666 target capabilities at definition time. */
12667 if (remote_supports_static_tracepoints ())
0fb4aa4b 12668 {
e8ba3115 12669 struct static_tracepoint_marker marker;
0fb4aa4b 12670
e8ba3115
YQ
12671 if (target_static_tracepoint_marker_at (tpaddr, &marker))
12672 strcat (buf, ":S");
0fb4aa4b 12673 else
e8ba3115 12674 error (_("Static tracepoint not valid during download"));
0fb4aa4b 12675 }
e8ba3115
YQ
12676 else
12677 /* Fast tracepoints are functionally identical to regular
12678 tracepoints, so don't take lack of support as a reason
12679 to give up on the trace run. */
12680 error (_("Target does not support static tracepoints"));
12681 }
12682 /* If the tracepoint has a conditional, make it into an agent
12683 expression and append to the definition. */
12684 if (loc->cond)
12685 {
12686 /* Only test support at download time, we may not know target
12687 capabilities at definition time. */
12688 if (remote_supports_cond_tracepoints ())
35b1e5cc 12689 {
833177a4 12690 agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
bba74b36
YQ
12691 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
12692 aexpr->len);
e8ba3115 12693 pkt = buf + strlen (buf);
b44ec619 12694 for (int ndx = 0; ndx < aexpr->len; ++ndx)
e8ba3115
YQ
12695 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
12696 *pkt = '\0';
35b1e5cc 12697 }
e8ba3115
YQ
12698 else
12699 warning (_("Target does not support conditional tracepoints, "
12700 "ignoring tp %d cond"), b->number);
12701 }
35b1e5cc 12702
d9b3f62e 12703 if (b->commands || *default_collect)
e8ba3115
YQ
12704 strcat (buf, "-");
12705 putpkt (buf);
b6bb3468
PA
12706 remote_get_noisy_reply ();
12707 if (strcmp (rs->buf, "OK"))
e8ba3115 12708 error (_("Target does not support tracepoints."));
35b1e5cc 12709
e8ba3115 12710 /* do_single_steps (t); */
b44ec619
SM
12711 for (auto action_it = tdp_actions.begin ();
12712 action_it != tdp_actions.end (); action_it++)
e8ba3115 12713 {
b44ec619
SM
12714 QUIT; /* Allow user to bail out with ^C. */
12715
12716 bool has_more = (action_it != tdp_actions.end ()
12717 || !stepping_actions.empty ());
12718
12719 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
12720 b->number, addrbuf, /* address */
12721 action_it->c_str (),
12722 has_more ? '-' : 0);
12723 putpkt (buf);
12724 remote_get_noisy_reply ();
12725 if (strcmp (rs->buf, "OK"))
12726 error (_("Error on target while setting tracepoints."));
e8ba3115 12727 }
409873ef 12728
b44ec619
SM
12729 for (auto action_it = stepping_actions.begin ();
12730 action_it != stepping_actions.end (); action_it++)
12731 {
12732 QUIT; /* Allow user to bail out with ^C. */
12733
12734 bool is_first = action_it == stepping_actions.begin ();
12735 bool has_more = action_it != stepping_actions.end ();
12736
12737 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
12738 b->number, addrbuf, /* address */
12739 is_first ? "S" : "",
12740 action_it->c_str (),
12741 has_more ? "-" : "");
12742 putpkt (buf);
12743 remote_get_noisy_reply ();
12744 if (strcmp (rs->buf, "OK"))
12745 error (_("Error on target while setting tracepoints."));
12746 }
12747
4082afcc 12748 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 12749 {
f00aae0f 12750 if (b->location != NULL)
409873ef 12751 {
e8ba3115 12752 strcpy (buf, "QTDPsrc:");
f00aae0f 12753 encode_source_string (b->number, loc->address, "at",
d28cd78a 12754 event_location_to_string (b->location.get ()),
f00aae0f 12755 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115 12756 putpkt (buf);
b6bb3468
PA
12757 remote_get_noisy_reply ();
12758 if (strcmp (rs->buf, "OK"))
e8ba3115 12759 warning (_("Target does not support source download."));
409873ef 12760 }
e8ba3115
YQ
12761 if (b->cond_string)
12762 {
12763 strcpy (buf, "QTDPsrc:");
12764 encode_source_string (b->number, loc->address,
12765 "cond", b->cond_string, buf + strlen (buf),
12766 2048 - strlen (buf));
12767 putpkt (buf);
b6bb3468
PA
12768 remote_get_noisy_reply ();
12769 if (strcmp (rs->buf, "OK"))
e8ba3115
YQ
12770 warning (_("Target does not support source download."));
12771 }
12772 remote_download_command_source (b->number, loc->address,
12773 breakpoint_commands (b));
35b1e5cc 12774 }
35b1e5cc
SS
12775}
12776
57810aa7 12777bool
f6ac5f3d 12778remote_target::can_download_tracepoint ()
1e4d1764 12779{
1e51243a
PA
12780 struct remote_state *rs = get_remote_state ();
12781 struct trace_status *ts;
12782 int status;
12783
12784 /* Don't try to install tracepoints until we've relocated our
12785 symbols, and fetched and merged the target's tracepoint list with
12786 ours. */
12787 if (rs->starting_up)
57810aa7 12788 return false;
1e51243a
PA
12789
12790 ts = current_trace_status ();
f6ac5f3d 12791 status = get_trace_status (ts);
1e4d1764
YQ
12792
12793 if (status == -1 || !ts->running_known || !ts->running)
57810aa7 12794 return false;
1e4d1764
YQ
12795
12796 /* If we are in a tracing experiment, but remote stub doesn't support
12797 installing tracepoint in trace, we have to return. */
12798 if (!remote_supports_install_in_trace ())
57810aa7 12799 return false;
1e4d1764 12800
57810aa7 12801 return true;
1e4d1764
YQ
12802}
12803
12804
f6ac5f3d
PA
12805void
12806remote_target::download_trace_state_variable (const trace_state_variable &tsv)
35b1e5cc
SS
12807{
12808 struct remote_state *rs = get_remote_state ();
00bf0b85 12809 char *p;
35b1e5cc 12810
bba74b36 12811 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
c252925c
SM
12812 tsv.number, phex ((ULONGEST) tsv.initial_value, 8),
12813 tsv.builtin);
00bf0b85 12814 p = rs->buf + strlen (rs->buf);
c252925c 12815 if ((p - rs->buf) + tsv.name.length () * 2 >= get_remote_packet_size ())
00bf0b85 12816 error (_("Trace state variable name too long for tsv definition packet"));
c252925c 12817 p += 2 * bin2hex ((gdb_byte *) (tsv.name.data ()), p, tsv.name.length ());
00bf0b85 12818 *p++ = '\0';
35b1e5cc 12819 putpkt (rs->buf);
b6bb3468
PA
12820 remote_get_noisy_reply ();
12821 if (*rs->buf == '\0')
ad91cd99 12822 error (_("Target does not support this command."));
b6bb3468 12823 if (strcmp (rs->buf, "OK") != 0)
ad91cd99 12824 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
12825}
12826
f6ac5f3d
PA
12827void
12828remote_target::enable_tracepoint (struct bp_location *location)
d248b706
KY
12829{
12830 struct remote_state *rs = get_remote_state ();
12831 char addr_buf[40];
12832
12833 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12834 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
12835 location->owner->number, addr_buf);
d248b706 12836 putpkt (rs->buf);
b6bb3468 12837 remote_get_noisy_reply ();
d248b706
KY
12838 if (*rs->buf == '\0')
12839 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
12840 if (strcmp (rs->buf, "OK") != 0)
12841 error (_("Error on target while enabling tracepoint."));
12842}
12843
f6ac5f3d
PA
12844void
12845remote_target::disable_tracepoint (struct bp_location *location)
d248b706
KY
12846{
12847 struct remote_state *rs = get_remote_state ();
12848 char addr_buf[40];
12849
12850 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12851 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
12852 location->owner->number, addr_buf);
d248b706 12853 putpkt (rs->buf);
b6bb3468 12854 remote_get_noisy_reply ();
d248b706
KY
12855 if (*rs->buf == '\0')
12856 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
12857 if (strcmp (rs->buf, "OK") != 0)
12858 error (_("Error on target while disabling tracepoint."));
12859}
12860
f6ac5f3d
PA
12861void
12862remote_target::trace_set_readonly_regions ()
35b1e5cc
SS
12863{
12864 asection *s;
81b9b86e 12865 bfd *abfd = NULL;
35b1e5cc 12866 bfd_size_type size;
608bcef2 12867 bfd_vma vma;
35b1e5cc 12868 int anysecs = 0;
c2fa21f1 12869 int offset = 0;
35b1e5cc
SS
12870
12871 if (!exec_bfd)
12872 return; /* No information to give. */
12873
b6bb3468
PA
12874 struct remote_state *rs = get_remote_state ();
12875
12876 strcpy (rs->buf, "QTro");
12877 offset = strlen (rs->buf);
35b1e5cc
SS
12878 for (s = exec_bfd->sections; s; s = s->next)
12879 {
12880 char tmp1[40], tmp2[40];
c2fa21f1 12881 int sec_length;
35b1e5cc
SS
12882
12883 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 12884 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
12885 (s->flags & SEC_READONLY) == 0)
12886 continue;
12887
12888 anysecs = 1;
81b9b86e 12889 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 12890 size = bfd_get_section_size (s);
608bcef2
HZ
12891 sprintf_vma (tmp1, vma);
12892 sprintf_vma (tmp2, vma + size);
c2fa21f1 12893 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
b6bb3468 12894 if (offset + sec_length + 1 > rs->buf_size)
c2fa21f1 12895 {
4082afcc 12896 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 12897 warning (_("\
c2fa21f1
HZ
12898Too many sections for read-only sections definition packet."));
12899 break;
12900 }
b6bb3468 12901 xsnprintf (rs->buf + offset, rs->buf_size - offset, ":%s,%s",
bba74b36 12902 tmp1, tmp2);
c2fa21f1 12903 offset += sec_length;
35b1e5cc
SS
12904 }
12905 if (anysecs)
12906 {
b6bb3468
PA
12907 putpkt (rs->buf);
12908 getpkt (&rs->buf, &rs->buf_size, 0);
35b1e5cc
SS
12909 }
12910}
12911
f6ac5f3d
PA
12912void
12913remote_target::trace_start ()
35b1e5cc 12914{
b6bb3468
PA
12915 struct remote_state *rs = get_remote_state ();
12916
35b1e5cc 12917 putpkt ("QTStart");
b6bb3468
PA
12918 remote_get_noisy_reply ();
12919 if (*rs->buf == '\0')
ad91cd99 12920 error (_("Target does not support this command."));
b6bb3468
PA
12921 if (strcmp (rs->buf, "OK") != 0)
12922 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12923}
12924
f6ac5f3d
PA
12925int
12926remote_target::get_trace_status (struct trace_status *ts)
35b1e5cc 12927{
953b98d1 12928 /* Initialize it just to avoid a GCC false warning. */
f652de6f 12929 char *p = NULL;
0df8b418 12930 /* FIXME we need to get register block size some other way. */
00bf0b85 12931 extern int trace_regblock_size;
bd3eecc3 12932 enum packet_result result;
b6bb3468 12933 struct remote_state *rs = get_remote_state ();
bd3eecc3 12934
4082afcc 12935 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 12936 return -1;
a744cf53 12937
5cd63fda 12938 trace_regblock_size
9d6eea31 12939 = rs->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet;
00bf0b85 12940
049dc89b
JK
12941 putpkt ("qTStatus");
12942
492d29ea 12943 TRY
67f41397 12944 {
b6bb3468 12945 p = remote_get_noisy_reply ();
67f41397 12946 }
492d29ea 12947 CATCH (ex, RETURN_MASK_ERROR)
67f41397 12948 {
598d3636
JK
12949 if (ex.error != TARGET_CLOSE_ERROR)
12950 {
12951 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
12952 return -1;
12953 }
12954 throw_exception (ex);
67f41397 12955 }
492d29ea 12956 END_CATCH
00bf0b85 12957
bd3eecc3
PA
12958 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
12959
00bf0b85 12960 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 12961 if (result == PACKET_UNKNOWN)
00bf0b85 12962 return -1;
35b1e5cc 12963
00bf0b85 12964 /* We're working with a live target. */
f5911ea1 12965 ts->filename = NULL;
00bf0b85 12966
00bf0b85 12967 if (*p++ != 'T')
b6bb3468 12968 error (_("Bogus trace status reply from target: %s"), rs->buf);
35b1e5cc 12969
84cebc4a
YQ
12970 /* Function 'parse_trace_status' sets default value of each field of
12971 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
12972 parse_trace_status (p, ts);
12973
12974 return ts->running;
35b1e5cc
SS
12975}
12976
f6ac5f3d
PA
12977void
12978remote_target::get_tracepoint_status (struct breakpoint *bp,
12979 struct uploaded_tp *utp)
f196051f
SS
12980{
12981 struct remote_state *rs = get_remote_state ();
f196051f
SS
12982 char *reply;
12983 struct bp_location *loc;
12984 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 12985 size_t size = get_remote_packet_size ();
f196051f
SS
12986
12987 if (tp)
12988 {
c1fc2657 12989 tp->hit_count = 0;
f196051f 12990 tp->traceframe_usage = 0;
c1fc2657 12991 for (loc = tp->loc; loc; loc = loc->next)
f196051f
SS
12992 {
12993 /* If the tracepoint was never downloaded, don't go asking for
12994 any status. */
12995 if (tp->number_on_target == 0)
12996 continue;
bba74b36
YQ
12997 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
12998 phex_nz (loc->address, 0));
f196051f 12999 putpkt (rs->buf);
b6bb3468 13000 reply = remote_get_noisy_reply ();
f196051f
SS
13001 if (reply && *reply)
13002 {
13003 if (*reply == 'V')
13004 parse_tracepoint_status (reply + 1, bp, utp);
13005 }
13006 }
13007 }
13008 else if (utp)
13009 {
13010 utp->hit_count = 0;
13011 utp->traceframe_usage = 0;
bba74b36
YQ
13012 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
13013 phex_nz (utp->addr, 0));
f196051f 13014 putpkt (rs->buf);
b6bb3468 13015 reply = remote_get_noisy_reply ();
f196051f
SS
13016 if (reply && *reply)
13017 {
13018 if (*reply == 'V')
13019 parse_tracepoint_status (reply + 1, bp, utp);
13020 }
13021 }
13022}
13023
f6ac5f3d
PA
13024void
13025remote_target::trace_stop ()
35b1e5cc 13026{
b6bb3468
PA
13027 struct remote_state *rs = get_remote_state ();
13028
35b1e5cc 13029 putpkt ("QTStop");
b6bb3468
PA
13030 remote_get_noisy_reply ();
13031 if (*rs->buf == '\0')
ad91cd99 13032 error (_("Target does not support this command."));
b6bb3468
PA
13033 if (strcmp (rs->buf, "OK") != 0)
13034 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
13035}
13036
f6ac5f3d
PA
13037int
13038remote_target::trace_find (enum trace_find_type type, int num,
13039 CORE_ADDR addr1, CORE_ADDR addr2,
13040 int *tpp)
35b1e5cc
SS
13041{
13042 struct remote_state *rs = get_remote_state ();
bba74b36 13043 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
13044 char *p, *reply;
13045 int target_frameno = -1, target_tracept = -1;
13046
e6e4e701
PA
13047 /* Lookups other than by absolute frame number depend on the current
13048 trace selected, so make sure it is correct on the remote end
13049 first. */
13050 if (type != tfind_number)
13051 set_remote_traceframe ();
13052
35b1e5cc
SS
13053 p = rs->buf;
13054 strcpy (p, "QTFrame:");
13055 p = strchr (p, '\0');
13056 switch (type)
13057 {
13058 case tfind_number:
bba74b36 13059 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
13060 break;
13061 case tfind_pc:
bba74b36 13062 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
13063 break;
13064 case tfind_tp:
bba74b36 13065 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
13066 break;
13067 case tfind_range:
bba74b36
YQ
13068 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
13069 phex_nz (addr2, 0));
35b1e5cc
SS
13070 break;
13071 case tfind_outside:
bba74b36
YQ
13072 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
13073 phex_nz (addr2, 0));
35b1e5cc
SS
13074 break;
13075 default:
9b20d036 13076 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
13077 }
13078
13079 putpkt (rs->buf);
b6bb3468 13080 reply = remote_get_noisy_reply ();
ad91cd99
PA
13081 if (*reply == '\0')
13082 error (_("Target does not support this command."));
35b1e5cc
SS
13083
13084 while (reply && *reply)
13085 switch (*reply)
13086 {
13087 case 'F':
f197e0f1
VP
13088 p = ++reply;
13089 target_frameno = (int) strtol (p, &reply, 16);
13090 if (reply == p)
13091 error (_("Unable to parse trace frame number"));
e6e4e701
PA
13092 /* Don't update our remote traceframe number cache on failure
13093 to select a remote traceframe. */
f197e0f1
VP
13094 if (target_frameno == -1)
13095 return -1;
35b1e5cc
SS
13096 break;
13097 case 'T':
f197e0f1
VP
13098 p = ++reply;
13099 target_tracept = (int) strtol (p, &reply, 16);
13100 if (reply == p)
13101 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
13102 break;
13103 case 'O': /* "OK"? */
13104 if (reply[1] == 'K' && reply[2] == '\0')
13105 reply += 2;
13106 else
13107 error (_("Bogus reply from target: %s"), reply);
13108 break;
13109 default:
13110 error (_("Bogus reply from target: %s"), reply);
13111 }
13112 if (tpp)
13113 *tpp = target_tracept;
e6e4e701 13114
262e1174 13115 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
13116 return target_frameno;
13117}
13118
57810aa7 13119bool
f6ac5f3d 13120remote_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
35b1e5cc
SS
13121{
13122 struct remote_state *rs = get_remote_state ();
13123 char *reply;
13124 ULONGEST uval;
13125
e6e4e701
PA
13126 set_remote_traceframe ();
13127
bba74b36 13128 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc 13129 putpkt (rs->buf);
b6bb3468 13130 reply = remote_get_noisy_reply ();
35b1e5cc
SS
13131 if (reply && *reply)
13132 {
13133 if (*reply == 'V')
13134 {
13135 unpack_varlen_hex (reply + 1, &uval);
13136 *val = (LONGEST) uval;
57810aa7 13137 return true;
35b1e5cc
SS
13138 }
13139 }
57810aa7 13140 return false;
35b1e5cc
SS
13141}
13142
f6ac5f3d
PA
13143int
13144remote_target::save_trace_data (const char *filename)
00bf0b85
SS
13145{
13146 struct remote_state *rs = get_remote_state ();
13147 char *p, *reply;
13148
13149 p = rs->buf;
13150 strcpy (p, "QTSave:");
13151 p += strlen (p);
13152 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
13153 error (_("Remote file name too long for trace save packet"));
9f1b45b0 13154 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
13155 *p++ = '\0';
13156 putpkt (rs->buf);
b6bb3468 13157 reply = remote_get_noisy_reply ();
d6c5869f 13158 if (*reply == '\0')
ad91cd99
PA
13159 error (_("Target does not support this command."));
13160 if (strcmp (reply, "OK") != 0)
13161 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
13162 return 0;
13163}
13164
13165/* This is basically a memory transfer, but needs to be its own packet
13166 because we don't know how the target actually organizes its trace
13167 memory, plus we want to be able to ask for as much as possible, but
13168 not be unhappy if we don't get as much as we ask for. */
13169
f6ac5f3d
PA
13170LONGEST
13171remote_target::get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
13172{
13173 struct remote_state *rs = get_remote_state ();
13174 char *reply;
13175 char *p;
13176 int rslt;
13177
13178 p = rs->buf;
13179 strcpy (p, "qTBuffer:");
13180 p += strlen (p);
13181 p += hexnumstr (p, offset);
13182 *p++ = ',';
13183 p += hexnumstr (p, len);
13184 *p++ = '\0';
13185
13186 putpkt (rs->buf);
b6bb3468 13187 reply = remote_get_noisy_reply ();
00bf0b85
SS
13188 if (reply && *reply)
13189 {
13190 /* 'l' by itself means we're at the end of the buffer and
13191 there is nothing more to get. */
13192 if (*reply == 'l')
13193 return 0;
13194
13195 /* Convert the reply into binary. Limit the number of bytes to
13196 convert according to our passed-in buffer size, rather than
13197 what was returned in the packet; if the target is
13198 unexpectedly generous and gives us a bigger reply than we
13199 asked for, we don't want to crash. */
b6bb3468 13200 rslt = hex2bin (reply, buf, len);
00bf0b85
SS
13201 return rslt;
13202 }
13203
13204 /* Something went wrong, flag as an error. */
13205 return -1;
13206}
13207
f6ac5f3d
PA
13208void
13209remote_target::set_disconnected_tracing (int val)
35b1e5cc
SS
13210{
13211 struct remote_state *rs = get_remote_state ();
13212
4082afcc 13213 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 13214 {
ad91cd99
PA
13215 char *reply;
13216
bba74b36 13217 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 13218 putpkt (rs->buf);
b6bb3468 13219 reply = remote_get_noisy_reply ();
ad91cd99 13220 if (*reply == '\0')
33da3f1c 13221 error (_("Target does not support this command."));
ad91cd99
PA
13222 if (strcmp (reply, "OK") != 0)
13223 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
13224 }
13225 else if (val)
13226 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
13227}
13228
f6ac5f3d
PA
13229int
13230remote_target::core_of_thread (ptid_t ptid)
dc146f7c
VP
13231{
13232 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 13233
7aabaf9d
SM
13234 if (info != NULL && info->priv != NULL)
13235 return get_remote_thread_info (info)->core;
13236
dc146f7c
VP
13237 return -1;
13238}
13239
f6ac5f3d
PA
13240void
13241remote_target::set_circular_trace_buffer (int val)
4daf5ac0
SS
13242{
13243 struct remote_state *rs = get_remote_state ();
ad91cd99 13244 char *reply;
4daf5ac0 13245
bba74b36 13246 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 13247 putpkt (rs->buf);
b6bb3468 13248 reply = remote_get_noisy_reply ();
ad91cd99 13249 if (*reply == '\0')
4daf5ac0 13250 error (_("Target does not support this command."));
ad91cd99
PA
13251 if (strcmp (reply, "OK") != 0)
13252 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
13253}
13254
f6ac5f3d
PA
13255traceframe_info_up
13256remote_target::traceframe_info ()
b3b9301e 13257{
9018be22 13258 gdb::optional<gdb::char_vector> text
f6ac5f3d 13259 = target_read_stralloc (target_stack, TARGET_OBJECT_TRACEFRAME_INFO,
b7b030ad 13260 NULL);
9018be22
SM
13261 if (text)
13262 return parse_traceframe_info (text->data ());
b3b9301e
PA
13263
13264 return NULL;
13265}
13266
405f8e94
SS
13267/* Handle the qTMinFTPILen packet. Returns the minimum length of
13268 instruction on which a fast tracepoint may be placed. Returns -1
13269 if the packet is not supported, and 0 if the minimum instruction
13270 length is unknown. */
13271
f6ac5f3d
PA
13272int
13273remote_target::get_min_fast_tracepoint_insn_len ()
405f8e94
SS
13274{
13275 struct remote_state *rs = get_remote_state ();
13276 char *reply;
13277
e886a173
PA
13278 /* If we're not debugging a process yet, the IPA can't be
13279 loaded. */
13280 if (!target_has_execution)
13281 return 0;
13282
13283 /* Make sure the remote is pointing at the right process. */
13284 set_general_process ();
13285
bba74b36 13286 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94 13287 putpkt (rs->buf);
b6bb3468 13288 reply = remote_get_noisy_reply ();
405f8e94
SS
13289 if (*reply == '\0')
13290 return -1;
13291 else
13292 {
13293 ULONGEST min_insn_len;
13294
13295 unpack_varlen_hex (reply, &min_insn_len);
13296
13297 return (int) min_insn_len;
13298 }
13299}
13300
f6ac5f3d
PA
13301void
13302remote_target::set_trace_buffer_size (LONGEST val)
f6f899bf 13303{
4082afcc 13304 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
13305 {
13306 struct remote_state *rs = get_remote_state ();
13307 char *buf = rs->buf;
13308 char *endbuf = rs->buf + get_remote_packet_size ();
13309 enum packet_result result;
13310
13311 gdb_assert (val >= 0 || val == -1);
13312 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
13313 /* Send -1 as literal "-1" to avoid host size dependency. */
13314 if (val < 0)
13315 {
13316 *buf++ = '-';
13317 buf += hexnumstr (buf, (ULONGEST) -val);
13318 }
13319 else
13320 buf += hexnumstr (buf, (ULONGEST) val);
13321
13322 putpkt (rs->buf);
b6bb3468 13323 remote_get_noisy_reply ();
f6f899bf
HAQ
13324 result = packet_ok (rs->buf,
13325 &remote_protocol_packets[PACKET_QTBuffer_size]);
13326
13327 if (result != PACKET_OK)
13328 warning (_("Bogus reply from target: %s"), rs->buf);
13329 }
13330}
13331
57810aa7 13332bool
f6ac5f3d
PA
13333remote_target::set_trace_notes (const char *user, const char *notes,
13334 const char *stop_notes)
f196051f
SS
13335{
13336 struct remote_state *rs = get_remote_state ();
13337 char *reply;
13338 char *buf = rs->buf;
13339 char *endbuf = rs->buf + get_remote_packet_size ();
13340 int nbytes;
13341
13342 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
13343 if (user)
13344 {
13345 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 13346 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
13347 buf += 2 * nbytes;
13348 *buf++ = ';';
13349 }
13350 if (notes)
13351 {
13352 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 13353 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
13354 buf += 2 * nbytes;
13355 *buf++ = ';';
13356 }
13357 if (stop_notes)
13358 {
13359 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 13360 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
13361 buf += 2 * nbytes;
13362 *buf++ = ';';
13363 }
13364 /* Ensure the buffer is terminated. */
13365 *buf = '\0';
13366
13367 putpkt (rs->buf);
b6bb3468 13368 reply = remote_get_noisy_reply ();
f196051f 13369 if (*reply == '\0')
57810aa7 13370 return false;
f196051f
SS
13371
13372 if (strcmp (reply, "OK") != 0)
13373 error (_("Bogus reply from target: %s"), reply);
13374
57810aa7 13375 return true;
f196051f
SS
13376}
13377
57810aa7
PA
13378bool
13379remote_target::use_agent (bool use)
d1feda86 13380{
4082afcc 13381 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
13382 {
13383 struct remote_state *rs = get_remote_state ();
13384
13385 /* If the stub supports QAgent. */
bba74b36 13386 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
13387 putpkt (rs->buf);
13388 getpkt (&rs->buf, &rs->buf_size, 0);
13389
13390 if (strcmp (rs->buf, "OK") == 0)
13391 {
f6ac5f3d 13392 ::use_agent = use;
57810aa7 13393 return true;
d1feda86
YQ
13394 }
13395 }
13396
57810aa7 13397 return false;
d1feda86
YQ
13398}
13399
57810aa7 13400bool
f6ac5f3d 13401remote_target::can_use_agent ()
d1feda86 13402{
4082afcc 13403 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
13404}
13405
9accd112
MM
13406struct btrace_target_info
13407{
13408 /* The ptid of the traced thread. */
13409 ptid_t ptid;
f4abbc16
MM
13410
13411 /* The obtained branch trace configuration. */
13412 struct btrace_config conf;
9accd112
MM
13413};
13414
f4abbc16
MM
13415/* Reset our idea of our target's btrace configuration. */
13416
13417static void
13418remote_btrace_reset (void)
13419{
13420 struct remote_state *rs = get_remote_state ();
13421
13422 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
13423}
13424
f4abbc16
MM
13425/* Synchronize the configuration with the target. */
13426
13427static void
13428btrace_sync_conf (const struct btrace_config *conf)
13429{
d33501a5
MM
13430 struct packet_config *packet;
13431 struct remote_state *rs;
13432 char *buf, *pos, *endbuf;
13433
13434 rs = get_remote_state ();
13435 buf = rs->buf;
13436 endbuf = buf + get_remote_packet_size ();
13437
13438 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
13439 if (packet_config_support (packet) == PACKET_ENABLE
13440 && conf->bts.size != rs->btrace_config.bts.size)
13441 {
13442 pos = buf;
13443 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13444 conf->bts.size);
13445
13446 putpkt (buf);
13447 getpkt (&buf, &rs->buf_size, 0);
13448
13449 if (packet_ok (buf, packet) == PACKET_ERROR)
13450 {
13451 if (buf[0] == 'E' && buf[1] == '.')
13452 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
13453 else
13454 error (_("Failed to configure the BTS buffer size."));
13455 }
13456
13457 rs->btrace_config.bts.size = conf->bts.size;
13458 }
b20a6524
MM
13459
13460 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
13461 if (packet_config_support (packet) == PACKET_ENABLE
13462 && conf->pt.size != rs->btrace_config.pt.size)
13463 {
13464 pos = buf;
13465 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13466 conf->pt.size);
13467
13468 putpkt (buf);
13469 getpkt (&buf, &rs->buf_size, 0);
13470
13471 if (packet_ok (buf, packet) == PACKET_ERROR)
13472 {
13473 if (buf[0] == 'E' && buf[1] == '.')
13474 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
13475 else
13476 error (_("Failed to configure the trace buffer size."));
13477 }
13478
13479 rs->btrace_config.pt.size = conf->pt.size;
13480 }
f4abbc16
MM
13481}
13482
13483/* Read the current thread's btrace configuration from the target and
13484 store it into CONF. */
13485
13486static void
13487btrace_read_config (struct btrace_config *conf)
13488{
9018be22 13489 gdb::optional<gdb::char_vector> xml
f6ac5f3d 13490 = target_read_stralloc (target_stack, TARGET_OBJECT_BTRACE_CONF, "");
9018be22
SM
13491 if (xml)
13492 parse_xml_btrace_conf (conf, xml->data ());
f4abbc16
MM
13493}
13494
c0272db5
TW
13495/* Maybe reopen target btrace. */
13496
13497static void
13498remote_btrace_maybe_reopen (void)
13499{
13500 struct remote_state *rs = get_remote_state ();
c0272db5
TW
13501 struct thread_info *tp;
13502 int btrace_target_pushed = 0;
13503 int warned = 0;
13504
5ed8105e
PA
13505 scoped_restore_current_thread restore_thread;
13506
c0272db5
TW
13507 ALL_NON_EXITED_THREADS (tp)
13508 {
13509 set_general_thread (tp->ptid);
13510
13511 memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
13512 btrace_read_config (&rs->btrace_config);
13513
13514 if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
13515 continue;
13516
13517#if !defined (HAVE_LIBIPT)
13518 if (rs->btrace_config.format == BTRACE_FORMAT_PT)
13519 {
13520 if (!warned)
13521 {
13522 warned = 1;
c4e12631
MM
13523 warning (_("Target is recording using Intel Processor Trace "
13524 "but support was disabled at compile time."));
c0272db5
TW
13525 }
13526
13527 continue;
13528 }
13529#endif /* !defined (HAVE_LIBIPT) */
13530
13531 /* Push target, once, but before anything else happens. This way our
13532 changes to the threads will be cleaned up by unpushing the target
13533 in case btrace_read_config () throws. */
13534 if (!btrace_target_pushed)
13535 {
13536 btrace_target_pushed = 1;
13537 record_btrace_push_target ();
13538 printf_filtered (_("Target is recording using %s.\n"),
13539 btrace_format_string (rs->btrace_config.format));
13540 }
13541
13542 tp->btrace.target = XCNEW (struct btrace_target_info);
13543 tp->btrace.target->ptid = tp->ptid;
13544 tp->btrace.target->conf = rs->btrace_config;
13545 }
c0272db5
TW
13546}
13547
9accd112
MM
13548/* Enable branch tracing. */
13549
f6ac5f3d
PA
13550struct btrace_target_info *
13551remote_target::enable_btrace (ptid_t ptid, const struct btrace_config *conf)
9accd112
MM
13552{
13553 struct btrace_target_info *tinfo = NULL;
b20a6524 13554 struct packet_config *packet = NULL;
9accd112
MM
13555 struct remote_state *rs = get_remote_state ();
13556 char *buf = rs->buf;
13557 char *endbuf = rs->buf + get_remote_packet_size ();
13558
b20a6524
MM
13559 switch (conf->format)
13560 {
13561 case BTRACE_FORMAT_BTS:
13562 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
13563 break;
13564
13565 case BTRACE_FORMAT_PT:
13566 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
13567 break;
13568 }
13569
13570 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13571 error (_("Target does not support branch tracing."));
13572
f4abbc16
MM
13573 btrace_sync_conf (conf);
13574
9accd112
MM
13575 set_general_thread (ptid);
13576
13577 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13578 putpkt (rs->buf);
13579 getpkt (&rs->buf, &rs->buf_size, 0);
13580
13581 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13582 {
13583 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13584 error (_("Could not enable branch tracing for %s: %s"),
13585 target_pid_to_str (ptid), rs->buf + 2);
13586 else
13587 error (_("Could not enable branch tracing for %s."),
13588 target_pid_to_str (ptid));
13589 }
13590
8d749320 13591 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
13592 tinfo->ptid = ptid;
13593
f4abbc16
MM
13594 /* If we fail to read the configuration, we lose some information, but the
13595 tracing itself is not impacted. */
492d29ea
PA
13596 TRY
13597 {
13598 btrace_read_config (&tinfo->conf);
13599 }
13600 CATCH (err, RETURN_MASK_ERROR)
13601 {
13602 if (err.message != NULL)
13603 warning ("%s", err.message);
13604 }
13605 END_CATCH
f4abbc16 13606
9accd112
MM
13607 return tinfo;
13608}
13609
13610/* Disable branch tracing. */
13611
f6ac5f3d
PA
13612void
13613remote_target::disable_btrace (struct btrace_target_info *tinfo)
9accd112
MM
13614{
13615 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
13616 struct remote_state *rs = get_remote_state ();
13617 char *buf = rs->buf;
13618 char *endbuf = rs->buf + get_remote_packet_size ();
13619
4082afcc 13620 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13621 error (_("Target does not support branch tracing."));
13622
13623 set_general_thread (tinfo->ptid);
13624
13625 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13626 putpkt (rs->buf);
13627 getpkt (&rs->buf, &rs->buf_size, 0);
13628
13629 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13630 {
13631 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13632 error (_("Could not disable branch tracing for %s: %s"),
13633 target_pid_to_str (tinfo->ptid), rs->buf + 2);
13634 else
13635 error (_("Could not disable branch tracing for %s."),
13636 target_pid_to_str (tinfo->ptid));
13637 }
13638
13639 xfree (tinfo);
13640}
13641
13642/* Teardown branch tracing. */
13643
f6ac5f3d
PA
13644void
13645remote_target::teardown_btrace (struct btrace_target_info *tinfo)
9accd112
MM
13646{
13647 /* We must not talk to the target during teardown. */
13648 xfree (tinfo);
13649}
13650
13651/* Read the branch trace. */
13652
f6ac5f3d
PA
13653enum btrace_error
13654remote_target::read_btrace (struct btrace_data *btrace,
13655 struct btrace_target_info *tinfo,
13656 enum btrace_read_type type)
9accd112
MM
13657{
13658 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
9accd112 13659 const char *annex;
9accd112 13660
4082afcc 13661 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13662 error (_("Target does not support branch tracing."));
13663
13664#if !defined(HAVE_LIBEXPAT)
13665 error (_("Cannot process branch tracing result. XML parsing not supported."));
13666#endif
13667
13668 switch (type)
13669 {
864089d2 13670 case BTRACE_READ_ALL:
9accd112
MM
13671 annex = "all";
13672 break;
864089d2 13673 case BTRACE_READ_NEW:
9accd112
MM
13674 annex = "new";
13675 break;
969c39fb
MM
13676 case BTRACE_READ_DELTA:
13677 annex = "delta";
13678 break;
9accd112
MM
13679 default:
13680 internal_error (__FILE__, __LINE__,
13681 _("Bad branch tracing read type: %u."),
13682 (unsigned int) type);
13683 }
13684
9018be22 13685 gdb::optional<gdb::char_vector> xml
f6ac5f3d 13686 = target_read_stralloc (target_stack, TARGET_OBJECT_BTRACE, annex);
9018be22 13687 if (!xml)
969c39fb 13688 return BTRACE_ERR_UNKNOWN;
9accd112 13689
9018be22 13690 parse_xml_btrace (btrace, xml->data ());
9accd112 13691
969c39fb 13692 return BTRACE_ERR_NONE;
9accd112
MM
13693}
13694
f6ac5f3d
PA
13695const struct btrace_config *
13696remote_target::btrace_conf (const struct btrace_target_info *tinfo)
f4abbc16
MM
13697{
13698 return &tinfo->conf;
13699}
13700
57810aa7 13701bool
f6ac5f3d 13702remote_target::augmented_libraries_svr4_read ()
ced63ec0 13703{
4082afcc
PA
13704 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
13705 == PACKET_ENABLE);
ced63ec0
GB
13706}
13707
9dd130a0
TT
13708/* Implementation of to_load. */
13709
f6ac5f3d
PA
13710void
13711remote_target::load (const char *name, int from_tty)
9dd130a0
TT
13712{
13713 generic_load (name, from_tty);
13714}
13715
c78fa86a
GB
13716/* Accepts an integer PID; returns a string representing a file that
13717 can be opened on the remote side to get the symbols for the child
13718 process. Returns NULL if the operation is not supported. */
13719
f6ac5f3d
PA
13720char *
13721remote_target::pid_to_exec_file (int pid)
c78fa86a 13722{
9018be22 13723 static gdb::optional<gdb::char_vector> filename;
835205d0
GB
13724 struct inferior *inf;
13725 char *annex = NULL;
c78fa86a
GB
13726
13727 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
13728 return NULL;
13729
835205d0
GB
13730 inf = find_inferior_pid (pid);
13731 if (inf == NULL)
13732 internal_error (__FILE__, __LINE__,
13733 _("not currently attached to process %d"), pid);
13734
13735 if (!inf->fake_pid_p)
13736 {
13737 const int annex_size = 9;
13738
224c3ddb 13739 annex = (char *) alloca (annex_size);
835205d0
GB
13740 xsnprintf (annex, annex_size, "%x", pid);
13741 }
13742
f6ac5f3d 13743 filename = target_read_stralloc (target_stack,
c78fa86a
GB
13744 TARGET_OBJECT_EXEC_FILE, annex);
13745
9018be22 13746 return filename ? filename->data () : nullptr;
c78fa86a
GB
13747}
13748
750ce8d1
YQ
13749/* Implement the to_can_do_single_step target_ops method. */
13750
f6ac5f3d
PA
13751int
13752remote_target::can_do_single_step ()
750ce8d1
YQ
13753{
13754 /* We can only tell whether target supports single step or not by
13755 supported s and S vCont actions if the stub supports vContSupported
13756 feature. If the stub doesn't support vContSupported feature,
13757 we have conservatively to think target doesn't supports single
13758 step. */
13759 if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
13760 {
13761 struct remote_state *rs = get_remote_state ();
13762
13763 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
13764 remote_vcont_probe (rs);
13765
13766 return rs->supports_vCont.s && rs->supports_vCont.S;
13767 }
13768 else
13769 return 0;
13770}
13771
3a00c802
PA
13772/* Implementation of the to_execution_direction method for the remote
13773 target. */
13774
f6ac5f3d
PA
13775enum exec_direction_kind
13776remote_target::execution_direction ()
3a00c802
PA
13777{
13778 struct remote_state *rs = get_remote_state ();
13779
13780 return rs->last_resume_exec_dir;
13781}
13782
f6327dcb
KB
13783/* Return pointer to the thread_info struct which corresponds to
13784 THREAD_HANDLE (having length HANDLE_LEN). */
13785
f6ac5f3d
PA
13786thread_info *
13787remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
13788 int handle_len,
13789 inferior *inf)
f6327dcb
KB
13790{
13791 struct thread_info *tp;
13792
13793 ALL_NON_EXITED_THREADS (tp)
13794 {
7aabaf9d 13795 remote_thread_info *priv = get_remote_thread_info (tp);
f6327dcb
KB
13796
13797 if (tp->inf == inf && priv != NULL)
13798 {
7aabaf9d 13799 if (handle_len != priv->thread_handle.size ())
f6327dcb 13800 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
7aabaf9d
SM
13801 handle_len, priv->thread_handle.size ());
13802 if (memcmp (thread_handle, priv->thread_handle.data (),
f6327dcb
KB
13803 handle_len) == 0)
13804 return tp;
13805 }
13806 }
13807
13808 return NULL;
13809}
13810
57810aa7 13811bool
f6ac5f3d 13812remote_target::can_async_p ()
6426a772 13813{
5d93a237
TT
13814 struct remote_state *rs = get_remote_state ();
13815
3015c064
SM
13816 /* We don't go async if the user has explicitly prevented it with the
13817 "maint set target-async" command. */
c6ebd6cf 13818 if (!target_async_permitted)
57810aa7 13819 return false;
75c99385 13820
23860348 13821 /* We're async whenever the serial device is. */
5d93a237 13822 return serial_can_async_p (rs->remote_desc);
6426a772
JM
13823}
13824
57810aa7 13825bool
f6ac5f3d 13826remote_target::is_async_p ()
6426a772 13827{
5d93a237
TT
13828 struct remote_state *rs = get_remote_state ();
13829
c6ebd6cf 13830 if (!target_async_permitted)
75c99385 13831 /* We only enable async when the user specifically asks for it. */
57810aa7 13832 return false;
75c99385 13833
23860348 13834 /* We're async whenever the serial device is. */
5d93a237 13835 return serial_is_async_p (rs->remote_desc);
6426a772
JM
13836}
13837
2acceee2
JM
13838/* Pass the SERIAL event on and up to the client. One day this code
13839 will be able to delay notifying the client of an event until the
23860348 13840 point where an entire packet has been received. */
2acceee2 13841
2acceee2
JM
13842static serial_event_ftype remote_async_serial_handler;
13843
6426a772 13844static void
819cc324 13845remote_async_serial_handler (struct serial *scb, void *context)
6426a772 13846{
2acceee2
JM
13847 /* Don't propogate error information up to the client. Instead let
13848 the client find out about the error by querying the target. */
6a3753b3 13849 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
13850}
13851
74531fed
PA
13852static void
13853remote_async_inferior_event_handler (gdb_client_data data)
13854{
13855 inferior_event_handler (INF_REG_EVENT, NULL);
13856}
13857
f6ac5f3d
PA
13858void
13859remote_target::async (int enable)
2acceee2 13860{
5d93a237
TT
13861 struct remote_state *rs = get_remote_state ();
13862
6a3753b3 13863 if (enable)
2acceee2 13864 {
88b496c3 13865 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
13866
13867 /* If there are pending events in the stop reply queue tell the
13868 event loop to process them. */
13869 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
13870 mark_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13871 /* For simplicity, below we clear the pending events token
13872 without remembering whether it is marked, so here we always
13873 mark it. If there's actually no pending notification to
13874 process, this ends up being a no-op (other than a spurious
13875 event-loop wakeup). */
13876 if (target_is_non_stop_p ())
13877 mark_async_event_handler (rs->notif_state->get_pending_events_token);
2acceee2
JM
13878 }
13879 else
b7d2e916
PA
13880 {
13881 serial_async (rs->remote_desc, NULL, NULL);
6efcd9a8
PA
13882 /* If the core is disabling async, it doesn't want to be
13883 disturbed with target events. Clear all async event sources
13884 too. */
b7d2e916 13885 clear_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13886 if (target_is_non_stop_p ())
13887 clear_async_event_handler (rs->notif_state->get_pending_events_token);
b7d2e916 13888 }
6426a772
JM
13889}
13890
65706a29
PA
13891/* Implementation of the to_thread_events method. */
13892
f6ac5f3d
PA
13893void
13894remote_target::thread_events (int enable)
65706a29
PA
13895{
13896 struct remote_state *rs = get_remote_state ();
13897 size_t size = get_remote_packet_size ();
65706a29
PA
13898
13899 if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
13900 return;
13901
13902 xsnprintf (rs->buf, size, "QThreadEvents:%x", enable ? 1 : 0);
13903 putpkt (rs->buf);
13904 getpkt (&rs->buf, &rs->buf_size, 0);
13905
13906 switch (packet_ok (rs->buf,
13907 &remote_protocol_packets[PACKET_QThreadEvents]))
13908 {
13909 case PACKET_OK:
13910 if (strcmp (rs->buf, "OK") != 0)
13911 error (_("Remote refused setting thread events: %s"), rs->buf);
13912 break;
13913 case PACKET_ERROR:
13914 warning (_("Remote failure reply: %s"), rs->buf);
13915 break;
13916 case PACKET_UNKNOWN:
13917 break;
13918 }
13919}
13920
5a2468f5 13921static void
981a3fb3 13922set_remote_cmd (const char *args, int from_tty)
5a2468f5 13923{
635c7e8a 13924 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
13925}
13926
d471ea57 13927static void
981a3fb3 13928show_remote_cmd (const char *args, int from_tty)
d471ea57 13929{
37a105a1 13930 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 13931 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1 13932 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 13933 struct ui_out *uiout = current_uiout;
37a105a1 13934
2e783024 13935 ui_out_emit_tuple tuple_emitter (uiout, "showlist");
37a105a1
DJ
13936 for (; list != NULL; list = list->next)
13937 if (strcmp (list->name, "Z-packet") == 0)
13938 continue;
427c3a89
DJ
13939 else if (list->type == not_set_cmd)
13940 /* Alias commands are exactly like the original, except they
13941 don't have the normal type. */
13942 continue;
13943 else
37a105a1 13944 {
2e783024 13945 ui_out_emit_tuple option_emitter (uiout, "option");
a744cf53 13946
112e8700
SM
13947 uiout->field_string ("name", list->name);
13948 uiout->text (": ");
427c3a89 13949 if (list->type == show_cmd)
f5c4fcd9 13950 do_show_command (NULL, from_tty, list);
427c3a89
DJ
13951 else
13952 cmd_func (list, NULL, from_tty);
37a105a1 13953 }
d471ea57 13954}
5a2468f5 13955
0f71a2f6 13956
23860348 13957/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
13958static void
13959remote_new_objfile (struct objfile *objfile)
13960{
5d93a237
TT
13961 struct remote_state *rs = get_remote_state ();
13962
13963 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 13964 remote_check_symbols ();
dc8acb97
MS
13965}
13966
00bf0b85
SS
13967/* Pull all the tracepoints defined on the target and create local
13968 data structures representing them. We don't want to create real
13969 tracepoints yet, we don't want to mess up the user's existing
13970 collection. */
13971
f6ac5f3d
PA
13972int
13973remote_target::upload_tracepoints (struct uploaded_tp **utpp)
d5551862 13974{
00bf0b85
SS
13975 struct remote_state *rs = get_remote_state ();
13976 char *p;
d5551862 13977
00bf0b85
SS
13978 /* Ask for a first packet of tracepoint definition. */
13979 putpkt ("qTfP");
13980 getpkt (&rs->buf, &rs->buf_size, 0);
13981 p = rs->buf;
13982 while (*p && *p != 'l')
d5551862 13983 {
00bf0b85
SS
13984 parse_tracepoint_definition (p, utpp);
13985 /* Ask for another packet of tracepoint definition. */
13986 putpkt ("qTsP");
13987 getpkt (&rs->buf, &rs->buf_size, 0);
13988 p = rs->buf;
d5551862 13989 }
00bf0b85 13990 return 0;
d5551862
SS
13991}
13992
f6ac5f3d
PA
13993int
13994remote_target::upload_trace_state_variables (struct uploaded_tsv **utsvp)
d5551862 13995{
00bf0b85 13996 struct remote_state *rs = get_remote_state ();
d5551862 13997 char *p;
d5551862 13998
00bf0b85
SS
13999 /* Ask for a first packet of variable definition. */
14000 putpkt ("qTfV");
d5551862
SS
14001 getpkt (&rs->buf, &rs->buf_size, 0);
14002 p = rs->buf;
00bf0b85 14003 while (*p && *p != 'l')
d5551862 14004 {
00bf0b85
SS
14005 parse_tsv_definition (p, utsvp);
14006 /* Ask for another packet of variable definition. */
14007 putpkt ("qTsV");
d5551862
SS
14008 getpkt (&rs->buf, &rs->buf_size, 0);
14009 p = rs->buf;
14010 }
00bf0b85 14011 return 0;
d5551862
SS
14012}
14013
c1e36e3e
PA
14014/* The "set/show range-stepping" show hook. */
14015
14016static void
14017show_range_stepping (struct ui_file *file, int from_tty,
14018 struct cmd_list_element *c,
14019 const char *value)
14020{
14021 fprintf_filtered (file,
14022 _("Debugger's willingness to use range stepping "
14023 "is %s.\n"), value);
14024}
14025
14026/* The "set/show range-stepping" set hook. */
14027
14028static void
eb4c3f4a 14029set_range_stepping (const char *ignore_args, int from_tty,
c1e36e3e
PA
14030 struct cmd_list_element *c)
14031{
5d93a237
TT
14032 struct remote_state *rs = get_remote_state ();
14033
c1e36e3e
PA
14034 /* Whene enabling, check whether range stepping is actually
14035 supported by the target, and warn if not. */
14036 if (use_range_stepping)
14037 {
5d93a237 14038 if (rs->remote_desc != NULL)
c1e36e3e 14039 {
4082afcc 14040 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
14041 remote_vcont_probe (rs);
14042
4082afcc 14043 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
14044 && rs->supports_vCont.r)
14045 return;
14046 }
14047
14048 warning (_("Range stepping is not supported by the current target"));
14049 }
14050}
14051
c906108c 14052void
fba45db2 14053_initialize_remote (void)
c906108c 14054{
9a7071a8 14055 struct cmd_list_element *cmd;
6f937416 14056 const char *cmd_name;
ea9c271d 14057
0f71a2f6 14058 /* architecture specific data */
29709017
DJ
14059 remote_g_packet_data_handle =
14060 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 14061
94585166
DB
14062 remote_pspace_data
14063 = register_program_space_data_with_cleanup (NULL,
14064 remote_pspace_data_cleanup);
14065
ea9c271d
DJ
14066 /* Initialize the per-target state. At the moment there is only one
14067 of these, not one per target. Only one target is active at a
cf792862 14068 time. */
de44f5a7 14069 remote_state = new struct remote_state ();
ea9c271d 14070
d9f719f1
PA
14071 add_target (remote_target_info, remote_target::open);
14072 add_target (extended_remote_target_info, extended_remote_target::open);
cce74817 14073
dc8acb97 14074 /* Hook into new objfile notification. */
76727919 14075 gdb::observers::new_objfile.attach (remote_new_objfile);
5f4cf0bb
YQ
14076 /* We're no longer interested in notification events of an inferior
14077 when it exits. */
76727919 14078 gdb::observers::inferior_exit.attach (discard_pending_stop_replies);
dc8acb97 14079
c906108c
SS
14080#if 0
14081 init_remote_threadtests ();
14082#endif
14083
722247f1 14084 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 14085 /* set/show remote ... */
d471ea57 14086
1bedd215 14087 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
14088Remote protocol specific variables\n\
14089Configure various remote-protocol specific variables such as\n\
1bedd215 14090the packets being used"),
cff3e48b 14091 &remote_set_cmdlist, "set remote ",
23860348 14092 0 /* allow-unknown */, &setlist);
1bedd215 14093 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
14094Remote protocol specific variables\n\
14095Configure various remote-protocol specific variables such as\n\
1bedd215 14096the packets being used"),
cff3e48b 14097 &remote_show_cmdlist, "show remote ",
23860348 14098 0 /* allow-unknown */, &showlist);
5a2468f5 14099
1a966eab
AC
14100 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
14101Compare section data on target to the exec file.\n\
95cf3b38
DT
14102Argument is a single section name (default: all loaded sections).\n\
14103To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
14104 &cmdlist);
14105
1a966eab
AC
14106 add_cmd ("packet", class_maintenance, packet_command, _("\
14107Send an arbitrary packet to a remote target.\n\
c906108c
SS
14108 maintenance packet TEXT\n\
14109If GDB is talking to an inferior via the GDB serial protocol, then\n\
14110this command sends the string TEXT to the inferior, and displays the\n\
14111response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 14112terminating `#' character and checksum."),
c906108c
SS
14113 &maintenancelist);
14114
7915a72c
AC
14115 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
14116Set whether to send break if interrupted."), _("\
14117Show whether to send break if interrupted."), _("\
14118If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 14119 set_remotebreak, show_remotebreak,
e707bbc2 14120 &setlist, &showlist);
9a7071a8
JB
14121 cmd_name = "remotebreak";
14122 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
14123 deprecate_cmd (cmd, "set remote interrupt-sequence");
14124 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
14125 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
14126 deprecate_cmd (cmd, "show remote interrupt-sequence");
14127
14128 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
14129 interrupt_sequence_modes, &interrupt_sequence_mode,
14130 _("\
9a7071a8
JB
14131Set interrupt sequence to remote target."), _("\
14132Show interrupt sequence to remote target."), _("\
14133Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14134 NULL, show_interrupt_sequence,
14135 &remote_set_cmdlist,
14136 &remote_show_cmdlist);
14137
14138 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
14139 &interrupt_on_connect, _("\
14140Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14141Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14142If set, interrupt sequence is sent to remote target."),
14143 NULL, NULL,
14144 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 14145
23860348 14146 /* Install commands for configuring memory read/write packets. */
11cf8741 14147
1a966eab
AC
14148 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
14149Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 14150 &setlist);
1a966eab
AC
14151 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
14152Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
14153 &showlist);
14154 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
14155 set_memory_write_packet_size, _("\
14156Set the maximum number of bytes per memory-write packet.\n\
14157Specify the number of bytes in a packet or 0 (zero) for the\n\
14158default packet size. The actual limit is further reduced\n\
14159dependent on the target. Specify ``fixed'' to disable the\n\
14160further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14161 &remote_set_cmdlist);
14162 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
14163 set_memory_read_packet_size, _("\
14164Set the maximum number of bytes per memory-read packet.\n\
14165Specify the number of bytes in a packet or 0 (zero) for the\n\
14166default packet size. The actual limit is further reduced\n\
14167dependent on the target. Specify ``fixed'' to disable the\n\
14168further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14169 &remote_set_cmdlist);
14170 add_cmd ("memory-write-packet-size", no_class,
14171 show_memory_write_packet_size,
1a966eab 14172 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
14173 &remote_show_cmdlist);
14174 add_cmd ("memory-read-packet-size", no_class,
14175 show_memory_read_packet_size,
1a966eab 14176 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 14177 &remote_show_cmdlist);
c906108c 14178
b3f42336 14179 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
14180 &remote_hw_watchpoint_limit, _("\
14181Set the maximum number of target hardware watchpoints."), _("\
14182Show the maximum number of target hardware watchpoints."), _("\
14183Specify a negative limit for unlimited."),
3e43a32a
MS
14184 NULL, NULL, /* FIXME: i18n: The maximum
14185 number of target hardware
14186 watchpoints is %s. */
b3f42336 14187 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
14188 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
14189 &remote_hw_watchpoint_length_limit, _("\
14190Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
14191Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
14192Specify a negative limit for unlimited."),
14193 NULL, NULL, /* FIXME: i18n: The maximum
14194 length (in bytes) of a target
14195 hardware watchpoint is %s. */
14196 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 14197 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
14198 &remote_hw_breakpoint_limit, _("\
14199Set the maximum number of target hardware breakpoints."), _("\
14200Show the maximum number of target hardware breakpoints."), _("\
14201Specify a negative limit for unlimited."),
3e43a32a
MS
14202 NULL, NULL, /* FIXME: i18n: The maximum
14203 number of target hardware
14204 breakpoints is %s. */
b3f42336 14205 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 14206
1b493192
PA
14207 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
14208 &remote_address_size, _("\
4d28ad1e
AC
14209Set the maximum size of the address (in bits) in a memory packet."), _("\
14210Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
14211 NULL,
14212 NULL, /* FIXME: i18n: */
14213 &setlist, &showlist);
c906108c 14214
ca4f7f8b
PA
14215 init_all_packet_configs ();
14216
444abaca 14217 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 14218 "X", "binary-download", 1);
0f71a2f6 14219
444abaca 14220 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 14221 "vCont", "verbose-resume", 0);
506fb367 14222
89be2091
DJ
14223 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
14224 "QPassSignals", "pass-signals", 0);
14225
82075af2
JS
14226 add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
14227 "QCatchSyscalls", "catch-syscalls", 0);
14228
9b224c5e
PA
14229 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
14230 "QProgramSignals", "program-signals", 0);
14231
bc3b087d
SDJ
14232 add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
14233 "QSetWorkingDir", "set-working-dir", 0);
14234
aefd8b33
SDJ
14235 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
14236 "QStartupWithShell", "startup-with-shell", 0);
14237
0a2dde4a
SDJ
14238 add_packet_config_cmd (&remote_protocol_packets
14239 [PACKET_QEnvironmentHexEncoded],
14240 "QEnvironmentHexEncoded", "environment-hex-encoded",
14241 0);
14242
14243 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
14244 "QEnvironmentReset", "environment-reset",
14245 0);
14246
14247 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
14248 "QEnvironmentUnset", "environment-unset",
14249 0);
14250
444abaca 14251 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 14252 "qSymbol", "symbol-lookup", 0);
dc8acb97 14253
444abaca 14254 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 14255 "P", "set-register", 1);
d471ea57 14256
444abaca 14257 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 14258 "p", "fetch-register", 1);
b96ec7ac 14259
444abaca 14260 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 14261 "Z0", "software-breakpoint", 0);
d471ea57 14262
444abaca 14263 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 14264 "Z1", "hardware-breakpoint", 0);
d471ea57 14265
444abaca 14266 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 14267 "Z2", "write-watchpoint", 0);
d471ea57 14268
444abaca 14269 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 14270 "Z3", "read-watchpoint", 0);
d471ea57 14271
444abaca 14272 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 14273 "Z4", "access-watchpoint", 0);
d471ea57 14274
0876f84a
DJ
14275 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
14276 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 14277
c78fa86a
GB
14278 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
14279 "qXfer:exec-file:read", "pid-to-exec-file", 0);
14280
23181151
DJ
14281 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
14282 "qXfer:features:read", "target-features", 0);
14283
cfa9d6d9
DJ
14284 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
14285 "qXfer:libraries:read", "library-info", 0);
14286
2268b414
JK
14287 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
14288 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
14289
fd79ecee
DJ
14290 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
14291 "qXfer:memory-map:read", "memory-map", 0);
14292
0e7f50da
UW
14293 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
14294 "qXfer:spu:read", "read-spu-object", 0);
14295
14296 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
14297 "qXfer:spu:write", "write-spu-object", 0);
14298
07e059b5
VP
14299 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
14300 "qXfer:osdata:read", "osdata", 0);
14301
dc146f7c
VP
14302 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
14303 "qXfer:threads:read", "threads", 0);
14304
4aa995e1
PA
14305 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
14306 "qXfer:siginfo:read", "read-siginfo-object", 0);
14307
14308 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
14309 "qXfer:siginfo:write", "write-siginfo-object", 0);
14310
b3b9301e
PA
14311 add_packet_config_cmd
14312 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 14313 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 14314
169081d0
TG
14315 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
14316 "qXfer:uib:read", "unwind-info-block", 0);
14317
444abaca 14318 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 14319 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
14320 0);
14321
711e434b
PM
14322 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
14323 "qGetTIBAddr", "get-thread-information-block-address",
14324 0);
14325
40ab02ce
MS
14326 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
14327 "bc", "reverse-continue", 0);
14328
14329 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
14330 "bs", "reverse-step", 0);
14331
be2a5f71
DJ
14332 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
14333 "qSupported", "supported-packets", 0);
14334
08388c79
DE
14335 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
14336 "qSearch:memory", "search-memory", 0);
14337
bd3eecc3
PA
14338 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
14339 "qTStatus", "trace-status", 0);
14340
15a201c8
GB
14341 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
14342 "vFile:setfs", "hostio-setfs", 0);
14343
a6b151f1
DJ
14344 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
14345 "vFile:open", "hostio-open", 0);
14346
14347 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
14348 "vFile:pread", "hostio-pread", 0);
14349
14350 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
14351 "vFile:pwrite", "hostio-pwrite", 0);
14352
14353 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
14354 "vFile:close", "hostio-close", 0);
14355
14356 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
14357 "vFile:unlink", "hostio-unlink", 0);
14358
b9e7b9c3
UW
14359 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
14360 "vFile:readlink", "hostio-readlink", 0);
14361
0a93529c
GB
14362 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
14363 "vFile:fstat", "hostio-fstat", 0);
14364
2d717e4f
DJ
14365 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
14366 "vAttach", "attach", 0);
14367
14368 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
14369 "vRun", "run", 0);
14370
a6f3e723
SL
14371 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
14372 "QStartNoAckMode", "noack", 0);
14373
82f73884
PA
14374 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
14375 "vKill", "kill", 0);
14376
0b16c5cf
PA
14377 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
14378 "qAttached", "query-attached", 0);
14379
782b2b07 14380 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
14381 "ConditionalTracepoints",
14382 "conditional-tracepoints", 0);
3788aec7
LM
14383
14384 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
14385 "ConditionalBreakpoints",
14386 "conditional-breakpoints", 0);
14387
d3ce09f5
SS
14388 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
14389 "BreakpointCommands",
14390 "breakpoint-commands", 0);
14391
7a697b8d
SS
14392 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
14393 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 14394
409873ef
SS
14395 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
14396 "TracepointSource", "TracepointSource", 0);
14397
d914c394
SS
14398 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
14399 "QAllow", "allow", 0);
14400
0fb4aa4b
PA
14401 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
14402 "StaticTracepoints", "static-tracepoints", 0);
14403
1e4d1764
YQ
14404 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
14405 "InstallInTrace", "install-in-trace", 0);
14406
0fb4aa4b
PA
14407 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
14408 "qXfer:statictrace:read", "read-sdata-object", 0);
14409
78d85199
YQ
14410 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
14411 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
14412
03583c20
UW
14413 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
14414 "QDisableRandomization", "disable-randomization", 0);
14415
d1feda86
YQ
14416 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
14417 "QAgent", "agent", 0);
14418
f6f899bf
HAQ
14419 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
14420 "QTBuffer:size", "trace-buffer-size", 0);
14421
9accd112
MM
14422 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
14423 "Qbtrace:off", "disable-btrace", 0);
14424
14425 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
14426 "Qbtrace:bts", "enable-btrace-bts", 0);
14427
14428 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
14429 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
14430
14431 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
14432 "qXfer:btrace", "read-btrace", 0);
14433
f4abbc16
MM
14434 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
14435 "qXfer:btrace-conf", "read-btrace-conf", 0);
14436
d33501a5
MM
14437 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
14438 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
14439
73b8c1fd
PA
14440 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
14441 "multiprocess-feature", "multiprocess-feature", 0);
14442
f7e6eed5
PA
14443 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
14444 "swbreak-feature", "swbreak-feature", 0);
14445
14446 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
14447 "hwbreak-feature", "hwbreak-feature", 0);
14448
89245bc0
DB
14449 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
14450 "fork-event-feature", "fork-event-feature", 0);
14451
14452 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
14453 "vfork-event-feature", "vfork-event-feature", 0);
14454
b20a6524
MM
14455 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
14456 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
14457
750ce8d1
YQ
14458 add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
14459 "vContSupported", "verbose-resume-supported", 0);
14460
94585166
DB
14461 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
14462 "exec-event-feature", "exec-event-feature", 0);
14463
de979965
PA
14464 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
14465 "vCtrlC", "ctrl-c", 0);
14466
65706a29
PA
14467 add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
14468 "QThreadEvents", "thread-events", 0);
14469
f2faf941
PA
14470 add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
14471 "N stop reply", "no-resumed-stop-reply", 0);
14472
0b736949
DB
14473 /* Assert that we've registered "set remote foo-packet" commands
14474 for all packet configs. */
ca4f7f8b
PA
14475 {
14476 int i;
14477
14478 for (i = 0; i < PACKET_MAX; i++)
14479 {
14480 /* Ideally all configs would have a command associated. Some
14481 still don't though. */
14482 int excepted;
14483
14484 switch (i)
14485 {
14486 case PACKET_QNonStop:
ca4f7f8b
PA
14487 case PACKET_EnableDisableTracepoints_feature:
14488 case PACKET_tracenz_feature:
14489 case PACKET_DisconnectedTracing_feature:
14490 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
14491 case PACKET_qCRC:
14492 /* Additions to this list need to be well justified:
14493 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
14494 excepted = 1;
14495 break;
14496 default:
14497 excepted = 0;
14498 break;
14499 }
14500
14501 /* This catches both forgetting to add a config command, and
14502 forgetting to remove a packet from the exception list. */
14503 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
14504 }
14505 }
14506
37a105a1
DJ
14507 /* Keep the old ``set remote Z-packet ...'' working. Each individual
14508 Z sub-packet has its own set and show commands, but users may
14509 have sets to this variable in their .gdbinit files (or in their
14510 documentation). */
e9e68a56 14511 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
14512 &remote_Z_packet_detect, _("\
14513Set use of remote protocol `Z' packets"), _("\
14514Show use of remote protocol `Z' packets "), _("\
3b64bf98 14515When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 14516packets."),
e9e68a56 14517 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
14518 show_remote_protocol_Z_packet_cmd,
14519 /* FIXME: i18n: Use of remote protocol
14520 `Z' packets is %s. */
e9e68a56 14521 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 14522
a6b151f1
DJ
14523 add_prefix_cmd ("remote", class_files, remote_command, _("\
14524Manipulate files on the remote system\n\
14525Transfer files to and from the remote target system."),
14526 &remote_cmdlist, "remote ",
14527 0 /* allow-unknown */, &cmdlist);
14528
14529 add_cmd ("put", class_files, remote_put_command,
14530 _("Copy a local file to the remote system."),
14531 &remote_cmdlist);
14532
14533 add_cmd ("get", class_files, remote_get_command,
14534 _("Copy a remote file to the local system."),
14535 &remote_cmdlist);
14536
14537 add_cmd ("delete", class_files, remote_delete_command,
14538 _("Delete a remote file."),
14539 &remote_cmdlist);
14540
2d717e4f 14541 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 14542 &remote_exec_file_var, _("\
2d717e4f 14543Set the remote pathname for \"run\""), _("\
94585166
DB
14544Show the remote pathname for \"run\""), NULL,
14545 set_remote_exec_file,
14546 show_remote_exec_file,
14547 &remote_set_cmdlist,
14548 &remote_show_cmdlist);
2d717e4f 14549
c1e36e3e
PA
14550 add_setshow_boolean_cmd ("range-stepping", class_run,
14551 &use_range_stepping, _("\
14552Enable or disable range stepping."), _("\
14553Show whether target-assisted range stepping is enabled."), _("\
14554If on, and the target supports it, when stepping a source line, GDB\n\
14555tells the target to step the corresponding range of addresses itself instead\n\
14556of issuing multiple single-steps. This speeds up source level\n\
14557stepping. If off, GDB always issues single-steps, even if range\n\
14558stepping is supported by the target. The default is on."),
14559 set_range_stepping,
14560 show_range_stepping,
14561 &setlist,
14562 &showlist);
14563
449092f6
CV
14564 /* Eventually initialize fileio. See fileio.c */
14565 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 14566
ba348170 14567 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 14568 special ptids with it set to != 0. */
ba348170
PA
14569 magic_null_ptid = ptid_build (42000, -1, 1);
14570 not_sent_ptid = ptid_build (42000, -2, 1);
14571 any_thread_ptid = ptid_build (42000, 0, 1);
c906108c 14572}
This page took 4.529518 seconds and 4 git commands to generate.