Reimplement support for "maint print registers" with no running inferior yet
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
61baf725 3 Copyright (C) 1988-2017 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"
6867ae3e 39#include "observer.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"
35b1e5cc 78
94585166
DB
79/* Per-program-space data key. */
80static const struct program_space_data *remote_pspace_data;
81
82/* The variable registered as the control variable used by the
83 remote exec-file commands. While the remote exec-file setting is
84 per-program-space, the set/show machinery uses this as the
85 location of the remote exec-file value. */
86static char *remote_exec_file_var;
87
6765f3e5
DJ
88/* The size to align memory write packets, when practical. The protocol
89 does not guarantee any alignment, and gdb will generate short
90 writes and unaligned writes, but even as a best-effort attempt this
91 can improve bulk transfers. For instance, if a write is misaligned
92 relative to the target's data bus, the stub may need to make an extra
93 round trip fetching data from the target. This doesn't make a
94 huge difference, but it's easy to do, so we try to be helpful.
95
96 The alignment chosen is arbitrary; usually data bus width is
97 important here, not the possibly larger cache line size. */
98enum { REMOTE_ALIGN_WRITES = 16 };
99
23860348 100/* Prototypes for local functions. */
6d820c5c 101static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 102static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 103 int forever, int *is_notif);
6426a772 104
a14ed312 105static void remote_files_info (struct target_ops *ignore);
c906108c 106
f32dbf8c
MM
107static void remote_prepare_to_store (struct target_ops *self,
108 struct regcache *regcache);
c906108c 109
014f9477
TT
110static void remote_open_1 (const char *, int, struct target_ops *,
111 int extended_p);
c906108c 112
de90e03d 113static void remote_close (struct target_ops *self);
c906108c 114
cbb8991c
DB
115struct remote_state;
116
117static int remote_vkill (int pid, struct remote_state *rs);
118
8020350c
DB
119static void remote_kill_k (void);
120
136d6dae 121static void remote_mourn (struct target_ops *ops);
c906108c 122
a14ed312 123static void extended_remote_restart (void);
c906108c 124
6d820c5c 125static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 126
a14ed312 127static int readchar (int timeout);
c906108c 128
c33e31fd
PA
129static void remote_serial_write (const char *str, int len);
130
7d85a9c0 131static void remote_kill (struct target_ops *ops);
c906108c 132
6a109b6b 133static int remote_can_async_p (struct target_ops *);
75c99385 134
6a109b6b 135static int remote_is_async_p (struct target_ops *);
75c99385 136
6a3753b3 137static void remote_async (struct target_ops *ops, int enable);
75c99385 138
65706a29
PA
139static void remote_thread_events (struct target_ops *ops, int enable);
140
a14ed312 141static void interrupt_query (void);
c906108c 142
d62a8ae2
SM
143static void set_general_thread (ptid_t ptid);
144static void set_continue_thread (ptid_t ptid);
c906108c 145
a14ed312 146static void get_offsets (void);
c906108c 147
6d820c5c
DJ
148static void skip_frame (void);
149
150static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 151
a14ed312 152static int hexnumlen (ULONGEST num);
c906108c 153
a14ed312 154static void init_remote_ops (void);
c906108c 155
a14ed312 156static void init_extended_remote_ops (void);
c906108c 157
1eab8a48 158static void remote_stop (struct target_ops *self, ptid_t);
c906108c 159
a14ed312 160static int stubhex (int ch);
c906108c 161
a14ed312 162static int hexnumstr (char *, ULONGEST);
c906108c 163
a14ed312 164static int hexnumnstr (char *, ULONGEST, int);
2df3850c 165
a14ed312 166static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 167
baa336ce 168static void print_packet (const char *);
c906108c 169
a14ed312 170static int stub_unpack_int (char *buff, int fieldlength);
c906108c 171
39f77062 172static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 173
baa336ce 174static int putpkt_binary (const char *buf, int cnt);
c906108c 175
a14ed312 176static void check_binary_download (CORE_ADDR addr);
c906108c 177
5a2468f5 178struct packet_config;
5a2468f5 179
a14ed312 180static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 181
bb572ddd
DJ
182static void show_remote_protocol_packet_cmd (struct ui_file *file,
183 int from_tty,
184 struct cmd_list_element *c,
185 const char *value);
186
82f73884 187static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
256642e8 188static ptid_t read_ptid (const char *buf, const char **obuf);
82f73884 189
c378d69d 190static void remote_set_permissions (struct target_ops *self);
d914c394 191
8bd200f1
TT
192static int remote_get_trace_status (struct target_ops *self,
193 struct trace_status *ts);
d5551862 194
ab6617cc
TT
195static int remote_upload_tracepoints (struct target_ops *self,
196 struct uploaded_tp **utpp);
00bf0b85 197
181e3713
TT
198static int remote_upload_trace_state_variables (struct target_ops *self,
199 struct uploaded_tsv **utsvp);
00bf0b85 200
c8d104ad
PA
201static void remote_query_supported (void);
202
36d25514 203static void remote_check_symbols (void);
c8d104ad 204
74531fed 205struct stop_reply;
74531fed 206static void stop_reply_xfree (struct stop_reply *);
722247f1 207static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 208static void push_stop_reply (struct stop_reply *);
bcc75809 209static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
210static int peek_stop_reply (ptid_t ptid);
211
cbb8991c
DB
212struct threads_listing_context;
213static void remove_new_fork_children (struct threads_listing_context *);
214
74531fed 215static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 216
e3594fd1 217static void remote_terminal_ours (struct target_ops *self);
d3fd5342 218
d962ef82
DJ
219static int remote_read_description_p (struct target_ops *target);
220
176a6961 221static void remote_console_output (char *msg);
dde08ee1 222
efcc2da7 223static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 224
78eff0ec 225static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 226
f4abbc16
MM
227static void remote_btrace_reset (void);
228
c0272db5
TW
229static void remote_btrace_maybe_reopen (void);
230
221e1a37
PA
231static int stop_reply_queue_length (void);
232
80152258
PA
233static void readahead_cache_invalidate (void);
234
048094ac
PA
235static void remote_unpush_and_throw (void);
236
b6bb3468
PA
237static struct remote_state *get_remote_state (void);
238
a6b151f1
DJ
239/* For "remote". */
240
241static struct cmd_list_element *remote_cmdlist;
242
bb572ddd
DJ
243/* For "set remote" and "show remote". */
244
245static struct cmd_list_element *remote_set_cmdlist;
246static struct cmd_list_element *remote_show_cmdlist;
247
d458bd84
PA
248/* Stub vCont actions support.
249
250 Each field is a boolean flag indicating whether the stub reports
251 support for the corresponding action. */
252
253struct vCont_action_support
254{
255 /* vCont;t */
256 int t;
c1e36e3e
PA
257
258 /* vCont;r */
259 int r;
750ce8d1
YQ
260
261 /* vCont;s */
262 int s;
263
264 /* vCont;S */
265 int S;
d458bd84
PA
266};
267
c1e36e3e
PA
268/* Controls whether GDB is willing to use range stepping. */
269
270static int use_range_stepping = 1;
271
0d031856
TT
272#define OPAQUETHREADBYTES 8
273
274/* a 64 bit opaque identifier */
275typedef unsigned char threadref[OPAQUETHREADBYTES];
276
277/* About this many threadisds fit in a packet. */
278
279#define MAXTHREADLISTRESULTS 32
280
6f8976bf
YQ
281/* The max number of chars in debug output. The rest of chars are
282 omitted. */
283
284#define REMOTE_DEBUG_MAX_CHAR 512
285
80152258
PA
286/* Data for the vFile:pread readahead cache. */
287
288struct readahead_cache
289{
290 /* The file descriptor for the file that is being cached. -1 if the
291 cache is invalid. */
292 int fd;
293
294 /* The offset into the file that the cache buffer corresponds
295 to. */
296 ULONGEST offset;
297
298 /* The buffer holding the cache contents. */
299 gdb_byte *buf;
300 /* The buffer's size. We try to read as much as fits into a packet
301 at a time. */
302 size_t bufsize;
303
304 /* Cache hit and miss counters. */
305 ULONGEST hit_count;
306 ULONGEST miss_count;
307};
308
ea9c271d
DJ
309/* Description of the remote protocol state for the currently
310 connected target. This is per-target state, and independent of the
311 selected architecture. */
312
313struct remote_state
314{
315 /* A buffer to use for incoming packets, and its current size. The
316 buffer is grown dynamically for larger incoming packets.
317 Outgoing packets may also be constructed in this buffer.
318 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
319 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
320 packets. */
321 char *buf;
322 long buf_size;
be2a5f71 323
1e51243a
PA
324 /* True if we're going through initial connection setup (finding out
325 about the remote side's threads, relocating symbols, etc.). */
326 int starting_up;
327
be2a5f71
DJ
328 /* If we negotiated packet size explicitly (and thus can bypass
329 heuristics for the largest packet size that will not overflow
330 a buffer in the stub), this will be set to that packet size.
331 Otherwise zero, meaning to use the guessed size. */
332 long explicit_packet_size;
2d717e4f
DJ
333
334 /* remote_wait is normally called when the target is running and
335 waits for a stop reply packet. But sometimes we need to call it
336 when the target is already stopped. We can send a "?" packet
337 and have remote_wait read the response. Or, if we already have
338 the response, we can stash it in BUF and tell remote_wait to
339 skip calling getpkt. This flag is set when BUF contains a
340 stop reply packet and the target is not waiting. */
341 int cached_wait_status;
a6f3e723
SL
342
343 /* True, if in no ack mode. That is, neither GDB nor the stub will
344 expect acks from each other. The connection is assumed to be
345 reliable. */
346 int noack_mode;
82f73884
PA
347
348 /* True if we're connected in extended remote mode. */
349 int extended;
350
e24a49d8
PA
351 /* True if we resumed the target and we're waiting for the target to
352 stop. In the mean time, we can't start another command/query.
353 The remote server wouldn't be ready to process it, so we'd
354 timeout waiting for a reply that would never come and eventually
355 we'd close the connection. This can happen in asynchronous mode
356 because we allow GDB commands while the target is running. */
357 int waiting_for_stop_reply;
74531fed 358
d458bd84
PA
359 /* The status of the stub support for the various vCont actions. */
360 struct vCont_action_support supports_vCont;
782b2b07 361
3a29589a
DJ
362 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
363 responded to that. */
364 int ctrlc_pending_p;
5d93a237 365
048094ac
PA
366 /* True if we saw a Ctrl-C while reading or writing from/to the
367 remote descriptor. At that point it is not safe to send a remote
368 interrupt packet, so we instead remember we saw the Ctrl-C and
369 process it once we're done with sending/receiving the current
370 packet, which should be shortly. If however that takes too long,
371 and the user presses Ctrl-C again, we offer to disconnect. */
372 int got_ctrlc_during_io;
373
5d93a237
TT
374 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
375 remote_open knows that we don't have a file open when the program
376 starts. */
377 struct serial *remote_desc;
47f8a51d
TT
378
379 /* These are the threads which we last sent to the remote system. The
380 TID member will be -1 for all or -2 for not sent yet. */
381 ptid_t general_thread;
382 ptid_t continue_thread;
262e1174
TT
383
384 /* This is the traceframe which we last selected on the remote system.
385 It will be -1 if no traceframe is selected. */
386 int remote_traceframe_number;
747dc59d
TT
387
388 char *last_pass_packet;
5e4a05c4
TT
389
390 /* The last QProgramSignals packet sent to the target. We bypass
391 sending a new program signals list down to the target if the new
392 packet is exactly the same as the last we sent. IOW, we only let
393 the target know about program signals list changes. */
394 char *last_program_signals_packet;
b73be471
TT
395
396 enum gdb_signal last_sent_signal;
280ceea3
TT
397
398 int last_sent_step;
8e88304f 399
3a00c802
PA
400 /* The execution direction of the last resume we got. */
401 enum exec_direction_kind last_resume_exec_dir;
402
8e88304f
TT
403 char *finished_object;
404 char *finished_annex;
405 ULONGEST finished_offset;
b80fafe3
TT
406
407 /* Should we try the 'ThreadInfo' query packet?
408
409 This variable (NOT available to the user: auto-detect only!)
410 determines whether GDB will use the new, simpler "ThreadInfo"
411 query or the older, more complex syntax for thread queries.
412 This is an auto-detect variable (set to true at each connect,
413 and set to false when the target fails to recognize it). */
414 int use_threadinfo_query;
415 int use_threadextra_query;
88b496c3 416
0d031856
TT
417 threadref echo_nextthread;
418 threadref nextthread;
419 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
420
421 /* The state of remote notification. */
422 struct remote_notif_state *notif_state;
f4abbc16
MM
423
424 /* The branch trace configuration. */
425 struct btrace_config btrace_config;
15a201c8
GB
426
427 /* The argument to the last "vFile:setfs:" packet we sent, used
428 to avoid sending repeated unnecessary "vFile:setfs:" packets.
429 Initialized to -1 to indicate that no "vFile:setfs:" packet
430 has yet been sent. */
431 int fs_pid;
80152258
PA
432
433 /* A readahead cache for vFile:pread. Often, reading a binary
434 involves a sequence of small reads. E.g., when parsing an ELF
435 file. A readahead cache helps mostly the case of remote
436 debugging on a connection with higher latency, due to the
437 request/reply nature of the RSP. We only cache data for a single
438 file descriptor at a time. */
439 struct readahead_cache readahead_cache;
ea9c271d
DJ
440};
441
dc146f7c
VP
442/* Private data that we'll store in (struct thread_info)->private. */
443struct private_thread_info
444{
445 char *extra;
79efa585 446 char *name;
dc146f7c 447 int core;
799a2abe 448
f6327dcb
KB
449 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
450 sequence of bytes. */
451 gdb::byte_vector *thread_handle;
452
799a2abe
PA
453 /* Whether the target stopped for a breakpoint/watchpoint. */
454 enum target_stop_reason stop_reason;
455
456 /* This is set to the data address of the access causing the target
457 to stop for a watchpoint. */
458 CORE_ADDR watch_data_address;
85ad3aaf
PA
459
460 /* Fields used by the vCont action coalescing implemented in
461 remote_resume / remote_commit_resume. remote_resume stores each
462 thread's last resume request in these fields, so that a later
463 remote_commit_resume knows which is the proper action for this
464 thread to include in the vCont packet. */
465
466 /* True if the last target_resume call for this thread was a step
467 request, false if a continue request. */
468 int last_resume_step;
469
470 /* The signal specified in the last target_resume call for this
471 thread. */
472 enum gdb_signal last_resume_sig;
473
474 /* Whether this thread was already vCont-resumed on the remote
475 side. */
476 int vcont_resumed;
dc146f7c
VP
477};
478
479static void
480free_private_thread_info (struct private_thread_info *info)
481{
482 xfree (info->extra);
79efa585 483 xfree (info->name);
f6327dcb 484 delete info->thread_handle;
dc146f7c
VP
485 xfree (info);
486}
487
ea9c271d
DJ
488/* This data could be associated with a target, but we do not always
489 have access to the current target when we need it, so for now it is
490 static. This will be fine for as long as only one target is in use
491 at a time. */
cf792862 492static struct remote_state *remote_state;
ea9c271d
DJ
493
494static struct remote_state *
0b83947e 495get_remote_state_raw (void)
ea9c271d 496{
cf792862
TT
497 return remote_state;
498}
499
500/* Allocate a new struct remote_state with xmalloc, initialize it, and
501 return it. */
502
503static struct remote_state *
504new_remote_state (void)
505{
506 struct remote_state *result = XCNEW (struct remote_state);
507
508 /* The default buffer size is unimportant; it will be expanded
509 whenever a larger buffer is needed. */
510 result->buf_size = 400;
224c3ddb 511 result->buf = (char *) xmalloc (result->buf_size);
262e1174 512 result->remote_traceframe_number = -1;
b73be471 513 result->last_sent_signal = GDB_SIGNAL_0;
3a00c802 514 result->last_resume_exec_dir = EXEC_FORWARD;
15a201c8 515 result->fs_pid = -1;
cf792862
TT
516
517 return result;
ea9c271d
DJ
518}
519
520/* Description of the remote protocol for a given architecture. */
d01949b6 521
ad10f812
AC
522struct packet_reg
523{
524 long offset; /* Offset into G packet. */
525 long regnum; /* GDB's internal register number. */
526 LONGEST pnum; /* Remote protocol register number. */
b323314b 527 int in_g_packet; /* Always part of G packet. */
f5656ead 528 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 529 at present. */
f5656ead 530 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 531 at present. */
ad10f812
AC
532};
533
ea9c271d 534struct remote_arch_state
d01949b6 535{
ad10f812
AC
536 /* Description of the remote protocol registers. */
537 long sizeof_g_packet;
b323314b
AC
538
539 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 540 (making an array gdbarch_num_regs in size). */
b323314b 541 struct packet_reg *regs;
ad10f812 542
d01949b6
AC
543 /* This is the size (in chars) of the first response to the ``g''
544 packet. It is used as a heuristic when determining the maximum
545 size of memory-read and memory-write packets. A target will
546 typically only reserve a buffer large enough to hold the ``g''
547 packet. The size does not include packet overhead (headers and
23860348 548 trailers). */
d01949b6
AC
549 long actual_register_packet_size;
550
551 /* This is the maximum size (in chars) of a non read/write packet.
23860348 552 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
553 long remote_packet_size;
554};
555
35b1e5cc
SS
556/* Utility: generate error from an incoming stub packet. */
557static void
558trace_error (char *buf)
559{
560 if (*buf++ != 'E')
561 return; /* not an error msg */
562 switch (*buf)
563 {
564 case '1': /* malformed packet error */
565 if (*++buf == '0') /* general case: */
566 error (_("remote.c: error in outgoing packet."));
567 else
568 error (_("remote.c: error in outgoing packet at field #%ld."),
569 strtol (buf, NULL, 16));
35b1e5cc
SS
570 default:
571 error (_("Target returns error code '%s'."), buf);
572 }
573}
574
575/* Utility: wait for reply from stub, while accepting "O" packets. */
b6bb3468 576
35b1e5cc 577static char *
b6bb3468 578remote_get_noisy_reply ()
35b1e5cc 579{
b6bb3468
PA
580 struct remote_state *rs = get_remote_state ();
581
35b1e5cc
SS
582 do /* Loop on reply from remote stub. */
583 {
584 char *buf;
a744cf53 585
0df8b418 586 QUIT; /* Allow user to bail out with ^C. */
b6bb3468
PA
587 getpkt (&rs->buf, &rs->buf_size, 0);
588 buf = rs->buf;
ad91cd99 589 if (buf[0] == 'E')
35b1e5cc 590 trace_error (buf);
61012eef 591 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
592 {
593 ULONGEST ul;
594 CORE_ADDR from, to, org_to;
256642e8 595 const char *p, *pp;
dde08ee1 596 int adjusted_size = 0;
7556d4a4 597 int relocated = 0;
dde08ee1
PA
598
599 p = buf + strlen ("qRelocInsn:");
600 pp = unpack_varlen_hex (p, &ul);
601 if (*pp != ';')
cb91c06a 602 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
603 from = ul;
604
605 p = pp + 1;
a9cbf802 606 unpack_varlen_hex (p, &ul);
dde08ee1
PA
607 to = ul;
608
609 org_to = to;
610
492d29ea 611 TRY
dde08ee1 612 {
f5656ead 613 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 614 relocated = 1;
dde08ee1 615 }
492d29ea 616 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
617 {
618 if (ex.error == MEMORY_ERROR)
619 {
620 /* Propagate memory errors silently back to the
621 target. The stub may have limited the range of
622 addresses we can write to, for example. */
623 }
624 else
625 {
626 /* Something unexpectedly bad happened. Be verbose
627 so we can tell what, and propagate the error back
628 to the stub, so it doesn't get stuck waiting for
629 a response. */
630 exception_fprintf (gdb_stderr, ex,
631 _("warning: relocating instruction: "));
632 }
633 putpkt ("E01");
634 }
492d29ea 635 END_CATCH
7556d4a4
PA
636
637 if (relocated)
dde08ee1
PA
638 {
639 adjusted_size = to - org_to;
640
b6bb3468 641 xsnprintf (buf, rs->buf_size, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
642 putpkt (buf);
643 }
dde08ee1 644 }
ad91cd99 645 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
646 remote_console_output (buf + 1); /* 'O' message from stub */
647 else
0df8b418 648 return buf; /* Here's the actual reply. */
35b1e5cc
SS
649 }
650 while (1);
651}
3c3bea1c 652
d01949b6
AC
653/* Handle for retreving the remote protocol data from gdbarch. */
654static struct gdbarch_data *remote_gdbarch_data_handle;
655
ea9c271d
DJ
656static struct remote_arch_state *
657get_remote_arch_state (void)
d01949b6 658{
17d8546e 659 gdb_assert (target_gdbarch () != NULL);
19ba03f4
SM
660 return ((struct remote_arch_state *)
661 gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle));
d01949b6
AC
662}
663
0b83947e
DJ
664/* Fetch the global remote target state. */
665
666static struct remote_state *
667get_remote_state (void)
668{
669 /* Make sure that the remote architecture state has been
670 initialized, because doing so might reallocate rs->buf. Any
671 function which calls getpkt also needs to be mindful of changes
672 to rs->buf, but this call limits the number of places which run
673 into trouble. */
674 get_remote_arch_state ();
675
676 return get_remote_state_raw ();
677}
678
94585166
DB
679/* Cleanup routine for the remote module's pspace data. */
680
681static void
682remote_pspace_data_cleanup (struct program_space *pspace, void *arg)
683{
19ba03f4 684 char *remote_exec_file = (char *) arg;
94585166
DB
685
686 xfree (remote_exec_file);
687}
688
689/* Fetch the remote exec-file from the current program space. */
690
691static const char *
692get_remote_exec_file (void)
693{
694 char *remote_exec_file;
695
19ba03f4
SM
696 remote_exec_file
697 = (char *) program_space_data (current_program_space,
698 remote_pspace_data);
94585166
DB
699 if (remote_exec_file == NULL)
700 return "";
701
702 return remote_exec_file;
703}
704
705/* Set the remote exec file for PSPACE. */
706
707static void
708set_pspace_remote_exec_file (struct program_space *pspace,
709 char *remote_exec_file)
710{
19ba03f4 711 char *old_file = (char *) program_space_data (pspace, remote_pspace_data);
94585166
DB
712
713 xfree (old_file);
714 set_program_space_data (pspace, remote_pspace_data,
715 xstrdup (remote_exec_file));
716}
717
718/* The "set/show remote exec-file" set command hook. */
719
720static void
721set_remote_exec_file (char *ignored, int from_tty,
722 struct cmd_list_element *c)
723{
724 gdb_assert (remote_exec_file_var != NULL);
725 set_pspace_remote_exec_file (current_program_space, remote_exec_file_var);
726}
727
728/* The "set/show remote exec-file" show command hook. */
729
730static void
731show_remote_exec_file (struct ui_file *file, int from_tty,
732 struct cmd_list_element *cmd, const char *value)
733{
734 fprintf_filtered (file, "%s\n", remote_exec_file_var);
735}
736
74ca34ce
DJ
737static int
738compare_pnums (const void *lhs_, const void *rhs_)
739{
19ba03f4
SM
740 const struct packet_reg * const *lhs
741 = (const struct packet_reg * const *) lhs_;
742 const struct packet_reg * const *rhs
743 = (const struct packet_reg * const *) rhs_;
74ca34ce
DJ
744
745 if ((*lhs)->pnum < (*rhs)->pnum)
746 return -1;
747 else if ((*lhs)->pnum == (*rhs)->pnum)
748 return 0;
749 else
750 return 1;
751}
752
c21236dc
PA
753static int
754map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 755{
74ca34ce 756 int regnum, num_remote_regs, offset;
74ca34ce 757 struct packet_reg **remote_regs;
ea9c271d 758
4a22f64d 759 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 760 {
c21236dc 761 struct packet_reg *r = &regs[regnum];
baef701f 762
4a22f64d 763 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
764 /* Do not try to fetch zero-sized (placeholder) registers. */
765 r->pnum = -1;
766 else
767 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
768
b323314b 769 r->regnum = regnum;
74ca34ce
DJ
770 }
771
772 /* Define the g/G packet format as the contents of each register
773 with a remote protocol number, in order of ascending protocol
774 number. */
775
224c3ddb 776 remote_regs = XALLOCAVEC (struct packet_reg *, gdbarch_num_regs (gdbarch));
f57d151a 777 for (num_remote_regs = 0, regnum = 0;
4a22f64d 778 regnum < gdbarch_num_regs (gdbarch);
f57d151a 779 regnum++)
c21236dc
PA
780 if (regs[regnum].pnum != -1)
781 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 782
74ca34ce
DJ
783 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
784 compare_pnums);
785
786 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
787 {
788 remote_regs[regnum]->in_g_packet = 1;
789 remote_regs[regnum]->offset = offset;
4a22f64d 790 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
791 }
792
c21236dc
PA
793 return offset;
794}
795
796/* Given the architecture described by GDBARCH, return the remote
797 protocol register's number and the register's offset in the g/G
798 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
799 If the target does not have a mapping for REGNUM, return false,
800 otherwise, return true. */
801
802int
803remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
804 int *pnum, int *poffset)
805{
c21236dc
PA
806 struct packet_reg *regs;
807 struct cleanup *old_chain;
808
809 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
810
224c3ddb 811 regs = XCNEWVEC (struct packet_reg, gdbarch_num_regs (gdbarch));
c21236dc
PA
812 old_chain = make_cleanup (xfree, regs);
813
54887903 814 map_regcache_remote_table (gdbarch, regs);
c21236dc
PA
815
816 *pnum = regs[regnum].pnum;
817 *poffset = regs[regnum].offset;
818
819 do_cleanups (old_chain);
820
821 return *pnum != -1;
822}
823
824static void *
825init_remote_state (struct gdbarch *gdbarch)
826{
827 struct remote_state *rs = get_remote_state_raw ();
828 struct remote_arch_state *rsa;
829
830 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
831
832 /* Use the architecture to build a regnum<->pnum table, which will be
833 1:1 unless a feature set specifies otherwise. */
834 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
835 gdbarch_num_regs (gdbarch),
836 struct packet_reg);
837
74ca34ce
DJ
838 /* Record the maximum possible size of the g packet - it may turn out
839 to be smaller. */
c21236dc 840 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 841
0df8b418 842 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
843 remote stubs have a hardwired buffer size of 400 bytes
844 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
845 as the maximum packet-size to ensure that the packet and an extra
846 NUL character can always fit in the buffer. This stops GDB
847 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
848 already a full buffer (As of 1999-12-04 that was most stubs). */
849 rsa->remote_packet_size = 400 - 1;
d01949b6 850
ea9c271d
DJ
851 /* This one is filled in when a ``g'' packet is received. */
852 rsa->actual_register_packet_size = 0;
853
854 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
855 default, adjust the size accordingly. Remember that each byte is
856 encoded as two characters. 32 is the overhead for the packet
857 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 858 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 859 little. */
ea9c271d
DJ
860 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
861 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 862
ea9c271d
DJ
863 /* Make sure that the packet buffer is plenty big enough for
864 this architecture. */
865 if (rs->buf_size < rsa->remote_packet_size)
866 {
867 rs->buf_size = 2 * rsa->remote_packet_size;
224c3ddb 868 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
ea9c271d 869 }
6d820c5c 870
ea9c271d
DJ
871 return rsa;
872}
873
874/* Return the current allowed size of a remote packet. This is
875 inferred from the current architecture, and should be used to
876 limit the length of outgoing packets. */
877static long
878get_remote_packet_size (void)
879{
be2a5f71 880 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
881 struct remote_arch_state *rsa = get_remote_arch_state ();
882
be2a5f71
DJ
883 if (rs->explicit_packet_size)
884 return rs->explicit_packet_size;
885
ea9c271d 886 return rsa->remote_packet_size;
d01949b6
AC
887}
888
ad10f812 889static struct packet_reg *
ea9c271d 890packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 891{
f5656ead 892 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
893 return NULL;
894 else
ad10f812 895 {
ea9c271d 896 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 897
b323314b
AC
898 gdb_assert (r->regnum == regnum);
899 return r;
ad10f812 900 }
ad10f812
AC
901}
902
903static struct packet_reg *
ea9c271d 904packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 905{
b323314b 906 int i;
a744cf53 907
f5656ead 908 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 909 {
ea9c271d 910 struct packet_reg *r = &rsa->regs[i];
a744cf53 911
b323314b
AC
912 if (r->pnum == pnum)
913 return r;
ad10f812
AC
914 }
915 return NULL;
d01949b6
AC
916}
917
c906108c
SS
918static struct target_ops remote_ops;
919
920static struct target_ops extended_remote_ops;
921
6426a772
JM
922/* FIXME: cagney/1999-09-23: Even though getpkt was called with
923 ``forever'' still use the normal timeout mechanism. This is
924 currently used by the ASYNC code to guarentee that target reads
925 during the initial connect always time-out. Once getpkt has been
926 modified to return a timeout indication and, in turn
927 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 928 this can go away. */
6426a772
JM
929static int wait_forever_enabled_p = 1;
930
9a7071a8
JB
931/* Allow the user to specify what sequence to send to the remote
932 when he requests a program interruption: Although ^C is usually
933 what remote systems expect (this is the default, here), it is
934 sometimes preferable to send a break. On other systems such
935 as the Linux kernel, a break followed by g, which is Magic SysRq g
936 is required in order to interrupt the execution. */
937const char interrupt_sequence_control_c[] = "Ctrl-C";
938const char interrupt_sequence_break[] = "BREAK";
939const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 940static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
941 {
942 interrupt_sequence_control_c,
943 interrupt_sequence_break,
944 interrupt_sequence_break_g,
945 NULL
946 };
947static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
948
949static void
950show_interrupt_sequence (struct ui_file *file, int from_tty,
951 struct cmd_list_element *c,
952 const char *value)
953{
954 if (interrupt_sequence_mode == interrupt_sequence_control_c)
955 fprintf_filtered (file,
956 _("Send the ASCII ETX character (Ctrl-c) "
957 "to the remote target to interrupt the "
958 "execution of the program.\n"));
959 else if (interrupt_sequence_mode == interrupt_sequence_break)
960 fprintf_filtered (file,
961 _("send a break signal to the remote target "
962 "to interrupt the execution of the program.\n"));
963 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
964 fprintf_filtered (file,
965 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
966 "the remote target to interrupt the execution "
967 "of Linux kernel.\n"));
968 else
969 internal_error (__FILE__, __LINE__,
970 _("Invalid value for interrupt_sequence_mode: %s."),
971 interrupt_sequence_mode);
972}
6426a772 973
9a7071a8
JB
974/* This boolean variable specifies whether interrupt_sequence is sent
975 to the remote target when gdb connects to it.
976 This is mostly needed when you debug the Linux kernel: The Linux kernel
977 expects BREAK g which is Magic SysRq g for connecting gdb. */
978static int interrupt_on_connect = 0;
c906108c 979
9a7071a8
JB
980/* This variable is used to implement the "set/show remotebreak" commands.
981 Since these commands are now deprecated in favor of "set/show remote
982 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
983static int remote_break;
984
9a7071a8
JB
985static void
986set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
987{
988 if (remote_break)
989 interrupt_sequence_mode = interrupt_sequence_break;
990 else
991 interrupt_sequence_mode = interrupt_sequence_control_c;
992}
993
994static void
995show_remotebreak (struct ui_file *file, int from_tty,
996 struct cmd_list_element *c,
997 const char *value)
998{
999}
1000
c906108c
SS
1001/* This variable sets the number of bits in an address that are to be
1002 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 1003 leading zeros, the entire address would be sent. This variable
c906108c
SS
1004 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1005 initial implementation of remote.c restricted the address sent in
1006 memory packets to ``host::sizeof long'' bytes - (typically 32
1007 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1008 address was never sent. Since fixing this bug may cause a break in
1009 some remote targets this variable is principly provided to
23860348 1010 facilitate backward compatibility. */
c906108c 1011
883b9c6c 1012static unsigned int remote_address_size;
c906108c 1013
75c99385
PA
1014/* Temporary to track who currently owns the terminal. See
1015 remote_terminal_* for more details. */
6426a772
JM
1016
1017static int remote_async_terminal_ours_p;
1018
11cf8741 1019\f
11cf8741 1020/* User configurable variables for the number of characters in a
ea9c271d
DJ
1021 memory read/write packet. MIN (rsa->remote_packet_size,
1022 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 1023 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
1024 (speed up transfers). The variables ``preferred_*'' (the user
1025 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 1026 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
1027
1028struct memory_packet_config
1029{
a121b7c1 1030 const char *name;
11cf8741
JM
1031 long size;
1032 int fixed_p;
1033};
1034
a5c0808e
PA
1035/* The default max memory-write-packet-size. The 16k is historical.
1036 (It came from older GDB's using alloca for buffers and the
1037 knowledge (folklore?) that some hosts don't cope very well with
1038 large alloca calls.) */
1039#define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384
1040
1041/* The minimum remote packet size for memory transfers. Ensures we
1042 can write at least one byte. */
1043#define MIN_MEMORY_PACKET_SIZE 20
1044
11cf8741
JM
1045/* Compute the current size of a read/write packet. Since this makes
1046 use of ``actual_register_packet_size'' the computation is dynamic. */
1047
1048static long
1049get_memory_packet_size (struct memory_packet_config *config)
1050{
d01949b6 1051 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
1052 struct remote_arch_state *rsa = get_remote_arch_state ();
1053
11cf8741
JM
1054 long what_they_get;
1055 if (config->fixed_p)
1056 {
1057 if (config->size <= 0)
a5c0808e 1058 what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE;
11cf8741
JM
1059 else
1060 what_they_get = config->size;
1061 }
1062 else
1063 {
ea9c271d 1064 what_they_get = get_remote_packet_size ();
23860348 1065 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1066 if (config->size > 0
1067 && what_they_get > config->size)
1068 what_they_get = config->size;
be2a5f71
DJ
1069
1070 /* Limit it to the size of the targets ``g'' response unless we have
1071 permission from the stub to use a larger packet size. */
1072 if (rs->explicit_packet_size == 0
1073 && rsa->actual_register_packet_size > 0
1074 && what_they_get > rsa->actual_register_packet_size)
1075 what_they_get = rsa->actual_register_packet_size;
11cf8741 1076 }
a5c0808e
PA
1077 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1078 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1079
1080 /* Make sure there is room in the global buffer for this packet
1081 (including its trailing NUL byte). */
1082 if (rs->buf_size < what_they_get + 1)
1083 {
1084 rs->buf_size = 2 * what_they_get;
224c3ddb 1085 rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get);
6d820c5c
DJ
1086 }
1087
11cf8741
JM
1088 return what_they_get;
1089}
1090
0df8b418 1091/* Update the size of a read/write packet. If they user wants
23860348 1092 something really big then do a sanity check. */
11cf8741
JM
1093
1094static void
ac88e2de 1095set_memory_packet_size (const char *args, struct memory_packet_config *config)
11cf8741
JM
1096{
1097 int fixed_p = config->fixed_p;
1098 long size = config->size;
a744cf53 1099
11cf8741 1100 if (args == NULL)
8a3fe4f8 1101 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1102 else if (strcmp (args, "hard") == 0
1103 || strcmp (args, "fixed") == 0)
1104 fixed_p = 1;
1105 else if (strcmp (args, "soft") == 0
1106 || strcmp (args, "limit") == 0)
1107 fixed_p = 0;
1108 else
1109 {
1110 char *end;
a744cf53 1111
11cf8741
JM
1112 size = strtoul (args, &end, 0);
1113 if (args == end)
8a3fe4f8 1114 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1115
1116 /* Instead of explicitly capping the size of a packet to or
1117 disallowing it, the user is allowed to set the size to
1118 something arbitrarily large. */
11cf8741 1119 }
a5c0808e
PA
1120
1121 /* So that the query shows the correct value. */
1122 if (size <= 0)
1123 size = DEFAULT_MAX_MEMORY_PACKET_SIZE;
1124
23860348 1125 /* Extra checks? */
11cf8741
JM
1126 if (fixed_p && !config->fixed_p)
1127 {
e2e0b3e5
AC
1128 if (! query (_("The target may not be able to correctly handle a %s\n"
1129 "of %ld bytes. Change the packet size? "),
11cf8741 1130 config->name, size))
8a3fe4f8 1131 error (_("Packet size not changed."));
11cf8741 1132 }
23860348 1133 /* Update the config. */
11cf8741
JM
1134 config->fixed_p = fixed_p;
1135 config->size = size;
1136}
1137
1138static void
1139show_memory_packet_size (struct memory_packet_config *config)
1140{
a3f17187 1141 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1142 if (config->fixed_p)
a3f17187 1143 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1144 get_memory_packet_size (config));
1145 else
a3f17187 1146 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1147 get_memory_packet_size (config));
1148}
1149
1150static struct memory_packet_config memory_write_packet_config =
1151{
1152 "memory-write-packet-size",
1153};
1154
1155static void
ac88e2de 1156set_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1157{
1158 set_memory_packet_size (args, &memory_write_packet_config);
1159}
1160
1161static void
ac88e2de 1162show_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1163{
1164 show_memory_packet_size (&memory_write_packet_config);
1165}
1166
1167static long
1168get_memory_write_packet_size (void)
1169{
1170 return get_memory_packet_size (&memory_write_packet_config);
1171}
1172
1173static struct memory_packet_config memory_read_packet_config =
1174{
1175 "memory-read-packet-size",
1176};
1177
1178static void
ac88e2de 1179set_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1180{
1181 set_memory_packet_size (args, &memory_read_packet_config);
1182}
1183
1184static void
ac88e2de 1185show_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1186{
1187 show_memory_packet_size (&memory_read_packet_config);
1188}
1189
1190static long
1191get_memory_read_packet_size (void)
1192{
1193 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1194
11cf8741
JM
1195 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1196 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1197 increased beyond this. */
1198 if (size > get_remote_packet_size ())
1199 size = get_remote_packet_size ();
11cf8741
JM
1200 return size;
1201}
1202
11cf8741 1203\f
5a2468f5 1204/* Generic configuration support for packets the stub optionally
0df8b418 1205 supports. Allows the user to specify the use of the packet as well
23860348 1206 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1207
1208enum packet_support
1209 {
1210 PACKET_SUPPORT_UNKNOWN = 0,
1211 PACKET_ENABLE,
1212 PACKET_DISABLE
1213 };
1214
5a2468f5
JM
1215struct packet_config
1216 {
bb572ddd
DJ
1217 const char *name;
1218 const char *title;
4082afcc
PA
1219
1220 /* If auto, GDB auto-detects support for this packet or feature,
1221 either through qSupported, or by trying the packet and looking
1222 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1223 packet. If false, the packet is disabled. Configs that don't
1224 have an associated command always have this set to auto. */
7f19b9a2 1225 enum auto_boolean detect;
4082afcc
PA
1226
1227 /* Does the target support this packet? */
5a2468f5
JM
1228 enum packet_support support;
1229 };
1230
d471ea57 1231/* Analyze a packet's return value and update the packet config
23860348 1232 accordingly. */
d471ea57
AC
1233
1234enum packet_result
1235{
1236 PACKET_ERROR,
1237 PACKET_OK,
1238 PACKET_UNKNOWN
1239};
1240
4082afcc
PA
1241static enum packet_support packet_config_support (struct packet_config *config);
1242static enum packet_support packet_support (int packet);
5a2468f5
JM
1243
1244static void
fba45db2 1245show_packet_config_cmd (struct packet_config *config)
5a2468f5 1246{
a121b7c1 1247 const char *support = "internal-error";
a744cf53 1248
4082afcc 1249 switch (packet_config_support (config))
5a2468f5
JM
1250 {
1251 case PACKET_ENABLE:
1252 support = "enabled";
1253 break;
1254 case PACKET_DISABLE:
1255 support = "disabled";
1256 break;
1257 case PACKET_SUPPORT_UNKNOWN:
1258 support = "unknown";
1259 break;
1260 }
1261 switch (config->detect)
1262 {
7f19b9a2 1263 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1264 printf_filtered (_("Support for the `%s' packet "
1265 "is auto-detected, currently %s.\n"),
37a105a1 1266 config->name, support);
5a2468f5 1267 break;
7f19b9a2
AC
1268 case AUTO_BOOLEAN_TRUE:
1269 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1270 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1271 config->name, support);
8e248173 1272 break;
5a2468f5
JM
1273 }
1274}
1275
1276static void
bb572ddd
DJ
1277add_packet_config_cmd (struct packet_config *config, const char *name,
1278 const char *title, int legacy)
d471ea57 1279{
5a2468f5
JM
1280 char *set_doc;
1281 char *show_doc;
d471ea57 1282 char *cmd_name;
3ed07be4 1283
5a2468f5
JM
1284 config->name = name;
1285 config->title = title;
b435e160
AC
1286 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1287 name, title);
3e43a32a
MS
1288 show_doc = xstrprintf ("Show current use of remote "
1289 "protocol `%s' (%s) packet",
b435e160 1290 name, title);
d471ea57 1291 /* set/show TITLE-packet {auto,on,off} */
b435e160 1292 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1293 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1294 &config->detect, set_doc,
1295 show_doc, NULL, /* help_doc */
4082afcc 1296 NULL,
bb572ddd
DJ
1297 show_remote_protocol_packet_cmd,
1298 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1299 /* The command code copies the documentation strings. */
1300 xfree (set_doc);
1301 xfree (show_doc);
23860348 1302 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1303 if (legacy)
1304 {
1305 char *legacy_name;
a744cf53 1306
b435e160 1307 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1308 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1309 &remote_set_cmdlist);
d471ea57 1310 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1311 &remote_show_cmdlist);
d471ea57 1312 }
5a2468f5
JM
1313}
1314
d471ea57 1315static enum packet_result
a76d924d 1316packet_check_result (const char *buf)
5a2468f5 1317{
d471ea57 1318 if (buf[0] != '\0')
5a2468f5 1319 {
d471ea57 1320 /* The stub recognized the packet request. Check that the
23860348 1321 operation succeeded. */
a76d924d
DJ
1322 if (buf[0] == 'E'
1323 && isxdigit (buf[1]) && isxdigit (buf[2])
1324 && buf[3] == '\0')
1325 /* "Enn" - definitly an error. */
1326 return PACKET_ERROR;
1327
1328 /* Always treat "E." as an error. This will be used for
1329 more verbose error messages, such as E.memtypes. */
1330 if (buf[0] == 'E' && buf[1] == '.')
1331 return PACKET_ERROR;
1332
1333 /* The packet may or may not be OK. Just assume it is. */
1334 return PACKET_OK;
1335 }
1336 else
1337 /* The stub does not support the packet. */
1338 return PACKET_UNKNOWN;
1339}
1340
1341static enum packet_result
1342packet_ok (const char *buf, struct packet_config *config)
1343{
1344 enum packet_result result;
1345
4082afcc
PA
1346 if (config->detect != AUTO_BOOLEAN_TRUE
1347 && config->support == PACKET_DISABLE)
1348 internal_error (__FILE__, __LINE__,
1349 _("packet_ok: attempt to use a disabled packet"));
1350
a76d924d
DJ
1351 result = packet_check_result (buf);
1352 switch (result)
1353 {
1354 case PACKET_OK:
1355 case PACKET_ERROR:
1356 /* The stub recognized the packet request. */
4082afcc 1357 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1358 {
d471ea57
AC
1359 if (remote_debug)
1360 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1361 "Packet %s (%s) is supported\n",
1362 config->name, config->title);
d471ea57 1363 config->support = PACKET_ENABLE;
d471ea57 1364 }
a76d924d
DJ
1365 break;
1366 case PACKET_UNKNOWN:
23860348 1367 /* The stub does not support the packet. */
4082afcc
PA
1368 if (config->detect == AUTO_BOOLEAN_AUTO
1369 && config->support == PACKET_ENABLE)
d471ea57 1370 {
4082afcc
PA
1371 /* If the stub previously indicated that the packet was
1372 supported then there is a protocol error. */
1373 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1374 config->name, config->title);
1375 }
1376 else if (config->detect == AUTO_BOOLEAN_TRUE)
1377 {
1378 /* The user set it wrong. */
1379 error (_("Enabled packet %s (%s) not recognized by stub"),
1380 config->name, config->title);
d471ea57 1381 }
4082afcc
PA
1382
1383 if (remote_debug)
1384 fprintf_unfiltered (gdb_stdlog,
1385 "Packet %s (%s) is NOT supported\n",
1386 config->name, config->title);
1387 config->support = PACKET_DISABLE;
a76d924d 1388 break;
5a2468f5 1389 }
a76d924d
DJ
1390
1391 return result;
5a2468f5
JM
1392}
1393
444abaca
DJ
1394enum {
1395 PACKET_vCont = 0,
1396 PACKET_X,
1397 PACKET_qSymbol,
1398 PACKET_P,
1399 PACKET_p,
1400 PACKET_Z0,
1401 PACKET_Z1,
1402 PACKET_Z2,
1403 PACKET_Z3,
1404 PACKET_Z4,
15a201c8 1405 PACKET_vFile_setfs,
a6b151f1
DJ
1406 PACKET_vFile_open,
1407 PACKET_vFile_pread,
1408 PACKET_vFile_pwrite,
1409 PACKET_vFile_close,
1410 PACKET_vFile_unlink,
b9e7b9c3 1411 PACKET_vFile_readlink,
0a93529c 1412 PACKET_vFile_fstat,
0876f84a 1413 PACKET_qXfer_auxv,
23181151 1414 PACKET_qXfer_features,
c78fa86a 1415 PACKET_qXfer_exec_file,
cfa9d6d9 1416 PACKET_qXfer_libraries,
2268b414 1417 PACKET_qXfer_libraries_svr4,
fd79ecee 1418 PACKET_qXfer_memory_map,
0e7f50da
UW
1419 PACKET_qXfer_spu_read,
1420 PACKET_qXfer_spu_write,
07e059b5 1421 PACKET_qXfer_osdata,
dc146f7c 1422 PACKET_qXfer_threads,
0fb4aa4b 1423 PACKET_qXfer_statictrace_read,
b3b9301e 1424 PACKET_qXfer_traceframe_info,
169081d0 1425 PACKET_qXfer_uib,
711e434b 1426 PACKET_qGetTIBAddr,
444abaca 1427 PACKET_qGetTLSAddr,
be2a5f71 1428 PACKET_qSupported,
bd3eecc3 1429 PACKET_qTStatus,
89be2091 1430 PACKET_QPassSignals,
82075af2 1431 PACKET_QCatchSyscalls,
9b224c5e 1432 PACKET_QProgramSignals,
bc3b087d 1433 PACKET_QSetWorkingDir,
aefd8b33 1434 PACKET_QStartupWithShell,
0a2dde4a
SDJ
1435 PACKET_QEnvironmentHexEncoded,
1436 PACKET_QEnvironmentReset,
1437 PACKET_QEnvironmentUnset,
936d2992 1438 PACKET_qCRC,
08388c79 1439 PACKET_qSearch_memory,
2d717e4f
DJ
1440 PACKET_vAttach,
1441 PACKET_vRun,
a6f3e723 1442 PACKET_QStartNoAckMode,
82f73884 1443 PACKET_vKill,
4aa995e1
PA
1444 PACKET_qXfer_siginfo_read,
1445 PACKET_qXfer_siginfo_write,
0b16c5cf 1446 PACKET_qAttached,
4082afcc
PA
1447
1448 /* Support for conditional tracepoints. */
782b2b07 1449 PACKET_ConditionalTracepoints,
4082afcc
PA
1450
1451 /* Support for target-side breakpoint conditions. */
3788aec7 1452 PACKET_ConditionalBreakpoints,
4082afcc
PA
1453
1454 /* Support for target-side breakpoint commands. */
d3ce09f5 1455 PACKET_BreakpointCommands,
4082afcc
PA
1456
1457 /* Support for fast tracepoints. */
7a697b8d 1458 PACKET_FastTracepoints,
4082afcc
PA
1459
1460 /* Support for static tracepoints. */
0fb4aa4b 1461 PACKET_StaticTracepoints,
4082afcc
PA
1462
1463 /* Support for installing tracepoints while a trace experiment is
1464 running. */
1e4d1764 1465 PACKET_InstallInTrace,
4082afcc 1466
40ab02ce
MS
1467 PACKET_bc,
1468 PACKET_bs,
409873ef 1469 PACKET_TracepointSource,
d914c394 1470 PACKET_QAllow,
78d85199 1471 PACKET_qXfer_fdpic,
03583c20 1472 PACKET_QDisableRandomization,
d1feda86 1473 PACKET_QAgent,
f6f899bf 1474 PACKET_QTBuffer_size,
9accd112
MM
1475 PACKET_Qbtrace_off,
1476 PACKET_Qbtrace_bts,
b20a6524 1477 PACKET_Qbtrace_pt,
9accd112 1478 PACKET_qXfer_btrace,
4082afcc
PA
1479
1480 /* Support for the QNonStop packet. */
1481 PACKET_QNonStop,
1482
65706a29
PA
1483 /* Support for the QThreadEvents packet. */
1484 PACKET_QThreadEvents,
1485
4082afcc
PA
1486 /* Support for multi-process extensions. */
1487 PACKET_multiprocess_feature,
1488
1489 /* Support for enabling and disabling tracepoints while a trace
1490 experiment is running. */
1491 PACKET_EnableDisableTracepoints_feature,
1492
1493 /* Support for collecting strings using the tracenz bytecode. */
1494 PACKET_tracenz_feature,
1495
1496 /* Support for continuing to run a trace experiment while GDB is
1497 disconnected. */
1498 PACKET_DisconnectedTracing_feature,
1499
1500 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1501 PACKET_augmented_libraries_svr4_read_feature,
1502
f4abbc16
MM
1503 /* Support for the qXfer:btrace-conf:read packet. */
1504 PACKET_qXfer_btrace_conf,
1505
d33501a5
MM
1506 /* Support for the Qbtrace-conf:bts:size packet. */
1507 PACKET_Qbtrace_conf_bts_size,
1508
f7e6eed5
PA
1509 /* Support for swbreak+ feature. */
1510 PACKET_swbreak_feature,
1511
1512 /* Support for hwbreak+ feature. */
1513 PACKET_hwbreak_feature,
1514
89245bc0
DB
1515 /* Support for fork events. */
1516 PACKET_fork_event_feature,
1517
1518 /* Support for vfork events. */
1519 PACKET_vfork_event_feature,
1520
b20a6524
MM
1521 /* Support for the Qbtrace-conf:pt:size packet. */
1522 PACKET_Qbtrace_conf_pt_size,
1523
94585166
DB
1524 /* Support for exec events. */
1525 PACKET_exec_event_feature,
1526
750ce8d1
YQ
1527 /* Support for query supported vCont actions. */
1528 PACKET_vContSupported,
1529
de979965
PA
1530 /* Support remote CTRL-C. */
1531 PACKET_vCtrlC,
1532
f2faf941
PA
1533 /* Support TARGET_WAITKIND_NO_RESUMED. */
1534 PACKET_no_resumed,
1535
444abaca
DJ
1536 PACKET_MAX
1537};
506fb367 1538
444abaca 1539static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1540
f7e6eed5
PA
1541/* Returns the packet's corresponding "set remote foo-packet" command
1542 state. See struct packet_config for more details. */
1543
1544static enum auto_boolean
1545packet_set_cmd_state (int packet)
1546{
1547 return remote_protocol_packets[packet].detect;
1548}
1549
4082afcc
PA
1550/* Returns whether a given packet or feature is supported. This takes
1551 into account the state of the corresponding "set remote foo-packet"
1552 command, which may be used to bypass auto-detection. */
dc8acb97 1553
4082afcc
PA
1554static enum packet_support
1555packet_config_support (struct packet_config *config)
1556{
1557 switch (config->detect)
444abaca 1558 {
4082afcc
PA
1559 case AUTO_BOOLEAN_TRUE:
1560 return PACKET_ENABLE;
1561 case AUTO_BOOLEAN_FALSE:
1562 return PACKET_DISABLE;
1563 case AUTO_BOOLEAN_AUTO:
1564 return config->support;
1565 default:
1566 gdb_assert_not_reached (_("bad switch"));
444abaca 1567 }
4082afcc
PA
1568}
1569
1570/* Same as packet_config_support, but takes the packet's enum value as
1571 argument. */
1572
1573static enum packet_support
1574packet_support (int packet)
1575{
1576 struct packet_config *config = &remote_protocol_packets[packet];
1577
1578 return packet_config_support (config);
dc8acb97
MS
1579}
1580
5a2468f5 1581static void
444abaca
DJ
1582show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1583 struct cmd_list_element *c,
1584 const char *value)
5a2468f5 1585{
444abaca 1586 struct packet_config *packet;
5a2468f5 1587
444abaca
DJ
1588 for (packet = remote_protocol_packets;
1589 packet < &remote_protocol_packets[PACKET_MAX];
1590 packet++)
1591 {
1592 if (&packet->detect == c->var)
1593 {
1594 show_packet_config_cmd (packet);
1595 return;
1596 }
1597 }
9b20d036 1598 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1599 c->name);
5a2468f5
JM
1600}
1601
d471ea57
AC
1602/* Should we try one of the 'Z' requests? */
1603
1604enum Z_packet_type
1605{
1606 Z_PACKET_SOFTWARE_BP,
1607 Z_PACKET_HARDWARE_BP,
1608 Z_PACKET_WRITE_WP,
1609 Z_PACKET_READ_WP,
1610 Z_PACKET_ACCESS_WP,
1611 NR_Z_PACKET_TYPES
1612};
96baa820 1613
d471ea57 1614/* For compatibility with older distributions. Provide a ``set remote
23860348 1615 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1616
7f19b9a2 1617static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1618
1619static void
fba45db2
KB
1620set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1621 struct cmd_list_element *c)
96baa820 1622{
d471ea57 1623 int i;
a744cf53 1624
d471ea57 1625 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1626 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1627}
1628
1629static void
08546159
AC
1630show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1631 struct cmd_list_element *c,
1632 const char *value)
96baa820 1633{
d471ea57 1634 int i;
a744cf53 1635
d471ea57
AC
1636 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1637 {
444abaca 1638 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1639 }
96baa820
JM
1640}
1641
4082afcc
PA
1642/* Returns true if the multi-process extensions are in effect. */
1643
1644static int
1645remote_multi_process_p (struct remote_state *rs)
1646{
1647 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1648}
1649
de0d863e
DB
1650/* Returns true if fork events are supported. */
1651
1652static int
1653remote_fork_event_p (struct remote_state *rs)
1654{
1655 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1656}
1657
c269dbdb
DB
1658/* Returns true if vfork events are supported. */
1659
1660static int
1661remote_vfork_event_p (struct remote_state *rs)
1662{
1663 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1664}
1665
d46addbb
DB
1666/* Returns true if exec events are supported. */
1667
1668static int
1669remote_exec_event_p (struct remote_state *rs)
1670{
1671 return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
1672}
1673
cbb8991c
DB
1674/* Insert fork catchpoint target routine. If fork events are enabled
1675 then return success, nothing more to do. */
1676
1677static int
1678remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1679{
1680 struct remote_state *rs = get_remote_state ();
1681
1682 return !remote_fork_event_p (rs);
1683}
1684
1685/* Remove fork catchpoint target routine. Nothing to do, just
1686 return success. */
1687
1688static int
1689remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1690{
1691 return 0;
1692}
1693
1694/* Insert vfork catchpoint target routine. If vfork events are enabled
1695 then return success, nothing more to do. */
1696
1697static int
1698remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1699{
1700 struct remote_state *rs = get_remote_state ();
1701
1702 return !remote_vfork_event_p (rs);
1703}
1704
1705/* Remove vfork catchpoint target routine. Nothing to do, just
1706 return success. */
1707
1708static int
1709remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1710{
1711 return 0;
1712}
1713
d46addbb
DB
1714/* Insert exec catchpoint target routine. If exec events are
1715 enabled, just return success. */
1716
1717static int
1718remote_insert_exec_catchpoint (struct target_ops *ops, int pid)
1719{
1720 struct remote_state *rs = get_remote_state ();
1721
1722 return !remote_exec_event_p (rs);
1723}
1724
1725/* Remove exec catchpoint target routine. Nothing to do, just
1726 return success. */
1727
1728static int
1729remote_remove_exec_catchpoint (struct target_ops *ops, int pid)
1730{
1731 return 0;
1732}
1733
74531fed
PA
1734\f
1735/* Asynchronous signal handle registered as event loop source for
1736 when we have pending events ready to be passed to the core. */
1737
1738static struct async_event_handler *remote_async_inferior_event_token;
1739
c906108c
SS
1740\f
1741
79d7f229
PA
1742static ptid_t magic_null_ptid;
1743static ptid_t not_sent_ptid;
1744static ptid_t any_thread_ptid;
1745
0b16c5cf
PA
1746/* Find out if the stub attached to PID (and hence GDB should offer to
1747 detach instead of killing it when bailing out). */
1748
1749static int
1750remote_query_attached (int pid)
1751{
1752 struct remote_state *rs = get_remote_state ();
bba74b36 1753 size_t size = get_remote_packet_size ();
0b16c5cf 1754
4082afcc 1755 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1756 return 0;
1757
1758 if (remote_multi_process_p (rs))
bba74b36 1759 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1760 else
bba74b36 1761 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1762
1763 putpkt (rs->buf);
1764 getpkt (&rs->buf, &rs->buf_size, 0);
1765
1766 switch (packet_ok (rs->buf,
1554e9be 1767 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1768 {
1769 case PACKET_OK:
1770 if (strcmp (rs->buf, "1") == 0)
1771 return 1;
1772 break;
1773 case PACKET_ERROR:
1774 warning (_("Remote failure reply: %s"), rs->buf);
1775 break;
1776 case PACKET_UNKNOWN:
1777 break;
1778 }
1779
1780 return 0;
1781}
1782
49c62f2e
PA
1783/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1784 has been invented by GDB, instead of reported by the target. Since
1785 we can be connected to a remote system before before knowing about
1786 any inferior, mark the target with execution when we find the first
1787 inferior. If ATTACHED is 1, then we had just attached to this
1788 inferior. If it is 0, then we just created this inferior. If it
1789 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1790 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1791 attempt to open this inferior's executable as the main executable
1792 if no main executable is open already. */
1941c569
PA
1793
1794static struct inferior *
1b6e6f5c
GB
1795remote_add_inferior (int fake_pid_p, int pid, int attached,
1796 int try_open_exec)
1941c569 1797{
1941c569
PA
1798 struct inferior *inf;
1799
0b16c5cf
PA
1800 /* Check whether this process we're learning about is to be
1801 considered attached, or if is to be considered to have been
1802 spawned by the stub. */
1803 if (attached == -1)
1804 attached = remote_query_attached (pid);
1805
f5656ead 1806 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1807 {
1808 /* If the target shares code across all inferiors, then every
1809 attach adds a new inferior. */
1810 inf = add_inferior (pid);
1811
1812 /* ... and every inferior is bound to the same program space.
1813 However, each inferior may still have its own address
1814 space. */
1815 inf->aspace = maybe_new_address_space ();
1816 inf->pspace = current_program_space;
1817 }
1818 else
1819 {
1820 /* In the traditional debugging scenario, there's a 1-1 match
1821 between program/address spaces. We simply bind the inferior
1822 to the program space's address space. */
1823 inf = current_inferior ();
1824 inferior_appeared (inf, pid);
1825 }
1941c569 1826
0b16c5cf 1827 inf->attach_flag = attached;
49c62f2e 1828 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1829
1b6e6f5c
GB
1830 /* If no main executable is currently open then attempt to
1831 open the file that was executed to create this inferior. */
835205d0 1832 if (try_open_exec && get_exec_file (0) == NULL)
bb805577 1833 exec_file_locate_attach (pid, 0, 1);
1b6e6f5c 1834
1941c569
PA
1835 return inf;
1836}
1837
85ad3aaf
PA
1838static struct private_thread_info *
1839 get_private_info_thread (struct thread_info *info);
1840
1941c569
PA
1841/* Add thread PTID to GDB's thread list. Tag it as executing/running
1842 according to RUNNING. */
1843
c906108c 1844static void
0d5b594f 1845remote_add_thread (ptid_t ptid, int running, int executing)
c906108c 1846{
b7ea362b 1847 struct remote_state *rs = get_remote_state ();
85ad3aaf 1848 struct thread_info *thread;
b7ea362b
PA
1849
1850 /* GDB historically didn't pull threads in the initial connection
1851 setup. If the remote target doesn't even have a concept of
1852 threads (e.g., a bare-metal target), even if internally we
1853 consider that a single-threaded target, mentioning a new thread
1854 might be confusing to the user. Be silent then, preserving the
1855 age old behavior. */
1856 if (rs->starting_up)
85ad3aaf 1857 thread = add_thread_silent (ptid);
b7ea362b 1858 else
85ad3aaf 1859 thread = add_thread (ptid);
1941c569 1860
85ad3aaf 1861 get_private_info_thread (thread)->vcont_resumed = executing;
0d5b594f 1862 set_executing (ptid, executing);
1941c569
PA
1863 set_running (ptid, running);
1864}
1865
1866/* Come here when we learn about a thread id from the remote target.
1867 It may be the first time we hear about such thread, so take the
1868 opportunity to add it to GDB's thread list. In case this is the
1869 first time we're noticing its corresponding inferior, add it to
0d5b594f
PA
1870 GDB's inferior list as well. EXECUTING indicates whether the
1871 thread is (internally) executing or stopped. */
1941c569
PA
1872
1873static void
0d5b594f 1874remote_notice_new_inferior (ptid_t currthread, int executing)
1941c569 1875{
0d5b594f
PA
1876 /* In non-stop mode, we assume new found threads are (externally)
1877 running until proven otherwise with a stop reply. In all-stop,
1878 we can only get here if all threads are stopped. */
1879 int running = target_is_non_stop_p () ? 1 : 0;
1880
c906108c
SS
1881 /* If this is a new thread, add it to GDB's thread list.
1882 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1883
1884 if (in_thread_list (currthread) && is_exited (currthread))
1885 {
1886 /* We're seeing an event on a thread id we knew had exited.
1887 This has to be a new thread reusing the old id. Add it. */
0d5b594f 1888 remote_add_thread (currthread, running, executing);
82f73884
PA
1889 return;
1890 }
1891
79d7f229 1892 if (!in_thread_list (currthread))
c0a2216e 1893 {
1941c569 1894 struct inferior *inf = NULL;
bad34192 1895 int pid = ptid_get_pid (currthread);
1941c569 1896
bad34192
PA
1897 if (ptid_is_pid (inferior_ptid)
1898 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1899 {
1900 /* inferior_ptid has no thread member yet. This can happen
1901 with the vAttach -> remote_wait,"TAAthread:" path if the
1902 stub doesn't support qC. This is the first stop reported
1903 after an attach, so this is the main thread. Update the
1904 ptid in the thread list. */
bad34192
PA
1905 if (in_thread_list (pid_to_ptid (pid)))
1906 thread_change_ptid (inferior_ptid, currthread);
1907 else
1908 {
0d5b594f 1909 remote_add_thread (currthread, running, executing);
bad34192
PA
1910 inferior_ptid = currthread;
1911 }
dc146f7c 1912 return;
c0a2216e 1913 }
82f73884
PA
1914
1915 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1916 {
1917 /* inferior_ptid is not set yet. This can happen with the
1918 vRun -> remote_wait,"TAAthread:" path if the stub
1919 doesn't support qC. This is the first stop reported
1920 after an attach, so this is the main thread. Update the
1921 ptid in the thread list. */
dc146f7c 1922 thread_change_ptid (inferior_ptid, currthread);
82f73884 1923 return;
c0a2216e 1924 }
82f73884 1925
29c87f7f
PA
1926 /* When connecting to a target remote, or to a target
1927 extended-remote which already was debugging an inferior, we
1928 may not know about it yet. Add it before adding its child
1929 thread, so notifications are emitted in a sensible order. */
1930 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1931 {
1932 struct remote_state *rs = get_remote_state ();
1933 int fake_pid_p = !remote_multi_process_p (rs);
1934
1935 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1936 ptid_get_pid (currthread), -1, 1);
49c62f2e 1937 }
29c87f7f 1938
82f73884 1939 /* This is really a new thread. Add it. */
0d5b594f 1940 remote_add_thread (currthread, running, executing);
1941c569
PA
1941
1942 /* If we found a new inferior, let the common code do whatever
1943 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1944 breakpoints), unless we're just setting up an all-stop
1945 connection. */
1941c569 1946 if (inf != NULL)
b7ea362b
PA
1947 {
1948 struct remote_state *rs = get_remote_state ();
1949
6efcd9a8 1950 if (!rs->starting_up)
0d5b594f 1951 notice_new_inferior (currthread, executing, 0);
b7ea362b 1952 }
c0a2216e 1953 }
c906108c
SS
1954}
1955
85ad3aaf 1956/* Return THREAD's private thread data, creating it if necessary. */
dc146f7c 1957
70221824 1958static struct private_thread_info *
85ad3aaf 1959get_private_info_thread (struct thread_info *thread)
dc146f7c 1960{
85ad3aaf 1961 gdb_assert (thread != NULL);
dc146f7c 1962
85ad3aaf 1963 if (thread->priv == NULL)
dc146f7c 1964 {
85ad3aaf
PA
1965 struct private_thread_info *priv = XNEW (struct private_thread_info);
1966
1967 thread->private_dtor = free_private_thread_info;
1968 thread->priv = priv;
1969
1970 priv->core = -1;
1971 priv->extra = NULL;
1972 priv->name = NULL;
1973 priv->name = NULL;
1974 priv->last_resume_step = 0;
1975 priv->last_resume_sig = GDB_SIGNAL_0;
1976 priv->vcont_resumed = 0;
f6327dcb 1977 priv->thread_handle = nullptr;
dc146f7c
VP
1978 }
1979
85ad3aaf
PA
1980 return thread->priv;
1981}
1982
1983/* Return PTID's private thread data, creating it if necessary. */
1984
1985static struct private_thread_info *
1986get_private_info_ptid (ptid_t ptid)
1987{
1988 struct thread_info *info = find_thread_ptid (ptid);
1989
1990 return get_private_info_thread (info);
dc146f7c
VP
1991}
1992
74531fed
PA
1993/* Call this function as a result of
1994 1) A halt indication (T packet) containing a thread id
1995 2) A direct query of currthread
0df8b418 1996 3) Successful execution of set thread */
74531fed
PA
1997
1998static void
47f8a51d 1999record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 2000{
47f8a51d 2001 rs->general_thread = currthread;
74531fed
PA
2002}
2003
89be2091
DJ
2004/* If 'QPassSignals' is supported, tell the remote stub what signals
2005 it can simply pass through to the inferior without reporting. */
2006
2007static void
94bedb42
TT
2008remote_pass_signals (struct target_ops *self,
2009 int numsigs, unsigned char *pass_signals)
89be2091 2010{
4082afcc 2011 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
2012 {
2013 char *pass_packet, *p;
89be2091 2014 int count = 0, i;
747dc59d 2015 struct remote_state *rs = get_remote_state ();
89be2091
DJ
2016
2017 gdb_assert (numsigs < 256);
2018 for (i = 0; i < numsigs; i++)
2019 {
2455069d 2020 if (pass_signals[i])
89be2091
DJ
2021 count++;
2022 }
224c3ddb 2023 pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
89be2091
DJ
2024 strcpy (pass_packet, "QPassSignals:");
2025 p = pass_packet + strlen (pass_packet);
2026 for (i = 0; i < numsigs; i++)
2027 {
2455069d 2028 if (pass_signals[i])
89be2091
DJ
2029 {
2030 if (i >= 16)
2031 *p++ = tohex (i >> 4);
2032 *p++ = tohex (i & 15);
2033 if (count)
2034 *p++ = ';';
2035 else
2036 break;
2037 count--;
2038 }
2039 }
2040 *p = 0;
747dc59d 2041 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 2042 {
89be2091
DJ
2043 putpkt (pass_packet);
2044 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2045 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
2046 if (rs->last_pass_packet)
2047 xfree (rs->last_pass_packet);
2048 rs->last_pass_packet = pass_packet;
89be2091
DJ
2049 }
2050 else
2051 xfree (pass_packet);
2052 }
2053}
2054
82075af2
JS
2055/* If 'QCatchSyscalls' is supported, tell the remote stub
2056 to report syscalls to GDB. */
2057
2058static int
2059remote_set_syscall_catchpoint (struct target_ops *self,
2060 int pid, int needed, int any_count,
2061 int table_size, int *table)
2062{
2063 char *catch_packet;
2064 enum packet_result result;
2065 int n_sysno = 0;
2066
2067 if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
2068 {
2069 /* Not supported. */
2070 return 1;
2071 }
2072
2073 if (needed && !any_count)
2074 {
2075 int i;
2076
2077 /* Count how many syscalls are to be caught (table[sysno] != 0). */
2078 for (i = 0; i < table_size; i++)
2079 {
2080 if (table[i] != 0)
2081 n_sysno++;
2082 }
2083 }
2084
2085 if (remote_debug)
2086 {
2087 fprintf_unfiltered (gdb_stdlog,
2088 "remote_set_syscall_catchpoint "
2089 "pid %d needed %d any_count %d n_sysno %d\n",
2090 pid, needed, any_count, n_sysno);
2091 }
2092
2093 if (needed)
2094 {
2095 /* Prepare a packet with the sysno list, assuming max 8+1
2096 characters for a sysno. If the resulting packet size is too
2097 big, fallback on the non-selective packet. */
2098 const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
2099
c0518081 2100 catch_packet = (char *) xmalloc (maxpktsz);
82075af2
JS
2101 strcpy (catch_packet, "QCatchSyscalls:1");
2102 if (!any_count)
2103 {
2104 int i;
2105 char *p;
2106
2107 p = catch_packet;
2108 p += strlen (p);
2109
2110 /* Add in catch_packet each syscall to be caught (table[i] != 0). */
2111 for (i = 0; i < table_size; i++)
2112 {
2113 if (table[i] != 0)
2114 p += xsnprintf (p, catch_packet + maxpktsz - p, ";%x", i);
2115 }
2116 }
2117 if (strlen (catch_packet) > get_remote_packet_size ())
2118 {
2119 /* catch_packet too big. Fallback to less efficient
2120 non selective mode, with GDB doing the filtering. */
2121 catch_packet[sizeof ("QCatchSyscalls:1") - 1] = 0;
2122 }
2123 }
2124 else
2125 catch_packet = xstrdup ("QCatchSyscalls:0");
2126
2127 {
2128 struct cleanup *old_chain = make_cleanup (xfree, catch_packet);
2129 struct remote_state *rs = get_remote_state ();
2130
2131 putpkt (catch_packet);
2132 getpkt (&rs->buf, &rs->buf_size, 0);
2133 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
2134 do_cleanups (old_chain);
2135 if (result == PACKET_OK)
2136 return 0;
2137 else
2138 return -1;
2139 }
2140}
2141
9b224c5e
PA
2142/* If 'QProgramSignals' is supported, tell the remote stub what
2143 signals it should pass through to the inferior when detaching. */
2144
2145static void
daf5e9b6
TT
2146remote_program_signals (struct target_ops *self,
2147 int numsigs, unsigned char *signals)
9b224c5e 2148{
4082afcc 2149 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
2150 {
2151 char *packet, *p;
2152 int count = 0, i;
5e4a05c4 2153 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
2154
2155 gdb_assert (numsigs < 256);
2156 for (i = 0; i < numsigs; i++)
2157 {
2158 if (signals[i])
2159 count++;
2160 }
224c3ddb 2161 packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
9b224c5e
PA
2162 strcpy (packet, "QProgramSignals:");
2163 p = packet + strlen (packet);
2164 for (i = 0; i < numsigs; i++)
2165 {
2166 if (signal_pass_state (i))
2167 {
2168 if (i >= 16)
2169 *p++ = tohex (i >> 4);
2170 *p++ = tohex (i & 15);
2171 if (count)
2172 *p++ = ';';
2173 else
2174 break;
2175 count--;
2176 }
2177 }
2178 *p = 0;
5e4a05c4
TT
2179 if (!rs->last_program_signals_packet
2180 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 2181 {
9b224c5e
PA
2182 putpkt (packet);
2183 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2184 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
2185 xfree (rs->last_program_signals_packet);
2186 rs->last_program_signals_packet = packet;
9b224c5e
PA
2187 }
2188 else
2189 xfree (packet);
2190 }
2191}
2192
79d7f229
PA
2193/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2194 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2195 thread. If GEN is set, set the general thread, if not, then set
2196 the step/continue thread. */
c906108c 2197static void
d62a8ae2 2198set_thread (ptid_t ptid, int gen)
c906108c 2199{
d01949b6 2200 struct remote_state *rs = get_remote_state ();
47f8a51d 2201 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 2202 char *buf = rs->buf;
79d7f229 2203 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 2204
79d7f229 2205 if (ptid_equal (state, ptid))
c906108c
SS
2206 return;
2207
79d7f229
PA
2208 *buf++ = 'H';
2209 *buf++ = gen ? 'g' : 'c';
2210 if (ptid_equal (ptid, magic_null_ptid))
2211 xsnprintf (buf, endbuf - buf, "0");
2212 else if (ptid_equal (ptid, any_thread_ptid))
2213 xsnprintf (buf, endbuf - buf, "0");
2214 else if (ptid_equal (ptid, minus_one_ptid))
2215 xsnprintf (buf, endbuf - buf, "-1");
2216 else
82f73884 2217 write_ptid (buf, endbuf, ptid);
79d7f229 2218 putpkt (rs->buf);
6d820c5c 2219 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2220 if (gen)
47f8a51d 2221 rs->general_thread = ptid;
c906108c 2222 else
47f8a51d 2223 rs->continue_thread = ptid;
c906108c 2224}
79d7f229
PA
2225
2226static void
d62a8ae2 2227set_general_thread (ptid_t ptid)
79d7f229
PA
2228{
2229 set_thread (ptid, 1);
2230}
2231
2232static void
d62a8ae2 2233set_continue_thread (ptid_t ptid)
79d7f229
PA
2234{
2235 set_thread (ptid, 0);
2236}
2237
3c9c4b83
PA
2238/* Change the remote current process. Which thread within the process
2239 ends up selected isn't important, as long as it is the same process
2240 as what INFERIOR_PTID points to.
2241
2242 This comes from that fact that there is no explicit notion of
2243 "selected process" in the protocol. The selected process for
2244 general operations is the process the selected general thread
2245 belongs to. */
2246
2247static void
2248set_general_process (void)
2249{
2250 struct remote_state *rs = get_remote_state ();
2251
2252 /* If the remote can't handle multiple processes, don't bother. */
8020350c 2253 if (!remote_multi_process_p (rs))
3c9c4b83
PA
2254 return;
2255
2256 /* We only need to change the remote current thread if it's pointing
2257 at some other process. */
47f8a51d 2258 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2259 set_general_thread (inferior_ptid);
2260}
2261
c906108c 2262\f
7d1a114c
PA
2263/* Return nonzero if this is the main thread that we made up ourselves
2264 to model non-threaded targets as single-threaded. */
c906108c
SS
2265
2266static int
7d1a114c 2267remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 2268{
c0a2216e
PA
2269 if (ptid_equal (ptid, magic_null_ptid))
2270 /* The main thread is always alive. */
2271 return 1;
2272
ba348170 2273 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2274 /* The main thread is always alive. This can happen after a
2275 vAttach, if the remote side doesn't support
2276 multi-threading. */
2277 return 1;
2278
7d1a114c
PA
2279 return 0;
2280}
2281
2282/* Return nonzero if the thread PTID is still alive on the remote
2283 system. */
2284
2285static int
2286remote_thread_alive (struct target_ops *ops, ptid_t ptid)
2287{
2288 struct remote_state *rs = get_remote_state ();
2289 char *p, *endp;
2290
2291 /* Check if this is a thread that we made up ourselves to model
2292 non-threaded targets as single-threaded. */
2293 if (remote_thread_always_alive (ops, ptid))
2294 return 1;
2295
82f73884
PA
2296 p = rs->buf;
2297 endp = rs->buf + get_remote_packet_size ();
2298
2299 *p++ = 'T';
2300 write_ptid (p, endp, ptid);
2301
2e9f7625 2302 putpkt (rs->buf);
6d820c5c 2303 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2304 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2305}
2306
79efa585
SM
2307/* Return a pointer to a thread name if we know it and NULL otherwise.
2308 The thread_info object owns the memory for the name. */
2309
2310static const char *
2311remote_thread_name (struct target_ops *ops, struct thread_info *info)
2312{
2313 if (info->priv != NULL)
2314 return info->priv->name;
2315
2316 return NULL;
2317}
2318
c906108c
SS
2319/* About these extended threadlist and threadinfo packets. They are
2320 variable length packets but, the fields within them are often fixed
2321 length. They are redundent enough to send over UDP as is the
2322 remote protocol in general. There is a matching unit test module
2323 in libstub. */
2324
23860348 2325/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2326 libstub protocol encoding, and remote.c. It is not particularly
23860348 2327 changable. */
cce74817
JM
2328
2329/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2330 Plan to fix this. */
cce74817 2331
23860348 2332typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2333
9d1f7ab2 2334/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2335 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2336
2337struct gdb_ext_thread_info
c5aa993b 2338 {
23860348 2339 threadref threadid; /* External form of thread reference. */
2bc416ba 2340 int active; /* Has state interesting to GDB?
23860348 2341 regs, stack. */
2bc416ba 2342 char display[256]; /* Brief state display, name,
cedea757 2343 blocked/suspended. */
23860348 2344 char shortname[32]; /* To be used to name threads. */
2bc416ba 2345 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2346 whatever. */
c5aa993b 2347 };
cce74817
JM
2348
2349/* The volume of remote transfers can be limited by submitting
2350 a mask containing bits specifying the desired information.
2351 Use a union of these values as the 'selection' parameter to
0df8b418 2352 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2353
2354#define TAG_THREADID 1
2355#define TAG_EXISTS 2
2356#define TAG_DISPLAY 4
2357#define TAG_THREADNAME 8
c5aa993b 2358#define TAG_MOREDISPLAY 16
cce74817 2359
23860348 2360#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2361
a14ed312 2362static char *unpack_nibble (char *buf, int *val);
cce74817 2363
a14ed312 2364static char *unpack_byte (char *buf, int *value);
cce74817 2365
a14ed312 2366static char *pack_int (char *buf, int value);
cce74817 2367
a14ed312 2368static char *unpack_int (char *buf, int *value);
cce74817 2369
a14ed312 2370static char *unpack_string (char *src, char *dest, int length);
cce74817 2371
23860348 2372static char *pack_threadid (char *pkt, threadref *id);
cce74817 2373
23860348 2374static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2375
23860348 2376void int_to_threadref (threadref *id, int value);
cce74817 2377
23860348 2378static int threadref_to_int (threadref *ref);
cce74817 2379
23860348 2380static void copy_threadref (threadref *dest, threadref *src);
cce74817 2381
23860348 2382static int threadmatch (threadref *dest, threadref *src);
cce74817 2383
2bc416ba 2384static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2385 threadref *id);
cce74817 2386
a14ed312 2387static int remote_unpack_thread_info_response (char *pkt,
23860348 2388 threadref *expectedref,
a14ed312
KB
2389 struct gdb_ext_thread_info
2390 *info);
cce74817
JM
2391
2392
2bc416ba 2393static int remote_get_threadinfo (threadref *threadid,
23860348 2394 int fieldset, /*TAG mask */
a14ed312 2395 struct gdb_ext_thread_info *info);
cce74817 2396
a14ed312
KB
2397static char *pack_threadlist_request (char *pkt, int startflag,
2398 int threadcount,
23860348 2399 threadref *nextthread);
cce74817 2400
a14ed312
KB
2401static int parse_threadlist_response (char *pkt,
2402 int result_limit,
23860348 2403 threadref *original_echo,
2bc416ba 2404 threadref *resultlist,
23860348 2405 int *doneflag);
cce74817 2406
a14ed312 2407static int remote_get_threadlist (int startflag,
23860348 2408 threadref *nextthread,
a14ed312
KB
2409 int result_limit,
2410 int *done,
2bc416ba 2411 int *result_count,
23860348 2412 threadref *threadlist);
cce74817 2413
23860348 2414typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2415
a14ed312
KB
2416static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2417 void *context, int looplimit);
cce74817 2418
23860348 2419static int remote_newthread_step (threadref *ref, void *context);
cce74817 2420
82f73884
PA
2421
2422/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2423 buffer we're allowed to write to. Returns
2424 BUF+CHARACTERS_WRITTEN. */
2425
2426static char *
2427write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2428{
2429 int pid, tid;
2430 struct remote_state *rs = get_remote_state ();
2431
2432 if (remote_multi_process_p (rs))
2433 {
2434 pid = ptid_get_pid (ptid);
2435 if (pid < 0)
2436 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2437 else
2438 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2439 }
ba348170 2440 tid = ptid_get_lwp (ptid);
82f73884
PA
2441 if (tid < 0)
2442 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2443 else
2444 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2445
2446 return buf;
2447}
2448
256642e8
PA
2449/* Extract a PTID from BUF. If non-null, OBUF is set to one past the
2450 last parsed char. Returns null_ptid if no thread id is found, and
2451 throws an error if the thread id has an invalid format. */
82f73884
PA
2452
2453static ptid_t
256642e8 2454read_ptid (const char *buf, const char **obuf)
82f73884 2455{
256642e8
PA
2456 const char *p = buf;
2457 const char *pp;
82f73884 2458 ULONGEST pid = 0, tid = 0;
82f73884
PA
2459
2460 if (*p == 'p')
2461 {
2462 /* Multi-process ptid. */
2463 pp = unpack_varlen_hex (p + 1, &pid);
2464 if (*pp != '.')
b37520b6 2465 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2466
2467 p = pp;
2468 pp = unpack_varlen_hex (p + 1, &tid);
2469 if (obuf)
2470 *obuf = pp;
ba348170 2471 return ptid_build (pid, tid, 0);
82f73884
PA
2472 }
2473
2474 /* No multi-process. Just a tid. */
2475 pp = unpack_varlen_hex (p, &tid);
2476
c9f35b34
KB
2477 /* Return null_ptid when no thread id is found. */
2478 if (p == pp)
2479 {
2480 if (obuf)
2481 *obuf = pp;
2482 return null_ptid;
2483 }
2484
82f73884 2485 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2486 what's in inferior_ptid, unless it's null at this point. If so,
2487 then since there's no way to know the pid of the reported
2488 threads, use the magic number. */
2489 if (ptid_equal (inferior_ptid, null_ptid))
2490 pid = ptid_get_pid (magic_null_ptid);
2491 else
2492 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2493
2494 if (obuf)
2495 *obuf = pp;
ba348170 2496 return ptid_build (pid, tid, 0);
82f73884
PA
2497}
2498
c906108c 2499static int
fba45db2 2500stubhex (int ch)
c906108c
SS
2501{
2502 if (ch >= 'a' && ch <= 'f')
2503 return ch - 'a' + 10;
2504 if (ch >= '0' && ch <= '9')
2505 return ch - '0';
2506 if (ch >= 'A' && ch <= 'F')
2507 return ch - 'A' + 10;
2508 return -1;
2509}
2510
2511static int
fba45db2 2512stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2513{
2514 int nibble;
2515 int retval = 0;
2516
2517 while (fieldlength)
2518 {
2519 nibble = stubhex (*buff++);
2520 retval |= nibble;
2521 fieldlength--;
2522 if (fieldlength)
2523 retval = retval << 4;
2524 }
2525 return retval;
2526}
2527
c906108c 2528static char *
fba45db2 2529unpack_nibble (char *buf, int *val)
c906108c 2530{
b7589f7d 2531 *val = fromhex (*buf++);
c906108c
SS
2532 return buf;
2533}
2534
c906108c 2535static char *
fba45db2 2536unpack_byte (char *buf, int *value)
c906108c
SS
2537{
2538 *value = stub_unpack_int (buf, 2);
2539 return buf + 2;
2540}
2541
2542static char *
fba45db2 2543pack_int (char *buf, int value)
c906108c
SS
2544{
2545 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2546 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2547 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2548 buf = pack_hex_byte (buf, (value & 0xff));
2549 return buf;
2550}
2551
2552static char *
fba45db2 2553unpack_int (char *buf, int *value)
c906108c
SS
2554{
2555 *value = stub_unpack_int (buf, 8);
2556 return buf + 8;
2557}
2558
23860348 2559#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2560static char *pack_string (char *pkt, char *string);
c906108c
SS
2561
2562static char *
fba45db2 2563pack_string (char *pkt, char *string)
c906108c
SS
2564{
2565 char ch;
2566 int len;
2567
2568 len = strlen (string);
2569 if (len > 200)
23860348 2570 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2571 pkt = pack_hex_byte (pkt, len);
2572 while (len-- > 0)
2573 {
2574 ch = *string++;
2575 if ((ch == '\0') || (ch == '#'))
23860348 2576 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2577 *pkt++ = ch;
2578 }
2579 return pkt;
2580}
2581#endif /* 0 (unused) */
2582
2583static char *
fba45db2 2584unpack_string (char *src, char *dest, int length)
c906108c
SS
2585{
2586 while (length--)
2587 *dest++ = *src++;
2588 *dest = '\0';
2589 return src;
2590}
2591
2592static char *
fba45db2 2593pack_threadid (char *pkt, threadref *id)
c906108c
SS
2594{
2595 char *limit;
2596 unsigned char *altid;
2597
2598 altid = (unsigned char *) id;
2599 limit = pkt + BUF_THREAD_ID_SIZE;
2600 while (pkt < limit)
2601 pkt = pack_hex_byte (pkt, *altid++);
2602 return pkt;
2603}
2604
2605
2606static char *
fba45db2 2607unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2608{
2609 char *altref;
2610 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2611 int x, y;
2612
2613 altref = (char *) id;
2614
2615 while (inbuf < limit)
2616 {
2617 x = stubhex (*inbuf++);
2618 y = stubhex (*inbuf++);
2619 *altref++ = (x << 4) | y;
2620 }
2621 return inbuf;
2622}
2623
2624/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2625 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2626 to use 64bit thread references internally. This is an adapter
2627 function. */
2628
2629void
fba45db2 2630int_to_threadref (threadref *id, int value)
c906108c
SS
2631{
2632 unsigned char *scan;
2633
2634 scan = (unsigned char *) id;
2635 {
2636 int i = 4;
2637 while (i--)
2638 *scan++ = 0;
2639 }
2640 *scan++ = (value >> 24) & 0xff;
2641 *scan++ = (value >> 16) & 0xff;
2642 *scan++ = (value >> 8) & 0xff;
2643 *scan++ = (value & 0xff);
2644}
2645
2646static int
fba45db2 2647threadref_to_int (threadref *ref)
c906108c
SS
2648{
2649 int i, value = 0;
2650 unsigned char *scan;
2651
cfd77fa1 2652 scan = *ref;
c906108c
SS
2653 scan += 4;
2654 i = 4;
2655 while (i-- > 0)
2656 value = (value << 8) | ((*scan++) & 0xff);
2657 return value;
2658}
2659
2660static void
fba45db2 2661copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2662{
2663 int i;
2664 unsigned char *csrc, *cdest;
2665
2666 csrc = (unsigned char *) src;
2667 cdest = (unsigned char *) dest;
2668 i = 8;
2669 while (i--)
2670 *cdest++ = *csrc++;
2671}
2672
2673static int
fba45db2 2674threadmatch (threadref *dest, threadref *src)
c906108c 2675{
23860348 2676 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2677#if 0
2678 unsigned char *srcp, *destp;
2679 int i, result;
2680 srcp = (char *) src;
2681 destp = (char *) dest;
2682
2683 result = 1;
2684 while (i-- > 0)
2685 result &= (*srcp++ == *destp++) ? 1 : 0;
2686 return result;
2687#endif
2688 return 1;
2689}
2690
2691/*
c5aa993b
JM
2692 threadid:1, # always request threadid
2693 context_exists:2,
2694 display:4,
2695 unique_name:8,
2696 more_display:16
2697 */
c906108c
SS
2698
2699/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2700
2701static char *
fba45db2 2702pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2703{
23860348
MS
2704 *pkt++ = 'q'; /* Info Query */
2705 *pkt++ = 'P'; /* process or thread info */
2706 pkt = pack_int (pkt, mode); /* mode */
c906108c 2707 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2708 *pkt = '\0'; /* terminate */
c906108c
SS
2709 return pkt;
2710}
2711
23860348 2712/* These values tag the fields in a thread info response packet. */
c906108c 2713/* Tagging the fields allows us to request specific fields and to
23860348 2714 add more fields as time goes by. */
c906108c 2715
23860348 2716#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2717#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2718 fetch registers and its stack? */
c5aa993b 2719#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2720#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2721#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2722 the process. */
c906108c
SS
2723
2724static int
fba45db2
KB
2725remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2726 struct gdb_ext_thread_info *info)
c906108c 2727{
d01949b6 2728 struct remote_state *rs = get_remote_state ();
c906108c 2729 int mask, length;
cfd77fa1 2730 int tag;
c906108c 2731 threadref ref;
6d820c5c 2732 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2733 int retval = 1;
2734
23860348 2735 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2736 info->active = 0;
2737 info->display[0] = '\0';
2738 info->shortname[0] = '\0';
2739 info->more_display[0] = '\0';
2740
23860348
MS
2741 /* Assume the characters indicating the packet type have been
2742 stripped. */
c906108c
SS
2743 pkt = unpack_int (pkt, &mask); /* arg mask */
2744 pkt = unpack_threadid (pkt, &ref);
2745
2746 if (mask == 0)
8a3fe4f8 2747 warning (_("Incomplete response to threadinfo request."));
c906108c 2748 if (!threadmatch (&ref, expectedref))
23860348 2749 { /* This is an answer to a different request. */
8a3fe4f8 2750 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2751 return 0;
2752 }
2753 copy_threadref (&info->threadid, &ref);
2754
23860348 2755 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2756
23860348
MS
2757 /* Packets are terminated with nulls. */
2758 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2759 {
2760 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2761 pkt = unpack_byte (pkt, &length); /* length */
2762 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2763 {
8a3fe4f8 2764 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2765 retval = 0;
2766 break;
2767 }
2768 if (tag == TAG_THREADID)
2769 {
2770 if (length != 16)
2771 {
8a3fe4f8 2772 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2773 retval = 0;
2774 break;
2775 }
2776 pkt = unpack_threadid (pkt, &ref);
2777 mask = mask & ~TAG_THREADID;
2778 continue;
2779 }
2780 if (tag == TAG_EXISTS)
2781 {
2782 info->active = stub_unpack_int (pkt, length);
2783 pkt += length;
2784 mask = mask & ~(TAG_EXISTS);
2785 if (length > 8)
2786 {
8a3fe4f8 2787 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2788 retval = 0;
2789 break;
2790 }
2791 continue;
2792 }
2793 if (tag == TAG_THREADNAME)
2794 {
2795 pkt = unpack_string (pkt, &info->shortname[0], length);
2796 mask = mask & ~TAG_THREADNAME;
2797 continue;
2798 }
2799 if (tag == TAG_DISPLAY)
2800 {
2801 pkt = unpack_string (pkt, &info->display[0], length);
2802 mask = mask & ~TAG_DISPLAY;
2803 continue;
2804 }
2805 if (tag == TAG_MOREDISPLAY)
2806 {
2807 pkt = unpack_string (pkt, &info->more_display[0], length);
2808 mask = mask & ~TAG_MOREDISPLAY;
2809 continue;
2810 }
8a3fe4f8 2811 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2812 break; /* Not a tag we know about. */
c906108c
SS
2813 }
2814 return retval;
2815}
2816
2817static int
fba45db2
KB
2818remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2819 struct gdb_ext_thread_info *info)
c906108c 2820{
d01949b6 2821 struct remote_state *rs = get_remote_state ();
c906108c 2822 int result;
c906108c 2823
2e9f7625
DJ
2824 pack_threadinfo_request (rs->buf, fieldset, threadid);
2825 putpkt (rs->buf);
6d820c5c 2826 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2827
2828 if (rs->buf[0] == '\0')
2829 return 0;
2830
2e9f7625 2831 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2832 threadid, info);
c906108c
SS
2833 return result;
2834}
2835
c906108c
SS
2836/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2837
2838static char *
fba45db2
KB
2839pack_threadlist_request (char *pkt, int startflag, int threadcount,
2840 threadref *nextthread)
c906108c
SS
2841{
2842 *pkt++ = 'q'; /* info query packet */
2843 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2844 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2845 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2846 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2847 *pkt = '\0';
2848 return pkt;
2849}
2850
2851/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2852
2853static int
fba45db2
KB
2854parse_threadlist_response (char *pkt, int result_limit,
2855 threadref *original_echo, threadref *resultlist,
2856 int *doneflag)
c906108c 2857{
d01949b6 2858 struct remote_state *rs = get_remote_state ();
c906108c
SS
2859 char *limit;
2860 int count, resultcount, done;
2861
2862 resultcount = 0;
2863 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2864 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2865 /* done parse past here */
c906108c
SS
2866 pkt = unpack_byte (pkt, &count); /* count field */
2867 pkt = unpack_nibble (pkt, &done);
2868 /* The first threadid is the argument threadid. */
2869 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2870 while ((count-- > 0) && (pkt < limit))
2871 {
2872 pkt = unpack_threadid (pkt, resultlist++);
2873 if (resultcount++ >= result_limit)
2874 break;
2875 }
2876 if (doneflag)
2877 *doneflag = done;
2878 return resultcount;
2879}
2880
6dc54d91
PA
2881/* Fetch the next batch of threads from the remote. Returns -1 if the
2882 qL packet is not supported, 0 on error and 1 on success. */
2883
c906108c 2884static int
fba45db2
KB
2885remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2886 int *done, int *result_count, threadref *threadlist)
c906108c 2887{
d01949b6 2888 struct remote_state *rs = get_remote_state ();
c906108c
SS
2889 int result = 1;
2890
23860348 2891 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2892 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2893 >= get_remote_packet_size ())
ea9c271d 2894 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2895
6d820c5c
DJ
2896 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2897 putpkt (rs->buf);
2898 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2899 if (*rs->buf == '\0')
6dc54d91
PA
2900 {
2901 /* Packet not supported. */
2902 return -1;
2903 }
2904
2905 *result_count =
2906 parse_threadlist_response (rs->buf + 2, result_limit,
2907 &rs->echo_nextthread, threadlist, done);
c906108c 2908
0d031856 2909 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2910 {
23860348
MS
2911 /* FIXME: This is a good reason to drop the packet. */
2912 /* Possably, there is a duplicate response. */
c906108c
SS
2913 /* Possabilities :
2914 retransmit immediatly - race conditions
2915 retransmit after timeout - yes
2916 exit
2917 wait for packet, then exit
2918 */
8a3fe4f8 2919 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2920 return 0; /* I choose simply exiting. */
c906108c
SS
2921 }
2922 if (*result_count <= 0)
2923 {
2924 if (*done != 1)
2925 {
8a3fe4f8 2926 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2927 result = 0;
2928 }
2929 return result; /* break; */
2930 }
2931 if (*result_count > result_limit)
2932 {
2933 *result_count = 0;
8a3fe4f8 2934 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2935 return 0;
2936 }
2937 return result;
2938}
2939
6dc54d91
PA
2940/* Fetch the list of remote threads, with the qL packet, and call
2941 STEPFUNCTION for each thread found. Stops iterating and returns 1
2942 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2943 STEPFUNCTION returns false. If the packet is not supported,
2944 returns -1. */
c906108c 2945
c906108c 2946static int
fba45db2
KB
2947remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2948 int looplimit)
c906108c 2949{
0d031856 2950 struct remote_state *rs = get_remote_state ();
c906108c
SS
2951 int done, i, result_count;
2952 int startflag = 1;
2953 int result = 1;
2954 int loopcount = 0;
c906108c
SS
2955
2956 done = 0;
2957 while (!done)
2958 {
2959 if (loopcount++ > looplimit)
2960 {
2961 result = 0;
8a3fe4f8 2962 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2963 break;
2964 }
6dc54d91
PA
2965 result = remote_get_threadlist (startflag, &rs->nextthread,
2966 MAXTHREADLISTRESULTS,
2967 &done, &result_count,
2968 rs->resultthreadlist);
2969 if (result <= 0)
2970 break;
23860348 2971 /* Clear for later iterations. */
c906108c
SS
2972 startflag = 0;
2973 /* Setup to resume next batch of thread references, set nextthread. */
2974 if (result_count >= 1)
0d031856
TT
2975 copy_threadref (&rs->nextthread,
2976 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2977 i = 0;
2978 while (result_count--)
6dc54d91
PA
2979 {
2980 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2981 {
2982 result = 0;
2983 break;
2984 }
2985 }
c906108c
SS
2986 }
2987 return result;
2988}
2989
6dc54d91
PA
2990/* A thread found on the remote target. */
2991
2992typedef struct thread_item
2993{
2994 /* The thread's PTID. */
2995 ptid_t ptid;
2996
2997 /* The thread's extra info. May be NULL. */
2998 char *extra;
2999
79efa585
SM
3000 /* The thread's name. May be NULL. */
3001 char *name;
3002
6dc54d91
PA
3003 /* The core the thread was running on. -1 if not known. */
3004 int core;
f6327dcb
KB
3005
3006 /* The thread handle associated with the thread. */
3007 gdb::byte_vector *thread_handle;
3008
6dc54d91
PA
3009} thread_item_t;
3010DEF_VEC_O(thread_item_t);
3011
3012/* Context passed around to the various methods listing remote
3013 threads. As new threads are found, they're added to the ITEMS
3014 vector. */
3015
3016struct threads_listing_context
3017{
3018 /* The threads found on the remote target. */
3019 VEC (thread_item_t) *items;
3020};
3021
80134cf5
PA
3022/* Discard the contents of the constructed thread listing context. */
3023
3024static void
3025clear_threads_listing_context (void *p)
3026{
19ba03f4
SM
3027 struct threads_listing_context *context
3028 = (struct threads_listing_context *) p;
80134cf5
PA
3029 int i;
3030 struct thread_item *item;
3031
3032 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
79efa585
SM
3033 {
3034 xfree (item->extra);
3035 xfree (item->name);
f6327dcb 3036 delete item->thread_handle;
79efa585 3037 }
80134cf5
PA
3038
3039 VEC_free (thread_item_t, context->items);
3040}
3041
cbb8991c
DB
3042/* Remove the thread specified as the related_pid field of WS
3043 from the CONTEXT list. */
3044
3045static void
3046threads_listing_context_remove (struct target_waitstatus *ws,
3047 struct threads_listing_context *context)
3048{
3049 struct thread_item *item;
3050 int i;
3051 ptid_t child_ptid = ws->value.related_pid;
3052
3053 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
3054 {
3055 if (ptid_equal (item->ptid, child_ptid))
3056 {
3057 VEC_ordered_remove (thread_item_t, context->items, i);
3058 break;
3059 }
3060 }
3061}
3062
c906108c 3063static int
6dc54d91 3064remote_newthread_step (threadref *ref, void *data)
c906108c 3065{
19ba03f4
SM
3066 struct threads_listing_context *context
3067 = (struct threads_listing_context *) data;
6dc54d91 3068 struct thread_item item;
79d7f229 3069 int pid = ptid_get_pid (inferior_ptid);
39f77062 3070
6dc54d91
PA
3071 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
3072 item.core = -1;
2e3b657e 3073 item.name = NULL;
6dc54d91 3074 item.extra = NULL;
f6327dcb 3075 item.thread_handle = nullptr;
6dc54d91
PA
3076
3077 VEC_safe_push (thread_item_t, context->items, &item);
3078
c906108c
SS
3079 return 1; /* continue iterator */
3080}
3081
3082#define CRAZY_MAX_THREADS 1000
3083
39f77062
KB
3084static ptid_t
3085remote_current_thread (ptid_t oldpid)
c906108c 3086{
d01949b6 3087 struct remote_state *rs = get_remote_state ();
c906108c
SS
3088
3089 putpkt ("qC");
6d820c5c 3090 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3091 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34 3092 {
256642e8 3093 const char *obuf;
c9f35b34
KB
3094 ptid_t result;
3095
3096 result = read_ptid (&rs->buf[2], &obuf);
3097 if (*obuf != '\0' && remote_debug)
3098 fprintf_unfiltered (gdb_stdlog,
3099 "warning: garbage in qC reply\n");
3100
3101 return result;
3102 }
c906108c
SS
3103 else
3104 return oldpid;
3105}
3106
6dc54d91 3107/* List remote threads using the deprecated qL packet. */
cce74817 3108
6dc54d91
PA
3109static int
3110remote_get_threads_with_ql (struct target_ops *ops,
3111 struct threads_listing_context *context)
c906108c 3112{
6dc54d91
PA
3113 if (remote_threadlist_iterator (remote_newthread_step, context,
3114 CRAZY_MAX_THREADS) >= 0)
3115 return 1;
3116
3117 return 0;
c906108c
SS
3118}
3119
dc146f7c
VP
3120#if defined(HAVE_LIBEXPAT)
3121
dc146f7c
VP
3122static void
3123start_thread (struct gdb_xml_parser *parser,
3124 const struct gdb_xml_element *element,
3125 void *user_data, VEC(gdb_xml_value_s) *attributes)
3126{
19ba03f4
SM
3127 struct threads_listing_context *data
3128 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3129
3130 struct thread_item item;
3131 char *id;
3d2c1d41 3132 struct gdb_xml_value *attr;
dc146f7c 3133
19ba03f4 3134 id = (char *) xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
3135 item.ptid = read_ptid (id, NULL);
3136
3d2c1d41
PA
3137 attr = xml_find_attribute (attributes, "core");
3138 if (attr != NULL)
3139 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
3140 else
3141 item.core = -1;
3142
79efa585 3143 attr = xml_find_attribute (attributes, "name");
e1961661 3144 item.name = attr != NULL ? xstrdup ((const char *) attr->value) : NULL;
79efa585 3145
f6327dcb
KB
3146 attr = xml_find_attribute (attributes, "handle");
3147 if (attr != NULL)
3148 {
3149 item.thread_handle = new gdb::byte_vector
3150 (strlen ((const char *) attr->value) / 2);
3151 hex2bin ((const char *) attr->value, item.thread_handle->data (),
3152 item.thread_handle->size ());
3153 }
3154 else
3155 item.thread_handle = nullptr;
3156
dc146f7c
VP
3157 item.extra = 0;
3158
3159 VEC_safe_push (thread_item_t, data->items, &item);
3160}
3161
3162static void
3163end_thread (struct gdb_xml_parser *parser,
3164 const struct gdb_xml_element *element,
3165 void *user_data, const char *body_text)
3166{
19ba03f4
SM
3167 struct threads_listing_context *data
3168 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3169
3170 if (body_text && *body_text)
2ae2a0b7 3171 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
3172}
3173
3174const struct gdb_xml_attribute thread_attributes[] = {
3175 { "id", GDB_XML_AF_NONE, NULL, NULL },
3176 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
79efa585 3177 { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
f6327dcb 3178 { "handle", GDB_XML_AF_OPTIONAL, NULL, NULL },
dc146f7c
VP
3179 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3180};
3181
3182const struct gdb_xml_element thread_children[] = {
3183 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3184};
3185
3186const struct gdb_xml_element threads_children[] = {
3187 { "thread", thread_attributes, thread_children,
3188 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3189 start_thread, end_thread },
3190 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3191};
3192
3193const struct gdb_xml_element threads_elements[] = {
3194 { "threads", NULL, threads_children,
3195 GDB_XML_EF_NONE, NULL, NULL },
3196 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3197};
3198
3199#endif
3200
6dc54d91 3201/* List remote threads using qXfer:threads:read. */
9d1f7ab2 3202
6dc54d91
PA
3203static int
3204remote_get_threads_with_qxfer (struct target_ops *ops,
3205 struct threads_listing_context *context)
0f71a2f6 3206{
dc146f7c 3207#if defined(HAVE_LIBEXPAT)
4082afcc 3208 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 3209 {
6dc54d91 3210 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 3211 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 3212
6dc54d91 3213 if (xml != NULL && *xml != '\0')
dc146f7c 3214 {
6dc54d91
PA
3215 gdb_xml_parse_quick (_("threads"), "threads.dtd",
3216 threads_elements, xml, context);
dc146f7c
VP
3217 }
3218
3219 do_cleanups (back_to);
6dc54d91 3220 return 1;
dc146f7c
VP
3221 }
3222#endif
3223
6dc54d91
PA
3224 return 0;
3225}
3226
3227/* List remote threads using qfThreadInfo/qsThreadInfo. */
3228
3229static int
3230remote_get_threads_with_qthreadinfo (struct target_ops *ops,
3231 struct threads_listing_context *context)
3232{
3233 struct remote_state *rs = get_remote_state ();
3234
b80fafe3 3235 if (rs->use_threadinfo_query)
9d1f7ab2 3236 {
256642e8 3237 const char *bufp;
6dc54d91 3238
9d1f7ab2 3239 putpkt ("qfThreadInfo");
6d820c5c 3240 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3241 bufp = rs->buf;
9d1f7ab2 3242 if (bufp[0] != '\0') /* q packet recognized */
802188a7 3243 {
9d1f7ab2
MS
3244 while (*bufp++ == 'm') /* reply contains one or more TID */
3245 {
3246 do
3247 {
6dc54d91
PA
3248 struct thread_item item;
3249
3250 item.ptid = read_ptid (bufp, &bufp);
3251 item.core = -1;
2e3b657e 3252 item.name = NULL;
6dc54d91 3253 item.extra = NULL;
f6327dcb 3254 item.thread_handle = nullptr;
6dc54d91
PA
3255
3256 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
3257 }
3258 while (*bufp++ == ','); /* comma-separated list */
3259 putpkt ("qsThreadInfo");
6d820c5c 3260 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3261 bufp = rs->buf;
9d1f7ab2 3262 }
6dc54d91
PA
3263 return 1;
3264 }
3265 else
3266 {
3267 /* Packet not recognized. */
3268 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3269 }
3270 }
3271
6dc54d91
PA
3272 return 0;
3273}
3274
e8032dde 3275/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3276 targets. */
3277
3278static void
e8032dde 3279remote_update_thread_list (struct target_ops *ops)
6dc54d91 3280{
6dc54d91
PA
3281 struct threads_listing_context context;
3282 struct cleanup *old_chain;
ab970af1 3283 int got_list = 0;
e8032dde 3284
6dc54d91
PA
3285 context.items = NULL;
3286 old_chain = make_cleanup (clear_threads_listing_context, &context);
3287
3288 /* We have a few different mechanisms to fetch the thread list. Try
3289 them all, starting with the most preferred one first, falling
3290 back to older methods. */
3291 if (remote_get_threads_with_qxfer (ops, &context)
3292 || remote_get_threads_with_qthreadinfo (ops, &context)
3293 || remote_get_threads_with_ql (ops, &context))
3294 {
3295 int i;
3296 struct thread_item *item;
ab970af1
PA
3297 struct thread_info *tp, *tmp;
3298
3299 got_list = 1;
3300
7d1a114c
PA
3301 if (VEC_empty (thread_item_t, context.items)
3302 && remote_thread_always_alive (ops, inferior_ptid))
3303 {
3304 /* Some targets don't really support threads, but still
3305 reply an (empty) thread list in response to the thread
3306 listing packets, instead of replying "packet not
3307 supported". Exit early so we don't delete the main
3308 thread. */
3309 do_cleanups (old_chain);
3310 return;
3311 }
3312
ab970af1
PA
3313 /* CONTEXT now holds the current thread list on the remote
3314 target end. Delete GDB-side threads no longer found on the
3315 target. */
8a06aea7 3316 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3317 {
ab970af1
PA
3318 for (i = 0;
3319 VEC_iterate (thread_item_t, context.items, i, item);
3320 ++i)
3321 {
3322 if (ptid_equal (item->ptid, tp->ptid))
3323 break;
3324 }
3325
3326 if (i == VEC_length (thread_item_t, context.items))
3327 {
3328 /* Not found. */
3329 delete_thread (tp->ptid);
3330 }
cbb8991c
DB
3331 }
3332
3333 /* Remove any unreported fork child threads from CONTEXT so
3334 that we don't interfere with follow fork, which is where
3335 creation of such threads is handled. */
3336 remove_new_fork_children (&context);
74531fed 3337
ab970af1 3338 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
3339 for (i = 0;
3340 VEC_iterate (thread_item_t, context.items, i, item);
3341 ++i)
3342 {
3343 if (!ptid_equal (item->ptid, null_ptid))
3344 {
3345 struct private_thread_info *info;
3346 /* In non-stop mode, we assume new found threads are
0d5b594f
PA
3347 executing until proven otherwise with a stop reply.
3348 In all-stop, we can only get here if all threads are
6dc54d91 3349 stopped. */
0d5b594f 3350 int executing = target_is_non_stop_p () ? 1 : 0;
6dc54d91 3351
0d5b594f 3352 remote_notice_new_inferior (item->ptid, executing);
6dc54d91 3353
85ad3aaf 3354 info = get_private_info_ptid (item->ptid);
6dc54d91
PA
3355 info->core = item->core;
3356 info->extra = item->extra;
3357 item->extra = NULL;
79efa585
SM
3358 info->name = item->name;
3359 item->name = NULL;
f6327dcb
KB
3360 info->thread_handle = item->thread_handle;
3361 item->thread_handle = nullptr;
6dc54d91
PA
3362 }
3363 }
3364 }
3365
ab970af1
PA
3366 if (!got_list)
3367 {
3368 /* If no thread listing method is supported, then query whether
3369 each known thread is alive, one by one, with the T packet.
3370 If the target doesn't support threads at all, then this is a
3371 no-op. See remote_thread_alive. */
3372 prune_threads ();
3373 }
3374
6dc54d91 3375 do_cleanups (old_chain);
9d1f7ab2
MS
3376}
3377
802188a7 3378/*
9d1f7ab2
MS
3379 * Collect a descriptive string about the given thread.
3380 * The target may say anything it wants to about the thread
3381 * (typically info about its blocked / runnable state, name, etc.).
3382 * This string will appear in the info threads display.
802188a7 3383 *
9d1f7ab2
MS
3384 * Optional: targets are not required to implement this function.
3385 */
3386
7a114964 3387static const char *
c15906d8 3388remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3389{
d01949b6 3390 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3391 int result;
3392 int set;
3393 threadref id;
3394 struct gdb_ext_thread_info threadinfo;
23860348 3395 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3396 int n = 0; /* position in display_buf */
3397
5d93a237 3398 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3399 internal_error (__FILE__, __LINE__,
e2e0b3e5 3400 _("remote_threads_extra_info"));
9d1f7ab2 3401
60e569b9 3402 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3403 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3404 /* This is the main thread which was added by GDB. The remote
3405 server doesn't know about it. */
3406 return NULL;
3407
4082afcc 3408 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3409 {
3410 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3411
fe978cb0
PA
3412 if (info && info->priv)
3413 return info->priv->extra;
dc146f7c
VP
3414 else
3415 return NULL;
3416 }
3417
b80fafe3 3418 if (rs->use_threadextra_query)
9d1f7ab2 3419 {
82f73884
PA
3420 char *b = rs->buf;
3421 char *endb = rs->buf + get_remote_packet_size ();
3422
3423 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3424 b += strlen (b);
3425 write_ptid (b, endb, tp->ptid);
3426
2e9f7625 3427 putpkt (rs->buf);
6d820c5c 3428 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3429 if (rs->buf[0] != 0)
9d1f7ab2 3430 {
325fac50 3431 n = std::min (strlen (rs->buf) / 2, sizeof (display_buf));
2e9f7625 3432 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3433 display_buf [result] = '\0';
9d1f7ab2
MS
3434 return display_buf;
3435 }
0f71a2f6 3436 }
9d1f7ab2
MS
3437
3438 /* If the above query fails, fall back to the old method. */
b80fafe3 3439 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3440 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3441 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3442 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3443 if (remote_get_threadinfo (&id, set, &threadinfo))
3444 if (threadinfo.active)
0f71a2f6 3445 {
9d1f7ab2 3446 if (*threadinfo.shortname)
2bc416ba 3447 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3448 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3449 if (*threadinfo.display)
2bc416ba 3450 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3451 " State: %s,", threadinfo.display);
9d1f7ab2 3452 if (*threadinfo.more_display)
2bc416ba 3453 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3454 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3455
3456 if (n > 0)
c5aa993b 3457 {
23860348 3458 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3459 if (',' == display_buf[n-1])
3460 display_buf[n-1] = ' ';
3461 return display_buf;
c5aa993b 3462 }
0f71a2f6 3463 }
9d1f7ab2 3464 return NULL;
0f71a2f6 3465}
c906108c 3466\f
c5aa993b 3467
0fb4aa4b 3468static int
61fc905d 3469remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3470 struct static_tracepoint_marker *marker)
3471{
3472 struct remote_state *rs = get_remote_state ();
3473 char *p = rs->buf;
3474
bba74b36 3475 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3476 p += strlen (p);
3477 p += hexnumstr (p, addr);
3478 putpkt (rs->buf);
3479 getpkt (&rs->buf, &rs->buf_size, 0);
3480 p = rs->buf;
3481
3482 if (*p == 'E')
3483 error (_("Remote failure reply: %s"), p);
3484
3485 if (*p++ == 'm')
3486 {
256642e8 3487 parse_static_tracepoint_marker_definition (p, NULL, marker);
0fb4aa4b
PA
3488 return 1;
3489 }
3490
3491 return 0;
3492}
3493
0fb4aa4b 3494static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3495remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3496 const char *strid)
0fb4aa4b
PA
3497{
3498 struct remote_state *rs = get_remote_state ();
3499 VEC(static_tracepoint_marker_p) *markers = NULL;
3500 struct static_tracepoint_marker *marker = NULL;
3501 struct cleanup *old_chain;
256642e8 3502 const char *p;
0fb4aa4b
PA
3503
3504 /* Ask for a first packet of static tracepoint marker
3505 definition. */
3506 putpkt ("qTfSTM");
3507 getpkt (&rs->buf, &rs->buf_size, 0);
3508 p = rs->buf;
3509 if (*p == 'E')
3510 error (_("Remote failure reply: %s"), p);
3511
3512 old_chain = make_cleanup (free_current_marker, &marker);
3513
3514 while (*p++ == 'm')
3515 {
3516 if (marker == NULL)
3517 marker = XCNEW (struct static_tracepoint_marker);
3518
3519 do
3520 {
3521 parse_static_tracepoint_marker_definition (p, &p, marker);
3522
3523 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3524 {
3525 VEC_safe_push (static_tracepoint_marker_p,
3526 markers, marker);
3527 marker = NULL;
3528 }
3529 else
3530 {
3531 release_static_tracepoint_marker (marker);
3532 memset (marker, 0, sizeof (*marker));
3533 }
3534 }
3535 while (*p++ == ','); /* comma-separated list */
3536 /* Ask for another packet of static tracepoint definition. */
3537 putpkt ("qTsSTM");
3538 getpkt (&rs->buf, &rs->buf_size, 0);
3539 p = rs->buf;
3540 }
3541
3542 do_cleanups (old_chain);
3543 return markers;
3544}
3545
3546\f
10760264
JB
3547/* Implement the to_get_ada_task_ptid function for the remote targets. */
3548
3549static ptid_t
1e6b91a4 3550remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3551{
ba348170 3552 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3553}
3554\f
3555
24b06219 3556/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3557
3558static void
fba45db2 3559extended_remote_restart (void)
c906108c 3560{
d01949b6 3561 struct remote_state *rs = get_remote_state ();
c906108c
SS
3562
3563 /* Send the restart command; for reasons I don't understand the
3564 remote side really expects a number after the "R". */
ea9c271d 3565 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3566 putpkt (rs->buf);
c906108c 3567
ad9a8f3f 3568 remote_fileio_reset ();
c906108c
SS
3569}
3570\f
3571/* Clean up connection to a remote debugger. */
3572
c906108c 3573static void
de90e03d 3574remote_close (struct target_ops *self)
c906108c 3575{
5d93a237
TT
3576 struct remote_state *rs = get_remote_state ();
3577
3578 if (rs->remote_desc == NULL)
d3fd5342
PA
3579 return; /* already closed */
3580
048094ac 3581 /* Make sure we leave stdin registered in the event loop. */
e3594fd1 3582 remote_terminal_ours (self);
ce5ce7ed 3583
5d93a237
TT
3584 serial_close (rs->remote_desc);
3585 rs->remote_desc = NULL;
ce5ce7ed
PA
3586
3587 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3588 of all the inferiors and their threads we were controlling.
3589 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3590 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3591 inferior_ptid = null_ptid;
f67fd822 3592 discard_all_inferiors ();
ce5ce7ed 3593
f48ff2a7
YQ
3594 /* We are closing the remote target, so we should discard
3595 everything of this target. */
bcc75809 3596 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3597
3598 if (remote_async_inferior_event_token)
3599 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3600
5965e028 3601 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3602
3603 trace_reset_local_state ();
c906108c
SS
3604}
3605
23860348 3606/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3607
3608static void
fba45db2 3609get_offsets (void)
c906108c 3610{
d01949b6 3611 struct remote_state *rs = get_remote_state ();
2e9f7625 3612 char *buf;
085dd6e6 3613 char *ptr;
31d99776
DJ
3614 int lose, num_segments = 0, do_sections, do_segments;
3615 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3616 struct section_offsets *offs;
31d99776
DJ
3617 struct symfile_segment_data *data;
3618
3619 if (symfile_objfile == NULL)
3620 return;
c906108c
SS
3621
3622 putpkt ("qOffsets");
6d820c5c 3623 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3624 buf = rs->buf;
c906108c
SS
3625
3626 if (buf[0] == '\000')
3627 return; /* Return silently. Stub doesn't support
23860348 3628 this command. */
c906108c
SS
3629 if (buf[0] == 'E')
3630 {
8a3fe4f8 3631 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3632 return;
3633 }
3634
3635 /* Pick up each field in turn. This used to be done with scanf, but
3636 scanf will make trouble if CORE_ADDR size doesn't match
3637 conversion directives correctly. The following code will work
3638 with any size of CORE_ADDR. */
3639 text_addr = data_addr = bss_addr = 0;
3640 ptr = buf;
3641 lose = 0;
3642
61012eef 3643 if (startswith (ptr, "Text="))
c906108c
SS
3644 {
3645 ptr += 5;
3646 /* Don't use strtol, could lose on big values. */
3647 while (*ptr && *ptr != ';')
3648 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3649
61012eef 3650 if (startswith (ptr, ";Data="))
31d99776
DJ
3651 {
3652 ptr += 6;
3653 while (*ptr && *ptr != ';')
3654 data_addr = (data_addr << 4) + fromhex (*ptr++);
3655 }
3656 else
3657 lose = 1;
3658
61012eef 3659 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3660 {
3661 ptr += 5;
3662 while (*ptr && *ptr != ';')
3663 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3664
31d99776
DJ
3665 if (bss_addr != data_addr)
3666 warning (_("Target reported unsupported offsets: %s"), buf);
3667 }
3668 else
3669 lose = 1;
3670 }
61012eef 3671 else if (startswith (ptr, "TextSeg="))
c906108c 3672 {
31d99776
DJ
3673 ptr += 8;
3674 /* Don't use strtol, could lose on big values. */
c906108c 3675 while (*ptr && *ptr != ';')
31d99776
DJ
3676 text_addr = (text_addr << 4) + fromhex (*ptr++);
3677 num_segments = 1;
3678
61012eef 3679 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3680 {
3681 ptr += 9;
3682 while (*ptr && *ptr != ';')
3683 data_addr = (data_addr << 4) + fromhex (*ptr++);
3684 num_segments++;
3685 }
c906108c
SS
3686 }
3687 else
3688 lose = 1;
3689
3690 if (lose)
8a3fe4f8 3691 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3692 else if (*ptr != '\0')
3693 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3694
802188a7 3695 offs = ((struct section_offsets *)
a39a16c4 3696 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3697 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3698 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3699
31d99776
DJ
3700 data = get_symfile_segment_data (symfile_objfile->obfd);
3701 do_segments = (data != NULL);
3702 do_sections = num_segments == 0;
c906108c 3703
28c32713 3704 if (num_segments > 0)
31d99776 3705 {
31d99776
DJ
3706 segments[0] = text_addr;
3707 segments[1] = data_addr;
3708 }
28c32713
JB
3709 /* If we have two segments, we can still try to relocate everything
3710 by assuming that the .text and .data offsets apply to the whole
3711 text and data segments. Convert the offsets given in the packet
3712 to base addresses for symfile_map_offsets_to_segments. */
3713 else if (data && data->num_segments == 2)
3714 {
3715 segments[0] = data->segment_bases[0] + text_addr;
3716 segments[1] = data->segment_bases[1] + data_addr;
3717 num_segments = 2;
3718 }
8d385431
DJ
3719 /* If the object file has only one segment, assume that it is text
3720 rather than data; main programs with no writable data are rare,
3721 but programs with no code are useless. Of course the code might
3722 have ended up in the data segment... to detect that we would need
3723 the permissions here. */
3724 else if (data && data->num_segments == 1)
3725 {
3726 segments[0] = data->segment_bases[0] + text_addr;
3727 num_segments = 1;
3728 }
28c32713
JB
3729 /* There's no way to relocate by segment. */
3730 else
3731 do_segments = 0;
31d99776
DJ
3732
3733 if (do_segments)
3734 {
3735 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3736 offs, num_segments, segments);
3737
3738 if (ret == 0 && !do_sections)
3e43a32a
MS
3739 error (_("Can not handle qOffsets TextSeg "
3740 "response with this symbol file"));
31d99776
DJ
3741
3742 if (ret > 0)
3743 do_sections = 0;
3744 }
c906108c 3745
9ef895d6
DJ
3746 if (data)
3747 free_symfile_segment_data (data);
31d99776
DJ
3748
3749 if (do_sections)
3750 {
3751 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3752
3e43a32a
MS
3753 /* This is a temporary kludge to force data and bss to use the
3754 same offsets because that's what nlmconv does now. The real
3755 solution requires changes to the stub and remote.c that I
3756 don't have time to do right now. */
31d99776
DJ
3757
3758 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3759 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3760 }
c906108c
SS
3761
3762 objfile_relocate (symfile_objfile, offs);
3763}
3764
9a7071a8
JB
3765/* Send interrupt_sequence to remote target. */
3766static void
eeae04df 3767send_interrupt_sequence (void)
9a7071a8 3768{
5d93a237
TT
3769 struct remote_state *rs = get_remote_state ();
3770
9a7071a8 3771 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3772 remote_serial_write ("\x03", 1);
9a7071a8 3773 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3774 serial_send_break (rs->remote_desc);
9a7071a8
JB
3775 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3776 {
5d93a237 3777 serial_send_break (rs->remote_desc);
c33e31fd 3778 remote_serial_write ("g", 1);
9a7071a8
JB
3779 }
3780 else
3781 internal_error (__FILE__, __LINE__,
3782 _("Invalid value for interrupt_sequence_mode: %s."),
3783 interrupt_sequence_mode);
3784}
3785
3405876a
PA
3786
3787/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3788 and extract the PTID. Returns NULL_PTID if not found. */
3789
3790static ptid_t
3791stop_reply_extract_thread (char *stop_reply)
3792{
3793 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3794 {
256642e8 3795 const char *p;
3405876a
PA
3796
3797 /* Txx r:val ; r:val (...) */
3798 p = &stop_reply[3];
3799
3800 /* Look for "register" named "thread". */
3801 while (*p != '\0')
3802 {
256642e8 3803 const char *p1;
3405876a
PA
3804
3805 p1 = strchr (p, ':');
3806 if (p1 == NULL)
3807 return null_ptid;
3808
3809 if (strncmp (p, "thread", p1 - p) == 0)
3810 return read_ptid (++p1, &p);
3811
3812 p1 = strchr (p, ';');
3813 if (p1 == NULL)
3814 return null_ptid;
3815 p1++;
3816
3817 p = p1;
3818 }
3819 }
3820
3821 return null_ptid;
3822}
3823
b7ea362b
PA
3824/* Determine the remote side's current thread. If we have a stop
3825 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3826 "thread" register we can extract the current thread from. If not,
3827 ask the remote which is the current thread with qC. The former
3828 method avoids a roundtrip. */
3829
3830static ptid_t
3831get_current_thread (char *wait_status)
3832{
6a49a997 3833 ptid_t ptid = null_ptid;
b7ea362b
PA
3834
3835 /* Note we don't use remote_parse_stop_reply as that makes use of
3836 the target architecture, which we haven't yet fully determined at
3837 this point. */
3838 if (wait_status != NULL)
3839 ptid = stop_reply_extract_thread (wait_status);
3840 if (ptid_equal (ptid, null_ptid))
3841 ptid = remote_current_thread (inferior_ptid);
3842
3843 return ptid;
3844}
3845
49c62f2e
PA
3846/* Query the remote target for which is the current thread/process,
3847 add it to our tables, and update INFERIOR_PTID. The caller is
3848 responsible for setting the state such that the remote end is ready
3405876a
PA
3849 to return the current thread.
3850
3851 This function is called after handling the '?' or 'vRun' packets,
3852 whose response is a stop reply from which we can also try
3853 extracting the thread. If the target doesn't support the explicit
3854 qC query, we infer the current thread from that stop reply, passed
3855 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3856
3857static void
3405876a 3858add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3859{
3860 struct remote_state *rs = get_remote_state ();
3861 int fake_pid_p = 0;
49c62f2e
PA
3862
3863 inferior_ptid = null_ptid;
3864
b7ea362b 3865 /* Now, if we have thread information, update inferior_ptid. */
87215ad1 3866 ptid_t curr_ptid = get_current_thread (wait_status);
3405876a 3867
87215ad1 3868 if (curr_ptid != null_ptid)
49c62f2e
PA
3869 {
3870 if (!remote_multi_process_p (rs))
3871 fake_pid_p = 1;
49c62f2e
PA
3872 }
3873 else
3874 {
3875 /* Without this, some commands which require an active target
3876 (such as kill) won't work. This variable serves (at least)
3877 double duty as both the pid of the target process (if it has
3878 such), and as a flag indicating that a target is active. */
87215ad1 3879 curr_ptid = magic_null_ptid;
49c62f2e
PA
3880 fake_pid_p = 1;
3881 }
3882
87215ad1 3883 remote_add_inferior (fake_pid_p, ptid_get_pid (curr_ptid), -1, 1);
49c62f2e 3884
87215ad1
SDJ
3885 /* Add the main thread and switch to it. Don't try reading
3886 registers yet, since we haven't fetched the target description
3887 yet. */
3888 thread_info *tp = add_thread_silent (curr_ptid);
3889 switch_to_thread_no_regs (tp);
49c62f2e
PA
3890}
3891
6efcd9a8
PA
3892/* Print info about a thread that was found already stopped on
3893 connection. */
3894
3895static void
3896print_one_stopped_thread (struct thread_info *thread)
3897{
3898 struct target_waitstatus *ws = &thread->suspend.waitstatus;
3899
3900 switch_to_thread (thread->ptid);
3901 stop_pc = get_frame_pc (get_current_frame ());
3902 set_current_sal_from_frame (get_current_frame ());
3903
3904 thread->suspend.waitstatus_pending_p = 0;
3905
3906 if (ws->kind == TARGET_WAITKIND_STOPPED)
3907 {
3908 enum gdb_signal sig = ws->value.sig;
3909
3910 if (signal_print_state (sig))
3911 observer_notify_signal_received (sig);
3912 }
3913 observer_notify_normal_stop (NULL, 1);
3914}
3915
221e1a37
PA
3916/* Process all initial stop replies the remote side sent in response
3917 to the ? packet. These indicate threads that were already stopped
3918 on initial connection. We mark these threads as stopped and print
3919 their current frame before giving the user the prompt. */
3920
3921static void
6efcd9a8 3922process_initial_stop_replies (int from_tty)
221e1a37
PA
3923{
3924 int pending_stop_replies = stop_reply_queue_length ();
6efcd9a8
PA
3925 struct inferior *inf;
3926 struct thread_info *thread;
3927 struct thread_info *selected = NULL;
3928 struct thread_info *lowest_stopped = NULL;
3929 struct thread_info *first = NULL;
221e1a37
PA
3930
3931 /* Consume the initial pending events. */
3932 while (pending_stop_replies-- > 0)
3933 {
3934 ptid_t waiton_ptid = minus_one_ptid;
3935 ptid_t event_ptid;
3936 struct target_waitstatus ws;
3937 int ignore_event = 0;
6efcd9a8 3938 struct thread_info *thread;
221e1a37
PA
3939
3940 memset (&ws, 0, sizeof (ws));
3941 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
3942 if (remote_debug)
3943 print_target_wait_results (waiton_ptid, event_ptid, &ws);
3944
3945 switch (ws.kind)
3946 {
3947 case TARGET_WAITKIND_IGNORE:
3948 case TARGET_WAITKIND_NO_RESUMED:
3949 case TARGET_WAITKIND_SIGNALLED:
3950 case TARGET_WAITKIND_EXITED:
3951 /* We shouldn't see these, but if we do, just ignore. */
3952 if (remote_debug)
3953 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
3954 ignore_event = 1;
3955 break;
3956
3957 case TARGET_WAITKIND_EXECD:
3958 xfree (ws.value.execd_pathname);
3959 break;
3960 default:
3961 break;
3962 }
3963
3964 if (ignore_event)
3965 continue;
3966
6efcd9a8 3967 thread = find_thread_ptid (event_ptid);
221e1a37
PA
3968
3969 if (ws.kind == TARGET_WAITKIND_STOPPED)
3970 {
3971 enum gdb_signal sig = ws.value.sig;
3972
3973 /* Stubs traditionally report SIGTRAP as initial signal,
3974 instead of signal 0. Suppress it. */
3975 if (sig == GDB_SIGNAL_TRAP)
3976 sig = GDB_SIGNAL_0;
6efcd9a8
PA
3977 thread->suspend.stop_signal = sig;
3978 ws.value.sig = sig;
3979 }
221e1a37 3980
6efcd9a8
PA
3981 thread->suspend.waitstatus = ws;
3982
3983 if (ws.kind != TARGET_WAITKIND_STOPPED
3984 || ws.value.sig != GDB_SIGNAL_0)
3985 thread->suspend.waitstatus_pending_p = 1;
3986
3987 set_executing (event_ptid, 0);
3988 set_running (event_ptid, 0);
85ad3aaf 3989 thread->priv->vcont_resumed = 0;
6efcd9a8
PA
3990 }
3991
3992 /* "Notice" the new inferiors before anything related to
3993 registers/memory. */
3994 ALL_INFERIORS (inf)
3995 {
3996 if (inf->pid == 0)
3997 continue;
3998
3999 inf->needs_setup = 1;
4000
4001 if (non_stop)
4002 {
4003 thread = any_live_thread_of_process (inf->pid);
4004 notice_new_inferior (thread->ptid,
4005 thread->state == THREAD_RUNNING,
4006 from_tty);
4007 }
4008 }
4009
4010 /* If all-stop on top of non-stop, pause all threads. Note this
4011 records the threads' stop pc, so must be done after "noticing"
4012 the inferiors. */
4013 if (!non_stop)
4014 {
4015 stop_all_threads ();
4016
4017 /* If all threads of an inferior were already stopped, we
4018 haven't setup the inferior yet. */
4019 ALL_INFERIORS (inf)
4020 {
4021 if (inf->pid == 0)
4022 continue;
221e1a37 4023
6efcd9a8
PA
4024 if (inf->needs_setup)
4025 {
4026 thread = any_live_thread_of_process (inf->pid);
4027 switch_to_thread_no_regs (thread);
4028 setup_inferior (0);
4029 }
4030 }
221e1a37 4031 }
6efcd9a8
PA
4032
4033 /* Now go over all threads that are stopped, and print their current
4034 frame. If all-stop, then if there's a signalled thread, pick
4035 that as current. */
4036 ALL_NON_EXITED_THREADS (thread)
4037 {
6efcd9a8
PA
4038 if (first == NULL)
4039 first = thread;
4040
4041 if (!non_stop)
4042 set_running (thread->ptid, 0);
4043 else if (thread->state != THREAD_STOPPED)
4044 continue;
4045
6efcd9a8
PA
4046 if (selected == NULL
4047 && thread->suspend.waitstatus_pending_p)
4048 selected = thread;
4049
5d5658a1
PA
4050 if (lowest_stopped == NULL
4051 || thread->inf->num < lowest_stopped->inf->num
4052 || thread->per_inf_num < lowest_stopped->per_inf_num)
6efcd9a8
PA
4053 lowest_stopped = thread;
4054
4055 if (non_stop)
4056 print_one_stopped_thread (thread);
4057 }
4058
4059 /* In all-stop, we only print the status of one thread, and leave
4060 others with their status pending. */
4061 if (!non_stop)
4062 {
4063 thread = selected;
4064 if (thread == NULL)
4065 thread = lowest_stopped;
4066 if (thread == NULL)
4067 thread = first;
4068
4069 print_one_stopped_thread (thread);
4070 }
4071
4072 /* For "info program". */
4073 thread = inferior_thread ();
4074 if (thread->state == THREAD_STOPPED)
4075 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
221e1a37
PA
4076}
4077
048094ac
PA
4078/* Start the remote connection and sync state. */
4079
9cbc821d 4080static void
04bd08de 4081remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 4082{
c8d104ad
PA
4083 struct remote_state *rs = get_remote_state ();
4084 struct packet_config *noack_config;
2d717e4f 4085 char *wait_status = NULL;
8621d6a9 4086
048094ac
PA
4087 /* Signal other parts that we're going through the initial setup,
4088 and so things may not be stable yet. E.g., we don't try to
4089 install tracepoints until we've relocated symbols. Also, a
4090 Ctrl-C before we're connected and synced up can't interrupt the
4091 target. Instead, it offers to drop the (potentially wedged)
4092 connection. */
4093 rs->starting_up = 1;
4094
522002f9 4095 QUIT;
c906108c 4096
9a7071a8
JB
4097 if (interrupt_on_connect)
4098 send_interrupt_sequence ();
4099
57e12211 4100 /* Ack any packet which the remote side has already sent. */
048094ac 4101 remote_serial_write ("+", 1);
1e51243a 4102
c8d104ad
PA
4103 /* The first packet we send to the target is the optional "supported
4104 packets" request. If the target can answer this, it will tell us
4105 which later probes to skip. */
4106 remote_query_supported ();
4107
d914c394 4108 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 4109 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 4110 remote_set_permissions (target);
d914c394 4111
57809e5e
JK
4112 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4113 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4114 as a reply to known packet. For packet "vFile:setfs:" it is an
4115 invalid reply and GDB would return error in
4116 remote_hostio_set_filesystem, making remote files access impossible.
4117 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4118 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4119 {
4120 const char v_mustreplyempty[] = "vMustReplyEmpty";
4121
4122 putpkt (v_mustreplyempty);
4123 getpkt (&rs->buf, &rs->buf_size, 0);
4124 if (strcmp (rs->buf, "OK") == 0)
4125 remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
4126 else if (strcmp (rs->buf, "") != 0)
4127 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
4128 rs->buf);
4129 }
4130
c8d104ad
PA
4131 /* Next, we possibly activate noack mode.
4132
4133 If the QStartNoAckMode packet configuration is set to AUTO,
4134 enable noack mode if the stub reported a wish for it with
4135 qSupported.
4136
4137 If set to TRUE, then enable noack mode even if the stub didn't
4138 report it in qSupported. If the stub doesn't reply OK, the
4139 session ends with an error.
4140
4141 If FALSE, then don't activate noack mode, regardless of what the
4142 stub claimed should be the default with qSupported. */
4143
4144 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 4145 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
4146 {
4147 putpkt ("QStartNoAckMode");
4148 getpkt (&rs->buf, &rs->buf_size, 0);
4149 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
4150 rs->noack_mode = 1;
4151 }
4152
04bd08de 4153 if (extended_p)
5fe04517
PA
4154 {
4155 /* Tell the remote that we are using the extended protocol. */
4156 putpkt ("!");
4157 getpkt (&rs->buf, &rs->buf_size, 0);
4158 }
4159
9b224c5e
PA
4160 /* Let the target know which signals it is allowed to pass down to
4161 the program. */
4162 update_signals_program_target ();
4163
d962ef82
DJ
4164 /* Next, if the target can specify a description, read it. We do
4165 this before anything involving memory or registers. */
4166 target_find_description ();
4167
6c95b8df
PA
4168 /* Next, now that we know something about the target, update the
4169 address spaces in the program spaces. */
4170 update_address_spaces ();
4171
50c71eaf
PA
4172 /* On OSs where the list of libraries is global to all
4173 processes, we fetch them early. */
f5656ead 4174 if (gdbarch_has_global_solist (target_gdbarch ()))
e696b3ad 4175 solib_add (NULL, from_tty, auto_solib_add);
50c71eaf 4176
6efcd9a8 4177 if (target_is_non_stop_p ())
74531fed 4178 {
4082afcc 4179 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
4180 error (_("Non-stop mode requested, but remote "
4181 "does not support non-stop"));
74531fed
PA
4182
4183 putpkt ("QNonStop:1");
4184 getpkt (&rs->buf, &rs->buf_size, 0);
4185
4186 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4187 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
4188
4189 /* Find about threads and processes the stub is already
4190 controlling. We default to adding them in the running state.
4191 The '?' query below will then tell us about which threads are
4192 stopped. */
e8032dde 4193 remote_update_thread_list (target);
74531fed 4194 }
4082afcc 4195 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
4196 {
4197 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 4198 Request it explicitly. */
74531fed
PA
4199 putpkt ("QNonStop:0");
4200 getpkt (&rs->buf, &rs->buf_size, 0);
4201
4202 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4203 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
4204 }
4205
a0743c90
YQ
4206 /* Upload TSVs regardless of whether the target is running or not. The
4207 remote stub, such as GDBserver, may have some predefined or builtin
4208 TSVs, even if the target is not running. */
8bd200f1 4209 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
4210 {
4211 struct uploaded_tsv *uploaded_tsvs = NULL;
4212
181e3713 4213 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
4214 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4215 }
4216
2d717e4f
DJ
4217 /* Check whether the target is running now. */
4218 putpkt ("?");
4219 getpkt (&rs->buf, &rs->buf_size, 0);
4220
6efcd9a8 4221 if (!target_is_non_stop_p ())
2d717e4f 4222 {
74531fed 4223 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 4224 {
04bd08de 4225 if (!extended_p)
74531fed 4226 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
4227
4228 /* We're connected, but not running. Drop out before we
4229 call start_remote. */
e278ad5b 4230 rs->starting_up = 0;
c35b1492 4231 return;
2d717e4f
DJ
4232 }
4233 else
74531fed 4234 {
74531fed 4235 /* Save the reply for later. */
224c3ddb 4236 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
4237 strcpy (wait_status, rs->buf);
4238 }
4239
b7ea362b 4240 /* Fetch thread list. */
e8032dde 4241 target_update_thread_list ();
b7ea362b 4242
74531fed
PA
4243 /* Let the stub know that we want it to return the thread. */
4244 set_continue_thread (minus_one_ptid);
4245
b7ea362b
PA
4246 if (thread_count () == 0)
4247 {
4248 /* Target has no concept of threads at all. GDB treats
4249 non-threaded target as single-threaded; add a main
4250 thread. */
4251 add_current_inferior_and_thread (wait_status);
4252 }
4253 else
4254 {
4255 /* We have thread information; select the thread the target
4256 says should be current. If we're reconnecting to a
4257 multi-threaded program, this will ideally be the thread
4258 that last reported an event before GDB disconnected. */
4259 inferior_ptid = get_current_thread (wait_status);
4260 if (ptid_equal (inferior_ptid, null_ptid))
4261 {
4262 /* Odd... The target was able to list threads, but not
4263 tell us which thread was current (no "thread"
4264 register in T stop reply?). Just pick the first
4265 thread in the thread list then. */
c9f35b34
KB
4266
4267 if (remote_debug)
4268 fprintf_unfiltered (gdb_stdlog,
4269 "warning: couldn't determine remote "
4270 "current thread; picking first in list.\n");
4271
b7ea362b
PA
4272 inferior_ptid = thread_list->ptid;
4273 }
4274 }
74531fed 4275
6e586cc5
YQ
4276 /* init_wait_for_inferior should be called before get_offsets in order
4277 to manage `inserted' flag in bp loc in a correct state.
4278 breakpoint_init_inferior, called from init_wait_for_inferior, set
4279 `inserted' flag to 0, while before breakpoint_re_set, called from
4280 start_remote, set `inserted' flag to 1. In the initialization of
4281 inferior, breakpoint_init_inferior should be called first, and then
4282 breakpoint_re_set can be called. If this order is broken, state of
4283 `inserted' flag is wrong, and cause some problems on breakpoint
4284 manipulation. */
4285 init_wait_for_inferior ();
4286
74531fed
PA
4287 get_offsets (); /* Get text, data & bss offsets. */
4288
d962ef82
DJ
4289 /* If we could not find a description using qXfer, and we know
4290 how to do it some other way, try again. This is not
4291 supported for non-stop; it could be, but it is tricky if
4292 there are no stopped threads when we connect. */
04bd08de 4293 if (remote_read_description_p (target)
f5656ead 4294 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
4295 {
4296 target_clear_description ();
4297 target_find_description ();
4298 }
4299
74531fed
PA
4300 /* Use the previously fetched status. */
4301 gdb_assert (wait_status != NULL);
4302 strcpy (rs->buf, wait_status);
4303 rs->cached_wait_status = 1;
4304
04bd08de 4305 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
4306 }
4307 else
4308 {
68c97600
PA
4309 /* Clear WFI global state. Do this before finding about new
4310 threads and inferiors, and setting the current inferior.
4311 Otherwise we would clear the proceed status of the current
4312 inferior when we want its stop_soon state to be preserved
4313 (see notice_new_inferior). */
4314 init_wait_for_inferior ();
4315
74531fed
PA
4316 /* In non-stop, we will either get an "OK", meaning that there
4317 are no stopped threads at this time; or, a regular stop
4318 reply. In the latter case, there may be more than one thread
4319 stopped --- we pull them all out using the vStopped
4320 mechanism. */
4321 if (strcmp (rs->buf, "OK") != 0)
4322 {
722247f1 4323 struct notif_client *notif = &notif_client_stop;
2d717e4f 4324
722247f1
YQ
4325 /* remote_notif_get_pending_replies acks this one, and gets
4326 the rest out. */
f48ff2a7 4327 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
4328 = remote_notif_parse (notif, rs->buf);
4329 remote_notif_get_pending_events (notif);
74531fed 4330 }
2d717e4f 4331
74531fed
PA
4332 if (thread_count () == 0)
4333 {
04bd08de 4334 if (!extended_p)
74531fed 4335 error (_("The target is not running (try extended-remote?)"));
82f73884 4336
c35b1492
PA
4337 /* We're connected, but not running. Drop out before we
4338 call start_remote. */
e278ad5b 4339 rs->starting_up = 0;
c35b1492
PA
4340 return;
4341 }
74531fed 4342
74531fed
PA
4343 /* In non-stop mode, any cached wait status will be stored in
4344 the stop reply queue. */
4345 gdb_assert (wait_status == NULL);
f0223081 4346
2455069d 4347 /* Report all signals during attach/startup. */
94bedb42 4348 remote_pass_signals (target, 0, NULL);
221e1a37
PA
4349
4350 /* If there are already stopped threads, mark them stopped and
4351 report their stops before giving the prompt to the user. */
6efcd9a8 4352 process_initial_stop_replies (from_tty);
221e1a37
PA
4353
4354 if (target_can_async_p ())
4355 target_async (1);
74531fed 4356 }
c8d104ad 4357
c8d104ad
PA
4358 /* If we connected to a live target, do some additional setup. */
4359 if (target_has_execution)
4360 {
f4ccffad 4361 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 4362 remote_check_symbols ();
c8d104ad 4363 }
50c71eaf 4364
d5551862
SS
4365 /* Possibly the target has been engaged in a trace run started
4366 previously; find out where things are at. */
8bd200f1 4367 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 4368 {
00bf0b85 4369 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4370
00bf0b85
SS
4371 if (current_trace_status ()->running)
4372 printf_filtered (_("Trace is already running on the target.\n"));
4373
ab6617cc 4374 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
4375
4376 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4377 }
4378
c0272db5
TW
4379 /* Possibly the target has been engaged in a btrace record started
4380 previously; find out where things are at. */
4381 remote_btrace_maybe_reopen ();
4382
1e51243a
PA
4383 /* The thread and inferior lists are now synchronized with the
4384 target, our symbols have been relocated, and we're merged the
4385 target's tracepoints with ours. We're done with basic start
4386 up. */
4387 rs->starting_up = 0;
4388
a25a5a45
PA
4389 /* Maybe breakpoints are global and need to be inserted now. */
4390 if (breakpoints_should_be_inserted_now ())
50c71eaf 4391 insert_breakpoints ();
c906108c
SS
4392}
4393
4394/* Open a connection to a remote debugger.
4395 NAME is the filename used for communication. */
4396
4397static void
014f9477 4398remote_open (const char *name, int from_tty)
c906108c 4399{
75c99385 4400 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
4401}
4402
c906108c
SS
4403/* Open a connection to a remote debugger using the extended
4404 remote gdb protocol. NAME is the filename used for communication. */
4405
4406static void
014f9477 4407extended_remote_open (const char *name, int from_tty)
c906108c 4408{
75c99385 4409 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
4410}
4411
ca4f7f8b
PA
4412/* Reset all packets back to "unknown support". Called when opening a
4413 new connection to a remote target. */
c906108c 4414
d471ea57 4415static void
ca4f7f8b 4416reset_all_packet_configs_support (void)
d471ea57
AC
4417{
4418 int i;
a744cf53 4419
444abaca 4420 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4421 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4422}
4423
ca4f7f8b
PA
4424/* Initialize all packet configs. */
4425
4426static void
4427init_all_packet_configs (void)
4428{
4429 int i;
4430
4431 for (i = 0; i < PACKET_MAX; i++)
4432 {
4433 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4434 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4435 }
4436}
4437
23860348 4438/* Symbol look-up. */
dc8acb97
MS
4439
4440static void
36d25514 4441remote_check_symbols (void)
dc8acb97 4442{
d01949b6 4443 struct remote_state *rs = get_remote_state ();
dc8acb97 4444 char *msg, *reply, *tmp;
dc8acb97 4445 int end;
28170b88 4446 long reply_size;
a5c0808e 4447 struct cleanup *old_chain;
dc8acb97 4448
63154eca
PA
4449 /* The remote side has no concept of inferiors that aren't running
4450 yet, it only knows about running processes. If we're connected
4451 but our current inferior is not running, we should not invite the
4452 remote target to request symbol lookups related to its
4453 (unrelated) current process. */
4454 if (!target_has_execution)
4455 return;
4456
4082afcc 4457 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4458 return;
4459
63154eca
PA
4460 /* Make sure the remote is pointing at the right process. Note
4461 there's no way to select "no process". */
3c9c4b83
PA
4462 set_general_process ();
4463
6d820c5c
DJ
4464 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4465 because we need both at the same time. */
224c3ddb 4466 msg = (char *) xmalloc (get_remote_packet_size ());
a5c0808e 4467 old_chain = make_cleanup (xfree, msg);
28170b88
MK
4468 reply = (char *) xmalloc (get_remote_packet_size ());
4469 make_cleanup (free_current_contents, &reply);
4470 reply_size = get_remote_packet_size ();
6d820c5c 4471
23860348 4472 /* Invite target to request symbol lookups. */
dc8acb97
MS
4473
4474 putpkt ("qSymbol::");
28170b88
MK
4475 getpkt (&reply, &reply_size, 0);
4476 packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97 4477
61012eef 4478 while (startswith (reply, "qSymbol:"))
dc8acb97 4479 {
77e371c0
TT
4480 struct bound_minimal_symbol sym;
4481
dc8acb97 4482 tmp = &reply[8];
cfd77fa1 4483 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4484 msg[end] = '\0';
4485 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4486 if (sym.minsym == NULL)
ea9c271d 4487 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4488 else
2bbe3cc1 4489 {
f5656ead 4490 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4491 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4492
4493 /* If this is a function address, return the start of code
4494 instead of any data function descriptor. */
f5656ead 4495 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
4496 sym_addr,
4497 &current_target);
4498
4499 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4500 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4501 }
4502
dc8acb97 4503 putpkt (msg);
28170b88 4504 getpkt (&reply, &reply_size, 0);
dc8acb97 4505 }
a5c0808e
PA
4506
4507 do_cleanups (old_chain);
dc8acb97
MS
4508}
4509
9db8d71f 4510static struct serial *
baa336ce 4511remote_serial_open (const char *name)
9db8d71f
DJ
4512{
4513 static int udp_warning = 0;
4514
4515 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4516 of in ser-tcp.c, because it is the remote protocol assuming that the
4517 serial connection is reliable and not the serial connection promising
4518 to be. */
61012eef 4519 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4520 {
3e43a32a
MS
4521 warning (_("The remote protocol may be unreliable over UDP.\n"
4522 "Some events may be lost, rendering further debugging "
4523 "impossible."));
9db8d71f
DJ
4524 udp_warning = 1;
4525 }
4526
4527 return serial_open (name);
4528}
4529
d914c394
SS
4530/* Inform the target of our permission settings. The permission flags
4531 work without this, but if the target knows the settings, it can do
4532 a couple things. First, it can add its own check, to catch cases
4533 that somehow manage to get by the permissions checks in target
4534 methods. Second, if the target is wired to disallow particular
4535 settings (for instance, a system in the field that is not set up to
4536 be able to stop at a breakpoint), it can object to any unavailable
4537 permissions. */
4538
4539void
c378d69d 4540remote_set_permissions (struct target_ops *self)
d914c394
SS
4541{
4542 struct remote_state *rs = get_remote_state ();
4543
bba74b36
YQ
4544 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4545 "WriteReg:%x;WriteMem:%x;"
4546 "InsertBreak:%x;InsertTrace:%x;"
4547 "InsertFastTrace:%x;Stop:%x",
4548 may_write_registers, may_write_memory,
4549 may_insert_breakpoints, may_insert_tracepoints,
4550 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4551 putpkt (rs->buf);
4552 getpkt (&rs->buf, &rs->buf_size, 0);
4553
4554 /* If the target didn't like the packet, warn the user. Do not try
4555 to undo the user's settings, that would just be maddening. */
4556 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4557 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4558}
4559
be2a5f71
DJ
4560/* This type describes each known response to the qSupported
4561 packet. */
4562struct protocol_feature
4563{
4564 /* The name of this protocol feature. */
4565 const char *name;
4566
4567 /* The default for this protocol feature. */
4568 enum packet_support default_support;
4569
4570 /* The function to call when this feature is reported, or after
4571 qSupported processing if the feature is not supported.
4572 The first argument points to this structure. The second
4573 argument indicates whether the packet requested support be
4574 enabled, disabled, or probed (or the default, if this function
4575 is being called at the end of processing and this feature was
4576 not reported). The third argument may be NULL; if not NULL, it
4577 is a NUL-terminated string taken from the packet following
4578 this feature's name and an equals sign. */
4579 void (*func) (const struct protocol_feature *, enum packet_support,
4580 const char *);
4581
4582 /* The corresponding packet for this feature. Only used if
4583 FUNC is remote_supported_packet. */
4584 int packet;
4585};
4586
be2a5f71
DJ
4587static void
4588remote_supported_packet (const struct protocol_feature *feature,
4589 enum packet_support support,
4590 const char *argument)
4591{
4592 if (argument)
4593 {
4594 warning (_("Remote qSupported response supplied an unexpected value for"
4595 " \"%s\"."), feature->name);
4596 return;
4597 }
4598
4082afcc 4599 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4600}
be2a5f71
DJ
4601
4602static void
4603remote_packet_size (const struct protocol_feature *feature,
4604 enum packet_support support, const char *value)
4605{
4606 struct remote_state *rs = get_remote_state ();
4607
4608 int packet_size;
4609 char *value_end;
4610
4611 if (support != PACKET_ENABLE)
4612 return;
4613
4614 if (value == NULL || *value == '\0')
4615 {
4616 warning (_("Remote target reported \"%s\" without a size."),
4617 feature->name);
4618 return;
4619 }
4620
4621 errno = 0;
4622 packet_size = strtol (value, &value_end, 16);
4623 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4624 {
4625 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4626 feature->name, value);
4627 return;
4628 }
4629
be2a5f71
DJ
4630 /* Record the new maximum packet size. */
4631 rs->explicit_packet_size = packet_size;
4632}
4633
dc473cfb 4634static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4635 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4636 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4637 PACKET_qXfer_auxv },
c78fa86a
GB
4638 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4639 PACKET_qXfer_exec_file },
23181151
DJ
4640 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4641 PACKET_qXfer_features },
cfa9d6d9
DJ
4642 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4643 PACKET_qXfer_libraries },
2268b414
JK
4644 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4645 PACKET_qXfer_libraries_svr4 },
ced63ec0 4646 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4647 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4648 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4649 PACKET_qXfer_memory_map },
4de6483e
UW
4650 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4651 PACKET_qXfer_spu_read },
4652 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4653 PACKET_qXfer_spu_write },
07e059b5
VP
4654 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4655 PACKET_qXfer_osdata },
dc146f7c
VP
4656 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4657 PACKET_qXfer_threads },
b3b9301e
PA
4658 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4659 PACKET_qXfer_traceframe_info },
89be2091
DJ
4660 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4661 PACKET_QPassSignals },
82075af2
JS
4662 { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
4663 PACKET_QCatchSyscalls },
9b224c5e
PA
4664 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4665 PACKET_QProgramSignals },
bc3b087d
SDJ
4666 { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
4667 PACKET_QSetWorkingDir },
aefd8b33
SDJ
4668 { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
4669 PACKET_QStartupWithShell },
0a2dde4a
SDJ
4670 { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
4671 PACKET_QEnvironmentHexEncoded },
4672 { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
4673 PACKET_QEnvironmentReset },
4674 { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
4675 PACKET_QEnvironmentUnset },
a6f3e723
SL
4676 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4677 PACKET_QStartNoAckMode },
4082afcc
PA
4678 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4679 PACKET_multiprocess_feature },
4680 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4681 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4682 PACKET_qXfer_siginfo_read },
4683 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4684 PACKET_qXfer_siginfo_write },
4082afcc 4685 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4686 PACKET_ConditionalTracepoints },
4082afcc 4687 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4688 PACKET_ConditionalBreakpoints },
4082afcc 4689 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4690 PACKET_BreakpointCommands },
4082afcc 4691 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4692 PACKET_FastTracepoints },
4082afcc 4693 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4694 PACKET_StaticTracepoints },
4082afcc 4695 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4696 PACKET_InstallInTrace},
4082afcc
PA
4697 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4698 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4699 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4700 PACKET_bc },
4701 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4702 PACKET_bs },
409873ef
SS
4703 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4704 PACKET_TracepointSource },
d914c394
SS
4705 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4706 PACKET_QAllow },
4082afcc
PA
4707 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4708 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4709 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4710 PACKET_qXfer_fdpic },
169081d0
TG
4711 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4712 PACKET_qXfer_uib },
03583c20
UW
4713 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4714 PACKET_QDisableRandomization },
d1feda86 4715 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4716 { "QTBuffer:size", PACKET_DISABLE,
4717 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4718 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4719 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4720 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4721 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4722 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4723 PACKET_qXfer_btrace },
4724 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4725 PACKET_qXfer_btrace_conf },
4726 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4727 PACKET_Qbtrace_conf_bts_size },
4728 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4729 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4730 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4731 PACKET_fork_event_feature },
4732 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4733 PACKET_vfork_event_feature },
94585166
DB
4734 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4735 PACKET_exec_event_feature },
b20a6524 4736 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
750ce8d1 4737 PACKET_Qbtrace_conf_pt_size },
65706a29
PA
4738 { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
4739 { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
f2faf941 4740 { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
be2a5f71
DJ
4741};
4742
c8d5aac9
L
4743static char *remote_support_xml;
4744
4745/* Register string appended to "xmlRegisters=" in qSupported query. */
4746
4747void
6e39997a 4748register_remote_support_xml (const char *xml)
c8d5aac9
L
4749{
4750#if defined(HAVE_LIBEXPAT)
4751 if (remote_support_xml == NULL)
c4f7c687 4752 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4753 else
4754 {
4755 char *copy = xstrdup (remote_support_xml + 13);
4756 char *p = strtok (copy, ",");
4757
4758 do
4759 {
4760 if (strcmp (p, xml) == 0)
4761 {
4762 /* already there */
4763 xfree (copy);
4764 return;
4765 }
4766 }
4767 while ((p = strtok (NULL, ",")) != NULL);
4768 xfree (copy);
4769
94b0dee1
PA
4770 remote_support_xml = reconcat (remote_support_xml,
4771 remote_support_xml, ",", xml,
4772 (char *) NULL);
c8d5aac9
L
4773 }
4774#endif
4775}
4776
4777static char *
4778remote_query_supported_append (char *msg, const char *append)
4779{
4780 if (msg)
94b0dee1 4781 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4782 else
4783 return xstrdup (append);
4784}
4785
be2a5f71
DJ
4786static void
4787remote_query_supported (void)
4788{
4789 struct remote_state *rs = get_remote_state ();
4790 char *next;
4791 int i;
4792 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4793
4794 /* The packet support flags are handled differently for this packet
4795 than for most others. We treat an error, a disabled packet, and
4796 an empty response identically: any features which must be reported
4797 to be used will be automatically disabled. An empty buffer
4798 accomplishes this, since that is also the representation for a list
4799 containing no features. */
4800
4801 rs->buf[0] = 0;
4082afcc 4802 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4803 {
c8d5aac9 4804 char *q = NULL;
94b0dee1 4805 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4806
73b8c1fd
PA
4807 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
4808 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4809
f7e6eed5
PA
4810 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4811 q = remote_query_supported_append (q, "swbreak+");
4812 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4813 q = remote_query_supported_append (q, "hwbreak+");
4814
dde08ee1
PA
4815 q = remote_query_supported_append (q, "qRelocInsn+");
4816
8020350c
DB
4817 if (packet_set_cmd_state (PACKET_fork_event_feature)
4818 != AUTO_BOOLEAN_FALSE)
4819 q = remote_query_supported_append (q, "fork-events+");
4820 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4821 != AUTO_BOOLEAN_FALSE)
4822 q = remote_query_supported_append (q, "vfork-events+");
4823 if (packet_set_cmd_state (PACKET_exec_event_feature)
4824 != AUTO_BOOLEAN_FALSE)
4825 q = remote_query_supported_append (q, "exec-events+");
89245bc0 4826
750ce8d1
YQ
4827 if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
4828 q = remote_query_supported_append (q, "vContSupported+");
4829
65706a29
PA
4830 if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
4831 q = remote_query_supported_append (q, "QThreadEvents+");
4832
f2faf941
PA
4833 if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
4834 q = remote_query_supported_append (q, "no-resumed+");
4835
b35d5edb
PA
4836 /* Keep this one last to work around a gdbserver <= 7.10 bug in
4837 the qSupported:xmlRegisters=i386 handling. */
4838 if (remote_support_xml != NULL)
4839 q = remote_query_supported_append (q, remote_support_xml);
4840
dde08ee1
PA
4841 q = reconcat (q, "qSupported:", q, (char *) NULL);
4842 putpkt (q);
82f73884 4843
94b0dee1
PA
4844 do_cleanups (old_chain);
4845
be2a5f71
DJ
4846 getpkt (&rs->buf, &rs->buf_size, 0);
4847
4848 /* If an error occured, warn, but do not return - just reset the
4849 buffer to empty and go on to disable features. */
4850 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4851 == PACKET_ERROR)
4852 {
4853 warning (_("Remote failure reply: %s"), rs->buf);
4854 rs->buf[0] = 0;
4855 }
4856 }
4857
4858 memset (seen, 0, sizeof (seen));
4859
4860 next = rs->buf;
4861 while (*next)
4862 {
4863 enum packet_support is_supported;
4864 char *p, *end, *name_end, *value;
4865
4866 /* First separate out this item from the rest of the packet. If
4867 there's another item after this, we overwrite the separator
4868 (terminated strings are much easier to work with). */
4869 p = next;
4870 end = strchr (p, ';');
4871 if (end == NULL)
4872 {
4873 end = p + strlen (p);
4874 next = end;
4875 }
4876 else
4877 {
89be2091
DJ
4878 *end = '\0';
4879 next = end + 1;
4880
be2a5f71
DJ
4881 if (end == p)
4882 {
4883 warning (_("empty item in \"qSupported\" response"));
4884 continue;
4885 }
be2a5f71
DJ
4886 }
4887
4888 name_end = strchr (p, '=');
4889 if (name_end)
4890 {
4891 /* This is a name=value entry. */
4892 is_supported = PACKET_ENABLE;
4893 value = name_end + 1;
4894 *name_end = '\0';
4895 }
4896 else
4897 {
4898 value = NULL;
4899 switch (end[-1])
4900 {
4901 case '+':
4902 is_supported = PACKET_ENABLE;
4903 break;
4904
4905 case '-':
4906 is_supported = PACKET_DISABLE;
4907 break;
4908
4909 case '?':
4910 is_supported = PACKET_SUPPORT_UNKNOWN;
4911 break;
4912
4913 default:
3e43a32a
MS
4914 warning (_("unrecognized item \"%s\" "
4915 "in \"qSupported\" response"), p);
be2a5f71
DJ
4916 continue;
4917 }
4918 end[-1] = '\0';
4919 }
4920
4921 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4922 if (strcmp (remote_protocol_features[i].name, p) == 0)
4923 {
4924 const struct protocol_feature *feature;
4925
4926 seen[i] = 1;
4927 feature = &remote_protocol_features[i];
4928 feature->func (feature, is_supported, value);
4929 break;
4930 }
4931 }
4932
4933 /* If we increased the packet size, make sure to increase the global
4934 buffer size also. We delay this until after parsing the entire
4935 qSupported packet, because this is the same buffer we were
4936 parsing. */
4937 if (rs->buf_size < rs->explicit_packet_size)
4938 {
4939 rs->buf_size = rs->explicit_packet_size;
224c3ddb 4940 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
be2a5f71
DJ
4941 }
4942
4943 /* Handle the defaults for unmentioned features. */
4944 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4945 if (!seen[i])
4946 {
4947 const struct protocol_feature *feature;
4948
4949 feature = &remote_protocol_features[i];
4950 feature->func (feature, feature->default_support, NULL);
4951 }
4952}
4953
048094ac
PA
4954/* Serial QUIT handler for the remote serial descriptor.
4955
4956 Defers handling a Ctrl-C until we're done with the current
4957 command/response packet sequence, unless:
4958
4959 - We're setting up the connection. Don't send a remote interrupt
4960 request, as we're not fully synced yet. Quit immediately
4961 instead.
4962
4963 - The target has been resumed in the foreground
223ffa71 4964 (target_terminal::is_ours is false) with a synchronous resume
048094ac
PA
4965 packet, and we're blocked waiting for the stop reply, thus a
4966 Ctrl-C should be immediately sent to the target.
4967
4968 - We get a second Ctrl-C while still within the same serial read or
4969 write. In that case the serial is seemingly wedged --- offer to
4970 quit/disconnect.
4971
4972 - We see a second Ctrl-C without target response, after having
4973 previously interrupted the target. In that case the target/stub
4974 is probably wedged --- offer to quit/disconnect.
4975*/
4976
4977static void
4978remote_serial_quit_handler (void)
4979{
4980 struct remote_state *rs = get_remote_state ();
4981
4982 if (check_quit_flag ())
4983 {
4984 /* If we're starting up, we're not fully synced yet. Quit
4985 immediately. */
4986 if (rs->starting_up)
4987 quit ();
4988 else if (rs->got_ctrlc_during_io)
4989 {
4990 if (query (_("The target is not responding to GDB commands.\n"
4991 "Stop debugging it? ")))
4992 remote_unpush_and_throw ();
4993 }
4994 /* If ^C has already been sent once, offer to disconnect. */
223ffa71 4995 else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
048094ac
PA
4996 interrupt_query ();
4997 /* All-stop protocol, and blocked waiting for stop reply. Send
4998 an interrupt request. */
223ffa71 4999 else if (!target_terminal::is_ours () && rs->waiting_for_stop_reply)
048094ac
PA
5000 target_interrupt (inferior_ptid);
5001 else
5002 rs->got_ctrlc_during_io = 1;
5003 }
5004}
5005
78a095c3
JK
5006/* Remove any of the remote.c targets from target stack. Upper targets depend
5007 on it so remove them first. */
5008
5009static void
5010remote_unpush_target (void)
5011{
915ef8b1 5012 pop_all_targets_at_and_above (process_stratum);
78a095c3 5013}
be2a5f71 5014
048094ac
PA
5015static void
5016remote_unpush_and_throw (void)
5017{
5018 remote_unpush_target ();
5019 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
5020}
5021
c906108c 5022static void
014f9477 5023remote_open_1 (const char *name, int from_tty,
3e43a32a 5024 struct target_ops *target, int extended_p)
c906108c 5025{
d01949b6 5026 struct remote_state *rs = get_remote_state ();
a6f3e723 5027
c906108c 5028 if (name == 0)
8a3fe4f8 5029 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 5030 "serial device is attached to the remote system\n"
8a3fe4f8 5031 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 5032
23860348 5033 /* See FIXME above. */
c6ebd6cf 5034 if (!target_async_permitted)
92d1e331 5035 wait_forever_enabled_p = 1;
6426a772 5036
2d717e4f 5037 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
5038 Ask this question first, before target_preopen has a chance to kill
5039 anything. */
5d93a237 5040 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 5041 {
78a095c3
JK
5042 if (from_tty
5043 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
5044 error (_("Still connected."));
5045 }
5046
78a095c3 5047 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
5048 target_preopen (from_tty);
5049
89be2091 5050 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
5051 xfree (rs->last_pass_packet);
5052 rs->last_pass_packet = NULL;
89be2091 5053
9b224c5e
PA
5054 /* Make sure we send the program signals list the next time we
5055 resume. */
5e4a05c4
TT
5056 xfree (rs->last_program_signals_packet);
5057 rs->last_program_signals_packet = NULL;
9b224c5e 5058
ad9a8f3f 5059 remote_fileio_reset ();
1dd41f16
NS
5060 reopen_exec_file ();
5061 reread_symbols ();
5062
5d93a237
TT
5063 rs->remote_desc = remote_serial_open (name);
5064 if (!rs->remote_desc)
c906108c
SS
5065 perror_with_name (name);
5066
5067 if (baud_rate != -1)
5068 {
5d93a237 5069 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 5070 {
9b74d5d3
KB
5071 /* The requested speed could not be set. Error out to
5072 top level after closing remote_desc. Take care to
5073 set remote_desc to NULL to avoid closing remote_desc
5074 more than once. */
5d93a237
TT
5075 serial_close (rs->remote_desc);
5076 rs->remote_desc = NULL;
c906108c
SS
5077 perror_with_name (name);
5078 }
5079 }
5080
236af5e3 5081 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 5082 serial_raw (rs->remote_desc);
c906108c
SS
5083
5084 /* If there is something sitting in the buffer we might take it as a
5085 response to a command, which would be bad. */
5d93a237 5086 serial_flush_input (rs->remote_desc);
c906108c
SS
5087
5088 if (from_tty)
5089 {
5090 puts_filtered ("Remote debugging using ");
5091 puts_filtered (name);
5092 puts_filtered ("\n");
5093 }
23860348 5094 push_target (target); /* Switch to using remote target now. */
c906108c 5095
74531fed
PA
5096 /* Register extra event sources in the event loop. */
5097 remote_async_inferior_event_token
5098 = create_async_event_handler (remote_async_inferior_event_handler,
5099 NULL);
5965e028 5100 rs->notif_state = remote_notif_state_allocate ();
74531fed 5101
be2a5f71
DJ
5102 /* Reset the target state; these things will be queried either by
5103 remote_query_supported or as they are needed. */
ca4f7f8b 5104 reset_all_packet_configs_support ();
74531fed 5105 rs->cached_wait_status = 0;
be2a5f71 5106 rs->explicit_packet_size = 0;
a6f3e723 5107 rs->noack_mode = 0;
82f73884 5108 rs->extended = extended_p;
e24a49d8 5109 rs->waiting_for_stop_reply = 0;
3a29589a 5110 rs->ctrlc_pending_p = 0;
048094ac 5111 rs->got_ctrlc_during_io = 0;
802188a7 5112
47f8a51d
TT
5113 rs->general_thread = not_sent_ptid;
5114 rs->continue_thread = not_sent_ptid;
262e1174 5115 rs->remote_traceframe_number = -1;
c906108c 5116
3a00c802
PA
5117 rs->last_resume_exec_dir = EXEC_FORWARD;
5118
9d1f7ab2 5119 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
5120 rs->use_threadinfo_query = 1;
5121 rs->use_threadextra_query = 1;
9d1f7ab2 5122
80152258
PA
5123 readahead_cache_invalidate ();
5124
048094ac
PA
5125 /* Start out by owning the terminal. */
5126 remote_async_terminal_ours_p = 1;
5127
c6ebd6cf 5128 if (target_async_permitted)
92d1e331 5129 {
92d1e331
DJ
5130 /* FIXME: cagney/1999-09-23: During the initial connection it is
5131 assumed that the target is already ready and able to respond to
0df8b418 5132 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 5133 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 5134 around this. Eventually a mechanism that allows
92d1e331 5135 wait_for_inferior() to expect/get timeouts will be
23860348 5136 implemented. */
92d1e331
DJ
5137 wait_forever_enabled_p = 0;
5138 }
5139
23860348 5140 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 5141 no_shared_libraries (NULL, 0);
f78f6cf1 5142
74531fed
PA
5143 /* Start afresh. */
5144 init_thread_list ();
5145
36918e70 5146 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
5147 target (we'd otherwise be in an inconsistent state) and then
5148 propogate the error on up the exception chain. This ensures that
5149 the caller doesn't stumble along blindly assuming that the
5150 function succeeded. The CLI doesn't have this problem but other
5151 UI's, such as MI do.
36918e70
AC
5152
5153 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5154 this function should return an error indication letting the
ce2826aa 5155 caller restore the previous state. Unfortunately the command
36918e70
AC
5156 ``target remote'' is directly wired to this function making that
5157 impossible. On a positive note, the CLI side of this problem has
5158 been fixed - the function set_cmd_context() makes it possible for
5159 all the ``target ....'' commands to share a common callback
5160 function. See cli-dump.c. */
109c3e39 5161 {
2d717e4f 5162
492d29ea 5163 TRY
04bd08de
TT
5164 {
5165 remote_start_remote (from_tty, target, extended_p);
5166 }
492d29ea 5167 CATCH (ex, RETURN_MASK_ALL)
109c3e39 5168 {
c8d104ad
PA
5169 /* Pop the partially set up target - unless something else did
5170 already before throwing the exception. */
5d93a237 5171 if (rs->remote_desc != NULL)
78a095c3 5172 remote_unpush_target ();
c6ebd6cf 5173 if (target_async_permitted)
109c3e39
AC
5174 wait_forever_enabled_p = 1;
5175 throw_exception (ex);
5176 }
492d29ea 5177 END_CATCH
109c3e39 5178 }
c906108c 5179
f4abbc16
MM
5180 remote_btrace_reset ();
5181
c6ebd6cf 5182 if (target_async_permitted)
92d1e331 5183 wait_forever_enabled_p = 1;
43ff13b4
JM
5184}
5185
de0d863e
DB
5186/* Detach the specified process. */
5187
5188static void
5189remote_detach_pid (int pid)
5190{
5191 struct remote_state *rs = get_remote_state ();
5192
5193 if (remote_multi_process_p (rs))
5194 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
5195 else
5196 strcpy (rs->buf, "D");
5197
5198 putpkt (rs->buf);
5199 getpkt (&rs->buf, &rs->buf_size, 0);
5200
5201 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
5202 ;
5203 else if (rs->buf[0] == '\0')
5204 error (_("Remote doesn't know how to detach"));
5205 else
5206 error (_("Can't detach process."));
5207}
5208
5209/* This detaches a program to which we previously attached, using
5210 inferior_ptid to identify the process. After this is done, GDB
5211 can be used to debug some other program. We better not have left
5212 any breakpoints in the target program or it'll die when it hits
5213 one. */
c906108c
SS
5214
5215static void
de0d863e 5216remote_detach_1 (const char *args, int from_tty)
c906108c 5217{
82f73884 5218 int pid = ptid_get_pid (inferior_ptid);
d01949b6 5219 struct remote_state *rs = get_remote_state ();
de0d863e
DB
5220 struct thread_info *tp = find_thread_ptid (inferior_ptid);
5221 int is_fork_parent;
c906108c
SS
5222
5223 if (args)
8a3fe4f8 5224 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 5225
2d717e4f
DJ
5226 if (!target_has_execution)
5227 error (_("No process to detach from."));
5228
0f48b757 5229 target_announce_detach (from_tty);
7cee1e54 5230
c906108c 5231 /* Tell the remote target to detach. */
de0d863e 5232 remote_detach_pid (pid);
82f73884 5233
8020350c
DB
5234 /* Exit only if this is the only active inferior. */
5235 if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
7cee1e54 5236 puts_filtered (_("Ending remote debugging.\n"));
82f73884 5237
de0d863e
DB
5238 /* Check to see if we are detaching a fork parent. Note that if we
5239 are detaching a fork child, tp == NULL. */
5240 is_fork_parent = (tp != NULL
5241 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
5242
5243 /* If doing detach-on-fork, we don't mourn, because that will delete
5244 breakpoints that should be available for the followed inferior. */
5245 if (!is_fork_parent)
bc1e6c81 5246 target_mourn_inferior (inferior_ptid);
de0d863e
DB
5247 else
5248 {
5249 inferior_ptid = null_ptid;
5250 detach_inferior (pid);
5251 }
2d717e4f
DJ
5252}
5253
5254static void
52554a0e 5255remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5256{
de0d863e 5257 remote_detach_1 (args, from_tty);
2d717e4f
DJ
5258}
5259
5260static void
52554a0e 5261extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5262{
de0d863e
DB
5263 remote_detach_1 (args, from_tty);
5264}
5265
5266/* Target follow-fork function for remote targets. On entry, and
5267 at return, the current inferior is the fork parent.
5268
5269 Note that although this is currently only used for extended-remote,
5270 it is named remote_follow_fork in anticipation of using it for the
5271 remote target as well. */
5272
5273static int
5274remote_follow_fork (struct target_ops *ops, int follow_child,
5275 int detach_fork)
5276{
5277 struct remote_state *rs = get_remote_state ();
c269dbdb 5278 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 5279
c269dbdb
DB
5280 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
5281 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
5282 {
5283 /* When following the parent and detaching the child, we detach
5284 the child here. For the case of following the child and
5285 detaching the parent, the detach is done in the target-
5286 independent follow fork code in infrun.c. We can't use
5287 target_detach when detaching an unfollowed child because
5288 the client side doesn't know anything about the child. */
5289 if (detach_fork && !follow_child)
5290 {
5291 /* Detach the fork child. */
5292 ptid_t child_ptid;
5293 pid_t child_pid;
5294
5295 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
5296 child_pid = ptid_get_pid (child_ptid);
5297
5298 remote_detach_pid (child_pid);
5299 detach_inferior (child_pid);
5300 }
5301 }
5302 return 0;
c906108c
SS
5303}
5304
94585166
DB
5305/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
5306 in the program space of the new inferior. On entry and at return the
5307 current inferior is the exec'ing inferior. INF is the new exec'd
5308 inferior, which may be the same as the exec'ing inferior unless
5309 follow-exec-mode is "new". */
5310
5311static void
5312remote_follow_exec (struct target_ops *ops,
5313 struct inferior *inf, char *execd_pathname)
5314{
5315 /* We know that this is a target file name, so if it has the "target:"
5316 prefix we strip it off before saving it in the program space. */
5317 if (is_target_filename (execd_pathname))
5318 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
5319
5320 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
5321}
5322
6ad8ae5c
DJ
5323/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
5324
43ff13b4 5325static void
fee354ee 5326remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 5327{
43ff13b4 5328 if (args)
2d717e4f 5329 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 5330
8020350c
DB
5331 /* Make sure we unpush even the extended remote targets. Calling
5332 target_mourn_inferior won't unpush, and remote_mourn won't
5333 unpush if there is more than one inferior left. */
5334 unpush_target (target);
5335 generic_mourn_inferior ();
2d717e4f 5336
43ff13b4
JM
5337 if (from_tty)
5338 puts_filtered ("Ending remote debugging.\n");
5339}
5340
2d717e4f
DJ
5341/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
5342 be chatty about it. */
5343
5344static void
20f796c9
GB
5345extended_remote_attach (struct target_ops *target, const char *args,
5346 int from_tty)
2d717e4f
DJ
5347{
5348 struct remote_state *rs = get_remote_state ();
be86555c 5349 int pid;
96ef3384 5350 char *wait_status = NULL;
2d717e4f 5351
74164c56 5352 pid = parse_pid_to_attach (args);
2d717e4f 5353
74164c56
JK
5354 /* Remote PID can be freely equal to getpid, do not check it here the same
5355 way as in other targets. */
2d717e4f 5356
4082afcc 5357 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
5358 error (_("This target does not support attaching to a process"));
5359
7cee1e54
PA
5360 if (from_tty)
5361 {
5362 char *exec_file = get_exec_file (0);
5363
5364 if (exec_file)
5365 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5366 target_pid_to_str (pid_to_ptid (pid)));
5367 else
5368 printf_unfiltered (_("Attaching to %s\n"),
5369 target_pid_to_str (pid_to_ptid (pid)));
5370
5371 gdb_flush (gdb_stdout);
5372 }
5373
bba74b36 5374 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
5375 putpkt (rs->buf);
5376 getpkt (&rs->buf, &rs->buf_size, 0);
5377
4082afcc
PA
5378 switch (packet_ok (rs->buf,
5379 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 5380 {
4082afcc 5381 case PACKET_OK:
6efcd9a8 5382 if (!target_is_non_stop_p ())
74531fed
PA
5383 {
5384 /* Save the reply for later. */
224c3ddb 5385 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
5386 strcpy (wait_status, rs->buf);
5387 }
5388 else if (strcmp (rs->buf, "OK") != 0)
5389 error (_("Attaching to %s failed with: %s"),
5390 target_pid_to_str (pid_to_ptid (pid)),
5391 rs->buf);
4082afcc
PA
5392 break;
5393 case PACKET_UNKNOWN:
5394 error (_("This target does not support attaching to a process"));
5395 default:
5396 error (_("Attaching to %s failed"),
5397 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 5398 }
2d717e4f 5399
1b6e6f5c 5400 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 5401
2d717e4f 5402 inferior_ptid = pid_to_ptid (pid);
79d7f229 5403
6efcd9a8 5404 if (target_is_non_stop_p ())
bad34192
PA
5405 {
5406 struct thread_info *thread;
79d7f229 5407
bad34192 5408 /* Get list of threads. */
e8032dde 5409 remote_update_thread_list (target);
82f73884 5410
bad34192
PA
5411 thread = first_thread_of_process (pid);
5412 if (thread)
5413 inferior_ptid = thread->ptid;
5414 else
5415 inferior_ptid = pid_to_ptid (pid);
5416
5417 /* Invalidate our notion of the remote current thread. */
47f8a51d 5418 record_currthread (rs, minus_one_ptid);
bad34192 5419 }
74531fed 5420 else
bad34192
PA
5421 {
5422 /* Now, if we have thread information, update inferior_ptid. */
5423 inferior_ptid = remote_current_thread (inferior_ptid);
5424
5425 /* Add the main thread to the thread list. */
5426 add_thread_silent (inferior_ptid);
5427 }
c0a2216e 5428
96ef3384
UW
5429 /* Next, if the target can specify a description, read it. We do
5430 this before anything involving memory or registers. */
5431 target_find_description ();
5432
6efcd9a8 5433 if (!target_is_non_stop_p ())
74531fed
PA
5434 {
5435 /* Use the previously fetched status. */
5436 gdb_assert (wait_status != NULL);
5437
5438 if (target_can_async_p ())
5439 {
722247f1
YQ
5440 struct notif_event *reply
5441 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 5442
722247f1 5443 push_stop_reply ((struct stop_reply *) reply);
74531fed 5444
6a3753b3 5445 target_async (1);
74531fed
PA
5446 }
5447 else
5448 {
5449 gdb_assert (wait_status != NULL);
5450 strcpy (rs->buf, wait_status);
5451 rs->cached_wait_status = 1;
5452 }
5453 }
5454 else
5455 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5456}
5457
b9c1d481
AS
5458/* Implementation of the to_post_attach method. */
5459
5460static void
5461extended_remote_post_attach (struct target_ops *ops, int pid)
5462{
6efcd9a8
PA
5463 /* Get text, data & bss offsets. */
5464 get_offsets ();
5465
b9c1d481
AS
5466 /* In certain cases GDB might not have had the chance to start
5467 symbol lookup up until now. This could happen if the debugged
5468 binary is not using shared libraries, the vsyscall page is not
5469 present (on Linux) and the binary itself hadn't changed since the
5470 debugging process was started. */
5471 if (symfile_objfile != NULL)
5472 remote_check_symbols();
5473}
5474
c906108c 5475\f
506fb367
DJ
5476/* Check for the availability of vCont. This function should also check
5477 the response. */
c906108c
SS
5478
5479static void
6d820c5c 5480remote_vcont_probe (struct remote_state *rs)
c906108c 5481{
2e9f7625 5482 char *buf;
6d820c5c 5483
2e9f7625
DJ
5484 strcpy (rs->buf, "vCont?");
5485 putpkt (rs->buf);
6d820c5c 5486 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5487 buf = rs->buf;
c906108c 5488
506fb367 5489 /* Make sure that the features we assume are supported. */
61012eef 5490 if (startswith (buf, "vCont"))
506fb367
DJ
5491 {
5492 char *p = &buf[5];
750ce8d1 5493 int support_c, support_C;
506fb367 5494
750ce8d1
YQ
5495 rs->supports_vCont.s = 0;
5496 rs->supports_vCont.S = 0;
506fb367
DJ
5497 support_c = 0;
5498 support_C = 0;
d458bd84 5499 rs->supports_vCont.t = 0;
c1e36e3e 5500 rs->supports_vCont.r = 0;
506fb367
DJ
5501 while (p && *p == ';')
5502 {
5503 p++;
5504 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5505 rs->supports_vCont.s = 1;
506fb367 5506 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5507 rs->supports_vCont.S = 1;
506fb367
DJ
5508 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5509 support_c = 1;
5510 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5511 support_C = 1;
74531fed 5512 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5513 rs->supports_vCont.t = 1;
c1e36e3e
PA
5514 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5515 rs->supports_vCont.r = 1;
506fb367
DJ
5516
5517 p = strchr (p, ';');
5518 }
c906108c 5519
750ce8d1
YQ
5520 /* If c, and C are not all supported, we can't use vCont. Clearing
5521 BUF will make packet_ok disable the packet. */
5522 if (!support_c || !support_C)
506fb367
DJ
5523 buf[0] = 0;
5524 }
c906108c 5525
444abaca 5526 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5527}
c906108c 5528
0d8f58ca
PA
5529/* Helper function for building "vCont" resumptions. Write a
5530 resumption to P. ENDP points to one-passed-the-end of the buffer
5531 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5532 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5533 resumed thread should be single-stepped and/or signalled. If PTID
5534 equals minus_one_ptid, then all threads are resumed; if PTID
5535 represents a process, then all threads of the process are resumed;
5536 the thread to be stepped and/or signalled is given in the global
5537 INFERIOR_PTID. */
5538
5539static char *
5540append_resumption (char *p, char *endp,
2ea28649 5541 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5542{
5543 struct remote_state *rs = get_remote_state ();
5544
a493e3e2 5545 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5546 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5547 else if (step
5548 /* GDB is willing to range step. */
5549 && use_range_stepping
5550 /* Target supports range stepping. */
5551 && rs->supports_vCont.r
5552 /* We don't currently support range stepping multiple
5553 threads with a wildcard (though the protocol allows it,
5554 so stubs shouldn't make an active effort to forbid
5555 it). */
5556 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5557 {
5558 struct thread_info *tp;
5559
5560 if (ptid_equal (ptid, minus_one_ptid))
5561 {
5562 /* If we don't know about the target thread's tid, then
5563 we're resuming magic_null_ptid (see caller). */
5564 tp = find_thread_ptid (magic_null_ptid);
5565 }
5566 else
5567 tp = find_thread_ptid (ptid);
5568 gdb_assert (tp != NULL);
5569
5570 if (tp->control.may_range_step)
5571 {
5572 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5573
5574 p += xsnprintf (p, endp - p, ";r%s,%s",
5575 phex_nz (tp->control.step_range_start,
5576 addr_size),
5577 phex_nz (tp->control.step_range_end,
5578 addr_size));
5579 }
5580 else
5581 p += xsnprintf (p, endp - p, ";s");
5582 }
0d8f58ca
PA
5583 else if (step)
5584 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5585 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5586 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5587 else
5588 p += xsnprintf (p, endp - p, ";c");
5589
5590 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5591 {
5592 ptid_t nptid;
5593
5594 /* All (-1) threads of process. */
ba348170 5595 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5596
5597 p += xsnprintf (p, endp - p, ":");
5598 p = write_ptid (p, endp, nptid);
5599 }
5600 else if (!ptid_equal (ptid, minus_one_ptid))
5601 {
5602 p += xsnprintf (p, endp - p, ":");
5603 p = write_ptid (p, endp, ptid);
5604 }
5605
5606 return p;
5607}
5608
799a2abe
PA
5609/* Clear the thread's private info on resume. */
5610
5611static void
5612resume_clear_thread_private_info (struct thread_info *thread)
5613{
5614 if (thread->priv != NULL)
5615 {
5616 thread->priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5617 thread->priv->watch_data_address = 0;
5618 }
5619}
5620
e5ef252a
PA
5621/* Append a vCont continue-with-signal action for threads that have a
5622 non-zero stop signal. */
5623
5624static char *
5625append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5626{
5627 struct thread_info *thread;
5628
034f788c 5629 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5630 if (ptid_match (thread->ptid, ptid)
5631 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5632 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5633 {
5634 p = append_resumption (p, endp, thread->ptid,
5635 0, thread->suspend.stop_signal);
5636 thread->suspend.stop_signal = GDB_SIGNAL_0;
799a2abe 5637 resume_clear_thread_private_info (thread);
e5ef252a
PA
5638 }
5639
5640 return p;
5641}
5642
7b68ffbb
PA
5643/* Set the target running, using the packets that use Hc
5644 (c/s/C/S). */
5645
5646static void
5647remote_resume_with_hc (struct target_ops *ops,
5648 ptid_t ptid, int step, enum gdb_signal siggnal)
5649{
5650 struct remote_state *rs = get_remote_state ();
5651 struct thread_info *thread;
5652 char *buf;
5653
5654 rs->last_sent_signal = siggnal;
5655 rs->last_sent_step = step;
5656
5657 /* The c/s/C/S resume packets use Hc, so set the continue
5658 thread. */
5659 if (ptid_equal (ptid, minus_one_ptid))
5660 set_continue_thread (any_thread_ptid);
5661 else
5662 set_continue_thread (ptid);
5663
5664 ALL_NON_EXITED_THREADS (thread)
5665 resume_clear_thread_private_info (thread);
5666
5667 buf = rs->buf;
5668 if (execution_direction == EXEC_REVERSE)
5669 {
5670 /* We don't pass signals to the target in reverse exec mode. */
5671 if (info_verbose && siggnal != GDB_SIGNAL_0)
5672 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5673 siggnal);
5674
5675 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5676 error (_("Remote reverse-step not supported."));
5677 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5678 error (_("Remote reverse-continue not supported."));
5679
5680 strcpy (buf, step ? "bs" : "bc");
5681 }
5682 else if (siggnal != GDB_SIGNAL_0)
5683 {
5684 buf[0] = step ? 'S' : 'C';
5685 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5686 buf[2] = tohex (((int) siggnal) & 0xf);
5687 buf[3] = '\0';
5688 }
5689 else
5690 strcpy (buf, step ? "s" : "c");
5691
5692 putpkt (buf);
5693}
5694
506fb367
DJ
5695/* Resume the remote inferior by using a "vCont" packet. The thread
5696 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5697 resumed thread should be single-stepped and/or signalled. If PTID
5698 equals minus_one_ptid, then all threads are resumed; the thread to
5699 be stepped and/or signalled is given in the global INFERIOR_PTID.
5700 This function returns non-zero iff it resumes the inferior.
44eaed12 5701
7b68ffbb
PA
5702 This function issues a strict subset of all possible vCont commands
5703 at the moment. */
44eaed12 5704
506fb367 5705static int
7b68ffbb 5706remote_resume_with_vcont (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5707{
5708 struct remote_state *rs = get_remote_state ();
82f73884
PA
5709 char *p;
5710 char *endp;
44eaed12 5711
7b68ffbb
PA
5712 /* No reverse execution actions defined for vCont. */
5713 if (execution_direction == EXEC_REVERSE)
5714 return 0;
5715
4082afcc 5716 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5717 remote_vcont_probe (rs);
44eaed12 5718
4082afcc 5719 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5720 return 0;
44eaed12 5721
82f73884
PA
5722 p = rs->buf;
5723 endp = rs->buf + get_remote_packet_size ();
5724
506fb367
DJ
5725 /* If we could generate a wider range of packets, we'd have to worry
5726 about overflowing BUF. Should there be a generic
5727 "multi-part-packet" packet? */
5728
0d8f58ca
PA
5729 p += xsnprintf (p, endp - p, "vCont");
5730
79d7f229 5731 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5732 {
79d7f229
PA
5733 /* MAGIC_NULL_PTID means that we don't have any active threads,
5734 so we don't have any TID numbers the inferior will
5735 understand. Make sure to only send forms that do not specify
5736 a TID. */
a9cbf802 5737 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5738 }
0d8f58ca 5739 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5740 {
0d8f58ca
PA
5741 /* Resume all threads (of all processes, or of a single
5742 process), with preference for INFERIOR_PTID. This assumes
5743 inferior_ptid belongs to the set of all threads we are about
5744 to resume. */
a493e3e2 5745 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5746 {
0d8f58ca
PA
5747 /* Step inferior_ptid, with or without signal. */
5748 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5749 }
0d8f58ca 5750
e5ef252a
PA
5751 /* Also pass down any pending signaled resumption for other
5752 threads not the current. */
5753 p = append_pending_thread_resumptions (p, endp, ptid);
5754
0d8f58ca 5755 /* And continue others without a signal. */
a493e3e2 5756 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5757 }
5758 else
506fb367
DJ
5759 {
5760 /* Scheduler locking; resume only PTID. */
a9cbf802 5761 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5762 }
c906108c 5763
82f73884
PA
5764 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5765 putpkt (rs->buf);
506fb367 5766
6efcd9a8 5767 if (target_is_non_stop_p ())
74531fed
PA
5768 {
5769 /* In non-stop, the stub replies to vCont with "OK". The stop
5770 reply will be reported asynchronously by means of a `%Stop'
5771 notification. */
5772 getpkt (&rs->buf, &rs->buf_size, 0);
5773 if (strcmp (rs->buf, "OK") != 0)
5774 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5775 }
5776
506fb367 5777 return 1;
c906108c 5778}
43ff13b4 5779
506fb367
DJ
5780/* Tell the remote machine to resume. */
5781
43ff13b4 5782static void
28439f5e 5783remote_resume (struct target_ops *ops,
2ea28649 5784 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5785{
d01949b6 5786 struct remote_state *rs = get_remote_state ();
43ff13b4 5787
85ad3aaf
PA
5788 /* When connected in non-stop mode, the core resumes threads
5789 individually. Resuming remote threads directly in target_resume
5790 would thus result in sending one packet per thread. Instead, to
5791 minimize roundtrip latency, here we just store the resume
5792 request; the actual remote resumption will be done in
5793 target_commit_resume / remote_commit_resume, where we'll be able
5794 to do vCont action coalescing. */
5795 if (target_is_non_stop_p () && execution_direction != EXEC_REVERSE)
5796 {
5797 struct private_thread_info *remote_thr;
5798
5799 if (ptid_equal (minus_one_ptid, ptid) || ptid_is_pid (ptid))
5800 remote_thr = get_private_info_ptid (inferior_ptid);
5801 else
5802 remote_thr = get_private_info_ptid (ptid);
5803 remote_thr->last_resume_step = step;
5804 remote_thr->last_resume_sig = siggnal;
5805 return;
5806 }
5807
722247f1
YQ
5808 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5809 (explained in remote-notif.c:handle_notification) so
5810 remote_notif_process is not called. We need find a place where
5811 it is safe to start a 'vNotif' sequence. It is good to do it
5812 before resuming inferior, because inferior was stopped and no RSP
5813 traffic at that moment. */
6efcd9a8 5814 if (!target_is_non_stop_p ())
5965e028 5815 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5816
3a00c802
PA
5817 rs->last_resume_exec_dir = execution_direction;
5818
7b68ffbb
PA
5819 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
5820 if (!remote_resume_with_vcont (ptid, step, siggnal))
5821 remote_resume_with_hc (ops, ptid, step, siggnal);
43ff13b4 5822
2acceee2 5823 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5824 with the event loop. NOTE: this is the one place where all the
5825 execution commands end up. We could alternatively do this in each
23860348 5826 of the execution commands in infcmd.c. */
2acceee2
JM
5827 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5828 into infcmd.c in order to allow inferior function calls to work
23860348 5829 NOT asynchronously. */
362646f5 5830 if (target_can_async_p ())
6a3753b3 5831 target_async (1);
e24a49d8
PA
5832
5833 /* We've just told the target to resume. The remote server will
5834 wait for the inferior to stop, and then send a stop reply. In
5835 the mean time, we can't start another command/query ourselves
74531fed
PA
5836 because the stub wouldn't be ready to process it. This applies
5837 only to the base all-stop protocol, however. In non-stop (which
5838 only supports vCont), the stub replies with an "OK", and is
5839 immediate able to process further serial input. */
6efcd9a8 5840 if (!target_is_non_stop_p ())
74531fed 5841 rs->waiting_for_stop_reply = 1;
43ff13b4 5842}
85ad3aaf
PA
5843
5844static void check_pending_events_prevent_wildcard_vcont
5845 (int *may_global_wildcard_vcont);
5846static int is_pending_fork_parent_thread (struct thread_info *thread);
5847
5848/* Private per-inferior info for target remote processes. */
5849
5850struct private_inferior
5851{
5852 /* Whether we can send a wildcard vCont for this process. */
5853 int may_wildcard_vcont;
5854};
5855
5856/* Structure used to track the construction of a vCont packet in the
5857 outgoing packet buffer. This is used to send multiple vCont
5858 packets if we have more actions than would fit a single packet. */
5859
5860struct vcont_builder
5861{
5862 /* Pointer to the first action. P points here if no action has been
5863 appended yet. */
5864 char *first_action;
5865
5866 /* Where the next action will be appended. */
5867 char *p;
5868
5869 /* The end of the buffer. Must never write past this. */
5870 char *endp;
5871};
5872
5873/* Prepare the outgoing buffer for a new vCont packet. */
5874
5875static void
5876vcont_builder_restart (struct vcont_builder *builder)
5877{
5878 struct remote_state *rs = get_remote_state ();
5879
5880 builder->p = rs->buf;
5881 builder->endp = rs->buf + get_remote_packet_size ();
5882 builder->p += xsnprintf (builder->p, builder->endp - builder->p, "vCont");
5883 builder->first_action = builder->p;
5884}
5885
5886/* If the vCont packet being built has any action, send it to the
5887 remote end. */
5888
5889static void
5890vcont_builder_flush (struct vcont_builder *builder)
5891{
5892 struct remote_state *rs;
5893
5894 if (builder->p == builder->first_action)
5895 return;
5896
5897 rs = get_remote_state ();
5898 putpkt (rs->buf);
5899 getpkt (&rs->buf, &rs->buf_size, 0);
5900 if (strcmp (rs->buf, "OK") != 0)
5901 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5902}
5903
5904/* The largest action is range-stepping, with its two addresses. This
5905 is more than sufficient. If a new, bigger action is created, it'll
5906 quickly trigger a failed assertion in append_resumption (and we'll
5907 just bump this). */
5908#define MAX_ACTION_SIZE 200
5909
5910/* Append a new vCont action in the outgoing packet being built. If
5911 the action doesn't fit the packet along with previous actions, push
5912 what we've got so far to the remote end and start over a new vCont
5913 packet (with the new action). */
5914
5915static void
5916vcont_builder_push_action (struct vcont_builder *builder,
5917 ptid_t ptid, int step, enum gdb_signal siggnal)
5918{
5919 char buf[MAX_ACTION_SIZE + 1];
5920 char *endp;
5921 size_t rsize;
5922
5923 endp = append_resumption (buf, buf + sizeof (buf),
5924 ptid, step, siggnal);
5925
5926 /* Check whether this new action would fit in the vCont packet along
5927 with previous actions. If not, send what we've got so far and
5928 start a new vCont packet. */
5929 rsize = endp - buf;
5930 if (rsize > builder->endp - builder->p)
5931 {
5932 vcont_builder_flush (builder);
5933 vcont_builder_restart (builder);
5934
5935 /* Should now fit. */
5936 gdb_assert (rsize <= builder->endp - builder->p);
5937 }
5938
5939 memcpy (builder->p, buf, rsize);
5940 builder->p += rsize;
5941 *builder->p = '\0';
5942}
5943
5944/* to_commit_resume implementation. */
5945
5946static void
5947remote_commit_resume (struct target_ops *ops)
5948{
5949 struct remote_state *rs = get_remote_state ();
5950 struct inferior *inf;
5951 struct thread_info *tp;
5952 int any_process_wildcard;
5953 int may_global_wildcard_vcont;
5954 struct vcont_builder vcont_builder;
5955
5956 /* If connected in all-stop mode, we'd send the remote resume
5957 request directly from remote_resume. Likewise if
5958 reverse-debugging, as there are no defined vCont actions for
5959 reverse execution. */
5960 if (!target_is_non_stop_p () || execution_direction == EXEC_REVERSE)
5961 return;
5962
5963 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
5964 instead of resuming all threads of each process individually.
5965 However, if any thread of a process must remain halted, we can't
5966 send wildcard resumes and must send one action per thread.
5967
5968 Care must be taken to not resume threads/processes the server
5969 side already told us are stopped, but the core doesn't know about
5970 yet, because the events are still in the vStopped notification
5971 queue. For example:
5972
5973 #1 => vCont s:p1.1;c
5974 #2 <= OK
5975 #3 <= %Stopped T05 p1.1
5976 #4 => vStopped
5977 #5 <= T05 p1.2
5978 #6 => vStopped
5979 #7 <= OK
5980 #8 (infrun handles the stop for p1.1 and continues stepping)
5981 #9 => vCont s:p1.1;c
5982
5983 The last vCont above would resume thread p1.2 by mistake, because
5984 the server has no idea that the event for p1.2 had not been
5985 handled yet.
5986
5987 The server side must similarly ignore resume actions for the
5988 thread that has a pending %Stopped notification (and any other
5989 threads with events pending), until GDB acks the notification
5990 with vStopped. Otherwise, e.g., the following case is
5991 mishandled:
5992
5993 #1 => g (or any other packet)
5994 #2 <= [registers]
5995 #3 <= %Stopped T05 p1.2
5996 #4 => vCont s:p1.1;c
5997 #5 <= OK
5998
5999 Above, the server must not resume thread p1.2. GDB can't know
6000 that p1.2 stopped until it acks the %Stopped notification, and
6001 since from GDB's perspective all threads should be running, it
6002 sends a "c" action.
6003
6004 Finally, special care must also be given to handling fork/vfork
6005 events. A (v)fork event actually tells us that two processes
6006 stopped -- the parent and the child. Until we follow the fork,
6007 we must not resume the child. Therefore, if we have a pending
6008 fork follow, we must not send a global wildcard resume action
6009 (vCont;c). We can still send process-wide wildcards though. */
6010
6011 /* Start by assuming a global wildcard (vCont;c) is possible. */
6012 may_global_wildcard_vcont = 1;
6013
6014 /* And assume every process is individually wildcard-able too. */
6015 ALL_NON_EXITED_INFERIORS (inf)
6016 {
6017 if (inf->priv == NULL)
6018 inf->priv = XNEW (struct private_inferior);
6019 inf->priv->may_wildcard_vcont = 1;
6020 }
6021
6022 /* Check for any pending events (not reported or processed yet) and
6023 disable process and global wildcard resumes appropriately. */
6024 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
6025
6026 ALL_NON_EXITED_THREADS (tp)
6027 {
6028 /* If a thread of a process is not meant to be resumed, then we
6029 can't wildcard that process. */
6030 if (!tp->executing)
6031 {
6032 tp->inf->priv->may_wildcard_vcont = 0;
6033
6034 /* And if we can't wildcard a process, we can't wildcard
6035 everything either. */
6036 may_global_wildcard_vcont = 0;
6037 continue;
6038 }
6039
6040 /* If a thread is the parent of an unfollowed fork, then we
6041 can't do a global wildcard, as that would resume the fork
6042 child. */
6043 if (is_pending_fork_parent_thread (tp))
6044 may_global_wildcard_vcont = 0;
6045 }
6046
6047 /* Now let's build the vCont packet(s). Actions must be appended
6048 from narrower to wider scopes (thread -> process -> global). If
6049 we end up with too many actions for a single packet vcont_builder
6050 flushes the current vCont packet to the remote side and starts a
6051 new one. */
6052 vcont_builder_restart (&vcont_builder);
6053
6054 /* Threads first. */
6055 ALL_NON_EXITED_THREADS (tp)
6056 {
6057 struct private_thread_info *remote_thr = tp->priv;
6058
6059 if (!tp->executing || remote_thr->vcont_resumed)
6060 continue;
6061
6062 gdb_assert (!thread_is_in_step_over_chain (tp));
6063
6064 if (!remote_thr->last_resume_step
6065 && remote_thr->last_resume_sig == GDB_SIGNAL_0
6066 && tp->inf->priv->may_wildcard_vcont)
6067 {
6068 /* We'll send a wildcard resume instead. */
6069 remote_thr->vcont_resumed = 1;
6070 continue;
6071 }
6072
6073 vcont_builder_push_action (&vcont_builder, tp->ptid,
6074 remote_thr->last_resume_step,
6075 remote_thr->last_resume_sig);
6076 remote_thr->vcont_resumed = 1;
6077 }
6078
6079 /* Now check whether we can send any process-wide wildcard. This is
6080 to avoid sending a global wildcard in the case nothing is
6081 supposed to be resumed. */
6082 any_process_wildcard = 0;
6083
6084 ALL_NON_EXITED_INFERIORS (inf)
6085 {
6086 if (inf->priv->may_wildcard_vcont)
6087 {
6088 any_process_wildcard = 1;
6089 break;
6090 }
6091 }
6092
6093 if (any_process_wildcard)
6094 {
6095 /* If all processes are wildcard-able, then send a single "c"
6096 action, otherwise, send an "all (-1) threads of process"
6097 continue action for each running process, if any. */
6098 if (may_global_wildcard_vcont)
6099 {
6100 vcont_builder_push_action (&vcont_builder, minus_one_ptid,
6101 0, GDB_SIGNAL_0);
6102 }
6103 else
6104 {
6105 ALL_NON_EXITED_INFERIORS (inf)
6106 {
6107 if (inf->priv->may_wildcard_vcont)
6108 {
6109 vcont_builder_push_action (&vcont_builder,
6110 pid_to_ptid (inf->pid),
6111 0, GDB_SIGNAL_0);
6112 }
6113 }
6114 }
6115 }
6116
6117 vcont_builder_flush (&vcont_builder);
6118}
6119
c906108c 6120\f
43ff13b4 6121
74531fed
PA
6122/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6123 thread, all threads of a remote process, or all threads of all
6124 processes. */
6125
6126static void
6127remote_stop_ns (ptid_t ptid)
6128{
6129 struct remote_state *rs = get_remote_state ();
6130 char *p = rs->buf;
6131 char *endp = rs->buf + get_remote_packet_size ();
74531fed 6132
4082afcc 6133 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
6134 remote_vcont_probe (rs);
6135
d458bd84 6136 if (!rs->supports_vCont.t)
74531fed
PA
6137 error (_("Remote server does not support stopping threads"));
6138
f91d3df5
PA
6139 if (ptid_equal (ptid, minus_one_ptid)
6140 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
6141 p += xsnprintf (p, endp - p, "vCont;t");
6142 else
6143 {
6144 ptid_t nptid;
6145
74531fed
PA
6146 p += xsnprintf (p, endp - p, "vCont;t:");
6147
6148 if (ptid_is_pid (ptid))
6149 /* All (-1) threads of process. */
ba348170 6150 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
6151 else
6152 {
6153 /* Small optimization: if we already have a stop reply for
6154 this thread, no use in telling the stub we want this
6155 stopped. */
6156 if (peek_stop_reply (ptid))
6157 return;
6158
6159 nptid = ptid;
6160 }
6161
a9cbf802 6162 write_ptid (p, endp, nptid);
74531fed
PA
6163 }
6164
6165 /* In non-stop, we get an immediate OK reply. The stop reply will
6166 come in asynchronously by notification. */
6167 putpkt (rs->buf);
6168 getpkt (&rs->buf, &rs->buf_size, 0);
6169 if (strcmp (rs->buf, "OK") != 0)
6170 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
6171}
6172
bfedc46a
PA
6173/* All-stop version of target_interrupt. Sends a break or a ^C to
6174 interrupt the remote target. It is undefined which thread of which
6175 process reports the interrupt. */
74531fed
PA
6176
6177static void
de979965 6178remote_interrupt_as (void)
74531fed
PA
6179{
6180 struct remote_state *rs = get_remote_state ();
6181
3a29589a
DJ
6182 rs->ctrlc_pending_p = 1;
6183
74531fed
PA
6184 /* If the inferior is stopped already, but the core didn't know
6185 about it yet, just ignore the request. The cached wait status
6186 will be collected in remote_wait. */
6187 if (rs->cached_wait_status)
6188 return;
6189
9a7071a8
JB
6190 /* Send interrupt_sequence to remote target. */
6191 send_interrupt_sequence ();
74531fed
PA
6192}
6193
de979965
PA
6194/* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
6195 the remote target. It is undefined which thread of which process
e42de8c7
PA
6196 reports the interrupt. Throws an error if the packet is not
6197 supported by the server. */
de979965 6198
e42de8c7 6199static void
de979965
PA
6200remote_interrupt_ns (void)
6201{
6202 struct remote_state *rs = get_remote_state ();
6203 char *p = rs->buf;
6204 char *endp = rs->buf + get_remote_packet_size ();
6205
6206 xsnprintf (p, endp - p, "vCtrlC");
6207
6208 /* In non-stop, we get an immediate OK reply. The stop reply will
6209 come in asynchronously by notification. */
6210 putpkt (rs->buf);
6211 getpkt (&rs->buf, &rs->buf_size, 0);
6212
6213 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
6214 {
6215 case PACKET_OK:
6216 break;
6217 case PACKET_UNKNOWN:
e42de8c7 6218 error (_("No support for interrupting the remote target."));
de979965
PA
6219 case PACKET_ERROR:
6220 error (_("Interrupting target failed: %s"), rs->buf);
6221 }
de979965
PA
6222}
6223
bfedc46a 6224/* Implement the to_stop function for the remote targets. */
74531fed 6225
c906108c 6226static void
1eab8a48 6227remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 6228{
7a292a7a 6229 if (remote_debug)
0f71a2f6 6230 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 6231
6efcd9a8 6232 if (target_is_non_stop_p ())
74531fed 6233 remote_stop_ns (ptid);
c906108c 6234 else
bfedc46a
PA
6235 {
6236 /* We don't currently have a way to transparently pause the
6237 remote target in all-stop mode. Interrupt it instead. */
de979965 6238 remote_interrupt_as ();
bfedc46a
PA
6239 }
6240}
6241
6242/* Implement the to_interrupt function for the remote targets. */
6243
6244static void
6245remote_interrupt (struct target_ops *self, ptid_t ptid)
6246{
e42de8c7
PA
6247 struct remote_state *rs = get_remote_state ();
6248
bfedc46a
PA
6249 if (remote_debug)
6250 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
6251
e42de8c7
PA
6252 if (target_is_non_stop_p ())
6253 remote_interrupt_ns ();
bfedc46a 6254 else
e42de8c7 6255 remote_interrupt_as ();
c906108c
SS
6256}
6257
93692b58
PA
6258/* Implement the to_pass_ctrlc function for the remote targets. */
6259
6260static void
6261remote_pass_ctrlc (struct target_ops *self)
6262{
6263 struct remote_state *rs = get_remote_state ();
6264
6265 if (remote_debug)
6266 fprintf_unfiltered (gdb_stdlog, "remote_pass_ctrlc called\n");
6267
6268 /* If we're starting up, we're not fully synced yet. Quit
6269 immediately. */
6270 if (rs->starting_up)
6271 quit ();
6272 /* If ^C has already been sent once, offer to disconnect. */
6273 else if (rs->ctrlc_pending_p)
6274 interrupt_query ();
6275 else
6276 target_interrupt (inferior_ptid);
6277}
6278
c906108c
SS
6279/* Ask the user what to do when an interrupt is received. */
6280
6281static void
fba45db2 6282interrupt_query (void)
c906108c 6283{
abc56d60 6284 struct remote_state *rs = get_remote_state ();
c906108c 6285
abc56d60 6286 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 6287 {
abc56d60
PA
6288 if (query (_("The target is not responding to interrupt requests.\n"
6289 "Stop debugging it? ")))
74531fed 6290 {
78a095c3 6291 remote_unpush_target ();
abc56d60 6292 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
6293 }
6294 }
abc56d60
PA
6295 else
6296 {
6297 if (query (_("Interrupted while waiting for the program.\n"
6298 "Give up waiting? ")))
6299 quit ();
6300 }
c906108c
SS
6301}
6302
6426a772
JM
6303/* Enable/disable target terminal ownership. Most targets can use
6304 terminal groups to control terminal ownership. Remote targets are
6305 different in that explicit transfer of ownership to/from GDB/target
23860348 6306 is required. */
6426a772
JM
6307
6308static void
d2f640d4 6309remote_terminal_inferior (struct target_ops *self)
6426a772 6310{
223ffa71 6311 /* FIXME: cagney/1999-09-27: Make calls to target_terminal::*()
d9d2d8b6
PA
6312 idempotent. The event-loop GDB talking to an asynchronous target
6313 with a synchronous command calls this function from both
6314 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
6315 transfer the terminal to the target when it shouldn't this guard
6316 can go away. */
6426a772
JM
6317 if (!remote_async_terminal_ours_p)
6318 return;
6426a772 6319 remote_async_terminal_ours_p = 0;
6426a772
JM
6320 /* NOTE: At this point we could also register our selves as the
6321 recipient of all input. Any characters typed could then be
23860348 6322 passed on down to the target. */
6426a772
JM
6323}
6324
6325static void
e3594fd1 6326remote_terminal_ours (struct target_ops *self)
6426a772 6327{
75c99385 6328 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
6329 if (remote_async_terminal_ours_p)
6330 return;
6426a772
JM
6331 remote_async_terminal_ours_p = 1;
6332}
6333
176a6961 6334static void
917317f4 6335remote_console_output (char *msg)
c906108c
SS
6336{
6337 char *p;
6338
c5aa993b 6339 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
6340 {
6341 char tb[2];
6342 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 6343
c906108c
SS
6344 tb[0] = c;
6345 tb[1] = 0;
43ff13b4 6346 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 6347 }
00db5b94
PA
6348 gdb_flush (gdb_stdtarg);
6349}
74531fed 6350
74531fed
PA
6351DEF_VEC_O(cached_reg_t);
6352
722247f1 6353typedef struct stop_reply
74531fed 6354{
722247f1 6355 struct notif_event base;
74531fed 6356
722247f1 6357 /* The identifier of the thread about this event */
74531fed
PA
6358 ptid_t ptid;
6359
340e3c99 6360 /* The remote state this event is associated with. When the remote
bcc75809
YQ
6361 connection, represented by a remote_state object, is closed,
6362 all the associated stop_reply events should be released. */
6363 struct remote_state *rs;
6364
74531fed
PA
6365 struct target_waitstatus ws;
6366
15148d6a
PA
6367 /* Expedited registers. This makes remote debugging a bit more
6368 efficient for those targets that provide critical registers as
6369 part of their normal status mechanism (as another roundtrip to
6370 fetch them is avoided). */
74531fed
PA
6371 VEC(cached_reg_t) *regcache;
6372
f7e6eed5
PA
6373 enum target_stop_reason stop_reason;
6374
74531fed
PA
6375 CORE_ADDR watch_data_address;
6376
dc146f7c 6377 int core;
722247f1 6378} *stop_reply_p;
a744cf53 6379
722247f1
YQ
6380DECLARE_QUEUE_P (stop_reply_p);
6381DEFINE_QUEUE_P (stop_reply_p);
6382/* The list of already fetched and acknowledged stop events. This
6383 queue is used for notification Stop, and other notifications
6384 don't need queue for their events, because the notification events
6385 of Stop can't be consumed immediately, so that events should be
6386 queued first, and be consumed by remote_wait_{ns,as} one per
6387 time. Other notifications can consume their events immediately,
6388 so queue is not needed for them. */
6389static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
6390
6391static void
6392stop_reply_xfree (struct stop_reply *r)
6393{
f48ff2a7 6394 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
6395}
6396
221e1a37
PA
6397/* Return the length of the stop reply queue. */
6398
6399static int
6400stop_reply_queue_length (void)
6401{
6402 return QUEUE_length (stop_reply_p, stop_reply_queue);
6403}
6404
722247f1
YQ
6405static void
6406remote_notif_stop_parse (struct notif_client *self, char *buf,
6407 struct notif_event *event)
6408{
6409 remote_parse_stop_reply (buf, (struct stop_reply *) event);
6410}
6411
6412static void
6413remote_notif_stop_ack (struct notif_client *self, char *buf,
6414 struct notif_event *event)
6415{
6416 struct stop_reply *stop_reply = (struct stop_reply *) event;
6417
6418 /* acknowledge */
f5c4fcd9 6419 putpkt (self->ack_command);
722247f1
YQ
6420
6421 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
6422 /* We got an unknown stop reply. */
6423 error (_("Unknown stop reply"));
6424
6425 push_stop_reply (stop_reply);
6426}
6427
6428static int
6429remote_notif_stop_can_get_pending_events (struct notif_client *self)
6430{
6431 /* We can't get pending events in remote_notif_process for
6432 notification stop, and we have to do this in remote_wait_ns
6433 instead. If we fetch all queued events from stub, remote stub
6434 may exit and we have no chance to process them back in
6435 remote_wait_ns. */
6436 mark_async_event_handler (remote_async_inferior_event_token);
6437 return 0;
6438}
6439
6440static void
6441stop_reply_dtr (struct notif_event *event)
6442{
6443 struct stop_reply *r = (struct stop_reply *) event;
d1dff226
AH
6444 cached_reg_t *reg;
6445 int ix;
6446
6447 for (ix = 0;
6448 VEC_iterate (cached_reg_t, r->regcache, ix, reg);
6449 ix++)
6450 xfree (reg->data);
722247f1
YQ
6451
6452 VEC_free (cached_reg_t, r->regcache);
6453}
6454
6455static struct notif_event *
6456remote_notif_stop_alloc_reply (void)
6457{
8d749320
SM
6458 /* We cast to a pointer to the "base class". */
6459 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
6460
6461 r->dtr = stop_reply_dtr;
6462
6463 return r;
6464}
6465
6466/* A client of notification Stop. */
6467
6468struct notif_client notif_client_stop =
6469{
6470 "Stop",
6471 "vStopped",
6472 remote_notif_stop_parse,
6473 remote_notif_stop_ack,
6474 remote_notif_stop_can_get_pending_events,
6475 remote_notif_stop_alloc_reply,
f48ff2a7 6476 REMOTE_NOTIF_STOP,
722247f1
YQ
6477};
6478
6479/* A parameter to pass data in and out. */
6480
6481struct queue_iter_param
6482{
6483 void *input;
6484 struct stop_reply *output;
6485};
6486
85ad3aaf 6487/* Determine if THREAD_PTID is a pending fork parent thread. ARG contains
cbb8991c
DB
6488 the pid of the process that owns the threads we want to check, or
6489 -1 if we want to check all threads. */
6490
6491static int
6492is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
6493 ptid_t thread_ptid)
6494{
6495 if (ws->kind == TARGET_WAITKIND_FORKED
6496 || ws->kind == TARGET_WAITKIND_VFORKED)
6497 {
6498 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
6499 return 1;
6500 }
6501
6502 return 0;
6503}
6504
85ad3aaf
PA
6505/* Return the thread's pending status used to determine whether the
6506 thread is a fork parent stopped at a fork event. */
6507
6508static struct target_waitstatus *
6509thread_pending_fork_status (struct thread_info *thread)
6510{
6511 if (thread->suspend.waitstatus_pending_p)
6512 return &thread->suspend.waitstatus;
6513 else
6514 return &thread->pending_follow;
6515}
6516
6517/* Determine if THREAD is a pending fork parent thread. */
6518
6519static int
6520is_pending_fork_parent_thread (struct thread_info *thread)
6521{
6522 struct target_waitstatus *ws = thread_pending_fork_status (thread);
6523 int pid = -1;
6524
6525 return is_pending_fork_parent (ws, pid, thread->ptid);
6526}
6527
cbb8991c
DB
6528/* Check whether EVENT is a fork event, and if it is, remove the
6529 fork child from the context list passed in DATA. */
6530
6531static int
6532remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
6533 QUEUE_ITER (stop_reply_p) *iter,
6534 stop_reply_p event,
6535 void *data)
6536{
19ba03f4
SM
6537 struct queue_iter_param *param = (struct queue_iter_param *) data;
6538 struct threads_listing_context *context
6539 = (struct threads_listing_context *) param->input;
cbb8991c
DB
6540
6541 if (event->ws.kind == TARGET_WAITKIND_FORKED
65706a29
PA
6542 || event->ws.kind == TARGET_WAITKIND_VFORKED
6543 || event->ws.kind == TARGET_WAITKIND_THREAD_EXITED)
6544 threads_listing_context_remove (&event->ws, context);
cbb8991c
DB
6545
6546 return 1;
6547}
6548
6549/* If CONTEXT contains any fork child threads that have not been
6550 reported yet, remove them from the CONTEXT list. If such a
6551 thread exists it is because we are stopped at a fork catchpoint
6552 and have not yet called follow_fork, which will set up the
6553 host-side data structures for the new process. */
6554
6555static void
6556remove_new_fork_children (struct threads_listing_context *context)
6557{
6558 struct thread_info * thread;
6559 int pid = -1;
6560 struct notif_client *notif = &notif_client_stop;
6561 struct queue_iter_param param;
6562
6563 /* For any threads stopped at a fork event, remove the corresponding
6564 fork child threads from the CONTEXT list. */
6565 ALL_NON_EXITED_THREADS (thread)
6566 {
85ad3aaf 6567 struct target_waitstatus *ws = thread_pending_fork_status (thread);
cbb8991c
DB
6568
6569 if (is_pending_fork_parent (ws, pid, thread->ptid))
6570 {
6571 threads_listing_context_remove (ws, context);
6572 }
6573 }
6574
6575 /* Check for any pending fork events (not reported or processed yet)
6576 in process PID and remove those fork child threads from the
6577 CONTEXT list as well. */
6578 remote_notif_get_pending_events (notif);
6579 param.input = context;
6580 param.output = NULL;
6581 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6582 remove_child_of_pending_fork, &param);
6583}
6584
85ad3aaf
PA
6585/* Check whether EVENT would prevent a global or process wildcard
6586 vCont action. */
6587
6588static int
6589check_pending_event_prevents_wildcard_vcont_callback
6590 (QUEUE (stop_reply_p) *q,
6591 QUEUE_ITER (stop_reply_p) *iter,
6592 stop_reply_p event,
6593 void *data)
6594{
6595 struct inferior *inf;
6596 int *may_global_wildcard_vcont = (int *) data;
6597
6598 if (event->ws.kind == TARGET_WAITKIND_NO_RESUMED
6599 || event->ws.kind == TARGET_WAITKIND_NO_HISTORY)
6600 return 1;
6601
6602 if (event->ws.kind == TARGET_WAITKIND_FORKED
6603 || event->ws.kind == TARGET_WAITKIND_VFORKED)
6604 *may_global_wildcard_vcont = 0;
6605
6606 inf = find_inferior_ptid (event->ptid);
6607
6608 /* This may be the first time we heard about this process.
6609 Regardless, we must not do a global wildcard resume, otherwise
6610 we'd resume this process too. */
6611 *may_global_wildcard_vcont = 0;
6612 if (inf != NULL)
6613 inf->priv->may_wildcard_vcont = 0;
6614
6615 return 1;
6616}
6617
6618/* Check whether any event pending in the vStopped queue would prevent
6619 a global or process wildcard vCont action. Clear
6620 *may_global_wildcard if we can't do a global wildcard (vCont;c),
6621 and clear the event inferior's may_wildcard_vcont flag if we can't
6622 do a process-wide wildcard resume (vCont;c:pPID.-1). */
6623
6624static void
6625check_pending_events_prevent_wildcard_vcont (int *may_global_wildcard)
6626{
6627 struct notif_client *notif = &notif_client_stop;
6628
6629 remote_notif_get_pending_events (notif);
6630 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6631 check_pending_event_prevents_wildcard_vcont_callback,
6632 may_global_wildcard);
6633}
6634
f48ff2a7
YQ
6635/* Remove stop replies in the queue if its pid is equal to the given
6636 inferior's pid. */
722247f1
YQ
6637
6638static int
f48ff2a7
YQ
6639remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
6640 QUEUE_ITER (stop_reply_p) *iter,
6641 stop_reply_p event,
6642 void *data)
722247f1 6643{
19ba03f4
SM
6644 struct queue_iter_param *param = (struct queue_iter_param *) data;
6645 struct inferior *inf = (struct inferior *) param->input;
722247f1 6646
f48ff2a7 6647 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
6648 {
6649 stop_reply_xfree (event);
6650 QUEUE_remove_elem (stop_reply_p, q, iter);
6651 }
6652
6653 return 1;
6654}
6655
f48ff2a7 6656/* Discard all pending stop replies of inferior INF. */
c906108c 6657
74531fed 6658static void
5f4cf0bb 6659discard_pending_stop_replies (struct inferior *inf)
c906108c 6660{
722247f1 6661 struct queue_iter_param param;
f48ff2a7
YQ
6662 struct stop_reply *reply;
6663 struct remote_state *rs = get_remote_state ();
6664 struct remote_notif_state *rns = rs->notif_state;
6665
6666 /* This function can be notified when an inferior exists. When the
6667 target is not remote, the notification state is NULL. */
6668 if (rs->remote_desc == NULL)
6669 return;
6670
6671 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 6672
74531fed 6673 /* Discard the in-flight notification. */
f48ff2a7 6674 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 6675 {
722247f1 6676 stop_reply_xfree (reply);
f48ff2a7 6677 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 6678 }
c906108c 6679
722247f1
YQ
6680 param.input = inf;
6681 param.output = NULL;
74531fed
PA
6682 /* Discard the stop replies we have already pulled with
6683 vStopped. */
722247f1 6684 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
6685 remove_stop_reply_for_inferior, &param);
6686}
6687
bcc75809
YQ
6688/* If its remote state is equal to the given remote state,
6689 remove EVENT from the stop reply queue. */
6690
6691static int
6692remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
6693 QUEUE_ITER (stop_reply_p) *iter,
6694 stop_reply_p event,
6695 void *data)
6696{
19ba03f4
SM
6697 struct queue_iter_param *param = (struct queue_iter_param *) data;
6698 struct remote_state *rs = (struct remote_state *) param->input;
bcc75809
YQ
6699
6700 if (event->rs == rs)
6701 {
6702 stop_reply_xfree (event);
6703 QUEUE_remove_elem (stop_reply_p, q, iter);
6704 }
6705
6706 return 1;
6707}
6708
6709/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
6710
6711static void
bcc75809 6712discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
6713{
6714 struct queue_iter_param param;
6715
bcc75809 6716 param.input = rs;
f48ff2a7
YQ
6717 param.output = NULL;
6718 /* Discard the stop replies we have already pulled with
6719 vStopped. */
6720 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 6721 remove_stop_reply_of_remote_state, &param);
722247f1 6722}
74531fed 6723
722247f1
YQ
6724/* A parameter to pass data in and out. */
6725
6726static int
6727remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
6728 QUEUE_ITER (stop_reply_p) *iter,
6729 stop_reply_p event,
6730 void *data)
6731{
19ba03f4
SM
6732 struct queue_iter_param *param = (struct queue_iter_param *) data;
6733 ptid_t *ptid = (ptid_t *) param->input;
722247f1
YQ
6734
6735 if (ptid_match (event->ptid, *ptid))
6736 {
6737 param->output = event;
6738 QUEUE_remove_elem (stop_reply_p, q, iter);
6739 return 0;
c8e38a49 6740 }
722247f1
YQ
6741
6742 return 1;
74531fed 6743}
43ff13b4 6744
722247f1
YQ
6745/* Remove the first reply in 'stop_reply_queue' which matches
6746 PTID. */
2e9f7625 6747
722247f1
YQ
6748static struct stop_reply *
6749remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 6750{
722247f1
YQ
6751 struct queue_iter_param param;
6752
6753 param.input = &ptid;
6754 param.output = NULL;
6755
6756 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6757 remote_notif_remove_once_on_match, &param);
6758 if (notif_debug)
6759 fprintf_unfiltered (gdb_stdlog,
6760 "notif: discard queued event: 'Stop' in %s\n",
6761 target_pid_to_str (ptid));
a744cf53 6762
722247f1 6763 return param.output;
74531fed 6764}
75c99385 6765
74531fed
PA
6766/* Look for a queued stop reply belonging to PTID. If one is found,
6767 remove it from the queue, and return it. Returns NULL if none is
6768 found. If there are still queued events left to process, tell the
6769 event loop to get back to target_wait soon. */
e24a49d8 6770
74531fed
PA
6771static struct stop_reply *
6772queued_stop_reply (ptid_t ptid)
6773{
722247f1 6774 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 6775
722247f1 6776 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
6777 /* There's still at least an event left. */
6778 mark_async_event_handler (remote_async_inferior_event_token);
6779
722247f1 6780 return r;
74531fed
PA
6781}
6782
6783/* Push a fully parsed stop reply in the stop reply queue. Since we
6784 know that we now have at least one queued event left to pass to the
6785 core side, tell the event loop to get back to target_wait soon. */
6786
6787static void
6788push_stop_reply (struct stop_reply *new_event)
6789{
722247f1 6790 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 6791
722247f1
YQ
6792 if (notif_debug)
6793 fprintf_unfiltered (gdb_stdlog,
6794 "notif: push 'Stop' %s to queue %d\n",
6795 target_pid_to_str (new_event->ptid),
6796 QUEUE_length (stop_reply_p,
6797 stop_reply_queue));
74531fed
PA
6798
6799 mark_async_event_handler (remote_async_inferior_event_token);
6800}
6801
722247f1
YQ
6802static int
6803stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
6804 QUEUE_ITER (stop_reply_p) *iter,
6805 struct stop_reply *event,
6806 void *data)
6807{
19ba03f4 6808 ptid_t *ptid = (ptid_t *) data;
722247f1
YQ
6809
6810 return !(ptid_equal (*ptid, event->ptid)
6811 && event->ws.kind == TARGET_WAITKIND_STOPPED);
6812}
6813
74531fed
PA
6814/* Returns true if we have a stop reply for PTID. */
6815
6816static int
6817peek_stop_reply (ptid_t ptid)
6818{
722247f1
YQ
6819 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
6820 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
6821}
6822
26d56a93
SL
6823/* Helper for remote_parse_stop_reply. Return nonzero if the substring
6824 starting with P and ending with PEND matches PREFIX. */
6825
6826static int
6827strprefix (const char *p, const char *pend, const char *prefix)
6828{
6829 for ( ; p < pend; p++, prefix++)
6830 if (*p != *prefix)
6831 return 0;
6832 return *prefix == '\0';
6833}
6834
74531fed
PA
6835/* Parse the stop reply in BUF. Either the function succeeds, and the
6836 result is stored in EVENT, or throws an error. */
6837
6838static void
6839remote_parse_stop_reply (char *buf, struct stop_reply *event)
6840{
6841 struct remote_arch_state *rsa = get_remote_arch_state ();
6842 ULONGEST addr;
256642e8 6843 const char *p;
94585166 6844 int skipregs = 0;
74531fed
PA
6845
6846 event->ptid = null_ptid;
bcc75809 6847 event->rs = get_remote_state ();
74531fed
PA
6848 event->ws.kind = TARGET_WAITKIND_IGNORE;
6849 event->ws.value.integer = 0;
f7e6eed5 6850 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 6851 event->regcache = NULL;
dc146f7c 6852 event->core = -1;
74531fed
PA
6853
6854 switch (buf[0])
6855 {
6856 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
6857 /* Expedited reply, containing Signal, {regno, reg} repeat. */
6858 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
6859 ss = signal number
6860 n... = register number
6861 r... = register contents
6862 */
6863
6864 p = &buf[3]; /* after Txx */
6865 while (*p)
6866 {
256642e8 6867 const char *p1;
cea39f65 6868 int fieldsize;
43ff13b4 6869
1f10ba14
PA
6870 p1 = strchr (p, ':');
6871 if (p1 == NULL)
6872 error (_("Malformed packet(a) (missing colon): %s\n\
6873Packet: '%s'\n"),
6874 p, buf);
6875 if (p == p1)
6876 error (_("Malformed packet(a) (missing register number): %s\n\
6877Packet: '%s'\n"),
6878 p, buf);
3c3bea1c 6879
1f10ba14
PA
6880 /* Some "registers" are actually extended stop information.
6881 Note if you're adding a new entry here: GDB 7.9 and
6882 earlier assume that all register "numbers" that start
6883 with an hex digit are real register numbers. Make sure
6884 the server only sends such a packet if it knows the
6885 client understands it. */
c8e38a49 6886
26d56a93 6887 if (strprefix (p, p1, "thread"))
1f10ba14 6888 event->ptid = read_ptid (++p1, &p);
82075af2
JS
6889 else if (strprefix (p, p1, "syscall_entry"))
6890 {
6891 ULONGEST sysno;
6892
6893 event->ws.kind = TARGET_WAITKIND_SYSCALL_ENTRY;
6894 p = unpack_varlen_hex (++p1, &sysno);
6895 event->ws.value.syscall_number = (int) sysno;
6896 }
6897 else if (strprefix (p, p1, "syscall_return"))
6898 {
6899 ULONGEST sysno;
6900
6901 event->ws.kind = TARGET_WAITKIND_SYSCALL_RETURN;
6902 p = unpack_varlen_hex (++p1, &sysno);
6903 event->ws.value.syscall_number = (int) sysno;
6904 }
26d56a93
SL
6905 else if (strprefix (p, p1, "watch")
6906 || strprefix (p, p1, "rwatch")
6907 || strprefix (p, p1, "awatch"))
cea39f65 6908 {
f7e6eed5 6909 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
6910 p = unpack_varlen_hex (++p1, &addr);
6911 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 6912 }
26d56a93 6913 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
6914 {
6915 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
6916
6917 /* Make sure the stub doesn't forget to indicate support
6918 with qSupported. */
6919 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
6920 error (_("Unexpected swbreak stop reason"));
6921
6922 /* The value part is documented as "must be empty",
6923 though we ignore it, in case we ever decide to make
6924 use of it in a backward compatible way. */
8424cc97 6925 p = strchrnul (p1 + 1, ';');
f7e6eed5 6926 }
26d56a93 6927 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
6928 {
6929 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
6930
6931 /* Make sure the stub doesn't forget to indicate support
6932 with qSupported. */
6933 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
6934 error (_("Unexpected hwbreak stop reason"));
6935
6936 /* See above. */
8424cc97 6937 p = strchrnul (p1 + 1, ';');
f7e6eed5 6938 }
26d56a93 6939 else if (strprefix (p, p1, "library"))
cea39f65 6940 {
1f10ba14 6941 event->ws.kind = TARGET_WAITKIND_LOADED;
8424cc97 6942 p = strchrnul (p1 + 1, ';');
1f10ba14 6943 }
26d56a93 6944 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
6945 {
6946 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
6947 /* p1 will indicate "begin" or "end", but it makes
6948 no difference for now, so ignore it. */
8424cc97 6949 p = strchrnul (p1 + 1, ';');
1f10ba14 6950 }
26d56a93 6951 else if (strprefix (p, p1, "core"))
1f10ba14
PA
6952 {
6953 ULONGEST c;
a744cf53 6954
1f10ba14
PA
6955 p = unpack_varlen_hex (++p1, &c);
6956 event->core = c;
cea39f65 6957 }
26d56a93 6958 else if (strprefix (p, p1, "fork"))
de0d863e
DB
6959 {
6960 event->ws.value.related_pid = read_ptid (++p1, &p);
6961 event->ws.kind = TARGET_WAITKIND_FORKED;
6962 }
26d56a93 6963 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
6964 {
6965 event->ws.value.related_pid = read_ptid (++p1, &p);
6966 event->ws.kind = TARGET_WAITKIND_VFORKED;
6967 }
26d56a93 6968 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
6969 {
6970 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
8424cc97 6971 p = strchrnul (p1 + 1, ';');
c269dbdb 6972 }
6ab24463 6973 else if (strprefix (p, p1, "exec"))
94585166
DB
6974 {
6975 ULONGEST ignored;
6976 char pathname[PATH_MAX];
6977 int pathlen;
6978
6979 /* Determine the length of the execd pathname. */
6980 p = unpack_varlen_hex (++p1, &ignored);
6981 pathlen = (p - p1) / 2;
6982
6983 /* Save the pathname for event reporting and for
6984 the next run command. */
6985 hex2bin (p1, (gdb_byte *) pathname, pathlen);
6986 pathname[pathlen] = '\0';
6987
6988 /* This is freed during event handling. */
6989 event->ws.value.execd_pathname = xstrdup (pathname);
6990 event->ws.kind = TARGET_WAITKIND_EXECD;
6991
6992 /* Skip the registers included in this packet, since
6993 they may be for an architecture different from the
6994 one used by the original program. */
6995 skipregs = 1;
6996 }
65706a29
PA
6997 else if (strprefix (p, p1, "create"))
6998 {
6999 event->ws.kind = TARGET_WAITKIND_THREAD_CREATED;
8424cc97 7000 p = strchrnul (p1 + 1, ';');
65706a29 7001 }
cea39f65
MS
7002 else
7003 {
1f10ba14 7004 ULONGEST pnum;
256642e8 7005 const char *p_temp;
1f10ba14 7006
94585166
DB
7007 if (skipregs)
7008 {
8424cc97 7009 p = strchrnul (p1 + 1, ';');
94585166
DB
7010 p++;
7011 continue;
7012 }
7013
1f10ba14
PA
7014 /* Maybe a real ``P'' register number. */
7015 p_temp = unpack_varlen_hex (p, &pnum);
7016 /* If the first invalid character is the colon, we got a
7017 register number. Otherwise, it's an unknown stop
7018 reason. */
7019 if (p_temp == p1)
7020 {
7021 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
7022 cached_reg_t cached_reg;
d1dff226 7023 struct gdbarch *gdbarch = target_gdbarch ();
43ff13b4 7024
1f10ba14
PA
7025 if (reg == NULL)
7026 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 7027Packet: '%s'\n"),
1f10ba14 7028 hex_string (pnum), p, buf);
c8e38a49 7029
1f10ba14 7030 cached_reg.num = reg->regnum;
d1dff226
AH
7031 cached_reg.data = (gdb_byte *)
7032 xmalloc (register_size (gdbarch, reg->regnum));
4100683b 7033
1f10ba14
PA
7034 p = p1 + 1;
7035 fieldsize = hex2bin (p, cached_reg.data,
d1dff226 7036 register_size (gdbarch, reg->regnum));
1f10ba14 7037 p += 2 * fieldsize;
d1dff226 7038 if (fieldsize < register_size (gdbarch, reg->regnum))
1f10ba14 7039 warning (_("Remote reply is too short: %s"), buf);
74531fed 7040
1f10ba14
PA
7041 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
7042 }
7043 else
7044 {
7045 /* Not a number. Silently skip unknown optional
7046 info. */
8424cc97 7047 p = strchrnul (p1 + 1, ';');
1f10ba14 7048 }
cea39f65 7049 }
c8e38a49 7050
cea39f65
MS
7051 if (*p != ';')
7052 error (_("Remote register badly formatted: %s\nhere: %s"),
7053 buf, p);
7054 ++p;
7055 }
5b5596ff
PA
7056
7057 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
7058 break;
7059
c8e38a49
PA
7060 /* fall through */
7061 case 'S': /* Old style status, just signal only. */
3a09da41
PA
7062 {
7063 int sig;
7064
7065 event->ws.kind = TARGET_WAITKIND_STOPPED;
7066 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
7067 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
7068 event->ws.value.sig = (enum gdb_signal) sig;
7069 else
7070 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
7071 }
c8e38a49 7072 break;
65706a29
PA
7073 case 'w': /* Thread exited. */
7074 {
256642e8 7075 const char *p;
65706a29
PA
7076 ULONGEST value;
7077
7078 event->ws.kind = TARGET_WAITKIND_THREAD_EXITED;
7079 p = unpack_varlen_hex (&buf[1], &value);
7080 event->ws.value.integer = value;
7081 if (*p != ';')
7082 error (_("stop reply packet badly formatted: %s"), buf);
974eac9d 7083 event->ptid = read_ptid (++p, NULL);
65706a29
PA
7084 break;
7085 }
c8e38a49
PA
7086 case 'W': /* Target exited. */
7087 case 'X':
7088 {
256642e8 7089 const char *p;
c8e38a49
PA
7090 int pid;
7091 ULONGEST value;
82f73884 7092
c8e38a49
PA
7093 /* GDB used to accept only 2 hex chars here. Stubs should
7094 only send more if they detect GDB supports multi-process
7095 support. */
7096 p = unpack_varlen_hex (&buf[1], &value);
82f73884 7097
c8e38a49
PA
7098 if (buf[0] == 'W')
7099 {
7100 /* The remote process exited. */
74531fed
PA
7101 event->ws.kind = TARGET_WAITKIND_EXITED;
7102 event->ws.value.integer = value;
c8e38a49
PA
7103 }
7104 else
7105 {
7106 /* The remote process exited with a signal. */
74531fed 7107 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
7108 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
7109 event->ws.value.sig = (enum gdb_signal) value;
7110 else
7111 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 7112 }
82f73884 7113
c8e38a49
PA
7114 /* If no process is specified, assume inferior_ptid. */
7115 pid = ptid_get_pid (inferior_ptid);
7116 if (*p == '\0')
7117 ;
7118 else if (*p == ';')
7119 {
7120 p++;
7121
0b24eb2d 7122 if (*p == '\0')
82f73884 7123 ;
61012eef 7124 else if (startswith (p, "process:"))
82f73884 7125 {
c8e38a49 7126 ULONGEST upid;
a744cf53 7127
c8e38a49
PA
7128 p += sizeof ("process:") - 1;
7129 unpack_varlen_hex (p, &upid);
7130 pid = upid;
82f73884
PA
7131 }
7132 else
7133 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 7134 }
c8e38a49
PA
7135 else
7136 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
7137 event->ptid = pid_to_ptid (pid);
7138 }
7139 break;
f2faf941
PA
7140 case 'N':
7141 event->ws.kind = TARGET_WAITKIND_NO_RESUMED;
7142 event->ptid = minus_one_ptid;
7143 break;
74531fed
PA
7144 }
7145
6efcd9a8 7146 if (target_is_non_stop_p () && ptid_equal (event->ptid, null_ptid))
74531fed
PA
7147 error (_("No process or thread specified in stop reply: %s"), buf);
7148}
7149
722247f1
YQ
7150/* When the stub wants to tell GDB about a new notification reply, it
7151 sends a notification (%Stop, for example). Those can come it at
7152 any time, hence, we have to make sure that any pending
7153 putpkt/getpkt sequence we're making is finished, before querying
7154 the stub for more events with the corresponding ack command
7155 (vStopped, for example). E.g., if we started a vStopped sequence
7156 immediately upon receiving the notification, something like this
7157 could happen:
74531fed
PA
7158
7159 1.1) --> Hg 1
7160 1.2) <-- OK
7161 1.3) --> g
7162 1.4) <-- %Stop
7163 1.5) --> vStopped
7164 1.6) <-- (registers reply to step #1.3)
7165
7166 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7167 query.
7168
796cb314 7169 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
7170 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7171 doing whatever we were doing:
7172
7173 2.1) --> Hg 1
7174 2.2) <-- OK
7175 2.3) --> g
7176 2.4) <-- %Stop
7177 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7178 2.5) <-- (registers reply to step #2.3)
7179
7180 Eventualy after step #2.5, we return to the event loop, which
7181 notices there's an event on the
7182 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7183 associated callback --- the function below. At this point, we're
7184 always safe to start a vStopped sequence. :
7185
7186 2.6) --> vStopped
7187 2.7) <-- T05 thread:2
7188 2.8) --> vStopped
7189 2.9) --> OK
7190*/
7191
722247f1
YQ
7192void
7193remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
7194{
7195 struct remote_state *rs = get_remote_state ();
74531fed 7196
f48ff2a7 7197 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 7198 {
722247f1
YQ
7199 if (notif_debug)
7200 fprintf_unfiltered (gdb_stdlog,
7201 "notif: process: '%s' ack pending event\n",
7202 nc->name);
74531fed 7203
722247f1 7204 /* acknowledge */
f48ff2a7
YQ
7205 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
7206 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
7207
7208 while (1)
7209 {
7210 getpkt (&rs->buf, &rs->buf_size, 0);
7211 if (strcmp (rs->buf, "OK") == 0)
7212 break;
7213 else
722247f1 7214 remote_notif_ack (nc, rs->buf);
74531fed
PA
7215 }
7216 }
722247f1
YQ
7217 else
7218 {
7219 if (notif_debug)
7220 fprintf_unfiltered (gdb_stdlog,
7221 "notif: process: '%s' no pending reply\n",
7222 nc->name);
7223 }
74531fed
PA
7224}
7225
74531fed
PA
7226/* Called when it is decided that STOP_REPLY holds the info of the
7227 event that is to be returned to the core. This function always
7228 destroys STOP_REPLY. */
7229
7230static ptid_t
7231process_stop_reply (struct stop_reply *stop_reply,
7232 struct target_waitstatus *status)
7233{
7234 ptid_t ptid;
7235
7236 *status = stop_reply->ws;
7237 ptid = stop_reply->ptid;
7238
7239 /* If no thread/process was reported by the stub, assume the current
7240 inferior. */
7241 if (ptid_equal (ptid, null_ptid))
7242 ptid = inferior_ptid;
7243
5f3563ea 7244 if (status->kind != TARGET_WAITKIND_EXITED
f2faf941
PA
7245 && status->kind != TARGET_WAITKIND_SIGNALLED
7246 && status->kind != TARGET_WAITKIND_NO_RESUMED)
74531fed 7247 {
799a2abe 7248 struct private_thread_info *remote_thr;
ee154bee 7249
5f3563ea
PA
7250 /* Expedited registers. */
7251 if (stop_reply->regcache)
7252 {
217f1f79 7253 struct regcache *regcache
f5656ead 7254 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
7255 cached_reg_t *reg;
7256 int ix;
7257
7258 for (ix = 0;
d1dff226 7259 VEC_iterate (cached_reg_t, stop_reply->regcache, ix, reg);
5f3563ea 7260 ix++)
d1dff226 7261 {
217f1f79 7262 regcache_raw_supply (regcache, reg->num, reg->data);
d1dff226
AH
7263 xfree (reg->data);
7264 }
7265
5f3563ea
PA
7266 VEC_free (cached_reg_t, stop_reply->regcache);
7267 }
74531fed 7268
1941c569 7269 remote_notice_new_inferior (ptid, 0);
85ad3aaf 7270 remote_thr = get_private_info_ptid (ptid);
799a2abe
PA
7271 remote_thr->core = stop_reply->core;
7272 remote_thr->stop_reason = stop_reply->stop_reason;
7273 remote_thr->watch_data_address = stop_reply->watch_data_address;
85ad3aaf 7274 remote_thr->vcont_resumed = 0;
74531fed
PA
7275 }
7276
74531fed
PA
7277 stop_reply_xfree (stop_reply);
7278 return ptid;
7279}
7280
7281/* The non-stop mode version of target_wait. */
7282
7283static ptid_t
47608cb1 7284remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7285{
7286 struct remote_state *rs = get_remote_state ();
74531fed
PA
7287 struct stop_reply *stop_reply;
7288 int ret;
fee9eda9 7289 int is_notif = 0;
74531fed
PA
7290
7291 /* If in non-stop mode, get out of getpkt even if a
7292 notification is received. */
7293
7294 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7295 0 /* forever */, &is_notif);
74531fed
PA
7296 while (1)
7297 {
fee9eda9 7298 if (ret != -1 && !is_notif)
74531fed
PA
7299 switch (rs->buf[0])
7300 {
7301 case 'E': /* Error of some sort. */
7302 /* We're out of sync with the target now. Did it continue
7303 or not? We can't tell which thread it was in non-stop,
7304 so just ignore this. */
7305 warning (_("Remote failure reply: %s"), rs->buf);
7306 break;
7307 case 'O': /* Console output. */
7308 remote_console_output (rs->buf + 1);
7309 break;
7310 default:
7311 warning (_("Invalid remote reply: %s"), rs->buf);
7312 break;
7313 }
7314
7315 /* Acknowledge a pending stop reply that may have arrived in the
7316 mean time. */
f48ff2a7 7317 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 7318 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
7319
7320 /* If indeed we noticed a stop reply, we're done. */
7321 stop_reply = queued_stop_reply (ptid);
7322 if (stop_reply != NULL)
7323 return process_stop_reply (stop_reply, status);
7324
47608cb1 7325 /* Still no event. If we're just polling for an event, then
74531fed 7326 return to the event loop. */
47608cb1 7327 if (options & TARGET_WNOHANG)
74531fed
PA
7328 {
7329 status->kind = TARGET_WAITKIND_IGNORE;
7330 return minus_one_ptid;
7331 }
7332
47608cb1 7333 /* Otherwise do a blocking wait. */
74531fed 7334 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7335 1 /* forever */, &is_notif);
74531fed
PA
7336 }
7337}
7338
7339/* Wait until the remote machine stops, then return, storing status in
7340 STATUS just as `wait' would. */
7341
7342static ptid_t
47608cb1 7343remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7344{
7345 struct remote_state *rs = get_remote_state ();
74531fed 7346 ptid_t event_ptid = null_ptid;
cea39f65 7347 char *buf;
74531fed
PA
7348 struct stop_reply *stop_reply;
7349
47608cb1
PA
7350 again:
7351
74531fed
PA
7352 status->kind = TARGET_WAITKIND_IGNORE;
7353 status->value.integer = 0;
7354
7355 stop_reply = queued_stop_reply (ptid);
7356 if (stop_reply != NULL)
7357 return process_stop_reply (stop_reply, status);
7358
7359 if (rs->cached_wait_status)
7360 /* Use the cached wait status, but only once. */
7361 rs->cached_wait_status = 0;
7362 else
7363 {
7364 int ret;
722247f1 7365 int is_notif;
567420d1
PA
7366 int forever = ((options & TARGET_WNOHANG) == 0
7367 && wait_forever_enabled_p);
7368
7369 if (!rs->waiting_for_stop_reply)
7370 {
7371 status->kind = TARGET_WAITKIND_NO_RESUMED;
7372 return minus_one_ptid;
7373 }
74531fed 7374
74531fed
PA
7375 /* FIXME: cagney/1999-09-27: If we're in async mode we should
7376 _never_ wait for ever -> test on target_is_async_p().
7377 However, before we do that we need to ensure that the caller
7378 knows how to take the target into/out of async mode. */
722247f1 7379 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 7380 forever, &is_notif);
722247f1
YQ
7381
7382 /* GDB gets a notification. Return to core as this event is
7383 not interesting. */
7384 if (ret != -1 && is_notif)
7385 return minus_one_ptid;
567420d1
PA
7386
7387 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
7388 return minus_one_ptid;
74531fed
PA
7389 }
7390
7391 buf = rs->buf;
7392
3a29589a
DJ
7393 /* Assume that the target has acknowledged Ctrl-C unless we receive
7394 an 'F' or 'O' packet. */
7395 if (buf[0] != 'F' && buf[0] != 'O')
7396 rs->ctrlc_pending_p = 0;
7397
74531fed
PA
7398 switch (buf[0])
7399 {
7400 case 'E': /* Error of some sort. */
7401 /* We're out of sync with the target now. Did it continue or
7402 not? Not is more likely, so report a stop. */
29090fb6
LM
7403 rs->waiting_for_stop_reply = 0;
7404
74531fed
PA
7405 warning (_("Remote failure reply: %s"), buf);
7406 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 7407 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
7408 break;
7409 case 'F': /* File-I/O request. */
e42e5352
YQ
7410 /* GDB may access the inferior memory while handling the File-I/O
7411 request, but we don't want GDB accessing memory while waiting
7412 for a stop reply. See the comments in putpkt_binary. Set
7413 waiting_for_stop_reply to 0 temporarily. */
7414 rs->waiting_for_stop_reply = 0;
3a29589a
DJ
7415 remote_fileio_request (buf, rs->ctrlc_pending_p);
7416 rs->ctrlc_pending_p = 0;
e42e5352
YQ
7417 /* GDB handled the File-I/O request, and the target is running
7418 again. Keep waiting for events. */
7419 rs->waiting_for_stop_reply = 1;
74531fed 7420 break;
f2faf941 7421 case 'N': case 'T': case 'S': case 'X': case 'W':
74531fed 7422 {
29090fb6
LM
7423 struct stop_reply *stop_reply;
7424
7425 /* There is a stop reply to handle. */
7426 rs->waiting_for_stop_reply = 0;
7427
7428 stop_reply
722247f1
YQ
7429 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
7430 rs->buf);
74531fed 7431
74531fed 7432 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
7433 break;
7434 }
7435 case 'O': /* Console output. */
7436 remote_console_output (buf + 1);
c8e38a49
PA
7437 break;
7438 case '\0':
b73be471 7439 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
7440 {
7441 /* Zero length reply means that we tried 'S' or 'C' and the
7442 remote system doesn't support it. */
223ffa71 7443 target_terminal::ours_for_output ();
c8e38a49
PA
7444 printf_filtered
7445 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
7446 gdb_signal_to_name (rs->last_sent_signal));
7447 rs->last_sent_signal = GDB_SIGNAL_0;
223ffa71 7448 target_terminal::inferior ();
c8e38a49 7449
f5c4fcd9
TT
7450 strcpy (buf, rs->last_sent_step ? "s" : "c");
7451 putpkt (buf);
c8e38a49 7452 break;
43ff13b4 7453 }
c8e38a49
PA
7454 /* else fallthrough */
7455 default:
7456 warning (_("Invalid remote reply: %s"), buf);
c8e38a49 7457 break;
43ff13b4 7458 }
c8e38a49 7459
f2faf941
PA
7460 if (status->kind == TARGET_WAITKIND_NO_RESUMED)
7461 return minus_one_ptid;
7462 else if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
7463 {
7464 /* Nothing interesting happened. If we're doing a non-blocking
7465 poll, we're done. Otherwise, go back to waiting. */
7466 if (options & TARGET_WNOHANG)
7467 return minus_one_ptid;
7468 else
7469 goto again;
7470 }
74531fed
PA
7471 else if (status->kind != TARGET_WAITKIND_EXITED
7472 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
7473 {
7474 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 7475 record_currthread (rs, event_ptid);
82f73884
PA
7476 else
7477 event_ptid = inferior_ptid;
43ff13b4 7478 }
74531fed
PA
7479 else
7480 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 7481 record_currthread (rs, minus_one_ptid);
79d7f229 7482
82f73884 7483 return event_ptid;
43ff13b4
JM
7484}
7485
74531fed
PA
7486/* Wait until the remote machine stops, then return, storing status in
7487 STATUS just as `wait' would. */
7488
c8e38a49 7489static ptid_t
117de6a9 7490remote_wait (struct target_ops *ops,
47608cb1 7491 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
7492{
7493 ptid_t event_ptid;
7494
6efcd9a8 7495 if (target_is_non_stop_p ())
47608cb1 7496 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 7497 else
47608cb1 7498 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 7499
d9d41e78 7500 if (target_is_async_p ())
c8e38a49 7501 {
74531fed
PA
7502 /* If there are are events left in the queue tell the event loop
7503 to return here. */
722247f1 7504 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 7505 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 7506 }
c8e38a49
PA
7507
7508 return event_ptid;
7509}
7510
74ca34ce 7511/* Fetch a single register using a 'p' packet. */
c906108c 7512
b96ec7ac 7513static int
56be3814 7514fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac 7515{
9890e433 7516 struct gdbarch *gdbarch = get_regcache_arch (regcache);
b96ec7ac 7517 struct remote_state *rs = get_remote_state ();
2e9f7625 7518 char *buf, *p;
9890e433 7519 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
b96ec7ac
AC
7520 int i;
7521
4082afcc 7522 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
7523 return 0;
7524
7525 if (reg->pnum == -1)
7526 return 0;
7527
2e9f7625 7528 p = rs->buf;
fcad0fa4 7529 *p++ = 'p';
74ca34ce 7530 p += hexnumstr (p, reg->pnum);
fcad0fa4 7531 *p++ = '\0';
1f4437a4
MS
7532 putpkt (rs->buf);
7533 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 7534
2e9f7625
DJ
7535 buf = rs->buf;
7536
74ca34ce
DJ
7537 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
7538 {
7539 case PACKET_OK:
7540 break;
7541 case PACKET_UNKNOWN:
7542 return 0;
7543 case PACKET_ERROR:
27a9c0bf
MS
7544 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
7545 gdbarch_register_name (get_regcache_arch (regcache),
7546 reg->regnum),
7547 buf);
74ca34ce 7548 }
3f9a994c
JB
7549
7550 /* If this register is unfetchable, tell the regcache. */
7551 if (buf[0] == 'x')
8480adf2 7552 {
56be3814 7553 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 7554 return 1;
b96ec7ac 7555 }
b96ec7ac 7556
3f9a994c
JB
7557 /* Otherwise, parse and supply the value. */
7558 p = buf;
7559 i = 0;
7560 while (p[0] != 0)
7561 {
7562 if (p[1] == 0)
74ca34ce 7563 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
7564
7565 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
7566 p += 2;
7567 }
56be3814 7568 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 7569 return 1;
b96ec7ac
AC
7570}
7571
74ca34ce
DJ
7572/* Fetch the registers included in the target's 'g' packet. */
7573
29709017
DJ
7574static int
7575send_g_packet (void)
c906108c 7576{
d01949b6 7577 struct remote_state *rs = get_remote_state ();
cea39f65 7578 int buf_len;
c906108c 7579
bba74b36 7580 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 7581 remote_send (&rs->buf, &rs->buf_size);
c906108c 7582
29709017
DJ
7583 /* We can get out of synch in various cases. If the first character
7584 in the buffer is not a hex character, assume that has happened
7585 and try to fetch another packet to read. */
7586 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
7587 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
7588 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
7589 && rs->buf[0] != 'x') /* New: unavailable register value. */
7590 {
7591 if (remote_debug)
7592 fprintf_unfiltered (gdb_stdlog,
7593 "Bad register packet; fetching a new packet\n");
7594 getpkt (&rs->buf, &rs->buf_size, 0);
7595 }
7596
74ca34ce
DJ
7597 buf_len = strlen (rs->buf);
7598
7599 /* Sanity check the received packet. */
7600 if (buf_len % 2 != 0)
7601 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
7602
7603 return buf_len / 2;
7604}
7605
7606static void
56be3814 7607process_g_packet (struct regcache *regcache)
29709017 7608{
4a22f64d 7609 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
7610 struct remote_state *rs = get_remote_state ();
7611 struct remote_arch_state *rsa = get_remote_arch_state ();
7612 int i, buf_len;
7613 char *p;
7614 char *regs;
7615
7616 buf_len = strlen (rs->buf);
7617
7618 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce 7619 if (buf_len > 2 * rsa->sizeof_g_packet)
fc809827
SM
7620 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
7621 "bytes): %s"), rsa->sizeof_g_packet, buf_len / 2, rs->buf);
74ca34ce
DJ
7622
7623 /* Save the size of the packet sent to us by the target. It is used
7624 as a heuristic when determining the max size of packets that the
7625 target can safely receive. */
7626 if (rsa->actual_register_packet_size == 0)
7627 rsa->actual_register_packet_size = buf_len;
7628
7629 /* If this is smaller than we guessed the 'g' packet would be,
7630 update our records. A 'g' reply that doesn't include a register's
7631 value implies either that the register is not available, or that
7632 the 'p' packet must be used. */
7633 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 7634 {
9dc193c3 7635 long sizeof_g_packet = buf_len / 2;
74ca34ce 7636
4a22f64d 7637 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 7638 {
9dc193c3
LF
7639 long offset = rsa->regs[i].offset;
7640 long reg_size = register_size (gdbarch, i);
7641
74ca34ce
DJ
7642 if (rsa->regs[i].pnum == -1)
7643 continue;
7644
9dc193c3 7645 if (offset >= sizeof_g_packet)
74ca34ce 7646 rsa->regs[i].in_g_packet = 0;
9dc193c3
LF
7647 else if (offset + reg_size > sizeof_g_packet)
7648 error (_("Truncated register %d in remote 'g' packet"), i);
b96ec7ac 7649 else
74ca34ce 7650 rsa->regs[i].in_g_packet = 1;
b96ec7ac 7651 }
9dc193c3
LF
7652
7653 /* Looks valid enough, we can assume this is the correct length
7654 for a 'g' packet. It's important not to adjust
7655 rsa->sizeof_g_packet if we have truncated registers otherwise
7656 this "if" won't be run the next time the method is called
7657 with a packet of the same size and one of the internal errors
7658 below will trigger instead. */
7659 rsa->sizeof_g_packet = sizeof_g_packet;
74ca34ce 7660 }
b323314b 7661
224c3ddb 7662 regs = (char *) alloca (rsa->sizeof_g_packet);
c906108c
SS
7663
7664 /* Unimplemented registers read as all bits zero. */
ea9c271d 7665 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 7666
c906108c
SS
7667 /* Reply describes registers byte by byte, each byte encoded as two
7668 hex characters. Suck them all up, then supply them to the
7669 register cacheing/storage mechanism. */
7670
74ca34ce 7671 p = rs->buf;
ea9c271d 7672 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 7673 {
74ca34ce
DJ
7674 if (p[0] == 0 || p[1] == 0)
7675 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
7676 internal_error (__FILE__, __LINE__,
9b20d036 7677 _("unexpected end of 'g' packet reply"));
74ca34ce 7678
c906108c 7679 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 7680 regs[i] = 0; /* 'x' */
c906108c
SS
7681 else
7682 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
7683 p += 2;
7684 }
7685
a744cf53
MS
7686 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
7687 {
7688 struct packet_reg *r = &rsa->regs[i];
9dc193c3 7689 long reg_size = register_size (gdbarch, i);
a744cf53
MS
7690
7691 if (r->in_g_packet)
7692 {
9dc193c3 7693 if ((r->offset + reg_size) * 2 > strlen (rs->buf))
a744cf53
MS
7694 /* This shouldn't happen - we adjusted in_g_packet above. */
7695 internal_error (__FILE__, __LINE__,
9b20d036 7696 _("unexpected end of 'g' packet reply"));
a744cf53
MS
7697 else if (rs->buf[r->offset * 2] == 'x')
7698 {
7699 gdb_assert (r->offset * 2 < strlen (rs->buf));
7700 /* The register isn't available, mark it as such (at
7701 the same time setting the value to zero). */
7702 regcache_raw_supply (regcache, r->regnum, NULL);
7703 }
7704 else
7705 regcache_raw_supply (regcache, r->regnum,
7706 regs + r->offset);
7707 }
7708 }
c906108c
SS
7709}
7710
29709017 7711static void
56be3814 7712fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
7713{
7714 send_g_packet ();
56be3814 7715 process_g_packet (regcache);
29709017
DJ
7716}
7717
e6e4e701
PA
7718/* Make the remote selected traceframe match GDB's selected
7719 traceframe. */
7720
7721static void
7722set_remote_traceframe (void)
7723{
7724 int newnum;
262e1174 7725 struct remote_state *rs = get_remote_state ();
e6e4e701 7726
262e1174 7727 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
7728 return;
7729
7730 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 7731 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
7732
7733 newnum = target_trace_find (tfind_number,
7734 get_traceframe_number (), 0, 0, NULL);
7735
7736 /* Should not happen. If it does, all bets are off. */
7737 if (newnum != get_traceframe_number ())
7738 warning (_("could not set remote traceframe"));
7739}
7740
74ca34ce 7741static void
28439f5e
PA
7742remote_fetch_registers (struct target_ops *ops,
7743 struct regcache *regcache, int regnum)
74ca34ce 7744{
74ca34ce
DJ
7745 struct remote_arch_state *rsa = get_remote_arch_state ();
7746 int i;
7747
e6e4e701 7748 set_remote_traceframe ();
bcc0c096 7749 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
7750
7751 if (regnum >= 0)
7752 {
7753 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 7754
74ca34ce
DJ
7755 gdb_assert (reg != NULL);
7756
7757 /* If this register might be in the 'g' packet, try that first -
7758 we are likely to read more than one register. If this is the
7759 first 'g' packet, we might be overly optimistic about its
7760 contents, so fall back to 'p'. */
7761 if (reg->in_g_packet)
7762 {
56be3814 7763 fetch_registers_using_g (regcache);
74ca34ce
DJ
7764 if (reg->in_g_packet)
7765 return;
7766 }
7767
56be3814 7768 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
7769 return;
7770
7771 /* This register is not available. */
56be3814 7772 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
7773
7774 return;
7775 }
7776
56be3814 7777 fetch_registers_using_g (regcache);
74ca34ce 7778
4a22f64d 7779 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 7780 if (!rsa->regs[i].in_g_packet)
56be3814 7781 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
7782 {
7783 /* This register is not available. */
56be3814 7784 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
7785 }
7786}
7787
c906108c
SS
7788/* Prepare to store registers. Since we may send them all (using a
7789 'G' request), we have to read out the ones we don't want to change
7790 first. */
7791
c5aa993b 7792static void
f32dbf8c 7793remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 7794{
ea9c271d 7795 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 7796 int i;
cf0e1e0d 7797
c906108c 7798 /* Make sure the entire registers array is valid. */
4082afcc 7799 switch (packet_support (PACKET_P))
5a2468f5
JM
7800 {
7801 case PACKET_DISABLE:
7802 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 7803 /* Make sure all the necessary registers are cached. */
4a22f64d 7804 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 7805 if (rsa->regs[i].in_g_packet)
8e368124 7806 regcache_raw_update (regcache, rsa->regs[i].regnum);
5a2468f5
JM
7807 break;
7808 case PACKET_ENABLE:
7809 break;
7810 }
7811}
7812
ad10f812 7813/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 7814 packet was not recognized. */
5a2468f5
JM
7815
7816static int
1f4437a4
MS
7817store_register_using_P (const struct regcache *regcache,
7818 struct packet_reg *reg)
5a2468f5 7819{
4a22f64d 7820 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 7821 struct remote_state *rs = get_remote_state ();
5a2468f5 7822 /* Try storing a single register. */
6d820c5c 7823 char *buf = rs->buf;
9890e433 7824 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
5a2468f5 7825 char *p;
5a2468f5 7826
4082afcc 7827 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
7828 return 0;
7829
7830 if (reg->pnum == -1)
7831 return 0;
7832
ea9c271d 7833 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 7834 p = buf + strlen (buf);
56be3814 7835 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 7836 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
7837 putpkt (rs->buf);
7838 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 7839
74ca34ce
DJ
7840 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
7841 {
7842 case PACKET_OK:
7843 return 1;
7844 case PACKET_ERROR:
27a9c0bf
MS
7845 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
7846 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
7847 case PACKET_UNKNOWN:
7848 return 0;
7849 default:
7850 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7851 }
c906108c
SS
7852}
7853
23860348
MS
7854/* Store register REGNUM, or all registers if REGNUM == -1, from the
7855 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
7856
7857static void
56be3814 7858store_registers_using_G (const struct regcache *regcache)
c906108c 7859{
d01949b6 7860 struct remote_state *rs = get_remote_state ();
ea9c271d 7861 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 7862 gdb_byte *regs;
c906108c
SS
7863 char *p;
7864
193cb69f
AC
7865 /* Extract all the registers in the regcache copying them into a
7866 local buffer. */
7867 {
b323314b 7868 int i;
a744cf53 7869
224c3ddb 7870 regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
ea9c271d 7871 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 7872 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 7873 {
ea9c271d 7874 struct packet_reg *r = &rsa->regs[i];
a744cf53 7875
b323314b 7876 if (r->in_g_packet)
56be3814 7877 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
7878 }
7879 }
c906108c
SS
7880
7881 /* Command describes registers byte by byte,
7882 each byte encoded as two hex characters. */
6d820c5c 7883 p = rs->buf;
193cb69f 7884 *p++ = 'G';
74ca34ce 7885 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
7886 putpkt (rs->buf);
7887 getpkt (&rs->buf, &rs->buf_size, 0);
7888 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
7889 error (_("Could not write registers; remote failure reply '%s'"),
7890 rs->buf);
c906108c 7891}
74ca34ce
DJ
7892
7893/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
7894 of the register cache buffer. FIXME: ignores errors. */
7895
7896static void
28439f5e
PA
7897remote_store_registers (struct target_ops *ops,
7898 struct regcache *regcache, int regnum)
74ca34ce 7899{
74ca34ce
DJ
7900 struct remote_arch_state *rsa = get_remote_arch_state ();
7901 int i;
7902
e6e4e701 7903 set_remote_traceframe ();
bcc0c096 7904 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
7905
7906 if (regnum >= 0)
7907 {
7908 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 7909
74ca34ce
DJ
7910 gdb_assert (reg != NULL);
7911
7912 /* Always prefer to store registers using the 'P' packet if
7913 possible; we often change only a small number of registers.
7914 Sometimes we change a larger number; we'd need help from a
7915 higher layer to know to use 'G'. */
56be3814 7916 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
7917 return;
7918
7919 /* For now, don't complain if we have no way to write the
7920 register. GDB loses track of unavailable registers too
7921 easily. Some day, this may be an error. We don't have
0df8b418 7922 any way to read the register, either... */
74ca34ce
DJ
7923 if (!reg->in_g_packet)
7924 return;
7925
56be3814 7926 store_registers_using_G (regcache);
74ca34ce
DJ
7927 return;
7928 }
7929
56be3814 7930 store_registers_using_G (regcache);
74ca34ce 7931
4a22f64d 7932 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 7933 if (!rsa->regs[i].in_g_packet)
56be3814 7934 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
7935 /* See above for why we do not issue an error here. */
7936 continue;
7937}
c906108c
SS
7938\f
7939
7940/* Return the number of hex digits in num. */
7941
7942static int
fba45db2 7943hexnumlen (ULONGEST num)
c906108c
SS
7944{
7945 int i;
7946
7947 for (i = 0; num != 0; i++)
7948 num >>= 4;
7949
325fac50 7950 return std::max (i, 1);
c906108c
SS
7951}
7952
2df3850c 7953/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
7954
7955static int
fba45db2 7956hexnumstr (char *buf, ULONGEST num)
c906108c 7957{
c906108c 7958 int len = hexnumlen (num);
a744cf53 7959
2df3850c
JM
7960 return hexnumnstr (buf, num, len);
7961}
7962
c906108c 7963
2df3850c 7964/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 7965
2df3850c 7966static int
fba45db2 7967hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
7968{
7969 int i;
7970
7971 buf[width] = '\0';
7972
7973 for (i = width - 1; i >= 0; i--)
c906108c 7974 {
c5aa993b 7975 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
7976 num >>= 4;
7977 }
7978
2df3850c 7979 return width;
c906108c
SS
7980}
7981
23860348 7982/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
7983
7984static CORE_ADDR
fba45db2 7985remote_address_masked (CORE_ADDR addr)
c906108c 7986{
883b9c6c 7987 unsigned int address_size = remote_address_size;
a744cf53 7988
911c95a5
UW
7989 /* If "remoteaddresssize" was not set, default to target address size. */
7990 if (!address_size)
f5656ead 7991 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
7992
7993 if (address_size > 0
7994 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
7995 {
7996 /* Only create a mask when that mask can safely be constructed
23860348 7997 in a ULONGEST variable. */
c906108c 7998 ULONGEST mask = 1;
a744cf53 7999
911c95a5 8000 mask = (mask << address_size) - 1;
c906108c
SS
8001 addr &= mask;
8002 }
8003 return addr;
8004}
8005
8006/* Determine whether the remote target supports binary downloading.
8007 This is accomplished by sending a no-op memory write of zero length
8008 to the target at the specified address. It does not suffice to send
23860348
MS
8009 the whole packet, since many stubs strip the eighth bit and
8010 subsequently compute a wrong checksum, which causes real havoc with
8011 remote_write_bytes.
7a292a7a 8012
96baa820 8013 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 8014 clean. In cases like this, the user should clear "remote
23860348 8015 X-packet". */
96baa820 8016
c906108c 8017static void
fba45db2 8018check_binary_download (CORE_ADDR addr)
c906108c 8019{
d01949b6 8020 struct remote_state *rs = get_remote_state ();
24b06219 8021
4082afcc 8022 switch (packet_support (PACKET_X))
c906108c 8023 {
96baa820
JM
8024 case PACKET_DISABLE:
8025 break;
8026 case PACKET_ENABLE:
8027 break;
8028 case PACKET_SUPPORT_UNKNOWN:
8029 {
96baa820 8030 char *p;
802188a7 8031
2e9f7625 8032 p = rs->buf;
96baa820
JM
8033 *p++ = 'X';
8034 p += hexnumstr (p, (ULONGEST) addr);
8035 *p++ = ',';
8036 p += hexnumstr (p, (ULONGEST) 0);
8037 *p++ = ':';
8038 *p = '\0';
802188a7 8039
2e9f7625 8040 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8041 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 8042
2e9f7625 8043 if (rs->buf[0] == '\0')
96baa820
JM
8044 {
8045 if (remote_debug)
8046 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8047 "binary downloading NOT "
8048 "supported by target\n");
444abaca 8049 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
8050 }
8051 else
8052 {
8053 if (remote_debug)
8054 fprintf_unfiltered (gdb_stdlog,
64b9b334 8055 "binary downloading supported by target\n");
444abaca 8056 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
8057 }
8058 break;
8059 }
c906108c
SS
8060 }
8061}
8062
124e13d9
SM
8063/* Helper function to resize the payload in order to try to get a good
8064 alignment. We try to write an amount of data such that the next write will
8065 start on an address aligned on REMOTE_ALIGN_WRITES. */
8066
8067static int
8068align_for_efficient_write (int todo, CORE_ADDR memaddr)
8069{
8070 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
8071}
8072
c906108c
SS
8073/* Write memory data directly to the remote machine.
8074 This does not inform the data cache; the data cache uses this.
a76d924d 8075 HEADER is the starting part of the packet.
c906108c
SS
8076 MEMADDR is the address in the remote memory space.
8077 MYADDR is the address of the buffer in our space.
124e13d9
SM
8078 LEN_UNITS is the number of addressable units to write.
8079 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
8080 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8081 should send data as binary ('X'), or hex-encoded ('M').
8082
8083 The function creates packet of the form
8084 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8085
124e13d9 8086 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
8087
8088 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8089 are omitted.
8090
9b409511 8091 Return the transferred status, error or OK (an
124e13d9
SM
8092 'enum target_xfer_status' value). Save the number of addressable units
8093 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8094
8095 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8096 exchange between gdb and the stub could look like (?? in place of the
8097 checksum):
8098
8099 -> $m1000,4#??
8100 <- aaaabbbbccccdddd
8101
8102 -> $M1000,3:eeeeffffeeee#??
8103 <- OK
8104
8105 -> $m1000,4#??
8106 <- eeeeffffeeeedddd */
c906108c 8107
9b409511 8108static enum target_xfer_status
a76d924d 8109remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
8110 const gdb_byte *myaddr, ULONGEST len_units,
8111 int unit_size, ULONGEST *xfered_len_units,
8112 char packet_format, int use_length)
c906108c 8113{
6d820c5c 8114 struct remote_state *rs = get_remote_state ();
cfd77fa1 8115 char *p;
a76d924d
DJ
8116 char *plen = NULL;
8117 int plenlen = 0;
124e13d9
SM
8118 int todo_units;
8119 int units_written;
8120 int payload_capacity_bytes;
8121 int payload_length_bytes;
a76d924d
DJ
8122
8123 if (packet_format != 'X' && packet_format != 'M')
8124 internal_error (__FILE__, __LINE__,
9b20d036 8125 _("remote_write_bytes_aux: bad packet format"));
c906108c 8126
124e13d9 8127 if (len_units == 0)
9b409511 8128 return TARGET_XFER_EOF;
b2182ed2 8129
124e13d9 8130 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 8131
6d820c5c
DJ
8132 /* The packet buffer will be large enough for the payload;
8133 get_memory_packet_size ensures this. */
a76d924d 8134 rs->buf[0] = '\0';
c906108c 8135
a257b5bb 8136 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
8137 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
8138
124e13d9 8139 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 8140 if (!use_length)
0df8b418 8141 /* The comma won't be used. */
124e13d9
SM
8142 payload_capacity_bytes += 1;
8143 payload_capacity_bytes -= strlen (header);
8144 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 8145
a76d924d 8146 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 8147
a76d924d
DJ
8148 strcat (rs->buf, header);
8149 p = rs->buf + strlen (header);
8150
8151 /* Compute a best guess of the number of bytes actually transfered. */
8152 if (packet_format == 'X')
c906108c 8153 {
23860348 8154 /* Best guess at number of bytes that will fit. */
325fac50
PA
8155 todo_units = std::min (len_units,
8156 (ULONGEST) payload_capacity_bytes / unit_size);
a76d924d 8157 if (use_length)
124e13d9 8158 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50 8159 todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
8160 }
8161 else
8162 {
124e13d9 8163 /* Number of bytes that will fit. */
325fac50
PA
8164 todo_units
8165 = std::min (len_units,
8166 (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
a76d924d 8167 if (use_length)
124e13d9 8168 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50
PA
8169 todo_units = std::min (todo_units,
8170 (payload_capacity_bytes / unit_size) / 2);
917317f4 8171 }
a76d924d 8172
124e13d9 8173 if (todo_units <= 0)
3de11b2e 8174 internal_error (__FILE__, __LINE__,
405f8e94 8175 _("minimum packet size too small to write data"));
802188a7 8176
6765f3e5
DJ
8177 /* If we already need another packet, then try to align the end
8178 of this packet to a useful boundary. */
124e13d9
SM
8179 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
8180 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 8181
a257b5bb 8182 /* Append "<memaddr>". */
917317f4
JM
8183 memaddr = remote_address_masked (memaddr);
8184 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 8185
a76d924d
DJ
8186 if (use_length)
8187 {
8188 /* Append ",". */
8189 *p++ = ',';
802188a7 8190
124e13d9
SM
8191 /* Append the length and retain its location and size. It may need to be
8192 adjusted once the packet body has been created. */
a76d924d 8193 plen = p;
124e13d9 8194 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
8195 p += plenlen;
8196 }
a257b5bb
AC
8197
8198 /* Append ":". */
917317f4
JM
8199 *p++ = ':';
8200 *p = '\0';
802188a7 8201
a257b5bb 8202 /* Append the packet body. */
a76d924d 8203 if (packet_format == 'X')
917317f4 8204 {
917317f4
JM
8205 /* Binary mode. Send target system values byte by byte, in
8206 increasing byte addresses. Only escape certain critical
8207 characters. */
124e13d9
SM
8208 payload_length_bytes =
8209 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
8210 &units_written, payload_capacity_bytes);
6765f3e5 8211
124e13d9 8212 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
8213 a second try to keep the end of the packet aligned. Don't do
8214 this if the packet is tiny. */
124e13d9 8215 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 8216 {
124e13d9
SM
8217 int new_todo_units;
8218
8219 new_todo_units = align_for_efficient_write (units_written, memaddr);
8220
8221 if (new_todo_units != units_written)
8222 payload_length_bytes =
8223 remote_escape_output (myaddr, new_todo_units, unit_size,
8224 (gdb_byte *) p, &units_written,
8225 payload_capacity_bytes);
6765f3e5
DJ
8226 }
8227
124e13d9
SM
8228 p += payload_length_bytes;
8229 if (use_length && units_written < todo_units)
c906108c 8230 {
802188a7 8231 /* Escape chars have filled up the buffer prematurely,
124e13d9 8232 and we have actually sent fewer units than planned.
917317f4
JM
8233 Fix-up the length field of the packet. Use the same
8234 number of characters as before. */
124e13d9
SM
8235 plen += hexnumnstr (plen, (ULONGEST) units_written,
8236 plenlen);
917317f4 8237 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 8238 }
a76d924d
DJ
8239 }
8240 else
8241 {
917317f4
JM
8242 /* Normal mode: Send target system values byte by byte, in
8243 increasing byte addresses. Each byte is encoded as a two hex
8244 value. */
124e13d9
SM
8245 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
8246 units_written = todo_units;
c906108c 8247 }
802188a7 8248
2e9f7625 8249 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8250 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8251
2e9f7625 8252 if (rs->buf[0] == 'E')
00d84524 8253 return TARGET_XFER_E_IO;
802188a7 8254
124e13d9
SM
8255 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
8256 send fewer units than we'd planned. */
8257 *xfered_len_units = (ULONGEST) units_written;
9b409511 8258 return TARGET_XFER_OK;
c906108c
SS
8259}
8260
a76d924d
DJ
8261/* Write memory data directly to the remote machine.
8262 This does not inform the data cache; the data cache uses this.
8263 MEMADDR is the address in the remote memory space.
8264 MYADDR is the address of the buffer in our space.
8265 LEN is the number of bytes.
8266
9b409511
YQ
8267 Return the transferred status, error or OK (an
8268 'enum target_xfer_status' value). Save the number of bytes
8269 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 8270
9b409511
YQ
8271static enum target_xfer_status
8272remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 8273 int unit_size, ULONGEST *xfered_len)
a76d924d 8274{
a121b7c1 8275 const char *packet_format = NULL;
a76d924d
DJ
8276
8277 /* Check whether the target supports binary download. */
8278 check_binary_download (memaddr);
8279
4082afcc 8280 switch (packet_support (PACKET_X))
a76d924d
DJ
8281 {
8282 case PACKET_ENABLE:
8283 packet_format = "X";
8284 break;
8285 case PACKET_DISABLE:
8286 packet_format = "M";
8287 break;
8288 case PACKET_SUPPORT_UNKNOWN:
8289 internal_error (__FILE__, __LINE__,
8290 _("remote_write_bytes: bad internal state"));
8291 default:
8292 internal_error (__FILE__, __LINE__, _("bad switch"));
8293 }
8294
8295 return remote_write_bytes_aux (packet_format,
124e13d9 8296 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 8297 packet_format[0], 1);
a76d924d
DJ
8298}
8299
9217e74e
YQ
8300/* Read memory data directly from the remote machine.
8301 This does not use the data cache; the data cache uses this.
8302 MEMADDR is the address in the remote memory space.
8303 MYADDR is the address of the buffer in our space.
124e13d9
SM
8304 LEN_UNITS is the number of addressable memory units to read..
8305 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
8306
8307 Return the transferred status, error or OK (an
8308 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
8309 transferred in *XFERED_LEN_UNITS.
8310
8311 See the comment of remote_write_bytes_aux for an example of
8312 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
8313
8314static enum target_xfer_status
124e13d9
SM
8315remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
8316 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
8317{
8318 struct remote_state *rs = get_remote_state ();
124e13d9 8319 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 8320 char *p;
124e13d9
SM
8321 int todo_units;
8322 int decoded_bytes;
9217e74e 8323
124e13d9 8324 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
8325 /* The packet buffer will be large enough for the payload;
8326 get_memory_packet_size ensures this. */
8327
124e13d9 8328 /* Number of units that will fit. */
325fac50
PA
8329 todo_units = std::min (len_units,
8330 (ULONGEST) (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
8331
8332 /* Construct "m"<memaddr>","<len>". */
8333 memaddr = remote_address_masked (memaddr);
8334 p = rs->buf;
8335 *p++ = 'm';
8336 p += hexnumstr (p, (ULONGEST) memaddr);
8337 *p++ = ',';
124e13d9 8338 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
8339 *p = '\0';
8340 putpkt (rs->buf);
8341 getpkt (&rs->buf, &rs->buf_size, 0);
8342 if (rs->buf[0] == 'E'
8343 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
8344 && rs->buf[3] == '\0')
8345 return TARGET_XFER_E_IO;
8346 /* Reply describes memory byte by byte, each byte encoded as two hex
8347 characters. */
8348 p = rs->buf;
124e13d9 8349 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 8350 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 8351 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
8352 return TARGET_XFER_OK;
8353}
8354
b55fbac4
YQ
8355/* Using the set of read-only target sections of remote, read live
8356 read-only memory.
8acf9577
YQ
8357
8358 For interface/parameters/return description see target.h,
8359 to_xfer_partial. */
8360
8361static enum target_xfer_status
b55fbac4
YQ
8362remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
8363 ULONGEST memaddr, ULONGEST len,
124e13d9 8364 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
8365{
8366 struct target_section *secp;
8367 struct target_section_table *table;
8368
8369 secp = target_section_by_addr (ops, memaddr);
8370 if (secp != NULL
8371 && (bfd_get_section_flags (secp->the_bfd_section->owner,
8372 secp->the_bfd_section)
8373 & SEC_READONLY))
8374 {
8375 struct target_section *p;
8376 ULONGEST memend = memaddr + len;
8377
8378 table = target_get_section_table (ops);
8379
8380 for (p = table->sections; p < table->sections_end; p++)
8381 {
8382 if (memaddr >= p->addr)
8383 {
8384 if (memend <= p->endaddr)
8385 {
8386 /* Entire transfer is within this section. */
124e13d9 8387 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8388 xfered_len);
8acf9577
YQ
8389 }
8390 else if (memaddr >= p->endaddr)
8391 {
8392 /* This section ends before the transfer starts. */
8393 continue;
8394 }
8395 else
8396 {
8397 /* This section overlaps the transfer. Just do half. */
8398 len = p->endaddr - memaddr;
124e13d9 8399 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8400 xfered_len);
8acf9577
YQ
8401 }
8402 }
8403 }
8404 }
8405
8406 return TARGET_XFER_EOF;
8407}
8408
9217e74e
YQ
8409/* Similar to remote_read_bytes_1, but it reads from the remote stub
8410 first if the requested memory is unavailable in traceframe.
8411 Otherwise, fall back to remote_read_bytes_1. */
c906108c 8412
9b409511 8413static enum target_xfer_status
8acf9577 8414remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
8415 gdb_byte *myaddr, ULONGEST len, int unit_size,
8416 ULONGEST *xfered_len)
c906108c 8417{
6b6aa828 8418 if (len == 0)
96c4f946 8419 return TARGET_XFER_EOF;
b2182ed2 8420
8acf9577
YQ
8421 if (get_traceframe_number () != -1)
8422 {
8423 VEC(mem_range_s) *available;
8424
8425 /* If we fail to get the set of available memory, then the
8426 target does not support querying traceframe info, and so we
8427 attempt reading from the traceframe anyway (assuming the
8428 target implements the old QTro packet then). */
8429 if (traceframe_available_memory (&available, memaddr, len))
8430 {
8431 struct cleanup *old_chain;
8432
8433 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
8434
8435 if (VEC_empty (mem_range_s, available)
8436 || VEC_index (mem_range_s, available, 0)->start != memaddr)
8437 {
8438 enum target_xfer_status res;
8439
8440 /* Don't read into the traceframe's available
8441 memory. */
8442 if (!VEC_empty (mem_range_s, available))
8443 {
8444 LONGEST oldlen = len;
8445
8446 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
8447 gdb_assert (len <= oldlen);
8448 }
8449
8450 do_cleanups (old_chain);
8451
8452 /* This goes through the topmost target again. */
b55fbac4 8453 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 8454 len, unit_size, xfered_len);
8acf9577
YQ
8455 if (res == TARGET_XFER_OK)
8456 return TARGET_XFER_OK;
8457 else
8458 {
8459 /* No use trying further, we know some memory starting
8460 at MEMADDR isn't available. */
8461 *xfered_len = len;
8462 return TARGET_XFER_UNAVAILABLE;
8463 }
8464 }
8465
8466 /* Don't try to read more than how much is available, in
8467 case the target implements the deprecated QTro packet to
8468 cater for older GDBs (the target's knowledge of read-only
8469 sections may be outdated by now). */
8470 len = VEC_index (mem_range_s, available, 0)->length;
8471
8472 do_cleanups (old_chain);
8473 }
8474 }
8475
124e13d9 8476 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 8477}
74531fed 8478
c906108c 8479\f
c906108c 8480
a76d924d
DJ
8481/* Sends a packet with content determined by the printf format string
8482 FORMAT and the remaining arguments, then gets the reply. Returns
8483 whether the packet was a success, a failure, or unknown. */
8484
77b64a49
PA
8485static enum packet_result remote_send_printf (const char *format, ...)
8486 ATTRIBUTE_PRINTF (1, 2);
8487
2c0b251b 8488static enum packet_result
a76d924d
DJ
8489remote_send_printf (const char *format, ...)
8490{
8491 struct remote_state *rs = get_remote_state ();
8492 int max_size = get_remote_packet_size ();
a76d924d 8493 va_list ap;
a744cf53 8494
a76d924d
DJ
8495 va_start (ap, format);
8496
8497 rs->buf[0] = '\0';
8498 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 8499 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
8500
8501 if (putpkt (rs->buf) < 0)
8502 error (_("Communication problem with target."));
8503
8504 rs->buf[0] = '\0';
8505 getpkt (&rs->buf, &rs->buf_size, 0);
8506
8507 return packet_check_result (rs->buf);
8508}
8509
a76d924d
DJ
8510/* Flash writing can take quite some time. We'll set
8511 effectively infinite timeout for flash operations.
8512 In future, we'll need to decide on a better approach. */
8513static const int remote_flash_timeout = 1000;
8514
8515static void
8516remote_flash_erase (struct target_ops *ops,
8517 ULONGEST address, LONGEST length)
8518{
f5656ead 8519 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d 8520 enum packet_result ret;
2ec845e7
TT
8521 scoped_restore restore_timeout
8522 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
a76d924d
DJ
8523
8524 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 8525 phex (address, addr_size),
a76d924d
DJ
8526 phex (length, 4));
8527 switch (ret)
8528 {
8529 case PACKET_UNKNOWN:
8530 error (_("Remote target does not support flash erase"));
8531 case PACKET_ERROR:
8532 error (_("Error erasing flash with vFlashErase packet"));
8533 default:
8534 break;
8535 }
a76d924d
DJ
8536}
8537
9b409511
YQ
8538static enum target_xfer_status
8539remote_flash_write (struct target_ops *ops, ULONGEST address,
8540 ULONGEST length, ULONGEST *xfered_len,
8541 const gdb_byte *data)
a76d924d 8542{
2ec845e7
TT
8543 scoped_restore restore_timeout
8544 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8545 return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
8546 xfered_len,'X', 0);
a76d924d
DJ
8547}
8548
8549static void
8550remote_flash_done (struct target_ops *ops)
8551{
a76d924d 8552 int ret;
a76d924d 8553
2ec845e7
TT
8554 scoped_restore restore_timeout
8555 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8556
a76d924d 8557 ret = remote_send_printf ("vFlashDone");
a76d924d
DJ
8558
8559 switch (ret)
8560 {
8561 case PACKET_UNKNOWN:
8562 error (_("Remote target does not support vFlashDone"));
8563 case PACKET_ERROR:
8564 error (_("Error finishing flash operation"));
8565 default:
8566 break;
8567 }
8568}
8569
c906108c 8570static void
fba45db2 8571remote_files_info (struct target_ops *ignore)
c906108c
SS
8572{
8573 puts_filtered ("Debugging a target over a serial line.\n");
8574}
8575\f
8576/* Stuff for dealing with the packets which are part of this protocol.
8577 See comment at top of file for details. */
8578
1927e618
PA
8579/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
8580 error to higher layers. Called when a serial error is detected.
8581 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
8582 the system error message for errno at function entry and final dot
8583 for output compatibility with throw_perror_with_name. */
1927e618
PA
8584
8585static void
8586unpush_and_perror (const char *string)
8587{
d6cb50a2 8588 int saved_errno = errno;
1927e618
PA
8589
8590 remote_unpush_target ();
d6cb50a2
JK
8591 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
8592 safe_strerror (saved_errno));
1927e618
PA
8593}
8594
048094ac
PA
8595/* Read a single character from the remote end. The current quit
8596 handler is overridden to avoid quitting in the middle of packet
8597 sequence, as that would break communication with the remote server.
8598 See remote_serial_quit_handler for more detail. */
c906108c
SS
8599
8600static int
fba45db2 8601readchar (int timeout)
c906108c
SS
8602{
8603 int ch;
5d93a237 8604 struct remote_state *rs = get_remote_state ();
048094ac 8605
2ec845e7
TT
8606 {
8607 scoped_restore restore_quit
8608 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
c906108c 8609
2ec845e7 8610 rs->got_ctrlc_during_io = 0;
c906108c 8611
2ec845e7 8612 ch = serial_readchar (rs->remote_desc, timeout);
048094ac 8613
2ec845e7
TT
8614 if (rs->got_ctrlc_during_io)
8615 set_quit_flag ();
8616 }
048094ac 8617
2acceee2 8618 if (ch >= 0)
0876f84a 8619 return ch;
2acceee2
JM
8620
8621 switch ((enum serial_rc) ch)
c906108c
SS
8622 {
8623 case SERIAL_EOF:
78a095c3 8624 remote_unpush_target ();
598d3636 8625 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 8626 /* no return */
c906108c 8627 case SERIAL_ERROR:
1927e618
PA
8628 unpush_and_perror (_("Remote communication error. "
8629 "Target disconnected."));
2acceee2 8630 /* no return */
c906108c 8631 case SERIAL_TIMEOUT:
2acceee2 8632 break;
c906108c 8633 }
2acceee2 8634 return ch;
c906108c
SS
8635}
8636
c33e31fd 8637/* Wrapper for serial_write that closes the target and throws if
048094ac
PA
8638 writing fails. The current quit handler is overridden to avoid
8639 quitting in the middle of packet sequence, as that would break
8640 communication with the remote server. See
8641 remote_serial_quit_handler for more detail. */
c33e31fd
PA
8642
8643static void
8644remote_serial_write (const char *str, int len)
8645{
5d93a237 8646 struct remote_state *rs = get_remote_state ();
048094ac 8647
2ec845e7
TT
8648 scoped_restore restore_quit
8649 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
048094ac
PA
8650
8651 rs->got_ctrlc_during_io = 0;
5d93a237
TT
8652
8653 if (serial_write (rs->remote_desc, str, len))
c33e31fd 8654 {
1927e618
PA
8655 unpush_and_perror (_("Remote communication error. "
8656 "Target disconnected."));
c33e31fd 8657 }
048094ac
PA
8658
8659 if (rs->got_ctrlc_during_io)
8660 set_quit_flag ();
c33e31fd
PA
8661}
8662
6d820c5c
DJ
8663/* Send the command in *BUF to the remote machine, and read the reply
8664 into *BUF. Report an error if we get an error reply. Resize
8665 *BUF using xrealloc if necessary to hold the result, and update
8666 *SIZEOF_BUF. */
c906108c
SS
8667
8668static void
6d820c5c
DJ
8669remote_send (char **buf,
8670 long *sizeof_buf)
c906108c 8671{
6d820c5c 8672 putpkt (*buf);
c2d11a7d 8673 getpkt (buf, sizeof_buf, 0);
c906108c 8674
6d820c5c
DJ
8675 if ((*buf)[0] == 'E')
8676 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
8677}
8678
b3ced9ba
PA
8679/* Return a string representing an escaped version of BUF, of len N.
8680 E.g. \n is converted to \\n, \t to \\t, etc. */
6e5abd65 8681
b3ced9ba 8682static std::string
6e5abd65
PA
8683escape_buffer (const char *buf, int n)
8684{
d7e74731 8685 string_file stb;
6e5abd65 8686
d7e74731
PA
8687 stb.putstrn (buf, n, '\\');
8688 return std::move (stb.string ());
6e5abd65
PA
8689}
8690
c906108c
SS
8691/* Display a null-terminated packet on stdout, for debugging, using C
8692 string notation. */
8693
8694static void
baa336ce 8695print_packet (const char *buf)
c906108c
SS
8696{
8697 puts_filtered ("\"");
43e526b9 8698 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
8699 puts_filtered ("\"");
8700}
8701
8702int
baa336ce 8703putpkt (const char *buf)
c906108c
SS
8704{
8705 return putpkt_binary (buf, strlen (buf));
8706}
8707
8708/* Send a packet to the remote machine, with error checking. The data
23860348 8709 of the packet is in BUF. The string in BUF can be at most
ea9c271d 8710 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
8711 and for a possible /0 if we are debugging (remote_debug) and want
8712 to print the sent packet as a string. */
c906108c
SS
8713
8714static int
baa336ce 8715putpkt_binary (const char *buf, int cnt)
c906108c 8716{
2d717e4f 8717 struct remote_state *rs = get_remote_state ();
c906108c
SS
8718 int i;
8719 unsigned char csum = 0;
224c3ddb 8720 char *buf2 = (char *) xmalloc (cnt + 6);
a5c0808e 8721 struct cleanup *old_chain = make_cleanup (xfree, buf2);
085dd6e6 8722
c906108c
SS
8723 int ch;
8724 int tcount = 0;
8725 char *p;
8726
e24a49d8
PA
8727 /* Catch cases like trying to read memory or listing threads while
8728 we're waiting for a stop reply. The remote server wouldn't be
8729 ready to handle this request, so we'd hang and timeout. We don't
8730 have to worry about this in synchronous mode, because in that
8731 case it's not possible to issue a command while the target is
74531fed
PA
8732 running. This is not a problem in non-stop mode, because in that
8733 case, the stub is always ready to process serial input. */
6efcd9a8
PA
8734 if (!target_is_non_stop_p ()
8735 && target_is_async_p ()
8736 && rs->waiting_for_stop_reply)
9597b22a
DE
8737 {
8738 error (_("Cannot execute this command while the target is running.\n"
8739 "Use the \"interrupt\" command to stop the target\n"
8740 "and then try again."));
8741 }
e24a49d8 8742
2d717e4f
DJ
8743 /* We're sending out a new packet. Make sure we don't look at a
8744 stale cached response. */
8745 rs->cached_wait_status = 0;
8746
c906108c
SS
8747 /* Copy the packet into buffer BUF2, encapsulating it
8748 and giving it a checksum. */
8749
c906108c
SS
8750 p = buf2;
8751 *p++ = '$';
8752
8753 for (i = 0; i < cnt; i++)
8754 {
8755 csum += buf[i];
8756 *p++ = buf[i];
8757 }
8758 *p++ = '#';
8759 *p++ = tohex ((csum >> 4) & 0xf);
8760 *p++ = tohex (csum & 0xf);
8761
8762 /* Send it over and over until we get a positive ack. */
8763
8764 while (1)
8765 {
8766 int started_error_output = 0;
8767
8768 if (remote_debug)
8769 {
8770 *p = '\0';
b3ced9ba 8771
6f8976bf
YQ
8772 int len = (int) (p - buf2);
8773
8774 std::string str
8775 = escape_buffer (buf2, std::min (len, REMOTE_DEBUG_MAX_CHAR));
8776
8777 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s", str.c_str ());
8778
8779 if (str.length () > REMOTE_DEBUG_MAX_CHAR)
8780 {
8781 fprintf_unfiltered (gdb_stdlog, "[%zu bytes omitted]",
8782 str.length () - REMOTE_DEBUG_MAX_CHAR);
8783 }
8784
8785 fprintf_unfiltered (gdb_stdlog, "...");
b3ced9ba 8786
0f71a2f6 8787 gdb_flush (gdb_stdlog);
c906108c 8788 }
c33e31fd 8789 remote_serial_write (buf2, p - buf2);
c906108c 8790
a6f3e723
SL
8791 /* If this is a no acks version of the remote protocol, send the
8792 packet and move on. */
8793 if (rs->noack_mode)
8794 break;
8795
74531fed
PA
8796 /* Read until either a timeout occurs (-2) or '+' is read.
8797 Handle any notification that arrives in the mean time. */
c906108c
SS
8798 while (1)
8799 {
8800 ch = readchar (remote_timeout);
8801
c5aa993b 8802 if (remote_debug)
c906108c
SS
8803 {
8804 switch (ch)
8805 {
8806 case '+':
1216fa2c 8807 case '-':
c906108c
SS
8808 case SERIAL_TIMEOUT:
8809 case '$':
74531fed 8810 case '%':
c906108c
SS
8811 if (started_error_output)
8812 {
8813 putchar_unfiltered ('\n');
8814 started_error_output = 0;
8815 }
8816 }
8817 }
8818
8819 switch (ch)
8820 {
8821 case '+':
8822 if (remote_debug)
0f71a2f6 8823 fprintf_unfiltered (gdb_stdlog, "Ack\n");
a5c0808e 8824 do_cleanups (old_chain);
c906108c 8825 return 1;
1216fa2c
AC
8826 case '-':
8827 if (remote_debug)
8828 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 8829 /* FALLTHROUGH */
c906108c 8830 case SERIAL_TIMEOUT:
c5aa993b 8831 tcount++;
c906108c 8832 if (tcount > 3)
a5c0808e
PA
8833 {
8834 do_cleanups (old_chain);
8835 return 0;
8836 }
23860348 8837 break; /* Retransmit buffer. */
c906108c
SS
8838 case '$':
8839 {
40e3f985 8840 if (remote_debug)
2bc416ba 8841 fprintf_unfiltered (gdb_stdlog,
23860348 8842 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
8843 /* It's probably an old response sent because an ACK
8844 was lost. Gobble up the packet and ack it so it
8845 doesn't get retransmitted when we resend this
8846 packet. */
6d820c5c 8847 skip_frame ();
c33e31fd 8848 remote_serial_write ("+", 1);
23860348 8849 continue; /* Now, go look for +. */
c906108c 8850 }
74531fed
PA
8851
8852 case '%':
8853 {
8854 int val;
8855
8856 /* If we got a notification, handle it, and go back to looking
8857 for an ack. */
8858 /* We've found the start of a notification. Now
8859 collect the data. */
8860 val = read_frame (&rs->buf, &rs->buf_size);
8861 if (val >= 0)
8862 {
8863 if (remote_debug)
8864 {
b3ced9ba 8865 std::string str = escape_buffer (rs->buf, val);
6e5abd65 8866
6e5abd65
PA
8867 fprintf_unfiltered (gdb_stdlog,
8868 " Notification received: %s\n",
b3ced9ba 8869 str.c_str ());
74531fed 8870 }
5965e028 8871 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
8872 /* We're in sync now, rewait for the ack. */
8873 tcount = 0;
8874 }
8875 else
8876 {
8877 if (remote_debug)
8878 {
8879 if (!started_error_output)
8880 {
8881 started_error_output = 1;
8882 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
8883 }
8884 fputc_unfiltered (ch & 0177, gdb_stdlog);
8885 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
8886 }
8887 }
8888 continue;
8889 }
8890 /* fall-through */
c906108c
SS
8891 default:
8892 if (remote_debug)
8893 {
8894 if (!started_error_output)
8895 {
8896 started_error_output = 1;
0f71a2f6 8897 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 8898 }
0f71a2f6 8899 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
8900 }
8901 continue;
8902 }
23860348 8903 break; /* Here to retransmit. */
c906108c
SS
8904 }
8905
8906#if 0
8907 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
8908 able to get out next time we call QUIT, without anything as
8909 violent as interrupt_query. If we want to provide a way out of
8910 here without getting to the next QUIT, it should be based on
8911 hitting ^C twice as in remote_wait. */
c906108c
SS
8912 if (quit_flag)
8913 {
8914 quit_flag = 0;
8915 interrupt_query ();
8916 }
8917#endif
8918 }
a5c0808e
PA
8919
8920 do_cleanups (old_chain);
a6f3e723 8921 return 0;
c906108c
SS
8922}
8923
6d820c5c
DJ
8924/* Come here after finding the start of a frame when we expected an
8925 ack. Do our best to discard the rest of this packet. */
8926
8927static void
8928skip_frame (void)
8929{
8930 int c;
8931
8932 while (1)
8933 {
8934 c = readchar (remote_timeout);
8935 switch (c)
8936 {
8937 case SERIAL_TIMEOUT:
8938 /* Nothing we can do. */
8939 return;
8940 case '#':
8941 /* Discard the two bytes of checksum and stop. */
8942 c = readchar (remote_timeout);
8943 if (c >= 0)
8944 c = readchar (remote_timeout);
8945
8946 return;
8947 case '*': /* Run length encoding. */
8948 /* Discard the repeat count. */
8949 c = readchar (remote_timeout);
8950 if (c < 0)
8951 return;
8952 break;
8953 default:
8954 /* A regular character. */
8955 break;
8956 }
8957 }
8958}
8959
c906108c 8960/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
8961 into *BUF, verifying the checksum, length, and handling run-length
8962 compression. NUL terminate the buffer. If there is not enough room,
8963 expand *BUF using xrealloc.
c906108c 8964
c2d11a7d
JM
8965 Returns -1 on error, number of characters in buffer (ignoring the
8966 trailing NULL) on success. (could be extended to return one of the
23860348 8967 SERIAL status indications). */
c2d11a7d
JM
8968
8969static long
6d820c5c
DJ
8970read_frame (char **buf_p,
8971 long *sizeof_buf)
c906108c
SS
8972{
8973 unsigned char csum;
c2d11a7d 8974 long bc;
c906108c 8975 int c;
6d820c5c 8976 char *buf = *buf_p;
a6f3e723 8977 struct remote_state *rs = get_remote_state ();
c906108c
SS
8978
8979 csum = 0;
c2d11a7d 8980 bc = 0;
c906108c
SS
8981
8982 while (1)
8983 {
8984 c = readchar (remote_timeout);
c906108c
SS
8985 switch (c)
8986 {
8987 case SERIAL_TIMEOUT:
8988 if (remote_debug)
0f71a2f6 8989 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 8990 return -1;
c906108c
SS
8991 case '$':
8992 if (remote_debug)
0f71a2f6
JM
8993 fputs_filtered ("Saw new packet start in middle of old one\n",
8994 gdb_stdlog);
23860348 8995 return -1; /* Start a new packet, count retries. */
c906108c
SS
8996 case '#':
8997 {
8998 unsigned char pktcsum;
e1b09194
AC
8999 int check_0 = 0;
9000 int check_1 = 0;
c906108c 9001
c2d11a7d 9002 buf[bc] = '\0';
c906108c 9003
e1b09194
AC
9004 check_0 = readchar (remote_timeout);
9005 if (check_0 >= 0)
9006 check_1 = readchar (remote_timeout);
802188a7 9007
e1b09194
AC
9008 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
9009 {
9010 if (remote_debug)
2bc416ba 9011 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 9012 gdb_stdlog);
e1b09194
AC
9013 return -1;
9014 }
9015 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
9016 {
9017 if (remote_debug)
2bc416ba 9018 fputs_filtered ("Communication error in checksum\n",
23860348 9019 gdb_stdlog);
40e3f985
FN
9020 return -1;
9021 }
c906108c 9022
a6f3e723
SL
9023 /* Don't recompute the checksum; with no ack packets we
9024 don't have any way to indicate a packet retransmission
9025 is necessary. */
9026 if (rs->noack_mode)
9027 return bc;
9028
e1b09194 9029 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 9030 if (csum == pktcsum)
c2d11a7d 9031 return bc;
c906108c 9032
c5aa993b 9033 if (remote_debug)
c906108c 9034 {
b3ced9ba 9035 std::string str = escape_buffer (buf, bc);
6e5abd65 9036
6e5abd65 9037 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
9038 "Bad checksum, sentsum=0x%x, "
9039 "csum=0x%x, buf=%s\n",
b3ced9ba 9040 pktcsum, csum, str.c_str ());
c906108c 9041 }
c2d11a7d 9042 /* Number of characters in buffer ignoring trailing
23860348 9043 NULL. */
c2d11a7d 9044 return -1;
c906108c 9045 }
23860348 9046 case '*': /* Run length encoding. */
c2c6d25f
JM
9047 {
9048 int repeat;
c906108c 9049
a744cf53 9050 csum += c;
b4501125
AC
9051 c = readchar (remote_timeout);
9052 csum += c;
23860348 9053 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 9054
23860348 9055 /* The character before ``*'' is repeated. */
c2d11a7d 9056
6d820c5c 9057 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 9058 {
6d820c5c
DJ
9059 if (bc + repeat - 1 >= *sizeof_buf - 1)
9060 {
9061 /* Make some more room in the buffer. */
9062 *sizeof_buf += repeat;
224c3ddb 9063 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c
DJ
9064 buf = *buf_p;
9065 }
9066
c2d11a7d
JM
9067 memset (&buf[bc], buf[bc - 1], repeat);
9068 bc += repeat;
c2c6d25f
JM
9069 continue;
9070 }
9071
c2d11a7d 9072 buf[bc] = '\0';
6d820c5c 9073 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 9074 return -1;
c2c6d25f 9075 }
c906108c 9076 default:
6d820c5c 9077 if (bc >= *sizeof_buf - 1)
c906108c 9078 {
6d820c5c
DJ
9079 /* Make some more room in the buffer. */
9080 *sizeof_buf *= 2;
224c3ddb 9081 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c 9082 buf = *buf_p;
c906108c
SS
9083 }
9084
6d820c5c
DJ
9085 buf[bc++] = c;
9086 csum += c;
9087 continue;
c906108c
SS
9088 }
9089 }
9090}
9091
9092/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9093 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9094 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9095 rather than timing out; this is used (in synchronous mode) to wait
9096 for a target that is is executing user code to stop. */
d9fcf2fb
JM
9097/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9098 don't have to change all the calls to getpkt to deal with the
9099 return value, because at the moment I don't know what the right
23860348 9100 thing to do it for those. */
c906108c 9101void
6d820c5c
DJ
9102getpkt (char **buf,
9103 long *sizeof_buf,
c2d11a7d 9104 int forever)
d9fcf2fb 9105{
54887903 9106 getpkt_sane (buf, sizeof_buf, forever);
d9fcf2fb
JM
9107}
9108
9109
9110/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9111 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9112 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9113 rather than timing out; this is used (in synchronous mode) to wait
9114 for a target that is is executing user code to stop. If FOREVER ==
9115 0, this function is allowed to time out gracefully and return an
74531fed
PA
9116 indication of this to the caller. Otherwise return the number of
9117 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
9118 enough reason to return to the caller. *IS_NOTIF is an output
9119 boolean that indicates whether *BUF holds a notification or not
9120 (a regular packet). */
74531fed 9121
3172dc30 9122static int
74531fed 9123getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 9124 int expecting_notif, int *is_notif)
c906108c 9125{
2d717e4f 9126 struct remote_state *rs = get_remote_state ();
c906108c
SS
9127 int c;
9128 int tries;
9129 int timeout;
df4b58fe 9130 int val = -1;
c906108c 9131
2d717e4f
DJ
9132 /* We're reading a new response. Make sure we don't look at a
9133 previously cached response. */
9134 rs->cached_wait_status = 0;
9135
6d820c5c 9136 strcpy (*buf, "timeout");
c906108c
SS
9137
9138 if (forever)
74531fed
PA
9139 timeout = watchdog > 0 ? watchdog : -1;
9140 else if (expecting_notif)
9141 timeout = 0; /* There should already be a char in the buffer. If
9142 not, bail out. */
c906108c
SS
9143 else
9144 timeout = remote_timeout;
9145
9146#define MAX_TRIES 3
9147
74531fed
PA
9148 /* Process any number of notifications, and then return when
9149 we get a packet. */
9150 for (;;)
c906108c 9151 {
d9c43928 9152 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
9153 times. */
9154 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 9155 {
74531fed
PA
9156 /* This can loop forever if the remote side sends us
9157 characters continuously, but if it pauses, we'll get
9158 SERIAL_TIMEOUT from readchar because of timeout. Then
9159 we'll count that as a retry.
9160
9161 Note that even when forever is set, we will only wait
9162 forever prior to the start of a packet. After that, we
9163 expect characters to arrive at a brisk pace. They should
9164 show up within remote_timeout intervals. */
9165 do
9166 c = readchar (timeout);
9167 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
9168
9169 if (c == SERIAL_TIMEOUT)
9170 {
74531fed
PA
9171 if (expecting_notif)
9172 return -1; /* Don't complain, it's normal to not get
9173 anything in this case. */
9174
23860348 9175 if (forever) /* Watchdog went off? Kill the target. */
c906108c 9176 {
78a095c3 9177 remote_unpush_target ();
598d3636
JK
9178 throw_error (TARGET_CLOSE_ERROR,
9179 _("Watchdog timeout has expired. "
9180 "Target detached."));
c906108c 9181 }
c906108c 9182 if (remote_debug)
0f71a2f6 9183 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 9184 }
74531fed
PA
9185 else
9186 {
9187 /* We've found the start of a packet or notification.
9188 Now collect the data. */
9189 val = read_frame (buf, sizeof_buf);
9190 if (val >= 0)
9191 break;
9192 }
9193
c33e31fd 9194 remote_serial_write ("-", 1);
c906108c 9195 }
c906108c 9196
74531fed
PA
9197 if (tries > MAX_TRIES)
9198 {
9199 /* We have tried hard enough, and just can't receive the
9200 packet/notification. Give up. */
9201 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 9202
74531fed
PA
9203 /* Skip the ack char if we're in no-ack mode. */
9204 if (!rs->noack_mode)
c33e31fd 9205 remote_serial_write ("+", 1);
74531fed
PA
9206 return -1;
9207 }
c906108c 9208
74531fed
PA
9209 /* If we got an ordinary packet, return that to our caller. */
9210 if (c == '$')
c906108c
SS
9211 {
9212 if (remote_debug)
43e526b9 9213 {
6f8976bf
YQ
9214 std::string str
9215 = escape_buffer (*buf,
9216 std::min (val, REMOTE_DEBUG_MAX_CHAR));
9217
9218 fprintf_unfiltered (gdb_stdlog, "Packet received: %s",
9219 str.c_str ());
9220
9221 if (str.length () > REMOTE_DEBUG_MAX_CHAR)
9222 {
9223 fprintf_unfiltered (gdb_stdlog, "[%zu bytes omitted]",
9224 str.length () - REMOTE_DEBUG_MAX_CHAR);
9225 }
6e5abd65 9226
6f8976bf 9227 fprintf_unfiltered (gdb_stdlog, "\n");
43e526b9 9228 }
a6f3e723
SL
9229
9230 /* Skip the ack char if we're in no-ack mode. */
9231 if (!rs->noack_mode)
c33e31fd 9232 remote_serial_write ("+", 1);
fee9eda9
YQ
9233 if (is_notif != NULL)
9234 *is_notif = 0;
0876f84a 9235 return val;
c906108c
SS
9236 }
9237
74531fed
PA
9238 /* If we got a notification, handle it, and go back to looking
9239 for a packet. */
9240 else
9241 {
9242 gdb_assert (c == '%');
9243
9244 if (remote_debug)
9245 {
b3ced9ba 9246 std::string str = escape_buffer (*buf, val);
6e5abd65 9247
6e5abd65
PA
9248 fprintf_unfiltered (gdb_stdlog,
9249 " Notification received: %s\n",
b3ced9ba 9250 str.c_str ());
74531fed 9251 }
fee9eda9
YQ
9252 if (is_notif != NULL)
9253 *is_notif = 1;
c906108c 9254
5965e028 9255 handle_notification (rs->notif_state, *buf);
c906108c 9256
74531fed 9257 /* Notifications require no acknowledgement. */
a6f3e723 9258
74531fed 9259 if (expecting_notif)
fee9eda9 9260 return val;
74531fed
PA
9261 }
9262 }
9263}
9264
9265static int
9266getpkt_sane (char **buf, long *sizeof_buf, int forever)
9267{
fee9eda9 9268 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
9269}
9270
9271static int
fee9eda9
YQ
9272getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
9273 int *is_notif)
74531fed 9274{
fee9eda9
YQ
9275 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
9276 is_notif);
c906108c 9277}
74531fed 9278
cbb8991c
DB
9279/* Check whether EVENT is a fork event for the process specified
9280 by the pid passed in DATA, and if it is, kill the fork child. */
9281
9282static int
9283kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
9284 QUEUE_ITER (stop_reply_p) *iter,
9285 stop_reply_p event,
9286 void *data)
9287{
19ba03f4 9288 struct queue_iter_param *param = (struct queue_iter_param *) data;
cbb8991c
DB
9289 int parent_pid = *(int *) param->input;
9290
9291 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
9292 {
9293 struct remote_state *rs = get_remote_state ();
9294 int child_pid = ptid_get_pid (event->ws.value.related_pid);
9295 int res;
9296
9297 res = remote_vkill (child_pid, rs);
9298 if (res != 0)
9299 error (_("Can't kill fork child process %d"), child_pid);
9300 }
9301
9302 return 1;
9303}
9304
9305/* Kill any new fork children of process PID that haven't been
9306 processed by follow_fork. */
9307
9308static void
9309kill_new_fork_children (int pid, struct remote_state *rs)
9310{
9311 struct thread_info *thread;
9312 struct notif_client *notif = &notif_client_stop;
9313 struct queue_iter_param param;
9314
9315 /* Kill the fork child threads of any threads in process PID
9316 that are stopped at a fork event. */
9317 ALL_NON_EXITED_THREADS (thread)
9318 {
9319 struct target_waitstatus *ws = &thread->pending_follow;
9320
9321 if (is_pending_fork_parent (ws, pid, thread->ptid))
9322 {
9323 struct remote_state *rs = get_remote_state ();
9324 int child_pid = ptid_get_pid (ws->value.related_pid);
9325 int res;
9326
9327 res = remote_vkill (child_pid, rs);
9328 if (res != 0)
9329 error (_("Can't kill fork child process %d"), child_pid);
9330 }
9331 }
9332
9333 /* Check for any pending fork events (not reported or processed yet)
9334 in process PID and kill those fork child threads as well. */
9335 remote_notif_get_pending_events (notif);
9336 param.input = &pid;
9337 param.output = NULL;
9338 QUEUE_iterate (stop_reply_p, stop_reply_queue,
9339 kill_child_of_pending_fork, &param);
9340}
9341
c906108c 9342\f
8020350c
DB
9343/* Target hook to kill the current inferior. */
9344
c906108c 9345static void
7d85a9c0 9346remote_kill (struct target_ops *ops)
43ff13b4 9347{
8020350c
DB
9348 int res = -1;
9349 int pid = ptid_get_pid (inferior_ptid);
9350 struct remote_state *rs = get_remote_state ();
0fdf84ca 9351
8020350c 9352 if (packet_support (PACKET_vKill) != PACKET_DISABLE)
0fdf84ca 9353 {
8020350c
DB
9354 /* If we're stopped while forking and we haven't followed yet,
9355 kill the child task. We need to do this before killing the
9356 parent task because if this is a vfork then the parent will
9357 be sleeping. */
9358 kill_new_fork_children (pid, rs);
9359
9360 res = remote_vkill (pid, rs);
9361 if (res == 0)
0fdf84ca 9362 {
bc1e6c81 9363 target_mourn_inferior (inferior_ptid);
0fdf84ca
PA
9364 return;
9365 }
8020350c 9366 }
0fdf84ca 9367
8020350c
DB
9368 /* If we are in 'target remote' mode and we are killing the only
9369 inferior, then we will tell gdbserver to exit and unpush the
9370 target. */
9371 if (res == -1 && !remote_multi_process_p (rs)
9372 && number_of_live_inferiors () == 1)
9373 {
9374 remote_kill_k ();
9375
9376 /* We've killed the remote end, we get to mourn it. If we are
9377 not in extended mode, mourning the inferior also unpushes
9378 remote_ops from the target stack, which closes the remote
9379 connection. */
bc1e6c81 9380 target_mourn_inferior (inferior_ptid);
8020350c
DB
9381
9382 return;
0fdf84ca 9383 }
43ff13b4 9384
8020350c 9385 error (_("Can't kill process"));
43ff13b4
JM
9386}
9387
8020350c
DB
9388/* Send a kill request to the target using the 'vKill' packet. */
9389
82f73884
PA
9390static int
9391remote_vkill (int pid, struct remote_state *rs)
9392{
4082afcc 9393 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
9394 return -1;
9395
9396 /* Tell the remote target to detach. */
bba74b36 9397 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
9398 putpkt (rs->buf);
9399 getpkt (&rs->buf, &rs->buf_size, 0);
9400
4082afcc
PA
9401 switch (packet_ok (rs->buf,
9402 &remote_protocol_packets[PACKET_vKill]))
9403 {
9404 case PACKET_OK:
9405 return 0;
9406 case PACKET_ERROR:
9407 return 1;
9408 case PACKET_UNKNOWN:
9409 return -1;
9410 default:
9411 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
9412 }
82f73884
PA
9413}
9414
8020350c
DB
9415/* Send a kill request to the target using the 'k' packet. */
9416
82f73884 9417static void
8020350c 9418remote_kill_k (void)
82f73884 9419{
8020350c
DB
9420 /* Catch errors so the user can quit from gdb even when we
9421 aren't on speaking terms with the remote system. */
9422 TRY
82f73884 9423 {
82f73884 9424 putpkt ("k");
82f73884 9425 }
8020350c
DB
9426 CATCH (ex, RETURN_MASK_ERROR)
9427 {
9428 if (ex.error == TARGET_CLOSE_ERROR)
9429 {
9430 /* If we got an (EOF) error that caused the target
9431 to go away, then we're done, that's what we wanted.
9432 "k" is susceptible to cause a premature EOF, given
9433 that the remote server isn't actually required to
9434 reply to "k", and it can happen that it doesn't
9435 even get to reply ACK to the "k". */
9436 return;
9437 }
82f73884 9438
8020350c
DB
9439 /* Otherwise, something went wrong. We didn't actually kill
9440 the target. Just propagate the exception, and let the
9441 user or higher layers decide what to do. */
9442 throw_exception (ex);
9443 }
9444 END_CATCH
82f73884
PA
9445}
9446
c906108c 9447static void
20f796c9 9448remote_mourn (struct target_ops *target)
c906108c 9449{
8020350c 9450 struct remote_state *rs = get_remote_state ();
ce5ce7ed 9451
8020350c
DB
9452 /* In 'target remote' mode with one inferior, we close the connection. */
9453 if (!rs->extended && number_of_live_inferiors () <= 1)
9454 {
9455 unpush_target (target);
c906108c 9456
8020350c
DB
9457 /* remote_close takes care of doing most of the clean up. */
9458 generic_mourn_inferior ();
9459 return;
9460 }
c906108c 9461
e24a49d8
PA
9462 /* In case we got here due to an error, but we're going to stay
9463 connected. */
9464 rs->waiting_for_stop_reply = 0;
9465
dc1981d7
PA
9466 /* If the current general thread belonged to the process we just
9467 detached from or has exited, the remote side current general
9468 thread becomes undefined. Considering a case like this:
9469
9470 - We just got here due to a detach.
9471 - The process that we're detaching from happens to immediately
9472 report a global breakpoint being hit in non-stop mode, in the
9473 same thread we had selected before.
9474 - GDB attaches to this process again.
9475 - This event happens to be the next event we handle.
9476
9477 GDB would consider that the current general thread didn't need to
9478 be set on the stub side (with Hg), since for all it knew,
9479 GENERAL_THREAD hadn't changed.
9480
9481 Notice that although in all-stop mode, the remote server always
9482 sets the current thread to the thread reporting the stop event,
9483 that doesn't happen in non-stop mode; in non-stop, the stub *must
9484 not* change the current thread when reporting a breakpoint hit,
9485 due to the decoupling of event reporting and event handling.
9486
9487 To keep things simple, we always invalidate our notion of the
9488 current thread. */
47f8a51d 9489 record_currthread (rs, minus_one_ptid);
dc1981d7 9490
8020350c 9491 /* Call common code to mark the inferior as not running. */
48aa3c27
PA
9492 generic_mourn_inferior ();
9493
d729566a 9494 if (!have_inferiors ())
2d717e4f 9495 {
82f73884
PA
9496 if (!remote_multi_process_p (rs))
9497 {
9498 /* Check whether the target is running now - some remote stubs
9499 automatically restart after kill. */
9500 putpkt ("?");
9501 getpkt (&rs->buf, &rs->buf_size, 0);
9502
9503 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
9504 {
3e43a32a
MS
9505 /* Assume that the target has been restarted. Set
9506 inferior_ptid so that bits of core GDB realizes
9507 there's something here, e.g., so that the user can
9508 say "kill" again. */
82f73884
PA
9509 inferior_ptid = magic_null_ptid;
9510 }
82f73884 9511 }
2d717e4f
DJ
9512 }
9513}
c906108c 9514
03583c20 9515static int
2bfc0540 9516extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 9517{
4082afcc 9518 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
9519}
9520
9521static void
9522extended_remote_disable_randomization (int val)
9523{
9524 struct remote_state *rs = get_remote_state ();
9525 char *reply;
9526
bba74b36
YQ
9527 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
9528 val);
03583c20 9529 putpkt (rs->buf);
b6bb3468 9530 reply = remote_get_noisy_reply ();
03583c20
UW
9531 if (*reply == '\0')
9532 error (_("Target does not support QDisableRandomization."));
9533 if (strcmp (reply, "OK") != 0)
9534 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
9535}
9536
2d717e4f 9537static int
7c5ded6a 9538extended_remote_run (const std::string &args)
2d717e4f
DJ
9539{
9540 struct remote_state *rs = get_remote_state ();
2d717e4f 9541 int len;
94585166 9542 const char *remote_exec_file = get_remote_exec_file ();
c906108c 9543
2d717e4f
DJ
9544 /* If the user has disabled vRun support, or we have detected that
9545 support is not available, do not try it. */
4082afcc 9546 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 9547 return -1;
424163ea 9548
2d717e4f
DJ
9549 strcpy (rs->buf, "vRun;");
9550 len = strlen (rs->buf);
c906108c 9551
2d717e4f
DJ
9552 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
9553 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
9554 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
9555 strlen (remote_exec_file));
2d717e4f 9556
7c5ded6a 9557 if (!args.empty ())
2d717e4f 9558 {
2d717e4f 9559 int i;
2d717e4f 9560
773a1edc 9561 gdb_argv argv (args.c_str ());
2d717e4f
DJ
9562 for (i = 0; argv[i] != NULL; i++)
9563 {
9564 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
9565 error (_("Argument list too long for run packet"));
9566 rs->buf[len++] = ';';
9f1b45b0
TT
9567 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
9568 strlen (argv[i]));
2d717e4f 9569 }
2d717e4f
DJ
9570 }
9571
9572 rs->buf[len++] = '\0';
9573
9574 putpkt (rs->buf);
9575 getpkt (&rs->buf, &rs->buf_size, 0);
9576
4082afcc 9577 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 9578 {
4082afcc 9579 case PACKET_OK:
3405876a 9580 /* We have a wait response. All is well. */
2d717e4f 9581 return 0;
4082afcc
PA
9582 case PACKET_UNKNOWN:
9583 return -1;
9584 case PACKET_ERROR:
2d717e4f
DJ
9585 if (remote_exec_file[0] == '\0')
9586 error (_("Running the default executable on the remote target failed; "
9587 "try \"set remote exec-file\"?"));
9588 else
9589 error (_("Running \"%s\" on the remote target failed"),
9590 remote_exec_file);
4082afcc
PA
9591 default:
9592 gdb_assert_not_reached (_("bad switch"));
2d717e4f 9593 }
c906108c
SS
9594}
9595
0a2dde4a
SDJ
9596/* Helper function to send set/unset environment packets. ACTION is
9597 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
9598 or "QEnvironmentUnsetVariable". VALUE is the variable to be
9599 sent. */
9600
9601static void
9602send_environment_packet (struct remote_state *rs,
9603 const char *action,
9604 const char *packet,
9605 const char *value)
9606{
9607 /* Convert the environment variable to an hex string, which
9608 is the best format to be transmitted over the wire. */
9609 std::string encoded_value = bin2hex ((const gdb_byte *) value,
9610 strlen (value));
9611
9612 xsnprintf (rs->buf, get_remote_packet_size (),
9613 "%s:%s", packet, encoded_value.c_str ());
9614
9615 putpkt (rs->buf);
9616 getpkt (&rs->buf, &rs->buf_size, 0);
9617 if (strcmp (rs->buf, "OK") != 0)
9618 warning (_("Unable to %s environment variable '%s' on remote."),
9619 action, value);
9620}
9621
9622/* Helper function to handle the QEnvironment* packets. */
9623
9624static void
9625extended_remote_environment_support (struct remote_state *rs)
9626{
9627 if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
9628 {
9629 putpkt ("QEnvironmentReset");
9630 getpkt (&rs->buf, &rs->buf_size, 0);
9631 if (strcmp (rs->buf, "OK") != 0)
9632 warning (_("Unable to reset environment on remote."));
9633 }
9634
9635 gdb_environ *e = &current_inferior ()->environment;
9636
9637 if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
9638 for (const std::string &el : e->user_set_env ())
9639 send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
9640 el.c_str ());
9641
9642 if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
9643 for (const std::string &el : e->user_unset_env ())
9644 send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
9645}
9646
bc3b087d
SDJ
9647/* Helper function to set the current working directory for the
9648 inferior in the remote target. */
9649
9650static void
9651extended_remote_set_inferior_cwd (struct remote_state *rs)
9652{
9653 if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
9654 {
9655 const char *inferior_cwd = get_inferior_cwd ();
9656
9657 if (inferior_cwd != NULL)
9658 {
9659 std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
9660 strlen (inferior_cwd));
9661
9662 xsnprintf (rs->buf, get_remote_packet_size (),
9663 "QSetWorkingDir:%s", hexpath.c_str ());
9664 }
9665 else
9666 {
9667 /* An empty inferior_cwd means that the user wants us to
9668 reset the remote server's inferior's cwd. */
9669 xsnprintf (rs->buf, get_remote_packet_size (),
9670 "QSetWorkingDir:");
9671 }
9672
9673 putpkt (rs->buf);
9674 getpkt (&rs->buf, &rs->buf_size, 0);
9675 if (packet_ok (rs->buf,
9676 &remote_protocol_packets[PACKET_QSetWorkingDir])
9677 != PACKET_OK)
9678 error (_("\
9679Remote replied unexpectedly while setting the inferior's working\n\
9680directory: %s"),
9681 rs->buf);
9682
9683 }
9684}
9685
2d717e4f
DJ
9686/* In the extended protocol we want to be able to do things like
9687 "run" and have them basically work as expected. So we need
9688 a special create_inferior function. We support changing the
9689 executable file and the command line arguments, but not the
9690 environment. */
9691
43ff13b4 9692static void
77a19445 9693extended_remote_create_inferior (struct target_ops *ops,
7c5ded6a
SDJ
9694 const char *exec_file,
9695 const std::string &args,
77a19445 9696 char **env, int from_tty)
43ff13b4 9697{
3405876a
PA
9698 int run_worked;
9699 char *stop_reply;
9700 struct remote_state *rs = get_remote_state ();
94585166 9701 const char *remote_exec_file = get_remote_exec_file ();
3405876a 9702
43ff13b4 9703 /* If running asynchronously, register the target file descriptor
23860348 9704 with the event loop. */
75c99385 9705 if (target_can_async_p ())
6a3753b3 9706 target_async (1);
43ff13b4 9707
03583c20 9708 /* Disable address space randomization if requested (and supported). */
2bfc0540 9709 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
9710 extended_remote_disable_randomization (disable_randomization);
9711
aefd8b33
SDJ
9712 /* If startup-with-shell is on, we inform gdbserver to start the
9713 remote inferior using a shell. */
9714 if (packet_support (PACKET_QStartupWithShell) != PACKET_DISABLE)
9715 {
9716 xsnprintf (rs->buf, get_remote_packet_size (),
9717 "QStartupWithShell:%d", startup_with_shell ? 1 : 0);
9718 putpkt (rs->buf);
9719 getpkt (&rs->buf, &rs->buf_size, 0);
9720 if (strcmp (rs->buf, "OK") != 0)
9721 error (_("\
9722Remote replied unexpectedly while setting startup-with-shell: %s"),
9723 rs->buf);
9724 }
9725
0a2dde4a
SDJ
9726 extended_remote_environment_support (rs);
9727
bc3b087d
SDJ
9728 extended_remote_set_inferior_cwd (rs);
9729
43ff13b4 9730 /* Now restart the remote server. */
3405876a
PA
9731 run_worked = extended_remote_run (args) != -1;
9732 if (!run_worked)
2d717e4f
DJ
9733 {
9734 /* vRun was not supported. Fail if we need it to do what the
9735 user requested. */
9736 if (remote_exec_file[0])
9737 error (_("Remote target does not support \"set remote exec-file\""));
7c5ded6a 9738 if (!args.empty ())
2d717e4f 9739 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 9740
2d717e4f
DJ
9741 /* Fall back to "R". */
9742 extended_remote_restart ();
9743 }
424163ea 9744
6c95b8df
PA
9745 if (!have_inferiors ())
9746 {
9747 /* Clean up from the last time we ran, before we mark the target
9748 running again. This will mark breakpoints uninserted, and
9749 get_offsets may insert breakpoints. */
9750 init_thread_list ();
9751 init_wait_for_inferior ();
9752 }
45280a52 9753
3405876a
PA
9754 /* vRun's success return is a stop reply. */
9755 stop_reply = run_worked ? rs->buf : NULL;
9756 add_current_inferior_and_thread (stop_reply);
c0a2216e 9757
2d717e4f
DJ
9758 /* Get updated offsets, if the stub uses qOffsets. */
9759 get_offsets ();
2d717e4f 9760}
c906108c 9761\f
c5aa993b 9762
b775012e
LM
9763/* Given a location's target info BP_TGT and the packet buffer BUF, output
9764 the list of conditions (in agent expression bytecode format), if any, the
9765 target needs to evaluate. The output is placed into the packet buffer
bba74b36 9766 started from BUF and ended at BUF_END. */
b775012e
LM
9767
9768static int
9769remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
9770 struct bp_target_info *bp_tgt, char *buf,
9771 char *buf_end)
b775012e 9772{
3cde5c42 9773 if (bp_tgt->conditions.empty ())
b775012e
LM
9774 return 0;
9775
9776 buf += strlen (buf);
bba74b36 9777 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
9778 buf++;
9779
83621223 9780 /* Send conditions to the target. */
d538e36d 9781 for (agent_expr *aexpr : bp_tgt->conditions)
b775012e 9782 {
bba74b36 9783 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e 9784 buf += strlen (buf);
3cde5c42 9785 for (int i = 0; i < aexpr->len; ++i)
b775012e
LM
9786 buf = pack_hex_byte (buf, aexpr->buf[i]);
9787 *buf = '\0';
9788 }
b775012e
LM
9789 return 0;
9790}
9791
d3ce09f5
SS
9792static void
9793remote_add_target_side_commands (struct gdbarch *gdbarch,
9794 struct bp_target_info *bp_tgt, char *buf)
9795{
3cde5c42 9796 if (bp_tgt->tcommands.empty ())
d3ce09f5
SS
9797 return;
9798
9799 buf += strlen (buf);
9800
9801 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
9802 buf += strlen (buf);
9803
9804 /* Concatenate all the agent expressions that are commands into the
9805 cmds parameter. */
df97be55 9806 for (agent_expr *aexpr : bp_tgt->tcommands)
d3ce09f5
SS
9807 {
9808 sprintf (buf, "X%x,", aexpr->len);
9809 buf += strlen (buf);
3cde5c42 9810 for (int i = 0; i < aexpr->len; ++i)
d3ce09f5
SS
9811 buf = pack_hex_byte (buf, aexpr->buf[i]);
9812 *buf = '\0';
9813 }
d3ce09f5
SS
9814}
9815
8181d85f
DJ
9816/* Insert a breakpoint. On targets that have software breakpoint
9817 support, we ask the remote target to do the work; on targets
9818 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
9819
9820static int
3db08215
MM
9821remote_insert_breakpoint (struct target_ops *ops,
9822 struct gdbarch *gdbarch,
a6d9a66e 9823 struct bp_target_info *bp_tgt)
c906108c 9824{
d471ea57
AC
9825 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
9826 If it succeeds, then set the support to PACKET_ENABLE. If it
9827 fails, and the user has explicitly requested the Z support then
23860348 9828 report an error, otherwise, mark it disabled and go on. */
802188a7 9829
4082afcc 9830 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9831 {
0d5ed153 9832 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 9833 struct remote_state *rs;
bba74b36 9834 char *p, *endbuf;
7c0f6dcc 9835 int bpsize;
4fff2411 9836
28439a30
PA
9837 /* Make sure the remote is pointing at the right process, if
9838 necessary. */
9839 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9840 set_general_process ();
9841
4fff2411
JZ
9842 rs = get_remote_state ();
9843 p = rs->buf;
bba74b36 9844 endbuf = rs->buf + get_remote_packet_size ();
802188a7 9845
96baa820
JM
9846 *(p++) = 'Z';
9847 *(p++) = '0';
9848 *(p++) = ',';
7c0f6dcc 9849 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 9850 p += hexnumstr (p, addr);
579c6ad9 9851 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 9852
efcc2da7 9853 if (remote_supports_cond_breakpoints (ops))
bba74b36 9854 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 9855
78eff0ec 9856 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
9857 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9858
6d820c5c
DJ
9859 putpkt (rs->buf);
9860 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9861
6d820c5c 9862 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 9863 {
d471ea57
AC
9864 case PACKET_ERROR:
9865 return -1;
9866 case PACKET_OK:
9867 return 0;
9868 case PACKET_UNKNOWN:
9869 break;
96baa820
JM
9870 }
9871 }
c906108c 9872
0000e5cc
PA
9873 /* If this breakpoint has target-side commands but this stub doesn't
9874 support Z0 packets, throw error. */
3cde5c42 9875 if (!bp_tgt->tcommands.empty ())
0000e5cc
PA
9876 throw_error (NOT_SUPPORTED_ERROR, _("\
9877Target doesn't support breakpoints that have target side commands."));
9878
3db08215 9879 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
9880}
9881
9882static int
3db08215
MM
9883remote_remove_breakpoint (struct target_ops *ops,
9884 struct gdbarch *gdbarch,
73971819
PA
9885 struct bp_target_info *bp_tgt,
9886 enum remove_bp_reason reason)
c906108c 9887{
8181d85f 9888 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 9889 struct remote_state *rs = get_remote_state ();
96baa820 9890
4082afcc 9891 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9892 {
6d820c5c 9893 char *p = rs->buf;
bba74b36 9894 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 9895
28439a30
PA
9896 /* Make sure the remote is pointing at the right process, if
9897 necessary. */
9898 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9899 set_general_process ();
9900
96baa820
JM
9901 *(p++) = 'z';
9902 *(p++) = '0';
9903 *(p++) = ',';
9904
8181d85f
DJ
9905 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
9906 p += hexnumstr (p, addr);
579c6ad9 9907 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 9908
6d820c5c
DJ
9909 putpkt (rs->buf);
9910 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9911
6d820c5c 9912 return (rs->buf[0] == 'E');
96baa820
JM
9913 }
9914
73971819 9915 return memory_remove_breakpoint (ops, gdbarch, bp_tgt, reason);
c906108c
SS
9916}
9917
f486487f 9918static enum Z_packet_type
d471ea57
AC
9919watchpoint_to_Z_packet (int type)
9920{
9921 switch (type)
9922 {
9923 case hw_write:
bb858e6a 9924 return Z_PACKET_WRITE_WP;
d471ea57
AC
9925 break;
9926 case hw_read:
bb858e6a 9927 return Z_PACKET_READ_WP;
d471ea57
AC
9928 break;
9929 case hw_access:
bb858e6a 9930 return Z_PACKET_ACCESS_WP;
d471ea57
AC
9931 break;
9932 default:
8e65ff28 9933 internal_error (__FILE__, __LINE__,
e2e0b3e5 9934 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
9935 }
9936}
9937
3c3bea1c 9938static int
f486487f
SM
9939remote_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9940 enum target_hw_bp_type type, struct expression *cond)
96baa820 9941{
d01949b6 9942 struct remote_state *rs = get_remote_state ();
bba74b36 9943 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9944 char *p;
d471ea57 9945 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 9946
4082afcc 9947 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 9948 return 1;
802188a7 9949
28439a30
PA
9950 /* Make sure the remote is pointing at the right process, if
9951 necessary. */
9952 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9953 set_general_process ();
9954
bba74b36 9955 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 9956 p = strchr (rs->buf, '\0');
96baa820
JM
9957 addr = remote_address_masked (addr);
9958 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9959 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 9960
6d820c5c
DJ
9961 putpkt (rs->buf);
9962 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9963
6d820c5c 9964 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
9965 {
9966 case PACKET_ERROR:
d471ea57 9967 return -1;
85d721b8
PA
9968 case PACKET_UNKNOWN:
9969 return 1;
d471ea57
AC
9970 case PACKET_OK:
9971 return 0;
9972 }
8e65ff28 9973 internal_error (__FILE__, __LINE__,
e2e0b3e5 9974 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
9975}
9976
283002cf
MR
9977static int
9978remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
9979 CORE_ADDR start, int length)
9980{
9981 CORE_ADDR diff = remote_address_masked (addr - start);
9982
9983 return diff < length;
9984}
9985
d471ea57 9986
3c3bea1c 9987static int
f486487f
SM
9988remote_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9989 enum target_hw_bp_type type, struct expression *cond)
96baa820 9990{
d01949b6 9991 struct remote_state *rs = get_remote_state ();
bba74b36 9992 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9993 char *p;
d471ea57
AC
9994 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
9995
4082afcc 9996 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 9997 return -1;
802188a7 9998
28439a30
PA
9999 /* Make sure the remote is pointing at the right process, if
10000 necessary. */
10001 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10002 set_general_process ();
10003
bba74b36 10004 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 10005 p = strchr (rs->buf, '\0');
96baa820
JM
10006 addr = remote_address_masked (addr);
10007 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10008 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
10009 putpkt (rs->buf);
10010 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10011
6d820c5c 10012 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10013 {
10014 case PACKET_ERROR:
10015 case PACKET_UNKNOWN:
10016 return -1;
10017 case PACKET_OK:
10018 return 0;
10019 }
8e65ff28 10020 internal_error (__FILE__, __LINE__,
e2e0b3e5 10021 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
10022}
10023
3c3bea1c 10024
501eef12 10025int remote_hw_watchpoint_limit = -1;
480a3f21 10026int remote_hw_watchpoint_length_limit = -1;
501eef12 10027int remote_hw_breakpoint_limit = -1;
d471ea57 10028
480a3f21 10029static int
31568a15
TT
10030remote_region_ok_for_hw_watchpoint (struct target_ops *self,
10031 CORE_ADDR addr, int len)
480a3f21
PW
10032{
10033 if (remote_hw_watchpoint_length_limit == 0)
10034 return 0;
10035 else if (remote_hw_watchpoint_length_limit < 0)
10036 return 1;
10037 else if (len <= remote_hw_watchpoint_length_limit)
10038 return 1;
10039 else
10040 return 0;
10041}
10042
b9362cc7 10043static int
5461485a 10044remote_check_watch_resources (struct target_ops *self,
f486487f 10045 enum bptype type, int cnt, int ot)
96baa820 10046{
3c3bea1c
GS
10047 if (type == bp_hardware_breakpoint)
10048 {
10049 if (remote_hw_breakpoint_limit == 0)
10050 return 0;
501eef12
AC
10051 else if (remote_hw_breakpoint_limit < 0)
10052 return 1;
3c3bea1c
GS
10053 else if (cnt <= remote_hw_breakpoint_limit)
10054 return 1;
10055 }
10056 else
10057 {
10058 if (remote_hw_watchpoint_limit == 0)
10059 return 0;
501eef12
AC
10060 else if (remote_hw_watchpoint_limit < 0)
10061 return 1;
3c3bea1c
GS
10062 else if (ot)
10063 return -1;
10064 else if (cnt <= remote_hw_watchpoint_limit)
10065 return 1;
10066 }
10067 return -1;
10068}
10069
f7e6eed5
PA
10070/* The to_stopped_by_sw_breakpoint method of target remote. */
10071
10072static int
10073remote_stopped_by_sw_breakpoint (struct target_ops *ops)
10074{
799a2abe 10075 struct thread_info *thread = inferior_thread ();
f7e6eed5 10076
799a2abe
PA
10077 return (thread->priv != NULL
10078 && thread->priv->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
f7e6eed5
PA
10079}
10080
10081/* The to_supports_stopped_by_sw_breakpoint method of target
10082 remote. */
10083
10084static int
10085remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
10086{
f7e6eed5
PA
10087 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
10088}
10089
10090/* The to_stopped_by_hw_breakpoint method of target remote. */
10091
10092static int
10093remote_stopped_by_hw_breakpoint (struct target_ops *ops)
10094{
799a2abe 10095 struct thread_info *thread = inferior_thread ();
f7e6eed5 10096
799a2abe
PA
10097 return (thread->priv != NULL
10098 && thread->priv->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
f7e6eed5
PA
10099}
10100
10101/* The to_supports_stopped_by_hw_breakpoint method of target
10102 remote. */
10103
10104static int
10105remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
10106{
f7e6eed5
PA
10107 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
10108}
10109
b9362cc7 10110static int
6a109b6b 10111remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 10112{
799a2abe 10113 struct thread_info *thread = inferior_thread ();
ee154bee 10114
799a2abe
PA
10115 return (thread->priv != NULL
10116 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT);
3c3bea1c
GS
10117}
10118
4aa7a7f5
JJ
10119static int
10120remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 10121{
799a2abe 10122 struct thread_info *thread = inferior_thread ();
a744cf53 10123
799a2abe
PA
10124 if (thread->priv != NULL
10125 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
4aa7a7f5 10126 {
799a2abe
PA
10127 *addr_p = thread->priv->watch_data_address;
10128 return 1;
4aa7a7f5
JJ
10129 }
10130
799a2abe 10131 return 0;
3c3bea1c
GS
10132}
10133
10134
10135static int
23a26771 10136remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 10137 struct bp_target_info *bp_tgt)
3c3bea1c 10138{
0d5ed153 10139 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10140 struct remote_state *rs;
bba74b36 10141 char *p, *endbuf;
dd61ec5c 10142 char *message;
3c3bea1c 10143
4082afcc 10144 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10145 return -1;
2bc416ba 10146
28439a30
PA
10147 /* Make sure the remote is pointing at the right process, if
10148 necessary. */
10149 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10150 set_general_process ();
10151
4fff2411
JZ
10152 rs = get_remote_state ();
10153 p = rs->buf;
bba74b36 10154 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 10155
96baa820
JM
10156 *(p++) = 'Z';
10157 *(p++) = '1';
10158 *(p++) = ',';
802188a7 10159
0d5ed153 10160 addr = remote_address_masked (addr);
96baa820 10161 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10162 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10163
efcc2da7 10164 if (remote_supports_cond_breakpoints (self))
bba74b36 10165 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10166
78eff0ec 10167 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
10168 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10169
6d820c5c
DJ
10170 putpkt (rs->buf);
10171 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10172
6d820c5c 10173 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10174 {
10175 case PACKET_ERROR:
dd61ec5c
MW
10176 if (rs->buf[1] == '.')
10177 {
10178 message = strchr (rs->buf + 2, '.');
10179 if (message)
0316657e 10180 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
10181 }
10182 return -1;
d471ea57
AC
10183 case PACKET_UNKNOWN:
10184 return -1;
10185 case PACKET_OK:
10186 return 0;
10187 }
8e65ff28 10188 internal_error (__FILE__, __LINE__,
e2e0b3e5 10189 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
10190}
10191
d471ea57 10192
802188a7 10193static int
a64dc96c 10194remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 10195 struct bp_target_info *bp_tgt)
96baa820 10196{
8181d85f 10197 CORE_ADDR addr;
d01949b6 10198 struct remote_state *rs = get_remote_state ();
6d820c5c 10199 char *p = rs->buf;
bba74b36 10200 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 10201
4082afcc 10202 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10203 return -1;
802188a7 10204
28439a30
PA
10205 /* Make sure the remote is pointing at the right process, if
10206 necessary. */
10207 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10208 set_general_process ();
10209
96baa820
JM
10210 *(p++) = 'z';
10211 *(p++) = '1';
10212 *(p++) = ',';
802188a7 10213
8181d85f 10214 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 10215 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10216 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10217
6d820c5c
DJ
10218 putpkt (rs->buf);
10219 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 10220
6d820c5c 10221 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10222 {
10223 case PACKET_ERROR:
10224 case PACKET_UNKNOWN:
10225 return -1;
10226 case PACKET_OK:
10227 return 0;
10228 }
8e65ff28 10229 internal_error (__FILE__, __LINE__,
e2e0b3e5 10230 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 10231}
96baa820 10232
4a5e7a5b
PA
10233/* Verify memory using the "qCRC:" request. */
10234
10235static int
10236remote_verify_memory (struct target_ops *ops,
10237 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
10238{
10239 struct remote_state *rs = get_remote_state ();
10240 unsigned long host_crc, target_crc;
10241 char *tmp;
10242
936d2992
PA
10243 /* It doesn't make sense to use qCRC if the remote target is
10244 connected but not running. */
10245 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
10246 {
10247 enum packet_result result;
28439a30 10248
936d2992
PA
10249 /* Make sure the remote is pointing at the right process. */
10250 set_general_process ();
4a5e7a5b 10251
936d2992
PA
10252 /* FIXME: assumes lma can fit into long. */
10253 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
10254 (long) lma, (long) size);
10255 putpkt (rs->buf);
4a5e7a5b 10256
936d2992
PA
10257 /* Be clever; compute the host_crc before waiting for target
10258 reply. */
10259 host_crc = xcrc32 (data, size, 0xffffffff);
10260
10261 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 10262
936d2992
PA
10263 result = packet_ok (rs->buf,
10264 &remote_protocol_packets[PACKET_qCRC]);
10265 if (result == PACKET_ERROR)
10266 return -1;
10267 else if (result == PACKET_OK)
10268 {
10269 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
10270 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 10271
936d2992
PA
10272 return (host_crc == target_crc);
10273 }
10274 }
4a5e7a5b 10275
936d2992 10276 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
10277}
10278
c906108c
SS
10279/* compare-sections command
10280
10281 With no arguments, compares each loadable section in the exec bfd
10282 with the same memory range on the target, and reports mismatches.
4a5e7a5b 10283 Useful for verifying the image on the target against the exec file. */
e514a9d6 10284
c906108c 10285static void
ac88e2de 10286compare_sections_command (const char *args, int from_tty)
c906108c
SS
10287{
10288 asection *s;
c906108c 10289 struct cleanup *old_chain;
948f8e3d 10290 gdb_byte *sectdata;
ce359b09 10291 const char *sectname;
c906108c
SS
10292 bfd_size_type size;
10293 bfd_vma lma;
10294 int matched = 0;
10295 int mismatched = 0;
4a5e7a5b 10296 int res;
95cf3b38 10297 int read_only = 0;
c906108c
SS
10298
10299 if (!exec_bfd)
8a3fe4f8 10300 error (_("command cannot be used without an exec file"));
c906108c 10301
28439a30
PA
10302 /* Make sure the remote is pointing at the right process. */
10303 set_general_process ();
10304
95cf3b38
DT
10305 if (args != NULL && strcmp (args, "-r") == 0)
10306 {
10307 read_only = 1;
10308 args = NULL;
10309 }
10310
c5aa993b 10311 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
10312 {
10313 if (!(s->flags & SEC_LOAD))
0df8b418 10314 continue; /* Skip non-loadable section. */
c906108c 10315
95cf3b38
DT
10316 if (read_only && (s->flags & SEC_READONLY) == 0)
10317 continue; /* Skip writeable sections */
10318
2c500098 10319 size = bfd_get_section_size (s);
c906108c 10320 if (size == 0)
0df8b418 10321 continue; /* Skip zero-length section. */
c906108c 10322
ce359b09 10323 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 10324 if (args && strcmp (args, sectname) != 0)
0df8b418 10325 continue; /* Not the section selected by user. */
c906108c 10326
0df8b418 10327 matched = 1; /* Do this section. */
c906108c 10328 lma = s->lma;
c906108c 10329
224c3ddb 10330 sectdata = (gdb_byte *) xmalloc (size);
b8c9b27d 10331 old_chain = make_cleanup (xfree, sectdata);
c906108c 10332 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 10333
4a5e7a5b
PA
10334 res = target_verify_memory (sectdata, lma, size);
10335
10336 if (res == -1)
5af949e3 10337 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
10338 paddress (target_gdbarch (), lma),
10339 paddress (target_gdbarch (), lma + size));
c906108c 10340
5af949e3 10341 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
10342 paddress (target_gdbarch (), lma),
10343 paddress (target_gdbarch (), lma + size));
4a5e7a5b 10344 if (res)
c906108c
SS
10345 printf_filtered ("matched.\n");
10346 else
c5aa993b
JM
10347 {
10348 printf_filtered ("MIS-MATCHED!\n");
10349 mismatched++;
10350 }
c906108c
SS
10351
10352 do_cleanups (old_chain);
10353 }
10354 if (mismatched > 0)
936d2992 10355 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 10356the loaded file\n"));
c906108c 10357 if (args && !matched)
a3f17187 10358 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
10359}
10360
0e7f50da
UW
10361/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
10362 into remote target. The number of bytes written to the remote
10363 target is returned, or -1 for error. */
10364
9b409511 10365static enum target_xfer_status
0e7f50da
UW
10366remote_write_qxfer (struct target_ops *ops, const char *object_name,
10367 const char *annex, const gdb_byte *writebuf,
9b409511 10368 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
10369 struct packet_config *packet)
10370{
10371 int i, buf_len;
10372 ULONGEST n;
0e7f50da
UW
10373 struct remote_state *rs = get_remote_state ();
10374 int max_size = get_memory_write_packet_size ();
10375
10376 if (packet->support == PACKET_DISABLE)
2ed4b548 10377 return TARGET_XFER_E_IO;
0e7f50da
UW
10378
10379 /* Insert header. */
10380 i = snprintf (rs->buf, max_size,
10381 "qXfer:%s:write:%s:%s:",
10382 object_name, annex ? annex : "",
10383 phex_nz (offset, sizeof offset));
10384 max_size -= (i + 1);
10385
10386 /* Escape as much data as fits into rs->buf. */
10387 buf_len = remote_escape_output
124e13d9 10388 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
10389
10390 if (putpkt_binary (rs->buf, i + buf_len) < 0
10391 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10392 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10393 return TARGET_XFER_E_IO;
0e7f50da
UW
10394
10395 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
10396
10397 *xfered_len = n;
10398 return TARGET_XFER_OK;
0e7f50da
UW
10399}
10400
0876f84a
DJ
10401/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
10402 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
10403 number of bytes read is returned, or 0 for EOF, or -1 for error.
10404 The number of bytes read may be less than LEN without indicating an
10405 EOF. PACKET is checked and updated to indicate whether the remote
10406 target supports this object. */
10407
9b409511 10408static enum target_xfer_status
0876f84a
DJ
10409remote_read_qxfer (struct target_ops *ops, const char *object_name,
10410 const char *annex,
10411 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 10412 ULONGEST *xfered_len,
0876f84a
DJ
10413 struct packet_config *packet)
10414{
0876f84a 10415 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
10416 LONGEST i, n, packet_len;
10417
10418 if (packet->support == PACKET_DISABLE)
2ed4b548 10419 return TARGET_XFER_E_IO;
0876f84a
DJ
10420
10421 /* Check whether we've cached an end-of-object packet that matches
10422 this request. */
8e88304f 10423 if (rs->finished_object)
0876f84a 10424 {
8e88304f
TT
10425 if (strcmp (object_name, rs->finished_object) == 0
10426 && strcmp (annex ? annex : "", rs->finished_annex) == 0
10427 && offset == rs->finished_offset)
9b409511
YQ
10428 return TARGET_XFER_EOF;
10429
0876f84a
DJ
10430
10431 /* Otherwise, we're now reading something different. Discard
10432 the cache. */
8e88304f
TT
10433 xfree (rs->finished_object);
10434 xfree (rs->finished_annex);
10435 rs->finished_object = NULL;
10436 rs->finished_annex = NULL;
0876f84a
DJ
10437 }
10438
10439 /* Request only enough to fit in a single packet. The actual data
10440 may not, since we don't know how much of it will need to be escaped;
10441 the target is free to respond with slightly less data. We subtract
10442 five to account for the response type and the protocol frame. */
768adc05 10443 n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
0876f84a
DJ
10444 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
10445 object_name, annex ? annex : "",
10446 phex_nz (offset, sizeof offset),
10447 phex_nz (n, sizeof n));
10448 i = putpkt (rs->buf);
10449 if (i < 0)
2ed4b548 10450 return TARGET_XFER_E_IO;
0876f84a
DJ
10451
10452 rs->buf[0] = '\0';
10453 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10454 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10455 return TARGET_XFER_E_IO;
0876f84a
DJ
10456
10457 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
10458 error (_("Unknown remote qXfer reply: %s"), rs->buf);
10459
10460 /* 'm' means there is (or at least might be) more data after this
10461 batch. That does not make sense unless there's at least one byte
10462 of data in this reply. */
10463 if (rs->buf[0] == 'm' && packet_len == 1)
10464 error (_("Remote qXfer reply contained no data."));
10465
10466 /* Got some data. */
bc20a4af
PA
10467 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
10468 packet_len - 1, readbuf, n);
0876f84a
DJ
10469
10470 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
10471 or possibly empty. If we have the final block of a non-empty
10472 object, record this fact to bypass a subsequent partial read. */
10473 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 10474 {
8e88304f
TT
10475 rs->finished_object = xstrdup (object_name);
10476 rs->finished_annex = xstrdup (annex ? annex : "");
10477 rs->finished_offset = offset + i;
0876f84a
DJ
10478 }
10479
9b409511
YQ
10480 if (i == 0)
10481 return TARGET_XFER_EOF;
10482 else
10483 {
10484 *xfered_len = i;
10485 return TARGET_XFER_OK;
10486 }
0876f84a
DJ
10487}
10488
9b409511 10489static enum target_xfer_status
4b8a223f 10490remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 10491 const char *annex, gdb_byte *readbuf,
9b409511
YQ
10492 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
10493 ULONGEST *xfered_len)
c906108c 10494{
82f73884 10495 struct remote_state *rs;
c906108c 10496 int i;
6d820c5c 10497 char *p2;
1e3ff5ad 10498 char query_type;
124e13d9 10499 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 10500
e6e4e701 10501 set_remote_traceframe ();
82f73884
PA
10502 set_general_thread (inferior_ptid);
10503
10504 rs = get_remote_state ();
10505
b2182ed2 10506 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
10507 if (object == TARGET_OBJECT_MEMORY)
10508 {
2d717e4f
DJ
10509 /* If the remote target is connected but not running, we should
10510 pass this request down to a lower stratum (e.g. the executable
10511 file). */
10512 if (!target_has_execution)
9b409511 10513 return TARGET_XFER_EOF;
2d717e4f 10514
21e3b9b9 10515 if (writebuf != NULL)
124e13d9
SM
10516 return remote_write_bytes (offset, writebuf, len, unit_size,
10517 xfered_len);
21e3b9b9 10518 else
124e13d9
SM
10519 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
10520 xfered_len);
21e3b9b9
DJ
10521 }
10522
0df8b418 10523 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
10524 if (object == TARGET_OBJECT_SPU)
10525 {
10526 if (readbuf)
10527 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
10528 xfered_len, &remote_protocol_packets
10529 [PACKET_qXfer_spu_read]);
0e7f50da
UW
10530 else
10531 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
10532 xfered_len, &remote_protocol_packets
10533 [PACKET_qXfer_spu_write]);
0e7f50da
UW
10534 }
10535
4aa995e1
PA
10536 /* Handle extra signal info using qxfer packets. */
10537 if (object == TARGET_OBJECT_SIGNAL_INFO)
10538 {
10539 if (readbuf)
10540 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 10541 xfered_len, &remote_protocol_packets
4aa995e1
PA
10542 [PACKET_qXfer_siginfo_read]);
10543 else
3e43a32a 10544 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 10545 writebuf, offset, len, xfered_len,
4aa995e1
PA
10546 &remote_protocol_packets
10547 [PACKET_qXfer_siginfo_write]);
10548 }
10549
0fb4aa4b
PA
10550 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
10551 {
10552 if (readbuf)
3e43a32a 10553 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 10554 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
10555 &remote_protocol_packets
10556 [PACKET_qXfer_statictrace_read]);
10557 else
2ed4b548 10558 return TARGET_XFER_E_IO;
0fb4aa4b
PA
10559 }
10560
a76d924d
DJ
10561 /* Only handle flash writes. */
10562 if (writebuf != NULL)
10563 {
a76d924d
DJ
10564 switch (object)
10565 {
10566 case TARGET_OBJECT_FLASH:
9b409511
YQ
10567 return remote_flash_write (ops, offset, len, xfered_len,
10568 writebuf);
a76d924d
DJ
10569
10570 default:
2ed4b548 10571 return TARGET_XFER_E_IO;
a76d924d
DJ
10572 }
10573 }
4b8a223f 10574
1e3ff5ad
AC
10575 /* Map pre-existing objects onto letters. DO NOT do this for new
10576 objects!!! Instead specify new query packets. */
10577 switch (object)
c906108c 10578 {
1e3ff5ad
AC
10579 case TARGET_OBJECT_AVR:
10580 query_type = 'R';
10581 break;
802188a7
RM
10582
10583 case TARGET_OBJECT_AUXV:
0876f84a
DJ
10584 gdb_assert (annex == NULL);
10585 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 10586 xfered_len,
0876f84a 10587 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 10588
23181151
DJ
10589 case TARGET_OBJECT_AVAILABLE_FEATURES:
10590 return remote_read_qxfer
9b409511 10591 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
10592 &remote_protocol_packets[PACKET_qXfer_features]);
10593
cfa9d6d9
DJ
10594 case TARGET_OBJECT_LIBRARIES:
10595 return remote_read_qxfer
9b409511 10596 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
10597 &remote_protocol_packets[PACKET_qXfer_libraries]);
10598
2268b414
JK
10599 case TARGET_OBJECT_LIBRARIES_SVR4:
10600 return remote_read_qxfer
9b409511 10601 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
10602 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
10603
fd79ecee
DJ
10604 case TARGET_OBJECT_MEMORY_MAP:
10605 gdb_assert (annex == NULL);
10606 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 10607 xfered_len,
fd79ecee
DJ
10608 &remote_protocol_packets[PACKET_qXfer_memory_map]);
10609
07e059b5
VP
10610 case TARGET_OBJECT_OSDATA:
10611 /* Should only get here if we're connected. */
5d93a237 10612 gdb_assert (rs->remote_desc);
07e059b5 10613 return remote_read_qxfer
9b409511 10614 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
10615 &remote_protocol_packets[PACKET_qXfer_osdata]);
10616
dc146f7c
VP
10617 case TARGET_OBJECT_THREADS:
10618 gdb_assert (annex == NULL);
10619 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 10620 xfered_len,
dc146f7c
VP
10621 &remote_protocol_packets[PACKET_qXfer_threads]);
10622
b3b9301e
PA
10623 case TARGET_OBJECT_TRACEFRAME_INFO:
10624 gdb_assert (annex == NULL);
10625 return remote_read_qxfer
9b409511 10626 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 10627 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
10628
10629 case TARGET_OBJECT_FDPIC:
10630 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 10631 xfered_len,
78d85199 10632 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
10633
10634 case TARGET_OBJECT_OPENVMS_UIB:
10635 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 10636 xfered_len,
169081d0
TG
10637 &remote_protocol_packets[PACKET_qXfer_uib]);
10638
9accd112
MM
10639 case TARGET_OBJECT_BTRACE:
10640 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 10641 xfered_len,
9accd112
MM
10642 &remote_protocol_packets[PACKET_qXfer_btrace]);
10643
f4abbc16
MM
10644 case TARGET_OBJECT_BTRACE_CONF:
10645 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
10646 len, xfered_len,
10647 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
10648
c78fa86a
GB
10649 case TARGET_OBJECT_EXEC_FILE:
10650 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
10651 len, xfered_len,
10652 &remote_protocol_packets[PACKET_qXfer_exec_file]);
10653
1e3ff5ad 10654 default:
2ed4b548 10655 return TARGET_XFER_E_IO;
c906108c
SS
10656 }
10657
0df8b418 10658 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 10659 large enough let the caller deal with it. */
ea9c271d 10660 if (len < get_remote_packet_size ())
2ed4b548 10661 return TARGET_XFER_E_IO;
ea9c271d 10662 len = get_remote_packet_size ();
1e3ff5ad 10663
23860348 10664 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 10665 if (!rs->remote_desc)
8a3fe4f8 10666 error (_("remote query is only available after target open"));
c906108c 10667
1e3ff5ad 10668 gdb_assert (annex != NULL);
4b8a223f 10669 gdb_assert (readbuf != NULL);
c906108c 10670
6d820c5c 10671 p2 = rs->buf;
c906108c
SS
10672 *p2++ = 'q';
10673 *p2++ = query_type;
10674
23860348
MS
10675 /* We used one buffer char for the remote protocol q command and
10676 another for the query type. As the remote protocol encapsulation
10677 uses 4 chars plus one extra in case we are debugging
10678 (remote_debug), we have PBUFZIZ - 7 left to pack the query
10679 string. */
c906108c 10680 i = 0;
ea9c271d 10681 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 10682 {
1e3ff5ad
AC
10683 /* Bad caller may have sent forbidden characters. */
10684 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
10685 *p2++ = annex[i];
c906108c
SS
10686 i++;
10687 }
1e3ff5ad
AC
10688 *p2 = '\0';
10689 gdb_assert (annex[i] == '\0');
c906108c 10690
6d820c5c 10691 i = putpkt (rs->buf);
c5aa993b 10692 if (i < 0)
2ed4b548 10693 return TARGET_XFER_E_IO;
c906108c 10694
6d820c5c
DJ
10695 getpkt (&rs->buf, &rs->buf_size, 0);
10696 strcpy ((char *) readbuf, rs->buf);
c906108c 10697
9b409511
YQ
10698 *xfered_len = strlen ((char *) readbuf);
10699 return TARGET_XFER_OK;
c906108c
SS
10700}
10701
09c98b44
DB
10702/* Implementation of to_get_memory_xfer_limit. */
10703
10704static ULONGEST
10705remote_get_memory_xfer_limit (struct target_ops *ops)
10706{
10707 return get_memory_write_packet_size ();
10708}
10709
08388c79
DE
10710static int
10711remote_search_memory (struct target_ops* ops,
10712 CORE_ADDR start_addr, ULONGEST search_space_len,
10713 const gdb_byte *pattern, ULONGEST pattern_len,
10714 CORE_ADDR *found_addrp)
10715{
f5656ead 10716 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
10717 struct remote_state *rs = get_remote_state ();
10718 int max_size = get_memory_write_packet_size ();
10719 struct packet_config *packet =
10720 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
10721 /* Number of packet bytes used to encode the pattern;
10722 this could be more than PATTERN_LEN due to escape characters. */
08388c79 10723 int escaped_pattern_len;
0df8b418 10724 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
10725 int used_pattern_len;
10726 int i;
10727 int found;
10728 ULONGEST found_addr;
10729
10730 /* Don't go to the target if we don't have to.
10731 This is done before checking packet->support to avoid the possibility that
10732 a success for this edge case means the facility works in general. */
10733 if (pattern_len > search_space_len)
10734 return 0;
10735 if (pattern_len == 0)
10736 {
10737 *found_addrp = start_addr;
10738 return 1;
10739 }
10740
10741 /* If we already know the packet isn't supported, fall back to the simple
10742 way of searching memory. */
10743
4082afcc 10744 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
10745 {
10746 /* Target doesn't provided special support, fall back and use the
10747 standard support (copy memory and do the search here). */
10748 return simple_search_memory (ops, start_addr, search_space_len,
10749 pattern, pattern_len, found_addrp);
10750 }
10751
28439a30
PA
10752 /* Make sure the remote is pointing at the right process. */
10753 set_general_process ();
10754
08388c79
DE
10755 /* Insert header. */
10756 i = snprintf (rs->buf, max_size,
10757 "qSearch:memory:%s;%s;",
5af949e3 10758 phex_nz (start_addr, addr_size),
08388c79
DE
10759 phex_nz (search_space_len, sizeof (search_space_len)));
10760 max_size -= (i + 1);
10761
10762 /* Escape as much data as fits into rs->buf. */
10763 escaped_pattern_len =
124e13d9 10764 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
10765 &used_pattern_len, max_size);
10766
10767 /* Bail if the pattern is too large. */
10768 if (used_pattern_len != pattern_len)
9b20d036 10769 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
10770
10771 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
10772 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10773 || packet_ok (rs->buf, packet) != PACKET_OK)
10774 {
10775 /* The request may not have worked because the command is not
10776 supported. If so, fall back to the simple way. */
10777 if (packet->support == PACKET_DISABLE)
10778 {
10779 return simple_search_memory (ops, start_addr, search_space_len,
10780 pattern, pattern_len, found_addrp);
10781 }
10782 return -1;
10783 }
10784
10785 if (rs->buf[0] == '0')
10786 found = 0;
10787 else if (rs->buf[0] == '1')
10788 {
10789 found = 1;
10790 if (rs->buf[1] != ',')
10e0fa18 10791 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10792 unpack_varlen_hex (rs->buf + 2, &found_addr);
10793 *found_addrp = found_addr;
10794 }
10795 else
10e0fa18 10796 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10797
10798 return found;
10799}
10800
96baa820 10801static void
a30bf1f1 10802remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 10803 struct ui_file *outbuf)
96baa820 10804{
d01949b6 10805 struct remote_state *rs = get_remote_state ();
2e9f7625 10806 char *p = rs->buf;
96baa820 10807
5d93a237 10808 if (!rs->remote_desc)
8a3fe4f8 10809 error (_("remote rcmd is only available after target open"));
96baa820 10810
23860348 10811 /* Send a NULL command across as an empty command. */
7be570e7
JM
10812 if (command == NULL)
10813 command = "";
10814
23860348 10815 /* The query prefix. */
2e9f7625
DJ
10816 strcpy (rs->buf, "qRcmd,");
10817 p = strchr (rs->buf, '\0');
96baa820 10818
3e43a32a
MS
10819 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
10820 > get_remote_packet_size ())
8a3fe4f8 10821 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 10822
23860348 10823 /* Encode the actual command. */
a30bf1f1 10824 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 10825
6d820c5c 10826 if (putpkt (rs->buf) < 0)
8a3fe4f8 10827 error (_("Communication problem with target."));
96baa820
JM
10828
10829 /* get/display the response */
10830 while (1)
10831 {
2e9f7625
DJ
10832 char *buf;
10833
00bf0b85 10834 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 10835 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 10836 rs->buf[0] = '\0';
5b37825d
PW
10837 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
10838 {
10839 /* Timeout. Continue to (try to) read responses.
10840 This is better than stopping with an error, assuming the stub
10841 is still executing the (long) monitor command.
10842 If needed, the user can interrupt gdb using C-c, obtaining
10843 an effect similar to stop on timeout. */
10844 continue;
10845 }
2e9f7625 10846 buf = rs->buf;
96baa820 10847 if (buf[0] == '\0')
8a3fe4f8 10848 error (_("Target does not support this command."));
96baa820
JM
10849 if (buf[0] == 'O' && buf[1] != 'K')
10850 {
23860348 10851 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
10852 continue;
10853 }
10854 if (strcmp (buf, "OK") == 0)
10855 break;
7be570e7
JM
10856 if (strlen (buf) == 3 && buf[0] == 'E'
10857 && isdigit (buf[1]) && isdigit (buf[2]))
10858 {
8a3fe4f8 10859 error (_("Protocol error with Rcmd"));
7be570e7 10860 }
96baa820
JM
10861 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
10862 {
10863 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 10864
96baa820
JM
10865 fputc_unfiltered (c, outbuf);
10866 }
10867 break;
10868 }
10869}
10870
fd79ecee
DJ
10871static VEC(mem_region_s) *
10872remote_memory_map (struct target_ops *ops)
10873{
10874 VEC(mem_region_s) *result = NULL;
10875 char *text = target_read_stralloc (&current_target,
10876 TARGET_OBJECT_MEMORY_MAP, NULL);
10877
10878 if (text)
10879 {
10880 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 10881
fd79ecee
DJ
10882 result = parse_memory_map (text);
10883 do_cleanups (back_to);
10884 }
10885
10886 return result;
10887}
10888
c906108c 10889static void
ac88e2de 10890packet_command (const char *args, int from_tty)
c906108c 10891{
d01949b6 10892 struct remote_state *rs = get_remote_state ();
c906108c 10893
5d93a237 10894 if (!rs->remote_desc)
8a3fe4f8 10895 error (_("command can only be used with remote target"));
c906108c 10896
c5aa993b 10897 if (!args)
8a3fe4f8 10898 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
10899
10900 puts_filtered ("sending: ");
10901 print_packet (args);
10902 puts_filtered ("\n");
10903 putpkt (args);
10904
6d820c5c 10905 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 10906 puts_filtered ("received: ");
6d820c5c 10907 print_packet (rs->buf);
c906108c
SS
10908 puts_filtered ("\n");
10909}
10910
10911#if 0
23860348 10912/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 10913
a14ed312 10914static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 10915
a14ed312 10916static void threadset_test_cmd (char *cmd, int tty);
c906108c 10917
a14ed312 10918static void threadalive_test (char *cmd, int tty);
c906108c 10919
a14ed312 10920static void threadlist_test_cmd (char *cmd, int tty);
c906108c 10921
23860348 10922int get_and_display_threadinfo (threadref *ref);
c906108c 10923
a14ed312 10924static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 10925
23860348 10926static int thread_display_step (threadref *ref, void *context);
c906108c 10927
a14ed312 10928static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 10929
a14ed312 10930static void init_remote_threadtests (void);
c906108c 10931
23860348 10932#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
10933
10934static void
fba45db2 10935threadset_test_cmd (char *cmd, int tty)
c906108c
SS
10936{
10937 int sample_thread = SAMPLE_THREAD;
10938
a3f17187 10939 printf_filtered (_("Remote threadset test\n"));
79d7f229 10940 set_general_thread (sample_thread);
c906108c
SS
10941}
10942
10943
10944static void
fba45db2 10945threadalive_test (char *cmd, int tty)
c906108c
SS
10946{
10947 int sample_thread = SAMPLE_THREAD;
79d7f229 10948 int pid = ptid_get_pid (inferior_ptid);
ba348170 10949 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 10950
79d7f229 10951 if (remote_thread_alive (ptid))
c906108c
SS
10952 printf_filtered ("PASS: Thread alive test\n");
10953 else
10954 printf_filtered ("FAIL: Thread alive test\n");
10955}
10956
23860348 10957void output_threadid (char *title, threadref *ref);
c906108c
SS
10958
10959void
fba45db2 10960output_threadid (char *title, threadref *ref)
c906108c
SS
10961{
10962 char hexid[20];
10963
23860348 10964 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
10965 hexid[16] = 0;
10966 printf_filtered ("%s %s\n", title, (&hexid[0]));
10967}
10968
10969static void
fba45db2 10970threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
10971{
10972 int startflag = 1;
10973 threadref nextthread;
10974 int done, result_count;
10975 threadref threadlist[3];
10976
10977 printf_filtered ("Remote Threadlist test\n");
10978 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
10979 &result_count, &threadlist[0]))
10980 printf_filtered ("FAIL: threadlist test\n");
10981 else
10982 {
10983 threadref *scan = threadlist;
10984 threadref *limit = scan + result_count;
10985
10986 while (scan < limit)
10987 output_threadid (" thread ", scan++);
10988 }
10989}
10990
10991void
fba45db2 10992display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
10993{
10994 output_threadid ("Threadid: ", &info->threadid);
10995 printf_filtered ("Name: %s\n ", info->shortname);
10996 printf_filtered ("State: %s\n", info->display);
10997 printf_filtered ("other: %s\n\n", info->more_display);
10998}
10999
11000int
fba45db2 11001get_and_display_threadinfo (threadref *ref)
c906108c
SS
11002{
11003 int result;
11004 int set;
11005 struct gdb_ext_thread_info threadinfo;
11006
11007 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
11008 | TAG_MOREDISPLAY | TAG_DISPLAY;
11009 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
11010 display_thread_info (&threadinfo);
11011 return result;
11012}
11013
11014static void
fba45db2 11015threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
11016{
11017 int athread = SAMPLE_THREAD;
11018 threadref thread;
11019 int set;
11020
11021 int_to_threadref (&thread, athread);
11022 printf_filtered ("Remote Threadinfo test\n");
11023 if (!get_and_display_threadinfo (&thread))
11024 printf_filtered ("FAIL cannot get thread info\n");
11025}
11026
11027static int
fba45db2 11028thread_display_step (threadref *ref, void *context)
c906108c
SS
11029{
11030 /* output_threadid(" threadstep ",ref); *//* simple test */
11031 return get_and_display_threadinfo (ref);
11032}
11033
11034static void
fba45db2 11035threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
11036{
11037 printf_filtered ("Remote Threadlist update test\n");
11038 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
11039}
11040
11041static void
11042init_remote_threadtests (void)
11043{
3e43a32a
MS
11044 add_com ("tlist", class_obscure, threadlist_test_cmd,
11045 _("Fetch and print the remote list of "
11046 "thread identifiers, one pkt only"));
c906108c 11047 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 11048 _("Fetch and display info about one thread"));
c906108c 11049 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 11050 _("Test setting to a different thread"));
c906108c 11051 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 11052 _("Iterate through updating all remote thread info"));
c906108c 11053 add_com ("talive", class_obscure, threadalive_test,
1bedd215 11054 _(" Remote thread alive test "));
c906108c
SS
11055}
11056
11057#endif /* 0 */
11058
f3fb8c85
MS
11059/* Convert a thread ID to a string. Returns the string in a static
11060 buffer. */
11061
7a114964 11062static const char *
117de6a9 11063remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 11064{
79d7f229 11065 static char buf[64];
82f73884 11066 struct remote_state *rs = get_remote_state ();
f3fb8c85 11067
7cee1e54
PA
11068 if (ptid_equal (ptid, null_ptid))
11069 return normal_pid_to_str (ptid);
11070 else if (ptid_is_pid (ptid))
ecd0ada5
PA
11071 {
11072 /* Printing an inferior target id. */
11073
11074 /* When multi-process extensions are off, there's no way in the
11075 remote protocol to know the remote process id, if there's any
11076 at all. There's one exception --- when we're connected with
11077 target extended-remote, and we manually attached to a process
11078 with "attach PID". We don't record anywhere a flag that
11079 allows us to distinguish that case from the case of
11080 connecting with extended-remote and the stub already being
11081 attached to a process, and reporting yes to qAttached, hence
11082 no smart special casing here. */
11083 if (!remote_multi_process_p (rs))
11084 {
11085 xsnprintf (buf, sizeof buf, "Remote target");
11086 return buf;
11087 }
11088
11089 return normal_pid_to_str (ptid);
82f73884 11090 }
ecd0ada5 11091 else
79d7f229 11092 {
ecd0ada5
PA
11093 if (ptid_equal (magic_null_ptid, ptid))
11094 xsnprintf (buf, sizeof buf, "Thread <main>");
8020350c 11095 else if (remote_multi_process_p (rs))
de0d863e
DB
11096 if (ptid_get_lwp (ptid) == 0)
11097 return normal_pid_to_str (ptid);
11098 else
11099 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
11100 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
11101 else
11102 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 11103 ptid_get_lwp (ptid));
79d7f229
PA
11104 return buf;
11105 }
f3fb8c85
MS
11106}
11107
38691318
KB
11108/* Get the address of the thread local variable in OBJFILE which is
11109 stored at OFFSET within the thread local storage for thread PTID. */
11110
11111static CORE_ADDR
117de6a9
PA
11112remote_get_thread_local_address (struct target_ops *ops,
11113 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 11114{
4082afcc 11115 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
11116 {
11117 struct remote_state *rs = get_remote_state ();
6d820c5c 11118 char *p = rs->buf;
82f73884 11119 char *endp = rs->buf + get_remote_packet_size ();
571dd617 11120 enum packet_result result;
38691318
KB
11121
11122 strcpy (p, "qGetTLSAddr:");
11123 p += strlen (p);
82f73884 11124 p = write_ptid (p, endp, ptid);
38691318
KB
11125 *p++ = ',';
11126 p += hexnumstr (p, offset);
11127 *p++ = ',';
11128 p += hexnumstr (p, lm);
11129 *p++ = '\0';
11130
6d820c5c
DJ
11131 putpkt (rs->buf);
11132 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
11133 result = packet_ok (rs->buf,
11134 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 11135 if (result == PACKET_OK)
38691318
KB
11136 {
11137 ULONGEST result;
11138
6d820c5c 11139 unpack_varlen_hex (rs->buf, &result);
38691318
KB
11140 return result;
11141 }
571dd617 11142 else if (result == PACKET_UNKNOWN)
109c3e39
AC
11143 throw_error (TLS_GENERIC_ERROR,
11144 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 11145 else
109c3e39
AC
11146 throw_error (TLS_GENERIC_ERROR,
11147 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
11148 }
11149 else
109c3e39
AC
11150 throw_error (TLS_GENERIC_ERROR,
11151 _("TLS not supported or disabled on this target"));
38691318
KB
11152 /* Not reached. */
11153 return 0;
11154}
11155
711e434b
PM
11156/* Provide thread local base, i.e. Thread Information Block address.
11157 Returns 1 if ptid is found and thread_local_base is non zero. */
11158
70221824 11159static int
bd7ae0f5 11160remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 11161{
4082afcc 11162 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
11163 {
11164 struct remote_state *rs = get_remote_state ();
11165 char *p = rs->buf;
11166 char *endp = rs->buf + get_remote_packet_size ();
11167 enum packet_result result;
11168
11169 strcpy (p, "qGetTIBAddr:");
11170 p += strlen (p);
11171 p = write_ptid (p, endp, ptid);
11172 *p++ = '\0';
11173
11174 putpkt (rs->buf);
11175 getpkt (&rs->buf, &rs->buf_size, 0);
11176 result = packet_ok (rs->buf,
11177 &remote_protocol_packets[PACKET_qGetTIBAddr]);
11178 if (result == PACKET_OK)
11179 {
11180 ULONGEST result;
11181
11182 unpack_varlen_hex (rs->buf, &result);
11183 if (addr)
11184 *addr = (CORE_ADDR) result;
11185 return 1;
11186 }
11187 else if (result == PACKET_UNKNOWN)
11188 error (_("Remote target doesn't support qGetTIBAddr packet"));
11189 else
11190 error (_("Remote target failed to process qGetTIBAddr request"));
11191 }
11192 else
11193 error (_("qGetTIBAddr not supported or disabled on this target"));
11194 /* Not reached. */
11195 return 0;
11196}
11197
29709017
DJ
11198/* Support for inferring a target description based on the current
11199 architecture and the size of a 'g' packet. While the 'g' packet
11200 can have any size (since optional registers can be left off the
11201 end), some sizes are easily recognizable given knowledge of the
11202 approximate architecture. */
11203
11204struct remote_g_packet_guess
11205{
11206 int bytes;
11207 const struct target_desc *tdesc;
11208};
11209typedef struct remote_g_packet_guess remote_g_packet_guess_s;
11210DEF_VEC_O(remote_g_packet_guess_s);
11211
11212struct remote_g_packet_data
11213{
11214 VEC(remote_g_packet_guess_s) *guesses;
11215};
11216
11217static struct gdbarch_data *remote_g_packet_data_handle;
11218
11219static void *
11220remote_g_packet_data_init (struct obstack *obstack)
11221{
11222 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
11223}
11224
11225void
11226register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
11227 const struct target_desc *tdesc)
11228{
11229 struct remote_g_packet_data *data
19ba03f4
SM
11230 = ((struct remote_g_packet_data *)
11231 gdbarch_data (gdbarch, remote_g_packet_data_handle));
29709017
DJ
11232 struct remote_g_packet_guess new_guess, *guess;
11233 int ix;
11234
11235 gdb_assert (tdesc != NULL);
11236
11237 for (ix = 0;
11238 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11239 ix++)
11240 if (guess->bytes == bytes)
11241 internal_error (__FILE__, __LINE__,
9b20d036 11242 _("Duplicate g packet description added for size %d"),
29709017
DJ
11243 bytes);
11244
11245 new_guess.bytes = bytes;
11246 new_guess.tdesc = tdesc;
11247 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
11248}
11249
d962ef82
DJ
11250/* Return 1 if remote_read_description would do anything on this target
11251 and architecture, 0 otherwise. */
11252
11253static int
11254remote_read_description_p (struct target_ops *target)
11255{
11256 struct remote_g_packet_data *data
19ba03f4
SM
11257 = ((struct remote_g_packet_data *)
11258 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
d962ef82
DJ
11259
11260 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11261 return 1;
11262
11263 return 0;
11264}
11265
29709017
DJ
11266static const struct target_desc *
11267remote_read_description (struct target_ops *target)
11268{
11269 struct remote_g_packet_data *data
19ba03f4
SM
11270 = ((struct remote_g_packet_data *)
11271 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
29709017 11272
d962ef82
DJ
11273 /* Do not try this during initial connection, when we do not know
11274 whether there is a running but stopped thread. */
11275 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 11276 return target->beneath->to_read_description (target->beneath);
d962ef82 11277
29709017
DJ
11278 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11279 {
11280 struct remote_g_packet_guess *guess;
11281 int ix;
11282 int bytes = send_g_packet ();
11283
11284 for (ix = 0;
11285 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11286 ix++)
11287 if (guess->bytes == bytes)
11288 return guess->tdesc;
11289
11290 /* We discard the g packet. A minor optimization would be to
11291 hold on to it, and fill the register cache once we have selected
11292 an architecture, but it's too tricky to do safely. */
11293 }
11294
2117c711 11295 return target->beneath->to_read_description (target->beneath);
29709017
DJ
11296}
11297
a6b151f1
DJ
11298/* Remote file transfer support. This is host-initiated I/O, not
11299 target-initiated; for target-initiated, see remote-fileio.c. */
11300
11301/* If *LEFT is at least the length of STRING, copy STRING to
11302 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11303 decrease *LEFT. Otherwise raise an error. */
11304
11305static void
a121b7c1 11306remote_buffer_add_string (char **buffer, int *left, const char *string)
a6b151f1
DJ
11307{
11308 int len = strlen (string);
11309
11310 if (len > *left)
11311 error (_("Packet too long for target."));
11312
11313 memcpy (*buffer, string, len);
11314 *buffer += len;
11315 *left -= len;
11316
11317 /* NUL-terminate the buffer as a convenience, if there is
11318 room. */
11319 if (*left)
11320 **buffer = '\0';
11321}
11322
11323/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
11324 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11325 decrease *LEFT. Otherwise raise an error. */
11326
11327static void
11328remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
11329 int len)
11330{
11331 if (2 * len > *left)
11332 error (_("Packet too long for target."));
11333
11334 bin2hex (bytes, *buffer, len);
11335 *buffer += 2 * len;
11336 *left -= 2 * len;
11337
11338 /* NUL-terminate the buffer as a convenience, if there is
11339 room. */
11340 if (*left)
11341 **buffer = '\0';
11342}
11343
11344/* If *LEFT is large enough, convert VALUE to hex and add it to
11345 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11346 decrease *LEFT. Otherwise raise an error. */
11347
11348static void
11349remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
11350{
11351 int len = hexnumlen (value);
11352
11353 if (len > *left)
11354 error (_("Packet too long for target."));
11355
11356 hexnumstr (*buffer, value);
11357 *buffer += len;
11358 *left -= len;
11359
11360 /* NUL-terminate the buffer as a convenience, if there is
11361 room. */
11362 if (*left)
11363 **buffer = '\0';
11364}
11365
11366/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
11367 value, *REMOTE_ERRNO to the remote error number or zero if none
11368 was included, and *ATTACHMENT to point to the start of the annex
11369 if any. The length of the packet isn't needed here; there may
11370 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
11371
11372 Return 0 if the packet could be parsed, -1 if it could not. If
11373 -1 is returned, the other variables may not be initialized. */
11374
11375static int
11376remote_hostio_parse_result (char *buffer, int *retcode,
11377 int *remote_errno, char **attachment)
11378{
11379 char *p, *p2;
11380
11381 *remote_errno = 0;
11382 *attachment = NULL;
11383
11384 if (buffer[0] != 'F')
11385 return -1;
11386
11387 errno = 0;
11388 *retcode = strtol (&buffer[1], &p, 16);
11389 if (errno != 0 || p == &buffer[1])
11390 return -1;
11391
11392 /* Check for ",errno". */
11393 if (*p == ',')
11394 {
11395 errno = 0;
11396 *remote_errno = strtol (p + 1, &p2, 16);
11397 if (errno != 0 || p + 1 == p2)
11398 return -1;
11399 p = p2;
11400 }
11401
11402 /* Check for ";attachment". If there is no attachment, the
11403 packet should end here. */
11404 if (*p == ';')
11405 {
11406 *attachment = p + 1;
11407 return 0;
11408 }
11409 else if (*p == '\0')
11410 return 0;
11411 else
11412 return -1;
11413}
11414
11415/* Send a prepared I/O packet to the target and read its response.
11416 The prepared packet is in the global RS->BUF before this function
11417 is called, and the answer is there when we return.
11418
11419 COMMAND_BYTES is the length of the request to send, which may include
11420 binary data. WHICH_PACKET is the packet configuration to check
11421 before attempting a packet. If an error occurs, *REMOTE_ERRNO
11422 is set to the error number and -1 is returned. Otherwise the value
11423 returned by the function is returned.
11424
11425 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
11426 attachment is expected; an error will be reported if there's a
11427 mismatch. If one is found, *ATTACHMENT will be set to point into
11428 the packet buffer and *ATTACHMENT_LEN will be set to the
11429 attachment's length. */
11430
11431static int
11432remote_hostio_send_command (int command_bytes, int which_packet,
11433 int *remote_errno, char **attachment,
11434 int *attachment_len)
11435{
11436 struct remote_state *rs = get_remote_state ();
11437 int ret, bytes_read;
11438 char *attachment_tmp;
11439
5d93a237 11440 if (!rs->remote_desc
4082afcc 11441 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
11442 {
11443 *remote_errno = FILEIO_ENOSYS;
11444 return -1;
11445 }
11446
11447 putpkt_binary (rs->buf, command_bytes);
11448 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
11449
11450 /* If it timed out, something is wrong. Don't try to parse the
11451 buffer. */
11452 if (bytes_read < 0)
11453 {
11454 *remote_errno = FILEIO_EINVAL;
11455 return -1;
11456 }
11457
11458 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
11459 {
11460 case PACKET_ERROR:
11461 *remote_errno = FILEIO_EINVAL;
11462 return -1;
11463 case PACKET_UNKNOWN:
11464 *remote_errno = FILEIO_ENOSYS;
11465 return -1;
11466 case PACKET_OK:
11467 break;
11468 }
11469
11470 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
11471 &attachment_tmp))
11472 {
11473 *remote_errno = FILEIO_EINVAL;
11474 return -1;
11475 }
11476
11477 /* Make sure we saw an attachment if and only if we expected one. */
11478 if ((attachment_tmp == NULL && attachment != NULL)
11479 || (attachment_tmp != NULL && attachment == NULL))
11480 {
11481 *remote_errno = FILEIO_EINVAL;
11482 return -1;
11483 }
11484
11485 /* If an attachment was found, it must point into the packet buffer;
11486 work out how many bytes there were. */
11487 if (attachment_tmp != NULL)
11488 {
11489 *attachment = attachment_tmp;
11490 *attachment_len = bytes_read - (*attachment - rs->buf);
11491 }
11492
11493 return ret;
11494}
11495
80152258
PA
11496/* Invalidate the readahead cache. */
11497
11498static void
11499readahead_cache_invalidate (void)
11500{
11501 struct remote_state *rs = get_remote_state ();
11502
11503 rs->readahead_cache.fd = -1;
11504}
11505
11506/* Invalidate the readahead cache if it is holding data for FD. */
11507
11508static void
11509readahead_cache_invalidate_fd (int fd)
11510{
11511 struct remote_state *rs = get_remote_state ();
11512
11513 if (rs->readahead_cache.fd == fd)
11514 rs->readahead_cache.fd = -1;
11515}
11516
15a201c8
GB
11517/* Set the filesystem remote_hostio functions that take FILENAME
11518 arguments will use. Return 0 on success, or -1 if an error
11519 occurs (and set *REMOTE_ERRNO). */
11520
11521static int
11522remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
11523{
11524 struct remote_state *rs = get_remote_state ();
11525 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
11526 char *p = rs->buf;
11527 int left = get_remote_packet_size () - 1;
11528 char arg[9];
11529 int ret;
11530
11531 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11532 return 0;
11533
11534 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
11535 return 0;
11536
11537 remote_buffer_add_string (&p, &left, "vFile:setfs:");
11538
11539 xsnprintf (arg, sizeof (arg), "%x", required_pid);
11540 remote_buffer_add_string (&p, &left, arg);
11541
11542 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
11543 remote_errno, NULL, NULL);
11544
11545 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11546 return 0;
11547
11548 if (ret == 0)
11549 rs->fs_pid = required_pid;
11550
11551 return ret;
11552}
11553
12e2a5fd 11554/* Implementation of to_fileio_open. */
a6b151f1
DJ
11555
11556static int
cd897586 11557remote_hostio_open (struct target_ops *self,
07c138c8 11558 struct inferior *inf, const char *filename,
4313b8c0
GB
11559 int flags, int mode, int warn_if_slow,
11560 int *remote_errno)
a6b151f1
DJ
11561{
11562 struct remote_state *rs = get_remote_state ();
11563 char *p = rs->buf;
11564 int left = get_remote_packet_size () - 1;
11565
4313b8c0
GB
11566 if (warn_if_slow)
11567 {
11568 static int warning_issued = 0;
11569
11570 printf_unfiltered (_("Reading %s from remote target...\n"),
11571 filename);
11572
11573 if (!warning_issued)
11574 {
11575 warning (_("File transfers from remote targets can be slow."
11576 " Use \"set sysroot\" to access files locally"
11577 " instead."));
11578 warning_issued = 1;
11579 }
11580 }
11581
15a201c8
GB
11582 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11583 return -1;
11584
a6b151f1
DJ
11585 remote_buffer_add_string (&p, &left, "vFile:open:");
11586
11587 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11588 strlen (filename));
11589 remote_buffer_add_string (&p, &left, ",");
11590
11591 remote_buffer_add_int (&p, &left, flags);
11592 remote_buffer_add_string (&p, &left, ",");
11593
11594 remote_buffer_add_int (&p, &left, mode);
11595
11596 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
11597 remote_errno, NULL, NULL);
11598}
11599
12e2a5fd 11600/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
11601
11602static int
0d866f62
TT
11603remote_hostio_pwrite (struct target_ops *self,
11604 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
11605 ULONGEST offset, int *remote_errno)
11606{
11607 struct remote_state *rs = get_remote_state ();
11608 char *p = rs->buf;
11609 int left = get_remote_packet_size ();
11610 int out_len;
11611
80152258
PA
11612 readahead_cache_invalidate_fd (fd);
11613
a6b151f1
DJ
11614 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
11615
11616 remote_buffer_add_int (&p, &left, fd);
11617 remote_buffer_add_string (&p, &left, ",");
11618
11619 remote_buffer_add_int (&p, &left, offset);
11620 remote_buffer_add_string (&p, &left, ",");
11621
124e13d9 11622 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
11623 get_remote_packet_size () - (p - rs->buf));
11624
11625 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
11626 remote_errno, NULL, NULL);
11627}
11628
80152258
PA
11629/* Helper for the implementation of to_fileio_pread. Read the file
11630 from the remote side with vFile:pread. */
a6b151f1
DJ
11631
11632static int
80152258
PA
11633remote_hostio_pread_vFile (struct target_ops *self,
11634 int fd, gdb_byte *read_buf, int len,
11635 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
11636{
11637 struct remote_state *rs = get_remote_state ();
11638 char *p = rs->buf;
11639 char *attachment;
11640 int left = get_remote_packet_size ();
11641 int ret, attachment_len;
11642 int read_len;
11643
11644 remote_buffer_add_string (&p, &left, "vFile:pread:");
11645
11646 remote_buffer_add_int (&p, &left, fd);
11647 remote_buffer_add_string (&p, &left, ",");
11648
11649 remote_buffer_add_int (&p, &left, len);
11650 remote_buffer_add_string (&p, &left, ",");
11651
11652 remote_buffer_add_int (&p, &left, offset);
11653
11654 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
11655 remote_errno, &attachment,
11656 &attachment_len);
11657
11658 if (ret < 0)
11659 return ret;
11660
bc20a4af 11661 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
11662 read_buf, len);
11663 if (read_len != ret)
11664 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
11665
11666 return ret;
11667}
11668
80152258
PA
11669/* Serve pread from the readahead cache. Returns number of bytes
11670 read, or 0 if the request can't be served from the cache. */
11671
11672static int
11673remote_hostio_pread_from_cache (struct remote_state *rs,
11674 int fd, gdb_byte *read_buf, size_t len,
11675 ULONGEST offset)
11676{
11677 struct readahead_cache *cache = &rs->readahead_cache;
11678
11679 if (cache->fd == fd
11680 && cache->offset <= offset
11681 && offset < cache->offset + cache->bufsize)
11682 {
11683 ULONGEST max = cache->offset + cache->bufsize;
11684
11685 if (offset + len > max)
11686 len = max - offset;
11687
11688 memcpy (read_buf, cache->buf + offset - cache->offset, len);
11689 return len;
11690 }
11691
11692 return 0;
11693}
11694
11695/* Implementation of to_fileio_pread. */
11696
11697static int
11698remote_hostio_pread (struct target_ops *self,
11699 int fd, gdb_byte *read_buf, int len,
11700 ULONGEST offset, int *remote_errno)
11701{
11702 int ret;
11703 struct remote_state *rs = get_remote_state ();
11704 struct readahead_cache *cache = &rs->readahead_cache;
11705
11706 ret = remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11707 if (ret > 0)
11708 {
11709 cache->hit_count++;
11710
11711 if (remote_debug)
11712 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
11713 pulongest (cache->hit_count));
11714 return ret;
11715 }
11716
11717 cache->miss_count++;
11718 if (remote_debug)
11719 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
11720 pulongest (cache->miss_count));
11721
11722 cache->fd = fd;
11723 cache->offset = offset;
11724 cache->bufsize = get_remote_packet_size ();
224c3ddb 11725 cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
80152258
PA
11726
11727 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
11728 cache->offset, remote_errno);
11729 if (ret <= 0)
11730 {
11731 readahead_cache_invalidate_fd (fd);
11732 return ret;
11733 }
11734
11735 cache->bufsize = ret;
11736 return remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11737}
11738
12e2a5fd 11739/* Implementation of to_fileio_close. */
a6b151f1
DJ
11740
11741static int
df39ea25 11742remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
11743{
11744 struct remote_state *rs = get_remote_state ();
11745 char *p = rs->buf;
11746 int left = get_remote_packet_size () - 1;
11747
80152258
PA
11748 readahead_cache_invalidate_fd (fd);
11749
a6b151f1
DJ
11750 remote_buffer_add_string (&p, &left, "vFile:close:");
11751
11752 remote_buffer_add_int (&p, &left, fd);
11753
11754 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
11755 remote_errno, NULL, NULL);
11756}
11757
12e2a5fd 11758/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
11759
11760static int
dbbca37d 11761remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
11762 struct inferior *inf, const char *filename,
11763 int *remote_errno)
a6b151f1
DJ
11764{
11765 struct remote_state *rs = get_remote_state ();
11766 char *p = rs->buf;
11767 int left = get_remote_packet_size () - 1;
11768
15a201c8
GB
11769 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11770 return -1;
11771
a6b151f1
DJ
11772 remote_buffer_add_string (&p, &left, "vFile:unlink:");
11773
11774 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11775 strlen (filename));
11776
11777 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
11778 remote_errno, NULL, NULL);
11779}
11780
12e2a5fd 11781/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
11782
11783static char *
fab5aa7c 11784remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
11785 struct inferior *inf, const char *filename,
11786 int *remote_errno)
b9e7b9c3
UW
11787{
11788 struct remote_state *rs = get_remote_state ();
11789 char *p = rs->buf;
11790 char *attachment;
11791 int left = get_remote_packet_size ();
11792 int len, attachment_len;
11793 int read_len;
11794 char *ret;
11795
15a201c8
GB
11796 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11797 return NULL;
11798
b9e7b9c3
UW
11799 remote_buffer_add_string (&p, &left, "vFile:readlink:");
11800
11801 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11802 strlen (filename));
11803
11804 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
11805 remote_errno, &attachment,
11806 &attachment_len);
11807
11808 if (len < 0)
11809 return NULL;
11810
224c3ddb 11811 ret = (char *) xmalloc (len + 1);
b9e7b9c3 11812
bc20a4af
PA
11813 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11814 (gdb_byte *) ret, len);
b9e7b9c3
UW
11815 if (read_len != len)
11816 error (_("Readlink returned %d, but %d bytes."), len, read_len);
11817
11818 ret[len] = '\0';
11819 return ret;
11820}
11821
12e2a5fd 11822/* Implementation of to_fileio_fstat. */
0a93529c
GB
11823
11824static int
11825remote_hostio_fstat (struct target_ops *self,
11826 int fd, struct stat *st,
11827 int *remote_errno)
11828{
11829 struct remote_state *rs = get_remote_state ();
11830 char *p = rs->buf;
11831 int left = get_remote_packet_size ();
11832 int attachment_len, ret;
11833 char *attachment;
11834 struct fio_stat fst;
11835 int read_len;
11836
464b0089
GB
11837 remote_buffer_add_string (&p, &left, "vFile:fstat:");
11838
11839 remote_buffer_add_int (&p, &left, fd);
11840
11841 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
11842 remote_errno, &attachment,
11843 &attachment_len);
11844 if (ret < 0)
0a93529c 11845 {
464b0089
GB
11846 if (*remote_errno != FILEIO_ENOSYS)
11847 return ret;
11848
0a93529c
GB
11849 /* Strictly we should return -1, ENOSYS here, but when
11850 "set sysroot remote:" was implemented in August 2008
11851 BFD's need for a stat function was sidestepped with
11852 this hack. This was not remedied until March 2015
11853 so we retain the previous behavior to avoid breaking
11854 compatibility.
11855
11856 Note that the memset is a March 2015 addition; older
11857 GDBs set st_size *and nothing else* so the structure
11858 would have garbage in all other fields. This might
11859 break something but retaining the previous behavior
11860 here would be just too wrong. */
11861
11862 memset (st, 0, sizeof (struct stat));
11863 st->st_size = INT_MAX;
11864 return 0;
11865 }
11866
0a93529c
GB
11867 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11868 (gdb_byte *) &fst, sizeof (fst));
11869
11870 if (read_len != ret)
11871 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
11872
11873 if (read_len != sizeof (fst))
11874 error (_("vFile:fstat returned %d bytes, but expecting %d."),
11875 read_len, (int) sizeof (fst));
11876
11877 remote_fileio_to_host_stat (&fst, st);
11878
11879 return 0;
11880}
11881
12e2a5fd 11882/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
11883
11884static int
11885remote_filesystem_is_local (struct target_ops *self)
11886{
11887 /* Valgrind GDB presents itself as a remote target but works
11888 on the local filesystem: it does not implement remote get
11889 and users are not expected to set a sysroot. To handle
11890 this case we treat the remote filesystem as local if the
11891 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
11892 does not support vFile:open. */
a3be80c3 11893 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
11894 {
11895 enum packet_support ps = packet_support (PACKET_vFile_open);
11896
11897 if (ps == PACKET_SUPPORT_UNKNOWN)
11898 {
11899 int fd, remote_errno;
11900
11901 /* Try opening a file to probe support. The supplied
11902 filename is irrelevant, we only care about whether
11903 the stub recognizes the packet or not. */
07c138c8 11904 fd = remote_hostio_open (self, NULL, "just probing",
4313b8c0 11905 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
11906 &remote_errno);
11907
11908 if (fd >= 0)
11909 remote_hostio_close (self, fd, &remote_errno);
11910
11911 ps = packet_support (PACKET_vFile_open);
11912 }
11913
11914 if (ps == PACKET_DISABLE)
11915 {
11916 static int warning_issued = 0;
11917
11918 if (!warning_issued)
11919 {
11920 warning (_("remote target does not support file"
11921 " transfer, attempting to access files"
11922 " from local filesystem."));
11923 warning_issued = 1;
11924 }
11925
11926 return 1;
11927 }
11928 }
11929
11930 return 0;
11931}
11932
a6b151f1
DJ
11933static int
11934remote_fileio_errno_to_host (int errnum)
11935{
11936 switch (errnum)
11937 {
11938 case FILEIO_EPERM:
11939 return EPERM;
11940 case FILEIO_ENOENT:
11941 return ENOENT;
11942 case FILEIO_EINTR:
11943 return EINTR;
11944 case FILEIO_EIO:
11945 return EIO;
11946 case FILEIO_EBADF:
11947 return EBADF;
11948 case FILEIO_EACCES:
11949 return EACCES;
11950 case FILEIO_EFAULT:
11951 return EFAULT;
11952 case FILEIO_EBUSY:
11953 return EBUSY;
11954 case FILEIO_EEXIST:
11955 return EEXIST;
11956 case FILEIO_ENODEV:
11957 return ENODEV;
11958 case FILEIO_ENOTDIR:
11959 return ENOTDIR;
11960 case FILEIO_EISDIR:
11961 return EISDIR;
11962 case FILEIO_EINVAL:
11963 return EINVAL;
11964 case FILEIO_ENFILE:
11965 return ENFILE;
11966 case FILEIO_EMFILE:
11967 return EMFILE;
11968 case FILEIO_EFBIG:
11969 return EFBIG;
11970 case FILEIO_ENOSPC:
11971 return ENOSPC;
11972 case FILEIO_ESPIPE:
11973 return ESPIPE;
11974 case FILEIO_EROFS:
11975 return EROFS;
11976 case FILEIO_ENOSYS:
11977 return ENOSYS;
11978 case FILEIO_ENAMETOOLONG:
11979 return ENAMETOOLONG;
11980 }
11981 return -1;
11982}
11983
11984static char *
11985remote_hostio_error (int errnum)
11986{
11987 int host_error = remote_fileio_errno_to_host (errnum);
11988
11989 if (host_error == -1)
11990 error (_("Unknown remote I/O error %d"), errnum);
11991 else
11992 error (_("Remote I/O error: %s"), safe_strerror (host_error));
11993}
11994
a6b151f1
DJ
11995static void
11996remote_hostio_close_cleanup (void *opaque)
11997{
11998 int fd = *(int *) opaque;
11999 int remote_errno;
12000
df39ea25 12001 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
12002}
12003
12004void
12005remote_file_put (const char *local_file, const char *remote_file, int from_tty)
12006{
12007 struct cleanup *back_to, *close_cleanup;
12008 int retcode, fd, remote_errno, bytes, io_size;
a6b151f1
DJ
12009 gdb_byte *buffer;
12010 int bytes_in_buffer;
12011 int saw_eof;
12012 ULONGEST offset;
5d93a237 12013 struct remote_state *rs = get_remote_state ();
a6b151f1 12014
5d93a237 12015 if (!rs->remote_desc)
a6b151f1
DJ
12016 error (_("command can only be used with remote target"));
12017
d419f42d 12018 gdb_file_up file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
12019 if (file == NULL)
12020 perror_with_name (local_file);
a6b151f1 12021
07c138c8 12022 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 12023 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1 12024 | FILEIO_O_TRUNC),
4313b8c0 12025 0700, 0, &remote_errno);
a6b151f1
DJ
12026 if (fd == -1)
12027 remote_hostio_error (remote_errno);
12028
12029 /* Send up to this many bytes at once. They won't all fit in the
12030 remote packet limit, so we'll transfer slightly fewer. */
12031 io_size = get_remote_packet_size ();
224c3ddb 12032 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12033 back_to = make_cleanup (xfree, buffer);
a6b151f1
DJ
12034
12035 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
12036
12037 bytes_in_buffer = 0;
12038 saw_eof = 0;
12039 offset = 0;
12040 while (bytes_in_buffer || !saw_eof)
12041 {
12042 if (!saw_eof)
12043 {
3e43a32a
MS
12044 bytes = fread (buffer + bytes_in_buffer, 1,
12045 io_size - bytes_in_buffer,
d419f42d 12046 file.get ());
a6b151f1
DJ
12047 if (bytes == 0)
12048 {
d419f42d 12049 if (ferror (file.get ()))
a6b151f1
DJ
12050 error (_("Error reading %s."), local_file);
12051 else
12052 {
12053 /* EOF. Unless there is something still in the
12054 buffer from the last iteration, we are done. */
12055 saw_eof = 1;
12056 if (bytes_in_buffer == 0)
12057 break;
12058 }
12059 }
12060 }
12061 else
12062 bytes = 0;
12063
12064 bytes += bytes_in_buffer;
12065 bytes_in_buffer = 0;
12066
0d866f62
TT
12067 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
12068 fd, buffer, bytes,
3e43a32a 12069 offset, &remote_errno);
a6b151f1
DJ
12070
12071 if (retcode < 0)
12072 remote_hostio_error (remote_errno);
12073 else if (retcode == 0)
12074 error (_("Remote write of %d bytes returned 0!"), bytes);
12075 else if (retcode < bytes)
12076 {
12077 /* Short write. Save the rest of the read data for the next
12078 write. */
12079 bytes_in_buffer = bytes - retcode;
12080 memmove (buffer, buffer + retcode, bytes_in_buffer);
12081 }
12082
12083 offset += retcode;
12084 }
12085
12086 discard_cleanups (close_cleanup);
df39ea25 12087 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
12088 remote_hostio_error (remote_errno);
12089
12090 if (from_tty)
12091 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
12092 do_cleanups (back_to);
12093}
12094
12095void
12096remote_file_get (const char *remote_file, const char *local_file, int from_tty)
12097{
12098 struct cleanup *back_to, *close_cleanup;
cea39f65 12099 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
12100 gdb_byte *buffer;
12101 ULONGEST offset;
5d93a237 12102 struct remote_state *rs = get_remote_state ();
a6b151f1 12103
5d93a237 12104 if (!rs->remote_desc)
a6b151f1
DJ
12105 error (_("command can only be used with remote target"));
12106
07c138c8 12107 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
4313b8c0
GB
12108 remote_file, FILEIO_O_RDONLY, 0, 0,
12109 &remote_errno);
a6b151f1
DJ
12110 if (fd == -1)
12111 remote_hostio_error (remote_errno);
12112
d419f42d 12113 gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
12114 if (file == NULL)
12115 perror_with_name (local_file);
a6b151f1
DJ
12116
12117 /* Send up to this many bytes at once. They won't all fit in the
12118 remote packet limit, so we'll transfer slightly fewer. */
12119 io_size = get_remote_packet_size ();
224c3ddb 12120 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12121 back_to = make_cleanup (xfree, buffer);
a6b151f1
DJ
12122
12123 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
12124
12125 offset = 0;
12126 while (1)
12127 {
a3be983c
TT
12128 bytes = remote_hostio_pread (find_target_at (process_stratum),
12129 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
12130 if (bytes == 0)
12131 /* Success, but no bytes, means end-of-file. */
12132 break;
12133 if (bytes == -1)
12134 remote_hostio_error (remote_errno);
12135
12136 offset += bytes;
12137
d419f42d 12138 bytes = fwrite (buffer, 1, bytes, file.get ());
a6b151f1
DJ
12139 if (bytes == 0)
12140 perror_with_name (local_file);
12141 }
12142
12143 discard_cleanups (close_cleanup);
df39ea25 12144 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
12145 remote_hostio_error (remote_errno);
12146
12147 if (from_tty)
12148 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
12149 do_cleanups (back_to);
12150}
12151
12152void
12153remote_file_delete (const char *remote_file, int from_tty)
12154{
12155 int retcode, remote_errno;
5d93a237 12156 struct remote_state *rs = get_remote_state ();
a6b151f1 12157
5d93a237 12158 if (!rs->remote_desc)
a6b151f1
DJ
12159 error (_("command can only be used with remote target"));
12160
dbbca37d 12161 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 12162 NULL, remote_file, &remote_errno);
a6b151f1
DJ
12163 if (retcode == -1)
12164 remote_hostio_error (remote_errno);
12165
12166 if (from_tty)
12167 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
12168}
12169
12170static void
ac88e2de 12171remote_put_command (const char *args, int from_tty)
a6b151f1 12172{
d1a41061
PP
12173 if (args == NULL)
12174 error_no_arg (_("file to put"));
12175
773a1edc 12176 gdb_argv argv (args);
a6b151f1
DJ
12177 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12178 error (_("Invalid parameters to remote put"));
12179
12180 remote_file_put (argv[0], argv[1], from_tty);
a6b151f1
DJ
12181}
12182
12183static void
ac88e2de 12184remote_get_command (const char *args, int from_tty)
a6b151f1 12185{
d1a41061
PP
12186 if (args == NULL)
12187 error_no_arg (_("file to get"));
12188
773a1edc 12189 gdb_argv argv (args);
a6b151f1
DJ
12190 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12191 error (_("Invalid parameters to remote get"));
12192
12193 remote_file_get (argv[0], argv[1], from_tty);
a6b151f1
DJ
12194}
12195
12196static void
ac88e2de 12197remote_delete_command (const char *args, int from_tty)
a6b151f1 12198{
d1a41061
PP
12199 if (args == NULL)
12200 error_no_arg (_("file to delete"));
12201
773a1edc 12202 gdb_argv argv (args);
a6b151f1
DJ
12203 if (argv[0] == NULL || argv[1] != NULL)
12204 error (_("Invalid parameters to remote delete"));
12205
12206 remote_file_delete (argv[0], from_tty);
a6b151f1
DJ
12207}
12208
12209static void
12210remote_command (char *args, int from_tty)
12211{
635c7e8a 12212 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
12213}
12214
b2175913 12215static int
19db3e69 12216remote_can_execute_reverse (struct target_ops *self)
b2175913 12217{
4082afcc
PA
12218 if (packet_support (PACKET_bs) == PACKET_ENABLE
12219 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
12220 return 1;
12221 else
12222 return 0;
b2175913
MS
12223}
12224
74531fed 12225static int
2a9a2795 12226remote_supports_non_stop (struct target_ops *self)
74531fed
PA
12227{
12228 return 1;
12229}
12230
03583c20 12231static int
2bfc0540 12232remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
12233{
12234 /* Only supported in extended mode. */
12235 return 0;
12236}
12237
8a305172 12238static int
86ce2668 12239remote_supports_multi_process (struct target_ops *self)
8a305172
PA
12240{
12241 struct remote_state *rs = get_remote_state ();
a744cf53 12242
8020350c 12243 return remote_multi_process_p (rs);
8a305172
PA
12244}
12245
70221824 12246static int
782b2b07
SS
12247remote_supports_cond_tracepoints (void)
12248{
4082afcc 12249 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
12250}
12251
3788aec7 12252static int
efcc2da7 12253remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 12254{
4082afcc 12255 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
12256}
12257
70221824 12258static int
7a697b8d
SS
12259remote_supports_fast_tracepoints (void)
12260{
4082afcc 12261 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
12262}
12263
0fb4aa4b
PA
12264static int
12265remote_supports_static_tracepoints (void)
12266{
4082afcc 12267 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
12268}
12269
1e4d1764
YQ
12270static int
12271remote_supports_install_in_trace (void)
12272{
4082afcc 12273 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
12274}
12275
d248b706 12276static int
7d178d6a 12277remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 12278{
4082afcc
PA
12279 return (packet_support (PACKET_EnableDisableTracepoints_feature)
12280 == PACKET_ENABLE);
d248b706
KY
12281}
12282
3065dfb6 12283static int
6de37a3a 12284remote_supports_string_tracing (struct target_ops *self)
3065dfb6 12285{
4082afcc 12286 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
12287}
12288
d3ce09f5 12289static int
78eff0ec 12290remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 12291{
4082afcc 12292 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
12293}
12294
35b1e5cc 12295static void
ecae04e1 12296remote_trace_init (struct target_ops *self)
35b1e5cc 12297{
b6bb3468
PA
12298 struct remote_state *rs = get_remote_state ();
12299
35b1e5cc 12300 putpkt ("QTinit");
b6bb3468
PA
12301 remote_get_noisy_reply ();
12302 if (strcmp (rs->buf, "OK") != 0)
35b1e5cc
SS
12303 error (_("Target does not support this command."));
12304}
12305
12306static void free_actions_list (char **actions_list);
12307static void free_actions_list_cleanup_wrapper (void *);
12308static void
12309free_actions_list_cleanup_wrapper (void *al)
12310{
19ba03f4 12311 free_actions_list ((char **) al);
35b1e5cc
SS
12312}
12313
12314static void
12315free_actions_list (char **actions_list)
12316{
12317 int ndx;
12318
12319 if (actions_list == 0)
12320 return;
12321
12322 for (ndx = 0; actions_list[ndx]; ndx++)
12323 xfree (actions_list[ndx]);
12324
12325 xfree (actions_list);
12326}
12327
409873ef
SS
12328/* Recursive routine to walk through command list including loops, and
12329 download packets for each command. */
12330
12331static void
12332remote_download_command_source (int num, ULONGEST addr,
12333 struct command_line *cmds)
12334{
12335 struct remote_state *rs = get_remote_state ();
12336 struct command_line *cmd;
12337
12338 for (cmd = cmds; cmd; cmd = cmd->next)
12339 {
0df8b418 12340 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12341 strcpy (rs->buf, "QTDPsrc:");
12342 encode_source_string (num, addr, "cmd", cmd->line,
12343 rs->buf + strlen (rs->buf),
12344 rs->buf_size - strlen (rs->buf));
12345 putpkt (rs->buf);
b6bb3468
PA
12346 remote_get_noisy_reply ();
12347 if (strcmp (rs->buf, "OK"))
409873ef
SS
12348 warning (_("Target does not support source download."));
12349
12350 if (cmd->control_type == while_control
12351 || cmd->control_type == while_stepping_control)
12352 {
12353 remote_download_command_source (num, addr, *cmd->body_list);
12354
0df8b418 12355 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12356 strcpy (rs->buf, "QTDPsrc:");
12357 encode_source_string (num, addr, "cmd", "end",
12358 rs->buf + strlen (rs->buf),
12359 rs->buf_size - strlen (rs->buf));
12360 putpkt (rs->buf);
b6bb3468
PA
12361 remote_get_noisy_reply ();
12362 if (strcmp (rs->buf, "OK"))
409873ef
SS
12363 warning (_("Target does not support source download."));
12364 }
12365 }
12366}
12367
35b1e5cc 12368static void
548f7808 12369remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 12370{
bba74b36 12371#define BUF_SIZE 2048
e8ba3115 12372
35b1e5cc 12373 CORE_ADDR tpaddr;
409873ef 12374 char addrbuf[40];
bba74b36 12375 char buf[BUF_SIZE];
35b1e5cc
SS
12376 char **tdp_actions;
12377 char **stepping_actions;
12378 int ndx;
12379 struct cleanup *old_chain = NULL;
35b1e5cc 12380 char *pkt;
e8ba3115 12381 struct breakpoint *b = loc->owner;
d9b3f62e 12382 struct tracepoint *t = (struct tracepoint *) b;
b6bb3468 12383 struct remote_state *rs = get_remote_state ();
35b1e5cc 12384
dc673c81 12385 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
12386 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
12387 tdp_actions);
12388 (void) make_cleanup (free_actions_list_cleanup_wrapper,
12389 stepping_actions);
12390
12391 tpaddr = loc->address;
12392 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
12393 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
12394 addrbuf, /* address */
12395 (b->enable_state == bp_enabled ? 'E' : 'D'),
12396 t->step_count, t->pass_count);
e8ba3115
YQ
12397 /* Fast tracepoints are mostly handled by the target, but we can
12398 tell the target how big of an instruction block should be moved
12399 around. */
12400 if (b->type == bp_fast_tracepoint)
12401 {
12402 /* Only test for support at download time; we may not know
12403 target capabilities at definition time. */
12404 if (remote_supports_fast_tracepoints ())
35b1e5cc 12405 {
6b940e6a
PL
12406 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
12407 NULL))
bba74b36 12408 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 12409 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 12410 else
e8ba3115
YQ
12411 /* If it passed validation at definition but fails now,
12412 something is very wrong. */
12413 internal_error (__FILE__, __LINE__,
12414 _("Fast tracepoint not "
12415 "valid during download"));
35b1e5cc 12416 }
e8ba3115
YQ
12417 else
12418 /* Fast tracepoints are functionally identical to regular
12419 tracepoints, so don't take lack of support as a reason to
12420 give up on the trace run. */
12421 warning (_("Target does not support fast tracepoints, "
12422 "downloading %d as regular tracepoint"), b->number);
12423 }
12424 else if (b->type == bp_static_tracepoint)
12425 {
12426 /* Only test for support at download time; we may not know
12427 target capabilities at definition time. */
12428 if (remote_supports_static_tracepoints ())
0fb4aa4b 12429 {
e8ba3115 12430 struct static_tracepoint_marker marker;
0fb4aa4b 12431
e8ba3115
YQ
12432 if (target_static_tracepoint_marker_at (tpaddr, &marker))
12433 strcat (buf, ":S");
0fb4aa4b 12434 else
e8ba3115 12435 error (_("Static tracepoint not valid during download"));
0fb4aa4b 12436 }
e8ba3115
YQ
12437 else
12438 /* Fast tracepoints are functionally identical to regular
12439 tracepoints, so don't take lack of support as a reason
12440 to give up on the trace run. */
12441 error (_("Target does not support static tracepoints"));
12442 }
12443 /* If the tracepoint has a conditional, make it into an agent
12444 expression and append to the definition. */
12445 if (loc->cond)
12446 {
12447 /* Only test support at download time, we may not know target
12448 capabilities at definition time. */
12449 if (remote_supports_cond_tracepoints ())
35b1e5cc 12450 {
833177a4 12451 agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
bba74b36
YQ
12452 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
12453 aexpr->len);
e8ba3115
YQ
12454 pkt = buf + strlen (buf);
12455 for (ndx = 0; ndx < aexpr->len; ++ndx)
12456 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
12457 *pkt = '\0';
35b1e5cc 12458 }
e8ba3115
YQ
12459 else
12460 warning (_("Target does not support conditional tracepoints, "
12461 "ignoring tp %d cond"), b->number);
12462 }
35b1e5cc 12463
d9b3f62e 12464 if (b->commands || *default_collect)
e8ba3115
YQ
12465 strcat (buf, "-");
12466 putpkt (buf);
b6bb3468
PA
12467 remote_get_noisy_reply ();
12468 if (strcmp (rs->buf, "OK"))
e8ba3115 12469 error (_("Target does not support tracepoints."));
35b1e5cc 12470
e8ba3115
YQ
12471 /* do_single_steps (t); */
12472 if (tdp_actions)
12473 {
12474 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 12475 {
e8ba3115 12476 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
12477 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
12478 b->number, addrbuf, /* address */
12479 tdp_actions[ndx],
12480 ((tdp_actions[ndx + 1] || stepping_actions)
12481 ? '-' : 0));
e8ba3115 12482 putpkt (buf);
b6bb3468
PA
12483 remote_get_noisy_reply ();
12484 if (strcmp (rs->buf, "OK"))
e8ba3115 12485 error (_("Error on target while setting tracepoints."));
35b1e5cc 12486 }
e8ba3115
YQ
12487 }
12488 if (stepping_actions)
12489 {
12490 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 12491 {
e8ba3115 12492 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
12493 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
12494 b->number, addrbuf, /* address */
12495 ((ndx == 0) ? "S" : ""),
12496 stepping_actions[ndx],
12497 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115 12498 putpkt (buf);
b6bb3468
PA
12499 remote_get_noisy_reply ();
12500 if (strcmp (rs->buf, "OK"))
e8ba3115 12501 error (_("Error on target while setting tracepoints."));
35b1e5cc 12502 }
e8ba3115 12503 }
409873ef 12504
4082afcc 12505 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 12506 {
f00aae0f 12507 if (b->location != NULL)
409873ef 12508 {
e8ba3115 12509 strcpy (buf, "QTDPsrc:");
f00aae0f 12510 encode_source_string (b->number, loc->address, "at",
d28cd78a 12511 event_location_to_string (b->location.get ()),
f00aae0f 12512 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115 12513 putpkt (buf);
b6bb3468
PA
12514 remote_get_noisy_reply ();
12515 if (strcmp (rs->buf, "OK"))
e8ba3115 12516 warning (_("Target does not support source download."));
409873ef 12517 }
e8ba3115
YQ
12518 if (b->cond_string)
12519 {
12520 strcpy (buf, "QTDPsrc:");
12521 encode_source_string (b->number, loc->address,
12522 "cond", b->cond_string, buf + strlen (buf),
12523 2048 - strlen (buf));
12524 putpkt (buf);
b6bb3468
PA
12525 remote_get_noisy_reply ();
12526 if (strcmp (rs->buf, "OK"))
e8ba3115
YQ
12527 warning (_("Target does not support source download."));
12528 }
12529 remote_download_command_source (b->number, loc->address,
12530 breakpoint_commands (b));
35b1e5cc 12531 }
e8ba3115
YQ
12532
12533 do_cleanups (old_chain);
35b1e5cc
SS
12534}
12535
1e4d1764 12536static int
a52a8357 12537remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 12538{
1e51243a
PA
12539 struct remote_state *rs = get_remote_state ();
12540 struct trace_status *ts;
12541 int status;
12542
12543 /* Don't try to install tracepoints until we've relocated our
12544 symbols, and fetched and merged the target's tracepoint list with
12545 ours. */
12546 if (rs->starting_up)
12547 return 0;
12548
12549 ts = current_trace_status ();
8bd200f1 12550 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
12551
12552 if (status == -1 || !ts->running_known || !ts->running)
12553 return 0;
12554
12555 /* If we are in a tracing experiment, but remote stub doesn't support
12556 installing tracepoint in trace, we have to return. */
12557 if (!remote_supports_install_in_trace ())
12558 return 0;
12559
12560 return 1;
12561}
12562
12563
35b1e5cc 12564static void
559d2b81
TT
12565remote_download_trace_state_variable (struct target_ops *self,
12566 struct trace_state_variable *tsv)
35b1e5cc
SS
12567{
12568 struct remote_state *rs = get_remote_state ();
00bf0b85 12569 char *p;
35b1e5cc 12570
bba74b36
YQ
12571 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
12572 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
12573 tsv->builtin);
00bf0b85
SS
12574 p = rs->buf + strlen (rs->buf);
12575 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
12576 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 12577 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 12578 *p++ = '\0';
35b1e5cc 12579 putpkt (rs->buf);
b6bb3468
PA
12580 remote_get_noisy_reply ();
12581 if (*rs->buf == '\0')
ad91cd99 12582 error (_("Target does not support this command."));
b6bb3468 12583 if (strcmp (rs->buf, "OK") != 0)
ad91cd99 12584 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
12585}
12586
d248b706 12587static void
46670d57
TT
12588remote_enable_tracepoint (struct target_ops *self,
12589 struct bp_location *location)
d248b706
KY
12590{
12591 struct remote_state *rs = get_remote_state ();
12592 char addr_buf[40];
12593
12594 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12595 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
12596 location->owner->number, addr_buf);
d248b706 12597 putpkt (rs->buf);
b6bb3468 12598 remote_get_noisy_reply ();
d248b706
KY
12599 if (*rs->buf == '\0')
12600 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
12601 if (strcmp (rs->buf, "OK") != 0)
12602 error (_("Error on target while enabling tracepoint."));
12603}
12604
12605static void
780b049c
TT
12606remote_disable_tracepoint (struct target_ops *self,
12607 struct bp_location *location)
d248b706
KY
12608{
12609 struct remote_state *rs = get_remote_state ();
12610 char addr_buf[40];
12611
12612 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12613 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
12614 location->owner->number, addr_buf);
d248b706 12615 putpkt (rs->buf);
b6bb3468 12616 remote_get_noisy_reply ();
d248b706
KY
12617 if (*rs->buf == '\0')
12618 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
12619 if (strcmp (rs->buf, "OK") != 0)
12620 error (_("Error on target while disabling tracepoint."));
12621}
12622
35b1e5cc 12623static void
583f9a86 12624remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
12625{
12626 asection *s;
81b9b86e 12627 bfd *abfd = NULL;
35b1e5cc 12628 bfd_size_type size;
608bcef2 12629 bfd_vma vma;
35b1e5cc 12630 int anysecs = 0;
c2fa21f1 12631 int offset = 0;
35b1e5cc
SS
12632
12633 if (!exec_bfd)
12634 return; /* No information to give. */
12635
b6bb3468
PA
12636 struct remote_state *rs = get_remote_state ();
12637
12638 strcpy (rs->buf, "QTro");
12639 offset = strlen (rs->buf);
35b1e5cc
SS
12640 for (s = exec_bfd->sections; s; s = s->next)
12641 {
12642 char tmp1[40], tmp2[40];
c2fa21f1 12643 int sec_length;
35b1e5cc
SS
12644
12645 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 12646 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
12647 (s->flags & SEC_READONLY) == 0)
12648 continue;
12649
12650 anysecs = 1;
81b9b86e 12651 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 12652 size = bfd_get_section_size (s);
608bcef2
HZ
12653 sprintf_vma (tmp1, vma);
12654 sprintf_vma (tmp2, vma + size);
c2fa21f1 12655 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
b6bb3468 12656 if (offset + sec_length + 1 > rs->buf_size)
c2fa21f1 12657 {
4082afcc 12658 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 12659 warning (_("\
c2fa21f1
HZ
12660Too many sections for read-only sections definition packet."));
12661 break;
12662 }
b6bb3468 12663 xsnprintf (rs->buf + offset, rs->buf_size - offset, ":%s,%s",
bba74b36 12664 tmp1, tmp2);
c2fa21f1 12665 offset += sec_length;
35b1e5cc
SS
12666 }
12667 if (anysecs)
12668 {
b6bb3468
PA
12669 putpkt (rs->buf);
12670 getpkt (&rs->buf, &rs->buf_size, 0);
35b1e5cc
SS
12671 }
12672}
12673
12674static void
e2d1aae3 12675remote_trace_start (struct target_ops *self)
35b1e5cc 12676{
b6bb3468
PA
12677 struct remote_state *rs = get_remote_state ();
12678
35b1e5cc 12679 putpkt ("QTStart");
b6bb3468
PA
12680 remote_get_noisy_reply ();
12681 if (*rs->buf == '\0')
ad91cd99 12682 error (_("Target does not support this command."));
b6bb3468
PA
12683 if (strcmp (rs->buf, "OK") != 0)
12684 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12685}
12686
12687static int
8bd200f1 12688remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 12689{
953b98d1 12690 /* Initialize it just to avoid a GCC false warning. */
f652de6f 12691 char *p = NULL;
0df8b418 12692 /* FIXME we need to get register block size some other way. */
00bf0b85 12693 extern int trace_regblock_size;
bd3eecc3 12694 enum packet_result result;
b6bb3468 12695 struct remote_state *rs = get_remote_state ();
bd3eecc3 12696
4082afcc 12697 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 12698 return -1;
a744cf53 12699
00bf0b85
SS
12700 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
12701
049dc89b
JK
12702 putpkt ("qTStatus");
12703
492d29ea 12704 TRY
67f41397 12705 {
b6bb3468 12706 p = remote_get_noisy_reply ();
67f41397 12707 }
492d29ea 12708 CATCH (ex, RETURN_MASK_ERROR)
67f41397 12709 {
598d3636
JK
12710 if (ex.error != TARGET_CLOSE_ERROR)
12711 {
12712 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
12713 return -1;
12714 }
12715 throw_exception (ex);
67f41397 12716 }
492d29ea 12717 END_CATCH
00bf0b85 12718
bd3eecc3
PA
12719 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
12720
00bf0b85 12721 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 12722 if (result == PACKET_UNKNOWN)
00bf0b85 12723 return -1;
35b1e5cc 12724
00bf0b85 12725 /* We're working with a live target. */
f5911ea1 12726 ts->filename = NULL;
00bf0b85 12727
00bf0b85 12728 if (*p++ != 'T')
b6bb3468 12729 error (_("Bogus trace status reply from target: %s"), rs->buf);
35b1e5cc 12730
84cebc4a
YQ
12731 /* Function 'parse_trace_status' sets default value of each field of
12732 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
12733 parse_trace_status (p, ts);
12734
12735 return ts->running;
35b1e5cc
SS
12736}
12737
70221824 12738static void
db90e85c 12739remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
12740 struct uploaded_tp *utp)
12741{
12742 struct remote_state *rs = get_remote_state ();
f196051f
SS
12743 char *reply;
12744 struct bp_location *loc;
12745 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 12746 size_t size = get_remote_packet_size ();
f196051f
SS
12747
12748 if (tp)
12749 {
c1fc2657 12750 tp->hit_count = 0;
f196051f 12751 tp->traceframe_usage = 0;
c1fc2657 12752 for (loc = tp->loc; loc; loc = loc->next)
f196051f
SS
12753 {
12754 /* If the tracepoint was never downloaded, don't go asking for
12755 any status. */
12756 if (tp->number_on_target == 0)
12757 continue;
bba74b36
YQ
12758 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
12759 phex_nz (loc->address, 0));
f196051f 12760 putpkt (rs->buf);
b6bb3468 12761 reply = remote_get_noisy_reply ();
f196051f
SS
12762 if (reply && *reply)
12763 {
12764 if (*reply == 'V')
12765 parse_tracepoint_status (reply + 1, bp, utp);
12766 }
12767 }
12768 }
12769 else if (utp)
12770 {
12771 utp->hit_count = 0;
12772 utp->traceframe_usage = 0;
bba74b36
YQ
12773 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
12774 phex_nz (utp->addr, 0));
f196051f 12775 putpkt (rs->buf);
b6bb3468 12776 reply = remote_get_noisy_reply ();
f196051f
SS
12777 if (reply && *reply)
12778 {
12779 if (*reply == 'V')
12780 parse_tracepoint_status (reply + 1, bp, utp);
12781 }
12782 }
12783}
12784
35b1e5cc 12785static void
74499f1b 12786remote_trace_stop (struct target_ops *self)
35b1e5cc 12787{
b6bb3468
PA
12788 struct remote_state *rs = get_remote_state ();
12789
35b1e5cc 12790 putpkt ("QTStop");
b6bb3468
PA
12791 remote_get_noisy_reply ();
12792 if (*rs->buf == '\0')
ad91cd99 12793 error (_("Target does not support this command."));
b6bb3468
PA
12794 if (strcmp (rs->buf, "OK") != 0)
12795 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12796}
12797
12798static int
bd4c6793
TT
12799remote_trace_find (struct target_ops *self,
12800 enum trace_find_type type, int num,
cc5925ad 12801 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
12802 int *tpp)
12803{
12804 struct remote_state *rs = get_remote_state ();
bba74b36 12805 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
12806 char *p, *reply;
12807 int target_frameno = -1, target_tracept = -1;
12808
e6e4e701
PA
12809 /* Lookups other than by absolute frame number depend on the current
12810 trace selected, so make sure it is correct on the remote end
12811 first. */
12812 if (type != tfind_number)
12813 set_remote_traceframe ();
12814
35b1e5cc
SS
12815 p = rs->buf;
12816 strcpy (p, "QTFrame:");
12817 p = strchr (p, '\0');
12818 switch (type)
12819 {
12820 case tfind_number:
bba74b36 12821 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
12822 break;
12823 case tfind_pc:
bba74b36 12824 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
12825 break;
12826 case tfind_tp:
bba74b36 12827 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
12828 break;
12829 case tfind_range:
bba74b36
YQ
12830 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
12831 phex_nz (addr2, 0));
35b1e5cc
SS
12832 break;
12833 case tfind_outside:
bba74b36
YQ
12834 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
12835 phex_nz (addr2, 0));
35b1e5cc
SS
12836 break;
12837 default:
9b20d036 12838 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
12839 }
12840
12841 putpkt (rs->buf);
b6bb3468 12842 reply = remote_get_noisy_reply ();
ad91cd99
PA
12843 if (*reply == '\0')
12844 error (_("Target does not support this command."));
35b1e5cc
SS
12845
12846 while (reply && *reply)
12847 switch (*reply)
12848 {
12849 case 'F':
f197e0f1
VP
12850 p = ++reply;
12851 target_frameno = (int) strtol (p, &reply, 16);
12852 if (reply == p)
12853 error (_("Unable to parse trace frame number"));
e6e4e701
PA
12854 /* Don't update our remote traceframe number cache on failure
12855 to select a remote traceframe. */
f197e0f1
VP
12856 if (target_frameno == -1)
12857 return -1;
35b1e5cc
SS
12858 break;
12859 case 'T':
f197e0f1
VP
12860 p = ++reply;
12861 target_tracept = (int) strtol (p, &reply, 16);
12862 if (reply == p)
12863 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
12864 break;
12865 case 'O': /* "OK"? */
12866 if (reply[1] == 'K' && reply[2] == '\0')
12867 reply += 2;
12868 else
12869 error (_("Bogus reply from target: %s"), reply);
12870 break;
12871 default:
12872 error (_("Bogus reply from target: %s"), reply);
12873 }
12874 if (tpp)
12875 *tpp = target_tracept;
e6e4e701 12876
262e1174 12877 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
12878 return target_frameno;
12879}
12880
12881static int
4011015b
TT
12882remote_get_trace_state_variable_value (struct target_ops *self,
12883 int tsvnum, LONGEST *val)
35b1e5cc
SS
12884{
12885 struct remote_state *rs = get_remote_state ();
12886 char *reply;
12887 ULONGEST uval;
12888
e6e4e701
PA
12889 set_remote_traceframe ();
12890
bba74b36 12891 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc 12892 putpkt (rs->buf);
b6bb3468 12893 reply = remote_get_noisy_reply ();
35b1e5cc
SS
12894 if (reply && *reply)
12895 {
12896 if (*reply == 'V')
12897 {
12898 unpack_varlen_hex (reply + 1, &uval);
12899 *val = (LONGEST) uval;
12900 return 1;
12901 }
12902 }
12903 return 0;
12904}
12905
00bf0b85 12906static int
dc3decaf 12907remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
12908{
12909 struct remote_state *rs = get_remote_state ();
12910 char *p, *reply;
12911
12912 p = rs->buf;
12913 strcpy (p, "QTSave:");
12914 p += strlen (p);
12915 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
12916 error (_("Remote file name too long for trace save packet"));
9f1b45b0 12917 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
12918 *p++ = '\0';
12919 putpkt (rs->buf);
b6bb3468 12920 reply = remote_get_noisy_reply ();
d6c5869f 12921 if (*reply == '\0')
ad91cd99
PA
12922 error (_("Target does not support this command."));
12923 if (strcmp (reply, "OK") != 0)
12924 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
12925 return 0;
12926}
12927
12928/* This is basically a memory transfer, but needs to be its own packet
12929 because we don't know how the target actually organizes its trace
12930 memory, plus we want to be able to ask for as much as possible, but
12931 not be unhappy if we don't get as much as we ask for. */
12932
12933static LONGEST
88ee6f45
TT
12934remote_get_raw_trace_data (struct target_ops *self,
12935 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
12936{
12937 struct remote_state *rs = get_remote_state ();
12938 char *reply;
12939 char *p;
12940 int rslt;
12941
12942 p = rs->buf;
12943 strcpy (p, "qTBuffer:");
12944 p += strlen (p);
12945 p += hexnumstr (p, offset);
12946 *p++ = ',';
12947 p += hexnumstr (p, len);
12948 *p++ = '\0';
12949
12950 putpkt (rs->buf);
b6bb3468 12951 reply = remote_get_noisy_reply ();
00bf0b85
SS
12952 if (reply && *reply)
12953 {
12954 /* 'l' by itself means we're at the end of the buffer and
12955 there is nothing more to get. */
12956 if (*reply == 'l')
12957 return 0;
12958
12959 /* Convert the reply into binary. Limit the number of bytes to
12960 convert according to our passed-in buffer size, rather than
12961 what was returned in the packet; if the target is
12962 unexpectedly generous and gives us a bigger reply than we
12963 asked for, we don't want to crash. */
b6bb3468 12964 rslt = hex2bin (reply, buf, len);
00bf0b85
SS
12965 return rslt;
12966 }
12967
12968 /* Something went wrong, flag as an error. */
12969 return -1;
12970}
12971
35b1e5cc 12972static void
37b25738 12973remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
12974{
12975 struct remote_state *rs = get_remote_state ();
12976
4082afcc 12977 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 12978 {
ad91cd99
PA
12979 char *reply;
12980
bba74b36 12981 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 12982 putpkt (rs->buf);
b6bb3468 12983 reply = remote_get_noisy_reply ();
ad91cd99 12984 if (*reply == '\0')
33da3f1c 12985 error (_("Target does not support this command."));
ad91cd99
PA
12986 if (strcmp (reply, "OK") != 0)
12987 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
12988 }
12989 else if (val)
12990 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
12991}
12992
dc146f7c
VP
12993static int
12994remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
12995{
12996 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 12997
fe978cb0
PA
12998 if (info && info->priv)
12999 return info->priv->core;
dc146f7c
VP
13000 return -1;
13001}
13002
4daf5ac0 13003static void
736d5b1f 13004remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
13005{
13006 struct remote_state *rs = get_remote_state ();
ad91cd99 13007 char *reply;
4daf5ac0 13008
bba74b36 13009 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 13010 putpkt (rs->buf);
b6bb3468 13011 reply = remote_get_noisy_reply ();
ad91cd99 13012 if (*reply == '\0')
4daf5ac0 13013 error (_("Target does not support this command."));
ad91cd99
PA
13014 if (strcmp (reply, "OK") != 0)
13015 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
13016}
13017
b3b9301e 13018static struct traceframe_info *
a893e81f 13019remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
13020{
13021 char *text;
13022
13023 text = target_read_stralloc (&current_target,
13024 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
13025 if (text != NULL)
13026 {
13027 struct traceframe_info *info;
13028 struct cleanup *back_to = make_cleanup (xfree, text);
13029
13030 info = parse_traceframe_info (text);
13031 do_cleanups (back_to);
13032 return info;
13033 }
13034
13035 return NULL;
13036}
13037
405f8e94
SS
13038/* Handle the qTMinFTPILen packet. Returns the minimum length of
13039 instruction on which a fast tracepoint may be placed. Returns -1
13040 if the packet is not supported, and 0 if the minimum instruction
13041 length is unknown. */
13042
13043static int
0e67620a 13044remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
13045{
13046 struct remote_state *rs = get_remote_state ();
13047 char *reply;
13048
e886a173
PA
13049 /* If we're not debugging a process yet, the IPA can't be
13050 loaded. */
13051 if (!target_has_execution)
13052 return 0;
13053
13054 /* Make sure the remote is pointing at the right process. */
13055 set_general_process ();
13056
bba74b36 13057 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94 13058 putpkt (rs->buf);
b6bb3468 13059 reply = remote_get_noisy_reply ();
405f8e94
SS
13060 if (*reply == '\0')
13061 return -1;
13062 else
13063 {
13064 ULONGEST min_insn_len;
13065
13066 unpack_varlen_hex (reply, &min_insn_len);
13067
13068 return (int) min_insn_len;
13069 }
13070}
13071
f6f899bf 13072static void
4da384be 13073remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 13074{
4082afcc 13075 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
13076 {
13077 struct remote_state *rs = get_remote_state ();
13078 char *buf = rs->buf;
13079 char *endbuf = rs->buf + get_remote_packet_size ();
13080 enum packet_result result;
13081
13082 gdb_assert (val >= 0 || val == -1);
13083 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
13084 /* Send -1 as literal "-1" to avoid host size dependency. */
13085 if (val < 0)
13086 {
13087 *buf++ = '-';
13088 buf += hexnumstr (buf, (ULONGEST) -val);
13089 }
13090 else
13091 buf += hexnumstr (buf, (ULONGEST) val);
13092
13093 putpkt (rs->buf);
b6bb3468 13094 remote_get_noisy_reply ();
f6f899bf
HAQ
13095 result = packet_ok (rs->buf,
13096 &remote_protocol_packets[PACKET_QTBuffer_size]);
13097
13098 if (result != PACKET_OK)
13099 warning (_("Bogus reply from target: %s"), rs->buf);
13100 }
13101}
13102
f196051f 13103static int
d9e68a2c
TT
13104remote_set_trace_notes (struct target_ops *self,
13105 const char *user, const char *notes,
ca623f82 13106 const char *stop_notes)
f196051f
SS
13107{
13108 struct remote_state *rs = get_remote_state ();
13109 char *reply;
13110 char *buf = rs->buf;
13111 char *endbuf = rs->buf + get_remote_packet_size ();
13112 int nbytes;
13113
13114 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
13115 if (user)
13116 {
13117 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 13118 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
13119 buf += 2 * nbytes;
13120 *buf++ = ';';
13121 }
13122 if (notes)
13123 {
13124 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 13125 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
13126 buf += 2 * nbytes;
13127 *buf++ = ';';
13128 }
13129 if (stop_notes)
13130 {
13131 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 13132 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
13133 buf += 2 * nbytes;
13134 *buf++ = ';';
13135 }
13136 /* Ensure the buffer is terminated. */
13137 *buf = '\0';
13138
13139 putpkt (rs->buf);
b6bb3468 13140 reply = remote_get_noisy_reply ();
f196051f
SS
13141 if (*reply == '\0')
13142 return 0;
13143
13144 if (strcmp (reply, "OK") != 0)
13145 error (_("Bogus reply from target: %s"), reply);
13146
13147 return 1;
13148}
13149
d1feda86 13150static int
2c152180 13151remote_use_agent (struct target_ops *self, int use)
d1feda86 13152{
4082afcc 13153 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
13154 {
13155 struct remote_state *rs = get_remote_state ();
13156
13157 /* If the stub supports QAgent. */
bba74b36 13158 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
13159 putpkt (rs->buf);
13160 getpkt (&rs->buf, &rs->buf_size, 0);
13161
13162 if (strcmp (rs->buf, "OK") == 0)
13163 {
13164 use_agent = use;
13165 return 1;
13166 }
13167 }
13168
13169 return 0;
13170}
13171
13172static int
fe38f897 13173remote_can_use_agent (struct target_ops *self)
d1feda86 13174{
4082afcc 13175 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
13176}
13177
9accd112
MM
13178struct btrace_target_info
13179{
13180 /* The ptid of the traced thread. */
13181 ptid_t ptid;
f4abbc16
MM
13182
13183 /* The obtained branch trace configuration. */
13184 struct btrace_config conf;
9accd112
MM
13185};
13186
f4abbc16
MM
13187/* Reset our idea of our target's btrace configuration. */
13188
13189static void
13190remote_btrace_reset (void)
13191{
13192 struct remote_state *rs = get_remote_state ();
13193
13194 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
13195}
13196
9accd112
MM
13197/* Check whether the target supports branch tracing. */
13198
13199static int
043c3577 13200remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 13201{
4082afcc 13202 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 13203 return 0;
4082afcc 13204 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
13205 return 0;
13206
043c3577
MM
13207 switch (format)
13208 {
13209 case BTRACE_FORMAT_NONE:
13210 return 0;
13211
13212 case BTRACE_FORMAT_BTS:
13213 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
13214
13215 case BTRACE_FORMAT_PT:
13216 /* The trace is decoded on the host. Even if our target supports it,
13217 we still need to have libipt to decode the trace. */
13218#if defined (HAVE_LIBIPT)
13219 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
13220#else /* !defined (HAVE_LIBIPT) */
13221 return 0;
13222#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
13223 }
13224
13225 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
13226}
13227
f4abbc16
MM
13228/* Synchronize the configuration with the target. */
13229
13230static void
13231btrace_sync_conf (const struct btrace_config *conf)
13232{
d33501a5
MM
13233 struct packet_config *packet;
13234 struct remote_state *rs;
13235 char *buf, *pos, *endbuf;
13236
13237 rs = get_remote_state ();
13238 buf = rs->buf;
13239 endbuf = buf + get_remote_packet_size ();
13240
13241 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
13242 if (packet_config_support (packet) == PACKET_ENABLE
13243 && conf->bts.size != rs->btrace_config.bts.size)
13244 {
13245 pos = buf;
13246 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13247 conf->bts.size);
13248
13249 putpkt (buf);
13250 getpkt (&buf, &rs->buf_size, 0);
13251
13252 if (packet_ok (buf, packet) == PACKET_ERROR)
13253 {
13254 if (buf[0] == 'E' && buf[1] == '.')
13255 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
13256 else
13257 error (_("Failed to configure the BTS buffer size."));
13258 }
13259
13260 rs->btrace_config.bts.size = conf->bts.size;
13261 }
b20a6524
MM
13262
13263 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
13264 if (packet_config_support (packet) == PACKET_ENABLE
13265 && conf->pt.size != rs->btrace_config.pt.size)
13266 {
13267 pos = buf;
13268 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13269 conf->pt.size);
13270
13271 putpkt (buf);
13272 getpkt (&buf, &rs->buf_size, 0);
13273
13274 if (packet_ok (buf, packet) == PACKET_ERROR)
13275 {
13276 if (buf[0] == 'E' && buf[1] == '.')
13277 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
13278 else
13279 error (_("Failed to configure the trace buffer size."));
13280 }
13281
13282 rs->btrace_config.pt.size = conf->pt.size;
13283 }
f4abbc16
MM
13284}
13285
13286/* Read the current thread's btrace configuration from the target and
13287 store it into CONF. */
13288
13289static void
13290btrace_read_config (struct btrace_config *conf)
13291{
13292 char *xml;
13293
13294 xml = target_read_stralloc (&current_target,
b20a6524 13295 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
13296 if (xml != NULL)
13297 {
13298 struct cleanup *cleanup;
13299
13300 cleanup = make_cleanup (xfree, xml);
13301 parse_xml_btrace_conf (conf, xml);
13302 do_cleanups (cleanup);
13303 }
13304}
13305
c0272db5
TW
13306/* Maybe reopen target btrace. */
13307
13308static void
13309remote_btrace_maybe_reopen (void)
13310{
13311 struct remote_state *rs = get_remote_state ();
c0272db5
TW
13312 struct thread_info *tp;
13313 int btrace_target_pushed = 0;
13314 int warned = 0;
13315
5ed8105e
PA
13316 scoped_restore_current_thread restore_thread;
13317
c0272db5
TW
13318 ALL_NON_EXITED_THREADS (tp)
13319 {
13320 set_general_thread (tp->ptid);
13321
13322 memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
13323 btrace_read_config (&rs->btrace_config);
13324
13325 if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
13326 continue;
13327
13328#if !defined (HAVE_LIBIPT)
13329 if (rs->btrace_config.format == BTRACE_FORMAT_PT)
13330 {
13331 if (!warned)
13332 {
13333 warned = 1;
13334 warning (_("GDB does not support Intel Processor Trace. "
13335 "\"record\" will not work in this session."));
13336 }
13337
13338 continue;
13339 }
13340#endif /* !defined (HAVE_LIBIPT) */
13341
13342 /* Push target, once, but before anything else happens. This way our
13343 changes to the threads will be cleaned up by unpushing the target
13344 in case btrace_read_config () throws. */
13345 if (!btrace_target_pushed)
13346 {
13347 btrace_target_pushed = 1;
13348 record_btrace_push_target ();
13349 printf_filtered (_("Target is recording using %s.\n"),
13350 btrace_format_string (rs->btrace_config.format));
13351 }
13352
13353 tp->btrace.target = XCNEW (struct btrace_target_info);
13354 tp->btrace.target->ptid = tp->ptid;
13355 tp->btrace.target->conf = rs->btrace_config;
13356 }
c0272db5
TW
13357}
13358
9accd112
MM
13359/* Enable branch tracing. */
13360
13361static struct btrace_target_info *
f4abbc16
MM
13362remote_enable_btrace (struct target_ops *self, ptid_t ptid,
13363 const struct btrace_config *conf)
9accd112
MM
13364{
13365 struct btrace_target_info *tinfo = NULL;
b20a6524 13366 struct packet_config *packet = NULL;
9accd112
MM
13367 struct remote_state *rs = get_remote_state ();
13368 char *buf = rs->buf;
13369 char *endbuf = rs->buf + get_remote_packet_size ();
13370
b20a6524
MM
13371 switch (conf->format)
13372 {
13373 case BTRACE_FORMAT_BTS:
13374 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
13375 break;
13376
13377 case BTRACE_FORMAT_PT:
13378 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
13379 break;
13380 }
13381
13382 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13383 error (_("Target does not support branch tracing."));
13384
f4abbc16
MM
13385 btrace_sync_conf (conf);
13386
9accd112
MM
13387 set_general_thread (ptid);
13388
13389 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13390 putpkt (rs->buf);
13391 getpkt (&rs->buf, &rs->buf_size, 0);
13392
13393 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13394 {
13395 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13396 error (_("Could not enable branch tracing for %s: %s"),
13397 target_pid_to_str (ptid), rs->buf + 2);
13398 else
13399 error (_("Could not enable branch tracing for %s."),
13400 target_pid_to_str (ptid));
13401 }
13402
8d749320 13403 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
13404 tinfo->ptid = ptid;
13405
f4abbc16
MM
13406 /* If we fail to read the configuration, we lose some information, but the
13407 tracing itself is not impacted. */
492d29ea
PA
13408 TRY
13409 {
13410 btrace_read_config (&tinfo->conf);
13411 }
13412 CATCH (err, RETURN_MASK_ERROR)
13413 {
13414 if (err.message != NULL)
13415 warning ("%s", err.message);
13416 }
13417 END_CATCH
f4abbc16 13418
9accd112
MM
13419 return tinfo;
13420}
13421
13422/* Disable branch tracing. */
13423
13424static void
25e95349
TT
13425remote_disable_btrace (struct target_ops *self,
13426 struct btrace_target_info *tinfo)
9accd112
MM
13427{
13428 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
13429 struct remote_state *rs = get_remote_state ();
13430 char *buf = rs->buf;
13431 char *endbuf = rs->buf + get_remote_packet_size ();
13432
4082afcc 13433 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13434 error (_("Target does not support branch tracing."));
13435
13436 set_general_thread (tinfo->ptid);
13437
13438 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13439 putpkt (rs->buf);
13440 getpkt (&rs->buf, &rs->buf_size, 0);
13441
13442 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13443 {
13444 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13445 error (_("Could not disable branch tracing for %s: %s"),
13446 target_pid_to_str (tinfo->ptid), rs->buf + 2);
13447 else
13448 error (_("Could not disable branch tracing for %s."),
13449 target_pid_to_str (tinfo->ptid));
13450 }
13451
13452 xfree (tinfo);
13453}
13454
13455/* Teardown branch tracing. */
13456
13457static void
1777056d
TT
13458remote_teardown_btrace (struct target_ops *self,
13459 struct btrace_target_info *tinfo)
9accd112
MM
13460{
13461 /* We must not talk to the target during teardown. */
13462 xfree (tinfo);
13463}
13464
13465/* Read the branch trace. */
13466
969c39fb 13467static enum btrace_error
39c49f83 13468remote_read_btrace (struct target_ops *self,
734b0e4b 13469 struct btrace_data *btrace,
969c39fb 13470 struct btrace_target_info *tinfo,
9accd112
MM
13471 enum btrace_read_type type)
13472{
13473 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
969c39fb 13474 struct cleanup *cleanup;
9accd112
MM
13475 const char *annex;
13476 char *xml;
13477
4082afcc 13478 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13479 error (_("Target does not support branch tracing."));
13480
13481#if !defined(HAVE_LIBEXPAT)
13482 error (_("Cannot process branch tracing result. XML parsing not supported."));
13483#endif
13484
13485 switch (type)
13486 {
864089d2 13487 case BTRACE_READ_ALL:
9accd112
MM
13488 annex = "all";
13489 break;
864089d2 13490 case BTRACE_READ_NEW:
9accd112
MM
13491 annex = "new";
13492 break;
969c39fb
MM
13493 case BTRACE_READ_DELTA:
13494 annex = "delta";
13495 break;
9accd112
MM
13496 default:
13497 internal_error (__FILE__, __LINE__,
13498 _("Bad branch tracing read type: %u."),
13499 (unsigned int) type);
13500 }
13501
13502 xml = target_read_stralloc (&current_target,
b20a6524 13503 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
13504 if (xml == NULL)
13505 return BTRACE_ERR_UNKNOWN;
9accd112 13506
969c39fb 13507 cleanup = make_cleanup (xfree, xml);
734b0e4b 13508 parse_xml_btrace (btrace, xml);
969c39fb 13509 do_cleanups (cleanup);
9accd112 13510
969c39fb 13511 return BTRACE_ERR_NONE;
9accd112
MM
13512}
13513
f4abbc16
MM
13514static const struct btrace_config *
13515remote_btrace_conf (struct target_ops *self,
13516 const struct btrace_target_info *tinfo)
13517{
13518 return &tinfo->conf;
13519}
13520
ced63ec0 13521static int
5436ff03 13522remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 13523{
4082afcc
PA
13524 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
13525 == PACKET_ENABLE);
ced63ec0
GB
13526}
13527
9dd130a0
TT
13528/* Implementation of to_load. */
13529
13530static void
9cbe5fff 13531remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
13532{
13533 generic_load (name, from_tty);
13534}
13535
c78fa86a
GB
13536/* Accepts an integer PID; returns a string representing a file that
13537 can be opened on the remote side to get the symbols for the child
13538 process. Returns NULL if the operation is not supported. */
13539
13540static char *
13541remote_pid_to_exec_file (struct target_ops *self, int pid)
13542{
13543 static char *filename = NULL;
835205d0
GB
13544 struct inferior *inf;
13545 char *annex = NULL;
c78fa86a
GB
13546
13547 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
13548 return NULL;
13549
13550 if (filename != NULL)
13551 xfree (filename);
13552
835205d0
GB
13553 inf = find_inferior_pid (pid);
13554 if (inf == NULL)
13555 internal_error (__FILE__, __LINE__,
13556 _("not currently attached to process %d"), pid);
13557
13558 if (!inf->fake_pid_p)
13559 {
13560 const int annex_size = 9;
13561
224c3ddb 13562 annex = (char *) alloca (annex_size);
835205d0
GB
13563 xsnprintf (annex, annex_size, "%x", pid);
13564 }
13565
c78fa86a
GB
13566 filename = target_read_stralloc (&current_target,
13567 TARGET_OBJECT_EXEC_FILE, annex);
13568
13569 return filename;
13570}
13571
750ce8d1
YQ
13572/* Implement the to_can_do_single_step target_ops method. */
13573
13574static int
13575remote_can_do_single_step (struct target_ops *ops)
13576{
13577 /* We can only tell whether target supports single step or not by
13578 supported s and S vCont actions if the stub supports vContSupported
13579 feature. If the stub doesn't support vContSupported feature,
13580 we have conservatively to think target doesn't supports single
13581 step. */
13582 if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
13583 {
13584 struct remote_state *rs = get_remote_state ();
13585
13586 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
13587 remote_vcont_probe (rs);
13588
13589 return rs->supports_vCont.s && rs->supports_vCont.S;
13590 }
13591 else
13592 return 0;
13593}
13594
3a00c802
PA
13595/* Implementation of the to_execution_direction method for the remote
13596 target. */
13597
13598static enum exec_direction_kind
13599remote_execution_direction (struct target_ops *self)
13600{
13601 struct remote_state *rs = get_remote_state ();
13602
13603 return rs->last_resume_exec_dir;
13604}
13605
f6327dcb
KB
13606/* Return pointer to the thread_info struct which corresponds to
13607 THREAD_HANDLE (having length HANDLE_LEN). */
13608
13609static struct thread_info *
13610remote_thread_handle_to_thread_info (struct target_ops *ops,
13611 const gdb_byte *thread_handle,
13612 int handle_len,
13613 struct inferior *inf)
13614{
13615 struct thread_info *tp;
13616
13617 ALL_NON_EXITED_THREADS (tp)
13618 {
13619 struct private_thread_info *priv = get_private_info_thread (tp);
13620
13621 if (tp->inf == inf && priv != NULL)
13622 {
13623 if (handle_len != priv->thread_handle->size ())
13624 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
13625 handle_len, priv->thread_handle->size ());
13626 if (memcmp (thread_handle, priv->thread_handle->data (),
13627 handle_len) == 0)
13628 return tp;
13629 }
13630 }
13631
13632 return NULL;
13633}
13634
c906108c 13635static void
fba45db2 13636init_remote_ops (void)
c906108c 13637{
c5aa993b 13638 remote_ops.to_shortname = "remote";
c906108c 13639 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 13640 remote_ops.to_doc =
c906108c 13641 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
13642Specify the serial device it is connected to\n\
13643(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
13644 remote_ops.to_open = remote_open;
13645 remote_ops.to_close = remote_close;
c906108c 13646 remote_ops.to_detach = remote_detach;
6ad8ae5c 13647 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 13648 remote_ops.to_resume = remote_resume;
85ad3aaf 13649 remote_ops.to_commit_resume = remote_commit_resume;
c906108c
SS
13650 remote_ops.to_wait = remote_wait;
13651 remote_ops.to_fetch_registers = remote_fetch_registers;
13652 remote_ops.to_store_registers = remote_store_registers;
13653 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 13654 remote_ops.to_files_info = remote_files_info;
c906108c
SS
13655 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
13656 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
13657 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
13658 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
13659 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
13660 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
13661 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
13662 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
13663 remote_ops.to_watchpoint_addr_within_range =
13664 remote_watchpoint_addr_within_range;
3c3bea1c
GS
13665 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
13666 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
13667 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
13668 remote_ops.to_region_ok_for_hw_watchpoint
13669 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
13670 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
13671 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 13672 remote_ops.to_kill = remote_kill;
9dd130a0 13673 remote_ops.to_load = remote_load;
c906108c 13674 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 13675 remote_ops.to_pass_signals = remote_pass_signals;
82075af2 13676 remote_ops.to_set_syscall_catchpoint = remote_set_syscall_catchpoint;
9b224c5e 13677 remote_ops.to_program_signals = remote_program_signals;
c906108c 13678 remote_ops.to_thread_alive = remote_thread_alive;
79efa585 13679 remote_ops.to_thread_name = remote_thread_name;
e8032dde 13680 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 13681 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 13682 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 13683 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 13684 remote_ops.to_stop = remote_stop;
bfedc46a 13685 remote_ops.to_interrupt = remote_interrupt;
93692b58 13686 remote_ops.to_pass_ctrlc = remote_pass_ctrlc;
4b8a223f 13687 remote_ops.to_xfer_partial = remote_xfer_partial;
09c98b44 13688 remote_ops.to_get_memory_xfer_limit = remote_get_memory_xfer_limit;
96baa820 13689 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 13690 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 13691 remote_ops.to_log_command = serial_log_command;
38691318 13692 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 13693 remote_ops.to_stratum = process_stratum;
c35b1492
PA
13694 remote_ops.to_has_all_memory = default_child_has_all_memory;
13695 remote_ops.to_has_memory = default_child_has_memory;
13696 remote_ops.to_has_stack = default_child_has_stack;
13697 remote_ops.to_has_registers = default_child_has_registers;
13698 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 13699 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 13700 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 13701 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 13702 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
13703 remote_ops.to_flash_erase = remote_flash_erase;
13704 remote_ops.to_flash_done = remote_flash_done;
29709017 13705 remote_ops.to_read_description = remote_read_description;
08388c79 13706 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
13707 remote_ops.to_can_async_p = remote_can_async_p;
13708 remote_ops.to_is_async_p = remote_is_async_p;
13709 remote_ops.to_async = remote_async;
65706a29 13710 remote_ops.to_thread_events = remote_thread_events;
750ce8d1 13711 remote_ops.to_can_do_single_step = remote_can_do_single_step;
75c99385
PA
13712 remote_ops.to_terminal_inferior = remote_terminal_inferior;
13713 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 13714 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 13715 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
13716 remote_ops.to_supports_disable_randomization
13717 = remote_supports_disable_randomization;
4bd7dc42 13718 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
13719 remote_ops.to_fileio_open = remote_hostio_open;
13720 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
13721 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 13722 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
13723 remote_ops.to_fileio_close = remote_hostio_close;
13724 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 13725 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 13726 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 13727 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 13728 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 13729 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
13730 remote_ops.to_trace_init = remote_trace_init;
13731 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 13732 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
13733 remote_ops.to_download_trace_state_variable
13734 = remote_download_trace_state_variable;
d248b706
KY
13735 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
13736 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
13737 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
13738 remote_ops.to_trace_start = remote_trace_start;
13739 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 13740 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
13741 remote_ops.to_trace_stop = remote_trace_stop;
13742 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
13743 remote_ops.to_get_trace_state_variable_value
13744 = remote_get_trace_state_variable_value;
00bf0b85
SS
13745 remote_ops.to_save_trace_data = remote_save_trace_data;
13746 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
13747 remote_ops.to_upload_trace_state_variables
13748 = remote_upload_trace_state_variables;
00bf0b85 13749 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 13750 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 13751 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 13752 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 13753 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 13754 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 13755 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 13756 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 13757 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 13758 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
13759 remote_ops.to_static_tracepoint_marker_at
13760 = remote_static_tracepoint_marker_at;
13761 remote_ops.to_static_tracepoint_markers_by_strid
13762 = remote_static_tracepoint_markers_by_strid;
b3b9301e 13763 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
13764 remote_ops.to_use_agent = remote_use_agent;
13765 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
13766 remote_ops.to_supports_btrace = remote_supports_btrace;
13767 remote_ops.to_enable_btrace = remote_enable_btrace;
13768 remote_ops.to_disable_btrace = remote_disable_btrace;
13769 remote_ops.to_teardown_btrace = remote_teardown_btrace;
13770 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 13771 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
13772 remote_ops.to_augmented_libraries_svr4_read =
13773 remote_augmented_libraries_svr4_read;
8020350c
DB
13774 remote_ops.to_follow_fork = remote_follow_fork;
13775 remote_ops.to_follow_exec = remote_follow_exec;
13776 remote_ops.to_insert_fork_catchpoint = remote_insert_fork_catchpoint;
13777 remote_ops.to_remove_fork_catchpoint = remote_remove_fork_catchpoint;
13778 remote_ops.to_insert_vfork_catchpoint = remote_insert_vfork_catchpoint;
13779 remote_ops.to_remove_vfork_catchpoint = remote_remove_vfork_catchpoint;
13780 remote_ops.to_insert_exec_catchpoint = remote_insert_exec_catchpoint;
13781 remote_ops.to_remove_exec_catchpoint = remote_remove_exec_catchpoint;
3a00c802 13782 remote_ops.to_execution_direction = remote_execution_direction;
f6327dcb
KB
13783 remote_ops.to_thread_handle_to_thread_info =
13784 remote_thread_handle_to_thread_info;
c906108c
SS
13785}
13786
13787/* Set up the extended remote vector by making a copy of the standard
13788 remote vector and adding to it. */
13789
13790static void
fba45db2 13791init_extended_remote_ops (void)
c906108c
SS
13792{
13793 extended_remote_ops = remote_ops;
13794
0f71a2f6 13795 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 13796 extended_remote_ops.to_longname =
c906108c 13797 "Extended remote serial target in gdb-specific protocol";
c5aa993b 13798 extended_remote_ops.to_doc =
c906108c 13799 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
13800Specify the serial device it is connected to (e.g. /dev/ttya).";
13801 extended_remote_ops.to_open = extended_remote_open;
c906108c 13802 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
2d717e4f
DJ
13803 extended_remote_ops.to_detach = extended_remote_detach;
13804 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 13805 extended_remote_ops.to_post_attach = extended_remote_post_attach;
03583c20
UW
13806 extended_remote_ops.to_supports_disable_randomization
13807 = extended_remote_supports_disable_randomization;
0f71a2f6
JM
13808}
13809
6426a772 13810static int
6a109b6b 13811remote_can_async_p (struct target_ops *ops)
6426a772 13812{
5d93a237
TT
13813 struct remote_state *rs = get_remote_state ();
13814
3015c064
SM
13815 /* We don't go async if the user has explicitly prevented it with the
13816 "maint set target-async" command. */
c6ebd6cf 13817 if (!target_async_permitted)
75c99385
PA
13818 return 0;
13819
23860348 13820 /* We're async whenever the serial device is. */
5d93a237 13821 return serial_can_async_p (rs->remote_desc);
6426a772
JM
13822}
13823
13824static int
6a109b6b 13825remote_is_async_p (struct target_ops *ops)
6426a772 13826{
5d93a237
TT
13827 struct remote_state *rs = get_remote_state ();
13828
c6ebd6cf 13829 if (!target_async_permitted)
75c99385
PA
13830 /* We only enable async when the user specifically asks for it. */
13831 return 0;
13832
23860348 13833 /* We're async whenever the serial device is. */
5d93a237 13834 return serial_is_async_p (rs->remote_desc);
6426a772
JM
13835}
13836
2acceee2
JM
13837/* Pass the SERIAL event on and up to the client. One day this code
13838 will be able to delay notifying the client of an event until the
23860348 13839 point where an entire packet has been received. */
2acceee2 13840
2acceee2
JM
13841static serial_event_ftype remote_async_serial_handler;
13842
6426a772 13843static void
819cc324 13844remote_async_serial_handler (struct serial *scb, void *context)
6426a772 13845{
2acceee2
JM
13846 /* Don't propogate error information up to the client. Instead let
13847 the client find out about the error by querying the target. */
6a3753b3 13848 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
13849}
13850
74531fed
PA
13851static void
13852remote_async_inferior_event_handler (gdb_client_data data)
13853{
13854 inferior_event_handler (INF_REG_EVENT, NULL);
13855}
13856
2acceee2 13857static void
6a3753b3 13858remote_async (struct target_ops *ops, int enable)
2acceee2 13859{
5d93a237
TT
13860 struct remote_state *rs = get_remote_state ();
13861
6a3753b3 13862 if (enable)
2acceee2 13863 {
88b496c3 13864 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
13865
13866 /* If there are pending events in the stop reply queue tell the
13867 event loop to process them. */
13868 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
13869 mark_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13870 /* For simplicity, below we clear the pending events token
13871 without remembering whether it is marked, so here we always
13872 mark it. If there's actually no pending notification to
13873 process, this ends up being a no-op (other than a spurious
13874 event-loop wakeup). */
13875 if (target_is_non_stop_p ())
13876 mark_async_event_handler (rs->notif_state->get_pending_events_token);
2acceee2
JM
13877 }
13878 else
b7d2e916
PA
13879 {
13880 serial_async (rs->remote_desc, NULL, NULL);
6efcd9a8
PA
13881 /* If the core is disabling async, it doesn't want to be
13882 disturbed with target events. Clear all async event sources
13883 too. */
b7d2e916 13884 clear_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13885 if (target_is_non_stop_p ())
13886 clear_async_event_handler (rs->notif_state->get_pending_events_token);
b7d2e916 13887 }
6426a772
JM
13888}
13889
65706a29
PA
13890/* Implementation of the to_thread_events method. */
13891
13892static void
13893remote_thread_events (struct target_ops *ops, int enable)
13894{
13895 struct remote_state *rs = get_remote_state ();
13896 size_t size = get_remote_packet_size ();
65706a29
PA
13897
13898 if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
13899 return;
13900
13901 xsnprintf (rs->buf, size, "QThreadEvents:%x", enable ? 1 : 0);
13902 putpkt (rs->buf);
13903 getpkt (&rs->buf, &rs->buf_size, 0);
13904
13905 switch (packet_ok (rs->buf,
13906 &remote_protocol_packets[PACKET_QThreadEvents]))
13907 {
13908 case PACKET_OK:
13909 if (strcmp (rs->buf, "OK") != 0)
13910 error (_("Remote refused setting thread events: %s"), rs->buf);
13911 break;
13912 case PACKET_ERROR:
13913 warning (_("Remote failure reply: %s"), rs->buf);
13914 break;
13915 case PACKET_UNKNOWN:
13916 break;
13917 }
13918}
13919
5a2468f5 13920static void
c2d11a7d 13921set_remote_cmd (char *args, int from_tty)
5a2468f5 13922{
635c7e8a 13923 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
13924}
13925
d471ea57
AC
13926static void
13927show_remote_cmd (char *args, int from_tty)
13928{
37a105a1 13929 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 13930 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1 13931 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 13932 struct ui_out *uiout = current_uiout;
37a105a1 13933
2e783024 13934 ui_out_emit_tuple tuple_emitter (uiout, "showlist");
37a105a1
DJ
13935 for (; list != NULL; list = list->next)
13936 if (strcmp (list->name, "Z-packet") == 0)
13937 continue;
427c3a89
DJ
13938 else if (list->type == not_set_cmd)
13939 /* Alias commands are exactly like the original, except they
13940 don't have the normal type. */
13941 continue;
13942 else
37a105a1 13943 {
2e783024 13944 ui_out_emit_tuple option_emitter (uiout, "option");
a744cf53 13945
112e8700
SM
13946 uiout->field_string ("name", list->name);
13947 uiout->text (": ");
427c3a89 13948 if (list->type == show_cmd)
f5c4fcd9 13949 do_show_command (NULL, from_tty, list);
427c3a89
DJ
13950 else
13951 cmd_func (list, NULL, from_tty);
37a105a1 13952 }
d471ea57 13953}
5a2468f5 13954
0f71a2f6 13955
23860348 13956/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
13957static void
13958remote_new_objfile (struct objfile *objfile)
13959{
5d93a237
TT
13960 struct remote_state *rs = get_remote_state ();
13961
13962 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 13963 remote_check_symbols ();
dc8acb97
MS
13964}
13965
00bf0b85
SS
13966/* Pull all the tracepoints defined on the target and create local
13967 data structures representing them. We don't want to create real
13968 tracepoints yet, we don't want to mess up the user's existing
13969 collection. */
13970
13971static int
ab6617cc 13972remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 13973{
00bf0b85
SS
13974 struct remote_state *rs = get_remote_state ();
13975 char *p;
d5551862 13976
00bf0b85
SS
13977 /* Ask for a first packet of tracepoint definition. */
13978 putpkt ("qTfP");
13979 getpkt (&rs->buf, &rs->buf_size, 0);
13980 p = rs->buf;
13981 while (*p && *p != 'l')
d5551862 13982 {
00bf0b85
SS
13983 parse_tracepoint_definition (p, utpp);
13984 /* Ask for another packet of tracepoint definition. */
13985 putpkt ("qTsP");
13986 getpkt (&rs->buf, &rs->buf_size, 0);
13987 p = rs->buf;
d5551862 13988 }
00bf0b85 13989 return 0;
d5551862
SS
13990}
13991
00bf0b85 13992static int
181e3713
TT
13993remote_upload_trace_state_variables (struct target_ops *self,
13994 struct uploaded_tsv **utsvp)
d5551862 13995{
00bf0b85 13996 struct remote_state *rs = get_remote_state ();
d5551862 13997 char *p;
d5551862 13998
00bf0b85
SS
13999 /* Ask for a first packet of variable definition. */
14000 putpkt ("qTfV");
d5551862
SS
14001 getpkt (&rs->buf, &rs->buf_size, 0);
14002 p = rs->buf;
00bf0b85 14003 while (*p && *p != 'l')
d5551862 14004 {
00bf0b85
SS
14005 parse_tsv_definition (p, utsvp);
14006 /* Ask for another packet of variable definition. */
14007 putpkt ("qTsV");
d5551862
SS
14008 getpkt (&rs->buf, &rs->buf_size, 0);
14009 p = rs->buf;
14010 }
00bf0b85 14011 return 0;
d5551862
SS
14012}
14013
c1e36e3e
PA
14014/* The "set/show range-stepping" show hook. */
14015
14016static void
14017show_range_stepping (struct ui_file *file, int from_tty,
14018 struct cmd_list_element *c,
14019 const char *value)
14020{
14021 fprintf_filtered (file,
14022 _("Debugger's willingness to use range stepping "
14023 "is %s.\n"), value);
14024}
14025
14026/* The "set/show range-stepping" set hook. */
14027
14028static void
14029set_range_stepping (char *ignore_args, int from_tty,
14030 struct cmd_list_element *c)
14031{
5d93a237
TT
14032 struct remote_state *rs = get_remote_state ();
14033
c1e36e3e
PA
14034 /* Whene enabling, check whether range stepping is actually
14035 supported by the target, and warn if not. */
14036 if (use_range_stepping)
14037 {
5d93a237 14038 if (rs->remote_desc != NULL)
c1e36e3e 14039 {
4082afcc 14040 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
14041 remote_vcont_probe (rs);
14042
4082afcc 14043 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
14044 && rs->supports_vCont.r)
14045 return;
14046 }
14047
14048 warning (_("Range stepping is not supported by the current target"));
14049 }
14050}
14051
c906108c 14052void
fba45db2 14053_initialize_remote (void)
c906108c 14054{
9a7071a8 14055 struct cmd_list_element *cmd;
6f937416 14056 const char *cmd_name;
ea9c271d 14057
0f71a2f6 14058 /* architecture specific data */
2bc416ba 14059 remote_gdbarch_data_handle =
23860348 14060 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
14061 remote_g_packet_data_handle =
14062 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 14063
94585166
DB
14064 remote_pspace_data
14065 = register_program_space_data_with_cleanup (NULL,
14066 remote_pspace_data_cleanup);
14067
ea9c271d
DJ
14068 /* Initialize the per-target state. At the moment there is only one
14069 of these, not one per target. Only one target is active at a
cf792862
TT
14070 time. */
14071 remote_state = new_remote_state ();
ea9c271d 14072
c906108c
SS
14073 init_remote_ops ();
14074 add_target (&remote_ops);
14075
14076 init_extended_remote_ops ();
14077 add_target (&extended_remote_ops);
cce74817 14078
dc8acb97 14079 /* Hook into new objfile notification. */
06d3b283 14080 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
14081 /* We're no longer interested in notification events of an inferior
14082 when it exits. */
14083 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 14084
c906108c
SS
14085#if 0
14086 init_remote_threadtests ();
14087#endif
14088
722247f1 14089 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 14090 /* set/show remote ... */
d471ea57 14091
1bedd215 14092 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
14093Remote protocol specific variables\n\
14094Configure various remote-protocol specific variables such as\n\
1bedd215 14095the packets being used"),
cff3e48b 14096 &remote_set_cmdlist, "set remote ",
23860348 14097 0 /* allow-unknown */, &setlist);
1bedd215 14098 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
14099Remote protocol specific variables\n\
14100Configure various remote-protocol specific variables such as\n\
1bedd215 14101the packets being used"),
cff3e48b 14102 &remote_show_cmdlist, "show remote ",
23860348 14103 0 /* allow-unknown */, &showlist);
5a2468f5 14104
1a966eab
AC
14105 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
14106Compare section data on target to the exec file.\n\
95cf3b38
DT
14107Argument is a single section name (default: all loaded sections).\n\
14108To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
14109 &cmdlist);
14110
1a966eab
AC
14111 add_cmd ("packet", class_maintenance, packet_command, _("\
14112Send an arbitrary packet to a remote target.\n\
c906108c
SS
14113 maintenance packet TEXT\n\
14114If GDB is talking to an inferior via the GDB serial protocol, then\n\
14115this command sends the string TEXT to the inferior, and displays the\n\
14116response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 14117terminating `#' character and checksum."),
c906108c
SS
14118 &maintenancelist);
14119
7915a72c
AC
14120 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
14121Set whether to send break if interrupted."), _("\
14122Show whether to send break if interrupted."), _("\
14123If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 14124 set_remotebreak, show_remotebreak,
e707bbc2 14125 &setlist, &showlist);
9a7071a8
JB
14126 cmd_name = "remotebreak";
14127 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
14128 deprecate_cmd (cmd, "set remote interrupt-sequence");
14129 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
14130 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
14131 deprecate_cmd (cmd, "show remote interrupt-sequence");
14132
14133 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
14134 interrupt_sequence_modes, &interrupt_sequence_mode,
14135 _("\
9a7071a8
JB
14136Set interrupt sequence to remote target."), _("\
14137Show interrupt sequence to remote target."), _("\
14138Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14139 NULL, show_interrupt_sequence,
14140 &remote_set_cmdlist,
14141 &remote_show_cmdlist);
14142
14143 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
14144 &interrupt_on_connect, _("\
14145Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14146Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14147If set, interrupt sequence is sent to remote target."),
14148 NULL, NULL,
14149 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 14150
23860348 14151 /* Install commands for configuring memory read/write packets. */
11cf8741 14152
1a966eab
AC
14153 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
14154Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 14155 &setlist);
1a966eab
AC
14156 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
14157Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
14158 &showlist);
14159 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
14160 set_memory_write_packet_size, _("\
14161Set the maximum number of bytes per memory-write packet.\n\
14162Specify the number of bytes in a packet or 0 (zero) for the\n\
14163default packet size. The actual limit is further reduced\n\
14164dependent on the target. Specify ``fixed'' to disable the\n\
14165further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14166 &remote_set_cmdlist);
14167 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
14168 set_memory_read_packet_size, _("\
14169Set the maximum number of bytes per memory-read packet.\n\
14170Specify the number of bytes in a packet or 0 (zero) for the\n\
14171default packet size. The actual limit is further reduced\n\
14172dependent on the target. Specify ``fixed'' to disable the\n\
14173further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14174 &remote_set_cmdlist);
14175 add_cmd ("memory-write-packet-size", no_class,
14176 show_memory_write_packet_size,
1a966eab 14177 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
14178 &remote_show_cmdlist);
14179 add_cmd ("memory-read-packet-size", no_class,
14180 show_memory_read_packet_size,
1a966eab 14181 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 14182 &remote_show_cmdlist);
c906108c 14183
b3f42336 14184 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
14185 &remote_hw_watchpoint_limit, _("\
14186Set the maximum number of target hardware watchpoints."), _("\
14187Show the maximum number of target hardware watchpoints."), _("\
14188Specify a negative limit for unlimited."),
3e43a32a
MS
14189 NULL, NULL, /* FIXME: i18n: The maximum
14190 number of target hardware
14191 watchpoints is %s. */
b3f42336 14192 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
14193 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
14194 &remote_hw_watchpoint_length_limit, _("\
14195Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
14196Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
14197Specify a negative limit for unlimited."),
14198 NULL, NULL, /* FIXME: i18n: The maximum
14199 length (in bytes) of a target
14200 hardware watchpoint is %s. */
14201 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 14202 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
14203 &remote_hw_breakpoint_limit, _("\
14204Set the maximum number of target hardware breakpoints."), _("\
14205Show the maximum number of target hardware breakpoints."), _("\
14206Specify a negative limit for unlimited."),
3e43a32a
MS
14207 NULL, NULL, /* FIXME: i18n: The maximum
14208 number of target hardware
14209 breakpoints is %s. */
b3f42336 14210 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 14211
1b493192
PA
14212 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
14213 &remote_address_size, _("\
4d28ad1e
AC
14214Set the maximum size of the address (in bits) in a memory packet."), _("\
14215Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
14216 NULL,
14217 NULL, /* FIXME: i18n: */
14218 &setlist, &showlist);
c906108c 14219
ca4f7f8b
PA
14220 init_all_packet_configs ();
14221
444abaca 14222 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 14223 "X", "binary-download", 1);
0f71a2f6 14224
444abaca 14225 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 14226 "vCont", "verbose-resume", 0);
506fb367 14227
89be2091
DJ
14228 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
14229 "QPassSignals", "pass-signals", 0);
14230
82075af2
JS
14231 add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
14232 "QCatchSyscalls", "catch-syscalls", 0);
14233
9b224c5e
PA
14234 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
14235 "QProgramSignals", "program-signals", 0);
14236
bc3b087d
SDJ
14237 add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
14238 "QSetWorkingDir", "set-working-dir", 0);
14239
aefd8b33
SDJ
14240 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
14241 "QStartupWithShell", "startup-with-shell", 0);
14242
0a2dde4a
SDJ
14243 add_packet_config_cmd (&remote_protocol_packets
14244 [PACKET_QEnvironmentHexEncoded],
14245 "QEnvironmentHexEncoded", "environment-hex-encoded",
14246 0);
14247
14248 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
14249 "QEnvironmentReset", "environment-reset",
14250 0);
14251
14252 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
14253 "QEnvironmentUnset", "environment-unset",
14254 0);
14255
444abaca 14256 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 14257 "qSymbol", "symbol-lookup", 0);
dc8acb97 14258
444abaca 14259 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 14260 "P", "set-register", 1);
d471ea57 14261
444abaca 14262 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 14263 "p", "fetch-register", 1);
b96ec7ac 14264
444abaca 14265 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 14266 "Z0", "software-breakpoint", 0);
d471ea57 14267
444abaca 14268 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 14269 "Z1", "hardware-breakpoint", 0);
d471ea57 14270
444abaca 14271 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 14272 "Z2", "write-watchpoint", 0);
d471ea57 14273
444abaca 14274 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 14275 "Z3", "read-watchpoint", 0);
d471ea57 14276
444abaca 14277 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 14278 "Z4", "access-watchpoint", 0);
d471ea57 14279
0876f84a
DJ
14280 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
14281 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 14282
c78fa86a
GB
14283 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
14284 "qXfer:exec-file:read", "pid-to-exec-file", 0);
14285
23181151
DJ
14286 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
14287 "qXfer:features:read", "target-features", 0);
14288
cfa9d6d9
DJ
14289 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
14290 "qXfer:libraries:read", "library-info", 0);
14291
2268b414
JK
14292 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
14293 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
14294
fd79ecee
DJ
14295 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
14296 "qXfer:memory-map:read", "memory-map", 0);
14297
0e7f50da
UW
14298 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
14299 "qXfer:spu:read", "read-spu-object", 0);
14300
14301 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
14302 "qXfer:spu:write", "write-spu-object", 0);
14303
07e059b5
VP
14304 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
14305 "qXfer:osdata:read", "osdata", 0);
14306
dc146f7c
VP
14307 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
14308 "qXfer:threads:read", "threads", 0);
14309
4aa995e1
PA
14310 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
14311 "qXfer:siginfo:read", "read-siginfo-object", 0);
14312
14313 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
14314 "qXfer:siginfo:write", "write-siginfo-object", 0);
14315
b3b9301e
PA
14316 add_packet_config_cmd
14317 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 14318 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 14319
169081d0
TG
14320 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
14321 "qXfer:uib:read", "unwind-info-block", 0);
14322
444abaca 14323 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 14324 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
14325 0);
14326
711e434b
PM
14327 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
14328 "qGetTIBAddr", "get-thread-information-block-address",
14329 0);
14330
40ab02ce
MS
14331 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
14332 "bc", "reverse-continue", 0);
14333
14334 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
14335 "bs", "reverse-step", 0);
14336
be2a5f71
DJ
14337 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
14338 "qSupported", "supported-packets", 0);
14339
08388c79
DE
14340 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
14341 "qSearch:memory", "search-memory", 0);
14342
bd3eecc3
PA
14343 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
14344 "qTStatus", "trace-status", 0);
14345
15a201c8
GB
14346 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
14347 "vFile:setfs", "hostio-setfs", 0);
14348
a6b151f1
DJ
14349 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
14350 "vFile:open", "hostio-open", 0);
14351
14352 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
14353 "vFile:pread", "hostio-pread", 0);
14354
14355 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
14356 "vFile:pwrite", "hostio-pwrite", 0);
14357
14358 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
14359 "vFile:close", "hostio-close", 0);
14360
14361 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
14362 "vFile:unlink", "hostio-unlink", 0);
14363
b9e7b9c3
UW
14364 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
14365 "vFile:readlink", "hostio-readlink", 0);
14366
0a93529c
GB
14367 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
14368 "vFile:fstat", "hostio-fstat", 0);
14369
2d717e4f
DJ
14370 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
14371 "vAttach", "attach", 0);
14372
14373 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
14374 "vRun", "run", 0);
14375
a6f3e723
SL
14376 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
14377 "QStartNoAckMode", "noack", 0);
14378
82f73884
PA
14379 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
14380 "vKill", "kill", 0);
14381
0b16c5cf
PA
14382 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
14383 "qAttached", "query-attached", 0);
14384
782b2b07 14385 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
14386 "ConditionalTracepoints",
14387 "conditional-tracepoints", 0);
3788aec7
LM
14388
14389 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
14390 "ConditionalBreakpoints",
14391 "conditional-breakpoints", 0);
14392
d3ce09f5
SS
14393 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
14394 "BreakpointCommands",
14395 "breakpoint-commands", 0);
14396
7a697b8d
SS
14397 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
14398 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 14399
409873ef
SS
14400 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
14401 "TracepointSource", "TracepointSource", 0);
14402
d914c394
SS
14403 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
14404 "QAllow", "allow", 0);
14405
0fb4aa4b
PA
14406 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
14407 "StaticTracepoints", "static-tracepoints", 0);
14408
1e4d1764
YQ
14409 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
14410 "InstallInTrace", "install-in-trace", 0);
14411
0fb4aa4b
PA
14412 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
14413 "qXfer:statictrace:read", "read-sdata-object", 0);
14414
78d85199
YQ
14415 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
14416 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
14417
03583c20
UW
14418 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
14419 "QDisableRandomization", "disable-randomization", 0);
14420
d1feda86
YQ
14421 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
14422 "QAgent", "agent", 0);
14423
f6f899bf
HAQ
14424 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
14425 "QTBuffer:size", "trace-buffer-size", 0);
14426
9accd112
MM
14427 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
14428 "Qbtrace:off", "disable-btrace", 0);
14429
14430 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
14431 "Qbtrace:bts", "enable-btrace-bts", 0);
14432
14433 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
14434 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
14435
14436 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
14437 "qXfer:btrace", "read-btrace", 0);
14438
f4abbc16
MM
14439 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
14440 "qXfer:btrace-conf", "read-btrace-conf", 0);
14441
d33501a5
MM
14442 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
14443 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
14444
73b8c1fd
PA
14445 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
14446 "multiprocess-feature", "multiprocess-feature", 0);
14447
f7e6eed5
PA
14448 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
14449 "swbreak-feature", "swbreak-feature", 0);
14450
14451 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
14452 "hwbreak-feature", "hwbreak-feature", 0);
14453
89245bc0
DB
14454 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
14455 "fork-event-feature", "fork-event-feature", 0);
14456
14457 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
14458 "vfork-event-feature", "vfork-event-feature", 0);
14459
b20a6524
MM
14460 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
14461 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
14462
750ce8d1
YQ
14463 add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
14464 "vContSupported", "verbose-resume-supported", 0);
14465
94585166
DB
14466 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
14467 "exec-event-feature", "exec-event-feature", 0);
14468
de979965
PA
14469 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
14470 "vCtrlC", "ctrl-c", 0);
14471
65706a29
PA
14472 add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
14473 "QThreadEvents", "thread-events", 0);
14474
f2faf941
PA
14475 add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
14476 "N stop reply", "no-resumed-stop-reply", 0);
14477
0b736949
DB
14478 /* Assert that we've registered "set remote foo-packet" commands
14479 for all packet configs. */
ca4f7f8b
PA
14480 {
14481 int i;
14482
14483 for (i = 0; i < PACKET_MAX; i++)
14484 {
14485 /* Ideally all configs would have a command associated. Some
14486 still don't though. */
14487 int excepted;
14488
14489 switch (i)
14490 {
14491 case PACKET_QNonStop:
ca4f7f8b
PA
14492 case PACKET_EnableDisableTracepoints_feature:
14493 case PACKET_tracenz_feature:
14494 case PACKET_DisconnectedTracing_feature:
14495 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
14496 case PACKET_qCRC:
14497 /* Additions to this list need to be well justified:
14498 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
14499 excepted = 1;
14500 break;
14501 default:
14502 excepted = 0;
14503 break;
14504 }
14505
14506 /* This catches both forgetting to add a config command, and
14507 forgetting to remove a packet from the exception list. */
14508 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
14509 }
14510 }
14511
37a105a1
DJ
14512 /* Keep the old ``set remote Z-packet ...'' working. Each individual
14513 Z sub-packet has its own set and show commands, but users may
14514 have sets to this variable in their .gdbinit files (or in their
14515 documentation). */
e9e68a56 14516 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
14517 &remote_Z_packet_detect, _("\
14518Set use of remote protocol `Z' packets"), _("\
14519Show use of remote protocol `Z' packets "), _("\
3b64bf98 14520When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 14521packets."),
e9e68a56 14522 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
14523 show_remote_protocol_Z_packet_cmd,
14524 /* FIXME: i18n: Use of remote protocol
14525 `Z' packets is %s. */
e9e68a56 14526 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 14527
a6b151f1
DJ
14528 add_prefix_cmd ("remote", class_files, remote_command, _("\
14529Manipulate files on the remote system\n\
14530Transfer files to and from the remote target system."),
14531 &remote_cmdlist, "remote ",
14532 0 /* allow-unknown */, &cmdlist);
14533
14534 add_cmd ("put", class_files, remote_put_command,
14535 _("Copy a local file to the remote system."),
14536 &remote_cmdlist);
14537
14538 add_cmd ("get", class_files, remote_get_command,
14539 _("Copy a remote file to the local system."),
14540 &remote_cmdlist);
14541
14542 add_cmd ("delete", class_files, remote_delete_command,
14543 _("Delete a remote file."),
14544 &remote_cmdlist);
14545
2d717e4f 14546 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 14547 &remote_exec_file_var, _("\
2d717e4f 14548Set the remote pathname for \"run\""), _("\
94585166
DB
14549Show the remote pathname for \"run\""), NULL,
14550 set_remote_exec_file,
14551 show_remote_exec_file,
14552 &remote_set_cmdlist,
14553 &remote_show_cmdlist);
2d717e4f 14554
c1e36e3e
PA
14555 add_setshow_boolean_cmd ("range-stepping", class_run,
14556 &use_range_stepping, _("\
14557Enable or disable range stepping."), _("\
14558Show whether target-assisted range stepping is enabled."), _("\
14559If on, and the target supports it, when stepping a source line, GDB\n\
14560tells the target to step the corresponding range of addresses itself instead\n\
14561of issuing multiple single-steps. This speeds up source level\n\
14562stepping. If off, GDB always issues single-steps, even if range\n\
14563stepping is supported by the target. The default is on."),
14564 set_range_stepping,
14565 show_range_stepping,
14566 &setlist,
14567 &showlist);
14568
449092f6
CV
14569 /* Eventually initialize fileio. See fileio.c */
14570 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 14571
ba348170 14572 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 14573 special ptids with it set to != 0. */
ba348170
PA
14574 magic_null_ptid = ptid_build (42000, -1, 1);
14575 not_sent_ptid = ptid_build (42000, -2, 1);
14576 any_thread_ptid = ptid_build (42000, 0, 1);
c906108c 14577}
This page took 5.737374 seconds and 4 git commands to generate.