gdb: Clean up remote.c:remote_resume
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
618f726f 3 Copyright (C) 1988-2016 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>
35b1e5cc 75
0df8b418 76/* Temp hacks for tracepoint encoding migration. */
35b1e5cc
SS
77static char *target_buf;
78static long target_buf_size;
35b1e5cc 79
94585166
DB
80/* Per-program-space data key. */
81static const struct program_space_data *remote_pspace_data;
82
83/* The variable registered as the control variable used by the
84 remote exec-file commands. While the remote exec-file setting is
85 per-program-space, the set/show machinery uses this as the
86 location of the remote exec-file value. */
87static char *remote_exec_file_var;
88
6765f3e5
DJ
89/* The size to align memory write packets, when practical. The protocol
90 does not guarantee any alignment, and gdb will generate short
91 writes and unaligned writes, but even as a best-effort attempt this
92 can improve bulk transfers. For instance, if a write is misaligned
93 relative to the target's data bus, the stub may need to make an extra
94 round trip fetching data from the target. This doesn't make a
95 huge difference, but it's easy to do, so we try to be helpful.
96
97 The alignment chosen is arbitrary; usually data bus width is
98 important here, not the possibly larger cache line size. */
99enum { REMOTE_ALIGN_WRITES = 16 };
100
23860348 101/* Prototypes for local functions. */
6d820c5c 102static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 103static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 104 int forever, int *is_notif);
6426a772 105
a14ed312 106static void remote_files_info (struct target_ops *ignore);
c906108c 107
f32dbf8c
MM
108static void remote_prepare_to_store (struct target_ops *self,
109 struct regcache *regcache);
c906108c 110
014f9477
TT
111static void remote_open_1 (const char *, int, struct target_ops *,
112 int extended_p);
c906108c 113
de90e03d 114static void remote_close (struct target_ops *self);
c906108c 115
cbb8991c
DB
116struct remote_state;
117
118static int remote_vkill (int pid, struct remote_state *rs);
119
8020350c
DB
120static void remote_kill_k (void);
121
136d6dae 122static void remote_mourn (struct target_ops *ops);
c906108c 123
a14ed312 124static void extended_remote_restart (void);
c906108c 125
6d820c5c 126static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 127
a14ed312 128static int readchar (int timeout);
c906108c 129
c33e31fd
PA
130static void remote_serial_write (const char *str, int len);
131
7d85a9c0 132static void remote_kill (struct target_ops *ops);
c906108c 133
6a109b6b 134static int remote_can_async_p (struct target_ops *);
75c99385 135
6a109b6b 136static int remote_is_async_p (struct target_ops *);
75c99385 137
6a3753b3 138static void remote_async (struct target_ops *ops, int enable);
75c99385 139
65706a29
PA
140static void remote_thread_events (struct target_ops *ops, int enable);
141
a14ed312 142static void interrupt_query (void);
c906108c 143
79d7f229
PA
144static void set_general_thread (struct ptid ptid);
145static void set_continue_thread (struct ptid ptid);
c906108c 146
a14ed312 147static void get_offsets (void);
c906108c 148
6d820c5c
DJ
149static void skip_frame (void);
150
151static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 152
a14ed312 153static int hexnumlen (ULONGEST num);
c906108c 154
a14ed312 155static void init_remote_ops (void);
c906108c 156
a14ed312 157static void init_extended_remote_ops (void);
c906108c 158
1eab8a48 159static void remote_stop (struct target_ops *self, ptid_t);
c906108c 160
a14ed312 161static int stubhex (int ch);
c906108c 162
a14ed312 163static int hexnumstr (char *, ULONGEST);
c906108c 164
a14ed312 165static int hexnumnstr (char *, ULONGEST, int);
2df3850c 166
a14ed312 167static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 168
baa336ce 169static void print_packet (const char *);
c906108c 170
a14ed312 171static void compare_sections_command (char *, int);
c906108c 172
a14ed312 173static void packet_command (char *, int);
c906108c 174
a14ed312 175static int stub_unpack_int (char *buff, int fieldlength);
c906108c 176
39f77062 177static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 178
baa336ce 179static int putpkt_binary (const char *buf, int cnt);
c906108c 180
a14ed312 181static void check_binary_download (CORE_ADDR addr);
c906108c 182
5a2468f5 183struct packet_config;
5a2468f5 184
a14ed312 185static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 186
bb572ddd
DJ
187static void show_remote_protocol_packet_cmd (struct ui_file *file,
188 int from_tty,
189 struct cmd_list_element *c,
190 const char *value);
191
82f73884
PA
192static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
193static ptid_t read_ptid (char *buf, char **obuf);
194
c378d69d 195static void remote_set_permissions (struct target_ops *self);
d914c394 196
8bd200f1
TT
197static int remote_get_trace_status (struct target_ops *self,
198 struct trace_status *ts);
d5551862 199
ab6617cc
TT
200static int remote_upload_tracepoints (struct target_ops *self,
201 struct uploaded_tp **utpp);
00bf0b85 202
181e3713
TT
203static int remote_upload_trace_state_variables (struct target_ops *self,
204 struct uploaded_tsv **utsvp);
00bf0b85 205
c8d104ad
PA
206static void remote_query_supported (void);
207
36d25514 208static void remote_check_symbols (void);
c8d104ad 209
a14ed312 210void _initialize_remote (void);
c906108c 211
74531fed 212struct stop_reply;
74531fed 213static void stop_reply_xfree (struct stop_reply *);
722247f1 214static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 215static void push_stop_reply (struct stop_reply *);
bcc75809 216static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
217static int peek_stop_reply (ptid_t ptid);
218
cbb8991c
DB
219struct threads_listing_context;
220static void remove_new_fork_children (struct threads_listing_context *);
221
74531fed 222static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 223
e3594fd1 224static void remote_terminal_ours (struct target_ops *self);
d3fd5342 225
d962ef82
DJ
226static int remote_read_description_p (struct target_ops *target);
227
176a6961 228static void remote_console_output (char *msg);
dde08ee1 229
efcc2da7 230static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 231
78eff0ec 232static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 233
f4abbc16
MM
234static void remote_btrace_reset (void);
235
c0272db5
TW
236static void remote_btrace_maybe_reopen (void);
237
221e1a37
PA
238static int stop_reply_queue_length (void);
239
80152258
PA
240static void readahead_cache_invalidate (void);
241
048094ac
PA
242static void remote_unpush_and_throw (void);
243
a6b151f1
DJ
244/* For "remote". */
245
246static struct cmd_list_element *remote_cmdlist;
247
bb572ddd
DJ
248/* For "set remote" and "show remote". */
249
250static struct cmd_list_element *remote_set_cmdlist;
251static struct cmd_list_element *remote_show_cmdlist;
252
d458bd84
PA
253/* Stub vCont actions support.
254
255 Each field is a boolean flag indicating whether the stub reports
256 support for the corresponding action. */
257
258struct vCont_action_support
259{
260 /* vCont;t */
261 int t;
c1e36e3e
PA
262
263 /* vCont;r */
264 int r;
750ce8d1
YQ
265
266 /* vCont;s */
267 int s;
268
269 /* vCont;S */
270 int S;
d458bd84
PA
271};
272
c1e36e3e
PA
273/* Controls whether GDB is willing to use range stepping. */
274
275static int use_range_stepping = 1;
276
0d031856
TT
277#define OPAQUETHREADBYTES 8
278
279/* a 64 bit opaque identifier */
280typedef unsigned char threadref[OPAQUETHREADBYTES];
281
282/* About this many threadisds fit in a packet. */
283
284#define MAXTHREADLISTRESULTS 32
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
PA
448
449 /* Whether the target stopped for a breakpoint/watchpoint. */
450 enum target_stop_reason stop_reason;
451
452 /* This is set to the data address of the access causing the target
453 to stop for a watchpoint. */
454 CORE_ADDR watch_data_address;
dc146f7c
VP
455};
456
457static void
458free_private_thread_info (struct private_thread_info *info)
459{
460 xfree (info->extra);
79efa585 461 xfree (info->name);
dc146f7c
VP
462 xfree (info);
463}
464
ea9c271d
DJ
465/* This data could be associated with a target, but we do not always
466 have access to the current target when we need it, so for now it is
467 static. This will be fine for as long as only one target is in use
468 at a time. */
cf792862 469static struct remote_state *remote_state;
ea9c271d
DJ
470
471static struct remote_state *
0b83947e 472get_remote_state_raw (void)
ea9c271d 473{
cf792862
TT
474 return remote_state;
475}
476
477/* Allocate a new struct remote_state with xmalloc, initialize it, and
478 return it. */
479
480static struct remote_state *
481new_remote_state (void)
482{
483 struct remote_state *result = XCNEW (struct remote_state);
484
485 /* The default buffer size is unimportant; it will be expanded
486 whenever a larger buffer is needed. */
487 result->buf_size = 400;
224c3ddb 488 result->buf = (char *) xmalloc (result->buf_size);
262e1174 489 result->remote_traceframe_number = -1;
b73be471 490 result->last_sent_signal = GDB_SIGNAL_0;
3a00c802 491 result->last_resume_exec_dir = EXEC_FORWARD;
15a201c8 492 result->fs_pid = -1;
cf792862
TT
493
494 return result;
ea9c271d
DJ
495}
496
497/* Description of the remote protocol for a given architecture. */
d01949b6 498
ad10f812
AC
499struct packet_reg
500{
501 long offset; /* Offset into G packet. */
502 long regnum; /* GDB's internal register number. */
503 LONGEST pnum; /* Remote protocol register number. */
b323314b 504 int in_g_packet; /* Always part of G packet. */
f5656ead 505 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 506 at present. */
f5656ead 507 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 508 at present. */
ad10f812
AC
509};
510
ea9c271d 511struct remote_arch_state
d01949b6 512{
ad10f812
AC
513 /* Description of the remote protocol registers. */
514 long sizeof_g_packet;
b323314b
AC
515
516 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 517 (making an array gdbarch_num_regs in size). */
b323314b 518 struct packet_reg *regs;
ad10f812 519
d01949b6
AC
520 /* This is the size (in chars) of the first response to the ``g''
521 packet. It is used as a heuristic when determining the maximum
522 size of memory-read and memory-write packets. A target will
523 typically only reserve a buffer large enough to hold the ``g''
524 packet. The size does not include packet overhead (headers and
23860348 525 trailers). */
d01949b6
AC
526 long actual_register_packet_size;
527
528 /* This is the maximum size (in chars) of a non read/write packet.
23860348 529 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
530 long remote_packet_size;
531};
532
35b1e5cc
SS
533/* Utility: generate error from an incoming stub packet. */
534static void
535trace_error (char *buf)
536{
537 if (*buf++ != 'E')
538 return; /* not an error msg */
539 switch (*buf)
540 {
541 case '1': /* malformed packet error */
542 if (*++buf == '0') /* general case: */
543 error (_("remote.c: error in outgoing packet."));
544 else
545 error (_("remote.c: error in outgoing packet at field #%ld."),
546 strtol (buf, NULL, 16));
35b1e5cc
SS
547 default:
548 error (_("Target returns error code '%s'."), buf);
549 }
550}
551
552/* Utility: wait for reply from stub, while accepting "O" packets. */
553static char *
554remote_get_noisy_reply (char **buf_p,
555 long *sizeof_buf)
556{
557 do /* Loop on reply from remote stub. */
558 {
559 char *buf;
a744cf53 560
0df8b418 561 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
562 getpkt (buf_p, sizeof_buf, 0);
563 buf = *buf_p;
ad91cd99 564 if (buf[0] == 'E')
35b1e5cc 565 trace_error (buf);
61012eef 566 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
567 {
568 ULONGEST ul;
569 CORE_ADDR from, to, org_to;
570 char *p, *pp;
571 int adjusted_size = 0;
7556d4a4 572 int relocated = 0;
dde08ee1
PA
573
574 p = buf + strlen ("qRelocInsn:");
575 pp = unpack_varlen_hex (p, &ul);
576 if (*pp != ';')
cb91c06a 577 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
578 from = ul;
579
580 p = pp + 1;
a9cbf802 581 unpack_varlen_hex (p, &ul);
dde08ee1
PA
582 to = ul;
583
584 org_to = to;
585
492d29ea 586 TRY
dde08ee1 587 {
f5656ead 588 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 589 relocated = 1;
dde08ee1 590 }
492d29ea 591 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
592 {
593 if (ex.error == MEMORY_ERROR)
594 {
595 /* Propagate memory errors silently back to the
596 target. The stub may have limited the range of
597 addresses we can write to, for example. */
598 }
599 else
600 {
601 /* Something unexpectedly bad happened. Be verbose
602 so we can tell what, and propagate the error back
603 to the stub, so it doesn't get stuck waiting for
604 a response. */
605 exception_fprintf (gdb_stderr, ex,
606 _("warning: relocating instruction: "));
607 }
608 putpkt ("E01");
609 }
492d29ea 610 END_CATCH
7556d4a4
PA
611
612 if (relocated)
dde08ee1
PA
613 {
614 adjusted_size = to - org_to;
615
bba74b36 616 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
617 putpkt (buf);
618 }
dde08ee1 619 }
ad91cd99 620 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
621 remote_console_output (buf + 1); /* 'O' message from stub */
622 else
0df8b418 623 return buf; /* Here's the actual reply. */
35b1e5cc
SS
624 }
625 while (1);
626}
3c3bea1c 627
d01949b6
AC
628/* Handle for retreving the remote protocol data from gdbarch. */
629static struct gdbarch_data *remote_gdbarch_data_handle;
630
ea9c271d
DJ
631static struct remote_arch_state *
632get_remote_arch_state (void)
d01949b6 633{
17d8546e 634 gdb_assert (target_gdbarch () != NULL);
19ba03f4
SM
635 return ((struct remote_arch_state *)
636 gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle));
d01949b6
AC
637}
638
0b83947e
DJ
639/* Fetch the global remote target state. */
640
641static struct remote_state *
642get_remote_state (void)
643{
644 /* Make sure that the remote architecture state has been
645 initialized, because doing so might reallocate rs->buf. Any
646 function which calls getpkt also needs to be mindful of changes
647 to rs->buf, but this call limits the number of places which run
648 into trouble. */
649 get_remote_arch_state ();
650
651 return get_remote_state_raw ();
652}
653
94585166
DB
654/* Cleanup routine for the remote module's pspace data. */
655
656static void
657remote_pspace_data_cleanup (struct program_space *pspace, void *arg)
658{
19ba03f4 659 char *remote_exec_file = (char *) arg;
94585166
DB
660
661 xfree (remote_exec_file);
662}
663
664/* Fetch the remote exec-file from the current program space. */
665
666static const char *
667get_remote_exec_file (void)
668{
669 char *remote_exec_file;
670
19ba03f4
SM
671 remote_exec_file
672 = (char *) program_space_data (current_program_space,
673 remote_pspace_data);
94585166
DB
674 if (remote_exec_file == NULL)
675 return "";
676
677 return remote_exec_file;
678}
679
680/* Set the remote exec file for PSPACE. */
681
682static void
683set_pspace_remote_exec_file (struct program_space *pspace,
684 char *remote_exec_file)
685{
19ba03f4 686 char *old_file = (char *) program_space_data (pspace, remote_pspace_data);
94585166
DB
687
688 xfree (old_file);
689 set_program_space_data (pspace, remote_pspace_data,
690 xstrdup (remote_exec_file));
691}
692
693/* The "set/show remote exec-file" set command hook. */
694
695static void
696set_remote_exec_file (char *ignored, int from_tty,
697 struct cmd_list_element *c)
698{
699 gdb_assert (remote_exec_file_var != NULL);
700 set_pspace_remote_exec_file (current_program_space, remote_exec_file_var);
701}
702
703/* The "set/show remote exec-file" show command hook. */
704
705static void
706show_remote_exec_file (struct ui_file *file, int from_tty,
707 struct cmd_list_element *cmd, const char *value)
708{
709 fprintf_filtered (file, "%s\n", remote_exec_file_var);
710}
711
74ca34ce
DJ
712static int
713compare_pnums (const void *lhs_, const void *rhs_)
714{
19ba03f4
SM
715 const struct packet_reg * const *lhs
716 = (const struct packet_reg * const *) lhs_;
717 const struct packet_reg * const *rhs
718 = (const struct packet_reg * const *) rhs_;
74ca34ce
DJ
719
720 if ((*lhs)->pnum < (*rhs)->pnum)
721 return -1;
722 else if ((*lhs)->pnum == (*rhs)->pnum)
723 return 0;
724 else
725 return 1;
726}
727
c21236dc
PA
728static int
729map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 730{
74ca34ce 731 int regnum, num_remote_regs, offset;
74ca34ce 732 struct packet_reg **remote_regs;
ea9c271d 733
4a22f64d 734 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 735 {
c21236dc 736 struct packet_reg *r = &regs[regnum];
baef701f 737
4a22f64d 738 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
739 /* Do not try to fetch zero-sized (placeholder) registers. */
740 r->pnum = -1;
741 else
742 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
743
b323314b 744 r->regnum = regnum;
74ca34ce
DJ
745 }
746
747 /* Define the g/G packet format as the contents of each register
748 with a remote protocol number, in order of ascending protocol
749 number. */
750
224c3ddb 751 remote_regs = XALLOCAVEC (struct packet_reg *, gdbarch_num_regs (gdbarch));
f57d151a 752 for (num_remote_regs = 0, regnum = 0;
4a22f64d 753 regnum < gdbarch_num_regs (gdbarch);
f57d151a 754 regnum++)
c21236dc
PA
755 if (regs[regnum].pnum != -1)
756 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 757
74ca34ce
DJ
758 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
759 compare_pnums);
760
761 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
762 {
763 remote_regs[regnum]->in_g_packet = 1;
764 remote_regs[regnum]->offset = offset;
4a22f64d 765 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
766 }
767
c21236dc
PA
768 return offset;
769}
770
771/* Given the architecture described by GDBARCH, return the remote
772 protocol register's number and the register's offset in the g/G
773 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
774 If the target does not have a mapping for REGNUM, return false,
775 otherwise, return true. */
776
777int
778remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
779 int *pnum, int *poffset)
780{
c21236dc
PA
781 struct packet_reg *regs;
782 struct cleanup *old_chain;
783
784 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
785
224c3ddb 786 regs = XCNEWVEC (struct packet_reg, gdbarch_num_regs (gdbarch));
c21236dc
PA
787 old_chain = make_cleanup (xfree, regs);
788
54887903 789 map_regcache_remote_table (gdbarch, regs);
c21236dc
PA
790
791 *pnum = regs[regnum].pnum;
792 *poffset = regs[regnum].offset;
793
794 do_cleanups (old_chain);
795
796 return *pnum != -1;
797}
798
799static void *
800init_remote_state (struct gdbarch *gdbarch)
801{
802 struct remote_state *rs = get_remote_state_raw ();
803 struct remote_arch_state *rsa;
804
805 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
806
807 /* Use the architecture to build a regnum<->pnum table, which will be
808 1:1 unless a feature set specifies otherwise. */
809 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
810 gdbarch_num_regs (gdbarch),
811 struct packet_reg);
812
74ca34ce
DJ
813 /* Record the maximum possible size of the g packet - it may turn out
814 to be smaller. */
c21236dc 815 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 816
0df8b418 817 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
818 remote stubs have a hardwired buffer size of 400 bytes
819 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
820 as the maximum packet-size to ensure that the packet and an extra
821 NUL character can always fit in the buffer. This stops GDB
822 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
823 already a full buffer (As of 1999-12-04 that was most stubs). */
824 rsa->remote_packet_size = 400 - 1;
d01949b6 825
ea9c271d
DJ
826 /* This one is filled in when a ``g'' packet is received. */
827 rsa->actual_register_packet_size = 0;
828
829 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
830 default, adjust the size accordingly. Remember that each byte is
831 encoded as two characters. 32 is the overhead for the packet
832 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 833 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 834 little. */
ea9c271d
DJ
835 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
836 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 837
ea9c271d
DJ
838 /* Make sure that the packet buffer is plenty big enough for
839 this architecture. */
840 if (rs->buf_size < rsa->remote_packet_size)
841 {
842 rs->buf_size = 2 * rsa->remote_packet_size;
224c3ddb 843 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
ea9c271d 844 }
6d820c5c 845
ea9c271d
DJ
846 return rsa;
847}
848
849/* Return the current allowed size of a remote packet. This is
850 inferred from the current architecture, and should be used to
851 limit the length of outgoing packets. */
852static long
853get_remote_packet_size (void)
854{
be2a5f71 855 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
856 struct remote_arch_state *rsa = get_remote_arch_state ();
857
be2a5f71
DJ
858 if (rs->explicit_packet_size)
859 return rs->explicit_packet_size;
860
ea9c271d 861 return rsa->remote_packet_size;
d01949b6
AC
862}
863
ad10f812 864static struct packet_reg *
ea9c271d 865packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 866{
f5656ead 867 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
868 return NULL;
869 else
ad10f812 870 {
ea9c271d 871 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 872
b323314b
AC
873 gdb_assert (r->regnum == regnum);
874 return r;
ad10f812 875 }
ad10f812
AC
876}
877
878static struct packet_reg *
ea9c271d 879packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 880{
b323314b 881 int i;
a744cf53 882
f5656ead 883 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 884 {
ea9c271d 885 struct packet_reg *r = &rsa->regs[i];
a744cf53 886
b323314b
AC
887 if (r->pnum == pnum)
888 return r;
ad10f812
AC
889 }
890 return NULL;
d01949b6
AC
891}
892
c906108c
SS
893static struct target_ops remote_ops;
894
895static struct target_ops extended_remote_ops;
896
6426a772
JM
897/* FIXME: cagney/1999-09-23: Even though getpkt was called with
898 ``forever'' still use the normal timeout mechanism. This is
899 currently used by the ASYNC code to guarentee that target reads
900 during the initial connect always time-out. Once getpkt has been
901 modified to return a timeout indication and, in turn
902 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 903 this can go away. */
6426a772
JM
904static int wait_forever_enabled_p = 1;
905
9a7071a8
JB
906/* Allow the user to specify what sequence to send to the remote
907 when he requests a program interruption: Although ^C is usually
908 what remote systems expect (this is the default, here), it is
909 sometimes preferable to send a break. On other systems such
910 as the Linux kernel, a break followed by g, which is Magic SysRq g
911 is required in order to interrupt the execution. */
912const char interrupt_sequence_control_c[] = "Ctrl-C";
913const char interrupt_sequence_break[] = "BREAK";
914const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 915static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
916 {
917 interrupt_sequence_control_c,
918 interrupt_sequence_break,
919 interrupt_sequence_break_g,
920 NULL
921 };
922static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
923
924static void
925show_interrupt_sequence (struct ui_file *file, int from_tty,
926 struct cmd_list_element *c,
927 const char *value)
928{
929 if (interrupt_sequence_mode == interrupt_sequence_control_c)
930 fprintf_filtered (file,
931 _("Send the ASCII ETX character (Ctrl-c) "
932 "to the remote target to interrupt the "
933 "execution of the program.\n"));
934 else if (interrupt_sequence_mode == interrupt_sequence_break)
935 fprintf_filtered (file,
936 _("send a break signal to the remote target "
937 "to interrupt the execution of the program.\n"));
938 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
939 fprintf_filtered (file,
940 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
941 "the remote target to interrupt the execution "
942 "of Linux kernel.\n"));
943 else
944 internal_error (__FILE__, __LINE__,
945 _("Invalid value for interrupt_sequence_mode: %s."),
946 interrupt_sequence_mode);
947}
6426a772 948
9a7071a8
JB
949/* This boolean variable specifies whether interrupt_sequence is sent
950 to the remote target when gdb connects to it.
951 This is mostly needed when you debug the Linux kernel: The Linux kernel
952 expects BREAK g which is Magic SysRq g for connecting gdb. */
953static int interrupt_on_connect = 0;
c906108c 954
9a7071a8
JB
955/* This variable is used to implement the "set/show remotebreak" commands.
956 Since these commands are now deprecated in favor of "set/show remote
957 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
958static int remote_break;
959
9a7071a8
JB
960static void
961set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
962{
963 if (remote_break)
964 interrupt_sequence_mode = interrupt_sequence_break;
965 else
966 interrupt_sequence_mode = interrupt_sequence_control_c;
967}
968
969static void
970show_remotebreak (struct ui_file *file, int from_tty,
971 struct cmd_list_element *c,
972 const char *value)
973{
974}
975
c906108c
SS
976/* This variable sets the number of bits in an address that are to be
977 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 978 leading zeros, the entire address would be sent. This variable
c906108c
SS
979 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
980 initial implementation of remote.c restricted the address sent in
981 memory packets to ``host::sizeof long'' bytes - (typically 32
982 bits). Consequently, for 64 bit targets, the upper 32 bits of an
983 address was never sent. Since fixing this bug may cause a break in
984 some remote targets this variable is principly provided to
23860348 985 facilitate backward compatibility. */
c906108c 986
883b9c6c 987static unsigned int remote_address_size;
c906108c 988
75c99385
PA
989/* Temporary to track who currently owns the terminal. See
990 remote_terminal_* for more details. */
6426a772
JM
991
992static int remote_async_terminal_ours_p;
993
11cf8741 994\f
11cf8741 995/* User configurable variables for the number of characters in a
ea9c271d
DJ
996 memory read/write packet. MIN (rsa->remote_packet_size,
997 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 998 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
999 (speed up transfers). The variables ``preferred_*'' (the user
1000 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 1001 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
1002
1003struct memory_packet_config
1004{
1005 char *name;
1006 long size;
1007 int fixed_p;
1008};
1009
a5c0808e
PA
1010/* The default max memory-write-packet-size. The 16k is historical.
1011 (It came from older GDB's using alloca for buffers and the
1012 knowledge (folklore?) that some hosts don't cope very well with
1013 large alloca calls.) */
1014#define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384
1015
1016/* The minimum remote packet size for memory transfers. Ensures we
1017 can write at least one byte. */
1018#define MIN_MEMORY_PACKET_SIZE 20
1019
11cf8741
JM
1020/* Compute the current size of a read/write packet. Since this makes
1021 use of ``actual_register_packet_size'' the computation is dynamic. */
1022
1023static long
1024get_memory_packet_size (struct memory_packet_config *config)
1025{
d01949b6 1026 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
1027 struct remote_arch_state *rsa = get_remote_arch_state ();
1028
11cf8741
JM
1029 long what_they_get;
1030 if (config->fixed_p)
1031 {
1032 if (config->size <= 0)
a5c0808e 1033 what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE;
11cf8741
JM
1034 else
1035 what_they_get = config->size;
1036 }
1037 else
1038 {
ea9c271d 1039 what_they_get = get_remote_packet_size ();
23860348 1040 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1041 if (config->size > 0
1042 && what_they_get > config->size)
1043 what_they_get = config->size;
be2a5f71
DJ
1044
1045 /* Limit it to the size of the targets ``g'' response unless we have
1046 permission from the stub to use a larger packet size. */
1047 if (rs->explicit_packet_size == 0
1048 && rsa->actual_register_packet_size > 0
1049 && what_they_get > rsa->actual_register_packet_size)
1050 what_they_get = rsa->actual_register_packet_size;
11cf8741 1051 }
a5c0808e
PA
1052 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1053 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1054
1055 /* Make sure there is room in the global buffer for this packet
1056 (including its trailing NUL byte). */
1057 if (rs->buf_size < what_they_get + 1)
1058 {
1059 rs->buf_size = 2 * what_they_get;
224c3ddb 1060 rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get);
6d820c5c
DJ
1061 }
1062
11cf8741
JM
1063 return what_they_get;
1064}
1065
0df8b418 1066/* Update the size of a read/write packet. If they user wants
23860348 1067 something really big then do a sanity check. */
11cf8741
JM
1068
1069static void
1070set_memory_packet_size (char *args, struct memory_packet_config *config)
1071{
1072 int fixed_p = config->fixed_p;
1073 long size = config->size;
a744cf53 1074
11cf8741 1075 if (args == NULL)
8a3fe4f8 1076 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1077 else if (strcmp (args, "hard") == 0
1078 || strcmp (args, "fixed") == 0)
1079 fixed_p = 1;
1080 else if (strcmp (args, "soft") == 0
1081 || strcmp (args, "limit") == 0)
1082 fixed_p = 0;
1083 else
1084 {
1085 char *end;
a744cf53 1086
11cf8741
JM
1087 size = strtoul (args, &end, 0);
1088 if (args == end)
8a3fe4f8 1089 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1090
1091 /* Instead of explicitly capping the size of a packet to or
1092 disallowing it, the user is allowed to set the size to
1093 something arbitrarily large. */
11cf8741 1094 }
a5c0808e
PA
1095
1096 /* So that the query shows the correct value. */
1097 if (size <= 0)
1098 size = DEFAULT_MAX_MEMORY_PACKET_SIZE;
1099
23860348 1100 /* Extra checks? */
11cf8741
JM
1101 if (fixed_p && !config->fixed_p)
1102 {
e2e0b3e5
AC
1103 if (! query (_("The target may not be able to correctly handle a %s\n"
1104 "of %ld bytes. Change the packet size? "),
11cf8741 1105 config->name, size))
8a3fe4f8 1106 error (_("Packet size not changed."));
11cf8741 1107 }
23860348 1108 /* Update the config. */
11cf8741
JM
1109 config->fixed_p = fixed_p;
1110 config->size = size;
1111}
1112
1113static void
1114show_memory_packet_size (struct memory_packet_config *config)
1115{
a3f17187 1116 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1117 if (config->fixed_p)
a3f17187 1118 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1119 get_memory_packet_size (config));
1120 else
a3f17187 1121 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1122 get_memory_packet_size (config));
1123}
1124
1125static struct memory_packet_config memory_write_packet_config =
1126{
1127 "memory-write-packet-size",
1128};
1129
1130static void
1131set_memory_write_packet_size (char *args, int from_tty)
1132{
1133 set_memory_packet_size (args, &memory_write_packet_config);
1134}
1135
1136static void
1137show_memory_write_packet_size (char *args, int from_tty)
1138{
1139 show_memory_packet_size (&memory_write_packet_config);
1140}
1141
1142static long
1143get_memory_write_packet_size (void)
1144{
1145 return get_memory_packet_size (&memory_write_packet_config);
1146}
1147
1148static struct memory_packet_config memory_read_packet_config =
1149{
1150 "memory-read-packet-size",
1151};
1152
1153static void
1154set_memory_read_packet_size (char *args, int from_tty)
1155{
1156 set_memory_packet_size (args, &memory_read_packet_config);
1157}
1158
1159static void
1160show_memory_read_packet_size (char *args, int from_tty)
1161{
1162 show_memory_packet_size (&memory_read_packet_config);
1163}
1164
1165static long
1166get_memory_read_packet_size (void)
1167{
1168 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1169
11cf8741
JM
1170 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1171 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1172 increased beyond this. */
1173 if (size > get_remote_packet_size ())
1174 size = get_remote_packet_size ();
11cf8741
JM
1175 return size;
1176}
1177
11cf8741 1178\f
5a2468f5 1179/* Generic configuration support for packets the stub optionally
0df8b418 1180 supports. Allows the user to specify the use of the packet as well
23860348 1181 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1182
1183enum packet_support
1184 {
1185 PACKET_SUPPORT_UNKNOWN = 0,
1186 PACKET_ENABLE,
1187 PACKET_DISABLE
1188 };
1189
5a2468f5
JM
1190struct packet_config
1191 {
bb572ddd
DJ
1192 const char *name;
1193 const char *title;
4082afcc
PA
1194
1195 /* If auto, GDB auto-detects support for this packet or feature,
1196 either through qSupported, or by trying the packet and looking
1197 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1198 packet. If false, the packet is disabled. Configs that don't
1199 have an associated command always have this set to auto. */
7f19b9a2 1200 enum auto_boolean detect;
4082afcc
PA
1201
1202 /* Does the target support this packet? */
5a2468f5
JM
1203 enum packet_support support;
1204 };
1205
d471ea57 1206/* Analyze a packet's return value and update the packet config
23860348 1207 accordingly. */
d471ea57
AC
1208
1209enum packet_result
1210{
1211 PACKET_ERROR,
1212 PACKET_OK,
1213 PACKET_UNKNOWN
1214};
1215
4082afcc
PA
1216static enum packet_support packet_config_support (struct packet_config *config);
1217static enum packet_support packet_support (int packet);
5a2468f5
JM
1218
1219static void
fba45db2 1220show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1221{
1222 char *support = "internal-error";
a744cf53 1223
4082afcc 1224 switch (packet_config_support (config))
5a2468f5
JM
1225 {
1226 case PACKET_ENABLE:
1227 support = "enabled";
1228 break;
1229 case PACKET_DISABLE:
1230 support = "disabled";
1231 break;
1232 case PACKET_SUPPORT_UNKNOWN:
1233 support = "unknown";
1234 break;
1235 }
1236 switch (config->detect)
1237 {
7f19b9a2 1238 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1239 printf_filtered (_("Support for the `%s' packet "
1240 "is auto-detected, currently %s.\n"),
37a105a1 1241 config->name, support);
5a2468f5 1242 break;
7f19b9a2
AC
1243 case AUTO_BOOLEAN_TRUE:
1244 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1245 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1246 config->name, support);
8e248173 1247 break;
5a2468f5
JM
1248 }
1249}
1250
1251static void
bb572ddd
DJ
1252add_packet_config_cmd (struct packet_config *config, const char *name,
1253 const char *title, int legacy)
d471ea57 1254{
5a2468f5
JM
1255 char *set_doc;
1256 char *show_doc;
d471ea57 1257 char *cmd_name;
3ed07be4 1258
5a2468f5
JM
1259 config->name = name;
1260 config->title = title;
b435e160
AC
1261 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1262 name, title);
3e43a32a
MS
1263 show_doc = xstrprintf ("Show current use of remote "
1264 "protocol `%s' (%s) packet",
b435e160 1265 name, title);
d471ea57 1266 /* set/show TITLE-packet {auto,on,off} */
b435e160 1267 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1268 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1269 &config->detect, set_doc,
1270 show_doc, NULL, /* help_doc */
4082afcc 1271 NULL,
bb572ddd
DJ
1272 show_remote_protocol_packet_cmd,
1273 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1274 /* The command code copies the documentation strings. */
1275 xfree (set_doc);
1276 xfree (show_doc);
23860348 1277 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1278 if (legacy)
1279 {
1280 char *legacy_name;
a744cf53 1281
b435e160 1282 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1283 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1284 &remote_set_cmdlist);
d471ea57 1285 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1286 &remote_show_cmdlist);
d471ea57 1287 }
5a2468f5
JM
1288}
1289
d471ea57 1290static enum packet_result
a76d924d 1291packet_check_result (const char *buf)
5a2468f5 1292{
d471ea57 1293 if (buf[0] != '\0')
5a2468f5 1294 {
d471ea57 1295 /* The stub recognized the packet request. Check that the
23860348 1296 operation succeeded. */
a76d924d
DJ
1297 if (buf[0] == 'E'
1298 && isxdigit (buf[1]) && isxdigit (buf[2])
1299 && buf[3] == '\0')
1300 /* "Enn" - definitly an error. */
1301 return PACKET_ERROR;
1302
1303 /* Always treat "E." as an error. This will be used for
1304 more verbose error messages, such as E.memtypes. */
1305 if (buf[0] == 'E' && buf[1] == '.')
1306 return PACKET_ERROR;
1307
1308 /* The packet may or may not be OK. Just assume it is. */
1309 return PACKET_OK;
1310 }
1311 else
1312 /* The stub does not support the packet. */
1313 return PACKET_UNKNOWN;
1314}
1315
1316static enum packet_result
1317packet_ok (const char *buf, struct packet_config *config)
1318{
1319 enum packet_result result;
1320
4082afcc
PA
1321 if (config->detect != AUTO_BOOLEAN_TRUE
1322 && config->support == PACKET_DISABLE)
1323 internal_error (__FILE__, __LINE__,
1324 _("packet_ok: attempt to use a disabled packet"));
1325
a76d924d
DJ
1326 result = packet_check_result (buf);
1327 switch (result)
1328 {
1329 case PACKET_OK:
1330 case PACKET_ERROR:
1331 /* The stub recognized the packet request. */
4082afcc 1332 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1333 {
d471ea57
AC
1334 if (remote_debug)
1335 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1336 "Packet %s (%s) is supported\n",
1337 config->name, config->title);
d471ea57 1338 config->support = PACKET_ENABLE;
d471ea57 1339 }
a76d924d
DJ
1340 break;
1341 case PACKET_UNKNOWN:
23860348 1342 /* The stub does not support the packet. */
4082afcc
PA
1343 if (config->detect == AUTO_BOOLEAN_AUTO
1344 && config->support == PACKET_ENABLE)
d471ea57 1345 {
4082afcc
PA
1346 /* If the stub previously indicated that the packet was
1347 supported then there is a protocol error. */
1348 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1349 config->name, config->title);
1350 }
1351 else if (config->detect == AUTO_BOOLEAN_TRUE)
1352 {
1353 /* The user set it wrong. */
1354 error (_("Enabled packet %s (%s) not recognized by stub"),
1355 config->name, config->title);
d471ea57 1356 }
4082afcc
PA
1357
1358 if (remote_debug)
1359 fprintf_unfiltered (gdb_stdlog,
1360 "Packet %s (%s) is NOT supported\n",
1361 config->name, config->title);
1362 config->support = PACKET_DISABLE;
a76d924d 1363 break;
5a2468f5 1364 }
a76d924d
DJ
1365
1366 return result;
5a2468f5
JM
1367}
1368
444abaca
DJ
1369enum {
1370 PACKET_vCont = 0,
1371 PACKET_X,
1372 PACKET_qSymbol,
1373 PACKET_P,
1374 PACKET_p,
1375 PACKET_Z0,
1376 PACKET_Z1,
1377 PACKET_Z2,
1378 PACKET_Z3,
1379 PACKET_Z4,
15a201c8 1380 PACKET_vFile_setfs,
a6b151f1
DJ
1381 PACKET_vFile_open,
1382 PACKET_vFile_pread,
1383 PACKET_vFile_pwrite,
1384 PACKET_vFile_close,
1385 PACKET_vFile_unlink,
b9e7b9c3 1386 PACKET_vFile_readlink,
0a93529c 1387 PACKET_vFile_fstat,
0876f84a 1388 PACKET_qXfer_auxv,
23181151 1389 PACKET_qXfer_features,
c78fa86a 1390 PACKET_qXfer_exec_file,
cfa9d6d9 1391 PACKET_qXfer_libraries,
2268b414 1392 PACKET_qXfer_libraries_svr4,
fd79ecee 1393 PACKET_qXfer_memory_map,
0e7f50da
UW
1394 PACKET_qXfer_spu_read,
1395 PACKET_qXfer_spu_write,
07e059b5 1396 PACKET_qXfer_osdata,
dc146f7c 1397 PACKET_qXfer_threads,
0fb4aa4b 1398 PACKET_qXfer_statictrace_read,
b3b9301e 1399 PACKET_qXfer_traceframe_info,
169081d0 1400 PACKET_qXfer_uib,
711e434b 1401 PACKET_qGetTIBAddr,
444abaca 1402 PACKET_qGetTLSAddr,
be2a5f71 1403 PACKET_qSupported,
bd3eecc3 1404 PACKET_qTStatus,
89be2091 1405 PACKET_QPassSignals,
82075af2 1406 PACKET_QCatchSyscalls,
9b224c5e 1407 PACKET_QProgramSignals,
936d2992 1408 PACKET_qCRC,
08388c79 1409 PACKET_qSearch_memory,
2d717e4f
DJ
1410 PACKET_vAttach,
1411 PACKET_vRun,
a6f3e723 1412 PACKET_QStartNoAckMode,
82f73884 1413 PACKET_vKill,
4aa995e1
PA
1414 PACKET_qXfer_siginfo_read,
1415 PACKET_qXfer_siginfo_write,
0b16c5cf 1416 PACKET_qAttached,
4082afcc
PA
1417
1418 /* Support for conditional tracepoints. */
782b2b07 1419 PACKET_ConditionalTracepoints,
4082afcc
PA
1420
1421 /* Support for target-side breakpoint conditions. */
3788aec7 1422 PACKET_ConditionalBreakpoints,
4082afcc
PA
1423
1424 /* Support for target-side breakpoint commands. */
d3ce09f5 1425 PACKET_BreakpointCommands,
4082afcc
PA
1426
1427 /* Support for fast tracepoints. */
7a697b8d 1428 PACKET_FastTracepoints,
4082afcc
PA
1429
1430 /* Support for static tracepoints. */
0fb4aa4b 1431 PACKET_StaticTracepoints,
4082afcc
PA
1432
1433 /* Support for installing tracepoints while a trace experiment is
1434 running. */
1e4d1764 1435 PACKET_InstallInTrace,
4082afcc 1436
40ab02ce
MS
1437 PACKET_bc,
1438 PACKET_bs,
409873ef 1439 PACKET_TracepointSource,
d914c394 1440 PACKET_QAllow,
78d85199 1441 PACKET_qXfer_fdpic,
03583c20 1442 PACKET_QDisableRandomization,
d1feda86 1443 PACKET_QAgent,
f6f899bf 1444 PACKET_QTBuffer_size,
9accd112
MM
1445 PACKET_Qbtrace_off,
1446 PACKET_Qbtrace_bts,
b20a6524 1447 PACKET_Qbtrace_pt,
9accd112 1448 PACKET_qXfer_btrace,
4082afcc
PA
1449
1450 /* Support for the QNonStop packet. */
1451 PACKET_QNonStop,
1452
65706a29
PA
1453 /* Support for the QThreadEvents packet. */
1454 PACKET_QThreadEvents,
1455
4082afcc
PA
1456 /* Support for multi-process extensions. */
1457 PACKET_multiprocess_feature,
1458
1459 /* Support for enabling and disabling tracepoints while a trace
1460 experiment is running. */
1461 PACKET_EnableDisableTracepoints_feature,
1462
1463 /* Support for collecting strings using the tracenz bytecode. */
1464 PACKET_tracenz_feature,
1465
1466 /* Support for continuing to run a trace experiment while GDB is
1467 disconnected. */
1468 PACKET_DisconnectedTracing_feature,
1469
1470 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1471 PACKET_augmented_libraries_svr4_read_feature,
1472
f4abbc16
MM
1473 /* Support for the qXfer:btrace-conf:read packet. */
1474 PACKET_qXfer_btrace_conf,
1475
d33501a5
MM
1476 /* Support for the Qbtrace-conf:bts:size packet. */
1477 PACKET_Qbtrace_conf_bts_size,
1478
f7e6eed5
PA
1479 /* Support for swbreak+ feature. */
1480 PACKET_swbreak_feature,
1481
1482 /* Support for hwbreak+ feature. */
1483 PACKET_hwbreak_feature,
1484
89245bc0
DB
1485 /* Support for fork events. */
1486 PACKET_fork_event_feature,
1487
1488 /* Support for vfork events. */
1489 PACKET_vfork_event_feature,
1490
b20a6524
MM
1491 /* Support for the Qbtrace-conf:pt:size packet. */
1492 PACKET_Qbtrace_conf_pt_size,
1493
94585166
DB
1494 /* Support for exec events. */
1495 PACKET_exec_event_feature,
1496
750ce8d1
YQ
1497 /* Support for query supported vCont actions. */
1498 PACKET_vContSupported,
1499
de979965
PA
1500 /* Support remote CTRL-C. */
1501 PACKET_vCtrlC,
1502
f2faf941
PA
1503 /* Support TARGET_WAITKIND_NO_RESUMED. */
1504 PACKET_no_resumed,
1505
444abaca
DJ
1506 PACKET_MAX
1507};
506fb367 1508
444abaca 1509static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1510
f7e6eed5
PA
1511/* Returns the packet's corresponding "set remote foo-packet" command
1512 state. See struct packet_config for more details. */
1513
1514static enum auto_boolean
1515packet_set_cmd_state (int packet)
1516{
1517 return remote_protocol_packets[packet].detect;
1518}
1519
4082afcc
PA
1520/* Returns whether a given packet or feature is supported. This takes
1521 into account the state of the corresponding "set remote foo-packet"
1522 command, which may be used to bypass auto-detection. */
dc8acb97 1523
4082afcc
PA
1524static enum packet_support
1525packet_config_support (struct packet_config *config)
1526{
1527 switch (config->detect)
444abaca 1528 {
4082afcc
PA
1529 case AUTO_BOOLEAN_TRUE:
1530 return PACKET_ENABLE;
1531 case AUTO_BOOLEAN_FALSE:
1532 return PACKET_DISABLE;
1533 case AUTO_BOOLEAN_AUTO:
1534 return config->support;
1535 default:
1536 gdb_assert_not_reached (_("bad switch"));
444abaca 1537 }
4082afcc
PA
1538}
1539
1540/* Same as packet_config_support, but takes the packet's enum value as
1541 argument. */
1542
1543static enum packet_support
1544packet_support (int packet)
1545{
1546 struct packet_config *config = &remote_protocol_packets[packet];
1547
1548 return packet_config_support (config);
dc8acb97
MS
1549}
1550
5a2468f5 1551static void
444abaca
DJ
1552show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1553 struct cmd_list_element *c,
1554 const char *value)
5a2468f5 1555{
444abaca 1556 struct packet_config *packet;
5a2468f5 1557
444abaca
DJ
1558 for (packet = remote_protocol_packets;
1559 packet < &remote_protocol_packets[PACKET_MAX];
1560 packet++)
1561 {
1562 if (&packet->detect == c->var)
1563 {
1564 show_packet_config_cmd (packet);
1565 return;
1566 }
1567 }
9b20d036 1568 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1569 c->name);
5a2468f5
JM
1570}
1571
d471ea57
AC
1572/* Should we try one of the 'Z' requests? */
1573
1574enum Z_packet_type
1575{
1576 Z_PACKET_SOFTWARE_BP,
1577 Z_PACKET_HARDWARE_BP,
1578 Z_PACKET_WRITE_WP,
1579 Z_PACKET_READ_WP,
1580 Z_PACKET_ACCESS_WP,
1581 NR_Z_PACKET_TYPES
1582};
96baa820 1583
d471ea57 1584/* For compatibility with older distributions. Provide a ``set remote
23860348 1585 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1586
7f19b9a2 1587static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1588
1589static void
fba45db2
KB
1590set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1591 struct cmd_list_element *c)
96baa820 1592{
d471ea57 1593 int i;
a744cf53 1594
d471ea57 1595 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1596 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1597}
1598
1599static void
08546159
AC
1600show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1601 struct cmd_list_element *c,
1602 const char *value)
96baa820 1603{
d471ea57 1604 int i;
a744cf53 1605
d471ea57
AC
1606 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1607 {
444abaca 1608 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1609 }
96baa820
JM
1610}
1611
4082afcc
PA
1612/* Returns true if the multi-process extensions are in effect. */
1613
1614static int
1615remote_multi_process_p (struct remote_state *rs)
1616{
1617 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1618}
1619
de0d863e
DB
1620/* Returns true if fork events are supported. */
1621
1622static int
1623remote_fork_event_p (struct remote_state *rs)
1624{
1625 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1626}
1627
c269dbdb
DB
1628/* Returns true if vfork events are supported. */
1629
1630static int
1631remote_vfork_event_p (struct remote_state *rs)
1632{
1633 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1634}
1635
d46addbb
DB
1636/* Returns true if exec events are supported. */
1637
1638static int
1639remote_exec_event_p (struct remote_state *rs)
1640{
1641 return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
1642}
1643
cbb8991c
DB
1644/* Insert fork catchpoint target routine. If fork events are enabled
1645 then return success, nothing more to do. */
1646
1647static int
1648remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1649{
1650 struct remote_state *rs = get_remote_state ();
1651
1652 return !remote_fork_event_p (rs);
1653}
1654
1655/* Remove fork catchpoint target routine. Nothing to do, just
1656 return success. */
1657
1658static int
1659remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1660{
1661 return 0;
1662}
1663
1664/* Insert vfork catchpoint target routine. If vfork events are enabled
1665 then return success, nothing more to do. */
1666
1667static int
1668remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1669{
1670 struct remote_state *rs = get_remote_state ();
1671
1672 return !remote_vfork_event_p (rs);
1673}
1674
1675/* Remove vfork catchpoint target routine. Nothing to do, just
1676 return success. */
1677
1678static int
1679remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1680{
1681 return 0;
1682}
1683
d46addbb
DB
1684/* Insert exec catchpoint target routine. If exec events are
1685 enabled, just return success. */
1686
1687static int
1688remote_insert_exec_catchpoint (struct target_ops *ops, int pid)
1689{
1690 struct remote_state *rs = get_remote_state ();
1691
1692 return !remote_exec_event_p (rs);
1693}
1694
1695/* Remove exec catchpoint target routine. Nothing to do, just
1696 return success. */
1697
1698static int
1699remote_remove_exec_catchpoint (struct target_ops *ops, int pid)
1700{
1701 return 0;
1702}
1703
74531fed
PA
1704\f
1705/* Asynchronous signal handle registered as event loop source for
1706 when we have pending events ready to be passed to the core. */
1707
1708static struct async_event_handler *remote_async_inferior_event_token;
1709
c906108c
SS
1710\f
1711
79d7f229
PA
1712static ptid_t magic_null_ptid;
1713static ptid_t not_sent_ptid;
1714static ptid_t any_thread_ptid;
1715
0b16c5cf
PA
1716/* Find out if the stub attached to PID (and hence GDB should offer to
1717 detach instead of killing it when bailing out). */
1718
1719static int
1720remote_query_attached (int pid)
1721{
1722 struct remote_state *rs = get_remote_state ();
bba74b36 1723 size_t size = get_remote_packet_size ();
0b16c5cf 1724
4082afcc 1725 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1726 return 0;
1727
1728 if (remote_multi_process_p (rs))
bba74b36 1729 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1730 else
bba74b36 1731 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1732
1733 putpkt (rs->buf);
1734 getpkt (&rs->buf, &rs->buf_size, 0);
1735
1736 switch (packet_ok (rs->buf,
1554e9be 1737 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1738 {
1739 case PACKET_OK:
1740 if (strcmp (rs->buf, "1") == 0)
1741 return 1;
1742 break;
1743 case PACKET_ERROR:
1744 warning (_("Remote failure reply: %s"), rs->buf);
1745 break;
1746 case PACKET_UNKNOWN:
1747 break;
1748 }
1749
1750 return 0;
1751}
1752
49c62f2e
PA
1753/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1754 has been invented by GDB, instead of reported by the target. Since
1755 we can be connected to a remote system before before knowing about
1756 any inferior, mark the target with execution when we find the first
1757 inferior. If ATTACHED is 1, then we had just attached to this
1758 inferior. If it is 0, then we just created this inferior. If it
1759 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1760 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1761 attempt to open this inferior's executable as the main executable
1762 if no main executable is open already. */
1941c569
PA
1763
1764static struct inferior *
1b6e6f5c
GB
1765remote_add_inferior (int fake_pid_p, int pid, int attached,
1766 int try_open_exec)
1941c569 1767{
1941c569
PA
1768 struct inferior *inf;
1769
0b16c5cf
PA
1770 /* Check whether this process we're learning about is to be
1771 considered attached, or if is to be considered to have been
1772 spawned by the stub. */
1773 if (attached == -1)
1774 attached = remote_query_attached (pid);
1775
f5656ead 1776 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1777 {
1778 /* If the target shares code across all inferiors, then every
1779 attach adds a new inferior. */
1780 inf = add_inferior (pid);
1781
1782 /* ... and every inferior is bound to the same program space.
1783 However, each inferior may still have its own address
1784 space. */
1785 inf->aspace = maybe_new_address_space ();
1786 inf->pspace = current_program_space;
1787 }
1788 else
1789 {
1790 /* In the traditional debugging scenario, there's a 1-1 match
1791 between program/address spaces. We simply bind the inferior
1792 to the program space's address space. */
1793 inf = current_inferior ();
1794 inferior_appeared (inf, pid);
1795 }
1941c569 1796
0b16c5cf 1797 inf->attach_flag = attached;
49c62f2e 1798 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1799
1b6e6f5c
GB
1800 /* If no main executable is currently open then attempt to
1801 open the file that was executed to create this inferior. */
835205d0 1802 if (try_open_exec && get_exec_file (0) == NULL)
bb805577 1803 exec_file_locate_attach (pid, 0, 1);
1b6e6f5c 1804
1941c569
PA
1805 return inf;
1806}
1807
1808/* Add thread PTID to GDB's thread list. Tag it as executing/running
1809 according to RUNNING. */
1810
c906108c 1811static void
0d5b594f 1812remote_add_thread (ptid_t ptid, int running, int executing)
c906108c 1813{
b7ea362b
PA
1814 struct remote_state *rs = get_remote_state ();
1815
1816 /* GDB historically didn't pull threads in the initial connection
1817 setup. If the remote target doesn't even have a concept of
1818 threads (e.g., a bare-metal target), even if internally we
1819 consider that a single-threaded target, mentioning a new thread
1820 might be confusing to the user. Be silent then, preserving the
1821 age old behavior. */
1822 if (rs->starting_up)
1823 add_thread_silent (ptid);
1824 else
1825 add_thread (ptid);
1941c569 1826
0d5b594f 1827 set_executing (ptid, executing);
1941c569
PA
1828 set_running (ptid, running);
1829}
1830
1831/* Come here when we learn about a thread id from the remote target.
1832 It may be the first time we hear about such thread, so take the
1833 opportunity to add it to GDB's thread list. In case this is the
1834 first time we're noticing its corresponding inferior, add it to
0d5b594f
PA
1835 GDB's inferior list as well. EXECUTING indicates whether the
1836 thread is (internally) executing or stopped. */
1941c569
PA
1837
1838static void
0d5b594f 1839remote_notice_new_inferior (ptid_t currthread, int executing)
1941c569 1840{
0d5b594f
PA
1841 /* In non-stop mode, we assume new found threads are (externally)
1842 running until proven otherwise with a stop reply. In all-stop,
1843 we can only get here if all threads are stopped. */
1844 int running = target_is_non_stop_p () ? 1 : 0;
1845
c906108c
SS
1846 /* If this is a new thread, add it to GDB's thread list.
1847 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1848
1849 if (in_thread_list (currthread) && is_exited (currthread))
1850 {
1851 /* We're seeing an event on a thread id we knew had exited.
1852 This has to be a new thread reusing the old id. Add it. */
0d5b594f 1853 remote_add_thread (currthread, running, executing);
82f73884
PA
1854 return;
1855 }
1856
79d7f229 1857 if (!in_thread_list (currthread))
c0a2216e 1858 {
1941c569 1859 struct inferior *inf = NULL;
bad34192 1860 int pid = ptid_get_pid (currthread);
1941c569 1861
bad34192
PA
1862 if (ptid_is_pid (inferior_ptid)
1863 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1864 {
1865 /* inferior_ptid has no thread member yet. This can happen
1866 with the vAttach -> remote_wait,"TAAthread:" path if the
1867 stub doesn't support qC. This is the first stop reported
1868 after an attach, so this is the main thread. Update the
1869 ptid in the thread list. */
bad34192
PA
1870 if (in_thread_list (pid_to_ptid (pid)))
1871 thread_change_ptid (inferior_ptid, currthread);
1872 else
1873 {
0d5b594f 1874 remote_add_thread (currthread, running, executing);
bad34192
PA
1875 inferior_ptid = currthread;
1876 }
dc146f7c 1877 return;
c0a2216e 1878 }
82f73884
PA
1879
1880 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1881 {
1882 /* inferior_ptid is not set yet. This can happen with the
1883 vRun -> remote_wait,"TAAthread:" path if the stub
1884 doesn't support qC. This is the first stop reported
1885 after an attach, so this is the main thread. Update the
1886 ptid in the thread list. */
dc146f7c 1887 thread_change_ptid (inferior_ptid, currthread);
82f73884 1888 return;
c0a2216e 1889 }
82f73884 1890
29c87f7f
PA
1891 /* When connecting to a target remote, or to a target
1892 extended-remote which already was debugging an inferior, we
1893 may not know about it yet. Add it before adding its child
1894 thread, so notifications are emitted in a sensible order. */
1895 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1896 {
1897 struct remote_state *rs = get_remote_state ();
1898 int fake_pid_p = !remote_multi_process_p (rs);
1899
1900 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1901 ptid_get_pid (currthread), -1, 1);
49c62f2e 1902 }
29c87f7f 1903
82f73884 1904 /* This is really a new thread. Add it. */
0d5b594f 1905 remote_add_thread (currthread, running, executing);
1941c569
PA
1906
1907 /* If we found a new inferior, let the common code do whatever
1908 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1909 breakpoints), unless we're just setting up an all-stop
1910 connection. */
1941c569 1911 if (inf != NULL)
b7ea362b
PA
1912 {
1913 struct remote_state *rs = get_remote_state ();
1914
6efcd9a8 1915 if (!rs->starting_up)
0d5b594f 1916 notice_new_inferior (currthread, executing, 0);
b7ea362b 1917 }
c0a2216e 1918 }
c906108c
SS
1919}
1920
dc146f7c
VP
1921/* Return the private thread data, creating it if necessary. */
1922
70221824 1923static struct private_thread_info *
dc146f7c
VP
1924demand_private_info (ptid_t ptid)
1925{
1926 struct thread_info *info = find_thread_ptid (ptid);
1927
1928 gdb_assert (info);
1929
fe978cb0 1930 if (!info->priv)
dc146f7c 1931 {
8d749320 1932 info->priv = XNEW (struct private_thread_info);
dc146f7c 1933 info->private_dtor = free_private_thread_info;
fe978cb0 1934 info->priv->core = -1;
8020350c
DB
1935 info->priv->extra = NULL;
1936 info->priv->name = NULL;
dc146f7c
VP
1937 }
1938
fe978cb0 1939 return info->priv;
dc146f7c
VP
1940}
1941
74531fed
PA
1942/* Call this function as a result of
1943 1) A halt indication (T packet) containing a thread id
1944 2) A direct query of currthread
0df8b418 1945 3) Successful execution of set thread */
74531fed
PA
1946
1947static void
47f8a51d 1948record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1949{
47f8a51d 1950 rs->general_thread = currthread;
74531fed
PA
1951}
1952
89be2091
DJ
1953/* If 'QPassSignals' is supported, tell the remote stub what signals
1954 it can simply pass through to the inferior without reporting. */
1955
1956static void
94bedb42
TT
1957remote_pass_signals (struct target_ops *self,
1958 int numsigs, unsigned char *pass_signals)
89be2091 1959{
4082afcc 1960 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1961 {
1962 char *pass_packet, *p;
89be2091 1963 int count = 0, i;
747dc59d 1964 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1965
1966 gdb_assert (numsigs < 256);
1967 for (i = 0; i < numsigs; i++)
1968 {
2455069d 1969 if (pass_signals[i])
89be2091
DJ
1970 count++;
1971 }
224c3ddb 1972 pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
89be2091
DJ
1973 strcpy (pass_packet, "QPassSignals:");
1974 p = pass_packet + strlen (pass_packet);
1975 for (i = 0; i < numsigs; i++)
1976 {
2455069d 1977 if (pass_signals[i])
89be2091
DJ
1978 {
1979 if (i >= 16)
1980 *p++ = tohex (i >> 4);
1981 *p++ = tohex (i & 15);
1982 if (count)
1983 *p++ = ';';
1984 else
1985 break;
1986 count--;
1987 }
1988 }
1989 *p = 0;
747dc59d 1990 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1991 {
89be2091
DJ
1992 putpkt (pass_packet);
1993 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1994 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1995 if (rs->last_pass_packet)
1996 xfree (rs->last_pass_packet);
1997 rs->last_pass_packet = pass_packet;
89be2091
DJ
1998 }
1999 else
2000 xfree (pass_packet);
2001 }
2002}
2003
82075af2
JS
2004/* If 'QCatchSyscalls' is supported, tell the remote stub
2005 to report syscalls to GDB. */
2006
2007static int
2008remote_set_syscall_catchpoint (struct target_ops *self,
2009 int pid, int needed, int any_count,
2010 int table_size, int *table)
2011{
2012 char *catch_packet;
2013 enum packet_result result;
2014 int n_sysno = 0;
2015
2016 if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
2017 {
2018 /* Not supported. */
2019 return 1;
2020 }
2021
2022 if (needed && !any_count)
2023 {
2024 int i;
2025
2026 /* Count how many syscalls are to be caught (table[sysno] != 0). */
2027 for (i = 0; i < table_size; i++)
2028 {
2029 if (table[i] != 0)
2030 n_sysno++;
2031 }
2032 }
2033
2034 if (remote_debug)
2035 {
2036 fprintf_unfiltered (gdb_stdlog,
2037 "remote_set_syscall_catchpoint "
2038 "pid %d needed %d any_count %d n_sysno %d\n",
2039 pid, needed, any_count, n_sysno);
2040 }
2041
2042 if (needed)
2043 {
2044 /* Prepare a packet with the sysno list, assuming max 8+1
2045 characters for a sysno. If the resulting packet size is too
2046 big, fallback on the non-selective packet. */
2047 const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
2048
c0518081 2049 catch_packet = (char *) xmalloc (maxpktsz);
82075af2
JS
2050 strcpy (catch_packet, "QCatchSyscalls:1");
2051 if (!any_count)
2052 {
2053 int i;
2054 char *p;
2055
2056 p = catch_packet;
2057 p += strlen (p);
2058
2059 /* Add in catch_packet each syscall to be caught (table[i] != 0). */
2060 for (i = 0; i < table_size; i++)
2061 {
2062 if (table[i] != 0)
2063 p += xsnprintf (p, catch_packet + maxpktsz - p, ";%x", i);
2064 }
2065 }
2066 if (strlen (catch_packet) > get_remote_packet_size ())
2067 {
2068 /* catch_packet too big. Fallback to less efficient
2069 non selective mode, with GDB doing the filtering. */
2070 catch_packet[sizeof ("QCatchSyscalls:1") - 1] = 0;
2071 }
2072 }
2073 else
2074 catch_packet = xstrdup ("QCatchSyscalls:0");
2075
2076 {
2077 struct cleanup *old_chain = make_cleanup (xfree, catch_packet);
2078 struct remote_state *rs = get_remote_state ();
2079
2080 putpkt (catch_packet);
2081 getpkt (&rs->buf, &rs->buf_size, 0);
2082 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
2083 do_cleanups (old_chain);
2084 if (result == PACKET_OK)
2085 return 0;
2086 else
2087 return -1;
2088 }
2089}
2090
9b224c5e
PA
2091/* If 'QProgramSignals' is supported, tell the remote stub what
2092 signals it should pass through to the inferior when detaching. */
2093
2094static void
daf5e9b6
TT
2095remote_program_signals (struct target_ops *self,
2096 int numsigs, unsigned char *signals)
9b224c5e 2097{
4082afcc 2098 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
2099 {
2100 char *packet, *p;
2101 int count = 0, i;
5e4a05c4 2102 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
2103
2104 gdb_assert (numsigs < 256);
2105 for (i = 0; i < numsigs; i++)
2106 {
2107 if (signals[i])
2108 count++;
2109 }
224c3ddb 2110 packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
9b224c5e
PA
2111 strcpy (packet, "QProgramSignals:");
2112 p = packet + strlen (packet);
2113 for (i = 0; i < numsigs; i++)
2114 {
2115 if (signal_pass_state (i))
2116 {
2117 if (i >= 16)
2118 *p++ = tohex (i >> 4);
2119 *p++ = tohex (i & 15);
2120 if (count)
2121 *p++ = ';';
2122 else
2123 break;
2124 count--;
2125 }
2126 }
2127 *p = 0;
5e4a05c4
TT
2128 if (!rs->last_program_signals_packet
2129 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 2130 {
9b224c5e
PA
2131 putpkt (packet);
2132 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 2133 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
2134 xfree (rs->last_program_signals_packet);
2135 rs->last_program_signals_packet = packet;
9b224c5e
PA
2136 }
2137 else
2138 xfree (packet);
2139 }
2140}
2141
79d7f229
PA
2142/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2143 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2144 thread. If GEN is set, set the general thread, if not, then set
2145 the step/continue thread. */
c906108c 2146static void
79d7f229 2147set_thread (struct ptid ptid, int gen)
c906108c 2148{
d01949b6 2149 struct remote_state *rs = get_remote_state ();
47f8a51d 2150 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 2151 char *buf = rs->buf;
79d7f229 2152 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 2153
79d7f229 2154 if (ptid_equal (state, ptid))
c906108c
SS
2155 return;
2156
79d7f229
PA
2157 *buf++ = 'H';
2158 *buf++ = gen ? 'g' : 'c';
2159 if (ptid_equal (ptid, magic_null_ptid))
2160 xsnprintf (buf, endbuf - buf, "0");
2161 else if (ptid_equal (ptid, any_thread_ptid))
2162 xsnprintf (buf, endbuf - buf, "0");
2163 else if (ptid_equal (ptid, minus_one_ptid))
2164 xsnprintf (buf, endbuf - buf, "-1");
2165 else
82f73884 2166 write_ptid (buf, endbuf, ptid);
79d7f229 2167 putpkt (rs->buf);
6d820c5c 2168 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2169 if (gen)
47f8a51d 2170 rs->general_thread = ptid;
c906108c 2171 else
47f8a51d 2172 rs->continue_thread = ptid;
c906108c 2173}
79d7f229
PA
2174
2175static void
2176set_general_thread (struct ptid ptid)
2177{
2178 set_thread (ptid, 1);
2179}
2180
2181static void
2182set_continue_thread (struct ptid ptid)
2183{
2184 set_thread (ptid, 0);
2185}
2186
3c9c4b83
PA
2187/* Change the remote current process. Which thread within the process
2188 ends up selected isn't important, as long as it is the same process
2189 as what INFERIOR_PTID points to.
2190
2191 This comes from that fact that there is no explicit notion of
2192 "selected process" in the protocol. The selected process for
2193 general operations is the process the selected general thread
2194 belongs to. */
2195
2196static void
2197set_general_process (void)
2198{
2199 struct remote_state *rs = get_remote_state ();
2200
2201 /* If the remote can't handle multiple processes, don't bother. */
8020350c 2202 if (!remote_multi_process_p (rs))
3c9c4b83
PA
2203 return;
2204
2205 /* We only need to change the remote current thread if it's pointing
2206 at some other process. */
47f8a51d 2207 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2208 set_general_thread (inferior_ptid);
2209}
2210
c906108c 2211\f
7d1a114c
PA
2212/* Return nonzero if this is the main thread that we made up ourselves
2213 to model non-threaded targets as single-threaded. */
c906108c
SS
2214
2215static int
7d1a114c 2216remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 2217{
c0a2216e
PA
2218 if (ptid_equal (ptid, magic_null_ptid))
2219 /* The main thread is always alive. */
2220 return 1;
2221
ba348170 2222 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2223 /* The main thread is always alive. This can happen after a
2224 vAttach, if the remote side doesn't support
2225 multi-threading. */
2226 return 1;
2227
7d1a114c
PA
2228 return 0;
2229}
2230
2231/* Return nonzero if the thread PTID is still alive on the remote
2232 system. */
2233
2234static int
2235remote_thread_alive (struct target_ops *ops, ptid_t ptid)
2236{
2237 struct remote_state *rs = get_remote_state ();
2238 char *p, *endp;
2239
2240 /* Check if this is a thread that we made up ourselves to model
2241 non-threaded targets as single-threaded. */
2242 if (remote_thread_always_alive (ops, ptid))
2243 return 1;
2244
82f73884
PA
2245 p = rs->buf;
2246 endp = rs->buf + get_remote_packet_size ();
2247
2248 *p++ = 'T';
2249 write_ptid (p, endp, ptid);
2250
2e9f7625 2251 putpkt (rs->buf);
6d820c5c 2252 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2253 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2254}
2255
79efa585
SM
2256/* Return a pointer to a thread name if we know it and NULL otherwise.
2257 The thread_info object owns the memory for the name. */
2258
2259static const char *
2260remote_thread_name (struct target_ops *ops, struct thread_info *info)
2261{
2262 if (info->priv != NULL)
2263 return info->priv->name;
2264
2265 return NULL;
2266}
2267
c906108c
SS
2268/* About these extended threadlist and threadinfo packets. They are
2269 variable length packets but, the fields within them are often fixed
2270 length. They are redundent enough to send over UDP as is the
2271 remote protocol in general. There is a matching unit test module
2272 in libstub. */
2273
23860348 2274/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2275 libstub protocol encoding, and remote.c. It is not particularly
23860348 2276 changable. */
cce74817
JM
2277
2278/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2279 Plan to fix this. */
cce74817 2280
23860348 2281typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2282
9d1f7ab2 2283/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2284 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2285
2286struct gdb_ext_thread_info
c5aa993b 2287 {
23860348 2288 threadref threadid; /* External form of thread reference. */
2bc416ba 2289 int active; /* Has state interesting to GDB?
23860348 2290 regs, stack. */
2bc416ba 2291 char display[256]; /* Brief state display, name,
cedea757 2292 blocked/suspended. */
23860348 2293 char shortname[32]; /* To be used to name threads. */
2bc416ba 2294 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2295 whatever. */
c5aa993b 2296 };
cce74817
JM
2297
2298/* The volume of remote transfers can be limited by submitting
2299 a mask containing bits specifying the desired information.
2300 Use a union of these values as the 'selection' parameter to
0df8b418 2301 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2302
2303#define TAG_THREADID 1
2304#define TAG_EXISTS 2
2305#define TAG_DISPLAY 4
2306#define TAG_THREADNAME 8
c5aa993b 2307#define TAG_MOREDISPLAY 16
cce74817 2308
23860348 2309#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2310
a14ed312 2311static char *unpack_nibble (char *buf, int *val);
cce74817 2312
a14ed312 2313static char *unpack_byte (char *buf, int *value);
cce74817 2314
a14ed312 2315static char *pack_int (char *buf, int value);
cce74817 2316
a14ed312 2317static char *unpack_int (char *buf, int *value);
cce74817 2318
a14ed312 2319static char *unpack_string (char *src, char *dest, int length);
cce74817 2320
23860348 2321static char *pack_threadid (char *pkt, threadref *id);
cce74817 2322
23860348 2323static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2324
23860348 2325void int_to_threadref (threadref *id, int value);
cce74817 2326
23860348 2327static int threadref_to_int (threadref *ref);
cce74817 2328
23860348 2329static void copy_threadref (threadref *dest, threadref *src);
cce74817 2330
23860348 2331static int threadmatch (threadref *dest, threadref *src);
cce74817 2332
2bc416ba 2333static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2334 threadref *id);
cce74817 2335
a14ed312 2336static int remote_unpack_thread_info_response (char *pkt,
23860348 2337 threadref *expectedref,
a14ed312
KB
2338 struct gdb_ext_thread_info
2339 *info);
cce74817
JM
2340
2341
2bc416ba 2342static int remote_get_threadinfo (threadref *threadid,
23860348 2343 int fieldset, /*TAG mask */
a14ed312 2344 struct gdb_ext_thread_info *info);
cce74817 2345
a14ed312
KB
2346static char *pack_threadlist_request (char *pkt, int startflag,
2347 int threadcount,
23860348 2348 threadref *nextthread);
cce74817 2349
a14ed312
KB
2350static int parse_threadlist_response (char *pkt,
2351 int result_limit,
23860348 2352 threadref *original_echo,
2bc416ba 2353 threadref *resultlist,
23860348 2354 int *doneflag);
cce74817 2355
a14ed312 2356static int remote_get_threadlist (int startflag,
23860348 2357 threadref *nextthread,
a14ed312
KB
2358 int result_limit,
2359 int *done,
2bc416ba 2360 int *result_count,
23860348 2361 threadref *threadlist);
cce74817 2362
23860348 2363typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2364
a14ed312
KB
2365static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2366 void *context, int looplimit);
cce74817 2367
23860348 2368static int remote_newthread_step (threadref *ref, void *context);
cce74817 2369
82f73884
PA
2370
2371/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2372 buffer we're allowed to write to. Returns
2373 BUF+CHARACTERS_WRITTEN. */
2374
2375static char *
2376write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2377{
2378 int pid, tid;
2379 struct remote_state *rs = get_remote_state ();
2380
2381 if (remote_multi_process_p (rs))
2382 {
2383 pid = ptid_get_pid (ptid);
2384 if (pid < 0)
2385 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2386 else
2387 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2388 }
ba348170 2389 tid = ptid_get_lwp (ptid);
82f73884
PA
2390 if (tid < 0)
2391 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2392 else
2393 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2394
2395 return buf;
2396}
2397
2398/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2399 passed the last parsed char. Returns null_ptid on error. */
2400
2401static ptid_t
2402read_ptid (char *buf, char **obuf)
2403{
2404 char *p = buf;
2405 char *pp;
2406 ULONGEST pid = 0, tid = 0;
82f73884
PA
2407
2408 if (*p == 'p')
2409 {
2410 /* Multi-process ptid. */
2411 pp = unpack_varlen_hex (p + 1, &pid);
2412 if (*pp != '.')
b37520b6 2413 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2414
2415 p = pp;
2416 pp = unpack_varlen_hex (p + 1, &tid);
2417 if (obuf)
2418 *obuf = pp;
ba348170 2419 return ptid_build (pid, tid, 0);
82f73884
PA
2420 }
2421
2422 /* No multi-process. Just a tid. */
2423 pp = unpack_varlen_hex (p, &tid);
2424
c9f35b34
KB
2425 /* Return null_ptid when no thread id is found. */
2426 if (p == pp)
2427 {
2428 if (obuf)
2429 *obuf = pp;
2430 return null_ptid;
2431 }
2432
82f73884 2433 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2434 what's in inferior_ptid, unless it's null at this point. If so,
2435 then since there's no way to know the pid of the reported
2436 threads, use the magic number. */
2437 if (ptid_equal (inferior_ptid, null_ptid))
2438 pid = ptid_get_pid (magic_null_ptid);
2439 else
2440 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2441
2442 if (obuf)
2443 *obuf = pp;
ba348170 2444 return ptid_build (pid, tid, 0);
82f73884
PA
2445}
2446
c906108c 2447static int
fba45db2 2448stubhex (int ch)
c906108c
SS
2449{
2450 if (ch >= 'a' && ch <= 'f')
2451 return ch - 'a' + 10;
2452 if (ch >= '0' && ch <= '9')
2453 return ch - '0';
2454 if (ch >= 'A' && ch <= 'F')
2455 return ch - 'A' + 10;
2456 return -1;
2457}
2458
2459static int
fba45db2 2460stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2461{
2462 int nibble;
2463 int retval = 0;
2464
2465 while (fieldlength)
2466 {
2467 nibble = stubhex (*buff++);
2468 retval |= nibble;
2469 fieldlength--;
2470 if (fieldlength)
2471 retval = retval << 4;
2472 }
2473 return retval;
2474}
2475
c906108c 2476static char *
fba45db2 2477unpack_nibble (char *buf, int *val)
c906108c 2478{
b7589f7d 2479 *val = fromhex (*buf++);
c906108c
SS
2480 return buf;
2481}
2482
c906108c 2483static char *
fba45db2 2484unpack_byte (char *buf, int *value)
c906108c
SS
2485{
2486 *value = stub_unpack_int (buf, 2);
2487 return buf + 2;
2488}
2489
2490static char *
fba45db2 2491pack_int (char *buf, int value)
c906108c
SS
2492{
2493 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2494 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2495 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2496 buf = pack_hex_byte (buf, (value & 0xff));
2497 return buf;
2498}
2499
2500static char *
fba45db2 2501unpack_int (char *buf, int *value)
c906108c
SS
2502{
2503 *value = stub_unpack_int (buf, 8);
2504 return buf + 8;
2505}
2506
23860348 2507#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2508static char *pack_string (char *pkt, char *string);
c906108c
SS
2509
2510static char *
fba45db2 2511pack_string (char *pkt, char *string)
c906108c
SS
2512{
2513 char ch;
2514 int len;
2515
2516 len = strlen (string);
2517 if (len > 200)
23860348 2518 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2519 pkt = pack_hex_byte (pkt, len);
2520 while (len-- > 0)
2521 {
2522 ch = *string++;
2523 if ((ch == '\0') || (ch == '#'))
23860348 2524 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2525 *pkt++ = ch;
2526 }
2527 return pkt;
2528}
2529#endif /* 0 (unused) */
2530
2531static char *
fba45db2 2532unpack_string (char *src, char *dest, int length)
c906108c
SS
2533{
2534 while (length--)
2535 *dest++ = *src++;
2536 *dest = '\0';
2537 return src;
2538}
2539
2540static char *
fba45db2 2541pack_threadid (char *pkt, threadref *id)
c906108c
SS
2542{
2543 char *limit;
2544 unsigned char *altid;
2545
2546 altid = (unsigned char *) id;
2547 limit = pkt + BUF_THREAD_ID_SIZE;
2548 while (pkt < limit)
2549 pkt = pack_hex_byte (pkt, *altid++);
2550 return pkt;
2551}
2552
2553
2554static char *
fba45db2 2555unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2556{
2557 char *altref;
2558 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2559 int x, y;
2560
2561 altref = (char *) id;
2562
2563 while (inbuf < limit)
2564 {
2565 x = stubhex (*inbuf++);
2566 y = stubhex (*inbuf++);
2567 *altref++ = (x << 4) | y;
2568 }
2569 return inbuf;
2570}
2571
2572/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2573 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2574 to use 64bit thread references internally. This is an adapter
2575 function. */
2576
2577void
fba45db2 2578int_to_threadref (threadref *id, int value)
c906108c
SS
2579{
2580 unsigned char *scan;
2581
2582 scan = (unsigned char *) id;
2583 {
2584 int i = 4;
2585 while (i--)
2586 *scan++ = 0;
2587 }
2588 *scan++ = (value >> 24) & 0xff;
2589 *scan++ = (value >> 16) & 0xff;
2590 *scan++ = (value >> 8) & 0xff;
2591 *scan++ = (value & 0xff);
2592}
2593
2594static int
fba45db2 2595threadref_to_int (threadref *ref)
c906108c
SS
2596{
2597 int i, value = 0;
2598 unsigned char *scan;
2599
cfd77fa1 2600 scan = *ref;
c906108c
SS
2601 scan += 4;
2602 i = 4;
2603 while (i-- > 0)
2604 value = (value << 8) | ((*scan++) & 0xff);
2605 return value;
2606}
2607
2608static void
fba45db2 2609copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2610{
2611 int i;
2612 unsigned char *csrc, *cdest;
2613
2614 csrc = (unsigned char *) src;
2615 cdest = (unsigned char *) dest;
2616 i = 8;
2617 while (i--)
2618 *cdest++ = *csrc++;
2619}
2620
2621static int
fba45db2 2622threadmatch (threadref *dest, threadref *src)
c906108c 2623{
23860348 2624 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2625#if 0
2626 unsigned char *srcp, *destp;
2627 int i, result;
2628 srcp = (char *) src;
2629 destp = (char *) dest;
2630
2631 result = 1;
2632 while (i-- > 0)
2633 result &= (*srcp++ == *destp++) ? 1 : 0;
2634 return result;
2635#endif
2636 return 1;
2637}
2638
2639/*
c5aa993b
JM
2640 threadid:1, # always request threadid
2641 context_exists:2,
2642 display:4,
2643 unique_name:8,
2644 more_display:16
2645 */
c906108c
SS
2646
2647/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2648
2649static char *
fba45db2 2650pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2651{
23860348
MS
2652 *pkt++ = 'q'; /* Info Query */
2653 *pkt++ = 'P'; /* process or thread info */
2654 pkt = pack_int (pkt, mode); /* mode */
c906108c 2655 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2656 *pkt = '\0'; /* terminate */
c906108c
SS
2657 return pkt;
2658}
2659
23860348 2660/* These values tag the fields in a thread info response packet. */
c906108c 2661/* Tagging the fields allows us to request specific fields and to
23860348 2662 add more fields as time goes by. */
c906108c 2663
23860348 2664#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2665#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2666 fetch registers and its stack? */
c5aa993b 2667#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2668#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2669#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2670 the process. */
c906108c
SS
2671
2672static int
fba45db2
KB
2673remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2674 struct gdb_ext_thread_info *info)
c906108c 2675{
d01949b6 2676 struct remote_state *rs = get_remote_state ();
c906108c 2677 int mask, length;
cfd77fa1 2678 int tag;
c906108c 2679 threadref ref;
6d820c5c 2680 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2681 int retval = 1;
2682
23860348 2683 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2684 info->active = 0;
2685 info->display[0] = '\0';
2686 info->shortname[0] = '\0';
2687 info->more_display[0] = '\0';
2688
23860348
MS
2689 /* Assume the characters indicating the packet type have been
2690 stripped. */
c906108c
SS
2691 pkt = unpack_int (pkt, &mask); /* arg mask */
2692 pkt = unpack_threadid (pkt, &ref);
2693
2694 if (mask == 0)
8a3fe4f8 2695 warning (_("Incomplete response to threadinfo request."));
c906108c 2696 if (!threadmatch (&ref, expectedref))
23860348 2697 { /* This is an answer to a different request. */
8a3fe4f8 2698 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2699 return 0;
2700 }
2701 copy_threadref (&info->threadid, &ref);
2702
23860348 2703 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2704
23860348
MS
2705 /* Packets are terminated with nulls. */
2706 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2707 {
2708 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2709 pkt = unpack_byte (pkt, &length); /* length */
2710 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2711 {
8a3fe4f8 2712 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2713 retval = 0;
2714 break;
2715 }
2716 if (tag == TAG_THREADID)
2717 {
2718 if (length != 16)
2719 {
8a3fe4f8 2720 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2721 retval = 0;
2722 break;
2723 }
2724 pkt = unpack_threadid (pkt, &ref);
2725 mask = mask & ~TAG_THREADID;
2726 continue;
2727 }
2728 if (tag == TAG_EXISTS)
2729 {
2730 info->active = stub_unpack_int (pkt, length);
2731 pkt += length;
2732 mask = mask & ~(TAG_EXISTS);
2733 if (length > 8)
2734 {
8a3fe4f8 2735 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2736 retval = 0;
2737 break;
2738 }
2739 continue;
2740 }
2741 if (tag == TAG_THREADNAME)
2742 {
2743 pkt = unpack_string (pkt, &info->shortname[0], length);
2744 mask = mask & ~TAG_THREADNAME;
2745 continue;
2746 }
2747 if (tag == TAG_DISPLAY)
2748 {
2749 pkt = unpack_string (pkt, &info->display[0], length);
2750 mask = mask & ~TAG_DISPLAY;
2751 continue;
2752 }
2753 if (tag == TAG_MOREDISPLAY)
2754 {
2755 pkt = unpack_string (pkt, &info->more_display[0], length);
2756 mask = mask & ~TAG_MOREDISPLAY;
2757 continue;
2758 }
8a3fe4f8 2759 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2760 break; /* Not a tag we know about. */
c906108c
SS
2761 }
2762 return retval;
2763}
2764
2765static int
fba45db2
KB
2766remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2767 struct gdb_ext_thread_info *info)
c906108c 2768{
d01949b6 2769 struct remote_state *rs = get_remote_state ();
c906108c 2770 int result;
c906108c 2771
2e9f7625
DJ
2772 pack_threadinfo_request (rs->buf, fieldset, threadid);
2773 putpkt (rs->buf);
6d820c5c 2774 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2775
2776 if (rs->buf[0] == '\0')
2777 return 0;
2778
2e9f7625 2779 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2780 threadid, info);
c906108c
SS
2781 return result;
2782}
2783
c906108c
SS
2784/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2785
2786static char *
fba45db2
KB
2787pack_threadlist_request (char *pkt, int startflag, int threadcount,
2788 threadref *nextthread)
c906108c
SS
2789{
2790 *pkt++ = 'q'; /* info query packet */
2791 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2792 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2793 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2794 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2795 *pkt = '\0';
2796 return pkt;
2797}
2798
2799/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2800
2801static int
fba45db2
KB
2802parse_threadlist_response (char *pkt, int result_limit,
2803 threadref *original_echo, threadref *resultlist,
2804 int *doneflag)
c906108c 2805{
d01949b6 2806 struct remote_state *rs = get_remote_state ();
c906108c
SS
2807 char *limit;
2808 int count, resultcount, done;
2809
2810 resultcount = 0;
2811 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2812 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2813 /* done parse past here */
c906108c
SS
2814 pkt = unpack_byte (pkt, &count); /* count field */
2815 pkt = unpack_nibble (pkt, &done);
2816 /* The first threadid is the argument threadid. */
2817 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2818 while ((count-- > 0) && (pkt < limit))
2819 {
2820 pkt = unpack_threadid (pkt, resultlist++);
2821 if (resultcount++ >= result_limit)
2822 break;
2823 }
2824 if (doneflag)
2825 *doneflag = done;
2826 return resultcount;
2827}
2828
6dc54d91
PA
2829/* Fetch the next batch of threads from the remote. Returns -1 if the
2830 qL packet is not supported, 0 on error and 1 on success. */
2831
c906108c 2832static int
fba45db2
KB
2833remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2834 int *done, int *result_count, threadref *threadlist)
c906108c 2835{
d01949b6 2836 struct remote_state *rs = get_remote_state ();
c906108c
SS
2837 int result = 1;
2838
23860348 2839 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2840 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2841 >= get_remote_packet_size ())
ea9c271d 2842 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2843
6d820c5c
DJ
2844 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2845 putpkt (rs->buf);
2846 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2847 if (*rs->buf == '\0')
6dc54d91
PA
2848 {
2849 /* Packet not supported. */
2850 return -1;
2851 }
2852
2853 *result_count =
2854 parse_threadlist_response (rs->buf + 2, result_limit,
2855 &rs->echo_nextthread, threadlist, done);
c906108c 2856
0d031856 2857 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2858 {
23860348
MS
2859 /* FIXME: This is a good reason to drop the packet. */
2860 /* Possably, there is a duplicate response. */
c906108c
SS
2861 /* Possabilities :
2862 retransmit immediatly - race conditions
2863 retransmit after timeout - yes
2864 exit
2865 wait for packet, then exit
2866 */
8a3fe4f8 2867 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2868 return 0; /* I choose simply exiting. */
c906108c
SS
2869 }
2870 if (*result_count <= 0)
2871 {
2872 if (*done != 1)
2873 {
8a3fe4f8 2874 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2875 result = 0;
2876 }
2877 return result; /* break; */
2878 }
2879 if (*result_count > result_limit)
2880 {
2881 *result_count = 0;
8a3fe4f8 2882 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2883 return 0;
2884 }
2885 return result;
2886}
2887
6dc54d91
PA
2888/* Fetch the list of remote threads, with the qL packet, and call
2889 STEPFUNCTION for each thread found. Stops iterating and returns 1
2890 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2891 STEPFUNCTION returns false. If the packet is not supported,
2892 returns -1. */
c906108c 2893
c906108c 2894static int
fba45db2
KB
2895remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2896 int looplimit)
c906108c 2897{
0d031856 2898 struct remote_state *rs = get_remote_state ();
c906108c
SS
2899 int done, i, result_count;
2900 int startflag = 1;
2901 int result = 1;
2902 int loopcount = 0;
c906108c
SS
2903
2904 done = 0;
2905 while (!done)
2906 {
2907 if (loopcount++ > looplimit)
2908 {
2909 result = 0;
8a3fe4f8 2910 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2911 break;
2912 }
6dc54d91
PA
2913 result = remote_get_threadlist (startflag, &rs->nextthread,
2914 MAXTHREADLISTRESULTS,
2915 &done, &result_count,
2916 rs->resultthreadlist);
2917 if (result <= 0)
2918 break;
23860348 2919 /* Clear for later iterations. */
c906108c
SS
2920 startflag = 0;
2921 /* Setup to resume next batch of thread references, set nextthread. */
2922 if (result_count >= 1)
0d031856
TT
2923 copy_threadref (&rs->nextthread,
2924 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2925 i = 0;
2926 while (result_count--)
6dc54d91
PA
2927 {
2928 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2929 {
2930 result = 0;
2931 break;
2932 }
2933 }
c906108c
SS
2934 }
2935 return result;
2936}
2937
6dc54d91
PA
2938/* A thread found on the remote target. */
2939
2940typedef struct thread_item
2941{
2942 /* The thread's PTID. */
2943 ptid_t ptid;
2944
2945 /* The thread's extra info. May be NULL. */
2946 char *extra;
2947
79efa585
SM
2948 /* The thread's name. May be NULL. */
2949 char *name;
2950
6dc54d91
PA
2951 /* The core the thread was running on. -1 if not known. */
2952 int core;
2953} thread_item_t;
2954DEF_VEC_O(thread_item_t);
2955
2956/* Context passed around to the various methods listing remote
2957 threads. As new threads are found, they're added to the ITEMS
2958 vector. */
2959
2960struct threads_listing_context
2961{
2962 /* The threads found on the remote target. */
2963 VEC (thread_item_t) *items;
2964};
2965
80134cf5
PA
2966/* Discard the contents of the constructed thread listing context. */
2967
2968static void
2969clear_threads_listing_context (void *p)
2970{
19ba03f4
SM
2971 struct threads_listing_context *context
2972 = (struct threads_listing_context *) p;
80134cf5
PA
2973 int i;
2974 struct thread_item *item;
2975
2976 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
79efa585
SM
2977 {
2978 xfree (item->extra);
2979 xfree (item->name);
2980 }
80134cf5
PA
2981
2982 VEC_free (thread_item_t, context->items);
2983}
2984
cbb8991c
DB
2985/* Remove the thread specified as the related_pid field of WS
2986 from the CONTEXT list. */
2987
2988static void
2989threads_listing_context_remove (struct target_waitstatus *ws,
2990 struct threads_listing_context *context)
2991{
2992 struct thread_item *item;
2993 int i;
2994 ptid_t child_ptid = ws->value.related_pid;
2995
2996 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2997 {
2998 if (ptid_equal (item->ptid, child_ptid))
2999 {
3000 VEC_ordered_remove (thread_item_t, context->items, i);
3001 break;
3002 }
3003 }
3004}
3005
c906108c 3006static int
6dc54d91 3007remote_newthread_step (threadref *ref, void *data)
c906108c 3008{
19ba03f4
SM
3009 struct threads_listing_context *context
3010 = (struct threads_listing_context *) data;
6dc54d91 3011 struct thread_item item;
79d7f229 3012 int pid = ptid_get_pid (inferior_ptid);
39f77062 3013
6dc54d91
PA
3014 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
3015 item.core = -1;
2e3b657e 3016 item.name = NULL;
6dc54d91
PA
3017 item.extra = NULL;
3018
3019 VEC_safe_push (thread_item_t, context->items, &item);
3020
c906108c
SS
3021 return 1; /* continue iterator */
3022}
3023
3024#define CRAZY_MAX_THREADS 1000
3025
39f77062
KB
3026static ptid_t
3027remote_current_thread (ptid_t oldpid)
c906108c 3028{
d01949b6 3029 struct remote_state *rs = get_remote_state ();
c906108c
SS
3030
3031 putpkt ("qC");
6d820c5c 3032 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3033 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34
KB
3034 {
3035 char *obuf;
3036 ptid_t result;
3037
3038 result = read_ptid (&rs->buf[2], &obuf);
3039 if (*obuf != '\0' && remote_debug)
3040 fprintf_unfiltered (gdb_stdlog,
3041 "warning: garbage in qC reply\n");
3042
3043 return result;
3044 }
c906108c
SS
3045 else
3046 return oldpid;
3047}
3048
6dc54d91 3049/* List remote threads using the deprecated qL packet. */
cce74817 3050
6dc54d91
PA
3051static int
3052remote_get_threads_with_ql (struct target_ops *ops,
3053 struct threads_listing_context *context)
c906108c 3054{
6dc54d91
PA
3055 if (remote_threadlist_iterator (remote_newthread_step, context,
3056 CRAZY_MAX_THREADS) >= 0)
3057 return 1;
3058
3059 return 0;
c906108c
SS
3060}
3061
dc146f7c
VP
3062#if defined(HAVE_LIBEXPAT)
3063
dc146f7c
VP
3064static void
3065start_thread (struct gdb_xml_parser *parser,
3066 const struct gdb_xml_element *element,
3067 void *user_data, VEC(gdb_xml_value_s) *attributes)
3068{
19ba03f4
SM
3069 struct threads_listing_context *data
3070 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3071
3072 struct thread_item item;
3073 char *id;
3d2c1d41 3074 struct gdb_xml_value *attr;
dc146f7c 3075
19ba03f4 3076 id = (char *) xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
3077 item.ptid = read_ptid (id, NULL);
3078
3d2c1d41
PA
3079 attr = xml_find_attribute (attributes, "core");
3080 if (attr != NULL)
3081 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
3082 else
3083 item.core = -1;
3084
79efa585 3085 attr = xml_find_attribute (attributes, "name");
e1961661 3086 item.name = attr != NULL ? xstrdup ((const char *) attr->value) : NULL;
79efa585 3087
dc146f7c
VP
3088 item.extra = 0;
3089
3090 VEC_safe_push (thread_item_t, data->items, &item);
3091}
3092
3093static void
3094end_thread (struct gdb_xml_parser *parser,
3095 const struct gdb_xml_element *element,
3096 void *user_data, const char *body_text)
3097{
19ba03f4
SM
3098 struct threads_listing_context *data
3099 = (struct threads_listing_context *) user_data;
dc146f7c
VP
3100
3101 if (body_text && *body_text)
2ae2a0b7 3102 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
3103}
3104
3105const struct gdb_xml_attribute thread_attributes[] = {
3106 { "id", GDB_XML_AF_NONE, NULL, NULL },
3107 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
79efa585 3108 { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
dc146f7c
VP
3109 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3110};
3111
3112const struct gdb_xml_element thread_children[] = {
3113 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3114};
3115
3116const struct gdb_xml_element threads_children[] = {
3117 { "thread", thread_attributes, thread_children,
3118 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3119 start_thread, end_thread },
3120 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3121};
3122
3123const struct gdb_xml_element threads_elements[] = {
3124 { "threads", NULL, threads_children,
3125 GDB_XML_EF_NONE, NULL, NULL },
3126 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3127};
3128
3129#endif
3130
6dc54d91 3131/* List remote threads using qXfer:threads:read. */
9d1f7ab2 3132
6dc54d91
PA
3133static int
3134remote_get_threads_with_qxfer (struct target_ops *ops,
3135 struct threads_listing_context *context)
0f71a2f6 3136{
dc146f7c 3137#if defined(HAVE_LIBEXPAT)
4082afcc 3138 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 3139 {
6dc54d91 3140 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 3141 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 3142
6dc54d91 3143 if (xml != NULL && *xml != '\0')
dc146f7c 3144 {
6dc54d91
PA
3145 gdb_xml_parse_quick (_("threads"), "threads.dtd",
3146 threads_elements, xml, context);
dc146f7c
VP
3147 }
3148
3149 do_cleanups (back_to);
6dc54d91 3150 return 1;
dc146f7c
VP
3151 }
3152#endif
3153
6dc54d91
PA
3154 return 0;
3155}
3156
3157/* List remote threads using qfThreadInfo/qsThreadInfo. */
3158
3159static int
3160remote_get_threads_with_qthreadinfo (struct target_ops *ops,
3161 struct threads_listing_context *context)
3162{
3163 struct remote_state *rs = get_remote_state ();
3164
b80fafe3 3165 if (rs->use_threadinfo_query)
9d1f7ab2 3166 {
6dc54d91
PA
3167 char *bufp;
3168
9d1f7ab2 3169 putpkt ("qfThreadInfo");
6d820c5c 3170 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3171 bufp = rs->buf;
9d1f7ab2 3172 if (bufp[0] != '\0') /* q packet recognized */
802188a7 3173 {
9d1f7ab2
MS
3174 while (*bufp++ == 'm') /* reply contains one or more TID */
3175 {
3176 do
3177 {
6dc54d91
PA
3178 struct thread_item item;
3179
3180 item.ptid = read_ptid (bufp, &bufp);
3181 item.core = -1;
2e3b657e 3182 item.name = NULL;
6dc54d91
PA
3183 item.extra = NULL;
3184
3185 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
3186 }
3187 while (*bufp++ == ','); /* comma-separated list */
3188 putpkt ("qsThreadInfo");
6d820c5c 3189 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3190 bufp = rs->buf;
9d1f7ab2 3191 }
6dc54d91
PA
3192 return 1;
3193 }
3194 else
3195 {
3196 /* Packet not recognized. */
3197 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3198 }
3199 }
3200
6dc54d91
PA
3201 return 0;
3202}
3203
e8032dde 3204/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3205 targets. */
3206
3207static void
e8032dde 3208remote_update_thread_list (struct target_ops *ops)
6dc54d91 3209{
6dc54d91
PA
3210 struct threads_listing_context context;
3211 struct cleanup *old_chain;
ab970af1 3212 int got_list = 0;
e8032dde 3213
6dc54d91
PA
3214 context.items = NULL;
3215 old_chain = make_cleanup (clear_threads_listing_context, &context);
3216
3217 /* We have a few different mechanisms to fetch the thread list. Try
3218 them all, starting with the most preferred one first, falling
3219 back to older methods. */
3220 if (remote_get_threads_with_qxfer (ops, &context)
3221 || remote_get_threads_with_qthreadinfo (ops, &context)
3222 || remote_get_threads_with_ql (ops, &context))
3223 {
3224 int i;
3225 struct thread_item *item;
ab970af1
PA
3226 struct thread_info *tp, *tmp;
3227
3228 got_list = 1;
3229
7d1a114c
PA
3230 if (VEC_empty (thread_item_t, context.items)
3231 && remote_thread_always_alive (ops, inferior_ptid))
3232 {
3233 /* Some targets don't really support threads, but still
3234 reply an (empty) thread list in response to the thread
3235 listing packets, instead of replying "packet not
3236 supported". Exit early so we don't delete the main
3237 thread. */
3238 do_cleanups (old_chain);
3239 return;
3240 }
3241
ab970af1
PA
3242 /* CONTEXT now holds the current thread list on the remote
3243 target end. Delete GDB-side threads no longer found on the
3244 target. */
8a06aea7 3245 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3246 {
ab970af1
PA
3247 for (i = 0;
3248 VEC_iterate (thread_item_t, context.items, i, item);
3249 ++i)
3250 {
3251 if (ptid_equal (item->ptid, tp->ptid))
3252 break;
3253 }
3254
3255 if (i == VEC_length (thread_item_t, context.items))
3256 {
3257 /* Not found. */
3258 delete_thread (tp->ptid);
3259 }
cbb8991c
DB
3260 }
3261
3262 /* Remove any unreported fork child threads from CONTEXT so
3263 that we don't interfere with follow fork, which is where
3264 creation of such threads is handled. */
3265 remove_new_fork_children (&context);
74531fed 3266
ab970af1 3267 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
3268 for (i = 0;
3269 VEC_iterate (thread_item_t, context.items, i, item);
3270 ++i)
3271 {
3272 if (!ptid_equal (item->ptid, null_ptid))
3273 {
3274 struct private_thread_info *info;
3275 /* In non-stop mode, we assume new found threads are
0d5b594f
PA
3276 executing until proven otherwise with a stop reply.
3277 In all-stop, we can only get here if all threads are
6dc54d91 3278 stopped. */
0d5b594f 3279 int executing = target_is_non_stop_p () ? 1 : 0;
6dc54d91 3280
0d5b594f 3281 remote_notice_new_inferior (item->ptid, executing);
6dc54d91
PA
3282
3283 info = demand_private_info (item->ptid);
3284 info->core = item->core;
3285 info->extra = item->extra;
3286 item->extra = NULL;
79efa585
SM
3287 info->name = item->name;
3288 item->name = NULL;
6dc54d91
PA
3289 }
3290 }
3291 }
3292
ab970af1
PA
3293 if (!got_list)
3294 {
3295 /* If no thread listing method is supported, then query whether
3296 each known thread is alive, one by one, with the T packet.
3297 If the target doesn't support threads at all, then this is a
3298 no-op. See remote_thread_alive. */
3299 prune_threads ();
3300 }
3301
6dc54d91 3302 do_cleanups (old_chain);
9d1f7ab2
MS
3303}
3304
802188a7 3305/*
9d1f7ab2
MS
3306 * Collect a descriptive string about the given thread.
3307 * The target may say anything it wants to about the thread
3308 * (typically info about its blocked / runnable state, name, etc.).
3309 * This string will appear in the info threads display.
802188a7 3310 *
9d1f7ab2
MS
3311 * Optional: targets are not required to implement this function.
3312 */
3313
3314static char *
c15906d8 3315remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3316{
d01949b6 3317 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3318 int result;
3319 int set;
3320 threadref id;
3321 struct gdb_ext_thread_info threadinfo;
23860348 3322 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3323 int n = 0; /* position in display_buf */
3324
5d93a237 3325 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3326 internal_error (__FILE__, __LINE__,
e2e0b3e5 3327 _("remote_threads_extra_info"));
9d1f7ab2 3328
60e569b9 3329 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3330 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3331 /* This is the main thread which was added by GDB. The remote
3332 server doesn't know about it. */
3333 return NULL;
3334
4082afcc 3335 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3336 {
3337 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3338
fe978cb0
PA
3339 if (info && info->priv)
3340 return info->priv->extra;
dc146f7c
VP
3341 else
3342 return NULL;
3343 }
3344
b80fafe3 3345 if (rs->use_threadextra_query)
9d1f7ab2 3346 {
82f73884
PA
3347 char *b = rs->buf;
3348 char *endb = rs->buf + get_remote_packet_size ();
3349
3350 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3351 b += strlen (b);
3352 write_ptid (b, endb, tp->ptid);
3353
2e9f7625 3354 putpkt (rs->buf);
6d820c5c 3355 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3356 if (rs->buf[0] != 0)
9d1f7ab2 3357 {
325fac50 3358 n = std::min (strlen (rs->buf) / 2, sizeof (display_buf));
2e9f7625 3359 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3360 display_buf [result] = '\0';
9d1f7ab2
MS
3361 return display_buf;
3362 }
0f71a2f6 3363 }
9d1f7ab2
MS
3364
3365 /* If the above query fails, fall back to the old method. */
b80fafe3 3366 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3367 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3368 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3369 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3370 if (remote_get_threadinfo (&id, set, &threadinfo))
3371 if (threadinfo.active)
0f71a2f6 3372 {
9d1f7ab2 3373 if (*threadinfo.shortname)
2bc416ba 3374 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3375 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3376 if (*threadinfo.display)
2bc416ba 3377 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3378 " State: %s,", threadinfo.display);
9d1f7ab2 3379 if (*threadinfo.more_display)
2bc416ba 3380 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3381 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3382
3383 if (n > 0)
c5aa993b 3384 {
23860348 3385 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3386 if (',' == display_buf[n-1])
3387 display_buf[n-1] = ' ';
3388 return display_buf;
c5aa993b 3389 }
0f71a2f6 3390 }
9d1f7ab2 3391 return NULL;
0f71a2f6 3392}
c906108c 3393\f
c5aa993b 3394
0fb4aa4b 3395static int
61fc905d 3396remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3397 struct static_tracepoint_marker *marker)
3398{
3399 struct remote_state *rs = get_remote_state ();
3400 char *p = rs->buf;
3401
bba74b36 3402 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3403 p += strlen (p);
3404 p += hexnumstr (p, addr);
3405 putpkt (rs->buf);
3406 getpkt (&rs->buf, &rs->buf_size, 0);
3407 p = rs->buf;
3408
3409 if (*p == 'E')
3410 error (_("Remote failure reply: %s"), p);
3411
3412 if (*p++ == 'm')
3413 {
3414 parse_static_tracepoint_marker_definition (p, &p, marker);
3415 return 1;
3416 }
3417
3418 return 0;
3419}
3420
0fb4aa4b 3421static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3422remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3423 const char *strid)
0fb4aa4b
PA
3424{
3425 struct remote_state *rs = get_remote_state ();
3426 VEC(static_tracepoint_marker_p) *markers = NULL;
3427 struct static_tracepoint_marker *marker = NULL;
3428 struct cleanup *old_chain;
3429 char *p;
3430
3431 /* Ask for a first packet of static tracepoint marker
3432 definition. */
3433 putpkt ("qTfSTM");
3434 getpkt (&rs->buf, &rs->buf_size, 0);
3435 p = rs->buf;
3436 if (*p == 'E')
3437 error (_("Remote failure reply: %s"), p);
3438
3439 old_chain = make_cleanup (free_current_marker, &marker);
3440
3441 while (*p++ == 'm')
3442 {
3443 if (marker == NULL)
3444 marker = XCNEW (struct static_tracepoint_marker);
3445
3446 do
3447 {
3448 parse_static_tracepoint_marker_definition (p, &p, marker);
3449
3450 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3451 {
3452 VEC_safe_push (static_tracepoint_marker_p,
3453 markers, marker);
3454 marker = NULL;
3455 }
3456 else
3457 {
3458 release_static_tracepoint_marker (marker);
3459 memset (marker, 0, sizeof (*marker));
3460 }
3461 }
3462 while (*p++ == ','); /* comma-separated list */
3463 /* Ask for another packet of static tracepoint definition. */
3464 putpkt ("qTsSTM");
3465 getpkt (&rs->buf, &rs->buf_size, 0);
3466 p = rs->buf;
3467 }
3468
3469 do_cleanups (old_chain);
3470 return markers;
3471}
3472
3473\f
10760264
JB
3474/* Implement the to_get_ada_task_ptid function for the remote targets. */
3475
3476static ptid_t
1e6b91a4 3477remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3478{
ba348170 3479 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3480}
3481\f
3482
24b06219 3483/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3484
3485static void
fba45db2 3486extended_remote_restart (void)
c906108c 3487{
d01949b6 3488 struct remote_state *rs = get_remote_state ();
c906108c
SS
3489
3490 /* Send the restart command; for reasons I don't understand the
3491 remote side really expects a number after the "R". */
ea9c271d 3492 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3493 putpkt (rs->buf);
c906108c 3494
ad9a8f3f 3495 remote_fileio_reset ();
c906108c
SS
3496}
3497\f
3498/* Clean up connection to a remote debugger. */
3499
c906108c 3500static void
de90e03d 3501remote_close (struct target_ops *self)
c906108c 3502{
5d93a237
TT
3503 struct remote_state *rs = get_remote_state ();
3504
3505 if (rs->remote_desc == NULL)
d3fd5342
PA
3506 return; /* already closed */
3507
048094ac 3508 /* Make sure we leave stdin registered in the event loop. */
e3594fd1 3509 remote_terminal_ours (self);
ce5ce7ed 3510
5d93a237
TT
3511 serial_close (rs->remote_desc);
3512 rs->remote_desc = NULL;
ce5ce7ed
PA
3513
3514 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3515 of all the inferiors and their threads we were controlling.
3516 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3517 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3518 inferior_ptid = null_ptid;
f67fd822 3519 discard_all_inferiors ();
ce5ce7ed 3520
f48ff2a7
YQ
3521 /* We are closing the remote target, so we should discard
3522 everything of this target. */
bcc75809 3523 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3524
3525 if (remote_async_inferior_event_token)
3526 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3527
5965e028 3528 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3529
3530 trace_reset_local_state ();
c906108c
SS
3531}
3532
23860348 3533/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3534
3535static void
fba45db2 3536get_offsets (void)
c906108c 3537{
d01949b6 3538 struct remote_state *rs = get_remote_state ();
2e9f7625 3539 char *buf;
085dd6e6 3540 char *ptr;
31d99776
DJ
3541 int lose, num_segments = 0, do_sections, do_segments;
3542 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3543 struct section_offsets *offs;
31d99776
DJ
3544 struct symfile_segment_data *data;
3545
3546 if (symfile_objfile == NULL)
3547 return;
c906108c
SS
3548
3549 putpkt ("qOffsets");
6d820c5c 3550 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3551 buf = rs->buf;
c906108c
SS
3552
3553 if (buf[0] == '\000')
3554 return; /* Return silently. Stub doesn't support
23860348 3555 this command. */
c906108c
SS
3556 if (buf[0] == 'E')
3557 {
8a3fe4f8 3558 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3559 return;
3560 }
3561
3562 /* Pick up each field in turn. This used to be done with scanf, but
3563 scanf will make trouble if CORE_ADDR size doesn't match
3564 conversion directives correctly. The following code will work
3565 with any size of CORE_ADDR. */
3566 text_addr = data_addr = bss_addr = 0;
3567 ptr = buf;
3568 lose = 0;
3569
61012eef 3570 if (startswith (ptr, "Text="))
c906108c
SS
3571 {
3572 ptr += 5;
3573 /* Don't use strtol, could lose on big values. */
3574 while (*ptr && *ptr != ';')
3575 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3576
61012eef 3577 if (startswith (ptr, ";Data="))
31d99776
DJ
3578 {
3579 ptr += 6;
3580 while (*ptr && *ptr != ';')
3581 data_addr = (data_addr << 4) + fromhex (*ptr++);
3582 }
3583 else
3584 lose = 1;
3585
61012eef 3586 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3587 {
3588 ptr += 5;
3589 while (*ptr && *ptr != ';')
3590 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3591
31d99776
DJ
3592 if (bss_addr != data_addr)
3593 warning (_("Target reported unsupported offsets: %s"), buf);
3594 }
3595 else
3596 lose = 1;
3597 }
61012eef 3598 else if (startswith (ptr, "TextSeg="))
c906108c 3599 {
31d99776
DJ
3600 ptr += 8;
3601 /* Don't use strtol, could lose on big values. */
c906108c 3602 while (*ptr && *ptr != ';')
31d99776
DJ
3603 text_addr = (text_addr << 4) + fromhex (*ptr++);
3604 num_segments = 1;
3605
61012eef 3606 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3607 {
3608 ptr += 9;
3609 while (*ptr && *ptr != ';')
3610 data_addr = (data_addr << 4) + fromhex (*ptr++);
3611 num_segments++;
3612 }
c906108c
SS
3613 }
3614 else
3615 lose = 1;
3616
3617 if (lose)
8a3fe4f8 3618 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3619 else if (*ptr != '\0')
3620 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3621
802188a7 3622 offs = ((struct section_offsets *)
a39a16c4 3623 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3624 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3625 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3626
31d99776
DJ
3627 data = get_symfile_segment_data (symfile_objfile->obfd);
3628 do_segments = (data != NULL);
3629 do_sections = num_segments == 0;
c906108c 3630
28c32713 3631 if (num_segments > 0)
31d99776 3632 {
31d99776
DJ
3633 segments[0] = text_addr;
3634 segments[1] = data_addr;
3635 }
28c32713
JB
3636 /* If we have two segments, we can still try to relocate everything
3637 by assuming that the .text and .data offsets apply to the whole
3638 text and data segments. Convert the offsets given in the packet
3639 to base addresses for symfile_map_offsets_to_segments. */
3640 else if (data && data->num_segments == 2)
3641 {
3642 segments[0] = data->segment_bases[0] + text_addr;
3643 segments[1] = data->segment_bases[1] + data_addr;
3644 num_segments = 2;
3645 }
8d385431
DJ
3646 /* If the object file has only one segment, assume that it is text
3647 rather than data; main programs with no writable data are rare,
3648 but programs with no code are useless. Of course the code might
3649 have ended up in the data segment... to detect that we would need
3650 the permissions here. */
3651 else if (data && data->num_segments == 1)
3652 {
3653 segments[0] = data->segment_bases[0] + text_addr;
3654 num_segments = 1;
3655 }
28c32713
JB
3656 /* There's no way to relocate by segment. */
3657 else
3658 do_segments = 0;
31d99776
DJ
3659
3660 if (do_segments)
3661 {
3662 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3663 offs, num_segments, segments);
3664
3665 if (ret == 0 && !do_sections)
3e43a32a
MS
3666 error (_("Can not handle qOffsets TextSeg "
3667 "response with this symbol file"));
31d99776
DJ
3668
3669 if (ret > 0)
3670 do_sections = 0;
3671 }
c906108c 3672
9ef895d6
DJ
3673 if (data)
3674 free_symfile_segment_data (data);
31d99776
DJ
3675
3676 if (do_sections)
3677 {
3678 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3679
3e43a32a
MS
3680 /* This is a temporary kludge to force data and bss to use the
3681 same offsets because that's what nlmconv does now. The real
3682 solution requires changes to the stub and remote.c that I
3683 don't have time to do right now. */
31d99776
DJ
3684
3685 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3686 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3687 }
c906108c
SS
3688
3689 objfile_relocate (symfile_objfile, offs);
3690}
3691
9a7071a8
JB
3692/* Send interrupt_sequence to remote target. */
3693static void
eeae04df 3694send_interrupt_sequence (void)
9a7071a8 3695{
5d93a237
TT
3696 struct remote_state *rs = get_remote_state ();
3697
9a7071a8 3698 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3699 remote_serial_write ("\x03", 1);
9a7071a8 3700 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3701 serial_send_break (rs->remote_desc);
9a7071a8
JB
3702 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3703 {
5d93a237 3704 serial_send_break (rs->remote_desc);
c33e31fd 3705 remote_serial_write ("g", 1);
9a7071a8
JB
3706 }
3707 else
3708 internal_error (__FILE__, __LINE__,
3709 _("Invalid value for interrupt_sequence_mode: %s."),
3710 interrupt_sequence_mode);
3711}
3712
3405876a
PA
3713
3714/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3715 and extract the PTID. Returns NULL_PTID if not found. */
3716
3717static ptid_t
3718stop_reply_extract_thread (char *stop_reply)
3719{
3720 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3721 {
3722 char *p;
3723
3724 /* Txx r:val ; r:val (...) */
3725 p = &stop_reply[3];
3726
3727 /* Look for "register" named "thread". */
3728 while (*p != '\0')
3729 {
3730 char *p1;
3731
3732 p1 = strchr (p, ':');
3733 if (p1 == NULL)
3734 return null_ptid;
3735
3736 if (strncmp (p, "thread", p1 - p) == 0)
3737 return read_ptid (++p1, &p);
3738
3739 p1 = strchr (p, ';');
3740 if (p1 == NULL)
3741 return null_ptid;
3742 p1++;
3743
3744 p = p1;
3745 }
3746 }
3747
3748 return null_ptid;
3749}
3750
b7ea362b
PA
3751/* Determine the remote side's current thread. If we have a stop
3752 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3753 "thread" register we can extract the current thread from. If not,
3754 ask the remote which is the current thread with qC. The former
3755 method avoids a roundtrip. */
3756
3757static ptid_t
3758get_current_thread (char *wait_status)
3759{
6a49a997 3760 ptid_t ptid = null_ptid;
b7ea362b
PA
3761
3762 /* Note we don't use remote_parse_stop_reply as that makes use of
3763 the target architecture, which we haven't yet fully determined at
3764 this point. */
3765 if (wait_status != NULL)
3766 ptid = stop_reply_extract_thread (wait_status);
3767 if (ptid_equal (ptid, null_ptid))
3768 ptid = remote_current_thread (inferior_ptid);
3769
3770 return ptid;
3771}
3772
49c62f2e
PA
3773/* Query the remote target for which is the current thread/process,
3774 add it to our tables, and update INFERIOR_PTID. The caller is
3775 responsible for setting the state such that the remote end is ready
3405876a
PA
3776 to return the current thread.
3777
3778 This function is called after handling the '?' or 'vRun' packets,
3779 whose response is a stop reply from which we can also try
3780 extracting the thread. If the target doesn't support the explicit
3781 qC query, we infer the current thread from that stop reply, passed
3782 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3783
3784static void
3405876a 3785add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3786{
3787 struct remote_state *rs = get_remote_state ();
3788 int fake_pid_p = 0;
6a49a997 3789 ptid_t ptid;
49c62f2e
PA
3790
3791 inferior_ptid = null_ptid;
3792
b7ea362b
PA
3793 /* Now, if we have thread information, update inferior_ptid. */
3794 ptid = get_current_thread (wait_status);
3405876a 3795
49c62f2e
PA
3796 if (!ptid_equal (ptid, null_ptid))
3797 {
3798 if (!remote_multi_process_p (rs))
3799 fake_pid_p = 1;
3800
3801 inferior_ptid = ptid;
3802 }
3803 else
3804 {
3805 /* Without this, some commands which require an active target
3806 (such as kill) won't work. This variable serves (at least)
3807 double duty as both the pid of the target process (if it has
3808 such), and as a flag indicating that a target is active. */
3809 inferior_ptid = magic_null_ptid;
3810 fake_pid_p = 1;
3811 }
3812
1b6e6f5c 3813 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3814
3815 /* Add the main thread. */
3816 add_thread_silent (inferior_ptid);
3817}
3818
6efcd9a8
PA
3819/* Print info about a thread that was found already stopped on
3820 connection. */
3821
3822static void
3823print_one_stopped_thread (struct thread_info *thread)
3824{
3825 struct target_waitstatus *ws = &thread->suspend.waitstatus;
3826
3827 switch_to_thread (thread->ptid);
3828 stop_pc = get_frame_pc (get_current_frame ());
3829 set_current_sal_from_frame (get_current_frame ());
3830
3831 thread->suspend.waitstatus_pending_p = 0;
3832
3833 if (ws->kind == TARGET_WAITKIND_STOPPED)
3834 {
3835 enum gdb_signal sig = ws->value.sig;
3836
3837 if (signal_print_state (sig))
3838 observer_notify_signal_received (sig);
3839 }
3840 observer_notify_normal_stop (NULL, 1);
3841}
3842
221e1a37
PA
3843/* Process all initial stop replies the remote side sent in response
3844 to the ? packet. These indicate threads that were already stopped
3845 on initial connection. We mark these threads as stopped and print
3846 their current frame before giving the user the prompt. */
3847
3848static void
6efcd9a8 3849process_initial_stop_replies (int from_tty)
221e1a37
PA
3850{
3851 int pending_stop_replies = stop_reply_queue_length ();
6efcd9a8
PA
3852 struct inferior *inf;
3853 struct thread_info *thread;
3854 struct thread_info *selected = NULL;
3855 struct thread_info *lowest_stopped = NULL;
3856 struct thread_info *first = NULL;
221e1a37
PA
3857
3858 /* Consume the initial pending events. */
3859 while (pending_stop_replies-- > 0)
3860 {
3861 ptid_t waiton_ptid = minus_one_ptid;
3862 ptid_t event_ptid;
3863 struct target_waitstatus ws;
3864 int ignore_event = 0;
6efcd9a8 3865 struct thread_info *thread;
221e1a37
PA
3866
3867 memset (&ws, 0, sizeof (ws));
3868 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
3869 if (remote_debug)
3870 print_target_wait_results (waiton_ptid, event_ptid, &ws);
3871
3872 switch (ws.kind)
3873 {
3874 case TARGET_WAITKIND_IGNORE:
3875 case TARGET_WAITKIND_NO_RESUMED:
3876 case TARGET_WAITKIND_SIGNALLED:
3877 case TARGET_WAITKIND_EXITED:
3878 /* We shouldn't see these, but if we do, just ignore. */
3879 if (remote_debug)
3880 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
3881 ignore_event = 1;
3882 break;
3883
3884 case TARGET_WAITKIND_EXECD:
3885 xfree (ws.value.execd_pathname);
3886 break;
3887 default:
3888 break;
3889 }
3890
3891 if (ignore_event)
3892 continue;
3893
6efcd9a8 3894 thread = find_thread_ptid (event_ptid);
221e1a37
PA
3895
3896 if (ws.kind == TARGET_WAITKIND_STOPPED)
3897 {
3898 enum gdb_signal sig = ws.value.sig;
3899
3900 /* Stubs traditionally report SIGTRAP as initial signal,
3901 instead of signal 0. Suppress it. */
3902 if (sig == GDB_SIGNAL_TRAP)
3903 sig = GDB_SIGNAL_0;
6efcd9a8
PA
3904 thread->suspend.stop_signal = sig;
3905 ws.value.sig = sig;
3906 }
221e1a37 3907
6efcd9a8
PA
3908 thread->suspend.waitstatus = ws;
3909
3910 if (ws.kind != TARGET_WAITKIND_STOPPED
3911 || ws.value.sig != GDB_SIGNAL_0)
3912 thread->suspend.waitstatus_pending_p = 1;
3913
3914 set_executing (event_ptid, 0);
3915 set_running (event_ptid, 0);
3916 }
3917
3918 /* "Notice" the new inferiors before anything related to
3919 registers/memory. */
3920 ALL_INFERIORS (inf)
3921 {
3922 if (inf->pid == 0)
3923 continue;
3924
3925 inf->needs_setup = 1;
3926
3927 if (non_stop)
3928 {
3929 thread = any_live_thread_of_process (inf->pid);
3930 notice_new_inferior (thread->ptid,
3931 thread->state == THREAD_RUNNING,
3932 from_tty);
3933 }
3934 }
3935
3936 /* If all-stop on top of non-stop, pause all threads. Note this
3937 records the threads' stop pc, so must be done after "noticing"
3938 the inferiors. */
3939 if (!non_stop)
3940 {
3941 stop_all_threads ();
3942
3943 /* If all threads of an inferior were already stopped, we
3944 haven't setup the inferior yet. */
3945 ALL_INFERIORS (inf)
3946 {
3947 if (inf->pid == 0)
3948 continue;
221e1a37 3949
6efcd9a8
PA
3950 if (inf->needs_setup)
3951 {
3952 thread = any_live_thread_of_process (inf->pid);
3953 switch_to_thread_no_regs (thread);
3954 setup_inferior (0);
3955 }
3956 }
221e1a37 3957 }
6efcd9a8
PA
3958
3959 /* Now go over all threads that are stopped, and print their current
3960 frame. If all-stop, then if there's a signalled thread, pick
3961 that as current. */
3962 ALL_NON_EXITED_THREADS (thread)
3963 {
6efcd9a8
PA
3964 if (first == NULL)
3965 first = thread;
3966
3967 if (!non_stop)
3968 set_running (thread->ptid, 0);
3969 else if (thread->state != THREAD_STOPPED)
3970 continue;
3971
6efcd9a8
PA
3972 if (selected == NULL
3973 && thread->suspend.waitstatus_pending_p)
3974 selected = thread;
3975
5d5658a1
PA
3976 if (lowest_stopped == NULL
3977 || thread->inf->num < lowest_stopped->inf->num
3978 || thread->per_inf_num < lowest_stopped->per_inf_num)
6efcd9a8
PA
3979 lowest_stopped = thread;
3980
3981 if (non_stop)
3982 print_one_stopped_thread (thread);
3983 }
3984
3985 /* In all-stop, we only print the status of one thread, and leave
3986 others with their status pending. */
3987 if (!non_stop)
3988 {
3989 thread = selected;
3990 if (thread == NULL)
3991 thread = lowest_stopped;
3992 if (thread == NULL)
3993 thread = first;
3994
3995 print_one_stopped_thread (thread);
3996 }
3997
3998 /* For "info program". */
3999 thread = inferior_thread ();
4000 if (thread->state == THREAD_STOPPED)
4001 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
221e1a37
PA
4002}
4003
048094ac
PA
4004/* Start the remote connection and sync state. */
4005
9cbc821d 4006static void
04bd08de 4007remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 4008{
c8d104ad
PA
4009 struct remote_state *rs = get_remote_state ();
4010 struct packet_config *noack_config;
2d717e4f 4011 char *wait_status = NULL;
8621d6a9 4012
048094ac
PA
4013 /* Signal other parts that we're going through the initial setup,
4014 and so things may not be stable yet. E.g., we don't try to
4015 install tracepoints until we've relocated symbols. Also, a
4016 Ctrl-C before we're connected and synced up can't interrupt the
4017 target. Instead, it offers to drop the (potentially wedged)
4018 connection. */
4019 rs->starting_up = 1;
4020
522002f9 4021 QUIT;
c906108c 4022
9a7071a8
JB
4023 if (interrupt_on_connect)
4024 send_interrupt_sequence ();
4025
57e12211 4026 /* Ack any packet which the remote side has already sent. */
048094ac 4027 remote_serial_write ("+", 1);
1e51243a 4028
c8d104ad
PA
4029 /* The first packet we send to the target is the optional "supported
4030 packets" request. If the target can answer this, it will tell us
4031 which later probes to skip. */
4032 remote_query_supported ();
4033
d914c394 4034 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 4035 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 4036 remote_set_permissions (target);
d914c394 4037
57809e5e
JK
4038 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4039 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4040 as a reply to known packet. For packet "vFile:setfs:" it is an
4041 invalid reply and GDB would return error in
4042 remote_hostio_set_filesystem, making remote files access impossible.
4043 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4044 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4045 {
4046 const char v_mustreplyempty[] = "vMustReplyEmpty";
4047
4048 putpkt (v_mustreplyempty);
4049 getpkt (&rs->buf, &rs->buf_size, 0);
4050 if (strcmp (rs->buf, "OK") == 0)
4051 remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
4052 else if (strcmp (rs->buf, "") != 0)
4053 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
4054 rs->buf);
4055 }
4056
c8d104ad
PA
4057 /* Next, we possibly activate noack mode.
4058
4059 If the QStartNoAckMode packet configuration is set to AUTO,
4060 enable noack mode if the stub reported a wish for it with
4061 qSupported.
4062
4063 If set to TRUE, then enable noack mode even if the stub didn't
4064 report it in qSupported. If the stub doesn't reply OK, the
4065 session ends with an error.
4066
4067 If FALSE, then don't activate noack mode, regardless of what the
4068 stub claimed should be the default with qSupported. */
4069
4070 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 4071 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
4072 {
4073 putpkt ("QStartNoAckMode");
4074 getpkt (&rs->buf, &rs->buf_size, 0);
4075 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
4076 rs->noack_mode = 1;
4077 }
4078
04bd08de 4079 if (extended_p)
5fe04517
PA
4080 {
4081 /* Tell the remote that we are using the extended protocol. */
4082 putpkt ("!");
4083 getpkt (&rs->buf, &rs->buf_size, 0);
4084 }
4085
9b224c5e
PA
4086 /* Let the target know which signals it is allowed to pass down to
4087 the program. */
4088 update_signals_program_target ();
4089
d962ef82
DJ
4090 /* Next, if the target can specify a description, read it. We do
4091 this before anything involving memory or registers. */
4092 target_find_description ();
4093
6c95b8df
PA
4094 /* Next, now that we know something about the target, update the
4095 address spaces in the program spaces. */
4096 update_address_spaces ();
4097
50c71eaf
PA
4098 /* On OSs where the list of libraries is global to all
4099 processes, we fetch them early. */
f5656ead 4100 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 4101 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 4102
6efcd9a8 4103 if (target_is_non_stop_p ())
74531fed 4104 {
4082afcc 4105 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
4106 error (_("Non-stop mode requested, but remote "
4107 "does not support non-stop"));
74531fed
PA
4108
4109 putpkt ("QNonStop:1");
4110 getpkt (&rs->buf, &rs->buf_size, 0);
4111
4112 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4113 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
4114
4115 /* Find about threads and processes the stub is already
4116 controlling. We default to adding them in the running state.
4117 The '?' query below will then tell us about which threads are
4118 stopped. */
e8032dde 4119 remote_update_thread_list (target);
74531fed 4120 }
4082afcc 4121 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
4122 {
4123 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 4124 Request it explicitly. */
74531fed
PA
4125 putpkt ("QNonStop:0");
4126 getpkt (&rs->buf, &rs->buf_size, 0);
4127
4128 if (strcmp (rs->buf, "OK") != 0)
9b20d036 4129 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
4130 }
4131
a0743c90
YQ
4132 /* Upload TSVs regardless of whether the target is running or not. The
4133 remote stub, such as GDBserver, may have some predefined or builtin
4134 TSVs, even if the target is not running. */
8bd200f1 4135 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
4136 {
4137 struct uploaded_tsv *uploaded_tsvs = NULL;
4138
181e3713 4139 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
4140 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4141 }
4142
2d717e4f
DJ
4143 /* Check whether the target is running now. */
4144 putpkt ("?");
4145 getpkt (&rs->buf, &rs->buf_size, 0);
4146
6efcd9a8 4147 if (!target_is_non_stop_p ())
2d717e4f 4148 {
74531fed 4149 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 4150 {
04bd08de 4151 if (!extended_p)
74531fed 4152 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
4153
4154 /* We're connected, but not running. Drop out before we
4155 call start_remote. */
e278ad5b 4156 rs->starting_up = 0;
c35b1492 4157 return;
2d717e4f
DJ
4158 }
4159 else
74531fed 4160 {
74531fed 4161 /* Save the reply for later. */
224c3ddb 4162 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
4163 strcpy (wait_status, rs->buf);
4164 }
4165
b7ea362b 4166 /* Fetch thread list. */
e8032dde 4167 target_update_thread_list ();
b7ea362b 4168
74531fed
PA
4169 /* Let the stub know that we want it to return the thread. */
4170 set_continue_thread (minus_one_ptid);
4171
b7ea362b
PA
4172 if (thread_count () == 0)
4173 {
4174 /* Target has no concept of threads at all. GDB treats
4175 non-threaded target as single-threaded; add a main
4176 thread. */
4177 add_current_inferior_and_thread (wait_status);
4178 }
4179 else
4180 {
4181 /* We have thread information; select the thread the target
4182 says should be current. If we're reconnecting to a
4183 multi-threaded program, this will ideally be the thread
4184 that last reported an event before GDB disconnected. */
4185 inferior_ptid = get_current_thread (wait_status);
4186 if (ptid_equal (inferior_ptid, null_ptid))
4187 {
4188 /* Odd... The target was able to list threads, but not
4189 tell us which thread was current (no "thread"
4190 register in T stop reply?). Just pick the first
4191 thread in the thread list then. */
c9f35b34
KB
4192
4193 if (remote_debug)
4194 fprintf_unfiltered (gdb_stdlog,
4195 "warning: couldn't determine remote "
4196 "current thread; picking first in list.\n");
4197
b7ea362b
PA
4198 inferior_ptid = thread_list->ptid;
4199 }
4200 }
74531fed 4201
6e586cc5
YQ
4202 /* init_wait_for_inferior should be called before get_offsets in order
4203 to manage `inserted' flag in bp loc in a correct state.
4204 breakpoint_init_inferior, called from init_wait_for_inferior, set
4205 `inserted' flag to 0, while before breakpoint_re_set, called from
4206 start_remote, set `inserted' flag to 1. In the initialization of
4207 inferior, breakpoint_init_inferior should be called first, and then
4208 breakpoint_re_set can be called. If this order is broken, state of
4209 `inserted' flag is wrong, and cause some problems on breakpoint
4210 manipulation. */
4211 init_wait_for_inferior ();
4212
74531fed
PA
4213 get_offsets (); /* Get text, data & bss offsets. */
4214
d962ef82
DJ
4215 /* If we could not find a description using qXfer, and we know
4216 how to do it some other way, try again. This is not
4217 supported for non-stop; it could be, but it is tricky if
4218 there are no stopped threads when we connect. */
04bd08de 4219 if (remote_read_description_p (target)
f5656ead 4220 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
4221 {
4222 target_clear_description ();
4223 target_find_description ();
4224 }
4225
74531fed
PA
4226 /* Use the previously fetched status. */
4227 gdb_assert (wait_status != NULL);
4228 strcpy (rs->buf, wait_status);
4229 rs->cached_wait_status = 1;
4230
04bd08de 4231 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
4232 }
4233 else
4234 {
68c97600
PA
4235 /* Clear WFI global state. Do this before finding about new
4236 threads and inferiors, and setting the current inferior.
4237 Otherwise we would clear the proceed status of the current
4238 inferior when we want its stop_soon state to be preserved
4239 (see notice_new_inferior). */
4240 init_wait_for_inferior ();
4241
74531fed
PA
4242 /* In non-stop, we will either get an "OK", meaning that there
4243 are no stopped threads at this time; or, a regular stop
4244 reply. In the latter case, there may be more than one thread
4245 stopped --- we pull them all out using the vStopped
4246 mechanism. */
4247 if (strcmp (rs->buf, "OK") != 0)
4248 {
722247f1 4249 struct notif_client *notif = &notif_client_stop;
2d717e4f 4250
722247f1
YQ
4251 /* remote_notif_get_pending_replies acks this one, and gets
4252 the rest out. */
f48ff2a7 4253 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
4254 = remote_notif_parse (notif, rs->buf);
4255 remote_notif_get_pending_events (notif);
74531fed 4256 }
2d717e4f 4257
74531fed
PA
4258 if (thread_count () == 0)
4259 {
04bd08de 4260 if (!extended_p)
74531fed 4261 error (_("The target is not running (try extended-remote?)"));
82f73884 4262
c35b1492
PA
4263 /* We're connected, but not running. Drop out before we
4264 call start_remote. */
e278ad5b 4265 rs->starting_up = 0;
c35b1492
PA
4266 return;
4267 }
74531fed 4268
74531fed
PA
4269 /* In non-stop mode, any cached wait status will be stored in
4270 the stop reply queue. */
4271 gdb_assert (wait_status == NULL);
f0223081 4272
2455069d 4273 /* Report all signals during attach/startup. */
94bedb42 4274 remote_pass_signals (target, 0, NULL);
221e1a37
PA
4275
4276 /* If there are already stopped threads, mark them stopped and
4277 report their stops before giving the prompt to the user. */
6efcd9a8 4278 process_initial_stop_replies (from_tty);
221e1a37
PA
4279
4280 if (target_can_async_p ())
4281 target_async (1);
74531fed 4282 }
c8d104ad 4283
c8d104ad
PA
4284 /* If we connected to a live target, do some additional setup. */
4285 if (target_has_execution)
4286 {
f4ccffad 4287 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 4288 remote_check_symbols ();
c8d104ad 4289 }
50c71eaf 4290
d5551862
SS
4291 /* Possibly the target has been engaged in a trace run started
4292 previously; find out where things are at. */
8bd200f1 4293 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 4294 {
00bf0b85 4295 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4296
00bf0b85
SS
4297 if (current_trace_status ()->running)
4298 printf_filtered (_("Trace is already running on the target.\n"));
4299
ab6617cc 4300 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
4301
4302 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4303 }
4304
c0272db5
TW
4305 /* Possibly the target has been engaged in a btrace record started
4306 previously; find out where things are at. */
4307 remote_btrace_maybe_reopen ();
4308
1e51243a
PA
4309 /* The thread and inferior lists are now synchronized with the
4310 target, our symbols have been relocated, and we're merged the
4311 target's tracepoints with ours. We're done with basic start
4312 up. */
4313 rs->starting_up = 0;
4314
a25a5a45
PA
4315 /* Maybe breakpoints are global and need to be inserted now. */
4316 if (breakpoints_should_be_inserted_now ())
50c71eaf 4317 insert_breakpoints ();
c906108c
SS
4318}
4319
4320/* Open a connection to a remote debugger.
4321 NAME is the filename used for communication. */
4322
4323static void
014f9477 4324remote_open (const char *name, int from_tty)
c906108c 4325{
75c99385 4326 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
4327}
4328
c906108c
SS
4329/* Open a connection to a remote debugger using the extended
4330 remote gdb protocol. NAME is the filename used for communication. */
4331
4332static void
014f9477 4333extended_remote_open (const char *name, int from_tty)
c906108c 4334{
75c99385 4335 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
4336}
4337
ca4f7f8b
PA
4338/* Reset all packets back to "unknown support". Called when opening a
4339 new connection to a remote target. */
c906108c 4340
d471ea57 4341static void
ca4f7f8b 4342reset_all_packet_configs_support (void)
d471ea57
AC
4343{
4344 int i;
a744cf53 4345
444abaca 4346 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4347 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4348}
4349
ca4f7f8b
PA
4350/* Initialize all packet configs. */
4351
4352static void
4353init_all_packet_configs (void)
4354{
4355 int i;
4356
4357 for (i = 0; i < PACKET_MAX; i++)
4358 {
4359 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4360 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4361 }
4362}
4363
23860348 4364/* Symbol look-up. */
dc8acb97
MS
4365
4366static void
36d25514 4367remote_check_symbols (void)
dc8acb97 4368{
d01949b6 4369 struct remote_state *rs = get_remote_state ();
dc8acb97 4370 char *msg, *reply, *tmp;
dc8acb97 4371 int end;
28170b88 4372 long reply_size;
a5c0808e 4373 struct cleanup *old_chain;
dc8acb97 4374
63154eca
PA
4375 /* The remote side has no concept of inferiors that aren't running
4376 yet, it only knows about running processes. If we're connected
4377 but our current inferior is not running, we should not invite the
4378 remote target to request symbol lookups related to its
4379 (unrelated) current process. */
4380 if (!target_has_execution)
4381 return;
4382
4082afcc 4383 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4384 return;
4385
63154eca
PA
4386 /* Make sure the remote is pointing at the right process. Note
4387 there's no way to select "no process". */
3c9c4b83
PA
4388 set_general_process ();
4389
6d820c5c
DJ
4390 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4391 because we need both at the same time. */
224c3ddb 4392 msg = (char *) xmalloc (get_remote_packet_size ());
a5c0808e 4393 old_chain = make_cleanup (xfree, msg);
28170b88
MK
4394 reply = (char *) xmalloc (get_remote_packet_size ());
4395 make_cleanup (free_current_contents, &reply);
4396 reply_size = get_remote_packet_size ();
6d820c5c 4397
23860348 4398 /* Invite target to request symbol lookups. */
dc8acb97
MS
4399
4400 putpkt ("qSymbol::");
28170b88
MK
4401 getpkt (&reply, &reply_size, 0);
4402 packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97 4403
61012eef 4404 while (startswith (reply, "qSymbol:"))
dc8acb97 4405 {
77e371c0
TT
4406 struct bound_minimal_symbol sym;
4407
dc8acb97 4408 tmp = &reply[8];
cfd77fa1 4409 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4410 msg[end] = '\0';
4411 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4412 if (sym.minsym == NULL)
ea9c271d 4413 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4414 else
2bbe3cc1 4415 {
f5656ead 4416 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4417 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4418
4419 /* If this is a function address, return the start of code
4420 instead of any data function descriptor. */
f5656ead 4421 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
4422 sym_addr,
4423 &current_target);
4424
4425 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4426 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4427 }
4428
dc8acb97 4429 putpkt (msg);
28170b88 4430 getpkt (&reply, &reply_size, 0);
dc8acb97 4431 }
a5c0808e
PA
4432
4433 do_cleanups (old_chain);
dc8acb97
MS
4434}
4435
9db8d71f 4436static struct serial *
baa336ce 4437remote_serial_open (const char *name)
9db8d71f
DJ
4438{
4439 static int udp_warning = 0;
4440
4441 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4442 of in ser-tcp.c, because it is the remote protocol assuming that the
4443 serial connection is reliable and not the serial connection promising
4444 to be. */
61012eef 4445 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4446 {
3e43a32a
MS
4447 warning (_("The remote protocol may be unreliable over UDP.\n"
4448 "Some events may be lost, rendering further debugging "
4449 "impossible."));
9db8d71f
DJ
4450 udp_warning = 1;
4451 }
4452
4453 return serial_open (name);
4454}
4455
d914c394
SS
4456/* Inform the target of our permission settings. The permission flags
4457 work without this, but if the target knows the settings, it can do
4458 a couple things. First, it can add its own check, to catch cases
4459 that somehow manage to get by the permissions checks in target
4460 methods. Second, if the target is wired to disallow particular
4461 settings (for instance, a system in the field that is not set up to
4462 be able to stop at a breakpoint), it can object to any unavailable
4463 permissions. */
4464
4465void
c378d69d 4466remote_set_permissions (struct target_ops *self)
d914c394
SS
4467{
4468 struct remote_state *rs = get_remote_state ();
4469
bba74b36
YQ
4470 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4471 "WriteReg:%x;WriteMem:%x;"
4472 "InsertBreak:%x;InsertTrace:%x;"
4473 "InsertFastTrace:%x;Stop:%x",
4474 may_write_registers, may_write_memory,
4475 may_insert_breakpoints, may_insert_tracepoints,
4476 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4477 putpkt (rs->buf);
4478 getpkt (&rs->buf, &rs->buf_size, 0);
4479
4480 /* If the target didn't like the packet, warn the user. Do not try
4481 to undo the user's settings, that would just be maddening. */
4482 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4483 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4484}
4485
be2a5f71
DJ
4486/* This type describes each known response to the qSupported
4487 packet. */
4488struct protocol_feature
4489{
4490 /* The name of this protocol feature. */
4491 const char *name;
4492
4493 /* The default for this protocol feature. */
4494 enum packet_support default_support;
4495
4496 /* The function to call when this feature is reported, or after
4497 qSupported processing if the feature is not supported.
4498 The first argument points to this structure. The second
4499 argument indicates whether the packet requested support be
4500 enabled, disabled, or probed (or the default, if this function
4501 is being called at the end of processing and this feature was
4502 not reported). The third argument may be NULL; if not NULL, it
4503 is a NUL-terminated string taken from the packet following
4504 this feature's name and an equals sign. */
4505 void (*func) (const struct protocol_feature *, enum packet_support,
4506 const char *);
4507
4508 /* The corresponding packet for this feature. Only used if
4509 FUNC is remote_supported_packet. */
4510 int packet;
4511};
4512
be2a5f71
DJ
4513static void
4514remote_supported_packet (const struct protocol_feature *feature,
4515 enum packet_support support,
4516 const char *argument)
4517{
4518 if (argument)
4519 {
4520 warning (_("Remote qSupported response supplied an unexpected value for"
4521 " \"%s\"."), feature->name);
4522 return;
4523 }
4524
4082afcc 4525 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4526}
be2a5f71
DJ
4527
4528static void
4529remote_packet_size (const struct protocol_feature *feature,
4530 enum packet_support support, const char *value)
4531{
4532 struct remote_state *rs = get_remote_state ();
4533
4534 int packet_size;
4535 char *value_end;
4536
4537 if (support != PACKET_ENABLE)
4538 return;
4539
4540 if (value == NULL || *value == '\0')
4541 {
4542 warning (_("Remote target reported \"%s\" without a size."),
4543 feature->name);
4544 return;
4545 }
4546
4547 errno = 0;
4548 packet_size = strtol (value, &value_end, 16);
4549 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4550 {
4551 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4552 feature->name, value);
4553 return;
4554 }
4555
be2a5f71
DJ
4556 /* Record the new maximum packet size. */
4557 rs->explicit_packet_size = packet_size;
4558}
4559
dc473cfb 4560static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4561 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4562 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4563 PACKET_qXfer_auxv },
c78fa86a
GB
4564 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4565 PACKET_qXfer_exec_file },
23181151
DJ
4566 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4567 PACKET_qXfer_features },
cfa9d6d9
DJ
4568 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4569 PACKET_qXfer_libraries },
2268b414
JK
4570 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4571 PACKET_qXfer_libraries_svr4 },
ced63ec0 4572 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4573 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4574 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4575 PACKET_qXfer_memory_map },
4de6483e
UW
4576 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4577 PACKET_qXfer_spu_read },
4578 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4579 PACKET_qXfer_spu_write },
07e059b5
VP
4580 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4581 PACKET_qXfer_osdata },
dc146f7c
VP
4582 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4583 PACKET_qXfer_threads },
b3b9301e
PA
4584 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4585 PACKET_qXfer_traceframe_info },
89be2091
DJ
4586 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4587 PACKET_QPassSignals },
82075af2
JS
4588 { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
4589 PACKET_QCatchSyscalls },
9b224c5e
PA
4590 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4591 PACKET_QProgramSignals },
a6f3e723
SL
4592 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4593 PACKET_QStartNoAckMode },
4082afcc
PA
4594 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4595 PACKET_multiprocess_feature },
4596 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4597 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4598 PACKET_qXfer_siginfo_read },
4599 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4600 PACKET_qXfer_siginfo_write },
4082afcc 4601 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4602 PACKET_ConditionalTracepoints },
4082afcc 4603 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4604 PACKET_ConditionalBreakpoints },
4082afcc 4605 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4606 PACKET_BreakpointCommands },
4082afcc 4607 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4608 PACKET_FastTracepoints },
4082afcc 4609 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4610 PACKET_StaticTracepoints },
4082afcc 4611 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4612 PACKET_InstallInTrace},
4082afcc
PA
4613 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4614 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4615 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4616 PACKET_bc },
4617 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4618 PACKET_bs },
409873ef
SS
4619 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4620 PACKET_TracepointSource },
d914c394
SS
4621 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4622 PACKET_QAllow },
4082afcc
PA
4623 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4624 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4625 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4626 PACKET_qXfer_fdpic },
169081d0
TG
4627 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4628 PACKET_qXfer_uib },
03583c20
UW
4629 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4630 PACKET_QDisableRandomization },
d1feda86 4631 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4632 { "QTBuffer:size", PACKET_DISABLE,
4633 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4634 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4635 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4636 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4637 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4638 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4639 PACKET_qXfer_btrace },
4640 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4641 PACKET_qXfer_btrace_conf },
4642 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4643 PACKET_Qbtrace_conf_bts_size },
4644 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4645 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4646 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4647 PACKET_fork_event_feature },
4648 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4649 PACKET_vfork_event_feature },
94585166
DB
4650 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4651 PACKET_exec_event_feature },
b20a6524 4652 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
750ce8d1 4653 PACKET_Qbtrace_conf_pt_size },
65706a29
PA
4654 { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
4655 { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
f2faf941 4656 { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
be2a5f71
DJ
4657};
4658
c8d5aac9
L
4659static char *remote_support_xml;
4660
4661/* Register string appended to "xmlRegisters=" in qSupported query. */
4662
4663void
6e39997a 4664register_remote_support_xml (const char *xml)
c8d5aac9
L
4665{
4666#if defined(HAVE_LIBEXPAT)
4667 if (remote_support_xml == NULL)
c4f7c687 4668 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4669 else
4670 {
4671 char *copy = xstrdup (remote_support_xml + 13);
4672 char *p = strtok (copy, ",");
4673
4674 do
4675 {
4676 if (strcmp (p, xml) == 0)
4677 {
4678 /* already there */
4679 xfree (copy);
4680 return;
4681 }
4682 }
4683 while ((p = strtok (NULL, ",")) != NULL);
4684 xfree (copy);
4685
94b0dee1
PA
4686 remote_support_xml = reconcat (remote_support_xml,
4687 remote_support_xml, ",", xml,
4688 (char *) NULL);
c8d5aac9
L
4689 }
4690#endif
4691}
4692
4693static char *
4694remote_query_supported_append (char *msg, const char *append)
4695{
4696 if (msg)
94b0dee1 4697 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4698 else
4699 return xstrdup (append);
4700}
4701
be2a5f71
DJ
4702static void
4703remote_query_supported (void)
4704{
4705 struct remote_state *rs = get_remote_state ();
4706 char *next;
4707 int i;
4708 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4709
4710 /* The packet support flags are handled differently for this packet
4711 than for most others. We treat an error, a disabled packet, and
4712 an empty response identically: any features which must be reported
4713 to be used will be automatically disabled. An empty buffer
4714 accomplishes this, since that is also the representation for a list
4715 containing no features. */
4716
4717 rs->buf[0] = 0;
4082afcc 4718 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4719 {
c8d5aac9 4720 char *q = NULL;
94b0dee1 4721 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4722
73b8c1fd
PA
4723 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
4724 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4725
f7e6eed5
PA
4726 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4727 q = remote_query_supported_append (q, "swbreak+");
4728 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4729 q = remote_query_supported_append (q, "hwbreak+");
4730
dde08ee1
PA
4731 q = remote_query_supported_append (q, "qRelocInsn+");
4732
8020350c
DB
4733 if (packet_set_cmd_state (PACKET_fork_event_feature)
4734 != AUTO_BOOLEAN_FALSE)
4735 q = remote_query_supported_append (q, "fork-events+");
4736 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4737 != AUTO_BOOLEAN_FALSE)
4738 q = remote_query_supported_append (q, "vfork-events+");
4739 if (packet_set_cmd_state (PACKET_exec_event_feature)
4740 != AUTO_BOOLEAN_FALSE)
4741 q = remote_query_supported_append (q, "exec-events+");
89245bc0 4742
750ce8d1
YQ
4743 if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
4744 q = remote_query_supported_append (q, "vContSupported+");
4745
65706a29
PA
4746 if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
4747 q = remote_query_supported_append (q, "QThreadEvents+");
4748
f2faf941
PA
4749 if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
4750 q = remote_query_supported_append (q, "no-resumed+");
4751
b35d5edb
PA
4752 /* Keep this one last to work around a gdbserver <= 7.10 bug in
4753 the qSupported:xmlRegisters=i386 handling. */
4754 if (remote_support_xml != NULL)
4755 q = remote_query_supported_append (q, remote_support_xml);
4756
dde08ee1
PA
4757 q = reconcat (q, "qSupported:", q, (char *) NULL);
4758 putpkt (q);
82f73884 4759
94b0dee1
PA
4760 do_cleanups (old_chain);
4761
be2a5f71
DJ
4762 getpkt (&rs->buf, &rs->buf_size, 0);
4763
4764 /* If an error occured, warn, but do not return - just reset the
4765 buffer to empty and go on to disable features. */
4766 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4767 == PACKET_ERROR)
4768 {
4769 warning (_("Remote failure reply: %s"), rs->buf);
4770 rs->buf[0] = 0;
4771 }
4772 }
4773
4774 memset (seen, 0, sizeof (seen));
4775
4776 next = rs->buf;
4777 while (*next)
4778 {
4779 enum packet_support is_supported;
4780 char *p, *end, *name_end, *value;
4781
4782 /* First separate out this item from the rest of the packet. If
4783 there's another item after this, we overwrite the separator
4784 (terminated strings are much easier to work with). */
4785 p = next;
4786 end = strchr (p, ';');
4787 if (end == NULL)
4788 {
4789 end = p + strlen (p);
4790 next = end;
4791 }
4792 else
4793 {
89be2091
DJ
4794 *end = '\0';
4795 next = end + 1;
4796
be2a5f71
DJ
4797 if (end == p)
4798 {
4799 warning (_("empty item in \"qSupported\" response"));
4800 continue;
4801 }
be2a5f71
DJ
4802 }
4803
4804 name_end = strchr (p, '=');
4805 if (name_end)
4806 {
4807 /* This is a name=value entry. */
4808 is_supported = PACKET_ENABLE;
4809 value = name_end + 1;
4810 *name_end = '\0';
4811 }
4812 else
4813 {
4814 value = NULL;
4815 switch (end[-1])
4816 {
4817 case '+':
4818 is_supported = PACKET_ENABLE;
4819 break;
4820
4821 case '-':
4822 is_supported = PACKET_DISABLE;
4823 break;
4824
4825 case '?':
4826 is_supported = PACKET_SUPPORT_UNKNOWN;
4827 break;
4828
4829 default:
3e43a32a
MS
4830 warning (_("unrecognized item \"%s\" "
4831 "in \"qSupported\" response"), p);
be2a5f71
DJ
4832 continue;
4833 }
4834 end[-1] = '\0';
4835 }
4836
4837 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4838 if (strcmp (remote_protocol_features[i].name, p) == 0)
4839 {
4840 const struct protocol_feature *feature;
4841
4842 seen[i] = 1;
4843 feature = &remote_protocol_features[i];
4844 feature->func (feature, is_supported, value);
4845 break;
4846 }
4847 }
4848
4849 /* If we increased the packet size, make sure to increase the global
4850 buffer size also. We delay this until after parsing the entire
4851 qSupported packet, because this is the same buffer we were
4852 parsing. */
4853 if (rs->buf_size < rs->explicit_packet_size)
4854 {
4855 rs->buf_size = rs->explicit_packet_size;
224c3ddb 4856 rs->buf = (char *) xrealloc (rs->buf, rs->buf_size);
be2a5f71
DJ
4857 }
4858
4859 /* Handle the defaults for unmentioned features. */
4860 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4861 if (!seen[i])
4862 {
4863 const struct protocol_feature *feature;
4864
4865 feature = &remote_protocol_features[i];
4866 feature->func (feature, feature->default_support, NULL);
4867 }
4868}
4869
048094ac
PA
4870/* Serial QUIT handler for the remote serial descriptor.
4871
4872 Defers handling a Ctrl-C until we're done with the current
4873 command/response packet sequence, unless:
4874
4875 - We're setting up the connection. Don't send a remote interrupt
4876 request, as we're not fully synced yet. Quit immediately
4877 instead.
4878
4879 - The target has been resumed in the foreground
4880 (target_terminal_is_ours is false) with a synchronous resume
4881 packet, and we're blocked waiting for the stop reply, thus a
4882 Ctrl-C should be immediately sent to the target.
4883
4884 - We get a second Ctrl-C while still within the same serial read or
4885 write. In that case the serial is seemingly wedged --- offer to
4886 quit/disconnect.
4887
4888 - We see a second Ctrl-C without target response, after having
4889 previously interrupted the target. In that case the target/stub
4890 is probably wedged --- offer to quit/disconnect.
4891*/
4892
4893static void
4894remote_serial_quit_handler (void)
4895{
4896 struct remote_state *rs = get_remote_state ();
4897
4898 if (check_quit_flag ())
4899 {
4900 /* If we're starting up, we're not fully synced yet. Quit
4901 immediately. */
4902 if (rs->starting_up)
4903 quit ();
4904 else if (rs->got_ctrlc_during_io)
4905 {
4906 if (query (_("The target is not responding to GDB commands.\n"
4907 "Stop debugging it? ")))
4908 remote_unpush_and_throw ();
4909 }
4910 /* If ^C has already been sent once, offer to disconnect. */
4911 else if (!target_terminal_is_ours () && rs->ctrlc_pending_p)
4912 interrupt_query ();
4913 /* All-stop protocol, and blocked waiting for stop reply. Send
4914 an interrupt request. */
4915 else if (!target_terminal_is_ours () && rs->waiting_for_stop_reply)
4916 target_interrupt (inferior_ptid);
4917 else
4918 rs->got_ctrlc_during_io = 1;
4919 }
4920}
4921
78a095c3
JK
4922/* Remove any of the remote.c targets from target stack. Upper targets depend
4923 on it so remove them first. */
4924
4925static void
4926remote_unpush_target (void)
4927{
915ef8b1 4928 pop_all_targets_at_and_above (process_stratum);
78a095c3 4929}
be2a5f71 4930
048094ac
PA
4931static void
4932remote_unpush_and_throw (void)
4933{
4934 remote_unpush_target ();
4935 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
4936}
4937
c906108c 4938static void
014f9477 4939remote_open_1 (const char *name, int from_tty,
3e43a32a 4940 struct target_ops *target, int extended_p)
c906108c 4941{
d01949b6 4942 struct remote_state *rs = get_remote_state ();
a6f3e723 4943
c906108c 4944 if (name == 0)
8a3fe4f8 4945 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4946 "serial device is attached to the remote system\n"
8a3fe4f8 4947 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4948
23860348 4949 /* See FIXME above. */
c6ebd6cf 4950 if (!target_async_permitted)
92d1e331 4951 wait_forever_enabled_p = 1;
6426a772 4952
2d717e4f 4953 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4954 Ask this question first, before target_preopen has a chance to kill
4955 anything. */
5d93a237 4956 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4957 {
78a095c3
JK
4958 if (from_tty
4959 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4960 error (_("Still connected."));
4961 }
4962
78a095c3 4963 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4964 target_preopen (from_tty);
4965
89be2091 4966 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4967 xfree (rs->last_pass_packet);
4968 rs->last_pass_packet = NULL;
89be2091 4969
9b224c5e
PA
4970 /* Make sure we send the program signals list the next time we
4971 resume. */
5e4a05c4
TT
4972 xfree (rs->last_program_signals_packet);
4973 rs->last_program_signals_packet = NULL;
9b224c5e 4974
ad9a8f3f 4975 remote_fileio_reset ();
1dd41f16
NS
4976 reopen_exec_file ();
4977 reread_symbols ();
4978
5d93a237
TT
4979 rs->remote_desc = remote_serial_open (name);
4980 if (!rs->remote_desc)
c906108c
SS
4981 perror_with_name (name);
4982
4983 if (baud_rate != -1)
4984 {
5d93a237 4985 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4986 {
9b74d5d3
KB
4987 /* The requested speed could not be set. Error out to
4988 top level after closing remote_desc. Take care to
4989 set remote_desc to NULL to avoid closing remote_desc
4990 more than once. */
5d93a237
TT
4991 serial_close (rs->remote_desc);
4992 rs->remote_desc = NULL;
c906108c
SS
4993 perror_with_name (name);
4994 }
4995 }
4996
236af5e3 4997 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4998 serial_raw (rs->remote_desc);
c906108c
SS
4999
5000 /* If there is something sitting in the buffer we might take it as a
5001 response to a command, which would be bad. */
5d93a237 5002 serial_flush_input (rs->remote_desc);
c906108c
SS
5003
5004 if (from_tty)
5005 {
5006 puts_filtered ("Remote debugging using ");
5007 puts_filtered (name);
5008 puts_filtered ("\n");
5009 }
23860348 5010 push_target (target); /* Switch to using remote target now. */
c906108c 5011
74531fed
PA
5012 /* Register extra event sources in the event loop. */
5013 remote_async_inferior_event_token
5014 = create_async_event_handler (remote_async_inferior_event_handler,
5015 NULL);
5965e028 5016 rs->notif_state = remote_notif_state_allocate ();
74531fed 5017
be2a5f71
DJ
5018 /* Reset the target state; these things will be queried either by
5019 remote_query_supported or as they are needed. */
ca4f7f8b 5020 reset_all_packet_configs_support ();
74531fed 5021 rs->cached_wait_status = 0;
be2a5f71 5022 rs->explicit_packet_size = 0;
a6f3e723 5023 rs->noack_mode = 0;
82f73884 5024 rs->extended = extended_p;
e24a49d8 5025 rs->waiting_for_stop_reply = 0;
3a29589a 5026 rs->ctrlc_pending_p = 0;
048094ac 5027 rs->got_ctrlc_during_io = 0;
802188a7 5028
47f8a51d
TT
5029 rs->general_thread = not_sent_ptid;
5030 rs->continue_thread = not_sent_ptid;
262e1174 5031 rs->remote_traceframe_number = -1;
c906108c 5032
3a00c802
PA
5033 rs->last_resume_exec_dir = EXEC_FORWARD;
5034
9d1f7ab2 5035 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
5036 rs->use_threadinfo_query = 1;
5037 rs->use_threadextra_query = 1;
9d1f7ab2 5038
80152258
PA
5039 readahead_cache_invalidate ();
5040
048094ac
PA
5041 /* Start out by owning the terminal. */
5042 remote_async_terminal_ours_p = 1;
5043
c6ebd6cf 5044 if (target_async_permitted)
92d1e331 5045 {
92d1e331
DJ
5046 /* FIXME: cagney/1999-09-23: During the initial connection it is
5047 assumed that the target is already ready and able to respond to
0df8b418 5048 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 5049 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 5050 around this. Eventually a mechanism that allows
92d1e331 5051 wait_for_inferior() to expect/get timeouts will be
23860348 5052 implemented. */
92d1e331
DJ
5053 wait_forever_enabled_p = 0;
5054 }
5055
23860348 5056 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 5057 no_shared_libraries (NULL, 0);
f78f6cf1 5058
74531fed
PA
5059 /* Start afresh. */
5060 init_thread_list ();
5061
36918e70 5062 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
5063 target (we'd otherwise be in an inconsistent state) and then
5064 propogate the error on up the exception chain. This ensures that
5065 the caller doesn't stumble along blindly assuming that the
5066 function succeeded. The CLI doesn't have this problem but other
5067 UI's, such as MI do.
36918e70
AC
5068
5069 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5070 this function should return an error indication letting the
ce2826aa 5071 caller restore the previous state. Unfortunately the command
36918e70
AC
5072 ``target remote'' is directly wired to this function making that
5073 impossible. On a positive note, the CLI side of this problem has
5074 been fixed - the function set_cmd_context() makes it possible for
5075 all the ``target ....'' commands to share a common callback
5076 function. See cli-dump.c. */
109c3e39 5077 {
2d717e4f 5078
492d29ea 5079 TRY
04bd08de
TT
5080 {
5081 remote_start_remote (from_tty, target, extended_p);
5082 }
492d29ea 5083 CATCH (ex, RETURN_MASK_ALL)
109c3e39 5084 {
c8d104ad
PA
5085 /* Pop the partially set up target - unless something else did
5086 already before throwing the exception. */
5d93a237 5087 if (rs->remote_desc != NULL)
78a095c3 5088 remote_unpush_target ();
c6ebd6cf 5089 if (target_async_permitted)
109c3e39
AC
5090 wait_forever_enabled_p = 1;
5091 throw_exception (ex);
5092 }
492d29ea 5093 END_CATCH
109c3e39 5094 }
c906108c 5095
f4abbc16
MM
5096 remote_btrace_reset ();
5097
c6ebd6cf 5098 if (target_async_permitted)
92d1e331 5099 wait_forever_enabled_p = 1;
43ff13b4
JM
5100}
5101
de0d863e
DB
5102/* Detach the specified process. */
5103
5104static void
5105remote_detach_pid (int pid)
5106{
5107 struct remote_state *rs = get_remote_state ();
5108
5109 if (remote_multi_process_p (rs))
5110 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
5111 else
5112 strcpy (rs->buf, "D");
5113
5114 putpkt (rs->buf);
5115 getpkt (&rs->buf, &rs->buf_size, 0);
5116
5117 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
5118 ;
5119 else if (rs->buf[0] == '\0')
5120 error (_("Remote doesn't know how to detach"));
5121 else
5122 error (_("Can't detach process."));
5123}
5124
5125/* This detaches a program to which we previously attached, using
5126 inferior_ptid to identify the process. After this is done, GDB
5127 can be used to debug some other program. We better not have left
5128 any breakpoints in the target program or it'll die when it hits
5129 one. */
c906108c
SS
5130
5131static void
de0d863e 5132remote_detach_1 (const char *args, int from_tty)
c906108c 5133{
82f73884 5134 int pid = ptid_get_pid (inferior_ptid);
d01949b6 5135 struct remote_state *rs = get_remote_state ();
de0d863e
DB
5136 struct thread_info *tp = find_thread_ptid (inferior_ptid);
5137 int is_fork_parent;
c906108c
SS
5138
5139 if (args)
8a3fe4f8 5140 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 5141
2d717e4f
DJ
5142 if (!target_has_execution)
5143 error (_("No process to detach from."));
5144
0f48b757 5145 target_announce_detach (from_tty);
7cee1e54 5146
c906108c 5147 /* Tell the remote target to detach. */
de0d863e 5148 remote_detach_pid (pid);
82f73884 5149
8020350c
DB
5150 /* Exit only if this is the only active inferior. */
5151 if (from_tty && !rs->extended && number_of_live_inferiors () == 1)
7cee1e54 5152 puts_filtered (_("Ending remote debugging.\n"));
82f73884 5153
de0d863e
DB
5154 /* Check to see if we are detaching a fork parent. Note that if we
5155 are detaching a fork child, tp == NULL. */
5156 is_fork_parent = (tp != NULL
5157 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
5158
5159 /* If doing detach-on-fork, we don't mourn, because that will delete
5160 breakpoints that should be available for the followed inferior. */
5161 if (!is_fork_parent)
bc1e6c81 5162 target_mourn_inferior (inferior_ptid);
de0d863e
DB
5163 else
5164 {
5165 inferior_ptid = null_ptid;
5166 detach_inferior (pid);
5167 }
2d717e4f
DJ
5168}
5169
5170static void
52554a0e 5171remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5172{
de0d863e 5173 remote_detach_1 (args, from_tty);
2d717e4f
DJ
5174}
5175
5176static void
52554a0e 5177extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 5178{
de0d863e
DB
5179 remote_detach_1 (args, from_tty);
5180}
5181
5182/* Target follow-fork function for remote targets. On entry, and
5183 at return, the current inferior is the fork parent.
5184
5185 Note that although this is currently only used for extended-remote,
5186 it is named remote_follow_fork in anticipation of using it for the
5187 remote target as well. */
5188
5189static int
5190remote_follow_fork (struct target_ops *ops, int follow_child,
5191 int detach_fork)
5192{
5193 struct remote_state *rs = get_remote_state ();
c269dbdb 5194 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 5195
c269dbdb
DB
5196 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
5197 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
5198 {
5199 /* When following the parent and detaching the child, we detach
5200 the child here. For the case of following the child and
5201 detaching the parent, the detach is done in the target-
5202 independent follow fork code in infrun.c. We can't use
5203 target_detach when detaching an unfollowed child because
5204 the client side doesn't know anything about the child. */
5205 if (detach_fork && !follow_child)
5206 {
5207 /* Detach the fork child. */
5208 ptid_t child_ptid;
5209 pid_t child_pid;
5210
5211 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
5212 child_pid = ptid_get_pid (child_ptid);
5213
5214 remote_detach_pid (child_pid);
5215 detach_inferior (child_pid);
5216 }
5217 }
5218 return 0;
c906108c
SS
5219}
5220
94585166
DB
5221/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
5222 in the program space of the new inferior. On entry and at return the
5223 current inferior is the exec'ing inferior. INF is the new exec'd
5224 inferior, which may be the same as the exec'ing inferior unless
5225 follow-exec-mode is "new". */
5226
5227static void
5228remote_follow_exec (struct target_ops *ops,
5229 struct inferior *inf, char *execd_pathname)
5230{
5231 /* We know that this is a target file name, so if it has the "target:"
5232 prefix we strip it off before saving it in the program space. */
5233 if (is_target_filename (execd_pathname))
5234 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
5235
5236 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
5237}
5238
6ad8ae5c
DJ
5239/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
5240
43ff13b4 5241static void
fee354ee 5242remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 5243{
43ff13b4 5244 if (args)
2d717e4f 5245 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 5246
8020350c
DB
5247 /* Make sure we unpush even the extended remote targets. Calling
5248 target_mourn_inferior won't unpush, and remote_mourn won't
5249 unpush if there is more than one inferior left. */
5250 unpush_target (target);
5251 generic_mourn_inferior ();
2d717e4f 5252
43ff13b4
JM
5253 if (from_tty)
5254 puts_filtered ("Ending remote debugging.\n");
5255}
5256
2d717e4f
DJ
5257/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
5258 be chatty about it. */
5259
5260static void
20f796c9
GB
5261extended_remote_attach (struct target_ops *target, const char *args,
5262 int from_tty)
2d717e4f
DJ
5263{
5264 struct remote_state *rs = get_remote_state ();
be86555c 5265 int pid;
96ef3384 5266 char *wait_status = NULL;
2d717e4f 5267
74164c56 5268 pid = parse_pid_to_attach (args);
2d717e4f 5269
74164c56
JK
5270 /* Remote PID can be freely equal to getpid, do not check it here the same
5271 way as in other targets. */
2d717e4f 5272
4082afcc 5273 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
5274 error (_("This target does not support attaching to a process"));
5275
7cee1e54
PA
5276 if (from_tty)
5277 {
5278 char *exec_file = get_exec_file (0);
5279
5280 if (exec_file)
5281 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5282 target_pid_to_str (pid_to_ptid (pid)));
5283 else
5284 printf_unfiltered (_("Attaching to %s\n"),
5285 target_pid_to_str (pid_to_ptid (pid)));
5286
5287 gdb_flush (gdb_stdout);
5288 }
5289
bba74b36 5290 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
5291 putpkt (rs->buf);
5292 getpkt (&rs->buf, &rs->buf_size, 0);
5293
4082afcc
PA
5294 switch (packet_ok (rs->buf,
5295 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 5296 {
4082afcc 5297 case PACKET_OK:
6efcd9a8 5298 if (!target_is_non_stop_p ())
74531fed
PA
5299 {
5300 /* Save the reply for later. */
224c3ddb 5301 wait_status = (char *) alloca (strlen (rs->buf) + 1);
74531fed
PA
5302 strcpy (wait_status, rs->buf);
5303 }
5304 else if (strcmp (rs->buf, "OK") != 0)
5305 error (_("Attaching to %s failed with: %s"),
5306 target_pid_to_str (pid_to_ptid (pid)),
5307 rs->buf);
4082afcc
PA
5308 break;
5309 case PACKET_UNKNOWN:
5310 error (_("This target does not support attaching to a process"));
5311 default:
5312 error (_("Attaching to %s failed"),
5313 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 5314 }
2d717e4f 5315
1b6e6f5c 5316 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 5317
2d717e4f 5318 inferior_ptid = pid_to_ptid (pid);
79d7f229 5319
6efcd9a8 5320 if (target_is_non_stop_p ())
bad34192
PA
5321 {
5322 struct thread_info *thread;
79d7f229 5323
bad34192 5324 /* Get list of threads. */
e8032dde 5325 remote_update_thread_list (target);
82f73884 5326
bad34192
PA
5327 thread = first_thread_of_process (pid);
5328 if (thread)
5329 inferior_ptid = thread->ptid;
5330 else
5331 inferior_ptid = pid_to_ptid (pid);
5332
5333 /* Invalidate our notion of the remote current thread. */
47f8a51d 5334 record_currthread (rs, minus_one_ptid);
bad34192 5335 }
74531fed 5336 else
bad34192
PA
5337 {
5338 /* Now, if we have thread information, update inferior_ptid. */
5339 inferior_ptid = remote_current_thread (inferior_ptid);
5340
5341 /* Add the main thread to the thread list. */
5342 add_thread_silent (inferior_ptid);
5343 }
c0a2216e 5344
96ef3384
UW
5345 /* Next, if the target can specify a description, read it. We do
5346 this before anything involving memory or registers. */
5347 target_find_description ();
5348
6efcd9a8 5349 if (!target_is_non_stop_p ())
74531fed
PA
5350 {
5351 /* Use the previously fetched status. */
5352 gdb_assert (wait_status != NULL);
5353
5354 if (target_can_async_p ())
5355 {
722247f1
YQ
5356 struct notif_event *reply
5357 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 5358
722247f1 5359 push_stop_reply ((struct stop_reply *) reply);
74531fed 5360
6a3753b3 5361 target_async (1);
74531fed
PA
5362 }
5363 else
5364 {
5365 gdb_assert (wait_status != NULL);
5366 strcpy (rs->buf, wait_status);
5367 rs->cached_wait_status = 1;
5368 }
5369 }
5370 else
5371 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5372}
5373
b9c1d481
AS
5374/* Implementation of the to_post_attach method. */
5375
5376static void
5377extended_remote_post_attach (struct target_ops *ops, int pid)
5378{
6efcd9a8
PA
5379 /* Get text, data & bss offsets. */
5380 get_offsets ();
5381
b9c1d481
AS
5382 /* In certain cases GDB might not have had the chance to start
5383 symbol lookup up until now. This could happen if the debugged
5384 binary is not using shared libraries, the vsyscall page is not
5385 present (on Linux) and the binary itself hadn't changed since the
5386 debugging process was started. */
5387 if (symfile_objfile != NULL)
5388 remote_check_symbols();
5389}
5390
c906108c 5391\f
506fb367
DJ
5392/* Check for the availability of vCont. This function should also check
5393 the response. */
c906108c
SS
5394
5395static void
6d820c5c 5396remote_vcont_probe (struct remote_state *rs)
c906108c 5397{
2e9f7625 5398 char *buf;
6d820c5c 5399
2e9f7625
DJ
5400 strcpy (rs->buf, "vCont?");
5401 putpkt (rs->buf);
6d820c5c 5402 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5403 buf = rs->buf;
c906108c 5404
506fb367 5405 /* Make sure that the features we assume are supported. */
61012eef 5406 if (startswith (buf, "vCont"))
506fb367
DJ
5407 {
5408 char *p = &buf[5];
750ce8d1 5409 int support_c, support_C;
506fb367 5410
750ce8d1
YQ
5411 rs->supports_vCont.s = 0;
5412 rs->supports_vCont.S = 0;
506fb367
DJ
5413 support_c = 0;
5414 support_C = 0;
d458bd84 5415 rs->supports_vCont.t = 0;
c1e36e3e 5416 rs->supports_vCont.r = 0;
506fb367
DJ
5417 while (p && *p == ';')
5418 {
5419 p++;
5420 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5421 rs->supports_vCont.s = 1;
506fb367 5422 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
750ce8d1 5423 rs->supports_vCont.S = 1;
506fb367
DJ
5424 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5425 support_c = 1;
5426 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5427 support_C = 1;
74531fed 5428 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5429 rs->supports_vCont.t = 1;
c1e36e3e
PA
5430 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5431 rs->supports_vCont.r = 1;
506fb367
DJ
5432
5433 p = strchr (p, ';');
5434 }
c906108c 5435
750ce8d1
YQ
5436 /* If c, and C are not all supported, we can't use vCont. Clearing
5437 BUF will make packet_ok disable the packet. */
5438 if (!support_c || !support_C)
506fb367
DJ
5439 buf[0] = 0;
5440 }
c906108c 5441
444abaca 5442 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5443}
c906108c 5444
0d8f58ca
PA
5445/* Helper function for building "vCont" resumptions. Write a
5446 resumption to P. ENDP points to one-passed-the-end of the buffer
5447 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5448 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5449 resumed thread should be single-stepped and/or signalled. If PTID
5450 equals minus_one_ptid, then all threads are resumed; if PTID
5451 represents a process, then all threads of the process are resumed;
5452 the thread to be stepped and/or signalled is given in the global
5453 INFERIOR_PTID. */
5454
5455static char *
5456append_resumption (char *p, char *endp,
2ea28649 5457 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5458{
5459 struct remote_state *rs = get_remote_state ();
5460
a493e3e2 5461 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5462 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5463 else if (step
5464 /* GDB is willing to range step. */
5465 && use_range_stepping
5466 /* Target supports range stepping. */
5467 && rs->supports_vCont.r
5468 /* We don't currently support range stepping multiple
5469 threads with a wildcard (though the protocol allows it,
5470 so stubs shouldn't make an active effort to forbid
5471 it). */
5472 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5473 {
5474 struct thread_info *tp;
5475
5476 if (ptid_equal (ptid, minus_one_ptid))
5477 {
5478 /* If we don't know about the target thread's tid, then
5479 we're resuming magic_null_ptid (see caller). */
5480 tp = find_thread_ptid (magic_null_ptid);
5481 }
5482 else
5483 tp = find_thread_ptid (ptid);
5484 gdb_assert (tp != NULL);
5485
5486 if (tp->control.may_range_step)
5487 {
5488 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5489
5490 p += xsnprintf (p, endp - p, ";r%s,%s",
5491 phex_nz (tp->control.step_range_start,
5492 addr_size),
5493 phex_nz (tp->control.step_range_end,
5494 addr_size));
5495 }
5496 else
5497 p += xsnprintf (p, endp - p, ";s");
5498 }
0d8f58ca
PA
5499 else if (step)
5500 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5501 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5502 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5503 else
5504 p += xsnprintf (p, endp - p, ";c");
5505
5506 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5507 {
5508 ptid_t nptid;
5509
5510 /* All (-1) threads of process. */
ba348170 5511 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5512
5513 p += xsnprintf (p, endp - p, ":");
5514 p = write_ptid (p, endp, nptid);
5515 }
5516 else if (!ptid_equal (ptid, minus_one_ptid))
5517 {
5518 p += xsnprintf (p, endp - p, ":");
5519 p = write_ptid (p, endp, ptid);
5520 }
5521
5522 return p;
5523}
5524
799a2abe
PA
5525/* Clear the thread's private info on resume. */
5526
5527static void
5528resume_clear_thread_private_info (struct thread_info *thread)
5529{
5530 if (thread->priv != NULL)
5531 {
5532 thread->priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
5533 thread->priv->watch_data_address = 0;
5534 }
5535}
5536
e5ef252a
PA
5537/* Append a vCont continue-with-signal action for threads that have a
5538 non-zero stop signal. */
5539
5540static char *
5541append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5542{
5543 struct thread_info *thread;
5544
034f788c 5545 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5546 if (ptid_match (thread->ptid, ptid)
5547 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5548 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5549 {
5550 p = append_resumption (p, endp, thread->ptid,
5551 0, thread->suspend.stop_signal);
5552 thread->suspend.stop_signal = GDB_SIGNAL_0;
799a2abe 5553 resume_clear_thread_private_info (thread);
e5ef252a
PA
5554 }
5555
5556 return p;
5557}
5558
7b68ffbb
PA
5559/* Set the target running, using the packets that use Hc
5560 (c/s/C/S). */
5561
5562static void
5563remote_resume_with_hc (struct target_ops *ops,
5564 ptid_t ptid, int step, enum gdb_signal siggnal)
5565{
5566 struct remote_state *rs = get_remote_state ();
5567 struct thread_info *thread;
5568 char *buf;
5569
5570 rs->last_sent_signal = siggnal;
5571 rs->last_sent_step = step;
5572
5573 /* The c/s/C/S resume packets use Hc, so set the continue
5574 thread. */
5575 if (ptid_equal (ptid, minus_one_ptid))
5576 set_continue_thread (any_thread_ptid);
5577 else
5578 set_continue_thread (ptid);
5579
5580 ALL_NON_EXITED_THREADS (thread)
5581 resume_clear_thread_private_info (thread);
5582
5583 buf = rs->buf;
5584 if (execution_direction == EXEC_REVERSE)
5585 {
5586 /* We don't pass signals to the target in reverse exec mode. */
5587 if (info_verbose && siggnal != GDB_SIGNAL_0)
5588 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
5589 siggnal);
5590
5591 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
5592 error (_("Remote reverse-step not supported."));
5593 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
5594 error (_("Remote reverse-continue not supported."));
5595
5596 strcpy (buf, step ? "bs" : "bc");
5597 }
5598 else if (siggnal != GDB_SIGNAL_0)
5599 {
5600 buf[0] = step ? 'S' : 'C';
5601 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
5602 buf[2] = tohex (((int) siggnal) & 0xf);
5603 buf[3] = '\0';
5604 }
5605 else
5606 strcpy (buf, step ? "s" : "c");
5607
5608 putpkt (buf);
5609}
5610
506fb367
DJ
5611/* Resume the remote inferior by using a "vCont" packet. The thread
5612 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5613 resumed thread should be single-stepped and/or signalled. If PTID
5614 equals minus_one_ptid, then all threads are resumed; the thread to
5615 be stepped and/or signalled is given in the global INFERIOR_PTID.
5616 This function returns non-zero iff it resumes the inferior.
44eaed12 5617
7b68ffbb
PA
5618 This function issues a strict subset of all possible vCont commands
5619 at the moment. */
44eaed12 5620
506fb367 5621static int
7b68ffbb 5622remote_resume_with_vcont (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5623{
5624 struct remote_state *rs = get_remote_state ();
82f73884
PA
5625 char *p;
5626 char *endp;
44eaed12 5627
7b68ffbb
PA
5628 /* No reverse execution actions defined for vCont. */
5629 if (execution_direction == EXEC_REVERSE)
5630 return 0;
5631
4082afcc 5632 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5633 remote_vcont_probe (rs);
44eaed12 5634
4082afcc 5635 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5636 return 0;
44eaed12 5637
82f73884
PA
5638 p = rs->buf;
5639 endp = rs->buf + get_remote_packet_size ();
5640
506fb367
DJ
5641 /* If we could generate a wider range of packets, we'd have to worry
5642 about overflowing BUF. Should there be a generic
5643 "multi-part-packet" packet? */
5644
0d8f58ca
PA
5645 p += xsnprintf (p, endp - p, "vCont");
5646
79d7f229 5647 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5648 {
79d7f229
PA
5649 /* MAGIC_NULL_PTID means that we don't have any active threads,
5650 so we don't have any TID numbers the inferior will
5651 understand. Make sure to only send forms that do not specify
5652 a TID. */
a9cbf802 5653 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5654 }
0d8f58ca 5655 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5656 {
0d8f58ca
PA
5657 /* Resume all threads (of all processes, or of a single
5658 process), with preference for INFERIOR_PTID. This assumes
5659 inferior_ptid belongs to the set of all threads we are about
5660 to resume. */
a493e3e2 5661 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5662 {
0d8f58ca
PA
5663 /* Step inferior_ptid, with or without signal. */
5664 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5665 }
0d8f58ca 5666
e5ef252a
PA
5667 /* Also pass down any pending signaled resumption for other
5668 threads not the current. */
5669 p = append_pending_thread_resumptions (p, endp, ptid);
5670
0d8f58ca 5671 /* And continue others without a signal. */
a493e3e2 5672 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5673 }
5674 else
506fb367
DJ
5675 {
5676 /* Scheduler locking; resume only PTID. */
a9cbf802 5677 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5678 }
c906108c 5679
82f73884
PA
5680 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5681 putpkt (rs->buf);
506fb367 5682
6efcd9a8 5683 if (target_is_non_stop_p ())
74531fed
PA
5684 {
5685 /* In non-stop, the stub replies to vCont with "OK". The stop
5686 reply will be reported asynchronously by means of a `%Stop'
5687 notification. */
5688 getpkt (&rs->buf, &rs->buf_size, 0);
5689 if (strcmp (rs->buf, "OK") != 0)
5690 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5691 }
5692
506fb367 5693 return 1;
c906108c 5694}
43ff13b4 5695
506fb367
DJ
5696/* Tell the remote machine to resume. */
5697
43ff13b4 5698static void
28439f5e 5699remote_resume (struct target_ops *ops,
2ea28649 5700 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5701{
d01949b6 5702 struct remote_state *rs = get_remote_state ();
43ff13b4 5703
722247f1
YQ
5704 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5705 (explained in remote-notif.c:handle_notification) so
5706 remote_notif_process is not called. We need find a place where
5707 it is safe to start a 'vNotif' sequence. It is good to do it
5708 before resuming inferior, because inferior was stopped and no RSP
5709 traffic at that moment. */
6efcd9a8 5710 if (!target_is_non_stop_p ())
5965e028 5711 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5712
3a00c802
PA
5713 rs->last_resume_exec_dir = execution_direction;
5714
7b68ffbb
PA
5715 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
5716 if (!remote_resume_with_vcont (ptid, step, siggnal))
5717 remote_resume_with_hc (ops, ptid, step, siggnal);
43ff13b4 5718
2acceee2 5719 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5720 with the event loop. NOTE: this is the one place where all the
5721 execution commands end up. We could alternatively do this in each
23860348 5722 of the execution commands in infcmd.c. */
2acceee2
JM
5723 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5724 into infcmd.c in order to allow inferior function calls to work
23860348 5725 NOT asynchronously. */
362646f5 5726 if (target_can_async_p ())
6a3753b3 5727 target_async (1);
e24a49d8
PA
5728
5729 /* We've just told the target to resume. The remote server will
5730 wait for the inferior to stop, and then send a stop reply. In
5731 the mean time, we can't start another command/query ourselves
74531fed
PA
5732 because the stub wouldn't be ready to process it. This applies
5733 only to the base all-stop protocol, however. In non-stop (which
5734 only supports vCont), the stub replies with an "OK", and is
5735 immediate able to process further serial input. */
6efcd9a8 5736 if (!target_is_non_stop_p ())
74531fed 5737 rs->waiting_for_stop_reply = 1;
43ff13b4 5738}
c906108c 5739\f
43ff13b4 5740
74531fed
PA
5741/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5742 thread, all threads of a remote process, or all threads of all
5743 processes. */
5744
5745static void
5746remote_stop_ns (ptid_t ptid)
5747{
5748 struct remote_state *rs = get_remote_state ();
5749 char *p = rs->buf;
5750 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5751
4082afcc 5752 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5753 remote_vcont_probe (rs);
5754
d458bd84 5755 if (!rs->supports_vCont.t)
74531fed
PA
5756 error (_("Remote server does not support stopping threads"));
5757
f91d3df5
PA
5758 if (ptid_equal (ptid, minus_one_ptid)
5759 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5760 p += xsnprintf (p, endp - p, "vCont;t");
5761 else
5762 {
5763 ptid_t nptid;
5764
74531fed
PA
5765 p += xsnprintf (p, endp - p, "vCont;t:");
5766
5767 if (ptid_is_pid (ptid))
5768 /* All (-1) threads of process. */
ba348170 5769 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5770 else
5771 {
5772 /* Small optimization: if we already have a stop reply for
5773 this thread, no use in telling the stub we want this
5774 stopped. */
5775 if (peek_stop_reply (ptid))
5776 return;
5777
5778 nptid = ptid;
5779 }
5780
a9cbf802 5781 write_ptid (p, endp, nptid);
74531fed
PA
5782 }
5783
5784 /* In non-stop, we get an immediate OK reply. The stop reply will
5785 come in asynchronously by notification. */
5786 putpkt (rs->buf);
5787 getpkt (&rs->buf, &rs->buf_size, 0);
5788 if (strcmp (rs->buf, "OK") != 0)
5789 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5790}
5791
bfedc46a
PA
5792/* All-stop version of target_interrupt. Sends a break or a ^C to
5793 interrupt the remote target. It is undefined which thread of which
5794 process reports the interrupt. */
74531fed
PA
5795
5796static void
de979965 5797remote_interrupt_as (void)
74531fed
PA
5798{
5799 struct remote_state *rs = get_remote_state ();
5800
3a29589a
DJ
5801 rs->ctrlc_pending_p = 1;
5802
74531fed
PA
5803 /* If the inferior is stopped already, but the core didn't know
5804 about it yet, just ignore the request. The cached wait status
5805 will be collected in remote_wait. */
5806 if (rs->cached_wait_status)
5807 return;
5808
9a7071a8
JB
5809 /* Send interrupt_sequence to remote target. */
5810 send_interrupt_sequence ();
74531fed
PA
5811}
5812
de979965
PA
5813/* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
5814 the remote target. It is undefined which thread of which process
e42de8c7
PA
5815 reports the interrupt. Throws an error if the packet is not
5816 supported by the server. */
de979965 5817
e42de8c7 5818static void
de979965
PA
5819remote_interrupt_ns (void)
5820{
5821 struct remote_state *rs = get_remote_state ();
5822 char *p = rs->buf;
5823 char *endp = rs->buf + get_remote_packet_size ();
5824
5825 xsnprintf (p, endp - p, "vCtrlC");
5826
5827 /* In non-stop, we get an immediate OK reply. The stop reply will
5828 come in asynchronously by notification. */
5829 putpkt (rs->buf);
5830 getpkt (&rs->buf, &rs->buf_size, 0);
5831
5832 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
5833 {
5834 case PACKET_OK:
5835 break;
5836 case PACKET_UNKNOWN:
e42de8c7 5837 error (_("No support for interrupting the remote target."));
de979965
PA
5838 case PACKET_ERROR:
5839 error (_("Interrupting target failed: %s"), rs->buf);
5840 }
de979965
PA
5841}
5842
bfedc46a 5843/* Implement the to_stop function for the remote targets. */
74531fed 5844
c906108c 5845static void
1eab8a48 5846remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5847{
7a292a7a 5848 if (remote_debug)
0f71a2f6 5849 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5850
6efcd9a8 5851 if (target_is_non_stop_p ())
74531fed 5852 remote_stop_ns (ptid);
c906108c 5853 else
bfedc46a
PA
5854 {
5855 /* We don't currently have a way to transparently pause the
5856 remote target in all-stop mode. Interrupt it instead. */
de979965 5857 remote_interrupt_as ();
bfedc46a
PA
5858 }
5859}
5860
5861/* Implement the to_interrupt function for the remote targets. */
5862
5863static void
5864remote_interrupt (struct target_ops *self, ptid_t ptid)
5865{
e42de8c7
PA
5866 struct remote_state *rs = get_remote_state ();
5867
bfedc46a
PA
5868 if (remote_debug)
5869 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
5870
e42de8c7
PA
5871 if (target_is_non_stop_p ())
5872 remote_interrupt_ns ();
bfedc46a 5873 else
e42de8c7 5874 remote_interrupt_as ();
c906108c
SS
5875}
5876
93692b58
PA
5877/* Implement the to_pass_ctrlc function for the remote targets. */
5878
5879static void
5880remote_pass_ctrlc (struct target_ops *self)
5881{
5882 struct remote_state *rs = get_remote_state ();
5883
5884 if (remote_debug)
5885 fprintf_unfiltered (gdb_stdlog, "remote_pass_ctrlc called\n");
5886
5887 /* If we're starting up, we're not fully synced yet. Quit
5888 immediately. */
5889 if (rs->starting_up)
5890 quit ();
5891 /* If ^C has already been sent once, offer to disconnect. */
5892 else if (rs->ctrlc_pending_p)
5893 interrupt_query ();
5894 else
5895 target_interrupt (inferior_ptid);
5896}
5897
c906108c
SS
5898/* Ask the user what to do when an interrupt is received. */
5899
5900static void
fba45db2 5901interrupt_query (void)
c906108c 5902{
abc56d60 5903 struct remote_state *rs = get_remote_state ();
c906108c 5904
abc56d60 5905 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 5906 {
abc56d60
PA
5907 if (query (_("The target is not responding to interrupt requests.\n"
5908 "Stop debugging it? ")))
74531fed 5909 {
78a095c3 5910 remote_unpush_target ();
abc56d60 5911 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
5912 }
5913 }
abc56d60
PA
5914 else
5915 {
5916 if (query (_("Interrupted while waiting for the program.\n"
5917 "Give up waiting? ")))
5918 quit ();
5919 }
c906108c
SS
5920}
5921
6426a772
JM
5922/* Enable/disable target terminal ownership. Most targets can use
5923 terminal groups to control terminal ownership. Remote targets are
5924 different in that explicit transfer of ownership to/from GDB/target
23860348 5925 is required. */
6426a772
JM
5926
5927static void
d2f640d4 5928remote_terminal_inferior (struct target_ops *self)
6426a772 5929{
d9d2d8b6
PA
5930 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5931 idempotent. The event-loop GDB talking to an asynchronous target
5932 with a synchronous command calls this function from both
5933 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5934 transfer the terminal to the target when it shouldn't this guard
5935 can go away. */
6426a772
JM
5936 if (!remote_async_terminal_ours_p)
5937 return;
6426a772 5938 remote_async_terminal_ours_p = 0;
6426a772
JM
5939 /* NOTE: At this point we could also register our selves as the
5940 recipient of all input. Any characters typed could then be
23860348 5941 passed on down to the target. */
6426a772
JM
5942}
5943
5944static void
e3594fd1 5945remote_terminal_ours (struct target_ops *self)
6426a772 5946{
75c99385 5947 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5948 if (remote_async_terminal_ours_p)
5949 return;
6426a772
JM
5950 remote_async_terminal_ours_p = 1;
5951}
5952
176a6961 5953static void
917317f4 5954remote_console_output (char *msg)
c906108c
SS
5955{
5956 char *p;
5957
c5aa993b 5958 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5959 {
5960 char tb[2];
5961 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5962
c906108c
SS
5963 tb[0] = c;
5964 tb[1] = 0;
43ff13b4 5965 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5966 }
00db5b94
PA
5967 gdb_flush (gdb_stdtarg);
5968}
74531fed
PA
5969
5970typedef struct cached_reg
5971{
5972 int num;
5973 gdb_byte data[MAX_REGISTER_SIZE];
5974} cached_reg_t;
5975
5976DEF_VEC_O(cached_reg_t);
5977
722247f1 5978typedef struct stop_reply
74531fed 5979{
722247f1 5980 struct notif_event base;
74531fed 5981
722247f1 5982 /* The identifier of the thread about this event */
74531fed
PA
5983 ptid_t ptid;
5984
340e3c99 5985 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5986 connection, represented by a remote_state object, is closed,
5987 all the associated stop_reply events should be released. */
5988 struct remote_state *rs;
5989
74531fed
PA
5990 struct target_waitstatus ws;
5991
15148d6a
PA
5992 /* Expedited registers. This makes remote debugging a bit more
5993 efficient for those targets that provide critical registers as
5994 part of their normal status mechanism (as another roundtrip to
5995 fetch them is avoided). */
74531fed
PA
5996 VEC(cached_reg_t) *regcache;
5997
f7e6eed5
PA
5998 enum target_stop_reason stop_reason;
5999
74531fed
PA
6000 CORE_ADDR watch_data_address;
6001
dc146f7c 6002 int core;
722247f1 6003} *stop_reply_p;
a744cf53 6004
722247f1
YQ
6005DECLARE_QUEUE_P (stop_reply_p);
6006DEFINE_QUEUE_P (stop_reply_p);
6007/* The list of already fetched and acknowledged stop events. This
6008 queue is used for notification Stop, and other notifications
6009 don't need queue for their events, because the notification events
6010 of Stop can't be consumed immediately, so that events should be
6011 queued first, and be consumed by remote_wait_{ns,as} one per
6012 time. Other notifications can consume their events immediately,
6013 so queue is not needed for them. */
6014static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
6015
6016static void
6017stop_reply_xfree (struct stop_reply *r)
6018{
f48ff2a7 6019 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
6020}
6021
221e1a37
PA
6022/* Return the length of the stop reply queue. */
6023
6024static int
6025stop_reply_queue_length (void)
6026{
6027 return QUEUE_length (stop_reply_p, stop_reply_queue);
6028}
6029
722247f1
YQ
6030static void
6031remote_notif_stop_parse (struct notif_client *self, char *buf,
6032 struct notif_event *event)
6033{
6034 remote_parse_stop_reply (buf, (struct stop_reply *) event);
6035}
6036
6037static void
6038remote_notif_stop_ack (struct notif_client *self, char *buf,
6039 struct notif_event *event)
6040{
6041 struct stop_reply *stop_reply = (struct stop_reply *) event;
6042
6043 /* acknowledge */
f5c4fcd9 6044 putpkt (self->ack_command);
722247f1
YQ
6045
6046 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
6047 /* We got an unknown stop reply. */
6048 error (_("Unknown stop reply"));
6049
6050 push_stop_reply (stop_reply);
6051}
6052
6053static int
6054remote_notif_stop_can_get_pending_events (struct notif_client *self)
6055{
6056 /* We can't get pending events in remote_notif_process for
6057 notification stop, and we have to do this in remote_wait_ns
6058 instead. If we fetch all queued events from stub, remote stub
6059 may exit and we have no chance to process them back in
6060 remote_wait_ns. */
6061 mark_async_event_handler (remote_async_inferior_event_token);
6062 return 0;
6063}
6064
6065static void
6066stop_reply_dtr (struct notif_event *event)
6067{
6068 struct stop_reply *r = (struct stop_reply *) event;
6069
6070 VEC_free (cached_reg_t, r->regcache);
6071}
6072
6073static struct notif_event *
6074remote_notif_stop_alloc_reply (void)
6075{
8d749320
SM
6076 /* We cast to a pointer to the "base class". */
6077 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
6078
6079 r->dtr = stop_reply_dtr;
6080
6081 return r;
6082}
6083
6084/* A client of notification Stop. */
6085
6086struct notif_client notif_client_stop =
6087{
6088 "Stop",
6089 "vStopped",
6090 remote_notif_stop_parse,
6091 remote_notif_stop_ack,
6092 remote_notif_stop_can_get_pending_events,
6093 remote_notif_stop_alloc_reply,
f48ff2a7 6094 REMOTE_NOTIF_STOP,
722247f1
YQ
6095};
6096
6097/* A parameter to pass data in and out. */
6098
6099struct queue_iter_param
6100{
6101 void *input;
6102 struct stop_reply *output;
6103};
6104
cbb8991c
DB
6105/* Determine if THREAD is a pending fork parent thread. ARG contains
6106 the pid of the process that owns the threads we want to check, or
6107 -1 if we want to check all threads. */
6108
6109static int
6110is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
6111 ptid_t thread_ptid)
6112{
6113 if (ws->kind == TARGET_WAITKIND_FORKED
6114 || ws->kind == TARGET_WAITKIND_VFORKED)
6115 {
6116 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
6117 return 1;
6118 }
6119
6120 return 0;
6121}
6122
6123/* Check whether EVENT is a fork event, and if it is, remove the
6124 fork child from the context list passed in DATA. */
6125
6126static int
6127remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
6128 QUEUE_ITER (stop_reply_p) *iter,
6129 stop_reply_p event,
6130 void *data)
6131{
19ba03f4
SM
6132 struct queue_iter_param *param = (struct queue_iter_param *) data;
6133 struct threads_listing_context *context
6134 = (struct threads_listing_context *) param->input;
cbb8991c
DB
6135
6136 if (event->ws.kind == TARGET_WAITKIND_FORKED
65706a29
PA
6137 || event->ws.kind == TARGET_WAITKIND_VFORKED
6138 || event->ws.kind == TARGET_WAITKIND_THREAD_EXITED)
6139 threads_listing_context_remove (&event->ws, context);
cbb8991c
DB
6140
6141 return 1;
6142}
6143
6144/* If CONTEXT contains any fork child threads that have not been
6145 reported yet, remove them from the CONTEXT list. If such a
6146 thread exists it is because we are stopped at a fork catchpoint
6147 and have not yet called follow_fork, which will set up the
6148 host-side data structures for the new process. */
6149
6150static void
6151remove_new_fork_children (struct threads_listing_context *context)
6152{
6153 struct thread_info * thread;
6154 int pid = -1;
6155 struct notif_client *notif = &notif_client_stop;
6156 struct queue_iter_param param;
6157
6158 /* For any threads stopped at a fork event, remove the corresponding
6159 fork child threads from the CONTEXT list. */
6160 ALL_NON_EXITED_THREADS (thread)
6161 {
4041ed77
DB
6162 struct target_waitstatus *ws;
6163
6164 if (thread->suspend.waitstatus_pending_p)
6165 ws = &thread->suspend.waitstatus;
6166 else
6167 ws = &thread->pending_follow;
cbb8991c
DB
6168
6169 if (is_pending_fork_parent (ws, pid, thread->ptid))
6170 {
6171 threads_listing_context_remove (ws, context);
6172 }
6173 }
6174
6175 /* Check for any pending fork events (not reported or processed yet)
6176 in process PID and remove those fork child threads from the
6177 CONTEXT list as well. */
6178 remote_notif_get_pending_events (notif);
6179 param.input = context;
6180 param.output = NULL;
6181 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6182 remove_child_of_pending_fork, &param);
6183}
6184
f48ff2a7
YQ
6185/* Remove stop replies in the queue if its pid is equal to the given
6186 inferior's pid. */
722247f1
YQ
6187
6188static int
f48ff2a7
YQ
6189remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
6190 QUEUE_ITER (stop_reply_p) *iter,
6191 stop_reply_p event,
6192 void *data)
722247f1 6193{
19ba03f4
SM
6194 struct queue_iter_param *param = (struct queue_iter_param *) data;
6195 struct inferior *inf = (struct inferior *) param->input;
722247f1 6196
f48ff2a7 6197 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
6198 {
6199 stop_reply_xfree (event);
6200 QUEUE_remove_elem (stop_reply_p, q, iter);
6201 }
6202
6203 return 1;
6204}
6205
f48ff2a7 6206/* Discard all pending stop replies of inferior INF. */
c906108c 6207
74531fed 6208static void
5f4cf0bb 6209discard_pending_stop_replies (struct inferior *inf)
c906108c 6210{
722247f1 6211 struct queue_iter_param param;
f48ff2a7
YQ
6212 struct stop_reply *reply;
6213 struct remote_state *rs = get_remote_state ();
6214 struct remote_notif_state *rns = rs->notif_state;
6215
6216 /* This function can be notified when an inferior exists. When the
6217 target is not remote, the notification state is NULL. */
6218 if (rs->remote_desc == NULL)
6219 return;
6220
6221 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 6222
74531fed 6223 /* Discard the in-flight notification. */
f48ff2a7 6224 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 6225 {
722247f1 6226 stop_reply_xfree (reply);
f48ff2a7 6227 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 6228 }
c906108c 6229
722247f1
YQ
6230 param.input = inf;
6231 param.output = NULL;
74531fed
PA
6232 /* Discard the stop replies we have already pulled with
6233 vStopped. */
722247f1 6234 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
6235 remove_stop_reply_for_inferior, &param);
6236}
6237
bcc75809
YQ
6238/* If its remote state is equal to the given remote state,
6239 remove EVENT from the stop reply queue. */
6240
6241static int
6242remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
6243 QUEUE_ITER (stop_reply_p) *iter,
6244 stop_reply_p event,
6245 void *data)
6246{
19ba03f4
SM
6247 struct queue_iter_param *param = (struct queue_iter_param *) data;
6248 struct remote_state *rs = (struct remote_state *) param->input;
bcc75809
YQ
6249
6250 if (event->rs == rs)
6251 {
6252 stop_reply_xfree (event);
6253 QUEUE_remove_elem (stop_reply_p, q, iter);
6254 }
6255
6256 return 1;
6257}
6258
6259/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
6260
6261static void
bcc75809 6262discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
6263{
6264 struct queue_iter_param param;
6265
bcc75809 6266 param.input = rs;
f48ff2a7
YQ
6267 param.output = NULL;
6268 /* Discard the stop replies we have already pulled with
6269 vStopped. */
6270 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 6271 remove_stop_reply_of_remote_state, &param);
722247f1 6272}
74531fed 6273
722247f1
YQ
6274/* A parameter to pass data in and out. */
6275
6276static int
6277remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
6278 QUEUE_ITER (stop_reply_p) *iter,
6279 stop_reply_p event,
6280 void *data)
6281{
19ba03f4
SM
6282 struct queue_iter_param *param = (struct queue_iter_param *) data;
6283 ptid_t *ptid = (ptid_t *) param->input;
722247f1
YQ
6284
6285 if (ptid_match (event->ptid, *ptid))
6286 {
6287 param->output = event;
6288 QUEUE_remove_elem (stop_reply_p, q, iter);
6289 return 0;
c8e38a49 6290 }
722247f1
YQ
6291
6292 return 1;
74531fed 6293}
43ff13b4 6294
722247f1
YQ
6295/* Remove the first reply in 'stop_reply_queue' which matches
6296 PTID. */
2e9f7625 6297
722247f1
YQ
6298static struct stop_reply *
6299remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 6300{
722247f1
YQ
6301 struct queue_iter_param param;
6302
6303 param.input = &ptid;
6304 param.output = NULL;
6305
6306 QUEUE_iterate (stop_reply_p, stop_reply_queue,
6307 remote_notif_remove_once_on_match, &param);
6308 if (notif_debug)
6309 fprintf_unfiltered (gdb_stdlog,
6310 "notif: discard queued event: 'Stop' in %s\n",
6311 target_pid_to_str (ptid));
a744cf53 6312
722247f1 6313 return param.output;
74531fed 6314}
75c99385 6315
74531fed
PA
6316/* Look for a queued stop reply belonging to PTID. If one is found,
6317 remove it from the queue, and return it. Returns NULL if none is
6318 found. If there are still queued events left to process, tell the
6319 event loop to get back to target_wait soon. */
e24a49d8 6320
74531fed
PA
6321static struct stop_reply *
6322queued_stop_reply (ptid_t ptid)
6323{
722247f1 6324 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 6325
722247f1 6326 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
6327 /* There's still at least an event left. */
6328 mark_async_event_handler (remote_async_inferior_event_token);
6329
722247f1 6330 return r;
74531fed
PA
6331}
6332
6333/* Push a fully parsed stop reply in the stop reply queue. Since we
6334 know that we now have at least one queued event left to pass to the
6335 core side, tell the event loop to get back to target_wait soon. */
6336
6337static void
6338push_stop_reply (struct stop_reply *new_event)
6339{
722247f1 6340 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 6341
722247f1
YQ
6342 if (notif_debug)
6343 fprintf_unfiltered (gdb_stdlog,
6344 "notif: push 'Stop' %s to queue %d\n",
6345 target_pid_to_str (new_event->ptid),
6346 QUEUE_length (stop_reply_p,
6347 stop_reply_queue));
74531fed
PA
6348
6349 mark_async_event_handler (remote_async_inferior_event_token);
6350}
6351
722247f1
YQ
6352static int
6353stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
6354 QUEUE_ITER (stop_reply_p) *iter,
6355 struct stop_reply *event,
6356 void *data)
6357{
19ba03f4 6358 ptid_t *ptid = (ptid_t *) data;
722247f1
YQ
6359
6360 return !(ptid_equal (*ptid, event->ptid)
6361 && event->ws.kind == TARGET_WAITKIND_STOPPED);
6362}
6363
74531fed
PA
6364/* Returns true if we have a stop reply for PTID. */
6365
6366static int
6367peek_stop_reply (ptid_t ptid)
6368{
722247f1
YQ
6369 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
6370 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
6371}
6372
26d56a93
SL
6373/* Helper for remote_parse_stop_reply. Return nonzero if the substring
6374 starting with P and ending with PEND matches PREFIX. */
6375
6376static int
6377strprefix (const char *p, const char *pend, const char *prefix)
6378{
6379 for ( ; p < pend; p++, prefix++)
6380 if (*p != *prefix)
6381 return 0;
6382 return *prefix == '\0';
6383}
6384
74531fed
PA
6385/* Parse the stop reply in BUF. Either the function succeeds, and the
6386 result is stored in EVENT, or throws an error. */
6387
6388static void
6389remote_parse_stop_reply (char *buf, struct stop_reply *event)
6390{
6391 struct remote_arch_state *rsa = get_remote_arch_state ();
6392 ULONGEST addr;
6393 char *p;
94585166 6394 int skipregs = 0;
74531fed
PA
6395
6396 event->ptid = null_ptid;
bcc75809 6397 event->rs = get_remote_state ();
74531fed
PA
6398 event->ws.kind = TARGET_WAITKIND_IGNORE;
6399 event->ws.value.integer = 0;
f7e6eed5 6400 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 6401 event->regcache = NULL;
dc146f7c 6402 event->core = -1;
74531fed
PA
6403
6404 switch (buf[0])
6405 {
6406 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
6407 /* Expedited reply, containing Signal, {regno, reg} repeat. */
6408 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
6409 ss = signal number
6410 n... = register number
6411 r... = register contents
6412 */
6413
6414 p = &buf[3]; /* after Txx */
6415 while (*p)
6416 {
6417 char *p1;
cea39f65 6418 int fieldsize;
43ff13b4 6419
1f10ba14
PA
6420 p1 = strchr (p, ':');
6421 if (p1 == NULL)
6422 error (_("Malformed packet(a) (missing colon): %s\n\
6423Packet: '%s'\n"),
6424 p, buf);
6425 if (p == p1)
6426 error (_("Malformed packet(a) (missing register number): %s\n\
6427Packet: '%s'\n"),
6428 p, buf);
3c3bea1c 6429
1f10ba14
PA
6430 /* Some "registers" are actually extended stop information.
6431 Note if you're adding a new entry here: GDB 7.9 and
6432 earlier assume that all register "numbers" that start
6433 with an hex digit are real register numbers. Make sure
6434 the server only sends such a packet if it knows the
6435 client understands it. */
c8e38a49 6436
26d56a93 6437 if (strprefix (p, p1, "thread"))
1f10ba14 6438 event->ptid = read_ptid (++p1, &p);
82075af2
JS
6439 else if (strprefix (p, p1, "syscall_entry"))
6440 {
6441 ULONGEST sysno;
6442
6443 event->ws.kind = TARGET_WAITKIND_SYSCALL_ENTRY;
6444 p = unpack_varlen_hex (++p1, &sysno);
6445 event->ws.value.syscall_number = (int) sysno;
6446 }
6447 else if (strprefix (p, p1, "syscall_return"))
6448 {
6449 ULONGEST sysno;
6450
6451 event->ws.kind = TARGET_WAITKIND_SYSCALL_RETURN;
6452 p = unpack_varlen_hex (++p1, &sysno);
6453 event->ws.value.syscall_number = (int) sysno;
6454 }
26d56a93
SL
6455 else if (strprefix (p, p1, "watch")
6456 || strprefix (p, p1, "rwatch")
6457 || strprefix (p, p1, "awatch"))
cea39f65 6458 {
f7e6eed5 6459 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
6460 p = unpack_varlen_hex (++p1, &addr);
6461 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 6462 }
26d56a93 6463 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
6464 {
6465 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
6466
6467 /* Make sure the stub doesn't forget to indicate support
6468 with qSupported. */
6469 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
6470 error (_("Unexpected swbreak stop reason"));
6471
6472 /* The value part is documented as "must be empty",
6473 though we ignore it, in case we ever decide to make
6474 use of it in a backward compatible way. */
8424cc97 6475 p = strchrnul (p1 + 1, ';');
f7e6eed5 6476 }
26d56a93 6477 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
6478 {
6479 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
6480
6481 /* Make sure the stub doesn't forget to indicate support
6482 with qSupported. */
6483 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
6484 error (_("Unexpected hwbreak stop reason"));
6485
6486 /* See above. */
8424cc97 6487 p = strchrnul (p1 + 1, ';');
f7e6eed5 6488 }
26d56a93 6489 else if (strprefix (p, p1, "library"))
cea39f65 6490 {
1f10ba14 6491 event->ws.kind = TARGET_WAITKIND_LOADED;
8424cc97 6492 p = strchrnul (p1 + 1, ';');
1f10ba14 6493 }
26d56a93 6494 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
6495 {
6496 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
6497 /* p1 will indicate "begin" or "end", but it makes
6498 no difference for now, so ignore it. */
8424cc97 6499 p = strchrnul (p1 + 1, ';');
1f10ba14 6500 }
26d56a93 6501 else if (strprefix (p, p1, "core"))
1f10ba14
PA
6502 {
6503 ULONGEST c;
a744cf53 6504
1f10ba14
PA
6505 p = unpack_varlen_hex (++p1, &c);
6506 event->core = c;
cea39f65 6507 }
26d56a93 6508 else if (strprefix (p, p1, "fork"))
de0d863e
DB
6509 {
6510 event->ws.value.related_pid = read_ptid (++p1, &p);
6511 event->ws.kind = TARGET_WAITKIND_FORKED;
6512 }
26d56a93 6513 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
6514 {
6515 event->ws.value.related_pid = read_ptid (++p1, &p);
6516 event->ws.kind = TARGET_WAITKIND_VFORKED;
6517 }
26d56a93 6518 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
6519 {
6520 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
8424cc97 6521 p = strchrnul (p1 + 1, ';');
c269dbdb 6522 }
6ab24463 6523 else if (strprefix (p, p1, "exec"))
94585166
DB
6524 {
6525 ULONGEST ignored;
6526 char pathname[PATH_MAX];
6527 int pathlen;
6528
6529 /* Determine the length of the execd pathname. */
6530 p = unpack_varlen_hex (++p1, &ignored);
6531 pathlen = (p - p1) / 2;
6532
6533 /* Save the pathname for event reporting and for
6534 the next run command. */
6535 hex2bin (p1, (gdb_byte *) pathname, pathlen);
6536 pathname[pathlen] = '\0';
6537
6538 /* This is freed during event handling. */
6539 event->ws.value.execd_pathname = xstrdup (pathname);
6540 event->ws.kind = TARGET_WAITKIND_EXECD;
6541
6542 /* Skip the registers included in this packet, since
6543 they may be for an architecture different from the
6544 one used by the original program. */
6545 skipregs = 1;
6546 }
65706a29
PA
6547 else if (strprefix (p, p1, "create"))
6548 {
6549 event->ws.kind = TARGET_WAITKIND_THREAD_CREATED;
8424cc97 6550 p = strchrnul (p1 + 1, ';');
65706a29 6551 }
cea39f65
MS
6552 else
6553 {
1f10ba14
PA
6554 ULONGEST pnum;
6555 char *p_temp;
6556
94585166
DB
6557 if (skipregs)
6558 {
8424cc97 6559 p = strchrnul (p1 + 1, ';');
94585166
DB
6560 p++;
6561 continue;
6562 }
6563
1f10ba14
PA
6564 /* Maybe a real ``P'' register number. */
6565 p_temp = unpack_varlen_hex (p, &pnum);
6566 /* If the first invalid character is the colon, we got a
6567 register number. Otherwise, it's an unknown stop
6568 reason. */
6569 if (p_temp == p1)
6570 {
6571 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
6572 cached_reg_t cached_reg;
43ff13b4 6573
1f10ba14
PA
6574 if (reg == NULL)
6575 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 6576Packet: '%s'\n"),
1f10ba14 6577 hex_string (pnum), p, buf);
c8e38a49 6578
1f10ba14 6579 cached_reg.num = reg->regnum;
4100683b 6580
1f10ba14
PA
6581 p = p1 + 1;
6582 fieldsize = hex2bin (p, cached_reg.data,
6583 register_size (target_gdbarch (),
6584 reg->regnum));
6585 p += 2 * fieldsize;
6586 if (fieldsize < register_size (target_gdbarch (),
6587 reg->regnum))
6588 warning (_("Remote reply is too short: %s"), buf);
74531fed 6589
1f10ba14
PA
6590 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
6591 }
6592 else
6593 {
6594 /* Not a number. Silently skip unknown optional
6595 info. */
8424cc97 6596 p = strchrnul (p1 + 1, ';');
1f10ba14 6597 }
cea39f65 6598 }
c8e38a49 6599
cea39f65
MS
6600 if (*p != ';')
6601 error (_("Remote register badly formatted: %s\nhere: %s"),
6602 buf, p);
6603 ++p;
6604 }
5b5596ff
PA
6605
6606 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
6607 break;
6608
c8e38a49
PA
6609 /* fall through */
6610 case 'S': /* Old style status, just signal only. */
3a09da41
PA
6611 {
6612 int sig;
6613
6614 event->ws.kind = TARGET_WAITKIND_STOPPED;
6615 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
6616 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
6617 event->ws.value.sig = (enum gdb_signal) sig;
6618 else
6619 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
6620 }
c8e38a49 6621 break;
65706a29
PA
6622 case 'w': /* Thread exited. */
6623 {
6624 char *p;
6625 ULONGEST value;
6626
6627 event->ws.kind = TARGET_WAITKIND_THREAD_EXITED;
6628 p = unpack_varlen_hex (&buf[1], &value);
6629 event->ws.value.integer = value;
6630 if (*p != ';')
6631 error (_("stop reply packet badly formatted: %s"), buf);
974eac9d 6632 event->ptid = read_ptid (++p, NULL);
65706a29
PA
6633 break;
6634 }
c8e38a49
PA
6635 case 'W': /* Target exited. */
6636 case 'X':
6637 {
6638 char *p;
6639 int pid;
6640 ULONGEST value;
82f73884 6641
c8e38a49
PA
6642 /* GDB used to accept only 2 hex chars here. Stubs should
6643 only send more if they detect GDB supports multi-process
6644 support. */
6645 p = unpack_varlen_hex (&buf[1], &value);
82f73884 6646
c8e38a49
PA
6647 if (buf[0] == 'W')
6648 {
6649 /* The remote process exited. */
74531fed
PA
6650 event->ws.kind = TARGET_WAITKIND_EXITED;
6651 event->ws.value.integer = value;
c8e38a49
PA
6652 }
6653 else
6654 {
6655 /* The remote process exited with a signal. */
74531fed 6656 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
6657 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
6658 event->ws.value.sig = (enum gdb_signal) value;
6659 else
6660 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 6661 }
82f73884 6662
c8e38a49
PA
6663 /* If no process is specified, assume inferior_ptid. */
6664 pid = ptid_get_pid (inferior_ptid);
6665 if (*p == '\0')
6666 ;
6667 else if (*p == ';')
6668 {
6669 p++;
6670
0b24eb2d 6671 if (*p == '\0')
82f73884 6672 ;
61012eef 6673 else if (startswith (p, "process:"))
82f73884 6674 {
c8e38a49 6675 ULONGEST upid;
a744cf53 6676
c8e38a49
PA
6677 p += sizeof ("process:") - 1;
6678 unpack_varlen_hex (p, &upid);
6679 pid = upid;
82f73884
PA
6680 }
6681 else
6682 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6683 }
c8e38a49
PA
6684 else
6685 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6686 event->ptid = pid_to_ptid (pid);
6687 }
6688 break;
f2faf941
PA
6689 case 'N':
6690 event->ws.kind = TARGET_WAITKIND_NO_RESUMED;
6691 event->ptid = minus_one_ptid;
6692 break;
74531fed
PA
6693 }
6694
6efcd9a8 6695 if (target_is_non_stop_p () && ptid_equal (event->ptid, null_ptid))
74531fed
PA
6696 error (_("No process or thread specified in stop reply: %s"), buf);
6697}
6698
722247f1
YQ
6699/* When the stub wants to tell GDB about a new notification reply, it
6700 sends a notification (%Stop, for example). Those can come it at
6701 any time, hence, we have to make sure that any pending
6702 putpkt/getpkt sequence we're making is finished, before querying
6703 the stub for more events with the corresponding ack command
6704 (vStopped, for example). E.g., if we started a vStopped sequence
6705 immediately upon receiving the notification, something like this
6706 could happen:
74531fed
PA
6707
6708 1.1) --> Hg 1
6709 1.2) <-- OK
6710 1.3) --> g
6711 1.4) <-- %Stop
6712 1.5) --> vStopped
6713 1.6) <-- (registers reply to step #1.3)
6714
6715 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6716 query.
6717
796cb314 6718 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6719 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6720 doing whatever we were doing:
6721
6722 2.1) --> Hg 1
6723 2.2) <-- OK
6724 2.3) --> g
6725 2.4) <-- %Stop
6726 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6727 2.5) <-- (registers reply to step #2.3)
6728
6729 Eventualy after step #2.5, we return to the event loop, which
6730 notices there's an event on the
6731 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6732 associated callback --- the function below. At this point, we're
6733 always safe to start a vStopped sequence. :
6734
6735 2.6) --> vStopped
6736 2.7) <-- T05 thread:2
6737 2.8) --> vStopped
6738 2.9) --> OK
6739*/
6740
722247f1
YQ
6741void
6742remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6743{
6744 struct remote_state *rs = get_remote_state ();
74531fed 6745
f48ff2a7 6746 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6747 {
722247f1
YQ
6748 if (notif_debug)
6749 fprintf_unfiltered (gdb_stdlog,
6750 "notif: process: '%s' ack pending event\n",
6751 nc->name);
74531fed 6752
722247f1 6753 /* acknowledge */
f48ff2a7
YQ
6754 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6755 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6756
6757 while (1)
6758 {
6759 getpkt (&rs->buf, &rs->buf_size, 0);
6760 if (strcmp (rs->buf, "OK") == 0)
6761 break;
6762 else
722247f1 6763 remote_notif_ack (nc, rs->buf);
74531fed
PA
6764 }
6765 }
722247f1
YQ
6766 else
6767 {
6768 if (notif_debug)
6769 fprintf_unfiltered (gdb_stdlog,
6770 "notif: process: '%s' no pending reply\n",
6771 nc->name);
6772 }
74531fed
PA
6773}
6774
74531fed
PA
6775/* Called when it is decided that STOP_REPLY holds the info of the
6776 event that is to be returned to the core. This function always
6777 destroys STOP_REPLY. */
6778
6779static ptid_t
6780process_stop_reply (struct stop_reply *stop_reply,
6781 struct target_waitstatus *status)
6782{
6783 ptid_t ptid;
6784
6785 *status = stop_reply->ws;
6786 ptid = stop_reply->ptid;
6787
6788 /* If no thread/process was reported by the stub, assume the current
6789 inferior. */
6790 if (ptid_equal (ptid, null_ptid))
6791 ptid = inferior_ptid;
6792
5f3563ea 6793 if (status->kind != TARGET_WAITKIND_EXITED
f2faf941
PA
6794 && status->kind != TARGET_WAITKIND_SIGNALLED
6795 && status->kind != TARGET_WAITKIND_NO_RESUMED)
74531fed 6796 {
799a2abe 6797 struct private_thread_info *remote_thr;
ee154bee 6798
5f3563ea
PA
6799 /* Expedited registers. */
6800 if (stop_reply->regcache)
6801 {
217f1f79 6802 struct regcache *regcache
f5656ead 6803 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6804 cached_reg_t *reg;
6805 int ix;
6806
6807 for (ix = 0;
6808 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6809 ix++)
217f1f79 6810 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6811 VEC_free (cached_reg_t, stop_reply->regcache);
6812 }
74531fed 6813
1941c569 6814 remote_notice_new_inferior (ptid, 0);
799a2abe
PA
6815 remote_thr = demand_private_info (ptid);
6816 remote_thr->core = stop_reply->core;
6817 remote_thr->stop_reason = stop_reply->stop_reason;
6818 remote_thr->watch_data_address = stop_reply->watch_data_address;
74531fed
PA
6819 }
6820
74531fed
PA
6821 stop_reply_xfree (stop_reply);
6822 return ptid;
6823}
6824
6825/* The non-stop mode version of target_wait. */
6826
6827static ptid_t
47608cb1 6828remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6829{
6830 struct remote_state *rs = get_remote_state ();
74531fed
PA
6831 struct stop_reply *stop_reply;
6832 int ret;
fee9eda9 6833 int is_notif = 0;
74531fed
PA
6834
6835 /* If in non-stop mode, get out of getpkt even if a
6836 notification is received. */
6837
6838 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6839 0 /* forever */, &is_notif);
74531fed
PA
6840 while (1)
6841 {
fee9eda9 6842 if (ret != -1 && !is_notif)
74531fed
PA
6843 switch (rs->buf[0])
6844 {
6845 case 'E': /* Error of some sort. */
6846 /* We're out of sync with the target now. Did it continue
6847 or not? We can't tell which thread it was in non-stop,
6848 so just ignore this. */
6849 warning (_("Remote failure reply: %s"), rs->buf);
6850 break;
6851 case 'O': /* Console output. */
6852 remote_console_output (rs->buf + 1);
6853 break;
6854 default:
6855 warning (_("Invalid remote reply: %s"), rs->buf);
6856 break;
6857 }
6858
6859 /* Acknowledge a pending stop reply that may have arrived in the
6860 mean time. */
f48ff2a7 6861 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6862 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6863
6864 /* If indeed we noticed a stop reply, we're done. */
6865 stop_reply = queued_stop_reply (ptid);
6866 if (stop_reply != NULL)
6867 return process_stop_reply (stop_reply, status);
6868
47608cb1 6869 /* Still no event. If we're just polling for an event, then
74531fed 6870 return to the event loop. */
47608cb1 6871 if (options & TARGET_WNOHANG)
74531fed
PA
6872 {
6873 status->kind = TARGET_WAITKIND_IGNORE;
6874 return minus_one_ptid;
6875 }
6876
47608cb1 6877 /* Otherwise do a blocking wait. */
74531fed 6878 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6879 1 /* forever */, &is_notif);
74531fed
PA
6880 }
6881}
6882
6883/* Wait until the remote machine stops, then return, storing status in
6884 STATUS just as `wait' would. */
6885
6886static ptid_t
47608cb1 6887remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6888{
6889 struct remote_state *rs = get_remote_state ();
74531fed 6890 ptid_t event_ptid = null_ptid;
cea39f65 6891 char *buf;
74531fed
PA
6892 struct stop_reply *stop_reply;
6893
47608cb1
PA
6894 again:
6895
74531fed
PA
6896 status->kind = TARGET_WAITKIND_IGNORE;
6897 status->value.integer = 0;
6898
6899 stop_reply = queued_stop_reply (ptid);
6900 if (stop_reply != NULL)
6901 return process_stop_reply (stop_reply, status);
6902
6903 if (rs->cached_wait_status)
6904 /* Use the cached wait status, but only once. */
6905 rs->cached_wait_status = 0;
6906 else
6907 {
6908 int ret;
722247f1 6909 int is_notif;
567420d1
PA
6910 int forever = ((options & TARGET_WNOHANG) == 0
6911 && wait_forever_enabled_p);
6912
6913 if (!rs->waiting_for_stop_reply)
6914 {
6915 status->kind = TARGET_WAITKIND_NO_RESUMED;
6916 return minus_one_ptid;
6917 }
74531fed 6918
74531fed
PA
6919 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6920 _never_ wait for ever -> test on target_is_async_p().
6921 However, before we do that we need to ensure that the caller
6922 knows how to take the target into/out of async mode. */
722247f1 6923 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 6924 forever, &is_notif);
722247f1
YQ
6925
6926 /* GDB gets a notification. Return to core as this event is
6927 not interesting. */
6928 if (ret != -1 && is_notif)
6929 return minus_one_ptid;
567420d1
PA
6930
6931 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
6932 return minus_one_ptid;
74531fed
PA
6933 }
6934
6935 buf = rs->buf;
6936
3a29589a
DJ
6937 /* Assume that the target has acknowledged Ctrl-C unless we receive
6938 an 'F' or 'O' packet. */
6939 if (buf[0] != 'F' && buf[0] != 'O')
6940 rs->ctrlc_pending_p = 0;
6941
74531fed
PA
6942 switch (buf[0])
6943 {
6944 case 'E': /* Error of some sort. */
6945 /* We're out of sync with the target now. Did it continue or
6946 not? Not is more likely, so report a stop. */
29090fb6
LM
6947 rs->waiting_for_stop_reply = 0;
6948
74531fed
PA
6949 warning (_("Remote failure reply: %s"), buf);
6950 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6951 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6952 break;
6953 case 'F': /* File-I/O request. */
e42e5352
YQ
6954 /* GDB may access the inferior memory while handling the File-I/O
6955 request, but we don't want GDB accessing memory while waiting
6956 for a stop reply. See the comments in putpkt_binary. Set
6957 waiting_for_stop_reply to 0 temporarily. */
6958 rs->waiting_for_stop_reply = 0;
3a29589a
DJ
6959 remote_fileio_request (buf, rs->ctrlc_pending_p);
6960 rs->ctrlc_pending_p = 0;
e42e5352
YQ
6961 /* GDB handled the File-I/O request, and the target is running
6962 again. Keep waiting for events. */
6963 rs->waiting_for_stop_reply = 1;
74531fed 6964 break;
f2faf941 6965 case 'N': case 'T': case 'S': case 'X': case 'W':
74531fed 6966 {
29090fb6
LM
6967 struct stop_reply *stop_reply;
6968
6969 /* There is a stop reply to handle. */
6970 rs->waiting_for_stop_reply = 0;
6971
6972 stop_reply
722247f1
YQ
6973 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6974 rs->buf);
74531fed 6975
74531fed 6976 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6977 break;
6978 }
6979 case 'O': /* Console output. */
6980 remote_console_output (buf + 1);
c8e38a49
PA
6981 break;
6982 case '\0':
b73be471 6983 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6984 {
6985 /* Zero length reply means that we tried 'S' or 'C' and the
6986 remote system doesn't support it. */
6987 target_terminal_ours_for_output ();
6988 printf_filtered
6989 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6990 gdb_signal_to_name (rs->last_sent_signal));
6991 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6992 target_terminal_inferior ();
6993
f5c4fcd9
TT
6994 strcpy (buf, rs->last_sent_step ? "s" : "c");
6995 putpkt (buf);
c8e38a49 6996 break;
43ff13b4 6997 }
c8e38a49
PA
6998 /* else fallthrough */
6999 default:
7000 warning (_("Invalid remote reply: %s"), buf);
c8e38a49 7001 break;
43ff13b4 7002 }
c8e38a49 7003
f2faf941
PA
7004 if (status->kind == TARGET_WAITKIND_NO_RESUMED)
7005 return minus_one_ptid;
7006 else if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
7007 {
7008 /* Nothing interesting happened. If we're doing a non-blocking
7009 poll, we're done. Otherwise, go back to waiting. */
7010 if (options & TARGET_WNOHANG)
7011 return minus_one_ptid;
7012 else
7013 goto again;
7014 }
74531fed
PA
7015 else if (status->kind != TARGET_WAITKIND_EXITED
7016 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
7017 {
7018 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 7019 record_currthread (rs, event_ptid);
82f73884
PA
7020 else
7021 event_ptid = inferior_ptid;
43ff13b4 7022 }
74531fed
PA
7023 else
7024 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 7025 record_currthread (rs, minus_one_ptid);
79d7f229 7026
82f73884 7027 return event_ptid;
43ff13b4
JM
7028}
7029
74531fed
PA
7030/* Wait until the remote machine stops, then return, storing status in
7031 STATUS just as `wait' would. */
7032
c8e38a49 7033static ptid_t
117de6a9 7034remote_wait (struct target_ops *ops,
47608cb1 7035 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
7036{
7037 ptid_t event_ptid;
7038
6efcd9a8 7039 if (target_is_non_stop_p ())
47608cb1 7040 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 7041 else
47608cb1 7042 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 7043
d9d41e78 7044 if (target_is_async_p ())
c8e38a49 7045 {
74531fed
PA
7046 /* If there are are events left in the queue tell the event loop
7047 to return here. */
722247f1 7048 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 7049 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 7050 }
c8e38a49
PA
7051
7052 return event_ptid;
7053}
7054
74ca34ce 7055/* Fetch a single register using a 'p' packet. */
c906108c 7056
b96ec7ac 7057static int
56be3814 7058fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
7059{
7060 struct remote_state *rs = get_remote_state ();
2e9f7625 7061 char *buf, *p;
b96ec7ac
AC
7062 char regp[MAX_REGISTER_SIZE];
7063 int i;
7064
4082afcc 7065 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
7066 return 0;
7067
7068 if (reg->pnum == -1)
7069 return 0;
7070
2e9f7625 7071 p = rs->buf;
fcad0fa4 7072 *p++ = 'p';
74ca34ce 7073 p += hexnumstr (p, reg->pnum);
fcad0fa4 7074 *p++ = '\0';
1f4437a4
MS
7075 putpkt (rs->buf);
7076 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 7077
2e9f7625
DJ
7078 buf = rs->buf;
7079
74ca34ce
DJ
7080 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
7081 {
7082 case PACKET_OK:
7083 break;
7084 case PACKET_UNKNOWN:
7085 return 0;
7086 case PACKET_ERROR:
27a9c0bf
MS
7087 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
7088 gdbarch_register_name (get_regcache_arch (regcache),
7089 reg->regnum),
7090 buf);
74ca34ce 7091 }
3f9a994c
JB
7092
7093 /* If this register is unfetchable, tell the regcache. */
7094 if (buf[0] == 'x')
8480adf2 7095 {
56be3814 7096 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 7097 return 1;
b96ec7ac 7098 }
b96ec7ac 7099
3f9a994c
JB
7100 /* Otherwise, parse and supply the value. */
7101 p = buf;
7102 i = 0;
7103 while (p[0] != 0)
7104 {
7105 if (p[1] == 0)
74ca34ce 7106 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
7107
7108 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
7109 p += 2;
7110 }
56be3814 7111 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 7112 return 1;
b96ec7ac
AC
7113}
7114
74ca34ce
DJ
7115/* Fetch the registers included in the target's 'g' packet. */
7116
29709017
DJ
7117static int
7118send_g_packet (void)
c906108c 7119{
d01949b6 7120 struct remote_state *rs = get_remote_state ();
cea39f65 7121 int buf_len;
c906108c 7122
bba74b36 7123 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 7124 remote_send (&rs->buf, &rs->buf_size);
c906108c 7125
29709017
DJ
7126 /* We can get out of synch in various cases. If the first character
7127 in the buffer is not a hex character, assume that has happened
7128 and try to fetch another packet to read. */
7129 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
7130 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
7131 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
7132 && rs->buf[0] != 'x') /* New: unavailable register value. */
7133 {
7134 if (remote_debug)
7135 fprintf_unfiltered (gdb_stdlog,
7136 "Bad register packet; fetching a new packet\n");
7137 getpkt (&rs->buf, &rs->buf_size, 0);
7138 }
7139
74ca34ce
DJ
7140 buf_len = strlen (rs->buf);
7141
7142 /* Sanity check the received packet. */
7143 if (buf_len % 2 != 0)
7144 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
7145
7146 return buf_len / 2;
7147}
7148
7149static void
56be3814 7150process_g_packet (struct regcache *regcache)
29709017 7151{
4a22f64d 7152 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
7153 struct remote_state *rs = get_remote_state ();
7154 struct remote_arch_state *rsa = get_remote_arch_state ();
7155 int i, buf_len;
7156 char *p;
7157 char *regs;
7158
7159 buf_len = strlen (rs->buf);
7160
7161 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
7162 if (buf_len > 2 * rsa->sizeof_g_packet)
7163 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
7164
7165 /* Save the size of the packet sent to us by the target. It is used
7166 as a heuristic when determining the max size of packets that the
7167 target can safely receive. */
7168 if (rsa->actual_register_packet_size == 0)
7169 rsa->actual_register_packet_size = buf_len;
7170
7171 /* If this is smaller than we guessed the 'g' packet would be,
7172 update our records. A 'g' reply that doesn't include a register's
7173 value implies either that the register is not available, or that
7174 the 'p' packet must be used. */
7175 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 7176 {
74ca34ce
DJ
7177 rsa->sizeof_g_packet = buf_len / 2;
7178
4a22f64d 7179 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 7180 {
74ca34ce
DJ
7181 if (rsa->regs[i].pnum == -1)
7182 continue;
7183
7184 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
7185 rsa->regs[i].in_g_packet = 0;
b96ec7ac 7186 else
74ca34ce 7187 rsa->regs[i].in_g_packet = 1;
b96ec7ac 7188 }
74ca34ce 7189 }
b323314b 7190
224c3ddb 7191 regs = (char *) alloca (rsa->sizeof_g_packet);
c906108c
SS
7192
7193 /* Unimplemented registers read as all bits zero. */
ea9c271d 7194 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 7195
c906108c
SS
7196 /* Reply describes registers byte by byte, each byte encoded as two
7197 hex characters. Suck them all up, then supply them to the
7198 register cacheing/storage mechanism. */
7199
74ca34ce 7200 p = rs->buf;
ea9c271d 7201 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 7202 {
74ca34ce
DJ
7203 if (p[0] == 0 || p[1] == 0)
7204 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
7205 internal_error (__FILE__, __LINE__,
9b20d036 7206 _("unexpected end of 'g' packet reply"));
74ca34ce 7207
c906108c 7208 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 7209 regs[i] = 0; /* 'x' */
c906108c
SS
7210 else
7211 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
7212 p += 2;
7213 }
7214
a744cf53
MS
7215 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
7216 {
7217 struct packet_reg *r = &rsa->regs[i];
7218
7219 if (r->in_g_packet)
7220 {
7221 if (r->offset * 2 >= strlen (rs->buf))
7222 /* This shouldn't happen - we adjusted in_g_packet above. */
7223 internal_error (__FILE__, __LINE__,
9b20d036 7224 _("unexpected end of 'g' packet reply"));
a744cf53
MS
7225 else if (rs->buf[r->offset * 2] == 'x')
7226 {
7227 gdb_assert (r->offset * 2 < strlen (rs->buf));
7228 /* The register isn't available, mark it as such (at
7229 the same time setting the value to zero). */
7230 regcache_raw_supply (regcache, r->regnum, NULL);
7231 }
7232 else
7233 regcache_raw_supply (regcache, r->regnum,
7234 regs + r->offset);
7235 }
7236 }
c906108c
SS
7237}
7238
29709017 7239static void
56be3814 7240fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
7241{
7242 send_g_packet ();
56be3814 7243 process_g_packet (regcache);
29709017
DJ
7244}
7245
e6e4e701
PA
7246/* Make the remote selected traceframe match GDB's selected
7247 traceframe. */
7248
7249static void
7250set_remote_traceframe (void)
7251{
7252 int newnum;
262e1174 7253 struct remote_state *rs = get_remote_state ();
e6e4e701 7254
262e1174 7255 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
7256 return;
7257
7258 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 7259 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
7260
7261 newnum = target_trace_find (tfind_number,
7262 get_traceframe_number (), 0, 0, NULL);
7263
7264 /* Should not happen. If it does, all bets are off. */
7265 if (newnum != get_traceframe_number ())
7266 warning (_("could not set remote traceframe"));
7267}
7268
74ca34ce 7269static void
28439f5e
PA
7270remote_fetch_registers (struct target_ops *ops,
7271 struct regcache *regcache, int regnum)
74ca34ce 7272{
74ca34ce
DJ
7273 struct remote_arch_state *rsa = get_remote_arch_state ();
7274 int i;
7275
e6e4e701 7276 set_remote_traceframe ();
79d7f229 7277 set_general_thread (inferior_ptid);
74ca34ce
DJ
7278
7279 if (regnum >= 0)
7280 {
7281 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 7282
74ca34ce
DJ
7283 gdb_assert (reg != NULL);
7284
7285 /* If this register might be in the 'g' packet, try that first -
7286 we are likely to read more than one register. If this is the
7287 first 'g' packet, we might be overly optimistic about its
7288 contents, so fall back to 'p'. */
7289 if (reg->in_g_packet)
7290 {
56be3814 7291 fetch_registers_using_g (regcache);
74ca34ce
DJ
7292 if (reg->in_g_packet)
7293 return;
7294 }
7295
56be3814 7296 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
7297 return;
7298
7299 /* This register is not available. */
56be3814 7300 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
7301
7302 return;
7303 }
7304
56be3814 7305 fetch_registers_using_g (regcache);
74ca34ce 7306
4a22f64d 7307 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 7308 if (!rsa->regs[i].in_g_packet)
56be3814 7309 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
7310 {
7311 /* This register is not available. */
56be3814 7312 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
7313 }
7314}
7315
c906108c
SS
7316/* Prepare to store registers. Since we may send them all (using a
7317 'G' request), we have to read out the ones we don't want to change
7318 first. */
7319
c5aa993b 7320static void
f32dbf8c 7321remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 7322{
ea9c271d 7323 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 7324 int i;
cfd77fa1 7325 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 7326
c906108c 7327 /* Make sure the entire registers array is valid. */
4082afcc 7328 switch (packet_support (PACKET_P))
5a2468f5
JM
7329 {
7330 case PACKET_DISABLE:
7331 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 7332 /* Make sure all the necessary registers are cached. */
4a22f64d 7333 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 7334 if (rsa->regs[i].in_g_packet)
316f2060 7335 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
7336 break;
7337 case PACKET_ENABLE:
7338 break;
7339 }
7340}
7341
ad10f812 7342/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 7343 packet was not recognized. */
5a2468f5
JM
7344
7345static int
1f4437a4
MS
7346store_register_using_P (const struct regcache *regcache,
7347 struct packet_reg *reg)
5a2468f5 7348{
4a22f64d 7349 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 7350 struct remote_state *rs = get_remote_state ();
5a2468f5 7351 /* Try storing a single register. */
6d820c5c 7352 char *buf = rs->buf;
cfd77fa1 7353 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 7354 char *p;
5a2468f5 7355
4082afcc 7356 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
7357 return 0;
7358
7359 if (reg->pnum == -1)
7360 return 0;
7361
ea9c271d 7362 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 7363 p = buf + strlen (buf);
56be3814 7364 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 7365 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
7366 putpkt (rs->buf);
7367 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 7368
74ca34ce
DJ
7369 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
7370 {
7371 case PACKET_OK:
7372 return 1;
7373 case PACKET_ERROR:
27a9c0bf
MS
7374 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
7375 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
7376 case PACKET_UNKNOWN:
7377 return 0;
7378 default:
7379 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7380 }
c906108c
SS
7381}
7382
23860348
MS
7383/* Store register REGNUM, or all registers if REGNUM == -1, from the
7384 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
7385
7386static void
56be3814 7387store_registers_using_G (const struct regcache *regcache)
c906108c 7388{
d01949b6 7389 struct remote_state *rs = get_remote_state ();
ea9c271d 7390 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 7391 gdb_byte *regs;
c906108c
SS
7392 char *p;
7393
193cb69f
AC
7394 /* Extract all the registers in the regcache copying them into a
7395 local buffer. */
7396 {
b323314b 7397 int i;
a744cf53 7398
224c3ddb 7399 regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
ea9c271d 7400 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 7401 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 7402 {
ea9c271d 7403 struct packet_reg *r = &rsa->regs[i];
a744cf53 7404
b323314b 7405 if (r->in_g_packet)
56be3814 7406 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
7407 }
7408 }
c906108c
SS
7409
7410 /* Command describes registers byte by byte,
7411 each byte encoded as two hex characters. */
6d820c5c 7412 p = rs->buf;
193cb69f 7413 *p++ = 'G';
74ca34ce
DJ
7414 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
7415 updated. */
7416 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
7417 putpkt (rs->buf);
7418 getpkt (&rs->buf, &rs->buf_size, 0);
7419 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
7420 error (_("Could not write registers; remote failure reply '%s'"),
7421 rs->buf);
c906108c 7422}
74ca34ce
DJ
7423
7424/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
7425 of the register cache buffer. FIXME: ignores errors. */
7426
7427static void
28439f5e
PA
7428remote_store_registers (struct target_ops *ops,
7429 struct regcache *regcache, int regnum)
74ca34ce 7430{
74ca34ce
DJ
7431 struct remote_arch_state *rsa = get_remote_arch_state ();
7432 int i;
7433
e6e4e701 7434 set_remote_traceframe ();
79d7f229 7435 set_general_thread (inferior_ptid);
74ca34ce
DJ
7436
7437 if (regnum >= 0)
7438 {
7439 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 7440
74ca34ce
DJ
7441 gdb_assert (reg != NULL);
7442
7443 /* Always prefer to store registers using the 'P' packet if
7444 possible; we often change only a small number of registers.
7445 Sometimes we change a larger number; we'd need help from a
7446 higher layer to know to use 'G'. */
56be3814 7447 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
7448 return;
7449
7450 /* For now, don't complain if we have no way to write the
7451 register. GDB loses track of unavailable registers too
7452 easily. Some day, this may be an error. We don't have
0df8b418 7453 any way to read the register, either... */
74ca34ce
DJ
7454 if (!reg->in_g_packet)
7455 return;
7456
56be3814 7457 store_registers_using_G (regcache);
74ca34ce
DJ
7458 return;
7459 }
7460
56be3814 7461 store_registers_using_G (regcache);
74ca34ce 7462
4a22f64d 7463 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 7464 if (!rsa->regs[i].in_g_packet)
56be3814 7465 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
7466 /* See above for why we do not issue an error here. */
7467 continue;
7468}
c906108c
SS
7469\f
7470
7471/* Return the number of hex digits in num. */
7472
7473static int
fba45db2 7474hexnumlen (ULONGEST num)
c906108c
SS
7475{
7476 int i;
7477
7478 for (i = 0; num != 0; i++)
7479 num >>= 4;
7480
325fac50 7481 return std::max (i, 1);
c906108c
SS
7482}
7483
2df3850c 7484/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
7485
7486static int
fba45db2 7487hexnumstr (char *buf, ULONGEST num)
c906108c 7488{
c906108c 7489 int len = hexnumlen (num);
a744cf53 7490
2df3850c
JM
7491 return hexnumnstr (buf, num, len);
7492}
7493
c906108c 7494
2df3850c 7495/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 7496
2df3850c 7497static int
fba45db2 7498hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
7499{
7500 int i;
7501
7502 buf[width] = '\0';
7503
7504 for (i = width - 1; i >= 0; i--)
c906108c 7505 {
c5aa993b 7506 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
7507 num >>= 4;
7508 }
7509
2df3850c 7510 return width;
c906108c
SS
7511}
7512
23860348 7513/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
7514
7515static CORE_ADDR
fba45db2 7516remote_address_masked (CORE_ADDR addr)
c906108c 7517{
883b9c6c 7518 unsigned int address_size = remote_address_size;
a744cf53 7519
911c95a5
UW
7520 /* If "remoteaddresssize" was not set, default to target address size. */
7521 if (!address_size)
f5656ead 7522 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
7523
7524 if (address_size > 0
7525 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
7526 {
7527 /* Only create a mask when that mask can safely be constructed
23860348 7528 in a ULONGEST variable. */
c906108c 7529 ULONGEST mask = 1;
a744cf53 7530
911c95a5 7531 mask = (mask << address_size) - 1;
c906108c
SS
7532 addr &= mask;
7533 }
7534 return addr;
7535}
7536
7537/* Determine whether the remote target supports binary downloading.
7538 This is accomplished by sending a no-op memory write of zero length
7539 to the target at the specified address. It does not suffice to send
23860348
MS
7540 the whole packet, since many stubs strip the eighth bit and
7541 subsequently compute a wrong checksum, which causes real havoc with
7542 remote_write_bytes.
7a292a7a 7543
96baa820 7544 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 7545 clean. In cases like this, the user should clear "remote
23860348 7546 X-packet". */
96baa820 7547
c906108c 7548static void
fba45db2 7549check_binary_download (CORE_ADDR addr)
c906108c 7550{
d01949b6 7551 struct remote_state *rs = get_remote_state ();
24b06219 7552
4082afcc 7553 switch (packet_support (PACKET_X))
c906108c 7554 {
96baa820
JM
7555 case PACKET_DISABLE:
7556 break;
7557 case PACKET_ENABLE:
7558 break;
7559 case PACKET_SUPPORT_UNKNOWN:
7560 {
96baa820 7561 char *p;
802188a7 7562
2e9f7625 7563 p = rs->buf;
96baa820
JM
7564 *p++ = 'X';
7565 p += hexnumstr (p, (ULONGEST) addr);
7566 *p++ = ',';
7567 p += hexnumstr (p, (ULONGEST) 0);
7568 *p++ = ':';
7569 *p = '\0';
802188a7 7570
2e9f7625 7571 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7572 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 7573
2e9f7625 7574 if (rs->buf[0] == '\0')
96baa820
JM
7575 {
7576 if (remote_debug)
7577 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7578 "binary downloading NOT "
7579 "supported by target\n");
444abaca 7580 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
7581 }
7582 else
7583 {
7584 if (remote_debug)
7585 fprintf_unfiltered (gdb_stdlog,
64b9b334 7586 "binary downloading supported by target\n");
444abaca 7587 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
7588 }
7589 break;
7590 }
c906108c
SS
7591 }
7592}
7593
124e13d9
SM
7594/* Helper function to resize the payload in order to try to get a good
7595 alignment. We try to write an amount of data such that the next write will
7596 start on an address aligned on REMOTE_ALIGN_WRITES. */
7597
7598static int
7599align_for_efficient_write (int todo, CORE_ADDR memaddr)
7600{
7601 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
7602}
7603
c906108c
SS
7604/* Write memory data directly to the remote machine.
7605 This does not inform the data cache; the data cache uses this.
a76d924d 7606 HEADER is the starting part of the packet.
c906108c
SS
7607 MEMADDR is the address in the remote memory space.
7608 MYADDR is the address of the buffer in our space.
124e13d9
SM
7609 LEN_UNITS is the number of addressable units to write.
7610 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
7611 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
7612 should send data as binary ('X'), or hex-encoded ('M').
7613
7614 The function creates packet of the form
7615 <HEADER><ADDRESS>,<LENGTH>:<DATA>
7616
124e13d9 7617 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
7618
7619 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
7620 are omitted.
7621
9b409511 7622 Return the transferred status, error or OK (an
124e13d9
SM
7623 'enum target_xfer_status' value). Save the number of addressable units
7624 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
7625
7626 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
7627 exchange between gdb and the stub could look like (?? in place of the
7628 checksum):
7629
7630 -> $m1000,4#??
7631 <- aaaabbbbccccdddd
7632
7633 -> $M1000,3:eeeeffffeeee#??
7634 <- OK
7635
7636 -> $m1000,4#??
7637 <- eeeeffffeeeedddd */
c906108c 7638
9b409511 7639static enum target_xfer_status
a76d924d 7640remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
7641 const gdb_byte *myaddr, ULONGEST len_units,
7642 int unit_size, ULONGEST *xfered_len_units,
7643 char packet_format, int use_length)
c906108c 7644{
6d820c5c 7645 struct remote_state *rs = get_remote_state ();
cfd77fa1 7646 char *p;
a76d924d
DJ
7647 char *plen = NULL;
7648 int plenlen = 0;
124e13d9
SM
7649 int todo_units;
7650 int units_written;
7651 int payload_capacity_bytes;
7652 int payload_length_bytes;
a76d924d
DJ
7653
7654 if (packet_format != 'X' && packet_format != 'M')
7655 internal_error (__FILE__, __LINE__,
9b20d036 7656 _("remote_write_bytes_aux: bad packet format"));
c906108c 7657
124e13d9 7658 if (len_units == 0)
9b409511 7659 return TARGET_XFER_EOF;
b2182ed2 7660
124e13d9 7661 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 7662
6d820c5c
DJ
7663 /* The packet buffer will be large enough for the payload;
7664 get_memory_packet_size ensures this. */
a76d924d 7665 rs->buf[0] = '\0';
c906108c 7666
a257b5bb 7667 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
7668 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7669
124e13d9 7670 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7671 if (!use_length)
0df8b418 7672 /* The comma won't be used. */
124e13d9
SM
7673 payload_capacity_bytes += 1;
7674 payload_capacity_bytes -= strlen (header);
7675 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7676
a76d924d 7677 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7678
a76d924d
DJ
7679 strcat (rs->buf, header);
7680 p = rs->buf + strlen (header);
7681
7682 /* Compute a best guess of the number of bytes actually transfered. */
7683 if (packet_format == 'X')
c906108c 7684 {
23860348 7685 /* Best guess at number of bytes that will fit. */
325fac50
PA
7686 todo_units = std::min (len_units,
7687 (ULONGEST) payload_capacity_bytes / unit_size);
a76d924d 7688 if (use_length)
124e13d9 7689 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50 7690 todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7691 }
7692 else
7693 {
124e13d9 7694 /* Number of bytes that will fit. */
325fac50
PA
7695 todo_units
7696 = std::min (len_units,
7697 (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
a76d924d 7698 if (use_length)
124e13d9 7699 payload_capacity_bytes -= hexnumlen (todo_units);
325fac50
PA
7700 todo_units = std::min (todo_units,
7701 (payload_capacity_bytes / unit_size) / 2);
917317f4 7702 }
a76d924d 7703
124e13d9 7704 if (todo_units <= 0)
3de11b2e 7705 internal_error (__FILE__, __LINE__,
405f8e94 7706 _("minimum packet size too small to write data"));
802188a7 7707
6765f3e5
DJ
7708 /* If we already need another packet, then try to align the end
7709 of this packet to a useful boundary. */
124e13d9
SM
7710 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7711 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7712
a257b5bb 7713 /* Append "<memaddr>". */
917317f4
JM
7714 memaddr = remote_address_masked (memaddr);
7715 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7716
a76d924d
DJ
7717 if (use_length)
7718 {
7719 /* Append ",". */
7720 *p++ = ',';
802188a7 7721
124e13d9
SM
7722 /* Append the length and retain its location and size. It may need to be
7723 adjusted once the packet body has been created. */
a76d924d 7724 plen = p;
124e13d9 7725 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7726 p += plenlen;
7727 }
a257b5bb
AC
7728
7729 /* Append ":". */
917317f4
JM
7730 *p++ = ':';
7731 *p = '\0';
802188a7 7732
a257b5bb 7733 /* Append the packet body. */
a76d924d 7734 if (packet_format == 'X')
917317f4 7735 {
917317f4
JM
7736 /* Binary mode. Send target system values byte by byte, in
7737 increasing byte addresses. Only escape certain critical
7738 characters. */
124e13d9
SM
7739 payload_length_bytes =
7740 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7741 &units_written, payload_capacity_bytes);
6765f3e5 7742
124e13d9 7743 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7744 a second try to keep the end of the packet aligned. Don't do
7745 this if the packet is tiny. */
124e13d9 7746 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7747 {
124e13d9
SM
7748 int new_todo_units;
7749
7750 new_todo_units = align_for_efficient_write (units_written, memaddr);
7751
7752 if (new_todo_units != units_written)
7753 payload_length_bytes =
7754 remote_escape_output (myaddr, new_todo_units, unit_size,
7755 (gdb_byte *) p, &units_written,
7756 payload_capacity_bytes);
6765f3e5
DJ
7757 }
7758
124e13d9
SM
7759 p += payload_length_bytes;
7760 if (use_length && units_written < todo_units)
c906108c 7761 {
802188a7 7762 /* Escape chars have filled up the buffer prematurely,
124e13d9 7763 and we have actually sent fewer units than planned.
917317f4
JM
7764 Fix-up the length field of the packet. Use the same
7765 number of characters as before. */
124e13d9
SM
7766 plen += hexnumnstr (plen, (ULONGEST) units_written,
7767 plenlen);
917317f4 7768 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7769 }
a76d924d
DJ
7770 }
7771 else
7772 {
917317f4
JM
7773 /* Normal mode: Send target system values byte by byte, in
7774 increasing byte addresses. Each byte is encoded as a two hex
7775 value. */
124e13d9
SM
7776 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7777 units_written = todo_units;
c906108c 7778 }
802188a7 7779
2e9f7625 7780 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7781 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7782
2e9f7625 7783 if (rs->buf[0] == 'E')
00d84524 7784 return TARGET_XFER_E_IO;
802188a7 7785
124e13d9
SM
7786 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7787 send fewer units than we'd planned. */
7788 *xfered_len_units = (ULONGEST) units_written;
9b409511 7789 return TARGET_XFER_OK;
c906108c
SS
7790}
7791
a76d924d
DJ
7792/* Write memory data directly to the remote machine.
7793 This does not inform the data cache; the data cache uses this.
7794 MEMADDR is the address in the remote memory space.
7795 MYADDR is the address of the buffer in our space.
7796 LEN is the number of bytes.
7797
9b409511
YQ
7798 Return the transferred status, error or OK (an
7799 'enum target_xfer_status' value). Save the number of bytes
7800 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7801
9b409511
YQ
7802static enum target_xfer_status
7803remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7804 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7805{
7806 char *packet_format = 0;
7807
7808 /* Check whether the target supports binary download. */
7809 check_binary_download (memaddr);
7810
4082afcc 7811 switch (packet_support (PACKET_X))
a76d924d
DJ
7812 {
7813 case PACKET_ENABLE:
7814 packet_format = "X";
7815 break;
7816 case PACKET_DISABLE:
7817 packet_format = "M";
7818 break;
7819 case PACKET_SUPPORT_UNKNOWN:
7820 internal_error (__FILE__, __LINE__,
7821 _("remote_write_bytes: bad internal state"));
7822 default:
7823 internal_error (__FILE__, __LINE__, _("bad switch"));
7824 }
7825
7826 return remote_write_bytes_aux (packet_format,
124e13d9 7827 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7828 packet_format[0], 1);
a76d924d
DJ
7829}
7830
9217e74e
YQ
7831/* Read memory data directly from the remote machine.
7832 This does not use the data cache; the data cache uses this.
7833 MEMADDR is the address in the remote memory space.
7834 MYADDR is the address of the buffer in our space.
124e13d9
SM
7835 LEN_UNITS is the number of addressable memory units to read..
7836 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7837
7838 Return the transferred status, error or OK (an
7839 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7840 transferred in *XFERED_LEN_UNITS.
7841
7842 See the comment of remote_write_bytes_aux for an example of
7843 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7844
7845static enum target_xfer_status
124e13d9
SM
7846remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7847 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7848{
7849 struct remote_state *rs = get_remote_state ();
124e13d9 7850 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7851 char *p;
124e13d9
SM
7852 int todo_units;
7853 int decoded_bytes;
9217e74e 7854
124e13d9 7855 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7856 /* The packet buffer will be large enough for the payload;
7857 get_memory_packet_size ensures this. */
7858
124e13d9 7859 /* Number of units that will fit. */
325fac50
PA
7860 todo_units = std::min (len_units,
7861 (ULONGEST) (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7862
7863 /* Construct "m"<memaddr>","<len>". */
7864 memaddr = remote_address_masked (memaddr);
7865 p = rs->buf;
7866 *p++ = 'm';
7867 p += hexnumstr (p, (ULONGEST) memaddr);
7868 *p++ = ',';
124e13d9 7869 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7870 *p = '\0';
7871 putpkt (rs->buf);
7872 getpkt (&rs->buf, &rs->buf_size, 0);
7873 if (rs->buf[0] == 'E'
7874 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7875 && rs->buf[3] == '\0')
7876 return TARGET_XFER_E_IO;
7877 /* Reply describes memory byte by byte, each byte encoded as two hex
7878 characters. */
7879 p = rs->buf;
124e13d9 7880 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7881 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7882 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7883 return TARGET_XFER_OK;
7884}
7885
b55fbac4
YQ
7886/* Using the set of read-only target sections of remote, read live
7887 read-only memory.
8acf9577
YQ
7888
7889 For interface/parameters/return description see target.h,
7890 to_xfer_partial. */
7891
7892static enum target_xfer_status
b55fbac4
YQ
7893remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7894 ULONGEST memaddr, ULONGEST len,
124e13d9 7895 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7896{
7897 struct target_section *secp;
7898 struct target_section_table *table;
7899
7900 secp = target_section_by_addr (ops, memaddr);
7901 if (secp != NULL
7902 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7903 secp->the_bfd_section)
7904 & SEC_READONLY))
7905 {
7906 struct target_section *p;
7907 ULONGEST memend = memaddr + len;
7908
7909 table = target_get_section_table (ops);
7910
7911 for (p = table->sections; p < table->sections_end; p++)
7912 {
7913 if (memaddr >= p->addr)
7914 {
7915 if (memend <= p->endaddr)
7916 {
7917 /* Entire transfer is within this section. */
124e13d9 7918 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7919 xfered_len);
8acf9577
YQ
7920 }
7921 else if (memaddr >= p->endaddr)
7922 {
7923 /* This section ends before the transfer starts. */
7924 continue;
7925 }
7926 else
7927 {
7928 /* This section overlaps the transfer. Just do half. */
7929 len = p->endaddr - memaddr;
124e13d9 7930 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7931 xfered_len);
8acf9577
YQ
7932 }
7933 }
7934 }
7935 }
7936
7937 return TARGET_XFER_EOF;
7938}
7939
9217e74e
YQ
7940/* Similar to remote_read_bytes_1, but it reads from the remote stub
7941 first if the requested memory is unavailable in traceframe.
7942 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7943
9b409511 7944static enum target_xfer_status
8acf9577 7945remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7946 gdb_byte *myaddr, ULONGEST len, int unit_size,
7947 ULONGEST *xfered_len)
c906108c 7948{
6b6aa828 7949 if (len == 0)
96c4f946 7950 return TARGET_XFER_EOF;
b2182ed2 7951
8acf9577
YQ
7952 if (get_traceframe_number () != -1)
7953 {
7954 VEC(mem_range_s) *available;
7955
7956 /* If we fail to get the set of available memory, then the
7957 target does not support querying traceframe info, and so we
7958 attempt reading from the traceframe anyway (assuming the
7959 target implements the old QTro packet then). */
7960 if (traceframe_available_memory (&available, memaddr, len))
7961 {
7962 struct cleanup *old_chain;
7963
7964 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7965
7966 if (VEC_empty (mem_range_s, available)
7967 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7968 {
7969 enum target_xfer_status res;
7970
7971 /* Don't read into the traceframe's available
7972 memory. */
7973 if (!VEC_empty (mem_range_s, available))
7974 {
7975 LONGEST oldlen = len;
7976
7977 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7978 gdb_assert (len <= oldlen);
7979 }
7980
7981 do_cleanups (old_chain);
7982
7983 /* This goes through the topmost target again. */
b55fbac4 7984 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7985 len, unit_size, xfered_len);
8acf9577
YQ
7986 if (res == TARGET_XFER_OK)
7987 return TARGET_XFER_OK;
7988 else
7989 {
7990 /* No use trying further, we know some memory starting
7991 at MEMADDR isn't available. */
7992 *xfered_len = len;
7993 return TARGET_XFER_UNAVAILABLE;
7994 }
7995 }
7996
7997 /* Don't try to read more than how much is available, in
7998 case the target implements the deprecated QTro packet to
7999 cater for older GDBs (the target's knowledge of read-only
8000 sections may be outdated by now). */
8001 len = VEC_index (mem_range_s, available, 0)->length;
8002
8003 do_cleanups (old_chain);
8004 }
8005 }
8006
124e13d9 8007 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 8008}
74531fed 8009
c906108c 8010\f
c906108c 8011
a76d924d
DJ
8012/* Sends a packet with content determined by the printf format string
8013 FORMAT and the remaining arguments, then gets the reply. Returns
8014 whether the packet was a success, a failure, or unknown. */
8015
77b64a49
PA
8016static enum packet_result remote_send_printf (const char *format, ...)
8017 ATTRIBUTE_PRINTF (1, 2);
8018
2c0b251b 8019static enum packet_result
a76d924d
DJ
8020remote_send_printf (const char *format, ...)
8021{
8022 struct remote_state *rs = get_remote_state ();
8023 int max_size = get_remote_packet_size ();
a76d924d 8024 va_list ap;
a744cf53 8025
a76d924d
DJ
8026 va_start (ap, format);
8027
8028 rs->buf[0] = '\0';
8029 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 8030 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
8031
8032 if (putpkt (rs->buf) < 0)
8033 error (_("Communication problem with target."));
8034
8035 rs->buf[0] = '\0';
8036 getpkt (&rs->buf, &rs->buf_size, 0);
8037
8038 return packet_check_result (rs->buf);
8039}
8040
8041static void
8042restore_remote_timeout (void *p)
8043{
8044 int value = *(int *)p;
a744cf53 8045
a76d924d
DJ
8046 remote_timeout = value;
8047}
8048
8049/* Flash writing can take quite some time. We'll set
8050 effectively infinite timeout for flash operations.
8051 In future, we'll need to decide on a better approach. */
8052static const int remote_flash_timeout = 1000;
8053
8054static void
8055remote_flash_erase (struct target_ops *ops,
8056 ULONGEST address, LONGEST length)
8057{
f5656ead 8058 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
8059 int saved_remote_timeout = remote_timeout;
8060 enum packet_result ret;
a76d924d
DJ
8061 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
8062 &saved_remote_timeout);
a744cf53 8063
a76d924d
DJ
8064 remote_timeout = remote_flash_timeout;
8065
8066 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 8067 phex (address, addr_size),
a76d924d
DJ
8068 phex (length, 4));
8069 switch (ret)
8070 {
8071 case PACKET_UNKNOWN:
8072 error (_("Remote target does not support flash erase"));
8073 case PACKET_ERROR:
8074 error (_("Error erasing flash with vFlashErase packet"));
8075 default:
8076 break;
8077 }
8078
8079 do_cleanups (back_to);
8080}
8081
9b409511
YQ
8082static enum target_xfer_status
8083remote_flash_write (struct target_ops *ops, ULONGEST address,
8084 ULONGEST length, ULONGEST *xfered_len,
8085 const gdb_byte *data)
a76d924d
DJ
8086{
8087 int saved_remote_timeout = remote_timeout;
9b409511 8088 enum target_xfer_status ret;
a76d924d 8089 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 8090 &saved_remote_timeout);
a76d924d
DJ
8091
8092 remote_timeout = remote_flash_timeout;
124e13d9 8093 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 8094 xfered_len,'X', 0);
a76d924d
DJ
8095 do_cleanups (back_to);
8096
8097 return ret;
8098}
8099
8100static void
8101remote_flash_done (struct target_ops *ops)
8102{
8103 int saved_remote_timeout = remote_timeout;
8104 int ret;
8105 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
8106 &saved_remote_timeout);
8107
8108 remote_timeout = remote_flash_timeout;
8109 ret = remote_send_printf ("vFlashDone");
8110 do_cleanups (back_to);
8111
8112 switch (ret)
8113 {
8114 case PACKET_UNKNOWN:
8115 error (_("Remote target does not support vFlashDone"));
8116 case PACKET_ERROR:
8117 error (_("Error finishing flash operation"));
8118 default:
8119 break;
8120 }
8121}
8122
c906108c 8123static void
fba45db2 8124remote_files_info (struct target_ops *ignore)
c906108c
SS
8125{
8126 puts_filtered ("Debugging a target over a serial line.\n");
8127}
8128\f
8129/* Stuff for dealing with the packets which are part of this protocol.
8130 See comment at top of file for details. */
8131
1927e618
PA
8132/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
8133 error to higher layers. Called when a serial error is detected.
8134 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
8135 the system error message for errno at function entry and final dot
8136 for output compatibility with throw_perror_with_name. */
1927e618
PA
8137
8138static void
8139unpush_and_perror (const char *string)
8140{
d6cb50a2 8141 int saved_errno = errno;
1927e618
PA
8142
8143 remote_unpush_target ();
d6cb50a2
JK
8144 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
8145 safe_strerror (saved_errno));
1927e618
PA
8146}
8147
048094ac
PA
8148/* Read a single character from the remote end. The current quit
8149 handler is overridden to avoid quitting in the middle of packet
8150 sequence, as that would break communication with the remote server.
8151 See remote_serial_quit_handler for more detail. */
c906108c
SS
8152
8153static int
fba45db2 8154readchar (int timeout)
c906108c
SS
8155{
8156 int ch;
5d93a237 8157 struct remote_state *rs = get_remote_state ();
048094ac
PA
8158 struct cleanup *old_chain;
8159
8160 old_chain = make_cleanup_override_quit_handler (remote_serial_quit_handler);
8161
8162 rs->got_ctrlc_during_io = 0;
c906108c 8163
5d93a237 8164 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 8165
048094ac
PA
8166 if (rs->got_ctrlc_during_io)
8167 set_quit_flag ();
8168
8169 do_cleanups (old_chain);
8170
2acceee2 8171 if (ch >= 0)
0876f84a 8172 return ch;
2acceee2
JM
8173
8174 switch ((enum serial_rc) ch)
c906108c
SS
8175 {
8176 case SERIAL_EOF:
78a095c3 8177 remote_unpush_target ();
598d3636 8178 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 8179 /* no return */
c906108c 8180 case SERIAL_ERROR:
1927e618
PA
8181 unpush_and_perror (_("Remote communication error. "
8182 "Target disconnected."));
2acceee2 8183 /* no return */
c906108c 8184 case SERIAL_TIMEOUT:
2acceee2 8185 break;
c906108c 8186 }
2acceee2 8187 return ch;
c906108c
SS
8188}
8189
c33e31fd 8190/* Wrapper for serial_write that closes the target and throws if
048094ac
PA
8191 writing fails. The current quit handler is overridden to avoid
8192 quitting in the middle of packet sequence, as that would break
8193 communication with the remote server. See
8194 remote_serial_quit_handler for more detail. */
c33e31fd
PA
8195
8196static void
8197remote_serial_write (const char *str, int len)
8198{
5d93a237 8199 struct remote_state *rs = get_remote_state ();
048094ac
PA
8200 struct cleanup *old_chain;
8201
8202 old_chain = make_cleanup_override_quit_handler (remote_serial_quit_handler);
8203
8204 rs->got_ctrlc_during_io = 0;
5d93a237
TT
8205
8206 if (serial_write (rs->remote_desc, str, len))
c33e31fd 8207 {
1927e618
PA
8208 unpush_and_perror (_("Remote communication error. "
8209 "Target disconnected."));
c33e31fd 8210 }
048094ac
PA
8211
8212 if (rs->got_ctrlc_during_io)
8213 set_quit_flag ();
8214
8215 do_cleanups (old_chain);
c33e31fd
PA
8216}
8217
6d820c5c
DJ
8218/* Send the command in *BUF to the remote machine, and read the reply
8219 into *BUF. Report an error if we get an error reply. Resize
8220 *BUF using xrealloc if necessary to hold the result, and update
8221 *SIZEOF_BUF. */
c906108c
SS
8222
8223static void
6d820c5c
DJ
8224remote_send (char **buf,
8225 long *sizeof_buf)
c906108c 8226{
6d820c5c 8227 putpkt (*buf);
c2d11a7d 8228 getpkt (buf, sizeof_buf, 0);
c906108c 8229
6d820c5c
DJ
8230 if ((*buf)[0] == 'E')
8231 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
8232}
8233
6e5abd65
PA
8234/* Return a pointer to an xmalloc'ed string representing an escaped
8235 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
8236 etc. The caller is responsible for releasing the returned
8237 memory. */
8238
8239static char *
8240escape_buffer (const char *buf, int n)
8241{
8242 struct cleanup *old_chain;
8243 struct ui_file *stb;
8244 char *str;
6e5abd65
PA
8245
8246 stb = mem_fileopen ();
8247 old_chain = make_cleanup_ui_file_delete (stb);
8248
6ef284bd 8249 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 8250 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
8251 do_cleanups (old_chain);
8252 return str;
8253}
8254
c906108c
SS
8255/* Display a null-terminated packet on stdout, for debugging, using C
8256 string notation. */
8257
8258static void
baa336ce 8259print_packet (const char *buf)
c906108c
SS
8260{
8261 puts_filtered ("\"");
43e526b9 8262 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
8263 puts_filtered ("\"");
8264}
8265
8266int
baa336ce 8267putpkt (const char *buf)
c906108c
SS
8268{
8269 return putpkt_binary (buf, strlen (buf));
8270}
8271
8272/* Send a packet to the remote machine, with error checking. The data
23860348 8273 of the packet is in BUF. The string in BUF can be at most
ea9c271d 8274 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
8275 and for a possible /0 if we are debugging (remote_debug) and want
8276 to print the sent packet as a string. */
c906108c
SS
8277
8278static int
baa336ce 8279putpkt_binary (const char *buf, int cnt)
c906108c 8280{
2d717e4f 8281 struct remote_state *rs = get_remote_state ();
c906108c
SS
8282 int i;
8283 unsigned char csum = 0;
224c3ddb 8284 char *buf2 = (char *) xmalloc (cnt + 6);
a5c0808e 8285 struct cleanup *old_chain = make_cleanup (xfree, buf2);
085dd6e6 8286
c906108c
SS
8287 int ch;
8288 int tcount = 0;
8289 char *p;
8290
e24a49d8
PA
8291 /* Catch cases like trying to read memory or listing threads while
8292 we're waiting for a stop reply. The remote server wouldn't be
8293 ready to handle this request, so we'd hang and timeout. We don't
8294 have to worry about this in synchronous mode, because in that
8295 case it's not possible to issue a command while the target is
74531fed
PA
8296 running. This is not a problem in non-stop mode, because in that
8297 case, the stub is always ready to process serial input. */
6efcd9a8
PA
8298 if (!target_is_non_stop_p ()
8299 && target_is_async_p ()
8300 && rs->waiting_for_stop_reply)
9597b22a
DE
8301 {
8302 error (_("Cannot execute this command while the target is running.\n"
8303 "Use the \"interrupt\" command to stop the target\n"
8304 "and then try again."));
8305 }
e24a49d8 8306
2d717e4f
DJ
8307 /* We're sending out a new packet. Make sure we don't look at a
8308 stale cached response. */
8309 rs->cached_wait_status = 0;
8310
c906108c
SS
8311 /* Copy the packet into buffer BUF2, encapsulating it
8312 and giving it a checksum. */
8313
c906108c
SS
8314 p = buf2;
8315 *p++ = '$';
8316
8317 for (i = 0; i < cnt; i++)
8318 {
8319 csum += buf[i];
8320 *p++ = buf[i];
8321 }
8322 *p++ = '#';
8323 *p++ = tohex ((csum >> 4) & 0xf);
8324 *p++ = tohex (csum & 0xf);
8325
8326 /* Send it over and over until we get a positive ack. */
8327
8328 while (1)
8329 {
8330 int started_error_output = 0;
8331
8332 if (remote_debug)
8333 {
6e5abd65
PA
8334 struct cleanup *old_chain;
8335 char *str;
8336
c906108c 8337 *p = '\0';
6e5abd65
PA
8338 str = escape_buffer (buf2, p - buf2);
8339 old_chain = make_cleanup (xfree, str);
8340 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 8341 gdb_flush (gdb_stdlog);
6e5abd65 8342 do_cleanups (old_chain);
c906108c 8343 }
c33e31fd 8344 remote_serial_write (buf2, p - buf2);
c906108c 8345
a6f3e723
SL
8346 /* If this is a no acks version of the remote protocol, send the
8347 packet and move on. */
8348 if (rs->noack_mode)
8349 break;
8350
74531fed
PA
8351 /* Read until either a timeout occurs (-2) or '+' is read.
8352 Handle any notification that arrives in the mean time. */
c906108c
SS
8353 while (1)
8354 {
8355 ch = readchar (remote_timeout);
8356
c5aa993b 8357 if (remote_debug)
c906108c
SS
8358 {
8359 switch (ch)
8360 {
8361 case '+':
1216fa2c 8362 case '-':
c906108c
SS
8363 case SERIAL_TIMEOUT:
8364 case '$':
74531fed 8365 case '%':
c906108c
SS
8366 if (started_error_output)
8367 {
8368 putchar_unfiltered ('\n');
8369 started_error_output = 0;
8370 }
8371 }
8372 }
8373
8374 switch (ch)
8375 {
8376 case '+':
8377 if (remote_debug)
0f71a2f6 8378 fprintf_unfiltered (gdb_stdlog, "Ack\n");
a5c0808e 8379 do_cleanups (old_chain);
c906108c 8380 return 1;
1216fa2c
AC
8381 case '-':
8382 if (remote_debug)
8383 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 8384 /* FALLTHROUGH */
c906108c 8385 case SERIAL_TIMEOUT:
c5aa993b 8386 tcount++;
c906108c 8387 if (tcount > 3)
a5c0808e
PA
8388 {
8389 do_cleanups (old_chain);
8390 return 0;
8391 }
23860348 8392 break; /* Retransmit buffer. */
c906108c
SS
8393 case '$':
8394 {
40e3f985 8395 if (remote_debug)
2bc416ba 8396 fprintf_unfiltered (gdb_stdlog,
23860348 8397 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
8398 /* It's probably an old response sent because an ACK
8399 was lost. Gobble up the packet and ack it so it
8400 doesn't get retransmitted when we resend this
8401 packet. */
6d820c5c 8402 skip_frame ();
c33e31fd 8403 remote_serial_write ("+", 1);
23860348 8404 continue; /* Now, go look for +. */
c906108c 8405 }
74531fed
PA
8406
8407 case '%':
8408 {
8409 int val;
8410
8411 /* If we got a notification, handle it, and go back to looking
8412 for an ack. */
8413 /* We've found the start of a notification. Now
8414 collect the data. */
8415 val = read_frame (&rs->buf, &rs->buf_size);
8416 if (val >= 0)
8417 {
8418 if (remote_debug)
8419 {
6e5abd65
PA
8420 struct cleanup *old_chain;
8421 char *str;
8422
8423 str = escape_buffer (rs->buf, val);
8424 old_chain = make_cleanup (xfree, str);
8425 fprintf_unfiltered (gdb_stdlog,
8426 " Notification received: %s\n",
8427 str);
8428 do_cleanups (old_chain);
74531fed 8429 }
5965e028 8430 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
8431 /* We're in sync now, rewait for the ack. */
8432 tcount = 0;
8433 }
8434 else
8435 {
8436 if (remote_debug)
8437 {
8438 if (!started_error_output)
8439 {
8440 started_error_output = 1;
8441 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
8442 }
8443 fputc_unfiltered (ch & 0177, gdb_stdlog);
8444 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
8445 }
8446 }
8447 continue;
8448 }
8449 /* fall-through */
c906108c
SS
8450 default:
8451 if (remote_debug)
8452 {
8453 if (!started_error_output)
8454 {
8455 started_error_output = 1;
0f71a2f6 8456 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 8457 }
0f71a2f6 8458 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
8459 }
8460 continue;
8461 }
23860348 8462 break; /* Here to retransmit. */
c906108c
SS
8463 }
8464
8465#if 0
8466 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
8467 able to get out next time we call QUIT, without anything as
8468 violent as interrupt_query. If we want to provide a way out of
8469 here without getting to the next QUIT, it should be based on
8470 hitting ^C twice as in remote_wait. */
c906108c
SS
8471 if (quit_flag)
8472 {
8473 quit_flag = 0;
8474 interrupt_query ();
8475 }
8476#endif
8477 }
a5c0808e
PA
8478
8479 do_cleanups (old_chain);
a6f3e723 8480 return 0;
c906108c
SS
8481}
8482
6d820c5c
DJ
8483/* Come here after finding the start of a frame when we expected an
8484 ack. Do our best to discard the rest of this packet. */
8485
8486static void
8487skip_frame (void)
8488{
8489 int c;
8490
8491 while (1)
8492 {
8493 c = readchar (remote_timeout);
8494 switch (c)
8495 {
8496 case SERIAL_TIMEOUT:
8497 /* Nothing we can do. */
8498 return;
8499 case '#':
8500 /* Discard the two bytes of checksum and stop. */
8501 c = readchar (remote_timeout);
8502 if (c >= 0)
8503 c = readchar (remote_timeout);
8504
8505 return;
8506 case '*': /* Run length encoding. */
8507 /* Discard the repeat count. */
8508 c = readchar (remote_timeout);
8509 if (c < 0)
8510 return;
8511 break;
8512 default:
8513 /* A regular character. */
8514 break;
8515 }
8516 }
8517}
8518
c906108c 8519/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
8520 into *BUF, verifying the checksum, length, and handling run-length
8521 compression. NUL terminate the buffer. If there is not enough room,
8522 expand *BUF using xrealloc.
c906108c 8523
c2d11a7d
JM
8524 Returns -1 on error, number of characters in buffer (ignoring the
8525 trailing NULL) on success. (could be extended to return one of the
23860348 8526 SERIAL status indications). */
c2d11a7d
JM
8527
8528static long
6d820c5c
DJ
8529read_frame (char **buf_p,
8530 long *sizeof_buf)
c906108c
SS
8531{
8532 unsigned char csum;
c2d11a7d 8533 long bc;
c906108c 8534 int c;
6d820c5c 8535 char *buf = *buf_p;
a6f3e723 8536 struct remote_state *rs = get_remote_state ();
c906108c
SS
8537
8538 csum = 0;
c2d11a7d 8539 bc = 0;
c906108c
SS
8540
8541 while (1)
8542 {
8543 c = readchar (remote_timeout);
c906108c
SS
8544 switch (c)
8545 {
8546 case SERIAL_TIMEOUT:
8547 if (remote_debug)
0f71a2f6 8548 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 8549 return -1;
c906108c
SS
8550 case '$':
8551 if (remote_debug)
0f71a2f6
JM
8552 fputs_filtered ("Saw new packet start in middle of old one\n",
8553 gdb_stdlog);
23860348 8554 return -1; /* Start a new packet, count retries. */
c906108c
SS
8555 case '#':
8556 {
8557 unsigned char pktcsum;
e1b09194
AC
8558 int check_0 = 0;
8559 int check_1 = 0;
c906108c 8560
c2d11a7d 8561 buf[bc] = '\0';
c906108c 8562
e1b09194
AC
8563 check_0 = readchar (remote_timeout);
8564 if (check_0 >= 0)
8565 check_1 = readchar (remote_timeout);
802188a7 8566
e1b09194
AC
8567 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
8568 {
8569 if (remote_debug)
2bc416ba 8570 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 8571 gdb_stdlog);
e1b09194
AC
8572 return -1;
8573 }
8574 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
8575 {
8576 if (remote_debug)
2bc416ba 8577 fputs_filtered ("Communication error in checksum\n",
23860348 8578 gdb_stdlog);
40e3f985
FN
8579 return -1;
8580 }
c906108c 8581
a6f3e723
SL
8582 /* Don't recompute the checksum; with no ack packets we
8583 don't have any way to indicate a packet retransmission
8584 is necessary. */
8585 if (rs->noack_mode)
8586 return bc;
8587
e1b09194 8588 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 8589 if (csum == pktcsum)
c2d11a7d 8590 return bc;
c906108c 8591
c5aa993b 8592 if (remote_debug)
c906108c 8593 {
6e5abd65
PA
8594 struct cleanup *old_chain;
8595 char *str;
8596
8597 str = escape_buffer (buf, bc);
8598 old_chain = make_cleanup (xfree, str);
8599 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8600 "Bad checksum, sentsum=0x%x, "
8601 "csum=0x%x, buf=%s\n",
6e5abd65
PA
8602 pktcsum, csum, str);
8603 do_cleanups (old_chain);
c906108c 8604 }
c2d11a7d 8605 /* Number of characters in buffer ignoring trailing
23860348 8606 NULL. */
c2d11a7d 8607 return -1;
c906108c 8608 }
23860348 8609 case '*': /* Run length encoding. */
c2c6d25f
JM
8610 {
8611 int repeat;
c906108c 8612
a744cf53 8613 csum += c;
b4501125
AC
8614 c = readchar (remote_timeout);
8615 csum += c;
23860348 8616 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 8617
23860348 8618 /* The character before ``*'' is repeated. */
c2d11a7d 8619
6d820c5c 8620 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 8621 {
6d820c5c
DJ
8622 if (bc + repeat - 1 >= *sizeof_buf - 1)
8623 {
8624 /* Make some more room in the buffer. */
8625 *sizeof_buf += repeat;
224c3ddb 8626 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c
DJ
8627 buf = *buf_p;
8628 }
8629
c2d11a7d
JM
8630 memset (&buf[bc], buf[bc - 1], repeat);
8631 bc += repeat;
c2c6d25f
JM
8632 continue;
8633 }
8634
c2d11a7d 8635 buf[bc] = '\0';
6d820c5c 8636 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 8637 return -1;
c2c6d25f 8638 }
c906108c 8639 default:
6d820c5c 8640 if (bc >= *sizeof_buf - 1)
c906108c 8641 {
6d820c5c
DJ
8642 /* Make some more room in the buffer. */
8643 *sizeof_buf *= 2;
224c3ddb 8644 *buf_p = (char *) xrealloc (*buf_p, *sizeof_buf);
6d820c5c 8645 buf = *buf_p;
c906108c
SS
8646 }
8647
6d820c5c
DJ
8648 buf[bc++] = c;
8649 csum += c;
8650 continue;
c906108c
SS
8651 }
8652 }
8653}
8654
8655/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8656 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8657 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8658 rather than timing out; this is used (in synchronous mode) to wait
8659 for a target that is is executing user code to stop. */
d9fcf2fb
JM
8660/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
8661 don't have to change all the calls to getpkt to deal with the
8662 return value, because at the moment I don't know what the right
23860348 8663 thing to do it for those. */
c906108c 8664void
6d820c5c
DJ
8665getpkt (char **buf,
8666 long *sizeof_buf,
c2d11a7d 8667 int forever)
d9fcf2fb 8668{
54887903 8669 getpkt_sane (buf, sizeof_buf, forever);
d9fcf2fb
JM
8670}
8671
8672
8673/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8674 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8675 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8676 rather than timing out; this is used (in synchronous mode) to wait
8677 for a target that is is executing user code to stop. If FOREVER ==
8678 0, this function is allowed to time out gracefully and return an
74531fed
PA
8679 indication of this to the caller. Otherwise return the number of
8680 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
8681 enough reason to return to the caller. *IS_NOTIF is an output
8682 boolean that indicates whether *BUF holds a notification or not
8683 (a regular packet). */
74531fed 8684
3172dc30 8685static int
74531fed 8686getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 8687 int expecting_notif, int *is_notif)
c906108c 8688{
2d717e4f 8689 struct remote_state *rs = get_remote_state ();
c906108c
SS
8690 int c;
8691 int tries;
8692 int timeout;
df4b58fe 8693 int val = -1;
c906108c 8694
2d717e4f
DJ
8695 /* We're reading a new response. Make sure we don't look at a
8696 previously cached response. */
8697 rs->cached_wait_status = 0;
8698
6d820c5c 8699 strcpy (*buf, "timeout");
c906108c
SS
8700
8701 if (forever)
74531fed
PA
8702 timeout = watchdog > 0 ? watchdog : -1;
8703 else if (expecting_notif)
8704 timeout = 0; /* There should already be a char in the buffer. If
8705 not, bail out. */
c906108c
SS
8706 else
8707 timeout = remote_timeout;
8708
8709#define MAX_TRIES 3
8710
74531fed
PA
8711 /* Process any number of notifications, and then return when
8712 we get a packet. */
8713 for (;;)
c906108c 8714 {
d9c43928 8715 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8716 times. */
8717 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8718 {
74531fed
PA
8719 /* This can loop forever if the remote side sends us
8720 characters continuously, but if it pauses, we'll get
8721 SERIAL_TIMEOUT from readchar because of timeout. Then
8722 we'll count that as a retry.
8723
8724 Note that even when forever is set, we will only wait
8725 forever prior to the start of a packet. After that, we
8726 expect characters to arrive at a brisk pace. They should
8727 show up within remote_timeout intervals. */
8728 do
8729 c = readchar (timeout);
8730 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8731
8732 if (c == SERIAL_TIMEOUT)
8733 {
74531fed
PA
8734 if (expecting_notif)
8735 return -1; /* Don't complain, it's normal to not get
8736 anything in this case. */
8737
23860348 8738 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8739 {
78a095c3 8740 remote_unpush_target ();
598d3636
JK
8741 throw_error (TARGET_CLOSE_ERROR,
8742 _("Watchdog timeout has expired. "
8743 "Target detached."));
c906108c 8744 }
c906108c 8745 if (remote_debug)
0f71a2f6 8746 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8747 }
74531fed
PA
8748 else
8749 {
8750 /* We've found the start of a packet or notification.
8751 Now collect the data. */
8752 val = read_frame (buf, sizeof_buf);
8753 if (val >= 0)
8754 break;
8755 }
8756
c33e31fd 8757 remote_serial_write ("-", 1);
c906108c 8758 }
c906108c 8759
74531fed
PA
8760 if (tries > MAX_TRIES)
8761 {
8762 /* We have tried hard enough, and just can't receive the
8763 packet/notification. Give up. */
8764 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8765
74531fed
PA
8766 /* Skip the ack char if we're in no-ack mode. */
8767 if (!rs->noack_mode)
c33e31fd 8768 remote_serial_write ("+", 1);
74531fed
PA
8769 return -1;
8770 }
c906108c 8771
74531fed
PA
8772 /* If we got an ordinary packet, return that to our caller. */
8773 if (c == '$')
c906108c
SS
8774 {
8775 if (remote_debug)
43e526b9 8776 {
6e5abd65
PA
8777 struct cleanup *old_chain;
8778 char *str;
8779
8780 str = escape_buffer (*buf, val);
8781 old_chain = make_cleanup (xfree, str);
8782 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8783 do_cleanups (old_chain);
43e526b9 8784 }
a6f3e723
SL
8785
8786 /* Skip the ack char if we're in no-ack mode. */
8787 if (!rs->noack_mode)
c33e31fd 8788 remote_serial_write ("+", 1);
fee9eda9
YQ
8789 if (is_notif != NULL)
8790 *is_notif = 0;
0876f84a 8791 return val;
c906108c
SS
8792 }
8793
74531fed
PA
8794 /* If we got a notification, handle it, and go back to looking
8795 for a packet. */
8796 else
8797 {
8798 gdb_assert (c == '%');
8799
8800 if (remote_debug)
8801 {
6e5abd65
PA
8802 struct cleanup *old_chain;
8803 char *str;
8804
8805 str = escape_buffer (*buf, val);
8806 old_chain = make_cleanup (xfree, str);
8807 fprintf_unfiltered (gdb_stdlog,
8808 " Notification received: %s\n",
8809 str);
8810 do_cleanups (old_chain);
74531fed 8811 }
fee9eda9
YQ
8812 if (is_notif != NULL)
8813 *is_notif = 1;
c906108c 8814
5965e028 8815 handle_notification (rs->notif_state, *buf);
c906108c 8816
74531fed 8817 /* Notifications require no acknowledgement. */
a6f3e723 8818
74531fed 8819 if (expecting_notif)
fee9eda9 8820 return val;
74531fed
PA
8821 }
8822 }
8823}
8824
8825static int
8826getpkt_sane (char **buf, long *sizeof_buf, int forever)
8827{
fee9eda9 8828 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8829}
8830
8831static int
fee9eda9
YQ
8832getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8833 int *is_notif)
74531fed 8834{
fee9eda9
YQ
8835 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8836 is_notif);
c906108c 8837}
74531fed 8838
cbb8991c
DB
8839/* Check whether EVENT is a fork event for the process specified
8840 by the pid passed in DATA, and if it is, kill the fork child. */
8841
8842static int
8843kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8844 QUEUE_ITER (stop_reply_p) *iter,
8845 stop_reply_p event,
8846 void *data)
8847{
19ba03f4 8848 struct queue_iter_param *param = (struct queue_iter_param *) data;
cbb8991c
DB
8849 int parent_pid = *(int *) param->input;
8850
8851 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8852 {
8853 struct remote_state *rs = get_remote_state ();
8854 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8855 int res;
8856
8857 res = remote_vkill (child_pid, rs);
8858 if (res != 0)
8859 error (_("Can't kill fork child process %d"), child_pid);
8860 }
8861
8862 return 1;
8863}
8864
8865/* Kill any new fork children of process PID that haven't been
8866 processed by follow_fork. */
8867
8868static void
8869kill_new_fork_children (int pid, struct remote_state *rs)
8870{
8871 struct thread_info *thread;
8872 struct notif_client *notif = &notif_client_stop;
8873 struct queue_iter_param param;
8874
8875 /* Kill the fork child threads of any threads in process PID
8876 that are stopped at a fork event. */
8877 ALL_NON_EXITED_THREADS (thread)
8878 {
8879 struct target_waitstatus *ws = &thread->pending_follow;
8880
8881 if (is_pending_fork_parent (ws, pid, thread->ptid))
8882 {
8883 struct remote_state *rs = get_remote_state ();
8884 int child_pid = ptid_get_pid (ws->value.related_pid);
8885 int res;
8886
8887 res = remote_vkill (child_pid, rs);
8888 if (res != 0)
8889 error (_("Can't kill fork child process %d"), child_pid);
8890 }
8891 }
8892
8893 /* Check for any pending fork events (not reported or processed yet)
8894 in process PID and kill those fork child threads as well. */
8895 remote_notif_get_pending_events (notif);
8896 param.input = &pid;
8897 param.output = NULL;
8898 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8899 kill_child_of_pending_fork, &param);
8900}
8901
c906108c 8902\f
8020350c
DB
8903/* Target hook to kill the current inferior. */
8904
c906108c 8905static void
7d85a9c0 8906remote_kill (struct target_ops *ops)
43ff13b4 8907{
8020350c
DB
8908 int res = -1;
8909 int pid = ptid_get_pid (inferior_ptid);
8910 struct remote_state *rs = get_remote_state ();
0fdf84ca 8911
8020350c 8912 if (packet_support (PACKET_vKill) != PACKET_DISABLE)
0fdf84ca 8913 {
8020350c
DB
8914 /* If we're stopped while forking and we haven't followed yet,
8915 kill the child task. We need to do this before killing the
8916 parent task because if this is a vfork then the parent will
8917 be sleeping. */
8918 kill_new_fork_children (pid, rs);
8919
8920 res = remote_vkill (pid, rs);
8921 if (res == 0)
0fdf84ca 8922 {
bc1e6c81 8923 target_mourn_inferior (inferior_ptid);
0fdf84ca
PA
8924 return;
8925 }
8020350c 8926 }
0fdf84ca 8927
8020350c
DB
8928 /* If we are in 'target remote' mode and we are killing the only
8929 inferior, then we will tell gdbserver to exit and unpush the
8930 target. */
8931 if (res == -1 && !remote_multi_process_p (rs)
8932 && number_of_live_inferiors () == 1)
8933 {
8934 remote_kill_k ();
8935
8936 /* We've killed the remote end, we get to mourn it. If we are
8937 not in extended mode, mourning the inferior also unpushes
8938 remote_ops from the target stack, which closes the remote
8939 connection. */
bc1e6c81 8940 target_mourn_inferior (inferior_ptid);
8020350c
DB
8941
8942 return;
0fdf84ca 8943 }
43ff13b4 8944
8020350c 8945 error (_("Can't kill process"));
43ff13b4
JM
8946}
8947
8020350c
DB
8948/* Send a kill request to the target using the 'vKill' packet. */
8949
82f73884
PA
8950static int
8951remote_vkill (int pid, struct remote_state *rs)
8952{
4082afcc 8953 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8954 return -1;
8955
8956 /* Tell the remote target to detach. */
bba74b36 8957 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8958 putpkt (rs->buf);
8959 getpkt (&rs->buf, &rs->buf_size, 0);
8960
4082afcc
PA
8961 switch (packet_ok (rs->buf,
8962 &remote_protocol_packets[PACKET_vKill]))
8963 {
8964 case PACKET_OK:
8965 return 0;
8966 case PACKET_ERROR:
8967 return 1;
8968 case PACKET_UNKNOWN:
8969 return -1;
8970 default:
8971 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8972 }
82f73884
PA
8973}
8974
8020350c
DB
8975/* Send a kill request to the target using the 'k' packet. */
8976
82f73884 8977static void
8020350c 8978remote_kill_k (void)
82f73884 8979{
8020350c
DB
8980 /* Catch errors so the user can quit from gdb even when we
8981 aren't on speaking terms with the remote system. */
8982 TRY
82f73884 8983 {
82f73884 8984 putpkt ("k");
82f73884 8985 }
8020350c
DB
8986 CATCH (ex, RETURN_MASK_ERROR)
8987 {
8988 if (ex.error == TARGET_CLOSE_ERROR)
8989 {
8990 /* If we got an (EOF) error that caused the target
8991 to go away, then we're done, that's what we wanted.
8992 "k" is susceptible to cause a premature EOF, given
8993 that the remote server isn't actually required to
8994 reply to "k", and it can happen that it doesn't
8995 even get to reply ACK to the "k". */
8996 return;
8997 }
82f73884 8998
8020350c
DB
8999 /* Otherwise, something went wrong. We didn't actually kill
9000 the target. Just propagate the exception, and let the
9001 user or higher layers decide what to do. */
9002 throw_exception (ex);
9003 }
9004 END_CATCH
82f73884
PA
9005}
9006
c906108c 9007static void
20f796c9 9008remote_mourn (struct target_ops *target)
c906108c 9009{
8020350c 9010 struct remote_state *rs = get_remote_state ();
ce5ce7ed 9011
8020350c
DB
9012 /* In 'target remote' mode with one inferior, we close the connection. */
9013 if (!rs->extended && number_of_live_inferiors () <= 1)
9014 {
9015 unpush_target (target);
c906108c 9016
8020350c
DB
9017 /* remote_close takes care of doing most of the clean up. */
9018 generic_mourn_inferior ();
9019 return;
9020 }
c906108c 9021
e24a49d8
PA
9022 /* In case we got here due to an error, but we're going to stay
9023 connected. */
9024 rs->waiting_for_stop_reply = 0;
9025
dc1981d7
PA
9026 /* If the current general thread belonged to the process we just
9027 detached from or has exited, the remote side current general
9028 thread becomes undefined. Considering a case like this:
9029
9030 - We just got here due to a detach.
9031 - The process that we're detaching from happens to immediately
9032 report a global breakpoint being hit in non-stop mode, in the
9033 same thread we had selected before.
9034 - GDB attaches to this process again.
9035 - This event happens to be the next event we handle.
9036
9037 GDB would consider that the current general thread didn't need to
9038 be set on the stub side (with Hg), since for all it knew,
9039 GENERAL_THREAD hadn't changed.
9040
9041 Notice that although in all-stop mode, the remote server always
9042 sets the current thread to the thread reporting the stop event,
9043 that doesn't happen in non-stop mode; in non-stop, the stub *must
9044 not* change the current thread when reporting a breakpoint hit,
9045 due to the decoupling of event reporting and event handling.
9046
9047 To keep things simple, we always invalidate our notion of the
9048 current thread. */
47f8a51d 9049 record_currthread (rs, minus_one_ptid);
dc1981d7 9050
8020350c 9051 /* Call common code to mark the inferior as not running. */
48aa3c27
PA
9052 generic_mourn_inferior ();
9053
d729566a 9054 if (!have_inferiors ())
2d717e4f 9055 {
82f73884
PA
9056 if (!remote_multi_process_p (rs))
9057 {
9058 /* Check whether the target is running now - some remote stubs
9059 automatically restart after kill. */
9060 putpkt ("?");
9061 getpkt (&rs->buf, &rs->buf_size, 0);
9062
9063 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
9064 {
3e43a32a
MS
9065 /* Assume that the target has been restarted. Set
9066 inferior_ptid so that bits of core GDB realizes
9067 there's something here, e.g., so that the user can
9068 say "kill" again. */
82f73884
PA
9069 inferior_ptid = magic_null_ptid;
9070 }
82f73884 9071 }
2d717e4f
DJ
9072 }
9073}
c906108c 9074
03583c20 9075static int
2bfc0540 9076extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 9077{
4082afcc 9078 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
9079}
9080
9081static void
9082extended_remote_disable_randomization (int val)
9083{
9084 struct remote_state *rs = get_remote_state ();
9085 char *reply;
9086
bba74b36
YQ
9087 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
9088 val);
03583c20
UW
9089 putpkt (rs->buf);
9090 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
9091 if (*reply == '\0')
9092 error (_("Target does not support QDisableRandomization."));
9093 if (strcmp (reply, "OK") != 0)
9094 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
9095}
9096
2d717e4f
DJ
9097static int
9098extended_remote_run (char *args)
9099{
9100 struct remote_state *rs = get_remote_state ();
2d717e4f 9101 int len;
94585166 9102 const char *remote_exec_file = get_remote_exec_file ();
c906108c 9103
2d717e4f
DJ
9104 /* If the user has disabled vRun support, or we have detected that
9105 support is not available, do not try it. */
4082afcc 9106 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 9107 return -1;
424163ea 9108
2d717e4f
DJ
9109 strcpy (rs->buf, "vRun;");
9110 len = strlen (rs->buf);
c906108c 9111
2d717e4f
DJ
9112 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
9113 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
9114 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
9115 strlen (remote_exec_file));
2d717e4f 9116
d1a41061 9117 gdb_assert (args != NULL);
2d717e4f
DJ
9118 if (*args)
9119 {
9120 struct cleanup *back_to;
9121 int i;
9122 char **argv;
9123
d1a41061 9124 argv = gdb_buildargv (args);
6e366df1 9125 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
9126 for (i = 0; argv[i] != NULL; i++)
9127 {
9128 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
9129 error (_("Argument list too long for run packet"));
9130 rs->buf[len++] = ';';
9f1b45b0
TT
9131 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
9132 strlen (argv[i]));
2d717e4f
DJ
9133 }
9134 do_cleanups (back_to);
9135 }
9136
9137 rs->buf[len++] = '\0';
9138
9139 putpkt (rs->buf);
9140 getpkt (&rs->buf, &rs->buf_size, 0);
9141
4082afcc 9142 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 9143 {
4082afcc 9144 case PACKET_OK:
3405876a 9145 /* We have a wait response. All is well. */
2d717e4f 9146 return 0;
4082afcc
PA
9147 case PACKET_UNKNOWN:
9148 return -1;
9149 case PACKET_ERROR:
2d717e4f
DJ
9150 if (remote_exec_file[0] == '\0')
9151 error (_("Running the default executable on the remote target failed; "
9152 "try \"set remote exec-file\"?"));
9153 else
9154 error (_("Running \"%s\" on the remote target failed"),
9155 remote_exec_file);
4082afcc
PA
9156 default:
9157 gdb_assert_not_reached (_("bad switch"));
2d717e4f 9158 }
c906108c
SS
9159}
9160
2d717e4f
DJ
9161/* In the extended protocol we want to be able to do things like
9162 "run" and have them basically work as expected. So we need
9163 a special create_inferior function. We support changing the
9164 executable file and the command line arguments, but not the
9165 environment. */
9166
43ff13b4 9167static void
77a19445
TT
9168extended_remote_create_inferior (struct target_ops *ops,
9169 char *exec_file, char *args,
9170 char **env, int from_tty)
43ff13b4 9171{
3405876a
PA
9172 int run_worked;
9173 char *stop_reply;
9174 struct remote_state *rs = get_remote_state ();
94585166 9175 const char *remote_exec_file = get_remote_exec_file ();
3405876a 9176
43ff13b4 9177 /* If running asynchronously, register the target file descriptor
23860348 9178 with the event loop. */
75c99385 9179 if (target_can_async_p ())
6a3753b3 9180 target_async (1);
43ff13b4 9181
03583c20 9182 /* Disable address space randomization if requested (and supported). */
2bfc0540 9183 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
9184 extended_remote_disable_randomization (disable_randomization);
9185
43ff13b4 9186 /* Now restart the remote server. */
3405876a
PA
9187 run_worked = extended_remote_run (args) != -1;
9188 if (!run_worked)
2d717e4f
DJ
9189 {
9190 /* vRun was not supported. Fail if we need it to do what the
9191 user requested. */
9192 if (remote_exec_file[0])
9193 error (_("Remote target does not support \"set remote exec-file\""));
9194 if (args[0])
9195 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 9196
2d717e4f
DJ
9197 /* Fall back to "R". */
9198 extended_remote_restart ();
9199 }
424163ea 9200
6c95b8df
PA
9201 if (!have_inferiors ())
9202 {
9203 /* Clean up from the last time we ran, before we mark the target
9204 running again. This will mark breakpoints uninserted, and
9205 get_offsets may insert breakpoints. */
9206 init_thread_list ();
9207 init_wait_for_inferior ();
9208 }
45280a52 9209
3405876a
PA
9210 /* vRun's success return is a stop reply. */
9211 stop_reply = run_worked ? rs->buf : NULL;
9212 add_current_inferior_and_thread (stop_reply);
c0a2216e 9213
2d717e4f
DJ
9214 /* Get updated offsets, if the stub uses qOffsets. */
9215 get_offsets ();
2d717e4f 9216}
c906108c 9217\f
c5aa993b 9218
b775012e
LM
9219/* Given a location's target info BP_TGT and the packet buffer BUF, output
9220 the list of conditions (in agent expression bytecode format), if any, the
9221 target needs to evaluate. The output is placed into the packet buffer
bba74b36 9222 started from BUF and ended at BUF_END. */
b775012e
LM
9223
9224static int
9225remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
9226 struct bp_target_info *bp_tgt, char *buf,
9227 char *buf_end)
b775012e
LM
9228{
9229 struct agent_expr *aexpr = NULL;
9230 int i, ix;
b775012e
LM
9231
9232 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
9233 return 0;
9234
9235 buf += strlen (buf);
bba74b36 9236 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
9237 buf++;
9238
9239 /* Send conditions to the target and free the vector. */
9240 for (ix = 0;
9241 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
9242 ix++)
9243 {
bba74b36 9244 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
9245 buf += strlen (buf);
9246 for (i = 0; i < aexpr->len; ++i)
9247 buf = pack_hex_byte (buf, aexpr->buf[i]);
9248 *buf = '\0';
9249 }
b775012e
LM
9250 return 0;
9251}
9252
d3ce09f5
SS
9253static void
9254remote_add_target_side_commands (struct gdbarch *gdbarch,
9255 struct bp_target_info *bp_tgt, char *buf)
9256{
9257 struct agent_expr *aexpr = NULL;
9258 int i, ix;
9259
9260 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
9261 return;
9262
9263 buf += strlen (buf);
9264
9265 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
9266 buf += strlen (buf);
9267
9268 /* Concatenate all the agent expressions that are commands into the
9269 cmds parameter. */
9270 for (ix = 0;
9271 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
9272 ix++)
9273 {
9274 sprintf (buf, "X%x,", aexpr->len);
9275 buf += strlen (buf);
9276 for (i = 0; i < aexpr->len; ++i)
9277 buf = pack_hex_byte (buf, aexpr->buf[i]);
9278 *buf = '\0';
9279 }
d3ce09f5
SS
9280}
9281
8181d85f
DJ
9282/* Insert a breakpoint. On targets that have software breakpoint
9283 support, we ask the remote target to do the work; on targets
9284 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
9285
9286static int
3db08215
MM
9287remote_insert_breakpoint (struct target_ops *ops,
9288 struct gdbarch *gdbarch,
a6d9a66e 9289 struct bp_target_info *bp_tgt)
c906108c 9290{
d471ea57
AC
9291 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
9292 If it succeeds, then set the support to PACKET_ENABLE. If it
9293 fails, and the user has explicitly requested the Z support then
23860348 9294 report an error, otherwise, mark it disabled and go on. */
802188a7 9295
4082afcc 9296 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9297 {
0d5ed153 9298 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 9299 struct remote_state *rs;
bba74b36 9300 char *p, *endbuf;
7c0f6dcc 9301 int bpsize;
4fff2411 9302
28439a30
PA
9303 /* Make sure the remote is pointing at the right process, if
9304 necessary. */
9305 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9306 set_general_process ();
9307
a1dcb23a 9308 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
9309
9310 rs = get_remote_state ();
9311 p = rs->buf;
bba74b36 9312 endbuf = rs->buf + get_remote_packet_size ();
802188a7 9313
96baa820
JM
9314 *(p++) = 'Z';
9315 *(p++) = '0';
9316 *(p++) = ',';
7c0f6dcc 9317 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 9318 p += hexnumstr (p, addr);
bba74b36 9319 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 9320
efcc2da7 9321 if (remote_supports_cond_breakpoints (ops))
bba74b36 9322 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 9323
78eff0ec 9324 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
9325 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9326
6d820c5c
DJ
9327 putpkt (rs->buf);
9328 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9329
6d820c5c 9330 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 9331 {
d471ea57
AC
9332 case PACKET_ERROR:
9333 return -1;
9334 case PACKET_OK:
7c0f6dcc
JL
9335 bp_tgt->placed_address = addr;
9336 bp_tgt->placed_size = bpsize;
d471ea57
AC
9337 return 0;
9338 case PACKET_UNKNOWN:
9339 break;
96baa820
JM
9340 }
9341 }
c906108c 9342
0000e5cc
PA
9343 /* If this breakpoint has target-side commands but this stub doesn't
9344 support Z0 packets, throw error. */
9345 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
9346 throw_error (NOT_SUPPORTED_ERROR, _("\
9347Target doesn't support breakpoints that have target side commands."));
9348
3db08215 9349 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
9350}
9351
9352static int
3db08215
MM
9353remote_remove_breakpoint (struct target_ops *ops,
9354 struct gdbarch *gdbarch,
73971819
PA
9355 struct bp_target_info *bp_tgt,
9356 enum remove_bp_reason reason)
c906108c 9357{
8181d85f 9358 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 9359 struct remote_state *rs = get_remote_state ();
96baa820 9360
4082afcc 9361 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 9362 {
6d820c5c 9363 char *p = rs->buf;
bba74b36 9364 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 9365
28439a30
PA
9366 /* Make sure the remote is pointing at the right process, if
9367 necessary. */
9368 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9369 set_general_process ();
9370
96baa820
JM
9371 *(p++) = 'z';
9372 *(p++) = '0';
9373 *(p++) = ',';
9374
8181d85f
DJ
9375 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
9376 p += hexnumstr (p, addr);
bba74b36 9377 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 9378
6d820c5c
DJ
9379 putpkt (rs->buf);
9380 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9381
6d820c5c 9382 return (rs->buf[0] == 'E');
96baa820
JM
9383 }
9384
73971819 9385 return memory_remove_breakpoint (ops, gdbarch, bp_tgt, reason);
c906108c
SS
9386}
9387
f486487f 9388static enum Z_packet_type
d471ea57
AC
9389watchpoint_to_Z_packet (int type)
9390{
9391 switch (type)
9392 {
9393 case hw_write:
bb858e6a 9394 return Z_PACKET_WRITE_WP;
d471ea57
AC
9395 break;
9396 case hw_read:
bb858e6a 9397 return Z_PACKET_READ_WP;
d471ea57
AC
9398 break;
9399 case hw_access:
bb858e6a 9400 return Z_PACKET_ACCESS_WP;
d471ea57
AC
9401 break;
9402 default:
8e65ff28 9403 internal_error (__FILE__, __LINE__,
e2e0b3e5 9404 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
9405 }
9406}
9407
3c3bea1c 9408static int
f486487f
SM
9409remote_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9410 enum target_hw_bp_type type, struct expression *cond)
96baa820 9411{
d01949b6 9412 struct remote_state *rs = get_remote_state ();
bba74b36 9413 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9414 char *p;
d471ea57 9415 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 9416
4082afcc 9417 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 9418 return 1;
802188a7 9419
28439a30
PA
9420 /* Make sure the remote is pointing at the right process, if
9421 necessary. */
9422 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9423 set_general_process ();
9424
bba74b36 9425 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 9426 p = strchr (rs->buf, '\0');
96baa820
JM
9427 addr = remote_address_masked (addr);
9428 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9429 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 9430
6d820c5c
DJ
9431 putpkt (rs->buf);
9432 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9433
6d820c5c 9434 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
9435 {
9436 case PACKET_ERROR:
d471ea57 9437 return -1;
85d721b8
PA
9438 case PACKET_UNKNOWN:
9439 return 1;
d471ea57
AC
9440 case PACKET_OK:
9441 return 0;
9442 }
8e65ff28 9443 internal_error (__FILE__, __LINE__,
e2e0b3e5 9444 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
9445}
9446
283002cf
MR
9447static int
9448remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
9449 CORE_ADDR start, int length)
9450{
9451 CORE_ADDR diff = remote_address_masked (addr - start);
9452
9453 return diff < length;
9454}
9455
d471ea57 9456
3c3bea1c 9457static int
f486487f
SM
9458remote_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9459 enum target_hw_bp_type type, struct expression *cond)
96baa820 9460{
d01949b6 9461 struct remote_state *rs = get_remote_state ();
bba74b36 9462 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9463 char *p;
d471ea57
AC
9464 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
9465
4082afcc 9466 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 9467 return -1;
802188a7 9468
28439a30
PA
9469 /* Make sure the remote is pointing at the right process, if
9470 necessary. */
9471 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9472 set_general_process ();
9473
bba74b36 9474 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 9475 p = strchr (rs->buf, '\0');
96baa820
JM
9476 addr = remote_address_masked (addr);
9477 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9478 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
9479 putpkt (rs->buf);
9480 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9481
6d820c5c 9482 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
9483 {
9484 case PACKET_ERROR:
9485 case PACKET_UNKNOWN:
9486 return -1;
9487 case PACKET_OK:
9488 return 0;
9489 }
8e65ff28 9490 internal_error (__FILE__, __LINE__,
e2e0b3e5 9491 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
9492}
9493
3c3bea1c 9494
501eef12 9495int remote_hw_watchpoint_limit = -1;
480a3f21 9496int remote_hw_watchpoint_length_limit = -1;
501eef12 9497int remote_hw_breakpoint_limit = -1;
d471ea57 9498
480a3f21 9499static int
31568a15
TT
9500remote_region_ok_for_hw_watchpoint (struct target_ops *self,
9501 CORE_ADDR addr, int len)
480a3f21
PW
9502{
9503 if (remote_hw_watchpoint_length_limit == 0)
9504 return 0;
9505 else if (remote_hw_watchpoint_length_limit < 0)
9506 return 1;
9507 else if (len <= remote_hw_watchpoint_length_limit)
9508 return 1;
9509 else
9510 return 0;
9511}
9512
b9362cc7 9513static int
5461485a 9514remote_check_watch_resources (struct target_ops *self,
f486487f 9515 enum bptype type, int cnt, int ot)
96baa820 9516{
3c3bea1c
GS
9517 if (type == bp_hardware_breakpoint)
9518 {
9519 if (remote_hw_breakpoint_limit == 0)
9520 return 0;
501eef12
AC
9521 else if (remote_hw_breakpoint_limit < 0)
9522 return 1;
3c3bea1c
GS
9523 else if (cnt <= remote_hw_breakpoint_limit)
9524 return 1;
9525 }
9526 else
9527 {
9528 if (remote_hw_watchpoint_limit == 0)
9529 return 0;
501eef12
AC
9530 else if (remote_hw_watchpoint_limit < 0)
9531 return 1;
3c3bea1c
GS
9532 else if (ot)
9533 return -1;
9534 else if (cnt <= remote_hw_watchpoint_limit)
9535 return 1;
9536 }
9537 return -1;
9538}
9539
f7e6eed5
PA
9540/* The to_stopped_by_sw_breakpoint method of target remote. */
9541
9542static int
9543remote_stopped_by_sw_breakpoint (struct target_ops *ops)
9544{
799a2abe 9545 struct thread_info *thread = inferior_thread ();
f7e6eed5 9546
799a2abe
PA
9547 return (thread->priv != NULL
9548 && thread->priv->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
f7e6eed5
PA
9549}
9550
9551/* The to_supports_stopped_by_sw_breakpoint method of target
9552 remote. */
9553
9554static int
9555remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
9556{
f7e6eed5
PA
9557 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
9558}
9559
9560/* The to_stopped_by_hw_breakpoint method of target remote. */
9561
9562static int
9563remote_stopped_by_hw_breakpoint (struct target_ops *ops)
9564{
799a2abe 9565 struct thread_info *thread = inferior_thread ();
f7e6eed5 9566
799a2abe
PA
9567 return (thread->priv != NULL
9568 && thread->priv->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
f7e6eed5
PA
9569}
9570
9571/* The to_supports_stopped_by_hw_breakpoint method of target
9572 remote. */
9573
9574static int
9575remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
9576{
f7e6eed5
PA
9577 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
9578}
9579
b9362cc7 9580static int
6a109b6b 9581remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 9582{
799a2abe 9583 struct thread_info *thread = inferior_thread ();
ee154bee 9584
799a2abe
PA
9585 return (thread->priv != NULL
9586 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT);
3c3bea1c
GS
9587}
9588
4aa7a7f5
JJ
9589static int
9590remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 9591{
799a2abe 9592 struct thread_info *thread = inferior_thread ();
a744cf53 9593
799a2abe
PA
9594 if (thread->priv != NULL
9595 && thread->priv->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
4aa7a7f5 9596 {
799a2abe
PA
9597 *addr_p = thread->priv->watch_data_address;
9598 return 1;
4aa7a7f5
JJ
9599 }
9600
799a2abe 9601 return 0;
3c3bea1c
GS
9602}
9603
9604
9605static int
23a26771 9606remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 9607 struct bp_target_info *bp_tgt)
3c3bea1c 9608{
0d5ed153 9609 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 9610 struct remote_state *rs;
bba74b36 9611 char *p, *endbuf;
dd61ec5c 9612 char *message;
0d5ed153 9613 int bpsize;
802188a7 9614
c8189ed1 9615 /* The length field should be set to the size of a breakpoint
8181d85f 9616 instruction, even though we aren't inserting one ourselves. */
c8189ed1 9617
0d5ed153 9618 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 9619
4082afcc 9620 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9621 return -1;
2bc416ba 9622
28439a30
PA
9623 /* Make sure the remote is pointing at the right process, if
9624 necessary. */
9625 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9626 set_general_process ();
9627
4fff2411
JZ
9628 rs = get_remote_state ();
9629 p = rs->buf;
bba74b36 9630 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 9631
96baa820
JM
9632 *(p++) = 'Z';
9633 *(p++) = '1';
9634 *(p++) = ',';
802188a7 9635
0d5ed153 9636 addr = remote_address_masked (addr);
96baa820 9637 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 9638 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 9639
efcc2da7 9640 if (remote_supports_cond_breakpoints (self))
bba74b36 9641 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 9642
78eff0ec 9643 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
9644 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9645
6d820c5c
DJ
9646 putpkt (rs->buf);
9647 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9648
6d820c5c 9649 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9650 {
9651 case PACKET_ERROR:
dd61ec5c
MW
9652 if (rs->buf[1] == '.')
9653 {
9654 message = strchr (rs->buf + 2, '.');
9655 if (message)
0316657e 9656 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
9657 }
9658 return -1;
d471ea57
AC
9659 case PACKET_UNKNOWN:
9660 return -1;
9661 case PACKET_OK:
0d5ed153
MR
9662 bp_tgt->placed_address = addr;
9663 bp_tgt->placed_size = bpsize;
d471ea57
AC
9664 return 0;
9665 }
8e65ff28 9666 internal_error (__FILE__, __LINE__,
e2e0b3e5 9667 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
9668}
9669
d471ea57 9670
802188a7 9671static int
a64dc96c 9672remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 9673 struct bp_target_info *bp_tgt)
96baa820 9674{
8181d85f 9675 CORE_ADDR addr;
d01949b6 9676 struct remote_state *rs = get_remote_state ();
6d820c5c 9677 char *p = rs->buf;
bba74b36 9678 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 9679
4082afcc 9680 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9681 return -1;
802188a7 9682
28439a30
PA
9683 /* Make sure the remote is pointing at the right process, if
9684 necessary. */
9685 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9686 set_general_process ();
9687
96baa820
JM
9688 *(p++) = 'z';
9689 *(p++) = '1';
9690 *(p++) = ',';
802188a7 9691
8181d85f 9692 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 9693 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9694 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 9695
6d820c5c
DJ
9696 putpkt (rs->buf);
9697 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 9698
6d820c5c 9699 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9700 {
9701 case PACKET_ERROR:
9702 case PACKET_UNKNOWN:
9703 return -1;
9704 case PACKET_OK:
9705 return 0;
9706 }
8e65ff28 9707 internal_error (__FILE__, __LINE__,
e2e0b3e5 9708 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9709}
96baa820 9710
4a5e7a5b
PA
9711/* Verify memory using the "qCRC:" request. */
9712
9713static int
9714remote_verify_memory (struct target_ops *ops,
9715 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9716{
9717 struct remote_state *rs = get_remote_state ();
9718 unsigned long host_crc, target_crc;
9719 char *tmp;
9720
936d2992
PA
9721 /* It doesn't make sense to use qCRC if the remote target is
9722 connected but not running. */
9723 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9724 {
9725 enum packet_result result;
28439a30 9726
936d2992
PA
9727 /* Make sure the remote is pointing at the right process. */
9728 set_general_process ();
4a5e7a5b 9729
936d2992
PA
9730 /* FIXME: assumes lma can fit into long. */
9731 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9732 (long) lma, (long) size);
9733 putpkt (rs->buf);
4a5e7a5b 9734
936d2992
PA
9735 /* Be clever; compute the host_crc before waiting for target
9736 reply. */
9737 host_crc = xcrc32 (data, size, 0xffffffff);
9738
9739 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9740
936d2992
PA
9741 result = packet_ok (rs->buf,
9742 &remote_protocol_packets[PACKET_qCRC]);
9743 if (result == PACKET_ERROR)
9744 return -1;
9745 else if (result == PACKET_OK)
9746 {
9747 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9748 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9749
936d2992
PA
9750 return (host_crc == target_crc);
9751 }
9752 }
4a5e7a5b 9753
936d2992 9754 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9755}
9756
c906108c
SS
9757/* compare-sections command
9758
9759 With no arguments, compares each loadable section in the exec bfd
9760 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9761 Useful for verifying the image on the target against the exec file. */
e514a9d6 9762
c906108c 9763static void
fba45db2 9764compare_sections_command (char *args, int from_tty)
c906108c
SS
9765{
9766 asection *s;
c906108c 9767 struct cleanup *old_chain;
948f8e3d 9768 gdb_byte *sectdata;
ce359b09 9769 const char *sectname;
c906108c
SS
9770 bfd_size_type size;
9771 bfd_vma lma;
9772 int matched = 0;
9773 int mismatched = 0;
4a5e7a5b 9774 int res;
95cf3b38 9775 int read_only = 0;
c906108c
SS
9776
9777 if (!exec_bfd)
8a3fe4f8 9778 error (_("command cannot be used without an exec file"));
c906108c 9779
28439a30
PA
9780 /* Make sure the remote is pointing at the right process. */
9781 set_general_process ();
9782
95cf3b38
DT
9783 if (args != NULL && strcmp (args, "-r") == 0)
9784 {
9785 read_only = 1;
9786 args = NULL;
9787 }
9788
c5aa993b 9789 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9790 {
9791 if (!(s->flags & SEC_LOAD))
0df8b418 9792 continue; /* Skip non-loadable section. */
c906108c 9793
95cf3b38
DT
9794 if (read_only && (s->flags & SEC_READONLY) == 0)
9795 continue; /* Skip writeable sections */
9796
2c500098 9797 size = bfd_get_section_size (s);
c906108c 9798 if (size == 0)
0df8b418 9799 continue; /* Skip zero-length section. */
c906108c 9800
ce359b09 9801 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9802 if (args && strcmp (args, sectname) != 0)
0df8b418 9803 continue; /* Not the section selected by user. */
c906108c 9804
0df8b418 9805 matched = 1; /* Do this section. */
c906108c 9806 lma = s->lma;
c906108c 9807
224c3ddb 9808 sectdata = (gdb_byte *) xmalloc (size);
b8c9b27d 9809 old_chain = make_cleanup (xfree, sectdata);
c906108c 9810 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9811
4a5e7a5b
PA
9812 res = target_verify_memory (sectdata, lma, size);
9813
9814 if (res == -1)
5af949e3 9815 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9816 paddress (target_gdbarch (), lma),
9817 paddress (target_gdbarch (), lma + size));
c906108c 9818
5af949e3 9819 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9820 paddress (target_gdbarch (), lma),
9821 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9822 if (res)
c906108c
SS
9823 printf_filtered ("matched.\n");
9824 else
c5aa993b
JM
9825 {
9826 printf_filtered ("MIS-MATCHED!\n");
9827 mismatched++;
9828 }
c906108c
SS
9829
9830 do_cleanups (old_chain);
9831 }
9832 if (mismatched > 0)
936d2992 9833 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9834the loaded file\n"));
c906108c 9835 if (args && !matched)
a3f17187 9836 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9837}
9838
0e7f50da
UW
9839/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9840 into remote target. The number of bytes written to the remote
9841 target is returned, or -1 for error. */
9842
9b409511 9843static enum target_xfer_status
0e7f50da
UW
9844remote_write_qxfer (struct target_ops *ops, const char *object_name,
9845 const char *annex, const gdb_byte *writebuf,
9b409511 9846 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9847 struct packet_config *packet)
9848{
9849 int i, buf_len;
9850 ULONGEST n;
0e7f50da
UW
9851 struct remote_state *rs = get_remote_state ();
9852 int max_size = get_memory_write_packet_size ();
9853
9854 if (packet->support == PACKET_DISABLE)
2ed4b548 9855 return TARGET_XFER_E_IO;
0e7f50da
UW
9856
9857 /* Insert header. */
9858 i = snprintf (rs->buf, max_size,
9859 "qXfer:%s:write:%s:%s:",
9860 object_name, annex ? annex : "",
9861 phex_nz (offset, sizeof offset));
9862 max_size -= (i + 1);
9863
9864 /* Escape as much data as fits into rs->buf. */
9865 buf_len = remote_escape_output
124e13d9 9866 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9867
9868 if (putpkt_binary (rs->buf, i + buf_len) < 0
9869 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9870 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9871 return TARGET_XFER_E_IO;
0e7f50da
UW
9872
9873 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9874
9875 *xfered_len = n;
9876 return TARGET_XFER_OK;
0e7f50da
UW
9877}
9878
0876f84a
DJ
9879/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9880 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9881 number of bytes read is returned, or 0 for EOF, or -1 for error.
9882 The number of bytes read may be less than LEN without indicating an
9883 EOF. PACKET is checked and updated to indicate whether the remote
9884 target supports this object. */
9885
9b409511 9886static enum target_xfer_status
0876f84a
DJ
9887remote_read_qxfer (struct target_ops *ops, const char *object_name,
9888 const char *annex,
9889 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9890 ULONGEST *xfered_len,
0876f84a
DJ
9891 struct packet_config *packet)
9892{
0876f84a 9893 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9894 LONGEST i, n, packet_len;
9895
9896 if (packet->support == PACKET_DISABLE)
2ed4b548 9897 return TARGET_XFER_E_IO;
0876f84a
DJ
9898
9899 /* Check whether we've cached an end-of-object packet that matches
9900 this request. */
8e88304f 9901 if (rs->finished_object)
0876f84a 9902 {
8e88304f
TT
9903 if (strcmp (object_name, rs->finished_object) == 0
9904 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9905 && offset == rs->finished_offset)
9b409511
YQ
9906 return TARGET_XFER_EOF;
9907
0876f84a
DJ
9908
9909 /* Otherwise, we're now reading something different. Discard
9910 the cache. */
8e88304f
TT
9911 xfree (rs->finished_object);
9912 xfree (rs->finished_annex);
9913 rs->finished_object = NULL;
9914 rs->finished_annex = NULL;
0876f84a
DJ
9915 }
9916
9917 /* Request only enough to fit in a single packet. The actual data
9918 may not, since we don't know how much of it will need to be escaped;
9919 the target is free to respond with slightly less data. We subtract
9920 five to account for the response type and the protocol frame. */
768adc05 9921 n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
0876f84a
DJ
9922 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9923 object_name, annex ? annex : "",
9924 phex_nz (offset, sizeof offset),
9925 phex_nz (n, sizeof n));
9926 i = putpkt (rs->buf);
9927 if (i < 0)
2ed4b548 9928 return TARGET_XFER_E_IO;
0876f84a
DJ
9929
9930 rs->buf[0] = '\0';
9931 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9932 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9933 return TARGET_XFER_E_IO;
0876f84a
DJ
9934
9935 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9936 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9937
9938 /* 'm' means there is (or at least might be) more data after this
9939 batch. That does not make sense unless there's at least one byte
9940 of data in this reply. */
9941 if (rs->buf[0] == 'm' && packet_len == 1)
9942 error (_("Remote qXfer reply contained no data."));
9943
9944 /* Got some data. */
bc20a4af
PA
9945 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9946 packet_len - 1, readbuf, n);
0876f84a
DJ
9947
9948 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9949 or possibly empty. If we have the final block of a non-empty
9950 object, record this fact to bypass a subsequent partial read. */
9951 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9952 {
8e88304f
TT
9953 rs->finished_object = xstrdup (object_name);
9954 rs->finished_annex = xstrdup (annex ? annex : "");
9955 rs->finished_offset = offset + i;
0876f84a
DJ
9956 }
9957
9b409511
YQ
9958 if (i == 0)
9959 return TARGET_XFER_EOF;
9960 else
9961 {
9962 *xfered_len = i;
9963 return TARGET_XFER_OK;
9964 }
0876f84a
DJ
9965}
9966
9b409511 9967static enum target_xfer_status
4b8a223f 9968remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9969 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9970 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9971 ULONGEST *xfered_len)
c906108c 9972{
82f73884 9973 struct remote_state *rs;
c906108c 9974 int i;
6d820c5c 9975 char *p2;
1e3ff5ad 9976 char query_type;
124e13d9 9977 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9978
e6e4e701 9979 set_remote_traceframe ();
82f73884
PA
9980 set_general_thread (inferior_ptid);
9981
9982 rs = get_remote_state ();
9983
b2182ed2 9984 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9985 if (object == TARGET_OBJECT_MEMORY)
9986 {
2d717e4f
DJ
9987 /* If the remote target is connected but not running, we should
9988 pass this request down to a lower stratum (e.g. the executable
9989 file). */
9990 if (!target_has_execution)
9b409511 9991 return TARGET_XFER_EOF;
2d717e4f 9992
21e3b9b9 9993 if (writebuf != NULL)
124e13d9
SM
9994 return remote_write_bytes (offset, writebuf, len, unit_size,
9995 xfered_len);
21e3b9b9 9996 else
124e13d9
SM
9997 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9998 xfered_len);
21e3b9b9
DJ
9999 }
10000
0df8b418 10001 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
10002 if (object == TARGET_OBJECT_SPU)
10003 {
10004 if (readbuf)
10005 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
10006 xfered_len, &remote_protocol_packets
10007 [PACKET_qXfer_spu_read]);
0e7f50da
UW
10008 else
10009 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
10010 xfered_len, &remote_protocol_packets
10011 [PACKET_qXfer_spu_write]);
0e7f50da
UW
10012 }
10013
4aa995e1
PA
10014 /* Handle extra signal info using qxfer packets. */
10015 if (object == TARGET_OBJECT_SIGNAL_INFO)
10016 {
10017 if (readbuf)
10018 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 10019 xfered_len, &remote_protocol_packets
4aa995e1
PA
10020 [PACKET_qXfer_siginfo_read]);
10021 else
3e43a32a 10022 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 10023 writebuf, offset, len, xfered_len,
4aa995e1
PA
10024 &remote_protocol_packets
10025 [PACKET_qXfer_siginfo_write]);
10026 }
10027
0fb4aa4b
PA
10028 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
10029 {
10030 if (readbuf)
3e43a32a 10031 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 10032 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
10033 &remote_protocol_packets
10034 [PACKET_qXfer_statictrace_read]);
10035 else
2ed4b548 10036 return TARGET_XFER_E_IO;
0fb4aa4b
PA
10037 }
10038
a76d924d
DJ
10039 /* Only handle flash writes. */
10040 if (writebuf != NULL)
10041 {
a76d924d
DJ
10042 switch (object)
10043 {
10044 case TARGET_OBJECT_FLASH:
9b409511
YQ
10045 return remote_flash_write (ops, offset, len, xfered_len,
10046 writebuf);
a76d924d
DJ
10047
10048 default:
2ed4b548 10049 return TARGET_XFER_E_IO;
a76d924d
DJ
10050 }
10051 }
4b8a223f 10052
1e3ff5ad
AC
10053 /* Map pre-existing objects onto letters. DO NOT do this for new
10054 objects!!! Instead specify new query packets. */
10055 switch (object)
c906108c 10056 {
1e3ff5ad
AC
10057 case TARGET_OBJECT_AVR:
10058 query_type = 'R';
10059 break;
802188a7
RM
10060
10061 case TARGET_OBJECT_AUXV:
0876f84a
DJ
10062 gdb_assert (annex == NULL);
10063 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 10064 xfered_len,
0876f84a 10065 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 10066
23181151
DJ
10067 case TARGET_OBJECT_AVAILABLE_FEATURES:
10068 return remote_read_qxfer
9b409511 10069 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
10070 &remote_protocol_packets[PACKET_qXfer_features]);
10071
cfa9d6d9
DJ
10072 case TARGET_OBJECT_LIBRARIES:
10073 return remote_read_qxfer
9b409511 10074 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
10075 &remote_protocol_packets[PACKET_qXfer_libraries]);
10076
2268b414
JK
10077 case TARGET_OBJECT_LIBRARIES_SVR4:
10078 return remote_read_qxfer
9b409511 10079 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
10080 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
10081
fd79ecee
DJ
10082 case TARGET_OBJECT_MEMORY_MAP:
10083 gdb_assert (annex == NULL);
10084 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 10085 xfered_len,
fd79ecee
DJ
10086 &remote_protocol_packets[PACKET_qXfer_memory_map]);
10087
07e059b5
VP
10088 case TARGET_OBJECT_OSDATA:
10089 /* Should only get here if we're connected. */
5d93a237 10090 gdb_assert (rs->remote_desc);
07e059b5 10091 return remote_read_qxfer
9b409511 10092 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
10093 &remote_protocol_packets[PACKET_qXfer_osdata]);
10094
dc146f7c
VP
10095 case TARGET_OBJECT_THREADS:
10096 gdb_assert (annex == NULL);
10097 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 10098 xfered_len,
dc146f7c
VP
10099 &remote_protocol_packets[PACKET_qXfer_threads]);
10100
b3b9301e
PA
10101 case TARGET_OBJECT_TRACEFRAME_INFO:
10102 gdb_assert (annex == NULL);
10103 return remote_read_qxfer
9b409511 10104 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 10105 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
10106
10107 case TARGET_OBJECT_FDPIC:
10108 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 10109 xfered_len,
78d85199 10110 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
10111
10112 case TARGET_OBJECT_OPENVMS_UIB:
10113 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 10114 xfered_len,
169081d0
TG
10115 &remote_protocol_packets[PACKET_qXfer_uib]);
10116
9accd112
MM
10117 case TARGET_OBJECT_BTRACE:
10118 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 10119 xfered_len,
9accd112
MM
10120 &remote_protocol_packets[PACKET_qXfer_btrace]);
10121
f4abbc16
MM
10122 case TARGET_OBJECT_BTRACE_CONF:
10123 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
10124 len, xfered_len,
10125 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
10126
c78fa86a
GB
10127 case TARGET_OBJECT_EXEC_FILE:
10128 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
10129 len, xfered_len,
10130 &remote_protocol_packets[PACKET_qXfer_exec_file]);
10131
1e3ff5ad 10132 default:
2ed4b548 10133 return TARGET_XFER_E_IO;
c906108c
SS
10134 }
10135
0df8b418 10136 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 10137 large enough let the caller deal with it. */
ea9c271d 10138 if (len < get_remote_packet_size ())
2ed4b548 10139 return TARGET_XFER_E_IO;
ea9c271d 10140 len = get_remote_packet_size ();
1e3ff5ad 10141
23860348 10142 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 10143 if (!rs->remote_desc)
8a3fe4f8 10144 error (_("remote query is only available after target open"));
c906108c 10145
1e3ff5ad 10146 gdb_assert (annex != NULL);
4b8a223f 10147 gdb_assert (readbuf != NULL);
c906108c 10148
6d820c5c 10149 p2 = rs->buf;
c906108c
SS
10150 *p2++ = 'q';
10151 *p2++ = query_type;
10152
23860348
MS
10153 /* We used one buffer char for the remote protocol q command and
10154 another for the query type. As the remote protocol encapsulation
10155 uses 4 chars plus one extra in case we are debugging
10156 (remote_debug), we have PBUFZIZ - 7 left to pack the query
10157 string. */
c906108c 10158 i = 0;
ea9c271d 10159 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 10160 {
1e3ff5ad
AC
10161 /* Bad caller may have sent forbidden characters. */
10162 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
10163 *p2++ = annex[i];
c906108c
SS
10164 i++;
10165 }
1e3ff5ad
AC
10166 *p2 = '\0';
10167 gdb_assert (annex[i] == '\0');
c906108c 10168
6d820c5c 10169 i = putpkt (rs->buf);
c5aa993b 10170 if (i < 0)
2ed4b548 10171 return TARGET_XFER_E_IO;
c906108c 10172
6d820c5c
DJ
10173 getpkt (&rs->buf, &rs->buf_size, 0);
10174 strcpy ((char *) readbuf, rs->buf);
c906108c 10175
9b409511
YQ
10176 *xfered_len = strlen ((char *) readbuf);
10177 return TARGET_XFER_OK;
c906108c
SS
10178}
10179
09c98b44
DB
10180/* Implementation of to_get_memory_xfer_limit. */
10181
10182static ULONGEST
10183remote_get_memory_xfer_limit (struct target_ops *ops)
10184{
10185 return get_memory_write_packet_size ();
10186}
10187
08388c79
DE
10188static int
10189remote_search_memory (struct target_ops* ops,
10190 CORE_ADDR start_addr, ULONGEST search_space_len,
10191 const gdb_byte *pattern, ULONGEST pattern_len,
10192 CORE_ADDR *found_addrp)
10193{
f5656ead 10194 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
10195 struct remote_state *rs = get_remote_state ();
10196 int max_size = get_memory_write_packet_size ();
10197 struct packet_config *packet =
10198 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
10199 /* Number of packet bytes used to encode the pattern;
10200 this could be more than PATTERN_LEN due to escape characters. */
08388c79 10201 int escaped_pattern_len;
0df8b418 10202 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
10203 int used_pattern_len;
10204 int i;
10205 int found;
10206 ULONGEST found_addr;
10207
10208 /* Don't go to the target if we don't have to.
10209 This is done before checking packet->support to avoid the possibility that
10210 a success for this edge case means the facility works in general. */
10211 if (pattern_len > search_space_len)
10212 return 0;
10213 if (pattern_len == 0)
10214 {
10215 *found_addrp = start_addr;
10216 return 1;
10217 }
10218
10219 /* If we already know the packet isn't supported, fall back to the simple
10220 way of searching memory. */
10221
4082afcc 10222 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
10223 {
10224 /* Target doesn't provided special support, fall back and use the
10225 standard support (copy memory and do the search here). */
10226 return simple_search_memory (ops, start_addr, search_space_len,
10227 pattern, pattern_len, found_addrp);
10228 }
10229
28439a30
PA
10230 /* Make sure the remote is pointing at the right process. */
10231 set_general_process ();
10232
08388c79
DE
10233 /* Insert header. */
10234 i = snprintf (rs->buf, max_size,
10235 "qSearch:memory:%s;%s;",
5af949e3 10236 phex_nz (start_addr, addr_size),
08388c79
DE
10237 phex_nz (search_space_len, sizeof (search_space_len)));
10238 max_size -= (i + 1);
10239
10240 /* Escape as much data as fits into rs->buf. */
10241 escaped_pattern_len =
124e13d9 10242 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
10243 &used_pattern_len, max_size);
10244
10245 /* Bail if the pattern is too large. */
10246 if (used_pattern_len != pattern_len)
9b20d036 10247 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
10248
10249 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
10250 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
10251 || packet_ok (rs->buf, packet) != PACKET_OK)
10252 {
10253 /* The request may not have worked because the command is not
10254 supported. If so, fall back to the simple way. */
10255 if (packet->support == PACKET_DISABLE)
10256 {
10257 return simple_search_memory (ops, start_addr, search_space_len,
10258 pattern, pattern_len, found_addrp);
10259 }
10260 return -1;
10261 }
10262
10263 if (rs->buf[0] == '0')
10264 found = 0;
10265 else if (rs->buf[0] == '1')
10266 {
10267 found = 1;
10268 if (rs->buf[1] != ',')
10e0fa18 10269 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10270 unpack_varlen_hex (rs->buf + 2, &found_addr);
10271 *found_addrp = found_addr;
10272 }
10273 else
10e0fa18 10274 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
10275
10276 return found;
10277}
10278
96baa820 10279static void
a30bf1f1 10280remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 10281 struct ui_file *outbuf)
96baa820 10282{
d01949b6 10283 struct remote_state *rs = get_remote_state ();
2e9f7625 10284 char *p = rs->buf;
96baa820 10285
5d93a237 10286 if (!rs->remote_desc)
8a3fe4f8 10287 error (_("remote rcmd is only available after target open"));
96baa820 10288
23860348 10289 /* Send a NULL command across as an empty command. */
7be570e7
JM
10290 if (command == NULL)
10291 command = "";
10292
23860348 10293 /* The query prefix. */
2e9f7625
DJ
10294 strcpy (rs->buf, "qRcmd,");
10295 p = strchr (rs->buf, '\0');
96baa820 10296
3e43a32a
MS
10297 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
10298 > get_remote_packet_size ())
8a3fe4f8 10299 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 10300
23860348 10301 /* Encode the actual command. */
a30bf1f1 10302 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 10303
6d820c5c 10304 if (putpkt (rs->buf) < 0)
8a3fe4f8 10305 error (_("Communication problem with target."));
96baa820
JM
10306
10307 /* get/display the response */
10308 while (1)
10309 {
2e9f7625
DJ
10310 char *buf;
10311
00bf0b85 10312 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 10313 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 10314 rs->buf[0] = '\0';
5b37825d
PW
10315 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
10316 {
10317 /* Timeout. Continue to (try to) read responses.
10318 This is better than stopping with an error, assuming the stub
10319 is still executing the (long) monitor command.
10320 If needed, the user can interrupt gdb using C-c, obtaining
10321 an effect similar to stop on timeout. */
10322 continue;
10323 }
2e9f7625 10324 buf = rs->buf;
96baa820 10325 if (buf[0] == '\0')
8a3fe4f8 10326 error (_("Target does not support this command."));
96baa820
JM
10327 if (buf[0] == 'O' && buf[1] != 'K')
10328 {
23860348 10329 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
10330 continue;
10331 }
10332 if (strcmp (buf, "OK") == 0)
10333 break;
7be570e7
JM
10334 if (strlen (buf) == 3 && buf[0] == 'E'
10335 && isdigit (buf[1]) && isdigit (buf[2]))
10336 {
8a3fe4f8 10337 error (_("Protocol error with Rcmd"));
7be570e7 10338 }
96baa820
JM
10339 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
10340 {
10341 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 10342
96baa820
JM
10343 fputc_unfiltered (c, outbuf);
10344 }
10345 break;
10346 }
10347}
10348
fd79ecee
DJ
10349static VEC(mem_region_s) *
10350remote_memory_map (struct target_ops *ops)
10351{
10352 VEC(mem_region_s) *result = NULL;
10353 char *text = target_read_stralloc (&current_target,
10354 TARGET_OBJECT_MEMORY_MAP, NULL);
10355
10356 if (text)
10357 {
10358 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 10359
fd79ecee
DJ
10360 result = parse_memory_map (text);
10361 do_cleanups (back_to);
10362 }
10363
10364 return result;
10365}
10366
c906108c 10367static void
fba45db2 10368packet_command (char *args, int from_tty)
c906108c 10369{
d01949b6 10370 struct remote_state *rs = get_remote_state ();
c906108c 10371
5d93a237 10372 if (!rs->remote_desc)
8a3fe4f8 10373 error (_("command can only be used with remote target"));
c906108c 10374
c5aa993b 10375 if (!args)
8a3fe4f8 10376 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
10377
10378 puts_filtered ("sending: ");
10379 print_packet (args);
10380 puts_filtered ("\n");
10381 putpkt (args);
10382
6d820c5c 10383 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 10384 puts_filtered ("received: ");
6d820c5c 10385 print_packet (rs->buf);
c906108c
SS
10386 puts_filtered ("\n");
10387}
10388
10389#if 0
23860348 10390/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 10391
a14ed312 10392static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 10393
a14ed312 10394static void threadset_test_cmd (char *cmd, int tty);
c906108c 10395
a14ed312 10396static void threadalive_test (char *cmd, int tty);
c906108c 10397
a14ed312 10398static void threadlist_test_cmd (char *cmd, int tty);
c906108c 10399
23860348 10400int get_and_display_threadinfo (threadref *ref);
c906108c 10401
a14ed312 10402static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 10403
23860348 10404static int thread_display_step (threadref *ref, void *context);
c906108c 10405
a14ed312 10406static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 10407
a14ed312 10408static void init_remote_threadtests (void);
c906108c 10409
23860348 10410#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
10411
10412static void
fba45db2 10413threadset_test_cmd (char *cmd, int tty)
c906108c
SS
10414{
10415 int sample_thread = SAMPLE_THREAD;
10416
a3f17187 10417 printf_filtered (_("Remote threadset test\n"));
79d7f229 10418 set_general_thread (sample_thread);
c906108c
SS
10419}
10420
10421
10422static void
fba45db2 10423threadalive_test (char *cmd, int tty)
c906108c
SS
10424{
10425 int sample_thread = SAMPLE_THREAD;
79d7f229 10426 int pid = ptid_get_pid (inferior_ptid);
ba348170 10427 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 10428
79d7f229 10429 if (remote_thread_alive (ptid))
c906108c
SS
10430 printf_filtered ("PASS: Thread alive test\n");
10431 else
10432 printf_filtered ("FAIL: Thread alive test\n");
10433}
10434
23860348 10435void output_threadid (char *title, threadref *ref);
c906108c
SS
10436
10437void
fba45db2 10438output_threadid (char *title, threadref *ref)
c906108c
SS
10439{
10440 char hexid[20];
10441
23860348 10442 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
10443 hexid[16] = 0;
10444 printf_filtered ("%s %s\n", title, (&hexid[0]));
10445}
10446
10447static void
fba45db2 10448threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
10449{
10450 int startflag = 1;
10451 threadref nextthread;
10452 int done, result_count;
10453 threadref threadlist[3];
10454
10455 printf_filtered ("Remote Threadlist test\n");
10456 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
10457 &result_count, &threadlist[0]))
10458 printf_filtered ("FAIL: threadlist test\n");
10459 else
10460 {
10461 threadref *scan = threadlist;
10462 threadref *limit = scan + result_count;
10463
10464 while (scan < limit)
10465 output_threadid (" thread ", scan++);
10466 }
10467}
10468
10469void
fba45db2 10470display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
10471{
10472 output_threadid ("Threadid: ", &info->threadid);
10473 printf_filtered ("Name: %s\n ", info->shortname);
10474 printf_filtered ("State: %s\n", info->display);
10475 printf_filtered ("other: %s\n\n", info->more_display);
10476}
10477
10478int
fba45db2 10479get_and_display_threadinfo (threadref *ref)
c906108c
SS
10480{
10481 int result;
10482 int set;
10483 struct gdb_ext_thread_info threadinfo;
10484
10485 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
10486 | TAG_MOREDISPLAY | TAG_DISPLAY;
10487 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
10488 display_thread_info (&threadinfo);
10489 return result;
10490}
10491
10492static void
fba45db2 10493threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
10494{
10495 int athread = SAMPLE_THREAD;
10496 threadref thread;
10497 int set;
10498
10499 int_to_threadref (&thread, athread);
10500 printf_filtered ("Remote Threadinfo test\n");
10501 if (!get_and_display_threadinfo (&thread))
10502 printf_filtered ("FAIL cannot get thread info\n");
10503}
10504
10505static int
fba45db2 10506thread_display_step (threadref *ref, void *context)
c906108c
SS
10507{
10508 /* output_threadid(" threadstep ",ref); *//* simple test */
10509 return get_and_display_threadinfo (ref);
10510}
10511
10512static void
fba45db2 10513threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
10514{
10515 printf_filtered ("Remote Threadlist update test\n");
10516 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
10517}
10518
10519static void
10520init_remote_threadtests (void)
10521{
3e43a32a
MS
10522 add_com ("tlist", class_obscure, threadlist_test_cmd,
10523 _("Fetch and print the remote list of "
10524 "thread identifiers, one pkt only"));
c906108c 10525 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 10526 _("Fetch and display info about one thread"));
c906108c 10527 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 10528 _("Test setting to a different thread"));
c906108c 10529 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 10530 _("Iterate through updating all remote thread info"));
c906108c 10531 add_com ("talive", class_obscure, threadalive_test,
1bedd215 10532 _(" Remote thread alive test "));
c906108c
SS
10533}
10534
10535#endif /* 0 */
10536
f3fb8c85
MS
10537/* Convert a thread ID to a string. Returns the string in a static
10538 buffer. */
10539
10540static char *
117de6a9 10541remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 10542{
79d7f229 10543 static char buf[64];
82f73884 10544 struct remote_state *rs = get_remote_state ();
f3fb8c85 10545
7cee1e54
PA
10546 if (ptid_equal (ptid, null_ptid))
10547 return normal_pid_to_str (ptid);
10548 else if (ptid_is_pid (ptid))
ecd0ada5
PA
10549 {
10550 /* Printing an inferior target id. */
10551
10552 /* When multi-process extensions are off, there's no way in the
10553 remote protocol to know the remote process id, if there's any
10554 at all. There's one exception --- when we're connected with
10555 target extended-remote, and we manually attached to a process
10556 with "attach PID". We don't record anywhere a flag that
10557 allows us to distinguish that case from the case of
10558 connecting with extended-remote and the stub already being
10559 attached to a process, and reporting yes to qAttached, hence
10560 no smart special casing here. */
10561 if (!remote_multi_process_p (rs))
10562 {
10563 xsnprintf (buf, sizeof buf, "Remote target");
10564 return buf;
10565 }
10566
10567 return normal_pid_to_str (ptid);
82f73884 10568 }
ecd0ada5 10569 else
79d7f229 10570 {
ecd0ada5
PA
10571 if (ptid_equal (magic_null_ptid, ptid))
10572 xsnprintf (buf, sizeof buf, "Thread <main>");
8020350c 10573 else if (remote_multi_process_p (rs))
de0d863e
DB
10574 if (ptid_get_lwp (ptid) == 0)
10575 return normal_pid_to_str (ptid);
10576 else
10577 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
10578 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
10579 else
10580 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 10581 ptid_get_lwp (ptid));
79d7f229
PA
10582 return buf;
10583 }
f3fb8c85
MS
10584}
10585
38691318
KB
10586/* Get the address of the thread local variable in OBJFILE which is
10587 stored at OFFSET within the thread local storage for thread PTID. */
10588
10589static CORE_ADDR
117de6a9
PA
10590remote_get_thread_local_address (struct target_ops *ops,
10591 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 10592{
4082afcc 10593 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
10594 {
10595 struct remote_state *rs = get_remote_state ();
6d820c5c 10596 char *p = rs->buf;
82f73884 10597 char *endp = rs->buf + get_remote_packet_size ();
571dd617 10598 enum packet_result result;
38691318
KB
10599
10600 strcpy (p, "qGetTLSAddr:");
10601 p += strlen (p);
82f73884 10602 p = write_ptid (p, endp, ptid);
38691318
KB
10603 *p++ = ',';
10604 p += hexnumstr (p, offset);
10605 *p++ = ',';
10606 p += hexnumstr (p, lm);
10607 *p++ = '\0';
10608
6d820c5c
DJ
10609 putpkt (rs->buf);
10610 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
10611 result = packet_ok (rs->buf,
10612 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 10613 if (result == PACKET_OK)
38691318
KB
10614 {
10615 ULONGEST result;
10616
6d820c5c 10617 unpack_varlen_hex (rs->buf, &result);
38691318
KB
10618 return result;
10619 }
571dd617 10620 else if (result == PACKET_UNKNOWN)
109c3e39
AC
10621 throw_error (TLS_GENERIC_ERROR,
10622 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 10623 else
109c3e39
AC
10624 throw_error (TLS_GENERIC_ERROR,
10625 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
10626 }
10627 else
109c3e39
AC
10628 throw_error (TLS_GENERIC_ERROR,
10629 _("TLS not supported or disabled on this target"));
38691318
KB
10630 /* Not reached. */
10631 return 0;
10632}
10633
711e434b
PM
10634/* Provide thread local base, i.e. Thread Information Block address.
10635 Returns 1 if ptid is found and thread_local_base is non zero. */
10636
70221824 10637static int
bd7ae0f5 10638remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 10639{
4082afcc 10640 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
10641 {
10642 struct remote_state *rs = get_remote_state ();
10643 char *p = rs->buf;
10644 char *endp = rs->buf + get_remote_packet_size ();
10645 enum packet_result result;
10646
10647 strcpy (p, "qGetTIBAddr:");
10648 p += strlen (p);
10649 p = write_ptid (p, endp, ptid);
10650 *p++ = '\0';
10651
10652 putpkt (rs->buf);
10653 getpkt (&rs->buf, &rs->buf_size, 0);
10654 result = packet_ok (rs->buf,
10655 &remote_protocol_packets[PACKET_qGetTIBAddr]);
10656 if (result == PACKET_OK)
10657 {
10658 ULONGEST result;
10659
10660 unpack_varlen_hex (rs->buf, &result);
10661 if (addr)
10662 *addr = (CORE_ADDR) result;
10663 return 1;
10664 }
10665 else if (result == PACKET_UNKNOWN)
10666 error (_("Remote target doesn't support qGetTIBAddr packet"));
10667 else
10668 error (_("Remote target failed to process qGetTIBAddr request"));
10669 }
10670 else
10671 error (_("qGetTIBAddr not supported or disabled on this target"));
10672 /* Not reached. */
10673 return 0;
10674}
10675
29709017
DJ
10676/* Support for inferring a target description based on the current
10677 architecture and the size of a 'g' packet. While the 'g' packet
10678 can have any size (since optional registers can be left off the
10679 end), some sizes are easily recognizable given knowledge of the
10680 approximate architecture. */
10681
10682struct remote_g_packet_guess
10683{
10684 int bytes;
10685 const struct target_desc *tdesc;
10686};
10687typedef struct remote_g_packet_guess remote_g_packet_guess_s;
10688DEF_VEC_O(remote_g_packet_guess_s);
10689
10690struct remote_g_packet_data
10691{
10692 VEC(remote_g_packet_guess_s) *guesses;
10693};
10694
10695static struct gdbarch_data *remote_g_packet_data_handle;
10696
10697static void *
10698remote_g_packet_data_init (struct obstack *obstack)
10699{
10700 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
10701}
10702
10703void
10704register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
10705 const struct target_desc *tdesc)
10706{
10707 struct remote_g_packet_data *data
19ba03f4
SM
10708 = ((struct remote_g_packet_data *)
10709 gdbarch_data (gdbarch, remote_g_packet_data_handle));
29709017
DJ
10710 struct remote_g_packet_guess new_guess, *guess;
10711 int ix;
10712
10713 gdb_assert (tdesc != NULL);
10714
10715 for (ix = 0;
10716 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10717 ix++)
10718 if (guess->bytes == bytes)
10719 internal_error (__FILE__, __LINE__,
9b20d036 10720 _("Duplicate g packet description added for size %d"),
29709017
DJ
10721 bytes);
10722
10723 new_guess.bytes = bytes;
10724 new_guess.tdesc = tdesc;
10725 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10726}
10727
d962ef82
DJ
10728/* Return 1 if remote_read_description would do anything on this target
10729 and architecture, 0 otherwise. */
10730
10731static int
10732remote_read_description_p (struct target_ops *target)
10733{
10734 struct remote_g_packet_data *data
19ba03f4
SM
10735 = ((struct remote_g_packet_data *)
10736 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
d962ef82
DJ
10737
10738 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10739 return 1;
10740
10741 return 0;
10742}
10743
29709017
DJ
10744static const struct target_desc *
10745remote_read_description (struct target_ops *target)
10746{
10747 struct remote_g_packet_data *data
19ba03f4
SM
10748 = ((struct remote_g_packet_data *)
10749 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
29709017 10750
d962ef82
DJ
10751 /* Do not try this during initial connection, when we do not know
10752 whether there is a running but stopped thread. */
10753 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10754 return target->beneath->to_read_description (target->beneath);
d962ef82 10755
29709017
DJ
10756 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10757 {
10758 struct remote_g_packet_guess *guess;
10759 int ix;
10760 int bytes = send_g_packet ();
10761
10762 for (ix = 0;
10763 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10764 ix++)
10765 if (guess->bytes == bytes)
10766 return guess->tdesc;
10767
10768 /* We discard the g packet. A minor optimization would be to
10769 hold on to it, and fill the register cache once we have selected
10770 an architecture, but it's too tricky to do safely. */
10771 }
10772
2117c711 10773 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10774}
10775
a6b151f1
DJ
10776/* Remote file transfer support. This is host-initiated I/O, not
10777 target-initiated; for target-initiated, see remote-fileio.c. */
10778
10779/* If *LEFT is at least the length of STRING, copy STRING to
10780 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10781 decrease *LEFT. Otherwise raise an error. */
10782
10783static void
10784remote_buffer_add_string (char **buffer, int *left, char *string)
10785{
10786 int len = strlen (string);
10787
10788 if (len > *left)
10789 error (_("Packet too long for target."));
10790
10791 memcpy (*buffer, string, len);
10792 *buffer += len;
10793 *left -= len;
10794
10795 /* NUL-terminate the buffer as a convenience, if there is
10796 room. */
10797 if (*left)
10798 **buffer = '\0';
10799}
10800
10801/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10802 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10803 decrease *LEFT. Otherwise raise an error. */
10804
10805static void
10806remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10807 int len)
10808{
10809 if (2 * len > *left)
10810 error (_("Packet too long for target."));
10811
10812 bin2hex (bytes, *buffer, len);
10813 *buffer += 2 * len;
10814 *left -= 2 * len;
10815
10816 /* NUL-terminate the buffer as a convenience, if there is
10817 room. */
10818 if (*left)
10819 **buffer = '\0';
10820}
10821
10822/* If *LEFT is large enough, convert VALUE to hex and add it to
10823 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10824 decrease *LEFT. Otherwise raise an error. */
10825
10826static void
10827remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10828{
10829 int len = hexnumlen (value);
10830
10831 if (len > *left)
10832 error (_("Packet too long for target."));
10833
10834 hexnumstr (*buffer, value);
10835 *buffer += len;
10836 *left -= len;
10837
10838 /* NUL-terminate the buffer as a convenience, if there is
10839 room. */
10840 if (*left)
10841 **buffer = '\0';
10842}
10843
10844/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10845 value, *REMOTE_ERRNO to the remote error number or zero if none
10846 was included, and *ATTACHMENT to point to the start of the annex
10847 if any. The length of the packet isn't needed here; there may
10848 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10849
10850 Return 0 if the packet could be parsed, -1 if it could not. If
10851 -1 is returned, the other variables may not be initialized. */
10852
10853static int
10854remote_hostio_parse_result (char *buffer, int *retcode,
10855 int *remote_errno, char **attachment)
10856{
10857 char *p, *p2;
10858
10859 *remote_errno = 0;
10860 *attachment = NULL;
10861
10862 if (buffer[0] != 'F')
10863 return -1;
10864
10865 errno = 0;
10866 *retcode = strtol (&buffer[1], &p, 16);
10867 if (errno != 0 || p == &buffer[1])
10868 return -1;
10869
10870 /* Check for ",errno". */
10871 if (*p == ',')
10872 {
10873 errno = 0;
10874 *remote_errno = strtol (p + 1, &p2, 16);
10875 if (errno != 0 || p + 1 == p2)
10876 return -1;
10877 p = p2;
10878 }
10879
10880 /* Check for ";attachment". If there is no attachment, the
10881 packet should end here. */
10882 if (*p == ';')
10883 {
10884 *attachment = p + 1;
10885 return 0;
10886 }
10887 else if (*p == '\0')
10888 return 0;
10889 else
10890 return -1;
10891}
10892
10893/* Send a prepared I/O packet to the target and read its response.
10894 The prepared packet is in the global RS->BUF before this function
10895 is called, and the answer is there when we return.
10896
10897 COMMAND_BYTES is the length of the request to send, which may include
10898 binary data. WHICH_PACKET is the packet configuration to check
10899 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10900 is set to the error number and -1 is returned. Otherwise the value
10901 returned by the function is returned.
10902
10903 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10904 attachment is expected; an error will be reported if there's a
10905 mismatch. If one is found, *ATTACHMENT will be set to point into
10906 the packet buffer and *ATTACHMENT_LEN will be set to the
10907 attachment's length. */
10908
10909static int
10910remote_hostio_send_command (int command_bytes, int which_packet,
10911 int *remote_errno, char **attachment,
10912 int *attachment_len)
10913{
10914 struct remote_state *rs = get_remote_state ();
10915 int ret, bytes_read;
10916 char *attachment_tmp;
10917
5d93a237 10918 if (!rs->remote_desc
4082afcc 10919 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10920 {
10921 *remote_errno = FILEIO_ENOSYS;
10922 return -1;
10923 }
10924
10925 putpkt_binary (rs->buf, command_bytes);
10926 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10927
10928 /* If it timed out, something is wrong. Don't try to parse the
10929 buffer. */
10930 if (bytes_read < 0)
10931 {
10932 *remote_errno = FILEIO_EINVAL;
10933 return -1;
10934 }
10935
10936 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10937 {
10938 case PACKET_ERROR:
10939 *remote_errno = FILEIO_EINVAL;
10940 return -1;
10941 case PACKET_UNKNOWN:
10942 *remote_errno = FILEIO_ENOSYS;
10943 return -1;
10944 case PACKET_OK:
10945 break;
10946 }
10947
10948 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10949 &attachment_tmp))
10950 {
10951 *remote_errno = FILEIO_EINVAL;
10952 return -1;
10953 }
10954
10955 /* Make sure we saw an attachment if and only if we expected one. */
10956 if ((attachment_tmp == NULL && attachment != NULL)
10957 || (attachment_tmp != NULL && attachment == NULL))
10958 {
10959 *remote_errno = FILEIO_EINVAL;
10960 return -1;
10961 }
10962
10963 /* If an attachment was found, it must point into the packet buffer;
10964 work out how many bytes there were. */
10965 if (attachment_tmp != NULL)
10966 {
10967 *attachment = attachment_tmp;
10968 *attachment_len = bytes_read - (*attachment - rs->buf);
10969 }
10970
10971 return ret;
10972}
10973
80152258
PA
10974/* Invalidate the readahead cache. */
10975
10976static void
10977readahead_cache_invalidate (void)
10978{
10979 struct remote_state *rs = get_remote_state ();
10980
10981 rs->readahead_cache.fd = -1;
10982}
10983
10984/* Invalidate the readahead cache if it is holding data for FD. */
10985
10986static void
10987readahead_cache_invalidate_fd (int fd)
10988{
10989 struct remote_state *rs = get_remote_state ();
10990
10991 if (rs->readahead_cache.fd == fd)
10992 rs->readahead_cache.fd = -1;
10993}
10994
15a201c8
GB
10995/* Set the filesystem remote_hostio functions that take FILENAME
10996 arguments will use. Return 0 on success, or -1 if an error
10997 occurs (and set *REMOTE_ERRNO). */
10998
10999static int
11000remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
11001{
11002 struct remote_state *rs = get_remote_state ();
11003 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
11004 char *p = rs->buf;
11005 int left = get_remote_packet_size () - 1;
11006 char arg[9];
11007 int ret;
11008
11009 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11010 return 0;
11011
11012 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
11013 return 0;
11014
11015 remote_buffer_add_string (&p, &left, "vFile:setfs:");
11016
11017 xsnprintf (arg, sizeof (arg), "%x", required_pid);
11018 remote_buffer_add_string (&p, &left, arg);
11019
11020 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
11021 remote_errno, NULL, NULL);
11022
11023 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
11024 return 0;
11025
11026 if (ret == 0)
11027 rs->fs_pid = required_pid;
11028
11029 return ret;
11030}
11031
12e2a5fd 11032/* Implementation of to_fileio_open. */
a6b151f1
DJ
11033
11034static int
cd897586 11035remote_hostio_open (struct target_ops *self,
07c138c8 11036 struct inferior *inf, const char *filename,
4313b8c0
GB
11037 int flags, int mode, int warn_if_slow,
11038 int *remote_errno)
a6b151f1
DJ
11039{
11040 struct remote_state *rs = get_remote_state ();
11041 char *p = rs->buf;
11042 int left = get_remote_packet_size () - 1;
11043
4313b8c0
GB
11044 if (warn_if_slow)
11045 {
11046 static int warning_issued = 0;
11047
11048 printf_unfiltered (_("Reading %s from remote target...\n"),
11049 filename);
11050
11051 if (!warning_issued)
11052 {
11053 warning (_("File transfers from remote targets can be slow."
11054 " Use \"set sysroot\" to access files locally"
11055 " instead."));
11056 warning_issued = 1;
11057 }
11058 }
11059
15a201c8
GB
11060 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11061 return -1;
11062
a6b151f1
DJ
11063 remote_buffer_add_string (&p, &left, "vFile:open:");
11064
11065 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11066 strlen (filename));
11067 remote_buffer_add_string (&p, &left, ",");
11068
11069 remote_buffer_add_int (&p, &left, flags);
11070 remote_buffer_add_string (&p, &left, ",");
11071
11072 remote_buffer_add_int (&p, &left, mode);
11073
11074 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
11075 remote_errno, NULL, NULL);
11076}
11077
12e2a5fd 11078/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
11079
11080static int
0d866f62
TT
11081remote_hostio_pwrite (struct target_ops *self,
11082 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
11083 ULONGEST offset, int *remote_errno)
11084{
11085 struct remote_state *rs = get_remote_state ();
11086 char *p = rs->buf;
11087 int left = get_remote_packet_size ();
11088 int out_len;
11089
80152258
PA
11090 readahead_cache_invalidate_fd (fd);
11091
a6b151f1
DJ
11092 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
11093
11094 remote_buffer_add_int (&p, &left, fd);
11095 remote_buffer_add_string (&p, &left, ",");
11096
11097 remote_buffer_add_int (&p, &left, offset);
11098 remote_buffer_add_string (&p, &left, ",");
11099
124e13d9 11100 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
11101 get_remote_packet_size () - (p - rs->buf));
11102
11103 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
11104 remote_errno, NULL, NULL);
11105}
11106
80152258
PA
11107/* Helper for the implementation of to_fileio_pread. Read the file
11108 from the remote side with vFile:pread. */
a6b151f1
DJ
11109
11110static int
80152258
PA
11111remote_hostio_pread_vFile (struct target_ops *self,
11112 int fd, gdb_byte *read_buf, int len,
11113 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
11114{
11115 struct remote_state *rs = get_remote_state ();
11116 char *p = rs->buf;
11117 char *attachment;
11118 int left = get_remote_packet_size ();
11119 int ret, attachment_len;
11120 int read_len;
11121
11122 remote_buffer_add_string (&p, &left, "vFile:pread:");
11123
11124 remote_buffer_add_int (&p, &left, fd);
11125 remote_buffer_add_string (&p, &left, ",");
11126
11127 remote_buffer_add_int (&p, &left, len);
11128 remote_buffer_add_string (&p, &left, ",");
11129
11130 remote_buffer_add_int (&p, &left, offset);
11131
11132 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
11133 remote_errno, &attachment,
11134 &attachment_len);
11135
11136 if (ret < 0)
11137 return ret;
11138
bc20a4af 11139 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
11140 read_buf, len);
11141 if (read_len != ret)
11142 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
11143
11144 return ret;
11145}
11146
80152258
PA
11147/* Serve pread from the readahead cache. Returns number of bytes
11148 read, or 0 if the request can't be served from the cache. */
11149
11150static int
11151remote_hostio_pread_from_cache (struct remote_state *rs,
11152 int fd, gdb_byte *read_buf, size_t len,
11153 ULONGEST offset)
11154{
11155 struct readahead_cache *cache = &rs->readahead_cache;
11156
11157 if (cache->fd == fd
11158 && cache->offset <= offset
11159 && offset < cache->offset + cache->bufsize)
11160 {
11161 ULONGEST max = cache->offset + cache->bufsize;
11162
11163 if (offset + len > max)
11164 len = max - offset;
11165
11166 memcpy (read_buf, cache->buf + offset - cache->offset, len);
11167 return len;
11168 }
11169
11170 return 0;
11171}
11172
11173/* Implementation of to_fileio_pread. */
11174
11175static int
11176remote_hostio_pread (struct target_ops *self,
11177 int fd, gdb_byte *read_buf, int len,
11178 ULONGEST offset, int *remote_errno)
11179{
11180 int ret;
11181 struct remote_state *rs = get_remote_state ();
11182 struct readahead_cache *cache = &rs->readahead_cache;
11183
11184 ret = remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11185 if (ret > 0)
11186 {
11187 cache->hit_count++;
11188
11189 if (remote_debug)
11190 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
11191 pulongest (cache->hit_count));
11192 return ret;
11193 }
11194
11195 cache->miss_count++;
11196 if (remote_debug)
11197 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
11198 pulongest (cache->miss_count));
11199
11200 cache->fd = fd;
11201 cache->offset = offset;
11202 cache->bufsize = get_remote_packet_size ();
224c3ddb 11203 cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
80152258
PA
11204
11205 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
11206 cache->offset, remote_errno);
11207 if (ret <= 0)
11208 {
11209 readahead_cache_invalidate_fd (fd);
11210 return ret;
11211 }
11212
11213 cache->bufsize = ret;
11214 return remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
11215}
11216
12e2a5fd 11217/* Implementation of to_fileio_close. */
a6b151f1
DJ
11218
11219static int
df39ea25 11220remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
11221{
11222 struct remote_state *rs = get_remote_state ();
11223 char *p = rs->buf;
11224 int left = get_remote_packet_size () - 1;
11225
80152258
PA
11226 readahead_cache_invalidate_fd (fd);
11227
a6b151f1
DJ
11228 remote_buffer_add_string (&p, &left, "vFile:close:");
11229
11230 remote_buffer_add_int (&p, &left, fd);
11231
11232 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
11233 remote_errno, NULL, NULL);
11234}
11235
12e2a5fd 11236/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
11237
11238static int
dbbca37d 11239remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
11240 struct inferior *inf, const char *filename,
11241 int *remote_errno)
a6b151f1
DJ
11242{
11243 struct remote_state *rs = get_remote_state ();
11244 char *p = rs->buf;
11245 int left = get_remote_packet_size () - 1;
11246
15a201c8
GB
11247 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11248 return -1;
11249
a6b151f1
DJ
11250 remote_buffer_add_string (&p, &left, "vFile:unlink:");
11251
11252 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11253 strlen (filename));
11254
11255 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
11256 remote_errno, NULL, NULL);
11257}
11258
12e2a5fd 11259/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
11260
11261static char *
fab5aa7c 11262remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
11263 struct inferior *inf, const char *filename,
11264 int *remote_errno)
b9e7b9c3
UW
11265{
11266 struct remote_state *rs = get_remote_state ();
11267 char *p = rs->buf;
11268 char *attachment;
11269 int left = get_remote_packet_size ();
11270 int len, attachment_len;
11271 int read_len;
11272 char *ret;
11273
15a201c8
GB
11274 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
11275 return NULL;
11276
b9e7b9c3
UW
11277 remote_buffer_add_string (&p, &left, "vFile:readlink:");
11278
11279 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
11280 strlen (filename));
11281
11282 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
11283 remote_errno, &attachment,
11284 &attachment_len);
11285
11286 if (len < 0)
11287 return NULL;
11288
224c3ddb 11289 ret = (char *) xmalloc (len + 1);
b9e7b9c3 11290
bc20a4af
PA
11291 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11292 (gdb_byte *) ret, len);
b9e7b9c3
UW
11293 if (read_len != len)
11294 error (_("Readlink returned %d, but %d bytes."), len, read_len);
11295
11296 ret[len] = '\0';
11297 return ret;
11298}
11299
12e2a5fd 11300/* Implementation of to_fileio_fstat. */
0a93529c
GB
11301
11302static int
11303remote_hostio_fstat (struct target_ops *self,
11304 int fd, struct stat *st,
11305 int *remote_errno)
11306{
11307 struct remote_state *rs = get_remote_state ();
11308 char *p = rs->buf;
11309 int left = get_remote_packet_size ();
11310 int attachment_len, ret;
11311 char *attachment;
11312 struct fio_stat fst;
11313 int read_len;
11314
464b0089
GB
11315 remote_buffer_add_string (&p, &left, "vFile:fstat:");
11316
11317 remote_buffer_add_int (&p, &left, fd);
11318
11319 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
11320 remote_errno, &attachment,
11321 &attachment_len);
11322 if (ret < 0)
0a93529c 11323 {
464b0089
GB
11324 if (*remote_errno != FILEIO_ENOSYS)
11325 return ret;
11326
0a93529c
GB
11327 /* Strictly we should return -1, ENOSYS here, but when
11328 "set sysroot remote:" was implemented in August 2008
11329 BFD's need for a stat function was sidestepped with
11330 this hack. This was not remedied until March 2015
11331 so we retain the previous behavior to avoid breaking
11332 compatibility.
11333
11334 Note that the memset is a March 2015 addition; older
11335 GDBs set st_size *and nothing else* so the structure
11336 would have garbage in all other fields. This might
11337 break something but retaining the previous behavior
11338 here would be just too wrong. */
11339
11340 memset (st, 0, sizeof (struct stat));
11341 st->st_size = INT_MAX;
11342 return 0;
11343 }
11344
0a93529c
GB
11345 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
11346 (gdb_byte *) &fst, sizeof (fst));
11347
11348 if (read_len != ret)
11349 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
11350
11351 if (read_len != sizeof (fst))
11352 error (_("vFile:fstat returned %d bytes, but expecting %d."),
11353 read_len, (int) sizeof (fst));
11354
11355 remote_fileio_to_host_stat (&fst, st);
11356
11357 return 0;
11358}
11359
12e2a5fd 11360/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
11361
11362static int
11363remote_filesystem_is_local (struct target_ops *self)
11364{
11365 /* Valgrind GDB presents itself as a remote target but works
11366 on the local filesystem: it does not implement remote get
11367 and users are not expected to set a sysroot. To handle
11368 this case we treat the remote filesystem as local if the
11369 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
11370 does not support vFile:open. */
a3be80c3 11371 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
11372 {
11373 enum packet_support ps = packet_support (PACKET_vFile_open);
11374
11375 if (ps == PACKET_SUPPORT_UNKNOWN)
11376 {
11377 int fd, remote_errno;
11378
11379 /* Try opening a file to probe support. The supplied
11380 filename is irrelevant, we only care about whether
11381 the stub recognizes the packet or not. */
07c138c8 11382 fd = remote_hostio_open (self, NULL, "just probing",
4313b8c0 11383 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
11384 &remote_errno);
11385
11386 if (fd >= 0)
11387 remote_hostio_close (self, fd, &remote_errno);
11388
11389 ps = packet_support (PACKET_vFile_open);
11390 }
11391
11392 if (ps == PACKET_DISABLE)
11393 {
11394 static int warning_issued = 0;
11395
11396 if (!warning_issued)
11397 {
11398 warning (_("remote target does not support file"
11399 " transfer, attempting to access files"
11400 " from local filesystem."));
11401 warning_issued = 1;
11402 }
11403
11404 return 1;
11405 }
11406 }
11407
11408 return 0;
11409}
11410
a6b151f1
DJ
11411static int
11412remote_fileio_errno_to_host (int errnum)
11413{
11414 switch (errnum)
11415 {
11416 case FILEIO_EPERM:
11417 return EPERM;
11418 case FILEIO_ENOENT:
11419 return ENOENT;
11420 case FILEIO_EINTR:
11421 return EINTR;
11422 case FILEIO_EIO:
11423 return EIO;
11424 case FILEIO_EBADF:
11425 return EBADF;
11426 case FILEIO_EACCES:
11427 return EACCES;
11428 case FILEIO_EFAULT:
11429 return EFAULT;
11430 case FILEIO_EBUSY:
11431 return EBUSY;
11432 case FILEIO_EEXIST:
11433 return EEXIST;
11434 case FILEIO_ENODEV:
11435 return ENODEV;
11436 case FILEIO_ENOTDIR:
11437 return ENOTDIR;
11438 case FILEIO_EISDIR:
11439 return EISDIR;
11440 case FILEIO_EINVAL:
11441 return EINVAL;
11442 case FILEIO_ENFILE:
11443 return ENFILE;
11444 case FILEIO_EMFILE:
11445 return EMFILE;
11446 case FILEIO_EFBIG:
11447 return EFBIG;
11448 case FILEIO_ENOSPC:
11449 return ENOSPC;
11450 case FILEIO_ESPIPE:
11451 return ESPIPE;
11452 case FILEIO_EROFS:
11453 return EROFS;
11454 case FILEIO_ENOSYS:
11455 return ENOSYS;
11456 case FILEIO_ENAMETOOLONG:
11457 return ENAMETOOLONG;
11458 }
11459 return -1;
11460}
11461
11462static char *
11463remote_hostio_error (int errnum)
11464{
11465 int host_error = remote_fileio_errno_to_host (errnum);
11466
11467 if (host_error == -1)
11468 error (_("Unknown remote I/O error %d"), errnum);
11469 else
11470 error (_("Remote I/O error: %s"), safe_strerror (host_error));
11471}
11472
a6b151f1
DJ
11473static void
11474remote_hostio_close_cleanup (void *opaque)
11475{
11476 int fd = *(int *) opaque;
11477 int remote_errno;
11478
df39ea25 11479 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
11480}
11481
11482void
11483remote_file_put (const char *local_file, const char *remote_file, int from_tty)
11484{
11485 struct cleanup *back_to, *close_cleanup;
11486 int retcode, fd, remote_errno, bytes, io_size;
11487 FILE *file;
11488 gdb_byte *buffer;
11489 int bytes_in_buffer;
11490 int saw_eof;
11491 ULONGEST offset;
5d93a237 11492 struct remote_state *rs = get_remote_state ();
a6b151f1 11493
5d93a237 11494 if (!rs->remote_desc)
a6b151f1
DJ
11495 error (_("command can only be used with remote target"));
11496
614c279d 11497 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
11498 if (file == NULL)
11499 perror_with_name (local_file);
7c8a8b04 11500 back_to = make_cleanup_fclose (file);
a6b151f1 11501
07c138c8 11502 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 11503 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1 11504 | FILEIO_O_TRUNC),
4313b8c0 11505 0700, 0, &remote_errno);
a6b151f1
DJ
11506 if (fd == -1)
11507 remote_hostio_error (remote_errno);
11508
11509 /* Send up to this many bytes at once. They won't all fit in the
11510 remote packet limit, so we'll transfer slightly fewer. */
11511 io_size = get_remote_packet_size ();
224c3ddb 11512 buffer = (gdb_byte *) xmalloc (io_size);
a6b151f1
DJ
11513 make_cleanup (xfree, buffer);
11514
11515 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
11516
11517 bytes_in_buffer = 0;
11518 saw_eof = 0;
11519 offset = 0;
11520 while (bytes_in_buffer || !saw_eof)
11521 {
11522 if (!saw_eof)
11523 {
3e43a32a
MS
11524 bytes = fread (buffer + bytes_in_buffer, 1,
11525 io_size - bytes_in_buffer,
a6b151f1
DJ
11526 file);
11527 if (bytes == 0)
11528 {
11529 if (ferror (file))
11530 error (_("Error reading %s."), local_file);
11531 else
11532 {
11533 /* EOF. Unless there is something still in the
11534 buffer from the last iteration, we are done. */
11535 saw_eof = 1;
11536 if (bytes_in_buffer == 0)
11537 break;
11538 }
11539 }
11540 }
11541 else
11542 bytes = 0;
11543
11544 bytes += bytes_in_buffer;
11545 bytes_in_buffer = 0;
11546
0d866f62
TT
11547 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
11548 fd, buffer, bytes,
3e43a32a 11549 offset, &remote_errno);
a6b151f1
DJ
11550
11551 if (retcode < 0)
11552 remote_hostio_error (remote_errno);
11553 else if (retcode == 0)
11554 error (_("Remote write of %d bytes returned 0!"), bytes);
11555 else if (retcode < bytes)
11556 {
11557 /* Short write. Save the rest of the read data for the next
11558 write. */
11559 bytes_in_buffer = bytes - retcode;
11560 memmove (buffer, buffer + retcode, bytes_in_buffer);
11561 }
11562
11563 offset += retcode;
11564 }
11565
11566 discard_cleanups (close_cleanup);
df39ea25 11567 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
11568 remote_hostio_error (remote_errno);
11569
11570 if (from_tty)
11571 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
11572 do_cleanups (back_to);
11573}
11574
11575void
11576remote_file_get (const char *remote_file, const char *local_file, int from_tty)
11577{
11578 struct cleanup *back_to, *close_cleanup;
cea39f65 11579 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
11580 FILE *file;
11581 gdb_byte *buffer;
11582 ULONGEST offset;
5d93a237 11583 struct remote_state *rs = get_remote_state ();
a6b151f1 11584
5d93a237 11585 if (!rs->remote_desc)
a6b151f1
DJ
11586 error (_("command can only be used with remote target"));
11587
07c138c8 11588 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
4313b8c0
GB
11589 remote_file, FILEIO_O_RDONLY, 0, 0,
11590 &remote_errno);
a6b151f1
DJ
11591 if (fd == -1)
11592 remote_hostio_error (remote_errno);
11593
614c279d 11594 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
11595 if (file == NULL)
11596 perror_with_name (local_file);
7c8a8b04 11597 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
11598
11599 /* Send up to this many bytes at once. They won't all fit in the
11600 remote packet limit, so we'll transfer slightly fewer. */
11601 io_size = get_remote_packet_size ();
224c3ddb 11602 buffer = (gdb_byte *) xmalloc (io_size);
a6b151f1
DJ
11603 make_cleanup (xfree, buffer);
11604
11605 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
11606
11607 offset = 0;
11608 while (1)
11609 {
a3be983c
TT
11610 bytes = remote_hostio_pread (find_target_at (process_stratum),
11611 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
11612 if (bytes == 0)
11613 /* Success, but no bytes, means end-of-file. */
11614 break;
11615 if (bytes == -1)
11616 remote_hostio_error (remote_errno);
11617
11618 offset += bytes;
11619
11620 bytes = fwrite (buffer, 1, bytes, file);
11621 if (bytes == 0)
11622 perror_with_name (local_file);
11623 }
11624
11625 discard_cleanups (close_cleanup);
df39ea25 11626 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
11627 remote_hostio_error (remote_errno);
11628
11629 if (from_tty)
11630 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
11631 do_cleanups (back_to);
11632}
11633
11634void
11635remote_file_delete (const char *remote_file, int from_tty)
11636{
11637 int retcode, remote_errno;
5d93a237 11638 struct remote_state *rs = get_remote_state ();
a6b151f1 11639
5d93a237 11640 if (!rs->remote_desc)
a6b151f1
DJ
11641 error (_("command can only be used with remote target"));
11642
dbbca37d 11643 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 11644 NULL, remote_file, &remote_errno);
a6b151f1
DJ
11645 if (retcode == -1)
11646 remote_hostio_error (remote_errno);
11647
11648 if (from_tty)
11649 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
11650}
11651
11652static void
11653remote_put_command (char *args, int from_tty)
11654{
11655 struct cleanup *back_to;
11656 char **argv;
11657
d1a41061
PP
11658 if (args == NULL)
11659 error_no_arg (_("file to put"));
11660
11661 argv = gdb_buildargv (args);
a6b151f1
DJ
11662 back_to = make_cleanup_freeargv (argv);
11663 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
11664 error (_("Invalid parameters to remote put"));
11665
11666 remote_file_put (argv[0], argv[1], from_tty);
11667
11668 do_cleanups (back_to);
11669}
11670
11671static void
11672remote_get_command (char *args, int from_tty)
11673{
11674 struct cleanup *back_to;
11675 char **argv;
11676
d1a41061
PP
11677 if (args == NULL)
11678 error_no_arg (_("file to get"));
11679
11680 argv = gdb_buildargv (args);
a6b151f1
DJ
11681 back_to = make_cleanup_freeargv (argv);
11682 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
11683 error (_("Invalid parameters to remote get"));
11684
11685 remote_file_get (argv[0], argv[1], from_tty);
11686
11687 do_cleanups (back_to);
11688}
11689
11690static void
11691remote_delete_command (char *args, int from_tty)
11692{
11693 struct cleanup *back_to;
11694 char **argv;
11695
d1a41061
PP
11696 if (args == NULL)
11697 error_no_arg (_("file to delete"));
11698
11699 argv = gdb_buildargv (args);
a6b151f1
DJ
11700 back_to = make_cleanup_freeargv (argv);
11701 if (argv[0] == NULL || argv[1] != NULL)
11702 error (_("Invalid parameters to remote delete"));
11703
11704 remote_file_delete (argv[0], from_tty);
11705
11706 do_cleanups (back_to);
11707}
11708
11709static void
11710remote_command (char *args, int from_tty)
11711{
635c7e8a 11712 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
11713}
11714
b2175913 11715static int
19db3e69 11716remote_can_execute_reverse (struct target_ops *self)
b2175913 11717{
4082afcc
PA
11718 if (packet_support (PACKET_bs) == PACKET_ENABLE
11719 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
11720 return 1;
11721 else
11722 return 0;
b2175913
MS
11723}
11724
74531fed 11725static int
2a9a2795 11726remote_supports_non_stop (struct target_ops *self)
74531fed
PA
11727{
11728 return 1;
11729}
11730
03583c20 11731static int
2bfc0540 11732remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
11733{
11734 /* Only supported in extended mode. */
11735 return 0;
11736}
11737
8a305172 11738static int
86ce2668 11739remote_supports_multi_process (struct target_ops *self)
8a305172
PA
11740{
11741 struct remote_state *rs = get_remote_state ();
a744cf53 11742
8020350c 11743 return remote_multi_process_p (rs);
8a305172
PA
11744}
11745
70221824 11746static int
782b2b07
SS
11747remote_supports_cond_tracepoints (void)
11748{
4082afcc 11749 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
11750}
11751
3788aec7 11752static int
efcc2da7 11753remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 11754{
4082afcc 11755 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
11756}
11757
70221824 11758static int
7a697b8d
SS
11759remote_supports_fast_tracepoints (void)
11760{
4082afcc 11761 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
11762}
11763
0fb4aa4b
PA
11764static int
11765remote_supports_static_tracepoints (void)
11766{
4082afcc 11767 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
11768}
11769
1e4d1764
YQ
11770static int
11771remote_supports_install_in_trace (void)
11772{
4082afcc 11773 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
11774}
11775
d248b706 11776static int
7d178d6a 11777remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 11778{
4082afcc
PA
11779 return (packet_support (PACKET_EnableDisableTracepoints_feature)
11780 == PACKET_ENABLE);
d248b706
KY
11781}
11782
3065dfb6 11783static int
6de37a3a 11784remote_supports_string_tracing (struct target_ops *self)
3065dfb6 11785{
4082afcc 11786 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
11787}
11788
d3ce09f5 11789static int
78eff0ec 11790remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 11791{
4082afcc 11792 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
11793}
11794
35b1e5cc 11795static void
ecae04e1 11796remote_trace_init (struct target_ops *self)
35b1e5cc
SS
11797{
11798 putpkt ("QTinit");
11799 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 11800 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11801 error (_("Target does not support this command."));
11802}
11803
11804static void free_actions_list (char **actions_list);
11805static void free_actions_list_cleanup_wrapper (void *);
11806static void
11807free_actions_list_cleanup_wrapper (void *al)
11808{
19ba03f4 11809 free_actions_list ((char **) al);
35b1e5cc
SS
11810}
11811
11812static void
11813free_actions_list (char **actions_list)
11814{
11815 int ndx;
11816
11817 if (actions_list == 0)
11818 return;
11819
11820 for (ndx = 0; actions_list[ndx]; ndx++)
11821 xfree (actions_list[ndx]);
11822
11823 xfree (actions_list);
11824}
11825
409873ef
SS
11826/* Recursive routine to walk through command list including loops, and
11827 download packets for each command. */
11828
11829static void
11830remote_download_command_source (int num, ULONGEST addr,
11831 struct command_line *cmds)
11832{
11833 struct remote_state *rs = get_remote_state ();
11834 struct command_line *cmd;
11835
11836 for (cmd = cmds; cmd; cmd = cmd->next)
11837 {
0df8b418 11838 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11839 strcpy (rs->buf, "QTDPsrc:");
11840 encode_source_string (num, addr, "cmd", cmd->line,
11841 rs->buf + strlen (rs->buf),
11842 rs->buf_size - strlen (rs->buf));
11843 putpkt (rs->buf);
11844 remote_get_noisy_reply (&target_buf, &target_buf_size);
11845 if (strcmp (target_buf, "OK"))
11846 warning (_("Target does not support source download."));
11847
11848 if (cmd->control_type == while_control
11849 || cmd->control_type == while_stepping_control)
11850 {
11851 remote_download_command_source (num, addr, *cmd->body_list);
11852
0df8b418 11853 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11854 strcpy (rs->buf, "QTDPsrc:");
11855 encode_source_string (num, addr, "cmd", "end",
11856 rs->buf + strlen (rs->buf),
11857 rs->buf_size - strlen (rs->buf));
11858 putpkt (rs->buf);
11859 remote_get_noisy_reply (&target_buf, &target_buf_size);
11860 if (strcmp (target_buf, "OK"))
11861 warning (_("Target does not support source download."));
11862 }
11863 }
11864}
11865
35b1e5cc 11866static void
548f7808 11867remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11868{
bba74b36 11869#define BUF_SIZE 2048
e8ba3115 11870
35b1e5cc 11871 CORE_ADDR tpaddr;
409873ef 11872 char addrbuf[40];
bba74b36 11873 char buf[BUF_SIZE];
35b1e5cc
SS
11874 char **tdp_actions;
11875 char **stepping_actions;
11876 int ndx;
11877 struct cleanup *old_chain = NULL;
11878 struct agent_expr *aexpr;
11879 struct cleanup *aexpr_chain = NULL;
11880 char *pkt;
e8ba3115 11881 struct breakpoint *b = loc->owner;
d9b3f62e 11882 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11883
dc673c81 11884 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11885 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11886 tdp_actions);
11887 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11888 stepping_actions);
11889
11890 tpaddr = loc->address;
11891 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11892 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11893 addrbuf, /* address */
11894 (b->enable_state == bp_enabled ? 'E' : 'D'),
11895 t->step_count, t->pass_count);
e8ba3115
YQ
11896 /* Fast tracepoints are mostly handled by the target, but we can
11897 tell the target how big of an instruction block should be moved
11898 around. */
11899 if (b->type == bp_fast_tracepoint)
11900 {
11901 /* Only test for support at download time; we may not know
11902 target capabilities at definition time. */
11903 if (remote_supports_fast_tracepoints ())
35b1e5cc 11904 {
6b940e6a
PL
11905 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
11906 NULL))
bba74b36 11907 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 11908 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 11909 else
e8ba3115
YQ
11910 /* If it passed validation at definition but fails now,
11911 something is very wrong. */
11912 internal_error (__FILE__, __LINE__,
11913 _("Fast tracepoint not "
11914 "valid during download"));
35b1e5cc 11915 }
e8ba3115
YQ
11916 else
11917 /* Fast tracepoints are functionally identical to regular
11918 tracepoints, so don't take lack of support as a reason to
11919 give up on the trace run. */
11920 warning (_("Target does not support fast tracepoints, "
11921 "downloading %d as regular tracepoint"), b->number);
11922 }
11923 else if (b->type == bp_static_tracepoint)
11924 {
11925 /* Only test for support at download time; we may not know
11926 target capabilities at definition time. */
11927 if (remote_supports_static_tracepoints ())
0fb4aa4b 11928 {
e8ba3115 11929 struct static_tracepoint_marker marker;
0fb4aa4b 11930
e8ba3115
YQ
11931 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11932 strcat (buf, ":S");
0fb4aa4b 11933 else
e8ba3115 11934 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11935 }
e8ba3115
YQ
11936 else
11937 /* Fast tracepoints are functionally identical to regular
11938 tracepoints, so don't take lack of support as a reason
11939 to give up on the trace run. */
11940 error (_("Target does not support static tracepoints"));
11941 }
11942 /* If the tracepoint has a conditional, make it into an agent
11943 expression and append to the definition. */
11944 if (loc->cond)
11945 {
11946 /* Only test support at download time, we may not know target
11947 capabilities at definition time. */
11948 if (remote_supports_cond_tracepoints ())
35b1e5cc 11949 {
e8ba3115
YQ
11950 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11951 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11952 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11953 aexpr->len);
e8ba3115
YQ
11954 pkt = buf + strlen (buf);
11955 for (ndx = 0; ndx < aexpr->len; ++ndx)
11956 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11957 *pkt = '\0';
11958 do_cleanups (aexpr_chain);
35b1e5cc 11959 }
e8ba3115
YQ
11960 else
11961 warning (_("Target does not support conditional tracepoints, "
11962 "ignoring tp %d cond"), b->number);
11963 }
35b1e5cc 11964
d9b3f62e 11965 if (b->commands || *default_collect)
e8ba3115
YQ
11966 strcat (buf, "-");
11967 putpkt (buf);
11968 remote_get_noisy_reply (&target_buf, &target_buf_size);
11969 if (strcmp (target_buf, "OK"))
11970 error (_("Target does not support tracepoints."));
35b1e5cc 11971
e8ba3115
YQ
11972 /* do_single_steps (t); */
11973 if (tdp_actions)
11974 {
11975 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11976 {
e8ba3115 11977 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11978 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11979 b->number, addrbuf, /* address */
11980 tdp_actions[ndx],
11981 ((tdp_actions[ndx + 1] || stepping_actions)
11982 ? '-' : 0));
e8ba3115
YQ
11983 putpkt (buf);
11984 remote_get_noisy_reply (&target_buf,
11985 &target_buf_size);
11986 if (strcmp (target_buf, "OK"))
11987 error (_("Error on target while setting tracepoints."));
35b1e5cc 11988 }
e8ba3115
YQ
11989 }
11990 if (stepping_actions)
11991 {
11992 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11993 {
e8ba3115 11994 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11995 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11996 b->number, addrbuf, /* address */
11997 ((ndx == 0) ? "S" : ""),
11998 stepping_actions[ndx],
11999 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
12000 putpkt (buf);
12001 remote_get_noisy_reply (&target_buf,
12002 &target_buf_size);
12003 if (strcmp (target_buf, "OK"))
12004 error (_("Error on target while setting tracepoints."));
35b1e5cc 12005 }
e8ba3115 12006 }
409873ef 12007
4082afcc 12008 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 12009 {
f00aae0f 12010 if (b->location != NULL)
409873ef 12011 {
e8ba3115 12012 strcpy (buf, "QTDPsrc:");
f00aae0f
KS
12013 encode_source_string (b->number, loc->address, "at",
12014 event_location_to_string (b->location),
12015 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115
YQ
12016 putpkt (buf);
12017 remote_get_noisy_reply (&target_buf, &target_buf_size);
12018 if (strcmp (target_buf, "OK"))
12019 warning (_("Target does not support source download."));
409873ef 12020 }
e8ba3115
YQ
12021 if (b->cond_string)
12022 {
12023 strcpy (buf, "QTDPsrc:");
12024 encode_source_string (b->number, loc->address,
12025 "cond", b->cond_string, buf + strlen (buf),
12026 2048 - strlen (buf));
12027 putpkt (buf);
12028 remote_get_noisy_reply (&target_buf, &target_buf_size);
12029 if (strcmp (target_buf, "OK"))
12030 warning (_("Target does not support source download."));
12031 }
12032 remote_download_command_source (b->number, loc->address,
12033 breakpoint_commands (b));
35b1e5cc 12034 }
e8ba3115
YQ
12035
12036 do_cleanups (old_chain);
35b1e5cc
SS
12037}
12038
1e4d1764 12039static int
a52a8357 12040remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 12041{
1e51243a
PA
12042 struct remote_state *rs = get_remote_state ();
12043 struct trace_status *ts;
12044 int status;
12045
12046 /* Don't try to install tracepoints until we've relocated our
12047 symbols, and fetched and merged the target's tracepoint list with
12048 ours. */
12049 if (rs->starting_up)
12050 return 0;
12051
12052 ts = current_trace_status ();
8bd200f1 12053 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
12054
12055 if (status == -1 || !ts->running_known || !ts->running)
12056 return 0;
12057
12058 /* If we are in a tracing experiment, but remote stub doesn't support
12059 installing tracepoint in trace, we have to return. */
12060 if (!remote_supports_install_in_trace ())
12061 return 0;
12062
12063 return 1;
12064}
12065
12066
35b1e5cc 12067static void
559d2b81
TT
12068remote_download_trace_state_variable (struct target_ops *self,
12069 struct trace_state_variable *tsv)
35b1e5cc
SS
12070{
12071 struct remote_state *rs = get_remote_state ();
00bf0b85 12072 char *p;
35b1e5cc 12073
bba74b36
YQ
12074 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
12075 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
12076 tsv->builtin);
00bf0b85
SS
12077 p = rs->buf + strlen (rs->buf);
12078 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
12079 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 12080 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 12081 *p++ = '\0';
35b1e5cc
SS
12082 putpkt (rs->buf);
12083 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
12084 if (*target_buf == '\0')
12085 error (_("Target does not support this command."));
12086 if (strcmp (target_buf, "OK") != 0)
12087 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
12088}
12089
d248b706 12090static void
46670d57
TT
12091remote_enable_tracepoint (struct target_ops *self,
12092 struct bp_location *location)
d248b706
KY
12093{
12094 struct remote_state *rs = get_remote_state ();
12095 char addr_buf[40];
12096
12097 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12098 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
12099 location->owner->number, addr_buf);
d248b706
KY
12100 putpkt (rs->buf);
12101 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
12102 if (*rs->buf == '\0')
12103 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
12104 if (strcmp (rs->buf, "OK") != 0)
12105 error (_("Error on target while enabling tracepoint."));
12106}
12107
12108static void
780b049c
TT
12109remote_disable_tracepoint (struct target_ops *self,
12110 struct bp_location *location)
d248b706
KY
12111{
12112 struct remote_state *rs = get_remote_state ();
12113 char addr_buf[40];
12114
12115 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
12116 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
12117 location->owner->number, addr_buf);
d248b706
KY
12118 putpkt (rs->buf);
12119 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
12120 if (*rs->buf == '\0')
12121 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
12122 if (strcmp (rs->buf, "OK") != 0)
12123 error (_("Error on target while disabling tracepoint."));
12124}
12125
35b1e5cc 12126static void
583f9a86 12127remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
12128{
12129 asection *s;
81b9b86e 12130 bfd *abfd = NULL;
35b1e5cc 12131 bfd_size_type size;
608bcef2 12132 bfd_vma vma;
35b1e5cc 12133 int anysecs = 0;
c2fa21f1 12134 int offset = 0;
35b1e5cc
SS
12135
12136 if (!exec_bfd)
12137 return; /* No information to give. */
12138
12139 strcpy (target_buf, "QTro");
9779ab84 12140 offset = strlen (target_buf);
35b1e5cc
SS
12141 for (s = exec_bfd->sections; s; s = s->next)
12142 {
12143 char tmp1[40], tmp2[40];
c2fa21f1 12144 int sec_length;
35b1e5cc
SS
12145
12146 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 12147 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
12148 (s->flags & SEC_READONLY) == 0)
12149 continue;
12150
12151 anysecs = 1;
81b9b86e 12152 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 12153 size = bfd_get_section_size (s);
608bcef2
HZ
12154 sprintf_vma (tmp1, vma);
12155 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
12156 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
12157 if (offset + sec_length + 1 > target_buf_size)
12158 {
4082afcc 12159 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 12160 warning (_("\
c2fa21f1
HZ
12161Too many sections for read-only sections definition packet."));
12162 break;
12163 }
bba74b36
YQ
12164 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
12165 tmp1, tmp2);
c2fa21f1 12166 offset += sec_length;
35b1e5cc
SS
12167 }
12168 if (anysecs)
12169 {
12170 putpkt (target_buf);
12171 getpkt (&target_buf, &target_buf_size, 0);
12172 }
12173}
12174
12175static void
e2d1aae3 12176remote_trace_start (struct target_ops *self)
35b1e5cc
SS
12177{
12178 putpkt ("QTStart");
12179 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
12180 if (*target_buf == '\0')
12181 error (_("Target does not support this command."));
12182 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
12183 error (_("Bogus reply from target: %s"), target_buf);
12184}
12185
12186static int
8bd200f1 12187remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 12188{
953b98d1 12189 /* Initialize it just to avoid a GCC false warning. */
f652de6f 12190 char *p = NULL;
0df8b418 12191 /* FIXME we need to get register block size some other way. */
00bf0b85 12192 extern int trace_regblock_size;
bd3eecc3
PA
12193 enum packet_result result;
12194
4082afcc 12195 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 12196 return -1;
a744cf53 12197
00bf0b85
SS
12198 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
12199
049dc89b
JK
12200 putpkt ("qTStatus");
12201
492d29ea 12202 TRY
67f41397
JK
12203 {
12204 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
12205 }
492d29ea 12206 CATCH (ex, RETURN_MASK_ERROR)
67f41397 12207 {
598d3636
JK
12208 if (ex.error != TARGET_CLOSE_ERROR)
12209 {
12210 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
12211 return -1;
12212 }
12213 throw_exception (ex);
67f41397 12214 }
492d29ea 12215 END_CATCH
00bf0b85 12216
bd3eecc3
PA
12217 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
12218
00bf0b85 12219 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 12220 if (result == PACKET_UNKNOWN)
00bf0b85 12221 return -1;
35b1e5cc 12222
00bf0b85 12223 /* We're working with a live target. */
f5911ea1 12224 ts->filename = NULL;
00bf0b85 12225
00bf0b85 12226 if (*p++ != 'T')
35b1e5cc
SS
12227 error (_("Bogus trace status reply from target: %s"), target_buf);
12228
84cebc4a
YQ
12229 /* Function 'parse_trace_status' sets default value of each field of
12230 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
12231 parse_trace_status (p, ts);
12232
12233 return ts->running;
35b1e5cc
SS
12234}
12235
70221824 12236static void
db90e85c 12237remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
12238 struct uploaded_tp *utp)
12239{
12240 struct remote_state *rs = get_remote_state ();
f196051f
SS
12241 char *reply;
12242 struct bp_location *loc;
12243 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 12244 size_t size = get_remote_packet_size ();
f196051f
SS
12245
12246 if (tp)
12247 {
12248 tp->base.hit_count = 0;
12249 tp->traceframe_usage = 0;
12250 for (loc = tp->base.loc; loc; loc = loc->next)
12251 {
12252 /* If the tracepoint was never downloaded, don't go asking for
12253 any status. */
12254 if (tp->number_on_target == 0)
12255 continue;
bba74b36
YQ
12256 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
12257 phex_nz (loc->address, 0));
f196051f
SS
12258 putpkt (rs->buf);
12259 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12260 if (reply && *reply)
12261 {
12262 if (*reply == 'V')
12263 parse_tracepoint_status (reply + 1, bp, utp);
12264 }
12265 }
12266 }
12267 else if (utp)
12268 {
12269 utp->hit_count = 0;
12270 utp->traceframe_usage = 0;
bba74b36
YQ
12271 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
12272 phex_nz (utp->addr, 0));
f196051f
SS
12273 putpkt (rs->buf);
12274 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12275 if (reply && *reply)
12276 {
12277 if (*reply == 'V')
12278 parse_tracepoint_status (reply + 1, bp, utp);
12279 }
12280 }
12281}
12282
35b1e5cc 12283static void
74499f1b 12284remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
12285{
12286 putpkt ("QTStop");
12287 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
12288 if (*target_buf == '\0')
12289 error (_("Target does not support this command."));
12290 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
12291 error (_("Bogus reply from target: %s"), target_buf);
12292}
12293
12294static int
bd4c6793
TT
12295remote_trace_find (struct target_ops *self,
12296 enum trace_find_type type, int num,
cc5925ad 12297 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
12298 int *tpp)
12299{
12300 struct remote_state *rs = get_remote_state ();
bba74b36 12301 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
12302 char *p, *reply;
12303 int target_frameno = -1, target_tracept = -1;
12304
e6e4e701
PA
12305 /* Lookups other than by absolute frame number depend on the current
12306 trace selected, so make sure it is correct on the remote end
12307 first. */
12308 if (type != tfind_number)
12309 set_remote_traceframe ();
12310
35b1e5cc
SS
12311 p = rs->buf;
12312 strcpy (p, "QTFrame:");
12313 p = strchr (p, '\0');
12314 switch (type)
12315 {
12316 case tfind_number:
bba74b36 12317 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
12318 break;
12319 case tfind_pc:
bba74b36 12320 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
12321 break;
12322 case tfind_tp:
bba74b36 12323 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
12324 break;
12325 case tfind_range:
bba74b36
YQ
12326 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
12327 phex_nz (addr2, 0));
35b1e5cc
SS
12328 break;
12329 case tfind_outside:
bba74b36
YQ
12330 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
12331 phex_nz (addr2, 0));
35b1e5cc
SS
12332 break;
12333 default:
9b20d036 12334 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
12335 }
12336
12337 putpkt (rs->buf);
2f65bcb7 12338 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
12339 if (*reply == '\0')
12340 error (_("Target does not support this command."));
35b1e5cc
SS
12341
12342 while (reply && *reply)
12343 switch (*reply)
12344 {
12345 case 'F':
f197e0f1
VP
12346 p = ++reply;
12347 target_frameno = (int) strtol (p, &reply, 16);
12348 if (reply == p)
12349 error (_("Unable to parse trace frame number"));
e6e4e701
PA
12350 /* Don't update our remote traceframe number cache on failure
12351 to select a remote traceframe. */
f197e0f1
VP
12352 if (target_frameno == -1)
12353 return -1;
35b1e5cc
SS
12354 break;
12355 case 'T':
f197e0f1
VP
12356 p = ++reply;
12357 target_tracept = (int) strtol (p, &reply, 16);
12358 if (reply == p)
12359 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
12360 break;
12361 case 'O': /* "OK"? */
12362 if (reply[1] == 'K' && reply[2] == '\0')
12363 reply += 2;
12364 else
12365 error (_("Bogus reply from target: %s"), reply);
12366 break;
12367 default:
12368 error (_("Bogus reply from target: %s"), reply);
12369 }
12370 if (tpp)
12371 *tpp = target_tracept;
e6e4e701 12372
262e1174 12373 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
12374 return target_frameno;
12375}
12376
12377static int
4011015b
TT
12378remote_get_trace_state_variable_value (struct target_ops *self,
12379 int tsvnum, LONGEST *val)
35b1e5cc
SS
12380{
12381 struct remote_state *rs = get_remote_state ();
12382 char *reply;
12383 ULONGEST uval;
12384
e6e4e701
PA
12385 set_remote_traceframe ();
12386
bba74b36 12387 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
12388 putpkt (rs->buf);
12389 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12390 if (reply && *reply)
12391 {
12392 if (*reply == 'V')
12393 {
12394 unpack_varlen_hex (reply + 1, &uval);
12395 *val = (LONGEST) uval;
12396 return 1;
12397 }
12398 }
12399 return 0;
12400}
12401
00bf0b85 12402static int
dc3decaf 12403remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
12404{
12405 struct remote_state *rs = get_remote_state ();
12406 char *p, *reply;
12407
12408 p = rs->buf;
12409 strcpy (p, "QTSave:");
12410 p += strlen (p);
12411 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
12412 error (_("Remote file name too long for trace save packet"));
9f1b45b0 12413 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
12414 *p++ = '\0';
12415 putpkt (rs->buf);
ad91cd99 12416 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 12417 if (*reply == '\0')
ad91cd99
PA
12418 error (_("Target does not support this command."));
12419 if (strcmp (reply, "OK") != 0)
12420 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
12421 return 0;
12422}
12423
12424/* This is basically a memory transfer, but needs to be its own packet
12425 because we don't know how the target actually organizes its trace
12426 memory, plus we want to be able to ask for as much as possible, but
12427 not be unhappy if we don't get as much as we ask for. */
12428
12429static LONGEST
88ee6f45
TT
12430remote_get_raw_trace_data (struct target_ops *self,
12431 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
12432{
12433 struct remote_state *rs = get_remote_state ();
12434 char *reply;
12435 char *p;
12436 int rslt;
12437
12438 p = rs->buf;
12439 strcpy (p, "qTBuffer:");
12440 p += strlen (p);
12441 p += hexnumstr (p, offset);
12442 *p++ = ',';
12443 p += hexnumstr (p, len);
12444 *p++ = '\0';
12445
12446 putpkt (rs->buf);
12447 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12448 if (reply && *reply)
12449 {
12450 /* 'l' by itself means we're at the end of the buffer and
12451 there is nothing more to get. */
12452 if (*reply == 'l')
12453 return 0;
12454
12455 /* Convert the reply into binary. Limit the number of bytes to
12456 convert according to our passed-in buffer size, rather than
12457 what was returned in the packet; if the target is
12458 unexpectedly generous and gives us a bigger reply than we
12459 asked for, we don't want to crash. */
12460 rslt = hex2bin (target_buf, buf, len);
12461 return rslt;
12462 }
12463
12464 /* Something went wrong, flag as an error. */
12465 return -1;
12466}
12467
35b1e5cc 12468static void
37b25738 12469remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
12470{
12471 struct remote_state *rs = get_remote_state ();
12472
4082afcc 12473 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 12474 {
ad91cd99
PA
12475 char *reply;
12476
bba74b36 12477 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 12478 putpkt (rs->buf);
ad91cd99
PA
12479 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12480 if (*reply == '\0')
33da3f1c 12481 error (_("Target does not support this command."));
ad91cd99
PA
12482 if (strcmp (reply, "OK") != 0)
12483 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
12484 }
12485 else if (val)
12486 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
12487}
12488
dc146f7c
VP
12489static int
12490remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
12491{
12492 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 12493
fe978cb0
PA
12494 if (info && info->priv)
12495 return info->priv->core;
dc146f7c
VP
12496 return -1;
12497}
12498
4daf5ac0 12499static void
736d5b1f 12500remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
12501{
12502 struct remote_state *rs = get_remote_state ();
ad91cd99 12503 char *reply;
4daf5ac0 12504
bba74b36 12505 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 12506 putpkt (rs->buf);
ad91cd99
PA
12507 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12508 if (*reply == '\0')
4daf5ac0 12509 error (_("Target does not support this command."));
ad91cd99
PA
12510 if (strcmp (reply, "OK") != 0)
12511 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
12512}
12513
b3b9301e 12514static struct traceframe_info *
a893e81f 12515remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
12516{
12517 char *text;
12518
12519 text = target_read_stralloc (&current_target,
12520 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
12521 if (text != NULL)
12522 {
12523 struct traceframe_info *info;
12524 struct cleanup *back_to = make_cleanup (xfree, text);
12525
12526 info = parse_traceframe_info (text);
12527 do_cleanups (back_to);
12528 return info;
12529 }
12530
12531 return NULL;
12532}
12533
405f8e94
SS
12534/* Handle the qTMinFTPILen packet. Returns the minimum length of
12535 instruction on which a fast tracepoint may be placed. Returns -1
12536 if the packet is not supported, and 0 if the minimum instruction
12537 length is unknown. */
12538
12539static int
0e67620a 12540remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
12541{
12542 struct remote_state *rs = get_remote_state ();
12543 char *reply;
12544
e886a173
PA
12545 /* If we're not debugging a process yet, the IPA can't be
12546 loaded. */
12547 if (!target_has_execution)
12548 return 0;
12549
12550 /* Make sure the remote is pointing at the right process. */
12551 set_general_process ();
12552
bba74b36 12553 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
12554 putpkt (rs->buf);
12555 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12556 if (*reply == '\0')
12557 return -1;
12558 else
12559 {
12560 ULONGEST min_insn_len;
12561
12562 unpack_varlen_hex (reply, &min_insn_len);
12563
12564 return (int) min_insn_len;
12565 }
12566}
12567
f6f899bf 12568static void
4da384be 12569remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 12570{
4082afcc 12571 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
12572 {
12573 struct remote_state *rs = get_remote_state ();
12574 char *buf = rs->buf;
12575 char *endbuf = rs->buf + get_remote_packet_size ();
12576 enum packet_result result;
12577
12578 gdb_assert (val >= 0 || val == -1);
12579 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
12580 /* Send -1 as literal "-1" to avoid host size dependency. */
12581 if (val < 0)
12582 {
12583 *buf++ = '-';
12584 buf += hexnumstr (buf, (ULONGEST) -val);
12585 }
12586 else
12587 buf += hexnumstr (buf, (ULONGEST) val);
12588
12589 putpkt (rs->buf);
12590 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
12591 result = packet_ok (rs->buf,
12592 &remote_protocol_packets[PACKET_QTBuffer_size]);
12593
12594 if (result != PACKET_OK)
12595 warning (_("Bogus reply from target: %s"), rs->buf);
12596 }
12597}
12598
f196051f 12599static int
d9e68a2c
TT
12600remote_set_trace_notes (struct target_ops *self,
12601 const char *user, const char *notes,
ca623f82 12602 const char *stop_notes)
f196051f
SS
12603{
12604 struct remote_state *rs = get_remote_state ();
12605 char *reply;
12606 char *buf = rs->buf;
12607 char *endbuf = rs->buf + get_remote_packet_size ();
12608 int nbytes;
12609
12610 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
12611 if (user)
12612 {
12613 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 12614 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
12615 buf += 2 * nbytes;
12616 *buf++ = ';';
12617 }
12618 if (notes)
12619 {
12620 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 12621 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
12622 buf += 2 * nbytes;
12623 *buf++ = ';';
12624 }
12625 if (stop_notes)
12626 {
12627 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 12628 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
12629 buf += 2 * nbytes;
12630 *buf++ = ';';
12631 }
12632 /* Ensure the buffer is terminated. */
12633 *buf = '\0';
12634
12635 putpkt (rs->buf);
12636 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12637 if (*reply == '\0')
12638 return 0;
12639
12640 if (strcmp (reply, "OK") != 0)
12641 error (_("Bogus reply from target: %s"), reply);
12642
12643 return 1;
12644}
12645
d1feda86 12646static int
2c152180 12647remote_use_agent (struct target_ops *self, int use)
d1feda86 12648{
4082afcc 12649 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
12650 {
12651 struct remote_state *rs = get_remote_state ();
12652
12653 /* If the stub supports QAgent. */
bba74b36 12654 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
12655 putpkt (rs->buf);
12656 getpkt (&rs->buf, &rs->buf_size, 0);
12657
12658 if (strcmp (rs->buf, "OK") == 0)
12659 {
12660 use_agent = use;
12661 return 1;
12662 }
12663 }
12664
12665 return 0;
12666}
12667
12668static int
fe38f897 12669remote_can_use_agent (struct target_ops *self)
d1feda86 12670{
4082afcc 12671 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
12672}
12673
9accd112
MM
12674struct btrace_target_info
12675{
12676 /* The ptid of the traced thread. */
12677 ptid_t ptid;
f4abbc16
MM
12678
12679 /* The obtained branch trace configuration. */
12680 struct btrace_config conf;
9accd112
MM
12681};
12682
f4abbc16
MM
12683/* Reset our idea of our target's btrace configuration. */
12684
12685static void
12686remote_btrace_reset (void)
12687{
12688 struct remote_state *rs = get_remote_state ();
12689
12690 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
12691}
12692
9accd112
MM
12693/* Check whether the target supports branch tracing. */
12694
12695static int
043c3577 12696remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 12697{
4082afcc 12698 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 12699 return 0;
4082afcc 12700 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
12701 return 0;
12702
043c3577
MM
12703 switch (format)
12704 {
12705 case BTRACE_FORMAT_NONE:
12706 return 0;
12707
12708 case BTRACE_FORMAT_BTS:
12709 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
12710
12711 case BTRACE_FORMAT_PT:
12712 /* The trace is decoded on the host. Even if our target supports it,
12713 we still need to have libipt to decode the trace. */
12714#if defined (HAVE_LIBIPT)
12715 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
12716#else /* !defined (HAVE_LIBIPT) */
12717 return 0;
12718#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
12719 }
12720
12721 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
12722}
12723
f4abbc16
MM
12724/* Synchronize the configuration with the target. */
12725
12726static void
12727btrace_sync_conf (const struct btrace_config *conf)
12728{
d33501a5
MM
12729 struct packet_config *packet;
12730 struct remote_state *rs;
12731 char *buf, *pos, *endbuf;
12732
12733 rs = get_remote_state ();
12734 buf = rs->buf;
12735 endbuf = buf + get_remote_packet_size ();
12736
12737 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
12738 if (packet_config_support (packet) == PACKET_ENABLE
12739 && conf->bts.size != rs->btrace_config.bts.size)
12740 {
12741 pos = buf;
12742 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
12743 conf->bts.size);
12744
12745 putpkt (buf);
12746 getpkt (&buf, &rs->buf_size, 0);
12747
12748 if (packet_ok (buf, packet) == PACKET_ERROR)
12749 {
12750 if (buf[0] == 'E' && buf[1] == '.')
12751 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
12752 else
12753 error (_("Failed to configure the BTS buffer size."));
12754 }
12755
12756 rs->btrace_config.bts.size = conf->bts.size;
12757 }
b20a6524
MM
12758
12759 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
12760 if (packet_config_support (packet) == PACKET_ENABLE
12761 && conf->pt.size != rs->btrace_config.pt.size)
12762 {
12763 pos = buf;
12764 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
12765 conf->pt.size);
12766
12767 putpkt (buf);
12768 getpkt (&buf, &rs->buf_size, 0);
12769
12770 if (packet_ok (buf, packet) == PACKET_ERROR)
12771 {
12772 if (buf[0] == 'E' && buf[1] == '.')
12773 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
12774 else
12775 error (_("Failed to configure the trace buffer size."));
12776 }
12777
12778 rs->btrace_config.pt.size = conf->pt.size;
12779 }
f4abbc16
MM
12780}
12781
12782/* Read the current thread's btrace configuration from the target and
12783 store it into CONF. */
12784
12785static void
12786btrace_read_config (struct btrace_config *conf)
12787{
12788 char *xml;
12789
12790 xml = target_read_stralloc (&current_target,
b20a6524 12791 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
12792 if (xml != NULL)
12793 {
12794 struct cleanup *cleanup;
12795
12796 cleanup = make_cleanup (xfree, xml);
12797 parse_xml_btrace_conf (conf, xml);
12798 do_cleanups (cleanup);
12799 }
12800}
12801
c0272db5
TW
12802/* Maybe reopen target btrace. */
12803
12804static void
12805remote_btrace_maybe_reopen (void)
12806{
12807 struct remote_state *rs = get_remote_state ();
12808 struct cleanup *cleanup;
12809 struct thread_info *tp;
12810 int btrace_target_pushed = 0;
12811 int warned = 0;
12812
12813 cleanup = make_cleanup_restore_current_thread ();
12814 ALL_NON_EXITED_THREADS (tp)
12815 {
12816 set_general_thread (tp->ptid);
12817
12818 memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
12819 btrace_read_config (&rs->btrace_config);
12820
12821 if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
12822 continue;
12823
12824#if !defined (HAVE_LIBIPT)
12825 if (rs->btrace_config.format == BTRACE_FORMAT_PT)
12826 {
12827 if (!warned)
12828 {
12829 warned = 1;
12830 warning (_("GDB does not support Intel Processor Trace. "
12831 "\"record\" will not work in this session."));
12832 }
12833
12834 continue;
12835 }
12836#endif /* !defined (HAVE_LIBIPT) */
12837
12838 /* Push target, once, but before anything else happens. This way our
12839 changes to the threads will be cleaned up by unpushing the target
12840 in case btrace_read_config () throws. */
12841 if (!btrace_target_pushed)
12842 {
12843 btrace_target_pushed = 1;
12844 record_btrace_push_target ();
12845 printf_filtered (_("Target is recording using %s.\n"),
12846 btrace_format_string (rs->btrace_config.format));
12847 }
12848
12849 tp->btrace.target = XCNEW (struct btrace_target_info);
12850 tp->btrace.target->ptid = tp->ptid;
12851 tp->btrace.target->conf = rs->btrace_config;
12852 }
12853 do_cleanups (cleanup);
12854}
12855
9accd112
MM
12856/* Enable branch tracing. */
12857
12858static struct btrace_target_info *
f4abbc16
MM
12859remote_enable_btrace (struct target_ops *self, ptid_t ptid,
12860 const struct btrace_config *conf)
9accd112
MM
12861{
12862 struct btrace_target_info *tinfo = NULL;
b20a6524 12863 struct packet_config *packet = NULL;
9accd112
MM
12864 struct remote_state *rs = get_remote_state ();
12865 char *buf = rs->buf;
12866 char *endbuf = rs->buf + get_remote_packet_size ();
12867
b20a6524
MM
12868 switch (conf->format)
12869 {
12870 case BTRACE_FORMAT_BTS:
12871 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
12872 break;
12873
12874 case BTRACE_FORMAT_PT:
12875 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
12876 break;
12877 }
12878
12879 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12880 error (_("Target does not support branch tracing."));
12881
f4abbc16
MM
12882 btrace_sync_conf (conf);
12883
9accd112
MM
12884 set_general_thread (ptid);
12885
12886 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12887 putpkt (rs->buf);
12888 getpkt (&rs->buf, &rs->buf_size, 0);
12889
12890 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12891 {
12892 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12893 error (_("Could not enable branch tracing for %s: %s"),
12894 target_pid_to_str (ptid), rs->buf + 2);
12895 else
12896 error (_("Could not enable branch tracing for %s."),
12897 target_pid_to_str (ptid));
12898 }
12899
8d749320 12900 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
12901 tinfo->ptid = ptid;
12902
f4abbc16
MM
12903 /* If we fail to read the configuration, we lose some information, but the
12904 tracing itself is not impacted. */
492d29ea
PA
12905 TRY
12906 {
12907 btrace_read_config (&tinfo->conf);
12908 }
12909 CATCH (err, RETURN_MASK_ERROR)
12910 {
12911 if (err.message != NULL)
12912 warning ("%s", err.message);
12913 }
12914 END_CATCH
f4abbc16 12915
9accd112
MM
12916 return tinfo;
12917}
12918
12919/* Disable branch tracing. */
12920
12921static void
25e95349
TT
12922remote_disable_btrace (struct target_ops *self,
12923 struct btrace_target_info *tinfo)
9accd112
MM
12924{
12925 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12926 struct remote_state *rs = get_remote_state ();
12927 char *buf = rs->buf;
12928 char *endbuf = rs->buf + get_remote_packet_size ();
12929
4082afcc 12930 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12931 error (_("Target does not support branch tracing."));
12932
12933 set_general_thread (tinfo->ptid);
12934
12935 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12936 putpkt (rs->buf);
12937 getpkt (&rs->buf, &rs->buf_size, 0);
12938
12939 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12940 {
12941 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12942 error (_("Could not disable branch tracing for %s: %s"),
12943 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12944 else
12945 error (_("Could not disable branch tracing for %s."),
12946 target_pid_to_str (tinfo->ptid));
12947 }
12948
12949 xfree (tinfo);
12950}
12951
12952/* Teardown branch tracing. */
12953
12954static void
1777056d
TT
12955remote_teardown_btrace (struct target_ops *self,
12956 struct btrace_target_info *tinfo)
9accd112
MM
12957{
12958 /* We must not talk to the target during teardown. */
12959 xfree (tinfo);
12960}
12961
12962/* Read the branch trace. */
12963
969c39fb 12964static enum btrace_error
39c49f83 12965remote_read_btrace (struct target_ops *self,
734b0e4b 12966 struct btrace_data *btrace,
969c39fb 12967 struct btrace_target_info *tinfo,
9accd112
MM
12968 enum btrace_read_type type)
12969{
12970 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
969c39fb 12971 struct cleanup *cleanup;
9accd112
MM
12972 const char *annex;
12973 char *xml;
12974
4082afcc 12975 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12976 error (_("Target does not support branch tracing."));
12977
12978#if !defined(HAVE_LIBEXPAT)
12979 error (_("Cannot process branch tracing result. XML parsing not supported."));
12980#endif
12981
12982 switch (type)
12983 {
864089d2 12984 case BTRACE_READ_ALL:
9accd112
MM
12985 annex = "all";
12986 break;
864089d2 12987 case BTRACE_READ_NEW:
9accd112
MM
12988 annex = "new";
12989 break;
969c39fb
MM
12990 case BTRACE_READ_DELTA:
12991 annex = "delta";
12992 break;
9accd112
MM
12993 default:
12994 internal_error (__FILE__, __LINE__,
12995 _("Bad branch tracing read type: %u."),
12996 (unsigned int) type);
12997 }
12998
12999 xml = target_read_stralloc (&current_target,
b20a6524 13000 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
13001 if (xml == NULL)
13002 return BTRACE_ERR_UNKNOWN;
9accd112 13003
969c39fb 13004 cleanup = make_cleanup (xfree, xml);
734b0e4b 13005 parse_xml_btrace (btrace, xml);
969c39fb 13006 do_cleanups (cleanup);
9accd112 13007
969c39fb 13008 return BTRACE_ERR_NONE;
9accd112
MM
13009}
13010
f4abbc16
MM
13011static const struct btrace_config *
13012remote_btrace_conf (struct target_ops *self,
13013 const struct btrace_target_info *tinfo)
13014{
13015 return &tinfo->conf;
13016}
13017
ced63ec0 13018static int
5436ff03 13019remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 13020{
4082afcc
PA
13021 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
13022 == PACKET_ENABLE);
ced63ec0
GB
13023}
13024
9dd130a0
TT
13025/* Implementation of to_load. */
13026
13027static void
9cbe5fff 13028remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
13029{
13030 generic_load (name, from_tty);
13031}
13032
c78fa86a
GB
13033/* Accepts an integer PID; returns a string representing a file that
13034 can be opened on the remote side to get the symbols for the child
13035 process. Returns NULL if the operation is not supported. */
13036
13037static char *
13038remote_pid_to_exec_file (struct target_ops *self, int pid)
13039{
13040 static char *filename = NULL;
835205d0
GB
13041 struct inferior *inf;
13042 char *annex = NULL;
c78fa86a
GB
13043
13044 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
13045 return NULL;
13046
13047 if (filename != NULL)
13048 xfree (filename);
13049
835205d0
GB
13050 inf = find_inferior_pid (pid);
13051 if (inf == NULL)
13052 internal_error (__FILE__, __LINE__,
13053 _("not currently attached to process %d"), pid);
13054
13055 if (!inf->fake_pid_p)
13056 {
13057 const int annex_size = 9;
13058
224c3ddb 13059 annex = (char *) alloca (annex_size);
835205d0
GB
13060 xsnprintf (annex, annex_size, "%x", pid);
13061 }
13062
c78fa86a
GB
13063 filename = target_read_stralloc (&current_target,
13064 TARGET_OBJECT_EXEC_FILE, annex);
13065
13066 return filename;
13067}
13068
750ce8d1
YQ
13069/* Implement the to_can_do_single_step target_ops method. */
13070
13071static int
13072remote_can_do_single_step (struct target_ops *ops)
13073{
13074 /* We can only tell whether target supports single step or not by
13075 supported s and S vCont actions if the stub supports vContSupported
13076 feature. If the stub doesn't support vContSupported feature,
13077 we have conservatively to think target doesn't supports single
13078 step. */
13079 if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
13080 {
13081 struct remote_state *rs = get_remote_state ();
13082
13083 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
13084 remote_vcont_probe (rs);
13085
13086 return rs->supports_vCont.s && rs->supports_vCont.S;
13087 }
13088 else
13089 return 0;
13090}
13091
3a00c802
PA
13092/* Implementation of the to_execution_direction method for the remote
13093 target. */
13094
13095static enum exec_direction_kind
13096remote_execution_direction (struct target_ops *self)
13097{
13098 struct remote_state *rs = get_remote_state ();
13099
13100 return rs->last_resume_exec_dir;
13101}
13102
c906108c 13103static void
fba45db2 13104init_remote_ops (void)
c906108c 13105{
c5aa993b 13106 remote_ops.to_shortname = "remote";
c906108c 13107 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 13108 remote_ops.to_doc =
c906108c 13109 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
13110Specify the serial device it is connected to\n\
13111(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
13112 remote_ops.to_open = remote_open;
13113 remote_ops.to_close = remote_close;
c906108c 13114 remote_ops.to_detach = remote_detach;
6ad8ae5c 13115 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 13116 remote_ops.to_resume = remote_resume;
c906108c
SS
13117 remote_ops.to_wait = remote_wait;
13118 remote_ops.to_fetch_registers = remote_fetch_registers;
13119 remote_ops.to_store_registers = remote_store_registers;
13120 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 13121 remote_ops.to_files_info = remote_files_info;
c906108c
SS
13122 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
13123 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
13124 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
13125 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
13126 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
13127 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
13128 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
13129 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
13130 remote_ops.to_watchpoint_addr_within_range =
13131 remote_watchpoint_addr_within_range;
3c3bea1c
GS
13132 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
13133 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
13134 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
13135 remote_ops.to_region_ok_for_hw_watchpoint
13136 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
13137 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
13138 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 13139 remote_ops.to_kill = remote_kill;
9dd130a0 13140 remote_ops.to_load = remote_load;
c906108c 13141 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 13142 remote_ops.to_pass_signals = remote_pass_signals;
82075af2 13143 remote_ops.to_set_syscall_catchpoint = remote_set_syscall_catchpoint;
9b224c5e 13144 remote_ops.to_program_signals = remote_program_signals;
c906108c 13145 remote_ops.to_thread_alive = remote_thread_alive;
79efa585 13146 remote_ops.to_thread_name = remote_thread_name;
e8032dde 13147 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 13148 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 13149 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 13150 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 13151 remote_ops.to_stop = remote_stop;
bfedc46a 13152 remote_ops.to_interrupt = remote_interrupt;
93692b58 13153 remote_ops.to_pass_ctrlc = remote_pass_ctrlc;
4b8a223f 13154 remote_ops.to_xfer_partial = remote_xfer_partial;
09c98b44 13155 remote_ops.to_get_memory_xfer_limit = remote_get_memory_xfer_limit;
96baa820 13156 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 13157 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 13158 remote_ops.to_log_command = serial_log_command;
38691318 13159 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 13160 remote_ops.to_stratum = process_stratum;
c35b1492
PA
13161 remote_ops.to_has_all_memory = default_child_has_all_memory;
13162 remote_ops.to_has_memory = default_child_has_memory;
13163 remote_ops.to_has_stack = default_child_has_stack;
13164 remote_ops.to_has_registers = default_child_has_registers;
13165 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 13166 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 13167 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 13168 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 13169 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
13170 remote_ops.to_flash_erase = remote_flash_erase;
13171 remote_ops.to_flash_done = remote_flash_done;
29709017 13172 remote_ops.to_read_description = remote_read_description;
08388c79 13173 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
13174 remote_ops.to_can_async_p = remote_can_async_p;
13175 remote_ops.to_is_async_p = remote_is_async_p;
13176 remote_ops.to_async = remote_async;
65706a29 13177 remote_ops.to_thread_events = remote_thread_events;
750ce8d1 13178 remote_ops.to_can_do_single_step = remote_can_do_single_step;
75c99385
PA
13179 remote_ops.to_terminal_inferior = remote_terminal_inferior;
13180 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 13181 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 13182 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
13183 remote_ops.to_supports_disable_randomization
13184 = remote_supports_disable_randomization;
4bd7dc42 13185 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
13186 remote_ops.to_fileio_open = remote_hostio_open;
13187 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
13188 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 13189 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
13190 remote_ops.to_fileio_close = remote_hostio_close;
13191 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 13192 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 13193 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 13194 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 13195 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 13196 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
13197 remote_ops.to_trace_init = remote_trace_init;
13198 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 13199 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
13200 remote_ops.to_download_trace_state_variable
13201 = remote_download_trace_state_variable;
d248b706
KY
13202 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
13203 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
13204 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
13205 remote_ops.to_trace_start = remote_trace_start;
13206 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 13207 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
13208 remote_ops.to_trace_stop = remote_trace_stop;
13209 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
13210 remote_ops.to_get_trace_state_variable_value
13211 = remote_get_trace_state_variable_value;
00bf0b85
SS
13212 remote_ops.to_save_trace_data = remote_save_trace_data;
13213 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
13214 remote_ops.to_upload_trace_state_variables
13215 = remote_upload_trace_state_variables;
00bf0b85 13216 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 13217 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 13218 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 13219 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 13220 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 13221 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 13222 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 13223 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 13224 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 13225 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
13226 remote_ops.to_static_tracepoint_marker_at
13227 = remote_static_tracepoint_marker_at;
13228 remote_ops.to_static_tracepoint_markers_by_strid
13229 = remote_static_tracepoint_markers_by_strid;
b3b9301e 13230 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
13231 remote_ops.to_use_agent = remote_use_agent;
13232 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
13233 remote_ops.to_supports_btrace = remote_supports_btrace;
13234 remote_ops.to_enable_btrace = remote_enable_btrace;
13235 remote_ops.to_disable_btrace = remote_disable_btrace;
13236 remote_ops.to_teardown_btrace = remote_teardown_btrace;
13237 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 13238 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
13239 remote_ops.to_augmented_libraries_svr4_read =
13240 remote_augmented_libraries_svr4_read;
8020350c
DB
13241 remote_ops.to_follow_fork = remote_follow_fork;
13242 remote_ops.to_follow_exec = remote_follow_exec;
13243 remote_ops.to_insert_fork_catchpoint = remote_insert_fork_catchpoint;
13244 remote_ops.to_remove_fork_catchpoint = remote_remove_fork_catchpoint;
13245 remote_ops.to_insert_vfork_catchpoint = remote_insert_vfork_catchpoint;
13246 remote_ops.to_remove_vfork_catchpoint = remote_remove_vfork_catchpoint;
13247 remote_ops.to_insert_exec_catchpoint = remote_insert_exec_catchpoint;
13248 remote_ops.to_remove_exec_catchpoint = remote_remove_exec_catchpoint;
3a00c802 13249 remote_ops.to_execution_direction = remote_execution_direction;
c906108c
SS
13250}
13251
13252/* Set up the extended remote vector by making a copy of the standard
13253 remote vector and adding to it. */
13254
13255static void
fba45db2 13256init_extended_remote_ops (void)
c906108c
SS
13257{
13258 extended_remote_ops = remote_ops;
13259
0f71a2f6 13260 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 13261 extended_remote_ops.to_longname =
c906108c 13262 "Extended remote serial target in gdb-specific protocol";
c5aa993b 13263 extended_remote_ops.to_doc =
c906108c 13264 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
13265Specify the serial device it is connected to (e.g. /dev/ttya).";
13266 extended_remote_ops.to_open = extended_remote_open;
c906108c 13267 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
2d717e4f
DJ
13268 extended_remote_ops.to_detach = extended_remote_detach;
13269 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 13270 extended_remote_ops.to_post_attach = extended_remote_post_attach;
03583c20
UW
13271 extended_remote_ops.to_supports_disable_randomization
13272 = extended_remote_supports_disable_randomization;
0f71a2f6
JM
13273}
13274
6426a772 13275static int
6a109b6b 13276remote_can_async_p (struct target_ops *ops)
6426a772 13277{
5d93a237
TT
13278 struct remote_state *rs = get_remote_state ();
13279
c6ebd6cf 13280 if (!target_async_permitted)
75c99385
PA
13281 /* We only enable async when the user specifically asks for it. */
13282 return 0;
13283
23860348 13284 /* We're async whenever the serial device is. */
5d93a237 13285 return serial_can_async_p (rs->remote_desc);
6426a772
JM
13286}
13287
13288static int
6a109b6b 13289remote_is_async_p (struct target_ops *ops)
6426a772 13290{
5d93a237
TT
13291 struct remote_state *rs = get_remote_state ();
13292
c6ebd6cf 13293 if (!target_async_permitted)
75c99385
PA
13294 /* We only enable async when the user specifically asks for it. */
13295 return 0;
13296
23860348 13297 /* We're async whenever the serial device is. */
5d93a237 13298 return serial_is_async_p (rs->remote_desc);
6426a772
JM
13299}
13300
2acceee2
JM
13301/* Pass the SERIAL event on and up to the client. One day this code
13302 will be able to delay notifying the client of an event until the
23860348 13303 point where an entire packet has been received. */
2acceee2 13304
2acceee2
JM
13305static serial_event_ftype remote_async_serial_handler;
13306
6426a772 13307static void
819cc324 13308remote_async_serial_handler (struct serial *scb, void *context)
6426a772 13309{
2acceee2
JM
13310 /* Don't propogate error information up to the client. Instead let
13311 the client find out about the error by querying the target. */
6a3753b3 13312 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
13313}
13314
74531fed
PA
13315static void
13316remote_async_inferior_event_handler (gdb_client_data data)
13317{
13318 inferior_event_handler (INF_REG_EVENT, NULL);
13319}
13320
2acceee2 13321static void
6a3753b3 13322remote_async (struct target_ops *ops, int enable)
2acceee2 13323{
5d93a237
TT
13324 struct remote_state *rs = get_remote_state ();
13325
6a3753b3 13326 if (enable)
2acceee2 13327 {
88b496c3 13328 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
13329
13330 /* If there are pending events in the stop reply queue tell the
13331 event loop to process them. */
13332 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
13333 mark_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13334 /* For simplicity, below we clear the pending events token
13335 without remembering whether it is marked, so here we always
13336 mark it. If there's actually no pending notification to
13337 process, this ends up being a no-op (other than a spurious
13338 event-loop wakeup). */
13339 if (target_is_non_stop_p ())
13340 mark_async_event_handler (rs->notif_state->get_pending_events_token);
2acceee2
JM
13341 }
13342 else
b7d2e916
PA
13343 {
13344 serial_async (rs->remote_desc, NULL, NULL);
6efcd9a8
PA
13345 /* If the core is disabling async, it doesn't want to be
13346 disturbed with target events. Clear all async event sources
13347 too. */
b7d2e916 13348 clear_async_event_handler (remote_async_inferior_event_token);
6efcd9a8
PA
13349 if (target_is_non_stop_p ())
13350 clear_async_event_handler (rs->notif_state->get_pending_events_token);
b7d2e916 13351 }
6426a772
JM
13352}
13353
65706a29
PA
13354/* Implementation of the to_thread_events method. */
13355
13356static void
13357remote_thread_events (struct target_ops *ops, int enable)
13358{
13359 struct remote_state *rs = get_remote_state ();
13360 size_t size = get_remote_packet_size ();
65706a29
PA
13361
13362 if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
13363 return;
13364
13365 xsnprintf (rs->buf, size, "QThreadEvents:%x", enable ? 1 : 0);
13366 putpkt (rs->buf);
13367 getpkt (&rs->buf, &rs->buf_size, 0);
13368
13369 switch (packet_ok (rs->buf,
13370 &remote_protocol_packets[PACKET_QThreadEvents]))
13371 {
13372 case PACKET_OK:
13373 if (strcmp (rs->buf, "OK") != 0)
13374 error (_("Remote refused setting thread events: %s"), rs->buf);
13375 break;
13376 case PACKET_ERROR:
13377 warning (_("Remote failure reply: %s"), rs->buf);
13378 break;
13379 case PACKET_UNKNOWN:
13380 break;
13381 }
13382}
13383
5a2468f5 13384static void
c2d11a7d 13385set_remote_cmd (char *args, int from_tty)
5a2468f5 13386{
635c7e8a 13387 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
13388}
13389
d471ea57
AC
13390static void
13391show_remote_cmd (char *args, int from_tty)
13392{
37a105a1 13393 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 13394 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
13395 struct cleanup *showlist_chain;
13396 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 13397 struct ui_out *uiout = current_uiout;
37a105a1
DJ
13398
13399 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
13400 for (; list != NULL; list = list->next)
13401 if (strcmp (list->name, "Z-packet") == 0)
13402 continue;
427c3a89
DJ
13403 else if (list->type == not_set_cmd)
13404 /* Alias commands are exactly like the original, except they
13405 don't have the normal type. */
13406 continue;
13407 else
37a105a1
DJ
13408 {
13409 struct cleanup *option_chain
13410 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 13411
37a105a1
DJ
13412 ui_out_field_string (uiout, "name", list->name);
13413 ui_out_text (uiout, ": ");
427c3a89 13414 if (list->type == show_cmd)
f5c4fcd9 13415 do_show_command (NULL, from_tty, list);
427c3a89
DJ
13416 else
13417 cmd_func (list, NULL, from_tty);
37a105a1
DJ
13418 /* Close the tuple. */
13419 do_cleanups (option_chain);
13420 }
427c3a89
DJ
13421
13422 /* Close the tuple. */
13423 do_cleanups (showlist_chain);
d471ea57 13424}
5a2468f5 13425
0f71a2f6 13426
23860348 13427/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
13428static void
13429remote_new_objfile (struct objfile *objfile)
13430{
5d93a237
TT
13431 struct remote_state *rs = get_remote_state ();
13432
13433 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 13434 remote_check_symbols ();
dc8acb97
MS
13435}
13436
00bf0b85
SS
13437/* Pull all the tracepoints defined on the target and create local
13438 data structures representing them. We don't want to create real
13439 tracepoints yet, we don't want to mess up the user's existing
13440 collection. */
13441
13442static int
ab6617cc 13443remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 13444{
00bf0b85
SS
13445 struct remote_state *rs = get_remote_state ();
13446 char *p;
d5551862 13447
00bf0b85
SS
13448 /* Ask for a first packet of tracepoint definition. */
13449 putpkt ("qTfP");
13450 getpkt (&rs->buf, &rs->buf_size, 0);
13451 p = rs->buf;
13452 while (*p && *p != 'l')
d5551862 13453 {
00bf0b85
SS
13454 parse_tracepoint_definition (p, utpp);
13455 /* Ask for another packet of tracepoint definition. */
13456 putpkt ("qTsP");
13457 getpkt (&rs->buf, &rs->buf_size, 0);
13458 p = rs->buf;
d5551862 13459 }
00bf0b85 13460 return 0;
d5551862
SS
13461}
13462
00bf0b85 13463static int
181e3713
TT
13464remote_upload_trace_state_variables (struct target_ops *self,
13465 struct uploaded_tsv **utsvp)
d5551862 13466{
00bf0b85 13467 struct remote_state *rs = get_remote_state ();
d5551862 13468 char *p;
d5551862 13469
00bf0b85
SS
13470 /* Ask for a first packet of variable definition. */
13471 putpkt ("qTfV");
d5551862
SS
13472 getpkt (&rs->buf, &rs->buf_size, 0);
13473 p = rs->buf;
00bf0b85 13474 while (*p && *p != 'l')
d5551862 13475 {
00bf0b85
SS
13476 parse_tsv_definition (p, utsvp);
13477 /* Ask for another packet of variable definition. */
13478 putpkt ("qTsV");
d5551862
SS
13479 getpkt (&rs->buf, &rs->buf_size, 0);
13480 p = rs->buf;
13481 }
00bf0b85 13482 return 0;
d5551862
SS
13483}
13484
c1e36e3e
PA
13485/* The "set/show range-stepping" show hook. */
13486
13487static void
13488show_range_stepping (struct ui_file *file, int from_tty,
13489 struct cmd_list_element *c,
13490 const char *value)
13491{
13492 fprintf_filtered (file,
13493 _("Debugger's willingness to use range stepping "
13494 "is %s.\n"), value);
13495}
13496
13497/* The "set/show range-stepping" set hook. */
13498
13499static void
13500set_range_stepping (char *ignore_args, int from_tty,
13501 struct cmd_list_element *c)
13502{
5d93a237
TT
13503 struct remote_state *rs = get_remote_state ();
13504
c1e36e3e
PA
13505 /* Whene enabling, check whether range stepping is actually
13506 supported by the target, and warn if not. */
13507 if (use_range_stepping)
13508 {
5d93a237 13509 if (rs->remote_desc != NULL)
c1e36e3e 13510 {
4082afcc 13511 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
13512 remote_vcont_probe (rs);
13513
4082afcc 13514 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
13515 && rs->supports_vCont.r)
13516 return;
13517 }
13518
13519 warning (_("Range stepping is not supported by the current target"));
13520 }
13521}
13522
c906108c 13523void
fba45db2 13524_initialize_remote (void)
c906108c 13525{
9a7071a8 13526 struct cmd_list_element *cmd;
6f937416 13527 const char *cmd_name;
ea9c271d 13528
0f71a2f6 13529 /* architecture specific data */
2bc416ba 13530 remote_gdbarch_data_handle =
23860348 13531 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
13532 remote_g_packet_data_handle =
13533 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 13534
94585166
DB
13535 remote_pspace_data
13536 = register_program_space_data_with_cleanup (NULL,
13537 remote_pspace_data_cleanup);
13538
ea9c271d
DJ
13539 /* Initialize the per-target state. At the moment there is only one
13540 of these, not one per target. Only one target is active at a
cf792862
TT
13541 time. */
13542 remote_state = new_remote_state ();
ea9c271d 13543
c906108c
SS
13544 init_remote_ops ();
13545 add_target (&remote_ops);
13546
13547 init_extended_remote_ops ();
13548 add_target (&extended_remote_ops);
cce74817 13549
dc8acb97 13550 /* Hook into new objfile notification. */
06d3b283 13551 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
13552 /* We're no longer interested in notification events of an inferior
13553 when it exits. */
13554 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 13555
c906108c
SS
13556#if 0
13557 init_remote_threadtests ();
13558#endif
13559
722247f1 13560 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 13561 /* set/show remote ... */
d471ea57 13562
1bedd215 13563 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
13564Remote protocol specific variables\n\
13565Configure various remote-protocol specific variables such as\n\
1bedd215 13566the packets being used"),
cff3e48b 13567 &remote_set_cmdlist, "set remote ",
23860348 13568 0 /* allow-unknown */, &setlist);
1bedd215 13569 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
13570Remote protocol specific variables\n\
13571Configure various remote-protocol specific variables such as\n\
1bedd215 13572the packets being used"),
cff3e48b 13573 &remote_show_cmdlist, "show remote ",
23860348 13574 0 /* allow-unknown */, &showlist);
5a2468f5 13575
1a966eab
AC
13576 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
13577Compare section data on target to the exec file.\n\
95cf3b38
DT
13578Argument is a single section name (default: all loaded sections).\n\
13579To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
13580 &cmdlist);
13581
1a966eab
AC
13582 add_cmd ("packet", class_maintenance, packet_command, _("\
13583Send an arbitrary packet to a remote target.\n\
c906108c
SS
13584 maintenance packet TEXT\n\
13585If GDB is talking to an inferior via the GDB serial protocol, then\n\
13586this command sends the string TEXT to the inferior, and displays the\n\
13587response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 13588terminating `#' character and checksum."),
c906108c
SS
13589 &maintenancelist);
13590
7915a72c
AC
13591 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
13592Set whether to send break if interrupted."), _("\
13593Show whether to send break if interrupted."), _("\
13594If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 13595 set_remotebreak, show_remotebreak,
e707bbc2 13596 &setlist, &showlist);
9a7071a8
JB
13597 cmd_name = "remotebreak";
13598 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
13599 deprecate_cmd (cmd, "set remote interrupt-sequence");
13600 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
13601 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
13602 deprecate_cmd (cmd, "show remote interrupt-sequence");
13603
13604 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
13605 interrupt_sequence_modes, &interrupt_sequence_mode,
13606 _("\
9a7071a8
JB
13607Set interrupt sequence to remote target."), _("\
13608Show interrupt sequence to remote target."), _("\
13609Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
13610 NULL, show_interrupt_sequence,
13611 &remote_set_cmdlist,
13612 &remote_show_cmdlist);
13613
13614 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
13615 &interrupt_on_connect, _("\
13616Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
13617Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
13618If set, interrupt sequence is sent to remote target."),
13619 NULL, NULL,
13620 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 13621
23860348 13622 /* Install commands for configuring memory read/write packets. */
11cf8741 13623
1a966eab
AC
13624 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
13625Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 13626 &setlist);
1a966eab
AC
13627 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
13628Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
13629 &showlist);
13630 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
13631 set_memory_write_packet_size, _("\
13632Set the maximum number of bytes per memory-write packet.\n\
13633Specify the number of bytes in a packet or 0 (zero) for the\n\
13634default packet size. The actual limit is further reduced\n\
13635dependent on the target. Specify ``fixed'' to disable the\n\
13636further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
13637 &remote_set_cmdlist);
13638 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
13639 set_memory_read_packet_size, _("\
13640Set the maximum number of bytes per memory-read packet.\n\
13641Specify the number of bytes in a packet or 0 (zero) for the\n\
13642default packet size. The actual limit is further reduced\n\
13643dependent on the target. Specify ``fixed'' to disable the\n\
13644further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
13645 &remote_set_cmdlist);
13646 add_cmd ("memory-write-packet-size", no_class,
13647 show_memory_write_packet_size,
1a966eab 13648 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
13649 &remote_show_cmdlist);
13650 add_cmd ("memory-read-packet-size", no_class,
13651 show_memory_read_packet_size,
1a966eab 13652 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 13653 &remote_show_cmdlist);
c906108c 13654
b3f42336 13655 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
13656 &remote_hw_watchpoint_limit, _("\
13657Set the maximum number of target hardware watchpoints."), _("\
13658Show the maximum number of target hardware watchpoints."), _("\
13659Specify a negative limit for unlimited."),
3e43a32a
MS
13660 NULL, NULL, /* FIXME: i18n: The maximum
13661 number of target hardware
13662 watchpoints is %s. */
b3f42336 13663 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
13664 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
13665 &remote_hw_watchpoint_length_limit, _("\
13666Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
13667Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
13668Specify a negative limit for unlimited."),
13669 NULL, NULL, /* FIXME: i18n: The maximum
13670 length (in bytes) of a target
13671 hardware watchpoint is %s. */
13672 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 13673 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
13674 &remote_hw_breakpoint_limit, _("\
13675Set the maximum number of target hardware breakpoints."), _("\
13676Show the maximum number of target hardware breakpoints."), _("\
13677Specify a negative limit for unlimited."),
3e43a32a
MS
13678 NULL, NULL, /* FIXME: i18n: The maximum
13679 number of target hardware
13680 breakpoints is %s. */
b3f42336 13681 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 13682
1b493192
PA
13683 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
13684 &remote_address_size, _("\
4d28ad1e
AC
13685Set the maximum size of the address (in bits) in a memory packet."), _("\
13686Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
13687 NULL,
13688 NULL, /* FIXME: i18n: */
13689 &setlist, &showlist);
c906108c 13690
ca4f7f8b
PA
13691 init_all_packet_configs ();
13692
444abaca 13693 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 13694 "X", "binary-download", 1);
0f71a2f6 13695
444abaca 13696 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 13697 "vCont", "verbose-resume", 0);
506fb367 13698
89be2091
DJ
13699 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
13700 "QPassSignals", "pass-signals", 0);
13701
82075af2
JS
13702 add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
13703 "QCatchSyscalls", "catch-syscalls", 0);
13704
9b224c5e
PA
13705 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
13706 "QProgramSignals", "program-signals", 0);
13707
444abaca 13708 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 13709 "qSymbol", "symbol-lookup", 0);
dc8acb97 13710
444abaca 13711 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 13712 "P", "set-register", 1);
d471ea57 13713
444abaca 13714 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 13715 "p", "fetch-register", 1);
b96ec7ac 13716
444abaca 13717 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 13718 "Z0", "software-breakpoint", 0);
d471ea57 13719
444abaca 13720 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 13721 "Z1", "hardware-breakpoint", 0);
d471ea57 13722
444abaca 13723 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 13724 "Z2", "write-watchpoint", 0);
d471ea57 13725
444abaca 13726 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 13727 "Z3", "read-watchpoint", 0);
d471ea57 13728
444abaca 13729 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 13730 "Z4", "access-watchpoint", 0);
d471ea57 13731
0876f84a
DJ
13732 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
13733 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 13734
c78fa86a
GB
13735 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
13736 "qXfer:exec-file:read", "pid-to-exec-file", 0);
13737
23181151
DJ
13738 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
13739 "qXfer:features:read", "target-features", 0);
13740
cfa9d6d9
DJ
13741 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
13742 "qXfer:libraries:read", "library-info", 0);
13743
2268b414
JK
13744 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
13745 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
13746
fd79ecee
DJ
13747 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
13748 "qXfer:memory-map:read", "memory-map", 0);
13749
0e7f50da
UW
13750 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
13751 "qXfer:spu:read", "read-spu-object", 0);
13752
13753 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
13754 "qXfer:spu:write", "write-spu-object", 0);
13755
07e059b5
VP
13756 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
13757 "qXfer:osdata:read", "osdata", 0);
13758
dc146f7c
VP
13759 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
13760 "qXfer:threads:read", "threads", 0);
13761
4aa995e1
PA
13762 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
13763 "qXfer:siginfo:read", "read-siginfo-object", 0);
13764
13765 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
13766 "qXfer:siginfo:write", "write-siginfo-object", 0);
13767
b3b9301e
PA
13768 add_packet_config_cmd
13769 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 13770 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 13771
169081d0
TG
13772 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
13773 "qXfer:uib:read", "unwind-info-block", 0);
13774
444abaca 13775 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 13776 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
13777 0);
13778
711e434b
PM
13779 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
13780 "qGetTIBAddr", "get-thread-information-block-address",
13781 0);
13782
40ab02ce
MS
13783 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
13784 "bc", "reverse-continue", 0);
13785
13786 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
13787 "bs", "reverse-step", 0);
13788
be2a5f71
DJ
13789 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
13790 "qSupported", "supported-packets", 0);
13791
08388c79
DE
13792 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
13793 "qSearch:memory", "search-memory", 0);
13794
bd3eecc3
PA
13795 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
13796 "qTStatus", "trace-status", 0);
13797
15a201c8
GB
13798 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
13799 "vFile:setfs", "hostio-setfs", 0);
13800
a6b151f1
DJ
13801 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
13802 "vFile:open", "hostio-open", 0);
13803
13804 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
13805 "vFile:pread", "hostio-pread", 0);
13806
13807 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
13808 "vFile:pwrite", "hostio-pwrite", 0);
13809
13810 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
13811 "vFile:close", "hostio-close", 0);
13812
13813 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
13814 "vFile:unlink", "hostio-unlink", 0);
13815
b9e7b9c3
UW
13816 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
13817 "vFile:readlink", "hostio-readlink", 0);
13818
0a93529c
GB
13819 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
13820 "vFile:fstat", "hostio-fstat", 0);
13821
2d717e4f
DJ
13822 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
13823 "vAttach", "attach", 0);
13824
13825 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
13826 "vRun", "run", 0);
13827
a6f3e723
SL
13828 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
13829 "QStartNoAckMode", "noack", 0);
13830
82f73884
PA
13831 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
13832 "vKill", "kill", 0);
13833
0b16c5cf
PA
13834 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
13835 "qAttached", "query-attached", 0);
13836
782b2b07 13837 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
13838 "ConditionalTracepoints",
13839 "conditional-tracepoints", 0);
3788aec7
LM
13840
13841 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
13842 "ConditionalBreakpoints",
13843 "conditional-breakpoints", 0);
13844
d3ce09f5
SS
13845 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
13846 "BreakpointCommands",
13847 "breakpoint-commands", 0);
13848
7a697b8d
SS
13849 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
13850 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 13851
409873ef
SS
13852 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
13853 "TracepointSource", "TracepointSource", 0);
13854
d914c394
SS
13855 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
13856 "QAllow", "allow", 0);
13857
0fb4aa4b
PA
13858 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
13859 "StaticTracepoints", "static-tracepoints", 0);
13860
1e4d1764
YQ
13861 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
13862 "InstallInTrace", "install-in-trace", 0);
13863
0fb4aa4b
PA
13864 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
13865 "qXfer:statictrace:read", "read-sdata-object", 0);
13866
78d85199
YQ
13867 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
13868 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
13869
03583c20
UW
13870 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
13871 "QDisableRandomization", "disable-randomization", 0);
13872
d1feda86
YQ
13873 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
13874 "QAgent", "agent", 0);
13875
f6f899bf
HAQ
13876 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
13877 "QTBuffer:size", "trace-buffer-size", 0);
13878
9accd112
MM
13879 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
13880 "Qbtrace:off", "disable-btrace", 0);
13881
13882 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
13883 "Qbtrace:bts", "enable-btrace-bts", 0);
13884
13885 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
13886 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
13887
13888 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
13889 "qXfer:btrace", "read-btrace", 0);
13890
f4abbc16
MM
13891 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
13892 "qXfer:btrace-conf", "read-btrace-conf", 0);
13893
d33501a5
MM
13894 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
13895 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
13896
73b8c1fd
PA
13897 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
13898 "multiprocess-feature", "multiprocess-feature", 0);
13899
f7e6eed5
PA
13900 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
13901 "swbreak-feature", "swbreak-feature", 0);
13902
13903 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
13904 "hwbreak-feature", "hwbreak-feature", 0);
13905
89245bc0
DB
13906 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
13907 "fork-event-feature", "fork-event-feature", 0);
13908
13909 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
13910 "vfork-event-feature", "vfork-event-feature", 0);
13911
b20a6524
MM
13912 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
13913 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
13914
750ce8d1
YQ
13915 add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
13916 "vContSupported", "verbose-resume-supported", 0);
13917
94585166
DB
13918 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
13919 "exec-event-feature", "exec-event-feature", 0);
13920
de979965
PA
13921 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
13922 "vCtrlC", "ctrl-c", 0);
13923
65706a29
PA
13924 add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
13925 "QThreadEvents", "thread-events", 0);
13926
f2faf941
PA
13927 add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
13928 "N stop reply", "no-resumed-stop-reply", 0);
13929
0b736949
DB
13930 /* Assert that we've registered "set remote foo-packet" commands
13931 for all packet configs. */
ca4f7f8b
PA
13932 {
13933 int i;
13934
13935 for (i = 0; i < PACKET_MAX; i++)
13936 {
13937 /* Ideally all configs would have a command associated. Some
13938 still don't though. */
13939 int excepted;
13940
13941 switch (i)
13942 {
13943 case PACKET_QNonStop:
ca4f7f8b
PA
13944 case PACKET_EnableDisableTracepoints_feature:
13945 case PACKET_tracenz_feature:
13946 case PACKET_DisconnectedTracing_feature:
13947 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
13948 case PACKET_qCRC:
13949 /* Additions to this list need to be well justified:
13950 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
13951 excepted = 1;
13952 break;
13953 default:
13954 excepted = 0;
13955 break;
13956 }
13957
13958 /* This catches both forgetting to add a config command, and
13959 forgetting to remove a packet from the exception list. */
13960 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
13961 }
13962 }
13963
37a105a1
DJ
13964 /* Keep the old ``set remote Z-packet ...'' working. Each individual
13965 Z sub-packet has its own set and show commands, but users may
13966 have sets to this variable in their .gdbinit files (or in their
13967 documentation). */
e9e68a56 13968 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
13969 &remote_Z_packet_detect, _("\
13970Set use of remote protocol `Z' packets"), _("\
13971Show use of remote protocol `Z' packets "), _("\
3b64bf98 13972When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 13973packets."),
e9e68a56 13974 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
13975 show_remote_protocol_Z_packet_cmd,
13976 /* FIXME: i18n: Use of remote protocol
13977 `Z' packets is %s. */
e9e68a56 13978 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 13979
a6b151f1
DJ
13980 add_prefix_cmd ("remote", class_files, remote_command, _("\
13981Manipulate files on the remote system\n\
13982Transfer files to and from the remote target system."),
13983 &remote_cmdlist, "remote ",
13984 0 /* allow-unknown */, &cmdlist);
13985
13986 add_cmd ("put", class_files, remote_put_command,
13987 _("Copy a local file to the remote system."),
13988 &remote_cmdlist);
13989
13990 add_cmd ("get", class_files, remote_get_command,
13991 _("Copy a remote file to the local system."),
13992 &remote_cmdlist);
13993
13994 add_cmd ("delete", class_files, remote_delete_command,
13995 _("Delete a remote file."),
13996 &remote_cmdlist);
13997
2d717e4f 13998 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 13999 &remote_exec_file_var, _("\
2d717e4f 14000Set the remote pathname for \"run\""), _("\
94585166
DB
14001Show the remote pathname for \"run\""), NULL,
14002 set_remote_exec_file,
14003 show_remote_exec_file,
14004 &remote_set_cmdlist,
14005 &remote_show_cmdlist);
2d717e4f 14006
c1e36e3e
PA
14007 add_setshow_boolean_cmd ("range-stepping", class_run,
14008 &use_range_stepping, _("\
14009Enable or disable range stepping."), _("\
14010Show whether target-assisted range stepping is enabled."), _("\
14011If on, and the target supports it, when stepping a source line, GDB\n\
14012tells the target to step the corresponding range of addresses itself instead\n\
14013of issuing multiple single-steps. This speeds up source level\n\
14014stepping. If off, GDB always issues single-steps, even if range\n\
14015stepping is supported by the target. The default is on."),
14016 set_range_stepping,
14017 show_range_stepping,
14018 &setlist,
14019 &showlist);
14020
449092f6
CV
14021 /* Eventually initialize fileio. See fileio.c */
14022 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 14023
ba348170 14024 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 14025 special ptids with it set to != 0. */
ba348170
PA
14026 magic_null_ptid = ptid_build (42000, -1, 1);
14027 not_sent_ptid = ptid_build (42000, -2, 1);
14028 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
14029
14030 target_buf_size = 2048;
224c3ddb 14031 target_buf = (char *) xmalloc (target_buf_size);
c906108c 14032}
10760264 14033
This page took 3.111373 seconds and 4 git commands to generate.