]> Git Repo - binutils.git/blame - gdb/remote.c
Comment and whitespace changes
[binutils.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"
c906108c 47
9846de1b 48#include <sys/time.h>
c906108c 49
43ff13b4 50#include "event-loop.h"
c2c6d25f 51#include "event-top.h"
2acceee2 52#include "inf-loop.h"
43ff13b4 53
c906108c
SS
54#include <signal.h>
55#include "serial.h"
56
6240bebf
MS
57#include "gdbcore.h" /* for exec_bfd */
58
449092f6 59#include "remote-fileio.h"
a6b151f1 60#include "gdb/fileio.h"
53ce3c39 61#include <sys/stat.h>
dc146f7c 62#include "xml-support.h"
449092f6 63
fd79ecee
DJ
64#include "memory-map.h"
65
35b1e5cc
SS
66#include "tracepoint.h"
67#include "ax.h"
68#include "ax-gdb.h"
d1feda86 69#include "agent.h"
9accd112 70#include "btrace.h"
35b1e5cc 71
0df8b418 72/* Temp hacks for tracepoint encoding migration. */
35b1e5cc
SS
73static char *target_buf;
74static long target_buf_size;
35b1e5cc 75
6765f3e5
DJ
76/* The size to align memory write packets, when practical. The protocol
77 does not guarantee any alignment, and gdb will generate short
78 writes and unaligned writes, but even as a best-effort attempt this
79 can improve bulk transfers. For instance, if a write is misaligned
80 relative to the target's data bus, the stub may need to make an extra
81 round trip fetching data from the target. This doesn't make a
82 huge difference, but it's easy to do, so we try to be helpful.
83
84 The alignment chosen is arbitrary; usually data bus width is
85 important here, not the possibly larger cache line size. */
86enum { REMOTE_ALIGN_WRITES = 16 };
87
23860348 88/* Prototypes for local functions. */
934b9bac 89static void async_cleanup_sigint_signal_handler (void *dummy);
6d820c5c 90static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 91static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 92 int forever, int *is_notif);
6426a772 93
934b9bac
JK
94static void async_handle_remote_sigint (int);
95static void async_handle_remote_sigint_twice (int);
43ff13b4 96
a14ed312 97static void remote_files_info (struct target_ops *ignore);
c906108c 98
f32dbf8c
MM
99static void remote_prepare_to_store (struct target_ops *self,
100 struct regcache *regcache);
c906108c 101
014f9477
TT
102static void remote_open_1 (const char *, int, struct target_ops *,
103 int extended_p);
c906108c 104
de90e03d 105static void remote_close (struct target_ops *self);
c906108c 106
cbb8991c
DB
107struct remote_state;
108
109static int remote_vkill (int pid, struct remote_state *rs);
110
136d6dae 111static void remote_mourn (struct target_ops *ops);
c906108c 112
a14ed312 113static void extended_remote_restart (void);
c906108c 114
136d6dae 115static void extended_remote_mourn (struct target_ops *);
c906108c 116
6d820c5c 117static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 118
a14ed312 119static int readchar (int timeout);
c906108c 120
c33e31fd
PA
121static void remote_serial_write (const char *str, int len);
122
7d85a9c0 123static void remote_kill (struct target_ops *ops);
c906108c 124
6a109b6b 125static int remote_can_async_p (struct target_ops *);
75c99385 126
6a109b6b 127static int remote_is_async_p (struct target_ops *);
75c99385 128
6a3753b3 129static void remote_async (struct target_ops *ops, int enable);
75c99385 130
934b9bac 131static void sync_remote_interrupt_twice (int signo);
7a292a7a 132
a14ed312 133static void interrupt_query (void);
c906108c 134
79d7f229
PA
135static void set_general_thread (struct ptid ptid);
136static void set_continue_thread (struct ptid ptid);
c906108c 137
a14ed312 138static void get_offsets (void);
c906108c 139
6d820c5c
DJ
140static void skip_frame (void);
141
142static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 143
a14ed312 144static int hexnumlen (ULONGEST num);
c906108c 145
a14ed312 146static void init_remote_ops (void);
c906108c 147
a14ed312 148static void init_extended_remote_ops (void);
c906108c 149
1eab8a48 150static void remote_stop (struct target_ops *self, ptid_t);
c906108c 151
a14ed312 152static int stubhex (int ch);
c906108c 153
a14ed312 154static int hexnumstr (char *, ULONGEST);
c906108c 155
a14ed312 156static int hexnumnstr (char *, ULONGEST, int);
2df3850c 157
a14ed312 158static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 159
baa336ce 160static void print_packet (const char *);
c906108c 161
a14ed312 162static void compare_sections_command (char *, int);
c906108c 163
a14ed312 164static void packet_command (char *, int);
c906108c 165
a14ed312 166static int stub_unpack_int (char *buff, int fieldlength);
c906108c 167
39f77062 168static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 169
baa336ce 170static int putpkt_binary (const char *buf, int cnt);
c906108c 171
a14ed312 172static void check_binary_download (CORE_ADDR addr);
c906108c 173
5a2468f5 174struct packet_config;
5a2468f5 175
a14ed312 176static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 177
bb572ddd
DJ
178static void show_remote_protocol_packet_cmd (struct ui_file *file,
179 int from_tty,
180 struct cmd_list_element *c,
181 const char *value);
182
82f73884
PA
183static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
184static ptid_t read_ptid (char *buf, char **obuf);
185
c378d69d 186static void remote_set_permissions (struct target_ops *self);
d914c394 187
8bd200f1
TT
188static int remote_get_trace_status (struct target_ops *self,
189 struct trace_status *ts);
d5551862 190
ab6617cc
TT
191static int remote_upload_tracepoints (struct target_ops *self,
192 struct uploaded_tp **utpp);
00bf0b85 193
181e3713
TT
194static int remote_upload_trace_state_variables (struct target_ops *self,
195 struct uploaded_tsv **utsvp);
00bf0b85 196
c8d104ad
PA
197static void remote_query_supported (void);
198
36d25514 199static void remote_check_symbols (void);
c8d104ad 200
a14ed312 201void _initialize_remote (void);
c906108c 202
74531fed 203struct stop_reply;
74531fed 204static void stop_reply_xfree (struct stop_reply *);
722247f1 205static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 206static void push_stop_reply (struct stop_reply *);
bcc75809 207static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
208static int peek_stop_reply (ptid_t ptid);
209
cbb8991c
DB
210struct threads_listing_context;
211static void remove_new_fork_children (struct threads_listing_context *);
212
74531fed 213static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 214
e3594fd1 215static void remote_terminal_ours (struct target_ops *self);
d3fd5342 216
d962ef82
DJ
217static int remote_read_description_p (struct target_ops *target);
218
176a6961 219static void remote_console_output (char *msg);
dde08ee1 220
efcc2da7 221static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 222
78eff0ec 223static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 224
f4abbc16
MM
225static void remote_btrace_reset (void);
226
a6b151f1
DJ
227/* For "remote". */
228
229static struct cmd_list_element *remote_cmdlist;
230
bb572ddd
DJ
231/* For "set remote" and "show remote". */
232
233static struct cmd_list_element *remote_set_cmdlist;
234static struct cmd_list_element *remote_show_cmdlist;
235
d458bd84
PA
236/* Stub vCont actions support.
237
238 Each field is a boolean flag indicating whether the stub reports
239 support for the corresponding action. */
240
241struct vCont_action_support
242{
243 /* vCont;t */
244 int t;
c1e36e3e
PA
245
246 /* vCont;r */
247 int r;
d458bd84
PA
248};
249
c1e36e3e
PA
250/* Controls whether GDB is willing to use range stepping. */
251
252static int use_range_stepping = 1;
253
0d031856
TT
254#define OPAQUETHREADBYTES 8
255
256/* a 64 bit opaque identifier */
257typedef unsigned char threadref[OPAQUETHREADBYTES];
258
259/* About this many threadisds fit in a packet. */
260
261#define MAXTHREADLISTRESULTS 32
262
ea9c271d
DJ
263/* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
266
267struct remote_state
268{
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
274 packets. */
275 char *buf;
276 long buf_size;
be2a5f71 277
1e51243a
PA
278 /* True if we're going through initial connection setup (finding out
279 about the remote side's threads, relocating symbols, etc.). */
280 int starting_up;
281
be2a5f71
DJ
282 /* If we negotiated packet size explicitly (and thus can bypass
283 heuristics for the largest packet size that will not overflow
284 a buffer in the stub), this will be set to that packet size.
285 Otherwise zero, meaning to use the guessed size. */
286 long explicit_packet_size;
2d717e4f
DJ
287
288 /* remote_wait is normally called when the target is running and
289 waits for a stop reply packet. But sometimes we need to call it
290 when the target is already stopped. We can send a "?" packet
291 and have remote_wait read the response. Or, if we already have
292 the response, we can stash it in BUF and tell remote_wait to
293 skip calling getpkt. This flag is set when BUF contains a
294 stop reply packet and the target is not waiting. */
295 int cached_wait_status;
a6f3e723
SL
296
297 /* True, if in no ack mode. That is, neither GDB nor the stub will
298 expect acks from each other. The connection is assumed to be
299 reliable. */
300 int noack_mode;
82f73884
PA
301
302 /* True if we're connected in extended remote mode. */
303 int extended;
304
e24a49d8
PA
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply;
74531fed 312
d458bd84
PA
313 /* The status of the stub support for the various vCont actions. */
314 struct vCont_action_support supports_vCont;
782b2b07 315
3a29589a
DJ
316 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
317 responded to that. */
318 int ctrlc_pending_p;
5d93a237
TT
319
320 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
321 remote_open knows that we don't have a file open when the program
322 starts. */
323 struct serial *remote_desc;
47f8a51d
TT
324
325 /* These are the threads which we last sent to the remote system. The
326 TID member will be -1 for all or -2 for not sent yet. */
327 ptid_t general_thread;
328 ptid_t continue_thread;
262e1174
TT
329
330 /* This is the traceframe which we last selected on the remote system.
331 It will be -1 if no traceframe is selected. */
332 int remote_traceframe_number;
747dc59d
TT
333
334 char *last_pass_packet;
5e4a05c4
TT
335
336 /* The last QProgramSignals packet sent to the target. We bypass
337 sending a new program signals list down to the target if the new
338 packet is exactly the same as the last we sent. IOW, we only let
339 the target know about program signals list changes. */
340 char *last_program_signals_packet;
b73be471
TT
341
342 enum gdb_signal last_sent_signal;
280ceea3
TT
343
344 int last_sent_step;
8e88304f
TT
345
346 char *finished_object;
347 char *finished_annex;
348 ULONGEST finished_offset;
b80fafe3
TT
349
350 /* Should we try the 'ThreadInfo' query packet?
351
352 This variable (NOT available to the user: auto-detect only!)
353 determines whether GDB will use the new, simpler "ThreadInfo"
354 query or the older, more complex syntax for thread queries.
355 This is an auto-detect variable (set to true at each connect,
356 and set to false when the target fails to recognize it). */
357 int use_threadinfo_query;
358 int use_threadextra_query;
88b496c3 359
ee154bee
TT
360 /* This is set to the data address of the access causing the target
361 to stop for a watchpoint. */
362 CORE_ADDR remote_watch_data_address;
363
f7e6eed5
PA
364 /* Whether the target stopped for a breakpoint/watchpoint. */
365 enum target_stop_reason stop_reason;
0d031856
TT
366
367 threadref echo_nextthread;
368 threadref nextthread;
369 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
370
371 /* The state of remote notification. */
372 struct remote_notif_state *notif_state;
f4abbc16
MM
373
374 /* The branch trace configuration. */
375 struct btrace_config btrace_config;
ea9c271d
DJ
376};
377
dc146f7c
VP
378/* Private data that we'll store in (struct thread_info)->private. */
379struct private_thread_info
380{
381 char *extra;
382 int core;
383};
384
385static void
386free_private_thread_info (struct private_thread_info *info)
387{
388 xfree (info->extra);
389 xfree (info);
390}
391
ea9c271d
DJ
392/* This data could be associated with a target, but we do not always
393 have access to the current target when we need it, so for now it is
394 static. This will be fine for as long as only one target is in use
395 at a time. */
cf792862 396static struct remote_state *remote_state;
ea9c271d
DJ
397
398static struct remote_state *
0b83947e 399get_remote_state_raw (void)
ea9c271d 400{
cf792862
TT
401 return remote_state;
402}
403
404/* Allocate a new struct remote_state with xmalloc, initialize it, and
405 return it. */
406
407static struct remote_state *
408new_remote_state (void)
409{
410 struct remote_state *result = XCNEW (struct remote_state);
411
412 /* The default buffer size is unimportant; it will be expanded
413 whenever a larger buffer is needed. */
414 result->buf_size = 400;
415 result->buf = xmalloc (result->buf_size);
262e1174 416 result->remote_traceframe_number = -1;
b73be471 417 result->last_sent_signal = GDB_SIGNAL_0;
cf792862
TT
418
419 return result;
ea9c271d
DJ
420}
421
422/* Description of the remote protocol for a given architecture. */
d01949b6 423
ad10f812
AC
424struct packet_reg
425{
426 long offset; /* Offset into G packet. */
427 long regnum; /* GDB's internal register number. */
428 LONGEST pnum; /* Remote protocol register number. */
b323314b 429 int in_g_packet; /* Always part of G packet. */
f5656ead 430 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 431 at present. */
f5656ead 432 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 433 at present. */
ad10f812
AC
434};
435
ea9c271d 436struct remote_arch_state
d01949b6 437{
ad10f812
AC
438 /* Description of the remote protocol registers. */
439 long sizeof_g_packet;
b323314b
AC
440
441 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 442 (making an array gdbarch_num_regs in size). */
b323314b 443 struct packet_reg *regs;
ad10f812 444
d01949b6
AC
445 /* This is the size (in chars) of the first response to the ``g''
446 packet. It is used as a heuristic when determining the maximum
447 size of memory-read and memory-write packets. A target will
448 typically only reserve a buffer large enough to hold the ``g''
449 packet. The size does not include packet overhead (headers and
23860348 450 trailers). */
d01949b6
AC
451 long actual_register_packet_size;
452
453 /* This is the maximum size (in chars) of a non read/write packet.
23860348 454 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
455 long remote_packet_size;
456};
457
35b1e5cc
SS
458/* Utility: generate error from an incoming stub packet. */
459static void
460trace_error (char *buf)
461{
462 if (*buf++ != 'E')
463 return; /* not an error msg */
464 switch (*buf)
465 {
466 case '1': /* malformed packet error */
467 if (*++buf == '0') /* general case: */
468 error (_("remote.c: error in outgoing packet."));
469 else
470 error (_("remote.c: error in outgoing packet at field #%ld."),
471 strtol (buf, NULL, 16));
35b1e5cc
SS
472 default:
473 error (_("Target returns error code '%s'."), buf);
474 }
475}
476
477/* Utility: wait for reply from stub, while accepting "O" packets. */
478static char *
479remote_get_noisy_reply (char **buf_p,
480 long *sizeof_buf)
481{
482 do /* Loop on reply from remote stub. */
483 {
484 char *buf;
a744cf53 485
0df8b418 486 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
487 getpkt (buf_p, sizeof_buf, 0);
488 buf = *buf_p;
ad91cd99 489 if (buf[0] == 'E')
35b1e5cc 490 trace_error (buf);
61012eef 491 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
492 {
493 ULONGEST ul;
494 CORE_ADDR from, to, org_to;
495 char *p, *pp;
496 int adjusted_size = 0;
7556d4a4 497 int relocated = 0;
dde08ee1
PA
498
499 p = buf + strlen ("qRelocInsn:");
500 pp = unpack_varlen_hex (p, &ul);
501 if (*pp != ';')
cb91c06a 502 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
503 from = ul;
504
505 p = pp + 1;
a9cbf802 506 unpack_varlen_hex (p, &ul);
dde08ee1
PA
507 to = ul;
508
509 org_to = to;
510
492d29ea 511 TRY
dde08ee1 512 {
f5656ead 513 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 514 relocated = 1;
dde08ee1 515 }
492d29ea 516 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
517 {
518 if (ex.error == MEMORY_ERROR)
519 {
520 /* Propagate memory errors silently back to the
521 target. The stub may have limited the range of
522 addresses we can write to, for example. */
523 }
524 else
525 {
526 /* Something unexpectedly bad happened. Be verbose
527 so we can tell what, and propagate the error back
528 to the stub, so it doesn't get stuck waiting for
529 a response. */
530 exception_fprintf (gdb_stderr, ex,
531 _("warning: relocating instruction: "));
532 }
533 putpkt ("E01");
534 }
492d29ea 535 END_CATCH
7556d4a4
PA
536
537 if (relocated)
dde08ee1
PA
538 {
539 adjusted_size = to - org_to;
540
bba74b36 541 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
542 putpkt (buf);
543 }
dde08ee1 544 }
ad91cd99 545 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
546 remote_console_output (buf + 1); /* 'O' message from stub */
547 else
0df8b418 548 return buf; /* Here's the actual reply. */
35b1e5cc
SS
549 }
550 while (1);
551}
3c3bea1c 552
d01949b6
AC
553/* Handle for retreving the remote protocol data from gdbarch. */
554static struct gdbarch_data *remote_gdbarch_data_handle;
555
ea9c271d
DJ
556static struct remote_arch_state *
557get_remote_arch_state (void)
d01949b6 558{
f5656ead 559 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
560}
561
0b83947e
DJ
562/* Fetch the global remote target state. */
563
564static struct remote_state *
565get_remote_state (void)
566{
567 /* Make sure that the remote architecture state has been
568 initialized, because doing so might reallocate rs->buf. Any
569 function which calls getpkt also needs to be mindful of changes
570 to rs->buf, but this call limits the number of places which run
571 into trouble. */
572 get_remote_arch_state ();
573
574 return get_remote_state_raw ();
575}
576
74ca34ce
DJ
577static int
578compare_pnums (const void *lhs_, const void *rhs_)
579{
580 const struct packet_reg * const *lhs = lhs_;
581 const struct packet_reg * const *rhs = rhs_;
582
583 if ((*lhs)->pnum < (*rhs)->pnum)
584 return -1;
585 else if ((*lhs)->pnum == (*rhs)->pnum)
586 return 0;
587 else
588 return 1;
589}
590
c21236dc
PA
591static int
592map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 593{
74ca34ce 594 int regnum, num_remote_regs, offset;
74ca34ce 595 struct packet_reg **remote_regs;
ea9c271d 596
4a22f64d 597 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 598 {
c21236dc 599 struct packet_reg *r = &regs[regnum];
baef701f 600
4a22f64d 601 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
602 /* Do not try to fetch zero-sized (placeholder) registers. */
603 r->pnum = -1;
604 else
605 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
606
b323314b 607 r->regnum = regnum;
74ca34ce
DJ
608 }
609
610 /* Define the g/G packet format as the contents of each register
611 with a remote protocol number, in order of ascending protocol
612 number. */
613
4a22f64d 614 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 615 * sizeof (struct packet_reg *));
f57d151a 616 for (num_remote_regs = 0, regnum = 0;
4a22f64d 617 regnum < gdbarch_num_regs (gdbarch);
f57d151a 618 regnum++)
c21236dc
PA
619 if (regs[regnum].pnum != -1)
620 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 621
74ca34ce
DJ
622 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
623 compare_pnums);
624
625 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
626 {
627 remote_regs[regnum]->in_g_packet = 1;
628 remote_regs[regnum]->offset = offset;
4a22f64d 629 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
630 }
631
c21236dc
PA
632 return offset;
633}
634
635/* Given the architecture described by GDBARCH, return the remote
636 protocol register's number and the register's offset in the g/G
637 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
638 If the target does not have a mapping for REGNUM, return false,
639 otherwise, return true. */
640
641int
642remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
643 int *pnum, int *poffset)
644{
645 int sizeof_g_packet;
646 struct packet_reg *regs;
647 struct cleanup *old_chain;
648
649 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
650
651 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
652 old_chain = make_cleanup (xfree, regs);
653
654 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
655
656 *pnum = regs[regnum].pnum;
657 *poffset = regs[regnum].offset;
658
659 do_cleanups (old_chain);
660
661 return *pnum != -1;
662}
663
664static void *
665init_remote_state (struct gdbarch *gdbarch)
666{
667 struct remote_state *rs = get_remote_state_raw ();
668 struct remote_arch_state *rsa;
669
670 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
671
672 /* Use the architecture to build a regnum<->pnum table, which will be
673 1:1 unless a feature set specifies otherwise. */
674 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
675 gdbarch_num_regs (gdbarch),
676 struct packet_reg);
677
74ca34ce
DJ
678 /* Record the maximum possible size of the g packet - it may turn out
679 to be smaller. */
c21236dc 680 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 681
0df8b418 682 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
683 remote stubs have a hardwired buffer size of 400 bytes
684 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
685 as the maximum packet-size to ensure that the packet and an extra
686 NUL character can always fit in the buffer. This stops GDB
687 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
688 already a full buffer (As of 1999-12-04 that was most stubs). */
689 rsa->remote_packet_size = 400 - 1;
d01949b6 690
ea9c271d
DJ
691 /* This one is filled in when a ``g'' packet is received. */
692 rsa->actual_register_packet_size = 0;
693
694 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
695 default, adjust the size accordingly. Remember that each byte is
696 encoded as two characters. 32 is the overhead for the packet
697 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 698 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 699 little. */
ea9c271d
DJ
700 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
701 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 702
ea9c271d
DJ
703 /* Make sure that the packet buffer is plenty big enough for
704 this architecture. */
705 if (rs->buf_size < rsa->remote_packet_size)
706 {
707 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 708 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 709 }
6d820c5c 710
ea9c271d
DJ
711 return rsa;
712}
713
714/* Return the current allowed size of a remote packet. This is
715 inferred from the current architecture, and should be used to
716 limit the length of outgoing packets. */
717static long
718get_remote_packet_size (void)
719{
be2a5f71 720 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
721 struct remote_arch_state *rsa = get_remote_arch_state ();
722
be2a5f71
DJ
723 if (rs->explicit_packet_size)
724 return rs->explicit_packet_size;
725
ea9c271d 726 return rsa->remote_packet_size;
d01949b6
AC
727}
728
ad10f812 729static struct packet_reg *
ea9c271d 730packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 731{
f5656ead 732 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
733 return NULL;
734 else
ad10f812 735 {
ea9c271d 736 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 737
b323314b
AC
738 gdb_assert (r->regnum == regnum);
739 return r;
ad10f812 740 }
ad10f812
AC
741}
742
743static struct packet_reg *
ea9c271d 744packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 745{
b323314b 746 int i;
a744cf53 747
f5656ead 748 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 749 {
ea9c271d 750 struct packet_reg *r = &rsa->regs[i];
a744cf53 751
b323314b
AC
752 if (r->pnum == pnum)
753 return r;
ad10f812
AC
754 }
755 return NULL;
d01949b6
AC
756}
757
c906108c
SS
758static struct target_ops remote_ops;
759
760static struct target_ops extended_remote_ops;
761
6426a772
JM
762/* FIXME: cagney/1999-09-23: Even though getpkt was called with
763 ``forever'' still use the normal timeout mechanism. This is
764 currently used by the ASYNC code to guarentee that target reads
765 during the initial connect always time-out. Once getpkt has been
766 modified to return a timeout indication and, in turn
767 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 768 this can go away. */
6426a772
JM
769static int wait_forever_enabled_p = 1;
770
9a7071a8
JB
771/* Allow the user to specify what sequence to send to the remote
772 when he requests a program interruption: Although ^C is usually
773 what remote systems expect (this is the default, here), it is
774 sometimes preferable to send a break. On other systems such
775 as the Linux kernel, a break followed by g, which is Magic SysRq g
776 is required in order to interrupt the execution. */
777const char interrupt_sequence_control_c[] = "Ctrl-C";
778const char interrupt_sequence_break[] = "BREAK";
779const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 780static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
781 {
782 interrupt_sequence_control_c,
783 interrupt_sequence_break,
784 interrupt_sequence_break_g,
785 NULL
786 };
787static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
788
789static void
790show_interrupt_sequence (struct ui_file *file, int from_tty,
791 struct cmd_list_element *c,
792 const char *value)
793{
794 if (interrupt_sequence_mode == interrupt_sequence_control_c)
795 fprintf_filtered (file,
796 _("Send the ASCII ETX character (Ctrl-c) "
797 "to the remote target to interrupt the "
798 "execution of the program.\n"));
799 else if (interrupt_sequence_mode == interrupt_sequence_break)
800 fprintf_filtered (file,
801 _("send a break signal to the remote target "
802 "to interrupt the execution of the program.\n"));
803 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
804 fprintf_filtered (file,
805 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
806 "the remote target to interrupt the execution "
807 "of Linux kernel.\n"));
808 else
809 internal_error (__FILE__, __LINE__,
810 _("Invalid value for interrupt_sequence_mode: %s."),
811 interrupt_sequence_mode);
812}
6426a772 813
9a7071a8
JB
814/* This boolean variable specifies whether interrupt_sequence is sent
815 to the remote target when gdb connects to it.
816 This is mostly needed when you debug the Linux kernel: The Linux kernel
817 expects BREAK g which is Magic SysRq g for connecting gdb. */
818static int interrupt_on_connect = 0;
c906108c 819
9a7071a8
JB
820/* This variable is used to implement the "set/show remotebreak" commands.
821 Since these commands are now deprecated in favor of "set/show remote
822 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
823static int remote_break;
824
9a7071a8
JB
825static void
826set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
827{
828 if (remote_break)
829 interrupt_sequence_mode = interrupt_sequence_break;
830 else
831 interrupt_sequence_mode = interrupt_sequence_control_c;
832}
833
834static void
835show_remotebreak (struct ui_file *file, int from_tty,
836 struct cmd_list_element *c,
837 const char *value)
838{
839}
840
c906108c
SS
841/* This variable sets the number of bits in an address that are to be
842 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 843 leading zeros, the entire address would be sent. This variable
c906108c
SS
844 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
845 initial implementation of remote.c restricted the address sent in
846 memory packets to ``host::sizeof long'' bytes - (typically 32
847 bits). Consequently, for 64 bit targets, the upper 32 bits of an
848 address was never sent. Since fixing this bug may cause a break in
849 some remote targets this variable is principly provided to
23860348 850 facilitate backward compatibility. */
c906108c 851
883b9c6c 852static unsigned int remote_address_size;
c906108c 853
75c99385
PA
854/* Temporary to track who currently owns the terminal. See
855 remote_terminal_* for more details. */
6426a772
JM
856
857static int remote_async_terminal_ours_p;
858
2d717e4f
DJ
859/* The executable file to use for "run" on the remote side. */
860
861static char *remote_exec_file = "";
862
11cf8741 863\f
11cf8741 864/* User configurable variables for the number of characters in a
ea9c271d
DJ
865 memory read/write packet. MIN (rsa->remote_packet_size,
866 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 867 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
868 (speed up transfers). The variables ``preferred_*'' (the user
869 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 870 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
871
872struct memory_packet_config
873{
874 char *name;
875 long size;
876 int fixed_p;
877};
878
879/* Compute the current size of a read/write packet. Since this makes
880 use of ``actual_register_packet_size'' the computation is dynamic. */
881
882static long
883get_memory_packet_size (struct memory_packet_config *config)
884{
d01949b6 885 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
886 struct remote_arch_state *rsa = get_remote_arch_state ();
887
11cf8741
JM
888 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
889 law?) that some hosts don't cope very well with large alloca()
890 calls. Eventually the alloca() code will be replaced by calls to
891 xmalloc() and make_cleanups() allowing this restriction to either
23860348 892 be lifted or removed. */
11cf8741
JM
893#ifndef MAX_REMOTE_PACKET_SIZE
894#define MAX_REMOTE_PACKET_SIZE 16384
895#endif
3de11b2e 896 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 897#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 898#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
899#endif
900 long what_they_get;
901 if (config->fixed_p)
902 {
903 if (config->size <= 0)
904 what_they_get = MAX_REMOTE_PACKET_SIZE;
905 else
906 what_they_get = config->size;
907 }
908 else
909 {
ea9c271d 910 what_they_get = get_remote_packet_size ();
23860348 911 /* Limit the packet to the size specified by the user. */
11cf8741
JM
912 if (config->size > 0
913 && what_they_get > config->size)
914 what_they_get = config->size;
be2a5f71
DJ
915
916 /* Limit it to the size of the targets ``g'' response unless we have
917 permission from the stub to use a larger packet size. */
918 if (rs->explicit_packet_size == 0
919 && rsa->actual_register_packet_size > 0
920 && what_they_get > rsa->actual_register_packet_size)
921 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
922 }
923 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
924 what_they_get = MAX_REMOTE_PACKET_SIZE;
925 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
926 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
927
928 /* Make sure there is room in the global buffer for this packet
929 (including its trailing NUL byte). */
930 if (rs->buf_size < what_they_get + 1)
931 {
932 rs->buf_size = 2 * what_they_get;
933 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
934 }
935
11cf8741
JM
936 return what_they_get;
937}
938
0df8b418 939/* Update the size of a read/write packet. If they user wants
23860348 940 something really big then do a sanity check. */
11cf8741
JM
941
942static void
943set_memory_packet_size (char *args, struct memory_packet_config *config)
944{
945 int fixed_p = config->fixed_p;
946 long size = config->size;
a744cf53 947
11cf8741 948 if (args == NULL)
8a3fe4f8 949 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
950 else if (strcmp (args, "hard") == 0
951 || strcmp (args, "fixed") == 0)
952 fixed_p = 1;
953 else if (strcmp (args, "soft") == 0
954 || strcmp (args, "limit") == 0)
955 fixed_p = 0;
956 else
957 {
958 char *end;
a744cf53 959
11cf8741
JM
960 size = strtoul (args, &end, 0);
961 if (args == end)
8a3fe4f8 962 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
963#if 0
964 /* Instead of explicitly capping the size of a packet to
965 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
966 instead allowed to set the size to something arbitrarily
23860348 967 large. */
11cf8741 968 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 969 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
970#endif
971 }
23860348 972 /* Extra checks? */
11cf8741
JM
973 if (fixed_p && !config->fixed_p)
974 {
e2e0b3e5
AC
975 if (! query (_("The target may not be able to correctly handle a %s\n"
976 "of %ld bytes. Change the packet size? "),
11cf8741 977 config->name, size))
8a3fe4f8 978 error (_("Packet size not changed."));
11cf8741 979 }
23860348 980 /* Update the config. */
11cf8741
JM
981 config->fixed_p = fixed_p;
982 config->size = size;
983}
984
985static void
986show_memory_packet_size (struct memory_packet_config *config)
987{
a3f17187 988 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 989 if (config->fixed_p)
a3f17187 990 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
991 get_memory_packet_size (config));
992 else
a3f17187 993 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
994 get_memory_packet_size (config));
995}
996
997static struct memory_packet_config memory_write_packet_config =
998{
999 "memory-write-packet-size",
1000};
1001
1002static void
1003set_memory_write_packet_size (char *args, int from_tty)
1004{
1005 set_memory_packet_size (args, &memory_write_packet_config);
1006}
1007
1008static void
1009show_memory_write_packet_size (char *args, int from_tty)
1010{
1011 show_memory_packet_size (&memory_write_packet_config);
1012}
1013
1014static long
1015get_memory_write_packet_size (void)
1016{
1017 return get_memory_packet_size (&memory_write_packet_config);
1018}
1019
1020static struct memory_packet_config memory_read_packet_config =
1021{
1022 "memory-read-packet-size",
1023};
1024
1025static void
1026set_memory_read_packet_size (char *args, int from_tty)
1027{
1028 set_memory_packet_size (args, &memory_read_packet_config);
1029}
1030
1031static void
1032show_memory_read_packet_size (char *args, int from_tty)
1033{
1034 show_memory_packet_size (&memory_read_packet_config);
1035}
1036
1037static long
1038get_memory_read_packet_size (void)
1039{
1040 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1041
11cf8741
JM
1042 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1043 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1044 increased beyond this. */
1045 if (size > get_remote_packet_size ())
1046 size = get_remote_packet_size ();
11cf8741
JM
1047 return size;
1048}
1049
11cf8741 1050\f
5a2468f5 1051/* Generic configuration support for packets the stub optionally
0df8b418 1052 supports. Allows the user to specify the use of the packet as well
23860348 1053 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1054
1055enum packet_support
1056 {
1057 PACKET_SUPPORT_UNKNOWN = 0,
1058 PACKET_ENABLE,
1059 PACKET_DISABLE
1060 };
1061
5a2468f5
JM
1062struct packet_config
1063 {
bb572ddd
DJ
1064 const char *name;
1065 const char *title;
4082afcc
PA
1066
1067 /* If auto, GDB auto-detects support for this packet or feature,
1068 either through qSupported, or by trying the packet and looking
1069 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1070 packet. If false, the packet is disabled. Configs that don't
1071 have an associated command always have this set to auto. */
7f19b9a2 1072 enum auto_boolean detect;
4082afcc
PA
1073
1074 /* Does the target support this packet? */
5a2468f5
JM
1075 enum packet_support support;
1076 };
1077
d471ea57 1078/* Analyze a packet's return value and update the packet config
23860348 1079 accordingly. */
d471ea57
AC
1080
1081enum packet_result
1082{
1083 PACKET_ERROR,
1084 PACKET_OK,
1085 PACKET_UNKNOWN
1086};
1087
4082afcc
PA
1088static enum packet_support packet_config_support (struct packet_config *config);
1089static enum packet_support packet_support (int packet);
5a2468f5
JM
1090
1091static void
fba45db2 1092show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1093{
1094 char *support = "internal-error";
a744cf53 1095
4082afcc 1096 switch (packet_config_support (config))
5a2468f5
JM
1097 {
1098 case PACKET_ENABLE:
1099 support = "enabled";
1100 break;
1101 case PACKET_DISABLE:
1102 support = "disabled";
1103 break;
1104 case PACKET_SUPPORT_UNKNOWN:
1105 support = "unknown";
1106 break;
1107 }
1108 switch (config->detect)
1109 {
7f19b9a2 1110 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1111 printf_filtered (_("Support for the `%s' packet "
1112 "is auto-detected, currently %s.\n"),
37a105a1 1113 config->name, support);
5a2468f5 1114 break;
7f19b9a2
AC
1115 case AUTO_BOOLEAN_TRUE:
1116 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1117 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1118 config->name, support);
8e248173 1119 break;
5a2468f5
JM
1120 }
1121}
1122
1123static void
bb572ddd
DJ
1124add_packet_config_cmd (struct packet_config *config, const char *name,
1125 const char *title, int legacy)
d471ea57 1126{
5a2468f5
JM
1127 char *set_doc;
1128 char *show_doc;
d471ea57 1129 char *cmd_name;
3ed07be4 1130
5a2468f5
JM
1131 config->name = name;
1132 config->title = title;
b435e160
AC
1133 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1134 name, title);
3e43a32a
MS
1135 show_doc = xstrprintf ("Show current use of remote "
1136 "protocol `%s' (%s) packet",
b435e160 1137 name, title);
d471ea57 1138 /* set/show TITLE-packet {auto,on,off} */
b435e160 1139 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1140 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1141 &config->detect, set_doc,
1142 show_doc, NULL, /* help_doc */
4082afcc 1143 NULL,
bb572ddd
DJ
1144 show_remote_protocol_packet_cmd,
1145 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1146 /* The command code copies the documentation strings. */
1147 xfree (set_doc);
1148 xfree (show_doc);
23860348 1149 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1150 if (legacy)
1151 {
1152 char *legacy_name;
a744cf53 1153
b435e160 1154 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1155 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1156 &remote_set_cmdlist);
d471ea57 1157 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1158 &remote_show_cmdlist);
d471ea57 1159 }
5a2468f5
JM
1160}
1161
d471ea57 1162static enum packet_result
a76d924d 1163packet_check_result (const char *buf)
5a2468f5 1164{
d471ea57 1165 if (buf[0] != '\0')
5a2468f5 1166 {
d471ea57 1167 /* The stub recognized the packet request. Check that the
23860348 1168 operation succeeded. */
a76d924d
DJ
1169 if (buf[0] == 'E'
1170 && isxdigit (buf[1]) && isxdigit (buf[2])
1171 && buf[3] == '\0')
1172 /* "Enn" - definitly an error. */
1173 return PACKET_ERROR;
1174
1175 /* Always treat "E." as an error. This will be used for
1176 more verbose error messages, such as E.memtypes. */
1177 if (buf[0] == 'E' && buf[1] == '.')
1178 return PACKET_ERROR;
1179
1180 /* The packet may or may not be OK. Just assume it is. */
1181 return PACKET_OK;
1182 }
1183 else
1184 /* The stub does not support the packet. */
1185 return PACKET_UNKNOWN;
1186}
1187
1188static enum packet_result
1189packet_ok (const char *buf, struct packet_config *config)
1190{
1191 enum packet_result result;
1192
4082afcc
PA
1193 if (config->detect != AUTO_BOOLEAN_TRUE
1194 && config->support == PACKET_DISABLE)
1195 internal_error (__FILE__, __LINE__,
1196 _("packet_ok: attempt to use a disabled packet"));
1197
a76d924d
DJ
1198 result = packet_check_result (buf);
1199 switch (result)
1200 {
1201 case PACKET_OK:
1202 case PACKET_ERROR:
1203 /* The stub recognized the packet request. */
4082afcc 1204 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1205 {
d471ea57
AC
1206 if (remote_debug)
1207 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1208 "Packet %s (%s) is supported\n",
1209 config->name, config->title);
d471ea57 1210 config->support = PACKET_ENABLE;
d471ea57 1211 }
a76d924d
DJ
1212 break;
1213 case PACKET_UNKNOWN:
23860348 1214 /* The stub does not support the packet. */
4082afcc
PA
1215 if (config->detect == AUTO_BOOLEAN_AUTO
1216 && config->support == PACKET_ENABLE)
d471ea57 1217 {
4082afcc
PA
1218 /* If the stub previously indicated that the packet was
1219 supported then there is a protocol error. */
1220 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1221 config->name, config->title);
1222 }
1223 else if (config->detect == AUTO_BOOLEAN_TRUE)
1224 {
1225 /* The user set it wrong. */
1226 error (_("Enabled packet %s (%s) not recognized by stub"),
1227 config->name, config->title);
d471ea57 1228 }
4082afcc
PA
1229
1230 if (remote_debug)
1231 fprintf_unfiltered (gdb_stdlog,
1232 "Packet %s (%s) is NOT supported\n",
1233 config->name, config->title);
1234 config->support = PACKET_DISABLE;
a76d924d 1235 break;
5a2468f5 1236 }
a76d924d
DJ
1237
1238 return result;
5a2468f5
JM
1239}
1240
444abaca
DJ
1241enum {
1242 PACKET_vCont = 0,
1243 PACKET_X,
1244 PACKET_qSymbol,
1245 PACKET_P,
1246 PACKET_p,
1247 PACKET_Z0,
1248 PACKET_Z1,
1249 PACKET_Z2,
1250 PACKET_Z3,
1251 PACKET_Z4,
a6b151f1
DJ
1252 PACKET_vFile_open,
1253 PACKET_vFile_pread,
1254 PACKET_vFile_pwrite,
1255 PACKET_vFile_close,
1256 PACKET_vFile_unlink,
b9e7b9c3 1257 PACKET_vFile_readlink,
0a93529c 1258 PACKET_vFile_fstat,
0876f84a 1259 PACKET_qXfer_auxv,
23181151 1260 PACKET_qXfer_features,
c78fa86a 1261 PACKET_qXfer_exec_file,
cfa9d6d9 1262 PACKET_qXfer_libraries,
2268b414 1263 PACKET_qXfer_libraries_svr4,
fd79ecee 1264 PACKET_qXfer_memory_map,
0e7f50da
UW
1265 PACKET_qXfer_spu_read,
1266 PACKET_qXfer_spu_write,
07e059b5 1267 PACKET_qXfer_osdata,
dc146f7c 1268 PACKET_qXfer_threads,
0fb4aa4b 1269 PACKET_qXfer_statictrace_read,
b3b9301e 1270 PACKET_qXfer_traceframe_info,
169081d0 1271 PACKET_qXfer_uib,
711e434b 1272 PACKET_qGetTIBAddr,
444abaca 1273 PACKET_qGetTLSAddr,
be2a5f71 1274 PACKET_qSupported,
bd3eecc3 1275 PACKET_qTStatus,
89be2091 1276 PACKET_QPassSignals,
9b224c5e 1277 PACKET_QProgramSignals,
936d2992 1278 PACKET_qCRC,
08388c79 1279 PACKET_qSearch_memory,
2d717e4f
DJ
1280 PACKET_vAttach,
1281 PACKET_vRun,
a6f3e723 1282 PACKET_QStartNoAckMode,
82f73884 1283 PACKET_vKill,
4aa995e1
PA
1284 PACKET_qXfer_siginfo_read,
1285 PACKET_qXfer_siginfo_write,
0b16c5cf 1286 PACKET_qAttached,
4082afcc
PA
1287
1288 /* Support for conditional tracepoints. */
782b2b07 1289 PACKET_ConditionalTracepoints,
4082afcc
PA
1290
1291 /* Support for target-side breakpoint conditions. */
3788aec7 1292 PACKET_ConditionalBreakpoints,
4082afcc
PA
1293
1294 /* Support for target-side breakpoint commands. */
d3ce09f5 1295 PACKET_BreakpointCommands,
4082afcc
PA
1296
1297 /* Support for fast tracepoints. */
7a697b8d 1298 PACKET_FastTracepoints,
4082afcc
PA
1299
1300 /* Support for static tracepoints. */
0fb4aa4b 1301 PACKET_StaticTracepoints,
4082afcc
PA
1302
1303 /* Support for installing tracepoints while a trace experiment is
1304 running. */
1e4d1764 1305 PACKET_InstallInTrace,
4082afcc 1306
40ab02ce
MS
1307 PACKET_bc,
1308 PACKET_bs,
409873ef 1309 PACKET_TracepointSource,
d914c394 1310 PACKET_QAllow,
78d85199 1311 PACKET_qXfer_fdpic,
03583c20 1312 PACKET_QDisableRandomization,
d1feda86 1313 PACKET_QAgent,
f6f899bf 1314 PACKET_QTBuffer_size,
9accd112
MM
1315 PACKET_Qbtrace_off,
1316 PACKET_Qbtrace_bts,
1317 PACKET_qXfer_btrace,
4082afcc
PA
1318
1319 /* Support for the QNonStop packet. */
1320 PACKET_QNonStop,
1321
1322 /* Support for multi-process extensions. */
1323 PACKET_multiprocess_feature,
1324
1325 /* Support for enabling and disabling tracepoints while a trace
1326 experiment is running. */
1327 PACKET_EnableDisableTracepoints_feature,
1328
1329 /* Support for collecting strings using the tracenz bytecode. */
1330 PACKET_tracenz_feature,
1331
1332 /* Support for continuing to run a trace experiment while GDB is
1333 disconnected. */
1334 PACKET_DisconnectedTracing_feature,
1335
1336 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1337 PACKET_augmented_libraries_svr4_read_feature,
1338
f4abbc16
MM
1339 /* Support for the qXfer:btrace-conf:read packet. */
1340 PACKET_qXfer_btrace_conf,
1341
d33501a5
MM
1342 /* Support for the Qbtrace-conf:bts:size packet. */
1343 PACKET_Qbtrace_conf_bts_size,
1344
f7e6eed5
PA
1345 /* Support for swbreak+ feature. */
1346 PACKET_swbreak_feature,
1347
1348 /* Support for hwbreak+ feature. */
1349 PACKET_hwbreak_feature,
1350
89245bc0
DB
1351 /* Support for fork events. */
1352 PACKET_fork_event_feature,
1353
1354 /* Support for vfork events. */
1355 PACKET_vfork_event_feature,
1356
444abaca
DJ
1357 PACKET_MAX
1358};
506fb367 1359
444abaca 1360static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1361
f7e6eed5
PA
1362/* Returns the packet's corresponding "set remote foo-packet" command
1363 state. See struct packet_config for more details. */
1364
1365static enum auto_boolean
1366packet_set_cmd_state (int packet)
1367{
1368 return remote_protocol_packets[packet].detect;
1369}
1370
4082afcc
PA
1371/* Returns whether a given packet or feature is supported. This takes
1372 into account the state of the corresponding "set remote foo-packet"
1373 command, which may be used to bypass auto-detection. */
dc8acb97 1374
4082afcc
PA
1375static enum packet_support
1376packet_config_support (struct packet_config *config)
1377{
1378 switch (config->detect)
444abaca 1379 {
4082afcc
PA
1380 case AUTO_BOOLEAN_TRUE:
1381 return PACKET_ENABLE;
1382 case AUTO_BOOLEAN_FALSE:
1383 return PACKET_DISABLE;
1384 case AUTO_BOOLEAN_AUTO:
1385 return config->support;
1386 default:
1387 gdb_assert_not_reached (_("bad switch"));
444abaca 1388 }
4082afcc
PA
1389}
1390
1391/* Same as packet_config_support, but takes the packet's enum value as
1392 argument. */
1393
1394static enum packet_support
1395packet_support (int packet)
1396{
1397 struct packet_config *config = &remote_protocol_packets[packet];
1398
1399 return packet_config_support (config);
dc8acb97
MS
1400}
1401
5a2468f5 1402static void
444abaca
DJ
1403show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1404 struct cmd_list_element *c,
1405 const char *value)
5a2468f5 1406{
444abaca 1407 struct packet_config *packet;
5a2468f5 1408
444abaca
DJ
1409 for (packet = remote_protocol_packets;
1410 packet < &remote_protocol_packets[PACKET_MAX];
1411 packet++)
1412 {
1413 if (&packet->detect == c->var)
1414 {
1415 show_packet_config_cmd (packet);
1416 return;
1417 }
1418 }
9b20d036 1419 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1420 c->name);
5a2468f5
JM
1421}
1422
d471ea57
AC
1423/* Should we try one of the 'Z' requests? */
1424
1425enum Z_packet_type
1426{
1427 Z_PACKET_SOFTWARE_BP,
1428 Z_PACKET_HARDWARE_BP,
1429 Z_PACKET_WRITE_WP,
1430 Z_PACKET_READ_WP,
1431 Z_PACKET_ACCESS_WP,
1432 NR_Z_PACKET_TYPES
1433};
96baa820 1434
d471ea57 1435/* For compatibility with older distributions. Provide a ``set remote
23860348 1436 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1437
7f19b9a2 1438static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1439
1440static void
fba45db2
KB
1441set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1442 struct cmd_list_element *c)
96baa820 1443{
d471ea57 1444 int i;
a744cf53 1445
d471ea57 1446 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1447 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1448}
1449
1450static void
08546159
AC
1451show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1452 struct cmd_list_element *c,
1453 const char *value)
96baa820 1454{
d471ea57 1455 int i;
a744cf53 1456
d471ea57
AC
1457 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1458 {
444abaca 1459 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1460 }
96baa820
JM
1461}
1462
4082afcc
PA
1463/* Returns true if the multi-process extensions are in effect. */
1464
1465static int
1466remote_multi_process_p (struct remote_state *rs)
1467{
1468 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1469}
1470
de0d863e
DB
1471/* Returns true if fork events are supported. */
1472
1473static int
1474remote_fork_event_p (struct remote_state *rs)
1475{
1476 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1477}
1478
c269dbdb
DB
1479/* Returns true if vfork events are supported. */
1480
1481static int
1482remote_vfork_event_p (struct remote_state *rs)
1483{
1484 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1485}
1486
cbb8991c
DB
1487/* Insert fork catchpoint target routine. If fork events are enabled
1488 then return success, nothing more to do. */
1489
1490static int
1491remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1492{
1493 struct remote_state *rs = get_remote_state ();
1494
1495 return !remote_fork_event_p (rs);
1496}
1497
1498/* Remove fork catchpoint target routine. Nothing to do, just
1499 return success. */
1500
1501static int
1502remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1503{
1504 return 0;
1505}
1506
1507/* Insert vfork catchpoint target routine. If vfork events are enabled
1508 then return success, nothing more to do. */
1509
1510static int
1511remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1512{
1513 struct remote_state *rs = get_remote_state ();
1514
1515 return !remote_vfork_event_p (rs);
1516}
1517
1518/* Remove vfork catchpoint target routine. Nothing to do, just
1519 return success. */
1520
1521static int
1522remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1523{
1524 return 0;
1525}
1526
23860348 1527/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1528static struct async_signal_handler *async_sigint_remote_twice_token;
1529static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1530
74531fed
PA
1531\f
1532/* Asynchronous signal handle registered as event loop source for
1533 when we have pending events ready to be passed to the core. */
1534
1535static struct async_event_handler *remote_async_inferior_event_token;
1536
c906108c
SS
1537\f
1538
79d7f229
PA
1539static ptid_t magic_null_ptid;
1540static ptid_t not_sent_ptid;
1541static ptid_t any_thread_ptid;
1542
0b16c5cf
PA
1543/* Find out if the stub attached to PID (and hence GDB should offer to
1544 detach instead of killing it when bailing out). */
1545
1546static int
1547remote_query_attached (int pid)
1548{
1549 struct remote_state *rs = get_remote_state ();
bba74b36 1550 size_t size = get_remote_packet_size ();
0b16c5cf 1551
4082afcc 1552 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1553 return 0;
1554
1555 if (remote_multi_process_p (rs))
bba74b36 1556 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1557 else
bba74b36 1558 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1559
1560 putpkt (rs->buf);
1561 getpkt (&rs->buf, &rs->buf_size, 0);
1562
1563 switch (packet_ok (rs->buf,
1554e9be 1564 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1565 {
1566 case PACKET_OK:
1567 if (strcmp (rs->buf, "1") == 0)
1568 return 1;
1569 break;
1570 case PACKET_ERROR:
1571 warning (_("Remote failure reply: %s"), rs->buf);
1572 break;
1573 case PACKET_UNKNOWN:
1574 break;
1575 }
1576
1577 return 0;
1578}
1579
49c62f2e
PA
1580/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1581 has been invented by GDB, instead of reported by the target. Since
1582 we can be connected to a remote system before before knowing about
1583 any inferior, mark the target with execution when we find the first
1584 inferior. If ATTACHED is 1, then we had just attached to this
1585 inferior. If it is 0, then we just created this inferior. If it
1586 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1587 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1588 attempt to open this inferior's executable as the main executable
1589 if no main executable is open already. */
1941c569
PA
1590
1591static struct inferior *
1b6e6f5c
GB
1592remote_add_inferior (int fake_pid_p, int pid, int attached,
1593 int try_open_exec)
1941c569 1594{
1941c569
PA
1595 struct inferior *inf;
1596
0b16c5cf
PA
1597 /* Check whether this process we're learning about is to be
1598 considered attached, or if is to be considered to have been
1599 spawned by the stub. */
1600 if (attached == -1)
1601 attached = remote_query_attached (pid);
1602
f5656ead 1603 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1604 {
1605 /* If the target shares code across all inferiors, then every
1606 attach adds a new inferior. */
1607 inf = add_inferior (pid);
1608
1609 /* ... and every inferior is bound to the same program space.
1610 However, each inferior may still have its own address
1611 space. */
1612 inf->aspace = maybe_new_address_space ();
1613 inf->pspace = current_program_space;
1614 }
1615 else
1616 {
1617 /* In the traditional debugging scenario, there's a 1-1 match
1618 between program/address spaces. We simply bind the inferior
1619 to the program space's address space. */
1620 inf = current_inferior ();
1621 inferior_appeared (inf, pid);
1622 }
1941c569 1623
0b16c5cf 1624 inf->attach_flag = attached;
49c62f2e 1625 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1626
1b6e6f5c
GB
1627 /* If no main executable is currently open then attempt to
1628 open the file that was executed to create this inferior. */
835205d0 1629 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1630 exec_file_locate_attach (pid, 1);
1631
1941c569
PA
1632 return inf;
1633}
1634
1635/* Add thread PTID to GDB's thread list. Tag it as executing/running
1636 according to RUNNING. */
1637
c906108c 1638static void
1941c569 1639remote_add_thread (ptid_t ptid, int running)
c906108c 1640{
b7ea362b
PA
1641 struct remote_state *rs = get_remote_state ();
1642
1643 /* GDB historically didn't pull threads in the initial connection
1644 setup. If the remote target doesn't even have a concept of
1645 threads (e.g., a bare-metal target), even if internally we
1646 consider that a single-threaded target, mentioning a new thread
1647 might be confusing to the user. Be silent then, preserving the
1648 age old behavior. */
1649 if (rs->starting_up)
1650 add_thread_silent (ptid);
1651 else
1652 add_thread (ptid);
1941c569
PA
1653
1654 set_executing (ptid, running);
1655 set_running (ptid, running);
1656}
1657
1658/* Come here when we learn about a thread id from the remote target.
1659 It may be the first time we hear about such thread, so take the
1660 opportunity to add it to GDB's thread list. In case this is the
1661 first time we're noticing its corresponding inferior, add it to
1662 GDB's inferior list as well. */
1663
1664static void
1665remote_notice_new_inferior (ptid_t currthread, int running)
1666{
c906108c
SS
1667 /* If this is a new thread, add it to GDB's thread list.
1668 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1669
1670 if (in_thread_list (currthread) && is_exited (currthread))
1671 {
1672 /* We're seeing an event on a thread id we knew had exited.
1673 This has to be a new thread reusing the old id. Add it. */
1941c569 1674 remote_add_thread (currthread, running);
82f73884
PA
1675 return;
1676 }
1677
79d7f229 1678 if (!in_thread_list (currthread))
c0a2216e 1679 {
1941c569 1680 struct inferior *inf = NULL;
bad34192 1681 int pid = ptid_get_pid (currthread);
1941c569 1682
bad34192
PA
1683 if (ptid_is_pid (inferior_ptid)
1684 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1685 {
1686 /* inferior_ptid has no thread member yet. This can happen
1687 with the vAttach -> remote_wait,"TAAthread:" path if the
1688 stub doesn't support qC. This is the first stop reported
1689 after an attach, so this is the main thread. Update the
1690 ptid in the thread list. */
bad34192
PA
1691 if (in_thread_list (pid_to_ptid (pid)))
1692 thread_change_ptid (inferior_ptid, currthread);
1693 else
1694 {
1695 remote_add_thread (currthread, running);
1696 inferior_ptid = currthread;
1697 }
dc146f7c 1698 return;
c0a2216e 1699 }
82f73884
PA
1700
1701 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1702 {
1703 /* inferior_ptid is not set yet. This can happen with the
1704 vRun -> remote_wait,"TAAthread:" path if the stub
1705 doesn't support qC. This is the first stop reported
1706 after an attach, so this is the main thread. Update the
1707 ptid in the thread list. */
dc146f7c 1708 thread_change_ptid (inferior_ptid, currthread);
82f73884 1709 return;
c0a2216e 1710 }
82f73884 1711
29c87f7f
PA
1712 /* When connecting to a target remote, or to a target
1713 extended-remote which already was debugging an inferior, we
1714 may not know about it yet. Add it before adding its child
1715 thread, so notifications are emitted in a sensible order. */
1716 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1717 {
1718 struct remote_state *rs = get_remote_state ();
1719 int fake_pid_p = !remote_multi_process_p (rs);
1720
1721 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1722 ptid_get_pid (currthread), -1, 1);
49c62f2e 1723 }
29c87f7f 1724
82f73884 1725 /* This is really a new thread. Add it. */
1941c569
PA
1726 remote_add_thread (currthread, running);
1727
1728 /* If we found a new inferior, let the common code do whatever
1729 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1730 breakpoints), unless we're just setting up an all-stop
1731 connection. */
1941c569 1732 if (inf != NULL)
b7ea362b
PA
1733 {
1734 struct remote_state *rs = get_remote_state ();
1735
1736 if (non_stop || !rs->starting_up)
1737 notice_new_inferior (currthread, running, 0);
1738 }
c0a2216e 1739 }
c906108c
SS
1740}
1741
dc146f7c
VP
1742/* Return the private thread data, creating it if necessary. */
1743
70221824 1744static struct private_thread_info *
dc146f7c
VP
1745demand_private_info (ptid_t ptid)
1746{
1747 struct thread_info *info = find_thread_ptid (ptid);
1748
1749 gdb_assert (info);
1750
fe978cb0 1751 if (!info->priv)
dc146f7c 1752 {
fe978cb0 1753 info->priv = xmalloc (sizeof (*(info->priv)));
dc146f7c 1754 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1755 info->priv->core = -1;
1756 info->priv->extra = 0;
dc146f7c
VP
1757 }
1758
fe978cb0 1759 return info->priv;
dc146f7c
VP
1760}
1761
74531fed
PA
1762/* Call this function as a result of
1763 1) A halt indication (T packet) containing a thread id
1764 2) A direct query of currthread
0df8b418 1765 3) Successful execution of set thread */
74531fed
PA
1766
1767static void
47f8a51d 1768record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1769{
47f8a51d 1770 rs->general_thread = currthread;
74531fed
PA
1771}
1772
89be2091
DJ
1773/* If 'QPassSignals' is supported, tell the remote stub what signals
1774 it can simply pass through to the inferior without reporting. */
1775
1776static void
94bedb42
TT
1777remote_pass_signals (struct target_ops *self,
1778 int numsigs, unsigned char *pass_signals)
89be2091 1779{
4082afcc 1780 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1781 {
1782 char *pass_packet, *p;
89be2091 1783 int count = 0, i;
747dc59d 1784 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1785
1786 gdb_assert (numsigs < 256);
1787 for (i = 0; i < numsigs; i++)
1788 {
2455069d 1789 if (pass_signals[i])
89be2091
DJ
1790 count++;
1791 }
1792 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1793 strcpy (pass_packet, "QPassSignals:");
1794 p = pass_packet + strlen (pass_packet);
1795 for (i = 0; i < numsigs; i++)
1796 {
2455069d 1797 if (pass_signals[i])
89be2091
DJ
1798 {
1799 if (i >= 16)
1800 *p++ = tohex (i >> 4);
1801 *p++ = tohex (i & 15);
1802 if (count)
1803 *p++ = ';';
1804 else
1805 break;
1806 count--;
1807 }
1808 }
1809 *p = 0;
747dc59d 1810 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1811 {
89be2091
DJ
1812 putpkt (pass_packet);
1813 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1814 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1815 if (rs->last_pass_packet)
1816 xfree (rs->last_pass_packet);
1817 rs->last_pass_packet = pass_packet;
89be2091
DJ
1818 }
1819 else
1820 xfree (pass_packet);
1821 }
1822}
1823
9b224c5e
PA
1824/* If 'QProgramSignals' is supported, tell the remote stub what
1825 signals it should pass through to the inferior when detaching. */
1826
1827static void
daf5e9b6
TT
1828remote_program_signals (struct target_ops *self,
1829 int numsigs, unsigned char *signals)
9b224c5e 1830{
4082afcc 1831 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1832 {
1833 char *packet, *p;
1834 int count = 0, i;
5e4a05c4 1835 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1836
1837 gdb_assert (numsigs < 256);
1838 for (i = 0; i < numsigs; i++)
1839 {
1840 if (signals[i])
1841 count++;
1842 }
1843 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1844 strcpy (packet, "QProgramSignals:");
1845 p = packet + strlen (packet);
1846 for (i = 0; i < numsigs; i++)
1847 {
1848 if (signal_pass_state (i))
1849 {
1850 if (i >= 16)
1851 *p++ = tohex (i >> 4);
1852 *p++ = tohex (i & 15);
1853 if (count)
1854 *p++ = ';';
1855 else
1856 break;
1857 count--;
1858 }
1859 }
1860 *p = 0;
5e4a05c4
TT
1861 if (!rs->last_program_signals_packet
1862 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1863 {
9b224c5e
PA
1864 putpkt (packet);
1865 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1866 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1867 xfree (rs->last_program_signals_packet);
1868 rs->last_program_signals_packet = packet;
9b224c5e
PA
1869 }
1870 else
1871 xfree (packet);
1872 }
1873}
1874
79d7f229
PA
1875/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1876 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1877 thread. If GEN is set, set the general thread, if not, then set
1878 the step/continue thread. */
c906108c 1879static void
79d7f229 1880set_thread (struct ptid ptid, int gen)
c906108c 1881{
d01949b6 1882 struct remote_state *rs = get_remote_state ();
47f8a51d 1883 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1884 char *buf = rs->buf;
79d7f229 1885 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1886
79d7f229 1887 if (ptid_equal (state, ptid))
c906108c
SS
1888 return;
1889
79d7f229
PA
1890 *buf++ = 'H';
1891 *buf++ = gen ? 'g' : 'c';
1892 if (ptid_equal (ptid, magic_null_ptid))
1893 xsnprintf (buf, endbuf - buf, "0");
1894 else if (ptid_equal (ptid, any_thread_ptid))
1895 xsnprintf (buf, endbuf - buf, "0");
1896 else if (ptid_equal (ptid, minus_one_ptid))
1897 xsnprintf (buf, endbuf - buf, "-1");
1898 else
82f73884 1899 write_ptid (buf, endbuf, ptid);
79d7f229 1900 putpkt (rs->buf);
6d820c5c 1901 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1902 if (gen)
47f8a51d 1903 rs->general_thread = ptid;
c906108c 1904 else
47f8a51d 1905 rs->continue_thread = ptid;
c906108c 1906}
79d7f229
PA
1907
1908static void
1909set_general_thread (struct ptid ptid)
1910{
1911 set_thread (ptid, 1);
1912}
1913
1914static void
1915set_continue_thread (struct ptid ptid)
1916{
1917 set_thread (ptid, 0);
1918}
1919
3c9c4b83
PA
1920/* Change the remote current process. Which thread within the process
1921 ends up selected isn't important, as long as it is the same process
1922 as what INFERIOR_PTID points to.
1923
1924 This comes from that fact that there is no explicit notion of
1925 "selected process" in the protocol. The selected process for
1926 general operations is the process the selected general thread
1927 belongs to. */
1928
1929static void
1930set_general_process (void)
1931{
1932 struct remote_state *rs = get_remote_state ();
1933
1934 /* If the remote can't handle multiple processes, don't bother. */
901f9912 1935 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
1936 return;
1937
1938 /* We only need to change the remote current thread if it's pointing
1939 at some other process. */
47f8a51d 1940 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
1941 set_general_thread (inferior_ptid);
1942}
1943
c906108c 1944\f
7d1a114c
PA
1945/* Return nonzero if this is the main thread that we made up ourselves
1946 to model non-threaded targets as single-threaded. */
c906108c
SS
1947
1948static int
7d1a114c 1949remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 1950{
6d820c5c 1951 struct remote_state *rs = get_remote_state ();
82f73884 1952 char *p, *endp;
c906108c 1953
c0a2216e
PA
1954 if (ptid_equal (ptid, magic_null_ptid))
1955 /* The main thread is always alive. */
1956 return 1;
1957
ba348170 1958 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
1959 /* The main thread is always alive. This can happen after a
1960 vAttach, if the remote side doesn't support
1961 multi-threading. */
1962 return 1;
1963
7d1a114c
PA
1964 return 0;
1965}
1966
1967/* Return nonzero if the thread PTID is still alive on the remote
1968 system. */
1969
1970static int
1971remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1972{
1973 struct remote_state *rs = get_remote_state ();
1974 char *p, *endp;
1975
1976 /* Check if this is a thread that we made up ourselves to model
1977 non-threaded targets as single-threaded. */
1978 if (remote_thread_always_alive (ops, ptid))
1979 return 1;
1980
82f73884
PA
1981 p = rs->buf;
1982 endp = rs->buf + get_remote_packet_size ();
1983
1984 *p++ = 'T';
1985 write_ptid (p, endp, ptid);
1986
2e9f7625 1987 putpkt (rs->buf);
6d820c5c 1988 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 1989 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
1990}
1991
1992/* About these extended threadlist and threadinfo packets. They are
1993 variable length packets but, the fields within them are often fixed
1994 length. They are redundent enough to send over UDP as is the
1995 remote protocol in general. There is a matching unit test module
1996 in libstub. */
1997
23860348 1998/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 1999 libstub protocol encoding, and remote.c. It is not particularly
23860348 2000 changable. */
cce74817
JM
2001
2002/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2003 Plan to fix this. */
cce74817 2004
23860348 2005typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2006
9d1f7ab2 2007/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2008 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2009
2010struct gdb_ext_thread_info
c5aa993b 2011 {
23860348 2012 threadref threadid; /* External form of thread reference. */
2bc416ba 2013 int active; /* Has state interesting to GDB?
23860348 2014 regs, stack. */
2bc416ba 2015 char display[256]; /* Brief state display, name,
cedea757 2016 blocked/suspended. */
23860348 2017 char shortname[32]; /* To be used to name threads. */
2bc416ba 2018 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2019 whatever. */
c5aa993b 2020 };
cce74817
JM
2021
2022/* The volume of remote transfers can be limited by submitting
2023 a mask containing bits specifying the desired information.
2024 Use a union of these values as the 'selection' parameter to
0df8b418 2025 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2026
2027#define TAG_THREADID 1
2028#define TAG_EXISTS 2
2029#define TAG_DISPLAY 4
2030#define TAG_THREADNAME 8
c5aa993b 2031#define TAG_MOREDISPLAY 16
cce74817 2032
23860348 2033#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2034
a14ed312 2035static char *unpack_nibble (char *buf, int *val);
cce74817 2036
a14ed312 2037static char *unpack_byte (char *buf, int *value);
cce74817 2038
a14ed312 2039static char *pack_int (char *buf, int value);
cce74817 2040
a14ed312 2041static char *unpack_int (char *buf, int *value);
cce74817 2042
a14ed312 2043static char *unpack_string (char *src, char *dest, int length);
cce74817 2044
23860348 2045static char *pack_threadid (char *pkt, threadref *id);
cce74817 2046
23860348 2047static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2048
23860348 2049void int_to_threadref (threadref *id, int value);
cce74817 2050
23860348 2051static int threadref_to_int (threadref *ref);
cce74817 2052
23860348 2053static void copy_threadref (threadref *dest, threadref *src);
cce74817 2054
23860348 2055static int threadmatch (threadref *dest, threadref *src);
cce74817 2056
2bc416ba 2057static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2058 threadref *id);
cce74817 2059
a14ed312 2060static int remote_unpack_thread_info_response (char *pkt,
23860348 2061 threadref *expectedref,
a14ed312
KB
2062 struct gdb_ext_thread_info
2063 *info);
cce74817
JM
2064
2065
2bc416ba 2066static int remote_get_threadinfo (threadref *threadid,
23860348 2067 int fieldset, /*TAG mask */
a14ed312 2068 struct gdb_ext_thread_info *info);
cce74817 2069
a14ed312
KB
2070static char *pack_threadlist_request (char *pkt, int startflag,
2071 int threadcount,
23860348 2072 threadref *nextthread);
cce74817 2073
a14ed312
KB
2074static int parse_threadlist_response (char *pkt,
2075 int result_limit,
23860348 2076 threadref *original_echo,
2bc416ba 2077 threadref *resultlist,
23860348 2078 int *doneflag);
cce74817 2079
a14ed312 2080static int remote_get_threadlist (int startflag,
23860348 2081 threadref *nextthread,
a14ed312
KB
2082 int result_limit,
2083 int *done,
2bc416ba 2084 int *result_count,
23860348 2085 threadref *threadlist);
cce74817 2086
23860348 2087typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2088
a14ed312
KB
2089static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2090 void *context, int looplimit);
cce74817 2091
23860348 2092static int remote_newthread_step (threadref *ref, void *context);
cce74817 2093
82f73884
PA
2094
2095/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2096 buffer we're allowed to write to. Returns
2097 BUF+CHARACTERS_WRITTEN. */
2098
2099static char *
2100write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2101{
2102 int pid, tid;
2103 struct remote_state *rs = get_remote_state ();
2104
2105 if (remote_multi_process_p (rs))
2106 {
2107 pid = ptid_get_pid (ptid);
2108 if (pid < 0)
2109 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2110 else
2111 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2112 }
ba348170 2113 tid = ptid_get_lwp (ptid);
82f73884
PA
2114 if (tid < 0)
2115 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2116 else
2117 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2118
2119 return buf;
2120}
2121
2122/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2123 passed the last parsed char. Returns null_ptid on error. */
2124
2125static ptid_t
2126read_ptid (char *buf, char **obuf)
2127{
2128 char *p = buf;
2129 char *pp;
2130 ULONGEST pid = 0, tid = 0;
82f73884
PA
2131
2132 if (*p == 'p')
2133 {
2134 /* Multi-process ptid. */
2135 pp = unpack_varlen_hex (p + 1, &pid);
2136 if (*pp != '.')
b37520b6 2137 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2138
2139 p = pp;
2140 pp = unpack_varlen_hex (p + 1, &tid);
2141 if (obuf)
2142 *obuf = pp;
ba348170 2143 return ptid_build (pid, tid, 0);
82f73884
PA
2144 }
2145
2146 /* No multi-process. Just a tid. */
2147 pp = unpack_varlen_hex (p, &tid);
2148
2149 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2150 what's in inferior_ptid, unless it's null at this point. If so,
2151 then since there's no way to know the pid of the reported
2152 threads, use the magic number. */
2153 if (ptid_equal (inferior_ptid, null_ptid))
2154 pid = ptid_get_pid (magic_null_ptid);
2155 else
2156 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2157
2158 if (obuf)
2159 *obuf = pp;
ba348170 2160 return ptid_build (pid, tid, 0);
82f73884
PA
2161}
2162
c906108c 2163static int
fba45db2 2164stubhex (int ch)
c906108c
SS
2165{
2166 if (ch >= 'a' && ch <= 'f')
2167 return ch - 'a' + 10;
2168 if (ch >= '0' && ch <= '9')
2169 return ch - '0';
2170 if (ch >= 'A' && ch <= 'F')
2171 return ch - 'A' + 10;
2172 return -1;
2173}
2174
2175static int
fba45db2 2176stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2177{
2178 int nibble;
2179 int retval = 0;
2180
2181 while (fieldlength)
2182 {
2183 nibble = stubhex (*buff++);
2184 retval |= nibble;
2185 fieldlength--;
2186 if (fieldlength)
2187 retval = retval << 4;
2188 }
2189 return retval;
2190}
2191
c906108c 2192static char *
fba45db2 2193unpack_nibble (char *buf, int *val)
c906108c 2194{
b7589f7d 2195 *val = fromhex (*buf++);
c906108c
SS
2196 return buf;
2197}
2198
c906108c 2199static char *
fba45db2 2200unpack_byte (char *buf, int *value)
c906108c
SS
2201{
2202 *value = stub_unpack_int (buf, 2);
2203 return buf + 2;
2204}
2205
2206static char *
fba45db2 2207pack_int (char *buf, int value)
c906108c
SS
2208{
2209 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2210 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2211 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2212 buf = pack_hex_byte (buf, (value & 0xff));
2213 return buf;
2214}
2215
2216static char *
fba45db2 2217unpack_int (char *buf, int *value)
c906108c
SS
2218{
2219 *value = stub_unpack_int (buf, 8);
2220 return buf + 8;
2221}
2222
23860348 2223#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2224static char *pack_string (char *pkt, char *string);
c906108c
SS
2225
2226static char *
fba45db2 2227pack_string (char *pkt, char *string)
c906108c
SS
2228{
2229 char ch;
2230 int len;
2231
2232 len = strlen (string);
2233 if (len > 200)
23860348 2234 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2235 pkt = pack_hex_byte (pkt, len);
2236 while (len-- > 0)
2237 {
2238 ch = *string++;
2239 if ((ch == '\0') || (ch == '#'))
23860348 2240 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2241 *pkt++ = ch;
2242 }
2243 return pkt;
2244}
2245#endif /* 0 (unused) */
2246
2247static char *
fba45db2 2248unpack_string (char *src, char *dest, int length)
c906108c
SS
2249{
2250 while (length--)
2251 *dest++ = *src++;
2252 *dest = '\0';
2253 return src;
2254}
2255
2256static char *
fba45db2 2257pack_threadid (char *pkt, threadref *id)
c906108c
SS
2258{
2259 char *limit;
2260 unsigned char *altid;
2261
2262 altid = (unsigned char *) id;
2263 limit = pkt + BUF_THREAD_ID_SIZE;
2264 while (pkt < limit)
2265 pkt = pack_hex_byte (pkt, *altid++);
2266 return pkt;
2267}
2268
2269
2270static char *
fba45db2 2271unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2272{
2273 char *altref;
2274 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2275 int x, y;
2276
2277 altref = (char *) id;
2278
2279 while (inbuf < limit)
2280 {
2281 x = stubhex (*inbuf++);
2282 y = stubhex (*inbuf++);
2283 *altref++ = (x << 4) | y;
2284 }
2285 return inbuf;
2286}
2287
2288/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2289 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2290 to use 64bit thread references internally. This is an adapter
2291 function. */
2292
2293void
fba45db2 2294int_to_threadref (threadref *id, int value)
c906108c
SS
2295{
2296 unsigned char *scan;
2297
2298 scan = (unsigned char *) id;
2299 {
2300 int i = 4;
2301 while (i--)
2302 *scan++ = 0;
2303 }
2304 *scan++ = (value >> 24) & 0xff;
2305 *scan++ = (value >> 16) & 0xff;
2306 *scan++ = (value >> 8) & 0xff;
2307 *scan++ = (value & 0xff);
2308}
2309
2310static int
fba45db2 2311threadref_to_int (threadref *ref)
c906108c
SS
2312{
2313 int i, value = 0;
2314 unsigned char *scan;
2315
cfd77fa1 2316 scan = *ref;
c906108c
SS
2317 scan += 4;
2318 i = 4;
2319 while (i-- > 0)
2320 value = (value << 8) | ((*scan++) & 0xff);
2321 return value;
2322}
2323
2324static void
fba45db2 2325copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2326{
2327 int i;
2328 unsigned char *csrc, *cdest;
2329
2330 csrc = (unsigned char *) src;
2331 cdest = (unsigned char *) dest;
2332 i = 8;
2333 while (i--)
2334 *cdest++ = *csrc++;
2335}
2336
2337static int
fba45db2 2338threadmatch (threadref *dest, threadref *src)
c906108c 2339{
23860348 2340 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2341#if 0
2342 unsigned char *srcp, *destp;
2343 int i, result;
2344 srcp = (char *) src;
2345 destp = (char *) dest;
2346
2347 result = 1;
2348 while (i-- > 0)
2349 result &= (*srcp++ == *destp++) ? 1 : 0;
2350 return result;
2351#endif
2352 return 1;
2353}
2354
2355/*
c5aa993b
JM
2356 threadid:1, # always request threadid
2357 context_exists:2,
2358 display:4,
2359 unique_name:8,
2360 more_display:16
2361 */
c906108c
SS
2362
2363/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2364
2365static char *
fba45db2 2366pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2367{
23860348
MS
2368 *pkt++ = 'q'; /* Info Query */
2369 *pkt++ = 'P'; /* process or thread info */
2370 pkt = pack_int (pkt, mode); /* mode */
c906108c 2371 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2372 *pkt = '\0'; /* terminate */
c906108c
SS
2373 return pkt;
2374}
2375
23860348 2376/* These values tag the fields in a thread info response packet. */
c906108c 2377/* Tagging the fields allows us to request specific fields and to
23860348 2378 add more fields as time goes by. */
c906108c 2379
23860348 2380#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2381#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2382 fetch registers and its stack? */
c5aa993b 2383#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2384#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2385#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2386 the process. */
c906108c
SS
2387
2388static int
fba45db2
KB
2389remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2390 struct gdb_ext_thread_info *info)
c906108c 2391{
d01949b6 2392 struct remote_state *rs = get_remote_state ();
c906108c 2393 int mask, length;
cfd77fa1 2394 int tag;
c906108c 2395 threadref ref;
6d820c5c 2396 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2397 int retval = 1;
2398
23860348 2399 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2400 info->active = 0;
2401 info->display[0] = '\0';
2402 info->shortname[0] = '\0';
2403 info->more_display[0] = '\0';
2404
23860348
MS
2405 /* Assume the characters indicating the packet type have been
2406 stripped. */
c906108c
SS
2407 pkt = unpack_int (pkt, &mask); /* arg mask */
2408 pkt = unpack_threadid (pkt, &ref);
2409
2410 if (mask == 0)
8a3fe4f8 2411 warning (_("Incomplete response to threadinfo request."));
c906108c 2412 if (!threadmatch (&ref, expectedref))
23860348 2413 { /* This is an answer to a different request. */
8a3fe4f8 2414 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2415 return 0;
2416 }
2417 copy_threadref (&info->threadid, &ref);
2418
23860348 2419 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2420
23860348
MS
2421 /* Packets are terminated with nulls. */
2422 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2423 {
2424 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2425 pkt = unpack_byte (pkt, &length); /* length */
2426 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2427 {
8a3fe4f8 2428 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2429 retval = 0;
2430 break;
2431 }
2432 if (tag == TAG_THREADID)
2433 {
2434 if (length != 16)
2435 {
8a3fe4f8 2436 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2437 retval = 0;
2438 break;
2439 }
2440 pkt = unpack_threadid (pkt, &ref);
2441 mask = mask & ~TAG_THREADID;
2442 continue;
2443 }
2444 if (tag == TAG_EXISTS)
2445 {
2446 info->active = stub_unpack_int (pkt, length);
2447 pkt += length;
2448 mask = mask & ~(TAG_EXISTS);
2449 if (length > 8)
2450 {
8a3fe4f8 2451 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2452 retval = 0;
2453 break;
2454 }
2455 continue;
2456 }
2457 if (tag == TAG_THREADNAME)
2458 {
2459 pkt = unpack_string (pkt, &info->shortname[0], length);
2460 mask = mask & ~TAG_THREADNAME;
2461 continue;
2462 }
2463 if (tag == TAG_DISPLAY)
2464 {
2465 pkt = unpack_string (pkt, &info->display[0], length);
2466 mask = mask & ~TAG_DISPLAY;
2467 continue;
2468 }
2469 if (tag == TAG_MOREDISPLAY)
2470 {
2471 pkt = unpack_string (pkt, &info->more_display[0], length);
2472 mask = mask & ~TAG_MOREDISPLAY;
2473 continue;
2474 }
8a3fe4f8 2475 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2476 break; /* Not a tag we know about. */
c906108c
SS
2477 }
2478 return retval;
2479}
2480
2481static int
fba45db2
KB
2482remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2483 struct gdb_ext_thread_info *info)
c906108c 2484{
d01949b6 2485 struct remote_state *rs = get_remote_state ();
c906108c 2486 int result;
c906108c 2487
2e9f7625
DJ
2488 pack_threadinfo_request (rs->buf, fieldset, threadid);
2489 putpkt (rs->buf);
6d820c5c 2490 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2491
2492 if (rs->buf[0] == '\0')
2493 return 0;
2494
2e9f7625 2495 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2496 threadid, info);
c906108c
SS
2497 return result;
2498}
2499
c906108c
SS
2500/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2501
2502static char *
fba45db2
KB
2503pack_threadlist_request (char *pkt, int startflag, int threadcount,
2504 threadref *nextthread)
c906108c
SS
2505{
2506 *pkt++ = 'q'; /* info query packet */
2507 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2508 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2509 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2510 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2511 *pkt = '\0';
2512 return pkt;
2513}
2514
2515/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2516
2517static int
fba45db2
KB
2518parse_threadlist_response (char *pkt, int result_limit,
2519 threadref *original_echo, threadref *resultlist,
2520 int *doneflag)
c906108c 2521{
d01949b6 2522 struct remote_state *rs = get_remote_state ();
c906108c
SS
2523 char *limit;
2524 int count, resultcount, done;
2525
2526 resultcount = 0;
2527 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2528 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2529 /* done parse past here */
c906108c
SS
2530 pkt = unpack_byte (pkt, &count); /* count field */
2531 pkt = unpack_nibble (pkt, &done);
2532 /* The first threadid is the argument threadid. */
2533 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2534 while ((count-- > 0) && (pkt < limit))
2535 {
2536 pkt = unpack_threadid (pkt, resultlist++);
2537 if (resultcount++ >= result_limit)
2538 break;
2539 }
2540 if (doneflag)
2541 *doneflag = done;
2542 return resultcount;
2543}
2544
6dc54d91
PA
2545/* Fetch the next batch of threads from the remote. Returns -1 if the
2546 qL packet is not supported, 0 on error and 1 on success. */
2547
c906108c 2548static int
fba45db2
KB
2549remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2550 int *done, int *result_count, threadref *threadlist)
c906108c 2551{
d01949b6 2552 struct remote_state *rs = get_remote_state ();
c906108c
SS
2553 int result = 1;
2554
23860348 2555 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2556 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2557 >= get_remote_packet_size ())
ea9c271d 2558 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2559
6d820c5c
DJ
2560 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2561 putpkt (rs->buf);
2562 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2563 if (*rs->buf == '\0')
6dc54d91
PA
2564 {
2565 /* Packet not supported. */
2566 return -1;
2567 }
2568
2569 *result_count =
2570 parse_threadlist_response (rs->buf + 2, result_limit,
2571 &rs->echo_nextthread, threadlist, done);
c906108c 2572
0d031856 2573 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2574 {
23860348
MS
2575 /* FIXME: This is a good reason to drop the packet. */
2576 /* Possably, there is a duplicate response. */
c906108c
SS
2577 /* Possabilities :
2578 retransmit immediatly - race conditions
2579 retransmit after timeout - yes
2580 exit
2581 wait for packet, then exit
2582 */
8a3fe4f8 2583 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2584 return 0; /* I choose simply exiting. */
c906108c
SS
2585 }
2586 if (*result_count <= 0)
2587 {
2588 if (*done != 1)
2589 {
8a3fe4f8 2590 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2591 result = 0;
2592 }
2593 return result; /* break; */
2594 }
2595 if (*result_count > result_limit)
2596 {
2597 *result_count = 0;
8a3fe4f8 2598 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2599 return 0;
2600 }
2601 return result;
2602}
2603
6dc54d91
PA
2604/* Fetch the list of remote threads, with the qL packet, and call
2605 STEPFUNCTION for each thread found. Stops iterating and returns 1
2606 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2607 STEPFUNCTION returns false. If the packet is not supported,
2608 returns -1. */
c906108c 2609
c906108c 2610static int
fba45db2
KB
2611remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2612 int looplimit)
c906108c 2613{
0d031856 2614 struct remote_state *rs = get_remote_state ();
c906108c
SS
2615 int done, i, result_count;
2616 int startflag = 1;
2617 int result = 1;
2618 int loopcount = 0;
c906108c
SS
2619
2620 done = 0;
2621 while (!done)
2622 {
2623 if (loopcount++ > looplimit)
2624 {
2625 result = 0;
8a3fe4f8 2626 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2627 break;
2628 }
6dc54d91
PA
2629 result = remote_get_threadlist (startflag, &rs->nextthread,
2630 MAXTHREADLISTRESULTS,
2631 &done, &result_count,
2632 rs->resultthreadlist);
2633 if (result <= 0)
2634 break;
23860348 2635 /* Clear for later iterations. */
c906108c
SS
2636 startflag = 0;
2637 /* Setup to resume next batch of thread references, set nextthread. */
2638 if (result_count >= 1)
0d031856
TT
2639 copy_threadref (&rs->nextthread,
2640 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2641 i = 0;
2642 while (result_count--)
6dc54d91
PA
2643 {
2644 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2645 {
2646 result = 0;
2647 break;
2648 }
2649 }
c906108c
SS
2650 }
2651 return result;
2652}
2653
6dc54d91
PA
2654/* A thread found on the remote target. */
2655
2656typedef struct thread_item
2657{
2658 /* The thread's PTID. */
2659 ptid_t ptid;
2660
2661 /* The thread's extra info. May be NULL. */
2662 char *extra;
2663
2664 /* The core the thread was running on. -1 if not known. */
2665 int core;
2666} thread_item_t;
2667DEF_VEC_O(thread_item_t);
2668
2669/* Context passed around to the various methods listing remote
2670 threads. As new threads are found, they're added to the ITEMS
2671 vector. */
2672
2673struct threads_listing_context
2674{
2675 /* The threads found on the remote target. */
2676 VEC (thread_item_t) *items;
2677};
2678
80134cf5
PA
2679/* Discard the contents of the constructed thread listing context. */
2680
2681static void
2682clear_threads_listing_context (void *p)
2683{
2684 struct threads_listing_context *context = p;
2685 int i;
2686 struct thread_item *item;
2687
2688 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2689 xfree (item->extra);
2690
2691 VEC_free (thread_item_t, context->items);
2692}
2693
cbb8991c
DB
2694/* Remove the thread specified as the related_pid field of WS
2695 from the CONTEXT list. */
2696
2697static void
2698threads_listing_context_remove (struct target_waitstatus *ws,
2699 struct threads_listing_context *context)
2700{
2701 struct thread_item *item;
2702 int i;
2703 ptid_t child_ptid = ws->value.related_pid;
2704
2705 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2706 {
2707 if (ptid_equal (item->ptid, child_ptid))
2708 {
2709 VEC_ordered_remove (thread_item_t, context->items, i);
2710 break;
2711 }
2712 }
2713}
2714
c906108c 2715static int
6dc54d91 2716remote_newthread_step (threadref *ref, void *data)
c906108c 2717{
6dc54d91
PA
2718 struct threads_listing_context *context = data;
2719 struct thread_item item;
79d7f229 2720 int pid = ptid_get_pid (inferior_ptid);
39f77062 2721
6dc54d91
PA
2722 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2723 item.core = -1;
2724 item.extra = NULL;
2725
2726 VEC_safe_push (thread_item_t, context->items, &item);
2727
c906108c
SS
2728 return 1; /* continue iterator */
2729}
2730
2731#define CRAZY_MAX_THREADS 1000
2732
39f77062
KB
2733static ptid_t
2734remote_current_thread (ptid_t oldpid)
c906108c 2735{
d01949b6 2736 struct remote_state *rs = get_remote_state ();
c906108c
SS
2737
2738 putpkt ("qC");
6d820c5c 2739 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2740 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
82f73884 2741 return read_ptid (&rs->buf[2], NULL);
c906108c
SS
2742 else
2743 return oldpid;
2744}
2745
6dc54d91 2746/* List remote threads using the deprecated qL packet. */
cce74817 2747
6dc54d91
PA
2748static int
2749remote_get_threads_with_ql (struct target_ops *ops,
2750 struct threads_listing_context *context)
c906108c 2751{
6dc54d91
PA
2752 if (remote_threadlist_iterator (remote_newthread_step, context,
2753 CRAZY_MAX_THREADS) >= 0)
2754 return 1;
2755
2756 return 0;
c906108c
SS
2757}
2758
dc146f7c
VP
2759#if defined(HAVE_LIBEXPAT)
2760
dc146f7c
VP
2761static void
2762start_thread (struct gdb_xml_parser *parser,
2763 const struct gdb_xml_element *element,
2764 void *user_data, VEC(gdb_xml_value_s) *attributes)
2765{
6dc54d91 2766 struct threads_listing_context *data = user_data;
dc146f7c
VP
2767
2768 struct thread_item item;
2769 char *id;
3d2c1d41 2770 struct gdb_xml_value *attr;
dc146f7c 2771
3d2c1d41 2772 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2773 item.ptid = read_ptid (id, NULL);
2774
3d2c1d41
PA
2775 attr = xml_find_attribute (attributes, "core");
2776 if (attr != NULL)
2777 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2778 else
2779 item.core = -1;
2780
2781 item.extra = 0;
2782
2783 VEC_safe_push (thread_item_t, data->items, &item);
2784}
2785
2786static void
2787end_thread (struct gdb_xml_parser *parser,
2788 const struct gdb_xml_element *element,
2789 void *user_data, const char *body_text)
2790{
6dc54d91 2791 struct threads_listing_context *data = user_data;
dc146f7c
VP
2792
2793 if (body_text && *body_text)
2ae2a0b7 2794 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2795}
2796
2797const struct gdb_xml_attribute thread_attributes[] = {
2798 { "id", GDB_XML_AF_NONE, NULL, NULL },
2799 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2800 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2801};
2802
2803const struct gdb_xml_element thread_children[] = {
2804 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2805};
2806
2807const struct gdb_xml_element threads_children[] = {
2808 { "thread", thread_attributes, thread_children,
2809 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2810 start_thread, end_thread },
2811 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2812};
2813
2814const struct gdb_xml_element threads_elements[] = {
2815 { "threads", NULL, threads_children,
2816 GDB_XML_EF_NONE, NULL, NULL },
2817 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2818};
2819
2820#endif
2821
6dc54d91 2822/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2823
6dc54d91
PA
2824static int
2825remote_get_threads_with_qxfer (struct target_ops *ops,
2826 struct threads_listing_context *context)
0f71a2f6 2827{
dc146f7c 2828#if defined(HAVE_LIBEXPAT)
4082afcc 2829 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2830 {
6dc54d91 2831 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2832 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2833
6dc54d91 2834 if (xml != NULL && *xml != '\0')
dc146f7c 2835 {
6dc54d91
PA
2836 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2837 threads_elements, xml, context);
dc146f7c
VP
2838 }
2839
2840 do_cleanups (back_to);
6dc54d91 2841 return 1;
dc146f7c
VP
2842 }
2843#endif
2844
6dc54d91
PA
2845 return 0;
2846}
2847
2848/* List remote threads using qfThreadInfo/qsThreadInfo. */
2849
2850static int
2851remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2852 struct threads_listing_context *context)
2853{
2854 struct remote_state *rs = get_remote_state ();
2855
b80fafe3 2856 if (rs->use_threadinfo_query)
9d1f7ab2 2857 {
6dc54d91
PA
2858 char *bufp;
2859
9d1f7ab2 2860 putpkt ("qfThreadInfo");
6d820c5c 2861 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2862 bufp = rs->buf;
9d1f7ab2 2863 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2864 {
9d1f7ab2
MS
2865 while (*bufp++ == 'm') /* reply contains one or more TID */
2866 {
2867 do
2868 {
6dc54d91
PA
2869 struct thread_item item;
2870
2871 item.ptid = read_ptid (bufp, &bufp);
2872 item.core = -1;
2873 item.extra = NULL;
2874
2875 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2876 }
2877 while (*bufp++ == ','); /* comma-separated list */
2878 putpkt ("qsThreadInfo");
6d820c5c 2879 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2880 bufp = rs->buf;
9d1f7ab2 2881 }
6dc54d91
PA
2882 return 1;
2883 }
2884 else
2885 {
2886 /* Packet not recognized. */
2887 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2888 }
2889 }
2890
6dc54d91
PA
2891 return 0;
2892}
2893
e8032dde 2894/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2895 targets. */
2896
2897static void
e8032dde 2898remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2899{
2900 struct remote_state *rs = get_remote_state ();
2901 struct threads_listing_context context;
2902 struct cleanup *old_chain;
ab970af1 2903 int got_list = 0;
e8032dde 2904
6dc54d91
PA
2905 context.items = NULL;
2906 old_chain = make_cleanup (clear_threads_listing_context, &context);
2907
2908 /* We have a few different mechanisms to fetch the thread list. Try
2909 them all, starting with the most preferred one first, falling
2910 back to older methods. */
2911 if (remote_get_threads_with_qxfer (ops, &context)
2912 || remote_get_threads_with_qthreadinfo (ops, &context)
2913 || remote_get_threads_with_ql (ops, &context))
2914 {
2915 int i;
2916 struct thread_item *item;
ab970af1
PA
2917 struct thread_info *tp, *tmp;
2918
2919 got_list = 1;
2920
7d1a114c
PA
2921 if (VEC_empty (thread_item_t, context.items)
2922 && remote_thread_always_alive (ops, inferior_ptid))
2923 {
2924 /* Some targets don't really support threads, but still
2925 reply an (empty) thread list in response to the thread
2926 listing packets, instead of replying "packet not
2927 supported". Exit early so we don't delete the main
2928 thread. */
2929 do_cleanups (old_chain);
2930 return;
2931 }
2932
ab970af1
PA
2933 /* CONTEXT now holds the current thread list on the remote
2934 target end. Delete GDB-side threads no longer found on the
2935 target. */
8a06aea7 2936 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 2937 {
ab970af1
PA
2938 for (i = 0;
2939 VEC_iterate (thread_item_t, context.items, i, item);
2940 ++i)
2941 {
2942 if (ptid_equal (item->ptid, tp->ptid))
2943 break;
2944 }
2945
2946 if (i == VEC_length (thread_item_t, context.items))
2947 {
2948 /* Not found. */
2949 delete_thread (tp->ptid);
2950 }
cbb8991c
DB
2951 }
2952
2953 /* Remove any unreported fork child threads from CONTEXT so
2954 that we don't interfere with follow fork, which is where
2955 creation of such threads is handled. */
2956 remove_new_fork_children (&context);
74531fed 2957
ab970af1 2958 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2959 for (i = 0;
2960 VEC_iterate (thread_item_t, context.items, i, item);
2961 ++i)
2962 {
2963 if (!ptid_equal (item->ptid, null_ptid))
2964 {
2965 struct private_thread_info *info;
2966 /* In non-stop mode, we assume new found threads are
2967 running until proven otherwise with a stop reply. In
2968 all-stop, we can only get here if all threads are
2969 stopped. */
2970 int running = non_stop ? 1 : 0;
2971
2972 remote_notice_new_inferior (item->ptid, running);
2973
2974 info = demand_private_info (item->ptid);
2975 info->core = item->core;
2976 info->extra = item->extra;
2977 item->extra = NULL;
2978 }
2979 }
2980 }
2981
ab970af1
PA
2982 if (!got_list)
2983 {
2984 /* If no thread listing method is supported, then query whether
2985 each known thread is alive, one by one, with the T packet.
2986 If the target doesn't support threads at all, then this is a
2987 no-op. See remote_thread_alive. */
2988 prune_threads ();
2989 }
2990
6dc54d91 2991 do_cleanups (old_chain);
9d1f7ab2
MS
2992}
2993
802188a7 2994/*
9d1f7ab2
MS
2995 * Collect a descriptive string about the given thread.
2996 * The target may say anything it wants to about the thread
2997 * (typically info about its blocked / runnable state, name, etc.).
2998 * This string will appear in the info threads display.
802188a7 2999 *
9d1f7ab2
MS
3000 * Optional: targets are not required to implement this function.
3001 */
3002
3003static char *
c15906d8 3004remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3005{
d01949b6 3006 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3007 int result;
3008 int set;
3009 threadref id;
3010 struct gdb_ext_thread_info threadinfo;
23860348 3011 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3012 int n = 0; /* position in display_buf */
3013
5d93a237 3014 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3015 internal_error (__FILE__, __LINE__,
e2e0b3e5 3016 _("remote_threads_extra_info"));
9d1f7ab2 3017
60e569b9 3018 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3019 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3020 /* This is the main thread which was added by GDB. The remote
3021 server doesn't know about it. */
3022 return NULL;
3023
4082afcc 3024 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3025 {
3026 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3027
fe978cb0
PA
3028 if (info && info->priv)
3029 return info->priv->extra;
dc146f7c
VP
3030 else
3031 return NULL;
3032 }
3033
b80fafe3 3034 if (rs->use_threadextra_query)
9d1f7ab2 3035 {
82f73884
PA
3036 char *b = rs->buf;
3037 char *endb = rs->buf + get_remote_packet_size ();
3038
3039 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3040 b += strlen (b);
3041 write_ptid (b, endb, tp->ptid);
3042
2e9f7625 3043 putpkt (rs->buf);
6d820c5c 3044 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3045 if (rs->buf[0] != 0)
9d1f7ab2 3046 {
2e9f7625
DJ
3047 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3048 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3049 display_buf [result] = '\0';
9d1f7ab2
MS
3050 return display_buf;
3051 }
0f71a2f6 3052 }
9d1f7ab2
MS
3053
3054 /* If the above query fails, fall back to the old method. */
b80fafe3 3055 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3056 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3057 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3058 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3059 if (remote_get_threadinfo (&id, set, &threadinfo))
3060 if (threadinfo.active)
0f71a2f6 3061 {
9d1f7ab2 3062 if (*threadinfo.shortname)
2bc416ba 3063 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3064 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3065 if (*threadinfo.display)
2bc416ba 3066 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3067 " State: %s,", threadinfo.display);
9d1f7ab2 3068 if (*threadinfo.more_display)
2bc416ba 3069 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3070 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3071
3072 if (n > 0)
c5aa993b 3073 {
23860348 3074 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3075 if (',' == display_buf[n-1])
3076 display_buf[n-1] = ' ';
3077 return display_buf;
c5aa993b 3078 }
0f71a2f6 3079 }
9d1f7ab2 3080 return NULL;
0f71a2f6 3081}
c906108c 3082\f
c5aa993b 3083
0fb4aa4b 3084static int
61fc905d 3085remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3086 struct static_tracepoint_marker *marker)
3087{
3088 struct remote_state *rs = get_remote_state ();
3089 char *p = rs->buf;
3090
bba74b36 3091 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3092 p += strlen (p);
3093 p += hexnumstr (p, addr);
3094 putpkt (rs->buf);
3095 getpkt (&rs->buf, &rs->buf_size, 0);
3096 p = rs->buf;
3097
3098 if (*p == 'E')
3099 error (_("Remote failure reply: %s"), p);
3100
3101 if (*p++ == 'm')
3102 {
3103 parse_static_tracepoint_marker_definition (p, &p, marker);
3104 return 1;
3105 }
3106
3107 return 0;
3108}
3109
0fb4aa4b 3110static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3111remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3112 const char *strid)
0fb4aa4b
PA
3113{
3114 struct remote_state *rs = get_remote_state ();
3115 VEC(static_tracepoint_marker_p) *markers = NULL;
3116 struct static_tracepoint_marker *marker = NULL;
3117 struct cleanup *old_chain;
3118 char *p;
3119
3120 /* Ask for a first packet of static tracepoint marker
3121 definition. */
3122 putpkt ("qTfSTM");
3123 getpkt (&rs->buf, &rs->buf_size, 0);
3124 p = rs->buf;
3125 if (*p == 'E')
3126 error (_("Remote failure reply: %s"), p);
3127
3128 old_chain = make_cleanup (free_current_marker, &marker);
3129
3130 while (*p++ == 'm')
3131 {
3132 if (marker == NULL)
3133 marker = XCNEW (struct static_tracepoint_marker);
3134
3135 do
3136 {
3137 parse_static_tracepoint_marker_definition (p, &p, marker);
3138
3139 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3140 {
3141 VEC_safe_push (static_tracepoint_marker_p,
3142 markers, marker);
3143 marker = NULL;
3144 }
3145 else
3146 {
3147 release_static_tracepoint_marker (marker);
3148 memset (marker, 0, sizeof (*marker));
3149 }
3150 }
3151 while (*p++ == ','); /* comma-separated list */
3152 /* Ask for another packet of static tracepoint definition. */
3153 putpkt ("qTsSTM");
3154 getpkt (&rs->buf, &rs->buf_size, 0);
3155 p = rs->buf;
3156 }
3157
3158 do_cleanups (old_chain);
3159 return markers;
3160}
3161
3162\f
10760264
JB
3163/* Implement the to_get_ada_task_ptid function for the remote targets. */
3164
3165static ptid_t
1e6b91a4 3166remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3167{
ba348170 3168 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3169}
3170\f
3171
24b06219 3172/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3173
3174static void
fba45db2 3175extended_remote_restart (void)
c906108c 3176{
d01949b6 3177 struct remote_state *rs = get_remote_state ();
c906108c
SS
3178
3179 /* Send the restart command; for reasons I don't understand the
3180 remote side really expects a number after the "R". */
ea9c271d 3181 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3182 putpkt (rs->buf);
c906108c 3183
ad9a8f3f 3184 remote_fileio_reset ();
c906108c
SS
3185}
3186\f
3187/* Clean up connection to a remote debugger. */
3188
c906108c 3189static void
de90e03d 3190remote_close (struct target_ops *self)
c906108c 3191{
5d93a237
TT
3192 struct remote_state *rs = get_remote_state ();
3193
3194 if (rs->remote_desc == NULL)
d3fd5342
PA
3195 return; /* already closed */
3196
3197 /* Make sure we leave stdin registered in the event loop, and we
3198 don't leave the async SIGINT signal handler installed. */
e3594fd1 3199 remote_terminal_ours (self);
ce5ce7ed 3200
5d93a237
TT
3201 serial_close (rs->remote_desc);
3202 rs->remote_desc = NULL;
ce5ce7ed
PA
3203
3204 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3205 of all the inferiors and their threads we were controlling.
3206 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3207 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3208 inferior_ptid = null_ptid;
f67fd822 3209 discard_all_inferiors ();
ce5ce7ed 3210
f48ff2a7
YQ
3211 /* We are closing the remote target, so we should discard
3212 everything of this target. */
bcc75809 3213 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3214
3215 if (remote_async_inferior_event_token)
3216 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3217
5965e028 3218 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3219
3220 trace_reset_local_state ();
c906108c
SS
3221}
3222
23860348 3223/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3224
3225static void
fba45db2 3226get_offsets (void)
c906108c 3227{
d01949b6 3228 struct remote_state *rs = get_remote_state ();
2e9f7625 3229 char *buf;
085dd6e6 3230 char *ptr;
31d99776
DJ
3231 int lose, num_segments = 0, do_sections, do_segments;
3232 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3233 struct section_offsets *offs;
31d99776
DJ
3234 struct symfile_segment_data *data;
3235
3236 if (symfile_objfile == NULL)
3237 return;
c906108c
SS
3238
3239 putpkt ("qOffsets");
6d820c5c 3240 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3241 buf = rs->buf;
c906108c
SS
3242
3243 if (buf[0] == '\000')
3244 return; /* Return silently. Stub doesn't support
23860348 3245 this command. */
c906108c
SS
3246 if (buf[0] == 'E')
3247 {
8a3fe4f8 3248 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3249 return;
3250 }
3251
3252 /* Pick up each field in turn. This used to be done with scanf, but
3253 scanf will make trouble if CORE_ADDR size doesn't match
3254 conversion directives correctly. The following code will work
3255 with any size of CORE_ADDR. */
3256 text_addr = data_addr = bss_addr = 0;
3257 ptr = buf;
3258 lose = 0;
3259
61012eef 3260 if (startswith (ptr, "Text="))
c906108c
SS
3261 {
3262 ptr += 5;
3263 /* Don't use strtol, could lose on big values. */
3264 while (*ptr && *ptr != ';')
3265 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3266
61012eef 3267 if (startswith (ptr, ";Data="))
31d99776
DJ
3268 {
3269 ptr += 6;
3270 while (*ptr && *ptr != ';')
3271 data_addr = (data_addr << 4) + fromhex (*ptr++);
3272 }
3273 else
3274 lose = 1;
3275
61012eef 3276 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3277 {
3278 ptr += 5;
3279 while (*ptr && *ptr != ';')
3280 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3281
31d99776
DJ
3282 if (bss_addr != data_addr)
3283 warning (_("Target reported unsupported offsets: %s"), buf);
3284 }
3285 else
3286 lose = 1;
3287 }
61012eef 3288 else if (startswith (ptr, "TextSeg="))
c906108c 3289 {
31d99776
DJ
3290 ptr += 8;
3291 /* Don't use strtol, could lose on big values. */
c906108c 3292 while (*ptr && *ptr != ';')
31d99776
DJ
3293 text_addr = (text_addr << 4) + fromhex (*ptr++);
3294 num_segments = 1;
3295
61012eef 3296 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3297 {
3298 ptr += 9;
3299 while (*ptr && *ptr != ';')
3300 data_addr = (data_addr << 4) + fromhex (*ptr++);
3301 num_segments++;
3302 }
c906108c
SS
3303 }
3304 else
3305 lose = 1;
3306
3307 if (lose)
8a3fe4f8 3308 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3309 else if (*ptr != '\0')
3310 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3311
802188a7 3312 offs = ((struct section_offsets *)
a39a16c4 3313 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3314 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3315 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3316
31d99776
DJ
3317 data = get_symfile_segment_data (symfile_objfile->obfd);
3318 do_segments = (data != NULL);
3319 do_sections = num_segments == 0;
c906108c 3320
28c32713 3321 if (num_segments > 0)
31d99776 3322 {
31d99776
DJ
3323 segments[0] = text_addr;
3324 segments[1] = data_addr;
3325 }
28c32713
JB
3326 /* If we have two segments, we can still try to relocate everything
3327 by assuming that the .text and .data offsets apply to the whole
3328 text and data segments. Convert the offsets given in the packet
3329 to base addresses for symfile_map_offsets_to_segments. */
3330 else if (data && data->num_segments == 2)
3331 {
3332 segments[0] = data->segment_bases[0] + text_addr;
3333 segments[1] = data->segment_bases[1] + data_addr;
3334 num_segments = 2;
3335 }
8d385431
DJ
3336 /* If the object file has only one segment, assume that it is text
3337 rather than data; main programs with no writable data are rare,
3338 but programs with no code are useless. Of course the code might
3339 have ended up in the data segment... to detect that we would need
3340 the permissions here. */
3341 else if (data && data->num_segments == 1)
3342 {
3343 segments[0] = data->segment_bases[0] + text_addr;
3344 num_segments = 1;
3345 }
28c32713
JB
3346 /* There's no way to relocate by segment. */
3347 else
3348 do_segments = 0;
31d99776
DJ
3349
3350 if (do_segments)
3351 {
3352 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3353 offs, num_segments, segments);
3354
3355 if (ret == 0 && !do_sections)
3e43a32a
MS
3356 error (_("Can not handle qOffsets TextSeg "
3357 "response with this symbol file"));
31d99776
DJ
3358
3359 if (ret > 0)
3360 do_sections = 0;
3361 }
c906108c 3362
9ef895d6
DJ
3363 if (data)
3364 free_symfile_segment_data (data);
31d99776
DJ
3365
3366 if (do_sections)
3367 {
3368 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3369
3e43a32a
MS
3370 /* This is a temporary kludge to force data and bss to use the
3371 same offsets because that's what nlmconv does now. The real
3372 solution requires changes to the stub and remote.c that I
3373 don't have time to do right now. */
31d99776
DJ
3374
3375 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3376 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3377 }
c906108c
SS
3378
3379 objfile_relocate (symfile_objfile, offs);
3380}
3381
74531fed
PA
3382/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3383 threads we know are stopped already. This is used during the
3384 initial remote connection in non-stop mode --- threads that are
3385 reported as already being stopped are left stopped. */
3386
3387static int
3388set_stop_requested_callback (struct thread_info *thread, void *data)
3389{
3390 /* If we have a stop reply for this thread, it must be stopped. */
3391 if (peek_stop_reply (thread->ptid))
3392 set_stop_requested (thread->ptid, 1);
3393
3394 return 0;
3395}
3396
9a7071a8
JB
3397/* Send interrupt_sequence to remote target. */
3398static void
eeae04df 3399send_interrupt_sequence (void)
9a7071a8 3400{
5d93a237
TT
3401 struct remote_state *rs = get_remote_state ();
3402
9a7071a8 3403 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3404 remote_serial_write ("\x03", 1);
9a7071a8 3405 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3406 serial_send_break (rs->remote_desc);
9a7071a8
JB
3407 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3408 {
5d93a237 3409 serial_send_break (rs->remote_desc);
c33e31fd 3410 remote_serial_write ("g", 1);
9a7071a8
JB
3411 }
3412 else
3413 internal_error (__FILE__, __LINE__,
3414 _("Invalid value for interrupt_sequence_mode: %s."),
3415 interrupt_sequence_mode);
3416}
3417
3405876a
PA
3418
3419/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3420 and extract the PTID. Returns NULL_PTID if not found. */
3421
3422static ptid_t
3423stop_reply_extract_thread (char *stop_reply)
3424{
3425 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3426 {
3427 char *p;
3428
3429 /* Txx r:val ; r:val (...) */
3430 p = &stop_reply[3];
3431
3432 /* Look for "register" named "thread". */
3433 while (*p != '\0')
3434 {
3435 char *p1;
3436
3437 p1 = strchr (p, ':');
3438 if (p1 == NULL)
3439 return null_ptid;
3440
3441 if (strncmp (p, "thread", p1 - p) == 0)
3442 return read_ptid (++p1, &p);
3443
3444 p1 = strchr (p, ';');
3445 if (p1 == NULL)
3446 return null_ptid;
3447 p1++;
3448
3449 p = p1;
3450 }
3451 }
3452
3453 return null_ptid;
3454}
3455
b7ea362b
PA
3456/* Determine the remote side's current thread. If we have a stop
3457 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3458 "thread" register we can extract the current thread from. If not,
3459 ask the remote which is the current thread with qC. The former
3460 method avoids a roundtrip. */
3461
3462static ptid_t
3463get_current_thread (char *wait_status)
3464{
3465 ptid_t ptid;
3466
3467 /* Note we don't use remote_parse_stop_reply as that makes use of
3468 the target architecture, which we haven't yet fully determined at
3469 this point. */
3470 if (wait_status != NULL)
3471 ptid = stop_reply_extract_thread (wait_status);
3472 if (ptid_equal (ptid, null_ptid))
3473 ptid = remote_current_thread (inferior_ptid);
3474
3475 return ptid;
3476}
3477
49c62f2e
PA
3478/* Query the remote target for which is the current thread/process,
3479 add it to our tables, and update INFERIOR_PTID. The caller is
3480 responsible for setting the state such that the remote end is ready
3405876a
PA
3481 to return the current thread.
3482
3483 This function is called after handling the '?' or 'vRun' packets,
3484 whose response is a stop reply from which we can also try
3485 extracting the thread. If the target doesn't support the explicit
3486 qC query, we infer the current thread from that stop reply, passed
3487 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3488
3489static void
3405876a 3490add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3491{
3492 struct remote_state *rs = get_remote_state ();
3493 int fake_pid_p = 0;
3405876a 3494 ptid_t ptid = null_ptid;
49c62f2e
PA
3495
3496 inferior_ptid = null_ptid;
3497
b7ea362b
PA
3498 /* Now, if we have thread information, update inferior_ptid. */
3499 ptid = get_current_thread (wait_status);
3405876a 3500
49c62f2e
PA
3501 if (!ptid_equal (ptid, null_ptid))
3502 {
3503 if (!remote_multi_process_p (rs))
3504 fake_pid_p = 1;
3505
3506 inferior_ptid = ptid;
3507 }
3508 else
3509 {
3510 /* Without this, some commands which require an active target
3511 (such as kill) won't work. This variable serves (at least)
3512 double duty as both the pid of the target process (if it has
3513 such), and as a flag indicating that a target is active. */
3514 inferior_ptid = magic_null_ptid;
3515 fake_pid_p = 1;
3516 }
3517
1b6e6f5c 3518 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3519
3520 /* Add the main thread. */
3521 add_thread_silent (inferior_ptid);
3522}
3523
9cbc821d 3524static void
04bd08de 3525remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3526{
c8d104ad
PA
3527 struct remote_state *rs = get_remote_state ();
3528 struct packet_config *noack_config;
2d717e4f 3529 char *wait_status = NULL;
8621d6a9 3530
23860348 3531 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3532 QUIT;
c906108c 3533
9a7071a8
JB
3534 if (interrupt_on_connect)
3535 send_interrupt_sequence ();
3536
57e12211 3537 /* Ack any packet which the remote side has already sent. */
5d93a237 3538 serial_write (rs->remote_desc, "+", 1);
57e12211 3539
1e51243a
PA
3540 /* Signal other parts that we're going through the initial setup,
3541 and so things may not be stable yet. */
3542 rs->starting_up = 1;
3543
c8d104ad
PA
3544 /* The first packet we send to the target is the optional "supported
3545 packets" request. If the target can answer this, it will tell us
3546 which later probes to skip. */
3547 remote_query_supported ();
3548
d914c394 3549 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3550 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3551 remote_set_permissions (target);
d914c394 3552
c8d104ad
PA
3553 /* Next, we possibly activate noack mode.
3554
3555 If the QStartNoAckMode packet configuration is set to AUTO,
3556 enable noack mode if the stub reported a wish for it with
3557 qSupported.
3558
3559 If set to TRUE, then enable noack mode even if the stub didn't
3560 report it in qSupported. If the stub doesn't reply OK, the
3561 session ends with an error.
3562
3563 If FALSE, then don't activate noack mode, regardless of what the
3564 stub claimed should be the default with qSupported. */
3565
3566 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3567 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3568 {
3569 putpkt ("QStartNoAckMode");
3570 getpkt (&rs->buf, &rs->buf_size, 0);
3571 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3572 rs->noack_mode = 1;
3573 }
3574
04bd08de 3575 if (extended_p)
5fe04517
PA
3576 {
3577 /* Tell the remote that we are using the extended protocol. */
3578 putpkt ("!");
3579 getpkt (&rs->buf, &rs->buf_size, 0);
3580 }
3581
9b224c5e
PA
3582 /* Let the target know which signals it is allowed to pass down to
3583 the program. */
3584 update_signals_program_target ();
3585
d962ef82
DJ
3586 /* Next, if the target can specify a description, read it. We do
3587 this before anything involving memory or registers. */
3588 target_find_description ();
3589
6c95b8df
PA
3590 /* Next, now that we know something about the target, update the
3591 address spaces in the program spaces. */
3592 update_address_spaces ();
3593
50c71eaf
PA
3594 /* On OSs where the list of libraries is global to all
3595 processes, we fetch them early. */
f5656ead 3596 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3597 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3598
74531fed
PA
3599 if (non_stop)
3600 {
4082afcc 3601 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3602 error (_("Non-stop mode requested, but remote "
3603 "does not support non-stop"));
74531fed
PA
3604
3605 putpkt ("QNonStop:1");
3606 getpkt (&rs->buf, &rs->buf_size, 0);
3607
3608 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3609 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3610
3611 /* Find about threads and processes the stub is already
3612 controlling. We default to adding them in the running state.
3613 The '?' query below will then tell us about which threads are
3614 stopped. */
e8032dde 3615 remote_update_thread_list (target);
74531fed 3616 }
4082afcc 3617 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3618 {
3619 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3620 Request it explicitly. */
74531fed
PA
3621 putpkt ("QNonStop:0");
3622 getpkt (&rs->buf, &rs->buf_size, 0);
3623
3624 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3625 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3626 }
3627
a0743c90
YQ
3628 /* Upload TSVs regardless of whether the target is running or not. The
3629 remote stub, such as GDBserver, may have some predefined or builtin
3630 TSVs, even if the target is not running. */
8bd200f1 3631 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3632 {
3633 struct uploaded_tsv *uploaded_tsvs = NULL;
3634
181e3713 3635 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3636 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3637 }
3638
2d717e4f
DJ
3639 /* Check whether the target is running now. */
3640 putpkt ("?");
3641 getpkt (&rs->buf, &rs->buf_size, 0);
3642
74531fed 3643 if (!non_stop)
2d717e4f 3644 {
e714e1bf
UW
3645 ptid_t ptid;
3646 int fake_pid_p = 0;
3647 struct inferior *inf;
3648
74531fed 3649 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3650 {
04bd08de 3651 if (!extended_p)
74531fed 3652 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3653
3654 /* We're connected, but not running. Drop out before we
3655 call start_remote. */
e278ad5b 3656 rs->starting_up = 0;
c35b1492 3657 return;
2d717e4f
DJ
3658 }
3659 else
74531fed 3660 {
74531fed
PA
3661 /* Save the reply for later. */
3662 wait_status = alloca (strlen (rs->buf) + 1);
3663 strcpy (wait_status, rs->buf);
3664 }
3665
b7ea362b 3666 /* Fetch thread list. */
e8032dde 3667 target_update_thread_list ();
b7ea362b 3668
74531fed
PA
3669 /* Let the stub know that we want it to return the thread. */
3670 set_continue_thread (minus_one_ptid);
3671
b7ea362b
PA
3672 if (thread_count () == 0)
3673 {
3674 /* Target has no concept of threads at all. GDB treats
3675 non-threaded target as single-threaded; add a main
3676 thread. */
3677 add_current_inferior_and_thread (wait_status);
3678 }
3679 else
3680 {
3681 /* We have thread information; select the thread the target
3682 says should be current. If we're reconnecting to a
3683 multi-threaded program, this will ideally be the thread
3684 that last reported an event before GDB disconnected. */
3685 inferior_ptid = get_current_thread (wait_status);
3686 if (ptid_equal (inferior_ptid, null_ptid))
3687 {
3688 /* Odd... The target was able to list threads, but not
3689 tell us which thread was current (no "thread"
3690 register in T stop reply?). Just pick the first
3691 thread in the thread list then. */
3692 inferior_ptid = thread_list->ptid;
3693 }
3694 }
74531fed 3695
6e586cc5
YQ
3696 /* init_wait_for_inferior should be called before get_offsets in order
3697 to manage `inserted' flag in bp loc in a correct state.
3698 breakpoint_init_inferior, called from init_wait_for_inferior, set
3699 `inserted' flag to 0, while before breakpoint_re_set, called from
3700 start_remote, set `inserted' flag to 1. In the initialization of
3701 inferior, breakpoint_init_inferior should be called first, and then
3702 breakpoint_re_set can be called. If this order is broken, state of
3703 `inserted' flag is wrong, and cause some problems on breakpoint
3704 manipulation. */
3705 init_wait_for_inferior ();
3706
74531fed
PA
3707 get_offsets (); /* Get text, data & bss offsets. */
3708
d962ef82
DJ
3709 /* If we could not find a description using qXfer, and we know
3710 how to do it some other way, try again. This is not
3711 supported for non-stop; it could be, but it is tricky if
3712 there are no stopped threads when we connect. */
04bd08de 3713 if (remote_read_description_p (target)
f5656ead 3714 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3715 {
3716 target_clear_description ();
3717 target_find_description ();
3718 }
3719
74531fed
PA
3720 /* Use the previously fetched status. */
3721 gdb_assert (wait_status != NULL);
3722 strcpy (rs->buf, wait_status);
3723 rs->cached_wait_status = 1;
3724
3725 immediate_quit--;
04bd08de 3726 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3727 }
3728 else
3729 {
68c97600
PA
3730 /* Clear WFI global state. Do this before finding about new
3731 threads and inferiors, and setting the current inferior.
3732 Otherwise we would clear the proceed status of the current
3733 inferior when we want its stop_soon state to be preserved
3734 (see notice_new_inferior). */
3735 init_wait_for_inferior ();
3736
74531fed
PA
3737 /* In non-stop, we will either get an "OK", meaning that there
3738 are no stopped threads at this time; or, a regular stop
3739 reply. In the latter case, there may be more than one thread
3740 stopped --- we pull them all out using the vStopped
3741 mechanism. */
3742 if (strcmp (rs->buf, "OK") != 0)
3743 {
722247f1 3744 struct notif_client *notif = &notif_client_stop;
2d717e4f 3745
722247f1
YQ
3746 /* remote_notif_get_pending_replies acks this one, and gets
3747 the rest out. */
f48ff2a7 3748 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3749 = remote_notif_parse (notif, rs->buf);
3750 remote_notif_get_pending_events (notif);
c906108c 3751
74531fed
PA
3752 /* Make sure that threads that were stopped remain
3753 stopped. */
3754 iterate_over_threads (set_stop_requested_callback, NULL);
3755 }
2d717e4f 3756
74531fed 3757 if (target_can_async_p ())
6a3753b3 3758 target_async (1);
c906108c 3759
74531fed
PA
3760 if (thread_count () == 0)
3761 {
04bd08de 3762 if (!extended_p)
74531fed 3763 error (_("The target is not running (try extended-remote?)"));
82f73884 3764
c35b1492
PA
3765 /* We're connected, but not running. Drop out before we
3766 call start_remote. */
e278ad5b 3767 rs->starting_up = 0;
c35b1492
PA
3768 return;
3769 }
74531fed
PA
3770
3771 /* Let the stub know that we want it to return the thread. */
c0a2216e 3772
74531fed
PA
3773 /* Force the stub to choose a thread. */
3774 set_general_thread (null_ptid);
c906108c 3775
74531fed
PA
3776 /* Query it. */
3777 inferior_ptid = remote_current_thread (minus_one_ptid);
3778 if (ptid_equal (inferior_ptid, minus_one_ptid))
3779 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3780
74531fed
PA
3781 get_offsets (); /* Get text, data & bss offsets. */
3782
3783 /* In non-stop mode, any cached wait status will be stored in
3784 the stop reply queue. */
3785 gdb_assert (wait_status == NULL);
f0223081 3786
2455069d 3787 /* Report all signals during attach/startup. */
94bedb42 3788 remote_pass_signals (target, 0, NULL);
74531fed 3789 }
c8d104ad 3790
c8d104ad
PA
3791 /* If we connected to a live target, do some additional setup. */
3792 if (target_has_execution)
3793 {
f4ccffad 3794 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3795 remote_check_symbols ();
c8d104ad 3796 }
50c71eaf 3797
d5551862
SS
3798 /* Possibly the target has been engaged in a trace run started
3799 previously; find out where things are at. */
8bd200f1 3800 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3801 {
00bf0b85 3802 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3803
00bf0b85
SS
3804 if (current_trace_status ()->running)
3805 printf_filtered (_("Trace is already running on the target.\n"));
3806
ab6617cc 3807 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3808
3809 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3810 }
3811
1e51243a
PA
3812 /* The thread and inferior lists are now synchronized with the
3813 target, our symbols have been relocated, and we're merged the
3814 target's tracepoints with ours. We're done with basic start
3815 up. */
3816 rs->starting_up = 0;
3817
a25a5a45
PA
3818 /* Maybe breakpoints are global and need to be inserted now. */
3819 if (breakpoints_should_be_inserted_now ())
50c71eaf 3820 insert_breakpoints ();
c906108c
SS
3821}
3822
3823/* Open a connection to a remote debugger.
3824 NAME is the filename used for communication. */
3825
3826static void
014f9477 3827remote_open (const char *name, int from_tty)
c906108c 3828{
75c99385 3829 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3830}
3831
c906108c
SS
3832/* Open a connection to a remote debugger using the extended
3833 remote gdb protocol. NAME is the filename used for communication. */
3834
3835static void
014f9477 3836extended_remote_open (const char *name, int from_tty)
c906108c 3837{
75c99385 3838 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3839}
3840
ca4f7f8b
PA
3841/* Reset all packets back to "unknown support". Called when opening a
3842 new connection to a remote target. */
c906108c 3843
d471ea57 3844static void
ca4f7f8b 3845reset_all_packet_configs_support (void)
d471ea57
AC
3846{
3847 int i;
a744cf53 3848
444abaca 3849 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3850 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3851}
3852
ca4f7f8b
PA
3853/* Initialize all packet configs. */
3854
3855static void
3856init_all_packet_configs (void)
3857{
3858 int i;
3859
3860 for (i = 0; i < PACKET_MAX; i++)
3861 {
3862 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3863 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3864 }
3865}
3866
23860348 3867/* Symbol look-up. */
dc8acb97
MS
3868
3869static void
36d25514 3870remote_check_symbols (void)
dc8acb97 3871{
d01949b6 3872 struct remote_state *rs = get_remote_state ();
dc8acb97 3873 char *msg, *reply, *tmp;
3b7344d5 3874 struct bound_minimal_symbol sym;
dc8acb97
MS
3875 int end;
3876
63154eca
PA
3877 /* The remote side has no concept of inferiors that aren't running
3878 yet, it only knows about running processes. If we're connected
3879 but our current inferior is not running, we should not invite the
3880 remote target to request symbol lookups related to its
3881 (unrelated) current process. */
3882 if (!target_has_execution)
3883 return;
3884
4082afcc 3885 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3886 return;
3887
63154eca
PA
3888 /* Make sure the remote is pointing at the right process. Note
3889 there's no way to select "no process". */
3c9c4b83
PA
3890 set_general_process ();
3891
6d820c5c
DJ
3892 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3893 because we need both at the same time. */
ea9c271d 3894 msg = alloca (get_remote_packet_size ());
6d820c5c 3895
23860348 3896 /* Invite target to request symbol lookups. */
dc8acb97
MS
3897
3898 putpkt ("qSymbol::");
6d820c5c
DJ
3899 getpkt (&rs->buf, &rs->buf_size, 0);
3900 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3901 reply = rs->buf;
dc8acb97 3902
61012eef 3903 while (startswith (reply, "qSymbol:"))
dc8acb97 3904 {
77e371c0
TT
3905 struct bound_minimal_symbol sym;
3906
dc8acb97 3907 tmp = &reply[8];
cfd77fa1 3908 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3909 msg[end] = '\0';
3910 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3911 if (sym.minsym == NULL)
ea9c271d 3912 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3913 else
2bbe3cc1 3914 {
f5656ead 3915 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3916 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3917
3918 /* If this is a function address, return the start of code
3919 instead of any data function descriptor. */
f5656ead 3920 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3921 sym_addr,
3922 &current_target);
3923
3924 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3925 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3926 }
3927
dc8acb97 3928 putpkt (msg);
6d820c5c 3929 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3930 reply = rs->buf;
dc8acb97
MS
3931 }
3932}
3933
9db8d71f 3934static struct serial *
baa336ce 3935remote_serial_open (const char *name)
9db8d71f
DJ
3936{
3937 static int udp_warning = 0;
3938
3939 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3940 of in ser-tcp.c, because it is the remote protocol assuming that the
3941 serial connection is reliable and not the serial connection promising
3942 to be. */
61012eef 3943 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3944 {
3e43a32a
MS
3945 warning (_("The remote protocol may be unreliable over UDP.\n"
3946 "Some events may be lost, rendering further debugging "
3947 "impossible."));
9db8d71f
DJ
3948 udp_warning = 1;
3949 }
3950
3951 return serial_open (name);
3952}
3953
d914c394
SS
3954/* Inform the target of our permission settings. The permission flags
3955 work without this, but if the target knows the settings, it can do
3956 a couple things. First, it can add its own check, to catch cases
3957 that somehow manage to get by the permissions checks in target
3958 methods. Second, if the target is wired to disallow particular
3959 settings (for instance, a system in the field that is not set up to
3960 be able to stop at a breakpoint), it can object to any unavailable
3961 permissions. */
3962
3963void
c378d69d 3964remote_set_permissions (struct target_ops *self)
d914c394
SS
3965{
3966 struct remote_state *rs = get_remote_state ();
3967
bba74b36
YQ
3968 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3969 "WriteReg:%x;WriteMem:%x;"
3970 "InsertBreak:%x;InsertTrace:%x;"
3971 "InsertFastTrace:%x;Stop:%x",
3972 may_write_registers, may_write_memory,
3973 may_insert_breakpoints, may_insert_tracepoints,
3974 may_insert_fast_tracepoints, may_stop);
d914c394
SS
3975 putpkt (rs->buf);
3976 getpkt (&rs->buf, &rs->buf_size, 0);
3977
3978 /* If the target didn't like the packet, warn the user. Do not try
3979 to undo the user's settings, that would just be maddening. */
3980 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 3981 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
3982}
3983
be2a5f71
DJ
3984/* This type describes each known response to the qSupported
3985 packet. */
3986struct protocol_feature
3987{
3988 /* The name of this protocol feature. */
3989 const char *name;
3990
3991 /* The default for this protocol feature. */
3992 enum packet_support default_support;
3993
3994 /* The function to call when this feature is reported, or after
3995 qSupported processing if the feature is not supported.
3996 The first argument points to this structure. The second
3997 argument indicates whether the packet requested support be
3998 enabled, disabled, or probed (or the default, if this function
3999 is being called at the end of processing and this feature was
4000 not reported). The third argument may be NULL; if not NULL, it
4001 is a NUL-terminated string taken from the packet following
4002 this feature's name and an equals sign. */
4003 void (*func) (const struct protocol_feature *, enum packet_support,
4004 const char *);
4005
4006 /* The corresponding packet for this feature. Only used if
4007 FUNC is remote_supported_packet. */
4008 int packet;
4009};
4010
be2a5f71
DJ
4011static void
4012remote_supported_packet (const struct protocol_feature *feature,
4013 enum packet_support support,
4014 const char *argument)
4015{
4016 if (argument)
4017 {
4018 warning (_("Remote qSupported response supplied an unexpected value for"
4019 " \"%s\"."), feature->name);
4020 return;
4021 }
4022
4082afcc 4023 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4024}
be2a5f71
DJ
4025
4026static void
4027remote_packet_size (const struct protocol_feature *feature,
4028 enum packet_support support, const char *value)
4029{
4030 struct remote_state *rs = get_remote_state ();
4031
4032 int packet_size;
4033 char *value_end;
4034
4035 if (support != PACKET_ENABLE)
4036 return;
4037
4038 if (value == NULL || *value == '\0')
4039 {
4040 warning (_("Remote target reported \"%s\" without a size."),
4041 feature->name);
4042 return;
4043 }
4044
4045 errno = 0;
4046 packet_size = strtol (value, &value_end, 16);
4047 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4048 {
4049 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4050 feature->name, value);
4051 return;
4052 }
4053
4054 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4055 {
4056 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4057 packet_size, MAX_REMOTE_PACKET_SIZE);
4058 packet_size = MAX_REMOTE_PACKET_SIZE;
4059 }
4060
4061 /* Record the new maximum packet size. */
4062 rs->explicit_packet_size = packet_size;
4063}
4064
dc473cfb 4065static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4066 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4067 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4068 PACKET_qXfer_auxv },
c78fa86a
GB
4069 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4070 PACKET_qXfer_exec_file },
23181151
DJ
4071 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4072 PACKET_qXfer_features },
cfa9d6d9
DJ
4073 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4074 PACKET_qXfer_libraries },
2268b414
JK
4075 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4076 PACKET_qXfer_libraries_svr4 },
ced63ec0 4077 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4078 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4079 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4080 PACKET_qXfer_memory_map },
4de6483e
UW
4081 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4082 PACKET_qXfer_spu_read },
4083 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4084 PACKET_qXfer_spu_write },
07e059b5
VP
4085 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4086 PACKET_qXfer_osdata },
dc146f7c
VP
4087 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4088 PACKET_qXfer_threads },
b3b9301e
PA
4089 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4090 PACKET_qXfer_traceframe_info },
89be2091
DJ
4091 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4092 PACKET_QPassSignals },
9b224c5e
PA
4093 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4094 PACKET_QProgramSignals },
a6f3e723
SL
4095 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4096 PACKET_QStartNoAckMode },
4082afcc
PA
4097 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4098 PACKET_multiprocess_feature },
4099 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4100 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4101 PACKET_qXfer_siginfo_read },
4102 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4103 PACKET_qXfer_siginfo_write },
4082afcc 4104 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4105 PACKET_ConditionalTracepoints },
4082afcc 4106 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4107 PACKET_ConditionalBreakpoints },
4082afcc 4108 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4109 PACKET_BreakpointCommands },
4082afcc 4110 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4111 PACKET_FastTracepoints },
4082afcc 4112 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4113 PACKET_StaticTracepoints },
4082afcc 4114 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4115 PACKET_InstallInTrace},
4082afcc
PA
4116 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4117 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4118 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4119 PACKET_bc },
4120 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4121 PACKET_bs },
409873ef
SS
4122 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4123 PACKET_TracepointSource },
d914c394
SS
4124 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4125 PACKET_QAllow },
4082afcc
PA
4126 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4127 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4128 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4129 PACKET_qXfer_fdpic },
169081d0
TG
4130 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4131 PACKET_qXfer_uib },
03583c20
UW
4132 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4133 PACKET_QDisableRandomization },
d1feda86 4134 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4135 { "QTBuffer:size", PACKET_DISABLE,
4136 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4137 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4138 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4139 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4140 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4141 PACKET_qXfer_btrace },
4142 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4143 PACKET_qXfer_btrace_conf },
4144 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4145 PACKET_Qbtrace_conf_bts_size },
4146 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4147 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4148 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4149 PACKET_fork_event_feature },
4150 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4151 PACKET_vfork_event_feature },
be2a5f71
DJ
4152};
4153
c8d5aac9
L
4154static char *remote_support_xml;
4155
4156/* Register string appended to "xmlRegisters=" in qSupported query. */
4157
4158void
6e39997a 4159register_remote_support_xml (const char *xml)
c8d5aac9
L
4160{
4161#if defined(HAVE_LIBEXPAT)
4162 if (remote_support_xml == NULL)
c4f7c687 4163 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4164 else
4165 {
4166 char *copy = xstrdup (remote_support_xml + 13);
4167 char *p = strtok (copy, ",");
4168
4169 do
4170 {
4171 if (strcmp (p, xml) == 0)
4172 {
4173 /* already there */
4174 xfree (copy);
4175 return;
4176 }
4177 }
4178 while ((p = strtok (NULL, ",")) != NULL);
4179 xfree (copy);
4180
94b0dee1
PA
4181 remote_support_xml = reconcat (remote_support_xml,
4182 remote_support_xml, ",", xml,
4183 (char *) NULL);
c8d5aac9
L
4184 }
4185#endif
4186}
4187
4188static char *
4189remote_query_supported_append (char *msg, const char *append)
4190{
4191 if (msg)
94b0dee1 4192 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4193 else
4194 return xstrdup (append);
4195}
4196
be2a5f71
DJ
4197static void
4198remote_query_supported (void)
4199{
4200 struct remote_state *rs = get_remote_state ();
4201 char *next;
4202 int i;
4203 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4204
4205 /* The packet support flags are handled differently for this packet
4206 than for most others. We treat an error, a disabled packet, and
4207 an empty response identically: any features which must be reported
4208 to be used will be automatically disabled. An empty buffer
4209 accomplishes this, since that is also the representation for a list
4210 containing no features. */
4211
4212 rs->buf[0] = 0;
4082afcc 4213 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4214 {
c8d5aac9 4215 char *q = NULL;
94b0dee1 4216 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4217
901f9912 4218 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4219
f7e6eed5
PA
4220 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4221 q = remote_query_supported_append (q, "swbreak+");
4222 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4223 q = remote_query_supported_append (q, "hwbreak+");
4224
c8d5aac9
L
4225 if (remote_support_xml)
4226 q = remote_query_supported_append (q, remote_support_xml);
4227
dde08ee1
PA
4228 q = remote_query_supported_append (q, "qRelocInsn+");
4229
89245bc0
DB
4230 if (rs->extended)
4231 {
4232 if (packet_set_cmd_state (PACKET_fork_event_feature)
4233 != AUTO_BOOLEAN_FALSE)
4234 q = remote_query_supported_append (q, "fork-events+");
4235 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4236 != AUTO_BOOLEAN_FALSE)
4237 q = remote_query_supported_append (q, "vfork-events+");
4238 }
4239
dde08ee1
PA
4240 q = reconcat (q, "qSupported:", q, (char *) NULL);
4241 putpkt (q);
82f73884 4242
94b0dee1
PA
4243 do_cleanups (old_chain);
4244
be2a5f71
DJ
4245 getpkt (&rs->buf, &rs->buf_size, 0);
4246
4247 /* If an error occured, warn, but do not return - just reset the
4248 buffer to empty and go on to disable features. */
4249 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4250 == PACKET_ERROR)
4251 {
4252 warning (_("Remote failure reply: %s"), rs->buf);
4253 rs->buf[0] = 0;
4254 }
4255 }
4256
4257 memset (seen, 0, sizeof (seen));
4258
4259 next = rs->buf;
4260 while (*next)
4261 {
4262 enum packet_support is_supported;
4263 char *p, *end, *name_end, *value;
4264
4265 /* First separate out this item from the rest of the packet. If
4266 there's another item after this, we overwrite the separator
4267 (terminated strings are much easier to work with). */
4268 p = next;
4269 end = strchr (p, ';');
4270 if (end == NULL)
4271 {
4272 end = p + strlen (p);
4273 next = end;
4274 }
4275 else
4276 {
89be2091
DJ
4277 *end = '\0';
4278 next = end + 1;
4279
be2a5f71
DJ
4280 if (end == p)
4281 {
4282 warning (_("empty item in \"qSupported\" response"));
4283 continue;
4284 }
be2a5f71
DJ
4285 }
4286
4287 name_end = strchr (p, '=');
4288 if (name_end)
4289 {
4290 /* This is a name=value entry. */
4291 is_supported = PACKET_ENABLE;
4292 value = name_end + 1;
4293 *name_end = '\0';
4294 }
4295 else
4296 {
4297 value = NULL;
4298 switch (end[-1])
4299 {
4300 case '+':
4301 is_supported = PACKET_ENABLE;
4302 break;
4303
4304 case '-':
4305 is_supported = PACKET_DISABLE;
4306 break;
4307
4308 case '?':
4309 is_supported = PACKET_SUPPORT_UNKNOWN;
4310 break;
4311
4312 default:
3e43a32a
MS
4313 warning (_("unrecognized item \"%s\" "
4314 "in \"qSupported\" response"), p);
be2a5f71
DJ
4315 continue;
4316 }
4317 end[-1] = '\0';
4318 }
4319
4320 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4321 if (strcmp (remote_protocol_features[i].name, p) == 0)
4322 {
4323 const struct protocol_feature *feature;
4324
4325 seen[i] = 1;
4326 feature = &remote_protocol_features[i];
4327 feature->func (feature, is_supported, value);
4328 break;
4329 }
4330 }
4331
4332 /* If we increased the packet size, make sure to increase the global
4333 buffer size also. We delay this until after parsing the entire
4334 qSupported packet, because this is the same buffer we were
4335 parsing. */
4336 if (rs->buf_size < rs->explicit_packet_size)
4337 {
4338 rs->buf_size = rs->explicit_packet_size;
4339 rs->buf = xrealloc (rs->buf, rs->buf_size);
4340 }
4341
4342 /* Handle the defaults for unmentioned features. */
4343 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4344 if (!seen[i])
4345 {
4346 const struct protocol_feature *feature;
4347
4348 feature = &remote_protocol_features[i];
4349 feature->func (feature, feature->default_support, NULL);
4350 }
4351}
4352
78a095c3
JK
4353/* Remove any of the remote.c targets from target stack. Upper targets depend
4354 on it so remove them first. */
4355
4356static void
4357remote_unpush_target (void)
4358{
4359 pop_all_targets_above (process_stratum - 1);
4360}
be2a5f71 4361
c906108c 4362static void
014f9477 4363remote_open_1 (const char *name, int from_tty,
3e43a32a 4364 struct target_ops *target, int extended_p)
c906108c 4365{
d01949b6 4366 struct remote_state *rs = get_remote_state ();
a6f3e723 4367
c906108c 4368 if (name == 0)
8a3fe4f8 4369 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4370 "serial device is attached to the remote system\n"
8a3fe4f8 4371 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4372
23860348 4373 /* See FIXME above. */
c6ebd6cf 4374 if (!target_async_permitted)
92d1e331 4375 wait_forever_enabled_p = 1;
6426a772 4376
2d717e4f 4377 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4378 Ask this question first, before target_preopen has a chance to kill
4379 anything. */
5d93a237 4380 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4381 {
78a095c3
JK
4382 if (from_tty
4383 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4384 error (_("Still connected."));
4385 }
4386
78a095c3 4387 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4388 target_preopen (from_tty);
4389
89be2091 4390 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4391 xfree (rs->last_pass_packet);
4392 rs->last_pass_packet = NULL;
89be2091 4393
9b224c5e
PA
4394 /* Make sure we send the program signals list the next time we
4395 resume. */
5e4a05c4
TT
4396 xfree (rs->last_program_signals_packet);
4397 rs->last_program_signals_packet = NULL;
9b224c5e 4398
ad9a8f3f 4399 remote_fileio_reset ();
1dd41f16
NS
4400 reopen_exec_file ();
4401 reread_symbols ();
4402
5d93a237
TT
4403 rs->remote_desc = remote_serial_open (name);
4404 if (!rs->remote_desc)
c906108c
SS
4405 perror_with_name (name);
4406
4407 if (baud_rate != -1)
4408 {
5d93a237 4409 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4410 {
9b74d5d3
KB
4411 /* The requested speed could not be set. Error out to
4412 top level after closing remote_desc. Take care to
4413 set remote_desc to NULL to avoid closing remote_desc
4414 more than once. */
5d93a237
TT
4415 serial_close (rs->remote_desc);
4416 rs->remote_desc = NULL;
c906108c
SS
4417 perror_with_name (name);
4418 }
4419 }
4420
236af5e3 4421 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4422 serial_raw (rs->remote_desc);
c906108c
SS
4423
4424 /* If there is something sitting in the buffer we might take it as a
4425 response to a command, which would be bad. */
5d93a237 4426 serial_flush_input (rs->remote_desc);
c906108c
SS
4427
4428 if (from_tty)
4429 {
4430 puts_filtered ("Remote debugging using ");
4431 puts_filtered (name);
4432 puts_filtered ("\n");
4433 }
23860348 4434 push_target (target); /* Switch to using remote target now. */
c906108c 4435
74531fed
PA
4436 /* Register extra event sources in the event loop. */
4437 remote_async_inferior_event_token
4438 = create_async_event_handler (remote_async_inferior_event_handler,
4439 NULL);
5965e028 4440 rs->notif_state = remote_notif_state_allocate ();
74531fed 4441
be2a5f71
DJ
4442 /* Reset the target state; these things will be queried either by
4443 remote_query_supported or as they are needed. */
ca4f7f8b 4444 reset_all_packet_configs_support ();
74531fed 4445 rs->cached_wait_status = 0;
be2a5f71 4446 rs->explicit_packet_size = 0;
a6f3e723 4447 rs->noack_mode = 0;
82f73884 4448 rs->extended = extended_p;
e24a49d8 4449 rs->waiting_for_stop_reply = 0;
3a29589a 4450 rs->ctrlc_pending_p = 0;
802188a7 4451
47f8a51d
TT
4452 rs->general_thread = not_sent_ptid;
4453 rs->continue_thread = not_sent_ptid;
262e1174 4454 rs->remote_traceframe_number = -1;
c906108c 4455
9d1f7ab2 4456 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4457 rs->use_threadinfo_query = 1;
4458 rs->use_threadextra_query = 1;
9d1f7ab2 4459
c6ebd6cf 4460 if (target_async_permitted)
92d1e331 4461 {
23860348 4462 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4463 remote_async_terminal_ours_p = 1;
4464
4465 /* FIXME: cagney/1999-09-23: During the initial connection it is
4466 assumed that the target is already ready and able to respond to
0df8b418 4467 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4468 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4469 around this. Eventually a mechanism that allows
92d1e331 4470 wait_for_inferior() to expect/get timeouts will be
23860348 4471 implemented. */
92d1e331
DJ
4472 wait_forever_enabled_p = 0;
4473 }
4474
23860348 4475 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4476 no_shared_libraries (NULL, 0);
f78f6cf1 4477
74531fed
PA
4478 /* Start afresh. */
4479 init_thread_list ();
4480
36918e70 4481 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4482 target (we'd otherwise be in an inconsistent state) and then
4483 propogate the error on up the exception chain. This ensures that
4484 the caller doesn't stumble along blindly assuming that the
4485 function succeeded. The CLI doesn't have this problem but other
4486 UI's, such as MI do.
36918e70
AC
4487
4488 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4489 this function should return an error indication letting the
ce2826aa 4490 caller restore the previous state. Unfortunately the command
36918e70
AC
4491 ``target remote'' is directly wired to this function making that
4492 impossible. On a positive note, the CLI side of this problem has
4493 been fixed - the function set_cmd_context() makes it possible for
4494 all the ``target ....'' commands to share a common callback
4495 function. See cli-dump.c. */
109c3e39 4496 {
2d717e4f 4497
492d29ea 4498 TRY
04bd08de
TT
4499 {
4500 remote_start_remote (from_tty, target, extended_p);
4501 }
492d29ea 4502 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4503 {
c8d104ad
PA
4504 /* Pop the partially set up target - unless something else did
4505 already before throwing the exception. */
5d93a237 4506 if (rs->remote_desc != NULL)
78a095c3 4507 remote_unpush_target ();
c6ebd6cf 4508 if (target_async_permitted)
109c3e39
AC
4509 wait_forever_enabled_p = 1;
4510 throw_exception (ex);
4511 }
492d29ea 4512 END_CATCH
109c3e39 4513 }
c906108c 4514
f4abbc16
MM
4515 remote_btrace_reset ();
4516
c6ebd6cf 4517 if (target_async_permitted)
92d1e331 4518 wait_forever_enabled_p = 1;
43ff13b4
JM
4519}
4520
de0d863e
DB
4521/* Detach the specified process. */
4522
4523static void
4524remote_detach_pid (int pid)
4525{
4526 struct remote_state *rs = get_remote_state ();
4527
4528 if (remote_multi_process_p (rs))
4529 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4530 else
4531 strcpy (rs->buf, "D");
4532
4533 putpkt (rs->buf);
4534 getpkt (&rs->buf, &rs->buf_size, 0);
4535
4536 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4537 ;
4538 else if (rs->buf[0] == '\0')
4539 error (_("Remote doesn't know how to detach"));
4540 else
4541 error (_("Can't detach process."));
4542}
4543
4544/* This detaches a program to which we previously attached, using
4545 inferior_ptid to identify the process. After this is done, GDB
4546 can be used to debug some other program. We better not have left
4547 any breakpoints in the target program or it'll die when it hits
4548 one. */
c906108c
SS
4549
4550static void
de0d863e 4551remote_detach_1 (const char *args, int from_tty)
c906108c 4552{
82f73884 4553 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4554 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4555 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4556 int is_fork_parent;
c906108c
SS
4557
4558 if (args)
8a3fe4f8 4559 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4560
2d717e4f
DJ
4561 if (!target_has_execution)
4562 error (_("No process to detach from."));
4563
7cee1e54
PA
4564 if (from_tty)
4565 {
4566 char *exec_file = get_exec_file (0);
4567 if (exec_file == NULL)
4568 exec_file = "";
4569 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4570 target_pid_to_str (pid_to_ptid (pid)));
4571 gdb_flush (gdb_stdout);
4572 }
4573
c906108c 4574 /* Tell the remote target to detach. */
de0d863e 4575 remote_detach_pid (pid);
82f73884 4576
de0d863e 4577 if (from_tty && !rs->extended)
7cee1e54 4578 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4579
de0d863e
DB
4580 /* Check to see if we are detaching a fork parent. Note that if we
4581 are detaching a fork child, tp == NULL. */
4582 is_fork_parent = (tp != NULL
4583 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4584
4585 /* If doing detach-on-fork, we don't mourn, because that will delete
4586 breakpoints that should be available for the followed inferior. */
4587 if (!is_fork_parent)
4588 target_mourn_inferior ();
4589 else
4590 {
4591 inferior_ptid = null_ptid;
4592 detach_inferior (pid);
4593 }
2d717e4f
DJ
4594}
4595
4596static void
52554a0e 4597remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4598{
de0d863e 4599 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4600}
4601
4602static void
52554a0e 4603extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4604{
de0d863e
DB
4605 remote_detach_1 (args, from_tty);
4606}
4607
4608/* Target follow-fork function for remote targets. On entry, and
4609 at return, the current inferior is the fork parent.
4610
4611 Note that although this is currently only used for extended-remote,
4612 it is named remote_follow_fork in anticipation of using it for the
4613 remote target as well. */
4614
4615static int
4616remote_follow_fork (struct target_ops *ops, int follow_child,
4617 int detach_fork)
4618{
4619 struct remote_state *rs = get_remote_state ();
c269dbdb 4620 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4621
c269dbdb
DB
4622 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4623 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4624 {
4625 /* When following the parent and detaching the child, we detach
4626 the child here. For the case of following the child and
4627 detaching the parent, the detach is done in the target-
4628 independent follow fork code in infrun.c. We can't use
4629 target_detach when detaching an unfollowed child because
4630 the client side doesn't know anything about the child. */
4631 if (detach_fork && !follow_child)
4632 {
4633 /* Detach the fork child. */
4634 ptid_t child_ptid;
4635 pid_t child_pid;
4636
4637 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4638 child_pid = ptid_get_pid (child_ptid);
4639
4640 remote_detach_pid (child_pid);
4641 detach_inferior (child_pid);
4642 }
4643 }
4644 return 0;
c906108c
SS
4645}
4646
6ad8ae5c
DJ
4647/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4648
43ff13b4 4649static void
fee354ee 4650remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4651{
43ff13b4 4652 if (args)
2d717e4f 4653 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4654
2d717e4f 4655 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4656 won't do it. So call remote_mourn directly instead of
2d717e4f 4657 target_mourn_inferior. */
20f796c9 4658 remote_mourn (target);
2d717e4f 4659
43ff13b4
JM
4660 if (from_tty)
4661 puts_filtered ("Ending remote debugging.\n");
4662}
4663
2d717e4f
DJ
4664/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4665 be chatty about it. */
4666
4667static void
20f796c9
GB
4668extended_remote_attach (struct target_ops *target, const char *args,
4669 int from_tty)
2d717e4f
DJ
4670{
4671 struct remote_state *rs = get_remote_state ();
be86555c 4672 int pid;
96ef3384 4673 char *wait_status = NULL;
2d717e4f 4674
74164c56 4675 pid = parse_pid_to_attach (args);
2d717e4f 4676
74164c56
JK
4677 /* Remote PID can be freely equal to getpid, do not check it here the same
4678 way as in other targets. */
2d717e4f 4679
4082afcc 4680 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4681 error (_("This target does not support attaching to a process"));
4682
7cee1e54
PA
4683 if (from_tty)
4684 {
4685 char *exec_file = get_exec_file (0);
4686
4687 if (exec_file)
4688 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4689 target_pid_to_str (pid_to_ptid (pid)));
4690 else
4691 printf_unfiltered (_("Attaching to %s\n"),
4692 target_pid_to_str (pid_to_ptid (pid)));
4693
4694 gdb_flush (gdb_stdout);
4695 }
4696
bba74b36 4697 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4698 putpkt (rs->buf);
4699 getpkt (&rs->buf, &rs->buf_size, 0);
4700
4082afcc
PA
4701 switch (packet_ok (rs->buf,
4702 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4703 {
4082afcc 4704 case PACKET_OK:
74531fed
PA
4705 if (!non_stop)
4706 {
4707 /* Save the reply for later. */
4708 wait_status = alloca (strlen (rs->buf) + 1);
4709 strcpy (wait_status, rs->buf);
4710 }
4711 else if (strcmp (rs->buf, "OK") != 0)
4712 error (_("Attaching to %s failed with: %s"),
4713 target_pid_to_str (pid_to_ptid (pid)),
4714 rs->buf);
4082afcc
PA
4715 break;
4716 case PACKET_UNKNOWN:
4717 error (_("This target does not support attaching to a process"));
4718 default:
4719 error (_("Attaching to %s failed"),
4720 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4721 }
2d717e4f 4722
1b6e6f5c 4723 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4724
2d717e4f 4725 inferior_ptid = pid_to_ptid (pid);
79d7f229 4726
bad34192
PA
4727 if (non_stop)
4728 {
4729 struct thread_info *thread;
79d7f229 4730
bad34192 4731 /* Get list of threads. */
e8032dde 4732 remote_update_thread_list (target);
82f73884 4733
bad34192
PA
4734 thread = first_thread_of_process (pid);
4735 if (thread)
4736 inferior_ptid = thread->ptid;
4737 else
4738 inferior_ptid = pid_to_ptid (pid);
4739
4740 /* Invalidate our notion of the remote current thread. */
47f8a51d 4741 record_currthread (rs, minus_one_ptid);
bad34192 4742 }
74531fed 4743 else
bad34192
PA
4744 {
4745 /* Now, if we have thread information, update inferior_ptid. */
4746 inferior_ptid = remote_current_thread (inferior_ptid);
4747
4748 /* Add the main thread to the thread list. */
4749 add_thread_silent (inferior_ptid);
4750 }
c0a2216e 4751
96ef3384
UW
4752 /* Next, if the target can specify a description, read it. We do
4753 this before anything involving memory or registers. */
4754 target_find_description ();
4755
74531fed
PA
4756 if (!non_stop)
4757 {
4758 /* Use the previously fetched status. */
4759 gdb_assert (wait_status != NULL);
4760
4761 if (target_can_async_p ())
4762 {
722247f1
YQ
4763 struct notif_event *reply
4764 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4765
722247f1 4766 push_stop_reply ((struct stop_reply *) reply);
74531fed 4767
6a3753b3 4768 target_async (1);
74531fed
PA
4769 }
4770 else
4771 {
4772 gdb_assert (wait_status != NULL);
4773 strcpy (rs->buf, wait_status);
4774 rs->cached_wait_status = 1;
4775 }
4776 }
4777 else
4778 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4779}
4780
b9c1d481
AS
4781/* Implementation of the to_post_attach method. */
4782
4783static void
4784extended_remote_post_attach (struct target_ops *ops, int pid)
4785{
4786 /* In certain cases GDB might not have had the chance to start
4787 symbol lookup up until now. This could happen if the debugged
4788 binary is not using shared libraries, the vsyscall page is not
4789 present (on Linux) and the binary itself hadn't changed since the
4790 debugging process was started. */
4791 if (symfile_objfile != NULL)
4792 remote_check_symbols();
4793}
4794
c906108c 4795\f
506fb367
DJ
4796/* Check for the availability of vCont. This function should also check
4797 the response. */
c906108c
SS
4798
4799static void
6d820c5c 4800remote_vcont_probe (struct remote_state *rs)
c906108c 4801{
2e9f7625 4802 char *buf;
6d820c5c 4803
2e9f7625
DJ
4804 strcpy (rs->buf, "vCont?");
4805 putpkt (rs->buf);
6d820c5c 4806 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4807 buf = rs->buf;
c906108c 4808
506fb367 4809 /* Make sure that the features we assume are supported. */
61012eef 4810 if (startswith (buf, "vCont"))
506fb367
DJ
4811 {
4812 char *p = &buf[5];
4813 int support_s, support_S, support_c, support_C;
4814
4815 support_s = 0;
4816 support_S = 0;
4817 support_c = 0;
4818 support_C = 0;
d458bd84 4819 rs->supports_vCont.t = 0;
c1e36e3e 4820 rs->supports_vCont.r = 0;
506fb367
DJ
4821 while (p && *p == ';')
4822 {
4823 p++;
4824 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4825 support_s = 1;
4826 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4827 support_S = 1;
4828 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4829 support_c = 1;
4830 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4831 support_C = 1;
74531fed 4832 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4833 rs->supports_vCont.t = 1;
c1e36e3e
PA
4834 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4835 rs->supports_vCont.r = 1;
506fb367
DJ
4836
4837 p = strchr (p, ';');
4838 }
c906108c 4839
506fb367
DJ
4840 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4841 BUF will make packet_ok disable the packet. */
4842 if (!support_s || !support_S || !support_c || !support_C)
4843 buf[0] = 0;
4844 }
c906108c 4845
444abaca 4846 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4847}
c906108c 4848
0d8f58ca
PA
4849/* Helper function for building "vCont" resumptions. Write a
4850 resumption to P. ENDP points to one-passed-the-end of the buffer
4851 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4852 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4853 resumed thread should be single-stepped and/or signalled. If PTID
4854 equals minus_one_ptid, then all threads are resumed; if PTID
4855 represents a process, then all threads of the process are resumed;
4856 the thread to be stepped and/or signalled is given in the global
4857 INFERIOR_PTID. */
4858
4859static char *
4860append_resumption (char *p, char *endp,
2ea28649 4861 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4862{
4863 struct remote_state *rs = get_remote_state ();
4864
a493e3e2 4865 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4866 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4867 else if (step
4868 /* GDB is willing to range step. */
4869 && use_range_stepping
4870 /* Target supports range stepping. */
4871 && rs->supports_vCont.r
4872 /* We don't currently support range stepping multiple
4873 threads with a wildcard (though the protocol allows it,
4874 so stubs shouldn't make an active effort to forbid
4875 it). */
4876 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4877 {
4878 struct thread_info *tp;
4879
4880 if (ptid_equal (ptid, minus_one_ptid))
4881 {
4882 /* If we don't know about the target thread's tid, then
4883 we're resuming magic_null_ptid (see caller). */
4884 tp = find_thread_ptid (magic_null_ptid);
4885 }
4886 else
4887 tp = find_thread_ptid (ptid);
4888 gdb_assert (tp != NULL);
4889
4890 if (tp->control.may_range_step)
4891 {
4892 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4893
4894 p += xsnprintf (p, endp - p, ";r%s,%s",
4895 phex_nz (tp->control.step_range_start,
4896 addr_size),
4897 phex_nz (tp->control.step_range_end,
4898 addr_size));
4899 }
4900 else
4901 p += xsnprintf (p, endp - p, ";s");
4902 }
0d8f58ca
PA
4903 else if (step)
4904 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4905 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4906 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4907 else
4908 p += xsnprintf (p, endp - p, ";c");
4909
4910 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4911 {
4912 ptid_t nptid;
4913
4914 /* All (-1) threads of process. */
ba348170 4915 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4916
4917 p += xsnprintf (p, endp - p, ":");
4918 p = write_ptid (p, endp, nptid);
4919 }
4920 else if (!ptid_equal (ptid, minus_one_ptid))
4921 {
4922 p += xsnprintf (p, endp - p, ":");
4923 p = write_ptid (p, endp, ptid);
4924 }
4925
4926 return p;
4927}
4928
e5ef252a
PA
4929/* Append a vCont continue-with-signal action for threads that have a
4930 non-zero stop signal. */
4931
4932static char *
4933append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4934{
4935 struct thread_info *thread;
4936
034f788c 4937 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4938 if (ptid_match (thread->ptid, ptid)
4939 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4940 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4941 {
4942 p = append_resumption (p, endp, thread->ptid,
4943 0, thread->suspend.stop_signal);
4944 thread->suspend.stop_signal = GDB_SIGNAL_0;
4945 }
4946
4947 return p;
4948}
4949
506fb367
DJ
4950/* Resume the remote inferior by using a "vCont" packet. The thread
4951 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4952 resumed thread should be single-stepped and/or signalled. If PTID
4953 equals minus_one_ptid, then all threads are resumed; the thread to
4954 be stepped and/or signalled is given in the global INFERIOR_PTID.
4955 This function returns non-zero iff it resumes the inferior.
44eaed12 4956
506fb367
DJ
4957 This function issues a strict subset of all possible vCont commands at the
4958 moment. */
44eaed12 4959
506fb367 4960static int
2ea28649 4961remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
4962{
4963 struct remote_state *rs = get_remote_state ();
82f73884
PA
4964 char *p;
4965 char *endp;
44eaed12 4966
4082afcc 4967 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 4968 remote_vcont_probe (rs);
44eaed12 4969
4082afcc 4970 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 4971 return 0;
44eaed12 4972
82f73884
PA
4973 p = rs->buf;
4974 endp = rs->buf + get_remote_packet_size ();
4975
506fb367
DJ
4976 /* If we could generate a wider range of packets, we'd have to worry
4977 about overflowing BUF. Should there be a generic
4978 "multi-part-packet" packet? */
4979
0d8f58ca
PA
4980 p += xsnprintf (p, endp - p, "vCont");
4981
79d7f229 4982 if (ptid_equal (ptid, magic_null_ptid))
c906108c 4983 {
79d7f229
PA
4984 /* MAGIC_NULL_PTID means that we don't have any active threads,
4985 so we don't have any TID numbers the inferior will
4986 understand. Make sure to only send forms that do not specify
4987 a TID. */
a9cbf802 4988 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 4989 }
0d8f58ca 4990 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 4991 {
0d8f58ca
PA
4992 /* Resume all threads (of all processes, or of a single
4993 process), with preference for INFERIOR_PTID. This assumes
4994 inferior_ptid belongs to the set of all threads we are about
4995 to resume. */
a493e3e2 4996 if (step || siggnal != GDB_SIGNAL_0)
82f73884 4997 {
0d8f58ca
PA
4998 /* Step inferior_ptid, with or without signal. */
4999 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5000 }
0d8f58ca 5001
e5ef252a
PA
5002 /* Also pass down any pending signaled resumption for other
5003 threads not the current. */
5004 p = append_pending_thread_resumptions (p, endp, ptid);
5005
0d8f58ca 5006 /* And continue others without a signal. */
a493e3e2 5007 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5008 }
5009 else
506fb367
DJ
5010 {
5011 /* Scheduler locking; resume only PTID. */
a9cbf802 5012 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5013 }
c906108c 5014
82f73884
PA
5015 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5016 putpkt (rs->buf);
506fb367 5017
74531fed
PA
5018 if (non_stop)
5019 {
5020 /* In non-stop, the stub replies to vCont with "OK". The stop
5021 reply will be reported asynchronously by means of a `%Stop'
5022 notification. */
5023 getpkt (&rs->buf, &rs->buf_size, 0);
5024 if (strcmp (rs->buf, "OK") != 0)
5025 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5026 }
5027
506fb367 5028 return 1;
c906108c 5029}
43ff13b4 5030
506fb367
DJ
5031/* Tell the remote machine to resume. */
5032
43ff13b4 5033static void
28439f5e 5034remote_resume (struct target_ops *ops,
2ea28649 5035 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5036{
d01949b6 5037 struct remote_state *rs = get_remote_state ();
2e9f7625 5038 char *buf;
43ff13b4 5039
722247f1
YQ
5040 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5041 (explained in remote-notif.c:handle_notification) so
5042 remote_notif_process is not called. We need find a place where
5043 it is safe to start a 'vNotif' sequence. It is good to do it
5044 before resuming inferior, because inferior was stopped and no RSP
5045 traffic at that moment. */
5046 if (!non_stop)
5965e028 5047 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5048
b73be471 5049 rs->last_sent_signal = siggnal;
280ceea3 5050 rs->last_sent_step = step;
43ff13b4 5051
506fb367 5052 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5053 /* No reverse support (yet) for vCont. */
5054 if (execution_direction != EXEC_REVERSE)
5055 if (remote_vcont_resume (ptid, step, siggnal))
5056 goto done;
506fb367 5057
79d7f229
PA
5058 /* All other supported resume packets do use Hc, so set the continue
5059 thread. */
5060 if (ptid_equal (ptid, minus_one_ptid))
5061 set_continue_thread (any_thread_ptid);
506fb367 5062 else
79d7f229 5063 set_continue_thread (ptid);
506fb367 5064
2e9f7625 5065 buf = rs->buf;
b2175913
MS
5066 if (execution_direction == EXEC_REVERSE)
5067 {
5068 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5069 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5070 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5071 siggnal);
40ab02ce 5072
4082afcc 5073 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5074 error (_("Remote reverse-step not supported."));
4082afcc 5075 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5076 error (_("Remote reverse-continue not supported."));
40ab02ce 5077
b2175913
MS
5078 strcpy (buf, step ? "bs" : "bc");
5079 }
a493e3e2 5080 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5081 {
5082 buf[0] = step ? 'S' : 'C';
c5aa993b 5083 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5084 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5085 buf[3] = '\0';
5086 }
5087 else
c5aa993b 5088 strcpy (buf, step ? "s" : "c");
506fb367 5089
44eaed12 5090 putpkt (buf);
43ff13b4 5091
75c99385 5092 done:
2acceee2 5093 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5094 with the event loop. NOTE: this is the one place where all the
5095 execution commands end up. We could alternatively do this in each
23860348 5096 of the execution commands in infcmd.c. */
2acceee2
JM
5097 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5098 into infcmd.c in order to allow inferior function calls to work
23860348 5099 NOT asynchronously. */
362646f5 5100 if (target_can_async_p ())
6a3753b3 5101 target_async (1);
e24a49d8
PA
5102
5103 /* We've just told the target to resume. The remote server will
5104 wait for the inferior to stop, and then send a stop reply. In
5105 the mean time, we can't start another command/query ourselves
74531fed
PA
5106 because the stub wouldn't be ready to process it. This applies
5107 only to the base all-stop protocol, however. In non-stop (which
5108 only supports vCont), the stub replies with an "OK", and is
5109 immediate able to process further serial input. */
5110 if (!non_stop)
5111 rs->waiting_for_stop_reply = 1;
43ff13b4 5112}
c906108c 5113\f
43ff13b4
JM
5114
5115/* Set up the signal handler for SIGINT, while the target is
23860348 5116 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5117static void
934b9bac 5118async_initialize_sigint_signal_handler (void)
43ff13b4 5119{
934b9bac 5120 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5121}
5122
23860348 5123/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5124static void
934b9bac 5125async_handle_remote_sigint (int sig)
43ff13b4 5126{
934b9bac 5127 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5128 /* Note we need to go through gdb_call_async_signal_handler in order
5129 to wake up the event loop on Windows. */
5130 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5131}
5132
5133/* Signal handler for SIGINT, installed after SIGINT has already been
5134 sent once. It will take effect the second time that the user sends
23860348 5135 a ^C. */
43ff13b4 5136static void
934b9bac 5137async_handle_remote_sigint_twice (int sig)
43ff13b4 5138{
934b9bac 5139 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5140 /* See note in async_handle_remote_sigint. */
5141 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5142}
5143
6426a772 5144/* Perform the real interruption of the target execution, in response
23860348 5145 to a ^C. */
c5aa993b 5146static void
fba45db2 5147async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5148{
5149 if (remote_debug)
248fd3bf 5150 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5151
94cc34af 5152 target_stop (inferior_ptid);
43ff13b4
JM
5153}
5154
0df8b418 5155/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5156 up on the target alltogether. */
47e1ce27 5157static void
fba45db2 5158async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5159{
2df3850c 5160 if (remote_debug)
248fd3bf 5161 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5162
5163 interrupt_query ();
43ff13b4
JM
5164}
5165
5166/* Reinstall the usual SIGINT handlers, after the target has
23860348 5167 stopped. */
6426a772 5168static void
934b9bac 5169async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5170{
5171 signal (SIGINT, handle_sigint);
43ff13b4
JM
5172}
5173
c906108c
SS
5174/* Send ^C to target to halt it. Target will respond, and send us a
5175 packet. */
507f3c78 5176static void (*ofunc) (int);
c906108c 5177
0df8b418
MS
5178/* The command line interface's stop routine. This function is installed
5179 as a signal handler for SIGINT. The first time a user requests a
5180 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 5181 response from the target (it didn't stop when the user requested it),
23860348 5182 we ask the user if he'd like to detach from the target. */
c906108c 5183static void
934b9bac 5184sync_remote_interrupt (int signo)
c906108c 5185{
23860348 5186 /* If this doesn't work, try more severe steps. */
934b9bac 5187 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5188
934b9bac 5189 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5190}
5191
5192/* The user typed ^C twice. */
5193
5194static void
934b9bac 5195sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5196{
5197 signal (signo, ofunc);
934b9bac
JK
5198 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5199 signal (signo, sync_remote_interrupt);
c906108c 5200}
7a292a7a 5201
74531fed
PA
5202/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5203 thread, all threads of a remote process, or all threads of all
5204 processes. */
5205
5206static void
5207remote_stop_ns (ptid_t ptid)
5208{
5209 struct remote_state *rs = get_remote_state ();
5210 char *p = rs->buf;
5211 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5212
4082afcc 5213 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5214 remote_vcont_probe (rs);
5215
d458bd84 5216 if (!rs->supports_vCont.t)
74531fed
PA
5217 error (_("Remote server does not support stopping threads"));
5218
f91d3df5
PA
5219 if (ptid_equal (ptid, minus_one_ptid)
5220 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5221 p += xsnprintf (p, endp - p, "vCont;t");
5222 else
5223 {
5224 ptid_t nptid;
5225
74531fed
PA
5226 p += xsnprintf (p, endp - p, "vCont;t:");
5227
5228 if (ptid_is_pid (ptid))
5229 /* All (-1) threads of process. */
ba348170 5230 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5231 else
5232 {
5233 /* Small optimization: if we already have a stop reply for
5234 this thread, no use in telling the stub we want this
5235 stopped. */
5236 if (peek_stop_reply (ptid))
5237 return;
5238
5239 nptid = ptid;
5240 }
5241
a9cbf802 5242 write_ptid (p, endp, nptid);
74531fed
PA
5243 }
5244
5245 /* In non-stop, we get an immediate OK reply. The stop reply will
5246 come in asynchronously by notification. */
5247 putpkt (rs->buf);
5248 getpkt (&rs->buf, &rs->buf_size, 0);
5249 if (strcmp (rs->buf, "OK") != 0)
5250 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5251}
5252
5253/* All-stop version of target_stop. Sends a break or a ^C to stop the
5254 remote target. It is undefined which thread of which process
5255 reports the stop. */
5256
5257static void
5258remote_stop_as (ptid_t ptid)
5259{
5260 struct remote_state *rs = get_remote_state ();
5261
3a29589a
DJ
5262 rs->ctrlc_pending_p = 1;
5263
74531fed
PA
5264 /* If the inferior is stopped already, but the core didn't know
5265 about it yet, just ignore the request. The cached wait status
5266 will be collected in remote_wait. */
5267 if (rs->cached_wait_status)
5268 return;
5269
9a7071a8
JB
5270 /* Send interrupt_sequence to remote target. */
5271 send_interrupt_sequence ();
74531fed
PA
5272}
5273
0df8b418 5274/* This is the generic stop called via the target vector. When a target
7a292a7a 5275 interrupt is requested, either by the command line or the GUI, we
23860348 5276 will eventually end up here. */
74531fed 5277
c906108c 5278static void
1eab8a48 5279remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5280{
7a292a7a 5281 if (remote_debug)
0f71a2f6 5282 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5283
74531fed
PA
5284 if (non_stop)
5285 remote_stop_ns (ptid);
c906108c 5286 else
74531fed 5287 remote_stop_as (ptid);
c906108c
SS
5288}
5289
5290/* Ask the user what to do when an interrupt is received. */
5291
5292static void
fba45db2 5293interrupt_query (void)
c906108c
SS
5294{
5295 target_terminal_ours ();
5296
d9d41e78 5297 if (target_is_async_p ())
c906108c 5298 {
74531fed 5299 signal (SIGINT, handle_sigint);
039e3c22 5300 quit ();
c906108c 5301 }
74531fed
PA
5302 else
5303 {
9e2f0ad4
HZ
5304 if (query (_("Interrupted while waiting for the program.\n\
5305Give up (and stop debugging it)? ")))
74531fed 5306 {
78a095c3 5307 remote_unpush_target ();
039e3c22 5308 quit ();
74531fed
PA
5309 }
5310 }
c906108c
SS
5311
5312 target_terminal_inferior ();
5313}
5314
6426a772
JM
5315/* Enable/disable target terminal ownership. Most targets can use
5316 terminal groups to control terminal ownership. Remote targets are
5317 different in that explicit transfer of ownership to/from GDB/target
23860348 5318 is required. */
6426a772
JM
5319
5320static void
d2f640d4 5321remote_terminal_inferior (struct target_ops *self)
6426a772 5322{
c6ebd6cf 5323 if (!target_async_permitted)
75c99385
PA
5324 /* Nothing to do. */
5325 return;
5326
d9d2d8b6
PA
5327 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5328 idempotent. The event-loop GDB talking to an asynchronous target
5329 with a synchronous command calls this function from both
5330 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5331 transfer the terminal to the target when it shouldn't this guard
5332 can go away. */
6426a772
JM
5333 if (!remote_async_terminal_ours_p)
5334 return;
5335 delete_file_handler (input_fd);
5336 remote_async_terminal_ours_p = 0;
934b9bac 5337 async_initialize_sigint_signal_handler ();
6426a772
JM
5338 /* NOTE: At this point we could also register our selves as the
5339 recipient of all input. Any characters typed could then be
23860348 5340 passed on down to the target. */
6426a772
JM
5341}
5342
5343static void
e3594fd1 5344remote_terminal_ours (struct target_ops *self)
6426a772 5345{
c6ebd6cf 5346 if (!target_async_permitted)
75c99385
PA
5347 /* Nothing to do. */
5348 return;
5349
5350 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5351 if (remote_async_terminal_ours_p)
5352 return;
934b9bac 5353 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5354 add_file_handler (input_fd, stdin_event_handler, 0);
5355 remote_async_terminal_ours_p = 1;
5356}
5357
176a6961 5358static void
917317f4 5359remote_console_output (char *msg)
c906108c
SS
5360{
5361 char *p;
5362
c5aa993b 5363 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5364 {
5365 char tb[2];
5366 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5367
c906108c
SS
5368 tb[0] = c;
5369 tb[1] = 0;
43ff13b4 5370 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5371 }
00db5b94
PA
5372 gdb_flush (gdb_stdtarg);
5373}
74531fed
PA
5374
5375typedef struct cached_reg
5376{
5377 int num;
5378 gdb_byte data[MAX_REGISTER_SIZE];
5379} cached_reg_t;
5380
5381DEF_VEC_O(cached_reg_t);
5382
722247f1 5383typedef struct stop_reply
74531fed 5384{
722247f1 5385 struct notif_event base;
74531fed 5386
722247f1 5387 /* The identifier of the thread about this event */
74531fed
PA
5388 ptid_t ptid;
5389
340e3c99 5390 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5391 connection, represented by a remote_state object, is closed,
5392 all the associated stop_reply events should be released. */
5393 struct remote_state *rs;
5394
74531fed
PA
5395 struct target_waitstatus ws;
5396
15148d6a
PA
5397 /* Expedited registers. This makes remote debugging a bit more
5398 efficient for those targets that provide critical registers as
5399 part of their normal status mechanism (as another roundtrip to
5400 fetch them is avoided). */
74531fed
PA
5401 VEC(cached_reg_t) *regcache;
5402
f7e6eed5
PA
5403 enum target_stop_reason stop_reason;
5404
74531fed
PA
5405 CORE_ADDR watch_data_address;
5406
dc146f7c 5407 int core;
722247f1 5408} *stop_reply_p;
a744cf53 5409
722247f1
YQ
5410DECLARE_QUEUE_P (stop_reply_p);
5411DEFINE_QUEUE_P (stop_reply_p);
5412/* The list of already fetched and acknowledged stop events. This
5413 queue is used for notification Stop, and other notifications
5414 don't need queue for their events, because the notification events
5415 of Stop can't be consumed immediately, so that events should be
5416 queued first, and be consumed by remote_wait_{ns,as} one per
5417 time. Other notifications can consume their events immediately,
5418 so queue is not needed for them. */
5419static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5420
5421static void
5422stop_reply_xfree (struct stop_reply *r)
5423{
f48ff2a7 5424 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5425}
5426
722247f1
YQ
5427static void
5428remote_notif_stop_parse (struct notif_client *self, char *buf,
5429 struct notif_event *event)
5430{
5431 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5432}
5433
5434static void
5435remote_notif_stop_ack (struct notif_client *self, char *buf,
5436 struct notif_event *event)
5437{
5438 struct stop_reply *stop_reply = (struct stop_reply *) event;
5439
5440 /* acknowledge */
5441 putpkt ((char *) self->ack_command);
5442
5443 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5444 /* We got an unknown stop reply. */
5445 error (_("Unknown stop reply"));
5446
5447 push_stop_reply (stop_reply);
5448}
5449
5450static int
5451remote_notif_stop_can_get_pending_events (struct notif_client *self)
5452{
5453 /* We can't get pending events in remote_notif_process for
5454 notification stop, and we have to do this in remote_wait_ns
5455 instead. If we fetch all queued events from stub, remote stub
5456 may exit and we have no chance to process them back in
5457 remote_wait_ns. */
5458 mark_async_event_handler (remote_async_inferior_event_token);
5459 return 0;
5460}
5461
5462static void
5463stop_reply_dtr (struct notif_event *event)
5464{
5465 struct stop_reply *r = (struct stop_reply *) event;
5466
5467 VEC_free (cached_reg_t, r->regcache);
5468}
5469
5470static struct notif_event *
5471remote_notif_stop_alloc_reply (void)
5472{
5473 struct notif_event *r
70ba0933 5474 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5475
5476 r->dtr = stop_reply_dtr;
5477
5478 return r;
5479}
5480
5481/* A client of notification Stop. */
5482
5483struct notif_client notif_client_stop =
5484{
5485 "Stop",
5486 "vStopped",
5487 remote_notif_stop_parse,
5488 remote_notif_stop_ack,
5489 remote_notif_stop_can_get_pending_events,
5490 remote_notif_stop_alloc_reply,
f48ff2a7 5491 REMOTE_NOTIF_STOP,
722247f1
YQ
5492};
5493
5494/* A parameter to pass data in and out. */
5495
5496struct queue_iter_param
5497{
5498 void *input;
5499 struct stop_reply *output;
5500};
5501
cbb8991c
DB
5502/* Determine if THREAD is a pending fork parent thread. ARG contains
5503 the pid of the process that owns the threads we want to check, or
5504 -1 if we want to check all threads. */
5505
5506static int
5507is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5508 ptid_t thread_ptid)
5509{
5510 if (ws->kind == TARGET_WAITKIND_FORKED
5511 || ws->kind == TARGET_WAITKIND_VFORKED)
5512 {
5513 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5514 return 1;
5515 }
5516
5517 return 0;
5518}
5519
5520/* Check whether EVENT is a fork event, and if it is, remove the
5521 fork child from the context list passed in DATA. */
5522
5523static int
5524remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5525 QUEUE_ITER (stop_reply_p) *iter,
5526 stop_reply_p event,
5527 void *data)
5528{
5529 struct queue_iter_param *param = data;
5530 struct threads_listing_context *context = param->input;
5531
5532 if (event->ws.kind == TARGET_WAITKIND_FORKED
5533 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5534 {
5535 threads_listing_context_remove (&event->ws, context);
5536 }
5537
5538 return 1;
5539}
5540
5541/* If CONTEXT contains any fork child threads that have not been
5542 reported yet, remove them from the CONTEXT list. If such a
5543 thread exists it is because we are stopped at a fork catchpoint
5544 and have not yet called follow_fork, which will set up the
5545 host-side data structures for the new process. */
5546
5547static void
5548remove_new_fork_children (struct threads_listing_context *context)
5549{
5550 struct thread_info * thread;
5551 int pid = -1;
5552 struct notif_client *notif = &notif_client_stop;
5553 struct queue_iter_param param;
5554
5555 /* For any threads stopped at a fork event, remove the corresponding
5556 fork child threads from the CONTEXT list. */
5557 ALL_NON_EXITED_THREADS (thread)
5558 {
5559 struct target_waitstatus *ws = &thread->pending_follow;
5560
5561 if (is_pending_fork_parent (ws, pid, thread->ptid))
5562 {
5563 threads_listing_context_remove (ws, context);
5564 }
5565 }
5566
5567 /* Check for any pending fork events (not reported or processed yet)
5568 in process PID and remove those fork child threads from the
5569 CONTEXT list as well. */
5570 remote_notif_get_pending_events (notif);
5571 param.input = context;
5572 param.output = NULL;
5573 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5574 remove_child_of_pending_fork, &param);
5575}
5576
f48ff2a7
YQ
5577/* Remove stop replies in the queue if its pid is equal to the given
5578 inferior's pid. */
722247f1
YQ
5579
5580static int
f48ff2a7
YQ
5581remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5582 QUEUE_ITER (stop_reply_p) *iter,
5583 stop_reply_p event,
5584 void *data)
722247f1
YQ
5585{
5586 struct queue_iter_param *param = data;
5587 struct inferior *inf = param->input;
5588
f48ff2a7 5589 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5590 {
5591 stop_reply_xfree (event);
5592 QUEUE_remove_elem (stop_reply_p, q, iter);
5593 }
5594
5595 return 1;
5596}
5597
f48ff2a7 5598/* Discard all pending stop replies of inferior INF. */
c906108c 5599
74531fed 5600static void
5f4cf0bb 5601discard_pending_stop_replies (struct inferior *inf)
c906108c 5602{
722247f1
YQ
5603 int i;
5604 struct queue_iter_param param;
f48ff2a7
YQ
5605 struct stop_reply *reply;
5606 struct remote_state *rs = get_remote_state ();
5607 struct remote_notif_state *rns = rs->notif_state;
5608
5609 /* This function can be notified when an inferior exists. When the
5610 target is not remote, the notification state is NULL. */
5611 if (rs->remote_desc == NULL)
5612 return;
5613
5614 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5615
74531fed 5616 /* Discard the in-flight notification. */
f48ff2a7 5617 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5618 {
722247f1 5619 stop_reply_xfree (reply);
f48ff2a7 5620 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5621 }
c906108c 5622
722247f1
YQ
5623 param.input = inf;
5624 param.output = NULL;
74531fed
PA
5625 /* Discard the stop replies we have already pulled with
5626 vStopped. */
722247f1 5627 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5628 remove_stop_reply_for_inferior, &param);
5629}
5630
bcc75809
YQ
5631/* If its remote state is equal to the given remote state,
5632 remove EVENT from the stop reply queue. */
5633
5634static int
5635remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5636 QUEUE_ITER (stop_reply_p) *iter,
5637 stop_reply_p event,
5638 void *data)
5639{
5640 struct queue_iter_param *param = data;
5641 struct remote_state *rs = param->input;
5642
5643 if (event->rs == rs)
5644 {
5645 stop_reply_xfree (event);
5646 QUEUE_remove_elem (stop_reply_p, q, iter);
5647 }
5648
5649 return 1;
5650}
5651
5652/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5653
5654static void
bcc75809 5655discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5656{
5657 struct queue_iter_param param;
5658
bcc75809 5659 param.input = rs;
f48ff2a7
YQ
5660 param.output = NULL;
5661 /* Discard the stop replies we have already pulled with
5662 vStopped. */
5663 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5664 remove_stop_reply_of_remote_state, &param);
722247f1 5665}
74531fed 5666
722247f1
YQ
5667/* A parameter to pass data in and out. */
5668
5669static int
5670remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5671 QUEUE_ITER (stop_reply_p) *iter,
5672 stop_reply_p event,
5673 void *data)
5674{
5675 struct queue_iter_param *param = data;
5676 ptid_t *ptid = param->input;
5677
5678 if (ptid_match (event->ptid, *ptid))
5679 {
5680 param->output = event;
5681 QUEUE_remove_elem (stop_reply_p, q, iter);
5682 return 0;
c8e38a49 5683 }
722247f1
YQ
5684
5685 return 1;
74531fed 5686}
43ff13b4 5687
722247f1
YQ
5688/* Remove the first reply in 'stop_reply_queue' which matches
5689 PTID. */
2e9f7625 5690
722247f1
YQ
5691static struct stop_reply *
5692remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5693{
722247f1
YQ
5694 struct queue_iter_param param;
5695
5696 param.input = &ptid;
5697 param.output = NULL;
5698
5699 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5700 remote_notif_remove_once_on_match, &param);
5701 if (notif_debug)
5702 fprintf_unfiltered (gdb_stdlog,
5703 "notif: discard queued event: 'Stop' in %s\n",
5704 target_pid_to_str (ptid));
a744cf53 5705
722247f1 5706 return param.output;
74531fed 5707}
75c99385 5708
74531fed
PA
5709/* Look for a queued stop reply belonging to PTID. If one is found,
5710 remove it from the queue, and return it. Returns NULL if none is
5711 found. If there are still queued events left to process, tell the
5712 event loop to get back to target_wait soon. */
e24a49d8 5713
74531fed
PA
5714static struct stop_reply *
5715queued_stop_reply (ptid_t ptid)
5716{
722247f1 5717 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5718
722247f1 5719 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5720 /* There's still at least an event left. */
5721 mark_async_event_handler (remote_async_inferior_event_token);
5722
722247f1 5723 return r;
74531fed
PA
5724}
5725
5726/* Push a fully parsed stop reply in the stop reply queue. Since we
5727 know that we now have at least one queued event left to pass to the
5728 core side, tell the event loop to get back to target_wait soon. */
5729
5730static void
5731push_stop_reply (struct stop_reply *new_event)
5732{
722247f1 5733 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5734
722247f1
YQ
5735 if (notif_debug)
5736 fprintf_unfiltered (gdb_stdlog,
5737 "notif: push 'Stop' %s to queue %d\n",
5738 target_pid_to_str (new_event->ptid),
5739 QUEUE_length (stop_reply_p,
5740 stop_reply_queue));
74531fed
PA
5741
5742 mark_async_event_handler (remote_async_inferior_event_token);
5743}
5744
722247f1
YQ
5745static int
5746stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5747 QUEUE_ITER (stop_reply_p) *iter,
5748 struct stop_reply *event,
5749 void *data)
5750{
5751 ptid_t *ptid = data;
5752
5753 return !(ptid_equal (*ptid, event->ptid)
5754 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5755}
5756
74531fed
PA
5757/* Returns true if we have a stop reply for PTID. */
5758
5759static int
5760peek_stop_reply (ptid_t ptid)
5761{
722247f1
YQ
5762 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5763 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5764}
5765
1f10ba14
PA
5766/* Skip PACKET until the next semi-colon (or end of string). */
5767
5768static char *
5769skip_to_semicolon (char *p)
5770{
5771 while (*p != '\0' && *p != ';')
5772 p++;
5773 return p;
5774}
5775
74531fed
PA
5776/* Parse the stop reply in BUF. Either the function succeeds, and the
5777 result is stored in EVENT, or throws an error. */
5778
5779static void
5780remote_parse_stop_reply (char *buf, struct stop_reply *event)
5781{
5782 struct remote_arch_state *rsa = get_remote_arch_state ();
5783 ULONGEST addr;
5784 char *p;
5785
5786 event->ptid = null_ptid;
bcc75809 5787 event->rs = get_remote_state ();
74531fed
PA
5788 event->ws.kind = TARGET_WAITKIND_IGNORE;
5789 event->ws.value.integer = 0;
f7e6eed5 5790 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5791 event->regcache = NULL;
dc146f7c 5792 event->core = -1;
74531fed
PA
5793
5794 switch (buf[0])
5795 {
5796 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5797 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5798 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5799 ss = signal number
5800 n... = register number
5801 r... = register contents
5802 */
5803
5804 p = &buf[3]; /* after Txx */
5805 while (*p)
5806 {
5807 char *p1;
cea39f65 5808 int fieldsize;
43ff13b4 5809
1f10ba14
PA
5810 p1 = strchr (p, ':');
5811 if (p1 == NULL)
5812 error (_("Malformed packet(a) (missing colon): %s\n\
5813Packet: '%s'\n"),
5814 p, buf);
5815 if (p == p1)
5816 error (_("Malformed packet(a) (missing register number): %s\n\
5817Packet: '%s'\n"),
5818 p, buf);
3c3bea1c 5819
1f10ba14
PA
5820 /* Some "registers" are actually extended stop information.
5821 Note if you're adding a new entry here: GDB 7.9 and
5822 earlier assume that all register "numbers" that start
5823 with an hex digit are real register numbers. Make sure
5824 the server only sends such a packet if it knows the
5825 client understands it. */
c8e38a49 5826
1f10ba14
PA
5827 if (strncmp (p, "thread", p1 - p) == 0)
5828 event->ptid = read_ptid (++p1, &p);
5829 else if ((strncmp (p, "watch", p1 - p) == 0)
5830 || (strncmp (p, "rwatch", p1 - p) == 0)
5831 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5832 {
f7e6eed5 5833 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5834 p = unpack_varlen_hex (++p1, &addr);
5835 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5836 }
f7e6eed5
PA
5837 else if (strncmp (p, "swbreak", p1 - p) == 0)
5838 {
5839 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5840
5841 /* Make sure the stub doesn't forget to indicate support
5842 with qSupported. */
5843 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5844 error (_("Unexpected swbreak stop reason"));
5845
5846 /* The value part is documented as "must be empty",
5847 though we ignore it, in case we ever decide to make
5848 use of it in a backward compatible way. */
5849 p = skip_to_semicolon (p1 + 1);
5850 }
5851 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5852 {
5853 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5854
5855 /* Make sure the stub doesn't forget to indicate support
5856 with qSupported. */
5857 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5858 error (_("Unexpected hwbreak stop reason"));
5859
5860 /* See above. */
5861 p = skip_to_semicolon (p1 + 1);
5862 }
1f10ba14 5863 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5864 {
1f10ba14
PA
5865 event->ws.kind = TARGET_WAITKIND_LOADED;
5866 p = skip_to_semicolon (p1 + 1);
5867 }
5868 else if (strncmp (p, "replaylog", p1 - p) == 0)
5869 {
5870 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5871 /* p1 will indicate "begin" or "end", but it makes
5872 no difference for now, so ignore it. */
5873 p = skip_to_semicolon (p1 + 1);
5874 }
5875 else if (strncmp (p, "core", p1 - p) == 0)
5876 {
5877 ULONGEST c;
a744cf53 5878
1f10ba14
PA
5879 p = unpack_varlen_hex (++p1, &c);
5880 event->core = c;
cea39f65 5881 }
de0d863e
DB
5882 else if (strncmp (p, "fork", p1 - p) == 0)
5883 {
5884 event->ws.value.related_pid = read_ptid (++p1, &p);
5885 event->ws.kind = TARGET_WAITKIND_FORKED;
5886 }
c269dbdb
DB
5887 else if (strncmp (p, "vfork", p1 - p) == 0)
5888 {
5889 event->ws.value.related_pid = read_ptid (++p1, &p);
5890 event->ws.kind = TARGET_WAITKIND_VFORKED;
5891 }
5892 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5893 {
5894 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5895 p = skip_to_semicolon (p1 + 1);
5896 }
cea39f65
MS
5897 else
5898 {
1f10ba14
PA
5899 ULONGEST pnum;
5900 char *p_temp;
5901
5902 /* Maybe a real ``P'' register number. */
5903 p_temp = unpack_varlen_hex (p, &pnum);
5904 /* If the first invalid character is the colon, we got a
5905 register number. Otherwise, it's an unknown stop
5906 reason. */
5907 if (p_temp == p1)
5908 {
5909 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5910 cached_reg_t cached_reg;
43ff13b4 5911
1f10ba14
PA
5912 if (reg == NULL)
5913 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5914Packet: '%s'\n"),
1f10ba14 5915 hex_string (pnum), p, buf);
c8e38a49 5916
1f10ba14 5917 cached_reg.num = reg->regnum;
4100683b 5918
1f10ba14
PA
5919 p = p1 + 1;
5920 fieldsize = hex2bin (p, cached_reg.data,
5921 register_size (target_gdbarch (),
5922 reg->regnum));
5923 p += 2 * fieldsize;
5924 if (fieldsize < register_size (target_gdbarch (),
5925 reg->regnum))
5926 warning (_("Remote reply is too short: %s"), buf);
74531fed 5927
1f10ba14
PA
5928 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5929 }
5930 else
5931 {
5932 /* Not a number. Silently skip unknown optional
5933 info. */
5934 p = skip_to_semicolon (p1 + 1);
5935 }
cea39f65 5936 }
c8e38a49 5937
cea39f65
MS
5938 if (*p != ';')
5939 error (_("Remote register badly formatted: %s\nhere: %s"),
5940 buf, p);
5941 ++p;
5942 }
5b5596ff
PA
5943
5944 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5945 break;
5946
c8e38a49
PA
5947 /* fall through */
5948 case 'S': /* Old style status, just signal only. */
3a09da41
PA
5949 {
5950 int sig;
5951
5952 event->ws.kind = TARGET_WAITKIND_STOPPED;
5953 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5954 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5955 event->ws.value.sig = (enum gdb_signal) sig;
5956 else
5957 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5958 }
c8e38a49
PA
5959 break;
5960 case 'W': /* Target exited. */
5961 case 'X':
5962 {
5963 char *p;
5964 int pid;
5965 ULONGEST value;
82f73884 5966
c8e38a49
PA
5967 /* GDB used to accept only 2 hex chars here. Stubs should
5968 only send more if they detect GDB supports multi-process
5969 support. */
5970 p = unpack_varlen_hex (&buf[1], &value);
82f73884 5971
c8e38a49
PA
5972 if (buf[0] == 'W')
5973 {
5974 /* The remote process exited. */
74531fed
PA
5975 event->ws.kind = TARGET_WAITKIND_EXITED;
5976 event->ws.value.integer = value;
c8e38a49
PA
5977 }
5978 else
5979 {
5980 /* The remote process exited with a signal. */
74531fed 5981 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
5982 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5983 event->ws.value.sig = (enum gdb_signal) value;
5984 else
5985 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 5986 }
82f73884 5987
c8e38a49
PA
5988 /* If no process is specified, assume inferior_ptid. */
5989 pid = ptid_get_pid (inferior_ptid);
5990 if (*p == '\0')
5991 ;
5992 else if (*p == ';')
5993 {
5994 p++;
5995
0b24eb2d 5996 if (*p == '\0')
82f73884 5997 ;
61012eef 5998 else if (startswith (p, "process:"))
82f73884 5999 {
c8e38a49 6000 ULONGEST upid;
a744cf53 6001
c8e38a49
PA
6002 p += sizeof ("process:") - 1;
6003 unpack_varlen_hex (p, &upid);
6004 pid = upid;
82f73884
PA
6005 }
6006 else
6007 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6008 }
c8e38a49
PA
6009 else
6010 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6011 event->ptid = pid_to_ptid (pid);
6012 }
6013 break;
6014 }
6015
6016 if (non_stop && ptid_equal (event->ptid, null_ptid))
6017 error (_("No process or thread specified in stop reply: %s"), buf);
6018}
6019
722247f1
YQ
6020/* When the stub wants to tell GDB about a new notification reply, it
6021 sends a notification (%Stop, for example). Those can come it at
6022 any time, hence, we have to make sure that any pending
6023 putpkt/getpkt sequence we're making is finished, before querying
6024 the stub for more events with the corresponding ack command
6025 (vStopped, for example). E.g., if we started a vStopped sequence
6026 immediately upon receiving the notification, something like this
6027 could happen:
74531fed
PA
6028
6029 1.1) --> Hg 1
6030 1.2) <-- OK
6031 1.3) --> g
6032 1.4) <-- %Stop
6033 1.5) --> vStopped
6034 1.6) <-- (registers reply to step #1.3)
6035
6036 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6037 query.
6038
796cb314 6039 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6040 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6041 doing whatever we were doing:
6042
6043 2.1) --> Hg 1
6044 2.2) <-- OK
6045 2.3) --> g
6046 2.4) <-- %Stop
6047 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6048 2.5) <-- (registers reply to step #2.3)
6049
6050 Eventualy after step #2.5, we return to the event loop, which
6051 notices there's an event on the
6052 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6053 associated callback --- the function below. At this point, we're
6054 always safe to start a vStopped sequence. :
6055
6056 2.6) --> vStopped
6057 2.7) <-- T05 thread:2
6058 2.8) --> vStopped
6059 2.9) --> OK
6060*/
6061
722247f1
YQ
6062void
6063remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6064{
6065 struct remote_state *rs = get_remote_state ();
74531fed 6066
f48ff2a7 6067 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6068 {
722247f1
YQ
6069 if (notif_debug)
6070 fprintf_unfiltered (gdb_stdlog,
6071 "notif: process: '%s' ack pending event\n",
6072 nc->name);
74531fed 6073
722247f1 6074 /* acknowledge */
f48ff2a7
YQ
6075 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6076 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6077
6078 while (1)
6079 {
6080 getpkt (&rs->buf, &rs->buf_size, 0);
6081 if (strcmp (rs->buf, "OK") == 0)
6082 break;
6083 else
722247f1 6084 remote_notif_ack (nc, rs->buf);
74531fed
PA
6085 }
6086 }
722247f1
YQ
6087 else
6088 {
6089 if (notif_debug)
6090 fprintf_unfiltered (gdb_stdlog,
6091 "notif: process: '%s' no pending reply\n",
6092 nc->name);
6093 }
74531fed
PA
6094}
6095
74531fed
PA
6096/* Called when it is decided that STOP_REPLY holds the info of the
6097 event that is to be returned to the core. This function always
6098 destroys STOP_REPLY. */
6099
6100static ptid_t
6101process_stop_reply (struct stop_reply *stop_reply,
6102 struct target_waitstatus *status)
6103{
6104 ptid_t ptid;
6105
6106 *status = stop_reply->ws;
6107 ptid = stop_reply->ptid;
6108
6109 /* If no thread/process was reported by the stub, assume the current
6110 inferior. */
6111 if (ptid_equal (ptid, null_ptid))
6112 ptid = inferior_ptid;
6113
5f3563ea
PA
6114 if (status->kind != TARGET_WAITKIND_EXITED
6115 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6116 {
ee154bee
TT
6117 struct remote_state *rs = get_remote_state ();
6118
5f3563ea
PA
6119 /* Expedited registers. */
6120 if (stop_reply->regcache)
6121 {
217f1f79 6122 struct regcache *regcache
f5656ead 6123 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6124 cached_reg_t *reg;
6125 int ix;
6126
6127 for (ix = 0;
6128 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6129 ix++)
217f1f79 6130 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6131 VEC_free (cached_reg_t, stop_reply->regcache);
6132 }
74531fed 6133
f7e6eed5 6134 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6135 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6136
6137 remote_notice_new_inferior (ptid, 0);
dc146f7c 6138 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6139 }
6140
74531fed
PA
6141 stop_reply_xfree (stop_reply);
6142 return ptid;
6143}
6144
6145/* The non-stop mode version of target_wait. */
6146
6147static ptid_t
47608cb1 6148remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6149{
6150 struct remote_state *rs = get_remote_state ();
74531fed
PA
6151 struct stop_reply *stop_reply;
6152 int ret;
fee9eda9 6153 int is_notif = 0;
74531fed
PA
6154
6155 /* If in non-stop mode, get out of getpkt even if a
6156 notification is received. */
6157
6158 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6159 0 /* forever */, &is_notif);
74531fed
PA
6160 while (1)
6161 {
fee9eda9 6162 if (ret != -1 && !is_notif)
74531fed
PA
6163 switch (rs->buf[0])
6164 {
6165 case 'E': /* Error of some sort. */
6166 /* We're out of sync with the target now. Did it continue
6167 or not? We can't tell which thread it was in non-stop,
6168 so just ignore this. */
6169 warning (_("Remote failure reply: %s"), rs->buf);
6170 break;
6171 case 'O': /* Console output. */
6172 remote_console_output (rs->buf + 1);
6173 break;
6174 default:
6175 warning (_("Invalid remote reply: %s"), rs->buf);
6176 break;
6177 }
6178
6179 /* Acknowledge a pending stop reply that may have arrived in the
6180 mean time. */
f48ff2a7 6181 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6182 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6183
6184 /* If indeed we noticed a stop reply, we're done. */
6185 stop_reply = queued_stop_reply (ptid);
6186 if (stop_reply != NULL)
6187 return process_stop_reply (stop_reply, status);
6188
47608cb1 6189 /* Still no event. If we're just polling for an event, then
74531fed 6190 return to the event loop. */
47608cb1 6191 if (options & TARGET_WNOHANG)
74531fed
PA
6192 {
6193 status->kind = TARGET_WAITKIND_IGNORE;
6194 return minus_one_ptid;
6195 }
6196
47608cb1 6197 /* Otherwise do a blocking wait. */
74531fed 6198 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6199 1 /* forever */, &is_notif);
74531fed
PA
6200 }
6201}
6202
6203/* Wait until the remote machine stops, then return, storing status in
6204 STATUS just as `wait' would. */
6205
6206static ptid_t
47608cb1 6207remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6208{
6209 struct remote_state *rs = get_remote_state ();
74531fed 6210 ptid_t event_ptid = null_ptid;
cea39f65 6211 char *buf;
74531fed
PA
6212 struct stop_reply *stop_reply;
6213
47608cb1
PA
6214 again:
6215
74531fed
PA
6216 status->kind = TARGET_WAITKIND_IGNORE;
6217 status->value.integer = 0;
6218
6219 stop_reply = queued_stop_reply (ptid);
6220 if (stop_reply != NULL)
6221 return process_stop_reply (stop_reply, status);
6222
6223 if (rs->cached_wait_status)
6224 /* Use the cached wait status, but only once. */
6225 rs->cached_wait_status = 0;
6226 else
6227 {
6228 int ret;
722247f1 6229 int is_notif;
74531fed
PA
6230
6231 if (!target_is_async_p ())
6232 {
934b9bac 6233 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6234 /* If the user hit C-c before this packet, or between packets,
6235 pretend that it was hit right here. */
522002f9 6236 if (check_quit_flag ())
74531fed 6237 {
522002f9 6238 clear_quit_flag ();
934b9bac 6239 sync_remote_interrupt (SIGINT);
74531fed
PA
6240 }
6241 }
6242
6243 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6244 _never_ wait for ever -> test on target_is_async_p().
6245 However, before we do that we need to ensure that the caller
6246 knows how to take the target into/out of async mode. */
722247f1
YQ
6247 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6248 wait_forever_enabled_p, &is_notif);
6249
5e1b953b
SDJ
6250 if (!target_is_async_p ())
6251 signal (SIGINT, ofunc);
6252
722247f1
YQ
6253 /* GDB gets a notification. Return to core as this event is
6254 not interesting. */
6255 if (ret != -1 && is_notif)
6256 return minus_one_ptid;
74531fed
PA
6257 }
6258
6259 buf = rs->buf;
6260
f7e6eed5 6261 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6262
6263 /* We got something. */
6264 rs->waiting_for_stop_reply = 0;
6265
3a29589a
DJ
6266 /* Assume that the target has acknowledged Ctrl-C unless we receive
6267 an 'F' or 'O' packet. */
6268 if (buf[0] != 'F' && buf[0] != 'O')
6269 rs->ctrlc_pending_p = 0;
6270
74531fed
PA
6271 switch (buf[0])
6272 {
6273 case 'E': /* Error of some sort. */
6274 /* We're out of sync with the target now. Did it continue or
6275 not? Not is more likely, so report a stop. */
6276 warning (_("Remote failure reply: %s"), buf);
6277 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6278 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6279 break;
6280 case 'F': /* File-I/O request. */
3a29589a
DJ
6281 remote_fileio_request (buf, rs->ctrlc_pending_p);
6282 rs->ctrlc_pending_p = 0;
74531fed
PA
6283 break;
6284 case 'T': case 'S': case 'X': case 'W':
6285 {
722247f1
YQ
6286 struct stop_reply *stop_reply
6287 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6288 rs->buf);
74531fed 6289
74531fed 6290 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6291 break;
6292 }
6293 case 'O': /* Console output. */
6294 remote_console_output (buf + 1);
e24a49d8 6295
c8e38a49
PA
6296 /* The target didn't really stop; keep waiting. */
6297 rs->waiting_for_stop_reply = 1;
e24a49d8 6298
c8e38a49
PA
6299 break;
6300 case '\0':
b73be471 6301 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6302 {
6303 /* Zero length reply means that we tried 'S' or 'C' and the
6304 remote system doesn't support it. */
6305 target_terminal_ours_for_output ();
6306 printf_filtered
6307 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6308 gdb_signal_to_name (rs->last_sent_signal));
6309 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6310 target_terminal_inferior ();
6311
280ceea3 6312 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6313 putpkt ((char *) buf);
6314
6315 /* We just told the target to resume, so a stop reply is in
6316 order. */
e24a49d8 6317 rs->waiting_for_stop_reply = 1;
c8e38a49 6318 break;
43ff13b4 6319 }
c8e38a49
PA
6320 /* else fallthrough */
6321 default:
6322 warning (_("Invalid remote reply: %s"), buf);
6323 /* Keep waiting. */
6324 rs->waiting_for_stop_reply = 1;
6325 break;
43ff13b4 6326 }
c8e38a49 6327
c8e38a49 6328 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6329 {
6330 /* Nothing interesting happened. If we're doing a non-blocking
6331 poll, we're done. Otherwise, go back to waiting. */
6332 if (options & TARGET_WNOHANG)
6333 return minus_one_ptid;
6334 else
6335 goto again;
6336 }
74531fed
PA
6337 else if (status->kind != TARGET_WAITKIND_EXITED
6338 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6339 {
6340 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6341 record_currthread (rs, event_ptid);
82f73884
PA
6342 else
6343 event_ptid = inferior_ptid;
43ff13b4 6344 }
74531fed
PA
6345 else
6346 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6347 record_currthread (rs, minus_one_ptid);
79d7f229 6348
82f73884 6349 return event_ptid;
43ff13b4
JM
6350}
6351
74531fed
PA
6352/* Wait until the remote machine stops, then return, storing status in
6353 STATUS just as `wait' would. */
6354
c8e38a49 6355static ptid_t
117de6a9 6356remote_wait (struct target_ops *ops,
47608cb1 6357 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6358{
6359 ptid_t event_ptid;
6360
74531fed 6361 if (non_stop)
47608cb1 6362 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6363 else
47608cb1 6364 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6365
d9d41e78 6366 if (target_is_async_p ())
c8e38a49 6367 {
74531fed
PA
6368 /* If there are are events left in the queue tell the event loop
6369 to return here. */
722247f1 6370 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6371 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6372 }
c8e38a49
PA
6373
6374 return event_ptid;
6375}
6376
74ca34ce 6377/* Fetch a single register using a 'p' packet. */
c906108c 6378
b96ec7ac 6379static int
56be3814 6380fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6381{
6382 struct remote_state *rs = get_remote_state ();
2e9f7625 6383 char *buf, *p;
b96ec7ac
AC
6384 char regp[MAX_REGISTER_SIZE];
6385 int i;
6386
4082afcc 6387 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6388 return 0;
6389
6390 if (reg->pnum == -1)
6391 return 0;
6392
2e9f7625 6393 p = rs->buf;
fcad0fa4 6394 *p++ = 'p';
74ca34ce 6395 p += hexnumstr (p, reg->pnum);
fcad0fa4 6396 *p++ = '\0';
1f4437a4
MS
6397 putpkt (rs->buf);
6398 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6399
2e9f7625
DJ
6400 buf = rs->buf;
6401
74ca34ce
DJ
6402 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6403 {
6404 case PACKET_OK:
6405 break;
6406 case PACKET_UNKNOWN:
6407 return 0;
6408 case PACKET_ERROR:
27a9c0bf
MS
6409 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6410 gdbarch_register_name (get_regcache_arch (regcache),
6411 reg->regnum),
6412 buf);
74ca34ce 6413 }
3f9a994c
JB
6414
6415 /* If this register is unfetchable, tell the regcache. */
6416 if (buf[0] == 'x')
8480adf2 6417 {
56be3814 6418 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6419 return 1;
b96ec7ac 6420 }
b96ec7ac 6421
3f9a994c
JB
6422 /* Otherwise, parse and supply the value. */
6423 p = buf;
6424 i = 0;
6425 while (p[0] != 0)
6426 {
6427 if (p[1] == 0)
74ca34ce 6428 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6429
6430 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6431 p += 2;
6432 }
56be3814 6433 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6434 return 1;
b96ec7ac
AC
6435}
6436
74ca34ce
DJ
6437/* Fetch the registers included in the target's 'g' packet. */
6438
29709017
DJ
6439static int
6440send_g_packet (void)
c906108c 6441{
d01949b6 6442 struct remote_state *rs = get_remote_state ();
cea39f65 6443 int buf_len;
c906108c 6444
bba74b36 6445 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6446 remote_send (&rs->buf, &rs->buf_size);
c906108c 6447
29709017
DJ
6448 /* We can get out of synch in various cases. If the first character
6449 in the buffer is not a hex character, assume that has happened
6450 and try to fetch another packet to read. */
6451 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6452 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6453 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6454 && rs->buf[0] != 'x') /* New: unavailable register value. */
6455 {
6456 if (remote_debug)
6457 fprintf_unfiltered (gdb_stdlog,
6458 "Bad register packet; fetching a new packet\n");
6459 getpkt (&rs->buf, &rs->buf_size, 0);
6460 }
6461
74ca34ce
DJ
6462 buf_len = strlen (rs->buf);
6463
6464 /* Sanity check the received packet. */
6465 if (buf_len % 2 != 0)
6466 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6467
6468 return buf_len / 2;
6469}
6470
6471static void
56be3814 6472process_g_packet (struct regcache *regcache)
29709017 6473{
4a22f64d 6474 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6475 struct remote_state *rs = get_remote_state ();
6476 struct remote_arch_state *rsa = get_remote_arch_state ();
6477 int i, buf_len;
6478 char *p;
6479 char *regs;
6480
6481 buf_len = strlen (rs->buf);
6482
6483 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6484 if (buf_len > 2 * rsa->sizeof_g_packet)
6485 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6486
6487 /* Save the size of the packet sent to us by the target. It is used
6488 as a heuristic when determining the max size of packets that the
6489 target can safely receive. */
6490 if (rsa->actual_register_packet_size == 0)
6491 rsa->actual_register_packet_size = buf_len;
6492
6493 /* If this is smaller than we guessed the 'g' packet would be,
6494 update our records. A 'g' reply that doesn't include a register's
6495 value implies either that the register is not available, or that
6496 the 'p' packet must be used. */
6497 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6498 {
74ca34ce
DJ
6499 rsa->sizeof_g_packet = buf_len / 2;
6500
4a22f64d 6501 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6502 {
74ca34ce
DJ
6503 if (rsa->regs[i].pnum == -1)
6504 continue;
6505
6506 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6507 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6508 else
74ca34ce 6509 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6510 }
74ca34ce 6511 }
b323314b 6512
74ca34ce 6513 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6514
6515 /* Unimplemented registers read as all bits zero. */
ea9c271d 6516 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6517
c906108c
SS
6518 /* Reply describes registers byte by byte, each byte encoded as two
6519 hex characters. Suck them all up, then supply them to the
6520 register cacheing/storage mechanism. */
6521
74ca34ce 6522 p = rs->buf;
ea9c271d 6523 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6524 {
74ca34ce
DJ
6525 if (p[0] == 0 || p[1] == 0)
6526 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6527 internal_error (__FILE__, __LINE__,
9b20d036 6528 _("unexpected end of 'g' packet reply"));
74ca34ce 6529
c906108c 6530 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6531 regs[i] = 0; /* 'x' */
c906108c
SS
6532 else
6533 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6534 p += 2;
6535 }
6536
a744cf53
MS
6537 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6538 {
6539 struct packet_reg *r = &rsa->regs[i];
6540
6541 if (r->in_g_packet)
6542 {
6543 if (r->offset * 2 >= strlen (rs->buf))
6544 /* This shouldn't happen - we adjusted in_g_packet above. */
6545 internal_error (__FILE__, __LINE__,
9b20d036 6546 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6547 else if (rs->buf[r->offset * 2] == 'x')
6548 {
6549 gdb_assert (r->offset * 2 < strlen (rs->buf));
6550 /* The register isn't available, mark it as such (at
6551 the same time setting the value to zero). */
6552 regcache_raw_supply (regcache, r->regnum, NULL);
6553 }
6554 else
6555 regcache_raw_supply (regcache, r->regnum,
6556 regs + r->offset);
6557 }
6558 }
c906108c
SS
6559}
6560
29709017 6561static void
56be3814 6562fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6563{
6564 send_g_packet ();
56be3814 6565 process_g_packet (regcache);
29709017
DJ
6566}
6567
e6e4e701
PA
6568/* Make the remote selected traceframe match GDB's selected
6569 traceframe. */
6570
6571static void
6572set_remote_traceframe (void)
6573{
6574 int newnum;
262e1174 6575 struct remote_state *rs = get_remote_state ();
e6e4e701 6576
262e1174 6577 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6578 return;
6579
6580 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6581 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6582
6583 newnum = target_trace_find (tfind_number,
6584 get_traceframe_number (), 0, 0, NULL);
6585
6586 /* Should not happen. If it does, all bets are off. */
6587 if (newnum != get_traceframe_number ())
6588 warning (_("could not set remote traceframe"));
6589}
6590
74ca34ce 6591static void
28439f5e
PA
6592remote_fetch_registers (struct target_ops *ops,
6593 struct regcache *regcache, int regnum)
74ca34ce 6594{
74ca34ce
DJ
6595 struct remote_arch_state *rsa = get_remote_arch_state ();
6596 int i;
6597
e6e4e701 6598 set_remote_traceframe ();
79d7f229 6599 set_general_thread (inferior_ptid);
74ca34ce
DJ
6600
6601 if (regnum >= 0)
6602 {
6603 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6604
74ca34ce
DJ
6605 gdb_assert (reg != NULL);
6606
6607 /* If this register might be in the 'g' packet, try that first -
6608 we are likely to read more than one register. If this is the
6609 first 'g' packet, we might be overly optimistic about its
6610 contents, so fall back to 'p'. */
6611 if (reg->in_g_packet)
6612 {
56be3814 6613 fetch_registers_using_g (regcache);
74ca34ce
DJ
6614 if (reg->in_g_packet)
6615 return;
6616 }
6617
56be3814 6618 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6619 return;
6620
6621 /* This register is not available. */
56be3814 6622 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6623
6624 return;
6625 }
6626
56be3814 6627 fetch_registers_using_g (regcache);
74ca34ce 6628
4a22f64d 6629 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6630 if (!rsa->regs[i].in_g_packet)
56be3814 6631 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6632 {
6633 /* This register is not available. */
56be3814 6634 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6635 }
6636}
6637
c906108c
SS
6638/* Prepare to store registers. Since we may send them all (using a
6639 'G' request), we have to read out the ones we don't want to change
6640 first. */
6641
c5aa993b 6642static void
f32dbf8c 6643remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6644{
ea9c271d 6645 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6646 int i;
cfd77fa1 6647 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6648
c906108c 6649 /* Make sure the entire registers array is valid. */
4082afcc 6650 switch (packet_support (PACKET_P))
5a2468f5
JM
6651 {
6652 case PACKET_DISABLE:
6653 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6654 /* Make sure all the necessary registers are cached. */
4a22f64d 6655 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6656 if (rsa->regs[i].in_g_packet)
316f2060 6657 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6658 break;
6659 case PACKET_ENABLE:
6660 break;
6661 }
6662}
6663
ad10f812 6664/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6665 packet was not recognized. */
5a2468f5
JM
6666
6667static int
1f4437a4
MS
6668store_register_using_P (const struct regcache *regcache,
6669 struct packet_reg *reg)
5a2468f5 6670{
4a22f64d 6671 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6672 struct remote_state *rs = get_remote_state ();
5a2468f5 6673 /* Try storing a single register. */
6d820c5c 6674 char *buf = rs->buf;
cfd77fa1 6675 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6676 char *p;
5a2468f5 6677
4082afcc 6678 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6679 return 0;
6680
6681 if (reg->pnum == -1)
6682 return 0;
6683
ea9c271d 6684 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6685 p = buf + strlen (buf);
56be3814 6686 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6687 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6688 putpkt (rs->buf);
6689 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6690
74ca34ce
DJ
6691 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6692 {
6693 case PACKET_OK:
6694 return 1;
6695 case PACKET_ERROR:
27a9c0bf
MS
6696 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6697 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6698 case PACKET_UNKNOWN:
6699 return 0;
6700 default:
6701 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6702 }
c906108c
SS
6703}
6704
23860348
MS
6705/* Store register REGNUM, or all registers if REGNUM == -1, from the
6706 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6707
6708static void
56be3814 6709store_registers_using_G (const struct regcache *regcache)
c906108c 6710{
d01949b6 6711 struct remote_state *rs = get_remote_state ();
ea9c271d 6712 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6713 gdb_byte *regs;
c906108c
SS
6714 char *p;
6715
193cb69f
AC
6716 /* Extract all the registers in the regcache copying them into a
6717 local buffer. */
6718 {
b323314b 6719 int i;
a744cf53 6720
ea9c271d
DJ
6721 regs = alloca (rsa->sizeof_g_packet);
6722 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6723 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6724 {
ea9c271d 6725 struct packet_reg *r = &rsa->regs[i];
a744cf53 6726
b323314b 6727 if (r->in_g_packet)
56be3814 6728 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6729 }
6730 }
c906108c
SS
6731
6732 /* Command describes registers byte by byte,
6733 each byte encoded as two hex characters. */
6d820c5c 6734 p = rs->buf;
193cb69f 6735 *p++ = 'G';
74ca34ce
DJ
6736 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6737 updated. */
6738 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6739 putpkt (rs->buf);
6740 getpkt (&rs->buf, &rs->buf_size, 0);
6741 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6742 error (_("Could not write registers; remote failure reply '%s'"),
6743 rs->buf);
c906108c 6744}
74ca34ce
DJ
6745
6746/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6747 of the register cache buffer. FIXME: ignores errors. */
6748
6749static void
28439f5e
PA
6750remote_store_registers (struct target_ops *ops,
6751 struct regcache *regcache, int regnum)
74ca34ce 6752{
74ca34ce
DJ
6753 struct remote_arch_state *rsa = get_remote_arch_state ();
6754 int i;
6755
e6e4e701 6756 set_remote_traceframe ();
79d7f229 6757 set_general_thread (inferior_ptid);
74ca34ce
DJ
6758
6759 if (regnum >= 0)
6760 {
6761 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6762
74ca34ce
DJ
6763 gdb_assert (reg != NULL);
6764
6765 /* Always prefer to store registers using the 'P' packet if
6766 possible; we often change only a small number of registers.
6767 Sometimes we change a larger number; we'd need help from a
6768 higher layer to know to use 'G'. */
56be3814 6769 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6770 return;
6771
6772 /* For now, don't complain if we have no way to write the
6773 register. GDB loses track of unavailable registers too
6774 easily. Some day, this may be an error. We don't have
0df8b418 6775 any way to read the register, either... */
74ca34ce
DJ
6776 if (!reg->in_g_packet)
6777 return;
6778
56be3814 6779 store_registers_using_G (regcache);
74ca34ce
DJ
6780 return;
6781 }
6782
56be3814 6783 store_registers_using_G (regcache);
74ca34ce 6784
4a22f64d 6785 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6786 if (!rsa->regs[i].in_g_packet)
56be3814 6787 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6788 /* See above for why we do not issue an error here. */
6789 continue;
6790}
c906108c
SS
6791\f
6792
6793/* Return the number of hex digits in num. */
6794
6795static int
fba45db2 6796hexnumlen (ULONGEST num)
c906108c
SS
6797{
6798 int i;
6799
6800 for (i = 0; num != 0; i++)
6801 num >>= 4;
6802
6803 return max (i, 1);
6804}
6805
2df3850c 6806/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6807
6808static int
fba45db2 6809hexnumstr (char *buf, ULONGEST num)
c906108c 6810{
c906108c 6811 int len = hexnumlen (num);
a744cf53 6812
2df3850c
JM
6813 return hexnumnstr (buf, num, len);
6814}
6815
c906108c 6816
2df3850c 6817/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6818
2df3850c 6819static int
fba45db2 6820hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6821{
6822 int i;
6823
6824 buf[width] = '\0';
6825
6826 for (i = width - 1; i >= 0; i--)
c906108c 6827 {
c5aa993b 6828 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6829 num >>= 4;
6830 }
6831
2df3850c 6832 return width;
c906108c
SS
6833}
6834
23860348 6835/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6836
6837static CORE_ADDR
fba45db2 6838remote_address_masked (CORE_ADDR addr)
c906108c 6839{
883b9c6c 6840 unsigned int address_size = remote_address_size;
a744cf53 6841
911c95a5
UW
6842 /* If "remoteaddresssize" was not set, default to target address size. */
6843 if (!address_size)
f5656ead 6844 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6845
6846 if (address_size > 0
6847 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6848 {
6849 /* Only create a mask when that mask can safely be constructed
23860348 6850 in a ULONGEST variable. */
c906108c 6851 ULONGEST mask = 1;
a744cf53 6852
911c95a5 6853 mask = (mask << address_size) - 1;
c906108c
SS
6854 addr &= mask;
6855 }
6856 return addr;
6857}
6858
6859/* Determine whether the remote target supports binary downloading.
6860 This is accomplished by sending a no-op memory write of zero length
6861 to the target at the specified address. It does not suffice to send
23860348
MS
6862 the whole packet, since many stubs strip the eighth bit and
6863 subsequently compute a wrong checksum, which causes real havoc with
6864 remote_write_bytes.
7a292a7a 6865
96baa820 6866 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6867 clean. In cases like this, the user should clear "remote
23860348 6868 X-packet". */
96baa820 6869
c906108c 6870static void
fba45db2 6871check_binary_download (CORE_ADDR addr)
c906108c 6872{
d01949b6 6873 struct remote_state *rs = get_remote_state ();
24b06219 6874
4082afcc 6875 switch (packet_support (PACKET_X))
c906108c 6876 {
96baa820
JM
6877 case PACKET_DISABLE:
6878 break;
6879 case PACKET_ENABLE:
6880 break;
6881 case PACKET_SUPPORT_UNKNOWN:
6882 {
96baa820 6883 char *p;
802188a7 6884
2e9f7625 6885 p = rs->buf;
96baa820
JM
6886 *p++ = 'X';
6887 p += hexnumstr (p, (ULONGEST) addr);
6888 *p++ = ',';
6889 p += hexnumstr (p, (ULONGEST) 0);
6890 *p++ = ':';
6891 *p = '\0';
802188a7 6892
2e9f7625 6893 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6894 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6895
2e9f7625 6896 if (rs->buf[0] == '\0')
96baa820
JM
6897 {
6898 if (remote_debug)
6899 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6900 "binary downloading NOT "
6901 "supported by target\n");
444abaca 6902 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6903 }
6904 else
6905 {
6906 if (remote_debug)
6907 fprintf_unfiltered (gdb_stdlog,
64b9b334 6908 "binary downloading supported by target\n");
444abaca 6909 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6910 }
6911 break;
6912 }
c906108c
SS
6913 }
6914}
6915
6916/* Write memory data directly to the remote machine.
6917 This does not inform the data cache; the data cache uses this.
a76d924d 6918 HEADER is the starting part of the packet.
c906108c
SS
6919 MEMADDR is the address in the remote memory space.
6920 MYADDR is the address of the buffer in our space.
6921 LEN is the number of bytes.
a76d924d
DJ
6922 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6923 should send data as binary ('X'), or hex-encoded ('M').
6924
6925 The function creates packet of the form
6926 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6927
6928 where encoding of <DATA> is termined by PACKET_FORMAT.
6929
6930 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6931 are omitted.
6932
9b409511
YQ
6933 Return the transferred status, error or OK (an
6934 'enum target_xfer_status' value). Save the number of bytes
6935 transferred in *XFERED_LEN. Only transfer a single packet. */
c906108c 6936
9b409511 6937static enum target_xfer_status
a76d924d 6938remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
5d6df423 6939 const gdb_byte *myaddr, ULONGEST len,
9b409511
YQ
6940 ULONGEST *xfered_len, char packet_format,
6941 int use_length)
c906108c 6942{
6d820c5c 6943 struct remote_state *rs = get_remote_state ();
cfd77fa1 6944 char *p;
a76d924d
DJ
6945 char *plen = NULL;
6946 int plenlen = 0;
917317f4
JM
6947 int todo;
6948 int nr_bytes;
a257b5bb 6949 int payload_size;
6765f3e5 6950 int payload_length;
a76d924d
DJ
6951 int header_length;
6952
6953 if (packet_format != 'X' && packet_format != 'M')
6954 internal_error (__FILE__, __LINE__,
9b20d036 6955 _("remote_write_bytes_aux: bad packet format"));
c906108c 6956
5d6df423 6957 if (len == 0)
9b409511 6958 return TARGET_XFER_EOF;
b2182ed2 6959
3de11b2e 6960 payload_size = get_memory_write_packet_size ();
2bc416ba 6961
6d820c5c
DJ
6962 /* The packet buffer will be large enough for the payload;
6963 get_memory_packet_size ensures this. */
a76d924d 6964 rs->buf[0] = '\0';
c906108c 6965
a257b5bb 6966 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
6967 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6968
a76d924d
DJ
6969 payload_size -= strlen ("$,:#NN");
6970 if (!use_length)
0df8b418 6971 /* The comma won't be used. */
a76d924d
DJ
6972 payload_size += 1;
6973 header_length = strlen (header);
6974 payload_size -= header_length;
3de11b2e 6975 payload_size -= hexnumlen (memaddr);
c906108c 6976
a76d924d 6977 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 6978
a76d924d
DJ
6979 strcat (rs->buf, header);
6980 p = rs->buf + strlen (header);
6981
6982 /* Compute a best guess of the number of bytes actually transfered. */
6983 if (packet_format == 'X')
c906108c 6984 {
23860348 6985 /* Best guess at number of bytes that will fit. */
a257b5bb 6986 todo = min (len, payload_size);
a76d924d
DJ
6987 if (use_length)
6988 payload_size -= hexnumlen (todo);
3de11b2e 6989 todo = min (todo, payload_size);
a76d924d
DJ
6990 }
6991 else
6992 {
23860348 6993 /* Num bytes that will fit. */
a257b5bb 6994 todo = min (len, payload_size / 2);
a76d924d
DJ
6995 if (use_length)
6996 payload_size -= hexnumlen (todo);
3de11b2e 6997 todo = min (todo, payload_size / 2);
917317f4 6998 }
a76d924d 6999
3de11b2e
NS
7000 if (todo <= 0)
7001 internal_error (__FILE__, __LINE__,
405f8e94 7002 _("minimum packet size too small to write data"));
802188a7 7003
6765f3e5
DJ
7004 /* If we already need another packet, then try to align the end
7005 of this packet to a useful boundary. */
7006 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
7007 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
7008
a257b5bb 7009 /* Append "<memaddr>". */
917317f4
JM
7010 memaddr = remote_address_masked (memaddr);
7011 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7012
a76d924d
DJ
7013 if (use_length)
7014 {
7015 /* Append ",". */
7016 *p++ = ',';
802188a7 7017
a76d924d
DJ
7018 /* Append <len>. Retain the location/size of <len>. It may need to
7019 be adjusted once the packet body has been created. */
7020 plen = p;
7021 plenlen = hexnumstr (p, (ULONGEST) todo);
7022 p += plenlen;
7023 }
a257b5bb
AC
7024
7025 /* Append ":". */
917317f4
JM
7026 *p++ = ':';
7027 *p = '\0';
802188a7 7028
a257b5bb 7029 /* Append the packet body. */
a76d924d 7030 if (packet_format == 'X')
917317f4 7031 {
917317f4
JM
7032 /* Binary mode. Send target system values byte by byte, in
7033 increasing byte addresses. Only escape certain critical
7034 characters. */
bc20a4af
PA
7035 payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p,
7036 &nr_bytes, payload_size);
6765f3e5
DJ
7037
7038 /* If not all TODO bytes fit, then we'll need another packet. Make
9b7194bc
DJ
7039 a second try to keep the end of the packet aligned. Don't do
7040 this if the packet is tiny. */
7041 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6765f3e5
DJ
7042 {
7043 int new_nr_bytes;
7044
7045 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
7046 - memaddr);
7047 if (new_nr_bytes != nr_bytes)
7048 payload_length = remote_escape_output (myaddr, new_nr_bytes,
bc20a4af 7049 (gdb_byte *) p, &nr_bytes,
6765f3e5
DJ
7050 payload_size);
7051 }
7052
7053 p += payload_length;
a76d924d 7054 if (use_length && nr_bytes < todo)
c906108c 7055 {
802188a7 7056 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
7057 and we have actually sent fewer bytes than planned.
7058 Fix-up the length field of the packet. Use the same
7059 number of characters as before. */
917317f4
JM
7060 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
7061 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7062 }
a76d924d
DJ
7063 }
7064 else
7065 {
917317f4
JM
7066 /* Normal mode: Send target system values byte by byte, in
7067 increasing byte addresses. Each byte is encoded as a two hex
7068 value. */
2644f393 7069 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 7070 p += 2 * nr_bytes;
c906108c 7071 }
802188a7 7072
2e9f7625 7073 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7074 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7075
2e9f7625 7076 if (rs->buf[0] == 'E')
00d84524 7077 return TARGET_XFER_E_IO;
802188a7 7078
23860348
MS
7079 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
7080 fewer bytes than we'd planned. */
9b409511
YQ
7081 *xfered_len = (ULONGEST) nr_bytes;
7082 return TARGET_XFER_OK;
c906108c
SS
7083}
7084
a76d924d
DJ
7085/* Write memory data directly to the remote machine.
7086 This does not inform the data cache; the data cache uses this.
7087 MEMADDR is the address in the remote memory space.
7088 MYADDR is the address of the buffer in our space.
7089 LEN is the number of bytes.
7090
9b409511
YQ
7091 Return the transferred status, error or OK (an
7092 'enum target_xfer_status' value). Save the number of bytes
7093 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7094
9b409511
YQ
7095static enum target_xfer_status
7096remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
7097 ULONGEST *xfered_len)
a76d924d
DJ
7098{
7099 char *packet_format = 0;
7100
7101 /* Check whether the target supports binary download. */
7102 check_binary_download (memaddr);
7103
4082afcc 7104 switch (packet_support (PACKET_X))
a76d924d
DJ
7105 {
7106 case PACKET_ENABLE:
7107 packet_format = "X";
7108 break;
7109 case PACKET_DISABLE:
7110 packet_format = "M";
7111 break;
7112 case PACKET_SUPPORT_UNKNOWN:
7113 internal_error (__FILE__, __LINE__,
7114 _("remote_write_bytes: bad internal state"));
7115 default:
7116 internal_error (__FILE__, __LINE__, _("bad switch"));
7117 }
7118
7119 return remote_write_bytes_aux (packet_format,
9b409511
YQ
7120 memaddr, myaddr, len, xfered_len,
7121 packet_format[0], 1);
a76d924d
DJ
7122}
7123
9217e74e
YQ
7124/* Read memory data directly from the remote machine.
7125 This does not use the data cache; the data cache uses this.
7126 MEMADDR is the address in the remote memory space.
7127 MYADDR is the address of the buffer in our space.
7128 LEN is the number of bytes.
7129
7130 Return the transferred status, error or OK (an
7131 'enum target_xfer_status' value). Save the number of bytes
7132 transferred in *XFERED_LEN. */
7133
7134static enum target_xfer_status
7135remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len,
7136 ULONGEST *xfered_len)
7137{
7138 struct remote_state *rs = get_remote_state ();
7139 int max_buf_size; /* Max size of packet output buffer. */
7140 char *p;
7141 int todo;
7142 int i;
7143
7144 max_buf_size = get_memory_read_packet_size ();
7145 /* The packet buffer will be large enough for the payload;
7146 get_memory_packet_size ensures this. */
7147
7148 /* Number if bytes that will fit. */
7149 todo = min (len, max_buf_size / 2);
7150
7151 /* Construct "m"<memaddr>","<len>". */
7152 memaddr = remote_address_masked (memaddr);
7153 p = rs->buf;
7154 *p++ = 'm';
7155 p += hexnumstr (p, (ULONGEST) memaddr);
7156 *p++ = ',';
7157 p += hexnumstr (p, (ULONGEST) todo);
7158 *p = '\0';
7159 putpkt (rs->buf);
7160 getpkt (&rs->buf, &rs->buf_size, 0);
7161 if (rs->buf[0] == 'E'
7162 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7163 && rs->buf[3] == '\0')
7164 return TARGET_XFER_E_IO;
7165 /* Reply describes memory byte by byte, each byte encoded as two hex
7166 characters. */
7167 p = rs->buf;
7168 i = hex2bin (p, myaddr, todo);
7169 /* Return what we have. Let higher layers handle partial reads. */
7170 *xfered_len = (ULONGEST) i;
7171 return TARGET_XFER_OK;
7172}
7173
b55fbac4
YQ
7174/* Using the set of read-only target sections of remote, read live
7175 read-only memory.
8acf9577
YQ
7176
7177 For interface/parameters/return description see target.h,
7178 to_xfer_partial. */
7179
7180static enum target_xfer_status
b55fbac4
YQ
7181remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7182 ULONGEST memaddr, ULONGEST len,
7183 ULONGEST *xfered_len)
8acf9577
YQ
7184{
7185 struct target_section *secp;
7186 struct target_section_table *table;
7187
7188 secp = target_section_by_addr (ops, memaddr);
7189 if (secp != NULL
7190 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7191 secp->the_bfd_section)
7192 & SEC_READONLY))
7193 {
7194 struct target_section *p;
7195 ULONGEST memend = memaddr + len;
7196
7197 table = target_get_section_table (ops);
7198
7199 for (p = table->sections; p < table->sections_end; p++)
7200 {
7201 if (memaddr >= p->addr)
7202 {
7203 if (memend <= p->endaddr)
7204 {
7205 /* Entire transfer is within this section. */
b55fbac4
YQ
7206 return remote_read_bytes_1 (memaddr, readbuf, len,
7207 xfered_len);
8acf9577
YQ
7208 }
7209 else if (memaddr >= p->endaddr)
7210 {
7211 /* This section ends before the transfer starts. */
7212 continue;
7213 }
7214 else
7215 {
7216 /* This section overlaps the transfer. Just do half. */
7217 len = p->endaddr - memaddr;
b55fbac4
YQ
7218 return remote_read_bytes_1 (memaddr, readbuf, len,
7219 xfered_len);
8acf9577
YQ
7220 }
7221 }
7222 }
7223 }
7224
7225 return TARGET_XFER_EOF;
7226}
7227
9217e74e
YQ
7228/* Similar to remote_read_bytes_1, but it reads from the remote stub
7229 first if the requested memory is unavailable in traceframe.
7230 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7231
9b409511 7232static enum target_xfer_status
8acf9577
YQ
7233remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
7234 gdb_byte *myaddr, ULONGEST len, ULONGEST *xfered_len)
c906108c 7235{
6b6aa828 7236 if (len == 0)
96c4f946 7237 return TARGET_XFER_EOF;
b2182ed2 7238
8acf9577
YQ
7239 if (get_traceframe_number () != -1)
7240 {
7241 VEC(mem_range_s) *available;
7242
7243 /* If we fail to get the set of available memory, then the
7244 target does not support querying traceframe info, and so we
7245 attempt reading from the traceframe anyway (assuming the
7246 target implements the old QTro packet then). */
7247 if (traceframe_available_memory (&available, memaddr, len))
7248 {
7249 struct cleanup *old_chain;
7250
7251 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7252
7253 if (VEC_empty (mem_range_s, available)
7254 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7255 {
7256 enum target_xfer_status res;
7257
7258 /* Don't read into the traceframe's available
7259 memory. */
7260 if (!VEC_empty (mem_range_s, available))
7261 {
7262 LONGEST oldlen = len;
7263
7264 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7265 gdb_assert (len <= oldlen);
7266 }
7267
7268 do_cleanups (old_chain);
7269
7270 /* This goes through the topmost target again. */
b55fbac4 7271 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
8acf9577
YQ
7272 len, xfered_len);
7273 if (res == TARGET_XFER_OK)
7274 return TARGET_XFER_OK;
7275 else
7276 {
7277 /* No use trying further, we know some memory starting
7278 at MEMADDR isn't available. */
7279 *xfered_len = len;
7280 return TARGET_XFER_UNAVAILABLE;
7281 }
7282 }
7283
7284 /* Don't try to read more than how much is available, in
7285 case the target implements the deprecated QTro packet to
7286 cater for older GDBs (the target's knowledge of read-only
7287 sections may be outdated by now). */
7288 len = VEC_index (mem_range_s, available, 0)->length;
7289
7290 do_cleanups (old_chain);
7291 }
7292 }
7293
9217e74e 7294 return remote_read_bytes_1 (memaddr, myaddr, len, xfered_len);
c906108c 7295}
74531fed 7296
c906108c 7297\f
c906108c 7298
a76d924d
DJ
7299/* Sends a packet with content determined by the printf format string
7300 FORMAT and the remaining arguments, then gets the reply. Returns
7301 whether the packet was a success, a failure, or unknown. */
7302
77b64a49
PA
7303static enum packet_result remote_send_printf (const char *format, ...)
7304 ATTRIBUTE_PRINTF (1, 2);
7305
2c0b251b 7306static enum packet_result
a76d924d
DJ
7307remote_send_printf (const char *format, ...)
7308{
7309 struct remote_state *rs = get_remote_state ();
7310 int max_size = get_remote_packet_size ();
a76d924d 7311 va_list ap;
a744cf53 7312
a76d924d
DJ
7313 va_start (ap, format);
7314
7315 rs->buf[0] = '\0';
7316 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7317 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7318
7319 if (putpkt (rs->buf) < 0)
7320 error (_("Communication problem with target."));
7321
7322 rs->buf[0] = '\0';
7323 getpkt (&rs->buf, &rs->buf_size, 0);
7324
7325 return packet_check_result (rs->buf);
7326}
7327
7328static void
7329restore_remote_timeout (void *p)
7330{
7331 int value = *(int *)p;
a744cf53 7332
a76d924d
DJ
7333 remote_timeout = value;
7334}
7335
7336/* Flash writing can take quite some time. We'll set
7337 effectively infinite timeout for flash operations.
7338 In future, we'll need to decide on a better approach. */
7339static const int remote_flash_timeout = 1000;
7340
7341static void
7342remote_flash_erase (struct target_ops *ops,
7343 ULONGEST address, LONGEST length)
7344{
f5656ead 7345 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7346 int saved_remote_timeout = remote_timeout;
7347 enum packet_result ret;
a76d924d
DJ
7348 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7349 &saved_remote_timeout);
a744cf53 7350
a76d924d
DJ
7351 remote_timeout = remote_flash_timeout;
7352
7353 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7354 phex (address, addr_size),
a76d924d
DJ
7355 phex (length, 4));
7356 switch (ret)
7357 {
7358 case PACKET_UNKNOWN:
7359 error (_("Remote target does not support flash erase"));
7360 case PACKET_ERROR:
7361 error (_("Error erasing flash with vFlashErase packet"));
7362 default:
7363 break;
7364 }
7365
7366 do_cleanups (back_to);
7367}
7368
9b409511
YQ
7369static enum target_xfer_status
7370remote_flash_write (struct target_ops *ops, ULONGEST address,
7371 ULONGEST length, ULONGEST *xfered_len,
7372 const gdb_byte *data)
a76d924d
DJ
7373{
7374 int saved_remote_timeout = remote_timeout;
9b409511 7375 enum target_xfer_status ret;
a76d924d 7376 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7377 &saved_remote_timeout);
a76d924d
DJ
7378
7379 remote_timeout = remote_flash_timeout;
9b409511
YQ
7380 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length,
7381 xfered_len,'X', 0);
a76d924d
DJ
7382 do_cleanups (back_to);
7383
7384 return ret;
7385}
7386
7387static void
7388remote_flash_done (struct target_ops *ops)
7389{
7390 int saved_remote_timeout = remote_timeout;
7391 int ret;
7392 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7393 &saved_remote_timeout);
7394
7395 remote_timeout = remote_flash_timeout;
7396 ret = remote_send_printf ("vFlashDone");
7397 do_cleanups (back_to);
7398
7399 switch (ret)
7400 {
7401 case PACKET_UNKNOWN:
7402 error (_("Remote target does not support vFlashDone"));
7403 case PACKET_ERROR:
7404 error (_("Error finishing flash operation"));
7405 default:
7406 break;
7407 }
7408}
7409
c906108c 7410static void
fba45db2 7411remote_files_info (struct target_ops *ignore)
c906108c
SS
7412{
7413 puts_filtered ("Debugging a target over a serial line.\n");
7414}
7415\f
7416/* Stuff for dealing with the packets which are part of this protocol.
7417 See comment at top of file for details. */
7418
1927e618
PA
7419/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7420 error to higher layers. Called when a serial error is detected.
7421 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7422 the system error message for errno at function entry and final dot
7423 for output compatibility with throw_perror_with_name. */
1927e618
PA
7424
7425static void
7426unpush_and_perror (const char *string)
7427{
d6cb50a2 7428 int saved_errno = errno;
1927e618
PA
7429
7430 remote_unpush_target ();
d6cb50a2
JK
7431 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7432 safe_strerror (saved_errno));
1927e618
PA
7433}
7434
0876f84a 7435/* Read a single character from the remote end. */
c906108c
SS
7436
7437static int
fba45db2 7438readchar (int timeout)
c906108c
SS
7439{
7440 int ch;
5d93a237 7441 struct remote_state *rs = get_remote_state ();
c906108c 7442
5d93a237 7443 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7444
2acceee2 7445 if (ch >= 0)
0876f84a 7446 return ch;
2acceee2
JM
7447
7448 switch ((enum serial_rc) ch)
c906108c
SS
7449 {
7450 case SERIAL_EOF:
78a095c3 7451 remote_unpush_target ();
598d3636 7452 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7453 /* no return */
c906108c 7454 case SERIAL_ERROR:
1927e618
PA
7455 unpush_and_perror (_("Remote communication error. "
7456 "Target disconnected."));
2acceee2 7457 /* no return */
c906108c 7458 case SERIAL_TIMEOUT:
2acceee2 7459 break;
c906108c 7460 }
2acceee2 7461 return ch;
c906108c
SS
7462}
7463
c33e31fd
PA
7464/* Wrapper for serial_write that closes the target and throws if
7465 writing fails. */
7466
7467static void
7468remote_serial_write (const char *str, int len)
7469{
5d93a237
TT
7470 struct remote_state *rs = get_remote_state ();
7471
7472 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7473 {
1927e618
PA
7474 unpush_and_perror (_("Remote communication error. "
7475 "Target disconnected."));
c33e31fd
PA
7476 }
7477}
7478
6d820c5c
DJ
7479/* Send the command in *BUF to the remote machine, and read the reply
7480 into *BUF. Report an error if we get an error reply. Resize
7481 *BUF using xrealloc if necessary to hold the result, and update
7482 *SIZEOF_BUF. */
c906108c
SS
7483
7484static void
6d820c5c
DJ
7485remote_send (char **buf,
7486 long *sizeof_buf)
c906108c 7487{
6d820c5c 7488 putpkt (*buf);
c2d11a7d 7489 getpkt (buf, sizeof_buf, 0);
c906108c 7490
6d820c5c
DJ
7491 if ((*buf)[0] == 'E')
7492 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7493}
7494
6e5abd65
PA
7495/* Return a pointer to an xmalloc'ed string representing an escaped
7496 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7497 etc. The caller is responsible for releasing the returned
7498 memory. */
7499
7500static char *
7501escape_buffer (const char *buf, int n)
7502{
7503 struct cleanup *old_chain;
7504 struct ui_file *stb;
7505 char *str;
6e5abd65
PA
7506
7507 stb = mem_fileopen ();
7508 old_chain = make_cleanup_ui_file_delete (stb);
7509
6ef284bd 7510 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7511 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7512 do_cleanups (old_chain);
7513 return str;
7514}
7515
c906108c
SS
7516/* Display a null-terminated packet on stdout, for debugging, using C
7517 string notation. */
7518
7519static void
baa336ce 7520print_packet (const char *buf)
c906108c
SS
7521{
7522 puts_filtered ("\"");
43e526b9 7523 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7524 puts_filtered ("\"");
7525}
7526
7527int
baa336ce 7528putpkt (const char *buf)
c906108c
SS
7529{
7530 return putpkt_binary (buf, strlen (buf));
7531}
7532
7533/* Send a packet to the remote machine, with error checking. The data
23860348 7534 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7535 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7536 and for a possible /0 if we are debugging (remote_debug) and want
7537 to print the sent packet as a string. */
c906108c
SS
7538
7539static int
baa336ce 7540putpkt_binary (const char *buf, int cnt)
c906108c 7541{
2d717e4f 7542 struct remote_state *rs = get_remote_state ();
c906108c
SS
7543 int i;
7544 unsigned char csum = 0;
11cf8741 7545 char *buf2 = alloca (cnt + 6);
085dd6e6 7546
c906108c
SS
7547 int ch;
7548 int tcount = 0;
7549 char *p;
dd61ec5c 7550 char *message;
c906108c 7551
e24a49d8
PA
7552 /* Catch cases like trying to read memory or listing threads while
7553 we're waiting for a stop reply. The remote server wouldn't be
7554 ready to handle this request, so we'd hang and timeout. We don't
7555 have to worry about this in synchronous mode, because in that
7556 case it's not possible to issue a command while the target is
74531fed
PA
7557 running. This is not a problem in non-stop mode, because in that
7558 case, the stub is always ready to process serial input. */
d9d41e78 7559 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7560 {
7561 error (_("Cannot execute this command while the target is running.\n"
7562 "Use the \"interrupt\" command to stop the target\n"
7563 "and then try again."));
7564 }
e24a49d8 7565
2d717e4f
DJ
7566 /* We're sending out a new packet. Make sure we don't look at a
7567 stale cached response. */
7568 rs->cached_wait_status = 0;
7569
c906108c
SS
7570 /* Copy the packet into buffer BUF2, encapsulating it
7571 and giving it a checksum. */
7572
c906108c
SS
7573 p = buf2;
7574 *p++ = '$';
7575
7576 for (i = 0; i < cnt; i++)
7577 {
7578 csum += buf[i];
7579 *p++ = buf[i];
7580 }
7581 *p++ = '#';
7582 *p++ = tohex ((csum >> 4) & 0xf);
7583 *p++ = tohex (csum & 0xf);
7584
7585 /* Send it over and over until we get a positive ack. */
7586
7587 while (1)
7588 {
7589 int started_error_output = 0;
7590
7591 if (remote_debug)
7592 {
6e5abd65
PA
7593 struct cleanup *old_chain;
7594 char *str;
7595
c906108c 7596 *p = '\0';
6e5abd65
PA
7597 str = escape_buffer (buf2, p - buf2);
7598 old_chain = make_cleanup (xfree, str);
7599 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7600 gdb_flush (gdb_stdlog);
6e5abd65 7601 do_cleanups (old_chain);
c906108c 7602 }
c33e31fd 7603 remote_serial_write (buf2, p - buf2);
c906108c 7604
a6f3e723
SL
7605 /* If this is a no acks version of the remote protocol, send the
7606 packet and move on. */
7607 if (rs->noack_mode)
7608 break;
7609
74531fed
PA
7610 /* Read until either a timeout occurs (-2) or '+' is read.
7611 Handle any notification that arrives in the mean time. */
c906108c
SS
7612 while (1)
7613 {
7614 ch = readchar (remote_timeout);
7615
c5aa993b 7616 if (remote_debug)
c906108c
SS
7617 {
7618 switch (ch)
7619 {
7620 case '+':
1216fa2c 7621 case '-':
c906108c
SS
7622 case SERIAL_TIMEOUT:
7623 case '$':
74531fed 7624 case '%':
c906108c
SS
7625 if (started_error_output)
7626 {
7627 putchar_unfiltered ('\n');
7628 started_error_output = 0;
7629 }
7630 }
7631 }
7632
7633 switch (ch)
7634 {
7635 case '+':
7636 if (remote_debug)
0f71a2f6 7637 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7638 return 1;
1216fa2c
AC
7639 case '-':
7640 if (remote_debug)
7641 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7642 /* FALLTHROUGH */
c906108c 7643 case SERIAL_TIMEOUT:
c5aa993b 7644 tcount++;
c906108c
SS
7645 if (tcount > 3)
7646 return 0;
23860348 7647 break; /* Retransmit buffer. */
c906108c
SS
7648 case '$':
7649 {
40e3f985 7650 if (remote_debug)
2bc416ba 7651 fprintf_unfiltered (gdb_stdlog,
23860348 7652 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7653 /* It's probably an old response sent because an ACK
7654 was lost. Gobble up the packet and ack it so it
7655 doesn't get retransmitted when we resend this
7656 packet. */
6d820c5c 7657 skip_frame ();
c33e31fd 7658 remote_serial_write ("+", 1);
23860348 7659 continue; /* Now, go look for +. */
c906108c 7660 }
74531fed
PA
7661
7662 case '%':
7663 {
7664 int val;
7665
7666 /* If we got a notification, handle it, and go back to looking
7667 for an ack. */
7668 /* We've found the start of a notification. Now
7669 collect the data. */
7670 val = read_frame (&rs->buf, &rs->buf_size);
7671 if (val >= 0)
7672 {
7673 if (remote_debug)
7674 {
6e5abd65
PA
7675 struct cleanup *old_chain;
7676 char *str;
7677
7678 str = escape_buffer (rs->buf, val);
7679 old_chain = make_cleanup (xfree, str);
7680 fprintf_unfiltered (gdb_stdlog,
7681 " Notification received: %s\n",
7682 str);
7683 do_cleanups (old_chain);
74531fed 7684 }
5965e028 7685 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7686 /* We're in sync now, rewait for the ack. */
7687 tcount = 0;
7688 }
7689 else
7690 {
7691 if (remote_debug)
7692 {
7693 if (!started_error_output)
7694 {
7695 started_error_output = 1;
7696 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7697 }
7698 fputc_unfiltered (ch & 0177, gdb_stdlog);
7699 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7700 }
7701 }
7702 continue;
7703 }
7704 /* fall-through */
c906108c
SS
7705 default:
7706 if (remote_debug)
7707 {
7708 if (!started_error_output)
7709 {
7710 started_error_output = 1;
0f71a2f6 7711 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7712 }
0f71a2f6 7713 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7714 }
7715 continue;
7716 }
23860348 7717 break; /* Here to retransmit. */
c906108c
SS
7718 }
7719
7720#if 0
7721 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7722 able to get out next time we call QUIT, without anything as
7723 violent as interrupt_query. If we want to provide a way out of
7724 here without getting to the next QUIT, it should be based on
7725 hitting ^C twice as in remote_wait. */
c906108c
SS
7726 if (quit_flag)
7727 {
7728 quit_flag = 0;
7729 interrupt_query ();
7730 }
7731#endif
7732 }
a6f3e723 7733 return 0;
c906108c
SS
7734}
7735
6d820c5c
DJ
7736/* Come here after finding the start of a frame when we expected an
7737 ack. Do our best to discard the rest of this packet. */
7738
7739static void
7740skip_frame (void)
7741{
7742 int c;
7743
7744 while (1)
7745 {
7746 c = readchar (remote_timeout);
7747 switch (c)
7748 {
7749 case SERIAL_TIMEOUT:
7750 /* Nothing we can do. */
7751 return;
7752 case '#':
7753 /* Discard the two bytes of checksum and stop. */
7754 c = readchar (remote_timeout);
7755 if (c >= 0)
7756 c = readchar (remote_timeout);
7757
7758 return;
7759 case '*': /* Run length encoding. */
7760 /* Discard the repeat count. */
7761 c = readchar (remote_timeout);
7762 if (c < 0)
7763 return;
7764 break;
7765 default:
7766 /* A regular character. */
7767 break;
7768 }
7769 }
7770}
7771
c906108c 7772/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7773 into *BUF, verifying the checksum, length, and handling run-length
7774 compression. NUL terminate the buffer. If there is not enough room,
7775 expand *BUF using xrealloc.
c906108c 7776
c2d11a7d
JM
7777 Returns -1 on error, number of characters in buffer (ignoring the
7778 trailing NULL) on success. (could be extended to return one of the
23860348 7779 SERIAL status indications). */
c2d11a7d
JM
7780
7781static long
6d820c5c
DJ
7782read_frame (char **buf_p,
7783 long *sizeof_buf)
c906108c
SS
7784{
7785 unsigned char csum;
c2d11a7d 7786 long bc;
c906108c 7787 int c;
6d820c5c 7788 char *buf = *buf_p;
a6f3e723 7789 struct remote_state *rs = get_remote_state ();
c906108c
SS
7790
7791 csum = 0;
c2d11a7d 7792 bc = 0;
c906108c
SS
7793
7794 while (1)
7795 {
7796 c = readchar (remote_timeout);
c906108c
SS
7797 switch (c)
7798 {
7799 case SERIAL_TIMEOUT:
7800 if (remote_debug)
0f71a2f6 7801 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7802 return -1;
c906108c
SS
7803 case '$':
7804 if (remote_debug)
0f71a2f6
JM
7805 fputs_filtered ("Saw new packet start in middle of old one\n",
7806 gdb_stdlog);
23860348 7807 return -1; /* Start a new packet, count retries. */
c906108c
SS
7808 case '#':
7809 {
7810 unsigned char pktcsum;
e1b09194
AC
7811 int check_0 = 0;
7812 int check_1 = 0;
c906108c 7813
c2d11a7d 7814 buf[bc] = '\0';
c906108c 7815
e1b09194
AC
7816 check_0 = readchar (remote_timeout);
7817 if (check_0 >= 0)
7818 check_1 = readchar (remote_timeout);
802188a7 7819
e1b09194
AC
7820 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7821 {
7822 if (remote_debug)
2bc416ba 7823 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7824 gdb_stdlog);
e1b09194
AC
7825 return -1;
7826 }
7827 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7828 {
7829 if (remote_debug)
2bc416ba 7830 fputs_filtered ("Communication error in checksum\n",
23860348 7831 gdb_stdlog);
40e3f985
FN
7832 return -1;
7833 }
c906108c 7834
a6f3e723
SL
7835 /* Don't recompute the checksum; with no ack packets we
7836 don't have any way to indicate a packet retransmission
7837 is necessary. */
7838 if (rs->noack_mode)
7839 return bc;
7840
e1b09194 7841 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7842 if (csum == pktcsum)
c2d11a7d 7843 return bc;
c906108c 7844
c5aa993b 7845 if (remote_debug)
c906108c 7846 {
6e5abd65
PA
7847 struct cleanup *old_chain;
7848 char *str;
7849
7850 str = escape_buffer (buf, bc);
7851 old_chain = make_cleanup (xfree, str);
7852 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7853 "Bad checksum, sentsum=0x%x, "
7854 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7855 pktcsum, csum, str);
7856 do_cleanups (old_chain);
c906108c 7857 }
c2d11a7d 7858 /* Number of characters in buffer ignoring trailing
23860348 7859 NULL. */
c2d11a7d 7860 return -1;
c906108c 7861 }
23860348 7862 case '*': /* Run length encoding. */
c2c6d25f
JM
7863 {
7864 int repeat;
c906108c 7865
a744cf53 7866 csum += c;
b4501125
AC
7867 c = readchar (remote_timeout);
7868 csum += c;
23860348 7869 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7870
23860348 7871 /* The character before ``*'' is repeated. */
c2d11a7d 7872
6d820c5c 7873 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7874 {
6d820c5c
DJ
7875 if (bc + repeat - 1 >= *sizeof_buf - 1)
7876 {
7877 /* Make some more room in the buffer. */
7878 *sizeof_buf += repeat;
7879 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7880 buf = *buf_p;
7881 }
7882
c2d11a7d
JM
7883 memset (&buf[bc], buf[bc - 1], repeat);
7884 bc += repeat;
c2c6d25f
JM
7885 continue;
7886 }
7887
c2d11a7d 7888 buf[bc] = '\0';
6d820c5c 7889 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7890 return -1;
c2c6d25f 7891 }
c906108c 7892 default:
6d820c5c 7893 if (bc >= *sizeof_buf - 1)
c906108c 7894 {
6d820c5c
DJ
7895 /* Make some more room in the buffer. */
7896 *sizeof_buf *= 2;
7897 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7898 buf = *buf_p;
c906108c
SS
7899 }
7900
6d820c5c
DJ
7901 buf[bc++] = c;
7902 csum += c;
7903 continue;
c906108c
SS
7904 }
7905 }
7906}
7907
7908/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7909 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7910 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7911 rather than timing out; this is used (in synchronous mode) to wait
7912 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7913/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7914 don't have to change all the calls to getpkt to deal with the
7915 return value, because at the moment I don't know what the right
23860348 7916 thing to do it for those. */
c906108c 7917void
6d820c5c
DJ
7918getpkt (char **buf,
7919 long *sizeof_buf,
c2d11a7d 7920 int forever)
d9fcf2fb
JM
7921{
7922 int timed_out;
7923
7924 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7925}
7926
7927
7928/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7929 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7930 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7931 rather than timing out; this is used (in synchronous mode) to wait
7932 for a target that is is executing user code to stop. If FOREVER ==
7933 0, this function is allowed to time out gracefully and return an
74531fed
PA
7934 indication of this to the caller. Otherwise return the number of
7935 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
7936 enough reason to return to the caller. *IS_NOTIF is an output
7937 boolean that indicates whether *BUF holds a notification or not
7938 (a regular packet). */
74531fed 7939
3172dc30 7940static int
74531fed 7941getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 7942 int expecting_notif, int *is_notif)
c906108c 7943{
2d717e4f 7944 struct remote_state *rs = get_remote_state ();
c906108c
SS
7945 int c;
7946 int tries;
7947 int timeout;
df4b58fe 7948 int val = -1;
c906108c 7949
2d717e4f
DJ
7950 /* We're reading a new response. Make sure we don't look at a
7951 previously cached response. */
7952 rs->cached_wait_status = 0;
7953
6d820c5c 7954 strcpy (*buf, "timeout");
c906108c
SS
7955
7956 if (forever)
74531fed
PA
7957 timeout = watchdog > 0 ? watchdog : -1;
7958 else if (expecting_notif)
7959 timeout = 0; /* There should already be a char in the buffer. If
7960 not, bail out. */
c906108c
SS
7961 else
7962 timeout = remote_timeout;
7963
7964#define MAX_TRIES 3
7965
74531fed
PA
7966 /* Process any number of notifications, and then return when
7967 we get a packet. */
7968 for (;;)
c906108c 7969 {
d9c43928 7970 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
7971 times. */
7972 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 7973 {
74531fed
PA
7974 /* This can loop forever if the remote side sends us
7975 characters continuously, but if it pauses, we'll get
7976 SERIAL_TIMEOUT from readchar because of timeout. Then
7977 we'll count that as a retry.
7978
7979 Note that even when forever is set, we will only wait
7980 forever prior to the start of a packet. After that, we
7981 expect characters to arrive at a brisk pace. They should
7982 show up within remote_timeout intervals. */
7983 do
7984 c = readchar (timeout);
7985 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
7986
7987 if (c == SERIAL_TIMEOUT)
7988 {
74531fed
PA
7989 if (expecting_notif)
7990 return -1; /* Don't complain, it's normal to not get
7991 anything in this case. */
7992
23860348 7993 if (forever) /* Watchdog went off? Kill the target. */
c906108c 7994 {
2acceee2 7995 QUIT;
78a095c3 7996 remote_unpush_target ();
598d3636
JK
7997 throw_error (TARGET_CLOSE_ERROR,
7998 _("Watchdog timeout has expired. "
7999 "Target detached."));
c906108c 8000 }
c906108c 8001 if (remote_debug)
0f71a2f6 8002 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8003 }
74531fed
PA
8004 else
8005 {
8006 /* We've found the start of a packet or notification.
8007 Now collect the data. */
8008 val = read_frame (buf, sizeof_buf);
8009 if (val >= 0)
8010 break;
8011 }
8012
c33e31fd 8013 remote_serial_write ("-", 1);
c906108c 8014 }
c906108c 8015
74531fed
PA
8016 if (tries > MAX_TRIES)
8017 {
8018 /* We have tried hard enough, and just can't receive the
8019 packet/notification. Give up. */
8020 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8021
74531fed
PA
8022 /* Skip the ack char if we're in no-ack mode. */
8023 if (!rs->noack_mode)
c33e31fd 8024 remote_serial_write ("+", 1);
74531fed
PA
8025 return -1;
8026 }
c906108c 8027
74531fed
PA
8028 /* If we got an ordinary packet, return that to our caller. */
8029 if (c == '$')
c906108c
SS
8030 {
8031 if (remote_debug)
43e526b9 8032 {
6e5abd65
PA
8033 struct cleanup *old_chain;
8034 char *str;
8035
8036 str = escape_buffer (*buf, val);
8037 old_chain = make_cleanup (xfree, str);
8038 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8039 do_cleanups (old_chain);
43e526b9 8040 }
a6f3e723
SL
8041
8042 /* Skip the ack char if we're in no-ack mode. */
8043 if (!rs->noack_mode)
c33e31fd 8044 remote_serial_write ("+", 1);
fee9eda9
YQ
8045 if (is_notif != NULL)
8046 *is_notif = 0;
0876f84a 8047 return val;
c906108c
SS
8048 }
8049
74531fed
PA
8050 /* If we got a notification, handle it, and go back to looking
8051 for a packet. */
8052 else
8053 {
8054 gdb_assert (c == '%');
8055
8056 if (remote_debug)
8057 {
6e5abd65
PA
8058 struct cleanup *old_chain;
8059 char *str;
8060
8061 str = escape_buffer (*buf, val);
8062 old_chain = make_cleanup (xfree, str);
8063 fprintf_unfiltered (gdb_stdlog,
8064 " Notification received: %s\n",
8065 str);
8066 do_cleanups (old_chain);
74531fed 8067 }
fee9eda9
YQ
8068 if (is_notif != NULL)
8069 *is_notif = 1;
c906108c 8070
5965e028 8071 handle_notification (rs->notif_state, *buf);
c906108c 8072
74531fed 8073 /* Notifications require no acknowledgement. */
a6f3e723 8074
74531fed 8075 if (expecting_notif)
fee9eda9 8076 return val;
74531fed
PA
8077 }
8078 }
8079}
8080
8081static int
8082getpkt_sane (char **buf, long *sizeof_buf, int forever)
8083{
fee9eda9 8084 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8085}
8086
8087static int
fee9eda9
YQ
8088getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8089 int *is_notif)
74531fed 8090{
fee9eda9
YQ
8091 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8092 is_notif);
c906108c 8093}
74531fed 8094
cbb8991c
DB
8095/* Check whether EVENT is a fork event for the process specified
8096 by the pid passed in DATA, and if it is, kill the fork child. */
8097
8098static int
8099kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8100 QUEUE_ITER (stop_reply_p) *iter,
8101 stop_reply_p event,
8102 void *data)
8103{
8104 struct queue_iter_param *param = data;
8105 int parent_pid = *(int *) param->input;
8106
8107 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8108 {
8109 struct remote_state *rs = get_remote_state ();
8110 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8111 int res;
8112
8113 res = remote_vkill (child_pid, rs);
8114 if (res != 0)
8115 error (_("Can't kill fork child process %d"), child_pid);
8116 }
8117
8118 return 1;
8119}
8120
8121/* Kill any new fork children of process PID that haven't been
8122 processed by follow_fork. */
8123
8124static void
8125kill_new_fork_children (int pid, struct remote_state *rs)
8126{
8127 struct thread_info *thread;
8128 struct notif_client *notif = &notif_client_stop;
8129 struct queue_iter_param param;
8130
8131 /* Kill the fork child threads of any threads in process PID
8132 that are stopped at a fork event. */
8133 ALL_NON_EXITED_THREADS (thread)
8134 {
8135 struct target_waitstatus *ws = &thread->pending_follow;
8136
8137 if (is_pending_fork_parent (ws, pid, thread->ptid))
8138 {
8139 struct remote_state *rs = get_remote_state ();
8140 int child_pid = ptid_get_pid (ws->value.related_pid);
8141 int res;
8142
8143 res = remote_vkill (child_pid, rs);
8144 if (res != 0)
8145 error (_("Can't kill fork child process %d"), child_pid);
8146 }
8147 }
8148
8149 /* Check for any pending fork events (not reported or processed yet)
8150 in process PID and kill those fork child threads as well. */
8151 remote_notif_get_pending_events (notif);
8152 param.input = &pid;
8153 param.output = NULL;
8154 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8155 kill_child_of_pending_fork, &param);
8156}
8157
c906108c
SS
8158\f
8159static void
7d85a9c0 8160remote_kill (struct target_ops *ops)
43ff13b4 8161{
0fdf84ca
PA
8162
8163 /* Catch errors so the user can quit from gdb even when we
23860348 8164 aren't on speaking terms with the remote system. */
492d29ea 8165 TRY
0fdf84ca
PA
8166 {
8167 putpkt ("k");
8168 }
492d29ea 8169 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8170 {
8171 if (ex.error == TARGET_CLOSE_ERROR)
8172 {
8173 /* If we got an (EOF) error that caused the target
8174 to go away, then we're done, that's what we wanted.
8175 "k" is susceptible to cause a premature EOF, given
8176 that the remote server isn't actually required to
8177 reply to "k", and it can happen that it doesn't
8178 even get to reply ACK to the "k". */
8179 return;
8180 }
8181
8182 /* Otherwise, something went wrong. We didn't actually kill
8183 the target. Just propagate the exception, and let the
8184 user or higher layers decide what to do. */
8185 throw_exception (ex);
8186 }
492d29ea 8187 END_CATCH
43ff13b4 8188
0fdf84ca
PA
8189 /* We've killed the remote end, we get to mourn it. Since this is
8190 target remote, single-process, mourning the inferior also
8191 unpushes remote_ops. */
43ff13b4
JM
8192 target_mourn_inferior ();
8193}
8194
82f73884
PA
8195static int
8196remote_vkill (int pid, struct remote_state *rs)
8197{
4082afcc 8198 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8199 return -1;
8200
8201 /* Tell the remote target to detach. */
bba74b36 8202 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8203 putpkt (rs->buf);
8204 getpkt (&rs->buf, &rs->buf_size, 0);
8205
4082afcc
PA
8206 switch (packet_ok (rs->buf,
8207 &remote_protocol_packets[PACKET_vKill]))
8208 {
8209 case PACKET_OK:
8210 return 0;
8211 case PACKET_ERROR:
8212 return 1;
8213 case PACKET_UNKNOWN:
8214 return -1;
8215 default:
8216 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8217 }
82f73884
PA
8218}
8219
8220static void
7d85a9c0 8221extended_remote_kill (struct target_ops *ops)
82f73884
PA
8222{
8223 int res;
8224 int pid = ptid_get_pid (inferior_ptid);
8225 struct remote_state *rs = get_remote_state ();
8226
cbb8991c
DB
8227 /* If we're stopped while forking and we haven't followed yet, kill the
8228 child task. We need to do this before killing the parent task
8229 because if this is a vfork then the parent will be sleeping. */
8230 kill_new_fork_children (pid, rs);
8231
82f73884 8232 res = remote_vkill (pid, rs);
901f9912 8233 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8234 {
8235 /* Don't try 'k' on a multi-process aware stub -- it has no way
8236 to specify the pid. */
8237
8238 putpkt ("k");
8239#if 0
8240 getpkt (&rs->buf, &rs->buf_size, 0);
8241 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8242 res = 1;
8243#else
8244 /* Don't wait for it to die. I'm not really sure it matters whether
8245 we do or not. For the existing stubs, kill is a noop. */
8246 res = 0;
8247#endif
8248 }
8249
8250 if (res != 0)
8251 error (_("Can't kill process"));
8252
82f73884
PA
8253 target_mourn_inferior ();
8254}
8255
c906108c 8256static void
20f796c9 8257remote_mourn (struct target_ops *target)
c906108c
SS
8258{
8259 unpush_target (target);
ce5ce7ed 8260
8a2492ee
PA
8261 /* remote_close takes care of doing most of the clean up. */
8262 generic_mourn_inferior ();
c906108c
SS
8263}
8264
2d717e4f 8265static void
20f796c9 8266extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8267{
8268 struct remote_state *rs = get_remote_state ();
c906108c 8269
e24a49d8
PA
8270 /* In case we got here due to an error, but we're going to stay
8271 connected. */
8272 rs->waiting_for_stop_reply = 0;
8273
dc1981d7
PA
8274 /* If the current general thread belonged to the process we just
8275 detached from or has exited, the remote side current general
8276 thread becomes undefined. Considering a case like this:
8277
8278 - We just got here due to a detach.
8279 - The process that we're detaching from happens to immediately
8280 report a global breakpoint being hit in non-stop mode, in the
8281 same thread we had selected before.
8282 - GDB attaches to this process again.
8283 - This event happens to be the next event we handle.
8284
8285 GDB would consider that the current general thread didn't need to
8286 be set on the stub side (with Hg), since for all it knew,
8287 GENERAL_THREAD hadn't changed.
8288
8289 Notice that although in all-stop mode, the remote server always
8290 sets the current thread to the thread reporting the stop event,
8291 that doesn't happen in non-stop mode; in non-stop, the stub *must
8292 not* change the current thread when reporting a breakpoint hit,
8293 due to the decoupling of event reporting and event handling.
8294
8295 To keep things simple, we always invalidate our notion of the
8296 current thread. */
47f8a51d 8297 record_currthread (rs, minus_one_ptid);
dc1981d7 8298
2d717e4f
DJ
8299 /* Unlike "target remote", we do not want to unpush the target; then
8300 the next time the user says "run", we won't be connected. */
8301
48aa3c27
PA
8302 /* Call common code to mark the inferior as not running. */
8303 generic_mourn_inferior ();
8304
d729566a 8305 if (!have_inferiors ())
2d717e4f 8306 {
82f73884
PA
8307 if (!remote_multi_process_p (rs))
8308 {
8309 /* Check whether the target is running now - some remote stubs
8310 automatically restart after kill. */
8311 putpkt ("?");
8312 getpkt (&rs->buf, &rs->buf_size, 0);
8313
8314 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8315 {
3e43a32a
MS
8316 /* Assume that the target has been restarted. Set
8317 inferior_ptid so that bits of core GDB realizes
8318 there's something here, e.g., so that the user can
8319 say "kill" again. */
82f73884
PA
8320 inferior_ptid = magic_null_ptid;
8321 }
82f73884 8322 }
2d717e4f
DJ
8323 }
8324}
c906108c 8325
03583c20 8326static int
2bfc0540 8327extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8328{
4082afcc 8329 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8330}
8331
8332static void
8333extended_remote_disable_randomization (int val)
8334{
8335 struct remote_state *rs = get_remote_state ();
8336 char *reply;
8337
bba74b36
YQ
8338 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8339 val);
03583c20
UW
8340 putpkt (rs->buf);
8341 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8342 if (*reply == '\0')
8343 error (_("Target does not support QDisableRandomization."));
8344 if (strcmp (reply, "OK") != 0)
8345 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8346}
8347
2d717e4f
DJ
8348static int
8349extended_remote_run (char *args)
8350{
8351 struct remote_state *rs = get_remote_state ();
2d717e4f 8352 int len;
c906108c 8353
2d717e4f
DJ
8354 /* If the user has disabled vRun support, or we have detected that
8355 support is not available, do not try it. */
4082afcc 8356 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8357 return -1;
424163ea 8358
2d717e4f
DJ
8359 strcpy (rs->buf, "vRun;");
8360 len = strlen (rs->buf);
c906108c 8361
2d717e4f
DJ
8362 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8363 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8364 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8365 strlen (remote_exec_file));
2d717e4f 8366
d1a41061 8367 gdb_assert (args != NULL);
2d717e4f
DJ
8368 if (*args)
8369 {
8370 struct cleanup *back_to;
8371 int i;
8372 char **argv;
8373
d1a41061 8374 argv = gdb_buildargv (args);
6e366df1 8375 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8376 for (i = 0; argv[i] != NULL; i++)
8377 {
8378 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8379 error (_("Argument list too long for run packet"));
8380 rs->buf[len++] = ';';
9f1b45b0
TT
8381 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8382 strlen (argv[i]));
2d717e4f
DJ
8383 }
8384 do_cleanups (back_to);
8385 }
8386
8387 rs->buf[len++] = '\0';
8388
8389 putpkt (rs->buf);
8390 getpkt (&rs->buf, &rs->buf_size, 0);
8391
4082afcc 8392 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8393 {
4082afcc 8394 case PACKET_OK:
3405876a 8395 /* We have a wait response. All is well. */
2d717e4f 8396 return 0;
4082afcc
PA
8397 case PACKET_UNKNOWN:
8398 return -1;
8399 case PACKET_ERROR:
2d717e4f
DJ
8400 if (remote_exec_file[0] == '\0')
8401 error (_("Running the default executable on the remote target failed; "
8402 "try \"set remote exec-file\"?"));
8403 else
8404 error (_("Running \"%s\" on the remote target failed"),
8405 remote_exec_file);
4082afcc
PA
8406 default:
8407 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8408 }
c906108c
SS
8409}
8410
2d717e4f
DJ
8411/* In the extended protocol we want to be able to do things like
8412 "run" and have them basically work as expected. So we need
8413 a special create_inferior function. We support changing the
8414 executable file and the command line arguments, but not the
8415 environment. */
8416
43ff13b4 8417static void
77a19445
TT
8418extended_remote_create_inferior (struct target_ops *ops,
8419 char *exec_file, char *args,
8420 char **env, int from_tty)
43ff13b4 8421{
3405876a
PA
8422 int run_worked;
8423 char *stop_reply;
8424 struct remote_state *rs = get_remote_state ();
8425
43ff13b4 8426 /* If running asynchronously, register the target file descriptor
23860348 8427 with the event loop. */
75c99385 8428 if (target_can_async_p ())
6a3753b3 8429 target_async (1);
43ff13b4 8430
03583c20 8431 /* Disable address space randomization if requested (and supported). */
2bfc0540 8432 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8433 extended_remote_disable_randomization (disable_randomization);
8434
43ff13b4 8435 /* Now restart the remote server. */
3405876a
PA
8436 run_worked = extended_remote_run (args) != -1;
8437 if (!run_worked)
2d717e4f
DJ
8438 {
8439 /* vRun was not supported. Fail if we need it to do what the
8440 user requested. */
8441 if (remote_exec_file[0])
8442 error (_("Remote target does not support \"set remote exec-file\""));
8443 if (args[0])
8444 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8445
2d717e4f
DJ
8446 /* Fall back to "R". */
8447 extended_remote_restart ();
8448 }
424163ea 8449
6c95b8df
PA
8450 if (!have_inferiors ())
8451 {
8452 /* Clean up from the last time we ran, before we mark the target
8453 running again. This will mark breakpoints uninserted, and
8454 get_offsets may insert breakpoints. */
8455 init_thread_list ();
8456 init_wait_for_inferior ();
8457 }
45280a52 8458
3405876a
PA
8459 /* vRun's success return is a stop reply. */
8460 stop_reply = run_worked ? rs->buf : NULL;
8461 add_current_inferior_and_thread (stop_reply);
c0a2216e 8462
2d717e4f
DJ
8463 /* Get updated offsets, if the stub uses qOffsets. */
8464 get_offsets ();
2d717e4f 8465}
c906108c 8466\f
c5aa993b 8467
b775012e
LM
8468/* Given a location's target info BP_TGT and the packet buffer BUF, output
8469 the list of conditions (in agent expression bytecode format), if any, the
8470 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8471 started from BUF and ended at BUF_END. */
b775012e
LM
8472
8473static int
8474remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8475 struct bp_target_info *bp_tgt, char *buf,
8476 char *buf_end)
b775012e
LM
8477{
8478 struct agent_expr *aexpr = NULL;
8479 int i, ix;
8480 char *pkt;
8481 char *buf_start = buf;
8482
8483 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8484 return 0;
8485
8486 buf += strlen (buf);
bba74b36 8487 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8488 buf++;
8489
8490 /* Send conditions to the target and free the vector. */
8491 for (ix = 0;
8492 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8493 ix++)
8494 {
bba74b36 8495 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8496 buf += strlen (buf);
8497 for (i = 0; i < aexpr->len; ++i)
8498 buf = pack_hex_byte (buf, aexpr->buf[i]);
8499 *buf = '\0';
8500 }
b775012e
LM
8501 return 0;
8502}
8503
d3ce09f5
SS
8504static void
8505remote_add_target_side_commands (struct gdbarch *gdbarch,
8506 struct bp_target_info *bp_tgt, char *buf)
8507{
8508 struct agent_expr *aexpr = NULL;
8509 int i, ix;
8510
8511 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8512 return;
8513
8514 buf += strlen (buf);
8515
8516 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8517 buf += strlen (buf);
8518
8519 /* Concatenate all the agent expressions that are commands into the
8520 cmds parameter. */
8521 for (ix = 0;
8522 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8523 ix++)
8524 {
8525 sprintf (buf, "X%x,", aexpr->len);
8526 buf += strlen (buf);
8527 for (i = 0; i < aexpr->len; ++i)
8528 buf = pack_hex_byte (buf, aexpr->buf[i]);
8529 *buf = '\0';
8530 }
d3ce09f5
SS
8531}
8532
8181d85f
DJ
8533/* Insert a breakpoint. On targets that have software breakpoint
8534 support, we ask the remote target to do the work; on targets
8535 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8536
8537static int
3db08215
MM
8538remote_insert_breakpoint (struct target_ops *ops,
8539 struct gdbarch *gdbarch,
a6d9a66e 8540 struct bp_target_info *bp_tgt)
c906108c 8541{
d471ea57
AC
8542 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8543 If it succeeds, then set the support to PACKET_ENABLE. If it
8544 fails, and the user has explicitly requested the Z support then
23860348 8545 report an error, otherwise, mark it disabled and go on. */
802188a7 8546
4082afcc 8547 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8548 {
0d5ed153 8549 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8550 struct remote_state *rs;
bba74b36 8551 char *p, *endbuf;
7c0f6dcc 8552 int bpsize;
b775012e 8553 struct condition_list *cond = NULL;
4fff2411 8554
28439a30
PA
8555 /* Make sure the remote is pointing at the right process, if
8556 necessary. */
8557 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8558 set_general_process ();
8559
a1dcb23a 8560 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8561
8562 rs = get_remote_state ();
8563 p = rs->buf;
bba74b36 8564 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8565
96baa820
JM
8566 *(p++) = 'Z';
8567 *(p++) = '0';
8568 *(p++) = ',';
7c0f6dcc 8569 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8570 p += hexnumstr (p, addr);
bba74b36 8571 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8572
efcc2da7 8573 if (remote_supports_cond_breakpoints (ops))
bba74b36 8574 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8575
78eff0ec 8576 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8577 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8578
6d820c5c
DJ
8579 putpkt (rs->buf);
8580 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8581
6d820c5c 8582 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8583 {
d471ea57
AC
8584 case PACKET_ERROR:
8585 return -1;
8586 case PACKET_OK:
7c0f6dcc
JL
8587 bp_tgt->placed_address = addr;
8588 bp_tgt->placed_size = bpsize;
d471ea57
AC
8589 return 0;
8590 case PACKET_UNKNOWN:
8591 break;
96baa820
JM
8592 }
8593 }
c906108c 8594
0000e5cc
PA
8595 /* If this breakpoint has target-side commands but this stub doesn't
8596 support Z0 packets, throw error. */
8597 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8598 throw_error (NOT_SUPPORTED_ERROR, _("\
8599Target doesn't support breakpoints that have target side commands."));
8600
3db08215 8601 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8602}
8603
8604static int
3db08215
MM
8605remote_remove_breakpoint (struct target_ops *ops,
8606 struct gdbarch *gdbarch,
a6d9a66e 8607 struct bp_target_info *bp_tgt)
c906108c 8608{
8181d85f 8609 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8610 struct remote_state *rs = get_remote_state ();
96baa820 8611
4082afcc 8612 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8613 {
6d820c5c 8614 char *p = rs->buf;
bba74b36 8615 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8616
28439a30
PA
8617 /* Make sure the remote is pointing at the right process, if
8618 necessary. */
8619 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8620 set_general_process ();
8621
96baa820
JM
8622 *(p++) = 'z';
8623 *(p++) = '0';
8624 *(p++) = ',';
8625
8181d85f
DJ
8626 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8627 p += hexnumstr (p, addr);
bba74b36 8628 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8629
6d820c5c
DJ
8630 putpkt (rs->buf);
8631 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8632
6d820c5c 8633 return (rs->buf[0] == 'E');
96baa820
JM
8634 }
8635
3db08215 8636 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8637}
8638
d471ea57
AC
8639static int
8640watchpoint_to_Z_packet (int type)
8641{
8642 switch (type)
8643 {
8644 case hw_write:
bb858e6a 8645 return Z_PACKET_WRITE_WP;
d471ea57
AC
8646 break;
8647 case hw_read:
bb858e6a 8648 return Z_PACKET_READ_WP;
d471ea57
AC
8649 break;
8650 case hw_access:
bb858e6a 8651 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8652 break;
8653 default:
8e65ff28 8654 internal_error (__FILE__, __LINE__,
e2e0b3e5 8655 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8656 }
8657}
8658
3c3bea1c 8659static int
7bb99c53
TT
8660remote_insert_watchpoint (struct target_ops *self,
8661 CORE_ADDR addr, int len, int type,
0cf6dd15 8662 struct expression *cond)
96baa820 8663{
d01949b6 8664 struct remote_state *rs = get_remote_state ();
bba74b36 8665 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8666 char *p;
d471ea57 8667 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8668
4082afcc 8669 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8670 return 1;
802188a7 8671
28439a30
PA
8672 /* Make sure the remote is pointing at the right process, if
8673 necessary. */
8674 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8675 set_general_process ();
8676
bba74b36 8677 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8678 p = strchr (rs->buf, '\0');
96baa820
JM
8679 addr = remote_address_masked (addr);
8680 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8681 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8682
6d820c5c
DJ
8683 putpkt (rs->buf);
8684 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8685
6d820c5c 8686 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8687 {
8688 case PACKET_ERROR:
d471ea57 8689 return -1;
85d721b8
PA
8690 case PACKET_UNKNOWN:
8691 return 1;
d471ea57
AC
8692 case PACKET_OK:
8693 return 0;
8694 }
8e65ff28 8695 internal_error (__FILE__, __LINE__,
e2e0b3e5 8696 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8697}
8698
283002cf
MR
8699static int
8700remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8701 CORE_ADDR start, int length)
8702{
8703 CORE_ADDR diff = remote_address_masked (addr - start);
8704
8705 return diff < length;
8706}
8707
d471ea57 8708
3c3bea1c 8709static int
11b5219a
TT
8710remote_remove_watchpoint (struct target_ops *self,
8711 CORE_ADDR addr, int len, int type,
0cf6dd15 8712 struct expression *cond)
96baa820 8713{
d01949b6 8714 struct remote_state *rs = get_remote_state ();
bba74b36 8715 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8716 char *p;
d471ea57
AC
8717 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8718
4082afcc 8719 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8720 return -1;
802188a7 8721
28439a30
PA
8722 /* Make sure the remote is pointing at the right process, if
8723 necessary. */
8724 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8725 set_general_process ();
8726
bba74b36 8727 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8728 p = strchr (rs->buf, '\0');
96baa820
JM
8729 addr = remote_address_masked (addr);
8730 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8731 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8732 putpkt (rs->buf);
8733 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8734
6d820c5c 8735 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8736 {
8737 case PACKET_ERROR:
8738 case PACKET_UNKNOWN:
8739 return -1;
8740 case PACKET_OK:
8741 return 0;
8742 }
8e65ff28 8743 internal_error (__FILE__, __LINE__,
e2e0b3e5 8744 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8745}
8746
3c3bea1c 8747
501eef12 8748int remote_hw_watchpoint_limit = -1;
480a3f21 8749int remote_hw_watchpoint_length_limit = -1;
501eef12 8750int remote_hw_breakpoint_limit = -1;
d471ea57 8751
480a3f21 8752static int
31568a15
TT
8753remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8754 CORE_ADDR addr, int len)
480a3f21
PW
8755{
8756 if (remote_hw_watchpoint_length_limit == 0)
8757 return 0;
8758 else if (remote_hw_watchpoint_length_limit < 0)
8759 return 1;
8760 else if (len <= remote_hw_watchpoint_length_limit)
8761 return 1;
8762 else
8763 return 0;
8764}
8765
b9362cc7 8766static int
5461485a
TT
8767remote_check_watch_resources (struct target_ops *self,
8768 int type, int cnt, int ot)
96baa820 8769{
3c3bea1c
GS
8770 if (type == bp_hardware_breakpoint)
8771 {
8772 if (remote_hw_breakpoint_limit == 0)
8773 return 0;
501eef12
AC
8774 else if (remote_hw_breakpoint_limit < 0)
8775 return 1;
3c3bea1c
GS
8776 else if (cnt <= remote_hw_breakpoint_limit)
8777 return 1;
8778 }
8779 else
8780 {
8781 if (remote_hw_watchpoint_limit == 0)
8782 return 0;
501eef12
AC
8783 else if (remote_hw_watchpoint_limit < 0)
8784 return 1;
3c3bea1c
GS
8785 else if (ot)
8786 return -1;
8787 else if (cnt <= remote_hw_watchpoint_limit)
8788 return 1;
8789 }
8790 return -1;
8791}
8792
f7e6eed5
PA
8793/* The to_stopped_by_sw_breakpoint method of target remote. */
8794
8795static int
8796remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8797{
8798 struct remote_state *rs = get_remote_state ();
8799
8800 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8801}
8802
8803/* The to_supports_stopped_by_sw_breakpoint method of target
8804 remote. */
8805
8806static int
8807remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8808{
8809 struct remote_state *rs = get_remote_state ();
8810
8811 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8812}
8813
8814/* The to_stopped_by_hw_breakpoint method of target remote. */
8815
8816static int
8817remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8818{
8819 struct remote_state *rs = get_remote_state ();
8820
8821 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8822}
8823
8824/* The to_supports_stopped_by_hw_breakpoint method of target
8825 remote. */
8826
8827static int
8828remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8829{
8830 struct remote_state *rs = get_remote_state ();
8831
8832 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8833}
8834
b9362cc7 8835static int
6a109b6b 8836remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8837{
ee154bee
TT
8838 struct remote_state *rs = get_remote_state ();
8839
f7e6eed5 8840 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8841}
8842
4aa7a7f5
JJ
8843static int
8844remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8845{
ee154bee 8846 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8847 int rc = 0;
a744cf53 8848
6a109b6b 8849 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8850 {
ee154bee 8851 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8852 rc = 1;
8853 }
8854
8855 return rc;
3c3bea1c
GS
8856}
8857
8858
8859static int
23a26771 8860remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8861 struct bp_target_info *bp_tgt)
3c3bea1c 8862{
0d5ed153 8863 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8864 struct remote_state *rs;
bba74b36 8865 char *p, *endbuf;
dd61ec5c 8866 char *message;
0d5ed153 8867 int bpsize;
802188a7 8868
c8189ed1 8869 /* The length field should be set to the size of a breakpoint
8181d85f 8870 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8871
0d5ed153 8872 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8873
4082afcc 8874 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8875 return -1;
2bc416ba 8876
28439a30
PA
8877 /* Make sure the remote is pointing at the right process, if
8878 necessary. */
8879 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8880 set_general_process ();
8881
4fff2411
JZ
8882 rs = get_remote_state ();
8883 p = rs->buf;
bba74b36 8884 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8885
96baa820
JM
8886 *(p++) = 'Z';
8887 *(p++) = '1';
8888 *(p++) = ',';
802188a7 8889
0d5ed153 8890 addr = remote_address_masked (addr);
96baa820 8891 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8892 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8893
efcc2da7 8894 if (remote_supports_cond_breakpoints (self))
bba74b36 8895 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8896
78eff0ec 8897 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8898 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8899
6d820c5c
DJ
8900 putpkt (rs->buf);
8901 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8902
6d820c5c 8903 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8904 {
8905 case PACKET_ERROR:
dd61ec5c
MW
8906 if (rs->buf[1] == '.')
8907 {
8908 message = strchr (rs->buf + 2, '.');
8909 if (message)
0316657e 8910 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
8911 }
8912 return -1;
d471ea57
AC
8913 case PACKET_UNKNOWN:
8914 return -1;
8915 case PACKET_OK:
0d5ed153
MR
8916 bp_tgt->placed_address = addr;
8917 bp_tgt->placed_size = bpsize;
d471ea57
AC
8918 return 0;
8919 }
8e65ff28 8920 internal_error (__FILE__, __LINE__,
e2e0b3e5 8921 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8922}
8923
d471ea57 8924
802188a7 8925static int
a64dc96c 8926remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8927 struct bp_target_info *bp_tgt)
96baa820 8928{
8181d85f 8929 CORE_ADDR addr;
d01949b6 8930 struct remote_state *rs = get_remote_state ();
6d820c5c 8931 char *p = rs->buf;
bba74b36 8932 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 8933
4082afcc 8934 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8935 return -1;
802188a7 8936
28439a30
PA
8937 /* Make sure the remote is pointing at the right process, if
8938 necessary. */
8939 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8940 set_general_process ();
8941
96baa820
JM
8942 *(p++) = 'z';
8943 *(p++) = '1';
8944 *(p++) = ',';
802188a7 8945
8181d85f 8946 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8947 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8948 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8949
6d820c5c
DJ
8950 putpkt (rs->buf);
8951 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8952
6d820c5c 8953 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8954 {
8955 case PACKET_ERROR:
8956 case PACKET_UNKNOWN:
8957 return -1;
8958 case PACKET_OK:
8959 return 0;
8960 }
8e65ff28 8961 internal_error (__FILE__, __LINE__,
e2e0b3e5 8962 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 8963}
96baa820 8964
4a5e7a5b
PA
8965/* Verify memory using the "qCRC:" request. */
8966
8967static int
8968remote_verify_memory (struct target_ops *ops,
8969 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8970{
8971 struct remote_state *rs = get_remote_state ();
8972 unsigned long host_crc, target_crc;
8973 char *tmp;
8974
936d2992
PA
8975 /* It doesn't make sense to use qCRC if the remote target is
8976 connected but not running. */
8977 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
8978 {
8979 enum packet_result result;
28439a30 8980
936d2992
PA
8981 /* Make sure the remote is pointing at the right process. */
8982 set_general_process ();
4a5e7a5b 8983
936d2992
PA
8984 /* FIXME: assumes lma can fit into long. */
8985 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8986 (long) lma, (long) size);
8987 putpkt (rs->buf);
4a5e7a5b 8988
936d2992
PA
8989 /* Be clever; compute the host_crc before waiting for target
8990 reply. */
8991 host_crc = xcrc32 (data, size, 0xffffffff);
8992
8993 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 8994
936d2992
PA
8995 result = packet_ok (rs->buf,
8996 &remote_protocol_packets[PACKET_qCRC]);
8997 if (result == PACKET_ERROR)
8998 return -1;
8999 else if (result == PACKET_OK)
9000 {
9001 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9002 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9003
936d2992
PA
9004 return (host_crc == target_crc);
9005 }
9006 }
4a5e7a5b 9007
936d2992 9008 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9009}
9010
c906108c
SS
9011/* compare-sections command
9012
9013 With no arguments, compares each loadable section in the exec bfd
9014 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9015 Useful for verifying the image on the target against the exec file. */
e514a9d6 9016
c906108c 9017static void
fba45db2 9018compare_sections_command (char *args, int from_tty)
c906108c
SS
9019{
9020 asection *s;
c906108c 9021 struct cleanup *old_chain;
948f8e3d 9022 gdb_byte *sectdata;
ce359b09 9023 const char *sectname;
c906108c
SS
9024 bfd_size_type size;
9025 bfd_vma lma;
9026 int matched = 0;
9027 int mismatched = 0;
4a5e7a5b 9028 int res;
95cf3b38 9029 int read_only = 0;
c906108c
SS
9030
9031 if (!exec_bfd)
8a3fe4f8 9032 error (_("command cannot be used without an exec file"));
c906108c 9033
28439a30
PA
9034 /* Make sure the remote is pointing at the right process. */
9035 set_general_process ();
9036
95cf3b38
DT
9037 if (args != NULL && strcmp (args, "-r") == 0)
9038 {
9039 read_only = 1;
9040 args = NULL;
9041 }
9042
c5aa993b 9043 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9044 {
9045 if (!(s->flags & SEC_LOAD))
0df8b418 9046 continue; /* Skip non-loadable section. */
c906108c 9047
95cf3b38
DT
9048 if (read_only && (s->flags & SEC_READONLY) == 0)
9049 continue; /* Skip writeable sections */
9050
2c500098 9051 size = bfd_get_section_size (s);
c906108c 9052 if (size == 0)
0df8b418 9053 continue; /* Skip zero-length section. */
c906108c 9054
ce359b09 9055 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9056 if (args && strcmp (args, sectname) != 0)
0df8b418 9057 continue; /* Not the section selected by user. */
c906108c 9058
0df8b418 9059 matched = 1; /* Do this section. */
c906108c 9060 lma = s->lma;
c906108c 9061
c906108c 9062 sectdata = xmalloc (size);
b8c9b27d 9063 old_chain = make_cleanup (xfree, sectdata);
c906108c 9064 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9065
4a5e7a5b
PA
9066 res = target_verify_memory (sectdata, lma, size);
9067
9068 if (res == -1)
5af949e3 9069 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9070 paddress (target_gdbarch (), lma),
9071 paddress (target_gdbarch (), lma + size));
c906108c 9072
5af949e3 9073 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9074 paddress (target_gdbarch (), lma),
9075 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9076 if (res)
c906108c
SS
9077 printf_filtered ("matched.\n");
9078 else
c5aa993b
JM
9079 {
9080 printf_filtered ("MIS-MATCHED!\n");
9081 mismatched++;
9082 }
c906108c
SS
9083
9084 do_cleanups (old_chain);
9085 }
9086 if (mismatched > 0)
936d2992 9087 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9088the loaded file\n"));
c906108c 9089 if (args && !matched)
a3f17187 9090 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9091}
9092
0e7f50da
UW
9093/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9094 into remote target. The number of bytes written to the remote
9095 target is returned, or -1 for error. */
9096
9b409511 9097static enum target_xfer_status
0e7f50da
UW
9098remote_write_qxfer (struct target_ops *ops, const char *object_name,
9099 const char *annex, const gdb_byte *writebuf,
9b409511 9100 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9101 struct packet_config *packet)
9102{
9103 int i, buf_len;
9104 ULONGEST n;
0e7f50da
UW
9105 struct remote_state *rs = get_remote_state ();
9106 int max_size = get_memory_write_packet_size ();
9107
9108 if (packet->support == PACKET_DISABLE)
2ed4b548 9109 return TARGET_XFER_E_IO;
0e7f50da
UW
9110
9111 /* Insert header. */
9112 i = snprintf (rs->buf, max_size,
9113 "qXfer:%s:write:%s:%s:",
9114 object_name, annex ? annex : "",
9115 phex_nz (offset, sizeof offset));
9116 max_size -= (i + 1);
9117
9118 /* Escape as much data as fits into rs->buf. */
9119 buf_len = remote_escape_output
bc20a4af 9120 (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9121
9122 if (putpkt_binary (rs->buf, i + buf_len) < 0
9123 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9124 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9125 return TARGET_XFER_E_IO;
0e7f50da
UW
9126
9127 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9128
9129 *xfered_len = n;
9130 return TARGET_XFER_OK;
0e7f50da
UW
9131}
9132
0876f84a
DJ
9133/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9134 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9135 number of bytes read is returned, or 0 for EOF, or -1 for error.
9136 The number of bytes read may be less than LEN without indicating an
9137 EOF. PACKET is checked and updated to indicate whether the remote
9138 target supports this object. */
9139
9b409511 9140static enum target_xfer_status
0876f84a
DJ
9141remote_read_qxfer (struct target_ops *ops, const char *object_name,
9142 const char *annex,
9143 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9144 ULONGEST *xfered_len,
0876f84a
DJ
9145 struct packet_config *packet)
9146{
0876f84a 9147 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9148 LONGEST i, n, packet_len;
9149
9150 if (packet->support == PACKET_DISABLE)
2ed4b548 9151 return TARGET_XFER_E_IO;
0876f84a
DJ
9152
9153 /* Check whether we've cached an end-of-object packet that matches
9154 this request. */
8e88304f 9155 if (rs->finished_object)
0876f84a 9156 {
8e88304f
TT
9157 if (strcmp (object_name, rs->finished_object) == 0
9158 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9159 && offset == rs->finished_offset)
9b409511
YQ
9160 return TARGET_XFER_EOF;
9161
0876f84a
DJ
9162
9163 /* Otherwise, we're now reading something different. Discard
9164 the cache. */
8e88304f
TT
9165 xfree (rs->finished_object);
9166 xfree (rs->finished_annex);
9167 rs->finished_object = NULL;
9168 rs->finished_annex = NULL;
0876f84a
DJ
9169 }
9170
9171 /* Request only enough to fit in a single packet. The actual data
9172 may not, since we don't know how much of it will need to be escaped;
9173 the target is free to respond with slightly less data. We subtract
9174 five to account for the response type and the protocol frame. */
9175 n = min (get_remote_packet_size () - 5, len);
9176 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9177 object_name, annex ? annex : "",
9178 phex_nz (offset, sizeof offset),
9179 phex_nz (n, sizeof n));
9180 i = putpkt (rs->buf);
9181 if (i < 0)
2ed4b548 9182 return TARGET_XFER_E_IO;
0876f84a
DJ
9183
9184 rs->buf[0] = '\0';
9185 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9186 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9187 return TARGET_XFER_E_IO;
0876f84a
DJ
9188
9189 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9190 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9191
9192 /* 'm' means there is (or at least might be) more data after this
9193 batch. That does not make sense unless there's at least one byte
9194 of data in this reply. */
9195 if (rs->buf[0] == 'm' && packet_len == 1)
9196 error (_("Remote qXfer reply contained no data."));
9197
9198 /* Got some data. */
bc20a4af
PA
9199 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9200 packet_len - 1, readbuf, n);
0876f84a
DJ
9201
9202 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9203 or possibly empty. If we have the final block of a non-empty
9204 object, record this fact to bypass a subsequent partial read. */
9205 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9206 {
8e88304f
TT
9207 rs->finished_object = xstrdup (object_name);
9208 rs->finished_annex = xstrdup (annex ? annex : "");
9209 rs->finished_offset = offset + i;
0876f84a
DJ
9210 }
9211
9b409511
YQ
9212 if (i == 0)
9213 return TARGET_XFER_EOF;
9214 else
9215 {
9216 *xfered_len = i;
9217 return TARGET_XFER_OK;
9218 }
0876f84a
DJ
9219}
9220
9b409511 9221static enum target_xfer_status
4b8a223f 9222remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9223 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9224 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9225 ULONGEST *xfered_len)
c906108c 9226{
82f73884 9227 struct remote_state *rs;
c906108c 9228 int i;
6d820c5c 9229 char *p2;
1e3ff5ad 9230 char query_type;
c906108c 9231
e6e4e701 9232 set_remote_traceframe ();
82f73884
PA
9233 set_general_thread (inferior_ptid);
9234
9235 rs = get_remote_state ();
9236
b2182ed2 9237 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9238 if (object == TARGET_OBJECT_MEMORY)
9239 {
2d717e4f
DJ
9240 /* If the remote target is connected but not running, we should
9241 pass this request down to a lower stratum (e.g. the executable
9242 file). */
9243 if (!target_has_execution)
9b409511 9244 return TARGET_XFER_EOF;
2d717e4f 9245
21e3b9b9 9246 if (writebuf != NULL)
9b409511 9247 return remote_write_bytes (offset, writebuf, len, xfered_len);
21e3b9b9 9248 else
8acf9577 9249 return remote_read_bytes (ops, offset, readbuf, len, xfered_len);
21e3b9b9
DJ
9250 }
9251
0df8b418 9252 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9253 if (object == TARGET_OBJECT_SPU)
9254 {
9255 if (readbuf)
9256 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9257 xfered_len, &remote_protocol_packets
9258 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9259 else
9260 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9261 xfered_len, &remote_protocol_packets
9262 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9263 }
9264
4aa995e1
PA
9265 /* Handle extra signal info using qxfer packets. */
9266 if (object == TARGET_OBJECT_SIGNAL_INFO)
9267 {
9268 if (readbuf)
9269 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9270 xfered_len, &remote_protocol_packets
4aa995e1
PA
9271 [PACKET_qXfer_siginfo_read]);
9272 else
3e43a32a 9273 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9274 writebuf, offset, len, xfered_len,
4aa995e1
PA
9275 &remote_protocol_packets
9276 [PACKET_qXfer_siginfo_write]);
9277 }
9278
0fb4aa4b
PA
9279 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9280 {
9281 if (readbuf)
3e43a32a 9282 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9283 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9284 &remote_protocol_packets
9285 [PACKET_qXfer_statictrace_read]);
9286 else
2ed4b548 9287 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9288 }
9289
a76d924d
DJ
9290 /* Only handle flash writes. */
9291 if (writebuf != NULL)
9292 {
9293 LONGEST xfered;
9294
9295 switch (object)
9296 {
9297 case TARGET_OBJECT_FLASH:
9b409511
YQ
9298 return remote_flash_write (ops, offset, len, xfered_len,
9299 writebuf);
a76d924d
DJ
9300
9301 default:
2ed4b548 9302 return TARGET_XFER_E_IO;
a76d924d
DJ
9303 }
9304 }
4b8a223f 9305
1e3ff5ad
AC
9306 /* Map pre-existing objects onto letters. DO NOT do this for new
9307 objects!!! Instead specify new query packets. */
9308 switch (object)
c906108c 9309 {
1e3ff5ad
AC
9310 case TARGET_OBJECT_AVR:
9311 query_type = 'R';
9312 break;
802188a7
RM
9313
9314 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9315 gdb_assert (annex == NULL);
9316 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9317 xfered_len,
0876f84a 9318 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9319
23181151
DJ
9320 case TARGET_OBJECT_AVAILABLE_FEATURES:
9321 return remote_read_qxfer
9b409511 9322 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9323 &remote_protocol_packets[PACKET_qXfer_features]);
9324
cfa9d6d9
DJ
9325 case TARGET_OBJECT_LIBRARIES:
9326 return remote_read_qxfer
9b409511 9327 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9328 &remote_protocol_packets[PACKET_qXfer_libraries]);
9329
2268b414
JK
9330 case TARGET_OBJECT_LIBRARIES_SVR4:
9331 return remote_read_qxfer
9b409511 9332 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9333 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9334
fd79ecee
DJ
9335 case TARGET_OBJECT_MEMORY_MAP:
9336 gdb_assert (annex == NULL);
9337 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9338 xfered_len,
fd79ecee
DJ
9339 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9340
07e059b5
VP
9341 case TARGET_OBJECT_OSDATA:
9342 /* Should only get here if we're connected. */
5d93a237 9343 gdb_assert (rs->remote_desc);
07e059b5 9344 return remote_read_qxfer
9b409511 9345 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9346 &remote_protocol_packets[PACKET_qXfer_osdata]);
9347
dc146f7c
VP
9348 case TARGET_OBJECT_THREADS:
9349 gdb_assert (annex == NULL);
9350 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9351 xfered_len,
dc146f7c
VP
9352 &remote_protocol_packets[PACKET_qXfer_threads]);
9353
b3b9301e
PA
9354 case TARGET_OBJECT_TRACEFRAME_INFO:
9355 gdb_assert (annex == NULL);
9356 return remote_read_qxfer
9b409511 9357 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9358 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9359
9360 case TARGET_OBJECT_FDPIC:
9361 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9362 xfered_len,
78d85199 9363 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9364
9365 case TARGET_OBJECT_OPENVMS_UIB:
9366 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9367 xfered_len,
169081d0
TG
9368 &remote_protocol_packets[PACKET_qXfer_uib]);
9369
9accd112
MM
9370 case TARGET_OBJECT_BTRACE:
9371 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9372 xfered_len,
9accd112
MM
9373 &remote_protocol_packets[PACKET_qXfer_btrace]);
9374
f4abbc16
MM
9375 case TARGET_OBJECT_BTRACE_CONF:
9376 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9377 len, xfered_len,
9378 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9379
c78fa86a
GB
9380 case TARGET_OBJECT_EXEC_FILE:
9381 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9382 len, xfered_len,
9383 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9384
1e3ff5ad 9385 default:
2ed4b548 9386 return TARGET_XFER_E_IO;
c906108c
SS
9387 }
9388
0df8b418 9389 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9390 large enough let the caller deal with it. */
ea9c271d 9391 if (len < get_remote_packet_size ())
2ed4b548 9392 return TARGET_XFER_E_IO;
ea9c271d 9393 len = get_remote_packet_size ();
1e3ff5ad 9394
23860348 9395 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9396 if (!rs->remote_desc)
8a3fe4f8 9397 error (_("remote query is only available after target open"));
c906108c 9398
1e3ff5ad 9399 gdb_assert (annex != NULL);
4b8a223f 9400 gdb_assert (readbuf != NULL);
c906108c 9401
6d820c5c 9402 p2 = rs->buf;
c906108c
SS
9403 *p2++ = 'q';
9404 *p2++ = query_type;
9405
23860348
MS
9406 /* We used one buffer char for the remote protocol q command and
9407 another for the query type. As the remote protocol encapsulation
9408 uses 4 chars plus one extra in case we are debugging
9409 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9410 string. */
c906108c 9411 i = 0;
ea9c271d 9412 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9413 {
1e3ff5ad
AC
9414 /* Bad caller may have sent forbidden characters. */
9415 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9416 *p2++ = annex[i];
c906108c
SS
9417 i++;
9418 }
1e3ff5ad
AC
9419 *p2 = '\0';
9420 gdb_assert (annex[i] == '\0');
c906108c 9421
6d820c5c 9422 i = putpkt (rs->buf);
c5aa993b 9423 if (i < 0)
2ed4b548 9424 return TARGET_XFER_E_IO;
c906108c 9425
6d820c5c
DJ
9426 getpkt (&rs->buf, &rs->buf_size, 0);
9427 strcpy ((char *) readbuf, rs->buf);
c906108c 9428
9b409511
YQ
9429 *xfered_len = strlen ((char *) readbuf);
9430 return TARGET_XFER_OK;
c906108c
SS
9431}
9432
08388c79
DE
9433static int
9434remote_search_memory (struct target_ops* ops,
9435 CORE_ADDR start_addr, ULONGEST search_space_len,
9436 const gdb_byte *pattern, ULONGEST pattern_len,
9437 CORE_ADDR *found_addrp)
9438{
f5656ead 9439 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9440 struct remote_state *rs = get_remote_state ();
9441 int max_size = get_memory_write_packet_size ();
9442 struct packet_config *packet =
9443 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9444 /* Number of packet bytes used to encode the pattern;
9445 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9446 int escaped_pattern_len;
0df8b418 9447 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9448 int used_pattern_len;
9449 int i;
9450 int found;
9451 ULONGEST found_addr;
9452
9453 /* Don't go to the target if we don't have to.
9454 This is done before checking packet->support to avoid the possibility that
9455 a success for this edge case means the facility works in general. */
9456 if (pattern_len > search_space_len)
9457 return 0;
9458 if (pattern_len == 0)
9459 {
9460 *found_addrp = start_addr;
9461 return 1;
9462 }
9463
9464 /* If we already know the packet isn't supported, fall back to the simple
9465 way of searching memory. */
9466
4082afcc 9467 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9468 {
9469 /* Target doesn't provided special support, fall back and use the
9470 standard support (copy memory and do the search here). */
9471 return simple_search_memory (ops, start_addr, search_space_len,
9472 pattern, pattern_len, found_addrp);
9473 }
9474
28439a30
PA
9475 /* Make sure the remote is pointing at the right process. */
9476 set_general_process ();
9477
08388c79
DE
9478 /* Insert header. */
9479 i = snprintf (rs->buf, max_size,
9480 "qSearch:memory:%s;%s;",
5af949e3 9481 phex_nz (start_addr, addr_size),
08388c79
DE
9482 phex_nz (search_space_len, sizeof (search_space_len)));
9483 max_size -= (i + 1);
9484
9485 /* Escape as much data as fits into rs->buf. */
9486 escaped_pattern_len =
bc20a4af 9487 remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i,
08388c79
DE
9488 &used_pattern_len, max_size);
9489
9490 /* Bail if the pattern is too large. */
9491 if (used_pattern_len != pattern_len)
9b20d036 9492 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9493
9494 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9495 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9496 || packet_ok (rs->buf, packet) != PACKET_OK)
9497 {
9498 /* The request may not have worked because the command is not
9499 supported. If so, fall back to the simple way. */
9500 if (packet->support == PACKET_DISABLE)
9501 {
9502 return simple_search_memory (ops, start_addr, search_space_len,
9503 pattern, pattern_len, found_addrp);
9504 }
9505 return -1;
9506 }
9507
9508 if (rs->buf[0] == '0')
9509 found = 0;
9510 else if (rs->buf[0] == '1')
9511 {
9512 found = 1;
9513 if (rs->buf[1] != ',')
10e0fa18 9514 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9515 unpack_varlen_hex (rs->buf + 2, &found_addr);
9516 *found_addrp = found_addr;
9517 }
9518 else
10e0fa18 9519 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9520
9521 return found;
9522}
9523
96baa820 9524static void
a30bf1f1 9525remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9526 struct ui_file *outbuf)
96baa820 9527{
d01949b6 9528 struct remote_state *rs = get_remote_state ();
2e9f7625 9529 char *p = rs->buf;
96baa820 9530
5d93a237 9531 if (!rs->remote_desc)
8a3fe4f8 9532 error (_("remote rcmd is only available after target open"));
96baa820 9533
23860348 9534 /* Send a NULL command across as an empty command. */
7be570e7
JM
9535 if (command == NULL)
9536 command = "";
9537
23860348 9538 /* The query prefix. */
2e9f7625
DJ
9539 strcpy (rs->buf, "qRcmd,");
9540 p = strchr (rs->buf, '\0');
96baa820 9541
3e43a32a
MS
9542 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9543 > get_remote_packet_size ())
8a3fe4f8 9544 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9545
23860348 9546 /* Encode the actual command. */
a30bf1f1 9547 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9548
6d820c5c 9549 if (putpkt (rs->buf) < 0)
8a3fe4f8 9550 error (_("Communication problem with target."));
96baa820
JM
9551
9552 /* get/display the response */
9553 while (1)
9554 {
2e9f7625
DJ
9555 char *buf;
9556
00bf0b85 9557 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9558 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9559 rs->buf[0] = '\0';
5b37825d
PW
9560 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9561 {
9562 /* Timeout. Continue to (try to) read responses.
9563 This is better than stopping with an error, assuming the stub
9564 is still executing the (long) monitor command.
9565 If needed, the user can interrupt gdb using C-c, obtaining
9566 an effect similar to stop on timeout. */
9567 continue;
9568 }
2e9f7625 9569 buf = rs->buf;
96baa820 9570 if (buf[0] == '\0')
8a3fe4f8 9571 error (_("Target does not support this command."));
96baa820
JM
9572 if (buf[0] == 'O' && buf[1] != 'K')
9573 {
23860348 9574 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9575 continue;
9576 }
9577 if (strcmp (buf, "OK") == 0)
9578 break;
7be570e7
JM
9579 if (strlen (buf) == 3 && buf[0] == 'E'
9580 && isdigit (buf[1]) && isdigit (buf[2]))
9581 {
8a3fe4f8 9582 error (_("Protocol error with Rcmd"));
7be570e7 9583 }
96baa820
JM
9584 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9585 {
9586 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9587
96baa820
JM
9588 fputc_unfiltered (c, outbuf);
9589 }
9590 break;
9591 }
9592}
9593
fd79ecee
DJ
9594static VEC(mem_region_s) *
9595remote_memory_map (struct target_ops *ops)
9596{
9597 VEC(mem_region_s) *result = NULL;
9598 char *text = target_read_stralloc (&current_target,
9599 TARGET_OBJECT_MEMORY_MAP, NULL);
9600
9601 if (text)
9602 {
9603 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9604
fd79ecee
DJ
9605 result = parse_memory_map (text);
9606 do_cleanups (back_to);
9607 }
9608
9609 return result;
9610}
9611
c906108c 9612static void
fba45db2 9613packet_command (char *args, int from_tty)
c906108c 9614{
d01949b6 9615 struct remote_state *rs = get_remote_state ();
c906108c 9616
5d93a237 9617 if (!rs->remote_desc)
8a3fe4f8 9618 error (_("command can only be used with remote target"));
c906108c 9619
c5aa993b 9620 if (!args)
8a3fe4f8 9621 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9622
9623 puts_filtered ("sending: ");
9624 print_packet (args);
9625 puts_filtered ("\n");
9626 putpkt (args);
9627
6d820c5c 9628 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9629 puts_filtered ("received: ");
6d820c5c 9630 print_packet (rs->buf);
c906108c
SS
9631 puts_filtered ("\n");
9632}
9633
9634#if 0
23860348 9635/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9636
a14ed312 9637static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9638
a14ed312 9639static void threadset_test_cmd (char *cmd, int tty);
c906108c 9640
a14ed312 9641static void threadalive_test (char *cmd, int tty);
c906108c 9642
a14ed312 9643static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9644
23860348 9645int get_and_display_threadinfo (threadref *ref);
c906108c 9646
a14ed312 9647static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9648
23860348 9649static int thread_display_step (threadref *ref, void *context);
c906108c 9650
a14ed312 9651static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9652
a14ed312 9653static void init_remote_threadtests (void);
c906108c 9654
23860348 9655#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9656
9657static void
fba45db2 9658threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9659{
9660 int sample_thread = SAMPLE_THREAD;
9661
a3f17187 9662 printf_filtered (_("Remote threadset test\n"));
79d7f229 9663 set_general_thread (sample_thread);
c906108c
SS
9664}
9665
9666
9667static void
fba45db2 9668threadalive_test (char *cmd, int tty)
c906108c
SS
9669{
9670 int sample_thread = SAMPLE_THREAD;
79d7f229 9671 int pid = ptid_get_pid (inferior_ptid);
ba348170 9672 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9673
79d7f229 9674 if (remote_thread_alive (ptid))
c906108c
SS
9675 printf_filtered ("PASS: Thread alive test\n");
9676 else
9677 printf_filtered ("FAIL: Thread alive test\n");
9678}
9679
23860348 9680void output_threadid (char *title, threadref *ref);
c906108c
SS
9681
9682void
fba45db2 9683output_threadid (char *title, threadref *ref)
c906108c
SS
9684{
9685 char hexid[20];
9686
23860348 9687 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9688 hexid[16] = 0;
9689 printf_filtered ("%s %s\n", title, (&hexid[0]));
9690}
9691
9692static void
fba45db2 9693threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9694{
9695 int startflag = 1;
9696 threadref nextthread;
9697 int done, result_count;
9698 threadref threadlist[3];
9699
9700 printf_filtered ("Remote Threadlist test\n");
9701 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9702 &result_count, &threadlist[0]))
9703 printf_filtered ("FAIL: threadlist test\n");
9704 else
9705 {
9706 threadref *scan = threadlist;
9707 threadref *limit = scan + result_count;
9708
9709 while (scan < limit)
9710 output_threadid (" thread ", scan++);
9711 }
9712}
9713
9714void
fba45db2 9715display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9716{
9717 output_threadid ("Threadid: ", &info->threadid);
9718 printf_filtered ("Name: %s\n ", info->shortname);
9719 printf_filtered ("State: %s\n", info->display);
9720 printf_filtered ("other: %s\n\n", info->more_display);
9721}
9722
9723int
fba45db2 9724get_and_display_threadinfo (threadref *ref)
c906108c
SS
9725{
9726 int result;
9727 int set;
9728 struct gdb_ext_thread_info threadinfo;
9729
9730 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9731 | TAG_MOREDISPLAY | TAG_DISPLAY;
9732 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9733 display_thread_info (&threadinfo);
9734 return result;
9735}
9736
9737static void
fba45db2 9738threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9739{
9740 int athread = SAMPLE_THREAD;
9741 threadref thread;
9742 int set;
9743
9744 int_to_threadref (&thread, athread);
9745 printf_filtered ("Remote Threadinfo test\n");
9746 if (!get_and_display_threadinfo (&thread))
9747 printf_filtered ("FAIL cannot get thread info\n");
9748}
9749
9750static int
fba45db2 9751thread_display_step (threadref *ref, void *context)
c906108c
SS
9752{
9753 /* output_threadid(" threadstep ",ref); *//* simple test */
9754 return get_and_display_threadinfo (ref);
9755}
9756
9757static void
fba45db2 9758threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9759{
9760 printf_filtered ("Remote Threadlist update test\n");
9761 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9762}
9763
9764static void
9765init_remote_threadtests (void)
9766{
3e43a32a
MS
9767 add_com ("tlist", class_obscure, threadlist_test_cmd,
9768 _("Fetch and print the remote list of "
9769 "thread identifiers, one pkt only"));
c906108c 9770 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9771 _("Fetch and display info about one thread"));
c906108c 9772 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9773 _("Test setting to a different thread"));
c906108c 9774 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9775 _("Iterate through updating all remote thread info"));
c906108c 9776 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9777 _(" Remote thread alive test "));
c906108c
SS
9778}
9779
9780#endif /* 0 */
9781
f3fb8c85
MS
9782/* Convert a thread ID to a string. Returns the string in a static
9783 buffer. */
9784
9785static char *
117de6a9 9786remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9787{
79d7f229 9788 static char buf[64];
82f73884 9789 struct remote_state *rs = get_remote_state ();
f3fb8c85 9790
7cee1e54
PA
9791 if (ptid_equal (ptid, null_ptid))
9792 return normal_pid_to_str (ptid);
9793 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9794 {
9795 /* Printing an inferior target id. */
9796
9797 /* When multi-process extensions are off, there's no way in the
9798 remote protocol to know the remote process id, if there's any
9799 at all. There's one exception --- when we're connected with
9800 target extended-remote, and we manually attached to a process
9801 with "attach PID". We don't record anywhere a flag that
9802 allows us to distinguish that case from the case of
9803 connecting with extended-remote and the stub already being
9804 attached to a process, and reporting yes to qAttached, hence
9805 no smart special casing here. */
9806 if (!remote_multi_process_p (rs))
9807 {
9808 xsnprintf (buf, sizeof buf, "Remote target");
9809 return buf;
9810 }
9811
9812 return normal_pid_to_str (ptid);
82f73884 9813 }
ecd0ada5 9814 else
79d7f229 9815 {
ecd0ada5
PA
9816 if (ptid_equal (magic_null_ptid, ptid))
9817 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9818 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
9819 if (ptid_get_lwp (ptid) == 0)
9820 return normal_pid_to_str (ptid);
9821 else
9822 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9823 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9824 else
9825 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9826 ptid_get_lwp (ptid));
79d7f229
PA
9827 return buf;
9828 }
f3fb8c85
MS
9829}
9830
38691318
KB
9831/* Get the address of the thread local variable in OBJFILE which is
9832 stored at OFFSET within the thread local storage for thread PTID. */
9833
9834static CORE_ADDR
117de6a9
PA
9835remote_get_thread_local_address (struct target_ops *ops,
9836 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9837{
4082afcc 9838 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9839 {
9840 struct remote_state *rs = get_remote_state ();
6d820c5c 9841 char *p = rs->buf;
82f73884 9842 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9843 enum packet_result result;
38691318
KB
9844
9845 strcpy (p, "qGetTLSAddr:");
9846 p += strlen (p);
82f73884 9847 p = write_ptid (p, endp, ptid);
38691318
KB
9848 *p++ = ',';
9849 p += hexnumstr (p, offset);
9850 *p++ = ',';
9851 p += hexnumstr (p, lm);
9852 *p++ = '\0';
9853
6d820c5c
DJ
9854 putpkt (rs->buf);
9855 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9856 result = packet_ok (rs->buf,
9857 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9858 if (result == PACKET_OK)
38691318
KB
9859 {
9860 ULONGEST result;
9861
6d820c5c 9862 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9863 return result;
9864 }
571dd617 9865 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9866 throw_error (TLS_GENERIC_ERROR,
9867 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9868 else
109c3e39
AC
9869 throw_error (TLS_GENERIC_ERROR,
9870 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9871 }
9872 else
109c3e39
AC
9873 throw_error (TLS_GENERIC_ERROR,
9874 _("TLS not supported or disabled on this target"));
38691318
KB
9875 /* Not reached. */
9876 return 0;
9877}
9878
711e434b
PM
9879/* Provide thread local base, i.e. Thread Information Block address.
9880 Returns 1 if ptid is found and thread_local_base is non zero. */
9881
70221824 9882static int
bd7ae0f5 9883remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9884{
4082afcc 9885 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9886 {
9887 struct remote_state *rs = get_remote_state ();
9888 char *p = rs->buf;
9889 char *endp = rs->buf + get_remote_packet_size ();
9890 enum packet_result result;
9891
9892 strcpy (p, "qGetTIBAddr:");
9893 p += strlen (p);
9894 p = write_ptid (p, endp, ptid);
9895 *p++ = '\0';
9896
9897 putpkt (rs->buf);
9898 getpkt (&rs->buf, &rs->buf_size, 0);
9899 result = packet_ok (rs->buf,
9900 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9901 if (result == PACKET_OK)
9902 {
9903 ULONGEST result;
9904
9905 unpack_varlen_hex (rs->buf, &result);
9906 if (addr)
9907 *addr = (CORE_ADDR) result;
9908 return 1;
9909 }
9910 else if (result == PACKET_UNKNOWN)
9911 error (_("Remote target doesn't support qGetTIBAddr packet"));
9912 else
9913 error (_("Remote target failed to process qGetTIBAddr request"));
9914 }
9915 else
9916 error (_("qGetTIBAddr not supported or disabled on this target"));
9917 /* Not reached. */
9918 return 0;
9919}
9920
29709017
DJ
9921/* Support for inferring a target description based on the current
9922 architecture and the size of a 'g' packet. While the 'g' packet
9923 can have any size (since optional registers can be left off the
9924 end), some sizes are easily recognizable given knowledge of the
9925 approximate architecture. */
9926
9927struct remote_g_packet_guess
9928{
9929 int bytes;
9930 const struct target_desc *tdesc;
9931};
9932typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9933DEF_VEC_O(remote_g_packet_guess_s);
9934
9935struct remote_g_packet_data
9936{
9937 VEC(remote_g_packet_guess_s) *guesses;
9938};
9939
9940static struct gdbarch_data *remote_g_packet_data_handle;
9941
9942static void *
9943remote_g_packet_data_init (struct obstack *obstack)
9944{
9945 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9946}
9947
9948void
9949register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9950 const struct target_desc *tdesc)
9951{
9952 struct remote_g_packet_data *data
9953 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9954 struct remote_g_packet_guess new_guess, *guess;
9955 int ix;
9956
9957 gdb_assert (tdesc != NULL);
9958
9959 for (ix = 0;
9960 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9961 ix++)
9962 if (guess->bytes == bytes)
9963 internal_error (__FILE__, __LINE__,
9b20d036 9964 _("Duplicate g packet description added for size %d"),
29709017
DJ
9965 bytes);
9966
9967 new_guess.bytes = bytes;
9968 new_guess.tdesc = tdesc;
9969 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9970}
9971
d962ef82
DJ
9972/* Return 1 if remote_read_description would do anything on this target
9973 and architecture, 0 otherwise. */
9974
9975static int
9976remote_read_description_p (struct target_ops *target)
9977{
9978 struct remote_g_packet_data *data
f5656ead 9979 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
9980
9981 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9982 return 1;
9983
9984 return 0;
9985}
9986
29709017
DJ
9987static const struct target_desc *
9988remote_read_description (struct target_ops *target)
9989{
9990 struct remote_g_packet_data *data
f5656ead 9991 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 9992
d962ef82
DJ
9993 /* Do not try this during initial connection, when we do not know
9994 whether there is a running but stopped thread. */
9995 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 9996 return target->beneath->to_read_description (target->beneath);
d962ef82 9997
29709017
DJ
9998 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9999 {
10000 struct remote_g_packet_guess *guess;
10001 int ix;
10002 int bytes = send_g_packet ();
10003
10004 for (ix = 0;
10005 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10006 ix++)
10007 if (guess->bytes == bytes)
10008 return guess->tdesc;
10009
10010 /* We discard the g packet. A minor optimization would be to
10011 hold on to it, and fill the register cache once we have selected
10012 an architecture, but it's too tricky to do safely. */
10013 }
10014
2117c711 10015 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10016}
10017
a6b151f1
DJ
10018/* Remote file transfer support. This is host-initiated I/O, not
10019 target-initiated; for target-initiated, see remote-fileio.c. */
10020
10021/* If *LEFT is at least the length of STRING, copy STRING to
10022 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10023 decrease *LEFT. Otherwise raise an error. */
10024
10025static void
10026remote_buffer_add_string (char **buffer, int *left, char *string)
10027{
10028 int len = strlen (string);
10029
10030 if (len > *left)
10031 error (_("Packet too long for target."));
10032
10033 memcpy (*buffer, string, len);
10034 *buffer += len;
10035 *left -= len;
10036
10037 /* NUL-terminate the buffer as a convenience, if there is
10038 room. */
10039 if (*left)
10040 **buffer = '\0';
10041}
10042
10043/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10044 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10045 decrease *LEFT. Otherwise raise an error. */
10046
10047static void
10048remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10049 int len)
10050{
10051 if (2 * len > *left)
10052 error (_("Packet too long for target."));
10053
10054 bin2hex (bytes, *buffer, len);
10055 *buffer += 2 * len;
10056 *left -= 2 * len;
10057
10058 /* NUL-terminate the buffer as a convenience, if there is
10059 room. */
10060 if (*left)
10061 **buffer = '\0';
10062}
10063
10064/* If *LEFT is large enough, convert VALUE to hex and add it to
10065 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10066 decrease *LEFT. Otherwise raise an error. */
10067
10068static void
10069remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10070{
10071 int len = hexnumlen (value);
10072
10073 if (len > *left)
10074 error (_("Packet too long for target."));
10075
10076 hexnumstr (*buffer, value);
10077 *buffer += len;
10078 *left -= len;
10079
10080 /* NUL-terminate the buffer as a convenience, if there is
10081 room. */
10082 if (*left)
10083 **buffer = '\0';
10084}
10085
10086/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10087 value, *REMOTE_ERRNO to the remote error number or zero if none
10088 was included, and *ATTACHMENT to point to the start of the annex
10089 if any. The length of the packet isn't needed here; there may
10090 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10091
10092 Return 0 if the packet could be parsed, -1 if it could not. If
10093 -1 is returned, the other variables may not be initialized. */
10094
10095static int
10096remote_hostio_parse_result (char *buffer, int *retcode,
10097 int *remote_errno, char **attachment)
10098{
10099 char *p, *p2;
10100
10101 *remote_errno = 0;
10102 *attachment = NULL;
10103
10104 if (buffer[0] != 'F')
10105 return -1;
10106
10107 errno = 0;
10108 *retcode = strtol (&buffer[1], &p, 16);
10109 if (errno != 0 || p == &buffer[1])
10110 return -1;
10111
10112 /* Check for ",errno". */
10113 if (*p == ',')
10114 {
10115 errno = 0;
10116 *remote_errno = strtol (p + 1, &p2, 16);
10117 if (errno != 0 || p + 1 == p2)
10118 return -1;
10119 p = p2;
10120 }
10121
10122 /* Check for ";attachment". If there is no attachment, the
10123 packet should end here. */
10124 if (*p == ';')
10125 {
10126 *attachment = p + 1;
10127 return 0;
10128 }
10129 else if (*p == '\0')
10130 return 0;
10131 else
10132 return -1;
10133}
10134
10135/* Send a prepared I/O packet to the target and read its response.
10136 The prepared packet is in the global RS->BUF before this function
10137 is called, and the answer is there when we return.
10138
10139 COMMAND_BYTES is the length of the request to send, which may include
10140 binary data. WHICH_PACKET is the packet configuration to check
10141 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10142 is set to the error number and -1 is returned. Otherwise the value
10143 returned by the function is returned.
10144
10145 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10146 attachment is expected; an error will be reported if there's a
10147 mismatch. If one is found, *ATTACHMENT will be set to point into
10148 the packet buffer and *ATTACHMENT_LEN will be set to the
10149 attachment's length. */
10150
10151static int
10152remote_hostio_send_command (int command_bytes, int which_packet,
10153 int *remote_errno, char **attachment,
10154 int *attachment_len)
10155{
10156 struct remote_state *rs = get_remote_state ();
10157 int ret, bytes_read;
10158 char *attachment_tmp;
10159
5d93a237 10160 if (!rs->remote_desc
4082afcc 10161 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10162 {
10163 *remote_errno = FILEIO_ENOSYS;
10164 return -1;
10165 }
10166
10167 putpkt_binary (rs->buf, command_bytes);
10168 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10169
10170 /* If it timed out, something is wrong. Don't try to parse the
10171 buffer. */
10172 if (bytes_read < 0)
10173 {
10174 *remote_errno = FILEIO_EINVAL;
10175 return -1;
10176 }
10177
10178 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10179 {
10180 case PACKET_ERROR:
10181 *remote_errno = FILEIO_EINVAL;
10182 return -1;
10183 case PACKET_UNKNOWN:
10184 *remote_errno = FILEIO_ENOSYS;
10185 return -1;
10186 case PACKET_OK:
10187 break;
10188 }
10189
10190 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10191 &attachment_tmp))
10192 {
10193 *remote_errno = FILEIO_EINVAL;
10194 return -1;
10195 }
10196
10197 /* Make sure we saw an attachment if and only if we expected one. */
10198 if ((attachment_tmp == NULL && attachment != NULL)
10199 || (attachment_tmp != NULL && attachment == NULL))
10200 {
10201 *remote_errno = FILEIO_EINVAL;
10202 return -1;
10203 }
10204
10205 /* If an attachment was found, it must point into the packet buffer;
10206 work out how many bytes there were. */
10207 if (attachment_tmp != NULL)
10208 {
10209 *attachment = attachment_tmp;
10210 *attachment_len = bytes_read - (*attachment - rs->buf);
10211 }
10212
10213 return ret;
10214}
10215
12e2a5fd 10216/* Implementation of to_fileio_open. */
a6b151f1
DJ
10217
10218static int
cd897586
TT
10219remote_hostio_open (struct target_ops *self,
10220 const char *filename, int flags, int mode,
a6b151f1
DJ
10221 int *remote_errno)
10222{
10223 struct remote_state *rs = get_remote_state ();
10224 char *p = rs->buf;
10225 int left = get_remote_packet_size () - 1;
10226
10227 remote_buffer_add_string (&p, &left, "vFile:open:");
10228
10229 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10230 strlen (filename));
10231 remote_buffer_add_string (&p, &left, ",");
10232
10233 remote_buffer_add_int (&p, &left, flags);
10234 remote_buffer_add_string (&p, &left, ",");
10235
10236 remote_buffer_add_int (&p, &left, mode);
10237
10238 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10239 remote_errno, NULL, NULL);
10240}
10241
12e2a5fd 10242/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10243
10244static int
0d866f62
TT
10245remote_hostio_pwrite (struct target_ops *self,
10246 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10247 ULONGEST offset, int *remote_errno)
10248{
10249 struct remote_state *rs = get_remote_state ();
10250 char *p = rs->buf;
10251 int left = get_remote_packet_size ();
10252 int out_len;
10253
10254 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10255
10256 remote_buffer_add_int (&p, &left, fd);
10257 remote_buffer_add_string (&p, &left, ",");
10258
10259 remote_buffer_add_int (&p, &left, offset);
10260 remote_buffer_add_string (&p, &left, ",");
10261
bc20a4af 10262 p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10263 get_remote_packet_size () - (p - rs->buf));
10264
10265 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10266 remote_errno, NULL, NULL);
10267}
10268
12e2a5fd 10269/* Implementation of to_fileio_pread. */
a6b151f1
DJ
10270
10271static int
a3be983c
TT
10272remote_hostio_pread (struct target_ops *self,
10273 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
10274 ULONGEST offset, int *remote_errno)
10275{
10276 struct remote_state *rs = get_remote_state ();
10277 char *p = rs->buf;
10278 char *attachment;
10279 int left = get_remote_packet_size ();
10280 int ret, attachment_len;
10281 int read_len;
10282
10283 remote_buffer_add_string (&p, &left, "vFile:pread:");
10284
10285 remote_buffer_add_int (&p, &left, fd);
10286 remote_buffer_add_string (&p, &left, ",");
10287
10288 remote_buffer_add_int (&p, &left, len);
10289 remote_buffer_add_string (&p, &left, ",");
10290
10291 remote_buffer_add_int (&p, &left, offset);
10292
10293 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10294 remote_errno, &attachment,
10295 &attachment_len);
10296
10297 if (ret < 0)
10298 return ret;
10299
bc20a4af 10300 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10301 read_buf, len);
10302 if (read_len != ret)
10303 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10304
10305 return ret;
10306}
10307
12e2a5fd 10308/* Implementation of to_fileio_close. */
a6b151f1
DJ
10309
10310static int
df39ea25 10311remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10312{
10313 struct remote_state *rs = get_remote_state ();
10314 char *p = rs->buf;
10315 int left = get_remote_packet_size () - 1;
10316
10317 remote_buffer_add_string (&p, &left, "vFile:close:");
10318
10319 remote_buffer_add_int (&p, &left, fd);
10320
10321 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10322 remote_errno, NULL, NULL);
10323}
10324
12e2a5fd 10325/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10326
10327static int
dbbca37d
TT
10328remote_hostio_unlink (struct target_ops *self,
10329 const char *filename, int *remote_errno)
a6b151f1
DJ
10330{
10331 struct remote_state *rs = get_remote_state ();
10332 char *p = rs->buf;
10333 int left = get_remote_packet_size () - 1;
10334
10335 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10336
10337 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10338 strlen (filename));
10339
10340 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10341 remote_errno, NULL, NULL);
10342}
10343
12e2a5fd 10344/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10345
10346static char *
fab5aa7c
TT
10347remote_hostio_readlink (struct target_ops *self,
10348 const char *filename, int *remote_errno)
b9e7b9c3
UW
10349{
10350 struct remote_state *rs = get_remote_state ();
10351 char *p = rs->buf;
10352 char *attachment;
10353 int left = get_remote_packet_size ();
10354 int len, attachment_len;
10355 int read_len;
10356 char *ret;
10357
10358 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10359
10360 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10361 strlen (filename));
10362
10363 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10364 remote_errno, &attachment,
10365 &attachment_len);
10366
10367 if (len < 0)
10368 return NULL;
10369
10370 ret = xmalloc (len + 1);
10371
bc20a4af
PA
10372 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10373 (gdb_byte *) ret, len);
b9e7b9c3
UW
10374 if (read_len != len)
10375 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10376
10377 ret[len] = '\0';
10378 return ret;
10379}
10380
12e2a5fd 10381/* Implementation of to_fileio_fstat. */
0a93529c
GB
10382
10383static int
10384remote_hostio_fstat (struct target_ops *self,
10385 int fd, struct stat *st,
10386 int *remote_errno)
10387{
10388 struct remote_state *rs = get_remote_state ();
10389 char *p = rs->buf;
10390 int left = get_remote_packet_size ();
10391 int attachment_len, ret;
10392 char *attachment;
10393 struct fio_stat fst;
10394 int read_len;
10395
464b0089
GB
10396 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10397
10398 remote_buffer_add_int (&p, &left, fd);
10399
10400 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10401 remote_errno, &attachment,
10402 &attachment_len);
10403 if (ret < 0)
0a93529c 10404 {
464b0089
GB
10405 if (*remote_errno != FILEIO_ENOSYS)
10406 return ret;
10407
0a93529c
GB
10408 /* Strictly we should return -1, ENOSYS here, but when
10409 "set sysroot remote:" was implemented in August 2008
10410 BFD's need for a stat function was sidestepped with
10411 this hack. This was not remedied until March 2015
10412 so we retain the previous behavior to avoid breaking
10413 compatibility.
10414
10415 Note that the memset is a March 2015 addition; older
10416 GDBs set st_size *and nothing else* so the structure
10417 would have garbage in all other fields. This might
10418 break something but retaining the previous behavior
10419 here would be just too wrong. */
10420
10421 memset (st, 0, sizeof (struct stat));
10422 st->st_size = INT_MAX;
10423 return 0;
10424 }
10425
0a93529c
GB
10426 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10427 (gdb_byte *) &fst, sizeof (fst));
10428
10429 if (read_len != ret)
10430 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10431
10432 if (read_len != sizeof (fst))
10433 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10434 read_len, (int) sizeof (fst));
10435
10436 remote_fileio_to_host_stat (&fst, st);
10437
10438 return 0;
10439}
10440
12e2a5fd 10441/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10442
10443static int
10444remote_filesystem_is_local (struct target_ops *self)
10445{
10446 /* Valgrind GDB presents itself as a remote target but works
10447 on the local filesystem: it does not implement remote get
10448 and users are not expected to set a sysroot. To handle
10449 this case we treat the remote filesystem as local if the
10450 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10451 does not support vFile:open. */
10452 if (gdb_sysroot != NULL
10453 && strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
10454 {
10455 enum packet_support ps = packet_support (PACKET_vFile_open);
10456
10457 if (ps == PACKET_SUPPORT_UNKNOWN)
10458 {
10459 int fd, remote_errno;
10460
10461 /* Try opening a file to probe support. The supplied
10462 filename is irrelevant, we only care about whether
10463 the stub recognizes the packet or not. */
10464 fd = remote_hostio_open (self, "just probing",
10465 FILEIO_O_RDONLY, 0700,
10466 &remote_errno);
10467
10468 if (fd >= 0)
10469 remote_hostio_close (self, fd, &remote_errno);
10470
10471 ps = packet_support (PACKET_vFile_open);
10472 }
10473
10474 if (ps == PACKET_DISABLE)
10475 {
10476 static int warning_issued = 0;
10477
10478 if (!warning_issued)
10479 {
10480 warning (_("remote target does not support file"
10481 " transfer, attempting to access files"
10482 " from local filesystem."));
10483 warning_issued = 1;
10484 }
10485
10486 return 1;
10487 }
10488 }
10489
10490 return 0;
10491}
10492
a6b151f1
DJ
10493static int
10494remote_fileio_errno_to_host (int errnum)
10495{
10496 switch (errnum)
10497 {
10498 case FILEIO_EPERM:
10499 return EPERM;
10500 case FILEIO_ENOENT:
10501 return ENOENT;
10502 case FILEIO_EINTR:
10503 return EINTR;
10504 case FILEIO_EIO:
10505 return EIO;
10506 case FILEIO_EBADF:
10507 return EBADF;
10508 case FILEIO_EACCES:
10509 return EACCES;
10510 case FILEIO_EFAULT:
10511 return EFAULT;
10512 case FILEIO_EBUSY:
10513 return EBUSY;
10514 case FILEIO_EEXIST:
10515 return EEXIST;
10516 case FILEIO_ENODEV:
10517 return ENODEV;
10518 case FILEIO_ENOTDIR:
10519 return ENOTDIR;
10520 case FILEIO_EISDIR:
10521 return EISDIR;
10522 case FILEIO_EINVAL:
10523 return EINVAL;
10524 case FILEIO_ENFILE:
10525 return ENFILE;
10526 case FILEIO_EMFILE:
10527 return EMFILE;
10528 case FILEIO_EFBIG:
10529 return EFBIG;
10530 case FILEIO_ENOSPC:
10531 return ENOSPC;
10532 case FILEIO_ESPIPE:
10533 return ESPIPE;
10534 case FILEIO_EROFS:
10535 return EROFS;
10536 case FILEIO_ENOSYS:
10537 return ENOSYS;
10538 case FILEIO_ENAMETOOLONG:
10539 return ENAMETOOLONG;
10540 }
10541 return -1;
10542}
10543
10544static char *
10545remote_hostio_error (int errnum)
10546{
10547 int host_error = remote_fileio_errno_to_host (errnum);
10548
10549 if (host_error == -1)
10550 error (_("Unknown remote I/O error %d"), errnum);
10551 else
10552 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10553}
10554
a6b151f1
DJ
10555static void
10556remote_hostio_close_cleanup (void *opaque)
10557{
10558 int fd = *(int *) opaque;
10559 int remote_errno;
10560
df39ea25 10561 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10562}
10563
10564void
10565remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10566{
10567 struct cleanup *back_to, *close_cleanup;
10568 int retcode, fd, remote_errno, bytes, io_size;
10569 FILE *file;
10570 gdb_byte *buffer;
10571 int bytes_in_buffer;
10572 int saw_eof;
10573 ULONGEST offset;
5d93a237 10574 struct remote_state *rs = get_remote_state ();
a6b151f1 10575
5d93a237 10576 if (!rs->remote_desc)
a6b151f1
DJ
10577 error (_("command can only be used with remote target"));
10578
614c279d 10579 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10580 if (file == NULL)
10581 perror_with_name (local_file);
7c8a8b04 10582 back_to = make_cleanup_fclose (file);
a6b151f1 10583
cd897586
TT
10584 fd = remote_hostio_open (find_target_at (process_stratum),
10585 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10586 | FILEIO_O_TRUNC),
10587 0700, &remote_errno);
10588 if (fd == -1)
10589 remote_hostio_error (remote_errno);
10590
10591 /* Send up to this many bytes at once. They won't all fit in the
10592 remote packet limit, so we'll transfer slightly fewer. */
10593 io_size = get_remote_packet_size ();
10594 buffer = xmalloc (io_size);
10595 make_cleanup (xfree, buffer);
10596
10597 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10598
10599 bytes_in_buffer = 0;
10600 saw_eof = 0;
10601 offset = 0;
10602 while (bytes_in_buffer || !saw_eof)
10603 {
10604 if (!saw_eof)
10605 {
3e43a32a
MS
10606 bytes = fread (buffer + bytes_in_buffer, 1,
10607 io_size - bytes_in_buffer,
a6b151f1
DJ
10608 file);
10609 if (bytes == 0)
10610 {
10611 if (ferror (file))
10612 error (_("Error reading %s."), local_file);
10613 else
10614 {
10615 /* EOF. Unless there is something still in the
10616 buffer from the last iteration, we are done. */
10617 saw_eof = 1;
10618 if (bytes_in_buffer == 0)
10619 break;
10620 }
10621 }
10622 }
10623 else
10624 bytes = 0;
10625
10626 bytes += bytes_in_buffer;
10627 bytes_in_buffer = 0;
10628
0d866f62
TT
10629 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10630 fd, buffer, bytes,
3e43a32a 10631 offset, &remote_errno);
a6b151f1
DJ
10632
10633 if (retcode < 0)
10634 remote_hostio_error (remote_errno);
10635 else if (retcode == 0)
10636 error (_("Remote write of %d bytes returned 0!"), bytes);
10637 else if (retcode < bytes)
10638 {
10639 /* Short write. Save the rest of the read data for the next
10640 write. */
10641 bytes_in_buffer = bytes - retcode;
10642 memmove (buffer, buffer + retcode, bytes_in_buffer);
10643 }
10644
10645 offset += retcode;
10646 }
10647
10648 discard_cleanups (close_cleanup);
df39ea25 10649 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10650 remote_hostio_error (remote_errno);
10651
10652 if (from_tty)
10653 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10654 do_cleanups (back_to);
10655}
10656
10657void
10658remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10659{
10660 struct cleanup *back_to, *close_cleanup;
cea39f65 10661 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10662 FILE *file;
10663 gdb_byte *buffer;
10664 ULONGEST offset;
5d93a237 10665 struct remote_state *rs = get_remote_state ();
a6b151f1 10666
5d93a237 10667 if (!rs->remote_desc)
a6b151f1
DJ
10668 error (_("command can only be used with remote target"));
10669
cd897586
TT
10670 fd = remote_hostio_open (find_target_at (process_stratum),
10671 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10672 if (fd == -1)
10673 remote_hostio_error (remote_errno);
10674
614c279d 10675 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10676 if (file == NULL)
10677 perror_with_name (local_file);
7c8a8b04 10678 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10679
10680 /* Send up to this many bytes at once. They won't all fit in the
10681 remote packet limit, so we'll transfer slightly fewer. */
10682 io_size = get_remote_packet_size ();
10683 buffer = xmalloc (io_size);
10684 make_cleanup (xfree, buffer);
10685
10686 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10687
10688 offset = 0;
10689 while (1)
10690 {
a3be983c
TT
10691 bytes = remote_hostio_pread (find_target_at (process_stratum),
10692 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10693 if (bytes == 0)
10694 /* Success, but no bytes, means end-of-file. */
10695 break;
10696 if (bytes == -1)
10697 remote_hostio_error (remote_errno);
10698
10699 offset += bytes;
10700
10701 bytes = fwrite (buffer, 1, bytes, file);
10702 if (bytes == 0)
10703 perror_with_name (local_file);
10704 }
10705
10706 discard_cleanups (close_cleanup);
df39ea25 10707 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10708 remote_hostio_error (remote_errno);
10709
10710 if (from_tty)
10711 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10712 do_cleanups (back_to);
10713}
10714
10715void
10716remote_file_delete (const char *remote_file, int from_tty)
10717{
10718 int retcode, remote_errno;
5d93a237 10719 struct remote_state *rs = get_remote_state ();
a6b151f1 10720
5d93a237 10721 if (!rs->remote_desc)
a6b151f1
DJ
10722 error (_("command can only be used with remote target"));
10723
dbbca37d
TT
10724 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10725 remote_file, &remote_errno);
a6b151f1
DJ
10726 if (retcode == -1)
10727 remote_hostio_error (remote_errno);
10728
10729 if (from_tty)
10730 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10731}
10732
10733static void
10734remote_put_command (char *args, int from_tty)
10735{
10736 struct cleanup *back_to;
10737 char **argv;
10738
d1a41061
PP
10739 if (args == NULL)
10740 error_no_arg (_("file to put"));
10741
10742 argv = gdb_buildargv (args);
a6b151f1
DJ
10743 back_to = make_cleanup_freeargv (argv);
10744 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10745 error (_("Invalid parameters to remote put"));
10746
10747 remote_file_put (argv[0], argv[1], from_tty);
10748
10749 do_cleanups (back_to);
10750}
10751
10752static void
10753remote_get_command (char *args, int from_tty)
10754{
10755 struct cleanup *back_to;
10756 char **argv;
10757
d1a41061
PP
10758 if (args == NULL)
10759 error_no_arg (_("file to get"));
10760
10761 argv = gdb_buildargv (args);
a6b151f1
DJ
10762 back_to = make_cleanup_freeargv (argv);
10763 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10764 error (_("Invalid parameters to remote get"));
10765
10766 remote_file_get (argv[0], argv[1], from_tty);
10767
10768 do_cleanups (back_to);
10769}
10770
10771static void
10772remote_delete_command (char *args, int from_tty)
10773{
10774 struct cleanup *back_to;
10775 char **argv;
10776
d1a41061
PP
10777 if (args == NULL)
10778 error_no_arg (_("file to delete"));
10779
10780 argv = gdb_buildargv (args);
a6b151f1
DJ
10781 back_to = make_cleanup_freeargv (argv);
10782 if (argv[0] == NULL || argv[1] != NULL)
10783 error (_("Invalid parameters to remote delete"));
10784
10785 remote_file_delete (argv[0], from_tty);
10786
10787 do_cleanups (back_to);
10788}
10789
10790static void
10791remote_command (char *args, int from_tty)
10792{
635c7e8a 10793 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10794}
10795
b2175913 10796static int
19db3e69 10797remote_can_execute_reverse (struct target_ops *self)
b2175913 10798{
4082afcc
PA
10799 if (packet_support (PACKET_bs) == PACKET_ENABLE
10800 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10801 return 1;
10802 else
10803 return 0;
b2175913
MS
10804}
10805
74531fed 10806static int
2a9a2795 10807remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10808{
10809 return 1;
10810}
10811
03583c20 10812static int
2bfc0540 10813remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10814{
10815 /* Only supported in extended mode. */
10816 return 0;
10817}
10818
8a305172 10819static int
86ce2668 10820remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10821{
10822 struct remote_state *rs = get_remote_state ();
a744cf53 10823
901f9912
UW
10824 /* Only extended-remote handles being attached to multiple
10825 processes, even though plain remote can use the multi-process
10826 thread id extensions, so that GDB knows the target process's
10827 PID. */
10828 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10829}
10830
70221824 10831static int
782b2b07
SS
10832remote_supports_cond_tracepoints (void)
10833{
4082afcc 10834 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10835}
10836
3788aec7 10837static int
efcc2da7 10838remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10839{
4082afcc 10840 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10841}
10842
70221824 10843static int
7a697b8d
SS
10844remote_supports_fast_tracepoints (void)
10845{
4082afcc 10846 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10847}
10848
0fb4aa4b
PA
10849static int
10850remote_supports_static_tracepoints (void)
10851{
4082afcc 10852 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
10853}
10854
1e4d1764
YQ
10855static int
10856remote_supports_install_in_trace (void)
10857{
4082afcc 10858 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
10859}
10860
d248b706 10861static int
7d178d6a 10862remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 10863{
4082afcc
PA
10864 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10865 == PACKET_ENABLE);
d248b706
KY
10866}
10867
3065dfb6 10868static int
6de37a3a 10869remote_supports_string_tracing (struct target_ops *self)
3065dfb6 10870{
4082afcc 10871 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
10872}
10873
d3ce09f5 10874static int
78eff0ec 10875remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 10876{
4082afcc 10877 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
10878}
10879
35b1e5cc 10880static void
ecae04e1 10881remote_trace_init (struct target_ops *self)
35b1e5cc
SS
10882{
10883 putpkt ("QTinit");
10884 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 10885 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10886 error (_("Target does not support this command."));
10887}
10888
10889static void free_actions_list (char **actions_list);
10890static void free_actions_list_cleanup_wrapper (void *);
10891static void
10892free_actions_list_cleanup_wrapper (void *al)
10893{
10894 free_actions_list (al);
10895}
10896
10897static void
10898free_actions_list (char **actions_list)
10899{
10900 int ndx;
10901
10902 if (actions_list == 0)
10903 return;
10904
10905 for (ndx = 0; actions_list[ndx]; ndx++)
10906 xfree (actions_list[ndx]);
10907
10908 xfree (actions_list);
10909}
10910
409873ef
SS
10911/* Recursive routine to walk through command list including loops, and
10912 download packets for each command. */
10913
10914static void
10915remote_download_command_source (int num, ULONGEST addr,
10916 struct command_line *cmds)
10917{
10918 struct remote_state *rs = get_remote_state ();
10919 struct command_line *cmd;
10920
10921 for (cmd = cmds; cmd; cmd = cmd->next)
10922 {
0df8b418 10923 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10924 strcpy (rs->buf, "QTDPsrc:");
10925 encode_source_string (num, addr, "cmd", cmd->line,
10926 rs->buf + strlen (rs->buf),
10927 rs->buf_size - strlen (rs->buf));
10928 putpkt (rs->buf);
10929 remote_get_noisy_reply (&target_buf, &target_buf_size);
10930 if (strcmp (target_buf, "OK"))
10931 warning (_("Target does not support source download."));
10932
10933 if (cmd->control_type == while_control
10934 || cmd->control_type == while_stepping_control)
10935 {
10936 remote_download_command_source (num, addr, *cmd->body_list);
10937
0df8b418 10938 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10939 strcpy (rs->buf, "QTDPsrc:");
10940 encode_source_string (num, addr, "cmd", "end",
10941 rs->buf + strlen (rs->buf),
10942 rs->buf_size - strlen (rs->buf));
10943 putpkt (rs->buf);
10944 remote_get_noisy_reply (&target_buf, &target_buf_size);
10945 if (strcmp (target_buf, "OK"))
10946 warning (_("Target does not support source download."));
10947 }
10948 }
10949}
10950
35b1e5cc 10951static void
548f7808 10952remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 10953{
bba74b36 10954#define BUF_SIZE 2048
e8ba3115 10955
35b1e5cc 10956 CORE_ADDR tpaddr;
409873ef 10957 char addrbuf[40];
bba74b36 10958 char buf[BUF_SIZE];
35b1e5cc
SS
10959 char **tdp_actions;
10960 char **stepping_actions;
10961 int ndx;
10962 struct cleanup *old_chain = NULL;
10963 struct agent_expr *aexpr;
10964 struct cleanup *aexpr_chain = NULL;
10965 char *pkt;
e8ba3115 10966 struct breakpoint *b = loc->owner;
d9b3f62e 10967 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 10968
dc673c81 10969 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
10970 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10971 tdp_actions);
10972 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10973 stepping_actions);
10974
10975 tpaddr = loc->address;
10976 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
10977 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10978 addrbuf, /* address */
10979 (b->enable_state == bp_enabled ? 'E' : 'D'),
10980 t->step_count, t->pass_count);
e8ba3115
YQ
10981 /* Fast tracepoints are mostly handled by the target, but we can
10982 tell the target how big of an instruction block should be moved
10983 around. */
10984 if (b->type == bp_fast_tracepoint)
10985 {
10986 /* Only test for support at download time; we may not know
10987 target capabilities at definition time. */
10988 if (remote_supports_fast_tracepoints ())
35b1e5cc 10989 {
e8ba3115 10990 int isize;
35b1e5cc 10991
f5656ead 10992 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
e8ba3115 10993 tpaddr, &isize, NULL))
bba74b36
YQ
10994 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10995 isize);
35b1e5cc 10996 else
e8ba3115
YQ
10997 /* If it passed validation at definition but fails now,
10998 something is very wrong. */
10999 internal_error (__FILE__, __LINE__,
11000 _("Fast tracepoint not "
11001 "valid during download"));
35b1e5cc 11002 }
e8ba3115
YQ
11003 else
11004 /* Fast tracepoints are functionally identical to regular
11005 tracepoints, so don't take lack of support as a reason to
11006 give up on the trace run. */
11007 warning (_("Target does not support fast tracepoints, "
11008 "downloading %d as regular tracepoint"), b->number);
11009 }
11010 else if (b->type == bp_static_tracepoint)
11011 {
11012 /* Only test for support at download time; we may not know
11013 target capabilities at definition time. */
11014 if (remote_supports_static_tracepoints ())
0fb4aa4b 11015 {
e8ba3115 11016 struct static_tracepoint_marker marker;
0fb4aa4b 11017
e8ba3115
YQ
11018 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11019 strcat (buf, ":S");
0fb4aa4b 11020 else
e8ba3115 11021 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11022 }
e8ba3115
YQ
11023 else
11024 /* Fast tracepoints are functionally identical to regular
11025 tracepoints, so don't take lack of support as a reason
11026 to give up on the trace run. */
11027 error (_("Target does not support static tracepoints"));
11028 }
11029 /* If the tracepoint has a conditional, make it into an agent
11030 expression and append to the definition. */
11031 if (loc->cond)
11032 {
11033 /* Only test support at download time, we may not know target
11034 capabilities at definition time. */
11035 if (remote_supports_cond_tracepoints ())
35b1e5cc 11036 {
e8ba3115
YQ
11037 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11038 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11039 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11040 aexpr->len);
e8ba3115
YQ
11041 pkt = buf + strlen (buf);
11042 for (ndx = 0; ndx < aexpr->len; ++ndx)
11043 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11044 *pkt = '\0';
11045 do_cleanups (aexpr_chain);
35b1e5cc 11046 }
e8ba3115
YQ
11047 else
11048 warning (_("Target does not support conditional tracepoints, "
11049 "ignoring tp %d cond"), b->number);
11050 }
35b1e5cc 11051
d9b3f62e 11052 if (b->commands || *default_collect)
e8ba3115
YQ
11053 strcat (buf, "-");
11054 putpkt (buf);
11055 remote_get_noisy_reply (&target_buf, &target_buf_size);
11056 if (strcmp (target_buf, "OK"))
11057 error (_("Target does not support tracepoints."));
35b1e5cc 11058
e8ba3115
YQ
11059 /* do_single_steps (t); */
11060 if (tdp_actions)
11061 {
11062 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11063 {
e8ba3115 11064 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11065 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11066 b->number, addrbuf, /* address */
11067 tdp_actions[ndx],
11068 ((tdp_actions[ndx + 1] || stepping_actions)
11069 ? '-' : 0));
e8ba3115
YQ
11070 putpkt (buf);
11071 remote_get_noisy_reply (&target_buf,
11072 &target_buf_size);
11073 if (strcmp (target_buf, "OK"))
11074 error (_("Error on target while setting tracepoints."));
35b1e5cc 11075 }
e8ba3115
YQ
11076 }
11077 if (stepping_actions)
11078 {
11079 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11080 {
e8ba3115 11081 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11082 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11083 b->number, addrbuf, /* address */
11084 ((ndx == 0) ? "S" : ""),
11085 stepping_actions[ndx],
11086 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11087 putpkt (buf);
11088 remote_get_noisy_reply (&target_buf,
11089 &target_buf_size);
11090 if (strcmp (target_buf, "OK"))
11091 error (_("Error on target while setting tracepoints."));
35b1e5cc 11092 }
e8ba3115 11093 }
409873ef 11094
4082afcc 11095 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115
YQ
11096 {
11097 if (b->addr_string)
409873ef 11098 {
e8ba3115
YQ
11099 strcpy (buf, "QTDPsrc:");
11100 encode_source_string (b->number, loc->address,
11101 "at", b->addr_string, buf + strlen (buf),
11102 2048 - strlen (buf));
409873ef 11103
e8ba3115
YQ
11104 putpkt (buf);
11105 remote_get_noisy_reply (&target_buf, &target_buf_size);
11106 if (strcmp (target_buf, "OK"))
11107 warning (_("Target does not support source download."));
409873ef 11108 }
e8ba3115
YQ
11109 if (b->cond_string)
11110 {
11111 strcpy (buf, "QTDPsrc:");
11112 encode_source_string (b->number, loc->address,
11113 "cond", b->cond_string, buf + strlen (buf),
11114 2048 - strlen (buf));
11115 putpkt (buf);
11116 remote_get_noisy_reply (&target_buf, &target_buf_size);
11117 if (strcmp (target_buf, "OK"))
11118 warning (_("Target does not support source download."));
11119 }
11120 remote_download_command_source (b->number, loc->address,
11121 breakpoint_commands (b));
35b1e5cc 11122 }
e8ba3115
YQ
11123
11124 do_cleanups (old_chain);
35b1e5cc
SS
11125}
11126
1e4d1764 11127static int
a52a8357 11128remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11129{
1e51243a
PA
11130 struct remote_state *rs = get_remote_state ();
11131 struct trace_status *ts;
11132 int status;
11133
11134 /* Don't try to install tracepoints until we've relocated our
11135 symbols, and fetched and merged the target's tracepoint list with
11136 ours. */
11137 if (rs->starting_up)
11138 return 0;
11139
11140 ts = current_trace_status ();
8bd200f1 11141 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11142
11143 if (status == -1 || !ts->running_known || !ts->running)
11144 return 0;
11145
11146 /* If we are in a tracing experiment, but remote stub doesn't support
11147 installing tracepoint in trace, we have to return. */
11148 if (!remote_supports_install_in_trace ())
11149 return 0;
11150
11151 return 1;
11152}
11153
11154
35b1e5cc 11155static void
559d2b81
TT
11156remote_download_trace_state_variable (struct target_ops *self,
11157 struct trace_state_variable *tsv)
35b1e5cc
SS
11158{
11159 struct remote_state *rs = get_remote_state ();
00bf0b85 11160 char *p;
35b1e5cc 11161
bba74b36
YQ
11162 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11163 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11164 tsv->builtin);
00bf0b85
SS
11165 p = rs->buf + strlen (rs->buf);
11166 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11167 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11168 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11169 *p++ = '\0';
35b1e5cc
SS
11170 putpkt (rs->buf);
11171 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11172 if (*target_buf == '\0')
11173 error (_("Target does not support this command."));
11174 if (strcmp (target_buf, "OK") != 0)
11175 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11176}
11177
d248b706 11178static void
46670d57
TT
11179remote_enable_tracepoint (struct target_ops *self,
11180 struct bp_location *location)
d248b706
KY
11181{
11182 struct remote_state *rs = get_remote_state ();
11183 char addr_buf[40];
11184
11185 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11186 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11187 location->owner->number, addr_buf);
d248b706
KY
11188 putpkt (rs->buf);
11189 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11190 if (*rs->buf == '\0')
11191 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11192 if (strcmp (rs->buf, "OK") != 0)
11193 error (_("Error on target while enabling tracepoint."));
11194}
11195
11196static void
780b049c
TT
11197remote_disable_tracepoint (struct target_ops *self,
11198 struct bp_location *location)
d248b706
KY
11199{
11200 struct remote_state *rs = get_remote_state ();
11201 char addr_buf[40];
11202
11203 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11204 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11205 location->owner->number, addr_buf);
d248b706
KY
11206 putpkt (rs->buf);
11207 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11208 if (*rs->buf == '\0')
11209 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11210 if (strcmp (rs->buf, "OK") != 0)
11211 error (_("Error on target while disabling tracepoint."));
11212}
11213
35b1e5cc 11214static void
583f9a86 11215remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11216{
11217 asection *s;
81b9b86e 11218 bfd *abfd = NULL;
35b1e5cc 11219 bfd_size_type size;
608bcef2 11220 bfd_vma vma;
35b1e5cc 11221 int anysecs = 0;
c2fa21f1 11222 int offset = 0;
35b1e5cc
SS
11223
11224 if (!exec_bfd)
11225 return; /* No information to give. */
11226
11227 strcpy (target_buf, "QTro");
9779ab84 11228 offset = strlen (target_buf);
35b1e5cc
SS
11229 for (s = exec_bfd->sections; s; s = s->next)
11230 {
11231 char tmp1[40], tmp2[40];
c2fa21f1 11232 int sec_length;
35b1e5cc
SS
11233
11234 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11235 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11236 (s->flags & SEC_READONLY) == 0)
11237 continue;
11238
11239 anysecs = 1;
81b9b86e 11240 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11241 size = bfd_get_section_size (s);
608bcef2
HZ
11242 sprintf_vma (tmp1, vma);
11243 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11244 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11245 if (offset + sec_length + 1 > target_buf_size)
11246 {
4082afcc 11247 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11248 warning (_("\
c2fa21f1
HZ
11249Too many sections for read-only sections definition packet."));
11250 break;
11251 }
bba74b36
YQ
11252 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11253 tmp1, tmp2);
c2fa21f1 11254 offset += sec_length;
35b1e5cc
SS
11255 }
11256 if (anysecs)
11257 {
11258 putpkt (target_buf);
11259 getpkt (&target_buf, &target_buf_size, 0);
11260 }
11261}
11262
11263static void
e2d1aae3 11264remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11265{
11266 putpkt ("QTStart");
11267 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11268 if (*target_buf == '\0')
11269 error (_("Target does not support this command."));
11270 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11271 error (_("Bogus reply from target: %s"), target_buf);
11272}
11273
11274static int
8bd200f1 11275remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11276{
953b98d1 11277 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11278 char *p = NULL;
0df8b418 11279 /* FIXME we need to get register block size some other way. */
00bf0b85 11280 extern int trace_regblock_size;
bd3eecc3
PA
11281 enum packet_result result;
11282
4082afcc 11283 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11284 return -1;
a744cf53 11285
00bf0b85
SS
11286 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11287
049dc89b
JK
11288 putpkt ("qTStatus");
11289
492d29ea 11290 TRY
67f41397
JK
11291 {
11292 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11293 }
492d29ea 11294 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11295 {
598d3636
JK
11296 if (ex.error != TARGET_CLOSE_ERROR)
11297 {
11298 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11299 return -1;
11300 }
11301 throw_exception (ex);
67f41397 11302 }
492d29ea 11303 END_CATCH
00bf0b85 11304
bd3eecc3
PA
11305 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11306
00bf0b85 11307 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11308 if (result == PACKET_UNKNOWN)
00bf0b85 11309 return -1;
35b1e5cc 11310
00bf0b85 11311 /* We're working with a live target. */
f5911ea1 11312 ts->filename = NULL;
00bf0b85 11313
00bf0b85 11314 if (*p++ != 'T')
35b1e5cc
SS
11315 error (_("Bogus trace status reply from target: %s"), target_buf);
11316
84cebc4a
YQ
11317 /* Function 'parse_trace_status' sets default value of each field of
11318 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11319 parse_trace_status (p, ts);
11320
11321 return ts->running;
35b1e5cc
SS
11322}
11323
70221824 11324static void
db90e85c 11325remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11326 struct uploaded_tp *utp)
11327{
11328 struct remote_state *rs = get_remote_state ();
f196051f
SS
11329 char *reply;
11330 struct bp_location *loc;
11331 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11332 size_t size = get_remote_packet_size ();
f196051f
SS
11333
11334 if (tp)
11335 {
11336 tp->base.hit_count = 0;
11337 tp->traceframe_usage = 0;
11338 for (loc = tp->base.loc; loc; loc = loc->next)
11339 {
11340 /* If the tracepoint was never downloaded, don't go asking for
11341 any status. */
11342 if (tp->number_on_target == 0)
11343 continue;
bba74b36
YQ
11344 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11345 phex_nz (loc->address, 0));
f196051f
SS
11346 putpkt (rs->buf);
11347 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11348 if (reply && *reply)
11349 {
11350 if (*reply == 'V')
11351 parse_tracepoint_status (reply + 1, bp, utp);
11352 }
11353 }
11354 }
11355 else if (utp)
11356 {
11357 utp->hit_count = 0;
11358 utp->traceframe_usage = 0;
bba74b36
YQ
11359 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11360 phex_nz (utp->addr, 0));
f196051f
SS
11361 putpkt (rs->buf);
11362 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11363 if (reply && *reply)
11364 {
11365 if (*reply == 'V')
11366 parse_tracepoint_status (reply + 1, bp, utp);
11367 }
11368 }
11369}
11370
35b1e5cc 11371static void
74499f1b 11372remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11373{
11374 putpkt ("QTStop");
11375 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11376 if (*target_buf == '\0')
11377 error (_("Target does not support this command."));
11378 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11379 error (_("Bogus reply from target: %s"), target_buf);
11380}
11381
11382static int
bd4c6793
TT
11383remote_trace_find (struct target_ops *self,
11384 enum trace_find_type type, int num,
cc5925ad 11385 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11386 int *tpp)
11387{
11388 struct remote_state *rs = get_remote_state ();
bba74b36 11389 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11390 char *p, *reply;
11391 int target_frameno = -1, target_tracept = -1;
11392
e6e4e701
PA
11393 /* Lookups other than by absolute frame number depend on the current
11394 trace selected, so make sure it is correct on the remote end
11395 first. */
11396 if (type != tfind_number)
11397 set_remote_traceframe ();
11398
35b1e5cc
SS
11399 p = rs->buf;
11400 strcpy (p, "QTFrame:");
11401 p = strchr (p, '\0');
11402 switch (type)
11403 {
11404 case tfind_number:
bba74b36 11405 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11406 break;
11407 case tfind_pc:
bba74b36 11408 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11409 break;
11410 case tfind_tp:
bba74b36 11411 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11412 break;
11413 case tfind_range:
bba74b36
YQ
11414 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11415 phex_nz (addr2, 0));
35b1e5cc
SS
11416 break;
11417 case tfind_outside:
bba74b36
YQ
11418 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11419 phex_nz (addr2, 0));
35b1e5cc
SS
11420 break;
11421 default:
9b20d036 11422 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11423 }
11424
11425 putpkt (rs->buf);
2f65bcb7 11426 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11427 if (*reply == '\0')
11428 error (_("Target does not support this command."));
35b1e5cc
SS
11429
11430 while (reply && *reply)
11431 switch (*reply)
11432 {
11433 case 'F':
f197e0f1
VP
11434 p = ++reply;
11435 target_frameno = (int) strtol (p, &reply, 16);
11436 if (reply == p)
11437 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11438 /* Don't update our remote traceframe number cache on failure
11439 to select a remote traceframe. */
f197e0f1
VP
11440 if (target_frameno == -1)
11441 return -1;
35b1e5cc
SS
11442 break;
11443 case 'T':
f197e0f1
VP
11444 p = ++reply;
11445 target_tracept = (int) strtol (p, &reply, 16);
11446 if (reply == p)
11447 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11448 break;
11449 case 'O': /* "OK"? */
11450 if (reply[1] == 'K' && reply[2] == '\0')
11451 reply += 2;
11452 else
11453 error (_("Bogus reply from target: %s"), reply);
11454 break;
11455 default:
11456 error (_("Bogus reply from target: %s"), reply);
11457 }
11458 if (tpp)
11459 *tpp = target_tracept;
e6e4e701 11460
262e1174 11461 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11462 return target_frameno;
11463}
11464
11465static int
4011015b
TT
11466remote_get_trace_state_variable_value (struct target_ops *self,
11467 int tsvnum, LONGEST *val)
35b1e5cc
SS
11468{
11469 struct remote_state *rs = get_remote_state ();
11470 char *reply;
11471 ULONGEST uval;
11472
e6e4e701
PA
11473 set_remote_traceframe ();
11474
bba74b36 11475 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11476 putpkt (rs->buf);
11477 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11478 if (reply && *reply)
11479 {
11480 if (*reply == 'V')
11481 {
11482 unpack_varlen_hex (reply + 1, &uval);
11483 *val = (LONGEST) uval;
11484 return 1;
11485 }
11486 }
11487 return 0;
11488}
11489
00bf0b85 11490static int
dc3decaf 11491remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11492{
11493 struct remote_state *rs = get_remote_state ();
11494 char *p, *reply;
11495
11496 p = rs->buf;
11497 strcpy (p, "QTSave:");
11498 p += strlen (p);
11499 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11500 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11501 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11502 *p++ = '\0';
11503 putpkt (rs->buf);
ad91cd99 11504 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11505 if (*reply == '\0')
ad91cd99
PA
11506 error (_("Target does not support this command."));
11507 if (strcmp (reply, "OK") != 0)
11508 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11509 return 0;
11510}
11511
11512/* This is basically a memory transfer, but needs to be its own packet
11513 because we don't know how the target actually organizes its trace
11514 memory, plus we want to be able to ask for as much as possible, but
11515 not be unhappy if we don't get as much as we ask for. */
11516
11517static LONGEST
88ee6f45
TT
11518remote_get_raw_trace_data (struct target_ops *self,
11519 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11520{
11521 struct remote_state *rs = get_remote_state ();
11522 char *reply;
11523 char *p;
11524 int rslt;
11525
11526 p = rs->buf;
11527 strcpy (p, "qTBuffer:");
11528 p += strlen (p);
11529 p += hexnumstr (p, offset);
11530 *p++ = ',';
11531 p += hexnumstr (p, len);
11532 *p++ = '\0';
11533
11534 putpkt (rs->buf);
11535 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11536 if (reply && *reply)
11537 {
11538 /* 'l' by itself means we're at the end of the buffer and
11539 there is nothing more to get. */
11540 if (*reply == 'l')
11541 return 0;
11542
11543 /* Convert the reply into binary. Limit the number of bytes to
11544 convert according to our passed-in buffer size, rather than
11545 what was returned in the packet; if the target is
11546 unexpectedly generous and gives us a bigger reply than we
11547 asked for, we don't want to crash. */
11548 rslt = hex2bin (target_buf, buf, len);
11549 return rslt;
11550 }
11551
11552 /* Something went wrong, flag as an error. */
11553 return -1;
11554}
11555
35b1e5cc 11556static void
37b25738 11557remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11558{
11559 struct remote_state *rs = get_remote_state ();
11560
4082afcc 11561 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11562 {
ad91cd99
PA
11563 char *reply;
11564
bba74b36 11565 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11566 putpkt (rs->buf);
ad91cd99
PA
11567 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11568 if (*reply == '\0')
33da3f1c 11569 error (_("Target does not support this command."));
ad91cd99
PA
11570 if (strcmp (reply, "OK") != 0)
11571 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11572 }
11573 else if (val)
11574 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11575}
11576
dc146f7c
VP
11577static int
11578remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11579{
11580 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11581
fe978cb0
PA
11582 if (info && info->priv)
11583 return info->priv->core;
dc146f7c
VP
11584 return -1;
11585}
11586
4daf5ac0 11587static void
736d5b1f 11588remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11589{
11590 struct remote_state *rs = get_remote_state ();
ad91cd99 11591 char *reply;
4daf5ac0 11592
bba74b36 11593 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11594 putpkt (rs->buf);
ad91cd99
PA
11595 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11596 if (*reply == '\0')
4daf5ac0 11597 error (_("Target does not support this command."));
ad91cd99
PA
11598 if (strcmp (reply, "OK") != 0)
11599 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11600}
11601
b3b9301e 11602static struct traceframe_info *
a893e81f 11603remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11604{
11605 char *text;
11606
11607 text = target_read_stralloc (&current_target,
11608 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11609 if (text != NULL)
11610 {
11611 struct traceframe_info *info;
11612 struct cleanup *back_to = make_cleanup (xfree, text);
11613
11614 info = parse_traceframe_info (text);
11615 do_cleanups (back_to);
11616 return info;
11617 }
11618
11619 return NULL;
11620}
11621
405f8e94
SS
11622/* Handle the qTMinFTPILen packet. Returns the minimum length of
11623 instruction on which a fast tracepoint may be placed. Returns -1
11624 if the packet is not supported, and 0 if the minimum instruction
11625 length is unknown. */
11626
11627static int
0e67620a 11628remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11629{
11630 struct remote_state *rs = get_remote_state ();
11631 char *reply;
11632
e886a173
PA
11633 /* If we're not debugging a process yet, the IPA can't be
11634 loaded. */
11635 if (!target_has_execution)
11636 return 0;
11637
11638 /* Make sure the remote is pointing at the right process. */
11639 set_general_process ();
11640
bba74b36 11641 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11642 putpkt (rs->buf);
11643 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11644 if (*reply == '\0')
11645 return -1;
11646 else
11647 {
11648 ULONGEST min_insn_len;
11649
11650 unpack_varlen_hex (reply, &min_insn_len);
11651
11652 return (int) min_insn_len;
11653 }
11654}
11655
f6f899bf 11656static void
4da384be 11657remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11658{
4082afcc 11659 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11660 {
11661 struct remote_state *rs = get_remote_state ();
11662 char *buf = rs->buf;
11663 char *endbuf = rs->buf + get_remote_packet_size ();
11664 enum packet_result result;
11665
11666 gdb_assert (val >= 0 || val == -1);
11667 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11668 /* Send -1 as literal "-1" to avoid host size dependency. */
11669 if (val < 0)
11670 {
11671 *buf++ = '-';
11672 buf += hexnumstr (buf, (ULONGEST) -val);
11673 }
11674 else
11675 buf += hexnumstr (buf, (ULONGEST) val);
11676
11677 putpkt (rs->buf);
11678 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11679 result = packet_ok (rs->buf,
11680 &remote_protocol_packets[PACKET_QTBuffer_size]);
11681
11682 if (result != PACKET_OK)
11683 warning (_("Bogus reply from target: %s"), rs->buf);
11684 }
11685}
11686
f196051f 11687static int
d9e68a2c
TT
11688remote_set_trace_notes (struct target_ops *self,
11689 const char *user, const char *notes,
ca623f82 11690 const char *stop_notes)
f196051f
SS
11691{
11692 struct remote_state *rs = get_remote_state ();
11693 char *reply;
11694 char *buf = rs->buf;
11695 char *endbuf = rs->buf + get_remote_packet_size ();
11696 int nbytes;
11697
11698 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11699 if (user)
11700 {
11701 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11702 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11703 buf += 2 * nbytes;
11704 *buf++ = ';';
11705 }
11706 if (notes)
11707 {
11708 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11709 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11710 buf += 2 * nbytes;
11711 *buf++ = ';';
11712 }
11713 if (stop_notes)
11714 {
11715 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11716 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11717 buf += 2 * nbytes;
11718 *buf++ = ';';
11719 }
11720 /* Ensure the buffer is terminated. */
11721 *buf = '\0';
11722
11723 putpkt (rs->buf);
11724 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11725 if (*reply == '\0')
11726 return 0;
11727
11728 if (strcmp (reply, "OK") != 0)
11729 error (_("Bogus reply from target: %s"), reply);
11730
11731 return 1;
11732}
11733
d1feda86 11734static int
2c152180 11735remote_use_agent (struct target_ops *self, int use)
d1feda86 11736{
4082afcc 11737 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11738 {
11739 struct remote_state *rs = get_remote_state ();
11740
11741 /* If the stub supports QAgent. */
bba74b36 11742 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11743 putpkt (rs->buf);
11744 getpkt (&rs->buf, &rs->buf_size, 0);
11745
11746 if (strcmp (rs->buf, "OK") == 0)
11747 {
11748 use_agent = use;
11749 return 1;
11750 }
11751 }
11752
11753 return 0;
11754}
11755
11756static int
fe38f897 11757remote_can_use_agent (struct target_ops *self)
d1feda86 11758{
4082afcc 11759 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11760}
11761
9accd112
MM
11762struct btrace_target_info
11763{
11764 /* The ptid of the traced thread. */
11765 ptid_t ptid;
f4abbc16
MM
11766
11767 /* The obtained branch trace configuration. */
11768 struct btrace_config conf;
9accd112
MM
11769};
11770
f4abbc16
MM
11771/* Reset our idea of our target's btrace configuration. */
11772
11773static void
11774remote_btrace_reset (void)
11775{
11776 struct remote_state *rs = get_remote_state ();
11777
11778 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11779}
11780
9accd112
MM
11781/* Check whether the target supports branch tracing. */
11782
11783static int
043c3577 11784remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11785{
4082afcc 11786 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11787 return 0;
4082afcc 11788 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11789 return 0;
11790
043c3577
MM
11791 switch (format)
11792 {
11793 case BTRACE_FORMAT_NONE:
11794 return 0;
11795
11796 case BTRACE_FORMAT_BTS:
11797 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
11798 }
11799
11800 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11801}
11802
f4abbc16
MM
11803/* Synchronize the configuration with the target. */
11804
11805static void
11806btrace_sync_conf (const struct btrace_config *conf)
11807{
d33501a5
MM
11808 struct packet_config *packet;
11809 struct remote_state *rs;
11810 char *buf, *pos, *endbuf;
11811
11812 rs = get_remote_state ();
11813 buf = rs->buf;
11814 endbuf = buf + get_remote_packet_size ();
11815
11816 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11817 if (packet_config_support (packet) == PACKET_ENABLE
11818 && conf->bts.size != rs->btrace_config.bts.size)
11819 {
11820 pos = buf;
11821 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11822 conf->bts.size);
11823
11824 putpkt (buf);
11825 getpkt (&buf, &rs->buf_size, 0);
11826
11827 if (packet_ok (buf, packet) == PACKET_ERROR)
11828 {
11829 if (buf[0] == 'E' && buf[1] == '.')
11830 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11831 else
11832 error (_("Failed to configure the BTS buffer size."));
11833 }
11834
11835 rs->btrace_config.bts.size = conf->bts.size;
11836 }
f4abbc16
MM
11837}
11838
11839/* Read the current thread's btrace configuration from the target and
11840 store it into CONF. */
11841
11842static void
11843btrace_read_config (struct btrace_config *conf)
11844{
11845 char *xml;
11846
11847 xml = target_read_stralloc (&current_target,
11848 TARGET_OBJECT_BTRACE_CONF, "");
11849 if (xml != NULL)
11850 {
11851 struct cleanup *cleanup;
11852
11853 cleanup = make_cleanup (xfree, xml);
11854 parse_xml_btrace_conf (conf, xml);
11855 do_cleanups (cleanup);
11856 }
11857}
11858
9accd112
MM
11859/* Enable branch tracing. */
11860
11861static struct btrace_target_info *
f4abbc16
MM
11862remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11863 const struct btrace_config *conf)
9accd112
MM
11864{
11865 struct btrace_target_info *tinfo = NULL;
11866 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11867 struct remote_state *rs = get_remote_state ();
11868 char *buf = rs->buf;
11869 char *endbuf = rs->buf + get_remote_packet_size ();
11870
4082afcc 11871 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11872 error (_("Target does not support branch tracing."));
11873
f4abbc16
MM
11874 btrace_sync_conf (conf);
11875
9accd112
MM
11876 set_general_thread (ptid);
11877
11878 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11879 putpkt (rs->buf);
11880 getpkt (&rs->buf, &rs->buf_size, 0);
11881
11882 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11883 {
11884 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11885 error (_("Could not enable branch tracing for %s: %s"),
11886 target_pid_to_str (ptid), rs->buf + 2);
11887 else
11888 error (_("Could not enable branch tracing for %s."),
11889 target_pid_to_str (ptid));
11890 }
11891
11892 tinfo = xzalloc (sizeof (*tinfo));
11893 tinfo->ptid = ptid;
11894
f4abbc16
MM
11895 /* If we fail to read the configuration, we lose some information, but the
11896 tracing itself is not impacted. */
492d29ea
PA
11897 TRY
11898 {
11899 btrace_read_config (&tinfo->conf);
11900 }
11901 CATCH (err, RETURN_MASK_ERROR)
11902 {
11903 if (err.message != NULL)
11904 warning ("%s", err.message);
11905 }
11906 END_CATCH
f4abbc16 11907
9accd112
MM
11908 return tinfo;
11909}
11910
11911/* Disable branch tracing. */
11912
11913static void
25e95349
TT
11914remote_disable_btrace (struct target_ops *self,
11915 struct btrace_target_info *tinfo)
9accd112
MM
11916{
11917 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11918 struct remote_state *rs = get_remote_state ();
11919 char *buf = rs->buf;
11920 char *endbuf = rs->buf + get_remote_packet_size ();
11921
4082afcc 11922 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11923 error (_("Target does not support branch tracing."));
11924
11925 set_general_thread (tinfo->ptid);
11926
11927 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11928 putpkt (rs->buf);
11929 getpkt (&rs->buf, &rs->buf_size, 0);
11930
11931 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11932 {
11933 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11934 error (_("Could not disable branch tracing for %s: %s"),
11935 target_pid_to_str (tinfo->ptid), rs->buf + 2);
11936 else
11937 error (_("Could not disable branch tracing for %s."),
11938 target_pid_to_str (tinfo->ptid));
11939 }
11940
11941 xfree (tinfo);
11942}
11943
11944/* Teardown branch tracing. */
11945
11946static void
1777056d
TT
11947remote_teardown_btrace (struct target_ops *self,
11948 struct btrace_target_info *tinfo)
9accd112
MM
11949{
11950 /* We must not talk to the target during teardown. */
11951 xfree (tinfo);
11952}
11953
11954/* Read the branch trace. */
11955
969c39fb 11956static enum btrace_error
39c49f83 11957remote_read_btrace (struct target_ops *self,
734b0e4b 11958 struct btrace_data *btrace,
969c39fb 11959 struct btrace_target_info *tinfo,
9accd112
MM
11960 enum btrace_read_type type)
11961{
11962 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11963 struct remote_state *rs = get_remote_state ();
969c39fb 11964 struct cleanup *cleanup;
9accd112
MM
11965 const char *annex;
11966 char *xml;
11967
4082afcc 11968 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11969 error (_("Target does not support branch tracing."));
11970
11971#if !defined(HAVE_LIBEXPAT)
11972 error (_("Cannot process branch tracing result. XML parsing not supported."));
11973#endif
11974
11975 switch (type)
11976 {
864089d2 11977 case BTRACE_READ_ALL:
9accd112
MM
11978 annex = "all";
11979 break;
864089d2 11980 case BTRACE_READ_NEW:
9accd112
MM
11981 annex = "new";
11982 break;
969c39fb
MM
11983 case BTRACE_READ_DELTA:
11984 annex = "delta";
11985 break;
9accd112
MM
11986 default:
11987 internal_error (__FILE__, __LINE__,
11988 _("Bad branch tracing read type: %u."),
11989 (unsigned int) type);
11990 }
11991
11992 xml = target_read_stralloc (&current_target,
11993 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
11994 if (xml == NULL)
11995 return BTRACE_ERR_UNKNOWN;
9accd112 11996
969c39fb 11997 cleanup = make_cleanup (xfree, xml);
734b0e4b 11998 parse_xml_btrace (btrace, xml);
969c39fb 11999 do_cleanups (cleanup);
9accd112 12000
969c39fb 12001 return BTRACE_ERR_NONE;
9accd112
MM
12002}
12003
f4abbc16
MM
12004static const struct btrace_config *
12005remote_btrace_conf (struct target_ops *self,
12006 const struct btrace_target_info *tinfo)
12007{
12008 return &tinfo->conf;
12009}
12010
ced63ec0 12011static int
5436ff03 12012remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12013{
4082afcc
PA
12014 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12015 == PACKET_ENABLE);
ced63ec0
GB
12016}
12017
9dd130a0
TT
12018/* Implementation of to_load. */
12019
12020static void
9cbe5fff 12021remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12022{
12023 generic_load (name, from_tty);
12024}
12025
c78fa86a
GB
12026/* Accepts an integer PID; returns a string representing a file that
12027 can be opened on the remote side to get the symbols for the child
12028 process. Returns NULL if the operation is not supported. */
12029
12030static char *
12031remote_pid_to_exec_file (struct target_ops *self, int pid)
12032{
12033 static char *filename = NULL;
835205d0
GB
12034 struct inferior *inf;
12035 char *annex = NULL;
c78fa86a
GB
12036
12037 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12038 return NULL;
12039
12040 if (filename != NULL)
12041 xfree (filename);
12042
835205d0
GB
12043 inf = find_inferior_pid (pid);
12044 if (inf == NULL)
12045 internal_error (__FILE__, __LINE__,
12046 _("not currently attached to process %d"), pid);
12047
12048 if (!inf->fake_pid_p)
12049 {
12050 const int annex_size = 9;
12051
12052 annex = alloca (annex_size);
12053 xsnprintf (annex, annex_size, "%x", pid);
12054 }
12055
c78fa86a
GB
12056 filename = target_read_stralloc (&current_target,
12057 TARGET_OBJECT_EXEC_FILE, annex);
12058
12059 return filename;
12060}
12061
c906108c 12062static void
fba45db2 12063init_remote_ops (void)
c906108c 12064{
c5aa993b 12065 remote_ops.to_shortname = "remote";
c906108c 12066 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12067 remote_ops.to_doc =
c906108c 12068 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12069Specify the serial device it is connected to\n\
12070(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12071 remote_ops.to_open = remote_open;
12072 remote_ops.to_close = remote_close;
c906108c 12073 remote_ops.to_detach = remote_detach;
6ad8ae5c 12074 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12075 remote_ops.to_resume = remote_resume;
c906108c
SS
12076 remote_ops.to_wait = remote_wait;
12077 remote_ops.to_fetch_registers = remote_fetch_registers;
12078 remote_ops.to_store_registers = remote_store_registers;
12079 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12080 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12081 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12082 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12083 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12084 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12085 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12086 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12087 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12088 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12089 remote_ops.to_watchpoint_addr_within_range =
12090 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12091 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12092 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12093 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12094 remote_ops.to_region_ok_for_hw_watchpoint
12095 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12096 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12097 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12098 remote_ops.to_kill = remote_kill;
9dd130a0 12099 remote_ops.to_load = remote_load;
c906108c 12100 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12101 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12102 remote_ops.to_program_signals = remote_program_signals;
c906108c 12103 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12104 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12105 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12106 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12107 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12108 remote_ops.to_stop = remote_stop;
4b8a223f 12109 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12110 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12111 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12112 remote_ops.to_log_command = serial_log_command;
38691318 12113 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12114 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12115 remote_ops.to_has_all_memory = default_child_has_all_memory;
12116 remote_ops.to_has_memory = default_child_has_memory;
12117 remote_ops.to_has_stack = default_child_has_stack;
12118 remote_ops.to_has_registers = default_child_has_registers;
12119 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12120 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12121 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12122 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12123 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12124 remote_ops.to_flash_erase = remote_flash_erase;
12125 remote_ops.to_flash_done = remote_flash_done;
29709017 12126 remote_ops.to_read_description = remote_read_description;
08388c79 12127 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12128 remote_ops.to_can_async_p = remote_can_async_p;
12129 remote_ops.to_is_async_p = remote_is_async_p;
12130 remote_ops.to_async = remote_async;
75c99385
PA
12131 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12132 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12133 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12134 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12135 remote_ops.to_supports_disable_randomization
12136 = remote_supports_disable_randomization;
4bd7dc42 12137 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12138 remote_ops.to_fileio_open = remote_hostio_open;
12139 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12140 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12141 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12142 remote_ops.to_fileio_close = remote_hostio_close;
12143 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12144 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12145 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12146 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12147 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12148 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12149 remote_ops.to_trace_init = remote_trace_init;
12150 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12151 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12152 remote_ops.to_download_trace_state_variable
12153 = remote_download_trace_state_variable;
d248b706
KY
12154 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12155 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12156 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12157 remote_ops.to_trace_start = remote_trace_start;
12158 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12159 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12160 remote_ops.to_trace_stop = remote_trace_stop;
12161 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12162 remote_ops.to_get_trace_state_variable_value
12163 = remote_get_trace_state_variable_value;
00bf0b85
SS
12164 remote_ops.to_save_trace_data = remote_save_trace_data;
12165 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12166 remote_ops.to_upload_trace_state_variables
12167 = remote_upload_trace_state_variables;
00bf0b85 12168 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12169 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12170 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12171 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12172 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12173 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12174 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12175 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12176 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12177 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12178 remote_ops.to_static_tracepoint_marker_at
12179 = remote_static_tracepoint_marker_at;
12180 remote_ops.to_static_tracepoint_markers_by_strid
12181 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12182 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12183 remote_ops.to_use_agent = remote_use_agent;
12184 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12185 remote_ops.to_supports_btrace = remote_supports_btrace;
12186 remote_ops.to_enable_btrace = remote_enable_btrace;
12187 remote_ops.to_disable_btrace = remote_disable_btrace;
12188 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12189 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12190 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12191 remote_ops.to_augmented_libraries_svr4_read =
12192 remote_augmented_libraries_svr4_read;
c906108c
SS
12193}
12194
12195/* Set up the extended remote vector by making a copy of the standard
12196 remote vector and adding to it. */
12197
12198static void
fba45db2 12199init_extended_remote_ops (void)
c906108c
SS
12200{
12201 extended_remote_ops = remote_ops;
12202
0f71a2f6 12203 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12204 extended_remote_ops.to_longname =
c906108c 12205 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12206 extended_remote_ops.to_doc =
c906108c 12207 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12208Specify the serial device it is connected to (e.g. /dev/ttya).";
12209 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12210 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12211 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12212 extended_remote_ops.to_detach = extended_remote_detach;
12213 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12214 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12215 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12216 extended_remote_ops.to_supports_disable_randomization
12217 = extended_remote_supports_disable_randomization;
de0d863e 12218 extended_remote_ops.to_follow_fork = remote_follow_fork;
cbb8991c
DB
12219 extended_remote_ops.to_insert_fork_catchpoint
12220 = remote_insert_fork_catchpoint;
12221 extended_remote_ops.to_remove_fork_catchpoint
12222 = remote_remove_fork_catchpoint;
12223 extended_remote_ops.to_insert_vfork_catchpoint
12224 = remote_insert_vfork_catchpoint;
12225 extended_remote_ops.to_remove_vfork_catchpoint
12226 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12227}
12228
6426a772 12229static int
6a109b6b 12230remote_can_async_p (struct target_ops *ops)
6426a772 12231{
5d93a237
TT
12232 struct remote_state *rs = get_remote_state ();
12233
c6ebd6cf 12234 if (!target_async_permitted)
75c99385
PA
12235 /* We only enable async when the user specifically asks for it. */
12236 return 0;
12237
23860348 12238 /* We're async whenever the serial device is. */
5d93a237 12239 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12240}
12241
12242static int
6a109b6b 12243remote_is_async_p (struct target_ops *ops)
6426a772 12244{
5d93a237
TT
12245 struct remote_state *rs = get_remote_state ();
12246
c6ebd6cf 12247 if (!target_async_permitted)
75c99385
PA
12248 /* We only enable async when the user specifically asks for it. */
12249 return 0;
12250
23860348 12251 /* We're async whenever the serial device is. */
5d93a237 12252 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12253}
12254
2acceee2
JM
12255/* Pass the SERIAL event on and up to the client. One day this code
12256 will be able to delay notifying the client of an event until the
23860348 12257 point where an entire packet has been received. */
2acceee2 12258
2acceee2
JM
12259static serial_event_ftype remote_async_serial_handler;
12260
6426a772 12261static void
819cc324 12262remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12263{
88b496c3
TT
12264 struct remote_state *rs = context;
12265
2acceee2
JM
12266 /* Don't propogate error information up to the client. Instead let
12267 the client find out about the error by querying the target. */
6a3753b3 12268 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12269}
12270
74531fed
PA
12271static void
12272remote_async_inferior_event_handler (gdb_client_data data)
12273{
12274 inferior_event_handler (INF_REG_EVENT, NULL);
12275}
12276
2acceee2 12277static void
6a3753b3 12278remote_async (struct target_ops *ops, int enable)
2acceee2 12279{
5d93a237
TT
12280 struct remote_state *rs = get_remote_state ();
12281
6a3753b3 12282 if (enable)
2acceee2 12283 {
88b496c3 12284 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12285
12286 /* If there are pending events in the stop reply queue tell the
12287 event loop to process them. */
12288 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12289 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12290 }
12291 else
b7d2e916
PA
12292 {
12293 serial_async (rs->remote_desc, NULL, NULL);
12294 clear_async_event_handler (remote_async_inferior_event_token);
12295 }
6426a772
JM
12296}
12297
5a2468f5 12298static void
c2d11a7d 12299set_remote_cmd (char *args, int from_tty)
5a2468f5 12300{
635c7e8a 12301 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12302}
12303
d471ea57
AC
12304static void
12305show_remote_cmd (char *args, int from_tty)
12306{
37a105a1 12307 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12308 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12309 struct cleanup *showlist_chain;
12310 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12311 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12312
12313 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12314 for (; list != NULL; list = list->next)
12315 if (strcmp (list->name, "Z-packet") == 0)
12316 continue;
427c3a89
DJ
12317 else if (list->type == not_set_cmd)
12318 /* Alias commands are exactly like the original, except they
12319 don't have the normal type. */
12320 continue;
12321 else
37a105a1
DJ
12322 {
12323 struct cleanup *option_chain
12324 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12325
37a105a1
DJ
12326 ui_out_field_string (uiout, "name", list->name);
12327 ui_out_text (uiout, ": ");
427c3a89 12328 if (list->type == show_cmd)
5b9afe8a 12329 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12330 else
12331 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12332 /* Close the tuple. */
12333 do_cleanups (option_chain);
12334 }
427c3a89
DJ
12335
12336 /* Close the tuple. */
12337 do_cleanups (showlist_chain);
d471ea57 12338}
5a2468f5 12339
0f71a2f6 12340
23860348 12341/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12342static void
12343remote_new_objfile (struct objfile *objfile)
12344{
5d93a237
TT
12345 struct remote_state *rs = get_remote_state ();
12346
12347 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12348 remote_check_symbols ();
dc8acb97
MS
12349}
12350
00bf0b85
SS
12351/* Pull all the tracepoints defined on the target and create local
12352 data structures representing them. We don't want to create real
12353 tracepoints yet, we don't want to mess up the user's existing
12354 collection. */
12355
12356static int
ab6617cc 12357remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12358{
00bf0b85
SS
12359 struct remote_state *rs = get_remote_state ();
12360 char *p;
d5551862 12361
00bf0b85
SS
12362 /* Ask for a first packet of tracepoint definition. */
12363 putpkt ("qTfP");
12364 getpkt (&rs->buf, &rs->buf_size, 0);
12365 p = rs->buf;
12366 while (*p && *p != 'l')
d5551862 12367 {
00bf0b85
SS
12368 parse_tracepoint_definition (p, utpp);
12369 /* Ask for another packet of tracepoint definition. */
12370 putpkt ("qTsP");
12371 getpkt (&rs->buf, &rs->buf_size, 0);
12372 p = rs->buf;
d5551862 12373 }
00bf0b85 12374 return 0;
d5551862
SS
12375}
12376
00bf0b85 12377static int
181e3713
TT
12378remote_upload_trace_state_variables (struct target_ops *self,
12379 struct uploaded_tsv **utsvp)
d5551862 12380{
00bf0b85 12381 struct remote_state *rs = get_remote_state ();
d5551862 12382 char *p;
d5551862 12383
00bf0b85
SS
12384 /* Ask for a first packet of variable definition. */
12385 putpkt ("qTfV");
d5551862
SS
12386 getpkt (&rs->buf, &rs->buf_size, 0);
12387 p = rs->buf;
00bf0b85 12388 while (*p && *p != 'l')
d5551862 12389 {
00bf0b85
SS
12390 parse_tsv_definition (p, utsvp);
12391 /* Ask for another packet of variable definition. */
12392 putpkt ("qTsV");
d5551862
SS
12393 getpkt (&rs->buf, &rs->buf_size, 0);
12394 p = rs->buf;
12395 }
00bf0b85 12396 return 0;
d5551862
SS
12397}
12398
c1e36e3e
PA
12399/* The "set/show range-stepping" show hook. */
12400
12401static void
12402show_range_stepping (struct ui_file *file, int from_tty,
12403 struct cmd_list_element *c,
12404 const char *value)
12405{
12406 fprintf_filtered (file,
12407 _("Debugger's willingness to use range stepping "
12408 "is %s.\n"), value);
12409}
12410
12411/* The "set/show range-stepping" set hook. */
12412
12413static void
12414set_range_stepping (char *ignore_args, int from_tty,
12415 struct cmd_list_element *c)
12416{
5d93a237
TT
12417 struct remote_state *rs = get_remote_state ();
12418
c1e36e3e
PA
12419 /* Whene enabling, check whether range stepping is actually
12420 supported by the target, and warn if not. */
12421 if (use_range_stepping)
12422 {
5d93a237 12423 if (rs->remote_desc != NULL)
c1e36e3e 12424 {
4082afcc 12425 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12426 remote_vcont_probe (rs);
12427
4082afcc 12428 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12429 && rs->supports_vCont.r)
12430 return;
12431 }
12432
12433 warning (_("Range stepping is not supported by the current target"));
12434 }
12435}
12436
c906108c 12437void
fba45db2 12438_initialize_remote (void)
c906108c 12439{
ea9c271d 12440 struct remote_state *rs;
9a7071a8 12441 struct cmd_list_element *cmd;
6f937416 12442 const char *cmd_name;
ea9c271d 12443
0f71a2f6 12444 /* architecture specific data */
2bc416ba 12445 remote_gdbarch_data_handle =
23860348 12446 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12447 remote_g_packet_data_handle =
12448 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12449
ea9c271d
DJ
12450 /* Initialize the per-target state. At the moment there is only one
12451 of these, not one per target. Only one target is active at a
cf792862
TT
12452 time. */
12453 remote_state = new_remote_state ();
ea9c271d 12454
c906108c
SS
12455 init_remote_ops ();
12456 add_target (&remote_ops);
12457
12458 init_extended_remote_ops ();
12459 add_target (&extended_remote_ops);
cce74817 12460
dc8acb97 12461 /* Hook into new objfile notification. */
06d3b283 12462 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12463 /* We're no longer interested in notification events of an inferior
12464 when it exits. */
12465 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12466
b803fb0f 12467 /* Set up signal handlers. */
934b9bac 12468 async_sigint_remote_token =
b803fb0f 12469 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12470 async_sigint_remote_twice_token =
6d549500 12471 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12472
c906108c
SS
12473#if 0
12474 init_remote_threadtests ();
12475#endif
12476
722247f1 12477 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12478 /* set/show remote ... */
d471ea57 12479
1bedd215 12480 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12481Remote protocol specific variables\n\
12482Configure various remote-protocol specific variables such as\n\
1bedd215 12483the packets being used"),
cff3e48b 12484 &remote_set_cmdlist, "set remote ",
23860348 12485 0 /* allow-unknown */, &setlist);
1bedd215 12486 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12487Remote protocol specific variables\n\
12488Configure various remote-protocol specific variables such as\n\
1bedd215 12489the packets being used"),
cff3e48b 12490 &remote_show_cmdlist, "show remote ",
23860348 12491 0 /* allow-unknown */, &showlist);
5a2468f5 12492
1a966eab
AC
12493 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12494Compare section data on target to the exec file.\n\
95cf3b38
DT
12495Argument is a single section name (default: all loaded sections).\n\
12496To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12497 &cmdlist);
12498
1a966eab
AC
12499 add_cmd ("packet", class_maintenance, packet_command, _("\
12500Send an arbitrary packet to a remote target.\n\
c906108c
SS
12501 maintenance packet TEXT\n\
12502If GDB is talking to an inferior via the GDB serial protocol, then\n\
12503this command sends the string TEXT to the inferior, and displays the\n\
12504response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12505terminating `#' character and checksum."),
c906108c
SS
12506 &maintenancelist);
12507
7915a72c
AC
12508 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12509Set whether to send break if interrupted."), _("\
12510Show whether to send break if interrupted."), _("\
12511If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12512 set_remotebreak, show_remotebreak,
e707bbc2 12513 &setlist, &showlist);
9a7071a8
JB
12514 cmd_name = "remotebreak";
12515 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12516 deprecate_cmd (cmd, "set remote interrupt-sequence");
12517 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12518 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12519 deprecate_cmd (cmd, "show remote interrupt-sequence");
12520
12521 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12522 interrupt_sequence_modes, &interrupt_sequence_mode,
12523 _("\
9a7071a8
JB
12524Set interrupt sequence to remote target."), _("\
12525Show interrupt sequence to remote target."), _("\
12526Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12527 NULL, show_interrupt_sequence,
12528 &remote_set_cmdlist,
12529 &remote_show_cmdlist);
12530
12531 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12532 &interrupt_on_connect, _("\
12533Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12534Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12535If set, interrupt sequence is sent to remote target."),
12536 NULL, NULL,
12537 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12538
23860348 12539 /* Install commands for configuring memory read/write packets. */
11cf8741 12540
1a966eab
AC
12541 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12542Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12543 &setlist);
1a966eab
AC
12544 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12545Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12546 &showlist);
12547 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12548 set_memory_write_packet_size, _("\
12549Set the maximum number of bytes per memory-write packet.\n\
12550Specify the number of bytes in a packet or 0 (zero) for the\n\
12551default packet size. The actual limit is further reduced\n\
12552dependent on the target. Specify ``fixed'' to disable the\n\
12553further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12554 &remote_set_cmdlist);
12555 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12556 set_memory_read_packet_size, _("\
12557Set the maximum number of bytes per memory-read packet.\n\
12558Specify the number of bytes in a packet or 0 (zero) for the\n\
12559default packet size. The actual limit is further reduced\n\
12560dependent on the target. Specify ``fixed'' to disable the\n\
12561further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12562 &remote_set_cmdlist);
12563 add_cmd ("memory-write-packet-size", no_class,
12564 show_memory_write_packet_size,
1a966eab 12565 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12566 &remote_show_cmdlist);
12567 add_cmd ("memory-read-packet-size", no_class,
12568 show_memory_read_packet_size,
1a966eab 12569 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12570 &remote_show_cmdlist);
c906108c 12571
b3f42336 12572 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12573 &remote_hw_watchpoint_limit, _("\
12574Set the maximum number of target hardware watchpoints."), _("\
12575Show the maximum number of target hardware watchpoints."), _("\
12576Specify a negative limit for unlimited."),
3e43a32a
MS
12577 NULL, NULL, /* FIXME: i18n: The maximum
12578 number of target hardware
12579 watchpoints is %s. */
b3f42336 12580 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12581 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12582 &remote_hw_watchpoint_length_limit, _("\
12583Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12584Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12585Specify a negative limit for unlimited."),
12586 NULL, NULL, /* FIXME: i18n: The maximum
12587 length (in bytes) of a target
12588 hardware watchpoint is %s. */
12589 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12590 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12591 &remote_hw_breakpoint_limit, _("\
12592Set the maximum number of target hardware breakpoints."), _("\
12593Show the maximum number of target hardware breakpoints."), _("\
12594Specify a negative limit for unlimited."),
3e43a32a
MS
12595 NULL, NULL, /* FIXME: i18n: The maximum
12596 number of target hardware
12597 breakpoints is %s. */
b3f42336 12598 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12599
1b493192
PA
12600 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12601 &remote_address_size, _("\
4d28ad1e
AC
12602Set the maximum size of the address (in bits) in a memory packet."), _("\
12603Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12604 NULL,
12605 NULL, /* FIXME: i18n: */
12606 &setlist, &showlist);
c906108c 12607
ca4f7f8b
PA
12608 init_all_packet_configs ();
12609
444abaca 12610 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12611 "X", "binary-download", 1);
0f71a2f6 12612
444abaca 12613 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12614 "vCont", "verbose-resume", 0);
506fb367 12615
89be2091
DJ
12616 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12617 "QPassSignals", "pass-signals", 0);
12618
9b224c5e
PA
12619 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12620 "QProgramSignals", "program-signals", 0);
12621
444abaca 12622 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12623 "qSymbol", "symbol-lookup", 0);
dc8acb97 12624
444abaca 12625 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12626 "P", "set-register", 1);
d471ea57 12627
444abaca 12628 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12629 "p", "fetch-register", 1);
b96ec7ac 12630
444abaca 12631 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12632 "Z0", "software-breakpoint", 0);
d471ea57 12633
444abaca 12634 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12635 "Z1", "hardware-breakpoint", 0);
d471ea57 12636
444abaca 12637 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12638 "Z2", "write-watchpoint", 0);
d471ea57 12639
444abaca 12640 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12641 "Z3", "read-watchpoint", 0);
d471ea57 12642
444abaca 12643 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12644 "Z4", "access-watchpoint", 0);
d471ea57 12645
0876f84a
DJ
12646 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12647 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12648
c78fa86a
GB
12649 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12650 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12651
23181151
DJ
12652 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12653 "qXfer:features:read", "target-features", 0);
12654
cfa9d6d9
DJ
12655 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12656 "qXfer:libraries:read", "library-info", 0);
12657
2268b414
JK
12658 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12659 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12660
fd79ecee
DJ
12661 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12662 "qXfer:memory-map:read", "memory-map", 0);
12663
0e7f50da
UW
12664 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12665 "qXfer:spu:read", "read-spu-object", 0);
12666
12667 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12668 "qXfer:spu:write", "write-spu-object", 0);
12669
07e059b5
VP
12670 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12671 "qXfer:osdata:read", "osdata", 0);
12672
dc146f7c
VP
12673 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12674 "qXfer:threads:read", "threads", 0);
12675
4aa995e1
PA
12676 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12677 "qXfer:siginfo:read", "read-siginfo-object", 0);
12678
12679 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12680 "qXfer:siginfo:write", "write-siginfo-object", 0);
12681
b3b9301e
PA
12682 add_packet_config_cmd
12683 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12684 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12685
169081d0
TG
12686 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12687 "qXfer:uib:read", "unwind-info-block", 0);
12688
444abaca 12689 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12690 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12691 0);
12692
711e434b
PM
12693 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12694 "qGetTIBAddr", "get-thread-information-block-address",
12695 0);
12696
40ab02ce
MS
12697 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12698 "bc", "reverse-continue", 0);
12699
12700 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12701 "bs", "reverse-step", 0);
12702
be2a5f71
DJ
12703 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12704 "qSupported", "supported-packets", 0);
12705
08388c79
DE
12706 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12707 "qSearch:memory", "search-memory", 0);
12708
bd3eecc3
PA
12709 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12710 "qTStatus", "trace-status", 0);
12711
a6b151f1
DJ
12712 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12713 "vFile:open", "hostio-open", 0);
12714
12715 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12716 "vFile:pread", "hostio-pread", 0);
12717
12718 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12719 "vFile:pwrite", "hostio-pwrite", 0);
12720
12721 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12722 "vFile:close", "hostio-close", 0);
12723
12724 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12725 "vFile:unlink", "hostio-unlink", 0);
12726
b9e7b9c3
UW
12727 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12728 "vFile:readlink", "hostio-readlink", 0);
12729
0a93529c
GB
12730 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12731 "vFile:fstat", "hostio-fstat", 0);
12732
2d717e4f
DJ
12733 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12734 "vAttach", "attach", 0);
12735
12736 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12737 "vRun", "run", 0);
12738
a6f3e723
SL
12739 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12740 "QStartNoAckMode", "noack", 0);
12741
82f73884
PA
12742 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12743 "vKill", "kill", 0);
12744
0b16c5cf
PA
12745 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12746 "qAttached", "query-attached", 0);
12747
782b2b07 12748 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12749 "ConditionalTracepoints",
12750 "conditional-tracepoints", 0);
3788aec7
LM
12751
12752 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12753 "ConditionalBreakpoints",
12754 "conditional-breakpoints", 0);
12755
d3ce09f5
SS
12756 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12757 "BreakpointCommands",
12758 "breakpoint-commands", 0);
12759
7a697b8d
SS
12760 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12761 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12762
409873ef
SS
12763 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12764 "TracepointSource", "TracepointSource", 0);
12765
d914c394
SS
12766 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12767 "QAllow", "allow", 0);
12768
0fb4aa4b
PA
12769 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12770 "StaticTracepoints", "static-tracepoints", 0);
12771
1e4d1764
YQ
12772 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12773 "InstallInTrace", "install-in-trace", 0);
12774
0fb4aa4b
PA
12775 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12776 "qXfer:statictrace:read", "read-sdata-object", 0);
12777
78d85199
YQ
12778 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12779 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12780
03583c20
UW
12781 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12782 "QDisableRandomization", "disable-randomization", 0);
12783
d1feda86
YQ
12784 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12785 "QAgent", "agent", 0);
12786
f6f899bf
HAQ
12787 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12788 "QTBuffer:size", "trace-buffer-size", 0);
12789
9accd112
MM
12790 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12791 "Qbtrace:off", "disable-btrace", 0);
12792
12793 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12794 "Qbtrace:bts", "enable-btrace", 0);
12795
12796 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12797 "qXfer:btrace", "read-btrace", 0);
12798
f4abbc16
MM
12799 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12800 "qXfer:btrace-conf", "read-btrace-conf", 0);
12801
d33501a5
MM
12802 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12803 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12804
f7e6eed5
PA
12805 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12806 "swbreak-feature", "swbreak-feature", 0);
12807
12808 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12809 "hwbreak-feature", "hwbreak-feature", 0);
12810
89245bc0
DB
12811 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12812 "fork-event-feature", "fork-event-feature", 0);
12813
12814 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12815 "vfork-event-feature", "vfork-event-feature", 0);
12816
0b736949
DB
12817 /* Assert that we've registered "set remote foo-packet" commands
12818 for all packet configs. */
ca4f7f8b
PA
12819 {
12820 int i;
12821
12822 for (i = 0; i < PACKET_MAX; i++)
12823 {
12824 /* Ideally all configs would have a command associated. Some
12825 still don't though. */
12826 int excepted;
12827
12828 switch (i)
12829 {
12830 case PACKET_QNonStop:
12831 case PACKET_multiprocess_feature:
12832 case PACKET_EnableDisableTracepoints_feature:
12833 case PACKET_tracenz_feature:
12834 case PACKET_DisconnectedTracing_feature:
12835 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
12836 case PACKET_qCRC:
12837 /* Additions to this list need to be well justified:
12838 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
12839 excepted = 1;
12840 break;
12841 default:
12842 excepted = 0;
12843 break;
12844 }
12845
12846 /* This catches both forgetting to add a config command, and
12847 forgetting to remove a packet from the exception list. */
12848 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12849 }
12850 }
12851
37a105a1
DJ
12852 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12853 Z sub-packet has its own set and show commands, but users may
12854 have sets to this variable in their .gdbinit files (or in their
12855 documentation). */
e9e68a56 12856 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
12857 &remote_Z_packet_detect, _("\
12858Set use of remote protocol `Z' packets"), _("\
12859Show use of remote protocol `Z' packets "), _("\
3b64bf98 12860When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 12861packets."),
e9e68a56 12862 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
12863 show_remote_protocol_Z_packet_cmd,
12864 /* FIXME: i18n: Use of remote protocol
12865 `Z' packets is %s. */
e9e68a56 12866 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 12867
a6b151f1
DJ
12868 add_prefix_cmd ("remote", class_files, remote_command, _("\
12869Manipulate files on the remote system\n\
12870Transfer files to and from the remote target system."),
12871 &remote_cmdlist, "remote ",
12872 0 /* allow-unknown */, &cmdlist);
12873
12874 add_cmd ("put", class_files, remote_put_command,
12875 _("Copy a local file to the remote system."),
12876 &remote_cmdlist);
12877
12878 add_cmd ("get", class_files, remote_get_command,
12879 _("Copy a remote file to the local system."),
12880 &remote_cmdlist);
12881
12882 add_cmd ("delete", class_files, remote_delete_command,
12883 _("Delete a remote file."),
12884 &remote_cmdlist);
12885
2d717e4f
DJ
12886 remote_exec_file = xstrdup ("");
12887 add_setshow_string_noescape_cmd ("exec-file", class_files,
12888 &remote_exec_file, _("\
12889Set the remote pathname for \"run\""), _("\
12890Show the remote pathname for \"run\""), NULL, NULL, NULL,
12891 &remote_set_cmdlist, &remote_show_cmdlist);
12892
c1e36e3e
PA
12893 add_setshow_boolean_cmd ("range-stepping", class_run,
12894 &use_range_stepping, _("\
12895Enable or disable range stepping."), _("\
12896Show whether target-assisted range stepping is enabled."), _("\
12897If on, and the target supports it, when stepping a source line, GDB\n\
12898tells the target to step the corresponding range of addresses itself instead\n\
12899of issuing multiple single-steps. This speeds up source level\n\
12900stepping. If off, GDB always issues single-steps, even if range\n\
12901stepping is supported by the target. The default is on."),
12902 set_range_stepping,
12903 show_range_stepping,
12904 &setlist,
12905 &showlist);
12906
449092f6
CV
12907 /* Eventually initialize fileio. See fileio.c */
12908 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 12909
ba348170 12910 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 12911 special ptids with it set to != 0. */
ba348170
PA
12912 magic_null_ptid = ptid_build (42000, -1, 1);
12913 not_sent_ptid = ptid_build (42000, -2, 1);
12914 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
12915
12916 target_buf_size = 2048;
12917 target_buf = xmalloc (target_buf_size);
c906108c 12918}
10760264 12919
This page took 6.538808 seconds and 4 git commands to generate.