[ARM] PR ld/21402, only override the symbol dynamic decision on undefined weak symbol.
[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 656static struct remote_arch_state *
5cd63fda 657get_remote_arch_state (struct gdbarch *gdbarch)
d01949b6 658{
5cd63fda 659 gdb_assert (gdbarch != NULL);
19ba03f4 660 return ((struct remote_arch_state *)
5cd63fda 661 gdbarch_data (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. */
5cd63fda 674 get_remote_arch_state (target_gdbarch ());
0b83947e
DJ
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 ();
5cd63fda 881 remote_arch_state *rsa = get_remote_arch_state (target_gdbarch ());
ea9c271d 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 *
5cd63fda
PA
890packet_reg_from_regnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
891 long regnum)
ad10f812 892{
5cd63fda 893 if (regnum < 0 && regnum >= gdbarch_num_regs (gdbarch))
b323314b
AC
894 return NULL;
895 else
ad10f812 896 {
ea9c271d 897 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 898
b323314b
AC
899 gdb_assert (r->regnum == regnum);
900 return r;
ad10f812 901 }
ad10f812
AC
902}
903
904static struct packet_reg *
5cd63fda
PA
905packet_reg_from_pnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
906 LONGEST pnum)
ad10f812 907{
b323314b 908 int i;
a744cf53 909
5cd63fda 910 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
ad10f812 911 {
ea9c271d 912 struct packet_reg *r = &rsa->regs[i];
a744cf53 913
b323314b
AC
914 if (r->pnum == pnum)
915 return r;
ad10f812
AC
916 }
917 return NULL;
d01949b6
AC
918}
919
c906108c
SS
920static struct target_ops remote_ops;
921
922static struct target_ops extended_remote_ops;
923
6426a772
JM
924/* FIXME: cagney/1999-09-23: Even though getpkt was called with
925 ``forever'' still use the normal timeout mechanism. This is
926 currently used by the ASYNC code to guarentee that target reads
927 during the initial connect always time-out. Once getpkt has been
928 modified to return a timeout indication and, in turn
929 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 930 this can go away. */
6426a772
JM
931static int wait_forever_enabled_p = 1;
932
9a7071a8
JB
933/* Allow the user to specify what sequence to send to the remote
934 when he requests a program interruption: Although ^C is usually
935 what remote systems expect (this is the default, here), it is
936 sometimes preferable to send a break. On other systems such
937 as the Linux kernel, a break followed by g, which is Magic SysRq g
938 is required in order to interrupt the execution. */
939const char interrupt_sequence_control_c[] = "Ctrl-C";
940const char interrupt_sequence_break[] = "BREAK";
941const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 942static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
943 {
944 interrupt_sequence_control_c,
945 interrupt_sequence_break,
946 interrupt_sequence_break_g,
947 NULL
948 };
949static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
950
951static void
952show_interrupt_sequence (struct ui_file *file, int from_tty,
953 struct cmd_list_element *c,
954 const char *value)
955{
956 if (interrupt_sequence_mode == interrupt_sequence_control_c)
957 fprintf_filtered (file,
958 _("Send the ASCII ETX character (Ctrl-c) "
959 "to the remote target to interrupt the "
960 "execution of the program.\n"));
961 else if (interrupt_sequence_mode == interrupt_sequence_break)
962 fprintf_filtered (file,
963 _("send a break signal to the remote target "
964 "to interrupt the execution of the program.\n"));
965 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
966 fprintf_filtered (file,
967 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
968 "the remote target to interrupt the execution "
969 "of Linux kernel.\n"));
970 else
971 internal_error (__FILE__, __LINE__,
972 _("Invalid value for interrupt_sequence_mode: %s."),
973 interrupt_sequence_mode);
974}
6426a772 975
9a7071a8
JB
976/* This boolean variable specifies whether interrupt_sequence is sent
977 to the remote target when gdb connects to it.
978 This is mostly needed when you debug the Linux kernel: The Linux kernel
979 expects BREAK g which is Magic SysRq g for connecting gdb. */
980static int interrupt_on_connect = 0;
c906108c 981
9a7071a8
JB
982/* This variable is used to implement the "set/show remotebreak" commands.
983 Since these commands are now deprecated in favor of "set/show remote
984 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
985static int remote_break;
986
9a7071a8
JB
987static void
988set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
989{
990 if (remote_break)
991 interrupt_sequence_mode = interrupt_sequence_break;
992 else
993 interrupt_sequence_mode = interrupt_sequence_control_c;
994}
995
996static void
997show_remotebreak (struct ui_file *file, int from_tty,
998 struct cmd_list_element *c,
999 const char *value)
1000{
1001}
1002
c906108c
SS
1003/* This variable sets the number of bits in an address that are to be
1004 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 1005 leading zeros, the entire address would be sent. This variable
c906108c
SS
1006 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1007 initial implementation of remote.c restricted the address sent in
1008 memory packets to ``host::sizeof long'' bytes - (typically 32
1009 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1010 address was never sent. Since fixing this bug may cause a break in
1011 some remote targets this variable is principly provided to
23860348 1012 facilitate backward compatibility. */
c906108c 1013
883b9c6c 1014static unsigned int remote_address_size;
c906108c 1015
75c99385
PA
1016/* Temporary to track who currently owns the terminal. See
1017 remote_terminal_* for more details. */
6426a772
JM
1018
1019static int remote_async_terminal_ours_p;
1020
11cf8741 1021\f
11cf8741 1022/* User configurable variables for the number of characters in a
ea9c271d
DJ
1023 memory read/write packet. MIN (rsa->remote_packet_size,
1024 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 1025 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
1026 (speed up transfers). The variables ``preferred_*'' (the user
1027 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 1028 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
1029
1030struct memory_packet_config
1031{
a121b7c1 1032 const char *name;
11cf8741
JM
1033 long size;
1034 int fixed_p;
1035};
1036
a5c0808e
PA
1037/* The default max memory-write-packet-size. The 16k is historical.
1038 (It came from older GDB's using alloca for buffers and the
1039 knowledge (folklore?) that some hosts don't cope very well with
1040 large alloca calls.) */
1041#define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384
1042
1043/* The minimum remote packet size for memory transfers. Ensures we
1044 can write at least one byte. */
1045#define MIN_MEMORY_PACKET_SIZE 20
1046
11cf8741
JM
1047/* Compute the current size of a read/write packet. Since this makes
1048 use of ``actual_register_packet_size'' the computation is dynamic. */
1049
1050static long
1051get_memory_packet_size (struct memory_packet_config *config)
1052{
d01949b6 1053 struct remote_state *rs = get_remote_state ();
5cd63fda 1054 remote_arch_state *rsa = get_remote_arch_state (target_gdbarch ());
ea9c271d 1055
11cf8741
JM
1056 long what_they_get;
1057 if (config->fixed_p)
1058 {
1059 if (config->size <= 0)
a5c0808e 1060 what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE;
11cf8741
JM
1061 else
1062 what_they_get = config->size;
1063 }
1064 else
1065 {
ea9c271d 1066 what_they_get = get_remote_packet_size ();
23860348 1067 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1068 if (config->size > 0
1069 && what_they_get > config->size)
1070 what_they_get = config->size;
be2a5f71
DJ
1071
1072 /* Limit it to the size of the targets ``g'' response unless we have
1073 permission from the stub to use a larger packet size. */
1074 if (rs->explicit_packet_size == 0
1075 && rsa->actual_register_packet_size > 0
1076 && what_they_get > rsa->actual_register_packet_size)
1077 what_they_get = rsa->actual_register_packet_size;
11cf8741 1078 }
a5c0808e
PA
1079 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1080 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1081
1082 /* Make sure there is room in the global buffer for this packet
1083 (including its trailing NUL byte). */
1084 if (rs->buf_size < what_they_get + 1)
1085 {
1086 rs->buf_size = 2 * what_they_get;
224c3ddb 1087 rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get);
6d820c5c
DJ
1088 }
1089
11cf8741
JM
1090 return what_they_get;
1091}
1092
0df8b418 1093/* Update the size of a read/write packet. If they user wants
23860348 1094 something really big then do a sanity check. */
11cf8741
JM
1095
1096static void
ac88e2de 1097set_memory_packet_size (const char *args, struct memory_packet_config *config)
11cf8741
JM
1098{
1099 int fixed_p = config->fixed_p;
1100 long size = config->size;
a744cf53 1101
11cf8741 1102 if (args == NULL)
8a3fe4f8 1103 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1104 else if (strcmp (args, "hard") == 0
1105 || strcmp (args, "fixed") == 0)
1106 fixed_p = 1;
1107 else if (strcmp (args, "soft") == 0
1108 || strcmp (args, "limit") == 0)
1109 fixed_p = 0;
1110 else
1111 {
1112 char *end;
a744cf53 1113
11cf8741
JM
1114 size = strtoul (args, &end, 0);
1115 if (args == end)
8a3fe4f8 1116 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1117
1118 /* Instead of explicitly capping the size of a packet to or
1119 disallowing it, the user is allowed to set the size to
1120 something arbitrarily large. */
11cf8741 1121 }
a5c0808e
PA
1122
1123 /* So that the query shows the correct value. */
1124 if (size <= 0)
1125 size = DEFAULT_MAX_MEMORY_PACKET_SIZE;
1126
23860348 1127 /* Extra checks? */
11cf8741
JM
1128 if (fixed_p && !config->fixed_p)
1129 {
e2e0b3e5
AC
1130 if (! query (_("The target may not be able to correctly handle a %s\n"
1131 "of %ld bytes. Change the packet size? "),
11cf8741 1132 config->name, size))
8a3fe4f8 1133 error (_("Packet size not changed."));
11cf8741 1134 }
23860348 1135 /* Update the config. */
11cf8741
JM
1136 config->fixed_p = fixed_p;
1137 config->size = size;
1138}
1139
1140static void
1141show_memory_packet_size (struct memory_packet_config *config)
1142{
a3f17187 1143 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1144 if (config->fixed_p)
a3f17187 1145 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1146 get_memory_packet_size (config));
1147 else
a3f17187 1148 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1149 get_memory_packet_size (config));
1150}
1151
1152static struct memory_packet_config memory_write_packet_config =
1153{
1154 "memory-write-packet-size",
1155};
1156
1157static void
ac88e2de 1158set_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1159{
1160 set_memory_packet_size (args, &memory_write_packet_config);
1161}
1162
1163static void
ac88e2de 1164show_memory_write_packet_size (const char *args, int from_tty)
11cf8741
JM
1165{
1166 show_memory_packet_size (&memory_write_packet_config);
1167}
1168
1169static long
1170get_memory_write_packet_size (void)
1171{
1172 return get_memory_packet_size (&memory_write_packet_config);
1173}
1174
1175static struct memory_packet_config memory_read_packet_config =
1176{
1177 "memory-read-packet-size",
1178};
1179
1180static void
ac88e2de 1181set_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1182{
1183 set_memory_packet_size (args, &memory_read_packet_config);
1184}
1185
1186static void
ac88e2de 1187show_memory_read_packet_size (const char *args, int from_tty)
11cf8741
JM
1188{
1189 show_memory_packet_size (&memory_read_packet_config);
1190}
1191
1192static long
1193get_memory_read_packet_size (void)
1194{
1195 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1196
11cf8741
JM
1197 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1198 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1199 increased beyond this. */
1200 if (size > get_remote_packet_size ())
1201 size = get_remote_packet_size ();
11cf8741
JM
1202 return size;
1203}
1204
11cf8741 1205\f
5a2468f5 1206/* Generic configuration support for packets the stub optionally
0df8b418 1207 supports. Allows the user to specify the use of the packet as well
23860348 1208 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1209
1210enum packet_support
1211 {
1212 PACKET_SUPPORT_UNKNOWN = 0,
1213 PACKET_ENABLE,
1214 PACKET_DISABLE
1215 };
1216
5a2468f5
JM
1217struct packet_config
1218 {
bb572ddd
DJ
1219 const char *name;
1220 const char *title;
4082afcc
PA
1221
1222 /* If auto, GDB auto-detects support for this packet or feature,
1223 either through qSupported, or by trying the packet and looking
1224 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1225 packet. If false, the packet is disabled. Configs that don't
1226 have an associated command always have this set to auto. */
7f19b9a2 1227 enum auto_boolean detect;
4082afcc
PA
1228
1229 /* Does the target support this packet? */
5a2468f5
JM
1230 enum packet_support support;
1231 };
1232
d471ea57 1233/* Analyze a packet's return value and update the packet config
23860348 1234 accordingly. */
d471ea57
AC
1235
1236enum packet_result
1237{
1238 PACKET_ERROR,
1239 PACKET_OK,
1240 PACKET_UNKNOWN
1241};
1242
4082afcc
PA
1243static enum packet_support packet_config_support (struct packet_config *config);
1244static enum packet_support packet_support (int packet);
5a2468f5
JM
1245
1246static void
fba45db2 1247show_packet_config_cmd (struct packet_config *config)
5a2468f5 1248{
a121b7c1 1249 const char *support = "internal-error";
a744cf53 1250
4082afcc 1251 switch (packet_config_support (config))
5a2468f5
JM
1252 {
1253 case PACKET_ENABLE:
1254 support = "enabled";
1255 break;
1256 case PACKET_DISABLE:
1257 support = "disabled";
1258 break;
1259 case PACKET_SUPPORT_UNKNOWN:
1260 support = "unknown";
1261 break;
1262 }
1263 switch (config->detect)
1264 {
7f19b9a2 1265 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1266 printf_filtered (_("Support for the `%s' packet "
1267 "is auto-detected, currently %s.\n"),
37a105a1 1268 config->name, support);
5a2468f5 1269 break;
7f19b9a2
AC
1270 case AUTO_BOOLEAN_TRUE:
1271 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1272 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1273 config->name, support);
8e248173 1274 break;
5a2468f5
JM
1275 }
1276}
1277
1278static void
bb572ddd
DJ
1279add_packet_config_cmd (struct packet_config *config, const char *name,
1280 const char *title, int legacy)
d471ea57 1281{
5a2468f5
JM
1282 char *set_doc;
1283 char *show_doc;
d471ea57 1284 char *cmd_name;
3ed07be4 1285
5a2468f5
JM
1286 config->name = name;
1287 config->title = title;
b435e160
AC
1288 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1289 name, title);
3e43a32a
MS
1290 show_doc = xstrprintf ("Show current use of remote "
1291 "protocol `%s' (%s) packet",
b435e160 1292 name, title);
d471ea57 1293 /* set/show TITLE-packet {auto,on,off} */
b435e160 1294 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1295 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1296 &config->detect, set_doc,
1297 show_doc, NULL, /* help_doc */
4082afcc 1298 NULL,
bb572ddd
DJ
1299 show_remote_protocol_packet_cmd,
1300 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1301 /* The command code copies the documentation strings. */
1302 xfree (set_doc);
1303 xfree (show_doc);
23860348 1304 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1305 if (legacy)
1306 {
1307 char *legacy_name;
a744cf53 1308
b435e160 1309 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1310 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1311 &remote_set_cmdlist);
d471ea57 1312 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1313 &remote_show_cmdlist);
d471ea57 1314 }
5a2468f5
JM
1315}
1316
d471ea57 1317static enum packet_result
a76d924d 1318packet_check_result (const char *buf)
5a2468f5 1319{
d471ea57 1320 if (buf[0] != '\0')
5a2468f5 1321 {
d471ea57 1322 /* The stub recognized the packet request. Check that the
23860348 1323 operation succeeded. */
a76d924d
DJ
1324 if (buf[0] == 'E'
1325 && isxdigit (buf[1]) && isxdigit (buf[2])
1326 && buf[3] == '\0')
1327 /* "Enn" - definitly an error. */
1328 return PACKET_ERROR;
1329
1330 /* Always treat "E." as an error. This will be used for
1331 more verbose error messages, such as E.memtypes. */
1332 if (buf[0] == 'E' && buf[1] == '.')
1333 return PACKET_ERROR;
1334
1335 /* The packet may or may not be OK. Just assume it is. */
1336 return PACKET_OK;
1337 }
1338 else
1339 /* The stub does not support the packet. */
1340 return PACKET_UNKNOWN;
1341}
1342
1343static enum packet_result
1344packet_ok (const char *buf, struct packet_config *config)
1345{
1346 enum packet_result result;
1347
4082afcc
PA
1348 if (config->detect != AUTO_BOOLEAN_TRUE
1349 && config->support == PACKET_DISABLE)
1350 internal_error (__FILE__, __LINE__,
1351 _("packet_ok: attempt to use a disabled packet"));
1352
a76d924d
DJ
1353 result = packet_check_result (buf);
1354 switch (result)
1355 {
1356 case PACKET_OK:
1357 case PACKET_ERROR:
1358 /* The stub recognized the packet request. */
4082afcc 1359 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1360 {
d471ea57
AC
1361 if (remote_debug)
1362 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1363 "Packet %s (%s) is supported\n",
1364 config->name, config->title);
d471ea57 1365 config->support = PACKET_ENABLE;
d471ea57 1366 }
a76d924d
DJ
1367 break;
1368 case PACKET_UNKNOWN:
23860348 1369 /* The stub does not support the packet. */
4082afcc
PA
1370 if (config->detect == AUTO_BOOLEAN_AUTO
1371 && config->support == PACKET_ENABLE)
d471ea57 1372 {
4082afcc
PA
1373 /* If the stub previously indicated that the packet was
1374 supported then there is a protocol error. */
1375 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1376 config->name, config->title);
1377 }
1378 else if (config->detect == AUTO_BOOLEAN_TRUE)
1379 {
1380 /* The user set it wrong. */
1381 error (_("Enabled packet %s (%s) not recognized by stub"),
1382 config->name, config->title);
d471ea57 1383 }
4082afcc
PA
1384
1385 if (remote_debug)
1386 fprintf_unfiltered (gdb_stdlog,
1387 "Packet %s (%s) is NOT supported\n",
1388 config->name, config->title);
1389 config->support = PACKET_DISABLE;
a76d924d 1390 break;
5a2468f5 1391 }
a76d924d
DJ
1392
1393 return result;
5a2468f5
JM
1394}
1395
444abaca
DJ
1396enum {
1397 PACKET_vCont = 0,
1398 PACKET_X,
1399 PACKET_qSymbol,
1400 PACKET_P,
1401 PACKET_p,
1402 PACKET_Z0,
1403 PACKET_Z1,
1404 PACKET_Z2,
1405 PACKET_Z3,
1406 PACKET_Z4,
15a201c8 1407 PACKET_vFile_setfs,
a6b151f1
DJ
1408 PACKET_vFile_open,
1409 PACKET_vFile_pread,
1410 PACKET_vFile_pwrite,
1411 PACKET_vFile_close,
1412 PACKET_vFile_unlink,
b9e7b9c3 1413 PACKET_vFile_readlink,
0a93529c 1414 PACKET_vFile_fstat,
0876f84a 1415 PACKET_qXfer_auxv,
23181151 1416 PACKET_qXfer_features,
c78fa86a 1417 PACKET_qXfer_exec_file,
cfa9d6d9 1418 PACKET_qXfer_libraries,
2268b414 1419 PACKET_qXfer_libraries_svr4,
fd79ecee 1420 PACKET_qXfer_memory_map,
0e7f50da
UW
1421 PACKET_qXfer_spu_read,
1422 PACKET_qXfer_spu_write,
07e059b5 1423 PACKET_qXfer_osdata,
dc146f7c 1424 PACKET_qXfer_threads,
0fb4aa4b 1425 PACKET_qXfer_statictrace_read,
b3b9301e 1426 PACKET_qXfer_traceframe_info,
169081d0 1427 PACKET_qXfer_uib,
711e434b 1428 PACKET_qGetTIBAddr,
444abaca 1429 PACKET_qGetTLSAddr,
be2a5f71 1430 PACKET_qSupported,
bd3eecc3 1431 PACKET_qTStatus,
89be2091 1432 PACKET_QPassSignals,
82075af2 1433 PACKET_QCatchSyscalls,
9b224c5e 1434 PACKET_QProgramSignals,
bc3b087d 1435 PACKET_QSetWorkingDir,
aefd8b33 1436 PACKET_QStartupWithShell,
0a2dde4a
SDJ
1437 PACKET_QEnvironmentHexEncoded,
1438 PACKET_QEnvironmentReset,
1439 PACKET_QEnvironmentUnset,
936d2992 1440 PACKET_qCRC,
08388c79 1441 PACKET_qSearch_memory,
2d717e4f
DJ
1442 PACKET_vAttach,
1443 PACKET_vRun,
a6f3e723 1444 PACKET_QStartNoAckMode,
82f73884 1445 PACKET_vKill,
4aa995e1
PA
1446 PACKET_qXfer_siginfo_read,
1447 PACKET_qXfer_siginfo_write,
0b16c5cf 1448 PACKET_qAttached,
4082afcc
PA
1449
1450 /* Support for conditional tracepoints. */
782b2b07 1451 PACKET_ConditionalTracepoints,
4082afcc
PA
1452
1453 /* Support for target-side breakpoint conditions. */
3788aec7 1454 PACKET_ConditionalBreakpoints,
4082afcc
PA
1455
1456 /* Support for target-side breakpoint commands. */
d3ce09f5 1457 PACKET_BreakpointCommands,
4082afcc
PA
1458
1459 /* Support for fast tracepoints. */
7a697b8d 1460 PACKET_FastTracepoints,
4082afcc
PA
1461
1462 /* Support for static tracepoints. */
0fb4aa4b 1463 PACKET_StaticTracepoints,
4082afcc
PA
1464
1465 /* Support for installing tracepoints while a trace experiment is
1466 running. */
1e4d1764 1467 PACKET_InstallInTrace,
4082afcc 1468
40ab02ce
MS
1469 PACKET_bc,
1470 PACKET_bs,
409873ef 1471 PACKET_TracepointSource,
d914c394 1472 PACKET_QAllow,
78d85199 1473 PACKET_qXfer_fdpic,
03583c20 1474 PACKET_QDisableRandomization,
d1feda86 1475 PACKET_QAgent,
f6f899bf 1476 PACKET_QTBuffer_size,
9accd112
MM
1477 PACKET_Qbtrace_off,
1478 PACKET_Qbtrace_bts,
b20a6524 1479 PACKET_Qbtrace_pt,
9accd112 1480 PACKET_qXfer_btrace,
4082afcc
PA
1481
1482 /* Support for the QNonStop packet. */
1483 PACKET_QNonStop,
1484
65706a29
PA
1485 /* Support for the QThreadEvents packet. */
1486 PACKET_QThreadEvents,
1487
4082afcc
PA
1488 /* Support for multi-process extensions. */
1489 PACKET_multiprocess_feature,
1490
1491 /* Support for enabling and disabling tracepoints while a trace
1492 experiment is running. */
1493 PACKET_EnableDisableTracepoints_feature,
1494
1495 /* Support for collecting strings using the tracenz bytecode. */
1496 PACKET_tracenz_feature,
1497
1498 /* Support for continuing to run a trace experiment while GDB is
1499 disconnected. */
1500 PACKET_DisconnectedTracing_feature,
1501
1502 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1503 PACKET_augmented_libraries_svr4_read_feature,
1504
f4abbc16
MM
1505 /* Support for the qXfer:btrace-conf:read packet. */
1506 PACKET_qXfer_btrace_conf,
1507
d33501a5
MM
1508 /* Support for the Qbtrace-conf:bts:size packet. */
1509 PACKET_Qbtrace_conf_bts_size,
1510
f7e6eed5
PA
1511 /* Support for swbreak+ feature. */
1512 PACKET_swbreak_feature,
1513
1514 /* Support for hwbreak+ feature. */
1515 PACKET_hwbreak_feature,
1516
89245bc0
DB
1517 /* Support for fork events. */
1518 PACKET_fork_event_feature,
1519
1520 /* Support for vfork events. */
1521 PACKET_vfork_event_feature,
1522
b20a6524
MM
1523 /* Support for the Qbtrace-conf:pt:size packet. */
1524 PACKET_Qbtrace_conf_pt_size,
1525
94585166
DB
1526 /* Support for exec events. */
1527 PACKET_exec_event_feature,
1528
750ce8d1
YQ
1529 /* Support for query supported vCont actions. */
1530 PACKET_vContSupported,
1531
de979965
PA
1532 /* Support remote CTRL-C. */
1533 PACKET_vCtrlC,
1534
f2faf941
PA
1535 /* Support TARGET_WAITKIND_NO_RESUMED. */
1536 PACKET_no_resumed,
1537
444abaca
DJ
1538 PACKET_MAX
1539};
506fb367 1540
444abaca 1541static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1542
f7e6eed5
PA
1543/* Returns the packet's corresponding "set remote foo-packet" command
1544 state. See struct packet_config for more details. */
1545
1546static enum auto_boolean
1547packet_set_cmd_state (int packet)
1548{
1549 return remote_protocol_packets[packet].detect;
1550}
1551
4082afcc
PA
1552/* Returns whether a given packet or feature is supported. This takes
1553 into account the state of the corresponding "set remote foo-packet"
1554 command, which may be used to bypass auto-detection. */
dc8acb97 1555
4082afcc
PA
1556static enum packet_support
1557packet_config_support (struct packet_config *config)
1558{
1559 switch (config->detect)
444abaca 1560 {
4082afcc
PA
1561 case AUTO_BOOLEAN_TRUE:
1562 return PACKET_ENABLE;
1563 case AUTO_BOOLEAN_FALSE:
1564 return PACKET_DISABLE;
1565 case AUTO_BOOLEAN_AUTO:
1566 return config->support;
1567 default:
1568 gdb_assert_not_reached (_("bad switch"));
444abaca 1569 }
4082afcc
PA
1570}
1571
1572/* Same as packet_config_support, but takes the packet's enum value as
1573 argument. */
1574
1575static enum packet_support
1576packet_support (int packet)
1577{
1578 struct packet_config *config = &remote_protocol_packets[packet];
1579
1580 return packet_config_support (config);
dc8acb97
MS
1581}
1582
5a2468f5 1583static void
444abaca
DJ
1584show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1585 struct cmd_list_element *c,
1586 const char *value)
5a2468f5 1587{
444abaca 1588 struct packet_config *packet;
5a2468f5 1589
444abaca
DJ
1590 for (packet = remote_protocol_packets;
1591 packet < &remote_protocol_packets[PACKET_MAX];
1592 packet++)
1593 {
1594 if (&packet->detect == c->var)
1595 {
1596 show_packet_config_cmd (packet);
1597 return;
1598 }
1599 }
9b20d036 1600 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1601 c->name);
5a2468f5
JM
1602}
1603
d471ea57
AC
1604/* Should we try one of the 'Z' requests? */
1605
1606enum Z_packet_type
1607{
1608 Z_PACKET_SOFTWARE_BP,
1609 Z_PACKET_HARDWARE_BP,
1610 Z_PACKET_WRITE_WP,
1611 Z_PACKET_READ_WP,
1612 Z_PACKET_ACCESS_WP,
1613 NR_Z_PACKET_TYPES
1614};
96baa820 1615
d471ea57 1616/* For compatibility with older distributions. Provide a ``set remote
23860348 1617 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1618
7f19b9a2 1619static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1620
1621static void
fba45db2
KB
1622set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1623 struct cmd_list_element *c)
96baa820 1624{
d471ea57 1625 int i;
a744cf53 1626
d471ea57 1627 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1628 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1629}
1630
1631static void
08546159
AC
1632show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1633 struct cmd_list_element *c,
1634 const char *value)
96baa820 1635{
d471ea57 1636 int i;
a744cf53 1637
d471ea57
AC
1638 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1639 {
444abaca 1640 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1641 }
96baa820
JM
1642}
1643
4082afcc
PA
1644/* Returns true if the multi-process extensions are in effect. */
1645
1646static int
1647remote_multi_process_p (struct remote_state *rs)
1648{
1649 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1650}
1651
de0d863e
DB
1652/* Returns true if fork events are supported. */
1653
1654static int
1655remote_fork_event_p (struct remote_state *rs)
1656{
1657 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1658}
1659
c269dbdb
DB
1660/* Returns true if vfork events are supported. */
1661
1662static int
1663remote_vfork_event_p (struct remote_state *rs)
1664{
1665 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1666}
1667
d46addbb
DB
1668/* Returns true if exec events are supported. */
1669
1670static int
1671remote_exec_event_p (struct remote_state *rs)
1672{
1673 return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
1674}
1675
cbb8991c
DB
1676/* Insert fork catchpoint target routine. If fork events are enabled
1677 then return success, nothing more to do. */
1678
1679static int
1680remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1681{
1682 struct remote_state *rs = get_remote_state ();
1683
1684 return !remote_fork_event_p (rs);
1685}
1686
1687/* Remove fork catchpoint target routine. Nothing to do, just
1688 return success. */
1689
1690static int
1691remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1692{
1693 return 0;
1694}
1695
1696/* Insert vfork catchpoint target routine. If vfork events are enabled
1697 then return success, nothing more to do. */
1698
1699static int
1700remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1701{
1702 struct remote_state *rs = get_remote_state ();
1703
1704 return !remote_vfork_event_p (rs);
1705}
1706
1707/* Remove vfork catchpoint target routine. Nothing to do, just
1708 return success. */
1709
1710static int
1711remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1712{
1713 return 0;
1714}
1715
d46addbb
DB
1716/* Insert exec catchpoint target routine. If exec events are
1717 enabled, just return success. */
1718
1719static int
1720remote_insert_exec_catchpoint (struct target_ops *ops, int pid)
1721{
1722 struct remote_state *rs = get_remote_state ();
1723
1724 return !remote_exec_event_p (rs);
1725}
1726
1727/* Remove exec catchpoint target routine. Nothing to do, just
1728 return success. */
1729
1730static int
1731remote_remove_exec_catchpoint (struct target_ops *ops, int pid)
1732{
1733 return 0;
1734}
1735
74531fed
PA
1736\f
1737/* Asynchronous signal handle registered as event loop source for
1738 when we have pending events ready to be passed to the core. */
1739
1740static struct async_event_handler *remote_async_inferior_event_token;
1741
c906108c
SS
1742\f
1743
79d7f229
PA
1744static ptid_t magic_null_ptid;
1745static ptid_t not_sent_ptid;
1746static ptid_t any_thread_ptid;
1747
0b16c5cf
PA
1748/* Find out if the stub attached to PID (and hence GDB should offer to
1749 detach instead of killing it when bailing out). */
1750
1751static int
1752remote_query_attached (int pid)
1753{
1754 struct remote_state *rs = get_remote_state ();
bba74b36 1755 size_t size = get_remote_packet_size ();
0b16c5cf 1756
4082afcc 1757 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1758 return 0;
1759
1760 if (remote_multi_process_p (rs))
bba74b36 1761 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1762 else
bba74b36 1763 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1764
1765 putpkt (rs->buf);
1766 getpkt (&rs->buf, &rs->buf_size, 0);
1767
1768 switch (packet_ok (rs->buf,
1554e9be 1769 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1770 {
1771 case PACKET_OK:
1772 if (strcmp (rs->buf, "1") == 0)
1773 return 1;
1774 break;
1775 case PACKET_ERROR:
1776 warning (_("Remote failure reply: %s"), rs->buf);
1777 break;
1778 case PACKET_UNKNOWN:
1779 break;
1780 }
1781
1782 return 0;
1783}
1784
49c62f2e
PA
1785/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1786 has been invented by GDB, instead of reported by the target. Since
1787 we can be connected to a remote system before before knowing about
1788 any inferior, mark the target with execution when we find the first
1789 inferior. If ATTACHED is 1, then we had just attached to this
1790 inferior. If it is 0, then we just created this inferior. If it
1791 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1792 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1793 attempt to open this inferior's executable as the main executable
1794 if no main executable is open already. */
1941c569
PA
1795
1796static struct inferior *
1b6e6f5c
GB
1797remote_add_inferior (int fake_pid_p, int pid, int attached,
1798 int try_open_exec)
1941c569 1799{
1941c569
PA
1800 struct inferior *inf;
1801
0b16c5cf
PA
1802 /* Check whether this process we're learning about is to be
1803 considered attached, or if is to be considered to have been
1804 spawned by the stub. */
1805 if (attached == -1)
1806 attached = remote_query_attached (pid);
1807
f5656ead 1808 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1809 {
1810 /* If the target shares code across all inferiors, then every
1811 attach adds a new inferior. */
1812 inf = add_inferior (pid);
1813
1814 /* ... and every inferior is bound to the same program space.
1815 However, each inferior may still have its own address
1816 space. */
1817 inf->aspace = maybe_new_address_space ();
1818 inf->pspace = current_program_space;
1819 }
1820 else
1821 {
1822 /* In the traditional debugging scenario, there's a 1-1 match
1823 between program/address spaces. We simply bind the inferior
1824 to the program space's address space. */
1825 inf = current_inferior ();
1826 inferior_appeared (inf, pid);
1827 }
1941c569 1828
0b16c5cf 1829 inf->attach_flag = attached;
49c62f2e 1830 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1831
1b6e6f5c
GB
1832 /* If no main executable is currently open then attempt to
1833 open the file that was executed to create this inferior. */
835205d0 1834 if (try_open_exec && get_exec_file (0) == NULL)
bb805577 1835 exec_file_locate_attach (pid, 0, 1);
1b6e6f5c 1836
1941c569
PA
1837 return inf;
1838}
1839
85ad3aaf
PA
1840static struct private_thread_info *
1841 get_private_info_thread (struct thread_info *info);
1842
1941c569
PA
1843/* Add thread PTID to GDB's thread list. Tag it as executing/running
1844 according to RUNNING. */
1845
c906108c 1846static void
0d5b594f 1847remote_add_thread (ptid_t ptid, int running, int executing)
c906108c 1848{
b7ea362b 1849 struct remote_state *rs = get_remote_state ();
85ad3aaf 1850 struct thread_info *thread;
b7ea362b
PA
1851
1852 /* GDB historically didn't pull threads in the initial connection
1853 setup. If the remote target doesn't even have a concept of
1854 threads (e.g., a bare-metal target), even if internally we
1855 consider that a single-threaded target, mentioning a new thread
1856 might be confusing to the user. Be silent then, preserving the
1857 age old behavior. */
1858 if (rs->starting_up)
85ad3aaf 1859 thread = add_thread_silent (ptid);
b7ea362b 1860 else
85ad3aaf 1861 thread = add_thread (ptid);
1941c569 1862
85ad3aaf 1863 get_private_info_thread (thread)->vcont_resumed = executing;
0d5b594f 1864 set_executing (ptid, executing);
1941c569
PA
1865 set_running (ptid, running);
1866}
1867
1868/* Come here when we learn about a thread id from the remote target.
1869 It may be the first time we hear about such thread, so take the
1870 opportunity to add it to GDB's thread list. In case this is the
1871 first time we're noticing its corresponding inferior, add it to
0d5b594f
PA
1872 GDB's inferior list as well. EXECUTING indicates whether the
1873 thread is (internally) executing or stopped. */
1941c569
PA
1874
1875static void
0d5b594f 1876remote_notice_new_inferior (ptid_t currthread, int executing)
1941c569 1877{
0d5b594f
PA
1878 /* In non-stop mode, we assume new found threads are (externally)
1879 running until proven otherwise with a stop reply. In all-stop,
1880 we can only get here if all threads are stopped. */
1881 int running = target_is_non_stop_p () ? 1 : 0;
1882
c906108c
SS
1883 /* If this is a new thread, add it to GDB's thread list.
1884 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1885
1886 if (in_thread_list (currthread) && is_exited (currthread))
1887 {
1888 /* We're seeing an event on a thread id we knew had exited.
1889 This has to be a new thread reusing the old id. Add it. */
0d5b594f 1890 remote_add_thread (currthread, running, executing);
82f73884
PA
1891 return;
1892 }
1893
79d7f229 1894 if (!in_thread_list (currthread))
c0a2216e 1895 {
1941c569 1896 struct inferior *inf = NULL;
bad34192 1897 int pid = ptid_get_pid (currthread);
1941c569 1898
bad34192
PA
1899 if (ptid_is_pid (inferior_ptid)
1900 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1901 {
1902 /* inferior_ptid has no thread member yet. This can happen
1903 with the vAttach -> remote_wait,"TAAthread:" path if the
1904 stub doesn't support qC. This is the first stop reported
1905 after an attach, so this is the main thread. Update the
1906 ptid in the thread list. */
bad34192
PA
1907 if (in_thread_list (pid_to_ptid (pid)))
1908 thread_change_ptid (inferior_ptid, currthread);
1909 else
1910 {
0d5b594f 1911 remote_add_thread (currthread, running, executing);
bad34192
PA
1912 inferior_ptid = currthread;
1913 }
dc146f7c 1914 return;
c0a2216e 1915 }
82f73884
PA
1916
1917 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1918 {
1919 /* inferior_ptid is not set yet. This can happen with the
1920 vRun -> remote_wait,"TAAthread:" path if the stub
1921 doesn't support qC. This is the first stop reported
1922 after an attach, so this is the main thread. Update the
1923 ptid in the thread list. */
dc146f7c 1924 thread_change_ptid (inferior_ptid, currthread);
82f73884 1925 return;
c0a2216e 1926 }
82f73884 1927
29c87f7f
PA
1928 /* When connecting to a target remote, or to a target
1929 extended-remote which already was debugging an inferior, we
1930 may not know about it yet. Add it before adding its child
1931 thread, so notifications are emitted in a sensible order. */
1932 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1933 {
1934 struct remote_state *rs = get_remote_state ();
1935 int fake_pid_p = !remote_multi_process_p (rs);
1936
1937 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1938 ptid_get_pid (currthread), -1, 1);
49c62f2e 1939 }
29c87f7f 1940
82f73884 1941 /* This is really a new thread. Add it. */
0d5b594f 1942 remote_add_thread (currthread, running, executing);
1941c569
PA
1943
1944 /* If we found a new inferior, let the common code do whatever
1945 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1946 breakpoints), unless we're just setting up an all-stop
1947 connection. */
1941c569 1948 if (inf != NULL)
b7ea362b
PA
1949 {
1950 struct remote_state *rs = get_remote_state ();
1951
6efcd9a8 1952 if (!rs->starting_up)
0d5b594f 1953 notice_new_inferior (currthread, executing, 0);
b7ea362b 1954 }
c0a2216e 1955 }
c906108c
SS
1956}
1957
85ad3aaf 1958/* Return THREAD's private thread data, creating it if necessary. */
dc146f7c 1959
70221824 1960static struct private_thread_info *
85ad3aaf 1961get_private_info_thread (struct thread_info *thread)
dc146f7c 1962{
85ad3aaf 1963 gdb_assert (thread != NULL);
dc146f7c 1964
85ad3aaf 1965 if (thread->priv == NULL)
dc146f7c 1966 {
85ad3aaf
PA
1967 struct private_thread_info *priv = XNEW (struct private_thread_info);
1968
1969 thread->private_dtor = free_private_thread_info;
1970 thread->priv = priv;
1971
1972 priv->core = -1;
1973 priv->extra = NULL;
1974 priv->name = NULL;
1975 priv->name = NULL;
1976 priv->last_resume_step = 0;
1977 priv->last_resume_sig = GDB_SIGNAL_0;
1978 priv->vcont_resumed = 0;
f6327dcb 1979 priv->thread_handle = nullptr;
dc146f7c
VP
1980 }
1981
85ad3aaf
PA
1982 return thread->priv;
1983}
1984
1985/* Return PTID's private thread data, creating it if necessary. */
1986
1987static struct private_thread_info *
1988get_private_info_ptid (ptid_t ptid)
1989{
1990 struct thread_info *info = find_thread_ptid (ptid);
1991
1992 return get_private_info_thread (info);
dc146f7c
VP
1993}
1994
74531fed
PA
1995/* Call this function as a result of
1996 1) A halt indication (T packet) containing a thread id
1997 2) A direct query of currthread
0df8b418 1998 3) Successful execution of set thread */
74531fed
PA
1999
2000static void
47f8a51d 2001record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 2002{
47f8a51d 2003 rs->general_thread = currthread;
74531fed
PA
2004}
2005
89be2091
DJ
2006/* If 'QPassSignals' is supported, tell the remote stub what signals
2007 it can simply pass through to the inferior without reporting. */
2008
2009static void
94bedb42
TT
2010remote_pass_signals (struct target_ops *self,
2011 int numsigs, unsigned char *pass_signals)
89be2091 2012{
4082afcc 2013 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
2014 {
2015 char *pass_packet, *p;
89be2091 2016 int count = 0, i;
747dc59d 2017 struct remote_state *rs = get_remote_state ();
89be2091
DJ
2018
2019 gdb_assert (numsigs < 256);
2020 for (i = 0; i < numsigs; i++)
2021 {
2455069d 2022 if (pass_signals[i])
89be2091
DJ
2023 count++;
2024 }
224c3ddb 2025 pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
89be2091
DJ
2026 strcpy (pass_packet, "QPassSignals:");
2027 p = pass_packet + strlen (pass_packet);
2028 for (i = 0; i < numsigs; i++)
2029 {
2455069d 2030 if (pass_signals[i])
89be2091
DJ
2031 {
2032 if (i >= 16)
2033 *p++ = tohex (i >> 4);
2034 *p++ = tohex (i & 15);
2035 if (count)
2036 *p++ = ';';
2037 else
2038 break;
2039 count--;
2040 }
2041 }
2042 *p = 0;
747dc59d 2043 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 2044 {
89be2091
DJ
2045 putpkt (pass_packet);
2046 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2047 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
2048 if (rs->last_pass_packet)
2049 xfree (rs->last_pass_packet);
2050 rs->last_pass_packet = pass_packet;
89be2091
DJ
2051 }
2052 else
2053 xfree (pass_packet);
2054 }
2055}
2056
82075af2
JS
2057/* If 'QCatchSyscalls' is supported, tell the remote stub
2058 to report syscalls to GDB. */
2059
2060static int
2061remote_set_syscall_catchpoint (struct target_ops *self,
2062 int pid, int needed, int any_count,
2063 int table_size, int *table)
2064{
2065 char *catch_packet;
2066 enum packet_result result;
2067 int n_sysno = 0;
2068
2069 if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
2070 {
2071 /* Not supported. */
2072 return 1;
2073 }
2074
2075 if (needed && !any_count)
2076 {
2077 int i;
2078
2079 /* Count how many syscalls are to be caught (table[sysno] != 0). */
2080 for (i = 0; i < table_size; i++)
2081 {
2082 if (table[i] != 0)
2083 n_sysno++;
2084 }
2085 }
2086
2087 if (remote_debug)
2088 {
2089 fprintf_unfiltered (gdb_stdlog,
2090 "remote_set_syscall_catchpoint "
2091 "pid %d needed %d any_count %d n_sysno %d\n",
2092 pid, needed, any_count, n_sysno);
2093 }
2094
2095 if (needed)
2096 {
2097 /* Prepare a packet with the sysno list, assuming max 8+1
2098 characters for a sysno. If the resulting packet size is too
2099 big, fallback on the non-selective packet. */
2100 const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
2101
c0518081 2102 catch_packet = (char *) xmalloc (maxpktsz);
82075af2
JS
2103 strcpy (catch_packet, "QCatchSyscalls:1");
2104 if (!any_count)
2105 {
2106 int i;
2107 char *p;
2108
2109 p = catch_packet;
2110 p += strlen (p);
2111
2112 /* Add in catch_packet each syscall to be caught (table[i] != 0). */
2113 for (i = 0; i < table_size; i++)
2114 {
2115 if (table[i] != 0)
2116 p += xsnprintf (p, catch_packet + maxpktsz - p, ";%x", i);
2117 }
2118 }
2119 if (strlen (catch_packet) > get_remote_packet_size ())
2120 {
2121 /* catch_packet too big. Fallback to less efficient
2122 non selective mode, with GDB doing the filtering. */
2123 catch_packet[sizeof ("QCatchSyscalls:1") - 1] = 0;
2124 }
2125 }
2126 else
2127 catch_packet = xstrdup ("QCatchSyscalls:0");
2128
2129 {
2130 struct cleanup *old_chain = make_cleanup (xfree, catch_packet);
2131 struct remote_state *rs = get_remote_state ();
2132
2133 putpkt (catch_packet);
2134 getpkt (&rs->buf, &rs->buf_size, 0);
2135 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
2136 do_cleanups (old_chain);
2137 if (result == PACKET_OK)
2138 return 0;
2139 else
2140 return -1;
2141 }
2142}
2143
9b224c5e
PA
2144/* If 'QProgramSignals' is supported, tell the remote stub what
2145 signals it should pass through to the inferior when detaching. */
2146
2147static void
daf5e9b6
TT
2148remote_program_signals (struct target_ops *self,
2149 int numsigs, unsigned char *signals)
9b224c5e 2150{
4082afcc 2151 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
2152 {
2153 char *packet, *p;
2154 int count = 0, i;
5e4a05c4 2155 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
2156
2157 gdb_assert (numsigs < 256);
2158 for (i = 0; i < numsigs; i++)
2159 {
2160 if (signals[i])
2161 count++;
2162 }
224c3ddb 2163 packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
9b224c5e
PA
2164 strcpy (packet, "QProgramSignals:");
2165 p = packet + strlen (packet);
2166 for (i = 0; i < numsigs; i++)
2167 {
2168 if (signal_pass_state (i))
2169 {
2170 if (i >= 16)
2171 *p++ = tohex (i >> 4);
2172 *p++ = tohex (i & 15);
2173 if (count)
2174 *p++ = ';';
2175 else
2176 break;
2177 count--;
2178 }
2179 }
2180 *p = 0;
5e4a05c4
TT
2181 if (!rs->last_program_signals_packet
2182 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 2183 {
9b224c5e
PA
2184 putpkt (packet);
2185 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2186 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
2187 xfree (rs->last_program_signals_packet);
2188 rs->last_program_signals_packet = packet;
9b224c5e
PA
2189 }
2190 else
2191 xfree (packet);
2192 }
2193}
2194
79d7f229
PA
2195/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2196 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2197 thread. If GEN is set, set the general thread, if not, then set
2198 the step/continue thread. */
c906108c 2199static void
d62a8ae2 2200set_thread (ptid_t ptid, int gen)
c906108c 2201{
d01949b6 2202 struct remote_state *rs = get_remote_state ();
47f8a51d 2203 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 2204 char *buf = rs->buf;
79d7f229 2205 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 2206
79d7f229 2207 if (ptid_equal (state, ptid))
c906108c
SS
2208 return;
2209
79d7f229
PA
2210 *buf++ = 'H';
2211 *buf++ = gen ? 'g' : 'c';
2212 if (ptid_equal (ptid, magic_null_ptid))
2213 xsnprintf (buf, endbuf - buf, "0");
2214 else if (ptid_equal (ptid, any_thread_ptid))
2215 xsnprintf (buf, endbuf - buf, "0");
2216 else if (ptid_equal (ptid, minus_one_ptid))
2217 xsnprintf (buf, endbuf - buf, "-1");
2218 else
82f73884 2219 write_ptid (buf, endbuf, ptid);
79d7f229 2220 putpkt (rs->buf);
6d820c5c 2221 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2222 if (gen)
47f8a51d 2223 rs->general_thread = ptid;
c906108c 2224 else
47f8a51d 2225 rs->continue_thread = ptid;
c906108c 2226}
79d7f229
PA
2227
2228static void
d62a8ae2 2229set_general_thread (ptid_t ptid)
79d7f229
PA
2230{
2231 set_thread (ptid, 1);
2232}
2233
2234static void
d62a8ae2 2235set_continue_thread (ptid_t ptid)
79d7f229
PA
2236{
2237 set_thread (ptid, 0);
2238}
2239
3c9c4b83
PA
2240/* Change the remote current process. Which thread within the process
2241 ends up selected isn't important, as long as it is the same process
2242 as what INFERIOR_PTID points to.
2243
2244 This comes from that fact that there is no explicit notion of
2245 "selected process" in the protocol. The selected process for
2246 general operations is the process the selected general thread
2247 belongs to. */
2248
2249static void
2250set_general_process (void)
2251{
2252 struct remote_state *rs = get_remote_state ();
2253
2254 /* If the remote can't handle multiple processes, don't bother. */
8020350c 2255 if (!remote_multi_process_p (rs))
3c9c4b83
PA
2256 return;
2257
2258 /* We only need to change the remote current thread if it's pointing
2259 at some other process. */
47f8a51d 2260 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2261 set_general_thread (inferior_ptid);
2262}
2263
c906108c 2264\f
7d1a114c
PA
2265/* Return nonzero if this is the main thread that we made up ourselves
2266 to model non-threaded targets as single-threaded. */
c906108c
SS
2267
2268static int
7d1a114c 2269remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 2270{
c0a2216e
PA
2271 if (ptid_equal (ptid, magic_null_ptid))
2272 /* The main thread is always alive. */
2273 return 1;
2274
ba348170 2275 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2276 /* The main thread is always alive. This can happen after a
2277 vAttach, if the remote side doesn't support
2278 multi-threading. */
2279 return 1;
2280
7d1a114c
PA
2281 return 0;
2282}
2283
2284/* Return nonzero if the thread PTID is still alive on the remote
2285 system. */
2286
2287static int
2288remote_thread_alive (struct target_ops *ops, ptid_t ptid)
2289{
2290 struct remote_state *rs = get_remote_state ();
2291 char *p, *endp;
2292
2293 /* Check if this is a thread that we made up ourselves to model
2294 non-threaded targets as single-threaded. */
2295 if (remote_thread_always_alive (ops, ptid))
2296 return 1;
2297
82f73884
PA
2298 p = rs->buf;
2299 endp = rs->buf + get_remote_packet_size ();
2300
2301 *p++ = 'T';
2302 write_ptid (p, endp, ptid);
2303
2e9f7625 2304 putpkt (rs->buf);
6d820c5c 2305 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2306 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2307}
2308
79efa585
SM
2309/* Return a pointer to a thread name if we know it and NULL otherwise.
2310 The thread_info object owns the memory for the name. */
2311
2312static const char *
2313remote_thread_name (struct target_ops *ops, struct thread_info *info)
2314{
2315 if (info->priv != NULL)
2316 return info->priv->name;
2317
2318 return NULL;
2319}
2320
c906108c
SS
2321/* About these extended threadlist and threadinfo packets. They are
2322 variable length packets but, the fields within them are often fixed
2323 length. They are redundent enough to send over UDP as is the
2324 remote protocol in general. There is a matching unit test module
2325 in libstub. */
2326
23860348 2327/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2328 libstub protocol encoding, and remote.c. It is not particularly
23860348 2329 changable. */
cce74817
JM
2330
2331/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2332 Plan to fix this. */
cce74817 2333
23860348 2334typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2335
9d1f7ab2 2336/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2337 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2338
2339struct gdb_ext_thread_info
c5aa993b 2340 {
23860348 2341 threadref threadid; /* External form of thread reference. */
2bc416ba 2342 int active; /* Has state interesting to GDB?
23860348 2343 regs, stack. */
2bc416ba 2344 char display[256]; /* Brief state display, name,
cedea757 2345 blocked/suspended. */
23860348 2346 char shortname[32]; /* To be used to name threads. */
2bc416ba 2347 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2348 whatever. */
c5aa993b 2349 };
cce74817
JM
2350
2351/* The volume of remote transfers can be limited by submitting
2352 a mask containing bits specifying the desired information.
2353 Use a union of these values as the 'selection' parameter to
0df8b418 2354 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2355
2356#define TAG_THREADID 1
2357#define TAG_EXISTS 2
2358#define TAG_DISPLAY 4
2359#define TAG_THREADNAME 8
c5aa993b 2360#define TAG_MOREDISPLAY 16
cce74817 2361
23860348 2362#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2363
a14ed312 2364static char *unpack_nibble (char *buf, int *val);
cce74817 2365
a14ed312 2366static char *unpack_byte (char *buf, int *value);
cce74817 2367
a14ed312 2368static char *pack_int (char *buf, int value);
cce74817 2369
a14ed312 2370static char *unpack_int (char *buf, int *value);
cce74817 2371
a14ed312 2372static char *unpack_string (char *src, char *dest, int length);
cce74817 2373
23860348 2374static char *pack_threadid (char *pkt, threadref *id);
cce74817 2375
23860348 2376static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2377
23860348 2378void int_to_threadref (threadref *id, int value);
cce74817 2379
23860348 2380static int threadref_to_int (threadref *ref);
cce74817 2381
23860348 2382static void copy_threadref (threadref *dest, threadref *src);
cce74817 2383
23860348 2384static int threadmatch (threadref *dest, threadref *src);
cce74817 2385
2bc416ba 2386static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2387 threadref *id);
cce74817 2388
a14ed312 2389static int remote_unpack_thread_info_response (char *pkt,
23860348 2390 threadref *expectedref,
a14ed312
KB
2391 struct gdb_ext_thread_info
2392 *info);
cce74817
JM
2393
2394
2bc416ba 2395static int remote_get_threadinfo (threadref *threadid,
23860348 2396 int fieldset, /*TAG mask */
a14ed312 2397 struct gdb_ext_thread_info *info);
cce74817 2398
a14ed312
KB
2399static char *pack_threadlist_request (char *pkt, int startflag,
2400 int threadcount,
23860348 2401 threadref *nextthread);
cce74817 2402
a14ed312
KB
2403static int parse_threadlist_response (char *pkt,
2404 int result_limit,
23860348 2405 threadref *original_echo,
2bc416ba 2406 threadref *resultlist,
23860348 2407 int *doneflag);
cce74817 2408
a14ed312 2409static int remote_get_threadlist (int startflag,
23860348 2410 threadref *nextthread,
a14ed312
KB
2411 int result_limit,
2412 int *done,
2bc416ba 2413 int *result_count,
23860348 2414 threadref *threadlist);
cce74817 2415
23860348 2416typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2417
a14ed312
KB
2418static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2419 void *context, int looplimit);
cce74817 2420
23860348 2421static int remote_newthread_step (threadref *ref, void *context);
cce74817 2422
82f73884
PA
2423
2424/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2425 buffer we're allowed to write to. Returns
2426 BUF+CHARACTERS_WRITTEN. */
2427
2428static char *
2429write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2430{
2431 int pid, tid;
2432 struct remote_state *rs = get_remote_state ();
2433
2434 if (remote_multi_process_p (rs))
2435 {
2436 pid = ptid_get_pid (ptid);
2437 if (pid < 0)
2438 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2439 else
2440 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2441 }
ba348170 2442 tid = ptid_get_lwp (ptid);
82f73884
PA
2443 if (tid < 0)
2444 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2445 else
2446 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2447
2448 return buf;
2449}
2450
256642e8
PA
2451/* Extract a PTID from BUF. If non-null, OBUF is set to one past the
2452 last parsed char. Returns null_ptid if no thread id is found, and
2453 throws an error if the thread id has an invalid format. */
82f73884
PA
2454
2455static ptid_t
256642e8 2456read_ptid (const char *buf, const char **obuf)
82f73884 2457{
256642e8
PA
2458 const char *p = buf;
2459 const char *pp;
82f73884 2460 ULONGEST pid = 0, tid = 0;
82f73884
PA
2461
2462 if (*p == 'p')
2463 {
2464 /* Multi-process ptid. */
2465 pp = unpack_varlen_hex (p + 1, &pid);
2466 if (*pp != '.')
b37520b6 2467 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2468
2469 p = pp;
2470 pp = unpack_varlen_hex (p + 1, &tid);
2471 if (obuf)
2472 *obuf = pp;
ba348170 2473 return ptid_build (pid, tid, 0);
82f73884
PA
2474 }
2475
2476 /* No multi-process. Just a tid. */
2477 pp = unpack_varlen_hex (p, &tid);
2478
c9f35b34
KB
2479 /* Return null_ptid when no thread id is found. */
2480 if (p == pp)
2481 {
2482 if (obuf)
2483 *obuf = pp;
2484 return null_ptid;
2485 }
2486
82f73884 2487 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2488 what's in inferior_ptid, unless it's null at this point. If so,
2489 then since there's no way to know the pid of the reported
2490 threads, use the magic number. */
2491 if (ptid_equal (inferior_ptid, null_ptid))
2492 pid = ptid_get_pid (magic_null_ptid);
2493 else
2494 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2495
2496 if (obuf)
2497 *obuf = pp;
ba348170 2498 return ptid_build (pid, tid, 0);
82f73884
PA
2499}
2500
c906108c 2501static int
fba45db2 2502stubhex (int ch)
c906108c
SS
2503{
2504 if (ch >= 'a' && ch <= 'f')
2505 return ch - 'a' + 10;
2506 if (ch >= '0' && ch <= '9')
2507 return ch - '0';
2508 if (ch >= 'A' && ch <= 'F')
2509 return ch - 'A' + 10;
2510 return -1;
2511}
2512
2513static int
fba45db2 2514stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2515{
2516 int nibble;
2517 int retval = 0;
2518
2519 while (fieldlength)
2520 {
2521 nibble = stubhex (*buff++);
2522 retval |= nibble;
2523 fieldlength--;
2524 if (fieldlength)
2525 retval = retval << 4;
2526 }
2527 return retval;
2528}
2529
c906108c 2530static char *
fba45db2 2531unpack_nibble (char *buf, int *val)
c906108c 2532{
b7589f7d 2533 *val = fromhex (*buf++);
c906108c
SS
2534 return buf;
2535}
2536
c906108c 2537static char *
fba45db2 2538unpack_byte (char *buf, int *value)
c906108c
SS
2539{
2540 *value = stub_unpack_int (buf, 2);
2541 return buf + 2;
2542}
2543
2544static char *
fba45db2 2545pack_int (char *buf, int value)
c906108c
SS
2546{
2547 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2548 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2549 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2550 buf = pack_hex_byte (buf, (value & 0xff));
2551 return buf;
2552}
2553
2554static char *
fba45db2 2555unpack_int (char *buf, int *value)
c906108c
SS
2556{
2557 *value = stub_unpack_int (buf, 8);
2558 return buf + 8;
2559}
2560
23860348 2561#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2562static char *pack_string (char *pkt, char *string);
c906108c
SS
2563
2564static char *
fba45db2 2565pack_string (char *pkt, char *string)
c906108c
SS
2566{
2567 char ch;
2568 int len;
2569
2570 len = strlen (string);
2571 if (len > 200)
23860348 2572 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2573 pkt = pack_hex_byte (pkt, len);
2574 while (len-- > 0)
2575 {
2576 ch = *string++;
2577 if ((ch == '\0') || (ch == '#'))
23860348 2578 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2579 *pkt++ = ch;
2580 }
2581 return pkt;
2582}
2583#endif /* 0 (unused) */
2584
2585static char *
fba45db2 2586unpack_string (char *src, char *dest, int length)
c906108c
SS
2587{
2588 while (length--)
2589 *dest++ = *src++;
2590 *dest = '\0';
2591 return src;
2592}
2593
2594static char *
fba45db2 2595pack_threadid (char *pkt, threadref *id)
c906108c
SS
2596{
2597 char *limit;
2598 unsigned char *altid;
2599
2600 altid = (unsigned char *) id;
2601 limit = pkt + BUF_THREAD_ID_SIZE;
2602 while (pkt < limit)
2603 pkt = pack_hex_byte (pkt, *altid++);
2604 return pkt;
2605}
2606
2607
2608static char *
fba45db2 2609unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2610{
2611 char *altref;
2612 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2613 int x, y;
2614
2615 altref = (char *) id;
2616
2617 while (inbuf < limit)
2618 {
2619 x = stubhex (*inbuf++);
2620 y = stubhex (*inbuf++);
2621 *altref++ = (x << 4) | y;
2622 }
2623 return inbuf;
2624}
2625
2626/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2627 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2628 to use 64bit thread references internally. This is an adapter
2629 function. */
2630
2631void
fba45db2 2632int_to_threadref (threadref *id, int value)
c906108c
SS
2633{
2634 unsigned char *scan;
2635
2636 scan = (unsigned char *) id;
2637 {
2638 int i = 4;
2639 while (i--)
2640 *scan++ = 0;
2641 }
2642 *scan++ = (value >> 24) & 0xff;
2643 *scan++ = (value >> 16) & 0xff;
2644 *scan++ = (value >> 8) & 0xff;
2645 *scan++ = (value & 0xff);
2646}
2647
2648static int
fba45db2 2649threadref_to_int (threadref *ref)
c906108c
SS
2650{
2651 int i, value = 0;
2652 unsigned char *scan;
2653
cfd77fa1 2654 scan = *ref;
c906108c
SS
2655 scan += 4;
2656 i = 4;
2657 while (i-- > 0)
2658 value = (value << 8) | ((*scan++) & 0xff);
2659 return value;
2660}
2661
2662static void
fba45db2 2663copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2664{
2665 int i;
2666 unsigned char *csrc, *cdest;
2667
2668 csrc = (unsigned char *) src;
2669 cdest = (unsigned char *) dest;
2670 i = 8;
2671 while (i--)
2672 *cdest++ = *csrc++;
2673}
2674
2675static int
fba45db2 2676threadmatch (threadref *dest, threadref *src)
c906108c 2677{
23860348 2678 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2679#if 0
2680 unsigned char *srcp, *destp;
2681 int i, result;
2682 srcp = (char *) src;
2683 destp = (char *) dest;
2684
2685 result = 1;
2686 while (i-- > 0)
2687 result &= (*srcp++ == *destp++) ? 1 : 0;
2688 return result;
2689#endif
2690 return 1;
2691}
2692
2693/*
c5aa993b
JM
2694 threadid:1, # always request threadid
2695 context_exists:2,
2696 display:4,
2697 unique_name:8,
2698 more_display:16
2699 */
c906108c
SS
2700
2701/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2702
2703static char *
fba45db2 2704pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2705{
23860348
MS
2706 *pkt++ = 'q'; /* Info Query */
2707 *pkt++ = 'P'; /* process or thread info */
2708 pkt = pack_int (pkt, mode); /* mode */
c906108c 2709 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2710 *pkt = '\0'; /* terminate */
c906108c
SS
2711 return pkt;
2712}
2713
23860348 2714/* These values tag the fields in a thread info response packet. */
c906108c 2715/* Tagging the fields allows us to request specific fields and to
23860348 2716 add more fields as time goes by. */
c906108c 2717
23860348 2718#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2719#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2720 fetch registers and its stack? */
c5aa993b 2721#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2722#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2723#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2724 the process. */
c906108c
SS
2725
2726static int
fba45db2
KB
2727remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2728 struct gdb_ext_thread_info *info)
c906108c 2729{
d01949b6 2730 struct remote_state *rs = get_remote_state ();
c906108c 2731 int mask, length;
cfd77fa1 2732 int tag;
c906108c 2733 threadref ref;
6d820c5c 2734 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2735 int retval = 1;
2736
23860348 2737 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2738 info->active = 0;
2739 info->display[0] = '\0';
2740 info->shortname[0] = '\0';
2741 info->more_display[0] = '\0';
2742
23860348
MS
2743 /* Assume the characters indicating the packet type have been
2744 stripped. */
c906108c
SS
2745 pkt = unpack_int (pkt, &mask); /* arg mask */
2746 pkt = unpack_threadid (pkt, &ref);
2747
2748 if (mask == 0)
8a3fe4f8 2749 warning (_("Incomplete response to threadinfo request."));
c906108c 2750 if (!threadmatch (&ref, expectedref))
23860348 2751 { /* This is an answer to a different request. */
8a3fe4f8 2752 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2753 return 0;
2754 }
2755 copy_threadref (&info->threadid, &ref);
2756
23860348 2757 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2758
23860348
MS
2759 /* Packets are terminated with nulls. */
2760 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2761 {
2762 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2763 pkt = unpack_byte (pkt, &length); /* length */
2764 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2765 {
8a3fe4f8 2766 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2767 retval = 0;
2768 break;
2769 }
2770 if (tag == TAG_THREADID)
2771 {
2772 if (length != 16)
2773 {
8a3fe4f8 2774 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2775 retval = 0;
2776 break;
2777 }
2778 pkt = unpack_threadid (pkt, &ref);
2779 mask = mask & ~TAG_THREADID;
2780 continue;
2781 }
2782 if (tag == TAG_EXISTS)
2783 {
2784 info->active = stub_unpack_int (pkt, length);
2785 pkt += length;
2786 mask = mask & ~(TAG_EXISTS);
2787 if (length > 8)
2788 {
8a3fe4f8 2789 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2790 retval = 0;
2791 break;
2792 }
2793 continue;
2794 }
2795 if (tag == TAG_THREADNAME)
2796 {
2797 pkt = unpack_string (pkt, &info->shortname[0], length);
2798 mask = mask & ~TAG_THREADNAME;
2799 continue;
2800 }
2801 if (tag == TAG_DISPLAY)
2802 {
2803 pkt = unpack_string (pkt, &info->display[0], length);
2804 mask = mask & ~TAG_DISPLAY;
2805 continue;
2806 }
2807 if (tag == TAG_MOREDISPLAY)
2808 {
2809 pkt = unpack_string (pkt, &info->more_display[0], length);
2810 mask = mask & ~TAG_MOREDISPLAY;
2811 continue;
2812 }
8a3fe4f8 2813 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2814 break; /* Not a tag we know about. */
c906108c
SS
2815 }
2816 return retval;
2817}
2818
2819static int
fba45db2
KB
2820remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2821 struct gdb_ext_thread_info *info)
c906108c 2822{
d01949b6 2823 struct remote_state *rs = get_remote_state ();
c906108c 2824 int result;
c906108c 2825
2e9f7625
DJ
2826 pack_threadinfo_request (rs->buf, fieldset, threadid);
2827 putpkt (rs->buf);
6d820c5c 2828 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2829
2830 if (rs->buf[0] == '\0')
2831 return 0;
2832
2e9f7625 2833 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2834 threadid, info);
c906108c
SS
2835 return result;
2836}
2837
c906108c
SS
2838/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2839
2840static char *
fba45db2
KB
2841pack_threadlist_request (char *pkt, int startflag, int threadcount,
2842 threadref *nextthread)
c906108c
SS
2843{
2844 *pkt++ = 'q'; /* info query packet */
2845 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2846 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2847 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2848 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2849 *pkt = '\0';
2850 return pkt;
2851}
2852
2853/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2854
2855static int
fba45db2
KB
2856parse_threadlist_response (char *pkt, int result_limit,
2857 threadref *original_echo, threadref *resultlist,
2858 int *doneflag)
c906108c 2859{
d01949b6 2860 struct remote_state *rs = get_remote_state ();
c906108c
SS
2861 char *limit;
2862 int count, resultcount, done;
2863
2864 resultcount = 0;
2865 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2866 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2867 /* done parse past here */
c906108c
SS
2868 pkt = unpack_byte (pkt, &count); /* count field */
2869 pkt = unpack_nibble (pkt, &done);
2870 /* The first threadid is the argument threadid. */
2871 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2872 while ((count-- > 0) && (pkt < limit))
2873 {
2874 pkt = unpack_threadid (pkt, resultlist++);
2875 if (resultcount++ >= result_limit)
2876 break;
2877 }
2878 if (doneflag)
2879 *doneflag = done;
2880 return resultcount;
2881}
2882
6dc54d91
PA
2883/* Fetch the next batch of threads from the remote. Returns -1 if the
2884 qL packet is not supported, 0 on error and 1 on success. */
2885
c906108c 2886static int
fba45db2
KB
2887remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2888 int *done, int *result_count, threadref *threadlist)
c906108c 2889{
d01949b6 2890 struct remote_state *rs = get_remote_state ();
c906108c
SS
2891 int result = 1;
2892
23860348 2893 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2894 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2895 >= get_remote_packet_size ())
ea9c271d 2896 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2897
6d820c5c
DJ
2898 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2899 putpkt (rs->buf);
2900 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2901 if (*rs->buf == '\0')
6dc54d91
PA
2902 {
2903 /* Packet not supported. */
2904 return -1;
2905 }
2906
2907 *result_count =
2908 parse_threadlist_response (rs->buf + 2, result_limit,
2909 &rs->echo_nextthread, threadlist, done);
c906108c 2910
0d031856 2911 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2912 {
23860348
MS
2913 /* FIXME: This is a good reason to drop the packet. */
2914 /* Possably, there is a duplicate response. */
c906108c
SS
2915 /* Possabilities :
2916 retransmit immediatly - race conditions
2917 retransmit after timeout - yes
2918 exit
2919 wait for packet, then exit
2920 */
8a3fe4f8 2921 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2922 return 0; /* I choose simply exiting. */
c906108c
SS
2923 }
2924 if (*result_count <= 0)
2925 {
2926 if (*done != 1)
2927 {
8a3fe4f8 2928 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2929 result = 0;
2930 }
2931 return result; /* break; */
2932 }
2933 if (*result_count > result_limit)
2934 {
2935 *result_count = 0;
8a3fe4f8 2936 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2937 return 0;
2938 }
2939 return result;
2940}
2941
6dc54d91
PA
2942/* Fetch the list of remote threads, with the qL packet, and call
2943 STEPFUNCTION for each thread found. Stops iterating and returns 1
2944 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2945 STEPFUNCTION returns false. If the packet is not supported,
2946 returns -1. */
c906108c 2947
c906108c 2948static int
fba45db2
KB
2949remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2950 int looplimit)
c906108c 2951{
0d031856 2952 struct remote_state *rs = get_remote_state ();
c906108c
SS
2953 int done, i, result_count;
2954 int startflag = 1;
2955 int result = 1;
2956 int loopcount = 0;
c906108c
SS
2957
2958 done = 0;
2959 while (!done)
2960 {
2961 if (loopcount++ > looplimit)
2962 {
2963 result = 0;
8a3fe4f8 2964 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2965 break;
2966 }
6dc54d91
PA
2967 result = remote_get_threadlist (startflag, &rs->nextthread,
2968 MAXTHREADLISTRESULTS,
2969 &done, &result_count,
2970 rs->resultthreadlist);
2971 if (result <= 0)
2972 break;
23860348 2973 /* Clear for later iterations. */
c906108c
SS
2974 startflag = 0;
2975 /* Setup to resume next batch of thread references, set nextthread. */
2976 if (result_count >= 1)
0d031856
TT
2977 copy_threadref (&rs->nextthread,
2978 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2979 i = 0;
2980 while (result_count--)
6dc54d91
PA
2981 {
2982 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2983 {
2984 result = 0;
2985 break;
2986 }
2987 }
c906108c
SS
2988 }
2989 return result;
2990}
2991
6dc54d91
PA
2992/* A thread found on the remote target. */
2993
2994typedef struct thread_item
2995{
2996 /* The thread's PTID. */
2997 ptid_t ptid;
2998
2999 /* The thread's extra info. May be NULL. */
3000 char *extra;
3001
79efa585
SM
3002 /* The thread's name. May be NULL. */
3003 char *name;
3004
6dc54d91
PA
3005 /* The core the thread was running on. -1 if not known. */
3006 int core;
f6327dcb
KB
3007
3008 /* The thread handle associated with the thread. */
3009 gdb::byte_vector *thread_handle;
3010
6dc54d91
PA
3011} thread_item_t;
3012DEF_VEC_O(thread_item_t);
3013
3014/* Context passed around to the various methods listing remote
3015 threads. As new threads are found, they're added to the ITEMS
3016 vector. */
3017
3018struct threads_listing_context
3019{
3020 /* The threads found on the remote target. */
3021 VEC (thread_item_t) *items;
3022};
3023
80134cf5
PA
3024/* Discard the contents of the constructed thread listing context. */
3025
3026static void
3027clear_threads_listing_context (void *p)
3028{
19ba03f4
SM
3029 struct threads_listing_context *context
3030 = (struct threads_listing_context *) p;
80134cf5
PA
3031 int i;
3032 struct thread_item *item;
3033
3034 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
79efa585
SM
3035 {
3036 xfree (item->extra);
3037 xfree (item->name);
f6327dcb 3038 delete item->thread_handle;
79efa585 3039 }
80134cf5
PA
3040
3041 VEC_free (thread_item_t, context->items);
3042}
3043
cbb8991c
DB
3044/* Remove the thread specified as the related_pid field of WS
3045 from the CONTEXT list. */
3046
3047static void
3048threads_listing_context_remove (struct target_waitstatus *ws,
3049 struct threads_listing_context *context)
3050{
3051 struct thread_item *item;
3052 int i;
3053 ptid_t child_ptid = ws->value.related_pid;
3054
3055 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
3056 {
3057 if (ptid_equal (item->ptid, child_ptid))
3058 {
3059 VEC_ordered_remove (thread_item_t, context->items, i);
3060 break;
3061 }
3062 }
3063}
3064
c906108c 3065static int
6dc54d91 3066remote_newthread_step (threadref *ref, void *data)
c906108c 3067{
19ba03f4
SM
3068 struct threads_listing_context *context
3069 = (struct threads_listing_context *) data;
6dc54d91 3070 struct thread_item item;
79d7f229 3071 int pid = ptid_get_pid (inferior_ptid);
39f77062 3072
6dc54d91
PA
3073 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
3074 item.core = -1;
2e3b657e 3075 item.name = NULL;
6dc54d91 3076 item.extra = NULL;
f6327dcb 3077 item.thread_handle = nullptr;
6dc54d91
PA
3078
3079 VEC_safe_push (thread_item_t, context->items, &item);
3080
c906108c
SS
3081 return 1; /* continue iterator */
3082}
3083
3084#define CRAZY_MAX_THREADS 1000
3085
39f77062
KB
3086static ptid_t
3087remote_current_thread (ptid_t oldpid)
c906108c 3088{
d01949b6 3089 struct remote_state *rs = get_remote_state ();
c906108c
SS
3090
3091 putpkt ("qC");
6d820c5c 3092 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3093 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34 3094 {
256642e8 3095 const char *obuf;
c9f35b34
KB
3096 ptid_t result;
3097
3098 result = read_ptid (&rs->buf[2], &obuf);
3099 if (*obuf != '\0' && remote_debug)
3100 fprintf_unfiltered (gdb_stdlog,
3101 "warning: garbage in qC reply\n");
3102
3103 return result;
3104 }
c906108c
SS
3105 else
3106 return oldpid;
3107}
3108
6dc54d91 3109/* List remote threads using the deprecated qL packet. */
cce74817 3110
6dc54d91
PA
3111static int
3112remote_get_threads_with_ql (struct target_ops *ops,
3113 struct threads_listing_context *context)
c906108c 3114{
6dc54d91
PA
3115 if (remote_threadlist_iterator (remote_newthread_step, context,
3116 CRAZY_MAX_THREADS) >= 0)
3117 return 1;
3118
3119 return 0;
c906108c
SS
3120}
3121
dc146f7c
VP
3122#if defined(HAVE_LIBEXPAT)
3123
dc146f7c
VP
3124static void
3125start_thread (struct gdb_xml_parser *parser,
3126 const struct gdb_xml_element *element,
3127 void *user_data, VEC(gdb_xml_value_s) *attributes)
3128{
19ba03f4
SM
3129 struct threads_listing_context *data
3130 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3131
3132 struct thread_item item;
3133 char *id;
3d2c1d41 3134 struct gdb_xml_value *attr;
dc146f7c 3135
19ba03f4 3136 id = (char *) xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
3137 item.ptid = read_ptid (id, NULL);
3138
3d2c1d41
PA
3139 attr = xml_find_attribute (attributes, "core");
3140 if (attr != NULL)
3141 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
3142 else
3143 item.core = -1;
3144
79efa585 3145 attr = xml_find_attribute (attributes, "name");
e1961661 3146 item.name = attr != NULL ? xstrdup ((const char *) attr->value) : NULL;
79efa585 3147
f6327dcb
KB
3148 attr = xml_find_attribute (attributes, "handle");
3149 if (attr != NULL)
3150 {
3151 item.thread_handle = new gdb::byte_vector
3152 (strlen ((const char *) attr->value) / 2);
3153 hex2bin ((const char *) attr->value, item.thread_handle->data (),
3154 item.thread_handle->size ());
3155 }
3156 else
3157 item.thread_handle = nullptr;
3158
dc146f7c
VP
3159 item.extra = 0;
3160
3161 VEC_safe_push (thread_item_t, data->items, &item);
3162}
3163
3164static void
3165end_thread (struct gdb_xml_parser *parser,
3166 const struct gdb_xml_element *element,
3167 void *user_data, const char *body_text)
3168{
19ba03f4
SM
3169 struct threads_listing_context *data
3170 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3171
3172 if (body_text && *body_text)
2ae2a0b7 3173 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
3174}
3175
3176const struct gdb_xml_attribute thread_attributes[] = {
3177 { "id", GDB_XML_AF_NONE, NULL, NULL },
3178 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
79efa585 3179 { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
f6327dcb 3180 { "handle", GDB_XML_AF_OPTIONAL, NULL, NULL },
dc146f7c
VP
3181 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3182};
3183
3184const struct gdb_xml_element thread_children[] = {
3185 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3186};
3187
3188const struct gdb_xml_element threads_children[] = {
3189 { "thread", thread_attributes, thread_children,
3190 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3191 start_thread, end_thread },
3192 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3193};
3194
3195const struct gdb_xml_element threads_elements[] = {
3196 { "threads", NULL, threads_children,
3197 GDB_XML_EF_NONE, NULL, NULL },
3198 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3199};
3200
3201#endif
3202
6dc54d91 3203/* List remote threads using qXfer:threads:read. */
9d1f7ab2 3204
6dc54d91
PA
3205static int
3206remote_get_threads_with_qxfer (struct target_ops *ops,
3207 struct threads_listing_context *context)
0f71a2f6 3208{
dc146f7c 3209#if defined(HAVE_LIBEXPAT)
4082afcc 3210 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 3211 {
6dc54d91 3212 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 3213 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 3214
6dc54d91 3215 if (xml != NULL && *xml != '\0')
dc146f7c 3216 {
6dc54d91
PA
3217 gdb_xml_parse_quick (_("threads"), "threads.dtd",
3218 threads_elements, xml, context);
dc146f7c
VP
3219 }
3220
3221 do_cleanups (back_to);
6dc54d91 3222 return 1;
dc146f7c
VP
3223 }
3224#endif
3225
6dc54d91
PA
3226 return 0;
3227}
3228
3229/* List remote threads using qfThreadInfo/qsThreadInfo. */
3230
3231static int
3232remote_get_threads_with_qthreadinfo (struct target_ops *ops,
3233 struct threads_listing_context *context)
3234{
3235 struct remote_state *rs = get_remote_state ();
3236
b80fafe3 3237 if (rs->use_threadinfo_query)
9d1f7ab2 3238 {
256642e8 3239 const char *bufp;
6dc54d91 3240
9d1f7ab2 3241 putpkt ("qfThreadInfo");
6d820c5c 3242 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3243 bufp = rs->buf;
9d1f7ab2 3244 if (bufp[0] != '\0') /* q packet recognized */
802188a7 3245 {
9d1f7ab2
MS
3246 while (*bufp++ == 'm') /* reply contains one or more TID */
3247 {
3248 do
3249 {
6dc54d91
PA
3250 struct thread_item item;
3251
3252 item.ptid = read_ptid (bufp, &bufp);
3253 item.core = -1;
2e3b657e 3254 item.name = NULL;
6dc54d91 3255 item.extra = NULL;
f6327dcb 3256 item.thread_handle = nullptr;
6dc54d91
PA
3257
3258 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
3259 }
3260 while (*bufp++ == ','); /* comma-separated list */
3261 putpkt ("qsThreadInfo");
6d820c5c 3262 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3263 bufp = rs->buf;
9d1f7ab2 3264 }
6dc54d91
PA
3265 return 1;
3266 }
3267 else
3268 {
3269 /* Packet not recognized. */
3270 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3271 }
3272 }
3273
6dc54d91
PA
3274 return 0;
3275}
3276
e8032dde 3277/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3278 targets. */
3279
3280static void
e8032dde 3281remote_update_thread_list (struct target_ops *ops)
6dc54d91 3282{
6dc54d91
PA
3283 struct threads_listing_context context;
3284 struct cleanup *old_chain;
ab970af1 3285 int got_list = 0;
e8032dde 3286
6dc54d91
PA
3287 context.items = NULL;
3288 old_chain = make_cleanup (clear_threads_listing_context, &context);
3289
3290 /* We have a few different mechanisms to fetch the thread list. Try
3291 them all, starting with the most preferred one first, falling
3292 back to older methods. */
3293 if (remote_get_threads_with_qxfer (ops, &context)
3294 || remote_get_threads_with_qthreadinfo (ops, &context)
3295 || remote_get_threads_with_ql (ops, &context))
3296 {
3297 int i;
3298 struct thread_item *item;
ab970af1
PA
3299 struct thread_info *tp, *tmp;
3300
3301 got_list = 1;
3302
7d1a114c
PA
3303 if (VEC_empty (thread_item_t, context.items)
3304 && remote_thread_always_alive (ops, inferior_ptid))
3305 {
3306 /* Some targets don't really support threads, but still
3307 reply an (empty) thread list in response to the thread
3308 listing packets, instead of replying "packet not
3309 supported". Exit early so we don't delete the main
3310 thread. */
3311 do_cleanups (old_chain);
3312 return;
3313 }
3314
ab970af1
PA
3315 /* CONTEXT now holds the current thread list on the remote
3316 target end. Delete GDB-side threads no longer found on the
3317 target. */
8a06aea7 3318 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3319 {
ab970af1
PA
3320 for (i = 0;
3321 VEC_iterate (thread_item_t, context.items, i, item);
3322 ++i)
3323 {
3324 if (ptid_equal (item->ptid, tp->ptid))
3325 break;
3326 }
3327
3328 if (i == VEC_length (thread_item_t, context.items))
3329 {
3330 /* Not found. */
3331 delete_thread (tp->ptid);
3332 }
cbb8991c
DB
3333 }
3334
3335 /* Remove any unreported fork child threads from CONTEXT so
3336 that we don't interfere with follow fork, which is where
3337 creation of such threads is handled. */
3338 remove_new_fork_children (&context);
74531fed 3339
ab970af1 3340 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
3341 for (i = 0;
3342 VEC_iterate (thread_item_t, context.items, i, item);
3343 ++i)
3344 {
3345 if (!ptid_equal (item->ptid, null_ptid))
3346 {
3347 struct private_thread_info *info;
3348 /* In non-stop mode, we assume new found threads are
0d5b594f
PA
3349 executing until proven otherwise with a stop reply.
3350 In all-stop, we can only get here if all threads are
6dc54d91 3351 stopped. */
0d5b594f 3352 int executing = target_is_non_stop_p () ? 1 : 0;
6dc54d91 3353
0d5b594f 3354 remote_notice_new_inferior (item->ptid, executing);
6dc54d91 3355
85ad3aaf 3356 info = get_private_info_ptid (item->ptid);
6dc54d91
PA
3357 info->core = item->core;
3358 info->extra = item->extra;
3359 item->extra = NULL;
79efa585
SM
3360 info->name = item->name;
3361 item->name = NULL;
f6327dcb
KB
3362 info->thread_handle = item->thread_handle;
3363 item->thread_handle = nullptr;
6dc54d91
PA
3364 }
3365 }
3366 }
3367
ab970af1
PA
3368 if (!got_list)
3369 {
3370 /* If no thread listing method is supported, then query whether
3371 each known thread is alive, one by one, with the T packet.
3372 If the target doesn't support threads at all, then this is a
3373 no-op. See remote_thread_alive. */
3374 prune_threads ();
3375 }
3376
6dc54d91 3377 do_cleanups (old_chain);
9d1f7ab2
MS
3378}
3379
802188a7 3380/*
9d1f7ab2
MS
3381 * Collect a descriptive string about the given thread.
3382 * The target may say anything it wants to about the thread
3383 * (typically info about its blocked / runnable state, name, etc.).
3384 * This string will appear in the info threads display.
802188a7 3385 *
9d1f7ab2
MS
3386 * Optional: targets are not required to implement this function.
3387 */
3388
7a114964 3389static const char *
c15906d8 3390remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3391{
d01949b6 3392 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3393 int result;
3394 int set;
3395 threadref id;
3396 struct gdb_ext_thread_info threadinfo;
23860348 3397 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3398 int n = 0; /* position in display_buf */
3399
5d93a237 3400 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3401 internal_error (__FILE__, __LINE__,
e2e0b3e5 3402 _("remote_threads_extra_info"));
9d1f7ab2 3403
60e569b9 3404 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3405 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3406 /* This is the main thread which was added by GDB. The remote
3407 server doesn't know about it. */
3408 return NULL;
3409
4082afcc 3410 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3411 {
3412 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3413
fe978cb0
PA
3414 if (info && info->priv)
3415 return info->priv->extra;
dc146f7c
VP
3416 else
3417 return NULL;
3418 }
3419
b80fafe3 3420 if (rs->use_threadextra_query)
9d1f7ab2 3421 {
82f73884
PA
3422 char *b = rs->buf;
3423 char *endb = rs->buf + get_remote_packet_size ();
3424
3425 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3426 b += strlen (b);
3427 write_ptid (b, endb, tp->ptid);
3428
2e9f7625 3429 putpkt (rs->buf);
6d820c5c 3430 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3431 if (rs->buf[0] != 0)
9d1f7ab2 3432 {
325fac50 3433 n = std::min (strlen (rs->buf) / 2, sizeof (display_buf));
2e9f7625 3434 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3435 display_buf [result] = '\0';
9d1f7ab2
MS
3436 return display_buf;
3437 }
0f71a2f6 3438 }
9d1f7ab2
MS
3439
3440 /* If the above query fails, fall back to the old method. */
b80fafe3 3441 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3442 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3443 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3444 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3445 if (remote_get_threadinfo (&id, set, &threadinfo))
3446 if (threadinfo.active)
0f71a2f6 3447 {
9d1f7ab2 3448 if (*threadinfo.shortname)
2bc416ba 3449 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3450 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3451 if (*threadinfo.display)
2bc416ba 3452 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3453 " State: %s,", threadinfo.display);
9d1f7ab2 3454 if (*threadinfo.more_display)
2bc416ba 3455 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3456 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3457
3458 if (n > 0)
c5aa993b 3459 {
23860348 3460 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3461 if (',' == display_buf[n-1])
3462 display_buf[n-1] = ' ';
3463 return display_buf;
c5aa993b 3464 }
0f71a2f6 3465 }
9d1f7ab2 3466 return NULL;
0f71a2f6 3467}
c906108c 3468\f
c5aa993b 3469
0fb4aa4b 3470static int
61fc905d 3471remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3472 struct static_tracepoint_marker *marker)
3473{
3474 struct remote_state *rs = get_remote_state ();
3475 char *p = rs->buf;
3476
bba74b36 3477 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3478 p += strlen (p);
3479 p += hexnumstr (p, addr);
3480 putpkt (rs->buf);
3481 getpkt (&rs->buf, &rs->buf_size, 0);
3482 p = rs->buf;
3483
3484 if (*p == 'E')
3485 error (_("Remote failure reply: %s"), p);
3486
3487 if (*p++ == 'm')
3488 {
256642e8 3489 parse_static_tracepoint_marker_definition (p, NULL, marker);
0fb4aa4b
PA
3490 return 1;
3491 }
3492
3493 return 0;
3494}
3495
0fb4aa4b 3496static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3497remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3498 const char *strid)
0fb4aa4b
PA
3499{
3500 struct remote_state *rs = get_remote_state ();
3501 VEC(static_tracepoint_marker_p) *markers = NULL;
3502 struct static_tracepoint_marker *marker = NULL;
3503 struct cleanup *old_chain;
256642e8 3504 const char *p;
0fb4aa4b
PA
3505
3506 /* Ask for a first packet of static tracepoint marker
3507 definition. */
3508 putpkt ("qTfSTM");
3509 getpkt (&rs->buf, &rs->buf_size, 0);
3510 p = rs->buf;
3511 if (*p == 'E')
3512 error (_("Remote failure reply: %s"), p);
3513
3514 old_chain = make_cleanup (free_current_marker, &marker);
3515
3516 while (*p++ == 'm')
3517 {
3518 if (marker == NULL)
3519 marker = XCNEW (struct static_tracepoint_marker);
3520
3521 do
3522 {
3523 parse_static_tracepoint_marker_definition (p, &p, marker);
3524
3525 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3526 {
3527 VEC_safe_push (static_tracepoint_marker_p,
3528 markers, marker);
3529 marker = NULL;
3530 }
3531 else
3532 {
3533 release_static_tracepoint_marker (marker);
3534 memset (marker, 0, sizeof (*marker));
3535 }
3536 }
3537 while (*p++ == ','); /* comma-separated list */
3538 /* Ask for another packet of static tracepoint definition. */
3539 putpkt ("qTsSTM");
3540 getpkt (&rs->buf, &rs->buf_size, 0);
3541 p = rs->buf;
3542 }
3543
3544 do_cleanups (old_chain);
3545 return markers;
3546}
3547
3548\f
10760264
JB
3549/* Implement the to_get_ada_task_ptid function for the remote targets. */
3550
3551static ptid_t
1e6b91a4 3552remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3553{
ba348170 3554 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3555}
3556\f
3557
24b06219 3558/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3559
3560static void
fba45db2 3561extended_remote_restart (void)
c906108c 3562{
d01949b6 3563 struct remote_state *rs = get_remote_state ();
c906108c
SS
3564
3565 /* Send the restart command; for reasons I don't understand the
3566 remote side really expects a number after the "R". */
ea9c271d 3567 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3568 putpkt (rs->buf);
c906108c 3569
ad9a8f3f 3570 remote_fileio_reset ();
c906108c
SS
3571}
3572\f
3573/* Clean up connection to a remote debugger. */
3574
c906108c 3575static void
de90e03d 3576remote_close (struct target_ops *self)
c906108c 3577{
5d93a237
TT
3578 struct remote_state *rs = get_remote_state ();
3579
3580 if (rs->remote_desc == NULL)
d3fd5342
PA
3581 return; /* already closed */
3582
048094ac 3583 /* Make sure we leave stdin registered in the event loop. */
e3594fd1 3584 remote_terminal_ours (self);
ce5ce7ed 3585
5d93a237
TT
3586 serial_close (rs->remote_desc);
3587 rs->remote_desc = NULL;
ce5ce7ed
PA
3588
3589 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3590 of all the inferiors and their threads we were controlling.
3591 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3592 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3593 inferior_ptid = null_ptid;
f67fd822 3594 discard_all_inferiors ();
ce5ce7ed 3595
f48ff2a7
YQ
3596 /* We are closing the remote target, so we should discard
3597 everything of this target. */
bcc75809 3598 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3599
3600 if (remote_async_inferior_event_token)
3601 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3602
5965e028 3603 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3604
3605 trace_reset_local_state ();
c906108c
SS
3606}
3607
23860348 3608/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3609
3610static void
fba45db2 3611get_offsets (void)
c906108c 3612{
d01949b6 3613 struct remote_state *rs = get_remote_state ();
2e9f7625 3614 char *buf;
085dd6e6 3615 char *ptr;
31d99776
DJ
3616 int lose, num_segments = 0, do_sections, do_segments;
3617 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3618 struct section_offsets *offs;
31d99776
DJ
3619 struct symfile_segment_data *data;
3620
3621 if (symfile_objfile == NULL)
3622 return;
c906108c
SS
3623
3624 putpkt ("qOffsets");
6d820c5c 3625 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3626 buf = rs->buf;
c906108c
SS
3627
3628 if (buf[0] == '\000')
3629 return; /* Return silently. Stub doesn't support
23860348 3630 this command. */
c906108c
SS
3631 if (buf[0] == 'E')
3632 {
8a3fe4f8 3633 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3634 return;
3635 }
3636
3637 /* Pick up each field in turn. This used to be done with scanf, but
3638 scanf will make trouble if CORE_ADDR size doesn't match
3639 conversion directives correctly. The following code will work
3640 with any size of CORE_ADDR. */
3641 text_addr = data_addr = bss_addr = 0;
3642 ptr = buf;
3643 lose = 0;
3644
61012eef 3645 if (startswith (ptr, "Text="))
c906108c
SS
3646 {
3647 ptr += 5;
3648 /* Don't use strtol, could lose on big values. */
3649 while (*ptr && *ptr != ';')
3650 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3651
61012eef 3652 if (startswith (ptr, ";Data="))
31d99776
DJ
3653 {
3654 ptr += 6;
3655 while (*ptr && *ptr != ';')
3656 data_addr = (data_addr << 4) + fromhex (*ptr++);
3657 }
3658 else
3659 lose = 1;
3660
61012eef 3661 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3662 {
3663 ptr += 5;
3664 while (*ptr && *ptr != ';')
3665 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3666
31d99776
DJ
3667 if (bss_addr != data_addr)
3668 warning (_("Target reported unsupported offsets: %s"), buf);
3669 }
3670 else
3671 lose = 1;
3672 }
61012eef 3673 else if (startswith (ptr, "TextSeg="))
c906108c 3674 {
31d99776
DJ
3675 ptr += 8;
3676 /* Don't use strtol, could lose on big values. */
c906108c 3677 while (*ptr && *ptr != ';')
31d99776
DJ
3678 text_addr = (text_addr << 4) + fromhex (*ptr++);
3679 num_segments = 1;
3680
61012eef 3681 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3682 {
3683 ptr += 9;
3684 while (*ptr && *ptr != ';')
3685 data_addr = (data_addr << 4) + fromhex (*ptr++);
3686 num_segments++;
3687 }
c906108c
SS
3688 }
3689 else
3690 lose = 1;
3691
3692 if (lose)
8a3fe4f8 3693 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3694 else if (*ptr != '\0')
3695 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3696
802188a7 3697 offs = ((struct section_offsets *)
a39a16c4 3698 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3699 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3700 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3701
31d99776
DJ
3702 data = get_symfile_segment_data (symfile_objfile->obfd);
3703 do_segments = (data != NULL);
3704 do_sections = num_segments == 0;
c906108c 3705
28c32713 3706 if (num_segments > 0)
31d99776 3707 {
31d99776
DJ
3708 segments[0] = text_addr;
3709 segments[1] = data_addr;
3710 }
28c32713
JB
3711 /* If we have two segments, we can still try to relocate everything
3712 by assuming that the .text and .data offsets apply to the whole
3713 text and data segments. Convert the offsets given in the packet
3714 to base addresses for symfile_map_offsets_to_segments. */
3715 else if (data && data->num_segments == 2)
3716 {
3717 segments[0] = data->segment_bases[0] + text_addr;
3718 segments[1] = data->segment_bases[1] + data_addr;
3719 num_segments = 2;
3720 }
8d385431
DJ
3721 /* If the object file has only one segment, assume that it is text
3722 rather than data; main programs with no writable data are rare,
3723 but programs with no code are useless. Of course the code might
3724 have ended up in the data segment... to detect that we would need
3725 the permissions here. */
3726 else if (data && data->num_segments == 1)
3727 {
3728 segments[0] = data->segment_bases[0] + text_addr;
3729 num_segments = 1;
3730 }
28c32713
JB
3731 /* There's no way to relocate by segment. */
3732 else
3733 do_segments = 0;
31d99776
DJ
3734
3735 if (do_segments)
3736 {
3737 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3738 offs, num_segments, segments);
3739
3740 if (ret == 0 && !do_sections)
3e43a32a
MS
3741 error (_("Can not handle qOffsets TextSeg "
3742 "response with this symbol file"));
31d99776
DJ
3743
3744 if (ret > 0)
3745 do_sections = 0;
3746 }
c906108c 3747
9ef895d6
DJ
3748 if (data)
3749 free_symfile_segment_data (data);
31d99776
DJ
3750
3751 if (do_sections)
3752 {
3753 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3754
3e43a32a
MS
3755 /* This is a temporary kludge to force data and bss to use the
3756 same offsets because that's what nlmconv does now. The real
3757 solution requires changes to the stub and remote.c that I
3758 don't have time to do right now. */
31d99776
DJ
3759
3760 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3761 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3762 }
c906108c
SS
3763
3764 objfile_relocate (symfile_objfile, offs);
3765}
3766
9a7071a8
JB
3767/* Send interrupt_sequence to remote target. */
3768static void
eeae04df 3769send_interrupt_sequence (void)
9a7071a8 3770{
5d93a237
TT
3771 struct remote_state *rs = get_remote_state ();
3772
9a7071a8 3773 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3774 remote_serial_write ("\x03", 1);
9a7071a8 3775 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3776 serial_send_break (rs->remote_desc);
9a7071a8
JB
3777 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3778 {
5d93a237 3779 serial_send_break (rs->remote_desc);
c33e31fd 3780 remote_serial_write ("g", 1);
9a7071a8
JB
3781 }
3782 else
3783 internal_error (__FILE__, __LINE__,
3784 _("Invalid value for interrupt_sequence_mode: %s."),
3785 interrupt_sequence_mode);
3786}
3787
3405876a
PA
3788
3789/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3790 and extract the PTID. Returns NULL_PTID if not found. */
3791
3792static ptid_t
3793stop_reply_extract_thread (char *stop_reply)
3794{
3795 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3796 {
256642e8 3797 const char *p;
3405876a
PA
3798
3799 /* Txx r:val ; r:val (...) */
3800 p = &stop_reply[3];
3801
3802 /* Look for "register" named "thread". */
3803 while (*p != '\0')
3804 {
256642e8 3805 const char *p1;
3405876a
PA
3806
3807 p1 = strchr (p, ':');
3808 if (p1 == NULL)
3809 return null_ptid;
3810
3811 if (strncmp (p, "thread", p1 - p) == 0)
3812 return read_ptid (++p1, &p);
3813
3814 p1 = strchr (p, ';');
3815 if (p1 == NULL)
3816 return null_ptid;
3817 p1++;
3818
3819 p = p1;
3820 }
3821 }
3822
3823 return null_ptid;
3824}
3825
b7ea362b
PA
3826/* Determine the remote side's current thread. If we have a stop
3827 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3828 "thread" register we can extract the current thread from. If not,
3829 ask the remote which is the current thread with qC. The former
3830 method avoids a roundtrip. */
3831
3832static ptid_t
3833get_current_thread (char *wait_status)
3834{
6a49a997 3835 ptid_t ptid = null_ptid;
b7ea362b
PA
3836
3837 /* Note we don't use remote_parse_stop_reply as that makes use of
3838 the target architecture, which we haven't yet fully determined at
3839 this point. */
3840 if (wait_status != NULL)
3841 ptid = stop_reply_extract_thread (wait_status);
3842 if (ptid_equal (ptid, null_ptid))
3843 ptid = remote_current_thread (inferior_ptid);
3844
3845 return ptid;
3846}
3847
49c62f2e
PA
3848/* Query the remote target for which is the current thread/process,
3849 add it to our tables, and update INFERIOR_PTID. The caller is
3850 responsible for setting the state such that the remote end is ready
3405876a
PA
3851 to return the current thread.
3852
3853 This function is called after handling the '?' or 'vRun' packets,
3854 whose response is a stop reply from which we can also try
3855 extracting the thread. If the target doesn't support the explicit
3856 qC query, we infer the current thread from that stop reply, passed
3857 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3858
3859static void
3405876a 3860add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3861{
3862 struct remote_state *rs = get_remote_state ();
3863 int fake_pid_p = 0;
49c62f2e
PA
3864
3865 inferior_ptid = null_ptid;
3866
b7ea362b 3867 /* Now, if we have thread information, update inferior_ptid. */
87215ad1 3868 ptid_t curr_ptid = get_current_thread (wait_status);
3405876a 3869
87215ad1 3870 if (curr_ptid != null_ptid)
49c62f2e
PA
3871 {
3872 if (!remote_multi_process_p (rs))
3873 fake_pid_p = 1;
49c62f2e
PA
3874 }
3875 else
3876 {
3877 /* Without this, some commands which require an active target
3878 (such as kill) won't work. This variable serves (at least)
3879 double duty as both the pid of the target process (if it has
3880 such), and as a flag indicating that a target is active. */
87215ad1 3881 curr_ptid = magic_null_ptid;
49c62f2e
PA
3882 fake_pid_p = 1;
3883 }
3884
87215ad1 3885 remote_add_inferior (fake_pid_p, ptid_get_pid (curr_ptid), -1, 1);
49c62f2e 3886
87215ad1
SDJ
3887 /* Add the main thread and switch to it. Don't try reading
3888 registers yet, since we haven't fetched the target description
3889 yet. */
3890 thread_info *tp = add_thread_silent (curr_ptid);
3891 switch_to_thread_no_regs (tp);
49c62f2e
PA
3892}
3893
6efcd9a8
PA
3894/* Print info about a thread that was found already stopped on
3895 connection. */
3896
3897static void
3898print_one_stopped_thread (struct thread_info *thread)
3899{
3900 struct target_waitstatus *ws = &thread->suspend.waitstatus;
3901
3902 switch_to_thread (thread->ptid);
3903 stop_pc = get_frame_pc (get_current_frame ());
3904 set_current_sal_from_frame (get_current_frame ());
3905
3906 thread->suspend.waitstatus_pending_p = 0;
3907
3908 if (ws->kind == TARGET_WAITKIND_STOPPED)
3909 {
3910 enum gdb_signal sig = ws->value.sig;
3911
3912 if (signal_print_state (sig))
3913 observer_notify_signal_received (sig);
3914 }
3915 observer_notify_normal_stop (NULL, 1);
3916}
3917
221e1a37
PA
3918/* Process all initial stop replies the remote side sent in response
3919 to the ? packet. These indicate threads that were already stopped
3920 on initial connection. We mark these threads as stopped and print
3921 their current frame before giving the user the prompt. */
3922
3923static void
6efcd9a8 3924process_initial_stop_replies (int from_tty)
221e1a37
PA
3925{
3926 int pending_stop_replies = stop_reply_queue_length ();
6efcd9a8
PA
3927 struct inferior *inf;
3928 struct thread_info *thread;
3929 struct thread_info *selected = NULL;
3930 struct thread_info *lowest_stopped = NULL;
3931 struct thread_info *first = NULL;
221e1a37
PA
3932
3933 /* Consume the initial pending events. */
3934 while (pending_stop_replies-- > 0)
3935 {
3936 ptid_t waiton_ptid = minus_one_ptid;
3937 ptid_t event_ptid;
3938 struct target_waitstatus ws;
3939 int ignore_event = 0;
6efcd9a8 3940 struct thread_info *thread;
221e1a37
PA
3941
3942 memset (&ws, 0, sizeof (ws));
3943 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
3944 if (remote_debug)
3945 print_target_wait_results (waiton_ptid, event_ptid, &ws);
3946
3947 switch (ws.kind)
3948 {
3949 case TARGET_WAITKIND_IGNORE:
3950 case TARGET_WAITKIND_NO_RESUMED:
3951 case TARGET_WAITKIND_SIGNALLED:
3952 case TARGET_WAITKIND_EXITED:
3953 /* We shouldn't see these, but if we do, just ignore. */
3954 if (remote_debug)
3955 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
3956 ignore_event = 1;
3957 break;
3958
3959 case TARGET_WAITKIND_EXECD:
3960 xfree (ws.value.execd_pathname);
3961 break;
3962 default:
3963 break;
3964 }
3965
3966 if (ignore_event)
3967 continue;
3968
6efcd9a8 3969 thread = find_thread_ptid (event_ptid);
221e1a37
PA
3970
3971 if (ws.kind == TARGET_WAITKIND_STOPPED)
3972 {
3973 enum gdb_signal sig = ws.value.sig;
3974
3975 /* Stubs traditionally report SIGTRAP as initial signal,
3976 instead of signal 0. Suppress it. */
3977 if (sig == GDB_SIGNAL_TRAP)
3978 sig = GDB_SIGNAL_0;
6efcd9a8
PA
3979 thread->suspend.stop_signal = sig;
3980 ws.value.sig = sig;
3981 }
221e1a37 3982
6efcd9a8
PA
3983 thread->suspend.waitstatus = ws;
3984
3985 if (ws.kind != TARGET_WAITKIND_STOPPED
3986 || ws.value.sig != GDB_SIGNAL_0)
3987 thread->suspend.waitstatus_pending_p = 1;
3988
3989 set_executing (event_ptid, 0);
3990 set_running (event_ptid, 0);
85ad3aaf 3991 thread->priv->vcont_resumed = 0;
6efcd9a8
PA
3992 }
3993
3994 /* "Notice" the new inferiors before anything related to
3995 registers/memory. */
3996 ALL_INFERIORS (inf)
3997 {
3998 if (inf->pid == 0)
3999 continue;
4000
4001 inf->needs_setup = 1;
4002
4003 if (non_stop)
4004 {
4005 thread = any_live_thread_of_process (inf->pid);
4006 notice_new_inferior (thread->ptid,
4007 thread->state == THREAD_RUNNING,
4008 from_tty);
4009 }
4010 }
4011
4012 /* If all-stop on top of non-stop, pause all threads. Note this
4013 records the threads' stop pc, so must be done after "noticing"
4014 the inferiors. */
4015 if (!non_stop)
4016 {
4017 stop_all_threads ();
4018
4019 /* If all threads of an inferior were already stopped, we
4020 haven't setup the inferior yet. */
4021 ALL_INFERIORS (inf)
4022 {
4023 if (inf->pid == 0)
4024 continue;
221e1a37 4025
6efcd9a8
PA
4026 if (inf->needs_setup)
4027 {
4028 thread = any_live_thread_of_process (inf->pid);
4029 switch_to_thread_no_regs (thread);
4030 setup_inferior (0);
4031 }
4032 }
221e1a37 4033 }
6efcd9a8
PA
4034
4035 /* Now go over all threads that are stopped, and print their current
4036 frame. If all-stop, then if there's a signalled thread, pick
4037 that as current. */
4038 ALL_NON_EXITED_THREADS (thread)
4039 {
6efcd9a8
PA
4040 if (first == NULL)
4041 first = thread;
4042
4043 if (!non_stop)
4044 set_running (thread->ptid, 0);
4045 else if (thread->state != THREAD_STOPPED)
4046 continue;
4047
6efcd9a8
PA
4048 if (selected == NULL
4049 && thread->suspend.waitstatus_pending_p)
4050 selected = thread;
4051
5d5658a1
PA
4052 if (lowest_stopped == NULL
4053 || thread->inf->num < lowest_stopped->inf->num
4054 || thread->per_inf_num < lowest_stopped->per_inf_num)
6efcd9a8
PA
4055 lowest_stopped = thread;
4056
4057 if (non_stop)
4058 print_one_stopped_thread (thread);
4059 }
4060
4061 /* In all-stop, we only print the status of one thread, and leave
4062 others with their status pending. */
4063 if (!non_stop)
4064 {
4065 thread = selected;
4066 if (thread == NULL)
4067 thread = lowest_stopped;
4068 if (thread == NULL)
4069 thread = first;
4070
4071 print_one_stopped_thread (thread);
4072 }
4073
4074 /* For "info program". */
4075 thread = inferior_thread ();
4076 if (thread->state == THREAD_STOPPED)
4077 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
221e1a37
PA
4078}
4079
048094ac
PA
4080/* Start the remote connection and sync state. */
4081
9cbc821d 4082static void
04bd08de 4083remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 4084{
c8d104ad
PA
4085 struct remote_state *rs = get_remote_state ();
4086 struct packet_config *noack_config;
2d717e4f 4087 char *wait_status = NULL;
8621d6a9 4088
048094ac
PA
4089 /* Signal other parts that we're going through the initial setup,
4090 and so things may not be stable yet. E.g., we don't try to
4091 install tracepoints until we've relocated symbols. Also, a
4092 Ctrl-C before we're connected and synced up can't interrupt the
4093 target. Instead, it offers to drop the (potentially wedged)
4094 connection. */
4095 rs->starting_up = 1;
4096
522002f9 4097 QUIT;
c906108c 4098
9a7071a8
JB
4099 if (interrupt_on_connect)
4100 send_interrupt_sequence ();
4101
57e12211 4102 /* Ack any packet which the remote side has already sent. */
048094ac 4103 remote_serial_write ("+", 1);
1e51243a 4104
c8d104ad
PA
4105 /* The first packet we send to the target is the optional "supported
4106 packets" request. If the target can answer this, it will tell us
4107 which later probes to skip. */
4108 remote_query_supported ();
4109
d914c394 4110 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 4111 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 4112 remote_set_permissions (target);
d914c394 4113
57809e5e
JK
4114 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4115 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4116 as a reply to known packet. For packet "vFile:setfs:" it is an
4117 invalid reply and GDB would return error in
4118 remote_hostio_set_filesystem, making remote files access impossible.
4119 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4120 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4121 {
4122 const char v_mustreplyempty[] = "vMustReplyEmpty";
4123
4124 putpkt (v_mustreplyempty);
4125 getpkt (&rs->buf, &rs->buf_size, 0);
4126 if (strcmp (rs->buf, "OK") == 0)
4127 remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
4128 else if (strcmp (rs->buf, "") != 0)
4129 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
4130 rs->buf);
4131 }
4132
c8d104ad
PA
4133 /* Next, we possibly activate noack mode.
4134
4135 If the QStartNoAckMode packet configuration is set to AUTO,
4136 enable noack mode if the stub reported a wish for it with
4137 qSupported.
4138
4139 If set to TRUE, then enable noack mode even if the stub didn't
4140 report it in qSupported. If the stub doesn't reply OK, the
4141 session ends with an error.
4142
4143 If FALSE, then don't activate noack mode, regardless of what the
4144 stub claimed should be the default with qSupported. */
4145
4146 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 4147 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
4148 {
4149 putpkt ("QStartNoAckMode");
4150 getpkt (&rs->buf, &rs->buf_size, 0);
4151 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
4152 rs->noack_mode = 1;
4153 }
4154
04bd08de 4155 if (extended_p)
5fe04517
PA
4156 {
4157 /* Tell the remote that we are using the extended protocol. */
4158 putpkt ("!");
4159 getpkt (&rs->buf, &rs->buf_size, 0);
4160 }
4161
9b224c5e
PA
4162 /* Let the target know which signals it is allowed to pass down to
4163 the program. */
4164 update_signals_program_target ();
4165
d962ef82
DJ
4166 /* Next, if the target can specify a description, read it. We do
4167 this before anything involving memory or registers. */
4168 target_find_description ();
4169
6c95b8df
PA
4170 /* Next, now that we know something about the target, update the
4171 address spaces in the program spaces. */
4172 update_address_spaces ();
4173
50c71eaf
PA
4174 /* On OSs where the list of libraries is global to all
4175 processes, we fetch them early. */
f5656ead 4176 if (gdbarch_has_global_solist (target_gdbarch ()))
e696b3ad 4177 solib_add (NULL, from_tty, auto_solib_add);
50c71eaf 4178
6efcd9a8 4179 if (target_is_non_stop_p ())
74531fed 4180 {
4082afcc 4181 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
4182 error (_("Non-stop mode requested, but remote "
4183 "does not support non-stop"));
74531fed
PA
4184
4185 putpkt ("QNonStop:1");
4186 getpkt (&rs->buf, &rs->buf_size, 0);
4187
4188 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4189 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
4190
4191 /* Find about threads and processes the stub is already
4192 controlling. We default to adding them in the running state.
4193 The '?' query below will then tell us about which threads are
4194 stopped. */
e8032dde 4195 remote_update_thread_list (target);
74531fed 4196 }
4082afcc 4197 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
4198 {
4199 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 4200 Request it explicitly. */
74531fed
PA
4201 putpkt ("QNonStop:0");
4202 getpkt (&rs->buf, &rs->buf_size, 0);
4203
4204 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4205 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
4206 }
4207
a0743c90
YQ
4208 /* Upload TSVs regardless of whether the target is running or not. The
4209 remote stub, such as GDBserver, may have some predefined or builtin
4210 TSVs, even if the target is not running. */
8bd200f1 4211 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
4212 {
4213 struct uploaded_tsv *uploaded_tsvs = NULL;
4214
181e3713 4215 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
4216 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4217 }
4218
2d717e4f
DJ
4219 /* Check whether the target is running now. */
4220 putpkt ("?");
4221 getpkt (&rs->buf, &rs->buf_size, 0);
4222
6efcd9a8 4223 if (!target_is_non_stop_p ())
2d717e4f 4224 {
74531fed 4225 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 4226 {
04bd08de 4227 if (!extended_p)
74531fed 4228 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
4229
4230 /* We're connected, but not running. Drop out before we
4231 call start_remote. */
e278ad5b 4232 rs->starting_up = 0;
c35b1492 4233 return;
2d717e4f
DJ
4234 }
4235 else
74531fed 4236 {
74531fed 4237 /* Save the reply for later. */
224c3ddb 4238 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
4239 strcpy (wait_status, rs->buf);
4240 }
4241
b7ea362b 4242 /* Fetch thread list. */
e8032dde 4243 target_update_thread_list ();
b7ea362b 4244
74531fed
PA
4245 /* Let the stub know that we want it to return the thread. */
4246 set_continue_thread (minus_one_ptid);
4247
b7ea362b
PA
4248 if (thread_count () == 0)
4249 {
4250 /* Target has no concept of threads at all. GDB treats
4251 non-threaded target as single-threaded; add a main
4252 thread. */
4253 add_current_inferior_and_thread (wait_status);
4254 }
4255 else
4256 {
4257 /* We have thread information; select the thread the target
4258 says should be current. If we're reconnecting to a
4259 multi-threaded program, this will ideally be the thread
4260 that last reported an event before GDB disconnected. */
4261 inferior_ptid = get_current_thread (wait_status);
4262 if (ptid_equal (inferior_ptid, null_ptid))
4263 {
4264 /* Odd... The target was able to list threads, but not
4265 tell us which thread was current (no "thread"
4266 register in T stop reply?). Just pick the first
4267 thread in the thread list then. */
c9f35b34
KB
4268
4269 if (remote_debug)
4270 fprintf_unfiltered (gdb_stdlog,
4271 "warning: couldn't determine remote "
4272 "current thread; picking first in list.\n");
4273
b7ea362b
PA
4274 inferior_ptid = thread_list->ptid;
4275 }
4276 }
74531fed 4277
6e586cc5
YQ
4278 /* init_wait_for_inferior should be called before get_offsets in order
4279 to manage `inserted' flag in bp loc in a correct state.
4280 breakpoint_init_inferior, called from init_wait_for_inferior, set
4281 `inserted' flag to 0, while before breakpoint_re_set, called from
4282 start_remote, set `inserted' flag to 1. In the initialization of
4283 inferior, breakpoint_init_inferior should be called first, and then
4284 breakpoint_re_set can be called. If this order is broken, state of
4285 `inserted' flag is wrong, and cause some problems on breakpoint
4286 manipulation. */
4287 init_wait_for_inferior ();
4288
74531fed
PA
4289 get_offsets (); /* Get text, data & bss offsets. */
4290
d962ef82
DJ
4291 /* If we could not find a description using qXfer, and we know
4292 how to do it some other way, try again. This is not
4293 supported for non-stop; it could be, but it is tricky if
4294 there are no stopped threads when we connect. */
04bd08de 4295 if (remote_read_description_p (target)
f5656ead 4296 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
4297 {
4298 target_clear_description ();
4299 target_find_description ();
4300 }
4301
74531fed
PA
4302 /* Use the previously fetched status. */
4303 gdb_assert (wait_status != NULL);
4304 strcpy (rs->buf, wait_status);
4305 rs->cached_wait_status = 1;
4306
04bd08de 4307 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
4308 }
4309 else
4310 {
68c97600
PA
4311 /* Clear WFI global state. Do this before finding about new
4312 threads and inferiors, and setting the current inferior.
4313 Otherwise we would clear the proceed status of the current
4314 inferior when we want its stop_soon state to be preserved
4315 (see notice_new_inferior). */
4316 init_wait_for_inferior ();
4317
74531fed
PA
4318 /* In non-stop, we will either get an "OK", meaning that there
4319 are no stopped threads at this time; or, a regular stop
4320 reply. In the latter case, there may be more than one thread
4321 stopped --- we pull them all out using the vStopped
4322 mechanism. */
4323 if (strcmp (rs->buf, "OK") != 0)
4324 {
722247f1 4325 struct notif_client *notif = &notif_client_stop;
2d717e4f 4326
722247f1
YQ
4327 /* remote_notif_get_pending_replies acks this one, and gets
4328 the rest out. */
f48ff2a7 4329 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
4330 = remote_notif_parse (notif, rs->buf);
4331 remote_notif_get_pending_events (notif);
74531fed 4332 }
2d717e4f 4333
74531fed
PA
4334 if (thread_count () == 0)
4335 {
04bd08de 4336 if (!extended_p)
74531fed 4337 error (_("The target is not running (try extended-remote?)"));
82f73884 4338
c35b1492
PA
4339 /* We're connected, but not running. Drop out before we
4340 call start_remote. */
e278ad5b 4341 rs->starting_up = 0;
c35b1492
PA
4342 return;
4343 }
74531fed 4344
74531fed
PA
4345 /* In non-stop mode, any cached wait status will be stored in
4346 the stop reply queue. */
4347 gdb_assert (wait_status == NULL);
f0223081 4348
2455069d 4349 /* Report all signals during attach/startup. */
94bedb42 4350 remote_pass_signals (target, 0, NULL);
221e1a37
PA
4351
4352 /* If there are already stopped threads, mark them stopped and
4353 report their stops before giving the prompt to the user. */
6efcd9a8 4354 process_initial_stop_replies (from_tty);
221e1a37
PA
4355
4356 if (target_can_async_p ())
4357 target_async (1);
74531fed 4358 }
c8d104ad 4359
c8d104ad
PA
4360 /* If we connected to a live target, do some additional setup. */
4361 if (target_has_execution)
4362 {
f4ccffad 4363 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 4364 remote_check_symbols ();
c8d104ad 4365 }
50c71eaf 4366
d5551862
SS
4367 /* Possibly the target has been engaged in a trace run started
4368 previously; find out where things are at. */
8bd200f1 4369 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 4370 {
00bf0b85 4371 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4372
00bf0b85
SS
4373 if (current_trace_status ()->running)
4374 printf_filtered (_("Trace is already running on the target.\n"));
4375
ab6617cc 4376 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
4377
4378 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4379 }
4380
c0272db5
TW
4381 /* Possibly the target has been engaged in a btrace record started
4382 previously; find out where things are at. */
4383 remote_btrace_maybe_reopen ();
4384
1e51243a
PA
4385 /* The thread and inferior lists are now synchronized with the
4386 target, our symbols have been relocated, and we're merged the
4387 target's tracepoints with ours. We're done with basic start
4388 up. */
4389 rs->starting_up = 0;
4390
a25a5a45
PA
4391 /* Maybe breakpoints are global and need to be inserted now. */
4392 if (breakpoints_should_be_inserted_now ())
50c71eaf 4393 insert_breakpoints ();
c906108c
SS
4394}
4395
4396/* Open a connection to a remote debugger.
4397 NAME is the filename used for communication. */
4398
4399static void
014f9477 4400remote_open (const char *name, int from_tty)
c906108c 4401{
75c99385 4402 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
4403}
4404
c906108c
SS
4405/* Open a connection to a remote debugger using the extended
4406 remote gdb protocol. NAME is the filename used for communication. */
4407
4408static void
014f9477 4409extended_remote_open (const char *name, int from_tty)
c906108c 4410{
75c99385 4411 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
4412}
4413
ca4f7f8b
PA
4414/* Reset all packets back to "unknown support". Called when opening a
4415 new connection to a remote target. */
c906108c 4416
d471ea57 4417static void
ca4f7f8b 4418reset_all_packet_configs_support (void)
d471ea57
AC
4419{
4420 int i;
a744cf53 4421
444abaca 4422 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4423 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4424}
4425
ca4f7f8b
PA
4426/* Initialize all packet configs. */
4427
4428static void
4429init_all_packet_configs (void)
4430{
4431 int i;
4432
4433 for (i = 0; i < PACKET_MAX; i++)
4434 {
4435 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4436 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4437 }
4438}
4439
23860348 4440/* Symbol look-up. */
dc8acb97
MS
4441
4442static void
36d25514 4443remote_check_symbols (void)
dc8acb97 4444{
d01949b6 4445 struct remote_state *rs = get_remote_state ();
dc8acb97 4446 char *msg, *reply, *tmp;
dc8acb97 4447 int end;
28170b88 4448 long reply_size;
a5c0808e 4449 struct cleanup *old_chain;
dc8acb97 4450
63154eca
PA
4451 /* The remote side has no concept of inferiors that aren't running
4452 yet, it only knows about running processes. If we're connected
4453 but our current inferior is not running, we should not invite the
4454 remote target to request symbol lookups related to its
4455 (unrelated) current process. */
4456 if (!target_has_execution)
4457 return;
4458
4082afcc 4459 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4460 return;
4461
63154eca
PA
4462 /* Make sure the remote is pointing at the right process. Note
4463 there's no way to select "no process". */
3c9c4b83
PA
4464 set_general_process ();
4465
6d820c5c
DJ
4466 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4467 because we need both at the same time. */
224c3ddb 4468 msg = (char *) xmalloc (get_remote_packet_size ());
a5c0808e 4469 old_chain = make_cleanup (xfree, msg);
28170b88
MK
4470 reply = (char *) xmalloc (get_remote_packet_size ());
4471 make_cleanup (free_current_contents, &reply);
4472 reply_size = get_remote_packet_size ();
6d820c5c 4473
23860348 4474 /* Invite target to request symbol lookups. */
dc8acb97
MS
4475
4476 putpkt ("qSymbol::");
28170b88
MK
4477 getpkt (&reply, &reply_size, 0);
4478 packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97 4479
61012eef 4480 while (startswith (reply, "qSymbol:"))
dc8acb97 4481 {
77e371c0
TT
4482 struct bound_minimal_symbol sym;
4483
dc8acb97 4484 tmp = &reply[8];
cfd77fa1 4485 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4486 msg[end] = '\0';
4487 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4488 if (sym.minsym == NULL)
ea9c271d 4489 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4490 else
2bbe3cc1 4491 {
f5656ead 4492 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4493 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4494
4495 /* If this is a function address, return the start of code
4496 instead of any data function descriptor. */
f5656ead 4497 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
4498 sym_addr,
4499 &current_target);
4500
4501 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4502 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4503 }
4504
dc8acb97 4505 putpkt (msg);
28170b88 4506 getpkt (&reply, &reply_size, 0);
dc8acb97 4507 }
a5c0808e
PA
4508
4509 do_cleanups (old_chain);
dc8acb97
MS
4510}
4511
9db8d71f 4512static struct serial *
baa336ce 4513remote_serial_open (const char *name)
9db8d71f
DJ
4514{
4515 static int udp_warning = 0;
4516
4517 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4518 of in ser-tcp.c, because it is the remote protocol assuming that the
4519 serial connection is reliable and not the serial connection promising
4520 to be. */
61012eef 4521 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4522 {
3e43a32a
MS
4523 warning (_("The remote protocol may be unreliable over UDP.\n"
4524 "Some events may be lost, rendering further debugging "
4525 "impossible."));
9db8d71f
DJ
4526 udp_warning = 1;
4527 }
4528
4529 return serial_open (name);
4530}
4531
d914c394
SS
4532/* Inform the target of our permission settings. The permission flags
4533 work without this, but if the target knows the settings, it can do
4534 a couple things. First, it can add its own check, to catch cases
4535 that somehow manage to get by the permissions checks in target
4536 methods. Second, if the target is wired to disallow particular
4537 settings (for instance, a system in the field that is not set up to
4538 be able to stop at a breakpoint), it can object to any unavailable
4539 permissions. */
4540
4541void
c378d69d 4542remote_set_permissions (struct target_ops *self)
d914c394
SS
4543{
4544 struct remote_state *rs = get_remote_state ();
4545
bba74b36
YQ
4546 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4547 "WriteReg:%x;WriteMem:%x;"
4548 "InsertBreak:%x;InsertTrace:%x;"
4549 "InsertFastTrace:%x;Stop:%x",
4550 may_write_registers, may_write_memory,
4551 may_insert_breakpoints, may_insert_tracepoints,
4552 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4553 putpkt (rs->buf);
4554 getpkt (&rs->buf, &rs->buf_size, 0);
4555
4556 /* If the target didn't like the packet, warn the user. Do not try
4557 to undo the user's settings, that would just be maddening. */
4558 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4559 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4560}
4561
be2a5f71
DJ
4562/* This type describes each known response to the qSupported
4563 packet. */
4564struct protocol_feature
4565{
4566 /* The name of this protocol feature. */
4567 const char *name;
4568
4569 /* The default for this protocol feature. */
4570 enum packet_support default_support;
4571
4572 /* The function to call when this feature is reported, or after
4573 qSupported processing if the feature is not supported.
4574 The first argument points to this structure. The second
4575 argument indicates whether the packet requested support be
4576 enabled, disabled, or probed (or the default, if this function
4577 is being called at the end of processing and this feature was
4578 not reported). The third argument may be NULL; if not NULL, it
4579 is a NUL-terminated string taken from the packet following
4580 this feature's name and an equals sign. */
4581 void (*func) (const struct protocol_feature *, enum packet_support,
4582 const char *);
4583
4584 /* The corresponding packet for this feature. Only used if
4585 FUNC is remote_supported_packet. */
4586 int packet;
4587};
4588
be2a5f71
DJ
4589static void
4590remote_supported_packet (const struct protocol_feature *feature,
4591 enum packet_support support,
4592 const char *argument)
4593{
4594 if (argument)
4595 {
4596 warning (_("Remote qSupported response supplied an unexpected value for"
4597 " \"%s\"."), feature->name);
4598 return;
4599 }
4600
4082afcc 4601 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4602}
be2a5f71
DJ
4603
4604static void
4605remote_packet_size (const struct protocol_feature *feature,
4606 enum packet_support support, const char *value)
4607{
4608 struct remote_state *rs = get_remote_state ();
4609
4610 int packet_size;
4611 char *value_end;
4612
4613 if (support != PACKET_ENABLE)
4614 return;
4615
4616 if (value == NULL || *value == '\0')
4617 {
4618 warning (_("Remote target reported \"%s\" without a size."),
4619 feature->name);
4620 return;
4621 }
4622
4623 errno = 0;
4624 packet_size = strtol (value, &value_end, 16);
4625 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4626 {
4627 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4628 feature->name, value);
4629 return;
4630 }
4631
be2a5f71
DJ
4632 /* Record the new maximum packet size. */
4633 rs->explicit_packet_size = packet_size;
4634}
4635
dc473cfb 4636static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4637 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4638 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4639 PACKET_qXfer_auxv },
c78fa86a
GB
4640 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4641 PACKET_qXfer_exec_file },
23181151
DJ
4642 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4643 PACKET_qXfer_features },
cfa9d6d9
DJ
4644 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4645 PACKET_qXfer_libraries },
2268b414
JK
4646 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4647 PACKET_qXfer_libraries_svr4 },
ced63ec0 4648 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4649 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4650 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4651 PACKET_qXfer_memory_map },
4de6483e
UW
4652 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4653 PACKET_qXfer_spu_read },
4654 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4655 PACKET_qXfer_spu_write },
07e059b5
VP
4656 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4657 PACKET_qXfer_osdata },
dc146f7c
VP
4658 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4659 PACKET_qXfer_threads },
b3b9301e
PA
4660 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4661 PACKET_qXfer_traceframe_info },
89be2091
DJ
4662 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4663 PACKET_QPassSignals },
82075af2
JS
4664 { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
4665 PACKET_QCatchSyscalls },
9b224c5e
PA
4666 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4667 PACKET_QProgramSignals },
bc3b087d
SDJ
4668 { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
4669 PACKET_QSetWorkingDir },
aefd8b33
SDJ
4670 { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
4671 PACKET_QStartupWithShell },
0a2dde4a
SDJ
4672 { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
4673 PACKET_QEnvironmentHexEncoded },
4674 { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
4675 PACKET_QEnvironmentReset },
4676 { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
4677 PACKET_QEnvironmentUnset },
a6f3e723
SL
4678 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4679 PACKET_QStartNoAckMode },
4082afcc
PA
4680 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4681 PACKET_multiprocess_feature },
4682 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4683 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4684 PACKET_qXfer_siginfo_read },
4685 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4686 PACKET_qXfer_siginfo_write },
4082afcc 4687 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4688 PACKET_ConditionalTracepoints },
4082afcc 4689 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4690 PACKET_ConditionalBreakpoints },
4082afcc 4691 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4692 PACKET_BreakpointCommands },
4082afcc 4693 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4694 PACKET_FastTracepoints },
4082afcc 4695 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4696 PACKET_StaticTracepoints },
4082afcc 4697 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4698 PACKET_InstallInTrace},
4082afcc
PA
4699 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4700 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4701 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4702 PACKET_bc },
4703 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4704 PACKET_bs },
409873ef
SS
4705 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4706 PACKET_TracepointSource },
d914c394
SS
4707 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4708 PACKET_QAllow },
4082afcc
PA
4709 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4710 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4711 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4712 PACKET_qXfer_fdpic },
169081d0
TG
4713 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4714 PACKET_qXfer_uib },
03583c20
UW
4715 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4716 PACKET_QDisableRandomization },
d1feda86 4717 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4718 { "QTBuffer:size", PACKET_DISABLE,
4719 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4720 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4721 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4722 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4723 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4724 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4725 PACKET_qXfer_btrace },
4726 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4727 PACKET_qXfer_btrace_conf },
4728 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4729 PACKET_Qbtrace_conf_bts_size },
4730 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4731 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4732 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4733 PACKET_fork_event_feature },
4734 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4735 PACKET_vfork_event_feature },
94585166
DB
4736 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4737 PACKET_exec_event_feature },
b20a6524 4738 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
750ce8d1 4739 PACKET_Qbtrace_conf_pt_size },
65706a29
PA
4740 { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
4741 { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
f2faf941 4742 { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
be2a5f71
DJ
4743};
4744
c8d5aac9
L
4745static char *remote_support_xml;
4746
4747/* Register string appended to "xmlRegisters=" in qSupported query. */
4748
4749void
6e39997a 4750register_remote_support_xml (const char *xml)
c8d5aac9
L
4751{
4752#if defined(HAVE_LIBEXPAT)
4753 if (remote_support_xml == NULL)
c4f7c687 4754 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4755 else
4756 {
4757 char *copy = xstrdup (remote_support_xml + 13);
4758 char *p = strtok (copy, ",");
4759
4760 do
4761 {
4762 if (strcmp (p, xml) == 0)
4763 {
4764 /* already there */
4765 xfree (copy);
4766 return;
4767 }
4768 }
4769 while ((p = strtok (NULL, ",")) != NULL);
4770 xfree (copy);
4771
94b0dee1
PA
4772 remote_support_xml = reconcat (remote_support_xml,
4773 remote_support_xml, ",", xml,
4774 (char *) NULL);
c8d5aac9
L
4775 }
4776#endif
4777}
4778
4779static char *
4780remote_query_supported_append (char *msg, const char *append)
4781{
4782 if (msg)
94b0dee1 4783 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4784 else
4785 return xstrdup (append);
4786}
4787
be2a5f71
DJ
4788static void
4789remote_query_supported (void)
4790{
4791 struct remote_state *rs = get_remote_state ();
4792 char *next;
4793 int i;
4794 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4795
4796 /* The packet support flags are handled differently for this packet
4797 than for most others. We treat an error, a disabled packet, and
4798 an empty response identically: any features which must be reported
4799 to be used will be automatically disabled. An empty buffer
4800 accomplishes this, since that is also the representation for a list
4801 containing no features. */
4802
4803 rs->buf[0] = 0;
4082afcc 4804 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4805 {
c8d5aac9 4806 char *q = NULL;
94b0dee1 4807 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4808
73b8c1fd
PA
4809 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
4810 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4811
f7e6eed5
PA
4812 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4813 q = remote_query_supported_append (q, "swbreak+");
4814 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4815 q = remote_query_supported_append (q, "hwbreak+");
4816
dde08ee1
PA
4817 q = remote_query_supported_append (q, "qRelocInsn+");
4818
8020350c
DB
4819 if (packet_set_cmd_state (PACKET_fork_event_feature)
4820 != AUTO_BOOLEAN_FALSE)
4821 q = remote_query_supported_append (q, "fork-events+");
4822 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4823 != AUTO_BOOLEAN_FALSE)
4824 q = remote_query_supported_append (q, "vfork-events+");
4825 if (packet_set_cmd_state (PACKET_exec_event_feature)
4826 != AUTO_BOOLEAN_FALSE)
4827 q = remote_query_supported_append (q, "exec-events+");
89245bc0 4828
750ce8d1
YQ
4829 if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
4830 q = remote_query_supported_append (q, "vContSupported+");
4831
65706a29
PA
4832 if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
4833 q = remote_query_supported_append (q, "QThreadEvents+");
4834
f2faf941
PA
4835 if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
4836 q = remote_query_supported_append (q, "no-resumed+");
4837
b35d5edb
PA
4838 /* Keep this one last to work around a gdbserver <= 7.10 bug in
4839 the qSupported:xmlRegisters=i386 handling. */
4840 if (remote_support_xml != NULL)
4841 q = remote_query_supported_append (q, remote_support_xml);
4842
dde08ee1
PA
4843 q = reconcat (q, "qSupported:", q, (char *) NULL);
4844 putpkt (q);
82f73884 4845
94b0dee1
PA
4846 do_cleanups (old_chain);
4847
be2a5f71
DJ
4848 getpkt (&rs->buf, &rs->buf_size, 0);
4849
4850 /* If an error occured, warn, but do not return - just reset the
4851 buffer to empty and go on to disable features. */
4852 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4853 == PACKET_ERROR)
4854 {
4855 warning (_("Remote failure reply: %s"), rs->buf);
4856 rs->buf[0] = 0;
4857 }
4858 }
4859
4860 memset (seen, 0, sizeof (seen));
4861
4862 next = rs->buf;
4863 while (*next)
4864 {
4865 enum packet_support is_supported;
4866 char *p, *end, *name_end, *value;
4867
4868 /* First separate out this item from the rest of the packet. If
4869 there's another item after this, we overwrite the separator
4870 (terminated strings are much easier to work with). */
4871 p = next;
4872 end = strchr (p, ';');
4873 if (end == NULL)
4874 {
4875 end = p + strlen (p);
4876 next = end;
4877 }
4878 else
4879 {
89be2091
DJ
4880 *end = '\0';
4881 next = end + 1;
4882
be2a5f71
DJ
4883 if (end == p)
4884 {
4885 warning (_("empty item in \"qSupported\" response"));
4886 continue;
4887 }
be2a5f71
DJ
4888 }
4889
4890 name_end = strchr (p, '=');
4891 if (name_end)
4892 {
4893 /* This is a name=value entry. */
4894 is_supported = PACKET_ENABLE;
4895 value = name_end + 1;
4896 *name_end = '\0';
4897 }
4898 else
4899 {
4900 value = NULL;
4901 switch (end[-1])
4902 {
4903 case '+':
4904 is_supported = PACKET_ENABLE;
4905 break;
4906
4907 case '-':
4908 is_supported = PACKET_DISABLE;
4909 break;
4910
4911 case '?':
4912 is_supported = PACKET_SUPPORT_UNKNOWN;
4913 break;
4914
4915 default:
3e43a32a
MS
4916 warning (_("unrecognized item \"%s\" "
4917 "in \"qSupported\" response"), p);
be2a5f71
DJ
4918 continue;
4919 }
4920 end[-1] = '\0';
4921 }
4922
4923 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4924 if (strcmp (remote_protocol_features[i].name, p) == 0)
4925 {
4926 const struct protocol_feature *feature;
4927
4928 seen[i] = 1;
4929 feature = &remote_protocol_features[i];
4930 feature->func (feature, is_supported, value);
4931 break;
4932 }
4933 }
4934
4935 /* If we increased the packet size, make sure to increase the global
4936 buffer size also. We delay this until after parsing the entire
4937 qSupported packet, because this is the same buffer we were
4938 parsing. */
4939 if (rs->buf_size < rs->explicit_packet_size)
4940 {
4941 rs->buf_size = rs->explicit_packet_size;
224c3ddb 4942 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
be2a5f71
DJ
4943 }
4944
4945 /* Handle the defaults for unmentioned features. */
4946 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4947 if (!seen[i])
4948 {
4949 const struct protocol_feature *feature;
4950
4951 feature = &remote_protocol_features[i];
4952 feature->func (feature, feature->default_support, NULL);
4953 }
4954}
4955
048094ac
PA
4956/* Serial QUIT handler for the remote serial descriptor.
4957
4958 Defers handling a Ctrl-C until we're done with the current
4959 command/response packet sequence, unless:
4960
4961 - We're setting up the connection. Don't send a remote interrupt
4962 request, as we're not fully synced yet. Quit immediately
4963 instead.
4964
4965 - The target has been resumed in the foreground
223ffa71 4966 (target_terminal::is_ours is false) with a synchronous resume
048094ac
PA
4967 packet, and we're blocked waiting for the stop reply, thus a
4968 Ctrl-C should be immediately sent to the target.
4969
4970 - We get a second Ctrl-C while still within the same serial read or
4971 write. In that case the serial is seemingly wedged --- offer to
4972 quit/disconnect.
4973
4974 - We see a second Ctrl-C without target response, after having
4975 previously interrupted the target. In that case the target/stub
4976 is probably wedged --- offer to quit/disconnect.
4977*/
4978
4979static void
4980remote_serial_quit_handler (void)
4981{
4982 struct remote_state *rs = get_remote_state ();
4983
4984 if (check_quit_flag ())
4985 {
4986 /* If we're starting up, we're not fully synced yet. Quit
4987 immediately. */
4988 if (rs->starting_up)
4989 quit ();
4990 else if (rs->got_ctrlc_during_io)
4991 {
4992 if (query (_("The target is not responding to GDB commands.\n"
4993 "Stop debugging it? ")))
4994 remote_unpush_and_throw ();
4995 }
4996 /* If ^C has already been sent once, offer to disconnect. */
223ffa71 4997 else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
048094ac
PA
4998 interrupt_query ();
4999 /* All-stop protocol, and blocked waiting for stop reply. Send
5000 an interrupt request. */
223ffa71 5001 else if (!target_terminal::is_ours () && rs->waiting_for_stop_reply)
048094ac
PA
5002 target_interrupt (inferior_ptid);
5003 else
5004 rs->got_ctrlc_during_io = 1;
5005 }
5006}
5007
78a095c3
JK
5008/* Remove any of the remote.c targets from target stack. Upper targets depend
5009 on it so remove them first. */
5010
5011static void
5012remote_unpush_target (void)
5013{
915ef8b1 5014 pop_all_targets_at_and_above (process_stratum);
78a095c3 5015}
be2a5f71 5016
048094ac
PA
5017static void
5018remote_unpush_and_throw (void)
5019{
5020 remote_unpush_target ();
5021 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
5022}
5023
c906108c 5024static void
014f9477 5025remote_open_1 (const char *name, int from_tty,
3e43a32a 5026 struct target_ops *target, int extended_p)
c906108c 5027{
d01949b6 5028 struct remote_state *rs = get_remote_state ();
a6f3e723 5029
c906108c 5030 if (name == 0)
8a3fe4f8 5031 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 5032 "serial device is attached to the remote system\n"
8a3fe4f8 5033 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 5034
23860348 5035 /* See FIXME above. */
c6ebd6cf 5036 if (!target_async_permitted)
92d1e331 5037 wait_forever_enabled_p = 1;
6426a772 5038
2d717e4f 5039 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
5040 Ask this question first, before target_preopen has a chance to kill
5041 anything. */
5d93a237 5042 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 5043 {
78a095c3
JK
5044 if (from_tty
5045 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
5046 error (_("Still connected."));
5047 }
5048
78a095c3 5049 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
5050 target_preopen (from_tty);
5051
89be2091 5052 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
5053 xfree (rs->last_pass_packet);
5054 rs->last_pass_packet = NULL;
89be2091 5055
9b224c5e
PA
5056 /* Make sure we send the program signals list the next time we
5057 resume. */
5e4a05c4
TT
5058 xfree (rs->last_program_signals_packet);
5059 rs->last_program_signals_packet = NULL;
9b224c5e 5060
ad9a8f3f 5061 remote_fileio_reset ();
1dd41f16
NS
5062 reopen_exec_file ();
5063 reread_symbols ();
5064
5d93a237
TT
5065 rs->remote_desc = remote_serial_open (name);
5066 if (!rs->remote_desc)
c906108c
SS
5067 perror_with_name (name);
5068
5069 if (baud_rate != -1)
5070 {
5d93a237 5071 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 5072 {
9b74d5d3
KB
5073 /* The requested speed could not be set. Error out to
5074 top level after closing remote_desc. Take care to
5075 set remote_desc to NULL to avoid closing remote_desc
5076 more than once. */
5d93a237
TT
5077 serial_close (rs->remote_desc);
5078 rs->remote_desc = NULL;
c906108c
SS
5079 perror_with_name (name);
5080 }
5081 }
5082
236af5e3 5083 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 5084 serial_raw (rs->remote_desc);
c906108c
SS
5085
5086 /* If there is something sitting in the buffer we might take it as a
5087 response to a command, which would be bad. */
5d93a237 5088 serial_flush_input (rs->remote_desc);
c906108c
SS
5089
5090 if (from_tty)
5091 {
5092 puts_filtered ("Remote debugging using ");
5093 puts_filtered (name);
5094 puts_filtered ("\n");
5095 }
23860348 5096 push_target (target); /* Switch to using remote target now. */
c906108c 5097
74531fed
PA
5098 /* Register extra event sources in the event loop. */
5099 remote_async_inferior_event_token
5100 = create_async_event_handler (remote_async_inferior_event_handler,
5101 NULL);
5965e028 5102 rs->notif_state = remote_notif_state_allocate ();
74531fed 5103
be2a5f71
DJ
5104 /* Reset the target state; these things will be queried either by
5105 remote_query_supported or as they are needed. */
ca4f7f8b 5106 reset_all_packet_configs_support ();
74531fed 5107 rs->cached_wait_status = 0;
be2a5f71 5108 rs->explicit_packet_size = 0;
a6f3e723 5109 rs->noack_mode = 0;
82f73884 5110 rs->extended = extended_p;
e24a49d8 5111 rs->waiting_for_stop_reply = 0;
3a29589a 5112 rs->ctrlc_pending_p = 0;
048094ac 5113 rs->got_ctrlc_during_io = 0;
802188a7 5114
47f8a51d
TT
5115 rs->general_thread = not_sent_ptid;
5116 rs->continue_thread = not_sent_ptid;
262e1174 5117 rs->remote_traceframe_number = -1;
c906108c 5118
3a00c802
PA
5119 rs->last_resume_exec_dir = EXEC_FORWARD;
5120
9d1f7ab2 5121 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
5122 rs->use_threadinfo_query = 1;
5123 rs->use_threadextra_query = 1;
9d1f7ab2 5124
80152258
PA
5125 readahead_cache_invalidate ();
5126
048094ac
PA
5127 /* Start out by owning the terminal. */
5128 remote_async_terminal_ours_p = 1;
5129
c6ebd6cf 5130 if (target_async_permitted)
92d1e331 5131 {
92d1e331
DJ
5132 /* FIXME: cagney/1999-09-23: During the initial connection it is
5133 assumed that the target is already ready and able to respond to
0df8b418 5134 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 5135 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 5136 around this. Eventually a mechanism that allows
92d1e331 5137 wait_for_inferior() to expect/get timeouts will be
23860348 5138 implemented. */
92d1e331
DJ
5139 wait_forever_enabled_p = 0;
5140 }
5141
23860348 5142 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 5143 no_shared_libraries (NULL, 0);
f78f6cf1 5144
74531fed
PA
5145 /* Start afresh. */
5146 init_thread_list ();
5147
36918e70 5148 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
5149 target (we'd otherwise be in an inconsistent state) and then
5150 propogate the error on up the exception chain. This ensures that
5151 the caller doesn't stumble along blindly assuming that the
5152 function succeeded. The CLI doesn't have this problem but other
5153 UI's, such as MI do.
36918e70
AC
5154
5155 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5156 this function should return an error indication letting the
ce2826aa 5157 caller restore the previous state. Unfortunately the command
36918e70
AC
5158 ``target remote'' is directly wired to this function making that
5159 impossible. On a positive note, the CLI side of this problem has
5160 been fixed - the function set_cmd_context() makes it possible for
5161 all the ``target ....'' commands to share a common callback
5162 function. See cli-dump.c. */
109c3e39 5163 {
2d717e4f 5164
492d29ea 5165 TRY
04bd08de
TT
5166 {
5167 remote_start_remote (from_tty, target, extended_p);
5168 }
492d29ea 5169 CATCH (ex, RETURN_MASK_ALL)
109c3e39 5170 {
c8d104ad
PA
5171 /* Pop the partially set up target - unless something else did
5172 already before throwing the exception. */
5d93a237 5173 if (rs->remote_desc != NULL)
78a095c3 5174 remote_unpush_target ();
c6ebd6cf 5175 if (target_async_permitted)
109c3e39
AC
5176 wait_forever_enabled_p = 1;
5177 throw_exception (ex);
5178 }
492d29ea 5179 END_CATCH
109c3e39 5180 }
c906108c 5181
f4abbc16
MM
5182 remote_btrace_reset ();
5183
c6ebd6cf 5184 if (target_async_permitted)
92d1e331 5185 wait_forever_enabled_p = 1;
43ff13b4
JM
5186}
5187
de0d863e
DB
5188/* Detach the specified process. */
5189
5190static void
5191remote_detach_pid (int pid)
5192{
5193 struct remote_state *rs = get_remote_state ();
5194
5195 if (remote_multi_process_p (rs))
5196 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
5197 else
5198 strcpy (rs->buf, "D");
5199
5200 putpkt (rs->buf);
5201 getpkt (&rs->buf, &rs->buf_size, 0);
5202
5203 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
5204 ;
5205 else if (rs->buf[0] == '\0')
5206 error (_("Remote doesn't know how to detach"));
5207 else
5208 error (_("Can't detach process."));
5209}
5210
5211/* This detaches a program to which we previously attached, using
5212 inferior_ptid to identify the process. After this is done, GDB
5213 can be used to debug some other program. We better not have left
5214 any breakpoints in the target program or it'll die when it hits
5215 one. */
c906108c
SS
5216
5217static void
de0d863e 5218remote_detach_1 (const char *args, int from_tty)
c906108c 5219{
82f73884 5220 int pid = ptid_get_pid (inferior_ptid);
d01949b6 5221 struct remote_state *rs = get_remote_state ();
de0d863e
DB
5222 struct thread_info *tp = find_thread_ptid (inferior_ptid);
5223 int is_fork_parent;
c906108c
SS
5224
5225 if (args)
8a3fe4f8 5226 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 5227
2d717e4f
DJ
5228 if (!target_has_execution)
5229 error (_("No process to detach from."));
5230
0f48b757 5231 target_announce_detach (from_tty);
7cee1e54 5232
c906108c 5233 /* Tell the remote target to detach. */
de0d863e 5234 remote_detach_pid (pid);
82f73884 5235
8020350c
DB
5236 /* Exit only if this is the only active inferior. */
5237 if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
7cee1e54 5238 puts_filtered (_("Ending remote debugging.\n"));
82f73884 5239
de0d863e
DB
5240 /* Check to see if we are detaching a fork parent. Note that if we
5241 are detaching a fork child, tp == NULL. */
5242 is_fork_parent = (tp != NULL
5243 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
5244
5245 /* If doing detach-on-fork, we don't mourn, because that will delete
5246 breakpoints that should be available for the followed inferior. */
5247 if (!is_fork_parent)
bc1e6c81 5248 target_mourn_inferior (inferior_ptid);
de0d863e
DB
5249 else
5250 {
5251 inferior_ptid = null_ptid;
5252 detach_inferior (pid);
5253 }
2d717e4f
DJ
5254}
5255
5256static void
52554a0e 5257remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5258{
de0d863e 5259 remote_detach_1 (args, from_tty);
2d717e4f
DJ
5260}
5261
5262static void
52554a0e 5263extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5264{
de0d863e
DB
5265 remote_detach_1 (args, from_tty);
5266}
5267
5268/* Target follow-fork function for remote targets. On entry, and
5269 at return, the current inferior is the fork parent.
5270
5271 Note that although this is currently only used for extended-remote,
5272 it is named remote_follow_fork in anticipation of using it for the
5273 remote target as well. */
5274
5275static int
5276remote_follow_fork (struct target_ops *ops, int follow_child,
5277 int detach_fork)
5278{
5279 struct remote_state *rs = get_remote_state ();
c269dbdb 5280 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 5281
c269dbdb
DB
5282 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
5283 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
5284 {
5285 /* When following the parent and detaching the child, we detach
5286 the child here. For the case of following the child and
5287 detaching the parent, the detach is done in the target-
5288 independent follow fork code in infrun.c. We can't use
5289 target_detach when detaching an unfollowed child because
5290 the client side doesn't know anything about the child. */
5291 if (detach_fork && !follow_child)
5292 {
5293 /* Detach the fork child. */
5294 ptid_t child_ptid;
5295 pid_t child_pid;
5296
5297 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
5298 child_pid = ptid_get_pid (child_ptid);
5299
5300 remote_detach_pid (child_pid);
5301 detach_inferior (child_pid);
5302 }
5303 }
5304 return 0;
c906108c
SS
5305}
5306
94585166
DB
5307/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
5308 in the program space of the new inferior. On entry and at return the
5309 current inferior is the exec'ing inferior. INF is the new exec'd
5310 inferior, which may be the same as the exec'ing inferior unless
5311 follow-exec-mode is "new". */
5312
5313static void
5314remote_follow_exec (struct target_ops *ops,
5315 struct inferior *inf, char *execd_pathname)
5316{
5317 /* We know that this is a target file name, so if it has the "target:"
5318 prefix we strip it off before saving it in the program space. */
5319 if (is_target_filename (execd_pathname))
5320 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
5321
5322 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
5323}
5324
6ad8ae5c
DJ
5325/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
5326
43ff13b4 5327static void
fee354ee 5328remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 5329{
43ff13b4 5330 if (args)
2d717e4f 5331 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 5332
8020350c
DB
5333 /* Make sure we unpush even the extended remote targets. Calling
5334 target_mourn_inferior won't unpush, and remote_mourn won't
5335 unpush if there is more than one inferior left. */
5336 unpush_target (target);
5337 generic_mourn_inferior ();
2d717e4f 5338
43ff13b4
JM
5339 if (from_tty)
5340 puts_filtered ("Ending remote debugging.\n");
5341}
5342
2d717e4f
DJ
5343/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
5344 be chatty about it. */
5345
5346static void
20f796c9
GB
5347extended_remote_attach (struct target_ops *target, const char *args,
5348 int from_tty)
2d717e4f
DJ
5349{
5350 struct remote_state *rs = get_remote_state ();
be86555c 5351 int pid;
96ef3384 5352 char *wait_status = NULL;
2d717e4f 5353
74164c56 5354 pid = parse_pid_to_attach (args);
2d717e4f 5355
74164c56
JK
5356 /* Remote PID can be freely equal to getpid, do not check it here the same
5357 way as in other targets. */
2d717e4f 5358
4082afcc 5359 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
5360 error (_("This target does not support attaching to a process"));
5361
7cee1e54
PA
5362 if (from_tty)
5363 {
5364 char *exec_file = get_exec_file (0);
5365
5366 if (exec_file)
5367 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5368 target_pid_to_str (pid_to_ptid (pid)));
5369 else
5370 printf_unfiltered (_("Attaching to %s\n"),
5371 target_pid_to_str (pid_to_ptid (pid)));
5372
5373 gdb_flush (gdb_stdout);
5374 }
5375
bba74b36 5376 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
5377 putpkt (rs->buf);
5378 getpkt (&rs->buf, &rs->buf_size, 0);
5379
4082afcc
PA
5380 switch (packet_ok (rs->buf,
5381 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 5382 {
4082afcc 5383 case PACKET_OK:
6efcd9a8 5384 if (!target_is_non_stop_p ())
74531fed
PA
5385 {
5386 /* Save the reply for later. */
224c3ddb 5387 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
5388 strcpy (wait_status, rs->buf);
5389 }
5390 else if (strcmp (rs->buf, "OK") != 0)
5391 error (_("Attaching to %s failed with: %s"),
5392 target_pid_to_str (pid_to_ptid (pid)),
5393 rs->buf);
4082afcc
PA
5394 break;
5395 case PACKET_UNKNOWN:
5396 error (_("This target does not support attaching to a process"));
5397 default:
5398 error (_("Attaching to %s failed"),
5399 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 5400 }
2d717e4f 5401
1b6e6f5c 5402 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 5403
2d717e4f 5404 inferior_ptid = pid_to_ptid (pid);
79d7f229 5405
6efcd9a8 5406 if (target_is_non_stop_p ())
bad34192
PA
5407 {
5408 struct thread_info *thread;
79d7f229 5409
bad34192 5410 /* Get list of threads. */
e8032dde 5411 remote_update_thread_list (target);
82f73884 5412
bad34192
PA
5413 thread = first_thread_of_process (pid);
5414 if (thread)
5415 inferior_ptid = thread->ptid;
5416 else
5417 inferior_ptid = pid_to_ptid (pid);
5418
5419 /* Invalidate our notion of the remote current thread. */
47f8a51d 5420 record_currthread (rs, minus_one_ptid);
bad34192 5421 }
74531fed 5422 else
bad34192
PA
5423 {
5424 /* Now, if we have thread information, update inferior_ptid. */
5425 inferior_ptid = remote_current_thread (inferior_ptid);
5426
5427 /* Add the main thread to the thread list. */
5428 add_thread_silent (inferior_ptid);
5429 }
c0a2216e 5430
96ef3384
UW
5431 /* Next, if the target can specify a description, read it. We do
5432 this before anything involving memory or registers. */
5433 target_find_description ();
5434
6efcd9a8 5435 if (!target_is_non_stop_p ())
74531fed
PA
5436 {
5437 /* Use the previously fetched status. */
5438 gdb_assert (wait_status != NULL);
5439
5440 if (target_can_async_p ())
5441 {
722247f1
YQ
5442 struct notif_event *reply
5443 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 5444
722247f1 5445 push_stop_reply ((struct stop_reply *) reply);
74531fed 5446
6a3753b3 5447 target_async (1);
74531fed
PA
5448 }
5449 else
5450 {
5451 gdb_assert (wait_status != NULL);
5452 strcpy (rs->buf, wait_status);
5453 rs->cached_wait_status = 1;
5454 }
5455 }
5456 else
5457 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5458}
5459
b9c1d481
AS
5460/* Implementation of the to_post_attach method. */
5461
5462static void
5463extended_remote_post_attach (struct target_ops *ops, int pid)
5464{
6efcd9a8
PA
5465 /* Get text, data & bss offsets. */
5466 get_offsets ();
5467
b9c1d481
AS
5468 /* In certain cases GDB might not have had the chance to start
5469 symbol lookup up until now. This could happen if the debugged
5470 binary is not using shared libraries, the vsyscall page is not
5471 present (on Linux) and the binary itself hadn't changed since the
5472 debugging process was started. */
5473 if (symfile_objfile != NULL)
5474 remote_check_symbols();
5475}
5476
c906108c 5477\f
506fb367
DJ
5478/* Check for the availability of vCont. This function should also check
5479 the response. */
c906108c
SS
5480
5481static void
6d820c5c 5482remote_vcont_probe (struct remote_state *rs)
c906108c 5483{
2e9f7625 5484 char *buf;
6d820c5c 5485
2e9f7625
DJ
5486 strcpy (rs->buf, "vCont?");
5487 putpkt (rs->buf);
6d820c5c 5488 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5489 buf = rs->buf;
c906108c 5490
506fb367 5491 /* Make sure that the features we assume are supported. */
61012eef 5492 if (startswith (buf, "vCont"))
506fb367
DJ
5493 {
5494 char *p = &buf[5];
750ce8d1 5495 int support_c, support_C;
506fb367 5496
750ce8d1
YQ
5497 rs->supports_vCont.s = 0;
5498 rs->supports_vCont.S = 0;
506fb367
DJ
5499 support_c = 0;
5500 support_C = 0;
d458bd84 5501 rs->supports_vCont.t = 0;
c1e36e3e 5502 rs->supports_vCont.r = 0;
506fb367
DJ
5503 while (p && *p == ';')
5504 {
5505 p++;
5506 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5507 rs->supports_vCont.s = 1;
506fb367 5508 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5509 rs->supports_vCont.S = 1;
506fb367
DJ
5510 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5511 support_c = 1;
5512 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5513 support_C = 1;
74531fed 5514 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5515 rs->supports_vCont.t = 1;
c1e36e3e
PA
5516 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5517 rs->supports_vCont.r = 1;
506fb367
DJ
5518
5519 p = strchr (p, ';');
5520 }
c906108c 5521
750ce8d1
YQ
5522 /* If c, and C are not all supported, we can't use vCont. Clearing
5523 BUF will make packet_ok disable the packet. */
5524 if (!support_c || !support_C)
506fb367
DJ
5525 buf[0] = 0;
5526 }
c906108c 5527
444abaca 5528 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5529}
c906108c 5530
0d8f58ca
PA
5531/* Helper function for building "vCont" resumptions. Write a
5532 resumption to P. ENDP points to one-passed-the-end of the buffer
5533 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5534 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5535 resumed thread should be single-stepped and/or signalled. If PTID
5536 equals minus_one_ptid, then all threads are resumed; if PTID
5537 represents a process, then all threads of the process are resumed;
5538 the thread to be stepped and/or signalled is given in the global
5539 INFERIOR_PTID. */
5540
5541static char *
5542append_resumption (char *p, char *endp,
2ea28649 5543 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5544{
5545 struct remote_state *rs = get_remote_state ();
5546
a493e3e2 5547 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5548 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5549 else if (step
5550 /* GDB is willing to range step. */
5551 && use_range_stepping
5552 /* Target supports range stepping. */
5553 && rs->supports_vCont.r
5554 /* We don't currently support range stepping multiple
5555 threads with a wildcard (though the protocol allows it,
5556 so stubs shouldn't make an active effort to forbid
5557 it). */
5558 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5559 {
5560 struct thread_info *tp;
5561
5562 if (ptid_equal (ptid, minus_one_ptid))
5563 {
5564 /* If we don't know about the target thread's tid, then
5565 we're resuming magic_null_ptid (see caller). */
5566 tp = find_thread_ptid (magic_null_ptid);
5567 }
5568 else
5569 tp = find_thread_ptid (ptid);
5570 gdb_assert (tp != NULL);
5571
5572 if (tp->control.may_range_step)
5573 {
5574 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5575
5576 p += xsnprintf (p, endp - p, ";r%s,%s",
5577 phex_nz (tp->control.step_range_start,
5578 addr_size),
5579 phex_nz (tp->control.step_range_end,
5580 addr_size));
5581 }
5582 else
5583 p += xsnprintf (p, endp - p, ";s");
5584 }
0d8f58ca
PA
5585 else if (step)
5586 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5587 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5588 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5589 else
5590 p += xsnprintf (p, endp - p, ";c");
5591
5592 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5593 {
5594 ptid_t nptid;
5595
5596 /* All (-1) threads of process. */
ba348170 5597 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5598
5599 p += xsnprintf (p, endp - p, ":");
5600 p = write_ptid (p, endp, nptid);
5601 }
5602 else if (!ptid_equal (ptid, minus_one_ptid))
5603 {
5604 p += xsnprintf (p, endp - p, ":");
5605 p = write_ptid (p, endp, ptid);
5606 }
5607
5608 return p;
5609}
5610
799a2abe
PA
5611/* Clear the thread's private info on resume. */
5612
5613static void
5614resume_clear_thread_private_info (struct thread_info *thread)
5615{
5616 if (thread->priv != NULL)
5617 {
5618 thread->priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5619 thread->priv->watch_data_address = 0;
5620 }
5621}
5622
e5ef252a
PA
5623/* Append a vCont continue-with-signal action for threads that have a
5624 non-zero stop signal. */
5625
5626static char *
5627append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5628{
5629 struct thread_info *thread;
5630
034f788c 5631 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5632 if (ptid_match (thread->ptid, ptid)
5633 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5634 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5635 {
5636 p = append_resumption (p, endp, thread->ptid,
5637 0, thread->suspend.stop_signal);
5638 thread->suspend.stop_signal = GDB_SIGNAL_0;
799a2abe 5639 resume_clear_thread_private_info (thread);
e5ef252a
PA
5640 }
5641
5642 return p;
5643}
5644
7b68ffbb
PA
5645/* Set the target running, using the packets that use Hc
5646 (c/s/C/S). */
5647
5648static void
5649remote_resume_with_hc (struct target_ops *ops,
5650 ptid_t ptid, int step, enum gdb_signal siggnal)
5651{
5652 struct remote_state *rs = get_remote_state ();
5653 struct thread_info *thread;
5654 char *buf;
5655
5656 rs->last_sent_signal = siggnal;
5657 rs->last_sent_step = step;
5658
5659 /* The c/s/C/S resume packets use Hc, so set the continue
5660 thread. */
5661 if (ptid_equal (ptid, minus_one_ptid))
5662 set_continue_thread (any_thread_ptid);
5663 else
5664 set_continue_thread (ptid);
5665
5666 ALL_NON_EXITED_THREADS (thread)
5667 resume_clear_thread_private_info (thread);
5668
5669 buf = rs->buf;
5670 if (execution_direction == EXEC_REVERSE)
5671 {
5672 /* We don't pass signals to the target in reverse exec mode. */
5673 if (info_verbose && siggnal != GDB_SIGNAL_0)
5674 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5675 siggnal);
5676
5677 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5678 error (_("Remote reverse-step not supported."));
5679 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5680 error (_("Remote reverse-continue not supported."));
5681
5682 strcpy (buf, step ? "bs" : "bc");
5683 }
5684 else if (siggnal != GDB_SIGNAL_0)
5685 {
5686 buf[0] = step ? 'S' : 'C';
5687 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5688 buf[2] = tohex (((int) siggnal) & 0xf);
5689 buf[3] = '\0';
5690 }
5691 else
5692 strcpy (buf, step ? "s" : "c");
5693
5694 putpkt (buf);
5695}
5696
506fb367
DJ
5697/* Resume the remote inferior by using a "vCont" packet. The thread
5698 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5699 resumed thread should be single-stepped and/or signalled. If PTID
5700 equals minus_one_ptid, then all threads are resumed; the thread to
5701 be stepped and/or signalled is given in the global INFERIOR_PTID.
5702 This function returns non-zero iff it resumes the inferior.
44eaed12 5703
7b68ffbb
PA
5704 This function issues a strict subset of all possible vCont commands
5705 at the moment. */
44eaed12 5706
506fb367 5707static int
7b68ffbb 5708remote_resume_with_vcont (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5709{
5710 struct remote_state *rs = get_remote_state ();
82f73884
PA
5711 char *p;
5712 char *endp;
44eaed12 5713
7b68ffbb
PA
5714 /* No reverse execution actions defined for vCont. */
5715 if (execution_direction == EXEC_REVERSE)
5716 return 0;
5717
4082afcc 5718 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5719 remote_vcont_probe (rs);
44eaed12 5720
4082afcc 5721 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5722 return 0;
44eaed12 5723
82f73884
PA
5724 p = rs->buf;
5725 endp = rs->buf + get_remote_packet_size ();
5726
506fb367
DJ
5727 /* If we could generate a wider range of packets, we'd have to worry
5728 about overflowing BUF. Should there be a generic
5729 "multi-part-packet" packet? */
5730
0d8f58ca
PA
5731 p += xsnprintf (p, endp - p, "vCont");
5732
79d7f229 5733 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5734 {
79d7f229
PA
5735 /* MAGIC_NULL_PTID means that we don't have any active threads,
5736 so we don't have any TID numbers the inferior will
5737 understand. Make sure to only send forms that do not specify
5738 a TID. */
a9cbf802 5739 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5740 }
0d8f58ca 5741 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5742 {
0d8f58ca
PA
5743 /* Resume all threads (of all processes, or of a single
5744 process), with preference for INFERIOR_PTID. This assumes
5745 inferior_ptid belongs to the set of all threads we are about
5746 to resume. */
a493e3e2 5747 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5748 {
0d8f58ca
PA
5749 /* Step inferior_ptid, with or without signal. */
5750 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5751 }
0d8f58ca 5752
e5ef252a
PA
5753 /* Also pass down any pending signaled resumption for other
5754 threads not the current. */
5755 p = append_pending_thread_resumptions (p, endp, ptid);
5756
0d8f58ca 5757 /* And continue others without a signal. */
a493e3e2 5758 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5759 }
5760 else
506fb367
DJ
5761 {
5762 /* Scheduler locking; resume only PTID. */
a9cbf802 5763 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5764 }
c906108c 5765
82f73884
PA
5766 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5767 putpkt (rs->buf);
506fb367 5768
6efcd9a8 5769 if (target_is_non_stop_p ())
74531fed
PA
5770 {
5771 /* In non-stop, the stub replies to vCont with "OK". The stop
5772 reply will be reported asynchronously by means of a `%Stop'
5773 notification. */
5774 getpkt (&rs->buf, &rs->buf_size, 0);
5775 if (strcmp (rs->buf, "OK") != 0)
5776 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5777 }
5778
506fb367 5779 return 1;
c906108c 5780}
43ff13b4 5781
506fb367
DJ
5782/* Tell the remote machine to resume. */
5783
43ff13b4 5784static void
28439f5e 5785remote_resume (struct target_ops *ops,
2ea28649 5786 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5787{
d01949b6 5788 struct remote_state *rs = get_remote_state ();
43ff13b4 5789
85ad3aaf
PA
5790 /* When connected in non-stop mode, the core resumes threads
5791 individually. Resuming remote threads directly in target_resume
5792 would thus result in sending one packet per thread. Instead, to
5793 minimize roundtrip latency, here we just store the resume
5794 request; the actual remote resumption will be done in
5795 target_commit_resume / remote_commit_resume, where we'll be able
5796 to do vCont action coalescing. */
5797 if (target_is_non_stop_p () && execution_direction != EXEC_REVERSE)
5798 {
5799 struct private_thread_info *remote_thr;
5800
5801 if (ptid_equal (minus_one_ptid, ptid) || ptid_is_pid (ptid))
5802 remote_thr = get_private_info_ptid (inferior_ptid);
5803 else
5804 remote_thr = get_private_info_ptid (ptid);
5805 remote_thr->last_resume_step = step;
5806 remote_thr->last_resume_sig = siggnal;
5807 return;
5808 }
5809
722247f1
YQ
5810 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5811 (explained in remote-notif.c:handle_notification) so
5812 remote_notif_process is not called. We need find a place where
5813 it is safe to start a 'vNotif' sequence. It is good to do it
5814 before resuming inferior, because inferior was stopped and no RSP
5815 traffic at that moment. */
6efcd9a8 5816 if (!target_is_non_stop_p ())
5965e028 5817 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5818
3a00c802
PA
5819 rs->last_resume_exec_dir = execution_direction;
5820
7b68ffbb
PA
5821 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
5822 if (!remote_resume_with_vcont (ptid, step, siggnal))
5823 remote_resume_with_hc (ops, ptid, step, siggnal);
43ff13b4 5824
2acceee2 5825 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5826 with the event loop. NOTE: this is the one place where all the
5827 execution commands end up. We could alternatively do this in each
23860348 5828 of the execution commands in infcmd.c. */
2acceee2
JM
5829 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5830 into infcmd.c in order to allow inferior function calls to work
23860348 5831 NOT asynchronously. */
362646f5 5832 if (target_can_async_p ())
6a3753b3 5833 target_async (1);
e24a49d8
PA
5834
5835 /* We've just told the target to resume. The remote server will
5836 wait for the inferior to stop, and then send a stop reply. In
5837 the mean time, we can't start another command/query ourselves
74531fed
PA
5838 because the stub wouldn't be ready to process it. This applies
5839 only to the base all-stop protocol, however. In non-stop (which
5840 only supports vCont), the stub replies with an "OK", and is
5841 immediate able to process further serial input. */
6efcd9a8 5842 if (!target_is_non_stop_p ())
74531fed 5843 rs->waiting_for_stop_reply = 1;
43ff13b4 5844}
85ad3aaf
PA
5845
5846static void check_pending_events_prevent_wildcard_vcont
5847 (int *may_global_wildcard_vcont);
5848static int is_pending_fork_parent_thread (struct thread_info *thread);
5849
5850/* Private per-inferior info for target remote processes. */
5851
5852struct private_inferior
5853{
5854 /* Whether we can send a wildcard vCont for this process. */
5855 int may_wildcard_vcont;
5856};
5857
5858/* Structure used to track the construction of a vCont packet in the
5859 outgoing packet buffer. This is used to send multiple vCont
5860 packets if we have more actions than would fit a single packet. */
5861
5862struct vcont_builder
5863{
5864 /* Pointer to the first action. P points here if no action has been
5865 appended yet. */
5866 char *first_action;
5867
5868 /* Where the next action will be appended. */
5869 char *p;
5870
5871 /* The end of the buffer. Must never write past this. */
5872 char *endp;
5873};
5874
5875/* Prepare the outgoing buffer for a new vCont packet. */
5876
5877static void
5878vcont_builder_restart (struct vcont_builder *builder)
5879{
5880 struct remote_state *rs = get_remote_state ();
5881
5882 builder->p = rs->buf;
5883 builder->endp = rs->buf + get_remote_packet_size ();
5884 builder->p += xsnprintf (builder->p, builder->endp - builder->p, "vCont");
5885 builder->first_action = builder->p;
5886}
5887
5888/* If the vCont packet being built has any action, send it to the
5889 remote end. */
5890
5891static void
5892vcont_builder_flush (struct vcont_builder *builder)
5893{
5894 struct remote_state *rs;
5895
5896 if (builder->p == builder->first_action)
5897 return;
5898
5899 rs = get_remote_state ();
5900 putpkt (rs->buf);
5901 getpkt (&rs->buf, &rs->buf_size, 0);
5902 if (strcmp (rs->buf, "OK") != 0)
5903 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5904}
5905
5906/* The largest action is range-stepping, with its two addresses. This
5907 is more than sufficient. If a new, bigger action is created, it'll
5908 quickly trigger a failed assertion in append_resumption (and we'll
5909 just bump this). */
5910#define MAX_ACTION_SIZE 200
5911
5912/* Append a new vCont action in the outgoing packet being built. If
5913 the action doesn't fit the packet along with previous actions, push
5914 what we've got so far to the remote end and start over a new vCont
5915 packet (with the new action). */
5916
5917static void
5918vcont_builder_push_action (struct vcont_builder *builder,
5919 ptid_t ptid, int step, enum gdb_signal siggnal)
5920{
5921 char buf[MAX_ACTION_SIZE + 1];
5922 char *endp;
5923 size_t rsize;
5924
5925 endp = append_resumption (buf, buf + sizeof (buf),
5926 ptid, step, siggnal);
5927
5928 /* Check whether this new action would fit in the vCont packet along
5929 with previous actions. If not, send what we've got so far and
5930 start a new vCont packet. */
5931 rsize = endp - buf;
5932 if (rsize > builder->endp - builder->p)
5933 {
5934 vcont_builder_flush (builder);
5935 vcont_builder_restart (builder);
5936
5937 /* Should now fit. */
5938 gdb_assert (rsize <= builder->endp - builder->p);
5939 }
5940
5941 memcpy (builder->p, buf, rsize);
5942 builder->p += rsize;
5943 *builder->p = '\0';
5944}
5945
5946/* to_commit_resume implementation. */
5947
5948static void
5949remote_commit_resume (struct target_ops *ops)
5950{
5951 struct remote_state *rs = get_remote_state ();
5952 struct inferior *inf;
5953 struct thread_info *tp;
5954 int any_process_wildcard;
5955 int may_global_wildcard_vcont;
5956 struct vcont_builder vcont_builder;
5957
5958 /* If connected in all-stop mode, we'd send the remote resume
5959 request directly from remote_resume. Likewise if
5960 reverse-debugging, as there are no defined vCont actions for
5961 reverse execution. */
5962 if (!target_is_non_stop_p () || execution_direction == EXEC_REVERSE)
5963 return;
5964
5965 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
5966 instead of resuming all threads of each process individually.
5967 However, if any thread of a process must remain halted, we can't
5968 send wildcard resumes and must send one action per thread.
5969
5970 Care must be taken to not resume threads/processes the server
5971 side already told us are stopped, but the core doesn't know about
5972 yet, because the events are still in the vStopped notification
5973 queue. For example:
5974
5975 #1 => vCont s:p1.1;c
5976 #2 <= OK
5977 #3 <= %Stopped T05 p1.1
5978 #4 => vStopped
5979 #5 <= T05 p1.2
5980 #6 => vStopped
5981 #7 <= OK
5982 #8 (infrun handles the stop for p1.1 and continues stepping)
5983 #9 => vCont s:p1.1;c
5984
5985 The last vCont above would resume thread p1.2 by mistake, because
5986 the server has no idea that the event for p1.2 had not been
5987 handled yet.
5988
5989 The server side must similarly ignore resume actions for the
5990 thread that has a pending %Stopped notification (and any other
5991 threads with events pending), until GDB acks the notification
5992 with vStopped. Otherwise, e.g., the following case is
5993 mishandled:
5994
5995 #1 => g (or any other packet)
5996 #2 <= [registers]
5997 #3 <= %Stopped T05 p1.2
5998 #4 => vCont s:p1.1;c
5999 #5 <= OK
6000
6001 Above, the server must not resume thread p1.2. GDB can't know
6002 that p1.2 stopped until it acks the %Stopped notification, and
6003 since from GDB's perspective all threads should be running, it
6004 sends a "c" action.
6005
6006 Finally, special care must also be given to handling fork/vfork
6007 events. A (v)fork event actually tells us that two processes
6008 stopped -- the parent and the child. Until we follow the fork,
6009 we must not resume the child. Therefore, if we have a pending
6010 fork follow, we must not send a global wildcard resume action
6011 (vCont;c). We can still send process-wide wildcards though. */
6012
6013 /* Start by assuming a global wildcard (vCont;c) is possible. */
6014 may_global_wildcard_vcont = 1;
6015
6016 /* And assume every process is individually wildcard-able too. */
6017 ALL_NON_EXITED_INFERIORS (inf)
6018 {
6019 if (inf->priv == NULL)
6020 inf->priv = XNEW (struct private_inferior);
6021 inf->priv->may_wildcard_vcont = 1;
6022 }
6023
6024 /* Check for any pending events (not reported or processed yet) and
6025 disable process and global wildcard resumes appropriately. */
6026 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
6027
6028 ALL_NON_EXITED_THREADS (tp)
6029 {
6030 /* If a thread of a process is not meant to be resumed, then we
6031 can't wildcard that process. */
6032 if (!tp->executing)
6033 {
6034 tp->inf->priv->may_wildcard_vcont = 0;
6035
6036 /* And if we can't wildcard a process, we can't wildcard
6037 everything either. */
6038 may_global_wildcard_vcont = 0;
6039 continue;
6040 }
6041
6042 /* If a thread is the parent of an unfollowed fork, then we
6043 can't do a global wildcard, as that would resume the fork
6044 child. */
6045 if (is_pending_fork_parent_thread (tp))
6046 may_global_wildcard_vcont = 0;
6047 }
6048
6049 /* Now let's build the vCont packet(s). Actions must be appended
6050 from narrower to wider scopes (thread -> process -> global). If
6051 we end up with too many actions for a single packet vcont_builder
6052 flushes the current vCont packet to the remote side and starts a
6053 new one. */
6054 vcont_builder_restart (&vcont_builder);
6055
6056 /* Threads first. */
6057 ALL_NON_EXITED_THREADS (tp)
6058 {
6059 struct private_thread_info *remote_thr = tp->priv;
6060
6061 if (!tp->executing || remote_thr->vcont_resumed)
6062 continue;
6063
6064 gdb_assert (!thread_is_in_step_over_chain (tp));
6065
6066 if (!remote_thr->last_resume_step
6067 && remote_thr->last_resume_sig == GDB_SIGNAL_0
6068 && tp->inf->priv->may_wildcard_vcont)
6069 {
6070 /* We'll send a wildcard resume instead. */
6071 remote_thr->vcont_resumed = 1;
6072 continue;
6073 }
6074
6075 vcont_builder_push_action (&vcont_builder, tp->ptid,
6076 remote_thr->last_resume_step,
6077 remote_thr->last_resume_sig);
6078 remote_thr->vcont_resumed = 1;
6079 }
6080
6081 /* Now check whether we can send any process-wide wildcard. This is
6082 to avoid sending a global wildcard in the case nothing is
6083 supposed to be resumed. */
6084 any_process_wildcard = 0;
6085
6086 ALL_NON_EXITED_INFERIORS (inf)
6087 {
6088 if (inf->priv->may_wildcard_vcont)
6089 {
6090 any_process_wildcard = 1;
6091 break;
6092 }
6093 }
6094
6095 if (any_process_wildcard)
6096 {
6097 /* If all processes are wildcard-able, then send a single "c"
6098 action, otherwise, send an "all (-1) threads of process"
6099 continue action for each running process, if any. */
6100 if (may_global_wildcard_vcont)
6101 {
6102 vcont_builder_push_action (&vcont_builder, minus_one_ptid,
6103 0, GDB_SIGNAL_0);
6104 }
6105 else
6106 {
6107 ALL_NON_EXITED_INFERIORS (inf)
6108 {
6109 if (inf->priv->may_wildcard_vcont)
6110 {
6111 vcont_builder_push_action (&vcont_builder,
6112 pid_to_ptid (inf->pid),
6113 0, GDB_SIGNAL_0);
6114 }
6115 }
6116 }
6117 }
6118
6119 vcont_builder_flush (&vcont_builder);
6120}
6121
c906108c 6122\f
43ff13b4 6123
74531fed
PA
6124/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6125 thread, all threads of a remote process, or all threads of all
6126 processes. */
6127
6128static void
6129remote_stop_ns (ptid_t ptid)
6130{
6131 struct remote_state *rs = get_remote_state ();
6132 char *p = rs->buf;
6133 char *endp = rs->buf + get_remote_packet_size ();
74531fed 6134
4082afcc 6135 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
6136 remote_vcont_probe (rs);
6137
d458bd84 6138 if (!rs->supports_vCont.t)
74531fed
PA
6139 error (_("Remote server does not support stopping threads"));
6140
f91d3df5
PA
6141 if (ptid_equal (ptid, minus_one_ptid)
6142 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
6143 p += xsnprintf (p, endp - p, "vCont;t");
6144 else
6145 {
6146 ptid_t nptid;
6147
74531fed
PA
6148 p += xsnprintf (p, endp - p, "vCont;t:");
6149
6150 if (ptid_is_pid (ptid))
6151 /* All (-1) threads of process. */
ba348170 6152 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
6153 else
6154 {
6155 /* Small optimization: if we already have a stop reply for
6156 this thread, no use in telling the stub we want this
6157 stopped. */
6158 if (peek_stop_reply (ptid))
6159 return;
6160
6161 nptid = ptid;
6162 }
6163
a9cbf802 6164 write_ptid (p, endp, nptid);
74531fed
PA
6165 }
6166
6167 /* In non-stop, we get an immediate OK reply. The stop reply will
6168 come in asynchronously by notification. */
6169 putpkt (rs->buf);
6170 getpkt (&rs->buf, &rs->buf_size, 0);
6171 if (strcmp (rs->buf, "OK") != 0)
6172 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
6173}
6174
bfedc46a
PA
6175/* All-stop version of target_interrupt. Sends a break or a ^C to
6176 interrupt the remote target. It is undefined which thread of which
6177 process reports the interrupt. */
74531fed
PA
6178
6179static void
de979965 6180remote_interrupt_as (void)
74531fed
PA
6181{
6182 struct remote_state *rs = get_remote_state ();
6183
3a29589a
DJ
6184 rs->ctrlc_pending_p = 1;
6185
74531fed
PA
6186 /* If the inferior is stopped already, but the core didn't know
6187 about it yet, just ignore the request. The cached wait status
6188 will be collected in remote_wait. */
6189 if (rs->cached_wait_status)
6190 return;
6191
9a7071a8
JB
6192 /* Send interrupt_sequence to remote target. */
6193 send_interrupt_sequence ();
74531fed
PA
6194}
6195
de979965
PA
6196/* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
6197 the remote target. It is undefined which thread of which process
e42de8c7
PA
6198 reports the interrupt. Throws an error if the packet is not
6199 supported by the server. */
de979965 6200
e42de8c7 6201static void
de979965
PA
6202remote_interrupt_ns (void)
6203{
6204 struct remote_state *rs = get_remote_state ();
6205 char *p = rs->buf;
6206 char *endp = rs->buf + get_remote_packet_size ();
6207
6208 xsnprintf (p, endp - p, "vCtrlC");
6209
6210 /* In non-stop, we get an immediate OK reply. The stop reply will
6211 come in asynchronously by notification. */
6212 putpkt (rs->buf);
6213 getpkt (&rs->buf, &rs->buf_size, 0);
6214
6215 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
6216 {
6217 case PACKET_OK:
6218 break;
6219 case PACKET_UNKNOWN:
e42de8c7 6220 error (_("No support for interrupting the remote target."));
de979965
PA
6221 case PACKET_ERROR:
6222 error (_("Interrupting target failed: %s"), rs->buf);
6223 }
de979965
PA
6224}
6225
bfedc46a 6226/* Implement the to_stop function for the remote targets. */
74531fed 6227
c906108c 6228static void
1eab8a48 6229remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 6230{
7a292a7a 6231 if (remote_debug)
0f71a2f6 6232 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 6233
6efcd9a8 6234 if (target_is_non_stop_p ())
74531fed 6235 remote_stop_ns (ptid);
c906108c 6236 else
bfedc46a
PA
6237 {
6238 /* We don't currently have a way to transparently pause the
6239 remote target in all-stop mode. Interrupt it instead. */
de979965 6240 remote_interrupt_as ();
bfedc46a
PA
6241 }
6242}
6243
6244/* Implement the to_interrupt function for the remote targets. */
6245
6246static void
6247remote_interrupt (struct target_ops *self, ptid_t ptid)
6248{
e42de8c7
PA
6249 struct remote_state *rs = get_remote_state ();
6250
bfedc46a
PA
6251 if (remote_debug)
6252 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
6253
e42de8c7
PA
6254 if (target_is_non_stop_p ())
6255 remote_interrupt_ns ();
bfedc46a 6256 else
e42de8c7 6257 remote_interrupt_as ();
c906108c
SS
6258}
6259
93692b58
PA
6260/* Implement the to_pass_ctrlc function for the remote targets. */
6261
6262static void
6263remote_pass_ctrlc (struct target_ops *self)
6264{
6265 struct remote_state *rs = get_remote_state ();
6266
6267 if (remote_debug)
6268 fprintf_unfiltered (gdb_stdlog, "remote_pass_ctrlc called\n");
6269
6270 /* If we're starting up, we're not fully synced yet. Quit
6271 immediately. */
6272 if (rs->starting_up)
6273 quit ();
6274 /* If ^C has already been sent once, offer to disconnect. */
6275 else if (rs->ctrlc_pending_p)
6276 interrupt_query ();
6277 else
6278 target_interrupt (inferior_ptid);
6279}
6280
c906108c
SS
6281/* Ask the user what to do when an interrupt is received. */
6282
6283static void
fba45db2 6284interrupt_query (void)
c906108c 6285{
abc56d60 6286 struct remote_state *rs = get_remote_state ();
c906108c 6287
abc56d60 6288 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 6289 {
abc56d60
PA
6290 if (query (_("The target is not responding to interrupt requests.\n"
6291 "Stop debugging it? ")))
74531fed 6292 {
78a095c3 6293 remote_unpush_target ();
abc56d60 6294 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
6295 }
6296 }
abc56d60
PA
6297 else
6298 {
6299 if (query (_("Interrupted while waiting for the program.\n"
6300 "Give up waiting? ")))
6301 quit ();
6302 }
c906108c
SS
6303}
6304
6426a772
JM
6305/* Enable/disable target terminal ownership. Most targets can use
6306 terminal groups to control terminal ownership. Remote targets are
6307 different in that explicit transfer of ownership to/from GDB/target
23860348 6308 is required. */
6426a772
JM
6309
6310static void
d2f640d4 6311remote_terminal_inferior (struct target_ops *self)
6426a772 6312{
223ffa71 6313 /* FIXME: cagney/1999-09-27: Make calls to target_terminal::*()
d9d2d8b6
PA
6314 idempotent. The event-loop GDB talking to an asynchronous target
6315 with a synchronous command calls this function from both
6316 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
6317 transfer the terminal to the target when it shouldn't this guard
6318 can go away. */
6426a772
JM
6319 if (!remote_async_terminal_ours_p)
6320 return;
6426a772 6321 remote_async_terminal_ours_p = 0;
6426a772
JM
6322 /* NOTE: At this point we could also register our selves as the
6323 recipient of all input. Any characters typed could then be
23860348 6324 passed on down to the target. */
6426a772
JM
6325}
6326
6327static void
e3594fd1 6328remote_terminal_ours (struct target_ops *self)
6426a772 6329{
75c99385 6330 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
6331 if (remote_async_terminal_ours_p)
6332 return;
6426a772
JM
6333 remote_async_terminal_ours_p = 1;
6334}
6335
176a6961 6336static void
917317f4 6337remote_console_output (char *msg)
c906108c
SS
6338{
6339 char *p;
6340
c5aa993b 6341 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
6342 {
6343 char tb[2];
6344 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 6345
c906108c
SS
6346 tb[0] = c;
6347 tb[1] = 0;
43ff13b4 6348 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 6349 }
00db5b94
PA
6350 gdb_flush (gdb_stdtarg);
6351}
74531fed 6352
74531fed
PA
6353DEF_VEC_O(cached_reg_t);
6354
722247f1 6355typedef struct stop_reply
74531fed 6356{
722247f1 6357 struct notif_event base;
74531fed 6358
722247f1 6359 /* The identifier of the thread about this event */
74531fed
PA
6360 ptid_t ptid;
6361
340e3c99 6362 /* The remote state this event is associated with. When the remote
bcc75809
YQ
6363 connection, represented by a remote_state object, is closed,
6364 all the associated stop_reply events should be released. */
6365 struct remote_state *rs;
6366
74531fed
PA
6367 struct target_waitstatus ws;
6368
5cd63fda
PA
6369 /* The architecture associated with the expedited registers. */
6370 gdbarch *arch;
6371
15148d6a
PA
6372 /* Expedited registers. This makes remote debugging a bit more
6373 efficient for those targets that provide critical registers as
6374 part of their normal status mechanism (as another roundtrip to
6375 fetch them is avoided). */
74531fed
PA
6376 VEC(cached_reg_t) *regcache;
6377
f7e6eed5
PA
6378 enum target_stop_reason stop_reason;
6379
74531fed
PA
6380 CORE_ADDR watch_data_address;
6381
dc146f7c 6382 int core;
722247f1 6383} *stop_reply_p;
a744cf53 6384
722247f1
YQ
6385DECLARE_QUEUE_P (stop_reply_p);
6386DEFINE_QUEUE_P (stop_reply_p);
6387/* The list of already fetched and acknowledged stop events. This
6388 queue is used for notification Stop, and other notifications
6389 don't need queue for their events, because the notification events
6390 of Stop can't be consumed immediately, so that events should be
6391 queued first, and be consumed by remote_wait_{ns,as} one per
6392 time. Other notifications can consume their events immediately,
6393 so queue is not needed for them. */
6394static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
6395
6396static void
6397stop_reply_xfree (struct stop_reply *r)
6398{
f48ff2a7 6399 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
6400}
6401
221e1a37
PA
6402/* Return the length of the stop reply queue. */
6403
6404static int
6405stop_reply_queue_length (void)
6406{
6407 return QUEUE_length (stop_reply_p, stop_reply_queue);
6408}
6409
722247f1
YQ
6410static void
6411remote_notif_stop_parse (struct notif_client *self, char *buf,
6412 struct notif_event *event)
6413{
6414 remote_parse_stop_reply (buf, (struct stop_reply *) event);
6415}
6416
6417static void
6418remote_notif_stop_ack (struct notif_client *self, char *buf,
6419 struct notif_event *event)
6420{
6421 struct stop_reply *stop_reply = (struct stop_reply *) event;
6422
6423 /* acknowledge */
f5c4fcd9 6424 putpkt (self->ack_command);
722247f1
YQ
6425
6426 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
6427 /* We got an unknown stop reply. */
6428 error (_("Unknown stop reply"));
6429
6430 push_stop_reply (stop_reply);
6431}
6432
6433static int
6434remote_notif_stop_can_get_pending_events (struct notif_client *self)
6435{
6436 /* We can't get pending events in remote_notif_process for
6437 notification stop, and we have to do this in remote_wait_ns
6438 instead. If we fetch all queued events from stub, remote stub
6439 may exit and we have no chance to process them back in
6440 remote_wait_ns. */
6441 mark_async_event_handler (remote_async_inferior_event_token);
6442 return 0;
6443}
6444
6445static void
6446stop_reply_dtr (struct notif_event *event)
6447{
6448 struct stop_reply *r = (struct stop_reply *) event;
d1dff226
AH
6449 cached_reg_t *reg;
6450 int ix;
6451
6452 for (ix = 0;
6453 VEC_iterate (cached_reg_t, r->regcache, ix, reg);
6454 ix++)
6455 xfree (reg->data);
722247f1
YQ
6456
6457 VEC_free (cached_reg_t, r->regcache);
6458}
6459
6460static struct notif_event *
6461remote_notif_stop_alloc_reply (void)
6462{
8d749320
SM
6463 /* We cast to a pointer to the "base class". */
6464 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
6465
6466 r->dtr = stop_reply_dtr;
6467
6468 return r;
6469}
6470
6471/* A client of notification Stop. */
6472
6473struct notif_client notif_client_stop =
6474{
6475 "Stop",
6476 "vStopped",
6477 remote_notif_stop_parse,
6478 remote_notif_stop_ack,
6479 remote_notif_stop_can_get_pending_events,
6480 remote_notif_stop_alloc_reply,
f48ff2a7 6481 REMOTE_NOTIF_STOP,
722247f1
YQ
6482};
6483
6484/* A parameter to pass data in and out. */
6485
6486struct queue_iter_param
6487{
6488 void *input;
6489 struct stop_reply *output;
6490};
6491
85ad3aaf 6492/* Determine if THREAD_PTID is a pending fork parent thread. ARG contains
cbb8991c
DB
6493 the pid of the process that owns the threads we want to check, or
6494 -1 if we want to check all threads. */
6495
6496static int
6497is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
6498 ptid_t thread_ptid)
6499{
6500 if (ws->kind == TARGET_WAITKIND_FORKED
6501 || ws->kind == TARGET_WAITKIND_VFORKED)
6502 {
6503 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
6504 return 1;
6505 }
6506
6507 return 0;
6508}
6509
85ad3aaf
PA
6510/* Return the thread's pending status used to determine whether the
6511 thread is a fork parent stopped at a fork event. */
6512
6513static struct target_waitstatus *
6514thread_pending_fork_status (struct thread_info *thread)
6515{
6516 if (thread->suspend.waitstatus_pending_p)
6517 return &thread->suspend.waitstatus;
6518 else
6519 return &thread->pending_follow;
6520}
6521
6522/* Determine if THREAD is a pending fork parent thread. */
6523
6524static int
6525is_pending_fork_parent_thread (struct thread_info *thread)
6526{
6527 struct target_waitstatus *ws = thread_pending_fork_status (thread);
6528 int pid = -1;
6529
6530 return is_pending_fork_parent (ws, pid, thread->ptid);
6531}
6532
cbb8991c
DB
6533/* Check whether EVENT is a fork event, and if it is, remove the
6534 fork child from the context list passed in DATA. */
6535
6536static int
6537remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
6538 QUEUE_ITER (stop_reply_p) *iter,
6539 stop_reply_p event,
6540 void *data)
6541{
19ba03f4
SM
6542 struct queue_iter_param *param = (struct queue_iter_param *) data;
6543 struct threads_listing_context *context
6544 = (struct threads_listing_context *) param->input;
cbb8991c
DB
6545
6546 if (event->ws.kind == TARGET_WAITKIND_FORKED
65706a29
PA
6547 || event->ws.kind == TARGET_WAITKIND_VFORKED
6548 || event->ws.kind == TARGET_WAITKIND_THREAD_EXITED)
6549 threads_listing_context_remove (&event->ws, context);
cbb8991c
DB
6550
6551 return 1;
6552}
6553
6554/* If CONTEXT contains any fork child threads that have not been
6555 reported yet, remove them from the CONTEXT list. If such a
6556 thread exists it is because we are stopped at a fork catchpoint
6557 and have not yet called follow_fork, which will set up the
6558 host-side data structures for the new process. */
6559
6560static void
6561remove_new_fork_children (struct threads_listing_context *context)
6562{
6563 struct thread_info * thread;
6564 int pid = -1;
6565 struct notif_client *notif = &notif_client_stop;
6566 struct queue_iter_param param;
6567
6568 /* For any threads stopped at a fork event, remove the corresponding
6569 fork child threads from the CONTEXT list. */
6570 ALL_NON_EXITED_THREADS (thread)
6571 {
85ad3aaf 6572 struct target_waitstatus *ws = thread_pending_fork_status (thread);
cbb8991c
DB
6573
6574 if (is_pending_fork_parent (ws, pid, thread->ptid))
6575 {
6576 threads_listing_context_remove (ws, context);
6577 }
6578 }
6579
6580 /* Check for any pending fork events (not reported or processed yet)
6581 in process PID and remove those fork child threads from the
6582 CONTEXT list as well. */
6583 remote_notif_get_pending_events (notif);
6584 param.input = context;
6585 param.output = NULL;
6586 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6587 remove_child_of_pending_fork, &param);
6588}
6589
85ad3aaf
PA
6590/* Check whether EVENT would prevent a global or process wildcard
6591 vCont action. */
6592
6593static int
6594check_pending_event_prevents_wildcard_vcont_callback
6595 (QUEUE (stop_reply_p) *q,
6596 QUEUE_ITER (stop_reply_p) *iter,
6597 stop_reply_p event,
6598 void *data)
6599{
6600 struct inferior *inf;
6601 int *may_global_wildcard_vcont = (int *) data;
6602
6603 if (event->ws.kind == TARGET_WAITKIND_NO_RESUMED
6604 || event->ws.kind == TARGET_WAITKIND_NO_HISTORY)
6605 return 1;
6606
6607 if (event->ws.kind == TARGET_WAITKIND_FORKED
6608 || event->ws.kind == TARGET_WAITKIND_VFORKED)
6609 *may_global_wildcard_vcont = 0;
6610
6611 inf = find_inferior_ptid (event->ptid);
6612
6613 /* This may be the first time we heard about this process.
6614 Regardless, we must not do a global wildcard resume, otherwise
6615 we'd resume this process too. */
6616 *may_global_wildcard_vcont = 0;
6617 if (inf != NULL)
6618 inf->priv->may_wildcard_vcont = 0;
6619
6620 return 1;
6621}
6622
6623/* Check whether any event pending in the vStopped queue would prevent
6624 a global or process wildcard vCont action. Clear
6625 *may_global_wildcard if we can't do a global wildcard (vCont;c),
6626 and clear the event inferior's may_wildcard_vcont flag if we can't
6627 do a process-wide wildcard resume (vCont;c:pPID.-1). */
6628
6629static void
6630check_pending_events_prevent_wildcard_vcont (int *may_global_wildcard)
6631{
6632 struct notif_client *notif = &notif_client_stop;
6633
6634 remote_notif_get_pending_events (notif);
6635 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6636 check_pending_event_prevents_wildcard_vcont_callback,
6637 may_global_wildcard);
6638}
6639
f48ff2a7
YQ
6640/* Remove stop replies in the queue if its pid is equal to the given
6641 inferior's pid. */
722247f1
YQ
6642
6643static int
f48ff2a7
YQ
6644remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
6645 QUEUE_ITER (stop_reply_p) *iter,
6646 stop_reply_p event,
6647 void *data)
722247f1 6648{
19ba03f4
SM
6649 struct queue_iter_param *param = (struct queue_iter_param *) data;
6650 struct inferior *inf = (struct inferior *) param->input;
722247f1 6651
f48ff2a7 6652 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
6653 {
6654 stop_reply_xfree (event);
6655 QUEUE_remove_elem (stop_reply_p, q, iter);
6656 }
6657
6658 return 1;
6659}
6660
f48ff2a7 6661/* Discard all pending stop replies of inferior INF. */
c906108c 6662
74531fed 6663static void
5f4cf0bb 6664discard_pending_stop_replies (struct inferior *inf)
c906108c 6665{
722247f1 6666 struct queue_iter_param param;
f48ff2a7
YQ
6667 struct stop_reply *reply;
6668 struct remote_state *rs = get_remote_state ();
6669 struct remote_notif_state *rns = rs->notif_state;
6670
6671 /* This function can be notified when an inferior exists. When the
6672 target is not remote, the notification state is NULL. */
6673 if (rs->remote_desc == NULL)
6674 return;
6675
6676 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 6677
74531fed 6678 /* Discard the in-flight notification. */
f48ff2a7 6679 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 6680 {
722247f1 6681 stop_reply_xfree (reply);
f48ff2a7 6682 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 6683 }
c906108c 6684
722247f1
YQ
6685 param.input = inf;
6686 param.output = NULL;
74531fed
PA
6687 /* Discard the stop replies we have already pulled with
6688 vStopped. */
722247f1 6689 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
6690 remove_stop_reply_for_inferior, &param);
6691}
6692
bcc75809
YQ
6693/* If its remote state is equal to the given remote state,
6694 remove EVENT from the stop reply queue. */
6695
6696static int
6697remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
6698 QUEUE_ITER (stop_reply_p) *iter,
6699 stop_reply_p event,
6700 void *data)
6701{
19ba03f4
SM
6702 struct queue_iter_param *param = (struct queue_iter_param *) data;
6703 struct remote_state *rs = (struct remote_state *) param->input;
bcc75809
YQ
6704
6705 if (event->rs == rs)
6706 {
6707 stop_reply_xfree (event);
6708 QUEUE_remove_elem (stop_reply_p, q, iter);
6709 }
6710
6711 return 1;
6712}
6713
6714/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
6715
6716static void
bcc75809 6717discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
6718{
6719 struct queue_iter_param param;
6720
bcc75809 6721 param.input = rs;
f48ff2a7
YQ
6722 param.output = NULL;
6723 /* Discard the stop replies we have already pulled with
6724 vStopped. */
6725 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 6726 remove_stop_reply_of_remote_state, &param);
722247f1 6727}
74531fed 6728
722247f1
YQ
6729/* A parameter to pass data in and out. */
6730
6731static int
6732remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
6733 QUEUE_ITER (stop_reply_p) *iter,
6734 stop_reply_p event,
6735 void *data)
6736{
19ba03f4
SM
6737 struct queue_iter_param *param = (struct queue_iter_param *) data;
6738 ptid_t *ptid = (ptid_t *) param->input;
722247f1
YQ
6739
6740 if (ptid_match (event->ptid, *ptid))
6741 {
6742 param->output = event;
6743 QUEUE_remove_elem (stop_reply_p, q, iter);
6744 return 0;
c8e38a49 6745 }
722247f1
YQ
6746
6747 return 1;
74531fed 6748}
43ff13b4 6749
722247f1
YQ
6750/* Remove the first reply in 'stop_reply_queue' which matches
6751 PTID. */
2e9f7625 6752
722247f1
YQ
6753static struct stop_reply *
6754remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 6755{
722247f1
YQ
6756 struct queue_iter_param param;
6757
6758 param.input = &ptid;
6759 param.output = NULL;
6760
6761 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6762 remote_notif_remove_once_on_match, &param);
6763 if (notif_debug)
6764 fprintf_unfiltered (gdb_stdlog,
6765 "notif: discard queued event: 'Stop' in %s\n",
6766 target_pid_to_str (ptid));
a744cf53 6767
722247f1 6768 return param.output;
74531fed 6769}
75c99385 6770
74531fed
PA
6771/* Look for a queued stop reply belonging to PTID. If one is found,
6772 remove it from the queue, and return it. Returns NULL if none is
6773 found. If there are still queued events left to process, tell the
6774 event loop to get back to target_wait soon. */
e24a49d8 6775
74531fed
PA
6776static struct stop_reply *
6777queued_stop_reply (ptid_t ptid)
6778{
722247f1 6779 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 6780
722247f1 6781 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
6782 /* There's still at least an event left. */
6783 mark_async_event_handler (remote_async_inferior_event_token);
6784
722247f1 6785 return r;
74531fed
PA
6786}
6787
6788/* Push a fully parsed stop reply in the stop reply queue. Since we
6789 know that we now have at least one queued event left to pass to the
6790 core side, tell the event loop to get back to target_wait soon. */
6791
6792static void
6793push_stop_reply (struct stop_reply *new_event)
6794{
722247f1 6795 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 6796
722247f1
YQ
6797 if (notif_debug)
6798 fprintf_unfiltered (gdb_stdlog,
6799 "notif: push 'Stop' %s to queue %d\n",
6800 target_pid_to_str (new_event->ptid),
6801 QUEUE_length (stop_reply_p,
6802 stop_reply_queue));
74531fed
PA
6803
6804 mark_async_event_handler (remote_async_inferior_event_token);
6805}
6806
722247f1
YQ
6807static int
6808stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
6809 QUEUE_ITER (stop_reply_p) *iter,
6810 struct stop_reply *event,
6811 void *data)
6812{
19ba03f4 6813 ptid_t *ptid = (ptid_t *) data;
722247f1
YQ
6814
6815 return !(ptid_equal (*ptid, event->ptid)
6816 && event->ws.kind == TARGET_WAITKIND_STOPPED);
6817}
6818
74531fed
PA
6819/* Returns true if we have a stop reply for PTID. */
6820
6821static int
6822peek_stop_reply (ptid_t ptid)
6823{
722247f1
YQ
6824 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
6825 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
6826}
6827
26d56a93
SL
6828/* Helper for remote_parse_stop_reply. Return nonzero if the substring
6829 starting with P and ending with PEND matches PREFIX. */
6830
6831static int
6832strprefix (const char *p, const char *pend, const char *prefix)
6833{
6834 for ( ; p < pend; p++, prefix++)
6835 if (*p != *prefix)
6836 return 0;
6837 return *prefix == '\0';
6838}
6839
74531fed
PA
6840/* Parse the stop reply in BUF. Either the function succeeds, and the
6841 result is stored in EVENT, or throws an error. */
6842
6843static void
6844remote_parse_stop_reply (char *buf, struct stop_reply *event)
6845{
5cd63fda 6846 remote_arch_state *rsa = NULL;
74531fed 6847 ULONGEST addr;
256642e8 6848 const char *p;
94585166 6849 int skipregs = 0;
74531fed
PA
6850
6851 event->ptid = null_ptid;
bcc75809 6852 event->rs = get_remote_state ();
74531fed
PA
6853 event->ws.kind = TARGET_WAITKIND_IGNORE;
6854 event->ws.value.integer = 0;
f7e6eed5 6855 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 6856 event->regcache = NULL;
dc146f7c 6857 event->core = -1;
74531fed
PA
6858
6859 switch (buf[0])
6860 {
6861 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
6862 /* Expedited reply, containing Signal, {regno, reg} repeat. */
6863 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
6864 ss = signal number
6865 n... = register number
6866 r... = register contents
6867 */
6868
6869 p = &buf[3]; /* after Txx */
6870 while (*p)
6871 {
256642e8 6872 const char *p1;
cea39f65 6873 int fieldsize;
43ff13b4 6874
1f10ba14
PA
6875 p1 = strchr (p, ':');
6876 if (p1 == NULL)
6877 error (_("Malformed packet(a) (missing colon): %s\n\
6878Packet: '%s'\n"),
6879 p, buf);
6880 if (p == p1)
6881 error (_("Malformed packet(a) (missing register number): %s\n\
6882Packet: '%s'\n"),
6883 p, buf);
3c3bea1c 6884
1f10ba14
PA
6885 /* Some "registers" are actually extended stop information.
6886 Note if you're adding a new entry here: GDB 7.9 and
6887 earlier assume that all register "numbers" that start
6888 with an hex digit are real register numbers. Make sure
6889 the server only sends such a packet if it knows the
6890 client understands it. */
c8e38a49 6891
26d56a93 6892 if (strprefix (p, p1, "thread"))
1f10ba14 6893 event->ptid = read_ptid (++p1, &p);
82075af2
JS
6894 else if (strprefix (p, p1, "syscall_entry"))
6895 {
6896 ULONGEST sysno;
6897
6898 event->ws.kind = TARGET_WAITKIND_SYSCALL_ENTRY;
6899 p = unpack_varlen_hex (++p1, &sysno);
6900 event->ws.value.syscall_number = (int) sysno;
6901 }
6902 else if (strprefix (p, p1, "syscall_return"))
6903 {
6904 ULONGEST sysno;
6905
6906 event->ws.kind = TARGET_WAITKIND_SYSCALL_RETURN;
6907 p = unpack_varlen_hex (++p1, &sysno);
6908 event->ws.value.syscall_number = (int) sysno;
6909 }
26d56a93
SL
6910 else if (strprefix (p, p1, "watch")
6911 || strprefix (p, p1, "rwatch")
6912 || strprefix (p, p1, "awatch"))
cea39f65 6913 {
f7e6eed5 6914 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
6915 p = unpack_varlen_hex (++p1, &addr);
6916 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 6917 }
26d56a93 6918 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
6919 {
6920 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
6921
6922 /* Make sure the stub doesn't forget to indicate support
6923 with qSupported. */
6924 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
6925 error (_("Unexpected swbreak stop reason"));
6926
6927 /* The value part is documented as "must be empty",
6928 though we ignore it, in case we ever decide to make
6929 use of it in a backward compatible way. */
8424cc97 6930 p = strchrnul (p1 + 1, ';');
f7e6eed5 6931 }
26d56a93 6932 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
6933 {
6934 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
6935
6936 /* Make sure the stub doesn't forget to indicate support
6937 with qSupported. */
6938 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
6939 error (_("Unexpected hwbreak stop reason"));
6940
6941 /* See above. */
8424cc97 6942 p = strchrnul (p1 + 1, ';');
f7e6eed5 6943 }
26d56a93 6944 else if (strprefix (p, p1, "library"))
cea39f65 6945 {
1f10ba14 6946 event->ws.kind = TARGET_WAITKIND_LOADED;
8424cc97 6947 p = strchrnul (p1 + 1, ';');
1f10ba14 6948 }
26d56a93 6949 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
6950 {
6951 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
6952 /* p1 will indicate "begin" or "end", but it makes
6953 no difference for now, so ignore it. */
8424cc97 6954 p = strchrnul (p1 + 1, ';');
1f10ba14 6955 }
26d56a93 6956 else if (strprefix (p, p1, "core"))
1f10ba14
PA
6957 {
6958 ULONGEST c;
a744cf53 6959
1f10ba14
PA
6960 p = unpack_varlen_hex (++p1, &c);
6961 event->core = c;
cea39f65 6962 }
26d56a93 6963 else if (strprefix (p, p1, "fork"))
de0d863e
DB
6964 {
6965 event->ws.value.related_pid = read_ptid (++p1, &p);
6966 event->ws.kind = TARGET_WAITKIND_FORKED;
6967 }
26d56a93 6968 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
6969 {
6970 event->ws.value.related_pid = read_ptid (++p1, &p);
6971 event->ws.kind = TARGET_WAITKIND_VFORKED;
6972 }
26d56a93 6973 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
6974 {
6975 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
8424cc97 6976 p = strchrnul (p1 + 1, ';');
c269dbdb 6977 }
6ab24463 6978 else if (strprefix (p, p1, "exec"))
94585166
DB
6979 {
6980 ULONGEST ignored;
6981 char pathname[PATH_MAX];
6982 int pathlen;
6983
6984 /* Determine the length of the execd pathname. */
6985 p = unpack_varlen_hex (++p1, &ignored);
6986 pathlen = (p - p1) / 2;
6987
6988 /* Save the pathname for event reporting and for
6989 the next run command. */
6990 hex2bin (p1, (gdb_byte *) pathname, pathlen);
6991 pathname[pathlen] = '\0';
6992
6993 /* This is freed during event handling. */
6994 event->ws.value.execd_pathname = xstrdup (pathname);
6995 event->ws.kind = TARGET_WAITKIND_EXECD;
6996
6997 /* Skip the registers included in this packet, since
6998 they may be for an architecture different from the
6999 one used by the original program. */
7000 skipregs = 1;
7001 }
65706a29
PA
7002 else if (strprefix (p, p1, "create"))
7003 {
7004 event->ws.kind = TARGET_WAITKIND_THREAD_CREATED;
8424cc97 7005 p = strchrnul (p1 + 1, ';');
65706a29 7006 }
cea39f65
MS
7007 else
7008 {
1f10ba14 7009 ULONGEST pnum;
256642e8 7010 const char *p_temp;
1f10ba14 7011
94585166
DB
7012 if (skipregs)
7013 {
8424cc97 7014 p = strchrnul (p1 + 1, ';');
94585166
DB
7015 p++;
7016 continue;
7017 }
7018
1f10ba14
PA
7019 /* Maybe a real ``P'' register number. */
7020 p_temp = unpack_varlen_hex (p, &pnum);
7021 /* If the first invalid character is the colon, we got a
7022 register number. Otherwise, it's an unknown stop
7023 reason. */
7024 if (p_temp == p1)
7025 {
5cd63fda
PA
7026 /* If we haven't parsed the event's thread yet, find
7027 it now, in order to find the architecture of the
7028 reported expedited registers. */
7029 if (event->ptid == null_ptid)
7030 {
7031 const char *thr = strstr (p1 + 1, ";thread:");
7032 if (thr != NULL)
7033 event->ptid = read_ptid (thr + strlen (";thread:"),
7034 NULL);
7035 else
7036 event->ptid = magic_null_ptid;
7037 }
7038
7039 if (rsa == NULL)
7040 {
7041 inferior *inf = (event->ptid == null_ptid
7042 ? NULL
7043 : find_inferior_ptid (event->ptid));
7044 /* If this is the first time we learn anything
7045 about this process, skip the registers
7046 included in this packet, since we don't yet
7047 know which architecture to use to parse them.
7048 We'll determine the architecture later when
7049 we process the stop reply and retrieve the
7050 target description, via
7051 remote_notice_new_inferior ->
7052 post_create_inferior. */
7053 if (inf == NULL)
7054 {
7055 p = strchrnul (p1 + 1, ';');
7056 p++;
7057 continue;
7058 }
7059
7060 event->arch = inf->gdbarch;
7061 rsa = get_remote_arch_state (event->arch);
7062 }
7063
7064 packet_reg *reg
7065 = packet_reg_from_pnum (event->arch, rsa, pnum);
1f10ba14 7066 cached_reg_t cached_reg;
43ff13b4 7067
1f10ba14
PA
7068 if (reg == NULL)
7069 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 7070Packet: '%s'\n"),
1f10ba14 7071 hex_string (pnum), p, buf);
c8e38a49 7072
1f10ba14 7073 cached_reg.num = reg->regnum;
d1dff226 7074 cached_reg.data = (gdb_byte *)
5cd63fda 7075 xmalloc (register_size (event->arch, reg->regnum));
4100683b 7076
1f10ba14
PA
7077 p = p1 + 1;
7078 fieldsize = hex2bin (p, cached_reg.data,
5cd63fda 7079 register_size (event->arch, reg->regnum));
1f10ba14 7080 p += 2 * fieldsize;
5cd63fda 7081 if (fieldsize < register_size (event->arch, reg->regnum))
1f10ba14 7082 warning (_("Remote reply is too short: %s"), buf);
74531fed 7083
1f10ba14
PA
7084 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
7085 }
7086 else
7087 {
7088 /* Not a number. Silently skip unknown optional
7089 info. */
8424cc97 7090 p = strchrnul (p1 + 1, ';');
1f10ba14 7091 }
cea39f65 7092 }
c8e38a49 7093
cea39f65
MS
7094 if (*p != ';')
7095 error (_("Remote register badly formatted: %s\nhere: %s"),
7096 buf, p);
7097 ++p;
7098 }
5b5596ff
PA
7099
7100 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
7101 break;
7102
c8e38a49
PA
7103 /* fall through */
7104 case 'S': /* Old style status, just signal only. */
3a09da41
PA
7105 {
7106 int sig;
7107
7108 event->ws.kind = TARGET_WAITKIND_STOPPED;
7109 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
7110 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
7111 event->ws.value.sig = (enum gdb_signal) sig;
7112 else
7113 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
7114 }
c8e38a49 7115 break;
65706a29
PA
7116 case 'w': /* Thread exited. */
7117 {
256642e8 7118 const char *p;
65706a29
PA
7119 ULONGEST value;
7120
7121 event->ws.kind = TARGET_WAITKIND_THREAD_EXITED;
7122 p = unpack_varlen_hex (&buf[1], &value);
7123 event->ws.value.integer = value;
7124 if (*p != ';')
7125 error (_("stop reply packet badly formatted: %s"), buf);
974eac9d 7126 event->ptid = read_ptid (++p, NULL);
65706a29
PA
7127 break;
7128 }
c8e38a49
PA
7129 case 'W': /* Target exited. */
7130 case 'X':
7131 {
256642e8 7132 const char *p;
c8e38a49
PA
7133 int pid;
7134 ULONGEST value;
82f73884 7135
c8e38a49
PA
7136 /* GDB used to accept only 2 hex chars here. Stubs should
7137 only send more if they detect GDB supports multi-process
7138 support. */
7139 p = unpack_varlen_hex (&buf[1], &value);
82f73884 7140
c8e38a49
PA
7141 if (buf[0] == 'W')
7142 {
7143 /* The remote process exited. */
74531fed
PA
7144 event->ws.kind = TARGET_WAITKIND_EXITED;
7145 event->ws.value.integer = value;
c8e38a49
PA
7146 }
7147 else
7148 {
7149 /* The remote process exited with a signal. */
74531fed 7150 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
7151 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
7152 event->ws.value.sig = (enum gdb_signal) value;
7153 else
7154 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 7155 }
82f73884 7156
c8e38a49
PA
7157 /* If no process is specified, assume inferior_ptid. */
7158 pid = ptid_get_pid (inferior_ptid);
7159 if (*p == '\0')
7160 ;
7161 else if (*p == ';')
7162 {
7163 p++;
7164
0b24eb2d 7165 if (*p == '\0')
82f73884 7166 ;
61012eef 7167 else if (startswith (p, "process:"))
82f73884 7168 {
c8e38a49 7169 ULONGEST upid;
a744cf53 7170
c8e38a49
PA
7171 p += sizeof ("process:") - 1;
7172 unpack_varlen_hex (p, &upid);
7173 pid = upid;
82f73884
PA
7174 }
7175 else
7176 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 7177 }
c8e38a49
PA
7178 else
7179 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
7180 event->ptid = pid_to_ptid (pid);
7181 }
7182 break;
f2faf941
PA
7183 case 'N':
7184 event->ws.kind = TARGET_WAITKIND_NO_RESUMED;
7185 event->ptid = minus_one_ptid;
7186 break;
74531fed
PA
7187 }
7188
6efcd9a8 7189 if (target_is_non_stop_p () && ptid_equal (event->ptid, null_ptid))
74531fed
PA
7190 error (_("No process or thread specified in stop reply: %s"), buf);
7191}
7192
722247f1
YQ
7193/* When the stub wants to tell GDB about a new notification reply, it
7194 sends a notification (%Stop, for example). Those can come it at
7195 any time, hence, we have to make sure that any pending
7196 putpkt/getpkt sequence we're making is finished, before querying
7197 the stub for more events with the corresponding ack command
7198 (vStopped, for example). E.g., if we started a vStopped sequence
7199 immediately upon receiving the notification, something like this
7200 could happen:
74531fed
PA
7201
7202 1.1) --> Hg 1
7203 1.2) <-- OK
7204 1.3) --> g
7205 1.4) <-- %Stop
7206 1.5) --> vStopped
7207 1.6) <-- (registers reply to step #1.3)
7208
7209 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7210 query.
7211
796cb314 7212 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
7213 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7214 doing whatever we were doing:
7215
7216 2.1) --> Hg 1
7217 2.2) <-- OK
7218 2.3) --> g
7219 2.4) <-- %Stop
7220 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7221 2.5) <-- (registers reply to step #2.3)
7222
7223 Eventualy after step #2.5, we return to the event loop, which
7224 notices there's an event on the
7225 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7226 associated callback --- the function below. At this point, we're
7227 always safe to start a vStopped sequence. :
7228
7229 2.6) --> vStopped
7230 2.7) <-- T05 thread:2
7231 2.8) --> vStopped
7232 2.9) --> OK
7233*/
7234
722247f1
YQ
7235void
7236remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
7237{
7238 struct remote_state *rs = get_remote_state ();
74531fed 7239
f48ff2a7 7240 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 7241 {
722247f1
YQ
7242 if (notif_debug)
7243 fprintf_unfiltered (gdb_stdlog,
7244 "notif: process: '%s' ack pending event\n",
7245 nc->name);
74531fed 7246
722247f1 7247 /* acknowledge */
f48ff2a7
YQ
7248 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
7249 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
7250
7251 while (1)
7252 {
7253 getpkt (&rs->buf, &rs->buf_size, 0);
7254 if (strcmp (rs->buf, "OK") == 0)
7255 break;
7256 else
722247f1 7257 remote_notif_ack (nc, rs->buf);
74531fed
PA
7258 }
7259 }
722247f1
YQ
7260 else
7261 {
7262 if (notif_debug)
7263 fprintf_unfiltered (gdb_stdlog,
7264 "notif: process: '%s' no pending reply\n",
7265 nc->name);
7266 }
74531fed
PA
7267}
7268
74531fed
PA
7269/* Called when it is decided that STOP_REPLY holds the info of the
7270 event that is to be returned to the core. This function always
7271 destroys STOP_REPLY. */
7272
7273static ptid_t
7274process_stop_reply (struct stop_reply *stop_reply,
7275 struct target_waitstatus *status)
7276{
7277 ptid_t ptid;
7278
7279 *status = stop_reply->ws;
7280 ptid = stop_reply->ptid;
7281
7282 /* If no thread/process was reported by the stub, assume the current
7283 inferior. */
7284 if (ptid_equal (ptid, null_ptid))
7285 ptid = inferior_ptid;
7286
5f3563ea 7287 if (status->kind != TARGET_WAITKIND_EXITED
f2faf941
PA
7288 && status->kind != TARGET_WAITKIND_SIGNALLED
7289 && status->kind != TARGET_WAITKIND_NO_RESUMED)
74531fed 7290 {
799a2abe 7291 struct private_thread_info *remote_thr;
ee154bee 7292
5f3563ea
PA
7293 /* Expedited registers. */
7294 if (stop_reply->regcache)
7295 {
217f1f79 7296 struct regcache *regcache
5cd63fda 7297 = get_thread_arch_regcache (ptid, stop_reply->arch);
5f3563ea
PA
7298 cached_reg_t *reg;
7299 int ix;
7300
7301 for (ix = 0;
d1dff226 7302 VEC_iterate (cached_reg_t, stop_reply->regcache, ix, reg);
5f3563ea 7303 ix++)
d1dff226 7304 {
217f1f79 7305 regcache_raw_supply (regcache, reg->num, reg->data);
d1dff226
AH
7306 xfree (reg->data);
7307 }
7308
5f3563ea
PA
7309 VEC_free (cached_reg_t, stop_reply->regcache);
7310 }
74531fed 7311
1941c569 7312 remote_notice_new_inferior (ptid, 0);
85ad3aaf 7313 remote_thr = get_private_info_ptid (ptid);
799a2abe
PA
7314 remote_thr->core = stop_reply->core;
7315 remote_thr->stop_reason = stop_reply->stop_reason;
7316 remote_thr->watch_data_address = stop_reply->watch_data_address;
85ad3aaf 7317 remote_thr->vcont_resumed = 0;
74531fed
PA
7318 }
7319
74531fed
PA
7320 stop_reply_xfree (stop_reply);
7321 return ptid;
7322}
7323
7324/* The non-stop mode version of target_wait. */
7325
7326static ptid_t
47608cb1 7327remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7328{
7329 struct remote_state *rs = get_remote_state ();
74531fed
PA
7330 struct stop_reply *stop_reply;
7331 int ret;
fee9eda9 7332 int is_notif = 0;
74531fed
PA
7333
7334 /* If in non-stop mode, get out of getpkt even if a
7335 notification is received. */
7336
7337 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7338 0 /* forever */, &is_notif);
74531fed
PA
7339 while (1)
7340 {
fee9eda9 7341 if (ret != -1 && !is_notif)
74531fed
PA
7342 switch (rs->buf[0])
7343 {
7344 case 'E': /* Error of some sort. */
7345 /* We're out of sync with the target now. Did it continue
7346 or not? We can't tell which thread it was in non-stop,
7347 so just ignore this. */
7348 warning (_("Remote failure reply: %s"), rs->buf);
7349 break;
7350 case 'O': /* Console output. */
7351 remote_console_output (rs->buf + 1);
7352 break;
7353 default:
7354 warning (_("Invalid remote reply: %s"), rs->buf);
7355 break;
7356 }
7357
7358 /* Acknowledge a pending stop reply that may have arrived in the
7359 mean time. */
f48ff2a7 7360 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 7361 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
7362
7363 /* If indeed we noticed a stop reply, we're done. */
7364 stop_reply = queued_stop_reply (ptid);
7365 if (stop_reply != NULL)
7366 return process_stop_reply (stop_reply, status);
7367
47608cb1 7368 /* Still no event. If we're just polling for an event, then
74531fed 7369 return to the event loop. */
47608cb1 7370 if (options & TARGET_WNOHANG)
74531fed
PA
7371 {
7372 status->kind = TARGET_WAITKIND_IGNORE;
7373 return minus_one_ptid;
7374 }
7375
47608cb1 7376 /* Otherwise do a blocking wait. */
74531fed 7377 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 7378 1 /* forever */, &is_notif);
74531fed
PA
7379 }
7380}
7381
7382/* Wait until the remote machine stops, then return, storing status in
7383 STATUS just as `wait' would. */
7384
7385static ptid_t
47608cb1 7386remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
7387{
7388 struct remote_state *rs = get_remote_state ();
74531fed 7389 ptid_t event_ptid = null_ptid;
cea39f65 7390 char *buf;
74531fed
PA
7391 struct stop_reply *stop_reply;
7392
47608cb1
PA
7393 again:
7394
74531fed
PA
7395 status->kind = TARGET_WAITKIND_IGNORE;
7396 status->value.integer = 0;
7397
7398 stop_reply = queued_stop_reply (ptid);
7399 if (stop_reply != NULL)
7400 return process_stop_reply (stop_reply, status);
7401
7402 if (rs->cached_wait_status)
7403 /* Use the cached wait status, but only once. */
7404 rs->cached_wait_status = 0;
7405 else
7406 {
7407 int ret;
722247f1 7408 int is_notif;
567420d1
PA
7409 int forever = ((options & TARGET_WNOHANG) == 0
7410 && wait_forever_enabled_p);
7411
7412 if (!rs->waiting_for_stop_reply)
7413 {
7414 status->kind = TARGET_WAITKIND_NO_RESUMED;
7415 return minus_one_ptid;
7416 }
74531fed 7417
74531fed
PA
7418 /* FIXME: cagney/1999-09-27: If we're in async mode we should
7419 _never_ wait for ever -> test on target_is_async_p().
7420 However, before we do that we need to ensure that the caller
7421 knows how to take the target into/out of async mode. */
722247f1 7422 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 7423 forever, &is_notif);
722247f1
YQ
7424
7425 /* GDB gets a notification. Return to core as this event is
7426 not interesting. */
7427 if (ret != -1 && is_notif)
7428 return minus_one_ptid;
567420d1
PA
7429
7430 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
7431 return minus_one_ptid;
74531fed
PA
7432 }
7433
7434 buf = rs->buf;
7435
3a29589a
DJ
7436 /* Assume that the target has acknowledged Ctrl-C unless we receive
7437 an 'F' or 'O' packet. */
7438 if (buf[0] != 'F' && buf[0] != 'O')
7439 rs->ctrlc_pending_p = 0;
7440
74531fed
PA
7441 switch (buf[0])
7442 {
7443 case 'E': /* Error of some sort. */
7444 /* We're out of sync with the target now. Did it continue or
7445 not? Not is more likely, so report a stop. */
29090fb6
LM
7446 rs->waiting_for_stop_reply = 0;
7447
74531fed
PA
7448 warning (_("Remote failure reply: %s"), buf);
7449 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 7450 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
7451 break;
7452 case 'F': /* File-I/O request. */
e42e5352
YQ
7453 /* GDB may access the inferior memory while handling the File-I/O
7454 request, but we don't want GDB accessing memory while waiting
7455 for a stop reply. See the comments in putpkt_binary. Set
7456 waiting_for_stop_reply to 0 temporarily. */
7457 rs->waiting_for_stop_reply = 0;
3a29589a
DJ
7458 remote_fileio_request (buf, rs->ctrlc_pending_p);
7459 rs->ctrlc_pending_p = 0;
e42e5352
YQ
7460 /* GDB handled the File-I/O request, and the target is running
7461 again. Keep waiting for events. */
7462 rs->waiting_for_stop_reply = 1;
74531fed 7463 break;
f2faf941 7464 case 'N': case 'T': case 'S': case 'X': case 'W':
74531fed 7465 {
29090fb6
LM
7466 struct stop_reply *stop_reply;
7467
7468 /* There is a stop reply to handle. */
7469 rs->waiting_for_stop_reply = 0;
7470
7471 stop_reply
722247f1
YQ
7472 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
7473 rs->buf);
74531fed 7474
74531fed 7475 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
7476 break;
7477 }
7478 case 'O': /* Console output. */
7479 remote_console_output (buf + 1);
c8e38a49
PA
7480 break;
7481 case '\0':
b73be471 7482 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
7483 {
7484 /* Zero length reply means that we tried 'S' or 'C' and the
7485 remote system doesn't support it. */
223ffa71 7486 target_terminal::ours_for_output ();
c8e38a49
PA
7487 printf_filtered
7488 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
7489 gdb_signal_to_name (rs->last_sent_signal));
7490 rs->last_sent_signal = GDB_SIGNAL_0;
223ffa71 7491 target_terminal::inferior ();
c8e38a49 7492
f5c4fcd9
TT
7493 strcpy (buf, rs->last_sent_step ? "s" : "c");
7494 putpkt (buf);
c8e38a49 7495 break;
43ff13b4 7496 }
c8e38a49
PA
7497 /* else fallthrough */
7498 default:
7499 warning (_("Invalid remote reply: %s"), buf);
c8e38a49 7500 break;
43ff13b4 7501 }
c8e38a49 7502
f2faf941
PA
7503 if (status->kind == TARGET_WAITKIND_NO_RESUMED)
7504 return minus_one_ptid;
7505 else if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
7506 {
7507 /* Nothing interesting happened. If we're doing a non-blocking
7508 poll, we're done. Otherwise, go back to waiting. */
7509 if (options & TARGET_WNOHANG)
7510 return minus_one_ptid;
7511 else
7512 goto again;
7513 }
74531fed
PA
7514 else if (status->kind != TARGET_WAITKIND_EXITED
7515 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
7516 {
7517 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 7518 record_currthread (rs, event_ptid);
82f73884
PA
7519 else
7520 event_ptid = inferior_ptid;
43ff13b4 7521 }
74531fed
PA
7522 else
7523 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 7524 record_currthread (rs, minus_one_ptid);
79d7f229 7525
82f73884 7526 return event_ptid;
43ff13b4
JM
7527}
7528
74531fed
PA
7529/* Wait until the remote machine stops, then return, storing status in
7530 STATUS just as `wait' would. */
7531
c8e38a49 7532static ptid_t
117de6a9 7533remote_wait (struct target_ops *ops,
47608cb1 7534 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
7535{
7536 ptid_t event_ptid;
7537
6efcd9a8 7538 if (target_is_non_stop_p ())
47608cb1 7539 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 7540 else
47608cb1 7541 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 7542
d9d41e78 7543 if (target_is_async_p ())
c8e38a49 7544 {
74531fed
PA
7545 /* If there are are events left in the queue tell the event loop
7546 to return here. */
722247f1 7547 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 7548 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 7549 }
c8e38a49
PA
7550
7551 return event_ptid;
7552}
7553
74ca34ce 7554/* Fetch a single register using a 'p' packet. */
c906108c 7555
b96ec7ac 7556static int
56be3814 7557fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac 7558{
9890e433 7559 struct gdbarch *gdbarch = get_regcache_arch (regcache);
b96ec7ac 7560 struct remote_state *rs = get_remote_state ();
2e9f7625 7561 char *buf, *p;
9890e433 7562 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
b96ec7ac
AC
7563 int i;
7564
4082afcc 7565 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
7566 return 0;
7567
7568 if (reg->pnum == -1)
7569 return 0;
7570
2e9f7625 7571 p = rs->buf;
fcad0fa4 7572 *p++ = 'p';
74ca34ce 7573 p += hexnumstr (p, reg->pnum);
fcad0fa4 7574 *p++ = '\0';
1f4437a4
MS
7575 putpkt (rs->buf);
7576 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 7577
2e9f7625
DJ
7578 buf = rs->buf;
7579
74ca34ce
DJ
7580 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
7581 {
7582 case PACKET_OK:
7583 break;
7584 case PACKET_UNKNOWN:
7585 return 0;
7586 case PACKET_ERROR:
27a9c0bf
MS
7587 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
7588 gdbarch_register_name (get_regcache_arch (regcache),
7589 reg->regnum),
7590 buf);
74ca34ce 7591 }
3f9a994c
JB
7592
7593 /* If this register is unfetchable, tell the regcache. */
7594 if (buf[0] == 'x')
8480adf2 7595 {
56be3814 7596 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 7597 return 1;
b96ec7ac 7598 }
b96ec7ac 7599
3f9a994c
JB
7600 /* Otherwise, parse and supply the value. */
7601 p = buf;
7602 i = 0;
7603 while (p[0] != 0)
7604 {
7605 if (p[1] == 0)
74ca34ce 7606 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
7607
7608 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
7609 p += 2;
7610 }
56be3814 7611 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 7612 return 1;
b96ec7ac
AC
7613}
7614
74ca34ce
DJ
7615/* Fetch the registers included in the target's 'g' packet. */
7616
29709017
DJ
7617static int
7618send_g_packet (void)
c906108c 7619{
d01949b6 7620 struct remote_state *rs = get_remote_state ();
cea39f65 7621 int buf_len;
c906108c 7622
bba74b36 7623 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 7624 remote_send (&rs->buf, &rs->buf_size);
c906108c 7625
29709017
DJ
7626 /* We can get out of synch in various cases. If the first character
7627 in the buffer is not a hex character, assume that has happened
7628 and try to fetch another packet to read. */
7629 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
7630 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
7631 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
7632 && rs->buf[0] != 'x') /* New: unavailable register value. */
7633 {
7634 if (remote_debug)
7635 fprintf_unfiltered (gdb_stdlog,
7636 "Bad register packet; fetching a new packet\n");
7637 getpkt (&rs->buf, &rs->buf_size, 0);
7638 }
7639
74ca34ce
DJ
7640 buf_len = strlen (rs->buf);
7641
7642 /* Sanity check the received packet. */
7643 if (buf_len % 2 != 0)
7644 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
7645
7646 return buf_len / 2;
7647}
7648
7649static void
56be3814 7650process_g_packet (struct regcache *regcache)
29709017 7651{
4a22f64d 7652 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017 7653 struct remote_state *rs = get_remote_state ();
5cd63fda 7654 remote_arch_state *rsa = get_remote_arch_state (gdbarch);
29709017
DJ
7655 int i, buf_len;
7656 char *p;
7657 char *regs;
7658
7659 buf_len = strlen (rs->buf);
7660
7661 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce 7662 if (buf_len > 2 * rsa->sizeof_g_packet)
fc809827
SM
7663 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
7664 "bytes): %s"), rsa->sizeof_g_packet, buf_len / 2, rs->buf);
74ca34ce
DJ
7665
7666 /* Save the size of the packet sent to us by the target. It is used
7667 as a heuristic when determining the max size of packets that the
7668 target can safely receive. */
7669 if (rsa->actual_register_packet_size == 0)
7670 rsa->actual_register_packet_size = buf_len;
7671
7672 /* If this is smaller than we guessed the 'g' packet would be,
7673 update our records. A 'g' reply that doesn't include a register's
7674 value implies either that the register is not available, or that
7675 the 'p' packet must be used. */
7676 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 7677 {
9dc193c3 7678 long sizeof_g_packet = buf_len / 2;
74ca34ce 7679
4a22f64d 7680 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 7681 {
9dc193c3
LF
7682 long offset = rsa->regs[i].offset;
7683 long reg_size = register_size (gdbarch, i);
7684
74ca34ce
DJ
7685 if (rsa->regs[i].pnum == -1)
7686 continue;
7687
9dc193c3 7688 if (offset >= sizeof_g_packet)
74ca34ce 7689 rsa->regs[i].in_g_packet = 0;
9dc193c3
LF
7690 else if (offset + reg_size > sizeof_g_packet)
7691 error (_("Truncated register %d in remote 'g' packet"), i);
b96ec7ac 7692 else
74ca34ce 7693 rsa->regs[i].in_g_packet = 1;
b96ec7ac 7694 }
9dc193c3
LF
7695
7696 /* Looks valid enough, we can assume this is the correct length
7697 for a 'g' packet. It's important not to adjust
7698 rsa->sizeof_g_packet if we have truncated registers otherwise
7699 this "if" won't be run the next time the method is called
7700 with a packet of the same size and one of the internal errors
7701 below will trigger instead. */
7702 rsa->sizeof_g_packet = sizeof_g_packet;
74ca34ce 7703 }
b323314b 7704
224c3ddb 7705 regs = (char *) alloca (rsa->sizeof_g_packet);
c906108c
SS
7706
7707 /* Unimplemented registers read as all bits zero. */
ea9c271d 7708 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 7709
c906108c
SS
7710 /* Reply describes registers byte by byte, each byte encoded as two
7711 hex characters. Suck them all up, then supply them to the
7712 register cacheing/storage mechanism. */
7713
74ca34ce 7714 p = rs->buf;
ea9c271d 7715 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 7716 {
74ca34ce
DJ
7717 if (p[0] == 0 || p[1] == 0)
7718 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
7719 internal_error (__FILE__, __LINE__,
9b20d036 7720 _("unexpected end of 'g' packet reply"));
74ca34ce 7721
c906108c 7722 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 7723 regs[i] = 0; /* 'x' */
c906108c
SS
7724 else
7725 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
7726 p += 2;
7727 }
7728
a744cf53
MS
7729 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
7730 {
7731 struct packet_reg *r = &rsa->regs[i];
9dc193c3 7732 long reg_size = register_size (gdbarch, i);
a744cf53
MS
7733
7734 if (r->in_g_packet)
7735 {
9dc193c3 7736 if ((r->offset + reg_size) * 2 > strlen (rs->buf))
a744cf53
MS
7737 /* This shouldn't happen - we adjusted in_g_packet above. */
7738 internal_error (__FILE__, __LINE__,
9b20d036 7739 _("unexpected end of 'g' packet reply"));
a744cf53
MS
7740 else if (rs->buf[r->offset * 2] == 'x')
7741 {
7742 gdb_assert (r->offset * 2 < strlen (rs->buf));
7743 /* The register isn't available, mark it as such (at
7744 the same time setting the value to zero). */
7745 regcache_raw_supply (regcache, r->regnum, NULL);
7746 }
7747 else
7748 regcache_raw_supply (regcache, r->regnum,
7749 regs + r->offset);
7750 }
7751 }
c906108c
SS
7752}
7753
29709017 7754static void
56be3814 7755fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
7756{
7757 send_g_packet ();
56be3814 7758 process_g_packet (regcache);
29709017
DJ
7759}
7760
e6e4e701
PA
7761/* Make the remote selected traceframe match GDB's selected
7762 traceframe. */
7763
7764static void
7765set_remote_traceframe (void)
7766{
7767 int newnum;
262e1174 7768 struct remote_state *rs = get_remote_state ();
e6e4e701 7769
262e1174 7770 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
7771 return;
7772
7773 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 7774 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
7775
7776 newnum = target_trace_find (tfind_number,
7777 get_traceframe_number (), 0, 0, NULL);
7778
7779 /* Should not happen. If it does, all bets are off. */
7780 if (newnum != get_traceframe_number ())
7781 warning (_("could not set remote traceframe"));
7782}
7783
74ca34ce 7784static void
28439f5e
PA
7785remote_fetch_registers (struct target_ops *ops,
7786 struct regcache *regcache, int regnum)
74ca34ce 7787{
5cd63fda
PA
7788 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7789 remote_arch_state *rsa = get_remote_arch_state (gdbarch);
74ca34ce
DJ
7790 int i;
7791
e6e4e701 7792 set_remote_traceframe ();
bcc0c096 7793 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
7794
7795 if (regnum >= 0)
7796 {
5cd63fda 7797 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 7798
74ca34ce
DJ
7799 gdb_assert (reg != NULL);
7800
7801 /* If this register might be in the 'g' packet, try that first -
7802 we are likely to read more than one register. If this is the
7803 first 'g' packet, we might be overly optimistic about its
7804 contents, so fall back to 'p'. */
7805 if (reg->in_g_packet)
7806 {
56be3814 7807 fetch_registers_using_g (regcache);
74ca34ce
DJ
7808 if (reg->in_g_packet)
7809 return;
7810 }
7811
56be3814 7812 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
7813 return;
7814
7815 /* This register is not available. */
56be3814 7816 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
7817
7818 return;
7819 }
7820
56be3814 7821 fetch_registers_using_g (regcache);
74ca34ce 7822
5cd63fda 7823 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 7824 if (!rsa->regs[i].in_g_packet)
56be3814 7825 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
7826 {
7827 /* This register is not available. */
56be3814 7828 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
7829 }
7830}
7831
c906108c
SS
7832/* Prepare to store registers. Since we may send them all (using a
7833 'G' request), we have to read out the ones we don't want to change
7834 first. */
7835
c5aa993b 7836static void
f32dbf8c 7837remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 7838{
5cd63fda 7839 remote_arch_state *rsa = get_remote_arch_state (regcache->arch ());
cf0e1e0d 7840 int i;
cf0e1e0d 7841
c906108c 7842 /* Make sure the entire registers array is valid. */
4082afcc 7843 switch (packet_support (PACKET_P))
5a2468f5
JM
7844 {
7845 case PACKET_DISABLE:
7846 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 7847 /* Make sure all the necessary registers are cached. */
4a22f64d 7848 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 7849 if (rsa->regs[i].in_g_packet)
8e368124 7850 regcache_raw_update (regcache, rsa->regs[i].regnum);
5a2468f5
JM
7851 break;
7852 case PACKET_ENABLE:
7853 break;
7854 }
7855}
7856
ad10f812 7857/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 7858 packet was not recognized. */
5a2468f5
JM
7859
7860static int
1f4437a4
MS
7861store_register_using_P (const struct regcache *regcache,
7862 struct packet_reg *reg)
5a2468f5 7863{
4a22f64d 7864 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 7865 struct remote_state *rs = get_remote_state ();
5a2468f5 7866 /* Try storing a single register. */
6d820c5c 7867 char *buf = rs->buf;
9890e433 7868 gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
5a2468f5 7869 char *p;
5a2468f5 7870
4082afcc 7871 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
7872 return 0;
7873
7874 if (reg->pnum == -1)
7875 return 0;
7876
ea9c271d 7877 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 7878 p = buf + strlen (buf);
56be3814 7879 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 7880 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
7881 putpkt (rs->buf);
7882 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 7883
74ca34ce
DJ
7884 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
7885 {
7886 case PACKET_OK:
7887 return 1;
7888 case PACKET_ERROR:
27a9c0bf
MS
7889 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
7890 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
7891 case PACKET_UNKNOWN:
7892 return 0;
7893 default:
7894 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7895 }
c906108c
SS
7896}
7897
23860348
MS
7898/* Store register REGNUM, or all registers if REGNUM == -1, from the
7899 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
7900
7901static void
56be3814 7902store_registers_using_G (const struct regcache *regcache)
c906108c 7903{
d01949b6 7904 struct remote_state *rs = get_remote_state ();
5cd63fda 7905 remote_arch_state *rsa = get_remote_arch_state (regcache->arch ());
cfd77fa1 7906 gdb_byte *regs;
c906108c
SS
7907 char *p;
7908
193cb69f
AC
7909 /* Extract all the registers in the regcache copying them into a
7910 local buffer. */
7911 {
b323314b 7912 int i;
a744cf53 7913
224c3ddb 7914 regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
ea9c271d 7915 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 7916 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 7917 {
ea9c271d 7918 struct packet_reg *r = &rsa->regs[i];
a744cf53 7919
b323314b 7920 if (r->in_g_packet)
56be3814 7921 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
7922 }
7923 }
c906108c
SS
7924
7925 /* Command describes registers byte by byte,
7926 each byte encoded as two hex characters. */
6d820c5c 7927 p = rs->buf;
193cb69f 7928 *p++ = 'G';
74ca34ce 7929 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
7930 putpkt (rs->buf);
7931 getpkt (&rs->buf, &rs->buf_size, 0);
7932 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
7933 error (_("Could not write registers; remote failure reply '%s'"),
7934 rs->buf);
c906108c 7935}
74ca34ce
DJ
7936
7937/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
7938 of the register cache buffer. FIXME: ignores errors. */
7939
7940static void
28439f5e
PA
7941remote_store_registers (struct target_ops *ops,
7942 struct regcache *regcache, int regnum)
74ca34ce 7943{
5cd63fda
PA
7944 struct gdbarch *gdbarch = regcache->arch ();
7945 remote_arch_state *rsa = get_remote_arch_state (gdbarch);
74ca34ce
DJ
7946 int i;
7947
e6e4e701 7948 set_remote_traceframe ();
bcc0c096 7949 set_general_thread (regcache_get_ptid (regcache));
74ca34ce
DJ
7950
7951 if (regnum >= 0)
7952 {
5cd63fda 7953 packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
a744cf53 7954
74ca34ce
DJ
7955 gdb_assert (reg != NULL);
7956
7957 /* Always prefer to store registers using the 'P' packet if
7958 possible; we often change only a small number of registers.
7959 Sometimes we change a larger number; we'd need help from a
7960 higher layer to know to use 'G'. */
56be3814 7961 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
7962 return;
7963
7964 /* For now, don't complain if we have no way to write the
7965 register. GDB loses track of unavailable registers too
7966 easily. Some day, this may be an error. We don't have
0df8b418 7967 any way to read the register, either... */
74ca34ce
DJ
7968 if (!reg->in_g_packet)
7969 return;
7970
56be3814 7971 store_registers_using_G (regcache);
74ca34ce
DJ
7972 return;
7973 }
7974
56be3814 7975 store_registers_using_G (regcache);
74ca34ce 7976
5cd63fda 7977 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
74ca34ce 7978 if (!rsa->regs[i].in_g_packet)
56be3814 7979 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
7980 /* See above for why we do not issue an error here. */
7981 continue;
7982}
c906108c
SS
7983\f
7984
7985/* Return the number of hex digits in num. */
7986
7987static int
fba45db2 7988hexnumlen (ULONGEST num)
c906108c
SS
7989{
7990 int i;
7991
7992 for (i = 0; num != 0; i++)
7993 num >>= 4;
7994
325fac50 7995 return std::max (i, 1);
c906108c
SS
7996}
7997
2df3850c 7998/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
7999
8000static int
fba45db2 8001hexnumstr (char *buf, ULONGEST num)
c906108c 8002{
c906108c 8003 int len = hexnumlen (num);
a744cf53 8004
2df3850c
JM
8005 return hexnumnstr (buf, num, len);
8006}
8007
c906108c 8008
2df3850c 8009/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 8010
2df3850c 8011static int
fba45db2 8012hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
8013{
8014 int i;
8015
8016 buf[width] = '\0';
8017
8018 for (i = width - 1; i >= 0; i--)
c906108c 8019 {
c5aa993b 8020 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
8021 num >>= 4;
8022 }
8023
2df3850c 8024 return width;
c906108c
SS
8025}
8026
23860348 8027/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
8028
8029static CORE_ADDR
fba45db2 8030remote_address_masked (CORE_ADDR addr)
c906108c 8031{
883b9c6c 8032 unsigned int address_size = remote_address_size;
a744cf53 8033
911c95a5
UW
8034 /* If "remoteaddresssize" was not set, default to target address size. */
8035 if (!address_size)
f5656ead 8036 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
8037
8038 if (address_size > 0
8039 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
8040 {
8041 /* Only create a mask when that mask can safely be constructed
23860348 8042 in a ULONGEST variable. */
c906108c 8043 ULONGEST mask = 1;
a744cf53 8044
911c95a5 8045 mask = (mask << address_size) - 1;
c906108c
SS
8046 addr &= mask;
8047 }
8048 return addr;
8049}
8050
8051/* Determine whether the remote target supports binary downloading.
8052 This is accomplished by sending a no-op memory write of zero length
8053 to the target at the specified address. It does not suffice to send
23860348
MS
8054 the whole packet, since many stubs strip the eighth bit and
8055 subsequently compute a wrong checksum, which causes real havoc with
8056 remote_write_bytes.
7a292a7a 8057
96baa820 8058 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 8059 clean. In cases like this, the user should clear "remote
23860348 8060 X-packet". */
96baa820 8061
c906108c 8062static void
fba45db2 8063check_binary_download (CORE_ADDR addr)
c906108c 8064{
d01949b6 8065 struct remote_state *rs = get_remote_state ();
24b06219 8066
4082afcc 8067 switch (packet_support (PACKET_X))
c906108c 8068 {
96baa820
JM
8069 case PACKET_DISABLE:
8070 break;
8071 case PACKET_ENABLE:
8072 break;
8073 case PACKET_SUPPORT_UNKNOWN:
8074 {
96baa820 8075 char *p;
802188a7 8076
2e9f7625 8077 p = rs->buf;
96baa820
JM
8078 *p++ = 'X';
8079 p += hexnumstr (p, (ULONGEST) addr);
8080 *p++ = ',';
8081 p += hexnumstr (p, (ULONGEST) 0);
8082 *p++ = ':';
8083 *p = '\0';
802188a7 8084
2e9f7625 8085 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8086 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 8087
2e9f7625 8088 if (rs->buf[0] == '\0')
96baa820
JM
8089 {
8090 if (remote_debug)
8091 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8092 "binary downloading NOT "
8093 "supported by target\n");
444abaca 8094 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
8095 }
8096 else
8097 {
8098 if (remote_debug)
8099 fprintf_unfiltered (gdb_stdlog,
64b9b334 8100 "binary downloading supported by target\n");
444abaca 8101 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
8102 }
8103 break;
8104 }
c906108c
SS
8105 }
8106}
8107
124e13d9
SM
8108/* Helper function to resize the payload in order to try to get a good
8109 alignment. We try to write an amount of data such that the next write will
8110 start on an address aligned on REMOTE_ALIGN_WRITES. */
8111
8112static int
8113align_for_efficient_write (int todo, CORE_ADDR memaddr)
8114{
8115 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
8116}
8117
c906108c
SS
8118/* Write memory data directly to the remote machine.
8119 This does not inform the data cache; the data cache uses this.
a76d924d 8120 HEADER is the starting part of the packet.
c906108c
SS
8121 MEMADDR is the address in the remote memory space.
8122 MYADDR is the address of the buffer in our space.
124e13d9
SM
8123 LEN_UNITS is the number of addressable units to write.
8124 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
8125 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8126 should send data as binary ('X'), or hex-encoded ('M').
8127
8128 The function creates packet of the form
8129 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8130
124e13d9 8131 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
8132
8133 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8134 are omitted.
8135
9b409511 8136 Return the transferred status, error or OK (an
124e13d9
SM
8137 'enum target_xfer_status' value). Save the number of addressable units
8138 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8139
8140 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8141 exchange between gdb and the stub could look like (?? in place of the
8142 checksum):
8143
8144 -> $m1000,4#??
8145 <- aaaabbbbccccdddd
8146
8147 -> $M1000,3:eeeeffffeeee#??
8148 <- OK
8149
8150 -> $m1000,4#??
8151 <- eeeeffffeeeedddd */
c906108c 8152
9b409511 8153static enum target_xfer_status
a76d924d 8154remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
8155 const gdb_byte *myaddr, ULONGEST len_units,
8156 int unit_size, ULONGEST *xfered_len_units,
8157 char packet_format, int use_length)
c906108c 8158{
6d820c5c 8159 struct remote_state *rs = get_remote_state ();
cfd77fa1 8160 char *p;
a76d924d
DJ
8161 char *plen = NULL;
8162 int plenlen = 0;
124e13d9
SM
8163 int todo_units;
8164 int units_written;
8165 int payload_capacity_bytes;
8166 int payload_length_bytes;
a76d924d
DJ
8167
8168 if (packet_format != 'X' && packet_format != 'M')
8169 internal_error (__FILE__, __LINE__,
9b20d036 8170 _("remote_write_bytes_aux: bad packet format"));
c906108c 8171
124e13d9 8172 if (len_units == 0)
9b409511 8173 return TARGET_XFER_EOF;
b2182ed2 8174
124e13d9 8175 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 8176
6d820c5c
DJ
8177 /* The packet buffer will be large enough for the payload;
8178 get_memory_packet_size ensures this. */
a76d924d 8179 rs->buf[0] = '\0';
c906108c 8180
a257b5bb 8181 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
8182 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
8183
124e13d9 8184 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 8185 if (!use_length)
0df8b418 8186 /* The comma won't be used. */
124e13d9
SM
8187 payload_capacity_bytes += 1;
8188 payload_capacity_bytes -= strlen (header);
8189 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 8190
a76d924d 8191 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 8192
a76d924d
DJ
8193 strcat (rs->buf, header);
8194 p = rs->buf + strlen (header);
8195
8196 /* Compute a best guess of the number of bytes actually transfered. */
8197 if (packet_format == 'X')
c906108c 8198 {
23860348 8199 /* Best guess at number of bytes that will fit. */
325fac50
PA
8200 todo_units = std::min (len_units,
8201 (ULONGEST) payload_capacity_bytes / unit_size);
a76d924d 8202 if (use_length)
124e13d9 8203 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50 8204 todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
8205 }
8206 else
8207 {
124e13d9 8208 /* Number of bytes that will fit. */
325fac50
PA
8209 todo_units
8210 = std::min (len_units,
8211 (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
a76d924d 8212 if (use_length)
124e13d9 8213 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50
PA
8214 todo_units = std::min (todo_units,
8215 (payload_capacity_bytes / unit_size) / 2);
917317f4 8216 }
a76d924d 8217
124e13d9 8218 if (todo_units <= 0)
3de11b2e 8219 internal_error (__FILE__, __LINE__,
405f8e94 8220 _("minimum packet size too small to write data"));
802188a7 8221
6765f3e5
DJ
8222 /* If we already need another packet, then try to align the end
8223 of this packet to a useful boundary. */
124e13d9
SM
8224 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
8225 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 8226
a257b5bb 8227 /* Append "<memaddr>". */
917317f4
JM
8228 memaddr = remote_address_masked (memaddr);
8229 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 8230
a76d924d
DJ
8231 if (use_length)
8232 {
8233 /* Append ",". */
8234 *p++ = ',';
802188a7 8235
124e13d9
SM
8236 /* Append the length and retain its location and size. It may need to be
8237 adjusted once the packet body has been created. */
a76d924d 8238 plen = p;
124e13d9 8239 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
8240 p += plenlen;
8241 }
a257b5bb
AC
8242
8243 /* Append ":". */
917317f4
JM
8244 *p++ = ':';
8245 *p = '\0';
802188a7 8246
a257b5bb 8247 /* Append the packet body. */
a76d924d 8248 if (packet_format == 'X')
917317f4 8249 {
917317f4
JM
8250 /* Binary mode. Send target system values byte by byte, in
8251 increasing byte addresses. Only escape certain critical
8252 characters. */
124e13d9
SM
8253 payload_length_bytes =
8254 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
8255 &units_written, payload_capacity_bytes);
6765f3e5 8256
124e13d9 8257 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
8258 a second try to keep the end of the packet aligned. Don't do
8259 this if the packet is tiny. */
124e13d9 8260 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 8261 {
124e13d9
SM
8262 int new_todo_units;
8263
8264 new_todo_units = align_for_efficient_write (units_written, memaddr);
8265
8266 if (new_todo_units != units_written)
8267 payload_length_bytes =
8268 remote_escape_output (myaddr, new_todo_units, unit_size,
8269 (gdb_byte *) p, &units_written,
8270 payload_capacity_bytes);
6765f3e5
DJ
8271 }
8272
124e13d9
SM
8273 p += payload_length_bytes;
8274 if (use_length && units_written < todo_units)
c906108c 8275 {
802188a7 8276 /* Escape chars have filled up the buffer prematurely,
124e13d9 8277 and we have actually sent fewer units than planned.
917317f4
JM
8278 Fix-up the length field of the packet. Use the same
8279 number of characters as before. */
124e13d9
SM
8280 plen += hexnumnstr (plen, (ULONGEST) units_written,
8281 plenlen);
917317f4 8282 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 8283 }
a76d924d
DJ
8284 }
8285 else
8286 {
917317f4
JM
8287 /* Normal mode: Send target system values byte by byte, in
8288 increasing byte addresses. Each byte is encoded as a two hex
8289 value. */
124e13d9
SM
8290 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
8291 units_written = todo_units;
c906108c 8292 }
802188a7 8293
2e9f7625 8294 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 8295 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8296
2e9f7625 8297 if (rs->buf[0] == 'E')
00d84524 8298 return TARGET_XFER_E_IO;
802188a7 8299
124e13d9
SM
8300 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
8301 send fewer units than we'd planned. */
8302 *xfered_len_units = (ULONGEST) units_written;
9b409511 8303 return TARGET_XFER_OK;
c906108c
SS
8304}
8305
a76d924d
DJ
8306/* Write memory data directly to the remote machine.
8307 This does not inform the data cache; the data cache uses this.
8308 MEMADDR is the address in the remote memory space.
8309 MYADDR is the address of the buffer in our space.
8310 LEN is the number of bytes.
8311
9b409511
YQ
8312 Return the transferred status, error or OK (an
8313 'enum target_xfer_status' value). Save the number of bytes
8314 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 8315
9b409511
YQ
8316static enum target_xfer_status
8317remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 8318 int unit_size, ULONGEST *xfered_len)
a76d924d 8319{
a121b7c1 8320 const char *packet_format = NULL;
a76d924d
DJ
8321
8322 /* Check whether the target supports binary download. */
8323 check_binary_download (memaddr);
8324
4082afcc 8325 switch (packet_support (PACKET_X))
a76d924d
DJ
8326 {
8327 case PACKET_ENABLE:
8328 packet_format = "X";
8329 break;
8330 case PACKET_DISABLE:
8331 packet_format = "M";
8332 break;
8333 case PACKET_SUPPORT_UNKNOWN:
8334 internal_error (__FILE__, __LINE__,
8335 _("remote_write_bytes: bad internal state"));
8336 default:
8337 internal_error (__FILE__, __LINE__, _("bad switch"));
8338 }
8339
8340 return remote_write_bytes_aux (packet_format,
124e13d9 8341 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 8342 packet_format[0], 1);
a76d924d
DJ
8343}
8344
9217e74e
YQ
8345/* Read memory data directly from the remote machine.
8346 This does not use the data cache; the data cache uses this.
8347 MEMADDR is the address in the remote memory space.
8348 MYADDR is the address of the buffer in our space.
124e13d9
SM
8349 LEN_UNITS is the number of addressable memory units to read..
8350 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
8351
8352 Return the transferred status, error or OK (an
8353 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
8354 transferred in *XFERED_LEN_UNITS.
8355
8356 See the comment of remote_write_bytes_aux for an example of
8357 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
8358
8359static enum target_xfer_status
124e13d9
SM
8360remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
8361 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
8362{
8363 struct remote_state *rs = get_remote_state ();
124e13d9 8364 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 8365 char *p;
124e13d9
SM
8366 int todo_units;
8367 int decoded_bytes;
9217e74e 8368
124e13d9 8369 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
8370 /* The packet buffer will be large enough for the payload;
8371 get_memory_packet_size ensures this. */
8372
124e13d9 8373 /* Number of units that will fit. */
325fac50
PA
8374 todo_units = std::min (len_units,
8375 (ULONGEST) (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
8376
8377 /* Construct "m"<memaddr>","<len>". */
8378 memaddr = remote_address_masked (memaddr);
8379 p = rs->buf;
8380 *p++ = 'm';
8381 p += hexnumstr (p, (ULONGEST) memaddr);
8382 *p++ = ',';
124e13d9 8383 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
8384 *p = '\0';
8385 putpkt (rs->buf);
8386 getpkt (&rs->buf, &rs->buf_size, 0);
8387 if (rs->buf[0] == 'E'
8388 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
8389 && rs->buf[3] == '\0')
8390 return TARGET_XFER_E_IO;
8391 /* Reply describes memory byte by byte, each byte encoded as two hex
8392 characters. */
8393 p = rs->buf;
124e13d9 8394 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 8395 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 8396 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
8397 return TARGET_XFER_OK;
8398}
8399
b55fbac4
YQ
8400/* Using the set of read-only target sections of remote, read live
8401 read-only memory.
8acf9577
YQ
8402
8403 For interface/parameters/return description see target.h,
8404 to_xfer_partial. */
8405
8406static enum target_xfer_status
b55fbac4
YQ
8407remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
8408 ULONGEST memaddr, ULONGEST len,
124e13d9 8409 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
8410{
8411 struct target_section *secp;
8412 struct target_section_table *table;
8413
8414 secp = target_section_by_addr (ops, memaddr);
8415 if (secp != NULL
8416 && (bfd_get_section_flags (secp->the_bfd_section->owner,
8417 secp->the_bfd_section)
8418 & SEC_READONLY))
8419 {
8420 struct target_section *p;
8421 ULONGEST memend = memaddr + len;
8422
8423 table = target_get_section_table (ops);
8424
8425 for (p = table->sections; p < table->sections_end; p++)
8426 {
8427 if (memaddr >= p->addr)
8428 {
8429 if (memend <= p->endaddr)
8430 {
8431 /* Entire transfer is within this section. */
124e13d9 8432 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8433 xfered_len);
8acf9577
YQ
8434 }
8435 else if (memaddr >= p->endaddr)
8436 {
8437 /* This section ends before the transfer starts. */
8438 continue;
8439 }
8440 else
8441 {
8442 /* This section overlaps the transfer. Just do half. */
8443 len = p->endaddr - memaddr;
124e13d9 8444 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 8445 xfered_len);
8acf9577
YQ
8446 }
8447 }
8448 }
8449 }
8450
8451 return TARGET_XFER_EOF;
8452}
8453
9217e74e
YQ
8454/* Similar to remote_read_bytes_1, but it reads from the remote stub
8455 first if the requested memory is unavailable in traceframe.
8456 Otherwise, fall back to remote_read_bytes_1. */
c906108c 8457
9b409511 8458static enum target_xfer_status
8acf9577 8459remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
8460 gdb_byte *myaddr, ULONGEST len, int unit_size,
8461 ULONGEST *xfered_len)
c906108c 8462{
6b6aa828 8463 if (len == 0)
96c4f946 8464 return TARGET_XFER_EOF;
b2182ed2 8465
8acf9577
YQ
8466 if (get_traceframe_number () != -1)
8467 {
8468 VEC(mem_range_s) *available;
8469
8470 /* If we fail to get the set of available memory, then the
8471 target does not support querying traceframe info, and so we
8472 attempt reading from the traceframe anyway (assuming the
8473 target implements the old QTro packet then). */
8474 if (traceframe_available_memory (&available, memaddr, len))
8475 {
8476 struct cleanup *old_chain;
8477
8478 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
8479
8480 if (VEC_empty (mem_range_s, available)
8481 || VEC_index (mem_range_s, available, 0)->start != memaddr)
8482 {
8483 enum target_xfer_status res;
8484
8485 /* Don't read into the traceframe's available
8486 memory. */
8487 if (!VEC_empty (mem_range_s, available))
8488 {
8489 LONGEST oldlen = len;
8490
8491 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
8492 gdb_assert (len <= oldlen);
8493 }
8494
8495 do_cleanups (old_chain);
8496
8497 /* This goes through the topmost target again. */
b55fbac4 8498 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 8499 len, unit_size, xfered_len);
8acf9577
YQ
8500 if (res == TARGET_XFER_OK)
8501 return TARGET_XFER_OK;
8502 else
8503 {
8504 /* No use trying further, we know some memory starting
8505 at MEMADDR isn't available. */
8506 *xfered_len = len;
8507 return TARGET_XFER_UNAVAILABLE;
8508 }
8509 }
8510
8511 /* Don't try to read more than how much is available, in
8512 case the target implements the deprecated QTro packet to
8513 cater for older GDBs (the target's knowledge of read-only
8514 sections may be outdated by now). */
8515 len = VEC_index (mem_range_s, available, 0)->length;
8516
8517 do_cleanups (old_chain);
8518 }
8519 }
8520
124e13d9 8521 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 8522}
74531fed 8523
c906108c 8524\f
c906108c 8525
a76d924d
DJ
8526/* Sends a packet with content determined by the printf format string
8527 FORMAT and the remaining arguments, then gets the reply. Returns
8528 whether the packet was a success, a failure, or unknown. */
8529
77b64a49
PA
8530static enum packet_result remote_send_printf (const char *format, ...)
8531 ATTRIBUTE_PRINTF (1, 2);
8532
2c0b251b 8533static enum packet_result
a76d924d
DJ
8534remote_send_printf (const char *format, ...)
8535{
8536 struct remote_state *rs = get_remote_state ();
8537 int max_size = get_remote_packet_size ();
a76d924d 8538 va_list ap;
a744cf53 8539
a76d924d
DJ
8540 va_start (ap, format);
8541
8542 rs->buf[0] = '\0';
8543 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 8544 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
8545
8546 if (putpkt (rs->buf) < 0)
8547 error (_("Communication problem with target."));
8548
8549 rs->buf[0] = '\0';
8550 getpkt (&rs->buf, &rs->buf_size, 0);
8551
8552 return packet_check_result (rs->buf);
8553}
8554
a76d924d
DJ
8555/* Flash writing can take quite some time. We'll set
8556 effectively infinite timeout for flash operations.
8557 In future, we'll need to decide on a better approach. */
8558static const int remote_flash_timeout = 1000;
8559
8560static void
8561remote_flash_erase (struct target_ops *ops,
8562 ULONGEST address, LONGEST length)
8563{
f5656ead 8564 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d 8565 enum packet_result ret;
2ec845e7
TT
8566 scoped_restore restore_timeout
8567 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
a76d924d
DJ
8568
8569 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 8570 phex (address, addr_size),
a76d924d
DJ
8571 phex (length, 4));
8572 switch (ret)
8573 {
8574 case PACKET_UNKNOWN:
8575 error (_("Remote target does not support flash erase"));
8576 case PACKET_ERROR:
8577 error (_("Error erasing flash with vFlashErase packet"));
8578 default:
8579 break;
8580 }
a76d924d
DJ
8581}
8582
9b409511
YQ
8583static enum target_xfer_status
8584remote_flash_write (struct target_ops *ops, ULONGEST address,
8585 ULONGEST length, ULONGEST *xfered_len,
8586 const gdb_byte *data)
a76d924d 8587{
2ec845e7
TT
8588 scoped_restore restore_timeout
8589 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8590 return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
8591 xfered_len,'X', 0);
a76d924d
DJ
8592}
8593
8594static void
8595remote_flash_done (struct target_ops *ops)
8596{
a76d924d 8597 int ret;
a76d924d 8598
2ec845e7
TT
8599 scoped_restore restore_timeout
8600 = make_scoped_restore (&remote_timeout, remote_flash_timeout);
8601
a76d924d 8602 ret = remote_send_printf ("vFlashDone");
a76d924d
DJ
8603
8604 switch (ret)
8605 {
8606 case PACKET_UNKNOWN:
8607 error (_("Remote target does not support vFlashDone"));
8608 case PACKET_ERROR:
8609 error (_("Error finishing flash operation"));
8610 default:
8611 break;
8612 }
8613}
8614
c906108c 8615static void
fba45db2 8616remote_files_info (struct target_ops *ignore)
c906108c
SS
8617{
8618 puts_filtered ("Debugging a target over a serial line.\n");
8619}
8620\f
8621/* Stuff for dealing with the packets which are part of this protocol.
8622 See comment at top of file for details. */
8623
1927e618
PA
8624/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
8625 error to higher layers. Called when a serial error is detected.
8626 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
8627 the system error message for errno at function entry and final dot
8628 for output compatibility with throw_perror_with_name. */
1927e618
PA
8629
8630static void
8631unpush_and_perror (const char *string)
8632{
d6cb50a2 8633 int saved_errno = errno;
1927e618
PA
8634
8635 remote_unpush_target ();
d6cb50a2
JK
8636 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
8637 safe_strerror (saved_errno));
1927e618
PA
8638}
8639
048094ac
PA
8640/* Read a single character from the remote end. The current quit
8641 handler is overridden to avoid quitting in the middle of packet
8642 sequence, as that would break communication with the remote server.
8643 See remote_serial_quit_handler for more detail. */
c906108c
SS
8644
8645static int
fba45db2 8646readchar (int timeout)
c906108c
SS
8647{
8648 int ch;
5d93a237 8649 struct remote_state *rs = get_remote_state ();
048094ac 8650
2ec845e7
TT
8651 {
8652 scoped_restore restore_quit
8653 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
c906108c 8654
2ec845e7 8655 rs->got_ctrlc_during_io = 0;
c906108c 8656
2ec845e7 8657 ch = serial_readchar (rs->remote_desc, timeout);
048094ac 8658
2ec845e7
TT
8659 if (rs->got_ctrlc_during_io)
8660 set_quit_flag ();
8661 }
048094ac 8662
2acceee2 8663 if (ch >= 0)
0876f84a 8664 return ch;
2acceee2
JM
8665
8666 switch ((enum serial_rc) ch)
c906108c
SS
8667 {
8668 case SERIAL_EOF:
78a095c3 8669 remote_unpush_target ();
598d3636 8670 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 8671 /* no return */
c906108c 8672 case SERIAL_ERROR:
1927e618
PA
8673 unpush_and_perror (_("Remote communication error. "
8674 "Target disconnected."));
2acceee2 8675 /* no return */
c906108c 8676 case SERIAL_TIMEOUT:
2acceee2 8677 break;
c906108c 8678 }
2acceee2 8679 return ch;
c906108c
SS
8680}
8681
c33e31fd 8682/* Wrapper for serial_write that closes the target and throws if
048094ac
PA
8683 writing fails. The current quit handler is overridden to avoid
8684 quitting in the middle of packet sequence, as that would break
8685 communication with the remote server. See
8686 remote_serial_quit_handler for more detail. */
c33e31fd
PA
8687
8688static void
8689remote_serial_write (const char *str, int len)
8690{
5d93a237 8691 struct remote_state *rs = get_remote_state ();
048094ac 8692
2ec845e7
TT
8693 scoped_restore restore_quit
8694 = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
048094ac
PA
8695
8696 rs->got_ctrlc_during_io = 0;
5d93a237
TT
8697
8698 if (serial_write (rs->remote_desc, str, len))
c33e31fd 8699 {
1927e618
PA
8700 unpush_and_perror (_("Remote communication error. "
8701 "Target disconnected."));
c33e31fd 8702 }
048094ac
PA
8703
8704 if (rs->got_ctrlc_during_io)
8705 set_quit_flag ();
c33e31fd
PA
8706}
8707
6d820c5c
DJ
8708/* Send the command in *BUF to the remote machine, and read the reply
8709 into *BUF. Report an error if we get an error reply. Resize
8710 *BUF using xrealloc if necessary to hold the result, and update
8711 *SIZEOF_BUF. */
c906108c
SS
8712
8713static void
6d820c5c
DJ
8714remote_send (char **buf,
8715 long *sizeof_buf)
c906108c 8716{
6d820c5c 8717 putpkt (*buf);
c2d11a7d 8718 getpkt (buf, sizeof_buf, 0);
c906108c 8719
6d820c5c
DJ
8720 if ((*buf)[0] == 'E')
8721 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
8722}
8723
b3ced9ba
PA
8724/* Return a string representing an escaped version of BUF, of len N.
8725 E.g. \n is converted to \\n, \t to \\t, etc. */
6e5abd65 8726
b3ced9ba 8727static std::string
6e5abd65
PA
8728escape_buffer (const char *buf, int n)
8729{
d7e74731 8730 string_file stb;
6e5abd65 8731
d7e74731
PA
8732 stb.putstrn (buf, n, '\\');
8733 return std::move (stb.string ());
6e5abd65
PA
8734}
8735
c906108c
SS
8736/* Display a null-terminated packet on stdout, for debugging, using C
8737 string notation. */
8738
8739static void
baa336ce 8740print_packet (const char *buf)
c906108c
SS
8741{
8742 puts_filtered ("\"");
43e526b9 8743 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
8744 puts_filtered ("\"");
8745}
8746
8747int
baa336ce 8748putpkt (const char *buf)
c906108c
SS
8749{
8750 return putpkt_binary (buf, strlen (buf));
8751}
8752
8753/* Send a packet to the remote machine, with error checking. The data
23860348 8754 of the packet is in BUF. The string in BUF can be at most
ea9c271d 8755 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
8756 and for a possible /0 if we are debugging (remote_debug) and want
8757 to print the sent packet as a string. */
c906108c
SS
8758
8759static int
baa336ce 8760putpkt_binary (const char *buf, int cnt)
c906108c 8761{
2d717e4f 8762 struct remote_state *rs = get_remote_state ();
c906108c
SS
8763 int i;
8764 unsigned char csum = 0;
224c3ddb 8765 char *buf2 = (char *) xmalloc (cnt + 6);
a5c0808e 8766 struct cleanup *old_chain = make_cleanup (xfree, buf2);
085dd6e6 8767
c906108c
SS
8768 int ch;
8769 int tcount = 0;
8770 char *p;
8771
e24a49d8
PA
8772 /* Catch cases like trying to read memory or listing threads while
8773 we're waiting for a stop reply. The remote server wouldn't be
8774 ready to handle this request, so we'd hang and timeout. We don't
8775 have to worry about this in synchronous mode, because in that
8776 case it's not possible to issue a command while the target is
74531fed
PA
8777 running. This is not a problem in non-stop mode, because in that
8778 case, the stub is always ready to process serial input. */
6efcd9a8
PA
8779 if (!target_is_non_stop_p ()
8780 && target_is_async_p ()
8781 && rs->waiting_for_stop_reply)
9597b22a
DE
8782 {
8783 error (_("Cannot execute this command while the target is running.\n"
8784 "Use the \"interrupt\" command to stop the target\n"
8785 "and then try again."));
8786 }
e24a49d8 8787
2d717e4f
DJ
8788 /* We're sending out a new packet. Make sure we don't look at a
8789 stale cached response. */
8790 rs->cached_wait_status = 0;
8791
c906108c
SS
8792 /* Copy the packet into buffer BUF2, encapsulating it
8793 and giving it a checksum. */
8794
c906108c
SS
8795 p = buf2;
8796 *p++ = '$';
8797
8798 for (i = 0; i < cnt; i++)
8799 {
8800 csum += buf[i];
8801 *p++ = buf[i];
8802 }
8803 *p++ = '#';
8804 *p++ = tohex ((csum >> 4) & 0xf);
8805 *p++ = tohex (csum & 0xf);
8806
8807 /* Send it over and over until we get a positive ack. */
8808
8809 while (1)
8810 {
8811 int started_error_output = 0;
8812
8813 if (remote_debug)
8814 {
8815 *p = '\0';
b3ced9ba 8816
6f8976bf
YQ
8817 int len = (int) (p - buf2);
8818
8819 std::string str
8820 = escape_buffer (buf2, std::min (len, REMOTE_DEBUG_MAX_CHAR));
8821
8822 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s", str.c_str ());
8823
8824 if (str.length () > REMOTE_DEBUG_MAX_CHAR)
8825 {
8826 fprintf_unfiltered (gdb_stdlog, "[%zu bytes omitted]",
8827 str.length () - REMOTE_DEBUG_MAX_CHAR);
8828 }
8829
8830 fprintf_unfiltered (gdb_stdlog, "...");
b3ced9ba 8831
0f71a2f6 8832 gdb_flush (gdb_stdlog);
c906108c 8833 }
c33e31fd 8834 remote_serial_write (buf2, p - buf2);
c906108c 8835
a6f3e723
SL
8836 /* If this is a no acks version of the remote protocol, send the
8837 packet and move on. */
8838 if (rs->noack_mode)
8839 break;
8840
74531fed
PA
8841 /* Read until either a timeout occurs (-2) or '+' is read.
8842 Handle any notification that arrives in the mean time. */
c906108c
SS
8843 while (1)
8844 {
8845 ch = readchar (remote_timeout);
8846
c5aa993b 8847 if (remote_debug)
c906108c
SS
8848 {
8849 switch (ch)
8850 {
8851 case '+':
1216fa2c 8852 case '-':
c906108c
SS
8853 case SERIAL_TIMEOUT:
8854 case '$':
74531fed 8855 case '%':
c906108c
SS
8856 if (started_error_output)
8857 {
8858 putchar_unfiltered ('\n');
8859 started_error_output = 0;
8860 }
8861 }
8862 }
8863
8864 switch (ch)
8865 {
8866 case '+':
8867 if (remote_debug)
0f71a2f6 8868 fprintf_unfiltered (gdb_stdlog, "Ack\n");
a5c0808e 8869 do_cleanups (old_chain);
c906108c 8870 return 1;
1216fa2c
AC
8871 case '-':
8872 if (remote_debug)
8873 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 8874 /* FALLTHROUGH */
c906108c 8875 case SERIAL_TIMEOUT:
c5aa993b 8876 tcount++;
c906108c 8877 if (tcount > 3)
a5c0808e
PA
8878 {
8879 do_cleanups (old_chain);
8880 return 0;
8881 }
23860348 8882 break; /* Retransmit buffer. */
c906108c
SS
8883 case '$':
8884 {
40e3f985 8885 if (remote_debug)
2bc416ba 8886 fprintf_unfiltered (gdb_stdlog,
23860348 8887 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
8888 /* It's probably an old response sent because an ACK
8889 was lost. Gobble up the packet and ack it so it
8890 doesn't get retransmitted when we resend this
8891 packet. */
6d820c5c 8892 skip_frame ();
c33e31fd 8893 remote_serial_write ("+", 1);
23860348 8894 continue; /* Now, go look for +. */
c906108c 8895 }
74531fed
PA
8896
8897 case '%':
8898 {
8899 int val;
8900
8901 /* If we got a notification, handle it, and go back to looking
8902 for an ack. */
8903 /* We've found the start of a notification. Now
8904 collect the data. */
8905 val = read_frame (&rs->buf, &rs->buf_size);
8906 if (val >= 0)
8907 {
8908 if (remote_debug)
8909 {
b3ced9ba 8910 std::string str = escape_buffer (rs->buf, val);
6e5abd65 8911
6e5abd65
PA
8912 fprintf_unfiltered (gdb_stdlog,
8913 " Notification received: %s\n",
b3ced9ba 8914 str.c_str ());
74531fed 8915 }
5965e028 8916 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
8917 /* We're in sync now, rewait for the ack. */
8918 tcount = 0;
8919 }
8920 else
8921 {
8922 if (remote_debug)
8923 {
8924 if (!started_error_output)
8925 {
8926 started_error_output = 1;
8927 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
8928 }
8929 fputc_unfiltered (ch & 0177, gdb_stdlog);
8930 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
8931 }
8932 }
8933 continue;
8934 }
8935 /* fall-through */
c906108c
SS
8936 default:
8937 if (remote_debug)
8938 {
8939 if (!started_error_output)
8940 {
8941 started_error_output = 1;
0f71a2f6 8942 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 8943 }
0f71a2f6 8944 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
8945 }
8946 continue;
8947 }
23860348 8948 break; /* Here to retransmit. */
c906108c
SS
8949 }
8950
8951#if 0
8952 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
8953 able to get out next time we call QUIT, without anything as
8954 violent as interrupt_query. If we want to provide a way out of
8955 here without getting to the next QUIT, it should be based on
8956 hitting ^C twice as in remote_wait. */
c906108c
SS
8957 if (quit_flag)
8958 {
8959 quit_flag = 0;
8960 interrupt_query ();
8961 }
8962#endif
8963 }
a5c0808e
PA
8964
8965 do_cleanups (old_chain);
a6f3e723 8966 return 0;
c906108c
SS
8967}
8968
6d820c5c
DJ
8969/* Come here after finding the start of a frame when we expected an
8970 ack. Do our best to discard the rest of this packet. */
8971
8972static void
8973skip_frame (void)
8974{
8975 int c;
8976
8977 while (1)
8978 {
8979 c = readchar (remote_timeout);
8980 switch (c)
8981 {
8982 case SERIAL_TIMEOUT:
8983 /* Nothing we can do. */
8984 return;
8985 case '#':
8986 /* Discard the two bytes of checksum and stop. */
8987 c = readchar (remote_timeout);
8988 if (c >= 0)
8989 c = readchar (remote_timeout);
8990
8991 return;
8992 case '*': /* Run length encoding. */
8993 /* Discard the repeat count. */
8994 c = readchar (remote_timeout);
8995 if (c < 0)
8996 return;
8997 break;
8998 default:
8999 /* A regular character. */
9000 break;
9001 }
9002 }
9003}
9004
c906108c 9005/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
9006 into *BUF, verifying the checksum, length, and handling run-length
9007 compression. NUL terminate the buffer. If there is not enough room,
9008 expand *BUF using xrealloc.
c906108c 9009
c2d11a7d
JM
9010 Returns -1 on error, number of characters in buffer (ignoring the
9011 trailing NULL) on success. (could be extended to return one of the
23860348 9012 SERIAL status indications). */
c2d11a7d
JM
9013
9014static long
6d820c5c
DJ
9015read_frame (char **buf_p,
9016 long *sizeof_buf)
c906108c
SS
9017{
9018 unsigned char csum;
c2d11a7d 9019 long bc;
c906108c 9020 int c;
6d820c5c 9021 char *buf = *buf_p;
a6f3e723 9022 struct remote_state *rs = get_remote_state ();
c906108c
SS
9023
9024 csum = 0;
c2d11a7d 9025 bc = 0;
c906108c
SS
9026
9027 while (1)
9028 {
9029 c = readchar (remote_timeout);
c906108c
SS
9030 switch (c)
9031 {
9032 case SERIAL_TIMEOUT:
9033 if (remote_debug)
0f71a2f6 9034 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 9035 return -1;
c906108c
SS
9036 case '$':
9037 if (remote_debug)
0f71a2f6
JM
9038 fputs_filtered ("Saw new packet start in middle of old one\n",
9039 gdb_stdlog);
23860348 9040 return -1; /* Start a new packet, count retries. */
c906108c
SS
9041 case '#':
9042 {
9043 unsigned char pktcsum;
e1b09194
AC
9044 int check_0 = 0;
9045 int check_1 = 0;
c906108c 9046
c2d11a7d 9047 buf[bc] = '\0';
c906108c 9048
e1b09194
AC
9049 check_0 = readchar (remote_timeout);
9050 if (check_0 >= 0)
9051 check_1 = readchar (remote_timeout);
802188a7 9052
e1b09194
AC
9053 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
9054 {
9055 if (remote_debug)
2bc416ba 9056 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 9057 gdb_stdlog);
e1b09194
AC
9058 return -1;
9059 }
9060 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
9061 {
9062 if (remote_debug)
2bc416ba 9063 fputs_filtered ("Communication error in checksum\n",
23860348 9064 gdb_stdlog);
40e3f985
FN
9065 return -1;
9066 }
c906108c 9067
a6f3e723
SL
9068 /* Don't recompute the checksum; with no ack packets we
9069 don't have any way to indicate a packet retransmission
9070 is necessary. */
9071 if (rs->noack_mode)
9072 return bc;
9073
e1b09194 9074 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 9075 if (csum == pktcsum)
c2d11a7d 9076 return bc;
c906108c 9077
c5aa993b 9078 if (remote_debug)
c906108c 9079 {
b3ced9ba 9080 std::string str = escape_buffer (buf, bc);
6e5abd65 9081
6e5abd65 9082 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
9083 "Bad checksum, sentsum=0x%x, "
9084 "csum=0x%x, buf=%s\n",
b3ced9ba 9085 pktcsum, csum, str.c_str ());
c906108c 9086 }
c2d11a7d 9087 /* Number of characters in buffer ignoring trailing
23860348 9088 NULL. */
c2d11a7d 9089 return -1;
c906108c 9090 }
23860348 9091 case '*': /* Run length encoding. */
c2c6d25f
JM
9092 {
9093 int repeat;
c906108c 9094
a744cf53 9095 csum += c;
b4501125
AC
9096 c = readchar (remote_timeout);
9097 csum += c;
23860348 9098 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 9099
23860348 9100 /* The character before ``*'' is repeated. */
c2d11a7d 9101
6d820c5c 9102 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 9103 {
6d820c5c
DJ
9104 if (bc + repeat - 1 >= *sizeof_buf - 1)
9105 {
9106 /* Make some more room in the buffer. */
9107 *sizeof_buf += repeat;
224c3ddb 9108 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c
DJ
9109 buf = *buf_p;
9110 }
9111
c2d11a7d
JM
9112 memset (&buf[bc], buf[bc - 1], repeat);
9113 bc += repeat;
c2c6d25f
JM
9114 continue;
9115 }
9116
c2d11a7d 9117 buf[bc] = '\0';
6d820c5c 9118 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 9119 return -1;
c2c6d25f 9120 }
c906108c 9121 default:
6d820c5c 9122 if (bc >= *sizeof_buf - 1)
c906108c 9123 {
6d820c5c
DJ
9124 /* Make some more room in the buffer. */
9125 *sizeof_buf *= 2;
224c3ddb 9126 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c 9127 buf = *buf_p;
c906108c
SS
9128 }
9129
6d820c5c
DJ
9130 buf[bc++] = c;
9131 csum += c;
9132 continue;
c906108c
SS
9133 }
9134 }
9135}
9136
9137/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9138 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9139 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9140 rather than timing out; this is used (in synchronous mode) to wait
9141 for a target that is is executing user code to stop. */
d9fcf2fb
JM
9142/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9143 don't have to change all the calls to getpkt to deal with the
9144 return value, because at the moment I don't know what the right
23860348 9145 thing to do it for those. */
c906108c 9146void
6d820c5c
DJ
9147getpkt (char **buf,
9148 long *sizeof_buf,
c2d11a7d 9149 int forever)
d9fcf2fb 9150{
54887903 9151 getpkt_sane (buf, sizeof_buf, forever);
d9fcf2fb
JM
9152}
9153
9154
9155/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
9156 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
9157 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
9158 rather than timing out; this is used (in synchronous mode) to wait
9159 for a target that is is executing user code to stop. If FOREVER ==
9160 0, this function is allowed to time out gracefully and return an
74531fed
PA
9161 indication of this to the caller. Otherwise return the number of
9162 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
9163 enough reason to return to the caller. *IS_NOTIF is an output
9164 boolean that indicates whether *BUF holds a notification or not
9165 (a regular packet). */
74531fed 9166
3172dc30 9167static int
74531fed 9168getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 9169 int expecting_notif, int *is_notif)
c906108c 9170{
2d717e4f 9171 struct remote_state *rs = get_remote_state ();
c906108c
SS
9172 int c;
9173 int tries;
9174 int timeout;
df4b58fe 9175 int val = -1;
c906108c 9176
2d717e4f
DJ
9177 /* We're reading a new response. Make sure we don't look at a
9178 previously cached response. */
9179 rs->cached_wait_status = 0;
9180
6d820c5c 9181 strcpy (*buf, "timeout");
c906108c
SS
9182
9183 if (forever)
74531fed
PA
9184 timeout = watchdog > 0 ? watchdog : -1;
9185 else if (expecting_notif)
9186 timeout = 0; /* There should already be a char in the buffer. If
9187 not, bail out. */
c906108c
SS
9188 else
9189 timeout = remote_timeout;
9190
9191#define MAX_TRIES 3
9192
74531fed
PA
9193 /* Process any number of notifications, and then return when
9194 we get a packet. */
9195 for (;;)
c906108c 9196 {
d9c43928 9197 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
9198 times. */
9199 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 9200 {
74531fed
PA
9201 /* This can loop forever if the remote side sends us
9202 characters continuously, but if it pauses, we'll get
9203 SERIAL_TIMEOUT from readchar because of timeout. Then
9204 we'll count that as a retry.
9205
9206 Note that even when forever is set, we will only wait
9207 forever prior to the start of a packet. After that, we
9208 expect characters to arrive at a brisk pace. They should
9209 show up within remote_timeout intervals. */
9210 do
9211 c = readchar (timeout);
9212 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
9213
9214 if (c == SERIAL_TIMEOUT)
9215 {
74531fed
PA
9216 if (expecting_notif)
9217 return -1; /* Don't complain, it's normal to not get
9218 anything in this case. */
9219
23860348 9220 if (forever) /* Watchdog went off? Kill the target. */
c906108c 9221 {
78a095c3 9222 remote_unpush_target ();
598d3636
JK
9223 throw_error (TARGET_CLOSE_ERROR,
9224 _("Watchdog timeout has expired. "
9225 "Target detached."));
c906108c 9226 }
c906108c 9227 if (remote_debug)
0f71a2f6 9228 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 9229 }
74531fed
PA
9230 else
9231 {
9232 /* We've found the start of a packet or notification.
9233 Now collect the data. */
9234 val = read_frame (buf, sizeof_buf);
9235 if (val >= 0)
9236 break;
9237 }
9238
c33e31fd 9239 remote_serial_write ("-", 1);
c906108c 9240 }
c906108c 9241
74531fed
PA
9242 if (tries > MAX_TRIES)
9243 {
9244 /* We have tried hard enough, and just can't receive the
9245 packet/notification. Give up. */
9246 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 9247
74531fed
PA
9248 /* Skip the ack char if we're in no-ack mode. */
9249 if (!rs->noack_mode)
c33e31fd 9250 remote_serial_write ("+", 1);
74531fed
PA
9251 return -1;
9252 }
c906108c 9253
74531fed
PA
9254 /* If we got an ordinary packet, return that to our caller. */
9255 if (c == '$')
c906108c
SS
9256 {
9257 if (remote_debug)
43e526b9 9258 {
6f8976bf
YQ
9259 std::string str
9260 = escape_buffer (*buf,
9261 std::min (val, REMOTE_DEBUG_MAX_CHAR));
9262
9263 fprintf_unfiltered (gdb_stdlog, "Packet received: %s",
9264 str.c_str ());
9265
9266 if (str.length () > REMOTE_DEBUG_MAX_CHAR)
9267 {
9268 fprintf_unfiltered (gdb_stdlog, "[%zu bytes omitted]",
9269 str.length () - REMOTE_DEBUG_MAX_CHAR);
9270 }
6e5abd65 9271
6f8976bf 9272 fprintf_unfiltered (gdb_stdlog, "\n");
43e526b9 9273 }
a6f3e723
SL
9274
9275 /* Skip the ack char if we're in no-ack mode. */
9276 if (!rs->noack_mode)
c33e31fd 9277 remote_serial_write ("+", 1);
fee9eda9
YQ
9278 if (is_notif != NULL)
9279 *is_notif = 0;
0876f84a 9280 return val;
c906108c
SS
9281 }
9282
74531fed
PA
9283 /* If we got a notification, handle it, and go back to looking
9284 for a packet. */
9285 else
9286 {
9287 gdb_assert (c == '%');
9288
9289 if (remote_debug)
9290 {
b3ced9ba 9291 std::string str = escape_buffer (*buf, val);
6e5abd65 9292
6e5abd65
PA
9293 fprintf_unfiltered (gdb_stdlog,
9294 " Notification received: %s\n",
b3ced9ba 9295 str.c_str ());
74531fed 9296 }
fee9eda9
YQ
9297 if (is_notif != NULL)
9298 *is_notif = 1;
c906108c 9299
5965e028 9300 handle_notification (rs->notif_state, *buf);
c906108c 9301
74531fed 9302 /* Notifications require no acknowledgement. */
a6f3e723 9303
74531fed 9304 if (expecting_notif)
fee9eda9 9305 return val;
74531fed
PA
9306 }
9307 }
9308}
9309
9310static int
9311getpkt_sane (char **buf, long *sizeof_buf, int forever)
9312{
fee9eda9 9313 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
9314}
9315
9316static int
fee9eda9
YQ
9317getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
9318 int *is_notif)
74531fed 9319{
fee9eda9
YQ
9320 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
9321 is_notif);
c906108c 9322}
74531fed 9323
cbb8991c
DB
9324/* Check whether EVENT is a fork event for the process specified
9325 by the pid passed in DATA, and if it is, kill the fork child. */
9326
9327static int
9328kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
9329 QUEUE_ITER (stop_reply_p) *iter,
9330 stop_reply_p event,
9331 void *data)
9332{
19ba03f4 9333 struct queue_iter_param *param = (struct queue_iter_param *) data;
cbb8991c
DB
9334 int parent_pid = *(int *) param->input;
9335
9336 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
9337 {
9338 struct remote_state *rs = get_remote_state ();
9339 int child_pid = ptid_get_pid (event->ws.value.related_pid);
9340 int res;
9341
9342 res = remote_vkill (child_pid, rs);
9343 if (res != 0)
9344 error (_("Can't kill fork child process %d"), child_pid);
9345 }
9346
9347 return 1;
9348}
9349
9350/* Kill any new fork children of process PID that haven't been
9351 processed by follow_fork. */
9352
9353static void
9354kill_new_fork_children (int pid, struct remote_state *rs)
9355{
9356 struct thread_info *thread;
9357 struct notif_client *notif = &notif_client_stop;
9358 struct queue_iter_param param;
9359
9360 /* Kill the fork child threads of any threads in process PID
9361 that are stopped at a fork event. */
9362 ALL_NON_EXITED_THREADS (thread)
9363 {
9364 struct target_waitstatus *ws = &thread->pending_follow;
9365
9366 if (is_pending_fork_parent (ws, pid, thread->ptid))
9367 {
9368 struct remote_state *rs = get_remote_state ();
9369 int child_pid = ptid_get_pid (ws->value.related_pid);
9370 int res;
9371
9372 res = remote_vkill (child_pid, rs);
9373 if (res != 0)
9374 error (_("Can't kill fork child process %d"), child_pid);
9375 }
9376 }
9377
9378 /* Check for any pending fork events (not reported or processed yet)
9379 in process PID and kill those fork child threads as well. */
9380 remote_notif_get_pending_events (notif);
9381 param.input = &pid;
9382 param.output = NULL;
9383 QUEUE_iterate (stop_reply_p, stop_reply_queue,
9384 kill_child_of_pending_fork, &param);
9385}
9386
c906108c 9387\f
8020350c
DB
9388/* Target hook to kill the current inferior. */
9389
c906108c 9390static void
7d85a9c0 9391remote_kill (struct target_ops *ops)
43ff13b4 9392{
8020350c
DB
9393 int res = -1;
9394 int pid = ptid_get_pid (inferior_ptid);
9395 struct remote_state *rs = get_remote_state ();
0fdf84ca 9396
8020350c 9397 if (packet_support (PACKET_vKill) != PACKET_DISABLE)
0fdf84ca 9398 {
8020350c
DB
9399 /* If we're stopped while forking and we haven't followed yet,
9400 kill the child task. We need to do this before killing the
9401 parent task because if this is a vfork then the parent will
9402 be sleeping. */
9403 kill_new_fork_children (pid, rs);
9404
9405 res = remote_vkill (pid, rs);
9406 if (res == 0)
0fdf84ca 9407 {
bc1e6c81 9408 target_mourn_inferior (inferior_ptid);
0fdf84ca
PA
9409 return;
9410 }
8020350c 9411 }
0fdf84ca 9412
8020350c
DB
9413 /* If we are in 'target remote' mode and we are killing the only
9414 inferior, then we will tell gdbserver to exit and unpush the
9415 target. */
9416 if (res == -1 && !remote_multi_process_p (rs)
9417 && number_of_live_inferiors () == 1)
9418 {
9419 remote_kill_k ();
9420
9421 /* We've killed the remote end, we get to mourn it. If we are
9422 not in extended mode, mourning the inferior also unpushes
9423 remote_ops from the target stack, which closes the remote
9424 connection. */
bc1e6c81 9425 target_mourn_inferior (inferior_ptid);
8020350c
DB
9426
9427 return;
0fdf84ca 9428 }
43ff13b4 9429
8020350c 9430 error (_("Can't kill process"));
43ff13b4
JM
9431}
9432
8020350c
DB
9433/* Send a kill request to the target using the 'vKill' packet. */
9434
82f73884
PA
9435static int
9436remote_vkill (int pid, struct remote_state *rs)
9437{
4082afcc 9438 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
9439 return -1;
9440
9441 /* Tell the remote target to detach. */
bba74b36 9442 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
9443 putpkt (rs->buf);
9444 getpkt (&rs->buf, &rs->buf_size, 0);
9445
4082afcc
PA
9446 switch (packet_ok (rs->buf,
9447 &remote_protocol_packets[PACKET_vKill]))
9448 {
9449 case PACKET_OK:
9450 return 0;
9451 case PACKET_ERROR:
9452 return 1;
9453 case PACKET_UNKNOWN:
9454 return -1;
9455 default:
9456 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
9457 }
82f73884
PA
9458}
9459
8020350c
DB
9460/* Send a kill request to the target using the 'k' packet. */
9461
82f73884 9462static void
8020350c 9463remote_kill_k (void)
82f73884 9464{
8020350c
DB
9465 /* Catch errors so the user can quit from gdb even when we
9466 aren't on speaking terms with the remote system. */
9467 TRY
82f73884 9468 {
82f73884 9469 putpkt ("k");
82f73884 9470 }
8020350c
DB
9471 CATCH (ex, RETURN_MASK_ERROR)
9472 {
9473 if (ex.error == TARGET_CLOSE_ERROR)
9474 {
9475 /* If we got an (EOF) error that caused the target
9476 to go away, then we're done, that's what we wanted.
9477 "k" is susceptible to cause a premature EOF, given
9478 that the remote server isn't actually required to
9479 reply to "k", and it can happen that it doesn't
9480 even get to reply ACK to the "k". */
9481 return;
9482 }
82f73884 9483
8020350c
DB
9484 /* Otherwise, something went wrong. We didn't actually kill
9485 the target. Just propagate the exception, and let the
9486 user or higher layers decide what to do. */
9487 throw_exception (ex);
9488 }
9489 END_CATCH
82f73884
PA
9490}
9491
c906108c 9492static void
20f796c9 9493remote_mourn (struct target_ops *target)
c906108c 9494{
8020350c 9495 struct remote_state *rs = get_remote_state ();
ce5ce7ed 9496
8020350c
DB
9497 /* In 'target remote' mode with one inferior, we close the connection. */
9498 if (!rs->extended && number_of_live_inferiors () <= 1)
9499 {
9500 unpush_target (target);
c906108c 9501
8020350c
DB
9502 /* remote_close takes care of doing most of the clean up. */
9503 generic_mourn_inferior ();
9504 return;
9505 }
c906108c 9506
e24a49d8
PA
9507 /* In case we got here due to an error, but we're going to stay
9508 connected. */
9509 rs->waiting_for_stop_reply = 0;
9510
dc1981d7
PA
9511 /* If the current general thread belonged to the process we just
9512 detached from or has exited, the remote side current general
9513 thread becomes undefined. Considering a case like this:
9514
9515 - We just got here due to a detach.
9516 - The process that we're detaching from happens to immediately
9517 report a global breakpoint being hit in non-stop mode, in the
9518 same thread we had selected before.
9519 - GDB attaches to this process again.
9520 - This event happens to be the next event we handle.
9521
9522 GDB would consider that the current general thread didn't need to
9523 be set on the stub side (with Hg), since for all it knew,
9524 GENERAL_THREAD hadn't changed.
9525
9526 Notice that although in all-stop mode, the remote server always
9527 sets the current thread to the thread reporting the stop event,
9528 that doesn't happen in non-stop mode; in non-stop, the stub *must
9529 not* change the current thread when reporting a breakpoint hit,
9530 due to the decoupling of event reporting and event handling.
9531
9532 To keep things simple, we always invalidate our notion of the
9533 current thread. */
47f8a51d 9534 record_currthread (rs, minus_one_ptid);
dc1981d7 9535
8020350c 9536 /* Call common code to mark the inferior as not running. */
48aa3c27
PA
9537 generic_mourn_inferior ();
9538
d729566a 9539 if (!have_inferiors ())
2d717e4f 9540 {
82f73884
PA
9541 if (!remote_multi_process_p (rs))
9542 {
9543 /* Check whether the target is running now - some remote stubs
9544 automatically restart after kill. */
9545 putpkt ("?");
9546 getpkt (&rs->buf, &rs->buf_size, 0);
9547
9548 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
9549 {
3e43a32a
MS
9550 /* Assume that the target has been restarted. Set
9551 inferior_ptid so that bits of core GDB realizes
9552 there's something here, e.g., so that the user can
9553 say "kill" again. */
82f73884
PA
9554 inferior_ptid = magic_null_ptid;
9555 }
82f73884 9556 }
2d717e4f
DJ
9557 }
9558}
c906108c 9559
03583c20 9560static int
2bfc0540 9561extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 9562{
4082afcc 9563 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
9564}
9565
9566static void
9567extended_remote_disable_randomization (int val)
9568{
9569 struct remote_state *rs = get_remote_state ();
9570 char *reply;
9571
bba74b36
YQ
9572 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
9573 val);
03583c20 9574 putpkt (rs->buf);
b6bb3468 9575 reply = remote_get_noisy_reply ();
03583c20
UW
9576 if (*reply == '\0')
9577 error (_("Target does not support QDisableRandomization."));
9578 if (strcmp (reply, "OK") != 0)
9579 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
9580}
9581
2d717e4f 9582static int
7c5ded6a 9583extended_remote_run (const std::string &args)
2d717e4f
DJ
9584{
9585 struct remote_state *rs = get_remote_state ();
2d717e4f 9586 int len;
94585166 9587 const char *remote_exec_file = get_remote_exec_file ();
c906108c 9588
2d717e4f
DJ
9589 /* If the user has disabled vRun support, or we have detected that
9590 support is not available, do not try it. */
4082afcc 9591 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 9592 return -1;
424163ea 9593
2d717e4f
DJ
9594 strcpy (rs->buf, "vRun;");
9595 len = strlen (rs->buf);
c906108c 9596
2d717e4f
DJ
9597 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
9598 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
9599 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
9600 strlen (remote_exec_file));
2d717e4f 9601
7c5ded6a 9602 if (!args.empty ())
2d717e4f 9603 {
2d717e4f 9604 int i;
2d717e4f 9605
773a1edc 9606 gdb_argv argv (args.c_str ());
2d717e4f
DJ
9607 for (i = 0; argv[i] != NULL; i++)
9608 {
9609 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
9610 error (_("Argument list too long for run packet"));
9611 rs->buf[len++] = ';';
9f1b45b0
TT
9612 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
9613 strlen (argv[i]));
2d717e4f 9614 }
2d717e4f
DJ
9615 }
9616
9617 rs->buf[len++] = '\0';
9618
9619 putpkt (rs->buf);
9620 getpkt (&rs->buf, &rs->buf_size, 0);
9621
4082afcc 9622 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 9623 {
4082afcc 9624 case PACKET_OK:
3405876a 9625 /* We have a wait response. All is well. */
2d717e4f 9626 return 0;
4082afcc
PA
9627 case PACKET_UNKNOWN:
9628 return -1;
9629 case PACKET_ERROR:
2d717e4f
DJ
9630 if (remote_exec_file[0] == '\0')
9631 error (_("Running the default executable on the remote target failed; "
9632 "try \"set remote exec-file\"?"));
9633 else
9634 error (_("Running \"%s\" on the remote target failed"),
9635 remote_exec_file);
4082afcc
PA
9636 default:
9637 gdb_assert_not_reached (_("bad switch"));
2d717e4f 9638 }
c906108c
SS
9639}
9640
0a2dde4a
SDJ
9641/* Helper function to send set/unset environment packets. ACTION is
9642 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
9643 or "QEnvironmentUnsetVariable". VALUE is the variable to be
9644 sent. */
9645
9646static void
9647send_environment_packet (struct remote_state *rs,
9648 const char *action,
9649 const char *packet,
9650 const char *value)
9651{
9652 /* Convert the environment variable to an hex string, which
9653 is the best format to be transmitted over the wire. */
9654 std::string encoded_value = bin2hex ((const gdb_byte *) value,
9655 strlen (value));
9656
9657 xsnprintf (rs->buf, get_remote_packet_size (),
9658 "%s:%s", packet, encoded_value.c_str ());
9659
9660 putpkt (rs->buf);
9661 getpkt (&rs->buf, &rs->buf_size, 0);
9662 if (strcmp (rs->buf, "OK") != 0)
9663 warning (_("Unable to %s environment variable '%s' on remote."),
9664 action, value);
9665}
9666
9667/* Helper function to handle the QEnvironment* packets. */
9668
9669static void
9670extended_remote_environment_support (struct remote_state *rs)
9671{
9672 if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
9673 {
9674 putpkt ("QEnvironmentReset");
9675 getpkt (&rs->buf, &rs->buf_size, 0);
9676 if (strcmp (rs->buf, "OK") != 0)
9677 warning (_("Unable to reset environment on remote."));
9678 }
9679
9680 gdb_environ *e = &current_inferior ()->environment;
9681
9682 if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
9683 for (const std::string &el : e->user_set_env ())
9684 send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
9685 el.c_str ());
9686
9687 if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
9688 for (const std::string &el : e->user_unset_env ())
9689 send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
9690}
9691
bc3b087d
SDJ
9692/* Helper function to set the current working directory for the
9693 inferior in the remote target. */
9694
9695static void
9696extended_remote_set_inferior_cwd (struct remote_state *rs)
9697{
9698 if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
9699 {
9700 const char *inferior_cwd = get_inferior_cwd ();
9701
9702 if (inferior_cwd != NULL)
9703 {
9704 std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
9705 strlen (inferior_cwd));
9706
9707 xsnprintf (rs->buf, get_remote_packet_size (),
9708 "QSetWorkingDir:%s", hexpath.c_str ());
9709 }
9710 else
9711 {
9712 /* An empty inferior_cwd means that the user wants us to
9713 reset the remote server's inferior's cwd. */
9714 xsnprintf (rs->buf, get_remote_packet_size (),
9715 "QSetWorkingDir:");
9716 }
9717
9718 putpkt (rs->buf);
9719 getpkt (&rs->buf, &rs->buf_size, 0);
9720 if (packet_ok (rs->buf,
9721 &remote_protocol_packets[PACKET_QSetWorkingDir])
9722 != PACKET_OK)
9723 error (_("\
9724Remote replied unexpectedly while setting the inferior's working\n\
9725directory: %s"),
9726 rs->buf);
9727
9728 }
9729}
9730
2d717e4f
DJ
9731/* In the extended protocol we want to be able to do things like
9732 "run" and have them basically work as expected. So we need
9733 a special create_inferior function. We support changing the
9734 executable file and the command line arguments, but not the
9735 environment. */
9736
43ff13b4 9737static void
77a19445 9738extended_remote_create_inferior (struct target_ops *ops,
7c5ded6a
SDJ
9739 const char *exec_file,
9740 const std::string &args,
77a19445 9741 char **env, int from_tty)
43ff13b4 9742{
3405876a
PA
9743 int run_worked;
9744 char *stop_reply;
9745 struct remote_state *rs = get_remote_state ();
94585166 9746 const char *remote_exec_file = get_remote_exec_file ();
3405876a 9747
43ff13b4 9748 /* If running asynchronously, register the target file descriptor
23860348 9749 with the event loop. */
75c99385 9750 if (target_can_async_p ())
6a3753b3 9751 target_async (1);
43ff13b4 9752
03583c20 9753 /* Disable address space randomization if requested (and supported). */
2bfc0540 9754 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
9755 extended_remote_disable_randomization (disable_randomization);
9756
aefd8b33
SDJ
9757 /* If startup-with-shell is on, we inform gdbserver to start the
9758 remote inferior using a shell. */
9759 if (packet_support (PACKET_QStartupWithShell) != PACKET_DISABLE)
9760 {
9761 xsnprintf (rs->buf, get_remote_packet_size (),
9762 "QStartupWithShell:%d", startup_with_shell ? 1 : 0);
9763 putpkt (rs->buf);
9764 getpkt (&rs->buf, &rs->buf_size, 0);
9765 if (strcmp (rs->buf, "OK") != 0)
9766 error (_("\
9767Remote replied unexpectedly while setting startup-with-shell: %s"),
9768 rs->buf);
9769 }
9770
0a2dde4a
SDJ
9771 extended_remote_environment_support (rs);
9772
bc3b087d
SDJ
9773 extended_remote_set_inferior_cwd (rs);
9774
43ff13b4 9775 /* Now restart the remote server. */
3405876a
PA
9776 run_worked = extended_remote_run (args) != -1;
9777 if (!run_worked)
2d717e4f
DJ
9778 {
9779 /* vRun was not supported. Fail if we need it to do what the
9780 user requested. */
9781 if (remote_exec_file[0])
9782 error (_("Remote target does not support \"set remote exec-file\""));
7c5ded6a 9783 if (!args.empty ())
2d717e4f 9784 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 9785
2d717e4f
DJ
9786 /* Fall back to "R". */
9787 extended_remote_restart ();
9788 }
424163ea 9789
6c95b8df
PA
9790 if (!have_inferiors ())
9791 {
9792 /* Clean up from the last time we ran, before we mark the target
9793 running again. This will mark breakpoints uninserted, and
9794 get_offsets may insert breakpoints. */
9795 init_thread_list ();
9796 init_wait_for_inferior ();
9797 }
45280a52 9798
3405876a
PA
9799 /* vRun's success return is a stop reply. */
9800 stop_reply = run_worked ? rs->buf : NULL;
9801 add_current_inferior_and_thread (stop_reply);
c0a2216e 9802
2d717e4f
DJ
9803 /* Get updated offsets, if the stub uses qOffsets. */
9804 get_offsets ();
2d717e4f 9805}
c906108c 9806\f
c5aa993b 9807
b775012e
LM
9808/* Given a location's target info BP_TGT and the packet buffer BUF, output
9809 the list of conditions (in agent expression bytecode format), if any, the
9810 target needs to evaluate. The output is placed into the packet buffer
bba74b36 9811 started from BUF and ended at BUF_END. */
b775012e
LM
9812
9813static int
9814remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
9815 struct bp_target_info *bp_tgt, char *buf,
9816 char *buf_end)
b775012e 9817{
3cde5c42 9818 if (bp_tgt->conditions.empty ())
b775012e
LM
9819 return 0;
9820
9821 buf += strlen (buf);
bba74b36 9822 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
9823 buf++;
9824
83621223 9825 /* Send conditions to the target. */
d538e36d 9826 for (agent_expr *aexpr : bp_tgt->conditions)
b775012e 9827 {
bba74b36 9828 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e 9829 buf += strlen (buf);
3cde5c42 9830 for (int i = 0; i < aexpr->len; ++i)
b775012e
LM
9831 buf = pack_hex_byte (buf, aexpr->buf[i]);
9832 *buf = '\0';
9833 }
b775012e
LM
9834 return 0;
9835}
9836
d3ce09f5
SS
9837static void
9838remote_add_target_side_commands (struct gdbarch *gdbarch,
9839 struct bp_target_info *bp_tgt, char *buf)
9840{
3cde5c42 9841 if (bp_tgt->tcommands.empty ())
d3ce09f5
SS
9842 return;
9843
9844 buf += strlen (buf);
9845
9846 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
9847 buf += strlen (buf);
9848
9849 /* Concatenate all the agent expressions that are commands into the
9850 cmds parameter. */
df97be55 9851 for (agent_expr *aexpr : bp_tgt->tcommands)
d3ce09f5
SS
9852 {
9853 sprintf (buf, "X%x,", aexpr->len);
9854 buf += strlen (buf);
3cde5c42 9855 for (int i = 0; i < aexpr->len; ++i)
d3ce09f5
SS
9856 buf = pack_hex_byte (buf, aexpr->buf[i]);
9857 *buf = '\0';
9858 }
d3ce09f5
SS
9859}
9860
8181d85f
DJ
9861/* Insert a breakpoint. On targets that have software breakpoint
9862 support, we ask the remote target to do the work; on targets
9863 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
9864
9865static int
3db08215
MM
9866remote_insert_breakpoint (struct target_ops *ops,
9867 struct gdbarch *gdbarch,
a6d9a66e 9868 struct bp_target_info *bp_tgt)
c906108c 9869{
d471ea57
AC
9870 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
9871 If it succeeds, then set the support to PACKET_ENABLE. If it
9872 fails, and the user has explicitly requested the Z support then
23860348 9873 report an error, otherwise, mark it disabled and go on. */
802188a7 9874
4082afcc 9875 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9876 {
0d5ed153 9877 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 9878 struct remote_state *rs;
bba74b36 9879 char *p, *endbuf;
7c0f6dcc 9880 int bpsize;
4fff2411 9881
28439a30
PA
9882 /* Make sure the remote is pointing at the right process, if
9883 necessary. */
9884 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9885 set_general_process ();
9886
4fff2411
JZ
9887 rs = get_remote_state ();
9888 p = rs->buf;
bba74b36 9889 endbuf = rs->buf + get_remote_packet_size ();
802188a7 9890
96baa820
JM
9891 *(p++) = 'Z';
9892 *(p++) = '0';
9893 *(p++) = ',';
7c0f6dcc 9894 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 9895 p += hexnumstr (p, addr);
579c6ad9 9896 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 9897
efcc2da7 9898 if (remote_supports_cond_breakpoints (ops))
bba74b36 9899 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 9900
78eff0ec 9901 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
9902 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9903
6d820c5c
DJ
9904 putpkt (rs->buf);
9905 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9906
6d820c5c 9907 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 9908 {
d471ea57
AC
9909 case PACKET_ERROR:
9910 return -1;
9911 case PACKET_OK:
9912 return 0;
9913 case PACKET_UNKNOWN:
9914 break;
96baa820
JM
9915 }
9916 }
c906108c 9917
0000e5cc
PA
9918 /* If this breakpoint has target-side commands but this stub doesn't
9919 support Z0 packets, throw error. */
3cde5c42 9920 if (!bp_tgt->tcommands.empty ())
0000e5cc
PA
9921 throw_error (NOT_SUPPORTED_ERROR, _("\
9922Target doesn't support breakpoints that have target side commands."));
9923
3db08215 9924 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
9925}
9926
9927static int
3db08215
MM
9928remote_remove_breakpoint (struct target_ops *ops,
9929 struct gdbarch *gdbarch,
73971819
PA
9930 struct bp_target_info *bp_tgt,
9931 enum remove_bp_reason reason)
c906108c 9932{
8181d85f 9933 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 9934 struct remote_state *rs = get_remote_state ();
96baa820 9935
4082afcc 9936 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9937 {
6d820c5c 9938 char *p = rs->buf;
bba74b36 9939 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 9940
28439a30
PA
9941 /* Make sure the remote is pointing at the right process, if
9942 necessary. */
9943 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9944 set_general_process ();
9945
96baa820
JM
9946 *(p++) = 'z';
9947 *(p++) = '0';
9948 *(p++) = ',';
9949
8181d85f
DJ
9950 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
9951 p += hexnumstr (p, addr);
579c6ad9 9952 xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
802188a7 9953
6d820c5c
DJ
9954 putpkt (rs->buf);
9955 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9956
6d820c5c 9957 return (rs->buf[0] == 'E');
96baa820
JM
9958 }
9959
73971819 9960 return memory_remove_breakpoint (ops, gdbarch, bp_tgt, reason);
c906108c
SS
9961}
9962
f486487f 9963static enum Z_packet_type
d471ea57
AC
9964watchpoint_to_Z_packet (int type)
9965{
9966 switch (type)
9967 {
9968 case hw_write:
bb858e6a 9969 return Z_PACKET_WRITE_WP;
d471ea57
AC
9970 break;
9971 case hw_read:
bb858e6a 9972 return Z_PACKET_READ_WP;
d471ea57
AC
9973 break;
9974 case hw_access:
bb858e6a 9975 return Z_PACKET_ACCESS_WP;
d471ea57
AC
9976 break;
9977 default:
8e65ff28 9978 internal_error (__FILE__, __LINE__,
e2e0b3e5 9979 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
9980 }
9981}
9982
3c3bea1c 9983static int
f486487f
SM
9984remote_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9985 enum target_hw_bp_type type, struct expression *cond)
96baa820 9986{
d01949b6 9987 struct remote_state *rs = get_remote_state ();
bba74b36 9988 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9989 char *p;
d471ea57 9990 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 9991
4082afcc 9992 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 9993 return 1;
802188a7 9994
28439a30
PA
9995 /* Make sure the remote is pointing at the right process, if
9996 necessary. */
9997 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9998 set_general_process ();
9999
bba74b36 10000 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 10001 p = strchr (rs->buf, '\0');
96baa820
JM
10002 addr = remote_address_masked (addr);
10003 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10004 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 10005
6d820c5c
DJ
10006 putpkt (rs->buf);
10007 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10008
6d820c5c 10009 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10010 {
10011 case PACKET_ERROR:
d471ea57 10012 return -1;
85d721b8
PA
10013 case PACKET_UNKNOWN:
10014 return 1;
d471ea57
AC
10015 case PACKET_OK:
10016 return 0;
10017 }
8e65ff28 10018 internal_error (__FILE__, __LINE__,
e2e0b3e5 10019 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
10020}
10021
283002cf
MR
10022static int
10023remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
10024 CORE_ADDR start, int length)
10025{
10026 CORE_ADDR diff = remote_address_masked (addr - start);
10027
10028 return diff < length;
10029}
10030
d471ea57 10031
3c3bea1c 10032static int
f486487f
SM
10033remote_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
10034 enum target_hw_bp_type type, struct expression *cond)
96baa820 10035{
d01949b6 10036 struct remote_state *rs = get_remote_state ();
bba74b36 10037 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 10038 char *p;
d471ea57
AC
10039 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
10040
4082afcc 10041 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 10042 return -1;
802188a7 10043
28439a30
PA
10044 /* Make sure the remote is pointing at the right process, if
10045 necessary. */
10046 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10047 set_general_process ();
10048
bba74b36 10049 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 10050 p = strchr (rs->buf, '\0');
96baa820
JM
10051 addr = remote_address_masked (addr);
10052 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 10053 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
10054 putpkt (rs->buf);
10055 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10056
6d820c5c 10057 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
10058 {
10059 case PACKET_ERROR:
10060 case PACKET_UNKNOWN:
10061 return -1;
10062 case PACKET_OK:
10063 return 0;
10064 }
8e65ff28 10065 internal_error (__FILE__, __LINE__,
e2e0b3e5 10066 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
10067}
10068
3c3bea1c 10069
501eef12 10070int remote_hw_watchpoint_limit = -1;
480a3f21 10071int remote_hw_watchpoint_length_limit = -1;
501eef12 10072int remote_hw_breakpoint_limit = -1;
d471ea57 10073
480a3f21 10074static int
31568a15
TT
10075remote_region_ok_for_hw_watchpoint (struct target_ops *self,
10076 CORE_ADDR addr, int len)
480a3f21
PW
10077{
10078 if (remote_hw_watchpoint_length_limit == 0)
10079 return 0;
10080 else if (remote_hw_watchpoint_length_limit < 0)
10081 return 1;
10082 else if (len <= remote_hw_watchpoint_length_limit)
10083 return 1;
10084 else
10085 return 0;
10086}
10087
b9362cc7 10088static int
5461485a 10089remote_check_watch_resources (struct target_ops *self,
f486487f 10090 enum bptype type, int cnt, int ot)
96baa820 10091{
3c3bea1c
GS
10092 if (type == bp_hardware_breakpoint)
10093 {
10094 if (remote_hw_breakpoint_limit == 0)
10095 return 0;
501eef12
AC
10096 else if (remote_hw_breakpoint_limit < 0)
10097 return 1;
3c3bea1c
GS
10098 else if (cnt <= remote_hw_breakpoint_limit)
10099 return 1;
10100 }
10101 else
10102 {
10103 if (remote_hw_watchpoint_limit == 0)
10104 return 0;
501eef12
AC
10105 else if (remote_hw_watchpoint_limit < 0)
10106 return 1;
3c3bea1c
GS
10107 else if (ot)
10108 return -1;
10109 else if (cnt <= remote_hw_watchpoint_limit)
10110 return 1;
10111 }
10112 return -1;
10113}
10114
f7e6eed5
PA
10115/* The to_stopped_by_sw_breakpoint method of target remote. */
10116
10117static int
10118remote_stopped_by_sw_breakpoint (struct target_ops *ops)
10119{
799a2abe 10120 struct thread_info *thread = inferior_thread ();
f7e6eed5 10121
799a2abe
PA
10122 return (thread->priv != NULL
10123 && thread->priv->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
f7e6eed5
PA
10124}
10125
10126/* The to_supports_stopped_by_sw_breakpoint method of target
10127 remote. */
10128
10129static int
10130remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
10131{
f7e6eed5
PA
10132 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
10133}
10134
10135/* The to_stopped_by_hw_breakpoint method of target remote. */
10136
10137static int
10138remote_stopped_by_hw_breakpoint (struct target_ops *ops)
10139{
799a2abe 10140 struct thread_info *thread = inferior_thread ();
f7e6eed5 10141
799a2abe
PA
10142 return (thread->priv != NULL
10143 && thread->priv->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
f7e6eed5
PA
10144}
10145
10146/* The to_supports_stopped_by_hw_breakpoint method of target
10147 remote. */
10148
10149static int
10150remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
10151{
f7e6eed5
PA
10152 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
10153}
10154
b9362cc7 10155static int
6a109b6b 10156remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 10157{
799a2abe 10158 struct thread_info *thread = inferior_thread ();
ee154bee 10159
799a2abe
PA
10160 return (thread->priv != NULL
10161 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT);
3c3bea1c
GS
10162}
10163
4aa7a7f5
JJ
10164static int
10165remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 10166{
799a2abe 10167 struct thread_info *thread = inferior_thread ();
a744cf53 10168
799a2abe
PA
10169 if (thread->priv != NULL
10170 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
4aa7a7f5 10171 {
799a2abe
PA
10172 *addr_p = thread->priv->watch_data_address;
10173 return 1;
4aa7a7f5
JJ
10174 }
10175
799a2abe 10176 return 0;
3c3bea1c
GS
10177}
10178
10179
10180static int
23a26771 10181remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 10182 struct bp_target_info *bp_tgt)
3c3bea1c 10183{
0d5ed153 10184 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 10185 struct remote_state *rs;
bba74b36 10186 char *p, *endbuf;
dd61ec5c 10187 char *message;
3c3bea1c 10188
4082afcc 10189 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10190 return -1;
2bc416ba 10191
28439a30
PA
10192 /* Make sure the remote is pointing at the right process, if
10193 necessary. */
10194 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10195 set_general_process ();
10196
4fff2411
JZ
10197 rs = get_remote_state ();
10198 p = rs->buf;
bba74b36 10199 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 10200
96baa820
JM
10201 *(p++) = 'Z';
10202 *(p++) = '1';
10203 *(p++) = ',';
802188a7 10204
0d5ed153 10205 addr = remote_address_masked (addr);
96baa820 10206 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10207 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10208
efcc2da7 10209 if (remote_supports_cond_breakpoints (self))
bba74b36 10210 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 10211
78eff0ec 10212 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
10213 remote_add_target_side_commands (gdbarch, bp_tgt, p);
10214
6d820c5c
DJ
10215 putpkt (rs->buf);
10216 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 10217
6d820c5c 10218 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10219 {
10220 case PACKET_ERROR:
dd61ec5c
MW
10221 if (rs->buf[1] == '.')
10222 {
10223 message = strchr (rs->buf + 2, '.');
10224 if (message)
0316657e 10225 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
10226 }
10227 return -1;
d471ea57
AC
10228 case PACKET_UNKNOWN:
10229 return -1;
10230 case PACKET_OK:
10231 return 0;
10232 }
8e65ff28 10233 internal_error (__FILE__, __LINE__,
e2e0b3e5 10234 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
10235}
10236
d471ea57 10237
802188a7 10238static int
a64dc96c 10239remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 10240 struct bp_target_info *bp_tgt)
96baa820 10241{
8181d85f 10242 CORE_ADDR addr;
d01949b6 10243 struct remote_state *rs = get_remote_state ();
6d820c5c 10244 char *p = rs->buf;
bba74b36 10245 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 10246
4082afcc 10247 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 10248 return -1;
802188a7 10249
28439a30
PA
10250 /* Make sure the remote is pointing at the right process, if
10251 necessary. */
10252 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10253 set_general_process ();
10254
96baa820
JM
10255 *(p++) = 'z';
10256 *(p++) = '1';
10257 *(p++) = ',';
802188a7 10258
8181d85f 10259 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 10260 p += hexnumstr (p, (ULONGEST) addr);
579c6ad9 10261 xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
96baa820 10262
6d820c5c
DJ
10263 putpkt (rs->buf);
10264 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 10265
6d820c5c 10266 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
10267 {
10268 case PACKET_ERROR:
10269 case PACKET_UNKNOWN:
10270 return -1;
10271 case PACKET_OK:
10272 return 0;
10273 }
8e65ff28 10274 internal_error (__FILE__, __LINE__,
e2e0b3e5 10275 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 10276}
96baa820 10277
4a5e7a5b
PA
10278/* Verify memory using the "qCRC:" request. */
10279
10280static int
10281remote_verify_memory (struct target_ops *ops,
10282 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
10283{
10284 struct remote_state *rs = get_remote_state ();
10285 unsigned long host_crc, target_crc;
10286 char *tmp;
10287
936d2992
PA
10288 /* It doesn't make sense to use qCRC if the remote target is
10289 connected but not running. */
10290 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
10291 {
10292 enum packet_result result;
28439a30 10293
936d2992
PA
10294 /* Make sure the remote is pointing at the right process. */
10295 set_general_process ();
4a5e7a5b 10296
936d2992
PA
10297 /* FIXME: assumes lma can fit into long. */
10298 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
10299 (long) lma, (long) size);
10300 putpkt (rs->buf);
4a5e7a5b 10301
936d2992
PA
10302 /* Be clever; compute the host_crc before waiting for target
10303 reply. */
10304 host_crc = xcrc32 (data, size, 0xffffffff);
10305
10306 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 10307
936d2992
PA
10308 result = packet_ok (rs->buf,
10309 &remote_protocol_packets[PACKET_qCRC]);
10310 if (result == PACKET_ERROR)
10311 return -1;
10312 else if (result == PACKET_OK)
10313 {
10314 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
10315 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 10316
936d2992
PA
10317 return (host_crc == target_crc);
10318 }
10319 }
4a5e7a5b 10320
936d2992 10321 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
10322}
10323
c906108c
SS
10324/* compare-sections command
10325
10326 With no arguments, compares each loadable section in the exec bfd
10327 with the same memory range on the target, and reports mismatches.
4a5e7a5b 10328 Useful for verifying the image on the target against the exec file. */
e514a9d6 10329
c906108c 10330static void
ac88e2de 10331compare_sections_command (const char *args, int from_tty)
c906108c
SS
10332{
10333 asection *s;
c906108c 10334 struct cleanup *old_chain;
948f8e3d 10335 gdb_byte *sectdata;
ce359b09 10336 const char *sectname;
c906108c
SS
10337 bfd_size_type size;
10338 bfd_vma lma;
10339 int matched = 0;
10340 int mismatched = 0;
4a5e7a5b 10341 int res;
95cf3b38 10342 int read_only = 0;
c906108c
SS
10343
10344 if (!exec_bfd)
8a3fe4f8 10345 error (_("command cannot be used without an exec file"));
c906108c 10346
28439a30
PA
10347 /* Make sure the remote is pointing at the right process. */
10348 set_general_process ();
10349
95cf3b38
DT
10350 if (args != NULL && strcmp (args, "-r") == 0)
10351 {
10352 read_only = 1;
10353 args = NULL;
10354 }
10355
c5aa993b 10356 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
10357 {
10358 if (!(s->flags & SEC_LOAD))
0df8b418 10359 continue; /* Skip non-loadable section. */
c906108c 10360
95cf3b38
DT
10361 if (read_only && (s->flags & SEC_READONLY) == 0)
10362 continue; /* Skip writeable sections */
10363
2c500098 10364 size = bfd_get_section_size (s);
c906108c 10365 if (size == 0)
0df8b418 10366 continue; /* Skip zero-length section. */
c906108c 10367
ce359b09 10368 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 10369 if (args && strcmp (args, sectname) != 0)
0df8b418 10370 continue; /* Not the section selected by user. */
c906108c 10371
0df8b418 10372 matched = 1; /* Do this section. */
c906108c 10373 lma = s->lma;
c906108c 10374
224c3ddb 10375 sectdata = (gdb_byte *) xmalloc (size);
b8c9b27d 10376 old_chain = make_cleanup (xfree, sectdata);
c906108c 10377 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 10378
4a5e7a5b
PA
10379 res = target_verify_memory (sectdata, lma, size);
10380
10381 if (res == -1)
5af949e3 10382 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
10383 paddress (target_gdbarch (), lma),
10384 paddress (target_gdbarch (), lma + size));
c906108c 10385
5af949e3 10386 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
10387 paddress (target_gdbarch (), lma),
10388 paddress (target_gdbarch (), lma + size));
4a5e7a5b 10389 if (res)
c906108c
SS
10390 printf_filtered ("matched.\n");
10391 else
c5aa993b
JM
10392 {
10393 printf_filtered ("MIS-MATCHED!\n");
10394 mismatched++;
10395 }
c906108c
SS
10396
10397 do_cleanups (old_chain);
10398 }
10399 if (mismatched > 0)
936d2992 10400 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 10401the loaded file\n"));
c906108c 10402 if (args && !matched)
a3f17187 10403 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
10404}
10405
0e7f50da
UW
10406/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
10407 into remote target. The number of bytes written to the remote
10408 target is returned, or -1 for error. */
10409
9b409511 10410static enum target_xfer_status
0e7f50da
UW
10411remote_write_qxfer (struct target_ops *ops, const char *object_name,
10412 const char *annex, const gdb_byte *writebuf,
9b409511 10413 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
10414 struct packet_config *packet)
10415{
10416 int i, buf_len;
10417 ULONGEST n;
0e7f50da
UW
10418 struct remote_state *rs = get_remote_state ();
10419 int max_size = get_memory_write_packet_size ();
10420
10421 if (packet->support == PACKET_DISABLE)
2ed4b548 10422 return TARGET_XFER_E_IO;
0e7f50da
UW
10423
10424 /* Insert header. */
10425 i = snprintf (rs->buf, max_size,
10426 "qXfer:%s:write:%s:%s:",
10427 object_name, annex ? annex : "",
10428 phex_nz (offset, sizeof offset));
10429 max_size -= (i + 1);
10430
10431 /* Escape as much data as fits into rs->buf. */
10432 buf_len = remote_escape_output
124e13d9 10433 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
10434
10435 if (putpkt_binary (rs->buf, i + buf_len) < 0
10436 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10437 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10438 return TARGET_XFER_E_IO;
0e7f50da
UW
10439
10440 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
10441
10442 *xfered_len = n;
10443 return TARGET_XFER_OK;
0e7f50da
UW
10444}
10445
0876f84a
DJ
10446/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
10447 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
10448 number of bytes read is returned, or 0 for EOF, or -1 for error.
10449 The number of bytes read may be less than LEN without indicating an
10450 EOF. PACKET is checked and updated to indicate whether the remote
10451 target supports this object. */
10452
9b409511 10453static enum target_xfer_status
0876f84a
DJ
10454remote_read_qxfer (struct target_ops *ops, const char *object_name,
10455 const char *annex,
10456 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 10457 ULONGEST *xfered_len,
0876f84a
DJ
10458 struct packet_config *packet)
10459{
0876f84a 10460 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
10461 LONGEST i, n, packet_len;
10462
10463 if (packet->support == PACKET_DISABLE)
2ed4b548 10464 return TARGET_XFER_E_IO;
0876f84a
DJ
10465
10466 /* Check whether we've cached an end-of-object packet that matches
10467 this request. */
8e88304f 10468 if (rs->finished_object)
0876f84a 10469 {
8e88304f
TT
10470 if (strcmp (object_name, rs->finished_object) == 0
10471 && strcmp (annex ? annex : "", rs->finished_annex) == 0
10472 && offset == rs->finished_offset)
9b409511
YQ
10473 return TARGET_XFER_EOF;
10474
0876f84a
DJ
10475
10476 /* Otherwise, we're now reading something different. Discard
10477 the cache. */
8e88304f
TT
10478 xfree (rs->finished_object);
10479 xfree (rs->finished_annex);
10480 rs->finished_object = NULL;
10481 rs->finished_annex = NULL;
0876f84a
DJ
10482 }
10483
10484 /* Request only enough to fit in a single packet. The actual data
10485 may not, since we don't know how much of it will need to be escaped;
10486 the target is free to respond with slightly less data. We subtract
10487 five to account for the response type and the protocol frame. */
768adc05 10488 n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
0876f84a
DJ
10489 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
10490 object_name, annex ? annex : "",
10491 phex_nz (offset, sizeof offset),
10492 phex_nz (n, sizeof n));
10493 i = putpkt (rs->buf);
10494 if (i < 0)
2ed4b548 10495 return TARGET_XFER_E_IO;
0876f84a
DJ
10496
10497 rs->buf[0] = '\0';
10498 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10499 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 10500 return TARGET_XFER_E_IO;
0876f84a
DJ
10501
10502 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
10503 error (_("Unknown remote qXfer reply: %s"), rs->buf);
10504
10505 /* 'm' means there is (or at least might be) more data after this
10506 batch. That does not make sense unless there's at least one byte
10507 of data in this reply. */
10508 if (rs->buf[0] == 'm' && packet_len == 1)
10509 error (_("Remote qXfer reply contained no data."));
10510
10511 /* Got some data. */
bc20a4af
PA
10512 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
10513 packet_len - 1, readbuf, n);
0876f84a
DJ
10514
10515 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
10516 or possibly empty. If we have the final block of a non-empty
10517 object, record this fact to bypass a subsequent partial read. */
10518 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 10519 {
8e88304f
TT
10520 rs->finished_object = xstrdup (object_name);
10521 rs->finished_annex = xstrdup (annex ? annex : "");
10522 rs->finished_offset = offset + i;
0876f84a
DJ
10523 }
10524
9b409511
YQ
10525 if (i == 0)
10526 return TARGET_XFER_EOF;
10527 else
10528 {
10529 *xfered_len = i;
10530 return TARGET_XFER_OK;
10531 }
0876f84a
DJ
10532}
10533
9b409511 10534static enum target_xfer_status
4b8a223f 10535remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 10536 const char *annex, gdb_byte *readbuf,
9b409511
YQ
10537 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
10538 ULONGEST *xfered_len)
c906108c 10539{
82f73884 10540 struct remote_state *rs;
c906108c 10541 int i;
6d820c5c 10542 char *p2;
1e3ff5ad 10543 char query_type;
124e13d9 10544 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 10545
e6e4e701 10546 set_remote_traceframe ();
82f73884
PA
10547 set_general_thread (inferior_ptid);
10548
10549 rs = get_remote_state ();
10550
b2182ed2 10551 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
10552 if (object == TARGET_OBJECT_MEMORY)
10553 {
2d717e4f
DJ
10554 /* If the remote target is connected but not running, we should
10555 pass this request down to a lower stratum (e.g. the executable
10556 file). */
10557 if (!target_has_execution)
9b409511 10558 return TARGET_XFER_EOF;
2d717e4f 10559
21e3b9b9 10560 if (writebuf != NULL)
124e13d9
SM
10561 return remote_write_bytes (offset, writebuf, len, unit_size,
10562 xfered_len);
21e3b9b9 10563 else
124e13d9
SM
10564 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
10565 xfered_len);
21e3b9b9
DJ
10566 }
10567
0df8b418 10568 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
10569 if (object == TARGET_OBJECT_SPU)
10570 {
10571 if (readbuf)
10572 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
10573 xfered_len, &remote_protocol_packets
10574 [PACKET_qXfer_spu_read]);
0e7f50da
UW
10575 else
10576 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
10577 xfered_len, &remote_protocol_packets
10578 [PACKET_qXfer_spu_write]);
0e7f50da
UW
10579 }
10580
4aa995e1
PA
10581 /* Handle extra signal info using qxfer packets. */
10582 if (object == TARGET_OBJECT_SIGNAL_INFO)
10583 {
10584 if (readbuf)
10585 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 10586 xfered_len, &remote_protocol_packets
4aa995e1
PA
10587 [PACKET_qXfer_siginfo_read]);
10588 else
3e43a32a 10589 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 10590 writebuf, offset, len, xfered_len,
4aa995e1
PA
10591 &remote_protocol_packets
10592 [PACKET_qXfer_siginfo_write]);
10593 }
10594
0fb4aa4b
PA
10595 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
10596 {
10597 if (readbuf)
3e43a32a 10598 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 10599 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
10600 &remote_protocol_packets
10601 [PACKET_qXfer_statictrace_read]);
10602 else
2ed4b548 10603 return TARGET_XFER_E_IO;
0fb4aa4b
PA
10604 }
10605
a76d924d
DJ
10606 /* Only handle flash writes. */
10607 if (writebuf != NULL)
10608 {
a76d924d
DJ
10609 switch (object)
10610 {
10611 case TARGET_OBJECT_FLASH:
9b409511
YQ
10612 return remote_flash_write (ops, offset, len, xfered_len,
10613 writebuf);
a76d924d
DJ
10614
10615 default:
2ed4b548 10616 return TARGET_XFER_E_IO;
a76d924d
DJ
10617 }
10618 }
4b8a223f 10619
1e3ff5ad
AC
10620 /* Map pre-existing objects onto letters. DO NOT do this for new
10621 objects!!! Instead specify new query packets. */
10622 switch (object)
c906108c 10623 {
1e3ff5ad
AC
10624 case TARGET_OBJECT_AVR:
10625 query_type = 'R';
10626 break;
802188a7
RM
10627
10628 case TARGET_OBJECT_AUXV:
0876f84a
DJ
10629 gdb_assert (annex == NULL);
10630 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 10631 xfered_len,
0876f84a 10632 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 10633
23181151
DJ
10634 case TARGET_OBJECT_AVAILABLE_FEATURES:
10635 return remote_read_qxfer
9b409511 10636 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
10637 &remote_protocol_packets[PACKET_qXfer_features]);
10638
cfa9d6d9
DJ
10639 case TARGET_OBJECT_LIBRARIES:
10640 return remote_read_qxfer
9b409511 10641 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
10642 &remote_protocol_packets[PACKET_qXfer_libraries]);
10643
2268b414
JK
10644 case TARGET_OBJECT_LIBRARIES_SVR4:
10645 return remote_read_qxfer
9b409511 10646 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
10647 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
10648
fd79ecee
DJ
10649 case TARGET_OBJECT_MEMORY_MAP:
10650 gdb_assert (annex == NULL);
10651 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 10652 xfered_len,
fd79ecee
DJ
10653 &remote_protocol_packets[PACKET_qXfer_memory_map]);
10654
07e059b5
VP
10655 case TARGET_OBJECT_OSDATA:
10656 /* Should only get here if we're connected. */
5d93a237 10657 gdb_assert (rs->remote_desc);
07e059b5 10658 return remote_read_qxfer
9b409511 10659 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
10660 &remote_protocol_packets[PACKET_qXfer_osdata]);
10661
dc146f7c
VP
10662 case TARGET_OBJECT_THREADS:
10663 gdb_assert (annex == NULL);
10664 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 10665 xfered_len,
dc146f7c
VP
10666 &remote_protocol_packets[PACKET_qXfer_threads]);
10667
b3b9301e
PA
10668 case TARGET_OBJECT_TRACEFRAME_INFO:
10669 gdb_assert (annex == NULL);
10670 return remote_read_qxfer
9b409511 10671 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 10672 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
10673
10674 case TARGET_OBJECT_FDPIC:
10675 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 10676 xfered_len,
78d85199 10677 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
10678
10679 case TARGET_OBJECT_OPENVMS_UIB:
10680 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 10681 xfered_len,
169081d0
TG
10682 &remote_protocol_packets[PACKET_qXfer_uib]);
10683
9accd112
MM
10684 case TARGET_OBJECT_BTRACE:
10685 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 10686 xfered_len,
9accd112
MM
10687 &remote_protocol_packets[PACKET_qXfer_btrace]);
10688
f4abbc16
MM
10689 case TARGET_OBJECT_BTRACE_CONF:
10690 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
10691 len, xfered_len,
10692 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
10693
c78fa86a
GB
10694 case TARGET_OBJECT_EXEC_FILE:
10695 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
10696 len, xfered_len,
10697 &remote_protocol_packets[PACKET_qXfer_exec_file]);
10698
1e3ff5ad 10699 default:
2ed4b548 10700 return TARGET_XFER_E_IO;
c906108c
SS
10701 }
10702
0df8b418 10703 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 10704 large enough let the caller deal with it. */
ea9c271d 10705 if (len < get_remote_packet_size ())
2ed4b548 10706 return TARGET_XFER_E_IO;
ea9c271d 10707 len = get_remote_packet_size ();
1e3ff5ad 10708
23860348 10709 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 10710 if (!rs->remote_desc)
8a3fe4f8 10711 error (_("remote query is only available after target open"));
c906108c 10712
1e3ff5ad 10713 gdb_assert (annex != NULL);
4b8a223f 10714 gdb_assert (readbuf != NULL);
c906108c 10715
6d820c5c 10716 p2 = rs->buf;
c906108c
SS
10717 *p2++ = 'q';
10718 *p2++ = query_type;
10719
23860348
MS
10720 /* We used one buffer char for the remote protocol q command and
10721 another for the query type. As the remote protocol encapsulation
10722 uses 4 chars plus one extra in case we are debugging
10723 (remote_debug), we have PBUFZIZ - 7 left to pack the query
10724 string. */
c906108c 10725 i = 0;
ea9c271d 10726 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 10727 {
1e3ff5ad
AC
10728 /* Bad caller may have sent forbidden characters. */
10729 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
10730 *p2++ = annex[i];
c906108c
SS
10731 i++;
10732 }
1e3ff5ad
AC
10733 *p2 = '\0';
10734 gdb_assert (annex[i] == '\0');
c906108c 10735
6d820c5c 10736 i = putpkt (rs->buf);
c5aa993b 10737 if (i < 0)
2ed4b548 10738 return TARGET_XFER_E_IO;
c906108c 10739
6d820c5c
DJ
10740 getpkt (&rs->buf, &rs->buf_size, 0);
10741 strcpy ((char *) readbuf, rs->buf);
c906108c 10742
9b409511
YQ
10743 *xfered_len = strlen ((char *) readbuf);
10744 return TARGET_XFER_OK;
c906108c
SS
10745}
10746
09c98b44
DB
10747/* Implementation of to_get_memory_xfer_limit. */
10748
10749static ULONGEST
10750remote_get_memory_xfer_limit (struct target_ops *ops)
10751{
10752 return get_memory_write_packet_size ();
10753}
10754
08388c79
DE
10755static int
10756remote_search_memory (struct target_ops* ops,
10757 CORE_ADDR start_addr, ULONGEST search_space_len,
10758 const gdb_byte *pattern, ULONGEST pattern_len,
10759 CORE_ADDR *found_addrp)
10760{
f5656ead 10761 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
10762 struct remote_state *rs = get_remote_state ();
10763 int max_size = get_memory_write_packet_size ();
10764 struct packet_config *packet =
10765 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
10766 /* Number of packet bytes used to encode the pattern;
10767 this could be more than PATTERN_LEN due to escape characters. */
08388c79 10768 int escaped_pattern_len;
0df8b418 10769 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
10770 int used_pattern_len;
10771 int i;
10772 int found;
10773 ULONGEST found_addr;
10774
10775 /* Don't go to the target if we don't have to.
10776 This is done before checking packet->support to avoid the possibility that
10777 a success for this edge case means the facility works in general. */
10778 if (pattern_len > search_space_len)
10779 return 0;
10780 if (pattern_len == 0)
10781 {
10782 *found_addrp = start_addr;
10783 return 1;
10784 }
10785
10786 /* If we already know the packet isn't supported, fall back to the simple
10787 way of searching memory. */
10788
4082afcc 10789 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
10790 {
10791 /* Target doesn't provided special support, fall back and use the
10792 standard support (copy memory and do the search here). */
10793 return simple_search_memory (ops, start_addr, search_space_len,
10794 pattern, pattern_len, found_addrp);
10795 }
10796
28439a30
PA
10797 /* Make sure the remote is pointing at the right process. */
10798 set_general_process ();
10799
08388c79
DE
10800 /* Insert header. */
10801 i = snprintf (rs->buf, max_size,
10802 "qSearch:memory:%s;%s;",
5af949e3 10803 phex_nz (start_addr, addr_size),
08388c79
DE
10804 phex_nz (search_space_len, sizeof (search_space_len)));
10805 max_size -= (i + 1);
10806
10807 /* Escape as much data as fits into rs->buf. */
10808 escaped_pattern_len =
124e13d9 10809 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
10810 &used_pattern_len, max_size);
10811
10812 /* Bail if the pattern is too large. */
10813 if (used_pattern_len != pattern_len)
9b20d036 10814 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
10815
10816 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
10817 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10818 || packet_ok (rs->buf, packet) != PACKET_OK)
10819 {
10820 /* The request may not have worked because the command is not
10821 supported. If so, fall back to the simple way. */
10822 if (packet->support == PACKET_DISABLE)
10823 {
10824 return simple_search_memory (ops, start_addr, search_space_len,
10825 pattern, pattern_len, found_addrp);
10826 }
10827 return -1;
10828 }
10829
10830 if (rs->buf[0] == '0')
10831 found = 0;
10832 else if (rs->buf[0] == '1')
10833 {
10834 found = 1;
10835 if (rs->buf[1] != ',')
10e0fa18 10836 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10837 unpack_varlen_hex (rs->buf + 2, &found_addr);
10838 *found_addrp = found_addr;
10839 }
10840 else
10e0fa18 10841 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10842
10843 return found;
10844}
10845
96baa820 10846static void
a30bf1f1 10847remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 10848 struct ui_file *outbuf)
96baa820 10849{
d01949b6 10850 struct remote_state *rs = get_remote_state ();
2e9f7625 10851 char *p = rs->buf;
96baa820 10852
5d93a237 10853 if (!rs->remote_desc)
8a3fe4f8 10854 error (_("remote rcmd is only available after target open"));
96baa820 10855
23860348 10856 /* Send a NULL command across as an empty command. */
7be570e7
JM
10857 if (command == NULL)
10858 command = "";
10859
23860348 10860 /* The query prefix. */
2e9f7625
DJ
10861 strcpy (rs->buf, "qRcmd,");
10862 p = strchr (rs->buf, '\0');
96baa820 10863
3e43a32a
MS
10864 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
10865 > get_remote_packet_size ())
8a3fe4f8 10866 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 10867
23860348 10868 /* Encode the actual command. */
a30bf1f1 10869 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 10870
6d820c5c 10871 if (putpkt (rs->buf) < 0)
8a3fe4f8 10872 error (_("Communication problem with target."));
96baa820
JM
10873
10874 /* get/display the response */
10875 while (1)
10876 {
2e9f7625
DJ
10877 char *buf;
10878
00bf0b85 10879 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 10880 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 10881 rs->buf[0] = '\0';
5b37825d
PW
10882 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
10883 {
10884 /* Timeout. Continue to (try to) read responses.
10885 This is better than stopping with an error, assuming the stub
10886 is still executing the (long) monitor command.
10887 If needed, the user can interrupt gdb using C-c, obtaining
10888 an effect similar to stop on timeout. */
10889 continue;
10890 }
2e9f7625 10891 buf = rs->buf;
96baa820 10892 if (buf[0] == '\0')
8a3fe4f8 10893 error (_("Target does not support this command."));
96baa820
JM
10894 if (buf[0] == 'O' && buf[1] != 'K')
10895 {
23860348 10896 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
10897 continue;
10898 }
10899 if (strcmp (buf, "OK") == 0)
10900 break;
7be570e7
JM
10901 if (strlen (buf) == 3 && buf[0] == 'E'
10902 && isdigit (buf[1]) && isdigit (buf[2]))
10903 {
8a3fe4f8 10904 error (_("Protocol error with Rcmd"));
7be570e7 10905 }
96baa820
JM
10906 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
10907 {
10908 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 10909
96baa820
JM
10910 fputc_unfiltered (c, outbuf);
10911 }
10912 break;
10913 }
10914}
10915
fd79ecee
DJ
10916static VEC(mem_region_s) *
10917remote_memory_map (struct target_ops *ops)
10918{
10919 VEC(mem_region_s) *result = NULL;
10920 char *text = target_read_stralloc (&current_target,
10921 TARGET_OBJECT_MEMORY_MAP, NULL);
10922
10923 if (text)
10924 {
10925 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 10926
fd79ecee
DJ
10927 result = parse_memory_map (text);
10928 do_cleanups (back_to);
10929 }
10930
10931 return result;
10932}
10933
c906108c 10934static void
ac88e2de 10935packet_command (const char *args, int from_tty)
c906108c 10936{
d01949b6 10937 struct remote_state *rs = get_remote_state ();
c906108c 10938
5d93a237 10939 if (!rs->remote_desc)
8a3fe4f8 10940 error (_("command can only be used with remote target"));
c906108c 10941
c5aa993b 10942 if (!args)
8a3fe4f8 10943 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
10944
10945 puts_filtered ("sending: ");
10946 print_packet (args);
10947 puts_filtered ("\n");
10948 putpkt (args);
10949
6d820c5c 10950 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 10951 puts_filtered ("received: ");
6d820c5c 10952 print_packet (rs->buf);
c906108c
SS
10953 puts_filtered ("\n");
10954}
10955
10956#if 0
23860348 10957/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 10958
a14ed312 10959static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 10960
a14ed312 10961static void threadset_test_cmd (char *cmd, int tty);
c906108c 10962
a14ed312 10963static void threadalive_test (char *cmd, int tty);
c906108c 10964
a14ed312 10965static void threadlist_test_cmd (char *cmd, int tty);
c906108c 10966
23860348 10967int get_and_display_threadinfo (threadref *ref);
c906108c 10968
a14ed312 10969static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 10970
23860348 10971static int thread_display_step (threadref *ref, void *context);
c906108c 10972
a14ed312 10973static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 10974
a14ed312 10975static void init_remote_threadtests (void);
c906108c 10976
23860348 10977#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
10978
10979static void
fba45db2 10980threadset_test_cmd (char *cmd, int tty)
c906108c
SS
10981{
10982 int sample_thread = SAMPLE_THREAD;
10983
a3f17187 10984 printf_filtered (_("Remote threadset test\n"));
79d7f229 10985 set_general_thread (sample_thread);
c906108c
SS
10986}
10987
10988
10989static void
fba45db2 10990threadalive_test (char *cmd, int tty)
c906108c
SS
10991{
10992 int sample_thread = SAMPLE_THREAD;
79d7f229 10993 int pid = ptid_get_pid (inferior_ptid);
ba348170 10994 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 10995
79d7f229 10996 if (remote_thread_alive (ptid))
c906108c
SS
10997 printf_filtered ("PASS: Thread alive test\n");
10998 else
10999 printf_filtered ("FAIL: Thread alive test\n");
11000}
11001
23860348 11002void output_threadid (char *title, threadref *ref);
c906108c
SS
11003
11004void
fba45db2 11005output_threadid (char *title, threadref *ref)
c906108c
SS
11006{
11007 char hexid[20];
11008
23860348 11009 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
11010 hexid[16] = 0;
11011 printf_filtered ("%s %s\n", title, (&hexid[0]));
11012}
11013
11014static void
fba45db2 11015threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
11016{
11017 int startflag = 1;
11018 threadref nextthread;
11019 int done, result_count;
11020 threadref threadlist[3];
11021
11022 printf_filtered ("Remote Threadlist test\n");
11023 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
11024 &result_count, &threadlist[0]))
11025 printf_filtered ("FAIL: threadlist test\n");
11026 else
11027 {
11028 threadref *scan = threadlist;
11029 threadref *limit = scan + result_count;
11030
11031 while (scan < limit)
11032 output_threadid (" thread ", scan++);
11033 }
11034}
11035
11036void
fba45db2 11037display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
11038{
11039 output_threadid ("Threadid: ", &info->threadid);
11040 printf_filtered ("Name: %s\n ", info->shortname);
11041 printf_filtered ("State: %s\n", info->display);
11042 printf_filtered ("other: %s\n\n", info->more_display);
11043}
11044
11045int
fba45db2 11046get_and_display_threadinfo (threadref *ref)
c906108c
SS
11047{
11048 int result;
11049 int set;
11050 struct gdb_ext_thread_info threadinfo;
11051
11052 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
11053 | TAG_MOREDISPLAY | TAG_DISPLAY;
11054 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
11055 display_thread_info (&threadinfo);
11056 return result;
11057}
11058
11059static void
fba45db2 11060threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
11061{
11062 int athread = SAMPLE_THREAD;
11063 threadref thread;
11064 int set;
11065
11066 int_to_threadref (&thread, athread);
11067 printf_filtered ("Remote Threadinfo test\n");
11068 if (!get_and_display_threadinfo (&thread))
11069 printf_filtered ("FAIL cannot get thread info\n");
11070}
11071
11072static int
fba45db2 11073thread_display_step (threadref *ref, void *context)
c906108c
SS
11074{
11075 /* output_threadid(" threadstep ",ref); *//* simple test */
11076 return get_and_display_threadinfo (ref);
11077}
11078
11079static void
fba45db2 11080threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
11081{
11082 printf_filtered ("Remote Threadlist update test\n");
11083 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
11084}
11085
11086static void
11087init_remote_threadtests (void)
11088{
3e43a32a
MS
11089 add_com ("tlist", class_obscure, threadlist_test_cmd,
11090 _("Fetch and print the remote list of "
11091 "thread identifiers, one pkt only"));
c906108c 11092 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 11093 _("Fetch and display info about one thread"));
c906108c 11094 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 11095 _("Test setting to a different thread"));
c906108c 11096 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 11097 _("Iterate through updating all remote thread info"));
c906108c 11098 add_com ("talive", class_obscure, threadalive_test,
1bedd215 11099 _(" Remote thread alive test "));
c906108c
SS
11100}
11101
11102#endif /* 0 */
11103
f3fb8c85
MS
11104/* Convert a thread ID to a string. Returns the string in a static
11105 buffer. */
11106
7a114964 11107static const char *
117de6a9 11108remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 11109{
79d7f229 11110 static char buf[64];
82f73884 11111 struct remote_state *rs = get_remote_state ();
f3fb8c85 11112
7cee1e54
PA
11113 if (ptid_equal (ptid, null_ptid))
11114 return normal_pid_to_str (ptid);
11115 else if (ptid_is_pid (ptid))
ecd0ada5
PA
11116 {
11117 /* Printing an inferior target id. */
11118
11119 /* When multi-process extensions are off, there's no way in the
11120 remote protocol to know the remote process id, if there's any
11121 at all. There's one exception --- when we're connected with
11122 target extended-remote, and we manually attached to a process
11123 with "attach PID". We don't record anywhere a flag that
11124 allows us to distinguish that case from the case of
11125 connecting with extended-remote and the stub already being
11126 attached to a process, and reporting yes to qAttached, hence
11127 no smart special casing here. */
11128 if (!remote_multi_process_p (rs))
11129 {
11130 xsnprintf (buf, sizeof buf, "Remote target");
11131 return buf;
11132 }
11133
11134 return normal_pid_to_str (ptid);
82f73884 11135 }
ecd0ada5 11136 else
79d7f229 11137 {
ecd0ada5
PA
11138 if (ptid_equal (magic_null_ptid, ptid))
11139 xsnprintf (buf, sizeof buf, "Thread <main>");
8020350c 11140 else if (remote_multi_process_p (rs))
de0d863e
DB
11141 if (ptid_get_lwp (ptid) == 0)
11142 return normal_pid_to_str (ptid);
11143 else
11144 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
11145 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
11146 else
11147 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 11148 ptid_get_lwp (ptid));
79d7f229
PA
11149 return buf;
11150 }
f3fb8c85
MS
11151}
11152
38691318
KB
11153/* Get the address of the thread local variable in OBJFILE which is
11154 stored at OFFSET within the thread local storage for thread PTID. */
11155
11156static CORE_ADDR
117de6a9
PA
11157remote_get_thread_local_address (struct target_ops *ops,
11158 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 11159{
4082afcc 11160 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
11161 {
11162 struct remote_state *rs = get_remote_state ();
6d820c5c 11163 char *p = rs->buf;
82f73884 11164 char *endp = rs->buf + get_remote_packet_size ();
571dd617 11165 enum packet_result result;
38691318
KB
11166
11167 strcpy (p, "qGetTLSAddr:");
11168 p += strlen (p);
82f73884 11169 p = write_ptid (p, endp, ptid);
38691318
KB
11170 *p++ = ',';
11171 p += hexnumstr (p, offset);
11172 *p++ = ',';
11173 p += hexnumstr (p, lm);
11174 *p++ = '\0';
11175
6d820c5c
DJ
11176 putpkt (rs->buf);
11177 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
11178 result = packet_ok (rs->buf,
11179 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 11180 if (result == PACKET_OK)
38691318
KB
11181 {
11182 ULONGEST result;
11183
6d820c5c 11184 unpack_varlen_hex (rs->buf, &result);
38691318
KB
11185 return result;
11186 }
571dd617 11187 else if (result == PACKET_UNKNOWN)
109c3e39
AC
11188 throw_error (TLS_GENERIC_ERROR,
11189 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 11190 else
109c3e39
AC
11191 throw_error (TLS_GENERIC_ERROR,
11192 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
11193 }
11194 else
109c3e39
AC
11195 throw_error (TLS_GENERIC_ERROR,
11196 _("TLS not supported or disabled on this target"));
38691318
KB
11197 /* Not reached. */
11198 return 0;
11199}
11200
711e434b
PM
11201/* Provide thread local base, i.e. Thread Information Block address.
11202 Returns 1 if ptid is found and thread_local_base is non zero. */
11203
70221824 11204static int
bd7ae0f5 11205remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 11206{
4082afcc 11207 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
11208 {
11209 struct remote_state *rs = get_remote_state ();
11210 char *p = rs->buf;
11211 char *endp = rs->buf + get_remote_packet_size ();
11212 enum packet_result result;
11213
11214 strcpy (p, "qGetTIBAddr:");
11215 p += strlen (p);
11216 p = write_ptid (p, endp, ptid);
11217 *p++ = '\0';
11218
11219 putpkt (rs->buf);
11220 getpkt (&rs->buf, &rs->buf_size, 0);
11221 result = packet_ok (rs->buf,
11222 &remote_protocol_packets[PACKET_qGetTIBAddr]);
11223 if (result == PACKET_OK)
11224 {
11225 ULONGEST result;
11226
11227 unpack_varlen_hex (rs->buf, &result);
11228 if (addr)
11229 *addr = (CORE_ADDR) result;
11230 return 1;
11231 }
11232 else if (result == PACKET_UNKNOWN)
11233 error (_("Remote target doesn't support qGetTIBAddr packet"));
11234 else
11235 error (_("Remote target failed to process qGetTIBAddr request"));
11236 }
11237 else
11238 error (_("qGetTIBAddr not supported or disabled on this target"));
11239 /* Not reached. */
11240 return 0;
11241}
11242
29709017
DJ
11243/* Support for inferring a target description based on the current
11244 architecture and the size of a 'g' packet. While the 'g' packet
11245 can have any size (since optional registers can be left off the
11246 end), some sizes are easily recognizable given knowledge of the
11247 approximate architecture. */
11248
11249struct remote_g_packet_guess
11250{
11251 int bytes;
11252 const struct target_desc *tdesc;
11253};
11254typedef struct remote_g_packet_guess remote_g_packet_guess_s;
11255DEF_VEC_O(remote_g_packet_guess_s);
11256
11257struct remote_g_packet_data
11258{
11259 VEC(remote_g_packet_guess_s) *guesses;
11260};
11261
11262static struct gdbarch_data *remote_g_packet_data_handle;
11263
11264static void *
11265remote_g_packet_data_init (struct obstack *obstack)
11266{
11267 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
11268}
11269
11270void
11271register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
11272 const struct target_desc *tdesc)
11273{
11274 struct remote_g_packet_data *data
19ba03f4
SM
11275 = ((struct remote_g_packet_data *)
11276 gdbarch_data (gdbarch, remote_g_packet_data_handle));
29709017
DJ
11277 struct remote_g_packet_guess new_guess, *guess;
11278 int ix;
11279
11280 gdb_assert (tdesc != NULL);
11281
11282 for (ix = 0;
11283 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11284 ix++)
11285 if (guess->bytes == bytes)
11286 internal_error (__FILE__, __LINE__,
9b20d036 11287 _("Duplicate g packet description added for size %d"),
29709017
DJ
11288 bytes);
11289
11290 new_guess.bytes = bytes;
11291 new_guess.tdesc = tdesc;
11292 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
11293}
11294
d962ef82
DJ
11295/* Return 1 if remote_read_description would do anything on this target
11296 and architecture, 0 otherwise. */
11297
11298static int
11299remote_read_description_p (struct target_ops *target)
11300{
11301 struct remote_g_packet_data *data
19ba03f4
SM
11302 = ((struct remote_g_packet_data *)
11303 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
d962ef82
DJ
11304
11305 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11306 return 1;
11307
11308 return 0;
11309}
11310
29709017
DJ
11311static const struct target_desc *
11312remote_read_description (struct target_ops *target)
11313{
11314 struct remote_g_packet_data *data
19ba03f4
SM
11315 = ((struct remote_g_packet_data *)
11316 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
29709017 11317
d962ef82
DJ
11318 /* Do not try this during initial connection, when we do not know
11319 whether there is a running but stopped thread. */
11320 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 11321 return target->beneath->to_read_description (target->beneath);
d962ef82 11322
29709017
DJ
11323 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
11324 {
11325 struct remote_g_packet_guess *guess;
11326 int ix;
11327 int bytes = send_g_packet ();
11328
11329 for (ix = 0;
11330 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
11331 ix++)
11332 if (guess->bytes == bytes)
11333 return guess->tdesc;
11334
11335 /* We discard the g packet. A minor optimization would be to
11336 hold on to it, and fill the register cache once we have selected
11337 an architecture, but it's too tricky to do safely. */
11338 }
11339
2117c711 11340 return target->beneath->to_read_description (target->beneath);
29709017
DJ
11341}
11342
a6b151f1
DJ
11343/* Remote file transfer support. This is host-initiated I/O, not
11344 target-initiated; for target-initiated, see remote-fileio.c. */
11345
11346/* If *LEFT is at least the length of STRING, copy STRING to
11347 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11348 decrease *LEFT. Otherwise raise an error. */
11349
11350static void
a121b7c1 11351remote_buffer_add_string (char **buffer, int *left, const char *string)
a6b151f1
DJ
11352{
11353 int len = strlen (string);
11354
11355 if (len > *left)
11356 error (_("Packet too long for target."));
11357
11358 memcpy (*buffer, string, len);
11359 *buffer += len;
11360 *left -= len;
11361
11362 /* NUL-terminate the buffer as a convenience, if there is
11363 room. */
11364 if (*left)
11365 **buffer = '\0';
11366}
11367
11368/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
11369 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11370 decrease *LEFT. Otherwise raise an error. */
11371
11372static void
11373remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
11374 int len)
11375{
11376 if (2 * len > *left)
11377 error (_("Packet too long for target."));
11378
11379 bin2hex (bytes, *buffer, len);
11380 *buffer += 2 * len;
11381 *left -= 2 * len;
11382
11383 /* NUL-terminate the buffer as a convenience, if there is
11384 room. */
11385 if (*left)
11386 **buffer = '\0';
11387}
11388
11389/* If *LEFT is large enough, convert VALUE to hex and add it to
11390 *BUFFER, update *BUFFER to point to the new end of the buffer, and
11391 decrease *LEFT. Otherwise raise an error. */
11392
11393static void
11394remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
11395{
11396 int len = hexnumlen (value);
11397
11398 if (len > *left)
11399 error (_("Packet too long for target."));
11400
11401 hexnumstr (*buffer, value);
11402 *buffer += len;
11403 *left -= len;
11404
11405 /* NUL-terminate the buffer as a convenience, if there is
11406 room. */
11407 if (*left)
11408 **buffer = '\0';
11409}
11410
11411/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
11412 value, *REMOTE_ERRNO to the remote error number or zero if none
11413 was included, and *ATTACHMENT to point to the start of the annex
11414 if any. The length of the packet isn't needed here; there may
11415 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
11416
11417 Return 0 if the packet could be parsed, -1 if it could not. If
11418 -1 is returned, the other variables may not be initialized. */
11419
11420static int
11421remote_hostio_parse_result (char *buffer, int *retcode,
11422 int *remote_errno, char **attachment)
11423{
11424 char *p, *p2;
11425
11426 *remote_errno = 0;
11427 *attachment = NULL;
11428
11429 if (buffer[0] != 'F')
11430 return -1;
11431
11432 errno = 0;
11433 *retcode = strtol (&buffer[1], &p, 16);
11434 if (errno != 0 || p == &buffer[1])
11435 return -1;
11436
11437 /* Check for ",errno". */
11438 if (*p == ',')
11439 {
11440 errno = 0;
11441 *remote_errno = strtol (p + 1, &p2, 16);
11442 if (errno != 0 || p + 1 == p2)
11443 return -1;
11444 p = p2;
11445 }
11446
11447 /* Check for ";attachment". If there is no attachment, the
11448 packet should end here. */
11449 if (*p == ';')
11450 {
11451 *attachment = p + 1;
11452 return 0;
11453 }
11454 else if (*p == '\0')
11455 return 0;
11456 else
11457 return -1;
11458}
11459
11460/* Send a prepared I/O packet to the target and read its response.
11461 The prepared packet is in the global RS->BUF before this function
11462 is called, and the answer is there when we return.
11463
11464 COMMAND_BYTES is the length of the request to send, which may include
11465 binary data. WHICH_PACKET is the packet configuration to check
11466 before attempting a packet. If an error occurs, *REMOTE_ERRNO
11467 is set to the error number and -1 is returned. Otherwise the value
11468 returned by the function is returned.
11469
11470 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
11471 attachment is expected; an error will be reported if there's a
11472 mismatch. If one is found, *ATTACHMENT will be set to point into
11473 the packet buffer and *ATTACHMENT_LEN will be set to the
11474 attachment's length. */
11475
11476static int
11477remote_hostio_send_command (int command_bytes, int which_packet,
11478 int *remote_errno, char **attachment,
11479 int *attachment_len)
11480{
11481 struct remote_state *rs = get_remote_state ();
11482 int ret, bytes_read;
11483 char *attachment_tmp;
11484
5d93a237 11485 if (!rs->remote_desc
4082afcc 11486 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
11487 {
11488 *remote_errno = FILEIO_ENOSYS;
11489 return -1;
11490 }
11491
11492 putpkt_binary (rs->buf, command_bytes);
11493 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
11494
11495 /* If it timed out, something is wrong. Don't try to parse the
11496 buffer. */
11497 if (bytes_read < 0)
11498 {
11499 *remote_errno = FILEIO_EINVAL;
11500 return -1;
11501 }
11502
11503 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
11504 {
11505 case PACKET_ERROR:
11506 *remote_errno = FILEIO_EINVAL;
11507 return -1;
11508 case PACKET_UNKNOWN:
11509 *remote_errno = FILEIO_ENOSYS;
11510 return -1;
11511 case PACKET_OK:
11512 break;
11513 }
11514
11515 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
11516 &attachment_tmp))
11517 {
11518 *remote_errno = FILEIO_EINVAL;
11519 return -1;
11520 }
11521
11522 /* Make sure we saw an attachment if and only if we expected one. */
11523 if ((attachment_tmp == NULL && attachment != NULL)
11524 || (attachment_tmp != NULL && attachment == NULL))
11525 {
11526 *remote_errno = FILEIO_EINVAL;
11527 return -1;
11528 }
11529
11530 /* If an attachment was found, it must point into the packet buffer;
11531 work out how many bytes there were. */
11532 if (attachment_tmp != NULL)
11533 {
11534 *attachment = attachment_tmp;
11535 *attachment_len = bytes_read - (*attachment - rs->buf);
11536 }
11537
11538 return ret;
11539}
11540
80152258
PA
11541/* Invalidate the readahead cache. */
11542
11543static void
11544readahead_cache_invalidate (void)
11545{
11546 struct remote_state *rs = get_remote_state ();
11547
11548 rs->readahead_cache.fd = -1;
11549}
11550
11551/* Invalidate the readahead cache if it is holding data for FD. */
11552
11553static void
11554readahead_cache_invalidate_fd (int fd)
11555{
11556 struct remote_state *rs = get_remote_state ();
11557
11558 if (rs->readahead_cache.fd == fd)
11559 rs->readahead_cache.fd = -1;
11560}
11561
15a201c8
GB
11562/* Set the filesystem remote_hostio functions that take FILENAME
11563 arguments will use. Return 0 on success, or -1 if an error
11564 occurs (and set *REMOTE_ERRNO). */
11565
11566static int
11567remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
11568{
11569 struct remote_state *rs = get_remote_state ();
11570 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
11571 char *p = rs->buf;
11572 int left = get_remote_packet_size () - 1;
11573 char arg[9];
11574 int ret;
11575
11576 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11577 return 0;
11578
11579 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
11580 return 0;
11581
11582 remote_buffer_add_string (&p, &left, "vFile:setfs:");
11583
11584 xsnprintf (arg, sizeof (arg), "%x", required_pid);
11585 remote_buffer_add_string (&p, &left, arg);
11586
11587 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
11588 remote_errno, NULL, NULL);
11589
11590 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11591 return 0;
11592
11593 if (ret == 0)
11594 rs->fs_pid = required_pid;
11595
11596 return ret;
11597}
11598
12e2a5fd 11599/* Implementation of to_fileio_open. */
a6b151f1
DJ
11600
11601static int
cd897586 11602remote_hostio_open (struct target_ops *self,
07c138c8 11603 struct inferior *inf, const char *filename,
4313b8c0
GB
11604 int flags, int mode, int warn_if_slow,
11605 int *remote_errno)
a6b151f1
DJ
11606{
11607 struct remote_state *rs = get_remote_state ();
11608 char *p = rs->buf;
11609 int left = get_remote_packet_size () - 1;
11610
4313b8c0
GB
11611 if (warn_if_slow)
11612 {
11613 static int warning_issued = 0;
11614
11615 printf_unfiltered (_("Reading %s from remote target...\n"),
11616 filename);
11617
11618 if (!warning_issued)
11619 {
11620 warning (_("File transfers from remote targets can be slow."
11621 " Use \"set sysroot\" to access files locally"
11622 " instead."));
11623 warning_issued = 1;
11624 }
11625 }
11626
15a201c8
GB
11627 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11628 return -1;
11629
a6b151f1
DJ
11630 remote_buffer_add_string (&p, &left, "vFile:open:");
11631
11632 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11633 strlen (filename));
11634 remote_buffer_add_string (&p, &left, ",");
11635
11636 remote_buffer_add_int (&p, &left, flags);
11637 remote_buffer_add_string (&p, &left, ",");
11638
11639 remote_buffer_add_int (&p, &left, mode);
11640
11641 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
11642 remote_errno, NULL, NULL);
11643}
11644
12e2a5fd 11645/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
11646
11647static int
0d866f62
TT
11648remote_hostio_pwrite (struct target_ops *self,
11649 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
11650 ULONGEST offset, int *remote_errno)
11651{
11652 struct remote_state *rs = get_remote_state ();
11653 char *p = rs->buf;
11654 int left = get_remote_packet_size ();
11655 int out_len;
11656
80152258
PA
11657 readahead_cache_invalidate_fd (fd);
11658
a6b151f1
DJ
11659 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
11660
11661 remote_buffer_add_int (&p, &left, fd);
11662 remote_buffer_add_string (&p, &left, ",");
11663
11664 remote_buffer_add_int (&p, &left, offset);
11665 remote_buffer_add_string (&p, &left, ",");
11666
124e13d9 11667 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
11668 get_remote_packet_size () - (p - rs->buf));
11669
11670 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
11671 remote_errno, NULL, NULL);
11672}
11673
80152258
PA
11674/* Helper for the implementation of to_fileio_pread. Read the file
11675 from the remote side with vFile:pread. */
a6b151f1
DJ
11676
11677static int
80152258
PA
11678remote_hostio_pread_vFile (struct target_ops *self,
11679 int fd, gdb_byte *read_buf, int len,
11680 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
11681{
11682 struct remote_state *rs = get_remote_state ();
11683 char *p = rs->buf;
11684 char *attachment;
11685 int left = get_remote_packet_size ();
11686 int ret, attachment_len;
11687 int read_len;
11688
11689 remote_buffer_add_string (&p, &left, "vFile:pread:");
11690
11691 remote_buffer_add_int (&p, &left, fd);
11692 remote_buffer_add_string (&p, &left, ",");
11693
11694 remote_buffer_add_int (&p, &left, len);
11695 remote_buffer_add_string (&p, &left, ",");
11696
11697 remote_buffer_add_int (&p, &left, offset);
11698
11699 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
11700 remote_errno, &attachment,
11701 &attachment_len);
11702
11703 if (ret < 0)
11704 return ret;
11705
bc20a4af 11706 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
11707 read_buf, len);
11708 if (read_len != ret)
11709 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
11710
11711 return ret;
11712}
11713
80152258
PA
11714/* Serve pread from the readahead cache. Returns number of bytes
11715 read, or 0 if the request can't be served from the cache. */
11716
11717static int
11718remote_hostio_pread_from_cache (struct remote_state *rs,
11719 int fd, gdb_byte *read_buf, size_t len,
11720 ULONGEST offset)
11721{
11722 struct readahead_cache *cache = &rs->readahead_cache;
11723
11724 if (cache->fd == fd
11725 && cache->offset <= offset
11726 && offset < cache->offset + cache->bufsize)
11727 {
11728 ULONGEST max = cache->offset + cache->bufsize;
11729
11730 if (offset + len > max)
11731 len = max - offset;
11732
11733 memcpy (read_buf, cache->buf + offset - cache->offset, len);
11734 return len;
11735 }
11736
11737 return 0;
11738}
11739
11740/* Implementation of to_fileio_pread. */
11741
11742static int
11743remote_hostio_pread (struct target_ops *self,
11744 int fd, gdb_byte *read_buf, int len,
11745 ULONGEST offset, int *remote_errno)
11746{
11747 int ret;
11748 struct remote_state *rs = get_remote_state ();
11749 struct readahead_cache *cache = &rs->readahead_cache;
11750
11751 ret = remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11752 if (ret > 0)
11753 {
11754 cache->hit_count++;
11755
11756 if (remote_debug)
11757 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
11758 pulongest (cache->hit_count));
11759 return ret;
11760 }
11761
11762 cache->miss_count++;
11763 if (remote_debug)
11764 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
11765 pulongest (cache->miss_count));
11766
11767 cache->fd = fd;
11768 cache->offset = offset;
11769 cache->bufsize = get_remote_packet_size ();
224c3ddb 11770 cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
80152258
PA
11771
11772 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
11773 cache->offset, remote_errno);
11774 if (ret <= 0)
11775 {
11776 readahead_cache_invalidate_fd (fd);
11777 return ret;
11778 }
11779
11780 cache->bufsize = ret;
11781 return remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11782}
11783
12e2a5fd 11784/* Implementation of to_fileio_close. */
a6b151f1
DJ
11785
11786static int
df39ea25 11787remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
11788{
11789 struct remote_state *rs = get_remote_state ();
11790 char *p = rs->buf;
11791 int left = get_remote_packet_size () - 1;
11792
80152258
PA
11793 readahead_cache_invalidate_fd (fd);
11794
a6b151f1
DJ
11795 remote_buffer_add_string (&p, &left, "vFile:close:");
11796
11797 remote_buffer_add_int (&p, &left, fd);
11798
11799 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
11800 remote_errno, NULL, NULL);
11801}
11802
12e2a5fd 11803/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
11804
11805static int
dbbca37d 11806remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
11807 struct inferior *inf, const char *filename,
11808 int *remote_errno)
a6b151f1
DJ
11809{
11810 struct remote_state *rs = get_remote_state ();
11811 char *p = rs->buf;
11812 int left = get_remote_packet_size () - 1;
11813
15a201c8
GB
11814 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11815 return -1;
11816
a6b151f1
DJ
11817 remote_buffer_add_string (&p, &left, "vFile:unlink:");
11818
11819 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11820 strlen (filename));
11821
11822 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
11823 remote_errno, NULL, NULL);
11824}
11825
12e2a5fd 11826/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
11827
11828static char *
fab5aa7c 11829remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
11830 struct inferior *inf, const char *filename,
11831 int *remote_errno)
b9e7b9c3
UW
11832{
11833 struct remote_state *rs = get_remote_state ();
11834 char *p = rs->buf;
11835 char *attachment;
11836 int left = get_remote_packet_size ();
11837 int len, attachment_len;
11838 int read_len;
11839 char *ret;
11840
15a201c8
GB
11841 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11842 return NULL;
11843
b9e7b9c3
UW
11844 remote_buffer_add_string (&p, &left, "vFile:readlink:");
11845
11846 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11847 strlen (filename));
11848
11849 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
11850 remote_errno, &attachment,
11851 &attachment_len);
11852
11853 if (len < 0)
11854 return NULL;
11855
224c3ddb 11856 ret = (char *) xmalloc (len + 1);
b9e7b9c3 11857
bc20a4af
PA
11858 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11859 (gdb_byte *) ret, len);
b9e7b9c3
UW
11860 if (read_len != len)
11861 error (_("Readlink returned %d, but %d bytes."), len, read_len);
11862
11863 ret[len] = '\0';
11864 return ret;
11865}
11866
12e2a5fd 11867/* Implementation of to_fileio_fstat. */
0a93529c
GB
11868
11869static int
11870remote_hostio_fstat (struct target_ops *self,
11871 int fd, struct stat *st,
11872 int *remote_errno)
11873{
11874 struct remote_state *rs = get_remote_state ();
11875 char *p = rs->buf;
11876 int left = get_remote_packet_size ();
11877 int attachment_len, ret;
11878 char *attachment;
11879 struct fio_stat fst;
11880 int read_len;
11881
464b0089
GB
11882 remote_buffer_add_string (&p, &left, "vFile:fstat:");
11883
11884 remote_buffer_add_int (&p, &left, fd);
11885
11886 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
11887 remote_errno, &attachment,
11888 &attachment_len);
11889 if (ret < 0)
0a93529c 11890 {
464b0089
GB
11891 if (*remote_errno != FILEIO_ENOSYS)
11892 return ret;
11893
0a93529c
GB
11894 /* Strictly we should return -1, ENOSYS here, but when
11895 "set sysroot remote:" was implemented in August 2008
11896 BFD's need for a stat function was sidestepped with
11897 this hack. This was not remedied until March 2015
11898 so we retain the previous behavior to avoid breaking
11899 compatibility.
11900
11901 Note that the memset is a March 2015 addition; older
11902 GDBs set st_size *and nothing else* so the structure
11903 would have garbage in all other fields. This might
11904 break something but retaining the previous behavior
11905 here would be just too wrong. */
11906
11907 memset (st, 0, sizeof (struct stat));
11908 st->st_size = INT_MAX;
11909 return 0;
11910 }
11911
0a93529c
GB
11912 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11913 (gdb_byte *) &fst, sizeof (fst));
11914
11915 if (read_len != ret)
11916 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
11917
11918 if (read_len != sizeof (fst))
11919 error (_("vFile:fstat returned %d bytes, but expecting %d."),
11920 read_len, (int) sizeof (fst));
11921
11922 remote_fileio_to_host_stat (&fst, st);
11923
11924 return 0;
11925}
11926
12e2a5fd 11927/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
11928
11929static int
11930remote_filesystem_is_local (struct target_ops *self)
11931{
11932 /* Valgrind GDB presents itself as a remote target but works
11933 on the local filesystem: it does not implement remote get
11934 and users are not expected to set a sysroot. To handle
11935 this case we treat the remote filesystem as local if the
11936 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
11937 does not support vFile:open. */
a3be80c3 11938 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
11939 {
11940 enum packet_support ps = packet_support (PACKET_vFile_open);
11941
11942 if (ps == PACKET_SUPPORT_UNKNOWN)
11943 {
11944 int fd, remote_errno;
11945
11946 /* Try opening a file to probe support. The supplied
11947 filename is irrelevant, we only care about whether
11948 the stub recognizes the packet or not. */
07c138c8 11949 fd = remote_hostio_open (self, NULL, "just probing",
4313b8c0 11950 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
11951 &remote_errno);
11952
11953 if (fd >= 0)
11954 remote_hostio_close (self, fd, &remote_errno);
11955
11956 ps = packet_support (PACKET_vFile_open);
11957 }
11958
11959 if (ps == PACKET_DISABLE)
11960 {
11961 static int warning_issued = 0;
11962
11963 if (!warning_issued)
11964 {
11965 warning (_("remote target does not support file"
11966 " transfer, attempting to access files"
11967 " from local filesystem."));
11968 warning_issued = 1;
11969 }
11970
11971 return 1;
11972 }
11973 }
11974
11975 return 0;
11976}
11977
a6b151f1
DJ
11978static int
11979remote_fileio_errno_to_host (int errnum)
11980{
11981 switch (errnum)
11982 {
11983 case FILEIO_EPERM:
11984 return EPERM;
11985 case FILEIO_ENOENT:
11986 return ENOENT;
11987 case FILEIO_EINTR:
11988 return EINTR;
11989 case FILEIO_EIO:
11990 return EIO;
11991 case FILEIO_EBADF:
11992 return EBADF;
11993 case FILEIO_EACCES:
11994 return EACCES;
11995 case FILEIO_EFAULT:
11996 return EFAULT;
11997 case FILEIO_EBUSY:
11998 return EBUSY;
11999 case FILEIO_EEXIST:
12000 return EEXIST;
12001 case FILEIO_ENODEV:
12002 return ENODEV;
12003 case FILEIO_ENOTDIR:
12004 return ENOTDIR;
12005 case FILEIO_EISDIR:
12006 return EISDIR;
12007 case FILEIO_EINVAL:
12008 return EINVAL;
12009 case FILEIO_ENFILE:
12010 return ENFILE;
12011 case FILEIO_EMFILE:
12012 return EMFILE;
12013 case FILEIO_EFBIG:
12014 return EFBIG;
12015 case FILEIO_ENOSPC:
12016 return ENOSPC;
12017 case FILEIO_ESPIPE:
12018 return ESPIPE;
12019 case FILEIO_EROFS:
12020 return EROFS;
12021 case FILEIO_ENOSYS:
12022 return ENOSYS;
12023 case FILEIO_ENAMETOOLONG:
12024 return ENAMETOOLONG;
12025 }
12026 return -1;
12027}
12028
12029static char *
12030remote_hostio_error (int errnum)
12031{
12032 int host_error = remote_fileio_errno_to_host (errnum);
12033
12034 if (host_error == -1)
12035 error (_("Unknown remote I/O error %d"), errnum);
12036 else
12037 error (_("Remote I/O error: %s"), safe_strerror (host_error));
12038}
12039
a6b151f1
DJ
12040static void
12041remote_hostio_close_cleanup (void *opaque)
12042{
12043 int fd = *(int *) opaque;
12044 int remote_errno;
12045
df39ea25 12046 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
12047}
12048
12049void
12050remote_file_put (const char *local_file, const char *remote_file, int from_tty)
12051{
12052 struct cleanup *back_to, *close_cleanup;
12053 int retcode, fd, remote_errno, bytes, io_size;
a6b151f1
DJ
12054 gdb_byte *buffer;
12055 int bytes_in_buffer;
12056 int saw_eof;
12057 ULONGEST offset;
5d93a237 12058 struct remote_state *rs = get_remote_state ();
a6b151f1 12059
5d93a237 12060 if (!rs->remote_desc)
a6b151f1
DJ
12061 error (_("command can only be used with remote target"));
12062
d419f42d 12063 gdb_file_up file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
12064 if (file == NULL)
12065 perror_with_name (local_file);
a6b151f1 12066
07c138c8 12067 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 12068 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1 12069 | FILEIO_O_TRUNC),
4313b8c0 12070 0700, 0, &remote_errno);
a6b151f1
DJ
12071 if (fd == -1)
12072 remote_hostio_error (remote_errno);
12073
12074 /* Send up to this many bytes at once. They won't all fit in the
12075 remote packet limit, so we'll transfer slightly fewer. */
12076 io_size = get_remote_packet_size ();
224c3ddb 12077 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12078 back_to = make_cleanup (xfree, buffer);
a6b151f1
DJ
12079
12080 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
12081
12082 bytes_in_buffer = 0;
12083 saw_eof = 0;
12084 offset = 0;
12085 while (bytes_in_buffer || !saw_eof)
12086 {
12087 if (!saw_eof)
12088 {
3e43a32a
MS
12089 bytes = fread (buffer + bytes_in_buffer, 1,
12090 io_size - bytes_in_buffer,
d419f42d 12091 file.get ());
a6b151f1
DJ
12092 if (bytes == 0)
12093 {
d419f42d 12094 if (ferror (file.get ()))
a6b151f1
DJ
12095 error (_("Error reading %s."), local_file);
12096 else
12097 {
12098 /* EOF. Unless there is something still in the
12099 buffer from the last iteration, we are done. */
12100 saw_eof = 1;
12101 if (bytes_in_buffer == 0)
12102 break;
12103 }
12104 }
12105 }
12106 else
12107 bytes = 0;
12108
12109 bytes += bytes_in_buffer;
12110 bytes_in_buffer = 0;
12111
0d866f62
TT
12112 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
12113 fd, buffer, bytes,
3e43a32a 12114 offset, &remote_errno);
a6b151f1
DJ
12115
12116 if (retcode < 0)
12117 remote_hostio_error (remote_errno);
12118 else if (retcode == 0)
12119 error (_("Remote write of %d bytes returned 0!"), bytes);
12120 else if (retcode < bytes)
12121 {
12122 /* Short write. Save the rest of the read data for the next
12123 write. */
12124 bytes_in_buffer = bytes - retcode;
12125 memmove (buffer, buffer + retcode, bytes_in_buffer);
12126 }
12127
12128 offset += retcode;
12129 }
12130
12131 discard_cleanups (close_cleanup);
df39ea25 12132 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
12133 remote_hostio_error (remote_errno);
12134
12135 if (from_tty)
12136 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
12137 do_cleanups (back_to);
12138}
12139
12140void
12141remote_file_get (const char *remote_file, const char *local_file, int from_tty)
12142{
12143 struct cleanup *back_to, *close_cleanup;
cea39f65 12144 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
12145 gdb_byte *buffer;
12146 ULONGEST offset;
5d93a237 12147 struct remote_state *rs = get_remote_state ();
a6b151f1 12148
5d93a237 12149 if (!rs->remote_desc)
a6b151f1
DJ
12150 error (_("command can only be used with remote target"));
12151
07c138c8 12152 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
4313b8c0
GB
12153 remote_file, FILEIO_O_RDONLY, 0, 0,
12154 &remote_errno);
a6b151f1
DJ
12155 if (fd == -1)
12156 remote_hostio_error (remote_errno);
12157
d419f42d 12158 gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
12159 if (file == NULL)
12160 perror_with_name (local_file);
a6b151f1
DJ
12161
12162 /* Send up to this many bytes at once. They won't all fit in the
12163 remote packet limit, so we'll transfer slightly fewer. */
12164 io_size = get_remote_packet_size ();
224c3ddb 12165 buffer = (gdb_byte *) xmalloc (io_size);
d419f42d 12166 back_to = make_cleanup (xfree, buffer);
a6b151f1
DJ
12167
12168 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
12169
12170 offset = 0;
12171 while (1)
12172 {
a3be983c
TT
12173 bytes = remote_hostio_pread (find_target_at (process_stratum),
12174 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
12175 if (bytes == 0)
12176 /* Success, but no bytes, means end-of-file. */
12177 break;
12178 if (bytes == -1)
12179 remote_hostio_error (remote_errno);
12180
12181 offset += bytes;
12182
d419f42d 12183 bytes = fwrite (buffer, 1, bytes, file.get ());
a6b151f1
DJ
12184 if (bytes == 0)
12185 perror_with_name (local_file);
12186 }
12187
12188 discard_cleanups (close_cleanup);
df39ea25 12189 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
12190 remote_hostio_error (remote_errno);
12191
12192 if (from_tty)
12193 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
12194 do_cleanups (back_to);
12195}
12196
12197void
12198remote_file_delete (const char *remote_file, int from_tty)
12199{
12200 int retcode, remote_errno;
5d93a237 12201 struct remote_state *rs = get_remote_state ();
a6b151f1 12202
5d93a237 12203 if (!rs->remote_desc)
a6b151f1
DJ
12204 error (_("command can only be used with remote target"));
12205
dbbca37d 12206 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 12207 NULL, remote_file, &remote_errno);
a6b151f1
DJ
12208 if (retcode == -1)
12209 remote_hostio_error (remote_errno);
12210
12211 if (from_tty)
12212 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
12213}
12214
12215static void
ac88e2de 12216remote_put_command (const char *args, int from_tty)
a6b151f1 12217{
d1a41061
PP
12218 if (args == NULL)
12219 error_no_arg (_("file to put"));
12220
773a1edc 12221 gdb_argv argv (args);
a6b151f1
DJ
12222 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12223 error (_("Invalid parameters to remote put"));
12224
12225 remote_file_put (argv[0], argv[1], from_tty);
a6b151f1
DJ
12226}
12227
12228static void
ac88e2de 12229remote_get_command (const char *args, int from_tty)
a6b151f1 12230{
d1a41061
PP
12231 if (args == NULL)
12232 error_no_arg (_("file to get"));
12233
773a1edc 12234 gdb_argv argv (args);
a6b151f1
DJ
12235 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
12236 error (_("Invalid parameters to remote get"));
12237
12238 remote_file_get (argv[0], argv[1], from_tty);
a6b151f1
DJ
12239}
12240
12241static void
ac88e2de 12242remote_delete_command (const char *args, int from_tty)
a6b151f1 12243{
d1a41061
PP
12244 if (args == NULL)
12245 error_no_arg (_("file to delete"));
12246
773a1edc 12247 gdb_argv argv (args);
a6b151f1
DJ
12248 if (argv[0] == NULL || argv[1] != NULL)
12249 error (_("Invalid parameters to remote delete"));
12250
12251 remote_file_delete (argv[0], from_tty);
a6b151f1
DJ
12252}
12253
12254static void
12255remote_command (char *args, int from_tty)
12256{
635c7e8a 12257 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
12258}
12259
b2175913 12260static int
19db3e69 12261remote_can_execute_reverse (struct target_ops *self)
b2175913 12262{
4082afcc
PA
12263 if (packet_support (PACKET_bs) == PACKET_ENABLE
12264 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
12265 return 1;
12266 else
12267 return 0;
b2175913
MS
12268}
12269
74531fed 12270static int
2a9a2795 12271remote_supports_non_stop (struct target_ops *self)
74531fed
PA
12272{
12273 return 1;
12274}
12275
03583c20 12276static int
2bfc0540 12277remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
12278{
12279 /* Only supported in extended mode. */
12280 return 0;
12281}
12282
8a305172 12283static int
86ce2668 12284remote_supports_multi_process (struct target_ops *self)
8a305172
PA
12285{
12286 struct remote_state *rs = get_remote_state ();
a744cf53 12287
8020350c 12288 return remote_multi_process_p (rs);
8a305172
PA
12289}
12290
70221824 12291static int
782b2b07
SS
12292remote_supports_cond_tracepoints (void)
12293{
4082afcc 12294 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
12295}
12296
3788aec7 12297static int
efcc2da7 12298remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 12299{
4082afcc 12300 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
12301}
12302
70221824 12303static int
7a697b8d
SS
12304remote_supports_fast_tracepoints (void)
12305{
4082afcc 12306 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
12307}
12308
0fb4aa4b
PA
12309static int
12310remote_supports_static_tracepoints (void)
12311{
4082afcc 12312 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
12313}
12314
1e4d1764
YQ
12315static int
12316remote_supports_install_in_trace (void)
12317{
4082afcc 12318 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
12319}
12320
d248b706 12321static int
7d178d6a 12322remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 12323{
4082afcc
PA
12324 return (packet_support (PACKET_EnableDisableTracepoints_feature)
12325 == PACKET_ENABLE);
d248b706
KY
12326}
12327
3065dfb6 12328static int
6de37a3a 12329remote_supports_string_tracing (struct target_ops *self)
3065dfb6 12330{
4082afcc 12331 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
12332}
12333
d3ce09f5 12334static int
78eff0ec 12335remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 12336{
4082afcc 12337 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
12338}
12339
35b1e5cc 12340static void
ecae04e1 12341remote_trace_init (struct target_ops *self)
35b1e5cc 12342{
b6bb3468
PA
12343 struct remote_state *rs = get_remote_state ();
12344
35b1e5cc 12345 putpkt ("QTinit");
b6bb3468
PA
12346 remote_get_noisy_reply ();
12347 if (strcmp (rs->buf, "OK") != 0)
35b1e5cc
SS
12348 error (_("Target does not support this command."));
12349}
12350
12351static void free_actions_list (char **actions_list);
12352static void free_actions_list_cleanup_wrapper (void *);
12353static void
12354free_actions_list_cleanup_wrapper (void *al)
12355{
19ba03f4 12356 free_actions_list ((char **) al);
35b1e5cc
SS
12357}
12358
12359static void
12360free_actions_list (char **actions_list)
12361{
12362 int ndx;
12363
12364 if (actions_list == 0)
12365 return;
12366
12367 for (ndx = 0; actions_list[ndx]; ndx++)
12368 xfree (actions_list[ndx]);
12369
12370 xfree (actions_list);
12371}
12372
409873ef
SS
12373/* Recursive routine to walk through command list including loops, and
12374 download packets for each command. */
12375
12376static void
12377remote_download_command_source (int num, ULONGEST addr,
12378 struct command_line *cmds)
12379{
12380 struct remote_state *rs = get_remote_state ();
12381 struct command_line *cmd;
12382
12383 for (cmd = cmds; cmd; cmd = cmd->next)
12384 {
0df8b418 12385 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12386 strcpy (rs->buf, "QTDPsrc:");
12387 encode_source_string (num, addr, "cmd", cmd->line,
12388 rs->buf + strlen (rs->buf),
12389 rs->buf_size - strlen (rs->buf));
12390 putpkt (rs->buf);
b6bb3468
PA
12391 remote_get_noisy_reply ();
12392 if (strcmp (rs->buf, "OK"))
409873ef
SS
12393 warning (_("Target does not support source download."));
12394
12395 if (cmd->control_type == while_control
12396 || cmd->control_type == while_stepping_control)
12397 {
12398 remote_download_command_source (num, addr, *cmd->body_list);
12399
0df8b418 12400 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
12401 strcpy (rs->buf, "QTDPsrc:");
12402 encode_source_string (num, addr, "cmd", "end",
12403 rs->buf + strlen (rs->buf),
12404 rs->buf_size - strlen (rs->buf));
12405 putpkt (rs->buf);
b6bb3468
PA
12406 remote_get_noisy_reply ();
12407 if (strcmp (rs->buf, "OK"))
409873ef
SS
12408 warning (_("Target does not support source download."));
12409 }
12410 }
12411}
12412
35b1e5cc 12413static void
548f7808 12414remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 12415{
bba74b36 12416#define BUF_SIZE 2048
e8ba3115 12417
35b1e5cc 12418 CORE_ADDR tpaddr;
409873ef 12419 char addrbuf[40];
bba74b36 12420 char buf[BUF_SIZE];
35b1e5cc
SS
12421 char **tdp_actions;
12422 char **stepping_actions;
12423 int ndx;
12424 struct cleanup *old_chain = NULL;
35b1e5cc 12425 char *pkt;
e8ba3115 12426 struct breakpoint *b = loc->owner;
d9b3f62e 12427 struct tracepoint *t = (struct tracepoint *) b;
b6bb3468 12428 struct remote_state *rs = get_remote_state ();
35b1e5cc 12429
dc673c81 12430 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
12431 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
12432 tdp_actions);
12433 (void) make_cleanup (free_actions_list_cleanup_wrapper,
12434 stepping_actions);
12435
12436 tpaddr = loc->address;
12437 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
12438 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
12439 addrbuf, /* address */
12440 (b->enable_state == bp_enabled ? 'E' : 'D'),
12441 t->step_count, t->pass_count);
e8ba3115
YQ
12442 /* Fast tracepoints are mostly handled by the target, but we can
12443 tell the target how big of an instruction block should be moved
12444 around. */
12445 if (b->type == bp_fast_tracepoint)
12446 {
12447 /* Only test for support at download time; we may not know
12448 target capabilities at definition time. */
12449 if (remote_supports_fast_tracepoints ())
35b1e5cc 12450 {
6b940e6a
PL
12451 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
12452 NULL))
bba74b36 12453 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 12454 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 12455 else
e8ba3115
YQ
12456 /* If it passed validation at definition but fails now,
12457 something is very wrong. */
12458 internal_error (__FILE__, __LINE__,
12459 _("Fast tracepoint not "
12460 "valid during download"));
35b1e5cc 12461 }
e8ba3115
YQ
12462 else
12463 /* Fast tracepoints are functionally identical to regular
12464 tracepoints, so don't take lack of support as a reason to
12465 give up on the trace run. */
12466 warning (_("Target does not support fast tracepoints, "
12467 "downloading %d as regular tracepoint"), b->number);
12468 }
12469 else if (b->type == bp_static_tracepoint)
12470 {
12471 /* Only test for support at download time; we may not know
12472 target capabilities at definition time. */
12473 if (remote_supports_static_tracepoints ())
0fb4aa4b 12474 {
e8ba3115 12475 struct static_tracepoint_marker marker;
0fb4aa4b 12476
e8ba3115
YQ
12477 if (target_static_tracepoint_marker_at (tpaddr, &marker))
12478 strcat (buf, ":S");
0fb4aa4b 12479 else
e8ba3115 12480 error (_("Static tracepoint not valid during download"));
0fb4aa4b 12481 }
e8ba3115
YQ
12482 else
12483 /* Fast tracepoints are functionally identical to regular
12484 tracepoints, so don't take lack of support as a reason
12485 to give up on the trace run. */
12486 error (_("Target does not support static tracepoints"));
12487 }
12488 /* If the tracepoint has a conditional, make it into an agent
12489 expression and append to the definition. */
12490 if (loc->cond)
12491 {
12492 /* Only test support at download time, we may not know target
12493 capabilities at definition time. */
12494 if (remote_supports_cond_tracepoints ())
35b1e5cc 12495 {
833177a4 12496 agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
bba74b36
YQ
12497 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
12498 aexpr->len);
e8ba3115
YQ
12499 pkt = buf + strlen (buf);
12500 for (ndx = 0; ndx < aexpr->len; ++ndx)
12501 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
12502 *pkt = '\0';
35b1e5cc 12503 }
e8ba3115
YQ
12504 else
12505 warning (_("Target does not support conditional tracepoints, "
12506 "ignoring tp %d cond"), b->number);
12507 }
35b1e5cc 12508
d9b3f62e 12509 if (b->commands || *default_collect)
e8ba3115
YQ
12510 strcat (buf, "-");
12511 putpkt (buf);
b6bb3468
PA
12512 remote_get_noisy_reply ();
12513 if (strcmp (rs->buf, "OK"))
e8ba3115 12514 error (_("Target does not support tracepoints."));
35b1e5cc 12515
e8ba3115
YQ
12516 /* do_single_steps (t); */
12517 if (tdp_actions)
12518 {
12519 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 12520 {
e8ba3115 12521 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
12522 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
12523 b->number, addrbuf, /* address */
12524 tdp_actions[ndx],
12525 ((tdp_actions[ndx + 1] || stepping_actions)
12526 ? '-' : 0));
e8ba3115 12527 putpkt (buf);
b6bb3468
PA
12528 remote_get_noisy_reply ();
12529 if (strcmp (rs->buf, "OK"))
e8ba3115 12530 error (_("Error on target while setting tracepoints."));
35b1e5cc 12531 }
e8ba3115
YQ
12532 }
12533 if (stepping_actions)
12534 {
12535 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 12536 {
e8ba3115 12537 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
12538 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
12539 b->number, addrbuf, /* address */
12540 ((ndx == 0) ? "S" : ""),
12541 stepping_actions[ndx],
12542 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115 12543 putpkt (buf);
b6bb3468
PA
12544 remote_get_noisy_reply ();
12545 if (strcmp (rs->buf, "OK"))
e8ba3115 12546 error (_("Error on target while setting tracepoints."));
35b1e5cc 12547 }
e8ba3115 12548 }
409873ef 12549
4082afcc 12550 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 12551 {
f00aae0f 12552 if (b->location != NULL)
409873ef 12553 {
e8ba3115 12554 strcpy (buf, "QTDPsrc:");
f00aae0f 12555 encode_source_string (b->number, loc->address, "at",
d28cd78a 12556 event_location_to_string (b->location.get ()),
f00aae0f 12557 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115 12558 putpkt (buf);
b6bb3468
PA
12559 remote_get_noisy_reply ();
12560 if (strcmp (rs->buf, "OK"))
e8ba3115 12561 warning (_("Target does not support source download."));
409873ef 12562 }
e8ba3115
YQ
12563 if (b->cond_string)
12564 {
12565 strcpy (buf, "QTDPsrc:");
12566 encode_source_string (b->number, loc->address,
12567 "cond", b->cond_string, buf + strlen (buf),
12568 2048 - strlen (buf));
12569 putpkt (buf);
b6bb3468
PA
12570 remote_get_noisy_reply ();
12571 if (strcmp (rs->buf, "OK"))
e8ba3115
YQ
12572 warning (_("Target does not support source download."));
12573 }
12574 remote_download_command_source (b->number, loc->address,
12575 breakpoint_commands (b));
35b1e5cc 12576 }
e8ba3115
YQ
12577
12578 do_cleanups (old_chain);
35b1e5cc
SS
12579}
12580
1e4d1764 12581static int
a52a8357 12582remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 12583{
1e51243a
PA
12584 struct remote_state *rs = get_remote_state ();
12585 struct trace_status *ts;
12586 int status;
12587
12588 /* Don't try to install tracepoints until we've relocated our
12589 symbols, and fetched and merged the target's tracepoint list with
12590 ours. */
12591 if (rs->starting_up)
12592 return 0;
12593
12594 ts = current_trace_status ();
8bd200f1 12595 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
12596
12597 if (status == -1 || !ts->running_known || !ts->running)
12598 return 0;
12599
12600 /* If we are in a tracing experiment, but remote stub doesn't support
12601 installing tracepoint in trace, we have to return. */
12602 if (!remote_supports_install_in_trace ())
12603 return 0;
12604
12605 return 1;
12606}
12607
12608
35b1e5cc 12609static void
559d2b81
TT
12610remote_download_trace_state_variable (struct target_ops *self,
12611 struct trace_state_variable *tsv)
35b1e5cc
SS
12612{
12613 struct remote_state *rs = get_remote_state ();
00bf0b85 12614 char *p;
35b1e5cc 12615
bba74b36
YQ
12616 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
12617 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
12618 tsv->builtin);
00bf0b85
SS
12619 p = rs->buf + strlen (rs->buf);
12620 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
12621 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 12622 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 12623 *p++ = '\0';
35b1e5cc 12624 putpkt (rs->buf);
b6bb3468
PA
12625 remote_get_noisy_reply ();
12626 if (*rs->buf == '\0')
ad91cd99 12627 error (_("Target does not support this command."));
b6bb3468 12628 if (strcmp (rs->buf, "OK") != 0)
ad91cd99 12629 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
12630}
12631
d248b706 12632static void
46670d57
TT
12633remote_enable_tracepoint (struct target_ops *self,
12634 struct bp_location *location)
d248b706
KY
12635{
12636 struct remote_state *rs = get_remote_state ();
12637 char addr_buf[40];
12638
12639 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12640 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
12641 location->owner->number, addr_buf);
d248b706 12642 putpkt (rs->buf);
b6bb3468 12643 remote_get_noisy_reply ();
d248b706
KY
12644 if (*rs->buf == '\0')
12645 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
12646 if (strcmp (rs->buf, "OK") != 0)
12647 error (_("Error on target while enabling tracepoint."));
12648}
12649
12650static void
780b049c
TT
12651remote_disable_tracepoint (struct target_ops *self,
12652 struct bp_location *location)
d248b706
KY
12653{
12654 struct remote_state *rs = get_remote_state ();
12655 char addr_buf[40];
12656
12657 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12658 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
12659 location->owner->number, addr_buf);
d248b706 12660 putpkt (rs->buf);
b6bb3468 12661 remote_get_noisy_reply ();
d248b706
KY
12662 if (*rs->buf == '\0')
12663 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
12664 if (strcmp (rs->buf, "OK") != 0)
12665 error (_("Error on target while disabling tracepoint."));
12666}
12667
35b1e5cc 12668static void
583f9a86 12669remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
12670{
12671 asection *s;
81b9b86e 12672 bfd *abfd = NULL;
35b1e5cc 12673 bfd_size_type size;
608bcef2 12674 bfd_vma vma;
35b1e5cc 12675 int anysecs = 0;
c2fa21f1 12676 int offset = 0;
35b1e5cc
SS
12677
12678 if (!exec_bfd)
12679 return; /* No information to give. */
12680
b6bb3468
PA
12681 struct remote_state *rs = get_remote_state ();
12682
12683 strcpy (rs->buf, "QTro");
12684 offset = strlen (rs->buf);
35b1e5cc
SS
12685 for (s = exec_bfd->sections; s; s = s->next)
12686 {
12687 char tmp1[40], tmp2[40];
c2fa21f1 12688 int sec_length;
35b1e5cc
SS
12689
12690 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 12691 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
12692 (s->flags & SEC_READONLY) == 0)
12693 continue;
12694
12695 anysecs = 1;
81b9b86e 12696 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 12697 size = bfd_get_section_size (s);
608bcef2
HZ
12698 sprintf_vma (tmp1, vma);
12699 sprintf_vma (tmp2, vma + size);
c2fa21f1 12700 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
b6bb3468 12701 if (offset + sec_length + 1 > rs->buf_size)
c2fa21f1 12702 {
4082afcc 12703 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 12704 warning (_("\
c2fa21f1
HZ
12705Too many sections for read-only sections definition packet."));
12706 break;
12707 }
b6bb3468 12708 xsnprintf (rs->buf + offset, rs->buf_size - offset, ":%s,%s",
bba74b36 12709 tmp1, tmp2);
c2fa21f1 12710 offset += sec_length;
35b1e5cc
SS
12711 }
12712 if (anysecs)
12713 {
b6bb3468
PA
12714 putpkt (rs->buf);
12715 getpkt (&rs->buf, &rs->buf_size, 0);
35b1e5cc
SS
12716 }
12717}
12718
12719static void
e2d1aae3 12720remote_trace_start (struct target_ops *self)
35b1e5cc 12721{
b6bb3468
PA
12722 struct remote_state *rs = get_remote_state ();
12723
35b1e5cc 12724 putpkt ("QTStart");
b6bb3468
PA
12725 remote_get_noisy_reply ();
12726 if (*rs->buf == '\0')
ad91cd99 12727 error (_("Target does not support this command."));
b6bb3468
PA
12728 if (strcmp (rs->buf, "OK") != 0)
12729 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12730}
12731
12732static int
8bd200f1 12733remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 12734{
953b98d1 12735 /* Initialize it just to avoid a GCC false warning. */
f652de6f 12736 char *p = NULL;
0df8b418 12737 /* FIXME we need to get register block size some other way. */
00bf0b85 12738 extern int trace_regblock_size;
bd3eecc3 12739 enum packet_result result;
b6bb3468 12740 struct remote_state *rs = get_remote_state ();
bd3eecc3 12741
4082afcc 12742 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 12743 return -1;
a744cf53 12744
5cd63fda
PA
12745 trace_regblock_size
12746 = get_remote_arch_state (target_gdbarch ())->sizeof_g_packet;
00bf0b85 12747
049dc89b
JK
12748 putpkt ("qTStatus");
12749
492d29ea 12750 TRY
67f41397 12751 {
b6bb3468 12752 p = remote_get_noisy_reply ();
67f41397 12753 }
492d29ea 12754 CATCH (ex, RETURN_MASK_ERROR)
67f41397 12755 {
598d3636
JK
12756 if (ex.error != TARGET_CLOSE_ERROR)
12757 {
12758 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
12759 return -1;
12760 }
12761 throw_exception (ex);
67f41397 12762 }
492d29ea 12763 END_CATCH
00bf0b85 12764
bd3eecc3
PA
12765 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
12766
00bf0b85 12767 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 12768 if (result == PACKET_UNKNOWN)
00bf0b85 12769 return -1;
35b1e5cc 12770
00bf0b85 12771 /* We're working with a live target. */
f5911ea1 12772 ts->filename = NULL;
00bf0b85 12773
00bf0b85 12774 if (*p++ != 'T')
b6bb3468 12775 error (_("Bogus trace status reply from target: %s"), rs->buf);
35b1e5cc 12776
84cebc4a
YQ
12777 /* Function 'parse_trace_status' sets default value of each field of
12778 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
12779 parse_trace_status (p, ts);
12780
12781 return ts->running;
35b1e5cc
SS
12782}
12783
70221824 12784static void
db90e85c 12785remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
12786 struct uploaded_tp *utp)
12787{
12788 struct remote_state *rs = get_remote_state ();
f196051f
SS
12789 char *reply;
12790 struct bp_location *loc;
12791 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 12792 size_t size = get_remote_packet_size ();
f196051f
SS
12793
12794 if (tp)
12795 {
c1fc2657 12796 tp->hit_count = 0;
f196051f 12797 tp->traceframe_usage = 0;
c1fc2657 12798 for (loc = tp->loc; loc; loc = loc->next)
f196051f
SS
12799 {
12800 /* If the tracepoint was never downloaded, don't go asking for
12801 any status. */
12802 if (tp->number_on_target == 0)
12803 continue;
bba74b36
YQ
12804 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
12805 phex_nz (loc->address, 0));
f196051f 12806 putpkt (rs->buf);
b6bb3468 12807 reply = remote_get_noisy_reply ();
f196051f
SS
12808 if (reply && *reply)
12809 {
12810 if (*reply == 'V')
12811 parse_tracepoint_status (reply + 1, bp, utp);
12812 }
12813 }
12814 }
12815 else if (utp)
12816 {
12817 utp->hit_count = 0;
12818 utp->traceframe_usage = 0;
bba74b36
YQ
12819 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
12820 phex_nz (utp->addr, 0));
f196051f 12821 putpkt (rs->buf);
b6bb3468 12822 reply = remote_get_noisy_reply ();
f196051f
SS
12823 if (reply && *reply)
12824 {
12825 if (*reply == 'V')
12826 parse_tracepoint_status (reply + 1, bp, utp);
12827 }
12828 }
12829}
12830
35b1e5cc 12831static void
74499f1b 12832remote_trace_stop (struct target_ops *self)
35b1e5cc 12833{
b6bb3468
PA
12834 struct remote_state *rs = get_remote_state ();
12835
35b1e5cc 12836 putpkt ("QTStop");
b6bb3468
PA
12837 remote_get_noisy_reply ();
12838 if (*rs->buf == '\0')
ad91cd99 12839 error (_("Target does not support this command."));
b6bb3468
PA
12840 if (strcmp (rs->buf, "OK") != 0)
12841 error (_("Bogus reply from target: %s"), rs->buf);
35b1e5cc
SS
12842}
12843
12844static int
bd4c6793
TT
12845remote_trace_find (struct target_ops *self,
12846 enum trace_find_type type, int num,
cc5925ad 12847 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
12848 int *tpp)
12849{
12850 struct remote_state *rs = get_remote_state ();
bba74b36 12851 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
12852 char *p, *reply;
12853 int target_frameno = -1, target_tracept = -1;
12854
e6e4e701
PA
12855 /* Lookups other than by absolute frame number depend on the current
12856 trace selected, so make sure it is correct on the remote end
12857 first. */
12858 if (type != tfind_number)
12859 set_remote_traceframe ();
12860
35b1e5cc
SS
12861 p = rs->buf;
12862 strcpy (p, "QTFrame:");
12863 p = strchr (p, '\0');
12864 switch (type)
12865 {
12866 case tfind_number:
bba74b36 12867 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
12868 break;
12869 case tfind_pc:
bba74b36 12870 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
12871 break;
12872 case tfind_tp:
bba74b36 12873 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
12874 break;
12875 case tfind_range:
bba74b36
YQ
12876 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
12877 phex_nz (addr2, 0));
35b1e5cc
SS
12878 break;
12879 case tfind_outside:
bba74b36
YQ
12880 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
12881 phex_nz (addr2, 0));
35b1e5cc
SS
12882 break;
12883 default:
9b20d036 12884 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
12885 }
12886
12887 putpkt (rs->buf);
b6bb3468 12888 reply = remote_get_noisy_reply ();
ad91cd99
PA
12889 if (*reply == '\0')
12890 error (_("Target does not support this command."));
35b1e5cc
SS
12891
12892 while (reply && *reply)
12893 switch (*reply)
12894 {
12895 case 'F':
f197e0f1
VP
12896 p = ++reply;
12897 target_frameno = (int) strtol (p, &reply, 16);
12898 if (reply == p)
12899 error (_("Unable to parse trace frame number"));
e6e4e701
PA
12900 /* Don't update our remote traceframe number cache on failure
12901 to select a remote traceframe. */
f197e0f1
VP
12902 if (target_frameno == -1)
12903 return -1;
35b1e5cc
SS
12904 break;
12905 case 'T':
f197e0f1
VP
12906 p = ++reply;
12907 target_tracept = (int) strtol (p, &reply, 16);
12908 if (reply == p)
12909 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
12910 break;
12911 case 'O': /* "OK"? */
12912 if (reply[1] == 'K' && reply[2] == '\0')
12913 reply += 2;
12914 else
12915 error (_("Bogus reply from target: %s"), reply);
12916 break;
12917 default:
12918 error (_("Bogus reply from target: %s"), reply);
12919 }
12920 if (tpp)
12921 *tpp = target_tracept;
e6e4e701 12922
262e1174 12923 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
12924 return target_frameno;
12925}
12926
12927static int
4011015b
TT
12928remote_get_trace_state_variable_value (struct target_ops *self,
12929 int tsvnum, LONGEST *val)
35b1e5cc
SS
12930{
12931 struct remote_state *rs = get_remote_state ();
12932 char *reply;
12933 ULONGEST uval;
12934
e6e4e701
PA
12935 set_remote_traceframe ();
12936
bba74b36 12937 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc 12938 putpkt (rs->buf);
b6bb3468 12939 reply = remote_get_noisy_reply ();
35b1e5cc
SS
12940 if (reply && *reply)
12941 {
12942 if (*reply == 'V')
12943 {
12944 unpack_varlen_hex (reply + 1, &uval);
12945 *val = (LONGEST) uval;
12946 return 1;
12947 }
12948 }
12949 return 0;
12950}
12951
00bf0b85 12952static int
dc3decaf 12953remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
12954{
12955 struct remote_state *rs = get_remote_state ();
12956 char *p, *reply;
12957
12958 p = rs->buf;
12959 strcpy (p, "QTSave:");
12960 p += strlen (p);
12961 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
12962 error (_("Remote file name too long for trace save packet"));
9f1b45b0 12963 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
12964 *p++ = '\0';
12965 putpkt (rs->buf);
b6bb3468 12966 reply = remote_get_noisy_reply ();
d6c5869f 12967 if (*reply == '\0')
ad91cd99
PA
12968 error (_("Target does not support this command."));
12969 if (strcmp (reply, "OK") != 0)
12970 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
12971 return 0;
12972}
12973
12974/* This is basically a memory transfer, but needs to be its own packet
12975 because we don't know how the target actually organizes its trace
12976 memory, plus we want to be able to ask for as much as possible, but
12977 not be unhappy if we don't get as much as we ask for. */
12978
12979static LONGEST
88ee6f45
TT
12980remote_get_raw_trace_data (struct target_ops *self,
12981 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
12982{
12983 struct remote_state *rs = get_remote_state ();
12984 char *reply;
12985 char *p;
12986 int rslt;
12987
12988 p = rs->buf;
12989 strcpy (p, "qTBuffer:");
12990 p += strlen (p);
12991 p += hexnumstr (p, offset);
12992 *p++ = ',';
12993 p += hexnumstr (p, len);
12994 *p++ = '\0';
12995
12996 putpkt (rs->buf);
b6bb3468 12997 reply = remote_get_noisy_reply ();
00bf0b85
SS
12998 if (reply && *reply)
12999 {
13000 /* 'l' by itself means we're at the end of the buffer and
13001 there is nothing more to get. */
13002 if (*reply == 'l')
13003 return 0;
13004
13005 /* Convert the reply into binary. Limit the number of bytes to
13006 convert according to our passed-in buffer size, rather than
13007 what was returned in the packet; if the target is
13008 unexpectedly generous and gives us a bigger reply than we
13009 asked for, we don't want to crash. */
b6bb3468 13010 rslt = hex2bin (reply, buf, len);
00bf0b85
SS
13011 return rslt;
13012 }
13013
13014 /* Something went wrong, flag as an error. */
13015 return -1;
13016}
13017
35b1e5cc 13018static void
37b25738 13019remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
13020{
13021 struct remote_state *rs = get_remote_state ();
13022
4082afcc 13023 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 13024 {
ad91cd99
PA
13025 char *reply;
13026
bba74b36 13027 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 13028 putpkt (rs->buf);
b6bb3468 13029 reply = remote_get_noisy_reply ();
ad91cd99 13030 if (*reply == '\0')
33da3f1c 13031 error (_("Target does not support this command."));
ad91cd99
PA
13032 if (strcmp (reply, "OK") != 0)
13033 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
13034 }
13035 else if (val)
13036 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
13037}
13038
dc146f7c
VP
13039static int
13040remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
13041{
13042 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 13043
fe978cb0
PA
13044 if (info && info->priv)
13045 return info->priv->core;
dc146f7c
VP
13046 return -1;
13047}
13048
4daf5ac0 13049static void
736d5b1f 13050remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
13051{
13052 struct remote_state *rs = get_remote_state ();
ad91cd99 13053 char *reply;
4daf5ac0 13054
bba74b36 13055 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 13056 putpkt (rs->buf);
b6bb3468 13057 reply = remote_get_noisy_reply ();
ad91cd99 13058 if (*reply == '\0')
4daf5ac0 13059 error (_("Target does not support this command."));
ad91cd99
PA
13060 if (strcmp (reply, "OK") != 0)
13061 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
13062}
13063
b3b9301e 13064static struct traceframe_info *
a893e81f 13065remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
13066{
13067 char *text;
13068
13069 text = target_read_stralloc (&current_target,
13070 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
13071 if (text != NULL)
13072 {
13073 struct traceframe_info *info;
13074 struct cleanup *back_to = make_cleanup (xfree, text);
13075
13076 info = parse_traceframe_info (text);
13077 do_cleanups (back_to);
13078 return info;
13079 }
13080
13081 return NULL;
13082}
13083
405f8e94
SS
13084/* Handle the qTMinFTPILen packet. Returns the minimum length of
13085 instruction on which a fast tracepoint may be placed. Returns -1
13086 if the packet is not supported, and 0 if the minimum instruction
13087 length is unknown. */
13088
13089static int
0e67620a 13090remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
13091{
13092 struct remote_state *rs = get_remote_state ();
13093 char *reply;
13094
e886a173
PA
13095 /* If we're not debugging a process yet, the IPA can't be
13096 loaded. */
13097 if (!target_has_execution)
13098 return 0;
13099
13100 /* Make sure the remote is pointing at the right process. */
13101 set_general_process ();
13102
bba74b36 13103 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94 13104 putpkt (rs->buf);
b6bb3468 13105 reply = remote_get_noisy_reply ();
405f8e94
SS
13106 if (*reply == '\0')
13107 return -1;
13108 else
13109 {
13110 ULONGEST min_insn_len;
13111
13112 unpack_varlen_hex (reply, &min_insn_len);
13113
13114 return (int) min_insn_len;
13115 }
13116}
13117
f6f899bf 13118static void
4da384be 13119remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 13120{
4082afcc 13121 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
13122 {
13123 struct remote_state *rs = get_remote_state ();
13124 char *buf = rs->buf;
13125 char *endbuf = rs->buf + get_remote_packet_size ();
13126 enum packet_result result;
13127
13128 gdb_assert (val >= 0 || val == -1);
13129 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
13130 /* Send -1 as literal "-1" to avoid host size dependency. */
13131 if (val < 0)
13132 {
13133 *buf++ = '-';
13134 buf += hexnumstr (buf, (ULONGEST) -val);
13135 }
13136 else
13137 buf += hexnumstr (buf, (ULONGEST) val);
13138
13139 putpkt (rs->buf);
b6bb3468 13140 remote_get_noisy_reply ();
f6f899bf
HAQ
13141 result = packet_ok (rs->buf,
13142 &remote_protocol_packets[PACKET_QTBuffer_size]);
13143
13144 if (result != PACKET_OK)
13145 warning (_("Bogus reply from target: %s"), rs->buf);
13146 }
13147}
13148
f196051f 13149static int
d9e68a2c
TT
13150remote_set_trace_notes (struct target_ops *self,
13151 const char *user, const char *notes,
ca623f82 13152 const char *stop_notes)
f196051f
SS
13153{
13154 struct remote_state *rs = get_remote_state ();
13155 char *reply;
13156 char *buf = rs->buf;
13157 char *endbuf = rs->buf + get_remote_packet_size ();
13158 int nbytes;
13159
13160 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
13161 if (user)
13162 {
13163 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 13164 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
13165 buf += 2 * nbytes;
13166 *buf++ = ';';
13167 }
13168 if (notes)
13169 {
13170 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 13171 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
13172 buf += 2 * nbytes;
13173 *buf++ = ';';
13174 }
13175 if (stop_notes)
13176 {
13177 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 13178 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
13179 buf += 2 * nbytes;
13180 *buf++ = ';';
13181 }
13182 /* Ensure the buffer is terminated. */
13183 *buf = '\0';
13184
13185 putpkt (rs->buf);
b6bb3468 13186 reply = remote_get_noisy_reply ();
f196051f
SS
13187 if (*reply == '\0')
13188 return 0;
13189
13190 if (strcmp (reply, "OK") != 0)
13191 error (_("Bogus reply from target: %s"), reply);
13192
13193 return 1;
13194}
13195
d1feda86 13196static int
2c152180 13197remote_use_agent (struct target_ops *self, int use)
d1feda86 13198{
4082afcc 13199 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
13200 {
13201 struct remote_state *rs = get_remote_state ();
13202
13203 /* If the stub supports QAgent. */
bba74b36 13204 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
13205 putpkt (rs->buf);
13206 getpkt (&rs->buf, &rs->buf_size, 0);
13207
13208 if (strcmp (rs->buf, "OK") == 0)
13209 {
13210 use_agent = use;
13211 return 1;
13212 }
13213 }
13214
13215 return 0;
13216}
13217
13218static int
fe38f897 13219remote_can_use_agent (struct target_ops *self)
d1feda86 13220{
4082afcc 13221 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
13222}
13223
9accd112
MM
13224struct btrace_target_info
13225{
13226 /* The ptid of the traced thread. */
13227 ptid_t ptid;
f4abbc16
MM
13228
13229 /* The obtained branch trace configuration. */
13230 struct btrace_config conf;
9accd112
MM
13231};
13232
f4abbc16
MM
13233/* Reset our idea of our target's btrace configuration. */
13234
13235static void
13236remote_btrace_reset (void)
13237{
13238 struct remote_state *rs = get_remote_state ();
13239
13240 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
13241}
13242
9accd112
MM
13243/* Check whether the target supports branch tracing. */
13244
13245static int
043c3577 13246remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 13247{
4082afcc 13248 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 13249 return 0;
4082afcc 13250 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
13251 return 0;
13252
043c3577
MM
13253 switch (format)
13254 {
13255 case BTRACE_FORMAT_NONE:
13256 return 0;
13257
13258 case BTRACE_FORMAT_BTS:
13259 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
13260
13261 case BTRACE_FORMAT_PT:
13262 /* The trace is decoded on the host. Even if our target supports it,
13263 we still need to have libipt to decode the trace. */
13264#if defined (HAVE_LIBIPT)
13265 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
13266#else /* !defined (HAVE_LIBIPT) */
13267 return 0;
13268#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
13269 }
13270
13271 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
13272}
13273
f4abbc16
MM
13274/* Synchronize the configuration with the target. */
13275
13276static void
13277btrace_sync_conf (const struct btrace_config *conf)
13278{
d33501a5
MM
13279 struct packet_config *packet;
13280 struct remote_state *rs;
13281 char *buf, *pos, *endbuf;
13282
13283 rs = get_remote_state ();
13284 buf = rs->buf;
13285 endbuf = buf + get_remote_packet_size ();
13286
13287 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
13288 if (packet_config_support (packet) == PACKET_ENABLE
13289 && conf->bts.size != rs->btrace_config.bts.size)
13290 {
13291 pos = buf;
13292 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13293 conf->bts.size);
13294
13295 putpkt (buf);
13296 getpkt (&buf, &rs->buf_size, 0);
13297
13298 if (packet_ok (buf, packet) == PACKET_ERROR)
13299 {
13300 if (buf[0] == 'E' && buf[1] == '.')
13301 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
13302 else
13303 error (_("Failed to configure the BTS buffer size."));
13304 }
13305
13306 rs->btrace_config.bts.size = conf->bts.size;
13307 }
b20a6524
MM
13308
13309 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
13310 if (packet_config_support (packet) == PACKET_ENABLE
13311 && conf->pt.size != rs->btrace_config.pt.size)
13312 {
13313 pos = buf;
13314 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
13315 conf->pt.size);
13316
13317 putpkt (buf);
13318 getpkt (&buf, &rs->buf_size, 0);
13319
13320 if (packet_ok (buf, packet) == PACKET_ERROR)
13321 {
13322 if (buf[0] == 'E' && buf[1] == '.')
13323 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
13324 else
13325 error (_("Failed to configure the trace buffer size."));
13326 }
13327
13328 rs->btrace_config.pt.size = conf->pt.size;
13329 }
f4abbc16
MM
13330}
13331
13332/* Read the current thread's btrace configuration from the target and
13333 store it into CONF. */
13334
13335static void
13336btrace_read_config (struct btrace_config *conf)
13337{
13338 char *xml;
13339
13340 xml = target_read_stralloc (&current_target,
b20a6524 13341 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
13342 if (xml != NULL)
13343 {
13344 struct cleanup *cleanup;
13345
13346 cleanup = make_cleanup (xfree, xml);
13347 parse_xml_btrace_conf (conf, xml);
13348 do_cleanups (cleanup);
13349 }
13350}
13351
c0272db5
TW
13352/* Maybe reopen target btrace. */
13353
13354static void
13355remote_btrace_maybe_reopen (void)
13356{
13357 struct remote_state *rs = get_remote_state ();
c0272db5
TW
13358 struct thread_info *tp;
13359 int btrace_target_pushed = 0;
13360 int warned = 0;
13361
5ed8105e
PA
13362 scoped_restore_current_thread restore_thread;
13363
c0272db5
TW
13364 ALL_NON_EXITED_THREADS (tp)
13365 {
13366 set_general_thread (tp->ptid);
13367
13368 memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
13369 btrace_read_config (&rs->btrace_config);
13370
13371 if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
13372 continue;
13373
13374#if !defined (HAVE_LIBIPT)
13375 if (rs->btrace_config.format == BTRACE_FORMAT_PT)
13376 {
13377 if (!warned)
13378 {
13379 warned = 1;
13380 warning (_("GDB does not support Intel Processor Trace. "
13381 "\"record\" will not work in this session."));
13382 }
13383
13384 continue;
13385 }
13386#endif /* !defined (HAVE_LIBIPT) */
13387
13388 /* Push target, once, but before anything else happens. This way our
13389 changes to the threads will be cleaned up by unpushing the target
13390 in case btrace_read_config () throws. */
13391 if (!btrace_target_pushed)
13392 {
13393 btrace_target_pushed = 1;
13394 record_btrace_push_target ();
13395 printf_filtered (_("Target is recording using %s.\n"),
13396 btrace_format_string (rs->btrace_config.format));
13397 }
13398
13399 tp->btrace.target = XCNEW (struct btrace_target_info);
13400 tp->btrace.target->ptid = tp->ptid;
13401 tp->btrace.target->conf = rs->btrace_config;
13402 }
c0272db5
TW
13403}
13404
9accd112
MM
13405/* Enable branch tracing. */
13406
13407static struct btrace_target_info *
f4abbc16
MM
13408remote_enable_btrace (struct target_ops *self, ptid_t ptid,
13409 const struct btrace_config *conf)
9accd112
MM
13410{
13411 struct btrace_target_info *tinfo = NULL;
b20a6524 13412 struct packet_config *packet = NULL;
9accd112
MM
13413 struct remote_state *rs = get_remote_state ();
13414 char *buf = rs->buf;
13415 char *endbuf = rs->buf + get_remote_packet_size ();
13416
b20a6524
MM
13417 switch (conf->format)
13418 {
13419 case BTRACE_FORMAT_BTS:
13420 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
13421 break;
13422
13423 case BTRACE_FORMAT_PT:
13424 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
13425 break;
13426 }
13427
13428 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13429 error (_("Target does not support branch tracing."));
13430
f4abbc16
MM
13431 btrace_sync_conf (conf);
13432
9accd112
MM
13433 set_general_thread (ptid);
13434
13435 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13436 putpkt (rs->buf);
13437 getpkt (&rs->buf, &rs->buf_size, 0);
13438
13439 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13440 {
13441 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13442 error (_("Could not enable branch tracing for %s: %s"),
13443 target_pid_to_str (ptid), rs->buf + 2);
13444 else
13445 error (_("Could not enable branch tracing for %s."),
13446 target_pid_to_str (ptid));
13447 }
13448
8d749320 13449 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
13450 tinfo->ptid = ptid;
13451
f4abbc16
MM
13452 /* If we fail to read the configuration, we lose some information, but the
13453 tracing itself is not impacted. */
492d29ea
PA
13454 TRY
13455 {
13456 btrace_read_config (&tinfo->conf);
13457 }
13458 CATCH (err, RETURN_MASK_ERROR)
13459 {
13460 if (err.message != NULL)
13461 warning ("%s", err.message);
13462 }
13463 END_CATCH
f4abbc16 13464
9accd112
MM
13465 return tinfo;
13466}
13467
13468/* Disable branch tracing. */
13469
13470static void
25e95349
TT
13471remote_disable_btrace (struct target_ops *self,
13472 struct btrace_target_info *tinfo)
9accd112
MM
13473{
13474 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
13475 struct remote_state *rs = get_remote_state ();
13476 char *buf = rs->buf;
13477 char *endbuf = rs->buf + get_remote_packet_size ();
13478
4082afcc 13479 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13480 error (_("Target does not support branch tracing."));
13481
13482 set_general_thread (tinfo->ptid);
13483
13484 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
13485 putpkt (rs->buf);
13486 getpkt (&rs->buf, &rs->buf_size, 0);
13487
13488 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
13489 {
13490 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
13491 error (_("Could not disable branch tracing for %s: %s"),
13492 target_pid_to_str (tinfo->ptid), rs->buf + 2);
13493 else
13494 error (_("Could not disable branch tracing for %s."),
13495 target_pid_to_str (tinfo->ptid));
13496 }
13497
13498 xfree (tinfo);
13499}
13500
13501/* Teardown branch tracing. */
13502
13503static void
1777056d
TT
13504remote_teardown_btrace (struct target_ops *self,
13505 struct btrace_target_info *tinfo)
9accd112
MM
13506{
13507 /* We must not talk to the target during teardown. */
13508 xfree (tinfo);
13509}
13510
13511/* Read the branch trace. */
13512
969c39fb 13513static enum btrace_error
39c49f83 13514remote_read_btrace (struct target_ops *self,
734b0e4b 13515 struct btrace_data *btrace,
969c39fb 13516 struct btrace_target_info *tinfo,
9accd112
MM
13517 enum btrace_read_type type)
13518{
13519 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
969c39fb 13520 struct cleanup *cleanup;
9accd112
MM
13521 const char *annex;
13522 char *xml;
13523
4082afcc 13524 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
13525 error (_("Target does not support branch tracing."));
13526
13527#if !defined(HAVE_LIBEXPAT)
13528 error (_("Cannot process branch tracing result. XML parsing not supported."));
13529#endif
13530
13531 switch (type)
13532 {
864089d2 13533 case BTRACE_READ_ALL:
9accd112
MM
13534 annex = "all";
13535 break;
864089d2 13536 case BTRACE_READ_NEW:
9accd112
MM
13537 annex = "new";
13538 break;
969c39fb
MM
13539 case BTRACE_READ_DELTA:
13540 annex = "delta";
13541 break;
9accd112
MM
13542 default:
13543 internal_error (__FILE__, __LINE__,
13544 _("Bad branch tracing read type: %u."),
13545 (unsigned int) type);
13546 }
13547
13548 xml = target_read_stralloc (&current_target,
b20a6524 13549 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
13550 if (xml == NULL)
13551 return BTRACE_ERR_UNKNOWN;
9accd112 13552
969c39fb 13553 cleanup = make_cleanup (xfree, xml);
734b0e4b 13554 parse_xml_btrace (btrace, xml);
969c39fb 13555 do_cleanups (cleanup);
9accd112 13556
969c39fb 13557 return BTRACE_ERR_NONE;
9accd112
MM
13558}
13559
f4abbc16
MM
13560static const struct btrace_config *
13561remote_btrace_conf (struct target_ops *self,
13562 const struct btrace_target_info *tinfo)
13563{
13564 return &tinfo->conf;
13565}
13566
ced63ec0 13567static int
5436ff03 13568remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 13569{
4082afcc
PA
13570 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
13571 == PACKET_ENABLE);
ced63ec0
GB
13572}
13573
9dd130a0
TT
13574/* Implementation of to_load. */
13575
13576static void
9cbe5fff 13577remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
13578{
13579 generic_load (name, from_tty);
13580}
13581
c78fa86a
GB
13582/* Accepts an integer PID; returns a string representing a file that
13583 can be opened on the remote side to get the symbols for the child
13584 process. Returns NULL if the operation is not supported. */
13585
13586static char *
13587remote_pid_to_exec_file (struct target_ops *self, int pid)
13588{
13589 static char *filename = NULL;
835205d0
GB
13590 struct inferior *inf;
13591 char *annex = NULL;
c78fa86a
GB
13592
13593 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
13594 return NULL;
13595
13596 if (filename != NULL)
13597 xfree (filename);
13598
835205d0
GB
13599 inf = find_inferior_pid (pid);
13600 if (inf == NULL)
13601 internal_error (__FILE__, __LINE__,
13602 _("not currently attached to process %d"), pid);
13603
13604 if (!inf->fake_pid_p)
13605 {
13606 const int annex_size = 9;
13607
224c3ddb 13608 annex = (char *) alloca (annex_size);
835205d0
GB
13609 xsnprintf (annex, annex_size, "%x", pid);
13610 }
13611
c78fa86a
GB
13612 filename = target_read_stralloc (&current_target,
13613 TARGET_OBJECT_EXEC_FILE, annex);
13614
13615 return filename;
13616}
13617
750ce8d1
YQ
13618/* Implement the to_can_do_single_step target_ops method. */
13619
13620static int
13621remote_can_do_single_step (struct target_ops *ops)
13622{
13623 /* We can only tell whether target supports single step or not by
13624 supported s and S vCont actions if the stub supports vContSupported
13625 feature. If the stub doesn't support vContSupported feature,
13626 we have conservatively to think target doesn't supports single
13627 step. */
13628 if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
13629 {
13630 struct remote_state *rs = get_remote_state ();
13631
13632 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
13633 remote_vcont_probe (rs);
13634
13635 return rs->supports_vCont.s && rs->supports_vCont.S;
13636 }
13637 else
13638 return 0;
13639}
13640
3a00c802
PA
13641/* Implementation of the to_execution_direction method for the remote
13642 target. */
13643
13644static enum exec_direction_kind
13645remote_execution_direction (struct target_ops *self)
13646{
13647 struct remote_state *rs = get_remote_state ();
13648
13649 return rs->last_resume_exec_dir;
13650}
13651
f6327dcb
KB
13652/* Return pointer to the thread_info struct which corresponds to
13653 THREAD_HANDLE (having length HANDLE_LEN). */
13654
13655static struct thread_info *
13656remote_thread_handle_to_thread_info (struct target_ops *ops,
13657 const gdb_byte *thread_handle,
13658 int handle_len,
13659 struct inferior *inf)
13660{
13661 struct thread_info *tp;
13662
13663 ALL_NON_EXITED_THREADS (tp)
13664 {
13665 struct private_thread_info *priv = get_private_info_thread (tp);
13666
13667 if (tp->inf == inf && priv != NULL)
13668 {
13669 if (handle_len != priv->thread_handle->size ())
13670 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
13671 handle_len, priv->thread_handle->size ());
13672 if (memcmp (thread_handle, priv->thread_handle->data (),
13673 handle_len) == 0)
13674 return tp;
13675 }
13676 }
13677
13678 return NULL;
13679}
13680
c906108c 13681static void
fba45db2 13682init_remote_ops (void)
c906108c 13683{
c5aa993b 13684 remote_ops.to_shortname = "remote";
c906108c 13685 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 13686 remote_ops.to_doc =
c906108c 13687 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
13688Specify the serial device it is connected to\n\
13689(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
13690 remote_ops.to_open = remote_open;
13691 remote_ops.to_close = remote_close;
c906108c 13692 remote_ops.to_detach = remote_detach;
6ad8ae5c 13693 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 13694 remote_ops.to_resume = remote_resume;
85ad3aaf 13695 remote_ops.to_commit_resume = remote_commit_resume;
c906108c
SS
13696 remote_ops.to_wait = remote_wait;
13697 remote_ops.to_fetch_registers = remote_fetch_registers;
13698 remote_ops.to_store_registers = remote_store_registers;
13699 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 13700 remote_ops.to_files_info = remote_files_info;
c906108c
SS
13701 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
13702 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
13703 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
13704 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
13705 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
13706 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
13707 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
13708 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
13709 remote_ops.to_watchpoint_addr_within_range =
13710 remote_watchpoint_addr_within_range;
3c3bea1c
GS
13711 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
13712 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
13713 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
13714 remote_ops.to_region_ok_for_hw_watchpoint
13715 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
13716 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
13717 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 13718 remote_ops.to_kill = remote_kill;
9dd130a0 13719 remote_ops.to_load = remote_load;
c906108c 13720 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 13721 remote_ops.to_pass_signals = remote_pass_signals;
82075af2 13722 remote_ops.to_set_syscall_catchpoint = remote_set_syscall_catchpoint;
9b224c5e 13723 remote_ops.to_program_signals = remote_program_signals;
c906108c 13724 remote_ops.to_thread_alive = remote_thread_alive;
79efa585 13725 remote_ops.to_thread_name = remote_thread_name;
e8032dde 13726 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 13727 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 13728 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 13729 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 13730 remote_ops.to_stop = remote_stop;
bfedc46a 13731 remote_ops.to_interrupt = remote_interrupt;
93692b58 13732 remote_ops.to_pass_ctrlc = remote_pass_ctrlc;
4b8a223f 13733 remote_ops.to_xfer_partial = remote_xfer_partial;
09c98b44 13734 remote_ops.to_get_memory_xfer_limit = remote_get_memory_xfer_limit;
96baa820 13735 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 13736 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 13737 remote_ops.to_log_command = serial_log_command;
38691318 13738 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 13739 remote_ops.to_stratum = process_stratum;
c35b1492
PA
13740 remote_ops.to_has_all_memory = default_child_has_all_memory;
13741 remote_ops.to_has_memory = default_child_has_memory;
13742 remote_ops.to_has_stack = default_child_has_stack;
13743 remote_ops.to_has_registers = default_child_has_registers;
13744 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 13745 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 13746 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 13747 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 13748 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
13749 remote_ops.to_flash_erase = remote_flash_erase;
13750 remote_ops.to_flash_done = remote_flash_done;
29709017 13751 remote_ops.to_read_description = remote_read_description;
08388c79 13752 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
13753 remote_ops.to_can_async_p = remote_can_async_p;
13754 remote_ops.to_is_async_p = remote_is_async_p;
13755 remote_ops.to_async = remote_async;
65706a29 13756 remote_ops.to_thread_events = remote_thread_events;
750ce8d1 13757 remote_ops.to_can_do_single_step = remote_can_do_single_step;
75c99385
PA
13758 remote_ops.to_terminal_inferior = remote_terminal_inferior;
13759 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 13760 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 13761 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
13762 remote_ops.to_supports_disable_randomization
13763 = remote_supports_disable_randomization;
4bd7dc42 13764 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
13765 remote_ops.to_fileio_open = remote_hostio_open;
13766 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
13767 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 13768 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
13769 remote_ops.to_fileio_close = remote_hostio_close;
13770 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 13771 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 13772 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 13773 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 13774 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 13775 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
13776 remote_ops.to_trace_init = remote_trace_init;
13777 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 13778 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
13779 remote_ops.to_download_trace_state_variable
13780 = remote_download_trace_state_variable;
d248b706
KY
13781 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
13782 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
13783 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
13784 remote_ops.to_trace_start = remote_trace_start;
13785 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 13786 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
13787 remote_ops.to_trace_stop = remote_trace_stop;
13788 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
13789 remote_ops.to_get_trace_state_variable_value
13790 = remote_get_trace_state_variable_value;
00bf0b85
SS
13791 remote_ops.to_save_trace_data = remote_save_trace_data;
13792 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
13793 remote_ops.to_upload_trace_state_variables
13794 = remote_upload_trace_state_variables;
00bf0b85 13795 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 13796 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 13797 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 13798 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 13799 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 13800 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 13801 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 13802 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 13803 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 13804 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
13805 remote_ops.to_static_tracepoint_marker_at
13806 = remote_static_tracepoint_marker_at;
13807 remote_ops.to_static_tracepoint_markers_by_strid
13808 = remote_static_tracepoint_markers_by_strid;
b3b9301e 13809 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
13810 remote_ops.to_use_agent = remote_use_agent;
13811 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
13812 remote_ops.to_supports_btrace = remote_supports_btrace;
13813 remote_ops.to_enable_btrace = remote_enable_btrace;
13814 remote_ops.to_disable_btrace = remote_disable_btrace;
13815 remote_ops.to_teardown_btrace = remote_teardown_btrace;
13816 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 13817 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
13818 remote_ops.to_augmented_libraries_svr4_read =
13819 remote_augmented_libraries_svr4_read;
8020350c
DB
13820 remote_ops.to_follow_fork = remote_follow_fork;
13821 remote_ops.to_follow_exec = remote_follow_exec;
13822 remote_ops.to_insert_fork_catchpoint = remote_insert_fork_catchpoint;
13823 remote_ops.to_remove_fork_catchpoint = remote_remove_fork_catchpoint;
13824 remote_ops.to_insert_vfork_catchpoint = remote_insert_vfork_catchpoint;
13825 remote_ops.to_remove_vfork_catchpoint = remote_remove_vfork_catchpoint;
13826 remote_ops.to_insert_exec_catchpoint = remote_insert_exec_catchpoint;
13827 remote_ops.to_remove_exec_catchpoint = remote_remove_exec_catchpoint;
3a00c802 13828 remote_ops.to_execution_direction = remote_execution_direction;
f6327dcb
KB
13829 remote_ops.to_thread_handle_to_thread_info =
13830 remote_thread_handle_to_thread_info;
c906108c
SS
13831}
13832
13833/* Set up the extended remote vector by making a copy of the standard
13834 remote vector and adding to it. */
13835
13836static void
fba45db2 13837init_extended_remote_ops (void)
c906108c
SS
13838{
13839 extended_remote_ops = remote_ops;
13840
0f71a2f6 13841 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 13842 extended_remote_ops.to_longname =
c906108c 13843 "Extended remote serial target in gdb-specific protocol";
c5aa993b 13844 extended_remote_ops.to_doc =
c906108c 13845 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
13846Specify the serial device it is connected to (e.g. /dev/ttya).";
13847 extended_remote_ops.to_open = extended_remote_open;
c906108c 13848 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
2d717e4f
DJ
13849 extended_remote_ops.to_detach = extended_remote_detach;
13850 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 13851 extended_remote_ops.to_post_attach = extended_remote_post_attach;
03583c20
UW
13852 extended_remote_ops.to_supports_disable_randomization
13853 = extended_remote_supports_disable_randomization;
0f71a2f6
JM
13854}
13855
6426a772 13856static int
6a109b6b 13857remote_can_async_p (struct target_ops *ops)
6426a772 13858{
5d93a237
TT
13859 struct remote_state *rs = get_remote_state ();
13860
3015c064
SM
13861 /* We don't go async if the user has explicitly prevented it with the
13862 "maint set target-async" command. */
c6ebd6cf 13863 if (!target_async_permitted)
75c99385
PA
13864 return 0;
13865
23860348 13866 /* We're async whenever the serial device is. */
5d93a237 13867 return serial_can_async_p (rs->remote_desc);
6426a772
JM
13868}
13869
13870static int
6a109b6b 13871remote_is_async_p (struct target_ops *ops)
6426a772 13872{
5d93a237
TT
13873 struct remote_state *rs = get_remote_state ();
13874
c6ebd6cf 13875 if (!target_async_permitted)
75c99385
PA
13876 /* We only enable async when the user specifically asks for it. */
13877 return 0;
13878
23860348 13879 /* We're async whenever the serial device is. */
5d93a237 13880 return serial_is_async_p (rs->remote_desc);
6426a772
JM
13881}
13882
2acceee2
JM
13883/* Pass the SERIAL event on and up to the client. One day this code
13884 will be able to delay notifying the client of an event until the
23860348 13885 point where an entire packet has been received. */
2acceee2 13886
2acceee2
JM
13887static serial_event_ftype remote_async_serial_handler;
13888
6426a772 13889static void
819cc324 13890remote_async_serial_handler (struct serial *scb, void *context)
6426a772 13891{
2acceee2
JM
13892 /* Don't propogate error information up to the client. Instead let
13893 the client find out about the error by querying the target. */
6a3753b3 13894 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
13895}
13896
74531fed
PA
13897static void
13898remote_async_inferior_event_handler (gdb_client_data data)
13899{
13900 inferior_event_handler (INF_REG_EVENT, NULL);
13901}
13902
2acceee2 13903static void
6a3753b3 13904remote_async (struct target_ops *ops, int enable)
2acceee2 13905{
5d93a237
TT
13906 struct remote_state *rs = get_remote_state ();
13907
6a3753b3 13908 if (enable)
2acceee2 13909 {
88b496c3 13910 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
13911
13912 /* If there are pending events in the stop reply queue tell the
13913 event loop to process them. */
13914 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
13915 mark_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13916 /* For simplicity, below we clear the pending events token
13917 without remembering whether it is marked, so here we always
13918 mark it. If there's actually no pending notification to
13919 process, this ends up being a no-op (other than a spurious
13920 event-loop wakeup). */
13921 if (target_is_non_stop_p ())
13922 mark_async_event_handler (rs->notif_state->get_pending_events_token);
2acceee2
JM
13923 }
13924 else
b7d2e916
PA
13925 {
13926 serial_async (rs->remote_desc, NULL, NULL);
6efcd9a8
PA
13927 /* If the core is disabling async, it doesn't want to be
13928 disturbed with target events. Clear all async event sources
13929 too. */
b7d2e916 13930 clear_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13931 if (target_is_non_stop_p ())
13932 clear_async_event_handler (rs->notif_state->get_pending_events_token);
b7d2e916 13933 }
6426a772
JM
13934}
13935
65706a29
PA
13936/* Implementation of the to_thread_events method. */
13937
13938static void
13939remote_thread_events (struct target_ops *ops, int enable)
13940{
13941 struct remote_state *rs = get_remote_state ();
13942 size_t size = get_remote_packet_size ();
65706a29
PA
13943
13944 if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
13945 return;
13946
13947 xsnprintf (rs->buf, size, "QThreadEvents:%x", enable ? 1 : 0);
13948 putpkt (rs->buf);
13949 getpkt (&rs->buf, &rs->buf_size, 0);
13950
13951 switch (packet_ok (rs->buf,
13952 &remote_protocol_packets[PACKET_QThreadEvents]))
13953 {
13954 case PACKET_OK:
13955 if (strcmp (rs->buf, "OK") != 0)
13956 error (_("Remote refused setting thread events: %s"), rs->buf);
13957 break;
13958 case PACKET_ERROR:
13959 warning (_("Remote failure reply: %s"), rs->buf);
13960 break;
13961 case PACKET_UNKNOWN:
13962 break;
13963 }
13964}
13965
5a2468f5 13966static void
c2d11a7d 13967set_remote_cmd (char *args, int from_tty)
5a2468f5 13968{
635c7e8a 13969 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
13970}
13971
d471ea57
AC
13972static void
13973show_remote_cmd (char *args, int from_tty)
13974{
37a105a1 13975 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 13976 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1 13977 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 13978 struct ui_out *uiout = current_uiout;
37a105a1 13979
2e783024 13980 ui_out_emit_tuple tuple_emitter (uiout, "showlist");
37a105a1
DJ
13981 for (; list != NULL; list = list->next)
13982 if (strcmp (list->name, "Z-packet") == 0)
13983 continue;
427c3a89
DJ
13984 else if (list->type == not_set_cmd)
13985 /* Alias commands are exactly like the original, except they
13986 don't have the normal type. */
13987 continue;
13988 else
37a105a1 13989 {
2e783024 13990 ui_out_emit_tuple option_emitter (uiout, "option");
a744cf53 13991
112e8700
SM
13992 uiout->field_string ("name", list->name);
13993 uiout->text (": ");
427c3a89 13994 if (list->type == show_cmd)
f5c4fcd9 13995 do_show_command (NULL, from_tty, list);
427c3a89
DJ
13996 else
13997 cmd_func (list, NULL, from_tty);
37a105a1 13998 }
d471ea57 13999}
5a2468f5 14000
0f71a2f6 14001
23860348 14002/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
14003static void
14004remote_new_objfile (struct objfile *objfile)
14005{
5d93a237
TT
14006 struct remote_state *rs = get_remote_state ();
14007
14008 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 14009 remote_check_symbols ();
dc8acb97
MS
14010}
14011
00bf0b85
SS
14012/* Pull all the tracepoints defined on the target and create local
14013 data structures representing them. We don't want to create real
14014 tracepoints yet, we don't want to mess up the user's existing
14015 collection. */
14016
14017static int
ab6617cc 14018remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 14019{
00bf0b85
SS
14020 struct remote_state *rs = get_remote_state ();
14021 char *p;
d5551862 14022
00bf0b85
SS
14023 /* Ask for a first packet of tracepoint definition. */
14024 putpkt ("qTfP");
14025 getpkt (&rs->buf, &rs->buf_size, 0);
14026 p = rs->buf;
14027 while (*p && *p != 'l')
d5551862 14028 {
00bf0b85
SS
14029 parse_tracepoint_definition (p, utpp);
14030 /* Ask for another packet of tracepoint definition. */
14031 putpkt ("qTsP");
14032 getpkt (&rs->buf, &rs->buf_size, 0);
14033 p = rs->buf;
d5551862 14034 }
00bf0b85 14035 return 0;
d5551862
SS
14036}
14037
00bf0b85 14038static int
181e3713
TT
14039remote_upload_trace_state_variables (struct target_ops *self,
14040 struct uploaded_tsv **utsvp)
d5551862 14041{
00bf0b85 14042 struct remote_state *rs = get_remote_state ();
d5551862 14043 char *p;
d5551862 14044
00bf0b85
SS
14045 /* Ask for a first packet of variable definition. */
14046 putpkt ("qTfV");
d5551862
SS
14047 getpkt (&rs->buf, &rs->buf_size, 0);
14048 p = rs->buf;
00bf0b85 14049 while (*p && *p != 'l')
d5551862 14050 {
00bf0b85
SS
14051 parse_tsv_definition (p, utsvp);
14052 /* Ask for another packet of variable definition. */
14053 putpkt ("qTsV");
d5551862
SS
14054 getpkt (&rs->buf, &rs->buf_size, 0);
14055 p = rs->buf;
14056 }
00bf0b85 14057 return 0;
d5551862
SS
14058}
14059
c1e36e3e
PA
14060/* The "set/show range-stepping" show hook. */
14061
14062static void
14063show_range_stepping (struct ui_file *file, int from_tty,
14064 struct cmd_list_element *c,
14065 const char *value)
14066{
14067 fprintf_filtered (file,
14068 _("Debugger's willingness to use range stepping "
14069 "is %s.\n"), value);
14070}
14071
14072/* The "set/show range-stepping" set hook. */
14073
14074static void
14075set_range_stepping (char *ignore_args, int from_tty,
14076 struct cmd_list_element *c)
14077{
5d93a237
TT
14078 struct remote_state *rs = get_remote_state ();
14079
c1e36e3e
PA
14080 /* Whene enabling, check whether range stepping is actually
14081 supported by the target, and warn if not. */
14082 if (use_range_stepping)
14083 {
5d93a237 14084 if (rs->remote_desc != NULL)
c1e36e3e 14085 {
4082afcc 14086 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
14087 remote_vcont_probe (rs);
14088
4082afcc 14089 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
14090 && rs->supports_vCont.r)
14091 return;
14092 }
14093
14094 warning (_("Range stepping is not supported by the current target"));
14095 }
14096}
14097
c906108c 14098void
fba45db2 14099_initialize_remote (void)
c906108c 14100{
9a7071a8 14101 struct cmd_list_element *cmd;
6f937416 14102 const char *cmd_name;
ea9c271d 14103
0f71a2f6 14104 /* architecture specific data */
2bc416ba 14105 remote_gdbarch_data_handle =
23860348 14106 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
14107 remote_g_packet_data_handle =
14108 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 14109
94585166
DB
14110 remote_pspace_data
14111 = register_program_space_data_with_cleanup (NULL,
14112 remote_pspace_data_cleanup);
14113
ea9c271d
DJ
14114 /* Initialize the per-target state. At the moment there is only one
14115 of these, not one per target. Only one target is active at a
cf792862
TT
14116 time. */
14117 remote_state = new_remote_state ();
ea9c271d 14118
c906108c
SS
14119 init_remote_ops ();
14120 add_target (&remote_ops);
14121
14122 init_extended_remote_ops ();
14123 add_target (&extended_remote_ops);
cce74817 14124
dc8acb97 14125 /* Hook into new objfile notification. */
06d3b283 14126 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
14127 /* We're no longer interested in notification events of an inferior
14128 when it exits. */
14129 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 14130
c906108c
SS
14131#if 0
14132 init_remote_threadtests ();
14133#endif
14134
722247f1 14135 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 14136 /* set/show remote ... */
d471ea57 14137
1bedd215 14138 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
14139Remote protocol specific variables\n\
14140Configure various remote-protocol specific variables such as\n\
1bedd215 14141the packets being used"),
cff3e48b 14142 &remote_set_cmdlist, "set remote ",
23860348 14143 0 /* allow-unknown */, &setlist);
1bedd215 14144 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
14145Remote protocol specific variables\n\
14146Configure various remote-protocol specific variables such as\n\
1bedd215 14147the packets being used"),
cff3e48b 14148 &remote_show_cmdlist, "show remote ",
23860348 14149 0 /* allow-unknown */, &showlist);
5a2468f5 14150
1a966eab
AC
14151 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
14152Compare section data on target to the exec file.\n\
95cf3b38
DT
14153Argument is a single section name (default: all loaded sections).\n\
14154To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
14155 &cmdlist);
14156
1a966eab
AC
14157 add_cmd ("packet", class_maintenance, packet_command, _("\
14158Send an arbitrary packet to a remote target.\n\
c906108c
SS
14159 maintenance packet TEXT\n\
14160If GDB is talking to an inferior via the GDB serial protocol, then\n\
14161this command sends the string TEXT to the inferior, and displays the\n\
14162response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 14163terminating `#' character and checksum."),
c906108c
SS
14164 &maintenancelist);
14165
7915a72c
AC
14166 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
14167Set whether to send break if interrupted."), _("\
14168Show whether to send break if interrupted."), _("\
14169If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 14170 set_remotebreak, show_remotebreak,
e707bbc2 14171 &setlist, &showlist);
9a7071a8
JB
14172 cmd_name = "remotebreak";
14173 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
14174 deprecate_cmd (cmd, "set remote interrupt-sequence");
14175 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
14176 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
14177 deprecate_cmd (cmd, "show remote interrupt-sequence");
14178
14179 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
14180 interrupt_sequence_modes, &interrupt_sequence_mode,
14181 _("\
9a7071a8
JB
14182Set interrupt sequence to remote target."), _("\
14183Show interrupt sequence to remote target."), _("\
14184Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14185 NULL, show_interrupt_sequence,
14186 &remote_set_cmdlist,
14187 &remote_show_cmdlist);
14188
14189 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
14190 &interrupt_on_connect, _("\
14191Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14192Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
14193If set, interrupt sequence is sent to remote target."),
14194 NULL, NULL,
14195 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 14196
23860348 14197 /* Install commands for configuring memory read/write packets. */
11cf8741 14198
1a966eab
AC
14199 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
14200Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 14201 &setlist);
1a966eab
AC
14202 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
14203Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
14204 &showlist);
14205 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
14206 set_memory_write_packet_size, _("\
14207Set the maximum number of bytes per memory-write packet.\n\
14208Specify the number of bytes in a packet or 0 (zero) for the\n\
14209default packet size. The actual limit is further reduced\n\
14210dependent on the target. Specify ``fixed'' to disable the\n\
14211further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14212 &remote_set_cmdlist);
14213 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
14214 set_memory_read_packet_size, _("\
14215Set the maximum number of bytes per memory-read packet.\n\
14216Specify the number of bytes in a packet or 0 (zero) for the\n\
14217default packet size. The actual limit is further reduced\n\
14218dependent on the target. Specify ``fixed'' to disable the\n\
14219further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
14220 &remote_set_cmdlist);
14221 add_cmd ("memory-write-packet-size", no_class,
14222 show_memory_write_packet_size,
1a966eab 14223 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
14224 &remote_show_cmdlist);
14225 add_cmd ("memory-read-packet-size", no_class,
14226 show_memory_read_packet_size,
1a966eab 14227 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 14228 &remote_show_cmdlist);
c906108c 14229
b3f42336 14230 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
14231 &remote_hw_watchpoint_limit, _("\
14232Set the maximum number of target hardware watchpoints."), _("\
14233Show the maximum number of target hardware watchpoints."), _("\
14234Specify a negative limit for unlimited."),
3e43a32a
MS
14235 NULL, NULL, /* FIXME: i18n: The maximum
14236 number of target hardware
14237 watchpoints is %s. */
b3f42336 14238 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
14239 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
14240 &remote_hw_watchpoint_length_limit, _("\
14241Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
14242Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
14243Specify a negative limit for unlimited."),
14244 NULL, NULL, /* FIXME: i18n: The maximum
14245 length (in bytes) of a target
14246 hardware watchpoint is %s. */
14247 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 14248 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
14249 &remote_hw_breakpoint_limit, _("\
14250Set the maximum number of target hardware breakpoints."), _("\
14251Show the maximum number of target hardware breakpoints."), _("\
14252Specify a negative limit for unlimited."),
3e43a32a
MS
14253 NULL, NULL, /* FIXME: i18n: The maximum
14254 number of target hardware
14255 breakpoints is %s. */
b3f42336 14256 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 14257
1b493192
PA
14258 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
14259 &remote_address_size, _("\
4d28ad1e
AC
14260Set the maximum size of the address (in bits) in a memory packet."), _("\
14261Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
14262 NULL,
14263 NULL, /* FIXME: i18n: */
14264 &setlist, &showlist);
c906108c 14265
ca4f7f8b
PA
14266 init_all_packet_configs ();
14267
444abaca 14268 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 14269 "X", "binary-download", 1);
0f71a2f6 14270
444abaca 14271 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 14272 "vCont", "verbose-resume", 0);
506fb367 14273
89be2091
DJ
14274 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
14275 "QPassSignals", "pass-signals", 0);
14276
82075af2
JS
14277 add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
14278 "QCatchSyscalls", "catch-syscalls", 0);
14279
9b224c5e
PA
14280 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
14281 "QProgramSignals", "program-signals", 0);
14282
bc3b087d
SDJ
14283 add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
14284 "QSetWorkingDir", "set-working-dir", 0);
14285
aefd8b33
SDJ
14286 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
14287 "QStartupWithShell", "startup-with-shell", 0);
14288
0a2dde4a
SDJ
14289 add_packet_config_cmd (&remote_protocol_packets
14290 [PACKET_QEnvironmentHexEncoded],
14291 "QEnvironmentHexEncoded", "environment-hex-encoded",
14292 0);
14293
14294 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
14295 "QEnvironmentReset", "environment-reset",
14296 0);
14297
14298 add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
14299 "QEnvironmentUnset", "environment-unset",
14300 0);
14301
444abaca 14302 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 14303 "qSymbol", "symbol-lookup", 0);
dc8acb97 14304
444abaca 14305 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 14306 "P", "set-register", 1);
d471ea57 14307
444abaca 14308 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 14309 "p", "fetch-register", 1);
b96ec7ac 14310
444abaca 14311 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 14312 "Z0", "software-breakpoint", 0);
d471ea57 14313
444abaca 14314 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 14315 "Z1", "hardware-breakpoint", 0);
d471ea57 14316
444abaca 14317 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 14318 "Z2", "write-watchpoint", 0);
d471ea57 14319
444abaca 14320 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 14321 "Z3", "read-watchpoint", 0);
d471ea57 14322
444abaca 14323 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 14324 "Z4", "access-watchpoint", 0);
d471ea57 14325
0876f84a
DJ
14326 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
14327 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 14328
c78fa86a
GB
14329 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
14330 "qXfer:exec-file:read", "pid-to-exec-file", 0);
14331
23181151
DJ
14332 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
14333 "qXfer:features:read", "target-features", 0);
14334
cfa9d6d9
DJ
14335 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
14336 "qXfer:libraries:read", "library-info", 0);
14337
2268b414
JK
14338 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
14339 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
14340
fd79ecee
DJ
14341 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
14342 "qXfer:memory-map:read", "memory-map", 0);
14343
0e7f50da
UW
14344 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
14345 "qXfer:spu:read", "read-spu-object", 0);
14346
14347 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
14348 "qXfer:spu:write", "write-spu-object", 0);
14349
07e059b5
VP
14350 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
14351 "qXfer:osdata:read", "osdata", 0);
14352
dc146f7c
VP
14353 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
14354 "qXfer:threads:read", "threads", 0);
14355
4aa995e1
PA
14356 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
14357 "qXfer:siginfo:read", "read-siginfo-object", 0);
14358
14359 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
14360 "qXfer:siginfo:write", "write-siginfo-object", 0);
14361
b3b9301e
PA
14362 add_packet_config_cmd
14363 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 14364 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 14365
169081d0
TG
14366 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
14367 "qXfer:uib:read", "unwind-info-block", 0);
14368
444abaca 14369 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 14370 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
14371 0);
14372
711e434b
PM
14373 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
14374 "qGetTIBAddr", "get-thread-information-block-address",
14375 0);
14376
40ab02ce
MS
14377 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
14378 "bc", "reverse-continue", 0);
14379
14380 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
14381 "bs", "reverse-step", 0);
14382
be2a5f71
DJ
14383 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
14384 "qSupported", "supported-packets", 0);
14385
08388c79
DE
14386 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
14387 "qSearch:memory", "search-memory", 0);
14388
bd3eecc3
PA
14389 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
14390 "qTStatus", "trace-status", 0);
14391
15a201c8
GB
14392 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
14393 "vFile:setfs", "hostio-setfs", 0);
14394
a6b151f1
DJ
14395 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
14396 "vFile:open", "hostio-open", 0);
14397
14398 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
14399 "vFile:pread", "hostio-pread", 0);
14400
14401 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
14402 "vFile:pwrite", "hostio-pwrite", 0);
14403
14404 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
14405 "vFile:close", "hostio-close", 0);
14406
14407 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
14408 "vFile:unlink", "hostio-unlink", 0);
14409
b9e7b9c3
UW
14410 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
14411 "vFile:readlink", "hostio-readlink", 0);
14412
0a93529c
GB
14413 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
14414 "vFile:fstat", "hostio-fstat", 0);
14415
2d717e4f
DJ
14416 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
14417 "vAttach", "attach", 0);
14418
14419 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
14420 "vRun", "run", 0);
14421
a6f3e723
SL
14422 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
14423 "QStartNoAckMode", "noack", 0);
14424
82f73884
PA
14425 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
14426 "vKill", "kill", 0);
14427
0b16c5cf
PA
14428 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
14429 "qAttached", "query-attached", 0);
14430
782b2b07 14431 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
14432 "ConditionalTracepoints",
14433 "conditional-tracepoints", 0);
3788aec7
LM
14434
14435 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
14436 "ConditionalBreakpoints",
14437 "conditional-breakpoints", 0);
14438
d3ce09f5
SS
14439 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
14440 "BreakpointCommands",
14441 "breakpoint-commands", 0);
14442
7a697b8d
SS
14443 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
14444 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 14445
409873ef
SS
14446 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
14447 "TracepointSource", "TracepointSource", 0);
14448
d914c394
SS
14449 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
14450 "QAllow", "allow", 0);
14451
0fb4aa4b
PA
14452 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
14453 "StaticTracepoints", "static-tracepoints", 0);
14454
1e4d1764
YQ
14455 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
14456 "InstallInTrace", "install-in-trace", 0);
14457
0fb4aa4b
PA
14458 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
14459 "qXfer:statictrace:read", "read-sdata-object", 0);
14460
78d85199
YQ
14461 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
14462 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
14463
03583c20
UW
14464 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
14465 "QDisableRandomization", "disable-randomization", 0);
14466
d1feda86
YQ
14467 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
14468 "QAgent", "agent", 0);
14469
f6f899bf
HAQ
14470 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
14471 "QTBuffer:size", "trace-buffer-size", 0);
14472
9accd112
MM
14473 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
14474 "Qbtrace:off", "disable-btrace", 0);
14475
14476 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
14477 "Qbtrace:bts", "enable-btrace-bts", 0);
14478
14479 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
14480 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
14481
14482 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
14483 "qXfer:btrace", "read-btrace", 0);
14484
f4abbc16
MM
14485 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
14486 "qXfer:btrace-conf", "read-btrace-conf", 0);
14487
d33501a5
MM
14488 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
14489 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
14490
73b8c1fd
PA
14491 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
14492 "multiprocess-feature", "multiprocess-feature", 0);
14493
f7e6eed5
PA
14494 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
14495 "swbreak-feature", "swbreak-feature", 0);
14496
14497 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
14498 "hwbreak-feature", "hwbreak-feature", 0);
14499
89245bc0
DB
14500 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
14501 "fork-event-feature", "fork-event-feature", 0);
14502
14503 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
14504 "vfork-event-feature", "vfork-event-feature", 0);
14505
b20a6524
MM
14506 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
14507 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
14508
750ce8d1
YQ
14509 add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
14510 "vContSupported", "verbose-resume-supported", 0);
14511
94585166
DB
14512 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
14513 "exec-event-feature", "exec-event-feature", 0);
14514
de979965
PA
14515 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
14516 "vCtrlC", "ctrl-c", 0);
14517
65706a29
PA
14518 add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
14519 "QThreadEvents", "thread-events", 0);
14520
f2faf941
PA
14521 add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
14522 "N stop reply", "no-resumed-stop-reply", 0);
14523
0b736949
DB
14524 /* Assert that we've registered "set remote foo-packet" commands
14525 for all packet configs. */
ca4f7f8b
PA
14526 {
14527 int i;
14528
14529 for (i = 0; i < PACKET_MAX; i++)
14530 {
14531 /* Ideally all configs would have a command associated. Some
14532 still don't though. */
14533 int excepted;
14534
14535 switch (i)
14536 {
14537 case PACKET_QNonStop:
ca4f7f8b
PA
14538 case PACKET_EnableDisableTracepoints_feature:
14539 case PACKET_tracenz_feature:
14540 case PACKET_DisconnectedTracing_feature:
14541 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
14542 case PACKET_qCRC:
14543 /* Additions to this list need to be well justified:
14544 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
14545 excepted = 1;
14546 break;
14547 default:
14548 excepted = 0;
14549 break;
14550 }
14551
14552 /* This catches both forgetting to add a config command, and
14553 forgetting to remove a packet from the exception list. */
14554 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
14555 }
14556 }
14557
37a105a1
DJ
14558 /* Keep the old ``set remote Z-packet ...'' working. Each individual
14559 Z sub-packet has its own set and show commands, but users may
14560 have sets to this variable in their .gdbinit files (or in their
14561 documentation). */
e9e68a56 14562 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
14563 &remote_Z_packet_detect, _("\
14564Set use of remote protocol `Z' packets"), _("\
14565Show use of remote protocol `Z' packets "), _("\
3b64bf98 14566When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 14567packets."),
e9e68a56 14568 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
14569 show_remote_protocol_Z_packet_cmd,
14570 /* FIXME: i18n: Use of remote protocol
14571 `Z' packets is %s. */
e9e68a56 14572 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 14573
a6b151f1
DJ
14574 add_prefix_cmd ("remote", class_files, remote_command, _("\
14575Manipulate files on the remote system\n\
14576Transfer files to and from the remote target system."),
14577 &remote_cmdlist, "remote ",
14578 0 /* allow-unknown */, &cmdlist);
14579
14580 add_cmd ("put", class_files, remote_put_command,
14581 _("Copy a local file to the remote system."),
14582 &remote_cmdlist);
14583
14584 add_cmd ("get", class_files, remote_get_command,
14585 _("Copy a remote file to the local system."),
14586 &remote_cmdlist);
14587
14588 add_cmd ("delete", class_files, remote_delete_command,
14589 _("Delete a remote file."),
14590 &remote_cmdlist);
14591
2d717e4f 14592 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 14593 &remote_exec_file_var, _("\
2d717e4f 14594Set the remote pathname for \"run\""), _("\
94585166
DB
14595Show the remote pathname for \"run\""), NULL,
14596 set_remote_exec_file,
14597 show_remote_exec_file,
14598 &remote_set_cmdlist,
14599 &remote_show_cmdlist);
2d717e4f 14600
c1e36e3e
PA
14601 add_setshow_boolean_cmd ("range-stepping", class_run,
14602 &use_range_stepping, _("\
14603Enable or disable range stepping."), _("\
14604Show whether target-assisted range stepping is enabled."), _("\
14605If on, and the target supports it, when stepping a source line, GDB\n\
14606tells the target to step the corresponding range of addresses itself instead\n\
14607of issuing multiple single-steps. This speeds up source level\n\
14608stepping. If off, GDB always issues single-steps, even if range\n\
14609stepping is supported by the target. The default is on."),
14610 set_range_stepping,
14611 show_range_stepping,
14612 &setlist,
14613 &showlist);
14614
449092f6
CV
14615 /* Eventually initialize fileio. See fileio.c */
14616 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 14617
ba348170 14618 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 14619 special ptids with it set to != 0. */
ba348170
PA
14620 magic_null_ptid = ptid_build (42000, -1, 1);
14621 not_sent_ptid = ptid_build (42000, -2, 1);
14622 any_thread_ptid = ptid_build (42000, 0, 1);
c906108c 14623}
This page took 3.692727 seconds and 4 git commands to generate.