Extended-remote follow-exec
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
32d0add0 3 Copyright (C) 1988-2015 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"
35b1e5cc 73
0df8b418 74/* Temp hacks for tracepoint encoding migration. */
35b1e5cc
SS
75static char *target_buf;
76static long target_buf_size;
35b1e5cc 77
94585166
DB
78/* Per-program-space data key. */
79static const struct program_space_data *remote_pspace_data;
80
81/* The variable registered as the control variable used by the
82 remote exec-file commands. While the remote exec-file setting is
83 per-program-space, the set/show machinery uses this as the
84 location of the remote exec-file value. */
85static char *remote_exec_file_var;
86
6765f3e5
DJ
87/* The size to align memory write packets, when practical. The protocol
88 does not guarantee any alignment, and gdb will generate short
89 writes and unaligned writes, but even as a best-effort attempt this
90 can improve bulk transfers. For instance, if a write is misaligned
91 relative to the target's data bus, the stub may need to make an extra
92 round trip fetching data from the target. This doesn't make a
93 huge difference, but it's easy to do, so we try to be helpful.
94
95 The alignment chosen is arbitrary; usually data bus width is
96 important here, not the possibly larger cache line size. */
97enum { REMOTE_ALIGN_WRITES = 16 };
98
23860348 99/* Prototypes for local functions. */
934b9bac 100static void async_cleanup_sigint_signal_handler (void *dummy);
6d820c5c 101static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 102static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 103 int forever, int *is_notif);
6426a772 104
934b9bac
JK
105static void async_handle_remote_sigint (int);
106static void async_handle_remote_sigint_twice (int);
43ff13b4 107
a14ed312 108static void remote_files_info (struct target_ops *ignore);
c906108c 109
f32dbf8c
MM
110static void remote_prepare_to_store (struct target_ops *self,
111 struct regcache *regcache);
c906108c 112
014f9477
TT
113static void remote_open_1 (const char *, int, struct target_ops *,
114 int extended_p);
c906108c 115
de90e03d 116static void remote_close (struct target_ops *self);
c906108c 117
cbb8991c
DB
118struct remote_state;
119
120static int remote_vkill (int pid, struct remote_state *rs);
121
136d6dae 122static void remote_mourn (struct target_ops *ops);
c906108c 123
a14ed312 124static void extended_remote_restart (void);
c906108c 125
136d6dae 126static void extended_remote_mourn (struct target_ops *);
c906108c 127
6d820c5c 128static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 129
a14ed312 130static int readchar (int timeout);
c906108c 131
c33e31fd
PA
132static void remote_serial_write (const char *str, int len);
133
7d85a9c0 134static void remote_kill (struct target_ops *ops);
c906108c 135
6a109b6b 136static int remote_can_async_p (struct target_ops *);
75c99385 137
6a109b6b 138static int remote_is_async_p (struct target_ops *);
75c99385 139
6a3753b3 140static void remote_async (struct target_ops *ops, int enable);
75c99385 141
934b9bac 142static void sync_remote_interrupt_twice (int signo);
7a292a7a 143
a14ed312 144static void interrupt_query (void);
c906108c 145
79d7f229
PA
146static void set_general_thread (struct ptid ptid);
147static void set_continue_thread (struct ptid ptid);
c906108c 148
a14ed312 149static void get_offsets (void);
c906108c 150
6d820c5c
DJ
151static void skip_frame (void);
152
153static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 154
a14ed312 155static int hexnumlen (ULONGEST num);
c906108c 156
a14ed312 157static void init_remote_ops (void);
c906108c 158
a14ed312 159static void init_extended_remote_ops (void);
c906108c 160
1eab8a48 161static void remote_stop (struct target_ops *self, ptid_t);
c906108c 162
a14ed312 163static int stubhex (int ch);
c906108c 164
a14ed312 165static int hexnumstr (char *, ULONGEST);
c906108c 166
a14ed312 167static int hexnumnstr (char *, ULONGEST, int);
2df3850c 168
a14ed312 169static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 170
baa336ce 171static void print_packet (const char *);
c906108c 172
a14ed312 173static void compare_sections_command (char *, int);
c906108c 174
a14ed312 175static void packet_command (char *, int);
c906108c 176
a14ed312 177static int stub_unpack_int (char *buff, int fieldlength);
c906108c 178
39f77062 179static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 180
baa336ce 181static int putpkt_binary (const char *buf, int cnt);
c906108c 182
a14ed312 183static void check_binary_download (CORE_ADDR addr);
c906108c 184
5a2468f5 185struct packet_config;
5a2468f5 186
a14ed312 187static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 188
bb572ddd
DJ
189static void show_remote_protocol_packet_cmd (struct ui_file *file,
190 int from_tty,
191 struct cmd_list_element *c,
192 const char *value);
193
82f73884
PA
194static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
195static ptid_t read_ptid (char *buf, char **obuf);
196
c378d69d 197static void remote_set_permissions (struct target_ops *self);
d914c394 198
8bd200f1
TT
199static int remote_get_trace_status (struct target_ops *self,
200 struct trace_status *ts);
d5551862 201
ab6617cc
TT
202static int remote_upload_tracepoints (struct target_ops *self,
203 struct uploaded_tp **utpp);
00bf0b85 204
181e3713
TT
205static int remote_upload_trace_state_variables (struct target_ops *self,
206 struct uploaded_tsv **utsvp);
00bf0b85 207
c8d104ad
PA
208static void remote_query_supported (void);
209
36d25514 210static void remote_check_symbols (void);
c8d104ad 211
a14ed312 212void _initialize_remote (void);
c906108c 213
74531fed 214struct stop_reply;
74531fed 215static void stop_reply_xfree (struct stop_reply *);
722247f1 216static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 217static void push_stop_reply (struct stop_reply *);
bcc75809 218static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
219static int peek_stop_reply (ptid_t ptid);
220
cbb8991c
DB
221struct threads_listing_context;
222static void remove_new_fork_children (struct threads_listing_context *);
223
74531fed 224static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 225
e3594fd1 226static void remote_terminal_ours (struct target_ops *self);
d3fd5342 227
d962ef82
DJ
228static int remote_read_description_p (struct target_ops *target);
229
176a6961 230static void remote_console_output (char *msg);
dde08ee1 231
efcc2da7 232static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 233
78eff0ec 234static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 235
f4abbc16
MM
236static void remote_btrace_reset (void);
237
221e1a37
PA
238static int stop_reply_queue_length (void);
239
80152258
PA
240static void readahead_cache_invalidate (void);
241
a6b151f1
DJ
242/* For "remote". */
243
244static struct cmd_list_element *remote_cmdlist;
245
bb572ddd
DJ
246/* For "set remote" and "show remote". */
247
248static struct cmd_list_element *remote_set_cmdlist;
249static struct cmd_list_element *remote_show_cmdlist;
250
d458bd84
PA
251/* Stub vCont actions support.
252
253 Each field is a boolean flag indicating whether the stub reports
254 support for the corresponding action. */
255
256struct vCont_action_support
257{
258 /* vCont;t */
259 int t;
c1e36e3e
PA
260
261 /* vCont;r */
262 int r;
d458bd84
PA
263};
264
c1e36e3e
PA
265/* Controls whether GDB is willing to use range stepping. */
266
267static int use_range_stepping = 1;
268
0d031856
TT
269#define OPAQUETHREADBYTES 8
270
271/* a 64 bit opaque identifier */
272typedef unsigned char threadref[OPAQUETHREADBYTES];
273
274/* About this many threadisds fit in a packet. */
275
276#define MAXTHREADLISTRESULTS 32
277
80152258
PA
278/* Data for the vFile:pread readahead cache. */
279
280struct readahead_cache
281{
282 /* The file descriptor for the file that is being cached. -1 if the
283 cache is invalid. */
284 int fd;
285
286 /* The offset into the file that the cache buffer corresponds
287 to. */
288 ULONGEST offset;
289
290 /* The buffer holding the cache contents. */
291 gdb_byte *buf;
292 /* The buffer's size. We try to read as much as fits into a packet
293 at a time. */
294 size_t bufsize;
295
296 /* Cache hit and miss counters. */
297 ULONGEST hit_count;
298 ULONGEST miss_count;
299};
300
ea9c271d
DJ
301/* Description of the remote protocol state for the currently
302 connected target. This is per-target state, and independent of the
303 selected architecture. */
304
305struct remote_state
306{
307 /* A buffer to use for incoming packets, and its current size. The
308 buffer is grown dynamically for larger incoming packets.
309 Outgoing packets may also be constructed in this buffer.
310 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
311 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
312 packets. */
313 char *buf;
314 long buf_size;
be2a5f71 315
1e51243a
PA
316 /* True if we're going through initial connection setup (finding out
317 about the remote side's threads, relocating symbols, etc.). */
318 int starting_up;
319
be2a5f71
DJ
320 /* If we negotiated packet size explicitly (and thus can bypass
321 heuristics for the largest packet size that will not overflow
322 a buffer in the stub), this will be set to that packet size.
323 Otherwise zero, meaning to use the guessed size. */
324 long explicit_packet_size;
2d717e4f
DJ
325
326 /* remote_wait is normally called when the target is running and
327 waits for a stop reply packet. But sometimes we need to call it
328 when the target is already stopped. We can send a "?" packet
329 and have remote_wait read the response. Or, if we already have
330 the response, we can stash it in BUF and tell remote_wait to
331 skip calling getpkt. This flag is set when BUF contains a
332 stop reply packet and the target is not waiting. */
333 int cached_wait_status;
a6f3e723
SL
334
335 /* True, if in no ack mode. That is, neither GDB nor the stub will
336 expect acks from each other. The connection is assumed to be
337 reliable. */
338 int noack_mode;
82f73884
PA
339
340 /* True if we're connected in extended remote mode. */
341 int extended;
342
e24a49d8
PA
343 /* True if we resumed the target and we're waiting for the target to
344 stop. In the mean time, we can't start another command/query.
345 The remote server wouldn't be ready to process it, so we'd
346 timeout waiting for a reply that would never come and eventually
347 we'd close the connection. This can happen in asynchronous mode
348 because we allow GDB commands while the target is running. */
349 int waiting_for_stop_reply;
74531fed 350
d458bd84
PA
351 /* The status of the stub support for the various vCont actions. */
352 struct vCont_action_support supports_vCont;
782b2b07 353
3a29589a
DJ
354 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
355 responded to that. */
356 int ctrlc_pending_p;
5d93a237
TT
357
358 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
359 remote_open knows that we don't have a file open when the program
360 starts. */
361 struct serial *remote_desc;
47f8a51d
TT
362
363 /* These are the threads which we last sent to the remote system. The
364 TID member will be -1 for all or -2 for not sent yet. */
365 ptid_t general_thread;
366 ptid_t continue_thread;
262e1174
TT
367
368 /* This is the traceframe which we last selected on the remote system.
369 It will be -1 if no traceframe is selected. */
370 int remote_traceframe_number;
747dc59d
TT
371
372 char *last_pass_packet;
5e4a05c4
TT
373
374 /* The last QProgramSignals packet sent to the target. We bypass
375 sending a new program signals list down to the target if the new
376 packet is exactly the same as the last we sent. IOW, we only let
377 the target know about program signals list changes. */
378 char *last_program_signals_packet;
b73be471
TT
379
380 enum gdb_signal last_sent_signal;
280ceea3
TT
381
382 int last_sent_step;
8e88304f
TT
383
384 char *finished_object;
385 char *finished_annex;
386 ULONGEST finished_offset;
b80fafe3
TT
387
388 /* Should we try the 'ThreadInfo' query packet?
389
390 This variable (NOT available to the user: auto-detect only!)
391 determines whether GDB will use the new, simpler "ThreadInfo"
392 query or the older, more complex syntax for thread queries.
393 This is an auto-detect variable (set to true at each connect,
394 and set to false when the target fails to recognize it). */
395 int use_threadinfo_query;
396 int use_threadextra_query;
88b496c3 397
ee154bee
TT
398 /* This is set to the data address of the access causing the target
399 to stop for a watchpoint. */
400 CORE_ADDR remote_watch_data_address;
401
f7e6eed5
PA
402 /* Whether the target stopped for a breakpoint/watchpoint. */
403 enum target_stop_reason stop_reason;
0d031856
TT
404
405 threadref echo_nextthread;
406 threadref nextthread;
407 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
408
409 /* The state of remote notification. */
410 struct remote_notif_state *notif_state;
f4abbc16
MM
411
412 /* The branch trace configuration. */
413 struct btrace_config btrace_config;
15a201c8
GB
414
415 /* The argument to the last "vFile:setfs:" packet we sent, used
416 to avoid sending repeated unnecessary "vFile:setfs:" packets.
417 Initialized to -1 to indicate that no "vFile:setfs:" packet
418 has yet been sent. */
419 int fs_pid;
80152258
PA
420
421 /* A readahead cache for vFile:pread. Often, reading a binary
422 involves a sequence of small reads. E.g., when parsing an ELF
423 file. A readahead cache helps mostly the case of remote
424 debugging on a connection with higher latency, due to the
425 request/reply nature of the RSP. We only cache data for a single
426 file descriptor at a time. */
427 struct readahead_cache readahead_cache;
ea9c271d
DJ
428};
429
dc146f7c
VP
430/* Private data that we'll store in (struct thread_info)->private. */
431struct private_thread_info
432{
433 char *extra;
434 int core;
435};
436
437static void
438free_private_thread_info (struct private_thread_info *info)
439{
440 xfree (info->extra);
441 xfree (info);
442}
443
ea9c271d
DJ
444/* This data could be associated with a target, but we do not always
445 have access to the current target when we need it, so for now it is
446 static. This will be fine for as long as only one target is in use
447 at a time. */
cf792862 448static struct remote_state *remote_state;
ea9c271d
DJ
449
450static struct remote_state *
0b83947e 451get_remote_state_raw (void)
ea9c271d 452{
cf792862
TT
453 return remote_state;
454}
455
456/* Allocate a new struct remote_state with xmalloc, initialize it, and
457 return it. */
458
459static struct remote_state *
460new_remote_state (void)
461{
462 struct remote_state *result = XCNEW (struct remote_state);
463
464 /* The default buffer size is unimportant; it will be expanded
465 whenever a larger buffer is needed. */
466 result->buf_size = 400;
467 result->buf = xmalloc (result->buf_size);
262e1174 468 result->remote_traceframe_number = -1;
b73be471 469 result->last_sent_signal = GDB_SIGNAL_0;
15a201c8 470 result->fs_pid = -1;
cf792862
TT
471
472 return result;
ea9c271d
DJ
473}
474
475/* Description of the remote protocol for a given architecture. */
d01949b6 476
ad10f812
AC
477struct packet_reg
478{
479 long offset; /* Offset into G packet. */
480 long regnum; /* GDB's internal register number. */
481 LONGEST pnum; /* Remote protocol register number. */
b323314b 482 int in_g_packet; /* Always part of G packet. */
f5656ead 483 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 484 at present. */
f5656ead 485 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 486 at present. */
ad10f812
AC
487};
488
ea9c271d 489struct remote_arch_state
d01949b6 490{
ad10f812
AC
491 /* Description of the remote protocol registers. */
492 long sizeof_g_packet;
b323314b
AC
493
494 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 495 (making an array gdbarch_num_regs in size). */
b323314b 496 struct packet_reg *regs;
ad10f812 497
d01949b6
AC
498 /* This is the size (in chars) of the first response to the ``g''
499 packet. It is used as a heuristic when determining the maximum
500 size of memory-read and memory-write packets. A target will
501 typically only reserve a buffer large enough to hold the ``g''
502 packet. The size does not include packet overhead (headers and
23860348 503 trailers). */
d01949b6
AC
504 long actual_register_packet_size;
505
506 /* This is the maximum size (in chars) of a non read/write packet.
23860348 507 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
508 long remote_packet_size;
509};
510
35b1e5cc
SS
511/* Utility: generate error from an incoming stub packet. */
512static void
513trace_error (char *buf)
514{
515 if (*buf++ != 'E')
516 return; /* not an error msg */
517 switch (*buf)
518 {
519 case '1': /* malformed packet error */
520 if (*++buf == '0') /* general case: */
521 error (_("remote.c: error in outgoing packet."));
522 else
523 error (_("remote.c: error in outgoing packet at field #%ld."),
524 strtol (buf, NULL, 16));
35b1e5cc
SS
525 default:
526 error (_("Target returns error code '%s'."), buf);
527 }
528}
529
530/* Utility: wait for reply from stub, while accepting "O" packets. */
531static char *
532remote_get_noisy_reply (char **buf_p,
533 long *sizeof_buf)
534{
535 do /* Loop on reply from remote stub. */
536 {
537 char *buf;
a744cf53 538
0df8b418 539 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
540 getpkt (buf_p, sizeof_buf, 0);
541 buf = *buf_p;
ad91cd99 542 if (buf[0] == 'E')
35b1e5cc 543 trace_error (buf);
61012eef 544 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
545 {
546 ULONGEST ul;
547 CORE_ADDR from, to, org_to;
548 char *p, *pp;
549 int adjusted_size = 0;
7556d4a4 550 int relocated = 0;
dde08ee1
PA
551
552 p = buf + strlen ("qRelocInsn:");
553 pp = unpack_varlen_hex (p, &ul);
554 if (*pp != ';')
cb91c06a 555 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
556 from = ul;
557
558 p = pp + 1;
a9cbf802 559 unpack_varlen_hex (p, &ul);
dde08ee1
PA
560 to = ul;
561
562 org_to = to;
563
492d29ea 564 TRY
dde08ee1 565 {
f5656ead 566 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 567 relocated = 1;
dde08ee1 568 }
492d29ea 569 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
570 {
571 if (ex.error == MEMORY_ERROR)
572 {
573 /* Propagate memory errors silently back to the
574 target. The stub may have limited the range of
575 addresses we can write to, for example. */
576 }
577 else
578 {
579 /* Something unexpectedly bad happened. Be verbose
580 so we can tell what, and propagate the error back
581 to the stub, so it doesn't get stuck waiting for
582 a response. */
583 exception_fprintf (gdb_stderr, ex,
584 _("warning: relocating instruction: "));
585 }
586 putpkt ("E01");
587 }
492d29ea 588 END_CATCH
7556d4a4
PA
589
590 if (relocated)
dde08ee1
PA
591 {
592 adjusted_size = to - org_to;
593
bba74b36 594 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
595 putpkt (buf);
596 }
dde08ee1 597 }
ad91cd99 598 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
599 remote_console_output (buf + 1); /* 'O' message from stub */
600 else
0df8b418 601 return buf; /* Here's the actual reply. */
35b1e5cc
SS
602 }
603 while (1);
604}
3c3bea1c 605
d01949b6
AC
606/* Handle for retreving the remote protocol data from gdbarch. */
607static struct gdbarch_data *remote_gdbarch_data_handle;
608
ea9c271d
DJ
609static struct remote_arch_state *
610get_remote_arch_state (void)
d01949b6 611{
17d8546e 612 gdb_assert (target_gdbarch () != NULL);
f5656ead 613 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
614}
615
0b83947e
DJ
616/* Fetch the global remote target state. */
617
618static struct remote_state *
619get_remote_state (void)
620{
621 /* Make sure that the remote architecture state has been
622 initialized, because doing so might reallocate rs->buf. Any
623 function which calls getpkt also needs to be mindful of changes
624 to rs->buf, but this call limits the number of places which run
625 into trouble. */
626 get_remote_arch_state ();
627
628 return get_remote_state_raw ();
629}
630
94585166
DB
631/* Cleanup routine for the remote module's pspace data. */
632
633static void
634remote_pspace_data_cleanup (struct program_space *pspace, void *arg)
635{
636 char *remote_exec_file = arg;
637
638 xfree (remote_exec_file);
639}
640
641/* Fetch the remote exec-file from the current program space. */
642
643static const char *
644get_remote_exec_file (void)
645{
646 char *remote_exec_file;
647
648 remote_exec_file = program_space_data (current_program_space,
649 remote_pspace_data);
650 if (remote_exec_file == NULL)
651 return "";
652
653 return remote_exec_file;
654}
655
656/* Set the remote exec file for PSPACE. */
657
658static void
659set_pspace_remote_exec_file (struct program_space *pspace,
660 char *remote_exec_file)
661{
662 char *old_file = program_space_data (pspace, remote_pspace_data);
663
664 xfree (old_file);
665 set_program_space_data (pspace, remote_pspace_data,
666 xstrdup (remote_exec_file));
667}
668
669/* The "set/show remote exec-file" set command hook. */
670
671static void
672set_remote_exec_file (char *ignored, int from_tty,
673 struct cmd_list_element *c)
674{
675 gdb_assert (remote_exec_file_var != NULL);
676 set_pspace_remote_exec_file (current_program_space, remote_exec_file_var);
677}
678
679/* The "set/show remote exec-file" show command hook. */
680
681static void
682show_remote_exec_file (struct ui_file *file, int from_tty,
683 struct cmd_list_element *cmd, const char *value)
684{
685 fprintf_filtered (file, "%s\n", remote_exec_file_var);
686}
687
74ca34ce
DJ
688static int
689compare_pnums (const void *lhs_, const void *rhs_)
690{
691 const struct packet_reg * const *lhs = lhs_;
692 const struct packet_reg * const *rhs = rhs_;
693
694 if ((*lhs)->pnum < (*rhs)->pnum)
695 return -1;
696 else if ((*lhs)->pnum == (*rhs)->pnum)
697 return 0;
698 else
699 return 1;
700}
701
c21236dc
PA
702static int
703map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 704{
74ca34ce 705 int regnum, num_remote_regs, offset;
74ca34ce 706 struct packet_reg **remote_regs;
ea9c271d 707
4a22f64d 708 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 709 {
c21236dc 710 struct packet_reg *r = &regs[regnum];
baef701f 711
4a22f64d 712 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
713 /* Do not try to fetch zero-sized (placeholder) registers. */
714 r->pnum = -1;
715 else
716 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
717
b323314b 718 r->regnum = regnum;
74ca34ce
DJ
719 }
720
721 /* Define the g/G packet format as the contents of each register
722 with a remote protocol number, in order of ascending protocol
723 number. */
724
4a22f64d 725 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 726 * sizeof (struct packet_reg *));
f57d151a 727 for (num_remote_regs = 0, regnum = 0;
4a22f64d 728 regnum < gdbarch_num_regs (gdbarch);
f57d151a 729 regnum++)
c21236dc
PA
730 if (regs[regnum].pnum != -1)
731 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 732
74ca34ce
DJ
733 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
734 compare_pnums);
735
736 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
737 {
738 remote_regs[regnum]->in_g_packet = 1;
739 remote_regs[regnum]->offset = offset;
4a22f64d 740 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
741 }
742
c21236dc
PA
743 return offset;
744}
745
746/* Given the architecture described by GDBARCH, return the remote
747 protocol register's number and the register's offset in the g/G
748 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
749 If the target does not have a mapping for REGNUM, return false,
750 otherwise, return true. */
751
752int
753remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
754 int *pnum, int *poffset)
755{
756 int sizeof_g_packet;
757 struct packet_reg *regs;
758 struct cleanup *old_chain;
759
760 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
761
762 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
763 old_chain = make_cleanup (xfree, regs);
764
765 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
766
767 *pnum = regs[regnum].pnum;
768 *poffset = regs[regnum].offset;
769
770 do_cleanups (old_chain);
771
772 return *pnum != -1;
773}
774
775static void *
776init_remote_state (struct gdbarch *gdbarch)
777{
778 struct remote_state *rs = get_remote_state_raw ();
779 struct remote_arch_state *rsa;
780
781 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
782
783 /* Use the architecture to build a regnum<->pnum table, which will be
784 1:1 unless a feature set specifies otherwise. */
785 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
786 gdbarch_num_regs (gdbarch),
787 struct packet_reg);
788
74ca34ce
DJ
789 /* Record the maximum possible size of the g packet - it may turn out
790 to be smaller. */
c21236dc 791 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 792
0df8b418 793 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
794 remote stubs have a hardwired buffer size of 400 bytes
795 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
796 as the maximum packet-size to ensure that the packet and an extra
797 NUL character can always fit in the buffer. This stops GDB
798 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
799 already a full buffer (As of 1999-12-04 that was most stubs). */
800 rsa->remote_packet_size = 400 - 1;
d01949b6 801
ea9c271d
DJ
802 /* This one is filled in when a ``g'' packet is received. */
803 rsa->actual_register_packet_size = 0;
804
805 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
806 default, adjust the size accordingly. Remember that each byte is
807 encoded as two characters. 32 is the overhead for the packet
808 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 809 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 810 little. */
ea9c271d
DJ
811 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
812 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 813
ea9c271d
DJ
814 /* Make sure that the packet buffer is plenty big enough for
815 this architecture. */
816 if (rs->buf_size < rsa->remote_packet_size)
817 {
818 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 819 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 820 }
6d820c5c 821
ea9c271d
DJ
822 return rsa;
823}
824
825/* Return the current allowed size of a remote packet. This is
826 inferred from the current architecture, and should be used to
827 limit the length of outgoing packets. */
828static long
829get_remote_packet_size (void)
830{
be2a5f71 831 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
832 struct remote_arch_state *rsa = get_remote_arch_state ();
833
be2a5f71
DJ
834 if (rs->explicit_packet_size)
835 return rs->explicit_packet_size;
836
ea9c271d 837 return rsa->remote_packet_size;
d01949b6
AC
838}
839
ad10f812 840static struct packet_reg *
ea9c271d 841packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 842{
f5656ead 843 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
844 return NULL;
845 else
ad10f812 846 {
ea9c271d 847 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 848
b323314b
AC
849 gdb_assert (r->regnum == regnum);
850 return r;
ad10f812 851 }
ad10f812
AC
852}
853
854static struct packet_reg *
ea9c271d 855packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 856{
b323314b 857 int i;
a744cf53 858
f5656ead 859 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 860 {
ea9c271d 861 struct packet_reg *r = &rsa->regs[i];
a744cf53 862
b323314b
AC
863 if (r->pnum == pnum)
864 return r;
ad10f812
AC
865 }
866 return NULL;
d01949b6
AC
867}
868
c906108c
SS
869static struct target_ops remote_ops;
870
871static struct target_ops extended_remote_ops;
872
6426a772
JM
873/* FIXME: cagney/1999-09-23: Even though getpkt was called with
874 ``forever'' still use the normal timeout mechanism. This is
875 currently used by the ASYNC code to guarentee that target reads
876 during the initial connect always time-out. Once getpkt has been
877 modified to return a timeout indication and, in turn
878 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 879 this can go away. */
6426a772
JM
880static int wait_forever_enabled_p = 1;
881
9a7071a8
JB
882/* Allow the user to specify what sequence to send to the remote
883 when he requests a program interruption: Although ^C is usually
884 what remote systems expect (this is the default, here), it is
885 sometimes preferable to send a break. On other systems such
886 as the Linux kernel, a break followed by g, which is Magic SysRq g
887 is required in order to interrupt the execution. */
888const char interrupt_sequence_control_c[] = "Ctrl-C";
889const char interrupt_sequence_break[] = "BREAK";
890const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 891static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
892 {
893 interrupt_sequence_control_c,
894 interrupt_sequence_break,
895 interrupt_sequence_break_g,
896 NULL
897 };
898static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
899
900static void
901show_interrupt_sequence (struct ui_file *file, int from_tty,
902 struct cmd_list_element *c,
903 const char *value)
904{
905 if (interrupt_sequence_mode == interrupt_sequence_control_c)
906 fprintf_filtered (file,
907 _("Send the ASCII ETX character (Ctrl-c) "
908 "to the remote target to interrupt the "
909 "execution of the program.\n"));
910 else if (interrupt_sequence_mode == interrupt_sequence_break)
911 fprintf_filtered (file,
912 _("send a break signal to the remote target "
913 "to interrupt the execution of the program.\n"));
914 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
915 fprintf_filtered (file,
916 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
917 "the remote target to interrupt the execution "
918 "of Linux kernel.\n"));
919 else
920 internal_error (__FILE__, __LINE__,
921 _("Invalid value for interrupt_sequence_mode: %s."),
922 interrupt_sequence_mode);
923}
6426a772 924
9a7071a8
JB
925/* This boolean variable specifies whether interrupt_sequence is sent
926 to the remote target when gdb connects to it.
927 This is mostly needed when you debug the Linux kernel: The Linux kernel
928 expects BREAK g which is Magic SysRq g for connecting gdb. */
929static int interrupt_on_connect = 0;
c906108c 930
9a7071a8
JB
931/* This variable is used to implement the "set/show remotebreak" commands.
932 Since these commands are now deprecated in favor of "set/show remote
933 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
934static int remote_break;
935
9a7071a8
JB
936static void
937set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
938{
939 if (remote_break)
940 interrupt_sequence_mode = interrupt_sequence_break;
941 else
942 interrupt_sequence_mode = interrupt_sequence_control_c;
943}
944
945static void
946show_remotebreak (struct ui_file *file, int from_tty,
947 struct cmd_list_element *c,
948 const char *value)
949{
950}
951
c906108c
SS
952/* This variable sets the number of bits in an address that are to be
953 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 954 leading zeros, the entire address would be sent. This variable
c906108c
SS
955 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
956 initial implementation of remote.c restricted the address sent in
957 memory packets to ``host::sizeof long'' bytes - (typically 32
958 bits). Consequently, for 64 bit targets, the upper 32 bits of an
959 address was never sent. Since fixing this bug may cause a break in
960 some remote targets this variable is principly provided to
23860348 961 facilitate backward compatibility. */
c906108c 962
883b9c6c 963static unsigned int remote_address_size;
c906108c 964
75c99385
PA
965/* Temporary to track who currently owns the terminal. See
966 remote_terminal_* for more details. */
6426a772
JM
967
968static int remote_async_terminal_ours_p;
969
11cf8741 970\f
11cf8741 971/* User configurable variables for the number of characters in a
ea9c271d
DJ
972 memory read/write packet. MIN (rsa->remote_packet_size,
973 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 974 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
975 (speed up transfers). The variables ``preferred_*'' (the user
976 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 977 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
978
979struct memory_packet_config
980{
981 char *name;
982 long size;
983 int fixed_p;
984};
985
a5c0808e
PA
986/* The default max memory-write-packet-size. The 16k is historical.
987 (It came from older GDB's using alloca for buffers and the
988 knowledge (folklore?) that some hosts don't cope very well with
989 large alloca calls.) */
990#define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384
991
992/* The minimum remote packet size for memory transfers. Ensures we
993 can write at least one byte. */
994#define MIN_MEMORY_PACKET_SIZE 20
995
11cf8741
JM
996/* Compute the current size of a read/write packet. Since this makes
997 use of ``actual_register_packet_size'' the computation is dynamic. */
998
999static long
1000get_memory_packet_size (struct memory_packet_config *config)
1001{
d01949b6 1002 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
1003 struct remote_arch_state *rsa = get_remote_arch_state ();
1004
11cf8741
JM
1005 long what_they_get;
1006 if (config->fixed_p)
1007 {
1008 if (config->size <= 0)
a5c0808e 1009 what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE;
11cf8741
JM
1010 else
1011 what_they_get = config->size;
1012 }
1013 else
1014 {
ea9c271d 1015 what_they_get = get_remote_packet_size ();
23860348 1016 /* Limit the packet to the size specified by the user. */
11cf8741
JM
1017 if (config->size > 0
1018 && what_they_get > config->size)
1019 what_they_get = config->size;
be2a5f71
DJ
1020
1021 /* Limit it to the size of the targets ``g'' response unless we have
1022 permission from the stub to use a larger packet size. */
1023 if (rs->explicit_packet_size == 0
1024 && rsa->actual_register_packet_size > 0
1025 && what_they_get > rsa->actual_register_packet_size)
1026 what_they_get = rsa->actual_register_packet_size;
11cf8741 1027 }
a5c0808e
PA
1028 if (what_they_get < MIN_MEMORY_PACKET_SIZE)
1029 what_they_get = MIN_MEMORY_PACKET_SIZE;
6d820c5c
DJ
1030
1031 /* Make sure there is room in the global buffer for this packet
1032 (including its trailing NUL byte). */
1033 if (rs->buf_size < what_they_get + 1)
1034 {
1035 rs->buf_size = 2 * what_they_get;
1036 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
1037 }
1038
11cf8741
JM
1039 return what_they_get;
1040}
1041
0df8b418 1042/* Update the size of a read/write packet. If they user wants
23860348 1043 something really big then do a sanity check. */
11cf8741
JM
1044
1045static void
1046set_memory_packet_size (char *args, struct memory_packet_config *config)
1047{
1048 int fixed_p = config->fixed_p;
1049 long size = config->size;
a744cf53 1050
11cf8741 1051 if (args == NULL)
8a3fe4f8 1052 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
1053 else if (strcmp (args, "hard") == 0
1054 || strcmp (args, "fixed") == 0)
1055 fixed_p = 1;
1056 else if (strcmp (args, "soft") == 0
1057 || strcmp (args, "limit") == 0)
1058 fixed_p = 0;
1059 else
1060 {
1061 char *end;
a744cf53 1062
11cf8741
JM
1063 size = strtoul (args, &end, 0);
1064 if (args == end)
8a3fe4f8 1065 error (_("Invalid %s (bad syntax)."), config->name);
a5c0808e
PA
1066
1067 /* Instead of explicitly capping the size of a packet to or
1068 disallowing it, the user is allowed to set the size to
1069 something arbitrarily large. */
11cf8741 1070 }
a5c0808e
PA
1071
1072 /* So that the query shows the correct value. */
1073 if (size <= 0)
1074 size = DEFAULT_MAX_MEMORY_PACKET_SIZE;
1075
23860348 1076 /* Extra checks? */
11cf8741
JM
1077 if (fixed_p && !config->fixed_p)
1078 {
e2e0b3e5
AC
1079 if (! query (_("The target may not be able to correctly handle a %s\n"
1080 "of %ld bytes. Change the packet size? "),
11cf8741 1081 config->name, size))
8a3fe4f8 1082 error (_("Packet size not changed."));
11cf8741 1083 }
23860348 1084 /* Update the config. */
11cf8741
JM
1085 config->fixed_p = fixed_p;
1086 config->size = size;
1087}
1088
1089static void
1090show_memory_packet_size (struct memory_packet_config *config)
1091{
a3f17187 1092 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 1093 if (config->fixed_p)
a3f17187 1094 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1095 get_memory_packet_size (config));
1096 else
a3f17187 1097 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1098 get_memory_packet_size (config));
1099}
1100
1101static struct memory_packet_config memory_write_packet_config =
1102{
1103 "memory-write-packet-size",
1104};
1105
1106static void
1107set_memory_write_packet_size (char *args, int from_tty)
1108{
1109 set_memory_packet_size (args, &memory_write_packet_config);
1110}
1111
1112static void
1113show_memory_write_packet_size (char *args, int from_tty)
1114{
1115 show_memory_packet_size (&memory_write_packet_config);
1116}
1117
1118static long
1119get_memory_write_packet_size (void)
1120{
1121 return get_memory_packet_size (&memory_write_packet_config);
1122}
1123
1124static struct memory_packet_config memory_read_packet_config =
1125{
1126 "memory-read-packet-size",
1127};
1128
1129static void
1130set_memory_read_packet_size (char *args, int from_tty)
1131{
1132 set_memory_packet_size (args, &memory_read_packet_config);
1133}
1134
1135static void
1136show_memory_read_packet_size (char *args, int from_tty)
1137{
1138 show_memory_packet_size (&memory_read_packet_config);
1139}
1140
1141static long
1142get_memory_read_packet_size (void)
1143{
1144 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1145
11cf8741
JM
1146 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1147 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1148 increased beyond this. */
1149 if (size > get_remote_packet_size ())
1150 size = get_remote_packet_size ();
11cf8741
JM
1151 return size;
1152}
1153
11cf8741 1154\f
5a2468f5 1155/* Generic configuration support for packets the stub optionally
0df8b418 1156 supports. Allows the user to specify the use of the packet as well
23860348 1157 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1158
1159enum packet_support
1160 {
1161 PACKET_SUPPORT_UNKNOWN = 0,
1162 PACKET_ENABLE,
1163 PACKET_DISABLE
1164 };
1165
5a2468f5
JM
1166struct packet_config
1167 {
bb572ddd
DJ
1168 const char *name;
1169 const char *title;
4082afcc
PA
1170
1171 /* If auto, GDB auto-detects support for this packet or feature,
1172 either through qSupported, or by trying the packet and looking
1173 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1174 packet. If false, the packet is disabled. Configs that don't
1175 have an associated command always have this set to auto. */
7f19b9a2 1176 enum auto_boolean detect;
4082afcc
PA
1177
1178 /* Does the target support this packet? */
5a2468f5
JM
1179 enum packet_support support;
1180 };
1181
d471ea57 1182/* Analyze a packet's return value and update the packet config
23860348 1183 accordingly. */
d471ea57
AC
1184
1185enum packet_result
1186{
1187 PACKET_ERROR,
1188 PACKET_OK,
1189 PACKET_UNKNOWN
1190};
1191
4082afcc
PA
1192static enum packet_support packet_config_support (struct packet_config *config);
1193static enum packet_support packet_support (int packet);
5a2468f5
JM
1194
1195static void
fba45db2 1196show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1197{
1198 char *support = "internal-error";
a744cf53 1199
4082afcc 1200 switch (packet_config_support (config))
5a2468f5
JM
1201 {
1202 case PACKET_ENABLE:
1203 support = "enabled";
1204 break;
1205 case PACKET_DISABLE:
1206 support = "disabled";
1207 break;
1208 case PACKET_SUPPORT_UNKNOWN:
1209 support = "unknown";
1210 break;
1211 }
1212 switch (config->detect)
1213 {
7f19b9a2 1214 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1215 printf_filtered (_("Support for the `%s' packet "
1216 "is auto-detected, currently %s.\n"),
37a105a1 1217 config->name, support);
5a2468f5 1218 break;
7f19b9a2
AC
1219 case AUTO_BOOLEAN_TRUE:
1220 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1221 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1222 config->name, support);
8e248173 1223 break;
5a2468f5
JM
1224 }
1225}
1226
1227static void
bb572ddd
DJ
1228add_packet_config_cmd (struct packet_config *config, const char *name,
1229 const char *title, int legacy)
d471ea57 1230{
5a2468f5
JM
1231 char *set_doc;
1232 char *show_doc;
d471ea57 1233 char *cmd_name;
3ed07be4 1234
5a2468f5
JM
1235 config->name = name;
1236 config->title = title;
b435e160
AC
1237 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1238 name, title);
3e43a32a
MS
1239 show_doc = xstrprintf ("Show current use of remote "
1240 "protocol `%s' (%s) packet",
b435e160 1241 name, title);
d471ea57 1242 /* set/show TITLE-packet {auto,on,off} */
b435e160 1243 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1244 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1245 &config->detect, set_doc,
1246 show_doc, NULL, /* help_doc */
4082afcc 1247 NULL,
bb572ddd
DJ
1248 show_remote_protocol_packet_cmd,
1249 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1250 /* The command code copies the documentation strings. */
1251 xfree (set_doc);
1252 xfree (show_doc);
23860348 1253 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1254 if (legacy)
1255 {
1256 char *legacy_name;
a744cf53 1257
b435e160 1258 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1259 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1260 &remote_set_cmdlist);
d471ea57 1261 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1262 &remote_show_cmdlist);
d471ea57 1263 }
5a2468f5
JM
1264}
1265
d471ea57 1266static enum packet_result
a76d924d 1267packet_check_result (const char *buf)
5a2468f5 1268{
d471ea57 1269 if (buf[0] != '\0')
5a2468f5 1270 {
d471ea57 1271 /* The stub recognized the packet request. Check that the
23860348 1272 operation succeeded. */
a76d924d
DJ
1273 if (buf[0] == 'E'
1274 && isxdigit (buf[1]) && isxdigit (buf[2])
1275 && buf[3] == '\0')
1276 /* "Enn" - definitly an error. */
1277 return PACKET_ERROR;
1278
1279 /* Always treat "E." as an error. This will be used for
1280 more verbose error messages, such as E.memtypes. */
1281 if (buf[0] == 'E' && buf[1] == '.')
1282 return PACKET_ERROR;
1283
1284 /* The packet may or may not be OK. Just assume it is. */
1285 return PACKET_OK;
1286 }
1287 else
1288 /* The stub does not support the packet. */
1289 return PACKET_UNKNOWN;
1290}
1291
1292static enum packet_result
1293packet_ok (const char *buf, struct packet_config *config)
1294{
1295 enum packet_result result;
1296
4082afcc
PA
1297 if (config->detect != AUTO_BOOLEAN_TRUE
1298 && config->support == PACKET_DISABLE)
1299 internal_error (__FILE__, __LINE__,
1300 _("packet_ok: attempt to use a disabled packet"));
1301
a76d924d
DJ
1302 result = packet_check_result (buf);
1303 switch (result)
1304 {
1305 case PACKET_OK:
1306 case PACKET_ERROR:
1307 /* The stub recognized the packet request. */
4082afcc 1308 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1309 {
d471ea57
AC
1310 if (remote_debug)
1311 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1312 "Packet %s (%s) is supported\n",
1313 config->name, config->title);
d471ea57 1314 config->support = PACKET_ENABLE;
d471ea57 1315 }
a76d924d
DJ
1316 break;
1317 case PACKET_UNKNOWN:
23860348 1318 /* The stub does not support the packet. */
4082afcc
PA
1319 if (config->detect == AUTO_BOOLEAN_AUTO
1320 && config->support == PACKET_ENABLE)
d471ea57 1321 {
4082afcc
PA
1322 /* If the stub previously indicated that the packet was
1323 supported then there is a protocol error. */
1324 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1325 config->name, config->title);
1326 }
1327 else if (config->detect == AUTO_BOOLEAN_TRUE)
1328 {
1329 /* The user set it wrong. */
1330 error (_("Enabled packet %s (%s) not recognized by stub"),
1331 config->name, config->title);
d471ea57 1332 }
4082afcc
PA
1333
1334 if (remote_debug)
1335 fprintf_unfiltered (gdb_stdlog,
1336 "Packet %s (%s) is NOT supported\n",
1337 config->name, config->title);
1338 config->support = PACKET_DISABLE;
a76d924d 1339 break;
5a2468f5 1340 }
a76d924d
DJ
1341
1342 return result;
5a2468f5
JM
1343}
1344
444abaca
DJ
1345enum {
1346 PACKET_vCont = 0,
1347 PACKET_X,
1348 PACKET_qSymbol,
1349 PACKET_P,
1350 PACKET_p,
1351 PACKET_Z0,
1352 PACKET_Z1,
1353 PACKET_Z2,
1354 PACKET_Z3,
1355 PACKET_Z4,
15a201c8 1356 PACKET_vFile_setfs,
a6b151f1
DJ
1357 PACKET_vFile_open,
1358 PACKET_vFile_pread,
1359 PACKET_vFile_pwrite,
1360 PACKET_vFile_close,
1361 PACKET_vFile_unlink,
b9e7b9c3 1362 PACKET_vFile_readlink,
0a93529c 1363 PACKET_vFile_fstat,
0876f84a 1364 PACKET_qXfer_auxv,
23181151 1365 PACKET_qXfer_features,
c78fa86a 1366 PACKET_qXfer_exec_file,
cfa9d6d9 1367 PACKET_qXfer_libraries,
2268b414 1368 PACKET_qXfer_libraries_svr4,
fd79ecee 1369 PACKET_qXfer_memory_map,
0e7f50da
UW
1370 PACKET_qXfer_spu_read,
1371 PACKET_qXfer_spu_write,
07e059b5 1372 PACKET_qXfer_osdata,
dc146f7c 1373 PACKET_qXfer_threads,
0fb4aa4b 1374 PACKET_qXfer_statictrace_read,
b3b9301e 1375 PACKET_qXfer_traceframe_info,
169081d0 1376 PACKET_qXfer_uib,
711e434b 1377 PACKET_qGetTIBAddr,
444abaca 1378 PACKET_qGetTLSAddr,
be2a5f71 1379 PACKET_qSupported,
bd3eecc3 1380 PACKET_qTStatus,
89be2091 1381 PACKET_QPassSignals,
9b224c5e 1382 PACKET_QProgramSignals,
936d2992 1383 PACKET_qCRC,
08388c79 1384 PACKET_qSearch_memory,
2d717e4f
DJ
1385 PACKET_vAttach,
1386 PACKET_vRun,
a6f3e723 1387 PACKET_QStartNoAckMode,
82f73884 1388 PACKET_vKill,
4aa995e1
PA
1389 PACKET_qXfer_siginfo_read,
1390 PACKET_qXfer_siginfo_write,
0b16c5cf 1391 PACKET_qAttached,
4082afcc
PA
1392
1393 /* Support for conditional tracepoints. */
782b2b07 1394 PACKET_ConditionalTracepoints,
4082afcc
PA
1395
1396 /* Support for target-side breakpoint conditions. */
3788aec7 1397 PACKET_ConditionalBreakpoints,
4082afcc
PA
1398
1399 /* Support for target-side breakpoint commands. */
d3ce09f5 1400 PACKET_BreakpointCommands,
4082afcc
PA
1401
1402 /* Support for fast tracepoints. */
7a697b8d 1403 PACKET_FastTracepoints,
4082afcc
PA
1404
1405 /* Support for static tracepoints. */
0fb4aa4b 1406 PACKET_StaticTracepoints,
4082afcc
PA
1407
1408 /* Support for installing tracepoints while a trace experiment is
1409 running. */
1e4d1764 1410 PACKET_InstallInTrace,
4082afcc 1411
40ab02ce
MS
1412 PACKET_bc,
1413 PACKET_bs,
409873ef 1414 PACKET_TracepointSource,
d914c394 1415 PACKET_QAllow,
78d85199 1416 PACKET_qXfer_fdpic,
03583c20 1417 PACKET_QDisableRandomization,
d1feda86 1418 PACKET_QAgent,
f6f899bf 1419 PACKET_QTBuffer_size,
9accd112
MM
1420 PACKET_Qbtrace_off,
1421 PACKET_Qbtrace_bts,
b20a6524 1422 PACKET_Qbtrace_pt,
9accd112 1423 PACKET_qXfer_btrace,
4082afcc
PA
1424
1425 /* Support for the QNonStop packet. */
1426 PACKET_QNonStop,
1427
1428 /* Support for multi-process extensions. */
1429 PACKET_multiprocess_feature,
1430
1431 /* Support for enabling and disabling tracepoints while a trace
1432 experiment is running. */
1433 PACKET_EnableDisableTracepoints_feature,
1434
1435 /* Support for collecting strings using the tracenz bytecode. */
1436 PACKET_tracenz_feature,
1437
1438 /* Support for continuing to run a trace experiment while GDB is
1439 disconnected. */
1440 PACKET_DisconnectedTracing_feature,
1441
1442 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1443 PACKET_augmented_libraries_svr4_read_feature,
1444
f4abbc16
MM
1445 /* Support for the qXfer:btrace-conf:read packet. */
1446 PACKET_qXfer_btrace_conf,
1447
d33501a5
MM
1448 /* Support for the Qbtrace-conf:bts:size packet. */
1449 PACKET_Qbtrace_conf_bts_size,
1450
f7e6eed5
PA
1451 /* Support for swbreak+ feature. */
1452 PACKET_swbreak_feature,
1453
1454 /* Support for hwbreak+ feature. */
1455 PACKET_hwbreak_feature,
1456
89245bc0
DB
1457 /* Support for fork events. */
1458 PACKET_fork_event_feature,
1459
1460 /* Support for vfork events. */
1461 PACKET_vfork_event_feature,
1462
b20a6524
MM
1463 /* Support for the Qbtrace-conf:pt:size packet. */
1464 PACKET_Qbtrace_conf_pt_size,
1465
94585166
DB
1466 /* Support for exec events. */
1467 PACKET_exec_event_feature,
1468
444abaca
DJ
1469 PACKET_MAX
1470};
506fb367 1471
444abaca 1472static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1473
f7e6eed5
PA
1474/* Returns the packet's corresponding "set remote foo-packet" command
1475 state. See struct packet_config for more details. */
1476
1477static enum auto_boolean
1478packet_set_cmd_state (int packet)
1479{
1480 return remote_protocol_packets[packet].detect;
1481}
1482
4082afcc
PA
1483/* Returns whether a given packet or feature is supported. This takes
1484 into account the state of the corresponding "set remote foo-packet"
1485 command, which may be used to bypass auto-detection. */
dc8acb97 1486
4082afcc
PA
1487static enum packet_support
1488packet_config_support (struct packet_config *config)
1489{
1490 switch (config->detect)
444abaca 1491 {
4082afcc
PA
1492 case AUTO_BOOLEAN_TRUE:
1493 return PACKET_ENABLE;
1494 case AUTO_BOOLEAN_FALSE:
1495 return PACKET_DISABLE;
1496 case AUTO_BOOLEAN_AUTO:
1497 return config->support;
1498 default:
1499 gdb_assert_not_reached (_("bad switch"));
444abaca 1500 }
4082afcc
PA
1501}
1502
1503/* Same as packet_config_support, but takes the packet's enum value as
1504 argument. */
1505
1506static enum packet_support
1507packet_support (int packet)
1508{
1509 struct packet_config *config = &remote_protocol_packets[packet];
1510
1511 return packet_config_support (config);
dc8acb97
MS
1512}
1513
5a2468f5 1514static void
444abaca
DJ
1515show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1516 struct cmd_list_element *c,
1517 const char *value)
5a2468f5 1518{
444abaca 1519 struct packet_config *packet;
5a2468f5 1520
444abaca
DJ
1521 for (packet = remote_protocol_packets;
1522 packet < &remote_protocol_packets[PACKET_MAX];
1523 packet++)
1524 {
1525 if (&packet->detect == c->var)
1526 {
1527 show_packet_config_cmd (packet);
1528 return;
1529 }
1530 }
9b20d036 1531 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1532 c->name);
5a2468f5
JM
1533}
1534
d471ea57
AC
1535/* Should we try one of the 'Z' requests? */
1536
1537enum Z_packet_type
1538{
1539 Z_PACKET_SOFTWARE_BP,
1540 Z_PACKET_HARDWARE_BP,
1541 Z_PACKET_WRITE_WP,
1542 Z_PACKET_READ_WP,
1543 Z_PACKET_ACCESS_WP,
1544 NR_Z_PACKET_TYPES
1545};
96baa820 1546
d471ea57 1547/* For compatibility with older distributions. Provide a ``set remote
23860348 1548 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1549
7f19b9a2 1550static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1551
1552static void
fba45db2
KB
1553set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1554 struct cmd_list_element *c)
96baa820 1555{
d471ea57 1556 int i;
a744cf53 1557
d471ea57 1558 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1559 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1560}
1561
1562static void
08546159
AC
1563show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1564 struct cmd_list_element *c,
1565 const char *value)
96baa820 1566{
d471ea57 1567 int i;
a744cf53 1568
d471ea57
AC
1569 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1570 {
444abaca 1571 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1572 }
96baa820
JM
1573}
1574
4082afcc
PA
1575/* Returns true if the multi-process extensions are in effect. */
1576
1577static int
1578remote_multi_process_p (struct remote_state *rs)
1579{
1580 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1581}
1582
de0d863e
DB
1583/* Returns true if fork events are supported. */
1584
1585static int
1586remote_fork_event_p (struct remote_state *rs)
1587{
1588 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1589}
1590
c269dbdb
DB
1591/* Returns true if vfork events are supported. */
1592
1593static int
1594remote_vfork_event_p (struct remote_state *rs)
1595{
1596 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1597}
1598
cbb8991c
DB
1599/* Insert fork catchpoint target routine. If fork events are enabled
1600 then return success, nothing more to do. */
1601
1602static int
1603remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1604{
1605 struct remote_state *rs = get_remote_state ();
1606
1607 return !remote_fork_event_p (rs);
1608}
1609
1610/* Remove fork catchpoint target routine. Nothing to do, just
1611 return success. */
1612
1613static int
1614remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1615{
1616 return 0;
1617}
1618
1619/* Insert vfork catchpoint target routine. If vfork events are enabled
1620 then return success, nothing more to do. */
1621
1622static int
1623remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1624{
1625 struct remote_state *rs = get_remote_state ();
1626
1627 return !remote_vfork_event_p (rs);
1628}
1629
1630/* Remove vfork catchpoint target routine. Nothing to do, just
1631 return success. */
1632
1633static int
1634remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1635{
1636 return 0;
1637}
1638
23860348 1639/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1640static struct async_signal_handler *async_sigint_remote_twice_token;
1641static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1642
74531fed
PA
1643\f
1644/* Asynchronous signal handle registered as event loop source for
1645 when we have pending events ready to be passed to the core. */
1646
1647static struct async_event_handler *remote_async_inferior_event_token;
1648
c906108c
SS
1649\f
1650
79d7f229
PA
1651static ptid_t magic_null_ptid;
1652static ptid_t not_sent_ptid;
1653static ptid_t any_thread_ptid;
1654
0b16c5cf
PA
1655/* Find out if the stub attached to PID (and hence GDB should offer to
1656 detach instead of killing it when bailing out). */
1657
1658static int
1659remote_query_attached (int pid)
1660{
1661 struct remote_state *rs = get_remote_state ();
bba74b36 1662 size_t size = get_remote_packet_size ();
0b16c5cf 1663
4082afcc 1664 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1665 return 0;
1666
1667 if (remote_multi_process_p (rs))
bba74b36 1668 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1669 else
bba74b36 1670 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1671
1672 putpkt (rs->buf);
1673 getpkt (&rs->buf, &rs->buf_size, 0);
1674
1675 switch (packet_ok (rs->buf,
1554e9be 1676 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1677 {
1678 case PACKET_OK:
1679 if (strcmp (rs->buf, "1") == 0)
1680 return 1;
1681 break;
1682 case PACKET_ERROR:
1683 warning (_("Remote failure reply: %s"), rs->buf);
1684 break;
1685 case PACKET_UNKNOWN:
1686 break;
1687 }
1688
1689 return 0;
1690}
1691
49c62f2e
PA
1692/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1693 has been invented by GDB, instead of reported by the target. Since
1694 we can be connected to a remote system before before knowing about
1695 any inferior, mark the target with execution when we find the first
1696 inferior. If ATTACHED is 1, then we had just attached to this
1697 inferior. If it is 0, then we just created this inferior. If it
1698 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1699 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1700 attempt to open this inferior's executable as the main executable
1701 if no main executable is open already. */
1941c569
PA
1702
1703static struct inferior *
1b6e6f5c
GB
1704remote_add_inferior (int fake_pid_p, int pid, int attached,
1705 int try_open_exec)
1941c569 1706{
1941c569
PA
1707 struct inferior *inf;
1708
0b16c5cf
PA
1709 /* Check whether this process we're learning about is to be
1710 considered attached, or if is to be considered to have been
1711 spawned by the stub. */
1712 if (attached == -1)
1713 attached = remote_query_attached (pid);
1714
f5656ead 1715 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1716 {
1717 /* If the target shares code across all inferiors, then every
1718 attach adds a new inferior. */
1719 inf = add_inferior (pid);
1720
1721 /* ... and every inferior is bound to the same program space.
1722 However, each inferior may still have its own address
1723 space. */
1724 inf->aspace = maybe_new_address_space ();
1725 inf->pspace = current_program_space;
1726 }
1727 else
1728 {
1729 /* In the traditional debugging scenario, there's a 1-1 match
1730 between program/address spaces. We simply bind the inferior
1731 to the program space's address space. */
1732 inf = current_inferior ();
1733 inferior_appeared (inf, pid);
1734 }
1941c569 1735
0b16c5cf 1736 inf->attach_flag = attached;
49c62f2e 1737 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1738
1b6e6f5c
GB
1739 /* If no main executable is currently open then attempt to
1740 open the file that was executed to create this inferior. */
835205d0 1741 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1742 exec_file_locate_attach (pid, 1);
1743
1941c569
PA
1744 return inf;
1745}
1746
1747/* Add thread PTID to GDB's thread list. Tag it as executing/running
1748 according to RUNNING. */
1749
c906108c 1750static void
1941c569 1751remote_add_thread (ptid_t ptid, int running)
c906108c 1752{
b7ea362b
PA
1753 struct remote_state *rs = get_remote_state ();
1754
1755 /* GDB historically didn't pull threads in the initial connection
1756 setup. If the remote target doesn't even have a concept of
1757 threads (e.g., a bare-metal target), even if internally we
1758 consider that a single-threaded target, mentioning a new thread
1759 might be confusing to the user. Be silent then, preserving the
1760 age old behavior. */
1761 if (rs->starting_up)
1762 add_thread_silent (ptid);
1763 else
1764 add_thread (ptid);
1941c569
PA
1765
1766 set_executing (ptid, running);
1767 set_running (ptid, running);
1768}
1769
1770/* Come here when we learn about a thread id from the remote target.
1771 It may be the first time we hear about such thread, so take the
1772 opportunity to add it to GDB's thread list. In case this is the
1773 first time we're noticing its corresponding inferior, add it to
1774 GDB's inferior list as well. */
1775
1776static void
1777remote_notice_new_inferior (ptid_t currthread, int running)
1778{
c906108c
SS
1779 /* If this is a new thread, add it to GDB's thread list.
1780 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1781
1782 if (in_thread_list (currthread) && is_exited (currthread))
1783 {
1784 /* We're seeing an event on a thread id we knew had exited.
1785 This has to be a new thread reusing the old id. Add it. */
1941c569 1786 remote_add_thread (currthread, running);
82f73884
PA
1787 return;
1788 }
1789
79d7f229 1790 if (!in_thread_list (currthread))
c0a2216e 1791 {
1941c569 1792 struct inferior *inf = NULL;
bad34192 1793 int pid = ptid_get_pid (currthread);
1941c569 1794
bad34192
PA
1795 if (ptid_is_pid (inferior_ptid)
1796 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1797 {
1798 /* inferior_ptid has no thread member yet. This can happen
1799 with the vAttach -> remote_wait,"TAAthread:" path if the
1800 stub doesn't support qC. This is the first stop reported
1801 after an attach, so this is the main thread. Update the
1802 ptid in the thread list. */
bad34192
PA
1803 if (in_thread_list (pid_to_ptid (pid)))
1804 thread_change_ptid (inferior_ptid, currthread);
1805 else
1806 {
1807 remote_add_thread (currthread, running);
1808 inferior_ptid = currthread;
1809 }
dc146f7c 1810 return;
c0a2216e 1811 }
82f73884
PA
1812
1813 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1814 {
1815 /* inferior_ptid is not set yet. This can happen with the
1816 vRun -> remote_wait,"TAAthread:" path if the stub
1817 doesn't support qC. This is the first stop reported
1818 after an attach, so this is the main thread. Update the
1819 ptid in the thread list. */
dc146f7c 1820 thread_change_ptid (inferior_ptid, currthread);
82f73884 1821 return;
c0a2216e 1822 }
82f73884 1823
29c87f7f
PA
1824 /* When connecting to a target remote, or to a target
1825 extended-remote which already was debugging an inferior, we
1826 may not know about it yet. Add it before adding its child
1827 thread, so notifications are emitted in a sensible order. */
1828 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1829 {
1830 struct remote_state *rs = get_remote_state ();
1831 int fake_pid_p = !remote_multi_process_p (rs);
1832
1833 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1834 ptid_get_pid (currthread), -1, 1);
49c62f2e 1835 }
29c87f7f 1836
82f73884 1837 /* This is really a new thread. Add it. */
1941c569
PA
1838 remote_add_thread (currthread, running);
1839
1840 /* If we found a new inferior, let the common code do whatever
1841 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1842 breakpoints), unless we're just setting up an all-stop
1843 connection. */
1941c569 1844 if (inf != NULL)
b7ea362b
PA
1845 {
1846 struct remote_state *rs = get_remote_state ();
1847
1848 if (non_stop || !rs->starting_up)
1849 notice_new_inferior (currthread, running, 0);
1850 }
c0a2216e 1851 }
c906108c
SS
1852}
1853
dc146f7c
VP
1854/* Return the private thread data, creating it if necessary. */
1855
70221824 1856static struct private_thread_info *
dc146f7c
VP
1857demand_private_info (ptid_t ptid)
1858{
1859 struct thread_info *info = find_thread_ptid (ptid);
1860
1861 gdb_assert (info);
1862
fe978cb0 1863 if (!info->priv)
dc146f7c 1864 {
8d749320 1865 info->priv = XNEW (struct private_thread_info);
dc146f7c 1866 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1867 info->priv->core = -1;
1868 info->priv->extra = 0;
dc146f7c
VP
1869 }
1870
fe978cb0 1871 return info->priv;
dc146f7c
VP
1872}
1873
74531fed
PA
1874/* Call this function as a result of
1875 1) A halt indication (T packet) containing a thread id
1876 2) A direct query of currthread
0df8b418 1877 3) Successful execution of set thread */
74531fed
PA
1878
1879static void
47f8a51d 1880record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1881{
47f8a51d 1882 rs->general_thread = currthread;
74531fed
PA
1883}
1884
89be2091
DJ
1885/* If 'QPassSignals' is supported, tell the remote stub what signals
1886 it can simply pass through to the inferior without reporting. */
1887
1888static void
94bedb42
TT
1889remote_pass_signals (struct target_ops *self,
1890 int numsigs, unsigned char *pass_signals)
89be2091 1891{
4082afcc 1892 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1893 {
1894 char *pass_packet, *p;
89be2091 1895 int count = 0, i;
747dc59d 1896 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1897
1898 gdb_assert (numsigs < 256);
1899 for (i = 0; i < numsigs; i++)
1900 {
2455069d 1901 if (pass_signals[i])
89be2091
DJ
1902 count++;
1903 }
1904 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1905 strcpy (pass_packet, "QPassSignals:");
1906 p = pass_packet + strlen (pass_packet);
1907 for (i = 0; i < numsigs; i++)
1908 {
2455069d 1909 if (pass_signals[i])
89be2091
DJ
1910 {
1911 if (i >= 16)
1912 *p++ = tohex (i >> 4);
1913 *p++ = tohex (i & 15);
1914 if (count)
1915 *p++ = ';';
1916 else
1917 break;
1918 count--;
1919 }
1920 }
1921 *p = 0;
747dc59d 1922 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1923 {
89be2091
DJ
1924 putpkt (pass_packet);
1925 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1926 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1927 if (rs->last_pass_packet)
1928 xfree (rs->last_pass_packet);
1929 rs->last_pass_packet = pass_packet;
89be2091
DJ
1930 }
1931 else
1932 xfree (pass_packet);
1933 }
1934}
1935
9b224c5e
PA
1936/* If 'QProgramSignals' is supported, tell the remote stub what
1937 signals it should pass through to the inferior when detaching. */
1938
1939static void
daf5e9b6
TT
1940remote_program_signals (struct target_ops *self,
1941 int numsigs, unsigned char *signals)
9b224c5e 1942{
4082afcc 1943 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1944 {
1945 char *packet, *p;
1946 int count = 0, i;
5e4a05c4 1947 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1948
1949 gdb_assert (numsigs < 256);
1950 for (i = 0; i < numsigs; i++)
1951 {
1952 if (signals[i])
1953 count++;
1954 }
1955 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1956 strcpy (packet, "QProgramSignals:");
1957 p = packet + strlen (packet);
1958 for (i = 0; i < numsigs; i++)
1959 {
1960 if (signal_pass_state (i))
1961 {
1962 if (i >= 16)
1963 *p++ = tohex (i >> 4);
1964 *p++ = tohex (i & 15);
1965 if (count)
1966 *p++ = ';';
1967 else
1968 break;
1969 count--;
1970 }
1971 }
1972 *p = 0;
5e4a05c4
TT
1973 if (!rs->last_program_signals_packet
1974 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1975 {
9b224c5e
PA
1976 putpkt (packet);
1977 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1978 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1979 xfree (rs->last_program_signals_packet);
1980 rs->last_program_signals_packet = packet;
9b224c5e
PA
1981 }
1982 else
1983 xfree (packet);
1984 }
1985}
1986
79d7f229
PA
1987/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1988 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1989 thread. If GEN is set, set the general thread, if not, then set
1990 the step/continue thread. */
c906108c 1991static void
79d7f229 1992set_thread (struct ptid ptid, int gen)
c906108c 1993{
d01949b6 1994 struct remote_state *rs = get_remote_state ();
47f8a51d 1995 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1996 char *buf = rs->buf;
79d7f229 1997 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1998
79d7f229 1999 if (ptid_equal (state, ptid))
c906108c
SS
2000 return;
2001
79d7f229
PA
2002 *buf++ = 'H';
2003 *buf++ = gen ? 'g' : 'c';
2004 if (ptid_equal (ptid, magic_null_ptid))
2005 xsnprintf (buf, endbuf - buf, "0");
2006 else if (ptid_equal (ptid, any_thread_ptid))
2007 xsnprintf (buf, endbuf - buf, "0");
2008 else if (ptid_equal (ptid, minus_one_ptid))
2009 xsnprintf (buf, endbuf - buf, "-1");
2010 else
82f73884 2011 write_ptid (buf, endbuf, ptid);
79d7f229 2012 putpkt (rs->buf);
6d820c5c 2013 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2014 if (gen)
47f8a51d 2015 rs->general_thread = ptid;
c906108c 2016 else
47f8a51d 2017 rs->continue_thread = ptid;
c906108c 2018}
79d7f229
PA
2019
2020static void
2021set_general_thread (struct ptid ptid)
2022{
2023 set_thread (ptid, 1);
2024}
2025
2026static void
2027set_continue_thread (struct ptid ptid)
2028{
2029 set_thread (ptid, 0);
2030}
2031
3c9c4b83
PA
2032/* Change the remote current process. Which thread within the process
2033 ends up selected isn't important, as long as it is the same process
2034 as what INFERIOR_PTID points to.
2035
2036 This comes from that fact that there is no explicit notion of
2037 "selected process" in the protocol. The selected process for
2038 general operations is the process the selected general thread
2039 belongs to. */
2040
2041static void
2042set_general_process (void)
2043{
2044 struct remote_state *rs = get_remote_state ();
2045
2046 /* If the remote can't handle multiple processes, don't bother. */
901f9912 2047 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
2048 return;
2049
2050 /* We only need to change the remote current thread if it's pointing
2051 at some other process. */
47f8a51d 2052 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
2053 set_general_thread (inferior_ptid);
2054}
2055
c906108c 2056\f
7d1a114c
PA
2057/* Return nonzero if this is the main thread that we made up ourselves
2058 to model non-threaded targets as single-threaded. */
c906108c
SS
2059
2060static int
7d1a114c 2061remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 2062{
6d820c5c 2063 struct remote_state *rs = get_remote_state ();
82f73884 2064 char *p, *endp;
c906108c 2065
c0a2216e
PA
2066 if (ptid_equal (ptid, magic_null_ptid))
2067 /* The main thread is always alive. */
2068 return 1;
2069
ba348170 2070 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
2071 /* The main thread is always alive. This can happen after a
2072 vAttach, if the remote side doesn't support
2073 multi-threading. */
2074 return 1;
2075
7d1a114c
PA
2076 return 0;
2077}
2078
2079/* Return nonzero if the thread PTID is still alive on the remote
2080 system. */
2081
2082static int
2083remote_thread_alive (struct target_ops *ops, ptid_t ptid)
2084{
2085 struct remote_state *rs = get_remote_state ();
2086 char *p, *endp;
2087
2088 /* Check if this is a thread that we made up ourselves to model
2089 non-threaded targets as single-threaded. */
2090 if (remote_thread_always_alive (ops, ptid))
2091 return 1;
2092
82f73884
PA
2093 p = rs->buf;
2094 endp = rs->buf + get_remote_packet_size ();
2095
2096 *p++ = 'T';
2097 write_ptid (p, endp, ptid);
2098
2e9f7625 2099 putpkt (rs->buf);
6d820c5c 2100 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2101 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2102}
2103
2104/* About these extended threadlist and threadinfo packets. They are
2105 variable length packets but, the fields within them are often fixed
2106 length. They are redundent enough to send over UDP as is the
2107 remote protocol in general. There is a matching unit test module
2108 in libstub. */
2109
23860348 2110/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2111 libstub protocol encoding, and remote.c. It is not particularly
23860348 2112 changable. */
cce74817
JM
2113
2114/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2115 Plan to fix this. */
cce74817 2116
23860348 2117typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2118
9d1f7ab2 2119/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2120 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2121
2122struct gdb_ext_thread_info
c5aa993b 2123 {
23860348 2124 threadref threadid; /* External form of thread reference. */
2bc416ba 2125 int active; /* Has state interesting to GDB?
23860348 2126 regs, stack. */
2bc416ba 2127 char display[256]; /* Brief state display, name,
cedea757 2128 blocked/suspended. */
23860348 2129 char shortname[32]; /* To be used to name threads. */
2bc416ba 2130 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2131 whatever. */
c5aa993b 2132 };
cce74817
JM
2133
2134/* The volume of remote transfers can be limited by submitting
2135 a mask containing bits specifying the desired information.
2136 Use a union of these values as the 'selection' parameter to
0df8b418 2137 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2138
2139#define TAG_THREADID 1
2140#define TAG_EXISTS 2
2141#define TAG_DISPLAY 4
2142#define TAG_THREADNAME 8
c5aa993b 2143#define TAG_MOREDISPLAY 16
cce74817 2144
23860348 2145#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2146
a14ed312 2147static char *unpack_nibble (char *buf, int *val);
cce74817 2148
a14ed312 2149static char *unpack_byte (char *buf, int *value);
cce74817 2150
a14ed312 2151static char *pack_int (char *buf, int value);
cce74817 2152
a14ed312 2153static char *unpack_int (char *buf, int *value);
cce74817 2154
a14ed312 2155static char *unpack_string (char *src, char *dest, int length);
cce74817 2156
23860348 2157static char *pack_threadid (char *pkt, threadref *id);
cce74817 2158
23860348 2159static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2160
23860348 2161void int_to_threadref (threadref *id, int value);
cce74817 2162
23860348 2163static int threadref_to_int (threadref *ref);
cce74817 2164
23860348 2165static void copy_threadref (threadref *dest, threadref *src);
cce74817 2166
23860348 2167static int threadmatch (threadref *dest, threadref *src);
cce74817 2168
2bc416ba 2169static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2170 threadref *id);
cce74817 2171
a14ed312 2172static int remote_unpack_thread_info_response (char *pkt,
23860348 2173 threadref *expectedref,
a14ed312
KB
2174 struct gdb_ext_thread_info
2175 *info);
cce74817
JM
2176
2177
2bc416ba 2178static int remote_get_threadinfo (threadref *threadid,
23860348 2179 int fieldset, /*TAG mask */
a14ed312 2180 struct gdb_ext_thread_info *info);
cce74817 2181
a14ed312
KB
2182static char *pack_threadlist_request (char *pkt, int startflag,
2183 int threadcount,
23860348 2184 threadref *nextthread);
cce74817 2185
a14ed312
KB
2186static int parse_threadlist_response (char *pkt,
2187 int result_limit,
23860348 2188 threadref *original_echo,
2bc416ba 2189 threadref *resultlist,
23860348 2190 int *doneflag);
cce74817 2191
a14ed312 2192static int remote_get_threadlist (int startflag,
23860348 2193 threadref *nextthread,
a14ed312
KB
2194 int result_limit,
2195 int *done,
2bc416ba 2196 int *result_count,
23860348 2197 threadref *threadlist);
cce74817 2198
23860348 2199typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2200
a14ed312
KB
2201static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2202 void *context, int looplimit);
cce74817 2203
23860348 2204static int remote_newthread_step (threadref *ref, void *context);
cce74817 2205
82f73884
PA
2206
2207/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2208 buffer we're allowed to write to. Returns
2209 BUF+CHARACTERS_WRITTEN. */
2210
2211static char *
2212write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2213{
2214 int pid, tid;
2215 struct remote_state *rs = get_remote_state ();
2216
2217 if (remote_multi_process_p (rs))
2218 {
2219 pid = ptid_get_pid (ptid);
2220 if (pid < 0)
2221 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2222 else
2223 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2224 }
ba348170 2225 tid = ptid_get_lwp (ptid);
82f73884
PA
2226 if (tid < 0)
2227 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2228 else
2229 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2230
2231 return buf;
2232}
2233
2234/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2235 passed the last parsed char. Returns null_ptid on error. */
2236
2237static ptid_t
2238read_ptid (char *buf, char **obuf)
2239{
2240 char *p = buf;
2241 char *pp;
2242 ULONGEST pid = 0, tid = 0;
82f73884
PA
2243
2244 if (*p == 'p')
2245 {
2246 /* Multi-process ptid. */
2247 pp = unpack_varlen_hex (p + 1, &pid);
2248 if (*pp != '.')
b37520b6 2249 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2250
2251 p = pp;
2252 pp = unpack_varlen_hex (p + 1, &tid);
2253 if (obuf)
2254 *obuf = pp;
ba348170 2255 return ptid_build (pid, tid, 0);
82f73884
PA
2256 }
2257
2258 /* No multi-process. Just a tid. */
2259 pp = unpack_varlen_hex (p, &tid);
2260
c9f35b34
KB
2261 /* Return null_ptid when no thread id is found. */
2262 if (p == pp)
2263 {
2264 if (obuf)
2265 *obuf = pp;
2266 return null_ptid;
2267 }
2268
82f73884 2269 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2270 what's in inferior_ptid, unless it's null at this point. If so,
2271 then since there's no way to know the pid of the reported
2272 threads, use the magic number. */
2273 if (ptid_equal (inferior_ptid, null_ptid))
2274 pid = ptid_get_pid (magic_null_ptid);
2275 else
2276 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2277
2278 if (obuf)
2279 *obuf = pp;
ba348170 2280 return ptid_build (pid, tid, 0);
82f73884
PA
2281}
2282
c906108c 2283static int
fba45db2 2284stubhex (int ch)
c906108c
SS
2285{
2286 if (ch >= 'a' && ch <= 'f')
2287 return ch - 'a' + 10;
2288 if (ch >= '0' && ch <= '9')
2289 return ch - '0';
2290 if (ch >= 'A' && ch <= 'F')
2291 return ch - 'A' + 10;
2292 return -1;
2293}
2294
2295static int
fba45db2 2296stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2297{
2298 int nibble;
2299 int retval = 0;
2300
2301 while (fieldlength)
2302 {
2303 nibble = stubhex (*buff++);
2304 retval |= nibble;
2305 fieldlength--;
2306 if (fieldlength)
2307 retval = retval << 4;
2308 }
2309 return retval;
2310}
2311
c906108c 2312static char *
fba45db2 2313unpack_nibble (char *buf, int *val)
c906108c 2314{
b7589f7d 2315 *val = fromhex (*buf++);
c906108c
SS
2316 return buf;
2317}
2318
c906108c 2319static char *
fba45db2 2320unpack_byte (char *buf, int *value)
c906108c
SS
2321{
2322 *value = stub_unpack_int (buf, 2);
2323 return buf + 2;
2324}
2325
2326static char *
fba45db2 2327pack_int (char *buf, int value)
c906108c
SS
2328{
2329 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2330 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2331 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2332 buf = pack_hex_byte (buf, (value & 0xff));
2333 return buf;
2334}
2335
2336static char *
fba45db2 2337unpack_int (char *buf, int *value)
c906108c
SS
2338{
2339 *value = stub_unpack_int (buf, 8);
2340 return buf + 8;
2341}
2342
23860348 2343#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2344static char *pack_string (char *pkt, char *string);
c906108c
SS
2345
2346static char *
fba45db2 2347pack_string (char *pkt, char *string)
c906108c
SS
2348{
2349 char ch;
2350 int len;
2351
2352 len = strlen (string);
2353 if (len > 200)
23860348 2354 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2355 pkt = pack_hex_byte (pkt, len);
2356 while (len-- > 0)
2357 {
2358 ch = *string++;
2359 if ((ch == '\0') || (ch == '#'))
23860348 2360 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2361 *pkt++ = ch;
2362 }
2363 return pkt;
2364}
2365#endif /* 0 (unused) */
2366
2367static char *
fba45db2 2368unpack_string (char *src, char *dest, int length)
c906108c
SS
2369{
2370 while (length--)
2371 *dest++ = *src++;
2372 *dest = '\0';
2373 return src;
2374}
2375
2376static char *
fba45db2 2377pack_threadid (char *pkt, threadref *id)
c906108c
SS
2378{
2379 char *limit;
2380 unsigned char *altid;
2381
2382 altid = (unsigned char *) id;
2383 limit = pkt + BUF_THREAD_ID_SIZE;
2384 while (pkt < limit)
2385 pkt = pack_hex_byte (pkt, *altid++);
2386 return pkt;
2387}
2388
2389
2390static char *
fba45db2 2391unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2392{
2393 char *altref;
2394 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2395 int x, y;
2396
2397 altref = (char *) id;
2398
2399 while (inbuf < limit)
2400 {
2401 x = stubhex (*inbuf++);
2402 y = stubhex (*inbuf++);
2403 *altref++ = (x << 4) | y;
2404 }
2405 return inbuf;
2406}
2407
2408/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2409 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2410 to use 64bit thread references internally. This is an adapter
2411 function. */
2412
2413void
fba45db2 2414int_to_threadref (threadref *id, int value)
c906108c
SS
2415{
2416 unsigned char *scan;
2417
2418 scan = (unsigned char *) id;
2419 {
2420 int i = 4;
2421 while (i--)
2422 *scan++ = 0;
2423 }
2424 *scan++ = (value >> 24) & 0xff;
2425 *scan++ = (value >> 16) & 0xff;
2426 *scan++ = (value >> 8) & 0xff;
2427 *scan++ = (value & 0xff);
2428}
2429
2430static int
fba45db2 2431threadref_to_int (threadref *ref)
c906108c
SS
2432{
2433 int i, value = 0;
2434 unsigned char *scan;
2435
cfd77fa1 2436 scan = *ref;
c906108c
SS
2437 scan += 4;
2438 i = 4;
2439 while (i-- > 0)
2440 value = (value << 8) | ((*scan++) & 0xff);
2441 return value;
2442}
2443
2444static void
fba45db2 2445copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2446{
2447 int i;
2448 unsigned char *csrc, *cdest;
2449
2450 csrc = (unsigned char *) src;
2451 cdest = (unsigned char *) dest;
2452 i = 8;
2453 while (i--)
2454 *cdest++ = *csrc++;
2455}
2456
2457static int
fba45db2 2458threadmatch (threadref *dest, threadref *src)
c906108c 2459{
23860348 2460 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2461#if 0
2462 unsigned char *srcp, *destp;
2463 int i, result;
2464 srcp = (char *) src;
2465 destp = (char *) dest;
2466
2467 result = 1;
2468 while (i-- > 0)
2469 result &= (*srcp++ == *destp++) ? 1 : 0;
2470 return result;
2471#endif
2472 return 1;
2473}
2474
2475/*
c5aa993b
JM
2476 threadid:1, # always request threadid
2477 context_exists:2,
2478 display:4,
2479 unique_name:8,
2480 more_display:16
2481 */
c906108c
SS
2482
2483/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2484
2485static char *
fba45db2 2486pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2487{
23860348
MS
2488 *pkt++ = 'q'; /* Info Query */
2489 *pkt++ = 'P'; /* process or thread info */
2490 pkt = pack_int (pkt, mode); /* mode */
c906108c 2491 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2492 *pkt = '\0'; /* terminate */
c906108c
SS
2493 return pkt;
2494}
2495
23860348 2496/* These values tag the fields in a thread info response packet. */
c906108c 2497/* Tagging the fields allows us to request specific fields and to
23860348 2498 add more fields as time goes by. */
c906108c 2499
23860348 2500#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2501#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2502 fetch registers and its stack? */
c5aa993b 2503#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2504#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2505#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2506 the process. */
c906108c
SS
2507
2508static int
fba45db2
KB
2509remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2510 struct gdb_ext_thread_info *info)
c906108c 2511{
d01949b6 2512 struct remote_state *rs = get_remote_state ();
c906108c 2513 int mask, length;
cfd77fa1 2514 int tag;
c906108c 2515 threadref ref;
6d820c5c 2516 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2517 int retval = 1;
2518
23860348 2519 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2520 info->active = 0;
2521 info->display[0] = '\0';
2522 info->shortname[0] = '\0';
2523 info->more_display[0] = '\0';
2524
23860348
MS
2525 /* Assume the characters indicating the packet type have been
2526 stripped. */
c906108c
SS
2527 pkt = unpack_int (pkt, &mask); /* arg mask */
2528 pkt = unpack_threadid (pkt, &ref);
2529
2530 if (mask == 0)
8a3fe4f8 2531 warning (_("Incomplete response to threadinfo request."));
c906108c 2532 if (!threadmatch (&ref, expectedref))
23860348 2533 { /* This is an answer to a different request. */
8a3fe4f8 2534 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2535 return 0;
2536 }
2537 copy_threadref (&info->threadid, &ref);
2538
23860348 2539 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2540
23860348
MS
2541 /* Packets are terminated with nulls. */
2542 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2543 {
2544 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2545 pkt = unpack_byte (pkt, &length); /* length */
2546 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2547 {
8a3fe4f8 2548 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2549 retval = 0;
2550 break;
2551 }
2552 if (tag == TAG_THREADID)
2553 {
2554 if (length != 16)
2555 {
8a3fe4f8 2556 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2557 retval = 0;
2558 break;
2559 }
2560 pkt = unpack_threadid (pkt, &ref);
2561 mask = mask & ~TAG_THREADID;
2562 continue;
2563 }
2564 if (tag == TAG_EXISTS)
2565 {
2566 info->active = stub_unpack_int (pkt, length);
2567 pkt += length;
2568 mask = mask & ~(TAG_EXISTS);
2569 if (length > 8)
2570 {
8a3fe4f8 2571 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2572 retval = 0;
2573 break;
2574 }
2575 continue;
2576 }
2577 if (tag == TAG_THREADNAME)
2578 {
2579 pkt = unpack_string (pkt, &info->shortname[0], length);
2580 mask = mask & ~TAG_THREADNAME;
2581 continue;
2582 }
2583 if (tag == TAG_DISPLAY)
2584 {
2585 pkt = unpack_string (pkt, &info->display[0], length);
2586 mask = mask & ~TAG_DISPLAY;
2587 continue;
2588 }
2589 if (tag == TAG_MOREDISPLAY)
2590 {
2591 pkt = unpack_string (pkt, &info->more_display[0], length);
2592 mask = mask & ~TAG_MOREDISPLAY;
2593 continue;
2594 }
8a3fe4f8 2595 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2596 break; /* Not a tag we know about. */
c906108c
SS
2597 }
2598 return retval;
2599}
2600
2601static int
fba45db2
KB
2602remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2603 struct gdb_ext_thread_info *info)
c906108c 2604{
d01949b6 2605 struct remote_state *rs = get_remote_state ();
c906108c 2606 int result;
c906108c 2607
2e9f7625
DJ
2608 pack_threadinfo_request (rs->buf, fieldset, threadid);
2609 putpkt (rs->buf);
6d820c5c 2610 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2611
2612 if (rs->buf[0] == '\0')
2613 return 0;
2614
2e9f7625 2615 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2616 threadid, info);
c906108c
SS
2617 return result;
2618}
2619
c906108c
SS
2620/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2621
2622static char *
fba45db2
KB
2623pack_threadlist_request (char *pkt, int startflag, int threadcount,
2624 threadref *nextthread)
c906108c
SS
2625{
2626 *pkt++ = 'q'; /* info query packet */
2627 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2628 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2629 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2630 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2631 *pkt = '\0';
2632 return pkt;
2633}
2634
2635/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2636
2637static int
fba45db2
KB
2638parse_threadlist_response (char *pkt, int result_limit,
2639 threadref *original_echo, threadref *resultlist,
2640 int *doneflag)
c906108c 2641{
d01949b6 2642 struct remote_state *rs = get_remote_state ();
c906108c
SS
2643 char *limit;
2644 int count, resultcount, done;
2645
2646 resultcount = 0;
2647 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2648 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2649 /* done parse past here */
c906108c
SS
2650 pkt = unpack_byte (pkt, &count); /* count field */
2651 pkt = unpack_nibble (pkt, &done);
2652 /* The first threadid is the argument threadid. */
2653 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2654 while ((count-- > 0) && (pkt < limit))
2655 {
2656 pkt = unpack_threadid (pkt, resultlist++);
2657 if (resultcount++ >= result_limit)
2658 break;
2659 }
2660 if (doneflag)
2661 *doneflag = done;
2662 return resultcount;
2663}
2664
6dc54d91
PA
2665/* Fetch the next batch of threads from the remote. Returns -1 if the
2666 qL packet is not supported, 0 on error and 1 on success. */
2667
c906108c 2668static int
fba45db2
KB
2669remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2670 int *done, int *result_count, threadref *threadlist)
c906108c 2671{
d01949b6 2672 struct remote_state *rs = get_remote_state ();
c906108c
SS
2673 int result = 1;
2674
23860348 2675 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2676 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2677 >= get_remote_packet_size ())
ea9c271d 2678 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2679
6d820c5c
DJ
2680 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2681 putpkt (rs->buf);
2682 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2683 if (*rs->buf == '\0')
6dc54d91
PA
2684 {
2685 /* Packet not supported. */
2686 return -1;
2687 }
2688
2689 *result_count =
2690 parse_threadlist_response (rs->buf + 2, result_limit,
2691 &rs->echo_nextthread, threadlist, done);
c906108c 2692
0d031856 2693 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2694 {
23860348
MS
2695 /* FIXME: This is a good reason to drop the packet. */
2696 /* Possably, there is a duplicate response. */
c906108c
SS
2697 /* Possabilities :
2698 retransmit immediatly - race conditions
2699 retransmit after timeout - yes
2700 exit
2701 wait for packet, then exit
2702 */
8a3fe4f8 2703 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2704 return 0; /* I choose simply exiting. */
c906108c
SS
2705 }
2706 if (*result_count <= 0)
2707 {
2708 if (*done != 1)
2709 {
8a3fe4f8 2710 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2711 result = 0;
2712 }
2713 return result; /* break; */
2714 }
2715 if (*result_count > result_limit)
2716 {
2717 *result_count = 0;
8a3fe4f8 2718 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2719 return 0;
2720 }
2721 return result;
2722}
2723
6dc54d91
PA
2724/* Fetch the list of remote threads, with the qL packet, and call
2725 STEPFUNCTION for each thread found. Stops iterating and returns 1
2726 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2727 STEPFUNCTION returns false. If the packet is not supported,
2728 returns -1. */
c906108c 2729
c906108c 2730static int
fba45db2
KB
2731remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2732 int looplimit)
c906108c 2733{
0d031856 2734 struct remote_state *rs = get_remote_state ();
c906108c
SS
2735 int done, i, result_count;
2736 int startflag = 1;
2737 int result = 1;
2738 int loopcount = 0;
c906108c
SS
2739
2740 done = 0;
2741 while (!done)
2742 {
2743 if (loopcount++ > looplimit)
2744 {
2745 result = 0;
8a3fe4f8 2746 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2747 break;
2748 }
6dc54d91
PA
2749 result = remote_get_threadlist (startflag, &rs->nextthread,
2750 MAXTHREADLISTRESULTS,
2751 &done, &result_count,
2752 rs->resultthreadlist);
2753 if (result <= 0)
2754 break;
23860348 2755 /* Clear for later iterations. */
c906108c
SS
2756 startflag = 0;
2757 /* Setup to resume next batch of thread references, set nextthread. */
2758 if (result_count >= 1)
0d031856
TT
2759 copy_threadref (&rs->nextthread,
2760 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2761 i = 0;
2762 while (result_count--)
6dc54d91
PA
2763 {
2764 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2765 {
2766 result = 0;
2767 break;
2768 }
2769 }
c906108c
SS
2770 }
2771 return result;
2772}
2773
6dc54d91
PA
2774/* A thread found on the remote target. */
2775
2776typedef struct thread_item
2777{
2778 /* The thread's PTID. */
2779 ptid_t ptid;
2780
2781 /* The thread's extra info. May be NULL. */
2782 char *extra;
2783
2784 /* The core the thread was running on. -1 if not known. */
2785 int core;
2786} thread_item_t;
2787DEF_VEC_O(thread_item_t);
2788
2789/* Context passed around to the various methods listing remote
2790 threads. As new threads are found, they're added to the ITEMS
2791 vector. */
2792
2793struct threads_listing_context
2794{
2795 /* The threads found on the remote target. */
2796 VEC (thread_item_t) *items;
2797};
2798
80134cf5
PA
2799/* Discard the contents of the constructed thread listing context. */
2800
2801static void
2802clear_threads_listing_context (void *p)
2803{
2804 struct threads_listing_context *context = p;
2805 int i;
2806 struct thread_item *item;
2807
2808 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2809 xfree (item->extra);
2810
2811 VEC_free (thread_item_t, context->items);
2812}
2813
cbb8991c
DB
2814/* Remove the thread specified as the related_pid field of WS
2815 from the CONTEXT list. */
2816
2817static void
2818threads_listing_context_remove (struct target_waitstatus *ws,
2819 struct threads_listing_context *context)
2820{
2821 struct thread_item *item;
2822 int i;
2823 ptid_t child_ptid = ws->value.related_pid;
2824
2825 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2826 {
2827 if (ptid_equal (item->ptid, child_ptid))
2828 {
2829 VEC_ordered_remove (thread_item_t, context->items, i);
2830 break;
2831 }
2832 }
2833}
2834
c906108c 2835static int
6dc54d91 2836remote_newthread_step (threadref *ref, void *data)
c906108c 2837{
6dc54d91
PA
2838 struct threads_listing_context *context = data;
2839 struct thread_item item;
79d7f229 2840 int pid = ptid_get_pid (inferior_ptid);
39f77062 2841
6dc54d91
PA
2842 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2843 item.core = -1;
2844 item.extra = NULL;
2845
2846 VEC_safe_push (thread_item_t, context->items, &item);
2847
c906108c
SS
2848 return 1; /* continue iterator */
2849}
2850
2851#define CRAZY_MAX_THREADS 1000
2852
39f77062
KB
2853static ptid_t
2854remote_current_thread (ptid_t oldpid)
c906108c 2855{
d01949b6 2856 struct remote_state *rs = get_remote_state ();
c906108c
SS
2857
2858 putpkt ("qC");
6d820c5c 2859 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2860 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34
KB
2861 {
2862 char *obuf;
2863 ptid_t result;
2864
2865 result = read_ptid (&rs->buf[2], &obuf);
2866 if (*obuf != '\0' && remote_debug)
2867 fprintf_unfiltered (gdb_stdlog,
2868 "warning: garbage in qC reply\n");
2869
2870 return result;
2871 }
c906108c
SS
2872 else
2873 return oldpid;
2874}
2875
6dc54d91 2876/* List remote threads using the deprecated qL packet. */
cce74817 2877
6dc54d91
PA
2878static int
2879remote_get_threads_with_ql (struct target_ops *ops,
2880 struct threads_listing_context *context)
c906108c 2881{
6dc54d91
PA
2882 if (remote_threadlist_iterator (remote_newthread_step, context,
2883 CRAZY_MAX_THREADS) >= 0)
2884 return 1;
2885
2886 return 0;
c906108c
SS
2887}
2888
dc146f7c
VP
2889#if defined(HAVE_LIBEXPAT)
2890
dc146f7c
VP
2891static void
2892start_thread (struct gdb_xml_parser *parser,
2893 const struct gdb_xml_element *element,
2894 void *user_data, VEC(gdb_xml_value_s) *attributes)
2895{
6dc54d91 2896 struct threads_listing_context *data = user_data;
dc146f7c
VP
2897
2898 struct thread_item item;
2899 char *id;
3d2c1d41 2900 struct gdb_xml_value *attr;
dc146f7c 2901
3d2c1d41 2902 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2903 item.ptid = read_ptid (id, NULL);
2904
3d2c1d41
PA
2905 attr = xml_find_attribute (attributes, "core");
2906 if (attr != NULL)
2907 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2908 else
2909 item.core = -1;
2910
2911 item.extra = 0;
2912
2913 VEC_safe_push (thread_item_t, data->items, &item);
2914}
2915
2916static void
2917end_thread (struct gdb_xml_parser *parser,
2918 const struct gdb_xml_element *element,
2919 void *user_data, const char *body_text)
2920{
6dc54d91 2921 struct threads_listing_context *data = user_data;
dc146f7c
VP
2922
2923 if (body_text && *body_text)
2ae2a0b7 2924 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2925}
2926
2927const struct gdb_xml_attribute thread_attributes[] = {
2928 { "id", GDB_XML_AF_NONE, NULL, NULL },
2929 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2930 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2931};
2932
2933const struct gdb_xml_element thread_children[] = {
2934 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2935};
2936
2937const struct gdb_xml_element threads_children[] = {
2938 { "thread", thread_attributes, thread_children,
2939 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2940 start_thread, end_thread },
2941 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2942};
2943
2944const struct gdb_xml_element threads_elements[] = {
2945 { "threads", NULL, threads_children,
2946 GDB_XML_EF_NONE, NULL, NULL },
2947 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2948};
2949
2950#endif
2951
6dc54d91 2952/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2953
6dc54d91
PA
2954static int
2955remote_get_threads_with_qxfer (struct target_ops *ops,
2956 struct threads_listing_context *context)
0f71a2f6 2957{
dc146f7c 2958#if defined(HAVE_LIBEXPAT)
4082afcc 2959 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2960 {
6dc54d91 2961 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2962 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2963
6dc54d91 2964 if (xml != NULL && *xml != '\0')
dc146f7c 2965 {
6dc54d91
PA
2966 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2967 threads_elements, xml, context);
dc146f7c
VP
2968 }
2969
2970 do_cleanups (back_to);
6dc54d91 2971 return 1;
dc146f7c
VP
2972 }
2973#endif
2974
6dc54d91
PA
2975 return 0;
2976}
2977
2978/* List remote threads using qfThreadInfo/qsThreadInfo. */
2979
2980static int
2981remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2982 struct threads_listing_context *context)
2983{
2984 struct remote_state *rs = get_remote_state ();
2985
b80fafe3 2986 if (rs->use_threadinfo_query)
9d1f7ab2 2987 {
6dc54d91
PA
2988 char *bufp;
2989
9d1f7ab2 2990 putpkt ("qfThreadInfo");
6d820c5c 2991 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2992 bufp = rs->buf;
9d1f7ab2 2993 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2994 {
9d1f7ab2
MS
2995 while (*bufp++ == 'm') /* reply contains one or more TID */
2996 {
2997 do
2998 {
6dc54d91
PA
2999 struct thread_item item;
3000
3001 item.ptid = read_ptid (bufp, &bufp);
3002 item.core = -1;
3003 item.extra = NULL;
3004
3005 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
3006 }
3007 while (*bufp++ == ','); /* comma-separated list */
3008 putpkt ("qsThreadInfo");
6d820c5c 3009 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 3010 bufp = rs->buf;
9d1f7ab2 3011 }
6dc54d91
PA
3012 return 1;
3013 }
3014 else
3015 {
3016 /* Packet not recognized. */
3017 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
3018 }
3019 }
3020
6dc54d91
PA
3021 return 0;
3022}
3023
e8032dde 3024/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
3025 targets. */
3026
3027static void
e8032dde 3028remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
3029{
3030 struct remote_state *rs = get_remote_state ();
3031 struct threads_listing_context context;
3032 struct cleanup *old_chain;
ab970af1 3033 int got_list = 0;
e8032dde 3034
6dc54d91
PA
3035 context.items = NULL;
3036 old_chain = make_cleanup (clear_threads_listing_context, &context);
3037
3038 /* We have a few different mechanisms to fetch the thread list. Try
3039 them all, starting with the most preferred one first, falling
3040 back to older methods. */
3041 if (remote_get_threads_with_qxfer (ops, &context)
3042 || remote_get_threads_with_qthreadinfo (ops, &context)
3043 || remote_get_threads_with_ql (ops, &context))
3044 {
3045 int i;
3046 struct thread_item *item;
ab970af1
PA
3047 struct thread_info *tp, *tmp;
3048
3049 got_list = 1;
3050
7d1a114c
PA
3051 if (VEC_empty (thread_item_t, context.items)
3052 && remote_thread_always_alive (ops, inferior_ptid))
3053 {
3054 /* Some targets don't really support threads, but still
3055 reply an (empty) thread list in response to the thread
3056 listing packets, instead of replying "packet not
3057 supported". Exit early so we don't delete the main
3058 thread. */
3059 do_cleanups (old_chain);
3060 return;
3061 }
3062
ab970af1
PA
3063 /* CONTEXT now holds the current thread list on the remote
3064 target end. Delete GDB-side threads no longer found on the
3065 target. */
8a06aea7 3066 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 3067 {
ab970af1
PA
3068 for (i = 0;
3069 VEC_iterate (thread_item_t, context.items, i, item);
3070 ++i)
3071 {
3072 if (ptid_equal (item->ptid, tp->ptid))
3073 break;
3074 }
3075
3076 if (i == VEC_length (thread_item_t, context.items))
3077 {
3078 /* Not found. */
3079 delete_thread (tp->ptid);
3080 }
cbb8991c
DB
3081 }
3082
3083 /* Remove any unreported fork child threads from CONTEXT so
3084 that we don't interfere with follow fork, which is where
3085 creation of such threads is handled. */
3086 remove_new_fork_children (&context);
74531fed 3087
ab970af1 3088 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
3089 for (i = 0;
3090 VEC_iterate (thread_item_t, context.items, i, item);
3091 ++i)
3092 {
3093 if (!ptid_equal (item->ptid, null_ptid))
3094 {
3095 struct private_thread_info *info;
3096 /* In non-stop mode, we assume new found threads are
3097 running until proven otherwise with a stop reply. In
3098 all-stop, we can only get here if all threads are
3099 stopped. */
3100 int running = non_stop ? 1 : 0;
3101
3102 remote_notice_new_inferior (item->ptid, running);
3103
3104 info = demand_private_info (item->ptid);
3105 info->core = item->core;
3106 info->extra = item->extra;
3107 item->extra = NULL;
3108 }
3109 }
3110 }
3111
ab970af1
PA
3112 if (!got_list)
3113 {
3114 /* If no thread listing method is supported, then query whether
3115 each known thread is alive, one by one, with the T packet.
3116 If the target doesn't support threads at all, then this is a
3117 no-op. See remote_thread_alive. */
3118 prune_threads ();
3119 }
3120
6dc54d91 3121 do_cleanups (old_chain);
9d1f7ab2
MS
3122}
3123
802188a7 3124/*
9d1f7ab2
MS
3125 * Collect a descriptive string about the given thread.
3126 * The target may say anything it wants to about the thread
3127 * (typically info about its blocked / runnable state, name, etc.).
3128 * This string will appear in the info threads display.
802188a7 3129 *
9d1f7ab2
MS
3130 * Optional: targets are not required to implement this function.
3131 */
3132
3133static char *
c15906d8 3134remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3135{
d01949b6 3136 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3137 int result;
3138 int set;
3139 threadref id;
3140 struct gdb_ext_thread_info threadinfo;
23860348 3141 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3142 int n = 0; /* position in display_buf */
3143
5d93a237 3144 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3145 internal_error (__FILE__, __LINE__,
e2e0b3e5 3146 _("remote_threads_extra_info"));
9d1f7ab2 3147
60e569b9 3148 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3149 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3150 /* This is the main thread which was added by GDB. The remote
3151 server doesn't know about it. */
3152 return NULL;
3153
4082afcc 3154 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3155 {
3156 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3157
fe978cb0
PA
3158 if (info && info->priv)
3159 return info->priv->extra;
dc146f7c
VP
3160 else
3161 return NULL;
3162 }
3163
b80fafe3 3164 if (rs->use_threadextra_query)
9d1f7ab2 3165 {
82f73884
PA
3166 char *b = rs->buf;
3167 char *endb = rs->buf + get_remote_packet_size ();
3168
3169 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3170 b += strlen (b);
3171 write_ptid (b, endb, tp->ptid);
3172
2e9f7625 3173 putpkt (rs->buf);
6d820c5c 3174 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3175 if (rs->buf[0] != 0)
9d1f7ab2 3176 {
2e9f7625
DJ
3177 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3178 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3179 display_buf [result] = '\0';
9d1f7ab2
MS
3180 return display_buf;
3181 }
0f71a2f6 3182 }
9d1f7ab2
MS
3183
3184 /* If the above query fails, fall back to the old method. */
b80fafe3 3185 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3186 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3187 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3188 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3189 if (remote_get_threadinfo (&id, set, &threadinfo))
3190 if (threadinfo.active)
0f71a2f6 3191 {
9d1f7ab2 3192 if (*threadinfo.shortname)
2bc416ba 3193 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3194 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3195 if (*threadinfo.display)
2bc416ba 3196 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3197 " State: %s,", threadinfo.display);
9d1f7ab2 3198 if (*threadinfo.more_display)
2bc416ba 3199 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3200 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3201
3202 if (n > 0)
c5aa993b 3203 {
23860348 3204 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3205 if (',' == display_buf[n-1])
3206 display_buf[n-1] = ' ';
3207 return display_buf;
c5aa993b 3208 }
0f71a2f6 3209 }
9d1f7ab2 3210 return NULL;
0f71a2f6 3211}
c906108c 3212\f
c5aa993b 3213
0fb4aa4b 3214static int
61fc905d 3215remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3216 struct static_tracepoint_marker *marker)
3217{
3218 struct remote_state *rs = get_remote_state ();
3219 char *p = rs->buf;
3220
bba74b36 3221 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3222 p += strlen (p);
3223 p += hexnumstr (p, addr);
3224 putpkt (rs->buf);
3225 getpkt (&rs->buf, &rs->buf_size, 0);
3226 p = rs->buf;
3227
3228 if (*p == 'E')
3229 error (_("Remote failure reply: %s"), p);
3230
3231 if (*p++ == 'm')
3232 {
3233 parse_static_tracepoint_marker_definition (p, &p, marker);
3234 return 1;
3235 }
3236
3237 return 0;
3238}
3239
0fb4aa4b 3240static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3241remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3242 const char *strid)
0fb4aa4b
PA
3243{
3244 struct remote_state *rs = get_remote_state ();
3245 VEC(static_tracepoint_marker_p) *markers = NULL;
3246 struct static_tracepoint_marker *marker = NULL;
3247 struct cleanup *old_chain;
3248 char *p;
3249
3250 /* Ask for a first packet of static tracepoint marker
3251 definition. */
3252 putpkt ("qTfSTM");
3253 getpkt (&rs->buf, &rs->buf_size, 0);
3254 p = rs->buf;
3255 if (*p == 'E')
3256 error (_("Remote failure reply: %s"), p);
3257
3258 old_chain = make_cleanup (free_current_marker, &marker);
3259
3260 while (*p++ == 'm')
3261 {
3262 if (marker == NULL)
3263 marker = XCNEW (struct static_tracepoint_marker);
3264
3265 do
3266 {
3267 parse_static_tracepoint_marker_definition (p, &p, marker);
3268
3269 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3270 {
3271 VEC_safe_push (static_tracepoint_marker_p,
3272 markers, marker);
3273 marker = NULL;
3274 }
3275 else
3276 {
3277 release_static_tracepoint_marker (marker);
3278 memset (marker, 0, sizeof (*marker));
3279 }
3280 }
3281 while (*p++ == ','); /* comma-separated list */
3282 /* Ask for another packet of static tracepoint definition. */
3283 putpkt ("qTsSTM");
3284 getpkt (&rs->buf, &rs->buf_size, 0);
3285 p = rs->buf;
3286 }
3287
3288 do_cleanups (old_chain);
3289 return markers;
3290}
3291
3292\f
10760264
JB
3293/* Implement the to_get_ada_task_ptid function for the remote targets. */
3294
3295static ptid_t
1e6b91a4 3296remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3297{
ba348170 3298 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3299}
3300\f
3301
24b06219 3302/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3303
3304static void
fba45db2 3305extended_remote_restart (void)
c906108c 3306{
d01949b6 3307 struct remote_state *rs = get_remote_state ();
c906108c
SS
3308
3309 /* Send the restart command; for reasons I don't understand the
3310 remote side really expects a number after the "R". */
ea9c271d 3311 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3312 putpkt (rs->buf);
c906108c 3313
ad9a8f3f 3314 remote_fileio_reset ();
c906108c
SS
3315}
3316\f
3317/* Clean up connection to a remote debugger. */
3318
c906108c 3319static void
de90e03d 3320remote_close (struct target_ops *self)
c906108c 3321{
5d93a237
TT
3322 struct remote_state *rs = get_remote_state ();
3323
3324 if (rs->remote_desc == NULL)
d3fd5342
PA
3325 return; /* already closed */
3326
3327 /* Make sure we leave stdin registered in the event loop, and we
3328 don't leave the async SIGINT signal handler installed. */
e3594fd1 3329 remote_terminal_ours (self);
ce5ce7ed 3330
5d93a237
TT
3331 serial_close (rs->remote_desc);
3332 rs->remote_desc = NULL;
ce5ce7ed
PA
3333
3334 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3335 of all the inferiors and their threads we were controlling.
3336 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3337 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3338 inferior_ptid = null_ptid;
f67fd822 3339 discard_all_inferiors ();
ce5ce7ed 3340
f48ff2a7
YQ
3341 /* We are closing the remote target, so we should discard
3342 everything of this target. */
bcc75809 3343 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3344
3345 if (remote_async_inferior_event_token)
3346 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3347
5965e028 3348 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3349
3350 trace_reset_local_state ();
c906108c
SS
3351}
3352
23860348 3353/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3354
3355static void
fba45db2 3356get_offsets (void)
c906108c 3357{
d01949b6 3358 struct remote_state *rs = get_remote_state ();
2e9f7625 3359 char *buf;
085dd6e6 3360 char *ptr;
31d99776
DJ
3361 int lose, num_segments = 0, do_sections, do_segments;
3362 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3363 struct section_offsets *offs;
31d99776
DJ
3364 struct symfile_segment_data *data;
3365
3366 if (symfile_objfile == NULL)
3367 return;
c906108c
SS
3368
3369 putpkt ("qOffsets");
6d820c5c 3370 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3371 buf = rs->buf;
c906108c
SS
3372
3373 if (buf[0] == '\000')
3374 return; /* Return silently. Stub doesn't support
23860348 3375 this command. */
c906108c
SS
3376 if (buf[0] == 'E')
3377 {
8a3fe4f8 3378 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3379 return;
3380 }
3381
3382 /* Pick up each field in turn. This used to be done with scanf, but
3383 scanf will make trouble if CORE_ADDR size doesn't match
3384 conversion directives correctly. The following code will work
3385 with any size of CORE_ADDR. */
3386 text_addr = data_addr = bss_addr = 0;
3387 ptr = buf;
3388 lose = 0;
3389
61012eef 3390 if (startswith (ptr, "Text="))
c906108c
SS
3391 {
3392 ptr += 5;
3393 /* Don't use strtol, could lose on big values. */
3394 while (*ptr && *ptr != ';')
3395 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3396
61012eef 3397 if (startswith (ptr, ";Data="))
31d99776
DJ
3398 {
3399 ptr += 6;
3400 while (*ptr && *ptr != ';')
3401 data_addr = (data_addr << 4) + fromhex (*ptr++);
3402 }
3403 else
3404 lose = 1;
3405
61012eef 3406 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3407 {
3408 ptr += 5;
3409 while (*ptr && *ptr != ';')
3410 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3411
31d99776
DJ
3412 if (bss_addr != data_addr)
3413 warning (_("Target reported unsupported offsets: %s"), buf);
3414 }
3415 else
3416 lose = 1;
3417 }
61012eef 3418 else if (startswith (ptr, "TextSeg="))
c906108c 3419 {
31d99776
DJ
3420 ptr += 8;
3421 /* Don't use strtol, could lose on big values. */
c906108c 3422 while (*ptr && *ptr != ';')
31d99776
DJ
3423 text_addr = (text_addr << 4) + fromhex (*ptr++);
3424 num_segments = 1;
3425
61012eef 3426 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3427 {
3428 ptr += 9;
3429 while (*ptr && *ptr != ';')
3430 data_addr = (data_addr << 4) + fromhex (*ptr++);
3431 num_segments++;
3432 }
c906108c
SS
3433 }
3434 else
3435 lose = 1;
3436
3437 if (lose)
8a3fe4f8 3438 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3439 else if (*ptr != '\0')
3440 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3441
802188a7 3442 offs = ((struct section_offsets *)
a39a16c4 3443 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3444 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3445 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3446
31d99776
DJ
3447 data = get_symfile_segment_data (symfile_objfile->obfd);
3448 do_segments = (data != NULL);
3449 do_sections = num_segments == 0;
c906108c 3450
28c32713 3451 if (num_segments > 0)
31d99776 3452 {
31d99776
DJ
3453 segments[0] = text_addr;
3454 segments[1] = data_addr;
3455 }
28c32713
JB
3456 /* If we have two segments, we can still try to relocate everything
3457 by assuming that the .text and .data offsets apply to the whole
3458 text and data segments. Convert the offsets given in the packet
3459 to base addresses for symfile_map_offsets_to_segments. */
3460 else if (data && data->num_segments == 2)
3461 {
3462 segments[0] = data->segment_bases[0] + text_addr;
3463 segments[1] = data->segment_bases[1] + data_addr;
3464 num_segments = 2;
3465 }
8d385431
DJ
3466 /* If the object file has only one segment, assume that it is text
3467 rather than data; main programs with no writable data are rare,
3468 but programs with no code are useless. Of course the code might
3469 have ended up in the data segment... to detect that we would need
3470 the permissions here. */
3471 else if (data && data->num_segments == 1)
3472 {
3473 segments[0] = data->segment_bases[0] + text_addr;
3474 num_segments = 1;
3475 }
28c32713
JB
3476 /* There's no way to relocate by segment. */
3477 else
3478 do_segments = 0;
31d99776
DJ
3479
3480 if (do_segments)
3481 {
3482 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3483 offs, num_segments, segments);
3484
3485 if (ret == 0 && !do_sections)
3e43a32a
MS
3486 error (_("Can not handle qOffsets TextSeg "
3487 "response with this symbol file"));
31d99776
DJ
3488
3489 if (ret > 0)
3490 do_sections = 0;
3491 }
c906108c 3492
9ef895d6
DJ
3493 if (data)
3494 free_symfile_segment_data (data);
31d99776
DJ
3495
3496 if (do_sections)
3497 {
3498 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3499
3e43a32a
MS
3500 /* This is a temporary kludge to force data and bss to use the
3501 same offsets because that's what nlmconv does now. The real
3502 solution requires changes to the stub and remote.c that I
3503 don't have time to do right now. */
31d99776
DJ
3504
3505 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3506 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3507 }
c906108c
SS
3508
3509 objfile_relocate (symfile_objfile, offs);
3510}
3511
9a7071a8
JB
3512/* Send interrupt_sequence to remote target. */
3513static void
eeae04df 3514send_interrupt_sequence (void)
9a7071a8 3515{
5d93a237
TT
3516 struct remote_state *rs = get_remote_state ();
3517
9a7071a8 3518 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3519 remote_serial_write ("\x03", 1);
9a7071a8 3520 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3521 serial_send_break (rs->remote_desc);
9a7071a8
JB
3522 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3523 {
5d93a237 3524 serial_send_break (rs->remote_desc);
c33e31fd 3525 remote_serial_write ("g", 1);
9a7071a8
JB
3526 }
3527 else
3528 internal_error (__FILE__, __LINE__,
3529 _("Invalid value for interrupt_sequence_mode: %s."),
3530 interrupt_sequence_mode);
3531}
3532
3405876a
PA
3533
3534/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3535 and extract the PTID. Returns NULL_PTID if not found. */
3536
3537static ptid_t
3538stop_reply_extract_thread (char *stop_reply)
3539{
3540 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3541 {
3542 char *p;
3543
3544 /* Txx r:val ; r:val (...) */
3545 p = &stop_reply[3];
3546
3547 /* Look for "register" named "thread". */
3548 while (*p != '\0')
3549 {
3550 char *p1;
3551
3552 p1 = strchr (p, ':');
3553 if (p1 == NULL)
3554 return null_ptid;
3555
3556 if (strncmp (p, "thread", p1 - p) == 0)
3557 return read_ptid (++p1, &p);
3558
3559 p1 = strchr (p, ';');
3560 if (p1 == NULL)
3561 return null_ptid;
3562 p1++;
3563
3564 p = p1;
3565 }
3566 }
3567
3568 return null_ptid;
3569}
3570
b7ea362b
PA
3571/* Determine the remote side's current thread. If we have a stop
3572 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3573 "thread" register we can extract the current thread from. If not,
3574 ask the remote which is the current thread with qC. The former
3575 method avoids a roundtrip. */
3576
3577static ptid_t
3578get_current_thread (char *wait_status)
3579{
6a49a997 3580 ptid_t ptid = null_ptid;
b7ea362b
PA
3581
3582 /* Note we don't use remote_parse_stop_reply as that makes use of
3583 the target architecture, which we haven't yet fully determined at
3584 this point. */
3585 if (wait_status != NULL)
3586 ptid = stop_reply_extract_thread (wait_status);
3587 if (ptid_equal (ptid, null_ptid))
3588 ptid = remote_current_thread (inferior_ptid);
3589
3590 return ptid;
3591}
3592
49c62f2e
PA
3593/* Query the remote target for which is the current thread/process,
3594 add it to our tables, and update INFERIOR_PTID. The caller is
3595 responsible for setting the state such that the remote end is ready
3405876a
PA
3596 to return the current thread.
3597
3598 This function is called after handling the '?' or 'vRun' packets,
3599 whose response is a stop reply from which we can also try
3600 extracting the thread. If the target doesn't support the explicit
3601 qC query, we infer the current thread from that stop reply, passed
3602 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3603
3604static void
3405876a 3605add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3606{
3607 struct remote_state *rs = get_remote_state ();
3608 int fake_pid_p = 0;
6a49a997 3609 ptid_t ptid;
49c62f2e
PA
3610
3611 inferior_ptid = null_ptid;
3612
b7ea362b
PA
3613 /* Now, if we have thread information, update inferior_ptid. */
3614 ptid = get_current_thread (wait_status);
3405876a 3615
49c62f2e
PA
3616 if (!ptid_equal (ptid, null_ptid))
3617 {
3618 if (!remote_multi_process_p (rs))
3619 fake_pid_p = 1;
3620
3621 inferior_ptid = ptid;
3622 }
3623 else
3624 {
3625 /* Without this, some commands which require an active target
3626 (such as kill) won't work. This variable serves (at least)
3627 double duty as both the pid of the target process (if it has
3628 such), and as a flag indicating that a target is active. */
3629 inferior_ptid = magic_null_ptid;
3630 fake_pid_p = 1;
3631 }
3632
1b6e6f5c 3633 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3634
3635 /* Add the main thread. */
3636 add_thread_silent (inferior_ptid);
3637}
3638
221e1a37
PA
3639/* Process all initial stop replies the remote side sent in response
3640 to the ? packet. These indicate threads that were already stopped
3641 on initial connection. We mark these threads as stopped and print
3642 their current frame before giving the user the prompt. */
3643
3644static void
3645process_initial_stop_replies (void)
3646{
3647 int pending_stop_replies = stop_reply_queue_length ();
3648
3649 /* Consume the initial pending events. */
3650 while (pending_stop_replies-- > 0)
3651 {
3652 ptid_t waiton_ptid = minus_one_ptid;
3653 ptid_t event_ptid;
3654 struct target_waitstatus ws;
3655 int ignore_event = 0;
3656
3657 memset (&ws, 0, sizeof (ws));
3658 event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
3659 if (remote_debug)
3660 print_target_wait_results (waiton_ptid, event_ptid, &ws);
3661
3662 switch (ws.kind)
3663 {
3664 case TARGET_WAITKIND_IGNORE:
3665 case TARGET_WAITKIND_NO_RESUMED:
3666 case TARGET_WAITKIND_SIGNALLED:
3667 case TARGET_WAITKIND_EXITED:
3668 /* We shouldn't see these, but if we do, just ignore. */
3669 if (remote_debug)
3670 fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
3671 ignore_event = 1;
3672 break;
3673
3674 case TARGET_WAITKIND_EXECD:
3675 xfree (ws.value.execd_pathname);
3676 break;
3677 default:
3678 break;
3679 }
3680
3681 if (ignore_event)
3682 continue;
3683
3684 switch_to_thread (event_ptid);
3685 set_executing (event_ptid, 0);
3686 set_running (event_ptid, 0);
3687
3688 stop_pc = get_frame_pc (get_current_frame ());
3689 set_current_sal_from_frame (get_current_frame ());
3690
3691 if (ws.kind == TARGET_WAITKIND_STOPPED)
3692 {
3693 enum gdb_signal sig = ws.value.sig;
3694
3695 /* Stubs traditionally report SIGTRAP as initial signal,
3696 instead of signal 0. Suppress it. */
3697 if (sig == GDB_SIGNAL_TRAP)
3698 sig = GDB_SIGNAL_0;
3699 inferior_thread ()->suspend.stop_signal = sig;
3700
3701 if (signal_print_state (sig))
3702 observer_notify_signal_received (sig);
3703 }
3704
243a9253 3705 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
221e1a37
PA
3706 observer_notify_normal_stop (NULL, 1);
3707 }
3708}
3709
9cbc821d 3710static void
04bd08de 3711remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3712{
c8d104ad
PA
3713 struct remote_state *rs = get_remote_state ();
3714 struct packet_config *noack_config;
2d717e4f 3715 char *wait_status = NULL;
8621d6a9 3716
23860348 3717 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3718 QUIT;
c906108c 3719
9a7071a8
JB
3720 if (interrupt_on_connect)
3721 send_interrupt_sequence ();
3722
57e12211 3723 /* Ack any packet which the remote side has already sent. */
5d93a237 3724 serial_write (rs->remote_desc, "+", 1);
57e12211 3725
1e51243a
PA
3726 /* Signal other parts that we're going through the initial setup,
3727 and so things may not be stable yet. */
3728 rs->starting_up = 1;
3729
c8d104ad
PA
3730 /* The first packet we send to the target is the optional "supported
3731 packets" request. If the target can answer this, it will tell us
3732 which later probes to skip. */
3733 remote_query_supported ();
3734
d914c394 3735 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3736 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3737 remote_set_permissions (target);
d914c394 3738
c8d104ad
PA
3739 /* Next, we possibly activate noack mode.
3740
3741 If the QStartNoAckMode packet configuration is set to AUTO,
3742 enable noack mode if the stub reported a wish for it with
3743 qSupported.
3744
3745 If set to TRUE, then enable noack mode even if the stub didn't
3746 report it in qSupported. If the stub doesn't reply OK, the
3747 session ends with an error.
3748
3749 If FALSE, then don't activate noack mode, regardless of what the
3750 stub claimed should be the default with qSupported. */
3751
3752 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3753 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3754 {
3755 putpkt ("QStartNoAckMode");
3756 getpkt (&rs->buf, &rs->buf_size, 0);
3757 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3758 rs->noack_mode = 1;
3759 }
3760
04bd08de 3761 if (extended_p)
5fe04517
PA
3762 {
3763 /* Tell the remote that we are using the extended protocol. */
3764 putpkt ("!");
3765 getpkt (&rs->buf, &rs->buf_size, 0);
3766 }
3767
9b224c5e
PA
3768 /* Let the target know which signals it is allowed to pass down to
3769 the program. */
3770 update_signals_program_target ();
3771
d962ef82
DJ
3772 /* Next, if the target can specify a description, read it. We do
3773 this before anything involving memory or registers. */
3774 target_find_description ();
3775
6c95b8df
PA
3776 /* Next, now that we know something about the target, update the
3777 address spaces in the program spaces. */
3778 update_address_spaces ();
3779
50c71eaf
PA
3780 /* On OSs where the list of libraries is global to all
3781 processes, we fetch them early. */
f5656ead 3782 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3783 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3784
74531fed
PA
3785 if (non_stop)
3786 {
4082afcc 3787 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3788 error (_("Non-stop mode requested, but remote "
3789 "does not support non-stop"));
74531fed
PA
3790
3791 putpkt ("QNonStop:1");
3792 getpkt (&rs->buf, &rs->buf_size, 0);
3793
3794 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3795 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3796
3797 /* Find about threads and processes the stub is already
3798 controlling. We default to adding them in the running state.
3799 The '?' query below will then tell us about which threads are
3800 stopped. */
e8032dde 3801 remote_update_thread_list (target);
74531fed 3802 }
4082afcc 3803 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3804 {
3805 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3806 Request it explicitly. */
74531fed
PA
3807 putpkt ("QNonStop:0");
3808 getpkt (&rs->buf, &rs->buf_size, 0);
3809
3810 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3811 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3812 }
3813
a0743c90
YQ
3814 /* Upload TSVs regardless of whether the target is running or not. The
3815 remote stub, such as GDBserver, may have some predefined or builtin
3816 TSVs, even if the target is not running. */
8bd200f1 3817 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3818 {
3819 struct uploaded_tsv *uploaded_tsvs = NULL;
3820
181e3713 3821 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3822 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3823 }
3824
2d717e4f
DJ
3825 /* Check whether the target is running now. */
3826 putpkt ("?");
3827 getpkt (&rs->buf, &rs->buf_size, 0);
3828
74531fed 3829 if (!non_stop)
2d717e4f 3830 {
e714e1bf
UW
3831 ptid_t ptid;
3832 int fake_pid_p = 0;
3833 struct inferior *inf;
3834
74531fed 3835 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3836 {
04bd08de 3837 if (!extended_p)
74531fed 3838 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3839
3840 /* We're connected, but not running. Drop out before we
3841 call start_remote. */
e278ad5b 3842 rs->starting_up = 0;
c35b1492 3843 return;
2d717e4f
DJ
3844 }
3845 else
74531fed 3846 {
74531fed
PA
3847 /* Save the reply for later. */
3848 wait_status = alloca (strlen (rs->buf) + 1);
3849 strcpy (wait_status, rs->buf);
3850 }
3851
b7ea362b 3852 /* Fetch thread list. */
e8032dde 3853 target_update_thread_list ();
b7ea362b 3854
74531fed
PA
3855 /* Let the stub know that we want it to return the thread. */
3856 set_continue_thread (minus_one_ptid);
3857
b7ea362b
PA
3858 if (thread_count () == 0)
3859 {
3860 /* Target has no concept of threads at all. GDB treats
3861 non-threaded target as single-threaded; add a main
3862 thread. */
3863 add_current_inferior_and_thread (wait_status);
3864 }
3865 else
3866 {
3867 /* We have thread information; select the thread the target
3868 says should be current. If we're reconnecting to a
3869 multi-threaded program, this will ideally be the thread
3870 that last reported an event before GDB disconnected. */
3871 inferior_ptid = get_current_thread (wait_status);
3872 if (ptid_equal (inferior_ptid, null_ptid))
3873 {
3874 /* Odd... The target was able to list threads, but not
3875 tell us which thread was current (no "thread"
3876 register in T stop reply?). Just pick the first
3877 thread in the thread list then. */
c9f35b34
KB
3878
3879 if (remote_debug)
3880 fprintf_unfiltered (gdb_stdlog,
3881 "warning: couldn't determine remote "
3882 "current thread; picking first in list.\n");
3883
b7ea362b
PA
3884 inferior_ptid = thread_list->ptid;
3885 }
3886 }
74531fed 3887
6e586cc5
YQ
3888 /* init_wait_for_inferior should be called before get_offsets in order
3889 to manage `inserted' flag in bp loc in a correct state.
3890 breakpoint_init_inferior, called from init_wait_for_inferior, set
3891 `inserted' flag to 0, while before breakpoint_re_set, called from
3892 start_remote, set `inserted' flag to 1. In the initialization of
3893 inferior, breakpoint_init_inferior should be called first, and then
3894 breakpoint_re_set can be called. If this order is broken, state of
3895 `inserted' flag is wrong, and cause some problems on breakpoint
3896 manipulation. */
3897 init_wait_for_inferior ();
3898
74531fed
PA
3899 get_offsets (); /* Get text, data & bss offsets. */
3900
d962ef82
DJ
3901 /* If we could not find a description using qXfer, and we know
3902 how to do it some other way, try again. This is not
3903 supported for non-stop; it could be, but it is tricky if
3904 there are no stopped threads when we connect. */
04bd08de 3905 if (remote_read_description_p (target)
f5656ead 3906 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3907 {
3908 target_clear_description ();
3909 target_find_description ();
3910 }
3911
74531fed
PA
3912 /* Use the previously fetched status. */
3913 gdb_assert (wait_status != NULL);
3914 strcpy (rs->buf, wait_status);
3915 rs->cached_wait_status = 1;
3916
3917 immediate_quit--;
04bd08de 3918 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3919 }
3920 else
3921 {
221e1a37
PA
3922 ptid_t current_ptid;
3923
68c97600
PA
3924 /* Clear WFI global state. Do this before finding about new
3925 threads and inferiors, and setting the current inferior.
3926 Otherwise we would clear the proceed status of the current
3927 inferior when we want its stop_soon state to be preserved
3928 (see notice_new_inferior). */
3929 init_wait_for_inferior ();
3930
74531fed
PA
3931 /* In non-stop, we will either get an "OK", meaning that there
3932 are no stopped threads at this time; or, a regular stop
3933 reply. In the latter case, there may be more than one thread
3934 stopped --- we pull them all out using the vStopped
3935 mechanism. */
3936 if (strcmp (rs->buf, "OK") != 0)
3937 {
722247f1 3938 struct notif_client *notif = &notif_client_stop;
2d717e4f 3939
722247f1
YQ
3940 /* remote_notif_get_pending_replies acks this one, and gets
3941 the rest out. */
f48ff2a7 3942 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3943 = remote_notif_parse (notif, rs->buf);
3944 remote_notif_get_pending_events (notif);
74531fed 3945 }
2d717e4f 3946
74531fed
PA
3947 if (thread_count () == 0)
3948 {
04bd08de 3949 if (!extended_p)
74531fed 3950 error (_("The target is not running (try extended-remote?)"));
82f73884 3951
c35b1492
PA
3952 /* We're connected, but not running. Drop out before we
3953 call start_remote. */
e278ad5b 3954 rs->starting_up = 0;
c35b1492
PA
3955 return;
3956 }
74531fed
PA
3957
3958 /* Let the stub know that we want it to return the thread. */
c0a2216e 3959
74531fed
PA
3960 /* Force the stub to choose a thread. */
3961 set_general_thread (null_ptid);
c906108c 3962
74531fed 3963 /* Query it. */
221e1a37 3964 current_ptid = remote_current_thread (minus_one_ptid);
74531fed
PA
3965 if (ptid_equal (inferior_ptid, minus_one_ptid))
3966 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3967
221e1a37 3968 inferior_ptid = current_ptid;
74531fed
PA
3969 get_offsets (); /* Get text, data & bss offsets. */
3970
3971 /* In non-stop mode, any cached wait status will be stored in
3972 the stop reply queue. */
3973 gdb_assert (wait_status == NULL);
f0223081 3974
2455069d 3975 /* Report all signals during attach/startup. */
94bedb42 3976 remote_pass_signals (target, 0, NULL);
221e1a37
PA
3977
3978 /* If there are already stopped threads, mark them stopped and
3979 report their stops before giving the prompt to the user. */
3980 process_initial_stop_replies ();
3981
3982 switch_to_thread (current_ptid);
3983
3984 if (target_can_async_p ())
3985 target_async (1);
74531fed 3986 }
c8d104ad 3987
c8d104ad
PA
3988 /* If we connected to a live target, do some additional setup. */
3989 if (target_has_execution)
3990 {
f4ccffad 3991 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3992 remote_check_symbols ();
c8d104ad 3993 }
50c71eaf 3994
d5551862
SS
3995 /* Possibly the target has been engaged in a trace run started
3996 previously; find out where things are at. */
8bd200f1 3997 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3998 {
00bf0b85 3999 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 4000
00bf0b85
SS
4001 if (current_trace_status ()->running)
4002 printf_filtered (_("Trace is already running on the target.\n"));
4003
ab6617cc 4004 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
4005
4006 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
4007 }
4008
1e51243a
PA
4009 /* The thread and inferior lists are now synchronized with the
4010 target, our symbols have been relocated, and we're merged the
4011 target's tracepoints with ours. We're done with basic start
4012 up. */
4013 rs->starting_up = 0;
4014
a25a5a45
PA
4015 /* Maybe breakpoints are global and need to be inserted now. */
4016 if (breakpoints_should_be_inserted_now ())
50c71eaf 4017 insert_breakpoints ();
c906108c
SS
4018}
4019
4020/* Open a connection to a remote debugger.
4021 NAME is the filename used for communication. */
4022
4023static void
014f9477 4024remote_open (const char *name, int from_tty)
c906108c 4025{
75c99385 4026 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
4027}
4028
c906108c
SS
4029/* Open a connection to a remote debugger using the extended
4030 remote gdb protocol. NAME is the filename used for communication. */
4031
4032static void
014f9477 4033extended_remote_open (const char *name, int from_tty)
c906108c 4034{
75c99385 4035 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
4036}
4037
ca4f7f8b
PA
4038/* Reset all packets back to "unknown support". Called when opening a
4039 new connection to a remote target. */
c906108c 4040
d471ea57 4041static void
ca4f7f8b 4042reset_all_packet_configs_support (void)
d471ea57
AC
4043{
4044 int i;
a744cf53 4045
444abaca 4046 for (i = 0; i < PACKET_MAX; i++)
4082afcc 4047 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
4048}
4049
ca4f7f8b
PA
4050/* Initialize all packet configs. */
4051
4052static void
4053init_all_packet_configs (void)
4054{
4055 int i;
4056
4057 for (i = 0; i < PACKET_MAX; i++)
4058 {
4059 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
4060 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
4061 }
4062}
4063
23860348 4064/* Symbol look-up. */
dc8acb97
MS
4065
4066static void
36d25514 4067remote_check_symbols (void)
dc8acb97 4068{
d01949b6 4069 struct remote_state *rs = get_remote_state ();
dc8acb97 4070 char *msg, *reply, *tmp;
3b7344d5 4071 struct bound_minimal_symbol sym;
dc8acb97 4072 int end;
a5c0808e 4073 struct cleanup *old_chain;
dc8acb97 4074
63154eca
PA
4075 /* The remote side has no concept of inferiors that aren't running
4076 yet, it only knows about running processes. If we're connected
4077 but our current inferior is not running, we should not invite the
4078 remote target to request symbol lookups related to its
4079 (unrelated) current process. */
4080 if (!target_has_execution)
4081 return;
4082
4082afcc 4083 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
4084 return;
4085
63154eca
PA
4086 /* Make sure the remote is pointing at the right process. Note
4087 there's no way to select "no process". */
3c9c4b83
PA
4088 set_general_process ();
4089
6d820c5c
DJ
4090 /* Allocate a message buffer. We can't reuse the input buffer in RS,
4091 because we need both at the same time. */
a5c0808e
PA
4092 msg = xmalloc (get_remote_packet_size ());
4093 old_chain = make_cleanup (xfree, msg);
6d820c5c 4094
23860348 4095 /* Invite target to request symbol lookups. */
dc8acb97
MS
4096
4097 putpkt ("qSymbol::");
6d820c5c
DJ
4098 getpkt (&rs->buf, &rs->buf_size, 0);
4099 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 4100 reply = rs->buf;
dc8acb97 4101
61012eef 4102 while (startswith (reply, "qSymbol:"))
dc8acb97 4103 {
77e371c0
TT
4104 struct bound_minimal_symbol sym;
4105
dc8acb97 4106 tmp = &reply[8];
cfd77fa1 4107 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
4108 msg[end] = '\0';
4109 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 4110 if (sym.minsym == NULL)
ea9c271d 4111 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 4112 else
2bbe3cc1 4113 {
f5656ead 4114 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 4115 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
4116
4117 /* If this is a function address, return the start of code
4118 instead of any data function descriptor. */
f5656ead 4119 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
4120 sym_addr,
4121 &current_target);
4122
4123 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 4124 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
4125 }
4126
dc8acb97 4127 putpkt (msg);
6d820c5c 4128 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4129 reply = rs->buf;
dc8acb97 4130 }
a5c0808e
PA
4131
4132 do_cleanups (old_chain);
dc8acb97
MS
4133}
4134
9db8d71f 4135static struct serial *
baa336ce 4136remote_serial_open (const char *name)
9db8d71f
DJ
4137{
4138 static int udp_warning = 0;
4139
4140 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
4141 of in ser-tcp.c, because it is the remote protocol assuming that the
4142 serial connection is reliable and not the serial connection promising
4143 to be. */
61012eef 4144 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 4145 {
3e43a32a
MS
4146 warning (_("The remote protocol may be unreliable over UDP.\n"
4147 "Some events may be lost, rendering further debugging "
4148 "impossible."));
9db8d71f
DJ
4149 udp_warning = 1;
4150 }
4151
4152 return serial_open (name);
4153}
4154
d914c394
SS
4155/* Inform the target of our permission settings. The permission flags
4156 work without this, but if the target knows the settings, it can do
4157 a couple things. First, it can add its own check, to catch cases
4158 that somehow manage to get by the permissions checks in target
4159 methods. Second, if the target is wired to disallow particular
4160 settings (for instance, a system in the field that is not set up to
4161 be able to stop at a breakpoint), it can object to any unavailable
4162 permissions. */
4163
4164void
c378d69d 4165remote_set_permissions (struct target_ops *self)
d914c394
SS
4166{
4167 struct remote_state *rs = get_remote_state ();
4168
bba74b36
YQ
4169 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4170 "WriteReg:%x;WriteMem:%x;"
4171 "InsertBreak:%x;InsertTrace:%x;"
4172 "InsertFastTrace:%x;Stop:%x",
4173 may_write_registers, may_write_memory,
4174 may_insert_breakpoints, may_insert_tracepoints,
4175 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4176 putpkt (rs->buf);
4177 getpkt (&rs->buf, &rs->buf_size, 0);
4178
4179 /* If the target didn't like the packet, warn the user. Do not try
4180 to undo the user's settings, that would just be maddening. */
4181 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4182 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4183}
4184
be2a5f71
DJ
4185/* This type describes each known response to the qSupported
4186 packet. */
4187struct protocol_feature
4188{
4189 /* The name of this protocol feature. */
4190 const char *name;
4191
4192 /* The default for this protocol feature. */
4193 enum packet_support default_support;
4194
4195 /* The function to call when this feature is reported, or after
4196 qSupported processing if the feature is not supported.
4197 The first argument points to this structure. The second
4198 argument indicates whether the packet requested support be
4199 enabled, disabled, or probed (or the default, if this function
4200 is being called at the end of processing and this feature was
4201 not reported). The third argument may be NULL; if not NULL, it
4202 is a NUL-terminated string taken from the packet following
4203 this feature's name and an equals sign. */
4204 void (*func) (const struct protocol_feature *, enum packet_support,
4205 const char *);
4206
4207 /* The corresponding packet for this feature. Only used if
4208 FUNC is remote_supported_packet. */
4209 int packet;
4210};
4211
be2a5f71
DJ
4212static void
4213remote_supported_packet (const struct protocol_feature *feature,
4214 enum packet_support support,
4215 const char *argument)
4216{
4217 if (argument)
4218 {
4219 warning (_("Remote qSupported response supplied an unexpected value for"
4220 " \"%s\"."), feature->name);
4221 return;
4222 }
4223
4082afcc 4224 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4225}
be2a5f71
DJ
4226
4227static void
4228remote_packet_size (const struct protocol_feature *feature,
4229 enum packet_support support, const char *value)
4230{
4231 struct remote_state *rs = get_remote_state ();
4232
4233 int packet_size;
4234 char *value_end;
4235
4236 if (support != PACKET_ENABLE)
4237 return;
4238
4239 if (value == NULL || *value == '\0')
4240 {
4241 warning (_("Remote target reported \"%s\" without a size."),
4242 feature->name);
4243 return;
4244 }
4245
4246 errno = 0;
4247 packet_size = strtol (value, &value_end, 16);
4248 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4249 {
4250 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4251 feature->name, value);
4252 return;
4253 }
4254
be2a5f71
DJ
4255 /* Record the new maximum packet size. */
4256 rs->explicit_packet_size = packet_size;
4257}
4258
dc473cfb 4259static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4260 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4261 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4262 PACKET_qXfer_auxv },
c78fa86a
GB
4263 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4264 PACKET_qXfer_exec_file },
23181151
DJ
4265 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4266 PACKET_qXfer_features },
cfa9d6d9
DJ
4267 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4268 PACKET_qXfer_libraries },
2268b414
JK
4269 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4270 PACKET_qXfer_libraries_svr4 },
ced63ec0 4271 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4272 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4273 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4274 PACKET_qXfer_memory_map },
4de6483e
UW
4275 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4276 PACKET_qXfer_spu_read },
4277 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4278 PACKET_qXfer_spu_write },
07e059b5
VP
4279 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4280 PACKET_qXfer_osdata },
dc146f7c
VP
4281 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4282 PACKET_qXfer_threads },
b3b9301e
PA
4283 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4284 PACKET_qXfer_traceframe_info },
89be2091
DJ
4285 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4286 PACKET_QPassSignals },
9b224c5e
PA
4287 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4288 PACKET_QProgramSignals },
a6f3e723
SL
4289 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4290 PACKET_QStartNoAckMode },
4082afcc
PA
4291 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4292 PACKET_multiprocess_feature },
4293 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4294 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4295 PACKET_qXfer_siginfo_read },
4296 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4297 PACKET_qXfer_siginfo_write },
4082afcc 4298 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4299 PACKET_ConditionalTracepoints },
4082afcc 4300 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4301 PACKET_ConditionalBreakpoints },
4082afcc 4302 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4303 PACKET_BreakpointCommands },
4082afcc 4304 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4305 PACKET_FastTracepoints },
4082afcc 4306 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4307 PACKET_StaticTracepoints },
4082afcc 4308 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4309 PACKET_InstallInTrace},
4082afcc
PA
4310 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4311 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4312 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4313 PACKET_bc },
4314 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4315 PACKET_bs },
409873ef
SS
4316 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4317 PACKET_TracepointSource },
d914c394
SS
4318 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4319 PACKET_QAllow },
4082afcc
PA
4320 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4321 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4322 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4323 PACKET_qXfer_fdpic },
169081d0
TG
4324 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4325 PACKET_qXfer_uib },
03583c20
UW
4326 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4327 PACKET_QDisableRandomization },
d1feda86 4328 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4329 { "QTBuffer:size", PACKET_DISABLE,
4330 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4331 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4332 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4333 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4334 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4335 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4336 PACKET_qXfer_btrace },
4337 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4338 PACKET_qXfer_btrace_conf },
4339 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4340 PACKET_Qbtrace_conf_bts_size },
4341 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4342 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4343 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4344 PACKET_fork_event_feature },
4345 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4346 PACKET_vfork_event_feature },
94585166
DB
4347 { "exec-events", PACKET_DISABLE, remote_supported_packet,
4348 PACKET_exec_event_feature },
b20a6524
MM
4349 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
4350 PACKET_Qbtrace_conf_pt_size }
be2a5f71
DJ
4351};
4352
c8d5aac9
L
4353static char *remote_support_xml;
4354
4355/* Register string appended to "xmlRegisters=" in qSupported query. */
4356
4357void
6e39997a 4358register_remote_support_xml (const char *xml)
c8d5aac9
L
4359{
4360#if defined(HAVE_LIBEXPAT)
4361 if (remote_support_xml == NULL)
c4f7c687 4362 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4363 else
4364 {
4365 char *copy = xstrdup (remote_support_xml + 13);
4366 char *p = strtok (copy, ",");
4367
4368 do
4369 {
4370 if (strcmp (p, xml) == 0)
4371 {
4372 /* already there */
4373 xfree (copy);
4374 return;
4375 }
4376 }
4377 while ((p = strtok (NULL, ",")) != NULL);
4378 xfree (copy);
4379
94b0dee1
PA
4380 remote_support_xml = reconcat (remote_support_xml,
4381 remote_support_xml, ",", xml,
4382 (char *) NULL);
c8d5aac9
L
4383 }
4384#endif
4385}
4386
4387static char *
4388remote_query_supported_append (char *msg, const char *append)
4389{
4390 if (msg)
94b0dee1 4391 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4392 else
4393 return xstrdup (append);
4394}
4395
be2a5f71
DJ
4396static void
4397remote_query_supported (void)
4398{
4399 struct remote_state *rs = get_remote_state ();
4400 char *next;
4401 int i;
4402 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4403
4404 /* The packet support flags are handled differently for this packet
4405 than for most others. We treat an error, a disabled packet, and
4406 an empty response identically: any features which must be reported
4407 to be used will be automatically disabled. An empty buffer
4408 accomplishes this, since that is also the representation for a list
4409 containing no features. */
4410
4411 rs->buf[0] = 0;
4082afcc 4412 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4413 {
c8d5aac9 4414 char *q = NULL;
94b0dee1 4415 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4416
73b8c1fd
PA
4417 if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
4418 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4419
f7e6eed5
PA
4420 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4421 q = remote_query_supported_append (q, "swbreak+");
4422 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4423 q = remote_query_supported_append (q, "hwbreak+");
4424
c8d5aac9
L
4425 if (remote_support_xml)
4426 q = remote_query_supported_append (q, remote_support_xml);
4427
dde08ee1
PA
4428 q = remote_query_supported_append (q, "qRelocInsn+");
4429
89245bc0
DB
4430 if (rs->extended)
4431 {
4432 if (packet_set_cmd_state (PACKET_fork_event_feature)
4433 != AUTO_BOOLEAN_FALSE)
4434 q = remote_query_supported_append (q, "fork-events+");
4435 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4436 != AUTO_BOOLEAN_FALSE)
4437 q = remote_query_supported_append (q, "vfork-events+");
94585166
DB
4438 if (packet_set_cmd_state (PACKET_exec_event_feature)
4439 != AUTO_BOOLEAN_FALSE)
4440 q = remote_query_supported_append (q, "exec-events+");
89245bc0
DB
4441 }
4442
dde08ee1
PA
4443 q = reconcat (q, "qSupported:", q, (char *) NULL);
4444 putpkt (q);
82f73884 4445
94b0dee1
PA
4446 do_cleanups (old_chain);
4447
be2a5f71
DJ
4448 getpkt (&rs->buf, &rs->buf_size, 0);
4449
4450 /* If an error occured, warn, but do not return - just reset the
4451 buffer to empty and go on to disable features. */
4452 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4453 == PACKET_ERROR)
4454 {
4455 warning (_("Remote failure reply: %s"), rs->buf);
4456 rs->buf[0] = 0;
4457 }
4458 }
4459
4460 memset (seen, 0, sizeof (seen));
4461
4462 next = rs->buf;
4463 while (*next)
4464 {
4465 enum packet_support is_supported;
4466 char *p, *end, *name_end, *value;
4467
4468 /* First separate out this item from the rest of the packet. If
4469 there's another item after this, we overwrite the separator
4470 (terminated strings are much easier to work with). */
4471 p = next;
4472 end = strchr (p, ';');
4473 if (end == NULL)
4474 {
4475 end = p + strlen (p);
4476 next = end;
4477 }
4478 else
4479 {
89be2091
DJ
4480 *end = '\0';
4481 next = end + 1;
4482
be2a5f71
DJ
4483 if (end == p)
4484 {
4485 warning (_("empty item in \"qSupported\" response"));
4486 continue;
4487 }
be2a5f71
DJ
4488 }
4489
4490 name_end = strchr (p, '=');
4491 if (name_end)
4492 {
4493 /* This is a name=value entry. */
4494 is_supported = PACKET_ENABLE;
4495 value = name_end + 1;
4496 *name_end = '\0';
4497 }
4498 else
4499 {
4500 value = NULL;
4501 switch (end[-1])
4502 {
4503 case '+':
4504 is_supported = PACKET_ENABLE;
4505 break;
4506
4507 case '-':
4508 is_supported = PACKET_DISABLE;
4509 break;
4510
4511 case '?':
4512 is_supported = PACKET_SUPPORT_UNKNOWN;
4513 break;
4514
4515 default:
3e43a32a
MS
4516 warning (_("unrecognized item \"%s\" "
4517 "in \"qSupported\" response"), p);
be2a5f71
DJ
4518 continue;
4519 }
4520 end[-1] = '\0';
4521 }
4522
4523 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4524 if (strcmp (remote_protocol_features[i].name, p) == 0)
4525 {
4526 const struct protocol_feature *feature;
4527
4528 seen[i] = 1;
4529 feature = &remote_protocol_features[i];
4530 feature->func (feature, is_supported, value);
4531 break;
4532 }
4533 }
4534
4535 /* If we increased the packet size, make sure to increase the global
4536 buffer size also. We delay this until after parsing the entire
4537 qSupported packet, because this is the same buffer we were
4538 parsing. */
4539 if (rs->buf_size < rs->explicit_packet_size)
4540 {
4541 rs->buf_size = rs->explicit_packet_size;
4542 rs->buf = xrealloc (rs->buf, rs->buf_size);
4543 }
4544
4545 /* Handle the defaults for unmentioned features. */
4546 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4547 if (!seen[i])
4548 {
4549 const struct protocol_feature *feature;
4550
4551 feature = &remote_protocol_features[i];
4552 feature->func (feature, feature->default_support, NULL);
4553 }
4554}
4555
78a095c3
JK
4556/* Remove any of the remote.c targets from target stack. Upper targets depend
4557 on it so remove them first. */
4558
4559static void
4560remote_unpush_target (void)
4561{
4562 pop_all_targets_above (process_stratum - 1);
4563}
be2a5f71 4564
c906108c 4565static void
014f9477 4566remote_open_1 (const char *name, int from_tty,
3e43a32a 4567 struct target_ops *target, int extended_p)
c906108c 4568{
d01949b6 4569 struct remote_state *rs = get_remote_state ();
a6f3e723 4570
c906108c 4571 if (name == 0)
8a3fe4f8 4572 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4573 "serial device is attached to the remote system\n"
8a3fe4f8 4574 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4575
23860348 4576 /* See FIXME above. */
c6ebd6cf 4577 if (!target_async_permitted)
92d1e331 4578 wait_forever_enabled_p = 1;
6426a772 4579
2d717e4f 4580 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4581 Ask this question first, before target_preopen has a chance to kill
4582 anything. */
5d93a237 4583 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4584 {
78a095c3
JK
4585 if (from_tty
4586 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4587 error (_("Still connected."));
4588 }
4589
78a095c3 4590 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4591 target_preopen (from_tty);
4592
89be2091 4593 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4594 xfree (rs->last_pass_packet);
4595 rs->last_pass_packet = NULL;
89be2091 4596
9b224c5e
PA
4597 /* Make sure we send the program signals list the next time we
4598 resume. */
5e4a05c4
TT
4599 xfree (rs->last_program_signals_packet);
4600 rs->last_program_signals_packet = NULL;
9b224c5e 4601
ad9a8f3f 4602 remote_fileio_reset ();
1dd41f16
NS
4603 reopen_exec_file ();
4604 reread_symbols ();
4605
5d93a237
TT
4606 rs->remote_desc = remote_serial_open (name);
4607 if (!rs->remote_desc)
c906108c
SS
4608 perror_with_name (name);
4609
4610 if (baud_rate != -1)
4611 {
5d93a237 4612 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4613 {
9b74d5d3
KB
4614 /* The requested speed could not be set. Error out to
4615 top level after closing remote_desc. Take care to
4616 set remote_desc to NULL to avoid closing remote_desc
4617 more than once. */
5d93a237
TT
4618 serial_close (rs->remote_desc);
4619 rs->remote_desc = NULL;
c906108c
SS
4620 perror_with_name (name);
4621 }
4622 }
4623
236af5e3 4624 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4625 serial_raw (rs->remote_desc);
c906108c
SS
4626
4627 /* If there is something sitting in the buffer we might take it as a
4628 response to a command, which would be bad. */
5d93a237 4629 serial_flush_input (rs->remote_desc);
c906108c
SS
4630
4631 if (from_tty)
4632 {
4633 puts_filtered ("Remote debugging using ");
4634 puts_filtered (name);
4635 puts_filtered ("\n");
4636 }
23860348 4637 push_target (target); /* Switch to using remote target now. */
c906108c 4638
74531fed
PA
4639 /* Register extra event sources in the event loop. */
4640 remote_async_inferior_event_token
4641 = create_async_event_handler (remote_async_inferior_event_handler,
4642 NULL);
5965e028 4643 rs->notif_state = remote_notif_state_allocate ();
74531fed 4644
be2a5f71
DJ
4645 /* Reset the target state; these things will be queried either by
4646 remote_query_supported or as they are needed. */
ca4f7f8b 4647 reset_all_packet_configs_support ();
74531fed 4648 rs->cached_wait_status = 0;
be2a5f71 4649 rs->explicit_packet_size = 0;
a6f3e723 4650 rs->noack_mode = 0;
82f73884 4651 rs->extended = extended_p;
e24a49d8 4652 rs->waiting_for_stop_reply = 0;
3a29589a 4653 rs->ctrlc_pending_p = 0;
802188a7 4654
47f8a51d
TT
4655 rs->general_thread = not_sent_ptid;
4656 rs->continue_thread = not_sent_ptid;
262e1174 4657 rs->remote_traceframe_number = -1;
c906108c 4658
9d1f7ab2 4659 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4660 rs->use_threadinfo_query = 1;
4661 rs->use_threadextra_query = 1;
9d1f7ab2 4662
80152258
PA
4663 readahead_cache_invalidate ();
4664
c6ebd6cf 4665 if (target_async_permitted)
92d1e331 4666 {
23860348 4667 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4668 remote_async_terminal_ours_p = 1;
4669
4670 /* FIXME: cagney/1999-09-23: During the initial connection it is
4671 assumed that the target is already ready and able to respond to
0df8b418 4672 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4673 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4674 around this. Eventually a mechanism that allows
92d1e331 4675 wait_for_inferior() to expect/get timeouts will be
23860348 4676 implemented. */
92d1e331
DJ
4677 wait_forever_enabled_p = 0;
4678 }
4679
23860348 4680 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4681 no_shared_libraries (NULL, 0);
f78f6cf1 4682
74531fed
PA
4683 /* Start afresh. */
4684 init_thread_list ();
4685
36918e70 4686 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4687 target (we'd otherwise be in an inconsistent state) and then
4688 propogate the error on up the exception chain. This ensures that
4689 the caller doesn't stumble along blindly assuming that the
4690 function succeeded. The CLI doesn't have this problem but other
4691 UI's, such as MI do.
36918e70
AC
4692
4693 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4694 this function should return an error indication letting the
ce2826aa 4695 caller restore the previous state. Unfortunately the command
36918e70
AC
4696 ``target remote'' is directly wired to this function making that
4697 impossible. On a positive note, the CLI side of this problem has
4698 been fixed - the function set_cmd_context() makes it possible for
4699 all the ``target ....'' commands to share a common callback
4700 function. See cli-dump.c. */
109c3e39 4701 {
2d717e4f 4702
492d29ea 4703 TRY
04bd08de
TT
4704 {
4705 remote_start_remote (from_tty, target, extended_p);
4706 }
492d29ea 4707 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4708 {
c8d104ad
PA
4709 /* Pop the partially set up target - unless something else did
4710 already before throwing the exception. */
5d93a237 4711 if (rs->remote_desc != NULL)
78a095c3 4712 remote_unpush_target ();
c6ebd6cf 4713 if (target_async_permitted)
109c3e39
AC
4714 wait_forever_enabled_p = 1;
4715 throw_exception (ex);
4716 }
492d29ea 4717 END_CATCH
109c3e39 4718 }
c906108c 4719
f4abbc16
MM
4720 remote_btrace_reset ();
4721
c6ebd6cf 4722 if (target_async_permitted)
92d1e331 4723 wait_forever_enabled_p = 1;
43ff13b4
JM
4724}
4725
de0d863e
DB
4726/* Detach the specified process. */
4727
4728static void
4729remote_detach_pid (int pid)
4730{
4731 struct remote_state *rs = get_remote_state ();
4732
4733 if (remote_multi_process_p (rs))
4734 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4735 else
4736 strcpy (rs->buf, "D");
4737
4738 putpkt (rs->buf);
4739 getpkt (&rs->buf, &rs->buf_size, 0);
4740
4741 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4742 ;
4743 else if (rs->buf[0] == '\0')
4744 error (_("Remote doesn't know how to detach"));
4745 else
4746 error (_("Can't detach process."));
4747}
4748
4749/* This detaches a program to which we previously attached, using
4750 inferior_ptid to identify the process. After this is done, GDB
4751 can be used to debug some other program. We better not have left
4752 any breakpoints in the target program or it'll die when it hits
4753 one. */
c906108c
SS
4754
4755static void
de0d863e 4756remote_detach_1 (const char *args, int from_tty)
c906108c 4757{
82f73884 4758 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4759 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4760 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4761 int is_fork_parent;
c906108c
SS
4762
4763 if (args)
8a3fe4f8 4764 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4765
2d717e4f
DJ
4766 if (!target_has_execution)
4767 error (_("No process to detach from."));
4768
7cee1e54
PA
4769 if (from_tty)
4770 {
4771 char *exec_file = get_exec_file (0);
4772 if (exec_file == NULL)
4773 exec_file = "";
4774 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4775 target_pid_to_str (pid_to_ptid (pid)));
4776 gdb_flush (gdb_stdout);
4777 }
4778
c906108c 4779 /* Tell the remote target to detach. */
de0d863e 4780 remote_detach_pid (pid);
82f73884 4781
de0d863e 4782 if (from_tty && !rs->extended)
7cee1e54 4783 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4784
de0d863e
DB
4785 /* Check to see if we are detaching a fork parent. Note that if we
4786 are detaching a fork child, tp == NULL. */
4787 is_fork_parent = (tp != NULL
4788 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4789
4790 /* If doing detach-on-fork, we don't mourn, because that will delete
4791 breakpoints that should be available for the followed inferior. */
4792 if (!is_fork_parent)
4793 target_mourn_inferior ();
4794 else
4795 {
4796 inferior_ptid = null_ptid;
4797 detach_inferior (pid);
4798 }
2d717e4f
DJ
4799}
4800
4801static void
52554a0e 4802remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4803{
de0d863e 4804 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4805}
4806
4807static void
52554a0e 4808extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4809{
de0d863e
DB
4810 remote_detach_1 (args, from_tty);
4811}
4812
4813/* Target follow-fork function for remote targets. On entry, and
4814 at return, the current inferior is the fork parent.
4815
4816 Note that although this is currently only used for extended-remote,
4817 it is named remote_follow_fork in anticipation of using it for the
4818 remote target as well. */
4819
4820static int
4821remote_follow_fork (struct target_ops *ops, int follow_child,
4822 int detach_fork)
4823{
4824 struct remote_state *rs = get_remote_state ();
c269dbdb 4825 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4826
c269dbdb
DB
4827 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4828 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4829 {
4830 /* When following the parent and detaching the child, we detach
4831 the child here. For the case of following the child and
4832 detaching the parent, the detach is done in the target-
4833 independent follow fork code in infrun.c. We can't use
4834 target_detach when detaching an unfollowed child because
4835 the client side doesn't know anything about the child. */
4836 if (detach_fork && !follow_child)
4837 {
4838 /* Detach the fork child. */
4839 ptid_t child_ptid;
4840 pid_t child_pid;
4841
4842 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4843 child_pid = ptid_get_pid (child_ptid);
4844
4845 remote_detach_pid (child_pid);
4846 detach_inferior (child_pid);
4847 }
4848 }
4849 return 0;
c906108c
SS
4850}
4851
94585166
DB
4852/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
4853 in the program space of the new inferior. On entry and at return the
4854 current inferior is the exec'ing inferior. INF is the new exec'd
4855 inferior, which may be the same as the exec'ing inferior unless
4856 follow-exec-mode is "new". */
4857
4858static void
4859remote_follow_exec (struct target_ops *ops,
4860 struct inferior *inf, char *execd_pathname)
4861{
4862 /* We know that this is a target file name, so if it has the "target:"
4863 prefix we strip it off before saving it in the program space. */
4864 if (is_target_filename (execd_pathname))
4865 execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
4866
4867 set_pspace_remote_exec_file (inf->pspace, execd_pathname);
4868}
4869
6ad8ae5c
DJ
4870/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4871
43ff13b4 4872static void
fee354ee 4873remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4874{
43ff13b4 4875 if (args)
2d717e4f 4876 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4877
2d717e4f 4878 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4879 won't do it. So call remote_mourn directly instead of
2d717e4f 4880 target_mourn_inferior. */
20f796c9 4881 remote_mourn (target);
2d717e4f 4882
43ff13b4
JM
4883 if (from_tty)
4884 puts_filtered ("Ending remote debugging.\n");
4885}
4886
2d717e4f
DJ
4887/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4888 be chatty about it. */
4889
4890static void
20f796c9
GB
4891extended_remote_attach (struct target_ops *target, const char *args,
4892 int from_tty)
2d717e4f
DJ
4893{
4894 struct remote_state *rs = get_remote_state ();
be86555c 4895 int pid;
96ef3384 4896 char *wait_status = NULL;
2d717e4f 4897
74164c56 4898 pid = parse_pid_to_attach (args);
2d717e4f 4899
74164c56
JK
4900 /* Remote PID can be freely equal to getpid, do not check it here the same
4901 way as in other targets. */
2d717e4f 4902
4082afcc 4903 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4904 error (_("This target does not support attaching to a process"));
4905
7cee1e54
PA
4906 if (from_tty)
4907 {
4908 char *exec_file = get_exec_file (0);
4909
4910 if (exec_file)
4911 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4912 target_pid_to_str (pid_to_ptid (pid)));
4913 else
4914 printf_unfiltered (_("Attaching to %s\n"),
4915 target_pid_to_str (pid_to_ptid (pid)));
4916
4917 gdb_flush (gdb_stdout);
4918 }
4919
bba74b36 4920 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4921 putpkt (rs->buf);
4922 getpkt (&rs->buf, &rs->buf_size, 0);
4923
4082afcc
PA
4924 switch (packet_ok (rs->buf,
4925 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4926 {
4082afcc 4927 case PACKET_OK:
74531fed
PA
4928 if (!non_stop)
4929 {
4930 /* Save the reply for later. */
4931 wait_status = alloca (strlen (rs->buf) + 1);
4932 strcpy (wait_status, rs->buf);
4933 }
4934 else if (strcmp (rs->buf, "OK") != 0)
4935 error (_("Attaching to %s failed with: %s"),
4936 target_pid_to_str (pid_to_ptid (pid)),
4937 rs->buf);
4082afcc
PA
4938 break;
4939 case PACKET_UNKNOWN:
4940 error (_("This target does not support attaching to a process"));
4941 default:
4942 error (_("Attaching to %s failed"),
4943 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4944 }
2d717e4f 4945
1b6e6f5c 4946 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4947
2d717e4f 4948 inferior_ptid = pid_to_ptid (pid);
79d7f229 4949
bad34192
PA
4950 if (non_stop)
4951 {
4952 struct thread_info *thread;
79d7f229 4953
bad34192 4954 /* Get list of threads. */
e8032dde 4955 remote_update_thread_list (target);
82f73884 4956
bad34192
PA
4957 thread = first_thread_of_process (pid);
4958 if (thread)
4959 inferior_ptid = thread->ptid;
4960 else
4961 inferior_ptid = pid_to_ptid (pid);
4962
4963 /* Invalidate our notion of the remote current thread. */
47f8a51d 4964 record_currthread (rs, minus_one_ptid);
bad34192 4965 }
74531fed 4966 else
bad34192
PA
4967 {
4968 /* Now, if we have thread information, update inferior_ptid. */
4969 inferior_ptid = remote_current_thread (inferior_ptid);
4970
4971 /* Add the main thread to the thread list. */
4972 add_thread_silent (inferior_ptid);
4973 }
c0a2216e 4974
96ef3384
UW
4975 /* Next, if the target can specify a description, read it. We do
4976 this before anything involving memory or registers. */
4977 target_find_description ();
4978
74531fed
PA
4979 if (!non_stop)
4980 {
4981 /* Use the previously fetched status. */
4982 gdb_assert (wait_status != NULL);
4983
4984 if (target_can_async_p ())
4985 {
722247f1
YQ
4986 struct notif_event *reply
4987 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4988
722247f1 4989 push_stop_reply ((struct stop_reply *) reply);
74531fed 4990
6a3753b3 4991 target_async (1);
74531fed
PA
4992 }
4993 else
4994 {
4995 gdb_assert (wait_status != NULL);
4996 strcpy (rs->buf, wait_status);
4997 rs->cached_wait_status = 1;
4998 }
4999 }
5000 else
5001 gdb_assert (wait_status == NULL);
2d717e4f
DJ
5002}
5003
b9c1d481
AS
5004/* Implementation of the to_post_attach method. */
5005
5006static void
5007extended_remote_post_attach (struct target_ops *ops, int pid)
5008{
5009 /* In certain cases GDB might not have had the chance to start
5010 symbol lookup up until now. This could happen if the debugged
5011 binary is not using shared libraries, the vsyscall page is not
5012 present (on Linux) and the binary itself hadn't changed since the
5013 debugging process was started. */
5014 if (symfile_objfile != NULL)
5015 remote_check_symbols();
5016}
5017
c906108c 5018\f
506fb367
DJ
5019/* Check for the availability of vCont. This function should also check
5020 the response. */
c906108c
SS
5021
5022static void
6d820c5c 5023remote_vcont_probe (struct remote_state *rs)
c906108c 5024{
2e9f7625 5025 char *buf;
6d820c5c 5026
2e9f7625
DJ
5027 strcpy (rs->buf, "vCont?");
5028 putpkt (rs->buf);
6d820c5c 5029 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 5030 buf = rs->buf;
c906108c 5031
506fb367 5032 /* Make sure that the features we assume are supported. */
61012eef 5033 if (startswith (buf, "vCont"))
506fb367
DJ
5034 {
5035 char *p = &buf[5];
5036 int support_s, support_S, support_c, support_C;
5037
5038 support_s = 0;
5039 support_S = 0;
5040 support_c = 0;
5041 support_C = 0;
d458bd84 5042 rs->supports_vCont.t = 0;
c1e36e3e 5043 rs->supports_vCont.r = 0;
506fb367
DJ
5044 while (p && *p == ';')
5045 {
5046 p++;
5047 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
5048 support_s = 1;
5049 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
5050 support_S = 1;
5051 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
5052 support_c = 1;
5053 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
5054 support_C = 1;
74531fed 5055 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 5056 rs->supports_vCont.t = 1;
c1e36e3e
PA
5057 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
5058 rs->supports_vCont.r = 1;
506fb367
DJ
5059
5060 p = strchr (p, ';');
5061 }
c906108c 5062
506fb367
DJ
5063 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
5064 BUF will make packet_ok disable the packet. */
5065 if (!support_s || !support_S || !support_c || !support_C)
5066 buf[0] = 0;
5067 }
c906108c 5068
444abaca 5069 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 5070}
c906108c 5071
0d8f58ca
PA
5072/* Helper function for building "vCont" resumptions. Write a
5073 resumption to P. ENDP points to one-passed-the-end of the buffer
5074 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
5075 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
5076 resumed thread should be single-stepped and/or signalled. If PTID
5077 equals minus_one_ptid, then all threads are resumed; if PTID
5078 represents a process, then all threads of the process are resumed;
5079 the thread to be stepped and/or signalled is given in the global
5080 INFERIOR_PTID. */
5081
5082static char *
5083append_resumption (char *p, char *endp,
2ea28649 5084 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
5085{
5086 struct remote_state *rs = get_remote_state ();
5087
a493e3e2 5088 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 5089 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
5090 else if (step
5091 /* GDB is willing to range step. */
5092 && use_range_stepping
5093 /* Target supports range stepping. */
5094 && rs->supports_vCont.r
5095 /* We don't currently support range stepping multiple
5096 threads with a wildcard (though the protocol allows it,
5097 so stubs shouldn't make an active effort to forbid
5098 it). */
5099 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
5100 {
5101 struct thread_info *tp;
5102
5103 if (ptid_equal (ptid, minus_one_ptid))
5104 {
5105 /* If we don't know about the target thread's tid, then
5106 we're resuming magic_null_ptid (see caller). */
5107 tp = find_thread_ptid (magic_null_ptid);
5108 }
5109 else
5110 tp = find_thread_ptid (ptid);
5111 gdb_assert (tp != NULL);
5112
5113 if (tp->control.may_range_step)
5114 {
5115 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
5116
5117 p += xsnprintf (p, endp - p, ";r%s,%s",
5118 phex_nz (tp->control.step_range_start,
5119 addr_size),
5120 phex_nz (tp->control.step_range_end,
5121 addr_size));
5122 }
5123 else
5124 p += xsnprintf (p, endp - p, ";s");
5125 }
0d8f58ca
PA
5126 else if (step)
5127 p += xsnprintf (p, endp - p, ";s");
a493e3e2 5128 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
5129 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
5130 else
5131 p += xsnprintf (p, endp - p, ";c");
5132
5133 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
5134 {
5135 ptid_t nptid;
5136
5137 /* All (-1) threads of process. */
ba348170 5138 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
5139
5140 p += xsnprintf (p, endp - p, ":");
5141 p = write_ptid (p, endp, nptid);
5142 }
5143 else if (!ptid_equal (ptid, minus_one_ptid))
5144 {
5145 p += xsnprintf (p, endp - p, ":");
5146 p = write_ptid (p, endp, ptid);
5147 }
5148
5149 return p;
5150}
5151
e5ef252a
PA
5152/* Append a vCont continue-with-signal action for threads that have a
5153 non-zero stop signal. */
5154
5155static char *
5156append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
5157{
5158 struct thread_info *thread;
5159
034f788c 5160 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
5161 if (ptid_match (thread->ptid, ptid)
5162 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 5163 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
5164 {
5165 p = append_resumption (p, endp, thread->ptid,
5166 0, thread->suspend.stop_signal);
5167 thread->suspend.stop_signal = GDB_SIGNAL_0;
5168 }
5169
5170 return p;
5171}
5172
506fb367
DJ
5173/* Resume the remote inferior by using a "vCont" packet. The thread
5174 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
5175 resumed thread should be single-stepped and/or signalled. If PTID
5176 equals minus_one_ptid, then all threads are resumed; the thread to
5177 be stepped and/or signalled is given in the global INFERIOR_PTID.
5178 This function returns non-zero iff it resumes the inferior.
44eaed12 5179
506fb367
DJ
5180 This function issues a strict subset of all possible vCont commands at the
5181 moment. */
44eaed12 5182
506fb367 5183static int
2ea28649 5184remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5185{
5186 struct remote_state *rs = get_remote_state ();
82f73884
PA
5187 char *p;
5188 char *endp;
44eaed12 5189
4082afcc 5190 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5191 remote_vcont_probe (rs);
44eaed12 5192
4082afcc 5193 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5194 return 0;
44eaed12 5195
82f73884
PA
5196 p = rs->buf;
5197 endp = rs->buf + get_remote_packet_size ();
5198
506fb367
DJ
5199 /* If we could generate a wider range of packets, we'd have to worry
5200 about overflowing BUF. Should there be a generic
5201 "multi-part-packet" packet? */
5202
0d8f58ca
PA
5203 p += xsnprintf (p, endp - p, "vCont");
5204
79d7f229 5205 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5206 {
79d7f229
PA
5207 /* MAGIC_NULL_PTID means that we don't have any active threads,
5208 so we don't have any TID numbers the inferior will
5209 understand. Make sure to only send forms that do not specify
5210 a TID. */
a9cbf802 5211 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5212 }
0d8f58ca 5213 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5214 {
0d8f58ca
PA
5215 /* Resume all threads (of all processes, or of a single
5216 process), with preference for INFERIOR_PTID. This assumes
5217 inferior_ptid belongs to the set of all threads we are about
5218 to resume. */
a493e3e2 5219 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5220 {
0d8f58ca
PA
5221 /* Step inferior_ptid, with or without signal. */
5222 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5223 }
0d8f58ca 5224
e5ef252a
PA
5225 /* Also pass down any pending signaled resumption for other
5226 threads not the current. */
5227 p = append_pending_thread_resumptions (p, endp, ptid);
5228
0d8f58ca 5229 /* And continue others without a signal. */
a493e3e2 5230 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5231 }
5232 else
506fb367
DJ
5233 {
5234 /* Scheduler locking; resume only PTID. */
a9cbf802 5235 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5236 }
c906108c 5237
82f73884
PA
5238 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5239 putpkt (rs->buf);
506fb367 5240
74531fed
PA
5241 if (non_stop)
5242 {
5243 /* In non-stop, the stub replies to vCont with "OK". The stop
5244 reply will be reported asynchronously by means of a `%Stop'
5245 notification. */
5246 getpkt (&rs->buf, &rs->buf_size, 0);
5247 if (strcmp (rs->buf, "OK") != 0)
5248 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5249 }
5250
506fb367 5251 return 1;
c906108c 5252}
43ff13b4 5253
506fb367
DJ
5254/* Tell the remote machine to resume. */
5255
43ff13b4 5256static void
28439f5e 5257remote_resume (struct target_ops *ops,
2ea28649 5258 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5259{
d01949b6 5260 struct remote_state *rs = get_remote_state ();
2e9f7625 5261 char *buf;
43ff13b4 5262
722247f1
YQ
5263 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5264 (explained in remote-notif.c:handle_notification) so
5265 remote_notif_process is not called. We need find a place where
5266 it is safe to start a 'vNotif' sequence. It is good to do it
5267 before resuming inferior, because inferior was stopped and no RSP
5268 traffic at that moment. */
5269 if (!non_stop)
5965e028 5270 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5271
b73be471 5272 rs->last_sent_signal = siggnal;
280ceea3 5273 rs->last_sent_step = step;
43ff13b4 5274
506fb367 5275 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5276 /* No reverse support (yet) for vCont. */
5277 if (execution_direction != EXEC_REVERSE)
5278 if (remote_vcont_resume (ptid, step, siggnal))
5279 goto done;
506fb367 5280
79d7f229
PA
5281 /* All other supported resume packets do use Hc, so set the continue
5282 thread. */
5283 if (ptid_equal (ptid, minus_one_ptid))
5284 set_continue_thread (any_thread_ptid);
506fb367 5285 else
79d7f229 5286 set_continue_thread (ptid);
506fb367 5287
2e9f7625 5288 buf = rs->buf;
b2175913
MS
5289 if (execution_direction == EXEC_REVERSE)
5290 {
5291 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5292 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5293 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5294 siggnal);
40ab02ce 5295
4082afcc 5296 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5297 error (_("Remote reverse-step not supported."));
4082afcc 5298 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5299 error (_("Remote reverse-continue not supported."));
40ab02ce 5300
b2175913
MS
5301 strcpy (buf, step ? "bs" : "bc");
5302 }
a493e3e2 5303 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5304 {
5305 buf[0] = step ? 'S' : 'C';
c5aa993b 5306 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5307 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5308 buf[3] = '\0';
5309 }
5310 else
c5aa993b 5311 strcpy (buf, step ? "s" : "c");
506fb367 5312
44eaed12 5313 putpkt (buf);
43ff13b4 5314
75c99385 5315 done:
2acceee2 5316 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5317 with the event loop. NOTE: this is the one place where all the
5318 execution commands end up. We could alternatively do this in each
23860348 5319 of the execution commands in infcmd.c. */
2acceee2
JM
5320 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5321 into infcmd.c in order to allow inferior function calls to work
23860348 5322 NOT asynchronously. */
362646f5 5323 if (target_can_async_p ())
6a3753b3 5324 target_async (1);
e24a49d8
PA
5325
5326 /* We've just told the target to resume. The remote server will
5327 wait for the inferior to stop, and then send a stop reply. In
5328 the mean time, we can't start another command/query ourselves
74531fed
PA
5329 because the stub wouldn't be ready to process it. This applies
5330 only to the base all-stop protocol, however. In non-stop (which
5331 only supports vCont), the stub replies with an "OK", and is
5332 immediate able to process further serial input. */
5333 if (!non_stop)
5334 rs->waiting_for_stop_reply = 1;
43ff13b4 5335}
c906108c 5336\f
43ff13b4
JM
5337
5338/* Set up the signal handler for SIGINT, while the target is
23860348 5339 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5340static void
934b9bac 5341async_initialize_sigint_signal_handler (void)
43ff13b4 5342{
934b9bac 5343 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5344}
5345
23860348 5346/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5347static void
934b9bac 5348async_handle_remote_sigint (int sig)
43ff13b4 5349{
934b9bac 5350 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5351 /* Note we need to go through gdb_call_async_signal_handler in order
5352 to wake up the event loop on Windows. */
5353 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5354}
5355
5356/* Signal handler for SIGINT, installed after SIGINT has already been
5357 sent once. It will take effect the second time that the user sends
23860348 5358 a ^C. */
43ff13b4 5359static void
934b9bac 5360async_handle_remote_sigint_twice (int sig)
43ff13b4 5361{
934b9bac 5362 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5363 /* See note in async_handle_remote_sigint. */
5364 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5365}
5366
abc56d60
PA
5367/* Implementation of to_check_pending_interrupt. */
5368
5369static void
5370remote_check_pending_interrupt (struct target_ops *self)
5371{
5372 struct async_signal_handler *token = async_sigint_remote_twice_token;
5373
5374 if (async_signal_handler_is_marked (token))
5375 {
5376 clear_async_signal_handler (token);
5377 call_async_signal_handler (token);
5378 }
5379}
5380
6426a772 5381/* Perform the real interruption of the target execution, in response
23860348 5382 to a ^C. */
c5aa993b 5383static void
fba45db2 5384async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5385{
5386 if (remote_debug)
248fd3bf 5387 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5388
94cc34af 5389 target_stop (inferior_ptid);
43ff13b4
JM
5390}
5391
0df8b418 5392/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5393 up on the target alltogether. */
47e1ce27 5394static void
fba45db2 5395async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5396{
2df3850c 5397 if (remote_debug)
248fd3bf 5398 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5399
5400 interrupt_query ();
43ff13b4
JM
5401}
5402
5403/* Reinstall the usual SIGINT handlers, after the target has
23860348 5404 stopped. */
6426a772 5405static void
934b9bac 5406async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5407{
5408 signal (SIGINT, handle_sigint);
43ff13b4
JM
5409}
5410
c906108c
SS
5411/* Send ^C to target to halt it. Target will respond, and send us a
5412 packet. */
507f3c78 5413static void (*ofunc) (int);
c906108c 5414
bfedc46a
PA
5415/* The command line interface's interrupt routine. This function is installed
5416 as a signal handler for SIGINT. The first time a user requests an
5417 interrupt, we call remote_interrupt to send a break or ^C. If there is no
7a292a7a 5418 response from the target (it didn't stop when the user requested it),
23860348 5419 we ask the user if he'd like to detach from the target. */
bfedc46a 5420
c906108c 5421static void
934b9bac 5422sync_remote_interrupt (int signo)
c906108c 5423{
23860348 5424 /* If this doesn't work, try more severe steps. */
934b9bac 5425 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5426
934b9bac 5427 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5428}
5429
5430/* The user typed ^C twice. */
5431
5432static void
934b9bac 5433sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5434{
5435 signal (signo, ofunc);
934b9bac
JK
5436 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5437 signal (signo, sync_remote_interrupt);
c906108c 5438}
7a292a7a 5439
74531fed
PA
5440/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5441 thread, all threads of a remote process, or all threads of all
5442 processes. */
5443
5444static void
5445remote_stop_ns (ptid_t ptid)
5446{
5447 struct remote_state *rs = get_remote_state ();
5448 char *p = rs->buf;
5449 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5450
4082afcc 5451 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5452 remote_vcont_probe (rs);
5453
d458bd84 5454 if (!rs->supports_vCont.t)
74531fed
PA
5455 error (_("Remote server does not support stopping threads"));
5456
f91d3df5
PA
5457 if (ptid_equal (ptid, minus_one_ptid)
5458 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5459 p += xsnprintf (p, endp - p, "vCont;t");
5460 else
5461 {
5462 ptid_t nptid;
5463
74531fed
PA
5464 p += xsnprintf (p, endp - p, "vCont;t:");
5465
5466 if (ptid_is_pid (ptid))
5467 /* All (-1) threads of process. */
ba348170 5468 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5469 else
5470 {
5471 /* Small optimization: if we already have a stop reply for
5472 this thread, no use in telling the stub we want this
5473 stopped. */
5474 if (peek_stop_reply (ptid))
5475 return;
5476
5477 nptid = ptid;
5478 }
5479
a9cbf802 5480 write_ptid (p, endp, nptid);
74531fed
PA
5481 }
5482
5483 /* In non-stop, we get an immediate OK reply. The stop reply will
5484 come in asynchronously by notification. */
5485 putpkt (rs->buf);
5486 getpkt (&rs->buf, &rs->buf_size, 0);
5487 if (strcmp (rs->buf, "OK") != 0)
5488 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5489}
5490
bfedc46a
PA
5491/* All-stop version of target_interrupt. Sends a break or a ^C to
5492 interrupt the remote target. It is undefined which thread of which
5493 process reports the interrupt. */
74531fed
PA
5494
5495static void
bfedc46a 5496remote_interrupt_as (ptid_t ptid)
74531fed
PA
5497{
5498 struct remote_state *rs = get_remote_state ();
5499
3a29589a
DJ
5500 rs->ctrlc_pending_p = 1;
5501
74531fed
PA
5502 /* If the inferior is stopped already, but the core didn't know
5503 about it yet, just ignore the request. The cached wait status
5504 will be collected in remote_wait. */
5505 if (rs->cached_wait_status)
5506 return;
5507
9a7071a8
JB
5508 /* Send interrupt_sequence to remote target. */
5509 send_interrupt_sequence ();
74531fed
PA
5510}
5511
bfedc46a 5512/* Implement the to_stop function for the remote targets. */
74531fed 5513
c906108c 5514static void
1eab8a48 5515remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5516{
7a292a7a 5517 if (remote_debug)
0f71a2f6 5518 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5519
74531fed
PA
5520 if (non_stop)
5521 remote_stop_ns (ptid);
c906108c 5522 else
bfedc46a
PA
5523 {
5524 /* We don't currently have a way to transparently pause the
5525 remote target in all-stop mode. Interrupt it instead. */
5526 remote_interrupt_as (ptid);
5527 }
5528}
5529
5530/* Implement the to_interrupt function for the remote targets. */
5531
5532static void
5533remote_interrupt (struct target_ops *self, ptid_t ptid)
5534{
5535 if (remote_debug)
5536 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
5537
5538 if (non_stop)
5539 {
5540 /* We don't currently have a way to ^C the remote target in
5541 non-stop mode. Stop it (with no signal) instead. */
5542 remote_stop_ns (ptid);
5543 }
5544 else
5545 remote_interrupt_as (ptid);
c906108c
SS
5546}
5547
5548/* Ask the user what to do when an interrupt is received. */
5549
5550static void
fba45db2 5551interrupt_query (void)
c906108c 5552{
abc56d60
PA
5553 struct remote_state *rs = get_remote_state ();
5554 struct cleanup *old_chain;
5555
5556 old_chain = make_cleanup_restore_target_terminal ();
c906108c
SS
5557 target_terminal_ours ();
5558
abc56d60 5559 if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
74531fed 5560 {
abc56d60
PA
5561 if (query (_("The target is not responding to interrupt requests.\n"
5562 "Stop debugging it? ")))
74531fed 5563 {
78a095c3 5564 remote_unpush_target ();
abc56d60 5565 throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
74531fed
PA
5566 }
5567 }
abc56d60
PA
5568 else
5569 {
5570 if (query (_("Interrupted while waiting for the program.\n"
5571 "Give up waiting? ")))
5572 quit ();
5573 }
c906108c 5574
abc56d60 5575 do_cleanups (old_chain);
c906108c
SS
5576}
5577
6426a772
JM
5578/* Enable/disable target terminal ownership. Most targets can use
5579 terminal groups to control terminal ownership. Remote targets are
5580 different in that explicit transfer of ownership to/from GDB/target
23860348 5581 is required. */
6426a772
JM
5582
5583static void
d2f640d4 5584remote_terminal_inferior (struct target_ops *self)
6426a772 5585{
c6ebd6cf 5586 if (!target_async_permitted)
75c99385
PA
5587 /* Nothing to do. */
5588 return;
5589
d9d2d8b6
PA
5590 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5591 idempotent. The event-loop GDB talking to an asynchronous target
5592 with a synchronous command calls this function from both
5593 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5594 transfer the terminal to the target when it shouldn't this guard
5595 can go away. */
6426a772
JM
5596 if (!remote_async_terminal_ours_p)
5597 return;
5598 delete_file_handler (input_fd);
5599 remote_async_terminal_ours_p = 0;
934b9bac 5600 async_initialize_sigint_signal_handler ();
6426a772
JM
5601 /* NOTE: At this point we could also register our selves as the
5602 recipient of all input. Any characters typed could then be
23860348 5603 passed on down to the target. */
6426a772
JM
5604}
5605
5606static void
e3594fd1 5607remote_terminal_ours (struct target_ops *self)
6426a772 5608{
c6ebd6cf 5609 if (!target_async_permitted)
75c99385
PA
5610 /* Nothing to do. */
5611 return;
5612
5613 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5614 if (remote_async_terminal_ours_p)
5615 return;
934b9bac 5616 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5617 add_file_handler (input_fd, stdin_event_handler, 0);
5618 remote_async_terminal_ours_p = 1;
5619}
5620
176a6961 5621static void
917317f4 5622remote_console_output (char *msg)
c906108c
SS
5623{
5624 char *p;
5625
c5aa993b 5626 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5627 {
5628 char tb[2];
5629 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5630
c906108c
SS
5631 tb[0] = c;
5632 tb[1] = 0;
43ff13b4 5633 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5634 }
00db5b94
PA
5635 gdb_flush (gdb_stdtarg);
5636}
74531fed
PA
5637
5638typedef struct cached_reg
5639{
5640 int num;
5641 gdb_byte data[MAX_REGISTER_SIZE];
5642} cached_reg_t;
5643
5644DEF_VEC_O(cached_reg_t);
5645
722247f1 5646typedef struct stop_reply
74531fed 5647{
722247f1 5648 struct notif_event base;
74531fed 5649
722247f1 5650 /* The identifier of the thread about this event */
74531fed
PA
5651 ptid_t ptid;
5652
340e3c99 5653 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5654 connection, represented by a remote_state object, is closed,
5655 all the associated stop_reply events should be released. */
5656 struct remote_state *rs;
5657
74531fed
PA
5658 struct target_waitstatus ws;
5659
15148d6a
PA
5660 /* Expedited registers. This makes remote debugging a bit more
5661 efficient for those targets that provide critical registers as
5662 part of their normal status mechanism (as another roundtrip to
5663 fetch them is avoided). */
74531fed
PA
5664 VEC(cached_reg_t) *regcache;
5665
f7e6eed5
PA
5666 enum target_stop_reason stop_reason;
5667
74531fed
PA
5668 CORE_ADDR watch_data_address;
5669
dc146f7c 5670 int core;
722247f1 5671} *stop_reply_p;
a744cf53 5672
722247f1
YQ
5673DECLARE_QUEUE_P (stop_reply_p);
5674DEFINE_QUEUE_P (stop_reply_p);
5675/* The list of already fetched and acknowledged stop events. This
5676 queue is used for notification Stop, and other notifications
5677 don't need queue for their events, because the notification events
5678 of Stop can't be consumed immediately, so that events should be
5679 queued first, and be consumed by remote_wait_{ns,as} one per
5680 time. Other notifications can consume their events immediately,
5681 so queue is not needed for them. */
5682static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5683
5684static void
5685stop_reply_xfree (struct stop_reply *r)
5686{
f48ff2a7 5687 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5688}
5689
221e1a37
PA
5690/* Return the length of the stop reply queue. */
5691
5692static int
5693stop_reply_queue_length (void)
5694{
5695 return QUEUE_length (stop_reply_p, stop_reply_queue);
5696}
5697
722247f1
YQ
5698static void
5699remote_notif_stop_parse (struct notif_client *self, char *buf,
5700 struct notif_event *event)
5701{
5702 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5703}
5704
5705static void
5706remote_notif_stop_ack (struct notif_client *self, char *buf,
5707 struct notif_event *event)
5708{
5709 struct stop_reply *stop_reply = (struct stop_reply *) event;
5710
5711 /* acknowledge */
5712 putpkt ((char *) self->ack_command);
5713
5714 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5715 /* We got an unknown stop reply. */
5716 error (_("Unknown stop reply"));
5717
5718 push_stop_reply (stop_reply);
5719}
5720
5721static int
5722remote_notif_stop_can_get_pending_events (struct notif_client *self)
5723{
5724 /* We can't get pending events in remote_notif_process for
5725 notification stop, and we have to do this in remote_wait_ns
5726 instead. If we fetch all queued events from stub, remote stub
5727 may exit and we have no chance to process them back in
5728 remote_wait_ns. */
5729 mark_async_event_handler (remote_async_inferior_event_token);
5730 return 0;
5731}
5732
5733static void
5734stop_reply_dtr (struct notif_event *event)
5735{
5736 struct stop_reply *r = (struct stop_reply *) event;
5737
5738 VEC_free (cached_reg_t, r->regcache);
5739}
5740
5741static struct notif_event *
5742remote_notif_stop_alloc_reply (void)
5743{
8d749320
SM
5744 /* We cast to a pointer to the "base class". */
5745 struct notif_event *r = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5746
5747 r->dtr = stop_reply_dtr;
5748
5749 return r;
5750}
5751
5752/* A client of notification Stop. */
5753
5754struct notif_client notif_client_stop =
5755{
5756 "Stop",
5757 "vStopped",
5758 remote_notif_stop_parse,
5759 remote_notif_stop_ack,
5760 remote_notif_stop_can_get_pending_events,
5761 remote_notif_stop_alloc_reply,
f48ff2a7 5762 REMOTE_NOTIF_STOP,
722247f1
YQ
5763};
5764
5765/* A parameter to pass data in and out. */
5766
5767struct queue_iter_param
5768{
5769 void *input;
5770 struct stop_reply *output;
5771};
5772
cbb8991c
DB
5773/* Determine if THREAD is a pending fork parent thread. ARG contains
5774 the pid of the process that owns the threads we want to check, or
5775 -1 if we want to check all threads. */
5776
5777static int
5778is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5779 ptid_t thread_ptid)
5780{
5781 if (ws->kind == TARGET_WAITKIND_FORKED
5782 || ws->kind == TARGET_WAITKIND_VFORKED)
5783 {
5784 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5785 return 1;
5786 }
5787
5788 return 0;
5789}
5790
5791/* Check whether EVENT is a fork event, and if it is, remove the
5792 fork child from the context list passed in DATA. */
5793
5794static int
5795remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5796 QUEUE_ITER (stop_reply_p) *iter,
5797 stop_reply_p event,
5798 void *data)
5799{
5800 struct queue_iter_param *param = data;
5801 struct threads_listing_context *context = param->input;
5802
5803 if (event->ws.kind == TARGET_WAITKIND_FORKED
5804 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5805 {
5806 threads_listing_context_remove (&event->ws, context);
5807 }
5808
5809 return 1;
5810}
5811
5812/* If CONTEXT contains any fork child threads that have not been
5813 reported yet, remove them from the CONTEXT list. If such a
5814 thread exists it is because we are stopped at a fork catchpoint
5815 and have not yet called follow_fork, which will set up the
5816 host-side data structures for the new process. */
5817
5818static void
5819remove_new_fork_children (struct threads_listing_context *context)
5820{
5821 struct thread_info * thread;
5822 int pid = -1;
5823 struct notif_client *notif = &notif_client_stop;
5824 struct queue_iter_param param;
5825
5826 /* For any threads stopped at a fork event, remove the corresponding
5827 fork child threads from the CONTEXT list. */
5828 ALL_NON_EXITED_THREADS (thread)
5829 {
5830 struct target_waitstatus *ws = &thread->pending_follow;
5831
5832 if (is_pending_fork_parent (ws, pid, thread->ptid))
5833 {
5834 threads_listing_context_remove (ws, context);
5835 }
5836 }
5837
5838 /* Check for any pending fork events (not reported or processed yet)
5839 in process PID and remove those fork child threads from the
5840 CONTEXT list as well. */
5841 remote_notif_get_pending_events (notif);
5842 param.input = context;
5843 param.output = NULL;
5844 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5845 remove_child_of_pending_fork, &param);
5846}
5847
f48ff2a7
YQ
5848/* Remove stop replies in the queue if its pid is equal to the given
5849 inferior's pid. */
722247f1
YQ
5850
5851static int
f48ff2a7
YQ
5852remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5853 QUEUE_ITER (stop_reply_p) *iter,
5854 stop_reply_p event,
5855 void *data)
722247f1
YQ
5856{
5857 struct queue_iter_param *param = data;
5858 struct inferior *inf = param->input;
5859
f48ff2a7 5860 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5861 {
5862 stop_reply_xfree (event);
5863 QUEUE_remove_elem (stop_reply_p, q, iter);
5864 }
5865
5866 return 1;
5867}
5868
f48ff2a7 5869/* Discard all pending stop replies of inferior INF. */
c906108c 5870
74531fed 5871static void
5f4cf0bb 5872discard_pending_stop_replies (struct inferior *inf)
c906108c 5873{
722247f1
YQ
5874 int i;
5875 struct queue_iter_param param;
f48ff2a7
YQ
5876 struct stop_reply *reply;
5877 struct remote_state *rs = get_remote_state ();
5878 struct remote_notif_state *rns = rs->notif_state;
5879
5880 /* This function can be notified when an inferior exists. When the
5881 target is not remote, the notification state is NULL. */
5882 if (rs->remote_desc == NULL)
5883 return;
5884
5885 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5886
74531fed 5887 /* Discard the in-flight notification. */
f48ff2a7 5888 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5889 {
722247f1 5890 stop_reply_xfree (reply);
f48ff2a7 5891 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5892 }
c906108c 5893
722247f1
YQ
5894 param.input = inf;
5895 param.output = NULL;
74531fed
PA
5896 /* Discard the stop replies we have already pulled with
5897 vStopped. */
722247f1 5898 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5899 remove_stop_reply_for_inferior, &param);
5900}
5901
bcc75809
YQ
5902/* If its remote state is equal to the given remote state,
5903 remove EVENT from the stop reply queue. */
5904
5905static int
5906remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5907 QUEUE_ITER (stop_reply_p) *iter,
5908 stop_reply_p event,
5909 void *data)
5910{
5911 struct queue_iter_param *param = data;
5912 struct remote_state *rs = param->input;
5913
5914 if (event->rs == rs)
5915 {
5916 stop_reply_xfree (event);
5917 QUEUE_remove_elem (stop_reply_p, q, iter);
5918 }
5919
5920 return 1;
5921}
5922
5923/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5924
5925static void
bcc75809 5926discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5927{
5928 struct queue_iter_param param;
5929
bcc75809 5930 param.input = rs;
f48ff2a7
YQ
5931 param.output = NULL;
5932 /* Discard the stop replies we have already pulled with
5933 vStopped. */
5934 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5935 remove_stop_reply_of_remote_state, &param);
722247f1 5936}
74531fed 5937
722247f1
YQ
5938/* A parameter to pass data in and out. */
5939
5940static int
5941remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5942 QUEUE_ITER (stop_reply_p) *iter,
5943 stop_reply_p event,
5944 void *data)
5945{
5946 struct queue_iter_param *param = data;
5947 ptid_t *ptid = param->input;
5948
5949 if (ptid_match (event->ptid, *ptid))
5950 {
5951 param->output = event;
5952 QUEUE_remove_elem (stop_reply_p, q, iter);
5953 return 0;
c8e38a49 5954 }
722247f1
YQ
5955
5956 return 1;
74531fed 5957}
43ff13b4 5958
722247f1
YQ
5959/* Remove the first reply in 'stop_reply_queue' which matches
5960 PTID. */
2e9f7625 5961
722247f1
YQ
5962static struct stop_reply *
5963remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5964{
722247f1
YQ
5965 struct queue_iter_param param;
5966
5967 param.input = &ptid;
5968 param.output = NULL;
5969
5970 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5971 remote_notif_remove_once_on_match, &param);
5972 if (notif_debug)
5973 fprintf_unfiltered (gdb_stdlog,
5974 "notif: discard queued event: 'Stop' in %s\n",
5975 target_pid_to_str (ptid));
a744cf53 5976
722247f1 5977 return param.output;
74531fed 5978}
75c99385 5979
74531fed
PA
5980/* Look for a queued stop reply belonging to PTID. If one is found,
5981 remove it from the queue, and return it. Returns NULL if none is
5982 found. If there are still queued events left to process, tell the
5983 event loop to get back to target_wait soon. */
e24a49d8 5984
74531fed
PA
5985static struct stop_reply *
5986queued_stop_reply (ptid_t ptid)
5987{
722247f1 5988 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5989
722247f1 5990 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5991 /* There's still at least an event left. */
5992 mark_async_event_handler (remote_async_inferior_event_token);
5993
722247f1 5994 return r;
74531fed
PA
5995}
5996
5997/* Push a fully parsed stop reply in the stop reply queue. Since we
5998 know that we now have at least one queued event left to pass to the
5999 core side, tell the event loop to get back to target_wait soon. */
6000
6001static void
6002push_stop_reply (struct stop_reply *new_event)
6003{
722247f1 6004 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 6005
722247f1
YQ
6006 if (notif_debug)
6007 fprintf_unfiltered (gdb_stdlog,
6008 "notif: push 'Stop' %s to queue %d\n",
6009 target_pid_to_str (new_event->ptid),
6010 QUEUE_length (stop_reply_p,
6011 stop_reply_queue));
74531fed
PA
6012
6013 mark_async_event_handler (remote_async_inferior_event_token);
6014}
6015
722247f1
YQ
6016static int
6017stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
6018 QUEUE_ITER (stop_reply_p) *iter,
6019 struct stop_reply *event,
6020 void *data)
6021{
6022 ptid_t *ptid = data;
6023
6024 return !(ptid_equal (*ptid, event->ptid)
6025 && event->ws.kind == TARGET_WAITKIND_STOPPED);
6026}
6027
74531fed
PA
6028/* Returns true if we have a stop reply for PTID. */
6029
6030static int
6031peek_stop_reply (ptid_t ptid)
6032{
722247f1
YQ
6033 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
6034 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
6035}
6036
1f10ba14
PA
6037/* Skip PACKET until the next semi-colon (or end of string). */
6038
6039static char *
6040skip_to_semicolon (char *p)
6041{
6042 while (*p != '\0' && *p != ';')
6043 p++;
6044 return p;
6045}
6046
26d56a93
SL
6047/* Helper for remote_parse_stop_reply. Return nonzero if the substring
6048 starting with P and ending with PEND matches PREFIX. */
6049
6050static int
6051strprefix (const char *p, const char *pend, const char *prefix)
6052{
6053 for ( ; p < pend; p++, prefix++)
6054 if (*p != *prefix)
6055 return 0;
6056 return *prefix == '\0';
6057}
6058
74531fed
PA
6059/* Parse the stop reply in BUF. Either the function succeeds, and the
6060 result is stored in EVENT, or throws an error. */
6061
6062static void
6063remote_parse_stop_reply (char *buf, struct stop_reply *event)
6064{
6065 struct remote_arch_state *rsa = get_remote_arch_state ();
6066 ULONGEST addr;
6067 char *p;
94585166 6068 int skipregs = 0;
74531fed
PA
6069
6070 event->ptid = null_ptid;
bcc75809 6071 event->rs = get_remote_state ();
74531fed
PA
6072 event->ws.kind = TARGET_WAITKIND_IGNORE;
6073 event->ws.value.integer = 0;
f7e6eed5 6074 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 6075 event->regcache = NULL;
dc146f7c 6076 event->core = -1;
74531fed
PA
6077
6078 switch (buf[0])
6079 {
6080 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
6081 /* Expedited reply, containing Signal, {regno, reg} repeat. */
6082 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
6083 ss = signal number
6084 n... = register number
6085 r... = register contents
6086 */
6087
6088 p = &buf[3]; /* after Txx */
6089 while (*p)
6090 {
6091 char *p1;
cea39f65 6092 int fieldsize;
43ff13b4 6093
1f10ba14
PA
6094 p1 = strchr (p, ':');
6095 if (p1 == NULL)
6096 error (_("Malformed packet(a) (missing colon): %s\n\
6097Packet: '%s'\n"),
6098 p, buf);
6099 if (p == p1)
6100 error (_("Malformed packet(a) (missing register number): %s\n\
6101Packet: '%s'\n"),
6102 p, buf);
3c3bea1c 6103
1f10ba14
PA
6104 /* Some "registers" are actually extended stop information.
6105 Note if you're adding a new entry here: GDB 7.9 and
6106 earlier assume that all register "numbers" that start
6107 with an hex digit are real register numbers. Make sure
6108 the server only sends such a packet if it knows the
6109 client understands it. */
c8e38a49 6110
26d56a93 6111 if (strprefix (p, p1, "thread"))
1f10ba14 6112 event->ptid = read_ptid (++p1, &p);
26d56a93
SL
6113 else if (strprefix (p, p1, "watch")
6114 || strprefix (p, p1, "rwatch")
6115 || strprefix (p, p1, "awatch"))
cea39f65 6116 {
f7e6eed5 6117 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
6118 p = unpack_varlen_hex (++p1, &addr);
6119 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 6120 }
26d56a93 6121 else if (strprefix (p, p1, "swbreak"))
f7e6eed5
PA
6122 {
6123 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
6124
6125 /* Make sure the stub doesn't forget to indicate support
6126 with qSupported. */
6127 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
6128 error (_("Unexpected swbreak stop reason"));
6129
6130 /* The value part is documented as "must be empty",
6131 though we ignore it, in case we ever decide to make
6132 use of it in a backward compatible way. */
6133 p = skip_to_semicolon (p1 + 1);
6134 }
26d56a93 6135 else if (strprefix (p, p1, "hwbreak"))
f7e6eed5
PA
6136 {
6137 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
6138
6139 /* Make sure the stub doesn't forget to indicate support
6140 with qSupported. */
6141 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
6142 error (_("Unexpected hwbreak stop reason"));
6143
6144 /* See above. */
6145 p = skip_to_semicolon (p1 + 1);
6146 }
26d56a93 6147 else if (strprefix (p, p1, "library"))
cea39f65 6148 {
1f10ba14
PA
6149 event->ws.kind = TARGET_WAITKIND_LOADED;
6150 p = skip_to_semicolon (p1 + 1);
6151 }
26d56a93 6152 else if (strprefix (p, p1, "replaylog"))
1f10ba14
PA
6153 {
6154 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
6155 /* p1 will indicate "begin" or "end", but it makes
6156 no difference for now, so ignore it. */
6157 p = skip_to_semicolon (p1 + 1);
6158 }
26d56a93 6159 else if (strprefix (p, p1, "core"))
1f10ba14
PA
6160 {
6161 ULONGEST c;
a744cf53 6162
1f10ba14
PA
6163 p = unpack_varlen_hex (++p1, &c);
6164 event->core = c;
cea39f65 6165 }
26d56a93 6166 else if (strprefix (p, p1, "fork"))
de0d863e
DB
6167 {
6168 event->ws.value.related_pid = read_ptid (++p1, &p);
6169 event->ws.kind = TARGET_WAITKIND_FORKED;
6170 }
26d56a93 6171 else if (strprefix (p, p1, "vfork"))
c269dbdb
DB
6172 {
6173 event->ws.value.related_pid = read_ptid (++p1, &p);
6174 event->ws.kind = TARGET_WAITKIND_VFORKED;
6175 }
26d56a93 6176 else if (strprefix (p, p1, "vforkdone"))
c269dbdb
DB
6177 {
6178 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
6179 p = skip_to_semicolon (p1 + 1);
6180 }
94585166
DB
6181 else if (strncmp (p, "exec", p1 - p) == 0)
6182 {
6183 ULONGEST ignored;
6184 char pathname[PATH_MAX];
6185 int pathlen;
6186
6187 /* Determine the length of the execd pathname. */
6188 p = unpack_varlen_hex (++p1, &ignored);
6189 pathlen = (p - p1) / 2;
6190
6191 /* Save the pathname for event reporting and for
6192 the next run command. */
6193 hex2bin (p1, (gdb_byte *) pathname, pathlen);
6194 pathname[pathlen] = '\0';
6195
6196 /* This is freed during event handling. */
6197 event->ws.value.execd_pathname = xstrdup (pathname);
6198 event->ws.kind = TARGET_WAITKIND_EXECD;
6199
6200 /* Skip the registers included in this packet, since
6201 they may be for an architecture different from the
6202 one used by the original program. */
6203 skipregs = 1;
6204 }
cea39f65
MS
6205 else
6206 {
1f10ba14
PA
6207 ULONGEST pnum;
6208 char *p_temp;
6209
94585166
DB
6210 if (skipregs)
6211 {
6212 p = skip_to_semicolon (p1 + 1);
6213 p++;
6214 continue;
6215 }
6216
1f10ba14
PA
6217 /* Maybe a real ``P'' register number. */
6218 p_temp = unpack_varlen_hex (p, &pnum);
6219 /* If the first invalid character is the colon, we got a
6220 register number. Otherwise, it's an unknown stop
6221 reason. */
6222 if (p_temp == p1)
6223 {
6224 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
6225 cached_reg_t cached_reg;
43ff13b4 6226
1f10ba14
PA
6227 if (reg == NULL)
6228 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 6229Packet: '%s'\n"),
1f10ba14 6230 hex_string (pnum), p, buf);
c8e38a49 6231
1f10ba14 6232 cached_reg.num = reg->regnum;
4100683b 6233
1f10ba14
PA
6234 p = p1 + 1;
6235 fieldsize = hex2bin (p, cached_reg.data,
6236 register_size (target_gdbarch (),
6237 reg->regnum));
6238 p += 2 * fieldsize;
6239 if (fieldsize < register_size (target_gdbarch (),
6240 reg->regnum))
6241 warning (_("Remote reply is too short: %s"), buf);
74531fed 6242
1f10ba14
PA
6243 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
6244 }
6245 else
6246 {
6247 /* Not a number. Silently skip unknown optional
6248 info. */
6249 p = skip_to_semicolon (p1 + 1);
6250 }
cea39f65 6251 }
c8e38a49 6252
cea39f65
MS
6253 if (*p != ';')
6254 error (_("Remote register badly formatted: %s\nhere: %s"),
6255 buf, p);
6256 ++p;
6257 }
5b5596ff
PA
6258
6259 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
6260 break;
6261
c8e38a49
PA
6262 /* fall through */
6263 case 'S': /* Old style status, just signal only. */
3a09da41
PA
6264 {
6265 int sig;
6266
6267 event->ws.kind = TARGET_WAITKIND_STOPPED;
6268 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
6269 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
6270 event->ws.value.sig = (enum gdb_signal) sig;
6271 else
6272 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
6273 }
c8e38a49
PA
6274 break;
6275 case 'W': /* Target exited. */
6276 case 'X':
6277 {
6278 char *p;
6279 int pid;
6280 ULONGEST value;
82f73884 6281
c8e38a49
PA
6282 /* GDB used to accept only 2 hex chars here. Stubs should
6283 only send more if they detect GDB supports multi-process
6284 support. */
6285 p = unpack_varlen_hex (&buf[1], &value);
82f73884 6286
c8e38a49
PA
6287 if (buf[0] == 'W')
6288 {
6289 /* The remote process exited. */
74531fed
PA
6290 event->ws.kind = TARGET_WAITKIND_EXITED;
6291 event->ws.value.integer = value;
c8e38a49
PA
6292 }
6293 else
6294 {
6295 /* The remote process exited with a signal. */
74531fed 6296 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
6297 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
6298 event->ws.value.sig = (enum gdb_signal) value;
6299 else
6300 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 6301 }
82f73884 6302
c8e38a49
PA
6303 /* If no process is specified, assume inferior_ptid. */
6304 pid = ptid_get_pid (inferior_ptid);
6305 if (*p == '\0')
6306 ;
6307 else if (*p == ';')
6308 {
6309 p++;
6310
0b24eb2d 6311 if (*p == '\0')
82f73884 6312 ;
61012eef 6313 else if (startswith (p, "process:"))
82f73884 6314 {
c8e38a49 6315 ULONGEST upid;
a744cf53 6316
c8e38a49
PA
6317 p += sizeof ("process:") - 1;
6318 unpack_varlen_hex (p, &upid);
6319 pid = upid;
82f73884
PA
6320 }
6321 else
6322 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6323 }
c8e38a49
PA
6324 else
6325 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6326 event->ptid = pid_to_ptid (pid);
6327 }
6328 break;
6329 }
6330
6331 if (non_stop && ptid_equal (event->ptid, null_ptid))
6332 error (_("No process or thread specified in stop reply: %s"), buf);
6333}
6334
722247f1
YQ
6335/* When the stub wants to tell GDB about a new notification reply, it
6336 sends a notification (%Stop, for example). Those can come it at
6337 any time, hence, we have to make sure that any pending
6338 putpkt/getpkt sequence we're making is finished, before querying
6339 the stub for more events with the corresponding ack command
6340 (vStopped, for example). E.g., if we started a vStopped sequence
6341 immediately upon receiving the notification, something like this
6342 could happen:
74531fed
PA
6343
6344 1.1) --> Hg 1
6345 1.2) <-- OK
6346 1.3) --> g
6347 1.4) <-- %Stop
6348 1.5) --> vStopped
6349 1.6) <-- (registers reply to step #1.3)
6350
6351 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6352 query.
6353
796cb314 6354 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6355 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6356 doing whatever we were doing:
6357
6358 2.1) --> Hg 1
6359 2.2) <-- OK
6360 2.3) --> g
6361 2.4) <-- %Stop
6362 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6363 2.5) <-- (registers reply to step #2.3)
6364
6365 Eventualy after step #2.5, we return to the event loop, which
6366 notices there's an event on the
6367 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6368 associated callback --- the function below. At this point, we're
6369 always safe to start a vStopped sequence. :
6370
6371 2.6) --> vStopped
6372 2.7) <-- T05 thread:2
6373 2.8) --> vStopped
6374 2.9) --> OK
6375*/
6376
722247f1
YQ
6377void
6378remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6379{
6380 struct remote_state *rs = get_remote_state ();
74531fed 6381
f48ff2a7 6382 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6383 {
722247f1
YQ
6384 if (notif_debug)
6385 fprintf_unfiltered (gdb_stdlog,
6386 "notif: process: '%s' ack pending event\n",
6387 nc->name);
74531fed 6388
722247f1 6389 /* acknowledge */
f48ff2a7
YQ
6390 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6391 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6392
6393 while (1)
6394 {
6395 getpkt (&rs->buf, &rs->buf_size, 0);
6396 if (strcmp (rs->buf, "OK") == 0)
6397 break;
6398 else
722247f1 6399 remote_notif_ack (nc, rs->buf);
74531fed
PA
6400 }
6401 }
722247f1
YQ
6402 else
6403 {
6404 if (notif_debug)
6405 fprintf_unfiltered (gdb_stdlog,
6406 "notif: process: '%s' no pending reply\n",
6407 nc->name);
6408 }
74531fed
PA
6409}
6410
74531fed
PA
6411/* Called when it is decided that STOP_REPLY holds the info of the
6412 event that is to be returned to the core. This function always
6413 destroys STOP_REPLY. */
6414
6415static ptid_t
6416process_stop_reply (struct stop_reply *stop_reply,
6417 struct target_waitstatus *status)
6418{
6419 ptid_t ptid;
6420
6421 *status = stop_reply->ws;
6422 ptid = stop_reply->ptid;
6423
6424 /* If no thread/process was reported by the stub, assume the current
6425 inferior. */
6426 if (ptid_equal (ptid, null_ptid))
6427 ptid = inferior_ptid;
6428
5f3563ea
PA
6429 if (status->kind != TARGET_WAITKIND_EXITED
6430 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6431 {
ee154bee
TT
6432 struct remote_state *rs = get_remote_state ();
6433
5f3563ea
PA
6434 /* Expedited registers. */
6435 if (stop_reply->regcache)
6436 {
217f1f79 6437 struct regcache *regcache
f5656ead 6438 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6439 cached_reg_t *reg;
6440 int ix;
6441
6442 for (ix = 0;
6443 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6444 ix++)
217f1f79 6445 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6446 VEC_free (cached_reg_t, stop_reply->regcache);
6447 }
74531fed 6448
f7e6eed5 6449 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6450 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6451
6452 remote_notice_new_inferior (ptid, 0);
dc146f7c 6453 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6454 }
6455
74531fed
PA
6456 stop_reply_xfree (stop_reply);
6457 return ptid;
6458}
6459
6460/* The non-stop mode version of target_wait. */
6461
6462static ptid_t
47608cb1 6463remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6464{
6465 struct remote_state *rs = get_remote_state ();
74531fed
PA
6466 struct stop_reply *stop_reply;
6467 int ret;
fee9eda9 6468 int is_notif = 0;
74531fed
PA
6469
6470 /* If in non-stop mode, get out of getpkt even if a
6471 notification is received. */
6472
6473 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6474 0 /* forever */, &is_notif);
74531fed
PA
6475 while (1)
6476 {
fee9eda9 6477 if (ret != -1 && !is_notif)
74531fed
PA
6478 switch (rs->buf[0])
6479 {
6480 case 'E': /* Error of some sort. */
6481 /* We're out of sync with the target now. Did it continue
6482 or not? We can't tell which thread it was in non-stop,
6483 so just ignore this. */
6484 warning (_("Remote failure reply: %s"), rs->buf);
6485 break;
6486 case 'O': /* Console output. */
6487 remote_console_output (rs->buf + 1);
6488 break;
6489 default:
6490 warning (_("Invalid remote reply: %s"), rs->buf);
6491 break;
6492 }
6493
6494 /* Acknowledge a pending stop reply that may have arrived in the
6495 mean time. */
f48ff2a7 6496 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6497 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6498
6499 /* If indeed we noticed a stop reply, we're done. */
6500 stop_reply = queued_stop_reply (ptid);
6501 if (stop_reply != NULL)
6502 return process_stop_reply (stop_reply, status);
6503
47608cb1 6504 /* Still no event. If we're just polling for an event, then
74531fed 6505 return to the event loop. */
47608cb1 6506 if (options & TARGET_WNOHANG)
74531fed
PA
6507 {
6508 status->kind = TARGET_WAITKIND_IGNORE;
6509 return minus_one_ptid;
6510 }
6511
47608cb1 6512 /* Otherwise do a blocking wait. */
74531fed 6513 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6514 1 /* forever */, &is_notif);
74531fed
PA
6515 }
6516}
6517
6518/* Wait until the remote machine stops, then return, storing status in
6519 STATUS just as `wait' would. */
6520
6521static ptid_t
47608cb1 6522remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6523{
6524 struct remote_state *rs = get_remote_state ();
74531fed 6525 ptid_t event_ptid = null_ptid;
cea39f65 6526 char *buf;
74531fed
PA
6527 struct stop_reply *stop_reply;
6528
47608cb1
PA
6529 again:
6530
74531fed
PA
6531 status->kind = TARGET_WAITKIND_IGNORE;
6532 status->value.integer = 0;
6533
6534 stop_reply = queued_stop_reply (ptid);
6535 if (stop_reply != NULL)
6536 return process_stop_reply (stop_reply, status);
6537
6538 if (rs->cached_wait_status)
6539 /* Use the cached wait status, but only once. */
6540 rs->cached_wait_status = 0;
6541 else
6542 {
6543 int ret;
722247f1 6544 int is_notif;
567420d1
PA
6545 int forever = ((options & TARGET_WNOHANG) == 0
6546 && wait_forever_enabled_p);
6547
6548 if (!rs->waiting_for_stop_reply)
6549 {
6550 status->kind = TARGET_WAITKIND_NO_RESUMED;
6551 return minus_one_ptid;
6552 }
74531fed
PA
6553
6554 if (!target_is_async_p ())
6555 {
934b9bac 6556 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6557 /* If the user hit C-c before this packet, or between packets,
6558 pretend that it was hit right here. */
522002f9 6559 if (check_quit_flag ())
74531fed 6560 {
522002f9 6561 clear_quit_flag ();
934b9bac 6562 sync_remote_interrupt (SIGINT);
74531fed
PA
6563 }
6564 }
6565
6566 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6567 _never_ wait for ever -> test on target_is_async_p().
6568 However, before we do that we need to ensure that the caller
6569 knows how to take the target into/out of async mode. */
722247f1 6570 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 6571 forever, &is_notif);
722247f1 6572
5e1b953b
SDJ
6573 if (!target_is_async_p ())
6574 signal (SIGINT, ofunc);
6575
722247f1
YQ
6576 /* GDB gets a notification. Return to core as this event is
6577 not interesting. */
6578 if (ret != -1 && is_notif)
6579 return minus_one_ptid;
567420d1
PA
6580
6581 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
6582 return minus_one_ptid;
74531fed
PA
6583 }
6584
6585 buf = rs->buf;
6586
f7e6eed5 6587 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6588
6589 /* We got something. */
6590 rs->waiting_for_stop_reply = 0;
6591
3a29589a
DJ
6592 /* Assume that the target has acknowledged Ctrl-C unless we receive
6593 an 'F' or 'O' packet. */
6594 if (buf[0] != 'F' && buf[0] != 'O')
6595 rs->ctrlc_pending_p = 0;
6596
74531fed
PA
6597 switch (buf[0])
6598 {
6599 case 'E': /* Error of some sort. */
6600 /* We're out of sync with the target now. Did it continue or
6601 not? Not is more likely, so report a stop. */
6602 warning (_("Remote failure reply: %s"), buf);
6603 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6604 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6605 break;
6606 case 'F': /* File-I/O request. */
3a29589a
DJ
6607 remote_fileio_request (buf, rs->ctrlc_pending_p);
6608 rs->ctrlc_pending_p = 0;
74531fed
PA
6609 break;
6610 case 'T': case 'S': case 'X': case 'W':
6611 {
722247f1
YQ
6612 struct stop_reply *stop_reply
6613 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6614 rs->buf);
74531fed 6615
74531fed 6616 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6617 break;
6618 }
6619 case 'O': /* Console output. */
6620 remote_console_output (buf + 1);
e24a49d8 6621
c8e38a49
PA
6622 /* The target didn't really stop; keep waiting. */
6623 rs->waiting_for_stop_reply = 1;
e24a49d8 6624
c8e38a49
PA
6625 break;
6626 case '\0':
b73be471 6627 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6628 {
6629 /* Zero length reply means that we tried 'S' or 'C' and the
6630 remote system doesn't support it. */
6631 target_terminal_ours_for_output ();
6632 printf_filtered
6633 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6634 gdb_signal_to_name (rs->last_sent_signal));
6635 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6636 target_terminal_inferior ();
6637
280ceea3 6638 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6639 putpkt ((char *) buf);
6640
6641 /* We just told the target to resume, so a stop reply is in
6642 order. */
e24a49d8 6643 rs->waiting_for_stop_reply = 1;
c8e38a49 6644 break;
43ff13b4 6645 }
c8e38a49
PA
6646 /* else fallthrough */
6647 default:
6648 warning (_("Invalid remote reply: %s"), buf);
6649 /* Keep waiting. */
6650 rs->waiting_for_stop_reply = 1;
6651 break;
43ff13b4 6652 }
c8e38a49 6653
c8e38a49 6654 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6655 {
6656 /* Nothing interesting happened. If we're doing a non-blocking
6657 poll, we're done. Otherwise, go back to waiting. */
6658 if (options & TARGET_WNOHANG)
6659 return minus_one_ptid;
6660 else
6661 goto again;
6662 }
74531fed
PA
6663 else if (status->kind != TARGET_WAITKIND_EXITED
6664 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6665 {
6666 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6667 record_currthread (rs, event_ptid);
82f73884
PA
6668 else
6669 event_ptid = inferior_ptid;
43ff13b4 6670 }
74531fed
PA
6671 else
6672 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6673 record_currthread (rs, minus_one_ptid);
79d7f229 6674
82f73884 6675 return event_ptid;
43ff13b4
JM
6676}
6677
74531fed
PA
6678/* Wait until the remote machine stops, then return, storing status in
6679 STATUS just as `wait' would. */
6680
c8e38a49 6681static ptid_t
117de6a9 6682remote_wait (struct target_ops *ops,
47608cb1 6683 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6684{
6685 ptid_t event_ptid;
6686
74531fed 6687 if (non_stop)
47608cb1 6688 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6689 else
47608cb1 6690 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6691
d9d41e78 6692 if (target_is_async_p ())
c8e38a49 6693 {
74531fed
PA
6694 /* If there are are events left in the queue tell the event loop
6695 to return here. */
722247f1 6696 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6697 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6698 }
c8e38a49
PA
6699
6700 return event_ptid;
6701}
6702
74ca34ce 6703/* Fetch a single register using a 'p' packet. */
c906108c 6704
b96ec7ac 6705static int
56be3814 6706fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6707{
6708 struct remote_state *rs = get_remote_state ();
2e9f7625 6709 char *buf, *p;
b96ec7ac
AC
6710 char regp[MAX_REGISTER_SIZE];
6711 int i;
6712
4082afcc 6713 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6714 return 0;
6715
6716 if (reg->pnum == -1)
6717 return 0;
6718
2e9f7625 6719 p = rs->buf;
fcad0fa4 6720 *p++ = 'p';
74ca34ce 6721 p += hexnumstr (p, reg->pnum);
fcad0fa4 6722 *p++ = '\0';
1f4437a4
MS
6723 putpkt (rs->buf);
6724 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6725
2e9f7625
DJ
6726 buf = rs->buf;
6727
74ca34ce
DJ
6728 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6729 {
6730 case PACKET_OK:
6731 break;
6732 case PACKET_UNKNOWN:
6733 return 0;
6734 case PACKET_ERROR:
27a9c0bf
MS
6735 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6736 gdbarch_register_name (get_regcache_arch (regcache),
6737 reg->regnum),
6738 buf);
74ca34ce 6739 }
3f9a994c
JB
6740
6741 /* If this register is unfetchable, tell the regcache. */
6742 if (buf[0] == 'x')
8480adf2 6743 {
56be3814 6744 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6745 return 1;
b96ec7ac 6746 }
b96ec7ac 6747
3f9a994c
JB
6748 /* Otherwise, parse and supply the value. */
6749 p = buf;
6750 i = 0;
6751 while (p[0] != 0)
6752 {
6753 if (p[1] == 0)
74ca34ce 6754 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6755
6756 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6757 p += 2;
6758 }
56be3814 6759 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6760 return 1;
b96ec7ac
AC
6761}
6762
74ca34ce
DJ
6763/* Fetch the registers included in the target's 'g' packet. */
6764
29709017
DJ
6765static int
6766send_g_packet (void)
c906108c 6767{
d01949b6 6768 struct remote_state *rs = get_remote_state ();
cea39f65 6769 int buf_len;
c906108c 6770
bba74b36 6771 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6772 remote_send (&rs->buf, &rs->buf_size);
c906108c 6773
29709017
DJ
6774 /* We can get out of synch in various cases. If the first character
6775 in the buffer is not a hex character, assume that has happened
6776 and try to fetch another packet to read. */
6777 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6778 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6779 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6780 && rs->buf[0] != 'x') /* New: unavailable register value. */
6781 {
6782 if (remote_debug)
6783 fprintf_unfiltered (gdb_stdlog,
6784 "Bad register packet; fetching a new packet\n");
6785 getpkt (&rs->buf, &rs->buf_size, 0);
6786 }
6787
74ca34ce
DJ
6788 buf_len = strlen (rs->buf);
6789
6790 /* Sanity check the received packet. */
6791 if (buf_len % 2 != 0)
6792 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6793
6794 return buf_len / 2;
6795}
6796
6797static void
56be3814 6798process_g_packet (struct regcache *regcache)
29709017 6799{
4a22f64d 6800 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6801 struct remote_state *rs = get_remote_state ();
6802 struct remote_arch_state *rsa = get_remote_arch_state ();
6803 int i, buf_len;
6804 char *p;
6805 char *regs;
6806
6807 buf_len = strlen (rs->buf);
6808
6809 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6810 if (buf_len > 2 * rsa->sizeof_g_packet)
6811 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6812
6813 /* Save the size of the packet sent to us by the target. It is used
6814 as a heuristic when determining the max size of packets that the
6815 target can safely receive. */
6816 if (rsa->actual_register_packet_size == 0)
6817 rsa->actual_register_packet_size = buf_len;
6818
6819 /* If this is smaller than we guessed the 'g' packet would be,
6820 update our records. A 'g' reply that doesn't include a register's
6821 value implies either that the register is not available, or that
6822 the 'p' packet must be used. */
6823 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6824 {
74ca34ce
DJ
6825 rsa->sizeof_g_packet = buf_len / 2;
6826
4a22f64d 6827 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6828 {
74ca34ce
DJ
6829 if (rsa->regs[i].pnum == -1)
6830 continue;
6831
6832 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6833 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6834 else
74ca34ce 6835 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6836 }
74ca34ce 6837 }
b323314b 6838
74ca34ce 6839 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6840
6841 /* Unimplemented registers read as all bits zero. */
ea9c271d 6842 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6843
c906108c
SS
6844 /* Reply describes registers byte by byte, each byte encoded as two
6845 hex characters. Suck them all up, then supply them to the
6846 register cacheing/storage mechanism. */
6847
74ca34ce 6848 p = rs->buf;
ea9c271d 6849 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6850 {
74ca34ce
DJ
6851 if (p[0] == 0 || p[1] == 0)
6852 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6853 internal_error (__FILE__, __LINE__,
9b20d036 6854 _("unexpected end of 'g' packet reply"));
74ca34ce 6855
c906108c 6856 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6857 regs[i] = 0; /* 'x' */
c906108c
SS
6858 else
6859 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6860 p += 2;
6861 }
6862
a744cf53
MS
6863 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6864 {
6865 struct packet_reg *r = &rsa->regs[i];
6866
6867 if (r->in_g_packet)
6868 {
6869 if (r->offset * 2 >= strlen (rs->buf))
6870 /* This shouldn't happen - we adjusted in_g_packet above. */
6871 internal_error (__FILE__, __LINE__,
9b20d036 6872 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6873 else if (rs->buf[r->offset * 2] == 'x')
6874 {
6875 gdb_assert (r->offset * 2 < strlen (rs->buf));
6876 /* The register isn't available, mark it as such (at
6877 the same time setting the value to zero). */
6878 regcache_raw_supply (regcache, r->regnum, NULL);
6879 }
6880 else
6881 regcache_raw_supply (regcache, r->regnum,
6882 regs + r->offset);
6883 }
6884 }
c906108c
SS
6885}
6886
29709017 6887static void
56be3814 6888fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6889{
6890 send_g_packet ();
56be3814 6891 process_g_packet (regcache);
29709017
DJ
6892}
6893
e6e4e701
PA
6894/* Make the remote selected traceframe match GDB's selected
6895 traceframe. */
6896
6897static void
6898set_remote_traceframe (void)
6899{
6900 int newnum;
262e1174 6901 struct remote_state *rs = get_remote_state ();
e6e4e701 6902
262e1174 6903 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6904 return;
6905
6906 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6907 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6908
6909 newnum = target_trace_find (tfind_number,
6910 get_traceframe_number (), 0, 0, NULL);
6911
6912 /* Should not happen. If it does, all bets are off. */
6913 if (newnum != get_traceframe_number ())
6914 warning (_("could not set remote traceframe"));
6915}
6916
74ca34ce 6917static void
28439f5e
PA
6918remote_fetch_registers (struct target_ops *ops,
6919 struct regcache *regcache, int regnum)
74ca34ce 6920{
74ca34ce
DJ
6921 struct remote_arch_state *rsa = get_remote_arch_state ();
6922 int i;
6923
e6e4e701 6924 set_remote_traceframe ();
79d7f229 6925 set_general_thread (inferior_ptid);
74ca34ce
DJ
6926
6927 if (regnum >= 0)
6928 {
6929 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6930
74ca34ce
DJ
6931 gdb_assert (reg != NULL);
6932
6933 /* If this register might be in the 'g' packet, try that first -
6934 we are likely to read more than one register. If this is the
6935 first 'g' packet, we might be overly optimistic about its
6936 contents, so fall back to 'p'. */
6937 if (reg->in_g_packet)
6938 {
56be3814 6939 fetch_registers_using_g (regcache);
74ca34ce
DJ
6940 if (reg->in_g_packet)
6941 return;
6942 }
6943
56be3814 6944 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6945 return;
6946
6947 /* This register is not available. */
56be3814 6948 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6949
6950 return;
6951 }
6952
56be3814 6953 fetch_registers_using_g (regcache);
74ca34ce 6954
4a22f64d 6955 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6956 if (!rsa->regs[i].in_g_packet)
56be3814 6957 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6958 {
6959 /* This register is not available. */
56be3814 6960 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6961 }
6962}
6963
c906108c
SS
6964/* Prepare to store registers. Since we may send them all (using a
6965 'G' request), we have to read out the ones we don't want to change
6966 first. */
6967
c5aa993b 6968static void
f32dbf8c 6969remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6970{
ea9c271d 6971 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6972 int i;
cfd77fa1 6973 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6974
c906108c 6975 /* Make sure the entire registers array is valid. */
4082afcc 6976 switch (packet_support (PACKET_P))
5a2468f5
JM
6977 {
6978 case PACKET_DISABLE:
6979 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6980 /* Make sure all the necessary registers are cached. */
4a22f64d 6981 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6982 if (rsa->regs[i].in_g_packet)
316f2060 6983 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6984 break;
6985 case PACKET_ENABLE:
6986 break;
6987 }
6988}
6989
ad10f812 6990/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6991 packet was not recognized. */
5a2468f5
JM
6992
6993static int
1f4437a4
MS
6994store_register_using_P (const struct regcache *regcache,
6995 struct packet_reg *reg)
5a2468f5 6996{
4a22f64d 6997 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6998 struct remote_state *rs = get_remote_state ();
5a2468f5 6999 /* Try storing a single register. */
6d820c5c 7000 char *buf = rs->buf;
cfd77fa1 7001 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 7002 char *p;
5a2468f5 7003
4082afcc 7004 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
7005 return 0;
7006
7007 if (reg->pnum == -1)
7008 return 0;
7009
ea9c271d 7010 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 7011 p = buf + strlen (buf);
56be3814 7012 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 7013 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
7014 putpkt (rs->buf);
7015 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 7016
74ca34ce
DJ
7017 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
7018 {
7019 case PACKET_OK:
7020 return 1;
7021 case PACKET_ERROR:
27a9c0bf
MS
7022 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
7023 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
7024 case PACKET_UNKNOWN:
7025 return 0;
7026 default:
7027 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7028 }
c906108c
SS
7029}
7030
23860348
MS
7031/* Store register REGNUM, or all registers if REGNUM == -1, from the
7032 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
7033
7034static void
56be3814 7035store_registers_using_G (const struct regcache *regcache)
c906108c 7036{
d01949b6 7037 struct remote_state *rs = get_remote_state ();
ea9c271d 7038 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 7039 gdb_byte *regs;
c906108c
SS
7040 char *p;
7041
193cb69f
AC
7042 /* Extract all the registers in the regcache copying them into a
7043 local buffer. */
7044 {
b323314b 7045 int i;
a744cf53 7046
ea9c271d
DJ
7047 regs = alloca (rsa->sizeof_g_packet);
7048 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 7049 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 7050 {
ea9c271d 7051 struct packet_reg *r = &rsa->regs[i];
a744cf53 7052
b323314b 7053 if (r->in_g_packet)
56be3814 7054 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
7055 }
7056 }
c906108c
SS
7057
7058 /* Command describes registers byte by byte,
7059 each byte encoded as two hex characters. */
6d820c5c 7060 p = rs->buf;
193cb69f 7061 *p++ = 'G';
74ca34ce
DJ
7062 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
7063 updated. */
7064 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
7065 putpkt (rs->buf);
7066 getpkt (&rs->buf, &rs->buf_size, 0);
7067 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
7068 error (_("Could not write registers; remote failure reply '%s'"),
7069 rs->buf);
c906108c 7070}
74ca34ce
DJ
7071
7072/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
7073 of the register cache buffer. FIXME: ignores errors. */
7074
7075static void
28439f5e
PA
7076remote_store_registers (struct target_ops *ops,
7077 struct regcache *regcache, int regnum)
74ca34ce 7078{
74ca34ce
DJ
7079 struct remote_arch_state *rsa = get_remote_arch_state ();
7080 int i;
7081
e6e4e701 7082 set_remote_traceframe ();
79d7f229 7083 set_general_thread (inferior_ptid);
74ca34ce
DJ
7084
7085 if (regnum >= 0)
7086 {
7087 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 7088
74ca34ce
DJ
7089 gdb_assert (reg != NULL);
7090
7091 /* Always prefer to store registers using the 'P' packet if
7092 possible; we often change only a small number of registers.
7093 Sometimes we change a larger number; we'd need help from a
7094 higher layer to know to use 'G'. */
56be3814 7095 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
7096 return;
7097
7098 /* For now, don't complain if we have no way to write the
7099 register. GDB loses track of unavailable registers too
7100 easily. Some day, this may be an error. We don't have
0df8b418 7101 any way to read the register, either... */
74ca34ce
DJ
7102 if (!reg->in_g_packet)
7103 return;
7104
56be3814 7105 store_registers_using_G (regcache);
74ca34ce
DJ
7106 return;
7107 }
7108
56be3814 7109 store_registers_using_G (regcache);
74ca34ce 7110
4a22f64d 7111 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 7112 if (!rsa->regs[i].in_g_packet)
56be3814 7113 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
7114 /* See above for why we do not issue an error here. */
7115 continue;
7116}
c906108c
SS
7117\f
7118
7119/* Return the number of hex digits in num. */
7120
7121static int
fba45db2 7122hexnumlen (ULONGEST num)
c906108c
SS
7123{
7124 int i;
7125
7126 for (i = 0; num != 0; i++)
7127 num >>= 4;
7128
7129 return max (i, 1);
7130}
7131
2df3850c 7132/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
7133
7134static int
fba45db2 7135hexnumstr (char *buf, ULONGEST num)
c906108c 7136{
c906108c 7137 int len = hexnumlen (num);
a744cf53 7138
2df3850c
JM
7139 return hexnumnstr (buf, num, len);
7140}
7141
c906108c 7142
2df3850c 7143/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 7144
2df3850c 7145static int
fba45db2 7146hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
7147{
7148 int i;
7149
7150 buf[width] = '\0';
7151
7152 for (i = width - 1; i >= 0; i--)
c906108c 7153 {
c5aa993b 7154 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
7155 num >>= 4;
7156 }
7157
2df3850c 7158 return width;
c906108c
SS
7159}
7160
23860348 7161/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
7162
7163static CORE_ADDR
fba45db2 7164remote_address_masked (CORE_ADDR addr)
c906108c 7165{
883b9c6c 7166 unsigned int address_size = remote_address_size;
a744cf53 7167
911c95a5
UW
7168 /* If "remoteaddresssize" was not set, default to target address size. */
7169 if (!address_size)
f5656ead 7170 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
7171
7172 if (address_size > 0
7173 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
7174 {
7175 /* Only create a mask when that mask can safely be constructed
23860348 7176 in a ULONGEST variable. */
c906108c 7177 ULONGEST mask = 1;
a744cf53 7178
911c95a5 7179 mask = (mask << address_size) - 1;
c906108c
SS
7180 addr &= mask;
7181 }
7182 return addr;
7183}
7184
7185/* Determine whether the remote target supports binary downloading.
7186 This is accomplished by sending a no-op memory write of zero length
7187 to the target at the specified address. It does not suffice to send
23860348
MS
7188 the whole packet, since many stubs strip the eighth bit and
7189 subsequently compute a wrong checksum, which causes real havoc with
7190 remote_write_bytes.
7a292a7a 7191
96baa820 7192 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 7193 clean. In cases like this, the user should clear "remote
23860348 7194 X-packet". */
96baa820 7195
c906108c 7196static void
fba45db2 7197check_binary_download (CORE_ADDR addr)
c906108c 7198{
d01949b6 7199 struct remote_state *rs = get_remote_state ();
24b06219 7200
4082afcc 7201 switch (packet_support (PACKET_X))
c906108c 7202 {
96baa820
JM
7203 case PACKET_DISABLE:
7204 break;
7205 case PACKET_ENABLE:
7206 break;
7207 case PACKET_SUPPORT_UNKNOWN:
7208 {
96baa820 7209 char *p;
802188a7 7210
2e9f7625 7211 p = rs->buf;
96baa820
JM
7212 *p++ = 'X';
7213 p += hexnumstr (p, (ULONGEST) addr);
7214 *p++ = ',';
7215 p += hexnumstr (p, (ULONGEST) 0);
7216 *p++ = ':';
7217 *p = '\0';
802188a7 7218
2e9f7625 7219 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7220 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 7221
2e9f7625 7222 if (rs->buf[0] == '\0')
96baa820
JM
7223 {
7224 if (remote_debug)
7225 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7226 "binary downloading NOT "
7227 "supported by target\n");
444abaca 7228 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
7229 }
7230 else
7231 {
7232 if (remote_debug)
7233 fprintf_unfiltered (gdb_stdlog,
64b9b334 7234 "binary downloading supported by target\n");
444abaca 7235 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
7236 }
7237 break;
7238 }
c906108c
SS
7239 }
7240}
7241
124e13d9
SM
7242/* Helper function to resize the payload in order to try to get a good
7243 alignment. We try to write an amount of data such that the next write will
7244 start on an address aligned on REMOTE_ALIGN_WRITES. */
7245
7246static int
7247align_for_efficient_write (int todo, CORE_ADDR memaddr)
7248{
7249 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
7250}
7251
c906108c
SS
7252/* Write memory data directly to the remote machine.
7253 This does not inform the data cache; the data cache uses this.
a76d924d 7254 HEADER is the starting part of the packet.
c906108c
SS
7255 MEMADDR is the address in the remote memory space.
7256 MYADDR is the address of the buffer in our space.
124e13d9
SM
7257 LEN_UNITS is the number of addressable units to write.
7258 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
7259 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
7260 should send data as binary ('X'), or hex-encoded ('M').
7261
7262 The function creates packet of the form
7263 <HEADER><ADDRESS>,<LENGTH>:<DATA>
7264
124e13d9 7265 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
7266
7267 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
7268 are omitted.
7269
9b409511 7270 Return the transferred status, error or OK (an
124e13d9
SM
7271 'enum target_xfer_status' value). Save the number of addressable units
7272 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
7273
7274 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
7275 exchange between gdb and the stub could look like (?? in place of the
7276 checksum):
7277
7278 -> $m1000,4#??
7279 <- aaaabbbbccccdddd
7280
7281 -> $M1000,3:eeeeffffeeee#??
7282 <- OK
7283
7284 -> $m1000,4#??
7285 <- eeeeffffeeeedddd */
c906108c 7286
9b409511 7287static enum target_xfer_status
a76d924d 7288remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
7289 const gdb_byte *myaddr, ULONGEST len_units,
7290 int unit_size, ULONGEST *xfered_len_units,
7291 char packet_format, int use_length)
c906108c 7292{
6d820c5c 7293 struct remote_state *rs = get_remote_state ();
cfd77fa1 7294 char *p;
a76d924d
DJ
7295 char *plen = NULL;
7296 int plenlen = 0;
124e13d9
SM
7297 int todo_units;
7298 int units_written;
7299 int payload_capacity_bytes;
7300 int payload_length_bytes;
a76d924d
DJ
7301
7302 if (packet_format != 'X' && packet_format != 'M')
7303 internal_error (__FILE__, __LINE__,
9b20d036 7304 _("remote_write_bytes_aux: bad packet format"));
c906108c 7305
124e13d9 7306 if (len_units == 0)
9b409511 7307 return TARGET_XFER_EOF;
b2182ed2 7308
124e13d9 7309 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 7310
6d820c5c
DJ
7311 /* The packet buffer will be large enough for the payload;
7312 get_memory_packet_size ensures this. */
a76d924d 7313 rs->buf[0] = '\0';
c906108c 7314
a257b5bb 7315 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
7316 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7317
124e13d9 7318 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7319 if (!use_length)
0df8b418 7320 /* The comma won't be used. */
124e13d9
SM
7321 payload_capacity_bytes += 1;
7322 payload_capacity_bytes -= strlen (header);
7323 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7324
a76d924d 7325 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7326
a76d924d
DJ
7327 strcat (rs->buf, header);
7328 p = rs->buf + strlen (header);
7329
7330 /* Compute a best guess of the number of bytes actually transfered. */
7331 if (packet_format == 'X')
c906108c 7332 {
23860348 7333 /* Best guess at number of bytes that will fit. */
124e13d9 7334 todo_units = min (len_units, payload_capacity_bytes / unit_size);
a76d924d 7335 if (use_length)
124e13d9
SM
7336 payload_capacity_bytes -= hexnumlen (todo_units);
7337 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7338 }
7339 else
7340 {
124e13d9
SM
7341 /* Number of bytes that will fit. */
7342 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
a76d924d 7343 if (use_length)
124e13d9
SM
7344 payload_capacity_bytes -= hexnumlen (todo_units);
7345 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
917317f4 7346 }
a76d924d 7347
124e13d9 7348 if (todo_units <= 0)
3de11b2e 7349 internal_error (__FILE__, __LINE__,
405f8e94 7350 _("minimum packet size too small to write data"));
802188a7 7351
6765f3e5
DJ
7352 /* If we already need another packet, then try to align the end
7353 of this packet to a useful boundary. */
124e13d9
SM
7354 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7355 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7356
a257b5bb 7357 /* Append "<memaddr>". */
917317f4
JM
7358 memaddr = remote_address_masked (memaddr);
7359 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7360
a76d924d
DJ
7361 if (use_length)
7362 {
7363 /* Append ",". */
7364 *p++ = ',';
802188a7 7365
124e13d9
SM
7366 /* Append the length and retain its location and size. It may need to be
7367 adjusted once the packet body has been created. */
a76d924d 7368 plen = p;
124e13d9 7369 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7370 p += plenlen;
7371 }
a257b5bb
AC
7372
7373 /* Append ":". */
917317f4
JM
7374 *p++ = ':';
7375 *p = '\0';
802188a7 7376
a257b5bb 7377 /* Append the packet body. */
a76d924d 7378 if (packet_format == 'X')
917317f4 7379 {
917317f4
JM
7380 /* Binary mode. Send target system values byte by byte, in
7381 increasing byte addresses. Only escape certain critical
7382 characters. */
124e13d9
SM
7383 payload_length_bytes =
7384 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7385 &units_written, payload_capacity_bytes);
6765f3e5 7386
124e13d9 7387 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7388 a second try to keep the end of the packet aligned. Don't do
7389 this if the packet is tiny. */
124e13d9 7390 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7391 {
124e13d9
SM
7392 int new_todo_units;
7393
7394 new_todo_units = align_for_efficient_write (units_written, memaddr);
7395
7396 if (new_todo_units != units_written)
7397 payload_length_bytes =
7398 remote_escape_output (myaddr, new_todo_units, unit_size,
7399 (gdb_byte *) p, &units_written,
7400 payload_capacity_bytes);
6765f3e5
DJ
7401 }
7402
124e13d9
SM
7403 p += payload_length_bytes;
7404 if (use_length && units_written < todo_units)
c906108c 7405 {
802188a7 7406 /* Escape chars have filled up the buffer prematurely,
124e13d9 7407 and we have actually sent fewer units than planned.
917317f4
JM
7408 Fix-up the length field of the packet. Use the same
7409 number of characters as before. */
124e13d9
SM
7410 plen += hexnumnstr (plen, (ULONGEST) units_written,
7411 plenlen);
917317f4 7412 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7413 }
a76d924d
DJ
7414 }
7415 else
7416 {
917317f4
JM
7417 /* Normal mode: Send target system values byte by byte, in
7418 increasing byte addresses. Each byte is encoded as a two hex
7419 value. */
124e13d9
SM
7420 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7421 units_written = todo_units;
c906108c 7422 }
802188a7 7423
2e9f7625 7424 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7425 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7426
2e9f7625 7427 if (rs->buf[0] == 'E')
00d84524 7428 return TARGET_XFER_E_IO;
802188a7 7429
124e13d9
SM
7430 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7431 send fewer units than we'd planned. */
7432 *xfered_len_units = (ULONGEST) units_written;
9b409511 7433 return TARGET_XFER_OK;
c906108c
SS
7434}
7435
a76d924d
DJ
7436/* Write memory data directly to the remote machine.
7437 This does not inform the data cache; the data cache uses this.
7438 MEMADDR is the address in the remote memory space.
7439 MYADDR is the address of the buffer in our space.
7440 LEN is the number of bytes.
7441
9b409511
YQ
7442 Return the transferred status, error or OK (an
7443 'enum target_xfer_status' value). Save the number of bytes
7444 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7445
9b409511
YQ
7446static enum target_xfer_status
7447remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7448 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7449{
7450 char *packet_format = 0;
7451
7452 /* Check whether the target supports binary download. */
7453 check_binary_download (memaddr);
7454
4082afcc 7455 switch (packet_support (PACKET_X))
a76d924d
DJ
7456 {
7457 case PACKET_ENABLE:
7458 packet_format = "X";
7459 break;
7460 case PACKET_DISABLE:
7461 packet_format = "M";
7462 break;
7463 case PACKET_SUPPORT_UNKNOWN:
7464 internal_error (__FILE__, __LINE__,
7465 _("remote_write_bytes: bad internal state"));
7466 default:
7467 internal_error (__FILE__, __LINE__, _("bad switch"));
7468 }
7469
7470 return remote_write_bytes_aux (packet_format,
124e13d9 7471 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7472 packet_format[0], 1);
a76d924d
DJ
7473}
7474
9217e74e
YQ
7475/* Read memory data directly from the remote machine.
7476 This does not use the data cache; the data cache uses this.
7477 MEMADDR is the address in the remote memory space.
7478 MYADDR is the address of the buffer in our space.
124e13d9
SM
7479 LEN_UNITS is the number of addressable memory units to read..
7480 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7481
7482 Return the transferred status, error or OK (an
7483 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7484 transferred in *XFERED_LEN_UNITS.
7485
7486 See the comment of remote_write_bytes_aux for an example of
7487 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7488
7489static enum target_xfer_status
124e13d9
SM
7490remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7491 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7492{
7493 struct remote_state *rs = get_remote_state ();
124e13d9 7494 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7495 char *p;
124e13d9
SM
7496 int todo_units;
7497 int decoded_bytes;
9217e74e 7498
124e13d9 7499 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7500 /* The packet buffer will be large enough for the payload;
7501 get_memory_packet_size ensures this. */
7502
124e13d9
SM
7503 /* Number of units that will fit. */
7504 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7505
7506 /* Construct "m"<memaddr>","<len>". */
7507 memaddr = remote_address_masked (memaddr);
7508 p = rs->buf;
7509 *p++ = 'm';
7510 p += hexnumstr (p, (ULONGEST) memaddr);
7511 *p++ = ',';
124e13d9 7512 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7513 *p = '\0';
7514 putpkt (rs->buf);
7515 getpkt (&rs->buf, &rs->buf_size, 0);
7516 if (rs->buf[0] == 'E'
7517 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7518 && rs->buf[3] == '\0')
7519 return TARGET_XFER_E_IO;
7520 /* Reply describes memory byte by byte, each byte encoded as two hex
7521 characters. */
7522 p = rs->buf;
124e13d9 7523 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7524 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7525 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7526 return TARGET_XFER_OK;
7527}
7528
b55fbac4
YQ
7529/* Using the set of read-only target sections of remote, read live
7530 read-only memory.
8acf9577
YQ
7531
7532 For interface/parameters/return description see target.h,
7533 to_xfer_partial. */
7534
7535static enum target_xfer_status
b55fbac4
YQ
7536remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7537 ULONGEST memaddr, ULONGEST len,
124e13d9 7538 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7539{
7540 struct target_section *secp;
7541 struct target_section_table *table;
7542
7543 secp = target_section_by_addr (ops, memaddr);
7544 if (secp != NULL
7545 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7546 secp->the_bfd_section)
7547 & SEC_READONLY))
7548 {
7549 struct target_section *p;
7550 ULONGEST memend = memaddr + len;
7551
7552 table = target_get_section_table (ops);
7553
7554 for (p = table->sections; p < table->sections_end; p++)
7555 {
7556 if (memaddr >= p->addr)
7557 {
7558 if (memend <= p->endaddr)
7559 {
7560 /* Entire transfer is within this section. */
124e13d9 7561 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7562 xfered_len);
8acf9577
YQ
7563 }
7564 else if (memaddr >= p->endaddr)
7565 {
7566 /* This section ends before the transfer starts. */
7567 continue;
7568 }
7569 else
7570 {
7571 /* This section overlaps the transfer. Just do half. */
7572 len = p->endaddr - memaddr;
124e13d9 7573 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7574 xfered_len);
8acf9577
YQ
7575 }
7576 }
7577 }
7578 }
7579
7580 return TARGET_XFER_EOF;
7581}
7582
9217e74e
YQ
7583/* Similar to remote_read_bytes_1, but it reads from the remote stub
7584 first if the requested memory is unavailable in traceframe.
7585 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7586
9b409511 7587static enum target_xfer_status
8acf9577 7588remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7589 gdb_byte *myaddr, ULONGEST len, int unit_size,
7590 ULONGEST *xfered_len)
c906108c 7591{
6b6aa828 7592 if (len == 0)
96c4f946 7593 return TARGET_XFER_EOF;
b2182ed2 7594
8acf9577
YQ
7595 if (get_traceframe_number () != -1)
7596 {
7597 VEC(mem_range_s) *available;
7598
7599 /* If we fail to get the set of available memory, then the
7600 target does not support querying traceframe info, and so we
7601 attempt reading from the traceframe anyway (assuming the
7602 target implements the old QTro packet then). */
7603 if (traceframe_available_memory (&available, memaddr, len))
7604 {
7605 struct cleanup *old_chain;
7606
7607 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7608
7609 if (VEC_empty (mem_range_s, available)
7610 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7611 {
7612 enum target_xfer_status res;
7613
7614 /* Don't read into the traceframe's available
7615 memory. */
7616 if (!VEC_empty (mem_range_s, available))
7617 {
7618 LONGEST oldlen = len;
7619
7620 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7621 gdb_assert (len <= oldlen);
7622 }
7623
7624 do_cleanups (old_chain);
7625
7626 /* This goes through the topmost target again. */
b55fbac4 7627 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7628 len, unit_size, xfered_len);
8acf9577
YQ
7629 if (res == TARGET_XFER_OK)
7630 return TARGET_XFER_OK;
7631 else
7632 {
7633 /* No use trying further, we know some memory starting
7634 at MEMADDR isn't available. */
7635 *xfered_len = len;
7636 return TARGET_XFER_UNAVAILABLE;
7637 }
7638 }
7639
7640 /* Don't try to read more than how much is available, in
7641 case the target implements the deprecated QTro packet to
7642 cater for older GDBs (the target's knowledge of read-only
7643 sections may be outdated by now). */
7644 len = VEC_index (mem_range_s, available, 0)->length;
7645
7646 do_cleanups (old_chain);
7647 }
7648 }
7649
124e13d9 7650 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 7651}
74531fed 7652
c906108c 7653\f
c906108c 7654
a76d924d
DJ
7655/* Sends a packet with content determined by the printf format string
7656 FORMAT and the remaining arguments, then gets the reply. Returns
7657 whether the packet was a success, a failure, or unknown. */
7658
77b64a49
PA
7659static enum packet_result remote_send_printf (const char *format, ...)
7660 ATTRIBUTE_PRINTF (1, 2);
7661
2c0b251b 7662static enum packet_result
a76d924d
DJ
7663remote_send_printf (const char *format, ...)
7664{
7665 struct remote_state *rs = get_remote_state ();
7666 int max_size = get_remote_packet_size ();
a76d924d 7667 va_list ap;
a744cf53 7668
a76d924d
DJ
7669 va_start (ap, format);
7670
7671 rs->buf[0] = '\0';
7672 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7673 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7674
7675 if (putpkt (rs->buf) < 0)
7676 error (_("Communication problem with target."));
7677
7678 rs->buf[0] = '\0';
7679 getpkt (&rs->buf, &rs->buf_size, 0);
7680
7681 return packet_check_result (rs->buf);
7682}
7683
7684static void
7685restore_remote_timeout (void *p)
7686{
7687 int value = *(int *)p;
a744cf53 7688
a76d924d
DJ
7689 remote_timeout = value;
7690}
7691
7692/* Flash writing can take quite some time. We'll set
7693 effectively infinite timeout for flash operations.
7694 In future, we'll need to decide on a better approach. */
7695static const int remote_flash_timeout = 1000;
7696
7697static void
7698remote_flash_erase (struct target_ops *ops,
7699 ULONGEST address, LONGEST length)
7700{
f5656ead 7701 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7702 int saved_remote_timeout = remote_timeout;
7703 enum packet_result ret;
a76d924d
DJ
7704 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7705 &saved_remote_timeout);
a744cf53 7706
a76d924d
DJ
7707 remote_timeout = remote_flash_timeout;
7708
7709 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7710 phex (address, addr_size),
a76d924d
DJ
7711 phex (length, 4));
7712 switch (ret)
7713 {
7714 case PACKET_UNKNOWN:
7715 error (_("Remote target does not support flash erase"));
7716 case PACKET_ERROR:
7717 error (_("Error erasing flash with vFlashErase packet"));
7718 default:
7719 break;
7720 }
7721
7722 do_cleanups (back_to);
7723}
7724
9b409511
YQ
7725static enum target_xfer_status
7726remote_flash_write (struct target_ops *ops, ULONGEST address,
7727 ULONGEST length, ULONGEST *xfered_len,
7728 const gdb_byte *data)
a76d924d
DJ
7729{
7730 int saved_remote_timeout = remote_timeout;
9b409511 7731 enum target_xfer_status ret;
a76d924d 7732 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7733 &saved_remote_timeout);
a76d924d
DJ
7734
7735 remote_timeout = remote_flash_timeout;
124e13d9 7736 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 7737 xfered_len,'X', 0);
a76d924d
DJ
7738 do_cleanups (back_to);
7739
7740 return ret;
7741}
7742
7743static void
7744remote_flash_done (struct target_ops *ops)
7745{
7746 int saved_remote_timeout = remote_timeout;
7747 int ret;
7748 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7749 &saved_remote_timeout);
7750
7751 remote_timeout = remote_flash_timeout;
7752 ret = remote_send_printf ("vFlashDone");
7753 do_cleanups (back_to);
7754
7755 switch (ret)
7756 {
7757 case PACKET_UNKNOWN:
7758 error (_("Remote target does not support vFlashDone"));
7759 case PACKET_ERROR:
7760 error (_("Error finishing flash operation"));
7761 default:
7762 break;
7763 }
7764}
7765
c906108c 7766static void
fba45db2 7767remote_files_info (struct target_ops *ignore)
c906108c
SS
7768{
7769 puts_filtered ("Debugging a target over a serial line.\n");
7770}
7771\f
7772/* Stuff for dealing with the packets which are part of this protocol.
7773 See comment at top of file for details. */
7774
1927e618
PA
7775/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7776 error to higher layers. Called when a serial error is detected.
7777 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7778 the system error message for errno at function entry and final dot
7779 for output compatibility with throw_perror_with_name. */
1927e618
PA
7780
7781static void
7782unpush_and_perror (const char *string)
7783{
d6cb50a2 7784 int saved_errno = errno;
1927e618
PA
7785
7786 remote_unpush_target ();
d6cb50a2
JK
7787 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7788 safe_strerror (saved_errno));
1927e618
PA
7789}
7790
0876f84a 7791/* Read a single character from the remote end. */
c906108c
SS
7792
7793static int
fba45db2 7794readchar (int timeout)
c906108c
SS
7795{
7796 int ch;
5d93a237 7797 struct remote_state *rs = get_remote_state ();
c906108c 7798
5d93a237 7799 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7800
2acceee2 7801 if (ch >= 0)
0876f84a 7802 return ch;
2acceee2
JM
7803
7804 switch ((enum serial_rc) ch)
c906108c
SS
7805 {
7806 case SERIAL_EOF:
78a095c3 7807 remote_unpush_target ();
598d3636 7808 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7809 /* no return */
c906108c 7810 case SERIAL_ERROR:
1927e618
PA
7811 unpush_and_perror (_("Remote communication error. "
7812 "Target disconnected."));
2acceee2 7813 /* no return */
c906108c 7814 case SERIAL_TIMEOUT:
2acceee2 7815 break;
c906108c 7816 }
2acceee2 7817 return ch;
c906108c
SS
7818}
7819
c33e31fd
PA
7820/* Wrapper for serial_write that closes the target and throws if
7821 writing fails. */
7822
7823static void
7824remote_serial_write (const char *str, int len)
7825{
5d93a237
TT
7826 struct remote_state *rs = get_remote_state ();
7827
7828 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7829 {
1927e618
PA
7830 unpush_and_perror (_("Remote communication error. "
7831 "Target disconnected."));
c33e31fd
PA
7832 }
7833}
7834
6d820c5c
DJ
7835/* Send the command in *BUF to the remote machine, and read the reply
7836 into *BUF. Report an error if we get an error reply. Resize
7837 *BUF using xrealloc if necessary to hold the result, and update
7838 *SIZEOF_BUF. */
c906108c
SS
7839
7840static void
6d820c5c
DJ
7841remote_send (char **buf,
7842 long *sizeof_buf)
c906108c 7843{
6d820c5c 7844 putpkt (*buf);
c2d11a7d 7845 getpkt (buf, sizeof_buf, 0);
c906108c 7846
6d820c5c
DJ
7847 if ((*buf)[0] == 'E')
7848 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7849}
7850
6e5abd65
PA
7851/* Return a pointer to an xmalloc'ed string representing an escaped
7852 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7853 etc. The caller is responsible for releasing the returned
7854 memory. */
7855
7856static char *
7857escape_buffer (const char *buf, int n)
7858{
7859 struct cleanup *old_chain;
7860 struct ui_file *stb;
7861 char *str;
6e5abd65
PA
7862
7863 stb = mem_fileopen ();
7864 old_chain = make_cleanup_ui_file_delete (stb);
7865
6ef284bd 7866 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7867 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7868 do_cleanups (old_chain);
7869 return str;
7870}
7871
c906108c
SS
7872/* Display a null-terminated packet on stdout, for debugging, using C
7873 string notation. */
7874
7875static void
baa336ce 7876print_packet (const char *buf)
c906108c
SS
7877{
7878 puts_filtered ("\"");
43e526b9 7879 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7880 puts_filtered ("\"");
7881}
7882
7883int
baa336ce 7884putpkt (const char *buf)
c906108c
SS
7885{
7886 return putpkt_binary (buf, strlen (buf));
7887}
7888
7889/* Send a packet to the remote machine, with error checking. The data
23860348 7890 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7891 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7892 and for a possible /0 if we are debugging (remote_debug) and want
7893 to print the sent packet as a string. */
c906108c
SS
7894
7895static int
baa336ce 7896putpkt_binary (const char *buf, int cnt)
c906108c 7897{
2d717e4f 7898 struct remote_state *rs = get_remote_state ();
c906108c
SS
7899 int i;
7900 unsigned char csum = 0;
a5c0808e
PA
7901 char *buf2 = xmalloc (cnt + 6);
7902 struct cleanup *old_chain = make_cleanup (xfree, buf2);
085dd6e6 7903
c906108c
SS
7904 int ch;
7905 int tcount = 0;
7906 char *p;
dd61ec5c 7907 char *message;
c906108c 7908
e24a49d8
PA
7909 /* Catch cases like trying to read memory or listing threads while
7910 we're waiting for a stop reply. The remote server wouldn't be
7911 ready to handle this request, so we'd hang and timeout. We don't
7912 have to worry about this in synchronous mode, because in that
7913 case it's not possible to issue a command while the target is
74531fed
PA
7914 running. This is not a problem in non-stop mode, because in that
7915 case, the stub is always ready to process serial input. */
d9d41e78 7916 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7917 {
7918 error (_("Cannot execute this command while the target is running.\n"
7919 "Use the \"interrupt\" command to stop the target\n"
7920 "and then try again."));
7921 }
e24a49d8 7922
2d717e4f
DJ
7923 /* We're sending out a new packet. Make sure we don't look at a
7924 stale cached response. */
7925 rs->cached_wait_status = 0;
7926
c906108c
SS
7927 /* Copy the packet into buffer BUF2, encapsulating it
7928 and giving it a checksum. */
7929
c906108c
SS
7930 p = buf2;
7931 *p++ = '$';
7932
7933 for (i = 0; i < cnt; i++)
7934 {
7935 csum += buf[i];
7936 *p++ = buf[i];
7937 }
7938 *p++ = '#';
7939 *p++ = tohex ((csum >> 4) & 0xf);
7940 *p++ = tohex (csum & 0xf);
7941
7942 /* Send it over and over until we get a positive ack. */
7943
7944 while (1)
7945 {
7946 int started_error_output = 0;
7947
7948 if (remote_debug)
7949 {
6e5abd65
PA
7950 struct cleanup *old_chain;
7951 char *str;
7952
c906108c 7953 *p = '\0';
6e5abd65
PA
7954 str = escape_buffer (buf2, p - buf2);
7955 old_chain = make_cleanup (xfree, str);
7956 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7957 gdb_flush (gdb_stdlog);
6e5abd65 7958 do_cleanups (old_chain);
c906108c 7959 }
c33e31fd 7960 remote_serial_write (buf2, p - buf2);
c906108c 7961
a6f3e723
SL
7962 /* If this is a no acks version of the remote protocol, send the
7963 packet and move on. */
7964 if (rs->noack_mode)
7965 break;
7966
74531fed
PA
7967 /* Read until either a timeout occurs (-2) or '+' is read.
7968 Handle any notification that arrives in the mean time. */
c906108c
SS
7969 while (1)
7970 {
7971 ch = readchar (remote_timeout);
7972
c5aa993b 7973 if (remote_debug)
c906108c
SS
7974 {
7975 switch (ch)
7976 {
7977 case '+':
1216fa2c 7978 case '-':
c906108c
SS
7979 case SERIAL_TIMEOUT:
7980 case '$':
74531fed 7981 case '%':
c906108c
SS
7982 if (started_error_output)
7983 {
7984 putchar_unfiltered ('\n');
7985 started_error_output = 0;
7986 }
7987 }
7988 }
7989
7990 switch (ch)
7991 {
7992 case '+':
7993 if (remote_debug)
0f71a2f6 7994 fprintf_unfiltered (gdb_stdlog, "Ack\n");
a5c0808e 7995 do_cleanups (old_chain);
c906108c 7996 return 1;
1216fa2c
AC
7997 case '-':
7998 if (remote_debug)
7999 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 8000 /* FALLTHROUGH */
c906108c 8001 case SERIAL_TIMEOUT:
c5aa993b 8002 tcount++;
c906108c 8003 if (tcount > 3)
a5c0808e
PA
8004 {
8005 do_cleanups (old_chain);
8006 return 0;
8007 }
23860348 8008 break; /* Retransmit buffer. */
c906108c
SS
8009 case '$':
8010 {
40e3f985 8011 if (remote_debug)
2bc416ba 8012 fprintf_unfiltered (gdb_stdlog,
23860348 8013 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
8014 /* It's probably an old response sent because an ACK
8015 was lost. Gobble up the packet and ack it so it
8016 doesn't get retransmitted when we resend this
8017 packet. */
6d820c5c 8018 skip_frame ();
c33e31fd 8019 remote_serial_write ("+", 1);
23860348 8020 continue; /* Now, go look for +. */
c906108c 8021 }
74531fed
PA
8022
8023 case '%':
8024 {
8025 int val;
8026
8027 /* If we got a notification, handle it, and go back to looking
8028 for an ack. */
8029 /* We've found the start of a notification. Now
8030 collect the data. */
8031 val = read_frame (&rs->buf, &rs->buf_size);
8032 if (val >= 0)
8033 {
8034 if (remote_debug)
8035 {
6e5abd65
PA
8036 struct cleanup *old_chain;
8037 char *str;
8038
8039 str = escape_buffer (rs->buf, val);
8040 old_chain = make_cleanup (xfree, str);
8041 fprintf_unfiltered (gdb_stdlog,
8042 " Notification received: %s\n",
8043 str);
8044 do_cleanups (old_chain);
74531fed 8045 }
5965e028 8046 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
8047 /* We're in sync now, rewait for the ack. */
8048 tcount = 0;
8049 }
8050 else
8051 {
8052 if (remote_debug)
8053 {
8054 if (!started_error_output)
8055 {
8056 started_error_output = 1;
8057 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
8058 }
8059 fputc_unfiltered (ch & 0177, gdb_stdlog);
8060 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
8061 }
8062 }
8063 continue;
8064 }
8065 /* fall-through */
c906108c
SS
8066 default:
8067 if (remote_debug)
8068 {
8069 if (!started_error_output)
8070 {
8071 started_error_output = 1;
0f71a2f6 8072 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 8073 }
0f71a2f6 8074 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
8075 }
8076 continue;
8077 }
23860348 8078 break; /* Here to retransmit. */
c906108c
SS
8079 }
8080
8081#if 0
8082 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
8083 able to get out next time we call QUIT, without anything as
8084 violent as interrupt_query. If we want to provide a way out of
8085 here without getting to the next QUIT, it should be based on
8086 hitting ^C twice as in remote_wait. */
c906108c
SS
8087 if (quit_flag)
8088 {
8089 quit_flag = 0;
8090 interrupt_query ();
8091 }
8092#endif
8093 }
a5c0808e
PA
8094
8095 do_cleanups (old_chain);
a6f3e723 8096 return 0;
c906108c
SS
8097}
8098
6d820c5c
DJ
8099/* Come here after finding the start of a frame when we expected an
8100 ack. Do our best to discard the rest of this packet. */
8101
8102static void
8103skip_frame (void)
8104{
8105 int c;
8106
8107 while (1)
8108 {
8109 c = readchar (remote_timeout);
8110 switch (c)
8111 {
8112 case SERIAL_TIMEOUT:
8113 /* Nothing we can do. */
8114 return;
8115 case '#':
8116 /* Discard the two bytes of checksum and stop. */
8117 c = readchar (remote_timeout);
8118 if (c >= 0)
8119 c = readchar (remote_timeout);
8120
8121 return;
8122 case '*': /* Run length encoding. */
8123 /* Discard the repeat count. */
8124 c = readchar (remote_timeout);
8125 if (c < 0)
8126 return;
8127 break;
8128 default:
8129 /* A regular character. */
8130 break;
8131 }
8132 }
8133}
8134
c906108c 8135/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
8136 into *BUF, verifying the checksum, length, and handling run-length
8137 compression. NUL terminate the buffer. If there is not enough room,
8138 expand *BUF using xrealloc.
c906108c 8139
c2d11a7d
JM
8140 Returns -1 on error, number of characters in buffer (ignoring the
8141 trailing NULL) on success. (could be extended to return one of the
23860348 8142 SERIAL status indications). */
c2d11a7d
JM
8143
8144static long
6d820c5c
DJ
8145read_frame (char **buf_p,
8146 long *sizeof_buf)
c906108c
SS
8147{
8148 unsigned char csum;
c2d11a7d 8149 long bc;
c906108c 8150 int c;
6d820c5c 8151 char *buf = *buf_p;
a6f3e723 8152 struct remote_state *rs = get_remote_state ();
c906108c
SS
8153
8154 csum = 0;
c2d11a7d 8155 bc = 0;
c906108c
SS
8156
8157 while (1)
8158 {
8159 c = readchar (remote_timeout);
c906108c
SS
8160 switch (c)
8161 {
8162 case SERIAL_TIMEOUT:
8163 if (remote_debug)
0f71a2f6 8164 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 8165 return -1;
c906108c
SS
8166 case '$':
8167 if (remote_debug)
0f71a2f6
JM
8168 fputs_filtered ("Saw new packet start in middle of old one\n",
8169 gdb_stdlog);
23860348 8170 return -1; /* Start a new packet, count retries. */
c906108c
SS
8171 case '#':
8172 {
8173 unsigned char pktcsum;
e1b09194
AC
8174 int check_0 = 0;
8175 int check_1 = 0;
c906108c 8176
c2d11a7d 8177 buf[bc] = '\0';
c906108c 8178
e1b09194
AC
8179 check_0 = readchar (remote_timeout);
8180 if (check_0 >= 0)
8181 check_1 = readchar (remote_timeout);
802188a7 8182
e1b09194
AC
8183 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
8184 {
8185 if (remote_debug)
2bc416ba 8186 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 8187 gdb_stdlog);
e1b09194
AC
8188 return -1;
8189 }
8190 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
8191 {
8192 if (remote_debug)
2bc416ba 8193 fputs_filtered ("Communication error in checksum\n",
23860348 8194 gdb_stdlog);
40e3f985
FN
8195 return -1;
8196 }
c906108c 8197
a6f3e723
SL
8198 /* Don't recompute the checksum; with no ack packets we
8199 don't have any way to indicate a packet retransmission
8200 is necessary. */
8201 if (rs->noack_mode)
8202 return bc;
8203
e1b09194 8204 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 8205 if (csum == pktcsum)
c2d11a7d 8206 return bc;
c906108c 8207
c5aa993b 8208 if (remote_debug)
c906108c 8209 {
6e5abd65
PA
8210 struct cleanup *old_chain;
8211 char *str;
8212
8213 str = escape_buffer (buf, bc);
8214 old_chain = make_cleanup (xfree, str);
8215 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
8216 "Bad checksum, sentsum=0x%x, "
8217 "csum=0x%x, buf=%s\n",
6e5abd65
PA
8218 pktcsum, csum, str);
8219 do_cleanups (old_chain);
c906108c 8220 }
c2d11a7d 8221 /* Number of characters in buffer ignoring trailing
23860348 8222 NULL. */
c2d11a7d 8223 return -1;
c906108c 8224 }
23860348 8225 case '*': /* Run length encoding. */
c2c6d25f
JM
8226 {
8227 int repeat;
c906108c 8228
a744cf53 8229 csum += c;
b4501125
AC
8230 c = readchar (remote_timeout);
8231 csum += c;
23860348 8232 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 8233
23860348 8234 /* The character before ``*'' is repeated. */
c2d11a7d 8235
6d820c5c 8236 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 8237 {
6d820c5c
DJ
8238 if (bc + repeat - 1 >= *sizeof_buf - 1)
8239 {
8240 /* Make some more room in the buffer. */
8241 *sizeof_buf += repeat;
8242 *buf_p = xrealloc (*buf_p, *sizeof_buf);
8243 buf = *buf_p;
8244 }
8245
c2d11a7d
JM
8246 memset (&buf[bc], buf[bc - 1], repeat);
8247 bc += repeat;
c2c6d25f
JM
8248 continue;
8249 }
8250
c2d11a7d 8251 buf[bc] = '\0';
6d820c5c 8252 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 8253 return -1;
c2c6d25f 8254 }
c906108c 8255 default:
6d820c5c 8256 if (bc >= *sizeof_buf - 1)
c906108c 8257 {
6d820c5c
DJ
8258 /* Make some more room in the buffer. */
8259 *sizeof_buf *= 2;
8260 *buf_p = xrealloc (*buf_p, *sizeof_buf);
8261 buf = *buf_p;
c906108c
SS
8262 }
8263
6d820c5c
DJ
8264 buf[bc++] = c;
8265 csum += c;
8266 continue;
c906108c
SS
8267 }
8268 }
8269}
8270
8271/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8272 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8273 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8274 rather than timing out; this is used (in synchronous mode) to wait
8275 for a target that is is executing user code to stop. */
d9fcf2fb
JM
8276/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
8277 don't have to change all the calls to getpkt to deal with the
8278 return value, because at the moment I don't know what the right
23860348 8279 thing to do it for those. */
c906108c 8280void
6d820c5c
DJ
8281getpkt (char **buf,
8282 long *sizeof_buf,
c2d11a7d 8283 int forever)
d9fcf2fb
JM
8284{
8285 int timed_out;
8286
8287 timed_out = getpkt_sane (buf, sizeof_buf, forever);
8288}
8289
8290
8291/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8292 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8293 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8294 rather than timing out; this is used (in synchronous mode) to wait
8295 for a target that is is executing user code to stop. If FOREVER ==
8296 0, this function is allowed to time out gracefully and return an
74531fed
PA
8297 indication of this to the caller. Otherwise return the number of
8298 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
8299 enough reason to return to the caller. *IS_NOTIF is an output
8300 boolean that indicates whether *BUF holds a notification or not
8301 (a regular packet). */
74531fed 8302
3172dc30 8303static int
74531fed 8304getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 8305 int expecting_notif, int *is_notif)
c906108c 8306{
2d717e4f 8307 struct remote_state *rs = get_remote_state ();
c906108c
SS
8308 int c;
8309 int tries;
8310 int timeout;
df4b58fe 8311 int val = -1;
c906108c 8312
2d717e4f
DJ
8313 /* We're reading a new response. Make sure we don't look at a
8314 previously cached response. */
8315 rs->cached_wait_status = 0;
8316
6d820c5c 8317 strcpy (*buf, "timeout");
c906108c
SS
8318
8319 if (forever)
74531fed
PA
8320 timeout = watchdog > 0 ? watchdog : -1;
8321 else if (expecting_notif)
8322 timeout = 0; /* There should already be a char in the buffer. If
8323 not, bail out. */
c906108c
SS
8324 else
8325 timeout = remote_timeout;
8326
8327#define MAX_TRIES 3
8328
74531fed
PA
8329 /* Process any number of notifications, and then return when
8330 we get a packet. */
8331 for (;;)
c906108c 8332 {
d9c43928 8333 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8334 times. */
8335 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8336 {
74531fed
PA
8337 /* This can loop forever if the remote side sends us
8338 characters continuously, but if it pauses, we'll get
8339 SERIAL_TIMEOUT from readchar because of timeout. Then
8340 we'll count that as a retry.
8341
8342 Note that even when forever is set, we will only wait
8343 forever prior to the start of a packet. After that, we
8344 expect characters to arrive at a brisk pace. They should
8345 show up within remote_timeout intervals. */
8346 do
8347 c = readchar (timeout);
8348 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8349
8350 if (c == SERIAL_TIMEOUT)
8351 {
74531fed
PA
8352 if (expecting_notif)
8353 return -1; /* Don't complain, it's normal to not get
8354 anything in this case. */
8355
23860348 8356 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8357 {
2acceee2 8358 QUIT;
78a095c3 8359 remote_unpush_target ();
598d3636
JK
8360 throw_error (TARGET_CLOSE_ERROR,
8361 _("Watchdog timeout has expired. "
8362 "Target detached."));
c906108c 8363 }
c906108c 8364 if (remote_debug)
0f71a2f6 8365 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8366 }
74531fed
PA
8367 else
8368 {
8369 /* We've found the start of a packet or notification.
8370 Now collect the data. */
8371 val = read_frame (buf, sizeof_buf);
8372 if (val >= 0)
8373 break;
8374 }
8375
c33e31fd 8376 remote_serial_write ("-", 1);
c906108c 8377 }
c906108c 8378
74531fed
PA
8379 if (tries > MAX_TRIES)
8380 {
8381 /* We have tried hard enough, and just can't receive the
8382 packet/notification. Give up. */
8383 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8384
74531fed
PA
8385 /* Skip the ack char if we're in no-ack mode. */
8386 if (!rs->noack_mode)
c33e31fd 8387 remote_serial_write ("+", 1);
74531fed
PA
8388 return -1;
8389 }
c906108c 8390
74531fed
PA
8391 /* If we got an ordinary packet, return that to our caller. */
8392 if (c == '$')
c906108c
SS
8393 {
8394 if (remote_debug)
43e526b9 8395 {
6e5abd65
PA
8396 struct cleanup *old_chain;
8397 char *str;
8398
8399 str = escape_buffer (*buf, val);
8400 old_chain = make_cleanup (xfree, str);
8401 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8402 do_cleanups (old_chain);
43e526b9 8403 }
a6f3e723
SL
8404
8405 /* Skip the ack char if we're in no-ack mode. */
8406 if (!rs->noack_mode)
c33e31fd 8407 remote_serial_write ("+", 1);
fee9eda9
YQ
8408 if (is_notif != NULL)
8409 *is_notif = 0;
0876f84a 8410 return val;
c906108c
SS
8411 }
8412
74531fed
PA
8413 /* If we got a notification, handle it, and go back to looking
8414 for a packet. */
8415 else
8416 {
8417 gdb_assert (c == '%');
8418
8419 if (remote_debug)
8420 {
6e5abd65
PA
8421 struct cleanup *old_chain;
8422 char *str;
8423
8424 str = escape_buffer (*buf, val);
8425 old_chain = make_cleanup (xfree, str);
8426 fprintf_unfiltered (gdb_stdlog,
8427 " Notification received: %s\n",
8428 str);
8429 do_cleanups (old_chain);
74531fed 8430 }
fee9eda9
YQ
8431 if (is_notif != NULL)
8432 *is_notif = 1;
c906108c 8433
5965e028 8434 handle_notification (rs->notif_state, *buf);
c906108c 8435
74531fed 8436 /* Notifications require no acknowledgement. */
a6f3e723 8437
74531fed 8438 if (expecting_notif)
fee9eda9 8439 return val;
74531fed
PA
8440 }
8441 }
8442}
8443
8444static int
8445getpkt_sane (char **buf, long *sizeof_buf, int forever)
8446{
fee9eda9 8447 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8448}
8449
8450static int
fee9eda9
YQ
8451getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8452 int *is_notif)
74531fed 8453{
fee9eda9
YQ
8454 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8455 is_notif);
c906108c 8456}
74531fed 8457
cbb8991c
DB
8458/* Check whether EVENT is a fork event for the process specified
8459 by the pid passed in DATA, and if it is, kill the fork child. */
8460
8461static int
8462kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8463 QUEUE_ITER (stop_reply_p) *iter,
8464 stop_reply_p event,
8465 void *data)
8466{
8467 struct queue_iter_param *param = data;
8468 int parent_pid = *(int *) param->input;
8469
8470 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8471 {
8472 struct remote_state *rs = get_remote_state ();
8473 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8474 int res;
8475
8476 res = remote_vkill (child_pid, rs);
8477 if (res != 0)
8478 error (_("Can't kill fork child process %d"), child_pid);
8479 }
8480
8481 return 1;
8482}
8483
8484/* Kill any new fork children of process PID that haven't been
8485 processed by follow_fork. */
8486
8487static void
8488kill_new_fork_children (int pid, struct remote_state *rs)
8489{
8490 struct thread_info *thread;
8491 struct notif_client *notif = &notif_client_stop;
8492 struct queue_iter_param param;
8493
8494 /* Kill the fork child threads of any threads in process PID
8495 that are stopped at a fork event. */
8496 ALL_NON_EXITED_THREADS (thread)
8497 {
8498 struct target_waitstatus *ws = &thread->pending_follow;
8499
8500 if (is_pending_fork_parent (ws, pid, thread->ptid))
8501 {
8502 struct remote_state *rs = get_remote_state ();
8503 int child_pid = ptid_get_pid (ws->value.related_pid);
8504 int res;
8505
8506 res = remote_vkill (child_pid, rs);
8507 if (res != 0)
8508 error (_("Can't kill fork child process %d"), child_pid);
8509 }
8510 }
8511
8512 /* Check for any pending fork events (not reported or processed yet)
8513 in process PID and kill those fork child threads as well. */
8514 remote_notif_get_pending_events (notif);
8515 param.input = &pid;
8516 param.output = NULL;
8517 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8518 kill_child_of_pending_fork, &param);
8519}
8520
c906108c
SS
8521\f
8522static void
7d85a9c0 8523remote_kill (struct target_ops *ops)
43ff13b4 8524{
0fdf84ca
PA
8525
8526 /* Catch errors so the user can quit from gdb even when we
23860348 8527 aren't on speaking terms with the remote system. */
492d29ea 8528 TRY
0fdf84ca
PA
8529 {
8530 putpkt ("k");
8531 }
492d29ea 8532 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8533 {
8534 if (ex.error == TARGET_CLOSE_ERROR)
8535 {
8536 /* If we got an (EOF) error that caused the target
8537 to go away, then we're done, that's what we wanted.
8538 "k" is susceptible to cause a premature EOF, given
8539 that the remote server isn't actually required to
8540 reply to "k", and it can happen that it doesn't
8541 even get to reply ACK to the "k". */
8542 return;
8543 }
8544
8545 /* Otherwise, something went wrong. We didn't actually kill
8546 the target. Just propagate the exception, and let the
8547 user or higher layers decide what to do. */
8548 throw_exception (ex);
8549 }
492d29ea 8550 END_CATCH
43ff13b4 8551
0fdf84ca
PA
8552 /* We've killed the remote end, we get to mourn it. Since this is
8553 target remote, single-process, mourning the inferior also
8554 unpushes remote_ops. */
43ff13b4
JM
8555 target_mourn_inferior ();
8556}
8557
82f73884
PA
8558static int
8559remote_vkill (int pid, struct remote_state *rs)
8560{
4082afcc 8561 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8562 return -1;
8563
8564 /* Tell the remote target to detach. */
bba74b36 8565 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8566 putpkt (rs->buf);
8567 getpkt (&rs->buf, &rs->buf_size, 0);
8568
4082afcc
PA
8569 switch (packet_ok (rs->buf,
8570 &remote_protocol_packets[PACKET_vKill]))
8571 {
8572 case PACKET_OK:
8573 return 0;
8574 case PACKET_ERROR:
8575 return 1;
8576 case PACKET_UNKNOWN:
8577 return -1;
8578 default:
8579 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8580 }
82f73884
PA
8581}
8582
8583static void
7d85a9c0 8584extended_remote_kill (struct target_ops *ops)
82f73884
PA
8585{
8586 int res;
8587 int pid = ptid_get_pid (inferior_ptid);
8588 struct remote_state *rs = get_remote_state ();
8589
cbb8991c
DB
8590 /* If we're stopped while forking and we haven't followed yet, kill the
8591 child task. We need to do this before killing the parent task
8592 because if this is a vfork then the parent will be sleeping. */
8593 kill_new_fork_children (pid, rs);
8594
82f73884 8595 res = remote_vkill (pid, rs);
901f9912 8596 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8597 {
8598 /* Don't try 'k' on a multi-process aware stub -- it has no way
8599 to specify the pid. */
8600
8601 putpkt ("k");
8602#if 0
8603 getpkt (&rs->buf, &rs->buf_size, 0);
8604 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8605 res = 1;
8606#else
8607 /* Don't wait for it to die. I'm not really sure it matters whether
8608 we do or not. For the existing stubs, kill is a noop. */
8609 res = 0;
8610#endif
8611 }
8612
8613 if (res != 0)
8614 error (_("Can't kill process"));
8615
82f73884
PA
8616 target_mourn_inferior ();
8617}
8618
c906108c 8619static void
20f796c9 8620remote_mourn (struct target_ops *target)
c906108c
SS
8621{
8622 unpush_target (target);
ce5ce7ed 8623
8a2492ee
PA
8624 /* remote_close takes care of doing most of the clean up. */
8625 generic_mourn_inferior ();
c906108c
SS
8626}
8627
2d717e4f 8628static void
20f796c9 8629extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8630{
8631 struct remote_state *rs = get_remote_state ();
c906108c 8632
e24a49d8
PA
8633 /* In case we got here due to an error, but we're going to stay
8634 connected. */
8635 rs->waiting_for_stop_reply = 0;
8636
dc1981d7
PA
8637 /* If the current general thread belonged to the process we just
8638 detached from or has exited, the remote side current general
8639 thread becomes undefined. Considering a case like this:
8640
8641 - We just got here due to a detach.
8642 - The process that we're detaching from happens to immediately
8643 report a global breakpoint being hit in non-stop mode, in the
8644 same thread we had selected before.
8645 - GDB attaches to this process again.
8646 - This event happens to be the next event we handle.
8647
8648 GDB would consider that the current general thread didn't need to
8649 be set on the stub side (with Hg), since for all it knew,
8650 GENERAL_THREAD hadn't changed.
8651
8652 Notice that although in all-stop mode, the remote server always
8653 sets the current thread to the thread reporting the stop event,
8654 that doesn't happen in non-stop mode; in non-stop, the stub *must
8655 not* change the current thread when reporting a breakpoint hit,
8656 due to the decoupling of event reporting and event handling.
8657
8658 To keep things simple, we always invalidate our notion of the
8659 current thread. */
47f8a51d 8660 record_currthread (rs, minus_one_ptid);
dc1981d7 8661
2d717e4f
DJ
8662 /* Unlike "target remote", we do not want to unpush the target; then
8663 the next time the user says "run", we won't be connected. */
8664
48aa3c27
PA
8665 /* Call common code to mark the inferior as not running. */
8666 generic_mourn_inferior ();
8667
d729566a 8668 if (!have_inferiors ())
2d717e4f 8669 {
82f73884
PA
8670 if (!remote_multi_process_p (rs))
8671 {
8672 /* Check whether the target is running now - some remote stubs
8673 automatically restart after kill. */
8674 putpkt ("?");
8675 getpkt (&rs->buf, &rs->buf_size, 0);
8676
8677 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8678 {
3e43a32a
MS
8679 /* Assume that the target has been restarted. Set
8680 inferior_ptid so that bits of core GDB realizes
8681 there's something here, e.g., so that the user can
8682 say "kill" again. */
82f73884
PA
8683 inferior_ptid = magic_null_ptid;
8684 }
82f73884 8685 }
2d717e4f
DJ
8686 }
8687}
c906108c 8688
03583c20 8689static int
2bfc0540 8690extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8691{
4082afcc 8692 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8693}
8694
8695static void
8696extended_remote_disable_randomization (int val)
8697{
8698 struct remote_state *rs = get_remote_state ();
8699 char *reply;
8700
bba74b36
YQ
8701 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8702 val);
03583c20
UW
8703 putpkt (rs->buf);
8704 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8705 if (*reply == '\0')
8706 error (_("Target does not support QDisableRandomization."));
8707 if (strcmp (reply, "OK") != 0)
8708 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8709}
8710
2d717e4f
DJ
8711static int
8712extended_remote_run (char *args)
8713{
8714 struct remote_state *rs = get_remote_state ();
2d717e4f 8715 int len;
94585166 8716 const char *remote_exec_file = get_remote_exec_file ();
c906108c 8717
2d717e4f
DJ
8718 /* If the user has disabled vRun support, or we have detected that
8719 support is not available, do not try it. */
4082afcc 8720 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8721 return -1;
424163ea 8722
2d717e4f
DJ
8723 strcpy (rs->buf, "vRun;");
8724 len = strlen (rs->buf);
c906108c 8725
2d717e4f
DJ
8726 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8727 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8728 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8729 strlen (remote_exec_file));
2d717e4f 8730
d1a41061 8731 gdb_assert (args != NULL);
2d717e4f
DJ
8732 if (*args)
8733 {
8734 struct cleanup *back_to;
8735 int i;
8736 char **argv;
8737
d1a41061 8738 argv = gdb_buildargv (args);
6e366df1 8739 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8740 for (i = 0; argv[i] != NULL; i++)
8741 {
8742 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8743 error (_("Argument list too long for run packet"));
8744 rs->buf[len++] = ';';
9f1b45b0
TT
8745 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8746 strlen (argv[i]));
2d717e4f
DJ
8747 }
8748 do_cleanups (back_to);
8749 }
8750
8751 rs->buf[len++] = '\0';
8752
8753 putpkt (rs->buf);
8754 getpkt (&rs->buf, &rs->buf_size, 0);
8755
4082afcc 8756 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8757 {
4082afcc 8758 case PACKET_OK:
3405876a 8759 /* We have a wait response. All is well. */
2d717e4f 8760 return 0;
4082afcc
PA
8761 case PACKET_UNKNOWN:
8762 return -1;
8763 case PACKET_ERROR:
2d717e4f
DJ
8764 if (remote_exec_file[0] == '\0')
8765 error (_("Running the default executable on the remote target failed; "
8766 "try \"set remote exec-file\"?"));
8767 else
8768 error (_("Running \"%s\" on the remote target failed"),
8769 remote_exec_file);
4082afcc
PA
8770 default:
8771 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8772 }
c906108c
SS
8773}
8774
2d717e4f
DJ
8775/* In the extended protocol we want to be able to do things like
8776 "run" and have them basically work as expected. So we need
8777 a special create_inferior function. We support changing the
8778 executable file and the command line arguments, but not the
8779 environment. */
8780
43ff13b4 8781static void
77a19445
TT
8782extended_remote_create_inferior (struct target_ops *ops,
8783 char *exec_file, char *args,
8784 char **env, int from_tty)
43ff13b4 8785{
3405876a
PA
8786 int run_worked;
8787 char *stop_reply;
8788 struct remote_state *rs = get_remote_state ();
94585166 8789 const char *remote_exec_file = get_remote_exec_file ();
3405876a 8790
43ff13b4 8791 /* If running asynchronously, register the target file descriptor
23860348 8792 with the event loop. */
75c99385 8793 if (target_can_async_p ())
6a3753b3 8794 target_async (1);
43ff13b4 8795
03583c20 8796 /* Disable address space randomization if requested (and supported). */
2bfc0540 8797 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8798 extended_remote_disable_randomization (disable_randomization);
8799
43ff13b4 8800 /* Now restart the remote server. */
3405876a
PA
8801 run_worked = extended_remote_run (args) != -1;
8802 if (!run_worked)
2d717e4f
DJ
8803 {
8804 /* vRun was not supported. Fail if we need it to do what the
8805 user requested. */
8806 if (remote_exec_file[0])
8807 error (_("Remote target does not support \"set remote exec-file\""));
8808 if (args[0])
8809 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8810
2d717e4f
DJ
8811 /* Fall back to "R". */
8812 extended_remote_restart ();
8813 }
424163ea 8814
6c95b8df
PA
8815 if (!have_inferiors ())
8816 {
8817 /* Clean up from the last time we ran, before we mark the target
8818 running again. This will mark breakpoints uninserted, and
8819 get_offsets may insert breakpoints. */
8820 init_thread_list ();
8821 init_wait_for_inferior ();
8822 }
45280a52 8823
3405876a
PA
8824 /* vRun's success return is a stop reply. */
8825 stop_reply = run_worked ? rs->buf : NULL;
8826 add_current_inferior_and_thread (stop_reply);
c0a2216e 8827
2d717e4f
DJ
8828 /* Get updated offsets, if the stub uses qOffsets. */
8829 get_offsets ();
2d717e4f 8830}
c906108c 8831\f
c5aa993b 8832
b775012e
LM
8833/* Given a location's target info BP_TGT and the packet buffer BUF, output
8834 the list of conditions (in agent expression bytecode format), if any, the
8835 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8836 started from BUF and ended at BUF_END. */
b775012e
LM
8837
8838static int
8839remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8840 struct bp_target_info *bp_tgt, char *buf,
8841 char *buf_end)
b775012e
LM
8842{
8843 struct agent_expr *aexpr = NULL;
8844 int i, ix;
8845 char *pkt;
8846 char *buf_start = buf;
8847
8848 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8849 return 0;
8850
8851 buf += strlen (buf);
bba74b36 8852 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8853 buf++;
8854
8855 /* Send conditions to the target and free the vector. */
8856 for (ix = 0;
8857 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8858 ix++)
8859 {
bba74b36 8860 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8861 buf += strlen (buf);
8862 for (i = 0; i < aexpr->len; ++i)
8863 buf = pack_hex_byte (buf, aexpr->buf[i]);
8864 *buf = '\0';
8865 }
b775012e
LM
8866 return 0;
8867}
8868
d3ce09f5
SS
8869static void
8870remote_add_target_side_commands (struct gdbarch *gdbarch,
8871 struct bp_target_info *bp_tgt, char *buf)
8872{
8873 struct agent_expr *aexpr = NULL;
8874 int i, ix;
8875
8876 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8877 return;
8878
8879 buf += strlen (buf);
8880
8881 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8882 buf += strlen (buf);
8883
8884 /* Concatenate all the agent expressions that are commands into the
8885 cmds parameter. */
8886 for (ix = 0;
8887 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8888 ix++)
8889 {
8890 sprintf (buf, "X%x,", aexpr->len);
8891 buf += strlen (buf);
8892 for (i = 0; i < aexpr->len; ++i)
8893 buf = pack_hex_byte (buf, aexpr->buf[i]);
8894 *buf = '\0';
8895 }
d3ce09f5
SS
8896}
8897
8181d85f
DJ
8898/* Insert a breakpoint. On targets that have software breakpoint
8899 support, we ask the remote target to do the work; on targets
8900 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8901
8902static int
3db08215
MM
8903remote_insert_breakpoint (struct target_ops *ops,
8904 struct gdbarch *gdbarch,
a6d9a66e 8905 struct bp_target_info *bp_tgt)
c906108c 8906{
d471ea57
AC
8907 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8908 If it succeeds, then set the support to PACKET_ENABLE. If it
8909 fails, and the user has explicitly requested the Z support then
23860348 8910 report an error, otherwise, mark it disabled and go on. */
802188a7 8911
4082afcc 8912 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8913 {
0d5ed153 8914 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8915 struct remote_state *rs;
bba74b36 8916 char *p, *endbuf;
7c0f6dcc 8917 int bpsize;
b775012e 8918 struct condition_list *cond = NULL;
4fff2411 8919
28439a30
PA
8920 /* Make sure the remote is pointing at the right process, if
8921 necessary. */
8922 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8923 set_general_process ();
8924
a1dcb23a 8925 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8926
8927 rs = get_remote_state ();
8928 p = rs->buf;
bba74b36 8929 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8930
96baa820
JM
8931 *(p++) = 'Z';
8932 *(p++) = '0';
8933 *(p++) = ',';
7c0f6dcc 8934 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8935 p += hexnumstr (p, addr);
bba74b36 8936 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8937
efcc2da7 8938 if (remote_supports_cond_breakpoints (ops))
bba74b36 8939 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8940
78eff0ec 8941 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8942 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8943
6d820c5c
DJ
8944 putpkt (rs->buf);
8945 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8946
6d820c5c 8947 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8948 {
d471ea57
AC
8949 case PACKET_ERROR:
8950 return -1;
8951 case PACKET_OK:
7c0f6dcc
JL
8952 bp_tgt->placed_address = addr;
8953 bp_tgt->placed_size = bpsize;
d471ea57
AC
8954 return 0;
8955 case PACKET_UNKNOWN:
8956 break;
96baa820
JM
8957 }
8958 }
c906108c 8959
0000e5cc
PA
8960 /* If this breakpoint has target-side commands but this stub doesn't
8961 support Z0 packets, throw error. */
8962 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8963 throw_error (NOT_SUPPORTED_ERROR, _("\
8964Target doesn't support breakpoints that have target side commands."));
8965
3db08215 8966 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8967}
8968
8969static int
3db08215
MM
8970remote_remove_breakpoint (struct target_ops *ops,
8971 struct gdbarch *gdbarch,
a6d9a66e 8972 struct bp_target_info *bp_tgt)
c906108c 8973{
8181d85f 8974 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8975 struct remote_state *rs = get_remote_state ();
96baa820 8976
4082afcc 8977 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8978 {
6d820c5c 8979 char *p = rs->buf;
bba74b36 8980 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8981
28439a30
PA
8982 /* Make sure the remote is pointing at the right process, if
8983 necessary. */
8984 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8985 set_general_process ();
8986
96baa820
JM
8987 *(p++) = 'z';
8988 *(p++) = '0';
8989 *(p++) = ',';
8990
8181d85f
DJ
8991 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8992 p += hexnumstr (p, addr);
bba74b36 8993 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8994
6d820c5c
DJ
8995 putpkt (rs->buf);
8996 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8997
6d820c5c 8998 return (rs->buf[0] == 'E');
96baa820
JM
8999 }
9000
3db08215 9001 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
9002}
9003
f486487f 9004static enum Z_packet_type
d471ea57
AC
9005watchpoint_to_Z_packet (int type)
9006{
9007 switch (type)
9008 {
9009 case hw_write:
bb858e6a 9010 return Z_PACKET_WRITE_WP;
d471ea57
AC
9011 break;
9012 case hw_read:
bb858e6a 9013 return Z_PACKET_READ_WP;
d471ea57
AC
9014 break;
9015 case hw_access:
bb858e6a 9016 return Z_PACKET_ACCESS_WP;
d471ea57
AC
9017 break;
9018 default:
8e65ff28 9019 internal_error (__FILE__, __LINE__,
e2e0b3e5 9020 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
9021 }
9022}
9023
3c3bea1c 9024static int
f486487f
SM
9025remote_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9026 enum target_hw_bp_type type, struct expression *cond)
96baa820 9027{
d01949b6 9028 struct remote_state *rs = get_remote_state ();
bba74b36 9029 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9030 char *p;
d471ea57 9031 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 9032
4082afcc 9033 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 9034 return 1;
802188a7 9035
28439a30
PA
9036 /* Make sure the remote is pointing at the right process, if
9037 necessary. */
9038 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9039 set_general_process ();
9040
bba74b36 9041 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 9042 p = strchr (rs->buf, '\0');
96baa820
JM
9043 addr = remote_address_masked (addr);
9044 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9045 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 9046
6d820c5c
DJ
9047 putpkt (rs->buf);
9048 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9049
6d820c5c 9050 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
9051 {
9052 case PACKET_ERROR:
d471ea57 9053 return -1;
85d721b8
PA
9054 case PACKET_UNKNOWN:
9055 return 1;
d471ea57
AC
9056 case PACKET_OK:
9057 return 0;
9058 }
8e65ff28 9059 internal_error (__FILE__, __LINE__,
e2e0b3e5 9060 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
9061}
9062
283002cf
MR
9063static int
9064remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
9065 CORE_ADDR start, int length)
9066{
9067 CORE_ADDR diff = remote_address_masked (addr - start);
9068
9069 return diff < length;
9070}
9071
d471ea57 9072
3c3bea1c 9073static int
f486487f
SM
9074remote_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
9075 enum target_hw_bp_type type, struct expression *cond)
96baa820 9076{
d01949b6 9077 struct remote_state *rs = get_remote_state ();
bba74b36 9078 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 9079 char *p;
d471ea57
AC
9080 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
9081
4082afcc 9082 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 9083 return -1;
802188a7 9084
28439a30
PA
9085 /* Make sure the remote is pointing at the right process, if
9086 necessary. */
9087 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9088 set_general_process ();
9089
bba74b36 9090 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 9091 p = strchr (rs->buf, '\0');
96baa820
JM
9092 addr = remote_address_masked (addr);
9093 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9094 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
9095 putpkt (rs->buf);
9096 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9097
6d820c5c 9098 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
9099 {
9100 case PACKET_ERROR:
9101 case PACKET_UNKNOWN:
9102 return -1;
9103 case PACKET_OK:
9104 return 0;
9105 }
8e65ff28 9106 internal_error (__FILE__, __LINE__,
e2e0b3e5 9107 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
9108}
9109
3c3bea1c 9110
501eef12 9111int remote_hw_watchpoint_limit = -1;
480a3f21 9112int remote_hw_watchpoint_length_limit = -1;
501eef12 9113int remote_hw_breakpoint_limit = -1;
d471ea57 9114
480a3f21 9115static int
31568a15
TT
9116remote_region_ok_for_hw_watchpoint (struct target_ops *self,
9117 CORE_ADDR addr, int len)
480a3f21
PW
9118{
9119 if (remote_hw_watchpoint_length_limit == 0)
9120 return 0;
9121 else if (remote_hw_watchpoint_length_limit < 0)
9122 return 1;
9123 else if (len <= remote_hw_watchpoint_length_limit)
9124 return 1;
9125 else
9126 return 0;
9127}
9128
b9362cc7 9129static int
5461485a 9130remote_check_watch_resources (struct target_ops *self,
f486487f 9131 enum bptype type, int cnt, int ot)
96baa820 9132{
3c3bea1c
GS
9133 if (type == bp_hardware_breakpoint)
9134 {
9135 if (remote_hw_breakpoint_limit == 0)
9136 return 0;
501eef12
AC
9137 else if (remote_hw_breakpoint_limit < 0)
9138 return 1;
3c3bea1c
GS
9139 else if (cnt <= remote_hw_breakpoint_limit)
9140 return 1;
9141 }
9142 else
9143 {
9144 if (remote_hw_watchpoint_limit == 0)
9145 return 0;
501eef12
AC
9146 else if (remote_hw_watchpoint_limit < 0)
9147 return 1;
3c3bea1c
GS
9148 else if (ot)
9149 return -1;
9150 else if (cnt <= remote_hw_watchpoint_limit)
9151 return 1;
9152 }
9153 return -1;
9154}
9155
f7e6eed5
PA
9156/* The to_stopped_by_sw_breakpoint method of target remote. */
9157
9158static int
9159remote_stopped_by_sw_breakpoint (struct target_ops *ops)
9160{
9161 struct remote_state *rs = get_remote_state ();
9162
9163 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
9164}
9165
9166/* The to_supports_stopped_by_sw_breakpoint method of target
9167 remote. */
9168
9169static int
9170remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
9171{
9172 struct remote_state *rs = get_remote_state ();
9173
9174 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
9175}
9176
9177/* The to_stopped_by_hw_breakpoint method of target remote. */
9178
9179static int
9180remote_stopped_by_hw_breakpoint (struct target_ops *ops)
9181{
9182 struct remote_state *rs = get_remote_state ();
9183
9184 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
9185}
9186
9187/* The to_supports_stopped_by_hw_breakpoint method of target
9188 remote. */
9189
9190static int
9191remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
9192{
9193 struct remote_state *rs = get_remote_state ();
9194
9195 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
9196}
9197
b9362cc7 9198static int
6a109b6b 9199remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 9200{
ee154bee
TT
9201 struct remote_state *rs = get_remote_state ();
9202
f7e6eed5 9203 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
9204}
9205
4aa7a7f5
JJ
9206static int
9207remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 9208{
ee154bee 9209 struct remote_state *rs = get_remote_state ();
4aa7a7f5 9210 int rc = 0;
a744cf53 9211
6a109b6b 9212 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 9213 {
ee154bee 9214 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
9215 rc = 1;
9216 }
9217
9218 return rc;
3c3bea1c
GS
9219}
9220
9221
9222static int
23a26771 9223remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 9224 struct bp_target_info *bp_tgt)
3c3bea1c 9225{
0d5ed153 9226 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 9227 struct remote_state *rs;
bba74b36 9228 char *p, *endbuf;
dd61ec5c 9229 char *message;
0d5ed153 9230 int bpsize;
802188a7 9231
c8189ed1 9232 /* The length field should be set to the size of a breakpoint
8181d85f 9233 instruction, even though we aren't inserting one ourselves. */
c8189ed1 9234
0d5ed153 9235 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 9236
4082afcc 9237 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9238 return -1;
2bc416ba 9239
28439a30
PA
9240 /* Make sure the remote is pointing at the right process, if
9241 necessary. */
9242 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9243 set_general_process ();
9244
4fff2411
JZ
9245 rs = get_remote_state ();
9246 p = rs->buf;
bba74b36 9247 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 9248
96baa820
JM
9249 *(p++) = 'Z';
9250 *(p++) = '1';
9251 *(p++) = ',';
802188a7 9252
0d5ed153 9253 addr = remote_address_masked (addr);
96baa820 9254 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 9255 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 9256
efcc2da7 9257 if (remote_supports_cond_breakpoints (self))
bba74b36 9258 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 9259
78eff0ec 9260 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
9261 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9262
6d820c5c
DJ
9263 putpkt (rs->buf);
9264 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9265
6d820c5c 9266 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9267 {
9268 case PACKET_ERROR:
dd61ec5c
MW
9269 if (rs->buf[1] == '.')
9270 {
9271 message = strchr (rs->buf + 2, '.');
9272 if (message)
0316657e 9273 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
9274 }
9275 return -1;
d471ea57
AC
9276 case PACKET_UNKNOWN:
9277 return -1;
9278 case PACKET_OK:
0d5ed153
MR
9279 bp_tgt->placed_address = addr;
9280 bp_tgt->placed_size = bpsize;
d471ea57
AC
9281 return 0;
9282 }
8e65ff28 9283 internal_error (__FILE__, __LINE__,
e2e0b3e5 9284 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
9285}
9286
d471ea57 9287
802188a7 9288static int
a64dc96c 9289remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 9290 struct bp_target_info *bp_tgt)
96baa820 9291{
8181d85f 9292 CORE_ADDR addr;
d01949b6 9293 struct remote_state *rs = get_remote_state ();
6d820c5c 9294 char *p = rs->buf;
bba74b36 9295 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 9296
4082afcc 9297 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9298 return -1;
802188a7 9299
28439a30
PA
9300 /* Make sure the remote is pointing at the right process, if
9301 necessary. */
9302 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9303 set_general_process ();
9304
96baa820
JM
9305 *(p++) = 'z';
9306 *(p++) = '1';
9307 *(p++) = ',';
802188a7 9308
8181d85f 9309 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 9310 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9311 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 9312
6d820c5c
DJ
9313 putpkt (rs->buf);
9314 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 9315
6d820c5c 9316 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9317 {
9318 case PACKET_ERROR:
9319 case PACKET_UNKNOWN:
9320 return -1;
9321 case PACKET_OK:
9322 return 0;
9323 }
8e65ff28 9324 internal_error (__FILE__, __LINE__,
e2e0b3e5 9325 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9326}
96baa820 9327
4a5e7a5b
PA
9328/* Verify memory using the "qCRC:" request. */
9329
9330static int
9331remote_verify_memory (struct target_ops *ops,
9332 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9333{
9334 struct remote_state *rs = get_remote_state ();
9335 unsigned long host_crc, target_crc;
9336 char *tmp;
9337
936d2992
PA
9338 /* It doesn't make sense to use qCRC if the remote target is
9339 connected but not running. */
9340 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9341 {
9342 enum packet_result result;
28439a30 9343
936d2992
PA
9344 /* Make sure the remote is pointing at the right process. */
9345 set_general_process ();
4a5e7a5b 9346
936d2992
PA
9347 /* FIXME: assumes lma can fit into long. */
9348 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9349 (long) lma, (long) size);
9350 putpkt (rs->buf);
4a5e7a5b 9351
936d2992
PA
9352 /* Be clever; compute the host_crc before waiting for target
9353 reply. */
9354 host_crc = xcrc32 (data, size, 0xffffffff);
9355
9356 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9357
936d2992
PA
9358 result = packet_ok (rs->buf,
9359 &remote_protocol_packets[PACKET_qCRC]);
9360 if (result == PACKET_ERROR)
9361 return -1;
9362 else if (result == PACKET_OK)
9363 {
9364 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9365 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9366
936d2992
PA
9367 return (host_crc == target_crc);
9368 }
9369 }
4a5e7a5b 9370
936d2992 9371 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9372}
9373
c906108c
SS
9374/* compare-sections command
9375
9376 With no arguments, compares each loadable section in the exec bfd
9377 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9378 Useful for verifying the image on the target against the exec file. */
e514a9d6 9379
c906108c 9380static void
fba45db2 9381compare_sections_command (char *args, int from_tty)
c906108c
SS
9382{
9383 asection *s;
c906108c 9384 struct cleanup *old_chain;
948f8e3d 9385 gdb_byte *sectdata;
ce359b09 9386 const char *sectname;
c906108c
SS
9387 bfd_size_type size;
9388 bfd_vma lma;
9389 int matched = 0;
9390 int mismatched = 0;
4a5e7a5b 9391 int res;
95cf3b38 9392 int read_only = 0;
c906108c
SS
9393
9394 if (!exec_bfd)
8a3fe4f8 9395 error (_("command cannot be used without an exec file"));
c906108c 9396
28439a30
PA
9397 /* Make sure the remote is pointing at the right process. */
9398 set_general_process ();
9399
95cf3b38
DT
9400 if (args != NULL && strcmp (args, "-r") == 0)
9401 {
9402 read_only = 1;
9403 args = NULL;
9404 }
9405
c5aa993b 9406 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9407 {
9408 if (!(s->flags & SEC_LOAD))
0df8b418 9409 continue; /* Skip non-loadable section. */
c906108c 9410
95cf3b38
DT
9411 if (read_only && (s->flags & SEC_READONLY) == 0)
9412 continue; /* Skip writeable sections */
9413
2c500098 9414 size = bfd_get_section_size (s);
c906108c 9415 if (size == 0)
0df8b418 9416 continue; /* Skip zero-length section. */
c906108c 9417
ce359b09 9418 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9419 if (args && strcmp (args, sectname) != 0)
0df8b418 9420 continue; /* Not the section selected by user. */
c906108c 9421
0df8b418 9422 matched = 1; /* Do this section. */
c906108c 9423 lma = s->lma;
c906108c 9424
c906108c 9425 sectdata = xmalloc (size);
b8c9b27d 9426 old_chain = make_cleanup (xfree, sectdata);
c906108c 9427 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9428
4a5e7a5b
PA
9429 res = target_verify_memory (sectdata, lma, size);
9430
9431 if (res == -1)
5af949e3 9432 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9433 paddress (target_gdbarch (), lma),
9434 paddress (target_gdbarch (), lma + size));
c906108c 9435
5af949e3 9436 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9437 paddress (target_gdbarch (), lma),
9438 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9439 if (res)
c906108c
SS
9440 printf_filtered ("matched.\n");
9441 else
c5aa993b
JM
9442 {
9443 printf_filtered ("MIS-MATCHED!\n");
9444 mismatched++;
9445 }
c906108c
SS
9446
9447 do_cleanups (old_chain);
9448 }
9449 if (mismatched > 0)
936d2992 9450 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9451the loaded file\n"));
c906108c 9452 if (args && !matched)
a3f17187 9453 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9454}
9455
0e7f50da
UW
9456/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9457 into remote target. The number of bytes written to the remote
9458 target is returned, or -1 for error. */
9459
9b409511 9460static enum target_xfer_status
0e7f50da
UW
9461remote_write_qxfer (struct target_ops *ops, const char *object_name,
9462 const char *annex, const gdb_byte *writebuf,
9b409511 9463 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9464 struct packet_config *packet)
9465{
9466 int i, buf_len;
9467 ULONGEST n;
0e7f50da
UW
9468 struct remote_state *rs = get_remote_state ();
9469 int max_size = get_memory_write_packet_size ();
9470
9471 if (packet->support == PACKET_DISABLE)
2ed4b548 9472 return TARGET_XFER_E_IO;
0e7f50da
UW
9473
9474 /* Insert header. */
9475 i = snprintf (rs->buf, max_size,
9476 "qXfer:%s:write:%s:%s:",
9477 object_name, annex ? annex : "",
9478 phex_nz (offset, sizeof offset));
9479 max_size -= (i + 1);
9480
9481 /* Escape as much data as fits into rs->buf. */
9482 buf_len = remote_escape_output
124e13d9 9483 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9484
9485 if (putpkt_binary (rs->buf, i + buf_len) < 0
9486 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9487 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9488 return TARGET_XFER_E_IO;
0e7f50da
UW
9489
9490 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9491
9492 *xfered_len = n;
9493 return TARGET_XFER_OK;
0e7f50da
UW
9494}
9495
0876f84a
DJ
9496/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9497 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9498 number of bytes read is returned, or 0 for EOF, or -1 for error.
9499 The number of bytes read may be less than LEN without indicating an
9500 EOF. PACKET is checked and updated to indicate whether the remote
9501 target supports this object. */
9502
9b409511 9503static enum target_xfer_status
0876f84a
DJ
9504remote_read_qxfer (struct target_ops *ops, const char *object_name,
9505 const char *annex,
9506 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9507 ULONGEST *xfered_len,
0876f84a
DJ
9508 struct packet_config *packet)
9509{
0876f84a 9510 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9511 LONGEST i, n, packet_len;
9512
9513 if (packet->support == PACKET_DISABLE)
2ed4b548 9514 return TARGET_XFER_E_IO;
0876f84a
DJ
9515
9516 /* Check whether we've cached an end-of-object packet that matches
9517 this request. */
8e88304f 9518 if (rs->finished_object)
0876f84a 9519 {
8e88304f
TT
9520 if (strcmp (object_name, rs->finished_object) == 0
9521 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9522 && offset == rs->finished_offset)
9b409511
YQ
9523 return TARGET_XFER_EOF;
9524
0876f84a
DJ
9525
9526 /* Otherwise, we're now reading something different. Discard
9527 the cache. */
8e88304f
TT
9528 xfree (rs->finished_object);
9529 xfree (rs->finished_annex);
9530 rs->finished_object = NULL;
9531 rs->finished_annex = NULL;
0876f84a
DJ
9532 }
9533
9534 /* Request only enough to fit in a single packet. The actual data
9535 may not, since we don't know how much of it will need to be escaped;
9536 the target is free to respond with slightly less data. We subtract
9537 five to account for the response type and the protocol frame. */
9538 n = min (get_remote_packet_size () - 5, len);
9539 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9540 object_name, annex ? annex : "",
9541 phex_nz (offset, sizeof offset),
9542 phex_nz (n, sizeof n));
9543 i = putpkt (rs->buf);
9544 if (i < 0)
2ed4b548 9545 return TARGET_XFER_E_IO;
0876f84a
DJ
9546
9547 rs->buf[0] = '\0';
9548 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9549 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9550 return TARGET_XFER_E_IO;
0876f84a
DJ
9551
9552 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9553 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9554
9555 /* 'm' means there is (or at least might be) more data after this
9556 batch. That does not make sense unless there's at least one byte
9557 of data in this reply. */
9558 if (rs->buf[0] == 'm' && packet_len == 1)
9559 error (_("Remote qXfer reply contained no data."));
9560
9561 /* Got some data. */
bc20a4af
PA
9562 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9563 packet_len - 1, readbuf, n);
0876f84a
DJ
9564
9565 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9566 or possibly empty. If we have the final block of a non-empty
9567 object, record this fact to bypass a subsequent partial read. */
9568 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9569 {
8e88304f
TT
9570 rs->finished_object = xstrdup (object_name);
9571 rs->finished_annex = xstrdup (annex ? annex : "");
9572 rs->finished_offset = offset + i;
0876f84a
DJ
9573 }
9574
9b409511
YQ
9575 if (i == 0)
9576 return TARGET_XFER_EOF;
9577 else
9578 {
9579 *xfered_len = i;
9580 return TARGET_XFER_OK;
9581 }
0876f84a
DJ
9582}
9583
9b409511 9584static enum target_xfer_status
4b8a223f 9585remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9586 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9587 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9588 ULONGEST *xfered_len)
c906108c 9589{
82f73884 9590 struct remote_state *rs;
c906108c 9591 int i;
6d820c5c 9592 char *p2;
1e3ff5ad 9593 char query_type;
124e13d9 9594 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9595
e6e4e701 9596 set_remote_traceframe ();
82f73884
PA
9597 set_general_thread (inferior_ptid);
9598
9599 rs = get_remote_state ();
9600
b2182ed2 9601 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9602 if (object == TARGET_OBJECT_MEMORY)
9603 {
2d717e4f
DJ
9604 /* If the remote target is connected but not running, we should
9605 pass this request down to a lower stratum (e.g. the executable
9606 file). */
9607 if (!target_has_execution)
9b409511 9608 return TARGET_XFER_EOF;
2d717e4f 9609
21e3b9b9 9610 if (writebuf != NULL)
124e13d9
SM
9611 return remote_write_bytes (offset, writebuf, len, unit_size,
9612 xfered_len);
21e3b9b9 9613 else
124e13d9
SM
9614 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9615 xfered_len);
21e3b9b9
DJ
9616 }
9617
0df8b418 9618 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9619 if (object == TARGET_OBJECT_SPU)
9620 {
9621 if (readbuf)
9622 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9623 xfered_len, &remote_protocol_packets
9624 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9625 else
9626 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9627 xfered_len, &remote_protocol_packets
9628 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9629 }
9630
4aa995e1
PA
9631 /* Handle extra signal info using qxfer packets. */
9632 if (object == TARGET_OBJECT_SIGNAL_INFO)
9633 {
9634 if (readbuf)
9635 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9636 xfered_len, &remote_protocol_packets
4aa995e1
PA
9637 [PACKET_qXfer_siginfo_read]);
9638 else
3e43a32a 9639 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9640 writebuf, offset, len, xfered_len,
4aa995e1
PA
9641 &remote_protocol_packets
9642 [PACKET_qXfer_siginfo_write]);
9643 }
9644
0fb4aa4b
PA
9645 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9646 {
9647 if (readbuf)
3e43a32a 9648 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9649 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9650 &remote_protocol_packets
9651 [PACKET_qXfer_statictrace_read]);
9652 else
2ed4b548 9653 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9654 }
9655
a76d924d
DJ
9656 /* Only handle flash writes. */
9657 if (writebuf != NULL)
9658 {
9659 LONGEST xfered;
9660
9661 switch (object)
9662 {
9663 case TARGET_OBJECT_FLASH:
9b409511
YQ
9664 return remote_flash_write (ops, offset, len, xfered_len,
9665 writebuf);
a76d924d
DJ
9666
9667 default:
2ed4b548 9668 return TARGET_XFER_E_IO;
a76d924d
DJ
9669 }
9670 }
4b8a223f 9671
1e3ff5ad
AC
9672 /* Map pre-existing objects onto letters. DO NOT do this for new
9673 objects!!! Instead specify new query packets. */
9674 switch (object)
c906108c 9675 {
1e3ff5ad
AC
9676 case TARGET_OBJECT_AVR:
9677 query_type = 'R';
9678 break;
802188a7
RM
9679
9680 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9681 gdb_assert (annex == NULL);
9682 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9683 xfered_len,
0876f84a 9684 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9685
23181151
DJ
9686 case TARGET_OBJECT_AVAILABLE_FEATURES:
9687 return remote_read_qxfer
9b409511 9688 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9689 &remote_protocol_packets[PACKET_qXfer_features]);
9690
cfa9d6d9
DJ
9691 case TARGET_OBJECT_LIBRARIES:
9692 return remote_read_qxfer
9b409511 9693 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9694 &remote_protocol_packets[PACKET_qXfer_libraries]);
9695
2268b414
JK
9696 case TARGET_OBJECT_LIBRARIES_SVR4:
9697 return remote_read_qxfer
9b409511 9698 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9699 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9700
fd79ecee
DJ
9701 case TARGET_OBJECT_MEMORY_MAP:
9702 gdb_assert (annex == NULL);
9703 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9704 xfered_len,
fd79ecee
DJ
9705 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9706
07e059b5
VP
9707 case TARGET_OBJECT_OSDATA:
9708 /* Should only get here if we're connected. */
5d93a237 9709 gdb_assert (rs->remote_desc);
07e059b5 9710 return remote_read_qxfer
9b409511 9711 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9712 &remote_protocol_packets[PACKET_qXfer_osdata]);
9713
dc146f7c
VP
9714 case TARGET_OBJECT_THREADS:
9715 gdb_assert (annex == NULL);
9716 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9717 xfered_len,
dc146f7c
VP
9718 &remote_protocol_packets[PACKET_qXfer_threads]);
9719
b3b9301e
PA
9720 case TARGET_OBJECT_TRACEFRAME_INFO:
9721 gdb_assert (annex == NULL);
9722 return remote_read_qxfer
9b409511 9723 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9724 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9725
9726 case TARGET_OBJECT_FDPIC:
9727 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9728 xfered_len,
78d85199 9729 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9730
9731 case TARGET_OBJECT_OPENVMS_UIB:
9732 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9733 xfered_len,
169081d0
TG
9734 &remote_protocol_packets[PACKET_qXfer_uib]);
9735
9accd112
MM
9736 case TARGET_OBJECT_BTRACE:
9737 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9738 xfered_len,
9accd112
MM
9739 &remote_protocol_packets[PACKET_qXfer_btrace]);
9740
f4abbc16
MM
9741 case TARGET_OBJECT_BTRACE_CONF:
9742 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9743 len, xfered_len,
9744 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9745
c78fa86a
GB
9746 case TARGET_OBJECT_EXEC_FILE:
9747 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9748 len, xfered_len,
9749 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9750
1e3ff5ad 9751 default:
2ed4b548 9752 return TARGET_XFER_E_IO;
c906108c
SS
9753 }
9754
0df8b418 9755 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9756 large enough let the caller deal with it. */
ea9c271d 9757 if (len < get_remote_packet_size ())
2ed4b548 9758 return TARGET_XFER_E_IO;
ea9c271d 9759 len = get_remote_packet_size ();
1e3ff5ad 9760
23860348 9761 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9762 if (!rs->remote_desc)
8a3fe4f8 9763 error (_("remote query is only available after target open"));
c906108c 9764
1e3ff5ad 9765 gdb_assert (annex != NULL);
4b8a223f 9766 gdb_assert (readbuf != NULL);
c906108c 9767
6d820c5c 9768 p2 = rs->buf;
c906108c
SS
9769 *p2++ = 'q';
9770 *p2++ = query_type;
9771
23860348
MS
9772 /* We used one buffer char for the remote protocol q command and
9773 another for the query type. As the remote protocol encapsulation
9774 uses 4 chars plus one extra in case we are debugging
9775 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9776 string. */
c906108c 9777 i = 0;
ea9c271d 9778 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9779 {
1e3ff5ad
AC
9780 /* Bad caller may have sent forbidden characters. */
9781 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9782 *p2++ = annex[i];
c906108c
SS
9783 i++;
9784 }
1e3ff5ad
AC
9785 *p2 = '\0';
9786 gdb_assert (annex[i] == '\0');
c906108c 9787
6d820c5c 9788 i = putpkt (rs->buf);
c5aa993b 9789 if (i < 0)
2ed4b548 9790 return TARGET_XFER_E_IO;
c906108c 9791
6d820c5c
DJ
9792 getpkt (&rs->buf, &rs->buf_size, 0);
9793 strcpy ((char *) readbuf, rs->buf);
c906108c 9794
9b409511
YQ
9795 *xfered_len = strlen ((char *) readbuf);
9796 return TARGET_XFER_OK;
c906108c
SS
9797}
9798
08388c79
DE
9799static int
9800remote_search_memory (struct target_ops* ops,
9801 CORE_ADDR start_addr, ULONGEST search_space_len,
9802 const gdb_byte *pattern, ULONGEST pattern_len,
9803 CORE_ADDR *found_addrp)
9804{
f5656ead 9805 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9806 struct remote_state *rs = get_remote_state ();
9807 int max_size = get_memory_write_packet_size ();
9808 struct packet_config *packet =
9809 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9810 /* Number of packet bytes used to encode the pattern;
9811 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9812 int escaped_pattern_len;
0df8b418 9813 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9814 int used_pattern_len;
9815 int i;
9816 int found;
9817 ULONGEST found_addr;
9818
9819 /* Don't go to the target if we don't have to.
9820 This is done before checking packet->support to avoid the possibility that
9821 a success for this edge case means the facility works in general. */
9822 if (pattern_len > search_space_len)
9823 return 0;
9824 if (pattern_len == 0)
9825 {
9826 *found_addrp = start_addr;
9827 return 1;
9828 }
9829
9830 /* If we already know the packet isn't supported, fall back to the simple
9831 way of searching memory. */
9832
4082afcc 9833 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9834 {
9835 /* Target doesn't provided special support, fall back and use the
9836 standard support (copy memory and do the search here). */
9837 return simple_search_memory (ops, start_addr, search_space_len,
9838 pattern, pattern_len, found_addrp);
9839 }
9840
28439a30
PA
9841 /* Make sure the remote is pointing at the right process. */
9842 set_general_process ();
9843
08388c79
DE
9844 /* Insert header. */
9845 i = snprintf (rs->buf, max_size,
9846 "qSearch:memory:%s;%s;",
5af949e3 9847 phex_nz (start_addr, addr_size),
08388c79
DE
9848 phex_nz (search_space_len, sizeof (search_space_len)));
9849 max_size -= (i + 1);
9850
9851 /* Escape as much data as fits into rs->buf. */
9852 escaped_pattern_len =
124e13d9 9853 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
9854 &used_pattern_len, max_size);
9855
9856 /* Bail if the pattern is too large. */
9857 if (used_pattern_len != pattern_len)
9b20d036 9858 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9859
9860 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9861 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9862 || packet_ok (rs->buf, packet) != PACKET_OK)
9863 {
9864 /* The request may not have worked because the command is not
9865 supported. If so, fall back to the simple way. */
9866 if (packet->support == PACKET_DISABLE)
9867 {
9868 return simple_search_memory (ops, start_addr, search_space_len,
9869 pattern, pattern_len, found_addrp);
9870 }
9871 return -1;
9872 }
9873
9874 if (rs->buf[0] == '0')
9875 found = 0;
9876 else if (rs->buf[0] == '1')
9877 {
9878 found = 1;
9879 if (rs->buf[1] != ',')
10e0fa18 9880 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9881 unpack_varlen_hex (rs->buf + 2, &found_addr);
9882 *found_addrp = found_addr;
9883 }
9884 else
10e0fa18 9885 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9886
9887 return found;
9888}
9889
96baa820 9890static void
a30bf1f1 9891remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9892 struct ui_file *outbuf)
96baa820 9893{
d01949b6 9894 struct remote_state *rs = get_remote_state ();
2e9f7625 9895 char *p = rs->buf;
96baa820 9896
5d93a237 9897 if (!rs->remote_desc)
8a3fe4f8 9898 error (_("remote rcmd is only available after target open"));
96baa820 9899
23860348 9900 /* Send a NULL command across as an empty command. */
7be570e7
JM
9901 if (command == NULL)
9902 command = "";
9903
23860348 9904 /* The query prefix. */
2e9f7625
DJ
9905 strcpy (rs->buf, "qRcmd,");
9906 p = strchr (rs->buf, '\0');
96baa820 9907
3e43a32a
MS
9908 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9909 > get_remote_packet_size ())
8a3fe4f8 9910 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9911
23860348 9912 /* Encode the actual command. */
a30bf1f1 9913 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9914
6d820c5c 9915 if (putpkt (rs->buf) < 0)
8a3fe4f8 9916 error (_("Communication problem with target."));
96baa820
JM
9917
9918 /* get/display the response */
9919 while (1)
9920 {
2e9f7625
DJ
9921 char *buf;
9922
00bf0b85 9923 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9924 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9925 rs->buf[0] = '\0';
5b37825d
PW
9926 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9927 {
9928 /* Timeout. Continue to (try to) read responses.
9929 This is better than stopping with an error, assuming the stub
9930 is still executing the (long) monitor command.
9931 If needed, the user can interrupt gdb using C-c, obtaining
9932 an effect similar to stop on timeout. */
9933 continue;
9934 }
2e9f7625 9935 buf = rs->buf;
96baa820 9936 if (buf[0] == '\0')
8a3fe4f8 9937 error (_("Target does not support this command."));
96baa820
JM
9938 if (buf[0] == 'O' && buf[1] != 'K')
9939 {
23860348 9940 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9941 continue;
9942 }
9943 if (strcmp (buf, "OK") == 0)
9944 break;
7be570e7
JM
9945 if (strlen (buf) == 3 && buf[0] == 'E'
9946 && isdigit (buf[1]) && isdigit (buf[2]))
9947 {
8a3fe4f8 9948 error (_("Protocol error with Rcmd"));
7be570e7 9949 }
96baa820
JM
9950 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9951 {
9952 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9953
96baa820
JM
9954 fputc_unfiltered (c, outbuf);
9955 }
9956 break;
9957 }
9958}
9959
fd79ecee
DJ
9960static VEC(mem_region_s) *
9961remote_memory_map (struct target_ops *ops)
9962{
9963 VEC(mem_region_s) *result = NULL;
9964 char *text = target_read_stralloc (&current_target,
9965 TARGET_OBJECT_MEMORY_MAP, NULL);
9966
9967 if (text)
9968 {
9969 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9970
fd79ecee
DJ
9971 result = parse_memory_map (text);
9972 do_cleanups (back_to);
9973 }
9974
9975 return result;
9976}
9977
c906108c 9978static void
fba45db2 9979packet_command (char *args, int from_tty)
c906108c 9980{
d01949b6 9981 struct remote_state *rs = get_remote_state ();
c906108c 9982
5d93a237 9983 if (!rs->remote_desc)
8a3fe4f8 9984 error (_("command can only be used with remote target"));
c906108c 9985
c5aa993b 9986 if (!args)
8a3fe4f8 9987 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9988
9989 puts_filtered ("sending: ");
9990 print_packet (args);
9991 puts_filtered ("\n");
9992 putpkt (args);
9993
6d820c5c 9994 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9995 puts_filtered ("received: ");
6d820c5c 9996 print_packet (rs->buf);
c906108c
SS
9997 puts_filtered ("\n");
9998}
9999
10000#if 0
23860348 10001/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 10002
a14ed312 10003static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 10004
a14ed312 10005static void threadset_test_cmd (char *cmd, int tty);
c906108c 10006
a14ed312 10007static void threadalive_test (char *cmd, int tty);
c906108c 10008
a14ed312 10009static void threadlist_test_cmd (char *cmd, int tty);
c906108c 10010
23860348 10011int get_and_display_threadinfo (threadref *ref);
c906108c 10012
a14ed312 10013static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 10014
23860348 10015static int thread_display_step (threadref *ref, void *context);
c906108c 10016
a14ed312 10017static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 10018
a14ed312 10019static void init_remote_threadtests (void);
c906108c 10020
23860348 10021#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
10022
10023static void
fba45db2 10024threadset_test_cmd (char *cmd, int tty)
c906108c
SS
10025{
10026 int sample_thread = SAMPLE_THREAD;
10027
a3f17187 10028 printf_filtered (_("Remote threadset test\n"));
79d7f229 10029 set_general_thread (sample_thread);
c906108c
SS
10030}
10031
10032
10033static void
fba45db2 10034threadalive_test (char *cmd, int tty)
c906108c
SS
10035{
10036 int sample_thread = SAMPLE_THREAD;
79d7f229 10037 int pid = ptid_get_pid (inferior_ptid);
ba348170 10038 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 10039
79d7f229 10040 if (remote_thread_alive (ptid))
c906108c
SS
10041 printf_filtered ("PASS: Thread alive test\n");
10042 else
10043 printf_filtered ("FAIL: Thread alive test\n");
10044}
10045
23860348 10046void output_threadid (char *title, threadref *ref);
c906108c
SS
10047
10048void
fba45db2 10049output_threadid (char *title, threadref *ref)
c906108c
SS
10050{
10051 char hexid[20];
10052
23860348 10053 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
10054 hexid[16] = 0;
10055 printf_filtered ("%s %s\n", title, (&hexid[0]));
10056}
10057
10058static void
fba45db2 10059threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
10060{
10061 int startflag = 1;
10062 threadref nextthread;
10063 int done, result_count;
10064 threadref threadlist[3];
10065
10066 printf_filtered ("Remote Threadlist test\n");
10067 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
10068 &result_count, &threadlist[0]))
10069 printf_filtered ("FAIL: threadlist test\n");
10070 else
10071 {
10072 threadref *scan = threadlist;
10073 threadref *limit = scan + result_count;
10074
10075 while (scan < limit)
10076 output_threadid (" thread ", scan++);
10077 }
10078}
10079
10080void
fba45db2 10081display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
10082{
10083 output_threadid ("Threadid: ", &info->threadid);
10084 printf_filtered ("Name: %s\n ", info->shortname);
10085 printf_filtered ("State: %s\n", info->display);
10086 printf_filtered ("other: %s\n\n", info->more_display);
10087}
10088
10089int
fba45db2 10090get_and_display_threadinfo (threadref *ref)
c906108c
SS
10091{
10092 int result;
10093 int set;
10094 struct gdb_ext_thread_info threadinfo;
10095
10096 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
10097 | TAG_MOREDISPLAY | TAG_DISPLAY;
10098 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
10099 display_thread_info (&threadinfo);
10100 return result;
10101}
10102
10103static void
fba45db2 10104threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
10105{
10106 int athread = SAMPLE_THREAD;
10107 threadref thread;
10108 int set;
10109
10110 int_to_threadref (&thread, athread);
10111 printf_filtered ("Remote Threadinfo test\n");
10112 if (!get_and_display_threadinfo (&thread))
10113 printf_filtered ("FAIL cannot get thread info\n");
10114}
10115
10116static int
fba45db2 10117thread_display_step (threadref *ref, void *context)
c906108c
SS
10118{
10119 /* output_threadid(" threadstep ",ref); *//* simple test */
10120 return get_and_display_threadinfo (ref);
10121}
10122
10123static void
fba45db2 10124threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
10125{
10126 printf_filtered ("Remote Threadlist update test\n");
10127 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
10128}
10129
10130static void
10131init_remote_threadtests (void)
10132{
3e43a32a
MS
10133 add_com ("tlist", class_obscure, threadlist_test_cmd,
10134 _("Fetch and print the remote list of "
10135 "thread identifiers, one pkt only"));
c906108c 10136 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 10137 _("Fetch and display info about one thread"));
c906108c 10138 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 10139 _("Test setting to a different thread"));
c906108c 10140 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 10141 _("Iterate through updating all remote thread info"));
c906108c 10142 add_com ("talive", class_obscure, threadalive_test,
1bedd215 10143 _(" Remote thread alive test "));
c906108c
SS
10144}
10145
10146#endif /* 0 */
10147
f3fb8c85
MS
10148/* Convert a thread ID to a string. Returns the string in a static
10149 buffer. */
10150
10151static char *
117de6a9 10152remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 10153{
79d7f229 10154 static char buf[64];
82f73884 10155 struct remote_state *rs = get_remote_state ();
f3fb8c85 10156
7cee1e54
PA
10157 if (ptid_equal (ptid, null_ptid))
10158 return normal_pid_to_str (ptid);
10159 else if (ptid_is_pid (ptid))
ecd0ada5
PA
10160 {
10161 /* Printing an inferior target id. */
10162
10163 /* When multi-process extensions are off, there's no way in the
10164 remote protocol to know the remote process id, if there's any
10165 at all. There's one exception --- when we're connected with
10166 target extended-remote, and we manually attached to a process
10167 with "attach PID". We don't record anywhere a flag that
10168 allows us to distinguish that case from the case of
10169 connecting with extended-remote and the stub already being
10170 attached to a process, and reporting yes to qAttached, hence
10171 no smart special casing here. */
10172 if (!remote_multi_process_p (rs))
10173 {
10174 xsnprintf (buf, sizeof buf, "Remote target");
10175 return buf;
10176 }
10177
10178 return normal_pid_to_str (ptid);
82f73884 10179 }
ecd0ada5 10180 else
79d7f229 10181 {
ecd0ada5
PA
10182 if (ptid_equal (magic_null_ptid, ptid))
10183 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 10184 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
10185 if (ptid_get_lwp (ptid) == 0)
10186 return normal_pid_to_str (ptid);
10187 else
10188 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
10189 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
10190 else
10191 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 10192 ptid_get_lwp (ptid));
79d7f229
PA
10193 return buf;
10194 }
f3fb8c85
MS
10195}
10196
38691318
KB
10197/* Get the address of the thread local variable in OBJFILE which is
10198 stored at OFFSET within the thread local storage for thread PTID. */
10199
10200static CORE_ADDR
117de6a9
PA
10201remote_get_thread_local_address (struct target_ops *ops,
10202 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 10203{
4082afcc 10204 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
10205 {
10206 struct remote_state *rs = get_remote_state ();
6d820c5c 10207 char *p = rs->buf;
82f73884 10208 char *endp = rs->buf + get_remote_packet_size ();
571dd617 10209 enum packet_result result;
38691318
KB
10210
10211 strcpy (p, "qGetTLSAddr:");
10212 p += strlen (p);
82f73884 10213 p = write_ptid (p, endp, ptid);
38691318
KB
10214 *p++ = ',';
10215 p += hexnumstr (p, offset);
10216 *p++ = ',';
10217 p += hexnumstr (p, lm);
10218 *p++ = '\0';
10219
6d820c5c
DJ
10220 putpkt (rs->buf);
10221 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
10222 result = packet_ok (rs->buf,
10223 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 10224 if (result == PACKET_OK)
38691318
KB
10225 {
10226 ULONGEST result;
10227
6d820c5c 10228 unpack_varlen_hex (rs->buf, &result);
38691318
KB
10229 return result;
10230 }
571dd617 10231 else if (result == PACKET_UNKNOWN)
109c3e39
AC
10232 throw_error (TLS_GENERIC_ERROR,
10233 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 10234 else
109c3e39
AC
10235 throw_error (TLS_GENERIC_ERROR,
10236 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
10237 }
10238 else
109c3e39
AC
10239 throw_error (TLS_GENERIC_ERROR,
10240 _("TLS not supported or disabled on this target"));
38691318
KB
10241 /* Not reached. */
10242 return 0;
10243}
10244
711e434b
PM
10245/* Provide thread local base, i.e. Thread Information Block address.
10246 Returns 1 if ptid is found and thread_local_base is non zero. */
10247
70221824 10248static int
bd7ae0f5 10249remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 10250{
4082afcc 10251 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
10252 {
10253 struct remote_state *rs = get_remote_state ();
10254 char *p = rs->buf;
10255 char *endp = rs->buf + get_remote_packet_size ();
10256 enum packet_result result;
10257
10258 strcpy (p, "qGetTIBAddr:");
10259 p += strlen (p);
10260 p = write_ptid (p, endp, ptid);
10261 *p++ = '\0';
10262
10263 putpkt (rs->buf);
10264 getpkt (&rs->buf, &rs->buf_size, 0);
10265 result = packet_ok (rs->buf,
10266 &remote_protocol_packets[PACKET_qGetTIBAddr]);
10267 if (result == PACKET_OK)
10268 {
10269 ULONGEST result;
10270
10271 unpack_varlen_hex (rs->buf, &result);
10272 if (addr)
10273 *addr = (CORE_ADDR) result;
10274 return 1;
10275 }
10276 else if (result == PACKET_UNKNOWN)
10277 error (_("Remote target doesn't support qGetTIBAddr packet"));
10278 else
10279 error (_("Remote target failed to process qGetTIBAddr request"));
10280 }
10281 else
10282 error (_("qGetTIBAddr not supported or disabled on this target"));
10283 /* Not reached. */
10284 return 0;
10285}
10286
29709017
DJ
10287/* Support for inferring a target description based on the current
10288 architecture and the size of a 'g' packet. While the 'g' packet
10289 can have any size (since optional registers can be left off the
10290 end), some sizes are easily recognizable given knowledge of the
10291 approximate architecture. */
10292
10293struct remote_g_packet_guess
10294{
10295 int bytes;
10296 const struct target_desc *tdesc;
10297};
10298typedef struct remote_g_packet_guess remote_g_packet_guess_s;
10299DEF_VEC_O(remote_g_packet_guess_s);
10300
10301struct remote_g_packet_data
10302{
10303 VEC(remote_g_packet_guess_s) *guesses;
10304};
10305
10306static struct gdbarch_data *remote_g_packet_data_handle;
10307
10308static void *
10309remote_g_packet_data_init (struct obstack *obstack)
10310{
10311 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
10312}
10313
10314void
10315register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
10316 const struct target_desc *tdesc)
10317{
10318 struct remote_g_packet_data *data
10319 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
10320 struct remote_g_packet_guess new_guess, *guess;
10321 int ix;
10322
10323 gdb_assert (tdesc != NULL);
10324
10325 for (ix = 0;
10326 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10327 ix++)
10328 if (guess->bytes == bytes)
10329 internal_error (__FILE__, __LINE__,
9b20d036 10330 _("Duplicate g packet description added for size %d"),
29709017
DJ
10331 bytes);
10332
10333 new_guess.bytes = bytes;
10334 new_guess.tdesc = tdesc;
10335 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10336}
10337
d962ef82
DJ
10338/* Return 1 if remote_read_description would do anything on this target
10339 and architecture, 0 otherwise. */
10340
10341static int
10342remote_read_description_p (struct target_ops *target)
10343{
10344 struct remote_g_packet_data *data
f5656ead 10345 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
10346
10347 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10348 return 1;
10349
10350 return 0;
10351}
10352
29709017
DJ
10353static const struct target_desc *
10354remote_read_description (struct target_ops *target)
10355{
10356 struct remote_g_packet_data *data
f5656ead 10357 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 10358
d962ef82
DJ
10359 /* Do not try this during initial connection, when we do not know
10360 whether there is a running but stopped thread. */
10361 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10362 return target->beneath->to_read_description (target->beneath);
d962ef82 10363
29709017
DJ
10364 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10365 {
10366 struct remote_g_packet_guess *guess;
10367 int ix;
10368 int bytes = send_g_packet ();
10369
10370 for (ix = 0;
10371 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10372 ix++)
10373 if (guess->bytes == bytes)
10374 return guess->tdesc;
10375
10376 /* We discard the g packet. A minor optimization would be to
10377 hold on to it, and fill the register cache once we have selected
10378 an architecture, but it's too tricky to do safely. */
10379 }
10380
2117c711 10381 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10382}
10383
a6b151f1
DJ
10384/* Remote file transfer support. This is host-initiated I/O, not
10385 target-initiated; for target-initiated, see remote-fileio.c. */
10386
10387/* If *LEFT is at least the length of STRING, copy STRING to
10388 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10389 decrease *LEFT. Otherwise raise an error. */
10390
10391static void
10392remote_buffer_add_string (char **buffer, int *left, char *string)
10393{
10394 int len = strlen (string);
10395
10396 if (len > *left)
10397 error (_("Packet too long for target."));
10398
10399 memcpy (*buffer, string, len);
10400 *buffer += len;
10401 *left -= len;
10402
10403 /* NUL-terminate the buffer as a convenience, if there is
10404 room. */
10405 if (*left)
10406 **buffer = '\0';
10407}
10408
10409/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10410 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10411 decrease *LEFT. Otherwise raise an error. */
10412
10413static void
10414remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10415 int len)
10416{
10417 if (2 * len > *left)
10418 error (_("Packet too long for target."));
10419
10420 bin2hex (bytes, *buffer, len);
10421 *buffer += 2 * len;
10422 *left -= 2 * len;
10423
10424 /* NUL-terminate the buffer as a convenience, if there is
10425 room. */
10426 if (*left)
10427 **buffer = '\0';
10428}
10429
10430/* If *LEFT is large enough, convert VALUE to hex and add it to
10431 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10432 decrease *LEFT. Otherwise raise an error. */
10433
10434static void
10435remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10436{
10437 int len = hexnumlen (value);
10438
10439 if (len > *left)
10440 error (_("Packet too long for target."));
10441
10442 hexnumstr (*buffer, value);
10443 *buffer += len;
10444 *left -= len;
10445
10446 /* NUL-terminate the buffer as a convenience, if there is
10447 room. */
10448 if (*left)
10449 **buffer = '\0';
10450}
10451
10452/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10453 value, *REMOTE_ERRNO to the remote error number or zero if none
10454 was included, and *ATTACHMENT to point to the start of the annex
10455 if any. The length of the packet isn't needed here; there may
10456 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10457
10458 Return 0 if the packet could be parsed, -1 if it could not. If
10459 -1 is returned, the other variables may not be initialized. */
10460
10461static int
10462remote_hostio_parse_result (char *buffer, int *retcode,
10463 int *remote_errno, char **attachment)
10464{
10465 char *p, *p2;
10466
10467 *remote_errno = 0;
10468 *attachment = NULL;
10469
10470 if (buffer[0] != 'F')
10471 return -1;
10472
10473 errno = 0;
10474 *retcode = strtol (&buffer[1], &p, 16);
10475 if (errno != 0 || p == &buffer[1])
10476 return -1;
10477
10478 /* Check for ",errno". */
10479 if (*p == ',')
10480 {
10481 errno = 0;
10482 *remote_errno = strtol (p + 1, &p2, 16);
10483 if (errno != 0 || p + 1 == p2)
10484 return -1;
10485 p = p2;
10486 }
10487
10488 /* Check for ";attachment". If there is no attachment, the
10489 packet should end here. */
10490 if (*p == ';')
10491 {
10492 *attachment = p + 1;
10493 return 0;
10494 }
10495 else if (*p == '\0')
10496 return 0;
10497 else
10498 return -1;
10499}
10500
10501/* Send a prepared I/O packet to the target and read its response.
10502 The prepared packet is in the global RS->BUF before this function
10503 is called, and the answer is there when we return.
10504
10505 COMMAND_BYTES is the length of the request to send, which may include
10506 binary data. WHICH_PACKET is the packet configuration to check
10507 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10508 is set to the error number and -1 is returned. Otherwise the value
10509 returned by the function is returned.
10510
10511 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10512 attachment is expected; an error will be reported if there's a
10513 mismatch. If one is found, *ATTACHMENT will be set to point into
10514 the packet buffer and *ATTACHMENT_LEN will be set to the
10515 attachment's length. */
10516
10517static int
10518remote_hostio_send_command (int command_bytes, int which_packet,
10519 int *remote_errno, char **attachment,
10520 int *attachment_len)
10521{
10522 struct remote_state *rs = get_remote_state ();
10523 int ret, bytes_read;
10524 char *attachment_tmp;
10525
5d93a237 10526 if (!rs->remote_desc
4082afcc 10527 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10528 {
10529 *remote_errno = FILEIO_ENOSYS;
10530 return -1;
10531 }
10532
10533 putpkt_binary (rs->buf, command_bytes);
10534 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10535
10536 /* If it timed out, something is wrong. Don't try to parse the
10537 buffer. */
10538 if (bytes_read < 0)
10539 {
10540 *remote_errno = FILEIO_EINVAL;
10541 return -1;
10542 }
10543
10544 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10545 {
10546 case PACKET_ERROR:
10547 *remote_errno = FILEIO_EINVAL;
10548 return -1;
10549 case PACKET_UNKNOWN:
10550 *remote_errno = FILEIO_ENOSYS;
10551 return -1;
10552 case PACKET_OK:
10553 break;
10554 }
10555
10556 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10557 &attachment_tmp))
10558 {
10559 *remote_errno = FILEIO_EINVAL;
10560 return -1;
10561 }
10562
10563 /* Make sure we saw an attachment if and only if we expected one. */
10564 if ((attachment_tmp == NULL && attachment != NULL)
10565 || (attachment_tmp != NULL && attachment == NULL))
10566 {
10567 *remote_errno = FILEIO_EINVAL;
10568 return -1;
10569 }
10570
10571 /* If an attachment was found, it must point into the packet buffer;
10572 work out how many bytes there were. */
10573 if (attachment_tmp != NULL)
10574 {
10575 *attachment = attachment_tmp;
10576 *attachment_len = bytes_read - (*attachment - rs->buf);
10577 }
10578
10579 return ret;
10580}
10581
80152258
PA
10582/* Invalidate the readahead cache. */
10583
10584static void
10585readahead_cache_invalidate (void)
10586{
10587 struct remote_state *rs = get_remote_state ();
10588
10589 rs->readahead_cache.fd = -1;
10590}
10591
10592/* Invalidate the readahead cache if it is holding data for FD. */
10593
10594static void
10595readahead_cache_invalidate_fd (int fd)
10596{
10597 struct remote_state *rs = get_remote_state ();
10598
10599 if (rs->readahead_cache.fd == fd)
10600 rs->readahead_cache.fd = -1;
10601}
10602
15a201c8
GB
10603/* Set the filesystem remote_hostio functions that take FILENAME
10604 arguments will use. Return 0 on success, or -1 if an error
10605 occurs (and set *REMOTE_ERRNO). */
10606
10607static int
10608remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10609{
10610 struct remote_state *rs = get_remote_state ();
10611 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10612 char *p = rs->buf;
10613 int left = get_remote_packet_size () - 1;
10614 char arg[9];
10615 int ret;
10616
10617 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10618 return 0;
10619
10620 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10621 return 0;
10622
10623 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10624
10625 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10626 remote_buffer_add_string (&p, &left, arg);
10627
10628 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10629 remote_errno, NULL, NULL);
10630
10631 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10632 return 0;
10633
10634 if (ret == 0)
10635 rs->fs_pid = required_pid;
10636
10637 return ret;
10638}
10639
12e2a5fd 10640/* Implementation of to_fileio_open. */
a6b151f1
DJ
10641
10642static int
cd897586 10643remote_hostio_open (struct target_ops *self,
07c138c8 10644 struct inferior *inf, const char *filename,
4313b8c0
GB
10645 int flags, int mode, int warn_if_slow,
10646 int *remote_errno)
a6b151f1
DJ
10647{
10648 struct remote_state *rs = get_remote_state ();
10649 char *p = rs->buf;
10650 int left = get_remote_packet_size () - 1;
10651
4313b8c0
GB
10652 if (warn_if_slow)
10653 {
10654 static int warning_issued = 0;
10655
10656 printf_unfiltered (_("Reading %s from remote target...\n"),
10657 filename);
10658
10659 if (!warning_issued)
10660 {
10661 warning (_("File transfers from remote targets can be slow."
10662 " Use \"set sysroot\" to access files locally"
10663 " instead."));
10664 warning_issued = 1;
10665 }
10666 }
10667
15a201c8
GB
10668 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10669 return -1;
10670
a6b151f1
DJ
10671 remote_buffer_add_string (&p, &left, "vFile:open:");
10672
10673 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10674 strlen (filename));
10675 remote_buffer_add_string (&p, &left, ",");
10676
10677 remote_buffer_add_int (&p, &left, flags);
10678 remote_buffer_add_string (&p, &left, ",");
10679
10680 remote_buffer_add_int (&p, &left, mode);
10681
10682 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10683 remote_errno, NULL, NULL);
10684}
10685
12e2a5fd 10686/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10687
10688static int
0d866f62
TT
10689remote_hostio_pwrite (struct target_ops *self,
10690 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10691 ULONGEST offset, int *remote_errno)
10692{
10693 struct remote_state *rs = get_remote_state ();
10694 char *p = rs->buf;
10695 int left = get_remote_packet_size ();
10696 int out_len;
10697
80152258
PA
10698 readahead_cache_invalidate_fd (fd);
10699
a6b151f1
DJ
10700 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10701
10702 remote_buffer_add_int (&p, &left, fd);
10703 remote_buffer_add_string (&p, &left, ",");
10704
10705 remote_buffer_add_int (&p, &left, offset);
10706 remote_buffer_add_string (&p, &left, ",");
10707
124e13d9 10708 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10709 get_remote_packet_size () - (p - rs->buf));
10710
10711 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10712 remote_errno, NULL, NULL);
10713}
10714
80152258
PA
10715/* Helper for the implementation of to_fileio_pread. Read the file
10716 from the remote side with vFile:pread. */
a6b151f1
DJ
10717
10718static int
80152258
PA
10719remote_hostio_pread_vFile (struct target_ops *self,
10720 int fd, gdb_byte *read_buf, int len,
10721 ULONGEST offset, int *remote_errno)
a6b151f1
DJ
10722{
10723 struct remote_state *rs = get_remote_state ();
10724 char *p = rs->buf;
10725 char *attachment;
10726 int left = get_remote_packet_size ();
10727 int ret, attachment_len;
10728 int read_len;
10729
10730 remote_buffer_add_string (&p, &left, "vFile:pread:");
10731
10732 remote_buffer_add_int (&p, &left, fd);
10733 remote_buffer_add_string (&p, &left, ",");
10734
10735 remote_buffer_add_int (&p, &left, len);
10736 remote_buffer_add_string (&p, &left, ",");
10737
10738 remote_buffer_add_int (&p, &left, offset);
10739
10740 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10741 remote_errno, &attachment,
10742 &attachment_len);
10743
10744 if (ret < 0)
10745 return ret;
10746
bc20a4af 10747 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10748 read_buf, len);
10749 if (read_len != ret)
10750 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10751
10752 return ret;
10753}
10754
80152258
PA
10755/* Serve pread from the readahead cache. Returns number of bytes
10756 read, or 0 if the request can't be served from the cache. */
10757
10758static int
10759remote_hostio_pread_from_cache (struct remote_state *rs,
10760 int fd, gdb_byte *read_buf, size_t len,
10761 ULONGEST offset)
10762{
10763 struct readahead_cache *cache = &rs->readahead_cache;
10764
10765 if (cache->fd == fd
10766 && cache->offset <= offset
10767 && offset < cache->offset + cache->bufsize)
10768 {
10769 ULONGEST max = cache->offset + cache->bufsize;
10770
10771 if (offset + len > max)
10772 len = max - offset;
10773
10774 memcpy (read_buf, cache->buf + offset - cache->offset, len);
10775 return len;
10776 }
10777
10778 return 0;
10779}
10780
10781/* Implementation of to_fileio_pread. */
10782
10783static int
10784remote_hostio_pread (struct target_ops *self,
10785 int fd, gdb_byte *read_buf, int len,
10786 ULONGEST offset, int *remote_errno)
10787{
10788 int ret;
10789 struct remote_state *rs = get_remote_state ();
10790 struct readahead_cache *cache = &rs->readahead_cache;
10791
10792 ret = remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
10793 if (ret > 0)
10794 {
10795 cache->hit_count++;
10796
10797 if (remote_debug)
10798 fprintf_unfiltered (gdb_stdlog, "readahead cache hit %s\n",
10799 pulongest (cache->hit_count));
10800 return ret;
10801 }
10802
10803 cache->miss_count++;
10804 if (remote_debug)
10805 fprintf_unfiltered (gdb_stdlog, "readahead cache miss %s\n",
10806 pulongest (cache->miss_count));
10807
10808 cache->fd = fd;
10809 cache->offset = offset;
10810 cache->bufsize = get_remote_packet_size ();
10811 cache->buf = xrealloc (cache->buf, cache->bufsize);
10812
10813 ret = remote_hostio_pread_vFile (self, cache->fd, cache->buf, cache->bufsize,
10814 cache->offset, remote_errno);
10815 if (ret <= 0)
10816 {
10817 readahead_cache_invalidate_fd (fd);
10818 return ret;
10819 }
10820
10821 cache->bufsize = ret;
10822 return remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
10823}
10824
12e2a5fd 10825/* Implementation of to_fileio_close. */
a6b151f1
DJ
10826
10827static int
df39ea25 10828remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10829{
10830 struct remote_state *rs = get_remote_state ();
10831 char *p = rs->buf;
10832 int left = get_remote_packet_size () - 1;
10833
80152258
PA
10834 readahead_cache_invalidate_fd (fd);
10835
a6b151f1
DJ
10836 remote_buffer_add_string (&p, &left, "vFile:close:");
10837
10838 remote_buffer_add_int (&p, &left, fd);
10839
10840 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10841 remote_errno, NULL, NULL);
10842}
10843
12e2a5fd 10844/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10845
10846static int
dbbca37d 10847remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
10848 struct inferior *inf, const char *filename,
10849 int *remote_errno)
a6b151f1
DJ
10850{
10851 struct remote_state *rs = get_remote_state ();
10852 char *p = rs->buf;
10853 int left = get_remote_packet_size () - 1;
10854
15a201c8
GB
10855 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10856 return -1;
10857
a6b151f1
DJ
10858 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10859
10860 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10861 strlen (filename));
10862
10863 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10864 remote_errno, NULL, NULL);
10865}
10866
12e2a5fd 10867/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10868
10869static char *
fab5aa7c 10870remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
10871 struct inferior *inf, const char *filename,
10872 int *remote_errno)
b9e7b9c3
UW
10873{
10874 struct remote_state *rs = get_remote_state ();
10875 char *p = rs->buf;
10876 char *attachment;
10877 int left = get_remote_packet_size ();
10878 int len, attachment_len;
10879 int read_len;
10880 char *ret;
10881
15a201c8
GB
10882 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10883 return NULL;
10884
b9e7b9c3
UW
10885 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10886
10887 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10888 strlen (filename));
10889
10890 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10891 remote_errno, &attachment,
10892 &attachment_len);
10893
10894 if (len < 0)
10895 return NULL;
10896
10897 ret = xmalloc (len + 1);
10898
bc20a4af
PA
10899 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10900 (gdb_byte *) ret, len);
b9e7b9c3
UW
10901 if (read_len != len)
10902 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10903
10904 ret[len] = '\0';
10905 return ret;
10906}
10907
12e2a5fd 10908/* Implementation of to_fileio_fstat. */
0a93529c
GB
10909
10910static int
10911remote_hostio_fstat (struct target_ops *self,
10912 int fd, struct stat *st,
10913 int *remote_errno)
10914{
10915 struct remote_state *rs = get_remote_state ();
10916 char *p = rs->buf;
10917 int left = get_remote_packet_size ();
10918 int attachment_len, ret;
10919 char *attachment;
10920 struct fio_stat fst;
10921 int read_len;
10922
464b0089
GB
10923 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10924
10925 remote_buffer_add_int (&p, &left, fd);
10926
10927 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10928 remote_errno, &attachment,
10929 &attachment_len);
10930 if (ret < 0)
0a93529c 10931 {
464b0089
GB
10932 if (*remote_errno != FILEIO_ENOSYS)
10933 return ret;
10934
0a93529c
GB
10935 /* Strictly we should return -1, ENOSYS here, but when
10936 "set sysroot remote:" was implemented in August 2008
10937 BFD's need for a stat function was sidestepped with
10938 this hack. This was not remedied until March 2015
10939 so we retain the previous behavior to avoid breaking
10940 compatibility.
10941
10942 Note that the memset is a March 2015 addition; older
10943 GDBs set st_size *and nothing else* so the structure
10944 would have garbage in all other fields. This might
10945 break something but retaining the previous behavior
10946 here would be just too wrong. */
10947
10948 memset (st, 0, sizeof (struct stat));
10949 st->st_size = INT_MAX;
10950 return 0;
10951 }
10952
0a93529c
GB
10953 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10954 (gdb_byte *) &fst, sizeof (fst));
10955
10956 if (read_len != ret)
10957 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10958
10959 if (read_len != sizeof (fst))
10960 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10961 read_len, (int) sizeof (fst));
10962
10963 remote_fileio_to_host_stat (&fst, st);
10964
10965 return 0;
10966}
10967
12e2a5fd 10968/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10969
10970static int
10971remote_filesystem_is_local (struct target_ops *self)
10972{
10973 /* Valgrind GDB presents itself as a remote target but works
10974 on the local filesystem: it does not implement remote get
10975 and users are not expected to set a sysroot. To handle
10976 this case we treat the remote filesystem as local if the
10977 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10978 does not support vFile:open. */
a3be80c3 10979 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
10980 {
10981 enum packet_support ps = packet_support (PACKET_vFile_open);
10982
10983 if (ps == PACKET_SUPPORT_UNKNOWN)
10984 {
10985 int fd, remote_errno;
10986
10987 /* Try opening a file to probe support. The supplied
10988 filename is irrelevant, we only care about whether
10989 the stub recognizes the packet or not. */
07c138c8 10990 fd = remote_hostio_open (self, NULL, "just probing",
4313b8c0 10991 FILEIO_O_RDONLY, 0700, 0,
e3dd7556
GB
10992 &remote_errno);
10993
10994 if (fd >= 0)
10995 remote_hostio_close (self, fd, &remote_errno);
10996
10997 ps = packet_support (PACKET_vFile_open);
10998 }
10999
11000 if (ps == PACKET_DISABLE)
11001 {
11002 static int warning_issued = 0;
11003
11004 if (!warning_issued)
11005 {
11006 warning (_("remote target does not support file"
11007 " transfer, attempting to access files"
11008 " from local filesystem."));
11009 warning_issued = 1;
11010 }
11011
11012 return 1;
11013 }
11014 }
11015
11016 return 0;
11017}
11018
a6b151f1
DJ
11019static int
11020remote_fileio_errno_to_host (int errnum)
11021{
11022 switch (errnum)
11023 {
11024 case FILEIO_EPERM:
11025 return EPERM;
11026 case FILEIO_ENOENT:
11027 return ENOENT;
11028 case FILEIO_EINTR:
11029 return EINTR;
11030 case FILEIO_EIO:
11031 return EIO;
11032 case FILEIO_EBADF:
11033 return EBADF;
11034 case FILEIO_EACCES:
11035 return EACCES;
11036 case FILEIO_EFAULT:
11037 return EFAULT;
11038 case FILEIO_EBUSY:
11039 return EBUSY;
11040 case FILEIO_EEXIST:
11041 return EEXIST;
11042 case FILEIO_ENODEV:
11043 return ENODEV;
11044 case FILEIO_ENOTDIR:
11045 return ENOTDIR;
11046 case FILEIO_EISDIR:
11047 return EISDIR;
11048 case FILEIO_EINVAL:
11049 return EINVAL;
11050 case FILEIO_ENFILE:
11051 return ENFILE;
11052 case FILEIO_EMFILE:
11053 return EMFILE;
11054 case FILEIO_EFBIG:
11055 return EFBIG;
11056 case FILEIO_ENOSPC:
11057 return ENOSPC;
11058 case FILEIO_ESPIPE:
11059 return ESPIPE;
11060 case FILEIO_EROFS:
11061 return EROFS;
11062 case FILEIO_ENOSYS:
11063 return ENOSYS;
11064 case FILEIO_ENAMETOOLONG:
11065 return ENAMETOOLONG;
11066 }
11067 return -1;
11068}
11069
11070static char *
11071remote_hostio_error (int errnum)
11072{
11073 int host_error = remote_fileio_errno_to_host (errnum);
11074
11075 if (host_error == -1)
11076 error (_("Unknown remote I/O error %d"), errnum);
11077 else
11078 error (_("Remote I/O error: %s"), safe_strerror (host_error));
11079}
11080
a6b151f1
DJ
11081static void
11082remote_hostio_close_cleanup (void *opaque)
11083{
11084 int fd = *(int *) opaque;
11085 int remote_errno;
11086
df39ea25 11087 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
11088}
11089
11090void
11091remote_file_put (const char *local_file, const char *remote_file, int from_tty)
11092{
11093 struct cleanup *back_to, *close_cleanup;
11094 int retcode, fd, remote_errno, bytes, io_size;
11095 FILE *file;
11096 gdb_byte *buffer;
11097 int bytes_in_buffer;
11098 int saw_eof;
11099 ULONGEST offset;
5d93a237 11100 struct remote_state *rs = get_remote_state ();
a6b151f1 11101
5d93a237 11102 if (!rs->remote_desc)
a6b151f1
DJ
11103 error (_("command can only be used with remote target"));
11104
614c279d 11105 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
11106 if (file == NULL)
11107 perror_with_name (local_file);
7c8a8b04 11108 back_to = make_cleanup_fclose (file);
a6b151f1 11109
07c138c8 11110 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 11111 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1 11112 | FILEIO_O_TRUNC),
4313b8c0 11113 0700, 0, &remote_errno);
a6b151f1
DJ
11114 if (fd == -1)
11115 remote_hostio_error (remote_errno);
11116
11117 /* Send up to this many bytes at once. They won't all fit in the
11118 remote packet limit, so we'll transfer slightly fewer. */
11119 io_size = get_remote_packet_size ();
11120 buffer = xmalloc (io_size);
11121 make_cleanup (xfree, buffer);
11122
11123 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
11124
11125 bytes_in_buffer = 0;
11126 saw_eof = 0;
11127 offset = 0;
11128 while (bytes_in_buffer || !saw_eof)
11129 {
11130 if (!saw_eof)
11131 {
3e43a32a
MS
11132 bytes = fread (buffer + bytes_in_buffer, 1,
11133 io_size - bytes_in_buffer,
a6b151f1
DJ
11134 file);
11135 if (bytes == 0)
11136 {
11137 if (ferror (file))
11138 error (_("Error reading %s."), local_file);
11139 else
11140 {
11141 /* EOF. Unless there is something still in the
11142 buffer from the last iteration, we are done. */
11143 saw_eof = 1;
11144 if (bytes_in_buffer == 0)
11145 break;
11146 }
11147 }
11148 }
11149 else
11150 bytes = 0;
11151
11152 bytes += bytes_in_buffer;
11153 bytes_in_buffer = 0;
11154
0d866f62
TT
11155 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
11156 fd, buffer, bytes,
3e43a32a 11157 offset, &remote_errno);
a6b151f1
DJ
11158
11159 if (retcode < 0)
11160 remote_hostio_error (remote_errno);
11161 else if (retcode == 0)
11162 error (_("Remote write of %d bytes returned 0!"), bytes);
11163 else if (retcode < bytes)
11164 {
11165 /* Short write. Save the rest of the read data for the next
11166 write. */
11167 bytes_in_buffer = bytes - retcode;
11168 memmove (buffer, buffer + retcode, bytes_in_buffer);
11169 }
11170
11171 offset += retcode;
11172 }
11173
11174 discard_cleanups (close_cleanup);
df39ea25 11175 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
11176 remote_hostio_error (remote_errno);
11177
11178 if (from_tty)
11179 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
11180 do_cleanups (back_to);
11181}
11182
11183void
11184remote_file_get (const char *remote_file, const char *local_file, int from_tty)
11185{
11186 struct cleanup *back_to, *close_cleanup;
cea39f65 11187 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
11188 FILE *file;
11189 gdb_byte *buffer;
11190 ULONGEST offset;
5d93a237 11191 struct remote_state *rs = get_remote_state ();
a6b151f1 11192
5d93a237 11193 if (!rs->remote_desc)
a6b151f1
DJ
11194 error (_("command can only be used with remote target"));
11195
07c138c8 11196 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
4313b8c0
GB
11197 remote_file, FILEIO_O_RDONLY, 0, 0,
11198 &remote_errno);
a6b151f1
DJ
11199 if (fd == -1)
11200 remote_hostio_error (remote_errno);
11201
614c279d 11202 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
11203 if (file == NULL)
11204 perror_with_name (local_file);
7c8a8b04 11205 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
11206
11207 /* Send up to this many bytes at once. They won't all fit in the
11208 remote packet limit, so we'll transfer slightly fewer. */
11209 io_size = get_remote_packet_size ();
11210 buffer = xmalloc (io_size);
11211 make_cleanup (xfree, buffer);
11212
11213 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
11214
11215 offset = 0;
11216 while (1)
11217 {
a3be983c
TT
11218 bytes = remote_hostio_pread (find_target_at (process_stratum),
11219 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
11220 if (bytes == 0)
11221 /* Success, but no bytes, means end-of-file. */
11222 break;
11223 if (bytes == -1)
11224 remote_hostio_error (remote_errno);
11225
11226 offset += bytes;
11227
11228 bytes = fwrite (buffer, 1, bytes, file);
11229 if (bytes == 0)
11230 perror_with_name (local_file);
11231 }
11232
11233 discard_cleanups (close_cleanup);
df39ea25 11234 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
11235 remote_hostio_error (remote_errno);
11236
11237 if (from_tty)
11238 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
11239 do_cleanups (back_to);
11240}
11241
11242void
11243remote_file_delete (const char *remote_file, int from_tty)
11244{
11245 int retcode, remote_errno;
5d93a237 11246 struct remote_state *rs = get_remote_state ();
a6b151f1 11247
5d93a237 11248 if (!rs->remote_desc)
a6b151f1
DJ
11249 error (_("command can only be used with remote target"));
11250
dbbca37d 11251 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 11252 NULL, remote_file, &remote_errno);
a6b151f1
DJ
11253 if (retcode == -1)
11254 remote_hostio_error (remote_errno);
11255
11256 if (from_tty)
11257 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
11258}
11259
11260static void
11261remote_put_command (char *args, int from_tty)
11262{
11263 struct cleanup *back_to;
11264 char **argv;
11265
d1a41061
PP
11266 if (args == NULL)
11267 error_no_arg (_("file to put"));
11268
11269 argv = gdb_buildargv (args);
a6b151f1
DJ
11270 back_to = make_cleanup_freeargv (argv);
11271 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
11272 error (_("Invalid parameters to remote put"));
11273
11274 remote_file_put (argv[0], argv[1], from_tty);
11275
11276 do_cleanups (back_to);
11277}
11278
11279static void
11280remote_get_command (char *args, int from_tty)
11281{
11282 struct cleanup *back_to;
11283 char **argv;
11284
d1a41061
PP
11285 if (args == NULL)
11286 error_no_arg (_("file to get"));
11287
11288 argv = gdb_buildargv (args);
a6b151f1
DJ
11289 back_to = make_cleanup_freeargv (argv);
11290 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
11291 error (_("Invalid parameters to remote get"));
11292
11293 remote_file_get (argv[0], argv[1], from_tty);
11294
11295 do_cleanups (back_to);
11296}
11297
11298static void
11299remote_delete_command (char *args, int from_tty)
11300{
11301 struct cleanup *back_to;
11302 char **argv;
11303
d1a41061
PP
11304 if (args == NULL)
11305 error_no_arg (_("file to delete"));
11306
11307 argv = gdb_buildargv (args);
a6b151f1
DJ
11308 back_to = make_cleanup_freeargv (argv);
11309 if (argv[0] == NULL || argv[1] != NULL)
11310 error (_("Invalid parameters to remote delete"));
11311
11312 remote_file_delete (argv[0], from_tty);
11313
11314 do_cleanups (back_to);
11315}
11316
11317static void
11318remote_command (char *args, int from_tty)
11319{
635c7e8a 11320 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
11321}
11322
b2175913 11323static int
19db3e69 11324remote_can_execute_reverse (struct target_ops *self)
b2175913 11325{
4082afcc
PA
11326 if (packet_support (PACKET_bs) == PACKET_ENABLE
11327 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
11328 return 1;
11329 else
11330 return 0;
b2175913
MS
11331}
11332
74531fed 11333static int
2a9a2795 11334remote_supports_non_stop (struct target_ops *self)
74531fed
PA
11335{
11336 return 1;
11337}
11338
03583c20 11339static int
2bfc0540 11340remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
11341{
11342 /* Only supported in extended mode. */
11343 return 0;
11344}
11345
8a305172 11346static int
86ce2668 11347remote_supports_multi_process (struct target_ops *self)
8a305172
PA
11348{
11349 struct remote_state *rs = get_remote_state ();
a744cf53 11350
901f9912
UW
11351 /* Only extended-remote handles being attached to multiple
11352 processes, even though plain remote can use the multi-process
11353 thread id extensions, so that GDB knows the target process's
11354 PID. */
11355 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
11356}
11357
70221824 11358static int
782b2b07
SS
11359remote_supports_cond_tracepoints (void)
11360{
4082afcc 11361 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
11362}
11363
3788aec7 11364static int
efcc2da7 11365remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 11366{
4082afcc 11367 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
11368}
11369
70221824 11370static int
7a697b8d
SS
11371remote_supports_fast_tracepoints (void)
11372{
4082afcc 11373 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
11374}
11375
0fb4aa4b
PA
11376static int
11377remote_supports_static_tracepoints (void)
11378{
4082afcc 11379 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
11380}
11381
1e4d1764
YQ
11382static int
11383remote_supports_install_in_trace (void)
11384{
4082afcc 11385 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
11386}
11387
d248b706 11388static int
7d178d6a 11389remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 11390{
4082afcc
PA
11391 return (packet_support (PACKET_EnableDisableTracepoints_feature)
11392 == PACKET_ENABLE);
d248b706
KY
11393}
11394
3065dfb6 11395static int
6de37a3a 11396remote_supports_string_tracing (struct target_ops *self)
3065dfb6 11397{
4082afcc 11398 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
11399}
11400
d3ce09f5 11401static int
78eff0ec 11402remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 11403{
4082afcc 11404 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
11405}
11406
35b1e5cc 11407static void
ecae04e1 11408remote_trace_init (struct target_ops *self)
35b1e5cc
SS
11409{
11410 putpkt ("QTinit");
11411 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 11412 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11413 error (_("Target does not support this command."));
11414}
11415
11416static void free_actions_list (char **actions_list);
11417static void free_actions_list_cleanup_wrapper (void *);
11418static void
11419free_actions_list_cleanup_wrapper (void *al)
11420{
11421 free_actions_list (al);
11422}
11423
11424static void
11425free_actions_list (char **actions_list)
11426{
11427 int ndx;
11428
11429 if (actions_list == 0)
11430 return;
11431
11432 for (ndx = 0; actions_list[ndx]; ndx++)
11433 xfree (actions_list[ndx]);
11434
11435 xfree (actions_list);
11436}
11437
409873ef
SS
11438/* Recursive routine to walk through command list including loops, and
11439 download packets for each command. */
11440
11441static void
11442remote_download_command_source (int num, ULONGEST addr,
11443 struct command_line *cmds)
11444{
11445 struct remote_state *rs = get_remote_state ();
11446 struct command_line *cmd;
11447
11448 for (cmd = cmds; cmd; cmd = cmd->next)
11449 {
0df8b418 11450 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11451 strcpy (rs->buf, "QTDPsrc:");
11452 encode_source_string (num, addr, "cmd", cmd->line,
11453 rs->buf + strlen (rs->buf),
11454 rs->buf_size - strlen (rs->buf));
11455 putpkt (rs->buf);
11456 remote_get_noisy_reply (&target_buf, &target_buf_size);
11457 if (strcmp (target_buf, "OK"))
11458 warning (_("Target does not support source download."));
11459
11460 if (cmd->control_type == while_control
11461 || cmd->control_type == while_stepping_control)
11462 {
11463 remote_download_command_source (num, addr, *cmd->body_list);
11464
0df8b418 11465 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11466 strcpy (rs->buf, "QTDPsrc:");
11467 encode_source_string (num, addr, "cmd", "end",
11468 rs->buf + strlen (rs->buf),
11469 rs->buf_size - strlen (rs->buf));
11470 putpkt (rs->buf);
11471 remote_get_noisy_reply (&target_buf, &target_buf_size);
11472 if (strcmp (target_buf, "OK"))
11473 warning (_("Target does not support source download."));
11474 }
11475 }
11476}
11477
35b1e5cc 11478static void
548f7808 11479remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11480{
bba74b36 11481#define BUF_SIZE 2048
e8ba3115 11482
35b1e5cc 11483 CORE_ADDR tpaddr;
409873ef 11484 char addrbuf[40];
bba74b36 11485 char buf[BUF_SIZE];
35b1e5cc
SS
11486 char **tdp_actions;
11487 char **stepping_actions;
11488 int ndx;
11489 struct cleanup *old_chain = NULL;
11490 struct agent_expr *aexpr;
11491 struct cleanup *aexpr_chain = NULL;
11492 char *pkt;
e8ba3115 11493 struct breakpoint *b = loc->owner;
d9b3f62e 11494 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11495
dc673c81 11496 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11497 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11498 tdp_actions);
11499 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11500 stepping_actions);
11501
11502 tpaddr = loc->address;
11503 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11504 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11505 addrbuf, /* address */
11506 (b->enable_state == bp_enabled ? 'E' : 'D'),
11507 t->step_count, t->pass_count);
e8ba3115
YQ
11508 /* Fast tracepoints are mostly handled by the target, but we can
11509 tell the target how big of an instruction block should be moved
11510 around. */
11511 if (b->type == bp_fast_tracepoint)
11512 {
11513 /* Only test for support at download time; we may not know
11514 target capabilities at definition time. */
11515 if (remote_supports_fast_tracepoints ())
35b1e5cc 11516 {
6b940e6a
PL
11517 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
11518 NULL))
bba74b36 11519 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 11520 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 11521 else
e8ba3115
YQ
11522 /* If it passed validation at definition but fails now,
11523 something is very wrong. */
11524 internal_error (__FILE__, __LINE__,
11525 _("Fast tracepoint not "
11526 "valid during download"));
35b1e5cc 11527 }
e8ba3115
YQ
11528 else
11529 /* Fast tracepoints are functionally identical to regular
11530 tracepoints, so don't take lack of support as a reason to
11531 give up on the trace run. */
11532 warning (_("Target does not support fast tracepoints, "
11533 "downloading %d as regular tracepoint"), b->number);
11534 }
11535 else if (b->type == bp_static_tracepoint)
11536 {
11537 /* Only test for support at download time; we may not know
11538 target capabilities at definition time. */
11539 if (remote_supports_static_tracepoints ())
0fb4aa4b 11540 {
e8ba3115 11541 struct static_tracepoint_marker marker;
0fb4aa4b 11542
e8ba3115
YQ
11543 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11544 strcat (buf, ":S");
0fb4aa4b 11545 else
e8ba3115 11546 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11547 }
e8ba3115
YQ
11548 else
11549 /* Fast tracepoints are functionally identical to regular
11550 tracepoints, so don't take lack of support as a reason
11551 to give up on the trace run. */
11552 error (_("Target does not support static tracepoints"));
11553 }
11554 /* If the tracepoint has a conditional, make it into an agent
11555 expression and append to the definition. */
11556 if (loc->cond)
11557 {
11558 /* Only test support at download time, we may not know target
11559 capabilities at definition time. */
11560 if (remote_supports_cond_tracepoints ())
35b1e5cc 11561 {
e8ba3115
YQ
11562 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11563 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11564 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11565 aexpr->len);
e8ba3115
YQ
11566 pkt = buf + strlen (buf);
11567 for (ndx = 0; ndx < aexpr->len; ++ndx)
11568 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11569 *pkt = '\0';
11570 do_cleanups (aexpr_chain);
35b1e5cc 11571 }
e8ba3115
YQ
11572 else
11573 warning (_("Target does not support conditional tracepoints, "
11574 "ignoring tp %d cond"), b->number);
11575 }
35b1e5cc 11576
d9b3f62e 11577 if (b->commands || *default_collect)
e8ba3115
YQ
11578 strcat (buf, "-");
11579 putpkt (buf);
11580 remote_get_noisy_reply (&target_buf, &target_buf_size);
11581 if (strcmp (target_buf, "OK"))
11582 error (_("Target does not support tracepoints."));
35b1e5cc 11583
e8ba3115
YQ
11584 /* do_single_steps (t); */
11585 if (tdp_actions)
11586 {
11587 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11588 {
e8ba3115 11589 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11590 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11591 b->number, addrbuf, /* address */
11592 tdp_actions[ndx],
11593 ((tdp_actions[ndx + 1] || stepping_actions)
11594 ? '-' : 0));
e8ba3115
YQ
11595 putpkt (buf);
11596 remote_get_noisy_reply (&target_buf,
11597 &target_buf_size);
11598 if (strcmp (target_buf, "OK"))
11599 error (_("Error on target while setting tracepoints."));
35b1e5cc 11600 }
e8ba3115
YQ
11601 }
11602 if (stepping_actions)
11603 {
11604 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11605 {
e8ba3115 11606 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11607 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11608 b->number, addrbuf, /* address */
11609 ((ndx == 0) ? "S" : ""),
11610 stepping_actions[ndx],
11611 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11612 putpkt (buf);
11613 remote_get_noisy_reply (&target_buf,
11614 &target_buf_size);
11615 if (strcmp (target_buf, "OK"))
11616 error (_("Error on target while setting tracepoints."));
35b1e5cc 11617 }
e8ba3115 11618 }
409873ef 11619
4082afcc 11620 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 11621 {
f00aae0f 11622 if (b->location != NULL)
409873ef 11623 {
e8ba3115 11624 strcpy (buf, "QTDPsrc:");
f00aae0f
KS
11625 encode_source_string (b->number, loc->address, "at",
11626 event_location_to_string (b->location),
11627 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115
YQ
11628 putpkt (buf);
11629 remote_get_noisy_reply (&target_buf, &target_buf_size);
11630 if (strcmp (target_buf, "OK"))
11631 warning (_("Target does not support source download."));
409873ef 11632 }
e8ba3115
YQ
11633 if (b->cond_string)
11634 {
11635 strcpy (buf, "QTDPsrc:");
11636 encode_source_string (b->number, loc->address,
11637 "cond", b->cond_string, buf + strlen (buf),
11638 2048 - strlen (buf));
11639 putpkt (buf);
11640 remote_get_noisy_reply (&target_buf, &target_buf_size);
11641 if (strcmp (target_buf, "OK"))
11642 warning (_("Target does not support source download."));
11643 }
11644 remote_download_command_source (b->number, loc->address,
11645 breakpoint_commands (b));
35b1e5cc 11646 }
e8ba3115
YQ
11647
11648 do_cleanups (old_chain);
35b1e5cc
SS
11649}
11650
1e4d1764 11651static int
a52a8357 11652remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11653{
1e51243a
PA
11654 struct remote_state *rs = get_remote_state ();
11655 struct trace_status *ts;
11656 int status;
11657
11658 /* Don't try to install tracepoints until we've relocated our
11659 symbols, and fetched and merged the target's tracepoint list with
11660 ours. */
11661 if (rs->starting_up)
11662 return 0;
11663
11664 ts = current_trace_status ();
8bd200f1 11665 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11666
11667 if (status == -1 || !ts->running_known || !ts->running)
11668 return 0;
11669
11670 /* If we are in a tracing experiment, but remote stub doesn't support
11671 installing tracepoint in trace, we have to return. */
11672 if (!remote_supports_install_in_trace ())
11673 return 0;
11674
11675 return 1;
11676}
11677
11678
35b1e5cc 11679static void
559d2b81
TT
11680remote_download_trace_state_variable (struct target_ops *self,
11681 struct trace_state_variable *tsv)
35b1e5cc
SS
11682{
11683 struct remote_state *rs = get_remote_state ();
00bf0b85 11684 char *p;
35b1e5cc 11685
bba74b36
YQ
11686 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11687 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11688 tsv->builtin);
00bf0b85
SS
11689 p = rs->buf + strlen (rs->buf);
11690 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11691 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11692 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11693 *p++ = '\0';
35b1e5cc
SS
11694 putpkt (rs->buf);
11695 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11696 if (*target_buf == '\0')
11697 error (_("Target does not support this command."));
11698 if (strcmp (target_buf, "OK") != 0)
11699 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11700}
11701
d248b706 11702static void
46670d57
TT
11703remote_enable_tracepoint (struct target_ops *self,
11704 struct bp_location *location)
d248b706
KY
11705{
11706 struct remote_state *rs = get_remote_state ();
11707 char addr_buf[40];
11708
11709 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11710 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11711 location->owner->number, addr_buf);
d248b706
KY
11712 putpkt (rs->buf);
11713 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11714 if (*rs->buf == '\0')
11715 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11716 if (strcmp (rs->buf, "OK") != 0)
11717 error (_("Error on target while enabling tracepoint."));
11718}
11719
11720static void
780b049c
TT
11721remote_disable_tracepoint (struct target_ops *self,
11722 struct bp_location *location)
d248b706
KY
11723{
11724 struct remote_state *rs = get_remote_state ();
11725 char addr_buf[40];
11726
11727 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11728 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11729 location->owner->number, addr_buf);
d248b706
KY
11730 putpkt (rs->buf);
11731 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11732 if (*rs->buf == '\0')
11733 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11734 if (strcmp (rs->buf, "OK") != 0)
11735 error (_("Error on target while disabling tracepoint."));
11736}
11737
35b1e5cc 11738static void
583f9a86 11739remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11740{
11741 asection *s;
81b9b86e 11742 bfd *abfd = NULL;
35b1e5cc 11743 bfd_size_type size;
608bcef2 11744 bfd_vma vma;
35b1e5cc 11745 int anysecs = 0;
c2fa21f1 11746 int offset = 0;
35b1e5cc
SS
11747
11748 if (!exec_bfd)
11749 return; /* No information to give. */
11750
11751 strcpy (target_buf, "QTro");
9779ab84 11752 offset = strlen (target_buf);
35b1e5cc
SS
11753 for (s = exec_bfd->sections; s; s = s->next)
11754 {
11755 char tmp1[40], tmp2[40];
c2fa21f1 11756 int sec_length;
35b1e5cc
SS
11757
11758 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11759 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11760 (s->flags & SEC_READONLY) == 0)
11761 continue;
11762
11763 anysecs = 1;
81b9b86e 11764 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11765 size = bfd_get_section_size (s);
608bcef2
HZ
11766 sprintf_vma (tmp1, vma);
11767 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11768 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11769 if (offset + sec_length + 1 > target_buf_size)
11770 {
4082afcc 11771 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11772 warning (_("\
c2fa21f1
HZ
11773Too many sections for read-only sections definition packet."));
11774 break;
11775 }
bba74b36
YQ
11776 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11777 tmp1, tmp2);
c2fa21f1 11778 offset += sec_length;
35b1e5cc
SS
11779 }
11780 if (anysecs)
11781 {
11782 putpkt (target_buf);
11783 getpkt (&target_buf, &target_buf_size, 0);
11784 }
11785}
11786
11787static void
e2d1aae3 11788remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11789{
11790 putpkt ("QTStart");
11791 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11792 if (*target_buf == '\0')
11793 error (_("Target does not support this command."));
11794 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11795 error (_("Bogus reply from target: %s"), target_buf);
11796}
11797
11798static int
8bd200f1 11799remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11800{
953b98d1 11801 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11802 char *p = NULL;
0df8b418 11803 /* FIXME we need to get register block size some other way. */
00bf0b85 11804 extern int trace_regblock_size;
bd3eecc3
PA
11805 enum packet_result result;
11806
4082afcc 11807 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11808 return -1;
a744cf53 11809
00bf0b85
SS
11810 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11811
049dc89b
JK
11812 putpkt ("qTStatus");
11813
492d29ea 11814 TRY
67f41397
JK
11815 {
11816 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11817 }
492d29ea 11818 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11819 {
598d3636
JK
11820 if (ex.error != TARGET_CLOSE_ERROR)
11821 {
11822 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11823 return -1;
11824 }
11825 throw_exception (ex);
67f41397 11826 }
492d29ea 11827 END_CATCH
00bf0b85 11828
bd3eecc3
PA
11829 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11830
00bf0b85 11831 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11832 if (result == PACKET_UNKNOWN)
00bf0b85 11833 return -1;
35b1e5cc 11834
00bf0b85 11835 /* We're working with a live target. */
f5911ea1 11836 ts->filename = NULL;
00bf0b85 11837
00bf0b85 11838 if (*p++ != 'T')
35b1e5cc
SS
11839 error (_("Bogus trace status reply from target: %s"), target_buf);
11840
84cebc4a
YQ
11841 /* Function 'parse_trace_status' sets default value of each field of
11842 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11843 parse_trace_status (p, ts);
11844
11845 return ts->running;
35b1e5cc
SS
11846}
11847
70221824 11848static void
db90e85c 11849remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11850 struct uploaded_tp *utp)
11851{
11852 struct remote_state *rs = get_remote_state ();
f196051f
SS
11853 char *reply;
11854 struct bp_location *loc;
11855 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11856 size_t size = get_remote_packet_size ();
f196051f
SS
11857
11858 if (tp)
11859 {
11860 tp->base.hit_count = 0;
11861 tp->traceframe_usage = 0;
11862 for (loc = tp->base.loc; loc; loc = loc->next)
11863 {
11864 /* If the tracepoint was never downloaded, don't go asking for
11865 any status. */
11866 if (tp->number_on_target == 0)
11867 continue;
bba74b36
YQ
11868 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11869 phex_nz (loc->address, 0));
f196051f
SS
11870 putpkt (rs->buf);
11871 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11872 if (reply && *reply)
11873 {
11874 if (*reply == 'V')
11875 parse_tracepoint_status (reply + 1, bp, utp);
11876 }
11877 }
11878 }
11879 else if (utp)
11880 {
11881 utp->hit_count = 0;
11882 utp->traceframe_usage = 0;
bba74b36
YQ
11883 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11884 phex_nz (utp->addr, 0));
f196051f
SS
11885 putpkt (rs->buf);
11886 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11887 if (reply && *reply)
11888 {
11889 if (*reply == 'V')
11890 parse_tracepoint_status (reply + 1, bp, utp);
11891 }
11892 }
11893}
11894
35b1e5cc 11895static void
74499f1b 11896remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11897{
11898 putpkt ("QTStop");
11899 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11900 if (*target_buf == '\0')
11901 error (_("Target does not support this command."));
11902 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11903 error (_("Bogus reply from target: %s"), target_buf);
11904}
11905
11906static int
bd4c6793
TT
11907remote_trace_find (struct target_ops *self,
11908 enum trace_find_type type, int num,
cc5925ad 11909 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11910 int *tpp)
11911{
11912 struct remote_state *rs = get_remote_state ();
bba74b36 11913 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11914 char *p, *reply;
11915 int target_frameno = -1, target_tracept = -1;
11916
e6e4e701
PA
11917 /* Lookups other than by absolute frame number depend on the current
11918 trace selected, so make sure it is correct on the remote end
11919 first. */
11920 if (type != tfind_number)
11921 set_remote_traceframe ();
11922
35b1e5cc
SS
11923 p = rs->buf;
11924 strcpy (p, "QTFrame:");
11925 p = strchr (p, '\0');
11926 switch (type)
11927 {
11928 case tfind_number:
bba74b36 11929 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11930 break;
11931 case tfind_pc:
bba74b36 11932 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11933 break;
11934 case tfind_tp:
bba74b36 11935 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11936 break;
11937 case tfind_range:
bba74b36
YQ
11938 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11939 phex_nz (addr2, 0));
35b1e5cc
SS
11940 break;
11941 case tfind_outside:
bba74b36
YQ
11942 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11943 phex_nz (addr2, 0));
35b1e5cc
SS
11944 break;
11945 default:
9b20d036 11946 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11947 }
11948
11949 putpkt (rs->buf);
2f65bcb7 11950 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11951 if (*reply == '\0')
11952 error (_("Target does not support this command."));
35b1e5cc
SS
11953
11954 while (reply && *reply)
11955 switch (*reply)
11956 {
11957 case 'F':
f197e0f1
VP
11958 p = ++reply;
11959 target_frameno = (int) strtol (p, &reply, 16);
11960 if (reply == p)
11961 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11962 /* Don't update our remote traceframe number cache on failure
11963 to select a remote traceframe. */
f197e0f1
VP
11964 if (target_frameno == -1)
11965 return -1;
35b1e5cc
SS
11966 break;
11967 case 'T':
f197e0f1
VP
11968 p = ++reply;
11969 target_tracept = (int) strtol (p, &reply, 16);
11970 if (reply == p)
11971 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11972 break;
11973 case 'O': /* "OK"? */
11974 if (reply[1] == 'K' && reply[2] == '\0')
11975 reply += 2;
11976 else
11977 error (_("Bogus reply from target: %s"), reply);
11978 break;
11979 default:
11980 error (_("Bogus reply from target: %s"), reply);
11981 }
11982 if (tpp)
11983 *tpp = target_tracept;
e6e4e701 11984
262e1174 11985 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11986 return target_frameno;
11987}
11988
11989static int
4011015b
TT
11990remote_get_trace_state_variable_value (struct target_ops *self,
11991 int tsvnum, LONGEST *val)
35b1e5cc
SS
11992{
11993 struct remote_state *rs = get_remote_state ();
11994 char *reply;
11995 ULONGEST uval;
11996
e6e4e701
PA
11997 set_remote_traceframe ();
11998
bba74b36 11999 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
12000 putpkt (rs->buf);
12001 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12002 if (reply && *reply)
12003 {
12004 if (*reply == 'V')
12005 {
12006 unpack_varlen_hex (reply + 1, &uval);
12007 *val = (LONGEST) uval;
12008 return 1;
12009 }
12010 }
12011 return 0;
12012}
12013
00bf0b85 12014static int
dc3decaf 12015remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
12016{
12017 struct remote_state *rs = get_remote_state ();
12018 char *p, *reply;
12019
12020 p = rs->buf;
12021 strcpy (p, "QTSave:");
12022 p += strlen (p);
12023 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
12024 error (_("Remote file name too long for trace save packet"));
9f1b45b0 12025 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
12026 *p++ = '\0';
12027 putpkt (rs->buf);
ad91cd99 12028 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 12029 if (*reply == '\0')
ad91cd99
PA
12030 error (_("Target does not support this command."));
12031 if (strcmp (reply, "OK") != 0)
12032 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
12033 return 0;
12034}
12035
12036/* This is basically a memory transfer, but needs to be its own packet
12037 because we don't know how the target actually organizes its trace
12038 memory, plus we want to be able to ask for as much as possible, but
12039 not be unhappy if we don't get as much as we ask for. */
12040
12041static LONGEST
88ee6f45
TT
12042remote_get_raw_trace_data (struct target_ops *self,
12043 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
12044{
12045 struct remote_state *rs = get_remote_state ();
12046 char *reply;
12047 char *p;
12048 int rslt;
12049
12050 p = rs->buf;
12051 strcpy (p, "qTBuffer:");
12052 p += strlen (p);
12053 p += hexnumstr (p, offset);
12054 *p++ = ',';
12055 p += hexnumstr (p, len);
12056 *p++ = '\0';
12057
12058 putpkt (rs->buf);
12059 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12060 if (reply && *reply)
12061 {
12062 /* 'l' by itself means we're at the end of the buffer and
12063 there is nothing more to get. */
12064 if (*reply == 'l')
12065 return 0;
12066
12067 /* Convert the reply into binary. Limit the number of bytes to
12068 convert according to our passed-in buffer size, rather than
12069 what was returned in the packet; if the target is
12070 unexpectedly generous and gives us a bigger reply than we
12071 asked for, we don't want to crash. */
12072 rslt = hex2bin (target_buf, buf, len);
12073 return rslt;
12074 }
12075
12076 /* Something went wrong, flag as an error. */
12077 return -1;
12078}
12079
35b1e5cc 12080static void
37b25738 12081remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
12082{
12083 struct remote_state *rs = get_remote_state ();
12084
4082afcc 12085 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 12086 {
ad91cd99
PA
12087 char *reply;
12088
bba74b36 12089 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 12090 putpkt (rs->buf);
ad91cd99
PA
12091 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12092 if (*reply == '\0')
33da3f1c 12093 error (_("Target does not support this command."));
ad91cd99
PA
12094 if (strcmp (reply, "OK") != 0)
12095 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
12096 }
12097 else if (val)
12098 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
12099}
12100
dc146f7c
VP
12101static int
12102remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
12103{
12104 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 12105
fe978cb0
PA
12106 if (info && info->priv)
12107 return info->priv->core;
dc146f7c
VP
12108 return -1;
12109}
12110
4daf5ac0 12111static void
736d5b1f 12112remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
12113{
12114 struct remote_state *rs = get_remote_state ();
ad91cd99 12115 char *reply;
4daf5ac0 12116
bba74b36 12117 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 12118 putpkt (rs->buf);
ad91cd99
PA
12119 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12120 if (*reply == '\0')
4daf5ac0 12121 error (_("Target does not support this command."));
ad91cd99
PA
12122 if (strcmp (reply, "OK") != 0)
12123 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
12124}
12125
b3b9301e 12126static struct traceframe_info *
a893e81f 12127remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
12128{
12129 char *text;
12130
12131 text = target_read_stralloc (&current_target,
12132 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
12133 if (text != NULL)
12134 {
12135 struct traceframe_info *info;
12136 struct cleanup *back_to = make_cleanup (xfree, text);
12137
12138 info = parse_traceframe_info (text);
12139 do_cleanups (back_to);
12140 return info;
12141 }
12142
12143 return NULL;
12144}
12145
405f8e94
SS
12146/* Handle the qTMinFTPILen packet. Returns the minimum length of
12147 instruction on which a fast tracepoint may be placed. Returns -1
12148 if the packet is not supported, and 0 if the minimum instruction
12149 length is unknown. */
12150
12151static int
0e67620a 12152remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
12153{
12154 struct remote_state *rs = get_remote_state ();
12155 char *reply;
12156
e886a173
PA
12157 /* If we're not debugging a process yet, the IPA can't be
12158 loaded. */
12159 if (!target_has_execution)
12160 return 0;
12161
12162 /* Make sure the remote is pointing at the right process. */
12163 set_general_process ();
12164
bba74b36 12165 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
12166 putpkt (rs->buf);
12167 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12168 if (*reply == '\0')
12169 return -1;
12170 else
12171 {
12172 ULONGEST min_insn_len;
12173
12174 unpack_varlen_hex (reply, &min_insn_len);
12175
12176 return (int) min_insn_len;
12177 }
12178}
12179
f6f899bf 12180static void
4da384be 12181remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 12182{
4082afcc 12183 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
12184 {
12185 struct remote_state *rs = get_remote_state ();
12186 char *buf = rs->buf;
12187 char *endbuf = rs->buf + get_remote_packet_size ();
12188 enum packet_result result;
12189
12190 gdb_assert (val >= 0 || val == -1);
12191 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
12192 /* Send -1 as literal "-1" to avoid host size dependency. */
12193 if (val < 0)
12194 {
12195 *buf++ = '-';
12196 buf += hexnumstr (buf, (ULONGEST) -val);
12197 }
12198 else
12199 buf += hexnumstr (buf, (ULONGEST) val);
12200
12201 putpkt (rs->buf);
12202 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
12203 result = packet_ok (rs->buf,
12204 &remote_protocol_packets[PACKET_QTBuffer_size]);
12205
12206 if (result != PACKET_OK)
12207 warning (_("Bogus reply from target: %s"), rs->buf);
12208 }
12209}
12210
f196051f 12211static int
d9e68a2c
TT
12212remote_set_trace_notes (struct target_ops *self,
12213 const char *user, const char *notes,
ca623f82 12214 const char *stop_notes)
f196051f
SS
12215{
12216 struct remote_state *rs = get_remote_state ();
12217 char *reply;
12218 char *buf = rs->buf;
12219 char *endbuf = rs->buf + get_remote_packet_size ();
12220 int nbytes;
12221
12222 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
12223 if (user)
12224 {
12225 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 12226 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
12227 buf += 2 * nbytes;
12228 *buf++ = ';';
12229 }
12230 if (notes)
12231 {
12232 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 12233 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
12234 buf += 2 * nbytes;
12235 *buf++ = ';';
12236 }
12237 if (stop_notes)
12238 {
12239 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 12240 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
12241 buf += 2 * nbytes;
12242 *buf++ = ';';
12243 }
12244 /* Ensure the buffer is terminated. */
12245 *buf = '\0';
12246
12247 putpkt (rs->buf);
12248 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
12249 if (*reply == '\0')
12250 return 0;
12251
12252 if (strcmp (reply, "OK") != 0)
12253 error (_("Bogus reply from target: %s"), reply);
12254
12255 return 1;
12256}
12257
d1feda86 12258static int
2c152180 12259remote_use_agent (struct target_ops *self, int use)
d1feda86 12260{
4082afcc 12261 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
12262 {
12263 struct remote_state *rs = get_remote_state ();
12264
12265 /* If the stub supports QAgent. */
bba74b36 12266 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
12267 putpkt (rs->buf);
12268 getpkt (&rs->buf, &rs->buf_size, 0);
12269
12270 if (strcmp (rs->buf, "OK") == 0)
12271 {
12272 use_agent = use;
12273 return 1;
12274 }
12275 }
12276
12277 return 0;
12278}
12279
12280static int
fe38f897 12281remote_can_use_agent (struct target_ops *self)
d1feda86 12282{
4082afcc 12283 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
12284}
12285
9accd112
MM
12286struct btrace_target_info
12287{
12288 /* The ptid of the traced thread. */
12289 ptid_t ptid;
f4abbc16
MM
12290
12291 /* The obtained branch trace configuration. */
12292 struct btrace_config conf;
9accd112
MM
12293};
12294
f4abbc16
MM
12295/* Reset our idea of our target's btrace configuration. */
12296
12297static void
12298remote_btrace_reset (void)
12299{
12300 struct remote_state *rs = get_remote_state ();
12301
12302 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
12303}
12304
9accd112
MM
12305/* Check whether the target supports branch tracing. */
12306
12307static int
043c3577 12308remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 12309{
4082afcc 12310 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 12311 return 0;
4082afcc 12312 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
12313 return 0;
12314
043c3577
MM
12315 switch (format)
12316 {
12317 case BTRACE_FORMAT_NONE:
12318 return 0;
12319
12320 case BTRACE_FORMAT_BTS:
12321 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
12322
12323 case BTRACE_FORMAT_PT:
12324 /* The trace is decoded on the host. Even if our target supports it,
12325 we still need to have libipt to decode the trace. */
12326#if defined (HAVE_LIBIPT)
12327 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
12328#else /* !defined (HAVE_LIBIPT) */
12329 return 0;
12330#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
12331 }
12332
12333 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
12334}
12335
f4abbc16
MM
12336/* Synchronize the configuration with the target. */
12337
12338static void
12339btrace_sync_conf (const struct btrace_config *conf)
12340{
d33501a5
MM
12341 struct packet_config *packet;
12342 struct remote_state *rs;
12343 char *buf, *pos, *endbuf;
12344
12345 rs = get_remote_state ();
12346 buf = rs->buf;
12347 endbuf = buf + get_remote_packet_size ();
12348
12349 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
12350 if (packet_config_support (packet) == PACKET_ENABLE
12351 && conf->bts.size != rs->btrace_config.bts.size)
12352 {
12353 pos = buf;
12354 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
12355 conf->bts.size);
12356
12357 putpkt (buf);
12358 getpkt (&buf, &rs->buf_size, 0);
12359
12360 if (packet_ok (buf, packet) == PACKET_ERROR)
12361 {
12362 if (buf[0] == 'E' && buf[1] == '.')
12363 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
12364 else
12365 error (_("Failed to configure the BTS buffer size."));
12366 }
12367
12368 rs->btrace_config.bts.size = conf->bts.size;
12369 }
b20a6524
MM
12370
12371 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
12372 if (packet_config_support (packet) == PACKET_ENABLE
12373 && conf->pt.size != rs->btrace_config.pt.size)
12374 {
12375 pos = buf;
12376 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
12377 conf->pt.size);
12378
12379 putpkt (buf);
12380 getpkt (&buf, &rs->buf_size, 0);
12381
12382 if (packet_ok (buf, packet) == PACKET_ERROR)
12383 {
12384 if (buf[0] == 'E' && buf[1] == '.')
12385 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
12386 else
12387 error (_("Failed to configure the trace buffer size."));
12388 }
12389
12390 rs->btrace_config.pt.size = conf->pt.size;
12391 }
f4abbc16
MM
12392}
12393
12394/* Read the current thread's btrace configuration from the target and
12395 store it into CONF. */
12396
12397static void
12398btrace_read_config (struct btrace_config *conf)
12399{
12400 char *xml;
12401
12402 xml = target_read_stralloc (&current_target,
b20a6524 12403 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
12404 if (xml != NULL)
12405 {
12406 struct cleanup *cleanup;
12407
12408 cleanup = make_cleanup (xfree, xml);
12409 parse_xml_btrace_conf (conf, xml);
12410 do_cleanups (cleanup);
12411 }
12412}
12413
9accd112
MM
12414/* Enable branch tracing. */
12415
12416static struct btrace_target_info *
f4abbc16
MM
12417remote_enable_btrace (struct target_ops *self, ptid_t ptid,
12418 const struct btrace_config *conf)
9accd112
MM
12419{
12420 struct btrace_target_info *tinfo = NULL;
b20a6524 12421 struct packet_config *packet = NULL;
9accd112
MM
12422 struct remote_state *rs = get_remote_state ();
12423 char *buf = rs->buf;
12424 char *endbuf = rs->buf + get_remote_packet_size ();
12425
b20a6524
MM
12426 switch (conf->format)
12427 {
12428 case BTRACE_FORMAT_BTS:
12429 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
12430 break;
12431
12432 case BTRACE_FORMAT_PT:
12433 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
12434 break;
12435 }
12436
12437 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12438 error (_("Target does not support branch tracing."));
12439
f4abbc16
MM
12440 btrace_sync_conf (conf);
12441
9accd112
MM
12442 set_general_thread (ptid);
12443
12444 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12445 putpkt (rs->buf);
12446 getpkt (&rs->buf, &rs->buf_size, 0);
12447
12448 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12449 {
12450 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12451 error (_("Could not enable branch tracing for %s: %s"),
12452 target_pid_to_str (ptid), rs->buf + 2);
12453 else
12454 error (_("Could not enable branch tracing for %s."),
12455 target_pid_to_str (ptid));
12456 }
12457
8d749320 12458 tinfo = XCNEW (struct btrace_target_info);
9accd112
MM
12459 tinfo->ptid = ptid;
12460
f4abbc16
MM
12461 /* If we fail to read the configuration, we lose some information, but the
12462 tracing itself is not impacted. */
492d29ea
PA
12463 TRY
12464 {
12465 btrace_read_config (&tinfo->conf);
12466 }
12467 CATCH (err, RETURN_MASK_ERROR)
12468 {
12469 if (err.message != NULL)
12470 warning ("%s", err.message);
12471 }
12472 END_CATCH
f4abbc16 12473
9accd112
MM
12474 return tinfo;
12475}
12476
12477/* Disable branch tracing. */
12478
12479static void
25e95349
TT
12480remote_disable_btrace (struct target_ops *self,
12481 struct btrace_target_info *tinfo)
9accd112
MM
12482{
12483 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12484 struct remote_state *rs = get_remote_state ();
12485 char *buf = rs->buf;
12486 char *endbuf = rs->buf + get_remote_packet_size ();
12487
4082afcc 12488 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12489 error (_("Target does not support branch tracing."));
12490
12491 set_general_thread (tinfo->ptid);
12492
12493 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12494 putpkt (rs->buf);
12495 getpkt (&rs->buf, &rs->buf_size, 0);
12496
12497 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12498 {
12499 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12500 error (_("Could not disable branch tracing for %s: %s"),
12501 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12502 else
12503 error (_("Could not disable branch tracing for %s."),
12504 target_pid_to_str (tinfo->ptid));
12505 }
12506
12507 xfree (tinfo);
12508}
12509
12510/* Teardown branch tracing. */
12511
12512static void
1777056d
TT
12513remote_teardown_btrace (struct target_ops *self,
12514 struct btrace_target_info *tinfo)
9accd112
MM
12515{
12516 /* We must not talk to the target during teardown. */
12517 xfree (tinfo);
12518}
12519
12520/* Read the branch trace. */
12521
969c39fb 12522static enum btrace_error
39c49f83 12523remote_read_btrace (struct target_ops *self,
734b0e4b 12524 struct btrace_data *btrace,
969c39fb 12525 struct btrace_target_info *tinfo,
9accd112
MM
12526 enum btrace_read_type type)
12527{
12528 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12529 struct remote_state *rs = get_remote_state ();
969c39fb 12530 struct cleanup *cleanup;
9accd112
MM
12531 const char *annex;
12532 char *xml;
12533
4082afcc 12534 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12535 error (_("Target does not support branch tracing."));
12536
12537#if !defined(HAVE_LIBEXPAT)
12538 error (_("Cannot process branch tracing result. XML parsing not supported."));
12539#endif
12540
12541 switch (type)
12542 {
864089d2 12543 case BTRACE_READ_ALL:
9accd112
MM
12544 annex = "all";
12545 break;
864089d2 12546 case BTRACE_READ_NEW:
9accd112
MM
12547 annex = "new";
12548 break;
969c39fb
MM
12549 case BTRACE_READ_DELTA:
12550 annex = "delta";
12551 break;
9accd112
MM
12552 default:
12553 internal_error (__FILE__, __LINE__,
12554 _("Bad branch tracing read type: %u."),
12555 (unsigned int) type);
12556 }
12557
12558 xml = target_read_stralloc (&current_target,
b20a6524 12559 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
12560 if (xml == NULL)
12561 return BTRACE_ERR_UNKNOWN;
9accd112 12562
969c39fb 12563 cleanup = make_cleanup (xfree, xml);
734b0e4b 12564 parse_xml_btrace (btrace, xml);
969c39fb 12565 do_cleanups (cleanup);
9accd112 12566
969c39fb 12567 return BTRACE_ERR_NONE;
9accd112
MM
12568}
12569
f4abbc16
MM
12570static const struct btrace_config *
12571remote_btrace_conf (struct target_ops *self,
12572 const struct btrace_target_info *tinfo)
12573{
12574 return &tinfo->conf;
12575}
12576
ced63ec0 12577static int
5436ff03 12578remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12579{
4082afcc
PA
12580 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12581 == PACKET_ENABLE);
ced63ec0
GB
12582}
12583
9dd130a0
TT
12584/* Implementation of to_load. */
12585
12586static void
9cbe5fff 12587remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12588{
12589 generic_load (name, from_tty);
12590}
12591
c78fa86a
GB
12592/* Accepts an integer PID; returns a string representing a file that
12593 can be opened on the remote side to get the symbols for the child
12594 process. Returns NULL if the operation is not supported. */
12595
12596static char *
12597remote_pid_to_exec_file (struct target_ops *self, int pid)
12598{
12599 static char *filename = NULL;
835205d0
GB
12600 struct inferior *inf;
12601 char *annex = NULL;
c78fa86a
GB
12602
12603 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12604 return NULL;
12605
12606 if (filename != NULL)
12607 xfree (filename);
12608
835205d0
GB
12609 inf = find_inferior_pid (pid);
12610 if (inf == NULL)
12611 internal_error (__FILE__, __LINE__,
12612 _("not currently attached to process %d"), pid);
12613
12614 if (!inf->fake_pid_p)
12615 {
12616 const int annex_size = 9;
12617
12618 annex = alloca (annex_size);
12619 xsnprintf (annex, annex_size, "%x", pid);
12620 }
12621
c78fa86a
GB
12622 filename = target_read_stralloc (&current_target,
12623 TARGET_OBJECT_EXEC_FILE, annex);
12624
12625 return filename;
12626}
12627
c906108c 12628static void
fba45db2 12629init_remote_ops (void)
c906108c 12630{
c5aa993b 12631 remote_ops.to_shortname = "remote";
c906108c 12632 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12633 remote_ops.to_doc =
c906108c 12634 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12635Specify the serial device it is connected to\n\
12636(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12637 remote_ops.to_open = remote_open;
12638 remote_ops.to_close = remote_close;
c906108c 12639 remote_ops.to_detach = remote_detach;
6ad8ae5c 12640 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12641 remote_ops.to_resume = remote_resume;
c906108c
SS
12642 remote_ops.to_wait = remote_wait;
12643 remote_ops.to_fetch_registers = remote_fetch_registers;
12644 remote_ops.to_store_registers = remote_store_registers;
12645 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12646 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12647 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12648 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12649 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12650 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12651 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12652 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12653 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12654 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12655 remote_ops.to_watchpoint_addr_within_range =
12656 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12657 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12658 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12659 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12660 remote_ops.to_region_ok_for_hw_watchpoint
12661 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12662 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12663 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12664 remote_ops.to_kill = remote_kill;
9dd130a0 12665 remote_ops.to_load = remote_load;
c906108c 12666 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12667 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12668 remote_ops.to_program_signals = remote_program_signals;
c906108c 12669 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12670 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12671 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12672 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12673 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12674 remote_ops.to_stop = remote_stop;
bfedc46a 12675 remote_ops.to_interrupt = remote_interrupt;
abc56d60 12676 remote_ops.to_check_pending_interrupt = remote_check_pending_interrupt;
4b8a223f 12677 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12678 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12679 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12680 remote_ops.to_log_command = serial_log_command;
38691318 12681 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12682 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12683 remote_ops.to_has_all_memory = default_child_has_all_memory;
12684 remote_ops.to_has_memory = default_child_has_memory;
12685 remote_ops.to_has_stack = default_child_has_stack;
12686 remote_ops.to_has_registers = default_child_has_registers;
12687 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12688 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12689 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12690 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12691 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12692 remote_ops.to_flash_erase = remote_flash_erase;
12693 remote_ops.to_flash_done = remote_flash_done;
29709017 12694 remote_ops.to_read_description = remote_read_description;
08388c79 12695 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12696 remote_ops.to_can_async_p = remote_can_async_p;
12697 remote_ops.to_is_async_p = remote_is_async_p;
12698 remote_ops.to_async = remote_async;
75c99385
PA
12699 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12700 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12701 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12702 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12703 remote_ops.to_supports_disable_randomization
12704 = remote_supports_disable_randomization;
4bd7dc42 12705 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12706 remote_ops.to_fileio_open = remote_hostio_open;
12707 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12708 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12709 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12710 remote_ops.to_fileio_close = remote_hostio_close;
12711 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12712 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12713 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12714 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12715 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12716 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12717 remote_ops.to_trace_init = remote_trace_init;
12718 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12719 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12720 remote_ops.to_download_trace_state_variable
12721 = remote_download_trace_state_variable;
d248b706
KY
12722 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12723 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12724 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12725 remote_ops.to_trace_start = remote_trace_start;
12726 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12727 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12728 remote_ops.to_trace_stop = remote_trace_stop;
12729 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12730 remote_ops.to_get_trace_state_variable_value
12731 = remote_get_trace_state_variable_value;
00bf0b85
SS
12732 remote_ops.to_save_trace_data = remote_save_trace_data;
12733 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12734 remote_ops.to_upload_trace_state_variables
12735 = remote_upload_trace_state_variables;
00bf0b85 12736 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12737 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12738 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12739 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12740 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12741 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12742 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12743 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12744 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12745 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12746 remote_ops.to_static_tracepoint_marker_at
12747 = remote_static_tracepoint_marker_at;
12748 remote_ops.to_static_tracepoint_markers_by_strid
12749 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12750 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12751 remote_ops.to_use_agent = remote_use_agent;
12752 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12753 remote_ops.to_supports_btrace = remote_supports_btrace;
12754 remote_ops.to_enable_btrace = remote_enable_btrace;
12755 remote_ops.to_disable_btrace = remote_disable_btrace;
12756 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12757 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12758 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12759 remote_ops.to_augmented_libraries_svr4_read =
12760 remote_augmented_libraries_svr4_read;
c906108c
SS
12761}
12762
12763/* Set up the extended remote vector by making a copy of the standard
12764 remote vector and adding to it. */
12765
12766static void
fba45db2 12767init_extended_remote_ops (void)
c906108c
SS
12768{
12769 extended_remote_ops = remote_ops;
12770
0f71a2f6 12771 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12772 extended_remote_ops.to_longname =
c906108c 12773 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12774 extended_remote_ops.to_doc =
c906108c 12775 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12776Specify the serial device it is connected to (e.g. /dev/ttya).";
12777 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12778 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12779 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12780 extended_remote_ops.to_detach = extended_remote_detach;
12781 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12782 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12783 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12784 extended_remote_ops.to_supports_disable_randomization
12785 = extended_remote_supports_disable_randomization;
de0d863e 12786 extended_remote_ops.to_follow_fork = remote_follow_fork;
94585166 12787 extended_remote_ops.to_follow_exec = remote_follow_exec;
cbb8991c
DB
12788 extended_remote_ops.to_insert_fork_catchpoint
12789 = remote_insert_fork_catchpoint;
12790 extended_remote_ops.to_remove_fork_catchpoint
12791 = remote_remove_fork_catchpoint;
12792 extended_remote_ops.to_insert_vfork_catchpoint
12793 = remote_insert_vfork_catchpoint;
12794 extended_remote_ops.to_remove_vfork_catchpoint
12795 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12796}
12797
6426a772 12798static int
6a109b6b 12799remote_can_async_p (struct target_ops *ops)
6426a772 12800{
5d93a237
TT
12801 struct remote_state *rs = get_remote_state ();
12802
c6ebd6cf 12803 if (!target_async_permitted)
75c99385
PA
12804 /* We only enable async when the user specifically asks for it. */
12805 return 0;
12806
23860348 12807 /* We're async whenever the serial device is. */
5d93a237 12808 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12809}
12810
12811static int
6a109b6b 12812remote_is_async_p (struct target_ops *ops)
6426a772 12813{
5d93a237
TT
12814 struct remote_state *rs = get_remote_state ();
12815
c6ebd6cf 12816 if (!target_async_permitted)
75c99385
PA
12817 /* We only enable async when the user specifically asks for it. */
12818 return 0;
12819
23860348 12820 /* We're async whenever the serial device is. */
5d93a237 12821 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12822}
12823
2acceee2
JM
12824/* Pass the SERIAL event on and up to the client. One day this code
12825 will be able to delay notifying the client of an event until the
23860348 12826 point where an entire packet has been received. */
2acceee2 12827
2acceee2
JM
12828static serial_event_ftype remote_async_serial_handler;
12829
6426a772 12830static void
819cc324 12831remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12832{
88b496c3
TT
12833 struct remote_state *rs = context;
12834
2acceee2
JM
12835 /* Don't propogate error information up to the client. Instead let
12836 the client find out about the error by querying the target. */
6a3753b3 12837 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12838}
12839
74531fed
PA
12840static void
12841remote_async_inferior_event_handler (gdb_client_data data)
12842{
12843 inferior_event_handler (INF_REG_EVENT, NULL);
12844}
12845
2acceee2 12846static void
6a3753b3 12847remote_async (struct target_ops *ops, int enable)
2acceee2 12848{
5d93a237
TT
12849 struct remote_state *rs = get_remote_state ();
12850
6a3753b3 12851 if (enable)
2acceee2 12852 {
88b496c3 12853 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12854
12855 /* If there are pending events in the stop reply queue tell the
12856 event loop to process them. */
12857 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12858 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12859 }
12860 else
b7d2e916
PA
12861 {
12862 serial_async (rs->remote_desc, NULL, NULL);
12863 clear_async_event_handler (remote_async_inferior_event_token);
12864 }
6426a772
JM
12865}
12866
5a2468f5 12867static void
c2d11a7d 12868set_remote_cmd (char *args, int from_tty)
5a2468f5 12869{
635c7e8a 12870 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12871}
12872
d471ea57
AC
12873static void
12874show_remote_cmd (char *args, int from_tty)
12875{
37a105a1 12876 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12877 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12878 struct cleanup *showlist_chain;
12879 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12880 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12881
12882 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12883 for (; list != NULL; list = list->next)
12884 if (strcmp (list->name, "Z-packet") == 0)
12885 continue;
427c3a89
DJ
12886 else if (list->type == not_set_cmd)
12887 /* Alias commands are exactly like the original, except they
12888 don't have the normal type. */
12889 continue;
12890 else
37a105a1
DJ
12891 {
12892 struct cleanup *option_chain
12893 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12894
37a105a1
DJ
12895 ui_out_field_string (uiout, "name", list->name);
12896 ui_out_text (uiout, ": ");
427c3a89 12897 if (list->type == show_cmd)
5b9afe8a 12898 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12899 else
12900 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12901 /* Close the tuple. */
12902 do_cleanups (option_chain);
12903 }
427c3a89
DJ
12904
12905 /* Close the tuple. */
12906 do_cleanups (showlist_chain);
d471ea57 12907}
5a2468f5 12908
0f71a2f6 12909
23860348 12910/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12911static void
12912remote_new_objfile (struct objfile *objfile)
12913{
5d93a237
TT
12914 struct remote_state *rs = get_remote_state ();
12915
12916 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12917 remote_check_symbols ();
dc8acb97
MS
12918}
12919
00bf0b85
SS
12920/* Pull all the tracepoints defined on the target and create local
12921 data structures representing them. We don't want to create real
12922 tracepoints yet, we don't want to mess up the user's existing
12923 collection. */
12924
12925static int
ab6617cc 12926remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12927{
00bf0b85
SS
12928 struct remote_state *rs = get_remote_state ();
12929 char *p;
d5551862 12930
00bf0b85
SS
12931 /* Ask for a first packet of tracepoint definition. */
12932 putpkt ("qTfP");
12933 getpkt (&rs->buf, &rs->buf_size, 0);
12934 p = rs->buf;
12935 while (*p && *p != 'l')
d5551862 12936 {
00bf0b85
SS
12937 parse_tracepoint_definition (p, utpp);
12938 /* Ask for another packet of tracepoint definition. */
12939 putpkt ("qTsP");
12940 getpkt (&rs->buf, &rs->buf_size, 0);
12941 p = rs->buf;
d5551862 12942 }
00bf0b85 12943 return 0;
d5551862
SS
12944}
12945
00bf0b85 12946static int
181e3713
TT
12947remote_upload_trace_state_variables (struct target_ops *self,
12948 struct uploaded_tsv **utsvp)
d5551862 12949{
00bf0b85 12950 struct remote_state *rs = get_remote_state ();
d5551862 12951 char *p;
d5551862 12952
00bf0b85
SS
12953 /* Ask for a first packet of variable definition. */
12954 putpkt ("qTfV");
d5551862
SS
12955 getpkt (&rs->buf, &rs->buf_size, 0);
12956 p = rs->buf;
00bf0b85 12957 while (*p && *p != 'l')
d5551862 12958 {
00bf0b85
SS
12959 parse_tsv_definition (p, utsvp);
12960 /* Ask for another packet of variable definition. */
12961 putpkt ("qTsV");
d5551862
SS
12962 getpkt (&rs->buf, &rs->buf_size, 0);
12963 p = rs->buf;
12964 }
00bf0b85 12965 return 0;
d5551862
SS
12966}
12967
c1e36e3e
PA
12968/* The "set/show range-stepping" show hook. */
12969
12970static void
12971show_range_stepping (struct ui_file *file, int from_tty,
12972 struct cmd_list_element *c,
12973 const char *value)
12974{
12975 fprintf_filtered (file,
12976 _("Debugger's willingness to use range stepping "
12977 "is %s.\n"), value);
12978}
12979
12980/* The "set/show range-stepping" set hook. */
12981
12982static void
12983set_range_stepping (char *ignore_args, int from_tty,
12984 struct cmd_list_element *c)
12985{
5d93a237
TT
12986 struct remote_state *rs = get_remote_state ();
12987
c1e36e3e
PA
12988 /* Whene enabling, check whether range stepping is actually
12989 supported by the target, and warn if not. */
12990 if (use_range_stepping)
12991 {
5d93a237 12992 if (rs->remote_desc != NULL)
c1e36e3e 12993 {
4082afcc 12994 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12995 remote_vcont_probe (rs);
12996
4082afcc 12997 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12998 && rs->supports_vCont.r)
12999 return;
13000 }
13001
13002 warning (_("Range stepping is not supported by the current target"));
13003 }
13004}
13005
c906108c 13006void
fba45db2 13007_initialize_remote (void)
c906108c 13008{
ea9c271d 13009 struct remote_state *rs;
9a7071a8 13010 struct cmd_list_element *cmd;
6f937416 13011 const char *cmd_name;
ea9c271d 13012
0f71a2f6 13013 /* architecture specific data */
2bc416ba 13014 remote_gdbarch_data_handle =
23860348 13015 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
13016 remote_g_packet_data_handle =
13017 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 13018
94585166
DB
13019 remote_pspace_data
13020 = register_program_space_data_with_cleanup (NULL,
13021 remote_pspace_data_cleanup);
13022
ea9c271d
DJ
13023 /* Initialize the per-target state. At the moment there is only one
13024 of these, not one per target. Only one target is active at a
cf792862
TT
13025 time. */
13026 remote_state = new_remote_state ();
ea9c271d 13027
c906108c
SS
13028 init_remote_ops ();
13029 add_target (&remote_ops);
13030
13031 init_extended_remote_ops ();
13032 add_target (&extended_remote_ops);
cce74817 13033
dc8acb97 13034 /* Hook into new objfile notification. */
06d3b283 13035 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
13036 /* We're no longer interested in notification events of an inferior
13037 when it exits. */
13038 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 13039
b803fb0f 13040 /* Set up signal handlers. */
934b9bac 13041 async_sigint_remote_token =
b803fb0f 13042 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 13043 async_sigint_remote_twice_token =
6d549500 13044 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 13045
c906108c
SS
13046#if 0
13047 init_remote_threadtests ();
13048#endif
13049
722247f1 13050 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 13051 /* set/show remote ... */
d471ea57 13052
1bedd215 13053 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
13054Remote protocol specific variables\n\
13055Configure various remote-protocol specific variables such as\n\
1bedd215 13056the packets being used"),
cff3e48b 13057 &remote_set_cmdlist, "set remote ",
23860348 13058 0 /* allow-unknown */, &setlist);
1bedd215 13059 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
13060Remote protocol specific variables\n\
13061Configure various remote-protocol specific variables such as\n\
1bedd215 13062the packets being used"),
cff3e48b 13063 &remote_show_cmdlist, "show remote ",
23860348 13064 0 /* allow-unknown */, &showlist);
5a2468f5 13065
1a966eab
AC
13066 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
13067Compare section data on target to the exec file.\n\
95cf3b38
DT
13068Argument is a single section name (default: all loaded sections).\n\
13069To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
13070 &cmdlist);
13071
1a966eab
AC
13072 add_cmd ("packet", class_maintenance, packet_command, _("\
13073Send an arbitrary packet to a remote target.\n\
c906108c
SS
13074 maintenance packet TEXT\n\
13075If GDB is talking to an inferior via the GDB serial protocol, then\n\
13076this command sends the string TEXT to the inferior, and displays the\n\
13077response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 13078terminating `#' character and checksum."),
c906108c
SS
13079 &maintenancelist);
13080
7915a72c
AC
13081 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
13082Set whether to send break if interrupted."), _("\
13083Show whether to send break if interrupted."), _("\
13084If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 13085 set_remotebreak, show_remotebreak,
e707bbc2 13086 &setlist, &showlist);
9a7071a8
JB
13087 cmd_name = "remotebreak";
13088 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
13089 deprecate_cmd (cmd, "set remote interrupt-sequence");
13090 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
13091 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
13092 deprecate_cmd (cmd, "show remote interrupt-sequence");
13093
13094 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
13095 interrupt_sequence_modes, &interrupt_sequence_mode,
13096 _("\
9a7071a8
JB
13097Set interrupt sequence to remote target."), _("\
13098Show interrupt sequence to remote target."), _("\
13099Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
13100 NULL, show_interrupt_sequence,
13101 &remote_set_cmdlist,
13102 &remote_show_cmdlist);
13103
13104 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
13105 &interrupt_on_connect, _("\
13106Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
13107Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
13108If set, interrupt sequence is sent to remote target."),
13109 NULL, NULL,
13110 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 13111
23860348 13112 /* Install commands for configuring memory read/write packets. */
11cf8741 13113
1a966eab
AC
13114 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
13115Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 13116 &setlist);
1a966eab
AC
13117 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
13118Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
13119 &showlist);
13120 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
13121 set_memory_write_packet_size, _("\
13122Set the maximum number of bytes per memory-write packet.\n\
13123Specify the number of bytes in a packet or 0 (zero) for the\n\
13124default packet size. The actual limit is further reduced\n\
13125dependent on the target. Specify ``fixed'' to disable the\n\
13126further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
13127 &remote_set_cmdlist);
13128 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
13129 set_memory_read_packet_size, _("\
13130Set the maximum number of bytes per memory-read packet.\n\
13131Specify the number of bytes in a packet or 0 (zero) for the\n\
13132default packet size. The actual limit is further reduced\n\
13133dependent on the target. Specify ``fixed'' to disable the\n\
13134further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
13135 &remote_set_cmdlist);
13136 add_cmd ("memory-write-packet-size", no_class,
13137 show_memory_write_packet_size,
1a966eab 13138 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
13139 &remote_show_cmdlist);
13140 add_cmd ("memory-read-packet-size", no_class,
13141 show_memory_read_packet_size,
1a966eab 13142 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 13143 &remote_show_cmdlist);
c906108c 13144
b3f42336 13145 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
13146 &remote_hw_watchpoint_limit, _("\
13147Set the maximum number of target hardware watchpoints."), _("\
13148Show the maximum number of target hardware watchpoints."), _("\
13149Specify a negative limit for unlimited."),
3e43a32a
MS
13150 NULL, NULL, /* FIXME: i18n: The maximum
13151 number of target hardware
13152 watchpoints is %s. */
b3f42336 13153 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
13154 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
13155 &remote_hw_watchpoint_length_limit, _("\
13156Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
13157Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
13158Specify a negative limit for unlimited."),
13159 NULL, NULL, /* FIXME: i18n: The maximum
13160 length (in bytes) of a target
13161 hardware watchpoint is %s. */
13162 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 13163 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
13164 &remote_hw_breakpoint_limit, _("\
13165Set the maximum number of target hardware breakpoints."), _("\
13166Show the maximum number of target hardware breakpoints."), _("\
13167Specify a negative limit for unlimited."),
3e43a32a
MS
13168 NULL, NULL, /* FIXME: i18n: The maximum
13169 number of target hardware
13170 breakpoints is %s. */
b3f42336 13171 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 13172
1b493192
PA
13173 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
13174 &remote_address_size, _("\
4d28ad1e
AC
13175Set the maximum size of the address (in bits) in a memory packet."), _("\
13176Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
13177 NULL,
13178 NULL, /* FIXME: i18n: */
13179 &setlist, &showlist);
c906108c 13180
ca4f7f8b
PA
13181 init_all_packet_configs ();
13182
444abaca 13183 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 13184 "X", "binary-download", 1);
0f71a2f6 13185
444abaca 13186 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 13187 "vCont", "verbose-resume", 0);
506fb367 13188
89be2091
DJ
13189 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
13190 "QPassSignals", "pass-signals", 0);
13191
9b224c5e
PA
13192 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
13193 "QProgramSignals", "program-signals", 0);
13194
444abaca 13195 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 13196 "qSymbol", "symbol-lookup", 0);
dc8acb97 13197
444abaca 13198 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 13199 "P", "set-register", 1);
d471ea57 13200
444abaca 13201 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 13202 "p", "fetch-register", 1);
b96ec7ac 13203
444abaca 13204 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 13205 "Z0", "software-breakpoint", 0);
d471ea57 13206
444abaca 13207 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 13208 "Z1", "hardware-breakpoint", 0);
d471ea57 13209
444abaca 13210 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 13211 "Z2", "write-watchpoint", 0);
d471ea57 13212
444abaca 13213 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 13214 "Z3", "read-watchpoint", 0);
d471ea57 13215
444abaca 13216 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 13217 "Z4", "access-watchpoint", 0);
d471ea57 13218
0876f84a
DJ
13219 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
13220 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 13221
c78fa86a
GB
13222 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
13223 "qXfer:exec-file:read", "pid-to-exec-file", 0);
13224
23181151
DJ
13225 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
13226 "qXfer:features:read", "target-features", 0);
13227
cfa9d6d9
DJ
13228 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
13229 "qXfer:libraries:read", "library-info", 0);
13230
2268b414
JK
13231 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
13232 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
13233
fd79ecee
DJ
13234 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
13235 "qXfer:memory-map:read", "memory-map", 0);
13236
0e7f50da
UW
13237 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
13238 "qXfer:spu:read", "read-spu-object", 0);
13239
13240 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
13241 "qXfer:spu:write", "write-spu-object", 0);
13242
07e059b5
VP
13243 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
13244 "qXfer:osdata:read", "osdata", 0);
13245
dc146f7c
VP
13246 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
13247 "qXfer:threads:read", "threads", 0);
13248
4aa995e1
PA
13249 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
13250 "qXfer:siginfo:read", "read-siginfo-object", 0);
13251
13252 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
13253 "qXfer:siginfo:write", "write-siginfo-object", 0);
13254
b3b9301e
PA
13255 add_packet_config_cmd
13256 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 13257 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 13258
169081d0
TG
13259 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
13260 "qXfer:uib:read", "unwind-info-block", 0);
13261
444abaca 13262 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 13263 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
13264 0);
13265
711e434b
PM
13266 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
13267 "qGetTIBAddr", "get-thread-information-block-address",
13268 0);
13269
40ab02ce
MS
13270 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
13271 "bc", "reverse-continue", 0);
13272
13273 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
13274 "bs", "reverse-step", 0);
13275
be2a5f71
DJ
13276 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
13277 "qSupported", "supported-packets", 0);
13278
08388c79
DE
13279 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
13280 "qSearch:memory", "search-memory", 0);
13281
bd3eecc3
PA
13282 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
13283 "qTStatus", "trace-status", 0);
13284
15a201c8
GB
13285 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
13286 "vFile:setfs", "hostio-setfs", 0);
13287
a6b151f1
DJ
13288 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
13289 "vFile:open", "hostio-open", 0);
13290
13291 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
13292 "vFile:pread", "hostio-pread", 0);
13293
13294 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
13295 "vFile:pwrite", "hostio-pwrite", 0);
13296
13297 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
13298 "vFile:close", "hostio-close", 0);
13299
13300 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
13301 "vFile:unlink", "hostio-unlink", 0);
13302
b9e7b9c3
UW
13303 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
13304 "vFile:readlink", "hostio-readlink", 0);
13305
0a93529c
GB
13306 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
13307 "vFile:fstat", "hostio-fstat", 0);
13308
2d717e4f
DJ
13309 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
13310 "vAttach", "attach", 0);
13311
13312 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
13313 "vRun", "run", 0);
13314
a6f3e723
SL
13315 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
13316 "QStartNoAckMode", "noack", 0);
13317
82f73884
PA
13318 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
13319 "vKill", "kill", 0);
13320
0b16c5cf
PA
13321 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
13322 "qAttached", "query-attached", 0);
13323
782b2b07 13324 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
13325 "ConditionalTracepoints",
13326 "conditional-tracepoints", 0);
3788aec7
LM
13327
13328 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
13329 "ConditionalBreakpoints",
13330 "conditional-breakpoints", 0);
13331
d3ce09f5
SS
13332 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
13333 "BreakpointCommands",
13334 "breakpoint-commands", 0);
13335
7a697b8d
SS
13336 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
13337 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 13338
409873ef
SS
13339 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
13340 "TracepointSource", "TracepointSource", 0);
13341
d914c394
SS
13342 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
13343 "QAllow", "allow", 0);
13344
0fb4aa4b
PA
13345 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
13346 "StaticTracepoints", "static-tracepoints", 0);
13347
1e4d1764
YQ
13348 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
13349 "InstallInTrace", "install-in-trace", 0);
13350
0fb4aa4b
PA
13351 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
13352 "qXfer:statictrace:read", "read-sdata-object", 0);
13353
78d85199
YQ
13354 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
13355 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
13356
03583c20
UW
13357 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
13358 "QDisableRandomization", "disable-randomization", 0);
13359
d1feda86
YQ
13360 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
13361 "QAgent", "agent", 0);
13362
f6f899bf
HAQ
13363 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
13364 "QTBuffer:size", "trace-buffer-size", 0);
13365
9accd112
MM
13366 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
13367 "Qbtrace:off", "disable-btrace", 0);
13368
13369 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
13370 "Qbtrace:bts", "enable-btrace-bts", 0);
13371
13372 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
13373 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
13374
13375 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
13376 "qXfer:btrace", "read-btrace", 0);
13377
f4abbc16
MM
13378 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
13379 "qXfer:btrace-conf", "read-btrace-conf", 0);
13380
d33501a5
MM
13381 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
13382 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
13383
73b8c1fd
PA
13384 add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
13385 "multiprocess-feature", "multiprocess-feature", 0);
13386
f7e6eed5
PA
13387 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
13388 "swbreak-feature", "swbreak-feature", 0);
13389
13390 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
13391 "hwbreak-feature", "hwbreak-feature", 0);
13392
89245bc0
DB
13393 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
13394 "fork-event-feature", "fork-event-feature", 0);
13395
13396 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
13397 "vfork-event-feature", "vfork-event-feature", 0);
13398
b20a6524
MM
13399 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
13400 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
13401
94585166
DB
13402 add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
13403 "exec-event-feature", "exec-event-feature", 0);
13404
0b736949
DB
13405 /* Assert that we've registered "set remote foo-packet" commands
13406 for all packet configs. */
ca4f7f8b
PA
13407 {
13408 int i;
13409
13410 for (i = 0; i < PACKET_MAX; i++)
13411 {
13412 /* Ideally all configs would have a command associated. Some
13413 still don't though. */
13414 int excepted;
13415
13416 switch (i)
13417 {
13418 case PACKET_QNonStop:
ca4f7f8b
PA
13419 case PACKET_EnableDisableTracepoints_feature:
13420 case PACKET_tracenz_feature:
13421 case PACKET_DisconnectedTracing_feature:
13422 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
13423 case PACKET_qCRC:
13424 /* Additions to this list need to be well justified:
13425 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
13426 excepted = 1;
13427 break;
13428 default:
13429 excepted = 0;
13430 break;
13431 }
13432
13433 /* This catches both forgetting to add a config command, and
13434 forgetting to remove a packet from the exception list. */
13435 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
13436 }
13437 }
13438
37a105a1
DJ
13439 /* Keep the old ``set remote Z-packet ...'' working. Each individual
13440 Z sub-packet has its own set and show commands, but users may
13441 have sets to this variable in their .gdbinit files (or in their
13442 documentation). */
e9e68a56 13443 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
13444 &remote_Z_packet_detect, _("\
13445Set use of remote protocol `Z' packets"), _("\
13446Show use of remote protocol `Z' packets "), _("\
3b64bf98 13447When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 13448packets."),
e9e68a56 13449 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
13450 show_remote_protocol_Z_packet_cmd,
13451 /* FIXME: i18n: Use of remote protocol
13452 `Z' packets is %s. */
e9e68a56 13453 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 13454
a6b151f1
DJ
13455 add_prefix_cmd ("remote", class_files, remote_command, _("\
13456Manipulate files on the remote system\n\
13457Transfer files to and from the remote target system."),
13458 &remote_cmdlist, "remote ",
13459 0 /* allow-unknown */, &cmdlist);
13460
13461 add_cmd ("put", class_files, remote_put_command,
13462 _("Copy a local file to the remote system."),
13463 &remote_cmdlist);
13464
13465 add_cmd ("get", class_files, remote_get_command,
13466 _("Copy a remote file to the local system."),
13467 &remote_cmdlist);
13468
13469 add_cmd ("delete", class_files, remote_delete_command,
13470 _("Delete a remote file."),
13471 &remote_cmdlist);
13472
2d717e4f 13473 add_setshow_string_noescape_cmd ("exec-file", class_files,
94585166 13474 &remote_exec_file_var, _("\
2d717e4f 13475Set the remote pathname for \"run\""), _("\
94585166
DB
13476Show the remote pathname for \"run\""), NULL,
13477 set_remote_exec_file,
13478 show_remote_exec_file,
13479 &remote_set_cmdlist,
13480 &remote_show_cmdlist);
2d717e4f 13481
c1e36e3e
PA
13482 add_setshow_boolean_cmd ("range-stepping", class_run,
13483 &use_range_stepping, _("\
13484Enable or disable range stepping."), _("\
13485Show whether target-assisted range stepping is enabled."), _("\
13486If on, and the target supports it, when stepping a source line, GDB\n\
13487tells the target to step the corresponding range of addresses itself instead\n\
13488of issuing multiple single-steps. This speeds up source level\n\
13489stepping. If off, GDB always issues single-steps, even if range\n\
13490stepping is supported by the target. The default is on."),
13491 set_range_stepping,
13492 show_range_stepping,
13493 &setlist,
13494 &showlist);
13495
449092f6
CV
13496 /* Eventually initialize fileio. See fileio.c */
13497 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 13498
ba348170 13499 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 13500 special ptids with it set to != 0. */
ba348170
PA
13501 magic_null_ptid = ptid_build (42000, -1, 1);
13502 not_sent_ptid = ptid_build (42000, -2, 1);
13503 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
13504
13505 target_buf_size = 2048;
13506 target_buf = xmalloc (target_buf_size);
c906108c 13507}
10760264 13508
This page took 3.091952 seconds and 4 git commands to generate.