remote: Make vcont_builder a class
[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
cc0be08f
PA
1331/* The default max memory-write-packet-size, when the setting is
1332 "fixed". The 16k is historical. (It came from older GDB's using
1333 alloca for buffers and the knowledge (folklore?) that some hosts
1334 don't cope very well with large alloca calls.) */
1335#define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
a5c0808e
PA
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
cc0be08f
PA
1341/* Get the memory packet size, assuming it is fixed. */
1342
1343static long
1344get_fixed_memory_packet_size (struct memory_packet_config *config)
1345{
1346 gdb_assert (config->fixed_p);
1347
1348 if (config->size <= 0)
1349 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED;
1350 else
1351 return config->size;
1352}
1353
11cf8741
JM
1354/* Compute the current size of a read/write packet. Since this makes
1355 use of ``actual_register_packet_size'' the computation is dynamic. */
1356
1357static long
1358get_memory_packet_size (struct memory_packet_config *config)
1359{
d01949b6 1360 struct remote_state *rs = get_remote_state ();
9d6eea31 1361 remote_arch_state *rsa = rs->get_remote_arch_state (target_gdbarch ());
ea9c271d 1362
11cf8741
JM
1363 long what_they_get;
1364 if (config->fixed_p)
cc0be08f 1365 what_they_get = get_fixed_memory_packet_size (config);
11cf8741
JM
1366 else
1367 {
ea9c271d 1368 what_they_get = get_remote_packet_size ();
23860348 1369 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1370 if (config->size > 0
1371 && what_they_get > config->size)
1372 what_they_get = config->size;
be2a5f71
DJ
1373
1374 /* Limit it to the size of the targets ``g'' response unless we have
1375 permission from the stub to use a larger packet size. */
1376 if (rs->explicit_packet_size == 0
1377 && rsa->actual_register_packet_size > 0
1378 && what_they_get > rsa->actual_register_packet_size)
1379 what_they_get = rsa->actual_register_packet_size;
11cf8741 1380 }
a5c0808e
PA
1381 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1382 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1383
1384 /* Make sure there is room in the global buffer for this packet
1385 (including its trailing NUL byte). */
1386 if (rs->buf_size < what_they_get + 1)
1387 {
1388 rs->buf_size = 2 * what_they_get;
224c3ddb 1389 rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get);
6d820c5c
DJ
1390 }
1391
11cf8741
JM
1392 return what_they_get;
1393}
1394
0df8b418 1395/* Update the size of a read/write packet. If they user wants
23860348 1396 something really big then do a sanity check. */
11cf8741
JM
1397
1398static void
ac88e2de 1399set_memory_packet_size (const char *args, struct memory_packet_config *config)
11cf8741
JM
1400{
1401 int fixed_p = config->fixed_p;
1402 long size = config->size;
a744cf53 1403
11cf8741 1404 if (args == NULL)
8a3fe4f8 1405 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1406 else if (strcmp (args, "hard") == 0
1407 || strcmp (args, "fixed") == 0)
1408 fixed_p = 1;
1409 else if (strcmp (args, "soft") == 0
1410 || strcmp (args, "limit") == 0)
1411 fixed_p = 0;
1412 else
1413 {
1414 char *end;
a744cf53 1415
11cf8741
JM
1416 size = strtoul (args, &end, 0);
1417 if (args == end)
8a3fe4f8 1418 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1419
1420 /* Instead of explicitly capping the size of a packet to or
1421 disallowing it, the user is allowed to set the size to
1422 something arbitrarily large. */
11cf8741 1423 }
a5c0808e 1424
23860348 1425 /* Extra checks? */
11cf8741
JM
1426 if (fixed_p && !config->fixed_p)
1427 {
cc0be08f
PA
1428 /* So that the query shows the correct value. */
1429 long query_size = (size <= 0
1430 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
1431 : size);
1432
e2e0b3e5
AC
1433 if (! query (_("The target may not be able to correctly handle a %s\n"
1434 "of %ld bytes. Change the packet size? "),
cc0be08f 1435 config->name, query_size))
8a3fe4f8 1436 error (_("Packet size not changed."));
11cf8741 1437 }
23860348 1438 /* Update the config. */
11cf8741
JM
1439 config->fixed_p = fixed_p;
1440 config->size = size;
1441}
1442
1443static void
1444show_memory_packet_size (struct memory_packet_config *config)
1445{
cc0be08f
PA
1446 if (config->size == 0)
1447 printf_filtered (_("The %s is 0 (default). "), config->name);
1448 else
1449 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1450 if (config->fixed_p)
a3f17187 1451 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
cc0be08f 1452 get_fixed_memory_packet_size (config));
11cf8741 1453 else
cc0be08f
PA
1454 {
1455 struct remote_state *rs = get_remote_state ();
1456
1457 if (rs->remote_desc != NULL)
1458 printf_filtered (_("Packets are limited to %ld bytes.\n"),
1459 get_memory_packet_size (config));
1460 else
1461 puts_filtered ("The actual limit will be further reduced "
1462 "dependent on the target.\n");
1463 }
11cf8741
JM
1464}
1465
1466static struct memory_packet_config memory_write_packet_config =
1467{
1468 "memory-write-packet-size",
1469};
1470
1471static void
ac88e2de 1472set_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1473{
1474 set_memory_packet_size (args, &memory_write_packet_config);
1475}
1476
1477static void
ac88e2de 1478show_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1479{
1480 show_memory_packet_size (&memory_write_packet_config);
1481}
1482
1483static long
1484get_memory_write_packet_size (void)
1485{
1486 return get_memory_packet_size (&memory_write_packet_config);
1487}
1488
1489static struct memory_packet_config memory_read_packet_config =
1490{
1491 "memory-read-packet-size",
1492};
1493
1494static void
ac88e2de 1495set_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1496{
1497 set_memory_packet_size (args, &memory_read_packet_config);
1498}
1499
1500static void
ac88e2de 1501show_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1502{
1503 show_memory_packet_size (&memory_read_packet_config);
1504}
1505
1506static long
1507get_memory_read_packet_size (void)
1508{
1509 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1510
11cf8741
JM
1511 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1512 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1513 increased beyond this. */
1514 if (size > get_remote_packet_size ())
1515 size = get_remote_packet_size ();
11cf8741
JM
1516 return size;
1517}
1518
11cf8741 1519\f
5a2468f5 1520/* Generic configuration support for packets the stub optionally
0df8b418 1521 supports. Allows the user to specify the use of the packet as well
23860348 1522 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1523
1524enum packet_support
1525 {
1526 PACKET_SUPPORT_UNKNOWN = 0,
1527 PACKET_ENABLE,
1528 PACKET_DISABLE
1529 };
1530
5a2468f5
JM
1531struct packet_config
1532 {
bb572ddd
DJ
1533 const char *name;
1534 const char *title;
4082afcc
PA
1535
1536 /* If auto, GDB auto-detects support for this packet or feature,
1537 either through qSupported, or by trying the packet and looking
1538 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1539 packet. If false, the packet is disabled. Configs that don't
1540 have an associated command always have this set to auto. */
7f19b9a2 1541 enum auto_boolean detect;
4082afcc
PA
1542
1543 /* Does the target support this packet? */
5a2468f5
JM
1544 enum packet_support support;
1545 };
1546
d471ea57 1547/* Analyze a packet's return value and update the packet config
23860348 1548 accordingly. */
d471ea57
AC
1549
1550enum packet_result
1551{
1552 PACKET_ERROR,
1553 PACKET_OK,
1554 PACKET_UNKNOWN
1555};
1556
4082afcc
PA
1557static enum packet_support packet_config_support (struct packet_config *config);
1558static enum packet_support packet_support (int packet);
5a2468f5
JM
1559
1560static void
fba45db2 1561show_packet_config_cmd (struct packet_config *config)
5a2468f5 1562{
a121b7c1 1563 const char *support = "internal-error";
a744cf53 1564
4082afcc 1565 switch (packet_config_support (config))
5a2468f5
JM
1566 {
1567 case PACKET_ENABLE:
1568 support = "enabled";
1569 break;
1570 case PACKET_DISABLE:
1571 support = "disabled";
1572 break;
1573 case PACKET_SUPPORT_UNKNOWN:
1574 support = "unknown";
1575 break;
1576 }
1577 switch (config->detect)
1578 {
7f19b9a2 1579 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1580 printf_filtered (_("Support for the `%s' packet "
1581 "is auto-detected, currently %s.\n"),
37a105a1 1582 config->name, support);
5a2468f5 1583 break;
7f19b9a2
AC
1584 case AUTO_BOOLEAN_TRUE:
1585 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1586 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1587 config->name, support);
8e248173 1588 break;
5a2468f5
JM
1589 }
1590}
1591
1592static void
bb572ddd
DJ
1593add_packet_config_cmd (struct packet_config *config, const char *name,
1594 const char *title, int legacy)
d471ea57 1595{
5a2468f5
JM
1596 char *set_doc;
1597 char *show_doc;
d471ea57 1598 char *cmd_name;
3ed07be4 1599
5a2468f5
JM
1600 config->name = name;
1601 config->title = title;
b435e160
AC
1602 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1603 name, title);
3e43a32a
MS
1604 show_doc = xstrprintf ("Show current use of remote "
1605 "protocol `%s' (%s) packet",
b435e160 1606 name, title);
d471ea57 1607 /* set/show TITLE-packet {auto,on,off} */
b435e160 1608 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1609 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1610 &config->detect, set_doc,
1611 show_doc, NULL, /* help_doc */
4082afcc 1612 NULL,
bb572ddd
DJ
1613 show_remote_protocol_packet_cmd,
1614 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1615 /* The command code copies the documentation strings. */
1616 xfree (set_doc);
1617 xfree (show_doc);
23860348 1618 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1619 if (legacy)
1620 {
1621 char *legacy_name;
a744cf53 1622
b435e160 1623 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1624 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1625 &remote_set_cmdlist);
d471ea57 1626 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1627 &remote_show_cmdlist);
d471ea57 1628 }
5a2468f5
JM
1629}
1630
d471ea57 1631static enum packet_result
a76d924d 1632packet_check_result (const char *buf)
5a2468f5 1633{
d471ea57 1634 if (buf[0] != '\0')
5a2468f5 1635 {
d471ea57 1636 /* The stub recognized the packet request. Check that the
23860348 1637 operation succeeded. */
a76d924d
DJ
1638 if (buf[0] == 'E'
1639 && isxdigit (buf[1]) && isxdigit (buf[2])
1640 && buf[3] == '\0')
1641 /* "Enn" - definitly an error. */
1642 return PACKET_ERROR;
1643
1644 /* Always treat "E." as an error. This will be used for
1645 more verbose error messages, such as E.memtypes. */
1646 if (buf[0] == 'E' && buf[1] == '.')
1647 return PACKET_ERROR;
1648
1649 /* The packet may or may not be OK. Just assume it is. */
1650 return PACKET_OK;
1651 }
1652 else
1653 /* The stub does not support the packet. */
1654 return PACKET_UNKNOWN;
1655}
1656
1657static enum packet_result
1658packet_ok (const char *buf, struct packet_config *config)
1659{
1660 enum packet_result result;
1661
4082afcc
PA
1662 if (config->detect != AUTO_BOOLEAN_TRUE
1663 && config->support == PACKET_DISABLE)
1664 internal_error (__FILE__, __LINE__,
1665 _("packet_ok: attempt to use a disabled packet"));
1666
a76d924d
DJ
1667 result = packet_check_result (buf);
1668 switch (result)
1669 {
1670 case PACKET_OK:
1671 case PACKET_ERROR:
1672 /* The stub recognized the packet request. */
4082afcc 1673 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1674 {
d471ea57
AC
1675 if (remote_debug)
1676 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1677 "Packet %s (%s) is supported\n",
1678 config->name, config->title);
d471ea57 1679 config->support = PACKET_ENABLE;
d471ea57 1680 }
a76d924d
DJ
1681 break;
1682 case PACKET_UNKNOWN:
23860348 1683 /* The stub does not support the packet. */
4082afcc
PA
1684 if (config->detect == AUTO_BOOLEAN_AUTO
1685 && config->support == PACKET_ENABLE)
d471ea57 1686 {
4082afcc
PA
1687 /* If the stub previously indicated that the packet was
1688 supported then there is a protocol error. */
1689 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1690 config->name, config->title);
1691 }
1692 else if (config->detect == AUTO_BOOLEAN_TRUE)
1693 {
1694 /* The user set it wrong. */
1695 error (_("Enabled packet %s (%s) not recognized by stub"),
1696 config->name, config->title);
d471ea57 1697 }
4082afcc
PA
1698
1699 if (remote_debug)
1700 fprintf_unfiltered (gdb_stdlog,
1701 "Packet %s (%s) is NOT supported\n",
1702 config->name, config->title);
1703 config->support = PACKET_DISABLE;
a76d924d 1704 break;
5a2468f5 1705 }
a76d924d
DJ
1706
1707 return result;
5a2468f5
JM
1708}
1709
444abaca
DJ
1710enum {
1711 PACKET_vCont = 0,
1712 PACKET_X,
1713 PACKET_qSymbol,
1714 PACKET_P,
1715 PACKET_p,
1716 PACKET_Z0,
1717 PACKET_Z1,
1718 PACKET_Z2,
1719 PACKET_Z3,
1720 PACKET_Z4,
15a201c8 1721 PACKET_vFile_setfs,
a6b151f1
DJ
1722 PACKET_vFile_open,
1723 PACKET_vFile_pread,
1724 PACKET_vFile_pwrite,
1725 PACKET_vFile_close,
1726 PACKET_vFile_unlink,
b9e7b9c3 1727 PACKET_vFile_readlink,
0a93529c 1728 PACKET_vFile_fstat,
0876f84a 1729 PACKET_qXfer_auxv,
23181151 1730 PACKET_qXfer_features,
c78fa86a 1731 PACKET_qXfer_exec_file,
cfa9d6d9 1732 PACKET_qXfer_libraries,
2268b414 1733 PACKET_qXfer_libraries_svr4,
fd79ecee 1734 PACKET_qXfer_memory_map,
0e7f50da
UW
1735 PACKET_qXfer_spu_read,
1736 PACKET_qXfer_spu_write,
07e059b5 1737 PACKET_qXfer_osdata,
dc146f7c 1738 PACKET_qXfer_threads,
0fb4aa4b 1739 PACKET_qXfer_statictrace_read,
b3b9301e 1740 PACKET_qXfer_traceframe_info,
169081d0 1741 PACKET_qXfer_uib,
711e434b 1742 PACKET_qGetTIBAddr,
444abaca 1743 PACKET_qGetTLSAddr,
be2a5f71 1744 PACKET_qSupported,
bd3eecc3 1745 PACKET_qTStatus,
89be2091 1746 PACKET_QPassSignals,
82075af2 1747 PACKET_QCatchSyscalls,
9b224c5e 1748 PACKET_QProgramSignals,
bc3b087d 1749 PACKET_QSetWorkingDir,
aefd8b33 1750 PACKET_QStartupWithShell,
0a2dde4a
SDJ
1751 PACKET_QEnvironmentHexEncoded,
1752 PACKET_QEnvironmentReset,
1753 PACKET_QEnvironmentUnset,
936d2992 1754 PACKET_qCRC,
08388c79 1755 PACKET_qSearch_memory,
2d717e4f
DJ
1756 PACKET_vAttach,
1757 PACKET_vRun,
a6f3e723 1758 PACKET_QStartNoAckMode,
82f73884 1759 PACKET_vKill,
4aa995e1
PA
1760 PACKET_qXfer_siginfo_read,
1761 PACKET_qXfer_siginfo_write,
0b16c5cf 1762 PACKET_qAttached,
4082afcc
PA
1763
1764 /* Support for conditional tracepoints. */
782b2b07 1765 PACKET_ConditionalTracepoints,
4082afcc
PA
1766
1767 /* Support for target-side breakpoint conditions. */
3788aec7 1768 PACKET_ConditionalBreakpoints,
4082afcc
PA
1769
1770 /* Support for target-side breakpoint commands. */
d3ce09f5 1771 PACKET_BreakpointCommands,
4082afcc
PA
1772
1773 /* Support for fast tracepoints. */
7a697b8d 1774 PACKET_FastTracepoints,
4082afcc
PA
1775
1776 /* Support for static tracepoints. */
0fb4aa4b 1777 PACKET_StaticTracepoints,
4082afcc
PA
1778
1779 /* Support for installing tracepoints while a trace experiment is
1780 running. */
1e4d1764 1781 PACKET_InstallInTrace,
4082afcc 1782
40ab02ce
MS
1783 PACKET_bc,
1784 PACKET_bs,
409873ef 1785 PACKET_TracepointSource,
d914c394 1786 PACKET_QAllow,
78d85199 1787 PACKET_qXfer_fdpic,
03583c20 1788 PACKET_QDisableRandomization,
d1feda86 1789 PACKET_QAgent,
f6f899bf 1790 PACKET_QTBuffer_size,
9accd112
MM
1791 PACKET_Qbtrace_off,
1792 PACKET_Qbtrace_bts,
b20a6524 1793 PACKET_Qbtrace_pt,
9accd112 1794 PACKET_qXfer_btrace,
4082afcc
PA
1795
1796 /* Support for the QNonStop packet. */
1797 PACKET_QNonStop,
1798
65706a29
PA
1799 /* Support for the QThreadEvents packet. */
1800 PACKET_QThreadEvents,
1801
4082afcc
PA
1802 /* Support for multi-process extensions. */
1803 PACKET_multiprocess_feature,
1804
1805 /* Support for enabling and disabling tracepoints while a trace
1806 experiment is running. */
1807 PACKET_EnableDisableTracepoints_feature,
1808
1809 /* Support for collecting strings using the tracenz bytecode. */
1810 PACKET_tracenz_feature,
1811
1812 /* Support for continuing to run a trace experiment while GDB is
1813 disconnected. */
1814 PACKET_DisconnectedTracing_feature,
1815
1816 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1817 PACKET_augmented_libraries_svr4_read_feature,
1818
f4abbc16
MM
1819 /* Support for the qXfer:btrace-conf:read packet. */
1820 PACKET_qXfer_btrace_conf,
1821
d33501a5
MM
1822 /* Support for the Qbtrace-conf:bts:size packet. */
1823 PACKET_Qbtrace_conf_bts_size,
1824
f7e6eed5
PA
1825 /* Support for swbreak+ feature. */
1826 PACKET_swbreak_feature,
1827
1828 /* Support for hwbreak+ feature. */
1829 PACKET_hwbreak_feature,
1830
89245bc0
DB
1831 /* Support for fork events. */
1832 PACKET_fork_event_feature,
1833
1834 /* Support for vfork events. */
1835 PACKET_vfork_event_feature,
1836
b20a6524
MM
1837 /* Support for the Qbtrace-conf:pt:size packet. */
1838 PACKET_Qbtrace_conf_pt_size,
1839
94585166
DB
1840 /* Support for exec events. */
1841 PACKET_exec_event_feature,
1842
750ce8d1
YQ
1843 /* Support for query supported vCont actions. */
1844 PACKET_vContSupported,
1845
de979965
PA
1846 /* Support remote CTRL-C. */
1847 PACKET_vCtrlC,
1848
f2faf941
PA
1849 /* Support TARGET_WAITKIND_NO_RESUMED. */
1850 PACKET_no_resumed,
1851
444abaca
DJ
1852 PACKET_MAX
1853};
506fb367 1854
444abaca 1855static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1856
f7e6eed5
PA
1857/* Returns the packet's corresponding "set remote foo-packet" command
1858 state. See struct packet_config for more details. */
1859
1860static enum auto_boolean
1861packet_set_cmd_state (int packet)
1862{
1863 return remote_protocol_packets[packet].detect;
1864}
1865
4082afcc
PA
1866/* Returns whether a given packet or feature is supported. This takes
1867 into account the state of the corresponding "set remote foo-packet"
1868 command, which may be used to bypass auto-detection. */
dc8acb97 1869
4082afcc
PA
1870static enum packet_support
1871packet_config_support (struct packet_config *config)
1872{
1873 switch (config->detect)
444abaca 1874 {
4082afcc
PA
1875 case AUTO_BOOLEAN_TRUE:
1876 return PACKET_ENABLE;
1877 case AUTO_BOOLEAN_FALSE:
1878 return PACKET_DISABLE;
1879 case AUTO_BOOLEAN_AUTO:
1880 return config->support;
1881 default:
1882 gdb_assert_not_reached (_("bad switch"));
444abaca 1883 }
4082afcc
PA
1884}
1885
1886/* Same as packet_config_support, but takes the packet's enum value as
1887 argument. */
1888
1889static enum packet_support
1890packet_support (int packet)
1891{
1892 struct packet_config *config = &remote_protocol_packets[packet];
1893
1894 return packet_config_support (config);
dc8acb97
MS
1895}
1896
5a2468f5 1897static void
444abaca
DJ
1898show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1899 struct cmd_list_element *c,
1900 const char *value)
5a2468f5 1901{
444abaca 1902 struct packet_config *packet;
5a2468f5 1903
444abaca
DJ
1904 for (packet = remote_protocol_packets;
1905 packet < &remote_protocol_packets[PACKET_MAX];
1906 packet++)
1907 {
1908 if (&packet->detect == c->var)
1909 {
1910 show_packet_config_cmd (packet);
1911 return;
1912 }
1913 }
9b20d036 1914 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1915 c->name);
5a2468f5
JM
1916}
1917
d471ea57
AC
1918/* Should we try one of the 'Z' requests? */
1919
1920enum Z_packet_type
1921{
1922 Z_PACKET_SOFTWARE_BP,
1923 Z_PACKET_HARDWARE_BP,
1924 Z_PACKET_WRITE_WP,
1925 Z_PACKET_READ_WP,
1926 Z_PACKET_ACCESS_WP,
1927 NR_Z_PACKET_TYPES
1928};
96baa820 1929
d471ea57 1930/* For compatibility with older distributions. Provide a ``set remote
23860348 1931 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1932
7f19b9a2 1933static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1934
1935static void
eb4c3f4a 1936set_remote_protocol_Z_packet_cmd (const char *args, int from_tty,
fba45db2 1937 struct cmd_list_element *c)
96baa820 1938{
d471ea57 1939 int i;
a744cf53 1940
d471ea57 1941 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1942 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1943}
1944
1945static void
08546159
AC
1946show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1947 struct cmd_list_element *c,
1948 const char *value)
96baa820 1949{
d471ea57 1950 int i;
a744cf53 1951
d471ea57
AC
1952 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1953 {
444abaca 1954 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1955 }
96baa820
JM
1956}
1957
4082afcc
PA
1958/* Returns true if the multi-process extensions are in effect. */
1959
1960static int
1961remote_multi_process_p (struct remote_state *rs)
1962{
1963 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1964}
1965
de0d863e
DB
1966/* Returns true if fork events are supported. */
1967
1968static int
1969remote_fork_event_p (struct remote_state *rs)
1970{
1971 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1972}
1973
c269dbdb
DB
1974/* Returns true if vfork events are supported. */
1975
1976static int
1977remote_vfork_event_p (struct remote_state *rs)
1978{
1979 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1980}
1981
d46addbb
DB
1982/* Returns true if exec events are supported. */
1983
1984static int
1985remote_exec_event_p (struct remote_state *rs)
1986{
1987 return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
1988}
1989
cbb8991c
DB
1990/* Insert fork catchpoint target routine. If fork events are enabled
1991 then return success, nothing more to do. */
1992
f6ac5f3d
PA
1993int
1994remote_target::insert_fork_catchpoint (int pid)
cbb8991c
DB
1995{
1996 struct remote_state *rs = get_remote_state ();
1997
1998 return !remote_fork_event_p (rs);
1999}
2000
2001/* Remove fork catchpoint target routine. Nothing to do, just
2002 return success. */
2003
f6ac5f3d
PA
2004int
2005remote_target::remove_fork_catchpoint (int pid)
cbb8991c
DB
2006{
2007 return 0;
2008}
2009
2010/* Insert vfork catchpoint target routine. If vfork events are enabled
2011 then return success, nothing more to do. */
2012
f6ac5f3d
PA
2013int
2014remote_target::insert_vfork_catchpoint (int pid)
cbb8991c
DB
2015{
2016 struct remote_state *rs = get_remote_state ();
2017
2018 return !remote_vfork_event_p (rs);
2019}
2020
2021/* Remove vfork catchpoint target routine. Nothing to do, just
2022 return success. */
2023
f6ac5f3d
PA
2024int
2025remote_target::remove_vfork_catchpoint (int pid)
cbb8991c
DB
2026{
2027 return 0;
2028}
2029
d46addbb
DB
2030/* Insert exec catchpoint target routine. If exec events are
2031 enabled, just return success. */
2032
f6ac5f3d
PA
2033int
2034remote_target::insert_exec_catchpoint (int pid)
d46addbb
DB
2035{
2036 struct remote_state *rs = get_remote_state ();
2037
2038 return !remote_exec_event_p (rs);
2039}
2040
2041/* Remove exec catchpoint target routine. Nothing to do, just
2042 return success. */
2043
f6ac5f3d
PA
2044int
2045remote_target::remove_exec_catchpoint (int pid)
d46addbb
DB
2046{
2047 return 0;
2048}
2049
74531fed
PA
2050\f
2051/* Asynchronous signal handle registered as event loop source for
2052 when we have pending events ready to be passed to the core. */
2053
2054static struct async_event_handler *remote_async_inferior_event_token;
2055
c906108c
SS
2056\f
2057
79d7f229
PA
2058static ptid_t magic_null_ptid;
2059static ptid_t not_sent_ptid;
2060static ptid_t any_thread_ptid;
2061
0b16c5cf
PA
2062/* Find out if the stub attached to PID (and hence GDB should offer to
2063 detach instead of killing it when bailing out). */
2064
2065static int
2066remote_query_attached (int pid)
2067{
2068 struct remote_state *rs = get_remote_state ();
bba74b36 2069 size_t size = get_remote_packet_size ();
0b16c5cf 2070
4082afcc 2071 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
2072 return 0;
2073
2074 if (remote_multi_process_p (rs))
bba74b36 2075 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 2076 else
bba74b36 2077 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
2078
2079 putpkt (rs->buf);
2080 getpkt (&rs->buf, &rs->buf_size, 0);
2081
2082 switch (packet_ok (rs->buf,
1554e9be 2083 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
2084 {
2085 case PACKET_OK:
2086 if (strcmp (rs->buf, "1") == 0)
2087 return 1;
2088 break;
2089 case PACKET_ERROR:
2090 warning (_("Remote failure reply: %s"), rs->buf);
2091 break;
2092 case PACKET_UNKNOWN:
2093 break;
2094 }
2095
2096 return 0;
2097}
2098
49c62f2e
PA
2099/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2100 has been invented by GDB, instead of reported by the target. Since
2101 we can be connected to a remote system before before knowing about
2102 any inferior, mark the target with execution when we find the first
2103 inferior. If ATTACHED is 1, then we had just attached to this
2104 inferior. If it is 0, then we just created this inferior. If it
2105 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
2106 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2107 attempt to open this inferior's executable as the main executable
2108 if no main executable is open already. */
1941c569
PA
2109
2110static struct inferior *
1b6e6f5c
GB
2111remote_add_inferior (int fake_pid_p, int pid, int attached,
2112 int try_open_exec)
1941c569 2113{
1941c569
PA
2114 struct inferior *inf;
2115
0b16c5cf
PA
2116 /* Check whether this process we're learning about is to be
2117 considered attached, or if is to be considered to have been
2118 spawned by the stub. */
2119 if (attached == -1)
2120 attached = remote_query_attached (pid);
2121
f5656ead 2122 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
2123 {
2124 /* If the target shares code across all inferiors, then every
2125 attach adds a new inferior. */
2126 inf = add_inferior (pid);
2127
2128 /* ... and every inferior is bound to the same program space.
2129 However, each inferior may still have its own address
2130 space. */
2131 inf->aspace = maybe_new_address_space ();
2132 inf->pspace = current_program_space;
2133 }
2134 else
2135 {
2136 /* In the traditional debugging scenario, there's a 1-1 match
2137 between program/address spaces. We simply bind the inferior
2138 to the program space's address space. */
2139 inf = current_inferior ();
2140 inferior_appeared (inf, pid);
2141 }
1941c569 2142
0b16c5cf 2143 inf->attach_flag = attached;
49c62f2e 2144 inf->fake_pid_p = fake_pid_p;
0b16c5cf 2145
1b6e6f5c
GB
2146 /* If no main executable is currently open then attempt to
2147 open the file that was executed to create this inferior. */
835205d0 2148 if (try_open_exec && get_exec_file (0) == NULL)
bb805577 2149 exec_file_locate_attach (pid, 0, 1);
1b6e6f5c 2150
1941c569
PA
2151 return inf;
2152}
2153
7aabaf9d 2154static remote_thread_info *get_remote_thread_info (thread_info *thread);
85ad3aaf 2155
1941c569
PA
2156/* Add thread PTID to GDB's thread list. Tag it as executing/running
2157 according to RUNNING. */
2158
c906108c 2159static void
0d5b594f 2160remote_add_thread (ptid_t ptid, int running, int executing)
c906108c 2161{
b7ea362b 2162 struct remote_state *rs = get_remote_state ();
85ad3aaf 2163 struct thread_info *thread;
b7ea362b
PA
2164
2165 /* GDB historically didn't pull threads in the initial connection
2166 setup. If the remote target doesn't even have a concept of
2167 threads (e.g., a bare-metal target), even if internally we
2168 consider that a single-threaded target, mentioning a new thread
2169 might be confusing to the user. Be silent then, preserving the
2170 age old behavior. */
2171 if (rs->starting_up)
85ad3aaf 2172 thread = add_thread_silent (ptid);
b7ea362b 2173 else
85ad3aaf 2174 thread = add_thread (ptid);
1941c569 2175
7aabaf9d 2176 get_remote_thread_info (thread)->vcont_resumed = executing;
0d5b594f 2177 set_executing (ptid, executing);
1941c569
PA
2178 set_running (ptid, running);
2179}
2180
2181/* Come here when we learn about a thread id from the remote target.
2182 It may be the first time we hear about such thread, so take the
2183 opportunity to add it to GDB's thread list. In case this is the
2184 first time we're noticing its corresponding inferior, add it to
0d5b594f
PA
2185 GDB's inferior list as well. EXECUTING indicates whether the
2186 thread is (internally) executing or stopped. */
1941c569
PA
2187
2188static void
0d5b594f 2189remote_notice_new_inferior (ptid_t currthread, int executing)
1941c569 2190{
0d5b594f
PA
2191 /* In non-stop mode, we assume new found threads are (externally)
2192 running until proven otherwise with a stop reply. In all-stop,
2193 we can only get here if all threads are stopped. */
2194 int running = target_is_non_stop_p () ? 1 : 0;
2195
c906108c
SS
2196 /* If this is a new thread, add it to GDB's thread list.
2197 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
2198
2199 if (in_thread_list (currthread) && is_exited (currthread))
2200 {
2201 /* We're seeing an event on a thread id we knew had exited.
2202 This has to be a new thread reusing the old id. Add it. */
0d5b594f 2203 remote_add_thread (currthread, running, executing);
82f73884
PA
2204 return;
2205 }
2206
79d7f229 2207 if (!in_thread_list (currthread))
c0a2216e 2208 {
1941c569 2209 struct inferior *inf = NULL;
bad34192 2210 int pid = ptid_get_pid (currthread);
1941c569 2211
bad34192
PA
2212 if (ptid_is_pid (inferior_ptid)
2213 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
2214 {
2215 /* inferior_ptid has no thread member yet. This can happen
2216 with the vAttach -> remote_wait,"TAAthread:" path if the
2217 stub doesn't support qC. This is the first stop reported
2218 after an attach, so this is the main thread. Update the
2219 ptid in the thread list. */
bad34192
PA
2220 if (in_thread_list (pid_to_ptid (pid)))
2221 thread_change_ptid (inferior_ptid, currthread);
2222 else
2223 {
0d5b594f 2224 remote_add_thread (currthread, running, executing);
bad34192
PA
2225 inferior_ptid = currthread;
2226 }
dc146f7c 2227 return;
c0a2216e 2228 }
82f73884
PA
2229
2230 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
2231 {
2232 /* inferior_ptid is not set yet. This can happen with the
2233 vRun -> remote_wait,"TAAthread:" path if the stub
2234 doesn't support qC. This is the first stop reported
2235 after an attach, so this is the main thread. Update the
2236 ptid in the thread list. */
dc146f7c 2237 thread_change_ptid (inferior_ptid, currthread);
82f73884 2238 return;
c0a2216e 2239 }
82f73884 2240
29c87f7f
PA
2241 /* When connecting to a target remote, or to a target
2242 extended-remote which already was debugging an inferior, we
2243 may not know about it yet. Add it before adding its child
2244 thread, so notifications are emitted in a sensible order. */
2245 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
2246 {
2247 struct remote_state *rs = get_remote_state ();
2248 int fake_pid_p = !remote_multi_process_p (rs);
2249
2250 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 2251 ptid_get_pid (currthread), -1, 1);
49c62f2e 2252 }
29c87f7f 2253
82f73884 2254 /* This is really a new thread. Add it. */
0d5b594f 2255 remote_add_thread (currthread, running, executing);
1941c569
PA
2256
2257 /* If we found a new inferior, let the common code do whatever
2258 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
2259 breakpoints), unless we're just setting up an all-stop
2260 connection. */
1941c569 2261 if (inf != NULL)
b7ea362b
PA
2262 {
2263 struct remote_state *rs = get_remote_state ();
2264
6efcd9a8 2265 if (!rs->starting_up)
0d5b594f 2266 notice_new_inferior (currthread, executing, 0);
b7ea362b 2267 }
c0a2216e 2268 }
c906108c
SS
2269}
2270
85ad3aaf 2271/* Return THREAD's private thread data, creating it if necessary. */
dc146f7c 2272
7aabaf9d
SM
2273static remote_thread_info *
2274get_remote_thread_info (thread_info *thread)
dc146f7c 2275{
85ad3aaf 2276 gdb_assert (thread != NULL);
dc146f7c 2277
85ad3aaf 2278 if (thread->priv == NULL)
7aabaf9d 2279 thread->priv.reset (new remote_thread_info);
dc146f7c 2280
7aabaf9d 2281 return static_cast<remote_thread_info *> (thread->priv.get ());
85ad3aaf
PA
2282}
2283
2284/* Return PTID's private thread data, creating it if necessary. */
2285
7aabaf9d
SM
2286static remote_thread_info *
2287get_remote_thread_info (ptid_t ptid)
85ad3aaf
PA
2288{
2289 struct thread_info *info = find_thread_ptid (ptid);
2290
7aabaf9d 2291 return get_remote_thread_info (info);
dc146f7c
VP
2292}
2293
74531fed
PA
2294/* Call this function as a result of
2295 1) A halt indication (T packet) containing a thread id
2296 2) A direct query of currthread
0df8b418 2297 3) Successful execution of set thread */
74531fed
PA
2298
2299static void
47f8a51d 2300record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 2301{
47f8a51d 2302 rs->general_thread = currthread;
74531fed
PA
2303}
2304
89be2091
DJ
2305/* If 'QPassSignals' is supported, tell the remote stub what signals
2306 it can simply pass through to the inferior without reporting. */
2307
f6ac5f3d
PA
2308void
2309remote_target::pass_signals (int numsigs, unsigned char *pass_signals)
89be2091 2310{
4082afcc 2311 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
2312 {
2313 char *pass_packet, *p;
89be2091 2314 int count = 0, i;
747dc59d 2315 struct remote_state *rs = get_remote_state ();
89be2091
DJ
2316
2317 gdb_assert (numsigs < 256);
2318 for (i = 0; i < numsigs; i++)
2319 {
2455069d 2320 if (pass_signals[i])
89be2091
DJ
2321 count++;
2322 }
224c3ddb 2323 pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
89be2091
DJ
2324 strcpy (pass_packet, "QPassSignals:");
2325 p = pass_packet + strlen (pass_packet);
2326 for (i = 0; i < numsigs; i++)
2327 {
2455069d 2328 if (pass_signals[i])
89be2091
DJ
2329 {
2330 if (i >= 16)
2331 *p++ = tohex (i >> 4);
2332 *p++ = tohex (i & 15);
2333 if (count)
2334 *p++ = ';';
2335 else
2336 break;
2337 count--;
2338 }
2339 }
2340 *p = 0;
747dc59d 2341 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 2342 {
89be2091
DJ
2343 putpkt (pass_packet);
2344 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2345 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
2346 if (rs->last_pass_packet)
2347 xfree (rs->last_pass_packet);
2348 rs->last_pass_packet = pass_packet;
89be2091
DJ
2349 }
2350 else
2351 xfree (pass_packet);
2352 }
2353}
2354
82075af2
JS
2355/* If 'QCatchSyscalls' is supported, tell the remote stub
2356 to report syscalls to GDB. */
2357
f6ac5f3d
PA
2358int
2359remote_target::set_syscall_catchpoint (int pid, bool needed, int any_count,
2360 gdb::array_view<const int> syscall_counts)
82075af2 2361{
b80406ac 2362 const char *catch_packet;
82075af2
JS
2363 enum packet_result result;
2364 int n_sysno = 0;
2365
2366 if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
2367 {
2368 /* Not supported. */
2369 return 1;
2370 }
2371
649a140c 2372 if (needed && any_count == 0)
82075af2 2373 {
649a140c
PA
2374 /* Count how many syscalls are to be caught. */
2375 for (size_t i = 0; i < syscall_counts.size (); i++)
82075af2 2376 {
649a140c 2377 if (syscall_counts[i] != 0)
82075af2
JS
2378 n_sysno++;
2379 }
2380 }
2381
2382 if (remote_debug)
2383 {
2384 fprintf_unfiltered (gdb_stdlog,
2385 "remote_set_syscall_catchpoint "
2386 "pid %d needed %d any_count %d n_sysno %d\n",
2387 pid, needed, any_count, n_sysno);
2388 }
2389
1b81856f 2390 std::string built_packet;
82075af2
JS
2391 if (needed)
2392 {
2393 /* Prepare a packet with the sysno list, assuming max 8+1
2394 characters for a sysno. If the resulting packet size is too
2395 big, fallback on the non-selective packet. */
2396 const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
1b81856f
PA
2397 built_packet.reserve (maxpktsz);
2398 built_packet = "QCatchSyscalls:1";
649a140c 2399 if (any_count == 0)
82075af2 2400 {
649a140c
PA
2401 /* Add in each syscall to be caught. */
2402 for (size_t i = 0; i < syscall_counts.size (); i++)
82075af2 2403 {
649a140c
PA
2404 if (syscall_counts[i] != 0)
2405 string_appendf (built_packet, ";%zx", i);
82075af2
JS
2406 }
2407 }
1b81856f 2408 if (built_packet.size () > get_remote_packet_size ())
82075af2
JS
2409 {
2410 /* catch_packet too big. Fallback to less efficient
2411 non selective mode, with GDB doing the filtering. */
b80406ac 2412 catch_packet = "QCatchSyscalls:1";
82075af2 2413 }
b80406ac 2414 else
1b81856f 2415 catch_packet = built_packet.c_str ();
82075af2
JS
2416 }
2417 else
b80406ac 2418 catch_packet = "QCatchSyscalls:0";
82075af2 2419
b80406ac 2420 struct remote_state *rs = get_remote_state ();
82075af2 2421
b80406ac
TT
2422 putpkt (catch_packet);
2423 getpkt (&rs->buf, &rs->buf_size, 0);
2424 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
2425 if (result == PACKET_OK)
2426 return 0;
2427 else
2428 return -1;
82075af2
JS
2429}
2430
9b224c5e
PA
2431/* If 'QProgramSignals' is supported, tell the remote stub what
2432 signals it should pass through to the inferior when detaching. */
2433
f6ac5f3d
PA
2434void
2435remote_target::program_signals (int numsigs, unsigned char *signals)
9b224c5e 2436{
4082afcc 2437 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
2438 {
2439 char *packet, *p;
2440 int count = 0, i;
5e4a05c4 2441 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
2442
2443 gdb_assert (numsigs < 256);
2444 for (i = 0; i < numsigs; i++)
2445 {
2446 if (signals[i])
2447 count++;
2448 }
224c3ddb 2449 packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
9b224c5e
PA
2450 strcpy (packet, "QProgramSignals:");
2451 p = packet + strlen (packet);
2452 for (i = 0; i < numsigs; i++)
2453 {
2454 if (signal_pass_state (i))
2455 {
2456 if (i >= 16)
2457 *p++ = tohex (i >> 4);
2458 *p++ = tohex (i & 15);
2459 if (count)
2460 *p++ = ';';
2461 else
2462 break;
2463 count--;
2464 }
2465 }
2466 *p = 0;
5e4a05c4
TT
2467 if (!rs->last_program_signals_packet
2468 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 2469 {
9b224c5e
PA
2470 putpkt (packet);
2471 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2472 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
2473 xfree (rs->last_program_signals_packet);
2474 rs->last_program_signals_packet = packet;
9b224c5e
PA
2475 }
2476 else
2477 xfree (packet);
2478 }
2479}
2480
79d7f229
PA
2481/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2482 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2483 thread. If GEN is set, set the general thread, if not, then set
2484 the step/continue thread. */
c906108c 2485static void
d62a8ae2 2486set_thread (ptid_t ptid, int gen)
c906108c 2487{
d01949b6 2488 struct remote_state *rs = get_remote_state ();
47f8a51d 2489 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 2490 char *buf = rs->buf;
79d7f229 2491 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 2492
79d7f229 2493 if (ptid_equal (state, ptid))
c906108c
SS
2494 return;
2495
79d7f229
PA
2496 *buf++ = 'H';
2497 *buf++ = gen ? 'g' : 'c';
2498 if (ptid_equal (ptid, magic_null_ptid))
2499 xsnprintf (buf, endbuf - buf, "0");
2500 else if (ptid_equal (ptid, any_thread_ptid))
2501 xsnprintf (buf, endbuf - buf, "0");
2502 else if (ptid_equal (ptid, minus_one_ptid))
2503 xsnprintf (buf, endbuf - buf, "-1");
2504 else
82f73884 2505 write_ptid (buf, endbuf, ptid);
79d7f229 2506 putpkt (rs->buf);
6d820c5c 2507 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2508 if (gen)
47f8a51d 2509 rs->general_thread = ptid;
c906108c 2510 else
47f8a51d 2511 rs->continue_thread = ptid;
c906108c 2512}
79d7f229
PA
2513
2514static void
d62a8ae2 2515set_general_thread (ptid_t ptid)
79d7f229
PA
2516{
2517 set_thread (ptid, 1);
2518}
2519
2520static void
d62a8ae2 2521set_continue_thread (ptid_t ptid)
79d7f229
PA
2522{
2523 set_thread (ptid, 0);
2524}
2525
3c9c4b83
PA
2526/* Change the remote current process. Which thread within the process
2527 ends up selected isn't important, as long as it is the same process
2528 as what INFERIOR_PTID points to.
2529
2530 This comes from that fact that there is no explicit notion of
2531 "selected process" in the protocol. The selected process for
2532 general operations is the process the selected general thread
2533 belongs to. */
2534
2535static void
2536set_general_process (void)
2537{
2538 struct remote_state *rs = get_remote_state ();
2539
2540 /* If the remote can't handle multiple processes, don't bother. */
8020350c 2541 if (!remote_multi_process_p (rs))
3c9c4b83
PA
2542 return;
2543
2544 /* We only need to change the remote current thread if it's pointing
2545 at some other process. */
47f8a51d 2546 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2547 set_general_thread (inferior_ptid);
2548}
2549
c906108c 2550\f
7d1a114c
PA
2551/* Return nonzero if this is the main thread that we made up ourselves
2552 to model non-threaded targets as single-threaded. */
c906108c
SS
2553
2554static int
f6ac5f3d 2555remote_thread_always_alive (ptid_t ptid)
c906108c 2556{
c0a2216e
PA
2557 if (ptid_equal (ptid, magic_null_ptid))
2558 /* The main thread is always alive. */
2559 return 1;
2560
ba348170 2561 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2562 /* The main thread is always alive. This can happen after a
2563 vAttach, if the remote side doesn't support
2564 multi-threading. */
2565 return 1;
2566
7d1a114c
PA
2567 return 0;
2568}
2569
2570/* Return nonzero if the thread PTID is still alive on the remote
2571 system. */
2572
57810aa7 2573bool
f6ac5f3d 2574remote_target::thread_alive (ptid_t ptid)
7d1a114c
PA
2575{
2576 struct remote_state *rs = get_remote_state ();
2577 char *p, *endp;
2578
2579 /* Check if this is a thread that we made up ourselves to model
2580 non-threaded targets as single-threaded. */
f6ac5f3d 2581 if (remote_thread_always_alive (ptid))
7d1a114c
PA
2582 return 1;
2583
82f73884
PA
2584 p = rs->buf;
2585 endp = rs->buf + get_remote_packet_size ();
2586
2587 *p++ = 'T';
2588 write_ptid (p, endp, ptid);
2589
2e9f7625 2590 putpkt (rs->buf);
6d820c5c 2591 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2592 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2593}
2594
79efa585
SM
2595/* Return a pointer to a thread name if we know it and NULL otherwise.
2596 The thread_info object owns the memory for the name. */
2597
f6ac5f3d
PA
2598const char *
2599remote_target::thread_name (struct thread_info *info)
79efa585
SM
2600{
2601 if (info->priv != NULL)
a9334058
SM
2602 {
2603 const std::string &name = get_remote_thread_info (info)->name;
2604 return !name.empty () ? name.c_str () : NULL;
2605 }
79efa585
SM
2606
2607 return NULL;
2608}
2609
c906108c
SS
2610/* About these extended threadlist and threadinfo packets. They are
2611 variable length packets but, the fields within them are often fixed
2612 length. They are redundent enough to send over UDP as is the
2613 remote protocol in general. There is a matching unit test module
2614 in libstub. */
2615
23860348 2616/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2617 libstub protocol encoding, and remote.c. It is not particularly
23860348 2618 changable. */
cce74817
JM
2619
2620/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2621 Plan to fix this. */
cce74817 2622
23860348 2623typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2624
9d1f7ab2 2625/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2626 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2627
2628struct gdb_ext_thread_info
c5aa993b 2629 {
23860348 2630 threadref threadid; /* External form of thread reference. */
2bc416ba 2631 int active; /* Has state interesting to GDB?
23860348 2632 regs, stack. */
2bc416ba 2633 char display[256]; /* Brief state display, name,
cedea757 2634 blocked/suspended. */
23860348 2635 char shortname[32]; /* To be used to name threads. */
2bc416ba 2636 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2637 whatever. */
c5aa993b 2638 };
cce74817
JM
2639
2640/* The volume of remote transfers can be limited by submitting
2641 a mask containing bits specifying the desired information.
2642 Use a union of these values as the 'selection' parameter to
0df8b418 2643 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2644
2645#define TAG_THREADID 1
2646#define TAG_EXISTS 2
2647#define TAG_DISPLAY 4
2648#define TAG_THREADNAME 8
c5aa993b 2649#define TAG_MOREDISPLAY 16
cce74817 2650
23860348 2651#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2652
a14ed312 2653static char *unpack_nibble (char *buf, int *val);
cce74817 2654
a14ed312 2655static char *unpack_byte (char *buf, int *value);
cce74817 2656
a14ed312 2657static char *pack_int (char *buf, int value);
cce74817 2658
a14ed312 2659static char *unpack_int (char *buf, int *value);
cce74817 2660
a14ed312 2661static char *unpack_string (char *src, char *dest, int length);
cce74817 2662
23860348 2663static char *pack_threadid (char *pkt, threadref *id);
cce74817 2664
23860348 2665static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2666
23860348 2667void int_to_threadref (threadref *id, int value);
cce74817 2668
23860348 2669static int threadref_to_int (threadref *ref);
cce74817 2670
23860348 2671static void copy_threadref (threadref *dest, threadref *src);
cce74817 2672
23860348 2673static int threadmatch (threadref *dest, threadref *src);
cce74817 2674
2bc416ba 2675static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2676 threadref *id);
cce74817 2677
a14ed312 2678static int remote_unpack_thread_info_response (char *pkt,
23860348 2679 threadref *expectedref,
a14ed312
KB
2680 struct gdb_ext_thread_info
2681 *info);
cce74817
JM
2682
2683
2bc416ba 2684static int remote_get_threadinfo (threadref *threadid,
23860348 2685 int fieldset, /*TAG mask */
a14ed312 2686 struct gdb_ext_thread_info *info);
cce74817 2687
a14ed312
KB
2688static char *pack_threadlist_request (char *pkt, int startflag,
2689 int threadcount,
23860348 2690 threadref *nextthread);
cce74817 2691
a14ed312
KB
2692static int parse_threadlist_response (char *pkt,
2693 int result_limit,
23860348 2694 threadref *original_echo,
2bc416ba 2695 threadref *resultlist,
23860348 2696 int *doneflag);
cce74817 2697
a14ed312 2698static int remote_get_threadlist (int startflag,
23860348 2699 threadref *nextthread,
a14ed312
KB
2700 int result_limit,
2701 int *done,
2bc416ba 2702 int *result_count,
23860348 2703 threadref *threadlist);
cce74817 2704
23860348 2705typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2706
a14ed312
KB
2707static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2708 void *context, int looplimit);
cce74817 2709
23860348 2710static int remote_newthread_step (threadref *ref, void *context);
cce74817 2711
82f73884
PA
2712
2713/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2714 buffer we're allowed to write to. Returns
2715 BUF+CHARACTERS_WRITTEN. */
2716
2717static char *
2718write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2719{
2720 int pid, tid;
2721 struct remote_state *rs = get_remote_state ();
2722
2723 if (remote_multi_process_p (rs))
2724 {
2725 pid = ptid_get_pid (ptid);
2726 if (pid < 0)
2727 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2728 else
2729 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2730 }
ba348170 2731 tid = ptid_get_lwp (ptid);
82f73884
PA
2732 if (tid < 0)
2733 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2734 else
2735 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2736
2737 return buf;
2738}
2739
256642e8
PA
2740/* Extract a PTID from BUF. If non-null, OBUF is set to one past the
2741 last parsed char. Returns null_ptid if no thread id is found, and
2742 throws an error if the thread id has an invalid format. */
82f73884
PA
2743
2744static ptid_t
256642e8 2745read_ptid (const char *buf, const char **obuf)
82f73884 2746{
256642e8
PA
2747 const char *p = buf;
2748 const char *pp;
82f73884 2749 ULONGEST pid = 0, tid = 0;
82f73884
PA
2750
2751 if (*p == 'p')
2752 {
2753 /* Multi-process ptid. */
2754 pp = unpack_varlen_hex (p + 1, &pid);
2755 if (*pp != '.')
b37520b6 2756 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2757
2758 p = pp;
2759 pp = unpack_varlen_hex (p + 1, &tid);
2760 if (obuf)
2761 *obuf = pp;
ba348170 2762 return ptid_build (pid, tid, 0);
82f73884
PA
2763 }
2764
2765 /* No multi-process. Just a tid. */
2766 pp = unpack_varlen_hex (p, &tid);
2767
c9f35b34
KB
2768 /* Return null_ptid when no thread id is found. */
2769 if (p == pp)
2770 {
2771 if (obuf)
2772 *obuf = pp;
2773 return null_ptid;
2774 }
2775
82f73884 2776 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2777 what's in inferior_ptid, unless it's null at this point. If so,
2778 then since there's no way to know the pid of the reported
2779 threads, use the magic number. */
2780 if (ptid_equal (inferior_ptid, null_ptid))
2781 pid = ptid_get_pid (magic_null_ptid);
2782 else
2783 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2784
2785 if (obuf)
2786 *obuf = pp;
ba348170 2787 return ptid_build (pid, tid, 0);
82f73884
PA
2788}
2789
c906108c 2790static int
fba45db2 2791stubhex (int ch)
c906108c
SS
2792{
2793 if (ch >= 'a' && ch <= 'f')
2794 return ch - 'a' + 10;
2795 if (ch >= '0' && ch <= '9')
2796 return ch - '0';
2797 if (ch >= 'A' && ch <= 'F')
2798 return ch - 'A' + 10;
2799 return -1;
2800}
2801
2802static int
fba45db2 2803stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2804{
2805 int nibble;
2806 int retval = 0;
2807
2808 while (fieldlength)
2809 {
2810 nibble = stubhex (*buff++);
2811 retval |= nibble;
2812 fieldlength--;
2813 if (fieldlength)
2814 retval = retval << 4;
2815 }
2816 return retval;
2817}
2818
c906108c 2819static char *
fba45db2 2820unpack_nibble (char *buf, int *val)
c906108c 2821{
b7589f7d 2822 *val = fromhex (*buf++);
c906108c
SS
2823 return buf;
2824}
2825
c906108c 2826static char *
fba45db2 2827unpack_byte (char *buf, int *value)
c906108c
SS
2828{
2829 *value = stub_unpack_int (buf, 2);
2830 return buf + 2;
2831}
2832
2833static char *
fba45db2 2834pack_int (char *buf, int value)
c906108c
SS
2835{
2836 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2837 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2838 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2839 buf = pack_hex_byte (buf, (value & 0xff));
2840 return buf;
2841}
2842
2843static char *
fba45db2 2844unpack_int (char *buf, int *value)
c906108c
SS
2845{
2846 *value = stub_unpack_int (buf, 8);
2847 return buf + 8;
2848}
2849
23860348 2850#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2851static char *pack_string (char *pkt, char *string);
c906108c
SS
2852
2853static char *
fba45db2 2854pack_string (char *pkt, char *string)
c906108c
SS
2855{
2856 char ch;
2857 int len;
2858
2859 len = strlen (string);
2860 if (len > 200)
23860348 2861 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2862 pkt = pack_hex_byte (pkt, len);
2863 while (len-- > 0)
2864 {
2865 ch = *string++;
2866 if ((ch == '\0') || (ch == '#'))
23860348 2867 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2868 *pkt++ = ch;
2869 }
2870 return pkt;
2871}
2872#endif /* 0 (unused) */
2873
2874static char *
fba45db2 2875unpack_string (char *src, char *dest, int length)
c906108c
SS
2876{
2877 while (length--)
2878 *dest++ = *src++;
2879 *dest = '\0';
2880 return src;
2881}
2882
2883static char *
fba45db2 2884pack_threadid (char *pkt, threadref *id)
c906108c
SS
2885{
2886 char *limit;
2887 unsigned char *altid;
2888
2889 altid = (unsigned char *) id;
2890 limit = pkt + BUF_THREAD_ID_SIZE;
2891 while (pkt < limit)
2892 pkt = pack_hex_byte (pkt, *altid++);
2893 return pkt;
2894}
2895
2896
2897static char *
fba45db2 2898unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2899{
2900 char *altref;
2901 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2902 int x, y;
2903
2904 altref = (char *) id;
2905
2906 while (inbuf < limit)
2907 {
2908 x = stubhex (*inbuf++);
2909 y = stubhex (*inbuf++);
2910 *altref++ = (x << 4) | y;
2911 }
2912 return inbuf;
2913}
2914
2915/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2916 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2917 to use 64bit thread references internally. This is an adapter
2918 function. */
2919
2920void
fba45db2 2921int_to_threadref (threadref *id, int value)
c906108c
SS
2922{
2923 unsigned char *scan;
2924
2925 scan = (unsigned char *) id;
2926 {
2927 int i = 4;
2928 while (i--)
2929 *scan++ = 0;
2930 }
2931 *scan++ = (value >> 24) & 0xff;
2932 *scan++ = (value >> 16) & 0xff;
2933 *scan++ = (value >> 8) & 0xff;
2934 *scan++ = (value & 0xff);
2935}
2936
2937static int
fba45db2 2938threadref_to_int (threadref *ref)
c906108c
SS
2939{
2940 int i, value = 0;
2941 unsigned char *scan;
2942
cfd77fa1 2943 scan = *ref;
c906108c
SS
2944 scan += 4;
2945 i = 4;
2946 while (i-- > 0)
2947 value = (value << 8) | ((*scan++) & 0xff);
2948 return value;
2949}
2950
2951static void
fba45db2 2952copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2953{
2954 int i;
2955 unsigned char *csrc, *cdest;
2956
2957 csrc = (unsigned char *) src;
2958 cdest = (unsigned char *) dest;
2959 i = 8;
2960 while (i--)
2961 *cdest++ = *csrc++;
2962}
2963
2964static int
fba45db2 2965threadmatch (threadref *dest, threadref *src)
c906108c 2966{
23860348 2967 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2968#if 0
2969 unsigned char *srcp, *destp;
2970 int i, result;
2971 srcp = (char *) src;
2972 destp = (char *) dest;
2973
2974 result = 1;
2975 while (i-- > 0)
2976 result &= (*srcp++ == *destp++) ? 1 : 0;
2977 return result;
2978#endif
2979 return 1;
2980}
2981
2982/*
c5aa993b
JM
2983 threadid:1, # always request threadid
2984 context_exists:2,
2985 display:4,
2986 unique_name:8,
2987 more_display:16
2988 */
c906108c
SS
2989
2990/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2991
2992static char *
fba45db2 2993pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2994{
23860348
MS
2995 *pkt++ = 'q'; /* Info Query */
2996 *pkt++ = 'P'; /* process or thread info */
2997 pkt = pack_int (pkt, mode); /* mode */
c906108c 2998 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2999 *pkt = '\0'; /* terminate */
c906108c
SS
3000 return pkt;
3001}
3002
23860348 3003/* These values tag the fields in a thread info response packet. */
c906108c 3004/* Tagging the fields allows us to request specific fields and to
23860348 3005 add more fields as time goes by. */
c906108c 3006
23860348 3007#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 3008#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 3009 fetch registers and its stack? */
c5aa993b 3010#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 3011#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 3012#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 3013 the process. */
c906108c
SS
3014
3015static int
fba45db2
KB
3016remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
3017 struct gdb_ext_thread_info *info)
c906108c 3018{
d01949b6 3019 struct remote_state *rs = get_remote_state ();
c906108c 3020 int mask, length;
cfd77fa1 3021 int tag;
c906108c 3022 threadref ref;
6d820c5c 3023 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
3024 int retval = 1;
3025
23860348 3026 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
3027 info->active = 0;
3028 info->display[0] = '\0';
3029 info->shortname[0] = '\0';
3030 info->more_display[0] = '\0';
3031
23860348
MS
3032 /* Assume the characters indicating the packet type have been
3033 stripped. */
c906108c
SS
3034 pkt = unpack_int (pkt, &mask); /* arg mask */
3035 pkt = unpack_threadid (pkt, &ref);
3036
3037 if (mask == 0)
8a3fe4f8 3038 warning (_("Incomplete response to threadinfo request."));
c906108c 3039 if (!threadmatch (&ref, expectedref))
23860348 3040 { /* This is an answer to a different request. */
8a3fe4f8 3041 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
3042 return 0;
3043 }
3044 copy_threadref (&info->threadid, &ref);
3045
23860348 3046 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 3047
23860348
MS
3048 /* Packets are terminated with nulls. */
3049 while ((pkt < limit) && mask && *pkt)
c906108c
SS
3050 {
3051 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
3052 pkt = unpack_byte (pkt, &length); /* length */
3053 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 3054 {
8a3fe4f8 3055 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
3056 retval = 0;
3057 break;
3058 }
3059 if (tag == TAG_THREADID)
3060 {
3061 if (length != 16)
3062 {
8a3fe4f8 3063 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
3064 retval = 0;
3065 break;
3066 }
3067 pkt = unpack_threadid (pkt, &ref);
3068 mask = mask & ~TAG_THREADID;
3069 continue;
3070 }
3071 if (tag == TAG_EXISTS)
3072 {
3073 info->active = stub_unpack_int (pkt, length);
3074 pkt += length;
3075 mask = mask & ~(TAG_EXISTS);
3076 if (length > 8)
3077 {
8a3fe4f8 3078 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
3079 retval = 0;
3080 break;
3081 }
3082 continue;
3083 }
3084 if (tag == TAG_THREADNAME)
3085 {
3086 pkt = unpack_string (pkt, &info->shortname[0], length);
3087 mask = mask & ~TAG_THREADNAME;
3088 continue;
3089 }
3090 if (tag == TAG_DISPLAY)
3091 {
3092 pkt = unpack_string (pkt, &info->display[0], length);
3093 mask = mask & ~TAG_DISPLAY;
3094 continue;
3095 }
3096 if (tag == TAG_MOREDISPLAY)
3097 {
3098 pkt = unpack_string (pkt, &info->more_display[0], length);
3099 mask = mask & ~TAG_MOREDISPLAY;
3100 continue;
3101 }
8a3fe4f8 3102 warning (_("ERROR RMT: unknown thread info tag."));
23860348 3103 break; /* Not a tag we know about. */
c906108c
SS
3104 }
3105 return retval;
3106}
3107
3108static int
fba45db2
KB
3109remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
3110 struct gdb_ext_thread_info *info)
c906108c 3111{
d01949b6 3112 struct remote_state *rs = get_remote_state ();
c906108c 3113 int result;
c906108c 3114
2e9f7625
DJ
3115 pack_threadinfo_request (rs->buf, fieldset, threadid);
3116 putpkt (rs->buf);
6d820c5c 3117 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
3118
3119 if (rs->buf[0] == '\0')
3120 return 0;
3121
2e9f7625 3122 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 3123 threadid, info);
c906108c
SS
3124 return result;
3125}
3126
c906108c
SS
3127/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3128
3129static char *
fba45db2
KB
3130pack_threadlist_request (char *pkt, int startflag, int threadcount,
3131 threadref *nextthread)
c906108c
SS
3132{
3133 *pkt++ = 'q'; /* info query packet */
3134 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 3135 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
3136 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
3137 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
3138 *pkt = '\0';
3139 return pkt;
3140}
3141
3142/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3143
3144static int
fba45db2
KB
3145parse_threadlist_response (char *pkt, int result_limit,
3146 threadref *original_echo, threadref *resultlist,
3147 int *doneflag)
c906108c 3148{
d01949b6 3149 struct remote_state *rs = get_remote_state ();
c906108c
SS
3150 char *limit;
3151 int count, resultcount, done;
3152
3153 resultcount = 0;
3154 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 3155 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 3156 /* done parse past here */
c906108c
SS
3157 pkt = unpack_byte (pkt, &count); /* count field */
3158 pkt = unpack_nibble (pkt, &done);
3159 /* The first threadid is the argument threadid. */
3160 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
3161 while ((count-- > 0) && (pkt < limit))
3162 {
3163 pkt = unpack_threadid (pkt, resultlist++);
3164 if (resultcount++ >= result_limit)
3165 break;
3166 }
3167 if (doneflag)
3168 *doneflag = done;
3169 return resultcount;
3170}
3171
6dc54d91
PA
3172/* Fetch the next batch of threads from the remote. Returns -1 if the
3173 qL packet is not supported, 0 on error and 1 on success. */
3174
c906108c 3175static int
fba45db2
KB
3176remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
3177 int *done, int *result_count, threadref *threadlist)
c906108c 3178{
d01949b6 3179 struct remote_state *rs = get_remote_state ();
c906108c
SS
3180 int result = 1;
3181
23860348 3182 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
3183 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
3184 >= get_remote_packet_size ())
ea9c271d 3185 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 3186
6d820c5c
DJ
3187 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
3188 putpkt (rs->buf);
3189 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 3190 if (*rs->buf == '\0')
6dc54d91
PA
3191 {
3192 /* Packet not supported. */
3193 return -1;
3194 }
3195
3196 *result_count =
3197 parse_threadlist_response (rs->buf + 2, result_limit,
3198 &rs->echo_nextthread, threadlist, done);
c906108c 3199
0d031856 3200 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 3201 {
23860348
MS
3202 /* FIXME: This is a good reason to drop the packet. */
3203 /* Possably, there is a duplicate response. */
c906108c
SS
3204 /* Possabilities :
3205 retransmit immediatly - race conditions
3206 retransmit after timeout - yes
3207 exit
3208 wait for packet, then exit
3209 */
8a3fe4f8 3210 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 3211 return 0; /* I choose simply exiting. */
c906108c
SS
3212 }
3213 if (*result_count <= 0)
3214 {
3215 if (*done != 1)
3216 {
8a3fe4f8 3217 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
3218 result = 0;
3219 }
3220 return result; /* break; */
3221 }
3222 if (*result_count > result_limit)
3223 {
3224 *result_count = 0;
8a3fe4f8 3225 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
3226 return 0;
3227 }
3228 return result;
3229}
3230
6dc54d91
PA
3231/* Fetch the list of remote threads, with the qL packet, and call
3232 STEPFUNCTION for each thread found. Stops iterating and returns 1
3233 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3234 STEPFUNCTION returns false. If the packet is not supported,
3235 returns -1. */
c906108c 3236
c906108c 3237static int
fba45db2
KB
3238remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
3239 int looplimit)
c906108c 3240{
0d031856 3241 struct remote_state *rs = get_remote_state ();
c906108c
SS
3242 int done, i, result_count;
3243 int startflag = 1;
3244 int result = 1;
3245 int loopcount = 0;
c906108c
SS
3246
3247 done = 0;
3248 while (!done)
3249 {
3250 if (loopcount++ > looplimit)
3251 {
3252 result = 0;
8a3fe4f8 3253 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
3254 break;
3255 }
6dc54d91
PA
3256 result = remote_get_threadlist (startflag, &rs->nextthread,
3257 MAXTHREADLISTRESULTS,
3258 &done, &result_count,
3259 rs->resultthreadlist);
3260 if (result <= 0)
3261 break;
23860348 3262 /* Clear for later iterations. */
c906108c
SS
3263 startflag = 0;
3264 /* Setup to resume next batch of thread references, set nextthread. */
3265 if (result_count >= 1)
0d031856
TT
3266 copy_threadref (&rs->nextthread,
3267 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
3268 i = 0;
3269 while (result_count--)
6dc54d91
PA
3270 {
3271 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
3272 {
3273 result = 0;
3274 break;
3275 }
3276 }
c906108c
SS
3277 }
3278 return result;
3279}
3280
6dc54d91
PA
3281/* A thread found on the remote target. */
3282
21fe1c75 3283struct thread_item
6dc54d91 3284{
21fe1c75
SM
3285 explicit thread_item (ptid_t ptid_)
3286 : ptid (ptid_)
3287 {}
3288
3289 thread_item (thread_item &&other) = default;
3290 thread_item &operator= (thread_item &&other) = default;
3291
3292 DISABLE_COPY_AND_ASSIGN (thread_item);
3293
6dc54d91
PA
3294 /* The thread's PTID. */
3295 ptid_t ptid;
3296
21fe1c75
SM
3297 /* The thread's extra info. */
3298 std::string extra;
6dc54d91 3299
21fe1c75
SM
3300 /* The thread's name. */
3301 std::string name;
79efa585 3302
6dc54d91 3303 /* The core the thread was running on. -1 if not known. */
21fe1c75 3304 int core = -1;
f6327dcb
KB
3305
3306 /* The thread handle associated with the thread. */
21fe1c75 3307 gdb::byte_vector thread_handle;
21fe1c75 3308};
6dc54d91
PA
3309
3310/* Context passed around to the various methods listing remote
3311 threads. As new threads are found, they're added to the ITEMS
3312 vector. */
3313
3314struct threads_listing_context
3315{
21fe1c75
SM
3316 /* Return true if this object contains an entry for a thread with ptid
3317 PTID. */
6dc54d91 3318
21fe1c75
SM
3319 bool contains_thread (ptid_t ptid) const
3320 {
3321 auto match_ptid = [&] (const thread_item &item)
3322 {
3323 return item.ptid == ptid;
3324 };
80134cf5 3325
21fe1c75
SM
3326 auto it = std::find_if (this->items.begin (),
3327 this->items.end (),
3328 match_ptid);
80134cf5 3329
21fe1c75
SM
3330 return it != this->items.end ();
3331 }
80134cf5 3332
21fe1c75 3333 /* Remove the thread with ptid PTID. */
80134cf5 3334
21fe1c75
SM
3335 void remove_thread (ptid_t ptid)
3336 {
3337 auto match_ptid = [&] (const thread_item &item)
3338 {
3339 return item.ptid == ptid;
3340 };
cbb8991c 3341
21fe1c75
SM
3342 auto it = std::remove_if (this->items.begin (),
3343 this->items.end (),
3344 match_ptid);
cbb8991c 3345
21fe1c75
SM
3346 if (it != this->items.end ())
3347 this->items.erase (it);
3348 }
3349
3350 /* The threads found on the remote target. */
3351 std::vector<thread_item> items;
3352};
cbb8991c 3353
c906108c 3354static int
6dc54d91 3355remote_newthread_step (threadref *ref, void *data)
c906108c 3356{
19ba03f4
SM
3357 struct threads_listing_context *context
3358 = (struct threads_listing_context *) data;
21fe1c75
SM
3359 int pid = inferior_ptid.pid ();
3360 int lwp = threadref_to_int (ref);
3361 ptid_t ptid (pid, lwp);
6dc54d91 3362
21fe1c75 3363 context->items.emplace_back (ptid);
6dc54d91 3364
c906108c
SS
3365 return 1; /* continue iterator */
3366}
3367
3368#define CRAZY_MAX_THREADS 1000
3369
39f77062
KB
3370static ptid_t
3371remote_current_thread (ptid_t oldpid)
c906108c 3372{
d01949b6 3373 struct remote_state *rs = get_remote_state ();
c906108c
SS
3374
3375 putpkt ("qC");
6d820c5c 3376 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3377 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34 3378 {
256642e8 3379 const char *obuf;
c9f35b34
KB
3380 ptid_t result;
3381
3382 result = read_ptid (&rs->buf[2], &obuf);
3383 if (*obuf != '\0' && remote_debug)
3384 fprintf_unfiltered (gdb_stdlog,
3385 "warning: garbage in qC reply\n");
3386
3387 return result;
3388 }
c906108c
SS
3389 else
3390 return oldpid;
3391}
3392
6dc54d91 3393/* List remote threads using the deprecated qL packet. */
cce74817 3394
6dc54d91
PA
3395static int
3396remote_get_threads_with_ql (struct target_ops *ops,
3397 struct threads_listing_context *context)
c906108c 3398{
6dc54d91
PA
3399 if (remote_threadlist_iterator (remote_newthread_step, context,
3400 CRAZY_MAX_THREADS) >= 0)
3401 return 1;
3402
3403 return 0;
c906108c
SS
3404}
3405
dc146f7c
VP
3406#if defined(HAVE_LIBEXPAT)
3407
dc146f7c
VP
3408static void
3409start_thread (struct gdb_xml_parser *parser,
3410 const struct gdb_xml_element *element,
4d0fdd9b
SM
3411 void *user_data,
3412 std::vector<gdb_xml_value> &attributes)
dc146f7c 3413{
19ba03f4
SM
3414 struct threads_listing_context *data
3415 = (struct threads_listing_context *) user_data;
3d2c1d41 3416 struct gdb_xml_value *attr;
dc146f7c 3417
4d0fdd9b 3418 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
21fe1c75
SM
3419 ptid_t ptid = read_ptid (id, NULL);
3420
3421 data->items.emplace_back (ptid);
3422 thread_item &item = data->items.back ();
dc146f7c 3423
3d2c1d41
PA
3424 attr = xml_find_attribute (attributes, "core");
3425 if (attr != NULL)
4d0fdd9b 3426 item.core = *(ULONGEST *) attr->value.get ();
dc146f7c 3427
79efa585 3428 attr = xml_find_attribute (attributes, "name");
21fe1c75 3429 if (attr != NULL)
4d0fdd9b 3430 item.name = (const char *) attr->value.get ();
79efa585 3431
f6327dcb
KB
3432 attr = xml_find_attribute (attributes, "handle");
3433 if (attr != NULL)
4d0fdd9b 3434 item.thread_handle = hex2bin ((const char *) attr->value.get ());
dc146f7c
VP
3435}
3436
3437static void
3438end_thread (struct gdb_xml_parser *parser,
3439 const struct gdb_xml_element *element,
3440 void *user_data, const char *body_text)
3441{
19ba03f4
SM
3442 struct threads_listing_context *data
3443 = (struct threads_listing_context *) user_data;
dc146f7c 3444
21fe1c75
SM
3445 if (body_text != NULL && *body_text != '\0')
3446 data->items.back ().extra = body_text;
dc146f7c
VP
3447}
3448
3449const struct gdb_xml_attribute thread_attributes[] = {
3450 { "id", GDB_XML_AF_NONE, NULL, NULL },
3451 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
79efa585 3452 { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
f6327dcb 3453 { "handle", GDB_XML_AF_OPTIONAL, NULL, NULL },
dc146f7c
VP
3454 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3455};
3456
3457const struct gdb_xml_element thread_children[] = {
3458 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3459};
3460
3461const struct gdb_xml_element threads_children[] = {
3462 { "thread", thread_attributes, thread_children,
3463 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3464 start_thread, end_thread },
3465 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3466};
3467
3468const struct gdb_xml_element threads_elements[] = {
3469 { "threads", NULL, threads_children,
3470 GDB_XML_EF_NONE, NULL, NULL },
3471 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3472};
3473
3474#endif
3475
6dc54d91 3476/* List remote threads using qXfer:threads:read. */
9d1f7ab2 3477
6dc54d91
PA
3478static int
3479remote_get_threads_with_qxfer (struct target_ops *ops,
3480 struct threads_listing_context *context)
0f71a2f6 3481{
dc146f7c 3482#if defined(HAVE_LIBEXPAT)
4082afcc 3483 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 3484 {
9018be22 3485 gdb::optional<gdb::char_vector> xml
b7b030ad 3486 = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
efc0eabd 3487
9018be22 3488 if (xml && (*xml)[0] != '\0')
dc146f7c 3489 {
6dc54d91 3490 gdb_xml_parse_quick (_("threads"), "threads.dtd",
9018be22 3491 threads_elements, xml->data (), context);
dc146f7c
VP
3492 }
3493
6dc54d91 3494 return 1;
dc146f7c
VP
3495 }
3496#endif
3497
6dc54d91
PA
3498 return 0;
3499}
3500
3501/* List remote threads using qfThreadInfo/qsThreadInfo. */
3502
3503static int
3504remote_get_threads_with_qthreadinfo (struct target_ops *ops,
3505 struct threads_listing_context *context)
3506{
3507 struct remote_state *rs = get_remote_state ();
3508
b80fafe3 3509 if (rs->use_threadinfo_query)
9d1f7ab2 3510 {
256642e8 3511 const char *bufp;
6dc54d91 3512
9d1f7ab2 3513 putpkt ("qfThreadInfo");
6d820c5c 3514 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3515 bufp = rs->buf;
9d1f7ab2 3516 if (bufp[0] != '\0') /* q packet recognized */
802188a7 3517 {
9d1f7ab2
MS
3518 while (*bufp++ == 'm') /* reply contains one or more TID */
3519 {
3520 do
3521 {
21fe1c75
SM
3522 ptid_t ptid = read_ptid (bufp, &bufp);
3523 context->items.emplace_back (ptid);
9d1f7ab2
MS
3524 }
3525 while (*bufp++ == ','); /* comma-separated list */
3526 putpkt ("qsThreadInfo");
6d820c5c 3527 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3528 bufp = rs->buf;
9d1f7ab2 3529 }
6dc54d91
PA
3530 return 1;
3531 }
3532 else
3533 {
3534 /* Packet not recognized. */
3535 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3536 }
3537 }
3538
6dc54d91
PA
3539 return 0;
3540}
3541
e8032dde 3542/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3543 targets. */
3544
f6ac5f3d
PA
3545void
3546remote_target::update_thread_list ()
6dc54d91 3547{
6dc54d91 3548 struct threads_listing_context context;
ab970af1 3549 int got_list = 0;
e8032dde 3550
6dc54d91
PA
3551 /* We have a few different mechanisms to fetch the thread list. Try
3552 them all, starting with the most preferred one first, falling
3553 back to older methods. */
f6ac5f3d
PA
3554 if (remote_get_threads_with_qxfer (this, &context)
3555 || remote_get_threads_with_qthreadinfo (this, &context)
3556 || remote_get_threads_with_ql (this, &context))
6dc54d91 3557 {
ab970af1
PA
3558 struct thread_info *tp, *tmp;
3559
3560 got_list = 1;
3561
21fe1c75 3562 if (context.items.empty ()
f6ac5f3d 3563 && remote_thread_always_alive (inferior_ptid))
7d1a114c
PA
3564 {
3565 /* Some targets don't really support threads, but still
3566 reply an (empty) thread list in response to the thread
3567 listing packets, instead of replying "packet not
3568 supported". Exit early so we don't delete the main
3569 thread. */
7d1a114c
PA
3570 return;
3571 }
3572
ab970af1
PA
3573 /* CONTEXT now holds the current thread list on the remote
3574 target end. Delete GDB-side threads no longer found on the
3575 target. */
8a06aea7 3576 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3577 {
21fe1c75 3578 if (!context.contains_thread (tp->ptid))
ab970af1
PA
3579 {
3580 /* Not found. */
3581 delete_thread (tp->ptid);
3582 }
cbb8991c
DB
3583 }
3584
3585 /* Remove any unreported fork child threads from CONTEXT so
3586 that we don't interfere with follow fork, which is where
3587 creation of such threads is handled. */
3588 remove_new_fork_children (&context);
74531fed 3589
ab970af1 3590 /* And now add threads we don't know about yet to our list. */
21fe1c75 3591 for (thread_item &item : context.items)
6dc54d91 3592 {
21fe1c75 3593 if (item.ptid != null_ptid)
6dc54d91 3594 {
6dc54d91 3595 /* In non-stop mode, we assume new found threads are
0d5b594f
PA
3596 executing until proven otherwise with a stop reply.
3597 In all-stop, we can only get here if all threads are
6dc54d91 3598 stopped. */
0d5b594f 3599 int executing = target_is_non_stop_p () ? 1 : 0;
6dc54d91 3600
21fe1c75 3601 remote_notice_new_inferior (item.ptid, executing);
6dc54d91 3602
7aabaf9d 3603 remote_thread_info *info = get_remote_thread_info (item.ptid);
21fe1c75 3604 info->core = item.core;
7aabaf9d
SM
3605 info->extra = std::move (item.extra);
3606 info->name = std::move (item.name);
3607 info->thread_handle = std::move (item.thread_handle);
6dc54d91
PA
3608 }
3609 }
3610 }
3611
ab970af1
PA
3612 if (!got_list)
3613 {
3614 /* If no thread listing method is supported, then query whether
3615 each known thread is alive, one by one, with the T packet.
3616 If the target doesn't support threads at all, then this is a
3617 no-op. See remote_thread_alive. */
3618 prune_threads ();
3619 }
9d1f7ab2
MS
3620}
3621
802188a7 3622/*
9d1f7ab2
MS
3623 * Collect a descriptive string about the given thread.
3624 * The target may say anything it wants to about the thread
3625 * (typically info about its blocked / runnable state, name, etc.).
3626 * This string will appear in the info threads display.
802188a7 3627 *
9d1f7ab2
MS
3628 * Optional: targets are not required to implement this function.
3629 */
3630
f6ac5f3d
PA
3631const char *
3632remote_target::extra_thread_info (thread_info *tp)
9d1f7ab2 3633{
d01949b6 3634 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3635 int result;
3636 int set;
3637 threadref id;
3638 struct gdb_ext_thread_info threadinfo;
23860348 3639 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3640 int n = 0; /* position in display_buf */
3641
5d93a237 3642 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3643 internal_error (__FILE__, __LINE__,
e2e0b3e5 3644 _("remote_threads_extra_info"));
9d1f7ab2 3645
60e569b9 3646 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3647 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3648 /* This is the main thread which was added by GDB. The remote
3649 server doesn't know about it. */
3650 return NULL;
3651
4082afcc 3652 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3653 {
3654 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3655
7aabaf9d 3656 if (info != NULL && info->priv != NULL)
a9334058
SM
3657 {
3658 const std::string &extra = get_remote_thread_info (info)->extra;
3659 return !extra.empty () ? extra.c_str () : NULL;
3660 }
dc146f7c
VP
3661 else
3662 return NULL;
3663 }
3664
b80fafe3 3665 if (rs->use_threadextra_query)
9d1f7ab2 3666 {
82f73884
PA
3667 char *b = rs->buf;
3668 char *endb = rs->buf + get_remote_packet_size ();
3669
3670 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3671 b += strlen (b);
3672 write_ptid (b, endb, tp->ptid);
3673
2e9f7625 3674 putpkt (rs->buf);
6d820c5c 3675 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3676 if (rs->buf[0] != 0)
9d1f7ab2 3677 {
325fac50 3678 n = std::min (strlen (rs->buf) / 2, sizeof (display_buf));
2e9f7625 3679 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3680 display_buf [result] = '\0';
9d1f7ab2
MS
3681 return display_buf;
3682 }
0f71a2f6 3683 }
9d1f7ab2
MS
3684
3685 /* If the above query fails, fall back to the old method. */
b80fafe3 3686 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3687 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3688 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3689 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3690 if (remote_get_threadinfo (&id, set, &threadinfo))
3691 if (threadinfo.active)
0f71a2f6 3692 {
9d1f7ab2 3693 if (*threadinfo.shortname)
2bc416ba 3694 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3695 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3696 if (*threadinfo.display)
2bc416ba 3697 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3698 " State: %s,", threadinfo.display);
9d1f7ab2 3699 if (*threadinfo.more_display)
2bc416ba 3700 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3701 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3702
3703 if (n > 0)
c5aa993b 3704 {
23860348 3705 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3706 if (',' == display_buf[n-1])
3707 display_buf[n-1] = ' ';
3708 return display_buf;
c5aa993b 3709 }
0f71a2f6 3710 }
9d1f7ab2 3711 return NULL;
0f71a2f6 3712}
c906108c 3713\f
c5aa993b 3714
f6ac5f3d
PA
3715bool
3716remote_target::static_tracepoint_marker_at (CORE_ADDR addr,
3717 struct static_tracepoint_marker *marker)
0fb4aa4b
PA
3718{
3719 struct remote_state *rs = get_remote_state ();
3720 char *p = rs->buf;
3721
bba74b36 3722 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3723 p += strlen (p);
3724 p += hexnumstr (p, addr);
3725 putpkt (rs->buf);
3726 getpkt (&rs->buf, &rs->buf_size, 0);
3727 p = rs->buf;
3728
3729 if (*p == 'E')
3730 error (_("Remote failure reply: %s"), p);
3731
3732 if (*p++ == 'm')
3733 {
256642e8 3734 parse_static_tracepoint_marker_definition (p, NULL, marker);
5d9310c4 3735 return true;
0fb4aa4b
PA
3736 }
3737
5d9310c4 3738 return false;
0fb4aa4b
PA
3739}
3740
f6ac5f3d
PA
3741std::vector<static_tracepoint_marker>
3742remote_target::static_tracepoint_markers_by_strid (const char *strid)
0fb4aa4b
PA
3743{
3744 struct remote_state *rs = get_remote_state ();
5d9310c4 3745 std::vector<static_tracepoint_marker> markers;
256642e8 3746 const char *p;
5d9310c4 3747 static_tracepoint_marker marker;
0fb4aa4b
PA
3748
3749 /* Ask for a first packet of static tracepoint marker
3750 definition. */
3751 putpkt ("qTfSTM");
3752 getpkt (&rs->buf, &rs->buf_size, 0);
3753 p = rs->buf;
3754 if (*p == 'E')
3755 error (_("Remote failure reply: %s"), p);
3756
0fb4aa4b
PA
3757 while (*p++ == 'm')
3758 {
0fb4aa4b
PA
3759 do
3760 {
5d9310c4 3761 parse_static_tracepoint_marker_definition (p, &p, &marker);
0fb4aa4b 3762
5d9310c4
SM
3763 if (strid == NULL || marker.str_id == strid)
3764 markers.push_back (std::move (marker));
0fb4aa4b
PA
3765 }
3766 while (*p++ == ','); /* comma-separated list */
3767 /* Ask for another packet of static tracepoint definition. */
3768 putpkt ("qTsSTM");
3769 getpkt (&rs->buf, &rs->buf_size, 0);
3770 p = rs->buf;
3771 }
3772
0fb4aa4b
PA
3773 return markers;
3774}
3775
3776\f
10760264
JB
3777/* Implement the to_get_ada_task_ptid function for the remote targets. */
3778
f6ac5f3d
PA
3779ptid_t
3780remote_target::get_ada_task_ptid (long lwp, long thread)
10760264 3781{
ba348170 3782 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3783}
3784\f
3785
24b06219 3786/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3787
3788static void
fba45db2 3789extended_remote_restart (void)
c906108c 3790{
d01949b6 3791 struct remote_state *rs = get_remote_state ();
c906108c
SS
3792
3793 /* Send the restart command; for reasons I don't understand the
3794 remote side really expects a number after the "R". */
ea9c271d 3795 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3796 putpkt (rs->buf);
c906108c 3797
ad9a8f3f 3798 remote_fileio_reset ();
c906108c
SS
3799}
3800\f
3801/* Clean up connection to a remote debugger. */
3802
f6ac5f3d
PA
3803void
3804remote_target::close ()
c906108c 3805{
5d93a237
TT
3806 struct remote_state *rs = get_remote_state ();
3807
3808 if (rs->remote_desc == NULL)
d3fd5342
PA
3809 return; /* already closed */
3810
048094ac 3811 /* Make sure we leave stdin registered in the event loop. */
f6ac5f3d 3812 terminal_ours ();
ce5ce7ed 3813
5d93a237
TT
3814 serial_close (rs->remote_desc);
3815 rs->remote_desc = NULL;
ce5ce7ed
PA
3816
3817 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3818 of all the inferiors and their threads we were controlling.
3819 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3820 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3821 inferior_ptid = null_ptid;
f67fd822 3822 discard_all_inferiors ();
ce5ce7ed 3823
f48ff2a7
YQ
3824 /* We are closing the remote target, so we should discard
3825 everything of this target. */
bcc75809 3826 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3827
3828 if (remote_async_inferior_event_token)
3829 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3830
5965e028 3831 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3832
3833 trace_reset_local_state ();
c906108c
SS
3834}
3835
23860348 3836/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3837
3838static void
fba45db2 3839get_offsets (void)
c906108c 3840{
d01949b6 3841 struct remote_state *rs = get_remote_state ();
2e9f7625 3842 char *buf;
085dd6e6 3843 char *ptr;
31d99776
DJ
3844 int lose, num_segments = 0, do_sections, do_segments;
3845 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3846 struct section_offsets *offs;
31d99776
DJ
3847 struct symfile_segment_data *data;
3848
3849 if (symfile_objfile == NULL)
3850 return;
c906108c
SS
3851
3852 putpkt ("qOffsets");
6d820c5c 3853 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3854 buf = rs->buf;
c906108c
SS
3855
3856 if (buf[0] == '\000')
3857 return; /* Return silently. Stub doesn't support
23860348 3858 this command. */
c906108c
SS
3859 if (buf[0] == 'E')
3860 {
8a3fe4f8 3861 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3862 return;
3863 }
3864
3865 /* Pick up each field in turn. This used to be done with scanf, but
3866 scanf will make trouble if CORE_ADDR size doesn't match
3867 conversion directives correctly. The following code will work
3868 with any size of CORE_ADDR. */
3869 text_addr = data_addr = bss_addr = 0;
3870 ptr = buf;
3871 lose = 0;
3872
61012eef 3873 if (startswith (ptr, "Text="))
c906108c
SS
3874 {
3875 ptr += 5;
3876 /* Don't use strtol, could lose on big values. */
3877 while (*ptr && *ptr != ';')
3878 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3879
61012eef 3880 if (startswith (ptr, ";Data="))
31d99776
DJ
3881 {
3882 ptr += 6;
3883 while (*ptr && *ptr != ';')
3884 data_addr = (data_addr << 4) + fromhex (*ptr++);
3885 }
3886 else
3887 lose = 1;
3888
61012eef 3889 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3890 {
3891 ptr += 5;
3892 while (*ptr && *ptr != ';')
3893 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3894
31d99776
DJ
3895 if (bss_addr != data_addr)
3896 warning (_("Target reported unsupported offsets: %s"), buf);
3897 }
3898 else
3899 lose = 1;
3900 }
61012eef 3901 else if (startswith (ptr, "TextSeg="))
c906108c 3902 {
31d99776
DJ
3903 ptr += 8;
3904 /* Don't use strtol, could lose on big values. */
c906108c 3905 while (*ptr && *ptr != ';')
31d99776
DJ
3906 text_addr = (text_addr << 4) + fromhex (*ptr++);
3907 num_segments = 1;
3908
61012eef 3909 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3910 {
3911 ptr += 9;
3912 while (*ptr && *ptr != ';')
3913 data_addr = (data_addr << 4) + fromhex (*ptr++);
3914 num_segments++;
3915 }
c906108c
SS
3916 }
3917 else
3918 lose = 1;
3919
3920 if (lose)
8a3fe4f8 3921 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3922 else if (*ptr != '\0')
3923 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3924
802188a7 3925 offs = ((struct section_offsets *)
a39a16c4 3926 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3927 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3928 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3929
31d99776
DJ
3930 data = get_symfile_segment_data (symfile_objfile->obfd);
3931 do_segments = (data != NULL);
3932 do_sections = num_segments == 0;
c906108c 3933
28c32713 3934 if (num_segments > 0)
31d99776 3935 {
31d99776
DJ
3936 segments[0] = text_addr;
3937 segments[1] = data_addr;
3938 }
28c32713
JB
3939 /* If we have two segments, we can still try to relocate everything
3940 by assuming that the .text and .data offsets apply to the whole
3941 text and data segments. Convert the offsets given in the packet
3942 to base addresses for symfile_map_offsets_to_segments. */
3943 else if (data && data->num_segments == 2)
3944 {
3945 segments[0] = data->segment_bases[0] + text_addr;
3946 segments[1] = data->segment_bases[1] + data_addr;
3947 num_segments = 2;
3948 }
8d385431
DJ
3949 /* If the object file has only one segment, assume that it is text
3950 rather than data; main programs with no writable data are rare,
3951 but programs with no code are useless. Of course the code might
3952 have ended up in the data segment... to detect that we would need
3953 the permissions here. */
3954 else if (data && data->num_segments == 1)
3955 {
3956 segments[0] = data->segment_bases[0] + text_addr;
3957 num_segments = 1;
3958 }
28c32713
JB
3959 /* There's no way to relocate by segment. */
3960 else
3961 do_segments = 0;
31d99776
DJ
3962
3963 if (do_segments)
3964 {
3965 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3966 offs, num_segments, segments);
3967
3968 if (ret == 0 && !do_sections)
3e43a32a
MS
3969 error (_("Can not handle qOffsets TextSeg "
3970 "response with this symbol file"));
31d99776
DJ
3971
3972 if (ret > 0)
3973 do_sections = 0;
3974 }
c906108c 3975
9ef895d6
DJ
3976 if (data)
3977 free_symfile_segment_data (data);
31d99776
DJ
3978
3979 if (do_sections)
3980 {
3981 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3982
3e43a32a
MS
3983 /* This is a temporary kludge to force data and bss to use the
3984 same offsets because that's what nlmconv does now. The real
3985 solution requires changes to the stub and remote.c that I
3986 don't have time to do right now. */
31d99776
DJ
3987
3988 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3989 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3990 }
c906108c
SS
3991
3992 objfile_relocate (symfile_objfile, offs);
3993}
3994
9a7071a8
JB
3995/* Send interrupt_sequence to remote target. */
3996static void
eeae04df 3997send_interrupt_sequence (void)
9a7071a8 3998{
5d93a237
TT
3999 struct remote_state *rs = get_remote_state ();
4000
9a7071a8 4001 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 4002 remote_serial_write ("\x03", 1);
9a7071a8 4003 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 4004 serial_send_break (rs->remote_desc);
9a7071a8
JB
4005 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
4006 {
5d93a237 4007 serial_send_break (rs->remote_desc);
c33e31fd 4008 remote_serial_write ("g", 1);
9a7071a8
JB
4009 }
4010 else
4011 internal_error (__FILE__, __LINE__,
4012 _("Invalid value for interrupt_sequence_mode: %s."),
4013 interrupt_sequence_mode);
4014}
4015
3405876a
PA
4016
4017/* If STOP_REPLY is a T stop reply, look for the "thread" register,
4018 and extract the PTID. Returns NULL_PTID if not found. */
4019
4020static ptid_t
4021stop_reply_extract_thread (char *stop_reply)
4022{
4023 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
4024 {
256642e8 4025 const char *p;
3405876a
PA
4026
4027 /* Txx r:val ; r:val (...) */
4028 p = &stop_reply[3];
4029
4030 /* Look for "register" named "thread". */
4031 while (*p != '\0')
4032 {
256642e8 4033 const char *p1;
3405876a
PA
4034
4035 p1 = strchr (p, ':');
4036 if (p1 == NULL)
4037 return null_ptid;
4038
4039 if (strncmp (p, "thread", p1 - p) == 0)
4040 return read_ptid (++p1, &p);
4041
4042 p1 = strchr (p, ';');
4043 if (p1 == NULL)
4044 return null_ptid;
4045 p1++;
4046
4047 p = p1;
4048 }
4049 }
4050
4051 return null_ptid;
4052}
4053
b7ea362b
PA
4054/* Determine the remote side's current thread. If we have a stop
4055 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4056 "thread" register we can extract the current thread from. If not,
4057 ask the remote which is the current thread with qC. The former
4058 method avoids a roundtrip. */
4059
4060static ptid_t
4061get_current_thread (char *wait_status)
4062{
6a49a997 4063 ptid_t ptid = null_ptid;
b7ea362b
PA
4064
4065 /* Note we don't use remote_parse_stop_reply as that makes use of
4066 the target architecture, which we haven't yet fully determined at
4067 this point. */
4068 if (wait_status != NULL)
4069 ptid = stop_reply_extract_thread (wait_status);
4070 if (ptid_equal (ptid, null_ptid))
4071 ptid = remote_current_thread (inferior_ptid);
4072
4073 return ptid;
4074}
4075
49c62f2e
PA
4076/* Query the remote target for which is the current thread/process,
4077 add it to our tables, and update INFERIOR_PTID. The caller is
4078 responsible for setting the state such that the remote end is ready
3405876a
PA
4079 to return the current thread.
4080
4081 This function is called after handling the '?' or 'vRun' packets,
4082 whose response is a stop reply from which we can also try
4083 extracting the thread. If the target doesn't support the explicit
4084 qC query, we infer the current thread from that stop reply, passed
4085 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
4086
4087static void
3405876a 4088add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
4089{
4090 struct remote_state *rs = get_remote_state ();
4091 int fake_pid_p = 0;
49c62f2e
PA
4092
4093 inferior_ptid = null_ptid;
4094
b7ea362b 4095 /* Now, if we have thread information, update inferior_ptid. */
87215ad1 4096 ptid_t curr_ptid = get_current_thread (wait_status);
3405876a 4097
87215ad1 4098 if (curr_ptid != null_ptid)
49c62f2e
PA
4099 {
4100 if (!remote_multi_process_p (rs))
4101 fake_pid_p = 1;
49c62f2e
PA
4102 }
4103 else
4104 {
4105 /* Without this, some commands which require an active target
4106 (such as kill) won't work. This variable serves (at least)
4107 double duty as both the pid of the target process (if it has
4108 such), and as a flag indicating that a target is active. */
87215ad1 4109 curr_ptid = magic_null_ptid;
49c62f2e
PA
4110 fake_pid_p = 1;
4111 }
4112
87215ad1 4113 remote_add_inferior (fake_pid_p, ptid_get_pid (curr_ptid), -1, 1);
49c62f2e 4114
87215ad1
SDJ
4115 /* Add the main thread and switch to it. Don't try reading
4116 registers yet, since we haven't fetched the target description
4117 yet. */
4118 thread_info *tp = add_thread_silent (curr_ptid);
4119 switch_to_thread_no_regs (tp);
49c62f2e
PA
4120}
4121
6efcd9a8
PA
4122/* Print info about a thread that was found already stopped on
4123 connection. */
4124
4125static void
4126print_one_stopped_thread (struct thread_info *thread)
4127{
4128 struct target_waitstatus *ws = &thread->suspend.waitstatus;
4129
4130 switch_to_thread (thread->ptid);
4131 stop_pc = get_frame_pc (get_current_frame ());
4132 set_current_sal_from_frame (get_current_frame ());
4133
4134 thread->suspend.waitstatus_pending_p = 0;
4135
4136 if (ws->kind == TARGET_WAITKIND_STOPPED)
4137 {
4138 enum gdb_signal sig = ws->value.sig;
4139
4140 if (signal_print_state (sig))
76727919 4141 gdb::observers::signal_received.notify (sig);
6efcd9a8 4142 }
76727919 4143 gdb::observers::normal_stop.notify (NULL, 1);
6efcd9a8
PA
4144}
4145
221e1a37
PA
4146/* Process all initial stop replies the remote side sent in response
4147 to the ? packet. These indicate threads that were already stopped
4148 on initial connection. We mark these threads as stopped and print
4149 their current frame before giving the user the prompt. */
4150
4151static void
6efcd9a8 4152process_initial_stop_replies (int from_tty)
221e1a37
PA
4153{
4154 int pending_stop_replies = stop_reply_queue_length ();
6efcd9a8
PA
4155 struct inferior *inf;
4156 struct thread_info *thread;
4157 struct thread_info *selected = NULL;
4158 struct thread_info *lowest_stopped = NULL;
4159 struct thread_info *first = NULL;
221e1a37
PA
4160
4161 /* Consume the initial pending events. */
4162 while (pending_stop_replies-- > 0)
4163 {
4164 ptid_t waiton_ptid = minus_one_ptid;
4165 ptid_t event_ptid;
4166 struct target_waitstatus ws;
4167 int ignore_event = 0;
6efcd9a8 4168 struct thread_info *thread;
221e1a37
PA
4169
4170 memset (&ws, 0, sizeof (ws));
4171 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
4172 if (remote_debug)
4173 print_target_wait_results (waiton_ptid, event_ptid, &ws);
4174
4175 switch (ws.kind)
4176 {
4177 case TARGET_WAITKIND_IGNORE:
4178 case TARGET_WAITKIND_NO_RESUMED:
4179 case TARGET_WAITKIND_SIGNALLED:
4180 case TARGET_WAITKIND_EXITED:
4181 /* We shouldn't see these, but if we do, just ignore. */
4182 if (remote_debug)
4183 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
4184 ignore_event = 1;
4185 break;
4186
4187 case TARGET_WAITKIND_EXECD:
4188 xfree (ws.value.execd_pathname);
4189 break;
4190 default:
4191 break;
4192 }
4193
4194 if (ignore_event)
4195 continue;
4196
6efcd9a8 4197 thread = find_thread_ptid (event_ptid);
221e1a37
PA
4198
4199 if (ws.kind == TARGET_WAITKIND_STOPPED)
4200 {
4201 enum gdb_signal sig = ws.value.sig;
4202
4203 /* Stubs traditionally report SIGTRAP as initial signal,
4204 instead of signal 0. Suppress it. */
4205 if (sig == GDB_SIGNAL_TRAP)
4206 sig = GDB_SIGNAL_0;
6efcd9a8
PA
4207 thread->suspend.stop_signal = sig;
4208 ws.value.sig = sig;
4209 }
221e1a37 4210
6efcd9a8
PA
4211 thread->suspend.waitstatus = ws;
4212
4213 if (ws.kind != TARGET_WAITKIND_STOPPED
4214 || ws.value.sig != GDB_SIGNAL_0)
4215 thread->suspend.waitstatus_pending_p = 1;
4216
4217 set_executing (event_ptid, 0);
4218 set_running (event_ptid, 0);
7aabaf9d 4219 get_remote_thread_info (thread)->vcont_resumed = 0;
6efcd9a8
PA
4220 }
4221
4222 /* "Notice" the new inferiors before anything related to
4223 registers/memory. */
4224 ALL_INFERIORS (inf)
4225 {
4226 if (inf->pid == 0)
4227 continue;
4228
4229 inf->needs_setup = 1;
4230
4231 if (non_stop)
4232 {
4233 thread = any_live_thread_of_process (inf->pid);
4234 notice_new_inferior (thread->ptid,
4235 thread->state == THREAD_RUNNING,
4236 from_tty);
4237 }
4238 }
4239
4240 /* If all-stop on top of non-stop, pause all threads. Note this
4241 records the threads' stop pc, so must be done after "noticing"
4242 the inferiors. */
4243 if (!non_stop)
4244 {
4245 stop_all_threads ();
4246
4247 /* If all threads of an inferior were already stopped, we
4248 haven't setup the inferior yet. */
4249 ALL_INFERIORS (inf)
4250 {
4251 if (inf->pid == 0)
4252 continue;
221e1a37 4253
6efcd9a8
PA
4254 if (inf->needs_setup)
4255 {
4256 thread = any_live_thread_of_process (inf->pid);
4257 switch_to_thread_no_regs (thread);
4258 setup_inferior (0);
4259 }
4260 }
221e1a37 4261 }
6efcd9a8
PA
4262
4263 /* Now go over all threads that are stopped, and print their current
4264 frame. If all-stop, then if there's a signalled thread, pick
4265 that as current. */
4266 ALL_NON_EXITED_THREADS (thread)
4267 {
6efcd9a8
PA
4268 if (first == NULL)
4269 first = thread;
4270
4271 if (!non_stop)
4272 set_running (thread->ptid, 0);
4273 else if (thread->state != THREAD_STOPPED)
4274 continue;
4275
6efcd9a8
PA
4276 if (selected == NULL
4277 && thread->suspend.waitstatus_pending_p)
4278 selected = thread;
4279
5d5658a1
PA
4280 if (lowest_stopped == NULL
4281 || thread->inf->num < lowest_stopped->inf->num
4282 || thread->per_inf_num < lowest_stopped->per_inf_num)
6efcd9a8
PA
4283 lowest_stopped = thread;
4284
4285 if (non_stop)
4286 print_one_stopped_thread (thread);
4287 }
4288
4289 /* In all-stop, we only print the status of one thread, and leave
4290 others with their status pending. */
4291 if (!non_stop)
4292 {
4293 thread = selected;
4294 if (thread == NULL)
4295 thread = lowest_stopped;
4296 if (thread == NULL)
4297 thread = first;
4298
4299 print_one_stopped_thread (thread);
4300 }
4301
4302 /* For "info program". */
4303 thread = inferior_thread ();
4304 if (thread->state == THREAD_STOPPED)
4305 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
221e1a37
PA
4306}
4307
048094ac
PA
4308/* Start the remote connection and sync state. */
4309
f6ac5f3d
PA
4310void
4311remote_target::start_remote (int from_tty, int extended_p)
c906108c 4312{
c8d104ad
PA
4313 struct remote_state *rs = get_remote_state ();
4314 struct packet_config *noack_config;
2d717e4f 4315 char *wait_status = NULL;
8621d6a9 4316
048094ac
PA
4317 /* Signal other parts that we're going through the initial setup,
4318 and so things may not be stable yet. E.g., we don't try to
4319 install tracepoints until we've relocated symbols. Also, a
4320 Ctrl-C before we're connected and synced up can't interrupt the
4321 target. Instead, it offers to drop the (potentially wedged)
4322 connection. */
4323 rs->starting_up = 1;
4324
522002f9 4325 QUIT;
c906108c 4326
9a7071a8
JB
4327 if (interrupt_on_connect)
4328 send_interrupt_sequence ();
4329
57e12211 4330 /* Ack any packet which the remote side has already sent. */
048094ac 4331 remote_serial_write ("+", 1);
1e51243a 4332
c8d104ad
PA
4333 /* The first packet we send to the target is the optional "supported
4334 packets" request. If the target can answer this, it will tell us
4335 which later probes to skip. */
4336 remote_query_supported ();
4337
d914c394 4338 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 4339 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
f6ac5f3d 4340 set_permissions ();
d914c394 4341
57809e5e
JK
4342 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4343 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4344 as a reply to known packet. For packet "vFile:setfs:" it is an
4345 invalid reply and GDB would return error in
4346 remote_hostio_set_filesystem, making remote files access impossible.
4347 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4348 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4349 {
4350 const char v_mustreplyempty[] = "vMustReplyEmpty";
4351
4352 putpkt (v_mustreplyempty);
4353 getpkt (&rs->buf, &rs->buf_size, 0);
4354 if (strcmp (rs->buf, "OK") == 0)
4355 remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
4356 else if (strcmp (rs->buf, "") != 0)
4357 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
4358 rs->buf);
4359 }
4360
c8d104ad
PA
4361 /* Next, we possibly activate noack mode.
4362
4363 If the QStartNoAckMode packet configuration is set to AUTO,
4364 enable noack mode if the stub reported a wish for it with
4365 qSupported.
4366
4367 If set to TRUE, then enable noack mode even if the stub didn't
4368 report it in qSupported. If the stub doesn't reply OK, the
4369 session ends with an error.
4370
4371 If FALSE, then don't activate noack mode, regardless of what the
4372 stub claimed should be the default with qSupported. */
4373
4374 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 4375 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
4376 {
4377 putpkt ("QStartNoAckMode");
4378 getpkt (&rs->buf, &rs->buf_size, 0);
4379 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
4380 rs->noack_mode = 1;
4381 }
4382
04bd08de 4383 if (extended_p)
5fe04517
PA
4384 {
4385 /* Tell the remote that we are using the extended protocol. */
4386 putpkt ("!");
4387 getpkt (&rs->buf, &rs->buf_size, 0);
4388 }
4389
9b224c5e
PA
4390 /* Let the target know which signals it is allowed to pass down to
4391 the program. */
4392 update_signals_program_target ();
4393
d962ef82
DJ
4394 /* Next, if the target can specify a description, read it. We do
4395 this before anything involving memory or registers. */
4396 target_find_description ();
4397
6c95b8df
PA
4398 /* Next, now that we know something about the target, update the
4399 address spaces in the program spaces. */
4400 update_address_spaces ();
4401
50c71eaf
PA
4402 /* On OSs where the list of libraries is global to all
4403 processes, we fetch them early. */
f5656ead 4404 if (gdbarch_has_global_solist (target_gdbarch ()))
e696b3ad 4405 solib_add (NULL, from_tty, auto_solib_add);
50c71eaf 4406
6efcd9a8 4407 if (target_is_non_stop_p ())
74531fed 4408 {
4082afcc 4409 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
4410 error (_("Non-stop mode requested, but remote "
4411 "does not support non-stop"));
74531fed
PA
4412
4413 putpkt ("QNonStop:1");
4414 getpkt (&rs->buf, &rs->buf_size, 0);
4415
4416 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4417 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
4418
4419 /* Find about threads and processes the stub is already
4420 controlling. We default to adding them in the running state.
4421 The '?' query below will then tell us about which threads are
4422 stopped. */
f6ac5f3d 4423 this->update_thread_list ();
74531fed 4424 }
4082afcc 4425 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
4426 {
4427 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 4428 Request it explicitly. */
74531fed
PA
4429 putpkt ("QNonStop:0");
4430 getpkt (&rs->buf, &rs->buf_size, 0);
4431
4432 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4433 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
4434 }
4435
a0743c90
YQ
4436 /* Upload TSVs regardless of whether the target is running or not. The
4437 remote stub, such as GDBserver, may have some predefined or builtin
4438 TSVs, even if the target is not running. */
f6ac5f3d 4439 if (get_trace_status (current_trace_status ()) != -1)
a0743c90
YQ
4440 {
4441 struct uploaded_tsv *uploaded_tsvs = NULL;
4442
f6ac5f3d 4443 upload_trace_state_variables (&uploaded_tsvs);
a0743c90
YQ
4444 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4445 }
4446
2d717e4f
DJ
4447 /* Check whether the target is running now. */
4448 putpkt ("?");
4449 getpkt (&rs->buf, &rs->buf_size, 0);
4450
6efcd9a8 4451 if (!target_is_non_stop_p ())
2d717e4f 4452 {
74531fed 4453 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 4454 {
04bd08de 4455 if (!extended_p)
74531fed 4456 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
4457
4458 /* We're connected, but not running. Drop out before we
4459 call start_remote. */
e278ad5b 4460 rs->starting_up = 0;
c35b1492 4461 return;
2d717e4f
DJ
4462 }
4463 else
74531fed 4464 {
74531fed 4465 /* Save the reply for later. */
224c3ddb 4466 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
4467 strcpy (wait_status, rs->buf);
4468 }
4469
b7ea362b 4470 /* Fetch thread list. */
e8032dde 4471 target_update_thread_list ();
b7ea362b 4472
74531fed
PA
4473 /* Let the stub know that we want it to return the thread. */
4474 set_continue_thread (minus_one_ptid);
4475
b7ea362b
PA
4476 if (thread_count () == 0)
4477 {
4478 /* Target has no concept of threads at all. GDB treats
4479 non-threaded target as single-threaded; add a main
4480 thread. */
4481 add_current_inferior_and_thread (wait_status);
4482 }
4483 else
4484 {
4485 /* We have thread information; select the thread the target
4486 says should be current. If we're reconnecting to a
4487 multi-threaded program, this will ideally be the thread
4488 that last reported an event before GDB disconnected. */
4489 inferior_ptid = get_current_thread (wait_status);
4490 if (ptid_equal (inferior_ptid, null_ptid))
4491 {
4492 /* Odd... The target was able to list threads, but not
4493 tell us which thread was current (no "thread"
4494 register in T stop reply?). Just pick the first
4495 thread in the thread list then. */
c9f35b34
KB
4496
4497 if (remote_debug)
4498 fprintf_unfiltered (gdb_stdlog,
4499 "warning: couldn't determine remote "
4500 "current thread; picking first in list.\n");
4501
b7ea362b
PA
4502 inferior_ptid = thread_list->ptid;
4503 }
4504 }
74531fed 4505
6e586cc5
YQ
4506 /* init_wait_for_inferior should be called before get_offsets in order
4507 to manage `inserted' flag in bp loc in a correct state.
4508 breakpoint_init_inferior, called from init_wait_for_inferior, set
4509 `inserted' flag to 0, while before breakpoint_re_set, called from
4510 start_remote, set `inserted' flag to 1. In the initialization of
4511 inferior, breakpoint_init_inferior should be called first, and then
4512 breakpoint_re_set can be called. If this order is broken, state of
4513 `inserted' flag is wrong, and cause some problems on breakpoint
4514 manipulation. */
4515 init_wait_for_inferior ();
4516
74531fed
PA
4517 get_offsets (); /* Get text, data & bss offsets. */
4518
d962ef82
DJ
4519 /* If we could not find a description using qXfer, and we know
4520 how to do it some other way, try again. This is not
4521 supported for non-stop; it could be, but it is tricky if
4522 there are no stopped threads when we connect. */
f6ac5f3d 4523 if (remote_read_description_p (this)
f5656ead 4524 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
4525 {
4526 target_clear_description ();
4527 target_find_description ();
4528 }
4529
74531fed
PA
4530 /* Use the previously fetched status. */
4531 gdb_assert (wait_status != NULL);
4532 strcpy (rs->buf, wait_status);
4533 rs->cached_wait_status = 1;
4534
f6ac5f3d 4535 ::start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
4536 }
4537 else
4538 {
68c97600
PA
4539 /* Clear WFI global state. Do this before finding about new
4540 threads and inferiors, and setting the current inferior.
4541 Otherwise we would clear the proceed status of the current
4542 inferior when we want its stop_soon state to be preserved
4543 (see notice_new_inferior). */
4544 init_wait_for_inferior ();
4545
74531fed
PA
4546 /* In non-stop, we will either get an "OK", meaning that there
4547 are no stopped threads at this time; or, a regular stop
4548 reply. In the latter case, there may be more than one thread
4549 stopped --- we pull them all out using the vStopped
4550 mechanism. */
4551 if (strcmp (rs->buf, "OK") != 0)
4552 {
722247f1 4553 struct notif_client *notif = &notif_client_stop;
2d717e4f 4554
722247f1
YQ
4555 /* remote_notif_get_pending_replies acks this one, and gets
4556 the rest out. */
f48ff2a7 4557 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
4558 = remote_notif_parse (notif, rs->buf);
4559 remote_notif_get_pending_events (notif);
74531fed 4560 }
2d717e4f 4561
74531fed
PA
4562 if (thread_count () == 0)
4563 {
04bd08de 4564 if (!extended_p)
74531fed 4565 error (_("The target is not running (try extended-remote?)"));
82f73884 4566
c35b1492
PA
4567 /* We're connected, but not running. Drop out before we
4568 call start_remote. */
e278ad5b 4569 rs->starting_up = 0;
c35b1492
PA
4570 return;
4571 }
74531fed 4572
74531fed
PA
4573 /* In non-stop mode, any cached wait status will be stored in
4574 the stop reply queue. */
4575 gdb_assert (wait_status == NULL);
f0223081 4576
2455069d 4577 /* Report all signals during attach/startup. */
f6ac5f3d 4578 pass_signals (0, NULL);
221e1a37
PA
4579
4580 /* If there are already stopped threads, mark them stopped and
4581 report their stops before giving the prompt to the user. */
6efcd9a8 4582 process_initial_stop_replies (from_tty);
221e1a37
PA
4583
4584 if (target_can_async_p ())
4585 target_async (1);
74531fed 4586 }
c8d104ad 4587
c8d104ad
PA
4588 /* If we connected to a live target, do some additional setup. */
4589 if (target_has_execution)
4590 {
f4ccffad 4591 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 4592 remote_check_symbols ();
c8d104ad 4593 }
50c71eaf 4594
d5551862
SS
4595 /* Possibly the target has been engaged in a trace run started
4596 previously; find out where things are at. */
f6ac5f3d 4597 if (get_trace_status (current_trace_status ()) != -1)
d5551862 4598 {
00bf0b85 4599 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4600
00bf0b85
SS
4601 if (current_trace_status ()->running)
4602 printf_filtered (_("Trace is already running on the target.\n"));
4603
f6ac5f3d 4604 upload_tracepoints (&uploaded_tps);
00bf0b85
SS
4605
4606 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4607 }
4608
c0272db5
TW
4609 /* Possibly the target has been engaged in a btrace record started
4610 previously; find out where things are at. */
4611 remote_btrace_maybe_reopen ();
4612
1e51243a
PA
4613 /* The thread and inferior lists are now synchronized with the
4614 target, our symbols have been relocated, and we're merged the
4615 target's tracepoints with ours. We're done with basic start
4616 up. */
4617 rs->starting_up = 0;
4618
a25a5a45
PA
4619 /* Maybe breakpoints are global and need to be inserted now. */
4620 if (breakpoints_should_be_inserted_now ())
50c71eaf 4621 insert_breakpoints ();
c906108c
SS
4622}
4623
4624/* Open a connection to a remote debugger.
4625 NAME is the filename used for communication. */
4626
f6ac5f3d
PA
4627void
4628remote_target::open (const char *name, int from_tty)
c906108c 4629{
f6ac5f3d 4630 open_1 (name, from_tty, 0);
43ff13b4
JM
4631}
4632
c906108c
SS
4633/* Open a connection to a remote debugger using the extended
4634 remote gdb protocol. NAME is the filename used for communication. */
4635
f6ac5f3d
PA
4636void
4637extended_remote_target::open (const char *name, int from_tty)
c906108c 4638{
f6ac5f3d 4639 open_1 (name, from_tty, 1 /*extended_p */);
43ff13b4
JM
4640}
4641
ca4f7f8b
PA
4642/* Reset all packets back to "unknown support". Called when opening a
4643 new connection to a remote target. */
c906108c 4644
d471ea57 4645static void
ca4f7f8b 4646reset_all_packet_configs_support (void)
d471ea57
AC
4647{
4648 int i;
a744cf53 4649
444abaca 4650 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4651 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4652}
4653
ca4f7f8b
PA
4654/* Initialize all packet configs. */
4655
4656static void
4657init_all_packet_configs (void)
4658{
4659 int i;
4660
4661 for (i = 0; i < PACKET_MAX; i++)
4662 {
4663 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4664 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4665 }
4666}
4667
23860348 4668/* Symbol look-up. */
dc8acb97
MS
4669
4670static void
36d25514 4671remote_check_symbols (void)
dc8acb97
MS
4672{
4673 char *msg, *reply, *tmp;
dc8acb97 4674 int end;
28170b88 4675 long reply_size;
a5c0808e 4676 struct cleanup *old_chain;
dc8acb97 4677
63154eca
PA
4678 /* The remote side has no concept of inferiors that aren't running
4679 yet, it only knows about running processes. If we're connected
4680 but our current inferior is not running, we should not invite the
4681 remote target to request symbol lookups related to its
4682 (unrelated) current process. */
4683 if (!target_has_execution)
4684 return;
4685
4082afcc 4686 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4687 return;
4688
63154eca
PA
4689 /* Make sure the remote is pointing at the right process. Note
4690 there's no way to select "no process". */
3c9c4b83
PA
4691 set_general_process ();
4692
6d820c5c
DJ
4693 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4694 because we need both at the same time. */
224c3ddb 4695 msg = (char *) xmalloc (get_remote_packet_size ());
a5c0808e 4696 old_chain = make_cleanup (xfree, msg);
28170b88
MK
4697 reply = (char *) xmalloc (get_remote_packet_size ());
4698 make_cleanup (free_current_contents, &reply);
4699 reply_size = get_remote_packet_size ();
6d820c5c 4700
23860348 4701 /* Invite target to request symbol lookups. */
dc8acb97
MS
4702
4703 putpkt ("qSymbol::");
28170b88
MK
4704 getpkt (&reply, &reply_size, 0);
4705 packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97 4706
61012eef 4707 while (startswith (reply, "qSymbol:"))
dc8acb97 4708 {
77e371c0
TT
4709 struct bound_minimal_symbol sym;
4710
dc8acb97 4711 tmp = &reply[8];
cfd77fa1 4712 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4713 msg[end] = '\0';
4714 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4715 if (sym.minsym == NULL)
ea9c271d 4716 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4717 else
2bbe3cc1 4718 {
f5656ead 4719 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4720 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4721
4722 /* If this is a function address, return the start of code
4723 instead of any data function descriptor. */
f5656ead 4724 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1 4725 sym_addr,
f6ac5f3d 4726 target_stack);
2bbe3cc1
DJ
4727
4728 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4729 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4730 }
4731
dc8acb97 4732 putpkt (msg);
28170b88 4733 getpkt (&reply, &reply_size, 0);
dc8acb97 4734 }
a5c0808e
PA
4735
4736 do_cleanups (old_chain);
dc8acb97
MS
4737}
4738
9db8d71f 4739static struct serial *
baa336ce 4740remote_serial_open (const char *name)
9db8d71f
DJ
4741{
4742 static int udp_warning = 0;
4743
4744 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4745 of in ser-tcp.c, because it is the remote protocol assuming that the
4746 serial connection is reliable and not the serial connection promising
4747 to be. */
61012eef 4748 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4749 {
3e43a32a
MS
4750 warning (_("The remote protocol may be unreliable over UDP.\n"
4751 "Some events may be lost, rendering further debugging "
4752 "impossible."));
9db8d71f
DJ
4753 udp_warning = 1;
4754 }
4755
4756 return serial_open (name);
4757}
4758
d914c394
SS
4759/* Inform the target of our permission settings. The permission flags
4760 work without this, but if the target knows the settings, it can do
4761 a couple things. First, it can add its own check, to catch cases
4762 that somehow manage to get by the permissions checks in target
4763 methods. Second, if the target is wired to disallow particular
4764 settings (for instance, a system in the field that is not set up to
4765 be able to stop at a breakpoint), it can object to any unavailable
4766 permissions. */
4767
4768void
f6ac5f3d 4769remote_target::set_permissions ()
d914c394
SS
4770{
4771 struct remote_state *rs = get_remote_state ();
4772
bba74b36
YQ
4773 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4774 "WriteReg:%x;WriteMem:%x;"
4775 "InsertBreak:%x;InsertTrace:%x;"
4776 "InsertFastTrace:%x;Stop:%x",
4777 may_write_registers, may_write_memory,
4778 may_insert_breakpoints, may_insert_tracepoints,
4779 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4780 putpkt (rs->buf);
4781 getpkt (&rs->buf, &rs->buf_size, 0);
4782
4783 /* If the target didn't like the packet, warn the user. Do not try
4784 to undo the user's settings, that would just be maddening. */
4785 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4786 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4787}
4788
be2a5f71
DJ
4789/* This type describes each known response to the qSupported
4790 packet. */
4791struct protocol_feature
4792{
4793 /* The name of this protocol feature. */
4794 const char *name;
4795
4796 /* The default for this protocol feature. */
4797 enum packet_support default_support;
4798
4799 /* The function to call when this feature is reported, or after
4800 qSupported processing if the feature is not supported.
4801 The first argument points to this structure. The second
4802 argument indicates whether the packet requested support be
4803 enabled, disabled, or probed (or the default, if this function
4804 is being called at the end of processing and this feature was
4805 not reported). The third argument may be NULL; if not NULL, it
4806 is a NUL-terminated string taken from the packet following
4807 this feature's name and an equals sign. */
4808 void (*func) (const struct protocol_feature *, enum packet_support,
4809 const char *);
4810
4811 /* The corresponding packet for this feature. Only used if
4812 FUNC is remote_supported_packet. */
4813 int packet;
4814};
4815
be2a5f71
DJ
4816static void
4817remote_supported_packet (const struct protocol_feature *feature,
4818 enum packet_support support,
4819 const char *argument)
4820{
4821 if (argument)
4822 {
4823 warning (_("Remote qSupported response supplied an unexpected value for"
4824 " \"%s\"."), feature->name);
4825 return;
4826 }
4827
4082afcc 4828 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4829}
be2a5f71
DJ
4830
4831static void
4832remote_packet_size (const struct protocol_feature *feature,
4833 enum packet_support support, const char *value)
4834{
4835 struct remote_state *rs = get_remote_state ();
4836
4837 int packet_size;
4838 char *value_end;
4839
4840 if (support != PACKET_ENABLE)
4841 return;
4842
4843 if (value == NULL || *value == '\0')
4844 {
4845 warning (_("Remote target reported \"%s\" without a size."),
4846 feature->name);
4847 return;
4848 }
4849
4850 errno = 0;
4851 packet_size = strtol (value, &value_end, 16);
4852 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4853 {
4854 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4855 feature->name, value);
4856 return;
4857 }
4858
be2a5f71
DJ
4859 /* Record the new maximum packet size. */
4860 rs->explicit_packet_size = packet_size;
4861}
4862
dc473cfb 4863static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4864 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4865 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4866 PACKET_qXfer_auxv },
c78fa86a
GB
4867 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4868 PACKET_qXfer_exec_file },
23181151
DJ
4869 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4870 PACKET_qXfer_features },
cfa9d6d9
DJ
4871 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4872 PACKET_qXfer_libraries },
2268b414
JK
4873 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4874 PACKET_qXfer_libraries_svr4 },
ced63ec0 4875 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4876 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4877 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4878 PACKET_qXfer_memory_map },
4de6483e
UW
4879 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4880 PACKET_qXfer_spu_read },
4881 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4882 PACKET_qXfer_spu_write },
07e059b5
VP
4883 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4884 PACKET_qXfer_osdata },
dc146f7c
VP
4885 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4886 PACKET_qXfer_threads },
b3b9301e
PA
4887 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4888 PACKET_qXfer_traceframe_info },
89be2091
DJ
4889 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4890 PACKET_QPassSignals },
82075af2
JS
4891 { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
4892 PACKET_QCatchSyscalls },
9b224c5e
PA
4893 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4894 PACKET_QProgramSignals },
bc3b087d
SDJ
4895 { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
4896 PACKET_QSetWorkingDir },
aefd8b33
SDJ
4897 { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
4898 PACKET_QStartupWithShell },
0a2dde4a
SDJ
4899 { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
4900 PACKET_QEnvironmentHexEncoded },
4901 { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
4902 PACKET_QEnvironmentReset },
4903 { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
4904 PACKET_QEnvironmentUnset },
a6f3e723
SL
4905 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4906 PACKET_QStartNoAckMode },
4082afcc
PA
4907 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4908 PACKET_multiprocess_feature },
4909 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4910 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4911 PACKET_qXfer_siginfo_read },
4912 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4913 PACKET_qXfer_siginfo_write },
4082afcc 4914 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4915 PACKET_ConditionalTracepoints },
4082afcc 4916 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4917 PACKET_ConditionalBreakpoints },
4082afcc 4918 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4919 PACKET_BreakpointCommands },
4082afcc 4920 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4921 PACKET_FastTracepoints },
4082afcc 4922 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4923 PACKET_StaticTracepoints },
4082afcc 4924 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4925 PACKET_InstallInTrace},
4082afcc
PA
4926 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4927 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4928 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4929 PACKET_bc },
4930 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4931 PACKET_bs },
409873ef
SS
4932 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4933 PACKET_TracepointSource },
d914c394
SS
4934 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4935 PACKET_QAllow },
4082afcc
PA
4936 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4937 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4938 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4939 PACKET_qXfer_fdpic },
169081d0
TG
4940 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4941 PACKET_qXfer_uib },
03583c20
UW
4942 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4943 PACKET_QDisableRandomization },
d1feda86 4944 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4945 { "QTBuffer:size", PACKET_DISABLE,
4946 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4947 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4948 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4949 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4950 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4951 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4952 PACKET_qXfer_btrace },
4953 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4954 PACKET_qXfer_btrace_conf },
4955 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4956 PACKET_Qbtrace_conf_bts_size },
4957 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4958 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4959 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4960 PACKET_fork_event_feature },
4961 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4962 PACKET_vfork_event_feature },
94585166
DB
4963 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4964 PACKET_exec_event_feature },
b20a6524 4965 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
750ce8d1 4966 PACKET_Qbtrace_conf_pt_size },
65706a29
PA
4967 { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
4968 { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
f2faf941 4969 { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
be2a5f71
DJ
4970};
4971
c8d5aac9
L
4972static char *remote_support_xml;
4973
4974/* Register string appended to "xmlRegisters=" in qSupported query. */
4975
4976void
6e39997a 4977register_remote_support_xml (const char *xml)
c8d5aac9
L
4978{
4979#if defined(HAVE_LIBEXPAT)
4980 if (remote_support_xml == NULL)
c4f7c687 4981 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4982 else
4983 {
4984 char *copy = xstrdup (remote_support_xml + 13);
4985 char *p = strtok (copy, ",");
4986
4987 do
4988 {
4989 if (strcmp (p, xml) == 0)
4990 {
4991 /* already there */
4992 xfree (copy);
4993 return;
4994 }
4995 }
4996 while ((p = strtok (NULL, ",")) != NULL);
4997 xfree (copy);
4998
94b0dee1
PA
4999 remote_support_xml = reconcat (remote_support_xml,
5000 remote_support_xml, ",", xml,
5001 (char *) NULL);
c8d5aac9
L
5002 }
5003#endif
5004}
5005
69b6ecb0
TT
5006static void
5007remote_query_supported_append (std::string *msg, const char *append)
c8d5aac9 5008{
69b6ecb0
TT
5009 if (!msg->empty ())
5010 msg->append (";");
5011 msg->append (append);
c8d5aac9
L
5012}
5013
be2a5f71
DJ
5014static void
5015remote_query_supported (void)
5016{
5017 struct remote_state *rs = get_remote_state ();
5018 char *next;
5019 int i;
5020 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
5021
5022 /* The packet support flags are handled differently for this packet
5023 than for most others. We treat an error, a disabled packet, and
5024 an empty response identically: any features which must be reported
5025 to be used will be automatically disabled. An empty buffer
5026 accomplishes this, since that is also the representation for a list
5027 containing no features. */
5028
5029 rs->buf[0] = 0;
4082afcc 5030 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 5031 {
69b6ecb0 5032 std::string q;
c8d5aac9 5033
73b8c1fd 5034 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5035 remote_query_supported_append (&q, "multiprocess+");
c8d5aac9 5036
f7e6eed5 5037 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5038 remote_query_supported_append (&q, "swbreak+");
f7e6eed5 5039 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5040 remote_query_supported_append (&q, "hwbreak+");
f7e6eed5 5041
69b6ecb0 5042 remote_query_supported_append (&q, "qRelocInsn+");
dde08ee1 5043
8020350c
DB
5044 if (packet_set_cmd_state (PACKET_fork_event_feature)
5045 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5046 remote_query_supported_append (&q, "fork-events+");
8020350c
DB
5047 if (packet_set_cmd_state (PACKET_vfork_event_feature)
5048 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5049 remote_query_supported_append (&q, "vfork-events+");
8020350c
DB
5050 if (packet_set_cmd_state (PACKET_exec_event_feature)
5051 != AUTO_BOOLEAN_FALSE)
69b6ecb0 5052 remote_query_supported_append (&q, "exec-events+");
89245bc0 5053
750ce8d1 5054 if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5055 remote_query_supported_append (&q, "vContSupported+");
750ce8d1 5056
65706a29 5057 if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5058 remote_query_supported_append (&q, "QThreadEvents+");
65706a29 5059
f2faf941 5060 if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
69b6ecb0 5061 remote_query_supported_append (&q, "no-resumed+");
f2faf941 5062
b35d5edb
PA
5063 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5064 the qSupported:xmlRegisters=i386 handling. */
7cc244de
PA
5065 if (remote_support_xml != NULL
5066 && packet_support (PACKET_qXfer_features) != PACKET_DISABLE)
69b6ecb0 5067 remote_query_supported_append (&q, remote_support_xml);
82f73884 5068
69b6ecb0
TT
5069 q = "qSupported:" + q;
5070 putpkt (q.c_str ());
94b0dee1 5071
be2a5f71
DJ
5072 getpkt (&rs->buf, &rs->buf_size, 0);
5073
5074 /* If an error occured, warn, but do not return - just reset the
5075 buffer to empty and go on to disable features. */
5076 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
5077 == PACKET_ERROR)
5078 {
5079 warning (_("Remote failure reply: %s"), rs->buf);
5080 rs->buf[0] = 0;
5081 }
5082 }
5083
5084 memset (seen, 0, sizeof (seen));
5085
5086 next = rs->buf;
5087 while (*next)
5088 {
5089 enum packet_support is_supported;
5090 char *p, *end, *name_end, *value;
5091
5092 /* First separate out this item from the rest of the packet. If
5093 there's another item after this, we overwrite the separator
5094 (terminated strings are much easier to work with). */
5095 p = next;
5096 end = strchr (p, ';');
5097 if (end == NULL)
5098 {
5099 end = p + strlen (p);
5100 next = end;
5101 }
5102 else
5103 {
89be2091
DJ
5104 *end = '\0';
5105 next = end + 1;
5106
be2a5f71
DJ
5107 if (end == p)
5108 {
5109 warning (_("empty item in \"qSupported\" response"));
5110 continue;
5111 }
be2a5f71
DJ
5112 }
5113
5114 name_end = strchr (p, '=');
5115 if (name_end)
5116 {
5117 /* This is a name=value entry. */
5118 is_supported = PACKET_ENABLE;
5119 value = name_end + 1;
5120 *name_end = '\0';
5121 }
5122 else
5123 {
5124 value = NULL;
5125 switch (end[-1])
5126 {
5127 case '+':
5128 is_supported = PACKET_ENABLE;
5129 break;
5130
5131 case '-':
5132 is_supported = PACKET_DISABLE;
5133 break;
5134
5135 case '?':
5136 is_supported = PACKET_SUPPORT_UNKNOWN;
5137 break;
5138
5139 default:
3e43a32a
MS
5140 warning (_("unrecognized item \"%s\" "
5141 "in \"qSupported\" response"), p);
be2a5f71
DJ
5142 continue;
5143 }
5144 end[-1] = '\0';
5145 }
5146
5147 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
5148 if (strcmp (remote_protocol_features[i].name, p) == 0)
5149 {
5150 const struct protocol_feature *feature;
5151
5152 seen[i] = 1;
5153 feature = &remote_protocol_features[i];
5154 feature->func (feature, is_supported, value);
5155 break;
5156 }
5157 }
5158
5159 /* If we increased the packet size, make sure to increase the global
5160 buffer size also. We delay this until after parsing the entire
5161 qSupported packet, because this is the same buffer we were
5162 parsing. */
5163 if (rs->buf_size < rs->explicit_packet_size)
5164 {
5165 rs->buf_size = rs->explicit_packet_size;
224c3ddb 5166 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
be2a5f71
DJ
5167 }
5168
5169 /* Handle the defaults for unmentioned features. */
5170 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
5171 if (!seen[i])
5172 {
5173 const struct protocol_feature *feature;
5174
5175 feature = &remote_protocol_features[i];
5176 feature->func (feature, feature->default_support, NULL);
5177 }
5178}
5179
048094ac
PA
5180/* Serial QUIT handler for the remote serial descriptor.
5181
5182 Defers handling a Ctrl-C until we're done with the current
5183 command/response packet sequence, unless:
5184
5185 - We're setting up the connection. Don't send a remote interrupt
5186 request, as we're not fully synced yet. Quit immediately
5187 instead.
5188
5189 - The target has been resumed in the foreground
223ffa71 5190 (target_terminal::is_ours is false) with a synchronous resume
048094ac
PA
5191 packet, and we're blocked waiting for the stop reply, thus a
5192 Ctrl-C should be immediately sent to the target.
5193
5194 - We get a second Ctrl-C while still within the same serial read or
5195 write. In that case the serial is seemingly wedged --- offer to
5196 quit/disconnect.
5197
5198 - We see a second Ctrl-C without target response, after having
5199 previously interrupted the target. In that case the target/stub
5200 is probably wedged --- offer to quit/disconnect.
5201*/
5202
5203static void
5204remote_serial_quit_handler (void)
5205{
5206 struct remote_state *rs = get_remote_state ();
5207
5208 if (check_quit_flag ())
5209 {
5210 /* If we're starting up, we're not fully synced yet. Quit
5211 immediately. */
5212 if (rs->starting_up)
5213 quit ();
5214 else if (rs->got_ctrlc_during_io)
5215 {
5216 if (query (_("The target is not responding to GDB commands.\n"
5217 "Stop debugging it? ")))
5218 remote_unpush_and_throw ();
5219 }
5220 /* If ^C has already been sent once, offer to disconnect. */
223ffa71 5221 else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
048094ac
PA
5222 interrupt_query ();
5223 /* All-stop protocol, and blocked waiting for stop reply. Send
5224 an interrupt request. */
223ffa71 5225 else if (!target_terminal::is_ours () && rs->waiting_for_stop_reply)
e671cd59 5226 target_interrupt ();
048094ac
PA
5227 else
5228 rs->got_ctrlc_during_io = 1;
5229 }
5230}
5231
78a095c3
JK
5232/* Remove any of the remote.c targets from target stack. Upper targets depend
5233 on it so remove them first. */
5234
5235static void
5236remote_unpush_target (void)
5237{
915ef8b1 5238 pop_all_targets_at_and_above (process_stratum);
78a095c3 5239}
be2a5f71 5240
048094ac
PA
5241static void
5242remote_unpush_and_throw (void)
5243{
5244 remote_unpush_target ();
5245 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
5246}
5247
f6ac5f3d
PA
5248void
5249remote_target::open_1 (const char *name, int from_tty, int extended_p)
c906108c 5250{
d01949b6 5251 struct remote_state *rs = get_remote_state ();
a6f3e723 5252
c906108c 5253 if (name == 0)
8a3fe4f8 5254 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 5255 "serial device is attached to the remote system\n"
8a3fe4f8 5256 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 5257
23860348 5258 /* See FIXME above. */
c6ebd6cf 5259 if (!target_async_permitted)
92d1e331 5260 wait_forever_enabled_p = 1;
6426a772 5261
2d717e4f 5262 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
5263 Ask this question first, before target_preopen has a chance to kill
5264 anything. */
5d93a237 5265 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 5266 {
78a095c3
JK
5267 if (from_tty
5268 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
5269 error (_("Still connected."));
5270 }
5271
78a095c3 5272 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
5273 target_preopen (from_tty);
5274
89be2091 5275 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
5276 xfree (rs->last_pass_packet);
5277 rs->last_pass_packet = NULL;
89be2091 5278
9b224c5e
PA
5279 /* Make sure we send the program signals list the next time we
5280 resume. */
5e4a05c4
TT
5281 xfree (rs->last_program_signals_packet);
5282 rs->last_program_signals_packet = NULL;
9b224c5e 5283
ad9a8f3f 5284 remote_fileio_reset ();
1dd41f16
NS
5285 reopen_exec_file ();
5286 reread_symbols ();
5287
5d93a237
TT
5288 rs->remote_desc = remote_serial_open (name);
5289 if (!rs->remote_desc)
c906108c
SS
5290 perror_with_name (name);
5291
5292 if (baud_rate != -1)
5293 {
5d93a237 5294 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 5295 {
9b74d5d3
KB
5296 /* The requested speed could not be set. Error out to
5297 top level after closing remote_desc. Take care to
5298 set remote_desc to NULL to avoid closing remote_desc
5299 more than once. */
5d93a237
TT
5300 serial_close (rs->remote_desc);
5301 rs->remote_desc = NULL;
c906108c
SS
5302 perror_with_name (name);
5303 }
5304 }
5305
236af5e3 5306 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 5307 serial_raw (rs->remote_desc);
c906108c
SS
5308
5309 /* If there is something sitting in the buffer we might take it as a
5310 response to a command, which would be bad. */
5d93a237 5311 serial_flush_input (rs->remote_desc);
c906108c
SS
5312
5313 if (from_tty)
5314 {
5315 puts_filtered ("Remote debugging using ");
5316 puts_filtered (name);
5317 puts_filtered ("\n");
5318 }
d9f719f1
PA
5319
5320 remote_target *target
5321 = extended_p ? &extended_remote_ops : &remote_ops;
5322 push_target (target); /* Switch to using remote target now. */
c906108c 5323
74531fed
PA
5324 /* Register extra event sources in the event loop. */
5325 remote_async_inferior_event_token
5326 = create_async_event_handler (remote_async_inferior_event_handler,
5327 NULL);
5965e028 5328 rs->notif_state = remote_notif_state_allocate ();
74531fed 5329
be2a5f71
DJ
5330 /* Reset the target state; these things will be queried either by
5331 remote_query_supported or as they are needed. */
ca4f7f8b 5332 reset_all_packet_configs_support ();
74531fed 5333 rs->cached_wait_status = 0;
be2a5f71 5334 rs->explicit_packet_size = 0;
a6f3e723 5335 rs->noack_mode = 0;
82f73884 5336 rs->extended = extended_p;
e24a49d8 5337 rs->waiting_for_stop_reply = 0;
3a29589a 5338 rs->ctrlc_pending_p = 0;
048094ac 5339 rs->got_ctrlc_during_io = 0;
802188a7 5340
47f8a51d
TT
5341 rs->general_thread = not_sent_ptid;
5342 rs->continue_thread = not_sent_ptid;
262e1174 5343 rs->remote_traceframe_number = -1;
c906108c 5344
3a00c802
PA
5345 rs->last_resume_exec_dir = EXEC_FORWARD;
5346
9d1f7ab2 5347 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
5348 rs->use_threadinfo_query = 1;
5349 rs->use_threadextra_query = 1;
9d1f7ab2 5350
dd194f6b 5351 rs->readahead_cache.invalidate ();
80152258 5352
c6ebd6cf 5353 if (target_async_permitted)
92d1e331 5354 {
92d1e331
DJ
5355 /* FIXME: cagney/1999-09-23: During the initial connection it is
5356 assumed that the target is already ready and able to respond to
0df8b418 5357 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 5358 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 5359 around this. Eventually a mechanism that allows
92d1e331 5360 wait_for_inferior() to expect/get timeouts will be
23860348 5361 implemented. */
92d1e331
DJ
5362 wait_forever_enabled_p = 0;
5363 }
5364
23860348 5365 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 5366 no_shared_libraries (NULL, 0);
f78f6cf1 5367
74531fed
PA
5368 /* Start afresh. */
5369 init_thread_list ();
5370
36918e70 5371 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
5372 target (we'd otherwise be in an inconsistent state) and then
5373 propogate the error on up the exception chain. This ensures that
5374 the caller doesn't stumble along blindly assuming that the
5375 function succeeded. The CLI doesn't have this problem but other
5376 UI's, such as MI do.
36918e70
AC
5377
5378 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5379 this function should return an error indication letting the
ce2826aa 5380 caller restore the previous state. Unfortunately the command
36918e70
AC
5381 ``target remote'' is directly wired to this function making that
5382 impossible. On a positive note, the CLI side of this problem has
5383 been fixed - the function set_cmd_context() makes it possible for
5384 all the ``target ....'' commands to share a common callback
5385 function. See cli-dump.c. */
109c3e39 5386 {
2d717e4f 5387
492d29ea 5388 TRY
04bd08de 5389 {
d9f719f1 5390 target->start_remote (from_tty, extended_p);
04bd08de 5391 }
492d29ea 5392 CATCH (ex, RETURN_MASK_ALL)
109c3e39 5393 {
c8d104ad
PA
5394 /* Pop the partially set up target - unless something else did
5395 already before throwing the exception. */
5d93a237 5396 if (rs->remote_desc != NULL)
78a095c3 5397 remote_unpush_target ();
c6ebd6cf 5398 if (target_async_permitted)
109c3e39
AC
5399 wait_forever_enabled_p = 1;
5400 throw_exception (ex);
5401 }
492d29ea 5402 END_CATCH
109c3e39 5403 }
c906108c 5404
f4abbc16
MM
5405 remote_btrace_reset ();
5406
c6ebd6cf 5407 if (target_async_permitted)
92d1e331 5408 wait_forever_enabled_p = 1;
43ff13b4
JM
5409}
5410
de0d863e
DB
5411/* Detach the specified process. */
5412
5413static void
5414remote_detach_pid (int pid)
5415{
5416 struct remote_state *rs = get_remote_state ();
5417
5418 if (remote_multi_process_p (rs))
5419 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
5420 else
5421 strcpy (rs->buf, "D");
5422
5423 putpkt (rs->buf);
5424 getpkt (&rs->buf, &rs->buf_size, 0);
5425
5426 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
5427 ;
5428 else if (rs->buf[0] == '\0')
5429 error (_("Remote doesn't know how to detach"));
5430 else
5431 error (_("Can't detach process."));
5432}
5433
5434/* This detaches a program to which we previously attached, using
5435 inferior_ptid to identify the process. After this is done, GDB
5436 can be used to debug some other program. We better not have left
5437 any breakpoints in the target program or it'll die when it hits
5438 one. */
c906108c
SS
5439
5440static void
6e1e1966 5441remote_detach_1 (int from_tty, inferior *inf)
c906108c 5442{
82f73884 5443 int pid = ptid_get_pid (inferior_ptid);
d01949b6 5444 struct remote_state *rs = get_remote_state ();
de0d863e
DB
5445 struct thread_info *tp = find_thread_ptid (inferior_ptid);
5446 int is_fork_parent;
c906108c 5447
2d717e4f
DJ
5448 if (!target_has_execution)
5449 error (_("No process to detach from."));
5450
0f48b757 5451 target_announce_detach (from_tty);
7cee1e54 5452
c906108c 5453 /* Tell the remote target to detach. */
de0d863e 5454 remote_detach_pid (pid);
82f73884 5455
8020350c
DB
5456 /* Exit only if this is the only active inferior. */
5457 if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
7cee1e54 5458 puts_filtered (_("Ending remote debugging.\n"));
82f73884 5459
de0d863e
DB
5460 /* Check to see if we are detaching a fork parent. Note that if we
5461 are detaching a fork child, tp == NULL. */
5462 is_fork_parent = (tp != NULL
5463 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
5464
5465 /* If doing detach-on-fork, we don't mourn, because that will delete
5466 breakpoints that should be available for the followed inferior. */
5467 if (!is_fork_parent)
f67c0c91 5468 {
249b5733
PA
5469 /* Save the pid as a string before mourning, since that will
5470 unpush the remote target, and we need the string after. */
5471 std::string infpid = target_pid_to_str (pid_to_ptid (pid));
f67c0c91
SDJ
5472
5473 target_mourn_inferior (inferior_ptid);
5474 if (print_inferior_events)
5475 printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
5476 inf->num, infpid.c_str ());
5477 }
de0d863e
DB
5478 else
5479 {
5480 inferior_ptid = null_ptid;
5481 detach_inferior (pid);
5482 }
2d717e4f
DJ
5483}
5484
f6ac5f3d
PA
5485void
5486remote_target::detach (inferior *inf, int from_tty)
2d717e4f 5487{
6e1e1966 5488 remote_detach_1 (from_tty, inf);
2d717e4f
DJ
5489}
5490
f6ac5f3d
PA
5491void
5492extended_remote_target::detach (inferior *inf, int from_tty)
2d717e4f 5493{
6e1e1966 5494 remote_detach_1 (from_tty, inf);
de0d863e
DB
5495}
5496
5497/* Target follow-fork function for remote targets. On entry, and
5498 at return, the current inferior is the fork parent.
5499
5500 Note that although this is currently only used for extended-remote,
5501 it is named remote_follow_fork in anticipation of using it for the
5502 remote target as well. */
5503
f6ac5f3d
PA
5504int
5505remote_target::follow_fork (int follow_child, int detach_fork)
de0d863e
DB
5506{
5507 struct remote_state *rs = get_remote_state ();
c269dbdb 5508 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 5509
c269dbdb
DB
5510 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
5511 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
5512 {
5513 /* When following the parent and detaching the child, we detach
5514 the child here. For the case of following the child and
5515 detaching the parent, the detach is done in the target-
5516 independent follow fork code in infrun.c. We can't use
5517 target_detach when detaching an unfollowed child because
5518 the client side doesn't know anything about the child. */
5519 if (detach_fork && !follow_child)
5520 {
5521 /* Detach the fork child. */
5522 ptid_t child_ptid;
5523 pid_t child_pid;
5524
5525 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
5526 child_pid = ptid_get_pid (child_ptid);
5527
5528 remote_detach_pid (child_pid);
de0d863e
DB
5529 }
5530 }
5531 return 0;
c906108c
SS
5532}
5533
94585166
DB
5534/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
5535 in the program space of the new inferior. On entry and at return the
5536 current inferior is the exec'ing inferior. INF is the new exec'd
5537 inferior, which may be the same as the exec'ing inferior unless
5538 follow-exec-mode is "new". */
5539
f6ac5f3d
PA
5540void
5541remote_target::follow_exec (struct inferior *inf, char *execd_pathname)
94585166
DB
5542{
5543 /* We know that this is a target file name, so if it has the "target:"
5544 prefix we strip it off before saving it in the program space. */
5545 if (is_target_filename (execd_pathname))
5546 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
5547
5548 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
5549}
5550
6ad8ae5c
DJ
5551/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
5552
f6ac5f3d
PA
5553void
5554remote_target::disconnect (const char *args, int from_tty)
43ff13b4 5555{
43ff13b4 5556 if (args)
2d717e4f 5557 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 5558
8020350c
DB
5559 /* Make sure we unpush even the extended remote targets. Calling
5560 target_mourn_inferior won't unpush, and remote_mourn won't
5561 unpush if there is more than one inferior left. */
f6ac5f3d 5562 unpush_target (this);
8020350c 5563 generic_mourn_inferior ();
2d717e4f 5564
43ff13b4
JM
5565 if (from_tty)
5566 puts_filtered ("Ending remote debugging.\n");
5567}
5568
2d717e4f
DJ
5569/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
5570 be chatty about it. */
5571
f6ac5f3d
PA
5572void
5573extended_remote_target::attach (const char *args, int from_tty)
2d717e4f
DJ
5574{
5575 struct remote_state *rs = get_remote_state ();
be86555c 5576 int pid;
96ef3384 5577 char *wait_status = NULL;
2d717e4f 5578
74164c56 5579 pid = parse_pid_to_attach (args);
2d717e4f 5580
74164c56
JK
5581 /* Remote PID can be freely equal to getpid, do not check it here the same
5582 way as in other targets. */
2d717e4f 5583
4082afcc 5584 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
5585 error (_("This target does not support attaching to a process"));
5586
7cee1e54
PA
5587 if (from_tty)
5588 {
5589 char *exec_file = get_exec_file (0);
5590
5591 if (exec_file)
5592 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5593 target_pid_to_str (pid_to_ptid (pid)));
5594 else
5595 printf_unfiltered (_("Attaching to %s\n"),
5596 target_pid_to_str (pid_to_ptid (pid)));
5597
5598 gdb_flush (gdb_stdout);
5599 }
5600
bba74b36 5601 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
5602 putpkt (rs->buf);
5603 getpkt (&rs->buf, &rs->buf_size, 0);
5604
4082afcc
PA
5605 switch (packet_ok (rs->buf,
5606 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 5607 {
4082afcc 5608 case PACKET_OK:
6efcd9a8 5609 if (!target_is_non_stop_p ())
74531fed
PA
5610 {
5611 /* Save the reply for later. */
224c3ddb 5612 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
5613 strcpy (wait_status, rs->buf);
5614 }
5615 else if (strcmp (rs->buf, "OK") != 0)
5616 error (_("Attaching to %s failed with: %s"),
5617 target_pid_to_str (pid_to_ptid (pid)),
5618 rs->buf);
4082afcc
PA
5619 break;
5620 case PACKET_UNKNOWN:
5621 error (_("This target does not support attaching to a process"));
5622 default:
5623 error (_("Attaching to %s failed"),
5624 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 5625 }
2d717e4f 5626
1b6e6f5c 5627 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 5628
2d717e4f 5629 inferior_ptid = pid_to_ptid (pid);
79d7f229 5630
6efcd9a8 5631 if (target_is_non_stop_p ())
bad34192
PA
5632 {
5633 struct thread_info *thread;
79d7f229 5634
bad34192 5635 /* Get list of threads. */
f6ac5f3d 5636 update_thread_list ();
82f73884 5637
bad34192
PA
5638 thread = first_thread_of_process (pid);
5639 if (thread)
5640 inferior_ptid = thread->ptid;
5641 else
5642 inferior_ptid = pid_to_ptid (pid);
5643
5644 /* Invalidate our notion of the remote current thread. */
47f8a51d 5645 record_currthread (rs, minus_one_ptid);
bad34192 5646 }
74531fed 5647 else
bad34192
PA
5648 {
5649 /* Now, if we have thread information, update inferior_ptid. */
5650 inferior_ptid = remote_current_thread (inferior_ptid);
5651
5652 /* Add the main thread to the thread list. */
00aecdcf
PA
5653 thread_info *thr = add_thread_silent (inferior_ptid);
5654 /* Don't consider the thread stopped until we've processed the
5655 saved stop reply. */
5656 set_executing (thr->ptid, true);
bad34192 5657 }
c0a2216e 5658
96ef3384
UW
5659 /* Next, if the target can specify a description, read it. We do
5660 this before anything involving memory or registers. */
5661 target_find_description ();
5662
6efcd9a8 5663 if (!target_is_non_stop_p ())
74531fed
PA
5664 {
5665 /* Use the previously fetched status. */
5666 gdb_assert (wait_status != NULL);
5667
5668 if (target_can_async_p ())
5669 {
722247f1
YQ
5670 struct notif_event *reply
5671 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 5672
722247f1 5673 push_stop_reply ((struct stop_reply *) reply);
74531fed 5674
6a3753b3 5675 target_async (1);
74531fed
PA
5676 }
5677 else
5678 {
5679 gdb_assert (wait_status != NULL);
5680 strcpy (rs->buf, wait_status);
5681 rs->cached_wait_status = 1;
5682 }
5683 }
5684 else
5685 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5686}
5687
b9c1d481
AS
5688/* Implementation of the to_post_attach method. */
5689
f6ac5f3d
PA
5690void
5691extended_remote_target::post_attach (int pid)
b9c1d481 5692{
6efcd9a8
PA
5693 /* Get text, data & bss offsets. */
5694 get_offsets ();
5695
b9c1d481
AS
5696 /* In certain cases GDB might not have had the chance to start
5697 symbol lookup up until now. This could happen if the debugged
5698 binary is not using shared libraries, the vsyscall page is not
5699 present (on Linux) and the binary itself hadn't changed since the
5700 debugging process was started. */
5701 if (symfile_objfile != NULL)
5702 remote_check_symbols();
5703}
5704
c906108c 5705\f
506fb367
DJ
5706/* Check for the availability of vCont. This function should also check
5707 the response. */
c906108c
SS
5708
5709static void
6d820c5c 5710remote_vcont_probe (struct remote_state *rs)
c906108c 5711{
2e9f7625 5712 char *buf;
6d820c5c 5713
2e9f7625
DJ
5714 strcpy (rs->buf, "vCont?");
5715 putpkt (rs->buf);
6d820c5c 5716 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5717 buf = rs->buf;
c906108c 5718
506fb367 5719 /* Make sure that the features we assume are supported. */
61012eef 5720 if (startswith (buf, "vCont"))
506fb367
DJ
5721 {
5722 char *p = &buf[5];
750ce8d1 5723 int support_c, support_C;
506fb367 5724
750ce8d1
YQ
5725 rs->supports_vCont.s = 0;
5726 rs->supports_vCont.S = 0;
506fb367
DJ
5727 support_c = 0;
5728 support_C = 0;
d458bd84 5729 rs->supports_vCont.t = 0;
c1e36e3e 5730 rs->supports_vCont.r = 0;
506fb367
DJ
5731 while (p && *p == ';')
5732 {
5733 p++;
5734 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5735 rs->supports_vCont.s = 1;
506fb367 5736 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5737 rs->supports_vCont.S = 1;
506fb367
DJ
5738 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5739 support_c = 1;
5740 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5741 support_C = 1;
74531fed 5742 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5743 rs->supports_vCont.t = 1;
c1e36e3e
PA
5744 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5745 rs->supports_vCont.r = 1;
506fb367
DJ
5746
5747 p = strchr (p, ';');
5748 }
c906108c 5749
750ce8d1
YQ
5750 /* If c, and C are not all supported, we can't use vCont. Clearing
5751 BUF will make packet_ok disable the packet. */
5752 if (!support_c || !support_C)
506fb367
DJ
5753 buf[0] = 0;
5754 }
c906108c 5755
444abaca 5756 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5757}
c906108c 5758
0d8f58ca
PA
5759/* Helper function for building "vCont" resumptions. Write a
5760 resumption to P. ENDP points to one-passed-the-end of the buffer
5761 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5762 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5763 resumed thread should be single-stepped and/or signalled. If PTID
5764 equals minus_one_ptid, then all threads are resumed; if PTID
5765 represents a process, then all threads of the process are resumed;
5766 the thread to be stepped and/or signalled is given in the global
5767 INFERIOR_PTID. */
5768
5769static char *
5770append_resumption (char *p, char *endp,
2ea28649 5771 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5772{
5773 struct remote_state *rs = get_remote_state ();
5774
a493e3e2 5775 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5776 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5777 else if (step
5778 /* GDB is willing to range step. */
5779 && use_range_stepping
5780 /* Target supports range stepping. */
5781 && rs->supports_vCont.r
5782 /* We don't currently support range stepping multiple
5783 threads with a wildcard (though the protocol allows it,
5784 so stubs shouldn't make an active effort to forbid
5785 it). */
5786 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5787 {
5788 struct thread_info *tp;
5789
5790 if (ptid_equal (ptid, minus_one_ptid))
5791 {
5792 /* If we don't know about the target thread's tid, then
5793 we're resuming magic_null_ptid (see caller). */
5794 tp = find_thread_ptid (magic_null_ptid);
5795 }
5796 else
5797 tp = find_thread_ptid (ptid);
5798 gdb_assert (tp != NULL);
5799
5800 if (tp->control.may_range_step)
5801 {
5802 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5803
5804 p += xsnprintf (p, endp - p, ";r%s,%s",
5805 phex_nz (tp->control.step_range_start,
5806 addr_size),
5807 phex_nz (tp->control.step_range_end,
5808 addr_size));
5809 }
5810 else
5811 p += xsnprintf (p, endp - p, ";s");
5812 }
0d8f58ca
PA
5813 else if (step)
5814 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5815 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5816 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5817 else
5818 p += xsnprintf (p, endp - p, ";c");
5819
5820 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5821 {
5822 ptid_t nptid;
5823
5824 /* All (-1) threads of process. */
ba348170 5825 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5826
5827 p += xsnprintf (p, endp - p, ":");
5828 p = write_ptid (p, endp, nptid);
5829 }
5830 else if (!ptid_equal (ptid, minus_one_ptid))
5831 {
5832 p += xsnprintf (p, endp - p, ":");
5833 p = write_ptid (p, endp, ptid);
5834 }
5835
5836 return p;
5837}
5838
799a2abe
PA
5839/* Clear the thread's private info on resume. */
5840
5841static void
5842resume_clear_thread_private_info (struct thread_info *thread)
5843{
5844 if (thread->priv != NULL)
5845 {
7aabaf9d
SM
5846 remote_thread_info *priv = get_remote_thread_info (thread);
5847
5848 priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5849 priv->watch_data_address = 0;
799a2abe
PA
5850 }
5851}
5852
e5ef252a
PA
5853/* Append a vCont continue-with-signal action for threads that have a
5854 non-zero stop signal. */
5855
5856static char *
5857append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5858{
5859 struct thread_info *thread;
5860
034f788c 5861 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5862 if (ptid_match (thread->ptid, ptid)
5863 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5864 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5865 {
5866 p = append_resumption (p, endp, thread->ptid,
5867 0, thread->suspend.stop_signal);
5868 thread->suspend.stop_signal = GDB_SIGNAL_0;
799a2abe 5869 resume_clear_thread_private_info (thread);
e5ef252a
PA
5870 }
5871
5872 return p;
5873}
5874
7b68ffbb
PA
5875/* Set the target running, using the packets that use Hc
5876 (c/s/C/S). */
5877
5878static void
5879remote_resume_with_hc (struct target_ops *ops,
5880 ptid_t ptid, int step, enum gdb_signal siggnal)
5881{
5882 struct remote_state *rs = get_remote_state ();
5883 struct thread_info *thread;
5884 char *buf;
5885
5886 rs->last_sent_signal = siggnal;
5887 rs->last_sent_step = step;
5888
5889 /* The c/s/C/S resume packets use Hc, so set the continue
5890 thread. */
5891 if (ptid_equal (ptid, minus_one_ptid))
5892 set_continue_thread (any_thread_ptid);
5893 else
5894 set_continue_thread (ptid);
5895
5896 ALL_NON_EXITED_THREADS (thread)
5897 resume_clear_thread_private_info (thread);
5898
5899 buf = rs->buf;
5900 if (execution_direction == EXEC_REVERSE)
5901 {
5902 /* We don't pass signals to the target in reverse exec mode. */
5903 if (info_verbose && siggnal != GDB_SIGNAL_0)
5904 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5905 siggnal);
5906
5907 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5908 error (_("Remote reverse-step not supported."));
5909 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5910 error (_("Remote reverse-continue not supported."));
5911
5912 strcpy (buf, step ? "bs" : "bc");
5913 }
5914 else if (siggnal != GDB_SIGNAL_0)
5915 {
5916 buf[0] = step ? 'S' : 'C';
5917 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5918 buf[2] = tohex (((int) siggnal) & 0xf);
5919 buf[3] = '\0';
5920 }
5921 else
5922 strcpy (buf, step ? "s" : "c");
5923
5924 putpkt (buf);
5925}
5926
506fb367
DJ
5927/* Resume the remote inferior by using a "vCont" packet. The thread
5928 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5929 resumed thread should be single-stepped and/or signalled. If PTID
5930 equals minus_one_ptid, then all threads are resumed; the thread to
5931 be stepped and/or signalled is given in the global INFERIOR_PTID.
5932 This function returns non-zero iff it resumes the inferior.
44eaed12 5933
7b68ffbb
PA
5934 This function issues a strict subset of all possible vCont commands
5935 at the moment. */
44eaed12 5936
506fb367 5937static int
7b68ffbb 5938remote_resume_with_vcont (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5939{
5940 struct remote_state *rs = get_remote_state ();
82f73884
PA
5941 char *p;
5942 char *endp;
44eaed12 5943
7b68ffbb
PA
5944 /* No reverse execution actions defined for vCont. */
5945 if (execution_direction == EXEC_REVERSE)
5946 return 0;
5947
4082afcc 5948 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5949 remote_vcont_probe (rs);
44eaed12 5950
4082afcc 5951 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5952 return 0;
44eaed12 5953
82f73884
PA
5954 p = rs->buf;
5955 endp = rs->buf + get_remote_packet_size ();
5956
506fb367
DJ
5957 /* If we could generate a wider range of packets, we'd have to worry
5958 about overflowing BUF. Should there be a generic
5959 "multi-part-packet" packet? */
5960
0d8f58ca
PA
5961 p += xsnprintf (p, endp - p, "vCont");
5962
79d7f229 5963 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5964 {
79d7f229
PA
5965 /* MAGIC_NULL_PTID means that we don't have any active threads,
5966 so we don't have any TID numbers the inferior will
5967 understand. Make sure to only send forms that do not specify
5968 a TID. */
a9cbf802 5969 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5970 }
0d8f58ca 5971 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5972 {
0d8f58ca
PA
5973 /* Resume all threads (of all processes, or of a single
5974 process), with preference for INFERIOR_PTID. This assumes
5975 inferior_ptid belongs to the set of all threads we are about
5976 to resume. */
a493e3e2 5977 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5978 {
0d8f58ca
PA
5979 /* Step inferior_ptid, with or without signal. */
5980 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5981 }
0d8f58ca 5982
e5ef252a
PA
5983 /* Also pass down any pending signaled resumption for other
5984 threads not the current. */
5985 p = append_pending_thread_resumptions (p, endp, ptid);
5986
0d8f58ca 5987 /* And continue others without a signal. */
a493e3e2 5988 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5989 }
5990 else
506fb367
DJ
5991 {
5992 /* Scheduler locking; resume only PTID. */
a9cbf802 5993 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5994 }
c906108c 5995
82f73884
PA
5996 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5997 putpkt (rs->buf);
506fb367 5998
6efcd9a8 5999 if (target_is_non_stop_p ())
74531fed
PA
6000 {
6001 /* In non-stop, the stub replies to vCont with "OK". The stop
6002 reply will be reported asynchronously by means of a `%Stop'
6003 notification. */
6004 getpkt (&rs->buf, &rs->buf_size, 0);
6005 if (strcmp (rs->buf, "OK") != 0)
6006 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
6007 }
6008
506fb367 6009 return 1;
c906108c 6010}
43ff13b4 6011
506fb367
DJ
6012/* Tell the remote machine to resume. */
6013
f6ac5f3d
PA
6014void
6015remote_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 6016{
d01949b6 6017 struct remote_state *rs = get_remote_state ();
43ff13b4 6018
85ad3aaf
PA
6019 /* When connected in non-stop mode, the core resumes threads
6020 individually. Resuming remote threads directly in target_resume
6021 would thus result in sending one packet per thread. Instead, to
6022 minimize roundtrip latency, here we just store the resume
6023 request; the actual remote resumption will be done in
6024 target_commit_resume / remote_commit_resume, where we'll be able
6025 to do vCont action coalescing. */
f6ac5f3d 6026 if (target_is_non_stop_p () && ::execution_direction != EXEC_REVERSE)
85ad3aaf 6027 {
7aabaf9d 6028 remote_thread_info *remote_thr;
85ad3aaf
PA
6029
6030 if (ptid_equal (minus_one_ptid, ptid) || ptid_is_pid (ptid))
7aabaf9d 6031 remote_thr = get_remote_thread_info (inferior_ptid);
85ad3aaf 6032 else
7aabaf9d
SM
6033 remote_thr = get_remote_thread_info (ptid);
6034
85ad3aaf
PA
6035 remote_thr->last_resume_step = step;
6036 remote_thr->last_resume_sig = siggnal;
6037 return;
6038 }
6039
722247f1
YQ
6040 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6041 (explained in remote-notif.c:handle_notification) so
6042 remote_notif_process is not called. We need find a place where
6043 it is safe to start a 'vNotif' sequence. It is good to do it
6044 before resuming inferior, because inferior was stopped and no RSP
6045 traffic at that moment. */
6efcd9a8 6046 if (!target_is_non_stop_p ())
5965e028 6047 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 6048
f6ac5f3d 6049 rs->last_resume_exec_dir = ::execution_direction;
3a00c802 6050
7b68ffbb
PA
6051 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6052 if (!remote_resume_with_vcont (ptid, step, siggnal))
f6ac5f3d 6053 remote_resume_with_hc (this, ptid, step, siggnal);
43ff13b4 6054
2acceee2 6055 /* We are about to start executing the inferior, let's register it
0df8b418
MS
6056 with the event loop. NOTE: this is the one place where all the
6057 execution commands end up. We could alternatively do this in each
23860348 6058 of the execution commands in infcmd.c. */
2acceee2
JM
6059 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
6060 into infcmd.c in order to allow inferior function calls to work
23860348 6061 NOT asynchronously. */
362646f5 6062 if (target_can_async_p ())
6a3753b3 6063 target_async (1);
e24a49d8
PA
6064
6065 /* We've just told the target to resume. The remote server will
6066 wait for the inferior to stop, and then send a stop reply. In
6067 the mean time, we can't start another command/query ourselves
74531fed
PA
6068 because the stub wouldn't be ready to process it. This applies
6069 only to the base all-stop protocol, however. In non-stop (which
6070 only supports vCont), the stub replies with an "OK", and is
6071 immediate able to process further serial input. */
6efcd9a8 6072 if (!target_is_non_stop_p ())
74531fed 6073 rs->waiting_for_stop_reply = 1;
43ff13b4 6074}
85ad3aaf
PA
6075
6076static void check_pending_events_prevent_wildcard_vcont
6077 (int *may_global_wildcard_vcont);
6078static int is_pending_fork_parent_thread (struct thread_info *thread);
6079
6080/* Private per-inferior info for target remote processes. */
6081
089354bb 6082struct remote_inferior : public private_inferior
85ad3aaf
PA
6083{
6084 /* Whether we can send a wildcard vCont for this process. */
089354bb 6085 bool may_wildcard_vcont = true;
85ad3aaf
PA
6086};
6087
089354bb
SM
6088/* Get the remote private inferior data associated to INF. */
6089
6090static remote_inferior *
6091get_remote_inferior (inferior *inf)
6092{
6093 if (inf->priv == NULL)
6094 inf->priv.reset (new remote_inferior);
6095
6096 return static_cast<remote_inferior *> (inf->priv.get ());
6097}
6098
f5db4863 6099/* Class used to track the construction of a vCont packet in the
85ad3aaf
PA
6100 outgoing packet buffer. This is used to send multiple vCont
6101 packets if we have more actions than would fit a single packet. */
6102
f5db4863 6103class vcont_builder
85ad3aaf 6104{
f5db4863
PA
6105public:
6106 vcont_builder ()
6107 {
6108 restart ();
6109 }
6110
6111 void flush ();
6112 void push_action (ptid_t ptid, bool step, gdb_signal siggnal);
6113
6114private:
6115 void restart ();
6116
85ad3aaf
PA
6117 /* Pointer to the first action. P points here if no action has been
6118 appended yet. */
f5db4863 6119 char *m_first_action;
85ad3aaf
PA
6120
6121 /* Where the next action will be appended. */
f5db4863 6122 char *m_p;
85ad3aaf
PA
6123
6124 /* The end of the buffer. Must never write past this. */
f5db4863 6125 char *m_endp;
85ad3aaf
PA
6126};
6127
6128/* Prepare the outgoing buffer for a new vCont packet. */
6129
f5db4863
PA
6130void
6131vcont_builder::restart ()
85ad3aaf
PA
6132{
6133 struct remote_state *rs = get_remote_state ();
6134
f5db4863
PA
6135 m_p = rs->buf;
6136 m_endp = rs->buf + get_remote_packet_size ();
6137 m_p += xsnprintf (m_p, m_endp - m_p, "vCont");
6138 m_first_action = m_p;
85ad3aaf
PA
6139}
6140
6141/* If the vCont packet being built has any action, send it to the
6142 remote end. */
6143
f5db4863
PA
6144void
6145vcont_builder::flush ()
85ad3aaf
PA
6146{
6147 struct remote_state *rs;
6148
f5db4863 6149 if (m_p == m_first_action)
85ad3aaf
PA
6150 return;
6151
6152 rs = get_remote_state ();
6153 putpkt (rs->buf);
6154 getpkt (&rs->buf, &rs->buf_size, 0);
6155 if (strcmp (rs->buf, "OK") != 0)
6156 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
6157}
6158
6159/* The largest action is range-stepping, with its two addresses. This
6160 is more than sufficient. If a new, bigger action is created, it'll
6161 quickly trigger a failed assertion in append_resumption (and we'll
6162 just bump this). */
6163#define MAX_ACTION_SIZE 200
6164
6165/* Append a new vCont action in the outgoing packet being built. If
6166 the action doesn't fit the packet along with previous actions, push
6167 what we've got so far to the remote end and start over a new vCont
6168 packet (with the new action). */
6169
f5db4863
PA
6170void
6171vcont_builder::push_action (ptid_t ptid, bool step, gdb_signal siggnal)
85ad3aaf
PA
6172{
6173 char buf[MAX_ACTION_SIZE + 1];
85ad3aaf 6174
f5db4863
PA
6175 char *endp = append_resumption (buf, buf + sizeof (buf),
6176 ptid, step, siggnal);
85ad3aaf
PA
6177
6178 /* Check whether this new action would fit in the vCont packet along
6179 with previous actions. If not, send what we've got so far and
6180 start a new vCont packet. */
f5db4863
PA
6181 size_t rsize = endp - buf;
6182 if (rsize > m_endp - m_p)
85ad3aaf 6183 {
f5db4863
PA
6184 flush ();
6185 restart ();
85ad3aaf
PA
6186
6187 /* Should now fit. */
f5db4863 6188 gdb_assert (rsize <= m_endp - m_p);
85ad3aaf
PA
6189 }
6190
f5db4863
PA
6191 memcpy (m_p, buf, rsize);
6192 m_p += rsize;
6193 *m_p = '\0';
85ad3aaf
PA
6194}
6195
6196/* to_commit_resume implementation. */
6197
f6ac5f3d
PA
6198void
6199remote_target::commit_resume ()
85ad3aaf 6200{
85ad3aaf
PA
6201 struct inferior *inf;
6202 struct thread_info *tp;
6203 int any_process_wildcard;
6204 int may_global_wildcard_vcont;
85ad3aaf
PA
6205
6206 /* If connected in all-stop mode, we'd send the remote resume
6207 request directly from remote_resume. Likewise if
6208 reverse-debugging, as there are no defined vCont actions for
6209 reverse execution. */
f6ac5f3d 6210 if (!target_is_non_stop_p () || ::execution_direction == EXEC_REVERSE)
85ad3aaf
PA
6211 return;
6212
6213 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6214 instead of resuming all threads of each process individually.
6215 However, if any thread of a process must remain halted, we can't
6216 send wildcard resumes and must send one action per thread.
6217
6218 Care must be taken to not resume threads/processes the server
6219 side already told us are stopped, but the core doesn't know about
6220 yet, because the events are still in the vStopped notification
6221 queue. For example:
6222
6223 #1 => vCont s:p1.1;c
6224 #2 <= OK
6225 #3 <= %Stopped T05 p1.1
6226 #4 => vStopped
6227 #5 <= T05 p1.2
6228 #6 => vStopped
6229 #7 <= OK
6230 #8 (infrun handles the stop for p1.1 and continues stepping)
6231 #9 => vCont s:p1.1;c
6232
6233 The last vCont above would resume thread p1.2 by mistake, because
6234 the server has no idea that the event for p1.2 had not been
6235 handled yet.
6236
6237 The server side must similarly ignore resume actions for the
6238 thread that has a pending %Stopped notification (and any other
6239 threads with events pending), until GDB acks the notification
6240 with vStopped. Otherwise, e.g., the following case is
6241 mishandled:
6242
6243 #1 => g (or any other packet)
6244 #2 <= [registers]
6245 #3 <= %Stopped T05 p1.2
6246 #4 => vCont s:p1.1;c
6247 #5 <= OK
6248
6249 Above, the server must not resume thread p1.2. GDB can't know
6250 that p1.2 stopped until it acks the %Stopped notification, and
6251 since from GDB's perspective all threads should be running, it
6252 sends a "c" action.
6253
6254 Finally, special care must also be given to handling fork/vfork
6255 events. A (v)fork event actually tells us that two processes
6256 stopped -- the parent and the child. Until we follow the fork,
6257 we must not resume the child. Therefore, if we have a pending
6258 fork follow, we must not send a global wildcard resume action
6259 (vCont;c). We can still send process-wide wildcards though. */
6260
6261 /* Start by assuming a global wildcard (vCont;c) is possible. */
6262 may_global_wildcard_vcont = 1;
6263
6264 /* And assume every process is individually wildcard-able too. */
6265 ALL_NON_EXITED_INFERIORS (inf)
6266 {
089354bb
SM
6267 remote_inferior *priv = get_remote_inferior (inf);
6268
6269 priv->may_wildcard_vcont = true;
85ad3aaf
PA
6270 }
6271
6272 /* Check for any pending events (not reported or processed yet) and
6273 disable process and global wildcard resumes appropriately. */
6274 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
6275
6276 ALL_NON_EXITED_THREADS (tp)
6277 {
6278 /* If a thread of a process is not meant to be resumed, then we
6279 can't wildcard that process. */
6280 if (!tp->executing)
6281 {
089354bb 6282 get_remote_inferior (tp->inf)->may_wildcard_vcont = false;
85ad3aaf
PA
6283
6284 /* And if we can't wildcard a process, we can't wildcard
6285 everything either. */
6286 may_global_wildcard_vcont = 0;
6287 continue;
6288 }
6289
6290 /* If a thread is the parent of an unfollowed fork, then we
6291 can't do a global wildcard, as that would resume the fork
6292 child. */
6293 if (is_pending_fork_parent_thread (tp))
6294 may_global_wildcard_vcont = 0;
6295 }
6296
6297 /* Now let's build the vCont packet(s). Actions must be appended
6298 from narrower to wider scopes (thread -> process -> global). If
6299 we end up with too many actions for a single packet vcont_builder
6300 flushes the current vCont packet to the remote side and starts a
6301 new one. */
f5db4863 6302 struct vcont_builder vcont_builder;
85ad3aaf
PA
6303
6304 /* Threads first. */
6305 ALL_NON_EXITED_THREADS (tp)
6306 {
7aabaf9d 6307 remote_thread_info *remote_thr = get_remote_thread_info (tp);
85ad3aaf
PA
6308
6309 if (!tp->executing || remote_thr->vcont_resumed)
6310 continue;
6311
6312 gdb_assert (!thread_is_in_step_over_chain (tp));
6313
6314 if (!remote_thr->last_resume_step
6315 && remote_thr->last_resume_sig == GDB_SIGNAL_0
089354bb 6316 && get_remote_inferior (tp->inf)->may_wildcard_vcont)
85ad3aaf
PA
6317 {
6318 /* We'll send a wildcard resume instead. */
6319 remote_thr->vcont_resumed = 1;
6320 continue;
6321 }
6322
f5db4863 6323 vcont_builder.push_action (tp->ptid,
85ad3aaf
PA
6324 remote_thr->last_resume_step,
6325 remote_thr->last_resume_sig);
6326 remote_thr->vcont_resumed = 1;
6327 }
6328
6329 /* Now check whether we can send any process-wide wildcard. This is
6330 to avoid sending a global wildcard in the case nothing is
6331 supposed to be resumed. */
6332 any_process_wildcard = 0;
6333
6334 ALL_NON_EXITED_INFERIORS (inf)
6335 {
089354bb 6336 if (get_remote_inferior (inf)->may_wildcard_vcont)
85ad3aaf
PA
6337 {
6338 any_process_wildcard = 1;
6339 break;
6340 }
6341 }
6342
6343 if (any_process_wildcard)
6344 {
6345 /* If all processes are wildcard-able, then send a single "c"
6346 action, otherwise, send an "all (-1) threads of process"
6347 continue action for each running process, if any. */
6348 if (may_global_wildcard_vcont)
6349 {
f5db4863
PA
6350 vcont_builder.push_action (minus_one_ptid,
6351 false, GDB_SIGNAL_0);
85ad3aaf
PA
6352 }
6353 else
6354 {
6355 ALL_NON_EXITED_INFERIORS (inf)
6356 {
089354bb 6357 if (get_remote_inferior (inf)->may_wildcard_vcont)
85ad3aaf 6358 {
f5db4863
PA
6359 vcont_builder.push_action (pid_to_ptid (inf->pid),
6360 false, GDB_SIGNAL_0);
85ad3aaf
PA
6361 }
6362 }
6363 }
6364 }
6365
f5db4863 6366 vcont_builder.flush ();
85ad3aaf
PA
6367}
6368
c906108c 6369\f
43ff13b4 6370
74531fed
PA
6371/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6372 thread, all threads of a remote process, or all threads of all
6373 processes. */
6374
6375static void
6376remote_stop_ns (ptid_t ptid)
6377{
6378 struct remote_state *rs = get_remote_state ();
6379 char *p = rs->buf;
6380 char *endp = rs->buf + get_remote_packet_size ();
74531fed 6381
4082afcc 6382 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
6383 remote_vcont_probe (rs);
6384
d458bd84 6385 if (!rs->supports_vCont.t)
74531fed
PA
6386 error (_("Remote server does not support stopping threads"));
6387
f91d3df5
PA
6388 if (ptid_equal (ptid, minus_one_ptid)
6389 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
6390 p += xsnprintf (p, endp - p, "vCont;t");
6391 else
6392 {
6393 ptid_t nptid;
6394
74531fed
PA
6395 p += xsnprintf (p, endp - p, "vCont;t:");
6396
6397 if (ptid_is_pid (ptid))
6398 /* All (-1) threads of process. */
ba348170 6399 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
6400 else
6401 {
6402 /* Small optimization: if we already have a stop reply for
6403 this thread, no use in telling the stub we want this
6404 stopped. */
6405 if (peek_stop_reply (ptid))
6406 return;
6407
6408 nptid = ptid;
6409 }
6410
a9cbf802 6411 write_ptid (p, endp, nptid);
74531fed
PA
6412 }
6413
6414 /* In non-stop, we get an immediate OK reply. The stop reply will
6415 come in asynchronously by notification. */
6416 putpkt (rs->buf);
6417 getpkt (&rs->buf, &rs->buf_size, 0);
6418 if (strcmp (rs->buf, "OK") != 0)
6419 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
6420}
6421
bfedc46a
PA
6422/* All-stop version of target_interrupt. Sends a break or a ^C to
6423 interrupt the remote target. It is undefined which thread of which
6424 process reports the interrupt. */
74531fed
PA
6425
6426static void
de979965 6427remote_interrupt_as (void)
74531fed
PA
6428{
6429 struct remote_state *rs = get_remote_state ();
6430
3a29589a
DJ
6431 rs->ctrlc_pending_p = 1;
6432
74531fed
PA
6433 /* If the inferior is stopped already, but the core didn't know
6434 about it yet, just ignore the request. The cached wait status
6435 will be collected in remote_wait. */
6436 if (rs->cached_wait_status)
6437 return;
6438
9a7071a8
JB
6439 /* Send interrupt_sequence to remote target. */
6440 send_interrupt_sequence ();
74531fed
PA
6441}
6442
de979965
PA
6443/* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
6444 the remote target. It is undefined which thread of which process
e42de8c7
PA
6445 reports the interrupt. Throws an error if the packet is not
6446 supported by the server. */
de979965 6447
e42de8c7 6448static void
de979965
PA
6449remote_interrupt_ns (void)
6450{
6451 struct remote_state *rs = get_remote_state ();
6452 char *p = rs->buf;
6453 char *endp = rs->buf + get_remote_packet_size ();
6454
6455 xsnprintf (p, endp - p, "vCtrlC");
6456
6457 /* In non-stop, we get an immediate OK reply. The stop reply will
6458 come in asynchronously by notification. */
6459 putpkt (rs->buf);
6460 getpkt (&rs->buf, &rs->buf_size, 0);
6461
6462 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
6463 {
6464 case PACKET_OK:
6465 break;
6466 case PACKET_UNKNOWN:
e42de8c7 6467 error (_("No support for interrupting the remote target."));
de979965
PA
6468 case PACKET_ERROR:
6469 error (_("Interrupting target failed: %s"), rs->buf);
6470 }
de979965
PA
6471}
6472
bfedc46a 6473/* Implement the to_stop function for the remote targets. */
74531fed 6474
f6ac5f3d
PA
6475void
6476remote_target::stop (ptid_t ptid)
c906108c 6477{
7a292a7a 6478 if (remote_debug)
0f71a2f6 6479 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 6480
6efcd9a8 6481 if (target_is_non_stop_p ())
74531fed 6482 remote_stop_ns (ptid);
c906108c 6483 else
bfedc46a
PA
6484 {
6485 /* We don't currently have a way to transparently pause the
6486 remote target in all-stop mode. Interrupt it instead. */
de979965 6487 remote_interrupt_as ();
bfedc46a
PA
6488 }
6489}
6490
6491/* Implement the to_interrupt function for the remote targets. */
6492
f6ac5f3d
PA
6493void
6494remote_target::interrupt ()
bfedc46a
PA
6495{
6496 if (remote_debug)
6497 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
6498
e42de8c7
PA
6499 if (target_is_non_stop_p ())
6500 remote_interrupt_ns ();
bfedc46a 6501 else
e42de8c7 6502 remote_interrupt_as ();
c906108c
SS
6503}
6504
93692b58
PA
6505/* Implement the to_pass_ctrlc function for the remote targets. */
6506
f6ac5f3d
PA
6507void
6508remote_target::pass_ctrlc ()
93692b58
PA
6509{
6510 struct remote_state *rs = get_remote_state ();
6511
6512 if (remote_debug)
6513 fprintf_unfiltered (gdb_stdlog, "remote_pass_ctrlc called\n");
6514
6515 /* If we're starting up, we're not fully synced yet. Quit
6516 immediately. */
6517 if (rs->starting_up)
6518 quit ();
6519 /* If ^C has already been sent once, offer to disconnect. */
6520 else if (rs->ctrlc_pending_p)
6521 interrupt_query ();
6522 else
e671cd59 6523 target_interrupt ();
93692b58
PA
6524}
6525
c906108c
SS
6526/* Ask the user what to do when an interrupt is received. */
6527
6528static void
fba45db2 6529interrupt_query (void)
c906108c 6530{
abc56d60 6531 struct remote_state *rs = get_remote_state ();
c906108c 6532
abc56d60 6533 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 6534 {
abc56d60
PA
6535 if (query (_("The target is not responding to interrupt requests.\n"
6536 "Stop debugging it? ")))
74531fed 6537 {
78a095c3 6538 remote_unpush_target ();
abc56d60 6539 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
6540 }
6541 }
abc56d60
PA
6542 else
6543 {
6544 if (query (_("Interrupted while waiting for the program.\n"
6545 "Give up waiting? ")))
6546 quit ();
6547 }
c906108c
SS
6548}
6549
6426a772
JM
6550/* Enable/disable target terminal ownership. Most targets can use
6551 terminal groups to control terminal ownership. Remote targets are
6552 different in that explicit transfer of ownership to/from GDB/target
23860348 6553 is required. */
6426a772 6554
f6ac5f3d
PA
6555void
6556remote_target::terminal_inferior ()
6426a772 6557{
6426a772
JM
6558 /* NOTE: At this point we could also register our selves as the
6559 recipient of all input. Any characters typed could then be
23860348 6560 passed on down to the target. */
6426a772
JM
6561}
6562
f6ac5f3d
PA
6563void
6564remote_target::terminal_ours ()
6426a772 6565{
6426a772
JM
6566}
6567
176a6961 6568static void
917317f4 6569remote_console_output (char *msg)
c906108c
SS
6570{
6571 char *p;
6572
c5aa993b 6573 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
6574 {
6575 char tb[2];
6576 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 6577
c906108c
SS
6578 tb[0] = c;
6579 tb[1] = 0;
43ff13b4 6580 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 6581 }
00db5b94
PA
6582 gdb_flush (gdb_stdtarg);
6583}
74531fed 6584
74531fed
PA
6585DEF_VEC_O(cached_reg_t);
6586
722247f1 6587typedef struct stop_reply
74531fed 6588{
722247f1 6589 struct notif_event base;
74531fed 6590
722247f1 6591 /* The identifier of the thread about this event */
74531fed
PA
6592 ptid_t ptid;
6593
340e3c99 6594 /* The remote state this event is associated with. When the remote
bcc75809
YQ
6595 connection, represented by a remote_state object, is closed,
6596 all the associated stop_reply events should be released. */
6597 struct remote_state *rs;
6598
74531fed
PA
6599 struct target_waitstatus ws;
6600
5cd63fda
PA
6601 /* The architecture associated with the expedited registers. */
6602 gdbarch *arch;
6603
15148d6a
PA
6604 /* Expedited registers. This makes remote debugging a bit more
6605 efficient for those targets that provide critical registers as
6606 part of their normal status mechanism (as another roundtrip to
6607 fetch them is avoided). */
74531fed
PA
6608 VEC(cached_reg_t) *regcache;
6609
f7e6eed5
PA
6610 enum target_stop_reason stop_reason;
6611
74531fed
PA
6612 CORE_ADDR watch_data_address;
6613
dc146f7c 6614 int core;
722247f1 6615} *stop_reply_p;
a744cf53 6616
722247f1
YQ
6617DECLARE_QUEUE_P (stop_reply_p);
6618DEFINE_QUEUE_P (stop_reply_p);
6619/* The list of already fetched and acknowledged stop events. This
6620 queue is used for notification Stop, and other notifications
6621 don't need queue for their events, because the notification events
6622 of Stop can't be consumed immediately, so that events should be
6623 queued first, and be consumed by remote_wait_{ns,as} one per
6624 time. Other notifications can consume their events immediately,
6625 so queue is not needed for them. */
6626static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
6627
6628static void
6629stop_reply_xfree (struct stop_reply *r)
6630{
f48ff2a7 6631 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
6632}
6633
221e1a37
PA
6634/* Return the length of the stop reply queue. */
6635
6636static int
6637stop_reply_queue_length (void)
6638{
6639 return QUEUE_length (stop_reply_p, stop_reply_queue);
6640}
6641
722247f1
YQ
6642static void
6643remote_notif_stop_parse (struct notif_client *self, char *buf,
6644 struct notif_event *event)
6645{
6646 remote_parse_stop_reply (buf, (struct stop_reply *) event);
6647}
6648
6649static void
6650remote_notif_stop_ack (struct notif_client *self, char *buf,
6651 struct notif_event *event)
6652{
6653 struct stop_reply *stop_reply = (struct stop_reply *) event;
6654
6655 /* acknowledge */
f5c4fcd9 6656 putpkt (self->ack_command);
722247f1
YQ
6657
6658 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
6659 /* We got an unknown stop reply. */
6660 error (_("Unknown stop reply"));
6661
6662 push_stop_reply (stop_reply);
6663}
6664
6665static int
6666remote_notif_stop_can_get_pending_events (struct notif_client *self)
6667{
6668 /* We can't get pending events in remote_notif_process for
6669 notification stop, and we have to do this in remote_wait_ns
6670 instead. If we fetch all queued events from stub, remote stub
6671 may exit and we have no chance to process them back in
6672 remote_wait_ns. */
6673 mark_async_event_handler (remote_async_inferior_event_token);
6674 return 0;
6675}
6676
6677static void
6678stop_reply_dtr (struct notif_event *event)
6679{
6680 struct stop_reply *r = (struct stop_reply *) event;
d1dff226
AH
6681 cached_reg_t *reg;
6682 int ix;
6683
6684 for (ix = 0;
6685 VEC_iterate (cached_reg_t, r->regcache, ix, reg);
6686 ix++)
6687 xfree (reg->data);
722247f1
YQ
6688
6689 VEC_free (cached_reg_t, r->regcache);
6690}
6691
6692static struct notif_event *
6693remote_notif_stop_alloc_reply (void)
6694{
8d749320
SM
6695 /* We cast to a pointer to the "base class". */
6696 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
6697
6698 r->dtr = stop_reply_dtr;
6699
6700 return r;
6701}
6702
6703/* A client of notification Stop. */
6704
6705struct notif_client notif_client_stop =
6706{
6707 "Stop",
6708 "vStopped",
6709 remote_notif_stop_parse,
6710 remote_notif_stop_ack,
6711 remote_notif_stop_can_get_pending_events,
6712 remote_notif_stop_alloc_reply,
f48ff2a7 6713 REMOTE_NOTIF_STOP,
722247f1
YQ
6714};
6715
6716/* A parameter to pass data in and out. */
6717
6718struct queue_iter_param
6719{
6720 void *input;
6721 struct stop_reply *output;
6722};
6723
85ad3aaf 6724/* Determine if THREAD_PTID is a pending fork parent thread. ARG contains
cbb8991c
DB
6725 the pid of the process that owns the threads we want to check, or
6726 -1 if we want to check all threads. */
6727
6728static int
6729is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
6730 ptid_t thread_ptid)
6731{
6732 if (ws->kind == TARGET_WAITKIND_FORKED
6733 || ws->kind == TARGET_WAITKIND_VFORKED)
6734 {
6735 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
6736 return 1;
6737 }
6738
6739 return 0;
6740}
6741
85ad3aaf
PA
6742/* Return the thread's pending status used to determine whether the
6743 thread is a fork parent stopped at a fork event. */
6744
6745static struct target_waitstatus *
6746thread_pending_fork_status (struct thread_info *thread)
6747{
6748 if (thread->suspend.waitstatus_pending_p)
6749 return &thread->suspend.waitstatus;
6750 else
6751 return &thread->pending_follow;
6752}
6753
6754/* Determine if THREAD is a pending fork parent thread. */
6755
6756static int
6757is_pending_fork_parent_thread (struct thread_info *thread)
6758{
6759 struct target_waitstatus *ws = thread_pending_fork_status (thread);
6760 int pid = -1;
6761
6762 return is_pending_fork_parent (ws, pid, thread->ptid);
6763}
6764
cbb8991c
DB
6765/* Check whether EVENT is a fork event, and if it is, remove the
6766 fork child from the context list passed in DATA. */
6767
6768static int
6769remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
6770 QUEUE_ITER (stop_reply_p) *iter,
6771 stop_reply_p event,
6772 void *data)
6773{
19ba03f4
SM
6774 struct queue_iter_param *param = (struct queue_iter_param *) data;
6775 struct threads_listing_context *context
6776 = (struct threads_listing_context *) param->input;
cbb8991c
DB
6777
6778 if (event->ws.kind == TARGET_WAITKIND_FORKED
65706a29
PA
6779 || event->ws.kind == TARGET_WAITKIND_VFORKED
6780 || event->ws.kind == TARGET_WAITKIND_THREAD_EXITED)
21fe1c75 6781 context->remove_thread (event->ws.value.related_pid);
cbb8991c
DB
6782
6783 return 1;
6784}
6785
6786/* If CONTEXT contains any fork child threads that have not been
6787 reported yet, remove them from the CONTEXT list. If such a
6788 thread exists it is because we are stopped at a fork catchpoint
6789 and have not yet called follow_fork, which will set up the
6790 host-side data structures for the new process. */
6791
6792static void
6793remove_new_fork_children (struct threads_listing_context *context)
6794{
6795 struct thread_info * thread;
6796 int pid = -1;
6797 struct notif_client *notif = &notif_client_stop;
6798 struct queue_iter_param param;
6799
6800 /* For any threads stopped at a fork event, remove the corresponding
6801 fork child threads from the CONTEXT list. */
6802 ALL_NON_EXITED_THREADS (thread)
6803 {
85ad3aaf 6804 struct target_waitstatus *ws = thread_pending_fork_status (thread);
cbb8991c
DB
6805
6806 if (is_pending_fork_parent (ws, pid, thread->ptid))
21fe1c75 6807 context->remove_thread (ws->value.related_pid);
cbb8991c
DB
6808 }
6809
6810 /* Check for any pending fork events (not reported or processed yet)
6811 in process PID and remove those fork child threads from the
6812 CONTEXT list as well. */
6813 remote_notif_get_pending_events (notif);
6814 param.input = context;
6815 param.output = NULL;
6816 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6817 remove_child_of_pending_fork, &param);
6818}
6819
85ad3aaf
PA
6820/* Check whether EVENT would prevent a global or process wildcard
6821 vCont action. */
6822
6823static int
6824check_pending_event_prevents_wildcard_vcont_callback
6825 (QUEUE (stop_reply_p) *q,
6826 QUEUE_ITER (stop_reply_p) *iter,
6827 stop_reply_p event,
6828 void *data)
6829{
6830 struct inferior *inf;
6831 int *may_global_wildcard_vcont = (int *) data;
6832
6833 if (event->ws.kind == TARGET_WAITKIND_NO_RESUMED
6834 || event->ws.kind == TARGET_WAITKIND_NO_HISTORY)
6835 return 1;
6836
6837 if (event->ws.kind == TARGET_WAITKIND_FORKED
6838 || event->ws.kind == TARGET_WAITKIND_VFORKED)
6839 *may_global_wildcard_vcont = 0;
6840
6841 inf = find_inferior_ptid (event->ptid);
6842
6843 /* This may be the first time we heard about this process.
6844 Regardless, we must not do a global wildcard resume, otherwise
6845 we'd resume this process too. */
6846 *may_global_wildcard_vcont = 0;
6847 if (inf != NULL)
089354bb 6848 get_remote_inferior (inf)->may_wildcard_vcont = false;
85ad3aaf
PA
6849
6850 return 1;
6851}
6852
6853/* Check whether any event pending in the vStopped queue would prevent
6854 a global or process wildcard vCont action. Clear
6855 *may_global_wildcard if we can't do a global wildcard (vCont;c),
6856 and clear the event inferior's may_wildcard_vcont flag if we can't
6857 do a process-wide wildcard resume (vCont;c:pPID.-1). */
6858
6859static void
6860check_pending_events_prevent_wildcard_vcont (int *may_global_wildcard)
6861{
6862 struct notif_client *notif = &notif_client_stop;
6863
6864 remote_notif_get_pending_events (notif);
6865 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6866 check_pending_event_prevents_wildcard_vcont_callback,
6867 may_global_wildcard);
6868}
6869
f48ff2a7
YQ
6870/* Remove stop replies in the queue if its pid is equal to the given
6871 inferior's pid. */
722247f1
YQ
6872
6873static int
f48ff2a7
YQ
6874remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
6875 QUEUE_ITER (stop_reply_p) *iter,
6876 stop_reply_p event,
6877 void *data)
722247f1 6878{
19ba03f4
SM
6879 struct queue_iter_param *param = (struct queue_iter_param *) data;
6880 struct inferior *inf = (struct inferior *) param->input;
722247f1 6881
f48ff2a7 6882 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
6883 {
6884 stop_reply_xfree (event);
6885 QUEUE_remove_elem (stop_reply_p, q, iter);
6886 }
6887
6888 return 1;
6889}
6890
f48ff2a7 6891/* Discard all pending stop replies of inferior INF. */
c906108c 6892
74531fed 6893static void
5f4cf0bb 6894discard_pending_stop_replies (struct inferior *inf)
c906108c 6895{
722247f1 6896 struct queue_iter_param param;
f48ff2a7
YQ
6897 struct stop_reply *reply;
6898 struct remote_state *rs = get_remote_state ();
6899 struct remote_notif_state *rns = rs->notif_state;
6900
6901 /* This function can be notified when an inferior exists. When the
6902 target is not remote, the notification state is NULL. */
6903 if (rs->remote_desc == NULL)
6904 return;
6905
6906 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 6907
74531fed 6908 /* Discard the in-flight notification. */
f48ff2a7 6909 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 6910 {
722247f1 6911 stop_reply_xfree (reply);
f48ff2a7 6912 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 6913 }
c906108c 6914
722247f1
YQ
6915 param.input = inf;
6916 param.output = NULL;
74531fed
PA
6917 /* Discard the stop replies we have already pulled with
6918 vStopped. */
722247f1 6919 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
6920 remove_stop_reply_for_inferior, &param);
6921}
6922
bcc75809
YQ
6923/* If its remote state is equal to the given remote state,
6924 remove EVENT from the stop reply queue. */
6925
6926static int
6927remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
6928 QUEUE_ITER (stop_reply_p) *iter,
6929 stop_reply_p event,
6930 void *data)
6931{
19ba03f4
SM
6932 struct queue_iter_param *param = (struct queue_iter_param *) data;
6933 struct remote_state *rs = (struct remote_state *) param->input;
bcc75809
YQ
6934
6935 if (event->rs == rs)
6936 {
6937 stop_reply_xfree (event);
6938 QUEUE_remove_elem (stop_reply_p, q, iter);
6939 }
6940
6941 return 1;
6942}
6943
6944/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
6945
6946static void
bcc75809 6947discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
6948{
6949 struct queue_iter_param param;
6950
bcc75809 6951 param.input = rs;
f48ff2a7
YQ
6952 param.output = NULL;
6953 /* Discard the stop replies we have already pulled with
6954 vStopped. */
6955 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 6956 remove_stop_reply_of_remote_state, &param);
722247f1 6957}
74531fed 6958
722247f1
YQ
6959/* A parameter to pass data in and out. */
6960
6961static int
6962remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
6963 QUEUE_ITER (stop_reply_p) *iter,
6964 stop_reply_p event,
6965 void *data)
6966{
19ba03f4
SM
6967 struct queue_iter_param *param = (struct queue_iter_param *) data;
6968 ptid_t *ptid = (ptid_t *) param->input;
722247f1
YQ
6969
6970 if (ptid_match (event->ptid, *ptid))
6971 {
6972 param->output = event;
6973 QUEUE_remove_elem (stop_reply_p, q, iter);
6974 return 0;
c8e38a49 6975 }
722247f1
YQ
6976
6977 return 1;
74531fed 6978}
43ff13b4 6979
722247f1
YQ
6980/* Remove the first reply in 'stop_reply_queue' which matches
6981 PTID. */
2e9f7625 6982
722247f1
YQ
6983static struct stop_reply *
6984remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 6985{
722247f1
YQ
6986 struct queue_iter_param param;
6987
6988 param.input = &ptid;
6989 param.output = NULL;
6990
6991 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6992 remote_notif_remove_once_on_match, &param);
6993 if (notif_debug)
6994 fprintf_unfiltered (gdb_stdlog,
6995 "notif: discard queued event: 'Stop' in %s\n",
6996 target_pid_to_str (ptid));
a744cf53 6997
722247f1 6998 return param.output;
74531fed 6999}
75c99385 7000
74531fed
PA
7001/* Look for a queued stop reply belonging to PTID. If one is found,
7002 remove it from the queue, and return it. Returns NULL if none is
7003 found. If there are still queued events left to process, tell the
7004 event loop to get back to target_wait soon. */
e24a49d8 7005
74531fed
PA
7006static struct stop_reply *
7007queued_stop_reply (ptid_t ptid)
7008{
722247f1 7009 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 7010
722247f1 7011 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
7012 /* There's still at least an event left. */
7013 mark_async_event_handler (remote_async_inferior_event_token);
7014
722247f1 7015 return r;
74531fed
PA
7016}
7017
7018/* Push a fully parsed stop reply in the stop reply queue. Since we
7019 know that we now have at least one queued event left to pass to the
7020 core side, tell the event loop to get back to target_wait soon. */
7021
7022static void
7023push_stop_reply (struct stop_reply *new_event)
7024{
722247f1 7025 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 7026
722247f1
YQ
7027 if (notif_debug)
7028 fprintf_unfiltered (gdb_stdlog,
7029 "notif: push 'Stop' %s to queue %d\n",
7030 target_pid_to_str (new_event->ptid),
7031 QUEUE_length (stop_reply_p,
7032 stop_reply_queue));
74531fed
PA
7033
7034 mark_async_event_handler (remote_async_inferior_event_token);
7035}
7036
722247f1
YQ
7037static int
7038stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
7039 QUEUE_ITER (stop_reply_p) *iter,
7040 struct stop_reply *event,
7041 void *data)
7042{
19ba03f4 7043 ptid_t *ptid = (ptid_t *) data;
722247f1
YQ
7044
7045 return !(ptid_equal (*ptid, event->ptid)
7046 && event->ws.kind == TARGET_WAITKIND_STOPPED);
7047}
7048
74531fed
PA
7049/* Returns true if we have a stop reply for PTID. */
7050
7051static int
7052peek_stop_reply (ptid_t ptid)
7053{
722247f1
YQ
7054 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
7055 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
7056}
7057
26d56a93
SL
7058/* Helper for remote_parse_stop_reply. Return nonzero if the substring
7059 starting with P and ending with PEND matches PREFIX. */
7060
7061static int
7062strprefix (const char *p, const char *pend, const char *prefix)
7063{
7064 for ( ; p < pend; p++, prefix++)
7065 if (*p != *prefix)
7066 return 0;
7067 return *prefix == '\0';
7068}
7069
74531fed
PA
7070/* Parse the stop reply in BUF. Either the function succeeds, and the
7071 result is stored in EVENT, or throws an error. */
7072
7073static void
7074remote_parse_stop_reply (char *buf, struct stop_reply *event)
7075{
5cd63fda 7076 remote_arch_state *rsa = NULL;
74531fed 7077 ULONGEST addr;
256642e8 7078 const char *p;
94585166 7079 int skipregs = 0;
74531fed
PA
7080
7081 event->ptid = null_ptid;
bcc75809 7082 event->rs = get_remote_state ();
74531fed
PA
7083 event->ws.kind = TARGET_WAITKIND_IGNORE;
7084 event->ws.value.integer = 0;
f7e6eed5 7085 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 7086 event->regcache = NULL;
dc146f7c 7087 event->core = -1;
74531fed
PA
7088
7089 switch (buf[0])
7090 {
7091 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
7092 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7093 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7094 ss = signal number
7095 n... = register number
7096 r... = register contents
7097 */
7098
7099 p = &buf[3]; /* after Txx */
7100 while (*p)
7101 {
256642e8 7102 const char *p1;
cea39f65 7103 int fieldsize;
43ff13b4 7104
1f10ba14
PA
7105 p1 = strchr (p, ':');
7106 if (p1 == NULL)
7107 error (_("Malformed packet(a) (missing colon): %s\n\
7108Packet: '%s'\n"),
7109 p, buf);
7110 if (p == p1)
7111 error (_("Malformed packet(a) (missing register number): %s\n\
7112Packet: '%s'\n"),
7113 p, buf);
3c3bea1c 7114
1f10ba14
PA
7115 /* Some "registers" are actually extended stop information.
7116 Note if you're adding a new entry here: GDB 7.9 and
7117 earlier assume that all register "numbers" that start
7118 with an hex digit are real register numbers. Make sure
7119 the server only sends such a packet if it knows the
7120 client understands it. */
c8e38a49 7121
26d56a93 7122 if (strprefix (p, p1, "thread"))
1f10ba14 7123 event->ptid = read_ptid (++p1, &p);
82075af2
JS
7124 else if (strprefix (p, p1, "syscall_entry"))
7125 {
7126 ULONGEST sysno;
7127
7128 event->ws.kind = TARGET_WAITKIND_SYSCALL_ENTRY;
7129 p = unpack_varlen_hex (++p1, &sysno);
7130 event->ws.value.syscall_number = (int) sysno;
7131 }
7132 else if (strprefix (p, p1, "syscall_return"))
7133 {
7134 ULONGEST sysno;
7135
7136 event->ws.kind = TARGET_WAITKIND_SYSCALL_RETURN;
7137 p = unpack_varlen_hex (++p1, &sysno);
7138 event->ws.value.syscall_number = (int) sysno;
7139 }
26d56a93
SL
7140 else if (strprefix (p, p1, "watch")
7141 || strprefix (p, p1, "rwatch")
7142 || strprefix (p, p1, "awatch"))
cea39f65 7143 {
f7e6eed5 7144 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
7145 p = unpack_varlen_hex (++p1, &addr);
7146 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 7147 }
26d56a93 7148 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
7149 {
7150 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
7151
7152 /* Make sure the stub doesn't forget to indicate support
7153 with qSupported. */
7154 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
7155 error (_("Unexpected swbreak stop reason"));
7156
7157 /* The value part is documented as "must be empty",
7158 though we ignore it, in case we ever decide to make
7159 use of it in a backward compatible way. */
8424cc97 7160 p = strchrnul (p1 + 1, ';');
f7e6eed5 7161 }
26d56a93 7162 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
7163 {
7164 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
7165
7166 /* Make sure the stub doesn't forget to indicate support
7167 with qSupported. */
7168 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
7169 error (_("Unexpected hwbreak stop reason"));
7170
7171 /* See above. */
8424cc97 7172 p = strchrnul (p1 + 1, ';');
f7e6eed5 7173 }
26d56a93 7174 else if (strprefix (p, p1, "library"))
cea39f65 7175 {
1f10ba14 7176 event->ws.kind = TARGET_WAITKIND_LOADED;
8424cc97 7177 p = strchrnul (p1 + 1, ';');
1f10ba14 7178 }
26d56a93 7179 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
7180 {
7181 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
7182 /* p1 will indicate "begin" or "end", but it makes
7183 no difference for now, so ignore it. */
8424cc97 7184 p = strchrnul (p1 + 1, ';');
1f10ba14 7185 }
26d56a93 7186 else if (strprefix (p, p1, "core"))
1f10ba14
PA
7187 {
7188 ULONGEST c;
a744cf53 7189
1f10ba14
PA
7190 p = unpack_varlen_hex (++p1, &c);
7191 event->core = c;
cea39f65 7192 }
26d56a93 7193 else if (strprefix (p, p1, "fork"))
de0d863e
DB
7194 {
7195 event->ws.value.related_pid = read_ptid (++p1, &p);
7196 event->ws.kind = TARGET_WAITKIND_FORKED;
7197 }
26d56a93 7198 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
7199 {
7200 event->ws.value.related_pid = read_ptid (++p1, &p);
7201 event->ws.kind = TARGET_WAITKIND_VFORKED;
7202 }
26d56a93 7203 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
7204 {
7205 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
8424cc97 7206 p = strchrnul (p1 + 1, ';');
c269dbdb 7207 }
6ab24463 7208 else if (strprefix (p, p1, "exec"))
94585166
DB
7209 {
7210 ULONGEST ignored;
7211 char pathname[PATH_MAX];
7212 int pathlen;
7213
7214 /* Determine the length of the execd pathname. */
7215 p = unpack_varlen_hex (++p1, &ignored);
7216 pathlen = (p - p1) / 2;
7217
7218 /* Save the pathname for event reporting and for
7219 the next run command. */
7220 hex2bin (p1, (gdb_byte *) pathname, pathlen);
7221 pathname[pathlen] = '\0';
7222
7223 /* This is freed during event handling. */
7224 event->ws.value.execd_pathname = xstrdup (pathname);
7225 event->ws.kind = TARGET_WAITKIND_EXECD;
7226
7227 /* Skip the registers included in this packet, since
7228 they may be for an architecture different from the
7229 one used by the original program. */
7230 skipregs = 1;
7231 }
65706a29
PA
7232 else if (strprefix (p, p1, "create"))
7233 {
7234 event->ws.kind = TARGET_WAITKIND_THREAD_CREATED;
8424cc97 7235 p = strchrnul (p1 + 1, ';');
65706a29 7236 }
cea39f65
MS
7237 else
7238 {
1f10ba14 7239 ULONGEST pnum;
256642e8 7240 const char *p_temp;
1f10ba14 7241
94585166
DB
7242 if (skipregs)
7243 {
8424cc97 7244 p = strchrnul (p1 + 1, ';');
94585166
DB
7245 p++;
7246 continue;
7247 }
7248
1f10ba14
PA
7249 /* Maybe a real ``P'' register number. */
7250 p_temp = unpack_varlen_hex (p, &pnum);
7251 /* If the first invalid character is the colon, we got a
7252 register number. Otherwise, it's an unknown stop
7253 reason. */
7254 if (p_temp == p1)
7255 {
5cd63fda
PA
7256 /* If we haven't parsed the event's thread yet, find
7257 it now, in order to find the architecture of the
7258 reported expedited registers. */
7259 if (event->ptid == null_ptid)
7260 {
7261 const char *thr = strstr (p1 + 1, ";thread:");
7262 if (thr != NULL)
7263 event->ptid = read_ptid (thr + strlen (";thread:"),
7264 NULL);
7265 else
3cada740
PA
7266 {
7267 /* Either the current thread hasn't changed,
7268 or the inferior is not multi-threaded.
7269 The event must be for the thread we last
7270 set as (or learned as being) current. */
7271 event->ptid = event->rs->general_thread;
7272 }
5cd63fda
PA
7273 }
7274
7275 if (rsa == NULL)
7276 {
7277 inferior *inf = (event->ptid == null_ptid
7278 ? NULL
7279 : find_inferior_ptid (event->ptid));
7280 /* If this is the first time we learn anything
7281 about this process, skip the registers
7282 included in this packet, since we don't yet
7283 know which architecture to use to parse them.
7284 We'll determine the architecture later when
7285 we process the stop reply and retrieve the
7286 target description, via
7287 remote_notice_new_inferior ->
7288 post_create_inferior. */
7289 if (inf == NULL)
7290 {
7291 p = strchrnul (p1 + 1, ';');
7292 p++;
7293 continue;
7294 }
7295
7296 event->arch = inf->gdbarch;
9d6eea31 7297 rsa = event->rs->get_remote_arch_state (event->arch);
5cd63fda
PA
7298 }
7299
7300 packet_reg *reg
7301 = packet_reg_from_pnum (event->arch, rsa, pnum);
1f10ba14 7302 cached_reg_t cached_reg;
43ff13b4 7303
1f10ba14
PA
7304 if (reg == NULL)
7305 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 7306Packet: '%s'\n"),
1f10ba14 7307 hex_string (pnum), p, buf);
c8e38a49 7308
1f10ba14 7309 cached_reg.num = reg->regnum;
d1dff226 7310 cached_reg.data = (gdb_byte *)
5cd63fda 7311 xmalloc (register_size (event->arch, reg->regnum));
4100683b 7312
1f10ba14
PA
7313 p = p1 + 1;
7314 fieldsize = hex2bin (p, cached_reg.data,
5cd63fda 7315 register_size (event->arch, reg->regnum));
1f10ba14 7316 p += 2 * fieldsize;
5cd63fda 7317 if (fieldsize < register_size (event->arch, reg->regnum))
1f10ba14 7318 warning (_("Remote reply is too short: %s"), buf);
74531fed 7319
1f10ba14
PA
7320 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
7321 }
7322 else
7323 {
7324 /* Not a number. Silently skip unknown optional
7325 info. */
8424cc97 7326 p = strchrnul (p1 + 1, ';');
1f10ba14 7327 }
cea39f65 7328 }
c8e38a49 7329
cea39f65
MS
7330 if (*p != ';')
7331 error (_("Remote register badly formatted: %s\nhere: %s"),
7332 buf, p);
7333 ++p;
7334 }
5b5596ff
PA
7335
7336 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
7337 break;
7338
c8e38a49
PA
7339 /* fall through */
7340 case 'S': /* Old style status, just signal only. */
3a09da41
PA
7341 {
7342 int sig;
7343
7344 event->ws.kind = TARGET_WAITKIND_STOPPED;
7345 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
7346 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
7347 event->ws.value.sig = (enum gdb_signal) sig;
7348 else
7349 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
7350 }
c8e38a49 7351 break;
65706a29
PA
7352 case 'w': /* Thread exited. */
7353 {
256642e8 7354 const char *p;
65706a29
PA
7355 ULONGEST value;
7356
7357 event->ws.kind = TARGET_WAITKIND_THREAD_EXITED;
7358 p = unpack_varlen_hex (&buf[1], &value);
7359 event->ws.value.integer = value;
7360 if (*p != ';')
7361 error (_("stop reply packet badly formatted: %s"), buf);
974eac9d 7362 event->ptid = read_ptid (++p, NULL);
65706a29
PA
7363 break;
7364 }
c8e38a49
PA
7365 case 'W': /* Target exited. */
7366 case 'X':
7367 {
256642e8 7368 const char *p;
c8e38a49
PA
7369 int pid;
7370 ULONGEST value;
82f73884 7371
c8e38a49
PA
7372 /* GDB used to accept only 2 hex chars here. Stubs should
7373 only send more if they detect GDB supports multi-process
7374 support. */
7375 p = unpack_varlen_hex (&buf[1], &value);
82f73884 7376
c8e38a49
PA
7377 if (buf[0] == 'W')
7378 {
7379 /* The remote process exited. */
74531fed
PA
7380 event->ws.kind = TARGET_WAITKIND_EXITED;
7381 event->ws.value.integer = value;
c8e38a49
PA
7382 }
7383 else
7384 {
7385 /* The remote process exited with a signal. */
74531fed 7386 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
7387 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
7388 event->ws.value.sig = (enum gdb_signal) value;
7389 else
7390 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 7391 }
82f73884 7392
c8e38a49
PA
7393 /* If no process is specified, assume inferior_ptid. */
7394 pid = ptid_get_pid (inferior_ptid);
7395 if (*p == '\0')
7396 ;
7397 else if (*p == ';')
7398 {
7399 p++;
7400
0b24eb2d 7401 if (*p == '\0')
82f73884 7402 ;
61012eef 7403 else if (startswith (p, "process:"))
82f73884 7404 {
c8e38a49 7405 ULONGEST upid;
a744cf53 7406
c8e38a49
PA
7407 p += sizeof ("process:") - 1;
7408 unpack_varlen_hex (p, &upid);
7409 pid = upid;
82f73884
PA
7410 }
7411 else
7412 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 7413 }
c8e38a49
PA
7414 else
7415 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
7416 event->ptid = pid_to_ptid (pid);
7417 }
7418 break;
f2faf941
PA
7419 case 'N':
7420 event->ws.kind = TARGET_WAITKIND_NO_RESUMED;
7421 event->ptid = minus_one_ptid;
7422 break;
74531fed
PA
7423 }
7424
6efcd9a8 7425 if (target_is_non_stop_p () && ptid_equal (event->ptid, null_ptid))
74531fed
PA
7426 error (_("No process or thread specified in stop reply: %s"), buf);
7427}
7428
722247f1
YQ
7429/* When the stub wants to tell GDB about a new notification reply, it
7430 sends a notification (%Stop, for example). Those can come it at
7431 any time, hence, we have to make sure that any pending
7432 putpkt/getpkt sequence we're making is finished, before querying
7433 the stub for more events with the corresponding ack command
7434 (vStopped, for example). E.g., if we started a vStopped sequence
7435 immediately upon receiving the notification, something like this
7436 could happen:
74531fed
PA
7437
7438 1.1) --> Hg 1
7439 1.2) <-- OK
7440 1.3) --> g
7441 1.4) <-- %Stop
7442 1.5) --> vStopped
7443 1.6) <-- (registers reply to step #1.3)
7444
7445 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7446 query.
7447
796cb314 7448 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
7449 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7450 doing whatever we were doing:
7451
7452 2.1) --> Hg 1
7453 2.2) <-- OK
7454 2.3) --> g
7455 2.4) <-- %Stop
7456 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7457 2.5) <-- (registers reply to step #2.3)
7458
7459 Eventualy after step #2.5, we return to the event loop, which
7460 notices there's an event on the
7461 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7462 associated callback --- the function below. At this point, we're
7463 always safe to start a vStopped sequence. :
7464
7465 2.6) --> vStopped
7466 2.7) <-- T05 thread:2
7467 2.8) --> vStopped
7468 2.9) --> OK
7469*/
7470
722247f1
YQ
7471void
7472remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
7473{
7474 struct remote_state *rs = get_remote_state ();
74531fed 7475
f48ff2a7 7476 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 7477 {
722247f1
YQ
7478 if (notif_debug)
7479 fprintf_unfiltered (gdb_stdlog,
7480 "notif: process: '%s' ack pending event\n",
7481 nc->name);
74531fed 7482
722247f1 7483 /* acknowledge */
f48ff2a7
YQ
7484 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
7485 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
7486
7487 while (1)
7488 {
7489 getpkt (&rs->buf, &rs->buf_size, 0);
7490 if (strcmp (rs->buf, "OK") == 0)
7491 break;
7492 else
722247f1 7493 remote_notif_ack (nc, rs->buf);
74531fed
PA
7494 }
7495 }
722247f1
YQ
7496 else
7497 {
7498 if (notif_debug)
7499 fprintf_unfiltered (gdb_stdlog,
7500 "notif: process: '%s' no pending reply\n",
7501 nc->name);
7502 }
74531fed
PA
7503}
7504
74531fed
PA
7505/* Called when it is decided that STOP_REPLY holds the info of the
7506 event that is to be returned to the core. This function always
7507 destroys STOP_REPLY. */
7508
7509static ptid_t
7510process_stop_reply (struct stop_reply *stop_reply,
7511 struct target_waitstatus *status)
7512{
7513 ptid_t ptid;
7514
7515 *status = stop_reply->ws;
7516 ptid = stop_reply->ptid;
7517
7518 /* If no thread/process was reported by the stub, assume the current
7519 inferior. */
7520 if (ptid_equal (ptid, null_ptid))
7521 ptid = inferior_ptid;
7522
5f3563ea 7523 if (status->kind != TARGET_WAITKIND_EXITED
f2faf941
PA
7524 && status->kind != TARGET_WAITKIND_SIGNALLED
7525 && status->kind != TARGET_WAITKIND_NO_RESUMED)
74531fed 7526 {
5f3563ea
PA
7527 /* Expedited registers. */
7528 if (stop_reply->regcache)
7529 {
217f1f79 7530 struct regcache *regcache
5cd63fda 7531 = get_thread_arch_regcache (ptid, stop_reply->arch);
5f3563ea
PA
7532 cached_reg_t *reg;
7533 int ix;
7534
7535 for (ix = 0;
d1dff226 7536 VEC_iterate (cached_reg_t, stop_reply->regcache, ix, reg);
5f3563ea 7537 ix++)
d1dff226 7538 {
217f1f79 7539 regcache_raw_supply (regcache, reg->num, reg->data);
d1dff226
AH
7540 xfree (reg->data);
7541 }
7542
5f3563ea
PA
7543 VEC_free (cached_reg_t, stop_reply->regcache);
7544 }
74531fed 7545
1941c569 7546 remote_notice_new_inferior (ptid, 0);
7aabaf9d 7547 remote_thread_info *remote_thr = get_remote_thread_info (ptid);
799a2abe
PA
7548 remote_thr->core = stop_reply->core;
7549 remote_thr->stop_reason = stop_reply->stop_reason;
7550 remote_thr->watch_data_address = stop_reply->watch_data_address;
85ad3aaf 7551 remote_thr->vcont_resumed = 0;
74531fed
PA
7552 }
7553
74531fed
PA
7554 stop_reply_xfree (stop_reply);
7555 return ptid;
7556}
7557
7558/* The non-stop mode version of target_wait. */
7559
7560static ptid_t
47608cb1 7561remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7562{
7563 struct remote_state *rs = get_remote_state ();
74531fed
PA
7564 struct stop_reply *stop_reply;
7565 int ret;
fee9eda9 7566 int is_notif = 0;
74531fed
PA
7567
7568 /* If in non-stop mode, get out of getpkt even if a
7569 notification is received. */
7570
7571 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7572 0 /* forever */, &is_notif);
74531fed
PA
7573 while (1)
7574 {
fee9eda9 7575 if (ret != -1 && !is_notif)
74531fed
PA
7576 switch (rs->buf[0])
7577 {
7578 case 'E': /* Error of some sort. */
7579 /* We're out of sync with the target now. Did it continue
7580 or not? We can't tell which thread it was in non-stop,
7581 so just ignore this. */
7582 warning (_("Remote failure reply: %s"), rs->buf);
7583 break;
7584 case 'O': /* Console output. */
7585 remote_console_output (rs->buf + 1);
7586 break;
7587 default:
7588 warning (_("Invalid remote reply: %s"), rs->buf);
7589 break;
7590 }
7591
7592 /* Acknowledge a pending stop reply that may have arrived in the
7593 mean time. */
f48ff2a7 7594 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 7595 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
7596
7597 /* If indeed we noticed a stop reply, we're done. */
7598 stop_reply = queued_stop_reply (ptid);
7599 if (stop_reply != NULL)
7600 return process_stop_reply (stop_reply, status);
7601
47608cb1 7602 /* Still no event. If we're just polling for an event, then
74531fed 7603 return to the event loop. */
47608cb1 7604 if (options & TARGET_WNOHANG)
74531fed
PA
7605 {
7606 status->kind = TARGET_WAITKIND_IGNORE;
7607 return minus_one_ptid;
7608 }
7609
47608cb1 7610 /* Otherwise do a blocking wait. */
74531fed 7611 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7612 1 /* forever */, &is_notif);
74531fed
PA
7613 }
7614}
7615
7616/* Wait until the remote machine stops, then return, storing status in
7617 STATUS just as `wait' would. */
7618
7619static ptid_t
47608cb1 7620remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7621{
7622 struct remote_state *rs = get_remote_state ();
74531fed 7623 ptid_t event_ptid = null_ptid;
cea39f65 7624 char *buf;
74531fed
PA
7625 struct stop_reply *stop_reply;
7626
47608cb1
PA
7627 again:
7628
74531fed
PA
7629 status->kind = TARGET_WAITKIND_IGNORE;
7630 status->value.integer = 0;
7631
7632 stop_reply = queued_stop_reply (ptid);
7633 if (stop_reply != NULL)
7634 return process_stop_reply (stop_reply, status);
7635
7636 if (rs->cached_wait_status)
7637 /* Use the cached wait status, but only once. */
7638 rs->cached_wait_status = 0;
7639 else
7640 {
7641 int ret;
722247f1 7642 int is_notif;
567420d1
PA
7643 int forever = ((options & TARGET_WNOHANG) == 0
7644 && wait_forever_enabled_p);
7645
7646 if (!rs->waiting_for_stop_reply)
7647 {
7648 status->kind = TARGET_WAITKIND_NO_RESUMED;
7649 return minus_one_ptid;
7650 }
74531fed 7651
74531fed
PA
7652 /* FIXME: cagney/1999-09-27: If we're in async mode we should
7653 _never_ wait for ever -> test on target_is_async_p().
7654 However, before we do that we need to ensure that the caller
7655 knows how to take the target into/out of async mode. */
722247f1 7656 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 7657 forever, &is_notif);
722247f1
YQ
7658
7659 /* GDB gets a notification. Return to core as this event is
7660 not interesting. */
7661 if (ret != -1 && is_notif)
7662 return minus_one_ptid;
567420d1
PA
7663
7664 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
7665 return minus_one_ptid;
74531fed
PA
7666 }
7667
7668 buf = rs->buf;
7669
3a29589a
DJ
7670 /* Assume that the target has acknowledged Ctrl-C unless we receive
7671 an 'F' or 'O' packet. */
7672 if (buf[0] != 'F' && buf[0] != 'O')
7673 rs->ctrlc_pending_p = 0;
7674
74531fed
PA
7675 switch (buf[0])
7676 {
7677 case 'E': /* Error of some sort. */
7678 /* We're out of sync with the target now. Did it continue or
7679 not? Not is more likely, so report a stop. */
29090fb6
LM
7680 rs->waiting_for_stop_reply = 0;
7681
74531fed
PA
7682 warning (_("Remote failure reply: %s"), buf);
7683 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 7684 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
7685 break;
7686 case 'F': /* File-I/O request. */
e42e5352
YQ
7687 /* GDB may access the inferior memory while handling the File-I/O
7688 request, but we don't want GDB accessing memory while waiting
7689 for a stop reply. See the comments in putpkt_binary. Set
7690 waiting_for_stop_reply to 0 temporarily. */
7691 rs->waiting_for_stop_reply = 0;
3a29589a
DJ
7692 remote_fileio_request (buf, rs->ctrlc_pending_p);
7693 rs->ctrlc_pending_p = 0;
e42e5352
YQ
7694 /* GDB handled the File-I/O request, and the target is running
7695 again. Keep waiting for events. */
7696 rs->waiting_for_stop_reply = 1;
74531fed 7697 break;
f2faf941 7698 case 'N': case 'T': case 'S': case 'X': case 'W':
74531fed 7699 {
29090fb6
LM
7700 struct stop_reply *stop_reply;
7701
7702 /* There is a stop reply to handle. */
7703 rs->waiting_for_stop_reply = 0;
7704
7705 stop_reply
722247f1
YQ
7706 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
7707 rs->buf);
74531fed 7708
74531fed 7709 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
7710 break;
7711 }
7712 case 'O': /* Console output. */
7713 remote_console_output (buf + 1);
c8e38a49
PA
7714 break;
7715 case '\0':
b73be471 7716 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
7717 {
7718 /* Zero length reply means that we tried 'S' or 'C' and the
7719 remote system doesn't support it. */
223ffa71 7720 target_terminal::ours_for_output ();
c8e38a49
PA
7721 printf_filtered
7722 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
7723 gdb_signal_to_name (rs->last_sent_signal));
7724 rs->last_sent_signal = GDB_SIGNAL_0;
223ffa71 7725 target_terminal::inferior ();
c8e38a49 7726
f5c4fcd9
TT
7727 strcpy (buf, rs->last_sent_step ? "s" : "c");
7728 putpkt (buf);
c8e38a49 7729 break;
43ff13b4 7730 }
86a73007 7731 /* fallthrough */
c8e38a49
PA
7732 default:
7733 warning (_("Invalid remote reply: %s"), buf);
c8e38a49 7734 break;
43ff13b4 7735 }
c8e38a49 7736
f2faf941
PA
7737 if (status->kind == TARGET_WAITKIND_NO_RESUMED)
7738 return minus_one_ptid;
7739 else if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
7740 {
7741 /* Nothing interesting happened. If we're doing a non-blocking
7742 poll, we're done. Otherwise, go back to waiting. */
7743 if (options & TARGET_WNOHANG)
7744 return minus_one_ptid;
7745 else
7746 goto again;
7747 }
74531fed
PA
7748 else if (status->kind != TARGET_WAITKIND_EXITED
7749 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
7750 {
7751 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 7752 record_currthread (rs, event_ptid);
82f73884
PA
7753 else
7754 event_ptid = inferior_ptid;
43ff13b4 7755 }
74531fed
PA
7756 else
7757 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 7758 record_currthread (rs, minus_one_ptid);
79d7f229 7759
82f73884 7760 return event_ptid;
43ff13b4
JM
7761}
7762
74531fed
PA
7763/* Wait until the remote machine stops, then return, storing status in
7764 STATUS just as `wait' would. */
7765
f6ac5f3d
PA
7766ptid_t
7767remote_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
7768{
7769 ptid_t event_ptid;
7770
6efcd9a8 7771 if (target_is_non_stop_p ())
47608cb1 7772 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 7773 else
47608cb1 7774 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 7775
d9d41e78 7776 if (target_is_async_p ())
c8e38a49 7777 {
74531fed
PA
7778 /* If there are are events left in the queue tell the event loop
7779 to return here. */
722247f1 7780 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 7781 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 7782 }
c8e38a49
PA
7783
7784 return event_ptid;
7785}
7786
74ca34ce 7787/* Fetch a single register using a 'p' packet. */
c906108c 7788
b96ec7ac 7789static int
56be3814 7790fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac 7791{
ac7936df 7792 struct gdbarch *gdbarch = regcache->arch ();
b96ec7ac 7793 struct remote_state *rs = get_remote_state ();
2e9f7625 7794 char *buf, *p;
9890e433 7795 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
b96ec7ac
AC
7796 int i;
7797
4082afcc 7798 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
7799 return 0;
7800
7801 if (reg->pnum == -1)
7802 return 0;
7803
2e9f7625 7804 p = rs->buf;
fcad0fa4 7805 *p++ = 'p';
74ca34ce 7806 p += hexnumstr (p, reg->pnum);
fcad0fa4 7807 *p++ = '\0';
1f4437a4
MS
7808 putpkt (rs->buf);
7809 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 7810
2e9f7625
DJ
7811 buf = rs->buf;
7812
74ca34ce
DJ
7813 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
7814 {
7815 case PACKET_OK:
7816 break;
7817 case PACKET_UNKNOWN:
7818 return 0;
7819 case PACKET_ERROR:
27a9c0bf 7820 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
ac7936df 7821 gdbarch_register_name (regcache->arch (),
27a9c0bf
MS
7822 reg->regnum),
7823 buf);
74ca34ce 7824 }
3f9a994c
JB
7825
7826 /* If this register is unfetchable, tell the regcache. */
7827 if (buf[0] == 'x')
8480adf2 7828 {
56be3814 7829 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 7830 return 1;
b96ec7ac 7831 }
b96ec7ac 7832
3f9a994c
JB
7833 /* Otherwise, parse and supply the value. */
7834 p = buf;
7835 i = 0;
7836 while (p[0] != 0)
7837 {
7838 if (p[1] == 0)
74ca34ce 7839 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
7840
7841 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
7842 p += 2;
7843 }
56be3814 7844 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 7845 return 1;
b96ec7ac
AC
7846}
7847
74ca34ce
DJ
7848/* Fetch the registers included in the target's 'g' packet. */
7849
29709017
DJ
7850static int
7851send_g_packet (void)
c906108c 7852{
d01949b6 7853 struct remote_state *rs = get_remote_state ();
cea39f65 7854 int buf_len;
c906108c 7855
bba74b36 7856 xsnprintf (rs->buf, get_remote_packet_size (), "g");
b75abf5b
AK
7857 putpkt (rs->buf);
7858 getpkt (&rs->buf, &rs->buf_size, 0);
7859 if (packet_check_result (rs->buf) == PACKET_ERROR)
7860 error (_("Could not read registers; remote failure reply '%s'"),
7861 rs->buf);
c906108c 7862
29709017
DJ
7863 /* We can get out of synch in various cases. If the first character
7864 in the buffer is not a hex character, assume that has happened
7865 and try to fetch another packet to read. */
7866 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
7867 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
7868 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
7869 && rs->buf[0] != 'x') /* New: unavailable register value. */
7870 {
7871 if (remote_debug)
7872 fprintf_unfiltered (gdb_stdlog,
7873 "Bad register packet; fetching a new packet\n");
7874 getpkt (&rs->buf, &rs->buf_size, 0);
7875 }
7876
74ca34ce
DJ
7877 buf_len = strlen (rs->buf);
7878
7879 /* Sanity check the received packet. */
7880 if (buf_len % 2 != 0)
7881 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
7882
7883 return buf_len / 2;
7884}
7885
7886static void
56be3814 7887process_g_packet (struct regcache *regcache)
29709017 7888{
ac7936df 7889 struct gdbarch *gdbarch = regcache->arch ();
29709017 7890 struct remote_state *rs = get_remote_state ();
9d6eea31 7891 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
29709017
DJ
7892 int i, buf_len;
7893 char *p;
7894 char *regs;
7895
7896 buf_len = strlen (rs->buf);
7897
7898 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce 7899 if (buf_len > 2 * rsa->sizeof_g_packet)
fc809827
SM
7900 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
7901 "bytes): %s"), rsa->sizeof_g_packet, buf_len / 2, rs->buf);
74ca34ce
DJ
7902
7903 /* Save the size of the packet sent to us by the target. It is used
7904 as a heuristic when determining the max size of packets that the
7905 target can safely receive. */
7906 if (rsa->actual_register_packet_size == 0)
7907 rsa->actual_register_packet_size = buf_len;
7908
7909 /* If this is smaller than we guessed the 'g' packet would be,
7910 update our records. A 'g' reply that doesn't include a register's
7911 value implies either that the register is not available, or that
7912 the 'p' packet must be used. */
7913 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 7914 {
9dc193c3 7915 long sizeof_g_packet = buf_len / 2;
74ca34ce 7916
4a22f64d 7917 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 7918 {
9dc193c3
LF
7919 long offset = rsa->regs[i].offset;
7920 long reg_size = register_size (gdbarch, i);
7921
74ca34ce
DJ
7922 if (rsa->regs[i].pnum == -1)
7923 continue;
7924
9dc193c3 7925 if (offset >= sizeof_g_packet)
74ca34ce 7926 rsa->regs[i].in_g_packet = 0;
9dc193c3
LF
7927 else if (offset + reg_size > sizeof_g_packet)
7928 error (_("Truncated register %d in remote 'g' packet"), i);
b96ec7ac 7929 else
74ca34ce 7930 rsa->regs[i].in_g_packet = 1;
b96ec7ac 7931 }
9dc193c3
LF
7932
7933 /* Looks valid enough, we can assume this is the correct length
7934 for a 'g' packet. It's important not to adjust
7935 rsa->sizeof_g_packet if we have truncated registers otherwise
7936 this "if" won't be run the next time the method is called
7937 with a packet of the same size and one of the internal errors
7938 below will trigger instead. */
7939 rsa->sizeof_g_packet = sizeof_g_packet;
74ca34ce 7940 }
b323314b 7941
224c3ddb 7942 regs = (char *) alloca (rsa->sizeof_g_packet);
c906108c
SS
7943
7944 /* Unimplemented registers read as all bits zero. */
ea9c271d 7945 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 7946
c906108c
SS
7947 /* Reply describes registers byte by byte, each byte encoded as two
7948 hex characters. Suck them all up, then supply them to the
7949 register cacheing/storage mechanism. */
7950
74ca34ce 7951 p = rs->buf;
ea9c271d 7952 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 7953 {
74ca34ce
DJ
7954 if (p[0] == 0 || p[1] == 0)
7955 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
7956 internal_error (__FILE__, __LINE__,
9b20d036 7957 _("unexpected end of 'g' packet reply"));
74ca34ce 7958
c906108c 7959 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 7960 regs[i] = 0; /* 'x' */
c906108c
SS
7961 else
7962 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
7963 p += 2;
7964 }
7965
a744cf53
MS
7966 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
7967 {
7968 struct packet_reg *r = &rsa->regs[i];
9dc193c3 7969 long reg_size = register_size (gdbarch, i);
a744cf53
MS
7970
7971 if (r->in_g_packet)
7972 {
9dc193c3 7973 if ((r->offset + reg_size) * 2 > strlen (rs->buf))
a744cf53
MS
7974 /* This shouldn't happen - we adjusted in_g_packet above. */
7975 internal_error (__FILE__, __LINE__,
9b20d036 7976 _("unexpected end of 'g' packet reply"));
a744cf53
MS
7977 else if (rs->buf[r->offset * 2] == 'x')
7978 {
7979 gdb_assert (r->offset * 2 < strlen (rs->buf));
7980 /* The register isn't available, mark it as such (at
7981 the same time setting the value to zero). */
7982 regcache_raw_supply (regcache, r->regnum, NULL);
7983 }
7984 else
7985 regcache_raw_supply (regcache, r->regnum,
7986 regs + r->offset);
7987 }
7988 }
c906108c
SS
7989}
7990
29709017 7991static void
56be3814 7992fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
7993{
7994 send_g_packet ();
56be3814 7995 process_g_packet (regcache);
29709017
DJ
7996}
7997
e6e4e701
PA
7998/* Make the remote selected traceframe match GDB's selected
7999 traceframe. */
8000
8001static void
8002set_remote_traceframe (void)
8003{
8004 int newnum;
262e1174 8005 struct remote_state *rs = get_remote_state ();
e6e4e701 8006
262e1174 8007 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
8008 return;
8009
8010 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 8011 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
8012
8013 newnum = target_trace_find (tfind_number,
8014 get_traceframe_number (), 0, 0, NULL);
8015
8016 /* Should not happen. If it does, all bets are off. */
8017 if (newnum != get_traceframe_number ())
8018 warning (_("could not set remote traceframe"));
8019}
8020
f6ac5f3d
PA
8021void
8022remote_target::fetch_registers (struct regcache *regcache, int regnum)
74ca34ce 8023{
ac7936df 8024 struct gdbarch *gdbarch = regcache->arch ();
9d6eea31
PA
8025 struct remote_state *rs = get_remote_state ();
8026 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
74ca34ce
DJ
8027 int i;
8028
e6e4e701 8029 set_remote_traceframe ();
bcc0c096 8030 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
8031
8032 if (regnum >= 0)
8033 {
5cd63fda 8034 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 8035
74ca34ce
DJ
8036 gdb_assert (reg != NULL);
8037
8038 /* If this register might be in the 'g' packet, try that first -
8039 we are likely to read more than one register. If this is the
8040 first 'g' packet, we might be overly optimistic about its
8041 contents, so fall back to 'p'. */
8042 if (reg->in_g_packet)
8043 {
56be3814 8044 fetch_registers_using_g (regcache);
74ca34ce
DJ
8045 if (reg->in_g_packet)
8046 return;
8047 }
8048
56be3814 8049 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
8050 return;
8051
8052 /* This register is not available. */
56be3814 8053 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
8054
8055 return;
8056 }
8057
56be3814 8058 fetch_registers_using_g (regcache);
74ca34ce 8059
5cd63fda 8060 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 8061 if (!rsa->regs[i].in_g_packet)
56be3814 8062 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
8063 {
8064 /* This register is not available. */
56be3814 8065 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
8066 }
8067}
8068
c906108c
SS
8069/* Prepare to store registers. Since we may send them all (using a
8070 'G' request), we have to read out the ones we don't want to change
8071 first. */
8072
f6ac5f3d
PA
8073void
8074remote_target::prepare_to_store (struct regcache *regcache)
c906108c 8075{
9d6eea31
PA
8076 struct remote_state *rs = get_remote_state ();
8077 remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
cf0e1e0d 8078 int i;
cf0e1e0d 8079
c906108c 8080 /* Make sure the entire registers array is valid. */
4082afcc 8081 switch (packet_support (PACKET_P))
5a2468f5
JM
8082 {
8083 case PACKET_DISABLE:
8084 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 8085 /* Make sure all the necessary registers are cached. */
ac7936df 8086 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
ea9c271d 8087 if (rsa->regs[i].in_g_packet)
8e368124 8088 regcache_raw_update (regcache, rsa->regs[i].regnum);
5a2468f5
JM
8089 break;
8090 case PACKET_ENABLE:
8091 break;
8092 }
8093}
8094
ad10f812 8095/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 8096 packet was not recognized. */
5a2468f5
JM
8097
8098static int
1f4437a4
MS
8099store_register_using_P (const struct regcache *regcache,
8100 struct packet_reg *reg)
5a2468f5 8101{
ac7936df 8102 struct gdbarch *gdbarch = regcache->arch ();
d01949b6 8103 struct remote_state *rs = get_remote_state ();
5a2468f5 8104 /* Try storing a single register. */
6d820c5c 8105 char *buf = rs->buf;
9890e433 8106 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
5a2468f5 8107 char *p;
5a2468f5 8108
4082afcc 8109 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
8110 return 0;
8111
8112 if (reg->pnum == -1)
8113 return 0;
8114
ea9c271d 8115 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 8116 p = buf + strlen (buf);
56be3814 8117 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 8118 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
8119 putpkt (rs->buf);
8120 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 8121
74ca34ce
DJ
8122 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
8123 {
8124 case PACKET_OK:
8125 return 1;
8126 case PACKET_ERROR:
27a9c0bf
MS
8127 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8128 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
8129 case PACKET_UNKNOWN:
8130 return 0;
8131 default:
8132 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8133 }
c906108c
SS
8134}
8135
23860348
MS
8136/* Store register REGNUM, or all registers if REGNUM == -1, from the
8137 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
8138
8139static void
56be3814 8140store_registers_using_G (const struct regcache *regcache)
c906108c 8141{
d01949b6 8142 struct remote_state *rs = get_remote_state ();
9d6eea31 8143 remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
cfd77fa1 8144 gdb_byte *regs;
c906108c
SS
8145 char *p;
8146
193cb69f
AC
8147 /* Extract all the registers in the regcache copying them into a
8148 local buffer. */
8149 {
b323314b 8150 int i;
a744cf53 8151
224c3ddb 8152 regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
ea9c271d 8153 memset (regs, 0, rsa->sizeof_g_packet);
ac7936df 8154 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
193cb69f 8155 {
ea9c271d 8156 struct packet_reg *r = &rsa->regs[i];
a744cf53 8157
b323314b 8158 if (r->in_g_packet)
56be3814 8159 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
8160 }
8161 }
c906108c
SS
8162
8163 /* Command describes registers byte by byte,
8164 each byte encoded as two hex characters. */
6d820c5c 8165 p = rs->buf;
193cb69f 8166 *p++ = 'G';
74ca34ce 8167 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
8168 putpkt (rs->buf);
8169 getpkt (&rs->buf, &rs->buf_size, 0);
8170 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
8171 error (_("Could not write registers; remote failure reply '%s'"),
8172 rs->buf);
c906108c 8173}
74ca34ce
DJ
8174
8175/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8176 of the register cache buffer. FIXME: ignores errors. */
8177
f6ac5f3d
PA
8178void
8179remote_target::store_registers (struct regcache *regcache, int regnum)
74ca34ce 8180{
5cd63fda 8181 struct gdbarch *gdbarch = regcache->arch ();
9d6eea31
PA
8182 struct remote_state *rs = get_remote_state ();
8183 remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
74ca34ce
DJ
8184 int i;
8185
e6e4e701 8186 set_remote_traceframe ();
bcc0c096 8187 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
8188
8189 if (regnum >= 0)
8190 {
5cd63fda 8191 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 8192
74ca34ce
DJ
8193 gdb_assert (reg != NULL);
8194
8195 /* Always prefer to store registers using the 'P' packet if
8196 possible; we often change only a small number of registers.
8197 Sometimes we change a larger number; we'd need help from a
8198 higher layer to know to use 'G'. */
56be3814 8199 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
8200 return;
8201
8202 /* For now, don't complain if we have no way to write the
8203 register. GDB loses track of unavailable registers too
8204 easily. Some day, this may be an error. We don't have
0df8b418 8205 any way to read the register, either... */
74ca34ce
DJ
8206 if (!reg->in_g_packet)
8207 return;
8208
56be3814 8209 store_registers_using_G (regcache);
74ca34ce
DJ
8210 return;
8211 }
8212
56be3814 8213 store_registers_using_G (regcache);
74ca34ce 8214
5cd63fda 8215 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 8216 if (!rsa->regs[i].in_g_packet)
56be3814 8217 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
8218 /* See above for why we do not issue an error here. */
8219 continue;
8220}
c906108c
SS
8221\f
8222
8223/* Return the number of hex digits in num. */
8224
8225static int
fba45db2 8226hexnumlen (ULONGEST num)
c906108c
SS
8227{
8228 int i;
8229
8230 for (i = 0; num != 0; i++)
8231 num >>= 4;
8232
325fac50 8233 return std::max (i, 1);
c906108c
SS
8234}
8235
2df3850c 8236/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
8237
8238static int
fba45db2 8239hexnumstr (char *buf, ULONGEST num)
c906108c 8240{
c906108c 8241 int len = hexnumlen (num);
a744cf53 8242
2df3850c
JM
8243 return hexnumnstr (buf, num, len);
8244}
8245
c906108c 8246
2df3850c 8247/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 8248
2df3850c 8249static int
fba45db2 8250hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
8251{
8252 int i;
8253
8254 buf[width] = '\0';
8255
8256 for (i = width - 1; i >= 0; i--)
c906108c 8257 {
c5aa993b 8258 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
8259 num >>= 4;
8260 }
8261
2df3850c 8262 return width;
c906108c
SS
8263}
8264
23860348 8265/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
8266
8267static CORE_ADDR
fba45db2 8268remote_address_masked (CORE_ADDR addr)
c906108c 8269{
883b9c6c 8270 unsigned int address_size = remote_address_size;
a744cf53 8271
911c95a5
UW
8272 /* If "remoteaddresssize" was not set, default to target address size. */
8273 if (!address_size)
f5656ead 8274 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
8275
8276 if (address_size > 0
8277 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
8278 {
8279 /* Only create a mask when that mask can safely be constructed
23860348 8280 in a ULONGEST variable. */
c906108c 8281 ULONGEST mask = 1;
a744cf53 8282
911c95a5 8283 mask = (mask << address_size) - 1;
c906108c
SS
8284 addr &= mask;
8285 }
8286 return addr;
8287}
8288
8289/* Determine whether the remote target supports binary downloading.
8290 This is accomplished by sending a no-op memory write of zero length
8291 to the target at the specified address. It does not suffice to send
23860348
MS
8292 the whole packet, since many stubs strip the eighth bit and
8293 subsequently compute a wrong checksum, which causes real havoc with
8294 remote_write_bytes.
7a292a7a 8295
96baa820 8296 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 8297 clean. In cases like this, the user should clear "remote
23860348 8298 X-packet". */
96baa820 8299
c906108c 8300static void
fba45db2 8301check_binary_download (CORE_ADDR addr)
c906108c 8302{
d01949b6 8303 struct remote_state *rs = get_remote_state ();
24b06219 8304
4082afcc 8305 switch (packet_support (PACKET_X))
c906108c 8306 {
96baa820
JM
8307 case PACKET_DISABLE:
8308 break;
8309 case PACKET_ENABLE:
8310 break;
8311 case PACKET_SUPPORT_UNKNOWN:
8312 {
96baa820 8313 char *p;
802188a7 8314
2e9f7625 8315 p = rs->buf;
96baa820
JM
8316 *p++ = 'X';
8317 p += hexnumstr (p, (ULONGEST) addr);
8318 *p++ = ',';
8319 p += hexnumstr (p, (ULONGEST) 0);
8320 *p++ = ':';
8321 *p = '\0';
802188a7 8322
2e9f7625 8323 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8324 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 8325
2e9f7625 8326 if (rs->buf[0] == '\0')
96baa820
JM
8327 {
8328 if (remote_debug)
8329 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8330 "binary downloading NOT "
8331 "supported by target\n");
444abaca 8332 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
8333 }
8334 else
8335 {
8336 if (remote_debug)
8337 fprintf_unfiltered (gdb_stdlog,
64b9b334 8338 "binary downloading supported by target\n");
444abaca 8339 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
8340 }
8341 break;
8342 }
c906108c
SS
8343 }
8344}
8345
124e13d9
SM
8346/* Helper function to resize the payload in order to try to get a good
8347 alignment. We try to write an amount of data such that the next write will
8348 start on an address aligned on REMOTE_ALIGN_WRITES. */
8349
8350static int
8351align_for_efficient_write (int todo, CORE_ADDR memaddr)
8352{
8353 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
8354}
8355
c906108c
SS
8356/* Write memory data directly to the remote machine.
8357 This does not inform the data cache; the data cache uses this.
a76d924d 8358 HEADER is the starting part of the packet.
c906108c
SS
8359 MEMADDR is the address in the remote memory space.
8360 MYADDR is the address of the buffer in our space.
124e13d9
SM
8361 LEN_UNITS is the number of addressable units to write.
8362 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
8363 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8364 should send data as binary ('X'), or hex-encoded ('M').
8365
8366 The function creates packet of the form
8367 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8368
124e13d9 8369 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
8370
8371 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8372 are omitted.
8373
9b409511 8374 Return the transferred status, error or OK (an
124e13d9
SM
8375 'enum target_xfer_status' value). Save the number of addressable units
8376 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8377
8378 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8379 exchange between gdb and the stub could look like (?? in place of the
8380 checksum):
8381
8382 -> $m1000,4#??
8383 <- aaaabbbbccccdddd
8384
8385 -> $M1000,3:eeeeffffeeee#??
8386 <- OK
8387
8388 -> $m1000,4#??
8389 <- eeeeffffeeeedddd */
c906108c 8390
9b409511 8391static enum target_xfer_status
a76d924d 8392remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
8393 const gdb_byte *myaddr, ULONGEST len_units,
8394 int unit_size, ULONGEST *xfered_len_units,
8395 char packet_format, int use_length)
c906108c 8396{
6d820c5c 8397 struct remote_state *rs = get_remote_state ();
cfd77fa1 8398 char *p;
a76d924d
DJ
8399 char *plen = NULL;
8400 int plenlen = 0;
124e13d9
SM
8401 int todo_units;
8402 int units_written;
8403 int payload_capacity_bytes;
8404 int payload_length_bytes;
a76d924d
DJ
8405
8406 if (packet_format != 'X' && packet_format != 'M')
8407 internal_error (__FILE__, __LINE__,
9b20d036 8408 _("remote_write_bytes_aux: bad packet format"));
c906108c 8409
124e13d9 8410 if (len_units == 0)
9b409511 8411 return TARGET_XFER_EOF;
b2182ed2 8412
124e13d9 8413 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 8414
6d820c5c
DJ
8415 /* The packet buffer will be large enough for the payload;
8416 get_memory_packet_size ensures this. */
a76d924d 8417 rs->buf[0] = '\0';
c906108c 8418
a257b5bb 8419 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
8420 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
8421
124e13d9 8422 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 8423 if (!use_length)
0df8b418 8424 /* The comma won't be used. */
124e13d9
SM
8425 payload_capacity_bytes += 1;
8426 payload_capacity_bytes -= strlen (header);
8427 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 8428
a76d924d 8429 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 8430
a76d924d
DJ
8431 strcat (rs->buf, header);
8432 p = rs->buf + strlen (header);
8433
8434 /* Compute a best guess of the number of bytes actually transfered. */
8435 if (packet_format == 'X')
c906108c 8436 {
23860348 8437 /* Best guess at number of bytes that will fit. */
325fac50
PA
8438 todo_units = std::min (len_units,
8439 (ULONGEST) payload_capacity_bytes / unit_size);
a76d924d 8440 if (use_length)
124e13d9 8441 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50 8442 todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
8443 }
8444 else
8445 {
124e13d9 8446 /* Number of bytes that will fit. */
325fac50
PA
8447 todo_units
8448 = std::min (len_units,
8449 (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
a76d924d 8450 if (use_length)
124e13d9 8451 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50
PA
8452 todo_units = std::min (todo_units,
8453 (payload_capacity_bytes / unit_size) / 2);
917317f4 8454 }
a76d924d 8455
124e13d9 8456 if (todo_units <= 0)
3de11b2e 8457 internal_error (__FILE__, __LINE__,
405f8e94 8458 _("minimum packet size too small to write data"));
802188a7 8459
6765f3e5
DJ
8460 /* If we already need another packet, then try to align the end
8461 of this packet to a useful boundary. */
124e13d9
SM
8462 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
8463 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 8464
a257b5bb 8465 /* Append "<memaddr>". */
917317f4
JM
8466 memaddr = remote_address_masked (memaddr);
8467 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 8468
a76d924d
DJ
8469 if (use_length)
8470 {
8471 /* Append ",". */
8472 *p++ = ',';
802188a7 8473
124e13d9
SM
8474 /* Append the length and retain its location and size. It may need to be
8475 adjusted once the packet body has been created. */
a76d924d 8476 plen = p;
124e13d9 8477 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
8478 p += plenlen;
8479 }
a257b5bb
AC
8480
8481 /* Append ":". */
917317f4
JM
8482 *p++ = ':';
8483 *p = '\0';
802188a7 8484
a257b5bb 8485 /* Append the packet body. */
a76d924d 8486 if (packet_format == 'X')
917317f4 8487 {
917317f4
JM
8488 /* Binary mode. Send target system values byte by byte, in
8489 increasing byte addresses. Only escape certain critical
8490 characters. */
124e13d9
SM
8491 payload_length_bytes =
8492 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
8493 &units_written, payload_capacity_bytes);
6765f3e5 8494
124e13d9 8495 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
8496 a second try to keep the end of the packet aligned. Don't do
8497 this if the packet is tiny. */
124e13d9 8498 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 8499 {
124e13d9
SM
8500 int new_todo_units;
8501
8502 new_todo_units = align_for_efficient_write (units_written, memaddr);
8503
8504 if (new_todo_units != units_written)
8505 payload_length_bytes =
8506 remote_escape_output (myaddr, new_todo_units, unit_size,
8507 (gdb_byte *) p, &units_written,
8508 payload_capacity_bytes);
6765f3e5
DJ
8509 }
8510
124e13d9
SM
8511 p += payload_length_bytes;
8512 if (use_length && units_written < todo_units)
c906108c 8513 {
802188a7 8514 /* Escape chars have filled up the buffer prematurely,
124e13d9 8515 and we have actually sent fewer units than planned.
917317f4
JM
8516 Fix-up the length field of the packet. Use the same
8517 number of characters as before. */
124e13d9
SM
8518 plen += hexnumnstr (plen, (ULONGEST) units_written,
8519 plenlen);
917317f4 8520 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 8521 }
a76d924d
DJ
8522 }
8523 else
8524 {
917317f4
JM
8525 /* Normal mode: Send target system values byte by byte, in
8526 increasing byte addresses. Each byte is encoded as a two hex
8527 value. */
124e13d9
SM
8528 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
8529 units_written = todo_units;
c906108c 8530 }
802188a7 8531
2e9f7625 8532 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8533 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8534
2e9f7625 8535 if (rs->buf[0] == 'E')
00d84524 8536 return TARGET_XFER_E_IO;
802188a7 8537
124e13d9
SM
8538 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
8539 send fewer units than we'd planned. */
8540 *xfered_len_units = (ULONGEST) units_written;
92ffd475 8541 return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
c906108c
SS
8542}
8543
a76d924d
DJ
8544/* Write memory data directly to the remote machine.
8545 This does not inform the data cache; the data cache uses this.
8546 MEMADDR is the address in the remote memory space.
8547 MYADDR is the address of the buffer in our space.
8548 LEN is the number of bytes.
8549
9b409511
YQ
8550 Return the transferred status, error or OK (an
8551 'enum target_xfer_status' value). Save the number of bytes
8552 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 8553
9b409511
YQ
8554static enum target_xfer_status
8555remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 8556 int unit_size, ULONGEST *xfered_len)
a76d924d 8557{
a121b7c1 8558 const char *packet_format = NULL;
a76d924d
DJ
8559
8560 /* Check whether the target supports binary download. */
8561 check_binary_download (memaddr);
8562
4082afcc 8563 switch (packet_support (PACKET_X))
a76d924d
DJ
8564 {
8565 case PACKET_ENABLE:
8566 packet_format = "X";
8567 break;
8568 case PACKET_DISABLE:
8569 packet_format = "M";
8570 break;
8571 case PACKET_SUPPORT_UNKNOWN:
8572 internal_error (__FILE__, __LINE__,
8573 _("remote_write_bytes: bad internal state"));
8574 default:
8575 internal_error (__FILE__, __LINE__, _("bad switch"));
8576 }
8577
8578 return remote_write_bytes_aux (packet_format,
124e13d9 8579 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 8580 packet_format[0], 1);
a76d924d
DJ
8581}
8582
9217e74e
YQ
8583/* Read memory data directly from the remote machine.
8584 This does not use the data cache; the data cache uses this.
8585 MEMADDR is the address in the remote memory space.
8586 MYADDR is the address of the buffer in our space.
124e13d9
SM
8587 LEN_UNITS is the number of addressable memory units to read..
8588 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
8589
8590 Return the transferred status, error or OK (an
8591 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
8592 transferred in *XFERED_LEN_UNITS.
8593
8594 See the comment of remote_write_bytes_aux for an example of
8595 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
8596
8597static enum target_xfer_status
124e13d9
SM
8598remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
8599 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
8600{
8601 struct remote_state *rs = get_remote_state ();
124e13d9 8602 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 8603 char *p;
124e13d9
SM
8604 int todo_units;
8605 int decoded_bytes;
9217e74e 8606
124e13d9 8607 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
8608 /* The packet buffer will be large enough for the payload;
8609 get_memory_packet_size ensures this. */
8610
124e13d9 8611 /* Number of units that will fit. */
325fac50
PA
8612 todo_units = std::min (len_units,
8613 (ULONGEST) (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
8614
8615 /* Construct "m"<memaddr>","<len>". */
8616 memaddr = remote_address_masked (memaddr);
8617 p = rs->buf;
8618 *p++ = 'm';
8619 p += hexnumstr (p, (ULONGEST) memaddr);
8620 *p++ = ',';
124e13d9 8621 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
8622 *p = '\0';
8623 putpkt (rs->buf);
8624 getpkt (&rs->buf, &rs->buf_size, 0);
8625 if (rs->buf[0] == 'E'
8626 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
8627 && rs->buf[3] == '\0')
8628 return TARGET_XFER_E_IO;
8629 /* Reply describes memory byte by byte, each byte encoded as two hex
8630 characters. */
8631 p = rs->buf;
124e13d9 8632 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 8633 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 8634 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
92ffd475 8635 return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
9217e74e
YQ
8636}
8637
b55fbac4
YQ
8638/* Using the set of read-only target sections of remote, read live
8639 read-only memory.
8acf9577
YQ
8640
8641 For interface/parameters/return description see target.h,
8642 to_xfer_partial. */
8643
8644static enum target_xfer_status
b55fbac4
YQ
8645remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
8646 ULONGEST memaddr, ULONGEST len,
124e13d9 8647 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
8648{
8649 struct target_section *secp;
8650 struct target_section_table *table;
8651
8652 secp = target_section_by_addr (ops, memaddr);
8653 if (secp != NULL
8654 && (bfd_get_section_flags (secp->the_bfd_section->owner,
8655 secp->the_bfd_section)
8656 & SEC_READONLY))
8657 {
8658 struct target_section *p;
8659 ULONGEST memend = memaddr + len;
8660
8661 table = target_get_section_table (ops);
8662
8663 for (p = table->sections; p < table->sections_end; p++)
8664 {
8665 if (memaddr >= p->addr)
8666 {
8667 if (memend <= p->endaddr)
8668 {
8669 /* Entire transfer is within this section. */
124e13d9 8670 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8671 xfered_len);
8acf9577
YQ
8672 }
8673 else if (memaddr >= p->endaddr)
8674 {
8675 /* This section ends before the transfer starts. */
8676 continue;
8677 }
8678 else
8679 {
8680 /* This section overlaps the transfer. Just do half. */
8681 len = p->endaddr - memaddr;
124e13d9 8682 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8683 xfered_len);
8acf9577
YQ
8684 }
8685 }
8686 }
8687 }
8688
8689 return TARGET_XFER_EOF;
8690}
8691
9217e74e
YQ
8692/* Similar to remote_read_bytes_1, but it reads from the remote stub
8693 first if the requested memory is unavailable in traceframe.
8694 Otherwise, fall back to remote_read_bytes_1. */
c906108c 8695
9b409511 8696static enum target_xfer_status
8acf9577 8697remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
8698 gdb_byte *myaddr, ULONGEST len, int unit_size,
8699 ULONGEST *xfered_len)
c906108c 8700{
6b6aa828 8701 if (len == 0)
96c4f946 8702 return TARGET_XFER_EOF;
b2182ed2 8703
8acf9577
YQ
8704 if (get_traceframe_number () != -1)
8705 {
a79b1bc6 8706 std::vector<mem_range> available;
8acf9577
YQ
8707
8708 /* If we fail to get the set of available memory, then the
8709 target does not support querying traceframe info, and so we
8710 attempt reading from the traceframe anyway (assuming the
8711 target implements the old QTro packet then). */
8712 if (traceframe_available_memory (&available, memaddr, len))
8713 {
a79b1bc6 8714 if (available.empty () || available[0].start != memaddr)
8acf9577
YQ
8715 {
8716 enum target_xfer_status res;
8717
8718 /* Don't read into the traceframe's available
8719 memory. */
a79b1bc6 8720 if (!available.empty ())
8acf9577
YQ
8721 {
8722 LONGEST oldlen = len;
8723
a79b1bc6 8724 len = available[0].start - memaddr;
8acf9577
YQ
8725 gdb_assert (len <= oldlen);
8726 }
8727
8acf9577 8728 /* This goes through the topmost target again. */
b55fbac4 8729 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 8730 len, unit_size, xfered_len);
8acf9577
YQ
8731 if (res == TARGET_XFER_OK)
8732 return TARGET_XFER_OK;
8733 else
8734 {
8735 /* No use trying further, we know some memory starting
8736 at MEMADDR isn't available. */
8737 *xfered_len = len;
92ffd475
PC
8738 return (*xfered_len != 0) ?
8739 TARGET_XFER_UNAVAILABLE : TARGET_XFER_EOF;
8acf9577
YQ
8740 }
8741 }
8742
8743 /* Don't try to read more than how much is available, in
8744 case the target implements the deprecated QTro packet to
8745 cater for older GDBs (the target's knowledge of read-only
8746 sections may be outdated by now). */
a79b1bc6 8747 len = available[0].length;
8acf9577
YQ
8748 }
8749 }
8750
124e13d9 8751 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 8752}
74531fed 8753
c906108c 8754\f
c906108c 8755
a76d924d
DJ
8756/* Sends a packet with content determined by the printf format string
8757 FORMAT and the remaining arguments, then gets the reply. Returns
8758 whether the packet was a success, a failure, or unknown. */
8759
77b64a49
PA
8760static enum packet_result remote_send_printf (const char *format, ...)
8761 ATTRIBUTE_PRINTF (1, 2);
8762
2c0b251b 8763static enum packet_result
a76d924d
DJ
8764remote_send_printf (const char *format, ...)
8765{
8766 struct remote_state *rs = get_remote_state ();
8767 int max_size = get_remote_packet_size ();
a76d924d 8768 va_list ap;
a744cf53 8769
a76d924d
DJ
8770 va_start (ap, format);
8771
8772 rs->buf[0] = '\0';
8773 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 8774 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
8775
8776 if (putpkt (rs->buf) < 0)
8777 error (_("Communication problem with target."));
8778
8779 rs->buf[0] = '\0';
8780 getpkt (&rs->buf, &rs->buf_size, 0);
8781
8782 return packet_check_result (rs->buf);
8783}
8784
a76d924d
DJ
8785/* Flash writing can take quite some time. We'll set
8786 effectively infinite timeout for flash operations.
8787 In future, we'll need to decide on a better approach. */
8788static const int remote_flash_timeout = 1000;
8789
f6ac5f3d
PA
8790void
8791remote_target::flash_erase (ULONGEST address, LONGEST length)
a76d924d 8792{
f5656ead 8793 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d 8794 enum packet_result ret;
2ec845e7
TT
8795 scoped_restore restore_timeout
8796 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
a76d924d
DJ
8797
8798 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 8799 phex (address, addr_size),
a76d924d
DJ
8800 phex (length, 4));
8801 switch (ret)
8802 {
8803 case PACKET_UNKNOWN:
8804 error (_("Remote target does not support flash erase"));
8805 case PACKET_ERROR:
8806 error (_("Error erasing flash with vFlashErase packet"));
8807 default:
8808 break;
8809 }
a76d924d
DJ
8810}
8811
9b409511
YQ
8812static enum target_xfer_status
8813remote_flash_write (struct target_ops *ops, ULONGEST address,
8814 ULONGEST length, ULONGEST *xfered_len,
8815 const gdb_byte *data)
a76d924d 8816{
2ec845e7
TT
8817 scoped_restore restore_timeout
8818 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8819 return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
8820 xfered_len,'X', 0);
a76d924d
DJ
8821}
8822
f6ac5f3d
PA
8823void
8824remote_target::flash_done ()
a76d924d 8825{
a76d924d 8826 int ret;
a76d924d 8827
2ec845e7
TT
8828 scoped_restore restore_timeout
8829 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8830
a76d924d 8831 ret = remote_send_printf ("vFlashDone");
a76d924d
DJ
8832
8833 switch (ret)
8834 {
8835 case PACKET_UNKNOWN:
8836 error (_("Remote target does not support vFlashDone"));
8837 case PACKET_ERROR:
8838 error (_("Error finishing flash operation"));
8839 default:
8840 break;
8841 }
8842}
8843
f6ac5f3d
PA
8844void
8845remote_target::files_info ()
c906108c
SS
8846{
8847 puts_filtered ("Debugging a target over a serial line.\n");
8848}
8849\f
8850/* Stuff for dealing with the packets which are part of this protocol.
8851 See comment at top of file for details. */
8852
1927e618
PA
8853/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
8854 error to higher layers. Called when a serial error is detected.
8855 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
8856 the system error message for errno at function entry and final dot
8857 for output compatibility with throw_perror_with_name. */
1927e618
PA
8858
8859static void
8860unpush_and_perror (const char *string)
8861{
d6cb50a2 8862 int saved_errno = errno;
1927e618
PA
8863
8864 remote_unpush_target ();
d6cb50a2
JK
8865 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
8866 safe_strerror (saved_errno));
1927e618
PA
8867}
8868
048094ac
PA
8869/* Read a single character from the remote end. The current quit
8870 handler is overridden to avoid quitting in the middle of packet
8871 sequence, as that would break communication with the remote server.
8872 See remote_serial_quit_handler for more detail. */
c906108c
SS
8873
8874static int
fba45db2 8875readchar (int timeout)
c906108c
SS
8876{
8877 int ch;
5d93a237 8878 struct remote_state *rs = get_remote_state ();
048094ac 8879
2ec845e7
TT
8880 {
8881 scoped_restore restore_quit
8882 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
c906108c 8883
2ec845e7 8884 rs->got_ctrlc_during_io = 0;
c906108c 8885
2ec845e7 8886 ch = serial_readchar (rs->remote_desc, timeout);
048094ac 8887
2ec845e7
TT
8888 if (rs->got_ctrlc_during_io)
8889 set_quit_flag ();
8890 }
048094ac 8891
2acceee2 8892 if (ch >= 0)
0876f84a 8893 return ch;
2acceee2
JM
8894
8895 switch ((enum serial_rc) ch)
c906108c
SS
8896 {
8897 case SERIAL_EOF:
78a095c3 8898 remote_unpush_target ();
598d3636 8899 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 8900 /* no return */
c906108c 8901 case SERIAL_ERROR:
1927e618
PA
8902 unpush_and_perror (_("Remote communication error. "
8903 "Target disconnected."));
2acceee2 8904 /* no return */
c906108c 8905 case SERIAL_TIMEOUT:
2acceee2 8906 break;
c906108c 8907 }
2acceee2 8908 return ch;
c906108c
SS
8909}
8910
c33e31fd 8911/* Wrapper for serial_write that closes the target and throws if
048094ac
PA
8912 writing fails. The current quit handler is overridden to avoid
8913 quitting in the middle of packet sequence, as that would break
8914 communication with the remote server. See
8915 remote_serial_quit_handler for more detail. */
c33e31fd
PA
8916
8917static void
8918remote_serial_write (const char *str, int len)
8919{
5d93a237 8920 struct remote_state *rs = get_remote_state ();
048094ac 8921
2ec845e7
TT
8922 scoped_restore restore_quit
8923 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
048094ac
PA
8924
8925 rs->got_ctrlc_during_io = 0;
5d93a237
TT
8926
8927 if (serial_write (rs->remote_desc, str, len))
c33e31fd 8928 {
1927e618
PA
8929 unpush_and_perror (_("Remote communication error. "
8930 "Target disconnected."));
c33e31fd 8931 }
048094ac
PA
8932
8933 if (rs->got_ctrlc_during_io)
8934 set_quit_flag ();
c33e31fd
PA
8935}
8936
b3ced9ba
PA
8937/* Return a string representing an escaped version of BUF, of len N.
8938 E.g. \n is converted to \\n, \t to \\t, etc. */
6e5abd65 8939
b3ced9ba 8940static std::string
6e5abd65
PA
8941escape_buffer (const char *buf, int n)
8942{
d7e74731 8943 string_file stb;
6e5abd65 8944
d7e74731
PA
8945 stb.putstrn (buf, n, '\\');
8946 return std::move (stb.string ());
6e5abd65
PA
8947}
8948
c906108c
SS
8949/* Display a null-terminated packet on stdout, for debugging, using C
8950 string notation. */
8951
8952static void
baa336ce 8953print_packet (const char *buf)
c906108c
SS
8954{
8955 puts_filtered ("\"");
43e526b9 8956 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
8957 puts_filtered ("\"");
8958}
8959
8960int
baa336ce 8961putpkt (const char *buf)
c906108c
SS
8962{
8963 return putpkt_binary (buf, strlen (buf));
8964}
8965
8966/* Send a packet to the remote machine, with error checking. The data
23860348 8967 of the packet is in BUF. The string in BUF can be at most
ea9c271d 8968 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
8969 and for a possible /0 if we are debugging (remote_debug) and want
8970 to print the sent packet as a string. */
c906108c
SS
8971
8972static int
baa336ce 8973putpkt_binary (const char *buf, int cnt)
c906108c 8974{
2d717e4f 8975 struct remote_state *rs = get_remote_state ();
c906108c
SS
8976 int i;
8977 unsigned char csum = 0;
b80406ac
TT
8978 gdb::def_vector<char> data (cnt + 6);
8979 char *buf2 = data.data ();
085dd6e6 8980
c906108c
SS
8981 int ch;
8982 int tcount = 0;
8983 char *p;
8984
e24a49d8
PA
8985 /* Catch cases like trying to read memory or listing threads while
8986 we're waiting for a stop reply. The remote server wouldn't be
8987 ready to handle this request, so we'd hang and timeout. We don't
8988 have to worry about this in synchronous mode, because in that
8989 case it's not possible to issue a command while the target is
74531fed
PA
8990 running. This is not a problem in non-stop mode, because in that
8991 case, the stub is always ready to process serial input. */
6efcd9a8
PA
8992 if (!target_is_non_stop_p ()
8993 && target_is_async_p ()
8994 && rs->waiting_for_stop_reply)
9597b22a
DE
8995 {
8996 error (_("Cannot execute this command while the target is running.\n"
8997 "Use the \"interrupt\" command to stop the target\n"
8998 "and then try again."));
8999 }
e24a49d8 9000
2d717e4f
DJ
9001 /* We're sending out a new packet. Make sure we don't look at a
9002 stale cached response. */
9003 rs->cached_wait_status = 0;
9004
c906108c
SS
9005 /* Copy the packet into buffer BUF2, encapsulating it
9006 and giving it a checksum. */
9007
c906108c
SS
9008 p = buf2;
9009 *p++ = '$';
9010
9011 for (i = 0; i < cnt; i++)
9012 {
9013 csum += buf[i];
9014 *p++ = buf[i];
9015 }
9016 *p++ = '#';
9017 *p++ = tohex ((csum >> 4) & 0xf);
9018 *p++ = tohex (csum & 0xf);
9019
9020 /* Send it over and over until we get a positive ack. */
9021
9022 while (1)
9023 {
9024 int started_error_output = 0;
9025
9026 if (remote_debug)
9027 {
9028 *p = '\0';
b3ced9ba 9029
6f8976bf
YQ
9030 int len = (int) (p - buf2);
9031
9032 std::string str
9033 = escape_buffer (buf2, std::min (len, REMOTE_DEBUG_MAX_CHAR));
9034
9035 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s", str.c_str ());
9036
567a3e54
SM
9037 if (len > REMOTE_DEBUG_MAX_CHAR)
9038 fprintf_unfiltered (gdb_stdlog, "[%d bytes omitted]",
9039 len - REMOTE_DEBUG_MAX_CHAR);
6f8976bf
YQ
9040
9041 fprintf_unfiltered (gdb_stdlog, "...");
b3ced9ba 9042
0f71a2f6 9043 gdb_flush (gdb_stdlog);
c906108c 9044 }
c33e31fd 9045 remote_serial_write (buf2, p - buf2);
c906108c 9046
a6f3e723
SL
9047 /* If this is a no acks version of the remote protocol, send the
9048 packet and move on. */
9049 if (rs->noack_mode)
9050 break;
9051
74531fed
PA
9052 /* Read until either a timeout occurs (-2) or '+' is read.
9053 Handle any notification that arrives in the mean time. */
c906108c
SS
9054 while (1)
9055 {
9056 ch = readchar (remote_timeout);
9057
c5aa993b 9058 if (remote_debug)
c906108c
SS
9059 {
9060 switch (ch)
9061 {
9062 case '+':
1216fa2c 9063 case '-':
c906108c
SS
9064 case SERIAL_TIMEOUT:
9065 case '$':
74531fed 9066 case '%':
c906108c
SS
9067 if (started_error_output)
9068 {
9069 putchar_unfiltered ('\n');
9070 started_error_output = 0;
9071 }
9072 }
9073 }
9074
9075 switch (ch)
9076 {
9077 case '+':
9078 if (remote_debug)
0f71a2f6 9079 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 9080 return 1;
1216fa2c
AC
9081 case '-':
9082 if (remote_debug)
9083 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 9084 /* FALLTHROUGH */
c906108c 9085 case SERIAL_TIMEOUT:
c5aa993b 9086 tcount++;
c906108c 9087 if (tcount > 3)
b80406ac 9088 return 0;
23860348 9089 break; /* Retransmit buffer. */
c906108c
SS
9090 case '$':
9091 {
40e3f985 9092 if (remote_debug)
2bc416ba 9093 fprintf_unfiltered (gdb_stdlog,
23860348 9094 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
9095 /* It's probably an old response sent because an ACK
9096 was lost. Gobble up the packet and ack it so it
9097 doesn't get retransmitted when we resend this
9098 packet. */
6d820c5c 9099 skip_frame ();
c33e31fd 9100 remote_serial_write ("+", 1);
23860348 9101 continue; /* Now, go look for +. */
c906108c 9102 }
74531fed
PA
9103
9104 case '%':
9105 {
9106 int val;
9107
9108 /* If we got a notification, handle it, and go back to looking
9109 for an ack. */
9110 /* We've found the start of a notification. Now
9111 collect the data. */
9112 val = read_frame (&rs->buf, &rs->buf_size);
9113 if (val >= 0)
9114 {
9115 if (remote_debug)
9116 {
b3ced9ba 9117 std::string str = escape_buffer (rs->buf, val);
6e5abd65 9118
6e5abd65
PA
9119 fprintf_unfiltered (gdb_stdlog,
9120 " Notification received: %s\n",
b3ced9ba 9121 str.c_str ());
74531fed 9122 }
5965e028 9123 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
9124 /* We're in sync now, rewait for the ack. */
9125 tcount = 0;
9126 }
9127 else
9128 {
9129 if (remote_debug)
9130 {
9131 if (!started_error_output)
9132 {
9133 started_error_output = 1;
9134 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
9135 }
9136 fputc_unfiltered (ch & 0177, gdb_stdlog);
9137 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
9138 }
9139 }
9140 continue;
9141 }
9142 /* fall-through */
c906108c
SS
9143 default:
9144 if (remote_debug)
9145 {
9146 if (!started_error_output)
9147 {
9148 started_error_output = 1;
0f71a2f6 9149 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 9150 }
0f71a2f6 9151 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
9152 }
9153 continue;
9154 }
23860348 9155 break; /* Here to retransmit. */
c906108c
SS
9156 }
9157
9158#if 0
9159 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
9160 able to get out next time we call QUIT, without anything as
9161 violent as interrupt_query. If we want to provide a way out of
9162 here without getting to the next QUIT, it should be based on
9163 hitting ^C twice as in remote_wait. */
c906108c
SS
9164 if (quit_flag)
9165 {
9166 quit_flag = 0;
9167 interrupt_query ();
9168 }
9169#endif
9170 }
a5c0808e 9171
a6f3e723 9172 return 0;
c906108c
SS
9173}
9174
6d820c5c
DJ
9175/* Come here after finding the start of a frame when we expected an
9176 ack. Do our best to discard the rest of this packet. */
9177
9178static void
9179skip_frame (void)
9180{
9181 int c;
9182
9183 while (1)
9184 {
9185 c = readchar (remote_timeout);
9186 switch (c)
9187 {
9188 case SERIAL_TIMEOUT:
9189 /* Nothing we can do. */
9190 return;
9191 case '#':
9192 /* Discard the two bytes of checksum and stop. */
9193 c = readchar (remote_timeout);
9194 if (c >= 0)
9195 c = readchar (remote_timeout);
9196
9197 return;
9198 case '*': /* Run length encoding. */
9199 /* Discard the repeat count. */
9200 c = readchar (remote_timeout);
9201 if (c < 0)
9202 return;
9203 break;
9204 default:
9205 /* A regular character. */
9206 break;
9207 }
9208 }
9209}
9210
c906108c 9211/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
9212 into *BUF, verifying the checksum, length, and handling run-length
9213 compression. NUL terminate the buffer. If there is not enough room,
9214 expand *BUF using xrealloc.
c906108c 9215
c2d11a7d
JM
9216 Returns -1 on error, number of characters in buffer (ignoring the
9217 trailing NULL) on success. (could be extended to return one of the
23860348 9218 SERIAL status indications). */
c2d11a7d
JM
9219
9220static long
6d820c5c
DJ
9221read_frame (char **buf_p,
9222 long *sizeof_buf)
c906108c
SS
9223{
9224 unsigned char csum;
c2d11a7d 9225 long bc;
c906108c 9226 int c;
6d820c5c 9227 char *buf = *buf_p;
a6f3e723 9228 struct remote_state *rs = get_remote_state ();
c906108c
SS
9229
9230 csum = 0;
c2d11a7d 9231 bc = 0;
c906108c
SS
9232
9233 while (1)
9234 {
9235 c = readchar (remote_timeout);
c906108c
SS
9236 switch (c)
9237 {
9238 case SERIAL_TIMEOUT:
9239 if (remote_debug)
0f71a2f6 9240 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 9241 return -1;
c906108c
SS
9242 case '$':
9243 if (remote_debug)
0f71a2f6
JM
9244 fputs_filtered ("Saw new packet start in middle of old one\n",
9245 gdb_stdlog);
23860348 9246 return -1; /* Start a new packet, count retries. */
c906108c
SS
9247 case '#':
9248 {
9249 unsigned char pktcsum;
e1b09194
AC
9250 int check_0 = 0;
9251 int check_1 = 0;
c906108c 9252
c2d11a7d 9253 buf[bc] = '\0';
c906108c 9254
e1b09194
AC
9255 check_0 = readchar (remote_timeout);
9256 if (check_0 >= 0)
9257 check_1 = readchar (remote_timeout);
802188a7 9258
e1b09194
AC
9259 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
9260 {
9261 if (remote_debug)
2bc416ba 9262 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 9263 gdb_stdlog);
e1b09194
AC
9264 return -1;
9265 }
9266 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
9267 {
9268 if (remote_debug)
2bc416ba 9269 fputs_filtered ("Communication error in checksum\n",
23860348 9270 gdb_stdlog);
40e3f985
FN
9271 return -1;
9272 }
c906108c 9273
a6f3e723
SL
9274 /* Don't recompute the checksum; with no ack packets we
9275 don't have any way to indicate a packet retransmission
9276 is necessary. */
9277 if (rs->noack_mode)
9278 return bc;
9279
e1b09194 9280 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 9281 if (csum == pktcsum)
c2d11a7d 9282 return bc;
c906108c 9283
c5aa993b 9284 if (remote_debug)
c906108c 9285 {
b3ced9ba 9286 std::string str = escape_buffer (buf, bc);
6e5abd65 9287
6e5abd65 9288 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
9289 "Bad checksum, sentsum=0x%x, "
9290 "csum=0x%x, buf=%s\n",
b3ced9ba 9291 pktcsum, csum, str.c_str ());
c906108c 9292 }
c2d11a7d 9293 /* Number of characters in buffer ignoring trailing
23860348 9294 NULL. */
c2d11a7d 9295 return -1;
c906108c 9296 }
23860348 9297 case '*': /* Run length encoding. */
c2c6d25f
JM
9298 {
9299 int repeat;
c906108c 9300
a744cf53 9301 csum += c;
b4501125
AC
9302 c = readchar (remote_timeout);
9303 csum += c;
23860348 9304 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 9305
23860348 9306 /* The character before ``*'' is repeated. */
c2d11a7d 9307
6d820c5c 9308 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 9309 {
6d820c5c
DJ
9310 if (bc + repeat - 1 >= *sizeof_buf - 1)
9311 {
9312 /* Make some more room in the buffer. */
9313 *sizeof_buf += repeat;
224c3ddb 9314 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c
DJ
9315 buf = *buf_p;
9316 }
9317
c2d11a7d
JM
9318 memset (&buf[bc], buf[bc - 1], repeat);
9319 bc += repeat;
c2c6d25f
JM
9320 continue;
9321 }
9322
c2d11a7d 9323 buf[bc] = '\0';
6d820c5c 9324 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 9325 return -1;
c2c6d25f 9326 }
c906108c 9327 default:
6d820c5c 9328 if (bc >= *sizeof_buf - 1)
c906108c 9329 {
6d820c5c
DJ
9330 /* Make some more room in the buffer. */
9331 *sizeof_buf *= 2;
224c3ddb 9332 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c 9333 buf = *buf_p;
c906108c
SS
9334 }
9335
6d820c5c
DJ
9336 buf[bc++] = c;
9337 csum += c;
9338 continue;
c906108c
SS
9339 }
9340 }
9341}
9342
9343/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9344 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9345 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9346 rather than timing out; this is used (in synchronous mode) to wait
9347 for a target that is is executing user code to stop. */
d9fcf2fb
JM
9348/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9349 don't have to change all the calls to getpkt to deal with the
9350 return value, because at the moment I don't know what the right
23860348 9351 thing to do it for those. */
c906108c 9352void
6d820c5c
DJ
9353getpkt (char **buf,
9354 long *sizeof_buf,
c2d11a7d 9355 int forever)
d9fcf2fb 9356{
54887903 9357 getpkt_sane (buf, sizeof_buf, forever);
d9fcf2fb
JM
9358}
9359
9360
9361/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9362 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9363 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9364 rather than timing out; this is used (in synchronous mode) to wait
9365 for a target that is is executing user code to stop. If FOREVER ==
9366 0, this function is allowed to time out gracefully and return an
74531fed
PA
9367 indication of this to the caller. Otherwise return the number of
9368 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
9369 enough reason to return to the caller. *IS_NOTIF is an output
9370 boolean that indicates whether *BUF holds a notification or not
9371 (a regular packet). */
74531fed 9372
3172dc30 9373static int
74531fed 9374getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 9375 int expecting_notif, int *is_notif)
c906108c 9376{
2d717e4f 9377 struct remote_state *rs = get_remote_state ();
c906108c
SS
9378 int c;
9379 int tries;
9380 int timeout;
df4b58fe 9381 int val = -1;
c906108c 9382
2d717e4f
DJ
9383 /* We're reading a new response. Make sure we don't look at a
9384 previously cached response. */
9385 rs->cached_wait_status = 0;
9386
6d820c5c 9387 strcpy (*buf, "timeout");
c906108c
SS
9388
9389 if (forever)
74531fed
PA
9390 timeout = watchdog > 0 ? watchdog : -1;
9391 else if (expecting_notif)
9392 timeout = 0; /* There should already be a char in the buffer. If
9393 not, bail out. */
c906108c
SS
9394 else
9395 timeout = remote_timeout;
9396
9397#define MAX_TRIES 3
9398
74531fed
PA
9399 /* Process any number of notifications, and then return when
9400 we get a packet. */
9401 for (;;)
c906108c 9402 {
d9c43928 9403 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
9404 times. */
9405 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 9406 {
74531fed
PA
9407 /* This can loop forever if the remote side sends us
9408 characters continuously, but if it pauses, we'll get
9409 SERIAL_TIMEOUT from readchar because of timeout. Then
9410 we'll count that as a retry.
9411
9412 Note that even when forever is set, we will only wait
9413 forever prior to the start of a packet. After that, we
9414 expect characters to arrive at a brisk pace. They should
9415 show up within remote_timeout intervals. */
9416 do
9417 c = readchar (timeout);
9418 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
9419
9420 if (c == SERIAL_TIMEOUT)
9421 {
74531fed
PA
9422 if (expecting_notif)
9423 return -1; /* Don't complain, it's normal to not get
9424 anything in this case. */
9425
23860348 9426 if (forever) /* Watchdog went off? Kill the target. */
c906108c 9427 {
78a095c3 9428 remote_unpush_target ();
598d3636
JK
9429 throw_error (TARGET_CLOSE_ERROR,
9430 _("Watchdog timeout has expired. "
9431 "Target detached."));
c906108c 9432 }
c906108c 9433 if (remote_debug)
0f71a2f6 9434 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 9435 }
74531fed
PA
9436 else
9437 {
9438 /* We've found the start of a packet or notification.
9439 Now collect the data. */
9440 val = read_frame (buf, sizeof_buf);
9441 if (val >= 0)
9442 break;
9443 }
9444
c33e31fd 9445 remote_serial_write ("-", 1);
c906108c 9446 }
c906108c 9447
74531fed
PA
9448 if (tries > MAX_TRIES)
9449 {
9450 /* We have tried hard enough, and just can't receive the
9451 packet/notification. Give up. */
9452 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 9453
74531fed
PA
9454 /* Skip the ack char if we're in no-ack mode. */
9455 if (!rs->noack_mode)
c33e31fd 9456 remote_serial_write ("+", 1);
74531fed
PA
9457 return -1;
9458 }
c906108c 9459
74531fed
PA
9460 /* If we got an ordinary packet, return that to our caller. */
9461 if (c == '$')
c906108c
SS
9462 {
9463 if (remote_debug)
43e526b9 9464 {
6f8976bf
YQ
9465 std::string str
9466 = escape_buffer (*buf,
9467 std::min (val, REMOTE_DEBUG_MAX_CHAR));
9468
9469 fprintf_unfiltered (gdb_stdlog, "Packet received: %s",
9470 str.c_str ());
9471
567a3e54
SM
9472 if (val > REMOTE_DEBUG_MAX_CHAR)
9473 fprintf_unfiltered (gdb_stdlog, "[%d bytes omitted]",
9474 val - REMOTE_DEBUG_MAX_CHAR);
6e5abd65 9475
6f8976bf 9476 fprintf_unfiltered (gdb_stdlog, "\n");
43e526b9 9477 }
a6f3e723
SL
9478
9479 /* Skip the ack char if we're in no-ack mode. */
9480 if (!rs->noack_mode)
c33e31fd 9481 remote_serial_write ("+", 1);
fee9eda9
YQ
9482 if (is_notif != NULL)
9483 *is_notif = 0;
0876f84a 9484 return val;
c906108c
SS
9485 }
9486
74531fed
PA
9487 /* If we got a notification, handle it, and go back to looking
9488 for a packet. */
9489 else
9490 {
9491 gdb_assert (c == '%');
9492
9493 if (remote_debug)
9494 {
b3ced9ba 9495 std::string str = escape_buffer (*buf, val);
6e5abd65 9496
6e5abd65
PA
9497 fprintf_unfiltered (gdb_stdlog,
9498 " Notification received: %s\n",
b3ced9ba 9499 str.c_str ());
74531fed 9500 }
fee9eda9
YQ
9501 if (is_notif != NULL)
9502 *is_notif = 1;
c906108c 9503
5965e028 9504 handle_notification (rs->notif_state, *buf);
c906108c 9505
74531fed 9506 /* Notifications require no acknowledgement. */
a6f3e723 9507
74531fed 9508 if (expecting_notif)
fee9eda9 9509 return val;
74531fed
PA
9510 }
9511 }
9512}
9513
9514static int
9515getpkt_sane (char **buf, long *sizeof_buf, int forever)
9516{
fee9eda9 9517 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
9518}
9519
9520static int
fee9eda9
YQ
9521getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
9522 int *is_notif)
74531fed 9523{
fee9eda9
YQ
9524 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
9525 is_notif);
c906108c 9526}
74531fed 9527
cbb8991c
DB
9528/* Check whether EVENT is a fork event for the process specified
9529 by the pid passed in DATA, and if it is, kill the fork child. */
9530
9531static int
9532kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
9533 QUEUE_ITER (stop_reply_p) *iter,
9534 stop_reply_p event,
9535 void *data)
9536{
19ba03f4 9537 struct queue_iter_param *param = (struct queue_iter_param *) data;
cbb8991c
DB
9538 int parent_pid = *(int *) param->input;
9539
9540 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
9541 {
9542 struct remote_state *rs = get_remote_state ();
9543 int child_pid = ptid_get_pid (event->ws.value.related_pid);
9544 int res;
9545
9546 res = remote_vkill (child_pid, rs);
9547 if (res != 0)
9548 error (_("Can't kill fork child process %d"), child_pid);
9549 }
9550
9551 return 1;
9552}
9553
9554/* Kill any new fork children of process PID that haven't been
9555 processed by follow_fork. */
9556
9557static void
9558kill_new_fork_children (int pid, struct remote_state *rs)
9559{
9560 struct thread_info *thread;
9561 struct notif_client *notif = &notif_client_stop;
9562 struct queue_iter_param param;
9563
9564 /* Kill the fork child threads of any threads in process PID
9565 that are stopped at a fork event. */
9566 ALL_NON_EXITED_THREADS (thread)
9567 {
9568 struct target_waitstatus *ws = &thread->pending_follow;
9569
9570 if (is_pending_fork_parent (ws, pid, thread->ptid))
9571 {
9572 struct remote_state *rs = get_remote_state ();
9573 int child_pid = ptid_get_pid (ws->value.related_pid);
9574 int res;
9575
9576 res = remote_vkill (child_pid, rs);
9577 if (res != 0)
9578 error (_("Can't kill fork child process %d"), child_pid);
9579 }
9580 }
9581
9582 /* Check for any pending fork events (not reported or processed yet)
9583 in process PID and kill those fork child threads as well. */
9584 remote_notif_get_pending_events (notif);
9585 param.input = &pid;
9586 param.output = NULL;
9587 QUEUE_iterate (stop_reply_p, stop_reply_queue,
9588 kill_child_of_pending_fork, &param);
9589}
9590
c906108c 9591\f
8020350c
DB
9592/* Target hook to kill the current inferior. */
9593
f6ac5f3d
PA
9594void
9595remote_target::kill ()
43ff13b4 9596{
8020350c
DB
9597 int res = -1;
9598 int pid = ptid_get_pid (inferior_ptid);
9599 struct remote_state *rs = get_remote_state ();
0fdf84ca 9600
8020350c 9601 if (packet_support (PACKET_vKill) != PACKET_DISABLE)
0fdf84ca 9602 {
8020350c
DB
9603 /* If we're stopped while forking and we haven't followed yet,
9604 kill the child task. We need to do this before killing the
9605 parent task because if this is a vfork then the parent will
9606 be sleeping. */
9607 kill_new_fork_children (pid, rs);
9608
9609 res = remote_vkill (pid, rs);
9610 if (res == 0)
0fdf84ca 9611 {
bc1e6c81 9612 target_mourn_inferior (inferior_ptid);
0fdf84ca
PA
9613 return;
9614 }
8020350c 9615 }
0fdf84ca 9616
8020350c
DB
9617 /* If we are in 'target remote' mode and we are killing the only
9618 inferior, then we will tell gdbserver to exit and unpush the
9619 target. */
9620 if (res == -1 && !remote_multi_process_p (rs)
9621 && number_of_live_inferiors () == 1)
9622 {
9623 remote_kill_k ();
9624
9625 /* We've killed the remote end, we get to mourn it. If we are
9626 not in extended mode, mourning the inferior also unpushes
9627 remote_ops from the target stack, which closes the remote
9628 connection. */
bc1e6c81 9629 target_mourn_inferior (inferior_ptid);
8020350c
DB
9630
9631 return;
0fdf84ca 9632 }
43ff13b4 9633
8020350c 9634 error (_("Can't kill process"));
43ff13b4
JM
9635}
9636
8020350c
DB
9637/* Send a kill request to the target using the 'vKill' packet. */
9638
82f73884
PA
9639static int
9640remote_vkill (int pid, struct remote_state *rs)
9641{
4082afcc 9642 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
9643 return -1;
9644
9645 /* Tell the remote target to detach. */
bba74b36 9646 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
9647 putpkt (rs->buf);
9648 getpkt (&rs->buf, &rs->buf_size, 0);
9649
4082afcc
PA
9650 switch (packet_ok (rs->buf,
9651 &remote_protocol_packets[PACKET_vKill]))
9652 {
9653 case PACKET_OK:
9654 return 0;
9655 case PACKET_ERROR:
9656 return 1;
9657 case PACKET_UNKNOWN:
9658 return -1;
9659 default:
9660 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
9661 }
82f73884
PA
9662}
9663
8020350c
DB
9664/* Send a kill request to the target using the 'k' packet. */
9665
82f73884 9666static void
8020350c 9667remote_kill_k (void)
82f73884 9668{
8020350c
DB
9669 /* Catch errors so the user can quit from gdb even when we
9670 aren't on speaking terms with the remote system. */
9671 TRY
82f73884 9672 {
82f73884 9673 putpkt ("k");
82f73884 9674 }
8020350c
DB
9675 CATCH (ex, RETURN_MASK_ERROR)
9676 {
9677 if (ex.error == TARGET_CLOSE_ERROR)
9678 {
9679 /* If we got an (EOF) error that caused the target
9680 to go away, then we're done, that's what we wanted.
9681 "k" is susceptible to cause a premature EOF, given
9682 that the remote server isn't actually required to
9683 reply to "k", and it can happen that it doesn't
9684 even get to reply ACK to the "k". */
9685 return;
9686 }
82f73884 9687
8020350c
DB
9688 /* Otherwise, something went wrong. We didn't actually kill
9689 the target. Just propagate the exception, and let the
9690 user or higher layers decide what to do. */
9691 throw_exception (ex);
9692 }
9693 END_CATCH
82f73884
PA
9694}
9695
f6ac5f3d
PA
9696void
9697remote_target::mourn_inferior ()
c906108c 9698{
8020350c 9699 struct remote_state *rs = get_remote_state ();
ce5ce7ed 9700
9607784a
PA
9701 /* We're no longer interested in notification events of an inferior
9702 that exited or was killed/detached. */
9703 discard_pending_stop_replies (current_inferior ());
9704
8020350c
DB
9705 /* In 'target remote' mode with one inferior, we close the connection. */
9706 if (!rs->extended && number_of_live_inferiors () <= 1)
9707 {
f6ac5f3d 9708 unpush_target (this);
c906108c 9709
8020350c
DB
9710 /* remote_close takes care of doing most of the clean up. */
9711 generic_mourn_inferior ();
9712 return;
9713 }
c906108c 9714
e24a49d8
PA
9715 /* In case we got here due to an error, but we're going to stay
9716 connected. */
9717 rs->waiting_for_stop_reply = 0;
9718
dc1981d7
PA
9719 /* If the current general thread belonged to the process we just
9720 detached from or has exited, the remote side current general
9721 thread becomes undefined. Considering a case like this:
9722
9723 - We just got here due to a detach.
9724 - The process that we're detaching from happens to immediately
9725 report a global breakpoint being hit in non-stop mode, in the
9726 same thread we had selected before.
9727 - GDB attaches to this process again.
9728 - This event happens to be the next event we handle.
9729
9730 GDB would consider that the current general thread didn't need to
9731 be set on the stub side (with Hg), since for all it knew,
9732 GENERAL_THREAD hadn't changed.
9733
9734 Notice that although in all-stop mode, the remote server always
9735 sets the current thread to the thread reporting the stop event,
9736 that doesn't happen in non-stop mode; in non-stop, the stub *must
9737 not* change the current thread when reporting a breakpoint hit,
9738 due to the decoupling of event reporting and event handling.
9739
9740 To keep things simple, we always invalidate our notion of the
9741 current thread. */
47f8a51d 9742 record_currthread (rs, minus_one_ptid);
dc1981d7 9743
8020350c 9744 /* Call common code to mark the inferior as not running. */
48aa3c27
PA
9745 generic_mourn_inferior ();
9746
d729566a 9747 if (!have_inferiors ())
2d717e4f 9748 {
82f73884
PA
9749 if (!remote_multi_process_p (rs))
9750 {
9751 /* Check whether the target is running now - some remote stubs
9752 automatically restart after kill. */
9753 putpkt ("?");
9754 getpkt (&rs->buf, &rs->buf_size, 0);
9755
9756 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
9757 {
3e43a32a
MS
9758 /* Assume that the target has been restarted. Set
9759 inferior_ptid so that bits of core GDB realizes
9760 there's something here, e.g., so that the user can
9761 say "kill" again. */
82f73884
PA
9762 inferior_ptid = magic_null_ptid;
9763 }
82f73884 9764 }
2d717e4f
DJ
9765 }
9766}
c906108c 9767
57810aa7 9768bool
f6ac5f3d 9769extended_remote_target::supports_disable_randomization ()
03583c20 9770{
4082afcc 9771 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
9772}
9773
9774static void
9775extended_remote_disable_randomization (int val)
9776{
9777 struct remote_state *rs = get_remote_state ();
9778 char *reply;
9779
bba74b36
YQ
9780 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
9781 val);
03583c20 9782 putpkt (rs->buf);
b6bb3468 9783 reply = remote_get_noisy_reply ();
03583c20
UW
9784 if (*reply == '\0')
9785 error (_("Target does not support QDisableRandomization."));
9786 if (strcmp (reply, "OK") != 0)
9787 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
9788}
9789
2d717e4f 9790static int
7c5ded6a 9791extended_remote_run (const std::string &args)
2d717e4f
DJ
9792{
9793 struct remote_state *rs = get_remote_state ();
2d717e4f 9794 int len;
94585166 9795 const char *remote_exec_file = get_remote_exec_file ();
c906108c 9796
2d717e4f
DJ
9797 /* If the user has disabled vRun support, or we have detected that
9798 support is not available, do not try it. */
4082afcc 9799 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 9800 return -1;
424163ea 9801
2d717e4f
DJ
9802 strcpy (rs->buf, "vRun;");
9803 len = strlen (rs->buf);
c906108c 9804
2d717e4f
DJ
9805 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
9806 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
9807 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
9808 strlen (remote_exec_file));
2d717e4f 9809
7c5ded6a 9810 if (!args.empty ())
2d717e4f 9811 {
2d717e4f 9812 int i;
2d717e4f 9813
773a1edc 9814 gdb_argv argv (args.c_str ());
2d717e4f
DJ
9815 for (i = 0; argv[i] != NULL; i++)
9816 {
9817 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
9818 error (_("Argument list too long for run packet"));
9819 rs->buf[len++] = ';';
9f1b45b0
TT
9820 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
9821 strlen (argv[i]));
2d717e4f 9822 }
2d717e4f
DJ
9823 }
9824
9825 rs->buf[len++] = '\0';
9826
9827 putpkt (rs->buf);
9828 getpkt (&rs->buf, &rs->buf_size, 0);
9829
4082afcc 9830 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 9831 {
4082afcc 9832 case PACKET_OK:
3405876a 9833 /* We have a wait response. All is well. */
2d717e4f 9834 return 0;
4082afcc
PA
9835 case PACKET_UNKNOWN:
9836 return -1;
9837 case PACKET_ERROR:
2d717e4f
DJ
9838 if (remote_exec_file[0] == '\0')
9839 error (_("Running the default executable on the remote target failed; "
9840 "try \"set remote exec-file\"?"));
9841 else
9842 error (_("Running \"%s\" on the remote target failed"),
9843 remote_exec_file);
4082afcc
PA
9844 default:
9845 gdb_assert_not_reached (_("bad switch"));
2d717e4f 9846 }
c906108c
SS
9847}
9848
0a2dde4a
SDJ
9849/* Helper function to send set/unset environment packets. ACTION is
9850 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
9851 or "QEnvironmentUnsetVariable". VALUE is the variable to be
9852 sent. */
9853
9854static void
9855send_environment_packet (struct remote_state *rs,
9856 const char *action,
9857 const char *packet,
9858 const char *value)
9859{
9860 /* Convert the environment variable to an hex string, which
9861 is the best format to be transmitted over the wire. */
9862 std::string encoded_value = bin2hex ((const gdb_byte *) value,
9863 strlen (value));
9864
9865 xsnprintf (rs->buf, get_remote_packet_size (),
9866 "%s:%s", packet, encoded_value.c_str ());
9867
9868 putpkt (rs->buf);
9869 getpkt (&rs->buf, &rs->buf_size, 0);
9870 if (strcmp (rs->buf, "OK") != 0)
9871 warning (_("Unable to %s environment variable '%s' on remote."),
9872 action, value);
9873}
9874
9875/* Helper function to handle the QEnvironment* packets. */
9876
9877static void
9878extended_remote_environment_support (struct remote_state *rs)
9879{
9880 if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
9881 {
9882 putpkt ("QEnvironmentReset");
9883 getpkt (&rs->buf, &rs->buf_size, 0);
9884 if (strcmp (rs->buf, "OK") != 0)
9885 warning (_("Unable to reset environment on remote."));
9886 }
9887
9888 gdb_environ *e = &current_inferior ()->environment;
9889
9890 if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
9891 for (const std::string &el : e->user_set_env ())
9892 send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
9893 el.c_str ());
9894
9895 if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
9896 for (const std::string &el : e->user_unset_env ())
9897 send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
9898}
9899
bc3b087d
SDJ
9900/* Helper function to set the current working directory for the
9901 inferior in the remote target. */
9902
9903static void
9904extended_remote_set_inferior_cwd (struct remote_state *rs)
9905{
9906 if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
9907 {
9908 const char *inferior_cwd = get_inferior_cwd ();
9909
9910 if (inferior_cwd != NULL)
9911 {
9912 std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
9913 strlen (inferior_cwd));
9914
9915 xsnprintf (rs->buf, get_remote_packet_size (),
9916 "QSetWorkingDir:%s", hexpath.c_str ());
9917 }
9918 else
9919 {
9920 /* An empty inferior_cwd means that the user wants us to
9921 reset the remote server's inferior's cwd. */
9922 xsnprintf (rs->buf, get_remote_packet_size (),
9923 "QSetWorkingDir:");
9924 }
9925
9926 putpkt (rs->buf);
9927 getpkt (&rs->buf, &rs->buf_size, 0);
9928 if (packet_ok (rs->buf,
9929 &remote_protocol_packets[PACKET_QSetWorkingDir])
9930 != PACKET_OK)
9931 error (_("\
9932Remote replied unexpectedly while setting the inferior's working\n\
9933directory: %s"),
9934 rs->buf);
9935
9936 }
9937}
9938
2d717e4f
DJ
9939/* In the extended protocol we want to be able to do things like
9940 "run" and have them basically work as expected. So we need
9941 a special create_inferior function. We support changing the
9942 executable file and the command line arguments, but not the
9943 environment. */
9944
f6ac5f3d
PA
9945void
9946extended_remote_target::create_inferior (const char *exec_file,
9947 const std::string &args,
9948 char **env, int from_tty)
43ff13b4 9949{
3405876a
PA
9950 int run_worked;
9951 char *stop_reply;
9952 struct remote_state *rs = get_remote_state ();
94585166 9953 const char *remote_exec_file = get_remote_exec_file ();
3405876a 9954
43ff13b4 9955 /* If running asynchronously, register the target file descriptor
23860348 9956 with the event loop. */
75c99385 9957 if (target_can_async_p ())
6a3753b3 9958 target_async (1);
43ff13b4 9959
03583c20 9960 /* Disable address space randomization if requested (and supported). */
f6ac5f3d 9961 if (supports_disable_randomization ())
03583c20
UW
9962 extended_remote_disable_randomization (disable_randomization);
9963
aefd8b33
SDJ
9964 /* If startup-with-shell is on, we inform gdbserver to start the
9965 remote inferior using a shell. */
9966 if (packet_support (PACKET_QStartupWithShell) != PACKET_DISABLE)
9967 {
9968 xsnprintf (rs->buf, get_remote_packet_size (),
9969 "QStartupWithShell:%d", startup_with_shell ? 1 : 0);
9970 putpkt (rs->buf);
9971 getpkt (&rs->buf, &rs->buf_size, 0);
9972 if (strcmp (rs->buf, "OK") != 0)
9973 error (_("\
9974Remote replied unexpectedly while setting startup-with-shell: %s"),
9975 rs->buf);
9976 }
9977
0a2dde4a
SDJ
9978 extended_remote_environment_support (rs);
9979
bc3b087d
SDJ
9980 extended_remote_set_inferior_cwd (rs);
9981
43ff13b4 9982 /* Now restart the remote server. */
3405876a
PA
9983 run_worked = extended_remote_run (args) != -1;
9984 if (!run_worked)
2d717e4f
DJ
9985 {
9986 /* vRun was not supported. Fail if we need it to do what the
9987 user requested. */
9988 if (remote_exec_file[0])
9989 error (_("Remote target does not support \"set remote exec-file\""));
7c5ded6a 9990 if (!args.empty ())
2d717e4f 9991 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 9992
2d717e4f
DJ
9993 /* Fall back to "R". */
9994 extended_remote_restart ();
9995 }
424163ea 9996
6c95b8df
PA
9997 if (!have_inferiors ())
9998 {
9999 /* Clean up from the last time we ran, before we mark the target
10000 running again. This will mark breakpoints uninserted, and
10001 get_offsets may insert breakpoints. */
10002 init_thread_list ();
10003 init_wait_for_inferior ();
10004 }
45280a52 10005
3405876a
PA
10006 /* vRun's success return is a stop reply. */
10007 stop_reply = run_worked ? rs->buf : NULL;
10008 add_current_inferior_and_thread (stop_reply);
c0a2216e 10009
2d717e4f
DJ
10010 /* Get updated offsets, if the stub uses qOffsets. */
10011 get_offsets ();
2d717e4f 10012}
c906108c 10013\f
c5aa993b 10014
b775012e
LM
10015/* Given a location's target info BP_TGT and the packet buffer BUF, output
10016 the list of conditions (in agent expression bytecode format), if any, the
10017 target needs to evaluate. The output is placed into the packet buffer
bba74b36 10018 started from BUF and ended at BUF_END. */
b775012e
LM
10019
10020static int
10021remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
10022 struct bp_target_info *bp_tgt, char *buf,
10023 char *buf_end)
b775012e 10024{
3cde5c42 10025 if (bp_tgt->conditions.empty ())
b775012e
LM
10026 return 0;
10027
10028 buf += strlen (buf);
bba74b36 10029 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
10030 buf++;
10031
83621223 10032 /* Send conditions to the target. */
d538e36d 10033 for (agent_expr *aexpr : bp_tgt->conditions)
b775012e 10034 {
bba74b36 10035 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e 10036 buf += strlen (buf);
3cde5c42 10037 for (int i = 0; i < aexpr->len; ++i)
b775012e
LM
10038 buf = pack_hex_byte (buf, aexpr->buf[i]);
10039 *buf = '\0';
10040 }
b775012e
LM
10041 return 0;
10042}
10043
d3ce09f5
SS
10044static void
10045remote_add_target_side_commands (struct gdbarch *gdbarch,
10046 struct bp_target_info *bp_tgt, char *buf)
10047{
3cde5c42 10048 if (bp_tgt->tcommands.empty ())
d3ce09f5
SS
10049 return;
10050
10051 buf += strlen (buf);
10052
10053 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
10054 buf += strlen (buf);
10055
10056 /* Concatenate all the agent expressions that are commands into the
10057 cmds parameter. */
df97be55 10058 for (agent_expr *aexpr : bp_tgt->tcommands)
d3ce09f5
SS
10059 {
10060 sprintf (buf, "X%x,", aexpr->len);
10061 buf += strlen (buf);
3cde5c42 10062 for (int i = 0; i < aexpr->len; ++i)
d3ce09f5
SS
10063 buf = pack_hex_byte (buf, aexpr->buf[i]);
10064 *buf = '\0';
10065 }
d3ce09f5
SS
10066}
10067
8181d85f
DJ
10068/* Insert a breakpoint. On targets that have software breakpoint
10069 support, we ask the remote target to do the work; on targets
10070 which don't, we insert a traditional memory breakpoint. */
c906108c 10071
f6ac5f3d
PA
10072int
10073remote_target::insert_breakpoint (struct gdbarch *gdbarch,
10074 struct bp_target_info *bp_tgt)
c906108c 10075{
d471ea57
AC
10076 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10077 If it succeeds, then set the support to PACKET_ENABLE. If it
10078 fails, and the user has explicitly requested the Z support then
23860348 10079 report an error, otherwise, mark it disabled and go on. */
802188a7 10080
4082afcc 10081 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 10082 {
0d5ed153 10083 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10084 struct remote_state *rs;
bba74b36 10085 char *p, *endbuf;
4fff2411 10086
28439a30
PA
10087 /* Make sure the remote is pointing at the right process, if
10088 necessary. */
10089 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10090 set_general_process ();
10091
4fff2411
JZ
10092 rs = get_remote_state ();
10093 p = rs->buf;
bba74b36 10094 endbuf = rs->buf + get_remote_packet_size ();
802188a7 10095
96baa820
JM
10096 *(p++) = 'Z';
10097 *(p++) = '0';
10098 *(p++) = ',';
7c0f6dcc 10099 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 10100 p += hexnumstr (p, addr);
579c6ad9 10101 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 10102
f6ac5f3d 10103 if (supports_evaluation_of_breakpoint_conditions ())
bba74b36 10104 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10105
f6ac5f3d 10106 if (can_run_breakpoint_commands ())
d3ce09f5
SS
10107 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10108
6d820c5c
DJ
10109 putpkt (rs->buf);
10110 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10111
6d820c5c 10112 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 10113 {
d471ea57
AC
10114 case PACKET_ERROR:
10115 return -1;
10116 case PACKET_OK:
10117 return 0;
10118 case PACKET_UNKNOWN:
10119 break;
96baa820
JM
10120 }
10121 }
c906108c 10122
0000e5cc
PA
10123 /* If this breakpoint has target-side commands but this stub doesn't
10124 support Z0 packets, throw error. */
3cde5c42 10125 if (!bp_tgt->tcommands.empty ())
0000e5cc
PA
10126 throw_error (NOT_SUPPORTED_ERROR, _("\
10127Target doesn't support breakpoints that have target side commands."));
10128
f6ac5f3d 10129 return memory_insert_breakpoint (this, gdbarch, bp_tgt);
c906108c
SS
10130}
10131
f6ac5f3d
PA
10132int
10133remote_target::remove_breakpoint (struct gdbarch *gdbarch,
10134 struct bp_target_info *bp_tgt,
10135 enum remove_bp_reason reason)
c906108c 10136{
8181d85f 10137 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 10138 struct remote_state *rs = get_remote_state ();
96baa820 10139
4082afcc 10140 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 10141 {
6d820c5c 10142 char *p = rs->buf;
bba74b36 10143 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 10144
28439a30
PA
10145 /* Make sure the remote is pointing at the right process, if
10146 necessary. */
10147 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10148 set_general_process ();
10149
96baa820
JM
10150 *(p++) = 'z';
10151 *(p++) = '0';
10152 *(p++) = ',';
10153
8181d85f
DJ
10154 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
10155 p += hexnumstr (p, addr);
579c6ad9 10156 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 10157
6d820c5c
DJ
10158 putpkt (rs->buf);
10159 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10160
6d820c5c 10161 return (rs->buf[0] == 'E');
96baa820
JM
10162 }
10163
f6ac5f3d 10164 return memory_remove_breakpoint (this, gdbarch, bp_tgt, reason);
c906108c
SS
10165}
10166
f486487f 10167static enum Z_packet_type
d471ea57
AC
10168watchpoint_to_Z_packet (int type)
10169{
10170 switch (type)
10171 {
10172 case hw_write:
bb858e6a 10173 return Z_PACKET_WRITE_WP;
d471ea57
AC
10174 break;
10175 case hw_read:
bb858e6a 10176 return Z_PACKET_READ_WP;
d471ea57
AC
10177 break;
10178 case hw_access:
bb858e6a 10179 return Z_PACKET_ACCESS_WP;
d471ea57
AC
10180 break;
10181 default:
8e65ff28 10182 internal_error (__FILE__, __LINE__,
e2e0b3e5 10183 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
10184 }
10185}
10186
f6ac5f3d
PA
10187int
10188remote_target::insert_watchpoint (CORE_ADDR addr, int len,
10189 enum target_hw_bp_type type, struct expression *cond)
96baa820 10190{
d01949b6 10191 struct remote_state *rs = get_remote_state ();
bba74b36 10192 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 10193 char *p;
d471ea57 10194 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 10195
4082afcc 10196 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 10197 return 1;
802188a7 10198
28439a30
PA
10199 /* Make sure the remote is pointing at the right process, if
10200 necessary. */
10201 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10202 set_general_process ();
10203
bba74b36 10204 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 10205 p = strchr (rs->buf, '\0');
96baa820
JM
10206 addr = remote_address_masked (addr);
10207 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10208 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 10209
6d820c5c
DJ
10210 putpkt (rs->buf);
10211 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10212
6d820c5c 10213 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10214 {
10215 case PACKET_ERROR:
d471ea57 10216 return -1;
85d721b8
PA
10217 case PACKET_UNKNOWN:
10218 return 1;
d471ea57
AC
10219 case PACKET_OK:
10220 return 0;
10221 }
8e65ff28 10222 internal_error (__FILE__, __LINE__,
e2e0b3e5 10223 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
10224}
10225
57810aa7 10226bool
f6ac5f3d
PA
10227remote_target::watchpoint_addr_within_range (CORE_ADDR addr,
10228 CORE_ADDR start, int length)
283002cf
MR
10229{
10230 CORE_ADDR diff = remote_address_masked (addr - start);
10231
10232 return diff < length;
10233}
10234
d471ea57 10235
f6ac5f3d
PA
10236int
10237remote_target::remove_watchpoint (CORE_ADDR addr, int len,
10238 enum target_hw_bp_type type, struct expression *cond)
96baa820 10239{
d01949b6 10240 struct remote_state *rs = get_remote_state ();
bba74b36 10241 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 10242 char *p;
d471ea57
AC
10243 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
10244
4082afcc 10245 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 10246 return -1;
802188a7 10247
28439a30
PA
10248 /* Make sure the remote is pointing at the right process, if
10249 necessary. */
10250 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10251 set_general_process ();
10252
bba74b36 10253 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 10254 p = strchr (rs->buf, '\0');
96baa820
JM
10255 addr = remote_address_masked (addr);
10256 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10257 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
10258 putpkt (rs->buf);
10259 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10260
6d820c5c 10261 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10262 {
10263 case PACKET_ERROR:
10264 case PACKET_UNKNOWN:
10265 return -1;
10266 case PACKET_OK:
10267 return 0;
10268 }
8e65ff28 10269 internal_error (__FILE__, __LINE__,
e2e0b3e5 10270 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
10271}
10272
3c3bea1c 10273
501eef12 10274int remote_hw_watchpoint_limit = -1;
480a3f21 10275int remote_hw_watchpoint_length_limit = -1;
501eef12 10276int remote_hw_breakpoint_limit = -1;
d471ea57 10277
f6ac5f3d
PA
10278int
10279remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
480a3f21
PW
10280{
10281 if (remote_hw_watchpoint_length_limit == 0)
10282 return 0;
10283 else if (remote_hw_watchpoint_length_limit < 0)
10284 return 1;
10285 else if (len <= remote_hw_watchpoint_length_limit)
10286 return 1;
10287 else
10288 return 0;
10289}
10290
f6ac5f3d
PA
10291int
10292remote_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
96baa820 10293{
3c3bea1c
GS
10294 if (type == bp_hardware_breakpoint)
10295 {
10296 if (remote_hw_breakpoint_limit == 0)
10297 return 0;
501eef12
AC
10298 else if (remote_hw_breakpoint_limit < 0)
10299 return 1;
3c3bea1c
GS
10300 else if (cnt <= remote_hw_breakpoint_limit)
10301 return 1;
10302 }
10303 else
10304 {
10305 if (remote_hw_watchpoint_limit == 0)
10306 return 0;
501eef12
AC
10307 else if (remote_hw_watchpoint_limit < 0)
10308 return 1;
3c3bea1c
GS
10309 else if (ot)
10310 return -1;
10311 else if (cnt <= remote_hw_watchpoint_limit)
10312 return 1;
10313 }
10314 return -1;
10315}
10316
f7e6eed5
PA
10317/* The to_stopped_by_sw_breakpoint method of target remote. */
10318
57810aa7 10319bool
f6ac5f3d 10320remote_target::stopped_by_sw_breakpoint ()
f7e6eed5 10321{
799a2abe 10322 struct thread_info *thread = inferior_thread ();
f7e6eed5 10323
799a2abe 10324 return (thread->priv != NULL
7aabaf9d
SM
10325 && (get_remote_thread_info (thread)->stop_reason
10326 == TARGET_STOPPED_BY_SW_BREAKPOINT));
f7e6eed5
PA
10327}
10328
10329/* The to_supports_stopped_by_sw_breakpoint method of target
10330 remote. */
10331
57810aa7 10332bool
f6ac5f3d 10333remote_target::supports_stopped_by_sw_breakpoint ()
f7e6eed5 10334{
f7e6eed5
PA
10335 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
10336}
10337
10338/* The to_stopped_by_hw_breakpoint method of target remote. */
10339
57810aa7 10340bool
f6ac5f3d 10341remote_target::stopped_by_hw_breakpoint ()
f7e6eed5 10342{
799a2abe 10343 struct thread_info *thread = inferior_thread ();
f7e6eed5 10344
799a2abe 10345 return (thread->priv != NULL
7aabaf9d
SM
10346 && (get_remote_thread_info (thread)->stop_reason
10347 == TARGET_STOPPED_BY_HW_BREAKPOINT));
f7e6eed5
PA
10348}
10349
10350/* The to_supports_stopped_by_hw_breakpoint method of target
10351 remote. */
10352
57810aa7 10353bool
f6ac5f3d 10354remote_target::supports_stopped_by_hw_breakpoint ()
f7e6eed5 10355{
f7e6eed5
PA
10356 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
10357}
10358
57810aa7 10359bool
f6ac5f3d 10360remote_target::stopped_by_watchpoint ()
3c3bea1c 10361{
799a2abe 10362 struct thread_info *thread = inferior_thread ();
ee154bee 10363
799a2abe 10364 return (thread->priv != NULL
7aabaf9d
SM
10365 && (get_remote_thread_info (thread)->stop_reason
10366 == TARGET_STOPPED_BY_WATCHPOINT));
3c3bea1c
GS
10367}
10368
57810aa7 10369bool
f6ac5f3d 10370remote_target::stopped_data_address (CORE_ADDR *addr_p)
3c3bea1c 10371{
799a2abe 10372 struct thread_info *thread = inferior_thread ();
a744cf53 10373
799a2abe 10374 if (thread->priv != NULL
7aabaf9d
SM
10375 && (get_remote_thread_info (thread)->stop_reason
10376 == TARGET_STOPPED_BY_WATCHPOINT))
4aa7a7f5 10377 {
7aabaf9d 10378 *addr_p = get_remote_thread_info (thread)->watch_data_address;
57810aa7 10379 return true;
4aa7a7f5
JJ
10380 }
10381
57810aa7 10382 return false;
3c3bea1c
GS
10383}
10384
10385
f6ac5f3d
PA
10386int
10387remote_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
10388 struct bp_target_info *bp_tgt)
3c3bea1c 10389{
0d5ed153 10390 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10391 struct remote_state *rs;
bba74b36 10392 char *p, *endbuf;
dd61ec5c 10393 char *message;
3c3bea1c 10394
4082afcc 10395 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10396 return -1;
2bc416ba 10397
28439a30
PA
10398 /* Make sure the remote is pointing at the right process, if
10399 necessary. */
10400 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10401 set_general_process ();
10402
4fff2411
JZ
10403 rs = get_remote_state ();
10404 p = rs->buf;
bba74b36 10405 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 10406
96baa820
JM
10407 *(p++) = 'Z';
10408 *(p++) = '1';
10409 *(p++) = ',';
802188a7 10410
0d5ed153 10411 addr = remote_address_masked (addr);
96baa820 10412 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10413 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10414
f6ac5f3d 10415 if (supports_evaluation_of_breakpoint_conditions ())
bba74b36 10416 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10417
f6ac5f3d 10418 if (can_run_breakpoint_commands ())
d3ce09f5
SS
10419 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10420
6d820c5c
DJ
10421 putpkt (rs->buf);
10422 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10423
6d820c5c 10424 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10425 {
10426 case PACKET_ERROR:
dd61ec5c
MW
10427 if (rs->buf[1] == '.')
10428 {
10429 message = strchr (rs->buf + 2, '.');
10430 if (message)
0316657e 10431 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
10432 }
10433 return -1;
d471ea57
AC
10434 case PACKET_UNKNOWN:
10435 return -1;
10436 case PACKET_OK:
10437 return 0;
10438 }
8e65ff28 10439 internal_error (__FILE__, __LINE__,
e2e0b3e5 10440 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
10441}
10442
d471ea57 10443
f6ac5f3d
PA
10444int
10445remote_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
10446 struct bp_target_info *bp_tgt)
96baa820 10447{
8181d85f 10448 CORE_ADDR addr;
d01949b6 10449 struct remote_state *rs = get_remote_state ();
6d820c5c 10450 char *p = rs->buf;
bba74b36 10451 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 10452
4082afcc 10453 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10454 return -1;
802188a7 10455
28439a30
PA
10456 /* Make sure the remote is pointing at the right process, if
10457 necessary. */
10458 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10459 set_general_process ();
10460
96baa820
JM
10461 *(p++) = 'z';
10462 *(p++) = '1';
10463 *(p++) = ',';
802188a7 10464
8181d85f 10465 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 10466 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10467 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10468
6d820c5c
DJ
10469 putpkt (rs->buf);
10470 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 10471
6d820c5c 10472 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10473 {
10474 case PACKET_ERROR:
10475 case PACKET_UNKNOWN:
10476 return -1;
10477 case PACKET_OK:
10478 return 0;
10479 }
8e65ff28 10480 internal_error (__FILE__, __LINE__,
e2e0b3e5 10481 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 10482}
96baa820 10483
4a5e7a5b
PA
10484/* Verify memory using the "qCRC:" request. */
10485
f6ac5f3d
PA
10486int
10487remote_target::verify_memory (const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
4a5e7a5b
PA
10488{
10489 struct remote_state *rs = get_remote_state ();
10490 unsigned long host_crc, target_crc;
10491 char *tmp;
10492
936d2992
PA
10493 /* It doesn't make sense to use qCRC if the remote target is
10494 connected but not running. */
10495 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
10496 {
10497 enum packet_result result;
28439a30 10498
936d2992
PA
10499 /* Make sure the remote is pointing at the right process. */
10500 set_general_process ();
4a5e7a5b 10501
936d2992
PA
10502 /* FIXME: assumes lma can fit into long. */
10503 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
10504 (long) lma, (long) size);
10505 putpkt (rs->buf);
4a5e7a5b 10506
936d2992
PA
10507 /* Be clever; compute the host_crc before waiting for target
10508 reply. */
10509 host_crc = xcrc32 (data, size, 0xffffffff);
10510
10511 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 10512
936d2992
PA
10513 result = packet_ok (rs->buf,
10514 &remote_protocol_packets[PACKET_qCRC]);
10515 if (result == PACKET_ERROR)
10516 return -1;
10517 else if (result == PACKET_OK)
10518 {
10519 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
10520 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 10521
936d2992
PA
10522 return (host_crc == target_crc);
10523 }
10524 }
4a5e7a5b 10525
f6ac5f3d 10526 return simple_verify_memory (this, data, lma, size);
4a5e7a5b
PA
10527}
10528
c906108c
SS
10529/* compare-sections command
10530
10531 With no arguments, compares each loadable section in the exec bfd
10532 with the same memory range on the target, and reports mismatches.
4a5e7a5b 10533 Useful for verifying the image on the target against the exec file. */
e514a9d6 10534
c906108c 10535static void
ac88e2de 10536compare_sections_command (const char *args, int from_tty)
c906108c
SS
10537{
10538 asection *s;
ce359b09 10539 const char *sectname;
c906108c
SS
10540 bfd_size_type size;
10541 bfd_vma lma;
10542 int matched = 0;
10543 int mismatched = 0;
4a5e7a5b 10544 int res;
95cf3b38 10545 int read_only = 0;
c906108c
SS
10546
10547 if (!exec_bfd)
8a3fe4f8 10548 error (_("command cannot be used without an exec file"));
c906108c 10549
95cf3b38
DT
10550 if (args != NULL && strcmp (args, "-r") == 0)
10551 {
10552 read_only = 1;
10553 args = NULL;
10554 }
10555
c5aa993b 10556 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
10557 {
10558 if (!(s->flags & SEC_LOAD))
0df8b418 10559 continue; /* Skip non-loadable section. */
c906108c 10560
95cf3b38
DT
10561 if (read_only && (s->flags & SEC_READONLY) == 0)
10562 continue; /* Skip writeable sections */
10563
2c500098 10564 size = bfd_get_section_size (s);
c906108c 10565 if (size == 0)
0df8b418 10566 continue; /* Skip zero-length section. */
c906108c 10567
ce359b09 10568 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 10569 if (args && strcmp (args, sectname) != 0)
0df8b418 10570 continue; /* Not the section selected by user. */
c906108c 10571
0df8b418 10572 matched = 1; /* Do this section. */
c906108c 10573 lma = s->lma;
c906108c 10574
b80406ac
TT
10575 gdb::byte_vector sectdata (size);
10576 bfd_get_section_contents (exec_bfd, s, sectdata.data (), 0, size);
c906108c 10577
b80406ac 10578 res = target_verify_memory (sectdata.data (), lma, size);
4a5e7a5b
PA
10579
10580 if (res == -1)
5af949e3 10581 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
10582 paddress (target_gdbarch (), lma),
10583 paddress (target_gdbarch (), lma + size));
c906108c 10584
5af949e3 10585 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
10586 paddress (target_gdbarch (), lma),
10587 paddress (target_gdbarch (), lma + size));
4a5e7a5b 10588 if (res)
c906108c
SS
10589 printf_filtered ("matched.\n");
10590 else
c5aa993b
JM
10591 {
10592 printf_filtered ("MIS-MATCHED!\n");
10593 mismatched++;
10594 }
c906108c
SS
10595 }
10596 if (mismatched > 0)
936d2992 10597 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 10598the loaded file\n"));
c906108c 10599 if (args && !matched)
a3f17187 10600 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
10601}
10602
0e7f50da
UW
10603/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
10604 into remote target. The number of bytes written to the remote
10605 target is returned, or -1 for error. */
10606
9b409511 10607static enum target_xfer_status
f6ac5f3d 10608remote_write_qxfer (const char *object_name,
0e7f50da 10609 const char *annex, const gdb_byte *writebuf,
9b409511 10610 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
10611 struct packet_config *packet)
10612{
10613 int i, buf_len;
10614 ULONGEST n;
0e7f50da
UW
10615 struct remote_state *rs = get_remote_state ();
10616 int max_size = get_memory_write_packet_size ();
10617
7cc244de 10618 if (packet_config_support (packet) == PACKET_DISABLE)
2ed4b548 10619 return TARGET_XFER_E_IO;
0e7f50da
UW
10620
10621 /* Insert header. */
10622 i = snprintf (rs->buf, max_size,
10623 "qXfer:%s:write:%s:%s:",
10624 object_name, annex ? annex : "",
10625 phex_nz (offset, sizeof offset));
10626 max_size -= (i + 1);
10627
10628 /* Escape as much data as fits into rs->buf. */
10629 buf_len = remote_escape_output
124e13d9 10630 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
10631
10632 if (putpkt_binary (rs->buf, i + buf_len) < 0
10633 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10634 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10635 return TARGET_XFER_E_IO;
0e7f50da
UW
10636
10637 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
10638
10639 *xfered_len = n;
92ffd475 10640 return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
0e7f50da
UW
10641}
10642
0876f84a
DJ
10643/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
10644 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
10645 number of bytes read is returned, or 0 for EOF, or -1 for error.
10646 The number of bytes read may be less than LEN without indicating an
10647 EOF. PACKET is checked and updated to indicate whether the remote
10648 target supports this object. */
10649
9b409511 10650static enum target_xfer_status
f6ac5f3d 10651remote_read_qxfer (const char *object_name,
0876f84a
DJ
10652 const char *annex,
10653 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 10654 ULONGEST *xfered_len,
0876f84a
DJ
10655 struct packet_config *packet)
10656{
0876f84a 10657 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
10658 LONGEST i, n, packet_len;
10659
7cc244de 10660 if (packet_config_support (packet) == PACKET_DISABLE)
2ed4b548 10661 return TARGET_XFER_E_IO;
0876f84a
DJ
10662
10663 /* Check whether we've cached an end-of-object packet that matches
10664 this request. */
8e88304f 10665 if (rs->finished_object)
0876f84a 10666 {
8e88304f
TT
10667 if (strcmp (object_name, rs->finished_object) == 0
10668 && strcmp (annex ? annex : "", rs->finished_annex) == 0
10669 && offset == rs->finished_offset)
9b409511
YQ
10670 return TARGET_XFER_EOF;
10671
0876f84a
DJ
10672
10673 /* Otherwise, we're now reading something different. Discard
10674 the cache. */
8e88304f
TT
10675 xfree (rs->finished_object);
10676 xfree (rs->finished_annex);
10677 rs->finished_object = NULL;
10678 rs->finished_annex = NULL;
0876f84a
DJ
10679 }
10680
10681 /* Request only enough to fit in a single packet. The actual data
10682 may not, since we don't know how much of it will need to be escaped;
10683 the target is free to respond with slightly less data. We subtract
10684 five to account for the response type and the protocol frame. */
768adc05 10685 n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
0876f84a
DJ
10686 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
10687 object_name, annex ? annex : "",
10688 phex_nz (offset, sizeof offset),
10689 phex_nz (n, sizeof n));
10690 i = putpkt (rs->buf);
10691 if (i < 0)
2ed4b548 10692 return TARGET_XFER_E_IO;
0876f84a
DJ
10693
10694 rs->buf[0] = '\0';
10695 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10696 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10697 return TARGET_XFER_E_IO;
0876f84a
DJ
10698
10699 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
10700 error (_("Unknown remote qXfer reply: %s"), rs->buf);
10701
10702 /* 'm' means there is (or at least might be) more data after this
10703 batch. That does not make sense unless there's at least one byte
10704 of data in this reply. */
10705 if (rs->buf[0] == 'm' && packet_len == 1)
10706 error (_("Remote qXfer reply contained no data."));
10707
10708 /* Got some data. */
bc20a4af
PA
10709 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
10710 packet_len - 1, readbuf, n);
0876f84a
DJ
10711
10712 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
10713 or possibly empty. If we have the final block of a non-empty
10714 object, record this fact to bypass a subsequent partial read. */
10715 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 10716 {
8e88304f
TT
10717 rs->finished_object = xstrdup (object_name);
10718 rs->finished_annex = xstrdup (annex ? annex : "");
10719 rs->finished_offset = offset + i;
0876f84a
DJ
10720 }
10721
9b409511
YQ
10722 if (i == 0)
10723 return TARGET_XFER_EOF;
10724 else
10725 {
10726 *xfered_len = i;
10727 return TARGET_XFER_OK;
10728 }
0876f84a
DJ
10729}
10730
f6ac5f3d
PA
10731enum target_xfer_status
10732remote_target::xfer_partial (enum target_object object,
10733 const char *annex, gdb_byte *readbuf,
10734 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
10735 ULONGEST *xfered_len)
c906108c 10736{
82f73884 10737 struct remote_state *rs;
c906108c 10738 int i;
6d820c5c 10739 char *p2;
1e3ff5ad 10740 char query_type;
124e13d9 10741 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 10742
e6e4e701 10743 set_remote_traceframe ();
82f73884
PA
10744 set_general_thread (inferior_ptid);
10745
10746 rs = get_remote_state ();
10747
b2182ed2 10748 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
10749 if (object == TARGET_OBJECT_MEMORY)
10750 {
2d717e4f
DJ
10751 /* If the remote target is connected but not running, we should
10752 pass this request down to a lower stratum (e.g. the executable
10753 file). */
10754 if (!target_has_execution)
9b409511 10755 return TARGET_XFER_EOF;
2d717e4f 10756
21e3b9b9 10757 if (writebuf != NULL)
124e13d9
SM
10758 return remote_write_bytes (offset, writebuf, len, unit_size,
10759 xfered_len);
21e3b9b9 10760 else
f6ac5f3d 10761 return remote_read_bytes (this, offset, readbuf, len, unit_size,
124e13d9 10762 xfered_len);
21e3b9b9
DJ
10763 }
10764
0df8b418 10765 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
10766 if (object == TARGET_OBJECT_SPU)
10767 {
10768 if (readbuf)
f6ac5f3d 10769 return remote_read_qxfer ("spu", annex, readbuf, offset, len,
9b409511
YQ
10770 xfered_len, &remote_protocol_packets
10771 [PACKET_qXfer_spu_read]);
0e7f50da 10772 else
f6ac5f3d 10773 return remote_write_qxfer ("spu", annex, writebuf, offset, len,
9b409511
YQ
10774 xfered_len, &remote_protocol_packets
10775 [PACKET_qXfer_spu_write]);
0e7f50da
UW
10776 }
10777
4aa995e1
PA
10778 /* Handle extra signal info using qxfer packets. */
10779 if (object == TARGET_OBJECT_SIGNAL_INFO)
10780 {
10781 if (readbuf)
f6ac5f3d 10782 return remote_read_qxfer ("siginfo", annex, readbuf, offset, len,
9b409511 10783 xfered_len, &remote_protocol_packets
4aa995e1
PA
10784 [PACKET_qXfer_siginfo_read]);
10785 else
f6ac5f3d 10786 return remote_write_qxfer ("siginfo", annex,
9b409511 10787 writebuf, offset, len, xfered_len,
4aa995e1
PA
10788 &remote_protocol_packets
10789 [PACKET_qXfer_siginfo_write]);
10790 }
10791
0fb4aa4b
PA
10792 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
10793 {
10794 if (readbuf)
f6ac5f3d 10795 return remote_read_qxfer ("statictrace", annex,
9b409511 10796 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
10797 &remote_protocol_packets
10798 [PACKET_qXfer_statictrace_read]);
10799 else
2ed4b548 10800 return TARGET_XFER_E_IO;
0fb4aa4b
PA
10801 }
10802
a76d924d
DJ
10803 /* Only handle flash writes. */
10804 if (writebuf != NULL)
10805 {
a76d924d
DJ
10806 switch (object)
10807 {
10808 case TARGET_OBJECT_FLASH:
f6ac5f3d 10809 return remote_flash_write (this, offset, len, xfered_len,
9b409511 10810 writebuf);
a76d924d
DJ
10811
10812 default:
2ed4b548 10813 return TARGET_XFER_E_IO;
a76d924d
DJ
10814 }
10815 }
4b8a223f 10816
1e3ff5ad
AC
10817 /* Map pre-existing objects onto letters. DO NOT do this for new
10818 objects!!! Instead specify new query packets. */
10819 switch (object)
c906108c 10820 {
1e3ff5ad
AC
10821 case TARGET_OBJECT_AVR:
10822 query_type = 'R';
10823 break;
802188a7
RM
10824
10825 case TARGET_OBJECT_AUXV:
0876f84a 10826 gdb_assert (annex == NULL);
f6ac5f3d 10827 return remote_read_qxfer ("auxv", annex, readbuf, offset, len,
9b409511 10828 xfered_len,
0876f84a 10829 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 10830
23181151
DJ
10831 case TARGET_OBJECT_AVAILABLE_FEATURES:
10832 return remote_read_qxfer
f6ac5f3d 10833 ("features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
10834 &remote_protocol_packets[PACKET_qXfer_features]);
10835
cfa9d6d9
DJ
10836 case TARGET_OBJECT_LIBRARIES:
10837 return remote_read_qxfer
f6ac5f3d 10838 ("libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
10839 &remote_protocol_packets[PACKET_qXfer_libraries]);
10840
2268b414
JK
10841 case TARGET_OBJECT_LIBRARIES_SVR4:
10842 return remote_read_qxfer
f6ac5f3d 10843 ("libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
10844 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
10845
fd79ecee
DJ
10846 case TARGET_OBJECT_MEMORY_MAP:
10847 gdb_assert (annex == NULL);
f6ac5f3d 10848 return remote_read_qxfer ("memory-map", annex, readbuf, offset, len,
9b409511 10849 xfered_len,
fd79ecee
DJ
10850 &remote_protocol_packets[PACKET_qXfer_memory_map]);
10851
07e059b5
VP
10852 case TARGET_OBJECT_OSDATA:
10853 /* Should only get here if we're connected. */
5d93a237 10854 gdb_assert (rs->remote_desc);
07e059b5 10855 return remote_read_qxfer
f6ac5f3d 10856 ("osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
10857 &remote_protocol_packets[PACKET_qXfer_osdata]);
10858
dc146f7c
VP
10859 case TARGET_OBJECT_THREADS:
10860 gdb_assert (annex == NULL);
f6ac5f3d 10861 return remote_read_qxfer ("threads", annex, readbuf, offset, len,
9b409511 10862 xfered_len,
dc146f7c
VP
10863 &remote_protocol_packets[PACKET_qXfer_threads]);
10864
b3b9301e
PA
10865 case TARGET_OBJECT_TRACEFRAME_INFO:
10866 gdb_assert (annex == NULL);
10867 return remote_read_qxfer
f6ac5f3d 10868 ("traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 10869 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
10870
10871 case TARGET_OBJECT_FDPIC:
f6ac5f3d 10872 return remote_read_qxfer ("fdpic", annex, readbuf, offset, len,
9b409511 10873 xfered_len,
78d85199 10874 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
10875
10876 case TARGET_OBJECT_OPENVMS_UIB:
f6ac5f3d 10877 return remote_read_qxfer ("uib", annex, readbuf, offset, len,
9b409511 10878 xfered_len,
169081d0
TG
10879 &remote_protocol_packets[PACKET_qXfer_uib]);
10880
9accd112 10881 case TARGET_OBJECT_BTRACE:
f6ac5f3d 10882 return remote_read_qxfer ("btrace", annex, readbuf, offset, len,
9b409511 10883 xfered_len,
9accd112
MM
10884 &remote_protocol_packets[PACKET_qXfer_btrace]);
10885
f4abbc16 10886 case TARGET_OBJECT_BTRACE_CONF:
f6ac5f3d 10887 return remote_read_qxfer ("btrace-conf", annex, readbuf, offset,
f4abbc16
MM
10888 len, xfered_len,
10889 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
10890
c78fa86a 10891 case TARGET_OBJECT_EXEC_FILE:
f6ac5f3d 10892 return remote_read_qxfer ("exec-file", annex, readbuf, offset,
c78fa86a
GB
10893 len, xfered_len,
10894 &remote_protocol_packets[PACKET_qXfer_exec_file]);
10895
1e3ff5ad 10896 default:
2ed4b548 10897 return TARGET_XFER_E_IO;
c906108c
SS
10898 }
10899
0df8b418 10900 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 10901 large enough let the caller deal with it. */
ea9c271d 10902 if (len < get_remote_packet_size ())
2ed4b548 10903 return TARGET_XFER_E_IO;
ea9c271d 10904 len = get_remote_packet_size ();
1e3ff5ad 10905
23860348 10906 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 10907 if (!rs->remote_desc)
8a3fe4f8 10908 error (_("remote query is only available after target open"));
c906108c 10909
1e3ff5ad 10910 gdb_assert (annex != NULL);
4b8a223f 10911 gdb_assert (readbuf != NULL);
c906108c 10912
6d820c5c 10913 p2 = rs->buf;
c906108c
SS
10914 *p2++ = 'q';
10915 *p2++ = query_type;
10916
23860348
MS
10917 /* We used one buffer char for the remote protocol q command and
10918 another for the query type. As the remote protocol encapsulation
10919 uses 4 chars plus one extra in case we are debugging
10920 (remote_debug), we have PBUFZIZ - 7 left to pack the query
10921 string. */
c906108c 10922 i = 0;
ea9c271d 10923 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 10924 {
1e3ff5ad
AC
10925 /* Bad caller may have sent forbidden characters. */
10926 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
10927 *p2++ = annex[i];
c906108c
SS
10928 i++;
10929 }
1e3ff5ad
AC
10930 *p2 = '\0';
10931 gdb_assert (annex[i] == '\0');
c906108c 10932
6d820c5c 10933 i = putpkt (rs->buf);
c5aa993b 10934 if (i < 0)
2ed4b548 10935 return TARGET_XFER_E_IO;
c906108c 10936
6d820c5c
DJ
10937 getpkt (&rs->buf, &rs->buf_size, 0);
10938 strcpy ((char *) readbuf, rs->buf);
c906108c 10939
9b409511 10940 *xfered_len = strlen ((char *) readbuf);
92ffd475 10941 return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
c906108c
SS
10942}
10943
09c98b44
DB
10944/* Implementation of to_get_memory_xfer_limit. */
10945
f6ac5f3d
PA
10946ULONGEST
10947remote_target::get_memory_xfer_limit ()
09c98b44
DB
10948{
10949 return get_memory_write_packet_size ();
10950}
10951
f6ac5f3d
PA
10952int
10953remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
10954 const gdb_byte *pattern, ULONGEST pattern_len,
10955 CORE_ADDR *found_addrp)
08388c79 10956{
f5656ead 10957 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
10958 struct remote_state *rs = get_remote_state ();
10959 int max_size = get_memory_write_packet_size ();
10960 struct packet_config *packet =
10961 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
10962 /* Number of packet bytes used to encode the pattern;
10963 this could be more than PATTERN_LEN due to escape characters. */
08388c79 10964 int escaped_pattern_len;
0df8b418 10965 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
10966 int used_pattern_len;
10967 int i;
10968 int found;
10969 ULONGEST found_addr;
10970
7cc244de
PA
10971 /* Don't go to the target if we don't have to. This is done before
10972 checking packet_config_support to avoid the possibility that a
10973 success for this edge case means the facility works in
10974 general. */
08388c79
DE
10975 if (pattern_len > search_space_len)
10976 return 0;
10977 if (pattern_len == 0)
10978 {
10979 *found_addrp = start_addr;
10980 return 1;
10981 }
10982
10983 /* If we already know the packet isn't supported, fall back to the simple
10984 way of searching memory. */
10985
4082afcc 10986 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
10987 {
10988 /* Target doesn't provided special support, fall back and use the
10989 standard support (copy memory and do the search here). */
f6ac5f3d 10990 return simple_search_memory (this, start_addr, search_space_len,
08388c79
DE
10991 pattern, pattern_len, found_addrp);
10992 }
10993
28439a30
PA
10994 /* Make sure the remote is pointing at the right process. */
10995 set_general_process ();
10996
08388c79
DE
10997 /* Insert header. */
10998 i = snprintf (rs->buf, max_size,
10999 "qSearch:memory:%s;%s;",
5af949e3 11000 phex_nz (start_addr, addr_size),
08388c79
DE
11001 phex_nz (search_space_len, sizeof (search_space_len)));
11002 max_size -= (i + 1);
11003
11004 /* Escape as much data as fits into rs->buf. */
11005 escaped_pattern_len =
124e13d9 11006 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
11007 &used_pattern_len, max_size);
11008
11009 /* Bail if the pattern is too large. */
11010 if (used_pattern_len != pattern_len)
9b20d036 11011 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
11012
11013 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
11014 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
11015 || packet_ok (rs->buf, packet) != PACKET_OK)
11016 {
11017 /* The request may not have worked because the command is not
11018 supported. If so, fall back to the simple way. */
7cc244de 11019 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79 11020 {
f6ac5f3d 11021 return simple_search_memory (this, start_addr, search_space_len,
08388c79
DE
11022 pattern, pattern_len, found_addrp);
11023 }
11024 return -1;
11025 }
11026
11027 if (rs->buf[0] == '0')
11028 found = 0;
11029 else if (rs->buf[0] == '1')
11030 {
11031 found = 1;
11032 if (rs->buf[1] != ',')
10e0fa18 11033 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
11034 unpack_varlen_hex (rs->buf + 2, &found_addr);
11035 *found_addrp = found_addr;
11036 }
11037 else
10e0fa18 11038 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
11039
11040 return found;
11041}
11042
f6ac5f3d
PA
11043void
11044remote_target::rcmd (const char *command, struct ui_file *outbuf)
96baa820 11045{
d01949b6 11046 struct remote_state *rs = get_remote_state ();
2e9f7625 11047 char *p = rs->buf;
96baa820 11048
5d93a237 11049 if (!rs->remote_desc)
8a3fe4f8 11050 error (_("remote rcmd is only available after target open"));
96baa820 11051
23860348 11052 /* Send a NULL command across as an empty command. */
7be570e7
JM
11053 if (command == NULL)
11054 command = "";
11055
23860348 11056 /* The query prefix. */
2e9f7625
DJ
11057 strcpy (rs->buf, "qRcmd,");
11058 p = strchr (rs->buf, '\0');
96baa820 11059
3e43a32a
MS
11060 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
11061 > get_remote_packet_size ())
8a3fe4f8 11062 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 11063
23860348 11064 /* Encode the actual command. */
a30bf1f1 11065 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 11066
6d820c5c 11067 if (putpkt (rs->buf) < 0)
8a3fe4f8 11068 error (_("Communication problem with target."));
96baa820
JM
11069
11070 /* get/display the response */
11071 while (1)
11072 {
2e9f7625
DJ
11073 char *buf;
11074
00bf0b85 11075 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 11076 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 11077 rs->buf[0] = '\0';
5b37825d
PW
11078 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
11079 {
11080 /* Timeout. Continue to (try to) read responses.
11081 This is better than stopping with an error, assuming the stub
11082 is still executing the (long) monitor command.
11083 If needed, the user can interrupt gdb using C-c, obtaining
11084 an effect similar to stop on timeout. */
11085 continue;
11086 }
2e9f7625 11087 buf = rs->buf;
96baa820 11088 if (buf[0] == '\0')
8a3fe4f8 11089 error (_("Target does not support this command."));
96baa820
JM
11090 if (buf[0] == 'O' && buf[1] != 'K')
11091 {
23860348 11092 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
11093 continue;
11094 }
11095 if (strcmp (buf, "OK") == 0)
11096 break;
7be570e7
JM
11097 if (strlen (buf) == 3 && buf[0] == 'E'
11098 && isdigit (buf[1]) && isdigit (buf[2]))
11099 {
8a3fe4f8 11100 error (_("Protocol error with Rcmd"));
7be570e7 11101 }
96baa820
JM
11102 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
11103 {
11104 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 11105
96baa820
JM
11106 fputc_unfiltered (c, outbuf);
11107 }
11108 break;
11109 }
11110}
11111
f6ac5f3d
PA
11112std::vector<mem_region>
11113remote_target::memory_map ()
fd79ecee 11114{
a664f67e 11115 std::vector<mem_region> result;
9018be22 11116 gdb::optional<gdb::char_vector> text
f6ac5f3d 11117 = target_read_stralloc (target_stack, TARGET_OBJECT_MEMORY_MAP, NULL);
fd79ecee
DJ
11118
11119 if (text)
9018be22 11120 result = parse_memory_map (text->data ());
fd79ecee
DJ
11121
11122 return result;
11123}
11124
c906108c 11125static void
ac88e2de 11126packet_command (const char *args, int from_tty)
c906108c 11127{
d01949b6 11128 struct remote_state *rs = get_remote_state ();
c906108c 11129
5d93a237 11130 if (!rs->remote_desc)
8a3fe4f8 11131 error (_("command can only be used with remote target"));
c906108c 11132
c5aa993b 11133 if (!args)
8a3fe4f8 11134 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
11135
11136 puts_filtered ("sending: ");
11137 print_packet (args);
11138 puts_filtered ("\n");
11139 putpkt (args);
11140
6d820c5c 11141 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 11142 puts_filtered ("received: ");
6d820c5c 11143 print_packet (rs->buf);
c906108c
SS
11144 puts_filtered ("\n");
11145}
11146
11147#if 0
23860348 11148/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 11149
a14ed312 11150static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 11151
a14ed312 11152static void threadset_test_cmd (char *cmd, int tty);
c906108c 11153
a14ed312 11154static void threadalive_test (char *cmd, int tty);
c906108c 11155
a14ed312 11156static void threadlist_test_cmd (char *cmd, int tty);
c906108c 11157
23860348 11158int get_and_display_threadinfo (threadref *ref);
c906108c 11159
a14ed312 11160static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 11161
23860348 11162static int thread_display_step (threadref *ref, void *context);
c906108c 11163
a14ed312 11164static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 11165
a14ed312 11166static void init_remote_threadtests (void);
c906108c 11167
23860348 11168#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
11169
11170static void
0b39b52e 11171threadset_test_cmd (const char *cmd, int tty)
c906108c
SS
11172{
11173 int sample_thread = SAMPLE_THREAD;
11174
a3f17187 11175 printf_filtered (_("Remote threadset test\n"));
79d7f229 11176 set_general_thread (sample_thread);
c906108c
SS
11177}
11178
11179
11180static void
0b39b52e 11181threadalive_test (const char *cmd, int tty)
c906108c
SS
11182{
11183 int sample_thread = SAMPLE_THREAD;
79d7f229 11184 int pid = ptid_get_pid (inferior_ptid);
ba348170 11185 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 11186
79d7f229 11187 if (remote_thread_alive (ptid))
c906108c
SS
11188 printf_filtered ("PASS: Thread alive test\n");
11189 else
11190 printf_filtered ("FAIL: Thread alive test\n");
11191}
11192
23860348 11193void output_threadid (char *title, threadref *ref);
c906108c
SS
11194
11195void
fba45db2 11196output_threadid (char *title, threadref *ref)
c906108c
SS
11197{
11198 char hexid[20];
11199
23860348 11200 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
11201 hexid[16] = 0;
11202 printf_filtered ("%s %s\n", title, (&hexid[0]));
11203}
11204
11205static void
0b39b52e 11206threadlist_test_cmd (const char *cmd, int tty)
c906108c
SS
11207{
11208 int startflag = 1;
11209 threadref nextthread;
11210 int done, result_count;
11211 threadref threadlist[3];
11212
11213 printf_filtered ("Remote Threadlist test\n");
11214 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
11215 &result_count, &threadlist[0]))
11216 printf_filtered ("FAIL: threadlist test\n");
11217 else
11218 {
11219 threadref *scan = threadlist;
11220 threadref *limit = scan + result_count;
11221
11222 while (scan < limit)
11223 output_threadid (" thread ", scan++);
11224 }
11225}
11226
11227void
fba45db2 11228display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
11229{
11230 output_threadid ("Threadid: ", &info->threadid);
11231 printf_filtered ("Name: %s\n ", info->shortname);
11232 printf_filtered ("State: %s\n", info->display);
11233 printf_filtered ("other: %s\n\n", info->more_display);
11234}
11235
11236int
fba45db2 11237get_and_display_threadinfo (threadref *ref)
c906108c
SS
11238{
11239 int result;
11240 int set;
11241 struct gdb_ext_thread_info threadinfo;
11242
11243 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
11244 | TAG_MOREDISPLAY | TAG_DISPLAY;
11245 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
11246 display_thread_info (&threadinfo);
11247 return result;
11248}
11249
11250static void
0b39b52e 11251threadinfo_test_cmd (const char *cmd, int tty)
c906108c
SS
11252{
11253 int athread = SAMPLE_THREAD;
11254 threadref thread;
11255 int set;
11256
11257 int_to_threadref (&thread, athread);
11258 printf_filtered ("Remote Threadinfo test\n");
11259 if (!get_and_display_threadinfo (&thread))
11260 printf_filtered ("FAIL cannot get thread info\n");
11261}
11262
11263static int
fba45db2 11264thread_display_step (threadref *ref, void *context)
c906108c
SS
11265{
11266 /* output_threadid(" threadstep ",ref); *//* simple test */
11267 return get_and_display_threadinfo (ref);
11268}
11269
11270static void
0b39b52e 11271threadlist_update_test_cmd (const char *cmd, int tty)
c906108c
SS
11272{
11273 printf_filtered ("Remote Threadlist update test\n");
11274 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
11275}
11276
11277static void
11278init_remote_threadtests (void)
11279{
3e43a32a
MS
11280 add_com ("tlist", class_obscure, threadlist_test_cmd,
11281 _("Fetch and print the remote list of "
11282 "thread identifiers, one pkt only"));
c906108c 11283 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 11284 _("Fetch and display info about one thread"));
c906108c 11285 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 11286 _("Test setting to a different thread"));
c906108c 11287 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 11288 _("Iterate through updating all remote thread info"));
c906108c 11289 add_com ("talive", class_obscure, threadalive_test,
1bedd215 11290 _(" Remote thread alive test "));
c906108c
SS
11291}
11292
11293#endif /* 0 */
11294
f3fb8c85
MS
11295/* Convert a thread ID to a string. Returns the string in a static
11296 buffer. */
11297
f6ac5f3d
PA
11298const char *
11299remote_target::pid_to_str (ptid_t ptid)
f3fb8c85 11300{
79d7f229 11301 static char buf[64];
82f73884 11302 struct remote_state *rs = get_remote_state ();
f3fb8c85 11303
7cee1e54
PA
11304 if (ptid_equal (ptid, null_ptid))
11305 return normal_pid_to_str (ptid);
11306 else if (ptid_is_pid (ptid))
ecd0ada5
PA
11307 {
11308 /* Printing an inferior target id. */
11309
11310 /* When multi-process extensions are off, there's no way in the
11311 remote protocol to know the remote process id, if there's any
11312 at all. There's one exception --- when we're connected with
11313 target extended-remote, and we manually attached to a process
11314 with "attach PID". We don't record anywhere a flag that
11315 allows us to distinguish that case from the case of
11316 connecting with extended-remote and the stub already being
11317 attached to a process, and reporting yes to qAttached, hence
11318 no smart special casing here. */
11319 if (!remote_multi_process_p (rs))
11320 {
11321 xsnprintf (buf, sizeof buf, "Remote target");
11322 return buf;
11323 }
11324
11325 return normal_pid_to_str (ptid);
82f73884 11326 }
ecd0ada5 11327 else
79d7f229 11328 {
ecd0ada5
PA
11329 if (ptid_equal (magic_null_ptid, ptid))
11330 xsnprintf (buf, sizeof buf, "Thread <main>");
8020350c 11331 else if (remote_multi_process_p (rs))
de0d863e
DB
11332 if (ptid_get_lwp (ptid) == 0)
11333 return normal_pid_to_str (ptid);
11334 else
11335 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
11336 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
11337 else
11338 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 11339 ptid_get_lwp (ptid));
79d7f229
PA
11340 return buf;
11341 }
f3fb8c85
MS
11342}
11343
38691318
KB
11344/* Get the address of the thread local variable in OBJFILE which is
11345 stored at OFFSET within the thread local storage for thread PTID. */
11346
f6ac5f3d
PA
11347CORE_ADDR
11348remote_target::get_thread_local_address (ptid_t ptid, CORE_ADDR lm,
11349 CORE_ADDR offset)
38691318 11350{
4082afcc 11351 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
11352 {
11353 struct remote_state *rs = get_remote_state ();
6d820c5c 11354 char *p = rs->buf;
82f73884 11355 char *endp = rs->buf + get_remote_packet_size ();
571dd617 11356 enum packet_result result;
38691318
KB
11357
11358 strcpy (p, "qGetTLSAddr:");
11359 p += strlen (p);
82f73884 11360 p = write_ptid (p, endp, ptid);
38691318
KB
11361 *p++ = ',';
11362 p += hexnumstr (p, offset);
11363 *p++ = ',';
11364 p += hexnumstr (p, lm);
11365 *p++ = '\0';
11366
6d820c5c
DJ
11367 putpkt (rs->buf);
11368 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
11369 result = packet_ok (rs->buf,
11370 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 11371 if (result == PACKET_OK)
38691318
KB
11372 {
11373 ULONGEST result;
11374
6d820c5c 11375 unpack_varlen_hex (rs->buf, &result);
38691318
KB
11376 return result;
11377 }
571dd617 11378 else if (result == PACKET_UNKNOWN)
109c3e39
AC
11379 throw_error (TLS_GENERIC_ERROR,
11380 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 11381 else
109c3e39
AC
11382 throw_error (TLS_GENERIC_ERROR,
11383 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
11384 }
11385 else
109c3e39
AC
11386 throw_error (TLS_GENERIC_ERROR,
11387 _("TLS not supported or disabled on this target"));
38691318
KB
11388 /* Not reached. */
11389 return 0;
11390}
11391
711e434b
PM
11392/* Provide thread local base, i.e. Thread Information Block address.
11393 Returns 1 if ptid is found and thread_local_base is non zero. */
11394
57810aa7 11395bool
f6ac5f3d 11396remote_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
711e434b 11397{
4082afcc 11398 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
11399 {
11400 struct remote_state *rs = get_remote_state ();
11401 char *p = rs->buf;
11402 char *endp = rs->buf + get_remote_packet_size ();
11403 enum packet_result result;
11404
11405 strcpy (p, "qGetTIBAddr:");
11406 p += strlen (p);
11407 p = write_ptid (p, endp, ptid);
11408 *p++ = '\0';
11409
11410 putpkt (rs->buf);
11411 getpkt (&rs->buf, &rs->buf_size, 0);
11412 result = packet_ok (rs->buf,
11413 &remote_protocol_packets[PACKET_qGetTIBAddr]);
11414 if (result == PACKET_OK)
11415 {
11416 ULONGEST result;
11417
11418 unpack_varlen_hex (rs->buf, &result);
11419 if (addr)
11420 *addr = (CORE_ADDR) result;
57810aa7 11421 return true;
711e434b
PM
11422 }
11423 else if (result == PACKET_UNKNOWN)
11424 error (_("Remote target doesn't support qGetTIBAddr packet"));
11425 else
11426 error (_("Remote target failed to process qGetTIBAddr request"));
11427 }
11428 else
11429 error (_("qGetTIBAddr not supported or disabled on this target"));
11430 /* Not reached. */
57810aa7 11431 return false;
711e434b
PM
11432}
11433
29709017
DJ
11434/* Support for inferring a target description based on the current
11435 architecture and the size of a 'g' packet. While the 'g' packet
11436 can have any size (since optional registers can be left off the
11437 end), some sizes are easily recognizable given knowledge of the
11438 approximate architecture. */
11439
11440struct remote_g_packet_guess
11441{
11442 int bytes;
11443 const struct target_desc *tdesc;
11444};
11445typedef struct remote_g_packet_guess remote_g_packet_guess_s;
11446DEF_VEC_O(remote_g_packet_guess_s);
11447
11448struct remote_g_packet_data
11449{
11450 VEC(remote_g_packet_guess_s) *guesses;
11451};
11452
11453static struct gdbarch_data *remote_g_packet_data_handle;
11454
11455static void *
11456remote_g_packet_data_init (struct obstack *obstack)
11457{
11458 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
11459}
11460
11461void
11462register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
11463 const struct target_desc *tdesc)
11464{
11465 struct remote_g_packet_data *data
19ba03f4
SM
11466 = ((struct remote_g_packet_data *)
11467 gdbarch_data (gdbarch, remote_g_packet_data_handle));
29709017
DJ
11468 struct remote_g_packet_guess new_guess, *guess;
11469 int ix;
11470
11471 gdb_assert (tdesc != NULL);
11472
11473 for (ix = 0;
11474 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11475 ix++)
11476 if (guess->bytes == bytes)
11477 internal_error (__FILE__, __LINE__,
9b20d036 11478 _("Duplicate g packet description added for size %d"),
29709017
DJ
11479 bytes);
11480
11481 new_guess.bytes = bytes;
11482 new_guess.tdesc = tdesc;
11483 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
11484}
11485
d962ef82
DJ
11486/* Return 1 if remote_read_description would do anything on this target
11487 and architecture, 0 otherwise. */
11488
11489static int
11490remote_read_description_p (struct target_ops *target)
11491{
11492 struct remote_g_packet_data *data
19ba03f4
SM
11493 = ((struct remote_g_packet_data *)
11494 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
d962ef82
DJ
11495
11496 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11497 return 1;
11498
11499 return 0;
11500}
11501
f6ac5f3d
PA
11502const struct target_desc *
11503remote_target::read_description ()
29709017
DJ
11504{
11505 struct remote_g_packet_data *data
19ba03f4
SM
11506 = ((struct remote_g_packet_data *)
11507 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
29709017 11508
d962ef82
DJ
11509 /* Do not try this during initial connection, when we do not know
11510 whether there is a running but stopped thread. */
11511 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
f6ac5f3d 11512 return beneath->read_description ();
d962ef82 11513
29709017
DJ
11514 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11515 {
11516 struct remote_g_packet_guess *guess;
11517 int ix;
11518 int bytes = send_g_packet ();
11519
11520 for (ix = 0;
11521 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11522 ix++)
11523 if (guess->bytes == bytes)
11524 return guess->tdesc;
11525
11526 /* We discard the g packet. A minor optimization would be to
11527 hold on to it, and fill the register cache once we have selected
11528 an architecture, but it's too tricky to do safely. */
11529 }
11530
f6ac5f3d 11531 return beneath->read_description ();
29709017
DJ
11532}
11533
a6b151f1
DJ
11534/* Remote file transfer support. This is host-initiated I/O, not
11535 target-initiated; for target-initiated, see remote-fileio.c. */
11536
11537/* If *LEFT is at least the length of STRING, copy STRING to
11538 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11539 decrease *LEFT. Otherwise raise an error. */
11540
11541static void
a121b7c1 11542remote_buffer_add_string (char **buffer, int *left, const char *string)
a6b151f1
DJ
11543{
11544 int len = strlen (string);
11545
11546 if (len > *left)
11547 error (_("Packet too long for target."));
11548
11549 memcpy (*buffer, string, len);
11550 *buffer += len;
11551 *left -= len;
11552
11553 /* NUL-terminate the buffer as a convenience, if there is
11554 room. */
11555 if (*left)
11556 **buffer = '\0';
11557}
11558
11559/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
11560 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11561 decrease *LEFT. Otherwise raise an error. */
11562
11563static void
11564remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
11565 int len)
11566{
11567 if (2 * len > *left)
11568 error (_("Packet too long for target."));
11569
11570 bin2hex (bytes, *buffer, len);
11571 *buffer += 2 * len;
11572 *left -= 2 * len;
11573
11574 /* NUL-terminate the buffer as a convenience, if there is
11575 room. */
11576 if (*left)
11577 **buffer = '\0';
11578}
11579
11580/* If *LEFT is large enough, convert VALUE to hex and add it to
11581 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11582 decrease *LEFT. Otherwise raise an error. */
11583
11584static void
11585remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
11586{
11587 int len = hexnumlen (value);
11588
11589 if (len > *left)
11590 error (_("Packet too long for target."));
11591
11592 hexnumstr (*buffer, value);
11593 *buffer += len;
11594 *left -= len;
11595
11596 /* NUL-terminate the buffer as a convenience, if there is
11597 room. */
11598 if (*left)
11599 **buffer = '\0';
11600}
11601
11602/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
11603 value, *REMOTE_ERRNO to the remote error number or zero if none
11604 was included, and *ATTACHMENT to point to the start of the annex
11605 if any. The length of the packet isn't needed here; there may
11606 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
11607
11608 Return 0 if the packet could be parsed, -1 if it could not. If
11609 -1 is returned, the other variables may not be initialized. */
11610
11611static int
11612remote_hostio_parse_result (char *buffer, int *retcode,
11613 int *remote_errno, char **attachment)
11614{
11615 char *p, *p2;
11616
11617 *remote_errno = 0;
11618 *attachment = NULL;
11619
11620 if (buffer[0] != 'F')
11621 return -1;
11622
11623 errno = 0;
11624 *retcode = strtol (&buffer[1], &p, 16);
11625 if (errno != 0 || p == &buffer[1])
11626 return -1;
11627
11628 /* Check for ",errno". */
11629 if (*p == ',')
11630 {
11631 errno = 0;
11632 *remote_errno = strtol (p + 1, &p2, 16);
11633 if (errno != 0 || p + 1 == p2)
11634 return -1;
11635 p = p2;
11636 }
11637
11638 /* Check for ";attachment". If there is no attachment, the
11639 packet should end here. */
11640 if (*p == ';')
11641 {
11642 *attachment = p + 1;
11643 return 0;
11644 }
11645 else if (*p == '\0')
11646 return 0;
11647 else
11648 return -1;
11649}
11650
11651/* Send a prepared I/O packet to the target and read its response.
11652 The prepared packet is in the global RS->BUF before this function
11653 is called, and the answer is there when we return.
11654
11655 COMMAND_BYTES is the length of the request to send, which may include
11656 binary data. WHICH_PACKET is the packet configuration to check
11657 before attempting a packet. If an error occurs, *REMOTE_ERRNO
11658 is set to the error number and -1 is returned. Otherwise the value
11659 returned by the function is returned.
11660
11661 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
11662 attachment is expected; an error will be reported if there's a
11663 mismatch. If one is found, *ATTACHMENT will be set to point into
11664 the packet buffer and *ATTACHMENT_LEN will be set to the
11665 attachment's length. */
11666
11667static int
11668remote_hostio_send_command (int command_bytes, int which_packet,
11669 int *remote_errno, char **attachment,
11670 int *attachment_len)
11671{
11672 struct remote_state *rs = get_remote_state ();
11673 int ret, bytes_read;
11674 char *attachment_tmp;
11675
20db9c52 11676 if (packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
11677 {
11678 *remote_errno = FILEIO_ENOSYS;
11679 return -1;
11680 }
11681
11682 putpkt_binary (rs->buf, command_bytes);
11683 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
11684
11685 /* If it timed out, something is wrong. Don't try to parse the
11686 buffer. */
11687 if (bytes_read < 0)
11688 {
11689 *remote_errno = FILEIO_EINVAL;
11690 return -1;
11691 }
11692
11693 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
11694 {
11695 case PACKET_ERROR:
11696 *remote_errno = FILEIO_EINVAL;
11697 return -1;
11698 case PACKET_UNKNOWN:
11699 *remote_errno = FILEIO_ENOSYS;
11700 return -1;
11701 case PACKET_OK:
11702 break;
11703 }
11704
11705 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
11706 &attachment_tmp))
11707 {
11708 *remote_errno = FILEIO_EINVAL;
11709 return -1;
11710 }
11711
11712 /* Make sure we saw an attachment if and only if we expected one. */
11713 if ((attachment_tmp == NULL && attachment != NULL)
11714 || (attachment_tmp != NULL && attachment == NULL))
11715 {
11716 *remote_errno = FILEIO_EINVAL;
11717 return -1;
11718 }
11719
11720 /* If an attachment was found, it must point into the packet buffer;
11721 work out how many bytes there were. */
11722 if (attachment_tmp != NULL)
11723 {
11724 *attachment = attachment_tmp;
11725 *attachment_len = bytes_read - (*attachment - rs->buf);
11726 }
11727
11728 return ret;
11729}
11730
dd194f6b 11731/* See declaration.h. */
80152258 11732
dd194f6b
PA
11733void
11734readahead_cache::invalidate ()
80152258 11735{
dd194f6b 11736 this->fd = -1;
80152258
PA
11737}
11738
dd194f6b 11739/* See declaration.h. */
80152258 11740
dd194f6b
PA
11741void
11742readahead_cache::invalidate_fd (int fd)
80152258 11743{
dd194f6b
PA
11744 if (this->fd == fd)
11745 this->fd = -1;
80152258
PA
11746}
11747
15a201c8
GB
11748/* Set the filesystem remote_hostio functions that take FILENAME
11749 arguments will use. Return 0 on success, or -1 if an error
11750 occurs (and set *REMOTE_ERRNO). */
11751
11752static int
11753remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
11754{
11755 struct remote_state *rs = get_remote_state ();
11756 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
11757 char *p = rs->buf;
11758 int left = get_remote_packet_size () - 1;
11759 char arg[9];
11760 int ret;
11761
11762 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11763 return 0;
11764
11765 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
11766 return 0;
11767
11768 remote_buffer_add_string (&p, &left, "vFile:setfs:");
11769
11770 xsnprintf (arg, sizeof (arg), "%x", required_pid);
11771 remote_buffer_add_string (&p, &left, arg);
11772
11773 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
11774 remote_errno, NULL, NULL);
11775
11776 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11777 return 0;
11778
11779 if (ret == 0)
11780 rs->fs_pid = required_pid;
11781
11782 return ret;
11783}
11784
12e2a5fd 11785/* Implementation of to_fileio_open. */
a6b151f1
DJ
11786
11787static int
cd897586 11788remote_hostio_open (struct target_ops *self,
07c138c8 11789 struct inferior *inf, const char *filename,
4313b8c0
GB
11790 int flags, int mode, int warn_if_slow,
11791 int *remote_errno)
a6b151f1
DJ
11792{
11793 struct remote_state *rs = get_remote_state ();
11794 char *p = rs->buf;
11795 int left = get_remote_packet_size () - 1;
11796
4313b8c0
GB
11797 if (warn_if_slow)
11798 {
11799 static int warning_issued = 0;
11800
11801 printf_unfiltered (_("Reading %s from remote target...\n"),
11802 filename);
11803
11804 if (!warning_issued)
11805 {
11806 warning (_("File transfers from remote targets can be slow."
11807 " Use \"set sysroot\" to access files locally"
11808 " instead."));
11809 warning_issued = 1;
11810 }
11811 }
11812
15a201c8
GB
11813 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11814 return -1;
11815
a6b151f1
DJ
11816 remote_buffer_add_string (&p, &left, "vFile:open:");
11817
11818 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11819 strlen (filename));
11820 remote_buffer_add_string (&p, &left, ",");
11821
11822 remote_buffer_add_int (&p, &left, flags);
11823 remote_buffer_add_string (&p, &left, ",");
11824
11825 remote_buffer_add_int (&p, &left, mode);
11826
11827 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
11828 remote_errno, NULL, NULL);
11829}
11830
f6ac5f3d
PA
11831int
11832remote_target::fileio_open (struct inferior *inf, const char *filename,
11833 int flags, int mode, int warn_if_slow,
11834 int *remote_errno)
11835{
11836 return remote_hostio_open (this, inf, filename, flags, mode, warn_if_slow,
11837 remote_errno);
11838}
11839
12e2a5fd 11840/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
11841
11842static int
0d866f62
TT
11843remote_hostio_pwrite (struct target_ops *self,
11844 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
11845 ULONGEST offset, int *remote_errno)
11846{
11847 struct remote_state *rs = get_remote_state ();
11848 char *p = rs->buf;
11849 int left = get_remote_packet_size ();
11850 int out_len;
11851
dd194f6b 11852 rs->readahead_cache.invalidate_fd (fd);
80152258 11853
a6b151f1
DJ
11854 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
11855
11856 remote_buffer_add_int (&p, &left, fd);
11857 remote_buffer_add_string (&p, &left, ",");
11858
11859 remote_buffer_add_int (&p, &left, offset);
11860 remote_buffer_add_string (&p, &left, ",");
11861
124e13d9 11862 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
11863 get_remote_packet_size () - (p - rs->buf));
11864
11865 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
11866 remote_errno, NULL, NULL);
11867}
11868
f6ac5f3d
PA
11869int
11870remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
11871 ULONGEST offset, int *remote_errno)
11872{
11873 return remote_hostio_pwrite (this, fd, write_buf, len, offset, remote_errno);
11874}
11875
80152258
PA
11876/* Helper for the implementation of to_fileio_pread. Read the file
11877 from the remote side with vFile:pread. */
a6b151f1
DJ
11878
11879static int
80152258
PA
11880remote_hostio_pread_vFile (struct target_ops *self,
11881 int fd, gdb_byte *read_buf, int len,
11882 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
11883{
11884 struct remote_state *rs = get_remote_state ();
11885 char *p = rs->buf;
11886 char *attachment;
11887 int left = get_remote_packet_size ();
11888 int ret, attachment_len;
11889 int read_len;
11890
11891 remote_buffer_add_string (&p, &left, "vFile:pread:");
11892
11893 remote_buffer_add_int (&p, &left, fd);
11894 remote_buffer_add_string (&p, &left, ",");
11895
11896 remote_buffer_add_int (&p, &left, len);
11897 remote_buffer_add_string (&p, &left, ",");
11898
11899 remote_buffer_add_int (&p, &left, offset);
11900
11901 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
11902 remote_errno, &attachment,
11903 &attachment_len);
11904
11905 if (ret < 0)
11906 return ret;
11907
bc20a4af 11908 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
11909 read_buf, len);
11910 if (read_len != ret)
11911 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
11912
11913 return ret;
11914}
11915
dd194f6b 11916/* See declaration.h. */
80152258 11917
dd194f6b
PA
11918int
11919readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
11920 ULONGEST offset)
80152258 11921{
dd194f6b
PA
11922 if (this->fd == fd
11923 && this->offset <= offset
11924 && offset < this->offset + this->bufsize)
80152258 11925 {
dd194f6b 11926 ULONGEST max = this->offset + this->bufsize;
80152258
PA
11927
11928 if (offset + len > max)
11929 len = max - offset;
11930
dd194f6b 11931 memcpy (read_buf, this->buf + offset - this->offset, len);
80152258
PA
11932 return len;
11933 }
11934
11935 return 0;
11936}
11937
11938/* Implementation of to_fileio_pread. */
11939
11940static int
11941remote_hostio_pread (struct target_ops *self,
11942 int fd, gdb_byte *read_buf, int len,
11943 ULONGEST offset, int *remote_errno)
11944{
11945 int ret;
11946 struct remote_state *rs = get_remote_state ();
dd194f6b 11947 readahead_cache *cache = &rs->readahead_cache;
80152258 11948
dd194f6b 11949 ret = cache->pread (fd, read_buf, len, offset);
80152258
PA
11950 if (ret > 0)
11951 {
11952 cache->hit_count++;
11953
11954 if (remote_debug)
11955 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
11956 pulongest (cache->hit_count));
11957 return ret;
11958 }
11959
11960 cache->miss_count++;
11961 if (remote_debug)
11962 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
11963 pulongest (cache->miss_count));
11964
11965 cache->fd = fd;
11966 cache->offset = offset;
11967 cache->bufsize = get_remote_packet_size ();
224c3ddb 11968 cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
80152258
PA
11969
11970 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
11971 cache->offset, remote_errno);
11972 if (ret <= 0)
11973 {
dd194f6b 11974 cache->invalidate_fd (fd);
80152258
PA
11975 return ret;
11976 }
11977
11978 cache->bufsize = ret;
dd194f6b 11979 return cache->pread (fd, read_buf, len, offset);
80152258
PA
11980}
11981
f6ac5f3d
PA
11982int
11983remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
11984 ULONGEST offset, int *remote_errno)
11985{
11986 return remote_hostio_pread (this, fd, read_buf, len, offset, remote_errno);
11987}
11988
12e2a5fd 11989/* Implementation of to_fileio_close. */
a6b151f1
DJ
11990
11991static int
df39ea25 11992remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
11993{
11994 struct remote_state *rs = get_remote_state ();
11995 char *p = rs->buf;
11996 int left = get_remote_packet_size () - 1;
11997
dd194f6b 11998 rs->readahead_cache.invalidate_fd (fd);
80152258 11999
a6b151f1
DJ
12000 remote_buffer_add_string (&p, &left, "vFile:close:");
12001
12002 remote_buffer_add_int (&p, &left, fd);
12003
12004 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
12005 remote_errno, NULL, NULL);
12006}
12007
f6ac5f3d
PA
12008int
12009remote_target::fileio_close (int fd, int *remote_errno)
12010{
12011 return remote_hostio_close (this, fd, remote_errno);
12012}
12013
12e2a5fd 12014/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
12015
12016static int
dbbca37d 12017remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
12018 struct inferior *inf, const char *filename,
12019 int *remote_errno)
a6b151f1
DJ
12020{
12021 struct remote_state *rs = get_remote_state ();
12022 char *p = rs->buf;
12023 int left = get_remote_packet_size () - 1;
12024
15a201c8
GB
12025 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
12026 return -1;
12027
a6b151f1
DJ
12028 remote_buffer_add_string (&p, &left, "vFile:unlink:");
12029
12030 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
12031 strlen (filename));
12032
12033 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
12034 remote_errno, NULL, NULL);
12035}
12036
f6ac5f3d
PA
12037int
12038remote_target::fileio_unlink (struct inferior *inf, const char *filename,
12039 int *remote_errno)
12040{
12041 return remote_hostio_unlink (this, inf, filename, remote_errno);
12042}
12043
12e2a5fd 12044/* Implementation of to_fileio_readlink. */
b9e7b9c3 12045
f6ac5f3d
PA
12046gdb::optional<std::string>
12047remote_target::fileio_readlink (struct inferior *inf, const char *filename,
12048 int *remote_errno)
b9e7b9c3
UW
12049{
12050 struct remote_state *rs = get_remote_state ();
12051 char *p = rs->buf;
12052 char *attachment;
12053 int left = get_remote_packet_size ();
12054 int len, attachment_len;
12055 int read_len;
b9e7b9c3 12056
15a201c8 12057 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
e0d3522b 12058 return {};
15a201c8 12059
b9e7b9c3
UW
12060 remote_buffer_add_string (&p, &left, "vFile:readlink:");
12061
12062 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
12063 strlen (filename));
12064
12065 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
12066 remote_errno, &attachment,
12067 &attachment_len);
12068
12069 if (len < 0)
e0d3522b 12070 return {};
b9e7b9c3 12071
e0d3522b 12072 std::string ret (len, '\0');
b9e7b9c3 12073
bc20a4af 12074 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
e0d3522b 12075 (gdb_byte *) &ret[0], len);
b9e7b9c3
UW
12076 if (read_len != len)
12077 error (_("Readlink returned %d, but %d bytes."), len, read_len);
12078
b9e7b9c3
UW
12079 return ret;
12080}
12081
12e2a5fd 12082/* Implementation of to_fileio_fstat. */
0a93529c 12083
f6ac5f3d
PA
12084int
12085remote_target::fileio_fstat (int fd, struct stat *st, int *remote_errno)
0a93529c
GB
12086{
12087 struct remote_state *rs = get_remote_state ();
12088 char *p = rs->buf;
12089 int left = get_remote_packet_size ();
12090 int attachment_len, ret;
12091 char *attachment;
12092 struct fio_stat fst;
12093 int read_len;
12094
464b0089
GB
12095 remote_buffer_add_string (&p, &left, "vFile:fstat:");
12096
12097 remote_buffer_add_int (&p, &left, fd);
12098
12099 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
12100 remote_errno, &attachment,
12101 &attachment_len);
12102 if (ret < 0)
0a93529c 12103 {
464b0089
GB
12104 if (*remote_errno != FILEIO_ENOSYS)
12105 return ret;
12106
0a93529c
GB
12107 /* Strictly we should return -1, ENOSYS here, but when
12108 "set sysroot remote:" was implemented in August 2008
12109 BFD's need for a stat function was sidestepped with
12110 this hack. This was not remedied until March 2015
12111 so we retain the previous behavior to avoid breaking
12112 compatibility.
12113
12114 Note that the memset is a March 2015 addition; older
12115 GDBs set st_size *and nothing else* so the structure
12116 would have garbage in all other fields. This might
12117 break something but retaining the previous behavior
12118 here would be just too wrong. */
12119
12120 memset (st, 0, sizeof (struct stat));
12121 st->st_size = INT_MAX;
12122 return 0;
12123 }
12124
0a93529c
GB
12125 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
12126 (gdb_byte *) &fst, sizeof (fst));
12127
12128 if (read_len != ret)
12129 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
12130
12131 if (read_len != sizeof (fst))
12132 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12133 read_len, (int) sizeof (fst));
12134
12135 remote_fileio_to_host_stat (&fst, st);
12136
12137 return 0;
12138}
12139
12e2a5fd 12140/* Implementation of to_filesystem_is_local. */
e3dd7556 12141
57810aa7 12142bool
f6ac5f3d 12143remote_target::filesystem_is_local ()
e3dd7556
GB
12144{
12145 /* Valgrind GDB presents itself as a remote target but works
12146 on the local filesystem: it does not implement remote get
12147 and users are not expected to set a sysroot. To handle
12148 this case we treat the remote filesystem as local if the
12149 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12150 does not support vFile:open. */
a3be80c3 12151 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
12152 {
12153 enum packet_support ps = packet_support (PACKET_vFile_open);
12154
12155 if (ps == PACKET_SUPPORT_UNKNOWN)
12156 {
12157 int fd, remote_errno;
12158
12159 /* Try opening a file to probe support. The supplied
12160 filename is irrelevant, we only care about whether
12161 the stub recognizes the packet or not. */
f6ac5f3d 12162 fd = remote_hostio_open (this, NULL, "just probing",
4313b8c0 12163 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
12164 &remote_errno);
12165
12166 if (fd >= 0)
f6ac5f3d 12167 remote_hostio_close (this, fd, &remote_errno);
e3dd7556
GB
12168
12169 ps = packet_support (PACKET_vFile_open);
12170 }
12171
12172 if (ps == PACKET_DISABLE)
12173 {
12174 static int warning_issued = 0;
12175
12176 if (!warning_issued)
12177 {
12178 warning (_("remote target does not support file"
12179 " transfer, attempting to access files"
12180 " from local filesystem."));
12181 warning_issued = 1;
12182 }
12183
57810aa7 12184 return true;
e3dd7556
GB
12185 }
12186 }
12187
57810aa7 12188 return false;
e3dd7556
GB
12189}
12190
a6b151f1
DJ
12191static int
12192remote_fileio_errno_to_host (int errnum)
12193{
12194 switch (errnum)
12195 {
12196 case FILEIO_EPERM:
12197 return EPERM;
12198 case FILEIO_ENOENT:
12199 return ENOENT;
12200 case FILEIO_EINTR:
12201 return EINTR;
12202 case FILEIO_EIO:
12203 return EIO;
12204 case FILEIO_EBADF:
12205 return EBADF;
12206 case FILEIO_EACCES:
12207 return EACCES;
12208 case FILEIO_EFAULT:
12209 return EFAULT;
12210 case FILEIO_EBUSY:
12211 return EBUSY;
12212 case FILEIO_EEXIST:
12213 return EEXIST;
12214 case FILEIO_ENODEV:
12215 return ENODEV;
12216 case FILEIO_ENOTDIR:
12217 return ENOTDIR;
12218 case FILEIO_EISDIR:
12219 return EISDIR;
12220 case FILEIO_EINVAL:
12221 return EINVAL;
12222 case FILEIO_ENFILE:
12223 return ENFILE;
12224 case FILEIO_EMFILE:
12225 return EMFILE;
12226 case FILEIO_EFBIG:
12227 return EFBIG;
12228 case FILEIO_ENOSPC:
12229 return ENOSPC;
12230 case FILEIO_ESPIPE:
12231 return ESPIPE;
12232 case FILEIO_EROFS:
12233 return EROFS;
12234 case FILEIO_ENOSYS:
12235 return ENOSYS;
12236 case FILEIO_ENAMETOOLONG:
12237 return ENAMETOOLONG;
12238 }
12239 return -1;
12240}
12241
12242static char *
12243remote_hostio_error (int errnum)
12244{
12245 int host_error = remote_fileio_errno_to_host (errnum);
12246
12247 if (host_error == -1)
12248 error (_("Unknown remote I/O error %d"), errnum);
12249 else
12250 error (_("Remote I/O error: %s"), safe_strerror (host_error));
12251}
12252
440b7aec
PA
12253/* A RAII wrapper around a remote file descriptor. */
12254
12255class scoped_remote_fd
a6b151f1 12256{
440b7aec
PA
12257public:
12258 explicit scoped_remote_fd (int fd)
12259 : m_fd (fd)
12260 {
12261 }
a6b151f1 12262
440b7aec
PA
12263 ~scoped_remote_fd ()
12264 {
12265 if (m_fd != -1)
12266 {
12267 try
12268 {
12269 int remote_errno;
12270 remote_hostio_close (find_target_at (process_stratum),
12271 m_fd, &remote_errno);
12272 }
12273 catch (...)
12274 {
12275 /* Swallow exception before it escapes the dtor. If
12276 something goes wrong, likely the connection is gone,
12277 and there's nothing else that can be done. */
12278 }
12279 }
12280 }
12281
12282 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd);
12283
12284 /* Release ownership of the file descriptor, and return it. */
12285 int release () noexcept
12286 {
12287 int fd = m_fd;
12288 m_fd = -1;
12289 return fd;
12290 }
12291
12292 /* Return the owned file descriptor. */
12293 int get () const noexcept
12294 {
12295 return m_fd;
12296 }
12297
12298private:
12299 /* The owned remote I/O file descriptor. */
12300 int m_fd;
12301};
a6b151f1
DJ
12302
12303void
12304remote_file_put (const char *local_file, const char *remote_file, int from_tty)
12305{
440b7aec
PA
12306 struct cleanup *back_to;
12307 int retcode, remote_errno, bytes, io_size;
a6b151f1
DJ
12308 gdb_byte *buffer;
12309 int bytes_in_buffer;
12310 int saw_eof;
12311 ULONGEST offset;
5d93a237 12312 struct remote_state *rs = get_remote_state ();
a6b151f1 12313
5d93a237 12314 if (!rs->remote_desc)
a6b151f1
DJ
12315 error (_("command can only be used with remote target"));
12316
d419f42d 12317 gdb_file_up file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
12318 if (file == NULL)
12319 perror_with_name (local_file);
a6b151f1 12320
440b7aec
PA
12321 scoped_remote_fd fd
12322 (remote_hostio_open (find_target_at (process_stratum), NULL,
12323 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
12324 | FILEIO_O_TRUNC),
12325 0700, 0, &remote_errno));
12326 if (fd.get () == -1)
a6b151f1
DJ
12327 remote_hostio_error (remote_errno);
12328
12329 /* Send up to this many bytes at once. They won't all fit in the
12330 remote packet limit, so we'll transfer slightly fewer. */
12331 io_size = get_remote_packet_size ();
224c3ddb 12332 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12333 back_to = make_cleanup (xfree, buffer);
a6b151f1 12334
a6b151f1
DJ
12335 bytes_in_buffer = 0;
12336 saw_eof = 0;
12337 offset = 0;
12338 while (bytes_in_buffer || !saw_eof)
12339 {
12340 if (!saw_eof)
12341 {
3e43a32a
MS
12342 bytes = fread (buffer + bytes_in_buffer, 1,
12343 io_size - bytes_in_buffer,
d419f42d 12344 file.get ());
a6b151f1
DJ
12345 if (bytes == 0)
12346 {
d419f42d 12347 if (ferror (file.get ()))
a6b151f1
DJ
12348 error (_("Error reading %s."), local_file);
12349 else
12350 {
12351 /* EOF. Unless there is something still in the
12352 buffer from the last iteration, we are done. */
12353 saw_eof = 1;
12354 if (bytes_in_buffer == 0)
12355 break;
12356 }
12357 }
12358 }
12359 else
12360 bytes = 0;
12361
12362 bytes += bytes_in_buffer;
12363 bytes_in_buffer = 0;
12364
0d866f62 12365 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
440b7aec 12366 fd.get (), buffer, bytes,
3e43a32a 12367 offset, &remote_errno);
a6b151f1
DJ
12368
12369 if (retcode < 0)
12370 remote_hostio_error (remote_errno);
12371 else if (retcode == 0)
12372 error (_("Remote write of %d bytes returned 0!"), bytes);
12373 else if (retcode < bytes)
12374 {
12375 /* Short write. Save the rest of the read data for the next
12376 write. */
12377 bytes_in_buffer = bytes - retcode;
12378 memmove (buffer, buffer + retcode, bytes_in_buffer);
12379 }
12380
12381 offset += retcode;
12382 }
12383
440b7aec
PA
12384 if (remote_hostio_close (find_target_at (process_stratum),
12385 fd.release (), &remote_errno))
a6b151f1
DJ
12386 remote_hostio_error (remote_errno);
12387
12388 if (from_tty)
12389 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
12390 do_cleanups (back_to);
12391}
12392
12393void
12394remote_file_get (const char *remote_file, const char *local_file, int from_tty)
12395{
440b7aec
PA
12396 struct cleanup *back_to;
12397 int remote_errno, bytes, io_size;
a6b151f1
DJ
12398 gdb_byte *buffer;
12399 ULONGEST offset;
5d93a237 12400 struct remote_state *rs = get_remote_state ();
a6b151f1 12401
5d93a237 12402 if (!rs->remote_desc)
a6b151f1
DJ
12403 error (_("command can only be used with remote target"));
12404
440b7aec
PA
12405 scoped_remote_fd fd
12406 (remote_hostio_open (find_target_at (process_stratum), NULL,
12407 remote_file, FILEIO_O_RDONLY, 0, 0,
12408 &remote_errno));
12409 if (fd.get () == -1)
a6b151f1
DJ
12410 remote_hostio_error (remote_errno);
12411
d419f42d 12412 gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
12413 if (file == NULL)
12414 perror_with_name (local_file);
a6b151f1
DJ
12415
12416 /* Send up to this many bytes at once. They won't all fit in the
12417 remote packet limit, so we'll transfer slightly fewer. */
12418 io_size = get_remote_packet_size ();
224c3ddb 12419 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12420 back_to = make_cleanup (xfree, buffer);
a6b151f1 12421
a6b151f1
DJ
12422 offset = 0;
12423 while (1)
12424 {
a3be983c 12425 bytes = remote_hostio_pread (find_target_at (process_stratum),
440b7aec
PA
12426 fd.get (), buffer, io_size, offset,
12427 &remote_errno);
a6b151f1
DJ
12428 if (bytes == 0)
12429 /* Success, but no bytes, means end-of-file. */
12430 break;
12431 if (bytes == -1)
12432 remote_hostio_error (remote_errno);
12433
12434 offset += bytes;
12435
d419f42d 12436 bytes = fwrite (buffer, 1, bytes, file.get ());
a6b151f1
DJ
12437 if (bytes == 0)
12438 perror_with_name (local_file);
12439 }
12440
440b7aec
PA
12441 if (remote_hostio_close (find_target_at (process_stratum),
12442 fd.release (), &remote_errno))
a6b151f1
DJ
12443 remote_hostio_error (remote_errno);
12444
12445 if (from_tty)
12446 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
12447 do_cleanups (back_to);
12448}
12449
12450void
12451remote_file_delete (const char *remote_file, int from_tty)
12452{
12453 int retcode, remote_errno;
5d93a237 12454 struct remote_state *rs = get_remote_state ();
a6b151f1 12455
5d93a237 12456 if (!rs->remote_desc)
a6b151f1
DJ
12457 error (_("command can only be used with remote target"));
12458
dbbca37d 12459 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 12460 NULL, remote_file, &remote_errno);
a6b151f1
DJ
12461 if (retcode == -1)
12462 remote_hostio_error (remote_errno);
12463
12464 if (from_tty)
12465 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
12466}
12467
12468static void
ac88e2de 12469remote_put_command (const char *args, int from_tty)
a6b151f1 12470{
d1a41061
PP
12471 if (args == NULL)
12472 error_no_arg (_("file to put"));
12473
773a1edc 12474 gdb_argv argv (args);
a6b151f1
DJ
12475 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12476 error (_("Invalid parameters to remote put"));
12477
12478 remote_file_put (argv[0], argv[1], from_tty);
a6b151f1
DJ
12479}
12480
12481static void
ac88e2de 12482remote_get_command (const char *args, int from_tty)
a6b151f1 12483{
d1a41061
PP
12484 if (args == NULL)
12485 error_no_arg (_("file to get"));
12486
773a1edc 12487 gdb_argv argv (args);
a6b151f1
DJ
12488 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12489 error (_("Invalid parameters to remote get"));
12490
12491 remote_file_get (argv[0], argv[1], from_tty);
a6b151f1
DJ
12492}
12493
12494static void
ac88e2de 12495remote_delete_command (const char *args, int from_tty)
a6b151f1 12496{
d1a41061
PP
12497 if (args == NULL)
12498 error_no_arg (_("file to delete"));
12499
773a1edc 12500 gdb_argv argv (args);
a6b151f1
DJ
12501 if (argv[0] == NULL || argv[1] != NULL)
12502 error (_("Invalid parameters to remote delete"));
12503
12504 remote_file_delete (argv[0], from_tty);
a6b151f1
DJ
12505}
12506
12507static void
981a3fb3 12508remote_command (const char *args, int from_tty)
a6b151f1 12509{
635c7e8a 12510 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
12511}
12512
57810aa7 12513bool
f6ac5f3d 12514remote_target::can_execute_reverse ()
b2175913 12515{
4082afcc
PA
12516 if (packet_support (PACKET_bs) == PACKET_ENABLE
12517 || packet_support (PACKET_bc) == PACKET_ENABLE)
57810aa7 12518 return true;
40ab02ce 12519 else
57810aa7 12520 return false;
b2175913
MS
12521}
12522
57810aa7 12523bool
f6ac5f3d 12524remote_target::supports_non_stop ()
74531fed 12525{
57810aa7 12526 return true;
74531fed
PA
12527}
12528
57810aa7 12529bool
f6ac5f3d 12530remote_target::supports_disable_randomization ()
03583c20
UW
12531{
12532 /* Only supported in extended mode. */
57810aa7 12533 return false;
03583c20
UW
12534}
12535
57810aa7 12536bool
f6ac5f3d 12537remote_target::supports_multi_process ()
8a305172
PA
12538{
12539 struct remote_state *rs = get_remote_state ();
a744cf53 12540
8020350c 12541 return remote_multi_process_p (rs);
8a305172
PA
12542}
12543
70221824 12544static int
f6ac5f3d 12545remote_supports_cond_tracepoints ()
782b2b07 12546{
4082afcc 12547 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
12548}
12549
57810aa7 12550bool
f6ac5f3d 12551remote_target::supports_evaluation_of_breakpoint_conditions ()
3788aec7 12552{
4082afcc 12553 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
12554}
12555
70221824 12556static int
f6ac5f3d 12557remote_supports_fast_tracepoints ()
7a697b8d 12558{
4082afcc 12559 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
12560}
12561
0fb4aa4b 12562static int
f6ac5f3d 12563remote_supports_static_tracepoints ()
0fb4aa4b 12564{
4082afcc 12565 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
12566}
12567
1e4d1764 12568static int
f6ac5f3d 12569remote_supports_install_in_trace ()
1e4d1764 12570{
4082afcc 12571 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
12572}
12573
57810aa7 12574bool
f6ac5f3d 12575remote_target::supports_enable_disable_tracepoint ()
d248b706 12576{
4082afcc
PA
12577 return (packet_support (PACKET_EnableDisableTracepoints_feature)
12578 == PACKET_ENABLE);
d248b706
KY
12579}
12580
57810aa7 12581bool
f6ac5f3d 12582remote_target::supports_string_tracing ()
3065dfb6 12583{
4082afcc 12584 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
12585}
12586
57810aa7 12587bool
f6ac5f3d 12588remote_target::can_run_breakpoint_commands ()
d3ce09f5 12589{
4082afcc 12590 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
12591}
12592
f6ac5f3d
PA
12593void
12594remote_target::trace_init ()
35b1e5cc 12595{
b6bb3468
PA
12596 struct remote_state *rs = get_remote_state ();
12597
35b1e5cc 12598 putpkt ("QTinit");
b6bb3468
PA
12599 remote_get_noisy_reply ();
12600 if (strcmp (rs->buf, "OK") != 0)
35b1e5cc
SS
12601 error (_("Target does not support this command."));
12602}
12603
409873ef
SS
12604/* Recursive routine to walk through command list including loops, and
12605 download packets for each command. */
12606
12607static void
12608remote_download_command_source (int num, ULONGEST addr,
12609 struct command_line *cmds)
12610{
12611 struct remote_state *rs = get_remote_state ();
12612 struct command_line *cmd;
12613
12614 for (cmd = cmds; cmd; cmd = cmd->next)
12615 {
0df8b418 12616 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12617 strcpy (rs->buf, "QTDPsrc:");
12618 encode_source_string (num, addr, "cmd", cmd->line,
12619 rs->buf + strlen (rs->buf),
12620 rs->buf_size - strlen (rs->buf));
12621 putpkt (rs->buf);
b6bb3468
PA
12622 remote_get_noisy_reply ();
12623 if (strcmp (rs->buf, "OK"))
409873ef
SS
12624 warning (_("Target does not support source download."));
12625
12626 if (cmd->control_type == while_control
12627 || cmd->control_type == while_stepping_control)
12628 {
12973681 12629 remote_download_command_source (num, addr, cmd->body_list_0.get ());
409873ef 12630
0df8b418 12631 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12632 strcpy (rs->buf, "QTDPsrc:");
12633 encode_source_string (num, addr, "cmd", "end",
12634 rs->buf + strlen (rs->buf),
12635 rs->buf_size - strlen (rs->buf));
12636 putpkt (rs->buf);
b6bb3468
PA
12637 remote_get_noisy_reply ();
12638 if (strcmp (rs->buf, "OK"))
409873ef
SS
12639 warning (_("Target does not support source download."));
12640 }
12641 }
12642}
12643
f6ac5f3d
PA
12644void
12645remote_target::download_tracepoint (struct bp_location *loc)
35b1e5cc 12646{
bba74b36 12647#define BUF_SIZE 2048
e8ba3115 12648
35b1e5cc 12649 CORE_ADDR tpaddr;
409873ef 12650 char addrbuf[40];
bba74b36 12651 char buf[BUF_SIZE];
b44ec619
SM
12652 std::vector<std::string> tdp_actions;
12653 std::vector<std::string> stepping_actions;
35b1e5cc 12654 char *pkt;
e8ba3115 12655 struct breakpoint *b = loc->owner;
d9b3f62e 12656 struct tracepoint *t = (struct tracepoint *) b;
b6bb3468 12657 struct remote_state *rs = get_remote_state ();
35b1e5cc 12658
dc673c81 12659 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
12660
12661 tpaddr = loc->address;
12662 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
12663 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
12664 addrbuf, /* address */
12665 (b->enable_state == bp_enabled ? 'E' : 'D'),
12666 t->step_count, t->pass_count);
e8ba3115
YQ
12667 /* Fast tracepoints are mostly handled by the target, but we can
12668 tell the target how big of an instruction block should be moved
12669 around. */
12670 if (b->type == bp_fast_tracepoint)
12671 {
12672 /* Only test for support at download time; we may not know
12673 target capabilities at definition time. */
12674 if (remote_supports_fast_tracepoints ())
35b1e5cc 12675 {
6b940e6a
PL
12676 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
12677 NULL))
bba74b36 12678 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 12679 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 12680 else
e8ba3115
YQ
12681 /* If it passed validation at definition but fails now,
12682 something is very wrong. */
12683 internal_error (__FILE__, __LINE__,
12684 _("Fast tracepoint not "
12685 "valid during download"));
35b1e5cc 12686 }
e8ba3115
YQ
12687 else
12688 /* Fast tracepoints are functionally identical to regular
12689 tracepoints, so don't take lack of support as a reason to
12690 give up on the trace run. */
12691 warning (_("Target does not support fast tracepoints, "
12692 "downloading %d as regular tracepoint"), b->number);
12693 }
12694 else if (b->type == bp_static_tracepoint)
12695 {
12696 /* Only test for support at download time; we may not know
12697 target capabilities at definition time. */
12698 if (remote_supports_static_tracepoints ())
0fb4aa4b 12699 {
e8ba3115 12700 struct static_tracepoint_marker marker;
0fb4aa4b 12701
e8ba3115
YQ
12702 if (target_static_tracepoint_marker_at (tpaddr, &marker))
12703 strcat (buf, ":S");
0fb4aa4b 12704 else
e8ba3115 12705 error (_("Static tracepoint not valid during download"));
0fb4aa4b 12706 }
e8ba3115
YQ
12707 else
12708 /* Fast tracepoints are functionally identical to regular
12709 tracepoints, so don't take lack of support as a reason
12710 to give up on the trace run. */
12711 error (_("Target does not support static tracepoints"));
12712 }
12713 /* If the tracepoint has a conditional, make it into an agent
12714 expression and append to the definition. */
12715 if (loc->cond)
12716 {
12717 /* Only test support at download time, we may not know target
12718 capabilities at definition time. */
12719 if (remote_supports_cond_tracepoints ())
35b1e5cc 12720 {
833177a4 12721 agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
bba74b36
YQ
12722 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
12723 aexpr->len);
e8ba3115 12724 pkt = buf + strlen (buf);
b44ec619 12725 for (int ndx = 0; ndx < aexpr->len; ++ndx)
e8ba3115
YQ
12726 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
12727 *pkt = '\0';
35b1e5cc 12728 }
e8ba3115
YQ
12729 else
12730 warning (_("Target does not support conditional tracepoints, "
12731 "ignoring tp %d cond"), b->number);
12732 }
35b1e5cc 12733
d9b3f62e 12734 if (b->commands || *default_collect)
e8ba3115
YQ
12735 strcat (buf, "-");
12736 putpkt (buf);
b6bb3468
PA
12737 remote_get_noisy_reply ();
12738 if (strcmp (rs->buf, "OK"))
e8ba3115 12739 error (_("Target does not support tracepoints."));
35b1e5cc 12740
e8ba3115 12741 /* do_single_steps (t); */
b44ec619
SM
12742 for (auto action_it = tdp_actions.begin ();
12743 action_it != tdp_actions.end (); action_it++)
e8ba3115 12744 {
b44ec619
SM
12745 QUIT; /* Allow user to bail out with ^C. */
12746
12747 bool has_more = (action_it != tdp_actions.end ()
12748 || !stepping_actions.empty ());
12749
12750 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
12751 b->number, addrbuf, /* address */
12752 action_it->c_str (),
12753 has_more ? '-' : 0);
12754 putpkt (buf);
12755 remote_get_noisy_reply ();
12756 if (strcmp (rs->buf, "OK"))
12757 error (_("Error on target while setting tracepoints."));
e8ba3115 12758 }
409873ef 12759
b44ec619
SM
12760 for (auto action_it = stepping_actions.begin ();
12761 action_it != stepping_actions.end (); action_it++)
12762 {
12763 QUIT; /* Allow user to bail out with ^C. */
12764
12765 bool is_first = action_it == stepping_actions.begin ();
12766 bool has_more = action_it != stepping_actions.end ();
12767
12768 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
12769 b->number, addrbuf, /* address */
12770 is_first ? "S" : "",
12771 action_it->c_str (),
12772 has_more ? "-" : "");
12773 putpkt (buf);
12774 remote_get_noisy_reply ();
12775 if (strcmp (rs->buf, "OK"))
12776 error (_("Error on target while setting tracepoints."));
12777 }
12778
4082afcc 12779 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 12780 {
f00aae0f 12781 if (b->location != NULL)
409873ef 12782 {
e8ba3115 12783 strcpy (buf, "QTDPsrc:");
f00aae0f 12784 encode_source_string (b->number, loc->address, "at",
d28cd78a 12785 event_location_to_string (b->location.get ()),
f00aae0f 12786 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115 12787 putpkt (buf);
b6bb3468
PA
12788 remote_get_noisy_reply ();
12789 if (strcmp (rs->buf, "OK"))
e8ba3115 12790 warning (_("Target does not support source download."));
409873ef 12791 }
e8ba3115
YQ
12792 if (b->cond_string)
12793 {
12794 strcpy (buf, "QTDPsrc:");
12795 encode_source_string (b->number, loc->address,
12796 "cond", b->cond_string, buf + strlen (buf),
12797 2048 - strlen (buf));
12798 putpkt (buf);
b6bb3468
PA
12799 remote_get_noisy_reply ();
12800 if (strcmp (rs->buf, "OK"))
e8ba3115
YQ
12801 warning (_("Target does not support source download."));
12802 }
12803 remote_download_command_source (b->number, loc->address,
12804 breakpoint_commands (b));
35b1e5cc 12805 }
35b1e5cc
SS
12806}
12807
57810aa7 12808bool
f6ac5f3d 12809remote_target::can_download_tracepoint ()
1e4d1764 12810{
1e51243a
PA
12811 struct remote_state *rs = get_remote_state ();
12812 struct trace_status *ts;
12813 int status;
12814
12815 /* Don't try to install tracepoints until we've relocated our
12816 symbols, and fetched and merged the target's tracepoint list with
12817 ours. */
12818 if (rs->starting_up)
57810aa7 12819 return false;
1e51243a
PA
12820
12821 ts = current_trace_status ();
f6ac5f3d 12822 status = get_trace_status (ts);
1e4d1764
YQ
12823
12824 if (status == -1 || !ts->running_known || !ts->running)
57810aa7 12825 return false;
1e4d1764
YQ
12826
12827 /* If we are in a tracing experiment, but remote stub doesn't support
12828 installing tracepoint in trace, we have to return. */
12829 if (!remote_supports_install_in_trace ())
57810aa7 12830 return false;
1e4d1764 12831
57810aa7 12832 return true;
1e4d1764
YQ
12833}
12834
12835
f6ac5f3d
PA
12836void
12837remote_target::download_trace_state_variable (const trace_state_variable &tsv)
35b1e5cc
SS
12838{
12839 struct remote_state *rs = get_remote_state ();
00bf0b85 12840 char *p;
35b1e5cc 12841
bba74b36 12842 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
c252925c
SM
12843 tsv.number, phex ((ULONGEST) tsv.initial_value, 8),
12844 tsv.builtin);
00bf0b85 12845 p = rs->buf + strlen (rs->buf);
c252925c 12846 if ((p - rs->buf) + tsv.name.length () * 2 >= get_remote_packet_size ())
00bf0b85 12847 error (_("Trace state variable name too long for tsv definition packet"));
c252925c 12848 p += 2 * bin2hex ((gdb_byte *) (tsv.name.data ()), p, tsv.name.length ());
00bf0b85 12849 *p++ = '\0';
35b1e5cc 12850 putpkt (rs->buf);
b6bb3468
PA
12851 remote_get_noisy_reply ();
12852 if (*rs->buf == '\0')
ad91cd99 12853 error (_("Target does not support this command."));
b6bb3468 12854 if (strcmp (rs->buf, "OK") != 0)
ad91cd99 12855 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
12856}
12857
f6ac5f3d
PA
12858void
12859remote_target::enable_tracepoint (struct bp_location *location)
d248b706
KY
12860{
12861 struct remote_state *rs = get_remote_state ();
12862 char addr_buf[40];
12863
12864 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12865 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
12866 location->owner->number, addr_buf);
d248b706 12867 putpkt (rs->buf);
b6bb3468 12868 remote_get_noisy_reply ();
d248b706
KY
12869 if (*rs->buf == '\0')
12870 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
12871 if (strcmp (rs->buf, "OK") != 0)
12872 error (_("Error on target while enabling tracepoint."));
12873}
12874
f6ac5f3d
PA
12875void
12876remote_target::disable_tracepoint (struct bp_location *location)
d248b706
KY
12877{
12878 struct remote_state *rs = get_remote_state ();
12879 char addr_buf[40];
12880
12881 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12882 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
12883 location->owner->number, addr_buf);
d248b706 12884 putpkt (rs->buf);
b6bb3468 12885 remote_get_noisy_reply ();
d248b706
KY
12886 if (*rs->buf == '\0')
12887 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
12888 if (strcmp (rs->buf, "OK") != 0)
12889 error (_("Error on target while disabling tracepoint."));
12890}
12891
f6ac5f3d
PA
12892void
12893remote_target::trace_set_readonly_regions ()
35b1e5cc
SS
12894{
12895 asection *s;
81b9b86e 12896 bfd *abfd = NULL;
35b1e5cc 12897 bfd_size_type size;
608bcef2 12898 bfd_vma vma;
35b1e5cc 12899 int anysecs = 0;
c2fa21f1 12900 int offset = 0;
35b1e5cc
SS
12901
12902 if (!exec_bfd)
12903 return; /* No information to give. */
12904
b6bb3468
PA
12905 struct remote_state *rs = get_remote_state ();
12906
12907 strcpy (rs->buf, "QTro");
12908 offset = strlen (rs->buf);
35b1e5cc
SS
12909 for (s = exec_bfd->sections; s; s = s->next)
12910 {
12911 char tmp1[40], tmp2[40];
c2fa21f1 12912 int sec_length;
35b1e5cc
SS
12913
12914 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 12915 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
12916 (s->flags & SEC_READONLY) == 0)
12917 continue;
12918
12919 anysecs = 1;
81b9b86e 12920 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 12921 size = bfd_get_section_size (s);
608bcef2
HZ
12922 sprintf_vma (tmp1, vma);
12923 sprintf_vma (tmp2, vma + size);
c2fa21f1 12924 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
b6bb3468 12925 if (offset + sec_length + 1 > rs->buf_size)
c2fa21f1 12926 {
4082afcc 12927 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 12928 warning (_("\
c2fa21f1
HZ
12929Too many sections for read-only sections definition packet."));
12930 break;
12931 }
b6bb3468 12932 xsnprintf (rs->buf + offset, rs->buf_size - offset, ":%s,%s",
bba74b36 12933 tmp1, tmp2);
c2fa21f1 12934 offset += sec_length;
35b1e5cc
SS
12935 }
12936 if (anysecs)
12937 {
b6bb3468
PA
12938 putpkt (rs->buf);
12939 getpkt (&rs->buf, &rs->buf_size, 0);
35b1e5cc
SS
12940 }
12941}
12942
f6ac5f3d
PA
12943void
12944remote_target::trace_start ()
35b1e5cc 12945{
b6bb3468
PA
12946 struct remote_state *rs = get_remote_state ();
12947
35b1e5cc 12948 putpkt ("QTStart");
b6bb3468
PA
12949 remote_get_noisy_reply ();
12950 if (*rs->buf == '\0')
ad91cd99 12951 error (_("Target does not support this command."));
b6bb3468
PA
12952 if (strcmp (rs->buf, "OK") != 0)
12953 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12954}
12955
f6ac5f3d
PA
12956int
12957remote_target::get_trace_status (struct trace_status *ts)
35b1e5cc 12958{
953b98d1 12959 /* Initialize it just to avoid a GCC false warning. */
f652de6f 12960 char *p = NULL;
0df8b418 12961 /* FIXME we need to get register block size some other way. */
00bf0b85 12962 extern int trace_regblock_size;
bd3eecc3 12963 enum packet_result result;
b6bb3468 12964 struct remote_state *rs = get_remote_state ();
bd3eecc3 12965
4082afcc 12966 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 12967 return -1;
a744cf53 12968
5cd63fda 12969 trace_regblock_size
9d6eea31 12970 = rs->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet;
00bf0b85 12971
049dc89b
JK
12972 putpkt ("qTStatus");
12973
492d29ea 12974 TRY
67f41397 12975 {
b6bb3468 12976 p = remote_get_noisy_reply ();
67f41397 12977 }
492d29ea 12978 CATCH (ex, RETURN_MASK_ERROR)
67f41397 12979 {
598d3636
JK
12980 if (ex.error != TARGET_CLOSE_ERROR)
12981 {
12982 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
12983 return -1;
12984 }
12985 throw_exception (ex);
67f41397 12986 }
492d29ea 12987 END_CATCH
00bf0b85 12988
bd3eecc3
PA
12989 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
12990
00bf0b85 12991 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 12992 if (result == PACKET_UNKNOWN)
00bf0b85 12993 return -1;
35b1e5cc 12994
00bf0b85 12995 /* We're working with a live target. */
f5911ea1 12996 ts->filename = NULL;
00bf0b85 12997
00bf0b85 12998 if (*p++ != 'T')
b6bb3468 12999 error (_("Bogus trace status reply from target: %s"), rs->buf);
35b1e5cc 13000
84cebc4a
YQ
13001 /* Function 'parse_trace_status' sets default value of each field of
13002 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
13003 parse_trace_status (p, ts);
13004
13005 return ts->running;
35b1e5cc
SS
13006}
13007
f6ac5f3d
PA
13008void
13009remote_target::get_tracepoint_status (struct breakpoint *bp,
13010 struct uploaded_tp *utp)
f196051f
SS
13011{
13012 struct remote_state *rs = get_remote_state ();
f196051f
SS
13013 char *reply;
13014 struct bp_location *loc;
13015 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 13016 size_t size = get_remote_packet_size ();
f196051f
SS
13017
13018 if (tp)
13019 {
c1fc2657 13020 tp->hit_count = 0;
f196051f 13021 tp->traceframe_usage = 0;
c1fc2657 13022 for (loc = tp->loc; loc; loc = loc->next)
f196051f
SS
13023 {
13024 /* If the tracepoint was never downloaded, don't go asking for
13025 any status. */
13026 if (tp->number_on_target == 0)
13027 continue;
bba74b36
YQ
13028 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
13029 phex_nz (loc->address, 0));
f196051f 13030 putpkt (rs->buf);
b6bb3468 13031 reply = remote_get_noisy_reply ();
f196051f
SS
13032 if (reply && *reply)
13033 {
13034 if (*reply == 'V')
13035 parse_tracepoint_status (reply + 1, bp, utp);
13036 }
13037 }
13038 }
13039 else if (utp)
13040 {
13041 utp->hit_count = 0;
13042 utp->traceframe_usage = 0;
bba74b36
YQ
13043 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
13044 phex_nz (utp->addr, 0));
f196051f 13045 putpkt (rs->buf);
b6bb3468 13046 reply = remote_get_noisy_reply ();
f196051f
SS
13047 if (reply && *reply)
13048 {
13049 if (*reply == 'V')
13050 parse_tracepoint_status (reply + 1, bp, utp);
13051 }
13052 }
13053}
13054
f6ac5f3d
PA
13055void
13056remote_target::trace_stop ()
35b1e5cc 13057{
b6bb3468
PA
13058 struct remote_state *rs = get_remote_state ();
13059
35b1e5cc 13060 putpkt ("QTStop");
b6bb3468
PA
13061 remote_get_noisy_reply ();
13062 if (*rs->buf == '\0')
ad91cd99 13063 error (_("Target does not support this command."));
b6bb3468
PA
13064 if (strcmp (rs->buf, "OK") != 0)
13065 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
13066}
13067
f6ac5f3d
PA
13068int
13069remote_target::trace_find (enum trace_find_type type, int num,
13070 CORE_ADDR addr1, CORE_ADDR addr2,
13071 int *tpp)
35b1e5cc
SS
13072{
13073 struct remote_state *rs = get_remote_state ();
bba74b36 13074 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
13075 char *p, *reply;
13076 int target_frameno = -1, target_tracept = -1;
13077
e6e4e701
PA
13078 /* Lookups other than by absolute frame number depend on the current
13079 trace selected, so make sure it is correct on the remote end
13080 first. */
13081 if (type != tfind_number)
13082 set_remote_traceframe ();
13083
35b1e5cc
SS
13084 p = rs->buf;
13085 strcpy (p, "QTFrame:");
13086 p = strchr (p, '\0');
13087 switch (type)
13088 {
13089 case tfind_number:
bba74b36 13090 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
13091 break;
13092 case tfind_pc:
bba74b36 13093 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
13094 break;
13095 case tfind_tp:
bba74b36 13096 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
13097 break;
13098 case tfind_range:
bba74b36
YQ
13099 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
13100 phex_nz (addr2, 0));
35b1e5cc
SS
13101 break;
13102 case tfind_outside:
bba74b36
YQ
13103 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
13104 phex_nz (addr2, 0));
35b1e5cc
SS
13105 break;
13106 default:
9b20d036 13107 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
13108 }
13109
13110 putpkt (rs->buf);
b6bb3468 13111 reply = remote_get_noisy_reply ();
ad91cd99
PA
13112 if (*reply == '\0')
13113 error (_("Target does not support this command."));
35b1e5cc
SS
13114
13115 while (reply && *reply)
13116 switch (*reply)
13117 {
13118 case 'F':
f197e0f1
VP
13119 p = ++reply;
13120 target_frameno = (int) strtol (p, &reply, 16);
13121 if (reply == p)
13122 error (_("Unable to parse trace frame number"));
e6e4e701
PA
13123 /* Don't update our remote traceframe number cache on failure
13124 to select a remote traceframe. */
f197e0f1
VP
13125 if (target_frameno == -1)
13126 return -1;
35b1e5cc
SS
13127 break;
13128 case 'T':
f197e0f1
VP
13129 p = ++reply;
13130 target_tracept = (int) strtol (p, &reply, 16);
13131 if (reply == p)
13132 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
13133 break;
13134 case 'O': /* "OK"? */
13135 if (reply[1] == 'K' && reply[2] == '\0')
13136 reply += 2;
13137 else
13138 error (_("Bogus reply from target: %s"), reply);
13139 break;
13140 default:
13141 error (_("Bogus reply from target: %s"), reply);
13142 }
13143 if (tpp)
13144 *tpp = target_tracept;
e6e4e701 13145
262e1174 13146 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
13147 return target_frameno;
13148}
13149
57810aa7 13150bool
f6ac5f3d 13151remote_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
35b1e5cc
SS
13152{
13153 struct remote_state *rs = get_remote_state ();
13154 char *reply;
13155 ULONGEST uval;
13156
e6e4e701
PA
13157 set_remote_traceframe ();
13158
bba74b36 13159 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc 13160 putpkt (rs->buf);
b6bb3468 13161 reply = remote_get_noisy_reply ();
35b1e5cc
SS
13162 if (reply && *reply)
13163 {
13164 if (*reply == 'V')
13165 {
13166 unpack_varlen_hex (reply + 1, &uval);
13167 *val = (LONGEST) uval;
57810aa7 13168 return true;
35b1e5cc
SS
13169 }
13170 }
57810aa7 13171 return false;
35b1e5cc
SS
13172}
13173
f6ac5f3d
PA
13174int
13175remote_target::save_trace_data (const char *filename)
00bf0b85
SS
13176{
13177 struct remote_state *rs = get_remote_state ();
13178 char *p, *reply;
13179
13180 p = rs->buf;
13181 strcpy (p, "QTSave:");
13182 p += strlen (p);
13183 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
13184 error (_("Remote file name too long for trace save packet"));
9f1b45b0 13185 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
13186 *p++ = '\0';
13187 putpkt (rs->buf);
b6bb3468 13188 reply = remote_get_noisy_reply ();
d6c5869f 13189 if (*reply == '\0')
ad91cd99
PA
13190 error (_("Target does not support this command."));
13191 if (strcmp (reply, "OK") != 0)
13192 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
13193 return 0;
13194}
13195
13196/* This is basically a memory transfer, but needs to be its own packet
13197 because we don't know how the target actually organizes its trace
13198 memory, plus we want to be able to ask for as much as possible, but
13199 not be unhappy if we don't get as much as we ask for. */
13200
f6ac5f3d
PA
13201LONGEST
13202remote_target::get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
13203{
13204 struct remote_state *rs = get_remote_state ();
13205 char *reply;
13206 char *p;
13207 int rslt;
13208
13209 p = rs->buf;
13210 strcpy (p, "qTBuffer:");
13211 p += strlen (p);
13212 p += hexnumstr (p, offset);
13213 *p++ = ',';
13214 p += hexnumstr (p, len);
13215 *p++ = '\0';
13216
13217 putpkt (rs->buf);
b6bb3468 13218 reply = remote_get_noisy_reply ();
00bf0b85
SS
13219 if (reply && *reply)
13220 {
13221 /* 'l' by itself means we're at the end of the buffer and
13222 there is nothing more to get. */
13223 if (*reply == 'l')
13224 return 0;
13225
13226 /* Convert the reply into binary. Limit the number of bytes to
13227 convert according to our passed-in buffer size, rather than
13228 what was returned in the packet; if the target is
13229 unexpectedly generous and gives us a bigger reply than we
13230 asked for, we don't want to crash. */
b6bb3468 13231 rslt = hex2bin (reply, buf, len);
00bf0b85
SS
13232 return rslt;
13233 }
13234
13235 /* Something went wrong, flag as an error. */
13236 return -1;
13237}
13238
f6ac5f3d
PA
13239void
13240remote_target::set_disconnected_tracing (int val)
35b1e5cc
SS
13241{
13242 struct remote_state *rs = get_remote_state ();
13243
4082afcc 13244 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 13245 {
ad91cd99
PA
13246 char *reply;
13247
bba74b36 13248 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 13249 putpkt (rs->buf);
b6bb3468 13250 reply = remote_get_noisy_reply ();
ad91cd99 13251 if (*reply == '\0')
33da3f1c 13252 error (_("Target does not support this command."));
ad91cd99
PA
13253 if (strcmp (reply, "OK") != 0)
13254 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
13255 }
13256 else if (val)
13257 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
13258}
13259
f6ac5f3d
PA
13260int
13261remote_target::core_of_thread (ptid_t ptid)
dc146f7c
VP
13262{
13263 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 13264
7aabaf9d
SM
13265 if (info != NULL && info->priv != NULL)
13266 return get_remote_thread_info (info)->core;
13267
dc146f7c
VP
13268 return -1;
13269}
13270
f6ac5f3d
PA
13271void
13272remote_target::set_circular_trace_buffer (int val)
4daf5ac0
SS
13273{
13274 struct remote_state *rs = get_remote_state ();
ad91cd99 13275 char *reply;
4daf5ac0 13276
bba74b36 13277 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 13278 putpkt (rs->buf);
b6bb3468 13279 reply = remote_get_noisy_reply ();
ad91cd99 13280 if (*reply == '\0')
4daf5ac0 13281 error (_("Target does not support this command."));
ad91cd99
PA
13282 if (strcmp (reply, "OK") != 0)
13283 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
13284}
13285
f6ac5f3d
PA
13286traceframe_info_up
13287remote_target::traceframe_info ()
b3b9301e 13288{
9018be22 13289 gdb::optional<gdb::char_vector> text
f6ac5f3d 13290 = target_read_stralloc (target_stack, TARGET_OBJECT_TRACEFRAME_INFO,
b7b030ad 13291 NULL);
9018be22
SM
13292 if (text)
13293 return parse_traceframe_info (text->data ());
b3b9301e
PA
13294
13295 return NULL;
13296}
13297
405f8e94
SS
13298/* Handle the qTMinFTPILen packet. Returns the minimum length of
13299 instruction on which a fast tracepoint may be placed. Returns -1
13300 if the packet is not supported, and 0 if the minimum instruction
13301 length is unknown. */
13302
f6ac5f3d
PA
13303int
13304remote_target::get_min_fast_tracepoint_insn_len ()
405f8e94
SS
13305{
13306 struct remote_state *rs = get_remote_state ();
13307 char *reply;
13308
e886a173
PA
13309 /* If we're not debugging a process yet, the IPA can't be
13310 loaded. */
13311 if (!target_has_execution)
13312 return 0;
13313
13314 /* Make sure the remote is pointing at the right process. */
13315 set_general_process ();
13316
bba74b36 13317 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94 13318 putpkt (rs->buf);
b6bb3468 13319 reply = remote_get_noisy_reply ();
405f8e94
SS
13320 if (*reply == '\0')
13321 return -1;
13322 else
13323 {
13324 ULONGEST min_insn_len;
13325
13326 unpack_varlen_hex (reply, &min_insn_len);
13327
13328 return (int) min_insn_len;
13329 }
13330}
13331
f6ac5f3d
PA
13332void
13333remote_target::set_trace_buffer_size (LONGEST val)
f6f899bf 13334{
4082afcc 13335 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
13336 {
13337 struct remote_state *rs = get_remote_state ();
13338 char *buf = rs->buf;
13339 char *endbuf = rs->buf + get_remote_packet_size ();
13340 enum packet_result result;
13341
13342 gdb_assert (val >= 0 || val == -1);
13343 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
13344 /* Send -1 as literal "-1" to avoid host size dependency. */
13345 if (val < 0)
13346 {
13347 *buf++ = '-';
13348 buf += hexnumstr (buf, (ULONGEST) -val);
13349 }
13350 else
13351 buf += hexnumstr (buf, (ULONGEST) val);
13352
13353 putpkt (rs->buf);
b6bb3468 13354 remote_get_noisy_reply ();
f6f899bf
HAQ
13355 result = packet_ok (rs->buf,
13356 &remote_protocol_packets[PACKET_QTBuffer_size]);
13357
13358 if (result != PACKET_OK)
13359 warning (_("Bogus reply from target: %s"), rs->buf);
13360 }
13361}
13362
57810aa7 13363bool
f6ac5f3d
PA
13364remote_target::set_trace_notes (const char *user, const char *notes,
13365 const char *stop_notes)
f196051f
SS
13366{
13367 struct remote_state *rs = get_remote_state ();
13368 char *reply;
13369 char *buf = rs->buf;
13370 char *endbuf = rs->buf + get_remote_packet_size ();
13371 int nbytes;
13372
13373 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
13374 if (user)
13375 {
13376 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 13377 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
13378 buf += 2 * nbytes;
13379 *buf++ = ';';
13380 }
13381 if (notes)
13382 {
13383 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 13384 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
13385 buf += 2 * nbytes;
13386 *buf++ = ';';
13387 }
13388 if (stop_notes)
13389 {
13390 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 13391 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
13392 buf += 2 * nbytes;
13393 *buf++ = ';';
13394 }
13395 /* Ensure the buffer is terminated. */
13396 *buf = '\0';
13397
13398 putpkt (rs->buf);
b6bb3468 13399 reply = remote_get_noisy_reply ();
f196051f 13400 if (*reply == '\0')
57810aa7 13401 return false;
f196051f
SS
13402
13403 if (strcmp (reply, "OK") != 0)
13404 error (_("Bogus reply from target: %s"), reply);
13405
57810aa7 13406 return true;
f196051f
SS
13407}
13408
57810aa7
PA
13409bool
13410remote_target::use_agent (bool use)
d1feda86 13411{
4082afcc 13412 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
13413 {
13414 struct remote_state *rs = get_remote_state ();
13415
13416 /* If the stub supports QAgent. */
bba74b36 13417 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
13418 putpkt (rs->buf);
13419 getpkt (&rs->buf, &rs->buf_size, 0);
13420
13421 if (strcmp (rs->buf, "OK") == 0)
13422 {
f6ac5f3d 13423 ::use_agent = use;
57810aa7 13424 return true;
d1feda86
YQ
13425 }
13426 }
13427
57810aa7 13428 return false;
d1feda86
YQ
13429}
13430
57810aa7 13431bool
f6ac5f3d 13432remote_target::can_use_agent ()
d1feda86 13433{
4082afcc 13434 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
13435}
13436
9accd112
MM
13437struct btrace_target_info
13438{
13439 /* The ptid of the traced thread. */
13440 ptid_t ptid;
f4abbc16
MM
13441
13442 /* The obtained branch trace configuration. */
13443 struct btrace_config conf;
9accd112
MM
13444};
13445
f4abbc16
MM
13446/* Reset our idea of our target's btrace configuration. */
13447
13448static void
13449remote_btrace_reset (void)
13450{
13451 struct remote_state *rs = get_remote_state ();
13452
13453 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
13454}
13455
f4abbc16
MM
13456/* Synchronize the configuration with the target. */
13457
13458static void
13459btrace_sync_conf (const struct btrace_config *conf)
13460{
d33501a5
MM
13461 struct packet_config *packet;
13462 struct remote_state *rs;
13463 char *buf, *pos, *endbuf;
13464
13465 rs = get_remote_state ();
13466 buf = rs->buf;
13467 endbuf = buf + get_remote_packet_size ();
13468
13469 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
13470 if (packet_config_support (packet) == PACKET_ENABLE
13471 && conf->bts.size != rs->btrace_config.bts.size)
13472 {
13473 pos = buf;
13474 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13475 conf->bts.size);
13476
13477 putpkt (buf);
13478 getpkt (&buf, &rs->buf_size, 0);
13479
13480 if (packet_ok (buf, packet) == PACKET_ERROR)
13481 {
13482 if (buf[0] == 'E' && buf[1] == '.')
13483 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
13484 else
13485 error (_("Failed to configure the BTS buffer size."));
13486 }
13487
13488 rs->btrace_config.bts.size = conf->bts.size;
13489 }
b20a6524
MM
13490
13491 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
13492 if (packet_config_support (packet) == PACKET_ENABLE
13493 && conf->pt.size != rs->btrace_config.pt.size)
13494 {
13495 pos = buf;
13496 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13497 conf->pt.size);
13498
13499 putpkt (buf);
13500 getpkt (&buf, &rs->buf_size, 0);
13501
13502 if (packet_ok (buf, packet) == PACKET_ERROR)
13503 {
13504 if (buf[0] == 'E' && buf[1] == '.')
13505 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
13506 else
13507 error (_("Failed to configure the trace buffer size."));
13508 }
13509
13510 rs->btrace_config.pt.size = conf->pt.size;
13511 }
f4abbc16
MM
13512}
13513
13514/* Read the current thread's btrace configuration from the target and
13515 store it into CONF. */
13516
13517static void
13518btrace_read_config (struct btrace_config *conf)
13519{
9018be22 13520 gdb::optional<gdb::char_vector> xml
f6ac5f3d 13521 = target_read_stralloc (target_stack, TARGET_OBJECT_BTRACE_CONF, "");
9018be22
SM
13522 if (xml)
13523 parse_xml_btrace_conf (conf, xml->data ());
f4abbc16
MM
13524}
13525
c0272db5
TW
13526/* Maybe reopen target btrace. */
13527
13528static void
13529remote_btrace_maybe_reopen (void)
13530{
13531 struct remote_state *rs = get_remote_state ();
c0272db5
TW
13532 struct thread_info *tp;
13533 int btrace_target_pushed = 0;
13534 int warned = 0;
13535
5ed8105e
PA
13536 scoped_restore_current_thread restore_thread;
13537
c0272db5
TW
13538 ALL_NON_EXITED_THREADS (tp)
13539 {
13540 set_general_thread (tp->ptid);
13541
13542 memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
13543 btrace_read_config (&rs->btrace_config);
13544
13545 if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
13546 continue;
13547
13548#if !defined (HAVE_LIBIPT)
13549 if (rs->btrace_config.format == BTRACE_FORMAT_PT)
13550 {
13551 if (!warned)
13552 {
13553 warned = 1;
c4e12631
MM
13554 warning (_("Target is recording using Intel Processor Trace "
13555 "but support was disabled at compile time."));
c0272db5
TW
13556 }
13557
13558 continue;
13559 }
13560#endif /* !defined (HAVE_LIBIPT) */
13561
13562 /* Push target, once, but before anything else happens. This way our
13563 changes to the threads will be cleaned up by unpushing the target
13564 in case btrace_read_config () throws. */
13565 if (!btrace_target_pushed)
13566 {
13567 btrace_target_pushed = 1;
13568 record_btrace_push_target ();
13569 printf_filtered (_("Target is recording using %s.\n"),
13570 btrace_format_string (rs->btrace_config.format));
13571 }
13572
13573 tp->btrace.target = XCNEW (struct btrace_target_info);
13574 tp->btrace.target->ptid = tp->ptid;
13575 tp->btrace.target->conf = rs->btrace_config;
13576 }
c0272db5
TW
13577}
13578
9accd112
MM
13579/* Enable branch tracing. */
13580
f6ac5f3d
PA
13581struct btrace_target_info *
13582remote_target::enable_btrace (ptid_t ptid, const struct btrace_config *conf)
9accd112
MM
13583{
13584 struct btrace_target_info *tinfo = NULL;
b20a6524 13585 struct packet_config *packet = NULL;
9accd112
MM
13586 struct remote_state *rs = get_remote_state ();
13587 char *buf = rs->buf;
13588 char *endbuf = rs->buf + get_remote_packet_size ();
13589
b20a6524
MM
13590 switch (conf->format)
13591 {
13592 case BTRACE_FORMAT_BTS:
13593 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
13594 break;
13595
13596 case BTRACE_FORMAT_PT:
13597 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
13598 break;
13599 }
13600
13601 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13602 error (_("Target does not support branch tracing."));
13603
f4abbc16
MM
13604 btrace_sync_conf (conf);
13605
9accd112
MM
13606 set_general_thread (ptid);
13607
13608 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13609 putpkt (rs->buf);
13610 getpkt (&rs->buf, &rs->buf_size, 0);
13611
13612 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13613 {
13614 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13615 error (_("Could not enable branch tracing for %s: %s"),
13616 target_pid_to_str (ptid), rs->buf + 2);
13617 else
13618 error (_("Could not enable branch tracing for %s."),
13619 target_pid_to_str (ptid));
13620 }
13621
8d749320 13622 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
13623 tinfo->ptid = ptid;
13624
f4abbc16
MM
13625 /* If we fail to read the configuration, we lose some information, but the
13626 tracing itself is not impacted. */
492d29ea
PA
13627 TRY
13628 {
13629 btrace_read_config (&tinfo->conf);
13630 }
13631 CATCH (err, RETURN_MASK_ERROR)
13632 {
13633 if (err.message != NULL)
13634 warning ("%s", err.message);
13635 }
13636 END_CATCH
f4abbc16 13637
9accd112
MM
13638 return tinfo;
13639}
13640
13641/* Disable branch tracing. */
13642
f6ac5f3d
PA
13643void
13644remote_target::disable_btrace (struct btrace_target_info *tinfo)
9accd112
MM
13645{
13646 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
13647 struct remote_state *rs = get_remote_state ();
13648 char *buf = rs->buf;
13649 char *endbuf = rs->buf + get_remote_packet_size ();
13650
4082afcc 13651 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13652 error (_("Target does not support branch tracing."));
13653
13654 set_general_thread (tinfo->ptid);
13655
13656 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13657 putpkt (rs->buf);
13658 getpkt (&rs->buf, &rs->buf_size, 0);
13659
13660 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13661 {
13662 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13663 error (_("Could not disable branch tracing for %s: %s"),
13664 target_pid_to_str (tinfo->ptid), rs->buf + 2);
13665 else
13666 error (_("Could not disable branch tracing for %s."),
13667 target_pid_to_str (tinfo->ptid));
13668 }
13669
13670 xfree (tinfo);
13671}
13672
13673/* Teardown branch tracing. */
13674
f6ac5f3d
PA
13675void
13676remote_target::teardown_btrace (struct btrace_target_info *tinfo)
9accd112
MM
13677{
13678 /* We must not talk to the target during teardown. */
13679 xfree (tinfo);
13680}
13681
13682/* Read the branch trace. */
13683
f6ac5f3d
PA
13684enum btrace_error
13685remote_target::read_btrace (struct btrace_data *btrace,
13686 struct btrace_target_info *tinfo,
13687 enum btrace_read_type type)
9accd112
MM
13688{
13689 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
9accd112 13690 const char *annex;
9accd112 13691
4082afcc 13692 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13693 error (_("Target does not support branch tracing."));
13694
13695#if !defined(HAVE_LIBEXPAT)
13696 error (_("Cannot process branch tracing result. XML parsing not supported."));
13697#endif
13698
13699 switch (type)
13700 {
864089d2 13701 case BTRACE_READ_ALL:
9accd112
MM
13702 annex = "all";
13703 break;
864089d2 13704 case BTRACE_READ_NEW:
9accd112
MM
13705 annex = "new";
13706 break;
969c39fb
MM
13707 case BTRACE_READ_DELTA:
13708 annex = "delta";
13709 break;
9accd112
MM
13710 default:
13711 internal_error (__FILE__, __LINE__,
13712 _("Bad branch tracing read type: %u."),
13713 (unsigned int) type);
13714 }
13715
9018be22 13716 gdb::optional<gdb::char_vector> xml
f6ac5f3d 13717 = target_read_stralloc (target_stack, TARGET_OBJECT_BTRACE, annex);
9018be22 13718 if (!xml)
969c39fb 13719 return BTRACE_ERR_UNKNOWN;
9accd112 13720
9018be22 13721 parse_xml_btrace (btrace, xml->data ());
9accd112 13722
969c39fb 13723 return BTRACE_ERR_NONE;
9accd112
MM
13724}
13725
f6ac5f3d
PA
13726const struct btrace_config *
13727remote_target::btrace_conf (const struct btrace_target_info *tinfo)
f4abbc16
MM
13728{
13729 return &tinfo->conf;
13730}
13731
57810aa7 13732bool
f6ac5f3d 13733remote_target::augmented_libraries_svr4_read ()
ced63ec0 13734{
4082afcc
PA
13735 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
13736 == PACKET_ENABLE);
ced63ec0
GB
13737}
13738
9dd130a0
TT
13739/* Implementation of to_load. */
13740
f6ac5f3d
PA
13741void
13742remote_target::load (const char *name, int from_tty)
9dd130a0
TT
13743{
13744 generic_load (name, from_tty);
13745}
13746
c78fa86a
GB
13747/* Accepts an integer PID; returns a string representing a file that
13748 can be opened on the remote side to get the symbols for the child
13749 process. Returns NULL if the operation is not supported. */
13750
f6ac5f3d
PA
13751char *
13752remote_target::pid_to_exec_file (int pid)
c78fa86a 13753{
9018be22 13754 static gdb::optional<gdb::char_vector> filename;
835205d0
GB
13755 struct inferior *inf;
13756 char *annex = NULL;
c78fa86a
GB
13757
13758 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
13759 return NULL;
13760
835205d0
GB
13761 inf = find_inferior_pid (pid);
13762 if (inf == NULL)
13763 internal_error (__FILE__, __LINE__,
13764 _("not currently attached to process %d"), pid);
13765
13766 if (!inf->fake_pid_p)
13767 {
13768 const int annex_size = 9;
13769
224c3ddb 13770 annex = (char *) alloca (annex_size);
835205d0
GB
13771 xsnprintf (annex, annex_size, "%x", pid);
13772 }
13773
f6ac5f3d 13774 filename = target_read_stralloc (target_stack,
c78fa86a
GB
13775 TARGET_OBJECT_EXEC_FILE, annex);
13776
9018be22 13777 return filename ? filename->data () : nullptr;
c78fa86a
GB
13778}
13779
750ce8d1
YQ
13780/* Implement the to_can_do_single_step target_ops method. */
13781
f6ac5f3d
PA
13782int
13783remote_target::can_do_single_step ()
750ce8d1
YQ
13784{
13785 /* We can only tell whether target supports single step or not by
13786 supported s and S vCont actions if the stub supports vContSupported
13787 feature. If the stub doesn't support vContSupported feature,
13788 we have conservatively to think target doesn't supports single
13789 step. */
13790 if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
13791 {
13792 struct remote_state *rs = get_remote_state ();
13793
13794 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
13795 remote_vcont_probe (rs);
13796
13797 return rs->supports_vCont.s && rs->supports_vCont.S;
13798 }
13799 else
13800 return 0;
13801}
13802
3a00c802
PA
13803/* Implementation of the to_execution_direction method for the remote
13804 target. */
13805
f6ac5f3d
PA
13806enum exec_direction_kind
13807remote_target::execution_direction ()
3a00c802
PA
13808{
13809 struct remote_state *rs = get_remote_state ();
13810
13811 return rs->last_resume_exec_dir;
13812}
13813
f6327dcb
KB
13814/* Return pointer to the thread_info struct which corresponds to
13815 THREAD_HANDLE (having length HANDLE_LEN). */
13816
f6ac5f3d
PA
13817thread_info *
13818remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
13819 int handle_len,
13820 inferior *inf)
f6327dcb
KB
13821{
13822 struct thread_info *tp;
13823
13824 ALL_NON_EXITED_THREADS (tp)
13825 {
7aabaf9d 13826 remote_thread_info *priv = get_remote_thread_info (tp);
f6327dcb
KB
13827
13828 if (tp->inf == inf && priv != NULL)
13829 {
7aabaf9d 13830 if (handle_len != priv->thread_handle.size ())
f6327dcb 13831 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
7aabaf9d
SM
13832 handle_len, priv->thread_handle.size ());
13833 if (memcmp (thread_handle, priv->thread_handle.data (),
f6327dcb
KB
13834 handle_len) == 0)
13835 return tp;
13836 }
13837 }
13838
13839 return NULL;
13840}
13841
57810aa7 13842bool
f6ac5f3d 13843remote_target::can_async_p ()
6426a772 13844{
5d93a237
TT
13845 struct remote_state *rs = get_remote_state ();
13846
3015c064
SM
13847 /* We don't go async if the user has explicitly prevented it with the
13848 "maint set target-async" command. */
c6ebd6cf 13849 if (!target_async_permitted)
57810aa7 13850 return false;
75c99385 13851
23860348 13852 /* We're async whenever the serial device is. */
5d93a237 13853 return serial_can_async_p (rs->remote_desc);
6426a772
JM
13854}
13855
57810aa7 13856bool
f6ac5f3d 13857remote_target::is_async_p ()
6426a772 13858{
5d93a237
TT
13859 struct remote_state *rs = get_remote_state ();
13860
c6ebd6cf 13861 if (!target_async_permitted)
75c99385 13862 /* We only enable async when the user specifically asks for it. */
57810aa7 13863 return false;
75c99385 13864
23860348 13865 /* We're async whenever the serial device is. */
5d93a237 13866 return serial_is_async_p (rs->remote_desc);
6426a772
JM
13867}
13868
2acceee2
JM
13869/* Pass the SERIAL event on and up to the client. One day this code
13870 will be able to delay notifying the client of an event until the
23860348 13871 point where an entire packet has been received. */
2acceee2 13872
2acceee2
JM
13873static serial_event_ftype remote_async_serial_handler;
13874
6426a772 13875static void
819cc324 13876remote_async_serial_handler (struct serial *scb, void *context)
6426a772 13877{
2acceee2
JM
13878 /* Don't propogate error information up to the client. Instead let
13879 the client find out about the error by querying the target. */
6a3753b3 13880 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
13881}
13882
74531fed
PA
13883static void
13884remote_async_inferior_event_handler (gdb_client_data data)
13885{
13886 inferior_event_handler (INF_REG_EVENT, NULL);
13887}
13888
f6ac5f3d
PA
13889void
13890remote_target::async (int enable)
2acceee2 13891{
5d93a237
TT
13892 struct remote_state *rs = get_remote_state ();
13893
6a3753b3 13894 if (enable)
2acceee2 13895 {
88b496c3 13896 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
13897
13898 /* If there are pending events in the stop reply queue tell the
13899 event loop to process them. */
13900 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
13901 mark_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13902 /* For simplicity, below we clear the pending events token
13903 without remembering whether it is marked, so here we always
13904 mark it. If there's actually no pending notification to
13905 process, this ends up being a no-op (other than a spurious
13906 event-loop wakeup). */
13907 if (target_is_non_stop_p ())
13908 mark_async_event_handler (rs->notif_state->get_pending_events_token);
2acceee2
JM
13909 }
13910 else
b7d2e916
PA
13911 {
13912 serial_async (rs->remote_desc, NULL, NULL);
6efcd9a8
PA
13913 /* If the core is disabling async, it doesn't want to be
13914 disturbed with target events. Clear all async event sources
13915 too. */
b7d2e916 13916 clear_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13917 if (target_is_non_stop_p ())
13918 clear_async_event_handler (rs->notif_state->get_pending_events_token);
b7d2e916 13919 }
6426a772
JM
13920}
13921
65706a29
PA
13922/* Implementation of the to_thread_events method. */
13923
f6ac5f3d
PA
13924void
13925remote_target::thread_events (int enable)
65706a29
PA
13926{
13927 struct remote_state *rs = get_remote_state ();
13928 size_t size = get_remote_packet_size ();
65706a29
PA
13929
13930 if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
13931 return;
13932
13933 xsnprintf (rs->buf, size, "QThreadEvents:%x", enable ? 1 : 0);
13934 putpkt (rs->buf);
13935 getpkt (&rs->buf, &rs->buf_size, 0);
13936
13937 switch (packet_ok (rs->buf,
13938 &remote_protocol_packets[PACKET_QThreadEvents]))
13939 {
13940 case PACKET_OK:
13941 if (strcmp (rs->buf, "OK") != 0)
13942 error (_("Remote refused setting thread events: %s"), rs->buf);
13943 break;
13944 case PACKET_ERROR:
13945 warning (_("Remote failure reply: %s"), rs->buf);
13946 break;
13947 case PACKET_UNKNOWN:
13948 break;
13949 }
13950}
13951
5a2468f5 13952static void
981a3fb3 13953set_remote_cmd (const char *args, int from_tty)
5a2468f5 13954{
635c7e8a 13955 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
13956}
13957
d471ea57 13958static void
981a3fb3 13959show_remote_cmd (const char *args, int from_tty)
d471ea57 13960{
37a105a1 13961 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 13962 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1 13963 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 13964 struct ui_out *uiout = current_uiout;
37a105a1 13965
2e783024 13966 ui_out_emit_tuple tuple_emitter (uiout, "showlist");
37a105a1
DJ
13967 for (; list != NULL; list = list->next)
13968 if (strcmp (list->name, "Z-packet") == 0)
13969 continue;
427c3a89
DJ
13970 else if (list->type == not_set_cmd)
13971 /* Alias commands are exactly like the original, except they
13972 don't have the normal type. */
13973 continue;
13974 else
37a105a1 13975 {
2e783024 13976 ui_out_emit_tuple option_emitter (uiout, "option");
a744cf53 13977
112e8700
SM
13978 uiout->field_string ("name", list->name);
13979 uiout->text (": ");
427c3a89 13980 if (list->type == show_cmd)
f5c4fcd9 13981 do_show_command (NULL, from_tty, list);
427c3a89
DJ
13982 else
13983 cmd_func (list, NULL, from_tty);
37a105a1 13984 }
d471ea57 13985}
5a2468f5 13986
0f71a2f6 13987
23860348 13988/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
13989static void
13990remote_new_objfile (struct objfile *objfile)
13991{
5d93a237
TT
13992 struct remote_state *rs = get_remote_state ();
13993
13994 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 13995 remote_check_symbols ();
dc8acb97
MS
13996}
13997
00bf0b85
SS
13998/* Pull all the tracepoints defined on the target and create local
13999 data structures representing them. We don't want to create real
14000 tracepoints yet, we don't want to mess up the user's existing
14001 collection. */
14002
f6ac5f3d
PA
14003int
14004remote_target::upload_tracepoints (struct uploaded_tp **utpp)
d5551862 14005{
00bf0b85
SS
14006 struct remote_state *rs = get_remote_state ();
14007 char *p;
d5551862 14008
00bf0b85
SS
14009 /* Ask for a first packet of tracepoint definition. */
14010 putpkt ("qTfP");
14011 getpkt (&rs->buf, &rs->buf_size, 0);
14012 p = rs->buf;
14013 while (*p && *p != 'l')
d5551862 14014 {
00bf0b85
SS
14015 parse_tracepoint_definition (p, utpp);
14016 /* Ask for another packet of tracepoint definition. */
14017 putpkt ("qTsP");
14018 getpkt (&rs->buf, &rs->buf_size, 0);
14019 p = rs->buf;
d5551862 14020 }
00bf0b85 14021 return 0;
d5551862
SS
14022}
14023
f6ac5f3d
PA
14024int
14025remote_target::upload_trace_state_variables (struct uploaded_tsv **utsvp)
d5551862 14026{
00bf0b85 14027 struct remote_state *rs = get_remote_state ();
d5551862 14028 char *p;
d5551862 14029
00bf0b85
SS
14030 /* Ask for a first packet of variable definition. */
14031 putpkt ("qTfV");
d5551862
SS
14032 getpkt (&rs->buf, &rs->buf_size, 0);
14033 p = rs->buf;
00bf0b85 14034 while (*p && *p != 'l')
d5551862 14035 {
00bf0b85
SS
14036 parse_tsv_definition (p, utsvp);
14037 /* Ask for another packet of variable definition. */
14038 putpkt ("qTsV");
d5551862
SS
14039 getpkt (&rs->buf, &rs->buf_size, 0);
14040 p = rs->buf;
14041 }
00bf0b85 14042 return 0;
d5551862
SS
14043}
14044
c1e36e3e
PA
14045/* The "set/show range-stepping" show hook. */
14046
14047static void
14048show_range_stepping (struct ui_file *file, int from_tty,
14049 struct cmd_list_element *c,
14050 const char *value)
14051{
14052 fprintf_filtered (file,
14053 _("Debugger's willingness to use range stepping "
14054 "is %s.\n"), value);
14055}
14056
14057/* The "set/show range-stepping" set hook. */
14058
14059static void
eb4c3f4a 14060set_range_stepping (const char *ignore_args, int from_tty,
c1e36e3e
PA
14061 struct cmd_list_element *c)
14062{
5d93a237
TT
14063 struct remote_state *rs = get_remote_state ();
14064
c1e36e3e
PA
14065 /* Whene enabling, check whether range stepping is actually
14066 supported by the target, and warn if not. */
14067 if (use_range_stepping)
14068 {
5d93a237 14069 if (rs->remote_desc != NULL)
c1e36e3e 14070 {
4082afcc 14071 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
14072 remote_vcont_probe (rs);
14073
4082afcc 14074 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
14075 && rs->supports_vCont.r)
14076 return;
14077 }
14078
14079 warning (_("Range stepping is not supported by the current target"));
14080 }
14081}
14082
c906108c 14083void
fba45db2 14084_initialize_remote (void)
c906108c 14085{
9a7071a8 14086 struct cmd_list_element *cmd;
6f937416 14087 const char *cmd_name;
ea9c271d 14088
0f71a2f6 14089 /* architecture specific data */
29709017
DJ
14090 remote_g_packet_data_handle =
14091 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 14092
94585166
DB
14093 remote_pspace_data
14094 = register_program_space_data_with_cleanup (NULL,
14095 remote_pspace_data_cleanup);
14096
ea9c271d
DJ
14097 /* Initialize the per-target state. At the moment there is only one
14098 of these, not one per target. Only one target is active at a
cf792862 14099 time. */
de44f5a7 14100 remote_state = new struct remote_state ();
ea9c271d 14101
d9f719f1
PA
14102 add_target (remote_target_info, remote_target::open);
14103 add_target (extended_remote_target_info, extended_remote_target::open);
cce74817 14104
dc8acb97 14105 /* Hook into new objfile notification. */
76727919 14106 gdb::observers::new_objfile.attach (remote_new_objfile);
dc8acb97 14107
c906108c
SS
14108#if 0
14109 init_remote_threadtests ();
14110#endif
14111
722247f1 14112 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 14113 /* set/show remote ... */
d471ea57 14114
1bedd215 14115 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
14116Remote protocol specific variables\n\
14117Configure various remote-protocol specific variables such as\n\
1bedd215 14118the packets being used"),
cff3e48b 14119 &remote_set_cmdlist, "set remote ",
23860348 14120 0 /* allow-unknown */, &setlist);
1bedd215 14121 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
14122Remote protocol specific variables\n\
14123Configure various remote-protocol specific variables such as\n\
1bedd215 14124the packets being used"),
cff3e48b 14125 &remote_show_cmdlist, "show remote ",
23860348 14126 0 /* allow-unknown */, &showlist);
5a2468f5 14127
1a966eab
AC
14128 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
14129Compare section data on target to the exec file.\n\
95cf3b38
DT
14130Argument is a single section name (default: all loaded sections).\n\
14131To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
14132 &cmdlist);
14133
1a966eab
AC
14134 add_cmd ("packet", class_maintenance, packet_command, _("\
14135Send an arbitrary packet to a remote target.\n\
c906108c
SS
14136 maintenance packet TEXT\n\
14137If GDB is talking to an inferior via the GDB serial protocol, then\n\
14138this command sends the string TEXT to the inferior, and displays the\n\
14139response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 14140terminating `#' character and checksum."),
c906108c
SS
14141 &maintenancelist);
14142
7915a72c
AC
14143 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
14144Set whether to send break if interrupted."), _("\
14145Show whether to send break if interrupted."), _("\
14146If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 14147 set_remotebreak, show_remotebreak,
e707bbc2 14148 &setlist, &showlist);
9a7071a8
JB
14149 cmd_name = "remotebreak";
14150 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
14151 deprecate_cmd (cmd, "set remote interrupt-sequence");
14152 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
14153 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
14154 deprecate_cmd (cmd, "show remote interrupt-sequence");
14155
14156 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
14157 interrupt_sequence_modes, &interrupt_sequence_mode,
14158 _("\
9a7071a8
JB
14159Set interrupt sequence to remote target."), _("\
14160Show interrupt sequence to remote target."), _("\
14161Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14162 NULL, show_interrupt_sequence,
14163 &remote_set_cmdlist,
14164 &remote_show_cmdlist);
14165
14166 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
14167 &interrupt_on_connect, _("\
14168Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14169Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14170If set, interrupt sequence is sent to remote target."),
14171 NULL, NULL,
14172 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 14173
23860348 14174 /* Install commands for configuring memory read/write packets. */
11cf8741 14175
1a966eab
AC
14176 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
14177Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 14178 &setlist);
1a966eab
AC
14179 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
14180Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
14181 &showlist);
14182 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
14183 set_memory_write_packet_size, _("\
14184Set the maximum number of bytes per memory-write packet.\n\
14185Specify the number of bytes in a packet or 0 (zero) for the\n\
14186default packet size. The actual limit is further reduced\n\
14187dependent on the target. Specify ``fixed'' to disable the\n\
14188further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14189 &remote_set_cmdlist);
14190 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
14191 set_memory_read_packet_size, _("\
14192Set the maximum number of bytes per memory-read packet.\n\
14193Specify the number of bytes in a packet or 0 (zero) for the\n\
14194default packet size. The actual limit is further reduced\n\
14195dependent on the target. Specify ``fixed'' to disable the\n\
14196further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14197 &remote_set_cmdlist);
14198 add_cmd ("memory-write-packet-size", no_class,
14199 show_memory_write_packet_size,
1a966eab 14200 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
14201 &remote_show_cmdlist);
14202 add_cmd ("memory-read-packet-size", no_class,
14203 show_memory_read_packet_size,
1a966eab 14204 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 14205 &remote_show_cmdlist);
c906108c 14206
b3f42336 14207 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
14208 &remote_hw_watchpoint_limit, _("\
14209Set the maximum number of target hardware watchpoints."), _("\
14210Show the maximum number of target hardware watchpoints."), _("\
14211Specify a negative limit for unlimited."),
3e43a32a
MS
14212 NULL, NULL, /* FIXME: i18n: The maximum
14213 number of target hardware
14214 watchpoints is %s. */
b3f42336 14215 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
14216 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
14217 &remote_hw_watchpoint_length_limit, _("\
14218Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
14219Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
14220Specify a negative limit for unlimited."),
14221 NULL, NULL, /* FIXME: i18n: The maximum
14222 length (in bytes) of a target
14223 hardware watchpoint is %s. */
14224 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 14225 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
14226 &remote_hw_breakpoint_limit, _("\
14227Set the maximum number of target hardware breakpoints."), _("\
14228Show the maximum number of target hardware breakpoints."), _("\
14229Specify a negative limit for unlimited."),
3e43a32a
MS
14230 NULL, NULL, /* FIXME: i18n: The maximum
14231 number of target hardware
14232 breakpoints is %s. */
b3f42336 14233 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 14234
1b493192
PA
14235 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
14236 &remote_address_size, _("\
4d28ad1e
AC
14237Set the maximum size of the address (in bits) in a memory packet."), _("\
14238Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
14239 NULL,
14240 NULL, /* FIXME: i18n: */
14241 &setlist, &showlist);
c906108c 14242
ca4f7f8b
PA
14243 init_all_packet_configs ();
14244
444abaca 14245 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 14246 "X", "binary-download", 1);
0f71a2f6 14247
444abaca 14248 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 14249 "vCont", "verbose-resume", 0);
506fb367 14250
89be2091
DJ
14251 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
14252 "QPassSignals", "pass-signals", 0);
14253
82075af2
JS
14254 add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
14255 "QCatchSyscalls", "catch-syscalls", 0);
14256
9b224c5e
PA
14257 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
14258 "QProgramSignals", "program-signals", 0);
14259
bc3b087d
SDJ
14260 add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
14261 "QSetWorkingDir", "set-working-dir", 0);
14262
aefd8b33
SDJ
14263 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
14264 "QStartupWithShell", "startup-with-shell", 0);
14265
0a2dde4a
SDJ
14266 add_packet_config_cmd (&remote_protocol_packets
14267 [PACKET_QEnvironmentHexEncoded],
14268 "QEnvironmentHexEncoded", "environment-hex-encoded",
14269 0);
14270
14271 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
14272 "QEnvironmentReset", "environment-reset",
14273 0);
14274
14275 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
14276 "QEnvironmentUnset", "environment-unset",
14277 0);
14278
444abaca 14279 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 14280 "qSymbol", "symbol-lookup", 0);
dc8acb97 14281
444abaca 14282 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 14283 "P", "set-register", 1);
d471ea57 14284
444abaca 14285 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 14286 "p", "fetch-register", 1);
b96ec7ac 14287
444abaca 14288 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 14289 "Z0", "software-breakpoint", 0);
d471ea57 14290
444abaca 14291 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 14292 "Z1", "hardware-breakpoint", 0);
d471ea57 14293
444abaca 14294 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 14295 "Z2", "write-watchpoint", 0);
d471ea57 14296
444abaca 14297 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 14298 "Z3", "read-watchpoint", 0);
d471ea57 14299
444abaca 14300 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 14301 "Z4", "access-watchpoint", 0);
d471ea57 14302
0876f84a
DJ
14303 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
14304 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 14305
c78fa86a
GB
14306 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
14307 "qXfer:exec-file:read", "pid-to-exec-file", 0);
14308
23181151
DJ
14309 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
14310 "qXfer:features:read", "target-features", 0);
14311
cfa9d6d9
DJ
14312 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
14313 "qXfer:libraries:read", "library-info", 0);
14314
2268b414
JK
14315 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
14316 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
14317
fd79ecee
DJ
14318 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
14319 "qXfer:memory-map:read", "memory-map", 0);
14320
0e7f50da
UW
14321 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
14322 "qXfer:spu:read", "read-spu-object", 0);
14323
14324 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
14325 "qXfer:spu:write", "write-spu-object", 0);
14326
07e059b5
VP
14327 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
14328 "qXfer:osdata:read", "osdata", 0);
14329
dc146f7c
VP
14330 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
14331 "qXfer:threads:read", "threads", 0);
14332
4aa995e1
PA
14333 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
14334 "qXfer:siginfo:read", "read-siginfo-object", 0);
14335
14336 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
14337 "qXfer:siginfo:write", "write-siginfo-object", 0);
14338
b3b9301e
PA
14339 add_packet_config_cmd
14340 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 14341 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 14342
169081d0
TG
14343 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
14344 "qXfer:uib:read", "unwind-info-block", 0);
14345
444abaca 14346 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 14347 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
14348 0);
14349
711e434b
PM
14350 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
14351 "qGetTIBAddr", "get-thread-information-block-address",
14352 0);
14353
40ab02ce
MS
14354 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
14355 "bc", "reverse-continue", 0);
14356
14357 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
14358 "bs", "reverse-step", 0);
14359
be2a5f71
DJ
14360 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
14361 "qSupported", "supported-packets", 0);
14362
08388c79
DE
14363 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
14364 "qSearch:memory", "search-memory", 0);
14365
bd3eecc3
PA
14366 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
14367 "qTStatus", "trace-status", 0);
14368
15a201c8
GB
14369 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
14370 "vFile:setfs", "hostio-setfs", 0);
14371
a6b151f1
DJ
14372 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
14373 "vFile:open", "hostio-open", 0);
14374
14375 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
14376 "vFile:pread", "hostio-pread", 0);
14377
14378 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
14379 "vFile:pwrite", "hostio-pwrite", 0);
14380
14381 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
14382 "vFile:close", "hostio-close", 0);
14383
14384 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
14385 "vFile:unlink", "hostio-unlink", 0);
14386
b9e7b9c3
UW
14387 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
14388 "vFile:readlink", "hostio-readlink", 0);
14389
0a93529c
GB
14390 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
14391 "vFile:fstat", "hostio-fstat", 0);
14392
2d717e4f
DJ
14393 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
14394 "vAttach", "attach", 0);
14395
14396 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
14397 "vRun", "run", 0);
14398
a6f3e723
SL
14399 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
14400 "QStartNoAckMode", "noack", 0);
14401
82f73884
PA
14402 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
14403 "vKill", "kill", 0);
14404
0b16c5cf
PA
14405 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
14406 "qAttached", "query-attached", 0);
14407
782b2b07 14408 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
14409 "ConditionalTracepoints",
14410 "conditional-tracepoints", 0);
3788aec7
LM
14411
14412 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
14413 "ConditionalBreakpoints",
14414 "conditional-breakpoints", 0);
14415
d3ce09f5
SS
14416 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
14417 "BreakpointCommands",
14418 "breakpoint-commands", 0);
14419
7a697b8d
SS
14420 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
14421 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 14422
409873ef
SS
14423 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
14424 "TracepointSource", "TracepointSource", 0);
14425
d914c394
SS
14426 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
14427 "QAllow", "allow", 0);
14428
0fb4aa4b
PA
14429 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
14430 "StaticTracepoints", "static-tracepoints", 0);
14431
1e4d1764
YQ
14432 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
14433 "InstallInTrace", "install-in-trace", 0);
14434
0fb4aa4b
PA
14435 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
14436 "qXfer:statictrace:read", "read-sdata-object", 0);
14437
78d85199
YQ
14438 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
14439 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
14440
03583c20
UW
14441 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
14442 "QDisableRandomization", "disable-randomization", 0);
14443
d1feda86
YQ
14444 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
14445 "QAgent", "agent", 0);
14446
f6f899bf
HAQ
14447 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
14448 "QTBuffer:size", "trace-buffer-size", 0);
14449
9accd112
MM
14450 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
14451 "Qbtrace:off", "disable-btrace", 0);
14452
14453 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
14454 "Qbtrace:bts", "enable-btrace-bts", 0);
14455
14456 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
14457 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
14458
14459 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
14460 "qXfer:btrace", "read-btrace", 0);
14461
f4abbc16
MM
14462 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
14463 "qXfer:btrace-conf", "read-btrace-conf", 0);
14464
d33501a5
MM
14465 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
14466 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
14467
73b8c1fd
PA
14468 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
14469 "multiprocess-feature", "multiprocess-feature", 0);
14470
f7e6eed5
PA
14471 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
14472 "swbreak-feature", "swbreak-feature", 0);
14473
14474 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
14475 "hwbreak-feature", "hwbreak-feature", 0);
14476
89245bc0
DB
14477 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
14478 "fork-event-feature", "fork-event-feature", 0);
14479
14480 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
14481 "vfork-event-feature", "vfork-event-feature", 0);
14482
b20a6524
MM
14483 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
14484 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
14485
750ce8d1
YQ
14486 add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
14487 "vContSupported", "verbose-resume-supported", 0);
14488
94585166
DB
14489 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
14490 "exec-event-feature", "exec-event-feature", 0);
14491
de979965
PA
14492 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
14493 "vCtrlC", "ctrl-c", 0);
14494
65706a29
PA
14495 add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
14496 "QThreadEvents", "thread-events", 0);
14497
f2faf941
PA
14498 add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
14499 "N stop reply", "no-resumed-stop-reply", 0);
14500
0b736949
DB
14501 /* Assert that we've registered "set remote foo-packet" commands
14502 for all packet configs. */
ca4f7f8b
PA
14503 {
14504 int i;
14505
14506 for (i = 0; i < PACKET_MAX; i++)
14507 {
14508 /* Ideally all configs would have a command associated. Some
14509 still don't though. */
14510 int excepted;
14511
14512 switch (i)
14513 {
14514 case PACKET_QNonStop:
ca4f7f8b
PA
14515 case PACKET_EnableDisableTracepoints_feature:
14516 case PACKET_tracenz_feature:
14517 case PACKET_DisconnectedTracing_feature:
14518 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
14519 case PACKET_qCRC:
14520 /* Additions to this list need to be well justified:
14521 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
14522 excepted = 1;
14523 break;
14524 default:
14525 excepted = 0;
14526 break;
14527 }
14528
14529 /* This catches both forgetting to add a config command, and
14530 forgetting to remove a packet from the exception list. */
14531 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
14532 }
14533 }
14534
37a105a1
DJ
14535 /* Keep the old ``set remote Z-packet ...'' working. Each individual
14536 Z sub-packet has its own set and show commands, but users may
14537 have sets to this variable in their .gdbinit files (or in their
14538 documentation). */
e9e68a56 14539 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
14540 &remote_Z_packet_detect, _("\
14541Set use of remote protocol `Z' packets"), _("\
14542Show use of remote protocol `Z' packets "), _("\
3b64bf98 14543When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 14544packets."),
e9e68a56 14545 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
14546 show_remote_protocol_Z_packet_cmd,
14547 /* FIXME: i18n: Use of remote protocol
14548 `Z' packets is %s. */
e9e68a56 14549 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 14550
a6b151f1
DJ
14551 add_prefix_cmd ("remote", class_files, remote_command, _("\
14552Manipulate files on the remote system\n\
14553Transfer files to and from the remote target system."),
14554 &remote_cmdlist, "remote ",
14555 0 /* allow-unknown */, &cmdlist);
14556
14557 add_cmd ("put", class_files, remote_put_command,
14558 _("Copy a local file to the remote system."),
14559 &remote_cmdlist);
14560
14561 add_cmd ("get", class_files, remote_get_command,
14562 _("Copy a remote file to the local system."),
14563 &remote_cmdlist);
14564
14565 add_cmd ("delete", class_files, remote_delete_command,
14566 _("Delete a remote file."),
14567 &remote_cmdlist);
14568
2d717e4f 14569 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 14570 &remote_exec_file_var, _("\
2d717e4f 14571Set the remote pathname for \"run\""), _("\
94585166
DB
14572Show the remote pathname for \"run\""), NULL,
14573 set_remote_exec_file,
14574 show_remote_exec_file,
14575 &remote_set_cmdlist,
14576 &remote_show_cmdlist);
2d717e4f 14577
c1e36e3e
PA
14578 add_setshow_boolean_cmd ("range-stepping", class_run,
14579 &use_range_stepping, _("\
14580Enable or disable range stepping."), _("\
14581Show whether target-assisted range stepping is enabled."), _("\
14582If on, and the target supports it, when stepping a source line, GDB\n\
14583tells the target to step the corresponding range of addresses itself instead\n\
14584of issuing multiple single-steps. This speeds up source level\n\
14585stepping. If off, GDB always issues single-steps, even if range\n\
14586stepping is supported by the target. The default is on."),
14587 set_range_stepping,
14588 show_range_stepping,
14589 &setlist,
14590 &showlist);
14591
449092f6
CV
14592 /* Eventually initialize fileio. See fileio.c */
14593 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 14594
ba348170 14595 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 14596 special ptids with it set to != 0. */
ba348170
PA
14597 magic_null_ptid = ptid_build (42000, -1, 1);
14598 not_sent_ptid = ptid_build (42000, -2, 1);
14599 any_thread_ptid = ptid_build (42000, 0, 1);
c906108c 14600}
This page took 4.753556 seconds and 4 git commands to generate.