Turn on dwarf2 unwinding for Renesas RX architecture
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
32d0add0 3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b
JM
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b
JM
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c5aa993b 19
23860348 20/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 21
c906108c 22#include "defs.h"
c906108c
SS
23#include <ctype.h>
24#include <fcntl.h>
c906108c 25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "bfd.h"
28#include "symfile.h"
29#include "target.h"
c5aa993b 30/*#include "terminal.h" */
c906108c
SS
31#include "gdbcmd.h"
32#include "objfiles.h"
33#include "gdb-stabs.h"
34#include "gdbthread.h"
c2c6d25f 35#include "remote.h"
722247f1 36#include "remote-notif.h"
4e052eda 37#include "regcache.h"
fd0407d6 38#include "value.h"
6867ae3e 39#include "observer.h"
a77053c2 40#include "solib.h"
37a105a1
DJ
41#include "cli/cli-decode.h"
42#include "cli/cli-setshow.h"
424163ea 43#include "target-descriptions.h"
a4453b7e 44#include "gdb_bfd.h"
614c279d 45#include "filestuff.h"
9c3d6531 46#include "rsp-low.h"
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;
15a201c8
GB
376
377 /* The argument to the last "vFile:setfs:" packet we sent, used
378 to avoid sending repeated unnecessary "vFile:setfs:" packets.
379 Initialized to -1 to indicate that no "vFile:setfs:" packet
380 has yet been sent. */
381 int fs_pid;
ea9c271d
DJ
382};
383
dc146f7c
VP
384/* Private data that we'll store in (struct thread_info)->private. */
385struct private_thread_info
386{
387 char *extra;
388 int core;
389};
390
391static void
392free_private_thread_info (struct private_thread_info *info)
393{
394 xfree (info->extra);
395 xfree (info);
396}
397
ea9c271d
DJ
398/* This data could be associated with a target, but we do not always
399 have access to the current target when we need it, so for now it is
400 static. This will be fine for as long as only one target is in use
401 at a time. */
cf792862 402static struct remote_state *remote_state;
ea9c271d
DJ
403
404static struct remote_state *
0b83947e 405get_remote_state_raw (void)
ea9c271d 406{
cf792862
TT
407 return remote_state;
408}
409
410/* Allocate a new struct remote_state with xmalloc, initialize it, and
411 return it. */
412
413static struct remote_state *
414new_remote_state (void)
415{
416 struct remote_state *result = XCNEW (struct remote_state);
417
418 /* The default buffer size is unimportant; it will be expanded
419 whenever a larger buffer is needed. */
420 result->buf_size = 400;
421 result->buf = xmalloc (result->buf_size);
262e1174 422 result->remote_traceframe_number = -1;
b73be471 423 result->last_sent_signal = GDB_SIGNAL_0;
15a201c8 424 result->fs_pid = -1;
cf792862
TT
425
426 return result;
ea9c271d
DJ
427}
428
429/* Description of the remote protocol for a given architecture. */
d01949b6 430
ad10f812
AC
431struct packet_reg
432{
433 long offset; /* Offset into G packet. */
434 long regnum; /* GDB's internal register number. */
435 LONGEST pnum; /* Remote protocol register number. */
b323314b 436 int in_g_packet; /* Always part of G packet. */
f5656ead 437 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 438 at present. */
f5656ead 439 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 440 at present. */
ad10f812
AC
441};
442
ea9c271d 443struct remote_arch_state
d01949b6 444{
ad10f812
AC
445 /* Description of the remote protocol registers. */
446 long sizeof_g_packet;
b323314b
AC
447
448 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 449 (making an array gdbarch_num_regs in size). */
b323314b 450 struct packet_reg *regs;
ad10f812 451
d01949b6
AC
452 /* This is the size (in chars) of the first response to the ``g''
453 packet. It is used as a heuristic when determining the maximum
454 size of memory-read and memory-write packets. A target will
455 typically only reserve a buffer large enough to hold the ``g''
456 packet. The size does not include packet overhead (headers and
23860348 457 trailers). */
d01949b6
AC
458 long actual_register_packet_size;
459
460 /* This is the maximum size (in chars) of a non read/write packet.
23860348 461 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
462 long remote_packet_size;
463};
464
35b1e5cc
SS
465/* Utility: generate error from an incoming stub packet. */
466static void
467trace_error (char *buf)
468{
469 if (*buf++ != 'E')
470 return; /* not an error msg */
471 switch (*buf)
472 {
473 case '1': /* malformed packet error */
474 if (*++buf == '0') /* general case: */
475 error (_("remote.c: error in outgoing packet."));
476 else
477 error (_("remote.c: error in outgoing packet at field #%ld."),
478 strtol (buf, NULL, 16));
35b1e5cc
SS
479 default:
480 error (_("Target returns error code '%s'."), buf);
481 }
482}
483
484/* Utility: wait for reply from stub, while accepting "O" packets. */
485static char *
486remote_get_noisy_reply (char **buf_p,
487 long *sizeof_buf)
488{
489 do /* Loop on reply from remote stub. */
490 {
491 char *buf;
a744cf53 492
0df8b418 493 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
494 getpkt (buf_p, sizeof_buf, 0);
495 buf = *buf_p;
ad91cd99 496 if (buf[0] == 'E')
35b1e5cc 497 trace_error (buf);
61012eef 498 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
499 {
500 ULONGEST ul;
501 CORE_ADDR from, to, org_to;
502 char *p, *pp;
503 int adjusted_size = 0;
7556d4a4 504 int relocated = 0;
dde08ee1
PA
505
506 p = buf + strlen ("qRelocInsn:");
507 pp = unpack_varlen_hex (p, &ul);
508 if (*pp != ';')
cb91c06a 509 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
510 from = ul;
511
512 p = pp + 1;
a9cbf802 513 unpack_varlen_hex (p, &ul);
dde08ee1
PA
514 to = ul;
515
516 org_to = to;
517
492d29ea 518 TRY
dde08ee1 519 {
f5656ead 520 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 521 relocated = 1;
dde08ee1 522 }
492d29ea 523 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
524 {
525 if (ex.error == MEMORY_ERROR)
526 {
527 /* Propagate memory errors silently back to the
528 target. The stub may have limited the range of
529 addresses we can write to, for example. */
530 }
531 else
532 {
533 /* Something unexpectedly bad happened. Be verbose
534 so we can tell what, and propagate the error back
535 to the stub, so it doesn't get stuck waiting for
536 a response. */
537 exception_fprintf (gdb_stderr, ex,
538 _("warning: relocating instruction: "));
539 }
540 putpkt ("E01");
541 }
492d29ea 542 END_CATCH
7556d4a4
PA
543
544 if (relocated)
dde08ee1
PA
545 {
546 adjusted_size = to - org_to;
547
bba74b36 548 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
549 putpkt (buf);
550 }
dde08ee1 551 }
ad91cd99 552 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
553 remote_console_output (buf + 1); /* 'O' message from stub */
554 else
0df8b418 555 return buf; /* Here's the actual reply. */
35b1e5cc
SS
556 }
557 while (1);
558}
3c3bea1c 559
d01949b6
AC
560/* Handle for retreving the remote protocol data from gdbarch. */
561static struct gdbarch_data *remote_gdbarch_data_handle;
562
ea9c271d
DJ
563static struct remote_arch_state *
564get_remote_arch_state (void)
d01949b6 565{
f5656ead 566 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
567}
568
0b83947e
DJ
569/* Fetch the global remote target state. */
570
571static struct remote_state *
572get_remote_state (void)
573{
574 /* Make sure that the remote architecture state has been
575 initialized, because doing so might reallocate rs->buf. Any
576 function which calls getpkt also needs to be mindful of changes
577 to rs->buf, but this call limits the number of places which run
578 into trouble. */
579 get_remote_arch_state ();
580
581 return get_remote_state_raw ();
582}
583
74ca34ce
DJ
584static int
585compare_pnums (const void *lhs_, const void *rhs_)
586{
587 const struct packet_reg * const *lhs = lhs_;
588 const struct packet_reg * const *rhs = rhs_;
589
590 if ((*lhs)->pnum < (*rhs)->pnum)
591 return -1;
592 else if ((*lhs)->pnum == (*rhs)->pnum)
593 return 0;
594 else
595 return 1;
596}
597
c21236dc
PA
598static int
599map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 600{
74ca34ce 601 int regnum, num_remote_regs, offset;
74ca34ce 602 struct packet_reg **remote_regs;
ea9c271d 603
4a22f64d 604 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 605 {
c21236dc 606 struct packet_reg *r = &regs[regnum];
baef701f 607
4a22f64d 608 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
609 /* Do not try to fetch zero-sized (placeholder) registers. */
610 r->pnum = -1;
611 else
612 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
613
b323314b 614 r->regnum = regnum;
74ca34ce
DJ
615 }
616
617 /* Define the g/G packet format as the contents of each register
618 with a remote protocol number, in order of ascending protocol
619 number. */
620
4a22f64d 621 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 622 * sizeof (struct packet_reg *));
f57d151a 623 for (num_remote_regs = 0, regnum = 0;
4a22f64d 624 regnum < gdbarch_num_regs (gdbarch);
f57d151a 625 regnum++)
c21236dc
PA
626 if (regs[regnum].pnum != -1)
627 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 628
74ca34ce
DJ
629 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
630 compare_pnums);
631
632 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
633 {
634 remote_regs[regnum]->in_g_packet = 1;
635 remote_regs[regnum]->offset = offset;
4a22f64d 636 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
637 }
638
c21236dc
PA
639 return offset;
640}
641
642/* Given the architecture described by GDBARCH, return the remote
643 protocol register's number and the register's offset in the g/G
644 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
645 If the target does not have a mapping for REGNUM, return false,
646 otherwise, return true. */
647
648int
649remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
650 int *pnum, int *poffset)
651{
652 int sizeof_g_packet;
653 struct packet_reg *regs;
654 struct cleanup *old_chain;
655
656 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
657
658 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
659 old_chain = make_cleanup (xfree, regs);
660
661 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
662
663 *pnum = regs[regnum].pnum;
664 *poffset = regs[regnum].offset;
665
666 do_cleanups (old_chain);
667
668 return *pnum != -1;
669}
670
671static void *
672init_remote_state (struct gdbarch *gdbarch)
673{
674 struct remote_state *rs = get_remote_state_raw ();
675 struct remote_arch_state *rsa;
676
677 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
678
679 /* Use the architecture to build a regnum<->pnum table, which will be
680 1:1 unless a feature set specifies otherwise. */
681 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
682 gdbarch_num_regs (gdbarch),
683 struct packet_reg);
684
74ca34ce
DJ
685 /* Record the maximum possible size of the g packet - it may turn out
686 to be smaller. */
c21236dc 687 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 688
0df8b418 689 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
690 remote stubs have a hardwired buffer size of 400 bytes
691 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
692 as the maximum packet-size to ensure that the packet and an extra
693 NUL character can always fit in the buffer. This stops GDB
694 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
695 already a full buffer (As of 1999-12-04 that was most stubs). */
696 rsa->remote_packet_size = 400 - 1;
d01949b6 697
ea9c271d
DJ
698 /* This one is filled in when a ``g'' packet is received. */
699 rsa->actual_register_packet_size = 0;
700
701 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
702 default, adjust the size accordingly. Remember that each byte is
703 encoded as two characters. 32 is the overhead for the packet
704 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 705 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 706 little. */
ea9c271d
DJ
707 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
708 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 709
ea9c271d
DJ
710 /* Make sure that the packet buffer is plenty big enough for
711 this architecture. */
712 if (rs->buf_size < rsa->remote_packet_size)
713 {
714 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 715 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 716 }
6d820c5c 717
ea9c271d
DJ
718 return rsa;
719}
720
721/* Return the current allowed size of a remote packet. This is
722 inferred from the current architecture, and should be used to
723 limit the length of outgoing packets. */
724static long
725get_remote_packet_size (void)
726{
be2a5f71 727 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
728 struct remote_arch_state *rsa = get_remote_arch_state ();
729
be2a5f71
DJ
730 if (rs->explicit_packet_size)
731 return rs->explicit_packet_size;
732
ea9c271d 733 return rsa->remote_packet_size;
d01949b6
AC
734}
735
ad10f812 736static struct packet_reg *
ea9c271d 737packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 738{
f5656ead 739 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
740 return NULL;
741 else
ad10f812 742 {
ea9c271d 743 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 744
b323314b
AC
745 gdb_assert (r->regnum == regnum);
746 return r;
ad10f812 747 }
ad10f812
AC
748}
749
750static struct packet_reg *
ea9c271d 751packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 752{
b323314b 753 int i;
a744cf53 754
f5656ead 755 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 756 {
ea9c271d 757 struct packet_reg *r = &rsa->regs[i];
a744cf53 758
b323314b
AC
759 if (r->pnum == pnum)
760 return r;
ad10f812
AC
761 }
762 return NULL;
d01949b6
AC
763}
764
c906108c
SS
765static struct target_ops remote_ops;
766
767static struct target_ops extended_remote_ops;
768
6426a772
JM
769/* FIXME: cagney/1999-09-23: Even though getpkt was called with
770 ``forever'' still use the normal timeout mechanism. This is
771 currently used by the ASYNC code to guarentee that target reads
772 during the initial connect always time-out. Once getpkt has been
773 modified to return a timeout indication and, in turn
774 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 775 this can go away. */
6426a772
JM
776static int wait_forever_enabled_p = 1;
777
9a7071a8
JB
778/* Allow the user to specify what sequence to send to the remote
779 when he requests a program interruption: Although ^C is usually
780 what remote systems expect (this is the default, here), it is
781 sometimes preferable to send a break. On other systems such
782 as the Linux kernel, a break followed by g, which is Magic SysRq g
783 is required in order to interrupt the execution. */
784const char interrupt_sequence_control_c[] = "Ctrl-C";
785const char interrupt_sequence_break[] = "BREAK";
786const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 787static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
788 {
789 interrupt_sequence_control_c,
790 interrupt_sequence_break,
791 interrupt_sequence_break_g,
792 NULL
793 };
794static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
795
796static void
797show_interrupt_sequence (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c,
799 const char *value)
800{
801 if (interrupt_sequence_mode == interrupt_sequence_control_c)
802 fprintf_filtered (file,
803 _("Send the ASCII ETX character (Ctrl-c) "
804 "to the remote target to interrupt the "
805 "execution of the program.\n"));
806 else if (interrupt_sequence_mode == interrupt_sequence_break)
807 fprintf_filtered (file,
808 _("send a break signal to the remote target "
809 "to interrupt the execution of the program.\n"));
810 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
811 fprintf_filtered (file,
812 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
813 "the remote target to interrupt the execution "
814 "of Linux kernel.\n"));
815 else
816 internal_error (__FILE__, __LINE__,
817 _("Invalid value for interrupt_sequence_mode: %s."),
818 interrupt_sequence_mode);
819}
6426a772 820
9a7071a8
JB
821/* This boolean variable specifies whether interrupt_sequence is sent
822 to the remote target when gdb connects to it.
823 This is mostly needed when you debug the Linux kernel: The Linux kernel
824 expects BREAK g which is Magic SysRq g for connecting gdb. */
825static int interrupt_on_connect = 0;
c906108c 826
9a7071a8
JB
827/* This variable is used to implement the "set/show remotebreak" commands.
828 Since these commands are now deprecated in favor of "set/show remote
829 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
830static int remote_break;
831
9a7071a8
JB
832static void
833set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
834{
835 if (remote_break)
836 interrupt_sequence_mode = interrupt_sequence_break;
837 else
838 interrupt_sequence_mode = interrupt_sequence_control_c;
839}
840
841static void
842show_remotebreak (struct ui_file *file, int from_tty,
843 struct cmd_list_element *c,
844 const char *value)
845{
846}
847
c906108c
SS
848/* This variable sets the number of bits in an address that are to be
849 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 850 leading zeros, the entire address would be sent. This variable
c906108c
SS
851 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
852 initial implementation of remote.c restricted the address sent in
853 memory packets to ``host::sizeof long'' bytes - (typically 32
854 bits). Consequently, for 64 bit targets, the upper 32 bits of an
855 address was never sent. Since fixing this bug may cause a break in
856 some remote targets this variable is principly provided to
23860348 857 facilitate backward compatibility. */
c906108c 858
883b9c6c 859static unsigned int remote_address_size;
c906108c 860
75c99385
PA
861/* Temporary to track who currently owns the terminal. See
862 remote_terminal_* for more details. */
6426a772
JM
863
864static int remote_async_terminal_ours_p;
865
2d717e4f
DJ
866/* The executable file to use for "run" on the remote side. */
867
868static char *remote_exec_file = "";
869
11cf8741 870\f
11cf8741 871/* User configurable variables for the number of characters in a
ea9c271d
DJ
872 memory read/write packet. MIN (rsa->remote_packet_size,
873 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 874 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
875 (speed up transfers). The variables ``preferred_*'' (the user
876 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 877 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
878
879struct memory_packet_config
880{
881 char *name;
882 long size;
883 int fixed_p;
884};
885
886/* Compute the current size of a read/write packet. Since this makes
887 use of ``actual_register_packet_size'' the computation is dynamic. */
888
889static long
890get_memory_packet_size (struct memory_packet_config *config)
891{
d01949b6 892 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
893 struct remote_arch_state *rsa = get_remote_arch_state ();
894
11cf8741
JM
895 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
896 law?) that some hosts don't cope very well with large alloca()
897 calls. Eventually the alloca() code will be replaced by calls to
898 xmalloc() and make_cleanups() allowing this restriction to either
23860348 899 be lifted or removed. */
11cf8741
JM
900#ifndef MAX_REMOTE_PACKET_SIZE
901#define MAX_REMOTE_PACKET_SIZE 16384
902#endif
3de11b2e 903 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 904#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 905#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
906#endif
907 long what_they_get;
908 if (config->fixed_p)
909 {
910 if (config->size <= 0)
911 what_they_get = MAX_REMOTE_PACKET_SIZE;
912 else
913 what_they_get = config->size;
914 }
915 else
916 {
ea9c271d 917 what_they_get = get_remote_packet_size ();
23860348 918 /* Limit the packet to the size specified by the user. */
11cf8741
JM
919 if (config->size > 0
920 && what_they_get > config->size)
921 what_they_get = config->size;
be2a5f71
DJ
922
923 /* Limit it to the size of the targets ``g'' response unless we have
924 permission from the stub to use a larger packet size. */
925 if (rs->explicit_packet_size == 0
926 && rsa->actual_register_packet_size > 0
927 && what_they_get > rsa->actual_register_packet_size)
928 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
929 }
930 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
931 what_they_get = MAX_REMOTE_PACKET_SIZE;
932 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
933 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
934
935 /* Make sure there is room in the global buffer for this packet
936 (including its trailing NUL byte). */
937 if (rs->buf_size < what_they_get + 1)
938 {
939 rs->buf_size = 2 * what_they_get;
940 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
941 }
942
11cf8741
JM
943 return what_they_get;
944}
945
0df8b418 946/* Update the size of a read/write packet. If they user wants
23860348 947 something really big then do a sanity check. */
11cf8741
JM
948
949static void
950set_memory_packet_size (char *args, struct memory_packet_config *config)
951{
952 int fixed_p = config->fixed_p;
953 long size = config->size;
a744cf53 954
11cf8741 955 if (args == NULL)
8a3fe4f8 956 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
957 else if (strcmp (args, "hard") == 0
958 || strcmp (args, "fixed") == 0)
959 fixed_p = 1;
960 else if (strcmp (args, "soft") == 0
961 || strcmp (args, "limit") == 0)
962 fixed_p = 0;
963 else
964 {
965 char *end;
a744cf53 966
11cf8741
JM
967 size = strtoul (args, &end, 0);
968 if (args == end)
8a3fe4f8 969 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
970#if 0
971 /* Instead of explicitly capping the size of a packet to
972 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
973 instead allowed to set the size to something arbitrarily
23860348 974 large. */
11cf8741 975 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 976 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
977#endif
978 }
23860348 979 /* Extra checks? */
11cf8741
JM
980 if (fixed_p && !config->fixed_p)
981 {
e2e0b3e5
AC
982 if (! query (_("The target may not be able to correctly handle a %s\n"
983 "of %ld bytes. Change the packet size? "),
11cf8741 984 config->name, size))
8a3fe4f8 985 error (_("Packet size not changed."));
11cf8741 986 }
23860348 987 /* Update the config. */
11cf8741
JM
988 config->fixed_p = fixed_p;
989 config->size = size;
990}
991
992static void
993show_memory_packet_size (struct memory_packet_config *config)
994{
a3f17187 995 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 996 if (config->fixed_p)
a3f17187 997 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
998 get_memory_packet_size (config));
999 else
a3f17187 1000 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1001 get_memory_packet_size (config));
1002}
1003
1004static struct memory_packet_config memory_write_packet_config =
1005{
1006 "memory-write-packet-size",
1007};
1008
1009static void
1010set_memory_write_packet_size (char *args, int from_tty)
1011{
1012 set_memory_packet_size (args, &memory_write_packet_config);
1013}
1014
1015static void
1016show_memory_write_packet_size (char *args, int from_tty)
1017{
1018 show_memory_packet_size (&memory_write_packet_config);
1019}
1020
1021static long
1022get_memory_write_packet_size (void)
1023{
1024 return get_memory_packet_size (&memory_write_packet_config);
1025}
1026
1027static struct memory_packet_config memory_read_packet_config =
1028{
1029 "memory-read-packet-size",
1030};
1031
1032static void
1033set_memory_read_packet_size (char *args, int from_tty)
1034{
1035 set_memory_packet_size (args, &memory_read_packet_config);
1036}
1037
1038static void
1039show_memory_read_packet_size (char *args, int from_tty)
1040{
1041 show_memory_packet_size (&memory_read_packet_config);
1042}
1043
1044static long
1045get_memory_read_packet_size (void)
1046{
1047 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1048
11cf8741
JM
1049 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1050 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1051 increased beyond this. */
1052 if (size > get_remote_packet_size ())
1053 size = get_remote_packet_size ();
11cf8741
JM
1054 return size;
1055}
1056
11cf8741 1057\f
5a2468f5 1058/* Generic configuration support for packets the stub optionally
0df8b418 1059 supports. Allows the user to specify the use of the packet as well
23860348 1060 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1061
1062enum packet_support
1063 {
1064 PACKET_SUPPORT_UNKNOWN = 0,
1065 PACKET_ENABLE,
1066 PACKET_DISABLE
1067 };
1068
5a2468f5
JM
1069struct packet_config
1070 {
bb572ddd
DJ
1071 const char *name;
1072 const char *title;
4082afcc
PA
1073
1074 /* If auto, GDB auto-detects support for this packet or feature,
1075 either through qSupported, or by trying the packet and looking
1076 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1077 packet. If false, the packet is disabled. Configs that don't
1078 have an associated command always have this set to auto. */
7f19b9a2 1079 enum auto_boolean detect;
4082afcc
PA
1080
1081 /* Does the target support this packet? */
5a2468f5
JM
1082 enum packet_support support;
1083 };
1084
d471ea57 1085/* Analyze a packet's return value and update the packet config
23860348 1086 accordingly. */
d471ea57
AC
1087
1088enum packet_result
1089{
1090 PACKET_ERROR,
1091 PACKET_OK,
1092 PACKET_UNKNOWN
1093};
1094
4082afcc
PA
1095static enum packet_support packet_config_support (struct packet_config *config);
1096static enum packet_support packet_support (int packet);
5a2468f5
JM
1097
1098static void
fba45db2 1099show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1100{
1101 char *support = "internal-error";
a744cf53 1102
4082afcc 1103 switch (packet_config_support (config))
5a2468f5
JM
1104 {
1105 case PACKET_ENABLE:
1106 support = "enabled";
1107 break;
1108 case PACKET_DISABLE:
1109 support = "disabled";
1110 break;
1111 case PACKET_SUPPORT_UNKNOWN:
1112 support = "unknown";
1113 break;
1114 }
1115 switch (config->detect)
1116 {
7f19b9a2 1117 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1118 printf_filtered (_("Support for the `%s' packet "
1119 "is auto-detected, currently %s.\n"),
37a105a1 1120 config->name, support);
5a2468f5 1121 break;
7f19b9a2
AC
1122 case AUTO_BOOLEAN_TRUE:
1123 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1124 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1125 config->name, support);
8e248173 1126 break;
5a2468f5
JM
1127 }
1128}
1129
1130static void
bb572ddd
DJ
1131add_packet_config_cmd (struct packet_config *config, const char *name,
1132 const char *title, int legacy)
d471ea57 1133{
5a2468f5
JM
1134 char *set_doc;
1135 char *show_doc;
d471ea57 1136 char *cmd_name;
3ed07be4 1137
5a2468f5
JM
1138 config->name = name;
1139 config->title = title;
b435e160
AC
1140 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1141 name, title);
3e43a32a
MS
1142 show_doc = xstrprintf ("Show current use of remote "
1143 "protocol `%s' (%s) packet",
b435e160 1144 name, title);
d471ea57 1145 /* set/show TITLE-packet {auto,on,off} */
b435e160 1146 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1147 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1148 &config->detect, set_doc,
1149 show_doc, NULL, /* help_doc */
4082afcc 1150 NULL,
bb572ddd
DJ
1151 show_remote_protocol_packet_cmd,
1152 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1153 /* The command code copies the documentation strings. */
1154 xfree (set_doc);
1155 xfree (show_doc);
23860348 1156 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1157 if (legacy)
1158 {
1159 char *legacy_name;
a744cf53 1160
b435e160 1161 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1162 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1163 &remote_set_cmdlist);
d471ea57 1164 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1165 &remote_show_cmdlist);
d471ea57 1166 }
5a2468f5
JM
1167}
1168
d471ea57 1169static enum packet_result
a76d924d 1170packet_check_result (const char *buf)
5a2468f5 1171{
d471ea57 1172 if (buf[0] != '\0')
5a2468f5 1173 {
d471ea57 1174 /* The stub recognized the packet request. Check that the
23860348 1175 operation succeeded. */
a76d924d
DJ
1176 if (buf[0] == 'E'
1177 && isxdigit (buf[1]) && isxdigit (buf[2])
1178 && buf[3] == '\0')
1179 /* "Enn" - definitly an error. */
1180 return PACKET_ERROR;
1181
1182 /* Always treat "E." as an error. This will be used for
1183 more verbose error messages, such as E.memtypes. */
1184 if (buf[0] == 'E' && buf[1] == '.')
1185 return PACKET_ERROR;
1186
1187 /* The packet may or may not be OK. Just assume it is. */
1188 return PACKET_OK;
1189 }
1190 else
1191 /* The stub does not support the packet. */
1192 return PACKET_UNKNOWN;
1193}
1194
1195static enum packet_result
1196packet_ok (const char *buf, struct packet_config *config)
1197{
1198 enum packet_result result;
1199
4082afcc
PA
1200 if (config->detect != AUTO_BOOLEAN_TRUE
1201 && config->support == PACKET_DISABLE)
1202 internal_error (__FILE__, __LINE__,
1203 _("packet_ok: attempt to use a disabled packet"));
1204
a76d924d
DJ
1205 result = packet_check_result (buf);
1206 switch (result)
1207 {
1208 case PACKET_OK:
1209 case PACKET_ERROR:
1210 /* The stub recognized the packet request. */
4082afcc 1211 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1212 {
d471ea57
AC
1213 if (remote_debug)
1214 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1215 "Packet %s (%s) is supported\n",
1216 config->name, config->title);
d471ea57 1217 config->support = PACKET_ENABLE;
d471ea57 1218 }
a76d924d
DJ
1219 break;
1220 case PACKET_UNKNOWN:
23860348 1221 /* The stub does not support the packet. */
4082afcc
PA
1222 if (config->detect == AUTO_BOOLEAN_AUTO
1223 && config->support == PACKET_ENABLE)
d471ea57 1224 {
4082afcc
PA
1225 /* If the stub previously indicated that the packet was
1226 supported then there is a protocol error. */
1227 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1228 config->name, config->title);
1229 }
1230 else if (config->detect == AUTO_BOOLEAN_TRUE)
1231 {
1232 /* The user set it wrong. */
1233 error (_("Enabled packet %s (%s) not recognized by stub"),
1234 config->name, config->title);
d471ea57 1235 }
4082afcc
PA
1236
1237 if (remote_debug)
1238 fprintf_unfiltered (gdb_stdlog,
1239 "Packet %s (%s) is NOT supported\n",
1240 config->name, config->title);
1241 config->support = PACKET_DISABLE;
a76d924d 1242 break;
5a2468f5 1243 }
a76d924d
DJ
1244
1245 return result;
5a2468f5
JM
1246}
1247
444abaca
DJ
1248enum {
1249 PACKET_vCont = 0,
1250 PACKET_X,
1251 PACKET_qSymbol,
1252 PACKET_P,
1253 PACKET_p,
1254 PACKET_Z0,
1255 PACKET_Z1,
1256 PACKET_Z2,
1257 PACKET_Z3,
1258 PACKET_Z4,
15a201c8 1259 PACKET_vFile_setfs,
a6b151f1
DJ
1260 PACKET_vFile_open,
1261 PACKET_vFile_pread,
1262 PACKET_vFile_pwrite,
1263 PACKET_vFile_close,
1264 PACKET_vFile_unlink,
b9e7b9c3 1265 PACKET_vFile_readlink,
0a93529c 1266 PACKET_vFile_fstat,
0876f84a 1267 PACKET_qXfer_auxv,
23181151 1268 PACKET_qXfer_features,
c78fa86a 1269 PACKET_qXfer_exec_file,
cfa9d6d9 1270 PACKET_qXfer_libraries,
2268b414 1271 PACKET_qXfer_libraries_svr4,
fd79ecee 1272 PACKET_qXfer_memory_map,
0e7f50da
UW
1273 PACKET_qXfer_spu_read,
1274 PACKET_qXfer_spu_write,
07e059b5 1275 PACKET_qXfer_osdata,
dc146f7c 1276 PACKET_qXfer_threads,
0fb4aa4b 1277 PACKET_qXfer_statictrace_read,
b3b9301e 1278 PACKET_qXfer_traceframe_info,
169081d0 1279 PACKET_qXfer_uib,
711e434b 1280 PACKET_qGetTIBAddr,
444abaca 1281 PACKET_qGetTLSAddr,
be2a5f71 1282 PACKET_qSupported,
bd3eecc3 1283 PACKET_qTStatus,
89be2091 1284 PACKET_QPassSignals,
9b224c5e 1285 PACKET_QProgramSignals,
936d2992 1286 PACKET_qCRC,
08388c79 1287 PACKET_qSearch_memory,
2d717e4f
DJ
1288 PACKET_vAttach,
1289 PACKET_vRun,
a6f3e723 1290 PACKET_QStartNoAckMode,
82f73884 1291 PACKET_vKill,
4aa995e1
PA
1292 PACKET_qXfer_siginfo_read,
1293 PACKET_qXfer_siginfo_write,
0b16c5cf 1294 PACKET_qAttached,
4082afcc
PA
1295
1296 /* Support for conditional tracepoints. */
782b2b07 1297 PACKET_ConditionalTracepoints,
4082afcc
PA
1298
1299 /* Support for target-side breakpoint conditions. */
3788aec7 1300 PACKET_ConditionalBreakpoints,
4082afcc
PA
1301
1302 /* Support for target-side breakpoint commands. */
d3ce09f5 1303 PACKET_BreakpointCommands,
4082afcc
PA
1304
1305 /* Support for fast tracepoints. */
7a697b8d 1306 PACKET_FastTracepoints,
4082afcc
PA
1307
1308 /* Support for static tracepoints. */
0fb4aa4b 1309 PACKET_StaticTracepoints,
4082afcc
PA
1310
1311 /* Support for installing tracepoints while a trace experiment is
1312 running. */
1e4d1764 1313 PACKET_InstallInTrace,
4082afcc 1314
40ab02ce
MS
1315 PACKET_bc,
1316 PACKET_bs,
409873ef 1317 PACKET_TracepointSource,
d914c394 1318 PACKET_QAllow,
78d85199 1319 PACKET_qXfer_fdpic,
03583c20 1320 PACKET_QDisableRandomization,
d1feda86 1321 PACKET_QAgent,
f6f899bf 1322 PACKET_QTBuffer_size,
9accd112
MM
1323 PACKET_Qbtrace_off,
1324 PACKET_Qbtrace_bts,
1325 PACKET_qXfer_btrace,
4082afcc
PA
1326
1327 /* Support for the QNonStop packet. */
1328 PACKET_QNonStop,
1329
1330 /* Support for multi-process extensions. */
1331 PACKET_multiprocess_feature,
1332
1333 /* Support for enabling and disabling tracepoints while a trace
1334 experiment is running. */
1335 PACKET_EnableDisableTracepoints_feature,
1336
1337 /* Support for collecting strings using the tracenz bytecode. */
1338 PACKET_tracenz_feature,
1339
1340 /* Support for continuing to run a trace experiment while GDB is
1341 disconnected. */
1342 PACKET_DisconnectedTracing_feature,
1343
1344 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1345 PACKET_augmented_libraries_svr4_read_feature,
1346
f4abbc16
MM
1347 /* Support for the qXfer:btrace-conf:read packet. */
1348 PACKET_qXfer_btrace_conf,
1349
d33501a5
MM
1350 /* Support for the Qbtrace-conf:bts:size packet. */
1351 PACKET_Qbtrace_conf_bts_size,
1352
f7e6eed5
PA
1353 /* Support for swbreak+ feature. */
1354 PACKET_swbreak_feature,
1355
1356 /* Support for hwbreak+ feature. */
1357 PACKET_hwbreak_feature,
1358
89245bc0
DB
1359 /* Support for fork events. */
1360 PACKET_fork_event_feature,
1361
1362 /* Support for vfork events. */
1363 PACKET_vfork_event_feature,
1364
444abaca
DJ
1365 PACKET_MAX
1366};
506fb367 1367
444abaca 1368static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1369
f7e6eed5
PA
1370/* Returns the packet's corresponding "set remote foo-packet" command
1371 state. See struct packet_config for more details. */
1372
1373static enum auto_boolean
1374packet_set_cmd_state (int packet)
1375{
1376 return remote_protocol_packets[packet].detect;
1377}
1378
4082afcc
PA
1379/* Returns whether a given packet or feature is supported. This takes
1380 into account the state of the corresponding "set remote foo-packet"
1381 command, which may be used to bypass auto-detection. */
dc8acb97 1382
4082afcc
PA
1383static enum packet_support
1384packet_config_support (struct packet_config *config)
1385{
1386 switch (config->detect)
444abaca 1387 {
4082afcc
PA
1388 case AUTO_BOOLEAN_TRUE:
1389 return PACKET_ENABLE;
1390 case AUTO_BOOLEAN_FALSE:
1391 return PACKET_DISABLE;
1392 case AUTO_BOOLEAN_AUTO:
1393 return config->support;
1394 default:
1395 gdb_assert_not_reached (_("bad switch"));
444abaca 1396 }
4082afcc
PA
1397}
1398
1399/* Same as packet_config_support, but takes the packet's enum value as
1400 argument. */
1401
1402static enum packet_support
1403packet_support (int packet)
1404{
1405 struct packet_config *config = &remote_protocol_packets[packet];
1406
1407 return packet_config_support (config);
dc8acb97
MS
1408}
1409
5a2468f5 1410static void
444abaca
DJ
1411show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1412 struct cmd_list_element *c,
1413 const char *value)
5a2468f5 1414{
444abaca 1415 struct packet_config *packet;
5a2468f5 1416
444abaca
DJ
1417 for (packet = remote_protocol_packets;
1418 packet < &remote_protocol_packets[PACKET_MAX];
1419 packet++)
1420 {
1421 if (&packet->detect == c->var)
1422 {
1423 show_packet_config_cmd (packet);
1424 return;
1425 }
1426 }
9b20d036 1427 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1428 c->name);
5a2468f5
JM
1429}
1430
d471ea57
AC
1431/* Should we try one of the 'Z' requests? */
1432
1433enum Z_packet_type
1434{
1435 Z_PACKET_SOFTWARE_BP,
1436 Z_PACKET_HARDWARE_BP,
1437 Z_PACKET_WRITE_WP,
1438 Z_PACKET_READ_WP,
1439 Z_PACKET_ACCESS_WP,
1440 NR_Z_PACKET_TYPES
1441};
96baa820 1442
d471ea57 1443/* For compatibility with older distributions. Provide a ``set remote
23860348 1444 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1445
7f19b9a2 1446static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1447
1448static void
fba45db2
KB
1449set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1450 struct cmd_list_element *c)
96baa820 1451{
d471ea57 1452 int i;
a744cf53 1453
d471ea57 1454 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1455 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1456}
1457
1458static void
08546159
AC
1459show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1460 struct cmd_list_element *c,
1461 const char *value)
96baa820 1462{
d471ea57 1463 int i;
a744cf53 1464
d471ea57
AC
1465 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1466 {
444abaca 1467 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1468 }
96baa820
JM
1469}
1470
4082afcc
PA
1471/* Returns true if the multi-process extensions are in effect. */
1472
1473static int
1474remote_multi_process_p (struct remote_state *rs)
1475{
1476 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1477}
1478
de0d863e
DB
1479/* Returns true if fork events are supported. */
1480
1481static int
1482remote_fork_event_p (struct remote_state *rs)
1483{
1484 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1485}
1486
c269dbdb
DB
1487/* Returns true if vfork events are supported. */
1488
1489static int
1490remote_vfork_event_p (struct remote_state *rs)
1491{
1492 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1493}
1494
cbb8991c
DB
1495/* Insert fork catchpoint target routine. If fork events are enabled
1496 then return success, nothing more to do. */
1497
1498static int
1499remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1500{
1501 struct remote_state *rs = get_remote_state ();
1502
1503 return !remote_fork_event_p (rs);
1504}
1505
1506/* Remove fork catchpoint target routine. Nothing to do, just
1507 return success. */
1508
1509static int
1510remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1511{
1512 return 0;
1513}
1514
1515/* Insert vfork catchpoint target routine. If vfork events are enabled
1516 then return success, nothing more to do. */
1517
1518static int
1519remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1520{
1521 struct remote_state *rs = get_remote_state ();
1522
1523 return !remote_vfork_event_p (rs);
1524}
1525
1526/* Remove vfork catchpoint target routine. Nothing to do, just
1527 return success. */
1528
1529static int
1530remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1531{
1532 return 0;
1533}
1534
23860348 1535/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1536static struct async_signal_handler *async_sigint_remote_twice_token;
1537static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1538
74531fed
PA
1539\f
1540/* Asynchronous signal handle registered as event loop source for
1541 when we have pending events ready to be passed to the core. */
1542
1543static struct async_event_handler *remote_async_inferior_event_token;
1544
c906108c
SS
1545\f
1546
79d7f229
PA
1547static ptid_t magic_null_ptid;
1548static ptid_t not_sent_ptid;
1549static ptid_t any_thread_ptid;
1550
0b16c5cf
PA
1551/* Find out if the stub attached to PID (and hence GDB should offer to
1552 detach instead of killing it when bailing out). */
1553
1554static int
1555remote_query_attached (int pid)
1556{
1557 struct remote_state *rs = get_remote_state ();
bba74b36 1558 size_t size = get_remote_packet_size ();
0b16c5cf 1559
4082afcc 1560 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1561 return 0;
1562
1563 if (remote_multi_process_p (rs))
bba74b36 1564 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1565 else
bba74b36 1566 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1567
1568 putpkt (rs->buf);
1569 getpkt (&rs->buf, &rs->buf_size, 0);
1570
1571 switch (packet_ok (rs->buf,
1554e9be 1572 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1573 {
1574 case PACKET_OK:
1575 if (strcmp (rs->buf, "1") == 0)
1576 return 1;
1577 break;
1578 case PACKET_ERROR:
1579 warning (_("Remote failure reply: %s"), rs->buf);
1580 break;
1581 case PACKET_UNKNOWN:
1582 break;
1583 }
1584
1585 return 0;
1586}
1587
49c62f2e
PA
1588/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1589 has been invented by GDB, instead of reported by the target. Since
1590 we can be connected to a remote system before before knowing about
1591 any inferior, mark the target with execution when we find the first
1592 inferior. If ATTACHED is 1, then we had just attached to this
1593 inferior. If it is 0, then we just created this inferior. If it
1594 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1595 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1596 attempt to open this inferior's executable as the main executable
1597 if no main executable is open already. */
1941c569
PA
1598
1599static struct inferior *
1b6e6f5c
GB
1600remote_add_inferior (int fake_pid_p, int pid, int attached,
1601 int try_open_exec)
1941c569 1602{
1941c569
PA
1603 struct inferior *inf;
1604
0b16c5cf
PA
1605 /* Check whether this process we're learning about is to be
1606 considered attached, or if is to be considered to have been
1607 spawned by the stub. */
1608 if (attached == -1)
1609 attached = remote_query_attached (pid);
1610
f5656ead 1611 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1612 {
1613 /* If the target shares code across all inferiors, then every
1614 attach adds a new inferior. */
1615 inf = add_inferior (pid);
1616
1617 /* ... and every inferior is bound to the same program space.
1618 However, each inferior may still have its own address
1619 space. */
1620 inf->aspace = maybe_new_address_space ();
1621 inf->pspace = current_program_space;
1622 }
1623 else
1624 {
1625 /* In the traditional debugging scenario, there's a 1-1 match
1626 between program/address spaces. We simply bind the inferior
1627 to the program space's address space. */
1628 inf = current_inferior ();
1629 inferior_appeared (inf, pid);
1630 }
1941c569 1631
0b16c5cf 1632 inf->attach_flag = attached;
49c62f2e 1633 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1634
1b6e6f5c
GB
1635 /* If no main executable is currently open then attempt to
1636 open the file that was executed to create this inferior. */
835205d0 1637 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1638 exec_file_locate_attach (pid, 1);
1639
1941c569
PA
1640 return inf;
1641}
1642
1643/* Add thread PTID to GDB's thread list. Tag it as executing/running
1644 according to RUNNING. */
1645
c906108c 1646static void
1941c569 1647remote_add_thread (ptid_t ptid, int running)
c906108c 1648{
b7ea362b
PA
1649 struct remote_state *rs = get_remote_state ();
1650
1651 /* GDB historically didn't pull threads in the initial connection
1652 setup. If the remote target doesn't even have a concept of
1653 threads (e.g., a bare-metal target), even if internally we
1654 consider that a single-threaded target, mentioning a new thread
1655 might be confusing to the user. Be silent then, preserving the
1656 age old behavior. */
1657 if (rs->starting_up)
1658 add_thread_silent (ptid);
1659 else
1660 add_thread (ptid);
1941c569
PA
1661
1662 set_executing (ptid, running);
1663 set_running (ptid, running);
1664}
1665
1666/* Come here when we learn about a thread id from the remote target.
1667 It may be the first time we hear about such thread, so take the
1668 opportunity to add it to GDB's thread list. In case this is the
1669 first time we're noticing its corresponding inferior, add it to
1670 GDB's inferior list as well. */
1671
1672static void
1673remote_notice_new_inferior (ptid_t currthread, int running)
1674{
c906108c
SS
1675 /* If this is a new thread, add it to GDB's thread list.
1676 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1677
1678 if (in_thread_list (currthread) && is_exited (currthread))
1679 {
1680 /* We're seeing an event on a thread id we knew had exited.
1681 This has to be a new thread reusing the old id. Add it. */
1941c569 1682 remote_add_thread (currthread, running);
82f73884
PA
1683 return;
1684 }
1685
79d7f229 1686 if (!in_thread_list (currthread))
c0a2216e 1687 {
1941c569 1688 struct inferior *inf = NULL;
bad34192 1689 int pid = ptid_get_pid (currthread);
1941c569 1690
bad34192
PA
1691 if (ptid_is_pid (inferior_ptid)
1692 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1693 {
1694 /* inferior_ptid has no thread member yet. This can happen
1695 with the vAttach -> remote_wait,"TAAthread:" path if the
1696 stub doesn't support qC. This is the first stop reported
1697 after an attach, so this is the main thread. Update the
1698 ptid in the thread list. */
bad34192
PA
1699 if (in_thread_list (pid_to_ptid (pid)))
1700 thread_change_ptid (inferior_ptid, currthread);
1701 else
1702 {
1703 remote_add_thread (currthread, running);
1704 inferior_ptid = currthread;
1705 }
dc146f7c 1706 return;
c0a2216e 1707 }
82f73884
PA
1708
1709 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1710 {
1711 /* inferior_ptid is not set yet. This can happen with the
1712 vRun -> remote_wait,"TAAthread:" path if the stub
1713 doesn't support qC. This is the first stop reported
1714 after an attach, so this is the main thread. Update the
1715 ptid in the thread list. */
dc146f7c 1716 thread_change_ptid (inferior_ptid, currthread);
82f73884 1717 return;
c0a2216e 1718 }
82f73884 1719
29c87f7f
PA
1720 /* When connecting to a target remote, or to a target
1721 extended-remote which already was debugging an inferior, we
1722 may not know about it yet. Add it before adding its child
1723 thread, so notifications are emitted in a sensible order. */
1724 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1725 {
1726 struct remote_state *rs = get_remote_state ();
1727 int fake_pid_p = !remote_multi_process_p (rs);
1728
1729 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1730 ptid_get_pid (currthread), -1, 1);
49c62f2e 1731 }
29c87f7f 1732
82f73884 1733 /* This is really a new thread. Add it. */
1941c569
PA
1734 remote_add_thread (currthread, running);
1735
1736 /* If we found a new inferior, let the common code do whatever
1737 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1738 breakpoints), unless we're just setting up an all-stop
1739 connection. */
1941c569 1740 if (inf != NULL)
b7ea362b
PA
1741 {
1742 struct remote_state *rs = get_remote_state ();
1743
1744 if (non_stop || !rs->starting_up)
1745 notice_new_inferior (currthread, running, 0);
1746 }
c0a2216e 1747 }
c906108c
SS
1748}
1749
dc146f7c
VP
1750/* Return the private thread data, creating it if necessary. */
1751
70221824 1752static struct private_thread_info *
dc146f7c
VP
1753demand_private_info (ptid_t ptid)
1754{
1755 struct thread_info *info = find_thread_ptid (ptid);
1756
1757 gdb_assert (info);
1758
fe978cb0 1759 if (!info->priv)
dc146f7c 1760 {
fe978cb0 1761 info->priv = xmalloc (sizeof (*(info->priv)));
dc146f7c 1762 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1763 info->priv->core = -1;
1764 info->priv->extra = 0;
dc146f7c
VP
1765 }
1766
fe978cb0 1767 return info->priv;
dc146f7c
VP
1768}
1769
74531fed
PA
1770/* Call this function as a result of
1771 1) A halt indication (T packet) containing a thread id
1772 2) A direct query of currthread
0df8b418 1773 3) Successful execution of set thread */
74531fed
PA
1774
1775static void
47f8a51d 1776record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1777{
47f8a51d 1778 rs->general_thread = currthread;
74531fed
PA
1779}
1780
89be2091
DJ
1781/* If 'QPassSignals' is supported, tell the remote stub what signals
1782 it can simply pass through to the inferior without reporting. */
1783
1784static void
94bedb42
TT
1785remote_pass_signals (struct target_ops *self,
1786 int numsigs, unsigned char *pass_signals)
89be2091 1787{
4082afcc 1788 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1789 {
1790 char *pass_packet, *p;
89be2091 1791 int count = 0, i;
747dc59d 1792 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1793
1794 gdb_assert (numsigs < 256);
1795 for (i = 0; i < numsigs; i++)
1796 {
2455069d 1797 if (pass_signals[i])
89be2091
DJ
1798 count++;
1799 }
1800 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1801 strcpy (pass_packet, "QPassSignals:");
1802 p = pass_packet + strlen (pass_packet);
1803 for (i = 0; i < numsigs; i++)
1804 {
2455069d 1805 if (pass_signals[i])
89be2091
DJ
1806 {
1807 if (i >= 16)
1808 *p++ = tohex (i >> 4);
1809 *p++ = tohex (i & 15);
1810 if (count)
1811 *p++ = ';';
1812 else
1813 break;
1814 count--;
1815 }
1816 }
1817 *p = 0;
747dc59d 1818 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1819 {
89be2091
DJ
1820 putpkt (pass_packet);
1821 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1822 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1823 if (rs->last_pass_packet)
1824 xfree (rs->last_pass_packet);
1825 rs->last_pass_packet = pass_packet;
89be2091
DJ
1826 }
1827 else
1828 xfree (pass_packet);
1829 }
1830}
1831
9b224c5e
PA
1832/* If 'QProgramSignals' is supported, tell the remote stub what
1833 signals it should pass through to the inferior when detaching. */
1834
1835static void
daf5e9b6
TT
1836remote_program_signals (struct target_ops *self,
1837 int numsigs, unsigned char *signals)
9b224c5e 1838{
4082afcc 1839 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1840 {
1841 char *packet, *p;
1842 int count = 0, i;
5e4a05c4 1843 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1844
1845 gdb_assert (numsigs < 256);
1846 for (i = 0; i < numsigs; i++)
1847 {
1848 if (signals[i])
1849 count++;
1850 }
1851 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1852 strcpy (packet, "QProgramSignals:");
1853 p = packet + strlen (packet);
1854 for (i = 0; i < numsigs; i++)
1855 {
1856 if (signal_pass_state (i))
1857 {
1858 if (i >= 16)
1859 *p++ = tohex (i >> 4);
1860 *p++ = tohex (i & 15);
1861 if (count)
1862 *p++ = ';';
1863 else
1864 break;
1865 count--;
1866 }
1867 }
1868 *p = 0;
5e4a05c4
TT
1869 if (!rs->last_program_signals_packet
1870 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1871 {
9b224c5e
PA
1872 putpkt (packet);
1873 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1874 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1875 xfree (rs->last_program_signals_packet);
1876 rs->last_program_signals_packet = packet;
9b224c5e
PA
1877 }
1878 else
1879 xfree (packet);
1880 }
1881}
1882
79d7f229
PA
1883/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1884 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1885 thread. If GEN is set, set the general thread, if not, then set
1886 the step/continue thread. */
c906108c 1887static void
79d7f229 1888set_thread (struct ptid ptid, int gen)
c906108c 1889{
d01949b6 1890 struct remote_state *rs = get_remote_state ();
47f8a51d 1891 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1892 char *buf = rs->buf;
79d7f229 1893 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1894
79d7f229 1895 if (ptid_equal (state, ptid))
c906108c
SS
1896 return;
1897
79d7f229
PA
1898 *buf++ = 'H';
1899 *buf++ = gen ? 'g' : 'c';
1900 if (ptid_equal (ptid, magic_null_ptid))
1901 xsnprintf (buf, endbuf - buf, "0");
1902 else if (ptid_equal (ptid, any_thread_ptid))
1903 xsnprintf (buf, endbuf - buf, "0");
1904 else if (ptid_equal (ptid, minus_one_ptid))
1905 xsnprintf (buf, endbuf - buf, "-1");
1906 else
82f73884 1907 write_ptid (buf, endbuf, ptid);
79d7f229 1908 putpkt (rs->buf);
6d820c5c 1909 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1910 if (gen)
47f8a51d 1911 rs->general_thread = ptid;
c906108c 1912 else
47f8a51d 1913 rs->continue_thread = ptid;
c906108c 1914}
79d7f229
PA
1915
1916static void
1917set_general_thread (struct ptid ptid)
1918{
1919 set_thread (ptid, 1);
1920}
1921
1922static void
1923set_continue_thread (struct ptid ptid)
1924{
1925 set_thread (ptid, 0);
1926}
1927
3c9c4b83
PA
1928/* Change the remote current process. Which thread within the process
1929 ends up selected isn't important, as long as it is the same process
1930 as what INFERIOR_PTID points to.
1931
1932 This comes from that fact that there is no explicit notion of
1933 "selected process" in the protocol. The selected process for
1934 general operations is the process the selected general thread
1935 belongs to. */
1936
1937static void
1938set_general_process (void)
1939{
1940 struct remote_state *rs = get_remote_state ();
1941
1942 /* If the remote can't handle multiple processes, don't bother. */
901f9912 1943 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
1944 return;
1945
1946 /* We only need to change the remote current thread if it's pointing
1947 at some other process. */
47f8a51d 1948 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
1949 set_general_thread (inferior_ptid);
1950}
1951
c906108c 1952\f
7d1a114c
PA
1953/* Return nonzero if this is the main thread that we made up ourselves
1954 to model non-threaded targets as single-threaded. */
c906108c
SS
1955
1956static int
7d1a114c 1957remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 1958{
6d820c5c 1959 struct remote_state *rs = get_remote_state ();
82f73884 1960 char *p, *endp;
c906108c 1961
c0a2216e
PA
1962 if (ptid_equal (ptid, magic_null_ptid))
1963 /* The main thread is always alive. */
1964 return 1;
1965
ba348170 1966 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
1967 /* The main thread is always alive. This can happen after a
1968 vAttach, if the remote side doesn't support
1969 multi-threading. */
1970 return 1;
1971
7d1a114c
PA
1972 return 0;
1973}
1974
1975/* Return nonzero if the thread PTID is still alive on the remote
1976 system. */
1977
1978static int
1979remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1980{
1981 struct remote_state *rs = get_remote_state ();
1982 char *p, *endp;
1983
1984 /* Check if this is a thread that we made up ourselves to model
1985 non-threaded targets as single-threaded. */
1986 if (remote_thread_always_alive (ops, ptid))
1987 return 1;
1988
82f73884
PA
1989 p = rs->buf;
1990 endp = rs->buf + get_remote_packet_size ();
1991
1992 *p++ = 'T';
1993 write_ptid (p, endp, ptid);
1994
2e9f7625 1995 putpkt (rs->buf);
6d820c5c 1996 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 1997 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
1998}
1999
2000/* About these extended threadlist and threadinfo packets. They are
2001 variable length packets but, the fields within them are often fixed
2002 length. They are redundent enough to send over UDP as is the
2003 remote protocol in general. There is a matching unit test module
2004 in libstub. */
2005
23860348 2006/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2007 libstub protocol encoding, and remote.c. It is not particularly
23860348 2008 changable. */
cce74817
JM
2009
2010/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2011 Plan to fix this. */
cce74817 2012
23860348 2013typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2014
9d1f7ab2 2015/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2016 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2017
2018struct gdb_ext_thread_info
c5aa993b 2019 {
23860348 2020 threadref threadid; /* External form of thread reference. */
2bc416ba 2021 int active; /* Has state interesting to GDB?
23860348 2022 regs, stack. */
2bc416ba 2023 char display[256]; /* Brief state display, name,
cedea757 2024 blocked/suspended. */
23860348 2025 char shortname[32]; /* To be used to name threads. */
2bc416ba 2026 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2027 whatever. */
c5aa993b 2028 };
cce74817
JM
2029
2030/* The volume of remote transfers can be limited by submitting
2031 a mask containing bits specifying the desired information.
2032 Use a union of these values as the 'selection' parameter to
0df8b418 2033 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2034
2035#define TAG_THREADID 1
2036#define TAG_EXISTS 2
2037#define TAG_DISPLAY 4
2038#define TAG_THREADNAME 8
c5aa993b 2039#define TAG_MOREDISPLAY 16
cce74817 2040
23860348 2041#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2042
a14ed312 2043static char *unpack_nibble (char *buf, int *val);
cce74817 2044
a14ed312 2045static char *unpack_byte (char *buf, int *value);
cce74817 2046
a14ed312 2047static char *pack_int (char *buf, int value);
cce74817 2048
a14ed312 2049static char *unpack_int (char *buf, int *value);
cce74817 2050
a14ed312 2051static char *unpack_string (char *src, char *dest, int length);
cce74817 2052
23860348 2053static char *pack_threadid (char *pkt, threadref *id);
cce74817 2054
23860348 2055static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2056
23860348 2057void int_to_threadref (threadref *id, int value);
cce74817 2058
23860348 2059static int threadref_to_int (threadref *ref);
cce74817 2060
23860348 2061static void copy_threadref (threadref *dest, threadref *src);
cce74817 2062
23860348 2063static int threadmatch (threadref *dest, threadref *src);
cce74817 2064
2bc416ba 2065static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2066 threadref *id);
cce74817 2067
a14ed312 2068static int remote_unpack_thread_info_response (char *pkt,
23860348 2069 threadref *expectedref,
a14ed312
KB
2070 struct gdb_ext_thread_info
2071 *info);
cce74817
JM
2072
2073
2bc416ba 2074static int remote_get_threadinfo (threadref *threadid,
23860348 2075 int fieldset, /*TAG mask */
a14ed312 2076 struct gdb_ext_thread_info *info);
cce74817 2077
a14ed312
KB
2078static char *pack_threadlist_request (char *pkt, int startflag,
2079 int threadcount,
23860348 2080 threadref *nextthread);
cce74817 2081
a14ed312
KB
2082static int parse_threadlist_response (char *pkt,
2083 int result_limit,
23860348 2084 threadref *original_echo,
2bc416ba 2085 threadref *resultlist,
23860348 2086 int *doneflag);
cce74817 2087
a14ed312 2088static int remote_get_threadlist (int startflag,
23860348 2089 threadref *nextthread,
a14ed312
KB
2090 int result_limit,
2091 int *done,
2bc416ba 2092 int *result_count,
23860348 2093 threadref *threadlist);
cce74817 2094
23860348 2095typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2096
a14ed312
KB
2097static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2098 void *context, int looplimit);
cce74817 2099
23860348 2100static int remote_newthread_step (threadref *ref, void *context);
cce74817 2101
82f73884
PA
2102
2103/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2104 buffer we're allowed to write to. Returns
2105 BUF+CHARACTERS_WRITTEN. */
2106
2107static char *
2108write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2109{
2110 int pid, tid;
2111 struct remote_state *rs = get_remote_state ();
2112
2113 if (remote_multi_process_p (rs))
2114 {
2115 pid = ptid_get_pid (ptid);
2116 if (pid < 0)
2117 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2118 else
2119 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2120 }
ba348170 2121 tid = ptid_get_lwp (ptid);
82f73884
PA
2122 if (tid < 0)
2123 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2124 else
2125 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2126
2127 return buf;
2128}
2129
2130/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2131 passed the last parsed char. Returns null_ptid on error. */
2132
2133static ptid_t
2134read_ptid (char *buf, char **obuf)
2135{
2136 char *p = buf;
2137 char *pp;
2138 ULONGEST pid = 0, tid = 0;
82f73884
PA
2139
2140 if (*p == 'p')
2141 {
2142 /* Multi-process ptid. */
2143 pp = unpack_varlen_hex (p + 1, &pid);
2144 if (*pp != '.')
b37520b6 2145 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2146
2147 p = pp;
2148 pp = unpack_varlen_hex (p + 1, &tid);
2149 if (obuf)
2150 *obuf = pp;
ba348170 2151 return ptid_build (pid, tid, 0);
82f73884
PA
2152 }
2153
2154 /* No multi-process. Just a tid. */
2155 pp = unpack_varlen_hex (p, &tid);
2156
2157 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2158 what's in inferior_ptid, unless it's null at this point. If so,
2159 then since there's no way to know the pid of the reported
2160 threads, use the magic number. */
2161 if (ptid_equal (inferior_ptid, null_ptid))
2162 pid = ptid_get_pid (magic_null_ptid);
2163 else
2164 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2165
2166 if (obuf)
2167 *obuf = pp;
ba348170 2168 return ptid_build (pid, tid, 0);
82f73884
PA
2169}
2170
c906108c 2171static int
fba45db2 2172stubhex (int ch)
c906108c
SS
2173{
2174 if (ch >= 'a' && ch <= 'f')
2175 return ch - 'a' + 10;
2176 if (ch >= '0' && ch <= '9')
2177 return ch - '0';
2178 if (ch >= 'A' && ch <= 'F')
2179 return ch - 'A' + 10;
2180 return -1;
2181}
2182
2183static int
fba45db2 2184stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2185{
2186 int nibble;
2187 int retval = 0;
2188
2189 while (fieldlength)
2190 {
2191 nibble = stubhex (*buff++);
2192 retval |= nibble;
2193 fieldlength--;
2194 if (fieldlength)
2195 retval = retval << 4;
2196 }
2197 return retval;
2198}
2199
c906108c 2200static char *
fba45db2 2201unpack_nibble (char *buf, int *val)
c906108c 2202{
b7589f7d 2203 *val = fromhex (*buf++);
c906108c
SS
2204 return buf;
2205}
2206
c906108c 2207static char *
fba45db2 2208unpack_byte (char *buf, int *value)
c906108c
SS
2209{
2210 *value = stub_unpack_int (buf, 2);
2211 return buf + 2;
2212}
2213
2214static char *
fba45db2 2215pack_int (char *buf, int value)
c906108c
SS
2216{
2217 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2218 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2219 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2220 buf = pack_hex_byte (buf, (value & 0xff));
2221 return buf;
2222}
2223
2224static char *
fba45db2 2225unpack_int (char *buf, int *value)
c906108c
SS
2226{
2227 *value = stub_unpack_int (buf, 8);
2228 return buf + 8;
2229}
2230
23860348 2231#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2232static char *pack_string (char *pkt, char *string);
c906108c
SS
2233
2234static char *
fba45db2 2235pack_string (char *pkt, char *string)
c906108c
SS
2236{
2237 char ch;
2238 int len;
2239
2240 len = strlen (string);
2241 if (len > 200)
23860348 2242 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2243 pkt = pack_hex_byte (pkt, len);
2244 while (len-- > 0)
2245 {
2246 ch = *string++;
2247 if ((ch == '\0') || (ch == '#'))
23860348 2248 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2249 *pkt++ = ch;
2250 }
2251 return pkt;
2252}
2253#endif /* 0 (unused) */
2254
2255static char *
fba45db2 2256unpack_string (char *src, char *dest, int length)
c906108c
SS
2257{
2258 while (length--)
2259 *dest++ = *src++;
2260 *dest = '\0';
2261 return src;
2262}
2263
2264static char *
fba45db2 2265pack_threadid (char *pkt, threadref *id)
c906108c
SS
2266{
2267 char *limit;
2268 unsigned char *altid;
2269
2270 altid = (unsigned char *) id;
2271 limit = pkt + BUF_THREAD_ID_SIZE;
2272 while (pkt < limit)
2273 pkt = pack_hex_byte (pkt, *altid++);
2274 return pkt;
2275}
2276
2277
2278static char *
fba45db2 2279unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2280{
2281 char *altref;
2282 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2283 int x, y;
2284
2285 altref = (char *) id;
2286
2287 while (inbuf < limit)
2288 {
2289 x = stubhex (*inbuf++);
2290 y = stubhex (*inbuf++);
2291 *altref++ = (x << 4) | y;
2292 }
2293 return inbuf;
2294}
2295
2296/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2297 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2298 to use 64bit thread references internally. This is an adapter
2299 function. */
2300
2301void
fba45db2 2302int_to_threadref (threadref *id, int value)
c906108c
SS
2303{
2304 unsigned char *scan;
2305
2306 scan = (unsigned char *) id;
2307 {
2308 int i = 4;
2309 while (i--)
2310 *scan++ = 0;
2311 }
2312 *scan++ = (value >> 24) & 0xff;
2313 *scan++ = (value >> 16) & 0xff;
2314 *scan++ = (value >> 8) & 0xff;
2315 *scan++ = (value & 0xff);
2316}
2317
2318static int
fba45db2 2319threadref_to_int (threadref *ref)
c906108c
SS
2320{
2321 int i, value = 0;
2322 unsigned char *scan;
2323
cfd77fa1 2324 scan = *ref;
c906108c
SS
2325 scan += 4;
2326 i = 4;
2327 while (i-- > 0)
2328 value = (value << 8) | ((*scan++) & 0xff);
2329 return value;
2330}
2331
2332static void
fba45db2 2333copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2334{
2335 int i;
2336 unsigned char *csrc, *cdest;
2337
2338 csrc = (unsigned char *) src;
2339 cdest = (unsigned char *) dest;
2340 i = 8;
2341 while (i--)
2342 *cdest++ = *csrc++;
2343}
2344
2345static int
fba45db2 2346threadmatch (threadref *dest, threadref *src)
c906108c 2347{
23860348 2348 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2349#if 0
2350 unsigned char *srcp, *destp;
2351 int i, result;
2352 srcp = (char *) src;
2353 destp = (char *) dest;
2354
2355 result = 1;
2356 while (i-- > 0)
2357 result &= (*srcp++ == *destp++) ? 1 : 0;
2358 return result;
2359#endif
2360 return 1;
2361}
2362
2363/*
c5aa993b
JM
2364 threadid:1, # always request threadid
2365 context_exists:2,
2366 display:4,
2367 unique_name:8,
2368 more_display:16
2369 */
c906108c
SS
2370
2371/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2372
2373static char *
fba45db2 2374pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2375{
23860348
MS
2376 *pkt++ = 'q'; /* Info Query */
2377 *pkt++ = 'P'; /* process or thread info */
2378 pkt = pack_int (pkt, mode); /* mode */
c906108c 2379 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2380 *pkt = '\0'; /* terminate */
c906108c
SS
2381 return pkt;
2382}
2383
23860348 2384/* These values tag the fields in a thread info response packet. */
c906108c 2385/* Tagging the fields allows us to request specific fields and to
23860348 2386 add more fields as time goes by. */
c906108c 2387
23860348 2388#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2389#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2390 fetch registers and its stack? */
c5aa993b 2391#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2392#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2393#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2394 the process. */
c906108c
SS
2395
2396static int
fba45db2
KB
2397remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2398 struct gdb_ext_thread_info *info)
c906108c 2399{
d01949b6 2400 struct remote_state *rs = get_remote_state ();
c906108c 2401 int mask, length;
cfd77fa1 2402 int tag;
c906108c 2403 threadref ref;
6d820c5c 2404 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2405 int retval = 1;
2406
23860348 2407 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2408 info->active = 0;
2409 info->display[0] = '\0';
2410 info->shortname[0] = '\0';
2411 info->more_display[0] = '\0';
2412
23860348
MS
2413 /* Assume the characters indicating the packet type have been
2414 stripped. */
c906108c
SS
2415 pkt = unpack_int (pkt, &mask); /* arg mask */
2416 pkt = unpack_threadid (pkt, &ref);
2417
2418 if (mask == 0)
8a3fe4f8 2419 warning (_("Incomplete response to threadinfo request."));
c906108c 2420 if (!threadmatch (&ref, expectedref))
23860348 2421 { /* This is an answer to a different request. */
8a3fe4f8 2422 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2423 return 0;
2424 }
2425 copy_threadref (&info->threadid, &ref);
2426
23860348 2427 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2428
23860348
MS
2429 /* Packets are terminated with nulls. */
2430 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2431 {
2432 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2433 pkt = unpack_byte (pkt, &length); /* length */
2434 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2435 {
8a3fe4f8 2436 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2437 retval = 0;
2438 break;
2439 }
2440 if (tag == TAG_THREADID)
2441 {
2442 if (length != 16)
2443 {
8a3fe4f8 2444 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2445 retval = 0;
2446 break;
2447 }
2448 pkt = unpack_threadid (pkt, &ref);
2449 mask = mask & ~TAG_THREADID;
2450 continue;
2451 }
2452 if (tag == TAG_EXISTS)
2453 {
2454 info->active = stub_unpack_int (pkt, length);
2455 pkt += length;
2456 mask = mask & ~(TAG_EXISTS);
2457 if (length > 8)
2458 {
8a3fe4f8 2459 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2460 retval = 0;
2461 break;
2462 }
2463 continue;
2464 }
2465 if (tag == TAG_THREADNAME)
2466 {
2467 pkt = unpack_string (pkt, &info->shortname[0], length);
2468 mask = mask & ~TAG_THREADNAME;
2469 continue;
2470 }
2471 if (tag == TAG_DISPLAY)
2472 {
2473 pkt = unpack_string (pkt, &info->display[0], length);
2474 mask = mask & ~TAG_DISPLAY;
2475 continue;
2476 }
2477 if (tag == TAG_MOREDISPLAY)
2478 {
2479 pkt = unpack_string (pkt, &info->more_display[0], length);
2480 mask = mask & ~TAG_MOREDISPLAY;
2481 continue;
2482 }
8a3fe4f8 2483 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2484 break; /* Not a tag we know about. */
c906108c
SS
2485 }
2486 return retval;
2487}
2488
2489static int
fba45db2
KB
2490remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2491 struct gdb_ext_thread_info *info)
c906108c 2492{
d01949b6 2493 struct remote_state *rs = get_remote_state ();
c906108c 2494 int result;
c906108c 2495
2e9f7625
DJ
2496 pack_threadinfo_request (rs->buf, fieldset, threadid);
2497 putpkt (rs->buf);
6d820c5c 2498 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2499
2500 if (rs->buf[0] == '\0')
2501 return 0;
2502
2e9f7625 2503 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2504 threadid, info);
c906108c
SS
2505 return result;
2506}
2507
c906108c
SS
2508/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2509
2510static char *
fba45db2
KB
2511pack_threadlist_request (char *pkt, int startflag, int threadcount,
2512 threadref *nextthread)
c906108c
SS
2513{
2514 *pkt++ = 'q'; /* info query packet */
2515 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2516 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2517 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2518 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2519 *pkt = '\0';
2520 return pkt;
2521}
2522
2523/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2524
2525static int
fba45db2
KB
2526parse_threadlist_response (char *pkt, int result_limit,
2527 threadref *original_echo, threadref *resultlist,
2528 int *doneflag)
c906108c 2529{
d01949b6 2530 struct remote_state *rs = get_remote_state ();
c906108c
SS
2531 char *limit;
2532 int count, resultcount, done;
2533
2534 resultcount = 0;
2535 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2536 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2537 /* done parse past here */
c906108c
SS
2538 pkt = unpack_byte (pkt, &count); /* count field */
2539 pkt = unpack_nibble (pkt, &done);
2540 /* The first threadid is the argument threadid. */
2541 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2542 while ((count-- > 0) && (pkt < limit))
2543 {
2544 pkt = unpack_threadid (pkt, resultlist++);
2545 if (resultcount++ >= result_limit)
2546 break;
2547 }
2548 if (doneflag)
2549 *doneflag = done;
2550 return resultcount;
2551}
2552
6dc54d91
PA
2553/* Fetch the next batch of threads from the remote. Returns -1 if the
2554 qL packet is not supported, 0 on error and 1 on success. */
2555
c906108c 2556static int
fba45db2
KB
2557remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2558 int *done, int *result_count, threadref *threadlist)
c906108c 2559{
d01949b6 2560 struct remote_state *rs = get_remote_state ();
c906108c
SS
2561 int result = 1;
2562
23860348 2563 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2564 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2565 >= get_remote_packet_size ())
ea9c271d 2566 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2567
6d820c5c
DJ
2568 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2569 putpkt (rs->buf);
2570 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2571 if (*rs->buf == '\0')
6dc54d91
PA
2572 {
2573 /* Packet not supported. */
2574 return -1;
2575 }
2576
2577 *result_count =
2578 parse_threadlist_response (rs->buf + 2, result_limit,
2579 &rs->echo_nextthread, threadlist, done);
c906108c 2580
0d031856 2581 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2582 {
23860348
MS
2583 /* FIXME: This is a good reason to drop the packet. */
2584 /* Possably, there is a duplicate response. */
c906108c
SS
2585 /* Possabilities :
2586 retransmit immediatly - race conditions
2587 retransmit after timeout - yes
2588 exit
2589 wait for packet, then exit
2590 */
8a3fe4f8 2591 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2592 return 0; /* I choose simply exiting. */
c906108c
SS
2593 }
2594 if (*result_count <= 0)
2595 {
2596 if (*done != 1)
2597 {
8a3fe4f8 2598 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2599 result = 0;
2600 }
2601 return result; /* break; */
2602 }
2603 if (*result_count > result_limit)
2604 {
2605 *result_count = 0;
8a3fe4f8 2606 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2607 return 0;
2608 }
2609 return result;
2610}
2611
6dc54d91
PA
2612/* Fetch the list of remote threads, with the qL packet, and call
2613 STEPFUNCTION for each thread found. Stops iterating and returns 1
2614 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2615 STEPFUNCTION returns false. If the packet is not supported,
2616 returns -1. */
c906108c 2617
c906108c 2618static int
fba45db2
KB
2619remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2620 int looplimit)
c906108c 2621{
0d031856 2622 struct remote_state *rs = get_remote_state ();
c906108c
SS
2623 int done, i, result_count;
2624 int startflag = 1;
2625 int result = 1;
2626 int loopcount = 0;
c906108c
SS
2627
2628 done = 0;
2629 while (!done)
2630 {
2631 if (loopcount++ > looplimit)
2632 {
2633 result = 0;
8a3fe4f8 2634 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2635 break;
2636 }
6dc54d91
PA
2637 result = remote_get_threadlist (startflag, &rs->nextthread,
2638 MAXTHREADLISTRESULTS,
2639 &done, &result_count,
2640 rs->resultthreadlist);
2641 if (result <= 0)
2642 break;
23860348 2643 /* Clear for later iterations. */
c906108c
SS
2644 startflag = 0;
2645 /* Setup to resume next batch of thread references, set nextthread. */
2646 if (result_count >= 1)
0d031856
TT
2647 copy_threadref (&rs->nextthread,
2648 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2649 i = 0;
2650 while (result_count--)
6dc54d91
PA
2651 {
2652 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2653 {
2654 result = 0;
2655 break;
2656 }
2657 }
c906108c
SS
2658 }
2659 return result;
2660}
2661
6dc54d91
PA
2662/* A thread found on the remote target. */
2663
2664typedef struct thread_item
2665{
2666 /* The thread's PTID. */
2667 ptid_t ptid;
2668
2669 /* The thread's extra info. May be NULL. */
2670 char *extra;
2671
2672 /* The core the thread was running on. -1 if not known. */
2673 int core;
2674} thread_item_t;
2675DEF_VEC_O(thread_item_t);
2676
2677/* Context passed around to the various methods listing remote
2678 threads. As new threads are found, they're added to the ITEMS
2679 vector. */
2680
2681struct threads_listing_context
2682{
2683 /* The threads found on the remote target. */
2684 VEC (thread_item_t) *items;
2685};
2686
80134cf5
PA
2687/* Discard the contents of the constructed thread listing context. */
2688
2689static void
2690clear_threads_listing_context (void *p)
2691{
2692 struct threads_listing_context *context = p;
2693 int i;
2694 struct thread_item *item;
2695
2696 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2697 xfree (item->extra);
2698
2699 VEC_free (thread_item_t, context->items);
2700}
2701
cbb8991c
DB
2702/* Remove the thread specified as the related_pid field of WS
2703 from the CONTEXT list. */
2704
2705static void
2706threads_listing_context_remove (struct target_waitstatus *ws,
2707 struct threads_listing_context *context)
2708{
2709 struct thread_item *item;
2710 int i;
2711 ptid_t child_ptid = ws->value.related_pid;
2712
2713 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2714 {
2715 if (ptid_equal (item->ptid, child_ptid))
2716 {
2717 VEC_ordered_remove (thread_item_t, context->items, i);
2718 break;
2719 }
2720 }
2721}
2722
c906108c 2723static int
6dc54d91 2724remote_newthread_step (threadref *ref, void *data)
c906108c 2725{
6dc54d91
PA
2726 struct threads_listing_context *context = data;
2727 struct thread_item item;
79d7f229 2728 int pid = ptid_get_pid (inferior_ptid);
39f77062 2729
6dc54d91
PA
2730 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2731 item.core = -1;
2732 item.extra = NULL;
2733
2734 VEC_safe_push (thread_item_t, context->items, &item);
2735
c906108c
SS
2736 return 1; /* continue iterator */
2737}
2738
2739#define CRAZY_MAX_THREADS 1000
2740
39f77062
KB
2741static ptid_t
2742remote_current_thread (ptid_t oldpid)
c906108c 2743{
d01949b6 2744 struct remote_state *rs = get_remote_state ();
c906108c
SS
2745
2746 putpkt ("qC");
6d820c5c 2747 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2748 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
82f73884 2749 return read_ptid (&rs->buf[2], NULL);
c906108c
SS
2750 else
2751 return oldpid;
2752}
2753
6dc54d91 2754/* List remote threads using the deprecated qL packet. */
cce74817 2755
6dc54d91
PA
2756static int
2757remote_get_threads_with_ql (struct target_ops *ops,
2758 struct threads_listing_context *context)
c906108c 2759{
6dc54d91
PA
2760 if (remote_threadlist_iterator (remote_newthread_step, context,
2761 CRAZY_MAX_THREADS) >= 0)
2762 return 1;
2763
2764 return 0;
c906108c
SS
2765}
2766
dc146f7c
VP
2767#if defined(HAVE_LIBEXPAT)
2768
dc146f7c
VP
2769static void
2770start_thread (struct gdb_xml_parser *parser,
2771 const struct gdb_xml_element *element,
2772 void *user_data, VEC(gdb_xml_value_s) *attributes)
2773{
6dc54d91 2774 struct threads_listing_context *data = user_data;
dc146f7c
VP
2775
2776 struct thread_item item;
2777 char *id;
3d2c1d41 2778 struct gdb_xml_value *attr;
dc146f7c 2779
3d2c1d41 2780 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2781 item.ptid = read_ptid (id, NULL);
2782
3d2c1d41
PA
2783 attr = xml_find_attribute (attributes, "core");
2784 if (attr != NULL)
2785 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2786 else
2787 item.core = -1;
2788
2789 item.extra = 0;
2790
2791 VEC_safe_push (thread_item_t, data->items, &item);
2792}
2793
2794static void
2795end_thread (struct gdb_xml_parser *parser,
2796 const struct gdb_xml_element *element,
2797 void *user_data, const char *body_text)
2798{
6dc54d91 2799 struct threads_listing_context *data = user_data;
dc146f7c
VP
2800
2801 if (body_text && *body_text)
2ae2a0b7 2802 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2803}
2804
2805const struct gdb_xml_attribute thread_attributes[] = {
2806 { "id", GDB_XML_AF_NONE, NULL, NULL },
2807 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2808 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2809};
2810
2811const struct gdb_xml_element thread_children[] = {
2812 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2813};
2814
2815const struct gdb_xml_element threads_children[] = {
2816 { "thread", thread_attributes, thread_children,
2817 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2818 start_thread, end_thread },
2819 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2820};
2821
2822const struct gdb_xml_element threads_elements[] = {
2823 { "threads", NULL, threads_children,
2824 GDB_XML_EF_NONE, NULL, NULL },
2825 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2826};
2827
2828#endif
2829
6dc54d91 2830/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2831
6dc54d91
PA
2832static int
2833remote_get_threads_with_qxfer (struct target_ops *ops,
2834 struct threads_listing_context *context)
0f71a2f6 2835{
dc146f7c 2836#if defined(HAVE_LIBEXPAT)
4082afcc 2837 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2838 {
6dc54d91 2839 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2840 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2841
6dc54d91 2842 if (xml != NULL && *xml != '\0')
dc146f7c 2843 {
6dc54d91
PA
2844 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2845 threads_elements, xml, context);
dc146f7c
VP
2846 }
2847
2848 do_cleanups (back_to);
6dc54d91 2849 return 1;
dc146f7c
VP
2850 }
2851#endif
2852
6dc54d91
PA
2853 return 0;
2854}
2855
2856/* List remote threads using qfThreadInfo/qsThreadInfo. */
2857
2858static int
2859remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2860 struct threads_listing_context *context)
2861{
2862 struct remote_state *rs = get_remote_state ();
2863
b80fafe3 2864 if (rs->use_threadinfo_query)
9d1f7ab2 2865 {
6dc54d91
PA
2866 char *bufp;
2867
9d1f7ab2 2868 putpkt ("qfThreadInfo");
6d820c5c 2869 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2870 bufp = rs->buf;
9d1f7ab2 2871 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2872 {
9d1f7ab2
MS
2873 while (*bufp++ == 'm') /* reply contains one or more TID */
2874 {
2875 do
2876 {
6dc54d91
PA
2877 struct thread_item item;
2878
2879 item.ptid = read_ptid (bufp, &bufp);
2880 item.core = -1;
2881 item.extra = NULL;
2882
2883 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2884 }
2885 while (*bufp++ == ','); /* comma-separated list */
2886 putpkt ("qsThreadInfo");
6d820c5c 2887 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2888 bufp = rs->buf;
9d1f7ab2 2889 }
6dc54d91
PA
2890 return 1;
2891 }
2892 else
2893 {
2894 /* Packet not recognized. */
2895 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2896 }
2897 }
2898
6dc54d91
PA
2899 return 0;
2900}
2901
e8032dde 2902/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2903 targets. */
2904
2905static void
e8032dde 2906remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2907{
2908 struct remote_state *rs = get_remote_state ();
2909 struct threads_listing_context context;
2910 struct cleanup *old_chain;
ab970af1 2911 int got_list = 0;
e8032dde 2912
6dc54d91
PA
2913 context.items = NULL;
2914 old_chain = make_cleanup (clear_threads_listing_context, &context);
2915
2916 /* We have a few different mechanisms to fetch the thread list. Try
2917 them all, starting with the most preferred one first, falling
2918 back to older methods. */
2919 if (remote_get_threads_with_qxfer (ops, &context)
2920 || remote_get_threads_with_qthreadinfo (ops, &context)
2921 || remote_get_threads_with_ql (ops, &context))
2922 {
2923 int i;
2924 struct thread_item *item;
ab970af1
PA
2925 struct thread_info *tp, *tmp;
2926
2927 got_list = 1;
2928
7d1a114c
PA
2929 if (VEC_empty (thread_item_t, context.items)
2930 && remote_thread_always_alive (ops, inferior_ptid))
2931 {
2932 /* Some targets don't really support threads, but still
2933 reply an (empty) thread list in response to the thread
2934 listing packets, instead of replying "packet not
2935 supported". Exit early so we don't delete the main
2936 thread. */
2937 do_cleanups (old_chain);
2938 return;
2939 }
2940
ab970af1
PA
2941 /* CONTEXT now holds the current thread list on the remote
2942 target end. Delete GDB-side threads no longer found on the
2943 target. */
8a06aea7 2944 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 2945 {
ab970af1
PA
2946 for (i = 0;
2947 VEC_iterate (thread_item_t, context.items, i, item);
2948 ++i)
2949 {
2950 if (ptid_equal (item->ptid, tp->ptid))
2951 break;
2952 }
2953
2954 if (i == VEC_length (thread_item_t, context.items))
2955 {
2956 /* Not found. */
2957 delete_thread (tp->ptid);
2958 }
cbb8991c
DB
2959 }
2960
2961 /* Remove any unreported fork child threads from CONTEXT so
2962 that we don't interfere with follow fork, which is where
2963 creation of such threads is handled. */
2964 remove_new_fork_children (&context);
74531fed 2965
ab970af1 2966 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2967 for (i = 0;
2968 VEC_iterate (thread_item_t, context.items, i, item);
2969 ++i)
2970 {
2971 if (!ptid_equal (item->ptid, null_ptid))
2972 {
2973 struct private_thread_info *info;
2974 /* In non-stop mode, we assume new found threads are
2975 running until proven otherwise with a stop reply. In
2976 all-stop, we can only get here if all threads are
2977 stopped. */
2978 int running = non_stop ? 1 : 0;
2979
2980 remote_notice_new_inferior (item->ptid, running);
2981
2982 info = demand_private_info (item->ptid);
2983 info->core = item->core;
2984 info->extra = item->extra;
2985 item->extra = NULL;
2986 }
2987 }
2988 }
2989
ab970af1
PA
2990 if (!got_list)
2991 {
2992 /* If no thread listing method is supported, then query whether
2993 each known thread is alive, one by one, with the T packet.
2994 If the target doesn't support threads at all, then this is a
2995 no-op. See remote_thread_alive. */
2996 prune_threads ();
2997 }
2998
6dc54d91 2999 do_cleanups (old_chain);
9d1f7ab2
MS
3000}
3001
802188a7 3002/*
9d1f7ab2
MS
3003 * Collect a descriptive string about the given thread.
3004 * The target may say anything it wants to about the thread
3005 * (typically info about its blocked / runnable state, name, etc.).
3006 * This string will appear in the info threads display.
802188a7 3007 *
9d1f7ab2
MS
3008 * Optional: targets are not required to implement this function.
3009 */
3010
3011static char *
c15906d8 3012remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3013{
d01949b6 3014 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3015 int result;
3016 int set;
3017 threadref id;
3018 struct gdb_ext_thread_info threadinfo;
23860348 3019 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3020 int n = 0; /* position in display_buf */
3021
5d93a237 3022 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3023 internal_error (__FILE__, __LINE__,
e2e0b3e5 3024 _("remote_threads_extra_info"));
9d1f7ab2 3025
60e569b9 3026 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3027 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3028 /* This is the main thread which was added by GDB. The remote
3029 server doesn't know about it. */
3030 return NULL;
3031
4082afcc 3032 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3033 {
3034 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3035
fe978cb0
PA
3036 if (info && info->priv)
3037 return info->priv->extra;
dc146f7c
VP
3038 else
3039 return NULL;
3040 }
3041
b80fafe3 3042 if (rs->use_threadextra_query)
9d1f7ab2 3043 {
82f73884
PA
3044 char *b = rs->buf;
3045 char *endb = rs->buf + get_remote_packet_size ();
3046
3047 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3048 b += strlen (b);
3049 write_ptid (b, endb, tp->ptid);
3050
2e9f7625 3051 putpkt (rs->buf);
6d820c5c 3052 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3053 if (rs->buf[0] != 0)
9d1f7ab2 3054 {
2e9f7625
DJ
3055 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3056 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3057 display_buf [result] = '\0';
9d1f7ab2
MS
3058 return display_buf;
3059 }
0f71a2f6 3060 }
9d1f7ab2
MS
3061
3062 /* If the above query fails, fall back to the old method. */
b80fafe3 3063 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3064 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3065 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3066 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3067 if (remote_get_threadinfo (&id, set, &threadinfo))
3068 if (threadinfo.active)
0f71a2f6 3069 {
9d1f7ab2 3070 if (*threadinfo.shortname)
2bc416ba 3071 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3072 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3073 if (*threadinfo.display)
2bc416ba 3074 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3075 " State: %s,", threadinfo.display);
9d1f7ab2 3076 if (*threadinfo.more_display)
2bc416ba 3077 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3078 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3079
3080 if (n > 0)
c5aa993b 3081 {
23860348 3082 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3083 if (',' == display_buf[n-1])
3084 display_buf[n-1] = ' ';
3085 return display_buf;
c5aa993b 3086 }
0f71a2f6 3087 }
9d1f7ab2 3088 return NULL;
0f71a2f6 3089}
c906108c 3090\f
c5aa993b 3091
0fb4aa4b 3092static int
61fc905d 3093remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3094 struct static_tracepoint_marker *marker)
3095{
3096 struct remote_state *rs = get_remote_state ();
3097 char *p = rs->buf;
3098
bba74b36 3099 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3100 p += strlen (p);
3101 p += hexnumstr (p, addr);
3102 putpkt (rs->buf);
3103 getpkt (&rs->buf, &rs->buf_size, 0);
3104 p = rs->buf;
3105
3106 if (*p == 'E')
3107 error (_("Remote failure reply: %s"), p);
3108
3109 if (*p++ == 'm')
3110 {
3111 parse_static_tracepoint_marker_definition (p, &p, marker);
3112 return 1;
3113 }
3114
3115 return 0;
3116}
3117
0fb4aa4b 3118static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3119remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3120 const char *strid)
0fb4aa4b
PA
3121{
3122 struct remote_state *rs = get_remote_state ();
3123 VEC(static_tracepoint_marker_p) *markers = NULL;
3124 struct static_tracepoint_marker *marker = NULL;
3125 struct cleanup *old_chain;
3126 char *p;
3127
3128 /* Ask for a first packet of static tracepoint marker
3129 definition. */
3130 putpkt ("qTfSTM");
3131 getpkt (&rs->buf, &rs->buf_size, 0);
3132 p = rs->buf;
3133 if (*p == 'E')
3134 error (_("Remote failure reply: %s"), p);
3135
3136 old_chain = make_cleanup (free_current_marker, &marker);
3137
3138 while (*p++ == 'm')
3139 {
3140 if (marker == NULL)
3141 marker = XCNEW (struct static_tracepoint_marker);
3142
3143 do
3144 {
3145 parse_static_tracepoint_marker_definition (p, &p, marker);
3146
3147 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3148 {
3149 VEC_safe_push (static_tracepoint_marker_p,
3150 markers, marker);
3151 marker = NULL;
3152 }
3153 else
3154 {
3155 release_static_tracepoint_marker (marker);
3156 memset (marker, 0, sizeof (*marker));
3157 }
3158 }
3159 while (*p++ == ','); /* comma-separated list */
3160 /* Ask for another packet of static tracepoint definition. */
3161 putpkt ("qTsSTM");
3162 getpkt (&rs->buf, &rs->buf_size, 0);
3163 p = rs->buf;
3164 }
3165
3166 do_cleanups (old_chain);
3167 return markers;
3168}
3169
3170\f
10760264
JB
3171/* Implement the to_get_ada_task_ptid function for the remote targets. */
3172
3173static ptid_t
1e6b91a4 3174remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3175{
ba348170 3176 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3177}
3178\f
3179
24b06219 3180/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3181
3182static void
fba45db2 3183extended_remote_restart (void)
c906108c 3184{
d01949b6 3185 struct remote_state *rs = get_remote_state ();
c906108c
SS
3186
3187 /* Send the restart command; for reasons I don't understand the
3188 remote side really expects a number after the "R". */
ea9c271d 3189 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3190 putpkt (rs->buf);
c906108c 3191
ad9a8f3f 3192 remote_fileio_reset ();
c906108c
SS
3193}
3194\f
3195/* Clean up connection to a remote debugger. */
3196
c906108c 3197static void
de90e03d 3198remote_close (struct target_ops *self)
c906108c 3199{
5d93a237
TT
3200 struct remote_state *rs = get_remote_state ();
3201
3202 if (rs->remote_desc == NULL)
d3fd5342
PA
3203 return; /* already closed */
3204
3205 /* Make sure we leave stdin registered in the event loop, and we
3206 don't leave the async SIGINT signal handler installed. */
e3594fd1 3207 remote_terminal_ours (self);
ce5ce7ed 3208
5d93a237
TT
3209 serial_close (rs->remote_desc);
3210 rs->remote_desc = NULL;
ce5ce7ed
PA
3211
3212 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3213 of all the inferiors and their threads we were controlling.
3214 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3215 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3216 inferior_ptid = null_ptid;
f67fd822 3217 discard_all_inferiors ();
ce5ce7ed 3218
f48ff2a7
YQ
3219 /* We are closing the remote target, so we should discard
3220 everything of this target. */
bcc75809 3221 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3222
3223 if (remote_async_inferior_event_token)
3224 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3225
5965e028 3226 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3227
3228 trace_reset_local_state ();
c906108c
SS
3229}
3230
23860348 3231/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3232
3233static void
fba45db2 3234get_offsets (void)
c906108c 3235{
d01949b6 3236 struct remote_state *rs = get_remote_state ();
2e9f7625 3237 char *buf;
085dd6e6 3238 char *ptr;
31d99776
DJ
3239 int lose, num_segments = 0, do_sections, do_segments;
3240 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3241 struct section_offsets *offs;
31d99776
DJ
3242 struct symfile_segment_data *data;
3243
3244 if (symfile_objfile == NULL)
3245 return;
c906108c
SS
3246
3247 putpkt ("qOffsets");
6d820c5c 3248 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3249 buf = rs->buf;
c906108c
SS
3250
3251 if (buf[0] == '\000')
3252 return; /* Return silently. Stub doesn't support
23860348 3253 this command. */
c906108c
SS
3254 if (buf[0] == 'E')
3255 {
8a3fe4f8 3256 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3257 return;
3258 }
3259
3260 /* Pick up each field in turn. This used to be done with scanf, but
3261 scanf will make trouble if CORE_ADDR size doesn't match
3262 conversion directives correctly. The following code will work
3263 with any size of CORE_ADDR. */
3264 text_addr = data_addr = bss_addr = 0;
3265 ptr = buf;
3266 lose = 0;
3267
61012eef 3268 if (startswith (ptr, "Text="))
c906108c
SS
3269 {
3270 ptr += 5;
3271 /* Don't use strtol, could lose on big values. */
3272 while (*ptr && *ptr != ';')
3273 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3274
61012eef 3275 if (startswith (ptr, ";Data="))
31d99776
DJ
3276 {
3277 ptr += 6;
3278 while (*ptr && *ptr != ';')
3279 data_addr = (data_addr << 4) + fromhex (*ptr++);
3280 }
3281 else
3282 lose = 1;
3283
61012eef 3284 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3285 {
3286 ptr += 5;
3287 while (*ptr && *ptr != ';')
3288 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3289
31d99776
DJ
3290 if (bss_addr != data_addr)
3291 warning (_("Target reported unsupported offsets: %s"), buf);
3292 }
3293 else
3294 lose = 1;
3295 }
61012eef 3296 else if (startswith (ptr, "TextSeg="))
c906108c 3297 {
31d99776
DJ
3298 ptr += 8;
3299 /* Don't use strtol, could lose on big values. */
c906108c 3300 while (*ptr && *ptr != ';')
31d99776
DJ
3301 text_addr = (text_addr << 4) + fromhex (*ptr++);
3302 num_segments = 1;
3303
61012eef 3304 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3305 {
3306 ptr += 9;
3307 while (*ptr && *ptr != ';')
3308 data_addr = (data_addr << 4) + fromhex (*ptr++);
3309 num_segments++;
3310 }
c906108c
SS
3311 }
3312 else
3313 lose = 1;
3314
3315 if (lose)
8a3fe4f8 3316 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3317 else if (*ptr != '\0')
3318 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3319
802188a7 3320 offs = ((struct section_offsets *)
a39a16c4 3321 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3322 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3323 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3324
31d99776
DJ
3325 data = get_symfile_segment_data (symfile_objfile->obfd);
3326 do_segments = (data != NULL);
3327 do_sections = num_segments == 0;
c906108c 3328
28c32713 3329 if (num_segments > 0)
31d99776 3330 {
31d99776
DJ
3331 segments[0] = text_addr;
3332 segments[1] = data_addr;
3333 }
28c32713
JB
3334 /* If we have two segments, we can still try to relocate everything
3335 by assuming that the .text and .data offsets apply to the whole
3336 text and data segments. Convert the offsets given in the packet
3337 to base addresses for symfile_map_offsets_to_segments. */
3338 else if (data && data->num_segments == 2)
3339 {
3340 segments[0] = data->segment_bases[0] + text_addr;
3341 segments[1] = data->segment_bases[1] + data_addr;
3342 num_segments = 2;
3343 }
8d385431
DJ
3344 /* If the object file has only one segment, assume that it is text
3345 rather than data; main programs with no writable data are rare,
3346 but programs with no code are useless. Of course the code might
3347 have ended up in the data segment... to detect that we would need
3348 the permissions here. */
3349 else if (data && data->num_segments == 1)
3350 {
3351 segments[0] = data->segment_bases[0] + text_addr;
3352 num_segments = 1;
3353 }
28c32713
JB
3354 /* There's no way to relocate by segment. */
3355 else
3356 do_segments = 0;
31d99776
DJ
3357
3358 if (do_segments)
3359 {
3360 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3361 offs, num_segments, segments);
3362
3363 if (ret == 0 && !do_sections)
3e43a32a
MS
3364 error (_("Can not handle qOffsets TextSeg "
3365 "response with this symbol file"));
31d99776
DJ
3366
3367 if (ret > 0)
3368 do_sections = 0;
3369 }
c906108c 3370
9ef895d6
DJ
3371 if (data)
3372 free_symfile_segment_data (data);
31d99776
DJ
3373
3374 if (do_sections)
3375 {
3376 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3377
3e43a32a
MS
3378 /* This is a temporary kludge to force data and bss to use the
3379 same offsets because that's what nlmconv does now. The real
3380 solution requires changes to the stub and remote.c that I
3381 don't have time to do right now. */
31d99776
DJ
3382
3383 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3384 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3385 }
c906108c
SS
3386
3387 objfile_relocate (symfile_objfile, offs);
3388}
3389
74531fed
PA
3390/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3391 threads we know are stopped already. This is used during the
3392 initial remote connection in non-stop mode --- threads that are
3393 reported as already being stopped are left stopped. */
3394
3395static int
3396set_stop_requested_callback (struct thread_info *thread, void *data)
3397{
3398 /* If we have a stop reply for this thread, it must be stopped. */
3399 if (peek_stop_reply (thread->ptid))
3400 set_stop_requested (thread->ptid, 1);
3401
3402 return 0;
3403}
3404
9a7071a8
JB
3405/* Send interrupt_sequence to remote target. */
3406static void
eeae04df 3407send_interrupt_sequence (void)
9a7071a8 3408{
5d93a237
TT
3409 struct remote_state *rs = get_remote_state ();
3410
9a7071a8 3411 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3412 remote_serial_write ("\x03", 1);
9a7071a8 3413 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3414 serial_send_break (rs->remote_desc);
9a7071a8
JB
3415 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3416 {
5d93a237 3417 serial_send_break (rs->remote_desc);
c33e31fd 3418 remote_serial_write ("g", 1);
9a7071a8
JB
3419 }
3420 else
3421 internal_error (__FILE__, __LINE__,
3422 _("Invalid value for interrupt_sequence_mode: %s."),
3423 interrupt_sequence_mode);
3424}
3425
3405876a
PA
3426
3427/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3428 and extract the PTID. Returns NULL_PTID if not found. */
3429
3430static ptid_t
3431stop_reply_extract_thread (char *stop_reply)
3432{
3433 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3434 {
3435 char *p;
3436
3437 /* Txx r:val ; r:val (...) */
3438 p = &stop_reply[3];
3439
3440 /* Look for "register" named "thread". */
3441 while (*p != '\0')
3442 {
3443 char *p1;
3444
3445 p1 = strchr (p, ':');
3446 if (p1 == NULL)
3447 return null_ptid;
3448
3449 if (strncmp (p, "thread", p1 - p) == 0)
3450 return read_ptid (++p1, &p);
3451
3452 p1 = strchr (p, ';');
3453 if (p1 == NULL)
3454 return null_ptid;
3455 p1++;
3456
3457 p = p1;
3458 }
3459 }
3460
3461 return null_ptid;
3462}
3463
b7ea362b
PA
3464/* Determine the remote side's current thread. If we have a stop
3465 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3466 "thread" register we can extract the current thread from. If not,
3467 ask the remote which is the current thread with qC. The former
3468 method avoids a roundtrip. */
3469
3470static ptid_t
3471get_current_thread (char *wait_status)
3472{
3473 ptid_t ptid;
3474
3475 /* Note we don't use remote_parse_stop_reply as that makes use of
3476 the target architecture, which we haven't yet fully determined at
3477 this point. */
3478 if (wait_status != NULL)
3479 ptid = stop_reply_extract_thread (wait_status);
3480 if (ptid_equal (ptid, null_ptid))
3481 ptid = remote_current_thread (inferior_ptid);
3482
3483 return ptid;
3484}
3485
49c62f2e
PA
3486/* Query the remote target for which is the current thread/process,
3487 add it to our tables, and update INFERIOR_PTID. The caller is
3488 responsible for setting the state such that the remote end is ready
3405876a
PA
3489 to return the current thread.
3490
3491 This function is called after handling the '?' or 'vRun' packets,
3492 whose response is a stop reply from which we can also try
3493 extracting the thread. If the target doesn't support the explicit
3494 qC query, we infer the current thread from that stop reply, passed
3495 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3496
3497static void
3405876a 3498add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3499{
3500 struct remote_state *rs = get_remote_state ();
3501 int fake_pid_p = 0;
3405876a 3502 ptid_t ptid = null_ptid;
49c62f2e
PA
3503
3504 inferior_ptid = null_ptid;
3505
b7ea362b
PA
3506 /* Now, if we have thread information, update inferior_ptid. */
3507 ptid = get_current_thread (wait_status);
3405876a 3508
49c62f2e
PA
3509 if (!ptid_equal (ptid, null_ptid))
3510 {
3511 if (!remote_multi_process_p (rs))
3512 fake_pid_p = 1;
3513
3514 inferior_ptid = ptid;
3515 }
3516 else
3517 {
3518 /* Without this, some commands which require an active target
3519 (such as kill) won't work. This variable serves (at least)
3520 double duty as both the pid of the target process (if it has
3521 such), and as a flag indicating that a target is active. */
3522 inferior_ptid = magic_null_ptid;
3523 fake_pid_p = 1;
3524 }
3525
1b6e6f5c 3526 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3527
3528 /* Add the main thread. */
3529 add_thread_silent (inferior_ptid);
3530}
3531
9cbc821d 3532static void
04bd08de 3533remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3534{
c8d104ad
PA
3535 struct remote_state *rs = get_remote_state ();
3536 struct packet_config *noack_config;
2d717e4f 3537 char *wait_status = NULL;
8621d6a9 3538
23860348 3539 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3540 QUIT;
c906108c 3541
9a7071a8
JB
3542 if (interrupt_on_connect)
3543 send_interrupt_sequence ();
3544
57e12211 3545 /* Ack any packet which the remote side has already sent. */
5d93a237 3546 serial_write (rs->remote_desc, "+", 1);
57e12211 3547
1e51243a
PA
3548 /* Signal other parts that we're going through the initial setup,
3549 and so things may not be stable yet. */
3550 rs->starting_up = 1;
3551
c8d104ad
PA
3552 /* The first packet we send to the target is the optional "supported
3553 packets" request. If the target can answer this, it will tell us
3554 which later probes to skip. */
3555 remote_query_supported ();
3556
d914c394 3557 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3558 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3559 remote_set_permissions (target);
d914c394 3560
c8d104ad
PA
3561 /* Next, we possibly activate noack mode.
3562
3563 If the QStartNoAckMode packet configuration is set to AUTO,
3564 enable noack mode if the stub reported a wish for it with
3565 qSupported.
3566
3567 If set to TRUE, then enable noack mode even if the stub didn't
3568 report it in qSupported. If the stub doesn't reply OK, the
3569 session ends with an error.
3570
3571 If FALSE, then don't activate noack mode, regardless of what the
3572 stub claimed should be the default with qSupported. */
3573
3574 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3575 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3576 {
3577 putpkt ("QStartNoAckMode");
3578 getpkt (&rs->buf, &rs->buf_size, 0);
3579 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3580 rs->noack_mode = 1;
3581 }
3582
04bd08de 3583 if (extended_p)
5fe04517
PA
3584 {
3585 /* Tell the remote that we are using the extended protocol. */
3586 putpkt ("!");
3587 getpkt (&rs->buf, &rs->buf_size, 0);
3588 }
3589
9b224c5e
PA
3590 /* Let the target know which signals it is allowed to pass down to
3591 the program. */
3592 update_signals_program_target ();
3593
d962ef82
DJ
3594 /* Next, if the target can specify a description, read it. We do
3595 this before anything involving memory or registers. */
3596 target_find_description ();
3597
6c95b8df
PA
3598 /* Next, now that we know something about the target, update the
3599 address spaces in the program spaces. */
3600 update_address_spaces ();
3601
50c71eaf
PA
3602 /* On OSs where the list of libraries is global to all
3603 processes, we fetch them early. */
f5656ead 3604 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3605 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3606
74531fed
PA
3607 if (non_stop)
3608 {
4082afcc 3609 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3610 error (_("Non-stop mode requested, but remote "
3611 "does not support non-stop"));
74531fed
PA
3612
3613 putpkt ("QNonStop:1");
3614 getpkt (&rs->buf, &rs->buf_size, 0);
3615
3616 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3617 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3618
3619 /* Find about threads and processes the stub is already
3620 controlling. We default to adding them in the running state.
3621 The '?' query below will then tell us about which threads are
3622 stopped. */
e8032dde 3623 remote_update_thread_list (target);
74531fed 3624 }
4082afcc 3625 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3626 {
3627 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3628 Request it explicitly. */
74531fed
PA
3629 putpkt ("QNonStop:0");
3630 getpkt (&rs->buf, &rs->buf_size, 0);
3631
3632 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3633 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3634 }
3635
a0743c90
YQ
3636 /* Upload TSVs regardless of whether the target is running or not. The
3637 remote stub, such as GDBserver, may have some predefined or builtin
3638 TSVs, even if the target is not running. */
8bd200f1 3639 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3640 {
3641 struct uploaded_tsv *uploaded_tsvs = NULL;
3642
181e3713 3643 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3644 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3645 }
3646
2d717e4f
DJ
3647 /* Check whether the target is running now. */
3648 putpkt ("?");
3649 getpkt (&rs->buf, &rs->buf_size, 0);
3650
74531fed 3651 if (!non_stop)
2d717e4f 3652 {
e714e1bf
UW
3653 ptid_t ptid;
3654 int fake_pid_p = 0;
3655 struct inferior *inf;
3656
74531fed 3657 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3658 {
04bd08de 3659 if (!extended_p)
74531fed 3660 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3661
3662 /* We're connected, but not running. Drop out before we
3663 call start_remote. */
e278ad5b 3664 rs->starting_up = 0;
c35b1492 3665 return;
2d717e4f
DJ
3666 }
3667 else
74531fed 3668 {
74531fed
PA
3669 /* Save the reply for later. */
3670 wait_status = alloca (strlen (rs->buf) + 1);
3671 strcpy (wait_status, rs->buf);
3672 }
3673
b7ea362b 3674 /* Fetch thread list. */
e8032dde 3675 target_update_thread_list ();
b7ea362b 3676
74531fed
PA
3677 /* Let the stub know that we want it to return the thread. */
3678 set_continue_thread (minus_one_ptid);
3679
b7ea362b
PA
3680 if (thread_count () == 0)
3681 {
3682 /* Target has no concept of threads at all. GDB treats
3683 non-threaded target as single-threaded; add a main
3684 thread. */
3685 add_current_inferior_and_thread (wait_status);
3686 }
3687 else
3688 {
3689 /* We have thread information; select the thread the target
3690 says should be current. If we're reconnecting to a
3691 multi-threaded program, this will ideally be the thread
3692 that last reported an event before GDB disconnected. */
3693 inferior_ptid = get_current_thread (wait_status);
3694 if (ptid_equal (inferior_ptid, null_ptid))
3695 {
3696 /* Odd... The target was able to list threads, but not
3697 tell us which thread was current (no "thread"
3698 register in T stop reply?). Just pick the first
3699 thread in the thread list then. */
3700 inferior_ptid = thread_list->ptid;
3701 }
3702 }
74531fed 3703
6e586cc5
YQ
3704 /* init_wait_for_inferior should be called before get_offsets in order
3705 to manage `inserted' flag in bp loc in a correct state.
3706 breakpoint_init_inferior, called from init_wait_for_inferior, set
3707 `inserted' flag to 0, while before breakpoint_re_set, called from
3708 start_remote, set `inserted' flag to 1. In the initialization of
3709 inferior, breakpoint_init_inferior should be called first, and then
3710 breakpoint_re_set can be called. If this order is broken, state of
3711 `inserted' flag is wrong, and cause some problems on breakpoint
3712 manipulation. */
3713 init_wait_for_inferior ();
3714
74531fed
PA
3715 get_offsets (); /* Get text, data & bss offsets. */
3716
d962ef82
DJ
3717 /* If we could not find a description using qXfer, and we know
3718 how to do it some other way, try again. This is not
3719 supported for non-stop; it could be, but it is tricky if
3720 there are no stopped threads when we connect. */
04bd08de 3721 if (remote_read_description_p (target)
f5656ead 3722 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3723 {
3724 target_clear_description ();
3725 target_find_description ();
3726 }
3727
74531fed
PA
3728 /* Use the previously fetched status. */
3729 gdb_assert (wait_status != NULL);
3730 strcpy (rs->buf, wait_status);
3731 rs->cached_wait_status = 1;
3732
3733 immediate_quit--;
04bd08de 3734 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3735 }
3736 else
3737 {
68c97600
PA
3738 /* Clear WFI global state. Do this before finding about new
3739 threads and inferiors, and setting the current inferior.
3740 Otherwise we would clear the proceed status of the current
3741 inferior when we want its stop_soon state to be preserved
3742 (see notice_new_inferior). */
3743 init_wait_for_inferior ();
3744
74531fed
PA
3745 /* In non-stop, we will either get an "OK", meaning that there
3746 are no stopped threads at this time; or, a regular stop
3747 reply. In the latter case, there may be more than one thread
3748 stopped --- we pull them all out using the vStopped
3749 mechanism. */
3750 if (strcmp (rs->buf, "OK") != 0)
3751 {
722247f1 3752 struct notif_client *notif = &notif_client_stop;
2d717e4f 3753
722247f1
YQ
3754 /* remote_notif_get_pending_replies acks this one, and gets
3755 the rest out. */
f48ff2a7 3756 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3757 = remote_notif_parse (notif, rs->buf);
3758 remote_notif_get_pending_events (notif);
c906108c 3759
74531fed
PA
3760 /* Make sure that threads that were stopped remain
3761 stopped. */
3762 iterate_over_threads (set_stop_requested_callback, NULL);
3763 }
2d717e4f 3764
74531fed 3765 if (target_can_async_p ())
6a3753b3 3766 target_async (1);
c906108c 3767
74531fed
PA
3768 if (thread_count () == 0)
3769 {
04bd08de 3770 if (!extended_p)
74531fed 3771 error (_("The target is not running (try extended-remote?)"));
82f73884 3772
c35b1492
PA
3773 /* We're connected, but not running. Drop out before we
3774 call start_remote. */
e278ad5b 3775 rs->starting_up = 0;
c35b1492
PA
3776 return;
3777 }
74531fed
PA
3778
3779 /* Let the stub know that we want it to return the thread. */
c0a2216e 3780
74531fed
PA
3781 /* Force the stub to choose a thread. */
3782 set_general_thread (null_ptid);
c906108c 3783
74531fed
PA
3784 /* Query it. */
3785 inferior_ptid = remote_current_thread (minus_one_ptid);
3786 if (ptid_equal (inferior_ptid, minus_one_ptid))
3787 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3788
74531fed
PA
3789 get_offsets (); /* Get text, data & bss offsets. */
3790
3791 /* In non-stop mode, any cached wait status will be stored in
3792 the stop reply queue. */
3793 gdb_assert (wait_status == NULL);
f0223081 3794
2455069d 3795 /* Report all signals during attach/startup. */
94bedb42 3796 remote_pass_signals (target, 0, NULL);
74531fed 3797 }
c8d104ad 3798
c8d104ad
PA
3799 /* If we connected to a live target, do some additional setup. */
3800 if (target_has_execution)
3801 {
f4ccffad 3802 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3803 remote_check_symbols ();
c8d104ad 3804 }
50c71eaf 3805
d5551862
SS
3806 /* Possibly the target has been engaged in a trace run started
3807 previously; find out where things are at. */
8bd200f1 3808 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3809 {
00bf0b85 3810 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3811
00bf0b85
SS
3812 if (current_trace_status ()->running)
3813 printf_filtered (_("Trace is already running on the target.\n"));
3814
ab6617cc 3815 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3816
3817 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3818 }
3819
1e51243a
PA
3820 /* The thread and inferior lists are now synchronized with the
3821 target, our symbols have been relocated, and we're merged the
3822 target's tracepoints with ours. We're done with basic start
3823 up. */
3824 rs->starting_up = 0;
3825
a25a5a45
PA
3826 /* Maybe breakpoints are global and need to be inserted now. */
3827 if (breakpoints_should_be_inserted_now ())
50c71eaf 3828 insert_breakpoints ();
c906108c
SS
3829}
3830
3831/* Open a connection to a remote debugger.
3832 NAME is the filename used for communication. */
3833
3834static void
014f9477 3835remote_open (const char *name, int from_tty)
c906108c 3836{
75c99385 3837 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3838}
3839
c906108c
SS
3840/* Open a connection to a remote debugger using the extended
3841 remote gdb protocol. NAME is the filename used for communication. */
3842
3843static void
014f9477 3844extended_remote_open (const char *name, int from_tty)
c906108c 3845{
75c99385 3846 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3847}
3848
ca4f7f8b
PA
3849/* Reset all packets back to "unknown support". Called when opening a
3850 new connection to a remote target. */
c906108c 3851
d471ea57 3852static void
ca4f7f8b 3853reset_all_packet_configs_support (void)
d471ea57
AC
3854{
3855 int i;
a744cf53 3856
444abaca 3857 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3858 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3859}
3860
ca4f7f8b
PA
3861/* Initialize all packet configs. */
3862
3863static void
3864init_all_packet_configs (void)
3865{
3866 int i;
3867
3868 for (i = 0; i < PACKET_MAX; i++)
3869 {
3870 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3871 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3872 }
3873}
3874
23860348 3875/* Symbol look-up. */
dc8acb97
MS
3876
3877static void
36d25514 3878remote_check_symbols (void)
dc8acb97 3879{
d01949b6 3880 struct remote_state *rs = get_remote_state ();
dc8acb97 3881 char *msg, *reply, *tmp;
3b7344d5 3882 struct bound_minimal_symbol sym;
dc8acb97
MS
3883 int end;
3884
63154eca
PA
3885 /* The remote side has no concept of inferiors that aren't running
3886 yet, it only knows about running processes. If we're connected
3887 but our current inferior is not running, we should not invite the
3888 remote target to request symbol lookups related to its
3889 (unrelated) current process. */
3890 if (!target_has_execution)
3891 return;
3892
4082afcc 3893 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3894 return;
3895
63154eca
PA
3896 /* Make sure the remote is pointing at the right process. Note
3897 there's no way to select "no process". */
3c9c4b83
PA
3898 set_general_process ();
3899
6d820c5c
DJ
3900 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3901 because we need both at the same time. */
ea9c271d 3902 msg = alloca (get_remote_packet_size ());
6d820c5c 3903
23860348 3904 /* Invite target to request symbol lookups. */
dc8acb97
MS
3905
3906 putpkt ("qSymbol::");
6d820c5c
DJ
3907 getpkt (&rs->buf, &rs->buf_size, 0);
3908 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3909 reply = rs->buf;
dc8acb97 3910
61012eef 3911 while (startswith (reply, "qSymbol:"))
dc8acb97 3912 {
77e371c0
TT
3913 struct bound_minimal_symbol sym;
3914
dc8acb97 3915 tmp = &reply[8];
cfd77fa1 3916 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3917 msg[end] = '\0';
3918 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3919 if (sym.minsym == NULL)
ea9c271d 3920 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3921 else
2bbe3cc1 3922 {
f5656ead 3923 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3924 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3925
3926 /* If this is a function address, return the start of code
3927 instead of any data function descriptor. */
f5656ead 3928 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3929 sym_addr,
3930 &current_target);
3931
3932 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3933 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3934 }
3935
dc8acb97 3936 putpkt (msg);
6d820c5c 3937 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3938 reply = rs->buf;
dc8acb97
MS
3939 }
3940}
3941
9db8d71f 3942static struct serial *
baa336ce 3943remote_serial_open (const char *name)
9db8d71f
DJ
3944{
3945 static int udp_warning = 0;
3946
3947 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3948 of in ser-tcp.c, because it is the remote protocol assuming that the
3949 serial connection is reliable and not the serial connection promising
3950 to be. */
61012eef 3951 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3952 {
3e43a32a
MS
3953 warning (_("The remote protocol may be unreliable over UDP.\n"
3954 "Some events may be lost, rendering further debugging "
3955 "impossible."));
9db8d71f
DJ
3956 udp_warning = 1;
3957 }
3958
3959 return serial_open (name);
3960}
3961
d914c394
SS
3962/* Inform the target of our permission settings. The permission flags
3963 work without this, but if the target knows the settings, it can do
3964 a couple things. First, it can add its own check, to catch cases
3965 that somehow manage to get by the permissions checks in target
3966 methods. Second, if the target is wired to disallow particular
3967 settings (for instance, a system in the field that is not set up to
3968 be able to stop at a breakpoint), it can object to any unavailable
3969 permissions. */
3970
3971void
c378d69d 3972remote_set_permissions (struct target_ops *self)
d914c394
SS
3973{
3974 struct remote_state *rs = get_remote_state ();
3975
bba74b36
YQ
3976 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3977 "WriteReg:%x;WriteMem:%x;"
3978 "InsertBreak:%x;InsertTrace:%x;"
3979 "InsertFastTrace:%x;Stop:%x",
3980 may_write_registers, may_write_memory,
3981 may_insert_breakpoints, may_insert_tracepoints,
3982 may_insert_fast_tracepoints, may_stop);
d914c394
SS
3983 putpkt (rs->buf);
3984 getpkt (&rs->buf, &rs->buf_size, 0);
3985
3986 /* If the target didn't like the packet, warn the user. Do not try
3987 to undo the user's settings, that would just be maddening. */
3988 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 3989 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
3990}
3991
be2a5f71
DJ
3992/* This type describes each known response to the qSupported
3993 packet. */
3994struct protocol_feature
3995{
3996 /* The name of this protocol feature. */
3997 const char *name;
3998
3999 /* The default for this protocol feature. */
4000 enum packet_support default_support;
4001
4002 /* The function to call when this feature is reported, or after
4003 qSupported processing if the feature is not supported.
4004 The first argument points to this structure. The second
4005 argument indicates whether the packet requested support be
4006 enabled, disabled, or probed (or the default, if this function
4007 is being called at the end of processing and this feature was
4008 not reported). The third argument may be NULL; if not NULL, it
4009 is a NUL-terminated string taken from the packet following
4010 this feature's name and an equals sign. */
4011 void (*func) (const struct protocol_feature *, enum packet_support,
4012 const char *);
4013
4014 /* The corresponding packet for this feature. Only used if
4015 FUNC is remote_supported_packet. */
4016 int packet;
4017};
4018
be2a5f71
DJ
4019static void
4020remote_supported_packet (const struct protocol_feature *feature,
4021 enum packet_support support,
4022 const char *argument)
4023{
4024 if (argument)
4025 {
4026 warning (_("Remote qSupported response supplied an unexpected value for"
4027 " \"%s\"."), feature->name);
4028 return;
4029 }
4030
4082afcc 4031 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4032}
be2a5f71
DJ
4033
4034static void
4035remote_packet_size (const struct protocol_feature *feature,
4036 enum packet_support support, const char *value)
4037{
4038 struct remote_state *rs = get_remote_state ();
4039
4040 int packet_size;
4041 char *value_end;
4042
4043 if (support != PACKET_ENABLE)
4044 return;
4045
4046 if (value == NULL || *value == '\0')
4047 {
4048 warning (_("Remote target reported \"%s\" without a size."),
4049 feature->name);
4050 return;
4051 }
4052
4053 errno = 0;
4054 packet_size = strtol (value, &value_end, 16);
4055 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4056 {
4057 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4058 feature->name, value);
4059 return;
4060 }
4061
4062 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4063 {
4064 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4065 packet_size, MAX_REMOTE_PACKET_SIZE);
4066 packet_size = MAX_REMOTE_PACKET_SIZE;
4067 }
4068
4069 /* Record the new maximum packet size. */
4070 rs->explicit_packet_size = packet_size;
4071}
4072
dc473cfb 4073static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4074 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4075 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4076 PACKET_qXfer_auxv },
c78fa86a
GB
4077 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4078 PACKET_qXfer_exec_file },
23181151
DJ
4079 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4080 PACKET_qXfer_features },
cfa9d6d9
DJ
4081 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4082 PACKET_qXfer_libraries },
2268b414
JK
4083 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4084 PACKET_qXfer_libraries_svr4 },
ced63ec0 4085 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4086 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4087 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4088 PACKET_qXfer_memory_map },
4de6483e
UW
4089 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4090 PACKET_qXfer_spu_read },
4091 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4092 PACKET_qXfer_spu_write },
07e059b5
VP
4093 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4094 PACKET_qXfer_osdata },
dc146f7c
VP
4095 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4096 PACKET_qXfer_threads },
b3b9301e
PA
4097 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4098 PACKET_qXfer_traceframe_info },
89be2091
DJ
4099 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4100 PACKET_QPassSignals },
9b224c5e
PA
4101 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4102 PACKET_QProgramSignals },
a6f3e723
SL
4103 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4104 PACKET_QStartNoAckMode },
4082afcc
PA
4105 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4106 PACKET_multiprocess_feature },
4107 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4108 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4109 PACKET_qXfer_siginfo_read },
4110 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4111 PACKET_qXfer_siginfo_write },
4082afcc 4112 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4113 PACKET_ConditionalTracepoints },
4082afcc 4114 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4115 PACKET_ConditionalBreakpoints },
4082afcc 4116 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4117 PACKET_BreakpointCommands },
4082afcc 4118 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4119 PACKET_FastTracepoints },
4082afcc 4120 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4121 PACKET_StaticTracepoints },
4082afcc 4122 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4123 PACKET_InstallInTrace},
4082afcc
PA
4124 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4125 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4126 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4127 PACKET_bc },
4128 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4129 PACKET_bs },
409873ef
SS
4130 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4131 PACKET_TracepointSource },
d914c394
SS
4132 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4133 PACKET_QAllow },
4082afcc
PA
4134 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4135 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4136 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4137 PACKET_qXfer_fdpic },
169081d0
TG
4138 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4139 PACKET_qXfer_uib },
03583c20
UW
4140 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4141 PACKET_QDisableRandomization },
d1feda86 4142 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4143 { "QTBuffer:size", PACKET_DISABLE,
4144 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4145 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4146 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4147 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4148 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4149 PACKET_qXfer_btrace },
4150 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4151 PACKET_qXfer_btrace_conf },
4152 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4153 PACKET_Qbtrace_conf_bts_size },
4154 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4155 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4156 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4157 PACKET_fork_event_feature },
4158 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4159 PACKET_vfork_event_feature },
be2a5f71
DJ
4160};
4161
c8d5aac9
L
4162static char *remote_support_xml;
4163
4164/* Register string appended to "xmlRegisters=" in qSupported query. */
4165
4166void
6e39997a 4167register_remote_support_xml (const char *xml)
c8d5aac9
L
4168{
4169#if defined(HAVE_LIBEXPAT)
4170 if (remote_support_xml == NULL)
c4f7c687 4171 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4172 else
4173 {
4174 char *copy = xstrdup (remote_support_xml + 13);
4175 char *p = strtok (copy, ",");
4176
4177 do
4178 {
4179 if (strcmp (p, xml) == 0)
4180 {
4181 /* already there */
4182 xfree (copy);
4183 return;
4184 }
4185 }
4186 while ((p = strtok (NULL, ",")) != NULL);
4187 xfree (copy);
4188
94b0dee1
PA
4189 remote_support_xml = reconcat (remote_support_xml,
4190 remote_support_xml, ",", xml,
4191 (char *) NULL);
c8d5aac9
L
4192 }
4193#endif
4194}
4195
4196static char *
4197remote_query_supported_append (char *msg, const char *append)
4198{
4199 if (msg)
94b0dee1 4200 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4201 else
4202 return xstrdup (append);
4203}
4204
be2a5f71
DJ
4205static void
4206remote_query_supported (void)
4207{
4208 struct remote_state *rs = get_remote_state ();
4209 char *next;
4210 int i;
4211 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4212
4213 /* The packet support flags are handled differently for this packet
4214 than for most others. We treat an error, a disabled packet, and
4215 an empty response identically: any features which must be reported
4216 to be used will be automatically disabled. An empty buffer
4217 accomplishes this, since that is also the representation for a list
4218 containing no features. */
4219
4220 rs->buf[0] = 0;
4082afcc 4221 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4222 {
c8d5aac9 4223 char *q = NULL;
94b0dee1 4224 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4225
901f9912 4226 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4227
f7e6eed5
PA
4228 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4229 q = remote_query_supported_append (q, "swbreak+");
4230 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4231 q = remote_query_supported_append (q, "hwbreak+");
4232
c8d5aac9
L
4233 if (remote_support_xml)
4234 q = remote_query_supported_append (q, remote_support_xml);
4235
dde08ee1
PA
4236 q = remote_query_supported_append (q, "qRelocInsn+");
4237
89245bc0
DB
4238 if (rs->extended)
4239 {
4240 if (packet_set_cmd_state (PACKET_fork_event_feature)
4241 != AUTO_BOOLEAN_FALSE)
4242 q = remote_query_supported_append (q, "fork-events+");
4243 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4244 != AUTO_BOOLEAN_FALSE)
4245 q = remote_query_supported_append (q, "vfork-events+");
4246 }
4247
dde08ee1
PA
4248 q = reconcat (q, "qSupported:", q, (char *) NULL);
4249 putpkt (q);
82f73884 4250
94b0dee1
PA
4251 do_cleanups (old_chain);
4252
be2a5f71
DJ
4253 getpkt (&rs->buf, &rs->buf_size, 0);
4254
4255 /* If an error occured, warn, but do not return - just reset the
4256 buffer to empty and go on to disable features. */
4257 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4258 == PACKET_ERROR)
4259 {
4260 warning (_("Remote failure reply: %s"), rs->buf);
4261 rs->buf[0] = 0;
4262 }
4263 }
4264
4265 memset (seen, 0, sizeof (seen));
4266
4267 next = rs->buf;
4268 while (*next)
4269 {
4270 enum packet_support is_supported;
4271 char *p, *end, *name_end, *value;
4272
4273 /* First separate out this item from the rest of the packet. If
4274 there's another item after this, we overwrite the separator
4275 (terminated strings are much easier to work with). */
4276 p = next;
4277 end = strchr (p, ';');
4278 if (end == NULL)
4279 {
4280 end = p + strlen (p);
4281 next = end;
4282 }
4283 else
4284 {
89be2091
DJ
4285 *end = '\0';
4286 next = end + 1;
4287
be2a5f71
DJ
4288 if (end == p)
4289 {
4290 warning (_("empty item in \"qSupported\" response"));
4291 continue;
4292 }
be2a5f71
DJ
4293 }
4294
4295 name_end = strchr (p, '=');
4296 if (name_end)
4297 {
4298 /* This is a name=value entry. */
4299 is_supported = PACKET_ENABLE;
4300 value = name_end + 1;
4301 *name_end = '\0';
4302 }
4303 else
4304 {
4305 value = NULL;
4306 switch (end[-1])
4307 {
4308 case '+':
4309 is_supported = PACKET_ENABLE;
4310 break;
4311
4312 case '-':
4313 is_supported = PACKET_DISABLE;
4314 break;
4315
4316 case '?':
4317 is_supported = PACKET_SUPPORT_UNKNOWN;
4318 break;
4319
4320 default:
3e43a32a
MS
4321 warning (_("unrecognized item \"%s\" "
4322 "in \"qSupported\" response"), p);
be2a5f71
DJ
4323 continue;
4324 }
4325 end[-1] = '\0';
4326 }
4327
4328 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4329 if (strcmp (remote_protocol_features[i].name, p) == 0)
4330 {
4331 const struct protocol_feature *feature;
4332
4333 seen[i] = 1;
4334 feature = &remote_protocol_features[i];
4335 feature->func (feature, is_supported, value);
4336 break;
4337 }
4338 }
4339
4340 /* If we increased the packet size, make sure to increase the global
4341 buffer size also. We delay this until after parsing the entire
4342 qSupported packet, because this is the same buffer we were
4343 parsing. */
4344 if (rs->buf_size < rs->explicit_packet_size)
4345 {
4346 rs->buf_size = rs->explicit_packet_size;
4347 rs->buf = xrealloc (rs->buf, rs->buf_size);
4348 }
4349
4350 /* Handle the defaults for unmentioned features. */
4351 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4352 if (!seen[i])
4353 {
4354 const struct protocol_feature *feature;
4355
4356 feature = &remote_protocol_features[i];
4357 feature->func (feature, feature->default_support, NULL);
4358 }
4359}
4360
78a095c3
JK
4361/* Remove any of the remote.c targets from target stack. Upper targets depend
4362 on it so remove them first. */
4363
4364static void
4365remote_unpush_target (void)
4366{
4367 pop_all_targets_above (process_stratum - 1);
4368}
be2a5f71 4369
c906108c 4370static void
014f9477 4371remote_open_1 (const char *name, int from_tty,
3e43a32a 4372 struct target_ops *target, int extended_p)
c906108c 4373{
d01949b6 4374 struct remote_state *rs = get_remote_state ();
a6f3e723 4375
c906108c 4376 if (name == 0)
8a3fe4f8 4377 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4378 "serial device is attached to the remote system\n"
8a3fe4f8 4379 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4380
23860348 4381 /* See FIXME above. */
c6ebd6cf 4382 if (!target_async_permitted)
92d1e331 4383 wait_forever_enabled_p = 1;
6426a772 4384
2d717e4f 4385 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4386 Ask this question first, before target_preopen has a chance to kill
4387 anything. */
5d93a237 4388 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4389 {
78a095c3
JK
4390 if (from_tty
4391 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4392 error (_("Still connected."));
4393 }
4394
78a095c3 4395 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4396 target_preopen (from_tty);
4397
89be2091 4398 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4399 xfree (rs->last_pass_packet);
4400 rs->last_pass_packet = NULL;
89be2091 4401
9b224c5e
PA
4402 /* Make sure we send the program signals list the next time we
4403 resume. */
5e4a05c4
TT
4404 xfree (rs->last_program_signals_packet);
4405 rs->last_program_signals_packet = NULL;
9b224c5e 4406
ad9a8f3f 4407 remote_fileio_reset ();
1dd41f16
NS
4408 reopen_exec_file ();
4409 reread_symbols ();
4410
5d93a237
TT
4411 rs->remote_desc = remote_serial_open (name);
4412 if (!rs->remote_desc)
c906108c
SS
4413 perror_with_name (name);
4414
4415 if (baud_rate != -1)
4416 {
5d93a237 4417 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4418 {
9b74d5d3
KB
4419 /* The requested speed could not be set. Error out to
4420 top level after closing remote_desc. Take care to
4421 set remote_desc to NULL to avoid closing remote_desc
4422 more than once. */
5d93a237
TT
4423 serial_close (rs->remote_desc);
4424 rs->remote_desc = NULL;
c906108c
SS
4425 perror_with_name (name);
4426 }
4427 }
4428
236af5e3 4429 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4430 serial_raw (rs->remote_desc);
c906108c
SS
4431
4432 /* If there is something sitting in the buffer we might take it as a
4433 response to a command, which would be bad. */
5d93a237 4434 serial_flush_input (rs->remote_desc);
c906108c
SS
4435
4436 if (from_tty)
4437 {
4438 puts_filtered ("Remote debugging using ");
4439 puts_filtered (name);
4440 puts_filtered ("\n");
4441 }
23860348 4442 push_target (target); /* Switch to using remote target now. */
c906108c 4443
74531fed
PA
4444 /* Register extra event sources in the event loop. */
4445 remote_async_inferior_event_token
4446 = create_async_event_handler (remote_async_inferior_event_handler,
4447 NULL);
5965e028 4448 rs->notif_state = remote_notif_state_allocate ();
74531fed 4449
be2a5f71
DJ
4450 /* Reset the target state; these things will be queried either by
4451 remote_query_supported or as they are needed. */
ca4f7f8b 4452 reset_all_packet_configs_support ();
74531fed 4453 rs->cached_wait_status = 0;
be2a5f71 4454 rs->explicit_packet_size = 0;
a6f3e723 4455 rs->noack_mode = 0;
82f73884 4456 rs->extended = extended_p;
e24a49d8 4457 rs->waiting_for_stop_reply = 0;
3a29589a 4458 rs->ctrlc_pending_p = 0;
802188a7 4459
47f8a51d
TT
4460 rs->general_thread = not_sent_ptid;
4461 rs->continue_thread = not_sent_ptid;
262e1174 4462 rs->remote_traceframe_number = -1;
c906108c 4463
9d1f7ab2 4464 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4465 rs->use_threadinfo_query = 1;
4466 rs->use_threadextra_query = 1;
9d1f7ab2 4467
c6ebd6cf 4468 if (target_async_permitted)
92d1e331 4469 {
23860348 4470 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4471 remote_async_terminal_ours_p = 1;
4472
4473 /* FIXME: cagney/1999-09-23: During the initial connection it is
4474 assumed that the target is already ready and able to respond to
0df8b418 4475 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4476 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4477 around this. Eventually a mechanism that allows
92d1e331 4478 wait_for_inferior() to expect/get timeouts will be
23860348 4479 implemented. */
92d1e331
DJ
4480 wait_forever_enabled_p = 0;
4481 }
4482
23860348 4483 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4484 no_shared_libraries (NULL, 0);
f78f6cf1 4485
74531fed
PA
4486 /* Start afresh. */
4487 init_thread_list ();
4488
36918e70 4489 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4490 target (we'd otherwise be in an inconsistent state) and then
4491 propogate the error on up the exception chain. This ensures that
4492 the caller doesn't stumble along blindly assuming that the
4493 function succeeded. The CLI doesn't have this problem but other
4494 UI's, such as MI do.
36918e70
AC
4495
4496 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4497 this function should return an error indication letting the
ce2826aa 4498 caller restore the previous state. Unfortunately the command
36918e70
AC
4499 ``target remote'' is directly wired to this function making that
4500 impossible. On a positive note, the CLI side of this problem has
4501 been fixed - the function set_cmd_context() makes it possible for
4502 all the ``target ....'' commands to share a common callback
4503 function. See cli-dump.c. */
109c3e39 4504 {
2d717e4f 4505
492d29ea 4506 TRY
04bd08de
TT
4507 {
4508 remote_start_remote (from_tty, target, extended_p);
4509 }
492d29ea 4510 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4511 {
c8d104ad
PA
4512 /* Pop the partially set up target - unless something else did
4513 already before throwing the exception. */
5d93a237 4514 if (rs->remote_desc != NULL)
78a095c3 4515 remote_unpush_target ();
c6ebd6cf 4516 if (target_async_permitted)
109c3e39
AC
4517 wait_forever_enabled_p = 1;
4518 throw_exception (ex);
4519 }
492d29ea 4520 END_CATCH
109c3e39 4521 }
c906108c 4522
f4abbc16
MM
4523 remote_btrace_reset ();
4524
c6ebd6cf 4525 if (target_async_permitted)
92d1e331 4526 wait_forever_enabled_p = 1;
43ff13b4
JM
4527}
4528
de0d863e
DB
4529/* Detach the specified process. */
4530
4531static void
4532remote_detach_pid (int pid)
4533{
4534 struct remote_state *rs = get_remote_state ();
4535
4536 if (remote_multi_process_p (rs))
4537 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4538 else
4539 strcpy (rs->buf, "D");
4540
4541 putpkt (rs->buf);
4542 getpkt (&rs->buf, &rs->buf_size, 0);
4543
4544 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4545 ;
4546 else if (rs->buf[0] == '\0')
4547 error (_("Remote doesn't know how to detach"));
4548 else
4549 error (_("Can't detach process."));
4550}
4551
4552/* This detaches a program to which we previously attached, using
4553 inferior_ptid to identify the process. After this is done, GDB
4554 can be used to debug some other program. We better not have left
4555 any breakpoints in the target program or it'll die when it hits
4556 one. */
c906108c
SS
4557
4558static void
de0d863e 4559remote_detach_1 (const char *args, int from_tty)
c906108c 4560{
82f73884 4561 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4562 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4563 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4564 int is_fork_parent;
c906108c
SS
4565
4566 if (args)
8a3fe4f8 4567 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4568
2d717e4f
DJ
4569 if (!target_has_execution)
4570 error (_("No process to detach from."));
4571
7cee1e54
PA
4572 if (from_tty)
4573 {
4574 char *exec_file = get_exec_file (0);
4575 if (exec_file == NULL)
4576 exec_file = "";
4577 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4578 target_pid_to_str (pid_to_ptid (pid)));
4579 gdb_flush (gdb_stdout);
4580 }
4581
c906108c 4582 /* Tell the remote target to detach. */
de0d863e 4583 remote_detach_pid (pid);
82f73884 4584
de0d863e 4585 if (from_tty && !rs->extended)
7cee1e54 4586 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4587
de0d863e
DB
4588 /* Check to see if we are detaching a fork parent. Note that if we
4589 are detaching a fork child, tp == NULL. */
4590 is_fork_parent = (tp != NULL
4591 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4592
4593 /* If doing detach-on-fork, we don't mourn, because that will delete
4594 breakpoints that should be available for the followed inferior. */
4595 if (!is_fork_parent)
4596 target_mourn_inferior ();
4597 else
4598 {
4599 inferior_ptid = null_ptid;
4600 detach_inferior (pid);
4601 }
2d717e4f
DJ
4602}
4603
4604static void
52554a0e 4605remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4606{
de0d863e 4607 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4608}
4609
4610static void
52554a0e 4611extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4612{
de0d863e
DB
4613 remote_detach_1 (args, from_tty);
4614}
4615
4616/* Target follow-fork function for remote targets. On entry, and
4617 at return, the current inferior is the fork parent.
4618
4619 Note that although this is currently only used for extended-remote,
4620 it is named remote_follow_fork in anticipation of using it for the
4621 remote target as well. */
4622
4623static int
4624remote_follow_fork (struct target_ops *ops, int follow_child,
4625 int detach_fork)
4626{
4627 struct remote_state *rs = get_remote_state ();
c269dbdb 4628 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4629
c269dbdb
DB
4630 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4631 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4632 {
4633 /* When following the parent and detaching the child, we detach
4634 the child here. For the case of following the child and
4635 detaching the parent, the detach is done in the target-
4636 independent follow fork code in infrun.c. We can't use
4637 target_detach when detaching an unfollowed child because
4638 the client side doesn't know anything about the child. */
4639 if (detach_fork && !follow_child)
4640 {
4641 /* Detach the fork child. */
4642 ptid_t child_ptid;
4643 pid_t child_pid;
4644
4645 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4646 child_pid = ptid_get_pid (child_ptid);
4647
4648 remote_detach_pid (child_pid);
4649 detach_inferior (child_pid);
4650 }
4651 }
4652 return 0;
c906108c
SS
4653}
4654
6ad8ae5c
DJ
4655/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4656
43ff13b4 4657static void
fee354ee 4658remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4659{
43ff13b4 4660 if (args)
2d717e4f 4661 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4662
2d717e4f 4663 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4664 won't do it. So call remote_mourn directly instead of
2d717e4f 4665 target_mourn_inferior. */
20f796c9 4666 remote_mourn (target);
2d717e4f 4667
43ff13b4
JM
4668 if (from_tty)
4669 puts_filtered ("Ending remote debugging.\n");
4670}
4671
2d717e4f
DJ
4672/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4673 be chatty about it. */
4674
4675static void
20f796c9
GB
4676extended_remote_attach (struct target_ops *target, const char *args,
4677 int from_tty)
2d717e4f
DJ
4678{
4679 struct remote_state *rs = get_remote_state ();
be86555c 4680 int pid;
96ef3384 4681 char *wait_status = NULL;
2d717e4f 4682
74164c56 4683 pid = parse_pid_to_attach (args);
2d717e4f 4684
74164c56
JK
4685 /* Remote PID can be freely equal to getpid, do not check it here the same
4686 way as in other targets. */
2d717e4f 4687
4082afcc 4688 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4689 error (_("This target does not support attaching to a process"));
4690
7cee1e54
PA
4691 if (from_tty)
4692 {
4693 char *exec_file = get_exec_file (0);
4694
4695 if (exec_file)
4696 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4697 target_pid_to_str (pid_to_ptid (pid)));
4698 else
4699 printf_unfiltered (_("Attaching to %s\n"),
4700 target_pid_to_str (pid_to_ptid (pid)));
4701
4702 gdb_flush (gdb_stdout);
4703 }
4704
bba74b36 4705 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4706 putpkt (rs->buf);
4707 getpkt (&rs->buf, &rs->buf_size, 0);
4708
4082afcc
PA
4709 switch (packet_ok (rs->buf,
4710 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4711 {
4082afcc 4712 case PACKET_OK:
74531fed
PA
4713 if (!non_stop)
4714 {
4715 /* Save the reply for later. */
4716 wait_status = alloca (strlen (rs->buf) + 1);
4717 strcpy (wait_status, rs->buf);
4718 }
4719 else if (strcmp (rs->buf, "OK") != 0)
4720 error (_("Attaching to %s failed with: %s"),
4721 target_pid_to_str (pid_to_ptid (pid)),
4722 rs->buf);
4082afcc
PA
4723 break;
4724 case PACKET_UNKNOWN:
4725 error (_("This target does not support attaching to a process"));
4726 default:
4727 error (_("Attaching to %s failed"),
4728 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4729 }
2d717e4f 4730
1b6e6f5c 4731 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4732
2d717e4f 4733 inferior_ptid = pid_to_ptid (pid);
79d7f229 4734
bad34192
PA
4735 if (non_stop)
4736 {
4737 struct thread_info *thread;
79d7f229 4738
bad34192 4739 /* Get list of threads. */
e8032dde 4740 remote_update_thread_list (target);
82f73884 4741
bad34192
PA
4742 thread = first_thread_of_process (pid);
4743 if (thread)
4744 inferior_ptid = thread->ptid;
4745 else
4746 inferior_ptid = pid_to_ptid (pid);
4747
4748 /* Invalidate our notion of the remote current thread. */
47f8a51d 4749 record_currthread (rs, minus_one_ptid);
bad34192 4750 }
74531fed 4751 else
bad34192
PA
4752 {
4753 /* Now, if we have thread information, update inferior_ptid. */
4754 inferior_ptid = remote_current_thread (inferior_ptid);
4755
4756 /* Add the main thread to the thread list. */
4757 add_thread_silent (inferior_ptid);
4758 }
c0a2216e 4759
96ef3384
UW
4760 /* Next, if the target can specify a description, read it. We do
4761 this before anything involving memory or registers. */
4762 target_find_description ();
4763
74531fed
PA
4764 if (!non_stop)
4765 {
4766 /* Use the previously fetched status. */
4767 gdb_assert (wait_status != NULL);
4768
4769 if (target_can_async_p ())
4770 {
722247f1
YQ
4771 struct notif_event *reply
4772 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4773
722247f1 4774 push_stop_reply ((struct stop_reply *) reply);
74531fed 4775
6a3753b3 4776 target_async (1);
74531fed
PA
4777 }
4778 else
4779 {
4780 gdb_assert (wait_status != NULL);
4781 strcpy (rs->buf, wait_status);
4782 rs->cached_wait_status = 1;
4783 }
4784 }
4785 else
4786 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4787}
4788
b9c1d481
AS
4789/* Implementation of the to_post_attach method. */
4790
4791static void
4792extended_remote_post_attach (struct target_ops *ops, int pid)
4793{
4794 /* In certain cases GDB might not have had the chance to start
4795 symbol lookup up until now. This could happen if the debugged
4796 binary is not using shared libraries, the vsyscall page is not
4797 present (on Linux) and the binary itself hadn't changed since the
4798 debugging process was started. */
4799 if (symfile_objfile != NULL)
4800 remote_check_symbols();
4801}
4802
c906108c 4803\f
506fb367
DJ
4804/* Check for the availability of vCont. This function should also check
4805 the response. */
c906108c
SS
4806
4807static void
6d820c5c 4808remote_vcont_probe (struct remote_state *rs)
c906108c 4809{
2e9f7625 4810 char *buf;
6d820c5c 4811
2e9f7625
DJ
4812 strcpy (rs->buf, "vCont?");
4813 putpkt (rs->buf);
6d820c5c 4814 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4815 buf = rs->buf;
c906108c 4816
506fb367 4817 /* Make sure that the features we assume are supported. */
61012eef 4818 if (startswith (buf, "vCont"))
506fb367
DJ
4819 {
4820 char *p = &buf[5];
4821 int support_s, support_S, support_c, support_C;
4822
4823 support_s = 0;
4824 support_S = 0;
4825 support_c = 0;
4826 support_C = 0;
d458bd84 4827 rs->supports_vCont.t = 0;
c1e36e3e 4828 rs->supports_vCont.r = 0;
506fb367
DJ
4829 while (p && *p == ';')
4830 {
4831 p++;
4832 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4833 support_s = 1;
4834 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4835 support_S = 1;
4836 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4837 support_c = 1;
4838 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4839 support_C = 1;
74531fed 4840 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4841 rs->supports_vCont.t = 1;
c1e36e3e
PA
4842 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4843 rs->supports_vCont.r = 1;
506fb367
DJ
4844
4845 p = strchr (p, ';');
4846 }
c906108c 4847
506fb367
DJ
4848 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4849 BUF will make packet_ok disable the packet. */
4850 if (!support_s || !support_S || !support_c || !support_C)
4851 buf[0] = 0;
4852 }
c906108c 4853
444abaca 4854 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4855}
c906108c 4856
0d8f58ca
PA
4857/* Helper function for building "vCont" resumptions. Write a
4858 resumption to P. ENDP points to one-passed-the-end of the buffer
4859 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4860 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4861 resumed thread should be single-stepped and/or signalled. If PTID
4862 equals minus_one_ptid, then all threads are resumed; if PTID
4863 represents a process, then all threads of the process are resumed;
4864 the thread to be stepped and/or signalled is given in the global
4865 INFERIOR_PTID. */
4866
4867static char *
4868append_resumption (char *p, char *endp,
2ea28649 4869 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4870{
4871 struct remote_state *rs = get_remote_state ();
4872
a493e3e2 4873 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4874 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4875 else if (step
4876 /* GDB is willing to range step. */
4877 && use_range_stepping
4878 /* Target supports range stepping. */
4879 && rs->supports_vCont.r
4880 /* We don't currently support range stepping multiple
4881 threads with a wildcard (though the protocol allows it,
4882 so stubs shouldn't make an active effort to forbid
4883 it). */
4884 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4885 {
4886 struct thread_info *tp;
4887
4888 if (ptid_equal (ptid, minus_one_ptid))
4889 {
4890 /* If we don't know about the target thread's tid, then
4891 we're resuming magic_null_ptid (see caller). */
4892 tp = find_thread_ptid (magic_null_ptid);
4893 }
4894 else
4895 tp = find_thread_ptid (ptid);
4896 gdb_assert (tp != NULL);
4897
4898 if (tp->control.may_range_step)
4899 {
4900 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4901
4902 p += xsnprintf (p, endp - p, ";r%s,%s",
4903 phex_nz (tp->control.step_range_start,
4904 addr_size),
4905 phex_nz (tp->control.step_range_end,
4906 addr_size));
4907 }
4908 else
4909 p += xsnprintf (p, endp - p, ";s");
4910 }
0d8f58ca
PA
4911 else if (step)
4912 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4913 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4914 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4915 else
4916 p += xsnprintf (p, endp - p, ";c");
4917
4918 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4919 {
4920 ptid_t nptid;
4921
4922 /* All (-1) threads of process. */
ba348170 4923 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4924
4925 p += xsnprintf (p, endp - p, ":");
4926 p = write_ptid (p, endp, nptid);
4927 }
4928 else if (!ptid_equal (ptid, minus_one_ptid))
4929 {
4930 p += xsnprintf (p, endp - p, ":");
4931 p = write_ptid (p, endp, ptid);
4932 }
4933
4934 return p;
4935}
4936
e5ef252a
PA
4937/* Append a vCont continue-with-signal action for threads that have a
4938 non-zero stop signal. */
4939
4940static char *
4941append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4942{
4943 struct thread_info *thread;
4944
034f788c 4945 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4946 if (ptid_match (thread->ptid, ptid)
4947 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4948 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4949 {
4950 p = append_resumption (p, endp, thread->ptid,
4951 0, thread->suspend.stop_signal);
4952 thread->suspend.stop_signal = GDB_SIGNAL_0;
4953 }
4954
4955 return p;
4956}
4957
506fb367
DJ
4958/* Resume the remote inferior by using a "vCont" packet. The thread
4959 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4960 resumed thread should be single-stepped and/or signalled. If PTID
4961 equals minus_one_ptid, then all threads are resumed; the thread to
4962 be stepped and/or signalled is given in the global INFERIOR_PTID.
4963 This function returns non-zero iff it resumes the inferior.
44eaed12 4964
506fb367
DJ
4965 This function issues a strict subset of all possible vCont commands at the
4966 moment. */
44eaed12 4967
506fb367 4968static int
2ea28649 4969remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
4970{
4971 struct remote_state *rs = get_remote_state ();
82f73884
PA
4972 char *p;
4973 char *endp;
44eaed12 4974
4082afcc 4975 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 4976 remote_vcont_probe (rs);
44eaed12 4977
4082afcc 4978 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 4979 return 0;
44eaed12 4980
82f73884
PA
4981 p = rs->buf;
4982 endp = rs->buf + get_remote_packet_size ();
4983
506fb367
DJ
4984 /* If we could generate a wider range of packets, we'd have to worry
4985 about overflowing BUF. Should there be a generic
4986 "multi-part-packet" packet? */
4987
0d8f58ca
PA
4988 p += xsnprintf (p, endp - p, "vCont");
4989
79d7f229 4990 if (ptid_equal (ptid, magic_null_ptid))
c906108c 4991 {
79d7f229
PA
4992 /* MAGIC_NULL_PTID means that we don't have any active threads,
4993 so we don't have any TID numbers the inferior will
4994 understand. Make sure to only send forms that do not specify
4995 a TID. */
a9cbf802 4996 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 4997 }
0d8f58ca 4998 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 4999 {
0d8f58ca
PA
5000 /* Resume all threads (of all processes, or of a single
5001 process), with preference for INFERIOR_PTID. This assumes
5002 inferior_ptid belongs to the set of all threads we are about
5003 to resume. */
a493e3e2 5004 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5005 {
0d8f58ca
PA
5006 /* Step inferior_ptid, with or without signal. */
5007 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5008 }
0d8f58ca 5009
e5ef252a
PA
5010 /* Also pass down any pending signaled resumption for other
5011 threads not the current. */
5012 p = append_pending_thread_resumptions (p, endp, ptid);
5013
0d8f58ca 5014 /* And continue others without a signal. */
a493e3e2 5015 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5016 }
5017 else
506fb367
DJ
5018 {
5019 /* Scheduler locking; resume only PTID. */
a9cbf802 5020 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5021 }
c906108c 5022
82f73884
PA
5023 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5024 putpkt (rs->buf);
506fb367 5025
74531fed
PA
5026 if (non_stop)
5027 {
5028 /* In non-stop, the stub replies to vCont with "OK". The stop
5029 reply will be reported asynchronously by means of a `%Stop'
5030 notification. */
5031 getpkt (&rs->buf, &rs->buf_size, 0);
5032 if (strcmp (rs->buf, "OK") != 0)
5033 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5034 }
5035
506fb367 5036 return 1;
c906108c 5037}
43ff13b4 5038
506fb367
DJ
5039/* Tell the remote machine to resume. */
5040
43ff13b4 5041static void
28439f5e 5042remote_resume (struct target_ops *ops,
2ea28649 5043 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5044{
d01949b6 5045 struct remote_state *rs = get_remote_state ();
2e9f7625 5046 char *buf;
43ff13b4 5047
722247f1
YQ
5048 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5049 (explained in remote-notif.c:handle_notification) so
5050 remote_notif_process is not called. We need find a place where
5051 it is safe to start a 'vNotif' sequence. It is good to do it
5052 before resuming inferior, because inferior was stopped and no RSP
5053 traffic at that moment. */
5054 if (!non_stop)
5965e028 5055 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5056
b73be471 5057 rs->last_sent_signal = siggnal;
280ceea3 5058 rs->last_sent_step = step;
43ff13b4 5059
506fb367 5060 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5061 /* No reverse support (yet) for vCont. */
5062 if (execution_direction != EXEC_REVERSE)
5063 if (remote_vcont_resume (ptid, step, siggnal))
5064 goto done;
506fb367 5065
79d7f229
PA
5066 /* All other supported resume packets do use Hc, so set the continue
5067 thread. */
5068 if (ptid_equal (ptid, minus_one_ptid))
5069 set_continue_thread (any_thread_ptid);
506fb367 5070 else
79d7f229 5071 set_continue_thread (ptid);
506fb367 5072
2e9f7625 5073 buf = rs->buf;
b2175913
MS
5074 if (execution_direction == EXEC_REVERSE)
5075 {
5076 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5077 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5078 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5079 siggnal);
40ab02ce 5080
4082afcc 5081 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5082 error (_("Remote reverse-step not supported."));
4082afcc 5083 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5084 error (_("Remote reverse-continue not supported."));
40ab02ce 5085
b2175913
MS
5086 strcpy (buf, step ? "bs" : "bc");
5087 }
a493e3e2 5088 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5089 {
5090 buf[0] = step ? 'S' : 'C';
c5aa993b 5091 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5092 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5093 buf[3] = '\0';
5094 }
5095 else
c5aa993b 5096 strcpy (buf, step ? "s" : "c");
506fb367 5097
44eaed12 5098 putpkt (buf);
43ff13b4 5099
75c99385 5100 done:
2acceee2 5101 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5102 with the event loop. NOTE: this is the one place where all the
5103 execution commands end up. We could alternatively do this in each
23860348 5104 of the execution commands in infcmd.c. */
2acceee2
JM
5105 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5106 into infcmd.c in order to allow inferior function calls to work
23860348 5107 NOT asynchronously. */
362646f5 5108 if (target_can_async_p ())
6a3753b3 5109 target_async (1);
e24a49d8
PA
5110
5111 /* We've just told the target to resume. The remote server will
5112 wait for the inferior to stop, and then send a stop reply. In
5113 the mean time, we can't start another command/query ourselves
74531fed
PA
5114 because the stub wouldn't be ready to process it. This applies
5115 only to the base all-stop protocol, however. In non-stop (which
5116 only supports vCont), the stub replies with an "OK", and is
5117 immediate able to process further serial input. */
5118 if (!non_stop)
5119 rs->waiting_for_stop_reply = 1;
43ff13b4 5120}
c906108c 5121\f
43ff13b4
JM
5122
5123/* Set up the signal handler for SIGINT, while the target is
23860348 5124 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5125static void
934b9bac 5126async_initialize_sigint_signal_handler (void)
43ff13b4 5127{
934b9bac 5128 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5129}
5130
23860348 5131/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5132static void
934b9bac 5133async_handle_remote_sigint (int sig)
43ff13b4 5134{
934b9bac 5135 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5136 /* Note we need to go through gdb_call_async_signal_handler in order
5137 to wake up the event loop on Windows. */
5138 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5139}
5140
5141/* Signal handler for SIGINT, installed after SIGINT has already been
5142 sent once. It will take effect the second time that the user sends
23860348 5143 a ^C. */
43ff13b4 5144static void
934b9bac 5145async_handle_remote_sigint_twice (int sig)
43ff13b4 5146{
934b9bac 5147 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5148 /* See note in async_handle_remote_sigint. */
5149 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5150}
5151
6426a772 5152/* Perform the real interruption of the target execution, in response
23860348 5153 to a ^C. */
c5aa993b 5154static void
fba45db2 5155async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5156{
5157 if (remote_debug)
248fd3bf 5158 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5159
94cc34af 5160 target_stop (inferior_ptid);
43ff13b4
JM
5161}
5162
0df8b418 5163/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5164 up on the target alltogether. */
47e1ce27 5165static void
fba45db2 5166async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5167{
2df3850c 5168 if (remote_debug)
248fd3bf 5169 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5170
5171 interrupt_query ();
43ff13b4
JM
5172}
5173
5174/* Reinstall the usual SIGINT handlers, after the target has
23860348 5175 stopped. */
6426a772 5176static void
934b9bac 5177async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5178{
5179 signal (SIGINT, handle_sigint);
43ff13b4
JM
5180}
5181
c906108c
SS
5182/* Send ^C to target to halt it. Target will respond, and send us a
5183 packet. */
507f3c78 5184static void (*ofunc) (int);
c906108c 5185
0df8b418
MS
5186/* The command line interface's stop routine. This function is installed
5187 as a signal handler for SIGINT. The first time a user requests a
5188 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 5189 response from the target (it didn't stop when the user requested it),
23860348 5190 we ask the user if he'd like to detach from the target. */
c906108c 5191static void
934b9bac 5192sync_remote_interrupt (int signo)
c906108c 5193{
23860348 5194 /* If this doesn't work, try more severe steps. */
934b9bac 5195 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5196
934b9bac 5197 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5198}
5199
5200/* The user typed ^C twice. */
5201
5202static void
934b9bac 5203sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5204{
5205 signal (signo, ofunc);
934b9bac
JK
5206 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5207 signal (signo, sync_remote_interrupt);
c906108c 5208}
7a292a7a 5209
74531fed
PA
5210/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5211 thread, all threads of a remote process, or all threads of all
5212 processes. */
5213
5214static void
5215remote_stop_ns (ptid_t ptid)
5216{
5217 struct remote_state *rs = get_remote_state ();
5218 char *p = rs->buf;
5219 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5220
4082afcc 5221 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5222 remote_vcont_probe (rs);
5223
d458bd84 5224 if (!rs->supports_vCont.t)
74531fed
PA
5225 error (_("Remote server does not support stopping threads"));
5226
f91d3df5
PA
5227 if (ptid_equal (ptid, minus_one_ptid)
5228 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5229 p += xsnprintf (p, endp - p, "vCont;t");
5230 else
5231 {
5232 ptid_t nptid;
5233
74531fed
PA
5234 p += xsnprintf (p, endp - p, "vCont;t:");
5235
5236 if (ptid_is_pid (ptid))
5237 /* All (-1) threads of process. */
ba348170 5238 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5239 else
5240 {
5241 /* Small optimization: if we already have a stop reply for
5242 this thread, no use in telling the stub we want this
5243 stopped. */
5244 if (peek_stop_reply (ptid))
5245 return;
5246
5247 nptid = ptid;
5248 }
5249
a9cbf802 5250 write_ptid (p, endp, nptid);
74531fed
PA
5251 }
5252
5253 /* In non-stop, we get an immediate OK reply. The stop reply will
5254 come in asynchronously by notification. */
5255 putpkt (rs->buf);
5256 getpkt (&rs->buf, &rs->buf_size, 0);
5257 if (strcmp (rs->buf, "OK") != 0)
5258 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5259}
5260
5261/* All-stop version of target_stop. Sends a break or a ^C to stop the
5262 remote target. It is undefined which thread of which process
5263 reports the stop. */
5264
5265static void
5266remote_stop_as (ptid_t ptid)
5267{
5268 struct remote_state *rs = get_remote_state ();
5269
3a29589a
DJ
5270 rs->ctrlc_pending_p = 1;
5271
74531fed
PA
5272 /* If the inferior is stopped already, but the core didn't know
5273 about it yet, just ignore the request. The cached wait status
5274 will be collected in remote_wait. */
5275 if (rs->cached_wait_status)
5276 return;
5277
9a7071a8
JB
5278 /* Send interrupt_sequence to remote target. */
5279 send_interrupt_sequence ();
74531fed
PA
5280}
5281
0df8b418 5282/* This is the generic stop called via the target vector. When a target
7a292a7a 5283 interrupt is requested, either by the command line or the GUI, we
23860348 5284 will eventually end up here. */
74531fed 5285
c906108c 5286static void
1eab8a48 5287remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5288{
7a292a7a 5289 if (remote_debug)
0f71a2f6 5290 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5291
74531fed
PA
5292 if (non_stop)
5293 remote_stop_ns (ptid);
c906108c 5294 else
74531fed 5295 remote_stop_as (ptid);
c906108c
SS
5296}
5297
5298/* Ask the user what to do when an interrupt is received. */
5299
5300static void
fba45db2 5301interrupt_query (void)
c906108c
SS
5302{
5303 target_terminal_ours ();
5304
d9d41e78 5305 if (target_is_async_p ())
c906108c 5306 {
74531fed 5307 signal (SIGINT, handle_sigint);
039e3c22 5308 quit ();
c906108c 5309 }
74531fed
PA
5310 else
5311 {
9e2f0ad4
HZ
5312 if (query (_("Interrupted while waiting for the program.\n\
5313Give up (and stop debugging it)? ")))
74531fed 5314 {
78a095c3 5315 remote_unpush_target ();
039e3c22 5316 quit ();
74531fed
PA
5317 }
5318 }
c906108c
SS
5319
5320 target_terminal_inferior ();
5321}
5322
6426a772
JM
5323/* Enable/disable target terminal ownership. Most targets can use
5324 terminal groups to control terminal ownership. Remote targets are
5325 different in that explicit transfer of ownership to/from GDB/target
23860348 5326 is required. */
6426a772
JM
5327
5328static void
d2f640d4 5329remote_terminal_inferior (struct target_ops *self)
6426a772 5330{
c6ebd6cf 5331 if (!target_async_permitted)
75c99385
PA
5332 /* Nothing to do. */
5333 return;
5334
d9d2d8b6
PA
5335 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5336 idempotent. The event-loop GDB talking to an asynchronous target
5337 with a synchronous command calls this function from both
5338 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5339 transfer the terminal to the target when it shouldn't this guard
5340 can go away. */
6426a772
JM
5341 if (!remote_async_terminal_ours_p)
5342 return;
5343 delete_file_handler (input_fd);
5344 remote_async_terminal_ours_p = 0;
934b9bac 5345 async_initialize_sigint_signal_handler ();
6426a772
JM
5346 /* NOTE: At this point we could also register our selves as the
5347 recipient of all input. Any characters typed could then be
23860348 5348 passed on down to the target. */
6426a772
JM
5349}
5350
5351static void
e3594fd1 5352remote_terminal_ours (struct target_ops *self)
6426a772 5353{
c6ebd6cf 5354 if (!target_async_permitted)
75c99385
PA
5355 /* Nothing to do. */
5356 return;
5357
5358 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5359 if (remote_async_terminal_ours_p)
5360 return;
934b9bac 5361 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5362 add_file_handler (input_fd, stdin_event_handler, 0);
5363 remote_async_terminal_ours_p = 1;
5364}
5365
176a6961 5366static void
917317f4 5367remote_console_output (char *msg)
c906108c
SS
5368{
5369 char *p;
5370
c5aa993b 5371 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5372 {
5373 char tb[2];
5374 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5375
c906108c
SS
5376 tb[0] = c;
5377 tb[1] = 0;
43ff13b4 5378 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5379 }
00db5b94
PA
5380 gdb_flush (gdb_stdtarg);
5381}
74531fed
PA
5382
5383typedef struct cached_reg
5384{
5385 int num;
5386 gdb_byte data[MAX_REGISTER_SIZE];
5387} cached_reg_t;
5388
5389DEF_VEC_O(cached_reg_t);
5390
722247f1 5391typedef struct stop_reply
74531fed 5392{
722247f1 5393 struct notif_event base;
74531fed 5394
722247f1 5395 /* The identifier of the thread about this event */
74531fed
PA
5396 ptid_t ptid;
5397
340e3c99 5398 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5399 connection, represented by a remote_state object, is closed,
5400 all the associated stop_reply events should be released. */
5401 struct remote_state *rs;
5402
74531fed
PA
5403 struct target_waitstatus ws;
5404
15148d6a
PA
5405 /* Expedited registers. This makes remote debugging a bit more
5406 efficient for those targets that provide critical registers as
5407 part of their normal status mechanism (as another roundtrip to
5408 fetch them is avoided). */
74531fed
PA
5409 VEC(cached_reg_t) *regcache;
5410
f7e6eed5
PA
5411 enum target_stop_reason stop_reason;
5412
74531fed
PA
5413 CORE_ADDR watch_data_address;
5414
dc146f7c 5415 int core;
722247f1 5416} *stop_reply_p;
a744cf53 5417
722247f1
YQ
5418DECLARE_QUEUE_P (stop_reply_p);
5419DEFINE_QUEUE_P (stop_reply_p);
5420/* The list of already fetched and acknowledged stop events. This
5421 queue is used for notification Stop, and other notifications
5422 don't need queue for their events, because the notification events
5423 of Stop can't be consumed immediately, so that events should be
5424 queued first, and be consumed by remote_wait_{ns,as} one per
5425 time. Other notifications can consume their events immediately,
5426 so queue is not needed for them. */
5427static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5428
5429static void
5430stop_reply_xfree (struct stop_reply *r)
5431{
f48ff2a7 5432 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5433}
5434
722247f1
YQ
5435static void
5436remote_notif_stop_parse (struct notif_client *self, char *buf,
5437 struct notif_event *event)
5438{
5439 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5440}
5441
5442static void
5443remote_notif_stop_ack (struct notif_client *self, char *buf,
5444 struct notif_event *event)
5445{
5446 struct stop_reply *stop_reply = (struct stop_reply *) event;
5447
5448 /* acknowledge */
5449 putpkt ((char *) self->ack_command);
5450
5451 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5452 /* We got an unknown stop reply. */
5453 error (_("Unknown stop reply"));
5454
5455 push_stop_reply (stop_reply);
5456}
5457
5458static int
5459remote_notif_stop_can_get_pending_events (struct notif_client *self)
5460{
5461 /* We can't get pending events in remote_notif_process for
5462 notification stop, and we have to do this in remote_wait_ns
5463 instead. If we fetch all queued events from stub, remote stub
5464 may exit and we have no chance to process them back in
5465 remote_wait_ns. */
5466 mark_async_event_handler (remote_async_inferior_event_token);
5467 return 0;
5468}
5469
5470static void
5471stop_reply_dtr (struct notif_event *event)
5472{
5473 struct stop_reply *r = (struct stop_reply *) event;
5474
5475 VEC_free (cached_reg_t, r->regcache);
5476}
5477
5478static struct notif_event *
5479remote_notif_stop_alloc_reply (void)
5480{
5481 struct notif_event *r
70ba0933 5482 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5483
5484 r->dtr = stop_reply_dtr;
5485
5486 return r;
5487}
5488
5489/* A client of notification Stop. */
5490
5491struct notif_client notif_client_stop =
5492{
5493 "Stop",
5494 "vStopped",
5495 remote_notif_stop_parse,
5496 remote_notif_stop_ack,
5497 remote_notif_stop_can_get_pending_events,
5498 remote_notif_stop_alloc_reply,
f48ff2a7 5499 REMOTE_NOTIF_STOP,
722247f1
YQ
5500};
5501
5502/* A parameter to pass data in and out. */
5503
5504struct queue_iter_param
5505{
5506 void *input;
5507 struct stop_reply *output;
5508};
5509
cbb8991c
DB
5510/* Determine if THREAD is a pending fork parent thread. ARG contains
5511 the pid of the process that owns the threads we want to check, or
5512 -1 if we want to check all threads. */
5513
5514static int
5515is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5516 ptid_t thread_ptid)
5517{
5518 if (ws->kind == TARGET_WAITKIND_FORKED
5519 || ws->kind == TARGET_WAITKIND_VFORKED)
5520 {
5521 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5522 return 1;
5523 }
5524
5525 return 0;
5526}
5527
5528/* Check whether EVENT is a fork event, and if it is, remove the
5529 fork child from the context list passed in DATA. */
5530
5531static int
5532remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5533 QUEUE_ITER (stop_reply_p) *iter,
5534 stop_reply_p event,
5535 void *data)
5536{
5537 struct queue_iter_param *param = data;
5538 struct threads_listing_context *context = param->input;
5539
5540 if (event->ws.kind == TARGET_WAITKIND_FORKED
5541 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5542 {
5543 threads_listing_context_remove (&event->ws, context);
5544 }
5545
5546 return 1;
5547}
5548
5549/* If CONTEXT contains any fork child threads that have not been
5550 reported yet, remove them from the CONTEXT list. If such a
5551 thread exists it is because we are stopped at a fork catchpoint
5552 and have not yet called follow_fork, which will set up the
5553 host-side data structures for the new process. */
5554
5555static void
5556remove_new_fork_children (struct threads_listing_context *context)
5557{
5558 struct thread_info * thread;
5559 int pid = -1;
5560 struct notif_client *notif = &notif_client_stop;
5561 struct queue_iter_param param;
5562
5563 /* For any threads stopped at a fork event, remove the corresponding
5564 fork child threads from the CONTEXT list. */
5565 ALL_NON_EXITED_THREADS (thread)
5566 {
5567 struct target_waitstatus *ws = &thread->pending_follow;
5568
5569 if (is_pending_fork_parent (ws, pid, thread->ptid))
5570 {
5571 threads_listing_context_remove (ws, context);
5572 }
5573 }
5574
5575 /* Check for any pending fork events (not reported or processed yet)
5576 in process PID and remove those fork child threads from the
5577 CONTEXT list as well. */
5578 remote_notif_get_pending_events (notif);
5579 param.input = context;
5580 param.output = NULL;
5581 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5582 remove_child_of_pending_fork, &param);
5583}
5584
f48ff2a7
YQ
5585/* Remove stop replies in the queue if its pid is equal to the given
5586 inferior's pid. */
722247f1
YQ
5587
5588static int
f48ff2a7
YQ
5589remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5590 QUEUE_ITER (stop_reply_p) *iter,
5591 stop_reply_p event,
5592 void *data)
722247f1
YQ
5593{
5594 struct queue_iter_param *param = data;
5595 struct inferior *inf = param->input;
5596
f48ff2a7 5597 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5598 {
5599 stop_reply_xfree (event);
5600 QUEUE_remove_elem (stop_reply_p, q, iter);
5601 }
5602
5603 return 1;
5604}
5605
f48ff2a7 5606/* Discard all pending stop replies of inferior INF. */
c906108c 5607
74531fed 5608static void
5f4cf0bb 5609discard_pending_stop_replies (struct inferior *inf)
c906108c 5610{
722247f1
YQ
5611 int i;
5612 struct queue_iter_param param;
f48ff2a7
YQ
5613 struct stop_reply *reply;
5614 struct remote_state *rs = get_remote_state ();
5615 struct remote_notif_state *rns = rs->notif_state;
5616
5617 /* This function can be notified when an inferior exists. When the
5618 target is not remote, the notification state is NULL. */
5619 if (rs->remote_desc == NULL)
5620 return;
5621
5622 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5623
74531fed 5624 /* Discard the in-flight notification. */
f48ff2a7 5625 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5626 {
722247f1 5627 stop_reply_xfree (reply);
f48ff2a7 5628 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5629 }
c906108c 5630
722247f1
YQ
5631 param.input = inf;
5632 param.output = NULL;
74531fed
PA
5633 /* Discard the stop replies we have already pulled with
5634 vStopped. */
722247f1 5635 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5636 remove_stop_reply_for_inferior, &param);
5637}
5638
bcc75809
YQ
5639/* If its remote state is equal to the given remote state,
5640 remove EVENT from the stop reply queue. */
5641
5642static int
5643remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5644 QUEUE_ITER (stop_reply_p) *iter,
5645 stop_reply_p event,
5646 void *data)
5647{
5648 struct queue_iter_param *param = data;
5649 struct remote_state *rs = param->input;
5650
5651 if (event->rs == rs)
5652 {
5653 stop_reply_xfree (event);
5654 QUEUE_remove_elem (stop_reply_p, q, iter);
5655 }
5656
5657 return 1;
5658}
5659
5660/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5661
5662static void
bcc75809 5663discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5664{
5665 struct queue_iter_param param;
5666
bcc75809 5667 param.input = rs;
f48ff2a7
YQ
5668 param.output = NULL;
5669 /* Discard the stop replies we have already pulled with
5670 vStopped. */
5671 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5672 remove_stop_reply_of_remote_state, &param);
722247f1 5673}
74531fed 5674
722247f1
YQ
5675/* A parameter to pass data in and out. */
5676
5677static int
5678remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5679 QUEUE_ITER (stop_reply_p) *iter,
5680 stop_reply_p event,
5681 void *data)
5682{
5683 struct queue_iter_param *param = data;
5684 ptid_t *ptid = param->input;
5685
5686 if (ptid_match (event->ptid, *ptid))
5687 {
5688 param->output = event;
5689 QUEUE_remove_elem (stop_reply_p, q, iter);
5690 return 0;
c8e38a49 5691 }
722247f1
YQ
5692
5693 return 1;
74531fed 5694}
43ff13b4 5695
722247f1
YQ
5696/* Remove the first reply in 'stop_reply_queue' which matches
5697 PTID. */
2e9f7625 5698
722247f1
YQ
5699static struct stop_reply *
5700remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5701{
722247f1
YQ
5702 struct queue_iter_param param;
5703
5704 param.input = &ptid;
5705 param.output = NULL;
5706
5707 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5708 remote_notif_remove_once_on_match, &param);
5709 if (notif_debug)
5710 fprintf_unfiltered (gdb_stdlog,
5711 "notif: discard queued event: 'Stop' in %s\n",
5712 target_pid_to_str (ptid));
a744cf53 5713
722247f1 5714 return param.output;
74531fed 5715}
75c99385 5716
74531fed
PA
5717/* Look for a queued stop reply belonging to PTID. If one is found,
5718 remove it from the queue, and return it. Returns NULL if none is
5719 found. If there are still queued events left to process, tell the
5720 event loop to get back to target_wait soon. */
e24a49d8 5721
74531fed
PA
5722static struct stop_reply *
5723queued_stop_reply (ptid_t ptid)
5724{
722247f1 5725 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5726
722247f1 5727 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5728 /* There's still at least an event left. */
5729 mark_async_event_handler (remote_async_inferior_event_token);
5730
722247f1 5731 return r;
74531fed
PA
5732}
5733
5734/* Push a fully parsed stop reply in the stop reply queue. Since we
5735 know that we now have at least one queued event left to pass to the
5736 core side, tell the event loop to get back to target_wait soon. */
5737
5738static void
5739push_stop_reply (struct stop_reply *new_event)
5740{
722247f1 5741 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5742
722247f1
YQ
5743 if (notif_debug)
5744 fprintf_unfiltered (gdb_stdlog,
5745 "notif: push 'Stop' %s to queue %d\n",
5746 target_pid_to_str (new_event->ptid),
5747 QUEUE_length (stop_reply_p,
5748 stop_reply_queue));
74531fed
PA
5749
5750 mark_async_event_handler (remote_async_inferior_event_token);
5751}
5752
722247f1
YQ
5753static int
5754stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5755 QUEUE_ITER (stop_reply_p) *iter,
5756 struct stop_reply *event,
5757 void *data)
5758{
5759 ptid_t *ptid = data;
5760
5761 return !(ptid_equal (*ptid, event->ptid)
5762 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5763}
5764
74531fed
PA
5765/* Returns true if we have a stop reply for PTID. */
5766
5767static int
5768peek_stop_reply (ptid_t ptid)
5769{
722247f1
YQ
5770 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5771 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5772}
5773
1f10ba14
PA
5774/* Skip PACKET until the next semi-colon (or end of string). */
5775
5776static char *
5777skip_to_semicolon (char *p)
5778{
5779 while (*p != '\0' && *p != ';')
5780 p++;
5781 return p;
5782}
5783
74531fed
PA
5784/* Parse the stop reply in BUF. Either the function succeeds, and the
5785 result is stored in EVENT, or throws an error. */
5786
5787static void
5788remote_parse_stop_reply (char *buf, struct stop_reply *event)
5789{
5790 struct remote_arch_state *rsa = get_remote_arch_state ();
5791 ULONGEST addr;
5792 char *p;
5793
5794 event->ptid = null_ptid;
bcc75809 5795 event->rs = get_remote_state ();
74531fed
PA
5796 event->ws.kind = TARGET_WAITKIND_IGNORE;
5797 event->ws.value.integer = 0;
f7e6eed5 5798 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5799 event->regcache = NULL;
dc146f7c 5800 event->core = -1;
74531fed
PA
5801
5802 switch (buf[0])
5803 {
5804 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5805 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5806 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5807 ss = signal number
5808 n... = register number
5809 r... = register contents
5810 */
5811
5812 p = &buf[3]; /* after Txx */
5813 while (*p)
5814 {
5815 char *p1;
cea39f65 5816 int fieldsize;
43ff13b4 5817
1f10ba14
PA
5818 p1 = strchr (p, ':');
5819 if (p1 == NULL)
5820 error (_("Malformed packet(a) (missing colon): %s\n\
5821Packet: '%s'\n"),
5822 p, buf);
5823 if (p == p1)
5824 error (_("Malformed packet(a) (missing register number): %s\n\
5825Packet: '%s'\n"),
5826 p, buf);
3c3bea1c 5827
1f10ba14
PA
5828 /* Some "registers" are actually extended stop information.
5829 Note if you're adding a new entry here: GDB 7.9 and
5830 earlier assume that all register "numbers" that start
5831 with an hex digit are real register numbers. Make sure
5832 the server only sends such a packet if it knows the
5833 client understands it. */
c8e38a49 5834
1f10ba14
PA
5835 if (strncmp (p, "thread", p1 - p) == 0)
5836 event->ptid = read_ptid (++p1, &p);
5837 else if ((strncmp (p, "watch", p1 - p) == 0)
5838 || (strncmp (p, "rwatch", p1 - p) == 0)
5839 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5840 {
f7e6eed5 5841 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5842 p = unpack_varlen_hex (++p1, &addr);
5843 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5844 }
f7e6eed5
PA
5845 else if (strncmp (p, "swbreak", p1 - p) == 0)
5846 {
5847 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5848
5849 /* Make sure the stub doesn't forget to indicate support
5850 with qSupported. */
5851 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5852 error (_("Unexpected swbreak stop reason"));
5853
5854 /* The value part is documented as "must be empty",
5855 though we ignore it, in case we ever decide to make
5856 use of it in a backward compatible way. */
5857 p = skip_to_semicolon (p1 + 1);
5858 }
5859 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5860 {
5861 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5862
5863 /* Make sure the stub doesn't forget to indicate support
5864 with qSupported. */
5865 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5866 error (_("Unexpected hwbreak stop reason"));
5867
5868 /* See above. */
5869 p = skip_to_semicolon (p1 + 1);
5870 }
1f10ba14 5871 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5872 {
1f10ba14
PA
5873 event->ws.kind = TARGET_WAITKIND_LOADED;
5874 p = skip_to_semicolon (p1 + 1);
5875 }
5876 else if (strncmp (p, "replaylog", p1 - p) == 0)
5877 {
5878 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5879 /* p1 will indicate "begin" or "end", but it makes
5880 no difference for now, so ignore it. */
5881 p = skip_to_semicolon (p1 + 1);
5882 }
5883 else if (strncmp (p, "core", p1 - p) == 0)
5884 {
5885 ULONGEST c;
a744cf53 5886
1f10ba14
PA
5887 p = unpack_varlen_hex (++p1, &c);
5888 event->core = c;
cea39f65 5889 }
de0d863e
DB
5890 else if (strncmp (p, "fork", p1 - p) == 0)
5891 {
5892 event->ws.value.related_pid = read_ptid (++p1, &p);
5893 event->ws.kind = TARGET_WAITKIND_FORKED;
5894 }
c269dbdb
DB
5895 else if (strncmp (p, "vfork", p1 - p) == 0)
5896 {
5897 event->ws.value.related_pid = read_ptid (++p1, &p);
5898 event->ws.kind = TARGET_WAITKIND_VFORKED;
5899 }
5900 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5901 {
5902 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5903 p = skip_to_semicolon (p1 + 1);
5904 }
cea39f65
MS
5905 else
5906 {
1f10ba14
PA
5907 ULONGEST pnum;
5908 char *p_temp;
5909
5910 /* Maybe a real ``P'' register number. */
5911 p_temp = unpack_varlen_hex (p, &pnum);
5912 /* If the first invalid character is the colon, we got a
5913 register number. Otherwise, it's an unknown stop
5914 reason. */
5915 if (p_temp == p1)
5916 {
5917 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5918 cached_reg_t cached_reg;
43ff13b4 5919
1f10ba14
PA
5920 if (reg == NULL)
5921 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5922Packet: '%s'\n"),
1f10ba14 5923 hex_string (pnum), p, buf);
c8e38a49 5924
1f10ba14 5925 cached_reg.num = reg->regnum;
4100683b 5926
1f10ba14
PA
5927 p = p1 + 1;
5928 fieldsize = hex2bin (p, cached_reg.data,
5929 register_size (target_gdbarch (),
5930 reg->regnum));
5931 p += 2 * fieldsize;
5932 if (fieldsize < register_size (target_gdbarch (),
5933 reg->regnum))
5934 warning (_("Remote reply is too short: %s"), buf);
74531fed 5935
1f10ba14
PA
5936 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5937 }
5938 else
5939 {
5940 /* Not a number. Silently skip unknown optional
5941 info. */
5942 p = skip_to_semicolon (p1 + 1);
5943 }
cea39f65 5944 }
c8e38a49 5945
cea39f65
MS
5946 if (*p != ';')
5947 error (_("Remote register badly formatted: %s\nhere: %s"),
5948 buf, p);
5949 ++p;
5950 }
5b5596ff
PA
5951
5952 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5953 break;
5954
c8e38a49
PA
5955 /* fall through */
5956 case 'S': /* Old style status, just signal only. */
3a09da41
PA
5957 {
5958 int sig;
5959
5960 event->ws.kind = TARGET_WAITKIND_STOPPED;
5961 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5962 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5963 event->ws.value.sig = (enum gdb_signal) sig;
5964 else
5965 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5966 }
c8e38a49
PA
5967 break;
5968 case 'W': /* Target exited. */
5969 case 'X':
5970 {
5971 char *p;
5972 int pid;
5973 ULONGEST value;
82f73884 5974
c8e38a49
PA
5975 /* GDB used to accept only 2 hex chars here. Stubs should
5976 only send more if they detect GDB supports multi-process
5977 support. */
5978 p = unpack_varlen_hex (&buf[1], &value);
82f73884 5979
c8e38a49
PA
5980 if (buf[0] == 'W')
5981 {
5982 /* The remote process exited. */
74531fed
PA
5983 event->ws.kind = TARGET_WAITKIND_EXITED;
5984 event->ws.value.integer = value;
c8e38a49
PA
5985 }
5986 else
5987 {
5988 /* The remote process exited with a signal. */
74531fed 5989 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
5990 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5991 event->ws.value.sig = (enum gdb_signal) value;
5992 else
5993 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 5994 }
82f73884 5995
c8e38a49
PA
5996 /* If no process is specified, assume inferior_ptid. */
5997 pid = ptid_get_pid (inferior_ptid);
5998 if (*p == '\0')
5999 ;
6000 else if (*p == ';')
6001 {
6002 p++;
6003
0b24eb2d 6004 if (*p == '\0')
82f73884 6005 ;
61012eef 6006 else if (startswith (p, "process:"))
82f73884 6007 {
c8e38a49 6008 ULONGEST upid;
a744cf53 6009
c8e38a49
PA
6010 p += sizeof ("process:") - 1;
6011 unpack_varlen_hex (p, &upid);
6012 pid = upid;
82f73884
PA
6013 }
6014 else
6015 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6016 }
c8e38a49
PA
6017 else
6018 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6019 event->ptid = pid_to_ptid (pid);
6020 }
6021 break;
6022 }
6023
6024 if (non_stop && ptid_equal (event->ptid, null_ptid))
6025 error (_("No process or thread specified in stop reply: %s"), buf);
6026}
6027
722247f1
YQ
6028/* When the stub wants to tell GDB about a new notification reply, it
6029 sends a notification (%Stop, for example). Those can come it at
6030 any time, hence, we have to make sure that any pending
6031 putpkt/getpkt sequence we're making is finished, before querying
6032 the stub for more events with the corresponding ack command
6033 (vStopped, for example). E.g., if we started a vStopped sequence
6034 immediately upon receiving the notification, something like this
6035 could happen:
74531fed
PA
6036
6037 1.1) --> Hg 1
6038 1.2) <-- OK
6039 1.3) --> g
6040 1.4) <-- %Stop
6041 1.5) --> vStopped
6042 1.6) <-- (registers reply to step #1.3)
6043
6044 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6045 query.
6046
796cb314 6047 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6048 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6049 doing whatever we were doing:
6050
6051 2.1) --> Hg 1
6052 2.2) <-- OK
6053 2.3) --> g
6054 2.4) <-- %Stop
6055 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6056 2.5) <-- (registers reply to step #2.3)
6057
6058 Eventualy after step #2.5, we return to the event loop, which
6059 notices there's an event on the
6060 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6061 associated callback --- the function below. At this point, we're
6062 always safe to start a vStopped sequence. :
6063
6064 2.6) --> vStopped
6065 2.7) <-- T05 thread:2
6066 2.8) --> vStopped
6067 2.9) --> OK
6068*/
6069
722247f1
YQ
6070void
6071remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6072{
6073 struct remote_state *rs = get_remote_state ();
74531fed 6074
f48ff2a7 6075 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6076 {
722247f1
YQ
6077 if (notif_debug)
6078 fprintf_unfiltered (gdb_stdlog,
6079 "notif: process: '%s' ack pending event\n",
6080 nc->name);
74531fed 6081
722247f1 6082 /* acknowledge */
f48ff2a7
YQ
6083 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6084 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6085
6086 while (1)
6087 {
6088 getpkt (&rs->buf, &rs->buf_size, 0);
6089 if (strcmp (rs->buf, "OK") == 0)
6090 break;
6091 else
722247f1 6092 remote_notif_ack (nc, rs->buf);
74531fed
PA
6093 }
6094 }
722247f1
YQ
6095 else
6096 {
6097 if (notif_debug)
6098 fprintf_unfiltered (gdb_stdlog,
6099 "notif: process: '%s' no pending reply\n",
6100 nc->name);
6101 }
74531fed
PA
6102}
6103
74531fed
PA
6104/* Called when it is decided that STOP_REPLY holds the info of the
6105 event that is to be returned to the core. This function always
6106 destroys STOP_REPLY. */
6107
6108static ptid_t
6109process_stop_reply (struct stop_reply *stop_reply,
6110 struct target_waitstatus *status)
6111{
6112 ptid_t ptid;
6113
6114 *status = stop_reply->ws;
6115 ptid = stop_reply->ptid;
6116
6117 /* If no thread/process was reported by the stub, assume the current
6118 inferior. */
6119 if (ptid_equal (ptid, null_ptid))
6120 ptid = inferior_ptid;
6121
5f3563ea
PA
6122 if (status->kind != TARGET_WAITKIND_EXITED
6123 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6124 {
ee154bee
TT
6125 struct remote_state *rs = get_remote_state ();
6126
5f3563ea
PA
6127 /* Expedited registers. */
6128 if (stop_reply->regcache)
6129 {
217f1f79 6130 struct regcache *regcache
f5656ead 6131 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6132 cached_reg_t *reg;
6133 int ix;
6134
6135 for (ix = 0;
6136 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6137 ix++)
217f1f79 6138 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6139 VEC_free (cached_reg_t, stop_reply->regcache);
6140 }
74531fed 6141
f7e6eed5 6142 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6143 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6144
6145 remote_notice_new_inferior (ptid, 0);
dc146f7c 6146 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6147 }
6148
74531fed
PA
6149 stop_reply_xfree (stop_reply);
6150 return ptid;
6151}
6152
6153/* The non-stop mode version of target_wait. */
6154
6155static ptid_t
47608cb1 6156remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6157{
6158 struct remote_state *rs = get_remote_state ();
74531fed
PA
6159 struct stop_reply *stop_reply;
6160 int ret;
fee9eda9 6161 int is_notif = 0;
74531fed
PA
6162
6163 /* If in non-stop mode, get out of getpkt even if a
6164 notification is received. */
6165
6166 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6167 0 /* forever */, &is_notif);
74531fed
PA
6168 while (1)
6169 {
fee9eda9 6170 if (ret != -1 && !is_notif)
74531fed
PA
6171 switch (rs->buf[0])
6172 {
6173 case 'E': /* Error of some sort. */
6174 /* We're out of sync with the target now. Did it continue
6175 or not? We can't tell which thread it was in non-stop,
6176 so just ignore this. */
6177 warning (_("Remote failure reply: %s"), rs->buf);
6178 break;
6179 case 'O': /* Console output. */
6180 remote_console_output (rs->buf + 1);
6181 break;
6182 default:
6183 warning (_("Invalid remote reply: %s"), rs->buf);
6184 break;
6185 }
6186
6187 /* Acknowledge a pending stop reply that may have arrived in the
6188 mean time. */
f48ff2a7 6189 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6190 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6191
6192 /* If indeed we noticed a stop reply, we're done. */
6193 stop_reply = queued_stop_reply (ptid);
6194 if (stop_reply != NULL)
6195 return process_stop_reply (stop_reply, status);
6196
47608cb1 6197 /* Still no event. If we're just polling for an event, then
74531fed 6198 return to the event loop. */
47608cb1 6199 if (options & TARGET_WNOHANG)
74531fed
PA
6200 {
6201 status->kind = TARGET_WAITKIND_IGNORE;
6202 return minus_one_ptid;
6203 }
6204
47608cb1 6205 /* Otherwise do a blocking wait. */
74531fed 6206 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6207 1 /* forever */, &is_notif);
74531fed
PA
6208 }
6209}
6210
6211/* Wait until the remote machine stops, then return, storing status in
6212 STATUS just as `wait' would. */
6213
6214static ptid_t
47608cb1 6215remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6216{
6217 struct remote_state *rs = get_remote_state ();
74531fed 6218 ptid_t event_ptid = null_ptid;
cea39f65 6219 char *buf;
74531fed
PA
6220 struct stop_reply *stop_reply;
6221
47608cb1
PA
6222 again:
6223
74531fed
PA
6224 status->kind = TARGET_WAITKIND_IGNORE;
6225 status->value.integer = 0;
6226
6227 stop_reply = queued_stop_reply (ptid);
6228 if (stop_reply != NULL)
6229 return process_stop_reply (stop_reply, status);
6230
6231 if (rs->cached_wait_status)
6232 /* Use the cached wait status, but only once. */
6233 rs->cached_wait_status = 0;
6234 else
6235 {
6236 int ret;
722247f1 6237 int is_notif;
74531fed
PA
6238
6239 if (!target_is_async_p ())
6240 {
934b9bac 6241 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6242 /* If the user hit C-c before this packet, or between packets,
6243 pretend that it was hit right here. */
522002f9 6244 if (check_quit_flag ())
74531fed 6245 {
522002f9 6246 clear_quit_flag ();
934b9bac 6247 sync_remote_interrupt (SIGINT);
74531fed
PA
6248 }
6249 }
6250
6251 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6252 _never_ wait for ever -> test on target_is_async_p().
6253 However, before we do that we need to ensure that the caller
6254 knows how to take the target into/out of async mode. */
722247f1
YQ
6255 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6256 wait_forever_enabled_p, &is_notif);
6257
5e1b953b
SDJ
6258 if (!target_is_async_p ())
6259 signal (SIGINT, ofunc);
6260
722247f1
YQ
6261 /* GDB gets a notification. Return to core as this event is
6262 not interesting. */
6263 if (ret != -1 && is_notif)
6264 return minus_one_ptid;
74531fed
PA
6265 }
6266
6267 buf = rs->buf;
6268
f7e6eed5 6269 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6270
6271 /* We got something. */
6272 rs->waiting_for_stop_reply = 0;
6273
3a29589a
DJ
6274 /* Assume that the target has acknowledged Ctrl-C unless we receive
6275 an 'F' or 'O' packet. */
6276 if (buf[0] != 'F' && buf[0] != 'O')
6277 rs->ctrlc_pending_p = 0;
6278
74531fed
PA
6279 switch (buf[0])
6280 {
6281 case 'E': /* Error of some sort. */
6282 /* We're out of sync with the target now. Did it continue or
6283 not? Not is more likely, so report a stop. */
6284 warning (_("Remote failure reply: %s"), buf);
6285 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6286 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6287 break;
6288 case 'F': /* File-I/O request. */
3a29589a
DJ
6289 remote_fileio_request (buf, rs->ctrlc_pending_p);
6290 rs->ctrlc_pending_p = 0;
74531fed
PA
6291 break;
6292 case 'T': case 'S': case 'X': case 'W':
6293 {
722247f1
YQ
6294 struct stop_reply *stop_reply
6295 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6296 rs->buf);
74531fed 6297
74531fed 6298 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6299 break;
6300 }
6301 case 'O': /* Console output. */
6302 remote_console_output (buf + 1);
e24a49d8 6303
c8e38a49
PA
6304 /* The target didn't really stop; keep waiting. */
6305 rs->waiting_for_stop_reply = 1;
e24a49d8 6306
c8e38a49
PA
6307 break;
6308 case '\0':
b73be471 6309 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6310 {
6311 /* Zero length reply means that we tried 'S' or 'C' and the
6312 remote system doesn't support it. */
6313 target_terminal_ours_for_output ();
6314 printf_filtered
6315 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6316 gdb_signal_to_name (rs->last_sent_signal));
6317 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6318 target_terminal_inferior ();
6319
280ceea3 6320 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6321 putpkt ((char *) buf);
6322
6323 /* We just told the target to resume, so a stop reply is in
6324 order. */
e24a49d8 6325 rs->waiting_for_stop_reply = 1;
c8e38a49 6326 break;
43ff13b4 6327 }
c8e38a49
PA
6328 /* else fallthrough */
6329 default:
6330 warning (_("Invalid remote reply: %s"), buf);
6331 /* Keep waiting. */
6332 rs->waiting_for_stop_reply = 1;
6333 break;
43ff13b4 6334 }
c8e38a49 6335
c8e38a49 6336 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6337 {
6338 /* Nothing interesting happened. If we're doing a non-blocking
6339 poll, we're done. Otherwise, go back to waiting. */
6340 if (options & TARGET_WNOHANG)
6341 return minus_one_ptid;
6342 else
6343 goto again;
6344 }
74531fed
PA
6345 else if (status->kind != TARGET_WAITKIND_EXITED
6346 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6347 {
6348 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6349 record_currthread (rs, event_ptid);
82f73884
PA
6350 else
6351 event_ptid = inferior_ptid;
43ff13b4 6352 }
74531fed
PA
6353 else
6354 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6355 record_currthread (rs, minus_one_ptid);
79d7f229 6356
82f73884 6357 return event_ptid;
43ff13b4
JM
6358}
6359
74531fed
PA
6360/* Wait until the remote machine stops, then return, storing status in
6361 STATUS just as `wait' would. */
6362
c8e38a49 6363static ptid_t
117de6a9 6364remote_wait (struct target_ops *ops,
47608cb1 6365 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6366{
6367 ptid_t event_ptid;
6368
74531fed 6369 if (non_stop)
47608cb1 6370 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6371 else
47608cb1 6372 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6373
d9d41e78 6374 if (target_is_async_p ())
c8e38a49 6375 {
74531fed
PA
6376 /* If there are are events left in the queue tell the event loop
6377 to return here. */
722247f1 6378 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6379 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6380 }
c8e38a49
PA
6381
6382 return event_ptid;
6383}
6384
74ca34ce 6385/* Fetch a single register using a 'p' packet. */
c906108c 6386
b96ec7ac 6387static int
56be3814 6388fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6389{
6390 struct remote_state *rs = get_remote_state ();
2e9f7625 6391 char *buf, *p;
b96ec7ac
AC
6392 char regp[MAX_REGISTER_SIZE];
6393 int i;
6394
4082afcc 6395 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6396 return 0;
6397
6398 if (reg->pnum == -1)
6399 return 0;
6400
2e9f7625 6401 p = rs->buf;
fcad0fa4 6402 *p++ = 'p';
74ca34ce 6403 p += hexnumstr (p, reg->pnum);
fcad0fa4 6404 *p++ = '\0';
1f4437a4
MS
6405 putpkt (rs->buf);
6406 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6407
2e9f7625
DJ
6408 buf = rs->buf;
6409
74ca34ce
DJ
6410 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6411 {
6412 case PACKET_OK:
6413 break;
6414 case PACKET_UNKNOWN:
6415 return 0;
6416 case PACKET_ERROR:
27a9c0bf
MS
6417 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6418 gdbarch_register_name (get_regcache_arch (regcache),
6419 reg->regnum),
6420 buf);
74ca34ce 6421 }
3f9a994c
JB
6422
6423 /* If this register is unfetchable, tell the regcache. */
6424 if (buf[0] == 'x')
8480adf2 6425 {
56be3814 6426 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6427 return 1;
b96ec7ac 6428 }
b96ec7ac 6429
3f9a994c
JB
6430 /* Otherwise, parse and supply the value. */
6431 p = buf;
6432 i = 0;
6433 while (p[0] != 0)
6434 {
6435 if (p[1] == 0)
74ca34ce 6436 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6437
6438 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6439 p += 2;
6440 }
56be3814 6441 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6442 return 1;
b96ec7ac
AC
6443}
6444
74ca34ce
DJ
6445/* Fetch the registers included in the target's 'g' packet. */
6446
29709017
DJ
6447static int
6448send_g_packet (void)
c906108c 6449{
d01949b6 6450 struct remote_state *rs = get_remote_state ();
cea39f65 6451 int buf_len;
c906108c 6452
bba74b36 6453 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6454 remote_send (&rs->buf, &rs->buf_size);
c906108c 6455
29709017
DJ
6456 /* We can get out of synch in various cases. If the first character
6457 in the buffer is not a hex character, assume that has happened
6458 and try to fetch another packet to read. */
6459 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6460 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6461 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6462 && rs->buf[0] != 'x') /* New: unavailable register value. */
6463 {
6464 if (remote_debug)
6465 fprintf_unfiltered (gdb_stdlog,
6466 "Bad register packet; fetching a new packet\n");
6467 getpkt (&rs->buf, &rs->buf_size, 0);
6468 }
6469
74ca34ce
DJ
6470 buf_len = strlen (rs->buf);
6471
6472 /* Sanity check the received packet. */
6473 if (buf_len % 2 != 0)
6474 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6475
6476 return buf_len / 2;
6477}
6478
6479static void
56be3814 6480process_g_packet (struct regcache *regcache)
29709017 6481{
4a22f64d 6482 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6483 struct remote_state *rs = get_remote_state ();
6484 struct remote_arch_state *rsa = get_remote_arch_state ();
6485 int i, buf_len;
6486 char *p;
6487 char *regs;
6488
6489 buf_len = strlen (rs->buf);
6490
6491 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6492 if (buf_len > 2 * rsa->sizeof_g_packet)
6493 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6494
6495 /* Save the size of the packet sent to us by the target. It is used
6496 as a heuristic when determining the max size of packets that the
6497 target can safely receive. */
6498 if (rsa->actual_register_packet_size == 0)
6499 rsa->actual_register_packet_size = buf_len;
6500
6501 /* If this is smaller than we guessed the 'g' packet would be,
6502 update our records. A 'g' reply that doesn't include a register's
6503 value implies either that the register is not available, or that
6504 the 'p' packet must be used. */
6505 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6506 {
74ca34ce
DJ
6507 rsa->sizeof_g_packet = buf_len / 2;
6508
4a22f64d 6509 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6510 {
74ca34ce
DJ
6511 if (rsa->regs[i].pnum == -1)
6512 continue;
6513
6514 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6515 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6516 else
74ca34ce 6517 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6518 }
74ca34ce 6519 }
b323314b 6520
74ca34ce 6521 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6522
6523 /* Unimplemented registers read as all bits zero. */
ea9c271d 6524 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6525
c906108c
SS
6526 /* Reply describes registers byte by byte, each byte encoded as two
6527 hex characters. Suck them all up, then supply them to the
6528 register cacheing/storage mechanism. */
6529
74ca34ce 6530 p = rs->buf;
ea9c271d 6531 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6532 {
74ca34ce
DJ
6533 if (p[0] == 0 || p[1] == 0)
6534 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6535 internal_error (__FILE__, __LINE__,
9b20d036 6536 _("unexpected end of 'g' packet reply"));
74ca34ce 6537
c906108c 6538 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6539 regs[i] = 0; /* 'x' */
c906108c
SS
6540 else
6541 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6542 p += 2;
6543 }
6544
a744cf53
MS
6545 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6546 {
6547 struct packet_reg *r = &rsa->regs[i];
6548
6549 if (r->in_g_packet)
6550 {
6551 if (r->offset * 2 >= strlen (rs->buf))
6552 /* This shouldn't happen - we adjusted in_g_packet above. */
6553 internal_error (__FILE__, __LINE__,
9b20d036 6554 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6555 else if (rs->buf[r->offset * 2] == 'x')
6556 {
6557 gdb_assert (r->offset * 2 < strlen (rs->buf));
6558 /* The register isn't available, mark it as such (at
6559 the same time setting the value to zero). */
6560 regcache_raw_supply (regcache, r->regnum, NULL);
6561 }
6562 else
6563 regcache_raw_supply (regcache, r->regnum,
6564 regs + r->offset);
6565 }
6566 }
c906108c
SS
6567}
6568
29709017 6569static void
56be3814 6570fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6571{
6572 send_g_packet ();
56be3814 6573 process_g_packet (regcache);
29709017
DJ
6574}
6575
e6e4e701
PA
6576/* Make the remote selected traceframe match GDB's selected
6577 traceframe. */
6578
6579static void
6580set_remote_traceframe (void)
6581{
6582 int newnum;
262e1174 6583 struct remote_state *rs = get_remote_state ();
e6e4e701 6584
262e1174 6585 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6586 return;
6587
6588 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6589 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6590
6591 newnum = target_trace_find (tfind_number,
6592 get_traceframe_number (), 0, 0, NULL);
6593
6594 /* Should not happen. If it does, all bets are off. */
6595 if (newnum != get_traceframe_number ())
6596 warning (_("could not set remote traceframe"));
6597}
6598
74ca34ce 6599static void
28439f5e
PA
6600remote_fetch_registers (struct target_ops *ops,
6601 struct regcache *regcache, int regnum)
74ca34ce 6602{
74ca34ce
DJ
6603 struct remote_arch_state *rsa = get_remote_arch_state ();
6604 int i;
6605
e6e4e701 6606 set_remote_traceframe ();
79d7f229 6607 set_general_thread (inferior_ptid);
74ca34ce
DJ
6608
6609 if (regnum >= 0)
6610 {
6611 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6612
74ca34ce
DJ
6613 gdb_assert (reg != NULL);
6614
6615 /* If this register might be in the 'g' packet, try that first -
6616 we are likely to read more than one register. If this is the
6617 first 'g' packet, we might be overly optimistic about its
6618 contents, so fall back to 'p'. */
6619 if (reg->in_g_packet)
6620 {
56be3814 6621 fetch_registers_using_g (regcache);
74ca34ce
DJ
6622 if (reg->in_g_packet)
6623 return;
6624 }
6625
56be3814 6626 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6627 return;
6628
6629 /* This register is not available. */
56be3814 6630 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6631
6632 return;
6633 }
6634
56be3814 6635 fetch_registers_using_g (regcache);
74ca34ce 6636
4a22f64d 6637 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6638 if (!rsa->regs[i].in_g_packet)
56be3814 6639 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6640 {
6641 /* This register is not available. */
56be3814 6642 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6643 }
6644}
6645
c906108c
SS
6646/* Prepare to store registers. Since we may send them all (using a
6647 'G' request), we have to read out the ones we don't want to change
6648 first. */
6649
c5aa993b 6650static void
f32dbf8c 6651remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6652{
ea9c271d 6653 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6654 int i;
cfd77fa1 6655 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6656
c906108c 6657 /* Make sure the entire registers array is valid. */
4082afcc 6658 switch (packet_support (PACKET_P))
5a2468f5
JM
6659 {
6660 case PACKET_DISABLE:
6661 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6662 /* Make sure all the necessary registers are cached. */
4a22f64d 6663 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6664 if (rsa->regs[i].in_g_packet)
316f2060 6665 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6666 break;
6667 case PACKET_ENABLE:
6668 break;
6669 }
6670}
6671
ad10f812 6672/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6673 packet was not recognized. */
5a2468f5
JM
6674
6675static int
1f4437a4
MS
6676store_register_using_P (const struct regcache *regcache,
6677 struct packet_reg *reg)
5a2468f5 6678{
4a22f64d 6679 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6680 struct remote_state *rs = get_remote_state ();
5a2468f5 6681 /* Try storing a single register. */
6d820c5c 6682 char *buf = rs->buf;
cfd77fa1 6683 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6684 char *p;
5a2468f5 6685
4082afcc 6686 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6687 return 0;
6688
6689 if (reg->pnum == -1)
6690 return 0;
6691
ea9c271d 6692 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6693 p = buf + strlen (buf);
56be3814 6694 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6695 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6696 putpkt (rs->buf);
6697 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6698
74ca34ce
DJ
6699 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6700 {
6701 case PACKET_OK:
6702 return 1;
6703 case PACKET_ERROR:
27a9c0bf
MS
6704 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6705 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6706 case PACKET_UNKNOWN:
6707 return 0;
6708 default:
6709 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6710 }
c906108c
SS
6711}
6712
23860348
MS
6713/* Store register REGNUM, or all registers if REGNUM == -1, from the
6714 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6715
6716static void
56be3814 6717store_registers_using_G (const struct regcache *regcache)
c906108c 6718{
d01949b6 6719 struct remote_state *rs = get_remote_state ();
ea9c271d 6720 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6721 gdb_byte *regs;
c906108c
SS
6722 char *p;
6723
193cb69f
AC
6724 /* Extract all the registers in the regcache copying them into a
6725 local buffer. */
6726 {
b323314b 6727 int i;
a744cf53 6728
ea9c271d
DJ
6729 regs = alloca (rsa->sizeof_g_packet);
6730 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6731 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6732 {
ea9c271d 6733 struct packet_reg *r = &rsa->regs[i];
a744cf53 6734
b323314b 6735 if (r->in_g_packet)
56be3814 6736 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6737 }
6738 }
c906108c
SS
6739
6740 /* Command describes registers byte by byte,
6741 each byte encoded as two hex characters. */
6d820c5c 6742 p = rs->buf;
193cb69f 6743 *p++ = 'G';
74ca34ce
DJ
6744 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6745 updated. */
6746 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6747 putpkt (rs->buf);
6748 getpkt (&rs->buf, &rs->buf_size, 0);
6749 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6750 error (_("Could not write registers; remote failure reply '%s'"),
6751 rs->buf);
c906108c 6752}
74ca34ce
DJ
6753
6754/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6755 of the register cache buffer. FIXME: ignores errors. */
6756
6757static void
28439f5e
PA
6758remote_store_registers (struct target_ops *ops,
6759 struct regcache *regcache, int regnum)
74ca34ce 6760{
74ca34ce
DJ
6761 struct remote_arch_state *rsa = get_remote_arch_state ();
6762 int i;
6763
e6e4e701 6764 set_remote_traceframe ();
79d7f229 6765 set_general_thread (inferior_ptid);
74ca34ce
DJ
6766
6767 if (regnum >= 0)
6768 {
6769 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6770
74ca34ce
DJ
6771 gdb_assert (reg != NULL);
6772
6773 /* Always prefer to store registers using the 'P' packet if
6774 possible; we often change only a small number of registers.
6775 Sometimes we change a larger number; we'd need help from a
6776 higher layer to know to use 'G'. */
56be3814 6777 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6778 return;
6779
6780 /* For now, don't complain if we have no way to write the
6781 register. GDB loses track of unavailable registers too
6782 easily. Some day, this may be an error. We don't have
0df8b418 6783 any way to read the register, either... */
74ca34ce
DJ
6784 if (!reg->in_g_packet)
6785 return;
6786
56be3814 6787 store_registers_using_G (regcache);
74ca34ce
DJ
6788 return;
6789 }
6790
56be3814 6791 store_registers_using_G (regcache);
74ca34ce 6792
4a22f64d 6793 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6794 if (!rsa->regs[i].in_g_packet)
56be3814 6795 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6796 /* See above for why we do not issue an error here. */
6797 continue;
6798}
c906108c
SS
6799\f
6800
6801/* Return the number of hex digits in num. */
6802
6803static int
fba45db2 6804hexnumlen (ULONGEST num)
c906108c
SS
6805{
6806 int i;
6807
6808 for (i = 0; num != 0; i++)
6809 num >>= 4;
6810
6811 return max (i, 1);
6812}
6813
2df3850c 6814/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6815
6816static int
fba45db2 6817hexnumstr (char *buf, ULONGEST num)
c906108c 6818{
c906108c 6819 int len = hexnumlen (num);
a744cf53 6820
2df3850c
JM
6821 return hexnumnstr (buf, num, len);
6822}
6823
c906108c 6824
2df3850c 6825/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6826
2df3850c 6827static int
fba45db2 6828hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6829{
6830 int i;
6831
6832 buf[width] = '\0';
6833
6834 for (i = width - 1; i >= 0; i--)
c906108c 6835 {
c5aa993b 6836 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6837 num >>= 4;
6838 }
6839
2df3850c 6840 return width;
c906108c
SS
6841}
6842
23860348 6843/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6844
6845static CORE_ADDR
fba45db2 6846remote_address_masked (CORE_ADDR addr)
c906108c 6847{
883b9c6c 6848 unsigned int address_size = remote_address_size;
a744cf53 6849
911c95a5
UW
6850 /* If "remoteaddresssize" was not set, default to target address size. */
6851 if (!address_size)
f5656ead 6852 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6853
6854 if (address_size > 0
6855 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6856 {
6857 /* Only create a mask when that mask can safely be constructed
23860348 6858 in a ULONGEST variable. */
c906108c 6859 ULONGEST mask = 1;
a744cf53 6860
911c95a5 6861 mask = (mask << address_size) - 1;
c906108c
SS
6862 addr &= mask;
6863 }
6864 return addr;
6865}
6866
6867/* Determine whether the remote target supports binary downloading.
6868 This is accomplished by sending a no-op memory write of zero length
6869 to the target at the specified address. It does not suffice to send
23860348
MS
6870 the whole packet, since many stubs strip the eighth bit and
6871 subsequently compute a wrong checksum, which causes real havoc with
6872 remote_write_bytes.
7a292a7a 6873
96baa820 6874 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6875 clean. In cases like this, the user should clear "remote
23860348 6876 X-packet". */
96baa820 6877
c906108c 6878static void
fba45db2 6879check_binary_download (CORE_ADDR addr)
c906108c 6880{
d01949b6 6881 struct remote_state *rs = get_remote_state ();
24b06219 6882
4082afcc 6883 switch (packet_support (PACKET_X))
c906108c 6884 {
96baa820
JM
6885 case PACKET_DISABLE:
6886 break;
6887 case PACKET_ENABLE:
6888 break;
6889 case PACKET_SUPPORT_UNKNOWN:
6890 {
96baa820 6891 char *p;
802188a7 6892
2e9f7625 6893 p = rs->buf;
96baa820
JM
6894 *p++ = 'X';
6895 p += hexnumstr (p, (ULONGEST) addr);
6896 *p++ = ',';
6897 p += hexnumstr (p, (ULONGEST) 0);
6898 *p++ = ':';
6899 *p = '\0';
802188a7 6900
2e9f7625 6901 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6902 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6903
2e9f7625 6904 if (rs->buf[0] == '\0')
96baa820
JM
6905 {
6906 if (remote_debug)
6907 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6908 "binary downloading NOT "
6909 "supported by target\n");
444abaca 6910 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6911 }
6912 else
6913 {
6914 if (remote_debug)
6915 fprintf_unfiltered (gdb_stdlog,
64b9b334 6916 "binary downloading supported by target\n");
444abaca 6917 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6918 }
6919 break;
6920 }
c906108c
SS
6921 }
6922}
6923
124e13d9
SM
6924/* Helper function to resize the payload in order to try to get a good
6925 alignment. We try to write an amount of data such that the next write will
6926 start on an address aligned on REMOTE_ALIGN_WRITES. */
6927
6928static int
6929align_for_efficient_write (int todo, CORE_ADDR memaddr)
6930{
6931 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6932}
6933
c906108c
SS
6934/* Write memory data directly to the remote machine.
6935 This does not inform the data cache; the data cache uses this.
a76d924d 6936 HEADER is the starting part of the packet.
c906108c
SS
6937 MEMADDR is the address in the remote memory space.
6938 MYADDR is the address of the buffer in our space.
124e13d9
SM
6939 LEN_UNITS is the number of addressable units to write.
6940 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
6941 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6942 should send data as binary ('X'), or hex-encoded ('M').
6943
6944 The function creates packet of the form
6945 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6946
124e13d9 6947 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
6948
6949 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6950 are omitted.
6951
9b409511 6952 Return the transferred status, error or OK (an
124e13d9
SM
6953 'enum target_xfer_status' value). Save the number of addressable units
6954 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
6955
6956 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
6957 exchange between gdb and the stub could look like (?? in place of the
6958 checksum):
6959
6960 -> $m1000,4#??
6961 <- aaaabbbbccccdddd
6962
6963 -> $M1000,3:eeeeffffeeee#??
6964 <- OK
6965
6966 -> $m1000,4#??
6967 <- eeeeffffeeeedddd */
c906108c 6968
9b409511 6969static enum target_xfer_status
a76d924d 6970remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
6971 const gdb_byte *myaddr, ULONGEST len_units,
6972 int unit_size, ULONGEST *xfered_len_units,
6973 char packet_format, int use_length)
c906108c 6974{
6d820c5c 6975 struct remote_state *rs = get_remote_state ();
cfd77fa1 6976 char *p;
a76d924d
DJ
6977 char *plen = NULL;
6978 int plenlen = 0;
124e13d9
SM
6979 int todo_units;
6980 int units_written;
6981 int payload_capacity_bytes;
6982 int payload_length_bytes;
a76d924d
DJ
6983
6984 if (packet_format != 'X' && packet_format != 'M')
6985 internal_error (__FILE__, __LINE__,
9b20d036 6986 _("remote_write_bytes_aux: bad packet format"));
c906108c 6987
124e13d9 6988 if (len_units == 0)
9b409511 6989 return TARGET_XFER_EOF;
b2182ed2 6990
124e13d9 6991 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 6992
6d820c5c
DJ
6993 /* The packet buffer will be large enough for the payload;
6994 get_memory_packet_size ensures this. */
a76d924d 6995 rs->buf[0] = '\0';
c906108c 6996
a257b5bb 6997 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
6998 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6999
124e13d9 7000 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7001 if (!use_length)
0df8b418 7002 /* The comma won't be used. */
124e13d9
SM
7003 payload_capacity_bytes += 1;
7004 payload_capacity_bytes -= strlen (header);
7005 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7006
a76d924d 7007 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7008
a76d924d
DJ
7009 strcat (rs->buf, header);
7010 p = rs->buf + strlen (header);
7011
7012 /* Compute a best guess of the number of bytes actually transfered. */
7013 if (packet_format == 'X')
c906108c 7014 {
23860348 7015 /* Best guess at number of bytes that will fit. */
124e13d9 7016 todo_units = min (len_units, payload_capacity_bytes / unit_size);
a76d924d 7017 if (use_length)
124e13d9
SM
7018 payload_capacity_bytes -= hexnumlen (todo_units);
7019 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7020 }
7021 else
7022 {
124e13d9
SM
7023 /* Number of bytes that will fit. */
7024 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
a76d924d 7025 if (use_length)
124e13d9
SM
7026 payload_capacity_bytes -= hexnumlen (todo_units);
7027 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
917317f4 7028 }
a76d924d 7029
124e13d9 7030 if (todo_units <= 0)
3de11b2e 7031 internal_error (__FILE__, __LINE__,
405f8e94 7032 _("minimum packet size too small to write data"));
802188a7 7033
6765f3e5
DJ
7034 /* If we already need another packet, then try to align the end
7035 of this packet to a useful boundary. */
124e13d9
SM
7036 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7037 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7038
a257b5bb 7039 /* Append "<memaddr>". */
917317f4
JM
7040 memaddr = remote_address_masked (memaddr);
7041 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7042
a76d924d
DJ
7043 if (use_length)
7044 {
7045 /* Append ",". */
7046 *p++ = ',';
802188a7 7047
124e13d9
SM
7048 /* Append the length and retain its location and size. It may need to be
7049 adjusted once the packet body has been created. */
a76d924d 7050 plen = p;
124e13d9 7051 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7052 p += plenlen;
7053 }
a257b5bb
AC
7054
7055 /* Append ":". */
917317f4
JM
7056 *p++ = ':';
7057 *p = '\0';
802188a7 7058
a257b5bb 7059 /* Append the packet body. */
a76d924d 7060 if (packet_format == 'X')
917317f4 7061 {
917317f4
JM
7062 /* Binary mode. Send target system values byte by byte, in
7063 increasing byte addresses. Only escape certain critical
7064 characters. */
124e13d9
SM
7065 payload_length_bytes =
7066 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7067 &units_written, payload_capacity_bytes);
6765f3e5 7068
124e13d9 7069 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7070 a second try to keep the end of the packet aligned. Don't do
7071 this if the packet is tiny. */
124e13d9 7072 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7073 {
124e13d9
SM
7074 int new_todo_units;
7075
7076 new_todo_units = align_for_efficient_write (units_written, memaddr);
7077
7078 if (new_todo_units != units_written)
7079 payload_length_bytes =
7080 remote_escape_output (myaddr, new_todo_units, unit_size,
7081 (gdb_byte *) p, &units_written,
7082 payload_capacity_bytes);
6765f3e5
DJ
7083 }
7084
124e13d9
SM
7085 p += payload_length_bytes;
7086 if (use_length && units_written < todo_units)
c906108c 7087 {
802188a7 7088 /* Escape chars have filled up the buffer prematurely,
124e13d9 7089 and we have actually sent fewer units than planned.
917317f4
JM
7090 Fix-up the length field of the packet. Use the same
7091 number of characters as before. */
124e13d9
SM
7092 plen += hexnumnstr (plen, (ULONGEST) units_written,
7093 plenlen);
917317f4 7094 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7095 }
a76d924d
DJ
7096 }
7097 else
7098 {
917317f4
JM
7099 /* Normal mode: Send target system values byte by byte, in
7100 increasing byte addresses. Each byte is encoded as a two hex
7101 value. */
124e13d9
SM
7102 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7103 units_written = todo_units;
c906108c 7104 }
802188a7 7105
2e9f7625 7106 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7107 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7108
2e9f7625 7109 if (rs->buf[0] == 'E')
00d84524 7110 return TARGET_XFER_E_IO;
802188a7 7111
124e13d9
SM
7112 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7113 send fewer units than we'd planned. */
7114 *xfered_len_units = (ULONGEST) units_written;
9b409511 7115 return TARGET_XFER_OK;
c906108c
SS
7116}
7117
a76d924d
DJ
7118/* Write memory data directly to the remote machine.
7119 This does not inform the data cache; the data cache uses this.
7120 MEMADDR is the address in the remote memory space.
7121 MYADDR is the address of the buffer in our space.
7122 LEN is the number of bytes.
7123
9b409511
YQ
7124 Return the transferred status, error or OK (an
7125 'enum target_xfer_status' value). Save the number of bytes
7126 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7127
9b409511
YQ
7128static enum target_xfer_status
7129remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7130 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7131{
7132 char *packet_format = 0;
7133
7134 /* Check whether the target supports binary download. */
7135 check_binary_download (memaddr);
7136
4082afcc 7137 switch (packet_support (PACKET_X))
a76d924d
DJ
7138 {
7139 case PACKET_ENABLE:
7140 packet_format = "X";
7141 break;
7142 case PACKET_DISABLE:
7143 packet_format = "M";
7144 break;
7145 case PACKET_SUPPORT_UNKNOWN:
7146 internal_error (__FILE__, __LINE__,
7147 _("remote_write_bytes: bad internal state"));
7148 default:
7149 internal_error (__FILE__, __LINE__, _("bad switch"));
7150 }
7151
7152 return remote_write_bytes_aux (packet_format,
124e13d9 7153 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7154 packet_format[0], 1);
a76d924d
DJ
7155}
7156
9217e74e
YQ
7157/* Read memory data directly from the remote machine.
7158 This does not use the data cache; the data cache uses this.
7159 MEMADDR is the address in the remote memory space.
7160 MYADDR is the address of the buffer in our space.
124e13d9
SM
7161 LEN_UNITS is the number of addressable memory units to read..
7162 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7163
7164 Return the transferred status, error or OK (an
7165 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7166 transferred in *XFERED_LEN_UNITS.
7167
7168 See the comment of remote_write_bytes_aux for an example of
7169 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7170
7171static enum target_xfer_status
124e13d9
SM
7172remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7173 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7174{
7175 struct remote_state *rs = get_remote_state ();
124e13d9 7176 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7177 char *p;
124e13d9
SM
7178 int todo_units;
7179 int decoded_bytes;
9217e74e 7180
124e13d9 7181 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7182 /* The packet buffer will be large enough for the payload;
7183 get_memory_packet_size ensures this. */
7184
124e13d9
SM
7185 /* Number of units that will fit. */
7186 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7187
7188 /* Construct "m"<memaddr>","<len>". */
7189 memaddr = remote_address_masked (memaddr);
7190 p = rs->buf;
7191 *p++ = 'm';
7192 p += hexnumstr (p, (ULONGEST) memaddr);
7193 *p++ = ',';
124e13d9 7194 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7195 *p = '\0';
7196 putpkt (rs->buf);
7197 getpkt (&rs->buf, &rs->buf_size, 0);
7198 if (rs->buf[0] == 'E'
7199 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7200 && rs->buf[3] == '\0')
7201 return TARGET_XFER_E_IO;
7202 /* Reply describes memory byte by byte, each byte encoded as two hex
7203 characters. */
7204 p = rs->buf;
124e13d9 7205 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7206 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7207 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7208 return TARGET_XFER_OK;
7209}
7210
b55fbac4
YQ
7211/* Using the set of read-only target sections of remote, read live
7212 read-only memory.
8acf9577
YQ
7213
7214 For interface/parameters/return description see target.h,
7215 to_xfer_partial. */
7216
7217static enum target_xfer_status
b55fbac4
YQ
7218remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7219 ULONGEST memaddr, ULONGEST len,
124e13d9 7220 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7221{
7222 struct target_section *secp;
7223 struct target_section_table *table;
7224
7225 secp = target_section_by_addr (ops, memaddr);
7226 if (secp != NULL
7227 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7228 secp->the_bfd_section)
7229 & SEC_READONLY))
7230 {
7231 struct target_section *p;
7232 ULONGEST memend = memaddr + len;
7233
7234 table = target_get_section_table (ops);
7235
7236 for (p = table->sections; p < table->sections_end; p++)
7237 {
7238 if (memaddr >= p->addr)
7239 {
7240 if (memend <= p->endaddr)
7241 {
7242 /* Entire transfer is within this section. */
124e13d9 7243 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7244 xfered_len);
8acf9577
YQ
7245 }
7246 else if (memaddr >= p->endaddr)
7247 {
7248 /* This section ends before the transfer starts. */
7249 continue;
7250 }
7251 else
7252 {
7253 /* This section overlaps the transfer. Just do half. */
7254 len = p->endaddr - memaddr;
124e13d9 7255 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7256 xfered_len);
8acf9577
YQ
7257 }
7258 }
7259 }
7260 }
7261
7262 return TARGET_XFER_EOF;
7263}
7264
9217e74e
YQ
7265/* Similar to remote_read_bytes_1, but it reads from the remote stub
7266 first if the requested memory is unavailable in traceframe.
7267 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7268
9b409511 7269static enum target_xfer_status
8acf9577 7270remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7271 gdb_byte *myaddr, ULONGEST len, int unit_size,
7272 ULONGEST *xfered_len)
c906108c 7273{
6b6aa828 7274 if (len == 0)
96c4f946 7275 return TARGET_XFER_EOF;
b2182ed2 7276
8acf9577
YQ
7277 if (get_traceframe_number () != -1)
7278 {
7279 VEC(mem_range_s) *available;
7280
7281 /* If we fail to get the set of available memory, then the
7282 target does not support querying traceframe info, and so we
7283 attempt reading from the traceframe anyway (assuming the
7284 target implements the old QTro packet then). */
7285 if (traceframe_available_memory (&available, memaddr, len))
7286 {
7287 struct cleanup *old_chain;
7288
7289 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7290
7291 if (VEC_empty (mem_range_s, available)
7292 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7293 {
7294 enum target_xfer_status res;
7295
7296 /* Don't read into the traceframe's available
7297 memory. */
7298 if (!VEC_empty (mem_range_s, available))
7299 {
7300 LONGEST oldlen = len;
7301
7302 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7303 gdb_assert (len <= oldlen);
7304 }
7305
7306 do_cleanups (old_chain);
7307
7308 /* This goes through the topmost target again. */
b55fbac4 7309 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7310 len, unit_size, xfered_len);
8acf9577
YQ
7311 if (res == TARGET_XFER_OK)
7312 return TARGET_XFER_OK;
7313 else
7314 {
7315 /* No use trying further, we know some memory starting
7316 at MEMADDR isn't available. */
7317 *xfered_len = len;
7318 return TARGET_XFER_UNAVAILABLE;
7319 }
7320 }
7321
7322 /* Don't try to read more than how much is available, in
7323 case the target implements the deprecated QTro packet to
7324 cater for older GDBs (the target's knowledge of read-only
7325 sections may be outdated by now). */
7326 len = VEC_index (mem_range_s, available, 0)->length;
7327
7328 do_cleanups (old_chain);
7329 }
7330 }
7331
124e13d9 7332 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 7333}
74531fed 7334
c906108c 7335\f
c906108c 7336
a76d924d
DJ
7337/* Sends a packet with content determined by the printf format string
7338 FORMAT and the remaining arguments, then gets the reply. Returns
7339 whether the packet was a success, a failure, or unknown. */
7340
77b64a49
PA
7341static enum packet_result remote_send_printf (const char *format, ...)
7342 ATTRIBUTE_PRINTF (1, 2);
7343
2c0b251b 7344static enum packet_result
a76d924d
DJ
7345remote_send_printf (const char *format, ...)
7346{
7347 struct remote_state *rs = get_remote_state ();
7348 int max_size = get_remote_packet_size ();
a76d924d 7349 va_list ap;
a744cf53 7350
a76d924d
DJ
7351 va_start (ap, format);
7352
7353 rs->buf[0] = '\0';
7354 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7355 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7356
7357 if (putpkt (rs->buf) < 0)
7358 error (_("Communication problem with target."));
7359
7360 rs->buf[0] = '\0';
7361 getpkt (&rs->buf, &rs->buf_size, 0);
7362
7363 return packet_check_result (rs->buf);
7364}
7365
7366static void
7367restore_remote_timeout (void *p)
7368{
7369 int value = *(int *)p;
a744cf53 7370
a76d924d
DJ
7371 remote_timeout = value;
7372}
7373
7374/* Flash writing can take quite some time. We'll set
7375 effectively infinite timeout for flash operations.
7376 In future, we'll need to decide on a better approach. */
7377static const int remote_flash_timeout = 1000;
7378
7379static void
7380remote_flash_erase (struct target_ops *ops,
7381 ULONGEST address, LONGEST length)
7382{
f5656ead 7383 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7384 int saved_remote_timeout = remote_timeout;
7385 enum packet_result ret;
a76d924d
DJ
7386 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7387 &saved_remote_timeout);
a744cf53 7388
a76d924d
DJ
7389 remote_timeout = remote_flash_timeout;
7390
7391 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7392 phex (address, addr_size),
a76d924d
DJ
7393 phex (length, 4));
7394 switch (ret)
7395 {
7396 case PACKET_UNKNOWN:
7397 error (_("Remote target does not support flash erase"));
7398 case PACKET_ERROR:
7399 error (_("Error erasing flash with vFlashErase packet"));
7400 default:
7401 break;
7402 }
7403
7404 do_cleanups (back_to);
7405}
7406
9b409511
YQ
7407static enum target_xfer_status
7408remote_flash_write (struct target_ops *ops, ULONGEST address,
7409 ULONGEST length, ULONGEST *xfered_len,
7410 const gdb_byte *data)
a76d924d
DJ
7411{
7412 int saved_remote_timeout = remote_timeout;
9b409511 7413 enum target_xfer_status ret;
a76d924d 7414 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7415 &saved_remote_timeout);
a76d924d
DJ
7416
7417 remote_timeout = remote_flash_timeout;
124e13d9 7418 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 7419 xfered_len,'X', 0);
a76d924d
DJ
7420 do_cleanups (back_to);
7421
7422 return ret;
7423}
7424
7425static void
7426remote_flash_done (struct target_ops *ops)
7427{
7428 int saved_remote_timeout = remote_timeout;
7429 int ret;
7430 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7431 &saved_remote_timeout);
7432
7433 remote_timeout = remote_flash_timeout;
7434 ret = remote_send_printf ("vFlashDone");
7435 do_cleanups (back_to);
7436
7437 switch (ret)
7438 {
7439 case PACKET_UNKNOWN:
7440 error (_("Remote target does not support vFlashDone"));
7441 case PACKET_ERROR:
7442 error (_("Error finishing flash operation"));
7443 default:
7444 break;
7445 }
7446}
7447
c906108c 7448static void
fba45db2 7449remote_files_info (struct target_ops *ignore)
c906108c
SS
7450{
7451 puts_filtered ("Debugging a target over a serial line.\n");
7452}
7453\f
7454/* Stuff for dealing with the packets which are part of this protocol.
7455 See comment at top of file for details. */
7456
1927e618
PA
7457/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7458 error to higher layers. Called when a serial error is detected.
7459 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7460 the system error message for errno at function entry and final dot
7461 for output compatibility with throw_perror_with_name. */
1927e618
PA
7462
7463static void
7464unpush_and_perror (const char *string)
7465{
d6cb50a2 7466 int saved_errno = errno;
1927e618
PA
7467
7468 remote_unpush_target ();
d6cb50a2
JK
7469 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7470 safe_strerror (saved_errno));
1927e618
PA
7471}
7472
0876f84a 7473/* Read a single character from the remote end. */
c906108c
SS
7474
7475static int
fba45db2 7476readchar (int timeout)
c906108c
SS
7477{
7478 int ch;
5d93a237 7479 struct remote_state *rs = get_remote_state ();
c906108c 7480
5d93a237 7481 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7482
2acceee2 7483 if (ch >= 0)
0876f84a 7484 return ch;
2acceee2
JM
7485
7486 switch ((enum serial_rc) ch)
c906108c
SS
7487 {
7488 case SERIAL_EOF:
78a095c3 7489 remote_unpush_target ();
598d3636 7490 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7491 /* no return */
c906108c 7492 case SERIAL_ERROR:
1927e618
PA
7493 unpush_and_perror (_("Remote communication error. "
7494 "Target disconnected."));
2acceee2 7495 /* no return */
c906108c 7496 case SERIAL_TIMEOUT:
2acceee2 7497 break;
c906108c 7498 }
2acceee2 7499 return ch;
c906108c
SS
7500}
7501
c33e31fd
PA
7502/* Wrapper for serial_write that closes the target and throws if
7503 writing fails. */
7504
7505static void
7506remote_serial_write (const char *str, int len)
7507{
5d93a237
TT
7508 struct remote_state *rs = get_remote_state ();
7509
7510 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7511 {
1927e618
PA
7512 unpush_and_perror (_("Remote communication error. "
7513 "Target disconnected."));
c33e31fd
PA
7514 }
7515}
7516
6d820c5c
DJ
7517/* Send the command in *BUF to the remote machine, and read the reply
7518 into *BUF. Report an error if we get an error reply. Resize
7519 *BUF using xrealloc if necessary to hold the result, and update
7520 *SIZEOF_BUF. */
c906108c
SS
7521
7522static void
6d820c5c
DJ
7523remote_send (char **buf,
7524 long *sizeof_buf)
c906108c 7525{
6d820c5c 7526 putpkt (*buf);
c2d11a7d 7527 getpkt (buf, sizeof_buf, 0);
c906108c 7528
6d820c5c
DJ
7529 if ((*buf)[0] == 'E')
7530 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7531}
7532
6e5abd65
PA
7533/* Return a pointer to an xmalloc'ed string representing an escaped
7534 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7535 etc. The caller is responsible for releasing the returned
7536 memory. */
7537
7538static char *
7539escape_buffer (const char *buf, int n)
7540{
7541 struct cleanup *old_chain;
7542 struct ui_file *stb;
7543 char *str;
6e5abd65
PA
7544
7545 stb = mem_fileopen ();
7546 old_chain = make_cleanup_ui_file_delete (stb);
7547
6ef284bd 7548 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7549 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7550 do_cleanups (old_chain);
7551 return str;
7552}
7553
c906108c
SS
7554/* Display a null-terminated packet on stdout, for debugging, using C
7555 string notation. */
7556
7557static void
baa336ce 7558print_packet (const char *buf)
c906108c
SS
7559{
7560 puts_filtered ("\"");
43e526b9 7561 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7562 puts_filtered ("\"");
7563}
7564
7565int
baa336ce 7566putpkt (const char *buf)
c906108c
SS
7567{
7568 return putpkt_binary (buf, strlen (buf));
7569}
7570
7571/* Send a packet to the remote machine, with error checking. The data
23860348 7572 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7573 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7574 and for a possible /0 if we are debugging (remote_debug) and want
7575 to print the sent packet as a string. */
c906108c
SS
7576
7577static int
baa336ce 7578putpkt_binary (const char *buf, int cnt)
c906108c 7579{
2d717e4f 7580 struct remote_state *rs = get_remote_state ();
c906108c
SS
7581 int i;
7582 unsigned char csum = 0;
11cf8741 7583 char *buf2 = alloca (cnt + 6);
085dd6e6 7584
c906108c
SS
7585 int ch;
7586 int tcount = 0;
7587 char *p;
dd61ec5c 7588 char *message;
c906108c 7589
e24a49d8
PA
7590 /* Catch cases like trying to read memory or listing threads while
7591 we're waiting for a stop reply. The remote server wouldn't be
7592 ready to handle this request, so we'd hang and timeout. We don't
7593 have to worry about this in synchronous mode, because in that
7594 case it's not possible to issue a command while the target is
74531fed
PA
7595 running. This is not a problem in non-stop mode, because in that
7596 case, the stub is always ready to process serial input. */
d9d41e78 7597 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7598 {
7599 error (_("Cannot execute this command while the target is running.\n"
7600 "Use the \"interrupt\" command to stop the target\n"
7601 "and then try again."));
7602 }
e24a49d8 7603
2d717e4f
DJ
7604 /* We're sending out a new packet. Make sure we don't look at a
7605 stale cached response. */
7606 rs->cached_wait_status = 0;
7607
c906108c
SS
7608 /* Copy the packet into buffer BUF2, encapsulating it
7609 and giving it a checksum. */
7610
c906108c
SS
7611 p = buf2;
7612 *p++ = '$';
7613
7614 for (i = 0; i < cnt; i++)
7615 {
7616 csum += buf[i];
7617 *p++ = buf[i];
7618 }
7619 *p++ = '#';
7620 *p++ = tohex ((csum >> 4) & 0xf);
7621 *p++ = tohex (csum & 0xf);
7622
7623 /* Send it over and over until we get a positive ack. */
7624
7625 while (1)
7626 {
7627 int started_error_output = 0;
7628
7629 if (remote_debug)
7630 {
6e5abd65
PA
7631 struct cleanup *old_chain;
7632 char *str;
7633
c906108c 7634 *p = '\0';
6e5abd65
PA
7635 str = escape_buffer (buf2, p - buf2);
7636 old_chain = make_cleanup (xfree, str);
7637 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7638 gdb_flush (gdb_stdlog);
6e5abd65 7639 do_cleanups (old_chain);
c906108c 7640 }
c33e31fd 7641 remote_serial_write (buf2, p - buf2);
c906108c 7642
a6f3e723
SL
7643 /* If this is a no acks version of the remote protocol, send the
7644 packet and move on. */
7645 if (rs->noack_mode)
7646 break;
7647
74531fed
PA
7648 /* Read until either a timeout occurs (-2) or '+' is read.
7649 Handle any notification that arrives in the mean time. */
c906108c
SS
7650 while (1)
7651 {
7652 ch = readchar (remote_timeout);
7653
c5aa993b 7654 if (remote_debug)
c906108c
SS
7655 {
7656 switch (ch)
7657 {
7658 case '+':
1216fa2c 7659 case '-':
c906108c
SS
7660 case SERIAL_TIMEOUT:
7661 case '$':
74531fed 7662 case '%':
c906108c
SS
7663 if (started_error_output)
7664 {
7665 putchar_unfiltered ('\n');
7666 started_error_output = 0;
7667 }
7668 }
7669 }
7670
7671 switch (ch)
7672 {
7673 case '+':
7674 if (remote_debug)
0f71a2f6 7675 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7676 return 1;
1216fa2c
AC
7677 case '-':
7678 if (remote_debug)
7679 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7680 /* FALLTHROUGH */
c906108c 7681 case SERIAL_TIMEOUT:
c5aa993b 7682 tcount++;
c906108c
SS
7683 if (tcount > 3)
7684 return 0;
23860348 7685 break; /* Retransmit buffer. */
c906108c
SS
7686 case '$':
7687 {
40e3f985 7688 if (remote_debug)
2bc416ba 7689 fprintf_unfiltered (gdb_stdlog,
23860348 7690 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7691 /* It's probably an old response sent because an ACK
7692 was lost. Gobble up the packet and ack it so it
7693 doesn't get retransmitted when we resend this
7694 packet. */
6d820c5c 7695 skip_frame ();
c33e31fd 7696 remote_serial_write ("+", 1);
23860348 7697 continue; /* Now, go look for +. */
c906108c 7698 }
74531fed
PA
7699
7700 case '%':
7701 {
7702 int val;
7703
7704 /* If we got a notification, handle it, and go back to looking
7705 for an ack. */
7706 /* We've found the start of a notification. Now
7707 collect the data. */
7708 val = read_frame (&rs->buf, &rs->buf_size);
7709 if (val >= 0)
7710 {
7711 if (remote_debug)
7712 {
6e5abd65
PA
7713 struct cleanup *old_chain;
7714 char *str;
7715
7716 str = escape_buffer (rs->buf, val);
7717 old_chain = make_cleanup (xfree, str);
7718 fprintf_unfiltered (gdb_stdlog,
7719 " Notification received: %s\n",
7720 str);
7721 do_cleanups (old_chain);
74531fed 7722 }
5965e028 7723 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7724 /* We're in sync now, rewait for the ack. */
7725 tcount = 0;
7726 }
7727 else
7728 {
7729 if (remote_debug)
7730 {
7731 if (!started_error_output)
7732 {
7733 started_error_output = 1;
7734 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7735 }
7736 fputc_unfiltered (ch & 0177, gdb_stdlog);
7737 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7738 }
7739 }
7740 continue;
7741 }
7742 /* fall-through */
c906108c
SS
7743 default:
7744 if (remote_debug)
7745 {
7746 if (!started_error_output)
7747 {
7748 started_error_output = 1;
0f71a2f6 7749 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7750 }
0f71a2f6 7751 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7752 }
7753 continue;
7754 }
23860348 7755 break; /* Here to retransmit. */
c906108c
SS
7756 }
7757
7758#if 0
7759 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7760 able to get out next time we call QUIT, without anything as
7761 violent as interrupt_query. If we want to provide a way out of
7762 here without getting to the next QUIT, it should be based on
7763 hitting ^C twice as in remote_wait. */
c906108c
SS
7764 if (quit_flag)
7765 {
7766 quit_flag = 0;
7767 interrupt_query ();
7768 }
7769#endif
7770 }
a6f3e723 7771 return 0;
c906108c
SS
7772}
7773
6d820c5c
DJ
7774/* Come here after finding the start of a frame when we expected an
7775 ack. Do our best to discard the rest of this packet. */
7776
7777static void
7778skip_frame (void)
7779{
7780 int c;
7781
7782 while (1)
7783 {
7784 c = readchar (remote_timeout);
7785 switch (c)
7786 {
7787 case SERIAL_TIMEOUT:
7788 /* Nothing we can do. */
7789 return;
7790 case '#':
7791 /* Discard the two bytes of checksum and stop. */
7792 c = readchar (remote_timeout);
7793 if (c >= 0)
7794 c = readchar (remote_timeout);
7795
7796 return;
7797 case '*': /* Run length encoding. */
7798 /* Discard the repeat count. */
7799 c = readchar (remote_timeout);
7800 if (c < 0)
7801 return;
7802 break;
7803 default:
7804 /* A regular character. */
7805 break;
7806 }
7807 }
7808}
7809
c906108c 7810/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7811 into *BUF, verifying the checksum, length, and handling run-length
7812 compression. NUL terminate the buffer. If there is not enough room,
7813 expand *BUF using xrealloc.
c906108c 7814
c2d11a7d
JM
7815 Returns -1 on error, number of characters in buffer (ignoring the
7816 trailing NULL) on success. (could be extended to return one of the
23860348 7817 SERIAL status indications). */
c2d11a7d
JM
7818
7819static long
6d820c5c
DJ
7820read_frame (char **buf_p,
7821 long *sizeof_buf)
c906108c
SS
7822{
7823 unsigned char csum;
c2d11a7d 7824 long bc;
c906108c 7825 int c;
6d820c5c 7826 char *buf = *buf_p;
a6f3e723 7827 struct remote_state *rs = get_remote_state ();
c906108c
SS
7828
7829 csum = 0;
c2d11a7d 7830 bc = 0;
c906108c
SS
7831
7832 while (1)
7833 {
7834 c = readchar (remote_timeout);
c906108c
SS
7835 switch (c)
7836 {
7837 case SERIAL_TIMEOUT:
7838 if (remote_debug)
0f71a2f6 7839 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7840 return -1;
c906108c
SS
7841 case '$':
7842 if (remote_debug)
0f71a2f6
JM
7843 fputs_filtered ("Saw new packet start in middle of old one\n",
7844 gdb_stdlog);
23860348 7845 return -1; /* Start a new packet, count retries. */
c906108c
SS
7846 case '#':
7847 {
7848 unsigned char pktcsum;
e1b09194
AC
7849 int check_0 = 0;
7850 int check_1 = 0;
c906108c 7851
c2d11a7d 7852 buf[bc] = '\0';
c906108c 7853
e1b09194
AC
7854 check_0 = readchar (remote_timeout);
7855 if (check_0 >= 0)
7856 check_1 = readchar (remote_timeout);
802188a7 7857
e1b09194
AC
7858 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7859 {
7860 if (remote_debug)
2bc416ba 7861 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7862 gdb_stdlog);
e1b09194
AC
7863 return -1;
7864 }
7865 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7866 {
7867 if (remote_debug)
2bc416ba 7868 fputs_filtered ("Communication error in checksum\n",
23860348 7869 gdb_stdlog);
40e3f985
FN
7870 return -1;
7871 }
c906108c 7872
a6f3e723
SL
7873 /* Don't recompute the checksum; with no ack packets we
7874 don't have any way to indicate a packet retransmission
7875 is necessary. */
7876 if (rs->noack_mode)
7877 return bc;
7878
e1b09194 7879 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7880 if (csum == pktcsum)
c2d11a7d 7881 return bc;
c906108c 7882
c5aa993b 7883 if (remote_debug)
c906108c 7884 {
6e5abd65
PA
7885 struct cleanup *old_chain;
7886 char *str;
7887
7888 str = escape_buffer (buf, bc);
7889 old_chain = make_cleanup (xfree, str);
7890 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7891 "Bad checksum, sentsum=0x%x, "
7892 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7893 pktcsum, csum, str);
7894 do_cleanups (old_chain);
c906108c 7895 }
c2d11a7d 7896 /* Number of characters in buffer ignoring trailing
23860348 7897 NULL. */
c2d11a7d 7898 return -1;
c906108c 7899 }
23860348 7900 case '*': /* Run length encoding. */
c2c6d25f
JM
7901 {
7902 int repeat;
c906108c 7903
a744cf53 7904 csum += c;
b4501125
AC
7905 c = readchar (remote_timeout);
7906 csum += c;
23860348 7907 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7908
23860348 7909 /* The character before ``*'' is repeated. */
c2d11a7d 7910
6d820c5c 7911 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7912 {
6d820c5c
DJ
7913 if (bc + repeat - 1 >= *sizeof_buf - 1)
7914 {
7915 /* Make some more room in the buffer. */
7916 *sizeof_buf += repeat;
7917 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7918 buf = *buf_p;
7919 }
7920
c2d11a7d
JM
7921 memset (&buf[bc], buf[bc - 1], repeat);
7922 bc += repeat;
c2c6d25f
JM
7923 continue;
7924 }
7925
c2d11a7d 7926 buf[bc] = '\0';
6d820c5c 7927 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7928 return -1;
c2c6d25f 7929 }
c906108c 7930 default:
6d820c5c 7931 if (bc >= *sizeof_buf - 1)
c906108c 7932 {
6d820c5c
DJ
7933 /* Make some more room in the buffer. */
7934 *sizeof_buf *= 2;
7935 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7936 buf = *buf_p;
c906108c
SS
7937 }
7938
6d820c5c
DJ
7939 buf[bc++] = c;
7940 csum += c;
7941 continue;
c906108c
SS
7942 }
7943 }
7944}
7945
7946/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7947 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7948 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7949 rather than timing out; this is used (in synchronous mode) to wait
7950 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7951/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7952 don't have to change all the calls to getpkt to deal with the
7953 return value, because at the moment I don't know what the right
23860348 7954 thing to do it for those. */
c906108c 7955void
6d820c5c
DJ
7956getpkt (char **buf,
7957 long *sizeof_buf,
c2d11a7d 7958 int forever)
d9fcf2fb
JM
7959{
7960 int timed_out;
7961
7962 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7963}
7964
7965
7966/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7967 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7968 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7969 rather than timing out; this is used (in synchronous mode) to wait
7970 for a target that is is executing user code to stop. If FOREVER ==
7971 0, this function is allowed to time out gracefully and return an
74531fed
PA
7972 indication of this to the caller. Otherwise return the number of
7973 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
7974 enough reason to return to the caller. *IS_NOTIF is an output
7975 boolean that indicates whether *BUF holds a notification or not
7976 (a regular packet). */
74531fed 7977
3172dc30 7978static int
74531fed 7979getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 7980 int expecting_notif, int *is_notif)
c906108c 7981{
2d717e4f 7982 struct remote_state *rs = get_remote_state ();
c906108c
SS
7983 int c;
7984 int tries;
7985 int timeout;
df4b58fe 7986 int val = -1;
c906108c 7987
2d717e4f
DJ
7988 /* We're reading a new response. Make sure we don't look at a
7989 previously cached response. */
7990 rs->cached_wait_status = 0;
7991
6d820c5c 7992 strcpy (*buf, "timeout");
c906108c
SS
7993
7994 if (forever)
74531fed
PA
7995 timeout = watchdog > 0 ? watchdog : -1;
7996 else if (expecting_notif)
7997 timeout = 0; /* There should already be a char in the buffer. If
7998 not, bail out. */
c906108c
SS
7999 else
8000 timeout = remote_timeout;
8001
8002#define MAX_TRIES 3
8003
74531fed
PA
8004 /* Process any number of notifications, and then return when
8005 we get a packet. */
8006 for (;;)
c906108c 8007 {
d9c43928 8008 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8009 times. */
8010 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8011 {
74531fed
PA
8012 /* This can loop forever if the remote side sends us
8013 characters continuously, but if it pauses, we'll get
8014 SERIAL_TIMEOUT from readchar because of timeout. Then
8015 we'll count that as a retry.
8016
8017 Note that even when forever is set, we will only wait
8018 forever prior to the start of a packet. After that, we
8019 expect characters to arrive at a brisk pace. They should
8020 show up within remote_timeout intervals. */
8021 do
8022 c = readchar (timeout);
8023 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8024
8025 if (c == SERIAL_TIMEOUT)
8026 {
74531fed
PA
8027 if (expecting_notif)
8028 return -1; /* Don't complain, it's normal to not get
8029 anything in this case. */
8030
23860348 8031 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8032 {
2acceee2 8033 QUIT;
78a095c3 8034 remote_unpush_target ();
598d3636
JK
8035 throw_error (TARGET_CLOSE_ERROR,
8036 _("Watchdog timeout has expired. "
8037 "Target detached."));
c906108c 8038 }
c906108c 8039 if (remote_debug)
0f71a2f6 8040 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8041 }
74531fed
PA
8042 else
8043 {
8044 /* We've found the start of a packet or notification.
8045 Now collect the data. */
8046 val = read_frame (buf, sizeof_buf);
8047 if (val >= 0)
8048 break;
8049 }
8050
c33e31fd 8051 remote_serial_write ("-", 1);
c906108c 8052 }
c906108c 8053
74531fed
PA
8054 if (tries > MAX_TRIES)
8055 {
8056 /* We have tried hard enough, and just can't receive the
8057 packet/notification. Give up. */
8058 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8059
74531fed
PA
8060 /* Skip the ack char if we're in no-ack mode. */
8061 if (!rs->noack_mode)
c33e31fd 8062 remote_serial_write ("+", 1);
74531fed
PA
8063 return -1;
8064 }
c906108c 8065
74531fed
PA
8066 /* If we got an ordinary packet, return that to our caller. */
8067 if (c == '$')
c906108c
SS
8068 {
8069 if (remote_debug)
43e526b9 8070 {
6e5abd65
PA
8071 struct cleanup *old_chain;
8072 char *str;
8073
8074 str = escape_buffer (*buf, val);
8075 old_chain = make_cleanup (xfree, str);
8076 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8077 do_cleanups (old_chain);
43e526b9 8078 }
a6f3e723
SL
8079
8080 /* Skip the ack char if we're in no-ack mode. */
8081 if (!rs->noack_mode)
c33e31fd 8082 remote_serial_write ("+", 1);
fee9eda9
YQ
8083 if (is_notif != NULL)
8084 *is_notif = 0;
0876f84a 8085 return val;
c906108c
SS
8086 }
8087
74531fed
PA
8088 /* If we got a notification, handle it, and go back to looking
8089 for a packet. */
8090 else
8091 {
8092 gdb_assert (c == '%');
8093
8094 if (remote_debug)
8095 {
6e5abd65
PA
8096 struct cleanup *old_chain;
8097 char *str;
8098
8099 str = escape_buffer (*buf, val);
8100 old_chain = make_cleanup (xfree, str);
8101 fprintf_unfiltered (gdb_stdlog,
8102 " Notification received: %s\n",
8103 str);
8104 do_cleanups (old_chain);
74531fed 8105 }
fee9eda9
YQ
8106 if (is_notif != NULL)
8107 *is_notif = 1;
c906108c 8108
5965e028 8109 handle_notification (rs->notif_state, *buf);
c906108c 8110
74531fed 8111 /* Notifications require no acknowledgement. */
a6f3e723 8112
74531fed 8113 if (expecting_notif)
fee9eda9 8114 return val;
74531fed
PA
8115 }
8116 }
8117}
8118
8119static int
8120getpkt_sane (char **buf, long *sizeof_buf, int forever)
8121{
fee9eda9 8122 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8123}
8124
8125static int
fee9eda9
YQ
8126getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8127 int *is_notif)
74531fed 8128{
fee9eda9
YQ
8129 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8130 is_notif);
c906108c 8131}
74531fed 8132
cbb8991c
DB
8133/* Check whether EVENT is a fork event for the process specified
8134 by the pid passed in DATA, and if it is, kill the fork child. */
8135
8136static int
8137kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8138 QUEUE_ITER (stop_reply_p) *iter,
8139 stop_reply_p event,
8140 void *data)
8141{
8142 struct queue_iter_param *param = data;
8143 int parent_pid = *(int *) param->input;
8144
8145 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8146 {
8147 struct remote_state *rs = get_remote_state ();
8148 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8149 int res;
8150
8151 res = remote_vkill (child_pid, rs);
8152 if (res != 0)
8153 error (_("Can't kill fork child process %d"), child_pid);
8154 }
8155
8156 return 1;
8157}
8158
8159/* Kill any new fork children of process PID that haven't been
8160 processed by follow_fork. */
8161
8162static void
8163kill_new_fork_children (int pid, struct remote_state *rs)
8164{
8165 struct thread_info *thread;
8166 struct notif_client *notif = &notif_client_stop;
8167 struct queue_iter_param param;
8168
8169 /* Kill the fork child threads of any threads in process PID
8170 that are stopped at a fork event. */
8171 ALL_NON_EXITED_THREADS (thread)
8172 {
8173 struct target_waitstatus *ws = &thread->pending_follow;
8174
8175 if (is_pending_fork_parent (ws, pid, thread->ptid))
8176 {
8177 struct remote_state *rs = get_remote_state ();
8178 int child_pid = ptid_get_pid (ws->value.related_pid);
8179 int res;
8180
8181 res = remote_vkill (child_pid, rs);
8182 if (res != 0)
8183 error (_("Can't kill fork child process %d"), child_pid);
8184 }
8185 }
8186
8187 /* Check for any pending fork events (not reported or processed yet)
8188 in process PID and kill those fork child threads as well. */
8189 remote_notif_get_pending_events (notif);
8190 param.input = &pid;
8191 param.output = NULL;
8192 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8193 kill_child_of_pending_fork, &param);
8194}
8195
c906108c
SS
8196\f
8197static void
7d85a9c0 8198remote_kill (struct target_ops *ops)
43ff13b4 8199{
0fdf84ca
PA
8200
8201 /* Catch errors so the user can quit from gdb even when we
23860348 8202 aren't on speaking terms with the remote system. */
492d29ea 8203 TRY
0fdf84ca
PA
8204 {
8205 putpkt ("k");
8206 }
492d29ea 8207 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8208 {
8209 if (ex.error == TARGET_CLOSE_ERROR)
8210 {
8211 /* If we got an (EOF) error that caused the target
8212 to go away, then we're done, that's what we wanted.
8213 "k" is susceptible to cause a premature EOF, given
8214 that the remote server isn't actually required to
8215 reply to "k", and it can happen that it doesn't
8216 even get to reply ACK to the "k". */
8217 return;
8218 }
8219
8220 /* Otherwise, something went wrong. We didn't actually kill
8221 the target. Just propagate the exception, and let the
8222 user or higher layers decide what to do. */
8223 throw_exception (ex);
8224 }
492d29ea 8225 END_CATCH
43ff13b4 8226
0fdf84ca
PA
8227 /* We've killed the remote end, we get to mourn it. Since this is
8228 target remote, single-process, mourning the inferior also
8229 unpushes remote_ops. */
43ff13b4
JM
8230 target_mourn_inferior ();
8231}
8232
82f73884
PA
8233static int
8234remote_vkill (int pid, struct remote_state *rs)
8235{
4082afcc 8236 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8237 return -1;
8238
8239 /* Tell the remote target to detach. */
bba74b36 8240 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8241 putpkt (rs->buf);
8242 getpkt (&rs->buf, &rs->buf_size, 0);
8243
4082afcc
PA
8244 switch (packet_ok (rs->buf,
8245 &remote_protocol_packets[PACKET_vKill]))
8246 {
8247 case PACKET_OK:
8248 return 0;
8249 case PACKET_ERROR:
8250 return 1;
8251 case PACKET_UNKNOWN:
8252 return -1;
8253 default:
8254 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8255 }
82f73884
PA
8256}
8257
8258static void
7d85a9c0 8259extended_remote_kill (struct target_ops *ops)
82f73884
PA
8260{
8261 int res;
8262 int pid = ptid_get_pid (inferior_ptid);
8263 struct remote_state *rs = get_remote_state ();
8264
cbb8991c
DB
8265 /* If we're stopped while forking and we haven't followed yet, kill the
8266 child task. We need to do this before killing the parent task
8267 because if this is a vfork then the parent will be sleeping. */
8268 kill_new_fork_children (pid, rs);
8269
82f73884 8270 res = remote_vkill (pid, rs);
901f9912 8271 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8272 {
8273 /* Don't try 'k' on a multi-process aware stub -- it has no way
8274 to specify the pid. */
8275
8276 putpkt ("k");
8277#if 0
8278 getpkt (&rs->buf, &rs->buf_size, 0);
8279 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8280 res = 1;
8281#else
8282 /* Don't wait for it to die. I'm not really sure it matters whether
8283 we do or not. For the existing stubs, kill is a noop. */
8284 res = 0;
8285#endif
8286 }
8287
8288 if (res != 0)
8289 error (_("Can't kill process"));
8290
82f73884
PA
8291 target_mourn_inferior ();
8292}
8293
c906108c 8294static void
20f796c9 8295remote_mourn (struct target_ops *target)
c906108c
SS
8296{
8297 unpush_target (target);
ce5ce7ed 8298
8a2492ee
PA
8299 /* remote_close takes care of doing most of the clean up. */
8300 generic_mourn_inferior ();
c906108c
SS
8301}
8302
2d717e4f 8303static void
20f796c9 8304extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8305{
8306 struct remote_state *rs = get_remote_state ();
c906108c 8307
e24a49d8
PA
8308 /* In case we got here due to an error, but we're going to stay
8309 connected. */
8310 rs->waiting_for_stop_reply = 0;
8311
dc1981d7
PA
8312 /* If the current general thread belonged to the process we just
8313 detached from or has exited, the remote side current general
8314 thread becomes undefined. Considering a case like this:
8315
8316 - We just got here due to a detach.
8317 - The process that we're detaching from happens to immediately
8318 report a global breakpoint being hit in non-stop mode, in the
8319 same thread we had selected before.
8320 - GDB attaches to this process again.
8321 - This event happens to be the next event we handle.
8322
8323 GDB would consider that the current general thread didn't need to
8324 be set on the stub side (with Hg), since for all it knew,
8325 GENERAL_THREAD hadn't changed.
8326
8327 Notice that although in all-stop mode, the remote server always
8328 sets the current thread to the thread reporting the stop event,
8329 that doesn't happen in non-stop mode; in non-stop, the stub *must
8330 not* change the current thread when reporting a breakpoint hit,
8331 due to the decoupling of event reporting and event handling.
8332
8333 To keep things simple, we always invalidate our notion of the
8334 current thread. */
47f8a51d 8335 record_currthread (rs, minus_one_ptid);
dc1981d7 8336
2d717e4f
DJ
8337 /* Unlike "target remote", we do not want to unpush the target; then
8338 the next time the user says "run", we won't be connected. */
8339
48aa3c27
PA
8340 /* Call common code to mark the inferior as not running. */
8341 generic_mourn_inferior ();
8342
d729566a 8343 if (!have_inferiors ())
2d717e4f 8344 {
82f73884
PA
8345 if (!remote_multi_process_p (rs))
8346 {
8347 /* Check whether the target is running now - some remote stubs
8348 automatically restart after kill. */
8349 putpkt ("?");
8350 getpkt (&rs->buf, &rs->buf_size, 0);
8351
8352 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8353 {
3e43a32a
MS
8354 /* Assume that the target has been restarted. Set
8355 inferior_ptid so that bits of core GDB realizes
8356 there's something here, e.g., so that the user can
8357 say "kill" again. */
82f73884
PA
8358 inferior_ptid = magic_null_ptid;
8359 }
82f73884 8360 }
2d717e4f
DJ
8361 }
8362}
c906108c 8363
03583c20 8364static int
2bfc0540 8365extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8366{
4082afcc 8367 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8368}
8369
8370static void
8371extended_remote_disable_randomization (int val)
8372{
8373 struct remote_state *rs = get_remote_state ();
8374 char *reply;
8375
bba74b36
YQ
8376 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8377 val);
03583c20
UW
8378 putpkt (rs->buf);
8379 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8380 if (*reply == '\0')
8381 error (_("Target does not support QDisableRandomization."));
8382 if (strcmp (reply, "OK") != 0)
8383 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8384}
8385
2d717e4f
DJ
8386static int
8387extended_remote_run (char *args)
8388{
8389 struct remote_state *rs = get_remote_state ();
2d717e4f 8390 int len;
c906108c 8391
2d717e4f
DJ
8392 /* If the user has disabled vRun support, or we have detected that
8393 support is not available, do not try it. */
4082afcc 8394 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8395 return -1;
424163ea 8396
2d717e4f
DJ
8397 strcpy (rs->buf, "vRun;");
8398 len = strlen (rs->buf);
c906108c 8399
2d717e4f
DJ
8400 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8401 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8402 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8403 strlen (remote_exec_file));
2d717e4f 8404
d1a41061 8405 gdb_assert (args != NULL);
2d717e4f
DJ
8406 if (*args)
8407 {
8408 struct cleanup *back_to;
8409 int i;
8410 char **argv;
8411
d1a41061 8412 argv = gdb_buildargv (args);
6e366df1 8413 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8414 for (i = 0; argv[i] != NULL; i++)
8415 {
8416 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8417 error (_("Argument list too long for run packet"));
8418 rs->buf[len++] = ';';
9f1b45b0
TT
8419 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8420 strlen (argv[i]));
2d717e4f
DJ
8421 }
8422 do_cleanups (back_to);
8423 }
8424
8425 rs->buf[len++] = '\0';
8426
8427 putpkt (rs->buf);
8428 getpkt (&rs->buf, &rs->buf_size, 0);
8429
4082afcc 8430 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8431 {
4082afcc 8432 case PACKET_OK:
3405876a 8433 /* We have a wait response. All is well. */
2d717e4f 8434 return 0;
4082afcc
PA
8435 case PACKET_UNKNOWN:
8436 return -1;
8437 case PACKET_ERROR:
2d717e4f
DJ
8438 if (remote_exec_file[0] == '\0')
8439 error (_("Running the default executable on the remote target failed; "
8440 "try \"set remote exec-file\"?"));
8441 else
8442 error (_("Running \"%s\" on the remote target failed"),
8443 remote_exec_file);
4082afcc
PA
8444 default:
8445 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8446 }
c906108c
SS
8447}
8448
2d717e4f
DJ
8449/* In the extended protocol we want to be able to do things like
8450 "run" and have them basically work as expected. So we need
8451 a special create_inferior function. We support changing the
8452 executable file and the command line arguments, but not the
8453 environment. */
8454
43ff13b4 8455static void
77a19445
TT
8456extended_remote_create_inferior (struct target_ops *ops,
8457 char *exec_file, char *args,
8458 char **env, int from_tty)
43ff13b4 8459{
3405876a
PA
8460 int run_worked;
8461 char *stop_reply;
8462 struct remote_state *rs = get_remote_state ();
8463
43ff13b4 8464 /* If running asynchronously, register the target file descriptor
23860348 8465 with the event loop. */
75c99385 8466 if (target_can_async_p ())
6a3753b3 8467 target_async (1);
43ff13b4 8468
03583c20 8469 /* Disable address space randomization if requested (and supported). */
2bfc0540 8470 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8471 extended_remote_disable_randomization (disable_randomization);
8472
43ff13b4 8473 /* Now restart the remote server. */
3405876a
PA
8474 run_worked = extended_remote_run (args) != -1;
8475 if (!run_worked)
2d717e4f
DJ
8476 {
8477 /* vRun was not supported. Fail if we need it to do what the
8478 user requested. */
8479 if (remote_exec_file[0])
8480 error (_("Remote target does not support \"set remote exec-file\""));
8481 if (args[0])
8482 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8483
2d717e4f
DJ
8484 /* Fall back to "R". */
8485 extended_remote_restart ();
8486 }
424163ea 8487
6c95b8df
PA
8488 if (!have_inferiors ())
8489 {
8490 /* Clean up from the last time we ran, before we mark the target
8491 running again. This will mark breakpoints uninserted, and
8492 get_offsets may insert breakpoints. */
8493 init_thread_list ();
8494 init_wait_for_inferior ();
8495 }
45280a52 8496
3405876a
PA
8497 /* vRun's success return is a stop reply. */
8498 stop_reply = run_worked ? rs->buf : NULL;
8499 add_current_inferior_and_thread (stop_reply);
c0a2216e 8500
2d717e4f
DJ
8501 /* Get updated offsets, if the stub uses qOffsets. */
8502 get_offsets ();
2d717e4f 8503}
c906108c 8504\f
c5aa993b 8505
b775012e
LM
8506/* Given a location's target info BP_TGT and the packet buffer BUF, output
8507 the list of conditions (in agent expression bytecode format), if any, the
8508 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8509 started from BUF and ended at BUF_END. */
b775012e
LM
8510
8511static int
8512remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8513 struct bp_target_info *bp_tgt, char *buf,
8514 char *buf_end)
b775012e
LM
8515{
8516 struct agent_expr *aexpr = NULL;
8517 int i, ix;
8518 char *pkt;
8519 char *buf_start = buf;
8520
8521 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8522 return 0;
8523
8524 buf += strlen (buf);
bba74b36 8525 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8526 buf++;
8527
8528 /* Send conditions to the target and free the vector. */
8529 for (ix = 0;
8530 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8531 ix++)
8532 {
bba74b36 8533 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8534 buf += strlen (buf);
8535 for (i = 0; i < aexpr->len; ++i)
8536 buf = pack_hex_byte (buf, aexpr->buf[i]);
8537 *buf = '\0';
8538 }
b775012e
LM
8539 return 0;
8540}
8541
d3ce09f5
SS
8542static void
8543remote_add_target_side_commands (struct gdbarch *gdbarch,
8544 struct bp_target_info *bp_tgt, char *buf)
8545{
8546 struct agent_expr *aexpr = NULL;
8547 int i, ix;
8548
8549 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8550 return;
8551
8552 buf += strlen (buf);
8553
8554 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8555 buf += strlen (buf);
8556
8557 /* Concatenate all the agent expressions that are commands into the
8558 cmds parameter. */
8559 for (ix = 0;
8560 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8561 ix++)
8562 {
8563 sprintf (buf, "X%x,", aexpr->len);
8564 buf += strlen (buf);
8565 for (i = 0; i < aexpr->len; ++i)
8566 buf = pack_hex_byte (buf, aexpr->buf[i]);
8567 *buf = '\0';
8568 }
d3ce09f5
SS
8569}
8570
8181d85f
DJ
8571/* Insert a breakpoint. On targets that have software breakpoint
8572 support, we ask the remote target to do the work; on targets
8573 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8574
8575static int
3db08215
MM
8576remote_insert_breakpoint (struct target_ops *ops,
8577 struct gdbarch *gdbarch,
a6d9a66e 8578 struct bp_target_info *bp_tgt)
c906108c 8579{
d471ea57
AC
8580 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8581 If it succeeds, then set the support to PACKET_ENABLE. If it
8582 fails, and the user has explicitly requested the Z support then
23860348 8583 report an error, otherwise, mark it disabled and go on. */
802188a7 8584
4082afcc 8585 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8586 {
0d5ed153 8587 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8588 struct remote_state *rs;
bba74b36 8589 char *p, *endbuf;
7c0f6dcc 8590 int bpsize;
b775012e 8591 struct condition_list *cond = NULL;
4fff2411 8592
28439a30
PA
8593 /* Make sure the remote is pointing at the right process, if
8594 necessary. */
8595 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8596 set_general_process ();
8597
a1dcb23a 8598 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8599
8600 rs = get_remote_state ();
8601 p = rs->buf;
bba74b36 8602 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8603
96baa820
JM
8604 *(p++) = 'Z';
8605 *(p++) = '0';
8606 *(p++) = ',';
7c0f6dcc 8607 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8608 p += hexnumstr (p, addr);
bba74b36 8609 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8610
efcc2da7 8611 if (remote_supports_cond_breakpoints (ops))
bba74b36 8612 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8613
78eff0ec 8614 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8615 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8616
6d820c5c
DJ
8617 putpkt (rs->buf);
8618 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8619
6d820c5c 8620 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8621 {
d471ea57
AC
8622 case PACKET_ERROR:
8623 return -1;
8624 case PACKET_OK:
7c0f6dcc
JL
8625 bp_tgt->placed_address = addr;
8626 bp_tgt->placed_size = bpsize;
d471ea57
AC
8627 return 0;
8628 case PACKET_UNKNOWN:
8629 break;
96baa820
JM
8630 }
8631 }
c906108c 8632
0000e5cc
PA
8633 /* If this breakpoint has target-side commands but this stub doesn't
8634 support Z0 packets, throw error. */
8635 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8636 throw_error (NOT_SUPPORTED_ERROR, _("\
8637Target doesn't support breakpoints that have target side commands."));
8638
3db08215 8639 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8640}
8641
8642static int
3db08215
MM
8643remote_remove_breakpoint (struct target_ops *ops,
8644 struct gdbarch *gdbarch,
a6d9a66e 8645 struct bp_target_info *bp_tgt)
c906108c 8646{
8181d85f 8647 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8648 struct remote_state *rs = get_remote_state ();
96baa820 8649
4082afcc 8650 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8651 {
6d820c5c 8652 char *p = rs->buf;
bba74b36 8653 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8654
28439a30
PA
8655 /* Make sure the remote is pointing at the right process, if
8656 necessary. */
8657 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8658 set_general_process ();
8659
96baa820
JM
8660 *(p++) = 'z';
8661 *(p++) = '0';
8662 *(p++) = ',';
8663
8181d85f
DJ
8664 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8665 p += hexnumstr (p, addr);
bba74b36 8666 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8667
6d820c5c
DJ
8668 putpkt (rs->buf);
8669 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8670
6d820c5c 8671 return (rs->buf[0] == 'E');
96baa820
JM
8672 }
8673
3db08215 8674 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8675}
8676
d471ea57
AC
8677static int
8678watchpoint_to_Z_packet (int type)
8679{
8680 switch (type)
8681 {
8682 case hw_write:
bb858e6a 8683 return Z_PACKET_WRITE_WP;
d471ea57
AC
8684 break;
8685 case hw_read:
bb858e6a 8686 return Z_PACKET_READ_WP;
d471ea57
AC
8687 break;
8688 case hw_access:
bb858e6a 8689 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8690 break;
8691 default:
8e65ff28 8692 internal_error (__FILE__, __LINE__,
e2e0b3e5 8693 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8694 }
8695}
8696
3c3bea1c 8697static int
7bb99c53
TT
8698remote_insert_watchpoint (struct target_ops *self,
8699 CORE_ADDR addr, int len, int type,
0cf6dd15 8700 struct expression *cond)
96baa820 8701{
d01949b6 8702 struct remote_state *rs = get_remote_state ();
bba74b36 8703 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8704 char *p;
d471ea57 8705 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8706
4082afcc 8707 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8708 return 1;
802188a7 8709
28439a30
PA
8710 /* Make sure the remote is pointing at the right process, if
8711 necessary. */
8712 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8713 set_general_process ();
8714
bba74b36 8715 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8716 p = strchr (rs->buf, '\0');
96baa820
JM
8717 addr = remote_address_masked (addr);
8718 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8719 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8720
6d820c5c
DJ
8721 putpkt (rs->buf);
8722 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8723
6d820c5c 8724 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8725 {
8726 case PACKET_ERROR:
d471ea57 8727 return -1;
85d721b8
PA
8728 case PACKET_UNKNOWN:
8729 return 1;
d471ea57
AC
8730 case PACKET_OK:
8731 return 0;
8732 }
8e65ff28 8733 internal_error (__FILE__, __LINE__,
e2e0b3e5 8734 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8735}
8736
283002cf
MR
8737static int
8738remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8739 CORE_ADDR start, int length)
8740{
8741 CORE_ADDR diff = remote_address_masked (addr - start);
8742
8743 return diff < length;
8744}
8745
d471ea57 8746
3c3bea1c 8747static int
11b5219a
TT
8748remote_remove_watchpoint (struct target_ops *self,
8749 CORE_ADDR addr, int len, int type,
0cf6dd15 8750 struct expression *cond)
96baa820 8751{
d01949b6 8752 struct remote_state *rs = get_remote_state ();
bba74b36 8753 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8754 char *p;
d471ea57
AC
8755 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8756
4082afcc 8757 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8758 return -1;
802188a7 8759
28439a30
PA
8760 /* Make sure the remote is pointing at the right process, if
8761 necessary. */
8762 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8763 set_general_process ();
8764
bba74b36 8765 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8766 p = strchr (rs->buf, '\0');
96baa820
JM
8767 addr = remote_address_masked (addr);
8768 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8769 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8770 putpkt (rs->buf);
8771 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8772
6d820c5c 8773 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8774 {
8775 case PACKET_ERROR:
8776 case PACKET_UNKNOWN:
8777 return -1;
8778 case PACKET_OK:
8779 return 0;
8780 }
8e65ff28 8781 internal_error (__FILE__, __LINE__,
e2e0b3e5 8782 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8783}
8784
3c3bea1c 8785
501eef12 8786int remote_hw_watchpoint_limit = -1;
480a3f21 8787int remote_hw_watchpoint_length_limit = -1;
501eef12 8788int remote_hw_breakpoint_limit = -1;
d471ea57 8789
480a3f21 8790static int
31568a15
TT
8791remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8792 CORE_ADDR addr, int len)
480a3f21
PW
8793{
8794 if (remote_hw_watchpoint_length_limit == 0)
8795 return 0;
8796 else if (remote_hw_watchpoint_length_limit < 0)
8797 return 1;
8798 else if (len <= remote_hw_watchpoint_length_limit)
8799 return 1;
8800 else
8801 return 0;
8802}
8803
b9362cc7 8804static int
5461485a
TT
8805remote_check_watch_resources (struct target_ops *self,
8806 int type, int cnt, int ot)
96baa820 8807{
3c3bea1c
GS
8808 if (type == bp_hardware_breakpoint)
8809 {
8810 if (remote_hw_breakpoint_limit == 0)
8811 return 0;
501eef12
AC
8812 else if (remote_hw_breakpoint_limit < 0)
8813 return 1;
3c3bea1c
GS
8814 else if (cnt <= remote_hw_breakpoint_limit)
8815 return 1;
8816 }
8817 else
8818 {
8819 if (remote_hw_watchpoint_limit == 0)
8820 return 0;
501eef12
AC
8821 else if (remote_hw_watchpoint_limit < 0)
8822 return 1;
3c3bea1c
GS
8823 else if (ot)
8824 return -1;
8825 else if (cnt <= remote_hw_watchpoint_limit)
8826 return 1;
8827 }
8828 return -1;
8829}
8830
f7e6eed5
PA
8831/* The to_stopped_by_sw_breakpoint method of target remote. */
8832
8833static int
8834remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8835{
8836 struct remote_state *rs = get_remote_state ();
8837
8838 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8839}
8840
8841/* The to_supports_stopped_by_sw_breakpoint method of target
8842 remote. */
8843
8844static int
8845remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8846{
8847 struct remote_state *rs = get_remote_state ();
8848
8849 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8850}
8851
8852/* The to_stopped_by_hw_breakpoint method of target remote. */
8853
8854static int
8855remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8856{
8857 struct remote_state *rs = get_remote_state ();
8858
8859 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8860}
8861
8862/* The to_supports_stopped_by_hw_breakpoint method of target
8863 remote. */
8864
8865static int
8866remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8867{
8868 struct remote_state *rs = get_remote_state ();
8869
8870 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8871}
8872
b9362cc7 8873static int
6a109b6b 8874remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8875{
ee154bee
TT
8876 struct remote_state *rs = get_remote_state ();
8877
f7e6eed5 8878 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8879}
8880
4aa7a7f5
JJ
8881static int
8882remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8883{
ee154bee 8884 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8885 int rc = 0;
a744cf53 8886
6a109b6b 8887 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8888 {
ee154bee 8889 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8890 rc = 1;
8891 }
8892
8893 return rc;
3c3bea1c
GS
8894}
8895
8896
8897static int
23a26771 8898remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8899 struct bp_target_info *bp_tgt)
3c3bea1c 8900{
0d5ed153 8901 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8902 struct remote_state *rs;
bba74b36 8903 char *p, *endbuf;
dd61ec5c 8904 char *message;
0d5ed153 8905 int bpsize;
802188a7 8906
c8189ed1 8907 /* The length field should be set to the size of a breakpoint
8181d85f 8908 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8909
0d5ed153 8910 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8911
4082afcc 8912 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8913 return -1;
2bc416ba 8914
28439a30
PA
8915 /* Make sure the remote is pointing at the right process, if
8916 necessary. */
8917 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8918 set_general_process ();
8919
4fff2411
JZ
8920 rs = get_remote_state ();
8921 p = rs->buf;
bba74b36 8922 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8923
96baa820
JM
8924 *(p++) = 'Z';
8925 *(p++) = '1';
8926 *(p++) = ',';
802188a7 8927
0d5ed153 8928 addr = remote_address_masked (addr);
96baa820 8929 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8930 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8931
efcc2da7 8932 if (remote_supports_cond_breakpoints (self))
bba74b36 8933 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8934
78eff0ec 8935 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8936 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8937
6d820c5c
DJ
8938 putpkt (rs->buf);
8939 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8940
6d820c5c 8941 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8942 {
8943 case PACKET_ERROR:
dd61ec5c
MW
8944 if (rs->buf[1] == '.')
8945 {
8946 message = strchr (rs->buf + 2, '.');
8947 if (message)
0316657e 8948 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
8949 }
8950 return -1;
d471ea57
AC
8951 case PACKET_UNKNOWN:
8952 return -1;
8953 case PACKET_OK:
0d5ed153
MR
8954 bp_tgt->placed_address = addr;
8955 bp_tgt->placed_size = bpsize;
d471ea57
AC
8956 return 0;
8957 }
8e65ff28 8958 internal_error (__FILE__, __LINE__,
e2e0b3e5 8959 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8960}
8961
d471ea57 8962
802188a7 8963static int
a64dc96c 8964remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8965 struct bp_target_info *bp_tgt)
96baa820 8966{
8181d85f 8967 CORE_ADDR addr;
d01949b6 8968 struct remote_state *rs = get_remote_state ();
6d820c5c 8969 char *p = rs->buf;
bba74b36 8970 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 8971
4082afcc 8972 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8973 return -1;
802188a7 8974
28439a30
PA
8975 /* Make sure the remote is pointing at the right process, if
8976 necessary. */
8977 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8978 set_general_process ();
8979
96baa820
JM
8980 *(p++) = 'z';
8981 *(p++) = '1';
8982 *(p++) = ',';
802188a7 8983
8181d85f 8984 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8985 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8986 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8987
6d820c5c
DJ
8988 putpkt (rs->buf);
8989 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8990
6d820c5c 8991 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8992 {
8993 case PACKET_ERROR:
8994 case PACKET_UNKNOWN:
8995 return -1;
8996 case PACKET_OK:
8997 return 0;
8998 }
8e65ff28 8999 internal_error (__FILE__, __LINE__,
e2e0b3e5 9000 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9001}
96baa820 9002
4a5e7a5b
PA
9003/* Verify memory using the "qCRC:" request. */
9004
9005static int
9006remote_verify_memory (struct target_ops *ops,
9007 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9008{
9009 struct remote_state *rs = get_remote_state ();
9010 unsigned long host_crc, target_crc;
9011 char *tmp;
9012
936d2992
PA
9013 /* It doesn't make sense to use qCRC if the remote target is
9014 connected but not running. */
9015 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9016 {
9017 enum packet_result result;
28439a30 9018
936d2992
PA
9019 /* Make sure the remote is pointing at the right process. */
9020 set_general_process ();
4a5e7a5b 9021
936d2992
PA
9022 /* FIXME: assumes lma can fit into long. */
9023 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9024 (long) lma, (long) size);
9025 putpkt (rs->buf);
4a5e7a5b 9026
936d2992
PA
9027 /* Be clever; compute the host_crc before waiting for target
9028 reply. */
9029 host_crc = xcrc32 (data, size, 0xffffffff);
9030
9031 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9032
936d2992
PA
9033 result = packet_ok (rs->buf,
9034 &remote_protocol_packets[PACKET_qCRC]);
9035 if (result == PACKET_ERROR)
9036 return -1;
9037 else if (result == PACKET_OK)
9038 {
9039 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9040 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9041
936d2992
PA
9042 return (host_crc == target_crc);
9043 }
9044 }
4a5e7a5b 9045
936d2992 9046 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9047}
9048
c906108c
SS
9049/* compare-sections command
9050
9051 With no arguments, compares each loadable section in the exec bfd
9052 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9053 Useful for verifying the image on the target against the exec file. */
e514a9d6 9054
c906108c 9055static void
fba45db2 9056compare_sections_command (char *args, int from_tty)
c906108c
SS
9057{
9058 asection *s;
c906108c 9059 struct cleanup *old_chain;
948f8e3d 9060 gdb_byte *sectdata;
ce359b09 9061 const char *sectname;
c906108c
SS
9062 bfd_size_type size;
9063 bfd_vma lma;
9064 int matched = 0;
9065 int mismatched = 0;
4a5e7a5b 9066 int res;
95cf3b38 9067 int read_only = 0;
c906108c
SS
9068
9069 if (!exec_bfd)
8a3fe4f8 9070 error (_("command cannot be used without an exec file"));
c906108c 9071
28439a30
PA
9072 /* Make sure the remote is pointing at the right process. */
9073 set_general_process ();
9074
95cf3b38
DT
9075 if (args != NULL && strcmp (args, "-r") == 0)
9076 {
9077 read_only = 1;
9078 args = NULL;
9079 }
9080
c5aa993b 9081 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9082 {
9083 if (!(s->flags & SEC_LOAD))
0df8b418 9084 continue; /* Skip non-loadable section. */
c906108c 9085
95cf3b38
DT
9086 if (read_only && (s->flags & SEC_READONLY) == 0)
9087 continue; /* Skip writeable sections */
9088
2c500098 9089 size = bfd_get_section_size (s);
c906108c 9090 if (size == 0)
0df8b418 9091 continue; /* Skip zero-length section. */
c906108c 9092
ce359b09 9093 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9094 if (args && strcmp (args, sectname) != 0)
0df8b418 9095 continue; /* Not the section selected by user. */
c906108c 9096
0df8b418 9097 matched = 1; /* Do this section. */
c906108c 9098 lma = s->lma;
c906108c 9099
c906108c 9100 sectdata = xmalloc (size);
b8c9b27d 9101 old_chain = make_cleanup (xfree, sectdata);
c906108c 9102 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9103
4a5e7a5b
PA
9104 res = target_verify_memory (sectdata, lma, size);
9105
9106 if (res == -1)
5af949e3 9107 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9108 paddress (target_gdbarch (), lma),
9109 paddress (target_gdbarch (), lma + size));
c906108c 9110
5af949e3 9111 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9112 paddress (target_gdbarch (), lma),
9113 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9114 if (res)
c906108c
SS
9115 printf_filtered ("matched.\n");
9116 else
c5aa993b
JM
9117 {
9118 printf_filtered ("MIS-MATCHED!\n");
9119 mismatched++;
9120 }
c906108c
SS
9121
9122 do_cleanups (old_chain);
9123 }
9124 if (mismatched > 0)
936d2992 9125 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9126the loaded file\n"));
c906108c 9127 if (args && !matched)
a3f17187 9128 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9129}
9130
0e7f50da
UW
9131/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9132 into remote target. The number of bytes written to the remote
9133 target is returned, or -1 for error. */
9134
9b409511 9135static enum target_xfer_status
0e7f50da
UW
9136remote_write_qxfer (struct target_ops *ops, const char *object_name,
9137 const char *annex, const gdb_byte *writebuf,
9b409511 9138 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9139 struct packet_config *packet)
9140{
9141 int i, buf_len;
9142 ULONGEST n;
0e7f50da
UW
9143 struct remote_state *rs = get_remote_state ();
9144 int max_size = get_memory_write_packet_size ();
9145
9146 if (packet->support == PACKET_DISABLE)
2ed4b548 9147 return TARGET_XFER_E_IO;
0e7f50da
UW
9148
9149 /* Insert header. */
9150 i = snprintf (rs->buf, max_size,
9151 "qXfer:%s:write:%s:%s:",
9152 object_name, annex ? annex : "",
9153 phex_nz (offset, sizeof offset));
9154 max_size -= (i + 1);
9155
9156 /* Escape as much data as fits into rs->buf. */
9157 buf_len = remote_escape_output
124e13d9 9158 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9159
9160 if (putpkt_binary (rs->buf, i + buf_len) < 0
9161 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9162 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9163 return TARGET_XFER_E_IO;
0e7f50da
UW
9164
9165 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9166
9167 *xfered_len = n;
9168 return TARGET_XFER_OK;
0e7f50da
UW
9169}
9170
0876f84a
DJ
9171/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9172 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9173 number of bytes read is returned, or 0 for EOF, or -1 for error.
9174 The number of bytes read may be less than LEN without indicating an
9175 EOF. PACKET is checked and updated to indicate whether the remote
9176 target supports this object. */
9177
9b409511 9178static enum target_xfer_status
0876f84a
DJ
9179remote_read_qxfer (struct target_ops *ops, const char *object_name,
9180 const char *annex,
9181 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9182 ULONGEST *xfered_len,
0876f84a
DJ
9183 struct packet_config *packet)
9184{
0876f84a 9185 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9186 LONGEST i, n, packet_len;
9187
9188 if (packet->support == PACKET_DISABLE)
2ed4b548 9189 return TARGET_XFER_E_IO;
0876f84a
DJ
9190
9191 /* Check whether we've cached an end-of-object packet that matches
9192 this request. */
8e88304f 9193 if (rs->finished_object)
0876f84a 9194 {
8e88304f
TT
9195 if (strcmp (object_name, rs->finished_object) == 0
9196 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9197 && offset == rs->finished_offset)
9b409511
YQ
9198 return TARGET_XFER_EOF;
9199
0876f84a
DJ
9200
9201 /* Otherwise, we're now reading something different. Discard
9202 the cache. */
8e88304f
TT
9203 xfree (rs->finished_object);
9204 xfree (rs->finished_annex);
9205 rs->finished_object = NULL;
9206 rs->finished_annex = NULL;
0876f84a
DJ
9207 }
9208
9209 /* Request only enough to fit in a single packet. The actual data
9210 may not, since we don't know how much of it will need to be escaped;
9211 the target is free to respond with slightly less data. We subtract
9212 five to account for the response type and the protocol frame. */
9213 n = min (get_remote_packet_size () - 5, len);
9214 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9215 object_name, annex ? annex : "",
9216 phex_nz (offset, sizeof offset),
9217 phex_nz (n, sizeof n));
9218 i = putpkt (rs->buf);
9219 if (i < 0)
2ed4b548 9220 return TARGET_XFER_E_IO;
0876f84a
DJ
9221
9222 rs->buf[0] = '\0';
9223 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9224 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9225 return TARGET_XFER_E_IO;
0876f84a
DJ
9226
9227 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9228 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9229
9230 /* 'm' means there is (or at least might be) more data after this
9231 batch. That does not make sense unless there's at least one byte
9232 of data in this reply. */
9233 if (rs->buf[0] == 'm' && packet_len == 1)
9234 error (_("Remote qXfer reply contained no data."));
9235
9236 /* Got some data. */
bc20a4af
PA
9237 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9238 packet_len - 1, readbuf, n);
0876f84a
DJ
9239
9240 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9241 or possibly empty. If we have the final block of a non-empty
9242 object, record this fact to bypass a subsequent partial read. */
9243 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9244 {
8e88304f
TT
9245 rs->finished_object = xstrdup (object_name);
9246 rs->finished_annex = xstrdup (annex ? annex : "");
9247 rs->finished_offset = offset + i;
0876f84a
DJ
9248 }
9249
9b409511
YQ
9250 if (i == 0)
9251 return TARGET_XFER_EOF;
9252 else
9253 {
9254 *xfered_len = i;
9255 return TARGET_XFER_OK;
9256 }
0876f84a
DJ
9257}
9258
9b409511 9259static enum target_xfer_status
4b8a223f 9260remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9261 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9262 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9263 ULONGEST *xfered_len)
c906108c 9264{
82f73884 9265 struct remote_state *rs;
c906108c 9266 int i;
6d820c5c 9267 char *p2;
1e3ff5ad 9268 char query_type;
124e13d9 9269 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9270
e6e4e701 9271 set_remote_traceframe ();
82f73884
PA
9272 set_general_thread (inferior_ptid);
9273
9274 rs = get_remote_state ();
9275
b2182ed2 9276 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9277 if (object == TARGET_OBJECT_MEMORY)
9278 {
2d717e4f
DJ
9279 /* If the remote target is connected but not running, we should
9280 pass this request down to a lower stratum (e.g. the executable
9281 file). */
9282 if (!target_has_execution)
9b409511 9283 return TARGET_XFER_EOF;
2d717e4f 9284
21e3b9b9 9285 if (writebuf != NULL)
124e13d9
SM
9286 return remote_write_bytes (offset, writebuf, len, unit_size,
9287 xfered_len);
21e3b9b9 9288 else
124e13d9
SM
9289 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9290 xfered_len);
21e3b9b9
DJ
9291 }
9292
0df8b418 9293 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9294 if (object == TARGET_OBJECT_SPU)
9295 {
9296 if (readbuf)
9297 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9298 xfered_len, &remote_protocol_packets
9299 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9300 else
9301 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9302 xfered_len, &remote_protocol_packets
9303 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9304 }
9305
4aa995e1
PA
9306 /* Handle extra signal info using qxfer packets. */
9307 if (object == TARGET_OBJECT_SIGNAL_INFO)
9308 {
9309 if (readbuf)
9310 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9311 xfered_len, &remote_protocol_packets
4aa995e1
PA
9312 [PACKET_qXfer_siginfo_read]);
9313 else
3e43a32a 9314 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9315 writebuf, offset, len, xfered_len,
4aa995e1
PA
9316 &remote_protocol_packets
9317 [PACKET_qXfer_siginfo_write]);
9318 }
9319
0fb4aa4b
PA
9320 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9321 {
9322 if (readbuf)
3e43a32a 9323 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9324 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9325 &remote_protocol_packets
9326 [PACKET_qXfer_statictrace_read]);
9327 else
2ed4b548 9328 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9329 }
9330
a76d924d
DJ
9331 /* Only handle flash writes. */
9332 if (writebuf != NULL)
9333 {
9334 LONGEST xfered;
9335
9336 switch (object)
9337 {
9338 case TARGET_OBJECT_FLASH:
9b409511
YQ
9339 return remote_flash_write (ops, offset, len, xfered_len,
9340 writebuf);
a76d924d
DJ
9341
9342 default:
2ed4b548 9343 return TARGET_XFER_E_IO;
a76d924d
DJ
9344 }
9345 }
4b8a223f 9346
1e3ff5ad
AC
9347 /* Map pre-existing objects onto letters. DO NOT do this for new
9348 objects!!! Instead specify new query packets. */
9349 switch (object)
c906108c 9350 {
1e3ff5ad
AC
9351 case TARGET_OBJECT_AVR:
9352 query_type = 'R';
9353 break;
802188a7
RM
9354
9355 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9356 gdb_assert (annex == NULL);
9357 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9358 xfered_len,
0876f84a 9359 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9360
23181151
DJ
9361 case TARGET_OBJECT_AVAILABLE_FEATURES:
9362 return remote_read_qxfer
9b409511 9363 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9364 &remote_protocol_packets[PACKET_qXfer_features]);
9365
cfa9d6d9
DJ
9366 case TARGET_OBJECT_LIBRARIES:
9367 return remote_read_qxfer
9b409511 9368 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9369 &remote_protocol_packets[PACKET_qXfer_libraries]);
9370
2268b414
JK
9371 case TARGET_OBJECT_LIBRARIES_SVR4:
9372 return remote_read_qxfer
9b409511 9373 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9374 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9375
fd79ecee
DJ
9376 case TARGET_OBJECT_MEMORY_MAP:
9377 gdb_assert (annex == NULL);
9378 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9379 xfered_len,
fd79ecee
DJ
9380 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9381
07e059b5
VP
9382 case TARGET_OBJECT_OSDATA:
9383 /* Should only get here if we're connected. */
5d93a237 9384 gdb_assert (rs->remote_desc);
07e059b5 9385 return remote_read_qxfer
9b409511 9386 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9387 &remote_protocol_packets[PACKET_qXfer_osdata]);
9388
dc146f7c
VP
9389 case TARGET_OBJECT_THREADS:
9390 gdb_assert (annex == NULL);
9391 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9392 xfered_len,
dc146f7c
VP
9393 &remote_protocol_packets[PACKET_qXfer_threads]);
9394
b3b9301e
PA
9395 case TARGET_OBJECT_TRACEFRAME_INFO:
9396 gdb_assert (annex == NULL);
9397 return remote_read_qxfer
9b409511 9398 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9399 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9400
9401 case TARGET_OBJECT_FDPIC:
9402 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9403 xfered_len,
78d85199 9404 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9405
9406 case TARGET_OBJECT_OPENVMS_UIB:
9407 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9408 xfered_len,
169081d0
TG
9409 &remote_protocol_packets[PACKET_qXfer_uib]);
9410
9accd112
MM
9411 case TARGET_OBJECT_BTRACE:
9412 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9413 xfered_len,
9accd112
MM
9414 &remote_protocol_packets[PACKET_qXfer_btrace]);
9415
f4abbc16
MM
9416 case TARGET_OBJECT_BTRACE_CONF:
9417 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9418 len, xfered_len,
9419 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9420
c78fa86a
GB
9421 case TARGET_OBJECT_EXEC_FILE:
9422 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9423 len, xfered_len,
9424 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9425
1e3ff5ad 9426 default:
2ed4b548 9427 return TARGET_XFER_E_IO;
c906108c
SS
9428 }
9429
0df8b418 9430 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9431 large enough let the caller deal with it. */
ea9c271d 9432 if (len < get_remote_packet_size ())
2ed4b548 9433 return TARGET_XFER_E_IO;
ea9c271d 9434 len = get_remote_packet_size ();
1e3ff5ad 9435
23860348 9436 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9437 if (!rs->remote_desc)
8a3fe4f8 9438 error (_("remote query is only available after target open"));
c906108c 9439
1e3ff5ad 9440 gdb_assert (annex != NULL);
4b8a223f 9441 gdb_assert (readbuf != NULL);
c906108c 9442
6d820c5c 9443 p2 = rs->buf;
c906108c
SS
9444 *p2++ = 'q';
9445 *p2++ = query_type;
9446
23860348
MS
9447 /* We used one buffer char for the remote protocol q command and
9448 another for the query type. As the remote protocol encapsulation
9449 uses 4 chars plus one extra in case we are debugging
9450 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9451 string. */
c906108c 9452 i = 0;
ea9c271d 9453 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9454 {
1e3ff5ad
AC
9455 /* Bad caller may have sent forbidden characters. */
9456 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9457 *p2++ = annex[i];
c906108c
SS
9458 i++;
9459 }
1e3ff5ad
AC
9460 *p2 = '\0';
9461 gdb_assert (annex[i] == '\0');
c906108c 9462
6d820c5c 9463 i = putpkt (rs->buf);
c5aa993b 9464 if (i < 0)
2ed4b548 9465 return TARGET_XFER_E_IO;
c906108c 9466
6d820c5c
DJ
9467 getpkt (&rs->buf, &rs->buf_size, 0);
9468 strcpy ((char *) readbuf, rs->buf);
c906108c 9469
9b409511
YQ
9470 *xfered_len = strlen ((char *) readbuf);
9471 return TARGET_XFER_OK;
c906108c
SS
9472}
9473
08388c79
DE
9474static int
9475remote_search_memory (struct target_ops* ops,
9476 CORE_ADDR start_addr, ULONGEST search_space_len,
9477 const gdb_byte *pattern, ULONGEST pattern_len,
9478 CORE_ADDR *found_addrp)
9479{
f5656ead 9480 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9481 struct remote_state *rs = get_remote_state ();
9482 int max_size = get_memory_write_packet_size ();
9483 struct packet_config *packet =
9484 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9485 /* Number of packet bytes used to encode the pattern;
9486 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9487 int escaped_pattern_len;
0df8b418 9488 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9489 int used_pattern_len;
9490 int i;
9491 int found;
9492 ULONGEST found_addr;
9493
9494 /* Don't go to the target if we don't have to.
9495 This is done before checking packet->support to avoid the possibility that
9496 a success for this edge case means the facility works in general. */
9497 if (pattern_len > search_space_len)
9498 return 0;
9499 if (pattern_len == 0)
9500 {
9501 *found_addrp = start_addr;
9502 return 1;
9503 }
9504
9505 /* If we already know the packet isn't supported, fall back to the simple
9506 way of searching memory. */
9507
4082afcc 9508 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9509 {
9510 /* Target doesn't provided special support, fall back and use the
9511 standard support (copy memory and do the search here). */
9512 return simple_search_memory (ops, start_addr, search_space_len,
9513 pattern, pattern_len, found_addrp);
9514 }
9515
28439a30
PA
9516 /* Make sure the remote is pointing at the right process. */
9517 set_general_process ();
9518
08388c79
DE
9519 /* Insert header. */
9520 i = snprintf (rs->buf, max_size,
9521 "qSearch:memory:%s;%s;",
5af949e3 9522 phex_nz (start_addr, addr_size),
08388c79
DE
9523 phex_nz (search_space_len, sizeof (search_space_len)));
9524 max_size -= (i + 1);
9525
9526 /* Escape as much data as fits into rs->buf. */
9527 escaped_pattern_len =
124e13d9 9528 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
9529 &used_pattern_len, max_size);
9530
9531 /* Bail if the pattern is too large. */
9532 if (used_pattern_len != pattern_len)
9b20d036 9533 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9534
9535 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9536 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9537 || packet_ok (rs->buf, packet) != PACKET_OK)
9538 {
9539 /* The request may not have worked because the command is not
9540 supported. If so, fall back to the simple way. */
9541 if (packet->support == PACKET_DISABLE)
9542 {
9543 return simple_search_memory (ops, start_addr, search_space_len,
9544 pattern, pattern_len, found_addrp);
9545 }
9546 return -1;
9547 }
9548
9549 if (rs->buf[0] == '0')
9550 found = 0;
9551 else if (rs->buf[0] == '1')
9552 {
9553 found = 1;
9554 if (rs->buf[1] != ',')
10e0fa18 9555 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9556 unpack_varlen_hex (rs->buf + 2, &found_addr);
9557 *found_addrp = found_addr;
9558 }
9559 else
10e0fa18 9560 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9561
9562 return found;
9563}
9564
96baa820 9565static void
a30bf1f1 9566remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9567 struct ui_file *outbuf)
96baa820 9568{
d01949b6 9569 struct remote_state *rs = get_remote_state ();
2e9f7625 9570 char *p = rs->buf;
96baa820 9571
5d93a237 9572 if (!rs->remote_desc)
8a3fe4f8 9573 error (_("remote rcmd is only available after target open"));
96baa820 9574
23860348 9575 /* Send a NULL command across as an empty command. */
7be570e7
JM
9576 if (command == NULL)
9577 command = "";
9578
23860348 9579 /* The query prefix. */
2e9f7625
DJ
9580 strcpy (rs->buf, "qRcmd,");
9581 p = strchr (rs->buf, '\0');
96baa820 9582
3e43a32a
MS
9583 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9584 > get_remote_packet_size ())
8a3fe4f8 9585 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9586
23860348 9587 /* Encode the actual command. */
a30bf1f1 9588 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9589
6d820c5c 9590 if (putpkt (rs->buf) < 0)
8a3fe4f8 9591 error (_("Communication problem with target."));
96baa820
JM
9592
9593 /* get/display the response */
9594 while (1)
9595 {
2e9f7625
DJ
9596 char *buf;
9597
00bf0b85 9598 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9599 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9600 rs->buf[0] = '\0';
5b37825d
PW
9601 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9602 {
9603 /* Timeout. Continue to (try to) read responses.
9604 This is better than stopping with an error, assuming the stub
9605 is still executing the (long) monitor command.
9606 If needed, the user can interrupt gdb using C-c, obtaining
9607 an effect similar to stop on timeout. */
9608 continue;
9609 }
2e9f7625 9610 buf = rs->buf;
96baa820 9611 if (buf[0] == '\0')
8a3fe4f8 9612 error (_("Target does not support this command."));
96baa820
JM
9613 if (buf[0] == 'O' && buf[1] != 'K')
9614 {
23860348 9615 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9616 continue;
9617 }
9618 if (strcmp (buf, "OK") == 0)
9619 break;
7be570e7
JM
9620 if (strlen (buf) == 3 && buf[0] == 'E'
9621 && isdigit (buf[1]) && isdigit (buf[2]))
9622 {
8a3fe4f8 9623 error (_("Protocol error with Rcmd"));
7be570e7 9624 }
96baa820
JM
9625 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9626 {
9627 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9628
96baa820
JM
9629 fputc_unfiltered (c, outbuf);
9630 }
9631 break;
9632 }
9633}
9634
fd79ecee
DJ
9635static VEC(mem_region_s) *
9636remote_memory_map (struct target_ops *ops)
9637{
9638 VEC(mem_region_s) *result = NULL;
9639 char *text = target_read_stralloc (&current_target,
9640 TARGET_OBJECT_MEMORY_MAP, NULL);
9641
9642 if (text)
9643 {
9644 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9645
fd79ecee
DJ
9646 result = parse_memory_map (text);
9647 do_cleanups (back_to);
9648 }
9649
9650 return result;
9651}
9652
c906108c 9653static void
fba45db2 9654packet_command (char *args, int from_tty)
c906108c 9655{
d01949b6 9656 struct remote_state *rs = get_remote_state ();
c906108c 9657
5d93a237 9658 if (!rs->remote_desc)
8a3fe4f8 9659 error (_("command can only be used with remote target"));
c906108c 9660
c5aa993b 9661 if (!args)
8a3fe4f8 9662 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9663
9664 puts_filtered ("sending: ");
9665 print_packet (args);
9666 puts_filtered ("\n");
9667 putpkt (args);
9668
6d820c5c 9669 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9670 puts_filtered ("received: ");
6d820c5c 9671 print_packet (rs->buf);
c906108c
SS
9672 puts_filtered ("\n");
9673}
9674
9675#if 0
23860348 9676/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9677
a14ed312 9678static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9679
a14ed312 9680static void threadset_test_cmd (char *cmd, int tty);
c906108c 9681
a14ed312 9682static void threadalive_test (char *cmd, int tty);
c906108c 9683
a14ed312 9684static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9685
23860348 9686int get_and_display_threadinfo (threadref *ref);
c906108c 9687
a14ed312 9688static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9689
23860348 9690static int thread_display_step (threadref *ref, void *context);
c906108c 9691
a14ed312 9692static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9693
a14ed312 9694static void init_remote_threadtests (void);
c906108c 9695
23860348 9696#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9697
9698static void
fba45db2 9699threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9700{
9701 int sample_thread = SAMPLE_THREAD;
9702
a3f17187 9703 printf_filtered (_("Remote threadset test\n"));
79d7f229 9704 set_general_thread (sample_thread);
c906108c
SS
9705}
9706
9707
9708static void
fba45db2 9709threadalive_test (char *cmd, int tty)
c906108c
SS
9710{
9711 int sample_thread = SAMPLE_THREAD;
79d7f229 9712 int pid = ptid_get_pid (inferior_ptid);
ba348170 9713 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9714
79d7f229 9715 if (remote_thread_alive (ptid))
c906108c
SS
9716 printf_filtered ("PASS: Thread alive test\n");
9717 else
9718 printf_filtered ("FAIL: Thread alive test\n");
9719}
9720
23860348 9721void output_threadid (char *title, threadref *ref);
c906108c
SS
9722
9723void
fba45db2 9724output_threadid (char *title, threadref *ref)
c906108c
SS
9725{
9726 char hexid[20];
9727
23860348 9728 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9729 hexid[16] = 0;
9730 printf_filtered ("%s %s\n", title, (&hexid[0]));
9731}
9732
9733static void
fba45db2 9734threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9735{
9736 int startflag = 1;
9737 threadref nextthread;
9738 int done, result_count;
9739 threadref threadlist[3];
9740
9741 printf_filtered ("Remote Threadlist test\n");
9742 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9743 &result_count, &threadlist[0]))
9744 printf_filtered ("FAIL: threadlist test\n");
9745 else
9746 {
9747 threadref *scan = threadlist;
9748 threadref *limit = scan + result_count;
9749
9750 while (scan < limit)
9751 output_threadid (" thread ", scan++);
9752 }
9753}
9754
9755void
fba45db2 9756display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9757{
9758 output_threadid ("Threadid: ", &info->threadid);
9759 printf_filtered ("Name: %s\n ", info->shortname);
9760 printf_filtered ("State: %s\n", info->display);
9761 printf_filtered ("other: %s\n\n", info->more_display);
9762}
9763
9764int
fba45db2 9765get_and_display_threadinfo (threadref *ref)
c906108c
SS
9766{
9767 int result;
9768 int set;
9769 struct gdb_ext_thread_info threadinfo;
9770
9771 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9772 | TAG_MOREDISPLAY | TAG_DISPLAY;
9773 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9774 display_thread_info (&threadinfo);
9775 return result;
9776}
9777
9778static void
fba45db2 9779threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9780{
9781 int athread = SAMPLE_THREAD;
9782 threadref thread;
9783 int set;
9784
9785 int_to_threadref (&thread, athread);
9786 printf_filtered ("Remote Threadinfo test\n");
9787 if (!get_and_display_threadinfo (&thread))
9788 printf_filtered ("FAIL cannot get thread info\n");
9789}
9790
9791static int
fba45db2 9792thread_display_step (threadref *ref, void *context)
c906108c
SS
9793{
9794 /* output_threadid(" threadstep ",ref); *//* simple test */
9795 return get_and_display_threadinfo (ref);
9796}
9797
9798static void
fba45db2 9799threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9800{
9801 printf_filtered ("Remote Threadlist update test\n");
9802 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9803}
9804
9805static void
9806init_remote_threadtests (void)
9807{
3e43a32a
MS
9808 add_com ("tlist", class_obscure, threadlist_test_cmd,
9809 _("Fetch and print the remote list of "
9810 "thread identifiers, one pkt only"));
c906108c 9811 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9812 _("Fetch and display info about one thread"));
c906108c 9813 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9814 _("Test setting to a different thread"));
c906108c 9815 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9816 _("Iterate through updating all remote thread info"));
c906108c 9817 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9818 _(" Remote thread alive test "));
c906108c
SS
9819}
9820
9821#endif /* 0 */
9822
f3fb8c85
MS
9823/* Convert a thread ID to a string. Returns the string in a static
9824 buffer. */
9825
9826static char *
117de6a9 9827remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9828{
79d7f229 9829 static char buf[64];
82f73884 9830 struct remote_state *rs = get_remote_state ();
f3fb8c85 9831
7cee1e54
PA
9832 if (ptid_equal (ptid, null_ptid))
9833 return normal_pid_to_str (ptid);
9834 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9835 {
9836 /* Printing an inferior target id. */
9837
9838 /* When multi-process extensions are off, there's no way in the
9839 remote protocol to know the remote process id, if there's any
9840 at all. There's one exception --- when we're connected with
9841 target extended-remote, and we manually attached to a process
9842 with "attach PID". We don't record anywhere a flag that
9843 allows us to distinguish that case from the case of
9844 connecting with extended-remote and the stub already being
9845 attached to a process, and reporting yes to qAttached, hence
9846 no smart special casing here. */
9847 if (!remote_multi_process_p (rs))
9848 {
9849 xsnprintf (buf, sizeof buf, "Remote target");
9850 return buf;
9851 }
9852
9853 return normal_pid_to_str (ptid);
82f73884 9854 }
ecd0ada5 9855 else
79d7f229 9856 {
ecd0ada5
PA
9857 if (ptid_equal (magic_null_ptid, ptid))
9858 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9859 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
9860 if (ptid_get_lwp (ptid) == 0)
9861 return normal_pid_to_str (ptid);
9862 else
9863 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9864 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9865 else
9866 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9867 ptid_get_lwp (ptid));
79d7f229
PA
9868 return buf;
9869 }
f3fb8c85
MS
9870}
9871
38691318
KB
9872/* Get the address of the thread local variable in OBJFILE which is
9873 stored at OFFSET within the thread local storage for thread PTID. */
9874
9875static CORE_ADDR
117de6a9
PA
9876remote_get_thread_local_address (struct target_ops *ops,
9877 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9878{
4082afcc 9879 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9880 {
9881 struct remote_state *rs = get_remote_state ();
6d820c5c 9882 char *p = rs->buf;
82f73884 9883 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9884 enum packet_result result;
38691318
KB
9885
9886 strcpy (p, "qGetTLSAddr:");
9887 p += strlen (p);
82f73884 9888 p = write_ptid (p, endp, ptid);
38691318
KB
9889 *p++ = ',';
9890 p += hexnumstr (p, offset);
9891 *p++ = ',';
9892 p += hexnumstr (p, lm);
9893 *p++ = '\0';
9894
6d820c5c
DJ
9895 putpkt (rs->buf);
9896 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9897 result = packet_ok (rs->buf,
9898 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9899 if (result == PACKET_OK)
38691318
KB
9900 {
9901 ULONGEST result;
9902
6d820c5c 9903 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9904 return result;
9905 }
571dd617 9906 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9907 throw_error (TLS_GENERIC_ERROR,
9908 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9909 else
109c3e39
AC
9910 throw_error (TLS_GENERIC_ERROR,
9911 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9912 }
9913 else
109c3e39
AC
9914 throw_error (TLS_GENERIC_ERROR,
9915 _("TLS not supported or disabled on this target"));
38691318
KB
9916 /* Not reached. */
9917 return 0;
9918}
9919
711e434b
PM
9920/* Provide thread local base, i.e. Thread Information Block address.
9921 Returns 1 if ptid is found and thread_local_base is non zero. */
9922
70221824 9923static int
bd7ae0f5 9924remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9925{
4082afcc 9926 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9927 {
9928 struct remote_state *rs = get_remote_state ();
9929 char *p = rs->buf;
9930 char *endp = rs->buf + get_remote_packet_size ();
9931 enum packet_result result;
9932
9933 strcpy (p, "qGetTIBAddr:");
9934 p += strlen (p);
9935 p = write_ptid (p, endp, ptid);
9936 *p++ = '\0';
9937
9938 putpkt (rs->buf);
9939 getpkt (&rs->buf, &rs->buf_size, 0);
9940 result = packet_ok (rs->buf,
9941 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9942 if (result == PACKET_OK)
9943 {
9944 ULONGEST result;
9945
9946 unpack_varlen_hex (rs->buf, &result);
9947 if (addr)
9948 *addr = (CORE_ADDR) result;
9949 return 1;
9950 }
9951 else if (result == PACKET_UNKNOWN)
9952 error (_("Remote target doesn't support qGetTIBAddr packet"));
9953 else
9954 error (_("Remote target failed to process qGetTIBAddr request"));
9955 }
9956 else
9957 error (_("qGetTIBAddr not supported or disabled on this target"));
9958 /* Not reached. */
9959 return 0;
9960}
9961
29709017
DJ
9962/* Support for inferring a target description based on the current
9963 architecture and the size of a 'g' packet. While the 'g' packet
9964 can have any size (since optional registers can be left off the
9965 end), some sizes are easily recognizable given knowledge of the
9966 approximate architecture. */
9967
9968struct remote_g_packet_guess
9969{
9970 int bytes;
9971 const struct target_desc *tdesc;
9972};
9973typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9974DEF_VEC_O(remote_g_packet_guess_s);
9975
9976struct remote_g_packet_data
9977{
9978 VEC(remote_g_packet_guess_s) *guesses;
9979};
9980
9981static struct gdbarch_data *remote_g_packet_data_handle;
9982
9983static void *
9984remote_g_packet_data_init (struct obstack *obstack)
9985{
9986 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9987}
9988
9989void
9990register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9991 const struct target_desc *tdesc)
9992{
9993 struct remote_g_packet_data *data
9994 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9995 struct remote_g_packet_guess new_guess, *guess;
9996 int ix;
9997
9998 gdb_assert (tdesc != NULL);
9999
10000 for (ix = 0;
10001 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10002 ix++)
10003 if (guess->bytes == bytes)
10004 internal_error (__FILE__, __LINE__,
9b20d036 10005 _("Duplicate g packet description added for size %d"),
29709017
DJ
10006 bytes);
10007
10008 new_guess.bytes = bytes;
10009 new_guess.tdesc = tdesc;
10010 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10011}
10012
d962ef82
DJ
10013/* Return 1 if remote_read_description would do anything on this target
10014 and architecture, 0 otherwise. */
10015
10016static int
10017remote_read_description_p (struct target_ops *target)
10018{
10019 struct remote_g_packet_data *data
f5656ead 10020 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
10021
10022 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10023 return 1;
10024
10025 return 0;
10026}
10027
29709017
DJ
10028static const struct target_desc *
10029remote_read_description (struct target_ops *target)
10030{
10031 struct remote_g_packet_data *data
f5656ead 10032 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 10033
d962ef82
DJ
10034 /* Do not try this during initial connection, when we do not know
10035 whether there is a running but stopped thread. */
10036 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10037 return target->beneath->to_read_description (target->beneath);
d962ef82 10038
29709017
DJ
10039 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10040 {
10041 struct remote_g_packet_guess *guess;
10042 int ix;
10043 int bytes = send_g_packet ();
10044
10045 for (ix = 0;
10046 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10047 ix++)
10048 if (guess->bytes == bytes)
10049 return guess->tdesc;
10050
10051 /* We discard the g packet. A minor optimization would be to
10052 hold on to it, and fill the register cache once we have selected
10053 an architecture, but it's too tricky to do safely. */
10054 }
10055
2117c711 10056 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10057}
10058
a6b151f1
DJ
10059/* Remote file transfer support. This is host-initiated I/O, not
10060 target-initiated; for target-initiated, see remote-fileio.c. */
10061
10062/* If *LEFT is at least the length of STRING, copy STRING to
10063 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10064 decrease *LEFT. Otherwise raise an error. */
10065
10066static void
10067remote_buffer_add_string (char **buffer, int *left, char *string)
10068{
10069 int len = strlen (string);
10070
10071 if (len > *left)
10072 error (_("Packet too long for target."));
10073
10074 memcpy (*buffer, string, len);
10075 *buffer += len;
10076 *left -= len;
10077
10078 /* NUL-terminate the buffer as a convenience, if there is
10079 room. */
10080 if (*left)
10081 **buffer = '\0';
10082}
10083
10084/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10085 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10086 decrease *LEFT. Otherwise raise an error. */
10087
10088static void
10089remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10090 int len)
10091{
10092 if (2 * len > *left)
10093 error (_("Packet too long for target."));
10094
10095 bin2hex (bytes, *buffer, len);
10096 *buffer += 2 * len;
10097 *left -= 2 * len;
10098
10099 /* NUL-terminate the buffer as a convenience, if there is
10100 room. */
10101 if (*left)
10102 **buffer = '\0';
10103}
10104
10105/* If *LEFT is large enough, convert VALUE to hex and add it to
10106 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10107 decrease *LEFT. Otherwise raise an error. */
10108
10109static void
10110remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10111{
10112 int len = hexnumlen (value);
10113
10114 if (len > *left)
10115 error (_("Packet too long for target."));
10116
10117 hexnumstr (*buffer, value);
10118 *buffer += len;
10119 *left -= len;
10120
10121 /* NUL-terminate the buffer as a convenience, if there is
10122 room. */
10123 if (*left)
10124 **buffer = '\0';
10125}
10126
10127/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10128 value, *REMOTE_ERRNO to the remote error number or zero if none
10129 was included, and *ATTACHMENT to point to the start of the annex
10130 if any. The length of the packet isn't needed here; there may
10131 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10132
10133 Return 0 if the packet could be parsed, -1 if it could not. If
10134 -1 is returned, the other variables may not be initialized. */
10135
10136static int
10137remote_hostio_parse_result (char *buffer, int *retcode,
10138 int *remote_errno, char **attachment)
10139{
10140 char *p, *p2;
10141
10142 *remote_errno = 0;
10143 *attachment = NULL;
10144
10145 if (buffer[0] != 'F')
10146 return -1;
10147
10148 errno = 0;
10149 *retcode = strtol (&buffer[1], &p, 16);
10150 if (errno != 0 || p == &buffer[1])
10151 return -1;
10152
10153 /* Check for ",errno". */
10154 if (*p == ',')
10155 {
10156 errno = 0;
10157 *remote_errno = strtol (p + 1, &p2, 16);
10158 if (errno != 0 || p + 1 == p2)
10159 return -1;
10160 p = p2;
10161 }
10162
10163 /* Check for ";attachment". If there is no attachment, the
10164 packet should end here. */
10165 if (*p == ';')
10166 {
10167 *attachment = p + 1;
10168 return 0;
10169 }
10170 else if (*p == '\0')
10171 return 0;
10172 else
10173 return -1;
10174}
10175
10176/* Send a prepared I/O packet to the target and read its response.
10177 The prepared packet is in the global RS->BUF before this function
10178 is called, and the answer is there when we return.
10179
10180 COMMAND_BYTES is the length of the request to send, which may include
10181 binary data. WHICH_PACKET is the packet configuration to check
10182 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10183 is set to the error number and -1 is returned. Otherwise the value
10184 returned by the function is returned.
10185
10186 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10187 attachment is expected; an error will be reported if there's a
10188 mismatch. If one is found, *ATTACHMENT will be set to point into
10189 the packet buffer and *ATTACHMENT_LEN will be set to the
10190 attachment's length. */
10191
10192static int
10193remote_hostio_send_command (int command_bytes, int which_packet,
10194 int *remote_errno, char **attachment,
10195 int *attachment_len)
10196{
10197 struct remote_state *rs = get_remote_state ();
10198 int ret, bytes_read;
10199 char *attachment_tmp;
10200
5d93a237 10201 if (!rs->remote_desc
4082afcc 10202 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10203 {
10204 *remote_errno = FILEIO_ENOSYS;
10205 return -1;
10206 }
10207
10208 putpkt_binary (rs->buf, command_bytes);
10209 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10210
10211 /* If it timed out, something is wrong. Don't try to parse the
10212 buffer. */
10213 if (bytes_read < 0)
10214 {
10215 *remote_errno = FILEIO_EINVAL;
10216 return -1;
10217 }
10218
10219 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10220 {
10221 case PACKET_ERROR:
10222 *remote_errno = FILEIO_EINVAL;
10223 return -1;
10224 case PACKET_UNKNOWN:
10225 *remote_errno = FILEIO_ENOSYS;
10226 return -1;
10227 case PACKET_OK:
10228 break;
10229 }
10230
10231 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10232 &attachment_tmp))
10233 {
10234 *remote_errno = FILEIO_EINVAL;
10235 return -1;
10236 }
10237
10238 /* Make sure we saw an attachment if and only if we expected one. */
10239 if ((attachment_tmp == NULL && attachment != NULL)
10240 || (attachment_tmp != NULL && attachment == NULL))
10241 {
10242 *remote_errno = FILEIO_EINVAL;
10243 return -1;
10244 }
10245
10246 /* If an attachment was found, it must point into the packet buffer;
10247 work out how many bytes there were. */
10248 if (attachment_tmp != NULL)
10249 {
10250 *attachment = attachment_tmp;
10251 *attachment_len = bytes_read - (*attachment - rs->buf);
10252 }
10253
10254 return ret;
10255}
10256
15a201c8
GB
10257/* Set the filesystem remote_hostio functions that take FILENAME
10258 arguments will use. Return 0 on success, or -1 if an error
10259 occurs (and set *REMOTE_ERRNO). */
10260
10261static int
10262remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10263{
10264 struct remote_state *rs = get_remote_state ();
10265 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10266 char *p = rs->buf;
10267 int left = get_remote_packet_size () - 1;
10268 char arg[9];
10269 int ret;
10270
10271 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10272 return 0;
10273
10274 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10275 return 0;
10276
10277 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10278
10279 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10280 remote_buffer_add_string (&p, &left, arg);
10281
10282 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10283 remote_errno, NULL, NULL);
10284
10285 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10286 return 0;
10287
10288 if (ret == 0)
10289 rs->fs_pid = required_pid;
10290
10291 return ret;
10292}
10293
12e2a5fd 10294/* Implementation of to_fileio_open. */
a6b151f1
DJ
10295
10296static int
cd897586 10297remote_hostio_open (struct target_ops *self,
07c138c8
GB
10298 struct inferior *inf, const char *filename,
10299 int flags, int mode, int *remote_errno)
a6b151f1
DJ
10300{
10301 struct remote_state *rs = get_remote_state ();
10302 char *p = rs->buf;
10303 int left = get_remote_packet_size () - 1;
10304
15a201c8
GB
10305 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10306 return -1;
10307
a6b151f1
DJ
10308 remote_buffer_add_string (&p, &left, "vFile:open:");
10309
10310 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10311 strlen (filename));
10312 remote_buffer_add_string (&p, &left, ",");
10313
10314 remote_buffer_add_int (&p, &left, flags);
10315 remote_buffer_add_string (&p, &left, ",");
10316
10317 remote_buffer_add_int (&p, &left, mode);
10318
10319 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10320 remote_errno, NULL, NULL);
10321}
10322
12e2a5fd 10323/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10324
10325static int
0d866f62
TT
10326remote_hostio_pwrite (struct target_ops *self,
10327 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10328 ULONGEST offset, int *remote_errno)
10329{
10330 struct remote_state *rs = get_remote_state ();
10331 char *p = rs->buf;
10332 int left = get_remote_packet_size ();
10333 int out_len;
10334
10335 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10336
10337 remote_buffer_add_int (&p, &left, fd);
10338 remote_buffer_add_string (&p, &left, ",");
10339
10340 remote_buffer_add_int (&p, &left, offset);
10341 remote_buffer_add_string (&p, &left, ",");
10342
124e13d9 10343 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10344 get_remote_packet_size () - (p - rs->buf));
10345
10346 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10347 remote_errno, NULL, NULL);
10348}
10349
12e2a5fd 10350/* Implementation of to_fileio_pread. */
a6b151f1
DJ
10351
10352static int
a3be983c
TT
10353remote_hostio_pread (struct target_ops *self,
10354 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
10355 ULONGEST offset, int *remote_errno)
10356{
10357 struct remote_state *rs = get_remote_state ();
10358 char *p = rs->buf;
10359 char *attachment;
10360 int left = get_remote_packet_size ();
10361 int ret, attachment_len;
10362 int read_len;
10363
10364 remote_buffer_add_string (&p, &left, "vFile:pread:");
10365
10366 remote_buffer_add_int (&p, &left, fd);
10367 remote_buffer_add_string (&p, &left, ",");
10368
10369 remote_buffer_add_int (&p, &left, len);
10370 remote_buffer_add_string (&p, &left, ",");
10371
10372 remote_buffer_add_int (&p, &left, offset);
10373
10374 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10375 remote_errno, &attachment,
10376 &attachment_len);
10377
10378 if (ret < 0)
10379 return ret;
10380
bc20a4af 10381 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10382 read_buf, len);
10383 if (read_len != ret)
10384 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10385
10386 return ret;
10387}
10388
12e2a5fd 10389/* Implementation of to_fileio_close. */
a6b151f1
DJ
10390
10391static int
df39ea25 10392remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10393{
10394 struct remote_state *rs = get_remote_state ();
10395 char *p = rs->buf;
10396 int left = get_remote_packet_size () - 1;
10397
10398 remote_buffer_add_string (&p, &left, "vFile:close:");
10399
10400 remote_buffer_add_int (&p, &left, fd);
10401
10402 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10403 remote_errno, NULL, NULL);
10404}
10405
12e2a5fd 10406/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10407
10408static int
dbbca37d 10409remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
10410 struct inferior *inf, const char *filename,
10411 int *remote_errno)
a6b151f1
DJ
10412{
10413 struct remote_state *rs = get_remote_state ();
10414 char *p = rs->buf;
10415 int left = get_remote_packet_size () - 1;
10416
15a201c8
GB
10417 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10418 return -1;
10419
a6b151f1
DJ
10420 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10421
10422 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10423 strlen (filename));
10424
10425 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10426 remote_errno, NULL, NULL);
10427}
10428
12e2a5fd 10429/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10430
10431static char *
fab5aa7c 10432remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
10433 struct inferior *inf, const char *filename,
10434 int *remote_errno)
b9e7b9c3
UW
10435{
10436 struct remote_state *rs = get_remote_state ();
10437 char *p = rs->buf;
10438 char *attachment;
10439 int left = get_remote_packet_size ();
10440 int len, attachment_len;
10441 int read_len;
10442 char *ret;
10443
15a201c8
GB
10444 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10445 return NULL;
10446
b9e7b9c3
UW
10447 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10448
10449 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10450 strlen (filename));
10451
10452 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10453 remote_errno, &attachment,
10454 &attachment_len);
10455
10456 if (len < 0)
10457 return NULL;
10458
10459 ret = xmalloc (len + 1);
10460
bc20a4af
PA
10461 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10462 (gdb_byte *) ret, len);
b9e7b9c3
UW
10463 if (read_len != len)
10464 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10465
10466 ret[len] = '\0';
10467 return ret;
10468}
10469
12e2a5fd 10470/* Implementation of to_fileio_fstat. */
0a93529c
GB
10471
10472static int
10473remote_hostio_fstat (struct target_ops *self,
10474 int fd, struct stat *st,
10475 int *remote_errno)
10476{
10477 struct remote_state *rs = get_remote_state ();
10478 char *p = rs->buf;
10479 int left = get_remote_packet_size ();
10480 int attachment_len, ret;
10481 char *attachment;
10482 struct fio_stat fst;
10483 int read_len;
10484
464b0089
GB
10485 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10486
10487 remote_buffer_add_int (&p, &left, fd);
10488
10489 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10490 remote_errno, &attachment,
10491 &attachment_len);
10492 if (ret < 0)
0a93529c 10493 {
464b0089
GB
10494 if (*remote_errno != FILEIO_ENOSYS)
10495 return ret;
10496
0a93529c
GB
10497 /* Strictly we should return -1, ENOSYS here, but when
10498 "set sysroot remote:" was implemented in August 2008
10499 BFD's need for a stat function was sidestepped with
10500 this hack. This was not remedied until March 2015
10501 so we retain the previous behavior to avoid breaking
10502 compatibility.
10503
10504 Note that the memset is a March 2015 addition; older
10505 GDBs set st_size *and nothing else* so the structure
10506 would have garbage in all other fields. This might
10507 break something but retaining the previous behavior
10508 here would be just too wrong. */
10509
10510 memset (st, 0, sizeof (struct stat));
10511 st->st_size = INT_MAX;
10512 return 0;
10513 }
10514
0a93529c
GB
10515 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10516 (gdb_byte *) &fst, sizeof (fst));
10517
10518 if (read_len != ret)
10519 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10520
10521 if (read_len != sizeof (fst))
10522 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10523 read_len, (int) sizeof (fst));
10524
10525 remote_fileio_to_host_stat (&fst, st);
10526
10527 return 0;
10528}
10529
12e2a5fd 10530/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10531
10532static int
10533remote_filesystem_is_local (struct target_ops *self)
10534{
10535 /* Valgrind GDB presents itself as a remote target but works
10536 on the local filesystem: it does not implement remote get
10537 and users are not expected to set a sysroot. To handle
10538 this case we treat the remote filesystem as local if the
10539 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10540 does not support vFile:open. */
a3be80c3 10541 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
10542 {
10543 enum packet_support ps = packet_support (PACKET_vFile_open);
10544
10545 if (ps == PACKET_SUPPORT_UNKNOWN)
10546 {
10547 int fd, remote_errno;
10548
10549 /* Try opening a file to probe support. The supplied
10550 filename is irrelevant, we only care about whether
10551 the stub recognizes the packet or not. */
07c138c8 10552 fd = remote_hostio_open (self, NULL, "just probing",
e3dd7556
GB
10553 FILEIO_O_RDONLY, 0700,
10554 &remote_errno);
10555
10556 if (fd >= 0)
10557 remote_hostio_close (self, fd, &remote_errno);
10558
10559 ps = packet_support (PACKET_vFile_open);
10560 }
10561
10562 if (ps == PACKET_DISABLE)
10563 {
10564 static int warning_issued = 0;
10565
10566 if (!warning_issued)
10567 {
10568 warning (_("remote target does not support file"
10569 " transfer, attempting to access files"
10570 " from local filesystem."));
10571 warning_issued = 1;
10572 }
10573
10574 return 1;
10575 }
10576 }
10577
10578 return 0;
10579}
10580
a6b151f1
DJ
10581static int
10582remote_fileio_errno_to_host (int errnum)
10583{
10584 switch (errnum)
10585 {
10586 case FILEIO_EPERM:
10587 return EPERM;
10588 case FILEIO_ENOENT:
10589 return ENOENT;
10590 case FILEIO_EINTR:
10591 return EINTR;
10592 case FILEIO_EIO:
10593 return EIO;
10594 case FILEIO_EBADF:
10595 return EBADF;
10596 case FILEIO_EACCES:
10597 return EACCES;
10598 case FILEIO_EFAULT:
10599 return EFAULT;
10600 case FILEIO_EBUSY:
10601 return EBUSY;
10602 case FILEIO_EEXIST:
10603 return EEXIST;
10604 case FILEIO_ENODEV:
10605 return ENODEV;
10606 case FILEIO_ENOTDIR:
10607 return ENOTDIR;
10608 case FILEIO_EISDIR:
10609 return EISDIR;
10610 case FILEIO_EINVAL:
10611 return EINVAL;
10612 case FILEIO_ENFILE:
10613 return ENFILE;
10614 case FILEIO_EMFILE:
10615 return EMFILE;
10616 case FILEIO_EFBIG:
10617 return EFBIG;
10618 case FILEIO_ENOSPC:
10619 return ENOSPC;
10620 case FILEIO_ESPIPE:
10621 return ESPIPE;
10622 case FILEIO_EROFS:
10623 return EROFS;
10624 case FILEIO_ENOSYS:
10625 return ENOSYS;
10626 case FILEIO_ENAMETOOLONG:
10627 return ENAMETOOLONG;
10628 }
10629 return -1;
10630}
10631
10632static char *
10633remote_hostio_error (int errnum)
10634{
10635 int host_error = remote_fileio_errno_to_host (errnum);
10636
10637 if (host_error == -1)
10638 error (_("Unknown remote I/O error %d"), errnum);
10639 else
10640 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10641}
10642
a6b151f1
DJ
10643static void
10644remote_hostio_close_cleanup (void *opaque)
10645{
10646 int fd = *(int *) opaque;
10647 int remote_errno;
10648
df39ea25 10649 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10650}
10651
10652void
10653remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10654{
10655 struct cleanup *back_to, *close_cleanup;
10656 int retcode, fd, remote_errno, bytes, io_size;
10657 FILE *file;
10658 gdb_byte *buffer;
10659 int bytes_in_buffer;
10660 int saw_eof;
10661 ULONGEST offset;
5d93a237 10662 struct remote_state *rs = get_remote_state ();
a6b151f1 10663
5d93a237 10664 if (!rs->remote_desc)
a6b151f1
DJ
10665 error (_("command can only be used with remote target"));
10666
614c279d 10667 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10668 if (file == NULL)
10669 perror_with_name (local_file);
7c8a8b04 10670 back_to = make_cleanup_fclose (file);
a6b151f1 10671
07c138c8 10672 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10673 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10674 | FILEIO_O_TRUNC),
10675 0700, &remote_errno);
10676 if (fd == -1)
10677 remote_hostio_error (remote_errno);
10678
10679 /* Send up to this many bytes at once. They won't all fit in the
10680 remote packet limit, so we'll transfer slightly fewer. */
10681 io_size = get_remote_packet_size ();
10682 buffer = xmalloc (io_size);
10683 make_cleanup (xfree, buffer);
10684
10685 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10686
10687 bytes_in_buffer = 0;
10688 saw_eof = 0;
10689 offset = 0;
10690 while (bytes_in_buffer || !saw_eof)
10691 {
10692 if (!saw_eof)
10693 {
3e43a32a
MS
10694 bytes = fread (buffer + bytes_in_buffer, 1,
10695 io_size - bytes_in_buffer,
a6b151f1
DJ
10696 file);
10697 if (bytes == 0)
10698 {
10699 if (ferror (file))
10700 error (_("Error reading %s."), local_file);
10701 else
10702 {
10703 /* EOF. Unless there is something still in the
10704 buffer from the last iteration, we are done. */
10705 saw_eof = 1;
10706 if (bytes_in_buffer == 0)
10707 break;
10708 }
10709 }
10710 }
10711 else
10712 bytes = 0;
10713
10714 bytes += bytes_in_buffer;
10715 bytes_in_buffer = 0;
10716
0d866f62
TT
10717 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10718 fd, buffer, bytes,
3e43a32a 10719 offset, &remote_errno);
a6b151f1
DJ
10720
10721 if (retcode < 0)
10722 remote_hostio_error (remote_errno);
10723 else if (retcode == 0)
10724 error (_("Remote write of %d bytes returned 0!"), bytes);
10725 else if (retcode < bytes)
10726 {
10727 /* Short write. Save the rest of the read data for the next
10728 write. */
10729 bytes_in_buffer = bytes - retcode;
10730 memmove (buffer, buffer + retcode, bytes_in_buffer);
10731 }
10732
10733 offset += retcode;
10734 }
10735
10736 discard_cleanups (close_cleanup);
df39ea25 10737 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10738 remote_hostio_error (remote_errno);
10739
10740 if (from_tty)
10741 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10742 do_cleanups (back_to);
10743}
10744
10745void
10746remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10747{
10748 struct cleanup *back_to, *close_cleanup;
cea39f65 10749 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10750 FILE *file;
10751 gdb_byte *buffer;
10752 ULONGEST offset;
5d93a237 10753 struct remote_state *rs = get_remote_state ();
a6b151f1 10754
5d93a237 10755 if (!rs->remote_desc)
a6b151f1
DJ
10756 error (_("command can only be used with remote target"));
10757
07c138c8 10758 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10759 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10760 if (fd == -1)
10761 remote_hostio_error (remote_errno);
10762
614c279d 10763 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10764 if (file == NULL)
10765 perror_with_name (local_file);
7c8a8b04 10766 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10767
10768 /* Send up to this many bytes at once. They won't all fit in the
10769 remote packet limit, so we'll transfer slightly fewer. */
10770 io_size = get_remote_packet_size ();
10771 buffer = xmalloc (io_size);
10772 make_cleanup (xfree, buffer);
10773
10774 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10775
10776 offset = 0;
10777 while (1)
10778 {
a3be983c
TT
10779 bytes = remote_hostio_pread (find_target_at (process_stratum),
10780 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10781 if (bytes == 0)
10782 /* Success, but no bytes, means end-of-file. */
10783 break;
10784 if (bytes == -1)
10785 remote_hostio_error (remote_errno);
10786
10787 offset += bytes;
10788
10789 bytes = fwrite (buffer, 1, bytes, file);
10790 if (bytes == 0)
10791 perror_with_name (local_file);
10792 }
10793
10794 discard_cleanups (close_cleanup);
df39ea25 10795 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10796 remote_hostio_error (remote_errno);
10797
10798 if (from_tty)
10799 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10800 do_cleanups (back_to);
10801}
10802
10803void
10804remote_file_delete (const char *remote_file, int from_tty)
10805{
10806 int retcode, remote_errno;
5d93a237 10807 struct remote_state *rs = get_remote_state ();
a6b151f1 10808
5d93a237 10809 if (!rs->remote_desc)
a6b151f1
DJ
10810 error (_("command can only be used with remote target"));
10811
dbbca37d 10812 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 10813 NULL, remote_file, &remote_errno);
a6b151f1
DJ
10814 if (retcode == -1)
10815 remote_hostio_error (remote_errno);
10816
10817 if (from_tty)
10818 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10819}
10820
10821static void
10822remote_put_command (char *args, int from_tty)
10823{
10824 struct cleanup *back_to;
10825 char **argv;
10826
d1a41061
PP
10827 if (args == NULL)
10828 error_no_arg (_("file to put"));
10829
10830 argv = gdb_buildargv (args);
a6b151f1
DJ
10831 back_to = make_cleanup_freeargv (argv);
10832 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10833 error (_("Invalid parameters to remote put"));
10834
10835 remote_file_put (argv[0], argv[1], from_tty);
10836
10837 do_cleanups (back_to);
10838}
10839
10840static void
10841remote_get_command (char *args, int from_tty)
10842{
10843 struct cleanup *back_to;
10844 char **argv;
10845
d1a41061
PP
10846 if (args == NULL)
10847 error_no_arg (_("file to get"));
10848
10849 argv = gdb_buildargv (args);
a6b151f1
DJ
10850 back_to = make_cleanup_freeargv (argv);
10851 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10852 error (_("Invalid parameters to remote get"));
10853
10854 remote_file_get (argv[0], argv[1], from_tty);
10855
10856 do_cleanups (back_to);
10857}
10858
10859static void
10860remote_delete_command (char *args, int from_tty)
10861{
10862 struct cleanup *back_to;
10863 char **argv;
10864
d1a41061
PP
10865 if (args == NULL)
10866 error_no_arg (_("file to delete"));
10867
10868 argv = gdb_buildargv (args);
a6b151f1
DJ
10869 back_to = make_cleanup_freeargv (argv);
10870 if (argv[0] == NULL || argv[1] != NULL)
10871 error (_("Invalid parameters to remote delete"));
10872
10873 remote_file_delete (argv[0], from_tty);
10874
10875 do_cleanups (back_to);
10876}
10877
10878static void
10879remote_command (char *args, int from_tty)
10880{
635c7e8a 10881 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10882}
10883
b2175913 10884static int
19db3e69 10885remote_can_execute_reverse (struct target_ops *self)
b2175913 10886{
4082afcc
PA
10887 if (packet_support (PACKET_bs) == PACKET_ENABLE
10888 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10889 return 1;
10890 else
10891 return 0;
b2175913
MS
10892}
10893
74531fed 10894static int
2a9a2795 10895remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10896{
10897 return 1;
10898}
10899
03583c20 10900static int
2bfc0540 10901remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10902{
10903 /* Only supported in extended mode. */
10904 return 0;
10905}
10906
8a305172 10907static int
86ce2668 10908remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10909{
10910 struct remote_state *rs = get_remote_state ();
a744cf53 10911
901f9912
UW
10912 /* Only extended-remote handles being attached to multiple
10913 processes, even though plain remote can use the multi-process
10914 thread id extensions, so that GDB knows the target process's
10915 PID. */
10916 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10917}
10918
70221824 10919static int
782b2b07
SS
10920remote_supports_cond_tracepoints (void)
10921{
4082afcc 10922 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10923}
10924
3788aec7 10925static int
efcc2da7 10926remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10927{
4082afcc 10928 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10929}
10930
70221824 10931static int
7a697b8d
SS
10932remote_supports_fast_tracepoints (void)
10933{
4082afcc 10934 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10935}
10936
0fb4aa4b
PA
10937static int
10938remote_supports_static_tracepoints (void)
10939{
4082afcc 10940 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
10941}
10942
1e4d1764
YQ
10943static int
10944remote_supports_install_in_trace (void)
10945{
4082afcc 10946 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
10947}
10948
d248b706 10949static int
7d178d6a 10950remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 10951{
4082afcc
PA
10952 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10953 == PACKET_ENABLE);
d248b706
KY
10954}
10955
3065dfb6 10956static int
6de37a3a 10957remote_supports_string_tracing (struct target_ops *self)
3065dfb6 10958{
4082afcc 10959 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
10960}
10961
d3ce09f5 10962static int
78eff0ec 10963remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 10964{
4082afcc 10965 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
10966}
10967
35b1e5cc 10968static void
ecae04e1 10969remote_trace_init (struct target_ops *self)
35b1e5cc
SS
10970{
10971 putpkt ("QTinit");
10972 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 10973 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10974 error (_("Target does not support this command."));
10975}
10976
10977static void free_actions_list (char **actions_list);
10978static void free_actions_list_cleanup_wrapper (void *);
10979static void
10980free_actions_list_cleanup_wrapper (void *al)
10981{
10982 free_actions_list (al);
10983}
10984
10985static void
10986free_actions_list (char **actions_list)
10987{
10988 int ndx;
10989
10990 if (actions_list == 0)
10991 return;
10992
10993 for (ndx = 0; actions_list[ndx]; ndx++)
10994 xfree (actions_list[ndx]);
10995
10996 xfree (actions_list);
10997}
10998
409873ef
SS
10999/* Recursive routine to walk through command list including loops, and
11000 download packets for each command. */
11001
11002static void
11003remote_download_command_source (int num, ULONGEST addr,
11004 struct command_line *cmds)
11005{
11006 struct remote_state *rs = get_remote_state ();
11007 struct command_line *cmd;
11008
11009 for (cmd = cmds; cmd; cmd = cmd->next)
11010 {
0df8b418 11011 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11012 strcpy (rs->buf, "QTDPsrc:");
11013 encode_source_string (num, addr, "cmd", cmd->line,
11014 rs->buf + strlen (rs->buf),
11015 rs->buf_size - strlen (rs->buf));
11016 putpkt (rs->buf);
11017 remote_get_noisy_reply (&target_buf, &target_buf_size);
11018 if (strcmp (target_buf, "OK"))
11019 warning (_("Target does not support source download."));
11020
11021 if (cmd->control_type == while_control
11022 || cmd->control_type == while_stepping_control)
11023 {
11024 remote_download_command_source (num, addr, *cmd->body_list);
11025
0df8b418 11026 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11027 strcpy (rs->buf, "QTDPsrc:");
11028 encode_source_string (num, addr, "cmd", "end",
11029 rs->buf + strlen (rs->buf),
11030 rs->buf_size - strlen (rs->buf));
11031 putpkt (rs->buf);
11032 remote_get_noisy_reply (&target_buf, &target_buf_size);
11033 if (strcmp (target_buf, "OK"))
11034 warning (_("Target does not support source download."));
11035 }
11036 }
11037}
11038
35b1e5cc 11039static void
548f7808 11040remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11041{
bba74b36 11042#define BUF_SIZE 2048
e8ba3115 11043
35b1e5cc 11044 CORE_ADDR tpaddr;
409873ef 11045 char addrbuf[40];
bba74b36 11046 char buf[BUF_SIZE];
35b1e5cc
SS
11047 char **tdp_actions;
11048 char **stepping_actions;
11049 int ndx;
11050 struct cleanup *old_chain = NULL;
11051 struct agent_expr *aexpr;
11052 struct cleanup *aexpr_chain = NULL;
11053 char *pkt;
e8ba3115 11054 struct breakpoint *b = loc->owner;
d9b3f62e 11055 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11056
dc673c81 11057 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11058 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11059 tdp_actions);
11060 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11061 stepping_actions);
11062
11063 tpaddr = loc->address;
11064 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11065 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11066 addrbuf, /* address */
11067 (b->enable_state == bp_enabled ? 'E' : 'D'),
11068 t->step_count, t->pass_count);
e8ba3115
YQ
11069 /* Fast tracepoints are mostly handled by the target, but we can
11070 tell the target how big of an instruction block should be moved
11071 around. */
11072 if (b->type == bp_fast_tracepoint)
11073 {
11074 /* Only test for support at download time; we may not know
11075 target capabilities at definition time. */
11076 if (remote_supports_fast_tracepoints ())
35b1e5cc 11077 {
e8ba3115 11078 int isize;
35b1e5cc 11079
f5656ead 11080 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
e8ba3115 11081 tpaddr, &isize, NULL))
bba74b36
YQ
11082 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
11083 isize);
35b1e5cc 11084 else
e8ba3115
YQ
11085 /* If it passed validation at definition but fails now,
11086 something is very wrong. */
11087 internal_error (__FILE__, __LINE__,
11088 _("Fast tracepoint not "
11089 "valid during download"));
35b1e5cc 11090 }
e8ba3115
YQ
11091 else
11092 /* Fast tracepoints are functionally identical to regular
11093 tracepoints, so don't take lack of support as a reason to
11094 give up on the trace run. */
11095 warning (_("Target does not support fast tracepoints, "
11096 "downloading %d as regular tracepoint"), b->number);
11097 }
11098 else if (b->type == bp_static_tracepoint)
11099 {
11100 /* Only test for support at download time; we may not know
11101 target capabilities at definition time. */
11102 if (remote_supports_static_tracepoints ())
0fb4aa4b 11103 {
e8ba3115 11104 struct static_tracepoint_marker marker;
0fb4aa4b 11105
e8ba3115
YQ
11106 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11107 strcat (buf, ":S");
0fb4aa4b 11108 else
e8ba3115 11109 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11110 }
e8ba3115
YQ
11111 else
11112 /* Fast tracepoints are functionally identical to regular
11113 tracepoints, so don't take lack of support as a reason
11114 to give up on the trace run. */
11115 error (_("Target does not support static tracepoints"));
11116 }
11117 /* If the tracepoint has a conditional, make it into an agent
11118 expression and append to the definition. */
11119 if (loc->cond)
11120 {
11121 /* Only test support at download time, we may not know target
11122 capabilities at definition time. */
11123 if (remote_supports_cond_tracepoints ())
35b1e5cc 11124 {
e8ba3115
YQ
11125 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11126 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11127 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11128 aexpr->len);
e8ba3115
YQ
11129 pkt = buf + strlen (buf);
11130 for (ndx = 0; ndx < aexpr->len; ++ndx)
11131 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11132 *pkt = '\0';
11133 do_cleanups (aexpr_chain);
35b1e5cc 11134 }
e8ba3115
YQ
11135 else
11136 warning (_("Target does not support conditional tracepoints, "
11137 "ignoring tp %d cond"), b->number);
11138 }
35b1e5cc 11139
d9b3f62e 11140 if (b->commands || *default_collect)
e8ba3115
YQ
11141 strcat (buf, "-");
11142 putpkt (buf);
11143 remote_get_noisy_reply (&target_buf, &target_buf_size);
11144 if (strcmp (target_buf, "OK"))
11145 error (_("Target does not support tracepoints."));
35b1e5cc 11146
e8ba3115
YQ
11147 /* do_single_steps (t); */
11148 if (tdp_actions)
11149 {
11150 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11151 {
e8ba3115 11152 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11153 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11154 b->number, addrbuf, /* address */
11155 tdp_actions[ndx],
11156 ((tdp_actions[ndx + 1] || stepping_actions)
11157 ? '-' : 0));
e8ba3115
YQ
11158 putpkt (buf);
11159 remote_get_noisy_reply (&target_buf,
11160 &target_buf_size);
11161 if (strcmp (target_buf, "OK"))
11162 error (_("Error on target while setting tracepoints."));
35b1e5cc 11163 }
e8ba3115
YQ
11164 }
11165 if (stepping_actions)
11166 {
11167 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11168 {
e8ba3115 11169 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11170 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11171 b->number, addrbuf, /* address */
11172 ((ndx == 0) ? "S" : ""),
11173 stepping_actions[ndx],
11174 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11175 putpkt (buf);
11176 remote_get_noisy_reply (&target_buf,
11177 &target_buf_size);
11178 if (strcmp (target_buf, "OK"))
11179 error (_("Error on target while setting tracepoints."));
35b1e5cc 11180 }
e8ba3115 11181 }
409873ef 11182
4082afcc 11183 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115
YQ
11184 {
11185 if (b->addr_string)
409873ef 11186 {
e8ba3115
YQ
11187 strcpy (buf, "QTDPsrc:");
11188 encode_source_string (b->number, loc->address,
11189 "at", b->addr_string, buf + strlen (buf),
11190 2048 - strlen (buf));
409873ef 11191
e8ba3115
YQ
11192 putpkt (buf);
11193 remote_get_noisy_reply (&target_buf, &target_buf_size);
11194 if (strcmp (target_buf, "OK"))
11195 warning (_("Target does not support source download."));
409873ef 11196 }
e8ba3115
YQ
11197 if (b->cond_string)
11198 {
11199 strcpy (buf, "QTDPsrc:");
11200 encode_source_string (b->number, loc->address,
11201 "cond", b->cond_string, buf + strlen (buf),
11202 2048 - strlen (buf));
11203 putpkt (buf);
11204 remote_get_noisy_reply (&target_buf, &target_buf_size);
11205 if (strcmp (target_buf, "OK"))
11206 warning (_("Target does not support source download."));
11207 }
11208 remote_download_command_source (b->number, loc->address,
11209 breakpoint_commands (b));
35b1e5cc 11210 }
e8ba3115
YQ
11211
11212 do_cleanups (old_chain);
35b1e5cc
SS
11213}
11214
1e4d1764 11215static int
a52a8357 11216remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11217{
1e51243a
PA
11218 struct remote_state *rs = get_remote_state ();
11219 struct trace_status *ts;
11220 int status;
11221
11222 /* Don't try to install tracepoints until we've relocated our
11223 symbols, and fetched and merged the target's tracepoint list with
11224 ours. */
11225 if (rs->starting_up)
11226 return 0;
11227
11228 ts = current_trace_status ();
8bd200f1 11229 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11230
11231 if (status == -1 || !ts->running_known || !ts->running)
11232 return 0;
11233
11234 /* If we are in a tracing experiment, but remote stub doesn't support
11235 installing tracepoint in trace, we have to return. */
11236 if (!remote_supports_install_in_trace ())
11237 return 0;
11238
11239 return 1;
11240}
11241
11242
35b1e5cc 11243static void
559d2b81
TT
11244remote_download_trace_state_variable (struct target_ops *self,
11245 struct trace_state_variable *tsv)
35b1e5cc
SS
11246{
11247 struct remote_state *rs = get_remote_state ();
00bf0b85 11248 char *p;
35b1e5cc 11249
bba74b36
YQ
11250 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11251 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11252 tsv->builtin);
00bf0b85
SS
11253 p = rs->buf + strlen (rs->buf);
11254 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11255 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11256 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11257 *p++ = '\0';
35b1e5cc
SS
11258 putpkt (rs->buf);
11259 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11260 if (*target_buf == '\0')
11261 error (_("Target does not support this command."));
11262 if (strcmp (target_buf, "OK") != 0)
11263 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11264}
11265
d248b706 11266static void
46670d57
TT
11267remote_enable_tracepoint (struct target_ops *self,
11268 struct bp_location *location)
d248b706
KY
11269{
11270 struct remote_state *rs = get_remote_state ();
11271 char addr_buf[40];
11272
11273 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11274 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11275 location->owner->number, addr_buf);
d248b706
KY
11276 putpkt (rs->buf);
11277 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11278 if (*rs->buf == '\0')
11279 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11280 if (strcmp (rs->buf, "OK") != 0)
11281 error (_("Error on target while enabling tracepoint."));
11282}
11283
11284static void
780b049c
TT
11285remote_disable_tracepoint (struct target_ops *self,
11286 struct bp_location *location)
d248b706
KY
11287{
11288 struct remote_state *rs = get_remote_state ();
11289 char addr_buf[40];
11290
11291 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11292 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11293 location->owner->number, addr_buf);
d248b706
KY
11294 putpkt (rs->buf);
11295 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11296 if (*rs->buf == '\0')
11297 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11298 if (strcmp (rs->buf, "OK") != 0)
11299 error (_("Error on target while disabling tracepoint."));
11300}
11301
35b1e5cc 11302static void
583f9a86 11303remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11304{
11305 asection *s;
81b9b86e 11306 bfd *abfd = NULL;
35b1e5cc 11307 bfd_size_type size;
608bcef2 11308 bfd_vma vma;
35b1e5cc 11309 int anysecs = 0;
c2fa21f1 11310 int offset = 0;
35b1e5cc
SS
11311
11312 if (!exec_bfd)
11313 return; /* No information to give. */
11314
11315 strcpy (target_buf, "QTro");
9779ab84 11316 offset = strlen (target_buf);
35b1e5cc
SS
11317 for (s = exec_bfd->sections; s; s = s->next)
11318 {
11319 char tmp1[40], tmp2[40];
c2fa21f1 11320 int sec_length;
35b1e5cc
SS
11321
11322 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11323 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11324 (s->flags & SEC_READONLY) == 0)
11325 continue;
11326
11327 anysecs = 1;
81b9b86e 11328 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11329 size = bfd_get_section_size (s);
608bcef2
HZ
11330 sprintf_vma (tmp1, vma);
11331 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11332 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11333 if (offset + sec_length + 1 > target_buf_size)
11334 {
4082afcc 11335 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11336 warning (_("\
c2fa21f1
HZ
11337Too many sections for read-only sections definition packet."));
11338 break;
11339 }
bba74b36
YQ
11340 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11341 tmp1, tmp2);
c2fa21f1 11342 offset += sec_length;
35b1e5cc
SS
11343 }
11344 if (anysecs)
11345 {
11346 putpkt (target_buf);
11347 getpkt (&target_buf, &target_buf_size, 0);
11348 }
11349}
11350
11351static void
e2d1aae3 11352remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11353{
11354 putpkt ("QTStart");
11355 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11356 if (*target_buf == '\0')
11357 error (_("Target does not support this command."));
11358 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11359 error (_("Bogus reply from target: %s"), target_buf);
11360}
11361
11362static int
8bd200f1 11363remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11364{
953b98d1 11365 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11366 char *p = NULL;
0df8b418 11367 /* FIXME we need to get register block size some other way. */
00bf0b85 11368 extern int trace_regblock_size;
bd3eecc3
PA
11369 enum packet_result result;
11370
4082afcc 11371 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11372 return -1;
a744cf53 11373
00bf0b85
SS
11374 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11375
049dc89b
JK
11376 putpkt ("qTStatus");
11377
492d29ea 11378 TRY
67f41397
JK
11379 {
11380 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11381 }
492d29ea 11382 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11383 {
598d3636
JK
11384 if (ex.error != TARGET_CLOSE_ERROR)
11385 {
11386 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11387 return -1;
11388 }
11389 throw_exception (ex);
67f41397 11390 }
492d29ea 11391 END_CATCH
00bf0b85 11392
bd3eecc3
PA
11393 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11394
00bf0b85 11395 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11396 if (result == PACKET_UNKNOWN)
00bf0b85 11397 return -1;
35b1e5cc 11398
00bf0b85 11399 /* We're working with a live target. */
f5911ea1 11400 ts->filename = NULL;
00bf0b85 11401
00bf0b85 11402 if (*p++ != 'T')
35b1e5cc
SS
11403 error (_("Bogus trace status reply from target: %s"), target_buf);
11404
84cebc4a
YQ
11405 /* Function 'parse_trace_status' sets default value of each field of
11406 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11407 parse_trace_status (p, ts);
11408
11409 return ts->running;
35b1e5cc
SS
11410}
11411
70221824 11412static void
db90e85c 11413remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11414 struct uploaded_tp *utp)
11415{
11416 struct remote_state *rs = get_remote_state ();
f196051f
SS
11417 char *reply;
11418 struct bp_location *loc;
11419 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11420 size_t size = get_remote_packet_size ();
f196051f
SS
11421
11422 if (tp)
11423 {
11424 tp->base.hit_count = 0;
11425 tp->traceframe_usage = 0;
11426 for (loc = tp->base.loc; loc; loc = loc->next)
11427 {
11428 /* If the tracepoint was never downloaded, don't go asking for
11429 any status. */
11430 if (tp->number_on_target == 0)
11431 continue;
bba74b36
YQ
11432 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11433 phex_nz (loc->address, 0));
f196051f
SS
11434 putpkt (rs->buf);
11435 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11436 if (reply && *reply)
11437 {
11438 if (*reply == 'V')
11439 parse_tracepoint_status (reply + 1, bp, utp);
11440 }
11441 }
11442 }
11443 else if (utp)
11444 {
11445 utp->hit_count = 0;
11446 utp->traceframe_usage = 0;
bba74b36
YQ
11447 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11448 phex_nz (utp->addr, 0));
f196051f
SS
11449 putpkt (rs->buf);
11450 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11451 if (reply && *reply)
11452 {
11453 if (*reply == 'V')
11454 parse_tracepoint_status (reply + 1, bp, utp);
11455 }
11456 }
11457}
11458
35b1e5cc 11459static void
74499f1b 11460remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11461{
11462 putpkt ("QTStop");
11463 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11464 if (*target_buf == '\0')
11465 error (_("Target does not support this command."));
11466 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11467 error (_("Bogus reply from target: %s"), target_buf);
11468}
11469
11470static int
bd4c6793
TT
11471remote_trace_find (struct target_ops *self,
11472 enum trace_find_type type, int num,
cc5925ad 11473 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11474 int *tpp)
11475{
11476 struct remote_state *rs = get_remote_state ();
bba74b36 11477 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11478 char *p, *reply;
11479 int target_frameno = -1, target_tracept = -1;
11480
e6e4e701
PA
11481 /* Lookups other than by absolute frame number depend on the current
11482 trace selected, so make sure it is correct on the remote end
11483 first. */
11484 if (type != tfind_number)
11485 set_remote_traceframe ();
11486
35b1e5cc
SS
11487 p = rs->buf;
11488 strcpy (p, "QTFrame:");
11489 p = strchr (p, '\0');
11490 switch (type)
11491 {
11492 case tfind_number:
bba74b36 11493 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11494 break;
11495 case tfind_pc:
bba74b36 11496 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11497 break;
11498 case tfind_tp:
bba74b36 11499 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11500 break;
11501 case tfind_range:
bba74b36
YQ
11502 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11503 phex_nz (addr2, 0));
35b1e5cc
SS
11504 break;
11505 case tfind_outside:
bba74b36
YQ
11506 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11507 phex_nz (addr2, 0));
35b1e5cc
SS
11508 break;
11509 default:
9b20d036 11510 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11511 }
11512
11513 putpkt (rs->buf);
2f65bcb7 11514 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11515 if (*reply == '\0')
11516 error (_("Target does not support this command."));
35b1e5cc
SS
11517
11518 while (reply && *reply)
11519 switch (*reply)
11520 {
11521 case 'F':
f197e0f1
VP
11522 p = ++reply;
11523 target_frameno = (int) strtol (p, &reply, 16);
11524 if (reply == p)
11525 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11526 /* Don't update our remote traceframe number cache on failure
11527 to select a remote traceframe. */
f197e0f1
VP
11528 if (target_frameno == -1)
11529 return -1;
35b1e5cc
SS
11530 break;
11531 case 'T':
f197e0f1
VP
11532 p = ++reply;
11533 target_tracept = (int) strtol (p, &reply, 16);
11534 if (reply == p)
11535 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11536 break;
11537 case 'O': /* "OK"? */
11538 if (reply[1] == 'K' && reply[2] == '\0')
11539 reply += 2;
11540 else
11541 error (_("Bogus reply from target: %s"), reply);
11542 break;
11543 default:
11544 error (_("Bogus reply from target: %s"), reply);
11545 }
11546 if (tpp)
11547 *tpp = target_tracept;
e6e4e701 11548
262e1174 11549 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11550 return target_frameno;
11551}
11552
11553static int
4011015b
TT
11554remote_get_trace_state_variable_value (struct target_ops *self,
11555 int tsvnum, LONGEST *val)
35b1e5cc
SS
11556{
11557 struct remote_state *rs = get_remote_state ();
11558 char *reply;
11559 ULONGEST uval;
11560
e6e4e701
PA
11561 set_remote_traceframe ();
11562
bba74b36 11563 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11564 putpkt (rs->buf);
11565 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11566 if (reply && *reply)
11567 {
11568 if (*reply == 'V')
11569 {
11570 unpack_varlen_hex (reply + 1, &uval);
11571 *val = (LONGEST) uval;
11572 return 1;
11573 }
11574 }
11575 return 0;
11576}
11577
00bf0b85 11578static int
dc3decaf 11579remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11580{
11581 struct remote_state *rs = get_remote_state ();
11582 char *p, *reply;
11583
11584 p = rs->buf;
11585 strcpy (p, "QTSave:");
11586 p += strlen (p);
11587 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11588 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11589 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11590 *p++ = '\0';
11591 putpkt (rs->buf);
ad91cd99 11592 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11593 if (*reply == '\0')
ad91cd99
PA
11594 error (_("Target does not support this command."));
11595 if (strcmp (reply, "OK") != 0)
11596 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11597 return 0;
11598}
11599
11600/* This is basically a memory transfer, but needs to be its own packet
11601 because we don't know how the target actually organizes its trace
11602 memory, plus we want to be able to ask for as much as possible, but
11603 not be unhappy if we don't get as much as we ask for. */
11604
11605static LONGEST
88ee6f45
TT
11606remote_get_raw_trace_data (struct target_ops *self,
11607 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11608{
11609 struct remote_state *rs = get_remote_state ();
11610 char *reply;
11611 char *p;
11612 int rslt;
11613
11614 p = rs->buf;
11615 strcpy (p, "qTBuffer:");
11616 p += strlen (p);
11617 p += hexnumstr (p, offset);
11618 *p++ = ',';
11619 p += hexnumstr (p, len);
11620 *p++ = '\0';
11621
11622 putpkt (rs->buf);
11623 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11624 if (reply && *reply)
11625 {
11626 /* 'l' by itself means we're at the end of the buffer and
11627 there is nothing more to get. */
11628 if (*reply == 'l')
11629 return 0;
11630
11631 /* Convert the reply into binary. Limit the number of bytes to
11632 convert according to our passed-in buffer size, rather than
11633 what was returned in the packet; if the target is
11634 unexpectedly generous and gives us a bigger reply than we
11635 asked for, we don't want to crash. */
11636 rslt = hex2bin (target_buf, buf, len);
11637 return rslt;
11638 }
11639
11640 /* Something went wrong, flag as an error. */
11641 return -1;
11642}
11643
35b1e5cc 11644static void
37b25738 11645remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11646{
11647 struct remote_state *rs = get_remote_state ();
11648
4082afcc 11649 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11650 {
ad91cd99
PA
11651 char *reply;
11652
bba74b36 11653 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11654 putpkt (rs->buf);
ad91cd99
PA
11655 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11656 if (*reply == '\0')
33da3f1c 11657 error (_("Target does not support this command."));
ad91cd99
PA
11658 if (strcmp (reply, "OK") != 0)
11659 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11660 }
11661 else if (val)
11662 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11663}
11664
dc146f7c
VP
11665static int
11666remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11667{
11668 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11669
fe978cb0
PA
11670 if (info && info->priv)
11671 return info->priv->core;
dc146f7c
VP
11672 return -1;
11673}
11674
4daf5ac0 11675static void
736d5b1f 11676remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11677{
11678 struct remote_state *rs = get_remote_state ();
ad91cd99 11679 char *reply;
4daf5ac0 11680
bba74b36 11681 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11682 putpkt (rs->buf);
ad91cd99
PA
11683 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11684 if (*reply == '\0')
4daf5ac0 11685 error (_("Target does not support this command."));
ad91cd99
PA
11686 if (strcmp (reply, "OK") != 0)
11687 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11688}
11689
b3b9301e 11690static struct traceframe_info *
a893e81f 11691remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11692{
11693 char *text;
11694
11695 text = target_read_stralloc (&current_target,
11696 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11697 if (text != NULL)
11698 {
11699 struct traceframe_info *info;
11700 struct cleanup *back_to = make_cleanup (xfree, text);
11701
11702 info = parse_traceframe_info (text);
11703 do_cleanups (back_to);
11704 return info;
11705 }
11706
11707 return NULL;
11708}
11709
405f8e94
SS
11710/* Handle the qTMinFTPILen packet. Returns the minimum length of
11711 instruction on which a fast tracepoint may be placed. Returns -1
11712 if the packet is not supported, and 0 if the minimum instruction
11713 length is unknown. */
11714
11715static int
0e67620a 11716remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11717{
11718 struct remote_state *rs = get_remote_state ();
11719 char *reply;
11720
e886a173
PA
11721 /* If we're not debugging a process yet, the IPA can't be
11722 loaded. */
11723 if (!target_has_execution)
11724 return 0;
11725
11726 /* Make sure the remote is pointing at the right process. */
11727 set_general_process ();
11728
bba74b36 11729 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11730 putpkt (rs->buf);
11731 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11732 if (*reply == '\0')
11733 return -1;
11734 else
11735 {
11736 ULONGEST min_insn_len;
11737
11738 unpack_varlen_hex (reply, &min_insn_len);
11739
11740 return (int) min_insn_len;
11741 }
11742}
11743
f6f899bf 11744static void
4da384be 11745remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11746{
4082afcc 11747 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11748 {
11749 struct remote_state *rs = get_remote_state ();
11750 char *buf = rs->buf;
11751 char *endbuf = rs->buf + get_remote_packet_size ();
11752 enum packet_result result;
11753
11754 gdb_assert (val >= 0 || val == -1);
11755 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11756 /* Send -1 as literal "-1" to avoid host size dependency. */
11757 if (val < 0)
11758 {
11759 *buf++ = '-';
11760 buf += hexnumstr (buf, (ULONGEST) -val);
11761 }
11762 else
11763 buf += hexnumstr (buf, (ULONGEST) val);
11764
11765 putpkt (rs->buf);
11766 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11767 result = packet_ok (rs->buf,
11768 &remote_protocol_packets[PACKET_QTBuffer_size]);
11769
11770 if (result != PACKET_OK)
11771 warning (_("Bogus reply from target: %s"), rs->buf);
11772 }
11773}
11774
f196051f 11775static int
d9e68a2c
TT
11776remote_set_trace_notes (struct target_ops *self,
11777 const char *user, const char *notes,
ca623f82 11778 const char *stop_notes)
f196051f
SS
11779{
11780 struct remote_state *rs = get_remote_state ();
11781 char *reply;
11782 char *buf = rs->buf;
11783 char *endbuf = rs->buf + get_remote_packet_size ();
11784 int nbytes;
11785
11786 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11787 if (user)
11788 {
11789 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11790 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11791 buf += 2 * nbytes;
11792 *buf++ = ';';
11793 }
11794 if (notes)
11795 {
11796 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11797 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11798 buf += 2 * nbytes;
11799 *buf++ = ';';
11800 }
11801 if (stop_notes)
11802 {
11803 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11804 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11805 buf += 2 * nbytes;
11806 *buf++ = ';';
11807 }
11808 /* Ensure the buffer is terminated. */
11809 *buf = '\0';
11810
11811 putpkt (rs->buf);
11812 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11813 if (*reply == '\0')
11814 return 0;
11815
11816 if (strcmp (reply, "OK") != 0)
11817 error (_("Bogus reply from target: %s"), reply);
11818
11819 return 1;
11820}
11821
d1feda86 11822static int
2c152180 11823remote_use_agent (struct target_ops *self, int use)
d1feda86 11824{
4082afcc 11825 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11826 {
11827 struct remote_state *rs = get_remote_state ();
11828
11829 /* If the stub supports QAgent. */
bba74b36 11830 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11831 putpkt (rs->buf);
11832 getpkt (&rs->buf, &rs->buf_size, 0);
11833
11834 if (strcmp (rs->buf, "OK") == 0)
11835 {
11836 use_agent = use;
11837 return 1;
11838 }
11839 }
11840
11841 return 0;
11842}
11843
11844static int
fe38f897 11845remote_can_use_agent (struct target_ops *self)
d1feda86 11846{
4082afcc 11847 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11848}
11849
9accd112
MM
11850struct btrace_target_info
11851{
11852 /* The ptid of the traced thread. */
11853 ptid_t ptid;
f4abbc16
MM
11854
11855 /* The obtained branch trace configuration. */
11856 struct btrace_config conf;
9accd112
MM
11857};
11858
f4abbc16
MM
11859/* Reset our idea of our target's btrace configuration. */
11860
11861static void
11862remote_btrace_reset (void)
11863{
11864 struct remote_state *rs = get_remote_state ();
11865
11866 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11867}
11868
9accd112
MM
11869/* Check whether the target supports branch tracing. */
11870
11871static int
043c3577 11872remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11873{
4082afcc 11874 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11875 return 0;
4082afcc 11876 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11877 return 0;
11878
043c3577
MM
11879 switch (format)
11880 {
11881 case BTRACE_FORMAT_NONE:
11882 return 0;
11883
11884 case BTRACE_FORMAT_BTS:
11885 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
11886 }
11887
11888 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11889}
11890
f4abbc16
MM
11891/* Synchronize the configuration with the target. */
11892
11893static void
11894btrace_sync_conf (const struct btrace_config *conf)
11895{
d33501a5
MM
11896 struct packet_config *packet;
11897 struct remote_state *rs;
11898 char *buf, *pos, *endbuf;
11899
11900 rs = get_remote_state ();
11901 buf = rs->buf;
11902 endbuf = buf + get_remote_packet_size ();
11903
11904 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11905 if (packet_config_support (packet) == PACKET_ENABLE
11906 && conf->bts.size != rs->btrace_config.bts.size)
11907 {
11908 pos = buf;
11909 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11910 conf->bts.size);
11911
11912 putpkt (buf);
11913 getpkt (&buf, &rs->buf_size, 0);
11914
11915 if (packet_ok (buf, packet) == PACKET_ERROR)
11916 {
11917 if (buf[0] == 'E' && buf[1] == '.')
11918 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11919 else
11920 error (_("Failed to configure the BTS buffer size."));
11921 }
11922
11923 rs->btrace_config.bts.size = conf->bts.size;
11924 }
f4abbc16
MM
11925}
11926
11927/* Read the current thread's btrace configuration from the target and
11928 store it into CONF. */
11929
11930static void
11931btrace_read_config (struct btrace_config *conf)
11932{
11933 char *xml;
11934
11935 xml = target_read_stralloc (&current_target,
11936 TARGET_OBJECT_BTRACE_CONF, "");
11937 if (xml != NULL)
11938 {
11939 struct cleanup *cleanup;
11940
11941 cleanup = make_cleanup (xfree, xml);
11942 parse_xml_btrace_conf (conf, xml);
11943 do_cleanups (cleanup);
11944 }
11945}
11946
9accd112
MM
11947/* Enable branch tracing. */
11948
11949static struct btrace_target_info *
f4abbc16
MM
11950remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11951 const struct btrace_config *conf)
9accd112
MM
11952{
11953 struct btrace_target_info *tinfo = NULL;
11954 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11955 struct remote_state *rs = get_remote_state ();
11956 char *buf = rs->buf;
11957 char *endbuf = rs->buf + get_remote_packet_size ();
11958
4082afcc 11959 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11960 error (_("Target does not support branch tracing."));
11961
f4abbc16
MM
11962 btrace_sync_conf (conf);
11963
9accd112
MM
11964 set_general_thread (ptid);
11965
11966 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11967 putpkt (rs->buf);
11968 getpkt (&rs->buf, &rs->buf_size, 0);
11969
11970 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11971 {
11972 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11973 error (_("Could not enable branch tracing for %s: %s"),
11974 target_pid_to_str (ptid), rs->buf + 2);
11975 else
11976 error (_("Could not enable branch tracing for %s."),
11977 target_pid_to_str (ptid));
11978 }
11979
11980 tinfo = xzalloc (sizeof (*tinfo));
11981 tinfo->ptid = ptid;
11982
f4abbc16
MM
11983 /* If we fail to read the configuration, we lose some information, but the
11984 tracing itself is not impacted. */
492d29ea
PA
11985 TRY
11986 {
11987 btrace_read_config (&tinfo->conf);
11988 }
11989 CATCH (err, RETURN_MASK_ERROR)
11990 {
11991 if (err.message != NULL)
11992 warning ("%s", err.message);
11993 }
11994 END_CATCH
f4abbc16 11995
9accd112
MM
11996 return tinfo;
11997}
11998
11999/* Disable branch tracing. */
12000
12001static void
25e95349
TT
12002remote_disable_btrace (struct target_ops *self,
12003 struct btrace_target_info *tinfo)
9accd112
MM
12004{
12005 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12006 struct remote_state *rs = get_remote_state ();
12007 char *buf = rs->buf;
12008 char *endbuf = rs->buf + get_remote_packet_size ();
12009
4082afcc 12010 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12011 error (_("Target does not support branch tracing."));
12012
12013 set_general_thread (tinfo->ptid);
12014
12015 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12016 putpkt (rs->buf);
12017 getpkt (&rs->buf, &rs->buf_size, 0);
12018
12019 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12020 {
12021 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12022 error (_("Could not disable branch tracing for %s: %s"),
12023 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12024 else
12025 error (_("Could not disable branch tracing for %s."),
12026 target_pid_to_str (tinfo->ptid));
12027 }
12028
12029 xfree (tinfo);
12030}
12031
12032/* Teardown branch tracing. */
12033
12034static void
1777056d
TT
12035remote_teardown_btrace (struct target_ops *self,
12036 struct btrace_target_info *tinfo)
9accd112
MM
12037{
12038 /* We must not talk to the target during teardown. */
12039 xfree (tinfo);
12040}
12041
12042/* Read the branch trace. */
12043
969c39fb 12044static enum btrace_error
39c49f83 12045remote_read_btrace (struct target_ops *self,
734b0e4b 12046 struct btrace_data *btrace,
969c39fb 12047 struct btrace_target_info *tinfo,
9accd112
MM
12048 enum btrace_read_type type)
12049{
12050 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12051 struct remote_state *rs = get_remote_state ();
969c39fb 12052 struct cleanup *cleanup;
9accd112
MM
12053 const char *annex;
12054 char *xml;
12055
4082afcc 12056 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12057 error (_("Target does not support branch tracing."));
12058
12059#if !defined(HAVE_LIBEXPAT)
12060 error (_("Cannot process branch tracing result. XML parsing not supported."));
12061#endif
12062
12063 switch (type)
12064 {
864089d2 12065 case BTRACE_READ_ALL:
9accd112
MM
12066 annex = "all";
12067 break;
864089d2 12068 case BTRACE_READ_NEW:
9accd112
MM
12069 annex = "new";
12070 break;
969c39fb
MM
12071 case BTRACE_READ_DELTA:
12072 annex = "delta";
12073 break;
9accd112
MM
12074 default:
12075 internal_error (__FILE__, __LINE__,
12076 _("Bad branch tracing read type: %u."),
12077 (unsigned int) type);
12078 }
12079
12080 xml = target_read_stralloc (&current_target,
12081 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
12082 if (xml == NULL)
12083 return BTRACE_ERR_UNKNOWN;
9accd112 12084
969c39fb 12085 cleanup = make_cleanup (xfree, xml);
734b0e4b 12086 parse_xml_btrace (btrace, xml);
969c39fb 12087 do_cleanups (cleanup);
9accd112 12088
969c39fb 12089 return BTRACE_ERR_NONE;
9accd112
MM
12090}
12091
f4abbc16
MM
12092static const struct btrace_config *
12093remote_btrace_conf (struct target_ops *self,
12094 const struct btrace_target_info *tinfo)
12095{
12096 return &tinfo->conf;
12097}
12098
ced63ec0 12099static int
5436ff03 12100remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12101{
4082afcc
PA
12102 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12103 == PACKET_ENABLE);
ced63ec0
GB
12104}
12105
9dd130a0
TT
12106/* Implementation of to_load. */
12107
12108static void
9cbe5fff 12109remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12110{
12111 generic_load (name, from_tty);
12112}
12113
c78fa86a
GB
12114/* Accepts an integer PID; returns a string representing a file that
12115 can be opened on the remote side to get the symbols for the child
12116 process. Returns NULL if the operation is not supported. */
12117
12118static char *
12119remote_pid_to_exec_file (struct target_ops *self, int pid)
12120{
12121 static char *filename = NULL;
835205d0
GB
12122 struct inferior *inf;
12123 char *annex = NULL;
c78fa86a
GB
12124
12125 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12126 return NULL;
12127
12128 if (filename != NULL)
12129 xfree (filename);
12130
835205d0
GB
12131 inf = find_inferior_pid (pid);
12132 if (inf == NULL)
12133 internal_error (__FILE__, __LINE__,
12134 _("not currently attached to process %d"), pid);
12135
12136 if (!inf->fake_pid_p)
12137 {
12138 const int annex_size = 9;
12139
12140 annex = alloca (annex_size);
12141 xsnprintf (annex, annex_size, "%x", pid);
12142 }
12143
c78fa86a
GB
12144 filename = target_read_stralloc (&current_target,
12145 TARGET_OBJECT_EXEC_FILE, annex);
12146
12147 return filename;
12148}
12149
c906108c 12150static void
fba45db2 12151init_remote_ops (void)
c906108c 12152{
c5aa993b 12153 remote_ops.to_shortname = "remote";
c906108c 12154 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12155 remote_ops.to_doc =
c906108c 12156 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12157Specify the serial device it is connected to\n\
12158(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12159 remote_ops.to_open = remote_open;
12160 remote_ops.to_close = remote_close;
c906108c 12161 remote_ops.to_detach = remote_detach;
6ad8ae5c 12162 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12163 remote_ops.to_resume = remote_resume;
c906108c
SS
12164 remote_ops.to_wait = remote_wait;
12165 remote_ops.to_fetch_registers = remote_fetch_registers;
12166 remote_ops.to_store_registers = remote_store_registers;
12167 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12168 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12169 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12170 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12171 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12172 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12173 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12174 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12175 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12176 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12177 remote_ops.to_watchpoint_addr_within_range =
12178 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12179 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12180 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12181 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12182 remote_ops.to_region_ok_for_hw_watchpoint
12183 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12184 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12185 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12186 remote_ops.to_kill = remote_kill;
9dd130a0 12187 remote_ops.to_load = remote_load;
c906108c 12188 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12189 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12190 remote_ops.to_program_signals = remote_program_signals;
c906108c 12191 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12192 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12193 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12194 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12195 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12196 remote_ops.to_stop = remote_stop;
4b8a223f 12197 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12198 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12199 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12200 remote_ops.to_log_command = serial_log_command;
38691318 12201 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12202 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12203 remote_ops.to_has_all_memory = default_child_has_all_memory;
12204 remote_ops.to_has_memory = default_child_has_memory;
12205 remote_ops.to_has_stack = default_child_has_stack;
12206 remote_ops.to_has_registers = default_child_has_registers;
12207 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12208 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12209 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12210 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12211 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12212 remote_ops.to_flash_erase = remote_flash_erase;
12213 remote_ops.to_flash_done = remote_flash_done;
29709017 12214 remote_ops.to_read_description = remote_read_description;
08388c79 12215 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12216 remote_ops.to_can_async_p = remote_can_async_p;
12217 remote_ops.to_is_async_p = remote_is_async_p;
12218 remote_ops.to_async = remote_async;
75c99385
PA
12219 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12220 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12221 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12222 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12223 remote_ops.to_supports_disable_randomization
12224 = remote_supports_disable_randomization;
4bd7dc42 12225 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12226 remote_ops.to_fileio_open = remote_hostio_open;
12227 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12228 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12229 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12230 remote_ops.to_fileio_close = remote_hostio_close;
12231 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12232 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12233 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12234 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12235 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12236 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12237 remote_ops.to_trace_init = remote_trace_init;
12238 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12239 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12240 remote_ops.to_download_trace_state_variable
12241 = remote_download_trace_state_variable;
d248b706
KY
12242 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12243 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12244 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12245 remote_ops.to_trace_start = remote_trace_start;
12246 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12247 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12248 remote_ops.to_trace_stop = remote_trace_stop;
12249 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12250 remote_ops.to_get_trace_state_variable_value
12251 = remote_get_trace_state_variable_value;
00bf0b85
SS
12252 remote_ops.to_save_trace_data = remote_save_trace_data;
12253 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12254 remote_ops.to_upload_trace_state_variables
12255 = remote_upload_trace_state_variables;
00bf0b85 12256 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12257 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12258 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12259 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12260 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12261 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12262 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12263 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12264 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12265 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12266 remote_ops.to_static_tracepoint_marker_at
12267 = remote_static_tracepoint_marker_at;
12268 remote_ops.to_static_tracepoint_markers_by_strid
12269 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12270 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12271 remote_ops.to_use_agent = remote_use_agent;
12272 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12273 remote_ops.to_supports_btrace = remote_supports_btrace;
12274 remote_ops.to_enable_btrace = remote_enable_btrace;
12275 remote_ops.to_disable_btrace = remote_disable_btrace;
12276 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12277 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12278 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12279 remote_ops.to_augmented_libraries_svr4_read =
12280 remote_augmented_libraries_svr4_read;
c906108c
SS
12281}
12282
12283/* Set up the extended remote vector by making a copy of the standard
12284 remote vector and adding to it. */
12285
12286static void
fba45db2 12287init_extended_remote_ops (void)
c906108c
SS
12288{
12289 extended_remote_ops = remote_ops;
12290
0f71a2f6 12291 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12292 extended_remote_ops.to_longname =
c906108c 12293 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12294 extended_remote_ops.to_doc =
c906108c 12295 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12296Specify the serial device it is connected to (e.g. /dev/ttya).";
12297 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12298 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12299 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12300 extended_remote_ops.to_detach = extended_remote_detach;
12301 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12302 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12303 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12304 extended_remote_ops.to_supports_disable_randomization
12305 = extended_remote_supports_disable_randomization;
de0d863e 12306 extended_remote_ops.to_follow_fork = remote_follow_fork;
cbb8991c
DB
12307 extended_remote_ops.to_insert_fork_catchpoint
12308 = remote_insert_fork_catchpoint;
12309 extended_remote_ops.to_remove_fork_catchpoint
12310 = remote_remove_fork_catchpoint;
12311 extended_remote_ops.to_insert_vfork_catchpoint
12312 = remote_insert_vfork_catchpoint;
12313 extended_remote_ops.to_remove_vfork_catchpoint
12314 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12315}
12316
6426a772 12317static int
6a109b6b 12318remote_can_async_p (struct target_ops *ops)
6426a772 12319{
5d93a237
TT
12320 struct remote_state *rs = get_remote_state ();
12321
c6ebd6cf 12322 if (!target_async_permitted)
75c99385
PA
12323 /* We only enable async when the user specifically asks for it. */
12324 return 0;
12325
23860348 12326 /* We're async whenever the serial device is. */
5d93a237 12327 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12328}
12329
12330static int
6a109b6b 12331remote_is_async_p (struct target_ops *ops)
6426a772 12332{
5d93a237
TT
12333 struct remote_state *rs = get_remote_state ();
12334
c6ebd6cf 12335 if (!target_async_permitted)
75c99385
PA
12336 /* We only enable async when the user specifically asks for it. */
12337 return 0;
12338
23860348 12339 /* We're async whenever the serial device is. */
5d93a237 12340 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12341}
12342
2acceee2
JM
12343/* Pass the SERIAL event on and up to the client. One day this code
12344 will be able to delay notifying the client of an event until the
23860348 12345 point where an entire packet has been received. */
2acceee2 12346
2acceee2
JM
12347static serial_event_ftype remote_async_serial_handler;
12348
6426a772 12349static void
819cc324 12350remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12351{
88b496c3
TT
12352 struct remote_state *rs = context;
12353
2acceee2
JM
12354 /* Don't propogate error information up to the client. Instead let
12355 the client find out about the error by querying the target. */
6a3753b3 12356 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12357}
12358
74531fed
PA
12359static void
12360remote_async_inferior_event_handler (gdb_client_data data)
12361{
12362 inferior_event_handler (INF_REG_EVENT, NULL);
12363}
12364
2acceee2 12365static void
6a3753b3 12366remote_async (struct target_ops *ops, int enable)
2acceee2 12367{
5d93a237
TT
12368 struct remote_state *rs = get_remote_state ();
12369
6a3753b3 12370 if (enable)
2acceee2 12371 {
88b496c3 12372 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12373
12374 /* If there are pending events in the stop reply queue tell the
12375 event loop to process them. */
12376 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12377 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12378 }
12379 else
b7d2e916
PA
12380 {
12381 serial_async (rs->remote_desc, NULL, NULL);
12382 clear_async_event_handler (remote_async_inferior_event_token);
12383 }
6426a772
JM
12384}
12385
5a2468f5 12386static void
c2d11a7d 12387set_remote_cmd (char *args, int from_tty)
5a2468f5 12388{
635c7e8a 12389 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12390}
12391
d471ea57
AC
12392static void
12393show_remote_cmd (char *args, int from_tty)
12394{
37a105a1 12395 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12396 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12397 struct cleanup *showlist_chain;
12398 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12399 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12400
12401 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12402 for (; list != NULL; list = list->next)
12403 if (strcmp (list->name, "Z-packet") == 0)
12404 continue;
427c3a89
DJ
12405 else if (list->type == not_set_cmd)
12406 /* Alias commands are exactly like the original, except they
12407 don't have the normal type. */
12408 continue;
12409 else
37a105a1
DJ
12410 {
12411 struct cleanup *option_chain
12412 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12413
37a105a1
DJ
12414 ui_out_field_string (uiout, "name", list->name);
12415 ui_out_text (uiout, ": ");
427c3a89 12416 if (list->type == show_cmd)
5b9afe8a 12417 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12418 else
12419 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12420 /* Close the tuple. */
12421 do_cleanups (option_chain);
12422 }
427c3a89
DJ
12423
12424 /* Close the tuple. */
12425 do_cleanups (showlist_chain);
d471ea57 12426}
5a2468f5 12427
0f71a2f6 12428
23860348 12429/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12430static void
12431remote_new_objfile (struct objfile *objfile)
12432{
5d93a237
TT
12433 struct remote_state *rs = get_remote_state ();
12434
12435 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12436 remote_check_symbols ();
dc8acb97
MS
12437}
12438
00bf0b85
SS
12439/* Pull all the tracepoints defined on the target and create local
12440 data structures representing them. We don't want to create real
12441 tracepoints yet, we don't want to mess up the user's existing
12442 collection. */
12443
12444static int
ab6617cc 12445remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12446{
00bf0b85
SS
12447 struct remote_state *rs = get_remote_state ();
12448 char *p;
d5551862 12449
00bf0b85
SS
12450 /* Ask for a first packet of tracepoint definition. */
12451 putpkt ("qTfP");
12452 getpkt (&rs->buf, &rs->buf_size, 0);
12453 p = rs->buf;
12454 while (*p && *p != 'l')
d5551862 12455 {
00bf0b85
SS
12456 parse_tracepoint_definition (p, utpp);
12457 /* Ask for another packet of tracepoint definition. */
12458 putpkt ("qTsP");
12459 getpkt (&rs->buf, &rs->buf_size, 0);
12460 p = rs->buf;
d5551862 12461 }
00bf0b85 12462 return 0;
d5551862
SS
12463}
12464
00bf0b85 12465static int
181e3713
TT
12466remote_upload_trace_state_variables (struct target_ops *self,
12467 struct uploaded_tsv **utsvp)
d5551862 12468{
00bf0b85 12469 struct remote_state *rs = get_remote_state ();
d5551862 12470 char *p;
d5551862 12471
00bf0b85
SS
12472 /* Ask for a first packet of variable definition. */
12473 putpkt ("qTfV");
d5551862
SS
12474 getpkt (&rs->buf, &rs->buf_size, 0);
12475 p = rs->buf;
00bf0b85 12476 while (*p && *p != 'l')
d5551862 12477 {
00bf0b85
SS
12478 parse_tsv_definition (p, utsvp);
12479 /* Ask for another packet of variable definition. */
12480 putpkt ("qTsV");
d5551862
SS
12481 getpkt (&rs->buf, &rs->buf_size, 0);
12482 p = rs->buf;
12483 }
00bf0b85 12484 return 0;
d5551862
SS
12485}
12486
c1e36e3e
PA
12487/* The "set/show range-stepping" show hook. */
12488
12489static void
12490show_range_stepping (struct ui_file *file, int from_tty,
12491 struct cmd_list_element *c,
12492 const char *value)
12493{
12494 fprintf_filtered (file,
12495 _("Debugger's willingness to use range stepping "
12496 "is %s.\n"), value);
12497}
12498
12499/* The "set/show range-stepping" set hook. */
12500
12501static void
12502set_range_stepping (char *ignore_args, int from_tty,
12503 struct cmd_list_element *c)
12504{
5d93a237
TT
12505 struct remote_state *rs = get_remote_state ();
12506
c1e36e3e
PA
12507 /* Whene enabling, check whether range stepping is actually
12508 supported by the target, and warn if not. */
12509 if (use_range_stepping)
12510 {
5d93a237 12511 if (rs->remote_desc != NULL)
c1e36e3e 12512 {
4082afcc 12513 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12514 remote_vcont_probe (rs);
12515
4082afcc 12516 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12517 && rs->supports_vCont.r)
12518 return;
12519 }
12520
12521 warning (_("Range stepping is not supported by the current target"));
12522 }
12523}
12524
c906108c 12525void
fba45db2 12526_initialize_remote (void)
c906108c 12527{
ea9c271d 12528 struct remote_state *rs;
9a7071a8 12529 struct cmd_list_element *cmd;
6f937416 12530 const char *cmd_name;
ea9c271d 12531
0f71a2f6 12532 /* architecture specific data */
2bc416ba 12533 remote_gdbarch_data_handle =
23860348 12534 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12535 remote_g_packet_data_handle =
12536 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12537
ea9c271d
DJ
12538 /* Initialize the per-target state. At the moment there is only one
12539 of these, not one per target. Only one target is active at a
cf792862
TT
12540 time. */
12541 remote_state = new_remote_state ();
ea9c271d 12542
c906108c
SS
12543 init_remote_ops ();
12544 add_target (&remote_ops);
12545
12546 init_extended_remote_ops ();
12547 add_target (&extended_remote_ops);
cce74817 12548
dc8acb97 12549 /* Hook into new objfile notification. */
06d3b283 12550 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12551 /* We're no longer interested in notification events of an inferior
12552 when it exits. */
12553 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12554
b803fb0f 12555 /* Set up signal handlers. */
934b9bac 12556 async_sigint_remote_token =
b803fb0f 12557 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12558 async_sigint_remote_twice_token =
6d549500 12559 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12560
c906108c
SS
12561#if 0
12562 init_remote_threadtests ();
12563#endif
12564
722247f1 12565 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12566 /* set/show remote ... */
d471ea57 12567
1bedd215 12568 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12569Remote protocol specific variables\n\
12570Configure various remote-protocol specific variables such as\n\
1bedd215 12571the packets being used"),
cff3e48b 12572 &remote_set_cmdlist, "set remote ",
23860348 12573 0 /* allow-unknown */, &setlist);
1bedd215 12574 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12575Remote protocol specific variables\n\
12576Configure various remote-protocol specific variables such as\n\
1bedd215 12577the packets being used"),
cff3e48b 12578 &remote_show_cmdlist, "show remote ",
23860348 12579 0 /* allow-unknown */, &showlist);
5a2468f5 12580
1a966eab
AC
12581 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12582Compare section data on target to the exec file.\n\
95cf3b38
DT
12583Argument is a single section name (default: all loaded sections).\n\
12584To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12585 &cmdlist);
12586
1a966eab
AC
12587 add_cmd ("packet", class_maintenance, packet_command, _("\
12588Send an arbitrary packet to a remote target.\n\
c906108c
SS
12589 maintenance packet TEXT\n\
12590If GDB is talking to an inferior via the GDB serial protocol, then\n\
12591this command sends the string TEXT to the inferior, and displays the\n\
12592response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12593terminating `#' character and checksum."),
c906108c
SS
12594 &maintenancelist);
12595
7915a72c
AC
12596 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12597Set whether to send break if interrupted."), _("\
12598Show whether to send break if interrupted."), _("\
12599If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12600 set_remotebreak, show_remotebreak,
e707bbc2 12601 &setlist, &showlist);
9a7071a8
JB
12602 cmd_name = "remotebreak";
12603 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12604 deprecate_cmd (cmd, "set remote interrupt-sequence");
12605 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12606 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12607 deprecate_cmd (cmd, "show remote interrupt-sequence");
12608
12609 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12610 interrupt_sequence_modes, &interrupt_sequence_mode,
12611 _("\
9a7071a8
JB
12612Set interrupt sequence to remote target."), _("\
12613Show interrupt sequence to remote target."), _("\
12614Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12615 NULL, show_interrupt_sequence,
12616 &remote_set_cmdlist,
12617 &remote_show_cmdlist);
12618
12619 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12620 &interrupt_on_connect, _("\
12621Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12622Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12623If set, interrupt sequence is sent to remote target."),
12624 NULL, NULL,
12625 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12626
23860348 12627 /* Install commands for configuring memory read/write packets. */
11cf8741 12628
1a966eab
AC
12629 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12630Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12631 &setlist);
1a966eab
AC
12632 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12633Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12634 &showlist);
12635 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12636 set_memory_write_packet_size, _("\
12637Set the maximum number of bytes per memory-write packet.\n\
12638Specify the number of bytes in a packet or 0 (zero) for the\n\
12639default packet size. The actual limit is further reduced\n\
12640dependent on the target. Specify ``fixed'' to disable the\n\
12641further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12642 &remote_set_cmdlist);
12643 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12644 set_memory_read_packet_size, _("\
12645Set the maximum number of bytes per memory-read packet.\n\
12646Specify the number of bytes in a packet or 0 (zero) for the\n\
12647default packet size. The actual limit is further reduced\n\
12648dependent on the target. Specify ``fixed'' to disable the\n\
12649further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12650 &remote_set_cmdlist);
12651 add_cmd ("memory-write-packet-size", no_class,
12652 show_memory_write_packet_size,
1a966eab 12653 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12654 &remote_show_cmdlist);
12655 add_cmd ("memory-read-packet-size", no_class,
12656 show_memory_read_packet_size,
1a966eab 12657 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12658 &remote_show_cmdlist);
c906108c 12659
b3f42336 12660 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12661 &remote_hw_watchpoint_limit, _("\
12662Set the maximum number of target hardware watchpoints."), _("\
12663Show the maximum number of target hardware watchpoints."), _("\
12664Specify a negative limit for unlimited."),
3e43a32a
MS
12665 NULL, NULL, /* FIXME: i18n: The maximum
12666 number of target hardware
12667 watchpoints is %s. */
b3f42336 12668 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12669 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12670 &remote_hw_watchpoint_length_limit, _("\
12671Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12672Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12673Specify a negative limit for unlimited."),
12674 NULL, NULL, /* FIXME: i18n: The maximum
12675 length (in bytes) of a target
12676 hardware watchpoint is %s. */
12677 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12678 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12679 &remote_hw_breakpoint_limit, _("\
12680Set the maximum number of target hardware breakpoints."), _("\
12681Show the maximum number of target hardware breakpoints."), _("\
12682Specify a negative limit for unlimited."),
3e43a32a
MS
12683 NULL, NULL, /* FIXME: i18n: The maximum
12684 number of target hardware
12685 breakpoints is %s. */
b3f42336 12686 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12687
1b493192
PA
12688 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12689 &remote_address_size, _("\
4d28ad1e
AC
12690Set the maximum size of the address (in bits) in a memory packet."), _("\
12691Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12692 NULL,
12693 NULL, /* FIXME: i18n: */
12694 &setlist, &showlist);
c906108c 12695
ca4f7f8b
PA
12696 init_all_packet_configs ();
12697
444abaca 12698 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12699 "X", "binary-download", 1);
0f71a2f6 12700
444abaca 12701 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12702 "vCont", "verbose-resume", 0);
506fb367 12703
89be2091
DJ
12704 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12705 "QPassSignals", "pass-signals", 0);
12706
9b224c5e
PA
12707 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12708 "QProgramSignals", "program-signals", 0);
12709
444abaca 12710 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12711 "qSymbol", "symbol-lookup", 0);
dc8acb97 12712
444abaca 12713 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12714 "P", "set-register", 1);
d471ea57 12715
444abaca 12716 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12717 "p", "fetch-register", 1);
b96ec7ac 12718
444abaca 12719 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12720 "Z0", "software-breakpoint", 0);
d471ea57 12721
444abaca 12722 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12723 "Z1", "hardware-breakpoint", 0);
d471ea57 12724
444abaca 12725 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12726 "Z2", "write-watchpoint", 0);
d471ea57 12727
444abaca 12728 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12729 "Z3", "read-watchpoint", 0);
d471ea57 12730
444abaca 12731 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12732 "Z4", "access-watchpoint", 0);
d471ea57 12733
0876f84a
DJ
12734 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12735 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12736
c78fa86a
GB
12737 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12738 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12739
23181151
DJ
12740 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12741 "qXfer:features:read", "target-features", 0);
12742
cfa9d6d9
DJ
12743 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12744 "qXfer:libraries:read", "library-info", 0);
12745
2268b414
JK
12746 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12747 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12748
fd79ecee
DJ
12749 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12750 "qXfer:memory-map:read", "memory-map", 0);
12751
0e7f50da
UW
12752 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12753 "qXfer:spu:read", "read-spu-object", 0);
12754
12755 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12756 "qXfer:spu:write", "write-spu-object", 0);
12757
07e059b5
VP
12758 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12759 "qXfer:osdata:read", "osdata", 0);
12760
dc146f7c
VP
12761 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12762 "qXfer:threads:read", "threads", 0);
12763
4aa995e1
PA
12764 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12765 "qXfer:siginfo:read", "read-siginfo-object", 0);
12766
12767 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12768 "qXfer:siginfo:write", "write-siginfo-object", 0);
12769
b3b9301e
PA
12770 add_packet_config_cmd
12771 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12772 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12773
169081d0
TG
12774 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12775 "qXfer:uib:read", "unwind-info-block", 0);
12776
444abaca 12777 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12778 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12779 0);
12780
711e434b
PM
12781 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12782 "qGetTIBAddr", "get-thread-information-block-address",
12783 0);
12784
40ab02ce
MS
12785 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12786 "bc", "reverse-continue", 0);
12787
12788 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12789 "bs", "reverse-step", 0);
12790
be2a5f71
DJ
12791 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12792 "qSupported", "supported-packets", 0);
12793
08388c79
DE
12794 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12795 "qSearch:memory", "search-memory", 0);
12796
bd3eecc3
PA
12797 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12798 "qTStatus", "trace-status", 0);
12799
15a201c8
GB
12800 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
12801 "vFile:setfs", "hostio-setfs", 0);
12802
a6b151f1
DJ
12803 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12804 "vFile:open", "hostio-open", 0);
12805
12806 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12807 "vFile:pread", "hostio-pread", 0);
12808
12809 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12810 "vFile:pwrite", "hostio-pwrite", 0);
12811
12812 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12813 "vFile:close", "hostio-close", 0);
12814
12815 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12816 "vFile:unlink", "hostio-unlink", 0);
12817
b9e7b9c3
UW
12818 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12819 "vFile:readlink", "hostio-readlink", 0);
12820
0a93529c
GB
12821 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12822 "vFile:fstat", "hostio-fstat", 0);
12823
2d717e4f
DJ
12824 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12825 "vAttach", "attach", 0);
12826
12827 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12828 "vRun", "run", 0);
12829
a6f3e723
SL
12830 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12831 "QStartNoAckMode", "noack", 0);
12832
82f73884
PA
12833 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12834 "vKill", "kill", 0);
12835
0b16c5cf
PA
12836 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12837 "qAttached", "query-attached", 0);
12838
782b2b07 12839 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12840 "ConditionalTracepoints",
12841 "conditional-tracepoints", 0);
3788aec7
LM
12842
12843 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12844 "ConditionalBreakpoints",
12845 "conditional-breakpoints", 0);
12846
d3ce09f5
SS
12847 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12848 "BreakpointCommands",
12849 "breakpoint-commands", 0);
12850
7a697b8d
SS
12851 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12852 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12853
409873ef
SS
12854 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12855 "TracepointSource", "TracepointSource", 0);
12856
d914c394
SS
12857 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12858 "QAllow", "allow", 0);
12859
0fb4aa4b
PA
12860 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12861 "StaticTracepoints", "static-tracepoints", 0);
12862
1e4d1764
YQ
12863 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12864 "InstallInTrace", "install-in-trace", 0);
12865
0fb4aa4b
PA
12866 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12867 "qXfer:statictrace:read", "read-sdata-object", 0);
12868
78d85199
YQ
12869 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12870 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12871
03583c20
UW
12872 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12873 "QDisableRandomization", "disable-randomization", 0);
12874
d1feda86
YQ
12875 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12876 "QAgent", "agent", 0);
12877
f6f899bf
HAQ
12878 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12879 "QTBuffer:size", "trace-buffer-size", 0);
12880
9accd112
MM
12881 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12882 "Qbtrace:off", "disable-btrace", 0);
12883
12884 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12885 "Qbtrace:bts", "enable-btrace", 0);
12886
12887 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12888 "qXfer:btrace", "read-btrace", 0);
12889
f4abbc16
MM
12890 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12891 "qXfer:btrace-conf", "read-btrace-conf", 0);
12892
d33501a5
MM
12893 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12894 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12895
f7e6eed5
PA
12896 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12897 "swbreak-feature", "swbreak-feature", 0);
12898
12899 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12900 "hwbreak-feature", "hwbreak-feature", 0);
12901
89245bc0
DB
12902 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12903 "fork-event-feature", "fork-event-feature", 0);
12904
12905 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12906 "vfork-event-feature", "vfork-event-feature", 0);
12907
0b736949
DB
12908 /* Assert that we've registered "set remote foo-packet" commands
12909 for all packet configs. */
ca4f7f8b
PA
12910 {
12911 int i;
12912
12913 for (i = 0; i < PACKET_MAX; i++)
12914 {
12915 /* Ideally all configs would have a command associated. Some
12916 still don't though. */
12917 int excepted;
12918
12919 switch (i)
12920 {
12921 case PACKET_QNonStop:
12922 case PACKET_multiprocess_feature:
12923 case PACKET_EnableDisableTracepoints_feature:
12924 case PACKET_tracenz_feature:
12925 case PACKET_DisconnectedTracing_feature:
12926 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
12927 case PACKET_qCRC:
12928 /* Additions to this list need to be well justified:
12929 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
12930 excepted = 1;
12931 break;
12932 default:
12933 excepted = 0;
12934 break;
12935 }
12936
12937 /* This catches both forgetting to add a config command, and
12938 forgetting to remove a packet from the exception list. */
12939 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12940 }
12941 }
12942
37a105a1
DJ
12943 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12944 Z sub-packet has its own set and show commands, but users may
12945 have sets to this variable in their .gdbinit files (or in their
12946 documentation). */
e9e68a56 12947 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
12948 &remote_Z_packet_detect, _("\
12949Set use of remote protocol `Z' packets"), _("\
12950Show use of remote protocol `Z' packets "), _("\
3b64bf98 12951When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 12952packets."),
e9e68a56 12953 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
12954 show_remote_protocol_Z_packet_cmd,
12955 /* FIXME: i18n: Use of remote protocol
12956 `Z' packets is %s. */
e9e68a56 12957 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 12958
a6b151f1
DJ
12959 add_prefix_cmd ("remote", class_files, remote_command, _("\
12960Manipulate files on the remote system\n\
12961Transfer files to and from the remote target system."),
12962 &remote_cmdlist, "remote ",
12963 0 /* allow-unknown */, &cmdlist);
12964
12965 add_cmd ("put", class_files, remote_put_command,
12966 _("Copy a local file to the remote system."),
12967 &remote_cmdlist);
12968
12969 add_cmd ("get", class_files, remote_get_command,
12970 _("Copy a remote file to the local system."),
12971 &remote_cmdlist);
12972
12973 add_cmd ("delete", class_files, remote_delete_command,
12974 _("Delete a remote file."),
12975 &remote_cmdlist);
12976
2d717e4f
DJ
12977 remote_exec_file = xstrdup ("");
12978 add_setshow_string_noescape_cmd ("exec-file", class_files,
12979 &remote_exec_file, _("\
12980Set the remote pathname for \"run\""), _("\
12981Show the remote pathname for \"run\""), NULL, NULL, NULL,
12982 &remote_set_cmdlist, &remote_show_cmdlist);
12983
c1e36e3e
PA
12984 add_setshow_boolean_cmd ("range-stepping", class_run,
12985 &use_range_stepping, _("\
12986Enable or disable range stepping."), _("\
12987Show whether target-assisted range stepping is enabled."), _("\
12988If on, and the target supports it, when stepping a source line, GDB\n\
12989tells the target to step the corresponding range of addresses itself instead\n\
12990of issuing multiple single-steps. This speeds up source level\n\
12991stepping. If off, GDB always issues single-steps, even if range\n\
12992stepping is supported by the target. The default is on."),
12993 set_range_stepping,
12994 show_range_stepping,
12995 &setlist,
12996 &showlist);
12997
449092f6
CV
12998 /* Eventually initialize fileio. See fileio.c */
12999 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 13000
ba348170 13001 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 13002 special ptids with it set to != 0. */
ba348170
PA
13003 magic_null_ptid = ptid_build (42000, -1, 1);
13004 not_sent_ptid = ptid_build (42000, -2, 1);
13005 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
13006
13007 target_buf_size = 2048;
13008 target_buf = xmalloc (target_buf_size);
c906108c 13009}
10760264 13010
This page took 2.835789 seconds and 4 git commands to generate.